NanoBob

modem repeater

Jul 31st, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.87 KB | None | 0 0
  1. -- local os.pullEvent = os.pullEventRaw
  2.  
  3. local channel
  4.  
  5. function drawScreen()
  6.     term.clear()
  7.     term.setCursorPos(1,1)
  8.     term.write("NanoBank Repeater 1.0")
  9.     term.setCursorPos(3,3)
  10. end
  11. drawScreen()
  12.  
  13. local modem = peripheral.find("modem")
  14. if modem == nil then
  15.     print("Failed to initialise modem.")
  16. else
  17.     print("Initialised modem.")
  18. end
  19.  
  20. term.setCursorPos(3,4)
  21. if fs.exists("event")==false then shell.run("pastebin get UKPy4iiE event") end
  22. if os.loadAPI("event")==false then error("Failed to load event API") end
  23.  
  24. if fs.exists("data")==false then shell.run("pastebin get LnvzL7ur data") end
  25. if os.loadAPI("data")==false then error("Failed to load data API") end
  26.  
  27. if fs.exists("utils")==false then shell.run("pastebin get dyvydHtK utils") end
  28. if os.loadAPI("utils")==false then error("Failed to load utils API") end
  29. term.write("Sucessfully loaded APIs")
  30.  
  31. if data.get("channel","channel") == nil then
  32.     term.setCursorPos(3,5)
  33.     term.write("Input channel please: ")
  34.     channel = tonumber(read())
  35.     data.set("channel",channel,"channel")
  36.     modem.open(channel)
  37.     term.setCursorPos(3,5)
  38.     term.write("Channel successfully opened ")
  39. else
  40.     channel = tonumber(data.get("channel","channel"))
  41.     modem.open(channel)
  42.     term.setCursorPos(3,5)
  43.     term.write("Channel successfully opened ")
  44. end
  45.  
  46. function received(side,receiveChannel,replyChannel,message)
  47.     local target = message[1]
  48.     if target == channel - 1 then
  49.         local message = table.remove(message,1)
  50.         modem.transmit(channel-1,replyChannel,message)
  51.     elseif target == channel + 1 then
  52.         local message = table.remove(message,1)
  53.         modem.transmit(channel+1,replyChannel,message)
  54.     elseif target > channel then
  55.         modem.transmit(channel+1,replyChannel,message)
  56.     elseif target < channel then
  57.         modem.transmit(channel-1,replyChannel,message)
  58.     end
  59. end
  60. event.addHandler("modem_message",received)
  61.  
  62. while true do
  63.     event.handleCCEvents()
  64. end
Add Comment
Please, Sign In to add comment