Advertisement
Guest User

rnhst bugfix 10/23/12

a guest
Oct 23rd, 2012
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.48 KB | None | 0 0
  1. local tArgs = { ... }
  2. local function opnmdm()
  3.     for i,v in pairs(rs.getSides()) do 
  4.         if peripheral.getType(v) == "modem" then
  5.             if not rednet.isOpen(v) then rednet.open(v) end
  6.         return true end
  7.     end
  8. return false end
  9. local dmnlst,rlylst,hstID,sndr,mssg,dstnc,cmnd,prm1 = {},{},os.computerID(),tonumber(tArgs[5])
  10. local hstfnc = {
  11. PING = function() rednet.send(sndr,textutils.serialize({xPos,yPos,zPos})) end,
  12. bcst = function(bmssg) if dmnlst[sndr] then
  13.     if bmssg then rednet.send(nil,bmssg) end
  14. end end,
  15. drp = function()
  16.     local rID = rlylst[sndr]
  17.     rlylst[sndr],rlylst[rID] = nil
  18.     local hndl = fs.open("rlylst","w")
  19.     hndl.write(textutils.serialize(rlylst)) hndl.close()
  20.     rednet.send(sndr,"Relay to ID"..rID.." dropped")
  21. end,
  22. fwd = function(frwd)
  23.     local rID,fmssg = frwd:match("(%d+)%s*(.*)$")
  24.     rID = tonumber(rID)
  25.     if rID and fmssg then rednet.send(rID,fmssg) end
  26. end,
  27. hsts = function() if dmnlst[sndr] then
  28.     local ndx,hstlst = 0,{}
  29.     rednet.broadcast("PING")
  30.     repeat ndx = ndx+1
  31.         hstlst[ndx] = {rednet.receive(0.5)}
  32.     until not hstlst[ndx][1]
  33.     rednet.send(sndr,textutils.serialize(hstlst))
  34. end end,
  35. reboot = function() if dmnlst[sndr] then shell.run("reboot") end end,
  36. rly = function(rlyID)
  37.     local rID = tonumber(rlyID)
  38.     rlylst[sndr],rlylst[rID] = rID,sndr
  39.     local hndl = fs.open("rlylst","w")
  40.     hndl.write(textutils.serialize(rlylst)) hndl.close()
  41.     rednet.send(sndr,"Relaying to ID"..rlylst[sndr])
  42. end,
  43. rcvfl = function(flnm) if dmnlst[sndr] then
  44.     local cntnt
  45.     flnm,cntnt = flnm:match("(%S+)%s*(.*)")
  46.     local hndl = fs.open(flnm,"w") hndl.write(cntnt) hndl.close()
  47.     rednet.send(sndr,flnm.." Updated")
  48. end end,
  49. srvfl = function(flnm)
  50.     flnm = flnm:match("%S+")
  51.     local hndl = fs.open(flnm,"r")
  52.     rednet.send(sndr,hndl.readAll()) hndl.close()
  53. end,
  54. }
  55.  
  56. if opnmdm() then
  57.     xPos,yPos,zPos = gps.locate(2,true )
  58.     if tArgs[1] == "host" then
  59.         if not xPos then xPos,yPos,zPos = tArgs[2],tArgs[3],tArgs[4]
  60.             local itr8 = 0
  61.             while not (xPos and yPos and zPos) do
  62.                 write("Enter x coordinate: ") xPos = tonumber(read())
  63.                 write("Enter y coordinate: ") yPos = tonumber(read())
  64.                 write("Enter z coordinate: ") zPos = tonumber(read())
  65.                 itr8 = itr8+1
  66.                 if itr8 > 3 then print("Invalid input") return false end
  67.             end
  68.         end
  69.         print("GPS/Relay Node position "..xPos..","..yPos..","..zPos)
  70.         local hndl = fs.open("startup","w")
  71.         hndl.writeLine("shell.run(\"rnhst\",\"host\","..xPos..","..yPos..","..zPos..")")
  72.         hndl.close()
  73.         if sndr then hndl,xclst,dmnlst = fs.open("xclst","w"),{[sndr] = true},{[sndr] = true}
  74.             hndl.writeLine(textutils.serialize(xclst)) hndl.close()
  75.         elseif fs.exists("xclst") then hndl = fs.open("xclst","r")
  76.             xclst = textutils.unserialize(hndl.readAll()) hndl.close()
  77.         end
  78.         if fs.exists("rlylst") then hndl = fs.open("rlylst","r")
  79.             rlylst = textutils.unserialize(hndl.readAll()) hndl.close()
  80.         end
  81.         if fs.exists("dmnlst") then hndl = fs.open("dmnlst","r")
  82.             dmnlst = textutils.unserialize(hndl.readAll()) hndl.close()
  83.         else hndl = fs.open("dmnlst","w") hndl.writeLine(textutils.serialize(dmnlst)) hndl.close()end
  84.         repeat sndr,mssg,dstnc = rednet.receive()
  85.             if rlylst[hstID] then rednet.send(rlylst[hstID],sndr..":"..mssg) end
  86.             if xclst and not xclst[sndr] then cmnd = "none" else
  87.                 cmnd,prm1 = mssg:match("^(%S+)%s*(.*)")
  88.                 if hstfnc[cmnd] then hstfnc[cmnd](prm1) end
  89.             end
  90.             if rlylst[sndr] and not hstfnc[cmnd] then rednet.send(rlylst[sndr],mssg) end
  91.         until false
  92.     return true end
  93. else print("could not open modem") return false
  94. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement