Advertisement
thatparadox

SimpleRednetRedstone

Oct 25th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.09 KB | None | 0 0
  1. --  SimpleRednetRedstone by thatParadox
  2. --  www.youtube.com/thatparadox
  3. --
  4. --  
  5. --  Designed for elevators but could be useful for all sorts of creations. This program let's you select from a list on an Advanced Monitor which computer you would like to activate a redstone signal. It works Wirelessly using Rednet.
  6. --  
  7. --  Installation:
  8. --  Place an Advanced Computer and attach a Wireless Modem and, if you want one, an Advanced Monitor.
  9. --  Copy and paste into the computer: pastebin get f9T5ee5S startup
  10. --  Press: Enter
  11. --  Push and hold CRTL + R until computer reboots.
  12. --  Computer will ask you "Which level is this?". Enter a number that when pressed on the Monitor will activate the redstone output on this computer.
  13. --
  14. --  Notes:
  15. --  Only one computer at a time will emit the redstone signal.
  16. --  Computer will emit redstone on all sides
  17. --  program will automatically detect your monitor and modem.
  18. --  You don't have to have a monitor attached if you only want the computer to receive signals but it will need a wireless modem
  19. --  The red number on the monitor is the computer that is currently emitting the redstone
  20. --  The number surrounded by > and < is the number of this computer
  21. --  You can change the rednet protocol if already in use by changing the number below in RedProtoc
  22.  
  23.  
  24.  
  25.  
  26. RedProtoc = 42 -- change to use different rednet protocol
  27.  
  28. -- Detects Monitor
  29. mon = peripheral.find("monitor")
  30.  
  31. --Detects Modem
  32. for k,v in pairs(rs.getSides()) do
  33.   if peripheral.getType(v) == "modem" then
  34.    rednet.open(v)
  35.   end
  36. end
  37.  
  38. -- Draws the background image on the monitor
  39. function drawBackground()
  40.   if mon ~= nil then
  41.     mon.setBackgroundColor(colors.black)
  42.     mon.clear()
  43.     x,y = mon.getSize()
  44.   for yc = 1,y do
  45.       if yc%2 == 1 then
  46.         mon.setBackgroundColor(colors.yellow)
  47.       else
  48.         mon.setBackgroundColor(colors.lightGray)
  49.       end
  50.       for xc = 2,x-1 do
  51.         mon.setCursorPos(xc, yc)
  52.         mon.write(" ")
  53.       end
  54.     mon.setCursorPos(x/2,yc)
  55.     end
  56.   end
  57. end
  58.  
  59.  
  60. -- Draws the numbers on the Monitor
  61. function drawButtons()
  62.   if mon ~= nil then
  63.     x,y = mon.getSize()
  64.     for i=1,y do
  65.       if i%2 == 1 then
  66.         mon.setBackgroundColor(colors.yellow)
  67.       else
  68.         mon.setBackgroundColor(colors.lightGray)
  69.       end
  70.       mon.setTextColor(colors.black)
  71.       if tonumber(isActive) ~= nil and tonumber(isActive) == i then
  72.         mon.setTextColor(colors.red)
  73.       end
  74.       mon.setCursorPos(x/2+1, i)
  75.       mon.write( tostring(i) )
  76.     end
  77.     mon.setBackgroundColor(colors.black)
  78.     mon.setTextColor(colors.yellow)
  79.     mon.setCursorPos(1, tonumber(level))
  80.     mon.write(">")
  81.     mon.setCursorPos(x, tonumber(level))
  82.     mon.write("<")
  83.   end
  84. end
  85.  
  86.  
  87. -- Makes the button flash when pressed
  88. function buttonSelect(buttonPosY)
  89.   if mon ~= nil then
  90.     x,y = mon.getSize()
  91.     mon.setBackgroundColor(colors.gray)
  92.     for xc = 2,x-1 do
  93.       mon.setCursorPos(xc, param3)
  94.       mon.write(" ")
  95.     end
  96.     mon.setTextColor(colors.orange)
  97.     mon.setCursorPos(x/2+1, buttonPosY)
  98.     mon.write(tostring(buttonPosY))
  99.     sleep(0.3)
  100.     drawBackground()
  101.     drawButtons()
  102.   end
  103. end
  104.  
  105. -- Makes redstone signal activate or deactivate on all sides
  106. function rsAllSides(state)
  107.   for k,v in pairs(rs.getSides()) do
  108.     rs.setOutput(v, state)
  109.   end
  110. end
  111.  
  112.  
  113. if fs.exists("level") == false then  --checks for if the level has been defined yet
  114.   print("Which level is this?")
  115.   write("> ")
  116.   level = read()
  117.   term.clear()
  118.   term.setCursorPos(1,1)
  119.   file = fs.open("level", "w")
  120.   file.write(level)
  121.   file.close()
  122. else
  123.   file = fs.open("level", "r")
  124.   level = file.readAll()
  125.   file.close()
  126. end
  127.  
  128. if fs.exists("isActive") then -- Checks if there should be an active redstone signal
  129.   file = fs.open("isActive","r")
  130.   isActive = tostring(file.readAll())
  131.   file.close()
  132.   if tonumber(isActive) == tonumber(level) then
  133.     rsAllSides(true)
  134.   else
  135.     rsAllSides(false)
  136.   end
  137. end
  138. drawBackground()
  139. drawButtons()
  140.  
  141. while true do
  142.   event, param1, param2, param3 = os.pullEvent()
  143.   if event == "rednet_message" and tonumber(param3) == RedProtoc then  -- Rednet message received
  144.     if tonumber(param2) == tonumber(level) then -- checks if message was for this computer to activate redstone
  145.       rsAllSides(true)
  146.       isActive = level
  147.     else  -- turns off redstone if message was not for this computer
  148.       rsAllSides(false)
  149.       isActive = param2
  150.     end
  151.   elseif event == "monitor_touch" then -- Monitor has been touched
  152.     if tonumber(param3) == tonumber(level) then -- checks if this computer was selected by monitor touch to activate redstone
  153.       rsAllSides(true)
  154.       rednet.broadcast(param3,RedProtoc)
  155.     else  -- turns off redstone if this computer wasn't selected by Monitor Touch and broadcasts the selected number
  156.       rsAllSides(false)
  157.       rednet.broadcast(param3,RedProtoc)
  158.     end
  159.     isActive = param3
  160.     buttonSelect(param3)
  161.   end
  162.   drawBackground()
  163.   drawButtons()
  164.   file = fs.open("isActive", "w")  -- writes selected floor to a file in case there is a reboot
  165.   file.write(isActive)
  166.   file.close()
  167. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement