car.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. class CarOwnerState : TransportOwnerState
  68. {
  69. };
  70. class CarMove : TransportMove
  71. {
  72. };
  73. //! Native class for cars - handles physics simulation
  74. class Car extends Transport
  75. {
  76. //!
  77. protected override event typename GetOwnerStateType()
  78. {
  79. return CarOwnerState;
  80. }
  81. //!
  82. protected override event typename GetMoveType()
  83. {
  84. return CarMove;
  85. }
  86. //! Returns the current speed of the vehicle in km/h.
  87. proto native float GetSpeedometer();
  88. //! Returns the current speed of the vehicle in km/h. Value is absolute
  89. float GetSpeedometerAbsolute()
  90. {
  91. return Math.AbsFloat(GetSpeedometer());
  92. }
  93. override bool IsAreaAtDoorFree( int currentSeat, float maxAllowedObjHeight = 0.5, float horizontalExtents = 0.5, float playerHeight = 1.7 )
  94. {
  95. vector transform[4];
  96. vector extents;
  97. extents[0] = horizontalExtents;
  98. extents[1] = playerHeight;
  99. extents[2] = horizontalExtents;
  100. float speed = GetSpeedometerAbsolute();
  101. if (speed > 8)
  102. extents[2] = extents[2] * 6;
  103. if (speed > 8)
  104. extents[0] = 2;
  105. return IsAreaAtDoorFree( currentSeat, maxAllowedObjHeight, extents, transform );
  106. }
  107. override Shape DebugFreeAreaAtDoor( int currentSeat, float maxAllowedObjHeight = 0.5, float horizontalExtents = 0.5, float playerHeight = 1.7 )
  108. {
  109. int color = ARGB(20, 0, 255, 0);
  110. vector transform[4];
  111. vector extents;
  112. extents[0] = horizontalExtents;
  113. extents[1] = playerHeight;
  114. extents[2] = horizontalExtents;
  115. float speed = GetSpeedometerAbsolute();
  116. if (speed > 8)
  117. extents[2] = extents[2] * 6;
  118. if (speed > 8)
  119. extents[0] = 2;
  120. if (!IsAreaAtDoorFree( currentSeat, maxAllowedObjHeight, extents, transform ))
  121. {
  122. color = ARGB(20, 255, 0, 0);
  123. }
  124. Shape shape = Debug.DrawBox(-extents * 0.5, extents * 0.5, color);
  125. shape.SetMatrix(transform);
  126. return shape;
  127. }
  128. protected bool DetectFlippedUsingWheels(VehicleFlippedContext ctx, bool disallowSide)
  129. {
  130. if (disallowSide && (vector.Dot(GetDirectionUp(), vector.Up) < 0.7))
  131. {
  132. // return as "flipped", vehicle isn't pointing enough up to be reasonably certain
  133. return true;
  134. }
  135. int wheelCount = WheelCount();
  136. for (int wheelIdx = 0; wheelIdx < wheelCount; wheelIdx++)
  137. {
  138. if (!WheelHasContact(wheelIdx))
  139. {
  140. // wheel not in contact, then we could be flipped, we assume there exist other predicates
  141. return true;
  142. }
  143. }
  144. // all wheels in contact (or zero registered wheels), then we are in contact
  145. return false;
  146. }
  147. //! Returns the current steering value in range <-1, 1>.
  148. proto native float GetSteering();
  149. //! Sets the future steering value.
  150. proto native void SetSteering(float value, bool unused0 = false);
  151. //! Returns the actual throttle value in range <0, 1>.
  152. proto native float GetThrottle();
  153. //! Sets the future throttle value.
  154. proto native void SetThrottle(float value);
  155. //! Returns the value of how much the clutch is disengaged.
  156. proto native int GetClutch();
  157. //! Sets the future clutch value.
  158. proto native void SetClutch(float value);
  159. //! Returns the current brake value in range <0, 1>.
  160. proto native float GetBrake();
  161. //! Sets the future brake value
  162. proto native void SetBrake(float value, float unused0 = 0, bool unused1 = false);
  163. //! Returns the current handbrake value in range <0, 1>.
  164. proto native float GetHandbrake();
  165. //! Sets the future handbrake value
  166. proto native void SetHandbrake(float value);
  167. //! Sets if brakes should activate without a driver present
  168. proto native void SetBrakesActivateWithoutDriver(bool activate = true);
  169. //! Returns engine's min operating rpm
  170. proto native float EngineGetRPMMin();
  171. //! Returns engine's idle rpm before engine stalls.
  172. proto native float EngineGetRPMIdle();
  173. //! Returns engine's max rpm before engine blows up.
  174. proto native float EngineGetRPMMax();
  175. //! Returns engine's maximal working rpm without damaging the engine.
  176. proto native float EngineGetRPMRedline();
  177. //! Returns engine's rpm value.
  178. proto native float EngineGetRPM();
  179. //! Returns true when engine is running, false otherwise.
  180. proto native bool EngineIsOn();
  181. //! Starts the engine.
  182. proto native void EngineStart();
  183. //! Stops the engine.
  184. proto native void EngineStop();
  185. //! Get actual position of engine (model space)
  186. proto native vector GetEnginePos();
  187. //! Override the position of engine (model space)
  188. proto native void SetEnginePos(vector pos);
  189. //! Returns the index of the current gear, -1 if there is no engine.
  190. proto native int GetCurrentGear();
  191. //! Returns the index of the future gear, -1 if there is no engine.
  192. proto native int GetGear();
  193. //! Returns the index of the neutral gear.
  194. proto native int GetNeutralGear();
  195. //! Returns the number of gears.
  196. proto native int GetGearCount();
  197. //! Shifts the future gear up, triggering gearbox simulation.
  198. proto native void ShiftUp();
  199. //! Shifts the future gear to selected gear, triggering gearbox simulation.
  200. proto native void ShiftTo(int gear);
  201. //! Shifts the future gear down, triggering gearbox simulation.
  202. proto native void ShiftDown();
  203. //! Returns gearbox type. See CarGearboxType enum for more info.
  204. proto native CarGearboxType GearboxGetType();
  205. //! Returns gearbox mode. This is useful when car has automatic gearbox.
  206. proto native CarAutomaticGearboxMode GearboxGetMode();
  207. //! Returns true if any of the wheels are locked in terms of its movement.
  208. proto native bool WheelIsAnyLocked();
  209. /*!
  210. Returns the raw angular velocity of the wheel, unstable value
  211. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  212. */
  213. proto native float WheelGetAngularVelocity( int wheelIdx );
  214. /*!
  215. Returns true if given wheel is making any contact
  216. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  217. */
  218. proto native bool WheelHasContact( int wheelIdx );
  219. /*!
  220. Returns the position of contact in world space, only valid if there was an actual contact
  221. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  222. */
  223. proto native vector WheelGetContactPosition( int wheelIdx );
  224. /*!
  225. Returns the normal of contact in world space, only valid if there was an actual contact
  226. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  227. */
  228. proto native vector WheelGetContactNormal( int wheelIdx );
  229. /*!
  230. Returns the direction pointing forwards that the wheel is facing
  231. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  232. */
  233. proto native vector WheelGetDirection( int wheelIdx );
  234. /*!
  235. Returns the surface that the wheel is nearby
  236. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  237. */
  238. proto native SurfaceInfo WheelGetSurface( int wheelIdx );
  239. /*!
  240. Returns the state that the wheel is in with water
  241. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  242. */
  243. proto native CarWheelWaterState WheelGetWaterState( int wheelIdx );
  244. /*!
  245. Returns the entity attached that represents the wheel
  246. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  247. */
  248. proto native EntityAI WheelGetEntity( int wheelIdx );
  249. /*!
  250. Returns true if given wheel is locked in terms of its movement.
  251. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  252. */
  253. proto native bool WheelIsLocked( int wheelIdx );
  254. //! How many wheel can be attached to a car (hubs only)
  255. proto native int WheelCount();
  256. //! Number of actually attached wheels (hubs only)
  257. proto native int WheelCountPresent();
  258. /*!
  259. Returns tank capacity for the specified vehicle's fluid.
  260. \param fluid the specified fluid type
  261. */
  262. proto native float GetFluidCapacity(CarFluid fluid);
  263. /*!
  264. Returns fraction value (in range <0, 1>)
  265. of the current state of the specified vehicle's fluid.
  266. \param[in] fluid the specified fluid type
  267. */
  268. proto native float GetFluidFraction(CarFluid fluid);
  269. //! Removes from the specified fluid the specified amount.
  270. proto native void Leak(CarFluid fluid, float amount);
  271. //! Removes all the specified fluid from vehicle.
  272. proto native void LeakAll(CarFluid fluid);
  273. //! Adds to the specified fluid the specified amount.
  274. proto native void Fill(CarFluid fluid, float amount);
  275. /*!
  276. Is called every time the game wants to start the engine.
  277. \return true if the engine can start, false otherwise.
  278. */
  279. bool OnBeforeEngineStart()
  280. {
  281. // engine can start by default
  282. return true;
  283. }
  284. /*!
  285. Is called every time the engine starts.
  286. */
  287. void OnEngineStart() {}
  288. /*!
  289. Is called every time the engine stops.
  290. */
  291. void OnEngineStop() {}
  292. /*!
  293. Is called every time when the simulation changed gear.
  294. \param[in] newGear new gear level
  295. \param[in] oldGear previous gear level before gear shift
  296. */
  297. void OnGearChanged(int newGear, int oldGear)
  298. {
  299. }
  300. /*!
  301. Is called every time when the specified vehicle's fluid level changes.
  302. This callback is called on owner only.
  303. \param[in] fluid fluid identifier, \see CarFluid
  304. \param[in] newValue new fluid level
  305. \param[in] oldValue previous fluid level before change
  306. */
  307. void OnFluidChanged(CarFluid fluid, float newValue, float oldValue) {}
  308. /*!
  309. Is called every sound simulation step.
  310. In this callback, user can modify behaviour of sound controllers.
  311. \param[in] ctrl sound controller identifier, \see CarSoundCtrl
  312. \param[in] oldValue already computed value by the game code
  313. \return new value of the specified sound controller.
  314. */
  315. float OnSound(CarSoundCtrl ctrl, float oldValue)
  316. {
  317. // just use the computed value by the game code
  318. return oldValue;
  319. }
  320. [Obsolete("no replacement")]
  321. proto native void ForcePosition(vector pos);
  322. [Obsolete("no replacement")]
  323. proto native void ForceDirection(vector dir);
  324. [Obsolete("Use methods directly on Car")]
  325. proto native CarController GetController();
  326. [Obsolete("Use Car.IsTurbo")]
  327. proto native float GetThrustTurbo();
  328. [Obsolete("Use Car.IsGentle")]
  329. proto native float GetThrustGentle();
  330. [Obsolete("Use Car.GetThrottle")]
  331. proto native float GetThrust();
  332. [Obsolete("Use Car.SetThrottle/Car.SetTurbo/Car.SetGentle")]
  333. proto native void SetThrust(float in, float gentle = 0, float turbo = 0);
  334. [Obsolete("no replacement")];
  335. proto native void SetClutchState(bool in);
  336. [Obsolete("Use Car.GetGearCount")]
  337. proto native int GetGearsCount();
  338. };
  339. //! DEPRECATED class left for backwards compatibility, methods are available on car itself now
  340. class CarController
  341. {
  342. private void CarController() {}
  343. private void ~CarController() {}
  344. //! Returns the current steering value in range <-1, 1>.
  345. proto float GetSteering();
  346. /*!
  347. Sets the steering value.
  348. \param in should be in range <-1, 1>
  349. \param analog indicates if the input value was taken from analog controller
  350. */
  351. proto void SetSteering( float in, bool analog = false );
  352. //! Returns the current thrust turbo modifier value in range <0, 1>.
  353. proto float GetThrustTurbo();
  354. //! Returns the current thrust gentle modifier value in range <0, 1>.
  355. proto float GetThrustGentle();
  356. //! Returns the current thrust value in range <0, 1>.
  357. proto float GetThrust();
  358. /*!
  359. Sets the thrust value.
  360. \param in should be in range <0, 1>
  361. \param gentle should be in range <0, 1>, thrust modifier
  362. \param turbo should be in range <0, 1>, thrust modifier
  363. */
  364. proto void SetThrust( float in, float gentle = 0, float turbo = 0 );
  365. //! Returns the current brake value in range <0, 1>.
  366. proto float GetBrake();
  367. /*!
  368. Sets the brake value.
  369. \param in should be in range <0, 1>
  370. \param panic should be in range <0, 1>
  371. */
  372. proto void SetBrake( float in, float panic = 0 );
  373. //! Returns index of the current gear.
  374. proto int GetGear();
  375. proto void ShiftUp();
  376. proto void ShiftTo( CarGear gear );
  377. proto void ShiftDown();
  378. };