robocyclone

endoflame controller.lua

Apr 7th, 2023
830
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.04 KB | None | 0 0
  1. --[[User Config]]
  2. local selfName = "Endoflame Controller" --Name to display at top of runtime
  3. local fuelBurnTime = 360 --How long to close output signal
  4. local fuelDropTime = 6.8 --How long to send output redstone signal
  5. local redstoneCompareSide = "right" --Which side to input redstone override to
  6. local redstoneOutputSide = "back" --Which side to output redstone control to
  7. local masterSwitch = true --True starts the machine in the ON state, false starts in the OFF state
  8. --[[]]
  9.  
  10. local switch = true --False = currently dropping items, true = not dropping items
  11. local masterDict = {
  12.     [true] = "Controller running.",
  13.     [false] = "Controller paused. Manual control still available."
  14. }
  15. local dict = {
  16.     [true] = "Burning fuel..",
  17.     [false] = "Dropping fuel..",
  18.     ["mana"] = "Mana full. Sleeping..."
  19. }
  20. local manaFull = rs.getInput(redstoneCompareSide)
  21. local burnTimer
  22. local dropTimer
  23.  
  24. local function checkRedstoneState() --Used to verify redstone signal change
  25.     local newState = rs.getInput(redstoneCompareSide)
  26.     if newState ~= manaFull then --If the state has changed
  27.         manaFull = newState
  28.         return true
  29.     end
  30. end
  31.  
  32. local function draw(arg) --Handles updating screen during runtime
  33.     if arg ~= nil then arg = switch end
  34.     term.clear()
  35.     term.setCursorPos(1,1)
  36.     write(selfName)
  37.     write("\n\n" .. masterDict[masterSwitch])
  38.     write("\n\n" .. dict[arg])
  39.     write("\n\n\nPress spacebar to drop fuel.\n\nPress T to toggle the controller on or off.")
  40. end
  41.  
  42. local function dropManual()
  43.     switch = false
  44.     draw()
  45.     rs.setOutput(redstoneOutputSide, false)
  46. end
  47.  
  48. local function dropOn()
  49.     switch = false
  50.     draw()
  51.     rs.setOutput(redstoneOutputSide, false)
  52.     dropTimer = os.startTimer(fuelDropTime)
  53. end
  54.  
  55. local function dropOff()
  56.     switch = true
  57.     draw(switch)
  58.     rs.setOutput(redstoneOutputSide, true)
  59.     burnTimer = os.startTimer(fuelBurnTime)
  60. end
  61.  
  62. local function masterToggle()
  63.     masterSwitch = not masterSwitch
  64.     if masterSwitch and switch then
  65.         dropOn()
  66.     end
  67. end
  68.  
  69. function Main()
  70.     if burnTimer == nil and dropTimer == nil and masterSwitch then --if we are initializing after first startup, and toggled controller ON
  71.         dropOn()
  72.     end
  73.     local event, param1 = os.pullEvent()
  74.     if event == "redstone" and checkRedstoneState() then --if redstone is updated..
  75.         if not manaFull then --and it's not full, then start dropTimer
  76.             dropOn()
  77.         else
  78.             draw("mana") --otherwise display full message in runtime
  79.         end
  80.     elseif event == "timer" and param1 == burnTimer and masterSwitch then --if burnTimer ends and the controller is running
  81.         dropOn()
  82.     elseif event == "timer" and param1 == dropTimer and masterSwitch then --if dropTimer ends and the controller is running
  83.         dropOff()
  84.     elseif event == "key" and param1 == 57 then --if Spacebar is pressed
  85.         dropManual()
  86.     elseif event == "key" and param1 == 20 then --if T is pressed
  87.         masterToggle()
  88.     end
  89.     Main()
  90. end
  91.  
  92. Main()
Advertisement
Add Comment
Please, Sign In to add comment