I Have been working on a program that triangulates the position of all broadcasting turtles and computers in its range. so far i had it working then tryed to make it store the targets in a table (this is where it started going wrong) now it just errors vector line 12 constantly. I have no idea what is up with my code. to use this you will need to make a radar station. you must have enough GPS towers around (or edit code and manual insert GPS' co ords) )first place 4 computers with modems on top in a pattern that looks like the corner of a box. then place a computer somewhere else as a base station. you need to then alter the program Ant.lua to the ID of the base station. run Ant on all four Sensor computers Now change pRadar.lua to use the ID's of the Ant computers. The reason it needs manual alteration is that it would cause feedback if it recorded its own packets so it must know to exclude cretin information

pRadar.lua



rednet.open("back") -- will be replaced with config latter
-- vars
local sec = 4 -- time to in valid
local offx,offy = 0,0
local vals = {}
local tVal
local one,two
local c = 0
local test = false
local targets = {} -- id X Y Z time
term.clear()
term.setCursorPos(1,1)

-- functions
local function safeWrite(this)
local termX,termY = term.getSize()
local curX,curY = term.getCursorPos()
if termX-curX-1 < 0 then
  return
else
  write (string.sub(this,1,termX-curX-1))
  return
end
end
local function trilaterate( A, B, C )
local a2b = B.position - A.position
local a2c = C.position - A.position
 
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.distance
local r2 = B.distance
local r3 = C.distance
 
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.position + (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(), result2:round()
  if rounded1.x ~= rounded2.x or rounded1.y ~= rounded2.y or rounded1.z ~= rounded2.z then
   return result1, result2
  else
   return rounded1
  end
end
return result:round()

end
local function narrow( p1, p2, fix )
local dist1 = math.abs( (p1 - fix.position):length() - fix.distance )
local dist2 = math.abs( (p2 - fix.position):length() - fix.distance )

if math.abs(dist1 - dist2) < 0.05 then
  return p1, p2
elseif dist1 < dist2 then
  return p1:round()
else
  return p2:round()
end
end
local function count(a)
a = tostring(a)
local curX,curY = term.getCursorPos()
local termX,termY = term.getSize()
term.setCursorPos(termX-2-string.len(a),termY-2)
write(a)
term.setCursorPos(curX,curY)
return
end
local function mark(xIn,yIn,zIn,idIn)
term.setCursorPos(xIn-offx,yIn-offy)
safeWrite("X")
term.setCursorPos(xIn-offx+1,yIn-offy-1)
safeWrite([[/]])
term.setCursorPos(xIn-offx+1,yIn-offy-2)
safeWrite(idIn.."Z"..zIn)
if idIn == 37 or idIn == 38 or idIn == 39 or idIn == 41 then
else
local temX,temY = term.getSize()
local temp = "Z:"..zIn.."ID:"..idIn
term.setCursorPos(temX-string.len(temp)-1,1)
safeWrite(temp)
end
end
local function hud()
local curx,cury = term.getCursorPos()
term.setCursorPos(1,1)
print("X:"..offx.." Y:"..offy)
term.setCursorPos(curx,cury)
end
local function tFinder(lookIn,ifor)
for i = 1,table.maxn(lookIn) do
  if lookIn[i][1] == ifor then
  return i
  end
end
return nil
end
local function drawTagets()
for i = 1, #targets do
  if targets[i][5] < os.clock()+sec then
  mark(targets[i][2],targets[i][3],targets[i][4],targets[i][1])
  else
  table.remove(targets,i)
  end
end
end
local function addTarget(iID,ix,iy,iz)
local at = tFinder(targets,iID)
if at == nil then
os.pullEvent()
targets[#targets+1] = {}
targets[#targets][1] = iID
targets[#targets][2] = ix
targets[#targets][3] = iy
targets[#targets][4] = iz
targets[#targets][5] = os.clock()
else
table.remove(targets,at)
end
end
-- end functions
os.startTimer(0.5)
while true do
e,e1,e2,e3,e4,e5 = os.pullEvent()

if e == "rednet_message" and e1 == 37 or e1 == 38 or e1 == 39 or e1 == 41 then -- id's of Ant's
  tVal = textutils.unserialize(e2) -- 1 x 2 y 3 z 4 id 5 distance
   local tFix = { id = tVal[4], position = vector.new( tVal[1], tVal[2], tVal[3] ), distance = tVal[5] }
  table.insert(vals,tFix)
  addTarget(tVal[1],tVal[2],tVal[3],e1)
  if test then
   c = c+1
   count(c)
  end
end

if #vals >= 4 then
  one , two = trilaterate(vals[1],vals[2],vals[3])
  one , two = narrow( one , two ,vals[4])
  if test then
   write("target "..vals[1].id.." at : ")
   print("X: "..one.x.." Y: "..one.y.." Z: "..one.z)
  else
   addTarget(one.x,one.y,one.z,vals[1].id)
  end
  one,two = nil,nil
  table.remove(vals)
  table.remove(vals)
  table.remove(vals)
  table.remove(vals)
end
if e == "key" then
  if e1 == 200 then -- up
  offy = offy-1
  end
  if e1 == 208 then -- down
  offy = offy+1
  end
  if e1 == 203 then -- left
  offx = offx-1
  end
  if e1 == 205 then -- rght
  offx = offx+1
  end
  term.clear()
  hud()
  drawTagets()
end
if e == "timer" then
  os.startTimer(0.5)
  term.clear()
  drawTagets()
  hud()
end
end

Ant.lua


rednet.open("top") -- will be replaced with config latter
local x,y,z = gps.locate( 2, true )
if x == nil or y == nil or z == nil then
error()
end
local ID = 28
local temp = {}
local event , var1 , var2 , var3
while true do
event , var1 , var2 , var3 = os.pullEvent()
if event == "rednet_message" then
  temp[1] = var1
  temp[2] = var3
  rednet.send(ID,textutils.serialize({x,y,z,temp[1],temp[2]}))
end
end

and this is a scrip for a turtle it will move and broadcast.


rednet.open("right")
local x,y,z
while true do
for i = 1,20 do
x,y,z = gps.locate(2)
term.clear()
term.setCursorPos(1,1)
print("x:"..tostring(x).." y:"..tostring(y).." z:"..tostring(z))
turtle.forward()
sleep(1)
end
turtle.turnLeft()
turtle.turnLeft()
end


I would be very great full to anyone this finds how i broke this code. If you have any suggestions to make it more stable that too would be appreciated.