summaryrefslogtreecommitdiff
path: root/game/shared/tfc/weapon_tfc_crowbar.cpp
blob: acd359fd6ac94ba4de26b4b40f2b589809d2ba31 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================//

#include "cbase.h"
#include "util.h"
#include "weapon_tfc_crowbar.h"
#include "decals.h"

#if defined( CLIENT_DLL )
	#include "c_tfc_player.h"
#else
	#include "tfc_player.h"
#endif


#define	KNIFE_BODYHIT_VOLUME 128
#define	KNIFE_WALLHIT_VOLUME 512


static ConVar tfc_crowbar_damage_first( "tfc_crowbar_damage_first", "25", 0, "First crowbar hit damage." );
static ConVar tfc_crowbar_damage_next( "tfc_crowbar_damage_next", "12.5", 0, "Crowbar hit damage after first hit." );
		

static Vector head_hull_mins( -16, -16, -18 );
static Vector head_hull_maxs( 16, 16, 18 );


// ----------------------------------------------------------------------------- //
// CTFCCrowbar tables.
// ----------------------------------------------------------------------------- //

IMPLEMENT_NETWORKCLASS_ALIASED( TFCCrowbar, DT_WeaponCrowbar )

BEGIN_NETWORK_TABLE( CTFCCrowbar, DT_WeaponCrowbar )
END_NETWORK_TABLE()

BEGIN_PREDICTION_DATA( CTFCCrowbar )
END_PREDICTION_DATA()

LINK_ENTITY_TO_CLASS( weapon_crowbar, CTFCCrowbar );
PRECACHE_WEAPON_REGISTER( weapon_crowbar );

#ifndef CLIENT_DLL

	BEGIN_DATADESC( CTFCCrowbar )
		DEFINE_FUNCTION( Smack )
	END_DATADESC()

#endif

// ----------------------------------------------------------------------------- //
// CTFCCrowbar implementation.
// ----------------------------------------------------------------------------- //

CTFCCrowbar::CTFCCrowbar()
{
}


bool CTFCCrowbar::HasPrimaryAmmo()
{
	return true;
}


bool CTFCCrowbar::CanBeSelected()
{
	return true;
}

void CTFCCrowbar::Precache()
{
	BaseClass::Precache();
}

void CTFCCrowbar::Spawn()
{
	Precache();

	m_iClip1 = -1;
	BaseClass::Spawn();
}


bool CTFCCrowbar::Deploy()
{
	CPASAttenuationFilter filter( this );
	filter.UsePredictionRules();
	EmitSound( filter, entindex(), "Weapon_Crowbar.Deploy" );

	return BaseClass::Deploy();
}

void CTFCCrowbar::Holster( int skiplocal )
{
	GetPlayerOwner()->m_flNextAttack = gpGlobals->curtime + 0.5;
}

void FindHullIntersection( const Vector &vecSrc, trace_t &tr, const Vector &mins, const Vector &maxs, CBaseEntity *pEntity )
{
	int			i, j, k;
	float		distance;
	Vector minmaxs[2] = {mins, maxs};
	trace_t tmpTrace;
	Vector		vecHullEnd = tr.endpos;
	Vector		vecEnd;

	distance = 1e6f;

	vecHullEnd = vecSrc + ((vecHullEnd - vecSrc)*2);
	UTIL_TraceLine( vecSrc, vecHullEnd, MASK_SOLID, pEntity, COLLISION_GROUP_NONE, &tmpTrace );
	if ( tmpTrace.fraction < 1.0 )
	{
		tr = tmpTrace;
		return;
	}

	for ( i = 0; i < 2; i++ )
	{
		for ( j = 0; j < 2; j++ )
		{
			for ( k = 0; k < 2; k++ )
			{
				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, MASK_SOLID, pEntity, COLLISION_GROUP_NONE, &tmpTrace );
				if ( tmpTrace.fraction < 1.0 )
				{
					float thisDistance = (tmpTrace.endpos - vecSrc).Length();
					if ( thisDistance < distance )
					{
						tr = tmpTrace;
						distance = thisDistance;
					}
				}
			}
		}
	}
}


void CTFCCrowbar::ItemPostFrame()
{
	// Store this off so we can detect if it's our first swing or not later on.
	m_flStoredPrimaryAttack = m_flNextPrimaryAttack;
	BaseClass::ItemPostFrame();
}


void CTFCCrowbar::PrimaryAttack()
{
	CTFCPlayer *pPlayer = GetPlayerOwner();

	Vector vForward;
	AngleVectors( pPlayer->EyeAngles(), &vForward );
	Vector vecSrc	= pPlayer->Weapon_ShootPosition();
	Vector vecEnd	= vecSrc + vForward * 32;

	trace_t tr;
	UTIL_TraceLine( vecSrc, vecEnd, MASK_SOLID, pPlayer, COLLISION_GROUP_NONE, &tr );

	if ( tr.fraction >= 1.0 )
	{
		UTIL_TraceHull( vecSrc, vecEnd, head_hull_mins, head_hull_maxs, MASK_SOLID, pPlayer, COLLISION_GROUP_NONE, &tr );
		if ( tr.fraction < 1.0 )
		{
			// 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 = tr.m_pEnt;
			if ( !pHit || pHit->IsBSPModel() )
				FindHullIntersection( vecSrc, tr, VEC_DUCK_HULL_MIN, VEC_DUCK_HULL_MAX, pPlayer );
			vecEnd = tr.endpos;	// This is the point on the actual surface (the hull could have hit space)
		}
	}

	bool bDidHit = tr.fraction < 1.0f;

#ifndef CLIENT_DLL
	bool bFirstSwing = (gpGlobals->curtime - m_flStoredPrimaryAttack) >= 1;
#endif

	pPlayer->DoAnimationEvent( PLAYERANIMEVENT_FIRE_GUN );

	m_flTimeWeaponIdle = gpGlobals->curtime + 2;
	m_flNextPrimaryAttack = gpGlobals->curtime + 0.4f;

	if ( bDidHit )
	{
		SendWeaponAnim( ACT_VM_HITCENTER );
	}
	else
	{
		// Allow for there only being hit activities.
		if ( !SendWeaponAnim( ACT_VM_MISSCENTER ) )
			SendWeaponAnim( ACT_VM_HITCENTER );

		// play wiff or swish sound
		WeaponSound( MELEE_MISS );
	}

	bool bPlayImpactEffect = false;
	
#ifndef CLIENT_DLL

	if ( bDidHit )
	{
		CBaseEntity *pEntity = tr.m_pEnt;
		
		ClearMultiDamage();

		float flDamage = 0;
		bool bDoEffects = true;
		AxeHit( pEntity, bFirstSwing, tr, &flDamage, &bDoEffects );
		if ( flDamage != 0 )
		{
			CTakeDamageInfo info( pPlayer, pPlayer, flDamage, DMG_CLUB | DMG_NEVERGIB );

			CalculateMeleeDamageForce( &info, vForward, tr.endpos, 1.0f/flDamage );
			pEntity->DispatchTraceAttack( info, vForward, &tr ); 
			ApplyMultiDamage();
		}

		if ( bDoEffects )
		{
			if ( pEntity && pEntity->IsPlayer() )
			{
				WeaponSound( MELEE_HIT );

				if ( pEntity->IsAlive() )
					bPlayImpactEffect = true; // no blood effect on dead bodies
			}
			else
			{
				bPlayImpactEffect = true; // always show impact effects on world objects
			}
		}
		else
		{
			bDoEffects = false;
		}
	}


#endif

	if ( bPlayImpactEffect )
	{
		// delay the decal a bit
		m_trHit = tr;
		
		// Store the ent in an EHANDLE, just in case it goes away by the time we get into our think function.
		m_pTraceHitEnt = tr.m_pEnt; 

		SetThink( &CTFCCrowbar::Smack );
		SetNextThink( gpGlobals->curtime + 0.2f );
	}
}


void CTFCCrowbar::Smack()
{
	m_trHit.m_pEnt = m_pTraceHitEnt;
	UTIL_ImpactTrace( &m_trHit, DMG_CLUB );
	
	surfacedata_t *psurf = physprops->GetSurfaceData( m_trHit.surface.surfaceProps );
	if ( psurf->game.material != CHAR_TEX_FLESH && psurf->game.material != CHAR_TEX_BLOODYFLESH )
		WeaponSound( MELEE_HIT_WORLD );
}



void CTFCCrowbar::WeaponIdle()
{
	//ResetEmptySound();

	CTFCPlayer *pPlayer = GetPlayerOwner();

	if (m_flTimeWeaponIdle > gpGlobals->curtime)
		return;

	m_flTimeWeaponIdle = gpGlobals->curtime + 20;

	// only idle if the slid isn't back
	SendWeaponAnim( ACT_VM_IDLE );
}


bool CTFCCrowbar::CanDrop()
{
	return false;
}


TFCWeaponID CTFCCrowbar::GetWeaponID( void ) const
{ 
	return WEAPON_CROWBAR;
}


#ifdef CLIENT_DLL
	
	// ------------------------------------------------------------------------------------------------ //
	// ------------------------------------------------------------------------------------------------ //
	// CLIENT DLL SPECIFIC CODE
	// ------------------------------------------------------------------------------------------------ //
	// ------------------------------------------------------------------------------------------------ //


#else

	// ------------------------------------------------------------------------------------------------ //
	// ------------------------------------------------------------------------------------------------ //
	// GAME DLL SPECIFIC CODE
	// ------------------------------------------------------------------------------------------------ //
	// ------------------------------------------------------------------------------------------------ //

	void CTFCCrowbar::AxeHit( CBaseEntity *pHit, bool bFirstSwing, trace_t &tr, float *flDamage, bool *bDoEffects )
	{
		if ( bFirstSwing )
			*flDamage = tfc_crowbar_damage_first.GetFloat();
		else
			*flDamage = tfc_crowbar_damage_next.GetFloat();
	}

#endif