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.

1185 lines
29 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.
*
****/
/*
===== bmodels.cpp ========================================================
spawn, think, and use functions for entities that use brush models
*/
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "saverestore.h"
#include "func_break.h"
#include "decals.h"
#include "explode.h"
8 years ago
extern DLL_GLOBAL Vector g_vecAttackDir;
7 years ago
// =================== FUNC_Breakable ==============================================
// Just add more items to the bottom of this array and they will automagically be supported
// This is done instead of just a classname in the FGD so we can control which entities can
// be spawned, and still remain fairly flexible
const char *CBreakable::pSpawnObjects[] =
{
8 years ago
NULL, // 0
7 years ago
"item_battery", // 1
"item_healthkit", // 2
8 years ago
"weapon_9mmhandgun", // 3
7 years ago
"ammo_9mmclip", // 4
"weapon_9mmAR", // 5
"ammo_9mmAR", // 6
"ammo_ARgrenades", // 7
"weapon_shotgun", // 8
"ammo_buckshot", // 9
"weapon_crossbow", // 10
"ammo_crossbow", // 11
"weapon_357", // 12
8 years ago
"ammo_357", // 13
7 years ago
"weapon_rpg", // 14
"ammo_rpgclip", // 15
"ammo_gaussclip", // 16
8 years ago
"weapon_handgrenade", // 17
7 years ago
"weapon_tripmine", // 18
"weapon_satchel", // 19
"weapon_snark", // 20
"weapon_hornetgun", // 21
};
void CBreakable::KeyValue( KeyValueData* pkvd )
{
// UNDONE_WC: explicitly ignoring these fields, but they shouldn't be in the map file!
8 years ago
if( FStrEq( pkvd->szKeyName, "explosion" ) )
7 years ago
{
8 years ago
if( !stricmp( pkvd->szValue, "directed" ) )
7 years ago
m_Explosion = expDirected;
8 years ago
else if( !stricmp( pkvd->szValue, "random" ) )
7 years ago
m_Explosion = expRandom;
else
m_Explosion = expRandom;
pkvd->fHandled = TRUE;
}
8 years ago
else if( FStrEq( pkvd->szKeyName, "material" ) )
7 years ago
{
8 years ago
int i = atoi( pkvd->szValue );
7 years ago
// 0:glass, 1:metal, 2:flesh, 3:wood
8 years ago
if( ( i < 0 ) || ( i >= matLastMaterial ) )
7 years ago
m_Material = matWood;
else
m_Material = (Materials)i;
pkvd->fHandled = TRUE;
}
8 years ago
else if( FStrEq( pkvd->szKeyName, "deadmodel" ) )
7 years ago
{
pkvd->fHandled = TRUE;
}
8 years ago
else if( FStrEq( pkvd->szKeyName, "shards" ) )
7 years ago
{
8 years ago
//m_iShards = atof( pkvd->szValue );
7 years ago
pkvd->fHandled = TRUE;
}
8 years ago
else if( FStrEq( pkvd->szKeyName, "gibmodel" ) )
7 years ago
{
8 years ago
m_iszGibModel = ALLOC_STRING( pkvd->szValue );
7 years ago
pkvd->fHandled = TRUE;
}
8 years ago
else if( FStrEq( pkvd->szKeyName, "spawnobject" ) )
7 years ago
{
int object = atoi( pkvd->szValue );
if( object > 0 && object < (int)ARRAYSIZE( pSpawnObjects ) )
7 years ago
m_iszSpawnObject = MAKE_STRING( pSpawnObjects[object] );
pkvd->fHandled = TRUE;
}
8 years ago
else if( FStrEq( pkvd->szKeyName, "explodemagnitude" ) )
7 years ago
{
ExplosionSetMagnitude( atoi( pkvd->szValue ) );
pkvd->fHandled = TRUE;
}
8 years ago
else if( FStrEq( pkvd->szKeyName, "lip" ) )
7 years ago
pkvd->fHandled = TRUE;
else if (FStrEq(pkvd->szKeyName, "respawn") ) //LRC
{
m_iRespawnTime = atoi( pkvd->szValue );
pkvd->fHandled = TRUE;
}
else if (FStrEq(pkvd->szKeyName, "whenhit") ) //LRC
{
m_iszWhenHit = ALLOC_STRING(pkvd->szValue);
pkvd->fHandled = TRUE;
}
else if (FStrEq(pkvd->szKeyName, "m_iClass") )
{
m_iClass = atoi( pkvd->szValue );
pkvd->fHandled = TRUE;
}
else
CBaseDelay::KeyValue( pkvd );
}
//
// func_breakable - bmodel that breaks into pieces after taking damage
//
LINK_ENTITY_TO_CLASS( func_breakable, CBreakable )
7 years ago
TYPEDESCRIPTION CBreakable::m_SaveData[] =
{
DEFINE_FIELD( CBreakable, m_Material, FIELD_INTEGER ),
DEFINE_FIELD( CBreakable, m_Explosion, FIELD_INTEGER ),
// Don't need to save/restore these because we precache after restore
// DEFINE_FIELD( CBreakable, m_idShard, FIELD_INTEGER ),
DEFINE_FIELD( CBreakable, m_angle, FIELD_FLOAT ),
DEFINE_FIELD( CBreakable, m_iszGibModel, FIELD_STRING ),
DEFINE_FIELD( CBreakable, m_iszSpawnObject, FIELD_STRING ),
// Explosion magnitude is stored in pev->impulse
//LRC- time until respawn
DEFINE_FIELD( CBreakable, m_iRespawnTime, FIELD_INTEGER ),
//LRC- health to set on respawn
DEFINE_FIELD( CBreakable, m_iInitialHealth, FIELD_INTEGER ),
DEFINE_FIELD( CBreakable, m_iInitialRenderAmt, FIELD_INTEGER ),
DEFINE_FIELD( CBreakable, m_iInitialRenderMode, FIELD_INTEGER ),
DEFINE_FIELD( CBreakable, m_iszWhenHit, FIELD_STRING ),
DEFINE_FIELD( CBreakable, m_pHitProxy, FIELD_CLASSPTR ),
};
IMPLEMENT_SAVERESTORE( CBreakable, CBaseEntity )
7 years ago
void CBreakable::Spawn( void )
{
8 years ago
Precache();
7 years ago
8 years ago
if( FBitSet( pev->spawnflags, SF_BREAK_TRIGGER_ONLY ) )
7 years ago
pev->takedamage = DAMAGE_NO;
else
pev->takedamage = DAMAGE_YES;
if (m_iClass) //LRC - might these additions cause problems?
{
pev->flags |= FL_MONSTER;
pev->view_ofs = (pev->maxs + pev->mins) / 2;
}
if (m_iszWhenHit) //LRC - locus trigger
{
m_pHitProxy = GetClassPtr( (CPointEntity*)NULL );
}
8 years ago
pev->solid = SOLID_BSP;
pev->movetype = MOVETYPE_PUSH;
m_angle = pev->angles.y;
pev->angles.y = 0;
7 years ago
m_iInitialHealth = pev->health;
m_iInitialRenderAmt = pev->renderamt;
m_iInitialRenderMode = pev->rendermode;
// HACK: matGlass can receive decals, we need the client to know about this
// so use class to store the material flag
8 years ago
if( m_Material == matGlass )
7 years ago
{
pev->playerclass = 1;
}
8 years ago
SET_MODEL( ENT( pev ), STRING( pev->model ) );//set size and link into world.
7 years ago
9 years ago
SetTouch( &CBreakable::BreakTouch );
7 years ago
SetUse(&CBreakable:: BreakUse );
8 years ago
if( FBitSet( pev->spawnflags, SF_BREAK_TRIGGER_ONLY ) ) // Only break on trigger
7 years ago
SetTouch( NULL );
// Flag unbreakable glass as "worldbrush" so it will block ALL tracelines
8 years ago
if( !IsBreakable() && pev->rendermode != kRenderNormal )
7 years ago
pev->flags |= FL_WORLDBRUSH;
if (m_iStyle >= 32)
LIGHT_STYLE(m_iStyle, "z");
else if (m_iStyle <= -32)
LIGHT_STYLE(-m_iStyle, "a");
}
STATE CBreakable::GetState( void )
{
if (m_iRespawnTime)
{
if (pev->effects & EF_NODRAW)
return STATE_OFF;
else
return STATE_ON;
}
else return STATE_OFF;
}
8 years ago
const char *CBreakable::pSoundsWood[] =
7 years ago
{
"debris/wood1.wav",
"debris/wood2.wav",
"debris/wood3.wav",
};
8 years ago
const char *CBreakable::pSoundsFlesh[] =
7 years ago
{
"debris/flesh1.wav",
"debris/flesh2.wav",
"debris/flesh3.wav",
"debris/flesh5.wav",
"debris/flesh6.wav",
"debris/flesh7.wav",
};
8 years ago
const char *CBreakable::pSoundsMetal[] =
7 years ago
{
"debris/metal1.wav",
"debris/metal2.wav",
"debris/metal3.wav",
};
8 years ago
const char *CBreakable::pSoundsConcrete[] =
7 years ago
{
"debris/concrete1.wav",
"debris/concrete2.wav",
"debris/concrete3.wav",
};
8 years ago
const char *CBreakable::pSoundsGlass[] =
7 years ago
{
"debris/glass1.wav",
"debris/glass2.wav",
"debris/glass3.wav",
};
const char **CBreakable::MaterialSoundList( Materials precacheMaterial, int &soundCount )
{
const char **pSoundList = NULL;
8 years ago
switch( precacheMaterial )
7 years ago
{
case matWood:
pSoundList = pSoundsWood;
8 years ago
soundCount = ARRAYSIZE( pSoundsWood );
7 years ago
break;
case matFlesh:
pSoundList = pSoundsFlesh;
8 years ago
soundCount = ARRAYSIZE( pSoundsFlesh );
7 years ago
break;
case matComputer:
case matUnbreakableGlass:
case matGlass:
pSoundList = pSoundsGlass;
8 years ago
soundCount = ARRAYSIZE( pSoundsGlass );
7 years ago
break;
case matMetal:
pSoundList = pSoundsMetal;
8 years ago
soundCount = ARRAYSIZE( pSoundsMetal );
7 years ago
break;
case matCinderBlock:
case matRocks:
pSoundList = pSoundsConcrete;
8 years ago
soundCount = ARRAYSIZE( pSoundsConcrete );
7 years ago
break;
case matCeilingTile:
case matNone:
default:
soundCount = 0;
break;
}
return pSoundList;
}
void CBreakable::MaterialSoundPrecache( Materials precacheMaterial )
{
8 years ago
const char **pSoundList;
int i, soundCount = 0;
7 years ago
pSoundList = MaterialSoundList( precacheMaterial, soundCount );
8 years ago
for( i = 0; i < soundCount; i++ )
7 years ago
{
PRECACHE_SOUND( pSoundList[i] );
7 years ago
}
}
void CBreakable::MaterialSoundRandom( edict_t *pEdict, Materials soundMaterial, float volume )
{
8 years ago
const char **pSoundList;
int soundCount = 0;
7 years ago
pSoundList = MaterialSoundList( soundMaterial, soundCount );
8 years ago
if( soundCount )
EMIT_SOUND( pEdict, CHAN_BODY, pSoundList[RANDOM_LONG( 0, soundCount - 1 )], volume, 1.0 );
7 years ago
}
void CBreakable::Precache( void )
{
const char *pGibName = NULL;
8 years ago
switch( m_Material )
7 years ago
{
case matWood:
pGibName = "models/woodgibs.mdl";
8 years ago
PRECACHE_SOUND( "debris/bustcrate1.wav" );
PRECACHE_SOUND( "debris/bustcrate2.wav" );
7 years ago
break;
case matFlesh:
pGibName = "models/fleshgibs.mdl";
8 years ago
PRECACHE_SOUND( "debris/bustflesh1.wav" );
PRECACHE_SOUND( "debris/bustflesh2.wav" );
7 years ago
break;
case matComputer:
8 years ago
PRECACHE_SOUND( "buttons/spark5.wav" );
PRECACHE_SOUND( "buttons/spark6.wav" );
7 years ago
pGibName = "models/computergibs.mdl";
8 years ago
PRECACHE_SOUND( "debris/bustmetal1.wav" );
PRECACHE_SOUND( "debris/bustmetal2.wav" );
break;
7 years ago
case matUnbreakableGlass:
case matGlass:
pGibName = "models/glassgibs.mdl";
8 years ago
PRECACHE_SOUND( "debris/bustglass1.wav" );
PRECACHE_SOUND( "debris/bustglass2.wav" );
7 years ago
break;
case matMetal:
pGibName = "models/metalplategibs.mdl";
8 years ago
PRECACHE_SOUND( "debris/bustmetal1.wav" );
PRECACHE_SOUND( "debris/bustmetal2.wav" );
7 years ago
break;
case matCinderBlock:
pGibName = "models/cindergibs.mdl";
8 years ago
PRECACHE_SOUND( "debris/bustconcrete1.wav" );
PRECACHE_SOUND( "debris/bustconcrete2.wav" );
7 years ago
break;
case matRocks:
pGibName = "models/rockgibs.mdl";
8 years ago
PRECACHE_SOUND( "debris/bustconcrete1.wav" );
PRECACHE_SOUND( "debris/bustconcrete2.wav" );
7 years ago
break;
case matCeilingTile:
pGibName = "models/ceilinggibs.mdl";
8 years ago
PRECACHE_SOUND( "debris/bustceiling.wav" );
7 years ago
break;
case matNone:
case matLastMaterial:
break;
default:
break;
}
MaterialSoundPrecache( m_Material );
8 years ago
if( m_iszGibModel )
pGibName = STRING( m_iszGibModel );
7 years ago
m_idShard = PRECACHE_MODEL( pGibName );
7 years ago
// Precache the spawn item's data
8 years ago
if( m_iszSpawnObject )
UTIL_PrecacheOther( STRING( m_iszSpawnObject ) );
7 years ago
}
// play shard sound when func_breakable takes damage.
// the more damage, the louder the shard sound.
void CBreakable::DamageSound( void )
{
int pitch;
float fvol;
const char *rgpsz[6];
7 years ago
int i = 0;
int material = m_Material;
8 years ago
//if( RANDOM_LONG( 0, 1 ) )
// return;
7 years ago
8 years ago
if( RANDOM_LONG( 0, 2 ) )
7 years ago
pitch = PITCH_NORM;
else
8 years ago
pitch = 95 + RANDOM_LONG( 0, 34 );
7 years ago
8 years ago
fvol = RANDOM_FLOAT( 0.75, 1.0 );
7 years ago
8 years ago
if( material == matComputer && RANDOM_LONG( 0, 1 ) )
7 years ago
material = matMetal;
8 years ago
switch( material )
7 years ago
{
case matComputer:
case matGlass:
case matUnbreakableGlass:
rgpsz[0] = "debris/glass1.wav";
rgpsz[1] = "debris/glass2.wav";
rgpsz[2] = "debris/glass3.wav";
i = 3;
break;
case matWood:
rgpsz[0] = "debris/wood1.wav";
rgpsz[1] = "debris/wood2.wav";
rgpsz[2] = "debris/wood3.wav";
i = 3;
break;
case matMetal:
rgpsz[0] = "debris/metal1.wav";
rgpsz[1] = "debris/metal3.wav";
rgpsz[2] = "debris/metal2.wav";
i = 2;
break;
case matFlesh:
rgpsz[0] = "debris/flesh1.wav";
rgpsz[1] = "debris/flesh2.wav";
rgpsz[2] = "debris/flesh3.wav";
rgpsz[3] = "debris/flesh5.wav";
rgpsz[4] = "debris/flesh6.wav";
rgpsz[5] = "debris/flesh7.wav";
i = 6;
break;
case matRocks:
case matCinderBlock:
rgpsz[0] = "debris/concrete1.wav";
rgpsz[1] = "debris/concrete2.wav";
rgpsz[2] = "debris/concrete3.wav";
i = 3;
break;
case matCeilingTile:
// UNDONE: no ceiling tile shard sound yet
i = 0;
break;
}
8 years ago
if( i )
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, rgpsz[ RANDOM_LONG( 0, i - 1 )], fvol, ATTN_NORM, 0, pitch );
7 years ago
}
void CBreakable::BreakTouch( CBaseEntity *pOther )
{
float flDamage;
8 years ago
entvars_t* pevToucher = pOther->pev;
7 years ago
// only players can break these right now
8 years ago
if( !pOther->IsPlayer() || !IsBreakable() )
7 years ago
{
8 years ago
return;
7 years ago
}
8 years ago
if( FBitSet( pev->spawnflags, SF_BREAK_TOUCH ) )
{
// can be broken when run into
7 years ago
flDamage = pevToucher->velocity.Length() * 0.01;
8 years ago
if( flDamage >= pev->health )
7 years ago
{
SetTouch( NULL );
8 years ago
TakeDamage( pevToucher, pevToucher, flDamage, DMG_CRUSH );
7 years ago
// do a little damage to player if we broke glass or computer
pOther->TakeDamage( pev, pev, flDamage/4, DMG_SLASH );
}
}
8 years ago
if( FBitSet( pev->spawnflags, SF_BREAK_PRESSURE ) && pevToucher->absmin.z >= pev->maxs.z - 2 )
{
// can be broken when stood upon
7 years ago
// play creaking sound here.
DamageSound();
9 years ago
SetThink( &CBreakable::Die );
7 years ago
SetTouch( NULL );
8 years ago
if( m_flDelay == 0 )
{
// !!!BUGBUG - why doesn't zero delay work?
7 years ago
m_flDelay = 0.1;
}
SetNextThink( m_flDelay );
}
}
//
// Smash the our breakable object
//
// Break when triggered
void CBreakable::BreakUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
{
// for a respawnable entity, ON means someone wants it to respawn- but this one's solid already.
if (m_iRespawnTime && useType == USE_ON)
return;
8 years ago
if( IsBreakable() )
7 years ago
{
pev->angles.y = m_angle;
8 years ago
UTIL_MakeVectors( pev->angles );
7 years ago
g_vecAttackDir = gpGlobals->v_forward;
Die();
}
}
//LRC
void CBreakable::RespawnUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
{
// OFF means someone wants it to break, but this one's broken already.
if (useType == USE_OFF)
{
if (m_iRespawnTime > 0)
{
// reset my respawn time
SetThink(&CBreakable:: RespawnThink );
SetNextThink( m_iRespawnTime );
}
return;
}
// ALERT(at_debug,"Respawn trigger received\n");
SetThink(&CBreakable:: RespawnThink );
SetNextThink( 0.1 );
}
//LRC
void CBreakable::RespawnThink( void )
{
// ALERT(at_debug,"RespawnThink: ");
CBaseEntity *pList[2];
int count = UTIL_EntitiesInBox( pList, 2, pev->mins, pev->maxs, FL_MONSTER | FL_CLIENT );
if ( count )
{
// Can't respawn right now, a monster or player is in the way. Wait a bit.
// ALERT(at_debug,"Respawn failed, count is %d\n",count);
SetThink(&CBreakable:: RespawnThink );
SetNextThink( 2 ); // CONSIDER: change this number?
}
else
{
// fade in, don't just appear(?)
if (pev->spawnflags & SF_BREAK_FADE_RESPAWN)
{
SetThink(&CBreakable:: RespawnFadeThink );
SetNextThink( 0.1 );
pev->renderamt = 0;
if (m_iInitialRenderMode == kRenderNormal)
{
pev->rendermode = kRenderTransTexture;
m_iInitialRenderAmt = 255;
}
}
// ALERT(at_debug,"Respawn OK\n");
pev->solid = SOLID_BSP;
pev->effects &= ~EF_NODRAW;
pev->health = m_iInitialHealth;
SetUse(&CBreakable:: BreakUse );
if ( !FBitSet( pev->spawnflags, SF_BREAK_TRIGGER_ONLY ) )
pev->takedamage = DAMAGE_YES;
// trigger the "fire on respawn" target
FireTargets( STRING(pev->netname), this, this, USE_TOGGLE, 0);
}
}
void CBreakable::RespawnFadeThink ( void )
{
int newamt = Q_min( pev->renderamt + 50, m_iInitialRenderAmt);
7 years ago
// ALERT(at_debug, "FadeThink: %d changed to %d\n",pev->renderamt,newamt);
pev->renderamt = newamt;
if (pev->renderamt < m_iInitialRenderAmt)
{
SetNextThink( 0.1 );
}
else
{
pev->rendermode = m_iInitialRenderMode;
}
}
void CBreakable::TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType )
{
// random spark if this is a 'computer' object
8 years ago
if( RANDOM_LONG( 0, 1 ) )
7 years ago
{
switch( m_Material )
{
case matComputer:
9 years ago
{
7 years ago
UTIL_Sparks( ptr->vecEndPos );
8 years ago
float flVolume = RANDOM_FLOAT( 0.7 , 1.0 );//random volume range
switch( RANDOM_LONG( 0, 1 ) )
7 years ago
{
8 years ago
case 0:
EMIT_SOUND( ENT( pev ), CHAN_VOICE, "buttons/spark5.wav", flVolume, ATTN_NORM );
break;
case 1:
EMIT_SOUND( ENT( pev ), CHAN_VOICE, "buttons/spark6.wav", flVolume, ATTN_NORM );
break;
7 years ago
}
}
break;
7 years ago
case matUnbreakableGlass:
8 years ago
UTIL_Ricochet( ptr->vecEndPos, RANDOM_FLOAT( 0.5, 1.5 ) );
7 years ago
break;
default:
break;
}
}
//LRC
if (m_iszWhenHit)
{
m_pHitProxy->pev->origin = ptr->vecEndPos;
m_pHitProxy->pev->velocity = vecDir;
FireTargets( STRING(m_iszWhenHit), m_pHitProxy, this, USE_TOGGLE, 0 );
}
CBaseDelay::TraceAttack( pevAttacker, flDamage, vecDir, ptr, bitsDamageType );
}
//=========================================================
// Special takedamage for func_breakable. Allows us to make
// exceptions that are breakable-specific
// bitsDamageType indicates the type of damage sustained ie: DMG_CRUSH
//=========================================================
8 years ago
int CBreakable::TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType )
7 years ago
{
8 years ago
Vector vecTemp;
7 years ago
// if Attacker == Inflictor, the attack was a melee or other instant-hit attack.
// (that is, no actual entity projectile was involved in the attack so use the shooter's origin).
8 years ago
if( pevAttacker == pevInflictor )
7 years ago
{
vecTemp = pevInflictor->origin - ( pev->absmin + ( pev->size * 0.5 ) );
// if a client hit the breakable with a crowbar, and breakable is crowbar-sensitive, break it now.
8 years ago
if( FBitSet ( pevAttacker->flags, FL_CLIENT ) &&
FBitSet ( pev->spawnflags, SF_BREAK_CROWBAR ) && ( bitsDamageType & DMG_CLUB ) )
7 years ago
flDamage = pev->health;
}
else
// an actual missile was involved.
{
vecTemp = pevInflictor->origin - ( pev->absmin + ( pev->size * 0.5 ) );
}
8 years ago
if( !IsBreakable() )
7 years ago
return 0;
// Breakables take double damage from the crowbar
8 years ago
if( bitsDamageType & DMG_CLUB )
7 years ago
flDamage *= 2;
// Boxes / glass / etc. don't take much poison damage, just the impact of the dart - consider that 10%
8 years ago
if( bitsDamageType & DMG_POISON )
7 years ago
flDamage *= 0.1;
// this global is still used for glass and other non-monster killables, along with decals.
7 years ago
g_vecAttackDir = vecTemp.Normalize();
// do the damage
7 years ago
pev->health -= flDamage;
8 years ago
if( pev->health <= 0 )
7 years ago
{
// LRC - Die() does everything necessary
// if (!m_iRespawnTime)
// {
// Killed( pevAttacker, GIB_NORMAL );
// }
Die();
return 0;
}
// Make a shard noise each time func breakable is hit.
// Don't play shard noise if cbreakable actually died.
DamageSound();
return 1;
}
void CBreakable::Die( void )
{
Vector vecSpot;// shard origin
Vector vecVelocity;// shard velocity
CBaseEntity *pEntity = NULL;
char cFlag = 0;
int pitch;
float fvol;
8 years ago
pitch = 95 + RANDOM_LONG( 0, 29 );
if( pitch > 97 && pitch < 103 )
7 years ago
pitch = 100;
// The more negative pev->health, the louder
// the sound should be.
fvol = RANDOM_FLOAT( 0.85, 1.0 ) + ( fabs( pev->health ) / 100.0 );
8 years ago
if( fvol > 1.0 )
7 years ago
fvol = 1.0;
8 years ago
switch( m_Material )
7 years ago
{
case matGlass:
8 years ago
switch( RANDOM_LONG( 0, 1 ) )
7 years ago
{
8 years ago
case 0:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustglass1.wav", fvol, ATTN_NORM, 0, pitch );
7 years ago
break;
8 years ago
case 1:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustglass2.wav", fvol, ATTN_NORM, 0, pitch );
7 years ago
break;
}
cFlag = BREAK_GLASS;
break;
case matWood:
8 years ago
switch( RANDOM_LONG( 0, 1 ) )
7 years ago
{
8 years ago
case 0:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustcrate1.wav", fvol, ATTN_NORM, 0, pitch );
7 years ago
break;
8 years ago
case 1:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustcrate2.wav", fvol, ATTN_NORM, 0, pitch );
7 years ago
break;
}
cFlag = BREAK_WOOD;
break;
case matComputer:
case matMetal:
8 years ago
switch( RANDOM_LONG( 0, 1 ) )
7 years ago
{
8 years ago
case 0:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustmetal1.wav", fvol, ATTN_NORM, 0, pitch );
7 years ago
break;
8 years ago
case 1:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustmetal2.wav", fvol, ATTN_NORM, 0, pitch );
7 years ago
break;
}
cFlag = BREAK_METAL;
break;
case matFlesh:
8 years ago
switch( RANDOM_LONG( 0, 1 ) )
7 years ago
{
8 years ago
case 0:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustflesh1.wav", fvol, ATTN_NORM, 0, pitch );
7 years ago
break;
8 years ago
case 1:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustflesh2.wav", fvol, ATTN_NORM, 0, pitch );
7 years ago
break;
}
cFlag = BREAK_FLESH;
break;
case matRocks:
case matCinderBlock:
8 years ago
switch( RANDOM_LONG( 0, 1 ) )
7 years ago
{
8 years ago
case 0:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustconcrete1.wav", fvol, ATTN_NORM, 0, pitch );
7 years ago
break;
8 years ago
case 1:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustconcrete2.wav", fvol, ATTN_NORM, 0, pitch );
7 years ago
break;
}
cFlag = BREAK_CONCRETE;
break;
case matCeilingTile:
8 years ago
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustceiling.wav", fvol, ATTN_NORM, 0, pitch );
7 years ago
break;
case matNone:
case matLastMaterial:
case matUnbreakableGlass:
break;
default:
break;
}
8 years ago
if( m_Explosion == expDirected )
7 years ago
vecVelocity = g_vecAttackDir * 200;
else
{
vecVelocity.x = 0;
vecVelocity.y = 0;
vecVelocity.z = 0;
}
8 years ago
vecSpot = pev->origin + ( pev->mins + pev->maxs ) * 0.5;
7 years ago
MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, vecSpot );
8 years ago
WRITE_BYTE( TE_BREAKMODEL );
7 years ago
// position
WRITE_COORD( vecSpot.x );
WRITE_COORD( vecSpot.y );
WRITE_COORD( vecSpot.z );
// size
8 years ago
WRITE_COORD( pev->size.x );
WRITE_COORD( pev->size.y );
WRITE_COORD( pev->size.z );
7 years ago
// velocity
WRITE_COORD( vecVelocity.x );
WRITE_COORD( vecVelocity.y );
WRITE_COORD( vecVelocity.z );
// randomization
8 years ago
WRITE_BYTE( 10 );
7 years ago
// Model
WRITE_SHORT( m_idShard ); //model id#
// # of shards
WRITE_BYTE( 0 ); // let client decide
// duration
WRITE_BYTE( 25 );// 2.5 seconds
// flags
WRITE_BYTE( cFlag );
MESSAGE_END();
float size = pev->size.x;
8 years ago
if( size < pev->size.y )
7 years ago
size = pev->size.y;
8 years ago
if( size < pev->size.z )
7 years ago
size = pev->size.z;
// !!! HACK This should work!
// Build a box above the entity that looks like an 8 pixel high sheet
Vector mins = pev->absmin;
Vector maxs = pev->absmax;
mins.z = pev->absmax.z;
maxs.z += 8;
// BUGBUG -- can only find 256 entities on a breakable -- should be enough
CBaseEntity *pList[256];
int count = UTIL_EntitiesInBox( pList, 256, mins, maxs, FL_ONGROUND );
8 years ago
if( count )
7 years ago
{
8 years ago
for( int i = 0; i < count; i++ )
7 years ago
{
ClearBits( pList[i]->pev->flags, FL_ONGROUND );
pList[i]->pev->groundentity = NULL;
}
}
// If I'm getting removed, don't fire something that could fire myself
if (!m_iRespawnTime)
9 years ago
pev->targetname = 0;
7 years ago
pev->solid = SOLID_NOT;
pev->effects |= EF_NODRAW;
pev->takedamage = DAMAGE_NO;
if (m_iStyle >= 32)
LIGHT_STYLE(m_iStyle, "a");
else if (m_iStyle <= -32)
LIGHT_STYLE(-m_iStyle, "z");
// Fire targets on break
SUB_UseTargets( NULL, USE_TOGGLE, 0 );
if (m_iRespawnTime == -1)
{
// ALERT(at_debug,"Waiting for respawn trigger\n");
SetUse(&CBreakable:: RespawnUse );
}
else if (m_iRespawnTime)
{
// ALERT(at_debug,"Respawning in %d secs\n",m_iRespawnTime);
SetThink(&CBreakable:: RespawnThink );
SetNextThink( m_iRespawnTime );
}
else
{
// ALERT(at_debug,"No respawn\n");
//tidy up
if (m_pHitProxy)
{
m_pHitProxy->SetThink(&CBreakable::SUB_Remove );
m_pHitProxy->SetNextThink( 0.1 );
m_pHitProxy = NULL;
}
SetThink(&CBreakable::SUB_Remove );
SetNextThink( 0.1 );
// ALERT(at_console, "Set SUB_Remove\n");
}
8 years ago
if( m_iszSpawnObject )
CBaseEntity::Create( STRING( m_iszSpawnObject ), VecBModelOrigin( pev ), pev->angles, edict() );
7 years ago
8 years ago
if( Explodable() )
7 years ago
{
ExplosionCreate( Center(), pev->angles, edict(), ExplosionMagnitude(), TRUE );
}
}
8 years ago
BOOL CBreakable::IsBreakable( void )
7 years ago
{
return m_Material != matUnbreakableGlass;
}
8 years ago
int CBreakable::DamageDecal( int bitsDamageType )
7 years ago
{
8 years ago
if( m_Material == matGlass )
return DECAL_GLASSBREAK1 + RANDOM_LONG( 0, 2 );
7 years ago
8 years ago
if( m_Material == matUnbreakableGlass )
7 years ago
return DECAL_BPROOF1;
return CBaseEntity::DamageDecal( bitsDamageType );
}
class CPushable : public CBreakable
{
public:
8 years ago
void Spawn ( void );
void Precache( void );
void Touch ( CBaseEntity *pOther );
void Move( CBaseEntity *pMover, int push );
void KeyValue( KeyValueData *pkvd );
void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
void EXPORT StopSound( void );
//virtual void SetActivator( CBaseEntity *pActivator ) { m_pPusher = pActivator; }
9 years ago
8 years ago
virtual int ObjectCaps( void ) { return ( CBaseEntity::ObjectCaps() & ~FCAP_ACROSS_TRANSITION ) | FCAP_CONTINUOUS_USE; }
virtual int Save( CSave &save );
virtual int Restore( CRestore &restore );
7 years ago
inline float MaxSpeed( void ) { return m_maxSpeed; }
7 years ago
// breakables use an overridden takedamage
8 years ago
virtual int TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType );
8 years ago
static TYPEDESCRIPTION m_SaveData[];
7 years ago
static const char *m_soundNames[3];
8 years ago
int m_lastSound; // no need to save/restore, just keeps the same sound from playing twice in a row
float m_maxSpeed;
float m_soundTime;
7 years ago
};
TYPEDESCRIPTION CPushable::m_SaveData[] =
7 years ago
{
DEFINE_FIELD( CPushable, m_maxSpeed, FIELD_FLOAT ),
DEFINE_FIELD( CPushable, m_soundTime, FIELD_TIME ),
};
IMPLEMENT_SAVERESTORE( CPushable, CBreakable )
7 years ago
LINK_ENTITY_TO_CLASS( func_pushable, CPushable )
7 years ago
const char *CPushable::m_soundNames[3] =
7 years ago
{
8 years ago
"debris/pushbox1.wav",
"debris/pushbox2.wav",
"debris/pushbox3.wav"
};
7 years ago
8 years ago
void CPushable::Spawn( void )
9 years ago
{
8 years ago
if( pev->spawnflags & SF_PUSH_BREAKABLE )
7 years ago
CBreakable::Spawn();
else
8 years ago
Precache();
7 years ago
8 years ago
pev->movetype = MOVETYPE_PUSHSTEP;
pev->solid = SOLID_BBOX;
SET_MODEL( ENT( pev ), STRING( pev->model ) );
7 years ago
8 years ago
if( pev->friction > 399 )
7 years ago
pev->friction = 399;
m_maxSpeed = 400 - pev->friction;
SetBits( pev->flags, FL_FLOAT );
pev->friction = 0;
pev->origin.z += 1; // Pick up off of the floor
UTIL_SetOrigin( this, pev->origin );
// Multiply by area of the box's cross-section (assume 1000 units^3 standard volume)
pev->skin = (int)( ( pev->skin * ( pev->maxs.x - pev->mins.x ) * ( pev->maxs.y - pev->mins.y ) ) * 0.0005 );
7 years ago
m_soundTime = 0;
}
8 years ago
void CPushable::Precache( void )
7 years ago
{
8 years ago
for( int i = 0; i < 3; i++ )
7 years ago
PRECACHE_SOUND( m_soundNames[i] );
8 years ago
if( pev->spawnflags & SF_PUSH_BREAKABLE )
CBreakable::Precache();
7 years ago
}
8 years ago
void CPushable::KeyValue( KeyValueData *pkvd )
7 years ago
{
8 years ago
if( FStrEq( pkvd->szKeyName, "size" ) )
7 years ago
{
8 years ago
int bbox = atoi( pkvd->szValue );
7 years ago
pkvd->fHandled = TRUE;
switch( bbox )
{
8 years ago
case 0:
// Point
UTIL_SetSize( pev, Vector( -8, -8, -8 ), Vector( 8, 8, 8 ) );
7 years ago
break;
8 years ago
case 2:
// Big Hull!?!? !!!BUGBUG Figure out what this hull really is
UTIL_SetSize( pev, VEC_DUCK_HULL_MIN*2, VEC_DUCK_HULL_MAX * 2 );
7 years ago
break;
8 years ago
case 3:
// Player duck
UTIL_SetSize( pev, VEC_DUCK_HULL_MIN, VEC_DUCK_HULL_MAX );
7 years ago
break;
default:
8 years ago
case 1:
// Player
UTIL_SetSize( pev, VEC_HULL_MIN, VEC_HULL_MAX );
7 years ago
break;
}
}
8 years ago
else if( FStrEq( pkvd->szKeyName, "buoyancy" ) )
7 years ago
{
pev->skin = atoi( pkvd->szValue );
7 years ago
pkvd->fHandled = TRUE;
}
else
CBreakable::KeyValue( pkvd );
}
// Pull the func_pushable
8 years ago
void CPushable::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
7 years ago
{
8 years ago
if( !pActivator || !pActivator->IsPlayer() )
7 years ago
{
8 years ago
if( pev->spawnflags & SF_PUSH_BREAKABLE )
7 years ago
this->CBreakable::Use( pActivator, pCaller, useType, value );
return;
}
if ( pev->spawnflags & SF_PUSH_NOPULL ) return; //LRC: a non-pullable pushable.
8 years ago
if( pActivator->pev->velocity != g_vecZero )
7 years ago
Move( pActivator, 0 );
}
8 years ago
void CPushable::Touch( CBaseEntity *pOther )
7 years ago
{
8 years ago
if( FClassnameIs( pOther->pev, "worldspawn" ) )
7 years ago
return;
Move( pOther, 1 );
}
8 years ago
void CPushable::Move( CBaseEntity *pOther, int push )
7 years ago
{
8 years ago
entvars_t* pevToucher = pOther->pev;
7 years ago
int playerTouch = 0;
// Is entity standing on this pushable ?
8 years ago
if( FBitSet( pevToucher->flags,FL_ONGROUND ) && pevToucher->groundentity && VARS( pevToucher->groundentity ) == pev )
7 years ago
{
// Only push if floating
if ( pev->waterlevel > 0 && pev->watertype > CONTENT_FLYFIELD)
pev->velocity.z += pevToucher->velocity.z * 0.1;
return;
}
9 years ago
// g-cont. fix pushable acceleration bug (reverted as it used in mods)
8 years ago
if( pOther->IsPlayer() )
7 years ago
{
9 years ago
// Don't push unless the player is pushing forward and NOT use (pull)
8 years ago
if( push && !( pevToucher->button & ( IN_FORWARD | IN_USE ) ) )
7 years ago
return;
playerTouch = 1;
}
float factor;
8 years ago
if( playerTouch )
7 years ago
{
8 years ago
if( !( pevToucher->flags & FL_ONGROUND ) ) // Don't push away from jumping/falling players unless in water
7 years ago
{
if ( pev->waterlevel < 1 || pev->watertype <= CONTENT_FLYFIELD)
return;
else
factor = 0.1;
}
else
factor = 1;
}
else
factor = 0.25;
if (!push)
factor = factor*0.5;
pev->velocity.x += pevToucher->velocity.x * factor;
pev->velocity.y += pevToucher->velocity.y * factor;
float length = sqrt( pev->velocity.x * pev->velocity.x + pev->velocity.y * pev->velocity.y );
8 years ago
if( push && ( length > MaxSpeed() ) )
7 years ago
{
pev->velocity.x = (pev->velocity.x * MaxSpeed() / length );
pev->velocity.y = (pev->velocity.y * MaxSpeed() / length );
}
8 years ago
if( playerTouch )
7 years ago
{
pevToucher->velocity.x = pev->velocity.x;
pevToucher->velocity.y = pev->velocity.y;
8 years ago
if( ( gpGlobals->time - m_soundTime ) > 0.7 )
7 years ago
{
m_soundTime = gpGlobals->time;
8 years ago
if( length > 0 && FBitSet( pev->flags,FL_ONGROUND ) )
7 years ago
{
8 years ago
m_lastSound = RANDOM_LONG( 0, 2 );
EMIT_SOUND( ENT( pev ), CHAN_WEAPON, m_soundNames[m_lastSound], 0.5, ATTN_NORM );
7 years ago
// SetThink( StopSound );
// SetNextThink( 0.1 );
}
else
8 years ago
STOP_SOUND( ENT( pev ), CHAN_WEAPON, m_soundNames[m_lastSound] );
7 years ago
}
}
}
#if 0
void CPushable::StopSound( void )
{
Vector dist = pev->oldorigin - pev->origin;
8 years ago
if( dist.Length() <= 0 )
STOP_SOUND( ENT( pev ), CHAN_WEAPON, m_soundNames[m_lastSound] );
7 years ago
}
#endif
8 years ago
int CPushable::TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType )
7 years ago
{
8 years ago
if( pev->spawnflags & SF_PUSH_BREAKABLE )
7 years ago
return CBreakable::TakeDamage( pevInflictor, pevAttacker, flDamage, bitsDamageType );
return 1;
}