hlsdk-xash3d/dlls/healthkit.cpp

255 lines
6.1 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.
*
****/
2016-06-04 15:24:23 +02:00
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "monsters.h"
#include "weapons.h"
#include "nodes.h"
#include "player.h"
#include "items.h"
#include "gamerules.h"
#include "game.h"
2016-06-04 15:24:23 +02:00
extern int gmsgItemPickup;
class CHealthKit : public CItem
{
void Spawn( void );
void Precache( void );
BOOL MyTouch( CBasePlayer *pPlayer );
/*
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
*/
};
LINK_ENTITY_TO_CLASS( item_healthkit, CHealthKit )
2016-06-04 15:24:23 +02:00
/*
TYPEDESCRIPTION CHealthKit::m_SaveData[] =
2016-06-04 15:24:23 +02:00
{
};
2016-07-31 15:48:50 +02:00
IMPLEMENT_SAVERESTORE( CHealthKit, CItem )
2016-06-04 15:24:23 +02:00
*/
2016-07-31 15:48:50 +02:00
void CHealthKit::Spawn( void )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
Precache();
SET_MODEL( ENT( pev ), "models/w_medkit.mdl" );
2016-06-04 15:24:23 +02:00
CItem::Spawn();
}
void CHealthKit::Precache( void )
{
2016-07-31 15:48:50 +02:00
PRECACHE_MODEL( "models/w_medkit.mdl" );
PRECACHE_SOUND( "items/smallmedkit1.wav" );
2016-06-04 15:24:23 +02:00
}
BOOL CHealthKit::MyTouch( CBasePlayer *pPlayer )
{
2016-07-31 15:48:50 +02:00
if( pPlayer->pev->deadflag != DEAD_NO )
2016-06-04 15:24:23 +02:00
{
return FALSE;
}
2016-07-31 15:48:50 +02:00
if( pPlayer->TakeHealth( gSkillData.healthkitCapacity, DMG_GENERIC ) )
2016-06-04 15:24:23 +02:00
{
MESSAGE_BEGIN( MSG_ONE, gmsgItemPickup, NULL, pPlayer->pev );
2016-07-31 15:48:50 +02:00
WRITE_STRING( STRING( pev->classname ) );
2016-06-04 15:24:23 +02:00
MESSAGE_END();
2016-07-31 15:48:50 +02:00
EMIT_SOUND( ENT( pPlayer->pev ), CHAN_ITEM, "items/smallmedkit1.wav", 1, ATTN_NORM );
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
if( g_pGameRules->ItemShouldRespawn( this ) )
2016-06-04 15:24:23 +02:00
{
Respawn();
}
else
{
2016-07-31 15:48:50 +02:00
UTIL_Remove( this );
2016-06-04 15:24:23 +02:00
}
return TRUE;
}
return FALSE;
}
//-------------------------------------------------------------
// Wall mounted health kit
//-------------------------------------------------------------
class CWallHealth : public CBaseToggle
{
public:
2016-07-31 15:48:50 +02:00
void Spawn();
2016-06-04 15:24:23 +02:00
void Precache( void );
2016-07-31 15:48:50 +02:00
void EXPORT Off( void );
void EXPORT Recharge( 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 ObjectCaps( void ) { return ( CBaseToggle::ObjectCaps() | FCAP_CONTINUOUS_USE ) & ~FCAP_ACROSS_TRANSITION; }
virtual int Save( CSave &save );
virtual int Restore( CRestore &restore );
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
static TYPEDESCRIPTION m_SaveData[];
2016-06-04 15:24:23 +02:00
float m_flNextCharge;
2016-07-31 15:48:50 +02:00
int m_iReactivate ; // DeathMatch Delay until reactvated
int m_iJuice;
int m_iOn; // 0 = off, 1 = startup, 2 = going
float m_flSoundTime;
2016-06-04 15:24:23 +02:00
};
TYPEDESCRIPTION CWallHealth::m_SaveData[] =
{
2016-07-31 15:48:50 +02:00
DEFINE_FIELD( CWallHealth, m_flNextCharge, FIELD_TIME ),
DEFINE_FIELD( CWallHealth, m_iReactivate, FIELD_INTEGER ),
DEFINE_FIELD( CWallHealth, m_iJuice, FIELD_INTEGER ),
DEFINE_FIELD( CWallHealth, m_iOn, FIELD_INTEGER ),
DEFINE_FIELD( CWallHealth, m_flSoundTime, FIELD_TIME ),
2016-06-04 15:24:23 +02:00
};
IMPLEMENT_SAVERESTORE( CWallHealth, CBaseEntity )
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
LINK_ENTITY_TO_CLASS( func_healthcharger, CWallHealth )
2016-06-04 15:24:23 +02:00
void CWallHealth::KeyValue( KeyValueData *pkvd )
{
2016-07-31 15:48:50 +02:00
if( FStrEq(pkvd->szKeyName, "style" ) ||
FStrEq( pkvd->szKeyName, "height" ) ||
FStrEq( pkvd->szKeyName, "value1" ) ||
FStrEq( pkvd->szKeyName, "value2" ) ||
FStrEq( pkvd->szKeyName, "value3" ) )
2016-06-04 15:24:23 +02:00
{
pkvd->fHandled = TRUE;
}
2016-07-31 15:48:50 +02:00
else if( FStrEq( pkvd->szKeyName, "dmdelay" ) )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
m_iReactivate = atoi( pkvd->szValue );
2016-06-04 15:24:23 +02:00
pkvd->fHandled = TRUE;
}
else
CBaseToggle::KeyValue( pkvd );
}
void CWallHealth::Spawn()
{
2016-07-31 15:48:50 +02:00
Precache();
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
pev->solid = SOLID_BSP;
pev->movetype = MOVETYPE_PUSH;
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
UTIL_SetOrigin( pev, pev->origin ); // set size and link into world
UTIL_SetSize( pev, pev->mins, pev->maxs );
SET_MODEL( ENT( pev ), STRING( pev->model ) );
2017-06-29 15:56:03 +02:00
m_iJuice = (int)gSkillData.healthchargerCapacity;
pev->frame = 0;
2016-06-04 15:24:23 +02:00
}
void CWallHealth::Precache()
{
2016-07-31 15:48:50 +02:00
PRECACHE_SOUND( "items/medshot4.wav" );
PRECACHE_SOUND( "items/medshotno1.wav" );
PRECACHE_SOUND( "items/medcharge4.wav" );
2016-06-04 15:24:23 +02:00
}
void CWallHealth::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
{
// Make sure that we have a caller
2016-07-31 15:48:50 +02:00
if( !pActivator )
2016-06-04 15:24:23 +02:00
return;
// if it's not a player, ignore
2016-07-31 15:48:50 +02:00
if( !pActivator->IsPlayer() )
2016-06-04 15:24:23 +02:00
return;
// if there is no juice left, turn it off
2016-07-31 15:48:50 +02:00
if( m_iJuice <= 0 )
2016-06-04 15:24:23 +02:00
{
pev->frame = 1;
Off();
}
// if the player doesn't have the suit, or there is no juice left, make the deny noise
2019-07-07 17:08:01 +02:00
if( ( m_iJuice <= 0 ) || ( !( pActivator->pev->weapons & ( 1 << WEAPON_SUIT ) ) ) || ( ( chargerfix.value ) && ( pActivator->pev->health >= pActivator->pev->max_health ) ) )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
if( m_flSoundTime <= gpGlobals->time )
2016-06-04 15:24:23 +02:00
{
2019-10-13 13:49:25 +02:00
m_flSoundTime = gpGlobals->time + 0.62f;
2016-07-31 15:48:50 +02:00
EMIT_SOUND( ENT( pev ), CHAN_ITEM, "items/medshotno1.wav", 1.0, ATTN_NORM );
2016-06-04 15:24:23 +02:00
}
return;
}
2019-10-13 13:49:25 +02:00
pev->nextthink = pev->ltime + 0.25f;
2016-06-04 15:24:23 +02:00
SetThink( &CWallHealth::Off );
// Time to recharge yet?
2016-07-31 15:48:50 +02:00
if( m_flNextCharge >= gpGlobals->time )
2016-06-04 15:24:23 +02:00
return;
// Play the on sound or the looping charging sound
2016-07-31 15:48:50 +02:00
if( !m_iOn )
2016-06-04 15:24:23 +02:00
{
m_iOn++;
2016-07-31 15:48:50 +02:00
EMIT_SOUND( ENT( pev ), CHAN_ITEM, "items/medshot4.wav", 1.0, ATTN_NORM );
2019-10-13 13:49:25 +02:00
m_flSoundTime = 0.56f + gpGlobals->time;
2016-06-04 15:24:23 +02:00
}
2016-07-31 15:48:50 +02:00
if( ( m_iOn == 1 ) && ( m_flSoundTime <= gpGlobals->time ) )
2016-06-04 15:24:23 +02:00
{
m_iOn++;
2016-07-31 15:48:50 +02:00
EMIT_SOUND( ENT( pev ), CHAN_STATIC, "items/medcharge4.wav", 1.0, ATTN_NORM );
2016-06-04 15:24:23 +02:00
}
// charge the player
2016-07-31 15:48:50 +02:00
if( pActivator->TakeHealth( 1, DMG_GENERIC ) )
2016-06-04 15:24:23 +02:00
{
m_iJuice--;
}
// govern the rate of charge
2019-10-13 13:49:25 +02:00
m_flNextCharge = gpGlobals->time + 0.1f;
2016-06-04 15:24:23 +02:00
}
2016-07-31 15:48:50 +02:00
void CWallHealth::Recharge( void )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
EMIT_SOUND( ENT( pev ), CHAN_ITEM, "items/medshot4.wav", 1.0, ATTN_NORM );
2017-06-29 15:56:03 +02:00
m_iJuice = (int)gSkillData.healthchargerCapacity;
2016-06-04 15:24:23 +02:00
pev->frame = 0;
SetThink( &CBaseEntity::SUB_DoNothing );
}
2016-07-31 15:48:50 +02:00
void CWallHealth::Off( void )
2016-06-04 15:24:23 +02:00
{
// Stop looping sound.
2016-07-31 15:48:50 +02:00
if( m_iOn > 1 )
STOP_SOUND( ENT( pev ), CHAN_STATIC, "items/medcharge4.wav" );
2016-06-04 15:24:23 +02:00
m_iOn = 0;
2017-06-29 15:56:03 +02:00
if( ( !m_iJuice ) && ( ( m_iReactivate = (int)g_pGameRules->FlHealthChargerRechargeTime() ) > 0 ) )
2016-06-04 15:24:23 +02:00
{
pev->nextthink = pev->ltime + m_iReactivate;
SetThink( &CWallHealth::Recharge );
}
else
SetThink( &CBaseEntity::SUB_DoNothing );
2016-04-17 22:30:05 +02:00
}