2
0
mirror of https://github.com/FWGS/hlsdk-xash3d synced 2024-11-24 10:50:48 +01:00
hlsdk-xash3d/dlls/explode.cpp

272 lines
6.5 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.
*
****/
/*
===== explode.cpp ========================================================
Explosion-related code
*/
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "decals.h"
#include "explode.h"
// Spark Shower
class CShower : public CBaseEntity
{
void Spawn( void );
void Think( void );
void Touch( CBaseEntity *pOther );
int ObjectCaps( void ) { return FCAP_DONT_SAVE; }
};
LINK_ENTITY_TO_CLASS( spark_shower, CShower )
2016-06-04 15:24:23 +02:00
void CShower::Spawn( void )
{
2019-10-13 13:49:25 +02:00
pev->velocity = RANDOM_FLOAT( 200.0f, 300.0f ) * pev->angles;
pev->velocity.x += RANDOM_FLOAT( -100.0f, 100.0f );
pev->velocity.y += RANDOM_FLOAT( -100.0f, 100.0f );
2016-07-31 15:48:50 +02:00
if( pev->velocity.z >= 0 )
2019-10-13 13:49:25 +02:00
pev->velocity.z += 200.0f;
2016-06-04 15:24:23 +02:00
else
2019-10-13 13:49:25 +02:00
pev->velocity.z -= 200.0f;
2016-06-04 15:24:23 +02:00
pev->movetype = MOVETYPE_BOUNCE;
pev->gravity = 0.5;
2019-10-13 13:49:25 +02:00
pev->nextthink = gpGlobals->time + 0.1f;
2016-06-04 15:24:23 +02:00
pev->solid = SOLID_NOT;
2016-07-31 15:48:50 +02:00
SET_MODEL( edict(), "models/grenade.mdl" ); // Need a model, just use the grenade, we don't draw it anyway
UTIL_SetSize( pev, g_vecZero, g_vecZero );
2016-06-04 15:24:23 +02:00
pev->effects |= EF_NODRAW;
2019-10-13 13:49:25 +02:00
pev->speed = RANDOM_FLOAT( 0.5f, 1.5f );
2016-06-04 15:24:23 +02:00
pev->angles = g_vecZero;
}
void CShower::Think( void )
{
UTIL_Sparks( pev->origin );
2019-10-13 13:49:25 +02:00
pev->speed -= 0.1f;
if( pev->speed > 0.0f )
pev->nextthink = gpGlobals->time + 0.1f;
2016-06-04 15:24:23 +02:00
else
UTIL_Remove( this );
pev->flags &= ~FL_ONGROUND;
}
void CShower::Touch( CBaseEntity *pOther )
{
2016-07-31 15:48:50 +02:00
if( pev->flags & FL_ONGROUND )
2019-10-13 13:49:25 +02:00
pev->velocity = pev->velocity * 0.1f;
2016-06-04 15:24:23 +02:00
else
2019-10-13 13:49:25 +02:00
pev->velocity = pev->velocity * 0.6f;
2016-06-04 15:24:23 +02:00
2019-10-13 13:49:25 +02:00
if( ( pev->velocity.x * pev->velocity.x + pev->velocity.y * pev->velocity.y ) < 10.0f )
pev->speed = 0.0f;
2016-06-04 15:24:23 +02:00
}
class CEnvExplosion : public CBaseMonster
{
public:
2016-07-31 15:48:50 +02:00
void Spawn();
void EXPORT Smoke( void );
2016-06-04 15:24:23 +02:00
void KeyValue( KeyValueData *pkvd );
void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
2016-07-31 15:48:50 +02:00
virtual int Save( CSave &save );
virtual int Restore( CRestore &restore );
static TYPEDESCRIPTION m_SaveData[];
2016-06-04 15:24:23 +02:00
int m_iMagnitude;// how large is the fireball? how much damage?
int m_spriteScale; // what's the exact fireball sprite scale?
};
2016-07-31 15:48:50 +02:00
TYPEDESCRIPTION CEnvExplosion::m_SaveData[] =
2016-06-04 15:24:23 +02:00
{
DEFINE_FIELD( CEnvExplosion, m_iMagnitude, FIELD_INTEGER ),
DEFINE_FIELD( CEnvExplosion, m_spriteScale, FIELD_INTEGER ),
};
IMPLEMENT_SAVERESTORE( CEnvExplosion, CBaseMonster )
LINK_ENTITY_TO_CLASS( env_explosion, CEnvExplosion )
2016-06-04 15:24:23 +02:00
void CEnvExplosion::KeyValue( KeyValueData *pkvd )
{
2016-07-31 15:48:50 +02:00
if( FStrEq( pkvd->szKeyName, "iMagnitude" ) )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
m_iMagnitude = atoi( pkvd->szValue );
2016-06-04 15:24:23 +02:00
pkvd->fHandled = TRUE;
}
else
CBaseEntity::KeyValue( pkvd );
}
void CEnvExplosion::Spawn( void )
{
pev->solid = SOLID_NOT;
pev->effects = EF_NODRAW;
pev->movetype = MOVETYPE_NONE;
/*
2016-07-31 15:48:50 +02:00
if( m_iMagnitude > 250 )
2016-06-04 15:24:23 +02:00
{
m_iMagnitude = 250;
}
*/
float flSpriteScale;
2019-10-13 13:49:25 +02:00
flSpriteScale = ( m_iMagnitude - 50 ) * 0.6f;
2016-07-31 15:48:50 +02:00
2016-06-04 15:24:23 +02:00
/*
2019-10-13 13:49:25 +02:00
if( flSpriteScale > 50.0f )
2016-06-04 15:24:23 +02:00
{
2019-10-13 13:49:25 +02:00
flSpriteScale = 50.0f;
2016-06-04 15:24:23 +02:00
}
*/
2019-10-13 13:49:25 +02:00
if( flSpriteScale < 10.0f )
2016-06-04 15:24:23 +02:00
{
2019-10-13 13:49:25 +02:00
flSpriteScale = 10.0f;
2016-06-04 15:24:23 +02:00
}
m_spriteScale = (int)flSpriteScale;
}
void CEnvExplosion::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
{
TraceResult tr;
pev->model = iStringNull;//invisible
pev->solid = SOLID_NOT;// intangible
2016-07-31 15:48:50 +02:00
Vector vecSpot;// trace starts here!
2019-10-13 13:49:25 +02:00
vecSpot = pev->origin + Vector( 0.0f, 0.0f, 8.0f );
2016-07-31 15:48:50 +02:00
2019-10-13 13:49:25 +02:00
UTIL_TraceLine( vecSpot, vecSpot + Vector( 0.0f, 0.0f, -40.0f ), ignore_monsters, ENT( pev ), &tr );
2016-06-04 15:24:23 +02:00
// Pull out of the wall a bit
2019-10-13 13:49:25 +02:00
if( tr.flFraction != 1.0f )
2016-06-04 15:24:23 +02:00
{
2019-10-13 13:49:25 +02:00
pev->origin = tr.vecEndPos + ( tr.vecPlaneNormal * ( m_iMagnitude - 24 ) * 0.6f );
2016-06-04 15:24:23 +02:00
}
else
{
pev->origin = pev->origin;
}
// draw decal
2016-07-31 15:48:50 +02:00
if( !( pev->spawnflags & SF_ENVEXPLOSION_NODECAL ) )
2016-06-04 15:24:23 +02:00
{
2019-10-13 13:49:25 +02:00
if( RANDOM_FLOAT( 0.0f, 1.0f ) < 0.5f )
2016-06-04 15:24:23 +02:00
{
UTIL_DecalTrace( &tr, DECAL_SCORCH1 );
}
else
{
UTIL_DecalTrace( &tr, DECAL_SCORCH2 );
}
}
// draw fireball
2016-07-31 15:48:50 +02:00
if( !( pev->spawnflags & SF_ENVEXPLOSION_NOFIREBALL ) )
2016-06-04 15:24:23 +02:00
{
MESSAGE_BEGIN( MSG_PAS, SVC_TEMPENTITY, pev->origin );
WRITE_BYTE( TE_EXPLOSION);
WRITE_COORD( pev->origin.x );
WRITE_COORD( pev->origin.y );
WRITE_COORD( pev->origin.z );
WRITE_SHORT( g_sModelIndexFireball );
WRITE_BYTE( (BYTE)m_spriteScale ); // 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();
}
else
{
MESSAGE_BEGIN( MSG_PAS, SVC_TEMPENTITY, pev->origin );
2016-07-31 15:48:50 +02:00
WRITE_BYTE( TE_EXPLOSION );
2016-06-04 15:24:23 +02:00
WRITE_COORD( pev->origin.x );
WRITE_COORD( pev->origin.y );
WRITE_COORD( pev->origin.z );
WRITE_SHORT( g_sModelIndexFireball );
WRITE_BYTE( 0 ); // no sprite
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();
}
// do damage
2016-07-31 15:48:50 +02:00
if( !( pev->spawnflags & SF_ENVEXPLOSION_NODAMAGE ) )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
RadiusDamage( pev, pev, m_iMagnitude, CLASS_NONE, DMG_BLAST );
2016-06-04 15:24:23 +02:00
}
SetThink( &CEnvExplosion::Smoke );
2019-10-13 13:49:25 +02:00
pev->nextthink = gpGlobals->time + 0.3f;
2016-06-04 15:24:23 +02:00
// draw sparks
2016-07-31 15:48:50 +02:00
if( !( pev->spawnflags & SF_ENVEXPLOSION_NOSPARKS ) )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
int sparkCount = RANDOM_LONG( 0, 3 );
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
for( int i = 0; i < sparkCount; i++ )
2016-06-04 15:24:23 +02:00
{
Create( "spark_shower", pev->origin, tr.vecPlaneNormal, NULL );
}
}
}
void CEnvExplosion::Smoke( void )
{
2016-07-31 15:48:50 +02:00
if( !( pev->spawnflags & SF_ENVEXPLOSION_NOSMOKE ) )
2016-06-04 15:24:23 +02:00
{
MESSAGE_BEGIN( MSG_PAS, 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( (BYTE)m_spriteScale ); // scale * 10
2016-07-31 15:48:50 +02:00
WRITE_BYTE( 12 ); // framerate
2016-06-04 15:24:23 +02:00
MESSAGE_END();
}
2016-07-31 15:48:50 +02:00
if( !( pev->spawnflags & SF_ENVEXPLOSION_REPEATABLE ) )
2016-06-04 15:24:23 +02:00
{
UTIL_Remove( this );
}
}
// HACKHACK -- create one of these and fake a keyvalue to get the right explosion setup
void ExplosionCreate( const Vector &center, const Vector &angles, edict_t *pOwner, int magnitude, BOOL doDamage )
{
2016-07-31 15:48:50 +02:00
KeyValueData kvd;
char buf[128];
2016-06-04 15:24:23 +02:00
CBaseEntity *pExplosion = CBaseEntity::Create( "env_explosion", center, angles, pOwner );
sprintf( buf, "%3d", magnitude );
kvd.szKeyName = "iMagnitude";
kvd.szValue = buf;
pExplosion->KeyValue( &kvd );
2016-07-31 15:48:50 +02:00
if( !doDamage )
2016-06-04 15:24:23 +02:00
pExplosion->pev->spawnflags |= SF_ENVEXPLOSION_NODAMAGE;
pExplosion->Spawn();
pExplosion->Use( NULL, NULL, USE_TOGGLE, 0 );
}