summaryrefslogtreecommitdiff
path: root/game/server/tf/bot/behavior/tf_bot_scenario_monitor.cpp
blob: 50ed4601cd3210892c742209ded35a7a48725907 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
// tf_bot_scenario_monitor.h
// Behavior layer that interrupts for scenario rules (picked up flag, drop what you're doing and capture, etc)
// Michael Booth, May 2011

#include "cbase.h"
#include "fmtstr.h"

#include "tf_gamerules.h"
#include "tf_weapon_pipebomblauncher.h"
#include "NextBot/NavMeshEntities/func_nav_prerequisite.h"

#include "bot/tf_bot.h"
#include "bot/tf_bot_manager.h"
#include "bot/behavior/nav_entities/tf_bot_nav_ent_destroy_entity.h"
#include "bot/behavior/nav_entities/tf_bot_nav_ent_move_to.h"
#include "bot/behavior/nav_entities/tf_bot_nav_ent_wait.h"
#include "bot/behavior/tf_bot_tactical_monitor.h"
#include "bot/behavior/tf_bot_retreat_to_cover.h"
#include "bot/behavior/tf_bot_get_health.h"
#include "bot/behavior/tf_bot_get_ammo.h"
#include "bot/behavior/sniper/tf_bot_sniper_lurk.h"
#include "bot/behavior/scenario/capture_point/tf_bot_capture_point.h"
#include "bot/behavior/scenario/capture_point/tf_bot_defend_point.h"
#include "bot/behavior/scenario/payload/tf_bot_payload_guard.h"
#include "bot/behavior/scenario/payload/tf_bot_payload_push.h"
#include "bot/behavior/tf_bot_use_teleporter.h"
#include "bot/behavior/training/tf_bot_training.h"
#include "bot/behavior/tf_bot_destroy_enemy_sentry.h"
#include "bot/behavior/engineer/tf_bot_engineer_building.h"
#include "bot/behavior/spy/tf_bot_spy_infiltrate.h"
#include "bot/behavior/spy/tf_bot_spy_leave_spawn_room.h"
#include "bot/behavior/medic/tf_bot_medic_heal.h"
#include "bot/behavior/engineer/tf_bot_engineer_build.h"
#include "bot/map_entities/tf_bot_hint_sentrygun.h"

#ifdef TF_RAID_MODE
#include "bot/behavior/scenario/raid/tf_bot_wander.h"
#include "bot/behavior/scenario/raid/tf_bot_companion.h"
#include "bot/behavior/scenario/raid/tf_bot_squad_attack.h"
#include "bot/behavior/scenario/raid/tf_bot_guard_area.h"
#endif // TF_RAID_MODE

#include "bot/behavior/tf_bot_attack.h"
#include "bot/behavior/tf_bot_seek_and_destroy.h"
#include "bot/behavior/tf_bot_taunt.h"
#include "bot/behavior/tf_bot_escort.h"
#include "bot/behavior/scenario/capture_the_flag/tf_bot_fetch_flag.h"
#include "bot/behavior/scenario/capture_the_flag/tf_bot_deliver_flag.h"

#include "bot/behavior/missions/tf_bot_mission_suicide_bomber.h"
#include "bot/behavior/squad/tf_bot_escort_squad_leader.h"
#include "bot/behavior/engineer/mvm_engineer/tf_bot_mvm_engineer_idle.h"
#include "bot/behavior/missions/tf_bot_mission_reprogrammed.h"

#include "bot/behavior/tf_bot_scenario_monitor.h"


extern ConVar tf_bot_health_ok_ratio;
extern ConVar tf_bot_health_critical_ratio;


//-----------------------------------------------------------------------------------------
// Returns the initial Action we will run concurrently as a child to us
Action< CTFBot > *CTFBotScenarioMonitor::InitialContainedAction( CTFBot *me )
{
	if ( me->IsInASquad() )
	{
		if ( me->GetSquad()->IsLeader( me ) )
		{
			// I'm the leader of this Squad, so I can do what I want and the other Squaddies will support me
			return DesiredScenarioAndClassAction( me );
		}

		// Medics are the exception - they always heal, and have special squad logic in their heal logic
		if ( me->IsPlayerClass( TF_CLASS_MEDIC ) )
		{
			return new CTFBotMedicHeal;
		}

		// I'm in a Squad but not the leader, do "escort and support" Squad behavior
		// until the Squad disbands, and then do my normal thing
		return new CTFBotEscortSquadLeader( DesiredScenarioAndClassAction( me ) );
	}

	return DesiredScenarioAndClassAction( me );
}


