car.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. protected bool DetectFlippedUsingWheels(VehicleFlippedContext ctx, bool disallowSide)
  115. {
  116. if (disallowSide && (vector.Dot(GetDirectionUp(), vector.Up) < 0.7))
  117. {
  118. // return as "flipped", vehicle isn't pointing enough up to be reasonably certain
  119. return true;
  120. }
  121. int wheelCount = WheelCount();
  122. for (int wheelIdx = 0; wheelIdx < wheelCount; wheelIdx++)
  123. {
  124. if (!WheelHasContact(wheelIdx))
  125. {
  126. // wheel not in contact, then we could be flipped, we assume there exist other predicates
  127. return true;
  128. }
  129. }
  130. // all wheels in contact (or zero registered wheels), then we are in contact
  131. return false;
  132. }
  133. //-----------------------------------------------------------------------------
  134. // controls
  135. //! Returns the current steering value in range <-1, 1>.
  136. proto native float GetSteering();
  137. /*!
  138. Sets the steering value.
  139. \param in should be in range <-1, 1>
  140. \param analog indicates if the input value was taken from analog controller
  141. */
  142. proto native void SetSteering( float in, bool analog = false );
  143. //! Returns the current thrust turbo modifier value in range <0, 1>.
  144. proto native float GetThrustTurbo();
  145. //! Returns the current thrust gentle modifier value in range <0, 1>.
  146. proto native float GetThrustGentle();
  147. //! Returns the current thrust value in range <0, 1>.
  148. proto native float GetThrust();
  149. /*!
  150. Sets the thrust value.
  151. \param in should be in range <0, 1>
  152. \param gentle should be in range <0, 1>, thrust modifier
  153. \param turbo should be in range <0, 1>, thrust modifier
  154. */
  155. proto native void SetThrust( float in, float gentle = 0, float turbo = 0 );
  156. //! Returns the current brake value in range <0, 1>.
  157. proto native float GetBrake();
  158. /*!
  159. Sets the brake value.
  160. \param in should be in range <0, 1>
  161. \param panic should be in range <0, 1>
  162. */
  163. proto native void SetBrake( float in, float panic = 0, bool gentle = false );
  164. //! Returns the current handbrake value in range <0, 1>.
  165. proto native float GetHandbrake();
  166. /*!
  167. Sets the handbrake value.
  168. \param in should be in range <0, 1>
  169. */
  170. proto native void SetHandbrake( float in );
  171. /*!
  172. Sets if brakes should activate without a driver present
  173. */
  174. proto native void SetBrakesActivateWithoutDriver( bool activate = true );
  175. //! Returns the current clutch value in range <0, 1>.
  176. proto native float GetClutch();
  177. /*!
  178. Sets the clutch state.
  179. */
  180. proto native void SetClutchState( bool in );
  181. //! Returns index of the current gear.
  182. proto native int GetGear();
  183. proto native void ShiftUp();
  184. proto native void ShiftTo( CarGear gear );
  185. proto native void ShiftDown();
  186. //-----------------------------------------------------------------------------
  187. //-----------------------------------------------------------------------------
  188. // fluids
  189. /*!
  190. Returns tank capacity for the specified vehicle's fluid.
  191. \param fluid the specified fluid type
  192. */
  193. proto native float GetFluidCapacity( CarFluid fluid );
  194. /*!
  195. Returns fraction value (in range <0, 1>)
  196. of the current state of the specified vehicle's fluid.
  197. \param[in] fluid the specified fluid type
  198. */
  199. proto native float GetFluidFraction( CarFluid fluid );
  200. //! Removes from the specified fluid the specified amount.
  201. proto native void Leak( CarFluid fluid, float amount );
  202. //! Removes all the specified fluid from vehicle.
  203. proto native void LeakAll( CarFluid fluid );
  204. //! Adds to the specified fluid the specified amount.
  205. proto native void Fill( CarFluid fluid, float amount );
  206. /*!
  207. Is called every time when the specified vehicle's fluid level
  208. changes eg. when vehicle is consuming fuel.
  209. \param[in] fluid fluid identifier, \see CarFluid
  210. \param[in] newValue new fluid level
  211. \param[in] oldValue previous fluid level before change
  212. */
  213. void OnFluidChanged( CarFluid fluid, float newValue, float oldValue ) {}
  214. //-----------------------------------------------------------------------------
  215. //-----------------------------------------------------------------------------
  216. // engine
  217. //! Returns engine's min operating rpm
  218. proto native float EngineGetRPMMin();
  219. //! Returns engine's idle rpm before engine stalls.
  220. proto native float EngineGetRPMIdle();
  221. //! Returns engine's max rpm before engine blows up.
  222. proto native float EngineGetRPMMax();
  223. //! Returns engine's maximal working rpm without damaging the engine.
  224. proto native float EngineGetRPMRedline();
  225. //! Returns engine's rpm value.
  226. proto native float EngineGetRPM();
  227. //! Returns true when engine is running, false otherwise.
  228. proto native bool EngineIsOn();
  229. //! Starts the engine.
  230. proto native void EngineStart();
  231. /*!
  232. Is called every time the game wants to start the engine.
  233. \return true if the engine can start, false otherwise.
  234. */
  235. bool OnBeforeEngineStart()
  236. {
  237. // engine can start by default
  238. return true;
  239. }
  240. //! Is called every time the engine starts.
  241. void OnEngineStart() {}
  242. //! Stops the engine.
  243. proto native void EngineStop();
  244. //! Is called every time the engine stops.
  245. void OnEngineStop() {}
  246. //! Get actual position of engine (model space)
  247. proto native vector GetEnginePos();
  248. //! Override the position of engine (model space)
  249. proto native void SetEnginePos(vector pos);
  250. //-----------------------------------------------------------------------------
  251. //-----------------------------------------------------------------------------
  252. // gearbox
  253. //! Returns total number of gears.
  254. proto native int GetGearsCount();
  255. //! Returns gearbox type. See CarGearboxType enum for more info.
  256. proto native CarGearboxType GearboxGetType();
  257. //! Returns gearbox mode. This is useful when car has automatic gearbox.
  258. proto native CarAutomaticGearboxMode GearboxGetMode();
  259. /*!
  260. Is called every time when the simulation changed gear.
  261. \param[in] newGear new gear level
  262. \param[in] oldGear previous gear level before gear shift
  263. */
  264. void OnGearChanged( int newGear, int oldGear )
  265. {
  266. }
  267. //-----------------------------------------------------------------------------
  268. //-----------------------------------------------------------------------------
  269. // wheels
  270. //! Returns true if any of the wheels are locked in terms of its movement.
  271. proto native bool WheelIsAnyLocked();
  272. /*!
  273. Returns the raw angular velocity of the wheel, unstable value
  274. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  275. */
  276. proto native float WheelGetAngularVelocity( int wheelIdx );
  277. /*!
  278. Returns true if given wheel is making any contact
  279. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  280. */
  281. proto native bool WheelHasContact( int wheelIdx );
  282. /*!
  283. Returns the position of contact in world space, only valid if there was an actual contact
  284. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  285. */
  286. proto native vector WheelGetContactPosition( int wheelIdx );
  287. /*!
  288. Returns the normal of contact in world space, only valid if there was an actual contact
  289. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  290. */
  291. proto native vector WheelGetContactNormal( int wheelIdx );
  292. /*!
  293. Returns the direction pointing forwards that the wheel is facing
  294. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  295. */
  296. proto native vector WheelGetDirection( int wheelIdx );
  297. /*!
  298. Returns the surface that the wheel is nearby
  299. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  300. */
  301. proto native SurfaceInfo WheelGetSurface( int wheelIdx );
  302. /*!
  303. Returns the state that the wheel is in with water
  304. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  305. */
  306. proto native CarWheelWaterState WheelGetWaterState( int wheelIdx );
  307. /*!
  308. Returns the entity attached that represents the wheel
  309. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  310. */
  311. proto native EntityAI WheelGetEntity( int wheelIdx );
  312. /*!
  313. Returns true if given wheel is locked in terms of its movement.
  314. \param[in] wheelIdx index of the wheel, they are counted from left-front to rear-right
  315. */
  316. proto native bool WheelIsLocked( int wheelIdx );
  317. //! How many wheel can be attached to a car (hubs only)
  318. proto native int WheelCount();
  319. //! Number of actually attached wheels (hubs only)
  320. proto native int WheelCountPresent();
  321. //-----------------------------------------------------------------------------
  322. //-----------------------------------------------------------------------------
  323. // events
  324. /*!
  325. Is called every time when vehicle collides with other object.
  326. \param[in] zoneName configured vehicle's zone that was hit
  327. \param[in] localPos position where the vehicle was hit in vehicle's space
  328. \param[in] other object with which the vehicle is colliding
  329. \param[in] data contact properties
  330. */
  331. void OnContact( string zoneName, vector localPos, IEntity other, Contact data ) {}
  332. /*!
  333. Is called every sound simulation step.
  334. In this callback, user can modify behaviour of sound controllers.
  335. \param[in] ctrl sound controller identifier, \see CarSoundCtrl
  336. \param[in] oldValue already computed value by the game code
  337. \return new value of the specified sound controller.
  338. */
  339. float OnSound( CarSoundCtrl ctrl, float oldValue )
  340. {
  341. // just use the computed value by the game code
  342. return oldValue;
  343. }
  344. /*!
  345. Is called after every input simulation step.
  346. Note that the player character and other systems can always change the internal state.
  347. It is highly recommended to store state of custom inputs elsewhere and call Setters here.
  348. \param[in] dt frame time in seconds
  349. */
  350. void OnInput( float dt ) {}
  351. /*!
  352. Is called every game frame.
  353. \param[in] dt frame time in seconds
  354. */
  355. void OnUpdate( float dt ) {}
  356. //-----------------------------------------------------------------------------
  357. // implemented only in internal configuration
  358. proto native void ForcePosition( vector pos );
  359. // implemented only in internal configuration
  360. proto native void ForceDirection( vector dir );
  361. };
  362. //! DEPRECATED class left for backwards compatibility, methods are available on car itself now
  363. class CarController
  364. {
  365. private void CarController() {}
  366. private void ~CarController() {}
  367. //! Returns the current steering value in range <-1, 1>.
  368. proto float GetSteering();
  369. /*!
  370. Sets the steering value.
  371. \param in should be in range <-1, 1>
  372. \param analog indicates if the input value was taken from analog controller
  373. */
  374. proto void SetSteering( float in, bool analog = false );
  375. //! Returns the current thrust turbo modifier value in range <0, 1>.
  376. proto float GetThrustTurbo();
  377. //! Returns the current thrust gentle modifier value in range <0, 1>.
  378. proto float GetThrustGentle();
  379. //! Returns the current thrust value in range <0, 1>.
  380. proto float GetThrust();
  381. /*!
  382. Sets the thrust value.
  383. \param in should be in range <0, 1>
  384. \param gentle should be in range <0, 1>, thrust modifier
  385. \param turbo should be in range <0, 1>, thrust modifier
  386. */
  387. proto void SetThrust( float in, float gentle = 0, float turbo = 0 );
  388. //! Returns the current brake value in range <0, 1>.
  389. proto float GetBrake();
  390. /*!
  391. Sets the brake value.
  392. \param in should be in range <0, 1>
  393. \param panic should be in range <0, 1>
  394. */
  395. proto void SetBrake( float in, float panic = 0 );
  396. //! Returns index of the current gear.
  397. proto int GetGear();
  398. proto void ShiftUp();
  399. proto void ShiftTo( CarGear gear );
  400. proto void ShiftDown();
  401. };