local xforce, zforce
local max
max = 0
local sp, tp
local cars = {}
local totalCars = ac.getSim().carsCount
local i

for i = 0 , totalCars - 1 do
  cars[i] = {}
  cars[i].name = ac.getCarName(i)
  cars[i].number = ac.getCarLeaderboardPosition(i)
end


function update(dt)
  xforce = math.abs(ac.getCar().acceleration["x"])
  zforce = math.abs(ac.getCar().acceleration["z"])
  if xforce > max then
    max = xforce
  end

  for i = 0, totalCars - 1 do
    tp = ac.getCarState(i+1).position
    cars[i].dist = math.sqrt((sp.x - tp.x)^2 + (sp.y - tp.y)^2 + (sp.z - tp.z)^2)
    cars[i].rx = tp.x - sp.x
    cars[i].ry = tp.y - sp.y
    cars[i].rz = tp.z - sp.z
  end
end

function script.windowMain(dt)
  sp = ac.getCar().position
  ui.text('position: ' .. sp.x .. ' , ' .. sp.y .. ' , ' .. sp.z)
  ui.text('rotation: x= ' .. ac.getCar().compass)
  ui.text('x-force: ' .. xforce .. ' / ' .. max)
  ui.text('z-force: ' .. zforce)
  ui.text('car skin name: ' ..ac.getCarSkinID())
  ui.text('--- entry cars ---')
  for i = 0 , totalCars - 1 do
    ui.text(i .. ' carNumber: ' .. cars[i].number .. ' : ' .. cars[i].name .. ' dist: ' .. cars[i].dist)
    ui.text(' relative coordinate: ' .. cars[i].rx .. ' , ' .. cars[i].ry .. ' , ' .. cars[i].rz)
  end
end
