import("ScarUtil.scar")


-- UCS loading functions

function Util_CreateLocString(text)
	local tmpstr = LOC(text)
	tmpstr[1] = text
	return tmpstr
end

function Util_CreateLocStringfromID(nID)
	local tmpstr	= Loc_FormatText(nID, Util_CreateLocString(" "))
	return tmpstr
end

function Util_GlobalMessage(title, displaytime, nosound)
	local _ShowMsg = function()
		CTRL.Game_TextTitleFade(title, 0, displaytime, 2, "")
		CTRL.WAIT()
	end

Util_StartIntel(_ShowMsg)

end

function OnGameSetup()

player1 = World_GetPlayerAt(1)
player2 = World_GetPlayerAt(2)

Setup_SetPlayerRace(player1, 0) --US Assault
Setup_SetPlayerRace(player2, 3) --US Air

local player1name = Util_CreateLocStringfromID(19040020)
local player2name = Util_CreateLocStringfromID(19040089)

Setup_SetPlayerName(player1, player1name)
Setup_SetPlayerName(player2, player2name)

Setup_SetPlayerTeam(player1, 1) 
Setup_SetPlayerTeam(player2, 2) 

Rule_AddOneShot(spawndefenders, 1)
Rule_AddOneShot(StartAA, 2)
Rule_AddInterval(attackmanager, 15)

Rule_AddInterval(remove_single_blackhawk, 10)
Rule_AddInterval(remove_single_apache, 10)

--START MISSIONS
Rule_AddOneShot(startAssaultmission, 90)
Rule_AddOneShot(startAmbushmission, 57)
Rule_AddOneShot(startAAmission, 64) --try this mixing with above

--Rule_AddOneShot(startMLRSMedalmission, 70)
--Rule_AddOneShot(killsentriestest, 80)

end



function OnGameRestore()

player1 = World_GetPlayerAt(1)
player2 = World_GetPlayerAt(2)

-- function takes care of restoring all global mission parameters after a save/load
Game_DefaultGameRestore()

end 

-----------------------AUTOEXEC

function OnInit()

--TEST SETUP

Rule_AddOneShot(StartIntro, 0.001)  --*****************ON
Game_FadeToBlack(true, 0) -- TURN ON FOR INTRO
FOW_RevealAll()  
g_dif = Game_GetSPDifficulty()

--STARTING RATES
Modify_PlayerResourceRate(player1, RT_Manpower, 0.001)  --Bonus manpower rate (adjusts later)
Modify_PlayerResourceRate(player1, RT_Munition, 1)  --Bonus Munitions Rate
Modify_PlayerResourceRate(player1, RT_Fuel, 1)  --Bonus fuel rate

if Misc_IsCommandLineOptionSet("dev") then
Scar_DebugConsoleExecute("bind([[ALT+1]], [[Scar_DoString('XDebug1()')]])")
Scar_DebugConsoleExecute("bind([[ALT+9]], [[setsimrate(10)]])")
Scar_DebugConsoleExecute("bind([[ALT+0]], [[setsimrate(100)]])")
end 

--DIFFICULTY SETTINGS

	if g_dif == 0 then
		
		--How much % health sentries have left before attempting to retreat and raise the alarm - e.g. 0.2 = 20%
		Retreat_Threshold_Beneath = 0.2
		Retreat_Threshold_Above = 0.19	
		
		Player_SetPopCapOverride(player1, 100)  --Set player pop cap
		Player_SetResource(player1, RT_Manpower, 2000)  --Starting Manpower
		Player_SetResource(player1, RT_Munition, 1500)  -- Starting Munitions
		Player_SetResource(player1, RT_Fuel, 750)  --Starting Fuel
	
		Modify_PlayerResourceRate(player1, RT_Manpower, 1)  --Bonus manpower rate (adjusts later)
		Modify_PlayerResourceRate(player1, RT_Munition, 2)  --Bonus Munitions Rate (x2)
		Modify_PlayerResourceRate(player1, RT_Fuel, 2)  --Bonus fuel rate
		Modify_PlayerProductionRate(player1, 1.4)  --How fast player building make units (x1.4 = 40% faster)
			
		--Starting Patrol Unit Possible Types
		types_sentryinf = {SBP.ELITE.MILITIA, SBP.ELITE.MILITIA, SBP.ELITE.LIGHT_AT}
		Types_Jeep = {SBP.ELITE.ATMP_RECON, SBP.ELITE.ATMP_RECON, SBP.ELITE.ATMP_RECON}

		--Mission Timers, in minutes
		InfiltrateTimer = 12  --how long before the assault mission starts
		BeachheadTimer = 16  --how long you have to capture the beachhead point
		DiginTimer = 5   --how long you have to prepare defences before the counterattack	
		
		--US Beach Reinforcement Numbers (Max total Reinforcements you can receive)
		usinfrein = 24  --Infantry, some javelins and MGs
		usapcrein = 8  --APCs, strykers and Bradleys
		ustankrein = 5  --M1A2 Abrams only
		maxsquadsrein = 20 --max number of squads to receive reinforcements

		--US Beach Active Numbers (Number of Active Units at once - ***MAX is 5 or it will crash!!***) --DEFUNCT
		dif_us_inf = 5  --Infantry, some javelins and MGs
		dif_us_apc = 3  --APCs, strykers and Bradleys
		dif_us_tank = 2  --M1A2 Abrams only
		
		--Chinese Counter Attack Numbers (Total Numbers to defeat)
		chineseinfno = 30  --Infantry, e.g. Regulars, Miltia and RPG teams
		chineseapcno = 10  --APCs
		chinesetankno = 5  --Tanks
		chinesesupportno = 15	--Support, e.g. MGs, Snipers, Mortars, ATguns etc

		--Marine Recon Force Bonuses
		MRFaccuracy = 1.4
		MRFdamage = 1.4
		MRFrange = 1.2
		MRFvuln = 0.7
		
	elseif g_dif == 1 then
		
		--How much % health sentries have left before attempting to retreat and raise the alarm - e.g. 0.4 = 40%
		Retreat_Threshold_Beneath = 0.4
		Retreat_Threshold_Above = 0.39
		
		Player_SetPopCapOverride(player1, 90)  --Set player pop cap
		Player_SetResource(player1, RT_Manpower, 1500)  --Starting Manpower
		Player_SetResource(player1, RT_Munition, 1200)  -- Starting Munitions
		Player_SetResource(player1, RT_Fuel, 600)  --Starting Fuel
	
		Modify_PlayerResourceRate(player1, RT_Manpower, 1)  --Bonus manpower rate (adjusts later)
		Modify_PlayerResourceRate(player1, RT_Munition, 2)  --Bonus Munitions Rate
		Modify_PlayerResourceRate(player1, RT_Fuel, 2)  --Bonus fuel rate
		Modify_PlayerProductionRate(player1, 1.2)  --How fast player building make units
			
		--Starting Patrol Unit Possible Types
		types_sentryinf = {SBP.ELITE.MILITIA, SBP.ELITE.REGULAR, SBP.ELITE.LIGHT_AT}
		Types_Jeep = {SBP.ELITE.ATMP_RECON, SBP.ELITE.ATMP_RECON, SBP.ELITE.ZSD_89}

		--Mission Timers, in minutes
		InfiltrateTimer = 10  --how long before the assault mission starts
		BeachheadTimer = 15  --how long you have to capture the beachhead point
		DiginTimer = 4   --how long you have to prepare defences before the counterattack	
		
		--US Beach Reinforcement Numbers (Max total Reinforcements you can receive)
		usinfrein = 21  --Infantry, some javelins and MGs
		usapcrein = 7  --APCs, strykers and Bradleys
		ustankrein = 3  --M1A2 Abrams only

		--US Beach Active Numbers (Number of Active Units at once - ***MAX is 5 or it will crash!!***) --DEFUNCT
		dif_us_inf = 5  --Infantry, some javelins and MGs
		dif_us_apc = 2  --APCs, strykers and Bradleys
		dif_us_tank = 2  --M1A2 Abrams only
		maxsquadsrein = 15 --max number of squads to receive reinforcements
		
		--Chinese Counter Attack Numbers (Total Numbers to defeat)
		chineseinfno = 32  --Infantry, e.g. Regulars, Miltia and RPG teams
		chineseapcno = 15  --APCs
		chinesetankno = 8  --Tanks
		chinesesupportno = 20	--Support, e.g. MGs, Snipers, Mortars, ATguns etc

		--Marine Recon Force Bonuses
		MRFaccuracy = 1.25
		MRFdamage = 1.25
		MRFrange = 1.1
		MRFvuln = 0.8
			
	elseif g_dif == 2 then
		
		--How much % health sentries have left before attempting to retreat and raise the alarm - e.g. 0.6 = 60%
		Retreat_Threshold_Beneath = 0.6
		Retreat_Threshold_Above = 0.59	
		
		Player_SetPopCapOverride(player1, 80)  --Set player pop cap
		Player_SetResource(player1, RT_Manpower, 1000)  --Starting Manpower
		Player_SetResource(player1, RT_Munition, 1000)  -- Starting Munitions
		Player_SetResource(player1, RT_Fuel, 400)  --Starting Fuel
	
		Modify_PlayerResourceRate(player1, RT_Manpower, 1)  --Bonus manpower rate (adjusts later)
		Modify_PlayerResourceRate(player1, RT_Munition, 2.2)  --Bonus Munitions Rate (120%)
		Modify_PlayerResourceRate(player1, RT_Fuel, 2.2)  --Bonus fuel rate
		Modify_PlayerProductionRate(player1, 1.1)  --How fast player building make units
			
		--Starting Patrol Unit Possible Types
		types_sentryinf = {SBP.ELITE.MILITIA, SBP.ELITE.REGULAR, SBP.ELITE.REGULAR, SBP.ELITE.LIGHT_AT}
		Types_Jeep = {SBP.ELITE.ATMP_RECON, SBP.ELITE.ZSD_89}

		--Mission Timers, in minutes
		InfiltrateTimer = 8  --how long before the assault mission starts
		BeachheadTimer = 12  --how long you have to capture the beachhead point
		DiginTimer = 3   --how long you have to prepare defences before the counterattack	
		
		--US Beach Reinforcement Numbers (Max total Reinforcements you can receive)
		usinfrein = 18  --Infantry, some javelins and MGs
		usapcrein = 6  --APCs, strykers and Bradleys
		ustankrein = 2  --M1A2 Abrams only

		--US Beach Active Numbers (Number of Active Units at once - ***MAX is 5 or it will crash!!***) --DEFUNCT
		dif_us_inf = 5  --Infantry, some javelins and MGs
		dif_us_apc = 2  --APCs, strykers and Bradleys
		dif_us_tank = 1  --M1A2 Abrams only
		maxsquadsrein = 12 --max number of squads to receive reinforcements
		
		--Chinese Counter Attack Numbers (Total Numbers to defeat)
		chineseinfno = 35  --Infantry, e.g. Regulars, Miltia and RPG teams
		chineseapcno = 25  --APCs
		chinesetankno = 12  --Tanks
		chinesesupportno = 25	--Support, e.g. MGs, Snipers, Mortars, ATguns etc

		--Marine Recon Force Bonuses
		MRFaccuracy = 1.1
		MRFdamage = 1.1
		MRFrange = 1
		MRFvuln = 0.9
		
	elseif g_dif == 3 then
		
		--How much % health sentries have left before attempting to retreat and raise the alarm - e.g. 0.6 = 60%
		Retreat_Threshold_Beneath = 0.65
		Retreat_Threshold_Above = 0.64	
		
		Player_SetPopCapOverride(player1, 70)  --Set player pop cap
		Player_SetResource(player1, RT_Manpower, 800)  --Starting Manpower
		Player_SetResource(player1, RT_Munition, 800)  -- Starting Munitions
		Player_SetResource(player1, RT_Fuel, 200)  --Starting Fuel
	
		Modify_PlayerResourceRate(player1, RT_Manpower, 1)  --Bonus manpower rate (adjusts later)
		Modify_PlayerResourceRate(player1, RT_Munition, 2.0)  --Bonus Munitions Rate (120%)
		Modify_PlayerResourceRate(player1, RT_Fuel, 2.0)  --Bonus fuel rate
		Modify_PlayerProductionRate(player1, 1.0)  --How fast player building make units
			
		--Starting Patrol Unit Possible Types
		types_sentryinf = {SBP.ELITE.MILITIA, SBP.ELITE.REGULAR, SBP.ELITE.REGULAR, SBP.ELITE.LIGHT_AT}
		Types_Jeep = {SBP.ELITE.ATMP_RECON, SBP.ELITE.ZSD_89}

		--Mission Timers, in minutes
		InfiltrateTimer = 8  --how long before the assault mission starts
		BeachheadTimer = 10  --how long you have to capture the beachhead point
		DiginTimer = 2   --how long you have to prepare defences before the counterattack	
		
		--US Beach Reinforcement Numbers (Max total Reinforcements you can receive)
		usinfrein = 15  --Infantry, some javelins and MGs
		usapcrein = 5  --APCs, strykers and Bradleys
		ustankrein = 2  --M1A2 Abrams only

		--US Beach Active Numbers (Number of Active Units at once - ***MAX is 5 or it will crash!!***) --DEFUNCT
		dif_us_inf = 5  --Infantry, some javelins and MGs
		dif_us_apc = 2  --APCs, strykers and Bradleys
		dif_us_tank = 1  --M1A2 Abrams only
		maxsquadsrein = 11 --max number of squads to receive reinforcements
		
		--Chinese Counter Attack Numbers (Total Numbers to defeat)
		chineseinfno = 35  --Infantry, e.g. Regulars, Miltia and RPG teams
		chineseapcno = 30  --APCs
		chinesetankno = 16  --Tanks
		chinesesupportno = 30	--Support, e.g. MGs, Snipers, Mortars, ATguns etc

		--Marine Recon Force Bonuses
		MRFaccuracy = 1
		MRFdamage = 1
		MRFrange = 1
		MRFvuln = 1.05
						
	end

AI_Enable(player1, false) 
AI_Enable(player2, false)

Modify_PlayerResourceRate(player1, RT_Action, 0) --Player receives no command points to spend

MissionNo = 0 --change to 0!
mission1 = false
mission2 = false
mission3 = false
mission4 = false
mission5 = false
mission6 = false

g_CheckAnnihilate = false
AIRSUPPORT = 0

--COOP DETECTION
COOP = 0

--	if AI_IsAIPlayer(player5) then
--	COOP = 0
--	end

ALARMON = 1 --Sentry Alarm Response is On
ASSAULTCOMPLETE = 0 --Is assault timer complete?
AMBUSHREADY = 0 --Has player reached ambush point?
CounterNo = 4

SOUNDS = 
	{
		ALARM = "SpecialFX/alarm_02",		
	}
	for key, value in pairs(SOUNDS) do Sound_PreCacheSound(value) end

Player_SetResource(player2, RT_Manpower, 10000)
Player_SetResource(player2, RT_Munition, 10000)
Player_SetResource(player2, RT_Fuel, 10000)

-------------
-- Tech Up --
-------------

--UNIT TECH

Cmd_InstantUpgrade(player1, UPG.CW.TIER1, ITEM_UNLOCKED)
Cmd_InstantUpgrade(player1, UPG.CW.TIER2, ITEM_UNLOCKED)

Cmd_InstantUpgrade(player1, UPG.CW.GERNADES, ITEM_UNLOCKED)
Cmd_InstantUpgrade(player1, UPG.CW.AP_ROUNDS, ITEM_UNLOCKED)
Cmd_InstantUpgrade(player1, UPG.CW.AT4, ITEM_UNLOCKED)
Cmd_InstantUpgrade(player1, UPG.CW.VEH_SMOKE_SCREEN, ITEM_UNLOCKED)

--COMMAND TREE

Cmd_InstantUpgrade(player1, UPG.COMMANDER_TREE.CW.CLOSE_AREA_COMBAT, ITEM_UNLOCKED)
Cmd_InstantUpgrade(player1, UPG.COMMANDER_TREE.CW.CLOSE_AREA_COMBAT_00, ITEM_UNLOCKED) --Fire up

Cmd_InstantUpgrade(player2, UPG.COMMANDER_TREE.ELITE.FUTURE_IS_NEAR, ITEM_UNLOCKED)
Cmd_InstantUpgrade(player2, UPG.COMMANDER_TREE.ELITE.FUTURE_IS_NEAR_12, ITEM_UNLOCKED)

Cmd_InstantUpgrade(player2, UPG.COMMANDER_TREE.ELITE.FUTURE_IS_NEAR, ITEM_UNLOCKED)

--ARTILLERY RANGE

Modify_AbilityMaxCastRange(player2, ABILITY.ELITE.PHZ89_BARRAGE, 5)

--BUILDING MODIFIERS AND LOCKS

--Modify_EntityCost(player1, EBP.CW.TOW_NEST, RT_Manpower, 70)
--Modify_EntityCost(player1, EBP.CW.TOW_NEST, RT_Fuel, 15)

Modify_EntityBuildTime(player1, EBP.CW.MG_NEST, 0.75)
Modify_EntityBuildTime(player1, EBP.CW.TOW_NEST, 0.75)
Modify_EntityBuildTime(player1, EBP.CW.BARBED_WIRE, 2)
Modify_EntityBuildTime(player1, EBP.CW.SANDBAG, 1.5)

Modify_VehicleRepairRate(player1, 1.5)

Player_SetEntityProductionAvailability(player1, EBP.CW.TANK_TRAP, ITEM_REMOVED)

Player_SetEntityProductionAvailability(player1, EBP.CW.BARRACKS, ITEM_UNLOCKED)
Player_SetEntityProductionAvailability(player1, EBP.CW.MOTORPOOL, ITEM_UNLOCKED)
Player_SetEntityProductionAvailability(player1, EBP.CW.TANK_DEPOT, ITEM_UNLOCKED)


--------------------
-- CHINESE TROOPS --
--------------------

--PATROLS
dif_jeep_no = 6
dif_inf_no = 6
alarmlevel = 0

--REINFORCEMENTS
dif_apc_no = 2
dif_tank_no = 1
dif_infrein_no = 4
dif_support_no = 2

--BEACH ARTILLERY
max_artillery = 6

--CHINESE TROOPS
types_apc = {SBP.ELITE.PGZ_95_PACIFIC_ASSAULT, SBP.ELITE.ZBD2K, SBP.ELITE.ZSD_89} -- 95 AA, ZBD AC, ZSD APC
types_tank = {SBP.ELITE.TYPE_59D, SBP.ELITE.TYPE_96, SBP.ELITE.TYPE_99} --59D medium, 96 heavy, 99 super heavy
types_infrein = {SBP.ELITE.PLA_ENGINEER, SBP.ELITE.MILITIA, SBP.ELITE.MILITIA, SBP.ELITE.REGULAR}
types_support = {SBP.ELITE.LIGHT_AT, SBP.ELITE.PF98_AT, SBP.ELITE.REGULAR_MG, SBP.ELITE.MORTAR, SBP.ELITE.SNIPER, SBP.ELITE.TYPE_86_ATG, SBP.ELITE.TYPE_89} -- 89 marder, 86 AT

vet_inf = 1
vet_apc = 1
vet_tank = 1
vet_support = 1

--US TROOPS
dif_types_usinf = {SBP.CW.MARINES_PACIFIC_ASSAULT, SBP.CW.MARINES_PACIFIC_ASSAULT, SBP.CW.MARINES_PACIFIC_ASSAULT, SBP.CW.MARINES_AT_PACIFIC_ASSAULT, SBP.CW.MARINES_HMG_PACIFIC_ASSAULT, SBP.CW.SEABEES_PACIFIC_ASSAULT}
dif_types_usapc = {SBP.CW.AAVP7A1_PACIFIC_ASSAULT}
dif_types_ustank = {SBP.CW.M1A2_ABRAMS, SBP.CW.M1A2_ABRAMS}

-------------
-- COOP/SP --
-------------

	if COOP == 1 then --Things to change for coop

	end

end

Scar_AddInit(OnInit)


-------------------
-- SETUP PATROLS --
-------------------

function startpatrols()

Rule_AddInterval(checkloss, 10) --Ends game if player runs out of units

Rule_AddOneShot(sentries, 1)
Rule_AddOneShot(sentries1, 2)
Rule_AddOneShot(sentries2, 3)
Rule_AddOneShot(sentries3, 4)
Rule_AddOneShot(sentries4, 5)

Rule_AddOneShot(SetupBunkers, 6)
Rule_AddOneShot(minefield, 1)

--Rule_AddOneShot(StartAA1, 30)

Rule_AddOneShot(SpawnJeepPatrols, 2)
Rule_AddInterval(jeepcheck, 10)

Rule_AddOneShot(SpawninfPatrols, 8)
Rule_AddInterval(infcheck, 10)

Rule_AddInterval(checksentryalarm, 10)
Rule_AddInterval(checkjeepalarm, 10)
Rule_AddInterval(checkinfalarm, 10)

	if COOP == 1 then --MAKE HARDER FOR 2 PLAYERS

	Retreat_Threshold_Beneath = 0.5
	Retreat_Threshold_Above = 0.49

	types_sentryinf = {SBP.ELITE.MILITIA, SBP.ELITE.MILITIA, SBP.ELITE.LIGHT_AT, SBP.ELITE.LIGHT_AT}
	Types_Jeep = {SBP.ELITE.ATMP_RECON, SBP.ELITE.ATMP_RECON, SBP.ELITE.ATMP_RECON, SBP.ELITE.ZSD_89, SBP.ELITE.ZSD_89}

	--US BEACH REINFORCEMENTS
	usinfrein = 15
	usapcrein = 8
	ustankrein = 3

	--COUNTER ATTACK NUMBERS
	chineseinfno = 50
	chineseapcno = 30
	chinesetankno = 25
	chinesesupportno = 35

	--Cmd_InstantUpgrade(player1, UPG.ALLIES.BAR, ITEM_UNLOCKED)
	--Player_SetAbilityAvailability(player1, ABILITY.ALLIES.STICKY_BOMB, ITEM_UNLOCKED)
	--Player_SetAbilityAvailability(player1, ABILITY.ALLIES.BAR_SUPPRESSION, ITEM_UNLOCKED)
	--Player_SetAbilityAvailability(player1, ABILITY.CW.GRENADE_COMMANDO, ITEM_UNLOCKED)

	end
end


function refreshpatrols()

Rule_AddInterval(RefreshJeepPatrols, 90)
Rule_AddInterval(RefreshinfPatrols, 90)

end


-----------------
--  BEACH ATT  --
-----------------

function StartBeachForces()

--BeachAssaultBoats()

--BeachForcesEng()
--BeachForcesInf()
--BeachForcesAPC()
--BeachForcesTank()

Rule_AddInterval(BeachForcesEng, 50)
Rule_AddInterval(BeachAAV, 115)

--Rule_AddInterval(BeachForcesInf, 75)
--Rule_AddInterval(BeachForcesAPC, 85)
--Rule_AddInterval(BeachForcesTank, 100)

end


function BeachAssaultBoats()

	for i = 1, 5 do
		local sg_temp = SGroup_CreateIfNotFound("sg_usbeachapc" .. i)
		local sg_temp1 = SGroup_CreateIfNotFound("sg_usbeachapcAAV" .. i)
		
			Util_CreateSquads(player1, sg_temp1, dif_types_usapc, Marker_FromName("s_ass" .. i, "basic_marker"), nil, 1)
			Util_CreateSquads(player1, sg_temp, dif_types_usinf, sg_temp1, nil, 1)
			Util_CreateSquads(player1, sg_temp, dif_types_usinf, sg_temp1, nil, 1)
			Util_CreateSquads(player1, sg_temp, dif_types_usinf, sg_temp1, nil, 1)
			--Cmd_Ungarrison(sg_temp1, Util_GetPosition(Marker_FromName("m_ass" .. i, "cyan_marker")), true)
			Cmd_Move(sg_temp1, Util_GetPosition(Marker_FromName("m_ass" .. i, "cyan_marker")), true)	
	end

end



function BeachForcesEng()

sg_usbeacheng = SGroup_CreateIfNotFound("sg_usbeacheng")
		
	if SGroup_IsEmpty(sg_usbeacheng) then
		Util_CreateSquads(player1, sg_usbeacheng, SBP.CW.SEABEES_PACIFIC_ASSAULT, Marker_FromName("s_ass" .. World_GetRand(1, 5), "basic_marker"), nil, 1)					
	end
end 



function BeachAAV()

sg_usbeachinf = SGroup_CreateIfNotFound("sg_usbeachinf")
sg_usbeachinf1 = SGroup_CreateIfNotFound("sg_usbeachinf1")

local squadcount = Player_GetSquadCount(player1) 

local movespot = World_GetRand(1, 5)

		if usinfrein > 0 and squadcount < maxsquadsrein then
			Util_CreateSquads(player1, sg_usbeachinf1, dif_types_usapc, Marker_FromName("s_ass" .. movespot, "basic_marker"), nil, 1)
			Util_CreateSquads(player1, sg_usbeachinf, dif_types_usinf, sg_usbeachinf1, nil, 1)
			Util_CreateSquads(player1, sg_usbeachinf, dif_types_usinf, sg_usbeachinf1, nil, 1)
			Util_CreateSquads(player1, sg_usbeachinf, dif_types_usinf, sg_usbeachinf1, nil, 1)
			--Cmd_AttackMove(sg_usbeachinf1, Util_GetPosition(Marker_FromName("m_ass" .. movespot, "cyan_marker")), true)
			--Cmd_Ungarrison(sg_usbeachinf1, Util_GetPosition(Marker_FromName("m_ass" .. movespot, "cyan_marker")), true)	
			usinfrein = usinfrein - 3
			usapcrein = usapcrein - 1

			aavmsg = Util_CreateLocStringfromID(14000049)
			Util_GlobalMessage(aavmsg, 3)
			UI_CreateMinimapBlip(Marker_FromName("s_ass" .. movespot, "basic_marker"), 3, BT_GeneralPing)

		elseif usinfrein < 1 then
			Rule_RemoveMe()					
		end

end




---DEFUNCT FOR NOW, LEFT FOR REFERENCE OR POSSIBLE RE-USE
---------------------------------------------------------

function BeachForcesInf()

	for i = 1, dif_us_inf do
		local sg_temp = SGroup_CreateIfNotFound("sg_usbeachinf" .. i)
		
		if SGroup_IsEmpty(sg_temp) and usinfrein > 0 then
			Util_CreateSquads(player1, sg_temp, dif_types_usinf, Marker_FromName("s_ass" .. i, "basic_marker"), nil, 1)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_ass" .. i, "cyan_marker")), true)	
			usinfrein = usinfrein - 1
		elseif usinfrein < 1 then
			Rule_RemoveMe()					
		end
	end
end 



function BeachForcesAPC()

	for i = 1, dif_us_apc do
		local sg_temp = SGroup_CreateIfNotFound("sg_usbeachapc" .. i)
		
		if SGroup_IsEmpty(sg_temp) and usapcrein > 0 then
			Util_CreateSquads(player1, sg_temp, dif_types_usapc, Marker_FromName("s_ass" .. i, "basic_marker"), nil, 1)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_ass" .. i, "cyan_marker")), true)	
			usapcrein = usapcrein - 1	
		elseif usapcrein < 1 then
			Rule_RemoveMe()		
		end
	end
end 


function BeachForcesTank()

	for i = 1, dif_us_tank do
		local sg_temp = SGroup_CreateIfNotFound("sg_usbeachtank" .. i)
		
		if SGroup_IsEmpty(sg_temp) and ustankrein > 0 then
			Util_CreateSquads(player1, sg_temp, dif_types_ustank, Marker_FromName("s_ass" .. World_GetRand(1, 5), 			"basic_marker"), nil, 1)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_ass" .. World_GetRand(1, 5), "cyan_marker")), true)	
			ustankrein = ustankrein - 1
		elseif ustankrein < 1 then
			Rule_RemoveMe()			
		end
	end
end 


--USED AGAIN--------------------------------------------------------------------

function UStechUp() -- Once beach assault begins

Cmd_InstantUpgrade(player1, UPG.SP.PA_NAVAL_BARRAGE, ITEM_UNLOCKED)
Cmd_InstantUpgrade(player1, UPG.SP.PA_LAV25_PARADROP, ITEM_UNLOCKED)
Cmd_InstantUpgrade(player1, UPG.SP.PA_LAVAT_PARADROP, ITEM_UNLOCKED)

Cmd_InstantUpgrade(player1, UPG.SP.PA_F16_JDAM, ITEM_UNLOCKED)

--Cmd_InstantUpgrade(player1, UPG.COMMANDER_TREE.CW.AIR_DOMINANCE_01, ITEM_UNLOCKED) --A10
--Cmd_InstantUpgrade(player1, UPG.COMMANDER_TREE.CW.AIR_DOMINANCE_12, ITEM_UNLOCKED) --Combat Drop

Cmd_InstantUpgrade(player1, UPG.CW.TIER1, ITEM_UNLOCKED)
Cmd_InstantUpgrade(player1, UPG.CW.TIER2, ITEM_UNLOCKED)
Cmd_InstantUpgrade(player1, UPG.CW.TIER3, ITEM_UNLOCKED)

Cmd_InstantUpgrade(player1, UPG.CW.GERNADES, ITEM_UNLOCKED)
Cmd_InstantUpgrade(player1, UPG.CW.AP_ROUNDS, ITEM_UNLOCKED)
Cmd_InstantUpgrade(player1, UPG.CW.AT4, ITEM_UNLOCKED)
Cmd_InstantUpgrade(player1, UPG.CW.VEH_SMOKE_SCREEN, ITEM_UNLOCKED)

Modify_PlayerProductionRate(player1, 2)

Player_SetSquadProductionAvailability(player1, SBP.CW.HUMVEE, ITEM_UNLOCKED)
Player_SetSquadProductionAvailability(player1, SBP.CW.M1064_MORTAR, ITEM_UNLOCKED)
Player_SetSquadProductionAvailability(player1, SBP.CW.LAV25, ITEM_UNLOCKED)
Player_SetSquadProductionAvailability(player1, SBP.CW.LAVAT, ITEM_UNLOCKED)

--Player_SetEntityProductionAvailability(player1, EBP.CW.MOTORPOOL, ITEM_UNLOCKED)
--Player_SetEntityProductionAvailability(player1, EBP.CW.TANK_DEPOT, ITEM_UNLOCKED)

	if COOP == 0 then

	--Cmd_InstantUpgrade(player1, UPG.COMMANDER_TREE.CW.ARMOURED_WARFARE_02, ITEM_UNLOCKED) --Paladin Artillery
	--Cmd_InstantUpgrade(player1, UPG.COMMANDER_TREE.CW.ARMOURED_WARFARE_12, ITEM_UNLOCKED) --M1A2 Tanks

	elseif COOP == 1 then
	
	end
end


-----------------
--  DEFENDERS  --
-----------------

function spawndefenders()

sg_bb1 = SGroup_CreateIfNotFound("sg_bb1")
Util_CreateSquads(player2, sg_bb1, SBP.ELITE.SNIPER, s_garrbot, nil, 1)
Cmd_Garrison(sg_bb1, eg_towerbot1, true, false)

sg_bb2 = SGroup_CreateIfNotFound("sg_bb2")
Util_CreateSquads(player2, sg_bb2, SBP.ELITE.REGULAR_MG, s_garrbot, nil, 1)
Cmd_Garrison(sg_bb2, eg_towerbot2, true, false)

sg_bb3 = SGroup_CreateIfNotFound("sg_bb3")
Util_CreateSquads(player2, sg_bb3, SBP.ELITE.REGULAR_MG, s_garrbot, nil, 1)
Cmd_Garrison(sg_bb3, eg_towerbot3, true, false)

sg_bb4 = SGroup_CreateIfNotFound("sg_bb4")
Util_CreateSquads(player2, sg_bb4, SBP.ELITE.SNIPER, s_garrbot, nil, 1)
Cmd_Garrison(sg_bb4, eg_towerbot4, true, false)

sg_bb5 = SGroup_CreateIfNotFound("sg_bb5")
Util_CreateSquads(player2, sg_bb5, SBP.ELITE.REGULAR_MG, s_garrbot, nil, 1)
Cmd_Garrison(sg_bb5, eg_towerbot5, true, false)

sg_bb6 = SGroup_CreateIfNotFound("sg_bb6")
Util_CreateSquads(player2, sg_bb6, SBP.ELITE.REGULAR_MG, s_garrbot, nil, 1)
Cmd_Garrison(sg_bb6, eg_towerbot6, true, false)

sg_bb7 = SGroup_CreateIfNotFound("sg_bb7")
Util_CreateSquads(player2, sg_bb7, SBP.ELITE.SNIPER, sent_22, nil, 1)
Cmd_Garrison(sg_bb7, eg_towertop1, true, false)

sg_bbb = SGroup_CreateIfNotFound("sg_bbb")
Util_CreateSquads(player2, sg_bbb, SBP.ELITE.REGULAR_MG, sent_22, nil, 1)
Cmd_Garrison(sg_bbb, eg_towertop2, true, false)

sg_bb8 = SGroup_CreateIfNotFound("sg_bb8")
Util_CreateSquads(player2, sg_bb8, SBP.ELITE.REGULAR_MG, sentryretreat, nil, 1)
Cmd_Garrison(sg_bb8, eg_sentryguard1, true, false)

sg_bb9 = SGroup_CreateIfNotFound("sg_bb9")
Util_CreateSquads(player2, sg_bb9, SBP.ELITE.REGULAR_MG, sentryretreat, nil, 1)
Cmd_Garrison(sg_bb9, eg_sentryguard2, true, false)

sg_bba = SGroup_CreateIfNotFound("sg_bba")
Util_CreateSquads(player2, sg_bba, SBP.ELITE.REGULAR_MG, sentryretreat, nil, 1)
Cmd_Garrison(sg_bba, eg_sentryhouse, true, false)

sg_bbc = SGroup_CreateIfNotFound("sg_bbc")
Util_CreateSquads(player2, sg_bbc, SBP.ELITE.REGULAR_MG, sent_23, nil, 1)
Cmd_Garrison(sg_bbc, eg_buildmid1, true, false)

sg_bbd = SGroup_CreateIfNotFound("sg_bbd")
Util_CreateSquads(player2, sg_bbd, SBP.ELITE.REGULAR_MG, sent_23, nil, 1)
Cmd_Garrison(sg_bbd, eg_buildmid2, true, false)

sg_bbe = SGroup_CreateIfNotFound("sg_bbe")
Util_CreateSquads(player2, sg_bbe, SBP.ELITE.SNIPER, s_aidef9, nil, 1)
Cmd_Garrison(sg_bbe, eg_towertop3, true, false)

sg_bbf = SGroup_CreateIfNotFound("sg_bbf")
Util_CreateSquads(player2, sg_bbf, SBP.ELITE.REGULAR, s_aidef9, nil, 1)
Cmd_Garrison(sg_bbf, eg_comms, true, false)

sg_bbg = SGroup_CreateIfNotFound("sg_bbg")
Util_CreateSquads(player2, sg_bbg, SBP.ELITE.SNIPER, spawninf3, nil, 1)
Cmd_Garrison(sg_bbg, eg_towertop4, true, false)

sg_bbh = SGroup_CreateIfNotFound("sg_bbh")
Util_CreateSquads(player2, sg_bbh, SBP.ELITE.SNIPER, s_aa, nil, 1)
Cmd_Garrison(sg_bbh, eg_towertop5, true, false)


	for i = 1, 8 do
		local sg_temp = SGroup_CreateIfNotFound("sg_mortar" .. i)
		
			Util_CreateSquads(player2, sg_temp, SBP.ELITE.MORTAR, Marker_FromName("s_mor" .. i, "basic_marker"), nil, 1)
			Cmd_InstantSetupTeamWeapon(sg_temp)
	end


	for i = 1, 7 do
		local sg_temp = SGroup_CreateIfNotFound("sg_atgun" .. i)
		
			--Util_CreateSquads(player2, sg_temp, SBP.ELITE.TYPE_86_ATG, Marker_FromName("s_atg" .. i, "basic_marker"), nil, 1)
			--Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("s_atg" .. i .. "f", "grey_marker")), true, nil, 5)
			ATGun_AddGroup(sg_temp, player2)
	end
end


-------------------
-- BUNKERS/MINES --
-------------------

function SetupBunkers()

for i = 1, 9 do
	local eg_temp = EGroup_CreateIfNotFound("eg_bunker" .. i)
		
	Util_CreateEntities(player2, eg_temp, EBP.ELITE.BUNKER, Util_GetPosition(Marker_FromName("bunk" .. i, "basic_marker")), 1, Util_GetPosition(Marker_FromName("bunk" .. i .. "f", "grey_marker")))

	end
end


function minefield()

local minepick1 = World_GetRand(6, 9)
local mineplace1 = Util_GetPosition(Marker_FromName("mines"..World_GetRand(1, 2), "red_marker"))

	for i = 1, minepick1 do 

	eg_mines = EGroup_CreateIfNotFound("eg_mines" .. i)

	Util_CreateEntities(player2, eg_mines, BP_GetEntityBlueprint("ebps/races/axis/axis_mine.lua"), Util_GetRandomOffset(Util_GetPosition(mineplace1), -35, 35), 1, sentryretreat)

	end
end

--------------
-- ANTI-AIR --
--------------

function StartAA()

sg_antiaira = SGroup_CreateIfNotFound("sg_antiaira")
Util_CreateSquads(player2, sg_antiaira, SBP.ELITE.PGZ_95_PACIFIC_ASSAULT, s_aa, nil, 1)
Modify_Vulnerability(sg_antiaira, 3)

Rule_AddInterval(Moveantiaira, World_GetRand(45, 65))

end


function StartAA1()

sg_antiairb = SGroup_CreateIfNotFound("sg_antiairb")
Util_CreateSquads(player2, sg_antiairb, SBP.ELITE.PGZ_95_PACIFIC_ASSAULT, s_aa1intro, nil, 1)
Modify_Vulnerability(sg_antiairb, 3)

Cmd_Move(sg_antiairb, Marker_FromName("m_rein42", "cyan_marker"), true)
Cmd_Move(sg_antiairb, Marker_FromName("m_rein11", "cyan_marker"), true)
Cmd_Move(sg_antiairb, Marker_FromName("m_rein42", "cyan_marker"), true)

Rule_AddInterval(Moveantiairb, World_GetRand(45, 65))

end


function Moveantiaira()

local aapick1 = World_GetRand(1, 4)

	if SGroup_GetAvgHealth(sg_antiaira) > 0.01 then

	--Workaround for weirdness
		if aapick1 == 1 then
			Cmd_AttackMove(sg_antiaira, Marker_FromName("m_aa1", "cyan_marker"), true)
		elseif aapick1 == 2 then
			Cmd_AttackMove(sg_antiaira, Marker_FromName("m_aa2", "cyan_marker"), true)
		elseif aapick1 == 3 then
			Cmd_AttackMove(sg_antiaira, Marker_FromName("m_aa3", "cyan_marker"), true)
		elseif aapick1 == 4 then
			Cmd_AttackMove(sg_antiaira, Marker_FromName("m_aa4", "cyan_marker"), true)
		end

	elseif SGroup_GetAvgHealth(sg_antiaira) == 0 then
	Rule_RemoveMe()
	end
end


function Moveantiairb()

local aapick2 = World_GetRand(1, 4)

	if SGroup_GetAvgHealth(sg_antiairb) > 0.01 then

	--Workaround for weirdness
		if aapick2 == 1 then
			Cmd_AttackMove(sg_antiairb, Marker_FromName("m_aa11", "cyan_marker"), true)
		elseif aapick2 == 2 then
			Cmd_AttackMove(sg_antiairb, Marker_FromName("m_aa12", "cyan_marker"), true)
		elseif aapick2 == 3 then
			Cmd_AttackMove(sg_antiairb, Marker_FromName("m_aa13", "cyan_marker"), true)
		elseif aapick2 == 4 then
			Cmd_AttackMove(sg_antiairb, Marker_FromName("m_aa14", "cyan_marker"), true)
		end

	elseif SGroup_GetAvgHealth(sg_antiairb) == 0 then
	Rule_RemoveMe()
	end
end

-----------------
--   SENTRIES  --
-----------------

function sentries()

sentpick1 = Marker_FromName("sent_1" .. World_GetRand(1, 4), "basic_marker")

sg_sentry5 = SGroup_CreateIfNotFound("sg_sentry5")
Util_CreateSquads(player2, sg_sentry5, types_sentryinf, sentpick1, nil, 1)	

Rule_AddInterval(sentrycheck, 5)
Cmd_SquadPatrolMarker(sg_sentry5, sentpick1)

end



function sentries1()

sentpick2 = Marker_FromName("sent_2" .. World_GetRand(1, 4), "basic_marker")

sg_sentry1 = SGroup_CreateIfNotFound("sg_sentry1")
Util_CreateSquads(player2, sg_sentry1, types_sentryinf, sentpick2, nil, 1)	

Rule_AddInterval(sentrycheck1, 5)
Cmd_SquadPatrolMarker(sg_sentry1, sentpick2)

end



function sentries2()

sentpick3 = Marker_FromName("sent_3" .. World_GetRand(1, 2), "basic_marker")

sg_sentry2 = SGroup_CreateIfNotFound("sg_sentry2")
Util_CreateSquads(player2, sg_sentry2, types_sentryinf, sentpick3, nil, 1)	

Rule_AddInterval(sentrycheck2, 5)
Cmd_SquadPatrolMarker(sg_sentry2, sentpick3)

end



function sentries3()

sentpick4 = Marker_FromName("sent_4" .. World_GetRand(1, 4), "basic_marker")

sg_sentry3 = SGroup_CreateIfNotFound("sg_sentry3")
Util_CreateSquads(player2, sg_sentry3, types_sentryinf, sentpick4, nil, 1)	

Rule_AddInterval(sentrycheck3, 5)
Cmd_SquadPatrolMarker(sg_sentry3, sentpick4)

end



function sentries4()

sentpick5 = Marker_FromName("sent_5" .. World_GetRand(1, 4), "basic_marker")

sg_sentry4 = SGroup_CreateIfNotFound("sg_sentry4")
Util_CreateSquads(player2, sg_sentry4, types_sentryinf, sentpick5, nil, 1)	

Rule_AddInterval(sentrycheck4, 5)
Cmd_SquadPatrolMarker(sg_sentry4, sentpick5)

end


-----------------------------------CHECKERS


function sentrycheck()

if SGroup_GetAvgHealth(sg_sentry5) > 0.39 and SGroup_IsUnderAttack(sg_sentry5, ANY, 3) then

local sg_temptargets1 = SGroup_CreateIfNotFound("sg_temptargets1")
SGroup_Clear(sg_temptargets1)
SGroup_GetLastAttacker(sg_sentry5, sg_temptargets1)

if SGroup_GetAvgHealth(sg_temptargets1) > 0.01 then
 
local target1 = SGroup_GetPosition(sg_temptargets1)
Cmd_AttackMove(sg_sentry5, target1, true, nil, 20)
Cmd_AttackMove(sg_sentry5, sentpick1, true, nil, 20)

end
end

if SGroup_GetAvgHealth(sg_sentry5) < 0.4 and SGroup_GetAvgHealth(sg_sentry5) > 0.01 then

Cmd_Stop(sg_sentry5)
Cmd_Move(sg_sentry5, sentryretreat, false)

end

if SGroup_GetAvgHealth(sg_sentry5) == 0 then
Rule_RemoveMe()

end
end


function sentrycheck1()

if SGroup_GetAvgHealth(sg_sentry1) > 0.39 and SGroup_IsUnderAttack(sg_sentry1, ANY, 3) then

local sg_temptargets2 = SGroup_CreateIfNotFound("sg_temptargets2")
SGroup_Clear(sg_temptargets2)
SGroup_GetLastAttacker(sg_sentry1, sg_temptargets2)

if SGroup_GetAvgHealth(sg_temptargets2) > 0.01 then

local target1 = SGroup_GetPosition(sg_temptargets2)
Cmd_AttackMove(sg_sentry1, target1, true, nil, 20)
Cmd_AttackMove(sg_sentry1, sentpick2, true, nil, 20)

end
end

if SGroup_GetAvgHealth(sg_sentry1) < 0.4 and SGroup_GetAvgHealth(sg_sentry1) > 0.01 then

Cmd_Stop(sg_sentry1)
Cmd_Move(sg_sentry1, sentryretreat, false)

end

if SGroup_GetAvgHealth(sg_sentry1) == 0 then
Rule_RemoveMe()

end
end




function sentrycheck2()

if SGroup_GetAvgHealth(sg_sentry2) > 0.39 and SGroup_IsUnderAttack(sg_sentry2, ANY, 3) then

local sg_temptargets3 = SGroup_CreateIfNotFound("sg_temptargets3")
SGroup_Clear(sg_temptargets3)
SGroup_GetLastAttacker(sg_sentry2, sg_temptargets3)

if SGroup_GetAvgHealth(sg_temptargets3) > 0.01 then

local target1 = SGroup_GetPosition(sg_temptargets3)
Cmd_AttackMove(sg_sentry2, target1, true, nil, 20)
Cmd_AttackMove(sg_sentry2, sentpick3, true, nil, 20)
end
end

if SGroup_GetAvgHealth(sg_sentry2) < 0.4 and SGroup_GetAvgHealth(sg_sentry2) > 0.01 then

Cmd_Stop(sg_sentry2)
Cmd_Move(sg_sentry2, sentryretreat, false)

end

if SGroup_GetAvgHealth(sg_sentry2) == 0 then
Rule_RemoveMe()

end
end



function sentrycheck3()

if SGroup_GetAvgHealth(sg_sentry3) > 0.39 and SGroup_IsUnderAttack(sg_sentry3, ANY, 3) then

local sg_temptargets4 = SGroup_CreateIfNotFound("sg_temptargets4")
SGroup_Clear(sg_temptargets4)
SGroup_GetLastAttacker(sg_sentry3, sg_temptargets4)

if SGroup_GetAvgHealth(sg_temptargets4) > 0.01 then

local target1 = SGroup_GetPosition(sg_temptargets4)
Cmd_AttackMove(sg_sentry3, target1, true, nil, 20)
Cmd_AttackMove(sg_sentry3, sentpick4, true, nil, 20)
end
end

if SGroup_GetAvgHealth(sg_sentry3) < 0.4 and SGroup_GetAvgHealth(sg_sentry3) > 0.01 then

Cmd_Stop(sg_sentry3)
Cmd_Move(sg_sentry3, sentryretreat, false)

end

if SGroup_GetAvgHealth(sg_sentry3) == 0 then
Rule_RemoveMe()

end
end



function sentrycheck4()

if SGroup_GetAvgHealth(sg_sentry4) > 0.39 and SGroup_IsUnderAttack(sg_sentry4, ANY, 3) then

local sg_temptargets5 = SGroup_CreateIfNotFound("sg_temptargets5")
SGroup_Clear(sg_temptargets5)
SGroup_GetLastAttacker(sg_sentry4, sg_temptargets5)

if SGroup_GetAvgHealth(sg_temptargets5) > 0.01 then

local target1 = SGroup_GetPosition(sg_temptargets5)
Cmd_AttackMove(sg_sentry4, target1, true, nil, 20)
Cmd_AttackMove(sg_sentry4, sentpick5, true, nil, 20)
end
end

if SGroup_GetAvgHealth(sg_sentry4) < 0.4 and SGroup_GetAvgHealth(sg_sentry4) > 0.01 then

Cmd_Stop(sg_sentry4)
Cmd_Move(sg_sentry4, sentryretreat, false)

end

if SGroup_GetAvgHealth(sg_sentry4) == 0 then
Rule_RemoveMe()

end
end


-----------------
--    PATROLS  --
-----------------

function SpawnJeepPatrols()
	--Patrolling Jeeps

	for i = 1, dif_jeep_no do
		local sg_temp = SGroup_CreateIfNotFound("sg_jeeps" .. i)
		
			Util_CreateSquads(player2, sg_temp, Types_Jeep, Marker_FromName("spawninf" .. i, "basic_marker"), nil, 1)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. World_GetRand(1, 3), "cyan_marker")), true)

			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 1, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 2, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 3, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 4, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 5, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 4, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 3, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 2, "cyan_marker")), true)

			Modify_UnitSpeed(sg_temp, 0.6)

		if COOP == 1 then

			-- ************ ADD DIFFICULTY FACTOR HERE
		end
	end
end




function RefreshJeepPatrols()
	--Patrolling Jeeps - refreshes patrols for all alive units every few minutes.  Queued so they can still react.

	for i = 1, dif_jeep_no do
		local sg_temp = SGroup_CreateIfNotFound("sg_jeeps" .. i)
		if SGroup_GetAvgHealth(sg_temp) > 0.01 and SGroup_IsIdle(sg_temp, ANY) then
			
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 1, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 2, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 3, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 4, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 5, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 4, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 3, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 2, "cyan_marker")), true)
		end
		
	end
end


function jeepcheck()
	--Patrolling Jeeps - Checks to see if damaged or under attack.

	for i = 1, dif_jeep_no do
		local sg_temp = SGroup_CreateIfNotFound("sg_jeeps" .. i)
		if SGroup_GetAvgHealth(sg_temp) > 0.01 then

			if SGroup_GetAvgHealth(sg_temp) > Retreat_Threshold_Above and SGroup_IsUnderAttack(sg_temp, ANY, 3) then

				local sg_temptargetsJ = SGroup_CreateIfNotFound("sg_temptargetsJ")
				SGroup_Clear(sg_temptargetsJ)
				SGroup_GetLastAttacker(sg_temp, sg_temptargetsJ)

				if SGroup_GetAvgHealth(sg_temptargetsJ) > 0.01 then

					local target1 = SGroup_GetPosition(sg_temptargetsJ)
					Cmd_AttackMove(sg_temp, target1, false)
					--Cmd_AttackMove(sg_temp, jeeprally, true)
				end
			end

			if SGroup_GetAvgHealth(sg_temp) < Retreat_Threshold_Beneath and SGroup_GetAvgHealth(sg_temp) > 0.01 then

				Cmd_Stop(sg_temp)
				Cmd_Move(sg_temp, sentryretreat, false)

			end
		end
	end
end




function SpawninfPatrols()
	--Patrolling infs

	for i = 1, dif_inf_no do
		local sg_temp = SGroup_CreateIfNotFound("sg_infs" .. i)
		
			Util_CreateSquads(player2, sg_temp, types_sentryinf, Marker_FromName("spawninf" .. i, "basic_marker"), nil, 1)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. World_GetRand(1, 3), "cyan_marker")), true)
			
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 1, "cyan_marker")), true, nil, 20)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 2, "cyan_marker")), true, nil, 20)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 3, "cyan_marker")), true, nil, 20)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 4, "cyan_marker")), true, nil, 20)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 5, "cyan_marker")), true, nil, 20)
		
			SGroup_EnableSurprise(sg_temp, true) 
	end
end




function RefreshinfPatrols()
	--Patrolling infs - refreshes patrols for all alive units every few minutes.  Queued so they can still react.

	for i = 1, dif_inf_no do
		local sg_temp = SGroup_CreateIfNotFound("sg_infs" .. i)
		if SGroup_GetAvgHealth(sg_temp) > 0.01 and SGroup_IsIdle(sg_temp, ANY) then
			
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 1, "cyan_marker")), true, nil, 20)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 2, "cyan_marker")), true, nil, 20)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 3, "cyan_marker")), true, nil, 20)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 4, "cyan_marker")), true, nil, 20)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("infmove" .. i .. 5, "cyan_marker")), true, nil, 20)
		end
		
	end
end


function infcheck()
	--Patrolling infs - Checks to see if damaged or under attack.

	for i = 1, dif_inf_no do
		local sg_temp = SGroup_CreateIfNotFound("sg_infs" .. i)
		if SGroup_GetAvgHealth(sg_temp) > 0.01 then

			if SGroup_GetAvgHealth(sg_temp) > Retreat_Threshold_Above and SGroup_IsUnderAttack(sg_temp, ANY, 3) then

				local sg_temptargetsI = SGroup_CreateIfNotFound("sg_temptargetsI")
				SGroup_Clear(sg_temptargetsI)
				SGroup_GetLastAttacker(sg_temp, sg_temptargetsI)

				if SGroup_GetAvgHealth(sg_temptargetsI) > 0.01 then

					local target1 = SGroup_GetPosition(sg_temptargetsI)
					Cmd_AttackMove(sg_temp, target1, false, nil, 20)					
				end
			end

			if SGroup_GetAvgHealth(sg_temp) < Retreat_Threshold_Beneath and SGroup_GetAvgHealth(sg_temp) > 0.01 then
				
				Cmd_Stop(sg_temp)
				Cmd_Move(sg_temp, sentryretreat, false)

			end
		end
	end
end



-----------------
--   ALARMS    --
-----------------

function checksentryalarm()

	for i = 1, 5 do
	local sg_temp = SGroup_CreateIfNotFound("sg_sentry" .. i)

		if SGroup_GetAvgHealth(sg_temp) > 0.01 and Prox_AreSquadsNearMarker(sg_temp, sentryretreat, ANY, 3) then

			local sg_temptargetalarm = SGroup_CreateIfNotFound("sg_temptargetalarm")
			SGroup_Clear(sg_temptargetalarm)
			SGroup_GetLastAttacker(sg_temp, sg_temptargetalarm)

			if not SGroup_IsEmpty(sg_temptargetalarm) then
				lastknownpos1 = SGroup_GetPosition(sg_temptargetalarm) 
			end

			SGroup_DestroyAllSquads(sg_temp)

			if ALARMON == 1 then

			Sound_PlayStreamed(SOUNDS.ALARM)

			sg_alarm = SGroup_CreateIfNotFound("sg_alarm")
			checkalarmlevel() --harder troops for more alarms

			Util_CreateSquads(player2, sg_alarm, types_alarm, sentryretreat, nil, 1)
			Util_CreateSquads(player2, sg_alarm, types_alarm, sentryretreat, nil, 1)
		
			alarmlevel = alarmlevel + 1

				if not SGroup_IsEmpty(sg_temptargetalarm) then

				Cmd_AttackMove(sg_alarm, sg_temptargetalarm, true, nil, 20)
				Cmd_Attack(sg_alarm, sg_temptargetalarm, true)
				Cmd_Move(sg_alarm, lastknownpos1, true)
				Cmd_AttackMove(sg_alarm, sg_temptargetalarm, true, nil, 20)
				Cmd_Attack(sg_alarm, sg_temptargetalarm, true)
				Cmd_Move(sg_alarm, lastknownpos1, true)
				Cmd_AttackMove(sg_alarm, sg_temptargetalarm, true, nil, 20)
				Cmd_Attack(sg_alarm, sg_temptargetalarm, true)

				elseif SGroup_IsEmpty(sg_temptargetalarm) then 
				Cmd_AttackMove(sg_alarm, sent_42, true, nil, 20)
				end
			end
		end
	end
