Portable Half-Life SDK. GoldSource and Xash3D. Crossplatform.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

921 lines
24 KiB

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