2
0
mirror of https://github.com/FWGS/hlsdk-xash3d synced 2024-11-11 12:49:49 +01:00
hlsdk-xash3d/dlls/leech.cpp

690 lines
18 KiB
C++
Raw Normal View History

2017-12-18 00:39:44 +01: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.
*
****/
//=========================================================
// leech - basic little swimming monster
//=========================================================
//
// UNDONE:
// DONE:Steering force model for attack
// DONE:Attack animation control / damage
// DONE:Establish range of up/down motion and steer around vertical obstacles
// DONE:Re-evaluate height periodically
// DONE:Fall (MOVETYPE_TOSS) and play different anim if out of water
// Test in complex room (c2a3?)
// DONE:Sounds? - Kelly will fix
// Blood cloud? Hurt effect?
// Group behavior?
// DONE:Save/restore
// Flop animation - just bind to ACT_TWITCH
// Fix fatal push into wall case
//
// Try this on a bird
// Try this on a model with hulls/tracehull?
//
#include "float.h"
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "monsters.h"
// Animation events
#define LEECH_AE_ATTACK 1
#define LEECH_AE_FLOP 2
// Movement constants
2016-08-01 13:13:13 +02:00
#define LEECH_ACCELERATE 10
#define LEECH_CHECK_DIST 45
2019-10-13 13:49:25 +02:00
#define LEECH_SWIM_SPEED 50.0f
#define LEECH_SWIM_ACCEL 80.0f
#define LEECH_SWIM_DECEL 10.0f
2016-08-01 13:13:13 +02:00
#define LEECH_TURN_RATE 90
#define LEECH_SIZEX 10
2019-10-13 13:49:25 +02:00
#define LEECH_FRAMETIME 0.1f
2017-12-18 00:39:44 +01:00
#define DEBUG_BEAMS 0
#if DEBUG_BEAMS
#include "effects.h"
#endif
class CLeech : public CBaseMonster
{
public:
void Spawn( void );
void Precache( void );
void EXPORT SwimThink( void );
void EXPORT DeadThink( void );
void Touch( CBaseEntity *pOther )
{
2016-08-01 13:13:13 +02:00
if( pOther->IsPlayer() )
2017-12-18 00:39:44 +01:00
{
// If the client is pushing me, give me some base velocity
2016-08-01 13:13:13 +02:00
if( gpGlobals->trace_ent && gpGlobals->trace_ent == edict() )
2017-12-18 00:39:44 +01:00
{
pev->basevelocity = pOther->pev->velocity;
pev->flags |= FL_BASEVELOCITY;
}
}
}
void SetObjectCollisionBox( void )
{
2016-08-01 13:13:13 +02:00
pev->absmin = pev->origin + Vector( -8, -8, 0 );
pev->absmax = pev->origin + Vector( 8, 8, 2 );
2017-12-18 00:39:44 +01:00
}
void AttackSound( void );
void AlertSound( void );
void UpdateMotion( void );
float ObstacleDistance( CBaseEntity *pTarget );
void MakeVectors( void );
void RecalculateWaterlevel( void );
void SwitchLeechState( void );
// Base entity functions
void HandleAnimEvent( MonsterEvent_t *pEvent );
2016-08-01 13:13:13 +02:00
int BloodColor( void ) { return DONT_BLEED; }
2017-12-18 00:39:44 +01:00
void Killed( entvars_t *pevAttacker, int iGib );
void Activate( void );
int TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType );
2016-08-01 13:13:13 +02:00
int Classify( void ) { return CLASS_INSECT; }
2017-12-18 00:39:44 +01:00
int IRelationship( CBaseEntity *pTarget );
2016-08-01 13:13:13 +02:00
virtual int Save( CSave &save );
virtual int Restore( CRestore &restore );
static TYPEDESCRIPTION m_SaveData[];
2017-12-18 00:39:44 +01:00
static const char *pAttackSounds[];
static const char *pAlertSounds[];
private:
// UNDONE: Remove unused boid vars, do group behavior
2016-08-01 13:13:13 +02:00
float m_flTurning;// is this boid turning?
BOOL m_fPathBlocked;// TRUE if there is an obstacle ahead
float m_flAccelerate;
float m_obstacle;
float m_top;
float m_bottom;
float m_height;
float m_waterTime;
float m_sideTime; // Timer to randomly check clearance on sides
float m_zTime;
float m_stateTime;
float m_attackSoundTime;
2017-12-18 00:39:44 +01:00
#if DEBUG_BEAMS
2016-08-01 13:13:13 +02:00
CBeam *m_pb;
CBeam *m_pt;
2017-12-18 00:39:44 +01:00
#endif
};
LINK_ENTITY_TO_CLASS( monster_leech, CLeech )
2017-12-18 00:39:44 +01:00
TYPEDESCRIPTION CLeech::m_SaveData[] =
2017-12-18 00:39:44 +01:00
{
DEFINE_FIELD( CLeech, m_flTurning, FIELD_FLOAT ),
DEFINE_FIELD( CLeech, m_fPathBlocked, FIELD_BOOLEAN ),
DEFINE_FIELD( CLeech, m_flAccelerate, FIELD_FLOAT ),
DEFINE_FIELD( CLeech, m_obstacle, FIELD_FLOAT ),
DEFINE_FIELD( CLeech, m_top, FIELD_FLOAT ),
DEFINE_FIELD( CLeech, m_bottom, FIELD_FLOAT ),
DEFINE_FIELD( CLeech, m_height, FIELD_FLOAT ),
DEFINE_FIELD( CLeech, m_waterTime, FIELD_TIME ),
DEFINE_FIELD( CLeech, m_sideTime, FIELD_TIME ),
DEFINE_FIELD( CLeech, m_zTime, FIELD_TIME ),
DEFINE_FIELD( CLeech, m_stateTime, FIELD_TIME ),
DEFINE_FIELD( CLeech, m_attackSoundTime, FIELD_TIME ),
};
IMPLEMENT_SAVERESTORE( CLeech, CBaseMonster )
2017-12-18 00:39:44 +01:00
const char *CLeech::pAttackSounds[] =
{
"leech/leech_bite1.wav",
"leech/leech_bite2.wav",
"leech/leech_bite3.wav",
};
const char *CLeech::pAlertSounds[] =
{
"leech/leech_alert1.wav",
"leech/leech_alert2.wav",
};
void CLeech::Spawn( void )
{
Precache();
if (pev->model)
SET_MODEL(ENT(pev), STRING(pev->model)); //LRC
else
2017-12-18 21:47:12 +01:00
SET_MODEL( ENT( pev ), "models/leech.mdl" );
2017-12-18 00:39:44 +01:00
// Just for fun
2016-08-01 13:13:13 +02:00
// SET_MODEL( ENT( pev ), "models/icky.mdl" );
2017-12-18 00:39:44 +01:00
//UTIL_SetSize( pev, g_vecZero, g_vecZero );
2016-08-01 13:13:13 +02:00
UTIL_SetSize( pev, Vector( -1, -1, 0 ), Vector( 1, 1, 2 ) );
2017-12-18 00:39:44 +01:00
// Don't push the minz down too much or the water check will fail because this entity is really point-sized
2016-08-01 13:13:13 +02:00
pev->solid = SOLID_SLIDEBOX;
pev->movetype = MOVETYPE_FLY;
SetBits( pev->flags, FL_SWIM );
2017-12-18 00:39:44 +01:00
if (pev->health == 0)
2017-12-18 21:47:12 +01:00
pev->health = gSkillData.leechHealth;
2017-12-18 00:39:44 +01:00
2016-08-01 13:13:13 +02:00
m_flFieldOfView = -0.5; // 180 degree FOV
m_flDistLook = 750;
2017-12-18 00:39:44 +01:00
MonsterInit();
2016-06-04 15:24:23 +02:00
SetThink( &CLeech::SwimThink );
2017-12-18 00:39:44 +01:00
SetUse( NULL );
SetTouch( NULL );
pev->view_ofs = g_vecZero;
m_flTurning = 0;
m_fPathBlocked = FALSE;
SetActivity( ACT_SWIM );
SetState( MONSTERSTATE_IDLE );
m_stateTime = gpGlobals->time + RANDOM_FLOAT( 1, 5 );
}
void CLeech::Activate( void )
{
RecalculateWaterlevel();
CBaseMonster::Activate();
}
void CLeech::RecalculateWaterlevel( void )
{
// Calculate boundaries
2016-08-01 13:13:13 +02:00
Vector vecTest = pev->origin - Vector( 0, 0, 400 );
2017-12-18 00:39:44 +01:00
TraceResult tr;
2016-08-01 13:13:13 +02:00
UTIL_TraceLine( pev->origin, vecTest, missile, edict(), &tr );
2019-10-13 13:49:25 +02:00
if( tr.flFraction != 1.0f )
m_bottom = tr.vecEndPos.z + 1.0f;
2017-12-18 00:39:44 +01:00
else
m_bottom = vecTest.z;
m_top = UTIL_WaterLevel( pev->origin, pev->origin.z, pev->origin.z + 400 ) - 1;
// Chop off 20% of the outside range
2019-10-13 13:49:25 +02:00
float newBottom = m_bottom * 0.8f + m_top * 0.2f;
m_top = m_bottom * 0.2f + m_top * 0.8f;
2017-12-18 00:39:44 +01:00
m_bottom = newBottom;
m_height = RANDOM_FLOAT( m_bottom, m_top );
m_waterTime = gpGlobals->time + RANDOM_FLOAT( 5, 7 );
}
void CLeech::SwitchLeechState( void )
{
m_stateTime = gpGlobals->time + RANDOM_FLOAT( 3, 6 );
2016-08-01 13:13:13 +02:00
if( m_MonsterState == MONSTERSTATE_COMBAT )
2017-12-18 00:39:44 +01:00
{
m_hEnemy = NULL;
SetState( MONSTERSTATE_IDLE );
// We may be up against the player, so redo the side checks
m_sideTime = 0;
}
else
{
Look( m_flDistLook );
CBaseEntity *pEnemy = BestVisibleEnemy();
if ( pEnemy && pEnemy->pev->waterlevel != 0 && pEnemy->pev->watertype != CONTENT_FOG)
{
m_hEnemy = pEnemy;
SetState( MONSTERSTATE_COMBAT );
m_stateTime = gpGlobals->time + RANDOM_FLOAT( 18, 25 );
AlertSound();
}
}
}
int CLeech::IRelationship( CBaseEntity *pTarget )
{
2016-08-01 13:13:13 +02:00
if( pTarget->IsPlayer() )
2017-12-18 00:39:44 +01:00
return R_DL;
return CBaseMonster::IRelationship( pTarget );
}
void CLeech::AttackSound( void )
{
2016-08-01 13:13:13 +02:00
if( gpGlobals->time > m_attackSoundTime )
2017-12-18 00:39:44 +01:00
{
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, RANDOM_SOUND_ARRAY( pAttackSounds ), 1.0f, ATTN_NORM, 0, PITCH_NORM );
2019-10-13 13:49:25 +02:00
m_attackSoundTime = gpGlobals->time + 0.5f;
2017-12-18 00:39:44 +01:00
}
}
void CLeech::AlertSound( void )
{
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, RANDOM_SOUND_ARRAY( pAlertSounds ), 1.0f, ATTN_NORM * 0.5f, 0, PITCH_NORM );
2017-12-18 00:39:44 +01:00
}
void CLeech::Precache( void )
{
2016-08-01 13:13:13 +02:00
//PRECACHE_MODEL( "models/icky.mdl" );
2017-12-18 00:39:44 +01:00
if (pev->model)
2017-12-18 21:47:12 +01:00
PRECACHE_MODEL(STRING(pev->model)); //LRC
2017-12-18 00:39:44 +01:00
else
2017-12-18 21:47:12 +01:00
PRECACHE_MODEL( "models/leech.mdl" );
2017-12-18 00:39:44 +01:00
PRECACHE_SOUND_ARRAY( pAttackSounds );
PRECACHE_SOUND_ARRAY( pAlertSounds );
2017-12-18 00:39:44 +01:00
}
int CLeech::TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType )
{
pev->velocity = g_vecZero;
// Nudge the leech away from the damage
2016-08-01 13:13:13 +02:00
if( pevInflictor )
2017-12-18 00:39:44 +01:00
{
2019-10-13 13:49:25 +02:00
pev->velocity = ( pev->origin - pevInflictor->origin ).Normalize() * 25.0f;
2017-12-18 00:39:44 +01:00
}
return CBaseMonster::TakeDamage( pevInflictor, pevAttacker, flDamage, bitsDamageType );
}
void CLeech::HandleAnimEvent( MonsterEvent_t *pEvent )
{
switch( pEvent->event )
{
case LEECH_AE_ATTACK:
AttackSound();
CBaseEntity *pEnemy;
pEnemy = m_hEnemy;
2016-08-01 13:13:13 +02:00
if( pEnemy != NULL )
2017-12-18 00:39:44 +01:00
{
Vector dir, face;
UTIL_MakeVectorsPrivate( pev->angles, face, NULL, NULL );
face.z = 0;
dir = (pEnemy->pev->origin - pev->origin);
dir.z = 0;
dir = dir.Normalize();
face = face.Normalize();
2019-10-13 13:49:25 +02:00
if( DotProduct( dir, face ) > 0.9f ) // Only take damage if the leech is facing the prey
2017-12-18 00:39:44 +01:00
pEnemy->TakeDamage( pev, pev, gSkillData.leechDmgBite, DMG_SLASH );
}
m_stateTime -= 2;
break;
case LEECH_AE_FLOP:
// Play flop sound
break;
default:
CBaseMonster::HandleAnimEvent( pEvent );
break;
}
}
void CLeech::MakeVectors( void )
{
Vector tmp = pev->angles;
tmp.x = -tmp.x;
2016-08-01 13:13:13 +02:00
UTIL_MakeVectors( tmp );
2017-12-18 00:39:44 +01:00
}
//
// ObstacleDistance - returns normalized distance to obstacle
//
float CLeech::ObstacleDistance( CBaseEntity *pTarget )
{
2016-08-01 13:13:13 +02:00
TraceResult tr;
Vector vecTest;
2017-12-18 00:39:44 +01:00
// use VELOCITY, not angles, not all boids point the direction they are flying
//Vector vecDir = UTIL_VecToAngles( pev->velocity );
MakeVectors();
// check for obstacle ahead
vecTest = pev->origin + gpGlobals->v_forward * LEECH_CHECK_DIST;
2016-08-01 13:13:13 +02:00
UTIL_TraceLine( pev->origin, vecTest, missile, edict(), &tr );
2017-12-18 00:39:44 +01:00
2016-08-01 13:13:13 +02:00
if( tr.fStartSolid )
2017-12-18 00:39:44 +01:00
{
2019-10-13 13:49:25 +02:00
pev->speed = -LEECH_SWIM_SPEED * 0.5f;
2016-08-01 13:13:13 +02:00
//ALERT( at_console, "Stuck from (%f %f %f) to (%f %f %f)\n", pev->oldorigin.x, pev->oldorigin.y, pev->oldorigin.z, pev->origin.x, pev->origin.y, pev->origin.z );
//UTIL_SetOrigin( pev, pev->oldorigin );
2017-12-18 00:39:44 +01:00
}
2019-10-13 13:49:25 +02:00
if( tr.flFraction != 1.0f )
2017-12-18 00:39:44 +01:00
{
2016-08-01 13:13:13 +02:00
if( ( pTarget == NULL || tr.pHit != pTarget->edict() ) )
2017-12-18 00:39:44 +01:00
{
return tr.flFraction;
}
else
{
2016-08-01 13:13:13 +02:00
if( fabs( m_height - pev->origin.z ) > 10 )
2017-12-18 00:39:44 +01:00
return tr.flFraction;
}
}
2016-08-01 13:13:13 +02:00
if( m_sideTime < gpGlobals->time )
2017-12-18 00:39:44 +01:00
{
// extra wide checks
vecTest = pev->origin + gpGlobals->v_right * LEECH_SIZEX * 2 + gpGlobals->v_forward * LEECH_CHECK_DIST;
2016-08-01 13:13:13 +02:00
UTIL_TraceLine( pev->origin, vecTest, missile, edict(), &tr );
2019-10-13 13:49:25 +02:00
if( tr.flFraction != 1.0f )
2017-12-18 00:39:44 +01:00
return tr.flFraction;
vecTest = pev->origin - gpGlobals->v_right * LEECH_SIZEX * 2 + gpGlobals->v_forward * LEECH_CHECK_DIST;
2016-08-01 13:13:13 +02:00
UTIL_TraceLine( pev->origin, vecTest, missile, edict(), &tr );
2019-10-13 13:49:25 +02:00
if( tr.flFraction != 1.0f )
2017-12-18 00:39:44 +01:00
return tr.flFraction;
// Didn't hit either side, so stop testing for another 0.5 - 1 seconds
2019-10-13 13:49:25 +02:00
m_sideTime = gpGlobals->time + RANDOM_FLOAT( 0.5f, 1.0f );
2017-12-18 00:39:44 +01:00
}
return 1.0;
}
void CLeech::DeadThink( void )
{
2016-08-01 13:13:13 +02:00
if( m_fSequenceFinished )
2017-12-18 00:39:44 +01:00
{
2016-08-01 13:13:13 +02:00
if( m_Activity == ACT_DIEFORWARD )
2017-12-18 00:39:44 +01:00
{
SetThink( NULL );
StopAnimation();
return;
}
2016-08-01 13:13:13 +02:00
else if( pev->flags & FL_ONGROUND )
2017-12-18 00:39:44 +01:00
{
pev->solid = SOLID_NOT;
SetActivity(ACT_DIEFORWARD);
}
}
StudioFrameAdvance();
2019-11-10 22:59:56 +01:00
SetNextThink( 0.1f );
2017-12-18 00:39:44 +01:00
// Apply damage velocity, but keep out of the walls
2016-08-01 13:13:13 +02:00
if( pev->velocity.x != 0 || pev->velocity.y != 0 )
2017-12-18 00:39:44 +01:00
{
TraceResult tr;
// Look 0.5 seconds ahead
2019-10-13 13:49:25 +02:00
UTIL_TraceLine( pev->origin, pev->origin + pev->velocity * 0.5f, missile, edict(), &tr );
if( tr.flFraction != 1.0f )
2017-12-18 00:39:44 +01:00
{
2019-10-13 13:49:25 +02:00
pev->velocity.x = 0.0f;
pev->velocity.y = 0.0f;
2017-12-18 00:39:44 +01:00
}
}
}
void CLeech::UpdateMotion( void )
{
2016-08-01 13:13:13 +02:00
float flapspeed = ( pev->speed - m_flAccelerate ) / LEECH_ACCELERATE;
2019-10-13 13:49:25 +02:00
m_flAccelerate = m_flAccelerate * 0.8f + pev->speed * 0.2f;
2017-12-18 00:39:44 +01:00
2016-08-01 13:13:13 +02:00
if( flapspeed < 0 )
2017-12-18 00:39:44 +01:00
flapspeed = -flapspeed;
2019-10-13 13:49:25 +02:00
flapspeed += 1.0f;
if( flapspeed < 0.5f )
flapspeed = 0.5f;
if( flapspeed > 1.9f )
flapspeed = 1.9f;
2017-12-18 00:39:44 +01:00
pev->framerate = flapspeed;
2016-08-01 13:13:13 +02:00
if( !m_fPathBlocked )
2017-12-18 00:39:44 +01:00
pev->avelocity.y = pev->ideal_yaw;
else
pev->avelocity.y = pev->ideal_yaw * m_obstacle;
2019-10-13 13:49:25 +02:00
if( pev->avelocity.y > 150.0f )
2017-12-18 00:39:44 +01:00
m_IdealActivity = ACT_TURN_LEFT;
2019-10-13 13:49:25 +02:00
else if( pev->avelocity.y < -150.0f )
2017-12-18 00:39:44 +01:00
m_IdealActivity = ACT_TURN_RIGHT;
else
m_IdealActivity = ACT_SWIM;
// lean
float targetPitch, delta;
delta = m_height - pev->origin.z;
2016-08-01 13:13:13 +02:00
if( delta < -10 )
2017-12-18 00:39:44 +01:00
targetPitch = -30;
2016-08-01 13:13:13 +02:00
else if( delta > 10 )
2017-12-18 00:39:44 +01:00
targetPitch = 30;
else
targetPitch = 0;
2019-10-13 13:49:25 +02:00
pev->angles.x = UTIL_Approach( targetPitch, pev->angles.x, 60.0f * LEECH_FRAMETIME );
2017-12-18 00:39:44 +01:00
// bank
2019-10-13 13:49:25 +02:00
pev->avelocity.z = -( pev->angles.z + ( pev->avelocity.y * 0.25f ) );
2017-12-18 00:39:44 +01:00
2016-08-01 13:13:13 +02:00
if( m_MonsterState == MONSTERSTATE_COMBAT && HasConditions( bits_COND_CAN_MELEE_ATTACK1 ) )
2017-12-18 00:39:44 +01:00
m_IdealActivity = ACT_MELEE_ATTACK1;
// Out of water check
if ( !pev->waterlevel || pev->watertype == CONTENT_FOG)
{
pev->movetype = MOVETYPE_TOSS;
m_IdealActivity = ACT_TWITCH;
pev->velocity = g_vecZero;
// Animation will intersect the floor if either of these is non-zero
2019-10-13 13:49:25 +02:00
pev->angles.z = 0.0f;
pev->angles.x = 0.0f;
2017-12-18 00:39:44 +01:00
2019-10-13 13:49:25 +02:00
if( pev->framerate < 1.0f )
pev->framerate = 1.0f;
2017-12-18 00:39:44 +01:00
}
2016-08-01 13:13:13 +02:00
else if( pev->movetype == MOVETYPE_TOSS )
2017-12-18 00:39:44 +01:00
{
pev->movetype = MOVETYPE_FLY;
pev->flags &= ~FL_ONGROUND;
RecalculateWaterlevel();
m_waterTime = gpGlobals->time + 2; // Recalc again soon, water may be rising
}
2016-08-01 13:13:13 +02:00
if( m_Activity != m_IdealActivity )
2017-12-18 00:39:44 +01:00
{
2016-08-01 13:13:13 +02:00
SetActivity( m_IdealActivity );
2017-12-18 00:39:44 +01:00
}
float flInterval = StudioFrameAdvance();
2016-08-01 13:13:13 +02:00
DispatchAnimEvents( flInterval );
2017-12-18 00:39:44 +01:00
#if DEBUG_BEAMS
2016-08-01 13:13:13 +02:00
if( !m_pb )
2017-12-18 00:39:44 +01:00
m_pb = CBeam::BeamCreate( "sprites/laserbeam.spr", 5 );
2016-08-01 13:13:13 +02:00
if( !m_pt )
2017-12-18 00:39:44 +01:00
m_pt = CBeam::BeamCreate( "sprites/laserbeam.spr", 5 );
m_pb->PointsInit( pev->origin, pev->origin + gpGlobals->v_forward * LEECH_CHECK_DIST );
2019-10-13 13:49:25 +02:00
m_pt->PointsInit( pev->origin, pev->origin - gpGlobals->v_right * ( pev->avelocity.y * 0.25f ) );
2016-08-01 13:13:13 +02:00
if( m_fPathBlocked )
2017-12-18 00:39:44 +01:00
{
float color = m_obstacle * 30;
2019-10-13 13:49:25 +02:00
if( m_obstacle == 1.0f )
2017-12-18 00:39:44 +01:00
color = 0;
2016-08-01 13:13:13 +02:00
if( color > 255 )
2017-12-18 00:39:44 +01:00
color = 255;
m_pb->SetColor( 255, (int)color, (int)color );
}
else
m_pb->SetColor( 255, 255, 0 );
m_pt->SetColor( 0, 0, 255 );
#endif
}
void CLeech::SwimThink( void )
{
2016-08-01 13:13:13 +02:00
TraceResult tr;
float flLeftSide;
float flRightSide;
float targetSpeed;
float targetYaw = 0;
CBaseEntity *pTarget;
if( FNullEnt( FIND_CLIENT_IN_PVS( edict() ) ) )
2017-12-18 00:39:44 +01:00
{
2019-11-10 22:59:56 +01:00
SetNextThink( RANDOM_FLOAT( 1.0f, 1.5f ) );
2017-12-18 00:39:44 +01:00
pev->velocity = g_vecZero;
return;
}
else
2019-11-10 22:59:56 +01:00
SetNextThink( 0.1f );
2017-12-18 00:39:44 +01:00
targetSpeed = LEECH_SWIM_SPEED;
2016-08-01 13:13:13 +02:00
if( m_waterTime < gpGlobals->time )
2017-12-18 00:39:44 +01:00
RecalculateWaterlevel();
2016-08-01 13:13:13 +02:00
if( m_stateTime < gpGlobals->time )
2017-12-18 00:39:44 +01:00
SwitchLeechState();
ClearConditions( bits_COND_CAN_MELEE_ATTACK1 );
switch( m_MonsterState )
{
case MONSTERSTATE_COMBAT:
pTarget = m_hEnemy;
2016-08-01 13:13:13 +02:00
if( !pTarget )
2017-12-18 00:39:44 +01:00
SwitchLeechState();
else
{
// Chase the enemy's eyes
m_height = pTarget->pev->origin.z + pTarget->pev->view_ofs.z - 5;
// Clip to viable water area
2016-08-01 13:13:13 +02:00
if( m_height < m_bottom )
2017-12-18 00:39:44 +01:00
m_height = m_bottom;
2016-08-01 13:13:13 +02:00
else if( m_height > m_top )
2017-12-18 00:39:44 +01:00
m_height = m_top;
Vector location = pTarget->pev->origin - pev->origin;
location.z += (pTarget->pev->view_ofs.z);
2016-08-01 13:13:13 +02:00
if( location.Length() < 40 )
2017-12-18 00:39:44 +01:00
SetConditions( bits_COND_CAN_MELEE_ATTACK1 );
// Turn towards target ent
targetYaw = UTIL_VecToYaw( location );
targetYaw = UTIL_AngleDiff( targetYaw, UTIL_AngleMod( pev->angles.y ) );
2019-10-13 13:49:25 +02:00
if( targetYaw < ( -LEECH_TURN_RATE * 0.75f ) )
targetYaw = ( -LEECH_TURN_RATE * 0.75f );
else if( targetYaw > ( LEECH_TURN_RATE * 0.75f ) )
targetYaw = ( LEECH_TURN_RATE * 0.75f );
2017-12-18 00:39:44 +01:00
else
targetSpeed *= 2;
}
break;
default:
2016-08-01 13:13:13 +02:00
if( m_zTime < gpGlobals->time )
2017-12-18 00:39:44 +01:00
{
float newHeight = RANDOM_FLOAT( m_bottom, m_top );
2019-10-13 13:49:25 +02:00
m_height = 0.5f * m_height + 0.5f * newHeight;
2017-12-18 00:39:44 +01:00
m_zTime = gpGlobals->time + RANDOM_FLOAT( 1, 4 );
}
2016-08-01 13:13:13 +02:00
if( RANDOM_LONG( 0, 100 ) < 10 )
2017-12-18 00:39:44 +01:00
targetYaw = RANDOM_LONG( -30, 30 );
pTarget = NULL;
2017-12-18 00:39:44 +01:00
// oldorigin test
2016-08-01 13:13:13 +02:00
if( ( pev->origin - pev->oldorigin ).Length() < 1 )
2017-12-18 00:39:44 +01:00
{
// If leech didn't move, there must be something blocking it, so try to turn
m_sideTime = 0;
}
break;
}
m_obstacle = ObstacleDistance( pTarget );
pev->oldorigin = pev->origin;
2019-10-13 13:49:25 +02:00
if( m_obstacle < 0.1f )
m_obstacle = 0.1f;
2017-12-18 00:39:44 +01:00
// is the way ahead clear?
2019-10-13 13:49:25 +02:00
if( m_obstacle == 1.0f )
2017-12-18 00:39:44 +01:00
{
// if the leech is turning, stop the trend.
2016-08-01 13:13:13 +02:00
if( m_flTurning != 0 )
2017-12-18 00:39:44 +01:00
{
m_flTurning = 0;
}
m_fPathBlocked = FALSE;
pev->speed = UTIL_Approach( targetSpeed, pev->speed, LEECH_SWIM_ACCEL * LEECH_FRAMETIME );
pev->velocity = gpGlobals->v_forward * pev->speed;
}
else
{
2019-10-13 13:49:25 +02:00
m_obstacle = 1.0f / m_obstacle;
2017-12-18 00:39:44 +01:00
// IF we get this far in the function, the leader's path is blocked!
m_fPathBlocked = TRUE;
2016-08-01 13:13:13 +02:00
if( m_flTurning == 0 )// something in the way and leech is not already turning to avoid
2017-12-18 00:39:44 +01:00
{
Vector vecTest;
// measure clearance on left and right to pick the best dir to turn
2016-08-01 13:13:13 +02:00
vecTest = pev->origin + ( gpGlobals->v_right * LEECH_SIZEX ) + ( gpGlobals->v_forward * LEECH_CHECK_DIST );
UTIL_TraceLine( pev->origin, vecTest, missile, edict(), &tr );
2017-12-18 00:39:44 +01:00
flRightSide = tr.flFraction;
2016-08-01 13:13:13 +02:00
vecTest = pev->origin + ( gpGlobals->v_right * -LEECH_SIZEX ) + ( gpGlobals->v_forward * LEECH_CHECK_DIST );
UTIL_TraceLine( pev->origin, vecTest, missile, edict(), &tr );
2017-12-18 00:39:44 +01:00
flLeftSide = tr.flFraction;
// turn left, right or random depending on clearance ratio
2016-08-01 13:13:13 +02:00
float delta = ( flRightSide - flLeftSide );
2019-10-13 13:49:25 +02:00
if( delta > 0.1f || ( delta > -0.1f && RANDOM_LONG( 0, 100 ) < 50 ) )
2017-12-18 00:39:44 +01:00
m_flTurning = -LEECH_TURN_RATE;
else
m_flTurning = LEECH_TURN_RATE;
}
2019-10-13 13:49:25 +02:00
pev->speed = UTIL_Approach( -( LEECH_SWIM_SPEED * 0.5f ), pev->speed, LEECH_SWIM_DECEL * LEECH_FRAMETIME * m_obstacle );
2017-12-18 00:39:44 +01:00
pev->velocity = gpGlobals->v_forward * pev->speed;
}
pev->ideal_yaw = m_flTurning + targetYaw;
UpdateMotion();
}
2016-08-01 13:13:13 +02:00
void CLeech::Killed( entvars_t *pevAttacker, int iGib )
2017-12-18 00:39:44 +01:00
{
2016-08-01 13:13:13 +02:00
Vector vecSplatDir;
TraceResult tr;
2017-12-18 00:39:44 +01:00
//ALERT(at_aiconsole, "Leech: killed\n");
// tell owner ( if any ) that we're dead.This is mostly for MonsterMaker functionality.
2016-08-01 13:13:13 +02:00
CBaseEntity *pOwner = CBaseEntity::Instance( pev->owner );
if( pOwner )
pOwner->DeathNotice( pev );
2017-12-18 00:39:44 +01:00
// When we hit the ground, play the "death_end" activity
if ( pev->waterlevel && pev->watertype != CONTENT_FOG)
{
pev->angles.z = 0;
pev->angles.x = 0;
pev->origin.z += 1;
pev->avelocity = g_vecZero;
2016-08-01 13:13:13 +02:00
if( RANDOM_LONG( 0, 99 ) < 70 )
2017-12-18 00:39:44 +01:00
pev->avelocity.y = RANDOM_LONG( -720, 720 );
pev->gravity = 0.02;
2016-08-01 13:13:13 +02:00
ClearBits( pev->flags, FL_ONGROUND );
2017-12-18 00:39:44 +01:00
SetActivity( ACT_DIESIMPLE );
}
else
SetActivity( ACT_DIEFORWARD );
pev->movetype = MOVETYPE_TOSS;
pev->takedamage = DAMAGE_NO;
2016-06-04 15:24:23 +02:00
SetThink( &CLeech::DeadThink );
2017-12-18 00:39:44 +01:00
}