Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local message,id,messageID
- local i,l,_,side,channel
- local command,id,
- local modems = {}
- local sides = {}
- local channels = {}
- local data = {}
- local function serialize(command,side,amount)
- data[1] = command
- data[2] = side
- data[3] = amount
- return textutils.serialize(data)
- end
- --This function adds the modem to the list of modems the computer will send messages to
- function addModem(side)
- if peripheral.getType(side) == "modem" then
- sides[i] = side
- modems[#modems+1] = peripheral.wrap(side)
- return true
- else
- return false
- end
- end
- --This function removes the modem from the list of modems this computer will send messages to
- function removeModem(side)
- for i = 1,#modems do
- if sides[i] == side then
- modems[i] = nil
- sides[i] = nil
- for i = 1,(#modems - i) do
- if modems[i+1] ~= nil then
- modems[i] = modems[i+1]
- sides[i] = sides[i+1]
- end
- end
- end
- end
- end
- --This function adds the channel to the list of channels this sends stuff to
- function addChannel(channel)
- for i = 1,#modems do
- if not modems[i].isOpen() then
- modems[i].open(channel)
- end
- end
- end
- --This function removes the channel from the list of channels this sends stuff to
- function removeChannel(channel)
- for i = 1,#modems do
- if modems[i].isOpen(channel) then
- modems[i].close(channel)
- end
- end
- end
- local function sendMessage()
- if #modems == 0 then
- return false
- else
- for i = 1,#modems do
- for l = 1,#channels do
- modems[i].transmit(channels[l],os.getComputerID(),message)
- end
- end
- return true
- end
- end
- function getInput(id,side)
- message = serialize("getInput",side,nil)
- sendMessage()
- _,_,_,messageID,message = os.pullEvent("modem_message")
- data = textutils.unserialize()
- if messageID == id then
- if message then
- return true
- else
- return false
- end
- end
- end
- function getOutput(id,side)
- message = serialize("getOutput",side,nil)
- sendMessage()
- _,_,_,messageID,message = os.pullEvent("modem_message")
- data = textutils.unserialize()
- if messageID == id then
- if message then
- return true
- else
- return false
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment