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

function PostCreate()
	mission_lua.BasicMissionInitialize()
	AIparts:AddOneTime("set_init_presets", 10, func_set_init_presets)
	AIparts:AddOneTime("Easy_Hard", 11, func_Easy_Hard)
	AIparts:AddPeriodic("american_bonus_reinf", 151, func_american_bonus_reinf, 1200 + RandomHalf(151))
	AIparts:AddPeriodic("british_bonus_reinf", 123, func_british_bonus_reinf, 1800 + RandomHalf(123))
	AIparts:AddPeriodic("american_init_spawn", 10, func_american_init_spawn, 0 + RandomHalf(10))
	AIparts:AddPeriodic("american_init_tanks", 77, func_american_init_tanks, 1800 + RandomHalf(77))
	AIparts:AddPeriodic("allies_reinforcement_spawn", 151, func_allies_reinforcement_spawn, 2400 + RandomHalf(151))
	AIparts:AddOneTime("group_counters", 1031, func_group_counters)
	AIparts:AddPeriodic("kicking_allie_forces", 51, func_kicking_allie_forces, 0 + RandomHalf(51))
	AIparts:AddPeriodic("german_reinf", 93, func_german_reinf, 0 + RandomHalf(93))
	AIparts:AddPeriodic("unload", 73, func_unload, 0 + RandomHalf(73))
	AIparts:AddPeriodic("trancfer_units", 93, func_trancfer_units, 100 + RandomHalf(93))
	AIparts:AddPeriodic("objective_center", 110, func_objective_center, 0 + RandomHalf(110))
	AIparts:AddPeriodic("objective_british_positions", 170, func_objective_british_positions, 300 + RandomHalf(170))
	AIparts:AddPeriodic("objective_guns", 83, func_objective_guns, 300 + RandomHalf(83))
	AIparts:AddPeriodic("objective_town", 193, func_objective_town, 1851 + RandomHalf(193))
	AIparts:AddPeriodic("win", 170, func_win, 6000 + RandomHalf(170))
	AIparts:AddPeriodic("defeat", 241, func_defeat, 6084 + RandomHalf(241))
	AIparts:AddPeriodic("spawn_switch_1", 211, func_spawn_switch_1, 5555 + RandomHalf(211))
	AIparts:AddPeriodic("spawn_switch_2", 197, func_spawn_switch_2, 5999 + RandomHalf(197))
	AIparts:AddOneTime("camera_moving", 4, func_camera_moving)
	AIparts:AddOneTime("Red_Green", 10, func_Red_Green)
	AIparts:AddPeriodic("bombardment", 76, func_bombardment, 600 + RandomHalf(76))
	AIparts:AddOneTime("init_bombardment", 100, func_init_bombardment)
	AIparts:AddPeriodic("figly_migly", 153, func_figly_migly, 3000 + RandomHalf(153))
	AIparts:AddPeriodic("bonus_score", 211, func_bonus_score, 30 + RandomHalf(211))
	AIparts:AddPeriodic("PROTECTION", 1800, func_PROTECTION, 9950 + RandomHalf(1800))
	AIparts:AddPeriodic("AirStricke", 3600, func_AirStricke, 163 + RandomHalf(3600))
	msd_vars = {}
	msd_vars.airstricke_1 = 1
	msd_vars.airstricke_2 = 1
	msd_vars.american_bonus_tanks_spawn_count = 1
	msd_vars.american_init_spawn_count = 1
	msd_vars.american_init_tanks_spawn_count = 1
	msd_vars.american_reinf_timeout = 1
	msd_vars.american_reinf_timeout_01 = 1
	msd_vars.american_units_count = 0
	msd_vars.bombardment_switch = 1
	msd_vars.british_reinf_bonus_count = 1
	msd_vars.british_reinf_timeout = 1
	msd_vars.camera_coordinates = 0
	msd_vars.delta = 0
	msd_vars.german_reinf_timeout_01 = 1
	msd_vars.german_reinf_timeout_02 = 1
	msd_vars.german_spawn_count = 0
	msd_vars.italian_spawn_count = 0
	msd_vars.level_easy = 0
	msd_vars.level_hardcore = 0
	msd_vars.level_regular = 0
	msd_vars.random = 0
	msd_vars.rp = 1
	msd_vars.score = 0
	msd_vars.spawn_gate_switch_1 = 1
	msd_vars.spawn_gate_switch_2 = 1
	msd_vars.temp = 0
	AIparts:AddPeriodic("CheckCountdownTimer", 53, CheckCountdownTimer, 10)
end

function func_set_init_presets()
	do
		groups.CreateTemplate("attack_platoon", groups.ttypes.attack,
			{ call_for_help = false, call_for_howitzer = true, reinf_lost = 50,
			attack_adv = 1.00, attack_timeout = 15, attack_delay = 15, distance = 30, retreat_adv = 0.50,
			points = { }, group_move = true,
			spread_rad = 15, wait_time = 60})
		groups.CreateTemplate("ZH_01", groups.ttypes.zone_help,
			{ enemy = "Allies", call_for_help = false, call_for_howitzer = true, reinf_lost = 30,
			zones = {"PLATOON_1","PLATOON_2","PLATOON_3","Center", },
			min_enemies = {5,5,5,5, },
			times2help = {0,0,0,0, },
			help_time = {90,90,90,90, },
			ignore_time = {30,30,30,30, },
			priority = {25,100,25,150, },
			assault = {false,false,false,false, },
			group_move = {false,false,false,false, },
			home_loc = "Center" })
		groups.CreateTemplate("ZH_02", groups.ttypes.zone_help,
			{ enemy = "Allies", call_for_help = false, call_for_howitzer = true, reinf_lost = 30,
			zones = {"Center", },
			min_enemies = {5, },
			times2help = {0, },
			help_time = {90, },
			ignore_time = {30, },
			priority = {0, },
			assault = {false, },
			group_move = {false, },
			home_loc = "Center" })
		groups.CreateTemplate("ZH_03", groups.ttypes.zone_help,
			{ enemy = "Allies", call_for_help = false, call_for_howitzer = true, reinf_lost = 30,
			zones = {"attack_zone_1","Center", },
			min_enemies = {5,5, },
			times2help = {0,0, },
			help_time = {90,90, },
			ignore_time = {30,30, },
			priority = {0,50, },
			assault = {false,false, },
			group_move = {false,false, },
			home_loc = "Center" })
		groups.CreateTemplate("ZH_04", groups.ttypes.zone_help,
			{ enemy = "Allies", call_for_help = false, call_for_howitzer = true, reinf_lost = 30,
			zones = {"attack_zone_1","Center","PLATOON_2", },
			min_enemies = {5,5,5, },
			times2help = {0,0,0, },
			help_time = {90,90,90, },
			ignore_time = {30,30,30, },
			priority = {50,0,25, },
			assault = {false,false,false, },
			group_move = {false,false,false, },
			home_loc = "" })
		groups.CreateTemplate("ZH_05", groups.ttypes.zone_help,
			{ enemy = "Allies", call_for_help = false, call_for_howitzer = true, reinf_lost = 30,
			zones = {"position_1","position_2","position_3", },
			min_enemies = {5,5,5, },
			times2help = {0,0,0, },
			help_time = {90,90,90, },
			ignore_time = {30,30,30, },
			priority = {25,50,25, },
			assault = {false,false,false, },
			group_move = {false,false,false, },
			home_loc = "Center" })
		groups.CreateTemplate("ZH_06", groups.ttypes.zone_help,
			{ enemy = "Allies", call_for_help = false, call_for_howitzer = true, reinf_lost = 30,
			zones = {"position_town_01","position_town_02","position_town_03","position_town_04", },
			min_enemies = {5,5,5,5, },
			times2help = {0,0,0,0, },
			help_time = {90,90,90,90, },
			ignore_time = {30,30,30,30, },
			priority = {25,50,25,50, },
			assault = {false,false,false,false, },
			group_move = {false,false,false,false, },
			home_loc = "Center" })
		groups.CreateTemplate("reinforcement_01", groups.ttypes.reinforce,
			{ call_for_help = false, call_for_howitzer = true, reinf_lost = 0,
			groups_list = {"platoon_attack_03","platoon_attack_02","zone_help_center","zone_help_south","zone_help_c_a", }, alert_rad = 0, assault = true, spread_rad = 30,
			empty_groups = {"reinf_01_01","reinf_01_02","reinf_01_03", }, group_move = false, timeout = 40 })
		groups.CreateTemplate("reinforcement_02", groups.ttypes.reinforce,
			{ call_for_help = false, call_for_howitzer = true, reinf_lost = 0,
			groups_list = {"platoon_attack_04","platoon_attack_01","zone_help_center","zone_help_south_2","zone_help_c_a_italian","zone_help_c_a", }, alert_rad = 30, assault = true, spread_rad = 30,
			empty_groups = {"reinf_02_01","reinf_02_02","reinf_02_03", }, group_move = false, timeout = 40 })
		groups.CreateTemplate("mortars", groups.ttypes.howitzer,
			{ call_for_help = false, call_for_howitzer = false, reinf_lost = 0,
			groups_list = { }, alert_rad = 200,
			num_voley = 2, advantage = 0.80, shot_timeout = 15, rest_time = 30, safe_rad = 30, min_ammo = 0})
		groups.CreateTemplate("rp_02", groups.ttypes.patrol,
			{ call_for_help = false, call_for_howitzer = true, reinf_lost = 0,
			points = {"Beach_1","attack_zone_1","Center","position_town_03","position_town_02", }, wait_time = {5,5,5,5,5, },
			spread_rad = 15, assault = true, group_move = false, circle = true })
		groups.CreateTemplate("rp_01", groups.ttypes.patrol,
			{ call_for_help = false, call_for_howitzer = true, reinf_lost = 0,
			points = {"Beach_3","PLATOON_3","position_2","position_3","attack_zone_1","Center","PLATOON_2", }, wait_time = {5,5,5,5,5,5,5, },
			spread_rad = 15, assault = true, group_move = false, circle = true })
	end
	do
		groups.AssignTemplate("ZH_01", "platoon_attack_01")
		groups.AssignTemplate("ZH_01", "platoon_attack_02")
		groups.AssignTemplate("ZH_01", "platoon_attack_03")
		groups.AssignTemplate("ZH_01", "platoon_attack_04")
		groups.AssignTemplate("ZH_02", "zone_help_center")
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_DO_"), 120*10, function()  groups.AssignTemplate("ZH_03", "zone_help_south") end )
		groups.AssignTemplate("ZH_04", "zone_help_south_2")
		groups.AssignTemplate("ZH_05", "zone_help_c_a")
		groups.AssignTemplate("ZH_06", "zone_help_c_a_italian")
		groups.AssignTemplate("reinforcement_01", "reinforcement_01")
		groups.AssignTemplate("reinforcement_02", "reinforcement_02")
		groups.AssignTemplate("mortars", "mortars_01")
		groups.AssignTemplate("mortars", "mortars_02")
		groups.AssignTemplate("mortars", "mortars_03")
		groups.AssignTemplate("mortars", "mortars_04")
		groups.AssignTemplate("mortars", "mortars_05")
		groups.AssignTemplate("mortars", "mortars_06")
		groups.AssignTemplate("mortars", "mortars_08")
		groups.AssignTemplate("mortars", "mortars_09")
		groups.AssignTemplate("mortars", "mortars_10")
		groups.AssignTemplate("mortars", "mortars_11")
		groups.AssignTemplate("rp_01", "rp_01")
		groups.AssignTemplate("rp_02", "rp_02")
	end
	do
		mission.CameraLock(true)
	end
