Advertisement
9551

Untitled

Jun 9th, 2021
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.15 KB | None | 0 0
  1. ---- FLY MK2 ----
  2. -- fly system mk III
  3. -- created and tested by Helldragger, have fun with it!
  4.  
  5. local function printDebug(msg)
  6.     msg = msg .. "\n"
  7.     print(msg)
  8. end
  9.  
  10. -- NEURAL INTERFACE REQUIRED
  11. local modules = peripheral.find("neuralInterface")
  12. if not modules then
  13.     error("Must have a neural interface", 0)
  14. end
  15.  
  16. -- MODULES REQUIRED
  17. if not modules.hasModule("plethora:sensor") then
  18.     error("Must have a sensor", 0)
  19. end
  20. if not modules.hasModule("plethora:introspection") then
  21.     error("Must have an introspection module", 0)
  22. end
  23. if not modules.hasModule("plethora:kinetic", 0) then
  24.     error("Must have a kinetic agument", 0)
  25. end
  26. if not modules.hasModule("plethora:glasses") then
  27.     error("The overlay glasses are missing", 0)
  28. end
  29.  
  30. -- KILL SWITCH CONTROL
  31. local stop = false
  32.  
  33. -- PLAYER DATA CACHE
  34. local meta = modules.getMetaOwner()
  35.  
  36. local function refreshMeta()
  37.     os.pullEvent("refreshMeta")
  38.     if DEBUGCALLS then
  39.         printDebug("refresh meta")
  40.     end
  41.     meta = modules.getMetaOwner()
  42. end
  43.  
  44. -- CONTROLS
  45. local LIGHTSPEED = 4
  46. local FASTER = 2.5
  47. local FAST = 1
  48. local NORMAL = 0.2
  49. local SPEEDMODE = NORMAL
  50.  
  51. local MAX_THRUST = SPEEDMODE
  52. local MIN_THRUST = 0.1
  53. local THRUST_GRADIENT = 0.01
  54. local ACTUAL_THRUST = 0.15
  55.  
  56. local MAX_PITCH = 90
  57. local MIN_PITCH = -90
  58. local PITCH_GRADIENT = 45 / 9 --(MAX_PITCH - MIN_PITCH) / 10
  59. local ACTUAL_PITCH = 90 --((MAX_PITCH - MIN_PITCH) / 2)+MIN_PITCH
  60.  
  61. local fly = false
  62. local flyActivatedTime = -1
  63.  
  64. local upLastPressedTime = -1
  65. local downLastPressedTime = -1
  66. local frontLastPressedTime = -1
  67. local backLastPressedTime = -1
  68. local rightLastPressedTime = -1
  69. local leftLastPressedTime = -1
  70. local KEY_UP_THRESHOLD = 0.3 --sec
  71.  
  72. local down = false
  73. local up = false
  74. local front = false
  75. local back = false
  76. local right = false
  77. local left = false
  78.  
  79. local lastSpaceTime = -1
  80. local spacePressed = false
  81.  
  82. local FLYCALLSSINCELASTCONTROL = 1
  83.  
  84. local function addPitch(theta, delta)
  85.     theta = math.fmod(theta + delta, 360)
  86.     if theta < 0 then
  87.         theta = theta + 360
  88.     end
  89.     return theta
  90. end
  91.  
  92. local function controls()
  93.     local event, key, held = os.pullEvent("key")
  94.     FLYCALLSSINCELASTCONTROL = 0
  95.     down = (os.clock() - downLastPressedTime) < KEY_UP_THRESHOLD
  96.     up = (os.clock() - upLastPressedTime) < KEY_UP_THRESHOLD
  97.     front = (os.clock() - frontLastPressedTime) < KEY_UP_THRESHOLD
  98.     back = (os.clock() - backLastPressedTime) < KEY_UP_THRESHOLD
  99.     right = (os.clock() - rightLastPressedTime) < KEY_UP_THRESHOLD
  100.     left = (os.clock() - leftLastPressedTime) < KEY_UP_THRESHOLD
  101.  
  102.     if key == keys.k then
  103.         stop = true
  104.         print("K pressed, stopping program...")
  105.     elseif key == keys.space and not held then
  106.         local spaceTime = os.clock()
  107.         local diff = spaceTime - lastSpaceTime
  108.         if (diff < 0.5) then
  109.             fly = not fly
  110.             spaceTime = -1
  111.             if fly then
  112.                 print("FLY MODE ENABLED")
  113.                 flyActivatedTime = os.clock()
  114.                 os.queueEvent("fly")
  115.             else
  116.                 print("FLY MODE DISABLED")
  117.             end
  118.         end
  119.         lastSpaceTime = spaceTime
  120.     end
  121.  
  122.     -- FLIGHT RELATED
  123.     -- period (.) => speedup
  124.     if key == keys.period then
  125.         if SPEEDMODE == NORMAL then
  126.             SPEEDMODE = FAST
  127.             print("Speed mode set to FAST (warning: high altitude might lead to death by asphyxia)")
  128.         elseif SPEEDMODE == FAST then
  129.             SPEEDMODE = FASTER
  130.             print("Speed mode set to FASTER (WARNING: can reach deadly altitude VERY quick!)")
  131.         elseif SPEEDMODE == FASTER then
  132.             SPEEDMODE = LIGHTSPEED
  133.             print("Speed mode set to LIGHTSPEED (BIG WARNING: might reach deadly altitude in LESS than a few second!)")
  134.         else
  135.             print("Speed mode is already maximal (warning: high altitude might lead to death by asphyxia)")
  136.         end
  137.         MAX_THRUST = SPEEDMODE
  138.         THRUST_GRADIENT = (MAX_THRUST - MIN_THRUST) / 10
  139.     end
  140.     -- comma (,) => slowdown
  141.     if key == keys.comma then
  142.         if SPEEDMODE == LIGHTSPEED then
  143.             SPEEDMODE = FASTER
  144.             print("Speed mode set to FASTER (WARNING: can reach deadly altitude VERY quick!)")
  145.         elseif SPEEDMODE == FASTER then
  146.             SPEEDMODE = FAST
  147.             print("Speed mode set to FAST (warning: high altitude might lead to death by asphyxia)")
  148.         elseif SPEEDMODE == FAST then
  149.             SPEEDMODE = NORMAL
  150.             print("Speed mode set to NORMAL")
  151.         else
  152.             print("Speed mode is already minimal")
  153.         end
  154.         MAX_THRUST = SPEEDMODE
  155.         if ACTUAL_THRUST > MAX_THRUST then
  156.             ACTUAL_THRUST = MAX_THRUST
  157.         end
  158.         THRUST_GRADIENT = (MAX_THRUST - MIN_THRUST) / 10
  159.     end
  160.     -- shift => descente
  161.     if key == keys.leftShift then
  162.         down = true
  163.         downLastPressedTime = os.clock()
  164.         if fly then
  165.             ACTUAL_THRUST = ACTUAL_THRUST - THRUST_GRADIENT
  166.             if ACTUAL_THRUST < MIN_THRUST then
  167.                 ACTUAL_THRUST = MIN_THRUST
  168.             end
  169.         end
  170.     end
  171.     -- space => montée
  172.     if key == keys.space then
  173.         up = true
  174.         upLastPressedTime = os.clock()
  175.         if fly then
  176.             ACTUAL_THRUST = ACTUAL_THRUST + THRUST_GRADIENT
  177.             if ACTUAL_THRUST > MAX_THRUST then
  178.                 ACTUAL_THRUST = MAX_THRUST
  179.             end
  180.         end
  181.     end
  182.     -- W => en avant
  183.     if key == keys.w then
  184.         front = true
  185.         frontLastPressedTime = os.clock()
  186.         if fly then
  187.             ACTUAL_PITCH = addPitch(ACTUAL_PITCH, PITCH_GRADIENT)
  188.         end
  189.     end
  190.     -- S => en arrière
  191.     if key == keys.s then
  192.         back = true
  193.         backLastPressedTime = os.clock()
  194.         if fly then
  195.             ACTUAL_PITCH = addPitch(ACTUAL_PITCH, -PITCH_GRADIENT)
  196.         end
  197.     end
  198.     -- A => à gauche
  199.     if key == keys.a then
  200.         left = true
  201.         leftLastPressedTime = os.clock()
  202.     end
  203.     -- D => à droite
  204.     if key == keys.d then
  205.         right = true
  206.         rightLastPressedTime = os.clock()
  207.     end
  208.     -- on refresh nos données
  209.     os.queueEvent("refreshMeta")
  210. end
  211.  
  212. local function flyMode()
  213.     os.pullEvent("fly")
  214.     -- APPLY
  215.     if fly then
  216.         FLYCALLSSINCELASTCONTROL = FLYCALLSSINCELASTCONTROL + 1
  217.         -- we shift the pitch in order to get up at 90 degrees and 0 at horizontal.
  218.         modules.launch(meta.yaw, math.fmod(-ACTUAL_PITCH, 360), ACTUAL_THRUST)
  219.         os.queueEvent("fly")
  220.     end
  221. end
  222.  
  223. local function getOrientation(pitch)
  224.     --                 ^ 90
  225.     --                 |
  226.     --                 |
  227.     -- 180 *-----------|------------> 0
  228.     --                 |
  229.     --                 |
  230.     --                 * 270
  231.  
  232.     if (pitch >= 0) then
  233.         if (pitch < 45 or pitch >= 315) then
  234.             return "front"
  235.         elseif (pitch < 135) then
  236.             return "up"
  237.         elseif (pitch < 225) then
  238.             return "back"
  239.         else
  240.             return "down"
  241.         end
  242.     end
  243. end
  244.  
  245. -- Get hold of the canvas
  246. local interface = peripheral.wrap("back")
  247. local canvas = interface.canvas()
  248. canvas.clear()
  249. -- And add a rectangle
  250.  
  251. local function round(value)
  252.     return math.floor(value * 100) / 100
  253. end
  254. local function calcTotalSpeed()
  255.     return (meta.motionX ^ 2 + meta.motionY ^ 2 + meta.motionZ ^ 2) ^ 0.5
  256. end
  257.  
  258. local function getSpeedMode()
  259.     if SPEEDMODE == LIGHTSPEED then
  260.         return "LIGHTSPEED"
  261.     elseif SPEEDMODE == FASTER then
  262.         return "FASTER"
  263.     elseif SPEEDMODE == FAST then
  264.         return "FAST"
  265.     else
  266.         return "NORMAL"
  267.     end
  268. end
  269.  
  270. local speedgroup = canvas.addGroup({10, 0})
  271.  
  272. speedgroup.addText({10, 10}, "Vertical")
  273. speedgroup.addText({10, 30}, "Total")
  274. speedgroup.addText({10, 50}, "Speed mode")
  275. speedgroup.addText({10, 70}, "Thrust")
  276. speedgroup.addText({10, 90}, "Pitch")
  277.  
  278. local YSpeed = speedgroup.addText({10, 20}, round(meta.motionY) .. "m/s")
  279. local totalSpeed = speedgroup.addText({10, 40}, round(calcTotalSpeed()) .. "m/s")
  280. local speedMode = speedgroup.addText({10, 60}, getSpeedMode())
  281. local ThrustSpeed = speedgroup.addText({10, 80}, (round(ACTUAL_THRUST) * 100) .. "%")
  282. local PitchSpeed =
  283.     speedgroup.addText({10, 100}, round(ACTUAL_PITCH) .. "degrees (" .. getOrientation(ACTUAL_PITCH) .. ")")
  284.  
  285. local function overlay()
  286.     YSpeed.setText(round(meta.motionY) .. "m/s")
  287.     speedMode.setText(getSpeedMode())
  288.     totalSpeed.setText(round(calcTotalSpeed()) .. "m/s")
  289.     ThrustSpeed.setText((round(ACTUAL_THRUST) * 100) .. "%")
  290.     PitchSpeed.setText(round(ACTUAL_PITCH) .. "degrees (" .. getOrientation(ACTUAL_PITCH) .. ")")
  291. end
  292.  
  293. local function untilKill(func, doesYield)
  294.     while not stop do
  295.         if doesYield then
  296.             coroutine.yield()
  297.         end
  298.         func()
  299.     end
  300. end
  301.  
  302. -- MAIN LOOP
  303. print("FLY BALANCER program started, press K to stop")
  304.  
  305. parallel.waitForAny(
  306.     function()
  307.         untilKill(refreshMeta, false)
  308.     end,
  309.     function()
  310.         untilKill(controls, false)
  311.     end,
  312.     function()
  313.         untilKill(overlay, true)
  314.     end,
  315.     function()
  316.         untilKill(flyMode, false)
  317.     end
  318. )
  319.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement