Advertisement
Lion4ever

GPS Locator

Mar 17th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.95 KB | None | 0 0
  1. local wlModem
  2. local wModem
  3.  
  4. for i,j in ipairs(rs.getSides()) do
  5. if peripheral.getType(j) == "modem" then
  6. if peripheral.call(j,"isWireless") then
  7. wlModem = j
  8. else
  9. wModem = j
  10. end
  11. end
  12. end
  13.  
  14. if not wlModem then
  15. print("No wireless modem attached!")
  16. return
  17. end
  18. if not wModem then
  19. print("The computers need to be connected")
  20. return
  21. end
  22. local computers = {peripheral.find("computer")}
  23. if #computers < 3 then
  24. print("Not enough Computers connected!\n(Turn on the wired modems, too)")
  25. return
  26. end
  27. for i,j in pairs(computers) do
  28. if not j.isOn() then
  29. j.turnOn()
  30. end
  31. end
  32. peripheral.call(wlModem,"open",gps.CHANNEL_GPS)
  33. peripheral.call(wlModem,"open",rednet.CHANNEL_REPEAT)
  34. peripheral.call(wlModem,"open",rednet.CHANNEL_BROADCAST)
  35.  
  36. local args = {...}
  37.  
  38. local isMaster = args[1] == "master"
  39. local pos = {gps.locate()}
  40.  
  41. if isMaster then
  42. local buffer = {}
  43. local bufferi = 0
  44. local calcID
  45. local env = {peripheral={
  46. wrap=function() return setmetatable({},{__index=function() end})end,
  47. call=function() return true end,
  48. getType=function() return "modem" end},
  49. os={getComputerID = function() return 1 end,startTimer=function() return 1 end,
  50. pullEvent=function() bufferi = bufferi+1
  51. return "modem_message",rs.getSides()[1],1,1,buffer[calcID][bufferi][3],
  52. buffer[calcID][bufferi][2] end}}
  53. while true do
  54. local msg = {os.pullEvent("modem_message")}
  55. local info = nil
  56. if msg[2] == wlModem then
  57. info = {msg[5].nMessageID,msg[6],unpack(pos)}
  58. end
  59. if msg[2] == wModem then
  60. info = msg[5].message
  61. end
  62. if info then
  63. if not buffer[info[1]] then
  64. buffer[info[1]] = {}
  65. end
  66. table.insert(buffer[info[1]],info)
  67. if #buffer[info[1]] >= computers+1 then
  68. bufferi = 0
  69. calcID = info[1]
  70. setfenv(gps.locate,env)
  71. print(table.concat({gps.locate()}," "))
  72. setfenv(gps.locate,getfenv())
  73. end
  74. end
  75. end
  76. else
  77. while true do
  78. local msg = {os.pullEvent("modem_message")}
  79. if msg[2] == wlModem then
  80. peripheral.call(wModem,"transmit",1,1,{msg[5].nMessageID,msg[6],pos})
  81. end
  82. end
  83. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement