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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: NPC that drives vehicles
//
//=============================================================================//
#ifndef NPC_VEHICLEDRIVER_H
#define NPC_VEHICLEDRIVER_H
#ifdef _WIN32
#pragma once
#endif
#include "ai_basenpc.h"
class CPropVehicleDriveable;
//------------------------------------
// Spawnflags
//------------------------------------
#define SF_VEHICLEDRIVER_INACTIVE (1 << 16)
//=========================================================
// Custom schedules
//=========================================================
enum
{
SCHED_VEHICLEDRIVER_INACTIVE = LAST_SHARED_SCHEDULE,
SCHED_VEHICLEDRIVER_COMBAT_WAIT,
SCHED_VEHICLEDRIVER_DRIVE_PATH,
LAST_VEHICLEDRIVER_SCHED,
};
//=========================================================
// Custom tasks
//=========================================================
enum
{
TASK_VEHICLEDRIVER_GET_PATH = LAST_SHARED_TASK,
LAST_VEHICLEDRIVER_TASK,
};
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
class CVehicleWaypoint
{
public:
CVehicleWaypoint( Vector &pPrevPoint, Vector &pCurPoint, Vector &pNextPoint, Vector &pNextNextPoint )
{
splinePoints[0] = pPrevPoint;
splinePoints[1] = pCurPoint;
splinePoints[2] = pNextPoint;
splinePoints[3] = pNextNextPoint;
RecalculateSpline();
}
void RecalculateSpline( void )
{
planeWaypoint.normal = (splinePoints[2] - splinePoints[1]);
VectorNormalize( planeWaypoint.normal );
planeWaypoint.type = PLANE_ANYZ;
planeWaypoint.dist = DotProduct( planeWaypoint.normal, splinePoints[2] );
planeWaypoint.signbits = SignbitsForPlane(&planeWaypoint);
// TODO: Use the vehicle's absbox
iInitialPlaneSide = BoxOnPlaneSide( -Vector(32,32,32), Vector(32,32,32), &planeWaypoint );
// Hackily calculate a length for the spline. Subdivide & measure.
flSplineLength = 0;
Vector vecPrev = splinePoints[1];
const int iDivs = 10;
for ( int i = 1; i <= iDivs; i++ )
{
Vector vecCurr;
float flT = (float)i / (float)iDivs;
Catmull_Rom_Spline( splinePoints[0], splinePoints[1], splinePoints[2], splinePoints[3], flT, vecCurr );
flSplineLength += (vecCurr - vecPrev).Length();
vecPrev = vecCurr;
}
}
Vector GetPointAt( float flT )
{
Vector vecCurr(0,0,0);
Catmull_Rom_Spline( splinePoints[0], splinePoints[1], splinePoints[2], splinePoints[3], flT, vecCurr );
return vecCurr;
}
Vector GetTangentAt( float flT )
{
Vector vecCurr(0,0,0);
Catmull_Rom_Spline_Tangent( splinePoints[0], splinePoints[1], splinePoints[2], splinePoints[3], flT, vecCurr );
return vecCurr;
}
float GetLength( void )
{
return flSplineLength;
}
public:
int iInitialPlaneSide;
float flSplineLength;
Vector splinePoints[4];
cplane_t planeWaypoint;
};
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
class CNPC_VehicleDriver : public CAI_BaseNPC
{
DECLARE_CLASS( CNPC_VehicleDriver, CAI_BaseNPC );
public:
DECLARE_DATADESC();
DEFINE_CUSTOM_AI;
CNPC_VehicleDriver( void );
~CNPC_VehicleDriver( void );
virtual void Spawn( void );
virtual void Precache( void );
virtual void Activate( void );
virtual void OnRestore();
virtual void UpdateOnRemove( void );
// AI
void UpdateEfficiency( bool bInPVS ) { SetEfficiency( ( GetSleepState() != AISS_AWAKE ) ? AIE_DORMANT : AIE_NORMAL ); SetMoveEfficiency( AIME_NORMAL ); }
virtual void PrescheduleThink( void );
virtual int TranslateSchedule( int scheduleType );
virtual int SelectSchedule( void );
virtual void StartTask( const Task_t *pTask );
virtual void RunTask( const Task_t *pTask );
virtual void GatherEnemyConditions( CBaseEntity *pEnemy );
virtual int RangeAttack1Conditions( float flDot, float flDist );
virtual int RangeAttack2Conditions( float flDot, float flDist );
// Driving
virtual void DriveVehicle( void );
virtual bool OverrideMove( float flInterval );
bool OverridePathMove( float flInterval );
void CalculatePostPoints( void );
bool WaypointReached( void );
float GetDefaultNavGoalTolerance();
void RecalculateSpeeds( void );
void ClearWaypoints( void );
void CheckForTeleport( void );
int BloodColor( void ) { return DONT_BLEED; }
#ifdef HL2_DLL
Class_T Classify( void ) { return CLASS_METROPOLICE; }
#else
Class_T Classify( void ) { return CLASS_NONE; }
#endif
Disposition_t IRelationType( CBaseEntity *pTarget );
// Inputs
void InputSetDriversMaxSpeed( inputdata_t &inputdata );
void InputSetDriversMinSpeed( inputdata_t &inputdata );
void InputStartForward( inputdata_t &inputdata );
void InputStop( inputdata_t &inputdata );
void InputStartFiring( inputdata_t &inputdata );
void InputStopFiring( inputdata_t &inputdata );
void InputGotoPathCorner( inputdata_t &inputdata );
public:
string_t m_iszVehicleName;
IServerVehicle *m_pVehicleInterface;
EHANDLE m_hVehicleEntity;
// Path driving
CVehicleWaypoint *m_Waypoints[2];
CVehicleWaypoint *m_pCurrentWaypoint;
CVehicleWaypoint *m_pNextWaypoint;
Vector m_vecDesiredVelocity;
Vector m_vecDesiredPosition;
Vector m_vecPrevPoint;
Vector m_vecPrevPrevPoint;
Vector m_vecPostPoint;
Vector m_vecPostPostPoint;
float m_flDistanceAlongSpline;
float m_flDriversMaxSpeed;
float m_flDriversMinSpeed;
// Speed
float m_flMaxSpeed; // Maximum speed this driver will go
float m_flGoalSpeed; // Desired speed
float m_flInitialSpeed;
float m_flSteering;
};
#endif // NPC_VEHICLEDRIVER_H
|