//-----------------------------------------------------------------------------------------
// Returns Action specific to the scenario and my class
Action< CTFBot > *CTFBotScenarioMonitor::DesiredScenarioAndClassAction( CTFBot *me )
{
	switch( me->GetMission() )
	{
	case CTFBot::MISSION_SEEK_AND_DESTROY:
		break;

	case CTFBot::MISSION_DESTROY_SENTRIES:
		return new CTFBotMissionSuicideBomber;

	case CTFBot::MISSION_SNIPER:
		return new CTFBotSniperLurk;

#ifdef STAGING_ONLY
	case CTFBot::MISSION_REPROGRAMMED:
		return new CTFBotMissionReprogrammed;
#endif
	}

#ifdef TF_RAID_MODE
	if ( me->HasAttribute( CTFBot::IS_NPC ) )
	{
		// map-spawned guardians
		return new CTFBotGuardian;
	}
#endif // TF_RAID_MODE

#ifdef TF_RAID_MODE
	if ( TFGameRules()->IsBossBattleMode() )
	{
		if ( me->GetTeamNumber() == TF_TEAM_BLUE )
		{
			// bot teammates
			return new CTFBotCompanion;
		}
		
		if ( me->IsPlayerClass( TF_CLASS_SNIPER ) )
		{
			return new CTFBotSniperLurk;
		}

		if ( me->IsPlayerClass( TF_CLASS_SPY ) )
		{
			return new CTFBotSpyInfiltrate;
		}

		if ( me->IsPlayerClass( TF_CLASS_MEDIC ) )
		{
			return new CTFBotMedicHeal;
		}

		if ( me->IsPlayerClass( TF_CLASS_ENGINEER ) )
		{
			return new CTFBotEngineerBuild;
		}

		return new CTFBotEscort( TFGameRules()->GetActiveBoss() );
	}
	else if ( TFGameRules()->IsRaidMode() )
	{
		if ( me->GetTeamNumber() == TF_TEAM_BLUE )
		{
			// bot teammates
			return new CTFBotCompanion;
		}

		if ( me->IsInASquad() )
		{
			// squad behavior
			return new CTFBotSquadAttack;
		}

		if ( me->IsPlayerClass( TF_CLASS_SCOUT ) || me->HasAttribute( CTFBot::AGGRESSIVE ) )
		{
			return new CTFBotWander;
		}

		if ( me->IsPlayerClass( TF_CLASS_SNIPER ) )
		{
			return new CTFBotSniperLurk;
		}

		if ( me->IsPlayerClass( TF_CLASS_SPY ) )
		{
			return new CTFBotSpyInfiltrate;
		}

		return new CTFBotGuardArea;
	}
#endif // TF_RAID_MODE	

	if ( TFGameRules()->IsMannVsMachineMode() )
	{
		if ( me->IsPlayerClass( TF_CLASS_SPY ) )
		{
			return new CTFBotSpyLeaveSpawnRoom;
		}

		if ( me->IsPlayerClass( TF_CLASS_MEDIC ) )
		{
			// if I'm being healed by another medic, I should do something else other than healing
			bool bIsBeingHealedByAMedic = false;
			int nNumHealers = me->m_Shared.GetNumHealers();
			for ( int i=0; i<nNumHealers; ++i )
			{
				CBaseEntity *pHealer = me->m_Shared.GetHealerByIndex(i);
				if ( pHealer && pHealer->IsPlayer() )
				{
					bIsBeingHealedByAMedic = true;
					break;
				}
			}

			if ( !bIsBeingHealedByAMedic )
			{
				return new CTFBotMedicHeal;
			}
		}

		if ( me->IsPlayerClass( TF_CLASS_ENGINEER ) )
		{
			return new CTFBotMvMEngineerIdle;
		}

		// NOTE: Snipers are intentionally left out so they go after the flag. Actual sniping behavior is done as a mission.

		if ( me->HasAttribute( CTFBot::AGGRESSIVE ) )
		{
			// push for the point first, then attack
			return new CTFBotPushToCapturePoint( new CTFBotFetchFlag );
		}

		// capture the flag
		return new CTFBotFetchFlag;
	}

	if ( me->IsPlayerClass( TF_CLASS_SPY ) )
	{
		return new CTFBotSpyInfiltrate;
	}

	if ( !TheTFBots().IsMeleeOnly() )
	{
		if ( me->IsPlayerClass( TF_CLASS_SNIPER ) )
		{
			return new CTFBotSniperLurk;
		}

		if ( me->IsPlayerClass( TF_CLASS_MEDIC ) )
		{
			return new CTFBotMedicHeal;
		}

		if ( me->IsPlayerClass( TF_CLASS_ENGINEER ) )
		{
			return new CTFBotEngineerBuild;
		}
	}

	if ( me->GetFlagToFetch() )
	{
		// capture the flag
		return new CTFBotFetchFlag;
	}
	else if ( TFGameRules()->GetGameType() == TF_GAMETYPE_ESCORT )
	{
		// push the cart
		if ( me->GetTeamNumber() == TF_TEAM_BLUE )
		{
			// blu is pushing
			return new CTFBotPayloadPush;
		}
		else if ( me->GetTeamNumber() == TF_TEAM_RED )
		{
			// red is blocking
			return new CTFBotPayloadGuard;
		}
	}
	else if ( TFGameRules()->GetGameType() == TF_GAMETYPE_CP )
	{
		// if we have a point we can capture - do it
		CUtlVector< CTeamControlPoint * > captureVector;
		TFGameRules()->CollectCapturePoints( me, &captureVector );

		if ( captureVector.Count() > 0 )
		{
			return new CTFBotCapturePoint;
		}

		// otherwise, defend our point(s) from capture
		CUtlVector< CTeamControlPoint * > defendVector;
		TFGameRules()->CollectDefendPoints( me, &defendVector );

		if ( defendVector.Count() > 0 )
		{
			return new CTFBotDefendPoint;
		}

		// likely KotH mode and/or all points are locked - assume capture
		DevMsg( "%3.2f: %s: Gametype is CP, but I can't find a point to capture or defend!\n", gpGlobals->curtime, me->GetDebugIdentifier() );
		return new CTFBotCapturePoint;
	}
	else
	{
		// scenario not implemented yet - just fight
		return new CTFBotSeekAndDestroy;
	}

	return NULL;
}


