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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
|
//========= Copyright Valve Corporation, All rights reserved. ============//
// tf_bot_engineer_building.cpp
// At building location, constructing buildings
// Michael Booth, May 2010
#include "cbase.h"
#include "nav_mesh.h"
#include "tf_player.h"
#include "tf_obj.h"
#include "tf_obj_sentrygun.h"
#include "tf_obj_dispenser.h"
#include "tf_gamerules.h"
#include "tf_weapon_builder.h"
#include "team_train_watcher.h"
#include "bot/tf_bot.h"
#include "bot/behavior/engineer/tf_bot_engineer_building.h"
#include "bot/behavior/engineer/tf_bot_engineer_move_to_build.h"
#include "bot/behavior/engineer/tf_bot_engineer_build_teleport_exit.h"
#include "bot/behavior/engineer/tf_bot_engineer_build_sentrygun.h"
#include "bot/behavior/engineer/tf_bot_engineer_build_dispenser.h"
#include "bot/behavior/tf_bot_attack.h"
#include "bot/behavior/tf_bot_get_ammo.h"
#include "bot/map_entities/tf_bot_hint_teleporter_exit.h"
#include "bot/map_entities/tf_bot_hint_sentrygun.h"
#include "NextBotUtil.h"
ConVar tf_bot_engineer_retaliate_range( "tf_bot_engineer_retaliate_range", "750", FCVAR_CHEAT, "If attacker who destroyed sentry is closer than this, attack. Otherwise, retreat" );
ConVar tf_bot_engineer_exit_near_sentry_range( "tf_bot_engineer_exit_near_sentry_range", "2500", FCVAR_CHEAT, "Maximum travel distance between a bot's Sentry gun and its Teleporter Exit" );
ConVar tf_bot_engineer_max_sentry_travel_distance_to_point( "tf_bot_engineer_max_sentry_travel_distance_to_point", "2500", FCVAR_CHEAT, "Maximum travel distance between a bot's Sentry gun and the currently contested point" );
extern ConVar tf_bot_path_lookahead_range;
const int MaxPlacementAttempts = 5;
//---------------------------------------------------------------------------------------------
CTFBotEngineerBuilding::CTFBotEngineerBuilding( void )
{
m_sentryBuildHint = NULL;
}
//---------------------------------------------------------------------------------------------
CTFBotEngineerBuilding::CTFBotEngineerBuilding( CTFBotHintSentrygun *sentryBuildHint )
{
m_sentryBuildHint = sentryBuildHint;
}
//---------------------------------------------------------------------------------------------
ActionResult< CTFBot > CTFBotEngineerBuilding::OnStart( CTFBot *me, Action< CTFBot > *priorAction )
{
m_sentryTriesLeft = MaxPlacementAttempts;
m_territoryRangeTimer.Invalidate();
m_hasBuiltSentry = false;
m_isSentryOutOfPosition = false;
m_nearbyMetalStatus = NEARBY_METAL_UNKNOWN;
return Continue();
}
//---------------------------------------------------------------------------------------------
// Everything is built, upgrade/maintain it
// TODO: Upgrade/maintain nearby friendly buildings, too.
void CTFBotEngineerBuilding::UpgradeAndMaintainBuildings( CTFBot *me )
{
CObjectSentrygun *mySentry = (CObjectSentrygun *)me->GetObjectOfType( OBJ_SENTRYGUN );
CObjectDispenser *myDispenser = (CObjectDispenser *)me->GetObjectOfType( OBJ_DISPENSER );
if ( !mySentry )
{
return;
}
CBaseCombatWeapon *wrench = me->Weapon_GetSlot( TF_WPN_TYPE_MELEE );
if ( wrench )
{
me->Weapon_Switch( wrench );
}
const float tooFarRange = 75.0f;
if ( !myDispenser )
{
// just work on our sentry
float rangeToSentry = me->GetDistanceBetween( mySentry );
if ( rangeToSentry < 1.2f * tooFarRange )
{
// crouch both for cover behind our buildings, but also to slow us down so we hit our move goal more accurately
me->PressCrouchButton();
}
if ( rangeToSentry > tooFarRange )
{
if ( m_repathTimer.IsElapsed() )
{
m_repathTimer.Start( RandomFloat( 1.0f, 2.0f ) );
CTFBotPathCost cost( me, FASTEST_ROUTE );
m_path.Compute( me, mySentry->GetAbsOrigin(), cost );
}
m_path.Update( me );
}
else
{
// we are in position - work on our buildings
me->StopLookingAroundForEnemies();
me->GetBodyInterface()->AimHeadTowards( mySentry->WorldSpaceCenter(), IBody::CRITICAL, 1.0f, NULL, "Work on my Sentry" );
me->PressFireButton();
}
return;
}
// sit near both buildings
Vector betweenMyBuildings = ( mySentry->GetAbsOrigin() + myDispenser->GetAbsOrigin() ) / 2.0f;
// try to equalize distance between both
float rangeToSentry = me->GetDistanceBetween( mySentry );
float rangeToDispenser = me->GetDistanceBetween( myDispenser );
const float equalTolerance = 25.0f;
if ( rangeToSentry < 1.2f * tooFarRange && rangeToDispenser < 1.2f * tooFarRange )
{
// crouch both for cover behind our buildings, but also to slow us down so we hit our move goal more accurately
me->PressCrouchButton();
}
if ( fabs( rangeToDispenser - rangeToSentry ) > equalTolerance || rangeToSentry > tooFarRange || rangeToDispenser > tooFarRange )
{
if ( m_repathTimer.IsElapsed() )
{
m_repathTimer.Start( RandomFloat( 1.0f, 2.0f ) );
CTFBotPathCost cost( me, FASTEST_ROUTE );
m_path.Compute( me, betweenMyBuildings, cost );
}
m_path.Update( me );
}
if ( rangeToSentry < tooFarRange || rangeToDispenser < tooFarRange )
{
// we are (nearly) in position - work on our buildings
m_searchTimer.Invalidate();
CBaseObject *workTarget = mySentry;
if ( mySentry->HasSapper() || mySentry->IsPlasmaDisabled() )
workTarget = mySentry;
else if ( myDispenser->HasSapper() || myDispenser->IsPlasmaDisabled() )
workTarget = myDispenser;
else if ( mySentry->GetTimeSinceLastInjury() < 1.0f || mySentry->GetHealth() < mySentry->GetMaxHealth() )
workTarget = mySentry;
else if ( mySentry->IsBuilding() )
workTarget = mySentry;
else if ( myDispenser->IsBuilding() )
workTarget = myDispenser;
else if ( mySentry->GetUpgradeLevel() < 3 )
workTarget = mySentry;
else if ( myDispenser->GetHealth() < myDispenser->GetMaxHealth() )
workTarget = myDispenser;
else if ( myDispenser->GetUpgradeLevel() < mySentry->GetUpgradeLevel() )
workTarget = myDispenser;
me->StopLookingAroundForEnemies();
me->GetBodyInterface()->AimHeadTowards( workTarget->WorldSpaceCenter(), IBody::CRITICAL, 1.0f, NULL, "Work on my buildings" );
me->PressFireButton();
}
}
//---------------------------------------------------------------------------------------------
bool CTFBotEngineerBuilding::IsMetalSourceNearby( CTFBot *me ) const
{
CUtlVector< CNavArea * > nearbyVector;
CollectSurroundingAreas( &nearbyVector, me->GetLastKnownArea(), 2000.0f, me->GetLocomotionInterface()->GetStepHeight(), me->GetLocomotionInterface()->GetStepHeight() );
for( int i=0; i<nearbyVector.Count(); ++i )
{
CTFNavArea *area = (CTFNavArea *)nearbyVector[i];
if ( area->HasAttributeTF( TF_NAV_HAS_AMMO ) )
{
return true;
}
// this assumes all spawn rooms have resupply cabinets
if ( me->GetTeamNumber() == TF_TEAM_RED && area->HasAttributeTF( TF_NAV_SPAWN_ROOM_RED ) )
{
return true;
}
if ( me->GetTeamNumber() == TF_TEAM_BLUE && area->HasAttributeTF( TF_NAV_SPAWN_ROOM_BLUE ) )
{
return true;
}
}
return false;
}
//---------------------------------------------------------------------------------------------
bool CTFBotEngineerBuilding::CheckIfSentryIsOutOfPosition( CTFBot *me ) const
{
CObjectSentrygun *mySentry = (CObjectSentrygun *)me->GetObjectOfType( OBJ_SENTRYGUN );
if ( !mySentry )
{
return false;
}
// payload
if ( TFGameRules()->GetGameType() == TF_GAMETYPE_ESCORT )
{
CTeamTrainWatcher *trainWatcher;
if ( me->GetTeamNumber() == TF_TEAM_BLUE )
{
trainWatcher = TFGameRules()->GetPayloadToPush( me->GetTeamNumber() );
}
else
{
trainWatcher = TFGameRules()->GetPayloadToBlock( me->GetTeamNumber() );
}
if ( trainWatcher )
{
float sentryDistanceAlongPath;
trainWatcher->ProjectPointOntoPath( mySentry->GetAbsOrigin(), NULL, &sentryDistanceAlongPath );
const float behindTrainTolerance = SENTRY_MAX_RANGE;
return ( trainWatcher->GetTrainDistanceAlongTrack() > sentryDistanceAlongPath + behindTrainTolerance );
}
}
// control points
mySentry->UpdateLastKnownArea();
CNavArea *sentryArea = mySentry->GetLastKnownArea();
CTeamControlPoint *point = me->GetMyControlPoint();
if ( point )
{
CTFNavArea *pointArea = TheTFNavMesh()->GetControlPointCenterArea( point->GetPointIndex() );
if ( sentryArea && pointArea )
{
CTFBotPathCost cost( me, FASTEST_ROUTE );
if ( NavAreaTravelDistance( sentryArea, pointArea, cost, tf_bot_engineer_max_sentry_travel_distance_to_point.GetFloat() ) < 0 &&
NavAreaTravelDistance( pointArea, sentryArea, cost, tf_bot_engineer_max_sentry_travel_distance_to_point.GetFloat() ) < 0 )
{
return true;
}
}
}
return false;
}
//---------------------------------------------------------------------------------------------
ActionResult< CTFBot > CTFBotEngineerBuilding::Update( CTFBot *me, float interval )
{
CObjectSentrygun *mySentry = (CObjectSentrygun *)me->GetObjectOfType( OBJ_SENTRYGUN );
CObjectDispenser *myDispenser = (CObjectDispenser *)me->GetObjectOfType( OBJ_DISPENSER );
CObjectTeleporter *myTeleportEntrance = (CObjectTeleporter *)me->GetObjectOfType( OBJ_TELEPORTER, MODE_TELEPORTER_ENTRANCE );
CObjectTeleporter *myTeleportExit = (CObjectTeleporter *)me->GetObjectOfType( OBJ_TELEPORTER, MODE_TELEPORTER_EXIT );
bool isUnderAttack = ( me->GetTimeSinceLastInjury() < 1.0f );
isUnderAttack |= ( mySentry && ( mySentry->HasSapper() || mySentry->IsPlasmaDisabled() ) );
isUnderAttack |= ( myDispenser && ( myDispenser->HasSapper() || myDispenser->IsPlasmaDisabled() ) );
me->StartLookingAroundForEnemies();
// try to build a Sentry
if ( !mySentry )
{
m_nearbyMetalStatus = NEARBY_METAL_UNKNOWN;
// react to nearby threats if our sentry is down
const CKnownEntity *threat = me->GetVisionInterface()->GetPrimaryKnownThreat();
if ( threat && threat->IsVisibleRecently() )
{
me->EquipBestWeaponForThreat( threat );
}
if ( !m_hasBuiltSentry && m_sentryTriesLeft > 0 )
{
--m_sentryTriesLeft;
if ( m_sentryBuildHint )
{
return SuspendFor( new CTFBotEngineerBuildSentryGun( m_sentryBuildHint ), "Building a Sentry at a hint location" );
}
return SuspendFor( new CTFBotEngineerBuildSentryGun, "Building a Sentry" );
}
else
{
// can't build a Sentry here - pick a new place
return ChangeTo( new CTFBotEngineerMoveToBuild, "Couldn't find a place to build" );
}
}
// I have a Sentry
m_hasBuiltSentry = true;
if ( m_sentryBuildHint != NULL && !m_sentryBuildHint->IsEnabled() )
{
// our hint has been disabled and no longer has influence on our behavior
m_sentryBuildHint = NULL;
}
// periodically check that our Sentry is still near the contested point
if ( m_sentryBuildHint == NULL || !m_sentryBuildHint->IsSticky() )
{
if ( !m_isSentryOutOfPosition && m_territoryRangeTimer.IsElapsed() )
{
m_territoryRangeTimer.Start( RandomFloat( 3.0f, 5.0f ) );
m_isSentryOutOfPosition = CheckIfSentryIsOutOfPosition( me );
}
m_isSentryOutOfPosition = false;
if ( m_isSentryOutOfPosition )
{
// the point has moved, only keep sentry as long as it keeps attacking
if ( mySentry->GetTimeSinceLastFired() > 10.0f )
{
mySentry->DetonateObject();
// if we built here because of a hint, disable that hint so we don't use it and rebuild here again
if ( m_sentryBuildHint != NULL )
{
inputdata_t dummy;
m_sentryBuildHint->InputDisable( dummy );
m_sentryBuildHint = NULL;
}
if ( myDispenser )
{
myDispenser->DetonateObject();
}
if ( myTeleportExit )
{
myTeleportExit->DetonateObject();
}
me->SpeakConceptIfAllowed( MP_CONCEPT_PLAYER_MOVEUP );
return ChangeTo( new CTFBotEngineerMoveToBuild, "Need to move my gear closer to the point!" );
}
}
}
// if my dispenser is too far away from my sentry, destroy and rebuild it next update
// @TODO: Flag hint-built entities for a larger range
const float maxSeparation = 500.0f;
if ( myDispenser )
{
if ( ( mySentry->GetAbsOrigin() - myDispenser->GetAbsOrigin() ).IsLengthGreaterThan( maxSeparation ) )
{
myDispenser->DestroyObject();
myDispenser = NULL;
}
}
// build up the sentry all the way if there is a metal source nearby
if ( mySentry->GetUpgradeLevel() < 3 )
{
if ( m_nearbyMetalStatus == NEARBY_METAL_UNKNOWN )
{
m_nearbyMetalStatus = IsMetalSourceNearby( me ) ? NEARBY_METAL_EXISTS : NEARBY_METAL_NONE;
}
if ( m_nearbyMetalStatus == NEARBY_METAL_EXISTS )
{
UpgradeAndMaintainBuildings( me );
return Continue();
}
}
/*
if ( myTeleportExit )
{
// if my teleporter exit is too far away from my sentry, destroy and rebuild it next update
if ( ( mySentry->GetAbsOrigin() - myTeleportExit->GetAbsOrigin() ).IsLengthGreaterThan( maxSeparation ) )
{
myTeleportExit->DestroyObject();
myTeleportExit = NULL;
}
}
*/
// try to build a Dispenser (build after tele exit in training)
if ( !TFGameRules()->IsInTraining() || myTeleportExit )
{
const float dispenserRebuildInterval = 10.0f;
if ( myDispenser )
{
// don't rebuild immediately after building is destroyed
m_dispenserRetryTimer.Start( dispenserRebuildInterval );
}
else if ( m_dispenserRetryTimer.IsElapsed() && !isUnderAttack )
{
m_dispenserRetryTimer.Start( dispenserRebuildInterval );
return SuspendFor( new CTFBotEngineerBuildDispenser, "Building a Dispenser" );
}
}
// try to build a Teleporter Exit
const float exitRebuildInterval = TFGameRules()->IsInTraining() ? 5.0f : 30.0f;
if ( myTeleportExit )
{
// don't rebuild immediately after building is destroyed
m_teleportExitRetryTimer.Start( exitRebuildInterval );
}
else if ( m_teleportExitRetryTimer.IsElapsed() && myTeleportEntrance && !isUnderAttack )
{
m_teleportExitRetryTimer.Start( exitRebuildInterval );
// we need to build a teleporter exit yet
if ( m_sentryBuildHint != NULL )
{
// if there are teleporter exit hints, find the closest one to our sentry and use it
CUtlVector< CBaseEntity * > hintVector;
CTFBotHintTeleporterExit *hint = NULL;
while( ( hint = (CTFBotHintTeleporterExit *)gEntList.FindEntityByClassname( hint, "bot_hint_teleporter_exit" ) ) != NULL )
{
if ( hint->IsEnabled() && hint->InSameTeam( me ) )
{
hintVector.AddToTail( hint );
}
}
if ( hintVector.Count() > 0 )
{
mySentry->UpdateLastKnownArea();
CBaseEntity *closeHint = SelectClosestEntityByTravelDistance( me, hintVector, mySentry->GetLastKnownArea(), tf_bot_engineer_exit_near_sentry_range.GetFloat() );
if ( closeHint )
{
return SuspendFor( new CTFBotEngineerBuildTeleportExit( closeHint->GetAbsOrigin(), closeHint->GetAbsAngles().y ), "Building teleporter exit at nearby hint" );
}
}
}
else if ( me->IsRangeLessThan( mySentry, 300.0f ) )
{
// drop a teleporter exit near our sentry
return SuspendFor( new CTFBotEngineerBuildTeleportExit(), "Building teleporter exit" );
}
}
// everything is built - maintain them
UpgradeAndMaintainBuildings( me );
return Continue();
}
//---------------------------------------------------------------------------------------------
void CTFBotEngineerBuilding::OnEnd( CTFBot *me, Action< CTFBot > *nextAction )
{
me->StartLookingAroundForEnemies();
}
//---------------------------------------------------------------------------------------------
ActionResult< CTFBot > CTFBotEngineerBuilding::OnResume( CTFBot *me, Action< CTFBot > *interruptingAction )
{
return Continue();
}
//---------------------------------------------------------------------------------------------
EventDesiredResult< CTFBot > CTFBotEngineerBuilding::OnTerritoryLost( CTFBot *me, int territoryID )
{
return TryContinue();
}
//---------------------------------------------------------------------------------------------
EventDesiredResult< CTFBot > CTFBotEngineerBuilding::OnTerritoryCaptured( CTFBot *me, int territoryID )
{
return TryContinue();
}
|