port glock silencer code from “/

Copyright © 1996-2001, Valve LLC. All rights reserved.

* This product contains software technology licensed from Id
* Software, Inc. (“Id Technology”). Id Technology © 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 GLOCK_MODEL_DEFAULT “models/w_9mmhandgun.mdl”
#define GLOCK_MODEL_SILENCER “models/w_silencer.mdl”

#define SILENCER_GROUP 2
#define SILENCER_OFF 0
#define SILENCER_ON 1
enum glock_e {
GLOCK_IDLE1 = 0,
GLOCK_IDLE2,
GLOCK_IDLE3,
GLOCK_SHOOT,
GLOCK_SHOOT_EMPTY,
GLOCK_RELOAD,
GLOCK_RELOAD_NOT_EMPTY,
GLOCK_DRAW,
GLOCK_HOLSTER,
GLOCK_ADD_SILENCER
};

LINK_ENTITY_TO_CLASS( weapon_glock, CGlock );
LINK_ENTITY_TO_CLASS( weapon_9mmhandgun, CGlock );

BOOL CGlock::IsSilencerOn( void ) const
{
return pev->body == SILENCER_ON;
}

void CGlock::SetSilencerOn( BOOL bOn )
{
if (bOn)
pev->body = SILENCER_ON;
else
pev->body = SILENCER_OFF;
}

void CGlock::Spawn( )
{
pev->classname = MAKE_STRING(“weapon_9mmhandgun”); // hack to allow for old names
Precache( );
m_iId = WEAPON_GLOCK;
char szModel = NULL;

if ( IsSilencerOn() )
{
szModel = GLOCK_MODEL_SILENCER;
}
else
{
szModel = GLOCK_MODEL_DEFAULT;
}

SET_MODEL(ENT(pev), szModel);

m_fInAttack = 0;

m_iDefaultAmmo = GLOCK_DEFAULT_GIVE;

FallInit();// get ready to fall down.
}


void CGlock::Precache( void )
{
PRECACHE_MODEL(“models/v_9mmhandgun.mdl”);
PRECACHE_MODEL(“models/w_9mmhandgun.mdl”);
PRECACHE_MODEL(“models/w_silencer.mdl”);
PRECACHE_MODEL(“models/p_9mmhandgun.mdl”);

m_iShell = PRECACHE_MODEL (“models/shell.mdl”);// brass shell

PRECACHE_SOUND(“items/9mmclip1.wav”);
PRECACHE_SOUND(“items/9mmclip2.wav”);

PRECACHE_SOUND (“weapons/pl_gun1.wav”);//silenced handgun
PRECACHE_SOUND (“weapons/pl_gun2.wav”);//silenced handgun
PRECACHE_SOUND (“weapons/pl_gun3.wav”);//handgun

m_usFireGlock1 = PRECACHE_EVENT( 1, “events/glock1.sc” );
m_usFireGlock2 = PRECACHE_EVENT( 1, “events/glock2.sc” );
}

int CGlock::GetItemInfo(ItemInfo p)
{
p->pszName = STRING(pev->classname);
p->pszAmmo1 = “9mm”;
p->iMaxAmmo1 = _9MM_MAX_CARRY;
p->pszAmmo2 = NULL;
p->iMaxAmmo2 = -1;
p->iMaxClip = GLOCK_MAX_CLIP;
p->iSlot = 1;
p->iPosition = 0;
p->iFlags = 0;
p->iId = m_iId = WEAPON_GLOCK;
p->iWeight = GLOCK_WEIGHT;

return 1;
}

int CGlock::AddToPlayer( CBasePlayer pPlayer )
{
if ( CBasePlayerWeapon::AddToPlayer( pPlayer ) )
{
MESSAGE_BEGIN( MSG_ONE, gmsgWeapPickup, NULL, pPlayer->pev );
WRITE_BYTE( m_iId );
MESSAGE_END();
return TRUE;
}
return FALSE;
}

BOOL CGlock::Deploy( )
{
return DefaultDeploy(“models/v_9mmhandgun.mdl”, “models/p_9mmhandgun.mdl”, GLOCK_DRAW, “onehanded”, UseDecrement(), pev->body);
}

