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.

479 lines
12 KiB

7 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.
*
* 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"
//===================grenade
LINK_ENTITY_TO_CLASS( grenade, CGrenade )
7 years ago
// 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;
8 years ago
UTIL_TraceLine( pev->origin, pev->origin + Vector( 0, 0, -32 ), ignore_monsters, ENT( pev ), & tr );
7 years ago
Explode( &tr, DMG_BLAST );
}
// UNDONE: temporary scorching for PreAlpha - find a less sleazy permenant solution.
void CGrenade::Explode( TraceResult *pTrace, int bitsDamageType )
{
// float flRndSound;// sound randomizer
7 years ago
pev->model = iStringNull;//invisible
pev->solid = SOLID_NOT;// intangible
pev->takedamage = DAMAGE_NO;
// Pull out of the wall a bit
8 years ago
if( pTrace->flFraction != 1.0 )
7 years ago
{
8 years ago
pev->origin = pTrace->vecEndPos + ( pTrace->vecPlaneNormal * ( pev->dmg - 24 ) * 0.6 );
7 years ago
}
8 years ago
int iContents = UTIL_PointContents( pev->origin );
7 years ago
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 );
8 years ago
if( iContents != CONTENTS_WATER )
7 years ago
{
WRITE_SHORT( g_sModelIndexFireball );
}
else
{
WRITE_SHORT( g_sModelIndexWExplosion );
}
8 years ago
WRITE_BYTE( ( pev->dmg - 50 ) * .60 ); // scale * 10
WRITE_BYTE( 15 ); // framerate
7 years ago
WRITE_BYTE( TE_EXPLFLAG_NONE );
MESSAGE_END();
8 years ago
CSoundEnt::InsertSound( bits_SOUND_COMBAT, pev->origin, NORMAL_EXPLOSION_VOLUME, 3.0 );
7 years ago
entvars_t *pevOwner;
8 years ago
if( pev->owner )
7 years ago
pevOwner = VARS( pev->owner );
else
pevOwner = NULL;
pev->owner = NULL; // can't traceline attack owner if this is set
8 years ago
RadiusDamage( pev, pevOwner, pev->dmg, CLASS_NONE, bitsDamageType );
7 years ago
8 years ago
if( RANDOM_FLOAT( 0, 1 ) < 0.5 )
7 years ago
{
UTIL_DecalTrace( pTrace, DECAL_SCORCH1 );
}
else
{
UTIL_DecalTrace( pTrace, DECAL_SCORCH2 );
}
//flRndSound = RANDOM_FLOAT( 0, 1 );
7 years ago
8 years ago
switch( RANDOM_LONG( 0, 2 ) )
7 years ago
{
8 years ago
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;
7 years ago
}
pev->effects |= EF_NODRAW;
9 years ago
SetThink( &CGrenade::Smoke );
7 years ago
pev->velocity = g_vecZero;
SetNextThink( 0.3 );
8 years ago
if( iContents != CONTENTS_WATER )
7 years ago
{
8 years ago
int sparkCount = RANDOM_LONG( 0, 3 );
for( int i = 0; i < sparkCount; i++ )
7 years ago
Create( "spark_shower", pev->origin, pTrace->vecPlaneNormal, NULL );
}
}
void CGrenade::Smoke( void )
{
8 years ago
if( UTIL_PointContents( pev->origin ) == CONTENTS_WATER )
7 years ago
{
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 );
WRITE_BYTE( (int)( ( pev->dmg - 50 ) * 0.80 ) ); // scale * 10
WRITE_BYTE( 12 ); // framerate
7 years ago
MESSAGE_END();
}
UTIL_Remove( this );
}
void CGrenade::Killed( entvars_t *pevAttacker, int iGib )
{
8 years ago
Detonate();
7 years ago
}
// Timed grenade, this think is called when time runs out.
void CGrenade::DetonateUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
{
9 years ago
SetThink( &CGrenade::Detonate );
7 years ago
SetNextThink( 0 );
}
void CGrenade::PreDetonate( void )
{
CSoundEnt::InsertSound( bits_SOUND_DANGER, pev->origin, 400, 0.3 );
7 years ago
9 years ago
SetThink( &CGrenade::Detonate );
7 years ago
SetNextThink( 1 );
}
void CGrenade::Detonate( void )
{
TraceResult tr;
8 years ago
Vector vecSpot;// trace starts here!
7 years ago
8 years ago
vecSpot = pev->origin + Vector( 0, 0, 8 );
UTIL_TraceLine( vecSpot, vecSpot + Vector( 0, 0, -40 ), ignore_monsters, ENT(pev), &tr );
7 years ago
Explode( &tr, DMG_BLAST );
}
//
// Contact grenade, explode when it touches something
//
void CGrenade::ExplodeTouch( CBaseEntity *pOther )
{
TraceResult tr;
8 years ago
Vector vecSpot;// trace starts here!
7 years ago
pev->enemy = pOther->edict();
vecSpot = pev->origin - pev->velocity.Normalize() * 32;
8 years ago
UTIL_TraceLine( vecSpot, vecSpot + pev->velocity.Normalize() * 64, ignore_monsters, ENT( pev ), &tr );
7 years ago
Explode( &tr, DMG_BLAST );
}
void CGrenade::DangerSoundThink( void )
{
8 years ago
if( !IsInWorld() )
7 years ago
{
UTIL_Remove( this );
return;
}
CSoundEnt::InsertSound ( bits_SOUND_DANGER, pev->origin + pev->velocity * 0.5, pev->velocity.Length( ), 0.2 );
SetNextThink( 0.2 );
if (pev->waterlevel != 0 && pev->watertype > CONTENT_FLYFIELD)
{
pev->velocity = pev->velocity * 0.5;
}
}
void CGrenade::BounceTouch( CBaseEntity *pOther )
{
// don't hit the guy that launched this grenade
8 years ago
if( pOther->edict() == pev->owner )
7 years ago
return;
// only do damage if we're moving fairly fast
8 years ago
if( m_flNextAttack < gpGlobals->time && pev->velocity.Length() > 100 )
7 years ago
{
entvars_t *pevOwner = VARS( pev->owner );
8 years ago
if( pevOwner )
7 years ago
{
8 years ago
TraceResult tr = UTIL_GetGlobalTrace();
ClearMultiDamage();
pOther->TraceAttack( pevOwner, 1, gpGlobals->v_forward, &tr, DMG_CLUB );
ApplyMultiDamage( pev, pevOwner );
7 years ago
}
m_flNextAttack = gpGlobals->time + 1.0; // debounce
}
Vector vecTestVelocity;
8 years ago
// pev->avelocity = Vector( 300, 300, 300 );
7 years ago
// 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;
vecTestVelocity.z *= 0.45;
8 years ago
if( !m_fRegisteredSound && vecTestVelocity.Length() <= 60 )
7 years ago
{
//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.
8 years ago
7 years ago
// register a radius louder than the explosion, so we make sure everyone gets out of the way
CSoundEnt::InsertSound( bits_SOUND_DANGER, pev->origin, (int)( pev->dmg / 0.4 ), 0.3 );
7 years ago
m_fRegisteredSound = TRUE;
}
8 years ago
if( pev->flags & FL_ONGROUND )
7 years ago
{
// add a bit of static friction
pev->velocity = pev->velocity * 0.8;
pev->sequence = RANDOM_LONG( 1, 1 );
}
else
{
// play bounce sound
BounceSound();
}
pev->framerate = pev->velocity.Length() / 200.0;
8 years ago
if( pev->framerate > 1.0 )
7 years ago
pev->framerate = 1;
8 years ago
else if( pev->framerate < 0.5 )
7 years ago
pev->framerate = 0;
}
void CGrenade::SlideTouch( CBaseEntity *pOther )
{
// don't hit the guy that launched this grenade
8 years ago
if( pOther->edict() == pev->owner )
7 years ago
return;
8 years ago
// pev->avelocity = Vector( 300, 300, 300 );
if( pev->flags & FL_ONGROUND )
7 years ago
{
// add a bit of static friction
pev->velocity = pev->velocity * 0.95;
8 years ago
if( pev->velocity.x != 0 || pev->velocity.y != 0 )
7 years ago
{
// maintain sliding sound
}
}
else
{
BounceSound();
}
}
8 years ago
void CGrenade::BounceSound( void )
7 years ago
{
8 years ago
switch( RANDOM_LONG( 0, 2 ) )
7 years ago
{
8 years ago
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;
7 years ago
}
}
8 years ago
void CGrenade::TumbleThink( void )
7 years ago
{
8 years ago
if( !IsInWorld() )
7 years ago
{
UTIL_Remove( this );
return;
}
8 years ago
StudioFrameAdvance();
7 years ago
SetNextThink( 0.1 );
8 years ago
if( pev->dmgtime - 1 < gpGlobals->time )
7 years ago
{
8 years ago
CSoundEnt::InsertSound( bits_SOUND_DANGER, pev->origin + pev->velocity * ( pev->dmgtime - gpGlobals->time ), 400, 0.1 );
7 years ago
}
8 years ago
if( pev->dmgtime <= gpGlobals->time )
7 years ago
{
9 years ago
SetThink( &CGrenade::Detonate );
7 years ago
}
if (pev->waterlevel != 0 && pev->watertype > CONTENT_FLYFIELD)
{
pev->velocity = pev->velocity * 0.5;
pev->framerate = 0.2;
}
}
8 years ago
void CGrenade::Spawn( void )
7 years ago
{
pev->movetype = MOVETYPE_BOUNCE;
pev->classname = MAKE_STRING( "grenade" );
8 years ago
7 years ago
pev->solid = SOLID_BBOX;
8 years ago
SET_MODEL( ENT( pev ), "models/grenade.mdl" );
UTIL_SetSize( pev, Vector( 0, 0, 0 ), Vector( 0, 0, 0 ) );
7 years ago
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, vecStart );
pGrenade->pev->velocity = vecVelocity;
8 years ago
pGrenade->pev->angles = UTIL_VecToAngles( pGrenade->pev->velocity );
pGrenade->pev->owner = ENT( pevOwner );
7 years ago
// make monsters afaid of it while in the air
9 years ago
pGrenade->SetThink( &CGrenade::DangerSoundThink );
7 years ago
pGrenade->SetNextThink( 0 );
8 years ago
7 years ago
// Tumble in air
8 years ago
pGrenade->pev->avelocity.x = RANDOM_FLOAT( -100, -500 );
7 years ago
// Explode on contact
9 years ago
pGrenade->SetTouch( &CGrenade::ExplodeTouch );
7 years ago
pGrenade->pev->dmg = 100;
7 years ago
return pGrenade;
}
8 years ago
CGrenade *CGrenade::ShootTimed( entvars_t *pevOwner, Vector vecStart, Vector vecVelocity, float time )
7 years ago
{
CGrenade *pGrenade = GetClassPtr( (CGrenade *)NULL );
pGrenade->Spawn();
UTIL_SetOrigin( pGrenade, vecStart );
pGrenade->pev->velocity = vecVelocity;
8 years ago
pGrenade->pev->angles = UTIL_VecToAngles( pGrenade->pev->velocity );
pGrenade->pev->owner = ENT( pevOwner );
9 years ago
pGrenade->SetTouch( &CGrenade::BounceTouch ); // Bounce if touched
8 years ago
7 years ago
// 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;
9 years ago
pGrenade->SetThink( &CGrenade::TumbleThink );
7 years ago
pGrenade->SetNextThink( 0.1 );
8 years ago
if( time < 0.1 )
7 years ago
{
pGrenade->SetNextThink( 0 );
pGrenade->pev->velocity = Vector( 0, 0, 0 );
}
8 years ago
7 years ago
pGrenade->pev->sequence = RANDOM_LONG( 3, 6 );
pGrenade->pev->framerate = 1.0;
// Tumble through the air
// pGrenade->pev->avelocity.x = -400;
pGrenade->pev->gravity = 0.5;
pGrenade->pev->friction = 0.8;
8 years ago
SET_MODEL( ENT( pGrenade->pev ), "models/w_grenade.mdl" );
7 years ago
pGrenade->pev->dmg = 100;
return pGrenade;
}
8 years ago
CGrenade *CGrenade::ShootSatchelCharge( entvars_t *pevOwner, Vector vecStart, Vector vecVelocity )
7 years ago
{
CGrenade *pGrenade = GetClassPtr( (CGrenade *)NULL );
pGrenade->pev->movetype = MOVETYPE_BOUNCE;
pGrenade->pev->classname = MAKE_STRING( "grenade" );
8 years ago
7 years ago
pGrenade->pev->solid = SOLID_BBOX;
8 years ago
SET_MODEL( ENT( pGrenade->pev ), "models/grenade.mdl" ); // Change this to satchel charge model
7 years ago
8 years ago
UTIL_SetSize( pGrenade->pev, Vector( 0, 0, 0 ), Vector( 0, 0, 0 ) );
7 years ago
pGrenade->pev->dmg = 200;
UTIL_SetOrigin( pGrenade, vecStart );
pGrenade->pev->velocity = vecVelocity;
pGrenade->pev->angles = g_vecZero;
8 years ago
pGrenade->pev->owner = ENT( pevOwner );
7 years ago
// Detonate in "time" seconds
pGrenade->SetThink(&CGrenade:: SUB_DoNothing );
9 years ago
pGrenade->SetUse( &CGrenade::DetonateUse );
pGrenade->SetTouch( &CGrenade::SlideTouch );
7 years ago
pGrenade->pev->spawnflags = SF_DETONATE;
pGrenade->pev->friction = 0.9;
return pGrenade;
}
8 years ago
void CGrenade::UseSatchelCharges( entvars_t *pevOwner, SATCHELCODE code )
7 years ago
{
edict_t *pentOwner;
8 years ago
if( !pevOwner )
7 years ago
return;
8 years ago
CBaseEntity *pOwner = CBaseEntity::Instance( pevOwner );
7 years ago
pentOwner = pOwner->edict();
CBaseEntity *pEnt = UTIL_FindEntityByClassname( NULL, "grenade" );
while ( pEnt )
{
8 years ago
if( FBitSet( pEnt->pev->spawnflags, SF_DETONATE ) && pEnt->pev->owner == pentOwner )
7 years ago
{
8 years ago
if( code == SATCHEL_DETONATE )
7 years ago
pEnt->Use( pOwner, pOwner, USE_ON, 0 );
8 years ago
else // SATCHEL_RELEASE
7 years ago
pEnt->pev->owner = NULL;
}
pEnt = UTIL_FindEntityByClassname( pEnt, "grenade" );
}
}
//======================end grenade