2
0
mirror of https://github.com/FWGS/hlsdk-xash3d synced 2024-11-25 03:09:24 +01:00
hlsdk-xash3d/dlls/handgrenade.cpp

232 lines
5.6 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"
#define HANDGRENADE_PRIMARY_VOLUME 450
2016-07-31 15:48:50 +02:00
enum handgrenade_e
{
2016-06-04 15:24:23 +02:00
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 )
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
void CHandGrenade::Spawn()
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
Precache();
2016-06-04 15:24:23 +02:00
m_iId = WEAPON_HANDGRENADE;
2016-07-31 15:48:50 +02:00
SET_MODEL( ENT( pev ), "models/w_grenade.mdl" );
2016-06-04 15:24:23 +02:00
2021-06-07 02:05:58 +02:00
#if !CLIENT_DLL
2016-06-04 15:24:23 +02:00
pev->dmg = gSkillData.plrDmgHandGrenade;
#endif
m_iDefaultAmmo = HANDGRENADE_DEFAULT_GIVE;
FallInit();// get ready to fall down.
}
void CHandGrenade::Precache( void )
{
2016-07-31 15:48:50 +02:00
PRECACHE_MODEL( "models/w_grenade.mdl" );
PRECACHE_MODEL( "models/v_grenade.mdl" );
PRECACHE_MODEL( "models/p_grenade.mdl" );
2016-06-04 15:24:23 +02:00
}
2016-07-31 15:48:50 +02:00
int CHandGrenade::GetItemInfo( ItemInfo *p )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
p->pszName = STRING( pev->classname );
2016-06-04 15:24:23 +02:00
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;
}
2016-07-31 15:48:50 +02:00
BOOL CHandGrenade::Deploy()
2016-06-04 15:24:23 +02:00
{
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 );
}
void CHandGrenade::Holster( int skiplocal /* = 0 */ )
{
2019-10-13 13:49:25 +02:00
m_pPlayer->m_flNextAttack = UTIL_WeaponTimeBase() + 0.5f;
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
if( m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] )
2016-06-04 15:24:23 +02:00
{
SendWeaponAnim( HANDGRENADE_HOLSTER );
}
else
{
// no more grenades!
2016-07-31 15:48:50 +02:00
m_pPlayer->pev->weapons &= ~( 1 << WEAPON_HANDGRENADE );
DestroyItem();
2016-06-04 15:24:23 +02:00
}
if( m_flStartThrow )
{
2019-10-13 13:49:25 +02:00
m_flStartThrow = 0.0f;
m_flReleaseThrow = 0.0f;
}
2019-10-13 13:49:25 +02:00
EMIT_SOUND( ENT( m_pPlayer->pev ), CHAN_WEAPON, "common/null.wav", 1.0f, ATTN_NORM );
2016-06-04 15:24:23 +02:00
}
void CHandGrenade::PrimaryAttack()
{
2016-07-31 15:48:50 +02:00
if( !m_flStartThrow && m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] > 0 )
2016-06-04 15:24:23 +02:00
{
m_flStartThrow = gpGlobals->time;
2019-10-13 13:49:25 +02:00
m_flReleaseThrow = 0.0f;
2016-06-04 15:24:23 +02:00
SendWeaponAnim( HANDGRENADE_PINPULL );
2019-10-13 13:49:25 +02:00
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 0.5f;
2016-06-04 15:24:23 +02:00
}
}
void CHandGrenade::WeaponIdle( void )
{
2019-10-13 13:49:25 +02:00
if( m_flReleaseThrow == 0.0f && m_flStartThrow )
2016-06-04 15:24:23 +02:00
m_flReleaseThrow = gpGlobals->time;
2016-07-31 15:48:50 +02:00
if( m_flTimeWeaponIdle > UTIL_WeaponTimeBase() )
2016-06-04 15:24:23 +02:00
return;
2016-07-31 15:48:50 +02:00
if( m_flStartThrow )
2016-06-04 15:24:23 +02:00
{
Vector angThrow = m_pPlayer->pev->v_angle + m_pPlayer->pev->punchangle;
2019-10-13 13:49:25 +02:00
if( angThrow.x < 0.0f )
angThrow.x = -10.0f + angThrow.x * ( ( 90.0f - 10.0f ) / 90.0f );
2016-06-04 15:24:23 +02:00
else
2019-10-13 13:49:25 +02:00
angThrow.x = -10.0f + angThrow.x * ( ( 90.0f + 10.0f ) / 90.0f );
2016-06-04 15:24:23 +02:00
2024-01-17 10:14:47 +01:00
float flVel = ( 90.0f - angThrow.x ) * 6.5f;
if( flVel > 1000.0f )
flVel = 1000.0f;
2016-06-04 15:24:23 +02:00
UTIL_MakeVectors( angThrow );
2019-10-13 13:49:25 +02:00
Vector vecSrc = m_pPlayer->pev->origin + m_pPlayer->pev->view_ofs + gpGlobals->v_forward * 16.0f;
2016-06-04 15:24:23 +02:00
Vector vecThrow = gpGlobals->v_forward * flVel + m_pPlayer->pev->velocity;
// alway explode 3 seconds after the pin was pulled
2019-10-13 13:49:25 +02:00
float time = m_flStartThrow - gpGlobals->time + 3.0f;
if( time < 0.0f )
time = 0.0f;
2016-06-04 15:24:23 +02:00
CGrenade::ShootTimed( m_pPlayer->pev, vecSrc, vecThrow, time );
2019-10-13 13:49:25 +02:00
if( flVel < 500.0f )
2016-06-04 15:24:23 +02:00
{
SendWeaponAnim( HANDGRENADE_THROW1 );
}
2019-10-13 13:49:25 +02:00
else if( flVel < 1000.0f )
2016-06-04 15:24:23 +02:00
{
SendWeaponAnim( HANDGRENADE_THROW2 );
}
else
{
SendWeaponAnim( HANDGRENADE_THROW3 );
}
// player "shoot" animation
m_pPlayer->SetAnimation( PLAYER_ATTACK1 );
#if !HANDGRENADE_DEPLOY_FIX
2019-10-13 13:49:25 +02:00
m_flReleaseThrow = 0.0f;
#endif
2019-10-13 13:49:25 +02:00
m_flStartThrow = 0.0f;
m_flNextPrimaryAttack = GetNextAttackDelay( 0.5f );
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 0.5f;
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType]--;
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
if( !m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] )
2016-06-04 15:24:23 +02: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.
2019-10-13 13:49:25 +02:00
m_flTimeWeaponIdle = m_flNextSecondaryAttack = m_flNextPrimaryAttack = GetNextAttackDelay( 0.5f );// ensure that the animation can finish playing
2016-06-04 15:24:23 +02:00
}
return;
}
2019-10-13 13:49:25 +02:00
else if( m_flReleaseThrow > 0.0f )
2016-06-04 15:24:23 +02:00
{
// we've finished the throw, restart.
2019-10-13 13:49:25 +02:00
m_flStartThrow = 0.0f;
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
if( m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] )
2016-06-04 15:24:23 +02:00
{
SendWeaponAnim( HANDGRENADE_DRAW );
}
else
{
RetireWeapon();
return;
}
2019-10-13 13:49:25 +02:00
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + UTIL_SharedRandomFloat( m_pPlayer->random_seed, 10.0f, 15.0f );
m_flReleaseThrow = -1.0f;
2016-06-04 15:24:23 +02:00
return;
}
2016-07-31 15:48:50 +02:00
if( m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] )
2016-06-04 15:24:23 +02:00
{
int iAnim;
2019-10-13 13:49:25 +02:00
float flRand = UTIL_SharedRandomFloat( m_pPlayer->random_seed, 0.0f, 1.0f );
if( flRand <= 0.75f )
2016-06-04 15:24:23 +02:00
{
iAnim = HANDGRENADE_IDLE;
2019-10-13 13:49:25 +02:00
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + UTIL_SharedRandomFloat( m_pPlayer->random_seed, 10.0f, 15.0f );// how long till we do this again.
2016-06-04 15:24:23 +02:00
}
2016-07-31 15:48:50 +02:00
else
2016-06-04 15:24:23 +02:00
{
iAnim = HANDGRENADE_FIDGET;
2019-10-13 13:49:25 +02:00
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 75.0f / 30.0f;
2016-06-04 15:24:23 +02:00
}
SendWeaponAnim( iAnim );
}
}