summaryrefslogtreecommitdiff
path: root/game/shared/tf/tf_weapon_wrench.cpp
blob: 1156a7132ae66c765daacfc68a70b0a01ed1c16b (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================

#include "cbase.h"
#include "tf_weapon_wrench.h"
#include "decals.h"
#include "baseobject_shared.h"
#include "tf_viewmodel.h"

// Client specific.
#ifdef CLIENT_DLL
	#include "c_tf_player.h"
	#include "in_buttons.h"
	#include "tf_hud_menu_eureka_teleport.h"
	// NVNT haptics system interface
	#include "haptics/ihaptics.h"
// Server specific.
#else
	#include "tf_player.h"
	#include "variant_t.h"
	#include "tf_gamerules.h"
	#include "particle_parse.h"
	#include "tf_fx.h"
	#include "tf_obj_sentrygun.h"
#endif

// Maximum time between robo arm hits to maintain the three-hit-combo
#define ROBOARM_COMBO_TIMEOUT 1.0f

//=============================================================================
//
// Weapon Wrench tables.
//
IMPLEMENT_NETWORKCLASS_ALIASED( TFWrench, DT_TFWeaponWrench )

BEGIN_NETWORK_TABLE( CTFWrench, DT_TFWeaponWrench )
END_NETWORK_TABLE()

BEGIN_PREDICTION_DATA( CTFWrench )
END_PREDICTION_DATA()

LINK_ENTITY_TO_CLASS( tf_weapon_wrench, CTFWrench );
PRECACHE_WEAPON_REGISTER( tf_weapon_wrench );

//=============================================================================
//
// Robot Arm tables.
//
IMPLEMENT_NETWORKCLASS_ALIASED( TFRobotArm, DT_TFWeaponRobotArm )

BEGIN_NETWORK_TABLE( CTFRobotArm, DT_TFWeaponRobotArm )
#ifdef GAME_DLL
SendPropEHandle(SENDINFO(m_hRobotArm)),
#else
RecvPropEHandle(RECVINFO(m_hRobotArm)),
#endif
END_NETWORK_TABLE()

#ifdef CLIENT_DLL
BEGIN_PREDICTION_DATA( CTFRobotArm )
// DEFINE_PRED_FIELD( name, fieldtype, flags )
DEFINE_PRED_FIELD( m_iComboCount, FIELD_INTEGER, 0 ),
DEFINE_PRED_FIELD( m_flLastComboHit, FIELD_FLOAT, 0 ),
END_PREDICTION_DATA()
#endif

LINK_ENTITY_TO_CLASS( tf_weapon_robot_arm, CTFRobotArm );
PRECACHE_WEAPON_REGISTER( tf_weapon_robot_arm );

IMPLEMENT_NETWORKCLASS_ALIASED( TFWearableRobotArm, DT_TFWearableRobotArm )

BEGIN_NETWORK_TABLE( CTFWearableRobotArm, DT_TFWearableRobotArm )
END_NETWORK_TABLE()

LINK_ENTITY_TO_CLASS( tf_wearable_robot_arm, CTFWearableRobotArm );

//=============================================================================
//
// Weapon Wrench functions.
//

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CTFWrench::CTFWrench()
	: m_bReloadDown( false )
{}



//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTFWrench::Spawn()
{
	BaseClass::Spawn();
}


#ifdef GAME_DLL
void CTFWrench::OnFriendlyBuildingHit( CBaseObject *pObject, CTFPlayer *pPlayer, Vector hitLoc )
{
	bool bHelpTeammateBuildStructure = pObject->IsBuilding() && pObject->GetOwner() != GetOwner();

	// Did this object hit do any work? repair or upgrade?
	bool bUsefulHit = pObject->InputWrenchHit( pPlayer, this, hitLoc );

	// award achievement if we helped a teammate build a structure
	if ( bUsefulHit && bHelpTeammateBuildStructure )
	{
		CTFPlayer *pOwner = ToTFPlayer( GetOwner() );
		if ( pOwner && pOwner->IsPlayerClass( TF_CLASS_ENGINEER ) )
		{
			pOwner->AwardAchievement( ACHIEVEMENT_TF_ENGINEER_HELP_BUILD_STRUCTURE );
		}
	}

	CDisablePredictionFiltering disabler;

	if ( pObject->IsDisposableBuilding() )
	{
		CSingleUserRecipientFilter singleFilter( pPlayer );
		EmitSound( singleFilter, pObject->entindex(), "Player.UseDeny" );
	}
	else
	{
		if ( bUsefulHit )
		{
			// play success sound
			WeaponSound( SPECIAL1 );
		}
		else
		{
			// play failure sound
			WeaponSound( SPECIAL2 );
		}
	}
}
#endif

void CTFWrench::Smack( void )
{
	// see if we can hit an object with a higher range

	// Get the current player.
	CTFPlayer *pPlayer = GetTFPlayerOwner();
	if ( !pPlayer )
		return;

	if ( !CanAttack() )
		return;

	// Setup a volume for the melee weapon to be swung - approx size, so all melee behave the same.
	static Vector vecSwingMins( -18, -18, -18 );
	static Vector vecSwingMaxs( 18, 18, 18 );

	// Setup the swing range.
	Vector vecForward; 
	AngleVectors( pPlayer->EyeAngles(), &vecForward );
	Vector vecSwingStart = pPlayer->Weapon_ShootPosition();
	Vector vecSwingEnd = vecSwingStart + vecForward * 70;

	// only trace against objects

	// See if we hit anything.
	trace_t trace;	

	CTraceFilterIgnorePlayers traceFilter( NULL, COLLISION_GROUP_NONE );
	UTIL_TraceLine( vecSwingStart, vecSwingEnd, MASK_SOLID, &traceFilter, &trace );
	if ( trace.fraction >= 1.0 )
	{
		UTIL_TraceHull( vecSwingStart, vecSwingEnd, vecSwingMins, vecSwingMaxs, MASK_SOLID, &traceFilter, &trace );
	}

	// We hit, setup the smack.
	if ( trace.fraction < 1.0f &&
		 trace.m_pEnt &&
		 trace.m_pEnt->IsBaseObject() &&
		 trace.m_pEnt->GetTeamNumber() == pPlayer->GetTeamNumber() )
	{
#ifdef GAME_DLL
		OnFriendlyBuildingHit( dynamic_cast< CBaseObject * >( trace.m_pEnt ), pPlayer, trace.endpos );
#else
		// NVNT if the local player is the owner of this wrench 
		//   Notify the haptics system we just repaired something.
		if(pPlayer==C_TFPlayer::GetLocalTFPlayer() && haptics)
			haptics->ProcessHapticEvent(2,"Weapons","tf_weapon_wrench_fix");
#endif
	}
	else
	{
		// if we cannot, Smack as usual for player hits
		BaseClass::Smack();
	}
}

#ifdef CLIENT_DLL
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFWrench::ItemPostFrame()
{
	BaseClass::ItemPostFrame();

	if ( !CanAttack() )
	{
		return;
	}

	CTFPlayer *pOwner = ToTFPlayer( GetOwnerEntity() );
	if ( !pOwner )
	{
		return;
	}

	// Just pressed reload?
	if ( pOwner->m_nButtons & IN_RELOAD && !m_bReloadDown )
	{
		m_bReloadDown = true;
		int iAltFireTeleportToSpawn = 0;
		CALL_ATTRIB_HOOK_INT( iAltFireTeleportToSpawn, alt_fire_teleport_to_spawn );
		if ( iAltFireTeleportToSpawn )
		{
			// Tell the teleport menu to show
			CHudEurekaEffectTeleportMenu *pTeleportMenu = ( CHudEurekaEffectTeleportMenu * )GET_HUDELEMENT( CHudEurekaEffectTeleportMenu );
			if ( pTeleportMenu )
			{
				pTeleportMenu->WantsToTeleport();
			}
		}
	}
	else if ( !(pOwner->m_nButtons & IN_RELOAD) && m_bReloadDown )
	{
		m_bReloadDown = false;
	}
}
#endif

//-----------------------------------------------------------------------------
// Purpose: Kill all buildings when wrench is changed.
//-----------------------------------------------------------------------------
#ifdef GAME_DLL
void CTFWrench::Equip( CBaseCombatCharacter *pOwner )
{
	// STAGING_ENGY
	CTFPlayer *pPlayer = ToTFPlayer( pOwner );
	if ( pPlayer )
	{
		// if switching too gunslinger, blow up other sentry
		int iMiniSentry = 0;
		CALL_ATTRIB_HOOK_INT( iMiniSentry, wrench_builds_minisentry );
		if ( iMiniSentry )
		{
			// Just detonate Sentries
			CObjectSentrygun *pSentry = dynamic_cast<CObjectSentrygun*>( pPlayer->GetObjectOfType( OBJ_SENTRYGUN ) );
			if ( pSentry )
			{
				pSentry->DetonateObject();
			}
		}
	}

	BaseClass::Equip( pOwner );
}
//-----------------------------------------------------------------------------
// Purpose: Kill all buildings when wrench is changed.
//-----------------------------------------------------------------------------
void CTFWrench::Detach( void )
{
	// STAGING_ENGY
	CTFPlayer *pPlayer = GetTFPlayerOwner();
	if ( pPlayer )
	{
		bool bDetonateObjects = true;

		// In MvM mode, leave engineer's buildings after he dies
		if ( TFGameRules() && TFGameRules()->IsMannVsMachineMode() )
		{
			if ( pPlayer->GetTeamNumber() != TF_TEAM_PVE_DEFENDERS )
			{
				bDetonateObjects = false;
			}
		}

		// Only detonate if we are unequipping gunslinger
		if ( bDetonateObjects )
		{
			// if switching off of gunslinger detonate
			int iMiniSentry = 0;
			CALL_ATTRIB_HOOK_INT( iMiniSentry, wrench_builds_minisentry );
			if ( iMiniSentry )
			{
				// Just detonate Sentries
				CObjectSentrygun *pSentry = dynamic_cast<CObjectSentrygun*>( pPlayer->GetObjectOfType( OBJ_SENTRYGUN ) );
				if ( pSentry )
				{
					pSentry->DetonateObject();
				}
			}
		}
	}

	BaseClass::Detach();
}


//-----------------------------------------------------------------------------
// Purpose: Apply health upgrade to our existing buildings
//-----------------------------------------------------------------------------
void CTFWrench::ApplyBuildingHealthUpgrade( void )
{
	CTFPlayer *pPlayer = GetTFPlayerOwner();
	if ( !pPlayer )
		return;

	for ( int i = pPlayer->GetObjectCount()-1; i >= 0; i-- )
	{
		CBaseObject *pObj = pPlayer->GetObject(i);
		if ( pObj )
		{
			pObj->ApplyHealthUpgrade();
		}		
	}
}

#endif

// STAGING_ENGY
ConVar tf_construction_build_rate_multiplier( "tf_construction_build_rate_multiplier", "1.5f", FCVAR_REPLICATED | FCVAR_DEVELOPMENTONLY );
float CTFWrench::GetConstructionValue( void )
{
	float flValue = tf_construction_build_rate_multiplier.GetFloat();
	CALL_ATTRIB_HOOK_FLOAT( flValue, mult_construction_value );
	return flValue;
}

float CTFWrench::GetRepairValue( void )
{
	float flValue = 1.0;
	CALL_ATTRIB_HOOK_FLOAT( flValue, mult_repair_value );

#ifdef GAME_DLL
	if ( GetOwner() )
	{
		CBaseCombatWeapon* pWpn = GetOwner()->Weapon_GetSlot( TF_WPN_TYPE_PRIMARY );
		if ( pWpn )
		{
			CALL_ATTRIB_HOOK_FLOAT_ON_OTHER( pWpn, flValue, mult_repair_value );
		}
	}
#endif

	return flValue;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CTFWrench::Holster( CBaseCombatWeapon *pSwitchingTo )
{
	return BaseClass::Holster( pSwitchingTo );
}


//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CTFRobotArm::CTFRobotArm()
{
	m_iComboCount = 0;
	m_flLastComboHit = 0.f;
	m_bBigIdle = false;
	m_bBigHit = false;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTFRobotArm::Precache()
{
	BaseClass::Precache();

	extern const char *g_HACK_GunslingerEngineerArmsOverride;
	PrecacheModel( g_HACK_GunslingerEngineerArmsOverride );
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
#ifdef GAME_DLL
void CTFRobotArm::Equip( CBaseCombatCharacter* pOwner )
{
	BaseClass::Equip( pOwner );

	if ( !IsPDQ() )
		return;

	CTFWearable* pArmItem = dynamic_cast<CTFWearable*>( CreateEntityByName( "tf_wearable_robot_arm" ) );
	if ( pArmItem )
	{
		pArmItem->AddSpawnFlags( SF_NORESPAWN );
		pArmItem->SetAlwaysAllow( true );
		DispatchSpawn( pArmItem );
		pArmItem->GiveTo( pOwner );
		pArmItem->AddHiddenBodyGroup( "rightarm" );
		pArmItem->SetOwnerEntity( pOwner );
		m_hRobotArm.Set( pArmItem );
	}
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTFRobotArm::Drop( const Vector &vecVelocity )
{
	RemoveRobotArm();

	BaseClass::Drop( vecVelocity );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFRobotArm::UpdateOnRemove( void )
{
	RemoveRobotArm();

	BaseClass::UpdateOnRemove();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFRobotArm::RemoveRobotArm( void )
{
	if ( m_hRobotArm )
	{
		m_hRobotArm->RemoveFrom( GetOwnerEntity() );
		m_hRobotArm = NULL;
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFRobotArm::OnActiveStateChanged( int iOldState )
{
	if ( m_iState == WEAPON_NOT_CARRIED )
	{
		RemoveRobotArm();
	}
}

#endif

// -----------------------------------------------------------------------------
// Purpose:
// -----------------------------------------------------------------------------
void CTFRobotArm::PrimaryAttack()
{
	CTFPlayer *pPlayer = GetTFPlayerOwner();
	if ( !pPlayer )
		return;

	if ( gpGlobals->curtime - m_flLastComboHit > ROBOARM_COMBO_TIMEOUT )
	{
		m_iComboCount = 0;
	}

	if ( m_iComboCount == 2 && CanAttack() )
	{
		pPlayer->m_Shared.SetNextMeleeCrit( MELEE_CRIT );
	}

	BaseClass::PrimaryAttack();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFRobotArm::Smack( void )
{
	CTFPlayer *pPlayer = GetTFPlayerOwner();
	if ( !pPlayer )
		return;

	trace_t trace;
	bool btrace = DoSwingTrace( trace );
	if ( btrace && trace.DidHitNonWorldEntity() && trace.m_pEnt && trace.m_pEnt->IsPlayer() &&
		 trace.m_pEnt->GetTeamNumber() != pPlayer->GetTeamNumber() )
	{
		m_iComboCount++;
		m_flLastComboHit = gpGlobals->curtime;

		if ( m_iComboCount == 3 )
		{
			m_iComboCount = 0;
			m_bBigIdle = true;
			m_bBigHit = true;
		}
	}
	else
	{
		m_iComboCount = 0;
	}

	BaseClass::Smack();

	m_bBigHit = false;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFRobotArm::DoViewModelAnimation( void )
{
	if ( m_iComboCount == 2 )
	{
		SendWeaponAnim( ACT_ITEM2_VM_SWINGHARD );
	}
	else
	{
		SendWeaponAnim( ACT_ITEM2_VM_HITCENTER );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
#ifdef GAME_DLL
int CTFRobotArm::GetDamageCustom()
{
	if ( m_bBigHit )
	{
		return TF_DMG_CUSTOM_COMBO_PUNCH;
	}
	else
	{
		return BaseClass::GetDamageCustom();
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
float CTFRobotArm::GetForceScale( void )
{
	if ( m_bBigHit )
	{
		return 500.f;
	}
	else
	{
		return BaseClass::GetForceScale();
	}
}

#endif

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFRobotArm::WeaponIdle( void )
{
#ifdef GAME_DLL
	if ( m_bBigIdle )
	{
		m_bBigIdle = false;
		SendWeaponAnim( ACT_ITEM2_VM_IDLE_2 );
		m_flTimeWeaponIdle = gpGlobals->curtime + SequenceDuration();
		return;
	}
#endif

	BaseClass::WeaponIdle();
}