end

function func_Easy_Hard()
	if IsDifficultyLevel("easy") then
		msd_vars.level_easy = 1
		msd_vars.score = 30000
		msd_vars.delta = 30000
	end
	if IsDifficultyLevel("regular") then
		msd_vars.level_regular = 1
		msd_vars.score = 50000
		msd_vars.delta = 50000
	end
	if IsDifficultyLevel("hardcore") then
		msd_vars.level_hardcore = 1
		msd_vars.score = 80000
		msd_vars.delta = 80000
	end
end

function func_american_bonus_reinf()
	if msd_vars.american_reinf_timeout_01 == 1 and msd_vars.american_bonus_tanks_spawn_count <= 2 and IsObjectiveCompleted("Help the British", "Allies") then
		RunSpawn("Allies", {"american_boat_spg", "lcm_flame", "lcm_flame", "lcm_m3a", }, "respawn_0", "", "", false, true, {{unload=true, }, {dislocation="american_out", }, {disappear=true, }, }, false, 10)
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_DO_"), 180*10, function()  RunSpawn("Allies", {"american_lcm_priest", "american_lcm_priest", "american_boat_tank", "american_boat_tank", }, "respawn_0", "", "", false, true, {{unload=true, }, {dislocation="american_out", }, {disappear=true, }, }, false, 10) end )
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_DO_"), 70*10, function()  TransferUnits("reinforcement_02", 0, "zone_help_south_2") end )
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_DO_"), 73*10, function()  TransferUnits("zone_help_c_a_italian", 0, "zone_help_c_a_italian") end )
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_DO_"), 80*10, function()  MoveGroupToZone("zone_help_c_a_italian", "position_town_01", true, false) end )
		msd_vars.american_reinf_timeout_01 = 0
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_VAR_"), 90*10, function() msd_vars.american_reinf_timeout_01 = 1 end)
		msd_vars.american_bonus_tanks_spawn_count = msd_vars.american_bonus_tanks_spawn_count + 1
	end
	if msd_vars.american_bonus_tanks_spawn_count > 2 then
		msd_vars.american_bonus_tanks_spawn_count = 1
		AIparts:RemovePeriodic("american_bonus_reinf")
	end
end

function func_british_bonus_reinf()
	if msd_vars.british_reinf_timeout == 1 and msd_vars.british_reinf_bonus_count <= 2 and IsObjectiveCompleted("Suppress the guns", "Allies") then
		RunSpawn("Allies", {"lcm_uk_challenger", "lcvp_riflemans", "lcm_uk_centaur", "lcm_uk_cromwell", "lcm_uk_centaur", }, "respawn_1", "", "", false, true, {{unload=true, }, {dislocation="british_out", }, {disappear=true, }, }, false, 10)
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_DO_"), 90*10, function()  RunSpawn("Allies", {"lcm_uk_centaur", "lcm_uk_centaur", "lcm_uk_cromwell", "lcm_uk_cromwell", }, "respawn_1", "", "", false, true, {{unload=true, }, {dislocation="british_out", }, {disappear=true, }, }, false, 10) end )
		msd_vars.british_reinf_timeout = 0
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_VAR_"), 90*10, function() msd_vars.british_reinf_timeout = 1 end)
		msd_vars.british_reinf_bonus_count = msd_vars.british_reinf_bonus_count + 1
	end
	if msd_vars.british_reinf_bonus_count > 2 then
		msd_vars.british_reinf_bonus_count = 1
		AIparts:RemovePeriodic("british_bonus_reinf")
	end
end

