Advertisement
lowlyforskin

Expression2 Transmission

Nov 1st, 2024 (edited)
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.78 KB | Gaming | 0 0
  1. @name Manual 5-speed with clutch assist AWD ALPHA
  2. #UPDATED BECAUSE OF CLUTCH WEIRDNESS
  3. # Inputs and Outputs
  4. @inputs [Wheel1 Wheel2 Wheel3 Wheel4 RotatingObject]:entity Gearup Geardown Base:entity RPM Clutchin SPEEDIN Throttle SETMPH AnyPiston:entity
  5. @outputs Gear Ratio Clutch WheelSpeed Calc RAngVelocity GearV SwitchGear
  6. @persist Wheels:array RotatingObjectWeight PistonWeight FullRatio FinalRotational RAngVelocity FlywheelMass DefinedClutch
  7. @persist Ratio Constant DifRat Gear Math1 Math2 FinalMass BoxSize AngVelMult Clutch AngVel1 AngVel2 GearV Wheel1Mass Mass [Speeds Constants Gears]:array SpeedSwitch Out OverrideClutch
  8. @persist GearclutchIncrease StaticCalc Calculation2
  9. @trigger
  10.  
  11. runOnTick(1)
  12.  
  13. if (first() | dupefinished()) {
  14.     # Initialize transmission and weight settings
  15.     DifRat = 1/3.98
  16.     Ratio = 1
  17.     AngVelMult = 1
  18.     PistonWeight = AnyPiston:mass() * 4  # Amount of pistons
  19.     RotatingObjectWeight = (RotatingObject:mass() / RotatingObject:inertia():z()) + PistonWeight
  20.     FlywheelMass = 0
  21.     FinalMass = 2106
  22.     Speeds = array(47, 80, 112, 150, 1350, 190)
  23.     Wheels = array(Wheel1, Wheel2, Wheel3, Wheel4)
  24.     Wheel1Mass = Wheel1:mass()
  25. }
  26.  
  27. # Gear Up
  28. if (changed(Gearup) & Gearup) {
  29.     if (Gear == 0) {
  30.         GearV = 1
  31.     }
  32.     if (Gear < 5) {
  33.         Gear++
  34.     }
  35.     SwitchGear = 1
  36.     Clutch = 0.2
  37.     soundPlay(1, 1, "acf_extra/vehiclefx/trans/default_shift.wav")
  38. }
  39.  
  40. # Gear Down
  41. elseif (changed(Geardown) & Geardown) {
  42.     if (Gear != -1) {
  43.         if (Gear == 0) {        
  44.             GearV = -1
  45.         }
  46.         Gear--
  47.         SwitchGear = 1
  48.         Clutch = 0.05
  49.         soundPlay(1, 1, "acf_extra/vehiclefx/trans/default_shift.wav")
  50.     }
  51. }
  52.  
  53. # Set Gear Velocity to 0 when Gear is Neutral
  54. elseif (Gear == 0) {
  55.     GearV = 0
  56. }
  57.  
  58. # Calculate Ratios for Different Gears
  59. if (Gear != 0) {
  60.     if (Gear == 1) {
  61.         Ratio = 1/0.1
  62.     }
  63.     elseif (Gear == 2) {
  64.         Ratio = 1/0.2
  65.         Calculation2 = abs(Wheels[1, entity]:angVel():pitch()) * 0.67128024961205
  66.     }
  67.     elseif (Gear == 3) {
  68.         Ratio = 1/0.3
  69.         Calculation2 = abs(Wheels[1, entity]:angVel():pitch()) * 0.50751866472455
  70.     }
  71.     elseif (Gear == 4) {
  72.         Ratio = 1/0.4
  73.         Calculation2 = abs(Wheels[1, entity]:angVel():pitch()) * 0.39893571719906
  74.     }
  75.     elseif (Gear == 5) {
  76.         Ratio = 1/0.5
  77.         Calculation2 = abs(Wheels[1, entity]:angVel():pitch()) * 0.34031409322928
  78.     }
  79.     elseif (Gear == -1) {
  80.         Ratio = 1/0.1
  81.     }
  82.  
  83.     # Apply force to wheels based on gear ratio
  84.     for (A = 1, 4, 1) {
  85.         FullRatio = (1 / (DifRat + Ratio))
  86.        
  87.         # Calculate angular velocities and apply forces to each wheel
  88.         if (A <= 2) {
  89.             WAngVelocity = Wheels[A, entity]:angVel():pitch()
  90.             RAngVelocity = (RotatingObject:angVel():yaw() * 6) * FullRatio
  91.             PercentageDifference = 1 - abs(WAngVelocity) / RAngVelocity
  92.             RotationalDifference = abs(abs(WAngVelocity) - RAngVelocity)
  93.             FinalRotational = ((RotationalDifference * ((RotationalDifference + (RotatingObjectWeight - FlywheelMass)) / RotationalDifference)) * PercentageDifference)
  94.             FinalWheel = (RotationalDifference * PercentageDifference) * (FinalMass / Wheel1Mass)
  95.  
  96.             Wheels[A, entity]:applyAngForce(ang(FinalWheel * Clutch * GearV, 0, 0))
  97.             RotatingObject:applyAngForce(ang(0, (-FinalRotational * Clutch) / 4, 0))
  98.         } else {
  99.             WAngVelocity = -Wheels[A, entity]:angVel():pitch()
  100.             RAngVelocity = (RotatingObject:angVel():yaw() * 6) * FullRatio
  101.             PercentageDifference = 1 - abs(WAngVelocity) / RAngVelocity
  102.             RotationalDifference = abs(abs(WAngVelocity) - RAngVelocity)
  103.             FinalRotational = ((RotationalDifference * ((RotationalDifference + (RotatingObjectWeight - FlywheelMass)) / RotationalDifference)) * PercentageDifference)
  104.             FinalWheel = (RotationalDifference * PercentageDifference) * (FinalMass / Wheel1Mass)
  105.  
  106.             Wheels[A, entity]:applyAngForce(ang(-FinalWheel * Clutch * GearV, 0, 0))
  107.             RotatingObject:applyAngForce(ang(0, (-FinalRotational * Clutch) / 4, 0))
  108.         }
  109.     }
  110. }
  111.  
  112. # Speed calculation and clutch control based on RPM and throttle input
  113. if (abs(-Wheels[1, entity]:angVel():pitch()) >= 1000 & StaticCalc == 0) {
  114.     StaticCalc = (SPEEDIN / abs(-Wheels[1, entity]:angVel():pitch()))
  115. }
  116.  
  117. WheelSpeed = StaticCalc * WAngVelocity
  118.  
  119. if (RPM < 400 | RAngVelocity <= 400) {
  120.     Clutch = 0
  121. } elseif (Calc >= 1) {
  122.     if (Throttle == 0) {
  123.         timer("Wait", 1200)
  124.     } else {
  125.         DefinedClutch = 1
  126.     }
  127. } elseif (Throttle > 0) {
  128.     DesiredRPM = 3900
  129.     Calc = RPM / DesiredRPM - 0.4
  130.     if (RPM >= 1800) {
  131.         DefinedClutch = clamp(Calc * 100, 0, 100) / 100
  132.         Clutch = clamp(Calc * 100, 0, 100) / 100
  133.     }
  134. } elseif (Gear != 0) {
  135.     DesiredIdleRPM = 2300
  136.     Calc = RPM / DesiredIdleRPM - 0.4
  137.     if (RPM >= 800) {
  138.         DefinedClutch = clamp(Calc * 100, 0, 100) / 100
  139.         Clutch = clamp(Calc * 100, 0, 100) / 100
  140.     }
  141. }
  142.  
  143. # Idle clutch control
  144. elseif (Clutchin == 0) {
  145.     Clutch = 0
  146. }
  147.  
  148. if (clk("Wait")) {
  149.     if (Throttle == 0) {
  150.         DefinedClutch = 1
  151.     }
  152. }
  153.  
  154. # Handle gear switching
  155. if (SwitchGear == 1) {
  156.     if (Gear != 0) {
  157.         if (Gear != 1 & Gear != -1) {
  158.             if (inrange(RotatingObject:angVel():yaw(), Calculation2 - 500, Calculation2 + 500)) {
  159.                 SwitchGear = 0
  160.                 Clutch = 1
  161.                 soundPlay(1, 1, "acf_extra/vehiclefx/trans/default_shift.wav")
  162.             }
  163.         } else {
  164.             Calc = 0
  165.             SwitchGear = 0
  166.             soundPlay(1, 1, "acf_extra/vehiclefx/trans/default_shift.wav")
  167.         }
  168.     } else {
  169.         Clutch = 0
  170.         SwitchGear = 0
  171.     }
  172. }
  173.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement