1
0
Fork 0
forked from valve/halflife-sdk
halflife-sdk-steam/dlls/handgrenade.cpp

234 lines
5.7 KiB
C++
Raw Normal View History

1999-04-07 00:00:00 +00:00
/***
*
* Copyright (c) 1996-2001, Valve LLC. All rights reserved.
1999-04-07 00:00:00 +00:00
*
* 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.
*
****/
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "monsters.h"
#include "weapons.h"
#include "nodes.h"
#include "player.h"
#define HANDGRENADE_PRIMARY_VOLUME 450
enum handgrenade_e {
HANDGRENADE_IDLE = 0,
HANDGRENADE_FIDGET,
HANDGRENADE_PINPULL,
HANDGRENADE_THROW1, // toss
HANDGRENADE_THROW2, // medium
HANDGRENADE_THROW3, // hard
HANDGRENADE_HOLSTER,
HANDGRENADE_DRAW
};
LINK_ENTITY_TO_CLASS( weapon_handgrenade, CHandGrenade );
void CHandGrenade::Spawn( )
{
Precache( );
m_iId = WEAPON_HANDGRENADE;
SET_MODEL(ENT(pev), "models/w_grenade.mdl");
2001-08-29 00:00:00 +00:00
#ifndef CLIENT_DLL
1999-04-07 00:00:00 +00:00
pev->dmg = gSkillData.plrDmgHandGrenade;
2001-08-29 00:00:00 +00:00
#endif
1999-04-07 00:00:00 +00:00
m_iDefaultAmmo = HANDGRENADE_DEFAULT_GIVE;
FallInit();// get ready to fall down.
}
void CHandGrenade::Precache( void )
{
PRECACHE_MODEL("models/w_grenade.mdl");
PRECACHE_MODEL("models/v_grenade.mdl");
PRECACHE_MODEL("models/p_grenade.mdl");
}
int CHandGrenade::GetItemInfo(ItemInfo *p)
{
p->pszName = STRING(pev->classname);
p->pszAmmo1 = "Hand Grenade";
p->iMaxAmmo1 = HANDGRENADE_MAX_CARRY;
p->pszAmmo2 = NULL;
p->iMaxAmmo2 = -1;
p->iMaxClip = WEAPON_NOCLIP;
p->iSlot = 4;
p->iPosition = 0;
p->iId = m_iId = WEAPON_HANDGRENADE;
p->iWeight = HANDGRENADE_WEIGHT;
p->iFlags = ITEM_FLAG_LIMITINWORLD | ITEM_FLAG_EXHAUSTIBLE;
return 1;
}
BOOL CHandGrenade::Deploy( )
{
m_flReleaseThrow = -1;
return DefaultDeploy( "models/v_grenade.mdl", "models/p_grenade.mdl", HANDGRENADE_DRAW, "crowbar" );
}
BOOL CHandGrenade::CanHolster( void )
{
// can only holster hand grenades when not primed!
return ( m_flStartThrow == 0 );
}
2000-06-05 00:00:00 +00:00
void CHandGrenade::Holster( int skiplocal /* = 0 */ )
1999-04-07 00:00:00 +00:00
{
2000-06-05 00:00:00 +00:00
m_pPlayer->m_flNextAttack = UTIL_WeaponTimeBase() + 0.5;
2001-08-29 00:00:00 +00:00
if ( m_pPlayer->m_rgAmmo[ m_iPrimaryAmmoType ] )
1999-04-07 00:00:00 +00:00
{
SendWeaponAnim( HANDGRENADE_HOLSTER );
}
else
{
// no more grenades!
m_pPlayer->pev->weapons &= ~(1<<WEAPON_HANDGRENADE);
SetThink( &CHandGrenade::DestroyItem );
1999-04-07 00:00:00 +00:00
pev->nextthink = gpGlobals->time + 0.1;
}
EMIT_SOUND(ENT(m_pPlayer->pev), CHAN_WEAPON, "common/null.wav", 1.0, ATTN_NORM);
}
void CHandGrenade::PrimaryAttack()
{
2001-08-29 00:00:00 +00:00
if ( !m_flStartThrow && m_pPlayer->m_rgAmmo[ m_iPrimaryAmmoType ] > 0 )
1999-04-07 00:00:00 +00:00
{
m_flStartThrow = gpGlobals->time;
m_flReleaseThrow = 0;
SendWeaponAnim( HANDGRENADE_PINPULL );
2001-08-29 00:00:00 +00:00
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 0.5;
1999-04-07 00:00:00 +00:00
}
}
void CHandGrenade::WeaponIdle( void )
{
2001-08-29 00:00:00 +00:00
if ( m_flReleaseThrow == 0 && m_flStartThrow )
m_flReleaseThrow = gpGlobals->time;
1999-04-07 00:00:00 +00:00
2001-08-29 00:00:00 +00:00
if ( m_flTimeWeaponIdle > UTIL_WeaponTimeBase() )
1999-04-07 00:00:00 +00:00
return;
2001-08-29 00:00:00 +00:00
if ( m_flStartThrow )
1999-04-07 00:00:00 +00:00
{
Vector angThrow = m_pPlayer->pev->v_angle + m_pPlayer->pev->punchangle;
2001-08-29 00:00:00 +00:00
if ( angThrow.x < 0 )
angThrow.x = -10 + angThrow.x * ( ( 90 - 10 ) / 90.0 );
1999-04-07 00:00:00 +00:00
else
2001-08-29 00:00:00 +00:00
angThrow.x = -10 + angThrow.x * ( ( 90 + 10 ) / 90.0 );
1999-04-07 00:00:00 +00:00
2001-08-29 00:00:00 +00:00
float flVel = ( 90 - angThrow.x ) * 4;
if ( flVel > 500 )
1999-04-07 00:00:00 +00:00
flVel = 500;
UTIL_MakeVectors( angThrow );
Vector vecSrc = m_pPlayer->pev->origin + m_pPlayer->pev->view_ofs + gpGlobals->v_forward * 16;
Vector vecThrow = gpGlobals->v_forward * flVel + m_pPlayer->pev->velocity;
// alway explode 3 seconds after the pin was pulled
float time = m_flStartThrow - gpGlobals->time + 3.0;
if (time < 0)
time = 0;
CGrenade::ShootTimed( m_pPlayer->pev, vecSrc, vecThrow, time );
2001-08-29 00:00:00 +00:00
if ( flVel < 500 )
1999-04-07 00:00:00 +00:00
{
SendWeaponAnim( HANDGRENADE_THROW1 );
}
2001-08-29 00:00:00 +00:00
else if ( flVel < 1000 )
1999-04-07 00:00:00 +00:00
{
SendWeaponAnim( HANDGRENADE_THROW2 );
}
else
{
SendWeaponAnim( HANDGRENADE_THROW3 );
}
// player "shoot" animation
m_pPlayer->SetAnimation( PLAYER_ATTACK1 );
2001-08-29 00:00:00 +00:00
m_flReleaseThrow = 0;
1999-04-07 00:00:00 +00:00
m_flStartThrow = 0;
m_flNextPrimaryAttack = GetNextAttackDelay(0.5);
2001-08-29 00:00:00 +00:00
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 0.5;
1999-04-07 00:00:00 +00:00
2001-08-29 00:00:00 +00:00
m_pPlayer->m_rgAmmo[ m_iPrimaryAmmoType ]--;
1999-04-07 00:00:00 +00:00
2001-08-29 00:00:00 +00:00
if ( !m_pPlayer->m_rgAmmo[ m_iPrimaryAmmoType ] )
1999-04-07 00:00:00 +00:00
{
// just threw last grenade
// set attack times in the future, and weapon idle in the future so we can see the whole throw
// animation, weapon idle will automatically retire the weapon for us.
m_flTimeWeaponIdle = m_flNextSecondaryAttack = m_flNextPrimaryAttack = GetNextAttackDelay(0.5);// ensure that the animation can finish playing
1999-04-07 00:00:00 +00:00
}
return;
}
2001-08-29 00:00:00 +00:00
else if ( m_flReleaseThrow > 0 )
1999-04-07 00:00:00 +00:00
{
// we've finished the throw, restart.
m_flStartThrow = 0;
2001-08-29 00:00:00 +00:00
if ( m_pPlayer->m_rgAmmo[ m_iPrimaryAmmoType ] )
1999-04-07 00:00:00 +00:00
{
SendWeaponAnim( HANDGRENADE_DRAW );
}
else
{
RetireWeapon();
return;
}
2001-08-29 00:00:00 +00:00
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + UTIL_SharedRandomFloat( m_pPlayer->random_seed, 10, 15 );
1999-04-07 00:00:00 +00:00
m_flReleaseThrow = -1;
return;
}
2001-08-29 00:00:00 +00:00
if ( m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] )
1999-04-07 00:00:00 +00:00
{
int iAnim;
2001-08-29 00:00:00 +00:00
float flRand = UTIL_SharedRandomFloat( m_pPlayer->random_seed, 0, 1 );
1999-04-07 00:00:00 +00:00
if (flRand <= 0.75)
{
iAnim = HANDGRENADE_IDLE;
2001-08-29 00:00:00 +00:00
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + UTIL_SharedRandomFloat( m_pPlayer->random_seed, 10, 15 );// how long till we do this again.
1999-04-07 00:00:00 +00:00
}
else
{
iAnim = HANDGRENADE_FIDGET;
2001-08-29 00:00:00 +00:00
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 75.0 / 30.0;
1999-04-07 00:00:00 +00:00
}
SendWeaponAnim( iAnim );
}
}