include("../scripts/ai/include/basic.lua")

-- Required control rate
control_rate = 2

-- Required "control" function.
function control()
 task = ai.taskname()

 enemy = ai.getenemy()
 if task ~= "attack" and enemy ~= nil then
   ai.hostile(enemy)
  ai.pushtask(0, "attack", enemy)
 elseif ai.taskname() == "none" then
  ai.pushtask(0, "scan", ai.rndpilot())
 end
end

-- Required "attacked" function
function attacked(attacker)
 task = ai.taskname()
 if task ~= "attack" and task ~= "runaway" then
  -- Some taunting.
  taunt(attacker)

  -- Now pilot fights back!
  ai.pushtask(0, "attack", attacker)

 elseif task == "attack" then
  if ai.targetid() ~= attaker then
   ai.pushtack(0, "attack", attacker)
  end
 end
end

function create()
 ai.setcredits(rnd.int(1000, ai.shipprice()/200))
 if rnd.int(0, 2)==0 then
  ai.broadcast("This area is under militia survellance.")
  end
end

-- Taunts
function taunt(target)
  taunts = {
    "How dare you attack me!?",
    "YOU! ARE NOT! PREPARED!",
    "Won't you just die already!?",
    "You won't survive!"
  }
  ai.comm(target, taunts[rnd.int(1, #taunts)])
end

function scan()
 target = ai.targetid()
 if not ai.exists(target) then
  ai.poptask()
  return
 end
 dir = ai.face(target)
 dist = ai.dist(ai.pos(target))
 if dir < 10 and dist > 300 then
  ai.accel()
 elseif dist < 300 then -- Scan the target.
  ai.poptask()
 end
end

