
-------------------------------------------------------------------------
-------------------------------------------------------------------------
--
-- Script File for TUTORIAL - Commonwealth Section
--
-------------------------------------------------------------------------
-------------------------------------------------------------------------


--------------------------------------------------------------------------------
-- Initialisation and Setup
--------------------------------------------------------------------------------
function CW_Init()
	
	sg_cw_tommies = SGroup_CreateIfNotFound("sg_cw_tommies")
	sg_cw_hq = SGroup_CreateIfNotFound("sg_cw_hq")
	sg_cw_lt = SGroup_CreateIfNotFound("sg_cw_lt")
	sg_cw_commandos = SGroup_CreateIfNotFound("sg_cw_commandos")
	
	sg_cw_axis = SGroup_CreateTable("sg_cw_axis%d", 5)
	sg_cw_axisAll = SGroup_CreateIfNotFound("sg_cw_axisAll")
	sg_cw_axisMortar = SGroup_CreateIfNotFound("sg_cw_axisMortar")
	
	eg_cw_mgNest = EGroup_CreateIfNotFound("eg_cw_mgNest")
	eg_cw_trench = EGroup_CreateIfNotFound("eg_cw_trench")
	
	CW = {
		
		capAttack = {
			{sgroup = sg_cw_axis[1], sbp = SBP.AXIS.VOLKSGRENADIER, src = mkr_brit_axisSpawn1, dest = mkr_brit_move2, num = 3},
			{sgroup = sg_cw_axis[2], sbp = SBP.AXIS.VOLKSGRENADIER, src = mkr_brit_axisSpawn2, dest = mkr_brit_move3, num = 2},
		},
		mgAttack = {
			{sgroup = sg_cw_axis[1], sbp = SBP.AXIS.VOLKSGRENADIER, src = mkr_brit_axisSpawn3, dest = mkr_brit_move4},
			{sgroup = sg_cw_axis[2], sbp = SBP.AXIS.VOLKSGRENADIER, src = mkr_brit_axisSpawn4, dest = mkr_brit_move5},
		},
		
		-- Camera Lock Markers
		camLock1 = mkr_brit_cam1, -- intro
		camLock2 = mkr_brit_cam2, -- move tommies
		camLock3 = mkr_brit_cam3, -- capture point
		camLock4 = mkr_brit_cam4, -- defend point
		camLock5 = mkr_brit_cam5, -- call in glider
		
		deploylockoutzones = {mkr_cw_lockout1, mkr_cw_lockout2, mkr_cw_lockout3, mkr_cw_lockout4, mkr_cw_lockout5, mkr_cw_glider1},
		gliderlockoutzones = {mkr_cw_lockout1, mkr_cw_lockout2, mkr_cw_lockout3, mkr_cw_lockout4, mkr_cw_lockout5, mkr_brit_hqDeploy},
		
	}

	CW_RegisterObjectives() -- see end of file

	hq_productionqueue = 0
	
end



--------------------------------------------------------------------------------
-- Start Tutorial
--------------------------------------------------------------------------------
-- cleans up, updates the tutorial state, and announces the section name
function CW_Start()

	-- clean up from the menu
	Rule_RemoveAll(0)
	HintPoint_RemoveAll()
	Game_SkipAllEvents(true)
	UI_ClearEventCues()
	
	tutorial_commonwealth_available = false
	tutorial_currentsection = "commonwealth"
	TutorialMenu_UpdateAvailability()
	
	Camera_SetInputEnabled(false)
	Game_FadeToBlack(FADE_OUT, TIMING_FADE_OUT)
	
	-- do the title card
	Game_TextTitleFade(409916, TIMING_FADE_IN, 3, TIMING_FADE_OUT, "" )
	
	Rule_AddOneShot(CW_StartB, 3+TIMING_FADE_IN)
	
	-- set up the construction menu callbacks so we can check the status with a variable check
	UI_SetConstructionMenuCallback(_ConstructionMenuCallback)
	
end

-- performs all the necessary presets
function CW_StartB()
	
	-- clean up player units
	Player_GetAll(player1)
	SGroup_DestroyAllSquads(sg_allsquads)
	Player_GetAll(player5)
	SGroup_DestroyAllSquads(sg_allsquads)
	Player_GetAll(player6)
	SGroup_DestroyAllSquads(sg_allsquads)
	
	-- switch to Commonwealth player
	Game_SetLocalPlayer(player5)
	
	Rule_AddOneShot(CW_StartC, 0.5)
	
end

function CW_StartC()

	Rule_AddIntervalEx(Tutorial_RemoveParatroopers, 0.25, 20)

	-- reload taskbar for bindings that have been disabled
	UI_LoadTaskbar("taskbar.lua")
	Rule_AddOneShot(RedoBindings, 0.1)
	
	-- brings the camera back in
	Game_SetMode(UI_Normal)
	Game_FadeToBlack(FADE_IN, TIMING_FADE_IN)
	Game_Letterbox(false, 0)
	
	-- camera clamping
	Camera_ClampToMarker(CW.camLock1)
	Camera_MoveToPosition(mkr_brit_tommy)
		Tutorial_RevealPos("Commonwealth", Marker_GetPosition(CW.camLock1), 40)
	Camera_ResetToDefault()
	
	-- bring up the perimeter
	EGroup_DeSpawn(LAYER_brit_hide)
	EGroup_ReSpawn(LAYER_brit_show)
	
	-- set point ownership
	EGroup_InstantCaptureStrategicPoint(eg_cw_startPoint, player2) -- hack to get it to not be yellow
	EGroup_InstantCaptureStrategicPoint(eg_cw_startPoint, player5)
	EGroup_SetStrategicPointNeutral(eg_cw_munitionsPoint)

	-- create all the relevant units
	Util_CreateSquads(player5, sg_cw_tommies, SBP.CW.TOMMIES, mkr_brit_tommyspawn, mkr_brit_tommy)
	Util_CreateSquads(player5, sg_cw_hq, SBP.CW.HQ, mkr_brit_hq)
	
	SGroup_SetInvulnerable(sg_cw_tommies, 0.6)
	SGroup_SetInvulnerable(sg_cw_hq, 0.6)
	
	-- ensure there are enough resources
	Player_SetResource(player5, RT_Manpower, 1000)
	Player_SetResource(player5, RT_Munition, 1000)
	Player_SetResource(player5, RT_Fuel, 1000)
	Player_SetPopCapOverride(player5, 17)
	
	-- reveal place space
	FOW_RevealMarker(CW.camLock1, -1)
	
	-- give the introductory talk
	Util_StartIntel(CW_EVENTS.Intro)
	
	Rule_AddInterval(CW_StartD, 0.5)
	
end


function CW_StartD()

	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		
		-- focus on the Tommies
		Camera_MoveToSGroup(sg_cw_tommies, true, SLOW_CAMERA_PANNING)
		
		-- give the introductory talk
		Util_StartIntel(CW_EVENTS.Intro2)
		
		-- start lesson 1 when ready
		Rule_AddInterval(CW_MoveTommies_BeginLesson, 0.5)
		
	end
	
end




--------------------------------------------------------------------------------
-- Lesson: Move Tommies
--------------------------------------------------------------------------------
function CW_MoveTommies_BeginLesson()
	
	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		
		-- give the objective
		Objective_Start(OBJ_CW_TOMMIES, false)
		
		-- remove the invisible wall
		EGroup_DeSpawn(LAYER_brit_block1)
		
		-- adjust the camera 
		Camera_ClampToMarker(CW.camLock2)
		Camera_SetInputEnabled(true)
		
		if Misc_IsSGroupSelected(sg_cw_tommies, ANY) then
			
			-- ask the player to move the Tommies
			Util_StartIntel(CW_EVENTS.PleaseMove)
			
			-- hint the destination
			OBJ_CW_TOMMIES.PingID = Objective_AddUIElements(OBJ_CW_TOMMIES, mkr_brit_move1, true, 409715, true)
			
			Camera_MoveToPosition(mkr_brit_move1, true, SLOW_CAMERA_PANNING)
			
			Rule_AddInterval(CW_MoveTommies_MovingNormal, 0.5)
			
		else
			
			-- ask to select the Tommies
			Util_StartIntel(CW_EVENTS.SelectTommies)
			
			OBJ_CW_TOMMIES.PingID = Objective_AddUIElements(OBJ_CW_TOMMIES, sg_cw_tommies, true, 409733, true)
			
			Rule_AddInterval(CW_MoveTommies_TommiesSelected, 0.5)
			
		end
		
	end
	
end


function CW_MoveTommies_TommiesSelected()

	if Misc_IsSGroupSelected(sg_cw_tommies, ANY) then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		-- ask the player to move the Tommies
		Util_StartIntel(CW_EVENTS.PleaseMove)
		
		-- remove hints 
		Objective_RemoveUIElements(OBJ_CW_TOMMIES, OBJ_CW_TOMMIES.PingID)
		OBJ_CW_TOMMIES.PingID = Objective_AddUIElements(OBJ_CW_TOMMIES, mkr_brit_move1, true, 409715, true)
		
		Camera_MoveToPosition(mkr_brit_move1, true, SLOW_CAMERA_PANNING)
		
		Rule_AddInterval(CW_MoveTommies_MovingNormal, 0.5)
		
	end

end


function CW_MoveTommies_TommiesIntroDone()

	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		
		
		-- FOW Reveal
		Tutorial_RevealPos("Commonwealth", Marker_GetPosition(CW.camLock2), 40)
		Tutorial_RevealPos("Commonwealth", Marker_GetPosition(mkr_brit_move1), 20)
		FOW_RevealMarker(mkr_brit_cam2, -1)
		
		
	end
	
end

