summaryrefslogtreecommitdiff
path: root/game/shared/dod/weapon_dodbasemelee.cpp
blob: 066bbfcaedcf22e490b479ac3a3691ebc6986b5f (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================//

#include "cbase.h"
#include "weapon_dodbasemelee.h"
#include "dod_gamerules.h"

#if defined( CLIENT_DLL )
	#include "c_dod_player.h"
#else
	#include "dod_player.h"
	#include "ilagcompensationmanager.h"
#endif

#include "effect_dispatch_data.h"


#define	KNIFE_BODYHIT_VOLUME 128
#define	KNIFE_WALLHIT_VOLUME 512

// ----------------------------------------------------------------------------- //
// CWeaponDODBaseMelee tables.
// ----------------------------------------------------------------------------- //

IMPLEMENT_NETWORKCLASS_ALIASED( WeaponDODBaseMelee, DT_WeaponDODBaseMelee )

BEGIN_NETWORK_TABLE_NOBASE( CWeaponDODBaseMelee, DT_LocalActiveWeaponBaseMeleeData )
END_NETWORK_TABLE()

BEGIN_NETWORK_TABLE( CWeaponDODBaseMelee, DT_WeaponDODBaseMelee )
END_NETWORK_TABLE()

#ifdef CLIENT_DLL
BEGIN_PREDICTION_DATA( CWeaponDODBaseMelee )
	DEFINE_PRED_FIELD( m_flSmackTime, FIELD_FLOAT, FTYPEDESC_INSENDTABLE ),
END_PREDICTION_DATA()
#endif

LINK_ENTITY_TO_CLASS( weapon_dod_base_melee, CWeaponDODBaseMelee );

#ifndef CLIENT_DLL

	BEGIN_DATADESC( CWeaponDODBaseMelee )
		DEFINE_FUNCTION( Smack )
	END_DATADESC()

#endif

// ----------------------------------------------------------------------------- //
// CWeaponDODBaseMelee implementation.
// ----------------------------------------------------------------------------- //

CWeaponDODBaseMelee::CWeaponDODBaseMelee()
{
}

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

	WEAPON_FILE_INFO_HANDLE	hWpnInfo = LookupWeaponInfoSlot( GetClassname() );

	Assert( hWpnInfo != GetInvalidWeaponInfoHandle() );

	CDODWeaponInfo *pWeaponInfo = dynamic_cast< CDODWeaponInfo* >( GetFileWeaponInfoFromHandle( hWpnInfo ) );

	Assert( pWeaponInfo && "Failed to get CDODWeaponInfo in melee weapon spawn" );
		
	m_pWeaponInfo = pWeaponInfo;

	Assert( m_pWeaponInfo );

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

void CWeaponDODBaseMelee::WeaponIdle( void )
{
	if ( m_flTimeWeaponIdle > gpGlobals->curtime )
		return;

	SendWeaponAnim( ACT_VM_IDLE );

	m_flTimeWeaponIdle = gpGlobals->curtime + SequenceDuration();
}

void CWeaponDODBaseMelee::PrimaryAttack()
{
	MeleeAttack( 60, MELEE_DMG_EDGE, 0.2f, 0.4f );
}

CBaseEntity *CWeaponDODBaseMelee::MeleeAttack( int iDamageAmount, int iDamageType, float flDmgDelay, float flAttackDelay )
{
	if ( !CanAttack() )
		return NULL;

	CDODPlayer *pPlayer = ToDODPlayer( GetPlayerOwner() );

#if !defined (CLIENT_DLL)
	// Move other players back to history positions based on local player's lag
	lagcompensation->StartLagCompensation( pPlayer, pPlayer->GetCurrentCommand() );
#endif

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

	CTraceFilterSimple filter( pPlayer, COLLISION_GROUP_NONE );

	int iTraceMask = MASK_SOLID | CONTENTS_HITBOX | CONTENTS_DEBRIS;

	trace_t tr;
	UTIL_TraceLine( vecSrc, vecEnd, iTraceMask, &filter, &tr );

	const float rayExtension = 40.0f;
	UTIL_ClipTraceToPlayers( vecSrc, vecEnd + vForward * rayExtension, iTraceMask, &filter, &tr );

	if ( tr.fraction >= 1.0 )
	{
		Vector head_hull_mins( -16, -16, -18 );
		Vector head_hull_maxs( 16, 16, 18 );

		UTIL_TraceHull( vecSrc, vecEnd, head_hull_mins, head_hull_maxs, MASK_SOLID, &filter, &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)

			// Make sure it is in front of us
			Vector vecToEnd = vecEnd - vecSrc;
			VectorNormalize( vecToEnd );

			// if zero length, always hit
			if ( vecToEnd.Length() > 0 )
			{
				float dot = DotProduct( vForward, vecToEnd );

				// sanity that our hit is within range
				if ( abs(dot) < 0.95 )
				{
					// fake that we actually missed
					tr.fraction = 1.0;
				}
			}
		}
	}

	bool bDidHit = ( tr.fraction < 1.0f );

	bool bDoStrongAttack = false;

	if ( bDidHit && tr.m_pEnt->IsPlayer() && tr.m_pEnt->m_takedamage != DAMAGE_YES )
	{
		bDidHit = 0;	// still play the animation, we just dont attempt to damage this player
	}

	if ( bDidHit )	//if the swing hit 
	{	
		// 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; 

		m_iSmackDamage = iDamageAmount;
		m_iSmackDamageType = iDamageType;

		m_flSmackTime = gpGlobals->curtime + flDmgDelay;

		int iOwnerTeam = pPlayer->GetTeamNumber();
		int iVictimTeam = tr.m_pEnt->GetTeamNumber();

		// do the mega attack if its a player, and we would do damage
		if ( tr.m_pEnt->IsPlayer() &&
			tr.m_pEnt->m_takedamage == DAMAGE_YES && 
			( iVictimTeam != iOwnerTeam || ( iVictimTeam == iOwnerTeam && friendlyfire.GetBool() ) ) )
		{
			CDODPlayer *pVictim = ToDODPlayer( tr.m_pEnt );

			Vector victimForward;
			AngleVectors( pVictim->GetAbsAngles(), &victimForward );

			if ( DotProduct( victimForward, vForward ) > 0.3 )
			{
				bDoStrongAttack = true;
			}
		}
	}

	if ( bDoStrongAttack )
	{
		m_iSmackDamage = 300;
		flAttackDelay = 0.9f;
		m_flSmackTime = gpGlobals->curtime + 0.4f;

		m_iSmackDamageType = MELEE_DMG_EDGE | MELEE_DMG_STRONGATTACK;

		// play a "Strong" attack
		SendWeaponAnim( ACT_VM_SECONDARYATTACK );
	}
	else
	{
		WeaponSound( MELEE_MISS );
		SendWeaponAnim( GetMeleeActivity() );
	}

	// player animation
	pPlayer->DoAnimationEvent( PLAYERANIMEVENT_SECONDARY_ATTACK );

	m_flNextPrimaryAttack = gpGlobals->curtime + flAttackDelay;
	m_flNextSecondaryAttack = gpGlobals->curtime + flAttackDelay;
	m_flTimeWeaponIdle = gpGlobals->curtime + SequenceDuration();


#ifndef CLIENT_DLL
	IGameEvent * event = gameeventmanager->CreateEvent( "dod_stats_weapon_attack" );
	if ( event )
	{
		event->SetInt( "attacker", pPlayer->GetUserID() );
		event->SetInt( "weapon", GetStatsWeaponID() );

		gameeventmanager->FireEvent( event );
	}

	lagcompensation->FinishLagCompensation( pPlayer );
#endif	//CLIENT_DLL

	return tr.m_pEnt;
}