-- [[
--  Basic tasks for a pilot, no need to reinvent the wheel with these.
--
--  The idea is to have it all here and only really work on the "control"
--  functions and such for each AI.
--]]

--[[
--  Attempt to land on a planet.
--]]
function land()
  -- Only want to land once, prevents guys fomr never leaving.
  if mem.landed then
    ai.poptask()
    return
  end

  target  = mem.land
  dir     = ai.face(target)
  dist    = ai.dist(target)
  bdist   = ai.minbrakedist()

  -- Need to get closer.
  if dir < 10 and dist > bdist then
    ai.accel()
  -- Need to start braking.
  elseif dist < bdist then
    ai.poptask()
    ai.pushtask(0, "landstop")
  end
end

function landstop()
  ai.brake()

  if ai.isstopped() then
    ai.stop() -- Will stop the pilot if below err vel.
    ai.settime(0, rnd.int(8000, 15000)) -- We wait during a while.
    ai.poptask()
    ai.pushtask(0, "landwait")
  end
end

function landwait()
  target = mem.land
  dist = ai.dist(target)
  
  -- In case for some reason landed far away..
  if dist > 50 then
    ai.poptask()
    ai.pushtask(0, "land")

  elseif ai.timeup(0) then
    mem.landed = true -- Mark as landed so they don't spend time forever floating around.
    ai.poptask() -- Ready to do whatever we were doing before.
  end
end

--[[
--  Attempts to run from the target.
--]]
function runaway()
  target = ai.target()

  -- Target must exist.
  if not ai.exists(target) then
    ai.poptask()
    return
  end

  dir = ai.face(target, true)
  ai.accel()

  --[[
  --  TODO: Afterburner handling.
  --  if ai.hasafterburner() then
  --    ai.afterburn(true)
  --  end
  --]]
  if ai.hasturrets() then
    dist = ai.dist(ai.pos(target))
    if dist < ai.getweaprange() then
      ai.settarget(target)
      ai.shoot()
    end
  end
end

--[[
-- Start heading away to try to hyperspace.
--
-- Will need the following in control() to work:
--
-- task = ai.taskname()
-- if task == "hyperspace" then
--   ai.hyperspace() -- Try to hyperspace.
-- end
--]]
function hyperspace()
  dir = ai.face(-1) -- Face away from (0,0).
  if(dir < 10) then
    ai.accel()
  end
end