-- are the tommies moving?
function CW_MoveTommies_MovingNormal()
	
	if SGroup_IsMoving(sg_cw_tommies, ANY) then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Camera_FollowSGroup(sg_cw_tommies)
		
		-- excellent, moving normal
		Util_StartIntel(CW_EVENTS.MovingNormal)
		
		Rule_AddInterval(CW_MoveTommies_MovingSlow, 0.5)
		
	elseif Misc_IsSGroupSelected(sg_cw_tommies, ANY) == false then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		-- ask to re-select paratroopers
		Util_StartIntel(EVENTS.BritTommiesReSelect)
		
		Objective_RemoveUIElements(OBJ_CW_TOMMIES, OBJ_CW_TOMMIES.PingID)
		OBJ_CW_TOMMIES.PingID = Objective_AddUIElements(OBJ_CW_TOMMIES, sg_cw_tommies, true, 409733, true)
		
		Rule_AddInterval(CW_MoveTommies_TommiesSelected, 0.5)
	end
	
end

-- wait for the player to move out of their territory and start moving slowly 
function CW_MoveTommies_MovingSlow()

	if SGroup_IsMoving(sg_cw_tommies, false) and World_IsPointInPlayerTerritory( player5, SGroup_GetPosition_EVEN_IF_EMPTY(sg_cw_tommies)) == false then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		-- excellent, moving normal
		Util_StartIntel(CW_EVENTS.MovingSlow)
		
		Rule_AddInterval(CW_MoveTommies_Done, 0.5)
		
	end

end


function CW_MoveTommies_Done()
	
	-- condition for complete
	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		
		-- remove hints 
		Objective_RemoveUIElements(OBJ_CW_TOMMIES, OBJ_CW_TOMMIES.PingID)
		
		-- complete the objective
		Objective_Complete(OBJ_CW_TOMMIES, false)
		
		-- stop the tommies and move them to cover
		Cmd_Stop(sg_cw_tommies)
		Cmd_Move(sg_cw_tommies, mkr_brit_move1)
		
		-- tell the player what's up
		Util_StartIntel(CW_EVENTS.StopTommies)
		
		-- next lesson
		Rule_AddInterval(CW_RecruitLt_BeginLesson, 0.5)
		
	end
	
end







--------------------------------------------------------------------------------
-- Lesson: Recruit LT
--------------------------------------------------------------------------------
function CW_RecruitLt_BeginLesson()
	
	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		
		-- give the objective
		Objective_Start(OBJ_CW_LIEUTENANT, false)
		
		-- move camera to HQ
		Camera_MoveToSGroup(sg_cw_hq, true, SLOW_CAMERA_PANNING)
		OBJ_CW_LIEUTENANT.PingID = Objective_AddUIElements(OBJ_CW_LIEUTENANT, sg_cw_hq, true, 409735, true, 2)
		
		-- unrestrict Lt
		Tutorial_SafetySetResources(cost.cw.lieutenant)
		
		-- select the hq
		Util_StartIntel(CW_EVENTS.SelectHQToRecruitLt)
		
		Rule_AddInterval(CW_RecruitLt_HQSelected, 0.5)
		
	end
	
end


-- is the HQ selected
function CW_RecruitLt_HQSelected()

	if Misc_IsSGroupSelected(sg_cw_hq, ANY) then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Objective_RemoveUIElements(OBJ_CW_LIEUTENANT, OBJ_CW_LIEUTENANT.PingID)
		
		Player_SetPopCapOverride(player5, 6)
		
		Player_SetSquadProductionAvailability(player5, SBP.CW.LIEUTENANT, ITEM_DEFAULT)
		
		-- build Lt
		Util_StartIntel(CW_EVENTS.RecruitLt)
		flashid_lieutenant = Util_AddProdFlash(PITEM_Spawn, SBP.CW.LIEUTENANT, true, 409716, BT_UI_Strong_CommandBtn)
		
		Rule_Add(CW_RecruitLt_BuildingLt)
		
	end
	
end

-- is the player recruiting an LT
-- TODO: need a better solution for this!
function CW_RecruitLt_BuildingLt()

	if SGroup_IsProducingSquad(sg_cw_hq, SBP.CW.LIEUTENANT, ANY)  then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Player_SetSquadProductionAvailability(player5, SBP.CW.LIEUTENANT, ITEM_LOCKED)
		
		local count = Squad_GetProductionQueueSize(SGroup_GetSpawnedSquadAt(sg_cw_hq, 1))
		if count >= 2 then
			for n = 2, count do
				hq_productionqueue = hq_productionqueue + 1
				Command_SquadExt(player5, sg_cw_hq, SCMD_CancelProduction, hq_productionqueue, false)
			end
		end
		Rule_AddOneShot(CW_RecruitLt_BuildingLtHintpoint, 0.1)
		
		-- Lt is being built
		Util_StartIntel(CW_EVENTS.LtOnRoute)
		
		Rule_AddInterval(CW_RecruitLt_LtDone, 0.5)
		
	elseif Misc_IsSGroupSelected(sg_cw_hq, ANY) == false then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		-- ask to reselect HQ
		Util_StartIntel(EVENTS.BritCommandTruckReSelect)
		
		Util_StopFlashing(flashid_lieutenant)
		OBJ_CW_LIEUTENANT.PingID = Objective_AddUIElements(OBJ_CW_LIEUTENANT, sg_cw_hq, true, 409735, true, 2)
		
		Rule_AddInterval(CW_RecruitLt_HQSelected, 0.5)
		
	end
	
end
function CW_RecruitLt_BuildingLtHintpoint()
	hpid_cw_lieutenant = HintPoint_AddToProductionQueueItem(0, 409585, true)
end
	

-- is the player recruiting an LT
function CW_RecruitLt_LtDone()

	Player_GetAll(player5, sg_cw_lt)
	SGroup_Filter(sg_cw_lt, SBP.CW.LIEUTENANT, FILTER_KEEP)
	
	if SGroup_Count(sg_cw_lt) >= 1 then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		hq_productionqueue = hq_productionqueue + 1
		
		HintPoint_Remove(hpid_cw_lieutenant)
		SGroup_SetInvulnerable(sg_cw_lt, 0.6)
		
		-- Lt arrives
		Util_StartIntel(CW_EVENTS.LtDone)
		
		Rule_AddInterval(CW_MoveLT_BeginLesson, 1)
		
	elseif SGroup_IsProducingSquad(sg_cw_hq, SBP.CW.LIEUTENANT, ANY) == false then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Player_SetSquadProductionAvailability(player5, SBP.CW.LIEUTENANT, ITEM_DEFAULT)
		
		hq_productionqueue = hq_productionqueue + 1
		
		-- build Lt
		Util_StartIntel(CW_EVENTS.RecruitLtAgain)
		flashid_lieutenant = Util_AddProdFlash(PITEM_Spawn, SBP.CW.LIEUTENANT, true, 409716, BT_UI_Strong_CommandBtn)
		
		Rule_Add(CW_RecruitLt_BuildingLt)
		
	end
	
end


--------------------------------------------------------------------------------
-- Lesson: Move To Tommies
--------------------------------------------------------------------------------

function CW_MoveLT_BeginLesson()

	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		
		-- release Camera 
		Camera_SetInputEnabled(true)
		
		Objective_UpdateText(OBJ_CW_LIEUTENANT, 409660, 409661, false)
		
		if Misc_IsSGroupSelected(sg_cw_lt, ANY) then
			
			OBJ_CW_LIEUTENANT.PingID = Objective_AddUIElements(OBJ_CW_LIEUTENANT, mkr_brit_move_lt, true, 409717, true)
			
			-- build Lt
			Util_StartIntel(CW_EVENTS.MoveLtNearTommies)
			
			Rule_AddInterval(CW_MoveLT_LtMoving, 0.5)
			
		else
			
			-- ask to select the lt
			Util_StartIntel(EVENTS.BritLieutenantSelect)
			
			OBJ_CW_LIEUTENANT.PingID = Objective_AddUIElements(OBJ_CW_LIEUTENANT, sg_cw_lt, true, 409734, true, 2)
			
			Rule_AddInterval(CW_MoveLT_SelectLt, 0.5)
			
		end
		
	end
	
end

-- is the LT selected
function CW_MoveLT_SelectLt()
	
	if Misc_IsSGroupSelected(sg_cw_lt, ANY) then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Objective_RemoveUIElements(OBJ_CW_LIEUTENANT, OBJ_CW_LIEUTENANT.PingID)
		OBJ_CW_LIEUTENANT.PingID = Objective_AddUIElements(OBJ_CW_LIEUTENANT, mkr_brit_move_lt, true, 409717, true)
		
		Camera_MoveToPosition(mkr_brit_move_lt, true, SLOW_CAMERA_PANNING)
		
		-- build Lt
		Util_StartIntel(CW_EVENTS.MoveLtNearTommies)
		
		Rule_AddInterval(CW_MoveLT_LtMoving, 0.5)
		
	end
	
end

-- is the Lt moving
function CW_MoveLT_LtMoving()

	if SGroup_IsMoving(sg_cw_lt, ANY) then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		-- Lt influence
		Util_StartIntel(CW_EVENTS.IntroLtInfluence)
		Camera_FollowSGroup(sg_cw_lt)
		
		Rule_AddInterval(CW_MoveLT_NearTommies, 0.5)
		
	elseif Misc_IsSGroupSelected(sg_cw_lt, ANY) == false then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Objective_RemoveUIElements(OBJ_CW_LIEUTENANT, OBJ_CW_LIEUTENANT.PingID)
		OBJ_CW_LIEUTENANT.PingID = Objective_AddUIElements(OBJ_CW_LIEUTENANT, sg_cw_lt, true, 409734, true, 2)
		
		-- ask to reselect the lt
		Util_StartIntel(EVENTS.BritLieutenantReSelect)
		
		Rule_AddInterval(CW_MoveLT_SelectLt, 0.5)
		
	end

end

