Advertisement
fatboychummy

doorThing.lua

Aug 13th, 2019
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.51 KB | None | 0 0
  1. local ints = {peripheral.find("redstone_integrator")}
  2. local mons = {peripheral.find("monitor")}
  3. local openTime = 15
  4. local scale = 4
  5.  
  6. local function forEachPeripheral(periphs, func)
  7.   local x = {}
  8.   for i, periph in ipairs(periphs) do
  9.     table.insert(x, {func(periph, i)})
  10.   end
  11.   return x
  12. end
  13.  
  14. local function turnOn()
  15.   forEachPeripheral(
  16.     ints,
  17.     function(integrator)
  18.       for i, side in ipairs(integrator.getSides()) do
  19.         integrator.setOutput(side, true)
  20.       end
  21.     end
  22.   )
  23. end
  24.  
  25. local function turnOff()
  26.   forEachPeripheral(
  27.     ints,
  28.     function(integrator)
  29.       for i, side in ipairs(integrator.getSides()) do
  30.         integrator.setOutput(side, false)
  31.       end
  32.     end
  33.   )
  34. end
  35.  
  36. local function writeMonitors(col1, col2, stuff)
  37.   forEachPeripheral(
  38.     mons,
  39.     function(monitor)
  40.       monitor.setBackgroundColor(col1 or colors.black)
  41.       monitor.setTextColor(col2 or colors.white)
  42.       monitor.clear()
  43.       monitor.setTextScale(scale)
  44.       monitor.setCursorPos(1, 1)
  45.       monitor.write(stuff or "")
  46.     end
  47.   )
  48. end
  49.  
  50. local function wait(tm)
  51.   local tmr = os.startTimer(tm)
  52.   while true do
  53.     local ev, tmr2 = os.pullEventRaw()
  54.  
  55.     if ev == "terminate" then
  56.       turnOff()
  57.       error("Terminated", 0)
  58.     elseif ev == "timer" and tmr == tmr2 then
  59.       return
  60.     end
  61.   end
  62. end
  63.  
  64. local function countDown()
  65.   local sizes = forEachPeripheral(
  66.     mons,
  67.     function(monitor)
  68.       return monitor.getSize()
  69.     end
  70.   )
  71.  
  72.   for i = openTime, 0, -1 do
  73.     forEachPeripheral(
  74.       mons,
  75.       function(monitor, o)
  76.         local mx, my = table.unpack(sizes[o])
  77.         local ln = #tostring(i)
  78.         local wrt = string.rep(' ', mx - ln) .. tostring(i)
  79.         monitor.setBackgroundColor(colors.black)
  80.         monitor.setTextColor(colors.white)
  81.         monitor.clear()
  82.         monitor.setCursorPos(1, 1)
  83.         monitor.write(wrt)
  84.       end
  85.     )
  86.  
  87.     wait(1)
  88.   end
  89. end
  90.  
  91. local function open()
  92.   writeMonitors(colors.red)
  93.   print("Opening.")
  94.   turnOn()
  95.   parallel.waitForAny(
  96.     function()
  97.       wait(openTime)
  98.     end,
  99.     countDown
  100.   )
  101.   turnOff()
  102.   writeMonitors(nil, nil, "DOORS")
  103. end
  104.  
  105.  
  106. local function main()
  107.   turnOff()
  108.   writeMonitors(nil, nil, "DOORS")
  109.   while true do
  110.     term.clear()
  111.     term.setCursorPos(1, 1)
  112.     print("Press 'o' to open.")
  113.     local ev, key = os.pullEvent()
  114.     if ev == "key" and key == keys.o then
  115.       open()
  116.     elseif ev == "monitor_touch" then
  117.       open()
  118.     end
  119.   end
  120. end
  121.  
  122. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement