Advertisement
IronicPickle

door.lua

Feb 27th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.43 KB | None | 0 0
  1. -- Args
  2. local args = { ... }
  3. local delay = tonumber(args[1]) or 15
  4. local openDelay = tonumber(args[2]) or 3
  5. local closeDelay = tonumber(args[3]) or 3
  6. local redstoneOutput = args[4] or "right"
  7. local name = args[5] or "Unnamed"
  8. local channel = tonumber(args[6]) or 20
  9.  
  10. -- Libraries
  11. local setup = require("/lua/lib/setupUtils")
  12. local monUtils = require("/lua/lib/monitorUtils")
  13. local write = monUtils.write
  14. local drawBox = monUtils.drawBox
  15. local stateHandler = require("/lua/lib/stateHandler")
  16.  
  17. -- Peripherals
  18. local wrappedPers = setup.getPers({
  19.     "monitor", "modem"
  20. })
  21. local monitor = setup.setupMonitor(
  22.     wrappedPers.monitor[1], 0.5
  23. )
  24. local modem = wrappedPers.modem[1]
  25. local speaker = peripheral.find("speaker")
  26.  
  27. -- Setup
  28. local stateData = stateHandler.getState("door")
  29. local defaultData = "closed"
  30. local state = stateData or defaultData
  31.  
  32. -- Windows
  33. local winHeader = setup.setupWindow(
  34.     monitor, 1, 1, monitor.x, 4
  35. )
  36. local winFooter = setup.setupWindow(
  37.     monitor, 1, (monitor.y - 3), monitor.x, 4
  38. )
  39. local winMain = setup.setupWindow(
  40.     monitor, 1, 5, monitor.x, (monitor.y - (4 + 4))
  41. )
  42.  
  43. -- Main
  44. function start()
  45.     print("# Program Started")
  46.     modem.open(channel)
  47.    
  48.     parallel.waitForAll(close, await)
  49. end
  50.  
  51. function await()
  52.     while(true) do
  53.         local event, p1, p2, p3, p4, p5 = os.pullEvent()
  54.        
  55.         local isTouch = (event == "monitor_touch")
  56.        
  57.         local isModemMessage = (event == "modem_message")
  58.        
  59.         if(isTouch) then
  60.             modem.transmit(channel, channel,
  61.                 { type = state }
  62.             )
  63.             if(state == "open") then
  64.                 break
  65.             elseif(state == "closed") then
  66.                 open()
  67.             end
  68.         elseif(isModemMessage) then
  69.             local body = p4
  70.             if(body.type == "open") then
  71.                 break
  72.             elseif(body.type == "closed") then
  73.                 open()
  74.             end
  75.         end
  76.     end
  77. end
  78.  
  79. function open()
  80.     parallel.waitForAny(await,
  81.         function()
  82.             rs.setAnalogOutput(redstoneOutput, 15)
  83.             if(speaker) then
  84.                 speaker.playSound(
  85.                     "minecraft:entity.experience_orb.pickup",
  86.                     1, 0.5
  87.                 )
  88.             end
  89.             state = "opening"
  90.             updateState()
  91.             drawHeader()
  92.             drawFooter()
  93.             for i = openDelay, 0, -0.1 do
  94.                 drawMain(i, openDelay)
  95.                 sleep(0.1)
  96.             end
  97.             state = "open"
  98.             updateState()
  99.             drawFooter()
  100.             for i = delay, 0, -0.1 do
  101.                 drawHeader(math.floor(i))
  102.                 drawMain(i, delay)
  103.                 sleep(0.1)
  104.             end
  105.         end
  106.     )
  107.     close()
  108. end
  109.  
  110. function close()
  111.     rs.setAnalogOutput(redstoneOutput, 0)
  112.     if(speaker) then
  113.         speaker.playSound(
  114.             "minecraft:entity.experience_orb.pickup",
  115.             1, 0.5
  116.         )
  117.     end
  118.     state = "closing"
  119.     updateState()
  120.     drawHeader()
  121.     drawFooter()
  122.     for i = openDelay, 0, -0.1 do
  123.         drawMain(i, openDelay)
  124.         sleep(0.1)
  125.     end
  126.     state = "closed"
  127.     updateState()
  128.     drawHeader()
  129.     drawFooter()
  130.     drawMain()
  131. end
  132.  
  133. function updateState()
  134.     stateHandler.updateState("door", state)
  135. end
  136.  
  137. function drawHeader(timeLeft)
  138.     local bgColors = {
  139.         open = colors.green,
  140.         opening = colors.red,
  141.         closed = colors.red,
  142.         closing = colors.green
  143.     }
  144.     winHeader.bg = bgColors[state]
  145.     winHeader.setBackgroundColor(winHeader.bg)
  146.    
  147.     drawBox(winHeader,
  148.         1, 1, winHeader.x, winHeader.y,
  149.         true
  150.     )
  151.     drawBox(winHeader,
  152.         1, winHeader.y, winHeader.x, winHeader.y,
  153.         true, colors.white
  154.     )
  155.     timeLeft = timeLeft or 10
  156.     local msgs = {
  157.         open = "Sealing in: " .. timeLeft,
  158.         opening = "Opening",
  159.         closed = name,
  160.         closing = "Sealing"
  161.     }
  162.    
  163.     write(winHeader, msgs[state], 0, 2, "centre")
  164. end
  165.  
  166. function drawFooter()
  167.     local bgColors = {
  168.         open = colors.green,
  169.         opening = colors.red,
  170.         closed = colors.red,
  171.         closing = colors.green
  172.     }
  173.     winFooter.bg = bgColors[state]
  174.     winFooter.setBackgroundColor(winFooter.bg)
  175.    
  176.     drawBox(winFooter,
  177.         1, 1, winFooter.x, winFooter.y,
  178.         true
  179.     )
  180.     drawBox(winFooter,
  181.         1, 1, winFooter.x, 1,
  182.         true, colors.white
  183.     )
  184.    
  185.     local msgs = {
  186.         open = "Click to Seal",
  187.         opening = "Please Wait",
  188.         closed = "Click to Open",
  189.         closing = "Please Wait"
  190.     }
  191.    
  192.     write(winFooter,
  193.         msgs[state],
  194.         0, 3, "centre"
  195.     )
  196. end
  197.  
  198. function drawMain(timeLeft, timeMax)
  199.     local bgColors = {
  200.         open = {colors.green, colors.red},
  201.         opening = {colors.green, colors.red},
  202.         closed = {colors.red, colors.green},
  203.         closing = {colors.red, colors.green}
  204.     }
  205.     winMain.bg = bgColors[state][1]
  206.     winMain.setBackgroundColor(winMain.bg)
  207.    
  208.     drawBox(winMain,
  209.         1, 1, winMain.x, winMain.y,
  210.         true
  211.     )
  212.    
  213.     if(state ~= "closed") then
  214.         local single = monitor.x / timeMax
  215.         local width = timeLeft * single
  216.        
  217.         drawBox(winMain,
  218.             1, 1, width, winMain.y,
  219.             true, bgColors[state][2]
  220.         )
  221.     end
  222. end
  223.  
  224. setup.utilsWrapper(start, modem, channel)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement