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

-- Mission Number - Mission Name

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

import("ScarUtil.scar")
import("MissionName.events")
-- custom file for table ID's in addition to the default WB generated file
import("MissionName_IDtables.scar")



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

-- [[ SETUP ]]

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

function OnGameSetup()
	--[[ string numbers should reference dat files
	player1 = Setup_Player(1, "$000000", "Allied Rifle Company", 1)
	player2 = Setup_Player(2, "$000000", "Axis Infantry Company", 2)
	player3 = Setup_Player(3, "$000000", "Allied Rifle Company", 1)
	player4 = Setup_Player(4, "$000000", "allies", 1)
	player4 = Setup_Player(5, "$000000", "allies", 1)
	]]
end

function OnGameRestore()
	player1 = World_GetPlayerAt(1)
	player2 = World_GetPlayerAt(2)
	player3 = World_GetPlayerAt(3)
	player4 = World_GetPlayerAt(4)
	player4 = World_GetPlayerAt(5)
end




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

-- [[ ONINIT ]]

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

function OnInit()
	
	--[[ PRESET GAME STATE ]]
	AutoCinematic(true, 0)
	
	--[[ REGISTER OBJECTIVES ]]
	M99__Objective_Register()
	
	--[[ MOD INITIAL STATS ]]
	M99_Set_StatMods()
	
	--[[ SET RESTRICTIONS ]]
	M99_Restrictions()
	
	--[[ SET INITIAL MOOD ]]
	M99_MOOD()
	
	--[[ MISSION PRESETS ]]
	M99_Mission_Preset()
	
	--[[ GAME START CHECK ]]
	Rule_Add(M99_Mission_Start)

end

Scar_AddInit(OnInit)

function M99_Objective_Register()

	--[[ *** MUST INITIALIZE THE OBJ TABLE BEFORE REGISTERING *** ]]
	Initialize_OBJ_ObjectiveName()
	
	--[[ Registering Objectives ]]
	OBJ_ObjectiveName.ID = Objective_Register(OBJ_ObjectiveName)

end

function M10_Set_StatMods()
	
	--[[ saved as example - deg
	-- shorten vision ranges
	Modify_PlayerSightRadius(player1, 0.75)

	-- mod resource rates
	player1resourcemodifiers = {}
	player1resourcemodifiers[1] = Modify_PlayerResourceRate(player1, RT_Manpower, 2)
	player1resourcemodifiers[2] = Modify_PlayerResourceRate(player1, RT_Munition, 2)
	player1resourcemodifiers[3] = Modify_PlayerResourceRate(player1, RT_Fuel, 1)
	
	]]	
	
end

function M99_Restrictions()

	--[[ UN/RESTRICT UPGRADES 
	Cmd_InstantPlayerUpgrade(player1, UPG.ALLIED_PHASE2)
	]]
	
	--[[ RESOURCES 
	Player_SetResource(player1, RT_Manpower, 600)
	Player_SetResource(player1, RT_Munition, 600)
	Player_SetResource(player1, RT_Fuel, 200)
	]]
	
	--[[ UN/RESTRICT ABILITIES 
	Player_SetAbilityAvailability(player1, ABILITY.ALLIED_THROWGRENADE, ITEM_UNLOCKED)
	Player_SetAbilityAvailability(player1, ABILITY.ALLIED_THROWSATCHELCHARGE, ITEM_REMOVED)
	Player_SetAbilityAvailability(player2, ABILITY.AXIS_THROWGRENADE, ITEM_UNLOCKED)
	]]
	
	--[[ UN/RESTRICT FOW 
	World_EnablePlayerToPlayerFOW(player1, player3, true)
	]]
	
	--[[ UN/RESTRICT POP CAP 
	Player_SetMaxPopulation(player1, CT_Personnel, 100)
	]]
	
	--[[ UN/RESTRICT UI 
	UI_BindingSetEnabled("company_commander", false)
	UI_BindingSetEnabled("squadcap", false)
	]]
	
	--[[ UN/RESTRICT SBPS 
	local sbps = {SBP.ALLIED_SHERMAN, SBP.ALLIED_CROCODILE, SBP.ALLIED_GREYHOUN, SBP.ALLIED_PERSHING }
	for i = 1, table.getn(sbps) do
		local id = Util_GetSquadBlueprintID(sbps[i])
		Player_SetSquadProductionAvailability(player1, id, ITEM_LOCKED)
	end
	]]
