summaryrefslogtreecommitdiff
path: root/game/server/ai_movetypes.h
blob: b255921e74141b6725fdc9f9772b1e7c0bbcf53e (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//

#ifndef AI_MOVETYPES_H
#define AI_MOVETYPES_H

#if defined( _WIN32 )
#pragma once
#endif

#include "ai_navtype.h"

class CAI_Path;

//-----------------------------------------------------------------------------
// Debugging code
//
// Use this function to set breakpoints to find out where movement is failing
//
#ifdef DEBUG
extern void DebugNoteMovementFailure();
#define DebugNoteMovementFailureIfBlocked( moveResult ) if ( !IsMoveBlocked( moveResult ) ) ((void)0); else DebugNoteMovementFailure()
#else
#define DebugNoteMovementFailure() ((void)0)
#define DebugNoteMovementFailureIfBlocked( moveResult ) ((void)0)
#endif

enum AIMoveResult_t
{
	AIMR_BLOCKED_ENTITY			= -1,	         // Move was blocked by an entity
	AIMR_BLOCKED_WORLD			= -2,	         // Move was blocked by the world
	AIMR_BLOCKED_NPC			= -3,	         // Move was blocked by an NPC
	AIMR_ILLEGAL				= -4,	         // Move is illegal for some reason

	AIMR_OK						= 0,
	
	AIMR_CHANGE_TYPE,	                         // Locomotion method has changed
};


#ifdef DEBUG
extern AIMoveResult_t DbgResult( AIMoveResult_t result );
#else
inline AIMoveResult_t DbgResult( AIMoveResult_t result ) { return result; } // inline not macro for compiler typing
#endif


//-----------------------------------------------------------------------------
// Movement related constants and base types
//-----------------------------------------------------------------------------
#ifdef PHYSICS_NPC_SHADOW_DISCREPENCY
const float AI_EPS_CASTS = 0.3;  // The amount physics and hull cast can disagree
#endif


inline bool IsMoveBlocked( AIMoveResult_t moveResult )
{
	return (moveResult < AIMR_OK );
}

//-------------------------------------

enum StepGroundTest_t
{
	STEP_DONT_CHECK_GROUND = 0,
	STEP_ON_VALID_GROUND,
	STEP_ON_INVALID_GROUND,
};


//-------------------------------------

struct AIMoveTrace_t
{
	AIMoveTrace_t()
	{
		memset( this, 0, sizeof(*this) );
	}
	
	AIMoveResult_t 	fStatus;				// See AIMoveResult_t
	Vector 			vEndPosition;			// The last point that could be moved to
	Vector			vHitNormal;				// The normal of a hit, if any. vec3_origin if none. Can be none even if "hit"
	CBaseEntity* 	pObstruction;			// The obstruction I bumped into (if any)
	float			flTotalDist;
	float 			flDistObstructed; 	// FIXME: This is a strange number. In the case
											// of calling MoveLimit with navtype NAV_GROUND,
											// it represents a 2D distance to the obstruction.
											// In the case of other navtypes, it represents a
											// 3D distance to the obstruction
	Vector 			vJumpVelocity;			// FIXME: Remove this; it's bogus
											// It's only returned by JumpMoveLimit
											// which seems to be a bogus concept to begin with
	float			flStepUpDistance;
};

inline bool IsMoveBlocked( const AIMoveTrace_t &moveTrace )
{
	return (moveTrace.fStatus < AIMR_OK );
}


// Categorizes the blocker and sets the appropriate bits
AIMoveResult_t		AIComputeBlockerMoveResult( CBaseEntity *pBlocker );


//-------------------------------------
// Purpose: Specifies an immediate, localized, straight line movement goal
//-------------------------------------

enum AILocalMoveGoalFlags_t
{
	AILMG_NONE,
	AILMG_TARGET_IS_GOAL		= 0x01,
	AILMG_CONSUME_INTERVAL		= 0x02,
	AILMG_TARGET_IS_TRANSITION 	= 0x04,
	AILMG_NO_STEER 				= 0x08,
	AILMG_NO_AVOIDANCE_PATHS 	= 0x10,
};

struct AILocalMoveGoal_t
{
	AILocalMoveGoal_t()
	{
		memset( this, 0, sizeof(*this) );
	}
	
	// Object of the goal
	Vector			target;

	// The actual move. Note these need not always agree with "target"
	Vector			dir; 
	Vector			facing;
	float			speed;
	
	// The distance maximum distance intended to travel in path length
	float			maxDist;

	// The distance expected to move this think
	float			curExpectedDist;

	Navigation_t	navType;
	CBaseEntity *	pMoveTarget;

	unsigned		flags;

	// The path from which this goal was derived
	CAI_Path *		pPath;

	// The result if a forward probing trace has been done
	bool			bHasTraced;
	AIMoveTrace_t	directTrace;
	AIMoveTrace_t	thinkTrace;

#ifdef DEBUG
	int				solveCookie;
#endif
};

//-------------------------------------

enum AIMotorMoveResult_t
{
	AIM_FAILED,
	AIM_SUCCESS,
	
	// Partial successes
	AIM_PARTIAL_HIT_NPC,
	AIM_PARTIAL_HIT_WORLD,
	AIM_PARTIAL_HIT_TARGET,
	
	AIM_NUM_RESULTS
};

//-----------------------------------------------------------------------------
// Purpose: The set of callbacks used by lower-level movement classes to
//			notify and receive guidance from higher level-classes
//-----------------------------------------------------------------------------

abstract_class IAI_MovementSink
{
public:
	//---------------------------------
	//
	// Queries
	//
	virtual float CalcYawSpeed( void ) = 0;

	//---------------------------------
	//
	// Local navigation notifications, each allows services provider to overridde default result
	//
	virtual bool OnCalcBaseMove( AILocalMoveGoal_t *pMoveGoal, 
								float distClear, 
								AIMoveResult_t *pResult ) = 0;

	virtual bool OnObstructionPreSteer( AILocalMoveGoal_t *pMoveGoal, 
											float distClear, 
											AIMoveResult_t *pResult ) = 0;

	virtual bool OnFailedSteer( AILocalMoveGoal_t *pMoveGoal, 
									float distClear, 
									AIMoveResult_t *pResult ) = 0;

	virtual bool OnFailedLocalNavigation( AILocalMoveGoal_t *pMoveGoal,
									float distClear, 
									AIMoveResult_t *pResult ) = 0;
	
	virtual bool OnInsufficientStopDist( AILocalMoveGoal_t *pMoveGoal, 
											 float distClear, 
											 AIMoveResult_t *pResult ) = 0;


	virtual bool OnMoveBlocked( AIMoveResult_t *pResult ) = 0;
	
	//---------------------------------
	//
	// Motor notifications, each allows services provider to overridde default result
	//
	virtual bool OnMoveStalled( const AILocalMoveGoal_t &move ) = 0;
	virtual bool OnMoveExecuteFailed( const AILocalMoveGoal_t &move, 
									const AIMoveTrace_t &trace, 
									AIMotorMoveResult_t fMotorResult, 
									AIMoveResult_t *pResult ) = 0;
};

//-----------------------------------------------------------------------------
// Purpose: Default implementations of IAI_MovementSink
//-----------------------------------------------------------------------------

class CAI_DefMovementSink : public IAI_MovementSink
{
public:
	//---------------------------------
	//
	// Queries
	//
	virtual float CalcYawSpeed( void ) { return -1.0; } 


	//---------------------------------
	//
	// Local navigation notifications, each allows services provider to overridde default result
	//
	virtual bool OnCalcBaseMove( AILocalMoveGoal_t *pMoveGoal, float distClear, AIMoveResult_t *pResult ) { return false; }
	virtual bool OnObstructionPreSteer( AILocalMoveGoal_t *pMoveGoal, float distClear, AIMoveResult_t *pResult ) { return false;	}
	virtual bool OnFailedSteer( AILocalMoveGoal_t *pMoveGoal, float distClear, AIMoveResult_t *pResult ) { return false;	}
	virtual bool OnFailedLocalNavigation( AILocalMoveGoal_t *pMoveGoal, float distClear, AIMoveResult_t *pResult ) { return false;	}
	virtual bool OnInsufficientStopDist( AILocalMoveGoal_t *pMoveGoal, float distClear, AIMoveResult_t *pResult ) { return false;	}
	virtual bool OnMoveBlocked( AIMoveResult_t *pResult ) { return false;	}
	
	//---------------------------------
	//
	// Motor notifications, each allows services provider to overridde default result
	//
	virtual bool OnMoveStalled( const AILocalMoveGoal_t &move ) { return false;	}
	virtual bool OnMoveExecuteFailed( const AILocalMoveGoal_t &move, const AIMoveTrace_t &trace, AIMotorMoveResult_t fMotorResult, AIMoveResult_t *pResult ) { return false;	}
	
};

//-------------------------------------

class CAI_ProxyMovementSink : public CAI_DefMovementSink
{
public:
	CAI_ProxyMovementSink()
	 :	m_pProxied( NULL )
	{
	}
	
	//---------------------------------
	
	void Init( IAI_MovementSink *pMovementServices ) { m_pProxied = pMovementServices; }
	
	//---------------------------------
	//
	// Queries
	//
	virtual float CalcYawSpeed( void );

	//---------------------------------
	//
	// Local navigation notifications, each allows services provider to overridde default result
	//
	virtual bool OnCalcBaseMove( AILocalMoveGoal_t *pMoveGoal, 
								float distClear, 
								AIMoveResult_t *pResult );

	virtual bool OnObstructionPreSteer( AILocalMoveGoal_t *pMoveGoal, 
											float distClear, 
											AIMoveResult_t *pResult );
	virtual bool OnFailedSteer( AILocalMoveGoal_t *pMoveGoal, 
									float distClear, 
									AIMoveResult_t *pResult );
	virtual bool OnFailedLocalNavigation( AILocalMoveGoal_t *pMoveGoal, 
									float distClear, 
									AIMoveResult_t *pResult );
	virtual bool OnInsufficientStopDist( AILocalMoveGoal_t *pMoveGoal, 
											 float distClear, 
											 AIMoveResult_t *pResult );
	virtual bool OnMoveBlocked( AIMoveResult_t *pResult );
	
	//---------------------------------
	//
	// Motor notifications, each allows services provider to overridde default result
	//
	virtual bool OnMoveStalled( const AILocalMoveGoal_t &move );
	virtual bool OnMoveExecuteFailed( const AILocalMoveGoal_t &move, const AIMoveTrace_t &trace, AIMotorMoveResult_t fMotorResult, AIMoveResult_t *pResult );
	
	IAI_MovementSink *m_pProxied;
};

// ----------------------------------------------------------------------------

inline float CAI_ProxyMovementSink::CalcYawSpeed( void )
{
	float result;
	if ( m_pProxied && ( result = m_pProxied->CalcYawSpeed() ) != -1.0 )
		return result;
	return CAI_DefMovementSink::CalcYawSpeed();
}

inline bool CAI_ProxyMovementSink::OnCalcBaseMove( AILocalMoveGoal_t *pMoveGoal, float distClear, AIMoveResult_t *pResult )
{
	if ( m_pProxied && m_pProxied->OnCalcBaseMove( pMoveGoal, distClear, pResult ) )
		return true;
	return CAI_DefMovementSink::OnCalcBaseMove( pMoveGoal, distClear, pResult );
}

inline bool CAI_ProxyMovementSink::OnObstructionPreSteer( AILocalMoveGoal_t *pMoveGoal, float distClear, AIMoveResult_t *pResult )
{
	if ( m_pProxied && m_pProxied->OnObstructionPreSteer( pMoveGoal, distClear, pResult ) )
		return true;
	return CAI_DefMovementSink::OnObstructionPreSteer( pMoveGoal, distClear, pResult );
}

inline bool CAI_ProxyMovementSink::OnFailedSteer( AILocalMoveGoal_t *pMoveGoal, float distClear, AIMoveResult_t *pResult )
{
	if ( m_pProxied && m_pProxied->OnFailedSteer( pMoveGoal, distClear, pResult ) )
		return true;
	return CAI_DefMovementSink::OnFailedSteer( pMoveGoal, distClear, pResult );
}

inline bool CAI_ProxyMovementSink::OnFailedLocalNavigation( AILocalMoveGoal_t *pMoveGoal, float distClear, AIMoveResult_t *pResult )
{
	if ( m_pProxied && m_pProxied->OnFailedLocalNavigation( pMoveGoal, distClear, pResult ) )
		return true;
	return CAI_DefMovementSink::OnFailedLocalNavigation( pMoveGoal, distClear, pResult );
}

inline bool CAI_ProxyMovementSink::OnInsufficientStopDist( AILocalMoveGoal_t *pMoveGoal, float distClear, AIMoveResult_t *pResult )
{
	if ( m_pProxied && m_pProxied->OnInsufficientStopDist( pMoveGoal, distClear, pResult ) )
		return true;
	return CAI_DefMovementSink::OnInsufficientStopDist( pMoveGoal, distClear, pResult );
}

inline bool CAI_ProxyMovementSink::OnMoveStalled( const AILocalMoveGoal_t &move )
{
	if ( m_pProxied && m_pProxied->OnMoveStalled( move ) )
		return true;
	return CAI_DefMovementSink::OnMoveStalled( move );
}

inline bool CAI_ProxyMovementSink::OnMoveExecuteFailed( const AILocalMoveGoal_t &move, const AIMoveTrace_t &trace, AIMotorMoveResult_t fMotorResult, AIMoveResult_t *pResult )
{
	if ( m_pProxied && m_pProxied->OnMoveExecuteFailed( move, trace, fMotorResult, pResult ) )
		return true;
	return CAI_DefMovementSink::OnMoveExecuteFailed( move, trace, fMotorResult, pResult );
}

inline bool CAI_ProxyMovementSink::OnMoveBlocked( AIMoveResult_t *pResult )
{
	if ( m_pProxied && m_pProxied->OnMoveBlocked( pResult ) )
		return true;
	return CAI_DefMovementSink::OnMoveBlocked( pResult );
}

//=============================================================================

#endif // AI_MOVETYPES_H