-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVATAR.CPP
3209 lines (2911 loc) · 83 KB
/
AVATAR.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ========================================================================
Copyright (c) 1990,1996 Synergistic Software
All Rights Reserved
========================================================================
Filename: Avatar.cpp
Author: Craig Clayton
======================================================================== */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include "AVATAR.HXX"
#include "TYPEDEFS.H"
#include "ENGINE.H"
#include "COMBOPTS.HXX"
#include "GAMEMAP.HXX"
#include "GMENUENM.H"
#include "THINGTYP.H"
#include "PLAYSTAT.HXX"
#include "PLAYHIT.HXX"
#include "INVNTORY.HXX"
#include "SPECIAL.H"
#include "HANDLE.HXX"
#include "UNITS.HXX"
#include "SCENE.HXX"
#define FALL_DIVISOR 128
#define DAMAGE_FRAME 5
#define LAVA_DAMAGE 1
#define ACID_DAMAGE 1
#define POISON_DAMAGE 1
SHORT CAvatar::PlayerAvatarHandle=fERROR;
const LONG CAvatar::ActivationDistance = 1000;
const LONG CAvatar::fgTauntDelay = 50;
// [d11-05-96 JPC] Experiment for keeping dead things from clipping so badly.
#define EXTRATHINGS 0 // conform to #define in THINGS.C
#if EXTRATHINGS
extern "C" {
void AddThingToAdjacentSubsectors (LONG ThingIndex);
}
#endif
void CAvatar::SetAIFuncIndex(AIFUNC aif)
{
AIFuncIndex = aif;
switch (aif)
{
case AI_FUNC_BATTLE_CAPTAIN:
pAvatarFunc = BattleCapt;
break;
case AI_FUNC_FOLLOW_PATH:
pAvatarFunc = FollowPath;
break;
case AI_FUNC_PING_PONG:
pAvatarFunc = PingPong;
break;
case AI_FUNC_FOLLOW_PLAYER:
pAvatarFunc = FollowPlayer;
break;
case AI_FUNC_FOLLOW_BTLCAP:
pAvatarFunc = FollowBtlCap;
break;
case AI_FUNC_SPIDERS:
pAvatarFunc = Spiders;
break;
case AI_FUNC_HARPIES:
pAvatarFunc = Harpies;
break;
case AI_FUNC_WYVERNS:
pAvatarFunc = Wyverns;
break;
case AI_FUNC_HELLHOUND:
pAvatarFunc = HellHounds;
break;
case AI_FUNC_FIREBALL:
pAvatarFunc = FireBall;
break;
case AI_FUNC_SPECTRE:
pAvatarFunc = Spectres;
break;
case AI_FUNC_STUNNED:
pAvatarFunc = Stunned;
break;
case AI_FUNC_GARGOYLES:
pAvatarFunc = Gargoyles;
break;
case AI_FUNC_HORSE:
pAvatarFunc = Horse;
break;
case AI_FUNC_NULL:
default:
pAvatarFunc = 0;
break;
}
}
/* ========================================================================
Function - MoveToward
Description - Move to withing BUMP distance from a point by my Rate amount
Returns - distance from point
======================================================================== */
LONG CAvatar::MoveToward(
LONG lTargetX,
LONG lTargetY,
LONG Bump,
LONG Rate
)
{
LONG dx = lTargetX - mfX();
LONG dy = lTargetY - mfY();
LONG h = dist( mfX(), mfY(), lTargetX, lTargetY );
if(abs(h) > Bump)
{
LONG NewX;
LONG NewY;
if (h > 0)
h -= Bump;
else
h += Bump;
FaceTo(lTargetX, lTargetY );
if (Bump > 2)
{
// Introduce some randomness as to exactly where
// we endup within "Bump" distance of the target.
dx += random(Bump - 1);
dy += random(Bump - 1);
}
if (abs(h) < Rate)
{
NewX = mfX() + dx;
NewY = mfY() + dy;
}
else
{
NewX = mfX() + ((dx * Rate)/h);
NewY = mfY() + ((dy * Rate)/h);
}
mfMoveTo(NewX, NewY);
}
else
{
h = 0;
}
return h;
}
/* ========================================================================
Function - NewAvatar
Description - Allocates memory and initializes it for an avatar.
Returns - Handle to the avatar.
======================================================================== */
SHORT CAvatar::NewAvatar(void)
{
SHORT hAvatar = NewLockedBlock(sizeof(CAvatar));
if (hAvatar != fERROR)
{
LONG i;
CAvatar *pAvatar = (CAvatar *) BLKPTR(hAvatar);
pAvatar->hThis = hAvatar; // Self reference pointer needed to attach to other things.
pAvatar->attrib.RuntimeCreated = FALSE;
pAvatar->attrib.SceneInstance = FALSE;
pAvatar->attrib.Engaged = FALSE;
pAvatar->attrib.IsFlying = FALSE;
pAvatar->attrib.IsFeathered = FALSE;
pAvatar->attrib.CanSeeInDark = FALSE;
pAvatar->attrib.CanSeeInfra = FALSE;
pAvatar->attrib.IssuedChallange = FALSE;
pAvatar->attrib.IssuedTaunt = FALSE;
pAvatar->attrib.IsFollowPath = FALSE;
pAvatar->attrib.KilledBySpectre = FALSE;
pAvatar->attrib.AttackMode = FIGHT_SEQUENCE::ATM_NONE;
pAvatar->attrib.PlayedFightSnd = FALSE;
pAvatar->attrib.IsJumpEnhanced = FALSE;
pAvatar->attrib.DiedLastFrame = FALSE;
pAvatar->attrib.BeingControlled = FALSE;
pAvatar->Version = NULL;
pAvatar->Id = NULL;
pAvatar->Status = CAvatar::AI_INIT;
pAvatar->hPlayerStats = fERROR;
pAvatar->Realm.HomeRealm = REALM::NO_COUNTRY;
pAvatar->Engaged = 0;
pAvatar->hEnemy = fERROR;
pAvatar->KillType = NO_THING;
pAvatar->fDamage = -1;
pAvatar->fAttackTurnFlag = ATTACK_TURN_NOT_DECIDED;
pAvatar->fDamageFlag = 0;
pAvatar->fOriginalRemapColor = 0;
pAvatar->fHighlightEndTime = ULONG_MAX;
pAvatar->fContactDistance = 0;
pAvatar->UnitIndex = -1;
for (i = 0; i < MAX_ATTACK_ROLLS; ++i)
pAvatar->fAttackRolls[i] = 0;
pAvatar->fFightSeq.mfInit();
pAvatar->CurSequence= INITSEQ;
pAvatar->SetAIFuncIndex(AI_FUNC_NULL);
pAvatar->fPrev.Sequence = INITSEQ;
pAvatar->fPrev.A = 0;
pAvatar->fPrev.Status = pAvatar->Status;
// Initialize the snd array.
memset(&(pAvatar->SndData[0].SndId), 0, MAX_SND_QUEUE*sizeof(AVATAR_SND_DATA));
}
return hAvatar; // return handle
}
/* ========================================================================
Function - mfCreateFireBall
Description - create a fireball
Resturns - void
======================================================================== */
void CAvatar::mfCreateFireBall(
LONG Id,
THINGTYPE Type,
LONG DamageLevel) const
{
PTR_SCENE pCurrentScene = (PTR_SCENE) BLKPTR(SCENE_MGR::hCurrentScene);
POINT PtInFront;
LONG Angle;
PtInFront.x = 0;
PtInFront.y = 2 * mfWidth();
Rotate(&PtInFront, mfAngle());
PtInFront.x += mfX();
PtInFront.y += mfY();
if(hEnemy != fERROR)
{
CAvatar const * const pEnemy = (CAvatar const * const)BLKPTR(hEnemy);
Angle = AngleFromPoint(PtInFront.x,
PtInFront.y,
pEnemy->mfX(), pEnemy->mfY(), RESOLUTION_4);
}
else
{
CAvatar const * const pAvatar = (CAvatar const * const)BLKPTR(hThis);
Angle = pAvatar->mfAngle();
}
LONG const i=CAvatar::CreateAvatar(Id, Type,
PtInFront.x,
PtInFront.y,
Angle,
AI_FUNC_FIREBALL, pCurrentScene,
Realm.HomeRealm);
if ( i >= 0)
{
CAvatar * pFireball = (CAvatar *) BLKPTR (pCurrentScene->Avatars[i]);
pFireball->mfMoveToXYZA(pFireball->mfX(), pFireball->mfY(), mfZ()+mfHeight()-33, pFireball->mfAngle());
pFireball->fDamage = DamageLevel;
pFireball->ThingType = Type;
pFireball->hEnemy=hEnemy;
pFireball->fFireBall.hWhoCast = hThis;
pFireball->fDamageFlag = 1;
}
#if defined (_DEBUG)
else
{
fatal_error("CreateFireBall ERROR! Unable to create fireball.\n");
}
#endif
}
/* ========================================================================
Function - mfCreateFireBall
Description - create a fireball
Resturns - void
======================================================================== */
void CAvatar::mfCreateHorse(
LONG Id,
THINGTYPE Type ) const
{
PTR_SCENE pCurrentScene = (PTR_SCENE) BLKPTR(SCENE_MGR::hCurrentScene);
LONG Angle;
CAvatar const * const pAvatar = (CAvatar const * const)BLKPTR(hThis);
Angle = 32;
LONG const i=CAvatar::CreateAvatar( Id, Type,
pAvatar->mfX(),
pAvatar->mfY(),
Angle,
AI_FUNC_HORSE,
pCurrentScene,
REALM::NO_COUNTRY);
if ( i >= 0)
{
CAvatar * pHorse = (CAvatar *) BLKPTR (pCurrentScene->Avatars[i]);
pHorse->ThingType = Type;
pHorse->hEnemy=fERROR;
}
#if defined (_DEBUG)
else
{
fatal_error("CreateHorse ERROR! Unable to create Horse.\n");
}
#endif
}
/* ========================================================================
Function - mfInflictMagicDamage
Description - checks to see if a kill has occured then checks resistances
Returns - TRUE for fatal, FALSE for alive
======================================================================== */
BOOL CAvatar::mfInflictMagicDamage(
CAvatar * const pEnemy,
THINGTYPE const NewKillType,
LONG const Damage)
{
SHORT result = 0;
if(NewKillType != NO_THING &&
!mfAmIImmoblized() &&
pEnemy->Status != AI_DEAD)
{
if (pEnemy->AIFuncIndex == AI_FUNC_BATTLE_CAPTAIN)
{
pEnemy->KillType = NewKillType;
pEnemy->fDamage = Damage;
}
else
{
SET_NOT_FRIENDLY(Realm.HomeRealm, pEnemy->Realm.HomeRealm);
if (pEnemy->hPlayerStats == fERROR ||
hPlayerStats == fERROR)
{
pEnemy->Status = AI_DEAD;
pEnemy->mfPlayAnimationOnce(EXPIRESEQ);
result = Damage;
}
else
{
DumbAutoLockPtr<PLAYER_STATS> const pPlayerStats(hPlayerStats);
DumbAutoLockPtr<PLAYER_STATS> const pEnemyStats(pEnemy->hPlayerStats);
PLAYER_HIT PlayerHitObj(pPlayerStats, pEnemyStats);
result = fDamage = PlayerHitObj.mfInflictMagicDamageOnTarget( NewKillType,
Damage);
// check to see if this kills the enemy
if(pEnemyStats->mfGetCurHitPoints() <= 0)
{
if (pEnemy->Status == AI_STONE)
{
mfKillAvatar(pEnemy);
pEnemy->mfPlayAnimationOnce(ATTACK1SEQ);
pEnemy->mfSetType(CRUMBLE_1);
}
else
{
mfKillAvatar(pEnemy);
// [d11-27-96 JPC] Removed MAGIC_MISSILE_1 test per Gary's request.
if (NewKillType == FIREBALL_1)
{
if (GAME_TTYPE::mfIsHuman(pEnemy->mfType()))
{
pEnemy->mfPlayAnimationOnce(ATTACK1SEQ);
}
else
{
pEnemy->mfPlayAnimationOnce(EXPIRESEQ);
}
pEnemy->mfSetType( NewKillType);
pEnemy->SetAIFuncIndex( CAvatar::AI_FUNC_FIREBALL );
}
else
if (NewKillType == CRUMBLE_1 ||
NewKillType == VAPORIZE_1 )
{
pEnemy->mfPlayAnimationOnce(ATTACK1SEQ);
pEnemy->mfSetType( NewKillType);
}
}
}
else
if (result > 3)
{
pEnemy->mfPainSounds();
}
}
}
}
fDamageFlag = 0;
return result;
}
/* ========================================================================
Function - DeleteAvatar
Description - Delete an avatar from the list and release its memory.
Returns - void
======================================================================== */
void CAvatar::DeleteAvatar(SHORT hAvatar)
{
CAvatar *pAvatar = (CAvatar *) BLKPTR(hAvatar);
PTR_SCENE pCurrentScene = (PTR_SCENE) BLKPTR(SCENE_MGR::hCurrentScene);
LONG i;
for(i=0; i<MAX_AVATARS&&pCurrentScene->Avatars[i]!=pAvatar->hThis; ++i)
{}
pCurrentScene->Avatars[i]=fERROR;
if (pAvatar->UnitIndex != -1)
units[pAvatar->UnitIndex].iHandle = fERROR;
// give the AI a chance to throw it's stuff away
switch(pAvatar->GetAIFuncIndex())
{
case AI_FUNC_FOLLOW_PATH:
pAvatar->fFollowPath.mfDelete();
break;
case AI_FUNC_FOLLOW_PLAYER:
pAvatar->fFollowPlayer.mfDelete();
break;
case AI_FUNC_BATTLE_CAPTAIN:
pAvatar->fBtlCap.mfDelete();
break;
}
// disable this in the things array
purge_thing(pAvatar->ThingIndex);
remove_thing(pAvatar->ThingIndex);
// get rid of his stats
if (pAvatar->hPlayerStats != fERROR)
{
DumbHandlePtr<PLAYER_STATS> const pPlayerStats(pAvatar->hPlayerStats);
pPlayerStats->mfDelete();
if (pAvatar->attrib.RuntimeCreated)
{
DisposBlock(pAvatar->hPlayerStats);
}
pAvatar->hPlayerStats = fERROR; // just incase something else looks here.
}
// get rid of the memory
DisposBlock(hAvatar);
}
/* ========================================================================
Function - CreateAvatar
Description - Create an avatar of a given type at given location
Returns - index in Avatars array or fERROR if unable to create.
======================================================================== */
LONG CAvatar::CreateAvatar(
LONG Id,
THINGTYPE Type,
LONG X,
LONG Y,
LONG A,
CAvatar::AIFUNC FuncIndex,
PTR_SCENE pScene,
const REALM::REALM_TYPE Realm
)
{
LONG i;
// find an empty Avatar slot, assume for now that we never run out
// if find a NULL pointer, allocate space for AVATAR structure
for (i=0; i<MAX_AVATARS && pScene->Avatars[i] != fERROR; ++i)
{}
if (i >= MAX_AVATARS)
{
i = fERROR; // We have no more slots!
printf("WARNING! Out of Avatar slots!\n");
}
else
{
LONG lMemoryAvail = ReportFreeMem(TRUE);
printf("Memory available before creating avatar %ld\n", lMemoryAvail);
// create the avatar
pScene->Avatars[i] = CAvatar::NewAvatar();
if (pScene->Avatars[i] != fERROR)
{
printf("Creating Avatar Id = %ld\n", Id);
//GEH SetBlockAttr(pScene->Avatars[i],LOCKED, LOCKED);
CAvatar *pAvatar = (CAvatar *) BLKPTR(pScene->Avatars[i]);
pAvatar->Id = Id;
pAvatar->UnitIndex = -1;
pAvatar->Status = CAvatar::AI_INIT;
pAvatar->mfDefineCThing(Type, X, Y, point_to_floor_height(X,Y), A, 0, 0, 0);
pAvatar->SetAIFuncIndex(FuncIndex);
pAvatar->mfInitVals();
pAvatar->Realm.HomeRealm = Realm;
pAvatar->mfSetOriginalRemapColor(Realm);
printf("Used memory = %ld\n", (lMemoryAvail - ReportFreeMem(TRUE)));
//GEH ClrLock(pScene->Avatars[i]);
}
else
{
printf("WARNING! Unable to create Avatar Id = %ld\n", Id);
i = fERROR;
}
}
return( i );
}
/* ========================================================================
Function - mfRotateToward
Description - Given an angle generate a new angle that is closer to it.
Call this fn until you are at your requested angle.
Returns - New angle
======================================================================== */
LONG CAvatar::mfRotateToward(LONG CurrentAngle,
LONG TargetAngle)
{
LONG da;
LONG lNewAngle;
da = CurrentAngle - TargetAngle;
if (da)
{
const LONG TurnRate = 15;
if (da < 0)
da += 256;
if (da >= 128)
{
lNewAngle = (CurrentAngle + MIN(abs(da),TurnRate)) % 256;
}
else
{
lNewAngle = CurrentAngle - MIN(abs(da),TurnRate);
if (lNewAngle < 0)
lNewAngle += 256;
}
}
else
{
lNewAngle = TargetAngle;
}
mfSetAngle(lNewAngle);
return lNewAngle;
}
/* ========================================================================
Function - mfCheckSnds
Description - check to see if this sequence/frame fires a sound
Returns - void
======================================================================== */
void CAvatar::mfCheckSnds()
{
for (LONG i = 0; i < MAX_SND_QUEUE; ++i)
{
// if the Trigger is valid, check if we have passed it
if(SCENE_MGR::gFrame > SndData[i].TriggerFrame)
{
// Play the sound.
if(SndData[i].SndId != 0)
{
AddSndObj(
SndData[i].SndId,
SndData[i].Range,
ThingIndex);
}
// clear the tick
if(SndData[i].Loop == FALSE)
SndData[i].TriggerFrame = MAX_TICK;
else
SndData[i].TriggerFrame += SndData[i].TotalFrames;
}
}
}
/* ========================================================================
Function - mfQueueSnd
Description - Queue up a sound to be played at some future frame cnt.
If the queue is full, it won't be added.
Returns -
======================================================================== */
void CAvatar::mfQueueSnd(BIRTHRT_SND const SndId,
SHORT const range,
LONG const inNumbFrames)
{
for (SHORT i = 0; i < MAX_SND_QUEUE; ++i)
{
if (SndData[i].SndId == SndId && SndData[i].TriggerFrame != MAX_TICK)
return; // HACK, HACK, HACK!!!!!!!!
}
for (SHORT i = 0; i < MAX_SND_QUEUE; ++i)
{
if (SndData[i].TriggerFrame == MAX_TICK)
{
SndData[i].TriggerFrame = SCENE_MGR::gFrame + inNumbFrames;
SndData[i].SndId = SndId;
SndData[i].Range = range;
SndData[i].Loop = FALSE;
break;
}
}
}
/* ========================================================================
Function -mfSetLoopingSnd
Description -loop sound, the sound has to be in the queue already
Returns -
======================================================================= */
void CAvatar::mfSetLoopingSnd(BIRTHRT_SND const SndId, SBYTE totalFrames)
{
for(LONG i = 0; i< MAX_SND_QUEUE; ++i)
{
if(SndData[i].SndId == SndId )
{
SndData[i].Loop = TRUE;
SndData[i].TotalFrames = totalFrames;
}
}
}
/* ========================================================================
Function - mfClearLoopingSnd
Description - stop the looping sound
Returns -
========================================================================*/
void CAvatar::mfClearLoopingSnd(BIRTHRT_SND const SndId)
{
for(LONG i=0; i<MAX_SND_QUEUE; ++i)
{
if(SndData[i].SndId == SndId)
{
SndData[i].Loop = FALSE;
SndData[i].TriggerFrame = MAX_TICK;
break;
}
}
}
/* ========================================================================
Function - mfClearAllLoopingSnds
Description - stop All of the looping sounds
Returns -
========================================================================*/
void CAvatar::mfClearAllLoopingSnds()
{
for(LONG i=0; i<MAX_SND_QUEUE; ++i)
{
if(SndData[i].Loop == TRUE)
{
SndData[i].Loop = FALSE;
SndData[i].TriggerFrame = MAX_TICK;
break;
}
}
}
/* ========================================================================
Function - AvatarIdToHdl
Description - find the Id, return the hThis
Note: If you can store the handle not the ID. Unless the
game could kill off the avatar and not notify you.
Returns -
======================================================================== */
SHORT AvatarIdToHdl ( LONG Id )
{
SHORT i;
SHORT hdl = (SHORT)fERROR; // assume id not found
PTR_SCENE pCurrentScene = 0;
pCurrentScene = (PTR_SCENE) BLKPTR(SCENE_MGR::hCurrentScene);
for(i=0;i<MAX_AVATARS;++i)
{
if (pCurrentScene->Avatars[i] != fERROR)
{
CAvatar const * const pA = (CAvatar const * const) BLKPTR(pCurrentScene->Avatars[i]);
if(pA->Id == Id)
{
hdl = pA->hThis;
break;
}
}
}
return hdl;
}
/* ========================================================================
Function - FindClosestFoe
Description - Search the entire Avatar list and find the closest unengaged
foe. Of the 5 closest foes, pick the closest one with the
least number of engagements.
Note: This may find that the nearest foe is the one behind
a wall, and thus we may want to check the visibilty flag.
Returns - handle to that avatar.
======================================================================== */
#define MAX_CLOSEST 5
// just find the closest enemy.
HDL_AVATAR CAvatar::mfFindClosestFoe() const
{
PTR_SCENE pCurrentScene = (PTR_SCENE) BLKPTR(SCENE_MGR::hCurrentScene);
SHORT i;
HDL_AVATAR Result = fERROR;
LONG prevDistance = LONG_MAX;
for (i = 0; i < MAX_AVATARS; ++i)
{
if (pCurrentScene->Avatars[i] != fERROR)
{
CAvatar const * const pAvatar = (CAvatar const * const) BLKPTR(pCurrentScene->Avatars[i]);
// Check that its first not me.
// And not Immoblized
// And not my ally
if (hThis != pCurrentScene->Avatars[i] &&
!pAvatar->mfAmIImmoblized() &&
!mfIsAlly(pAvatar) &&
pAvatar->AIFuncIndex != AI_FUNC_FIREBALL
)
{
// if I can see his sector, check to see if he's closest
if(reject( mfGetSector(), pAvatar->mfGetSector()))
{
LONG const distance = aprox_dist(mfX(), mfY(),
pAvatar->mfX(), pAvatar->mfY());
// See if we are smaller than the previous distance.
if (distance < prevDistance)
{
Result = pCurrentScene->Avatars[i];
prevDistance = distance;
}
}
}
}
}
return Result;
}
/* ========================================================================
Function - FindClosestVisibleFoe
Description - The foe's must be visible to you to "count."
Returns - Handle to the foe.
======================================================================== */
// just find the closest enemy.
HDL_AVATAR CAvatar::mfFindClosestVisibleFoe() const
{
PTR_SCENE pCurrentScene = (PTR_SCENE) BLKPTR(SCENE_MGR::hCurrentScene);
SHORT i;
HDL_AVATAR Result = fERROR;
LONG prevDistance = LONG_MAX;
HDL_AVATAR HackOnMe = fERROR;
LONG prevHackOnMeDistance = LONG_MAX;
for (i = 0; i < MAX_AVATARS; ++i)
{
if (pCurrentScene->Avatars[i] != fERROR)
{
CAvatar const * const pAvatar = (CAvatar const * const) BLKPTR(pCurrentScene->Avatars[i]);
// Check that its first not me.
// And not Immoblized
// And not my ally
if (hThis != pCurrentScene->Avatars[i] &&
!pAvatar->mfAmIImmoblized() &&
!mfIsAlly(pAvatar) &&
pAvatar->AIFuncIndex != AI_FUNC_FIREBALL
)
{
// if I can see his sector, check to see if he's closest
if(reject( mfGetSector(), pAvatar->mfGetSector()))
{
LONG const distance = aprox_dist(mfX(), mfY(),
pAvatar->mfX(), pAvatar->mfY());
if (pAvatar->hEnemy == hThis && Engaged > 0)
{
if (distance < prevHackOnMeDistance)
{
HackOnMe = pCurrentScene->Avatars[i];
prevHackOnMeDistance = distance;
}
}
// See if we are smaller than the previous distance.
if (distance < prevDistance)
{
PLAYER TempPlayer;
LONG Angle;
LONG BumpDistance;
Angle = AngleFromPoint2 ( mfX(), mfY(),
pAvatar->mfX(),
pAvatar->mfY(), RESOLUTION_1);
TempPlayer.x = mfX() << PLAYER_FIXEDPT;
TempPlayer.y = mfY() << PLAYER_FIXEDPT;
TempPlayer.z = mfZ() + 32;
TempPlayer.p = 0;
TempPlayer.a = mfAngle();
TempPlayer.Flying = TRUE;
TempPlayer.w = 16;
TempPlayer.h = 16;
TempPlayer.bump = iNOTHING;
TempPlayer.ThingIndex = fERROR;
CheckLongMove(&TempPlayer,
Angle,
distance,
0,
pAvatar->mfZ(),
&BumpDistance);
switch(TempPlayer.bump)
{
case iNOTHING:
case iSHALLOW_WATER:
case iDEEP_WATER:
case iLAVA:
case iACID:
Result = pCurrentScene->Avatars[i];
prevDistance = distance;
break;
case iFLOOR:
if (GAME_TTYPE::mfCanFly(mfType()))
{
Result = pCurrentScene->Avatars[i];
prevDistance = distance;
}
break;
}
}
}
}
}
}
// If I for some reason can't see the avatar wacking on me,
// wack back at the one who is hitting me. -GWP-
if (Result == fERROR)
{
Result = HackOnMe;
}
return Result;
}
/* ========================================================================
Function - MoveTowardWithBump
Description - Move to withing BUMP distance from a point by my Rate amount
Returns - distance from point
======================================================================== */
WadThingType CAvatar::mfMoveTowardWithBump(
LONG distance,
LONG TargetX,
LONG TargetY,
LONG Bump,
LONG Rate,
SHORT sSpecial,
LONG *pAngle,
LONG *pmyThingBumped
)
{
PLAYER TempPlayer;
LONG BumpDistance;
WadThingType bumpResult; // [d11-30-96 JPC] we need to manipulate this for flying things
LONG bumpObjectType;
if(abs(distance) > Bump)
{
FIXED_VECTOR_3D NewVector;
LONG dx = TargetX - mfX();
LONG dy = TargetY - mfY();
if (distance > 0)
distance -= Bump;
else
distance += Bump;
if (Bump > 2)
{
// Introduce some randomness as to exactly where
// we endup within "Bump" distance of the target.
// Determine whether we are to the N/S of the target or E/W
// So we woble on the short axis.
if (ABS(dx) > ABS(dy))
{
if (random(2))
{
dy += random(Bump - 1);
}
else
{
dy -= random(Bump - 1);
}
}
else
{
if (random(2))
{
dx += random(Bump - 1);
}
else
{
dx -= random(Bump - 1);
}
}
}
FaceTo(TargetX, TargetY);
mfConvertPositionToPlayer(TempPlayer);
if (ABS(distance) < Rate)
{
NewVector.dx = dx;
NewVector.dy = dy;
}
else
{
NewVector.dx = ((dx * Rate)/distance);
NewVector.dy = ((dy * Rate)/distance);
}
NewVector.dx <<= PLAYER_FIXEDPT;
NewVector.dy <<= PLAYER_FIXEDPT;
NewVector.dz = 0;
// For now ignore the noise we make if we hit something.
// GWP optimize by not doing this check if we're not "visible."
// GWP if we can't be seen bumping objects who cares?
//if (mfIsVisible())
{
bumpObjectType = CheckBump(&TempPlayer, &NewVector, Rate);
}
bumpResult = CheckMove(&TempPlayer, &NewVector, sSpecial, pAngle, &BumpDistance);
// [d11-30-96 JPC] For a flying thing, treat various nasty sectors as nothing.
if (attrib.IsFlying)
{
if (bumpResult == iLAVA ||
bumpResult == iSHALLOW_WATER ||
bumpResult == iDEEP_WATER ||
bumpResult == iHOLE ||
bumpResult == iACID)
bumpResult = iNOTHING;
}
if (bumpResult == iNOTHING &&
bumpObjectType != 0)
{
bumpResult = iOBJECT;
TempPlayer.bump = iOBJECT;
}
switch (bumpResult)
{
case iOBJECT:
//case iSLIDE_ON_WALL: // GWP Seems broken.
case iSHALLOW_WATER:
case iNOTHING:
// [d11-26-96 JPC] Before doing the move, make SURE we are not
// moving into a nasty sector.
if (attrib.IsFlying || !mfIsNastySector (mfX() + PLAYER_INT_VAL(NewVector.dx),
mfY() + PLAYER_INT_VAL(NewVector.dy)))
{
mfMoveToXYA(mfX() + PLAYER_INT_VAL(NewVector.dx),