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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#include "cbase.h"
#include "ai_localnavigator.h"
#include "ai_basenpc.h"
#include "ai_planesolver.h"
#include "ai_moveprobe.h"
#include "ai_motor.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
ConVar ai_debug_directnavprobe("ai_debug_directnavprobe", "0");
const float TIME_DELAY_FULL_DIRECT_PROBE[2] = { 0.25, 0.35 };
//-----------------------------------------------------------------------------
BEGIN_SIMPLE_DATADESC(CAI_LocalNavigator)
// m_fLastWasClear (not saved)
// m_LastMoveGoal (not saved)
// m_FullDirectTimer (not saved)
// m_pPlaneSolver (not saved)
// m_pMoveProbe (not saved)
END_DATADESC();
//-------------------------------------
CAI_LocalNavigator::CAI_LocalNavigator(CAI_BaseNPC *pOuter) : CAI_Component( pOuter )
{
m_pMoveProbe = NULL;
m_pPlaneSolver = new CAI_PlaneSolver( pOuter );
m_fLastWasClear = false;
memset( &m_LastMoveGoal, 0, sizeof(m_LastMoveGoal) );
}
//-------------------------------------
CAI_LocalNavigator::~CAI_LocalNavigator()
{
delete m_pPlaneSolver;
}
//-------------------------------------
void CAI_LocalNavigator::Init( IAI_MovementSink *pMovementServices )
{
CAI_ProxyMovementSink::Init( pMovementServices );
m_pMoveProbe = GetOuter()->GetMoveProbe(); // @TODO (toml 03-30-03): this is a "bad" way to grab this pointer. Components should have an explcit "init" phase.
}
//-------------------------------------
void CAI_LocalNavigator::ResetMoveCalculations()
{
m_FullDirectTimer.Force();
m_pPlaneSolver->Reset();
}
//-------------------------------------
void CAI_LocalNavigator::AddObstacle( const Vector &pos, float radius, AI_MoveSuggType_t type )
{
m_pPlaneSolver->AddObstacle( pos, radius, NULL, type );
}
//-------------------------------------
bool CAI_LocalNavigator::HaveObstacles()
{
return m_pPlaneSolver->HaveObstacles();
}
//-------------------------------------
bool CAI_LocalNavigator::MoveCalcDirect( AILocalMoveGoal_t *pMoveGoal, bool bOnlyCurThink, float *pDistClear, AIMoveResult_t *pResult )
{
AI_PROFILE_SCOPE(CAI_LocalNavigator_MoveCalcDirect);
bool bRetVal = false;
if ( pMoveGoal->speed )
{
CAI_Motor *pMotor = GetOuter()->GetMotor();
float minCheckDist = pMotor->MinCheckDist();
float probeDist = m_pPlaneSolver->CalcProbeDist( pMoveGoal->speed ); // having this match steering allows one fewer traces
float checkDist = MAX( minCheckDist, probeDist );
float checkStepDist = MAX( 16.0, probeDist * 0.5 );
if ( pMoveGoal->flags & ( AILMG_TARGET_IS_TRANSITION | AILMG_TARGET_IS_GOAL ) )
{
// clamp checkDist to be no farther than max distance to goal
checkDist = MIN( checkDist, pMoveGoal->maxDist );
}
if ( checkDist <= 0.0 )
{
*pResult = AIMR_OK;
return true;
}
float moveThisInterval = pMotor->CalcIntervalMove();
bool bExpectingArrival = (moveThisInterval >= checkDist);
if ( !m_FullDirectTimer.Expired() )
{
if ( !m_fLastWasClear ||
( !VectorsAreEqual(pMoveGoal->target, m_LastMoveGoal.target, 0.1) ||
!VectorsAreEqual(pMoveGoal->dir, m_LastMoveGoal.dir, 0.1) ) ||
bExpectingArrival )
{
m_FullDirectTimer.Force();
}
}
if ( bOnlyCurThink ) // Outer code claims to have done a validation (probably a simplify operation)
{
m_FullDirectTimer.Set( TIME_DELAY_FULL_DIRECT_PROBE[AIStrongOpt()] );
}
// First, check the probable move for this cycle
bool bTraceClear = true;
Vector testPos;
if ( !bExpectingArrival )
{
testPos = GetLocalOrigin() + pMoveGoal->dir * moveThisInterval;
bTraceClear = GetMoveProbe()->MoveLimit( pMoveGoal->navType, GetLocalOrigin(), testPos,
MASK_NPCSOLID, pMoveGoal->pMoveTarget,
100.0,
( pMoveGoal->navType == NAV_GROUND ) ? AIMLF_2D : AIMLF_DEFAULT,
&pMoveGoal->directTrace );
if ( !bTraceClear )
{
// Adjust probe top match expected probe dist (relied on later in process)
pMoveGoal->directTrace.flDistObstructed = (checkDist - moveThisInterval) + pMoveGoal->directTrace.flDistObstructed;
}
if ( !IsRetail() && ai_debug_directnavprobe.GetBool() )
{
if ( !bTraceClear )
{
DevMsg( GetOuter(), "Close obstruction %f\n", checkDist - pMoveGoal->directTrace.flDistObstructed );
NDebugOverlay::Line( WorldSpaceCenter(), Vector( testPos.x, testPos.y, WorldSpaceCenter().z ), 255, 0, 0, false, 0.1 );
if ( pMoveGoal->directTrace.pObstruction )
NDebugOverlay::Line( WorldSpaceCenter(), pMoveGoal->directTrace.pObstruction->WorldSpaceCenter(), 255, 0, 255, false, 0.1 );
}
else
{
NDebugOverlay::Line( WorldSpaceCenter(), Vector( testPos.x, testPos.y, WorldSpaceCenter().z ), 0, 255, 0, false, 0.1 );
}
}
pMoveGoal->thinkTrace = pMoveGoal->directTrace;
}
// Now project out for future obstructions
if ( bTraceClear )
{
if ( m_FullDirectTimer.Expired() )
{
testPos = GetLocalOrigin() + pMoveGoal->dir * checkDist;
float checkStepPct = (checkStepDist / checkDist) * 100.0;
if ( checkStepPct > 100.0 )
checkStepPct = 100.0;
bTraceClear = GetMoveProbe()->MoveLimit( pMoveGoal->navType, GetLocalOrigin(), testPos,
MASK_NPCSOLID, pMoveGoal->pMoveTarget,
checkStepPct,
( pMoveGoal->navType == NAV_GROUND ) ? AIMLF_2D : AIMLF_DEFAULT,
&pMoveGoal->directTrace );
if ( bExpectingArrival )
pMoveGoal->thinkTrace = pMoveGoal->directTrace;
if (ai_debug_directnavprobe.GetBool() )
{
if ( !bTraceClear )
{
NDebugOverlay::Line( GetOuter()->EyePosition(), Vector( testPos.x, testPos.y, GetOuter()->EyePosition().z ), 255, 0, 0, false, 0.1 );
DevMsg( GetOuter(), "Obstruction %f\n", checkDist - pMoveGoal->directTrace.flDistObstructed );
}
else
{
NDebugOverlay::Line( GetOuter()->EyePosition(), Vector( testPos.x, testPos.y, GetOuter()->EyePosition().z ), 0, 255, 0, false, 0.1 );
DevMsg( GetOuter(), "No obstruction\n" );
}
}
}
else
{
if ( ai_debug_directnavprobe.GetBool() )
DevMsg( GetOuter(), "No obstruction (Near probe only)\n" );
}
}
pMoveGoal->bHasTraced = true;
float distClear = checkDist - pMoveGoal->directTrace.flDistObstructed;
if (distClear < 0.001)
distClear = 0;
if ( bTraceClear )
{
*pResult = AIMR_OK;
bRetVal = true;
m_fLastWasClear = true;
}
else if ( ( pMoveGoal->flags & ( AILMG_TARGET_IS_TRANSITION | AILMG_TARGET_IS_GOAL ) ) &&
pMoveGoal->maxDist < distClear )
{
*pResult = AIMR_OK;
bRetVal = true;
m_fLastWasClear = true;
}
else
{
*pDistClear = distClear;
m_fLastWasClear = false;
}
}
else
{
// Should never end up in this function with speed of zero. Probably an activity problem.
*pResult = AIMR_ILLEGAL;
bRetVal = true;
}
m_LastMoveGoal = *pMoveGoal;
if ( bRetVal && m_FullDirectTimer.Expired() )
m_FullDirectTimer.Set( TIME_DELAY_FULL_DIRECT_PROBE[AIStrongOpt()] );
return bRetVal;
}
//-------------------------------------
ConVar ai_no_steer( "ai_no_steer", "0" );
bool CAI_LocalNavigator::MoveCalcSteer( AILocalMoveGoal_t *pMoveGoal, float distClear, AIMoveResult_t *pResult )
{
if ( (pMoveGoal->flags & AILMG_NO_STEER) )
return false;
if ( ai_no_steer.GetBool() )
return false;
if ( GetOuter()->IsFlaggedEfficient() )
return false;
AI_PROFILE_SCOPE(CAI_Motor_MoveCalcSteer);
Vector moveSolution;
if ( m_pPlaneSolver->Solve( *pMoveGoal, distClear, &moveSolution ) )
{
if ( moveSolution != pMoveGoal->dir )
{
float dot = moveSolution.AsVector2D().Dot( pMoveGoal->dir.AsVector2D() );
const float COS_HALF_30 = 0.966;
if ( dot > COS_HALF_30 )
{
float probeDist = m_pPlaneSolver->CalcProbeDist( pMoveGoal->speed );
if ( pMoveGoal->maxDist < probeDist * 0.33333 && distClear > probeDist * 0.6666)
{
// A waypoint is coming up, but there's probably time to steer
// away after hitting it
*pResult = AIMR_OK;
return true;
}
}
pMoveGoal->facing = pMoveGoal->dir = moveSolution;
}
*pResult = AIMR_OK;
return true;
}
return false;
}
//-------------------------------------
bool CAI_LocalNavigator::MoveCalcStop( AILocalMoveGoal_t *pMoveGoal, float distClear, AIMoveResult_t *pResult )
{
if (distClear < pMoveGoal->maxDist)
{
if ( distClear < 0.1 )
{
DebugNoteMovementFailure();
*pResult = AIMR_ILLEGAL;
}
else
{
pMoveGoal->maxDist = distClear;
*pResult = AIMR_OK;
}
return true;
}
*pResult = AIMR_OK;
return true;
}
//-------------------------------------
#ifdef DEBUG
#define SetSolveCookie() pMoveGoal->solveCookie = __LINE__;
#else
#define SetSolveCookie() ((void)0)
#endif
AIMoveResult_t CAI_LocalNavigator::MoveCalcRaw( AILocalMoveGoal_t *pMoveGoal, bool bOnlyCurThink )
{
AI_PROFILE_SCOPE(CAI_Motor_MoveCalc);
AIMoveResult_t result = AIMR_OK; // Assume success
AIMoveTrace_t directTrace;
float distClear;
// --------------------------------------------------
bool bDirectClear = MoveCalcDirect( pMoveGoal, bOnlyCurThink, &distClear, &result);
if ( OnCalcBaseMove( pMoveGoal, distClear, &result ) )
{
SetSolveCookie();
return DbgResult( result );
}
bool bShouldSteer = ( !(pMoveGoal->flags & AILMG_NO_STEER) && ( !bDirectClear || HaveObstacles() ) );
if ( bDirectClear && !bShouldSteer )
{
SetSolveCookie();
return DbgResult( result );
}
// --------------------------------------------------
if ( bShouldSteer )
{
if ( !bDirectClear )
{
if ( OnObstructionPreSteer( pMoveGoal, distClear, &result ) )
{
SetSolveCookie();
return DbgResult( result );
}
}
if ( MoveCalcSteer( pMoveGoal, distClear, &result ) )
{
SetSolveCookie();
return DbgResult( result );
}
}
if ( OnFailedSteer( pMoveGoal, distClear, &result ) )
{
SetSolveCookie();
return DbgResult( result );
}
// --------------------------------------------------
if ( OnFailedLocalNavigation( pMoveGoal, distClear, &result ) )
{
SetSolveCookie();
return DbgResult( result );
}
if ( distClear < GetOuter()->GetMotor()->MinStoppingDist() )
{
if ( OnInsufficientStopDist( pMoveGoal, distClear, &result ) )
{
SetSolveCookie();
return DbgResult( result );
}
if ( MoveCalcStop( pMoveGoal, distClear, &result) )
{
SetSolveCookie();
return DbgResult( result );
}
}
// A hopeful result... may get in trouble at next waypoint and obstruction is still there
if ( distClear > pMoveGoal->curExpectedDist )
{
SetSolveCookie();
return DbgResult( AIMR_OK );
}
// --------------------------------------------------
DebugNoteMovementFailure();
SetSolveCookie();
return DbgResult( IsMoveBlocked( pMoveGoal->directTrace.fStatus ) ? pMoveGoal->directTrace.fStatus : AIMR_ILLEGAL );
}
//-------------------------------------
AIMoveResult_t CAI_LocalNavigator::MoveCalc( AILocalMoveGoal_t *pMoveGoal, bool bPreviouslyValidated )
{
bool bOnlyCurThink = ( bPreviouslyValidated && !HaveObstacles() );
AIMoveResult_t result = MoveCalcRaw( pMoveGoal, bOnlyCurThink );
if ( pMoveGoal->curExpectedDist > pMoveGoal->maxDist )
pMoveGoal->curExpectedDist = pMoveGoal->maxDist;
// If success, try to dampen really fast turning movement
if ( result == AIMR_OK)
{
float interval = GetOuter()->GetMotor()->GetMoveInterval();
float currentYaw = UTIL_AngleMod( GetLocalAngles().y );
float goalYaw;
float deltaYaw;
float speed;
float clampedYaw;
// Clamp yaw
goalYaw = UTIL_VecToYaw( pMoveGoal->facing );
deltaYaw = fabs( UTIL_AngleDiff( goalYaw, currentYaw ) );
if ( deltaYaw > 15 )
{
speed = deltaYaw * 4.0; // i.e., any maneuver takes a quarter a second
clampedYaw = AI_ClampYaw( speed, currentYaw, goalYaw, interval );
if ( clampedYaw != goalYaw )
{
pMoveGoal->facing = UTIL_YawToVector( clampedYaw );
}
}
}
return result;
}
//-----------------------------------------------------------------------------
|