dofile("Scripts/mission.lua");
dofile("Scripts/msl.lua");

function PostCreate()
	mission_lua.BasicMissionInitialize()
	AIparts:AddPeriodic("WIN", 10, func_WIN, 73 + RandomHalf(10))
	AIparts:AddPeriodic("DEFEAT", 10, func_DEFEAT, 73 + RandomHalf(10))
	AIparts:AddPeriodic("Check_Enemy_Spawn", 73, func_Check_Enemy_Spawn, 73 + RandomHalf(73))
	AIparts:AddPeriodic("Ammo_Objectiv_1", 10, func_Ammo_Objectiv_1, 53 + RandomHalf(10))
	AIparts:AddPeriodic("Ammo_Objectiv_2", 10, func_Ammo_Objectiv_2, 73 + RandomHalf(10))
	AIparts:AddPeriodic("Sappers_10_cars", 10, func_Sappers_10_cars, 33 + RandomHalf(10))
	AIparts:AddPeriodic("Flak_01", 58, func_Flak_01, 73 + RandomHalf(58))
	AIparts:AddPeriodic("Flak_02", 10, func_Flak_02, 53 + RandomHalf(10))
	AIparts:AddPeriodic("Flak_03", 10, func_Flak_03, 133 + RandomHalf(10))
	AIparts:AddPeriodic("Flak_04", 10, func_Flak_04, 94 + RandomHalf(10))
	AIparts:AddPeriodic("Patrol_pz4", 73, func_Patrol_pz4, 73 + RandomHalf(73))
	AIparts:AddPeriodic("Spawn_pz_1", 5785, func_Spawn_pz_1, 33 + RandomHalf(100))
	AIparts:AddPeriodic("Spawn_pz_2", 5793, func_Spawn_pz_2, 33 + RandomHalf(100))
	AIparts:AddPeriodic("Spawn_pz_3", 5879, func_Spawn_pz_3, 430 + RandomHalf(100))
	AIparts:AddPeriodic("Spawn_pz_4", 6785, func_Spawn_pz_4, 33 + RandomHalf(100))
	AIparts:AddPeriodic("Spawn_pz_5", 33, func_Spawn_pz_5, 73 + RandomHalf(33))
	AIparts:AddOneTime("PresetS", 43, func_PresetS)
	AIparts:AddPeriodic("CountSappers", 13, func_CountSappers, 13 + RandomHalf(13))
	AIparts:AddPeriodic("Reinforcement_Start", 43, func_Reinforcement_Start, 33 + RandomHalf(43))
	AIparts:AddPeriodic("Reinforcement_Town_1", 43, func_Reinforcement_Town_1, 43 + RandomHalf(43))
	AIparts:AddPeriodic("Reinforcement_AmmoFuel1", 43, func_Reinforcement_AmmoFuel1, 33 + RandomHalf(43))
	AIparts:AddPeriodic("Germany_Supply_01", 5578, func_Germany_Supply_01, 33 + RandomHalf(100))
	AIparts:AddPeriodic("Germany_Supply_02", 5875, func_Germany_Supply_02, 33 + RandomHalf(100))
	AIparts:AddPeriodic("Flak_All", 43, func_Flak_All, 33 + RandomHalf(43))
	AIparts:AddPeriodic("ObjectivTownComplited", 10, func_ObjectivTownComplited, 33 + RandomHalf(10))
	msd_vars = {
		ammo_1 = 2,
		ammo_time = 0,
		bridge_02 = 0,
		flak_01 = 0,
		flak_02 = 0,
		flak_03 = 0,
		flak_04 = 0,
		number_t_random = 0,
		pz_1 = 0,
		pz_2 = 0,
		pz_3 = 0,
		pz_4 = 0,
		pz_5 = 0,
		pz_random_1_2 = 0,
		pz_random_2_4 = 0,
		pz_random_spawn_1 = 0,
		pz_random_spawn_2 = 0,
		pz_random_spawn_3 = 0,
		pz_random_spawn_4 = 0,
		pz_random_spawn_5 = 0,
		pz_spawn_1 = 0,
		pz_spawn_2 = 0,
		sappers = 0,
		sappers_count = 0,
		usa_ammo = 0,
		usa_fuel = 0,
	}
end

function func_WIN()
	if IsObjectiveCompleted("Town", "Player") then
		mission.OutScreenMessage(129, 5, "0xFF000000")
		PlayerAddScore(1, 1000)
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_DO_"), 30*10, function()  PlayerWin("Player") end)
		AIparts:RemovePeriodic("WIN")
	end
end

function func_DEFEAT()
	if NumPlayerUnits("Player") == 0 then
		PlayerDefeat("Player")
		AIparts:RemovePeriodic("DEFEAT")
	end
end

function func_Check_Enemy_Spawn()
	if IsPlayerUnitsPresent("Player", "spawn_1", 150) then
		msd_vars.pz_spawn_1 = 0
	else
		msd_vars.pz_spawn_1 = 1
	end
	if IsPlayerUnitsPresent("Player", "spawn_2", 170) then
		msd_vars.pz_spawn_2 = 0
	else
		msd_vars.pz_spawn_2 = 1
	end
end

function func_Ammo_Objectiv_1()
	if IsDifficultyLevel("easy") then
		SetObjectiveTimeout("FuellandAmmo", 3600)
		msd_vars.ammo_time = 3600
	elseif IsDifficultyLevel("regular") then
		SetObjectiveTimeout("FuellandAmmo", 1800)
		msd_vars.ammo_time = 1800
	elseif IsDifficultyLevel("hardcore") then
		SetObjectiveTimeout("FuellandAmmo", 900)
		msd_vars.ammo_time = 900
	end
	do
		ProceedObjective("FuellandAmmo", "Player")
	end
	if NumPlayerUnitsInZone("Enemy", "Ammo") > 0 then
		msd_vars.ammo_1 = 1
	elseif NumPlayerUnitsInZone("Enemy", "Ammo") <= 0 then
		msd_vars.ammo_1 = 0
	end
	if msd_vars.ammo_1 == 0 then
		mission.OutScreenMessage(129, 12, "0xFF000000")
		SetObjectiveComplete("FuellandAmmo", "Player")
		AcivateObjective("Town")
		SetObjectiveTimeout("Town", 999999)
		ProceedObjective("Town", "Enemy")
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_DO_"), 10*10, function()  DisableObjective("FuellandAmmo") end)
		RunSpawn("Player", {"Ammo_Usa", }, "Usa_ammo1_spawn", "Ammo_Usa", "", true, true, {}, false, 2)
		RunSpawn("Player", {"Fuel_Usa", }, "Usa_ammo1_spawn", "Fuel_Usa", "", true, true, {}, false, 2)
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_DO_"), 30*10, function()  SetEvent("SpawnAmmoFuel") end)
		AcivateObjective("Flak1")
		AcivateObjective("Flak2")
		AcivateObjective("Flak3")
		AcivateObjective("Flak4")
		SetObjectiveTimeout("Flak1", 999999)
		SetObjectiveTimeout("Flak2", 999999)
		SetObjectiveTimeout("Flak3", 999999)
		SetObjectiveTimeout("Flak4", 999999)
		ProceedObjective("Flak1", "Enemy")
		ProceedObjective("Flak2", "Enemy")
		ProceedObjective("Flak3", "Enemy")
		ProceedObjective("Flak4", "Enemy")
		AcivateObjective("usa_ammo_fuel")
		AIparts:AddOneTime("TIMER_VAR_ammo_1", 30*10, function() msd_vars.ammo_1 = 4 end)
		AIparts:RemovePeriodic("Ammo_Objectiv_1")
	elseif GetGameTime() >= msd_vars.ammo_time and msd_vars.ammo_1 == 1 then
		FailObjective("FuellandAmmo")
		PlayerDefeat("Player")
		AIparts:RemovePeriodic("Ammo_Objectiv_1")
	end
end

