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.

488 lines
13 KiB

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