summaryrefslogtreecommitdiff
path: root/game/shared/tf/tf_weapon_lunchbox.cpp
blob: bfb646e3a9d3aec40292dbcc8fd140ff8dbcc936 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
//
//=============================================================================
#include "cbase.h"
#include "tf_weapon_lunchbox.h"
#include "tf_fx_shared.h"

// Client specific.
#ifdef CLIENT_DLL
#include "c_tf_player.h"
// Server specific.
#else
#include "tf_player.h"
#include "entity_healthkit.h"
#include "econ_item_view.h"
#include "econ_item_system.h"
#include "tf_gamestats.h"
#endif

//=============================================================================
//
// Weapon Lunchbox tables.
//
IMPLEMENT_NETWORKCLASS_ALIASED( TFLunchBox, DT_WeaponLunchBox )

BEGIN_NETWORK_TABLE( CTFLunchBox, DT_WeaponLunchBox )
END_NETWORK_TABLE()

BEGIN_PREDICTION_DATA( CTFLunchBox )
END_PREDICTION_DATA()

LINK_ENTITY_TO_CLASS( tf_weapon_lunchbox, CTFLunchBox );
PRECACHE_WEAPON_REGISTER( tf_weapon_lunchbox );

//=============================================================================

IMPLEMENT_NETWORKCLASS_ALIASED( TFLunchBox_Drink, DT_TFLunchBox_Drink )

BEGIN_NETWORK_TABLE( CTFLunchBox_Drink, DT_TFLunchBox_Drink )
END_NETWORK_TABLE()

BEGIN_PREDICTION_DATA( CTFLunchBox_Drink )
END_PREDICTION_DATA()

LINK_ENTITY_TO_CLASS( tf_weapon_lunchbox_drink, CTFLunchBox_Drink );
PRECACHE_WEAPON_REGISTER( tf_weapon_lunchbox_drink );

// Server specific.
#ifndef CLIENT_DLL
BEGIN_DATADESC( CTFLunchBox )
END_DATADESC()
#endif

#define LUNCHBOX_DROP_MODEL  "models/items/plate.mdl"
#define LUNCHBOX_STEAK_DROP_MODEL  "models/items/plate_steak.mdl"
#define LUNCHBOX_ROBOT_DROP_MODEL  "models/items/plate_robo_sandwich.mdl"
#define LUNCHBOX_FESTIVE_DROP_MODEL  "models/items/plate_sandwich_xmas.mdl"
#define LUNCHBOX_CHOCOLATE_BAR		"models/props_halloween/halloween_medkit_small.mdl"

#define LUNCHBOX_DROPPED_MINS	Vector( -17, -17, -10 )
#define LUNCHBOX_DROPPED_MAXS	Vector( 17, 17, 10 )

static const char *s_pszLunchboxMaxHealThink = "LunchboxMaxHealThink";

