summaryrefslogtreecommitdiff
path: root/game/server/tf2/fire_damage_mgr.cpp
blob: 4d15084fe18aaa6452197aea0b25b293a2b9715f (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#include "cbase.h"
#include "fire_damage_mgr.h"
#include "entity_burn_effect.h"
#include "gasoline_blob.h"
#include "tf_obj.h"
#include "ai_basenpc.h"
#include "tf_gamerules.h"


#define FIRE_DAMAGE_APPLY_INTERVAL	0.5	// Apply the damage at this interval.
#define FIRE_DECAY_END_VALUE		0.00001


// No more damage from fire can be applied to a player per second.
#define MAX_FIRE_DAMAGE_PER_SECOND	15

// The fire heat uses exponential decay. It goes from MAX_FIRE_DAMAGE_PER_SECOND to
// FIRE_DECAY_END_VALUE in FIRE_DECAY_SECONDS.
#define FIRE_DECAY_SECONDS			3


ConVar fire_damageall( "fire_damageall", "0", 0, "Enable fire damaging team members." );


bool CFireDamageMgr::Init()
{
	m_flApplyDamageCountdown = FIRE_DAMAGE_APPLY_INTERVAL;

	// Fire decays exponentially: B = A * e^(-kt)
	// So we set B=FIRE_DECAY_END_VALUE, A=flMaxDamagePerSecond, and t=flFireDecaySeconds, then solve for K.
	m_flMaxDamagePerSecond = MAX_FIRE_DAMAGE_PER_SECOND;
	m_flDecayConstant = -log( FIRE_DECAY_END_VALUE / m_flMaxDamagePerSecond ) / FIRE_DECAY_SECONDS;

	return true;
}


void CFireDamageMgr::AddDamage( CBaseEntity *pTarget, CBaseEntity *pAttacker, float flDamageAccel, bool bMakeBurnEffect )
{
	FOR_EACH_LL( m_DamageEnts, iDamageEnt )
	{
		CDamageEnt *pEnt = &m_DamageEnts[iDamageEnt];

		if ( pEnt->m_hEnt != pTarget )
			continue;
	
		for ( int i=0; i < pEnt->m_nAttackers; i++ )
		{
			if ( pEnt->m_Attackers[i].m_hAttacker == pAttacker )
			{
				pEnt->m_Attackers[i].m_flVelocity += flDamageAccel * gpGlobals->frametime;
				return;
			}
		}

		
		if ( pEnt->m_nAttackers < CDamageEnt::MAX_ATTACKERS )
		{
			// Add a new attacker.
			pEnt->m_Attackers[pEnt->m_nAttackers].Init( pAttacker, flDamageAccel * gpGlobals->frametime );
			++pEnt->m_nAttackers;
			return;
		}
		else
		{
			// No room for more attackers.
			Warning( "CFireDamageMgr: ran out of attackers\n" );
			return;
		}
	}

	// Add a new CDamageEnt.
	int iNew = m_DamageEnts.AddToTail();
	CDamageEnt *pEnt = &m_DamageEnts[iNew];
	pEnt->m_hEnt = pTarget;
	pEnt->m_bWasAlive = pTarget->IsAlive();
	pEnt->m_nAttackers = 1;
	pEnt->m_Attackers[0].Init( pAttacker, flDamageAccel * gpGlobals->frametime );
	if ( bMakeBurnEffect )
		pEnt->m_pBurnEffect = CEntityBurnEffect::Create( pTarget );
	else
		pEnt->m_pBurnEffect = NULL;
}


void CFireDamageMgr::RemoveDamageEnt( int iEnt )
{
	UTIL_Remove( m_DamageEnts[iEnt].m_pBurnEffect );
	m_DamageEnts.Remove( iEnt );
}


void CFireDamageMgr::FrameUpdatePostEntityThink()
{
	VPROF( "CFireDamageMgr::FrameUpdatePostEntityThink" );
	float frametime = gpGlobals->frametime;
	
	// Update the damage countdown.
	m_flApplyDamageCountdown -= gpGlobals->frametime;
	bool bApplyDamageThisFrame = false;
	if ( m_flApplyDamageCountdown <= 0 )
	{
		bApplyDamageThisFrame = true;
		m_flApplyDamageCountdown += FIRE_DAMAGE_APPLY_INTERVAL;
	}


	//   													   (-kt)
	// Figure out how much all the damage decays this frame:  e
	float flFrameDecay = pow( 2.718281828459045235360, -m_flDecayConstant * frametime );


	int iNext;
	for ( int iCur = m_DamageEnts.Head(); iCur != m_DamageEnts.InvalidIndex(); iCur = iNext )
	{
		iNext = m_DamageEnts.Next( iCur );
		CDamageEnt *pEnt = &m_DamageEnts[iCur];


		// If the entity was dead and is now alive, stop damage to them so their new body doesn't burn.
		if ( !pEnt->m_hEnt.Get() || ( !pEnt->m_bWasAlive && pEnt->m_hEnt->IsAlive() ) )
		{
			RemoveDamageEnt( iCur );
			pEnt = NULL;
			continue;
		}
		
		pEnt->m_bWasAlive = pEnt->m_hEnt->IsAlive();

		// Sum up each attacker's velocity.
		float flTotalVelocity = 0;
		for ( int i=0; i < pEnt->m_nAttackers; i++ )
			flTotalVelocity += pEnt->m_Attackers[i].m_flVelocity;


		// Figure out each attacker's contribution.
		float flContributionPercent[CDamageEnt::MAX_ATTACKERS];
		for ( i=0; i < pEnt->m_nAttackers; i++ )
			flContributionPercent[i] = pEnt->m_Attackers[i].m_flVelocity / flTotalVelocity;
		
		
		// Decay each attacker's velocity.
		flTotalVelocity *= flFrameDecay;

		// Uniformly scale each attacker's velocity down so the sum total doesn't exceed our maximum.
		float flPercentScale = 1;
		if ( flTotalVelocity > m_flMaxDamagePerSecond )
			flPercentScale = m_flMaxDamagePerSecond / flTotalVelocity;

		for ( i=0; i < pEnt->m_nAttackers; i++ )
		{
			CDamageAttacker *pAttacker = &pEnt->m_Attackers[i];

			pAttacker->m_flVelocity *= flFrameDecay * flPercentScale;

			bool bEntsValid = (pEnt->m_Attackers[i].m_hAttacker.Get() != NULL);
			if ( !bEntsValid ||
				pEnt->m_Attackers[i].m_flVelocity <= 0.001 )
			{
				if ( bEntsValid )
					ApplyCollectedDamage( pEnt, i );	// Apply the last-remaining damage from this guy.

				Q_memmove( &pEnt->m_Attackers[i], &pEnt->m_Attackers[i+1], sizeof( pEnt->m_Attackers[0] ) * (pEnt->m_nAttackers-i-1) );
				Q_memmove( &flContributionPercent[i], &flContributionPercent[i+1], sizeof( flContributionPercent[0] ) * (pEnt->m_nAttackers-i-1) );
				
				--pEnt->m_nAttackers;
				if ( pEnt->m_nAttackers == 0 )
				{
					// This ent isn't being damaged anymore.
					RemoveDamageEnt( iCur );
					break;
				}

				--i;
			}

			// Update their current damage sum and maybe apply the damage.
			pAttacker->m_flDamageSum += pAttacker->m_flVelocity * frametime;
			if ( bApplyDamageThisFrame )
			{
				ApplyCollectedDamage( pEnt, i );
			}
		}
	}
}


float GetFireDamageScale( CBaseEntity *pEnt )
{
	// Objects have a lot more health and we want them to take damage faster.
	if ( dynamic_cast< CBaseObject* >( pEnt ) )
		return 4;
	else
		return 1;
}


void CFireDamageMgr::ApplyCollectedDamage( CFireDamageMgr::CDamageEnt *pEnt, int iAttacker )
{
	CDamageAttacker *pAttacker = &pEnt->m_Attackers[iAttacker];

	CTakeDamageInfo info( NULL, pAttacker->m_hAttacker, pAttacker->m_flDamageSum * GetFireDamageScale( pEnt->m_hEnt ), DMG_BURN );
	pEnt->m_hEnt->TakeDamage( info );

	pAttacker->m_flDamageSum = 0;
}


// ------------------------------------------------------------------------------------------------ //
// Global functions.
// ------------------------------------------------------------------------------------------------ //

bool IsBurnableEnt( CBaseEntity *pEntity, int iIgnoreTeam )
{
	if ( pEntity->m_takedamage == DAMAGE_NO )
		return false;

	CGasolineBlob *pBlob = dynamic_cast< CGasolineBlob* >( pEntity );
	if ( pBlob )
	{
		return !pBlob->IsLit();
	}

	if ( pEntity->GetTeamNumber() == iIgnoreTeam && !fire_damageall.GetInt() )
	{
		// Don't damage anyone on the pyro's team (including the pyro himself).
		return false;
	}

	// Now only allow specific types of objects to be damaged.
	if ( dynamic_cast< CBasePlayer* >( pEntity ) || 
		dynamic_cast< CAI_BaseNPC* >( pEntity ) || 
		dynamic_cast< CBaseObject* >( pEntity ) )
	{
		return true;
	}

	return false;
}


int FindBurnableEntsInSphere(
	CBaseEntity **ents,
	float *dists,
	int nMaxEnts,
	const Vector &vecCenter,
	float flSearchRadius,
	CBaseEntity *pOwner )
{
	Assert( nMaxEnts > 0 );
	int nOutEnts = 0;

	CBaseEntity *pEntity;
	for ( CEntitySphereQuery sphere( vecCenter, flSearchRadius ); ( pEntity = sphere.GetCurrentEntity() ) != NULL; sphere.NextEntity() )
	{
		if ( !IsBurnableEnt( pEntity, pOwner->GetTeamNumber() ) )
			continue;
	
		// Make sure it's not blocked.
		trace_t tr;
		Vector vCenter = pEntity->WorldSpaceCenter();
		
		UTIL_TraceLine ( vecCenter, vCenter, MASK_SHOT & (~CONTENTS_HITBOX), NULL, COLLISION_GROUP_NONE, &tr );
		if ( tr.fraction != 1.0 && tr.m_pEnt != pEntity )
			continue;

		if ( TFGameRules()->IsTraceBlockedByWorldOrShield( vecCenter, vCenter, pOwner, DMG_BURN | DMG_PROBE, &tr ) )
			continue;

		// Make sure it's in range.
		const Vector &mins = pEntity->WorldAlignMins();
		const Vector &maxs = pEntity->WorldAlignMaxs();
		float approxTargetRadius = ( Vector( maxs.x, maxs.y, 0 ) - Vector( mins.x, mins.y, 0 )).Length() * 0.5f;

		float flDistFromCenter = ( vecCenter - tr.endpos ).Length() - approxTargetRadius;

		ents[nOutEnts] = pEntity;
		dists[nOutEnts] = flDistFromCenter;
		nOutEnts++;
		if ( nOutEnts >= nMaxEnts )
			return nOutEnts;
	}

	return nOutEnts;
}


CFireDamageMgr g_FireDamageMgr;

CFireDamageMgr* GetFireDamageMgr()
{
	return &g_FireDamageMgr;
}