-- is the Lt near the Tommies
function CW_MoveLT_NearTommies()

	if Prox_SGroupSGroup(sg_cw_tommies, sg_cw_lt, PROX_SHORTEST) < 28 then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Objective_RemoveUIElements(OBJ_CW_LIEUTENANT, OBJ_CW_LIEUTENANT.PingID)
		Camera_MoveToSGroup(sg_cw_tommies, true, SLOW_CAMERA_PANNING)
		
		-- build Lt
		Util_StartIntel(CW_EVENTS.InLtInfluence)
		
		Rule_AddInterval(CW_AttachLt_BeginLesson, 0.5)
		
	end

end




--------------------------------------------------------------------------------
-- Lesson: Attach To Tommies
--------------------------------------------------------------------------------


function CW_AttachLt_BeginLesson()
	
	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		
		Objective_UpdateText(OBJ_CW_LIEUTENANT, 409665, 409666, false)
		OBJ_CW_LIEUTENANT.PingID = nil
		
		if Misc_IsSGroupSelected(sg_cw_lt, ANY) then
			
			if SGroup_IsDoingAbility(sg_cw_lt, ABILITY.CW.LIEUTENANT_FOLLOW, ANY) == false then
				
				Util_StartIntel(CW_EVENTS.AttachOrder)
				
				-- flash the button
				if flashid_linklieutenant == nil then
					flashid_linklieutenant = Util_AddAbilityFlash(ABILITY.CW.LIEUTENANT_FOLLOW, false, 409718, BT_UI_Strong_AbilityBtn)
				end
				
				-- start ability watchers
				UI_SetModalAbilityPhaseCallback(CW_LinkingLieutenant)
				
			end
			
			Rule_AddInterval(CW_AttachLt_IsAttached, 0.5)
			
		else
			
			Util_StartIntel(EVENTS.BritLieutenantSelect)
			
			OBJ_CW_LIEUTENANT.PingID = Objective_AddUIElements(OBJ_CW_LIEUTENANT, sg_cw_lt, false, 409734, true)
			
			Rule_AddInterval(CW_AttachLt_SelectLt, 0.5)
			
		end
		
	end
	
end


function CW_AttachLt_SelectLt()
	
	if Misc_IsSGroupSelected(sg_cw_lt, ANY) then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Objective_RemoveUIElements(OBJ_CW_LIEUTENANT, OBJ_CW_LIEUTENANT.PingID)
		OBJ_CW_LIEUTENANT.PingID = nil
		
		if SGroup_IsDoingAbility(sg_cw_lt, ABILITY.CW.LIEUTENANT_FOLLOW, ANY) == false then
			
			Util_StartIntel(CW_EVENTS.AttachOrder)
			
			-- flash the button
			if flashid_linklieutenant == nil then
				flashid_linklieutenant = Util_AddAbilityFlash(ABILITY.CW.LIEUTENANT_FOLLOW, false, 409718, BT_UI_Strong_AbilityBtn)
			end
			
			-- start ability watchers
			UI_SetModalAbilityPhaseCallback(CW_LinkingLieutenant)
			
		end
		
		Rule_AddInterval(CW_AttachLt_IsAttached, 0.5)
		
	end
	
end


function CW_LinkingLieutenant(ability, phase)

	if ability == ABILITY.CW.LIEUTENANT_FOLLOW then
		
		if phase == MAP_Placing then
			
			Game_SkipEvent()
			Util_StartIntel(CW_EVENTS.AttachOrderTarget)
			if OBJ_CW_LIEUTENANT.PingID == nil then
				OBJ_CW_LIEUTENANT.PingID = Objective_AddUIElements(OBJ_CW_LIEUTENANT, sg_cw_tommies, false, 409719, true)
			end
			
		elseif phase == MAP_Confirmed then
			
			Util_StopFlashing(flashid_linklieutenant)
			
		end
		
	end
	
end

function CW_AttachLt_IsAttached()

	if SGroup_IsDoingAbility(sg_cw_lt, ABILITY.CW.LIEUTENANT_FOLLOW, ANY) then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		UI_ClearModalAbilityPhaseCallback()
		
		Util_StartIntel(CW_EVENTS.LtAttached)
		
		Objective_RemoveUIElements(OBJ_CW_LIEUTENANT, OBJ_CW_LIEUTENANT.PingID)
		OBJ_CW_LIEUTENANT.PingID = nil
		
		Rule_AddInterval(CW_AttachLt_Done, 0.5)
		
	elseif Misc_IsSGroupSelected(sg_cw_lt, ANY) == false then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		UI_ClearModalAbilityPhaseCallback()
		
		Util_StopFlashing(flashid_linklieutenant)
		
		Util_StartIntel(EVENTS.BritLieutenantReSelect)
		
		if OBJ_CW_LIEUTENANT.PingID ~= nil then
			Objective_RemoveUIElements(OBJ_CW_LIEUTENANT, OBJ_CW_LIEUTENANT.PingID)
		end
		OBJ_CW_LIEUTENANT.PingID = Objective_AddUIElements(OBJ_CW_LIEUTENANT, sg_cw_lt, false, 409734, true)
		
		Rule_AddInterval(CW_AttachLt_SelectLt, 0.5)
		
	end
	
end


-- done with this section
function CW_AttachLt_Done()
	
	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		
		-- complete the objective
		Objective_Complete(OBJ_CW_LIEUTENANT, false)
		
		-- next lesson
		Rule_AddInterval(CW_Capture_BeginLesson, 0.5)
		
	end
	
end




--------------------------------------------------------------------------------
-- Lesson: Capture Point / Ambush
--------------------------------------------------------------------------------
function CW_Capture_BeginLesson()
	
	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		
		-- give the objective
		Objective_Start(OBJ_CW_CAPTURE, false)
		
		-- adjust the camera 
		Camera_ClampToMarker(CW.camLock3)
		Tutorial_RevealPos("Commonwealth", Marker_GetPosition(CW.camLock3), 40)
		
		-- despawn the blocking wall
		EGroup_DeSpawn(LAYER_brit_block2)
		
		if Misc_IsSGroupSelected(sg_cw_tommies, ANY) then
			
			-- give the introductory talk
			Util_StartIntel(CW_EVENTS.CaptureThePoint)
			Camera_MoveToEGroup(eg_cw_munitionsPoint, true, SLOW_CAMERA_PANNING)
			
			OBJ_CW_CAPTURE.PingID = Objective_AddUIElements(OBJ_CW_CAPTURE, eg_cw_munitionsPoint, false, 409720, true, 2)
			
			-- next
			Rule_AddInterval(CW_Capture_Moving, 0.5)
			
		else
			
			Util_StartIntel(EVENTS.BritTommiesSelect)
			
			OBJ_CW_CAPTURE.PingID = Objective_AddUIElements(OBJ_CW_CAPTURE, sg_cw_tommies, false, 409733, true)
			
			Rule_AddInterval(CW_Capture_SelectTommies, 0.5)
			
		end
		
	end
	
end


function CW_Capture_SelectTommies()

	if Misc_IsSGroupSelected(sg_cw_tommies, ANY) then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		-- give the introductory talk
		Util_StartIntel(CW_EVENTS.CaptureThePoint)
		Camera_MoveToEGroup(eg_cw_munitionsPoint, true, SLOW_CAMERA_PANNING)
		
		Objective_RemoveUIElements(OBJ_CW_CAPTURE, OBJ_CW_CAPTURE.PingID)
		OBJ_CW_CAPTURE.PingID = Objective_AddUIElements(OBJ_CW_CAPTURE, eg_cw_munitionsPoint, false, 409720, true, 2)
		
		Rule_AddInterval(CW_Capture_Moving, 0.5)
		
	end

end


function CW_Capture_Moving()

	if SGroup_IsMoving(sg_cw_tommies, ANY) or Prox_AreSquadsNearMarker(sg_cw_lt, EGroup_GetPosition(eg_cw_munitionsPoint), ANY, 10) then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		-- point out the speed difference
		Util_StartIntel(CW_EVENTS.CapSpeedDifference)
		Camera_FollowSGroup(sg_cw_tommies)
		
		-- start attack
		Rule_AddOneShot(CW_Capture_AttackCreate, 1)
		
	elseif Misc_IsSGroupSelected(sg_cw_tommies, ANY) == false then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Util_StartIntel(EVENTS.BritTommiesReSelect)
		
		Objective_RemoveUIElements(OBJ_CW_CAPTURE, OBJ_CW_CAPTURE.PingID)
		OBJ_CW_CAPTURE.PingID = Objective_AddUIElements(OBJ_CW_CAPTURE, sg_cw_tommies, false, 409733, true)
		
		Rule_AddInterval(CW_Capture_SelectTommies, 0.5)
		
	end
	
end


function CW_Capture_AttackCreate()

	-- create the Germans
	for k,v in pairs(CW.capAttack) do 
		Util_CreateSquads(player2, v.sgroup, v.sbp, v.src, v.dest, 1, v.num)
		SGroup_AddGroup(sg_cw_axisAll, v.sgroup)
		Modify_Vulnerability(v.sgroup, 1.2)
		Cmd_AttackMove(v.sgroup, v.dest)
	end
	
	Rule_AddInterval(CW_Capture_AttackContact, 0.5)

end


function CW_Capture_AttackContact()

	-- alert the player
	if SGroup_IsUnderAttack(sg_cw_axisAll, false, 10) or SGroup_IsDoingAttack(sg_cw_axisAll, false, 10) then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Cmd_Attack(sg_cw_tommies, sg_cw_axisAll)
		
		Objective_RemoveUIElements(OBJ_CW_CAPTURE, OBJ_CW_CAPTURE.PingID)
		
		-- camera changes
		Camera_ClampToMarker(CW.camLock4)
		Tutorial_RevealPos("Commonwealth", Marker_GetPosition(CW.camLock4), 40)
		
		-- the attackers have struck
		Util_StartIntel(CW_EVENTS.CapAttack)
		ThreatArrow_CreateGroup(sg_cw_axisAll)
		
		Rule_AddInterval(CW_Capture_AttackRetreat, 0.5)
		
	end

end