//=============================================================================
//
// Weapon Lunchbox functions.
//

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CTFLunchBox::CTFLunchBox()
{
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTFLunchBox::UpdateOnRemove( void )
{
#ifndef CLIENT_DLL
	// If we're removed, we remove any dropped powerups. This prevents an exploit
	// where they switch classes away & back to get another lunchbox to drop with.
	if ( m_hThrownPowerup )
	{
		UTIL_Remove( m_hThrownPowerup );
	}
#endif

	BaseClass::UpdateOnRemove();
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTFLunchBox::Precache( void )
{
	if ( DropAllowed() )
	{
		PrecacheModel( "models/items/medkit_medium.mdl" );
		PrecacheModel( "models/items/medkit_medium_bday.mdl" );
		PrecacheModel( LUNCHBOX_DROP_MODEL );
		PrecacheModel( LUNCHBOX_STEAK_DROP_MODEL );
		PrecacheModel( LUNCHBOX_ROBOT_DROP_MODEL );
		PrecacheModel( LUNCHBOX_FESTIVE_DROP_MODEL );
		PrecacheModel( LUNCHBOX_CHOCOLATE_BAR );
	}

	BaseClass::Precache();
}


//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTFLunchBox::WeaponReset( void )
{
	BaseClass::WeaponReset();
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CTFLunchBox::UsesPrimaryAmmo( void )
{
	return CBaseCombatWeapon::UsesPrimaryAmmo();
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CTFLunchBox::DropAllowed( void )
{
	CTFPlayer *pOwner = ToTFPlayer( GetPlayerOwner() );
	if ( pOwner )
	{
		if ( pOwner->m_Shared.InCond( TF_COND_TAUNTING ) )
			return false;
	}

	return true;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTFLunchBox::PrimaryAttack( void )
{
	CTFPlayer *pOwner = ToTFPlayer( GetPlayerOwner() );
	if ( !pOwner )
		return;

	if ( !HasAmmo() )
		return;

#if GAME_DLL
	pOwner->Taunt();
	m_flNextPrimaryAttack = pOwner->GetTauntRemoveTime() + 0.1f;
#else
	m_flNextPrimaryAttack = gpGlobals->curtime + 2.0f; // this will be corrected by the game server
#endif
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTFLunchBox::SecondaryAttack( void )
{
	if ( !DropAllowed() )
		return;

	CTFPlayer *pPlayer = ToTFPlayer( GetPlayerOwner() );
	if ( !pPlayer )
		return;

	if ( !HasAmmo() )
		return;

#ifndef CLIENT_DLL

	if ( m_hThrownPowerup )
	{
		UTIL_Remove( m_hThrownPowerup );
	}

	// Throw out the medikit
	Vector vecSrc = pPlayer->EyePosition() + Vector(0,0,-8);
	QAngle angForward = pPlayer->EyeAngles() + QAngle(-10,0,0);

	int nLunchBoxType = GetLunchboxType();

	const char *pszHealthKit;	
	switch ( nLunchBoxType )
	{
	case LUNCHBOX_ADDS_MAXHEALTH:
		pszHealthKit = "item_healthkit_small";
		break;

	case LUNCHBOX_ADDS_AMMO:
		pszHealthKit = "item_healthammokit";
		break;

	default:
		pszHealthKit = "item_healthkit_medium";
	}

	CHealthKit *pMedKit = assert_cast<CHealthKit*>( CBaseEntity::Create( pszHealthKit, vecSrc, angForward, pPlayer ) );

	if ( pMedKit )
	{
		Vector vecForward, vecRight, vecUp;
		AngleVectors( angForward, &vecForward, &vecRight, &vecUp );
		Vector vecVelocity = vecForward * 500.0;
		
		if ( nLunchBoxType == LUNCHBOX_ADDS_MINICRITS )
		{
			pMedKit->SetModel( LUNCHBOX_STEAK_DROP_MODEL );
		}
		else if ( nLunchBoxType == LUNCHBOX_STANDARD_ROBO )
		{
			pMedKit->SetModel( LUNCHBOX_ROBOT_DROP_MODEL );
			pMedKit->m_nSkin = ( pPlayer->GetTeamNumber() == TF_TEAM_RED ) ? 0 : 1;
		}
		else if ( nLunchBoxType == LUNCHBOX_STANDARD_FESTIVE )
		{
			pMedKit->SetModel( LUNCHBOX_FESTIVE_DROP_MODEL );
			pMedKit->m_nSkin = ( pPlayer->GetTeamNumber() == TF_TEAM_RED ) ? 0 : 1;
		}
		else if ( nLunchBoxType == LUNCHBOX_ADDS_MAXHEALTH )
		{
			pMedKit->SetModel( LUNCHBOX_CHOCOLATE_BAR );
		}
		else
		{
			pMedKit->SetModel( LUNCHBOX_DROP_MODEL );
		}

		// clear out the overrides so the thrown sandvich/steak look correct in either vision mode
		pMedKit->ClearModelIndexOverrides();

		pMedKit->SetAbsAngles( vec3_angle );
		pMedKit->SetSize( LUNCHBOX_DROPPED_MINS, LUNCHBOX_DROPPED_MAXS );

		// the thrower has to wait 0.3 to pickup the powerup (so he can throw it while running forward)
		pMedKit->DropSingleInstance( vecVelocity, pPlayer, 0.3 );
	}

	m_hThrownPowerup = pMedKit;
#endif

	pPlayer->RemoveAmmo( m_pWeaponInfo->GetWeaponData( m_iWeaponMode ).m_iAmmoPerShot, m_iPrimaryAmmoType );
	g_pGameRules->SwitchToNextBestWeapon( pPlayer, this );

	StartEffectBarRegen();
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTFLunchBox::DrainAmmo( bool bForceCooldown )
{
	CTFPlayer *pOwner = ToTFPlayer( GetPlayerOwner() );
	if ( !pOwner )
		return;

#ifdef GAME_DLL

	int iLunchboxType = GetLunchboxType();
	// If we're damaged while eating/taunting, bForceCooldown will be true
	if ( pOwner->IsPlayerClass( TF_CLASS_HEAVYWEAPONS ) )
	{
		if ( pOwner->GetHealth() < pOwner->GetMaxHealth() || GetLunchboxType() == LUNCHBOX_ADDS_MINICRITS || iLunchboxType == LUNCHBOX_ADDS_MAXHEALTH || bForceCooldown )
		{
			StartEffectBarRegen();
		}
		else	// Full health regular sandwhich, I can eat forever
		{	
			return;
		}
	}
	else if ( pOwner->IsPlayerClass( TF_CLASS_SCOUT ) )
	{
		StartEffectBarRegen();
	}

	// Strange Tracking.  Only go through if we have ammo at this point.
	if ( !pOwner->IsBot() && pOwner->GetAmmoCount( m_iPrimaryAmmoType ) > 0 )
	{
		EconEntity_OnOwnerKillEaterEventNoPartner( dynamic_cast<CEconEntity *>( this ), pOwner, kKillEaterEvent_FoodEaten );
	}

	pOwner->RemoveAmmo( 1, m_iPrimaryAmmoType );
#else
	
	pOwner->RemoveAmmo( 1, m_iPrimaryAmmoType );
	StartEffectBarRegen();

#endif
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTFLunchBox::Detach( void )
{
#ifdef GAME_DLL
	// Terrible - but for now, we're the only place that adds this (custom) attribute
	if ( GetLunchboxType() == LUNCHBOX_ADDS_MAXHEALTH )
	{
		CTFPlayer *pOwner = ToTFPlayer( GetPlayerOwner() );
		if ( pOwner )
		{
			// Prevents use-then-switch-class exploit (heavy->scout)
			// Not a big deal in pubs, but it can mess with competitive
			pOwner->RemoveCustomAttribute( "hidden maxhealth non buffed" );
		}
	}
#endif

	BaseClass::Detach();
}

#ifdef GAME_DLL
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTFLunchBox::ApplyBiteEffects( CTFPlayer *pPlayer )
{
	int nLunchBoxType = GetLunchboxType();

	if ( nLunchBoxType == LUNCHBOX_ADDS_MAXHEALTH )
	{
		// add 50 health to player for 30 seconds
		pPlayer->AddCustomAttribute( "hidden maxhealth non buffed", 50.f, 30.f );
	}
	else if ( nLunchBoxType == LUNCHBOX_ADDS_MINICRITS )
	{
		static const float s_fSteakSandwichDuration = 16.0f;

		// Steak sandvich.
		pPlayer->m_Shared.AddCond( TF_COND_ENERGY_BUFF, s_fSteakSandwichDuration );
		pPlayer->m_Shared.AddCond( TF_COND_CANNOT_SWITCH_FROM_MELEE, s_fSteakSandwichDuration );
		pPlayer->m_Shared.SetBiteEffectWasApplied();

		return;
	}
	
	// Then heal the player
	int iHeal = ( nLunchBoxType == LUNCHBOX_ADDS_MAXHEALTH ) ? 25 : 75;
	int iHealType = DMG_GENERIC;
	if ( nLunchBoxType == LUNCHBOX_ADDS_MAXHEALTH && pPlayer->GetHealth() < 400 )
	{
		iHealType = DMG_IGNORE_MAXHEALTH;
		iHeal = Min( 25, 400 - pPlayer->GetHealth() );
	}

	float flHealScale = 1.0f;
	CALL_ATTRIB_HOOK_FLOAT( flHealScale, lunchbox_healing_scale );
	iHeal = iHeal * flHealScale;

	int iHealed = pPlayer->TakeHealth( iHeal, iHealType );

	if ( iHealed > 0 )
	{
		CTF_GameStats.Event_PlayerHealedOther( pPlayer, iHealed );
	}

	// Restore ammo if applicable
	if ( nLunchBoxType == LUNCHBOX_ADDS_AMMO )
	{
		int maxPrimary = pPlayer->GetMaxAmmo( TF_AMMO_PRIMARY );
		pPlayer->GiveAmmo( maxPrimary * 0.25, TF_AMMO_PRIMARY, true );
	}
}
#endif // GAME_DLL

//-----------------------------------------------------------------------------
// Purpose:  Energy Drink
//-----------------------------------------------------------------------------
CTFLunchBox_Drink::CTFLunchBox_Drink()
{
}

#ifdef CLIENT_DLL
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CTFLunchBox_Drink::Holster( CBaseCombatWeapon *pSwitchingTo )
{
	CTFPlayer *pOwner = ToTFPlayer( GetPlayerOwner() );
	if ( pOwner && pOwner->IsLocalPlayer() )
	{
		C_BaseEntity *pParticleEnt = pOwner->GetViewModel(0);
		if ( pParticleEnt )
		{
			pOwner->StopViewModelParticles( pParticleEnt );
		}
	}

	return BaseClass::Holster( pSwitchingTo );
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
const char* CTFLunchBox_Drink::ModifyEventParticles( const char* token )
{
	if ( GetLunchboxType() == LUNCHBOX_ADDS_MINICRITS )
	{
		if ( FStrEq( token, "energydrink_splash") )
		{
			CEconItemView *pItem = m_AttributeManager.GetItem();
			int iSystems = pItem->GetStaticData()->GetNumAttachedParticles( GetTeamNumber() );
			for ( int i = 0; i < iSystems; i++ )
			{
				attachedparticlesystem_t *pSystem = pItem->GetStaticData()->GetAttachedParticleData( GetTeamNumber(),i );
				if ( pSystem->iCustomType == 1 )
				{
					return pSystem->pszSystemName;
				}
			}
		}
	}

	return BaseClass::ModifyEventParticles( token );
}

#endif // CLIENT_DLL