summaryrefslogtreecommitdiff
path: root/game/server/tf2/tf_obj_resupply.cpp
blob: 31fbbb2516f0b0f386ed7717db0f206223a3864b (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Medic's resupply beacon
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"

#include "tf_obj_resupply.h"
#include "engine/IEngineSound.h"
#include "tf_player.h"
#include "tf_team.h"
#include "VGuiScreen.h"
#include "world.h"

#define RESUPPLY_HEAL_AMT				100
#define RESUPPLY_AMMO_AMT				0.25f

// Wall mounted version
#define RESUPPLY_WALL_MODEL				"models/objects/obj_resupply.mdl"
#define RESUPPLY_WALL_MODEL_ALIEN		"models/objects/alien_obj_resupply.mdl"
#define RESUPPLY_WALL_MINS				Vector(-10, -10, -40)
#define RESUPPLY_WALL_MAXS				Vector( 10,  10, 40)

// Ground placed version
#define RESUPPLY_GROUND_MODEL			"models/objects/obj_resupply_ground.mdl"
#define RESUPPLY_GROUND_MODEL_HUMAN		"models/objects/human_obj_resupply_ground.mdl"
#define RESUPPLY_GROUND_MINS			Vector(-20, -20, 0)
#define RESUPPLY_GROUND_MAXS			Vector( 20,  20, 55)

IMPLEMENT_SERVERCLASS_ST( CObjectResupply, DT_ObjectResupply )
END_SEND_TABLE()

LINK_ENTITY_TO_CLASS(obj_resupply, CObjectResupply);
PRECACHE_REGISTER(obj_resupply);

ConVar	obj_resupply_health( "obj_resupply_health","100", FCVAR_NONE, "Resupply Station health" );

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CObjectResupply::CObjectResupply()
{
	m_iHealth = obj_resupply_health.GetInt();
	UseClientSideAnimation();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CObjectResupply::Spawn()
{
	SetModel( RESUPPLY_WALL_MODEL );
	SetSolid( SOLID_BBOX );

	UTIL_SetSize(this, RESUPPLY_WALL_MINS, RESUPPLY_WALL_MAXS);
	m_takedamage = DAMAGE_YES;

	SetType( OBJ_RESUPPLY );
	m_fObjectFlags |= OF_DONT_PREVENT_BUILD_NEAR_OBJ;

	BaseClass::Spawn();
}


//-----------------------------------------------------------------------------
// Spawn the vgui control screens on the object
//-----------------------------------------------------------------------------
void CObjectResupply::GetControlPanelInfo( int nPanelIndex, const char *&pPanelName )
{
	pPanelName = "screen_obj_resupply";
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CObjectResupply::Precache()
{
	BaseClass::Precache();
	PrecacheModel( RESUPPLY_WALL_MODEL );
	PrecacheModel( RESUPPLY_WALL_MODEL_ALIEN );
	PrecacheModel( RESUPPLY_GROUND_MODEL );
	PrecacheModel( RESUPPLY_GROUND_MODEL_HUMAN );
	PrecacheVGuiScreen( "screen_obj_resupply" );

	PrecacheScriptSound( "ObjectResupply.InsufficientFunds" );
	PrecacheScriptSound( "BaseCombatCharacter.AmmoPickup" );
}


//-----------------------------------------------------------------------------
// Resupply Health 
//-----------------------------------------------------------------------------
bool CObjectResupply::ResupplyHealth( CBaseTFPlayer *pPlayer, float flFraction )
{
	// Calculate the amount to heal
	float flAmountToHeal = flFraction * RESUPPLY_HEAL_AMT;
	if (flAmountToHeal > (pPlayer->m_iMaxHealth - pPlayer->m_iHealth))
	{
		flAmountToHeal = (pPlayer->m_iMaxHealth - pPlayer->m_iHealth);
	}

	if ( flAmountToHeal > 0 )
	{
		pPlayer->TakeHealth( flAmountToHeal, 0 );
		return true;
	}

	return false;
}


//-----------------------------------------------------------------------------
// Handle commands sent from vgui panels on the client 
//-----------------------------------------------------------------------------
bool CObjectResupply::ClientCommand( CBaseTFPlayer *pPlayer, const char *pCmd, ICommandArguments *pArg )
{
	// NOTE: Must match ResupplyBuyType_t
	static float s_Costs[] =
	{
		RESUPPLY_AMMO_COST,
		RESUPPLY_HEALTH_COST,
		RESUPPLY_GRENADES_COST,
		RESUPPLY_ALL_COST
	};

	COMPILE_TIME_ASSERT( RESUPPLY_BUY_TYPE_COUNT == 4 );

	if ( FStrEq( pCmd, "buy" ) )
	{
		if ( pArg->Argc() < 2 )
			return true;

		// I can't do anything if I'm not active
		if ( !ShouldBeActive() ) 
			return true;

		// Do we have enough resources to activate it?
		if (pPlayer->GetBankResources() <= 0)
		{
			// Play a sound indicating it didn't work...
			CSingleUserRecipientFilter filter( pPlayer );
			EmitSound( filter, pPlayer->entindex(), "ObjectResupply.InsufficientFunds" );
			return true;
		}

		bool bUsedResupply = false;
		ResupplyBuyType_t type = (ResupplyBuyType_t)atoi( pArg->Argv(1) );
		if (type >= RESUPPLY_BUY_TYPE_COUNT)
			return true;

		// Get the potential cost.
		float flCost = s_Costs[type];
//		flCost += pPlayer->ClassCostAdjustment( type );

		float flFraction = pPlayer->GetBankResources() / flCost;
		if (flFraction > 1.0f)
			flFraction = 1.0f;

		switch( type )
		{
		case RESUPPLY_BUY_HEALTH:
			// Calculate the amount to heal
			if (ResupplyHealth(pPlayer, flFraction))
			{
				bUsedResupply = true;
			}
			break;

		case RESUPPLY_BUY_AMMO:
			// Refill the player's ammo too
			if (pPlayer->ResupplyAmmo( flFraction * RESUPPLY_AMMO_AMT, RESUPPLY_AMMO_FROM_STATION ))
			{
				bUsedResupply = true;
			}
			break;

		case RESUPPLY_BUY_GRENADES:
			// Refill the player's ammo too
			if (pPlayer->ResupplyAmmo( flFraction * RESUPPLY_AMMO_AMT, RESUPPLY_GRENADES_FROM_STATION ))
			{
				bUsedResupply = true;
			}
			break;

		case RESUPPLY_BUY_ALL:
			// Calculate the amount to heal
			if (ResupplyHealth(pPlayer, flFraction))
			{
				bUsedResupply = true;
			}

			// Refill the player's ammo too
			if (pPlayer->ResupplyAmmo( flFraction * RESUPPLY_AMMO_AMT, RESUPPLY_ALL_FROM_STATION ))
			{
				bUsedResupply = true;
			}
			break;
		}

		if (bUsedResupply)
		{
			// Play an ammo pickup just to this player
			CSingleUserRecipientFilter filter( pPlayer );
			pPlayer->EmitSound( filter, pPlayer->entindex(), "BaseCombatCharacter.AmmoPickup" );

			pPlayer->RemoveBankResources( flFraction * flCost );
		}

		return true;
	}

	return BaseClass::ClientCommand( pPlayer, pCmd, pArg );
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CObjectResupply::DestroyObject( void )
{
	if ( GetTeam() )
	{
		((CTFTeam*)GetTeam())->RemoveResupply( this );
	}
	BaseClass::DestroyObject();
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *pTeam - 
//-----------------------------------------------------------------------------
void CObjectResupply::ChangeTeam( int iTeamNum )
{
	CTFTeam *pExisting = (CTFTeam*)GetTeam();
	CTFTeam *pTeam = (CTFTeam*)GetGlobalTeam( iTeamNum );

	// Already on this team
	if ( GetTeamNumber() == iTeamNum )
		return;

	if ( pExisting )
	{
		// Remove it from current team ( if it's in one ) and give it to new team
		pExisting->RemoveResupply( this );
	}
		
	// Change to new team
	BaseClass::ChangeTeam( iTeamNum );
	
	// Add this object to the team's list
	if (pTeam)
	{
		pTeam->AddResupply( this );
	}
}


//-----------------------------------------------------------------------------
// Purpose: Resupply always wants to use the wall mount for attachment points
//-----------------------------------------------------------------------------
void CObjectResupply::SetupAttachedVersion( void )
{
	BaseClass::SetupAttachedVersion();

	if ( GetTeamNumber() == TEAM_ALIENS )
	{
		SetModel( RESUPPLY_WALL_MODEL_ALIEN );
	}
	else
	{
		SetModel( RESUPPLY_WALL_MODEL );
	}

	UTIL_SetSize(this, RESUPPLY_WALL_MINS, RESUPPLY_WALL_MAXS);
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CObjectResupply::CalculatePlacement( CBaseTFPlayer *pPlayer )
{
	trace_t tr;
	Vector vecAiming;
	// Get an aim vector. Don't use GetAimVector() because we don't want autoaiming.
	Vector vecSrc = pPlayer->Weapon_ShootPosition( );
	pPlayer->EyeVectors( &vecAiming );
	Vector vecTarget;
	VectorMA( vecSrc, 90, vecAiming, vecTarget );
	m_vecBuildOrigin = vecTarget;

	// Angle it towards me
	Vector vecForward = pPlayer->WorldSpaceCenter() - m_vecBuildOrigin;
	SetLocalAngles( QAngle( 0, UTIL_VecToYaw( vecForward ), 0 ) );

	// Is there something to attach to? 
	// Use my bounding box, not the build box, so I fit to the wall
	UTIL_TraceLine( vecSrc, vecTarget, MASK_SOLID, pPlayer, COLLISION_GROUP_PLAYER_MOVEMENT, &tr);
	//UTIL_TraceHull( vecSrc, vecTarget, WorldAlignMins(), WorldAlignMaxs(), MASK_SOLID, pPlayer, TFCOLLISION_GROUP_OBJECT, &tr );
	m_vecBuildOrigin = tr.endpos;
	bool bTryToPlaceGroundVersion = false;
	if ( tr.allsolid || (tr.fraction == 1.0) )
	{
		bTryToPlaceGroundVersion = true;
	}
	else 
	{
		// Make sure we're planting on the world
		CBaseEntity *pEntity = tr.m_pEnt;
		if ( pEntity != GetWorldEntity() )
		{
			bTryToPlaceGroundVersion = true;
		}
	}

	// Make sure the wall we've touched is vertical
	if ( !bTryToPlaceGroundVersion && fabs(tr.plane.normal.z) > 0.3 )
	{
		bTryToPlaceGroundVersion = true;
	}

	// Aborting?
	if ( bTryToPlaceGroundVersion )
	{
		// We couldn't find a wall, so try and place a ground version instead
		if ( GetTeamNumber() == TEAM_HUMANS )
		{
			SetModel( RESUPPLY_GROUND_MODEL_HUMAN );
		}
		else
		{
			SetModel( RESUPPLY_GROUND_MODEL );
		}
		UTIL_SetSize(this, RESUPPLY_GROUND_MINS, RESUPPLY_GROUND_MAXS);
		m_vecBuildMins = WorldAlignMins() - Vector( 4,4,0 );
		m_vecBuildMaxs = WorldAlignMaxs() + Vector( 4,4,0 );
		return BaseClass::CalculatePlacement( pPlayer );
	}

	SetupAttachedVersion();
	m_vecBuildMins = WorldAlignMins() - Vector( 4,4,0 );
	m_vecBuildMaxs = WorldAlignMaxs() + Vector( 4,4,0 );

	// Set the angles
	vecForward = tr.plane.normal;
	SetLocalAngles( QAngle( 0, UTIL_VecToYaw( vecForward ), 0 ) );

	// Trace back from the corners
	Vector vecMins, vecMaxs, vecModelMins, vecModelMaxs;
	const model_t *pModel = GetModel();
	modelinfo->GetModelBounds( pModel, vecModelMins, vecModelMaxs );

	// Check the four build points
	Vector vecPointCheck = (vecForward * 32);
	Vector vecUp = Vector(0,0,1);
	Vector vecRight;
	CrossProduct( vecUp, vecForward, vecRight );
	float flWidth = fabs(vecModelMaxs.y - vecModelMins.y) * 0.5;
	float flHeight = fabs(vecModelMaxs.z - vecModelMins.z) * 0.5;

	bool bResult = true;
	if ( bResult )
	{
		bResult = CheckBuildPoint( m_vecBuildOrigin + (vecRight * flWidth) + (vecUp * flHeight), vecPointCheck );
	}
	if ( bResult )
	{
		bResult = CheckBuildPoint( m_vecBuildOrigin + (vecRight * flWidth) - (vecUp * flHeight), vecPointCheck );
	}
	if ( bResult )
	{
		bResult = CheckBuildPoint( m_vecBuildOrigin - (vecRight * flWidth) + (vecUp * flHeight), vecPointCheck );
	}
	if ( bResult )
	{
		bResult = CheckBuildPoint( m_vecBuildOrigin - (vecRight * flWidth) - (vecUp * flHeight), vecPointCheck );
	}

	AttemptToFindPower();

	return bResult;
}