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.

1000 lines
24 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.
*
****/
/*
===== 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"
9 years ago
extern DLL_GLOBAL Vector g_vecAttackDir;
9 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[] =
{
9 years ago
NULL, // 0
9 years ago
"item_battery", // 1
"item_healthkit", // 2
9 years ago
"weapon_9mmhandgun", // 3
9 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
9 years ago
"ammo_357", // 13
9 years ago
"weapon_rpg", // 14
"ammo_rpgclip", // 15
"ammo_gaussclip", // 16
9 years ago
"weapon_handgrenade", // 17
9 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!
9 years ago
if( FStrEq( pkvd->szKeyName, "explosion" ) )
9 years ago
{
9 years ago
if( !stricmp( pkvd->szValue, "directed" ) )
9 years ago
m_Explosion = expDirected;
9 years ago
else if( !stricmp( pkvd->szValue, "random" ) )
9 years ago
m_Explosion = expRandom;
else
m_Explosion = expRandom;
pkvd->fHandled = TRUE;
}
9 years ago
else if( FStrEq( pkvd->szKeyName, "material" ) )
9 years ago
{
9 years ago
int i = atoi( pkvd->szValue );
9 years ago
// 0:glass, 1:metal, 2:flesh, 3:wood
9 years ago
if( ( i < 0 ) || ( i >= matLastMaterial ) )
9 years ago
m_Material = matWood;
else
m_Material = (Materials)i;
pkvd->fHandled = TRUE;
}
9 years ago
else if( FStrEq( pkvd->szKeyName, "deadmodel" ) )
9 years ago
{
pkvd->fHandled = TRUE;
}
9 years ago
else if( FStrEq( pkvd->szKeyName, "shards" ) )
9 years ago
{
9 years ago
//m_iShards = atof( pkvd->szValue );
9 years ago
pkvd->fHandled = TRUE;
}
9 years ago
else if( FStrEq( pkvd->szKeyName, "gibmodel" ) )
9 years ago
{
9 years ago
m_iszGibModel = ALLOC_STRING( pkvd->szValue );
9 years ago
pkvd->fHandled = TRUE;
}
9 years ago
else if( FStrEq( pkvd->szKeyName, "spawnobject" ) )
9 years ago
{
int object = atoi( pkvd->szValue );
9 years ago
if( object > 0 && object < ARRAYSIZE( pSpawnObjects ) )
9 years ago
m_iszSpawnObject = MAKE_STRING( pSpawnObjects[object] );
pkvd->fHandled = TRUE;
}
9 years ago
else if( FStrEq( pkvd->szKeyName, "explodemagnitude" ) )
9 years ago
{
ExplosionSetMagnitude( atoi( pkvd->szValue ) );
pkvd->fHandled = TRUE;
}
9 years ago
else if( FStrEq( pkvd->szKeyName, "lip" ) )
9 years ago
pkvd->fHandled = TRUE;
else
CBaseDelay::KeyValue( pkvd );
}
//
// func_breakable - bmodel that breaks into pieces after taking damage
//
LINK_ENTITY_TO_CLASS( func_breakable, CBreakable )
9 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
};
IMPLEMENT_SAVERESTORE( CBreakable, CBaseEntity )
9 years ago
void CBreakable::Spawn( void )
{
9 years ago
Precache();
9 years ago
9 years ago
if( FBitSet( pev->spawnflags, SF_BREAK_TRIGGER_ONLY ) )
9 years ago
pev->takedamage = DAMAGE_NO;
else
pev->takedamage = DAMAGE_YES;
9 years ago
pev->solid = SOLID_BSP;
pev->movetype = MOVETYPE_PUSH;
m_angle = pev->angles.y;
pev->angles.y = 0;
9 years ago
// HACK: matGlass can receive decals, we need the client to know about this
// so use class to store the material flag
9 years ago
if( m_Material == matGlass )
9 years ago
{
pev->playerclass = 1;
}
9 years ago
SET_MODEL( ENT( pev ), STRING( pev->model ) );//set size and link into world.
9 years ago
SetTouch( &CBreakable::BreakTouch );
9 years ago
if( FBitSet( pev->spawnflags, SF_BREAK_TRIGGER_ONLY ) ) // Only break on trigger
9 years ago
SetTouch( NULL );
// Flag unbreakable glass as "worldbrush" so it will block ALL tracelines
9 years ago
if( !IsBreakable() && pev->rendermode != kRenderNormal )
9 years ago
pev->flags |= FL_WORLDBRUSH;
}
9 years ago
const char *CBreakable::pSoundsWood[] =
9 years ago
{
"debris/wood1.wav",
"debris/wood2.wav",
"debris/wood3.wav",
};
9 years ago
const char *CBreakable::pSoundsFlesh[] =
9 years ago
{
"debris/flesh1.wav",
"debris/flesh2.wav",
"debris/flesh3.wav",
"debris/flesh5.wav",
"debris/flesh6.wav",
"debris/flesh7.wav",
};
9 years ago
const char *CBreakable::pSoundsMetal[] =
9 years ago
{
"debris/metal1.wav",
"debris/metal2.wav",
"debris/metal3.wav",
};
9 years ago
const char *CBreakable::pSoundsConcrete[] =
9 years ago
{
"debris/concrete1.wav",
"debris/concrete2.wav",
"debris/concrete3.wav",
};
9 years ago
const char *CBreakable::pSoundsGlass[] =
9 years ago
{
"debris/glass1.wav",
"debris/glass2.wav",
"debris/glass3.wav",
};
const char **CBreakable::MaterialSoundList( Materials precacheMaterial, int &soundCount )
{
const char **pSoundList = NULL;
9 years ago
switch( precacheMaterial )
9 years ago
{
case matWood:
pSoundList = pSoundsWood;
9 years ago
soundCount = ARRAYSIZE( pSoundsWood );
9 years ago
break;
case matFlesh:
pSoundList = pSoundsFlesh;
9 years ago
soundCount = ARRAYSIZE( pSoundsFlesh );
9 years ago
break;
case matComputer:
case matUnbreakableGlass:
case matGlass:
pSoundList = pSoundsGlass;
9 years ago
soundCount = ARRAYSIZE( pSoundsGlass );
9 years ago
break;
case matMetal:
pSoundList = pSoundsMetal;
9 years ago
soundCount = ARRAYSIZE( pSoundsMetal );
9 years ago
break;
case matCinderBlock:
case matRocks:
pSoundList = pSoundsConcrete;
9 years ago
soundCount = ARRAYSIZE( pSoundsConcrete );
9 years ago
break;
case matCeilingTile:
case matNone:
default:
soundCount = 0;
break;
}
return pSoundList;
}
void CBreakable::MaterialSoundPrecache( Materials precacheMaterial )
{
9 years ago
const char **pSoundList;
int i, soundCount = 0;
9 years ago
pSoundList = MaterialSoundList( precacheMaterial, soundCount );
9 years ago
for( i = 0; i < soundCount; i++ )
9 years ago
{
PRECACHE_SOUND( (char *)pSoundList[i] );
}
}
void CBreakable::MaterialSoundRandom( edict_t *pEdict, Materials soundMaterial, float volume )
{
9 years ago
const char **pSoundList;
int soundCount = 0;
9 years ago
pSoundList = MaterialSoundList( soundMaterial, soundCount );
9 years ago
if( soundCount )
EMIT_SOUND( pEdict, CHAN_BODY, pSoundList[RANDOM_LONG( 0, soundCount - 1 )], volume, 1.0 );
9 years ago
}
void CBreakable::Precache( void )
{
const char *pGibName = NULL;
9 years ago
9 years ago
switch( m_Material )
9 years ago
{
case matWood:
pGibName = "models/woodgibs.mdl";
9 years ago
PRECACHE_SOUND( "debris/bustcrate1.wav" );
PRECACHE_SOUND( "debris/bustcrate2.wav" );
9 years ago
break;
case matFlesh:
pGibName = "models/fleshgibs.mdl";
9 years ago
PRECACHE_SOUND( "debris/bustflesh1.wav" );
PRECACHE_SOUND( "debris/bustflesh2.wav" );
9 years ago
break;
case matComputer:
9 years ago
PRECACHE_SOUND( "buttons/spark5.wav" );
PRECACHE_SOUND( "buttons/spark6.wav" );
9 years ago
pGibName = "models/computergibs.mdl";
9 years ago
PRECACHE_SOUND( "debris/bustmetal1.wav" );
PRECACHE_SOUND( "debris/bustmetal2.wav" );
break;
9 years ago
case matUnbreakableGlass:
case matGlass:
pGibName = "models/glassgibs.mdl";
9 years ago
PRECACHE_SOUND( "debris/bustglass1.wav" );
PRECACHE_SOUND( "debris/bustglass2.wav" );
9 years ago
break;
case matMetal:
pGibName = "models/metalplategibs.mdl";
9 years ago
PRECACHE_SOUND( "debris/bustmetal1.wav" );
PRECACHE_SOUND( "debris/bustmetal2.wav" );
9 years ago
break;
case matCinderBlock:
pGibName = "models/cindergibs.mdl";
9 years ago
PRECACHE_SOUND( "debris/bustconcrete1.wav" );
PRECACHE_SOUND( "debris/bustconcrete2.wav" );
9 years ago
break;
case matRocks:
pGibName = "models/rockgibs.mdl";
9 years ago
PRECACHE_SOUND( "debris/bustconcrete1.wav" );
PRECACHE_SOUND( "debris/bustconcrete2.wav" );
9 years ago
break;
case matCeilingTile:
pGibName = "models/ceilinggibs.mdl";
9 years ago
PRECACHE_SOUND( "debris/bustceiling.wav" );
9 years ago
break;
case matNone:
case matLastMaterial:
break;
default:
break;
9 years ago
}
MaterialSoundPrecache( m_Material );
9 years ago
if( m_iszGibModel )
pGibName = STRING( m_iszGibModel );
9 years ago
m_idShard = PRECACHE_MODEL( (char *)pGibName );
// Precache the spawn item's data
9 years ago
if( m_iszSpawnObject )
9 years ago
UTIL_PrecacheOther( (char *)STRING( m_iszSpawnObject ) );
}
// play shard sound when func_breakable takes damage.
// the more damage, the louder the shard sound.
void CBreakable::DamageSound( void )
{
int pitch;
float fvol;
char *rgpsz[6];
int i = 0;
int material = m_Material;
9 years ago
//if( RANDOM_LONG( 0, 1 ) )
// return;
9 years ago
9 years ago
if( RANDOM_LONG( 0, 2 ) )
9 years ago
pitch = PITCH_NORM;
else
9 years ago
pitch = 95 + RANDOM_LONG( 0, 34 );
9 years ago
9 years ago
fvol = RANDOM_FLOAT( 0.75, 1.0 );
9 years ago
9 years ago
if( material == matComputer && RANDOM_LONG( 0, 1 ) )
9 years ago
material = matMetal;
9 years ago
switch( material )
9 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;
}
9 years ago
if( i )
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, rgpsz[ RANDOM_LONG( 0, i - 1 )], fvol, ATTN_NORM, 0, pitch );
9 years ago
}
void CBreakable::BreakTouch( CBaseEntity *pOther )
{
float flDamage;
9 years ago
entvars_t* pevToucher = pOther->pev;
9 years ago
// only players can break these right now
9 years ago
if( !pOther->IsPlayer() || !IsBreakable() )
9 years ago
{
9 years ago
return;
9 years ago
}
9 years ago
if( FBitSet( pev->spawnflags, SF_BREAK_TOUCH ) )
{
// can be broken when run into
9 years ago
flDamage = pevToucher->velocity.Length() * 0.01;
9 years ago
if( flDamage >= pev->health )
9 years ago
{
SetTouch( NULL );
9 years ago
TakeDamage( pevToucher, pevToucher, flDamage, DMG_CRUSH );
9 years ago
// do a little damage to player if we broke glass or computer
pOther->TakeDamage( pev, pev, flDamage/4, DMG_SLASH );
}
}
9 years ago
if( FBitSet( pev->spawnflags, SF_BREAK_PRESSURE ) && pevToucher->absmin.z >= pev->maxs.z - 2 )
{
// can be broken when stood upon
9 years ago
// play creaking sound here.
DamageSound();
SetThink( &CBreakable::Die );
SetTouch( NULL );
9 years ago
if( m_flDelay == 0 )
{
// !!!BUGBUG - why doesn't zero delay work?
9 years ago
m_flDelay = 0.1;
}
pev->nextthink = pev->ltime + m_flDelay;
}
}
//
// Smash the our breakable object
//
// Break when triggered
void CBreakable::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
{
9 years ago
if( IsBreakable() )
9 years ago
{
pev->angles.y = m_angle;
9 years ago
UTIL_MakeVectors( pev->angles );
9 years ago
g_vecAttackDir = gpGlobals->v_forward;
Die();
}
}
void CBreakable::TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType )
{
// random spark if this is a 'computer' object
9 years ago
if( RANDOM_LONG( 0, 1 ) )
9 years ago
{
switch( m_Material )
{
case matComputer:
{
UTIL_Sparks( ptr->vecEndPos );
9 years ago
float flVolume = RANDOM_FLOAT( 0.7 , 1.0 );//random volume range
switch( RANDOM_LONG( 0, 1 ) )
9 years ago
{
9 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;
9 years ago
}
}
break;
9 years ago
case matUnbreakableGlass:
9 years ago
UTIL_Ricochet( ptr->vecEndPos, RANDOM_FLOAT( 0.5, 1.5 ) );
break;
default:
break;
9 years ago
}
}
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
//=========================================================
9 years ago
int CBreakable::TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType )
9 years ago
{
9 years ago
Vector vecTemp;
9 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).
9 years ago
if( pevAttacker == pevInflictor )
9 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.
9 years ago
if( FBitSet ( pevAttacker->flags, FL_CLIENT ) &&
FBitSet ( pev->spawnflags, SF_BREAK_CROWBAR ) && ( bitsDamageType & DMG_CLUB ) )
9 years ago
flDamage = pev->health;
}
else
// an actual missile was involved.
{
vecTemp = pevInflictor->origin - ( pev->absmin + ( pev->size * 0.5 ) );
}
9 years ago
if( !IsBreakable() )
9 years ago
return 0;
// Breakables take double damage from the crowbar
9 years ago
if( bitsDamageType & DMG_CLUB )
9 years ago
flDamage *= 2;
// Boxes / glass / etc. don't take much poison damage, just the impact of the dart - consider that 10%
9 years ago
if( bitsDamageType & DMG_POISON )
9 years ago
flDamage *= 0.1;
// this global is still used for glass and other non-monster killables, along with decals.
9 years ago
g_vecAttackDir = vecTemp.Normalize();
// do the damage
9 years ago
pev->health -= flDamage;
9 years ago
if( pev->health <= 0 )
9 years ago
{
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;
9 years ago
pitch = 95 + RANDOM_LONG( 0, 29 );
if( pitch > 97 && pitch < 103 )
9 years ago
pitch = 100;
// The more negative pev->health, the louder
// the sound should be.
9 years ago
fvol = RANDOM_FLOAT( 0.85, 1.0 ) + ( fabs( pev->health ) / 100.0 );
9 years ago
9 years ago
if( fvol > 1.0 )
9 years ago
fvol = 1.0;
9 years ago
switch( m_Material )
9 years ago
{
case matGlass:
9 years ago
switch( RANDOM_LONG( 0, 1 ) )
9 years ago
{
9 years ago
case 0:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustglass1.wav", fvol, ATTN_NORM, 0, pitch );
9 years ago
break;
9 years ago
case 1:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustglass2.wav", fvol, ATTN_NORM, 0, pitch );
9 years ago
break;
}
cFlag = BREAK_GLASS;
break;
case matWood:
9 years ago
switch( RANDOM_LONG( 0, 1 ) )
9 years ago
{
9 years ago
case 0:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustcrate1.wav", fvol, ATTN_NORM, 0, pitch );
9 years ago
break;
9 years ago
case 1:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustcrate2.wav", fvol, ATTN_NORM, 0, pitch );
9 years ago
break;
}
cFlag = BREAK_WOOD;
break;
case matComputer:
case matMetal:
9 years ago
switch( RANDOM_LONG( 0, 1 ) )
9 years ago
{
9 years ago
case 0:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustmetal1.wav", fvol, ATTN_NORM, 0, pitch );
9 years ago
break;
9 years ago
case 1:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustmetal2.wav", fvol, ATTN_NORM, 0, pitch );
9 years ago
break;
}
cFlag = BREAK_METAL;
break;
case matFlesh:
9 years ago
switch( RANDOM_LONG( 0, 1 ) )
9 years ago
{
9 years ago
case 0:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustflesh1.wav", fvol, ATTN_NORM, 0, pitch );
9 years ago
break;
9 years ago
case 1:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustflesh2.wav", fvol, ATTN_NORM, 0, pitch );
9 years ago
break;
}
cFlag = BREAK_FLESH;
break;
case matRocks:
case matCinderBlock:
9 years ago
switch( RANDOM_LONG( 0, 1 ) )
9 years ago
{
9 years ago
case 0:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustconcrete1.wav", fvol, ATTN_NORM, 0, pitch );
9 years ago
break;
9 years ago
case 1:
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustconcrete2.wav", fvol, ATTN_NORM, 0, pitch );
9 years ago
break;
}
cFlag = BREAK_CONCRETE;
break;
case matCeilingTile:
9 years ago
EMIT_SOUND_DYN( ENT( pev ), CHAN_VOICE, "debris/bustceiling.wav", fvol, ATTN_NORM, 0, pitch );
9 years ago
break;
case matNone:
case matLastMaterial:
case matUnbreakableGlass:
break;
default:
break;
9 years ago
}
9 years ago
if( m_Explosion == expDirected )
9 years ago
vecVelocity = g_vecAttackDir * 200;
else
{
vecVelocity.x = 0;
vecVelocity.y = 0;
vecVelocity.z = 0;
}
9 years ago
vecSpot = pev->origin + ( pev->mins + pev->maxs ) * 0.5;
9 years ago
MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, vecSpot );
9 years ago
WRITE_BYTE( TE_BREAKMODEL );
9 years ago
// position
WRITE_COORD( vecSpot.x );
WRITE_COORD( vecSpot.y );
WRITE_COORD( vecSpot.z );
// size
9 years ago
WRITE_COORD( pev->size.x );
WRITE_COORD( pev->size.y );
WRITE_COORD( pev->size.z );
9 years ago
// velocity
WRITE_COORD( vecVelocity.x );
WRITE_COORD( vecVelocity.y );
WRITE_COORD( vecVelocity.z );
// randomization
9 years ago
WRITE_BYTE( 10 );
9 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;
9 years ago
if( size < pev->size.y )
9 years ago
size = pev->size.y;
9 years ago
if( size < pev->size.z )
9 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 );
9 years ago
if( count )
9 years ago
{
9 years ago
for( int i = 0; i < count; i++ )
9 years ago
{
ClearBits( pList[i]->pev->flags, FL_ONGROUND );
pList[i]->pev->groundentity = NULL;
}
}
// Don't fire something that could fire myself
pev->targetname = 0;
pev->solid = SOLID_NOT;
9 years ago
// Fire targets on break
SUB_UseTargets( NULL, USE_TOGGLE, 0 );
SetThink( &CBaseEntity::SUB_Remove );
pev->nextthink = pev->ltime + 0.1;
9 years ago
if( m_iszSpawnObject )
CBaseEntity::Create( (char *)STRING( m_iszSpawnObject ), VecBModelOrigin( pev ), pev->angles, edict() );
9 years ago
9 years ago
if( Explodable() )
9 years ago
{
ExplosionCreate( Center(), pev->angles, edict(), ExplosionMagnitude(), TRUE );
}
}
9 years ago
BOOL CBreakable::IsBreakable( void )
9 years ago
{
return m_Material != matUnbreakableGlass;
}
9 years ago
int CBreakable::DamageDecal( int bitsDamageType )
9 years ago
{
9 years ago
if( m_Material == matGlass )
return DECAL_GLASSBREAK1 + RANDOM_LONG( 0, 2 );
9 years ago
9 years ago
if( m_Material == matUnbreakableGlass )
9 years ago
return DECAL_BPROOF1;
return CBaseEntity::DamageDecal( bitsDamageType );
}
class CPushable : public CBreakable
{
public:
9 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
9 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 );
9 years ago
inline float MaxSpeed( void ) { return m_maxSpeed; }
9 years ago
// breakables use an overridden takedamage
9 years ago
virtual int TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType );
9 years ago
static TYPEDESCRIPTION m_SaveData[];
9 years ago
static char *m_soundNames[3];
9 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;
9 years ago
};
TYPEDESCRIPTION CPushable::m_SaveData[] =
9 years ago
{
DEFINE_FIELD( CPushable, m_maxSpeed, FIELD_FLOAT ),
DEFINE_FIELD( CPushable, m_soundTime, FIELD_TIME ),
};
IMPLEMENT_SAVERESTORE( CPushable, CBreakable )
9 years ago
LINK_ENTITY_TO_CLASS( func_pushable, CPushable )
9 years ago
9 years ago
char *CPushable::m_soundNames[3] =
{
"debris/pushbox1.wav",
"debris/pushbox2.wav",
"debris/pushbox3.wav"
};
9 years ago
9 years ago
void CPushable::Spawn( void )
9 years ago
{
9 years ago
if( pev->spawnflags & SF_PUSH_BREAKABLE )
9 years ago
CBreakable::Spawn();
else
9 years ago
Precache();
9 years ago
9 years ago
pev->movetype = MOVETYPE_PUSHSTEP;
pev->solid = SOLID_BBOX;
SET_MODEL( ENT( pev ), STRING( pev->model ) );
9 years ago
9 years ago
if( pev->friction > 399 )
9 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( pev, pev->origin );
// Multiply by area of the box's cross-section (assume 1000 units^3 standard volume)
9 years ago
pev->skin = ( pev->skin * ( pev->maxs.x - pev->mins.x ) * ( pev->maxs.y - pev->mins.y ) ) * 0.0005;
9 years ago
m_soundTime = 0;
}
9 years ago
void CPushable::Precache( void )
9 years ago
{
9 years ago
for( int i = 0; i < 3; i++ )
9 years ago
PRECACHE_SOUND( m_soundNames[i] );
9 years ago
if( pev->spawnflags & SF_PUSH_BREAKABLE )
CBreakable::Precache();
9 years ago
}
9 years ago
void CPushable::KeyValue( KeyValueData *pkvd )
9 years ago
{
9 years ago
if( FStrEq( pkvd->szKeyName, "size" ) )
9 years ago
{
9 years ago
int bbox = atoi( pkvd->szValue );
9 years ago
pkvd->fHandled = TRUE;
switch( bbox )
{
9 years ago
case 0:
// Point
UTIL_SetSize( pev, Vector( -8, -8, -8 ), Vector( 8, 8, 8 ) );
9 years ago
break;
9 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 );
9 years ago
break;
9 years ago
case 3:
// Player duck
UTIL_SetSize( pev, VEC_DUCK_HULL_MIN, VEC_DUCK_HULL_MAX );
9 years ago
break;
default:
9 years ago
case 1:
// Player
UTIL_SetSize( pev, VEC_HULL_MIN, VEC_HULL_MAX );
9 years ago
break;
}
}
9 years ago
else if( FStrEq( pkvd->szKeyName, "buoyancy" ) )
9 years ago
{
9 years ago
pev->skin = atof( pkvd->szValue );
9 years ago
pkvd->fHandled = TRUE;
}
else
CBreakable::KeyValue( pkvd );
}
// Pull the func_pushable
9 years ago
void CPushable::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
9 years ago
{
9 years ago
if( !pActivator || !pActivator->IsPlayer() )
9 years ago
{
9 years ago
if( pev->spawnflags & SF_PUSH_BREAKABLE )
9 years ago
this->CBreakable::Use( pActivator, pCaller, useType, value );
return;
}
9 years ago
if( pActivator->pev->velocity != g_vecZero )
9 years ago
Move( pActivator, 0 );
}
9 years ago
void CPushable::Touch( CBaseEntity *pOther )
9 years ago
{
9 years ago
if( FClassnameIs( pOther->pev, "worldspawn" ) )
9 years ago
return;
Move( pOther, 1 );
}
9 years ago
void CPushable::Move( CBaseEntity *pOther, int push )
9 years ago
{
9 years ago
entvars_t* pevToucher = pOther->pev;
9 years ago
int playerTouch = 0;
// Is entity standing on this pushable ?
9 years ago
if( FBitSet( pevToucher->flags,FL_ONGROUND ) && pevToucher->groundentity && VARS( pevToucher->groundentity ) == pev )
9 years ago
{
// Only push if floating
9 years ago
if( pev->waterlevel > 0 )
9 years ago
pev->velocity.z += pevToucher->velocity.z * 0.1;
return;
}
// g-cont. fix pushable acceleration bug (reverted as it used in mods)
9 years ago
if( pOther->IsPlayer() )
9 years ago
{
// Don't push unless the player is pushing forward and NOT use (pull)
9 years ago
if( push && !( pevToucher->button & ( IN_FORWARD | IN_USE ) ) )
9 years ago
return;
playerTouch = 1;
}
float factor;
9 years ago
if( playerTouch )
9 years ago
{
9 years ago
if( !( pevToucher->flags & FL_ONGROUND ) ) // Don't push away from jumping/falling players unless in water
9 years ago
{
9 years ago
if( pev->waterlevel < 1 )
9 years ago
return;
else
factor = 0.1;
}
else
factor = 1;
}
else
factor = 0.25;
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 );
9 years ago
if( push && ( length > MaxSpeed() ) )
9 years ago
{
pev->velocity.x = (pev->velocity.x * MaxSpeed() / length );
pev->velocity.y = (pev->velocity.y * MaxSpeed() / length );
}
9 years ago
if( playerTouch )
9 years ago
{
pevToucher->velocity.x = pev->velocity.x;
pevToucher->velocity.y = pev->velocity.y;
9 years ago
if( ( gpGlobals->time - m_soundTime ) > 0.7 )
9 years ago
{
m_soundTime = gpGlobals->time;
9 years ago
if( length > 0 && FBitSet( pev->flags,FL_ONGROUND ) )
9 years ago
{
9 years ago
m_lastSound = RANDOM_LONG( 0, 2 );
EMIT_SOUND( ENT( pev ), CHAN_WEAPON, m_soundNames[m_lastSound], 0.5, ATTN_NORM );
//SetThink( &StopSound );
//pev->nextthink = pev->ltime + 0.1;
9 years ago
}
else
9 years ago
STOP_SOUND( ENT( pev ), CHAN_WEAPON, m_soundNames[m_lastSound] );
9 years ago
}
}
}
#if 0
void CPushable::StopSound( void )
{
Vector dist = pev->oldorigin - pev->origin;
9 years ago
if( dist.Length() <= 0 )
STOP_SOUND( ENT( pev ), CHAN_WEAPON, m_soundNames[m_lastSound] );
9 years ago
}
#endif
9 years ago
int CPushable::TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType )
9 years ago
{
9 years ago
if( pev->spawnflags & SF_PUSH_BREAKABLE )
9 years ago
return CBreakable::TakeDamage( pevInflictor, pevAttacker, flDamage, bitsDamageType );
return 1;
}