Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if fs.exists("isrlib") then
- os.loadAPI("isrlib")
- end
- function tableContainsValue(table, value)
- for i, v in pairs(table) do
- if v == value then return true end
- end
- return false
- end
- function initRednet(side)
- rednet.open(side)
- end
- function printTable(tbl)
- printTableIndex(tbl, "", {})
- end
- function printTableIndex(tbl, before, alreadyUsed)
- for i, v in pairs(tbl) do
- if type(v) == "table" then
- if tableContainsValue(alreadyUsed, v) then
- print(before .. i .. ":", "alreadyUsed");
- else
- print(before .. i .. ": [");
- table.insert(alreadyUsed, v)
- printTableIndex(v, before .. "\t", alreadyUsed)
- print(before .. "]")
- end
- else
- print(before .. i .. ":", v);
- end
- end
- end
- function printKeys(t)
- local keys = ""
- local c = 0
- for i, v in pairs(t) do
- keys = keys .. ((c ~= 0 and ",") or "") .. i
- c = 1
- end
- print(keys)
- end
- function split(s, delimiter)
- local result = {};
- for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do
- table.insert(result, match);
- end
- return result;
- end
- function getMsgData(msg)
- local i, j = string.find(msg, ":")
- local tag = string.sub(msg, 1, i - 1)
- local pts = split(string.sub(msg, i + 1, -1), ",")
- return tag, pts
- end
- local thisComputerID
- function receiveRednet()
- while true do
- local id, msg = rednet.receive()
- if type(msg) == "string" then
- if thisComputerID == nil then findComputerId() end
- local cid
- if #msg > 8 and string.sub(msg, 1, 8) == "<global>" then
- msg = string.sub(msg, 9)
- cid = id
- elseif #msg > 8 and string.sub(msg, 1, 8) == "<direct>" then
- msg = string.sub(msg, 9)
- cid = -1
- end
- if cid ~= nil and (thisComputerID == cid or cid == -1) then
- if string.find(msg, ":") ~= nil then
- if string.sub(msg, 1, 1) == "[" then
- local ind = string.find(msg, "]")
- id = tonumber(string.sub(msg, 2, ind - 1))
- msg = string.sub(msg, ind + 1)
- end
- return id, msg
- end
- end
- end
- end
- end
- function sendRednet(id, msg)
- if isrlib ~= nil then
- rednet.send(id, "<direct>[" .. isrlib.getCId() .. "]" .. msg)
- return
- end
- rednet.send(id, "<direct>" .. msg)
- end
- function findComputerId()
- rednet.broadcast("@globalNet:access")
- while true do
- local id, msg = rednet.receive()
- if type(msg) == "string" and #msg >= 12 and string.sub(msg, 1, 12) == "@gNetAnsw:hu" then
- thisComputerID = id;
- break
- end
- rednet.broadcast("@globalNet:access")
- end
- end
- function broadcast(msg)
- if thisComputerID == nil then findComputerId() end
- if isrlib ~= nil then
- rednet.send(thisComputerID, "@globalNet:broadcast,[" .. isrlib.getCId() .. "]" .. msg)
- --rednet.broadcast("[" .. isrlib.getCId() .. "]" .. msg)
- return
- end
- rednet.send(thisComputerID, "@globalNet:broadcast," .. msg)
- end
- monitor={
- getSize= function (monitor)
- local answ = getAnswer("@monman:" .. monitor.name .. ",get,size","@monAnsw", monitor.id)
- local ind = string.find(answ.msg, ",")
- return tonumber(string.sub(answ.msg, 1, ind - 1)), tonumber(string.sub(answ.msg, ind + 1))
- end,
- connect=function (name)
- local event = getAnswer("@monman:" .. name .. ",get,ping", "@monAnsw")
- return { id = event.id, name = name }
- end
- }
- function getAnswer(msg,key, id, timeout)
- if timeout == nil then timeout = 3 end
- if id == nil then
- broadcast(msg)
- else
- sendRednet(id, msg)
- end
- for i = 1, timeout * 20 do
- for _, e in ipairs(isrlib.getEvents()) do
- if e.type == isrlib.ETYPE_RNET then
- if #e.msg > 9 and string.sub(e.msg, 1, 9) == key .. ":" then
- e.msg = string.sub(e.msg, 10)
- return e
- end
- end
- end
- sleep(0.05)
- end
- error("answer-timeout")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement