Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
5.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: NPC that drives vehicles
  4. //
  5. //=============================================================================//
  6. #ifndef NPC_VEHICLEDRIVER_H
  7. #define NPC_VEHICLEDRIVER_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "ai_basenpc.h"
  12. class CPropVehicleDriveable;
  13. //------------------------------------
  14. // Spawnflags
  15. //------------------------------------
  16. #define SF_VEHICLEDRIVER_INACTIVE (1 << 16)
  17. //=========================================================
  18. // Custom schedules
  19. //=========================================================
  20. enum
  21. {
  22. SCHED_VEHICLEDRIVER_INACTIVE = LAST_SHARED_SCHEDULE,
  23. SCHED_VEHICLEDRIVER_COMBAT_WAIT,
  24. SCHED_VEHICLEDRIVER_DRIVE_PATH,
  25. LAST_VEHICLEDRIVER_SCHED,
  26. };
  27. //=========================================================
  28. // Custom tasks
  29. //=========================================================
  30. enum
  31. {
  32. TASK_VEHICLEDRIVER_GET_PATH = LAST_SHARED_TASK,
  33. LAST_VEHICLEDRIVER_TASK,
  34. };
  35. //-----------------------------------------------------------------------------
  36. // Purpose:
  37. //-----------------------------------------------------------------------------
  38. class CVehicleWaypoint
  39. {
  40. public:
  41. CVehicleWaypoint( Vector &pPrevPoint, Vector &pCurPoint, Vector &pNextPoint, Vector &pNextNextPoint )
  42. {
  43. splinePoints[0] = pPrevPoint;
  44. splinePoints[1] = pCurPoint;
  45. splinePoints[2] = pNextPoint;
  46. splinePoints[3] = pNextNextPoint;
  47. RecalculateSpline();
  48. }
  49. void RecalculateSpline( void )
  50. {
  51. planeWaypoint.normal = (splinePoints[2] - splinePoints[1]);
  52. VectorNormalize( planeWaypoint.normal );
  53. planeWaypoint.type = PLANE_ANYZ;
  54. planeWaypoint.dist = DotProduct( planeWaypoint.normal, splinePoints[2] );
  55. planeWaypoint.signbits = SignbitsForPlane(&planeWaypoint);
  56. // TODO: Use the vehicle's absbox
  57. iInitialPlaneSide = BoxOnPlaneSide( -Vector(32,32,32), Vector(32,32,32), &planeWaypoint );
  58. // Hackily calculate a length for the spline. Subdivide & measure.
  59. flSplineLength = 0;
  60. Vector vecPrev = splinePoints[1];
  61. const int iDivs = 10;
  62. for ( int i = 1; i <= iDivs; i++ )
  63. {
  64. Vector vecCurr;
  65. float flT = (float)i / (float)iDivs;
  66. Catmull_Rom_Spline( splinePoints[0], splinePoints[1], splinePoints[2], splinePoints[3], flT, vecCurr );
  67. flSplineLength += (vecCurr - vecPrev).Length();
  68. vecPrev = vecCurr;
  69. }
  70. }
  71. Vector GetPointAt( float flT )
  72. {
  73. Vector vecCurr(0,0,0);
  74. Catmull_Rom_Spline( splinePoints[0], splinePoints[1], splinePoints[2], splinePoints[3], flT, vecCurr );
  75. return vecCurr;
  76. }
  77. Vector GetTangentAt( float flT )
  78. {
  79. Vector vecCurr(0,0,0);
  80. Catmull_Rom_Spline_Tangent( splinePoints[0], splinePoints[1], splinePoints[2], splinePoints[3], flT, vecCurr );
  81. return vecCurr;
  82. }
  83. float GetLength( void )
  84. {
  85. return flSplineLength;
  86. }
  87. public:
  88. int iInitialPlaneSide;
  89. float flSplineLength;
  90. Vector splinePoints[4];
  91. cplane_t planeWaypoint;
  92. };
  93. //-----------------------------------------------------------------------------
  94. // Purpose:
  95. //-----------------------------------------------------------------------------
  96. class CNPC_VehicleDriver : public CAI_BaseNPC
  97. {
  98. DECLARE_CLASS( CNPC_VehicleDriver, CAI_BaseNPC );
  99. public:
  100. DECLARE_DATADESC();
  101. DEFINE_CUSTOM_AI;
  102. CNPC_VehicleDriver( void );
  103. ~CNPC_VehicleDriver( void );
  104. virtual void Spawn( void );
  105. virtual void Precache( void );
  106. virtual void Activate( void );
  107. virtual void OnRestore();
  108. virtual void UpdateOnRemove( void );
  109. // AI
  110. void UpdateEfficiency( bool bInPVS ) { SetEfficiency( ( GetSleepState() != AISS_AWAKE ) ? AIE_DORMANT : AIE_NORMAL ); SetMoveEfficiency( AIME_NORMAL ); }
  111. virtual void PrescheduleThink( void );
  112. virtual int TranslateSchedule( int scheduleType );
  113. virtual int SelectSchedule( void );
  114. virtual void StartTask( const Task_t *pTask );
  115. virtual void RunTask( const Task_t *pTask );
  116. virtual void GatherEnemyConditions( CBaseEntity *pEnemy );
  117. virtual int RangeAttack1Conditions( float flDot, float flDist );
  118. virtual int RangeAttack2Conditions( float flDot, float flDist );
  119. // Driving
  120. virtual void DriveVehicle( void );
  121. virtual bool OverrideMove( float flInterval );
  122. bool OverridePathMove( float flInterval );
  123. void CalculatePostPoints( void );
  124. bool WaypointReached( void );
  125. float GetDefaultNavGoalTolerance();
  126. void RecalculateSpeeds( void );
  127. void ClearWaypoints( void );
  128. void CheckForTeleport( void );
  129. int BloodColor( void ) { return DONT_BLEED; }
  130. #ifdef HL2_DLL
  131. Class_T Classify( void ) { return CLASS_METROPOLICE; }
  132. #else
  133. Class_T Classify( void ) { return CLASS_NONE; }
  134. #endif
  135. Disposition_t IRelationType( CBaseEntity *pTarget );
  136. // Inputs
  137. void InputSetDriversMaxSpeed( inputdata_t &inputdata );
  138. void InputSetDriversMinSpeed( inputdata_t &inputdata );
  139. void InputStartForward( inputdata_t &inputdata );
  140. void InputStop( inputdata_t &inputdata );
  141. void InputStartFiring( inputdata_t &inputdata );
  142. void InputStopFiring( inputdata_t &inputdata );
  143. void InputGotoPathCorner( inputdata_t &inputdata );
  144. public:
  145. string_t m_iszVehicleName;
  146. IServerVehicle *m_pVehicleInterface;
  147. EHANDLE m_hVehicleEntity;
  148. // Path driving
  149. CVehicleWaypoint *m_Waypoints[2];
  150. CVehicleWaypoint *m_pCurrentWaypoint;
  151. CVehicleWaypoint *m_pNextWaypoint;
  152. Vector m_vecDesiredVelocity;
  153. Vector m_vecDesiredPosition;
  154. Vector m_vecPrevPoint;
  155. Vector m_vecPrevPrevPoint;
  156. Vector m_vecPostPoint;
  157. Vector m_vecPostPostPoint;
  158. float m_flDistanceAlongSpline;
  159. float m_flDriversMaxSpeed;
  160. float m_flDriversMinSpeed;
  161. // Speed
  162. float m_flMaxSpeed; // Maximum speed this driver will go
  163. float m_flGoalSpeed; // Desired speed
  164. float m_flInitialSpeed;
  165. float m_flSteering;
  166. };
  167. #endif // NPC_VEHICLEDRIVER_H