end


function checkjeepalarm()

	for i = 1, 5 do --not doing on dif_jeep_no to avoid Jeep 6 setting off alarm on it's patrol

	local sg_temp = SGroup_CreateIfNotFound("sg_jeeps" .. i)

		if SGroup_GetAvgHealth(sg_temp) > 0.01 and Prox_AreSquadsNearMarker(sg_temp, sentryretreat, ANY, 3) then

			local sg_temptargetalarm = SGroup_CreateIfNotFound("sg_temptargetalarm")
			SGroup_Clear(sg_temptargetalarm)
			SGroup_GetLastAttacker(sg_temp, sg_temptargetalarm)

			if not SGroup_IsEmpty(sg_temptargetalarm) then
				lastknownpos2 = SGroup_GetPosition(sg_temptargetalarm) 
			end

			SGroup_DestroyAllSquads(sg_temp)

			if ALARMON == 1 then

			Sound_PlayStreamed(SOUNDS.ALARM)

			sg_alarm = SGroup_CreateIfNotFound("sg_alarm")
			checkalarmlevel() --harder troops for more alarms

			Util_CreateSquads(player2, sg_alarm, types_alarm, sentryretreat, nil, 1)
			Util_CreateSquads(player2, sg_alarm, types_alarm, sentryretreat, nil, 1)
	
			alarmlevel = alarmlevel + 1

				if not SGroup_IsEmpty(sg_temptargetalarm) then

				Cmd_AttackMove(sg_alarm, sg_temptargetalarm, true, nil, 20)
				Cmd_Attack(sg_alarm, sg_temptargetalarm, true)
				Cmd_Move(sg_alarm, lastknownpos2, true)
				Cmd_AttackMove(sg_alarm, sg_temptargetalarm, true, nil, 20)
				Cmd_Attack(sg_alarm, sg_temptargetalarm, true)
				Cmd_Move(sg_alarm, lastknownpos2, true)
				Cmd_AttackMove(sg_alarm, sg_temptargetalarm, true, nil, 20)
				Cmd_Attack(sg_alarm, sg_temptargetalarm, true)

				elseif SGroup_IsEmpty(sg_temptargetalarm) then 
				Cmd_AttackMove(sg_alarm, sent_43, true, nil, 20)
				end
			end
		end
	end
end



function checkinfalarm()

	for i = 1, dif_inf_no do
	local sg_temp = SGroup_CreateIfNotFound("sg_infs" .. i)

		if SGroup_GetAvgHealth(sg_temp) > 0.01 and Prox_AreSquadsNearMarker(sg_temp, sentryretreat, ANY, 3) then

			local sg_temptargetalarm = SGroup_CreateIfNotFound("sg_temptargetalarm")
			SGroup_Clear(sg_temptargetalarm)
			SGroup_GetLastAttacker(sg_temp, sg_temptargetalarm)

			if not SGroup_IsEmpty(sg_temptargetalarm) then
				lastknownpos3 = SGroup_GetPosition(sg_temptargetalarm) 
			end

			SGroup_DestroyAllSquads(sg_temp)

			if ALARMON == 1 then

			Sound_PlayStreamed(SOUNDS.ALARM)

			sg_alarm = SGroup_CreateIfNotFound("sg_alarm")
			checkalarmlevel() --harder troops for more alarms

			Util_CreateSquads(player2, sg_alarm, types_alarm, sentryretreat, nil, 1)
			Util_CreateSquads(player2, sg_alarm, types_alarm, sentryretreat, nil, 1)
	
			alarmlevel = alarmlevel + 1

				if not SGroup_IsEmpty(sg_temptargetalarm) then

				Cmd_AttackMove(sg_alarm, sg_temptargetalarm, true, nil, 20)
				Cmd_Attack(sg_alarm, sg_temptargetalarm, true)
				Cmd_Move(sg_alarm, lastknownpos3, true)
				Cmd_AttackMove(sg_alarm, sg_temptargetalarm, true, nil, 20)
				Cmd_Attack(sg_alarm, sg_temptargetalarm, true)
				Cmd_Move(sg_alarm, lastknownpos3, true)
				Cmd_AttackMove(sg_alarm, sg_temptargetalarm, true, nil, 20)
				Cmd_Attack(sg_alarm, sg_temptargetalarm, true)

				elseif SGroup_IsEmpty(sg_temptargetalarm) then 
				Cmd_AttackMove(sg_alarm, sent_44, true, nil, 20)
				end
			end
		end
	end
end



function checkalarmlevel()

if alarmlevel == 0 then 

types_alarm = {SBP.ELITE.ATMP_RECON, SBP.ELITE.REGULAR, SBP.ELITE.SNIPER}

elseif alarmlevel == 1 then 

types_alarm = {SBP.ELITE.ATMP_RECON, SBP.ELITE.REGULAR_MG, SBP.ELITE.ZSD_89}

elseif alarmlevel == 2 then 

types_alarm = {SBP.ELITE.ATMP_RECON, SBP.ELITE.ZBD2K, SBP.ELITE.ZSD_89}

elseif alarmlevel > 2 then 

types_alarm = {SBP.ELITE.ZSD_89, SBP.ELITE.ZBD2K}

end
end


---------Remove old sentry/patrol checkers

function removeoldcheckers()

if Rule_Exists(checksentryalarm) then

Rule_Remove(checksentryalarm)

end

if Rule_Exists(checkjeepalarm) then

Rule_Remove(checkjeepalarm)

end

if Rule_Exists(checkinfalarm) then

Rule_Remove(checkinfalarm)

end

if Rule_Exists(infcheck) then

Rule_Remove(infcheck)

end

if Rule_Exists(jeepcheck) then

Rule_Remove(jeepcheck)

end
end


---------------
-- RECAPTURE --
---------------

function CaptureTeams()
	--Capture Teams

	for i = 1, 2 do
		local sg_temp = SGroup_CreateIfNotFound("sg_capteam" .. i)
		
		if SGroup_IsEmpty(sg_temp) then
			Util_CreateSquads(player2, sg_temp, SBP.ELITE.PLA_ENGINEER, Marker_FromName("s_rein3", "basic_marker"), nil, 1)

			Cmd_Move(sg_temp, Util_GetPosition(eg_stratpoint1), true)
			Command_SquadEntity(player2, sg_temp, SCMD_Capture, eg_stratpoint1, true)
			Cmd_Move(sg_temp, Util_GetPosition(eg_stratpoint2), true)
			Command_SquadEntity(player2, sg_temp, SCMD_Capture, eg_stratpoint2, true)
			Cmd_Move(sg_temp, Util_GetPosition(eg_stratpoint3), true)
			Command_SquadEntity(player2, sg_temp, SCMD_Capture, eg_stratpoint3, true)

			Cmd_Move(sg_temp, Util_GetPosition(eg_stratpoint4), true)
			Command_SquadEntity(player2, sg_temp, SCMD_Capture, eg_stratpoint4, true)
			Cmd_Move(sg_temp, Util_GetPosition(eg_stratpoint5), true)
			Command_SquadEntity(player2, sg_temp, SCMD_Capture, eg_stratpoint5, true)
			Cmd_Move(sg_temp, Util_GetPosition(eg_stratpoint6), true)
			Command_SquadEntity(player2, sg_temp, SCMD_Capture, eg_stratpoint6, true)
			
			Cmd_Move(sg_temp, Util_GetPosition(eg_stratpoint7), true)
			Command_SquadEntity(player2, sg_temp, SCMD_Capture, eg_stratpoint7, true)
			Cmd_Move(sg_temp, Util_GetPosition(eg_stratpoint8), true)
			Command_SquadEntity(player2, sg_temp, SCMD_Capture, eg_stratpoint8, true)
			Cmd_Move(sg_temp, Util_GetPosition(eg_stratpoint9), true)
			Command_SquadEntity(player2, sg_temp, SCMD_Capture, eg_stratpoint9, true)
		end
	end
end 





---------------
-- ARTILLERY --
---------------

-- Onto Beach from Rocket Trucks
--Must have sg group and unit added on WB -- Done -- If this doesnt work cos of range, use cw_realarty instead


function StartRocketFire()

Rule_AddInterval(Rocketfire1, World_GetRand(80, 120))
Rule_AddInterval(Rocketfire2, World_GetRand(80, 120))
Rule_AddInterval(Rocketfire3, World_GetRand(80, 120))

--Cmd_Ability(sg_rockettruck1, ABILITY.ELITE.PHZ89_LOCKDOWN)
--Cmd_Ability(sg_sg_rockettruck2, ABILITY.ELITE.PHZ89_LOCKDOWN)
--Cmd_Ability(sg_rockettruck3, ABILITY.ELITE.PHZ89_LOCKDOWN)

end


function Rocketfire1() 

	if SGroup_GetAvgHealth(sg_rockettruck1) > 0.01 then

	local rocktar1 = Util_GetPosition(Marker_FromName("artytarget45", "red_marker"))
	Cmd_Ability(sg_rockettruck1, ABILITY.ELITE.PHZ89_BARRAGE, Util_GetRandomOffset(Util_GetPosition(rocktar1), -35, 35), true)
	local randpick = World_GetRand(1, 3)
	--Cmd_Move(sg_rockettruck1, Util_GetPosition(Marker_FromName("m_arty1" .. randpick, "green_marker")), true) 

	elseif SGroup_GetAvgHealth(sg_rockettruck1) == 0 then
	Rule_RemoveMe()
 
	end
end



function Rocketfire2()

	if SGroup_GetAvgHealth(sg_sg_rockettruck2) > 0.01 then

	local rocktar1 = Util_GetPosition(Marker_FromName("artytarget45", "red_marker"))
	Cmd_Ability(sg_sg_rockettruck2, ABILITY.ELITE.PHZ89_BARRAGE, Util_GetRandomOffset(Util_GetPosition(rocktar1), -35, 35), true)
	local randpick = World_GetRand(1, 3)
	--Cmd_Move(sg_sg_rockettruck2, Util_GetPosition(Marker_FromName("m_arty2" .. randpick, "green_marker")), true) 

	elseif SGroup_GetAvgHealth(sg_sg_rockettruck2) == 0 then
	Rule_RemoveMe()
 
	end
end



function Rocketfire3() 

	if SGroup_GetAvgHealth(sg_rockettruck3) > 0.01 then

	local rocktar1 = Util_GetPosition(Marker_FromName("artytarget45", "red_marker"))
	Cmd_Ability(sg_rockettruck3, ABILITY.ELITE.PHZ89_BARRAGE, Util_GetRandomOffset(Util_GetPosition(rocktar1), -35, 35), true)
	local randpick = World_GetRand(1, 3)
	--Cmd_Move(sg_rockettruck3, Util_GetPosition(Marker_FromName("m_arty3" .. randpick, "green_marker")), true) 

	elseif SGroup_GetAvgHealth(sg_rockettruck3) == 0 then
	Rule_RemoveMe()
 
	end
end



function FinalBarrage()

local bridge_targeta = Util_GetPosition(Marker_FromName("finalarty" .. World_GetRand(1, 6), "red_marker"))
local bridge_targetb = Util_GetPosition(Marker_FromName("finalarty" .. World_GetRand(1, 6), "red_marker"))

Cmd_Ability(player2, ABILITY.COMMANDER_TREE.ELITE.PHL03_BARRAGE, Util_GetRandomOffset(Util_GetPosition(bridge_targeta), -30, 30), nil, true)
Cmd_Ability(player2, ABILITY.COMMANDER_TREE.ELITE.PHL03_BARRAGE, Util_GetRandomOffset(Util_GetPosition(bridge_targetb), -30, 30), nil, true)

end



--Use for beach assault

function smallstartarty()

local picktype1 = World_GetRand(1, 4)

	if picktype1 == 1 then

	artyattacktype1 = ABILITY.SP.SINGLE_CREEPING_SMOKE
	artyattacktype2 = ABILITY.SP.SINGLE_MORTAR

	elseif picktype1 == 2 then

	artyattacktype1 = ABILITY.SP.SINGLE_MORTAR
	artyattacktype2 = ABILITY.SP.SINGLE_HOWITZER_ALTSOUND

	elseif picktype1 == 3 then

	artyattacktype1 = ABILITY.SP.SINGLE_MORTAR
	artyattacktype2 = ABILITY.SP.SINGLE_HOWITZER

	elseif picktype1 == 4 then

	artyattacktype1 = ABILITY.SP.SINGLE_CREEPING_SMOKE
	artyattacktype2 = ABILITY.SP.SINGLE_HOWITZER

	end

	for i = 1, max_artillery do

	local bridge_targeta = Util_GetPosition(Marker_FromName("arty" .. World_GetRand(1, 4), "red_marker"))
	local bridge_targetb = Util_GetPosition(Marker_FromName("arty" .. World_GetRand(1, 4), "red_marker"))
	local bridge_targetc = Util_GetPosition(Marker_FromName("arty" .. World_GetRand(1, 4), "red_marker"))
	local bridge_targetd = Util_GetPosition(Marker_FromName("arty" .. World_GetRand(1, 4), "red_marker"))

	Cmd_Ability(player2, artyattacktype1, Util_GetRandomOffset(Util_GetPosition(bridge_targeta), -30, 30), nil, true)
	Cmd_Ability(player2, artyattacktype1, Util_GetRandomOffset(Util_GetPosition(bridge_targetb), -30, 30), nil, true)
	Cmd_Ability(player2, artyattacktype2, Util_GetRandomOffset(Util_GetPosition(bridge_targetc), -30, 30), nil, true)
	Cmd_Ability(player2, artyattacktype2, Util_GetRandomOffset(Util_GetPosition(bridge_targetd), -30, 30), nil, true)

	end
end


-----------
-- TANKS --
-----------

function TankManager()

--Rule_AddOneShot(MoveSentryTank, World_GetRand(30, 180))
Rule_AddOneShot(MoveResponseTank1, World_GetRand(30, 240))
Rule_AddOneShot(MoveResponseTank2, World_GetRand(15, 210))

end


function MoveSentryTank()

Cmd_AttackMove(sg_sentrytank, Util_GetPosition(Marker_FromName("m_rein11", "cyan_marker")), true)
Cmd_AttackMove(sg_sentrytank, Util_GetPosition(Marker_FromName("sent_14", "basic_marker")), true)
Cmd_AttackMove(sg_sentrytank, Util_GetPosition(Marker_FromName("m_aa2", "cyan_marker")), true)

end


function MoveResponseTank1()

Cmd_AttackMove(sg_responsetank1, Util_GetPosition(Marker_FromName("m_rein32", "cyan_marker")), true)
Cmd_AttackMove(sg_responsetank1, Util_GetPosition(Marker_FromName("m_rein33", "cyan_marker")), true)
--Cmd_AttackMove(sg_responsetank1, Util_GetPosition(Marker_FromName("m_rein34", "cyan_marker")), true)
Cmd_AttackMove(sg_responsetank1, Util_GetPosition(Marker_FromName("m_ass2", "cyan_marker")), true)
Cmd_AttackMove(sg_responsetank1, Util_GetPosition(Marker_FromName("m_ass5", "cyan_marker")), true)
Cmd_AttackMove(sg_responsetank1, Util_GetPosition(eg_beachhead), true)

end


function MoveResponseTank2()

Cmd_AttackMove(sg_responsetank2, Util_GetPosition(Marker_FromName("m_rein23", "cyan_marker")), true)
Cmd_AttackMove(sg_responsetank2, Util_GetPosition(Marker_FromName("m_rein24", "cyan_marker")), true)
Cmd_AttackMove(sg_responsetank2, Util_GetPosition(Marker_FromName("s_ass5", "basic_marker")), true)

end



-------------------
---Attack Manager--
-------------------

function attackmanager()

--Add, change or remove wave types/times
--Alter wave sizes and weapon numbers
--Increase veterancy
--Might need to add more coop difficulty settings for missions 4-6

-------------------
----By Mission ----
-------------------

 if MissionNo == 1 and mission1 == false then

	if COOP == 0 then

		max_artillery = 2
		dif_apc_no = 1
		dif_infrein_no = 1
		dif_support_no = 1

		types_infrein = {SBP.ELITE.PLA_ENGINEER, SBP.ELITE.MILITIA, SBP.ELITE.MILITIA}
		types_support = {SBP.ELITE.LIGHT_AT, SBP.ELITE.LIGHT_AT, SBP.ELITE.REGULAR_MG}
		types_apc = {SBP.ELITE.ATMP_RECON, SBP.ELITE.ATMP_RECON, SBP.ELITE.ZSD_89} -- ZBD AC, ZSD APC

		TankManager()

		InfantryAttackForces()		
		SupportForces()
		LightArmourForces()

		if AMBUSHREADY == 1 then -- DELAY FROM COMMS DOWN

		Rule_AddInterval(InfantryAttackForces, 200)
		Rule_AddInterval(SupportForces, 300)
		Rule_AddInterval(LightArmourForces, 325)
		Rule_AddInterval(CaptureTeams, 245)

		elseif AMBUSHREADY == 0 then -- COMMS STILL UP

		Rule_AddInterval(InfantryAttackForces, 180)
		Rule_AddInterval(SupportForces, 255)
		Rule_AddInterval(LightArmourForces, 280)
		Rule_AddInterval(CaptureTeams, 230)

		end


	elseif COOP == 1 then

		max_artillery = 2
		dif_apc_no = 1
		dif_infrein_no = 3
		dif_support_no = 2

		types_infrein = {SBP.ELITE.PLA_ENGINEER, SBP.ELITE.MILITIA, SBP.ELITE.MILITIA}
		types_support = {SBP.ELITE.LIGHT_AT, SBP.ELITE.LIGHT_AT, SBP.ELITE.REGULAR_MG}
		types_apc = {SBP.ELITE.ATMP_RECON, SBP.ELITE.ZSD_89} -- ZBD AC, ZSD APC

		TankManager()

		InfantryAttackForces()
		Rule_AddInterval(InfantryAttackForces, 150)

		SupportForces()
		Rule_AddInterval(SupportForces, 195)

		LightArmourForces()
		Rule_AddInterval(LightArmourForces, 225)

		Rule_AddInterval(CaptureTeams, 180)

	end	
		
		mission1 = true