function func_american_init_spawn()
	if msd_vars.american_reinf_timeout == 1 and msd_vars.american_init_spawn_count <= 2 then
		RunSpawn("Allies", {"lcvp_smg", "lcvp_amy", "lcvp_smg", "lcvp_amy", }, "respawn_0", "init", "american_forces", false, true, {{unload=true, }, {dislocation="american_out", }, {disappear=true, }, }, false, 10)
		msd_vars.american_init_spawn_count = msd_vars.american_init_spawn_count + 1
		msd_vars.american_reinf_timeout = 0
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_VAR_"), 60*10, function() msd_vars.american_reinf_timeout = 1 end)
	end
	if msd_vars.american_init_spawn_count > 2 then
		AIparts:RemovePeriodic("american_init_spawn")
	end
end

function func_american_init_tanks()
	if msd_vars.american_reinf_timeout_01 == 1 and msd_vars.american_init_tanks_spawn_count <= 2 then
		RunSpawn("Allies", {"american_boat_tank", "american_boat_light_tank", "lcm_m3a", "american_boat_light_tank", }, "respawn_0", "init", "", false, true, {{unload=true, }, {dislocation="american_out", }, {disappear=true, }, }, false, 10)
		msd_vars.american_init_tanks_spawn_count = msd_vars.american_init_tanks_spawn_count + 1
		msd_vars.american_reinf_timeout_01 = 0
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_VAR_"), 140*10, function() msd_vars.american_reinf_timeout_01 = 1 end)
	end
	if msd_vars.american_init_tanks_spawn_count > 2 and msd_vars.american_reinf_timeout_01 == 1 then
		RunSpawn("Allies", {"lcm_supply", "lcm_supply", "lcm_m3a", "american_boat_infantry_squad_10men", }, "respawn_0", "", "", false, true, {{unload=true, }, {dislocation="american_out", }, {disappear=true, }, }, false, 10)
		msd_vars.american_init_tanks_spawn_count = 1
		log("init tanks removed")
		LogVariable("american_init_tanks_spawn_count = ", msd_vars.american_init_tanks_spawn_count)
		AIparts:RemovePeriodic("american_init_tanks")
	end
end

function func_allies_reinforcement_spawn()
	if msd_vars.american_reinf_timeout_01 == 1 and NumGroupUnits("american_forces", 0, false, false, false) <= 40 then
		RunSpawn("Allies", {"lcvp_smg", "lcvp_smg", "lcvp_smg", "american_boat_light_tank", }, "respawn_0", "", "american_forces", false, true, {{unload=true, }, {dislocation="american_out", }, {disappear=true, }, }, false, 10)
		msd_vars.american_reinf_timeout_01 = 0
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_VAR_"), 120*10, function() msd_vars.american_reinf_timeout_01 = 1 end)
	end
	do
		msd_vars.temp = NumGroupUnits("british_forces", 0, false, false, false)
	end
	if msd_vars.temp <= 30 and msd_vars.british_reinf_timeout == 1 and GetGameTime() >= 900 then
		RunSpawn("Allies", {"lcm_usa_willys_supply", "lcvp_riflemans", "lcvp_riflemans", "lcvp_riflemans", }, "respawn_1", "", "british_forces", false, false, {{unload=true, }, {dislocation="british_out", }, {disappear=true, }, }, false, 10)
		msd_vars.british_reinf_timeout = 0
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_VAR_"), 90*10, function() msd_vars.british_reinf_timeout = 1 end)
	end
	if msd_vars.temp <= 30 and msd_vars.british_reinf_timeout == 1 then
		RunSpawn("Allies", {"lcvp_riflemans", "lcvp_riflemans", "lcvp_riflemans", "lcvp_riflemans", }, "respawn_1", "", "british_forces", false, false, {{unload=true, }, {dislocation="british_out", }, {disappear=true, }, }, false, 10)
		msd_vars.british_reinf_timeout = 0
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_VAR_"), 90*10, function() msd_vars.british_reinf_timeout = 1 end)
	end
end

function func_group_counters()
	do
		UpdateGroupCounters("american_forces")
		msd_vars.american_units_count = NumGroupUnits("american_forces", 0, false, false, false)
	end
end

