melzneni

sys2_modem_recvWire

Feb 11th, 2020
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.24 KB | None | 0 0
  1. --sys2_modem_recv_wire
  2.  
  3. function split(s, delimiter)
  4.     local result = {};
  5.     for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do
  6.         table.insert(result, match);
  7.     end
  8.     return result;
  9. end
  10.  
  11. function getMsgData(msg)
  12.     local i, j = string.find(msg, ":")
  13.     local tag = string.sub(msg, 1, i - 1)
  14.     local pts = syslib.split(string.sub(msg, i + 1, -1), ",")
  15.     return tag, pts
  16. end
  17.  
  18. --syslib_end
  19.  
  20. rednet.open("bottom")
  21.  
  22. function receiveRednet()
  23.     while true do
  24.         local id, msg = rednet.receive()
  25.         if type(msg) == "string" and string.find(msg, ":") ~= nil then
  26.             return id, msg
  27.         end
  28.     end
  29. end
  30.  
  31. print("modemRecvWire")
  32.  
  33. local modem = peripheral.wrap("back")
  34.  
  35. while true do
  36.     local id, msg = receiveRednet()
  37.  
  38.     if type(msg) == "string" then
  39.         print("original: ", msg)
  40.         local i, j = string.find(msg, ":")
  41.         local tag = string.sub(msg, 1, i - 1)
  42.         if tag == "@other" then
  43.             msg = string.sub(msg, i + 1, -1)
  44.             print("sending: ",msg)
  45.             modem.transmit(1,3,"<send>:"..id..","..msg)
  46.         elseif tag ~= "<transmitted>" then
  47.             modem.transmit(1, 3, "<transmitted>:" .. id .. "," .. msg)
  48.         end
  49.     end
  50. end
Add Comment
Please, Sign In to add comment