summaryrefslogtreecommitdiff
path: root/game/server/tf/bot/tf_bot_vision.cpp
blob: 6fae0ff77ff15e123f2c9844e7b1db45f5f3a76a (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
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
//========= Copyright Valve Corporation, All rights reserved. ============//
// tf_bot_vision.cpp
// Team Fortress NextBot vision interface
// Michael Booth, May 2009

#include "cbase.h"
#include "vprof.h"

#include "tf_bot.h"
#include "tf_bot_vision.h"
#include "tf_player.h"
#include "tf_gamerules.h"
#include "tf_obj_sentrygun.h"

ConVar tf_bot_choose_target_interval( "tf_bot_choose_target_interval", "0.3f", FCVAR_CHEAT, "How often, in seconds, a TFBot can reselect his target" );
ConVar tf_bot_sniper_choose_target_interval( "tf_bot_sniper_choose_target_interval", "3.0f", FCVAR_CHEAT, "How often, in seconds, a zoomed-in Sniper can reselect his target" );


//------------------------------------------------------------------------------------------
// Update internal state
void CTFBotVision::Update( void )
{
	if ( TFGameRules()->IsMannVsMachineMode() )
	{
		// Throttle vision update rate of robots in MvM for perf at the expense of reaction times
		if ( !m_scanTimer.IsElapsed() )
		{
			return;
		}

		m_scanTimer.Start( RandomFloat( 0.9f, 1.1f ) );
	}

	IVision::Update();

	CTFBot *me = (CTFBot *)GetBot()->GetEntity();
	if ( !me )
		return;

	// forget spies we have lost sight of
	CUtlVector< CTFPlayer * > playerVector;
	CollectPlayers( &playerVector, GetEnemyTeam( me->GetTeamNumber() ), COLLECT_ONLY_LIVING_PLAYERS );

	for( int i=0; i<playerVector.Count(); ++i )
	{
		if ( !playerVector[i]->IsPlayerClass( TF_CLASS_SPY ) )
			continue;

		const CKnownEntity *known = GetKnown( playerVector[i] );

		if ( !known || !known->IsVisibleRecently() )
		{
			// if a hidden spy changes disguises, we no longer recognize him
			if ( playerVector[i]->m_Shared.InCond( TF_COND_DISGUISING ) )
			{
				me->ForgetSpy( playerVector[i] );				
			}
		}
	}
}


//------------------------------------------------------------------------------------------
void CTFBotVision::CollectPotentiallyVisibleEntities( CUtlVector< CBaseEntity * > *potentiallyVisible )
{
	VPROF_BUDGET( "CTFBotVision::CollectPotentiallyVisibleEntities", "NextBot" );

	potentiallyVisible->RemoveAll();

	// include all players
	for( int i=1; i<=gpGlobals->maxClients; ++i )
	{
		CBasePlayer *player = UTIL_PlayerByIndex( i );

		if ( player == NULL )
			continue;

		if ( FNullEnt( player->edict() ) )
			continue;

		if ( !player->IsPlayer() )
			continue;

		if ( !player->IsConnected() )
			continue;

		if ( !player->IsAlive() )
			continue;

		potentiallyVisible->AddToTail( player );
	}

	// include sentry guns
	UpdatePotentiallyVisibleNPCVector();

	FOR_EACH_VEC( m_potentiallyVisibleNPCVector, it )
	{
		potentiallyVisible->AddToTail( m_potentiallyVisibleNPCVector[ it ] );
	}
}


//------------------------------------------------------------------------------------------
void CTFBotVision::UpdatePotentiallyVisibleNPCVector( void )
{
	if ( m_potentiallyVisibleUpdateTimer.IsElapsed() )
	{
		m_potentiallyVisibleUpdateTimer.Start( RandomFloat( 3.0f, 4.0f ) );

		// collect list of active buildings
		m_potentiallyVisibleNPCVector.RemoveAll();

		bool bShouldSeeTeleporter = !TFGameRules()->IsMannVsMachineMode() || GetBot()->GetEntity()->GetTeamNumber() != TF_TEAM_PVE_INVADERS;
		for ( int i=0; i<IBaseObjectAutoList::AutoList().Count(); ++i )
		{
			CBaseObject* pObj = static_cast< CBaseObject* >( IBaseObjectAutoList::AutoList()[i] );
			if ( pObj->ObjectType() == OBJ_SENTRYGUN )
			{
				m_potentiallyVisibleNPCVector.AddToTail( pObj );
			}
			else if ( pObj->ObjectType() == OBJ_DISPENSER && pObj->ClassMatches( "obj_dispenser" ) )
			{
				m_potentiallyVisibleNPCVector.AddToTail( pObj );
			}
			else if ( bShouldSeeTeleporter && pObj->ObjectType() == OBJ_TELEPORTER )
			{
				m_potentiallyVisibleNPCVector.AddToTail( pObj );
			}
		}

		CUtlVector< INextBot * > botVector;
		TheNextBots().CollectAllBots( &botVector );
		for( int i=0; i<botVector.Count(); ++i )
		{
			CBaseCombatCharacter *botEntity = botVector[i]->GetEntity();
			if ( botEntity && !botEntity->IsPlayer() )
			{
				// NPC
				m_potentiallyVisibleNPCVector.AddToTail( botEntity );
			}
		}
	}
}


//------------------------------------------------------------------------------------------
/**
 * Return true to completely ignore this entity.
 * This is mostly for enemy spies.  If we don't ignore them, we will look at them.
 */
bool CTFBotVision::IsIgnored( CBaseEntity *subject ) const
{
	CTFBot *me = (CTFBot *)GetBot()->GetEntity();

#ifdef TF_RAID_MODE
	if ( TFGameRules()->IsRaidMode() )
	{
		if ( me->IsPlayerClass( TF_CLASS_SCOUT ) )
		{
			// Scouts are wandering defenders, and aggro purely on proximity or damage, not vision
			return true;
		}
	}
#endif // TF_RAID_MODE

	if ( me->IsAttentionFocused() )
	{
		// our attention is restricted to certain subjects
		if ( !me->IsAttentionFocusedOn( subject ) )
		{
			return false;
		}
	}

	if ( !me->IsEnemy( subject ) )
	{
		// don't ignore friends
		return false;
	}

	if ( subject->IsEffectActive( EF_NODRAW ) )
	{
		return true;
	}

	if ( subject->IsPlayer() )
	{
		CTFPlayer *enemy = static_cast< CTFPlayer * >( subject );

		// test for designer-defined ignorance
		switch( enemy->GetPlayerClass()->GetClassIndex() )
		{
		case TF_CLASS_MEDIC:
			if ( me->IsBehaviorFlagSet( TFBOT_IGNORE_ENEMY_MEDICS ) )
			{
				return true;
			}
			break;

		case TF_CLASS_ENGINEER:
			if ( me->IsBehaviorFlagSet( TFBOT_IGNORE_ENEMY_ENGINEERS ) )
			{
				return true;
			}
			break;

		case TF_CLASS_SNIPER:
			if ( me->IsBehaviorFlagSet( TFBOT_IGNORE_ENEMY_SNIPERS ) )
			{
				return true;
			}
			break;

		case TF_CLASS_SCOUT:
			if ( me->IsBehaviorFlagSet( TFBOT_IGNORE_ENEMY_SCOUTS ) )
			{
				return true;
			}
			break;

		case TF_CLASS_SPY:
			if ( me->IsBehaviorFlagSet( TFBOT_IGNORE_ENEMY_SPIES ) )
			{
				return true;
			}
			break;

		case TF_CLASS_DEMOMAN:
			if ( me->IsBehaviorFlagSet( TFBOT_IGNORE_ENEMY_DEMOMEN ) )
			{
				return true;
			}
			break;

		case TF_CLASS_SOLDIER:
			if ( me->IsBehaviorFlagSet( TFBOT_IGNORE_ENEMY_SOLDIERS ) )
			{
				return true;
			}
			break;

		case TF_CLASS_HEAVYWEAPONS:
			if ( me->IsBehaviorFlagSet( TFBOT_IGNORE_ENEMY_HEAVIES ) )
			{
				return true;
			}
			break;

		case TF_CLASS_PYRO:
			if ( me->IsBehaviorFlagSet( TFBOT_IGNORE_ENEMY_PYROS ) )
			{
				return true;
			}
			break;
		}

#ifdef STAGING_ONLY
		if ( enemy->m_Shared.InCond( TF_COND_REPROGRAMMED ) )
		{
			return true;
		}
#endif // STAGING_ONLY

		if ( me->IsKnownSpy( enemy ) )
		{
			// don't ignore revealed spies
			return false;
		}

		if ( enemy->m_Shared.InCond( TF_COND_BURNING ) ||
			 enemy->m_Shared.InCond( TF_COND_URINE ) ||
			 enemy->m_Shared.InCond( TF_COND_STEALTHED_BLINK ) ||
			 enemy->m_Shared.InCond( TF_COND_BLEEDING ) )
		{
			// always notice players with these conditions
			return false;
		}

		// An upgrade in MvM grants AE stealth where the player can fire
		// while in stealth, and for a short period after it drops
		if ( enemy->m_Shared.InCond( TF_COND_STEALTHED_USER_BUFF_FADING ) )
		{
			return true;
		}

		if ( enemy->m_Shared.IsStealthed() )
		{
			if ( enemy->m_Shared.GetPercentInvisible() < 0.75f )
			{
				// spy is partially cloaked, and therefore attracts our attention
				return false;
			}

			// invisible!
			return true;
		}

		if ( enemy->IsPlacingSapper() )
		{
			return false;
		}

		if ( enemy->m_Shared.InCond( TF_COND_DISGUISING ) )
		{
			return false;
		}
		
		if ( enemy->m_Shared.InCond( TF_COND_DISGUISED ) && enemy->m_Shared.GetDisguiseTeam() == me->GetTeamNumber() )
		{
			// spy is disguised as a member of my team
			return true;
		}
	}
	else if ( subject->IsBaseObject() ) // not a player
	{
		CBaseObject *object = assert_cast< CBaseObject * >( subject );
		if ( object )
		{
			// ignore sapped enemy objects
			if ( object->HasSapper() )
			{
				// unless we're in MvM where buildings can have really large health pools,
				// so an engineer can die and run back in time to repair their stuff
				if ( TFGameRules() && TFGameRules()->IsMannVsMachineMode() )
				{
					return false;
				}

				return true;
			}
			
			// ignore carried objects
			if ( object->IsPlacing() || object->IsCarried() )
			{
				return true;
			}
			
			if ( object->GetType() == OBJ_SENTRYGUN && me->IsBehaviorFlagSet( TFBOT_IGNORE_ENEMY_SENTRY_GUNS ) )
			{
				return true;
			}
		}
	}

	return false;
}


//------------------------------------------------------------------------------------------
// Return true if we 'notice' the subject, even though we have LOS to it
bool CTFBotVision::IsVisibleEntityNoticed( CBaseEntity *subject ) const
{
	CTFBot *me = (CTFBot *)GetBot()->GetEntity();

	if ( subject->IsPlayer() && me->IsEnemy( subject ) )
	{
		CTFPlayer *player = static_cast< CTFPlayer * >( subject );

		if ( player->m_Shared.InCond( TF_COND_BURNING ) ||
			 player->m_Shared.InCond( TF_COND_URINE ) ||
			 player->m_Shared.InCond( TF_COND_STEALTHED_BLINK ) ||
			 player->m_Shared.InCond( TF_COND_BLEEDING ) )
		{
			// always notice players with these conditions
			if ( player->m_Shared.InCond( TF_COND_STEALTHED ) )
			{
				me->RealizeSpy( player );
			}
			return true;
		}

#ifdef STAGING_ONLY
		// Bots can be hacked/reprogrammed by spies.  Ignore.
		if ( player->m_Shared.InCond( TF_COND_REPROGRAMMED ) )
		{
			return false;
		}
#endif // STAGING_ONLY

		// An upgrade in MvM grants AE stealth where the player can fire
		// while in stealth, and for a short period after it drops
		if ( player->m_Shared.InCond( TF_COND_STEALTHED_USER_BUFF_FADING ) )
		{
			me->ForgetSpy( player );
			return false;
		}

		if ( player->m_Shared.IsStealthed() )
		{
			if ( player->m_Shared.GetPercentInvisible() < 0.75f )
			{
				// spy is partially cloaked, and therefore attracts our attention
				me->RealizeSpy( player );
				return true;
			}

			// invisible!
			me->ForgetSpy( player );
			return false;
		}

		if ( TFGameRules()->IsMannVsMachineMode() )	// in MvM mode, forget spies as soon as they are fully disguised
		{
			CTFBot::SuspectedSpyInfo_t* pSuspectInfo = me->IsSuspectedSpy( player );
			// But only if we aren't suspecting them currently.  This happens when we bump into them.
			if( !pSuspectInfo || !pSuspectInfo->IsCurrentlySuspected() )
			{
				if ( player->m_Shared.InCond( TF_COND_DISGUISED ) && player->m_Shared.GetDisguiseTeam() == me->GetTeamNumber() )
				{
					me->ForgetSpy( player );
					return false;
				}
			}
		}

		if ( me->IsKnownSpy( player ) )
		{
			// always notice non-invisible revealed spies
			return true;
		}

		if ( !TFGameRules()->IsMannVsMachineMode() )	// ignore in MvM mode
		{
			if ( player->IsPlacingSapper() )
			{
				// spotted a spy!
				me->RealizeSpy( player );
				return true;
			}
		}

		if ( player->m_Shared.InCond( TF_COND_DISGUISING ) )
		{
			// spotted a spy!
			me->RealizeSpy( player );
			return true;
		}

		if ( player->m_Shared.InCond( TF_COND_DISGUISED ) && player->m_Shared.GetDisguiseTeam() == me->GetTeamNumber() )
		{
			// spy is disguised as a member of my team, don't notice him
			return false;
		}
	}

	return true;
}


//------------------------------------------------------------------------------------------
// Return VISUAL reaction time
float CTFBotVision::GetMinRecognizeTime( void ) const
{
	CTFBot *me = (CTFBot *)GetBot();

	switch ( me->GetDifficulty() )
	{
	case CTFBot::EASY:		return 1.0f;
	case CTFBot::NORMAL:	return 0.5f;
	case CTFBot::HARD:		return 0.3f;
	case CTFBot::EXPERT:	return 0.2f;
	}

	return 1.0f;
}



//------------------------------------------------------------------------------------------
float CTFBotVision::GetMaxVisionRange( void ) const
{
	CTFBot *me = (CTFBot *)GetBot();

	if ( me->GetMaxVisionRangeOverride() > 0.0f )
	{
		// designer specified vision range
		return me->GetMaxVisionRangeOverride();
	}

	// long range, particularly for snipers
	return 6000.0f;
}