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.

460 lines
14 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.
*
****/
//=========================================================
// cockroach
//=========================================================
9 years ago
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "monsters.h"
#include "schedule.h"
#include "soundent.h"
#include "decals.h"
#define ROACH_IDLE 0
#define ROACH_BORED 1
#define ROACH_SCARED_BY_ENT 2
#define ROACH_SCARED_BY_LIGHT 3
#define ROACH_SMELL_FOOD 4
#define ROACH_EAT 5
9 years ago
//=========================================================
// Monster's Anim Events Go Here
//=========================================================
class CRoach : public CBaseMonster
{
public:
void Spawn( void );
void Precache( void );
void SetYawSpeed( void );
void EXPORT MonsterThink ( void );
9 years ago
void Move( float flInterval );
void PickNewDest( int iCondition );
void EXPORT Touch( CBaseEntity *pOther );
9 years ago
void Killed( entvars_t *pevAttacker, int iGib );
9 years ago
float m_flLastLightLevel;
float m_flNextSmellTime;
int Classify( void );
void Look( int iDistance );
int ISoundMask( void );
9 years ago
// UNDONE: These don't necessarily need to be save/restored, but if we add more data, it may
9 years ago
BOOL m_fLightHacked;
int m_iMode;
9 years ago
// -----------------------------
};
9 years ago
LINK_ENTITY_TO_CLASS( monster_cockroach, CRoach )
9 years ago
//=========================================================
// 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.
//=========================================================
9 years ago
int CRoach::ISoundMask( void )
9 years ago
{
9 years ago
return bits_SOUND_CARCASS | bits_SOUND_MEAT;
9 years ago
}
//=========================================================
// Classify - indicates this monster's place in the
// relationship table.
//=========================================================
9 years ago
int CRoach::Classify( void )
9 years ago
{
return CLASS_INSECT;
}
//=========================================================
// Touch
//=========================================================
9 years ago
void CRoach::Touch( CBaseEntity *pOther )
9 years ago
{
9 years ago
Vector vecSpot;
TraceResult tr;
9 years ago
9 years ago
if( pOther->pev->velocity == g_vecZero || !pOther->IsPlayer() )
9 years ago
{
return;
}
9 years ago
vecSpot = pev->origin + Vector( 0, 0, 8 );//move up a bit, and trace down.
UTIL_TraceLine( vecSpot, vecSpot + Vector( 0, 0, -24 ), ignore_monsters, ENT( pev ), &tr );
9 years ago
// This isn't really blood. So you don't have to screen it out based on violence levels (UTIL_ShouldShowBlood())
9 years ago
UTIL_DecalTrace( &tr, DECAL_YBLOOD1 + RANDOM_LONG( 0, 5 ) );
9 years ago
TakeDamage( pOther->pev, pOther->pev, pev->health, DMG_CRUSH );
}
//=========================================================
// SetYawSpeed - allows each sequence to have a different
// turn rate associated with it.
//=========================================================
9 years ago
void CRoach::SetYawSpeed( void )
9 years ago
{
int ys;
ys = 120;
pev->yaw_speed = ys;
}
//=========================================================
// Spawn
//=========================================================
9 years ago
void CRoach::Spawn()
9 years ago
{
9 years ago
Precache();
9 years ago
9 years ago
SET_MODEL( ENT( pev ), "models/roach.mdl" );
9 years ago
UTIL_SetSize( pev, Vector( -1, -1, 0 ), Vector( 1, 1, 2 ) );
9 years ago
pev->solid = SOLID_SLIDEBOX;
pev->movetype = MOVETYPE_STEP;
m_bloodColor = BLOOD_COLOR_YELLOW;
pev->effects = 0;
pev->health = 1;
m_flFieldOfView = 0.5;// indicates the width of this monster's forward view cone ( as a dotproduct result )
m_MonsterState = MONSTERSTATE_NONE;
9 years ago
MonsterInit();
9 years ago
SetActivity( ACT_IDLE );
pev->view_ofs = Vector( 0, 0, 1 );// position of the eyes relative to monster's origin.
pev->takedamage = DAMAGE_YES;
m_fLightHacked = FALSE;
m_flLastLightLevel = -1;
m_iMode = ROACH_IDLE;
m_flNextSmellTime = gpGlobals->time;
9 years ago
}
//=========================================================
// Precache - precaches all resources this monster needs
//=========================================================
9 years ago
void CRoach::Precache()
9 years ago
{
9 years ago
PRECACHE_MODEL( "models/roach.mdl" );
9 years ago
9 years ago
PRECACHE_SOUND( "roach/rch_die.wav" );
PRECACHE_SOUND( "roach/rch_walk.wav" );
PRECACHE_SOUND( "roach/rch_smash.wav" );
}
9 years ago
//=========================================================
// Killed.
//=========================================================
9 years ago
void CRoach::Killed( entvars_t *pevAttacker, int iGib )
9 years ago
{
pev->solid = SOLID_NOT;
//random sound
9 years ago
if( RANDOM_LONG( 0, 4 ) == 1 )
9 years ago
{
9 years ago
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "roach/rch_die.wav", 0.8, ATTN_NORM, 0, 80 + RANDOM_LONG( 0, 39 ) );
9 years ago
}
else
{
9 years ago
EMIT_SOUND_DYN( ENT( pev ), CHAN_BODY, "roach/rch_smash.wav", 0.7, ATTN_NORM, 0, 80 + RANDOM_LONG( 0, 39 ) );
9 years ago
}
9 years ago
CSoundEnt::InsertSound( bits_SOUND_WORLD, pev->origin, 128, 1 );
9 years ago
9 years ago
CBaseEntity *pOwner = CBaseEntity::Instance( pev->owner );
if( pOwner )
9 years ago
{
pOwner->DeathNotice( pev );
}
UTIL_Remove( this );
}
//=========================================================
// MonsterThink, overridden for roaches.
//=========================================================
9 years ago
void CRoach::MonsterThink( void )
9 years ago
{
9 years ago
if( FNullEnt( FIND_CLIENT_IN_PVS( edict() ) ) )
pev->nextthink = gpGlobals->time + RANDOM_FLOAT( 1, 1.5 );
9 years ago
else
pev->nextthink = gpGlobals->time + 0.1f;// keep monster thinking
9 years ago
9 years ago
float flInterval = StudioFrameAdvance(); // animate
9 years ago
9 years ago
if( !m_fLightHacked )
9 years ago
{
// if light value hasn't been collection for the first time yet,
// suspend the creature for a second so the world finishes spawning, then we'll collect the light level.
pev->nextthink = gpGlobals->time + 1;
m_fLightHacked = TRUE;
return;
}
9 years ago
else if( m_flLastLightLevel < 0 )
9 years ago
{
// collect light level for the first time, now that all of the lightmaps in the roach's area have been calculated.
m_flLastLightLevel = GETENTITYILLUM( ENT( pev ) );
}
9 years ago
switch( m_iMode )
9 years ago
{
9 years ago
case ROACH_IDLE:
case ROACH_EAT:
9 years ago
{
// if not moving, sample environment to see if anything scary is around. Do a radius search 'look' at random.
9 years ago
if( RANDOM_LONG( 0, 3 ) == 1 )
9 years ago
{
Look( 150 );
9 years ago
if( HasConditions( bits_COND_SEE_FEAR ) )
9 years ago
{
// if see something scary
9 years ago
//ALERT( at_aiconsole, "Scared\n" );
Eat( 30 + ( RANDOM_LONG( 0, 14 ) ) );// roach will ignore food for 30 to 45 seconds
9 years ago
PickNewDest( ROACH_SCARED_BY_ENT );
9 years ago
SetActivity( ACT_WALK );
9 years ago
}
9 years ago
else if( RANDOM_LONG( 0, 149 ) == 1 )
9 years ago
{
// if roach doesn't see anything, there's still a chance that it will move. (boredom)
9 years ago
//ALERT( at_aiconsole, "Bored\n" );
9 years ago
PickNewDest( ROACH_BORED );
9 years ago
SetActivity( ACT_WALK );
9 years ago
9 years ago
if( m_iMode == ROACH_EAT )
9 years ago
{
// roach will ignore food for 30 to 45 seconds if it got bored while eating.
9 years ago
Eat( 30 + ( RANDOM_LONG( 0, 14 ) ) );
9 years ago
}
}
}
9 years ago
9 years ago
// don't do this stuff if eating!
9 years ago
if( m_iMode == ROACH_IDLE )
9 years ago
{
9 years ago
if( FShouldEat() )
9 years ago
{
Listen();
}
9 years ago
if( GETENTITYILLUM( ENT( pev ) ) > m_flLastLightLevel )
9 years ago
{
// someone turned on lights!
9 years ago
//ALERT( at_console, "Lights!\n" );
9 years ago
PickNewDest( ROACH_SCARED_BY_LIGHT );
9 years ago
SetActivity( ACT_WALK );
9 years ago
}
9 years ago
else if( HasConditions( bits_COND_SMELL_FOOD ) )
9 years ago
{
CSound *pSound;
pSound = CSoundEnt::SoundPointerForIndex( m_iAudibleList );
// roach smells food and is just standing around. Go to food unless food isn't on same z-plane.
if( pSound && fabs( pSound->m_vecOrigin.z - pev->origin.z ) <= 3.0f )
9 years ago
{
PickNewDest( ROACH_SMELL_FOOD );
9 years ago
SetActivity( ACT_WALK );
9 years ago
}
}
}
break;
}
9 years ago
case ROACH_SCARED_BY_LIGHT:
9 years ago
{
// if roach was scared by light, then stop if we're over a spot at least as dark as where we started!
9 years ago
if( GETENTITYILLUM( ENT( pev ) ) <= m_flLastLightLevel )
9 years ago
{
9 years ago
SetActivity( ACT_IDLE );
m_flLastLightLevel = GETENTITYILLUM( ENT( pev ) );// make this our new light level.
9 years ago
}
break;
}
}
9 years ago
if( m_flGroundSpeed != 0 )
9 years ago
{
Move( flInterval );
}
}
//=========================================================
// Picks a new spot for roach to run to.(
//=========================================================
9 years ago
void CRoach::PickNewDest( int iCondition )
9 years ago
{
9 years ago
Vector vecNewDir;
Vector vecDest;
float flDist;
9 years ago
m_iMode = iCondition;
9 years ago
if( m_iMode == ROACH_SMELL_FOOD )
9 years ago
{
// find the food and go there.
CSound *pSound;
pSound = CSoundEnt::SoundPointerForIndex( m_iAudibleList );
9 years ago
if( pSound )
9 years ago
{
9 years ago
m_Route[0].vecLocation.x = pSound->m_vecOrigin.x + ( 3 - RANDOM_LONG( 0, 5 ) );
m_Route[0].vecLocation.y = pSound->m_vecOrigin.y + ( 3 - RANDOM_LONG( 0, 5 ) );
m_Route[0].vecLocation.z = pSound->m_vecOrigin.z;
m_Route[0].iType = bits_MF_TO_LOCATION;
m_movementGoal = RouteClassify( m_Route[0].iType );
9 years ago
return;
}
}
9 years ago
do
9 years ago
{
// picks a random spot, requiring that it be at least 128 units away
// else, the roach will pick a spot too close to itself and run in
// circles. this is a hack but buys me time to work on the real monsters.
vecNewDir.x = RANDOM_FLOAT( -1, 1 );
vecNewDir.y = RANDOM_FLOAT( -1, 1 );
9 years ago
flDist = 256 + ( RANDOM_LONG( 0, 255 ) );
9 years ago
vecDest = pev->origin + vecNewDir * flDist;
9 years ago
} while( ( vecDest - pev->origin ).Length2D() < 128 );
9 years ago
9 years ago
m_Route[0].vecLocation.x = vecDest.x;
m_Route[0].vecLocation.y = vecDest.y;
m_Route[0].vecLocation.z = pev->origin.z;
m_Route[0].iType = bits_MF_TO_LOCATION;
m_movementGoal = RouteClassify( m_Route[0].iType );
9 years ago
9 years ago
if( RANDOM_LONG( 0, 9 ) == 1 )
9 years ago
{
// every once in a while, a roach will play a skitter sound when they decide to run
9 years ago
EMIT_SOUND_DYN( ENT( pev ), CHAN_BODY, "roach/rch_walk.wav", 1, ATTN_NORM, 0, 80 + RANDOM_LONG( 0, 39 ) );
9 years ago
}
}
//=========================================================
// roach's move function
//=========================================================
9 years ago
void CRoach::Move( float flInterval )
9 years ago
{
9 years ago
float flWaypointDist;
Vector vecApex;
9 years ago
// local move to waypoint.
9 years ago
flWaypointDist = ( m_Route[m_iRouteIndex].vecLocation - pev->origin ).Length2D();
MakeIdealYaw( m_Route[m_iRouteIndex].vecLocation );
9 years ago
9 years ago
ChangeYaw( pev->yaw_speed );
9 years ago
UTIL_MakeVectors( pev->angles );
9 years ago
if( RANDOM_LONG( 0, 7 ) == 1 )
9 years ago
{
// randomly check for blocked path.(more random load balancing)
9 years ago
if( !WALK_MOVE( ENT( pev ), pev->ideal_yaw, 4, WALKMOVE_NORMAL ) )
9 years ago
{
// stuck, so just pick a new spot to run off to
PickNewDest( m_iMode );
}
}
9 years ago
WALK_MOVE( ENT( pev ), pev->ideal_yaw, m_flGroundSpeed * flInterval, WALKMOVE_NORMAL );
9 years ago
// if the waypoint is closer than step size, then stop after next step (ok for roach to overshoot)
9 years ago
if( flWaypointDist <= m_flGroundSpeed * flInterval )
9 years ago
{
// take truncated step and stop
9 years ago
SetActivity( ACT_IDLE );
m_flLastLightLevel = GETENTITYILLUM( ENT( pev ) );// this is roach's new comfortable light level
9 years ago
9 years ago
if( m_iMode == ROACH_SMELL_FOOD )
9 years ago
{
m_iMode = ROACH_EAT;
}
else
{
m_iMode = ROACH_IDLE;
}
}
9 years ago
if( RANDOM_LONG( 0, 149 ) == 1 && m_iMode != ROACH_SCARED_BY_LIGHT && m_iMode != ROACH_SMELL_FOOD )
9 years ago
{
// random skitter while moving as long as not on a b-line to get out of light or going to food
PickNewDest( FALSE );
}
}
//=========================================================
// Look - overriden for the roach, which can virtually see
// 360 degrees.
//=========================================================
9 years ago
void CRoach::Look( int iDistance )
9 years ago
{
9 years ago
CBaseEntity *pSightEnt = NULL;// the current visible entity that we're dealing with
CBaseEntity *pPreviousEnt;// the last entity added to the link list
int iSighted = 0;
9 years ago
// DON'T let visibility information from last frame sit around!
9 years ago
ClearConditions( bits_COND_SEE_HATE | bits_COND_SEE_DISLIKE | bits_COND_SEE_ENEMY | bits_COND_SEE_FEAR );
9 years ago
// don't let monsters outside of the player's PVS act up, or most of the interesting
// things will happen before the player gets there!
9 years ago
if( FNullEnt( FIND_CLIENT_IN_PVS( edict() ) ) )
9 years ago
{
return;
}
m_pLink = NULL;
pPreviousEnt = this;
// Does sphere also limit itself to PVS?
// Examine all entities within a reasonable radius
// !!!PERFORMANCE - let's trivially reject the ent list before radius searching!
9 years ago
while( ( pSightEnt = UTIL_FindEntityInSphere( pSightEnt, pev->origin, iDistance ) ) != NULL )
9 years ago
{
// only consider ents that can be damaged. !!!temporarily only considering other monsters and clients
9 years ago
if( pSightEnt->IsPlayer() || FBitSet( pSightEnt->pev->flags, FL_MONSTER ) )
9 years ago
{
9 years ago
if( /*FVisible( pSightEnt ) &&*/ !FBitSet( pSightEnt->pev->flags, FL_NOTARGET ) && pSightEnt->pev->health > 0 )
9 years ago
{
// NULL the Link pointer for each ent added to the link list. If other ents follow, the will overwrite
// this value. If this ent happens to be the last, the list will be properly terminated.
pPreviousEnt->m_pLink = pSightEnt;
pSightEnt->m_pLink = NULL;
pPreviousEnt = pSightEnt;
// 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.
9 years ago
switch( IRelationship( pSightEnt ) )
9 years ago
{
9 years ago
case R_FR:
9 years ago
iSighted |= bits_COND_SEE_FEAR;
break;
9 years ago
case R_NO:
9 years ago
break;
default:
9 years ago
ALERT( at_console, "%s can't asses %s\n", STRING( pev->classname ), STRING( pSightEnt->pev->classname ) );
9 years ago
break;
}
}
}
}
SetConditions( iSighted );
}
//=========================================================
// AI Schedules Specific to this monster
//=========================================================