function func_Ammo_Objectiv_2()
	if msd_vars.usa_ammo == 1 and msd_vars.usa_fuel == 1 then
		DisableObjective("usa_ammo_fuel")
		DisableObjective("usa_ammo")
		DisableObjective("usa_fuell")
		AIparts:RemovePeriodic("Ammo_Objectiv_2")
	end
	if NumPlayerUnitsInZoneByType("Player", "Ammo2", 21) > 0 and msd_vars.usa_ammo == 0 then
		RunSpawn("Player", {"Ammo_Usa", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 2)
		RunSpawn("Player", {"Ammo_2_reinforcement", }, "Usa_ammo2_spawn", "", "", true, true, {}, false, 0)
		PlayerAddScore(1, 3000)
		msd_vars.usa_ammo = 1
		DisableObjective("usa_ammo_fuel")
		AcivateObjective("usa_fuell")
	elseif NumPlayerUnitsInZoneByType("Player", "Ammo2", 20) > 0 and msd_vars.usa_fuel == 0 then
		RunSpawn("Player", {"Fuel_Usa", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 2)
		RunSpawn("Player", {"Fuel_2_reinforcement", }, "Usa_ammo2_spawn", "", "", true, true, {}, false, 0)
		PlayerAddScore(1, 3000)
		msd_vars.usa_fuel = 1
		DisableObjective("usa_ammo_fuel")
		AcivateObjective("usa_ammo")
	end
end

function func_Sappers_10_cars()
	if NumGroupUnits("SAPPERS", 0, true, false, false) == 0 and msd_vars.sappers == 0 then
		RunSpawn("Player", {"Sappers", }, "Main_Spawn", "", "SAPPERS", true, true, {}, false, 0)
		msd_vars.sappers_count = msd_vars.sappers_count - 1
		msd_vars.sappers = 1
		AIparts:AddOneTime("TIMER_VAR_sappers", 60*10, function() msd_vars.sappers = 0 end)
	elseif msd_vars.sappers_count <= 0 then
		mission.OutScreenMessage(166, 5, "red")
		AIparts:RemovePeriodic("Sappers_10_cars")
	end
end

function func_Flak_01()
	if msd_vars.flak_01 == 2 then
		mission.OutScreenMessage(129, 5, "0xFF000000")
		DisableObjective("Flak1")
		PlayerAddScore(1, 1000)
		AIparts:RemovePeriodic("Flak_01")
	end
	if NumGroupUnits("Flak_01", 0, false, false, false) == 0 and msd_vars.flak_01 == 0 and msd_vars.ammo_1 == 4 then
		msd_vars.flak_01 = 1
	end
	do
		msd_vars.number_t_random = RandomHalf(100)
	end
	if ( msd_vars.flak_01 == 1 and IsDifficultyLevel("easy") and msd_vars.number_t_random >= 0 and msd_vars.number_t_random <= 25  ) then
		RunSpawn("Player", {"pz_usa_tank_1_1", "pz_usa_tank_1_1", "pz_usa_soldiers_1_1", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_01 = 2
	elseif msd_vars.flak_01 == 1 and IsDifficultyLevel("easy") and msd_vars.number_t_random > 25 and msd_vars.number_t_random <= 50 then
		RunSpawn("Player", {"pz_usa_tank_1_2", "pz_usa_tank_1_2", "pz_usa_soldiers_1_2", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_01 = 2
	elseif msd_vars.flak_01 == 1 and IsDifficultyLevel("easy") and msd_vars.number_t_random > 50 and msd_vars.number_t_random <= 75 then
		RunSpawn("Player", {"pz_usa_tank_1_3", "pz_usa_tank_1_3", "pz_usa_tank_1_2", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_01 = 2
	elseif msd_vars.flak_01 == 1 and IsDifficultyLevel("easy") and msd_vars.number_t_random > 75 and msd_vars.number_t_random <= 100 then
		RunSpawn("Player", {"pz_usa_tank_1_4", "pz_usa_soldiers_1_4", "pz_usa_tank_1_4", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_01 = 2
	end
	if ( msd_vars.flak_01 == 1 and IsDifficultyLevel("regular") and msd_vars.number_t_random >= 0 and msd_vars.number_t_random <= 25  ) then
		RunSpawn("Player", {"pz_usa_tank_1_1", "pz_usa_soldiers_1_1", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_01 = 2
	elseif msd_vars.flak_01 == 1 and IsDifficultyLevel("regular") and msd_vars.number_t_random > 25 and msd_vars.number_t_random <= 50 then
		RunSpawn("Player", {"pz_usa_tank_1_2", "pz_usa_soldiers_1_2", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_01 = 2
	elseif msd_vars.flak_01 == 1 and IsDifficultyLevel("regular") and msd_vars.number_t_random > 50 and msd_vars.number_t_random <= 75 then
		RunSpawn("Player", {"pz_usa_tank_1_3", "pz_usa_soldiers_1_3", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_01 = 2
	elseif msd_vars.flak_01 == 1 and IsDifficultyLevel("regular") and msd_vars.number_t_random > 75 and msd_vars.number_t_random <= 100 then
		RunSpawn("Player", {"pz_usa_tank_1_4", "pz_usa_soldiers_1_4", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_01 = 2
	end
	if ( msd_vars.flak_01 == 1 and IsDifficultyLevel("hardcore") and msd_vars.number_t_random >= 0 and msd_vars.number_t_random <= 25  ) then
		RunSpawn("Player", {"pz_usa_soldiers_1_1", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_01 = 2
	elseif msd_vars.flak_01 == 1 and IsDifficultyLevel("hardcore") and msd_vars.number_t_random > 25 and msd_vars.number_t_random <= 50 then
		RunSpawn("Player", {"pz_usa_soldiers_1_2", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_01 = 2
	elseif msd_vars.flak_01 == 1 and IsDifficultyLevel("hardcore") and msd_vars.number_t_random > 50 and msd_vars.number_t_random <= 75 then
		RunSpawn("Player", {"pz_usa_soldiers_1_3", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_01 = 2
	elseif msd_vars.flak_01 == 1 and IsDifficultyLevel("hardcore") and msd_vars.number_t_random > 75 and msd_vars.number_t_random <= 100 then
		RunSpawn("Player", {"pz_usa_soldiers_1_4", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_01 = 2
	end
end

function func_Flak_02()
	if msd_vars.flak_02 == 2 then
		NewObjectiveMessage(129, "Flak2", 5, "0xFF000000")
		DisableObjective("Flak2")
		PlayerAddScore(1, 1000)
		AIparts:RemovePeriodic("Flak_02")
	end
	if NumGroupUnits("Flak_02", 0, false, false, false) == 0 and msd_vars.flak_02 == 0 and msd_vars.ammo_1 == 4 then
		msd_vars.flak_02 = 1
	end
	do
		msd_vars.number_t_random = RandomHalf(100)
	end
	if ( msd_vars.flak_02 == 1 and IsDifficultyLevel("easy") and msd_vars.number_t_random >= 0 and msd_vars.number_t_random <= 25  ) then
		RunSpawn("Player", {"pz_usa_tank_1_1", "pz_usa_tank_1_1", "pz_usa_soldiers_1_1", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_02 = 2
	elseif msd_vars.flak_02 == 1 and IsDifficultyLevel("easy") and msd_vars.number_t_random > 25 and msd_vars.number_t_random <= 50 then
		RunSpawn("Player", {"pz_usa_tank_1_2", "pz_usa_tank_1_2", "pz_usa_soldiers_1_2", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_02 = 2
	elseif msd_vars.flak_02 == 1 and IsDifficultyLevel("easy") and msd_vars.number_t_random > 50 and msd_vars.number_t_random <= 75 then
		RunSpawn("Player", {"pz_usa_tank_1_3", "pz_usa_tank_1_3", "pz_usa_soldiers_1_3", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_02 = 2
	elseif msd_vars.flak_02 == 1 and IsDifficultyLevel("easy") and msd_vars.number_t_random > 75 and msd_vars.number_t_random <= 100 then
		RunSpawn("Player", {"pz_usa_tank_1_4", "pz_usa_tank_1_4", "pz_usa_soldiers_1_4", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_02 = 2
	end
	if ( msd_vars.flak_02 == 1 and IsDifficultyLevel("regular") and msd_vars.number_t_random >= 0 and msd_vars.number_t_random <= 25  ) then
		RunSpawn("Player", {"pz_usa_tank_1_1", "pz_usa_soldiers_1_1", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_02 = 2
	elseif msd_vars.flak_02 == 1 and IsDifficultyLevel("regular") and msd_vars.number_t_random > 25 and msd_vars.number_t_random <= 50 then
		RunSpawn("Player", {"pz_usa_tank_1_2", "pz_usa_soldiers_1_2", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_02 = 2
	elseif msd_vars.flak_02 == 1 and IsDifficultyLevel("regular") and msd_vars.number_t_random > 50 and msd_vars.number_t_random <= 75 then
		RunSpawn("Player", {"pz_usa_tank_1_3", "pz_usa_soldiers_1_3", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_02 = 2
	elseif msd_vars.flak_02 == 1 and IsDifficultyLevel("regular") and msd_vars.number_t_random > 75 and msd_vars.number_t_random <= 100 then
		RunSpawn("Player", {"pz_usa_tank_1_4", "pz_usa_soldiers_1_4", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_02 = 2
	end
	if ( msd_vars.flak_02 == 1 and IsDifficultyLevel("hardcore") and msd_vars.number_t_random >= 0 and msd_vars.number_t_random <= 25  ) then
		RunSpawn("Player", {"pz_usa_soldiers_1_1", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_02 = 2
	elseif msd_vars.flak_02 == 1 and IsDifficultyLevel("hardcore") and msd_vars.number_t_random > 25 and msd_vars.number_t_random <= 50 then
		RunSpawn("Player", {"pz_usa_soldiers_1_2", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_02 = 2
	elseif msd_vars.flak_02 == 1 and IsDifficultyLevel("hardcore") and msd_vars.number_t_random > 50 and msd_vars.number_t_random <= 75 then
		RunSpawn("Player", {"pz_usa_soldiers_1_3", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_02 = 2
	elseif msd_vars.flak_02 == 1 and IsDifficultyLevel("hardcore") and msd_vars.number_t_random > 75 and msd_vars.number_t_random <= 100 then
		RunSpawn("Player", {"pz_usa_soldiers_1_4", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_02 = 2
	end
end

function func_Flak_03()
	if msd_vars.flak_03 == 2 then
		NewObjectiveMessage(129, "Flak3", 5, "0xFF000000")
		DisableObjective("Flak3")
		PlayerAddScore(1, 1000)
		AIparts:RemovePeriodic("Flak_03")
	end
	if NumGroupUnits("Flak_03", 0, false, false, false) == 0 and msd_vars.flak_03 == 0 and msd_vars.ammo_1 == 4 then
		msd_vars.flak_03 = 1
	end
	do
		msd_vars.number_t_random = RandomHalf(100)
	end
	if ( msd_vars.flak_03 == 1 and IsDifficultyLevel("easy") and msd_vars.number_t_random >= 0 and msd_vars.number_t_random <= 25  ) then
		RunSpawn("Player", {"pz_usa_tank_1_1", "pz_usa_tank_1_1", "pz_usa_soldiers_1_1", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_03 = 2
	elseif msd_vars.flak_03 == 1 and IsDifficultyLevel("easy") and msd_vars.number_t_random > 25 and msd_vars.number_t_random <= 50 then
		RunSpawn("Player", {"pz_usa_tank_1_2", "pz_usa_tank_1_2", "pz_usa_soldiers_1_2", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_03 = 2
	elseif msd_vars.flak_03 == 1 and IsDifficultyLevel("easy") and msd_vars.number_t_random > 50 and msd_vars.number_t_random <= 75 then
		RunSpawn("Player", {"pz_usa_tank_1_3", "pz_usa_tank_1_3", "pz_usa_soldiers_1_3", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_01 = 2
	elseif msd_vars.flak_03 == 1 and IsDifficultyLevel("easy") and msd_vars.number_t_random > 75 and msd_vars.number_t_random <= 100 then
		RunSpawn("Player", {"pz_usa_tank_1_4", "pz_usa_tank_1_4", "pz_usa_soldiers_1_4", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_01 = 2
	end
	if ( msd_vars.flak_03 == 1 and IsDifficultyLevel("regular") and msd_vars.number_t_random >= 0 and msd_vars.number_t_random <= 25  ) then
		RunSpawn("Player", {"pz_usa_tank_1_1", "pz_usa_soldiers_1_1", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_03 = 2
	elseif msd_vars.flak_03 == 1 and IsDifficultyLevel("regular") and msd_vars.number_t_random > 25 and msd_vars.number_t_random <= 50 then
		RunSpawn("Player", {"pz_usa_tank_1_2", "pz_usa_soldiers_1_2", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_03 = 2
	elseif msd_vars.flak_03 == 1 and IsDifficultyLevel("regular") and msd_vars.number_t_random > 50 and msd_vars.number_t_random <= 75 then
		RunSpawn("Player", {"pz_usa_tank_1_3", "pz_usa_soldiers_1_3", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_03 = 2
	elseif msd_vars.flak_03 == 1 and IsDifficultyLevel("regular") and msd_vars.number_t_random > 75 and msd_vars.number_t_random <= 100 then
		RunSpawn("Player", {"pz_usa_tank_1_4", "pz_usa_soldiers_1_4", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_03 = 2
	end
	if ( msd_vars.flak_03 == 1 and IsDifficultyLevel("hardcore") and msd_vars.number_t_random >= 0 and msd_vars.number_t_random <= 25  ) then
		RunSpawn("Player", {"pz_usa_soldiers_1_1", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_03 = 2
	elseif msd_vars.flak_03 == 1 and IsDifficultyLevel("hardcore") and msd_vars.number_t_random > 25 and msd_vars.number_t_random <= 50 then
		RunSpawn("Player", {"pz_usa_soldiers_1_2", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_03 = 2
	elseif msd_vars.flak_03 == 1 and IsDifficultyLevel("hardcore") and msd_vars.number_t_random > 50 and msd_vars.number_t_random <= 75 then
		RunSpawn("Player", {"pz_usa_soldiers_1_3", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_03 = 2
	elseif msd_vars.flak_03 == 1 and IsDifficultyLevel("hardcore") and msd_vars.number_t_random > 75 and msd_vars.number_t_random <= 100 then
		RunSpawn("Player", {"pz_usa_soldiers_1_4", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_03 = 2
	end
end

function func_Flak_04()
	if msd_vars.flak_04 == 2 then
		NewObjectiveMessage(129, "Flak4", 5, "0xFF000000")
		DisableObjective("Flak4")
		PlayerAddScore(1, 1000)
		AIparts:RemovePeriodic("Flak_04")
	end
	if NumGroupUnits("Flak_04", 0, false, false, false) == 0 and msd_vars.flak_04 == 0 and msd_vars.ammo_1 == 4 then
		msd_vars.flak_04 = 1
	end
	do
		msd_vars.number_t_random = RandomHalf(100)
	end
	if ( msd_vars.flak_04 == 1 and IsDifficultyLevel("easy") and msd_vars.number_t_random >= 0 and msd_vars.number_t_random <= 25  ) then
		RunSpawn("Player", {"pz_usa_tank_1_1", "pz_usa_tank_1_1", "pz_usa_soldiers_1_1", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_04 = 2
	elseif msd_vars.flak_04 == 1 and IsDifficultyLevel("easy") and msd_vars.number_t_random > 25 and msd_vars.number_t_random <= 50 then
		RunSpawn("Player", {"pz_usa_tank_1_2", "pz_usa_tank_1_2", "pz_usa_soldiers_1_2", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_04 = 2
	elseif msd_vars.flak_04 == 1 and IsDifficultyLevel("easy") and msd_vars.number_t_random > 50 and msd_vars.number_t_random <= 75 then
		RunSpawn("Player", {"pz_usa_tank_1_3", "pz_usa_tank_1_3", "pz_usa_soldiers_1_3", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_04 = 2
	elseif msd_vars.flak_04 == 1 and IsDifficultyLevel("easy") and msd_vars.number_t_random > 75 and msd_vars.number_t_random <= 100 then
		RunSpawn("Player", {"pz_usa_tank_1_4", "pz_usa_tank_1_4", "pz_usa_soldiers_1_4", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_04 = 2
	end
	if ( msd_vars.flak_04 == 1 and IsDifficultyLevel("regular") and msd_vars.number_t_random >= 0 and msd_vars.number_t_random <= 25  ) then
		RunSpawn("Player", {"pz_usa_tank_1_1", "pz_usa_soldiers_1_1", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_04 = 2
	elseif msd_vars.flak_04 == 1 and IsDifficultyLevel("regular") and msd_vars.number_t_random > 25 and msd_vars.number_t_random <= 50 then
		RunSpawn("Player", {"pz_usa_tank_1_2", "pz_usa_soldiers_1_2", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_04 = 2
	elseif msd_vars.flak_04 == 1 and IsDifficultyLevel("regular") and msd_vars.number_t_random > 50 and msd_vars.number_t_random <= 75 then
		RunSpawn("Player", {"pz_usa_tank_1_3", "pz_usa_soldiers_1_3", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_04 = 2
	elseif msd_vars.flak_04 == 1 and IsDifficultyLevel("regular") and msd_vars.number_t_random > 75 and msd_vars.number_t_random <= 100 then
		RunSpawn("Player", {"pz_usa_tank_1_4", "pz_usa_soldiers_1_4", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_04 = 2
	end
	if ( msd_vars.flak_04 == 1 and IsDifficultyLevel("hardcore") and msd_vars.number_t_random >= 0 and msd_vars.number_t_random <= 25  ) then
		RunSpawn("Player", {"pz_usa_soldiers_1_1", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_04 = 2
	elseif msd_vars.flak_04 == 1 and IsDifficultyLevel("hardcore") and msd_vars.number_t_random > 25 and msd_vars.number_t_random <= 50 then
		RunSpawn("Player", {"pz_usa_soldiers_1_2", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_04 = 2
	elseif msd_vars.flak_04 == 1 and IsDifficultyLevel("hardcore") and msd_vars.number_t_random > 50 and msd_vars.number_t_random <= 75 then
		RunSpawn("Player", {"pz_usa_soldiers_1_3", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_04 = 2
	elseif msd_vars.flak_04 == 1 and IsDifficultyLevel("hardcore") and msd_vars.number_t_random > 75 and msd_vars.number_t_random <= 100 then
		RunSpawn("Player", {"pz_usa_soldiers_1_4", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 12)
		msd_vars.flak_04 = 2
	end
end

function func_Patrol_pz4()
	if NumGroupUnits("Defence_Town_02", 0, false, false, false) < 5 then
		AIparts:RemovePeriodic("Patrol_pz4")
	end
	if not IsBridgeBroken(6535) then
		groups.ResetTemplate("pz_4")
		groups.CreateTemplate("pz_4", groups.ttypes.patrol,
			{ call_for_help = false, call_for_howitzer = false, reinf_lost = 0,
			points = {"pz_Town_2","4_1","pz_Town_1","pz_1_2","pz_2_2","4_1","pz_Town_2", }, wait_time = {180,60,120,120,120,120,180, },
			spread_rad = 15, assault = true, group_move = false, circle = true })
		groups.AssignTemplate("pz_4", "pz_4")
		AIparts:UpdatePeriodic("Patrol_pz4", 5871)
	end
	if IsBridgeBroken(6535) then
		groups.ResetTemplate("pz_4")
		groups.CreateTemplate("pz_4", groups.ttypes.patrol,
			{ call_for_help = false, call_for_howitzer = false, reinf_lost = 0,
			points = {"pz_Town_2","4_1","F_2_2","pz_3_3","F_1_1","pz_Town_2", }, wait_time = {180,60,120,120,120,180, },
			spread_rad = 15, assault = true, group_move = false, circle = true })
		groups.AssignTemplate("pz_4", "pz_4")
		AIparts:UpdatePeriodic("Patrol_pz4", 5871)
	end
end

function func_Spawn_pz_1()
	if NumGroupUnits("Defence_Town_01", 0, false, false, false) < 5 then
		AIparts:RemovePeriodic("Spawn_pz_1")
	end
	do
		AIparts:UpdatePeriodic("Spawn_pz_1", 51)
		msd_vars.pz_random_spawn_1 = RandomHalf(100)
	end
	if NumGroupUnits("pz_1", 0, true, false, false) == 0 then
		msd_vars.pz_1 = 0
	else
		msd_vars.pz_1 = 1
	end
	if msd_vars.pz_1 == 0 and msd_vars.pz_spawn_1 == 1 and msd_vars.pz_random_spawn_1 < 25 then
		RunSpawn("Enemy", {"pz_1_1", }, "Spawn_1", "pz_1", "", true, true, {}, false, 12)
		AIparts:UpdatePeriodic("Spawn_pz_1", 5871)
	end
	if msd_vars.pz_1 == 0 and msd_vars.pz_spawn_1 == 1 and msd_vars.pz_random_spawn_1 >= 25 and msd_vars.pz_random_spawn_1 < 50 then
		RunSpawn("Enemy", {"pz_1_2", }, "Spawn_1", "pz_1", "", true, true, {}, false, 12)
		AIparts:UpdatePeriodic("Spawn_pz_1", 12871)
	end
	if msd_vars.pz_1 == 0 and msd_vars.pz_spawn_1 == 1 and msd_vars.pz_random_spawn_1 >= 50 and msd_vars.pz_random_spawn_1 < 75 then
		RunSpawn("Enemy", {"pz_1_3", }, "Spawn_1", "pz_1", "", true, true, {}, false, 12)
		AIparts:UpdatePeriodic("Spawn_pz_1", 5871)
	end
	if msd_vars.pz_1 == 0 and msd_vars.pz_spawn_1 == 1 and msd_vars.pz_random_spawn_1 >= 75 then
		RunSpawn("Enemy", {"pz_1_4", }, "Spawn_1", "pz_1", "", true, true, {}, false, 12)
		AIparts:UpdatePeriodic("Spawn_pz_1", 15871)
	end
end

function func_Spawn_pz_2()
	if NumGroupUnits("Defence_Town_01", 0, false, false, false) < 5 then
		AIparts:RemovePeriodic("Spawn_pz_2")
	end
	do
		AIparts:UpdatePeriodic("Spawn_pz_2", 51)
		msd_vars.pz_random_spawn_2 = RandomHalf(100)
	end
	if NumGroupUnits("pz_2", 0, true, false, false) == 0 then
		msd_vars.pz_2 = 0
	else
		msd_vars.pz_2 = 1
	end
	if msd_vars.pz_2 == 0 and msd_vars.pz_spawn_1 == 1 and msd_vars.pz_random_spawn_2 < 25 then
		RunSpawn("Enemy", {"pz_2_1", }, "Spawn_1", "pz_2", "", true, true, {}, false, 12)
		AIparts:UpdatePeriodic("Spawn_pz_2", 9871)
	end
	if msd_vars.pz_2 == 0 and msd_vars.pz_spawn_1 == 1 and msd_vars.pz_random_spawn_2 >= 25 and msd_vars.pz_random_spawn_2 < 50 then
		RunSpawn("Enemy", {"pz_2_2", }, "Spawn_1", "pz_2", "", true, true, {}, false, 12)
		AIparts:UpdatePeriodic("Spawn_pz_2", 5871)
	end
	if msd_vars.pz_2 == 0 and msd_vars.pz_spawn_1 == 1 and msd_vars.pz_random_spawn_2 >= 50 and msd_vars.pz_random_spawn_2 < 75 then
		RunSpawn("Enemy", {"pz_2_3", }, "Spawn_1", "pz_2", "", true, true, {}, false, 12)
		AIparts:UpdatePeriodic("Spawn_pz_2", 15871)
	end
	if msd_vars.pz_2 == 0 and msd_vars.pz_spawn_1 == 1 and msd_vars.pz_random_spawn_2 >= 75 then
		RunSpawn("Enemy", {"pz_2_4", }, "Spawn_1", "pz_2", "", true, true, {}, false, 12)
		AIparts:UpdatePeriodic("Spawn_pz_2", 17000)
	end
end

function func_Spawn_pz_3()
	if NumGroupUnits("Defence_Town_02", 0, false, false, false) < 5 then
		AIparts:RemovePeriodic("Spawn_pz_3")
	end
	do
		AIparts:UpdatePeriodic("Spawn_pz_3", 51)
		msd_vars.pz_random_spawn_3 = RandomHalf(100)
	end
	if NumGroupUnits("pz_3", 0, true, false, false) == 0 then
		msd_vars.pz_3 = 0
	else
		msd_vars.pz_3 = 1
	end
	if msd_vars.pz_3 == 0 and msd_vars.pz_spawn_2 == 1 and msd_vars.pz_random_spawn_3 < 25 then
		RunSpawn("Enemy", {"pz_3_1", }, "Spawn_2", "pz_3", "", true, true, {}, false, 12)
		AIparts:UpdatePeriodic("Spawn_pz_3", 5871)
	end
	if msd_vars.pz_3 == 0 and msd_vars.pz_spawn_2 == 1 and msd_vars.pz_random_spawn_3 >= 25 and msd_vars.pz_random_spawn_3 < 50 then
		RunSpawn("Enemy", {"pz_3_2", }, "Spawn_2", "pz_3", "", true, true, {}, false, 12)
		AIparts:UpdatePeriodic("Spawn_pz_3", 5871)
	end
	if msd_vars.pz_3 == 0 and msd_vars.pz_spawn_2 == 1 and msd_vars.pz_random_spawn_3 >= 50 and msd_vars.pz_random_spawn_3 < 75 then
		RunSpawn("Enemy", {"pz_3_3", }, "Spawn_2", "pz_3", "", true, true, {}, false, 12)
		AIparts:UpdatePeriodic("Spawn_pz_3", 5871)
	end
	if msd_vars.pz_3 == 0 and msd_vars.pz_spawn_2 == 1 and msd_vars.pz_random_spawn_3 >= 75 then
		RunSpawn("Enemy", {"pz_3_4", }, "Spawn_2", "pz_3", "", true, true, {}, false, 12)
		AIparts:UpdatePeriodic("Spawn_pz_3", 5871)
	end
end

function func_Spawn_pz_4()
	if NumGroupUnits("Defence_Town_02", 0, false, false, false) < 5 then
		AIparts:RemovePeriodic("Spawn_pz_4")
	end
	do
		AIparts:UpdatePeriodic("Spawn_pz_4", 51)
		msd_vars.pz_random_spawn_4 = RandomHalf(100)
	end
	if NumGroupUnits("pz_4", 0, true, false, false) == 0 then
		msd_vars.pz_4 = 0
	else
		msd_vars.pz_4 = 1
	end
	if msd_vars.pz_4 == 0 and msd_vars.pz_spawn_2 == 1 and msd_vars.pz_random_spawn_4 < 25 then
		RunSpawn("Enemy", {"pz_4_1", }, "Spawn_2", "pz_4", "", true, true, {}, false, 12)
		AIparts:UpdatePeriodic("Spawn_pz_4", 11871)
	end
	if msd_vars.pz_4 == 0 and msd_vars.pz_spawn_2 == 1 and msd_vars.pz_random_spawn_4 >= 25 and msd_vars.pz_random_spawn_4 < 50 then
		RunSpawn("Enemy", {"pz_4_2", }, "Spawn_2", "pz_4", "", true, true, {}, false, 12)
		AIparts:UpdatePeriodic("Spawn_pz_4", 15871)
	end
	if msd_vars.pz_4 == 0 and msd_vars.pz_spawn_2 == 1 and msd_vars.pz_random_spawn_4 >= 50 and msd_vars.pz_random_spawn_4 < 75 then
		RunSpawn("Enemy", {"pz_4_3", }, "Spawn_2", "pz_4", "", true, true, {}, false, 12)
		AIparts:UpdatePeriodic("Spawn_pz_4", 5871)
	end
	if msd_vars.pz_4 == 0 and msd_vars.pz_spawn_2 == 1 and msd_vars.pz_random_spawn_4 >= 75 then
		RunSpawn("Enemy", {"pz_4_4", }, "Spawn_2", "pz_4", "", true, true, {}, false, 12)
		AIparts:UpdatePeriodic("Spawn_pz_4", 9871)
	end
end

function func_Spawn_pz_5()
	if NumGroupUnits("Defence_Town_02", 0, false, false, false) < 5 then
		AIparts:RemovePeriodic("Spawn_pz_5")
	end
	if not msd_vars.usa_ammo ~= 1 then
		return
	end
	do
		AIparts:UpdatePeriodic("Spawn_pz_5", 51)
		msd_vars.pz_random_spawn_5 = RandomHalf(100)
	end
	if NumGroupUnits("pz_5", 0, true, false, false) == 0 then
		msd_vars.pz_5 = 0
	else
		msd_vars.pz_5 = 1
	end
	if msd_vars.pz_5 == 0 and msd_vars.pz_spawn_2 == 1 and msd_vars.pz_random_spawn_5 < 25 then
		RunSpawn("Enemy", {"pz_5_1", }, "Spawn_2", "pz_5", "", true, true, {}, false, 12)
		AIparts:UpdatePeriodic("Spawn_pz_5", 5871)
	end
	if msd_vars.pz_5 == 0 and msd_vars.pz_spawn_2 == 1 and msd_vars.pz_random_spawn_5 >= 25 and msd_vars.pz_random_spawn_5 < 50 then
		RunSpawn("Enemy", {"pz_5_2", }, "Spawn_2", "pz_5", "", true, true, {}, false, 12)
		AIparts:UpdatePeriodic("Spawn_pz_5", 6871)
	end
	if msd_vars.pz_5 == 0 and msd_vars.pz_spawn_2 == 1 and msd_vars.pz_random_spawn_5 >= 50 and msd_vars.pz_random_spawn_5 < 75 then
		RunSpawn("Enemy", {"pz_5_3", }, "Spawn_2", "pz_5", "", true, true, {}, false, 12)
		AIparts:UpdatePeriodic("Spawn_pz_5", 7871)
	end
	if msd_vars.pz_5 == 0 and msd_vars.pz_spawn_2 == 1 and msd_vars.pz_random_spawn_5 >= 75 then
		RunSpawn("Enemy", {"pz_5_4", }, "Spawn_2", "pz_5", "", true, true, {}, false, 12)
		AIparts:UpdatePeriodic("Spawn_pz_5", 8871)
	end
end

function func_PresetS()
	do
		groups.CreateTemplate("pz_1", groups.ttypes.patrol,
			{ call_for_help = true, call_for_howitzer = false, reinf_lost = 0,
			points = {"pz_Town_1","pz_1_2","pz_1_3","pz_1_4","pz_1_5", }, wait_time = {240,45,45,45,90, },
			spread_rad = 15, assault = true, group_move = false, circle = true })
		groups.CreateTemplate("pz_2", groups.ttypes.patrol,
			{ call_for_help = false, call_for_howitzer = false, reinf_lost = 0,
			points = {"pz_Town_1","pz_2_1","pz_2_2","pz_1_2","pz_2_3","pz_2_4", }, wait_time = {240,45,45,120,45,60, },
			spread_rad = 15, assault = true, group_move = false, circle = true })
		groups.CreateTemplate("pz_3", groups.ttypes.patrol,
			{ call_for_help = false, call_for_howitzer = false, reinf_lost = 0,
			points = {"pz_Town_2","F_1_1","F_3_2","pz_3_3","pz_3_4","pz_3_5","pz_Town_2", }, wait_time = {180,120,120,120,60,120,120, },
			spread_rad = 15, assault = true, group_move = false, circle = true })
		groups.CreateTemplate("pz_5", groups.ttypes.patrol,
			{ call_for_help = false, call_for_howitzer = false, reinf_lost = 0,
			points = {"pz_Town_2","F_4_1","usa_ammo_fuel","Town_2", }, wait_time = {180,120,120,180, },
			spread_rad = 15, assault = true, group_move = false, circle = true })
		groups.AssignTemplate("pz_1", "pz_1")
		groups.AssignTemplate("pz_2", "pz_2")
		groups.AssignTemplate("pz_3", "pz_3")
		groups.AssignTemplate("pz_5", "pz_5")
	end
	do
		groups.CreateTemplate("Defence", groups.ttypes.defence, { call_for_help = true, call_for_howitzer = false, reinf_lost = 0 })
		groups.AssignTemplate("Defence", "village_01")
		groups.AssignTemplate("Defence", "village_02")
		groups.AssignTemplate("Defence", "village_03")
		groups.AssignTemplate("Defence", "village_04")
		groups.AssignTemplate("Defence", "Defence_Town_01_Fuel")
		groups.AssignTemplate("Defence", "Defence_Town_01_Supply")
		groups.AssignTemplate("Defence", "Defence_Town_02_Fuel")
		groups.AssignTemplate("Defence", "Defence_Town_02_Supply")
	end
	do
		groups.CreateTemplate("Flak_1", groups.ttypes.attack,
			{ call_for_help = false, call_for_howitzer = false, reinf_lost = 0,
			attack_adv = 1.00, attack_timeout = 15, attack_delay = 15, distance = 50, retreat_adv = 0.50,
			points = {"F_1_1","F_1_2", }, group_move = true,
			spread_rad = 15, wait_time = 60})
		groups.CreateTemplate("Flak_2", groups.ttypes.attack,
			{ call_for_help = false, call_for_howitzer = false, reinf_lost = 0,
			attack_adv = 1.00, attack_timeout = 15, attack_delay = 15, distance = 50, retreat_adv = 0.50,
			points = {"F_2_1","F_2_2", }, group_move = true,
			spread_rad = 15, wait_time = 60})
		groups.CreateTemplate("Flak_3", groups.ttypes.attack,
			{ call_for_help = false, call_for_howitzer = false, reinf_lost = 0,
			attack_adv = 1.00, attack_timeout = 15, attack_delay = 15, distance = 50, retreat_adv = 0.50,
			points = {"F_3_1","F_3_2", }, group_move = true,
			spread_rad = 15, wait_time = 60})
		groups.CreateTemplate("Flak_4", groups.ttypes.attack,
			{ call_for_help = false, call_for_howitzer = false, reinf_lost = 0,
			attack_adv = 1.00, attack_timeout = 15, attack_delay = 15, distance = 50, retreat_adv = 0.50,
			points = {"F_4_1","F_4_2", }, group_move = true,
			spread_rad = 15, wait_time = 60})
	end
	do
		groups.CreateTemplate("Town2_1", groups.ttypes.patrol,
			{ call_for_help = false, call_for_howitzer = false, reinf_lost = 0,
			points = {"F_1_1","F_2_2","F_3_1","F_3_2","F_4_2","Town_2", }, wait_time = {120,120,120,120,120,120, },
			spread_rad = 15, assault = true, group_move = false, circle = true })
		groups.CreateTemplate("Town2_2", groups.ttypes.patrol,
			{ call_for_help = false, call_for_howitzer = false, reinf_lost = 0,
			points = {"F_3_2","F_4_1","F_3_1","pz_3_5","F_4_2", }, wait_time = {120,120,120,120,120, },
			spread_rad = 15, assault = true, group_move = false, circle = false })
		groups.CreateTemplate("Town2_3", groups.ttypes.patrol,
			{ call_for_help = false, call_for_howitzer = false, reinf_lost = 0,
			points = {"pz_3_5","F_3_2","pz_3_3","pz_3_2","F_4_2","Town_2", }, wait_time = {120,45,120,120,45,120, },
			spread_rad = 15, assault = true, group_move = false, circle = false })
		groups.CreateTemplate("Town2_4", groups.ttypes.patrol,
			{ call_for_help = false, call_for_howitzer = false, reinf_lost = 0,
			points = {"111","112","F_3_1","F_3_2","F_4_2","pz_3_3", }, wait_time = {120,120,45,120,60,120, },
			spread_rad = 15, assault = true, group_move = false, circle = true })
		groups.AssignTemplate("Town2_1", "Town2_1")
		groups.AssignTemplate("Town2_2", "Town2_2")
		groups.AssignTemplate("Town2_3", "Town2_3")
		groups.AssignTemplate("Town2_4", "Town2_4")
	end
end

function func_CountSappers()
	if IsDifficultyLevel("easy") then
		msd_vars.sappers_count = 20
		AIparts:RemovePeriodic("CountSappers")
	elseif IsDifficultyLevel("regular") then
		msd_vars.sappers_count = 10
		AIparts:RemovePeriodic("CountSappers")
	elseif IsDifficultyLevel("hardcore") then
		msd_vars.sappers_count = 5
		AIparts:RemovePeriodic("CountSappers")
	end
end

function func_Reinforcement_Start()
	if IsDifficultyLevel("easy") then
		RunSpawn("Player", {"Start_Easy", }, "Main_Spawn", "", "", true, true, {}, false, 0)
		AIparts:RemovePeriodic("Reinforcement_Start")
	elseif IsDifficultyLevel("regular") then
		RunSpawn("Player", {"Start_Normal", }, "Main_Spawn", "", "", true, true, {}, false, 0)
		AIparts:RemovePeriodic("Reinforcement_Start")
	elseif IsDifficultyLevel("hardcore") then
		RunSpawn("Player", {"Start_Hard", }, "Main_Spawn", "", "", true, true, {}, false, 0)
		AIparts:RemovePeriodic("Reinforcement_Start")
	end
end

function func_Reinforcement_Town_1()
	if IsDifficultyLevel("easy") and NumPlayerUnitsInZone("Enemy", "Town_1") <= 5 then
		RunSpawn("Player", {"Town_1_Easy", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 0)
		AIparts:RemovePeriodic("Reinforcement_Town_1")
	elseif IsDifficultyLevel("regular") and NumPlayerUnitsInZone("Enemy", "Town_1") <= 5 then
		RunSpawn("Player", {"Town_1_Normal", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 0)
		AIparts:RemovePeriodic("Reinforcement_Town_1")
	elseif IsDifficultyLevel("hardcore") and NumPlayerUnitsInZone("Enemy", "Town_1") <= 5 then
		RunSpawn("Player", {"Town_1_Hard", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 0)
		AIparts:RemovePeriodic("Reinforcement_Town_1")
	end
end

function func_Reinforcement_AmmoFuel1()
	if IsDifficultyLevel("easy") and IsEventPresent("SpawnAmmoFuel") then
		RunSpawn("Player", {"usa_ammofiel_easy", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 5)
		AIparts:RemovePeriodic("Reinforcement_AmmoFuel1")
	elseif IsDifficultyLevel("regular") and IsEventPresent("SpawnAmmoFuel") then
		RunSpawn("Player", {"usa_ammofiel_normal", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 5)
		AIparts:RemovePeriodic("Reinforcement_AmmoFuel1")
	elseif IsDifficultyLevel("hardcore") and IsEventPresent("SpawnAmmoFuel") then
		RunSpawn("Player", {"usa_ammofiel_normal", }, "Usa_ammo1_spawn", "", "", true, true, {}, false, 2)
		AIparts:RemovePeriodic("Reinforcement_AmmoFuel1")
	end
end

function func_Germany_Supply_01()
	if NumGroupUnits("Defence_Town_01", 0, false, false, false) < 5 then
		AIparts:RemovePeriodic("Germany_Supply_01")
	end
	if NumGroupUnits("Defence_Town_01_Fuel", 0, false, false, false) == 0 and msd_vars.pz_spawn_1 == 1 then
		RunSpawn("Enemy", {"germany_fuel", }, "Spawn_1", "Defence_Town_01_Fuel", "", true, true, {}, false, 0)
	end
	if NumGroupUnits("Defence_Town_01_Supply", 0, false, false, false) == 0 and msd_vars.pz_spawn_1 == 1 then
		RunSpawn("Enemy", {"germany_supply", }, "Spawn_1", "Defence_Town_01_Supply", "", true, true, {}, false, 0)
	end
end

function func_Germany_Supply_02()
	if NumGroupUnits("Defence_Town_02", 0, false, false, false) < 5 then
		AIparts:RemovePeriodic("Germany_Supply_02")
	end
	if NumGroupUnits("Defence_Town_02_Fuel", 0, false, false, false) == 0 and msd_vars.pz_spawn_1 == 1 then
		RunSpawn("Enemy", {"germany_fuel", }, "Spawn_2", "Defence_Town_02_Fuel", "", true, true, {}, false, 0)
	end
	if NumGroupUnits("Defence_Town_02_Supply", 0, false, false, false) == 0 and msd_vars.pz_spawn_1 == 1 then
		RunSpawn("Enemy", {"germany_supply", }, "Spawn_2", "Defence_Town_02_Supply", "", true, true, {}, false, 0)
	end
end

function func_Flak_All()
	if msd_vars.flak_01 == 2 and msd_vars.flak_02 == 2 and msd_vars.flak_03 == 2 and msd_vars.flak_04 == 2 then
		SendPlane("Player", "Flak_01", "transport", "S", 0, 4, "Flak_1")
		SendPlane("Player", "Flak_01", "transport", "S", 0, 4, "Flak_1")
		SendPlane("Player", "Flak_02", "transport", "S", 0, 3, "Flak_2")
		SendPlane("Player", "Flak_02", "transport", "S", 0, 5, "Flak_2")
		SendPlane("Player", "Flak_03", "transport", "S", 0, 6, "Flak_3")
		SendPlane("Player", "Flak_03", "transport", "S", 0, 4, "Flak_3")
		SendPlane("Player", "Flak_04", "transport", "S", 0, 4, "Flak_4")
		SendPlane("Player", "Flak_04", "transport", "S", 0, 4, "Flak_4")
		groups.AssignTemplate("Flak_1", "Flak_1")
		groups.AssignTemplate("Flak_2", "Flak_2")
		groups.AssignTemplate("Flak_3", "Flak_3")
		groups.AssignTemplate("Flak_4", "Flak_4")
		AIparts:RemovePeriodic("Flak_All")
	end
end

function func_ObjectivTownComplited()
	if NumPlayerUnitsInZone("Enemy", "Town_2") <= 2 then
		SetObjectiveComplete("Town", "Player")
		SendPlane("Allied", "pz_Town_2", "bomber", "S", 15, 0, "")
		SendPlane("Allied", "pz_Town_2", "bomber", "S", 15, 5, "")
		SendPlane("Allied", "pz_Town_2", "bomber", "S", 15, 1, "")
		SendPlane("Allied", "pz_Town_2", "bomber", "S", 15, 6, "")
		AIparts:RemovePeriodic("ObjectivTownComplited")
	end
end

