Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local m = peripheral.wrap("back")
- m.open(1)
- channels = {65533, 65534, 65535}
- local m2 = peripheral.wrap("top")
- local function trilaterate( A1, B1, C1 )
- local A = A1
- A.vPosition = vector.new(A1.vPosition.x,A1.vPosition.y,A1.vPosition.z)
- local B = B1
- B.vPosition = vector.new(B1.vPosition.x,B1.vPosition.y,B1.vPosition.z)
- local C = C1
- C.vPosition = vector.new(C1.vPosition.x,C1.vPosition.y,C1.vPosition.z)
- local a2b = B.vPosition - A.vPosition
- local a2c = C.vPosition - A.vPosition
- if math.abs( a2b:normalize():dot( a2c:normalize() ) ) > 0.999 then
- return nil
- end
- local d = a2b:length()
- local ex = a2b:normalize( )
- local i = ex:dot( a2c )
- local ey = (a2c - (ex * i)):normalize()
- local j = ey:dot( a2c )
- local ez = ex:cross( ey )
- local r1 = A.nDistance
- local r2 = B.nDistance
- local r3 = C.nDistance
- local x = (r1*r1 - r2*r2 + d*d) / (2*d)
- local y = (r1*r1 - r3*r3 - x*x + (x-i)*(x-i) + j*j) / (2*j)
- local result = A.vPosition + (ex * x) + (ey * y)
- local zSquared = r1*r1 - x*x - y*y
- if zSquared > 0 then
- local z = math.sqrt( zSquared )
- local result1 = result + (ez * z)
- local result2 = result - (ez * z)
- local rounded1, rounded2 = result1:round( 0.01 ), result2:round( 0.01 )
- if rounded1.x ~= rounded2.x or rounded1.y ~= rounded2.y or rounded1.z ~= rounded2.z then
- return rounded1, rounded2
- else
- return rounded1
- end
- end
- return result:round( 0.01 )
- end
- local args = ({...})
- if #args > 0 then
- if args[1] == "config" then
- local cmp = {}
- for ai = 1, 3 do
- cmp[ai] = {}
- cmp[ai]["vPosition"] = {}
- write("Computer "..ai.." X: ")
- cmp[ai]["vPosition"].x = tonumber(read())
- write("Computer "..ai.." Y: ")
- cmp[ai]["vPosition"].y = tonumber(read())
- write("Computer "..ai.." Z: ")
- cmp[ai]["vPosition"].z = tonumber(read())
- end
- write("File name: ")
- local f = fs.open(read(),"w")
- f.write(textutils.serialize(cmp))
- f.close()
- else
- local compid = 0
- compid = tonumber(args[1])
- for o = 1, #channels do
- m2.open(channels[o])
- end
- if compid == 0 then
- error("No computer index specified.")
- end
- if compid == 1 then
- if fs.exists(args[2]) then
- local f = fs.open(args[2],"r")
- local f2 = f.readAll()
- local ecmp = textutils.unserialize(f2)
- f.close()
- while true do
- local msg = "Test"
- local cmp = {}
- while true do
- local e = {os.pullEvent("modem_message")}
- if e[2] == "top" then
- ecmp[1]["nDistance"] = e[6]
- cmp[1] = true
- msg = e[5]
- else
- if type(e[5]) == "table" then
- ecmp[tonumber(e[5][1])]["nDistance"] = tonumber(e[5][2])
- msg = e[5][3]
- cmp[tonumber(e[5][1])] = true
- end
- end
- if cmp[1] ~= nil and cmp[2] ~= nil and cmp[3] ~= nil then
- break
- end
- end
- tri = trilaterate(ecmp[1],ecmp[2],ecmp[3])
- print(msg.." ("..tri.x..", "..tri.y..", "..tri.z..")")
- if #args >= 3 then
- local f = fs.open(args[3],"a")
- f.write(msg.." ("..tri.x..", "..tri.y..", "..tri.z..")")
- f.close()
- end
- end
- else
- print("Configuration file does not exist.")
- end
- else
- while true do
- local e = {os.pullEvent("modem_message")}
- m.transmit(1,1,{compid,e[6],e[5]})
- print(e[5])
- end
- end
- end
- else
- print("mtracker <config, or computer index (1-3)> <config filename>")
- print("If index is 1, the computer is the main computer.")
- print("Main computer requires the config filename argument.")
- end
Add Comment
Please, Sign In to add comment