void CGlock::Holster(int skiplocal /= 0/)
{
m_pPlayer->m_flNextAttack = UTIL_WeaponTimeBase() + 0.5;
SendWeaponAnim(GLOCK_HOLSTER, UseDecrement(), pev->body);

m_fInAttack = 0;
}

BOOL CGlock::ShouldWeaponIdle(void)
{
return m_fInAttack != 0;
}
void CGlock::SecondaryAttack( void )
{
if (m_fInAttack != 0)
return;

if ( !IsSilencerOn() )
{
// Add silencer.
m_fInAttack = 1;

m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 1.0;

SendWeaponAnim(GLOCK_HOLSTER, UseDecrement(), pev->body);

m_flNextPrimaryAttack = m_flNextSecondaryAttack = GetNextAttackDelay(2.0);
}
else
{
// Remove silencer.
m_fInAttack = 2;

m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 2.4; // 3.3

SendWeaponAnim( GLOCK_ADD_SILENCER, UseDecrement(), pev->body);

m_flNextPrimaryAttack = m_flNextSecondaryAttack = GetNextAttackDelay(3.4);
}
}

void CGlock::PrimaryAttack( void )
{
if (m_fInAttack != 0)
return;

if ( IsSilencerOn() )
{
GlockFire(0.01, 0.3, TRUE);
}
else
{
GlockFire(0.1, 0.2, FALSE);
}
}

void CGlock::GlockFire( float flSpread , float flCycleTime, BOOL fUseAutoAim )
{
if (m_iClip <= 0)
{
if (m_fFireOnEmpty)
{
PlayEmptySound();
m_flNextPrimaryAttack = GetNextAttackDelay(0.2);
}

return;
}

m_iClip–;

m_pPlayer->pev->effects = (int)(m_pPlayer->pev->effects) | EF_MUZZLEFLASH;

int flags;

#if defined( CLIENT_WEAPONS )
flags = FEV_NOTHOST;
#else
flags = 0;
#endif

// player “shoot” animation
m_pPlayer->SetAnimation( PLAYER_ATTACK1 );

// silenced
if ( IsSilencerOn() )
{
m_pPlayer->m_iWeaponVolume = QUIET_GUN_VOLUME;
m_pPlayer->m_iWeaponFlash = DIM_GUN_FLASH;
}
else
{
// non-silenced
m_pPlayer->m_iWeaponVolume = NORMAL_GUN_VOLUME;
m_pPlayer->m_iWeaponFlash = NORMAL_GUN_FLASH;
}

Vector vecSrc = m_pPlayer->GetGunPosition( );
Vector vecAiming;

if ( fUseAutoAim )
{
vecAiming = m_pPlayer->GetAutoaimVector( AUTOAIM_10DEGREES );
}
else
{
vecAiming = gpGlobals->v_forward;
}

Vector vecDir;
vecDir = m_pPlayer->FireBulletsPlayer( 1, vecSrc, vecAiming, Vector( flSpread, flSpread, flSpread ), 8192, BULLET_PLAYER_9MM, 0, 0, m_pPlayer->pev, m_pPlayer->random_seed );

PLAYBACK_EVENT_FULL(flags, m_pPlayer->edict(), m_usFireGlock1, 0.0, (float *)&g_vecZero, (float *)&g_vecZero, vecDir.x, vecDir.y, pev->body, 0, (m_iClip == 0) ? 1 : 0, IsSilencerOn() );

m_flNextPrimaryAttack = m_flNextSecondaryAttack = GetNextAttackDelay(flCycleTime);

if (!m_iClip && m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] <= 0)
// HEV suit - indicate out of ammo condition
m_pPlayer->SetSuitUpdate(”!HEV_AMO0", FALSE, 0);

m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + UTIL_SharedRandomFloat( m_pPlayer->random_seed, 10, 15 );
}


void CGlock::Reload( void )
{
if ( m_pPlayer->ammo_9mm <= 0 )
return;

int iResult;

if (m_iClip == 0)
iResult = DefaultReload(GLOCK_MAX_CLIP, GLOCK_RELOAD, 1.5, pev->body);
else
iResult = DefaultReload(GLOCK_MAX_CLIP, GLOCK_RELOAD_NOT_EMPTY, 1.5, pev->body);

if (iResult)
{
m_fInAttack = 0;

m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + UTIL_SharedRandomFloat( m_pPlayer->random_seed, 10, 15 );
}
}



void CGlock::WeaponIdle( void )
{
ResetEmptySound( );

m_pPlayer->GetAutoaimVector( AUTOAIM_10DEGREES );

if ( m_flTimeWeaponIdle > UTIL_WeaponTimeBase() )
return;

if (m_fInAttack != 0)
{
if (m_fInAttack == 1)
{
SetSilencerOn( TRUE );
}
else if (m_fInAttack == 2)
{
SetSilencerOn( FALSE );
}

SendWeaponAnim(GLOCK_DRAW, UseDecrement(), pev->body);

m_fInAttack = 0;

m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 1.0;
}
else
{
// only idle if the slid isn’t back
if (m_iClip != 0)
{
int iAnim;
float flRand = UTIL_SharedRandomFloat( m_pPlayer->random_seed, 0.0, 1.0 );

if (flRand <= 0.3 + 0 * 0.75)
{
iAnim = GLOCK_IDLE3;
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 49.0 / 16;
}
else if (flRand <= 0.6 + 0 * 0.875)
{
iAnim = GLOCK_IDLE1;
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 60.0 / 16.0;
}
else
{
iAnim = GLOCK_IDLE2;
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 40.0 / 16.0;
}

SendWeaponAnim(iAnim, UseDecrement(), pev->body);
}
}
}








class CGlockAmmo : public CBasePlayerAmmo
{
void Spawn( void )
{
Precache( );
SET_MODEL(ENT(pev), “models/w_9mmclip.mdl”);
CBasePlayerAmmo::Spawn( );
}
void Precache( void )
{
PRECACHE_MODEL (“models/w_9mmclip.mdl”);
PRECACHE_SOUND(“items/9mmclip1.wav”);
}
BOOL AddAmmo( CBaseEntity *pOther )
{
if (pOther->GiveAmmo( AMMO_GLOCKCLIP_GIVE, “9mm”, _9MM_MAX_CARRY ) != -1)
{
EMIT_SOUND(ENT(pev), CHAN_ITEM, “items/9mmclip1.wav”, 1, ATTN_NORM);
return TRUE;
}
return FALSE;
}
};
LINK_ENTITY_TO_CLASS( ammo_glockclip, CGlockAmmo );
LINK_ENTITY_TO_CLASS( ammo_9mmclip, CGlockAmmo );" to this code “//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose: Glock - hand gun
//
// NoKeywords:
//=============================================================================//

#include “cbase.h”
#include “npcevent.h”
#include “hl1_baseweapon_shared.h”
//#include “basecombatcharacter.h”
//#include “ai_basenpc.h”
#ifdef CLIENT_DLL
#include “c_baseplayer.h”
#else
#include “player.h”
#endif
#include “gamerules.h”
#include “in_buttons.h”
#ifdef CLIENT_DLL
#else
#include “soundent.h”
#include “game.h”
#endif
#include “vstdlib/random.h”
#include “engine/IEngineSound.h”

#ifdef CLIENT_DLL
#define CWeaponGlock C_WeaponGlock
#endif

class CWeaponGlock : public CBaseHL1CombatWeapon
{
DECLARE_CLASS( CWeaponGlock, CBaseHL1CombatWeapon );

DECLARE_NETWORKCLASS();
DECLARE_PREDICTABLE();

public:

CWeaponGlock(void);

void Precache( void );
void PrimaryAttack( void );
void SecondaryAttack( void );
bool Reload( void );
void WeaponIdle( void );
void DryFire( void );

private:
void GlockFire( float flSpread , float flCycleTime, bool fUseAutoAim );
};

IMPLEMENT_NETWORKCLASS_ALIASED( WeaponGlock, DT_WeaponGlock );

BEGIN_NETWORK_TABLE( CWeaponGlock, DT_WeaponGlock )
END_NETWORK_TABLE()

LINK_ENTITY_TO_CLASS( weapon_glock, CWeaponGlock );

BEGIN_PREDICTION_DATA( CWeaponGlock )
END_PREDICTION_DATA()

PRECACHE_WEAPON_REGISTER( weapon_glock );


CWeaponGlock::CWeaponGlock( void )
{
m_bReloadsSingly = false;
m_bFiresUnderwater = true;
}


void CWeaponGlock::Precache( void )
{

BaseClass::Precache();
}


void CWeaponGlock::DryFire( void )
{
WeaponSound( EMPTY );
SendWeaponAnim( ACT_VM_DRYFIRE );

m_flNextPrimaryAttack = gpGlobals->curtime + 0.2;
}


void CWeaponGlock::PrimaryAttack( void )
{
GlockFire( 0.01, 0.3, TRUE );
}


void CWeaponGlock::SecondaryAttack( void )
{
GlockFire( 0.1, 0.2, FALSE );
}


void CWeaponGlock::GlockFire( float flSpread , float flCycleTime, bool fUseAutoAim )
{
// Only the player fires this way so we can cast
CBasePlayer *pPlayer = ToBasePlayer( GetOwner() );

if ( !pPlayer )
{
return;
}

if ( m_iClip1 <= 0 )
{
if ( !m_bFireOnEmpty )
{
Reload();
}
else
{
DryFire();
}

return;
}

WeaponSound( SINGLE );

pPlayer->DoMuzzleFlash();

m_iClip1–;

if ( m_iClip1 == 0 )
SendWeaponAnim( ACT_GLOCK_SHOOTEMPTY );
else
SendWeaponAnim( ACT_VM_PRIMARYATTACK );

pPlayer->SetAnimation( PLAYER_ATTACK1 );

m_flNextPrimaryAttack = gpGlobals->curtime + flCycleTime;
m_flNextSecondaryAttack = gpGlobals->curtime + flCycleTime;

Vector vecSrc = pPlayer->Weapon_ShootPosition();
Vector vecAiming;

if ( fUseAutoAim )
{
vecAiming = pPlayer->GetAutoaimVector( AUTOAIM_10DEGREES );
}
else
{
vecAiming = pPlayer->GetAutoaimVector( 0 );
}

// pPlayer->FireBullets( 1, vecSrc, vecAiming, Vector( flSpread, flSpread, flSpread ), MAX_TRACE_LENGTH, m_iPrimaryAmmoType, 0 );
FireBulletsInfo_t info( 1, vecSrc, vecAiming, Vector( flSpread, flSpread, flSpread ), MAX_TRACE_LENGTH, m_iPrimaryAmmoType );
info.m_pAttacker = pPlayer;
pPlayer->FireBullets( info );

#if !defined(CLIENT_DLL)
EjectShell( pPlayer, 0 );
#endif
pPlayer->ViewPunch( QAngle( -2, 0, 0 ) );
#if !defined(CLIENT_DLL)
pPlayer->SetMuzzleFlashTime( gpGlobals->curtime + 0.5 );

CSoundEnt::InsertSound( SOUND_COMBAT, GetAbsOrigin(), 0, 0 );
#endif

if ( !m_iClip1 && pPlayer->GetAmmoCount( m_iPrimaryAmmoType ) <= 0 )
{
// HEV suit - indicate out of ammo condition
pPlayer->SetSuitUpdate(”!HEV_AMO0", FALSE, 0);
}

SetWeaponIdleTime( gpGlobals->curtime + random->RandomFloat( 10, 15 ) );
}


bool CWeaponGlock::Reload( void )
{
bool iResult;

if ( m_iClip1 == 0 )
iResult = DefaultReload( GetMaxClip1(), GetMaxClip2(), ACT_GLOCK_SHOOT_RELOAD );
else
iResult = DefaultReload( GetMaxClip1(), GetMaxClip2(), ACT_VM_RELOAD );

return iResult;
}



void CWeaponGlock::WeaponIdle( void )
{
CBasePlayer *pPlayer = ToBasePlayer( GetOwner() );
if ( pPlayer )
{
pPlayer->GetAutoaimVector( AUTOAIM_10DEGREES );
}

// only idle if the slid isn’t back
if ( m_iClip1 != 0 )
{
BaseClass::WeaponIdle();
}
}
" dont write all code, only changed lines and where