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.

919 lines
24 KiB

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