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.

903 lines
23 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 "doors.h"
extern DLL_GLOBAL Vector g_vecAttackDir;
#define SF_BRUSH_ACCDCC 16// brush should accelerate and decelerate when toggled
#define SF_BRUSH_HURT 32// rotating brush that inflicts pain based on rotation speed
#define SF_ROTATING_NOT_SOLID 64 // some special rotating objects are not solid.
// covering cheesy noise1, noise2, & noise3 fields so they make more sense (for rotating fans)
#define noiseStart noise1
#define noiseStop noise2
#define noiseRunning noise3
#define SF_PENDULUM_SWING 2 // spawnflag that makes a pendulum a rope swing.
//
// BModelOrigin - calculates origin of a bmodel from absmin/size because all bmodel origins are 0 0 0
//
Vector VecBModelOrigin( entvars_t* pevBModel )
{
return pevBModel->absmin + ( pevBModel->size * 0.5 );
}
// =================== FUNC_WALL ==============================================
/*QUAKED func_wall (0 .5 .8) ?
This is just a solid wall if not inhibited
*/
class CFuncWall : public CBaseEntity
{
public:
8 years ago
void Spawn( void );
void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
9 years ago
// Bmodels don't go across transitions
8 years ago
virtual int ObjectCaps( void ) { return CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; }
9 years ago
};
LINK_ENTITY_TO_CLASS( func_wall, CFuncWall )
9 years ago
8 years ago
void CFuncWall::Spawn( void )
9 years ago
{
8 years ago
pev->angles = g_vecZero;
pev->movetype = MOVETYPE_PUSH; // so it doesn't get pushed by anything
pev->solid = SOLID_BSP;
SET_MODEL( ENT( pev ), STRING( pev->model ) );
9 years ago
// If it can't move/go away, it's really part of the world
pev->flags |= FL_WORLDBRUSH;
}
8 years ago
void CFuncWall::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
9 years ago
{
8 years ago
if( ShouldToggle( useType, (int)( pev->frame ) ) )
9 years ago
pev->frame = 1 - pev->frame;
}
#define SF_WALL_START_OFF 0x0001
class CFuncWallToggle : public CFuncWall
{
public:
8 years ago
void Spawn( void );
void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
void TurnOff( void );
void TurnOn( void );
BOOL IsOn( void );
9 years ago
};
LINK_ENTITY_TO_CLASS( func_wall_toggle, CFuncWallToggle )
9 years ago
8 years ago
void CFuncWallToggle::Spawn( void )
9 years ago
{
CFuncWall::Spawn();
8 years ago
if( pev->spawnflags & SF_WALL_START_OFF )
9 years ago
TurnOff();
}
8 years ago
void CFuncWallToggle::TurnOff( void )
9 years ago
{
pev->solid = SOLID_NOT;
pev->effects |= EF_NODRAW;
UTIL_SetOrigin( pev, pev->origin );
}
8 years ago
void CFuncWallToggle::TurnOn( void )
9 years ago
{
pev->solid = SOLID_BSP;
pev->effects &= ~EF_NODRAW;
UTIL_SetOrigin( pev, pev->origin );
}
8 years ago
BOOL CFuncWallToggle::IsOn( void )
9 years ago
{
8 years ago
if( pev->solid == SOLID_NOT )
9 years ago
return FALSE;
return TRUE;
}
8 years ago
void CFuncWallToggle::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
9 years ago
{
int status = IsOn();
8 years ago
if( ShouldToggle( useType, status ) )
9 years ago
{
8 years ago
if( status )
9 years ago
TurnOff();
else
TurnOn();
}
}
8 years ago
#define SF_CONVEYOR_VISUAL 0x0001
9 years ago
#define SF_CONVEYOR_NOTSOLID 0x0002
class CFuncConveyor : public CFuncWall
{
public:
8 years ago
void Spawn( void );
void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
void UpdateSpeed( float speed );
9 years ago
};
LINK_ENTITY_TO_CLASS( func_conveyor, CFuncConveyor )
8 years ago
void CFuncConveyor::Spawn( void )
9 years ago
{
SetMovedir( pev );
CFuncWall::Spawn();
8 years ago
if( !( pev->spawnflags & SF_CONVEYOR_VISUAL ) )
9 years ago
SetBits( pev->flags, FL_CONVEYOR );
// HACKHACK - This is to allow for some special effects
8 years ago
if( pev->spawnflags & SF_CONVEYOR_NOTSOLID )
9 years ago
{
pev->solid = SOLID_NOT;
pev->skin = 0; // Don't want the engine thinking we've got special contents on this brush
}
8 years ago
if( pev->speed == 0 )
9 years ago
pev->speed = 100;
UpdateSpeed( pev->speed );
}
// HACKHACK -- This is ugly, but encode the speed in the rendercolor to avoid adding more data to the network stream
8 years ago
void CFuncConveyor::UpdateSpeed( float speed )
9 years ago
{
// Encode it as an integer with 4 fractional bits
8 years ago
int speedCode = (int)( fabs( speed ) * 16.0 );
9 years ago
8 years ago
if( speed < 0 )
9 years ago
pev->rendercolor.x = 1;
else
pev->rendercolor.x = 0;
8 years ago
pev->rendercolor.y = speedCode >> 8;
pev->rendercolor.z = speedCode & 0xFF;
9 years ago
}
8 years ago
void CFuncConveyor::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
9 years ago
{
pev->speed = -pev->speed;
UpdateSpeed( pev->speed );
}
// =================== FUNC_ILLUSIONARY ==============================================
/*QUAKED func_illusionary (0 .5 .8) ?
A simple entity that looks solid but lets you walk through it.
*/
class CFuncIllusionary : public CBaseToggle
{
public:
void Spawn( void );
void EXPORT SloshTouch( CBaseEntity *pOther );
void KeyValue( KeyValueData *pkvd );
8 years ago
virtual int ObjectCaps( void ) { return CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; }
9 years ago
};
LINK_ENTITY_TO_CLASS( func_illusionary, CFuncIllusionary )
9 years ago
8 years ago
void CFuncIllusionary::KeyValue( KeyValueData *pkvd )
9 years ago
{
8 years ago
if( FStrEq( pkvd->szKeyName, "skin" ) )//skin is used for content type
9 years ago
{
pev->skin = atoi( pkvd->szValue );
9 years ago
pkvd->fHandled = TRUE;
}
else
CBaseToggle::KeyValue( pkvd );
}
8 years ago
void CFuncIllusionary::Spawn( void )
9 years ago
{
pev->angles = g_vecZero;
pev->movetype = MOVETYPE_NONE;
pev->solid = SOLID_NOT;// always solid_not
8 years ago
SET_MODEL( ENT( pev ), STRING( pev->model ) );
9 years ago
// I'd rather eat the network bandwidth of this than figure out how to save/restore
// these entities after they have been moved to the client, or respawn them ala Quake
// Perhaps we can do this in deathmatch only.
// MAKE_STATIC(ENT(pev));
}
// -------------------------------------------------------------------------------
//
// Monster only clip brush
//
// This brush will be solid for any entity who has the FL_MONSTERCLIP flag set
// in pev->flags
//
// otherwise it will be invisible and not solid. This can be used to keep
// specific monsters out of certain areas
//
// -------------------------------------------------------------------------------
class CFuncMonsterClip : public CFuncWall
{
public:
8 years ago
void Spawn( void );
void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) {} // Clear out func_wall's use function
9 years ago
};
LINK_ENTITY_TO_CLASS( func_monsterclip, CFuncMonsterClip )
9 years ago
void CFuncMonsterClip::Spawn( void )
{
CFuncWall::Spawn();
8 years ago
if( CVAR_GET_FLOAT( "showtriggers" ) == 0 )
9 years ago
pev->effects = EF_NODRAW;
pev->flags |= FL_MONSTERCLIP;
}
// =================== FUNC_ROTATING ==============================================
class CFuncRotating : public CBaseEntity
{
public:
// basic functions
void Spawn( void );
void Precache( void );
8 years ago
void EXPORT SpinUp( void );
void EXPORT SpinDown( void );
9 years ago
void KeyValue( KeyValueData* pkvd);
8 years ago
void EXPORT HurtTouch( CBaseEntity *pOther );
9 years ago
void EXPORT RotatingUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
void EXPORT Rotate( void );
8 years ago
void RampPitchVol(int fUp );
9 years ago
void Blocked( CBaseEntity *pOther );
8 years ago
virtual int ObjectCaps( void ) { return CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; }
virtual int Save( CSave &save );
virtual int Restore( CRestore &restore );
static TYPEDESCRIPTION m_SaveData[];
9 years ago
float m_flFanFriction;
float m_flAttenuation;
float m_flVolume;
float m_pitch;
8 years ago
int m_sounds;
9 years ago
};
8 years ago
TYPEDESCRIPTION CFuncRotating::m_SaveData[] =
9 years ago
{
DEFINE_FIELD( CFuncRotating, m_flFanFriction, FIELD_FLOAT ),
DEFINE_FIELD( CFuncRotating, m_flAttenuation, FIELD_FLOAT ),
DEFINE_FIELD( CFuncRotating, m_flVolume, FIELD_FLOAT ),
DEFINE_FIELD( CFuncRotating, m_pitch, FIELD_FLOAT ),
DEFINE_FIELD( CFuncRotating, m_sounds, FIELD_INTEGER )
};
IMPLEMENT_SAVERESTORE( CFuncRotating, CBaseEntity )
9 years ago
LINK_ENTITY_TO_CLASS( func_rotating, CFuncRotating )
9 years ago
8 years ago
void CFuncRotating::KeyValue( KeyValueData* pkvd )
9 years ago
{
8 years ago
if( FStrEq( pkvd->szKeyName, "fanfriction" ) )
9 years ago
{
8 years ago
m_flFanFriction = atof( pkvd->szValue ) / 100;
9 years ago
pkvd->fHandled = TRUE;
}
8 years ago
else if( FStrEq( pkvd->szKeyName, "Volume" ) )
9 years ago
{
8 years ago
m_flVolume = atof( pkvd->szValue ) / 10.0;
9 years ago
8 years ago
if( m_flVolume > 1.0 )
9 years ago
m_flVolume = 1.0;
8 years ago
if( m_flVolume < 0.0 )
9 years ago
m_flVolume = 0.0;
pkvd->fHandled = TRUE;
}
8 years ago
else if( FStrEq( pkvd->szKeyName, "spawnorigin" ) )
9 years ago
{
Vector tmp;
UTIL_StringToVector( (float *)tmp, pkvd->szValue );
8 years ago
if( tmp != g_vecZero )
9 years ago
pev->origin = tmp;
}
8 years ago
else if( FStrEq( pkvd->szKeyName, "sounds" ) )
9 years ago
{
8 years ago
m_sounds = atoi( pkvd->szValue );
9 years ago
pkvd->fHandled = TRUE;
}
else
CBaseEntity::KeyValue( pkvd );
}
/*QUAKED func_rotating (0 .5 .8) ? START_ON REVERSE X_AXIS Y_AXIS
You need to have an origin brush as part of this entity. The
center of that brush will be
the point around which it is rotated. It will rotate around the Z
axis by default. You can
check either the X_AXIS or Y_AXIS box to change that.
"speed" determines how fast it moves; default value is 100.
"dmg" damage to inflict when blocked (2 default)
REVERSE will cause the it to rotate in the opposite direction.
*/
8 years ago
void CFuncRotating::Spawn()
9 years ago
{
// set final pitch. Must not be PITCH_NORM, since we
// plan on pitch shifting later.
m_pitch = PITCH_NORM - 1;
// maintain compatibility with previous maps
8 years ago
if( m_flVolume == 0.0 )
9 years ago
m_flVolume = 1.0;
// if the designer didn't set a sound attenuation, default to one.
m_flAttenuation = ATTN_NORM;
8 years ago
if( FBitSet( pev->spawnflags, SF_BRUSH_ROTATE_SMALLRADIUS) )
9 years ago
{
m_flAttenuation = ATTN_IDLE;
}
8 years ago
else if( FBitSet( pev->spawnflags, SF_BRUSH_ROTATE_MEDIUMRADIUS ) )
9 years ago
{
m_flAttenuation = ATTN_STATIC;
}
8 years ago
else if( FBitSet( pev->spawnflags, SF_BRUSH_ROTATE_LARGERADIUS ) )
9 years ago
{
m_flAttenuation = ATTN_NORM;
}
// prevent divide by zero if level designer forgets friction!
8 years ago
if( m_flFanFriction == 0 )
9 years ago
{
m_flFanFriction = 1;
}
8 years ago
if( FBitSet( pev->spawnflags, SF_BRUSH_ROTATE_Z_AXIS ) )
pev->movedir = Vector( 0, 0, 1 );
else if( FBitSet( pev->spawnflags, SF_BRUSH_ROTATE_X_AXIS ) )
pev->movedir = Vector( 1, 0, 0 );
9 years ago
else
8 years ago
pev->movedir = Vector( 0, 1, 0 ); // y-axis
9 years ago
// check for reverse rotation
8 years ago
if( FBitSet( pev->spawnflags, SF_BRUSH_ROTATE_BACKWARDS ) )
9 years ago
pev->movedir = pev->movedir * -1;
// some rotating objects like fake volumetric lights will not be solid.
8 years ago
if( FBitSet( pev->spawnflags, SF_ROTATING_NOT_SOLID ) )
9 years ago
{
pev->solid = SOLID_NOT;
pev->skin = CONTENTS_EMPTY;
8 years ago
pev->movetype = MOVETYPE_PUSH;
9 years ago
}
else
{
8 years ago
pev->solid = SOLID_BSP;
pev->movetype = MOVETYPE_PUSH;
9 years ago
}
8 years ago
UTIL_SetOrigin( pev, pev->origin );
9 years ago
SET_MODEL( ENT(pev), STRING(pev->model) );
SetUse( &CFuncRotating::RotatingUse );
// did level designer forget to assign speed?
8 years ago
if( pev->speed <= 0 )
9 years ago
pev->speed = 0;
// Removed this per level designers request. -- JAY
8 years ago
// if( pev->dmg == 0 )
9 years ago
// pev->dmg = 2;
// instant-use brush?
8 years ago
if( FBitSet( pev->spawnflags, SF_BRUSH_ROTATE_INSTANT ) )
{
9 years ago
SetThink( &CBaseEntity::SUB_CallUseToggle );
pev->nextthink = pev->ltime + 1.5; // leave a magic delay for client to start up
8 years ago
}
9 years ago
// can this brush inflict pain?
8 years ago
if( FBitSet( pev->spawnflags, SF_BRUSH_HURT ) )
9 years ago
{
SetTouch( &CFuncRotating::HurtTouch );
}
8 years ago
Precache();
9 years ago
}
8 years ago
void CFuncRotating::Precache( void )
9 years ago
{
const char* szSoundFile = STRING( pev->message );
BOOL NullSound = FALSE;
9 years ago
// set up fan sounds
8 years ago
if( !FStringNull( pev->message ) && strlen( szSoundFile ) > 0 )
9 years ago
{
// if a path is set for a wave, use it
}
else
9 years ago
{
// otherwise use preset sound
8 years ago
switch( m_sounds )
9 years ago
{
case 1:
szSoundFile = "fans/fan1.wav";
9 years ago
break;
case 2:
szSoundFile = "fans/fan2.wav";
9 years ago
break;
case 3:
szSoundFile = "fans/fan3.wav";
9 years ago
break;
case 4:
szSoundFile = "fans/fan4.wav";
9 years ago
break;
case 5:
szSoundFile = "fans/fan5.wav";
9 years ago
break;
case 0:
default:
szSoundFile = "common/null.wav";
NullSound = TRUE;
break;
9 years ago
}
}
8 years ago
if( !NullSound )
PRECACHE_SOUND( szSoundFile );
pev->noiseRunning = MAKE_STRING( szSoundFile );
8 years ago
if( pev->avelocity != g_vecZero )
9 years ago
{
// if fan was spinning, and we went through transition or save/restore,
// make sure we restart the sound. 1.5 sec delay is magic number. KDB
SetThink( &CFuncRotating::SpinUp );
pev->nextthink = pev->ltime + 1.5;
}
}
//
// Touch - will hurt others based on how fast the brush is spinning
//
8 years ago
void CFuncRotating::HurtTouch( CBaseEntity *pOther )
9 years ago
{
8 years ago
entvars_t *pevOther = pOther->pev;
9 years ago
// we can't hurt this thing, so we're not concerned with it
8 years ago
if( !pevOther->takedamage )
9 years ago
return;
// calculate damage based on rotation speed
pev->dmg = pev->avelocity.Length() / 10;
8 years ago
pOther->TakeDamage( pev, pev, pev->dmg, DMG_CRUSH );
pevOther->velocity = ( pevOther->origin - VecBModelOrigin( pev ) ).Normalize() * pev->dmg;
9 years ago
}
//
// RampPitchVol - ramp pitch and volume up to final values, based on difference
// between how fast we're going vs how fast we plan to go
//
#define FANPITCHMIN 30
#define FANPITCHMAX 100
8 years ago
void CFuncRotating::RampPitchVol( int fUp )
9 years ago
{
Vector vecAVel = pev->avelocity;
vec_t vecCur;
vec_t vecFinal;
float fpct;
float fvol;
float fpitch;
int pitch;
8 years ago
9 years ago
// get current angular velocity
8 years ago
vecCur = fabs( vecAVel.x != 0 ? vecAVel.x : ( vecAVel.y != 0 ? vecAVel.y : vecAVel.z ) );
9 years ago
// get target angular velocity
8 years ago
vecFinal = ( pev->movedir.x != 0 ? pev->movedir.x : ( pev->movedir.y != 0 ? pev->movedir.y : pev->movedir.z ) );
9 years ago
vecFinal *= pev->speed;
8 years ago
vecFinal = fabs( vecFinal );
9 years ago
// calc volume and pitch as % of final vol and pitch
fpct = vecCur / vecFinal;
8 years ago
//if (fUp)
// fvol = m_flVolume * (0.5 + fpct/2.0); // spinup volume ramps up from 50% max vol
//else
9 years ago
fvol = m_flVolume * fpct; // slowdown volume ramps down to 0
8 years ago
fpitch = FANPITCHMIN + ( FANPITCHMAX - FANPITCHMIN ) * fpct;
pitch = (int)fpitch;
if( pitch == PITCH_NORM )
pitch = PITCH_NORM - 1;
9 years ago
// change the fan's vol and pitch
8 years ago
EMIT_SOUND_DYN( ENT( pev ), CHAN_STATIC, (char *)STRING( pev->noiseRunning ),
fvol, m_flAttenuation, SND_CHANGE_PITCH | SND_CHANGE_VOL, pitch );
9 years ago
}
//
// SpinUp - accelerates a non-moving func_rotating up to it's speed
//
8 years ago
void CFuncRotating::SpinUp( void )
9 years ago
{
Vector vecAVel;//rotational velocity
pev->nextthink = pev->ltime + 0.1;
pev->avelocity = pev->avelocity + ( pev->movedir * ( pev->speed * m_flFanFriction ) );
vecAVel = pev->avelocity;// cache entity's rotational velocity
// if we've met or exceeded target speed, set target speed and stop thinking
if( fabs( vecAVel.x ) >= fabs( pev->movedir.x * pev->speed ) &&
fabs( vecAVel.y ) >= fabs( pev->movedir.y * pev->speed ) &&
fabs( vecAVel.z ) >= fabs( pev->movedir.z * pev->speed ) )
9 years ago
{
pev->avelocity = pev->movedir * pev->speed;// set speed in case we overshot
8 years ago
EMIT_SOUND_DYN( ENT( pev ), CHAN_STATIC, (char *)STRING( pev->noiseRunning ),
m_flVolume, m_flAttenuation, SND_CHANGE_PITCH | SND_CHANGE_VOL, FANPITCHMAX );
9 years ago
SetThink( &CFuncRotating::Rotate );
Rotate();
}
else
{
8 years ago
RampPitchVol( TRUE );
9 years ago
}
}
//
// SpinDown - decelerates a moving func_rotating to a standstill.
//
8 years ago
void CFuncRotating::SpinDown( void )
9 years ago
{
8 years ago
Vector vecAVel;//rotational velocity
9 years ago
vec_t vecdir;
pev->nextthink = pev->ltime + 0.1;
pev->avelocity = pev->avelocity - ( pev->movedir * ( pev->speed * m_flFanFriction ) );//spin down slower than spinup
vecAVel = pev->avelocity;// cache entity's rotational velocity
8 years ago
if( pev->movedir.x != 0 )
9 years ago
vecdir = pev->movedir.x;
8 years ago
else if( pev->movedir.y != 0 )
9 years ago
vecdir = pev->movedir.y;
else
vecdir = pev->movedir.z;
// if we've met or exceeded target speed, set target speed and stop thinking
// (note: must check for movedir > 0 or < 0)
8 years ago
if( ( ( vecdir > 0 ) && ( vecAVel.x <= 0 && vecAVel.y <= 0 && vecAVel.z <= 0 ) ) ||
( ( vecdir < 0 ) && ( vecAVel.x >= 0 && vecAVel.y >= 0 && vecAVel.z >= 0 ) ) )
9 years ago
{
pev->avelocity = g_vecZero;// set speed in case we overshot
8 years ago
9 years ago
// stop sound, we're done
8 years ago
EMIT_SOUND_DYN( ENT( pev ), CHAN_STATIC, (char *)STRING( pev->noiseRunning /* Stop */ ),
0, 0, SND_STOP, (int)m_pitch );
9 years ago
SetThink( &CFuncRotating::Rotate );
Rotate();
}
else
{
8 years ago
RampPitchVol( FALSE );
9 years ago
}
}
8 years ago
void CFuncRotating::Rotate( void )
9 years ago
{
pev->nextthink = pev->ltime + 10;
}
//=========================================================
// Rotating Use - when a rotating brush is triggered
//=========================================================
8 years ago
void CFuncRotating::RotatingUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
9 years ago
{
// is this a brush that should accelerate and decelerate when turned on/off (fan)?
8 years ago
if( FBitSet ( pev->spawnflags, SF_BRUSH_ACCDCC ) )
9 years ago
{
// fan is spinning, so stop it.
8 years ago
if( pev->avelocity != g_vecZero )
9 years ago
{
SetThink( &CFuncRotating::SpinDown );
8 years ago
//EMIT_SOUND_DYN( ENT( pev ), CHAN_WEAPON, (char *)STRING( pev->noiseStop ),
// m_flVolume, m_flAttenuation, 0, m_pitch );
9 years ago
pev->nextthink = pev->ltime + 0.1;
}
else// fan is not moving, so start it
{
SetThink( &CFuncRotating::SpinUp );
8 years ago
EMIT_SOUND_DYN( ENT( pev ), CHAN_STATIC, (char *)STRING( pev->noiseRunning ),
0.01, m_flAttenuation, 0, FANPITCHMIN );
9 years ago
pev->nextthink = pev->ltime + 0.1;
}
}
8 years ago
else if( !FBitSet( pev->spawnflags, SF_BRUSH_ACCDCC ) )//this is a normal start/stop brush.
9 years ago
{
8 years ago
if( pev->avelocity != g_vecZero )
9 years ago
{
// play stopping sound here
SetThink( &CFuncRotating::SpinDown );
8 years ago
// EMIT_SOUND_DYN( ENT( pev ), CHAN_WEAPON, (char *)STRING( pev->noiseStop ),
// m_flVolume, m_flAttenuation, 0, m_pitch );
9 years ago
pev->nextthink = pev->ltime + 0.1;
// pev->avelocity = g_vecZero;
}
else
{
8 years ago
EMIT_SOUND_DYN( ENT( pev ), CHAN_STATIC, (char *)STRING( pev->noiseRunning ),
m_flVolume, m_flAttenuation, 0, FANPITCHMAX );
9 years ago
pev->avelocity = pev->movedir * pev->speed;
SetThink( &CFuncRotating::Rotate );
Rotate();
}
}
}
//
// RotatingBlocked - An entity has blocked the brush
//
8 years ago
void CFuncRotating::Blocked( CBaseEntity *pOther )
9 years ago
{
8 years ago
pOther->TakeDamage( pev, pev, pev->dmg, DMG_CRUSH );
9 years ago
}
//#endif
class CPendulum : public CBaseEntity
{
public:
8 years ago
void Spawn( void );
void KeyValue( KeyValueData *pkvd );
void EXPORT Swing( void );
void EXPORT PendulumUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
void EXPORT Stop( void );
void Touch( CBaseEntity *pOther );
void EXPORT RopeTouch( CBaseEntity *pOther );// this touch func makes the pendulum a rope
virtual int ObjectCaps( void ) { return CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; }
virtual int Save( CSave &save );
virtual int Restore( CRestore &restore );
void Blocked( CBaseEntity *pOther );
static TYPEDESCRIPTION m_SaveData[];
float m_accel; // Acceleration
float m_distance;
float m_time;
float m_damp;
float m_maxSpeed;
float m_dampSpeed;
vec3_t m_center;
vec3_t m_start;
9 years ago
};
LINK_ENTITY_TO_CLASS( func_pendulum, CPendulum )
9 years ago
TYPEDESCRIPTION CPendulum::m_SaveData[] =
9 years ago
{
DEFINE_FIELD( CPendulum, m_accel, FIELD_FLOAT ),
DEFINE_FIELD( CPendulum, m_distance, FIELD_FLOAT ),
DEFINE_FIELD( CPendulum, m_time, FIELD_TIME ),
DEFINE_FIELD( CPendulum, m_damp, FIELD_FLOAT ),
DEFINE_FIELD( CPendulum, m_maxSpeed, FIELD_FLOAT ),
DEFINE_FIELD( CPendulum, m_dampSpeed, FIELD_FLOAT ),
DEFINE_FIELD( CPendulum, m_center, FIELD_VECTOR ),
DEFINE_FIELD( CPendulum, m_start, FIELD_VECTOR ),
};
IMPLEMENT_SAVERESTORE( CPendulum, CBaseEntity )
9 years ago
8 years ago
void CPendulum::KeyValue( KeyValueData *pkvd )
9 years ago
{
8 years ago
if( FStrEq( pkvd->szKeyName, "distance" ) )
9 years ago
{
8 years ago
m_distance = atof( pkvd->szValue );
9 years ago
pkvd->fHandled = TRUE;
}
8 years ago
else if( FStrEq( pkvd->szKeyName, "damp" ) )
9 years ago
{
8 years ago
m_damp = atof( pkvd->szValue ) * 0.001;
9 years ago
pkvd->fHandled = TRUE;
}
else
CBaseEntity::KeyValue( pkvd );
}
8 years ago
void CPendulum::Spawn( void )
9 years ago
{
// set the axis of rotation
8 years ago
CBaseToggle::AxisDir( pev );
9 years ago
8 years ago
if( FBitSet( pev->spawnflags, SF_DOOR_PASSABLE ) )
pev->solid = SOLID_NOT;
9 years ago
else
8 years ago
pev->solid = SOLID_BSP;
pev->movetype = MOVETYPE_PUSH;
UTIL_SetOrigin( pev, pev->origin );
SET_MODEL( ENT( pev ), STRING( pev->model ) );
9 years ago
8 years ago
if( m_distance == 0 )
9 years ago
return;
8 years ago
if( pev->speed == 0 )
9 years ago
pev->speed = 100;
8 years ago
m_accel = ( pev->speed * pev->speed ) / ( 2 * fabs( m_distance ) ); // Calculate constant acceleration from speed and distance
9 years ago
m_maxSpeed = pev->speed;
m_start = pev->angles;
8 years ago
m_center = pev->angles + ( m_distance * 0.5 ) * pev->movedir;
9 years ago
8 years ago
if( FBitSet( pev->spawnflags, SF_BRUSH_ROTATE_INSTANT ) )
{
9 years ago
SetThink( &CBaseEntity::SUB_CallUseToggle );
pev->nextthink = gpGlobals->time + 0.1;
}
pev->speed = 0;
SetUse( &CPendulum::PendulumUse );
8 years ago
if( FBitSet( pev->spawnflags, SF_PENDULUM_SWING ) )
9 years ago
{
SetTouch( &CPendulum::RopeTouch );
}
}
8 years ago
void CPendulum::PendulumUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
9 years ago
{
8 years ago
if( pev->speed ) // Pendulum is moving, stop it and auto-return if necessary
9 years ago
{
8 years ago
if( FBitSet( pev->spawnflags, SF_PENDULUM_AUTO_RETURN ) )
9 years ago
{
8 years ago
float delta;
9 years ago
8 years ago
delta = CBaseToggle::AxisDelta( pev->spawnflags, pev->angles, m_start );
9 years ago
pev->avelocity = m_maxSpeed * pev->movedir;
8 years ago
pev->nextthink = pev->ltime + ( delta / m_maxSpeed );
9 years ago
SetThink( &CPendulum::Stop );
}
else
{
pev->speed = 0; // Dead stop
SetThink( NULL );
pev->avelocity = g_vecZero;
}
}
else
{
pev->nextthink = pev->ltime + 0.1; // Start the pendulum moving
m_time = gpGlobals->time; // Save time to calculate dt
SetThink( &CPendulum::Swing );
m_dampSpeed = m_maxSpeed;
}
}
8 years ago
void CPendulum::Stop( void )
9 years ago
{
pev->angles = m_start;
pev->speed = 0;
SetThink( NULL );
pev->avelocity = g_vecZero;
}
void CPendulum::Blocked( CBaseEntity *pOther )
{
m_time = gpGlobals->time;
}
8 years ago
void CPendulum::Swing( void )
9 years ago
{
float delta, dt;
8 years ago
delta = CBaseToggle::AxisDelta( pev->spawnflags, pev->angles, m_center );
9 years ago
dt = gpGlobals->time - m_time; // How much time has passed?
m_time = gpGlobals->time; // Remember the last time called
8 years ago
if( delta > 0 && m_accel > 0 )
9 years ago
pev->speed -= m_accel * dt; // Integrate velocity
else
pev->speed += m_accel * dt;
8 years ago
if( pev->speed > m_maxSpeed )
9 years ago
pev->speed = m_maxSpeed;
8 years ago
else if( pev->speed < -m_maxSpeed )
9 years ago
pev->speed = -m_maxSpeed;
// scale the destdelta vector by the time spent traveling to get velocity
pev->avelocity = pev->speed * pev->movedir;
// Call this again
pev->nextthink = pev->ltime + 0.1;
8 years ago
if( m_damp )
9 years ago
{
m_dampSpeed -= m_damp * m_dampSpeed * dt;
8 years ago
if( m_dampSpeed < 30.0 )
9 years ago
{
pev->angles = m_center;
pev->speed = 0;
SetThink( NULL );
pev->avelocity = g_vecZero;
}
8 years ago
else if( pev->speed > m_dampSpeed )
9 years ago
pev->speed = m_dampSpeed;
8 years ago
else if( pev->speed < -m_dampSpeed )
9 years ago
pev->speed = -m_dampSpeed;
}
}
8 years ago
void CPendulum::Touch( CBaseEntity *pOther )
9 years ago
{
8 years ago
entvars_t *pevOther = pOther->pev;
9 years ago
8 years ago
if( pev->dmg <= 0 )
9 years ago
return;
// we can't hurt this thing, so we're not concerned with it
8 years ago
if( !pevOther->takedamage )
9 years ago
return;
// calculate damage based on rotation speed
float damage = pev->dmg * pev->speed * 0.01;
8 years ago
if( damage < 0 )
9 years ago
damage = -damage;
pOther->TakeDamage( pev, pev, damage, DMG_CRUSH );
8 years ago
pevOther->velocity = ( pevOther->origin - VecBModelOrigin( pev ) ).Normalize() * damage;
9 years ago
}
8 years ago
void CPendulum::RopeTouch( CBaseEntity *pOther )
9 years ago
{
8 years ago
entvars_t *pevOther = pOther->pev;
9 years ago
8 years ago
if( !pOther->IsPlayer() )
{
// not a player!
ALERT( at_console, "Not a client\n" );
9 years ago
return;
}
8 years ago
if( ENT( pevOther ) == pev->enemy )
{
// this player already on the rope.
9 years ago
return;
}
pev->enemy = pOther->edict();
pevOther->velocity = g_vecZero;
pevOther->movetype = MOVETYPE_NONE;
}