function CW_Capture_AttackRetreat()

	-- when the attack has gone on long enough or the Germans have lost enough guys
	if SGroup_TotalMembersCount(sg_cw_axisAll) <= 3 or EGroup_IsCapturedByPlayer(eg_cw_munitionsPoint, player5, ALL) then
		
		Rule_RemoveMe()
		
		if SGroup_Count(sg_cw_axisAll) >= 1 then
			Cmd_Retreat(sg_cw_axisAll, Marker_GetPosition(mkr_brit_axisSpawn1), mkr_brit_axisSpawn1)
			Util_StartIntel(CW_EVENTS.AttackRetreat)
		end
		
		Rule_AddInterval(CW_Capture_AttackRetreatB, 0.5)
		
	end
	
end
function CW_Capture_AttackRetreatB()

	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		
		if EGroup_IsCapturedByPlayer(eg_cw_munitionsPoint, player5, ALL) == false then
			OBJ_CW_CAPTURE.PingID = Objective_AddUIElements(OBJ_CW_CAPTURE, eg_cw_munitionsPoint, false, 409720, true, 2)
			Camera_MoveToEGroup(eg_cw_munitionsPoint, true, SLOW_CAMERA_PANNING)
			Util_StartIntel(CW_EVENTS.CaptureProceedCapturing)
		end
		
		Rule_AddInterval(CW_Capture_Done, 0.5)
		
	end

end


function CW_Capture_Done()
	
	-- condition for complete
	if EGroup_IsCapturedByPlayer(eg_cw_munitionsPoint, player5, true) then
		
		-- standard
		Rule_RemoveMe()
		Game_SkipEvent()
		
		-- complete the objective
		Objective_Complete(OBJ_CW_CAPTURE, false)
		
		Util_StartIntel(CW_EVENTS.CaptureDone)
		
		-- next lesson
		Rule_AddInterval(CW_MoveHQ_BeginLesson, 0.5)
		
	end
	
end




--------------------------------------------------------------------------------
-- Lesson: Move HQ
--------------------------------------------------------------------------------

function CW_MoveHQ_BeginLesson()

	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		
		-- give the introductory talk
		Util_StartIntel(CW_EVENTS.MoveHQIntro)
		
		Rule_AddInterval(CW_MoveHQ_BeginLessonB, 0.5)
		
	end
	
end


function CW_MoveHQ_BeginLessonB()

	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		
		Camera_MoveToSGroup(sg_cw_hq, true, SLOW_CAMERA_PANNING)
		
		-- lockout zones for deploying the HQ
		for k, this in pairs(CW.deploylockoutzones) do 
			Player_AddAbilityLockoutZone(player5, ABILITY.CW.HQ_SETUP_MAINHQ, this)
		end
		
		-- give the introductory talk
		Util_StartIntel(CW_EVENTS.HqCanMove)
		
		Rule_AddInterval(CW_MoveHQ_BeginLessonC, 0.5)
		
	end

end


function CW_MoveHQ_BeginLessonC()
	
	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		
		-- give the objective
		Objective_Start(OBJ_CW_MOVEHQ, false)
		
		-- camera changes
		FOW_RevealMarker(mkr_brit_cam3, -1)
		
		if Misc_IsSGroupSelected(sg_cw_hq, ANY) then
			
			Util_StartIntel(CW_EVENTS.PackItUp)
			
			Player_SetUpgradeAvailability(player5, UPG.CW.ENABLE_MOBILE_HQ, ITEM_DEFAULT)
			UI_FlashProductionButton(PITEM_Upgrade, UPG.CW.ENABLE_MOBILE_HQ, true, BT_UI_Strong_CommandBtn)
			
			Rule_AddInterval(CW_MoveHQ_PackItUp, 0.5)
			
		else
			
			Util_StartIntel(EVENTS.BritCommandTruckSelect)
			
			OBJ_CW_MOVEHQ.PingID = Objective_AddUIElements(OBJ_CW_MOVEHQ, sg_cw_hq, true, 409735, true, 2, true)
			
			Rule_AddInterval(CW_MoveHQ_SelectHQ, 0.5)
			
		end
		
	end
	
end


function CW_MoveHQ_SelectHQ()

	if Misc_IsSGroupSelected(sg_cw_hq, ANY) then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Objective_RemoveUIElements(OBJ_CW_MOVEHQ, OBJ_CW_MOVEHQ.PingID)
		
		Util_StartIntel(CW_EVENTS.PackItUp)
		
		Rule_AddInterval(CW_MoveHQ_SelectHQB, 0.5)
		
	end
	
end

function CW_MoveHQ_SelectHQB()

	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		
		Util_StartIntel(CW_EVENTS.PackItUpB)
		
		Player_SetUpgradeAvailability(player5, UPG.CW.ENABLE_MOBILE_HQ, ITEM_DEFAULT)
		UI_FlashProductionButton(PITEM_Upgrade, UPG.CW.ENABLE_MOBILE_HQ, true, BT_UI_Strong_CommandBtn)
		
		Rule_AddInterval(CW_MoveHQ_PackItUp, 0.5)
		
	end
	
end

function CW_MoveHQ_PackItUp()

	if Util_GetCommonwealthHQMobility(sg_cw_hq) == true then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Rule_AddOneShot(CW_MoveHQ_PackItUpB, 9)
		
	elseif Misc_IsSGroupSelected(sg_cw_hq, ANY) == false then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Util_StartIntel(EVENTS.BritCommandTruckReSelect)
		
		OBJ_CW_MOVEHQ.PingID = Objective_AddUIElements(OBJ_CW_MOVEHQ, sg_cw_hq, true, 409735, true, 2, true)
		
		Rule_AddInterval(CW_MoveHQ_SelectHQ, 0.5)
		
	end
	
end


function CW_MoveHQ_PackItUpB()

	Util_StartIntel(CW_EVENTS.HQPackedUp)
	
	Player_SetUpgradeAvailability(player5, UPG.CW.ENABLE_MOBILE_HQ, ITEM_LOCKED)
	
	Rule_AddInterval(CW_MoveHQ_PackItUpC, 0.5)
	
end


function CW_MoveHQ_PackItUpC()

	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		
		Util_StartIntel(CW_EVENTS.MoveHQ)
		
		Camera_MoveToPosition(mkr_brit_hqMoveTo, true, SLOW_CAMERA_PANNING)
		
		OBJ_CW_MOVEHQ.PingID = Objective_AddUIElements(OBJ_CW_MOVEHQ, mkr_brit_hqMoveTo, true, 409723, true)
		Objective_UpdateText(OBJ_CW_MOVEHQ, 409680, 409681, false)
		
		Rule_AddInterval(CW_MoveHQ_MovingIt, 0.5)
		Rule_AddInterval(CW_MoveHQ_DeployIt, 0.5)
		
	end
	
end


-- Is it moving?
function CW_MoveHQ_MovingIt()

	if SGroup_IsMoving(sg_cw_hq, ANY) then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Util_StartIntel(CW_EVENTS.MoveInfo)
		Camera_FollowSGroup(sg_cw_hq)
		
	end
	
end

-- Once it's near, order it to deploy
function CW_MoveHQ_DeployIt()

	if Prox_AreSquadsNearMarker(sg_cw_hq, mkr_brit_hqMoveTo, false, 10) then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Objective_RemoveUIElements(OBJ_CW_MOVEHQ, OBJ_CW_MOVEHQ.PingID)
		
		Util_StartIntel(CW_EVENTS.Deploy)
		
		Rule_AddInterval(CW_MoveHQ_DeployItB, 0.5)
		
	end
	
end


function CW_MoveHQ_DeployItB()
	
	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		
		Util_StartIntel(CW_EVENTS.DeployB)
		
		--Player_SetUpgradeAvailability(player5, UPG.CW.MOBILEHQ, ITEM_DEFAULT)
		--UI_FlashAbilityButton(ABILITY.CW.HQ_SETUP_MAINHQ, true, BT_UI_Strong_CommandBtn)
		Player_SetAbilityAvailability(player5, ABILITY.CW.HQ_SETUP_MAINHQ, ITEM_DEFAULT)
		flashid_deployhq = Util_AddProdFlash(PITEM_Upgrade, UPG.CW.MOBILEHQ, false, 409722, BT_UI_Strong_CommandBtn)	-- need to flash this button rather than the ability button for some reason
		
		UI_SetModalAbilityPhaseCallback(CW_MoveHQ_DeployButtonClicked)
		
	end	
	
end


function CW_MoveHQ_DeployButtonClicked(ability, phase)
	
	if ability == ABILITY.CW.HQ_SETUP_MAINHQ then
		
		if phase == MAP_Placing then
			
			Game_SkipEvent()
			
			Util_StartIntel(CW_EVENTS.DeployC)
			
			OBJ_CW_MOVEHQ.PingID = Objective_AddUIElements(OBJ_CW_MOVEHQ, mkr_brit_hqDeploy, true, 409723, true)
			
		elseif phase == MAP_Confirmed then
			
			UI_ClearModalAbilityPhaseCallback()
			Util_StopFlashing(flashid_deployhq)
			
			Rule_AddInterval(CW_MoveHQ_Done, 0.5)
			
		end
		
	end
	
end


function CW_MoveHQ_Done()

	if Util_GetCommonwealthHQMobility(sg_cw_hq) == false then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Objective_RemoveUIElements(OBJ_CW_MOVEHQ, OBJ_CW_MOVEHQ.PingID)
		Rule_AddOneShot(CW_MoveHQ_DoneB, 9)
		
	end
	
end
function CW_MoveHQ_DoneB()
	
	-- complete the objective
	Objective_Complete(OBJ_CW_MOVEHQ, false)
	
	Util_StartIntel(CW_EVENTS.Deployed)
	
	-- next lesson
	Rule_AddInterval(CW_BuildMG_BeginLesson, 0.5)
	
end








