summaryrefslogtreecommitdiff
path: root/game/server/tf2/basecombatcharacter_tf2.cpp
blob: 453856a8abf2c71ee52082b09d8dd6d292c2ca83 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: TF2 specific CBaseCombatCharacter code.
//
//=============================================================================//
#include "cbase.h"
#include "basecombatcharacter.h"
#include "engine/IEngineSound.h"
#include "tf_player.h"
#include "tf_stats.h"

extern char *g_pszEMPPulseStart;

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CBaseCombatCharacter::HasPowerup( int iPowerup )
{
	Assert( iPowerup >= 0 && iPowerup < MAX_POWERUPS );
	return ( m_iPowerups & (1 << iPowerup) ) != 0;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CBaseCombatCharacter::CanPowerupEver( int iPowerup )
{
	Assert( iPowerup >= 0 && iPowerup < MAX_POWERUPS );

	// Only objects use power
	if ( iPowerup == POWERUP_POWER )
		return false;

	// Accept everything else
	return true;
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CBaseCombatCharacter::CanPowerupNow( int iPowerup )
{
	Assert( iPowerup >= 0 && iPowerup < MAX_POWERUPS );

	if ( !CanPowerupEver(iPowerup) )
		return false;

	switch( iPowerup )
	{
	case POWERUP_BOOST:
		{
			// Am I taking EMP damage, or is a technician trying to drain me?
			if ( HasPowerup( POWERUP_EMP ) || ( (m_flPowerupAttemptTimes[POWERUP_EMP] + 0.5) > gpGlobals->curtime ) )
			{
				// Reduce EMP time
				m_flPowerupEndTimes[POWERUP_EMP] -= 0.05;

				// Don't apply any boost effects
				return false;
			}
		}
		break;

	case POWERUP_EMP:
		{
			// Was I just boosted? If so, I don't take EMP damage for a bit
			if ( (m_flPowerupAttemptTimes[POWERUP_BOOST] + 0.5) > gpGlobals->curtime )
				return false;
		}
		break;

	default:
		break;
	}

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CBaseCombatCharacter::SetPowerup( int iPowerup, bool bState, float flTime, float flAmount, CBaseEntity *pAttacker, CDamageModifier *pDamageModifier )
{
	Assert( iPowerup >= 0 && iPowerup < MAX_POWERUPS );

	// Some powerups trigger their on state continuously, as opposed to turning it on for some time.
	bool bTriggerStart = ( bState && !HasPowerup( iPowerup ) );
	if ( bState && iPowerup == POWERUP_BOOST )
	{
		// Health boost always triggers
		bTriggerStart = true;
	}

	bool bHadPowerup = false;
	if ( HasPowerup( iPowerup ) && !bState )
	{
		bHadPowerup = true;
	}

	if ( bState )
	{
		m_iPowerups |= (1 << iPowerup);
	}
	else
	{
		m_iPowerups &= ~(1 << iPowerup);
	}

	// Fire start/end triggers
	if ( bTriggerStart  )
	{
		PowerupStart( iPowerup, flAmount, pAttacker, pDamageModifier );
	}
	else if ( bHadPowerup )
	{
		PowerupEnd( iPowerup );
	}

	// If we've got an active powerup, keep thinking
	if ( m_iPowerups )
	{
		SetContextThink( PowerupThink, gpGlobals->curtime + 0.1, POWERUP_THINK_CONTEXT );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CBaseCombatCharacter::PowerupThink( void )
{
	// If we don't have any powerups, stop thinking
	if ( !m_iPowerups )
		return;

	// Check all the powerups
	for ( int i = 0; i < MAX_POWERUPS; i++ )
	{
		// Don't check power, because it never runs out naturally
		if ( i == POWERUP_POWER )
			continue;

		if ( m_iPowerups & (1 << i) )
		{
			// Should it finish now?
			if ( m_flPowerupEndTimes[i] < gpGlobals->curtime )
			{
				SetPowerup( i, false );
			}
		}
	}

	SetNextThink( gpGlobals->curtime + 0.1, POWERUP_THINK_CONTEXT );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CBaseCombatCharacter::AttemptToPowerup( int iPowerup, float flTime, float flAmount, CBaseEntity *pAttacker, CDamageModifier *pDamageModifier )
{
	Assert( iPowerup >= 0 && iPowerup < MAX_POWERUPS );

	// Ignore it if I'm dead
	if ( !IsAlive() )
		return false;

	m_flPowerupAttemptTimes[iPowerup] = gpGlobals->curtime;

	// If we can't be powerup this type, abort
	if ( !CanPowerupNow( iPowerup ) )
		return false;

	// Get the correct duration
	flTime = PowerupDuration( iPowerup, flTime );
	m_flPowerupEndTimes[iPowerup] = MAX( m_flPowerupEndTimes[iPowerup], gpGlobals->curtime + flTime );

	// Turn it on
	SetPowerup( iPowerup, true, flTime, flAmount, pAttacker, pDamageModifier );

	// Add the damage modifier to the player
	if ( pDamageModifier )
	{
		pDamageModifier->AddModifierToEntity( this );
	}

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: Powerup has just started
//-----------------------------------------------------------------------------
void CBaseCombatCharacter::PowerupStart( int iPowerup, float flAmount, CBaseEntity *pAttacker, CDamageModifier *pDamageModifier )
{
	Assert( iPowerup >= 0 && iPowerup < MAX_POWERUPS );

	switch( iPowerup )
	{
	case POWERUP_BOOST:
		{
			// Players can be boosted over their max
			int iMaxBoostedHealth;
			if ( IsPlayer() )
			{
				iMaxBoostedHealth = GetMaxHealth() + GetMaxHealth() / 2;
			}
			else
			{
				iMaxBoostedHealth = GetMaxHealth();
			}

			// Can we boost health further?
			if ( GetHealth() < iMaxBoostedHealth )
			{
				int maxHealthToAdd = iMaxBoostedHealth - GetHealth();

				// It uses floating point in here so it doesn't lose the fractional healing part on small frame times.
				float flHealthToAdd = flAmount + m_flFractionalBoost;
				int nHealthToAdd = (int)flHealthToAdd;
				m_flFractionalBoost = flHealthToAdd - nHealthToAdd; 
				if ( nHealthToAdd )
				{
					int nHealthAdded = MIN( nHealthToAdd, maxHealthToAdd );
					if ( IsPlayer() )
					{
						((CBaseTFPlayer*)this)->TakeHealthBoost( nHealthAdded, GetMaxHealth(), 25 );
					}
					else
					{
						TakeHealth( nHealthAdded, DMG_GENERIC );
					}

					TFStats()->IncrementPlayerStat( pAttacker, TF_PLAYER_STAT_HEALTH_GIVEN, nHealthAdded );
				}
			}
		}
		break;

	case POWERUP_EMP:
		{
			// EMP removes adrenalin rush
			SetPowerup( POWERUP_RUSH, false );
		}
		break;

	default:
		break;
	}
}