Dimasw

temp

Jun 21st, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.28 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.     ---
  26.     --- We recommend running [with the keyboard in your neural interface](../items/keyboard.html#using-with-the-neural-interface),
  27.     --- as this allows you to navigate without having the interface open.
  28.     function()
  29.         while true do
  30.             local event, key = os.pullEvent()
  31.             if event == "key" and key == keys.o then
  32.                 -- The O key launches you high into the air.
  33.                 modules.launch(0, -90, 3)
  34.             elseif event == "key" and key == keys.p then
  35.                 -- The P key launches you a little into the air.
  36.                 modules.launch(0, -90, 1)
  37.             elseif event == "key" and key == keys.l then
  38.                 -- The l key launches you in whatever direction you are looking.
  39.                 modules.launch(meta.yaw, meta.pitch, 3)
  40.             elseif event == "key" and key == keys.k then
  41.                 -- Holding the K key enables "hover" mode. We disable it when it is released.
  42.                 if not hover then
  43.                     hover = true
  44.                     os.queueEvent("hover")
  45.                 end
  46.             elseif event == "key_up" and key == keys.k then
  47.                 hover = false
  48.             end
  49.         end
  50.     end,
  51.     --- Continuously update the metadata. We do this in a separate loop to ensure this doesn't delay
  52.     --- other functions
  53.     function()
  54.         while true do
  55.             meta = modules.getMetaOwner()
  56.         end
  57.     end,
  58.     --- If we are hovering then attempt to catapult us back into air, with sufficient velocity to
  59.     --- just counteract gravity.
  60.     function()
  61.         while true do
  62.             if hover then
  63.                 -- We calculate the required motion we need to take
  64.                 local mY = meta.motionY
  65.                 mY = (mY - 0.138) / 0.8
  66.  
  67.                 -- If it is sufficiently large then we fire ourselves in that direction.
  68.                 if mY > 0.5 or mY < 0 then
  69.                     local sign = 1
  70.                     if mY < 0 then sign = -1 end
  71.                     modules.launch(0, 90 * sign, math.min(4, math.abs(mY)))
  72.                 else
  73.                     sleep(0)
  74.                 end
  75.             else
  76.                 os.pullEvent("hover")
  77.             end
  78.         end
  79.     end,
  80.     --- If we can detect a block below us, and we're falling sufficiently fast, then attempt to slow our fall. This
  81.     ---needs to react as fast as possible, so we can't call many peripheral methods here.
  82.     function()
  83.         while true do
  84.             local blocks = modules.scan()
  85.             for y = 0, -8, -1 do
  86.                 -- Scan from the current block downwards
  87.                 local block = blocks[1 + (8 + (8 + y)*17 + 8*17^2)]
  88.                 if block.name ~= "minecraft:air" then
  89.                     if meta.motionY < -0.3 then
  90.                         -- If we're moving slowly, then launch ourselves up
  91.                         modules.launch(0, -90, math.min(4, meta.motionY / -0.5))
  92.                     end
  93.                     break
  94.                 end
  95.             end
  96.         end
  97.     end
  98. )
Add Comment
Please, Sign In to add comment