dofile("Scripts/utils.lua");
dofile("Scripts/Init/units.lua");
dofile("Scripts/Init/vehicle2.lua");

fire_timeout = 15;
airplane_priority = 10000;

function Initialize()
	BasicInit();
	V2Init();
end

function Main()
	AIparts:Run();
end

function Fire()
	if hold_fire or not unit.IsGunIdle() or unit.GetAmmo1() <= 0 then return end
	local best_target;
	local best_value = 0;

	local enemies = unit.GetEnemyPlanes(0, object.desc.AlarmRange*2);

	if #enemies <= 0 then
		enemies = unit.GetEnemyList(0, object.desc.AlarmRange)
		if #enemies == 0 then
-- ScreenLog("no aerial or ground enemies found");
			return
		end
-- ScreenLog("ground units");
	end

-- ScreenLog("enemies found: " .. #enemies);
-- utils.log_value(#enemies);
	for _,target in pairs(enemies) do
		local target_type = object.GetTargetType(target);
		local target_dist = unit.GetTargetDistance(target);

		local target_priority = object.desc.Armament.TargetPriorities[target_type];
		local target_cost = target_priority / target_dist;

		if target_cost > best_value then
			best_value = target_cost;
			best_target = target;
		end
	end

	if best_target then 
		unit.AttackObject(best_target);
	end
end
