Wiiplay123

RSP Client

Apr 12th, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.87 KB | None | 0 0
  1. --RSP Client
  2. side = peripheral.find("modem")
  3. rednet.open(side)
  4. local cn = tostring(os.getComputerID())
  5. local oldPull = os.pullEvent
  6. rednet.host("rsp",cn)
  7. local idlist = {rednet.lookup("rspserv")}
  8. if #idlist == 0 then
  9.     error("No RSP servers found.",0)
  10. elseif #idlist == 1 then
  11.     idzz = idlist[1]
  12. else
  13.     print("Computer ID List")
  14.     for i,v in ipairs(idlist) do
  15.         rednet.send(v,{"label"})
  16.         local id, msg = rednet.receive(0.5)
  17.         if type(msg) == "table" then
  18.             if #msg == 1 then
  19.                 idlist[i] = tostring(idlist[i]).." ("..msg[1]..")"
  20.             end
  21.         end
  22.         print(idlist[i])
  23.     end
  24.     write("Connect to: ")
  25.     idzz = tonumber(read())
  26. end
  27. rednet.send(idzz,{"init",unpack({term.getSize()})})
  28. function os.pullEvent(f)
  29.     local e = {os.pullEventRaw(f)}
  30.     if e[1] == "terminate" then
  31.         rednet.unhost("rsp",cn)
  32.         rednet.send(idzz,{"dc"})
  33.         rednet.close(side)
  34.         os.pullEvent = oldPull
  35.         error("Terminated",0)
  36.     else
  37.         return unpack(e)
  38.     end
  39. end
  40. function loop()
  41.     local id = "msg"
  42.     while true do
  43.         local event, ids, msg = os.pullEvent()
  44.         if event == "rednet_message" and ids == idzz then
  45.             if type(msg) == "table" then
  46.                 idz = true
  47.                 if msg[1] == "write" then
  48.                     term.write(msg[2])
  49.                 elseif msg[1] == "pos" then
  50.                     term.setCursorPos(msg[2],msg[3])
  51.                 elseif msg[1] == "clear" then
  52.                     term.clear()
  53.                 elseif msg[1] == "tc" then
  54.                     term.setTextColor(msg[2])
  55.                 elseif msg[1] == "bc" then
  56.                     term.setBackgroundColor(msg[3])
  57.                 elseif msg[1] == "cl" then
  58.                     term.clearLine()
  59.                 elseif msg[1] == "bl" then
  60.                     term.setCursorBlink(msg[2])
  61.                 elseif msg[1] == "scroll" then
  62.                     term.scroll(msg[2])
  63.                 else
  64.                     idz = false
  65.                 end
  66.                 if idz then
  67.                     id = ids
  68.                 end
  69.             end
  70.         elseif event == "key" and id ~= "msg" then
  71.             rednet.send(id,{"key",ids})
  72.         elseif event == "char" and id ~= "msg" then
  73.             rednet.send(id,{"char",ids})
  74.         end
  75.     end
  76. end
  77. loop()
Advertisement
Add Comment
Please, Sign In to add comment