end

function M99_MOOD()
	Player_SetDefaultSquadMoodMode(player1, MM_Auto)
	Player_SetDefaultSquadMoodMode(player2, MM_Auto)
	Player_SetDefaultSquadMoodMode(player3, MM_ForceTense)
end





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

-- [[ MISSION Preset ]]

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

function M99_Mission_Preset()

	-- create the temperary starting units for player 1
	--[[ this is an example of what you might do - sgroup and marker must exist
	local sbps = {SBP.ALLIED_OFFICER, SBP.ALLIED_RIFLEMEN, SBP.ALLIED_ENGINEER, SBP.ALLIED_JEEP, SBP.ALLIED_RIFLEMEN}
	for i = 1, 5 do
		Util_CreateSquadsAtMarker(player1, sg_player1[i], sbps[i], marker1, 1)
	end
	]]
end





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

-- [[ MISSION START ]]

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

function M99_Mission_Start()
	-- out of cinematic
	AutoCinematic(false, 2)
	
	--[[ mission title should be found in the dat file
	Util_MissionTitle(000000) 
	]]
	
	-- delay first objective
	Rule_AddOneShot(M99_DelayOBJ_ObjectiveName, 10)
	
	Rule_RemoveMe()
end


function M99_DelayOBJ_ObjectiveName()
	Objective_Start(OBJ_ObjectiveName)
end
















-------------------------------------------------------------------------
-- [[ Objective Name ]]
-------------------------------------------------------------------------
-- Describe what the player is charged to do
-- give a brief description of win conditions and loose conditions if any 
-- also describe the kind of challenges the player will face
-------------------------------------------------------------------------
function Initialize_OBJ_ObjectiveName()

	OBJ_ObjectiveName = {
		
		OnStart = function()
			
			-- announce the goal
			Util_StartIntel(EVENTS.OBJ_NAME_START)
			
			-- add completion check
			Rule_AddInterval(OBJ_NAME_WinConditionCheck, 7)
			
			-- delay next objective
			Rule_AddOneShot(M99_DelayOBJ_ObjectiveName2, 15)
			
			-- start related action
			OBJ_NAME_ActionKickoff()
			
		end,
		
		OnComplete = function()
			
			-- tell the player they did good
			Util_StartIntel(EVENTS.OBJ_NAME_COMPLETE)
			
		end,
		
		OnFail = function()
			
			
		end,
		
		Title 			= 000000,
		Description 	= 000000,
		isPrimary 		= true,
		ID 				= nil, -- don't worry about this nil
		BlipPos			= { marker1 },
		BlipID			= {	nil }, -- don't worry about this nil
		Pointers 		= { marker1 },
		LabelPoints 	= { marker1 },
		LabelNames 		= { 000000 }, 		
		Pings 			= { marker1 },
		FOW 			= { marker1 },
		
	}

end

-------------------------------------------------------------------------
-- [[ Objective Name - Functions ]]
-------------------------------------------------------------------------

function OBJ_NAME_WinConditionCheck()
	
	-- g_axisAllDead and g_axisAllRetreated are temp/uncreated yet
	-- one of them must be set to true before the player can win the objective
	if ( g_axisAllDead or g_axisAllRetreated ) and Player_AreSquadsNearMarker(player1, mkr_town_sqaure) then
		
		-- the player has just won
		Objective_Complete(OBJ_ObjectiveName)
		
		Rule_RemoveMe()
		
	end
	
end


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


-- start the related action
function OBJ_NAME_ActionKickoff()
	

	
	--[[ Create Defenses
	
	The following is an example of creating a dynamic defense force - deg
	
	-- Defense 1 - the residential section
	g_obj_tt_spawnbank = 45
	g_obj_tt_debt = 1
	t_obj_tt_def1 = {
		-- the HQ buildings near the guns
		src = { eg_axres_src[1], eg_axres_src[1], eg_axres_src[1], eg_axres_src[1]},
		-- the markers
		pos = {mkr_axdef_res[1], mkr_axdef_ind[2], mkr_axdef_ind[5], mkr_axdef_res[17]},
		-- the target territory markers
		ter = {eg_ter_approach[1], eg_ter_approach[2], eg_ter_approach[5], eg_ter_approach[4]},
		-- the fallback markers
		pos2 = {mkr_axdef_res[3], mkr_axdef_res[3], mkr_axdef_res[18], mkr_axdef_res[18]},
		-- the target territory markers
		fallback = {false, false, false, false},
	}
	Rule_AddInterval(OBJ_TT_Defense1, 1)
	Rule_AddInterval(OBJ_TT_Defense_Manager1, 10)
	
	]]
