MegiRicky

CC:VS FBW

Jul 21st, 2024 (edited)
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.29 KB | None | 0 0
  1. local modem = peripheral.wrap("left")
  2. local monitor = peripheral.find("monitor")
  3. local controller = peripheral.find("tweaked_controller")
  4. local arController = peripheral.find("arController")
  5.  
  6. local throttleChannel = 400
  7. local rudderChannel = 401
  8. local elevatorChannel = 402
  9. local aileronChannel = 403
  10. local landingGearChannel = 404
  11. local flapChannel = 405
  12. local startX,startY,endX,endY = 50,2,100,70
  13. if modem then
  14. modem.open(throttleChannel)
  15. modem.open(rudderChannel)
  16. modem.open(elevatorChannel)
  17. modem.open(aileronChannel)
  18. modem.open(landingGearChannel)
  19. modem.open(flapChannel)
  20. end
  21.  
  22. local throttle = 0
  23. local speed = 0
  24. local landingGear = "down" -- Correctly initialize landingGear as a string
  25. local flap = 0
  26. local pos = { x = 0, y = 0, z = 0 }
  27. local rudderControl = " "
  28. local aileronControl = "0"
  29. local elevatorControl = "0"
  30. print("Input roll and pitch mode 1/2: ")
  31. local mode = tonumber(io.read())
  32.  
  33. local debounceTime = 0.2 -- debounce time in seconds
  34. local lastToggleTime = 0 -- last time the landing gear button was toggled
  35. local lastFlapTime = 0 -- last time the flap button was toggled
  36.  
  37. local prevDisplayText = {}
  38. arController.clear()
  39. arController.setRelativeMode(true, 1920, 1080)
  40.  
  41.  
  42. local function round(num)
  43. return math.floor(num * 100 + 0.5) / 100
  44. end
  45. local function toDegrees(radians)
  46. return radians * (180 / math.pi)
  47. end
  48. local function handleController()
  49. while true do
  50. if controller then
  51. controller.setFullPrecision(true)
  52. local yAxis = controller.getAxis(2)
  53. local xAxis = controller.getAxis(1)
  54.  
  55. throttle = -controller.getAxis(2) * 256
  56.  
  57. if throttle > 256 then
  58. throttle = 256
  59. elseif throttle < -128 then
  60. throttle = -128
  61. end
  62.  
  63. if xAxis > 0.5 then
  64. rudderControl = "left"
  65. elseif xAxis < -0.5 then
  66. rudderControl = "right"
  67. else
  68. rudderControl = "stop"
  69. end
  70.  
  71. if controller.getAxis(4) > 0.2 then
  72. elevatorControl = -math.floor(controller.getAxis(4) * 15)
  73. elseif controller.getAxis(4) < -0.2 then
  74. elevatorControl = -math.floor(controller.getAxis(4) * 15)
  75. else
  76. elevatorControl = 0
  77. end
  78.  
  79. if controller.getAxis(3) > 0.2 then
  80. aileronControl = -math.floor(controller.getAxis(3) * 15)
  81. elseif controller.getAxis(3) < -0.2 then
  82. aileronControl = -math.floor(controller.getAxis(3) * 15)
  83. else
  84. aileronControl = 0
  85. end
  86.  
  87. -- Debounce mechanism for button 7 (landing gear)
  88. if controller.getButton(3) then
  89. local currentTime = os.clock()
  90. if currentTime - lastToggleTime > debounceTime then
  91. if landingGear == "up" then
  92. landingGear = "down"
  93. else
  94. landingGear = "up"
  95. end
  96. lastToggleTime = currentTime
  97. end
  98. end
  99.  
  100. -- Debounce mechanism for flap control buttons
  101. local currentTime = os.clock()
  102. if controller.getButton(1) and currentTime - lastFlapTime > debounceTime then
  103. flap = flap - 1
  104. if flap < 0 then
  105. flap = 0
  106. end
  107. lastFlapTime = currentTime
  108. elseif controller.getButton(2) and currentTime - lastFlapTime > debounceTime then
  109. flap = flap + 1
  110. if flap > 15 then
  111. flap = 15
  112. end
  113. lastFlapTime = currentTime
  114. end
  115. end
  116. sleep(0.05)
  117. end
  118. end
  119.  
  120. local function setSpeed()
  121. while true do
  122. if modem then
  123. modem.transmit(throttleChannel, 0, -throttle)
  124. modem.transmit(rudderChannel, 0, rudderControl)
  125. modem.transmit(elevatorChannel, 0, elevatorControl)
  126. modem.transmit(aileronChannel, 0, aileronControl)
  127. modem.transmit(landingGearChannel, 0, landingGear) -- Transmit landing gear state
  128. modem.transmit(flapChannel, 0, flap)
  129. end
  130. sleep(0.05)
  131. end
  132. end
  133.  
  134. local function clearLine(monitor, x, y, length)
  135. if monitor then
  136. monitor.setCursorPos(x, y)
  137. monitor.write(string.rep(" ", length))
  138. end
  139. end
  140.  
  141. local function updateDisplay()
  142. local velocity = { x = 0, y = 0, z = 0 }
  143. if ship then
  144. velocity = ship.getVelocity()
  145. pos = ship.getWorldspacePosition()
  146. pitch = toDegrees(ship.getRoll())
  147. roll = toDegrees(ship.getPitch())
  148. end
  149. speed = math.sqrt(velocity.x^2 + velocity.y^2 + velocity.z^2) * 1.9438452
  150. local booster = " "
  151. if throttle > 128 then
  152. booster = "Jet Engaged"
  153. end
  154. local displayText = {
  155. "Speed: " .. math.floor(speed) .. " knots",
  156. "Altitude: " .. math.floor(pos.y) .. " blocks",
  157. "Throttle: " .. throttle .. " RPM "..booster,
  158. "Rudder: " .. rudderControl,
  159. "Elevator: " .. elevatorControl,
  160. "Aileron: " .. aileronControl,
  161. "L Gear: " .. landingGear,
  162. "Pitch: "..pitch,
  163. "Roll: "..roll,
  164. flap == 0 and "Flap: off" or flap == 15 and "Flap: full" or "Flap: " .. flap
  165. }
  166.  
  167.  
  168. if arController then
  169. for i, line in ipairs(displayText) do
  170. if prevDisplayText[i] ~= line then
  171. arController.clearElement(tostring(i))
  172. arController.drawStringWithId(tostring(i), line, 1, i * 10 + 60, 0xFFFFFF)
  173. end
  174. end
  175. end
  176.  
  177. local pitch, roll = 0, 0
  178.  
  179. prevDisplayText = displayText
  180. end
  181.  
  182. local function updateMonitor()
  183. while true do
  184. updateDisplay()
  185. sleep(0.01)
  186. end
  187. end
  188.  
  189. arController.fill(startX,startY,endX,endY,0x000000)
  190. local midY = startY + math.floor(endY - startY / 2)
  191. local adjacent = (endY - startY) / 2
  192. -- ... (previous code remains the same)
  193.  
  194. -- ... (previous code remains the same)
  195.  
  196. local function altitudeIndicatorQuarter(startX, endX, startY, endY, midX, midY, adjacent, pitch, roll)
  197. local slope = math.tan(roll)
  198.  
  199. for x = startX, endX, 4 do
  200. arController.clearElement("horizon"..x)
  201.  
  202. -- Calculate the y-coordinate of the horizon line based on the slope and pitch
  203. local lineY = midY - (x - midX) * slope + adjacent * math.tan(pitch)
  204.  
  205. arController.drawStringWithId("horizon"..x, ".", x, lineY, 0xFFFFFF)
  206. end
  207. end
  208.  
  209. local function altitudeIndicator()
  210. while true do
  211. local pitch, roll = 0, 0
  212. if ship then
  213. if mode == 1 then
  214. pitch = - toDegrees(ship.getRoll())
  215. roll = toDegrees(ship.getPitch())
  216. yaw = toDegrees(ship.getYaw()) - 90
  217. if yaw < 0 then yaw = yaw + 360 end
  218. print("mode1")
  219. else
  220. pitch = toDegrees(ship.getPitch())
  221. roll = toDegrees(ship.getRoll())
  222. yaw = toDegrees(ship.getYaw()) - 90
  223. if yaw < 0 then yaw = yaw + 360 end
  224. print("mode2")
  225. end
  226. end
  227.  
  228. local width, height = endX - startX + 1, endY - startY
  229. local midY = startY + math.floor(height / 2)
  230. local midX = (startX + endX) / 2
  231.  
  232. -- Clear previous elements
  233. arController.clearElement("sky")
  234. arController.clearElement("ground")
  235.  
  236. pitch = math.rad(pitch)
  237. roll = - math.rad(roll)
  238.  
  239. -- Generate the horizon line from four quarters simultaneously
  240. local quarterWidth = math.floor((endX - startX + 1) / 4)
  241. parallel.waitForAll(
  242. function()
  243. altitudeIndicatorQuarter(startX, startX + quarterWidth, startY, endY, midX, midY, adjacent, pitch, roll)
  244. end,
  245. function()
  246. altitudeIndicatorQuarter(startX + quarterWidth, startX + 2 * quarterWidth, startY, endY, midX, midY, adjacent, pitch, roll)
  247. end,
  248. function()
  249. altitudeIndicatorQuarter(startX + 2 * quarterWidth, startX + 3 * quarterWidth, startY, endY, midX, midY, adjacent, pitch, roll)
  250. end,
  251. function()
  252. altitudeIndicatorQuarter(startX + 3 * quarterWidth, endX, startY, endY, midX, midY, adjacent, pitch, roll)
  253. end
  254. )
  255.  
  256. arController.clearElement("hLine")
  257. arController.clearElement("vLine")
  258. if mode == 1 then
  259. pitchStr = tostring(math.floor(- toDegrees(ship.getRoll())))
  260. else
  261. pitchStr = tostring(math.floor( toDegrees(ship.getPitch())))
  262. end
  263. local pitchStrWidth = string.len(pitchStr)
  264. local lineLength = 10
  265. local spacing = 2
  266.  
  267. -- Clear previous elements
  268. arController.clearElement("hLineLeft")
  269. arController.clearElement("hLineRight")
  270. arController.clearElement("pitchValue")
  271. arController.clearElement("yaw")
  272.  
  273. -- Draw horizontal lines on both sides
  274. arController.horizontalLineWithId("hLineLeft", midX - lineLength - spacing - pitchStrWidth - 2, midX - spacing - pitchStrWidth - 2, midY + 5, 0xFFFFFF)
  275. arController.horizontalLineWithId("hLineRight", midX + spacing + pitchStrWidth, midX + lineLength + spacing + pitchStrWidth, midY + 5, 0xFFFFFF)
  276.  
  277. -- Draw the pitch value in the middle
  278. arController.drawCenteredStringWithId("pitchValue", pitchStr, midX, midY + 1, 0xFFFFFF)
  279.  
  280. arController.verticalLineWithId("vLine", midX - 1, startY + 10, midY - 5, 0xFFFFFF)
  281. if mode == 1 then
  282. arController.drawCenteredStringWithId("roll", tostring(math.floor(math.abs(toDegrees(ship.getPitch())))),midX, startY + 1, 0xFFFFFF)
  283. else
  284. arController.drawCenteredStringWithId("roll", tostring(math.floor(math.abs(toDegrees(ship.getRoll())))),midX, startY + 1, 0xFFFFFF)
  285. end
  286. arController.drawCenteredStringWithId("yaw", tostring(math.floor(yaw)), midX, endY + 1, 0xFFFFFF)
  287.  
  288. sleep(0.001) -- Adjust the update frequency as needed
  289. end
  290. end
  291.  
  292. -- ... (remaining code remains the same)
  293.  
  294. parallel.waitForAny(handleController, updateMonitor, setSpeed, altitudeIndicator)
  295.  
  296.  
  297.  
  298. --[[ Mouse and keyboard
  299. if yAxis < 0 then
  300. throttle = throttle + 8
  301. elseif yAxis > 0 then
  302. throttle = throttle - 8
  303. end
  304.  
  305. if throttle > 256 then
  306. throttle = 256
  307. elseif throttle < -128 then
  308. throttle = -128
  309. end
  310.  
  311. if xAxis > 0 then
  312. rudderControl = "right"
  313. elseif xAxis < 0 then
  314. rudderControl = "left"
  315. else
  316. rudderControl = "stop"
  317. end
  318.  
  319. if controller.getButton(12) then
  320. elevatorControl = "down"
  321. elseif controller.getButton(14) then
  322. elevatorControl = "up"
  323. else
  324. elevatorControl = "stop"
  325. end
  326.  
  327. if controller.getButton(13) then
  328. aileronControl = "right"
  329. elseif controller.getButton(15) then
  330. aileronControl = "left"
  331. else
  332. aileronControl = "stop"
  333. end]]
Advertisement
Add Comment
Please, Sign In to add comment