--------------------------------------------------------------------------------
-- Lesson: Build MG
--------------------------------------------------------------------------------
function CW_BuildMG_BeginLesson()
	
	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		
		-- give the objective
		Objective_Start(OBJ_CW_BUILDMGNEST, false)
		
		Player_SetPopCapOverride(player5, 9)
		
		-- set up a restriction for this lesson
		UI_RestrictBuildingPlacement(mkr_brit_mgBuild)
		
		-- remove any previous attempts
		SGroup_Clear(sg_temp)
		EGroup_GetSquadsHeld(eg_cw_mgNest, sg_temp)
		SGroup_DestroyAllSquads(sg_temp)
		EGroup_Hide(eg_cw_mgNest, true)
		EGroup_DestroyAllEntities(eg_cw_mgNest)
		
		-- give the introductory talk
		Util_StartIntel(CW_EVENTS.BuildMG)
		
		Camera_MoveToSGroup(sg_cw_tommies, true, SLOW_CAMERA_PANNING)
		
		OBJ_CW_BUILDMGNEST.PingID = Objective_AddUIElements(OBJ_CW_BUILDMGNEST, sg_cw_tommies, true, 409733, true)
		Rule_AddInterval(CW_BuildMG_TommiesSelected, 0.5)
		
	end
	
end


function CW_BuildMG_TommiesSelected()

	if Misc_IsSGroupSelected(sg_cw_tommies, ANY) then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Player_SetEntityProductionAvailability(player5, EBP.CW.MG_NEST, ITEM_DEFAULT)
		Player_SetConstructionMenuAvailability(player5, "tp_construction_commonwealth_infantry", ITEM_DEFAULT)
		Tutorial_SafetySetResources(cost.cw.mgnest)
		
		-- build MG
		Util_StartIntel(CW_EVENTS.ConstructionButton)
		
		Objective_RemoveUIElements(OBJ_CW_BUILDMGNEST, OBJ_CW_BUILDMGNEST.PingID)
		
		if flashid_mgnest1 == nil then
			flashid_mgnest1 = Util_AddConstructionMenuFlash("tp_construction_commonwealth_infantry", false, 409724, BT_UI_Strong_CommandBtn)		
		end
		
		Rule_AddInterval(CW_BuildMG_BuildMenuSelected, 0.5)
		UI_SetBuildingPlacementCallback(CW_BuildMG_PlacingMG)
		
	end
	
end


-- the tommies are deselected
function CW_BuildMG_Deselected()

	if Misc_IsSGroupSelected(sg_cw_tommies, ANY) == false then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Objective_RemoveUIElements(OBJ_CW_BUILDMGNEST, OBJ_CW_BUILDMGNEST.PingID)
		EGroup_DeSpawn(eg_cw_mgnestfacing)			-- hide rotation arrow
		
		OBJ_CW_BUILDMGNEST.PingID = Objective_AddUIElements(OBJ_CW_BUILDMGNEST, sg_cw_tommies, true, 409733, true)
		Rule_AddInterval(CW_BuildMG_TommiesSelected, 0.5)
		
	end

end


-- build menu selected, now flash the AT gun button
function CW_BuildMG_BuildMenuSelected()

	if _ConstructionMenuStatus == "tp_construction_commonwealth_infantry" then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Util_StartIntel(CW_EVENTS.MGButton)
		
		if flashid_mgnest2 == nil then
			flashid_mgnest2 = Util_AddConstructionFlash(EBP.CW.MG_NEST, false, 409724, BT_UI_Strong_CommandBtn)
		end
		
		Rule_AddInterval(CW_BuildMG_Deselected, 0.5)
		
	elseif Misc_IsSGroupSelected(sg_cw_tommies, ANY) == false then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Util_StartIntel(EVENTS.BritTommiesReSelect)
		
		OBJ_CW_BUILDMGNEST.PingID = Objective_AddUIElements(OBJ_CW_BUILDMGNEST, sg_cw_tommies, true, 409733, true)
		Rule_AddInterval(CW_BuildMG_TommiesSelected, 0.5)
		
	end

end


-- callback function deals with stages for building the AT gun
function CW_BuildMG_PlacingMG(ebp, phase)
	
	if ebp == EBP.CW.MG_NEST then
		
		-- placing
		if phase == CP_Selected then
			
			Game_SkipEvent()
			
			Util_StartIntel(CW_EVENTS.MGPosition)
			OBJ_CW_BUILDMGNEST.PingID = Objective_AddUIElements(OBJ_CW_BUILDMGNEST, mkr_brit_mgBuild, true, 409725, true)
			
			Camera_MoveToPosition(mkr_brit_mgBuild, true, SLOW_CAMERA_PANNING)
			
		-- rotating
		elseif phase == CP_StartedPlacing then
			
			Game_SkipEvent()
			
			Util_StartIntel(CW_EVENTS.MGFacing)
			Objective_RemoveUIElements(OBJ_CW_BUILDMGNEST, OBJ_CW_BUILDMGNEST.PingID)
			OBJ_CW_BUILDMGNEST.PingID = Objective_AddUIElements(OBJ_CW_BUILDMGNEST,  mkr_brit_mgFacing, false, 409726, false)
			
			EGroup_ReSpawn(eg_cw_mgnestfacing)			-- show rotation arrow
			
		-- placed
		elseif phase == CP_FinishedPlacing then
			
			UI_ClearBuildingPlacementCallback()
			UI_UnrestrictBuildingPlacement()
			
			Game_SkipEvent()
			
			Player_SetConstructionMenuAvailability(player5, "tp_construction_commonwealth_infantry", ITEM_LOCKED)
			
			if flashid_mgnest1 ~= nil then
				Util_StopFlashing(flashid_mgnest1)
			end
			if flashid_mgnest2 ~= nil then
				Util_StopFlashing(flashid_mgnest2)
			end
			
			flashid_mgnest1 = nil
			flashid_mgnest2 = nil
			
			Objective_RemoveUIElements(OBJ_CW_BUILDMGNEST, OBJ_CW_BUILDMGNEST.PingID)
			EGroup_DeSpawn(eg_cw_mgnestfacing)			-- hide rotation arrow
			
			Rule_RemoveIfExist(CW_BuildMG_Deselected)
			Rule_RemoveIfExist(CW_BuildMG_BuildMenuSelected)
			Rule_AddInterval(CW_BuildMG_MGBuilt, 1)
			Rule_AddOneShot(CW_BuildMG_LockoutOption, 1)
			
		end
		
	end		
	
end



function CW_BuildMG_LockoutOption()
	Player_SetEntityProductionAvailability(player5, EBP.CW.MG_NEST, ITEM_LOCKED)
end


function CW_BuildMG_StartedRebuilding()
	
	if SGroup_IsConstructingBuilding(sg_cw_tommies, ANY) then
		Rule_RemoveMe()
		Objective_RemoveUIElements(OBJ_CW_BUILDMGNEST, OBJ_CW_BUILDMGNEST.PingID)
		Rule_AddInterval(CW_BuildMG_MGBuilt, 1)
	end
	
end


function CW_BuildMG_MGBuilt()

	if Player_HasBuilding(player5, EBP.CW.MG_NEST) then
		
		Rule_RemoveMe()
		Game_SkipAllEvents(false)
		
		Player_GetAll(player5)
		EGroup_Filter(eg_allentities, EBP.CW.MG_NEST, FILTER_KEEP)
		EGroup_FilterUnderConstruction(eg_allentities, FILTER_REMOVE)
		EGroup_AddEGroup(eg_cw_mgNest, eg_allentities)
		
		local dir1 = Entity_GetHeading(EGroup_GetSpawnedEntityAt(eg_cw_mgNest, 1))
		
		-- dir2 is the direction from the HMG to the requested direction
		local hmgpos = EGroup_GetPosition(eg_cw_mgNest)
		local enemypos = Marker_GetPosition(mkr_brit_mgFacing)
		local dist = World_DistancePointToPoint(hmgpos, enemypos)
		local dir2 = World_Pos((enemypos.x - hmgpos.x)/dist, (enemypos.y - hmgpos.y)/dist, (enemypos.z - hmgpos.z)/dist)
		
		-- distance between the points should be less than 0.5 (approx. 30 degrees either side)
		if World_DistancePointToPoint(dir1, dir2) < 0.5 then
			
			Modify_WeaponRange(eg_cw_mgNest, "hardpoint_01", 3)		-- make it shoot farther
			
			Util_StartIntel(CW_EVENTS.MGDone)
			
			-- start the enemies coming
			EGroup_DeSpawn(LAYER_brit_block3)
			
			CW_BuildMG_MGAttack()
			Rule_AddIntervalEx(CW_BuildMG_MGAttack, 10, 4)
			Rule_AddInterval(CW_BuildMG_MGAttack_Track, 1)
			
			Rule_AddDelayedInterval(CW_BuildMG_Done, 6, 1)
			
		else
			
			Util_StartIntel(CW_EVENTS.MGWrongDirection)
			
			Rule_AddInterval(CW_BuildMG_BeginLesson, 0.5)
			
		end
		
		
	elseif SGroup_IsConstructingBuilding(sg_cw_tommies, ANY) == false then
		
		Rule_RemoveMe()
		
		Player_GetAll(player5)
		EGroup_Filter(eg_allentities, EBP.CW.MG_NEST, FILTER_KEEP)
		
		if EGroup_Count(eg_allentities) >= 1 then
			OBJ_CW_BUILDMGNEST.PingID = Objective_AddUIElements(OBJ_CW_BUILDMGNEST, eg_allentities, false, 409736, true)
			Rule_AddInterval(CW_BuildMG_StartedRebuilding, 0.5)
		else
			Rule_AddInterval(CW_BuildMG_BeginLesson, 0.5)
		end
		
	end
	
end




-- start the enemies coming
function CW_BuildMG_MGAttack()

	-- create the Germans
	for k,v in pairs(CW.mgAttack) do
		-- retreat first
		if SGroup_IsEmpty(v.sgroup) == false and SGroup_TotalMembersCount(v.sgroup) < 4 then
			Cmd_Retreat(v.sgroup, Marker_GetPosition(v.src), v.src)
		elseif SGroup_Count(v.sgroup) < 3 then
			Util_CreateSquads(player2, v.sgroup, v.sbp, v.src)
			Modify_Vulnerability(v.sgroup, 1.5)
			SGroup_AddGroup(sg_cw_axisAll, v.sgroup)
			Cmd_Move(v.sgroup, v.dest, true, NIL_DELETE, NIL_FACE, NIL_OFFSET, NIL_DIST, 5)
		end
	end

end


-- start the enemies coming
function CW_BuildMG_MGAttack_Track()

	if SGroup_IsDoingAttack(sg_cw_axisAll, ANY, 10) then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		-- play info
		Util_StartIntel(CW_EVENTS.MGAttack)
		Util_StartIntel(CW_EVENTS.MGInfo)
		
		Objective_UpdateText(OBJ_CW_BUILDMGNEST, 409690, 409691, false)
		
	elseif SGroup_IsEmpty(sg_cw_axisAll) then
		Rule_RemoveMe()
	end

end


function CW_BuildMG_Done()
	
	if Rule_Exists(CW_BuildMG_MGAttack) == false then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Cmd_Retreat(sg_cw_axisAll, Marker_GetPosition(mkr_brit_axisSpawn3), mkr_brit_axisSpawn3)
		
		Rule_AddOneShot(CW_BuildMG_DoneB, 5)
		
	end
	
end


function CW_BuildMG_DoneB()

	-- complete the objective
	Objective_Complete(OBJ_CW_BUILDMGNEST, false)
	
	Util_StartIntel(CW_EVENTS.MGAttackDone)
	
	-- next lesson
	Rule_AddInterval(CW_Trench_BeginLesson, 0.5)
	
end





--------------------------------------------------------------------------------
-- Lesson: Build Trench
--------------------------------------------------------------------------------
function CW_Trench_BeginLesson()
	
	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		
		-- give the objective
		Objective_Start(OBJ_CW_BUILDTRENCH, false)
		
		-- give the introductory talk
		Util_StartIntel(CW_EVENTS.TrenchStart)
		
		-- remove any previous attempts
		EGroup_Hide(eg_cw_trench, true)
		EGroup_Kill(eg_cw_trench)
		
		-- set up a restriction for this lesson
		UI_RestrictBuildingPlacement(mkr_brit_trenchBuild)
		
		Rule_AddInterval(CW_Trench_BuildTrench, 0.5)
		
	end
	
end



function CW_Trench_BuildTrench()

	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		if Misc_IsSGroupSelected(sg_cw_tommies, ANY) then
			
			Player_SetEntityProductionAvailability(player5, EBP.CW.SLITTRENCH_NEST, ITEM_DEFAULT)
			Player_SetConstructionMenuAvailability(player5, "tp_construction_commonwealth_infantry", ITEM_DEFAULT)
			Tutorial_SafetySetResources(cost.cw.slittrench)
			
			Util_StartIntel(CW_EVENTS.ConstructionButton)
			
			if flashid_trench1 == nil then
				flashid_trench1 = Util_AddConstructionMenuFlash("tp_construction_commonwealth_infantry", false, 409727, BT_UI_Strong_CommandBtn)
			end
			
			Rule_AddInterval(CW_Trench_BuildMenuSelected, 0.5)
			UI_SetBuildingPlacementCallback(CW_Trench_PlacingMG)
			
		else
			
			-- select tommies
			Util_StartIntel(CW_EVENTS.SelectTommies)
			
			Camera_MoveToSGroup(sg_cw_tommies, true, SLOW_CAMERA_PANNING)
			
			OBJ_CW_BUILDTRENCH.PingID = Objective_AddUIElements(OBJ_CW_BUILDTRENCH, sg_cw_tommies, true, 409733, true)
			Rule_AddInterval(CW_Trench_TommiesSelected, 0.5)
			
		end
		
	end
	
end


function CW_Trench_TommiesSelected()

	if Misc_IsSGroupSelected(sg_cw_tommies, ANY) then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Player_SetEntityProductionAvailability(player5, EBP.CW.SLITTRENCH_NEST, ITEM_DEFAULT)
		Player_SetConstructionMenuAvailability(player5, "tp_construction_commonwealth_infantry", ITEM_DEFAULT)
		Tutorial_SafetySetResources(cost.cw.slittrench)
		
		Util_StartIntel(CW_EVENTS.ConstructionButton)
		
		if flashid_trench1 == nil then
			flashid_trench1 = Util_AddConstructionMenuFlash("tp_construction_commonwealth_infantry", false, 409727, BT_UI_Strong_CommandBtn)
		end
		
		Objective_RemoveUIElements(OBJ_CW_BUILDTRENCH, OBJ_CW_BUILDTRENCH.PingID)
		
		Rule_AddInterval(CW_Trench_BuildMenuSelected, 0.5)
		UI_SetBuildingPlacementCallback(CW_Trench_PlacingMG)
		
	end
	
end


function CW_Trench_Deselected()

	if Misc_IsSGroupSelected(sg_cw_tommies, ANY) == false then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Objective_RemoveUIElements(OBJ_CW_BUILDTRENCH, OBJ_CW_BUILDTRENCH.PingID)
		UI_ClearBuildingPlacementCallback()
		EGroup_DeSpawn(eg_cw_slittrenchfacing)			-- hide rotation arrow
		
		Util_StartIntel(EVENTS.BritTommiesReSelect)
		
		OBJ_CW_BUILDTRENCH.PingID = Objective_AddUIElements(OBJ_CW_BUILDTRENCH, sg_cw_tommies, true, 409733, true)
		Rule_AddInterval(CW_Trench_TommiesSelected, 0.5)
		
	end

end


-- build menu selected, now flash the AT gun button
function CW_Trench_BuildMenuSelected()

	if _ConstructionMenuStatus == "tp_construction_commonwealth_infantry" then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		if flashid_trench2 == nil then
			flashid_trench2 = Util_AddConstructionFlash(EBP.CW.SLITTRENCH_NEST, false, 409727, BT_UI_Strong_CommandBtn)
		end
		
		Util_StartIntel(CW_EVENTS.TrenchButton)
		
		Rule_AddInterval(CW_Trench_Deselected, 0.5)
		
	elseif Misc_IsSGroupSelected(sg_cw_tommies, ANY) == false then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		OBJ_CW_BUILDTRENCH.PingID = Objective_AddUIElements(OBJ_CW_BUILDTRENCH, sg_cw_tommies, true, 409733, true)
		Util_StartIntel(EVENTS.BritTommiesReSelect)
		
		Rule_AddInterval(CW_Trench_TommiesSelected, 0.5)
		
	end

end


-- callback function deals with stages for building the AT gun
function CW_Trench_PlacingMG(ebp, phase)
	
	if ebp == EBP.CW.SLITTRENCH_NEST then
		
		-- placing
		if phase == CP_Selected then
			
			Game_SkipEvent()
			
			Util_StartIntel(CW_EVENTS.TrenchLocation)
			
			Camera_MoveToPosition(mkr_brit_trenchBuild, true, SLOW_CAMERA_PANNING)
			
			OBJ_CW_BUILDTRENCH.PingID = Objective_AddUIElements(OBJ_CW_BUILDTRENCH, mkr_brit_trenchBuild, true, 409728, true)
			
		-- rotating
		elseif phase == CP_StartedPlacing then
			
			Game_SkipEvent()
			
			Util_StartIntel(CW_EVENTS.TrenchFacing)
			
			Objective_RemoveUIElements(OBJ_CW_BUILDTRENCH, OBJ_CW_BUILDTRENCH.PingID)
			OBJ_CW_BUILDTRENCH.PingID = Objective_AddUIElements(OBJ_CW_BUILDTRENCH,  mkr_brit_trenchFacing, false, 409729, false)
			
			EGroup_ReSpawn(eg_cw_slittrenchfacing)			-- show rotation arrow
			
		-- placed
		elseif phase == CP_FinishedPlacing then
			
			UI_ClearBuildingPlacementCallback()
			UI_UnrestrictBuildingPlacement()
			
			Game_SkipEvent()
			
			Player_SetConstructionMenuAvailability(player5, "tp_construction_commonwealth_infantry", ITEM_LOCKED)
			
			Util_StartIntel(CW_EVENTS.TrenchInfo)
			
			if flashid_trench1 ~= nil then
				Util_StopFlashing(flashid_trench1)
			end
			if flashid_trench2 ~= nil then
				Util_StopFlashing(flashid_trench2)
			end
			
			flashid_trench1 = nil
			flashid_trench2 = nil
			
			Objective_RemoveUIElements(OBJ_CW_BUILDTRENCH, OBJ_CW_BUILDTRENCH.PingID)
			
			EGroup_DeSpawn(eg_cw_slittrenchfacing)			-- hide rotation arrow
			
			Rule_RemoveIfExist(CW_Trench_Deselected)
			Rule_RemoveIfExist(CW_Trench_BuildMenuSelected)
			Rule_AddInterval(CW_Trench_TrenchBuilt, 1)
			Rule_AddOneShot(CW_Trench_LockoutOption, 1)
			
		end
		
	end		
	
end


function CW_Trench_LockoutOption()
	Player_SetEntityProductionAvailability(player5, EBP.CW.SLITTRENCH_NEST, ITEM_LOCKED)
end	


function CW_Trench_StartedRebuilding()
	
	if SGroup_IsConstructingBuilding(sg_cw_tommies, ANY) then
		Rule_RemoveMe()
		Objective_RemoveUIElements(OBJ_CW_BUILDTRENCH, OBJ_CW_BUILDTRENCH.PingID)
		Rule_AddInterval(CW_Trench_TrenchBuilt, 1)
	end
	
end