function func_kicking_allie_forces()
	do
		MoveUnitsFromZoneToZone("Allies", 13,"Beach_1", "attack_zone_1")
		MoveUnitsFromZoneToZone("Allies", 16,"Beach_1", "attack_zone_1")
		MoveUnitsFromZoneToZone("Allies", 18,"Beach_1", "attack_zone_1")
		MoveUnitsFromZoneToZone("Allies", 17,"Beach_1", "attack_zone_1")
		MoveUnitsFromZoneToZone("Allies", 14,"Beach_1", "attack_zone_1")
	end
	do
		MoveUnitsFromZoneToZone("Allies", 13,"Beach_2", "PLATOON_1")
		MoveUnitsFromZoneToZone("Allies", 16,"Beach_2", "PLATOON_1")
		MoveUnitsFromZoneToZone("Allies", 18,"Beach_2", "PLATOON_1")
		MoveUnitsFromZoneToZone("Allies", 17,"Beach_2", "PLATOON_1")
		MoveUnitsFromZoneToZone("Allies", 19,"Beach_2", "PLATOON_1")
		MoveUnitsFromZoneToZone("Allies", 14,"Beach_2", "PLATOON_1")
		MoveUnitsFromZoneToZone("Germans", 13,"Beach_2", "PLATOON_1")
		MoveUnitsFromZoneToZone("Germans", 16,"Beach_2", "PLATOON_1")
	end
	do
		MoveUnitsFromZoneToZone("Allies", 13,"Beach_3", "PLATOON_2")
		MoveUnitsFromZoneToZone("Allies", 16,"Beach_3", "PLATOON_2")
		MoveUnitsFromZoneToZone("Allies", 18,"Beach_3", "PLATOON_2")
		MoveUnitsFromZoneToZone("Allies", 17,"Beach_3", "PLATOON_2")
		MoveUnitsFromZoneToZone("Allies", 19,"Beach_3", "PLATOON_2")
		MoveUnitsFromZoneToZone("Allies", 14,"Beach_3", "PLATOON_2")
		MoveUnitsFromZoneToZone("Germans", 13,"Beach_3", "PLATOON_2")
		MoveUnitsFromZoneToZone("Germans", 16,"Beach_3", "PLATOON_2")
	end
	do
		MoveUnitsFromZoneToZone("Allies", 13,"Beach_3", "PLATOON_2")
		MoveUnitsFromZoneToZone("Allies", 16,"Beach_3", "PLATOON_2")
		MoveUnitsFromZoneToZone("Allies", 18,"Beach_3", "PLATOON_2")
		MoveUnitsFromZoneToZone("Allies", 17,"Beach_3", "PLATOON_2")
		MoveUnitsFromZoneToZone("Allies", 19,"Beach_3", "PLATOON_2")
		MoveUnitsFromZoneToZone("Allies", 14,"Beach_3", "PLATOON_2")
		MoveUnitsFromZoneToZone("Germans", 13,"Beach_3", "PLATOON_2")
		MoveUnitsFromZoneToZone("Germans", 16,"Beach_3", "PLATOON_2")
	end
	do
		MoveUnitsFromZoneToZone("Allies", 13,"Beach_4", "PLATOON_3")
		MoveUnitsFromZoneToZone("Allies", 16,"Beach_4", "PLATOON_3")
		MoveUnitsFromZoneToZone("Allies", 18,"Beach_4", "PLATOON_3")
		MoveUnitsFromZoneToZone("Allies", 17,"Beach_4", "PLATOON_3")
		MoveUnitsFromZoneToZone("Allies", 19,"Beach_4", "PLATOON_3")
		MoveUnitsFromZoneToZone("Allies", 14,"Beach_4", "PLATOON_3")
		MoveUnitsFromZoneToZone("Germans", 13,"Beach_4", "PLATOON_3")
		MoveUnitsFromZoneToZone("Germans", 16,"Beach_4", "PLATOON_3")
	end
end

function func_german_reinf()
	if msd_vars.german_reinf_timeout_01 == 1 and msd_vars.spawn_gate_switch_1 == 1 and NumGroupUnits("reinforcement_01", 0, false, false, true) <= 10 then
		RunSpawn("Germans", {"german_infantry_squad_mehanised_10menVsdkfz_251", "german_infantry_squad_mehanised_10menVsdkfz_251", "german_panzer_squad_2_vehicles", }, "respawn_0", "reinforcement_01", "reinforcement_01", true, true, {}, false, 30)
		msd_vars.german_reinf_timeout_01 = 0
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_VAR_"), 180*10, function() msd_vars.german_reinf_timeout_01 = 1 end)
		msd_vars.german_spawn_count = msd_vars.german_spawn_count + 1
	end
	if msd_vars.german_reinf_timeout_02 == 1 and msd_vars.spawn_gate_switch_2 == 1 and NumGroupUnits("reinforcement_02", 0, false, false, true) <= 10 then
		RunSpawn("Germans", {"italian_tank_squad_2_vehicles_l6", "italian_tank_squad_2_vehicles_m15", "german_infantry_squad_mehanised_10menVsdkfz_251", "german_infantry_squad_mehanised_10menVsdkfz_251", "italian_tank_squad_2_vehicles_p40", }, "respawn_1", "reinforcement_02", "reinforcement_02", true, true, {}, false, 30)
		msd_vars.german_reinf_timeout_02 = 0
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_VAR_"), 180*10, function() msd_vars.german_reinf_timeout_02 = 1 end)
		msd_vars.italian_spawn_count = msd_vars.italian_spawn_count + 1
	end
end

function func_unload()
	if GroupSeeEnemyNow("platoon_attack_01") then
		UnloadTransports("platoon_attack_01")
	end
	if GroupSeeEnemyNow("platoon_attack_02") then
		UnloadTransports("platoon_attack_02")
	end
	if GroupSeeEnemyNow("platoon_attack_03") then
		UnloadTransports("platoon_attack_03")
	end
	if GroupSeeEnemyNow("platoon_attack_04") then
		UnloadTransports("platoon_attack_04")
	end
	if GroupSeeEnemyNow("zone_help_south") then
		UnloadTransports("zone_help_south")
	end
	if GroupSeeEnemyNow("zone_help_south_2") then
		UnloadTransports("zone_help_south_2")
	end
	if GroupSeeEnemyNow("zone_help_center") then
		UnloadTransports("zone_help_center")
	end
	if GroupSeeEnemyNow("zone_help_c_a") then
		UnloadTransports("zone_help_c_a")
	end
	if GroupSeeEnemyNow("zone_help_c_a_italian") then
		UnloadTransports("zone_help_c_a_italian")
	end
	if GroupSeeEnemyNow("reinf_01_01") then
		UnloadTransports("reinf_01_01")
	end
	if GroupSeeEnemyNow("reinf_01_02") then
		UnloadTransports("reinf_01_02")
	end
	if GroupSeeEnemyNow("reinf_01_03") then
		UnloadTransports("reinf_01_03")
	end
	if GroupSeeEnemyNow("reinf_02_01") then
		UnloadTransports("reinf_02_01")
	end
	if GroupSeeEnemyNow("reinf_02_02") then
		UnloadTransports("reinf_02_02")
	end
	if GroupSeeEnemyNow("reinf_02_03") then
		UnloadTransports("reinf_02_03")
	end
end

function func_trancfer_units()
	if NumPlayerUnitsInZone("Allies", "Center") > 5 then
		TransferUnits("platoon_attack_04", 0, "zone_help_center")
		TransferUnits("platoon_attack_02", 0, "zone_help_center")
		AIparts:RemovePeriodic("trancfer_units")
	end
end

function func_objective_center()
	if NumPlayerUnitsInZone("Allies", "Center") > 0 and NumPlayerUnitsInZone("Germans", "Center") <= 5 then
		SetObjectiveTimeout("Take the foothold", 10)
		ProceedObjective("Take the foothold", "Allies")
		AddBonus("Allies", 3, 1, tonumber(string.sub("00000",1,5)))
		mission.OutScreenMessage(tonumber(string.sub("237. Bonus!",1,5)), 2, "0xFFE0E0E0")
		PlayerAddScore(1, 25000)
		AIparts:RemovePeriodic("objective_center")
	end
end

function func_objective_british_positions()
	if NumGroupUnitsInZone("american_forces", "PLATOON_1") > 5 or NumGroupUnitsInZone("american_forces", "PLATOON_2") > 5 or NumGroupUnitsInZone("american_forces", "PLATOON_3") > 5 then
		SetObjectiveTimeout("Help the British", 10)
		ProceedObjective("Help the British", "Allies")
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_DO_"), 20*10, function()  SendPlane("Germans", "Airstrike_1", "bomber", "N", 15) end )
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_DO_"), 21*10, function()  SendPlane("Germans", "Airstrike_2", "bomber", "N", 15) end )
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_DO_"), 22*10, function()  SendPlane("Germans", "Airstrike_3", "bomber", "N", 15) end )
		AddBonus("Allies", 3, 1, tonumber(string.sub("00000",1,5)))
		mission.OutScreenMessage(tonumber(string.sub("237. Bonus!",1,5)), 2, "0xFFE0E0E0")
		mission.OutScreenMessage(tonumber(string.sub("239. Reinforcements will arrive shortly!",1,5)), 3, "0xFFE0E0E0")
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_DO_"), 7*10, function()  mission.OutScreenMessage(tonumber(string.sub("238. Warning! Airstrike is imminent!",1,5)), 3, "0xFFE0E0E0") end )
		PlayerAddScore(1, 50000)
		AIparts:RemovePeriodic("objective_british_positions")
	end
end

function func_objective_guns()
	if NumGroupUnits("Guns", 0, false, false, false) == 0 and ( NumPlayerUnitsInZones("Allies", {"position_2", "german_sp_gate_2", }) >= 10 and NumPlayerUnitsInZones("Germans", {"position_2", "german_sp_gate_2", }) <= 30  ) then
		SetObjectiveTimeout("Suppress the guns", 10)
		ProceedObjective("Suppress the guns", "Allies")
		TransferUnits("reinforcement_02", 0, "zone_help_c_a")
		AddBonus("Allies", 3, 1, tonumber(string.sub("00000",1,5)))
		msd_vars.bombardment_switch = 0
		mission.OutScreenMessage(tonumber(string.sub("237. Bonus!",1,5)), 2, "0xE0FFFFFF")
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_DO_"), 5*10, function()  mission.OutScreenMessage(tonumber(string.sub("239. Reinforcements will arrive shortly!",1,5)), 3, "0xFFE0E0E0") end )
		PlayerAddScore(1, 100000)
		AIparts:RemovePeriodic("objective_guns")
	end
end

function func_objective_town()
	if NumPlayerUnitsInZones("Allies", {"position_town_01", "position_town_02", "position_town_03", "german_sp_gate_2", }) > 10 and NumPlayerUnitsInZones("Germans", {"position_town_01", "position_town_02", "position_town_03", "german_sp_gate_2", }) < 3 then
		SetObjectiveTimeout("Occupy the town", 10)
		ProceedObjective("Occupy the town", "Allies")
		AddBonus("Allies", 3, 1, tonumber(string.sub("00000",1,5)))
		mission.OutScreenMessage(tonumber(string.sub("129. Objective completed!",1,5)), 2, "0xFFE0E0E0")
		PlayerAddScore(1, 100000)
		AIparts:RemovePeriodic("objective_town")
	end
