-
Notifications
You must be signed in to change notification settings - Fork 0
/
aflock.cpp
925 lines (778 loc) · 23.7 KB
/
aflock.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
/***
*
* 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.
*
****/
//=========================================================
//=========================================================
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "monsters.h"
#include "squadmonster.h"
#define AFLOCK_MAX_RECRUIT_RADIUS 1024
#define AFLOCK_FLY_SPEED 125
#define AFLOCK_TURN_RATE 75
#define AFLOCK_ACCELERATE 10
#define AFLOCK_CHECK_DIST 192
#define AFLOCK_TOO_CLOSE 100
#define AFLOCK_TOO_FAR 256
//=========================================================
//=========================================================
class CFlockingFlyerFlock : public CBaseMonster
{
public:
void Spawn() override;
void Precache() override;
bool KeyValue(KeyValueData* pkvd) override;
void SpawnFlock();
bool Save(CSave& save) override;
bool Restore(CRestore& restore) override;
static TYPEDESCRIPTION m_SaveData[];
// Sounds are shared by the flock
static void PrecacheFlockSounds();
int m_cFlockSize;
float m_flFlockRadius;
};
TYPEDESCRIPTION CFlockingFlyerFlock::m_SaveData[] =
{
DEFINE_FIELD(CFlockingFlyerFlock, m_cFlockSize, FIELD_INTEGER),
DEFINE_FIELD(CFlockingFlyerFlock, m_flFlockRadius, FIELD_FLOAT),
};
IMPLEMENT_SAVERESTORE(CFlockingFlyerFlock, CBaseMonster);
//=========================================================
//=========================================================
class CFlockingFlyer : public CBaseMonster
{
public:
void Spawn() override;
void Precache() override;
void SpawnCommonCode();
void EXPORT IdleThink();
void BoidAdvanceFrame();
void EXPORT FormFlock();
void EXPORT Start();
void EXPORT FlockLeaderThink();
void EXPORT FlockFollowerThink();
void EXPORT FallHack();
void MakeSound();
void AlertFlock();
void SpreadFlock();
void SpreadFlock2();
void Killed(entvars_t* pevAttacker, int iGib) override;
void Poop();
bool FPathBlocked();
//void KeyValue( KeyValueData *pkvd ) override;
bool Save(CSave& save) override;
bool Restore(CRestore& restore) override;
static TYPEDESCRIPTION m_SaveData[];
bool IsLeader() { return m_pSquadLeader == this; }
bool InSquad() { return m_pSquadLeader != NULL; }
int SquadCount();
void SquadRemove(CFlockingFlyer* pRemove);
void SquadUnlink();
void SquadAdd(CFlockingFlyer* pAdd);
void SquadDisband();
CFlockingFlyer* m_pSquadLeader;
CFlockingFlyer* m_pSquadNext;
bool m_fTurning; // is this boid turning?
bool m_fCourseAdjust; // followers set this flag true to override flocking while they avoid something
bool m_fPathBlocked; // true if there is an obstacle ahead
Vector m_vecReferencePoint; // last place we saw leader
Vector m_vecAdjustedVelocity; // adjusted velocity (used when fCourseAdjust is true)
float m_flGoalSpeed;
float m_flLastBlockedTime;
float m_flFakeBlockedTime;
float m_flAlertTime;
float m_flFlockNextSoundTime;
};
LINK_ENTITY_TO_CLASS(monster_flyer, CFlockingFlyer);
LINK_ENTITY_TO_CLASS(monster_flyer_flock, CFlockingFlyerFlock);
TYPEDESCRIPTION CFlockingFlyer::m_SaveData[] =
{
DEFINE_FIELD(CFlockingFlyer, m_pSquadLeader, FIELD_CLASSPTR),
DEFINE_FIELD(CFlockingFlyer, m_pSquadNext, FIELD_CLASSPTR),
DEFINE_FIELD(CFlockingFlyer, m_fTurning, FIELD_BOOLEAN),
DEFINE_FIELD(CFlockingFlyer, m_fCourseAdjust, FIELD_BOOLEAN),
DEFINE_FIELD(CFlockingFlyer, m_fPathBlocked, FIELD_BOOLEAN),
DEFINE_FIELD(CFlockingFlyer, m_vecReferencePoint, FIELD_POSITION_VECTOR),
DEFINE_FIELD(CFlockingFlyer, m_vecAdjustedVelocity, FIELD_VECTOR),
DEFINE_FIELD(CFlockingFlyer, m_flGoalSpeed, FIELD_FLOAT),
DEFINE_FIELD(CFlockingFlyer, m_flLastBlockedTime, FIELD_TIME),
DEFINE_FIELD(CFlockingFlyer, m_flFakeBlockedTime, FIELD_TIME),
DEFINE_FIELD(CFlockingFlyer, m_flAlertTime, FIELD_TIME),
// DEFINE_FIELD( CFlockingFlyer, m_flFlockNextSoundTime, FIELD_TIME ), // don't need to save
};
IMPLEMENT_SAVERESTORE(CFlockingFlyer, CBaseMonster);
//=========================================================
//=========================================================
bool CFlockingFlyerFlock::KeyValue(KeyValueData* pkvd)
{
if (FStrEq(pkvd->szKeyName, "iFlockSize"))
{
m_cFlockSize = atoi(pkvd->szValue);
return true;
}
else if (FStrEq(pkvd->szKeyName, "flFlockRadius"))
{
m_flFlockRadius = atof(pkvd->szValue);
return true;
}
return false;
}
//=========================================================
//=========================================================
void CFlockingFlyerFlock::Spawn()
{
Precache();
SpawnFlock();
REMOVE_ENTITY(ENT(pev)); // dump the spawn ent
}
//=========================================================
//=========================================================
void CFlockingFlyerFlock::Precache()
{
//PRECACHE_MODEL("models/aflock.mdl");
PRECACHE_MODEL("models/boid.mdl");
PrecacheFlockSounds();
}
void CFlockingFlyerFlock::PrecacheFlockSounds()
{
PRECACHE_SOUND("boid/boid_alert1.wav");
PRECACHE_SOUND("boid/boid_alert2.wav");
PRECACHE_SOUND("boid/boid_idle1.wav");
PRECACHE_SOUND("boid/boid_idle2.wav");
}
//=========================================================
//=========================================================
void CFlockingFlyerFlock::SpawnFlock()
{
float R = m_flFlockRadius;
int iCount;
Vector vecSpot;
CFlockingFlyer *pBoid, *pLeader;
pLeader = pBoid = NULL;
for (iCount = 0; iCount < m_cFlockSize; iCount++)
{
pBoid = GetClassPtr((CFlockingFlyer*)NULL);
if (!pLeader)
{
// make this guy the leader.
pLeader = pBoid;
pLeader->m_pSquadLeader = pLeader;
pLeader->m_pSquadNext = NULL;
}
vecSpot.x = RANDOM_FLOAT(-R, R);
vecSpot.y = RANDOM_FLOAT(-R, R);
vecSpot.z = RANDOM_FLOAT(0, 16);
vecSpot = pev->origin + vecSpot;
UTIL_SetOrigin(pBoid->pev, vecSpot);
pBoid->pev->movetype = MOVETYPE_FLY;
pBoid->SpawnCommonCode();
pBoid->pev->flags &= ~FL_ONGROUND;
pBoid->pev->velocity = g_vecZero;
pBoid->pev->angles = pev->angles;
pBoid->pev->frame = 0;
pBoid->pev->nextthink = gpGlobals->time + 0.2;
pBoid->SetThink(&CFlockingFlyer::IdleThink);
if (pBoid != pLeader)
{
pLeader->SquadAdd(pBoid);
}
}
}
//=========================================================
//=========================================================
void CFlockingFlyer::Spawn()
{
Precache();
SpawnCommonCode();
pev->frame = 0;
pev->nextthink = gpGlobals->time + 0.1;
SetThink(&CFlockingFlyer::IdleThink);
}
//=========================================================
//=========================================================
void CFlockingFlyer::Precache()
{
//PRECACHE_MODEL("models/aflock.mdl");
PRECACHE_MODEL("models/boid.mdl");
CFlockingFlyerFlock::PrecacheFlockSounds();
}
//=========================================================
//=========================================================
void CFlockingFlyer::MakeSound()
{
if (m_flAlertTime > gpGlobals->time)
{
// make agitated sounds
switch (RANDOM_LONG(0, 1))
{
case 0:
EMIT_SOUND(ENT(pev), CHAN_WEAPON, "boid/boid_alert1.wav", 1, ATTN_NORM);
break;
case 1:
EMIT_SOUND(ENT(pev), CHAN_WEAPON, "boid/boid_alert2.wav", 1, ATTN_NORM);
break;
}
return;
}
// make normal sound
switch (RANDOM_LONG(0, 1))
{
case 0:
EMIT_SOUND(ENT(pev), CHAN_WEAPON, "boid/boid_idle1.wav", 1, ATTN_NORM);
break;
case 1:
EMIT_SOUND(ENT(pev), CHAN_WEAPON, "boid/boid_idle2.wav", 1, ATTN_NORM);
break;
}
}
//=========================================================
//=========================================================
void CFlockingFlyer::Killed(entvars_t* pevAttacker, int iGib)
{
CFlockingFlyer* pSquad;
pSquad = (CFlockingFlyer*)m_pSquadLeader;
while (pSquad)
{
pSquad->m_flAlertTime = gpGlobals->time + 15;
pSquad = (CFlockingFlyer*)pSquad->m_pSquadNext;
}
if (m_pSquadLeader)
{
m_pSquadLeader->SquadRemove(this);
}
pev->deadflag = DEAD_DEAD;
pev->framerate = 0;
pev->effects = EF_NOINTERP;
UTIL_SetSize(pev, Vector(0, 0, 0), Vector(0, 0, 0));
pev->movetype = MOVETYPE_TOSS;
SetThink(&CFlockingFlyer::FallHack);
pev->nextthink = gpGlobals->time + 0.1;
}
void CFlockingFlyer::FallHack()
{
if ((pev->flags & FL_ONGROUND) != 0)
{
if (!FClassnameIs(pev->groundentity, "worldspawn"))
{
pev->flags &= ~FL_ONGROUND;
pev->nextthink = gpGlobals->time + 0.1;
}
else
{
pev->velocity = g_vecZero;
SetThink(NULL);
}
}
}
//=========================================================
//=========================================================
void CFlockingFlyer::SpawnCommonCode()
{
pev->deadflag = DEAD_NO;
pev->classname = MAKE_STRING("monster_flyer");
pev->solid = SOLID_SLIDEBOX;
pev->movetype = MOVETYPE_FLY;
pev->takedamage = DAMAGE_YES;
pev->health = 10;
m_bloodColor = BLOOD_COLOR_GREEN;
m_fPathBlocked = false; // obstacles will be detected
m_flFieldOfView = 0.2;
//SET_MODEL(ENT(pev), "models/aflock.mdl");
SET_MODEL(ENT(pev), "models/boid.mdl");
// UTIL_SetSize(pev, Vector(0,0,0), Vector(0,0,0));
UTIL_SetSize(pev, Vector(-5, -5, 0), Vector(5, 5, 2));
}
//=========================================================
//=========================================================
void CFlockingFlyer::BoidAdvanceFrame()
{
float flapspeed = (pev->speed - pev->armorvalue) / AFLOCK_ACCELERATE;
pev->armorvalue = pev->armorvalue * .8 + pev->speed * .2;
if (flapspeed < 0)
flapspeed = -flapspeed;
if (flapspeed < 0.25)
flapspeed = 0.25;
if (flapspeed > 1.9)
flapspeed = 1.9;
pev->framerate = flapspeed;
// lean
pev->avelocity.x = -(pev->angles.x + flapspeed * 5);
// bank
pev->avelocity.z = -(pev->angles.z + pev->avelocity.y);
// pev->framerate = flapspeed;
StudioFrameAdvance(0.1);
}
//=========================================================
//=========================================================
void CFlockingFlyer::IdleThink()
{
pev->nextthink = gpGlobals->time + 0.2;
// see if there's a client in the same pvs as the monster
if (!FNullEnt(FIND_CLIENT_IN_PVS(edict())))
{
SetThink(&CFlockingFlyer::Start);
pev->nextthink = gpGlobals->time + 0.1;
}
}
//=========================================================
// Start - player enters the pvs, so get things going.
//=========================================================
void CFlockingFlyer::Start()
{
pev->nextthink = gpGlobals->time + 0.1;
if (IsLeader())
{
SetThink(&CFlockingFlyer::FlockLeaderThink);
}
else
{
SetThink(&CFlockingFlyer::FlockFollowerThink);
}
/*
Vector vecTakeOff;
vecTakeOff = Vector ( 0 , 0 , 0 );
vecTakeOff.z = 50 + RANDOM_FLOAT ( 0, 100 );
vecTakeOff.x = 20 - RANDOM_FLOAT ( 0, 40);
vecTakeOff.y = 20 - RANDOM_FLOAT ( 0, 40);
pev->velocity = vecTakeOff;
pev->speed = pev->velocity.Length();
pev->sequence = 0;
*/
SetActivity(ACT_FLY);
ResetSequenceInfo();
BoidAdvanceFrame();
pev->speed = AFLOCK_FLY_SPEED; // no delay!
}
//=========================================================
// Leader boid calls this to form a flock from surrounding boids
//=========================================================
void CFlockingFlyer::FormFlock()
{
if (!InSquad())
{
// I am my own leader
m_pSquadLeader = this;
m_pSquadNext = NULL;
int squadCount = 1;
CBaseEntity* pEntity = NULL;
while ((pEntity = UTIL_FindEntityInSphere(pEntity, pev->origin, AFLOCK_MAX_RECRUIT_RADIUS)) != NULL)
{
CBaseMonster* pRecruit = pEntity->MyMonsterPointer();
if (pRecruit && pRecruit != this && pRecruit->IsAlive() && !pRecruit->m_pCine)
{
// Can we recruit this guy?
if (FClassnameIs(pRecruit->pev, "monster_flyer"))
{
squadCount++;
SquadAdd((CFlockingFlyer*)pRecruit);
}
}
}
}
SetThink(&CFlockingFlyer::IdleThink); // now that flock is formed, go to idle and wait for a player to come along.
pev->nextthink = gpGlobals->time;
}
//=========================================================
// Searches for boids that are too close and pushes them away
//=========================================================
void CFlockingFlyer::SpreadFlock()
{
Vector vecDir;
float flSpeed; // holds vector magnitude while we fiddle with the direction
CFlockingFlyer* pList = m_pSquadLeader;
while (pList)
{
if (pList != this && (pev->origin - pList->pev->origin).Length() <= AFLOCK_TOO_CLOSE)
{
// push the other away
vecDir = (pList->pev->origin - pev->origin);
vecDir = vecDir.Normalize();
// store the magnitude of the other boid's velocity, and normalize it so we
// can average in a course that points away from the leader.
flSpeed = pList->pev->velocity.Length();
pList->pev->velocity = pList->pev->velocity.Normalize();
pList->pev->velocity = (pList->pev->velocity + vecDir) * 0.5;
pList->pev->velocity = pList->pev->velocity * flSpeed;
}
pList = pList->m_pSquadNext;
}
}
//=========================================================
// Alters the caller's course if he's too close to others
//
// This function should **ONLY** be called when Caller's velocity is normalized!!
//=========================================================
void CFlockingFlyer::SpreadFlock2()
{
Vector vecDir;
CFlockingFlyer* pList = m_pSquadLeader;
while (pList)
{
if (pList != this && (pev->origin - pList->pev->origin).Length() <= AFLOCK_TOO_CLOSE)
{
vecDir = (pev->origin - pList->pev->origin);
vecDir = vecDir.Normalize();
pev->velocity = (pev->velocity + vecDir);
}
pList = pList->m_pSquadNext;
}
}
//=========================================================
// FBoidPathBlocked - returns true if there is an obstacle ahead
//=========================================================
bool CFlockingFlyer::FPathBlocked()
{
TraceResult tr;
Vector vecDist; // used for general measurements
Vector vecDir; // used for general measurements
bool fBlocked;
if (m_flFakeBlockedTime > gpGlobals->time)
{
m_flLastBlockedTime = gpGlobals->time;
return true;
}
// use VELOCITY, not angles, not all boids point the direction they are flying
//vecDir = UTIL_VecToAngles( pevBoid->velocity );
UTIL_MakeVectors(pev->angles);
fBlocked = false; // assume the way ahead is clear
// check for obstacle ahead
UTIL_TraceLine(pev->origin, pev->origin + gpGlobals->v_forward * AFLOCK_CHECK_DIST, ignore_monsters, ENT(pev), &tr);
if (tr.flFraction != 1.0)
{
m_flLastBlockedTime = gpGlobals->time;
fBlocked = true;
}
// extra wide checks
UTIL_TraceLine(pev->origin + gpGlobals->v_right * 12, pev->origin + gpGlobals->v_right * 12 + gpGlobals->v_forward * AFLOCK_CHECK_DIST, ignore_monsters, ENT(pev), &tr);
if (tr.flFraction != 1.0)
{
m_flLastBlockedTime = gpGlobals->time;
fBlocked = true;
}
UTIL_TraceLine(pev->origin - gpGlobals->v_right * 12, pev->origin - gpGlobals->v_right * 12 + gpGlobals->v_forward * AFLOCK_CHECK_DIST, ignore_monsters, ENT(pev), &tr);
if (tr.flFraction != 1.0)
{
m_flLastBlockedTime = gpGlobals->time;
fBlocked = true;
}
if (!fBlocked && gpGlobals->time - m_flLastBlockedTime > 6)
{
// not blocked, and it's been a few seconds since we've actually been blocked.
m_flFakeBlockedTime = gpGlobals->time + RANDOM_LONG(1, 3);
}
return fBlocked;
}
//=========================================================
// Leader boids use this think every tenth
//=========================================================
void CFlockingFlyer::FlockLeaderThink()
{
TraceResult tr;
Vector vecDist; // used for general measurements
Vector vecDir; // used for general measurements
int cProcessed = 0; // keep track of how many other boids we've processed
float flLeftSide;
float flRightSide;
pev->nextthink = gpGlobals->time + 0.1;
UTIL_MakeVectors(pev->angles);
// is the way ahead clear?
if (!FPathBlocked())
{
// if the boid is turning, stop the trend.
if (m_fTurning)
{
m_fTurning = false;
pev->avelocity.y = 0;
}
m_fPathBlocked = false;
if (pev->speed <= AFLOCK_FLY_SPEED)
pev->speed += 5;
pev->velocity = gpGlobals->v_forward * pev->speed;
BoidAdvanceFrame();
return;
}
// IF we get this far in the function, the leader's path is blocked!
m_fPathBlocked = true;
if (!m_fTurning) // something in the way and boid is not already turning to avoid
{
// measure clearance on left and right to pick the best dir to turn
UTIL_TraceLine(pev->origin, pev->origin + gpGlobals->v_right * AFLOCK_CHECK_DIST, ignore_monsters, ENT(pev), &tr);
vecDist = (tr.vecEndPos - pev->origin);
flRightSide = vecDist.Length();
UTIL_TraceLine(pev->origin, pev->origin - gpGlobals->v_right * AFLOCK_CHECK_DIST, ignore_monsters, ENT(pev), &tr);
vecDist = (tr.vecEndPos - pev->origin);
flLeftSide = vecDist.Length();
// turn right if more clearance on right side
if (flRightSide > flLeftSide)
{
pev->avelocity.y = -AFLOCK_TURN_RATE;
m_fTurning = true;
}
// default to left turn :)
else if (flLeftSide > flRightSide)
{
pev->avelocity.y = AFLOCK_TURN_RATE;
m_fTurning = true;
}
else
{
// equidistant. Pick randomly between left and right.
m_fTurning = true;
if (RANDOM_LONG(0, 1) == 0)
{
pev->avelocity.y = AFLOCK_TURN_RATE;
}
else
{
pev->avelocity.y = -AFLOCK_TURN_RATE;
}
}
}
SpreadFlock();
pev->velocity = gpGlobals->v_forward * pev->speed;
// check and make sure we aren't about to plow into the ground, don't let it happen
UTIL_TraceLine(pev->origin, pev->origin - gpGlobals->v_up * 16, ignore_monsters, ENT(pev), &tr);
if (tr.flFraction != 1.0 && pev->velocity.z < 0)
pev->velocity.z = 0;
// maybe it did, though.
if (FBitSet(pev->flags, FL_ONGROUND))
{
UTIL_SetOrigin(pev, pev->origin + Vector(0, 0, 1));
pev->velocity.z = 0;
}
if (m_flFlockNextSoundTime < gpGlobals->time)
{
MakeSound();
m_flFlockNextSoundTime = gpGlobals->time + RANDOM_FLOAT(1, 3);
}
BoidAdvanceFrame();
return;
}
//=========================================================
// follower boids execute this code when flocking
//=========================================================
void CFlockingFlyer::FlockFollowerThink()
{
TraceResult tr;
Vector vecDist;
Vector vecDir;
Vector vecDirToLeader;
float flDistToLeader;
pev->nextthink = gpGlobals->time + 0.1;
if (IsLeader() || !InSquad())
{
// the leader has been killed and this flyer suddenly finds himself the leader.
SetThink(&CFlockingFlyer::FlockLeaderThink);
return;
}
vecDirToLeader = (m_pSquadLeader->pev->origin - pev->origin);
flDistToLeader = vecDirToLeader.Length();
// match heading with leader
pev->angles = m_pSquadLeader->pev->angles;
//
// We can see the leader, so try to catch up to it
//
if (FInViewCone(m_pSquadLeader))
{
// if we're too far away, speed up
if (flDistToLeader > AFLOCK_TOO_FAR)
{
m_flGoalSpeed = m_pSquadLeader->pev->velocity.Length() * 1.5;
}
// if we're too close, slow down
else if (flDistToLeader < AFLOCK_TOO_CLOSE)
{
m_flGoalSpeed = m_pSquadLeader->pev->velocity.Length() * 0.5;
}
}
else
{
// wait up! the leader isn't out in front, so we slow down to let him pass
m_flGoalSpeed = m_pSquadLeader->pev->velocity.Length() * 0.5;
}
SpreadFlock2();
pev->speed = pev->velocity.Length();
pev->velocity = pev->velocity.Normalize();
// if we are too far from leader, average a vector towards it into our current velocity
if (flDistToLeader > AFLOCK_TOO_FAR)
{
vecDirToLeader = vecDirToLeader.Normalize();
pev->velocity = (pev->velocity + vecDirToLeader) * 0.5;
}
// clamp speeds and handle acceleration
if (m_flGoalSpeed > AFLOCK_FLY_SPEED * 2)
{
m_flGoalSpeed = AFLOCK_FLY_SPEED * 2;
}
if (pev->speed < m_flGoalSpeed)
{
pev->speed += AFLOCK_ACCELERATE;
}
else if (pev->speed > m_flGoalSpeed)
{
pev->speed -= AFLOCK_ACCELERATE;
}
pev->velocity = pev->velocity * pev->speed;
BoidAdvanceFrame();
}
/*
// Is this boid's course blocked?
if ( FBoidPathBlocked (pev) )
{
// course is still blocked from last time. Just keep flying along adjusted
// velocity
if ( m_fCourseAdjust )
{
pev->velocity = m_vecAdjustedVelocity * pev->speed;
return;
}
else // set course adjust flag and calculate adjusted velocity
{
m_fCourseAdjust = true;
// use VELOCITY, not angles, not all boids point the direction they are flying
//vecDir = UTIL_VecToAngles( pev->velocity );
//UTIL_MakeVectors ( vecDir );
UTIL_MakeVectors ( pev->angles );
// measure clearance on left and right to pick the best dir to turn
UTIL_TraceLine(pev->origin, pev->origin + gpGlobals->v_right * AFLOCK_CHECK_DIST, ignore_monsters, ENT(pev), &tr);
vecDist = (tr.vecEndPos - pev->origin);
flRightSide = vecDist.Length();
UTIL_TraceLine(pev->origin, pev->origin - gpGlobals->v_right * AFLOCK_CHECK_DIST, ignore_monsters, ENT(pev), &tr);
vecDist = (tr.vecEndPos - pev->origin);
flLeftSide = vecDist.Length();
// slide right if more clearance on right side
if ( flRightSide > flLeftSide )
{
m_vecAdjustedVelocity = gpGlobals->v_right;
}
// else slide left
else
{
m_vecAdjustedVelocity = gpGlobals->v_right * -1;
}
}
return;
}
// if we make it this far, boids path is CLEAR!
m_fCourseAdjust = false;
*/
//=========================================================
//
// SquadUnlink(), Unlink the squad pointers.
//
//=========================================================
void CFlockingFlyer::SquadUnlink()
{
m_pSquadLeader = NULL;
m_pSquadNext = NULL;
}
//=========================================================
//
// SquadAdd(), add pAdd to my squad
//
//=========================================================
void CFlockingFlyer::SquadAdd(CFlockingFlyer* pAdd)
{
ASSERT(pAdd != NULL);
ASSERT(!pAdd->InSquad());
ASSERT(this->IsLeader());
pAdd->m_pSquadNext = m_pSquadNext;
m_pSquadNext = pAdd;
pAdd->m_pSquadLeader = this;
}
//=========================================================
//
// SquadRemove(), remove pRemove from my squad.
// If I am pRemove, promote m_pSquadNext to leader
//
//=========================================================
void CFlockingFlyer::SquadRemove(CFlockingFlyer* pRemove)
{
ASSERT(pRemove != NULL);
ASSERT(this->IsLeader());
ASSERT(pRemove->m_pSquadLeader == this);
if (SquadCount() > 2)
{
// Removing the leader, promote m_pSquadNext to leader
if (pRemove == this)
{
CFlockingFlyer* pLeader = m_pSquadNext;
// copy the enemy LKP to the new leader
pLeader->m_vecEnemyLKP = m_vecEnemyLKP;
if (pLeader)
{
CFlockingFlyer* pList = pLeader;
while (pList)
{
pList->m_pSquadLeader = pLeader;
pList = pList->m_pSquadNext;
}
}
SquadUnlink();
}
else // removing a node
{
CFlockingFlyer* pList = this;
// Find the node before pRemove
while (pList->m_pSquadNext != pRemove)
{
// assert to test valid list construction
ASSERT(pList->m_pSquadNext != NULL);
pList = pList->m_pSquadNext;
}
// List validity
ASSERT(pList->m_pSquadNext == pRemove);
// Relink without pRemove
pList->m_pSquadNext = pRemove->m_pSquadNext;
// Unlink pRemove
pRemove->SquadUnlink();
}
}
else
SquadDisband();
}
//=========================================================
//
// SquadCount(), return the number of members of this squad
// callable from leaders & followers
//
//=========================================================
int CFlockingFlyer::SquadCount()
{
CFlockingFlyer* pList = m_pSquadLeader;
int squadCount = 0;
while (pList)
{
squadCount++;
pList = pList->m_pSquadNext;
}
return squadCount;
}
//=========================================================
//
// SquadDisband(), Unlink all squad members
//
//=========================================================
void CFlockingFlyer::SquadDisband()
{
CFlockingFlyer* pList = m_pSquadLeader;
CFlockingFlyer* pNext;
while (pList)
{
pNext = pList->m_pSquadNext;
pList->SquadUnlink();
pList = pNext;
}
}