2
0
mirror of https://github.com/FWGS/hlsdk-xash3d synced 2024-11-22 01:47:45 +01:00
hlsdk-xash3d/dlls/roach.cpp

460 lines
14 KiB
C++
Raw Normal View History

2016-06-04 15:24:23 +02:00
/***
*
* 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
//=========================================================
2016-07-31 15:48:50 +02:00
#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
2016-06-04 15:24:23 +02:00
//=========================================================
// Monster's Anim Events Go Here
//=========================================================
class CRoach : public CBaseMonster
{
public:
void Spawn( void );
void Precache( void );
void SetYawSpeed( void );
void EXPORT MonsterThink ( void );
2016-07-31 15:48:50 +02:00
void Move( float flInterval );
void PickNewDest( int iCondition );
void EXPORT Touch( CBaseEntity *pOther );
2016-06-04 15:24:23 +02:00
void Killed( entvars_t *pevAttacker, int iGib );
2016-07-31 15:48:50 +02:00
float m_flLastLightLevel;
float m_flNextSmellTime;
int Classify( void );
void Look( int iDistance );
int ISoundMask( void );
2016-06-04 15:24:23 +02:00
// UNDONE: These don't necessarily need to be save/restored, but if we add more data, it may
2016-07-31 15:48:50 +02:00
BOOL m_fLightHacked;
int m_iMode;
2016-06-04 15:24:23 +02:00
// -----------------------------
};
2016-07-31 15:48:50 +02:00
2016-06-25 20:21:01 +02:00
LINK_ENTITY_TO_CLASS( monster_cockroach, CRoach )
2016-06-04 15:24:23 +02:00
//=========================================================
// 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.
//=========================================================
2016-07-31 15:48:50 +02:00
int CRoach::ISoundMask( void )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
return bits_SOUND_CARCASS | bits_SOUND_MEAT;
2016-06-04 15:24:23 +02:00
}
//=========================================================
// Classify - indicates this monster's place in the
// relationship table.
//=========================================================
2016-07-31 15:48:50 +02:00
int CRoach::Classify( void )
2016-06-04 15:24:23 +02:00
{
return CLASS_INSECT;
}
//=========================================================
// Touch
//=========================================================
2016-07-31 15:48:50 +02:00
void CRoach::Touch( CBaseEntity *pOther )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
Vector vecSpot;
TraceResult tr;
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
if( pOther->pev->velocity == g_vecZero || !pOther->IsPlayer() )
2016-06-04 15:24:23 +02:00
{
return;
}
2016-07-31 15:48:50 +02:00
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 );
2016-06-04 15:24:23 +02:00
// This isn't really blood. So you don't have to screen it out based on violence levels (UTIL_ShouldShowBlood())
2016-07-31 15:48:50 +02:00
UTIL_DecalTrace( &tr, DECAL_YBLOOD1 + RANDOM_LONG( 0, 5 ) );
2016-06-04 15:24:23 +02:00
TakeDamage( pOther->pev, pOther->pev, pev->health, DMG_CRUSH );
}
//=========================================================
// SetYawSpeed - allows each sequence to have a different
// turn rate associated with it.
//=========================================================
2016-07-31 15:48:50 +02:00
void CRoach::SetYawSpeed( void )
2016-06-04 15:24:23 +02:00
{
int ys;
ys = 120;
pev->yaw_speed = ys;
}
//=========================================================
// Spawn
//=========================================================
2016-07-31 15:48:50 +02:00
void CRoach::Spawn()
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
Precache();
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
SET_MODEL( ENT( pev ), "models/roach.mdl" );
2016-06-04 15:24:23 +02:00
UTIL_SetSize( pev, Vector( -1, -1, 0 ), Vector( 1, 1, 2 ) );
2016-07-31 15:48:50 +02:00
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;
2016-06-04 15:24:23 +02:00
MonsterInit();
2016-07-31 15:48:50 +02:00
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;
2016-06-04 15:24:23 +02:00
}
//=========================================================
// Precache - precaches all resources this monster needs
//=========================================================
2016-07-31 15:48:50 +02:00
void CRoach::Precache()
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
PRECACHE_MODEL( "models/roach.mdl" );
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
PRECACHE_SOUND( "roach/rch_die.wav" );
PRECACHE_SOUND( "roach/rch_walk.wav" );
PRECACHE_SOUND( "roach/rch_smash.wav" );
}
2016-06-04 15:24:23 +02:00
//=========================================================
// Killed.
//=========================================================
2016-07-31 15:48:50 +02:00
void CRoach::Killed( entvars_t *pevAttacker, int iGib )
2016-06-04 15:24:23 +02:00
{
pev->solid = SOLID_NOT;
//random sound
2016-07-31 15:48:50 +02:00
if( RANDOM_LONG( 0, 4 ) == 1 )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "roach/rch_die.wav", 0.8, ATTN_NORM, 0, 80 + RANDOM_LONG( 0, 39 ) );
2016-06-04 15:24:23 +02:00
}
else
{
2016-07-31 15:48:50 +02:00
EMIT_SOUND_DYN( ENT( pev ), CHAN_BODY, "roach/rch_smash.wav", 0.7, ATTN_NORM, 0, 80 + RANDOM_LONG( 0, 39 ) );
2016-06-04 15:24:23 +02:00
}
2016-07-31 15:48:50 +02:00
CSoundEnt::InsertSound( bits_SOUND_WORLD, pev->origin, 128, 1 );
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
CBaseEntity *pOwner = CBaseEntity::Instance( pev->owner );
if( pOwner )
2016-06-04 15:24:23 +02:00
{
pOwner->DeathNotice( pev );
}
UTIL_Remove( this );
}
//=========================================================
// MonsterThink, overridden for roaches.
//=========================================================
2016-07-31 15:48:50 +02:00
void CRoach::MonsterThink( void )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
if( FNullEnt( FIND_CLIENT_IN_PVS( edict() ) ) )
pev->nextthink = gpGlobals->time + RANDOM_FLOAT( 1, 1.5 );
2016-06-04 15:24:23 +02:00
else
2019-10-13 13:49:25 +02:00
pev->nextthink = gpGlobals->time + 0.1f;// keep monster thinking
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
float flInterval = StudioFrameAdvance(); // animate
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
if( !m_fLightHacked )
2016-06-04 15:24:23 +02:00
{
// 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;
}
2016-07-31 15:48:50 +02:00
else if( m_flLastLightLevel < 0 )
2016-06-04 15:24:23 +02:00
{
// 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 ) );
}
2016-07-31 15:48:50 +02:00
switch( m_iMode )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
case ROACH_IDLE:
case ROACH_EAT:
2016-06-04 15:24:23 +02:00
{
// if not moving, sample environment to see if anything scary is around. Do a radius search 'look' at random.
2016-07-31 15:48:50 +02:00
if( RANDOM_LONG( 0, 3 ) == 1 )
2016-06-04 15:24:23 +02:00
{
Look( 150 );
2016-07-31 15:48:50 +02:00
if( HasConditions( bits_COND_SEE_FEAR ) )
2016-06-04 15:24:23 +02:00
{
// if see something scary
2016-07-31 15:48:50 +02:00
//ALERT( at_aiconsole, "Scared\n" );
Eat( 30 + ( RANDOM_LONG( 0, 14 ) ) );// roach will ignore food for 30 to 45 seconds
2016-06-04 15:24:23 +02:00
PickNewDest( ROACH_SCARED_BY_ENT );
2016-07-31 15:48:50 +02:00
SetActivity( ACT_WALK );
2016-06-04 15:24:23 +02:00
}
2016-07-31 15:48:50 +02:00
else if( RANDOM_LONG( 0, 149 ) == 1 )
2016-06-04 15:24:23 +02:00
{
// if roach doesn't see anything, there's still a chance that it will move. (boredom)
2016-07-31 15:48:50 +02:00
//ALERT( at_aiconsole, "Bored\n" );
2016-06-04 15:24:23 +02:00
PickNewDest( ROACH_BORED );
2016-07-31 15:48:50 +02:00
SetActivity( ACT_WALK );
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
if( m_iMode == ROACH_EAT )
2016-06-04 15:24:23 +02:00
{
// roach will ignore food for 30 to 45 seconds if it got bored while eating.
2016-07-31 15:48:50 +02:00
Eat( 30 + ( RANDOM_LONG( 0, 14 ) ) );
2016-06-04 15:24:23 +02:00
}
}
}
2016-07-31 15:48:50 +02:00
2016-06-04 15:24:23 +02:00
// don't do this stuff if eating!
2016-07-31 15:48:50 +02:00
if( m_iMode == ROACH_IDLE )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
if( FShouldEat() )
2016-06-04 15:24:23 +02:00
{
Listen();
}
2016-07-31 15:48:50 +02:00
if( GETENTITYILLUM( ENT( pev ) ) > m_flLastLightLevel )
2016-06-04 15:24:23 +02:00
{
// someone turned on lights!
2016-07-31 15:48:50 +02:00
//ALERT( at_console, "Lights!\n" );
2016-06-04 15:24:23 +02:00
PickNewDest( ROACH_SCARED_BY_LIGHT );
2016-07-31 15:48:50 +02:00
SetActivity( ACT_WALK );
2016-06-04 15:24:23 +02:00
}
2016-07-31 15:48:50 +02:00
else if( HasConditions( bits_COND_SMELL_FOOD ) )
2016-06-04 15:24:23 +02:00
{
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.
2019-10-13 13:49:25 +02:00
if( pSound && fabs( pSound->m_vecOrigin.z - pev->origin.z ) <= 3.0f )
2016-06-04 15:24:23 +02:00
{
PickNewDest( ROACH_SMELL_FOOD );
2016-07-31 15:48:50 +02:00
SetActivity( ACT_WALK );
2016-06-04 15:24:23 +02:00
}
}
}
break;
}
2016-07-31 15:48:50 +02:00
case ROACH_SCARED_BY_LIGHT:
2016-06-04 15:24:23 +02:00
{
// if roach was scared by light, then stop if we're over a spot at least as dark as where we started!
2016-07-31 15:48:50 +02:00
if( GETENTITYILLUM( ENT( pev ) ) <= m_flLastLightLevel )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
SetActivity( ACT_IDLE );
m_flLastLightLevel = GETENTITYILLUM( ENT( pev ) );// make this our new light level.
2016-06-04 15:24:23 +02:00
}
break;
}
}
2016-07-31 15:48:50 +02:00
if( m_flGroundSpeed != 0 )
2016-06-04 15:24:23 +02:00
{
Move( flInterval );
}
}
//=========================================================
// Picks a new spot for roach to run to.(
//=========================================================
2016-07-31 15:48:50 +02:00
void CRoach::PickNewDest( int iCondition )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
Vector vecNewDir;
Vector vecDest;
float flDist;
2016-06-04 15:24:23 +02:00
m_iMode = iCondition;
2016-07-31 15:48:50 +02:00
if( m_iMode == ROACH_SMELL_FOOD )
2016-06-04 15:24:23 +02:00
{
// find the food and go there.
CSound *pSound;
pSound = CSoundEnt::SoundPointerForIndex( m_iAudibleList );
2016-07-31 15:48:50 +02:00
if( pSound )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
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 );
2016-06-04 15:24:23 +02:00
return;
}
}
2016-07-31 15:48:50 +02:00
do
2016-06-04 15:24:23 +02:00
{
// 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 );
2016-07-31 15:48:50 +02:00
flDist = 256 + ( RANDOM_LONG( 0, 255 ) );
2016-06-04 15:24:23 +02:00
vecDest = pev->origin + vecNewDir * flDist;
2016-07-31 15:48:50 +02:00
} while( ( vecDest - pev->origin ).Length2D() < 128 );
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
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 );
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
if( RANDOM_LONG( 0, 9 ) == 1 )
2016-06-04 15:24:23 +02:00
{
// every once in a while, a roach will play a skitter sound when they decide to run
2016-07-31 15:48:50 +02:00
EMIT_SOUND_DYN( ENT( pev ), CHAN_BODY, "roach/rch_walk.wav", 1, ATTN_NORM, 0, 80 + RANDOM_LONG( 0, 39 ) );
2016-06-04 15:24:23 +02:00
}
}
//=========================================================
// roach's move function
//=========================================================
2016-07-31 15:48:50 +02:00
void CRoach::Move( float flInterval )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
float flWaypointDist;
Vector vecApex;
2016-06-04 15:24:23 +02:00
// local move to waypoint.
2016-07-31 15:48:50 +02:00
flWaypointDist = ( m_Route[m_iRouteIndex].vecLocation - pev->origin ).Length2D();
MakeIdealYaw( m_Route[m_iRouteIndex].vecLocation );
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
ChangeYaw( pev->yaw_speed );
2016-06-04 15:24:23 +02:00
UTIL_MakeVectors( pev->angles );
2016-07-31 15:48:50 +02:00
if( RANDOM_LONG( 0, 7 ) == 1 )
2016-06-04 15:24:23 +02:00
{
// randomly check for blocked path.(more random load balancing)
2016-07-31 15:48:50 +02:00
if( !WALK_MOVE( ENT( pev ), pev->ideal_yaw, 4, WALKMOVE_NORMAL ) )
2016-06-04 15:24:23 +02:00
{
// stuck, so just pick a new spot to run off to
PickNewDest( m_iMode );
}
}
2016-07-31 15:48:50 +02:00
WALK_MOVE( ENT( pev ), pev->ideal_yaw, m_flGroundSpeed * flInterval, WALKMOVE_NORMAL );
2016-06-04 15:24:23 +02:00
// if the waypoint is closer than step size, then stop after next step (ok for roach to overshoot)
2016-07-31 15:48:50 +02:00
if( flWaypointDist <= m_flGroundSpeed * flInterval )
2016-06-04 15:24:23 +02:00
{
// take truncated step and stop
2016-07-31 15:48:50 +02:00
SetActivity( ACT_IDLE );
m_flLastLightLevel = GETENTITYILLUM( ENT( pev ) );// this is roach's new comfortable light level
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
if( m_iMode == ROACH_SMELL_FOOD )
2016-06-04 15:24:23 +02:00
{
m_iMode = ROACH_EAT;
}
else
{
m_iMode = ROACH_IDLE;
}
}
2016-07-31 15:48:50 +02:00
if( RANDOM_LONG( 0, 149 ) == 1 && m_iMode != ROACH_SCARED_BY_LIGHT && m_iMode != ROACH_SMELL_FOOD )
2016-06-04 15:24:23 +02:00
{
// 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.
//=========================================================
2016-07-31 15:48:50 +02:00
void CRoach::Look( int iDistance )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
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;
2016-06-04 15:24:23 +02:00
// DON'T let visibility information from last frame sit around!
2016-07-31 15:48:50 +02:00
ClearConditions( bits_COND_SEE_HATE | bits_COND_SEE_DISLIKE | bits_COND_SEE_ENEMY | bits_COND_SEE_FEAR );
2016-06-04 15:24:23 +02:00
// 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!
2016-07-31 15:48:50 +02:00
if( FNullEnt( FIND_CLIENT_IN_PVS( edict() ) ) )
2016-06-04 15:24:23 +02:00
{
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!
2016-07-31 15:48:50 +02:00
while( ( pSightEnt = UTIL_FindEntityInSphere( pSightEnt, pev->origin, iDistance ) ) != NULL )
2016-06-04 15:24:23 +02:00
{
// only consider ents that can be damaged. !!!temporarily only considering other monsters and clients
2016-07-31 15:48:50 +02:00
if( pSightEnt->IsPlayer() || FBitSet( pSightEnt->pev->flags, FL_MONSTER ) )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
if( /*FVisible( pSightEnt ) &&*/ !FBitSet( pSightEnt->pev->flags, FL_NOTARGET ) && pSightEnt->pev->health > 0 )
2016-06-04 15:24:23 +02:00
{
// 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.
2016-07-31 15:48:50 +02:00
switch( IRelationship( pSightEnt ) )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
case R_FR:
2016-06-04 15:24:23 +02:00
iSighted |= bits_COND_SEE_FEAR;
break;
2016-07-31 15:48:50 +02:00
case R_NO:
2016-06-04 15:24:23 +02:00
break;
default:
2016-07-31 15:48:50 +02:00
ALERT( at_console, "%s can't asses %s\n", STRING( pev->classname ), STRING( pSightEnt->pev->classname ) );
2016-06-04 15:24:23 +02:00
break;
}
}
}
}
SetConditions( iSighted );
}
//=========================================================
// AI Schedules Specific to this monster
//=========================================================