--------------------------------------------------------------------------------------

 elseif MissionNo == 2 and mission2 == false then

	if COOP == 0 then

		max_artillery = 3
		dif_apc_no = 1
		dif_infrein_no = 2
		dif_support_no = 2

		Cmd_InstantUpgrade(player2, UPG.COMMANDER_TREE.ELITE.FUTURE_IS_NEAR_10, ITEM_UNLOCKED)

		types_infrein = {SBP.ELITE.PLA_ENGINEER, SBP.ELITE.MILITIA, SBP.ELITE.REGULAR}
		types_support = {SBP.ELITE.LIGHT_AT, SBP.ELITE.LIGHT_AT, SBP.ELITE.REGULAR_MG, SBP.ELITE.MORTAR} -- 89 marder, 86 AT
		types_apc = {SBP.ELITE.ATMP_RECON, SBP.ELITE.ZBD2K, SBP.ELITE.ZSD_89} -- ZBD AC, ZSD APC

	elseif COOP == 1 then

		max_artillery = 4
		dif_apc_no = 1
		dif_infrein_no = 4
		dif_support_no = 3

		Cmd_InstantUpgrade(player2, UPG.COMMANDER_TREE.ELITE.FUTURE_IS_NEAR_10, ITEM_UNLOCKED)

		types_infrein = {SBP.ELITE.PLA_ENGINEER, SBP.ELITE.MILITIA, SBP.ELITE.REGULAR}
		types_support = {SBP.ELITE.LIGHT_AT, SBP.ELITE.LIGHT_AT, SBP.ELITE.PF98_AT, SBP.ELITE.REGULAR_MG, SBP.ELITE.MORTAR, 				SBP.ELITE.SNIPER, SBP.ELITE.TYPE_86_ATG} -- 89 marder, 86 AT
		types_apc = {SBP.ELITE.ZBD2K, SBP.ELITE.ZSD_89} -- ZBD AC, ZSD APC

	end

		mission2 = true

--------------------------------------------------------------------------------------

 elseif MissionNo == 3 and mission3 == false then

	if COOP == 0 then

		max_artillery = 3
		dif_tank_no = 1
		dif_apc_no = 1
		dif_infrein_no = 3
		dif_support_no = 1

		Rule_AddOneShot(CounterHeavyArmourForces, World_GetRand(15, 160))

		types_tank = {SBP.ELITE.ZBD2K}
		types_infrein = {SBP.ELITE.REGULAR}
		types_support = {SBP.ELITE.LIGHT_AT, SBP.ELITE.PF98_AT, SBP.ELITE.REGULAR_MG, SBP.ELITE.MORTAR, SBP.ELITE.SNIPER, SBP.ELITE.TYPE_86_ATG, SBP.ELITE.TYPE_89} -- 89 marder, 86 AT
		types_apc = {SBP.ELITE.ZBD2K, SBP.ELITE.ZSD_89} -- ZBD AC, ZSD APC

	elseif COOP == 1 then

		max_artillery = 3
		dif_tank_no = 1
		dif_apc_no = 1
		dif_infrein_no = 3
		dif_support_no = 2

		Rule_AddOneShot(CounterHeavyArmourForces, World_GetRand(15, 160))

		types_tank = {SBP.ELITE.TYPE_59D}
		types_infrein = {SBP.ELITE.REGULAR}
		types_support = {SBP.ELITE.LIGHT_AT, SBP.ELITE.PF98_AT, SBP.ELITE.REGULAR_MG, SBP.ELITE.MORTAR, SBP.ELITE.SNIPER, SBP.ELITE.TYPE_86_ATG, SBP.ELITE.TYPE_89} -- 89 marder, 86 AT
		types_apc = {SBP.ELITE.ZBD2K, SBP.ELITE.ZSD_89} -- ZBD AC, ZSD APC

	end

		mission3 = true
 end

end

-------------------------------------------------------------------------------------- STOP, DIGIN, COUNTER
-------------------------------------------------------------------------------------- STOP, DIGIN, COUNTER
-------------------------------------------------------------------------------------- STOP, DIGIN, COUNTER

function CounterManager()

 if CounterNo == 4 and mission4 == false then

		types_tank = {SBP.ELITE.TYPE_59D, SBP.ELITE.TYPE_59D, SBP.ELITE.TYPE_96} --59D medium, 96 heavy, 99 super heavy

		dif_apc_no = 3
		dif_tank_no = 1
		dif_infrein_no = 4
		dif_support_no = 2 

		Cmd_InstantUpgrade(player2, UPG.COMMANDER_TREE.ELITE.FUTURE_IS_NEAR_00, ITEM_UNLOCKED)

		CounterHeavyArmourForces()
		Rule_AddInterval(CounterHeavyArmourForces, 100)

		CounterInfantryAttackForces()
		Rule_AddInterval(CounterInfantryAttackForces, 40)

		CounterSupportForces()
		Rule_AddInterval(CounterSupportForces, 70)

		CounterLightArmourForces()
		Rule_AddInterval(CounterLightArmourForces, 55)

		Rule_Remove(CaptureTeams)

		mission4 = true

--------------------------------------------------------------------------------------

 elseif CounterNo == 5 and mission5 == false then

		types_tank = {SBP.ELITE.TYPE_59D, SBP.ELITE.TYPE_59D, SBP.ELITE.TYPE_96, SBP.ELITE.TYPE_96} --59D medium, 96 heavy, 99 super heavy
		dif_apc_no = 4
		dif_tank_no = 3
		dif_infrein_no = 6
		dif_support_no = 3

		vet_inf = 1
		vet_apc = 2
		vet_tank = 1
		vet_support = 1

			if COOP == 1 then
				vet_inf = 2
				vet_apc = 2
				vet_tank = 1
				vet_support = 1
			end

		mission5 = true

--------------------------------------------------------------------------------------

 elseif CounterNo == 6 and mission6 == false then

		types_tank = {SBP.ELITE.TYPE_96, SBP.ELITE.TYPE_99} --59D medium, 96 heavy, 99 super heavy
		dif_apc_no = 10
		dif_tank_no = 10
		dif_infrein_no = 10
		dif_support_no = 10 --use up the rest of their forces quickly

		vet_inf = 2
		vet_apc = 2
		vet_tank = 2
		vet_support = 2

			if COOP == 1 then
				vet_inf = 3
				vet_apc = 2
				vet_tank = 2
				vet_support = 2
			end

		mission6 = true

--------------------------------------------------------------------------------------
 end
end


--------------------
---Mission Manager--
--------------------

function missioncounter()

MissionNo = MissionNo + 1

end


function countercounter()

CounterNo = CounterNo + 1

end


function stopattacks()

Rule_Remove(missioncounter)
Rule_Remove(InfantryAttackForces)
Rule_Remove(SupportForces)
Rule_Remove(LightArmourForces)

--Rule_AddOneShot(resumeattacks, 240)

	if Rule_Exists(smallstartarty) then
	Rule_Remove(smallstartarty)
	end
end


function resumeattacks()

Rule_AddInterval(CounterManager, 15)
Rule_AddInterval(countercounter, 240)

end

--------------------------
-- EARLY REINFORCEMENTS --
--------------------------

function InfantryAttackForces()
	--Infantry troop reinforcement, move down main road to Beach only

	for i = 1, dif_infrein_no do
		local sg_temp = SGroup_CreateIfNotFound("sg_inf" .. i)
		
		if SGroup_IsEmpty(sg_temp) then
			Util_CreateSquads(player2, sg_temp, types_infrein, Marker_FromName("s_rein1", "basic_marker"), nil, 1)

			local attackormove = World_GetRand(1, 3)
			if attackormove < 3 then

			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein11", "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein12", "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein13", "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein14", "cyan_marker")), true)

			elseif attackormove == 3 then
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein11", "cyan_marker")), true)
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein12", "cyan_marker")), true)
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein13", "cyan_marker")), true)
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein14", "cyan_marker")), true)			
			end

			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("s_ass" .. World_GetRand(1, 5), "basic_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("s_ass" .. World_GetRand(1, 5), "basic_marker")), true)	
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("s_ass" .. World_GetRand(1, 5), "basic_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("s_ass" .. World_GetRand(1, 5), "basic_marker")), true)	
		end
	end
end 



function LightArmourForces()
	--Vehicle reinforcement, move down main road to Beach only

	for i = 1, dif_apc_no do
		local sg_temp = SGroup_CreateIfNotFound("sg_lightveh" .. i)
		
		if SGroup_IsEmpty(sg_temp) then
			Util_CreateSquads(player2, sg_temp, types_apc, Marker_FromName("s_rein1", "basic_marker"), nil, 1)

			local attackormove = World_GetRand(1, 3)
			if attackormove < 3 then

			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein11", "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein12", "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein13", "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein14", "cyan_marker")), true)

			elseif attackormove == 3 then
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein11", "cyan_marker")), true)
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein12", "cyan_marker")), true)
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein13", "cyan_marker")), true)
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein14", "cyan_marker")), true)			
			end

			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("s_ass" .. World_GetRand(1, 5), "basic_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("s_ass" .. World_GetRand(1, 5), "basic_marker")), true)	
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("s_ass" .. World_GetRand(1, 5), "basic_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("s_ass" .. World_GetRand(1, 5), "basic_marker")), true)	
		end
	end
end 



function SupportForces()
	--Support reinforcement, move down main road to Beach only

	for i = 1, dif_support_no do
		local sg_temp = SGroup_CreateIfNotFound("sg_support" .. i)
		
		if SGroup_IsEmpty(sg_temp) then
			Util_CreateSquads(player2, sg_temp, types_support, Marker_FromName("s_rein1", "basic_marker"), nil, 1)

			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein11", "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein12", "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein13", "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein14", "cyan_marker")), true)

			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("s_ass" .. World_GetRand(1, 5), "basic_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("s_ass" .. World_GetRand(1, 5), "basic_marker")), true)	
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("s_ass" .. World_GetRand(1, 5), "basic_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("s_ass" .. World_GetRand(1, 5), "basic_marker")), true)	
		end
	end
end 


--------------------------
-- LATER REINFORCEMENTS --
--------------------------

--Programmed with alternative routes
--Programmed with counter for units lost

function CounterInfantryAttackForces()
	--Infantry troops multiple attack routes

	for i = 1, dif_infrein_no do
		local sg_temp = SGroup_CreateIfNotFound("sg_inf" .. i)
		
		if SGroup_IsEmpty(sg_temp) and chineseinfno > 0 then
			chineseinfno = chineseinfno - 1
			local chooseroute = World_GetRand(1, 6)
			Util_CreateSquads(player2, sg_temp, types_infrein, Marker_FromName("s_rein" .. chooseroute, "basic_marker"), nil, 1)
			SGroup_IncreaseVeterancyRank(sg_temp, vet_inf, true)			

			local attackormove = World_GetRand(1, 3)
			if attackormove < 3 then

			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 1, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 2, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 3, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 4, "cyan_marker")), true)

			elseif attackormove == 3 then
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 1, "cyan_marker")), true)
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 2, "cyan_marker")), true)
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 3, "cyan_marker")), true)
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 4, "cyan_marker")), true)			
			end

			Cmd_AttackMove(sg_temp, Util_GetPosition(eg_beachhead), true)
			Command_SquadEntity(player2, sg_temp, SCMD_Capture, eg_beachhead, true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("s_ass5", "basic_marker")), true)

			local unitcount = chineseinfno - 1
			chineseinfno = unitcount

		elseif chineseinfno < 1 then
 
		Rule_RemoveMe()

		end
	end
end 


function CounterLightArmourForces()
	--APC troops multiple attack routes

	for i = 1, dif_apc_no do
		local sg_temp = SGroup_CreateIfNotFound("sg_light" .. i)
	
		if SGroup_IsEmpty(sg_temp) and chineseapcno > 0 then
			chineseapcno = chineseapcno - 1
			local chooseroute = World_GetRand(1, 5)	
			Util_CreateSquads(player2, sg_temp, types_apc, Marker_FromName("s_rein" .. chooseroute, "basic_marker"), nil, 1)
			SGroup_IncreaseVeterancyRank(sg_temp, vet_apc, true)

			local attackormove = World_GetRand(1, 3)
			if attackormove < 3 then

			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 1, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 2, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 3, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 4, "cyan_marker")), true)

			elseif attackormove == 3 then
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 1, "cyan_marker")), true)
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 2, "cyan_marker")), true)
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 3, "cyan_marker")), true)
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 4, "cyan_marker")), true)			
			end

			Cmd_AttackMove(sg_temp, Util_GetPosition(eg_beachhead), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("s_ass5", "basic_marker")), true)

			local unitcount = chineseapcno - 1
			chineseapcno = unitcount

		elseif chineseapcno < 1 then
 
		Rule_RemoveMe()

		end
	end
end 


function CounterHeavyArmourForces()
	--Tanks multiple attack routes

	for i = 1, dif_tank_no do
		local sg_temp = SGroup_CreateIfNotFound("sg_tank" .. i)
		
		if SGroup_IsEmpty(sg_temp) and chinesetankno > 0 then
			local chooseroute = World_GetRand(1, 5)
			Util_CreateSquads(player2, sg_temp, types_tank, Marker_FromName("s_rein" .. chooseroute, "basic_marker"), nil, 1)
			SGroup_IncreaseVeterancyRank(sg_temp, vet_tank, true)

			local attackormove = World_GetRand(1, 3)
			if attackormove < 3 then

			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 1, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 2, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 3, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 4, "cyan_marker")), true)

			elseif attackormove == 3 then
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 1, "cyan_marker")), true)
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 2, "cyan_marker")), true)
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 3, "cyan_marker")), true)
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 4, "cyan_marker")), true)			
			end

			Cmd_AttackMove(sg_temp, Util_GetPosition(eg_beachhead), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("s_ass5", "basic_marker")), true)

			chinesetankno = chinesetankno - 1

		elseif chinesetankno < 1 then
 
		Rule_RemoveMe()

		end
	end
end 


function CounterSupportForces()
	--Support multiple attack routes

	for i = 1, dif_support_no do
		local sg_temp = SGroup_CreateIfNotFound("sg_support" .. i)
		
		if SGroup_IsEmpty(sg_temp) and chinesesupportno > 0 then
			chinesesupportno = chinesesupportno - 1
			local chooseroute = World_GetRand(1, 6)
			Util_CreateSquads(player2, sg_temp, types_support, Marker_FromName("s_rein" .. chooseroute, "basic_marker"), nil, 1)
			SGroup_IncreaseVeterancyRank(sg_temp, vet_support, true)

			local attackormove = World_GetRand(1, 3)
			if attackormove < 3 then

			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 1, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 2, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 3, "cyan_marker")), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 4, "cyan_marker")), true)

			elseif attackormove == 3 then
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 1, "cyan_marker")), true)
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 2, "cyan_marker")), true)
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 3, "cyan_marker")), true)
			Cmd_Move(sg_temp, Util_GetPosition(Marker_FromName("m_rein" .. chooseroute .. 4, "cyan_marker")), true)			
			end

			Cmd_AttackMove(sg_temp, Util_GetPosition(eg_beachhead), true)
			Cmd_AttackMove(sg_temp, Util_GetPosition(Marker_FromName("s_ass5", "basic_marker")), true)

			local unitcount = chinesesupportno - 1
			chinesesupportno = unitcount

		elseif chinesesupportno < 1 then
 
		Rule_RemoveMe()

		end
	end
end 


---------------ADDITIONAL SP STUFF

function buildp2defences() -------DEFUNCT


end


function setupp2defences()


end




----------------
-- OBJECTIVES --
----------------

---------------- Check players still have troops before assault

function checkloss()

local p1squadcount = Player_GetUnitCount(player1) 

	if p1squadcount == 0 then

	LoseSpecOpsMovieManager() --Play ambushed movie

	end
end



---------------- Wait for Assault (P)

function startAssaultmission()

ObjectiveInit_Assault()
Objective_Start(OBJECTIVE_Assault, true)

end


function ObjectiveInit_Assault()

local Assault_TITLE = Util_CreateLocStringfromID(14000050)
local Assault_STARTMSG = Util_CreateLocStringfromID(14000051)
local Assault_HINT = Util_CreateLocStringfromID(14000052) 


        OBJECTIVE_Assault =
        {
                SetupUI = function()
                OBJECTIVE_Assault.HintID = HintPoint_Add(s_ass1, true, Assault_HINT)  
                OBJECTIVE_Assault.HintID1 = HintPoint_Add(s_ass5, true, Assault_HINT) 
                end,

                OnStart = function()  
			Objective_StartTimer(OBJECTIVE_Assault, COUNT_DOWN, InfiltrateTimer * 60)
                        Rule_AddInterval(ObjectiveCheck_Assault, 10)                                       
                        --Util_PlayMusic("SOUND/Music/omg/motivation/the_blitz_v2", 6, 0)
                end,

                OnComplete = function()
                end,

                IsComplete = function()
                        return false
                end,

                OnFail = function()
                end,

                Title = Assault_TITLE,
                Description = Assault_TITLE,
                DisplayTitleStart = Assault_STARTMSG,
                Icon = IT_P_Capture,
                Type = OT_Primary,
        }

        Objective_Register(OBJECTIVE_Assault)                    