end

function func_win()
	if IsObjectiveCompleted("Help the British", "Allies") and IsObjectiveCompleted("Take the foothold", "Allies") and IsObjectiveCompleted("Occupy the town", "Allies") and IsObjectiveCompleted("Suppress the guns", "Allies") then
		PlayerWin("Allies")
		mission.OutScreenMessage(tonumber(string.sub("124. Mission successful!",1,5)), 5, "0xFFE0E0E0")
	end
end

function func_defeat()
	if NumPlayerUnits("Allies") < 3 then
		PlayerDefeat("Allies")
	end
end

function func_spawn_switch_1()
	if NumPlayerUnitsInZone("Allies", "german_sp_gate_1") > 0 or ( NumPlayerUnitsInZone("Allies", "position_3") > 0 and NumPlayerUnitsInZone("Germans", "position_3") <= 3  ) or ( NumPlayerUnitsInZone("Allies", "position_2") > 0 and NumPlayerUnitsInZone("Germans", "position_2") <= 5  ) then
		msd_vars.spawn_gate_switch_1 = 0
		TransferUnits("reinforcement_01", 0, "zone_help_c_a")
		AIparts:RemovePeriodic("spawn_switch_1")
	end
end

function func_spawn_switch_2()
	if NumPlayerUnitsInZone("Allies", "german_sp_gate_2") > 0 or ( NumPlayerUnitsInZone("Germans", "position_town_01") <= 3 and NumPlayerUnitsInZone("Allies", "position_town_01") > 0  ) or ( NumPlayerUnitsInZone("Germans", "position_town_03") <= 3 and NumPlayerUnitsInZone("Allies", "position_town_03") > 0  ) or ( NumPlayerUnitsInZone("Germans", "position_town_05") <= 3 and NumPlayerUnitsInZone("Allies", "position_town_05") > 0  ) then
		msd_vars.spawn_gate_switch_2 = 0
		TransferUnits("reinforcement_02", 0, "zone_help_c_a_italian")
		AIparts:RemovePeriodic("spawn_switch_2")
	end
end

function func_camera_moving()
	do
		mission.CameraAt({176.00, -0.45, -194.00}, 45.00, 103.00, 101.00, 8.00)
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_DO_"), 8*10, function()  mission.CameraLock(false) end )
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_DO_"), 15*10, function()  mission.OutScreenMessage(tonumber(string.sub("248. Hit CapsLock!",1,5)), 5, "0xFFE0E0E0") end )
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_DO_"), 41*10, function()  mission.OutScreenMessage(tonumber(string.sub("234. Hit F9 and see your current objectives.",1,5)), 5, "0xFFE0E0E0") end )
	end
end

function func_Red_Green()
	do
		ProceedObjective("Help the British", "Germans")
		ProceedObjective("Suppress the guns", "Germans")
		ProceedObjective("Occupy the town", "Germans")
		ProceedObjective("Take the foothold", "Germans")
	end
end

function func_bombardment()
	if msd_vars.bombardment_switch == 1 and NumPlayerUnitsInZone("Allies", "Beach_3") > 0 then
		Bombardment("b_5", 60, 1, 6, 30)
	end
	if msd_vars.bombardment_switch == 1 and NumPlayerUnitsInZone("Allies", "Beach_5") > 0 then
		Bombardment("b_1", 60, 1, 6, 30)
	end
	if msd_vars.bombardment_switch == 1 and NumPlayerUnitsInZone("Allies", "Beach_6") > 0 then
		Bombardment("b_2", 60, 1, 6, 30)
	end
	if msd_vars.bombardment_switch == 1 and NumPlayerUnitsInZone("Allies", "Beach_7") > 0 then
		Bombardment("b_3", 60, 1, 6, 30)
	end
	if msd_vars.bombardment_switch == 1 and NumPlayerUnitsInZone("Allies", "Beach_4") > 0 then
		Bombardment("b_6", 60, 1, 6, 30)
	end
	if msd_vars.bombardment_switch == 0 then
		AIparts:RemovePeriodic("bombardment")
	end
end

function func_init_bombardment()
	do
		Bombardment("b_5", 60, 10, 8, 310)
		Bombardment("b_1", 60, 10, 10, 570)
		Bombardment("b_2", 60, 10, 12, 190)
		Bombardment("b_3", 60, 10, 6, 610)
		Bombardment("b_6", 60, 10, 11, 370)
	end
end

function func_figly_migly()
	if IsObjectiveCompleted("Help the British", "Allies") then
		TransferUnits("platoon_attack_01", 0, "zone_help_south")
		TransferUnits("platoon_attack_02", 0, "zone_help_south")
		TransferUnits("platoon_attack_03", 0, "zone_help_south")
		TransferUnits("platoon_attack_04", 0, "zone_help_south")
		MoveGroupToZone("zone_help_south", "attack_zone_1", false, false)
		AIparts:RemovePeriodic("figly_migly")
	end
end

