Advertisement
9551

Untitled

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