function CW_Trench_TrenchBuilt()

	EGroup_Clear(eg_cw_trench)
	World_GetNeutralEntitiesNearMarker(eg_cw_trench, mkr_brit_trenchBuild)
	EGroup_Filter(eg_cw_trench, EBP.CW.SLITTRENCH_NEST, FILTER_KEEP)
	EGroup_FilterUnderConstruction(eg_cw_trench, FILTER_REMOVE)

	if EGroup_Count(eg_cw_trench) >= 1 then
		
		Rule_RemoveMe()
		Game_SkipAllEvents(false)
		
		Modify_WeaponRange(eg_cw_mgNest, "hardpoint_01", 3)		-- make it shoot farther
		
		Player_SetCommandAvailability(player5, SCMD_Load, ITEM_DEFAULT)
		Util_StartIntel(CW_EVENTS.TrenchDone)
		
		Rule_AddIntervalEx(CW_Trench_MGAttack, 6, 5) -- 5 possible generations of attackers
		
		Rule_AddOneShot(CW_Trench_CreateMortar, 10)
		
	elseif SGroup_IsConstructingBuilding(sg_cw_tommies, ANY) == false then
		
		Rule_RemoveMe()
		
		Player_GetAll(player5)
		EGroup_Filter(eg_allentities, EBP.CW.SLITTRENCH_NEST, FILTER_KEEP)
		
		if EGroup_Count(eg_allentities) >= 1 then
			OBJ_CW_BUILDTRENCH.PingID = Objective_AddUIElements(OBJ_CW_BUILDTRENCH, eg_allentities, false, 409737, true)
			Rule_AddInterval(CW_Trench_StartedRebuilding, 0.5)
		else
			Rule_AddInterval(CW_Trench_BeginLesson, 0.5)
		end
		
	end
	
end



-- start the enemies coming
function CW_Trench_MGAttack()

	-- create the Germans from ONLY ONE SIDE
	local v = CW.mgAttack[2] 
	if SGroup_IsEmpty(v.sgroup) == false and SGroup_TotalMembersCount(v.sgroup) < 4 then
		Cmd_Retreat(v.sgroup, Marker_GetPosition(v.src), v.src)
	elseif SGroup_Count(v.sgroup) < 3 then
		Util_CreateSquads(player2, v.sgroup, v.sbp, v.src, v.dest, 1)
		Modify_Vulnerability(v.sgroup, 2)
		SGroup_AddGroup(sg_cw_axisAll, v.sgroup)
		Cmd_Move(v.sgroup, v.dest, NO_QUEUE, NIL_DELETE, NIL_FACE, NIL_OFFSET, NIL_DIST, 10)
	end

end


-- create the mortar guys
function CW_Trench_CreateMortar()

	Util_CreateSquads(player2, sg_cw_axisMortar, SBP.AXIS.MORTAR, mkr_brit_axisMortar)
	Util_CreateSquads(player2, sg_cw_axisMortar, SBP.AXIS.GRENADIER, mkr_brit_axisMortarDefenders)
	Modify_Vulnerability(sg_cw_axisMortar, 1.15)
	SGroup_SetInvulnerable(sg_cw_axisMortar, true)

	Modify_ReceivedDamage(sg_cw_tommies, .2)
	Modify_ReceivedDamage(sg_cw_lt, .2)
	
	Rule_AddDelayedInterval(CW_Trench_Mortar_Track, 12, 1)

end

-- start the enemies coming
function CW_Trench_Mortar_Track()

	if SGroup_IsUnderAttack(sg_cw_axisMortar, false, 10) or SGroup_IsDoingAttack(sg_cw_axisMortar, false, 10) then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Tutorial_RevealPos("Commonwealth", Marker_GetPosition(mkr_brit_axisMortar), 10)
		Camera_MoveToPosition(SGroup_GetPosition_EVEN_IF_EMPTY(sg_cw_axisMortar), true, SLOW_CAMERA_PANNING)
		
		-- play info
		Util_StartIntel(CW_EVENTS.TrenchMortar1)
		Util_StartIntel(CW_EVENTS.TrenchMortar2)
		
		Objective_UpdateText(OBJ_CW_BUILDTRENCH, 409700, 409701, false)
		
		Rule_AddInterval(CW_Trench_Garrison, 1)
		
	end

end

-- start the enemies coming
function CW_Trench_Garrison()

	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		
		Camera_MoveToEGroup(eg_cw_trench, true, SLOW_CAMERA_PANNING)
		
		-- play info
		Util_StartIntel(CW_EVENTS.TrenchGetIn)
		
		Rule_AddInterval(CW_Trench_Done, 1)
		
	end

end


function CW_Trench_Done()

	if Player_OwnsEGroup(player5, eg_cw_trench) then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		-- complete the objective
		Objective_Complete(OBJ_CW_BUILDTRENCH, false)
		-- UI_StopFlashing(flashid_infantry_paradrop)
		-- HintPoint_RemoveAll()
		-- FOW_UnRevealMarker(mkr_infantry_revealparadrop)
		
		-- camera changes
		-- Camera_SetInputEnabled(true)
		-- Camera_ClampToMarker(camera_infantry_a)
		
		Util_StartIntel(CW_EVENTS.TrenchGarrisoned)
		
		-- next lesson
		Rule_AddInterval(CW_Glider_BeginLesson, 0.5)
		
	end
	
end



--------------------------------------------------------------------------------
-- Lesson: Landing Gliders
--------------------------------------------------------------------------------
function CW_Glider_BeginLesson()
	
	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		
		-- lock camera input
		--Camera_MoveToSGroup(sg_cw_tommies, true, SLOW_CAMERA_PANNING)
		Camera_SetInputEnabled(false)
		
		-- give the objective
		Objective_Start(OBJ_CW_GLIDER, false)
		
		for k, this in pairs(CW.gliderlockoutzones) do 
			Player_AddAbilityLockoutZone(player5, ABILITY.SP.GLIDER_COMMANDOS, this)
		end
		CW.glider = {}
		
		-- move camera
		Camera_ClampToMarker(mkr_brit_cam5)
		Camera_MoveToPosition(mkr_brit_axisMortar, true, SLOW_CAMERA_PANNING)
		
		-- give the introductory talk
		Util_StartIntel(CW_EVENTS.KillMortar)
		
		Rule_AddInterval(CW_Glider_CallGlider, 0.5)
		
	end
	
end



function CW_Glider_CallGlider()

	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		-- select tommies
		Util_StartIntel(CW_EVENTS.GliderButton)
		
		-- popcap
		Player_SetPopCapOverride(player5, 20)
		
		Player_SetAbilityAvailability(player5, ABILITY.SP.GLIDER_COMMANDOS, ITEM_UNLOCKED)
		Tutorial_SafetySetResources(cost.cw.glider)
		
		Util_AddAbilityFlash(ABILITY.SP.GLIDER_COMMANDOS, true, 409730, BT_UI_Strong_AbilityBtn)
		
		Rule_AddPlayerEvent(CW_Glider_Called, player5, GE_AbilityExecuted)
		UI_SetModalAbilityPhaseCallback(CW_Glider_LandingZone)
		
	end
	
end


-- callback function deals with stages for building the AT gun
function CW_Glider_LandingZone(ability, phase)
	
	if ability == ABILITY.SP.GLIDER_COMMANDOS then
		
		-- placing
		if phase == MAP_Placing then
			
			Game_SkipEvent()
			
			Camera_MoveToPosition(mkr_cw_glider2, true, SLOW_CAMERA_PANNING)
			
			Util_StartIntel(CW_EVENTS.GliderPosition)
			
			OBJ_CW_GLIDER.PingID = Objective_AddUIElements(OBJ_CW_GLIDER, mkr_cw_glider1, true, 409731, true)
			
		-- facing
		elseif phase == MAP_Facing then
			
			Game_SkipEvent()
			
			Util_StartIntel(CW_EVENTS.GliderDirection)
			
			Objective_RemoveUIElements(OBJ_CW_GLIDER, OBJ_CW_GLIDER.PingID)
			OBJ_CW_GLIDER.PingID = Objective_AddUIElements(OBJ_CW_GLIDER,  mkr_cw_glider2, false, 409732, false)
			
			EGroup_ReSpawn(eg_cw_gliderfacing)			-- show helper arrow
			
		end
	end		
	
end



function CW_Glider_Called(player, abilityID, target)

	if abilityID == ABILITY.SP.GLIDER_COMMANDOS then	
		
		Rule_RemoveMe()
		Game_SkipEvent()
		
		Rule_RemovePlayerEvent(CW_Glider_Called, player5)
		UI_ClearModalAbilityPhaseCallback()
		
		EGroup_DeSpawn(eg_cw_gliderfacing)			-- hide helper arrow
		
		Objective_RemoveUIElements(OBJ_CW_GLIDER, OBJ_CW_GLIDER.PingID)
		
		-- move camera
		Camera_MoveToPosition(mkr_cw_glider2, true, SLOW_CAMERA_PANNING)
		
		Rule_AddInterval(CW_Glider_CommandosHere, 1)
		
	end

end



function CW_Glider_CommandosHere()
	
	Player_GetAll(player5)
	SGroup_Filter(sg_allsquads, SBP.CW.COMMANDOS, FILTER_KEEP)
	
	-- condition for complete
	if SGroup_Count(sg_allsquads) >= 1 then
		
		-- standard
		Rule_RemoveMe()
		Game_SkipEvent()
		
		SGroup_AddGroup(sg_cw_commandos, sg_allsquads)
		
		Cmd_Move(sg_cw_commandos, Util_GetPositionFromAtoB(SGroup_GetPosition(sg_cw_commandos), SGroup_GetPosition(sg_cw_axisMortar), 0.2))
		
		OBJ_CW_GLIDER.PingID = Objective_AddUIElements(OBJ_CW_GLIDER, sg_cw_axisMortar, true, false, true)
		
		SGroup_SetInvulnerable(sg_cw_commandos, 0.6)
		Objective_UpdateText(OBJ_CW_GLIDER, 409710, 409711, false)
		
		-- mortar squad
		SGroup_SetInvulnerable(sg_cw_axisMortar, false)
		
		-- camera changes
		Camera_SetInputEnabled(true)
		
		Util_StartIntel(CW_EVENTS.CommandosHere)
		Util_StartIntel(CW_EVENTS.CommandosAttack)
		
		Rule_AddInterval(CW_Glider_Done, 1)
	end
