TheDieselPunk

hover

May 9th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.50 KB | None | 0 0
  1. --- This script allows the player to fly, as if they were in creative mode. Be warned, this isn't perfect, and lag may
  2. --- result in your death.
  3.  
  4. --- Firstly we want to ensure that we have a neural interface and wrap it.
  5. local modules = peripheral.find("neuralInterface")
  6. if not modules then
  7.     error("Must have a neural interface", 0)
  8. end
  9.  
  10. --- - We require a sensor and introspection module in order to gather information about the player
  11. --- - The sensor is used to determine where the ground is relative to the player, meaning we can slow the player
  12. ---   before they hit the floor.
  13. --- - The kinetic augment is (obviously) used to launch the player.
  14. if not modules.hasModule("plethora:sensor") then error("Must have a sensor", 0) end
  15. if not modules.hasModule("plethora:scanner") then error("Must have a scanner", 0) end
  16. if not modules.hasModule("plethora:introspection") then error("Must have an introspection module", 0) end
  17. if not modules.hasModule("plethora:kinetic", 0) then error("Must have a kinetic agument", 0) end
  18.  
  19. --- We run several loop at once, to ensure that various components do not delay each other.
  20. local meta = {}
  21. local hover = false
  22. parallel.waitForAny(
  23.     --- This loop just pulls user input. It handles a couple of function keys, as well as
  24.     --- setting the "hover" field to true/false.
  25.     function()
  26.         while true do
  27.             local event, key = os.pullEvent()
  28.             if event == "key" and key == keys.o then
  29.                 -- The O key launches you high into the air.
  30.                 modules.launch(0, -90, 3)
  31.             elseif event == "key" and key == keys.p then
  32.                 -- The P key launches you a little into the air.
  33.                 modules.launch(0, -90, 1)
  34.             elseif event == "key" and key == keys.l then
  35.                 -- The l key launches you in whatever direction you are looking.
  36.                 modules.launch(meta.yaw, meta.pitch, 3)
  37.             elseif event == "key" and key == keys.k then
  38.                 -- Holding the K key enables "hover" mode. We disable it when it is released.
  39.                 if not hover then
  40.                     hover = true
  41.                     os.queueEvent("hover")
  42.                 end
  43.             elseif event == "key_up" and key == keys.k then
  44.                 hover = false
  45.             end
  46.         end
  47.     end,
  48.     --- Continuously update the metadata. We do this in a separate loop to ensure this doesn't delay
  49.     --- other functions
  50.     function()
  51.         while true do
  52.             meta = modules.getMetaOwner()
  53.         end
  54.     end,
  55.     --- If we are hovering then attempt to catapult us back into air, with sufficient velocity to
  56.     --- just counteract gravity.
  57.     function()
  58.         while true do
  59.             if hover then
  60.                 -- We calculate the required motion we need to take
  61.                 local mY = meta.motionY
  62.                 mY = (mY - 0.138) / 0.8
  63.  
  64.                 -- If it is sufficiently large then we fire ourselves in that direction.
  65.                 if mY > 0.5 or mY < 0 then
  66.                     local sign = 1
  67.                     if mY < 0 then sign = -1 end
  68.                     modules.launch(0, 90 * sign, math.min(4, math.abs(mY)))
  69.                 else
  70.                     sleep(0)
  71.                 end
  72.             else
  73.                 os.pullEvent("hover")
  74.             end
  75.         end
  76.     end,
  77.     --- If we can detect a block below us, and we're falling sufficiently fast, then attempt to slow our fall. This
  78.     ---needs to react as fast as possible, so we can't call many peripheral methods here.
  79.     function()
  80.         while true do
  81.             local blocks = modules.scan()
  82.             for y = 0, -8, -1 do
  83.                 -- Scan from the current block downwards
  84.                 local block = blocks[1 + (8 + (8 + y)*17 + 8*17^2)]
  85.                 if block.name ~= "minecraft:air" then
  86.                     if meta.motionY < -0.3 then
  87.                         -- If we're moving slowly, then launch ourselves up
  88.                         modules.launch(0, -90, math.min(4, meta.motionY / -0.5))
  89.                     end
  90.                     break
  91.                 end
  92.             end
  93.         end
  94.     end
  95. )
Add Comment
Please, Sign In to add comment