Advertisement
montana_1

gpsHost

Jul 28th, 2015
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.94 KB | None | 0 0
  1. --[[
  2. --      Code by minecraft: montana_1
  3. --      please send comments or questions to [email protected] with a subject I might read
  4. --
  5. --      Description of code:
  6. --          This is a program written for use in minecraft with the mod "OpenComputers"
  7. --          This is the server side or host code for a single computer or possible micro-controller to broadcast
  8. --          a signal from a minimum of 4 (possibly more but not tested) access points containing there predetermined
  9. --          co-ordinates which are entered into the program upon first run and saved in a file for later use.
  10. --          The co-ordinates can then be determined by a client program from this information
  11. --      Note:
  12. --          Because the co-ordinates are calculated by the distance from each access point,
  13. --          it is very important that all other access points within range are set to NOT REPEAT.
  14. --          Which by default is ON. If you do not know how to change this, there is another program
  15. --          on my pastebin account (montana_1) that will first find all access points on a network
  16. --          and second set there range to zero and turn Repeating OFF. This is good practice with any programs
  17. --          to turn the access points effectively off when not being immediately used to save power and
  18. --          prevent errors.
  19. --      Also Note:
  20. --          The timings for this program are set so that there are no processors placed within the access points
  21. ]]--
  22.  
  23.  
  24.  
  25.  
  26. function getAccessPoints()
  27.     local component = require("component")
  28.     local event = require("event")
  29.     local t = component.list()
  30.     local accessPoints = {}
  31.     local i = 0
  32.     for address,componentType in pairs(t) do
  33.         if(componentType == "access_point") then
  34.             accessPoints[i] = address
  35.             i = i + 1
  36.         end
  37.     end
  38.     return accessPoints
  39. end
  40.  
  41. function turnOffRepeaters(accessPoints)
  42.     local component = require("component")
  43.     for i=0, #accessPoints do
  44.         print(accessPoints[i])
  45.         component.proxy(accessPoints[i]).setRepeater(false)
  46.         component.proxy(accessPoints[i]).setStrength(0)
  47.         print(component.proxy(accessPoints[i]).isRepeater())
  48.     end
  49. end
  50.  
  51. function split(inputstr, sep)
  52.     if(inputstr == nil or inputstr == "") then
  53.                 return nil
  54.         end
  55.         if sep == nil then
  56.                 sep = ","
  57.         end
  58.         local t={} ; i=0
  59.         for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  60.                 t[i] = str
  61.                 i = i + 1
  62.         end
  63.         return t
  64. end
  65.  
  66.  
  67. local fileSystem = require("filesystem")
  68. local accessPoints = getAccessPoints()
  69. turnOffRepeaters(accessPoints)
  70. local gps_port = 3665
  71. local signal_strength = 400
  72.  
  73. if(#accessPoints+1 < 4) then
  74.     print("Insuficient access_points, 4 are required you have (".. #accessPoints+1 ..")")
  75.     error()
  76. end
  77.  
  78. local term = require("term")
  79. local shell = require("shell")
  80. local component = require("component")
  81. local hostCoordinates = {}
  82.  
  83. if(fileSystem.exists(shell.resolve("gpsHostData"))) then
  84.     local file = io.open(shell.resolve("gpsHostData"), "r")
  85.     local i = 0
  86.     for line in io.lines(shell.resolve("gpsHostData")) do
  87.         hostCoordinates[i] = split(line,",")
  88.         i=i+1
  89.     end
  90.     file:close()
  91. else
  92.     local file = io.open(shell.resolve("gpsHostData"), "a")
  93.     for i=0, #accessPoints do
  94.         print("Enter coordinates (x,y,z) for access_point: " .. accessPoints[i])
  95.         local tmp = term.read()
  96.         hostCoordinates[i] = split(accessPoints[i]..","..tmp,",")
  97.         file:write(accessPoints[i]..","..tmp)
  98.     end
  99.     file:close()
  100. end
  101.  
  102. component.modem.open(gps_port)
  103. while true do
  104.     for i=0, #hostCoordinates do
  105.         io.write(i..": "..hostCoordinates[i][0].." "..hostCoordinates[i][1]..","..hostCoordinates[i][2]..","..hostCoordinates[i][3])
  106.         component.setPrimary("access_point",hostCoordinates[i][0])
  107.         print("Primary set")
  108.         component.proxy(hostCoordinates[i][0]).setStrength(signal_strength)
  109.         component.modem.broadcast(gps_port, tonumber(hostCoordinates[i][1]),tonumber(hostCoordinates[i][2]),tonumber(hostCoordinates[i][3]))
  110.         os.sleep(0.25)
  111.         component.getPrimary("access_point").setStrength(0)
  112.         os.sleep(1)
  113.     end
  114. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement