Michael45678

door

Nov 26th, 2021 (edited)
1,041
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Yellow sticky piston
  2. -- Green placer
  3. -- Orange Piston
  4.  
  5. BLOCK_STRENGHT = 0.8
  6. DOOR_HEIGHT = 5
  7. BUNDLE_SIDE = "bottom"
  8. CLOSE_DELAY = 10
  9.  
  10. CURRENT_REDSTONE_STATE = 0
  11.  
  12. settings.define("door_state", {default = "unknown"})
  13. settings.load()
  14. while (settings.get("door_state") == "unknown") do
  15.     print("Unknown door position!")
  16.     print("Is the door currently open or closed? [open/closed]")
  17.     write("> ")
  18.     local answer = read()
  19.     if answer == "open"  or answer == "closed" then
  20.         settings.set("door_state", answer)
  21.     end
  22.  
  23.     settings.save()
  24. end
  25.  
  26. term.clear()
  27. print("Door control script active!")
  28.  
  29.  
  30.  
  31.  
  32. function disableAll()
  33.     redstone.setBundledOutput(BUNDLE_SIDE, 0)
  34.     CURRENT_REDSTONE_STATE = 0
  35. end
  36.  
  37. function enableRed(color)
  38.     CURRENT_REDSTONE_STATE = colors.combine(CURRENT_REDSTONE_STATE, color)
  39.     redstone.setBundledOutput(BUNDLE_SIDE, CURRENT_REDSTONE_STATE)
  40. end
  41.  
  42. function disableRed(color)
  43.     CURRENT_REDSTONE_STATE = colors.subtract(CURRENT_REDSTONE_STATE, color)
  44.     redstone.setBundledOutput(BUNDLE_SIDE, CURRENT_REDSTONE_STATE)
  45. end
  46.  
  47.  
  48. function placeRow()
  49.     enableRed(colors.orange)
  50.     sleep(0.2)
  51.     disableRed(colors.orange)
  52. end
  53.  
  54. function close()
  55.     disableAll()
  56.     enableRed(colors.yellow)
  57.  
  58.     sleep(0.2)
  59.  
  60.     for i = 1, DOOR_HEIGHT+1, 1
  61.     do
  62.         sleep(1.1)
  63.         placeRow()
  64.     end
  65.  
  66.     sleep(0.2)
  67.  
  68.     disableAll();
  69.  
  70.     settings.set("door_state", "closed")
  71. end
  72.  
  73. function open()
  74.     disableAll()
  75.     enableRed(colors.orange)
  76.     sleep(0.5)
  77.     enableRed(colors.blue)
  78.     sleep(BLOCK_STRENGHT*DOOR_HEIGHT+1)
  79.     disableAll()
  80.  
  81.     enableRed(colors.yellow)
  82.  
  83.     sleep(1.1)
  84.     placeRow()
  85.     sleep(0.2)
  86.     disableAll()
  87.     settings.set("door_state", "open")
  88. end
  89.  
  90. rednet.open("left")
  91.  
  92. local timer_id = -1
  93. while true do
  94.  
  95.     local event = {os.pullEvent()}
  96.     local state = settings.get("door_state")
  97.  
  98.     if ((event[1] == "redstone" and redstone.testBundledInput(BUNDLE_SIDE, colors.lime)) or (event[1] == "rednet_message" and event[3] == "doit")) then
  99.         if (state == "closed") then
  100.             open()
  101.         end
  102.         if timer_id ~= -1 then
  103.             os.cancelTimer(timer_id)
  104.         end
  105.         timer_id = os.startTimer(CLOSE_DELAY)
  106.     elseif (event[1] == "timer" and state == "open")
  107.     then
  108.         close()
  109.     end
  110.  
  111.     settings.save();
  112. end
RAW Paste Data