radius = 500
scale = 50

if not term.isColor() then
  print("Advanced computer required")
  exit()
end

sides = peripheral.getNames()
for key,side in pairs(sides) do
  if peripheral.getType(side) == "warpdriveRadar" then
    print("Radar found on " .. side)
    radar = peripheral.wrap(side)
  end
end
if radar == nil then
  term.setBackgroundColor(colors.red)
  term.setTextColor(colors.white)
  term.write("No radar detected")
  return
end

w, h = term.getSize()

term.clear()

function colorScreen(color)
  for a = 2,w-1 do
    for b = 1,h do
      paintutils.drawPixel(a, b, color)
    end
  end
end

function textOut(x, y, text, fg, bg)
  term.setCursorPos(x, y)
  term.setTextColor(fg)
  term.setBackgroundColor(bg)
  term.write(text)
  local xt,yt = term.getCursorPos()
  term.setCursorPos(1, yt + 1)
end	

function translateXZ(oldX, oldZ, i)
  local x = radarX - oldX
  local z = radarZ - oldZ
  
  x = x / (radius / scale)
  z = z / (radius / scale)
  
  x = x + (w / 2)
  z = z + (h / 2)
  
  x = math.floor(x)
  z = math.floor(z)
  
  return x,z
end

function drawContact(x, y, z, name, color)
  local newX, newZ = translateXZ(x, z)
  
  paintutils.drawPixel(newX, newZ, color)
  textOut(newX - 3, newZ + 1, "[" .. name .. "]", colors.white, colors.black)
end

function scanAndDraw()
  local energy, energyMax = radar.energy()
  local energyRequired = radar.getEnergyRequired(radius)
  if (energy < energyRequired) then
    hh = math.floor(h / 2)
    hw = math.floor(w / 2)
    
    paintutils.drawLine(hw - 5, hh - 1, hw + 5, hh - 1, colors.red)
    paintutils.drawLine(hw - 5, hh, hw + 5, hh, colors.red)
    textOut(hw - 4, hh,"LOW POWER", colors.white, colors.red)
    paintutils.drawLine(hw - 5, hh + 1, hw + 5, hh + 1, colors.red)
    sleep(1)
    
    return 0
  end
  radar.radius(radius)
  radar.start()
  local scanDuration = radar.getScanDuration(radius)
  sleep(scanDuration)
  
  redraw()
  
  numResults = radar.getResultsCount()
  
  if (numResults ~= 0) then
    for i = 0, numResults-1 do
      success, type, name, cx, cy, cz = radar.getResult(i)
      
      drawContact(cx, cy, cz, name, colors.red)
    end
  end
  
  drawContact(radarX, radarY, radarZ, "RAD", colors.yellow)
end

function redraw()
  colorScreen(colors.green)
  
  paintutils.drawLine(1, 1, w, 1, colors.black)
  
  textOut((w / 2) - 8, 1, "= Q-Radar v0.2 =", colors.white, colors.black)
  
  textOut(w - 3, 1, "[X]", colors.white, colors.red)
  
  paintutils.drawLine(1, h, w, h, colors.black)
  local energy, energyMax = radar.energy()
  textOut(4, h, "Energy: " .. energy .. " EU | Scan radius: " .. radius, colors.white, colors.black)
end

radarX, radarY, radarZ = radar.position()
mrun = true
while (mrun) do
  scanAndDraw()
end

term.clear()