//-----------------------------------------------------------------------------------------
ActionResult< CTFBot >	CTFBotScenarioMonitor::OnStart( CTFBot *me, Action< CTFBot > *priorAction )
{
	m_ignoreLostFlagTimer.Start( 20.0f );
	m_lostFlagTimer.Invalidate();
	return Continue();
}


ConVar tf_bot_fetch_lost_flag_time( "tf_bot_fetch_lost_flag_time", "10", FCVAR_CHEAT, "How long busy TFBots will ignore the dropped flag before they give up what they are doing and go after it" );
ConVar tf_bot_flag_kill_on_touch( "tf_bot_flag_kill_on_touch", "0", FCVAR_CHEAT, "If nonzero, any bot that picks up the flag dies. For testing." );


//-----------------------------------------------------------------------------------------
ActionResult< CTFBot >	CTFBotScenarioMonitor::Update( CTFBot *me, float interval )
{
	// CTF Scenario
	if ( me->HasTheFlag() )
	{
		if ( tf_bot_flag_kill_on_touch.GetBool() )
		{
			me->CommitSuicide( false, true );
			return Done( "Flag kill" );
		}

		// we just picked up the flag - drop what we're doing and take it in
		return SuspendFor( new CTFBotDeliverFlag, "I've picked up the flag! Running it in..." );
	}

	if ( me->HasMission( CTFBot::NO_MISSION ) && m_ignoreLostFlagTimer.IsElapsed() && me->IsAllowedToPickUpFlag() )
	{
		CCaptureFlag *flag = me->GetFlagToFetch();

		if ( flag )
		{
			CTFPlayer *carrier = ToTFPlayer( flag->GetOwnerEntity() );
			if ( carrier )
			{
				m_lostFlagTimer.Invalidate();
			}
			else
			{
				// flag is loose
				if ( !m_lostFlagTimer.HasStarted() )
				{
					m_lostFlagTimer.Start( tf_bot_fetch_lost_flag_time.GetFloat() );
				}
				else if ( m_lostFlagTimer.IsElapsed() )
				{
					m_lostFlagTimer.Invalidate();

					// if we're a Medic an actively healing someone, don't interrupt
					if ( !me->MedicGetHealTarget() )
					{
						// we better go get the flag
						return SuspendFor( new CTFBotFetchFlag( TEMPORARY_FLAG_FETCH ), "Fetching lost flag..." );
					}
				}
			}
		}
	}

	return Continue();
}