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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Base class for many flying NPCs
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "ai_basenpc_physicsflyer.h"
#include "ai_route.h"
#include "ai_navigator.h"
#include "ai_motor.h"
#include "physics_saverestore.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
BEGIN_DATADESC( CAI_BasePhysicsFlyingBot )
DEFINE_FIELD( m_vCurrentVelocity, 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),
DEFINE_PHYSPTR( m_pMotionController ),
END_DATADESC()
//------------------------------------------------------------------------------
// Purpose : Override to return correct velocity
// Input :
// Output :
//------------------------------------------------------------------------------
void CAI_BasePhysicsFlyingBot::GetVelocity(Vector *vVelocity, AngularImpulse *vAngVelocity)
{
Assert( GetMoveType() == MOVETYPE_VPHYSICS );
if ( VPhysicsGetObject() )
{
VPhysicsGetObject()->GetVelocity( vVelocity, vAngVelocity );
}
else
{
if ( vVelocity )
{
vVelocity->Init();
}
if ( vAngVelocity )
{
vAngVelocity->Init();
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Turn head yaw into facing direction
// Input :
// Output :
//-----------------------------------------------------------------------------
QAngle CAI_BasePhysicsFlyingBot::BodyAngles()
{
return QAngle(0,m_fHeadYaw,0);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input :
// Output :
//-----------------------------------------------------------------------------
void CAI_BasePhysicsFlyingBot::TurnHeadToTarget(float flInterval, const Vector &MoveTarget )
{
float desYaw = UTIL_AngleDiff(VecToYaw(MoveTarget - GetLocalOrigin()), 0 );
m_fHeadYaw = desYaw;
return;
// If I've flipped completely around, reverse angles
float fYawDiff = m_fHeadYaw - desYaw;
if (fYawDiff > 180)
{
m_fHeadYaw -= 360;
}
else if (fYawDiff < -180)
{
m_fHeadYaw += 360;
}
// RIGHT NOW, this affects every flying bot. This rate should be member data that individuals
// can manipulate. THIS change for MANHACKS E3 2003 (sjb)
float iRate = 0.8;
// Make frame rate independent
float timeToUse = flInterval;
while (timeToUse > 0)
{
m_fHeadYaw = (iRate * m_fHeadYaw) + (1-iRate)*desYaw;
timeToUse -= 0.1;
}
while( m_fHeadYaw > 360 )
{
m_fHeadYaw -= 360.0f;
}
while( m_fHeadYaw < 0 )
{
m_fHeadYaw += 360.f;
}
// SetBoneController( 0, m_fHeadYaw );
}
//------------------------------------------------------------------------------
// Purpose :
// Input :
// Output :
//------------------------------------------------------------------------------
float CAI_BasePhysicsFlyingBot::MinGroundDist(void)
{
return 0;
}
//------------------------------------------------------------------------------
// Purpose :
// Input :
// Output :
//------------------------------------------------------------------------------
Vector CAI_BasePhysicsFlyingBot::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_BasePhysicsFlyingBot::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_BasePhysicsFlyingBot::MoveToTarget(float flInterval, const Vector &MoveTarget)
{
Assert(0); // This must be overridden in the leaf classes
}
//------------------------------------------------------------------------------
AI_NavPathProgress_t CAI_BasePhysicsFlyingBot::ProgressFlyPath(
float flInterval,
const CBaseEntity *pNewTarget,
unsigned collisionMask,
bool bNewTrySimplify,
float strictPointTolerance)
{
AI_ProgressFlyPathParams_t params( collisionMask );
params.strictPointTolerance = 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 :
// Output :
//------------------------------------------------------------------------------
CAI_BasePhysicsFlyingBot::CAI_BasePhysicsFlyingBot()
{
#ifdef _DEBUG
m_vCurrentVelocity.Init();
m_vCurrentBanking.Init();
m_vLastPatrolDir.Init();
#endif
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
CAI_BasePhysicsFlyingBot::~CAI_BasePhysicsFlyingBot( void )
{
physenv->DestroyMotionController( m_pMotionController );
}
//-----------------------------------------------------------------------------
// Purpose:
//
//
//-----------------------------------------------------------------------------
bool CAI_BasePhysicsFlyingBot::CreateVPhysics( void )
{
// Create the object in the physics system
IPhysicsObject *pPhysicsObject = VPhysicsInitNormal( SOLID_BBOX, FSOLID_NOT_STANDABLE, false );
m_pMotionController = physenv->CreateMotionController( this );
m_pMotionController->AttachObject( pPhysicsObject, true );
return true;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *pTarget -
// &chasePosition -
//-----------------------------------------------------------------------------
void CAI_BasePhysicsFlyingBot::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 : *pController -
// *pObject -
// deltaTime -
// &linear -
// &angular -
// Output : IMotionEvent::simresult_e
//-----------------------------------------------------------------------------
IMotionEvent::simresult_e CAI_BasePhysicsFlyingBot::Simulate( IPhysicsMotionController *pController, IPhysicsObject *pObject, float deltaTime, Vector &linear, AngularImpulse &angular )
{
static int count;
IPhysicsObject *pPhysicsObject = VPhysicsGetObject();
// Assert( pPhysicsObject );
if (!pPhysicsObject)
return SIM_NOTHING;
// move
Vector actualVelocity;
AngularImpulse actualAngularVelocity;
pPhysicsObject->GetVelocity( &actualVelocity, &actualAngularVelocity );
linear = (m_vCurrentVelocity - actualVelocity) * (0.1 / deltaTime) * 10.0;
/*
DevMsg("Sim %d : %5.1f %5.1f %5.1f\n", count++,
m_vCurrentVelocity.x - actualVelocity.x,
m_vCurrentVelocity.y - actualVelocity.y,
m_vCurrentVelocity.z - actualVelocity.z );
*/
// do angles.
Vector actualPosition;
QAngle actualAngles;
pPhysicsObject->GetPosition( &actualPosition, &actualAngles );
// FIXME: banking currently disabled, forces simple upright posture
angular.x = (UTIL_AngleDiff( m_vCurrentBanking.z, actualAngles.z ) - actualAngularVelocity.x) * (1 / deltaTime);
angular.y = (UTIL_AngleDiff( m_vCurrentBanking.x, actualAngles.x ) - actualAngularVelocity.y) * (1 / deltaTime);
// turn toward target
angular.z = UTIL_AngleDiff( m_fHeadYaw, actualAngles.y + actualAngularVelocity.z * 0.1 ) * (1 / deltaTime);
// angular = m_vCurrentAngularVelocity - actualAngularVelocity;
// DevMsg("Sim %d : %.1f %.1f %.1f (%.1f)\n", count++, actualAngles.x, actualAngles.y, actualAngles.z, m_fHeadYaw );
// FIXME: remove the stuff from MoveExecute();
// FIXME: check MOVE?
ClampMotorForces( linear, angular );
return SIM_GLOBAL_ACCELERATION; // on my local axis. SIM_GLOBAL_ACCELERATION
}
|