Wiiplay123

MTracker

Feb 23rd, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.50 KB | None | 0 0
  1. local m = peripheral.wrap("back")
  2. m.open(1)
  3. channels = {65533, 65534, 65535}
  4. local m2 = peripheral.wrap("top")
  5. local function trilaterate( A1, B1, C1 )
  6.     local A = A1
  7.     A.vPosition = vector.new(A1.vPosition.x,A1.vPosition.y,A1.vPosition.z)
  8.     local B = B1
  9.     B.vPosition = vector.new(B1.vPosition.x,B1.vPosition.y,B1.vPosition.z)
  10.     local C = C1
  11.     C.vPosition = vector.new(C1.vPosition.x,C1.vPosition.y,C1.vPosition.z)
  12.     local a2b = B.vPosition - A.vPosition
  13.     local a2c = C.vPosition - A.vPosition
  14.        
  15.     if math.abs( a2b:normalize():dot( a2c:normalize() ) ) > 0.999 then
  16.         return nil
  17.     end
  18.    
  19.     local d = a2b:length()
  20.     local ex = a2b:normalize( )
  21.     local i = ex:dot( a2c )
  22.     local ey = (a2c - (ex * i)):normalize()
  23.     local j = ey:dot( a2c )
  24.     local ez = ex:cross( ey )
  25.  
  26.     local r1 = A.nDistance
  27.     local r2 = B.nDistance
  28.     local r3 = C.nDistance
  29.        
  30.     local x = (r1*r1 - r2*r2 + d*d) / (2*d)
  31.     local y = (r1*r1 - r3*r3 - x*x + (x-i)*(x-i) + j*j) / (2*j)
  32.        
  33.     local result = A.vPosition + (ex * x) + (ey * y)
  34.  
  35.     local zSquared = r1*r1 - x*x - y*y
  36.     if zSquared > 0 then
  37.         local z = math.sqrt( zSquared )
  38.         local result1 = result + (ez * z)
  39.         local result2 = result - (ez * z)
  40.        
  41.         local rounded1, rounded2 = result1:round( 0.01 ), result2:round( 0.01 )
  42.         if rounded1.x ~= rounded2.x or rounded1.y ~= rounded2.y or rounded1.z ~= rounded2.z then
  43.             return rounded1, rounded2
  44.         else
  45.             return rounded1
  46.         end
  47.     end
  48.     return result:round( 0.01 )
  49.    
  50. end
  51.  
  52. local args = ({...})
  53. if #args > 0 then
  54.     if args[1] == "config" then
  55.         local cmp = {}
  56.         for ai = 1, 3 do
  57.             cmp[ai] = {}
  58.             cmp[ai]["vPosition"] = {}
  59.             write("Computer "..ai.." X: ")
  60.             cmp[ai]["vPosition"].x = tonumber(read())
  61.             write("Computer "..ai.." Y: ")
  62.             cmp[ai]["vPosition"].y = tonumber(read())
  63.             write("Computer "..ai.." Z: ")
  64.             cmp[ai]["vPosition"].z = tonumber(read())
  65.         end
  66.         write("File name: ")
  67.         local f = fs.open(read(),"w")
  68.         f.write(textutils.serialize(cmp))
  69.         f.close()
  70.     else
  71.         local compid = 0
  72.         compid = tonumber(args[1])
  73.         for o = 1, #channels do
  74.             m2.open(channels[o])
  75.         end
  76.         if compid == 0 then
  77.             error("No computer index specified.")
  78.         end
  79.         if compid == 1 then
  80.             if fs.exists(args[2]) then
  81.                 local f = fs.open(args[2],"r")
  82.                 local f2 = f.readAll()
  83.                 local ecmp = textutils.unserialize(f2)
  84.                 f.close()
  85.                 while true do
  86.                     local msg = "Test"
  87.                     local cmp = {}
  88.                     while true do
  89.                         local e = {os.pullEvent("modem_message")}
  90.                         if e[2] == "top" then
  91.                             ecmp[1]["nDistance"] = e[6]
  92.                             cmp[1] = true
  93.                             msg = e[5]
  94.                         else
  95.                             if type(e[5]) == "table" then
  96.                                 ecmp[tonumber(e[5][1])]["nDistance"] = tonumber(e[5][2])
  97.                                 msg = e[5][3]
  98.                                 cmp[tonumber(e[5][1])] = true
  99.                             end
  100.                         end
  101.                         if cmp[1] ~= nil and cmp[2] ~= nil and cmp[3] ~= nil then
  102.                             break
  103.                         end
  104.                     end
  105.                     tri = trilaterate(ecmp[1],ecmp[2],ecmp[3])
  106.                     print(msg.." ("..tri.x..", "..tri.y..", "..tri.z..")")
  107.                     if #args >= 3 then
  108.                         local f = fs.open(args[3],"a")
  109.                         f.write(msg.." ("..tri.x..", "..tri.y..", "..tri.z..")")
  110.                         f.close()
  111.                     end
  112.                 end
  113.             else
  114.                 print("Configuration file does not exist.")
  115.             end
  116.         else
  117.             while true do
  118.                 local e = {os.pullEvent("modem_message")}
  119.                 m.transmit(1,1,{compid,e[6],e[5]})
  120.                 print(e[5])
  121.             end
  122.         end
  123.     end
  124. else
  125.     print("mtracker <config, or computer index (1-3)> <config filename>")
  126.     print("If index is 1, the computer is the main computer.")
  127.     print("Main computer requires the config filename argument.")
  128. end
Add Comment
Please, Sign In to add comment