hlsdk-portable/dlls/crowbar.cpp

363 lines
9.0 KiB
C++
Raw Normal View History

2016-06-04 13:24:23 +00: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"
#define CROWBAR_BODYHIT_VOLUME 128
#define CROWBAR_WALLHIT_VOLUME 512
LINK_ENTITY_TO_CLASS( weapon_heaterpipe, CCrowbar )
2016-06-04 13:24:23 +00:00
enum crowbar_e
2016-07-31 13:48:50 +00:00
{
2016-06-04 13:24:23 +00:00
CROWBAR_IDLE = 0,
CROWBAR_DRAW,
CROWBAR_HOLSTER,
CROWBAR_ATTACK1HIT,
CROWBAR_ATTACK1MISS,
CROWBAR_ATTACK2MISS,
CROWBAR_ATTACK2HIT,
CROWBAR_ATTACK3MISS,
2021-06-07 00:05:58 +00:00
#if !CROWBAR_IDLE_ANIM
2016-06-04 13:24:23 +00:00
CROWBAR_ATTACK3HIT
#else
CROWBAR_ATTACK3HIT,
CROWBAR_IDLE2,
CROWBAR_IDLE3
#endif
2016-06-04 13:24:23 +00:00
};
void CCrowbar::Spawn()
2016-06-04 13:24:23 +00:00
{
2016-07-31 13:48:50 +00:00
Precache();
2016-06-04 13:24:23 +00:00
m_iId = WEAPON_CROWBAR;
SET_MODEL( ENT( pev ), "models/w_heaterpipe.mdl" );
2016-06-04 13:24:23 +00:00
m_iClip = -1;
FallInit();// get ready to fall down.
}
void CCrowbar::Precache( void )
{
PRECACHE_MODEL( "models/v_heaterpipe.mdl" );
PRECACHE_MODEL( "models/w_heaterpipe.mdl" );
PRECACHE_MODEL( "models/p_heaterpipe.mdl" );
PRECACHE_SOUND( "weapons/pipe_hit1.wav" );
PRECACHE_SOUND( "weapons/pipe_hit2.wav" );
2016-08-13 07:16:23 +00:00
PRECACHE_SOUND( "weapons/cbar_hitbod1.wav" );
PRECACHE_SOUND( "weapons/cbar_hitbod2.wav" );
PRECACHE_SOUND( "weapons/cbar_hitbod3.wav" );
PRECACHE_SOUND( "weapons/pipe_miss.wav" );
PRECACHE_SOUND( "player/pl_breathe.wav" );
2016-06-04 13:24:23 +00:00
}
2016-07-31 13:48:50 +00:00
int CCrowbar::GetItemInfo( ItemInfo *p )
2016-06-04 13:24:23 +00:00
{
2016-07-31 13:48:50 +00:00
p->pszName = STRING( pev->classname );
2016-06-04 13:24:23 +00:00
p->pszAmmo1 = NULL;
p->iMaxAmmo1 = -1;
p->pszAmmo2 = NULL;
p->iMaxAmmo2 = -1;
p->iMaxClip = WEAPON_NOCLIP;
p->iSlot = 0;
p->iPosition = 0;
p->iId = m_iId = WEAPON_CROWBAR;
2016-06-04 13:24:23 +00:00
p->iWeight = CROWBAR_WEIGHT;
return 1;
}
int CCrowbar::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;
}
2016-07-31 13:48:50 +00:00
BOOL CCrowbar::Deploy()
2016-06-04 13:24:23 +00:00
{
return DefaultDeploy( "models/v_heaterpipe.mdl", "models/p_heaterpipe.mdl", CROWBAR_DRAW, "crowbar" );
2016-06-04 13:24:23 +00:00
}
void CCrowbar::Holster( int skiplocal /* = 0 */ )
{
2019-10-13 11:49:25 +00:00
m_pPlayer->m_flNextAttack = UTIL_WeaponTimeBase() + 0.5f;
2016-06-04 13:24:23 +00:00
SendWeaponAnim( CROWBAR_HOLSTER );
}
void FindHullIntersection( const Vector &vecSrc, TraceResult &tr, float *mins, float *maxs, edict_t *pEntity )
{
2016-07-31 13:48:50 +00:00
int i, j, k;
2016-06-04 13:24:23 +00:00
float distance;
float *minmaxs[2] = {mins, maxs};
2016-07-31 13:48:50 +00:00
TraceResult tmpTrace;
2016-06-04 13:24:23 +00:00
Vector vecHullEnd = tr.vecEndPos;
Vector vecEnd;
distance = 1e6f;
2019-10-13 11:49:25 +00:00
vecHullEnd = vecSrc + ( ( vecHullEnd - vecSrc ) * 2.0f );
2016-06-04 13:24:23 +00:00
UTIL_TraceLine( vecSrc, vecHullEnd, dont_ignore_monsters, pEntity, &tmpTrace );
2019-10-13 11:49:25 +00:00
if( tmpTrace.flFraction < 1.0f )
2016-06-04 13:24:23 +00:00
{
tr = tmpTrace;
return;
}
2016-07-31 13:48:50 +00:00
for( i = 0; i < 2; i++ )
2016-06-04 13:24:23 +00:00
{
2016-07-31 13:48:50 +00:00
for( j = 0; j < 2; j++ )
2016-06-04 13:24:23 +00:00
{
2016-07-31 13:48:50 +00:00
for( k = 0; k < 2; k++ )
2016-06-04 13:24:23 +00:00
{
vecEnd.x = vecHullEnd.x + minmaxs[i][0];
vecEnd.y = vecHullEnd.y + minmaxs[j][1];
vecEnd.z = vecHullEnd.z + minmaxs[k][2];
UTIL_TraceLine( vecSrc, vecEnd, dont_ignore_monsters, pEntity, &tmpTrace );
2019-10-13 11:49:25 +00:00
if( tmpTrace.flFraction < 1.0f )
2016-06-04 13:24:23 +00:00
{
2016-07-31 13:48:50 +00:00
float thisDistance = ( tmpTrace.vecEndPos - vecSrc ).Length();
if( thisDistance < distance )
2016-06-04 13:24:23 +00:00
{
tr = tmpTrace;
distance = thisDistance;
}
}
}
}
}
}
void CCrowbar::PrimaryAttack()
{
if( m_flNextPrimaryAttack + 1 <= UTIL_WeaponTimeBase() )
2016-06-04 13:24:23 +00:00
{
m_iSwing = Q_max( 0, m_iSwing - static_cast<int>( ( UTIL_WeaponTimeBase() - m_flNextPrimaryAttack ) * 5.0f ) );
2016-06-04 13:24:23 +00:00
}
Swing();
++m_iSwing;
2016-06-04 13:24:23 +00:00
}
2016-07-31 13:48:50 +00:00
void CCrowbar::Smack()
2016-06-04 13:24:23 +00:00
{
DecalGunshot( &m_trHit, BULLET_PLAYER_CROWBAR );
}
void CCrowbar::Swing()
2016-06-04 13:24:23 +00:00
{
TraceResult tr;
float flVol;
2016-06-04 13:24:23 +00:00
2016-07-31 13:48:50 +00:00
UTIL_MakeVectors( m_pPlayer->pev->v_angle );
Vector vecSrc = m_pPlayer->GetGunPosition();
2019-10-13 11:49:25 +00:00
Vector vecEnd = vecSrc + gpGlobals->v_forward * 32.0f;
2016-06-04 13:24:23 +00:00
UTIL_TraceLine( vecSrc, vecEnd, dont_ignore_monsters, ENT( m_pPlayer->pev ), &tr );
2021-06-07 00:05:58 +00:00
#if !CLIENT_DLL
2019-10-13 11:49:25 +00:00
if( tr.flFraction >= 1.0f )
2016-06-04 13:24:23 +00:00
{
UTIL_TraceHull( vecSrc, vecEnd, dont_ignore_monsters, head_hull, ENT( m_pPlayer->pev ), &tr );
2019-10-13 11:49:25 +00:00
if( tr.flFraction < 1.0f )
2016-06-04 13:24:23 +00:00
{
// Calculate the point of intersection of the line (or hull) and the object we hit
// This is and approximation of the "best" intersection
CBaseEntity *pHit = CBaseEntity::Instance( tr.pHit );
2016-07-31 13:48:50 +00:00
if( !pHit || pHit->IsBSPModel() )
2016-06-04 13:24:23 +00:00
FindHullIntersection( vecSrc, tr, VEC_DUCK_HULL_MIN, VEC_DUCK_HULL_MAX, m_pPlayer->edict() );
vecEnd = tr.vecEndPos; // This is the point on the actual surface (the hull could have hit space)
}
}
#endif
if( m_iSwing > 4 && m_flNextBreathSoundTime <= UTIL_WeaponTimeBase() )
{
2019-11-17 11:41:46 +00:00
flVol = Q_min( 1.0f, static_cast<float>( m_iSwing - 3 ) * 0.2f );
EMIT_SOUND( ENT( m_pPlayer->pev ), CHAN_BODY, "player/pl_breathe.wav", flVol, ATTN_NORM );
m_flNextBreathSoundTime = UTIL_WeaponTimeBase() + 1.2f;
}
2016-06-04 13:24:23 +00:00
2019-10-13 11:49:25 +00:00
if( tr.flFraction >= 1.0f )
2016-06-04 13:24:23 +00:00
{
// miss
switch( ( m_iSwing++ ) % 3 )
2016-06-04 13:24:23 +00:00
{
case 0:
SendWeaponAnim( CROWBAR_ATTACK1MISS );
break;
case 1:
SendWeaponAnim( CROWBAR_ATTACK2MISS );
break;
case 2:
SendWeaponAnim( CROWBAR_ATTACK3MISS );
break;
}
m_flNextPrimaryAttack = ( static_cast<float>( m_iSwing ) * 0.03f ) + UTIL_WeaponTimeBase() + 0.5f;
EMIT_SOUND_DYN( ENT( m_pPlayer->pev ), CHAN_WEAPON, "weapons/pipe_miss.wav", 1, ATTN_NORM, 0, 94 + RANDOM_LONG( 0, 0xF ) );
2021-06-07 00:05:58 +00:00
#if CROWBAR_IDLE_ANIM
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + UTIL_SharedRandomFloat( m_pPlayer->random_seed, 10, 15 );
#endif
// player "shoot" animation
m_pPlayer->SetAnimation( PLAYER_ATTACK1 );
2016-06-04 13:24:23 +00:00
}
else
{
switch( ( m_iSwing++ ) % 3 )
2016-06-04 13:24:23 +00:00
{
case 0:
2016-07-31 13:48:50 +00:00
SendWeaponAnim( CROWBAR_ATTACK1HIT );
break;
2016-06-04 13:24:23 +00:00
case 1:
2016-07-31 13:48:50 +00:00
SendWeaponAnim( CROWBAR_ATTACK2HIT );
break;
2016-06-04 13:24:23 +00:00
case 2:
2016-07-31 13:48:50 +00:00
SendWeaponAnim( CROWBAR_ATTACK3HIT );
break;
2016-06-04 13:24:23 +00:00
}
// player "shoot" animation
m_pPlayer->SetAnimation( PLAYER_ATTACK1 );
2021-06-07 00:05:58 +00:00
#if !CLIENT_DLL
2016-06-04 13:24:23 +00:00
// hit
2016-07-31 13:48:50 +00:00
CBaseEntity *pEntity = CBaseEntity::Instance( tr.pHit );
2016-06-04 13:24:23 +00:00
// play thwack, smack, or dong sound
2019-11-17 11:41:46 +00:00
flVol = 1.0f;
int fHitWorld = TRUE;
m_flNextPrimaryAttack = ( static_cast<float>( m_iSwing ) * 0.03f ) + UTIL_WeaponTimeBase() + 0.3f;
2016-06-04 13:24:23 +00:00
2016-07-31 13:48:50 +00:00
if( pEntity )
2016-06-04 13:24:23 +00:00
{
ClearMultiDamage();
float flDmg = Q_max( 1.0f, gSkillData.plrDmgCrowbar - ( static_cast<float>( m_iSwing ) * 0.5f ) );
pEntity->TraceAttack( m_pPlayer->pev, flDmg, gpGlobals->v_forward, &tr, DMG_CLUB );
ApplyMultiDamage( m_pPlayer->pev, m_pPlayer->pev );
2016-07-31 13:48:50 +00:00
if( pEntity->Classify() != CLASS_NONE && pEntity->Classify() != CLASS_MACHINE )
2016-06-04 13:24:23 +00:00
{
// play thwack or smack sound
2016-07-31 13:48:50 +00:00
switch( RANDOM_LONG( 0, 2 ) )
2016-06-04 13:24:23 +00:00
{
case 0:
2019-10-13 11:49:25 +00:00
EMIT_SOUND( ENT( m_pPlayer->pev ), CHAN_ITEM, "weapons/cbar_hitbod1.wav", 1.0f, ATTN_NORM );
2016-07-31 13:48:50 +00:00
break;
2016-06-04 13:24:23 +00:00
case 1:
2019-10-13 11:49:25 +00:00
EMIT_SOUND( ENT( m_pPlayer->pev ), CHAN_ITEM, "weapons/cbar_hitbod2.wav", 1.0f, ATTN_NORM );
2016-07-31 13:48:50 +00:00
break;
2016-06-04 13:24:23 +00:00
case 2:
2019-10-13 11:49:25 +00:00
EMIT_SOUND( ENT( m_pPlayer->pev ), CHAN_ITEM, "weapons/cbar_hitbod3.wav", 1.0f, ATTN_NORM );
2016-07-31 13:48:50 +00:00
break;
2016-06-04 13:24:23 +00:00
}
2019-10-13 11:49:25 +00:00
2016-06-04 13:24:23 +00:00
m_pPlayer->m_iWeaponVolume = CROWBAR_BODYHIT_VOLUME;
2019-10-13 11:49:25 +00:00
2016-07-31 13:48:50 +00:00
if( !pEntity->IsAlive() )
return;
2016-06-04 13:24:23 +00:00
else
2019-10-13 11:49:25 +00:00
flVol = 0.1f;
2016-06-04 13:24:23 +00:00
fHitWorld = FALSE;
}
}
// play texture hit sound
// UNDONE: Calculate the correct point of intersection when we hit with the hull instead of the line
2016-07-31 13:48:50 +00:00
if( fHitWorld )
2016-06-04 13:24:23 +00:00
{
2019-10-13 11:49:25 +00:00
float fvolbar = TEXTURETYPE_PlaySound( &tr, vecSrc, vecSrc + ( vecEnd - vecSrc ) * 2.0f, BULLET_PLAYER_CROWBAR );
2016-06-04 13:24:23 +00:00
2016-07-31 13:48:50 +00:00
if( g_pGameRules->IsMultiplayer() )
2016-06-04 13:24:23 +00:00
{
// override the volume here, cause we don't play texture sounds in multiplayer,
// and fvolbar is going to be 0 from the above call.
2019-10-13 11:49:25 +00:00
fvolbar = 1.0f;
2016-06-04 13:24:23 +00:00
}
// also play crowbar strike
2016-07-31 13:48:50 +00:00
switch( RANDOM_LONG( 0, 1 ) )
2016-06-04 13:24:23 +00:00
{
case 0:
EMIT_SOUND_DYN( ENT( m_pPlayer->pev ), CHAN_ITEM, "weapons/pipe_hit1.wav", fvolbar, ATTN_NORM, 0, 98 + RANDOM_LONG( 0, 3 ) );
2016-06-04 13:24:23 +00:00
break;
case 1:
EMIT_SOUND_DYN( ENT( m_pPlayer->pev ), CHAN_ITEM, "weapons/pipe_hit2.wav", fvolbar, ATTN_NORM, 0, 98 + RANDOM_LONG( 0, 3 ) );
2016-06-04 13:24:23 +00:00
break;
}
// delay the decal a bit
m_trHit = tr;
}
2017-06-29 13:56:03 +00:00
m_pPlayer->m_iWeaponVolume = (int)( flVol * CROWBAR_WALLHIT_VOLUME );
2016-07-31 13:48:50 +00:00
2016-06-04 13:24:23 +00:00
SetThink( &CCrowbar::Smack );
2021-06-05 23:50:15 +00:00
2021-02-27 11:37:12 +00:00
pev->nextthink = gpGlobals->time + 0.2f;
#endif
2016-06-04 13:24:23 +00:00
}
2021-06-07 00:05:58 +00:00
#if CROWBAR_IDLE_ANIM
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + UTIL_SharedRandomFloat( m_pPlayer->random_seed, 10, 15 );
#endif
2016-06-04 13:24:23 +00:00
}
2021-06-07 00:05:58 +00:00
#if CROWBAR_IDLE_ANIM
void CCrowbar::WeaponIdle( void )
{
if( m_flTimeWeaponIdle < UTIL_WeaponTimeBase() )
{
int iAnim;
float flRand = UTIL_SharedRandomFloat( m_pPlayer->random_seed, 0, 1 );
2019-10-13 11:49:25 +00:00
if( flRand > 0.9f )
{
iAnim = CROWBAR_IDLE2;
2019-10-13 11:49:25 +00:00
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 160.0f / 30.0f;
}
else
{
2019-10-13 11:49:25 +00:00
if( flRand > 0.5f )
{
iAnim = CROWBAR_IDLE;
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 70.0f / 25.0f;
}
else
{
iAnim = CROWBAR_IDLE3;
2019-10-13 11:49:25 +00:00
m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 160.0f / 30.0f;
}
}
SendWeaponAnim( iAnim );
}
}
#endif