car.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. //! Car's sound controller list. (native, do not change or extend)
  2. enum CarSoundCtrl
  3. {
  4. // simulation
  5. ENGINE, //!< indicates if engine is ON
  6. RPM, //!< engine's RPM
  7. SPEED, //!< speed of the car in km/h
  8. // miscellaneous
  9. DOORS, //!< indicates if doors are open
  10. PLAYER //!< indicates if driver is controlled by player
  11. };
  12. //! Type of vehicle's fluid. (native, do not change or extend)
  13. enum CarFluid
  14. {
  15. FUEL,
  16. OIL,
  17. BRAKE,
  18. COOLANT,
  19. USER1, //!< reserved for user / modding support
  20. USER2, //!< reserved for user / modding support
  21. USER3, //!< reserved for user / modding support
  22. USER4 //!< reserved for user / modding support
  23. };
  24. //! Enumerated gearbox types. (native, do not change or extend)
  25. enum CarGearboxType
  26. {
  27. MANUAL, //!< classic manual transmission with friction plates between engine and gearbox
  28. AUTOMATIC //!< automatic transmission with torque converter between engine and gearbox
  29. }
  30. //! Enumerated vehicle's gears. (native, do not change or extend)
  31. enum CarGear
  32. {
  33. REVERSE,
  34. NEUTRAL,
  35. FIRST,
  36. SECOND,
  37. THIRD,
  38. FOURTH,
  39. FIFTH,
  40. SIXTH,
  41. SEVENTH,
  42. EIGTH,
  43. NINTH,
  44. TENTH,
  45. ELEVENTH,
  46. TWELFTH,
  47. THIRTEENTH,
  48. FOURTEENTH,
  49. FIFTEENTH,
  50. SIXTEENTH
  51. };
  52. //! Enumerated automatic gearbox modes. (native, do not change or extend)
  53. enum CarAutomaticGearboxMode
  54. {
  55. P, //!< park
  56. R, //!< reverse
  57. N, //!< neutral
  58. D //!< drive
  59. };
  60. //! Enumerated car wheel water state. (native, do not change or extend)
  61. enum CarWheelWaterState
  62. {
  63. ON_LAND, //!< if the wheel is on or above land
  64. IN_WATER, //!< if the wheel is partially within some water plane
  65. UNDER_WATER //!< if the wheel is under a water plane
  66. };
  67. //! Base native class for all motorized wheeled vehicles.
  68. class Car extends Transport
  69. {
  70. //! DEPRECATED, left for backwards compatibility, the methods of this class are now directly accessible on Car itself
  71. proto native CarController GetController();
  72. //! Returns the current speed of the vehicle in km/h.
  73. proto native float GetSpeedometer();
  74. //! Returns the current speed of the vehicle in km/h. Value is absolute
  75. float GetSpeedometerAbsolute()
  76. {
  77. return Math.AbsFloat(GetSpeedometer());
  78. }
  79. override bool IsAreaAtDoorFree( int currentSeat, float maxAllowedObjHeight = 0.5, float horizontalExtents = 0.5, float playerHeight = 1.7 )
  80. {
  81. vector transform[4];
  82. vector extents;
  83. extents[0] = horizontalExtents;
  84. extents[1] = playerHeight;
  85. extents[2] = horizontalExtents;
  86. float speed = GetSpeedometerAbsolute();
  87. if (speed > 8)
  88. extents[2] = extents[2] * 6;
  89. if (speed > 8)
  90. extents[0] = 2;
  91. return IsAreaAtDoorFree( currentSeat, maxAllowedObjHeight, extents, transform );
  92. }
  93. override Shape DebugFreeAreaAtDoor( int currentSeat, float maxAllowedObjHeight = 0.5, float horizontalExtents = 0.5, float playerHeight = 1.7 )
  94. {
  95. int color = ARGB(20, 0, 255, 0);
  96. vector transform[4];
  97. vector extents;
  98. extents[0] = horizontalExtents;
  99. extents[1] = playerHeight;
  100. extents[2] = horizontalExtents;
  101. float speed = GetSpeedometerAbsolute();
  102. if (speed > 8)
  103. extents[2] = extents[2] * 6;
  104. if (speed > 8)
  105. extents[0] = 2;
  106. if (!IsAreaAtDoorFree( currentSeat, maxAllowedObjHeight, extents, transform ))
  107. {
  108. color = ARGB(20, 255, 0, 0);
  109. }
  110. Shape shape = Debug.DrawBox(-extents * 0.5, extents * 0.5, color);
  111. shape.SetMatrix(transform);
  112. return shape;
  113. }
  114. //-----------------------------------------------------------------------------
  115. // controls
  116. //! Returns the current steering value in range <-1, 1>.
  117. proto native float GetSteering();
  118. /*!
  119. Sets the steering value.
  120. \param in should be in range <-1, 1>
  121. \param analog indicates if the input value was taken from analog controller
  122. */
  123. proto native void SetSteering( float in, bool analog = false );
  124. //! Returns the current thrust turbo modifier value in range <0, 1>.
  125. proto native float GetThrustTurbo();
  126. //! Returns the current thrust gentle modifier value in range <0, 1>.
  127. proto native float GetThrustGentle();
  128. //! Returns the current thrust value in range <0, 1>.
  129. proto native float GetThrust();
  130. /*!
  131. Sets the thrust value.
  132. \param in should be in range <0, 1>
  133. \param gentle should be in range <0, 1>, thrust modifier
  134. \param turbo should be in range <0, 1>, thrust modifier
  135. */
  136. proto native void SetThrust( float in, float gentle = 0, float turbo = 0 );
  137. //! Returns the current brake value in range <0, 1>.
  138. proto native float GetBrake();
  139. /*!
  140. Sets the brake value.
  141. \param in should be in range <0, 1>
  142. \param panic should be in range <0, 1>
  143. */
  144. proto native void SetBrake( float in, float panic = 0, bool gentle = false );
  145. //! Returns the current handbrake value in range <0, 1>.
  146. proto native float GetHandbrake();
  147. /*!
  148. Sets the handbrake value.
  149. \param in should be in range <0, 1>
  150. */
  151. proto native void SetHandbrake( float in );
  152. /*!
  153. Sets if brakes should activate without a driver present
  154. */
  155. proto native void SetBrakesActivateWithoutDriver( bool activate = true );
  156. //! Returns the current clutch value in range <0, 1>.
  157. proto native float GetClutch();
  158. /*!
  159. Sets the clutch state.
  160. */
  161. proto native void SetClutchState( bool in );
  162. //! Returns index of the current gear.
  163. proto native int GetGear();
  164. proto native void ShiftUp();
  165. proto native void ShiftTo( CarGear gear );
  166. proto native void ShiftDown();
  167. //-----------------------------------------------------------------------------
  168. //-----------------------------------------------------------------------------
  169. // fluids
  170. /*!
  171. Returns tank capacity for the specified vehicle's fluid.
  172. \param fluid the specified fluid type
  173. */
  174. proto native float GetFluidCapacity( CarFluid fluid );
  175. /*!
  176. Returns fraction value (in range <0, 1>)
  177. of the current state of the specified vehicle's fluid.
  178. \param[in] fluid the specified fluid type
  179. */
  180. proto native float GetFluidFraction( CarFluid fluid );
  181. //! Removes from the specified fluid the specified amount.
  182. proto native void Leak( CarFluid fluid, float amount );
  183. //! Removes all the specified fluid from vehicle.
  184. proto native void LeakAll( CarFluid fluid );
  185. //! Adds to the specified fluid the specified amount.
  186. proto native void Fill( CarFluid fluid, float amount );
  187. /*!
  188. Is called every time when the specified vehicle's fluid level
  189. changes eg. when vehicle is consuming fuel.
  190. \param[in] fluid fluid identifier, \see CarFluid
  191. \param[in] newValue new fluid level
  192. \param[in] oldValue previous fluid level before change
  193. */
  194. void OnFluidChanged( CarFluid fluid, float newValue, float oldValue ) {}
  195. //-----------------------------------------------------------------------------
  196. //-----------------------------------------------------------------------------
  197. // engine
  198. //! Returns engine's min operating rpm
  199. proto native float EngineGetRPMMin();
  200. //! Returns engine's idle rpm before engine stalls.
  201. proto native float EngineGetRPMIdle();
  202. //! Returns engine's max rpm before engine blows up.
  203. proto native float EngineGetRPMMax();
  204. //! Returns engine's maximal working rpm without damaging the engine.
  205. proto native float EngineGetRPMRedline();
  206. //! Returns engine's rpm value.
  207. proto native float EngineGetRPM();
  208. //! Returns true when engine is running, false otherwise.
  209. proto native bool EngineIsOn();
  210. //! Starts the engine.
  211. proto native void EngineStart();
  212. /*!
  213. Is called every time the game wants to start the engine.
  214. \return true if the engine can start, false otherwise.
  215. */
  216. bool OnBeforeEngineStart()
  217. {
  218. // engine can start by default
  219. return true;
  220. }
  221. //! Is called every time the engine starts.
  222. void OnEngineStart() {}
  223. //! Stops the engine.
  224. proto native void EngineStop();
  225. //! Is called every time the engine stops.
  226. void OnEngineStop() {}
  227. //! Get actual position of engine (model space)
  228. proto native vector GetEnginePos();
  229. //! Override the position of engine (model space)
  230. proto native void SetEnginePos(vector pos);
  231. //-----------------------------------------------------------------------------
  232. //-----------------------------------------------------------------------------
  233. // gearbox
  234. //! Returns total number of gears.
  235. proto native int GetGearsCount();
  236. //! Returns gearbox type. See CarGearboxType enum for more info.
  237. proto native CarGearboxType GearboxGetType();
  238. //! Returns gearbox mode. This is useful when car has automatic gearbox.
  239. proto native CarAutomaticGearboxMode GearboxGetMode();
  240. /*!
  241. Is called every time when the simulation changed gear.
  242. \param[in] newGear new gear level
  243. \param[in] oldGear previous gear level before gear shift
  244. */
  245. void OnGearChanged( int newGear, int oldGear )
  246. {
  247. }
  248. //-----------------------------------------------------------------------------
  249. //-----------------------------------------------------------------------------
  250. // wheels
  251. //! Returns true if any of the wheels are locked in terms of its movement.
  252. proto native bool WheelIsAnyLocked();
  253. /*!
  254. Returns the raw angular velocity of the wheel, unstable value
  255. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  256. */
  257. proto native float WheelGetAngularVelocity( int wheelIdx );
  258. /*!
  259. Returns true if given wheel is making any contact
  260. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  261. */
  262. proto native bool WheelHasContact( int wheelIdx );
  263. /*!
  264. Returns the position of contact in world space, only valid if there was an actual contact
  265. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  266. */
  267. proto native vector WheelGetContactPosition( int wheelIdx );
  268. /*!
  269. Returns the normal of contact in world space, only valid if there was an actual contact
  270. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  271. */
  272. proto native vector WheelGetContactNormal( int wheelIdx );
  273. /*!
  274. Returns the direction pointing forwards that the wheel is facing
  275. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  276. */
  277. proto native vector WheelGetDirection( int wheelIdx );
  278. /*!
  279. Returns the surface that the wheel is nearby
  280. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  281. */
  282. proto native SurfaceInfo WheelGetSurface( int wheelIdx );
  283. /*!
  284. Returns the state that the wheel is in with water
  285. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  286. */
  287. proto native CarWheelWaterState WheelGetWaterState( int wheelIdx );
  288. /*!
  289. Returns the entity attached that represents the wheel
  290. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  291. */
  292. proto native EntityAI WheelGetEntity( int wheelIdx );
  293. /*!
  294. Returns true if given wheel is locked in terms of its movement.
  295. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  296. */
  297. proto native bool WheelIsLocked( int wheelIdx );
  298. //! How many wheel can be attached to a car (hubs only)
  299. proto native int WheelCount();
  300. //! Number of actually attached wheels (hubs only)
  301. proto native int WheelCountPresent();
  302. //-----------------------------------------------------------------------------
  303. //-----------------------------------------------------------------------------
  304. // events
  305. /*!
  306. Is called every time when vehicle collides with other object.
  307. \param[in] zoneName configured vehicle's zone that was hit
  308. \param[in] localPos position where the vehicle was hit in vehicle's space
  309. \param[in] other object with which the vehicle is colliding
  310. \param[in] data contact properties
  311. */
  312. void OnContact( string zoneName, vector localPos, IEntity other, Contact data ) {}
  313. /*!
  314. Is called every sound simulation step.
  315. In this callback, user can modify behaviour of sound controllers.
  316. \param[in] ctrl sound controller identifier, \see CarSoundCtrl
  317. \param[in] oldValue already computed value by the game code
  318. \return new value of the specified sound controller.
  319. */
  320. float OnSound( CarSoundCtrl ctrl, float oldValue )
  321. {
  322. // just use the computed value by the game code
  323. return oldValue;
  324. }
  325. /*!
  326. Is called after every input simulation step.
  327. Note that the player character and other systems can always change the internal state.
  328. It is highly recommended to store state of custom inputs elsewhere and call Setters here.
  329. \param[in] dt frame time in seconds
  330. */
  331. void OnInput( float dt ) {}
  332. /*!
  333. Is called every game frame.
  334. \param[in] dt frame time in seconds
  335. */
  336. void OnUpdate( float dt ) {}
  337. //-----------------------------------------------------------------------------
  338. // implemented only in internal configuration
  339. proto native void ForcePosition( vector pos );
  340. // implemented only in internal configuration
  341. proto native void ForceDirection( vector dir );
  342. };
  343. //! DEPRECATED class left for backwards compatibility, methods are available on car itself now
  344. class CarController
  345. {
  346. private void CarController() {}
  347. private void ~CarController() {}
  348. //! Returns the current steering value in range <-1, 1>.
  349. proto float GetSteering();
  350. /*!
  351. Sets the steering value.
  352. \param in should be in range <-1, 1>
  353. \param analog indicates if the input value was taken from analog controller
  354. */
  355. proto void SetSteering( float in, bool analog = false );
  356. //! Returns the current thrust turbo modifier value in range <0, 1>.
  357. proto float GetThrustTurbo();
  358. //! Returns the current thrust gentle modifier value in range <0, 1>.
  359. proto float GetThrustGentle();
  360. //! Returns the current thrust value in range <0, 1>.
  361. proto float GetThrust();
  362. /*!
  363. Sets the thrust value.
  364. \param in should be in range <0, 1>
  365. \param gentle should be in range <0, 1>, thrust modifier
  366. \param turbo should be in range <0, 1>, thrust modifier
  367. */
  368. proto void SetThrust( float in, float gentle = 0, float turbo = 0 );
  369. //! Returns the current brake value in range <0, 1>.
  370. proto float GetBrake();
  371. /*!
  372. Sets the brake value.
  373. \param in should be in range <0, 1>
  374. \param panic should be in range <0, 1>
  375. */
  376. proto void SetBrake( float in, float panic = 0 );
  377. //! Returns index of the current gear.
  378. proto int GetGear();
  379. proto void ShiftUp();
  380. proto void ShiftTo( CarGear gear );
  381. proto void ShiftDown();
  382. };