end


function ObjectiveCheck_Assault()
        -- Check if done

	if Objective_GetTimerSeconds(OBJECTIVE_Assault) < 1 then 

        Objective_Complete(OBJECTIVE_Assault)
        HintPoint_Remove(OBJECTIVE_Assault.HintID)
        HintPoint_Remove(OBJECTIVE_Assault.HintID1)

	ASSAULTCOMPLETE = 1

	LandingWait() -- Start wait for Landing

        Rule_RemoveMe()
                                                                   
	end
end



---------------- Establish Ambush Point (S)

function startAmbushmission()

ObjectiveInit_Ambush()
Objective_Start(OBJECTIVE_Ambush, true)
ShowDesignateMessage = 1

end


function ObjectiveInit_Ambush()

local Ambush_TITLE = Util_CreateLocStringfromID(14000053)
local Ambush_STARTMSG = Util_CreateLocStringfromID(14000054)
local Ambush_HINT = Util_CreateLocStringfromID(14000055) 


        OBJECTIVE_Ambush =
        {
                SetupUI = function()
                
                OBJECTIVE_Ambush.PingID = Objective_AddUIElements(OBJECTIVE_Ambush, ambush1, true, Ambush_Hint, true)
		UI_ShowTacticalMap()

		arrow1 = Util_GetPosition(Marker_FromName("s_sf1", "basic_marker"))
		arrow2 = Util_GetPosition(Marker_FromName("ambush1", "blue_marker"))

		TacticalMap_AddArrow(1, FN_OnShow, arrow1, arrow2) 
		TacticalMap_AddMessage(1, FN_OnShow, arrow2, Ambush_HINT)	

		--OBJECTIVE_Ambush.HintID = HintPoint_Add(ambush1, true, Ambush_HINT) 
		--UI_CreateMinimapBlip(ambush1, -1, BT_ObjectivePrimary)
                --OBJECTIVE_Ambush.HintID1 = HintPoint_Add(ambush2, true, Ambush_HINT) 
                end,

                OnStart = function()   
			Rule_AddInterval(Objective_AmbushCheck, 10) 
			Rule_AddInterval(CheckDesignation, 5)                                        
                        Util_PlayMusic("SOUND/Music/omg/motivation/the_blitz_v2", 6, 0)
                end,

                OnComplete = function()
                end,

                IsComplete = function()
                        return false
                end,

                OnFail = function()
                end,

                Title = Ambush_TITLE,
                Description = Ambush_TITLE,
                DisplayTitleStart = Ambush_STARTMSG,
                Icon = IT_P_Capture,
                Type = OT_Primary,
        }

        Objective_Register(OBJECTIVE_Ambush)                    

end


function Objective_AmbushCheck() -- Call this from later mission to end objective

	if Prox_ArePlayersNearMarker(player1, ambush1, ANY, 50) and ShowDesignateMessage == 1 then

		ShowDesignateMessage = 0

		allywin4msg = Util_CreateLocStringfromID(14000056)
		Util_GlobalMessage(allywin4msg, 5)
	end


	if EGroup_GetAvgHealth(eg_comms) == 0 then

		Objective_Complete(OBJECTIVE_Ambush)
        	UI_DeleteMinimapBlip(OBJECTIVE_Ambush.PingID)
        	HintPoint_Remove(OBJECTIVE_Ambush.HintID)
        	HintPoint_Remove(OBJECTIVE_Ambush.HintID1)
	
	AMBUSHREADY = 1

	Rule_RemoveMe()

	elseif ASSAULTCOMPLETE == 1 then

	        Objective_Fail(OBJECTIVE_Ambush)
        	UI_DeleteMinimapBlip(OBJECTIVE_Ambush.PingID)
        	HintPoint_Remove(OBJECTIVE_Ambush.HintID)
        	HintPoint_Remove(OBJECTIVE_Ambush.HintID1)

	--chinesewin4()

	Rule_RemoveMe()

	end

end


function CheckDesignation()

	if EGroup_HasUpgrade(eg_comms, UPG.SP.PA_IS_DESIGNATED, ANY) then

	JDAMMovieManager()
	Rule_RemoveMe()

	elseif EGroup_GetAvgHealth(eg_comms) == 0 then

	Rule_RemoveMe()

	end
end


---------------- Destroy the AA positions (S)

function startAAmission()

ObjectiveInit_AA()
Objective_Start(OBJECTIVE_AA, true)

end


function ObjectiveInit_AA()

local AA_TITLE = Util_CreateLocStringfromID(14000057)
local AA_STARTMSG = Util_CreateLocStringfromID(14000058)
local AA_HINT = Util_CreateLocStringfromID(14000077) 


        OBJECTIVE_AA =
        {
                SetupUI = function()
                OBJECTIVE_AA.PingID = UI_CreateMinimapBlip(sg_antiaira, -1, BT_ObjectiveSecondary)
                OBJECTIVE_AA.PingID1 = UI_CreateMinimapBlip(sg_antiairb, -1, BT_ObjectiveSecondary) 
                OBJECTIVE_AA.HintID = HintPoint_Add(sg_antiaira, true, AA_HINT)  
                OBJECTIVE_AA.HintID1 = HintPoint_Add(sg_antiairb, true, AA_HINT) 
                end,

                OnStart = function()     
                        Rule_AddInterval(ObjectiveCheck_AA, 5)                                       
                        Util_PlayMusic("SOUND/Music/omg/motivation/the_blitz_v2", 6, 0)
                end,

                OnComplete = function()
                end,

                IsComplete = function()
                        return false
                end,

                OnFail = function()
                end,

                Title = AA_TITLE,
                Description = AA_TITLE,
                DisplayTitleStart = AA_STARTMSG,
                Icon = IT_P_Capture,
                Type = OT_Secondary,
        }

        Objective_Register(OBJECTIVE_AA)                    

end


function ObjectiveCheck_AA()
        -- Check if done

	if SGroup_GetAvgHealth(sg_antiaira) == 0 and SGroup_GetAvgHealth(sg_antiairb) == 0 then 

        Objective_Complete(OBJECTIVE_AA)
        UI_DeleteMinimapBlip(OBJECTIVE_AA.PingID)
        HintPoint_Remove(OBJECTIVE_AA.HintID)
        UI_DeleteMinimapBlip(OBJECTIVE_AA.PingID1)
        HintPoint_Remove(OBJECTIVE_AA.HintID1)

        --Unlock air power when assault begins, start medal mission
		
	AIRSUPPORT = 1
	Rule_AddOneShot(startSniperMedalmission, 10)

        Rule_RemoveMe()
                                                                   
	end
end



function LandingWait()

Rule_AddOneShot(AssaultMovieManager, 6)

end


function CheckAirSupport()

	if AIRSUPPORT == 1 then

	Cmd_InstantUpgrade(player1, UPG.COMMANDER_TREE.CW.AIR_DOMINANCE_00, ITEM_UNLOCKED) --Little Bird
	Cmd_InstantUpgrade(player1, UPG.COMMANDER_TREE.CW.AIR_DOMINANCE_01, ITEM_UNLOCKED) --A10
	Cmd_InstantUpgrade(player1, UPG.SP.PA_APACHE, ITEM_UNLOCKED) --Apache
	Cmd_InstantUpgrade(player1, UPG.COMMANDER_TREE.CW.AIR_DOMINANCE_12, ITEM_UNLOCKED) --Combat Drop

	Rule_RemoveMe()

	end
end


function LandingWaitResume()

--Rule_AddOneShot(EndObjective_Ambush, 8)
Rule_AddOneShot(startLandingmission, 16)

--Rule_AddOneShot(startAAmission, 40)

--BEACH ARTILLERY START
Modify_PlayerResourceRate(player1, RT_Manpower, 2500) --begin resource collection
StartRocketFire() --Unit Artillery
UStechUp() --Upgrade tech
Rule_Remove(checkloss)
Rule_AddInterval(smallstartarty, World_GetRand(50, 60)) --Off map Artillery 
Rule_AddOneShot(startMLRSMedalmission, 60)

end


---------------- Begin Landing, capture Beachhead (P)

function startLandingmission()

ObjectiveInit_Landing()
Objective_Start(OBJECTIVE_Landing, true)

end


function ObjectiveInit_Landing()

local Landing_TITLE = Util_CreateLocStringfromID(14000059)
local Landing_STARTMSG = Util_CreateLocStringfromID(14000060)
local Landing_HINT = Util_CreateLocStringfromID(14000061) 


        OBJECTIVE_Landing =
        {
                SetupUI = function()
                OBJECTIVE_Landing.PingID = UI_CreateMinimapBlip(eg_beachhead, -1, BT_ObjectivePrimary)
                --OBJECTIVE_Landing.PingID1 = UI_CreateMinimapBlip(s_ass5, -1, BT_ObjectivePrimary) 
                OBJECTIVE_Landing.HintID = HintPoint_Add(eg_beachhead, true, Landing_HINT)   
                end,

                OnStart = function()      
			Objective_StartTimer(OBJECTIVE_Landing, COUNT_DOWN, BeachheadTimer * 60)			
                        Rule_AddInterval(ObjectiveCheck_Landing, 5) 
			Rule_AddInterval(ReinforcementCheck, 90)  
			Rule_AddInterval(CheckAirSupport, 5)  

			StartBeachForces()
			SmokeCover()
			missioncounter()
			Rule_AddInterval(missioncounter, 240)

			ALARMON = 0 --turn off Alarm response
			Retreat_Threshold_Beneath = 0.2 --Sentry squads retreat later now
			Retreat_Threshold_Above = 0.19

                        --Util_PlayMusic("SOUND/Music/omg/motivation/the_blitz_v2", 6, 0)
                end,

                OnComplete = function()
                end,

                IsComplete = function()
                        return false
                end,

                OnFail = function()
                end,

                Title = Landing_TITLE,
                Description = Landing_TITLE,
                DisplayTitleStart = Landing_STARTMSG,
                Icon = IT_P_Capture,
                Type = OT_Primary,
        }

        Objective_Register(OBJECTIVE_Landing)                    

end


function ObjectiveCheck_Landing()
        -- Check if done

	if Objective_GetTimerSeconds(OBJECTIVE_Landing) < 1 then 

        Objective_Fail(OBJECTIVE_Landing)
        UI_DeleteMinimapBlip(OBJECTIVE_Landing.PingID)
        HintPoint_Remove(OBJECTIVE_Landing.HintID)
        --UI_DeleteMinimapBlip(OBJECTIVE_Landing.PingID1)

	chinesewin1()

	Rule_RemoveMe()

	elseif EGroup_IsCapturedByPlayer(eg_beachhead, player1, ANY) then

        Objective_Complete(OBJECTIVE_Landing)
        UI_DeleteMinimapBlip(OBJECTIVE_Landing.PingID)
        HintPoint_Remove(OBJECTIVE_Landing.HintID)
        --UI_DeleteMinimapBlip(OBJECTIVE_Landing.PingID1)

	--REMAININGTIME = Objective_GetTimerSeconds(OBJECTIVE_Landing)

	DiginWait()

        Rule_RemoveMe()

	end
end


function ReinforcementCheck()

	if usinfrein > 0 or usapcrein > 0 or ustankrein > 0 then
		
	reinmsg = Util_CreateLocString((Loc_ToAnsi(14000062))..(usinfrein)..(Loc_ToAnsi(14000063))..(usapcrein))
	Util_GlobalMessage(reinmsg, 5)

	elseif usinfrein == 0 and usapcrein == 0 and ustankrein == 0 then
	Rule_RemoveMe()

	end
end


function SmokeCover()

SmokeEntrance_Do(Marker_FromName("s_ass1", "basic_marker"))
SmokeEntrance_Do(Marker_FromName("s_ass2", "basic_marker"))
SmokeEntrance_Do(Marker_FromName("s_ass3", "basic_marker"))
SmokeEntrance_Do(Marker_FromName("s_ass4", "basic_marker"))
SmokeEntrance_Do(Marker_FromName("s_ass5", "basic_marker"))

end



function DiginWait()

Rule_AddOneShot(startDiginmission, 10)
stopattacks() -- no more reinforcements down road
Rule_Remove(BeachForcesEng) -- no more free engineers

end


---------------- Begin Digin, defend Beachhead (P)

function startDiginmission()

ObjectiveInit_Digin()
Objective_Start(OBJECTIVE_Digin, true)

end


function ObjectiveInit_Digin()

local Digin_TITLE = Util_CreateLocStringfromID(14000064)
local Digin_STARTMSG = Util_CreateLocStringfromID(14000065)
local Digin_HINT = Util_CreateLocStringfromID(14000066) 


        OBJECTIVE_Digin =
        {
                SetupUI = function()
                OBJECTIVE_Digin.PingID = UI_CreateMinimapBlip(eg_beachhead, -1, BT_ObjectivePrimary)
                OBJECTIVE_Digin.HintID = HintPoint_Add(eg_beachhead, true, Digin_HINT)  
                end,

                OnStart = function()     
			Objective_StartTimer(OBJECTIVE_Digin, COUNT_DOWN, DiginTimer * 60)
                        Rule_AddInterval(ObjectiveCheck_Digin, 10)                                       
                        --Util_PlayMusic("SOUND/Music/omg/motivation/the_blitz_v2", 6, 0)
                end,

                OnComplete = function()
                end,

                IsComplete = function()
                        return false
                end,

                OnFail = function()
                end,

                Title = Digin_TITLE,
                Description = Digin_TITLE,
                DisplayTitleStart = Digin_STARTMSG,
                Icon = IT_P_Capture,
                Type = OT_Primary,
        }

        Objective_Register(OBJECTIVE_Digin)                    

end


function ObjectiveCheck_Digin()
        -- Check if done

	if Objective_GetTimerSeconds(OBJECTIVE_Digin) < 1 then 

        Objective_Complete(OBJECTIVE_Digin)
        UI_DeleteMinimapBlip(OBJECTIVE_Digin.PingID)
        HintPoint_Remove(OBJECTIVE_Digin.HintID)

	CounterWait()	

	Rule_RemoveMe()
	end
end




function CounterWait()

Rule_AddOneShot(resumeattacks, 5)
Rule_AddOneShot(startCountermission, 10)

chinesetotalno = chineseinfno + chineseapcno + chinesetankno + chinesesupportno
Cmd_InstantUpgrade(player1, UPG.COMMANDER_TREE.CW.CLOSE_AREA_COMBAT_12, ITEM_UNLOCKED) --Tomahawk

end

---------------- Begin Counter, defend Beachhead (P)

function startCountermission()

ObjectiveInit_Counter()
Objective_Start(OBJECTIVE_Counter, true)

end


function ObjectiveInit_Counter()

local Counter_TITLE = Util_CreateLocStringfromID(14000067)
local Counter_STARTMSG = Util_CreateLocStringfromID(14000068)
local Counter_HINT = Util_CreateLocStringfromID(14000069) 


        OBJECTIVE_Counter =
        {
                SetupUI = function()
                OBJECTIVE_Counter.PingID = UI_CreateMinimapBlip(eg_beachhead, -1, BT_ObjectivePrimary)
                OBJECTIVE_Counter.HintID = HintPoint_Add(eg_beachhead, true, Counter_HINT)  
                end,

                OnStart = function()     
                        Rule_AddInterval(ObjectiveCheck_Counter, 2)
			Rule_AddInterval(AttackersCheck, 60)   
			
			Cmd_Ability(player1, ABILITY.SP.PA_PARADROP, Util_GetPosition(Marker_FromName("airdrop1", "basic_marker")), nil, true)
			Cmd_Ability(player1, ABILITY.SP.PA_PARADROP, Util_GetPosition(Marker_FromName("airdrop2", "basic_marker")), nil, true)

			if Rule_Exists(ReinforcementCheck) then
				Rule_Remove(ReinforcementCheck)
			end      

                        --Util_PlayMusic("SOUND/Music/omg/motivation/the_blitz_v2", 6, 0)
                end,

                OnComplete = function()
                end,

                IsComplete = function()
                        return false
                end,

                OnFail = function()
                end,

                Title = Counter_TITLE,
                Description = Counter_TITLE,
                DisplayTitleStart = Counter_STARTMSG,
                Icon = IT_P_Capture,
                Type = OT_Primary,
        }

        Objective_Register(OBJECTIVE_Counter)                    

end


function ObjectiveCheck_Counter()
        -- Check if done

	if chinesetotalno < 1 then 

        Objective_Complete(OBJECTIVE_Counter)
        UI_DeleteMinimapBlip(OBJECTIVE_Counter.PingID)
        HintPoint_Remove(OBJECTIVE_Counter.HintID)

	WinMovie() 

	Rule_RemoveMe()

	elseif EGroup_IsCapturedByPlayer(eg_beachhead, player2, ANY) then

        Objective_Fail(OBJECTIVE_Counter)
        UI_DeleteMinimapBlip(OBJECTIVE_Counter.PingID)
        HintPoint_Remove(OBJECTIVE_Counter.HintID)

	chinesewin3()

        Rule_RemoveMe()
                                                                   
	end
end


function AttackersCheck()

chinesetotalno = chineseinfno + chineseapcno + chinesetankno + chinesesupportno

	attackersmsg = Util_CreateLocString((Loc_ToAnsi(14000070))..(chinesetotalno))
	Util_GlobalMessage(attackersmsg, 7)
end



----------------------------------------- MEDAL

function startSniperMedalmission()

ObjectiveInit_SniperMedal()
Objective_Start(OBJECTIVE_SniperMedal, true)
SNIPERSLEFT = 0

end


function ObjectiveInit_SniperMedal()

