-- Required control rate.
control_rate = 2

function control()
	if ai.taskname() == "none" then
		enemy = ai.getenemy()

		if enemey ~= 0 then
			-- Make hostile to enemy (mainly player).
			ai.hostile(enemy)
			ai.pushtask(0, "attack", enemy)
		end
	end
end

-- Required "attacked" function.
function attacked(attacker)
	task = ai.taskname()
	if task ~= "attack" then
		-- Now pilot fights back.
		ai.pushtask(0, "attack", attacker)
	
	elseif task == "attack" then
		if ai.targetid() ~= attacker then
			ai.pushtask(0, "attack", attacker)
		end
	end
end

function attack()
	target = ai.targetid()

	-- Make sure pilot exists.
	if not ai.exists(target) then
		ai.poptask()
		return
	end

	dir = ai.face(target)
	dist = ai.dist(ai.pos(target))
	second = ai.secondary()

	if ai.secondary() == "Launcher" then
		ai.settarget(target)
		is.shoot(2)
	end

	if dir < 10 and dist > 300 then
		ai.accel()
	elseif dir < 10 and dist < 300 then
		ai.shoot()
	end
end