MegiRicky

StrykerDriveGen3.lua

Jul 22nd, 2024 (edited)
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 KB | None | 0 0
  1. local left = peripheral.wrap("left")
  2. local right = peripheral.wrap("right")
  3. local monitor = peripheral.find("monitor")
  4. local controller = peripheral.find("tweaked_controller")
  5. local router = peripheral.find("redrouter")
  6. local modem = peripheral.wrap("bottom")
  7.  
  8. local function round(num)
  9. return math.floor(num * 100 + 0.5) / 100
  10. end
  11.  
  12. local leftSpeed = 0
  13. local rightSpeed = 0
  14. local turn = 0
  15. local debounceTime = 0.2 -- debounce time in seconds
  16. local lastToggleTime = 0 -- last time the button was toggled
  17. local cannonPitch = "stop"
  18. local cannonYaw = "stop"
  19.  
  20. local cannonPitchCh = 0
  21. local cannonYawCh = 1
  22.  
  23. if modem then
  24. modem.open(cannonPitchCh)
  25. modem.open(cannonYawCh)
  26. end
  27.  
  28.  
  29. redstone.setOutput("front", true)
  30. router.setOutput("left", true)
  31. redstone.setOutput("bottom",true)
  32. local function handleController()
  33. while true do
  34. local yAxis = controller.getAxis(2)
  35. local xAxis = controller.getAxis(1)
  36.  
  37. if yAxis < 0 then
  38. if leftSpeed < 0 or rightSpeed < 0 then
  39. leftSpeed = leftSpeed + 32
  40. rightSpeed = rightSpeed + 32
  41. else
  42. leftSpeed = leftSpeed + 8
  43. rightSpeed = rightSpeed + 8
  44. end
  45. elseif yAxis > 0 then
  46. if leftSpeed > 0 or rightSpeed > 0 then
  47. leftSpeed = leftSpeed - 32
  48. rightSpeed = rightSpeed - 32
  49. else
  50. leftSpeed = leftSpeed -8
  51. rightSpeed = rightSpeed -8
  52. end
  53. else
  54. if leftSpeed > 0 and rightSpeed > 0 then
  55. leftSpeed = leftSpeed - 8
  56. rightSpeed = rightSpeed - 8
  57. elseif leftSpeed < 0 and rightSpeed < 0 then
  58. leftSpeed = leftSpeed + 8
  59. rightSpeed = rightSpeed + 8
  60. end
  61. if math.abs(leftSpeed) < 16 then
  62. leftSpeed = 0
  63. rightSpeed = 0
  64. end
  65. end
  66.  
  67. if leftSpeed > 256 then
  68. leftSpeed = 256
  69. elseif leftSpeed < -256 then
  70. leftSpeed = -256
  71. end
  72. if rightSpeed > 256 then
  73. rightSpeed = 256
  74. elseif rightSpeed < -256 then
  75. rightSpeed = -256
  76. end
  77.  
  78. if xAxis > 0 then
  79. turn = 1
  80. elseif xAxis < 0 then
  81. turn = -1
  82. else
  83. turn = 0
  84. end
  85.  
  86. if xAxis > 0 and math.abs(leftSpeed) < 64 and math.abs(rightSpeed) < 64 then
  87. turn = 2
  88. elseif xAxis < 0 and math.abs(leftSpeed) < 64 and math.abs(rightSpeed) < 64 then
  89. turn = -2
  90. end
  91.  
  92. if controller.getButton(7) then
  93. monitor.setCursorPos(1,5)
  94. monitor.write("Smoke")
  95. redstone.setOutput("bottom",false)
  96. sleep(0.2)
  97. else
  98. redstone.setOutput("bottom",true)
  99. end
  100.  
  101. -- Debounce mechanism for button 8
  102. if controller.getButton(8) then
  103. local currentTime = os.clock()
  104. if currentTime - lastToggleTime > debounceTime then
  105. if router.getOutput("bottom") then
  106. router.setOutput("bottom", false)
  107. else
  108. router.setOutput("bottom", true)
  109. end
  110. lastToggleTime = currentTime
  111. end
  112. end
  113.  
  114. sleep(0.05)
  115. end
  116. end
  117.  
  118. local function setSpeed()
  119. while true do
  120. local currentLeft = left.getTargetSpeed()
  121. local currentRight = right.getTargetSpeed()
  122.  
  123. if turn == 0 then
  124. if leftSpeed ~= currentLeft then
  125. left.setTargetSpeed(leftSpeed)
  126. end
  127. if rightSpeed ~= currentRight then
  128. right.setTargetSpeed(rightSpeed)
  129. end
  130. elseif turn == 1 then
  131. if true then
  132. left.setTargetSpeed(leftSpeed / 1)
  133. end
  134. if true then
  135. right.setTargetSpeed(rightSpeed / 2)
  136. end
  137. elseif turn == -1 then
  138. if true then
  139. left.setTargetSpeed(leftSpeed / 2)
  140. end
  141. if true then
  142. right.setTargetSpeed(rightSpeed / 1)
  143. end
  144. elseif turn == 2 then
  145. if currentLeft ~= 32 then
  146. left.setTargetSpeed(64)
  147. end
  148. if currentRight ~= -32 then
  149. right.setTargetSpeed(-64)
  150. end
  151. elseif turn == -2 then
  152. if currentLeft ~= -32 then
  153. left.setTargetSpeed(-64)
  154. end
  155. if currentRight ~= 32 then
  156. right.setTargetSpeed(64)
  157. end
  158. end
  159.  
  160. sleep(0.05)
  161. end
  162. end
  163.  
  164. local function gunControl()
  165. while true do
  166. if modem then
  167. if controller.getButton(12) then
  168. cannonPitch = "up"
  169. elseif controller.getButton(14) then
  170. cannonPitch = "down"
  171. else
  172. cannonPitch = "stop"
  173. end
  174.  
  175. if controller.getButton(13) then
  176. cannonYaw = "right"
  177. elseif controller.getButton(15) then
  178. cannonYaw = "left"
  179. else
  180. cannonYaw = "stop"
  181. end
  182. modem.transmit(cannonPitchCh, 0, cannonPitch)
  183. modem.transmit(cannonYawCh, 0, cannonYaw)
  184. end
  185. sleep()
  186. end
  187. end
  188.  
  189.  
  190. local prevSpeed = 0
  191. local prevEnergy = 0
  192. local prevLeftSpeed = 0
  193. local prevRightSpeed = 0
  194.  
  195. local function updateMonitor()
  196. while true do
  197. local velocity = ship.getVelocity()
  198. local speed = math.sqrt(velocity.x^2 + velocity.y^2 + velocity.z^2) * 3.6
  199. local currentLeft = left.getTargetSpeed()
  200. local currentRight = right.getTargetSpeed()
  201. local suspension = router.getOutput("bottom") and "Up" or "Down"
  202. if monitor then
  203. if speed ~= prevSpeed or currentLeft ~= prevLeftSpeed or currentRight ~= prevRightSpeed then
  204. monitor.setTextScale(0.5)
  205. monitor.clear()
  206. monitor.setCursorPos(1, 1)
  207. monitor.write("Speed:" .. round(speed) .. "km/h")
  208. monitor.setCursorPos(1, 2)
  209. monitor.write("Left RPM: " .. round(currentLeft))
  210. monitor.setCursorPos(1, 3)
  211. monitor.write("Right RPM: " .. round(currentRight))
  212. monitor.setCursorPos(1, 4)
  213. monitor.write("Suspension:" .. suspension)
  214. monitor.setCursorPos(1, 5)
  215. monitor.write("Pitch: "..cannonPitch)
  216. monitor.setCursorPos(1, 6)
  217. monitor.write("Yaw: "..cannonYaw)
  218.  
  219. prevSpeed = speed
  220. prevEnergy = energy
  221. prevLeftSpeed = currentLeft
  222. prevRightSpeed = currentRight
  223. end
  224. end
  225.  
  226. print("Speed:" .. speed .. "km/h")
  227. print("Left RPM: " .. round(currentLeft))
  228. print("Right RPM: " .. round(currentRight))
  229. print("set left rpm: " .. leftSpeed)
  230. print("set right rpm: " .. rightSpeed)
  231. print(turn)
  232.  
  233. sleep(0.05)
  234. end
  235. end
  236.  
  237. parallel.waitForAny(handleController, updateMonitor, setSpeed, gunControl)
  238.  
  239. --pastebin get EeTXQnHt strykerDriveGen3
Advertisement
Add Comment
Please, Sign In to add comment