IsCactus

Crane Controls

Aug 9th, 2025 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.42 KB | Gaming | 0 0
  1. local wires = {
  2.     POWER_OFF = colours.black,
  3.     REVERSE = colours.white,
  4.     SPEED_DOUBLE = colours.red,
  5.     SPEED_HALF = colours.cyan,
  6.     TRANSFER_SIDEWAYS = colours.pink,
  7.     TRANSFER_PULLEY = colours.lightBlue,
  8.     GRAB = colours.magenta
  9. }
  10. local speeds = {
  11.     HALF = { wires.SPEED_HALF, "1x" },
  12.     SINGLE = { 0, "2x" },
  13.     DOUBLE = { wires.SPEED_DOUBLE, "4x" },
  14. }
  15. local actions = {
  16.     NONE = { colours.combine(wires.POWER_OFF), "None" },
  17.     FORWARD = { 0, "Forward" },
  18.     BACKWARD = { colours.combine(wires.REVERSE), "Backward" },
  19.     LEFT = { colours.combine(wires.TRANSFER_SIDEWAYS), "Left" },
  20.     RIGHT = { colours.combine(wires.TRANSFER_SIDEWAYS, wires.REVERSE), "Right" },
  21.     UP = { colours.combine(wires.TRANSFER_SIDEWAYS, wires.TRANSFER_PULLEY), "Up" },
  22.     DOWN = { colours.combine(wires.TRANSFER_SIDEWAYS, wires.TRANSFER_PULLEY, wires.REVERSE), "Down" },
  23.     GRAB = { colours.combine(wires.TRANSFER_SIDEWAYS, wires.TRANSFER_PULLEY, wires.GRAB), "Grab" }
  24. }
  25.  
  26. local grabbing = false
  27. local side = "left"
  28. local speed = speeds.SINGLE
  29. local action = actions.NONE
  30. local resetTimer = 0
  31.  
  32. local function setAction(newAction, newSpeed)
  33.     if (action ~= newAction or speed ~= newSpeed) then
  34.         action = newAction
  35.         speed = newSpeed
  36.         local output = colours.combine(action[1], speed[1])
  37.         if (grabbing) then
  38.             output = colours.combine(output, wires.GRAB)
  39.         end
  40.         redstone.setBundledOutput(side, output)
  41.     end
  42. end
  43.  
  44. local function grab()
  45.     grabbing = true
  46.     resetTimer = 10
  47.     setAction(actions.GRAB, speed)
  48. end
  49.  
  50. local function controls()
  51.     while (true) do
  52.         local eventData = {os.pullEvent()}
  53.         local event = eventData[1]
  54.        
  55.         if (event == "key" and not eventData[3]) then
  56.             local key = eventData[2]       
  57.                 if (key == keys.w or key == keys.up)    then setAction(actions.FORWARD, speed)
  58.             elseif (key == keys.s or key == keys.down)  then setAction(actions.BACKWARD, speed)
  59.             elseif (key == keys.a or key == keys.left)  then setAction(actions.LEFT, speed)
  60.             elseif (key == keys.d or key == keys.right) then setAction(actions.RIGHT, speed)
  61.             elseif (key == keys.space)                  then setAction(actions.UP, speed)
  62.             elseif (key == keys.leftShift)              then setAction(actions.DOWN, speed)
  63.             elseif (key == keys.one)                    then setAction(action, speeds.HALF)
  64.             elseif (key == keys.two)                    then setAction(action, speeds.SINGLE)
  65.             elseif (key == keys.three)                  then setAction(action, speeds.DOUBLE)
  66.             elseif (key == keys.g)                      then grab()
  67.             end
  68.         elseif (event == "key_up") then
  69.             local key = eventData[2]
  70.             if ((action == actions.FORWARD and (key == keys.w or key == keys.up)) or
  71.                 (action == actions.BACKWARD and (key == keys.s or key == keys.down)) or
  72.                 (action == actions.LEFT and (key == keys.a or key == keys.left)) or
  73.                 (action == actions.RIGHT and (key == keys.d or key == keys.right)) or
  74.                 (action == actions.UP and (key == keys.space)) or
  75.                 (action == actions.DOWN and (key == keys.leftShift))) then
  76.                 setAction(actions.NONE, speed)
  77.             end
  78.         end
  79.     end
  80. end
  81.  
  82. local function status()
  83.     term.clear()
  84.     while (true) do
  85.         term.setCursorPos(1, 1)
  86.         term.clearLine()
  87.         print(action[2] .. " [" .. speed[2] .. "]\n")
  88.         print(grabbing and "Attached" or "Detached")
  89.        
  90.         if (grabbing) then
  91.             resetTimer = resetTimer - 1
  92.             if (resetTimer <= 0) then
  93.                 resetTimer = 0
  94.                 grabbing = false
  95.             end
  96.         end
  97.        
  98.         sleep(0.1)
  99.     end
  100. end
  101.  
  102. parallel.waitForAll(controls, status)
Advertisement
Add Comment
Please, Sign In to add comment