function func_bonus_score()
	if GetPlayerScore(1) > msd_vars.score then
		mission.OutScreenMessage(tonumber(string.sub("237. Bonus!",1,5)), 2, "0xFFE0E0E0")
		AddBonus("Allies", 2, 2, tonumber(string.sub("00000",1,5)))
		AddBonus("Allies", 3, 2, tonumber(string.sub("00000",1,5)))
		msd_vars.score = msd_vars.score + msd_vars.delta
	end
end

function func_PROTECTION()
	if not mission.BuildingDestroyed() or IsEventPresent("die_mother_fuckers") and msd_vars.rp == 1 then
		RunSpawn("Germans", {"railroad_car_13", "railroad_car_14", }, "rp_spawn_1", "rp_02", "rp_02", true, true, {}, false, 10)
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_DO_"), 10*10, function()  RunSpawn("Germans", {"railroad_car_13", "railroad_car_14", }, "rp_spawn_1", "rp_02", "rp_02", true, true, {}, false, 10) end )
		RunSpawn("Germans", {"railroad_car_13", "railroad_car_13", }, "rp_spawn_2", "rp_01", "rp_01", true, true, {}, false, 10)
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_DO_"), 10*10, function()  RunSpawn("Germans", {"railroad_car_13", "railroad_car_14", }, "rp_spawn_2", "rp_01", "rp_01", true, true, {}, false, 10) end )
		msd_vars.rp = 0
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_VAR_"), 600*10, function() msd_vars.rp = 1 end)
	end
end

function func_AirStricke()
	if msd_vars.airstricke_1 == 1 and NumPlayerUnitsInZoneByType("Allies", "A_0", 16) > 2 then
		SendPlane("Germans", "a_0", "fighter", "W", 30)
		msd_vars.airstricke_1 = 0
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_VAR_"), 300*10, function() msd_vars.airstricke_1 = 1 end) 
	end
	if msd_vars.airstricke_1 == 1 and NumPlayerUnitsInZoneByType("Allies", "A_2", 16) > 2 then
		SendPlane("Germans", "a_2", "fighter", "W", 30)
		msd_vars.airstricke_1 = 0
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_VAR_"), 300*10, function() msd_vars.airstricke_1 = 1 end) 
	end
	if msd_vars.airstricke_1 == 1 and NumPlayerUnitsInZoneByType("Allies", "A_3", 16) > 2 then
		SendPlane("Germans", "a_3", "fighter", "W", 30)
		msd_vars.airstricke_1 = 0
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_VAR_"), 300*10, function() msd_vars.airstricke_1 = 1 end) 
	end
	if msd_vars.airstricke_2 == 1 and NumPlayerUnitsInZoneByType("Allies", "A_5", 16) > 2 then
		SendPlane("Germans", "a_5", "fighter", "E", 30)
		msd_vars.airstricke_2 = 0
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_VAR_"), 180*10, function() msd_vars.airstricke_2 = 1 end) 
	end
	if msd_vars.airstricke_2 == 1 and NumPlayerUnitsInZoneByType("Allies", "A_8", 16) > 2 then
		SendPlane("Germans", "a_8", "fighter", "E", 30)
		msd_vars.airstricke_2 = 0
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_VAR_"), 180*10, function() msd_vars.airstricke_2 = 1 end) 
	end
	if msd_vars.airstricke_2 == 1 and NumPlayerUnitsInZoneByType("Allies", "A_9", 16) > 2 then
		SendPlane("Germans", "a_9", "fighter", "E", 30)
		msd_vars.airstricke_2 = 0
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_VAR_"), 180*10, function() msd_vars.airstricke_2 = 1 end) 
	end
	if msd_vars.airstricke_2 == 1 and NumPlayerUnitsInZoneByType("Allies", "PLATOON_2", 16) > 2 then
		SendPlane("Germans", "a_6", "fighter", "E", 30)
		msd_vars.airstricke_2 = 0
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_VAR_"), 180*10, function() msd_vars.airstricke_2 = 1 end) 
	end
	if msd_vars.airstricke_2 == 1 and NumPlayerUnitsInZoneByType("Allies", "Center", 16) > 2 then
		SendPlane("Germans", "a_4", "fighter", "E", 30)
		msd_vars.airstricke_1 = 0
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_VAR_"), 240*10, function() msd_vars.airstricke_1 = 1 end) 
	end
	if msd_vars.airstricke_1 == 1 and NumPlayerUnitsInZoneByType("Allies", "PLATOON_3", 16) > 2 then
		SendPlane("Germans", "a_7", "fighter", "E", 30)
		msd_vars.airstricke_1 = 0
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_VAR_"), 240*10, function() msd_vars.airstricke_1 = 1 end) 
	end
	if msd_vars.airstricke_1 == 1 and NumPlayerUnitsInZoneByType("Allies", "german_sp_gate_1", 16) > 2 then
		SendPlane("Germans", "a_10", "fighter", "W", 30)
		msd_vars.airstricke_1 = 0
		AIparts:AddOneTime(AIparts:GetUniqueId("TIMER_VAR_"), 240*10, function() msd_vars.airstricke_1 = 1 end) 
	end
end

