2
0
mirror of https://github.com/FWGS/hlsdk-xash3d synced 2024-11-25 03:09:24 +01:00
hlsdk-xash3d/dlls/ggrenade.cpp

493 lines
13 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.
*
****/
/*
===== generic grenade.cpp ========================================================
*/
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "monsters.h"
#include "weapons.h"
#include "nodes.h"
#include "soundent.h"
#include "decals.h"
#include "game.h"
2016-06-04 15:24:23 +02:00
//===================grenade
LINK_ENTITY_TO_CLASS( grenade, CGrenade )
2016-06-04 15:24:23 +02:00
// Grenades flagged with this will be triggered when the owner calls detonateSatchelCharges
#define SF_DETONATE 0x0001
//
// Grenade Explode
//
void CGrenade::Explode( Vector vecSrc, Vector vecAim )
{
TraceResult tr;
2016-07-31 15:48:50 +02:00
UTIL_TraceLine( pev->origin, pev->origin + Vector( 0, 0, -32 ), ignore_monsters, ENT( pev ), & tr );
2016-06-04 15:24:23 +02:00
Explode( &tr, DMG_BLAST );
}
// UNDONE: temporary scorching for PreAlpha - find a less sleazy permenant solution.
void CGrenade::Explode( TraceResult *pTrace, int bitsDamageType )
{
2017-06-29 15:56:03 +02:00
// float flRndSound;// sound randomizer
2016-06-04 15:24:23 +02:00
pev->model = iStringNull;//invisible
pev->solid = SOLID_NOT;// intangible
pev->takedamage = DAMAGE_NO;
// Pull out of the wall a bit
2019-10-13 13:49:25 +02:00
if( pTrace->flFraction != 1.0f )
2016-06-04 15:24:23 +02:00
{
if (explosionfix.value)
pev->origin = pTrace->vecEndPos + ( pTrace->vecPlaneNormal * 0.6f );
else
pev->origin = pTrace->vecEndPos + ( pTrace->vecPlaneNormal * ( pev->dmg - 24 ) * 0.6f );
2016-06-04 15:24:23 +02:00
}
2016-07-31 15:48:50 +02:00
int iContents = UTIL_PointContents( pev->origin );
2016-06-04 15:24:23 +02:00
MESSAGE_BEGIN( MSG_PAS, SVC_TEMPENTITY, pev->origin );
WRITE_BYTE( TE_EXPLOSION ); // This makes a dynamic light and the explosion sprites/sound
WRITE_COORD( pev->origin.x ); // Send to PAS because of the sound
WRITE_COORD( pev->origin.y );
WRITE_COORD( pev->origin.z );
2016-07-31 15:48:50 +02:00
if( iContents != CONTENTS_WATER )
2016-06-04 15:24:23 +02:00
{
WRITE_SHORT( g_sModelIndexFireball );
}
else
{
WRITE_SHORT( g_sModelIndexWExplosion );
}
2019-10-13 13:49:25 +02:00
WRITE_BYTE( ( pev->dmg - 50 ) * 0.6f ); // scale * 10
2016-07-31 15:48:50 +02:00
WRITE_BYTE( 15 ); // framerate
2016-06-04 15:24:23 +02:00
WRITE_BYTE( TE_EXPLFLAG_NONE );
MESSAGE_END();
2016-07-31 15:48:50 +02:00
CSoundEnt::InsertSound( bits_SOUND_COMBAT, pev->origin, NORMAL_EXPLOSION_VOLUME, 3.0 );
2016-06-04 15:24:23 +02:00
entvars_t *pevOwner;
2016-07-31 15:48:50 +02:00
if( pev->owner )
2016-06-04 15:24:23 +02:00
pevOwner = VARS( pev->owner );
else
pevOwner = NULL;
pev->owner = NULL; // can't traceline attack owner if this is set
2016-07-31 15:48:50 +02:00
RadiusDamage( pev, pevOwner, pev->dmg, CLASS_NONE, bitsDamageType );
2016-06-04 15:24:23 +02:00
2019-10-13 13:49:25 +02:00
if( RANDOM_FLOAT( 0, 1 ) < 0.5f )
2016-06-04 15:24:23 +02:00
{
UTIL_DecalTrace( pTrace, DECAL_SCORCH1 );
}
else
{
UTIL_DecalTrace( pTrace, DECAL_SCORCH2 );
}
2017-06-29 15:56:03 +02:00
//flRndSound = RANDOM_FLOAT( 0, 1 );
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
switch( RANDOM_LONG( 0, 2 ) )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
case 0:
EMIT_SOUND( ENT( pev ), CHAN_VOICE, "weapons/debris1.wav", 0.55, ATTN_NORM );
break;
case 1:
EMIT_SOUND( ENT( pev ), CHAN_VOICE, "weapons/debris2.wav", 0.55, ATTN_NORM );
break;
case 2:
EMIT_SOUND( ENT( pev ), CHAN_VOICE, "weapons/debris3.wav", 0.55, ATTN_NORM );
break;
2016-06-04 15:24:23 +02:00
}
pev->effects |= EF_NODRAW;
SetThink( &CGrenade::Smoke );
pev->velocity = g_vecZero;
2019-10-13 13:49:25 +02:00
pev->nextthink = gpGlobals->time + 0.3f;
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
if( iContents != CONTENTS_WATER )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
int sparkCount = RANDOM_LONG( 0, 3 );
for( int i = 0; i < sparkCount; i++ )
2016-06-04 15:24:23 +02:00
Create( "spark_shower", pev->origin, pTrace->vecPlaneNormal, NULL );
}
}
void CGrenade::Smoke( void )
{
2016-07-31 15:48:50 +02:00
if( UTIL_PointContents( pev->origin ) == CONTENTS_WATER )
2016-06-04 15:24:23 +02:00
{
UTIL_Bubbles( pev->origin - Vector( 64, 64, 64 ), pev->origin + Vector( 64, 64, 64 ), 100 );
}
else
{
MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, pev->origin );
WRITE_BYTE( TE_SMOKE );
WRITE_COORD( pev->origin.x );
WRITE_COORD( pev->origin.y );
WRITE_COORD( pev->origin.z );
WRITE_SHORT( g_sModelIndexSmoke );
2019-10-13 13:49:25 +02:00
WRITE_BYTE( (int)( ( pev->dmg - 50 ) * 0.8f ) ); // scale * 10
2017-06-29 15:56:03 +02:00
WRITE_BYTE( 12 ); // framerate
2016-06-04 15:24:23 +02:00
MESSAGE_END();
}
UTIL_Remove( this );
}
void CGrenade::Killed( entvars_t *pevAttacker, int iGib )
{
2016-07-31 15:48:50 +02:00
Detonate();
2016-06-04 15:24:23 +02:00
}
// Timed grenade, this think is called when time runs out.
void CGrenade::DetonateUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
{
SetThink( &CGrenade::Detonate );
pev->nextthink = gpGlobals->time;
}
void CGrenade::PreDetonate( void )
{
2017-06-29 15:56:03 +02:00
CSoundEnt::InsertSound( bits_SOUND_DANGER, pev->origin, 400, 0.3 );
2016-06-04 15:24:23 +02:00
SetThink( &CGrenade::Detonate );
pev->nextthink = gpGlobals->time + 1;
}
void CGrenade::Detonate( void )
{
TraceResult tr;
2016-07-31 15:48:50 +02:00
Vector vecSpot;// trace starts here!
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
vecSpot = pev->origin + Vector( 0, 0, 8 );
UTIL_TraceLine( vecSpot, vecSpot + Vector( 0, 0, -40 ), ignore_monsters, ENT(pev), &tr );
2016-06-04 15:24:23 +02:00
Explode( &tr, DMG_BLAST );
}
//
// Contact grenade, explode when it touches something
//
void CGrenade::ExplodeTouch( CBaseEntity *pOther )
{
TraceResult tr;
2016-07-31 15:48:50 +02:00
Vector vecSpot;// trace starts here!
2016-06-04 15:24:23 +02:00
pev->enemy = pOther->edict();
vecSpot = pev->origin - pev->velocity.Normalize() * 32;
2016-07-31 15:48:50 +02:00
UTIL_TraceLine( vecSpot, vecSpot + pev->velocity.Normalize() * 64, ignore_monsters, ENT( pev ), &tr );
2016-06-04 15:24:23 +02:00
Explode( &tr, DMG_BLAST );
}
void CGrenade::DangerSoundThink( void )
{
2016-07-31 15:48:50 +02:00
if( !IsInWorld() )
2016-06-04 15:24:23 +02:00
{
UTIL_Remove( this );
return;
}
2019-10-13 13:49:25 +02:00
CSoundEnt::InsertSound( bits_SOUND_DANGER, pev->origin + pev->velocity * 0.5f, (int)pev->velocity.Length(), 0.2 );
pev->nextthink = gpGlobals->time + 0.2f;
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
if( pev->waterlevel != 0 )
2016-06-04 15:24:23 +02:00
{
2019-10-13 13:49:25 +02:00
pev->velocity = pev->velocity * 0.5f;
2016-06-04 15:24:23 +02:00
}
}
void CGrenade::BounceTouch( CBaseEntity *pOther )
{
// don't hit the guy that launched this grenade
2016-07-31 15:48:50 +02:00
if( pOther->edict() == pev->owner )
2016-06-04 15:24:23 +02:00
return;
// only do damage if we're moving fairly fast
2016-07-31 15:48:50 +02:00
if( m_flNextAttack < gpGlobals->time && pev->velocity.Length() > 100 )
2016-06-04 15:24:23 +02:00
{
entvars_t *pevOwner = VARS( pev->owner );
2016-07-31 15:48:50 +02:00
if( pevOwner )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
TraceResult tr = UTIL_GetGlobalTrace();
ClearMultiDamage();
pOther->TraceAttack( pevOwner, 1, gpGlobals->v_forward, &tr, DMG_CLUB );
ApplyMultiDamage( pev, pevOwner );
2016-06-04 15:24:23 +02:00
}
2019-10-13 13:49:25 +02:00
m_flNextAttack = gpGlobals->time + 1.0f; // debounce
2016-06-04 15:24:23 +02:00
}
Vector vecTestVelocity;
2016-07-31 15:48:50 +02:00
// pev->avelocity = Vector( 300, 300, 300 );
2016-06-04 15:24:23 +02:00
// this is my heuristic for modulating the grenade velocity because grenades dropped purely vertical
// or thrown very far tend to slow down too quickly for me to always catch just by testing velocity.
// trimming the Z velocity a bit seems to help quite a bit.
vecTestVelocity = pev->velocity;
2019-10-13 14:12:41 +02:00
vecTestVelocity.z *= 0.45f;
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
if( !m_fRegisteredSound && vecTestVelocity.Length() <= 60 )
2016-06-04 15:24:23 +02:00
{
//ALERT( at_console, "Grenade Registered!: %f\n", vecTestVelocity.Length() );
// grenade is moving really slow. It's probably very close to where it will ultimately stop moving.
// go ahead and emit the danger sound.
2016-07-31 15:48:50 +02:00
2016-06-04 15:24:23 +02:00
// register a radius louder than the explosion, so we make sure everyone gets out of the way
2019-10-13 13:49:25 +02:00
CSoundEnt::InsertSound( bits_SOUND_DANGER, pev->origin, (int)( pev->dmg / 0.4f ), 0.3f );
2016-06-04 15:24:23 +02:00
m_fRegisteredSound = TRUE;
}
2016-07-31 15:48:50 +02:00
if( pev->flags & FL_ONGROUND )
2016-06-04 15:24:23 +02:00
{
// add a bit of static friction
2019-10-13 13:49:25 +02:00
pev->velocity = pev->velocity * 0.8f;
2016-06-04 15:24:23 +02:00
pev->sequence = RANDOM_LONG( 1, 1 );
ResetSequenceInfo();
2016-06-04 15:24:23 +02:00
}
else
{
// play bounce sound
BounceSound();
}
2019-10-13 13:49:25 +02:00
pev->framerate = pev->velocity.Length() / 200.0f;
if( pev->framerate > 1.0f )
pev->framerate = 1.0f;
else if( pev->framerate < 0.5f )
{
2019-10-13 13:49:25 +02:00
pev->framerate = 0.0f;
pev->frame = 0.0f;
}
2016-06-04 15:24:23 +02:00
}
void CGrenade::SlideTouch( CBaseEntity *pOther )
{
// don't hit the guy that launched this grenade
2016-07-31 15:48:50 +02:00
if( pOther->edict() == pev->owner )
2016-06-04 15:24:23 +02:00
return;
2016-07-31 15:48:50 +02:00
// pev->avelocity = Vector( 300, 300, 300 );
if( pev->flags & FL_ONGROUND )
2016-06-04 15:24:23 +02:00
{
// add a bit of static friction
2019-10-13 13:49:25 +02:00
pev->velocity = pev->velocity * 0.95f;
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
if( pev->velocity.x != 0 || pev->velocity.y != 0 )
2016-06-04 15:24:23 +02:00
{
// maintain sliding sound
}
}
else
{
BounceSound();
}
}
2016-07-31 15:48:50 +02:00
void CGrenade::BounceSound( void )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
switch( RANDOM_LONG( 0, 2 ) )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
case 0:
EMIT_SOUND( ENT( pev ), CHAN_VOICE, "weapons/grenade_hit1.wav", 0.25, ATTN_NORM );
break;
case 1:
EMIT_SOUND( ENT( pev ), CHAN_VOICE, "weapons/grenade_hit2.wav", 0.25, ATTN_NORM );
break;
case 2:
EMIT_SOUND( ENT( pev ), CHAN_VOICE, "weapons/grenade_hit3.wav", 0.25, ATTN_NORM );
break;
2016-06-04 15:24:23 +02:00
}
}
2016-07-31 15:48:50 +02:00
void CGrenade::TumbleThink( void )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
if( !IsInWorld() )
2016-06-04 15:24:23 +02:00
{
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
2016-07-31 15:48:50 +02:00
if( pev->dmgtime - 1 < gpGlobals->time )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
CSoundEnt::InsertSound( bits_SOUND_DANGER, pev->origin + pev->velocity * ( pev->dmgtime - gpGlobals->time ), 400, 0.1 );
2016-06-04 15:24:23 +02:00
}
2016-07-31 15:48:50 +02:00
if( pev->dmgtime <= gpGlobals->time )
2016-06-04 15:24:23 +02:00
{
SetThink( &CGrenade::Detonate );
}
2016-07-31 15:48:50 +02:00
if( pev->waterlevel != 0 )
2016-06-04 15:24:23 +02:00
{
2019-10-13 13:49:25 +02:00
pev->velocity = pev->velocity * 0.5f;
pev->framerate = 0.2f;
2016-06-04 15:24:23 +02:00
}
}
2016-07-31 15:48:50 +02:00
void CGrenade::Spawn( void )
2016-06-04 15:24:23 +02:00
{
pev->movetype = MOVETYPE_BOUNCE;
pev->classname = MAKE_STRING( "grenade" );
2016-07-31 15:48:50 +02:00
2016-06-04 15:24:23 +02:00
pev->solid = SOLID_BBOX;
2016-07-31 15:48:50 +02:00
SET_MODEL( ENT( pev ), "models/grenade.mdl" );
UTIL_SetSize( pev, Vector( 0, 0, 0 ), Vector( 0, 0, 0 ) );
2016-06-04 15:24:23 +02:00
pev->dmg = 100;
m_fRegisteredSound = FALSE;
}
CGrenade *CGrenade::ShootContact( entvars_t *pevOwner, Vector vecStart, Vector vecVelocity )
{
CGrenade *pGrenade = GetClassPtr( (CGrenade *)NULL );
pGrenade->Spawn();
// contact grenades arc lower
pGrenade->pev->gravity = 0.5;// lower gravity since grenade is aerodynamic and engine doesn't know it.
UTIL_SetOrigin( pGrenade->pev, vecStart );
pGrenade->pev->velocity = vecVelocity;
2016-07-31 15:48:50 +02:00
pGrenade->pev->angles = UTIL_VecToAngles( pGrenade->pev->velocity );
pGrenade->pev->owner = ENT( pevOwner );
2016-06-04 15:24:23 +02:00
// make monsters afaid of it while in the air
pGrenade->SetThink( &CGrenade::DangerSoundThink );
pGrenade->pev->nextthink = gpGlobals->time;
2016-07-31 15:48:50 +02:00
2016-06-04 15:24:23 +02:00
// Tumble in air
2016-07-31 15:48:50 +02:00
pGrenade->pev->avelocity.x = RANDOM_FLOAT( -100, -500 );
2016-06-04 15:24:23 +02:00
// Explode on contact
pGrenade->SetTouch( &CGrenade::ExplodeTouch );
pGrenade->pev->dmg = gSkillData.plrDmgM203Grenade;
return pGrenade;
}
2016-07-31 15:48:50 +02:00
CGrenade *CGrenade::ShootTimed( entvars_t *pevOwner, Vector vecStart, Vector vecVelocity, float time )
2016-06-04 15:24:23 +02:00
{
CGrenade *pGrenade = GetClassPtr( (CGrenade *)NULL );
pGrenade->Spawn();
UTIL_SetOrigin( pGrenade->pev, vecStart );
pGrenade->pev->velocity = vecVelocity;
2016-07-31 15:48:50 +02:00
pGrenade->pev->angles = UTIL_VecToAngles( pGrenade->pev->velocity );
pGrenade->pev->owner = ENT( pevOwner );
2016-06-04 15:24:23 +02:00
pGrenade->SetTouch( &CGrenade::BounceTouch ); // Bounce if touched
2016-07-31 15:48:50 +02:00
2016-06-04 15:24:23 +02:00
// Take one second off of the desired detonation time and set the think to PreDetonate. PreDetonate
// will insert a DANGER sound into the world sound list and delay detonation for one second so that
// the grenade explodes after the exact amount of time specified in the call to ShootTimed().
pGrenade->pev->dmgtime = gpGlobals->time + time;
pGrenade->SetThink( &CGrenade::TumbleThink );
2019-10-13 13:49:25 +02:00
pGrenade->pev->nextthink = gpGlobals->time + 0.1f;
if( time < 0.1f )
2016-06-04 15:24:23 +02:00
{
pGrenade->pev->nextthink = gpGlobals->time;
pGrenade->pev->velocity = Vector( 0, 0, 0 );
}
2016-07-31 15:48:50 +02:00
SET_MODEL( ENT( pGrenade->pev ), "models/w_grenade.mdl" );
2016-06-04 15:24:23 +02:00
pGrenade->pev->sequence = RANDOM_LONG( 3, 6 );
pGrenade->ResetSequenceInfo();
2019-10-13 13:49:25 +02:00
pGrenade->pev->framerate = 1.0f;
2016-06-04 15:24:23 +02:00
// Tumble through the air
// pGrenade->pev->avelocity.x = -400;
2019-10-13 13:49:25 +02:00
pGrenade->pev->gravity = 0.5f;
pGrenade->pev->friction = 0.8f;
2016-06-04 15:24:23 +02:00
pGrenade->pev->dmg = 100;
return pGrenade;
}
2016-07-31 15:48:50 +02:00
CGrenade *CGrenade::ShootSatchelCharge( entvars_t *pevOwner, Vector vecStart, Vector vecVelocity )
2016-06-04 15:24:23 +02:00
{
CGrenade *pGrenade = GetClassPtr( (CGrenade *)NULL );
pGrenade->pev->movetype = MOVETYPE_BOUNCE;
pGrenade->pev->classname = MAKE_STRING( "grenade" );
2016-07-31 15:48:50 +02:00
2016-06-04 15:24:23 +02:00
pGrenade->pev->solid = SOLID_BBOX;
2016-07-31 15:48:50 +02:00
SET_MODEL( ENT( pGrenade->pev ), "models/grenade.mdl" ); // Change this to satchel charge model
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
UTIL_SetSize( pGrenade->pev, Vector( 0, 0, 0 ), Vector( 0, 0, 0 ) );
2016-06-04 15:24:23 +02:00
pGrenade->pev->dmg = 200;
UTIL_SetOrigin( pGrenade->pev, vecStart );
pGrenade->pev->velocity = vecVelocity;
pGrenade->pev->angles = g_vecZero;
2016-07-31 15:48:50 +02:00
pGrenade->pev->owner = ENT( pevOwner );
2016-06-04 15:24:23 +02:00
// Detonate in "time" seconds
pGrenade->SetThink( &CBaseEntity::SUB_DoNothing );
pGrenade->SetUse( &CGrenade::DetonateUse );
pGrenade->SetTouch( &CGrenade::SlideTouch );
pGrenade->pev->spawnflags = SF_DETONATE;
2019-10-13 13:49:25 +02:00
pGrenade->pev->friction = 0.9f;
2016-06-04 15:24:23 +02:00
return pGrenade;
}
2016-07-31 15:48:50 +02:00
void CGrenade::UseSatchelCharges( entvars_t *pevOwner, SATCHELCODE code )
2016-06-04 15:24:23 +02:00
{
edict_t *pentFind;
edict_t *pentOwner;
2016-07-31 15:48:50 +02:00
if( !pevOwner )
2016-06-04 15:24:23 +02:00
return;
2016-07-31 15:48:50 +02:00
CBaseEntity *pOwner = CBaseEntity::Instance( pevOwner );
2016-06-04 15:24:23 +02:00
pentOwner = pOwner->edict();
pentFind = FIND_ENTITY_BY_CLASSNAME( NULL, "grenade" );
2016-07-31 15:48:50 +02:00
while( !FNullEnt( pentFind ) )
2016-06-04 15:24:23 +02:00
{
CBaseEntity *pEnt = Instance( pentFind );
2016-07-31 15:48:50 +02:00
if( pEnt )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
if( FBitSet( pEnt->pev->spawnflags, SF_DETONATE ) && pEnt->pev->owner == pentOwner )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
if( code == SATCHEL_DETONATE )
2016-06-04 15:24:23 +02:00
pEnt->Use( pOwner, pOwner, USE_ON, 0 );
2016-07-31 15:48:50 +02:00
else // SATCHEL_RELEASE
2016-06-04 15:24:23 +02:00
pEnt->pev->owner = NULL;
}
}
pentFind = FIND_ENTITY_BY_CLASSNAME( pentFind, "grenade" );
}
}
//======================end grenade