end






-------------------------------------------------------------------------
--[[ Defense 1 - Around the Town ]]

-- guards created in the preset, focus is on the area 'around' the town center
-- once the presets are dead, the guards will respawn to more retreated positions
function OBJ_TT_Defense1()
	-- easy way to limit the flow of reinforcements
	if g_obj_tt_spawnbank >= g_obj_tt_debt then
		for i = 1, 4 do 
			if SGroup_IsEmpty(sg_axdef1[i]) and EGroup_IsEmpty(t_obj_tt_def1.src[i]) == false then
				Util_CreateSquadsAndGarrisonExit(player2, sg_axdef1[i], SBP.AXIS_VOLKSGRENADIER, t_obj_tt_def1.src[i], 1, t_obj_tt_def1.pos[i], true, World_GetRand(3, 4))
				SGroup_AddGroup(sg_axdef1_all, sg_axdef1[i])
				-- 7 secs added whenever someone is spawned
				g_obj_tt_spawnbank = g_obj_tt_spawnbank-18
				g_obj_tt_debt = g_obj_tt_debt*2
				break
			end
		end
	end
	
	if g_obj_tt_spawnbank < 45 then
		g_obj_tt_spawnbank = g_obj_tt_spawnbank+1
	end
	if g_obj_tt_debt > 0 then
		g_obj_tt_debt = g_obj_tt_debt-1
	end

end

-- first step to making the axis defenders 'around' the town retreat
function OBJ_TT_Defense_Manager1()

	for i = 1, table.getn(t_obj_tt_def1.fallback) do 
		if t_obj_tt_def1.fallback[i] == false and EGroup_IsCapturedByPlayer(t_obj_tt_def1.ter[i], player1, false) then
			-- switch the rally points
			t_obj_tt_def1.pos[i] = t_obj_tt_def1.pos2[i]
			t_obj_tt_def1.fallback[i] = true
		end
		
		-- quit condition
		local count = 0
		if t_obj_tt_def1.fallback[i] == true then
			count = count+1
		end
		if count == table.getn(t_obj_tt_def1.fallback) then
			-- trigger the next rule
			
			-- remove this one
			Rule_RemoveMe()
		end
	end

end



















--[[ Random Util Stuff ]]
-------------------------------------------------------------------------

-- returns a random offset, same as Marker_GetRandomPosition
-- but for a position value
function Util_GetRandOffset(pos, dist)
	if (scartype(pos) == ST_MARKER) then
		pos = Marker_GetPosition(pos)
	end
	
	local newpos = pos
	newpos.x = World_GetRand(dist*-1, dist)
	newpos.z = World_GetRand(dist*-1, dist)
	return newpos
end

-- when I don't feel like making up my mind
-- returns a random SBP
function Util_GetRandSBP_AxisInf()
	local blue = {SBP.AXIS_GRENADIER, SBP.AXIS_STORMTROOPER, SBP.AXIS_VOLKSGRENADIER}
	return blue[World_GetRand(1, 3)]
end


-- when I don't feel like making up my mind
-- returns a random SBP
function Util_GetRandSBP_AxisArt()
	local blue = {SBP.AXIS_PANZER_FLAK, SBP.AXIS_STORMTROOPER, SBP.AXIS_ARMOURCAR}
	return blue[World_GetRand(1, 3)]
end

function M99_CreateInfantryResponse_Med(group, pos)
	-- 1 squad of Stormtroopers
	Util_CreateSquadsAtMarker(player2, group, SBP.AXIS_STORMTROOPER, pos, 1)
	-- 2 squads of Grenadiers
	Util_CreateSquadsAtMarker(player2, group, SBP.AXIS_GRENADIER, pos, 1)
	Util_CreateSquadsAtMarker(player2, group, SBP.AXIS_GRENADIER_MG42_SP, pos, 1)
end

