forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai_basenpc_flyer.cpp
263 lines (232 loc) · 6.72 KB
/
ai_basenpc_flyer.cpp
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Base class for many flying NPCs
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "ai_basenpc_flyer.h"
#include "ai_route.h"
#include "ai_navigator.h"
#include "ai_motor.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
BEGIN_DATADESC( CAI_BaseFlyingBot )
DEFINE_FIELD( m_vCurrentVelocity, FIELD_VECTOR),
DEFINE_FIELD( m_vCurrentAngularVelocity, FIELD_VECTOR ),
DEFINE_FIELD( m_vCurrentBanking, FIELD_VECTOR),
DEFINE_FIELD( m_vNoiseMod, FIELD_VECTOR),
DEFINE_FIELD( m_fHeadYaw, FIELD_FLOAT),
DEFINE_FIELD( m_vLastPatrolDir, FIELD_VECTOR),
END_DATADESC()
//------------------------------------------------------------------------------
// Purpose : Override to return correct velocity
// Input :
// Output :
//------------------------------------------------------------------------------
void CAI_BaseFlyingBot::GetVelocity(Vector *vVelocity, AngularImpulse *vAngVelocity)
{
if (vVelocity != NULL)
{
VectorCopy(m_vCurrentVelocity,*vVelocity);
}
if (vAngVelocity != NULL)
{
QAngle tmp = GetLocalAngularVelocity();
QAngleToAngularImpulse( tmp, *vAngVelocity );
}
}
//-----------------------------------------------------------------------------
// Purpose: Turn head yaw into facing direction
// Input :
// Output :
//-----------------------------------------------------------------------------
QAngle CAI_BaseFlyingBot::BodyAngles()
{
return QAngle(0,m_fHeadYaw,0);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input :
// Output :
//-----------------------------------------------------------------------------
void CAI_BaseFlyingBot::TurnHeadToTarget(float flInterval, const Vector &MoveTarget )
{
float flDestYaw = VecToYaw( MoveTarget - GetLocalOrigin() );
float newYaw = AI_ClampYaw( GetHeadTurnRate() * 10.0f, m_fHeadYaw, flDestYaw, gpGlobals->curtime - GetLastThink() );
if ( newYaw != m_fHeadYaw )
{
m_fHeadYaw = newYaw;
}
// Set us to face that way
SetBoneController( 0, m_fHeadYaw );
}
//------------------------------------------------------------------------------
// Purpose :
// Input :
// Output :
//------------------------------------------------------------------------------
float CAI_BaseFlyingBot::MinGroundDist(void)
{
return 0;
}
//------------------------------------------------------------------------------
// Purpose :
// Input :
// Output :
//------------------------------------------------------------------------------
Vector CAI_BaseFlyingBot::VelocityToAvoidObstacles(float flInterval)
{
// --------------------------------
// Avoid banging into stuff
// --------------------------------
trace_t tr;
Vector vTravelDir = m_vCurrentVelocity*flInterval;
Vector endPos = GetAbsOrigin() + vTravelDir;
AI_TraceEntity( this, GetAbsOrigin(), endPos, MASK_NPCSOLID|CONTENTS_WATER, &tr );
if (tr.fraction != 1.0)
{
// Bounce off in normal
Vector vBounce = tr.plane.normal * 0.5 * m_vCurrentVelocity.Length();
return (vBounce);
}
// --------------------------------
// Try to remain above the ground.
// --------------------------------
float flMinGroundDist = MinGroundDist();
AI_TraceLine(GetAbsOrigin(), GetAbsOrigin() + Vector(0, 0, -flMinGroundDist),
MASK_NPCSOLID_BRUSHONLY|CONTENTS_WATER, this, COLLISION_GROUP_NONE, &tr);
if (tr.fraction < 1)
{
// Clamp veloctiy
if (tr.fraction < 0.1)
{
tr.fraction = 0.1;
}
return Vector(0, 0, 50/tr.fraction);
}
return vec3_origin;
}
//------------------------------------------------------------------------------
// Purpose :
// Input :
// Output :
//------------------------------------------------------------------------------
void CAI_BaseFlyingBot::StartTask( const Task_t *pTask )
{
switch (pTask->iTask)
{
// Skip as done via bone controller
case TASK_FACE_ENEMY:
{
TaskComplete();
break;
}
// Activity is just idle (have no run)
case TASK_RUN_PATH:
{
GetNavigator()->SetMovementActivity(ACT_IDLE);
TaskComplete();
break;
}
// Don't check for run/walk activity
case TASK_SCRIPT_RUN_TO_TARGET:
case TASK_SCRIPT_WALK_TO_TARGET:
{
if (GetTarget() == NULL)
{
TaskFail(FAIL_NO_TARGET);
}
else
{
if (!GetNavigator()->SetGoal( GOALTYPE_TARGETENT ) )
{
TaskFail(FAIL_NO_ROUTE);
GetNavigator()->ClearGoal();
}
}
TaskComplete();
break;
}
// Override to get more to get a directional path
case TASK_GET_PATH_TO_RANDOM_NODE:
{
if ( GetNavigator()->SetRandomGoal( pTask->flTaskData, m_vLastPatrolDir ) )
TaskComplete();
else
TaskFail(FAIL_NO_REACHABLE_NODE);
break;
}
default:
{
BaseClass::StartTask(pTask);
}
}
}
//------------------------------------------------------------------------------
void CAI_BaseFlyingBot::MoveToTarget(float flInterval, const Vector &MoveTarget)
{
Assert(0); // This must be overridden in the leaf classes
}
//------------------------------------------------------------------------------
AI_NavPathProgress_t CAI_BaseFlyingBot::ProgressFlyPath(
float flInterval,
const CBaseEntity *pNewTarget,
unsigned collisionMask,
bool bNewTrySimplify,
float strictPointTolerance)
{
AI_ProgressFlyPathParams_t params( collisionMask, strictPointTolerance );
params.SetCurrent( pNewTarget, bNewTrySimplify );
AI_NavPathProgress_t progress = GetNavigator()->ProgressFlyPath( params );
switch ( progress )
{
case AINPP_NO_CHANGE:
case AINPP_ADVANCED:
{
MoveToTarget(flInterval, GetNavigator()->GetCurWaypointPos());
break;
}
case AINPP_COMPLETE:
{
TaskMovementComplete();
break;
}
case AINPP_BLOCKED: // function is not supposed to test blocking, just simple path progression
default:
{
AssertMsg( 0, ( "Unexpected result" ) );
break;
}
}
return progress;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *pTarget -
// &chasePosition -
//-----------------------------------------------------------------------------
void CAI_BaseFlyingBot::TranslateNavGoal( CBaseEntity *pTarget, Vector &chasePosition )
{
Assert( pTarget != NULL );
if ( pTarget == NULL )
{
chasePosition = vec3_origin;
return;
}
// Chase their eyes
chasePosition = pTarget->GetAbsOrigin() + pTarget->GetViewOffset();
}
//------------------------------------------------------------------------------
// Purpose :
// Input :
// Output :
//------------------------------------------------------------------------------
CAI_BaseFlyingBot::CAI_BaseFlyingBot()
{
#ifdef _DEBUG
m_vCurrentVelocity.Init();
m_vCurrentBanking.Init();
m_vLastPatrolDir.Init();
#endif
}