summaryrefslogtreecommitdiff
path: root/game/server/tf/bot/behavior/tf_bot_seek_and_destroy.cpp
blob: 5e78ea3e45f4229b878ef9f7c63e68369f6eb4a9 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
// tf_bot_seek_and_destroy.h
// Roam the environment, attacking victims
// Michael Booth, January 2010

#include "cbase.h"
#include "tf_player.h"
#include "tf_gamerules.h"
#include "team_control_point_master.h"
#include "bot/tf_bot.h"
#include "bot/behavior/tf_bot_attack.h"
#include "bot/behavior/tf_bot_seek_and_destroy.h"
#include "bot/behavior/sniper/tf_bot_sniper_attack.h"
#include "nav_mesh.h"

extern ConVar tf_bot_path_lookahead_range;
extern ConVar tf_bot_offense_must_push_time;
extern ConVar tf_bot_defense_must_defend_time;

ConVar tf_bot_debug_seek_and_destroy( "tf_bot_debug_seek_and_destroy", "0", FCVAR_CHEAT );


//---------------------------------------------------------------------------------------------
CTFBotSeekAndDestroy::CTFBotSeekAndDestroy( float duration )
{
	if ( duration > 0.0f )
	{
		m_giveUpTimer.Start( duration );
	}
}


//---------------------------------------------------------------------------------------------
ActionResult< CTFBot >	CTFBotSeekAndDestroy::OnStart( CTFBot *me, Action< CTFBot > *priorAction )
{
	m_path.SetMinLookAheadDistance( me->GetDesiredPathLookAheadRange() );

	RecomputeSeekPath( me );

	CTeamControlPoint *point = me->GetMyControlPoint();
	m_isPointLocked = ( point && point->IsLocked() );

	// restart the timer if we have one
	if ( m_giveUpTimer.HasStarted() )
	{
		m_giveUpTimer.Reset();
	}

	return Continue();
}


//---------------------------------------------------------------------------------------------
ActionResult< CTFBot >	CTFBotSeekAndDestroy::Update( CTFBot *me, float interval )
{
	if ( m_giveUpTimer.HasStarted() && m_giveUpTimer.IsElapsed() )
	{
		return Done( "Behavior duration elapsed" );
	}

	if ( TFGameRules()->IsInTraining() )
	{
		// if the trainee has started capturing the point, assist them
		if ( me->IsAnyPointBeingCaptured() )
		{
			return Done( "Assist trainee in capturing the point" );
		}
	}
	else
	{
		if ( me->IsCapturingPoint() )
		{
			return Done( "Keep capturing point I happened to stumble upon" );
		}

		if ( m_isPointLocked )
		{
			CTeamControlPoint *point = me->GetMyControlPoint();

			if ( point && !point->IsLocked() )
			{
				return Done( "The point just unlocked" );
			}
		}
		
		if ( !TFGameRules()->RoundHasBeenWon() && me->GetTimeLeftToCapture() < tf_bot_offense_must_push_time.GetFloat() )
		{
			return Done( "Time to push for the objective" );
		}
	}

	const CKnownEntity *threat = me->GetVisionInterface()->GetPrimaryKnownThreat();
	if ( threat )
	{
		if ( TFGameRules()->RoundHasBeenWon() )
		{
			// hunt down the losers
			return SuspendFor( new CTFBotAttack, "Chasing down the losers" );
		}

		const float engageRange = 1000.0f;
		if ( me->IsRangeLessThan( threat->GetLastKnownPosition(), engageRange ) )
		{
			return SuspendFor( new CTFBotAttack, "Going after an enemy" );
		}
	}

	// move towards our seek goal
	m_path.Update( me );

	if ( !m_path.IsValid() && m_repathTimer.IsElapsed() )
	{
		m_repathTimer.Start( 1.0f );

		RecomputeSeekPath( me );
	}

	return Continue();
}


//---------------------------------------------------------------------------------------------
ActionResult< CTFBot > CTFBotSeekAndDestroy::OnResume( CTFBot *me, Action< CTFBot > *interruptingAction )
{
	RecomputeSeekPath( me );

	return Continue();
}


//---------------------------------------------------------------------------------------------
EventDesiredResult< CTFBot > CTFBotSeekAndDestroy::OnStuck( CTFBot *me )
{
	RecomputeSeekPath( me );

	return TryContinue();
}


//---------------------------------------------------------------------------------------------
EventDesiredResult< CTFBot > CTFBotSeekAndDestroy::OnMoveToSuccess( CTFBot *me, const Path *path )
{
	RecomputeSeekPath( me );

	return TryContinue();
}


//---------------------------------------------------------------------------------------------
EventDesiredResult< CTFBot > CTFBotSeekAndDestroy::OnMoveToFailure( CTFBot *me, const Path *path, MoveToFailureType reason )
{
	RecomputeSeekPath( me );

	return TryContinue();
}


//---------------------------------------------------------------------------------------------
QueryResultType	CTFBotSeekAndDestroy::ShouldRetreat( const INextBot *meBot ) const
{
	CTFBot *me = (CTFBot *)meBot->GetEntity();

	if ( me->IsPlayerClass( TF_CLASS_PYRO ) )
	{
		return ANSWER_NO;
	}

	return ANSWER_UNDEFINED;
}


//---------------------------------------------------------------------------------------------
QueryResultType CTFBotSeekAndDestroy::ShouldHurry( const INextBot *me ) const
{
	return ANSWER_UNDEFINED;
}


//---------------------------------------------------------------------------------------------
CTFNavArea *CTFBotSeekAndDestroy::ChooseGoalArea( CTFBot *me )
{
	CUtlVector< CTFNavArea * > goalVector;

	TheTFNavMesh()->CollectSpawnRoomThresholdAreas( &goalVector, GetEnemyTeam( me->GetTeamNumber() ) );

	CTeamControlPoint *point = me->GetMyControlPoint();
	if ( point && !point->IsLocked() )
	{
		// add current control point as a seek goal
		const CUtlVector< CTFNavArea * > *controlPointAreas = TheTFNavMesh()->GetControlPointAreas( point->GetPointIndex() );
		if ( controlPointAreas && controlPointAreas->Count() > 0 )
		{
			goalVector.AddToTail( controlPointAreas->Element( RandomInt( 0, controlPointAreas->Count()-1 ) ) );
		}
	}

	if ( tf_bot_debug_seek_and_destroy.GetBool() )
	{
		for( int i=0; i<goalVector.Count(); ++i )
		{
			TheNavMesh->AddToSelectedSet( goalVector[i] );
		}
	}

	// pick a new goal
	if ( goalVector.Count() > 0 )
	{
		return goalVector[ RandomInt( 0, goalVector.Count()-1 ) ];
	}

	return NULL;
}


//---------------------------------------------------------------------------------------------
void CTFBotSeekAndDestroy::RecomputeSeekPath( CTFBot *me )
{
	m_goalArea = ChooseGoalArea( me );
	if ( m_goalArea )
	{
		CTFBotPathCost cost( me, SAFEST_ROUTE );
		m_path.Compute( me, m_goalArea->GetCenter(), cost );
	}
	else
	{
		m_path.Invalidate();
	}
}


//---------------------------------------------------------------------------------------------
EventDesiredResult< CTFBot > CTFBotSeekAndDestroy::OnTerritoryContested( CTFBot *me, int territoryID )
{
	return TryDone( RESULT_IMPORTANT, "Defending the point" );
}


//---------------------------------------------------------------------------------------------
EventDesiredResult< CTFBot > CTFBotSeekAndDestroy::OnTerritoryCaptured( CTFBot *me, int territoryID )
{
	return TryDone( RESULT_IMPORTANT, "Giving up due to point capture" );
}


//---------------------------------------------------------------------------------------------
EventDesiredResult< CTFBot > CTFBotSeekAndDestroy::OnTerritoryLost( CTFBot *me, int territoryID )
{
	return TryDone( RESULT_IMPORTANT, "Giving up due to point lost" );
}