local SniperMedal_TITLE = Util_CreateLocStringfromID(14000071)
local SniperMedal_STARTMSG = Util_CreateLocStringfromID(14000072)
local SniperMedal_HINT = Util_CreateLocStringfromID(14000073) 


        OBJECTIVE_SniperMedal =
        {
                SetupUI = function()
                OBJECTIVE_SniperMedal.PingID = UI_CreateMinimapBlip(eg_towertop1, -1, BT_ObjectiveMedal)
                OBJECTIVE_SniperMedal.PingID1 = UI_CreateMinimapBlip(eg_towertop3, -1, BT_ObjectiveMedal) 
                OBJECTIVE_SniperMedal.PingID2 = UI_CreateMinimapBlip(eg_towertop4, -1, BT_ObjectiveMedal) 
                OBJECTIVE_SniperMedal.PingID3 = UI_CreateMinimapBlip(eg_towertop5, -1, BT_ObjectiveMedal) 
                OBJECTIVE_SniperMedal.HintID = HintPoint_Add(eg_towertop1, true, SniperMedal_HINT)  
		OBJECTIVE_SniperMedal.HintID1 = HintPoint_Add(eg_towertop3, true, SniperMedal_HINT) 
		OBJECTIVE_SniperMedal.HintID2 = HintPoint_Add(eg_towertop4, true, SniperMedal_HINT)  
		OBJECTIVE_SniperMedal.HintID3 = HintPoint_Add(eg_towertop5, true, SniperMedal_HINT)  
                end,

                OnStart = function()      			
                        Rule_AddInterval(ObjectiveCheck_SniperMedal, 5) 
			Rule_AddInterval(Sniper1Down, 5) 
			Rule_AddInterval(Sniper2Down, 5) 
			Rule_AddInterval(Sniper3Down, 5) 
			Rule_AddInterval(Sniper4Down, 5) 
                end,

                OnComplete = function()
                end,

                IsComplete = function()
                        return false
                end,

                OnFail = function()
                end,

                Title = SniperMedal_TITLE,
                Description = SniperMedal_TITLE,
                DisplayTitleStart = SniperMedal_STARTMSG,
                Icon = IT_P_Capture,
		Type = OT_Secondary,
                --Type = OT_Medal, 
		--MedalID = MEDALS.ARMY_SHARPSHOOTER_BADGE
        }

        Objective_Register(OBJECTIVE_SniperMedal)                    

end


function ObjectiveCheck_SniperMedal()
        -- Check if done

	if ALARMON == 0 then 

        Objective_Fail(OBJECTIVE_SniperMedal)
        UI_DeleteMinimapBlip(OBJECTIVE_SniperMedal.PingID)
        HintPoint_Remove(OBJECTIVE_SniperMedal.HintID)
        UI_DeleteMinimapBlip(OBJECTIVE_SniperMedal.PingID1)
        HintPoint_Remove(OBJECTIVE_SniperMedal.HintID1)
        UI_DeleteMinimapBlip(OBJECTIVE_SniperMedal.PingID2)
        HintPoint_Remove(OBJECTIVE_SniperMedal.HintID2)
        UI_DeleteMinimapBlip(OBJECTIVE_SniperMedal.PingID3)
        HintPoint_Remove(OBJECTIVE_SniperMedal.HintID3)

	Rule_RemoveMe()

	elseif SNIPERSLEFT == 4 then

        Objective_Complete(OBJECTIVE_SniperMedal)

        Rule_RemoveMe()
                                                                   
	end
end


function Sniper1Down()

	if SGroup_GetAvgHealth(sg_bb7) == 0 then

	SNIPERSLEFT = SNIPERSLEFT + 1
	UI_DeleteMinimapBlip(OBJECTIVE_SniperMedal.PingID)
        HintPoint_Remove(OBJECTIVE_SniperMedal.HintID)
	SniperMessage()
        Rule_RemoveMe()

	end
end

function killsentriestest() --TESTING ONLY!!

SGroup_Kill(sg_rockettruck1) 
SGroup_Kill(sg_sg_rockettruck2) 
SGroup_Kill(sg_rockettruck3) 
--SGroup_Kill(sg_bbh) 

end

function Sniper2Down()

	if SGroup_GetAvgHealth(sg_bbe) == 0 then

        UI_DeleteMinimapBlip(OBJECTIVE_SniperMedal.PingID1)
        HintPoint_Remove(OBJECTIVE_SniperMedal.HintID1)
	SNIPERSLEFT = SNIPERSLEFT + 1
	SniperMessage()
        Rule_RemoveMe()

	end
end


function Sniper3Down()

	if SGroup_GetAvgHealth(sg_bbg) == 0 then

        UI_DeleteMinimapBlip(OBJECTIVE_SniperMedal.PingID2)
        HintPoint_Remove(OBJECTIVE_SniperMedal.HintID2)
	SNIPERSLEFT = SNIPERSLEFT + 1
	SniperMessage()
        Rule_RemoveMe()

	end
end


function Sniper4Down()

	if SGroup_GetAvgHealth(sg_bbh) == 0 then

        UI_DeleteMinimapBlip(OBJECTIVE_SniperMedal.PingID3)
        HintPoint_Remove(OBJECTIVE_SniperMedal.HintID3)
	SNIPERSLEFT = SNIPERSLEFT + 1
	SniperMessage()
        Rule_RemoveMe()

	end
end



function SniperMessage()
	snipersmsg = Util_CreateLocString((Loc_ToAnsi(14000074))..(SNIPERSLEFT)..("/4"))
	Util_GlobalMessage(snipersmsg, 3)
end


----------------


function startMLRSMedalmission()

ObjectiveInit_MLRSMedal()
Objective_Start(OBJECTIVE_MLRSMedal, true)

end


function ObjectiveInit_MLRSMedal()

local MLRSMedal_TITLE = Util_CreateLocStringfromID(14000075)
local MLRSMedal_STARTMSG = Util_CreateLocStringfromID(14000076)
local MLRSMedal_HINT = Util_CreateLocStringfromID(14000077) 


        OBJECTIVE_MLRSMedal =
        {
                SetupUI = function()
                OBJECTIVE_MLRSMedal.PingID = UI_CreateMinimapBlip(sg_rockettruck1, -1, BT_ObjectiveMedal)
                OBJECTIVE_MLRSMedal.PingID1 = UI_CreateMinimapBlip(sg_sg_rockettruck2, -1, BT_ObjectiveMedal) 
                OBJECTIVE_MLRSMedal.PingID2 = UI_CreateMinimapBlip(sg_rockettruck3, -1, BT_ObjectiveMedal) 

                OBJECTIVE_MLRSMedal.HintID = HintPoint_Add(sg_rockettruck1, true, MLRSMedal_HINT)  
		OBJECTIVE_MLRSMedal.HintID1 = HintPoint_Add(sg_sg_rockettruck2, true, MLRSMedal_HINT) 
		OBJECTIVE_MLRSMedal.HintID2 = HintPoint_Add(sg_rockettruck3, true, MLRSMedal_HINT)   
                end,

                OnStart = function()      			
                        Rule_AddInterval(ObjectiveCheck_MLRSMedal, 5) 
                end,

                OnComplete = function()
                end,

                IsComplete = function()
                        return false
                end,

                OnFail = function()
                end,

                Title = MLRSMedal_TITLE,
                Description = MLRSMedal_TITLE,
                DisplayTitleStart = MLRSMedal_STARTMSG,
                Icon = IT_P_Capture,
		Type = OT_Secondary,
                --Type = OT_Medal, 
		--MedalID = MEDALS.EXPERT_INFANTRYMAN_BADGE
        }

        Objective_Register(OBJECTIVE_MLRSMedal)                    

end


function ObjectiveCheck_MLRSMedal()
        -- Check if done

	if SGroup_GetAvgHealth(sg_rockettruck1) == 0 and SGroup_GetAvgHealth(sg_sg_rockettruck2) == 0 and 
		SGroup_GetAvgHealth(sg_rockettruck3) == 0 then

        Objective_Complete(OBJECTIVE_MLRSMedal)
        UI_DeleteMinimapBlip(OBJECTIVE_MLRSMedal.PingID)
        HintPoint_Remove(OBJECTIVE_MLRSMedal.HintID)
        UI_DeleteMinimapBlip(OBJECTIVE_MLRSMedal.PingID1)
        HintPoint_Remove(OBJECTIVE_MLRSMedal.HintID1)
        UI_DeleteMinimapBlip(OBJECTIVE_MLRSMedal.PingID2)
        HintPoint_Remove(OBJECTIVE_MLRSMedal.HintID2)
        UI_DeleteMinimapBlip(OBJECTIVE_MLRSMedal.PingID3)
        HintPoint_Remove(OBJECTIVE_MLRSMedal.HintID3)

        Rule_RemoveMe()
                                                                   
	end
end




----------------------------------------- INTRO

----Intro Video Test

function StartIntro()


--s_aa1
--
--Prespawn units to enemy comms centre, AA unit 1, enemy defences around AA/comms
--Camera starts looking out to sea, quite zoomed in, slowly zooms out, 3 sets of messages
--Switch camera to show AA unit, autorotate around it briefly, destroy AA msg
--Switch camera to show comms centre, destroy msg, autorotate briefly
--Switch camera back to sea, spawn boats and messages
--Follow boats in
--Start briefing as they land

EGroup_CreateIfNotFound("eg_comms") 

Util_CreateEntities(player2, eg_comms, EBP.SP.PA_COMMS, Util_GetPosition(Marker_FromName("commsbuild", "basic_marker")), 1, Util_GetPosition(Marker_FromName("commsbuildf", "green_marker"))) 


Rule_AddOneShot(Intro1, 0.2) -- Maybe add this to Init instead?
Rule_AddOneShot(Intro1a, 6)
Rule_AddOneShot(Intro2, 12) --Camera still while unfade
Rule_AddOneShot(Intro2a, 18) --Camera still while unfade
Rule_AddOneShot(Intro2a1, 22)
Rule_AddOneShot(Intro2b, 25) --Move to AA
Rule_AddOneShot(Intro2b1, 31)
Rule_AddOneShot(Intro2c, 34) -- Move to Comms
Rule_AddOneShot(Intro2c1, 35)

Rule_AddOneShot(Intro2d, 40) 

--Rule_AddOneShot(Intro2e, 38)

Rule_AddOneShot(Intro3, 43) -- Move back to beach --Spawn rafts
Rule_AddOneShot(Intro3a, 44)  --Follow boats
Rule_AddOneShot(Intro4, 49) --Boats have arrived and disembarked
Rule_AddOneShot(Intro5, 52) --Fade from 4 has completed

end

function Intro1()

Resources_Disable()
--Game_FadeToBlack(true, 0)
Game_Letterbox(true, 5) 
Game_FadeToBlack(false, 6)

Camera_MoveToPosition(cameraend, true) --Setup ready to begin pan up beach
Util_PlayMusic("SOUND/Music/omg/motivation/the_blitz_v2", 6, 0)

Camera_SetOrbit(2.6)
Camera_SetZoomDist(5) --45.99
Camera_SetDeclination(0.21) 

FOW_RevealAll()

end


function Intro1a()

--Camera_AutoRotate(Util_GetPosition(Marker_FromName("camerastart", "orange_marker")), 115, 15, 2.0)

intromsga = Util_CreateLocStringfromID(14000078)
Util_GlobalMessage(intromsga, 3)

ZoomOutRate = 0.1
ZoomInRate = 1
DecUpRate = 0.001
DecDownRate = 0.001

MaxZoom = 10
MinZoom = 80
MaxDec = 0.21
MinDec = 0.12

Rule_AddInterval(ZoomOutCamera, 0.1)
Rule_AddInterval(DecUpCamera, 0.001)

end


function Intro2()

--Camera_MoveToPosition(cameraend, true, SLOW_CAMERA_PANNING) --Pan up beach to start position

intromsgb = Util_CreateLocStringfromID(14000079)
Util_GlobalMessage(intromsgb, 4)

end


function Intro2a()

intromsg1 = Util_CreateLocStringfromID(14000080)
Util_GlobalMessage(intromsg1, 4)
Rule_AddOneShot(StartAA1, 1.5)

end


function Intro2a1()

Game_FadeToBlack(true, 3)

end


function Intro2b()

Rule_Remove(ZoomOutCamera)
Rule_Remove(DecUpCamera)

Camera_SetOrbit(1.5)
Camera_SetZoomDist(80) --45.99
Camera_SetDeclination(0.21) 

Camera_MoveToPosition(s_aa, true)

Game_FadeToBlack(false, 3)

Camera_AutoRotate(Util_GetPosition(Marker_FromName("s_aa", "basic_marker")), 125, 35, 2.0)

intromsg1a = Util_CreateLocStringfromID(14000081)
Util_GlobalMessage(intromsg1a, 4)

end


function Intro2b1()

Game_FadeToBlack(true, 3)

end


function Intro2c()

Camera_StopAutoRotating()

Game_FadeToBlack(false, 3)

Camera_MoveToPosition(ambush1, true)

Camera_AutoRotate(Util_GetPosition(Marker_FromName("ambush1", "blue_marker")), 115, 45, 2.0)

--Camera_FocusOnTargetPos(Util_GetPosition(Marker_FromName("s_sf3", "basic_marker")))

end


function Intro2c1()

intromsg2 = Util_CreateLocStringfromID(14000082)
Util_GlobalMessage(intromsg2, 4)

end


function Intro2d()

Game_FadeToBlack(true, 3)

end


function Intro2d2()



end


function Intro2e()

--Camera_SetOrbit(2.6)
--Rule_AddInterval(ZoomOutCamera, 0.1)
--Rule_AddInterval(DecUpCamera, 0.001)

end


function Intro3()

Game_FadeToBlack(false, 3)

Camera_StopAutoRotating()

Camera_SetOrbit(4.2)
Camera_SetZoomDist(50) --45.99
Camera_SetDeclination(0.23) 

Camera_MoveToPosition(camerastart, true)

sg_Raft1 = SGroup_CreateIfNotFound("sg_Raft1")
eg_Raft1 = EGroup_CreateIfNotFound("eg_Raft1")
sg_Raft2 = SGroup_CreateIfNotFound("sg_Raft2")
eg_Raft2 = EGroup_CreateIfNotFound("eg_Raft2")

	if COOP == 0 then
		Util_SpawnRaft(player1, eg_Raft1, sg_Raft1, SBP.CW.MARINES_FORCE_RECON_PACIFIC_ASSAULT, boat1, boat2, 1, 6)
		Util_SpawnRaft(player1, eg_Raft2, sg_Raft2, SBP.CW.MARINES_FORCE_RECON_PACIFIC_ASSAULT, boat3, boat4, 1, 6) --MARINES_FORCE_RECON_PACIFIC_ASSAULT
	elseif COOP == 1 then

	end

Modify_WeaponRange(sg_Raft1, "hardpoint_01", MRFrange)
Modify_WeaponDamage(sg_Raft1, "hardpoint_01", MRFdamage)
Modify_WeaponAccuracy(sg_Raft1, "hardpoint_01", MRFaccuracy)
Modify_Vulnerability(sg_Raft1, MRFvuln)

Modify_WeaponRange(sg_Raft2, "hardpoint_01", MRFrange)
Modify_WeaponDamage(sg_Raft2, "hardpoint_01", MRFdamage)
Modify_WeaponAccuracy(sg_Raft2, "hardpoint_01", MRFaccuracy)
Modify_Vulnerability(sg_Raft2, MRFvuln)

ZoomOutRate = 0.1
ZoomInRate = 0.5
DecUpRate = 0.001
DecDownRate = 0.0001

MaxZoom = 5
MinZoom = 80
MaxDec = 0.21
MinDec = 0.14

Rule_AddInterval(ZoomInCamera, 0.1)
Rule_AddInterval(DecDownCamera, 0.0005)

end


function Intro3a()

intromsg2b = Util_CreateLocStringfromID(14000083)
Util_GlobalMessage(intromsg2b, 4)

end


function Intro4()

Game_FadeToBlack(true, 3) 

end


function Intro5()

intromsg2d2 = Util_CreateLocStringfromID(14000084)
Util_GlobalMessage(intromsg2d2, 3)

Rule_Remove(ZoomInCamera)
Rule_Remove(DecDownCamera)

Camera_FollowEGroup(eg_Raft2)
Game_FadeToBlack(false, 3)
Game_Letterbox(false, 3) 

FOW_UnRevealAll()   ----------------------*************** TURN ON OUTSIDE TESTING
Camera_ResetToDefault() 
Resources_Enable()

Rule_AddOneShot(startpatrols, 2)
Rule_AddOneShot(refreshpatrols, 10)

end


-------------------------------------JDAM


function JDAMMovieManager() -- 5 secs after JDAM designate

Rule_AddOneShot(JDAMMovie, 0.01) -- Letterbox
Rule_AddOneShot(JDAMMovie1, 3)
Rule_AddOneShot(JDAMMovie2, 16)

Camera_SetDeclination(0.12) 

DecUpRate = 0.001
DecDownRate = 0.001
MaxDec = 0.30
MinDec = 0.01

end


function JDAMMovie()

Game_Letterbox(true, 3) 
Camera_MoveToPosition(jdamcam, true)

Cmd_Ability(player1, ABILITY.SP.PA_F16_JDAM, Util_GetPosition(Marker_FromName("commsbuild", "basic_marker")), nil, true)


end


function JDAMMovie1()

Rule_AddInterval(DecDownCamera, 0.0001)
jdammsg = Util_CreateLocStringfromID(14000085)
Util_GlobalMessage(jdammsg, 5)

end


function JDAMMovie2()

EGroup_Kill(eg_comms)
Rule_Remove(DecDownCamera)
Camera_ResetToDefault()
Game_Letterbox(false, 3) 

end


-------------------------------------ASSAULT MOVIE

function AssaultMovieManager()

Rule_AddOneShot(AssaultMovie1, 3) --spawn
Rule_AddOneShot(AssaultMovie2, 6) --Unfade
Rule_AddOneShot(AssaultMovie3, 9) --Message
Rule_AddOneShot(AssaultMovie4, 11) --Arty
Rule_AddOneShot(AssaultMovie5, 20) --fade
Rule_AddOneShot(AssaultMovie6, 23) --unfade
Rule_AddOneShot(AssaultMovie7, 27) --Airbourne
Rule_AddOneShot(AssaultMovie8, 30) --Msg
Rule_AddOneShot(AssaultMovie9, 38) --Fade
Rule_AddOneShot(AssaultMovie9a, 37.6) --Deploy AAVs
Rule_AddOneShot(AssaultMovie10, 41) --Return to game
Rule_AddOneShot(AssaultMovie11, 43) --Message
Rule_AddOneShot(AssaultMovie12, 45) --Return to game

