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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
// tf_bot_get_health.h
// Pick up any nearby health kit
// Michael Booth, May 2009
#include "cbase.h"
#include "tf_gamerules.h"
#include "tf_obj.h"
#include "bot/tf_bot.h"
#include "bot/behavior/tf_bot_get_health.h"
extern ConVar tf_bot_path_lookahead_range;
ConVar tf_bot_health_critical_ratio( "tf_bot_health_critical_ratio", "0.3", FCVAR_CHEAT );
ConVar tf_bot_health_ok_ratio( "tf_bot_health_ok_ratio", "0.8", FCVAR_CHEAT );
ConVar tf_bot_health_search_near_range( "tf_bot_health_search_near_range", "1000", FCVAR_CHEAT );
ConVar tf_bot_health_search_far_range( "tf_bot_health_search_far_range", "2000", FCVAR_CHEAT );
//---------------------------------------------------------------------------------------------
class CHealthFilter : public INextBotFilter
{
public:
CHealthFilter( CTFBot *me )
{
m_me = me;
}
bool IsSelected( const CBaseEntity *constCandidate ) const
{
if ( !constCandidate )
return false;
CBaseEntity *candidate = const_cast< CBaseEntity * >( constCandidate );
CTFNavArea *area = (CTFNavArea *)TheNavMesh->GetNearestNavArea( candidate->WorldSpaceCenter() );
if ( !area )
return false;
CClosestTFPlayer close( candidate );
ForEachPlayer( close );
// if the closest player to this candidate object is an enemy, don't use it
if ( close.m_closePlayer && !m_me->InSameTeam( close.m_closePlayer ) )
return false;
// resupply cabinets (not assigned a team)
if ( candidate->ClassMatches( "func_regenerate" ) )
{
if ( !area->HasAttributeTF( TF_NAV_SPAWN_ROOM_BLUE | TF_NAV_SPAWN_ROOM_RED ) )
{
// Assume any resupply cabinets not in a teamed spawn room are inaccessible.
// Ex: pl_upward has forward spawn rooms that neither team can use until
// certain checkpoints are reached.
return false;
}
if ( ( m_me->GetTeamNumber() == TF_TEAM_RED && area->HasAttributeTF( TF_NAV_SPAWN_ROOM_RED ) ) ||
( m_me->GetTeamNumber() == TF_TEAM_BLUE && area->HasAttributeTF( TF_NAV_SPAWN_ROOM_BLUE ) ) )
{
// the supply cabinet is in my spawn room
return true;
}
return false;
}
// ignore non-existent ammo to ensure we collect nearby existing ammo
if ( candidate->IsEffectActive( EF_NODRAW ) )
return false;
if ( candidate->ClassMatches( "item_healthkit*" ) )
return true;
if ( m_me->InSameTeam( candidate ) )
{
// friendly engineer's dispenser
if ( candidate->ClassMatches( "obj_dispenser*" ) )
{
CBaseObject *dispenser = (CBaseObject *)candidate;
if ( !dispenser->IsBuilding() && !dispenser->IsPlacing() && !dispenser->IsDisabled() )
{
return true;
}
}
}
return false;
}
CTFBot *m_me;
};
//---------------------------------------------------------------------------------------------
static CTFBot *s_possibleBot = NULL;
static CHandle< CBaseEntity > s_possibleHealth = NULL;
static int s_possibleFrame = 0;
//---------------------------------------------------------------------------------------------
/**
* Return true if this Action has what it needs to perform right now
*/
bool CTFBotGetHealth::IsPossible( CTFBot *me )
{
VPROF_BUDGET( "CTFBotGetHealth::IsPossible", "NextBot" );
// don't move to fetch health if we have a healer
if ( me->m_Shared.GetNumHealers() > 0 )
return false;
#ifdef TF_RAID_MODE
// mobs don't heal
if ( TFGameRules()->IsRaidMode() && me->HasAttribute( CTFBot::AGGRESSIVE ) )
{
return false;
}
#endif // TF_RAID_MODE
if ( TFGameRules()->IsMannVsMachineMode() )
{
return false;
}
float healthRatio = (float)me->GetHealth() / (float)me->GetMaxHealth();
float t = ( healthRatio - tf_bot_health_critical_ratio.GetFloat() ) / ( tf_bot_health_ok_ratio.GetFloat() - tf_bot_health_critical_ratio.GetFloat() );
t = clamp( t, 0.0f, 1.0f );
if ( me->m_Shared.InCond( TF_COND_BURNING ) )
{
// on fire - get health now
t = 0.0f;
}
// the more we are hurt, the farther we'll travel to get health
float searchRange = tf_bot_health_search_far_range.GetFloat() + t * ( tf_bot_health_search_near_range.GetFloat() - tf_bot_health_search_far_range.GetFloat() );
CUtlVector< CHandle< CBaseEntity > > healthVector;
CHealthFilter healthFilter( me );
me->SelectReachableObjects( TFGameRules()->GetHealthEntityVector(), &healthVector, healthFilter, me->GetLastKnownArea(), searchRange );
if ( healthVector.Count() == 0 )
{
if ( me->IsDebugging( NEXTBOT_BEHAVIOR ) )
{
Warning( "%3.2f: No health nearby\n", gpGlobals->curtime );
}
return false;
}
// use the first item in the list, since it will be the closest to us (or nearly so)
CBaseEntity *health = healthVector[0];
for( int i=0; i<healthVector.Count(); ++i )
{
if ( healthVector[i]->GetTeamNumber() != GetEnemyTeam( me->GetTeamNumber() ) )
{
health = healthVector[i];
break;
}
}
if ( health == NULL )
{
if ( me->IsDebugging( NEXTBOT_BEHAVIOR ) )
{
Warning( "%3.2f: No health available to my team nearby\n", gpGlobals->curtime );
}
return false;
}
CTFBotPathCost cost( me, FASTEST_ROUTE );
PathFollower path;
if ( !path.Compute( me, health->WorldSpaceCenter(), cost ) )
{
if ( me->IsDebugging( NEXTBOT_BEHAVIOR ) )
{
Warning( "%3.2f: No path to health!\n", gpGlobals->curtime );
}
return false;
}
s_possibleBot = me;
s_possibleHealth = health;
s_possibleFrame = gpGlobals->framecount;
return true;
}
//---------------------------------------------------------------------------------------------
ActionResult< CTFBot > CTFBotGetHealth::OnStart( CTFBot *me, Action< CTFBot > *priorAction )
{
VPROF_BUDGET( "CTFBotGetHealth::OnStart", "NextBot" );
m_path.SetMinLookAheadDistance( me->GetDesiredPathLookAheadRange() );
// if IsPossible() has already been called, use its cached data
if ( s_possibleFrame != gpGlobals->framecount || s_possibleBot != me )
{
if ( !IsPossible( me ) || s_possibleHealth == NULL )
{
return Done( "Can't get health" );
}
}
m_healthKit = s_possibleHealth;
m_isGoalDispenser = m_healthKit->ClassMatches( "obj_dispenser*" );
CTFBotPathCost cost( me, SAFEST_ROUTE );
if ( !m_path.Compute( me, m_healthKit->WorldSpaceCenter(), cost ) )
{
return Done( "No path to health!" );
}
// if I'm a spy, cloak and disguise
if ( me->IsPlayerClass( TF_CLASS_SPY ) )
{
if ( !me->m_Shared.IsStealthed() )
{
me->PressAltFireButton();
}
}
return Continue();
}
//---------------------------------------------------------------------------------------------
ActionResult< CTFBot > CTFBotGetHealth::Update( CTFBot *me, float interval )
{
if ( m_healthKit == NULL || ( m_healthKit->IsEffectActive( EF_NODRAW ) && !FClassnameIs( m_healthKit, "func_regenerate" ) ) )
{
return Done( "Health kit I was going for has been taken" );
}
// if a medic is healing us, give up on getting a kit
int i;
for( i=0; i<me->m_Shared.GetNumHealers(); ++i )
{
if ( !me->m_Shared.HealerIsDispenser( i ) )
break;
}
if ( i < me->m_Shared.GetNumHealers() )
{
return Done( "A Medic is healing me" );
}
if ( me->m_Shared.GetNumHealers() )
{
// a dispenser is healing me, don't wait if I'm in combat
const CKnownEntity *known = me->GetVisionInterface()->GetPrimaryKnownThreat();
if ( known && known->IsVisibleInFOVNow() )
{
return Done( "No time to wait for health, I must fight" );
}
}
if ( me->GetHealth() >= me->GetMaxHealth() )
{
return Done( "I've been healed" );
}
// if the closest player to the item we're after is an enemy, give up
CClosestTFPlayer close( m_healthKit );
ForEachPlayer( close );
if ( close.m_closePlayer && !me->InSameTeam( close.m_closePlayer ) )
return Done( "An enemy is closer to it" );
// un-zoom
CTFWeaponBase *myWeapon = me->m_Shared.GetActiveTFWeapon();
if ( myWeapon && myWeapon->IsWeapon( TF_WEAPON_SNIPERRIFLE ) && me->m_Shared.InCond( TF_COND_ZOOMED ) )
me->PressAltFireButton();
if ( !m_path.IsValid() )
{
// this can occur if we overshoot the health kit's location
// because it is momentarily gone
CTFBotPathCost cost( me, SAFEST_ROUTE );
if ( !m_path.Compute( me, m_healthKit->WorldSpaceCenter(), cost ) )
{
return Done( "No path to health!" );
}
}
m_path.Update( me );
// may need to switch weapons (ie: engineer holding toolbox now needs to heal and defend himself)
const CKnownEntity *threat = me->GetVisionInterface()->GetPrimaryKnownThreat();
me->EquipBestWeaponForThreat( threat );
return Continue();
}
//---------------------------------------------------------------------------------------------
EventDesiredResult< CTFBot > CTFBotGetHealth::OnStuck( CTFBot *me )
{
return TryDone( RESULT_CRITICAL, "Stuck trying to reach health kit" );
}
//---------------------------------------------------------------------------------------------
EventDesiredResult< CTFBot > CTFBotGetHealth::OnMoveToSuccess( CTFBot *me, const Path *path )
{
return TryContinue();
}
//---------------------------------------------------------------------------------------------
EventDesiredResult< CTFBot > CTFBotGetHealth::OnMoveToFailure( CTFBot *me, const Path *path, MoveToFailureType reason )
{
return TryDone( RESULT_CRITICAL, "Failed to reach health kit" );
}
//---------------------------------------------------------------------------------------------
// We are always hurrying if we need to collect health
QueryResultType CTFBotGetHealth::ShouldHurry( const INextBot *me ) const
{
return ANSWER_YES;
}
|