Advertisement
Guest User

Untitled

a guest
May 17th, 2015
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.88 KB | None | 0 0
  1. @name Simulated_Engine
  2. @inputs [POD, Chassis, Engine, Flywheel, Slave, FuelTank]:wirelink
  3. @outputs [Engine_Active, Engine_RPM, Engine_Torque, Flywheel_PowerInertia, Engine_Ignition, Gearbox_Gear, Gearbox_Clutch, Turbo_Boost, Turbo_PSI, Fuel_Capacity, Fuel_Liters, Flywheel_RPM]:number
  4. @persist [C, E, S, T]:entity
  5. @persist [Engine_IdleRev, Engine_RevLimiter, Engine_Bore, Engine_Stroke, Engine_Cylinders, Engine_Airflow, Engine_Configuration, Engine_Displacement, Engine_PeakTorque, Engine_Redline, Engine_Mass, Engine_FlywheelMass, Engine_Idle]:number [Engine_TorqueTable]:array
  6. @persist [Fuel_Enabled, Fuel_Capacity, Fuel_Air_Ratio, Fuel_Density, Air_Density]:number
  7. @persist [Gearbox_FinalDrive, Gearbox_Delay, Gearbox_LockValue, Gearbox_ClutchSpeed]:number [Gearbox_Ratios]:array
  8. @persist [Turbo, Turbo_Exp, Turbo_Inertia, Turbo_FlowScale, Turbo_DragScale, Turbo_PSIMax, Turbo_AirPressure, Turbo_MaxFlow, Turbo_MaxRPM]:number
  9. @persist [Engine_SoundOn, Engine_SoundOff]:string
  10. @trigger none
  11.  
  12. @persist [Gearbox_Ratio, Gearbox_Gear, Gearbox_Lock, Gearbox_Clutch]:number
  13. @persist [Throttle_Delay, Clutch_Delay]:number
  14. @persist [Fuel_Liters]:number
  15. @persist [Brake, Engine_IdleThrottle, Engine_Ignition, Engine_PowerCut, Engine_Volume, Flywheel_PowerInertia]:number
  16. @persist [Turbo_RPM]:number
  17.  
  18. if (duped() | dupefinished()) {reset()} else {runOnTick(1)}
  19.  
  20. if (first()) {
  21. Engine_Bore = 84
  22. Engine_Stroke = 89.6
  23. Engine_Cylinders = 6
  24. Engine_Airflow = 135.5
  25. Engine_Idle = 800
  26. Engine_IdleRev = 0.1
  27. Engine_RevLimiter = 1
  28. Engine_TorqueTable = array(0.6, 0.7, 0.8, 0.74, 0.5)
  29. Engine_FlywheelMass = 12
  30. Engine_Configuration = 0
  31. Engine_Configurations = table(
  32. 0 = "Inline-",
  33. 1 = "V"
  34. )
  35.  
  36. Gearbox_Ratios = array(
  37. 4.11,
  38. 2.315,
  39. 1.542,
  40. 1.179,
  41. 1.000,
  42. 0.846
  43. )
  44.  
  45. Gearbox_Ratios[-1, number] = -3.727
  46. Gearbox_FinalDrive = 3.154
  47. Gearbox_Delay = 150
  48. Gearbox_ClutchSpeed = 0.1
  49. Gearbox_LockValue = 0.05
  50.  
  51. Fuel_Enabled = 1
  52. Fuel_Air_Ratio = 1 / 12.5
  53. Fuel_Density = 0.72 # 0.72 | 0.745 ( Petrol | Diesel )
  54. Air_Density = 0.0012
  55.  
  56. Engine_Displacement = round((Engine_Cylinders * pi() * Engine_Bore ^ 2 * Engine_Stroke / 4000000), 1)
  57. Engine_PeakTorque = round((Engine_Stroke / Engine_Bore * Engine_Displacement * Engine_Airflow) * (Engine_Stroke / Engine_Bore), 2)
  58. Engine_PeakTorque *= 0.8
  59. #Engine_Redline = round((Engine_Bore / Engine_Stroke / Engine_Cylinders * Engine_Airflow * 2000 / 6), 0)
  60. #Engine_Redline *= (Engine_Bore / Engine_Stroke)
  61. #Engine_Redline -= Engine_Redline % 100
  62. Engine_Redline = 7500 # Override original value
  63. Engine_Mass = round((floor(Engine_Displacement * 45 + Engine_Cylinders * 4 + clamp(Engine_Configuration, 0, 1) * 10)), 0)
  64. Engine_Mass += Engine_FlywheelMass
  65. Engine_Mass += (Gearbox_Ratios:count() * 8.66)
  66.  
  67. Turbo = 1
  68. Turbo_Exp = 1.95
  69. Turbo_Inertia = 0.025
  70. Turbo_FlowScale = 75
  71. Turbo_DragScale = 0.01
  72. Turbo_PSIMax = 30
  73. Turbo_AirPressure = 24.7
  74. Turbo_MaxFlow = Engine_Redline * ((Turbo_PSIMax + Turbo_AirPressure) / Turbo_AirPressure)
  75. Turbo_MaxRPM = 140000
  76.  
  77. C = Chassis:entity()
  78.  
  79. S = Slave:entity()
  80. S:setMass(5e4)
  81. S:propGravity(0)
  82.  
  83. E = Engine:entity()
  84. E:setMass(Engine_Mass)
  85. #E:propInertia(maxVec(vec(Engine_Mass, Engine_Mass, 2), vec(40, 40, 2)))
  86.  
  87. T = FuelTank:entity()
  88. Fuel_Capacity = T:boxSize():length()
  89. Fuel_Liters = Fuel_Capacity
  90.  
  91. foreach (K, V:entity = Flywheel["Entities", array]) {
  92. V:setMass(Engine_Mass)
  93. V:propInertia(maxVec(vec(Engine_Mass, Engine_Mass, 2), vec(40, 40, 2)))
  94. V:propGravity(0)
  95. }
  96.  
  97. setName(
  98. "Simulated Engine" + "\n"
  99. + Engine_Configurations[Engine_Configuration, string] + Engine_Cylinders + "\n"
  100. + "Displacement: " + Engine_Displacement + " L\n"
  101. + "Peak Torque: " + Engine_PeakTorque + " nm\n"
  102. + "Redline: " + Engine_Redline + "\n"
  103. + "Flywheel Mass: " + Engine_FlywheelMass * 0.1 + " kg"
  104. )
  105.  
  106. Engine_SoundOn = "acf_extra/vehiclefx/engines/l4/escort_onmid.WAV"
  107. Engine_SoundOff = "acf_extra/vehiclefx/engines/l4/escort_onverylow.WAV"
  108.  
  109. function void loadSounds() {
  110. E:soundPlay("On", 0, Engine_SoundOn:lower())
  111. E:soundPlay("Off", 0, Engine_SoundOff:lower())
  112. if (Turbo) {
  113. E:soundPlay("Turbo", 0, "acf_engines/turbine_small.wav")
  114. }
  115. }
  116.  
  117. loadSounds()
  118.  
  119. runOnLast(1)
  120. }
  121.  
  122. POD_Active = POD:number("Active")
  123.  
  124. if (changed(POD_Active) | POD_Active) {
  125. Key_W = POD:number("W")
  126. Key_S = POD:number("S")
  127. Key_M1 = POD:number("Mouse1")
  128. Key_M2 = POD:number("Mouse2")
  129. Key_F = POD:number("Light")
  130. Key_R = POD:number("R")
  131. Key_Sp = POD:number("Shift")
  132. Key_Sh = POD:number("Space")
  133. Key_Alt = POD:number("Alt")
  134. Key_Z = POD:number("Zoom")
  135.  
  136. if (changed(Key_Z) && Key_Z) {
  137. Gearbox_Lock = !Gearbox_Lock
  138. }
  139.  
  140. if (changed(Gearbox_Lock)) {
  141. foreach (K, V:entity = Flywheel["Entities", array]) {
  142. if (Gearbox_Lock) {
  143. Pair = Flywheel["Entities", array][(K + 1) % Flywheel["Entities", array]:count(), entity]
  144.  
  145. V:advBallsocketTo(
  146. K,
  147. Pair,
  148. 0,
  149. 0,
  150. vec(),
  151. vec(),
  152. 0,
  153. 0,
  154. vec(-180),
  155. vec(180),
  156. vec(Gearbox_LockValue),
  157. 1,
  158. 0
  159. )
  160. } else {
  161. removeConstraint(K)
  162. }
  163. }
  164. }
  165.  
  166. if (changed(Key_F)) {
  167. if (Engine_Ignition == 1 && Engine_Active == 0) {
  168. Engine_Ignition = 2
  169. }
  170.  
  171. if (Engine_Ignition == 2 & !Key_F) {
  172. Engine_Ignition = 1
  173. }
  174. }
  175.  
  176. if (changed(Key_F) & Key_F) {
  177. if (Engine_Ignition == 0) {
  178. Engine_Ignition = 1
  179. }
  180.  
  181. if (Engine_Ignition == 1 && Engine_Active == 1) {
  182. Engine_Ignition = 0
  183. }
  184. }
  185.  
  186. Engine_Ignition = min(Engine_Ignition, (Engine_Active ? 1 : 2))
  187.  
  188. if (changed(Engine_Ignition)) {
  189. if (Engine_Ignition == 2) {
  190. E:soundPlay("Start", 0, "acf_extra/vehiclefx/starters/starter2.wav")
  191. soundVolume("Start", 0.5)
  192. } else {
  193. soundStop("Start")
  194. }
  195. }
  196.  
  197. soundPitch("Start", max(Engine_RPM / Engine_Idle, 0.35) * 200)
  198.  
  199. Gearbox_Gear += (changed(Key_M1) & Key_M1) - (changed(Key_M2) & Key_M2)
  200. Gearbox_Gear *= !(changed(Key_R) & Key_R)
  201. Gearbox_Gear = clamp(Gearbox_Gear, Gearbox_Ratios:minIndex(), Gearbox_Ratios:count())
  202.  
  203. if (changed(Gearbox_Gear)) {
  204. if (Gearbox_Ratios[Gearbox_Gear, number]) {
  205. Gearbox_Ratio = Gearbox_Ratios[Gearbox_Gear, number] * Gearbox_FinalDrive
  206. }
  207.  
  208. C:soundPlay("Gear_Change", 0, "physics/plaster/ceiling_tile_impact_hard3.wav")
  209. soundPitch("Gear_Change", 60)
  210. soundVolume("Gear_Change", 0.3)
  211.  
  212. timer("Gear_Change", Gearbox_Delay)
  213.  
  214. Throttle_Delay = 1
  215. Clutch_Delay = 1
  216.  
  217. foreach (K, V:entity = Flywheel["Entities", array]) {
  218. local Mass = max(Engine_Mass * Gearbox_Ratio, Engine_Mass)
  219. V:setMass(Mass)
  220. V:propInertia(maxVec(vec(Mass, Mass, 2), vec(40, 40, 2)))
  221. }
  222. }
  223.  
  224. Brake += (Key_S - Brake) * 0.025
  225. Brake *= Key_S
  226. }
  227.  
  228. if (clk("Beep")) {
  229. soundStop("Beep")
  230.  
  231. C:soundPlay("Beep", 1, "buttons/bell1.wav")
  232. soundVolume("Beep", 0.2)
  233. soundPitch("Beep", 200)
  234. }
  235.  
  236. Engine_Active = (Engine_RPM > 350) * sign(Engine_Ignition)
  237. if (Engine_Ignition & !Engine_Active) {timer("Beep", 1200)}
  238. if (changed(Engine_Active) & Engine_Active) {loadSounds(), stoptimer("Beep")}
  239. if (!Engine_Active & (Engine_Ignition == 2) & (Gearbox_Clutch == 1)) {Engine_RPM += 25}
  240.  
  241. Chassis_Feedback = vec()
  242. Flywheels_RPM = array()
  243.  
  244. foreach (K, V:entity = Flywheel["Entities", array]) {
  245. Flywheels_RPM[K, number] = V:angVelVector():dot(V:toLocalAxis(E:up())) / 6
  246.  
  247. if (Key_S) {
  248. local Force = 500 * Flywheels_RPM[K, number] * Brake
  249. V:applyTorque(V:toLocalAxis(-E:up()) * Force)
  250. Chassis_Feedback += Force
  251. }
  252. }
  253.  
  254. if (Chassis_Feedback) {C:applyTorque(Chassis_Feedback * C:toLocalAxis(E:up()))}
  255.  
  256. Flywheel_RPM = Flywheels_RPM:average()
  257.  
  258. Gearbox_Clutch -= Gearbox_Clutch * Gearbox_ClutchSpeed
  259. if (!Gearbox_Gear | Clutch_Delay | Key_S | Key_Sp) {Gearbox_Clutch = 1}
  260. if (Key_Sh) {Gearbox_Clutch = max(Gearbox_Clutch, 0.25)}
  261. #Gearbox_Clutch = max(Gearbox_Clutch, 0.02, (Engine_RPM / Engine_Redline) * 0.1)
  262.  
  263. Engine_RealRPM = (Engine_RPM * Gearbox_Clutch) + ((Flywheel_RPM * Gearbox_Ratio) * (1 - Gearbox_Clutch))
  264.  
  265. Engine_Throttle = (Key_W ? (Key_Alt ? 1 : 0.50) : 0)
  266.  
  267. Engine_IdleThrottle += ((min(Engine_RPM, Engine_RealRPM) < Engine_Idle) ? Engine_IdleRev : -Engine_IdleRev)
  268. Engine_IdleThrottle = clamp(Engine_IdleThrottle, 0, 0.5) * Engine_Active
  269.  
  270. Engine_Throttle = max(Engine_Throttle, Engine_IdleThrottle)
  271.  
  272. if (Fuel_Enabled) {
  273. Fuel_Liters -= ((((Engine_Displacement * (Engine_RPM/2)) * Air_Density * Fuel_Air_Ratio)/Fuel_Density)/(65*60) * max(Engine_Throttle, 0.01)) * Engine_Active
  274. Fuel_Liters = clamp(Fuel_Liters, 0, T:boxSize():length())
  275. Engine_Active *= (Fuel_Liters > 0)
  276. T:setMass(round((Fuel_Liters * Fuel_Density) + (T:boxSize():length() * 0.1), 1))
  277. }
  278.  
  279. Engine_Throttle *= Engine_Active
  280. Engine_Throttle *= !Throttle_Delay
  281.  
  282. Engine_PowerCut += ((Engine_RealRPM > Engine_Redline) ? 0.05 : -0.05)
  283. Engine_PowerCut = clamp(Engine_PowerCut, 0, 1) * round(Engine_Throttle)
  284.  
  285. if (clk("Gear_Change")) {Throttle_Delay = 0, Clutch_Delay = 0}
  286.  
  287. Factor = clamp((Engine_RPM / Engine_Redline), 0, 1)
  288. Engine_Torque = (5 * (1 - Factor) ^ 4 * Factor * Engine_TorqueTable[1, number]) + (10 * (1 - Factor) ^ 3 * Factor ^ 2 * Engine_TorqueTable[2, number]) + (10 * (1 - Factor) ^ 2 * Factor ^ 3 * Engine_TorqueTable[3, number]) + (5 * (1 - Factor) * Factor ^ 4 * Engine_TorqueTable[4, number]) + (Factor ^ 5 * Engine_TorqueTable[5, number])
  289. Engine_Torque = clamp(Engine_Torque, 0, 1)
  290.  
  291. if (Turbo) {
  292. Turbo_PSI = Turbo_PSIMax * Turbo_RPM / Turbo_MaxRPM
  293.  
  294. Turbo_Boost = (Turbo_PSI + Turbo_AirPressure) / Turbo_AirPressure
  295.  
  296. Turbo_Flow = max(Engine_RPM, 1) * Turbo_Boost / Turbo_MaxFlow
  297.  
  298. Turbo_Drag = (Turbo_MaxFlow * Turbo_DragScale) * (Turbo_RPM / Turbo_MaxRPM) * (1 - Engine_Throttle) / Turbo_Inertia
  299.  
  300. Turbo_RPM = clamp(Turbo_RPM + (Turbo_FlowScale * (Turbo_Flow ^ Turbo_Exp)) / Turbo_Inertia - Turbo_Drag, 1, Turbo_MaxRPM)
  301.  
  302. soundVolume("Turbo", (Turbo_Flow / 1.5 - 0.25))
  303. soundPitch("Turbo", 75 + 50 * Turbo_RPM / Turbo_MaxRPM)
  304.  
  305. if (changed(Engine_Throttle) & !Engine_Throttle) {
  306. soundStop("BOV")
  307.  
  308. E:soundPlay("BOV", 1, "acf_extra/vehiclefx/boost/turbo_hiss1.wav")
  309. soundVolume("BOV", max((Turbo_RPM / Turbo_MaxRPM) - 0.2, 0))
  310. soundPitch("BOV", 85 + 25 * (Turbo_RPM / Turbo_MaxRPM))
  311. }
  312.  
  313. Engine_Torque *= (Turbo_Boost / 1.25)
  314. }
  315.  
  316. Flywheel_PowerInertia += ((Engine_PeakTorque * Engine_Torque) - Flywheel_PowerInertia) / Engine_FlywheelMass
  317. Flywheel_PowerInertia = max(Flywheel_PowerInertia, (Engine_PeakTorque * Engine_Torque))
  318.  
  319. if (Gearbox_Clutch < 1) {
  320. Engine_Inertia = clamp(Engine_RealRPM, 0, Engine_Redline) - (Flywheel_RPM * Gearbox_Ratio)
  321. Engine_Inertia *= (1 - Gearbox_Clutch)
  322. Engine_Inertia /= Engine_Redline
  323.  
  324. Engine_Throttle -= ((0.285 * (Engine_RealRPM / Engine_Redline)) * (Engine_Throttle <= 0))
  325.  
  326. Flywheel_Power = Flywheel_PowerInertia
  327. Flywheel_Power *= Gearbox_Ratio
  328. Flywheel_Power *= (1 - Gearbox_Clutch)
  329.  
  330. Flywheel_Power *= min(clamp(Engine_Throttle, -1, ((1 - Engine_PowerCut) ^ 2)) + Engine_Inertia, 1)
  331. Flywheel_Power *= clamp(1 - (((Engine_RealRPM / Engine_RPM) - 1) * Engine_Throttle), -1, 1) ^ Engine_FlywheelMass
  332.  
  333. if (Flywheel_Power) {
  334. foreach (K, V:entity = Flywheel["Entities", array]) {
  335. V:applyOffsetForce(E:right() * (Flywheel_Power / Flywheel["Entities", array]:count()), V:pos() - (E:forward() * 39.37))
  336. V:applyOffsetForce(E:right() * -(Flywheel_Power / Flywheel["Entities", array]:count()), V:pos() + (E:forward() * 39.37))
  337. }
  338.  
  339. C:applyOffsetForce(E:right() * -Flywheel_Power, E:pos() - (E:forward() * 39.37))
  340. C:applyOffsetForce(E:right() * Flywheel_Power, E:pos() + (E:forward() * 39.37))
  341. }
  342.  
  343. Gearbox_Feedback = (Flywheel_RPM * Gearbox_Ratio) - Engine_RPM
  344. Gearbox_Feedback /= Engine_FlywheelMass
  345. }
  346.  
  347. Gearbox_Feedback *= (1 - Gearbox_Clutch)
  348.  
  349. Engine_Feedback = (sign(Engine_Throttle) * Engine_Redline - Engine_RPM) * (sign(Engine_Throttle) ? (abs(Engine_Throttle * Engine_Torque)) : (Engine_Active ? 0.03 : 0.13)) / 4
  350. Engine_Feedback *= Gearbox_Clutch
  351.  
  352. Engine_RPM += min(Gearbox_Feedback + Engine_Feedback, (Engine_Redline - Engine_RPM) / (Engine_FlywheelMass * (Engine_Stroke / Engine_Bore)))
  353. if (Engine_RPM > (Engine_Redline * 0.95) && Engine_RevLimiter) {Engine_RPM = Engine_Redline * 0.9}
  354. Engine_RPM = min(Engine_RPM, Engine_Redline)
  355. Engine_RPM *= random(0.995, 1.005)
  356.  
  357. Engine_Volume += (max(Engine_Throttle ^ 1.25, (Engine_RPM / 9000) * 0.25) - Engine_Volume) * 0.2
  358.  
  359. soundPitch("On", (Engine_RPM / 9000) * 155 + (50 * Engine_Active))
  360. soundVolume("On", Engine_Volume * 0.75)
  361.  
  362. soundPitch("Off", (Engine_RPM / 9000) * 155 + (50 * Engine_Active))
  363. soundVolume("Off", ((1 - Engine_Volume) ^ 1.5) * 0.25)
  364.  
  365. S:propFreeze(1)
  366. S:setAng(E:angles())
  367.  
  368. if (last()) {
  369. foreach (K, V:entity = Flywheel["Entities", array]) {
  370. removeConstraint(K)
  371. }
  372. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement