aboutsummaryrefslogtreecommitdiff
path: root/mp/src/game/server/ai_planesolver.h
blob: 867d1e813c903b2dd115e55afa44051565e395e7 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//

#ifndef AI_PLANE_SOLVER_H
#define AI_PLANE_SOLVER_H

#ifdef _WIN32
#pragma once
#endif

#include "utlvector.h"
#include "ai_movesolver.h"
#include "ehandle.h"
#include "mathlib/vector.h"
#include "simtimer.h"
#include "ai_navtype.h"

//-----------------------------------------------------------------------------
// Forward declarations
//-----------------------------------------------------------------------------
class Vector2D;
class CBaseEntity;
struct edict_t;
class CAI_BaseNPC;
class CAI_Motor;
class CAI_Navigator;
struct AILocalMoveGoal_t;
struct AIMoveTrace_t;

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

enum AI_SuggestorResult_t
{
	SR_NONE,
	SR_OK,
	SR_FAIL
};

class CAI_PlaneSolver
{
public:
	// constructor
	CAI_PlaneSolver( CAI_BaseNPC *pNpc );
	
	// Attempt to find a valid move direction for the specifed goal
	bool Solve( const AILocalMoveGoal_t &goal, float distClear, Vector *pSolution );

	float CalcProbeDist( float speed );
	
	// Flush any cached results (e.g., hull changed, results not valid)
	void Reset();
	
	void AddObstacle( const Vector &pos, float radius, CBaseEntity *pEntity = NULL, AI_MoveSuggType_t type = AIMST_AVOID_OBJECT );
	bool HaveObstacles()	{ return ( m_Obstacles.Count() != 0 ); }

private:

	enum 
	{
		DEGREES_POSITIVE_ARC				   = 270,
		DEGREES_POSITIVE_ARC_CLOSE_OBSTRUCTION = 340,
		NUM_PROBES 							   = 5
	};

	// How far ahead does the ground solver look (seconds)
	float GetLookaheadTime() 		{ return 1.0; }

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

	// For debugging purposes
	void VisualizeRegulations();
	void VisualizeSolution( const Vector &vecGoal, const Vector& vecActual );

	// --------------------------------
	bool MoveLimit( Navigation_t navType, const Vector &target, bool ignoreTransients, bool fCheckStep, AIMoveTrace_t *pMoveTrace );
	bool MoveLimit( Navigation_t navType, const Vector &target, bool ignoreTransients, bool fCheckStep, int contents, AIMoveTrace_t *pMoveTrace );

	//-----------------------------------------------------------------------------
	// Adjust the solution for fliers
	//-----------------------------------------------------------------------------
	void AdjustSolutionForFliers( const AILocalMoveGoal_t &goal, float flSolutionYaw, Vector *pSolution );

	// --------------------------------
	// Convenience accessors
	
	CAI_BaseNPC * 	GetNpc();
	CAI_Motor * 	GetMotor();
	const Vector &	GetLocalOrigin();

	// --------------------------------
	
	void				 GenerateObstacleNpcs( const AILocalMoveGoal_t &goal, float probeDist );
	AI_SuggestorResult_t GenerateObstacleSuggestions( const AILocalMoveGoal_t &goal, const AIMoveTrace_t &directTrace, float distClear, float probeDist, float degreesToProbe, int nProbes );
	AI_SuggestorResult_t GenerateObstacleSuggestion( const AILocalMoveGoal_t &goal, float yawScanCenter, float probeDist, float spanPerProbe, int probeOffset );
	void				 GenerateSuggestionFromTrace( const AILocalMoveGoal_t &goal,
													  const AIMoveTrace_t &moveTrace, float probeDist, 
													  float arcCenter, float arcSpan, int probeOffset );
	bool				 GenerateCircleObstacleSuggestions( const AILocalMoveGoal_t &moveGoal, float probeDist );

	void				 CalcYawsFromOffset( float yawScanCenter, float spanPerProbe, int probeOffset,
											 float *pYawTest, float *pYawCenter );
	
	float CalculateRegulationWeight( const AIMoveTrace_t &moveTrace, float pctBlockedt );
	float AdjustRegulationWeight( CBaseEntity *pEntity, float weight );
	unsigned ComputeTurnBiasFlags( const AILocalMoveGoal_t &goal, const AIMoveTrace_t &directTrace );


	bool RunMoveSolver( const AILocalMoveGoal_t &goal, const AIMoveTrace_t &directTrace, 
						float degreesPositiveArc, bool fDeterOscillation, 
						Vector *pResult );
	bool DetectUnsolvable( const AILocalMoveGoal_t &goal );

	// --------------------------------
	
	CAI_BaseNPC *		m_pNpc;

	Vector				m_PrevTarget;
	bool				m_fSolvedPrev;
	float 				m_PrevSolution;
	Vector				m_PrevSolutionVector;
	
	float				m_ClosestHaveBeenToCurrent;
	float 				m_TimeLastProgress;

	bool				m_fCannotSolveCurrent;
	
	CSimTimer			m_RefreshSamplesTimer;
	
	// --------------------------------
	
	struct CircleObstacles_t
	{
		CircleObstacles_t( const Vector &center, float radius, CBaseEntity *pEntity, AI_MoveSuggType_t type )
		 :	center(center), 
			radius(radius), 
			hEntity(pEntity),
			type(type)
		{
		}
		
		Vector				center;
		float				radius;
		AI_MoveSuggType_t 	type;
		EHANDLE				hEntity;
	};
	
	CUtlVector<CircleObstacles_t> m_Obstacles;
	
	// --------------------------------
	
	CAI_MoveSolver m_Solver;
};
	
//-------------------------------------

inline void CAI_PlaneSolver::Reset()
{
	m_RefreshSamplesTimer.Force();

	m_fSolvedPrev = false;
	m_PrevTarget.Init( FLT_MAX, FLT_MAX, FLT_MAX ),
	m_PrevSolution = 0;
	m_ClosestHaveBeenToCurrent = FLT_MAX;
	m_TimeLastProgress = FLT_MAX;
	m_fCannotSolveCurrent = false;
}
	
//=============================================================================

#endif // AI_PLANE_SOLVER_H