summaryrefslogtreecommitdiff
path: root/game/server/tf/halloween/merasmus/merasmus_behavior/merasmus_attack.cpp
blob: 9a40377ea6e2b431a98b7242508754d774fa382e (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
//
//
//=============================================================================
#include "cbase.h"

#include "tf_player.h"
#include "tf_gamerules.h"
#include "tf_team.h"
#include "nav_mesh/tf_nav_area.h"
#include "particle_parse.h"
#include "team_control_point_master.h"

#include "../merasmus.h"
#include "../merasmus_trick_or_treat_prop.h"
#include "merasmus_attack.h"
#include "merasmus_staff_attack.h"
#include "merasmus_stunned.h"
#include "merasmus_throwing_grenade.h"
#include "merasmus_zap.h"

//----------------------------------------------------------------------------------
ActionResult< CMerasmus >	CMerasmusAttack::OnStart( CMerasmus *me, Action< CMerasmus > *priorAction )
{
	// smooth out the bot's path following by moving toward a point farther down the path
	m_path.SetMinLookAheadDistance( 100.0f );

	// start animation
	me->GetBodyInterface()->StartActivity( ACT_MP_RUN_ITEM1 );

	m_attackTimer.Invalidate();

	m_attackTarget = NULL;
	m_attackTargetFocusTimer.Start( 0.f );

	RandomGrenadeTimer();
	RandomZapTimer();

	m_homePos = me->GetAbsOrigin();
	m_homePosRecalcTimer.Start( 3.0f );

	m_bombHeadTimer.Start( 10.f );

	return Continue();
}


//----------------------------------------------------------------------------------
bool CMerasmusAttack::IsPotentiallyChaseable( CMerasmus *me, CTFPlayer *victim )
{
	if ( !victim )
	{
		return false;
	}

	if ( !victim->IsAlive() )
	{
		// victim is dead - pick a new one
		return false;
	}

	CTFNavArea *victimArea = (CTFNavArea *)victim->GetLastKnownArea();
	if ( !victimArea || victimArea->HasAttributeTF( TF_NAV_SPAWN_ROOM_BLUE | TF_NAV_SPAWN_ROOM_RED ) )
	{
		// unreachable - pick a new victim
		return false;
	}

	if ( victim->GetGroundEntity() != NULL )
	{
		Vector victimAreaPos;
		victimArea->GetClosestPointOnArea( victim->GetAbsOrigin(), &victimAreaPos );
		if ( ( victim->GetAbsOrigin() - victimAreaPos ).AsVector2D().IsLengthGreaterThan( 50.0f ) )
		{
			// off the mesh and unreachable - pick a new victim
			return false;
		}
	}

	if ( victim->m_Shared.IsInvulnerable() )
	{
		// invulnerable - pick a new victim
		return false;
	}

	Vector toHome = m_homePos - victim->GetAbsOrigin();
	if ( toHome.IsLengthGreaterThan( tf_merasmus_chase_range.GetFloat() ) )
	{
		// too far from home - pick a new victim
		return false;
	}

	return true;
}


//----------------------------------------------------------------------------------
void CMerasmusAttack::SelectVictim( CMerasmus *me )
{
	if ( IsPotentiallyChaseable( me, m_attackTarget ) && !m_attackTargetFocusTimer.IsElapsed() )
	{
		return;
	}

	// pick a new victim to chase
	CTFPlayer *newVictim = NULL;
	CUtlVector< CTFPlayer * > playerVector;

	// collect everyone
	CollectPlayers( &playerVector, TF_TEAM_RED, COLLECT_ONLY_LIVING_PLAYERS );
	CollectPlayers( &playerVector, TF_TEAM_BLUE, COLLECT_ONLY_LIVING_PLAYERS, APPEND_PLAYERS );

	float victimRangeSq = FLT_MAX;
	for( int i=0; i<playerVector.Count(); ++i )
	{
		if ( !IsPotentiallyChaseable( me, playerVector[i] ) )
		{
			continue;
		}

		float rangeSq = me->GetRangeSquaredTo( playerVector[i] );
		if ( rangeSq < victimRangeSq )
		{
			newVictim = playerVector[i];
			victimRangeSq = rangeSq;
		}
	}

	if ( newVictim )
	{
		// we have a new victim
		m_attackTargetFocusTimer.Start( tf_merasmus_chase_duration.GetFloat() );
	}

	m_attackTarget = newVictim;
}


//----------------------------------------------------------------------------------
ActionResult< CMerasmus >	CMerasmusAttack::Update( CMerasmus *me, float interval )
{
	if ( !me->IsAlive() )
	{
		return Done();
	}

	if ( me->HasStunTimer() )
	{
		return SuspendFor( new CMerasmusStunned, "Stunned!" );
	}

	SelectVictim( me );
	RecomputeHomePosition();

	if ( m_attackTarget == NULL )
	{
		// go home
		const float atHomeRange = 50.0f;
		if ( me->IsRangeGreaterThan( m_homePos, atHomeRange ) )
		{
			if ( m_path.GetAge() > 3.0f )
			{
				CMerasmusPathCost cost( me );
				m_path.Compute( me, m_homePos, cost );
			}

			m_path.Update( me );
		}
// 		else
// 		{
// 			// at home with nothing to do - taunt!
// 			if ( !me->IsMoving() && m_tauntTimer.IsElapsed() )
// 			{
// 				m_tauntTimer.Start( RandomFloat( 3.0f, 5.0f ) );
// 
// 				return SuspendFor( new CMerasmusTaunt, "Taunting because I have nothing to do." );
// 			}
// 		}
	}
	else
	{
		// chase after our chase victim
		const float standAndSwingRange = 100.0f;
		CTFPlayer *chaseVictim = m_attackTarget;

		if ( me->IsRangeGreaterThan( chaseVictim, standAndSwingRange ) || !me->IsLineOfSightClear( chaseVictim ) )
		{
			if ( m_path.GetAge() > 1.0f )
			{
				CMerasmusPathCost cost( me );
				m_path.Compute( me, chaseVictim, cost );
			}

			m_path.Update( me );
		}
	}

	if ( m_bombHeadTimer.IsElapsed() )
	{
		// bomb heads last 15 seconds - make sure we don't add more while existing ones are out
		m_bombHeadTimer.Start( 16.0f );
		me->BombHeadMode();
	}

	// swing our axe at our attack target if they are in range
	if ( m_attackTarget != NULL && m_attackTarget->IsAlive() )
	{
		if ( m_zapTimer.IsElapsed() )
		{
			RandomZapTimer();
			return SuspendFor( new CMerasmusZap, "Zap!" );
		}

		if ( me->IsRangeLessThan( m_attackTarget, tf_merasmus_attack_range.GetFloat() ) )
		{
			if ( m_attackTimer.IsElapsed() )
			{
				m_attackTimer.Start( 1.f );
				return SuspendFor( new CMerasmusStaffAttack( m_attackTarget ), "Whack!" );
			}

			me->GetLocomotionInterface()->FaceTowards( m_attackTarget->WorldSpaceCenter() );
		}

		if ( m_grenadeTimer.IsElapsed() )
		{
			RandomGrenadeTimer();
			return SuspendFor( new CMerasmusThrowingGrenade( m_attackTarget ), "Fire in the hole!" );
		}
	}

	if ( !me->GetBodyInterface()->IsActivity( ACT_MP_RUN_MELEE ) )
	{
		me->GetBodyInterface()->StartActivity( ACT_MP_RUN_MELEE );
	}


	return Continue();
}


//---------------------------------------------------------------------------------------------
EventDesiredResult< CMerasmus > CMerasmusAttack::OnStuck( CMerasmus *me )
{
	// we're stuck - just warp to the our next path goal
	if ( m_path.GetCurrentGoal() )
	{
		me->SetAbsOrigin( m_path.GetCurrentGoal()->pos + Vector( 0, 0, 10.0f ) );
	}

	return TryContinue( RESULT_TRY );
}


//---------------------------------------------------------------------------------------------
EventDesiredResult< CMerasmus > CMerasmusAttack::OnContact( CMerasmus *me, CBaseEntity *other, CGameTrace *result )
{
	if ( other->IsPlayer() )
	{
		CTFPlayer *pTFPlayer = ToTFPlayer( other );
		if ( pTFPlayer )
		{
			if ( pTFPlayer->IsAlive() )
			{
				// force attack the thing we bumped into
				// this prevents us from being stuck on dispensers, for example
				m_attackTarget = pTFPlayer;
				m_attackTargetFocusTimer.Start( tf_merasmus_chase_duration.GetFloat() );
			}
		}
	}

	return TryContinue( RESULT_TRY );
}


//---------------------------------------------------------------------------------------------
void CMerasmusAttack::RecomputeHomePosition( void )
{
	if ( !m_homePosRecalcTimer.IsElapsed() )
	{
		return;
	}

	m_homePosRecalcTimer.Reset();

	CTeamControlPoint *contestedPoint = NULL;
	CTeamControlPointMaster *pMaster = g_hControlPointMasters.Count() ? g_hControlPointMasters[0] : NULL;
	if ( pMaster )
	{
		for( int i=0; i<pMaster->GetNumPoints(); ++i )
		{
			contestedPoint = pMaster->GetControlPoint( i );
			if ( contestedPoint && pMaster->IsInRound( contestedPoint ) )
			{
				if ( ObjectiveResource()->GetOwningTeam( contestedPoint->GetPointIndex() ) == TF_TEAM_BLUE )
					continue;

				// blue are the invaders
				if ( !TeamplayGameRules()->TeamMayCapturePoint( TF_TEAM_BLUE, contestedPoint->GetPointIndex() ) )
					continue;

				break;
			}
		}
	}

	if ( contestedPoint )
	{
		m_homePos = contestedPoint->GetAbsOrigin();
	}
}


void CMerasmusAttack::RandomGrenadeTimer()
{
	m_grenadeTimer.Start( RandomFloat( 2.f, 3.f ) );
}


void CMerasmusAttack::RandomZapTimer()
{
	m_zapTimer.Start( RandomFloat( 3.f, 4.f ) );
}


//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
ActionResult< CMerasmus > CMerasmusTaunt::OnStart( CMerasmus *me, Action< CMerasmus > *priorAction )
{
	m_timer.Start( 3.0f );

	const char *taunts[] =
	{
		"gesture_melee_cheer",
		"gesture_melee_go",
		"taunt01",		// wave
		"taunt06",		// thriller
		"taunt_laugh",
		NULL
	};

	// count the available taunts
	int count = 0;
	while( true )
	{
		if ( taunts[ count ] == NULL )
			break;

		++count;
	}

	// pick one and play it
	int which = RandomInt( 0, count-1 );
	me->AddGestureSequence( me->LookupSequence( taunts[ which ] ) );

	int staffBodyGroup = me->FindBodygroupByName( "staff" );
	me->SetBodygroup( staffBodyGroup, 1 );

	return Continue();
}


//---------------------------------------------------------------------------------------------
ActionResult< CMerasmus > CMerasmusTaunt::Update( CMerasmus *me, float interval )
{
	if ( m_timer.IsElapsed() )
	{
		return Done();
	}

	return Continue();
}


//---------------------------------------------------------------------------------------------
void CMerasmusTaunt::OnEnd( CMerasmus *me, Action< CMerasmus > *nextAction )
{
	// turn the staff back on
	int staffBodyGroup = me->FindBodygroupByName( "staff" );
	me->SetBodygroup( staffBodyGroup, 0 );
}