end

function CW_Glider_Done()
	
	
	-- condition for complete
	if SGroup_Count(sg_cw_axisMortar) == 0 then
		
		-- standard
		Rule_RemoveMe()
		Game_SkipEvent()
		
		-- complete the objective
		Objective_Complete(OBJ_CW_GLIDER, false)
		
		-- UI_StopFlashing(flashid_infantry_paradrop)
		-- HintPoint_RemoveAll()
		-- FOW_UnRevealMarker(mkr_infantry_revealparadrop)
		
		-- Util_StartIntel(EVENTS.InfantryParatroopersComingIn)
		-- Util_StartIntel(EVENTS.InfantryParatroopersDone)
		-- Player_SetAbilityAvailability(player1, ABILITY.COMMANDER_TREE.ALLIES.RECRUIT_PARADROP, ITEM_DEFAULT)
		
		Util_StartIntel(CW_EVENTS.CommonweathEnd)
		
		-- next lesson
		Rule_AddInterval(CW_TutorialComplete, 0.5)
		
	end
	
end


--------------------------------------------------------------------------------
-- Closing
--------------------------------------------------------------------------------
function CW_TutorialComplete()
	
	if Event_IsAnyRunning() == false then
		
		Rule_RemoveMe()
		
		Util_MissionTitle(409926)
		
		PlayerProfile_SetVar("CommonwealthTutorial_Completed", "true")
		
		Rule_AddOneShot(CW_EndTutorial, 5)
		
	end
	
end




--------------------------------------------------------------------------------
-- End the tutorial and return to the courtyard
--------------------------------------------------------------------------------
function CW_EndTutorial()

	Rule_RemoveAll(0)
	Game_SkipAllEvents(true)
	
	CW_CleanUp()
	Courtyard_Start()

end

function CW_CleanUp()

	HintPoint_RemoveAll()
	SelectionCheck_Stop()
	
	-- stop everything
	Player_GetAll(player5)
	Cmd_Stop(eg_allentities)
	Cmd_Stop(sg_allsquads)
	
	-- FOW
	Tutorial_UnRevealAll("Commonwealth")
	
	Player_SetEntityProductionAvailability(player5, EBP.CW.MG_NEST, ITEM_LOCKED)
	Player_SetEntityProductionAvailability(player5, EBP.CW.SLITTRENCH_NEST, ITEM_LOCKED)
	Player_SetCommandAvailability(player5, SCMD_Load, ITEM_LOCKED)
	Player_SetUpgradeAvailability(player5, UPG.CW.ENABLE_MOBILE_HQ, ITEM_LOCKED)
	Player_SetAbilityAvailability(player5, ABILITY.CW.HQ_SETUP_MAINHQ, ITEM_LOCKED)
	
	-- revert all territories
	-- EGroup_SetStrategicPointNeutral(eg_vp_infantry)
	
	-- remove all the units spawned for this tutorial
	SGroup_DestroyAllSquads(sg_cw_tommies)
	SGroup_DestroyAllSquads(sg_cw_lt)
	SGroup_DestroyAllSquads(sg_cw_commandos)
	SGroup_DestroyAllSquads(sg_cw_hq)
	EGroup_DestroyAllEntities(eg_cw_mgNest)
	EGroup_DestroyAllEntities(eg_cw_trench)
	SGroup_DestroyAllSquads(sg_cw_axisMortar)
	SGroup_DestroyAllSquads(sg_cw_axisAll)
	
	Player_GetAll(player5)
	EGroup_Filter(eg_allentities, {EBP.CW.GLIDER_COMMANDOS}, FILTER_KEEP)
	EGroup_DestroyAllEntities(eg_allentities)
	
	-- remove all the objectives used in this tutorial
	Objective_Show(OBJ_CW_TOMMIES, false)
	Objective_Show(OBJ_CW_LIEUTENANT, false)
	Objective_Show(OBJ_CW_CAPTURE, false)
	Objective_Show(OBJ_CW_MOVEHQ, false)
	Objective_Show(OBJ_CW_BUILDMGNEST, false)
	Objective_Show(OBJ_CW_BUILDTRENCH, false)
	Objective_Show(OBJ_CW_GLIDER, false)

	-- remove any titlecards
	if screen_lieutenantstar ~= nil then
		Misc_ScreenFadeChange(screen_lieutenantstar, 0.0, 0.0, 0.1)
	end
	if screen_lieutenantlink ~= nil then
		Misc_ScreenFadeChange(screen_lieutenantlink, 0.0, 0.0, 0.1)
	end
	
	-- remove lockout zones
	for k, this in pairs(CW.gliderlockoutzones) do 
		Player_RemoveAbilityLockoutZone(player5, ABILITY.SP.GLIDER_COMMANDOS, this)
	end
	for k, this in pairs(CW.deploylockoutzones) do 
		Player_RemoveAbilityLockoutZone(player5, ABILITY.CW.HQ_SETUP_MAINHQ, this)
	end
	
	-- remove any perimeters
	EGroup_DeSpawn(LAYER_brit_show)
	EGroup_ReSpawn(LAYER_brit_hide)
	
end





--------------------------------------------------------------------------------
-- Objectives
--------------------------------------------------------------------------------
function CW_RegisterObjectives()


	OBJ_CW_TOMMIES = {
		SetupUI = function()
		end,
		OnStart = function() 
			Objective_SetAlwaysShowDetails(OBJ_CW_TOMMIES, true, true, true)
		end,
		OnComplete = function()
			Objective_SetAlwaysShowDetails(OBJ_CW_TOMMIES, false, false, false)
		end,
		OnFail = function() end,
		OnSitRep = function() end,
		Title = 409650,
		Description = 409651,
		Type = OT_Primary,
		Icon = IT_P_Default,
	}
	Objective_Register(OBJ_CW_TOMMIES)
	
	OBJ_CW_LIEUTENANT = {
		SetupUI = function()
		end,
		OnStart = function() 
			Objective_SetAlwaysShowDetails(OBJ_CW_LIEUTENANT, true, true, true)
		end,
		OnComplete = function()
			Objective_SetAlwaysShowDetails(OBJ_CW_LIEUTENANT, false, false, false)
		end,
		OnFail = function() end,
		OnSitRep = function() end,
		Title = 409655,
		Description = 409656,
		Type = OT_Primary,
		Icon = IT_P_Default,
	}
	Objective_Register(OBJ_CW_LIEUTENANT)

	OBJ_CW_CAPTURE = {
		SetupUI = function()
		end,
		OnStart = function() 
			Objective_SetAlwaysShowDetails(OBJ_CW_CAPTURE, true, true, true)
		end,
		OnComplete = function()
			Objective_SetAlwaysShowDetails(OBJ_CW_CAPTURE, false, false, false)
		end,
		OnFail = function() end,
		OnSitRep = function() end,
		Title = 409670,
		Description = 409671,
		Type = OT_Primary,
		Icon = IT_P_Capture,
	}
	Objective_Register(OBJ_CW_CAPTURE)


	OBJ_CW_MOVEHQ = {
		SetupUI = function()
		end,
		OnStart = function() 
			Objective_SetAlwaysShowDetails(OBJ_CW_MOVEHQ, true, true, true)
		end,
		OnComplete = function()
			Objective_SetAlwaysShowDetails(OBJ_CW_MOVEHQ, false, false, false)
		end,
		OnFail = function() end,
		OnSitRep = function() end,
		Title = 409675,
		Description = 409676,
		Type = OT_Primary,
		Icon = IT_P_Capture,
	}
	Objective_Register(OBJ_CW_MOVEHQ)

	OBJ_CW_BUILDMGNEST = {
		SetupUI = function()
		end,
		OnStart = function() 
			Objective_SetAlwaysShowDetails(OBJ_CW_BUILDMGNEST, true, true, true)
		end,
		OnComplete = function()
			Objective_SetAlwaysShowDetails(OBJ_CW_BUILDMGNEST, false, false, false)
		end,
		OnFail = function() end,
		OnSitRep = function() end,
		Title = 409685,
		Description = 409686,
		Type = OT_Primary,
		Icon = IT_P_Defend,
	}
	Objective_Register(OBJ_CW_BUILDMGNEST)
	

	OBJ_CW_BUILDTRENCH = {
		SetupUI = function()
		end,
		OnStart = function() 
			Objective_SetAlwaysShowDetails(OBJ_CW_BUILDTRENCH, true, true, true)
		end,
		OnComplete = function()
			Objective_SetAlwaysShowDetails(OBJ_CW_BUILDTRENCH, false, false, false)
		end,
		OnFail = function() end,
		OnSitRep = function() end,
		Title = 409695,
		Description = 409696,
		Type = OT_Primary,
		Icon = IT_P_Defend,
	}
	Objective_Register(OBJ_CW_BUILDTRENCH)

	OBJ_CW_GLIDER = {
		SetupUI = function()
		end,
		OnStart = function() 
			Objective_SetAlwaysShowDetails(OBJ_CW_GLIDER, true, true, true)
		end,
		OnComplete = function()
			Objective_SetAlwaysShowDetails(OBJ_CW_GLIDER, false, false, false)
		end,
		OnFail = function() end,
		OnSitRep = function() end,
		Title = 409705,
		Description = 409706,
		Type = OT_Primary,
		Icon = IT_P_Attack,
	}
	Objective_Register(OBJ_CW_GLIDER)
	
end


--------------------------------------------------------------------------------
-- Utils
--------------------------------------------------------------------------------

-- callback function, keeps the table that states which menu the player is in updated
function _ConstructionMenuCallback(menu, bool)
	if bool == true then
		_ConstructionMenuStatus = menu
	else
		_ConstructionMenuStatus = nil
	end
end
