osmarks

Piston Door Controller

Mar 6th, 2022 (edited)
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.35 KB | None | 0 0
  1. local integrators = {
  2.     redstone_integrator_788 = "north",
  3.     redstone_integrator_790 = "east"
  4. }
  5. local screen = peripheral.find "monitor"
  6. local side = "north"
  7. local button_side = "front"
  8. local modem = peripheral.find "modem"
  9. modem.open(49961)
  10. os.pullEvent = function(filter)
  11.     while true do
  12.         local ev = {coroutine.yield(filter)}
  13.         if ev[1] == filter then
  14.             return unpack(ev)
  15.         end
  16.     end
  17. end
  18. local w, h = screen.getSize()
  19.  
  20. screen.setTextScale(1.5)
  21. screen.setBackgroundColor(colors.black)
  22. screen.clear()
  23.  
  24. local function set_open(state)
  25.     for i, side in pairs(integrators) do
  26.         peripheral.call(i, "setOutput", side, not state)
  27.     end
  28. end
  29.  
  30. local scramble = {}
  31. for i = 1, ((w * h) - 2) do
  32.     table.insert(scramble, string.char(47 + i))
  33. end
  34.  
  35. local function shuffle()
  36.     for i = 1, (#scramble - 1) do
  37.         local j = math.random(i, #scramble)
  38.         local a = scramble[i]
  39.         local b = scramble[j]
  40.         scramble[j] = a
  41.         scramble[i] = b
  42.     end
  43. end
  44.  
  45. local function draw_display()
  46.     screen.setTextColor(colors.black)
  47.     for i = 0, ((w * h) - 3) do
  48.         local x = i % w
  49.         local y = math.floor(i / w)
  50.         screen.setCursorPos(x + 1, y + 1)
  51.         local c = scramble[i + 1]
  52.         screen.setBackgroundColor(2 ^ ((string.byte(c) - 48) % 15))
  53.         screen.write(c)
  54.     end
  55.     screen.setTextColor(colors.white)
  56.     screen.setBackgroundColor(colors.black)
  57.     screen.write "CE"
  58. end
  59.  
  60. local function flash_display(color)
  61.     screen.setBackgroundColor(color)
  62.     screen.clear()
  63.     sleep(0.1)
  64.     draw_display()
  65. end
  66.  
  67. set_open(false)
  68. draw_display()
  69.  
  70. local can_open = false
  71.  
  72. local function monitor_ui()
  73.     local inp = ""
  74.     while true do
  75.         local _, _, x, y = os.pullEvent "monitor_touch"
  76.         if y == h and x == w then
  77.             print "E"
  78.             flash_display(colors.blue)
  79.             shuffle()
  80.             inp = ""
  81.             if can_open then set_open(true) sleep(1) set_open(false) end
  82.             draw_display()
  83.         elseif y == h and x == (w - 1) then
  84.             print "C"
  85.             flash_display(colors.red)
  86.             shuffle()
  87.             draw_display()
  88.             inp = ""
  89.         else
  90.             flash_display(colors.lime)
  91.             local i = (x - 1) + (y - 1) * w
  92.             inp = inp .. string.char(i + 48)
  93.         end
  94.     end
  95. end
  96. local function comms()
  97.     while true do
  98.         local _, _, c, rc, open = os.pullEvent "modem_message"
  99.         can_open = open
  100.     end
  101. end
  102. local function button()
  103.     while true do
  104.         os.pullEvent "redstone"
  105.         set_open(rs.getInput(button_side))
  106.     end
  107. end
  108.  
  109. parallel.waitForAll(monitor_ui, button, comms)
Add Comment
Please, Sign In to add comment