hlsdk-xash3d/dlls/squeakgrenade.cpp

411 lines
10 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.
*
* Use, distribution, and modification of this source code and/or resulting
* object code is restricted to non-commercial enhancements to products from
* Valve LLC. All other use, distribution, or modification is prohibited
* without written permission from Valve LLC.
*
****/
2021-06-07 16:17:22 +02:00
#if !OEM_BUILD && !HLDEMO_BUILD
2016-06-04 15:24:23 +02:00
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "monsters.h"
#include "weapons.h"
#include "nodes.h"
#include "player.h"
#include "soundent.h"
#include "gamerules.h"
2016-07-31 15:48:50 +02:00
enum w_squeak_e
{
2016-06-04 15:24:23 +02:00
WSQUEAK_IDLE1 = 0,
WSQUEAK_FIDGET,
WSQUEAK_JUMP,
WSQUEAK_RUN
2016-06-04 15:24:23 +02:00
};
2016-07-31 15:48:50 +02:00
enum squeak_e
{
2016-06-04 15:24:23 +02:00
SQUEAK_IDLE1 = 0,
SQUEAK_FIDGETFIT,
SQUEAK_FIDGETNIP,
SQUEAK_DOWN,
SQUEAK_UP,
SQUEAK_THROW
};
2021-06-07 02:05:58 +02:00
#if !CLIENT_DLL
2016-06-04 15:24:23 +02:00
class CSqueakGrenade : public CGrenade
{
void Spawn( void );
void Precache( void );
2016-07-31 15:48:50 +02:00
int Classify( void );
2016-06-04 15:24:23 +02:00
void EXPORT SuperBounceTouch( CBaseEntity *pOther );
void EXPORT HuntThink( void );
2016-07-31 15:48:50 +02:00
int BloodColor( void ) { return BLOOD_COLOR_YELLOW; }
2016-06-04 15:24:23 +02:00
void Killed( entvars_t *pevAttacker, int iGib );
void GibMonster( void );
2016-07-31 15:48:50 +02:00
virtual int Save( CSave &save );
virtual int Restore( CRestore &restore );
2016-07-31 15:48:50 +02:00
static TYPEDESCRIPTION m_SaveData[];
2016-06-04 15:24:23 +02:00
static float m_flNextBounceSoundTime;
// CBaseEntity *m_pTarget;
float m_flDie;
Vector m_vecTarget;
float m_flNextHunt;
float m_flNextHit;
Vector m_posPrev;
EHANDLE m_hOwner;
2016-07-31 15:48:50 +02:00
int m_iMyClass;
2016-06-04 15:24:23 +02:00
};
float CSqueakGrenade::m_flNextBounceSoundTime = 0;
LINK_ENTITY_TO_CLASS( monster_snark, CSqueakGrenade )
TYPEDESCRIPTION CSqueakGrenade::m_SaveData[] =
2016-06-04 15:24:23 +02:00
{
DEFINE_FIELD( CSqueakGrenade, m_flDie, FIELD_TIME ),
DEFINE_FIELD( CSqueakGrenade, m_vecTarget, FIELD_VECTOR ),
DEFINE_FIELD( CSqueakGrenade, m_flNextHunt, FIELD_TIME ),
DEFINE_FIELD( CSqueakGrenade, m_flNextHit, FIELD_TIME ),
DEFINE_FIELD( CSqueakGrenade, m_posPrev, FIELD_POSITION_VECTOR ),
DEFINE_FIELD( CSqueakGrenade, m_hOwner, FIELD_EHANDLE ),
};
IMPLEMENT_SAVERESTORE( CSqueakGrenade, CGrenade )
2016-06-04 15:24:23 +02:00
2019-10-13 13:49:25 +02:00
#define SQUEEK_DETONATE_DELAY 15.0f
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
int CSqueakGrenade::Classify( void )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
if( m_iMyClass != 0 )
2016-06-04 15:24:23 +02:00
return m_iMyClass; // protect against recursion
2017-06-29 15:56:03 +02:00
if( m_hEnemy != 0 )
2016-06-04 15:24:23 +02:00
{
m_iMyClass = CLASS_INSECT; // no one cares about it
2016-07-31 15:48:50 +02:00
switch( m_hEnemy->Classify() )
2016-06-04 15:24:23 +02:00
{
case CLASS_PLAYER:
case CLASS_HUMAN_PASSIVE:
case CLASS_HUMAN_MILITARY:
m_iMyClass = 0;
return CLASS_ALIEN_MILITARY; // barney's get mad, grunts get mad at it
}
m_iMyClass = 0;
}
return CLASS_ALIEN_BIOWEAPON;
}
2016-07-31 15:48:50 +02:00
void CSqueakGrenade::Spawn( void )
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
// motor
pev->movetype = MOVETYPE_BOUNCE;
pev->solid = SOLID_BBOX;
2016-07-31 15:48:50 +02:00
SET_MODEL( ENT( pev ), "models/w_squeak.mdl" );
2019-10-13 13:49:25 +02:00
UTIL_SetSize( pev, Vector( -4.0f, -4.0f, 0.0f ), Vector( 4.0f, 4.0f, 8.0f ) );
2016-06-04 15:24:23 +02:00
UTIL_SetOrigin( pev, pev->origin );
SetTouch( &CSqueakGrenade::SuperBounceTouch );
SetThink( &CSqueakGrenade::HuntThink );
2019-10-13 13:49:25 +02:00
pev->nextthink = gpGlobals->time + 0.1f;
m_flNextHunt = gpGlobals->time + (float)1E6;
2016-06-04 15:24:23 +02:00
pev->flags |= FL_MONSTER;
2016-07-31 15:48:50 +02:00
pev->takedamage = DAMAGE_AIM;
pev->health = gSkillData.snarkHealth;
2019-10-13 13:49:25 +02:00
pev->gravity = 0.5f;
pev->friction = 0.5f;
2016-06-04 15:24:23 +02:00
pev->dmg = gSkillData.snarkDmgPop;
m_flDie = gpGlobals->time + SQUEEK_DETONATE_DELAY;
2019-10-13 13:49:25 +02:00
m_flFieldOfView = 0.0f; // 180 degrees
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
if( pev->owner )
2016-06-04 15:24:23 +02:00
m_hOwner = Instance( pev->owner );
m_flNextBounceSoundTime = gpGlobals->time;// reset each time a snark is spawned.
pev->sequence = WSQUEAK_RUN;
2016-07-31 15:48:50 +02:00
ResetSequenceInfo();
2016-06-04 15:24:23 +02:00
}
void CSqueakGrenade::Precache( void )
{
2016-07-31 15:48:50 +02:00
PRECACHE_MODEL( "models/w_squeak.mdl" );
PRECACHE_SOUND( "squeek/sqk_blast1.wav" );
PRECACHE_SOUND( "common/bodysplat.wav" );
PRECACHE_SOUND( "squeek/sqk_die1.wav" );
PRECACHE_SOUND( "squeek/sqk_hunt1.wav" );
PRECACHE_SOUND( "squeek/sqk_hunt2.wav" );
PRECACHE_SOUND( "squeek/sqk_hunt3.wav" );
PRECACHE_SOUND( "squeek/sqk_deploy1.wav" );
2016-06-04 15:24:23 +02:00
}
2016-07-31 15:48:50 +02:00
void CSqueakGrenade::Killed( entvars_t *pevAttacker, int iGib )
2016-06-04 15:24:23 +02:00
{
pev->model = iStringNull;// make invisible
SetThink( &CBaseEntity::SUB_Remove );
SetTouch( NULL );
2019-10-13 13:49:25 +02:00
pev->nextthink = gpGlobals->time + 0.1f;
2016-06-04 15:24:23 +02:00
// since squeak grenades never leave a body behind, clear out their takedamage now.
// Squeaks do a bit of radius damage when they pop, and that radius damage will
// continue to call this function unless we acknowledge the Squeak's death now. (sjb)
pev->takedamage = DAMAGE_NO;
// play squeek blast
2019-10-13 13:49:25 +02:00
EMIT_SOUND_DYN( ENT( pev ), CHAN_ITEM, "squeek/sqk_blast1.wav", 1, 0.5f, 0, PITCH_NORM );
2016-06-04 15:24:23 +02:00
2019-10-13 13:49:25 +02:00
CSoundEnt::InsertSound( bits_SOUND_COMBAT, pev->origin, SMALL_EXPLOSION_VOLUME, 3.0f );
2016-06-04 15:24:23 +02:00
UTIL_BloodDrips( pev->origin, g_vecZero, BloodColor(), 80 );
2017-06-29 15:56:03 +02:00
if( m_hOwner != 0 )
2016-07-31 15:48:50 +02:00
RadiusDamage( pev, m_hOwner->pev, pev->dmg, CLASS_NONE, DMG_BLAST );
2016-06-04 15:24:23 +02:00
else
2016-07-31 15:48:50 +02:00
RadiusDamage( pev, pev, pev->dmg, CLASS_NONE, DMG_BLAST );
2016-06-04 15:24:23 +02:00
// reset owner so death message happens
2017-06-29 15:56:03 +02:00
if( m_hOwner != 0 )
2016-06-04 15:24:23 +02:00
pev->owner = m_hOwner->edict();
2016-07-31 15:48:50 +02:00
CBaseMonster::Killed( pevAttacker, GIB_ALWAYS );
2016-06-04 15:24:23 +02:00
}
2016-07-31 15:48:50 +02:00
void CSqueakGrenade::GibMonster( void )
2016-06-04 15:24:23 +02:00
{
2019-10-13 13:49:25 +02:00
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "common/bodysplat.wav", 0.75f, ATTN_NORM, 0, 200 );
2016-06-04 15:24:23 +02:00
}
void CSqueakGrenade::HuntThink( void )
{
// ALERT( at_console, "think\n" );
2016-07-31 15:48:50 +02:00
if( !IsInWorld() )
2016-06-04 15:24:23 +02:00
{
SetTouch( NULL );
UTIL_Remove( this );
return;
}
2016-07-31 15:48:50 +02:00
StudioFrameAdvance();
2019-10-13 13:49:25 +02:00
pev->nextthink = gpGlobals->time + 0.1f;
2016-06-04 15:24:23 +02:00
// explode when ready
2016-07-31 15:48:50 +02:00
if( gpGlobals->time >= m_flDie )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
g_vecAttackDir = pev->velocity.Normalize();
2016-06-04 15:24:23 +02:00
pev->health = -1;
Killed( pev, 0 );
return;
}
// float
2016-07-31 15:48:50 +02:00
if( pev->waterlevel != 0 )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
if( pev->movetype == MOVETYPE_BOUNCE )
2016-06-04 15:24:23 +02:00
{
pev->movetype = MOVETYPE_FLY;
}
2019-10-13 13:49:25 +02:00
pev->velocity = pev->velocity * 0.9f;
pev->velocity.z += 8.0f;
2016-06-04 15:24:23 +02:00
}
2017-06-29 15:56:03 +02:00
else if( pev->movetype == MOVETYPE_FLY )
2016-06-04 15:24:23 +02:00
{
pev->movetype = MOVETYPE_BOUNCE;
}
// return if not time to hunt
2016-07-31 15:48:50 +02:00
if( m_flNextHunt > gpGlobals->time )
2016-06-04 15:24:23 +02:00
return;
2019-10-13 13:49:25 +02:00
m_flNextHunt = gpGlobals->time + 2.0f;
2017-06-29 15:56:03 +02:00
//CBaseEntity *pOther = NULL;
2016-06-04 15:24:23 +02:00
Vector vecDir;
TraceResult tr;
Vector vecFlat = pev->velocity;
vecFlat.z = 0;
2016-07-31 15:48:50 +02:00
vecFlat = vecFlat.Normalize();
2016-06-04 15:24:23 +02:00
UTIL_MakeVectors( pev->angles );
2017-06-29 15:56:03 +02:00
if( m_hEnemy == 0 || !m_hEnemy->IsAlive() )
2016-06-04 15:24:23 +02:00
{
// find target, bounce a bit towards it.
Look( 512 );
2016-07-31 15:48:50 +02:00
m_hEnemy = BestVisibleEnemy();
2016-06-04 15:24:23 +02:00
}
// squeek if it's about time blow up
2019-10-13 13:49:25 +02:00
if( ( m_flDie - gpGlobals->time <= 0.5f ) && ( m_flDie - gpGlobals->time >= 0.3f ) )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "squeek/sqk_die1.wav", 1, ATTN_NORM, 0, 100 + RANDOM_LONG( 0, 0x3F ) );
2019-10-13 13:49:25 +02:00
CSoundEnt::InsertSound( bits_SOUND_COMBAT, pev->origin, 256, 0.25f );
2016-06-04 15:24:23 +02:00
}
// higher pitch as squeeker gets closer to detonation time
/*float flpitch = 155.0f - 60.0f * ( ( m_flDie - gpGlobals->time ) / SQUEEK_DETONATE_DELAY );
2019-10-13 13:49:25 +02:00
if( flpitch < 80.0f )
flpitch = 80.0f;*/
2016-06-04 15:24:23 +02:00
2017-06-29 15:56:03 +02:00
if( m_hEnemy != 0 )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
if( FVisible( m_hEnemy ) )
2016-06-04 15:24:23 +02:00
{
vecDir = m_hEnemy->EyePosition() - pev->origin;
2016-07-31 15:48:50 +02:00
m_vecTarget = vecDir.Normalize();
2016-06-04 15:24:23 +02:00
}
float flVel = pev->velocity.Length();
2019-10-13 13:49:25 +02:00
float flAdj = 50.0f / ( flVel + 10.0f );
2016-06-04 15:24:23 +02:00
2019-10-13 13:49:25 +02:00
if( flAdj > 1.2f )
flAdj = 1.2f;
2016-06-04 15:24:23 +02:00
// ALERT( at_console, "think : enemy\n");
// ALERT( at_console, "%.0f %.2f %.2f %.2f\n", flVel, m_vecTarget.x, m_vecTarget.y, m_vecTarget.z );
2019-10-13 13:49:25 +02:00
pev->velocity = pev->velocity * flAdj + m_vecTarget * 300.0f;
2016-06-04 15:24:23 +02:00
}
2016-07-31 15:48:50 +02:00
if( pev->flags & FL_ONGROUND )
2016-06-04 15:24:23 +02:00
{
pev->avelocity = Vector( 0, 0, 0 );
}
else
{
2016-07-31 15:48:50 +02:00
if( pev->avelocity == Vector( 0, 0, 0 ) )
2016-06-04 15:24:23 +02:00
{
pev->avelocity.x = RANDOM_FLOAT( -100, 100 );
pev->avelocity.z = RANDOM_FLOAT( -100, 100 );
}
}
2019-10-13 13:49:25 +02:00
if( ( pev->origin - m_posPrev ).Length() < 1.0f )
2016-06-04 15:24:23 +02:00
{
pev->velocity.x = RANDOM_FLOAT( -100, 100 );
pev->velocity.y = RANDOM_FLOAT( -100, 100 );
}
m_posPrev = pev->origin;
pev->angles = UTIL_VecToAngles( pev->velocity );
pev->angles.z = 0;
pev->angles.x = 0;
}
void CSqueakGrenade::SuperBounceTouch( CBaseEntity *pOther )
{
2016-07-31 15:48:50 +02:00
float flpitch;
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
TraceResult tr = UTIL_GetGlobalTrace();
2016-06-04 15:24:23 +02:00
// don't hit the guy that launched this grenade
2016-07-31 15:48:50 +02:00
if( pev->owner && pOther->edict() == pev->owner )
2016-06-04 15:24:23 +02:00
return;
// at least until we've bounced once
pev->owner = NULL;
2019-10-13 13:49:25 +02:00
pev->angles.x = 0.0f;
pev->angles.z = 0.0f;
2016-06-04 15:24:23 +02:00
// avoid bouncing too much
2016-07-31 15:48:50 +02:00
if( m_flNextHit > gpGlobals->time )
2016-06-04 15:24:23 +02:00
return;
// higher pitch as squeeker gets closer to detonation time
2019-10-13 13:49:25 +02:00
flpitch = 155.0f - 60.0f * ( ( m_flDie - gpGlobals->time ) / SQUEEK_DETONATE_DELAY );
2016-06-04 15:24:23 +02:00
if( !FBitSet( pOther->pev->flags, FL_WORLDBRUSH )
&& pOther->pev->takedamage && m_flNextAttack < gpGlobals->time )
2016-06-04 15:24:23 +02:00
{
// attack!
// make sure it's me who has touched them
2016-07-31 15:48:50 +02:00
if( tr.pHit == pOther->edict() )
2016-06-04 15:24:23 +02:00
{
// and it's not another squeakgrenade
2016-07-31 15:48:50 +02:00
if( tr.pHit->v.modelindex != pev->modelindex )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
// ALERT( at_console, "hit enemy\n" );
2017-06-29 15:56:03 +02:00
ClearMultiDamage();
2016-07-31 15:48:50 +02:00
pOther->TraceAttack( pev, gSkillData.snarkDmgBite, gpGlobals->v_forward, &tr, DMG_SLASH );
2017-06-29 15:56:03 +02:00
if( m_hOwner != 0 )
2016-06-04 15:24:23 +02:00
ApplyMultiDamage( pev, m_hOwner->pev );
else
ApplyMultiDamage( pev, pev );
pev->dmg += gSkillData.snarkDmgPop; // add more explosion damage
2019-10-13 13:49:25 +02:00
// m_flDie += 2.0f; // add more life
2016-06-04 15:24:23 +02:00
// make bite sound
2019-10-13 13:49:25 +02:00
EMIT_SOUND_DYN( ENT( pev ), CHAN_WEAPON, "squeek/sqk_deploy1.wav", 1.0f, ATTN_NORM, 0, (int)flpitch );
m_flNextAttack = gpGlobals->time + 0.5f;
2016-06-04 15:24:23 +02:00
}
}
else
{
2016-07-31 15:48:50 +02:00
// ALERT( at_console, "been hit\n" );
2016-06-04 15:24:23 +02:00
}
}
2019-10-13 13:49:25 +02:00
m_flNextHit = gpGlobals->time + 0.1f;
2016-06-04 15:24:23 +02:00
m_flNextHunt = gpGlobals->time;
2016-07-31 15:48:50 +02:00
if( g_pGameRules->IsMultiplayer() )
2016-06-04 15:24:23 +02:00
{
// in multiplayer, we limit how often snarks can make their bounce sounds to prevent overflows.
2016-07-31 15:48:50 +02:00
if( gpGlobals->time < m_flNextBounceSoundTime )
2016-06-04 15:24:23 +02:00
{
// too soon!
return;
}
}
2016-07-31 15:48:50 +02:00
if( !( pev->flags & FL_ONGROUND ) )
2016-06-04 15:24:23 +02:00
{
// play bounce sound
2019-10-13 13:49:25 +02:00
float flRndSound = RANDOM_FLOAT( 0.0f, 1.0f );
2016-07-31 15:48:50 +02:00
2019-10-13 13:49:25 +02:00
if( flRndSound <= 0.33f )
2016-07-31 15:48:50 +02:00
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "squeek/sqk_hunt1.wav", 1, ATTN_NORM, 0, (int)flpitch );
2019-10-13 13:49:25 +02:00
else if( flRndSound <= 0.66f )
2016-07-31 15:48:50 +02:00
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "squeek/sqk_hunt2.wav", 1, ATTN_NORM, 0, (int)flpitch );
else
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "squeek/sqk_hunt3.wav", 1, ATTN_NORM, 0, (int)flpitch );
2019-10-13 13:49:25 +02:00
CSoundEnt::InsertSound( bits_SOUND_COMBAT, pev->origin, 256, 0.25f );
2016-06-04 15:24:23 +02:00
}
else
{
// skittering sound
2019-10-13 13:49:25 +02:00
CSoundEnt::InsertSound( bits_SOUND_COMBAT, pev->origin, 100, 0.1f );
2016-06-04 15:24:23 +02:00
}
2019-10-13 13:49:25 +02:00
m_flNextBounceSoundTime = gpGlobals->time + 0.5f;// half second.
2016-06-04 15:24:23 +02:00
}
#endif
2016-04-17 22:30:05 +02:00
#endif