Game_FadeToBlack(true, 3)
Game_Letterbox(true, 3)

Camera_SetZoomDist(70) --45.99

end



--ASSAULT MOVIE

function AssaultMovie1()

Camera_MoveToPosition(m_ass3, true)

--Cmd_Ability(player1, ABILITY.CW.SATELLITE_OBSERVATION, Util_GetPosition(Marker_FromName("m_ass3", "cyan_marker")), nil, true)

end


function AssaultMovie2()

Game_FadeToBlack(false, 3)
Cmd_Ability(player1, ABILITY.COMMANDER_TREE.CW.SATELLITE_OBSERVATION, Util_GetPosition(Marker_FromName("m_ass3", "cyan_marker")), nil, true)
Cmd_Ability(player1, ABILITY.COMMANDER_TREE.CW.SATELLITE_OBSERVATION, Util_GetPosition(Marker_FromName("arty1", "red_marker")), nil, true)

end


function AssaultMovie3()

assintromsg = Util_CreateLocStringfromID(14000086)
Util_GlobalMessage(assintromsg, 5)

end


function AssaultMovie4()

Cmd_Ability(player1, ABILITY.SP.PA_NAVAL_BARRAGE, Util_GetPosition(Marker_FromName("arty1", "red_marker")), nil, true)
Cmd_Ability(player1, ABILITY.SP.PA_NAVAL_BARRAGE, Util_GetPosition(Marker_FromName("arty2", "red_marker")), nil, true)

Cmd_Ability(player1, ABILITY.SP.PA_SMOKE_BARRAGE, Util_GetPosition(Marker_FromName("m_ass3", "cyan_marker")), nil, true)
Cmd_Ability(player1, ABILITY.SP.PA_SMOKE_BARRAGE, Util_GetPosition(Marker_FromName("m_ass2", "cyan_marker")), nil, true)
Cmd_Ability(player1, ABILITY.SP.PA_SMOKE_BARRAGE, Util_GetPosition(Marker_FromName("m_ass4", "cyan_marker")), nil, true)

local artyattacktype1 = ABILITY.SP.SINGLE_HOWITZER
local artyattacktype2 = ABILITY.SP.SINGLE_HOWITZER_ALTSOUND

local bridge_targeta = Util_GetPosition(Marker_FromName("arty" .. World_GetRand(1, 2), "red_marker"))
local bridge_targetb = Util_GetPosition(Marker_FromName("arty" .. World_GetRand(1, 2), "red_marker"))
local bridge_targetc = Util_GetPosition(Marker_FromName("arty" .. World_GetRand(1, 2), "red_marker"))
local bridge_targetd = Util_GetPosition(Marker_FromName("arty" .. World_GetRand(1, 2), "red_marker"))

Cmd_Ability(player1, artyattacktype1, Util_GetRandomOffset(Util_GetPosition(bridge_targeta), -15, 15), nil, true)
Cmd_Ability(player1, artyattacktype1, Util_GetRandomOffset(Util_GetPosition(bridge_targetb), -15, 15), nil, true)
Cmd_Ability(player1, artyattacktype2, Util_GetRandomOffset(Util_GetPosition(bridge_targetc), -15, 15), nil, true)
Cmd_Ability(player1, artyattacktype2, Util_GetRandomOffset(Util_GetPosition(bridge_targetd), -15, 15), nil, true)


end


function AssaultMovie5()

Game_FadeToBlack(true, 3)

end


function AssaultMovie6()

Camera_MoveToPosition(s_aidef9, true)
Game_FadeToBlack(false, 3)

end


function AssaultMovie7()

Cmd_Ability(player1, ABILITY.SP.PA_PARADROP, Util_GetPosition(Marker_FromName("s_aidef1", "basic_marker")), nil, true)
Cmd_Ability(player1, ABILITY.SP.PA_PARADROP, Util_GetPosition(Marker_FromName("s_aidef9", "basic_marker")), nil, true)
Cmd_Ability(player1, ABILITY.SP.PA_LAV25, Util_GetPosition(Marker_FromName("s_aidef10", "basic_marker")), nil, true)

end


function AssaultMovie8()

assintromsg = Util_CreateLocStringfromID(14000087)
Util_GlobalMessage(assintromsg, 5)

end


function AssaultMovie9()

Game_FadeToBlack(true, 3)

end

function AssaultMovie9a()

BeachAssaultBoats()

end


function AssaultMovie10()

Camera_MoveToPosition(m_ass3, true)
Game_FadeToBlack(false, 3)

end



function AssaultMovie11()

assintromsg = Util_CreateLocStringfromID(14000088)
Util_GlobalMessage(assintromsg, 5)

end



function AssaultMovie12()

Game_Letterbox(false, 3) 
--FOW_UnRevealAll() 
--Camera_ResetToDefault() 

LandingWaitResume()

end


-------------------------------------Lose Spec Ops Mission Movie



function LoseSpecOpsMovieManager()

Rule_AddOneShot(LoseSpecOpsMovie, 3) -- Fade
Rule_AddOneShot(LoseSpecOpsMovie1, 6) -- Remove all old squads
Rule_AddOneShot(LoseSpecOpsMovie2, 6.5) --Reveal
Rule_AddOneShot(LoseSpecOpsMovie2a, 9.9) --Sniper
Rule_AddOneShot(LoseSpecOpsMovie3, 14) --Surrender
Rule_AddOneShot(LoseSpecOpsMovie3a, 15) --Follow
Rule_AddOneShot(LoseSpecOpsMovie4, 18) --escort
Rule_AddOneShot(LoseSpecOpsMovie5, 23) --end game

end


function LoseSpecOpsMovie()

Game_FadeToBlack(true, 3)
Game_Letterbox(true, 3) 

end


function LoseSpecOpsMovie1()

	for i = 1, 2 do

		plr = World_GetPlayerAt(i)
		Player_GetAll(plr) 	
		SGroup_DestroyAllSquads(sg_allsquads)
	end

sg_loseso = SGroup_CreateIfNotFound("sg_loseso")
Util_CreateSquads(player1, sg_loseso, SBP.CW.MARINES_FORCE_RECON_PACIFIC_ASSAULT, s_aidef4, nil, 1)
Util_CreateSquads(player1, sg_loseso, SBP.CW.MARINES_FORCE_RECON_PACIFIC_ASSAULT, s_aip2mg, nil, 1)
SGroup_SetAvgHealth(sg_loseso, 0.165)

sg_loseso1 = SGroup_CreateIfNotFound("sg_loseso1")
sg_loseso2 = SGroup_CreateIfNotFound("sg_loseso2")
sg_loseso3 = SGroup_CreateIfNotFound("sg_loseso3")
sg_loseso4 = SGroup_CreateIfNotFound("sg_loseso4")
sg_loseso5 = SGroup_CreateIfNotFound("sg_loseso5")
sg_loseso6 = SGroup_CreateIfNotFound("sg_loseso6")

Util_CreateSquads(player2, sg_loseso1, SBP.ELITE.MILITIA, sent_32, nil, 1)
Util_CreateSquads(player2, sg_loseso2, SBP.ELITE.REGULAR, endingspecops1, nil, 1)
Util_CreateSquads(player2, sg_loseso3, SBP.ELITE.ZBD2K, s_aidef9, nil, 1)
--Util_CreateSquads(player2, sg_loseso4, SBP.ELITE.MILITIA, s_rein1, nil, 1)
Util_CreateSquads(player2, sg_loseso5, SBP.ELITE.MILITIA, endingspecops2, nil, 1)
Util_CreateSquads(player2, sg_loseso6, SBP.ELITE.SNIPER, endingspecops3, nil, 1)

--Cmd_Ability(sg_loseso6, BP_GetAbilityBlueprint("abilities/camouflage_toggled.lua"), nil, nil, true)

Modify_Vulnerability(sg_loseso1, 0.25) 
Modify_Vulnerability(sg_loseso2, 0.25) 

Modify_ReceivedSuppression(sg_loseso1, 0.5) 
Modify_ReceivedSuppression(sg_loseso2, 0.5) 
Modify_UnitSpeed(sg_loseso3, 1.4)

--Camera_MoveToPosition(endingspecops3, true)

Camera_MoveToPosition(cameraspecops, true)

Camera_SetOrbit(-0.3)
Camera_SetZoomDist(29.5)
Camera_SetDeclination(0.045) 

end


function LoseSpecOpsMovie2()

Cmd_AttackMove(sg_loseso1, Util_GetPosition(Marker_FromName("s_aip2tow", "basic_marker")), false)
Cmd_AttackMove(sg_loseso2, Util_GetPosition(Marker_FromName("infmove25", "cyan_marker")), false)
Cmd_AttackMove(sg_loseso3, Util_GetPosition(Marker_FromName("s_aidef4", "basic_marker")), false)
--Cmd_AttackMove(sg_loseso4, Util_GetPosition(Marker_FromName("ambush1", "blue_marker")), false)
Cmd_AttackMove(sg_loseso5, Util_GetPosition(Marker_FromName("sent_31", "basic_marker")), false)

FOW_RevealAll()
Game_FadeToBlack(false, 3)

end



function LoseSpecOpsMovie2a()

ZoomOutRate = 0.25
DecUpRate = 0.002

MaxZoom = 5
MinZoom = 95
MaxDec = 0.7
MinDec = 0.7

Camera_SetZoomDist(5.5)

Rule_AddInterval(ZoomOutCamera, 0.001)

Camera_MoveToPosition(s_aidef4, false)
Rule_AddInterval(DecUpCamera, 0.00001)

end


function LoseSpecOpsMovie3()

--Camera_SetZoomDist(29.99)

--Camera_FollowSGroup(sg_beachapc)

--Camera_SetOrbit(-0.05)
--Camera_SetZoomDist(20.99)
--Camera_SetDeclination(0.02) 

Ceasefire_AddSGroup(sg_loseso1) 
Ceasefire_AddSGroup(sg_loseso2) 
Ceasefire_AddSGroup(sg_loseso3) 
Ceasefire_AddSGroup(sg_loseso4) 
Cmd_Surrender(sg_loseso, 0, Util_GetPosition(Marker_FromName("sent_34", "basic_marker")))

end


function LoseSpecOpsMovie3a()

Camera_FollowSGroup(sg_loseso)

LoseSpecOpsmsg = Util_CreateLocStringfromID(14000089)
Util_GlobalMessage(LoseSpecOpsmsg, 5)

end


function LoseSpecOpsMovie4()

SGroup_SetMoodMode(sg_loseso1, MM_ForceCalm) 
SGroup_SetMoodMode(sg_loseso2, MM_ForceCalm) 
SGroup_SetMoodMode(sg_loseso4, MM_ForceCalm)

Cmd_AttackMove(sg_loseso1, Util_GetPosition(Marker_FromName("s_aip2tow", "basic_marker")), false)
Cmd_AttackMove(sg_loseso2, Util_GetPosition(Marker_FromName("s_aip2mg", "basic_marker")), false)
Cmd_AttackMove(sg_loseso3, Util_GetPosition(Marker_FromName("s_aidef9", "basic_marker")), false)
--Cmd_AttackMove(sg_loseso4, Util_GetPosition(Marker_FromName("s_aidef9", "basic_marker")), false)

Modify_UnitSpeed(sg_loseso3, 0.25)

end



function LoseSpecOpsMovie5()

Game_Letterbox(false, 3) 
Rule_AddOneShot(chinesewinend, 3.2)

end

-------------------------------------Lose Assault/Defend Missions Movie


function LoseAssaultMovieManager()

Rule_AddOneShot(AssaultMovie, 0.01) -- Fade
Rule_AddOneShot(AssaultMovie1, 3) -- Remove all old squads
Rule_AddOneShot(AssaultMovie2, 6) --Msg and smoke
Rule_AddOneShot(AssaultMovie3, 7) --Boats
Rule_AddOneShot(AssaultMovie4, 13) --Return to game

end


function LoseAssaultMovie1()

	--for i = 1, 4 do

	--	plr = World_GetPlayerAt(i)
	--	Player_GetAll(plr) 	
	--	SGroup_DestroyAllSquads(sg_allsquads)
	--end

FOW_RevealAll()
Camera_MoveToPosition(m_ass4, true)
Game_FadeToBlack(false, 3)

LoseAssaultmsg = Util_CreateLocStringfromID(14000090)
Util_GlobalMessage(LoseAssaultmsg, 5)

end


-------------------------------------Win Mission Movie


function WinMovie()

Rule_AddOneShot(WinMovie1, 3) -- Unfade
Rule_AddOneShot(uswin, 13) -- Remove all old squads

Game_FadeToBlack(true, 3)
Game_Letterbox(true, 3)

end


function WinMovie1()

Camera_MoveToPosition(infmove62, false)

ZoomOutRate = 0.25
DecUpRate = 0.002

MaxZoom = 5
MinZoom = 95
MaxDec = 0.7
MinDec = 0.7

Camera_SetZoomDist(15.5)

Rule_AddInterval(ZoomOutCamera, 0.001)
Rule_AddInterval(DecUpCamera, 0.00001)

Player_GetAll(player1) 
SGroup_SetMoodMode(sg_allsquads, MM_ForceCalm)

Game_FadeToBlack(false, 3)

end




-------------------------------------Camera Functions

function ZoomInCamera()

local ZoomDist = Camera_GetZoomDist() 

	if ZoomDist > MaxZoom then

	local NewZoom = ZoomDist - ZoomInRate
	Camera_SetZoomDist(NewZoom)

	elseif ZoomDist <= MaxZoom then
	Rule_RemoveMe()
	end
end


function ZoomOutCamera()

local ZoomDist = Camera_GetZoomDist() 

	if ZoomDist < MinZoom then

	local NewZoom = ZoomDist + ZoomOutRate
	Camera_SetZoomDist(NewZoom)

	elseif ZoomDist >= MinZoom then
	Rule_RemoveMe()
	end
end



function DecDownCamera()

local Dec = Camera_GetDeclination() 

	if Dec > MaxDec then

	local NewDec = Dec - DecDownRate
	Camera_SetDeclination(NewDec)

	elseif Dec <= MaxDec then
	Rule_RemoveMe()
	end
end


function DecUpCamera()

local Dec = Camera_GetDeclination() 

	if Dec < MinDec then

	local NewDec = Dec + DecUpRate
	Camera_SetDeclination(NewDec)

	elseif Dec >= MinDec then
	Rule_RemoveMe()
	end
end


----------------------------------------- ENDINGS


function chinesewin1() --Fail to take beachhead within time limit

-- Possible NIS/outro stuff

allywin1msg = Util_CreateLocStringfromID(14000091)
Util_GlobalMessage(allywin1msg, 5)

Rule_AddOneShot(chinesewinend, 10)

end


function chinesewin2() --All squads dead

-- Possible NIS/outro stuff

allywin2msg = Util_CreateLocStringfromID(14000092)
Util_GlobalMessage(allywin2msg, 5)

Rule_AddOneShot(chinesewinend, 10)

end


function chinesewin3() --Chinese recapture point

-- Possible NIS/outro stuff

allywin3msg = Util_CreateLocStringfromID(14000093)
Util_GlobalMessage(allywin3msg, 5)

Rule_AddOneShot(chinesewinend, 10)

end


function chinesewin4() --Chinese recapture point

-- Possible NIS/outro stuff

allywin4msg = Util_CreateLocStringfromID(14000094)
Util_GlobalMessage(allywin4msg, 5)

Rule_AddOneShot(chinesewinend, 10)

end




function uswin() --Took and held beachhead

-- Possible NIS/outro stuff

allywin1msg = Util_CreateLocStringfromID(14000095)
Util_GlobalMessage(allywin1msg, 5)

Rule_AddOneShot(uswinend, 10)

end


-------------------------------------------

function chinesewinend()

--World_SetTeamWin(Player_GetTeam(player2), "annihilate") 
--World_SetGameOver()

Game_EndSP(false)

end


function uswinend()

--World_SetTeamWin(Player_GetTeam(player1), "annihilate") 
--World_SetGameOver()

Game_EndSP(true)

end


----------------------------------------- UTILITIES

function Util_SpawnAAVP(player, sgroup, sgroup1, sbp, spawn, land, squad_num, loadout)

Util_CreateSquads(player, sgroup, SBP.CW.AAVP7A1, spawn)
Util_CreateSquads(player, sgroup1, sbp, sgroup)
Cmd_Ability(sgroup, ABILITY.SP.MOVE_BOAT, land, nil, nil, true)

end 


-- Get random position near position
function Util_GetRandomOffset(pos, mindist, maxdist)	
	local newpos = pos
	newpos.x = newpos.x + World_GetRand(mindist, maxdist)
	newpos.z = newpos.z + World_GetRand(mindist, maxdist)
	return newpos
end


function XDebug1()
	Util_ReloadScript()
	g_CheckAnnihilate = false
end

function XMaptest_Misc1()
	Util_ReloadScript()
	g_CheckAnnihilate = false
end 

function remove_single_blackhawk()

	for i = 1,World_GetPlayerCount() do
		local player_id = World_GetPlayerAt(i)
		
		local sg_bh = function(SGroupID, itemindex, SquadID)
			if (Squad_GetBlueprint(SquadID) == SBP.CW.BLACKHAWK) then
				if Squad_HasUpgrade(SquadID, BP_GetUpgradeBlueprint("upgrade/amer/blackhawk_exit_upgrade.lua")) then
					Squad_Destroy(SquadID)
				end
			end
		end

		Player_GetAll(player_id)
		SGroup_ForEach(sg_allsquads, sg_bh)
	end
end


function remove_single_apache()

	for i = 1,World_GetPlayerCount() do
		local player_id = World_GetPlayerAt(i)
		
		local sg_bh = function(SGroupID, itemindex, SquadID)
			if (Squad_GetBlueprint(SquadID) == SBP.CW.APACHE) then
				if Squad_HasUpgrade(SquadID, BP_GetUpgradeBlueprint("upgrade/amer/apache_exit_upgrade.lua")) then
					Squad_Destroy(SquadID)
				end
			end
		end

		Player_GetAll(player_id)
		SGroup_ForEach(sg_allsquads, sg_bh)
	end
end

