-
Notifications
You must be signed in to change notification settings - Fork 638
/
Copy pathmonsters.cpp
3448 lines (2929 loc) · 97.6 KB
/
monsters.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) 1996-2001, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* This source code contains proprietary and confidential information of
* Valve LLC and its suppliers. Access to this code is restricted to
* persons who have executed a written SDK license with Valve. Any access,
* use or distribution of this code by or to any unlicensed person is illegal.
*
****/
/*
===== monsters.cpp ========================================================
Monster-related utility code
*/
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "nodes.h"
#include "monsters.h"
#include "animation.h"
#include "saverestore.h"
#include "weapons.h"
#include "scripted.h"
#include "squadmonster.h"
#include "decals.h"
#include "soundent.h"
#include "gamerules.h"
#define MONSTER_CUT_CORNER_DIST 8 // 8 means the monster's bounding box is contained without the box of the node in WC
Vector VecBModelOrigin( entvars_t* pevBModel );
extern DLL_GLOBAL BOOL g_fDrawLines;
extern DLL_GLOBAL short g_sModelIndexLaser;// holds the index for the laser beam
extern DLL_GLOBAL short g_sModelIndexLaserDot;// holds the index for the laser beam dot
extern CGraph WorldGraph;// the world node graph
// Global Savedata for monster
// UNDONE: Save schedule data? Can this be done? We may
// lose our enemy pointer or other data (goal ent, target, etc)
// that make the current schedule invalid, perhaps it's best
// to just pick a new one when we start up again.
TYPEDESCRIPTION CBaseMonster::m_SaveData[] =
{
DEFINE_FIELD( CBaseMonster, m_hEnemy, FIELD_EHANDLE ),
DEFINE_FIELD( CBaseMonster, m_hTargetEnt, FIELD_EHANDLE ),
DEFINE_ARRAY( CBaseMonster, m_hOldEnemy, FIELD_EHANDLE, MAX_OLD_ENEMIES ),
DEFINE_ARRAY( CBaseMonster, m_vecOldEnemy, FIELD_POSITION_VECTOR, MAX_OLD_ENEMIES ),
DEFINE_FIELD( CBaseMonster, m_flFieldOfView, FIELD_FLOAT ),
DEFINE_FIELD( CBaseMonster, m_flWaitFinished, FIELD_TIME ),
DEFINE_FIELD( CBaseMonster, m_flMoveWaitFinished, FIELD_TIME ),
DEFINE_FIELD( CBaseMonster, m_Activity, FIELD_INTEGER ),
DEFINE_FIELD( CBaseMonster, m_IdealActivity, FIELD_INTEGER ),
DEFINE_FIELD( CBaseMonster, m_LastHitGroup, FIELD_INTEGER ),
DEFINE_FIELD( CBaseMonster, m_MonsterState, FIELD_INTEGER ),
DEFINE_FIELD( CBaseMonster, m_IdealMonsterState, FIELD_INTEGER ),
DEFINE_FIELD( CBaseMonster, m_iTaskStatus, FIELD_INTEGER ),
//Schedule_t *m_pSchedule;
DEFINE_FIELD( CBaseMonster, m_iScheduleIndex, FIELD_INTEGER ),
DEFINE_FIELD( CBaseMonster, m_afConditions, FIELD_INTEGER ),
//WayPoint_t m_Route[ ROUTE_SIZE ];
// DEFINE_FIELD( CBaseMonster, m_movementGoal, FIELD_INTEGER ),
// DEFINE_FIELD( CBaseMonster, m_iRouteIndex, FIELD_INTEGER ),
// DEFINE_FIELD( CBaseMonster, m_moveWaitTime, FIELD_FLOAT ),
DEFINE_FIELD( CBaseMonster, m_vecMoveGoal, FIELD_POSITION_VECTOR ),
DEFINE_FIELD( CBaseMonster, m_movementActivity, FIELD_INTEGER ),
// int m_iAudibleList; // first index of a linked list of sounds that the monster can hear.
// DEFINE_FIELD( CBaseMonster, m_afSoundTypes, FIELD_INTEGER ),
DEFINE_FIELD( CBaseMonster, m_vecLastPosition, FIELD_POSITION_VECTOR ),
DEFINE_FIELD( CBaseMonster, m_iHintNode, FIELD_INTEGER ),
DEFINE_FIELD( CBaseMonster, m_afMemory, FIELD_INTEGER ),
DEFINE_FIELD( CBaseMonster, m_iMaxHealth, FIELD_INTEGER ),
DEFINE_FIELD( CBaseMonster, m_vecEnemyLKP, FIELD_POSITION_VECTOR ),
DEFINE_FIELD( CBaseMonster, m_cAmmoLoaded, FIELD_INTEGER ),
DEFINE_FIELD( CBaseMonster, m_afCapability, FIELD_INTEGER ),
DEFINE_FIELD( CBaseMonster, m_flNextAttack, FIELD_TIME ),
DEFINE_FIELD( CBaseMonster, m_bitsDamageType, FIELD_INTEGER ),
DEFINE_ARRAY( CBaseMonster, m_rgbTimeBasedDamage, FIELD_CHARACTER, CDMG_TIMEBASED ),
DEFINE_FIELD( CBaseMonster, m_bloodColor, FIELD_INTEGER ),
DEFINE_FIELD( CBaseMonster, m_failSchedule, FIELD_INTEGER ),
DEFINE_FIELD( CBaseMonster, m_flHungryTime, FIELD_TIME ),
DEFINE_FIELD( CBaseMonster, m_flDistTooFar, FIELD_FLOAT ),
DEFINE_FIELD( CBaseMonster, m_flDistLook, FIELD_FLOAT ),
DEFINE_FIELD( CBaseMonster, m_iTriggerCondition, FIELD_INTEGER ),
DEFINE_FIELD( CBaseMonster, m_iszTriggerTarget, FIELD_STRING ),
DEFINE_FIELD( CBaseMonster, m_HackedGunPos, FIELD_VECTOR ),
DEFINE_FIELD( CBaseMonster, m_scriptState, FIELD_INTEGER ),
DEFINE_FIELD( CBaseMonster, m_pCine, FIELD_CLASSPTR ),
};
//IMPLEMENT_SAVERESTORE( CBaseMonster, CBaseToggle );
int CBaseMonster::Save( CSave &save )
{
if ( !CBaseToggle::Save(save) )
return 0;
return save.WriteFields( "CBaseMonster", this, m_SaveData, ARRAYSIZE(m_SaveData) );
}
int CBaseMonster::Restore( CRestore &restore )
{
if ( !CBaseToggle::Restore(restore) )
return 0;
int status = restore.ReadFields( "CBaseMonster", this, m_SaveData, ARRAYSIZE(m_SaveData) );
// We don't save/restore routes yet
RouteClear();
// We don't save/restore schedules yet
m_pSchedule = NULL;
m_iTaskStatus = TASKSTATUS_NEW;
// Reset animation
m_Activity = ACT_RESET;
// If we don't have an enemy, clear conditions like see enemy, etc.
if ( m_hEnemy == NULL )
m_afConditions = 0;
return status;
}
//=========================================================
// Eat - makes a monster full for a little while.
//=========================================================
void CBaseMonster :: Eat ( float flFullDuration )
{
m_flHungryTime = gpGlobals->time + flFullDuration;
}
//=========================================================
// FShouldEat - returns true if a monster is hungry.
//=========================================================
BOOL CBaseMonster :: FShouldEat ( void )
{
if ( m_flHungryTime > gpGlobals->time )
{
return FALSE;
}
return TRUE;
}
//=========================================================
// BarnacleVictimBitten - called
// by Barnacle victims when the barnacle pulls their head
// into its mouth
//=========================================================
void CBaseMonster :: BarnacleVictimBitten ( entvars_t *pevBarnacle )
{
Schedule_t *pNewSchedule;
pNewSchedule = GetScheduleOfType( SCHED_BARNACLE_VICTIM_CHOMP );
if ( pNewSchedule )
{
ChangeSchedule( pNewSchedule );
}
}
//=========================================================
// BarnacleVictimReleased - called by barnacle victims when
// the host barnacle is killed.
//=========================================================
void CBaseMonster :: BarnacleVictimReleased ( void )
{
m_IdealMonsterState = MONSTERSTATE_IDLE;
pev->velocity = g_vecZero;
pev->movetype = MOVETYPE_STEP;
}
//=========================================================
// Listen - monsters dig through the active sound list for
// any sounds that may interest them. (smells, too!)
//=========================================================
void CBaseMonster :: Listen ( void )
{
int iSound;
int iMySounds;
float hearingSensitivity;
CSound *pCurrentSound;
m_iAudibleList = SOUNDLIST_EMPTY;
ClearConditions(bits_COND_HEAR_SOUND | bits_COND_SMELL | bits_COND_SMELL_FOOD);
m_afSoundTypes = 0;
iMySounds = ISoundMask();
if ( m_pSchedule )
{
//!!!WATCH THIS SPOT IF YOU ARE HAVING SOUND RELATED BUGS!
// Make sure your schedule AND personal sound masks agree!
iMySounds &= m_pSchedule->iSoundMask;
}
iSound = CSoundEnt::ActiveList();
// UNDONE: Clear these here?
ClearConditions( bits_COND_HEAR_SOUND | bits_COND_SMELL_FOOD | bits_COND_SMELL );
hearingSensitivity = HearingSensitivity( );
while ( iSound != SOUNDLIST_EMPTY )
{
pCurrentSound = CSoundEnt::SoundPointerForIndex( iSound );
if ( pCurrentSound &&
( pCurrentSound->m_iType & iMySounds ) &&
( pCurrentSound->m_vecOrigin - EarPosition() ).Length() <= pCurrentSound->m_iVolume * hearingSensitivity )
//if ( ( g_pSoundEnt->m_SoundPool[ iSound ].m_iType & iMySounds ) && ( g_pSoundEnt->m_SoundPool[ iSound ].m_vecOrigin - EarPosition()).Length () <= g_pSoundEnt->m_SoundPool[ iSound ].m_iVolume * hearingSensitivity )
{
// the monster cares about this sound, and it's close enough to hear.
//g_pSoundEnt->m_SoundPool[ iSound ].m_iNextAudible = m_iAudibleList;
pCurrentSound->m_iNextAudible = m_iAudibleList;
if ( pCurrentSound->FIsSound() )
{
// this is an audible sound.
SetConditions( bits_COND_HEAR_SOUND );
}
else
{
// if not a sound, must be a smell - determine if it's just a scent, or if it's a food scent
// if ( g_pSoundEnt->m_SoundPool[ iSound ].m_iType & ( bits_SOUND_MEAT | bits_SOUND_CARCASS ) )
if ( pCurrentSound->m_iType & ( bits_SOUND_MEAT | bits_SOUND_CARCASS ) )
{
// the detected scent is a food item, so set both conditions.
// !!!BUGBUG - maybe a virtual function to determine whether or not the scent is food?
SetConditions( bits_COND_SMELL_FOOD );
SetConditions( bits_COND_SMELL );
}
else
{
// just a normal scent.
SetConditions( bits_COND_SMELL );
}
}
// m_afSoundTypes |= g_pSoundEnt->m_SoundPool[ iSound ].m_iType;
m_afSoundTypes |= pCurrentSound->m_iType;
m_iAudibleList = iSound;
}
// iSound = g_pSoundEnt->m_SoundPool[ iSound ].m_iNext;
iSound = pCurrentSound->m_iNext;
}
}
//=========================================================
// FLSoundVolume - subtracts the volume of the given sound
// from the distance the sound source is from the caller,
// and returns that value, which is considered to be the 'local'
// volume of the sound.
//=========================================================
float CBaseMonster :: FLSoundVolume ( CSound *pSound )
{
return ( pSound->m_iVolume - ( ( pSound->m_vecOrigin - pev->origin ).Length() ) );
}
//=========================================================
// FValidateHintType - tells use whether or not the monster cares
// about the type of Hint Node given
//=========================================================
BOOL CBaseMonster :: FValidateHintType ( short sHint )
{
return FALSE;
}
//=========================================================
// Look - Base class monster function to find enemies or
// food by sight. iDistance is distance ( in units ) that the
// monster can see.
//
// Sets the sight bits of the m_afConditions mask to indicate
// which types of entities were sighted.
// Function also sets the Looker's m_pLink
// to the head of a link list that contains all visible ents.
// (linked via each ent's m_pLink field)
//
//=========================================================
void CBaseMonster :: Look ( int iDistance )
{
int iSighted = 0;
// DON'T let visibility information from last frame sit around!
ClearConditions(bits_COND_SEE_HATE | bits_COND_SEE_DISLIKE | bits_COND_SEE_ENEMY | bits_COND_SEE_FEAR | bits_COND_SEE_NEMESIS | bits_COND_SEE_CLIENT);
m_pLink = NULL;
CBaseEntity *pSightEnt = NULL;// the current visible entity that we're dealing with
// See no evil if prisoner is set
if ( !FBitSet( pev->spawnflags, SF_MONSTER_PRISONER ) )
{
CBaseEntity *pList[100];
Vector delta = Vector( iDistance, iDistance, iDistance );
// Find only monsters/clients in box, NOT limited to PVS
int count = UTIL_EntitiesInBox( pList, 100, pev->origin - delta, pev->origin + delta, FL_CLIENT|FL_MONSTER );
for ( int i = 0; i < count; i++ )
{
pSightEnt = pList[i];
// !!!temporarily only considering other monsters and clients, don't see prisoners
if ( pSightEnt != this &&
!FBitSet( pSightEnt->pev->spawnflags, SF_MONSTER_PRISONER ) &&
pSightEnt->pev->health > 0 )
{
// the looker will want to consider this entity
// don't check anything else about an entity that can't be seen, or an entity that you don't care about.
if ( IRelationship( pSightEnt ) != R_NO && FInViewCone( pSightEnt ) && !FBitSet( pSightEnt->pev->flags, FL_NOTARGET ) && FVisible( pSightEnt ) )
{
if ( pSightEnt->IsPlayer() )
{
if ( pev->spawnflags & SF_MONSTER_WAIT_TILL_SEEN )
{
CBaseMonster *pClient;
pClient = pSightEnt->MyMonsterPointer();
// don't link this client in the list if the monster is wait till seen and the player isn't facing the monster
if ( pSightEnt && !pClient->FInViewCone( this ) )
{
// we're not in the player's view cone.
continue;
}
else
{
// player sees us, become normal now.
pev->spawnflags &= ~SF_MONSTER_WAIT_TILL_SEEN;
}
}
// if we see a client, remember that (mostly for scripted AI)
iSighted |= bits_COND_SEE_CLIENT;
}
pSightEnt->m_pLink = m_pLink;
m_pLink = pSightEnt;
if ( pSightEnt == m_hEnemy )
{
// we know this ent is visible, so if it also happens to be our enemy, store that now.
iSighted |= bits_COND_SEE_ENEMY;
}
// don't add the Enemy's relationship to the conditions. We only want to worry about conditions when
// we see monsters other than the Enemy.
switch ( IRelationship ( pSightEnt ) )
{
case R_NM:
iSighted |= bits_COND_SEE_NEMESIS;
break;
case R_HT:
iSighted |= bits_COND_SEE_HATE;
break;
case R_DL:
iSighted |= bits_COND_SEE_DISLIKE;
break;
case R_FR:
iSighted |= bits_COND_SEE_FEAR;
break;
case R_AL:
break;
default:
ALERT ( at_aiconsole, "%s can't assess %s\n", STRING(pev->classname), STRING(pSightEnt->pev->classname ) );
break;
}
}
}
}
}
SetConditions( iSighted );
}
//=========================================================
// ISoundMask - returns a bit mask indicating which types
// of sounds this monster regards. In the base class implementation,
// monsters care about all sounds, but no scents.
//=========================================================
int CBaseMonster :: ISoundMask ( void )
{
return bits_SOUND_WORLD |
bits_SOUND_COMBAT |
bits_SOUND_PLAYER;
}
//=========================================================
// PBestSound - returns a pointer to the sound the monster
// should react to. Right now responds only to nearest sound.
//=========================================================
CSound* CBaseMonster :: PBestSound ( void )
{
int iThisSound;
int iBestSound = -1;
float flBestDist = 8192;// so first nearby sound will become best so far.
float flDist;
CSound *pSound;
iThisSound = m_iAudibleList;
if ( iThisSound == SOUNDLIST_EMPTY )
{
ALERT ( at_aiconsole, "ERROR! monster %s has no audible sounds!\n", STRING(pev->classname) );
#if _DEBUG
ALERT( at_error, "NULL Return from PBestSound\n" );
#endif
return NULL;
}
while ( iThisSound != SOUNDLIST_EMPTY )
{
pSound = CSoundEnt::SoundPointerForIndex( iThisSound );
if ( pSound && pSound->FIsSound() )
{
flDist = ( pSound->m_vecOrigin - EarPosition()).Length();
if ( flDist < flBestDist )
{
iBestSound = iThisSound;
flBestDist = flDist;
}
}
iThisSound = pSound->m_iNextAudible;
}
if ( iBestSound >= 0 )
{
pSound = CSoundEnt::SoundPointerForIndex( iBestSound );
return pSound;
}
#if _DEBUG
ALERT( at_error, "NULL Return from PBestSound\n" );
#endif
return NULL;
}
//=========================================================
// PBestScent - returns a pointer to the scent the monster
// should react to. Right now responds only to nearest scent
//=========================================================
CSound* CBaseMonster :: PBestScent ( void )
{
int iThisScent;
int iBestScent = -1;
float flBestDist = 8192;// so first nearby smell will become best so far.
float flDist;
CSound *pSound;
iThisScent = m_iAudibleList;// smells are in the sound list.
if ( iThisScent == SOUNDLIST_EMPTY )
{
ALERT ( at_aiconsole, "ERROR! PBestScent() has empty soundlist!\n" );
#if _DEBUG
ALERT( at_error, "NULL Return from PBestSound\n" );
#endif
return NULL;
}
while ( iThisScent != SOUNDLIST_EMPTY )
{
pSound = CSoundEnt::SoundPointerForIndex( iThisScent );
if ( pSound->FIsScent() )
{
flDist = ( pSound->m_vecOrigin - pev->origin ).Length();
if ( flDist < flBestDist )
{
iBestScent = iThisScent;
flBestDist = flDist;
}
}
iThisScent = pSound->m_iNextAudible;
}
if ( iBestScent >= 0 )
{
pSound = CSoundEnt::SoundPointerForIndex( iBestScent );
return pSound;
}
#if _DEBUG
ALERT( at_error, "NULL Return from PBestScent\n" );
#endif
return NULL;
}
//=========================================================
// Monster Think - calls out to core AI functions and handles this
// monster's specific animation events
//=========================================================
void CBaseMonster :: MonsterThink ( void )
{
pev->nextthink = gpGlobals->time + 0.1;// keep monster thinking.
RunAI();
float flInterval = StudioFrameAdvance( ); // animate
// start or end a fidget
// This needs a better home -- switching animations over time should be encapsulated on a per-activity basis
// perhaps MaintainActivity() or a ShiftAnimationOverTime() or something.
if ( m_MonsterState != MONSTERSTATE_SCRIPT && m_MonsterState != MONSTERSTATE_DEAD && m_Activity == ACT_IDLE && m_fSequenceFinished )
{
int iSequence;
if ( m_fSequenceLoops )
{
// animation does loop, which means we're playing subtle idle. Might need to
// fidget.
iSequence = LookupActivity ( m_Activity );
}
else
{
// animation that just ended doesn't loop! That means we just finished a fidget
// and should return to our heaviest weighted idle (the subtle one)
iSequence = LookupActivityHeaviest ( m_Activity );
}
if ( iSequence != ACTIVITY_NOT_AVAILABLE )
{
pev->sequence = iSequence; // Set to new anim (if it's there)
ResetSequenceInfo( );
}
}
DispatchAnimEvents( flInterval );
if ( !MovementIsComplete() )
{
Move( flInterval );
}
#if _DEBUG
else
{
if ( !TaskIsRunning() && !TaskIsComplete() )
ALERT( at_error, "Schedule stalled!!\n" );
}
#endif
}
//=========================================================
// CBaseMonster - USE - will make a monster angry at whomever
// activated it.
//=========================================================
void CBaseMonster :: MonsterUse ( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
{
m_IdealMonsterState = MONSTERSTATE_ALERT;
}
//=========================================================
// Ignore conditions - before a set of conditions is allowed
// to interrupt a monster's schedule, this function removes
// conditions that we have flagged to interrupt the current
// schedule, but may not want to interrupt the schedule every
// time. (Pain, for instance)
//=========================================================
int CBaseMonster :: IgnoreConditions ( void )
{
int iIgnoreConditions = 0;
if ( !FShouldEat() )
{
// not hungry? Ignore food smell.
iIgnoreConditions |= bits_COND_SMELL_FOOD;
}
if ( m_MonsterState == MONSTERSTATE_SCRIPT && m_pCine )
iIgnoreConditions |= m_pCine->IgnoreConditions();
return iIgnoreConditions;
}
//=========================================================
// RouteClear - zeroes out the monster's route array and goal
//=========================================================
void CBaseMonster :: RouteClear ( void )
{
RouteNew();
m_movementGoal = MOVEGOAL_NONE;
m_movementActivity = ACT_IDLE;
Forget( bits_MEMORY_MOVE_FAILED );
}
//=========================================================
// Route New - clears out a route to be changed, but keeps
// goal intact.
//=========================================================
void CBaseMonster :: RouteNew ( void )
{
m_Route[ 0 ].iType = 0;
m_iRouteIndex = 0;
}
//=========================================================
// FRouteClear - returns TRUE is the Route is cleared out
// ( invalid )
//=========================================================
BOOL CBaseMonster :: FRouteClear ( void )
{
if ( m_Route[ m_iRouteIndex ].iType == 0 || m_movementGoal == MOVEGOAL_NONE )
return TRUE;
return FALSE;
}
//=========================================================
// FRefreshRoute - after calculating a path to the monster's
// target, this function copies as many waypoints as possible
// from that path to the monster's Route array
//=========================================================
BOOL CBaseMonster :: FRefreshRoute ( void )
{
CBaseEntity *pPathCorner;
int i;
BOOL returnCode;
RouteNew();
returnCode = FALSE;
switch( m_movementGoal )
{
case MOVEGOAL_PATHCORNER:
{
// monster is on a path_corner loop
pPathCorner = m_pGoalEnt;
i = 0;
while ( pPathCorner && i < ROUTE_SIZE )
{
m_Route[ i ].iType = bits_MF_TO_PATHCORNER;
m_Route[ i ].vecLocation = pPathCorner->pev->origin;
pPathCorner = pPathCorner->GetNextTarget();
// Last path_corner in list?
if ( !pPathCorner )
m_Route[i].iType |= bits_MF_IS_GOAL;
i++;
}
}
returnCode = TRUE;
break;
case MOVEGOAL_ENEMY:
returnCode = BuildRoute( m_vecEnemyLKP, bits_MF_TO_ENEMY, m_hEnemy );
break;
case MOVEGOAL_LOCATION:
returnCode = BuildRoute( m_vecMoveGoal, bits_MF_TO_LOCATION, NULL );
break;
case MOVEGOAL_TARGETENT:
if (m_hTargetEnt != NULL)
{
returnCode = BuildRoute( m_hTargetEnt->pev->origin, bits_MF_TO_TARGETENT, m_hTargetEnt );
}
break;
case MOVEGOAL_NODE:
returnCode = FGetNodeRoute( m_vecMoveGoal );
// if ( returnCode )
// RouteSimplify( NULL );
break;
}
return returnCode;
}
BOOL CBaseMonster::MoveToEnemy( Activity movementAct, float waitTime )
{
m_movementActivity = movementAct;
m_moveWaitTime = waitTime;
m_movementGoal = MOVEGOAL_ENEMY;
return FRefreshRoute();
}
BOOL CBaseMonster::MoveToLocation( Activity movementAct, float waitTime, const Vector &goal )
{
m_movementActivity = movementAct;
m_moveWaitTime = waitTime;
m_movementGoal = MOVEGOAL_LOCATION;
m_vecMoveGoal = goal;
return FRefreshRoute();
}
BOOL CBaseMonster::MoveToTarget( Activity movementAct, float waitTime )
{
m_movementActivity = movementAct;
m_moveWaitTime = waitTime;
m_movementGoal = MOVEGOAL_TARGETENT;
return FRefreshRoute();
}
BOOL CBaseMonster::MoveToNode( Activity movementAct, float waitTime, const Vector &goal )
{
m_movementActivity = movementAct;
m_moveWaitTime = waitTime;
m_movementGoal = MOVEGOAL_NODE;
m_vecMoveGoal = goal;
return FRefreshRoute();
}
#ifdef _DEBUG
void DrawRoute( entvars_t *pev, WayPoint_t *m_Route, int m_iRouteIndex, int r, int g, int b )
{
int i;
if ( m_Route[m_iRouteIndex].iType == 0 )
{
ALERT( at_aiconsole, "Can't draw route!\n" );
return;
}
// UTIL_ParticleEffect ( m_Route[ m_iRouteIndex ].vecLocation, g_vecZero, 255, 25 );
MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY );
WRITE_BYTE( TE_BEAMPOINTS);
WRITE_COORD( pev->origin.x );
WRITE_COORD( pev->origin.y );
WRITE_COORD( pev->origin.z );
WRITE_COORD( m_Route[ m_iRouteIndex ].vecLocation.x );
WRITE_COORD( m_Route[ m_iRouteIndex ].vecLocation.y );
WRITE_COORD( m_Route[ m_iRouteIndex ].vecLocation.z );
WRITE_SHORT( g_sModelIndexLaser );
WRITE_BYTE( 0 ); // frame start
WRITE_BYTE( 10 ); // framerate
WRITE_BYTE( 1 ); // life
WRITE_BYTE( 16 ); // width
WRITE_BYTE( 0 ); // noise
WRITE_BYTE( r ); // r, g, b
WRITE_BYTE( g ); // r, g, b
WRITE_BYTE( b ); // r, g, b
WRITE_BYTE( 255 ); // brightness
WRITE_BYTE( 10 ); // speed
MESSAGE_END();
for ( i = m_iRouteIndex ; i < ROUTE_SIZE - 1; i++ )
{
if ( (m_Route[ i ].iType & bits_MF_IS_GOAL) || (m_Route[ i+1 ].iType == 0) )
break;
MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY );
WRITE_BYTE( TE_BEAMPOINTS );
WRITE_COORD( m_Route[ i ].vecLocation.x );
WRITE_COORD( m_Route[ i ].vecLocation.y );
WRITE_COORD( m_Route[ i ].vecLocation.z );
WRITE_COORD( m_Route[ i + 1 ].vecLocation.x );
WRITE_COORD( m_Route[ i + 1 ].vecLocation.y );
WRITE_COORD( m_Route[ i + 1 ].vecLocation.z );
WRITE_SHORT( g_sModelIndexLaser );
WRITE_BYTE( 0 ); // frame start
WRITE_BYTE( 10 ); // framerate
WRITE_BYTE( 1 ); // life
WRITE_BYTE( 8 ); // width
WRITE_BYTE( 0 ); // noise
WRITE_BYTE( r ); // r, g, b
WRITE_BYTE( g ); // r, g, b
WRITE_BYTE( b ); // r, g, b
WRITE_BYTE( 255 ); // brightness
WRITE_BYTE( 10 ); // speed
MESSAGE_END();
// UTIL_ParticleEffect ( m_Route[ i ].vecLocation, g_vecZero, 255, 25 );
}
}
#endif
int ShouldSimplify( int routeType )
{
routeType &= ~bits_MF_IS_GOAL;
if ( (routeType == bits_MF_TO_PATHCORNER) || (routeType & bits_MF_DONT_SIMPLIFY) )
return FALSE;
return TRUE;
}
//=========================================================
// RouteSimplify
//
// Attempts to make the route more direct by cutting out
// unnecessary nodes & cutting corners.
//
//=========================================================
void CBaseMonster :: RouteSimplify( CBaseEntity *pTargetEnt )
{
// BUGBUG: this doesn't work 100% yet
int i, count, outCount;
Vector vecStart;
WayPoint_t outRoute[ ROUTE_SIZE * 2 ]; // Any points except the ends can turn into 2 points in the simplified route
count = 0;
for ( i = m_iRouteIndex; i < ROUTE_SIZE; i++ )
{
if ( !m_Route[i].iType )
break;
else
count++;
if ( m_Route[i].iType & bits_MF_IS_GOAL )
break;
}
// Can't simplify a direct route!
if ( count < 2 )
{
// DrawRoute( pev, m_Route, m_iRouteIndex, 0, 0, 255 );
return;
}
outCount = 0;
vecStart = pev->origin;
for ( i = 0; i < count-1; i++ )
{
// Don't eliminate path_corners
if ( !ShouldSimplify( m_Route[m_iRouteIndex+i].iType ) )
{
outRoute[outCount] = m_Route[ m_iRouteIndex + i ];
outCount++;
}
else if ( CheckLocalMove ( vecStart, m_Route[m_iRouteIndex+i+1].vecLocation, pTargetEnt, NULL ) == LOCALMOVE_VALID )
{
// Skip vert
continue;
}
else
{
Vector vecTest, vecSplit;
// Halfway between this and next
vecTest = (m_Route[m_iRouteIndex+i+1].vecLocation + m_Route[m_iRouteIndex+i].vecLocation) * 0.5;
// Halfway between this and previous
vecSplit = (m_Route[m_iRouteIndex+i].vecLocation + vecStart) * 0.5;
int iType = (m_Route[m_iRouteIndex+i].iType | bits_MF_TO_DETOUR) & ~bits_MF_NOT_TO_MASK;
if ( CheckLocalMove ( vecStart, vecTest, pTargetEnt, NULL ) == LOCALMOVE_VALID )
{
outRoute[outCount].iType = iType;
outRoute[outCount].vecLocation = vecTest;
}
else if ( CheckLocalMove ( vecSplit, vecTest, pTargetEnt, NULL ) == LOCALMOVE_VALID )
{
outRoute[outCount].iType = iType;
outRoute[outCount].vecLocation = vecSplit;
outRoute[outCount+1].iType = iType;
outRoute[outCount+1].vecLocation = vecTest;
outCount++; // Adding an extra point
}
else
{
outRoute[outCount] = m_Route[ m_iRouteIndex + i ];
}
}
// Get last point
vecStart = outRoute[ outCount ].vecLocation;
outCount++;
}
ASSERT( i < count );
outRoute[outCount] = m_Route[ m_iRouteIndex + i ];
outCount++;
// Terminate
outRoute[outCount].iType = 0;
ASSERT( outCount < (ROUTE_SIZE*2) );
// Copy the simplified route, disable for testing
m_iRouteIndex = 0;
for ( i = 0; i < ROUTE_SIZE && i < outCount; i++ )
{
m_Route[i] = outRoute[i];
}
// Terminate route
if ( i < ROUTE_SIZE )
m_Route[i].iType = 0;
// Debug, test movement code
#if 0
// if ( CVAR_GET_FLOAT( "simplify" ) != 0 )
DrawRoute( pev, outRoute, 0, 255, 0, 0 );
// else
DrawRoute( pev, m_Route, m_iRouteIndex, 0, 255, 0 );
#endif
}
//=========================================================
// FBecomeProne - tries to send a monster into PRONE state.
// right now only used when a barnacle snatches someone, so
// may have some special case stuff for that.
//=========================================================
BOOL CBaseMonster :: FBecomeProne ( void )
{
if ( FBitSet ( pev->flags, FL_ONGROUND ) )
{
pev->flags -= FL_ONGROUND;
}
m_IdealMonsterState = MONSTERSTATE_PRONE;
return TRUE;
}
//=========================================================
// CheckRangeAttack1
//=========================================================
BOOL CBaseMonster :: CheckRangeAttack1 ( float flDot, float flDist )
{
if ( flDist > 64 && flDist <= 784 && flDot >= 0.5 )
{
return TRUE;
}
return FALSE;
}
//=========================================================
// CheckRangeAttack2
//=========================================================
BOOL CBaseMonster :: CheckRangeAttack2 ( float flDot, float flDist )
{
if ( flDist > 64 && flDist <= 512 && flDot >= 0.5 )
{
return TRUE;
}
return FALSE;
}
//=========================================================
// CheckMeleeAttack1
//=========================================================
BOOL CBaseMonster :: CheckMeleeAttack1 ( float flDot, float flDist )
{
// Decent fix to keep folks from kicking/punching hornets and snarks is to check the onground flag(sjb)
if ( flDist <= 64 && flDot >= 0.7 && m_hEnemy != NULL && FBitSet ( m_hEnemy->pev->flags, FL_ONGROUND ) )
{
return TRUE;
}
return FALSE;
}
//=========================================================
// CheckMeleeAttack2
//=========================================================
BOOL CBaseMonster :: CheckMeleeAttack2 ( float flDot, float flDist )
{
if ( flDist <= 64 && flDot >= 0.7 )
{
return TRUE;
}
return FALSE;
}
//=========================================================
// CheckAttacks - sets all of the bits for attacks that the
// monster is capable of carrying out on the passed entity.
//=========================================================
void CBaseMonster :: CheckAttacks ( CBaseEntity *pTarget, float flDist )
{
Vector2D vec2LOS;
float flDot;