2
0
mirror of https://github.com/FWGS/hlsdk-xash3d synced 2024-11-22 09:57:21 +01:00
hlsdk-xash3d/dlls/shotgun.cpp

395 lines
10 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.
*
****/
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "monsters.h"
#include "weapons.h"
#include "nodes.h"
#include "player.h"
#include "gamerules.h"
// special deathmatch shotgun spreads
2016-07-31 15:48:50 +02:00
#define VECTOR_CONE_DM_SHOTGUN Vector( 0.08716, 0.04362, 0.00 )// 10 degrees by 5 degrees
2016-06-04 15:24:23 +02:00
#define VECTOR_CONE_DM_DOUBLESHOTGUN Vector( 0.17365, 0.04362, 0.00 ) // 20 degrees by 5 degrees
2016-07-31 15:48:50 +02:00
enum shotgun_e
{
2016-06-04 15:24:23 +02:00
SHOTGUN_IDLE = 0,
SHOTGUN_FIRE,
SHOTGUN_FIRE2,
SHOTGUN_RELOAD,
SHOTGUN_PUMP,
SHOTGUN_START_RELOAD,
SHOTGUN_DRAW,
SHOTGUN_HOLSTER,
SHOTGUN_IDLE4,
SHOTGUN_IDLE_DEEP
};
LINK_ENTITY_TO_CLASS( weapon_shotgun, CShotgun )
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
void CShotgun::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_SHOTGUN;
2016-07-31 15:48:50 +02:00
SET_MODEL( ENT( pev ), "models/w_shotgun.mdl" );
2016-06-04 15:24:23 +02:00
m_iDefaultAmmo = SHOTGUN_DEFAULT_GIVE;
FallInit();// get ready to fall
}
void CShotgun::Precache( void )
{
2016-07-31 15:48:50 +02:00
PRECACHE_MODEL( "models/v_shotgun.mdl" );
PRECACHE_MODEL( "models/w_shotgun.mdl" );
PRECACHE_MODEL( "models/p_shotgun.mdl" );
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
m_iShell = PRECACHE_MODEL( "models/shotgunshell.mdl" );// shotgun shell
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
PRECACHE_SOUND( "items/9mmclip1.wav" );
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
PRECACHE_SOUND( "weapons/dbarrel1.wav" );//shotgun
PRECACHE_SOUND( "weapons/sbarrel1.wav" );//shotgun
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
PRECACHE_SOUND( "weapons/reload1.wav" ); // shotgun reload
PRECACHE_SOUND( "weapons/reload3.wav" ); // shotgun reload
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
//PRECACHE_SOUND( "weapons/sshell1.wav" ); // shotgun reload - played on client
//PRECACHE_SOUND( "weapons/sshell3.wav" ); // shotgun reload - played on client
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
PRECACHE_SOUND( "weapons/357_cock1.wav" ); // gun empty sound
PRECACHE_SOUND( "weapons/scock1.wav" ); // cock gun
2016-06-04 15:24:23 +02:00
m_usSingleFire = PRECACHE_EVENT( 1, "events/shotgun1.sc" );
m_usDoubleFire = PRECACHE_EVENT( 1, "events/shotgun2.sc" );
}
int CShotgun::AddToPlayer( CBasePlayer *pPlayer )
{
2016-07-31 15:48:50 +02:00
if( CBasePlayerWeapon::AddToPlayer( pPlayer ) )
2016-06-04 15:24:23 +02:00
{
MESSAGE_BEGIN( MSG_ONE, gmsgWeapPickup, NULL, pPlayer->pev );
WRITE_BYTE( m_iId );
MESSAGE_END();
return TRUE;
}
return FALSE;
}
2016-07-31 15:48:50 +02:00
int CShotgun::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 = "buckshot";
p->iMaxAmmo1 = BUCKSHOT_MAX_CARRY;
p->pszAmmo2 = NULL;
p->iMaxAmmo2 = -1;
p->iMaxClip = SHOTGUN_MAX_CLIP;
p->iSlot = 2;
p->iPosition = 1;
p->iFlags = 0;
p->iId = m_iId = WEAPON_SHOTGUN;
p->iWeight = SHOTGUN_WEIGHT;
return 1;
}
2016-07-31 15:48:50 +02:00
BOOL CShotgun::Deploy()
2016-06-04 15:24:23 +02:00
{
return DefaultDeploy( "models/v_shotgun.mdl", "models/p_shotgun.mdl", SHOTGUN_DRAW, "shotgun" );
}
void CShotgun::PrimaryAttack()
{
// don't fire underwater
2016-07-31 15:48:50 +02:00
if( m_pPlayer->pev->waterlevel == 3 )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
PlayEmptySound();
2019-10-13 13:49:25 +02:00
m_flNextPrimaryAttack = GetNextAttackDelay( 0.15f );
2016-06-04 15:24:23 +02:00
return;
}
2016-07-31 15:48:50 +02:00
if( m_iClip <= 0 )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
Reload();
if( m_iClip == 0 )
PlayEmptySound();
2016-06-04 15:24:23 +02:00
return;
}
m_pPlayer->m_iWeaponVolume = LOUD_GUN_VOLUME;
m_pPlayer->m_iWeaponFlash = NORMAL_GUN_FLASH;
m_iClip--;
int flags;
2021-06-07 16:17:22 +02:00
#if CLIENT_WEAPONS
2016-06-04 15:24:23 +02:00
flags = FEV_NOTHOST;
#else
flags = 0;
#endif
2016-07-31 15:48:50 +02:00
m_pPlayer->pev->effects = (int)( m_pPlayer->pev->effects ) | EF_MUZZLEFLASH;
2016-06-04 15:24:23 +02:00
// player "shoot" animation
m_pPlayer->SetAnimation( PLAYER_ATTACK1 );
2016-07-31 15:48:50 +02:00
Vector vecSrc = m_pPlayer->GetGunPosition();
2016-06-04 15:24:23 +02:00
Vector vecAiming = m_pPlayer->GetAutoaimVector( AUTOAIM_5DEGREES );
Vector vecDir;
2021-06-07 02:05:58 +02:00
#if CLIENT_DLL
2016-07-31 15:48:50 +02:00
if( bIsMultiplayer() )
2016-06-04 15:24:23 +02:00
#else
2016-07-31 15:48:50 +02:00
if( g_pGameRules->IsMultiplayer() )
2016-06-04 15:24:23 +02:00
#endif
{
vecDir = m_pPlayer->FireBulletsPlayer( 4, vecSrc, vecAiming, VECTOR_CONE_DM_SHOTGUN, 2048, BULLET_PLAYER_BUCKSHOT, 0, 0, m_pPlayer->pev, m_pPlayer->random_seed );
}
else
{
// regular old, untouched spread.
vecDir = m_pPlayer->FireBulletsPlayer( 6, vecSrc, vecAiming, VECTOR_CONE_10DEGREES, 2048, BULLET_PLAYER_BUCKSHOT, 0, 0, m_pPlayer->pev, m_pPlayer->random_seed );
}
PLAYBACK_EVENT_FULL( flags, m_pPlayer->edict(), m_usSingleFire, 0.0, g_vecZero, g_vecZero, vecDir.x, vecDir.y, 0, 0, 0, 0 );
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
if( !m_iClip && m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] <= 0 )
2016-06-04 15:24:23 +02:00
// HEV suit - indicate out of ammo condition
2016-07-31 15:48:50 +02:00
m_pPlayer->SetSuitUpdate( "!HEV_AMO0", FALSE, 0 );
2016-06-04 15:24:23 +02:00
//if( m_iClip != 0 )
2019-10-13 13:49:25 +02:00
m_flPumpTime = gpGlobals->time + 0.5f;
2016-06-04 15:24:23 +02:00
2019-10-13 13:49:25 +02:00
m_flNextPrimaryAttack = GetNextAttackDelay( 0.75f );
m_flNextSecondaryAttack = UTIL_WeaponTimeBase() + 0.75f;
2016-07-31 15:48:50 +02:00
if( m_iClip != 0 )
2019-10-13 13:49:25 +02:00
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 5.0f;
2016-06-04 15:24:23 +02:00
else
2019-10-13 13:49:25 +02:00
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 0.75f;
2016-06-04 15:24:23 +02:00
m_fInSpecialReload = 0;
}
void CShotgun::SecondaryAttack( void )
{
// don't fire underwater
2016-07-31 15:48:50 +02:00
if( m_pPlayer->pev->waterlevel == 3 )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
PlayEmptySound();
2019-10-13 13:49:25 +02:00
m_flNextPrimaryAttack = GetNextAttackDelay( 0.15f );
2016-06-04 15:24:23 +02:00
return;
}
2016-07-31 15:48:50 +02:00
if( m_iClip <= 1 )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
Reload();
PlayEmptySound();
2016-06-04 15:24:23 +02:00
return;
}
m_pPlayer->m_iWeaponVolume = LOUD_GUN_VOLUME;
m_pPlayer->m_iWeaponFlash = NORMAL_GUN_FLASH;
m_iClip -= 2;
int flags;
2021-06-07 16:17:22 +02:00
#if CLIENT_WEAPONS
2016-06-04 15:24:23 +02:00
flags = FEV_NOTHOST;
#else
flags = 0;
#endif
2016-07-31 15:48:50 +02:00
m_pPlayer->pev->effects = (int)( m_pPlayer->pev->effects ) | EF_MUZZLEFLASH;
2016-06-04 15:24:23 +02:00
// player "shoot" animation
m_pPlayer->SetAnimation( PLAYER_ATTACK1 );
2016-07-31 15:48:50 +02:00
Vector vecSrc = m_pPlayer->GetGunPosition();
2016-06-04 15:24:23 +02:00
Vector vecAiming = m_pPlayer->GetAutoaimVector( AUTOAIM_5DEGREES );
Vector vecDir;
2016-07-31 15:48:50 +02:00
2021-06-07 02:05:58 +02:00
#if CLIENT_DLL
2016-07-31 15:48:50 +02:00
if( bIsMultiplayer() )
2016-06-04 15:24:23 +02:00
#else
2016-07-31 15:48:50 +02:00
if( g_pGameRules->IsMultiplayer() )
2016-06-04 15:24:23 +02:00
#endif
{
// tuned for deathmatch
vecDir = m_pPlayer->FireBulletsPlayer( 8, vecSrc, vecAiming, VECTOR_CONE_DM_DOUBLESHOTGUN, 2048, BULLET_PLAYER_BUCKSHOT, 0, 0, m_pPlayer->pev, m_pPlayer->random_seed );
}
else
{
// untouched default single player
vecDir = m_pPlayer->FireBulletsPlayer( 12, vecSrc, vecAiming, VECTOR_CONE_10DEGREES, 2048, BULLET_PLAYER_BUCKSHOT, 0, 0, m_pPlayer->pev, m_pPlayer->random_seed );
}
2016-07-31 15:48:50 +02:00
2019-10-13 13:49:25 +02:00
PLAYBACK_EVENT_FULL( flags, m_pPlayer->edict(), m_usDoubleFire, 0.0f, g_vecZero, g_vecZero, vecDir.x, vecDir.y, 0, 0, 0, 0 );
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
if( !m_iClip && m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] <= 0 )
2016-06-04 15:24:23 +02:00
// HEV suit - indicate out of ammo condition
2016-07-31 15:48:50 +02:00
m_pPlayer->SetSuitUpdate( "!HEV_AMO0", FALSE, 0 );
2016-06-04 15:24:23 +02:00
//if( m_iClip != 0 )
2019-10-13 13:49:25 +02:00
m_flPumpTime = gpGlobals->time + 0.95f;
2016-06-04 15:24:23 +02:00
2019-10-13 13:49:25 +02:00
m_flNextPrimaryAttack = GetNextAttackDelay( 1.5f );
m_flNextSecondaryAttack = UTIL_WeaponTimeBase() + 1.5f;
2016-07-31 15:48:50 +02:00
if( m_iClip != 0 )
2019-10-13 13:49:25 +02:00
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 6.0f;
2016-06-04 15:24:23 +02:00
else
m_flTimeWeaponIdle = 1.5;
m_fInSpecialReload = 0;
}
void CShotgun::Reload( void )
{
2016-07-31 15:48:50 +02:00
if( m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] <= 0 || m_iClip == SHOTGUN_MAX_CLIP )
2016-06-04 15:24:23 +02:00
return;
// don't reload until recoil is done
2016-07-31 15:48:50 +02:00
if( m_flNextPrimaryAttack > UTIL_WeaponTimeBase() )
2016-06-04 15:24:23 +02:00
return;
// check to see if we're ready to reload
2016-07-31 15:48:50 +02:00
if( m_fInSpecialReload == 0 )
2016-06-04 15:24:23 +02:00
{
SendWeaponAnim( SHOTGUN_START_RELOAD );
m_fInSpecialReload = 1;
2019-10-13 13:49:25 +02:00
m_pPlayer->m_flNextAttack = UTIL_WeaponTimeBase() + 0.6f;
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 0.6f;
m_flNextPrimaryAttack = GetNextAttackDelay( 1.0f );
m_flNextSecondaryAttack = UTIL_WeaponTimeBase() + 1.0f;
2016-06-04 15:24:23 +02:00
return;
}
2016-07-31 15:48:50 +02:00
else if( m_fInSpecialReload == 1 )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
if( m_flTimeWeaponIdle > UTIL_WeaponTimeBase() )
2016-06-04 15:24:23 +02:00
return;
// was waiting for gun to move to side
m_fInSpecialReload = 2;
2016-07-31 15:48:50 +02:00
if( RANDOM_LONG( 0, 1 ) )
EMIT_SOUND_DYN( ENT( m_pPlayer->pev ), CHAN_ITEM, "weapons/reload1.wav", 1, ATTN_NORM, 0, 85 + RANDOM_LONG( 0, 0x1f ) );
2016-06-04 15:24:23 +02:00
else
2016-07-31 15:48:50 +02:00
EMIT_SOUND_DYN( ENT( m_pPlayer->pev ), CHAN_ITEM, "weapons/reload3.wav", 1, ATTN_NORM, 0, 85 + RANDOM_LONG( 0, 0x1f ) );
2016-06-04 15:24:23 +02:00
SendWeaponAnim( SHOTGUN_RELOAD );
2019-10-13 13:49:25 +02:00
m_flNextReload = UTIL_WeaponTimeBase() + 0.5f;
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 0.5f;
2016-06-04 15:24:23 +02:00
}
else
{
// Add them to the clip
m_iClip += 1;
m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] -= 1;
m_fInSpecialReload = 1;
}
}
void CShotgun::ItemPostFrame( void )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
if( m_flPumpTime && m_flPumpTime < gpGlobals->time )
2016-06-04 15:24:23 +02:00
{
// play pumping sound
2016-07-31 15:48:50 +02:00
EMIT_SOUND_DYN( ENT( m_pPlayer->pev ), CHAN_ITEM, "weapons/scock1.wav", 1, ATTN_NORM, 0, 95 + RANDOM_LONG( 0, 0x1f ) );
2016-06-04 15:24:23 +02:00
m_flPumpTime = 0;
}
CBasePlayerWeapon::ItemPostFrame();
}
void CShotgun::WeaponIdle( void )
{
ResetEmptySound();
m_pPlayer->GetAutoaimVector( AUTOAIM_5DEGREES );
2016-06-04 15:24:23 +02:00
2016-07-31 15:48:50 +02:00
if( m_flTimeWeaponIdle < UTIL_WeaponTimeBase() )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
if( m_iClip == 0 && m_fInSpecialReload == 0 && m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
Reload();
2016-06-04 15:24:23 +02:00
}
2016-07-31 15:48:50 +02:00
else if( m_fInSpecialReload != 0 )
2016-06-04 15:24:23 +02:00
{
if( m_iClip != SHOTGUN_MAX_CLIP && m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
Reload();
2016-06-04 15:24:23 +02:00
}
else
{
// reload debounce has timed out
SendWeaponAnim( SHOTGUN_PUMP );
// play cocking sound
2016-07-31 15:48:50 +02:00
EMIT_SOUND_DYN( ENT( m_pPlayer->pev ), CHAN_ITEM, "weapons/scock1.wav", 1, ATTN_NORM, 0, 95 + RANDOM_LONG( 0, 0x1f ) );
2016-06-04 15:24:23 +02:00
m_fInSpecialReload = 0;
2019-10-13 13:49:25 +02:00
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 1.5f;
2016-06-04 15:24:23 +02:00
}
}
else
{
int iAnim;
float flRand = UTIL_SharedRandomFloat( m_pPlayer->random_seed, 0, 1 );
2019-10-13 13:49:25 +02:00
if( flRand <= 0.8f )
2016-06-04 15:24:23 +02:00
{
iAnim = SHOTGUN_IDLE_DEEP;
2019-10-13 13:49:25 +02:00
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + ( 60.0f / 12.0f );// * RANDOM_LONG( 2, 5 );
2016-06-04 15:24:23 +02:00
}
2019-10-13 13:49:25 +02:00
else if( flRand <= 0.95f )
2016-06-04 15:24:23 +02:00
{
iAnim = SHOTGUN_IDLE;
2019-10-13 13:49:25 +02:00
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + ( 20.0f / 9.0f );
2016-06-04 15:24:23 +02:00
}
else
{
iAnim = SHOTGUN_IDLE4;
2019-10-13 13:49:25 +02:00
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + ( 20.0f / 9.0f );
2016-06-04 15:24:23 +02:00
}
SendWeaponAnim( iAnim );
}
}
}
class CShotgunAmmo : public CBasePlayerAmmo
{
void Spawn( void )
{
2016-07-31 15:48:50 +02:00
Precache();
SET_MODEL( ENT( pev ), "models/w_shotbox.mdl" );
CBasePlayerAmmo::Spawn();
2016-06-04 15:24:23 +02:00
}
void Precache( void )
{
2016-07-31 15:48:50 +02:00
PRECACHE_MODEL( "models/w_shotbox.mdl" );
PRECACHE_SOUND( "items/9mmclip1.wav" );
2016-06-04 15:24:23 +02:00
}
BOOL AddAmmo( CBaseEntity *pOther )
{
2016-07-31 15:48:50 +02:00
if( pOther->GiveAmmo( AMMO_BUCKSHOTBOX_GIVE, "buckshot", BUCKSHOT_MAX_CARRY ) != -1 )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
EMIT_SOUND( ENT( pev ), CHAN_ITEM, "items/9mmclip1.wav", 1, ATTN_NORM );
2016-06-04 15:24:23 +02:00
return TRUE;
}
return FALSE;
}
};
LINK_ENTITY_TO_CLASS( ammo_buckshot, CShotgunAmmo )