------------------------------------------------------------------------------------
-- SetupUtil.scar
-- (c) 2003 Relic Entertainment Inc.
--
-- Description:
--		Contains helper functions for OnGameSetup function.  OnGameSetup
--	is used for initializing the player settings (name, race, team etc)
--
-- Usage:
--		Currently, the only function you need is Setup_Player( ).  This function
--	is used to set a players settings all in one function.  The function returns
--	you the playersId so you can store it in a global variable.
--
-- Example:
--	function OnGameSetup( )
--		g_player1 = Setup_Player(0, "PlayerDisplayName", "Allies", 1) 
--		g_player2 = Setup_Player(1, "$20000", "Axis", 2)
--		g_player3 = Setup_Player(2, "$20001", "Axis", 2)
--	end
--
--		In the above example, g_player2 and g_player3 will be on the same team.
--	The Setup_Player function takes care of setting all player relationships.
--


--? @group Setup

--? @shortdesc Initializes the all of the players settings in one function and returns the playerID.
--? @extdesc 
--? In most cases you will call this function from OnGameSetup and store the returned playerId in  a global variable.  The player index should be in the range [1 , (numPlayers)].\n\n
--? Valid player races: "Allied Airborne Company", "Allied Assault Company", "Allied Rifle Company", "Allied Tank Company", "Axis Armored Company","Axis Heavy Tank Company", "Axis Infantry Company","Axis Pioneer Company"\n\n
--? Example:\n
--?	function OnGameSetup( )\n
--?	\tg_player1 = Setup_Player(0, "PlayerDisplayName", "Allied Assault Company", 1)\n
--? \t-- g_player2 and g_player3 will be on the same team (allies)\n
--? \tg_player2 = Setup_Player(1, "$20000", "Axis Infantry Company", 2)\n
--? \tg_player3 = Setup_Player(2, "$20001", "Axis Infantry Company", 2)\n
--? --? The team ID starts from one!!
--?	end
--? @args Integer playerIndex, LocString playerName, String playerRace, Integer team
--? @result PlayerID
function Setup_Player(playerIndex, playerName, playerRace, team)

	if (scartype(team) ~= ST_NUMBER or team < 1 ) then
		fatal( "Setup_Player: team ID has to be a number starting from 1")
	end
	
	-- accept raw strings for now... convert them to LocString
	if (scartype(playerName) == ST_STRING) then
		playerName = LOC(playerName)
	end
	
	-- get player handle
	local playerId = World_GetPlayerAt( playerIndex )
	
	-- set player name and race
	Setup_SetPlayerName(playerId, playerName)
	Setup_SetPlayerRace(playerId, World_GetRaceIndex(playerRace))
	
	-- start the index from 0
	if ( team ~= TEAM_NEUTRAL ) then
		team = team-1
	end

	-- mod
	Setup_SetPlayerTeam(playerId, team)
	
	return playerId
end

--? @group Setup
--? @shortdesc Sets up difficulty (currently: modifies entity health, and sets AI difficulty). You should pass in Game_GetSPDifficulty() as the difficulty.
--? @args PlayerID player[, Integer difficulty]
function Setup_Difficulty(player, dif)

	if dif == nil then
		dif = Game_GetSPDifficulty()
	end
	
	-- these tables define what effect difficulty has on the game. if you add anything in here, update the Scardoc comment
	-- above so people know what this function is doing
	local AlliesDifficultyTable =
	{
		EBP_prefix = "EBPs/races/allies/",
		INF = {
			EBP =
			{
				"soldiers/airborne_infantry.lua",
				"soldiers/engineer.lua",
				"soldiers/infantry_rifleman.lua",
				"soldiers/infantry_rifleman_sergeant.lua",
				"soldiers/officer_lieutenant.lua",
				"soldiers/ranger.lua",
				"soldiers/sniper.lua",
				"soldiers/weapon_crew.lua",
				"soldiers/airborne_sergeant.lua",
				"soldiers/infantry_rifleman_sergeant.lua",
				"soldiers/mortar_gunner.lua",
				"soldiers/repair_engineer.lua",
				"soldiers/sniper_airborne.lua",
				"soldiers/weapon_crew_howitzer.lua",
				
				--[[ CUSTOM FOR MISSION 01 ]]
				"soldiers/infantry_rifleman_sp_m01.lua",
				"soldiers/engineer_sp_m01.lua",
			},
			health_modifiers =
			{
				3,
				2,
				1,
				1,
			},
		},
		VEH = {
			EBP =
			{
				"vehicles/deuce_and_half_cckw_truck.lua",
				"vehicles/m3_halftrack.lua",
				"vehicles/m4_crocodile.lua",
				"vehicles/m4_sherman.lua",
				"vehicles/m4_sherman_calliope.lua",
				"vehicles/m8_greyhound.lua",
				"vehicles/m10_tank_destroyer.lua",
				"vehicles/m26_pershing.lua",
			},
			health_modifiers =
			{
				2,
				1.5,
				1,
				.8,
			},
		}
	}

	local AxisDifficultyTable =
	{
		EBP_prefix = "EBPs/races/axis/",
		INF = {
			EBP =
			{
				"soldiers/grenadier.lua",
				"soldiers/pioneer.lua",
				"soldiers/sniper.lua",
				"soldiers/volksgrenadier.lua",
				"soldiers/stormtrooper_grenadier.lua",
				"soldiers/officer_leutnant.lua",
				"soldiers/weapon_crew.lua",
				"soldiers/knights_cross_holder.lua",
				"soldiers/machine_gunner.lua",
				"soldiers/mortar_gunner.lua",
				"soldiers/repair_pioneer.lua",
				"soldiers/weapon_crew_88.lua",
				
				--[[ CUSTOM FOR MISSION 01 ]]
				"soldiers/machine_gunner_sp_m01_noautotarget",
			},
			health_modifiers =
			{
				.6,
				.8,
				1,
				1.5,
			},
		},
		VEH = {
			EBP =
			{
				"vehicles/goliath_remote_controlled_bomb.lua",
				"vehicles/motorcycle.lua",
				"vehicles/ostwind_flak_panzer.lua",
				"vehicles/panther.lua",
				"vehicles/panzer_iv.lua",
				"vehicles/sdkfz234_armoured_car.lua",
				"vehicles/sdkfz_251_halftrack.lua",
				"vehicles/stug_iv.lua",
				"vehicles/tiger.lua",
				"vehicles/tiger_ace.lua",
				"vehicles/tiger_spg_ace.lua",
			},
			health_modifiers =
			{
				.70,  --75
				.80,  -- 85
				1,   --1
				1,  -- 1.2
			},
		},
	}
	
	--local playerCount = World_GetPlayerCount()
	--for i = 1, playerCount do
	--
	--	local player = World_GetPlayerAt(i)
		
		-- if it's an AI player, set his difficulty to match the game difficulty
		if AI_IsAIPlayer(player) then
			AI_SetDifficulty(player, dif)
		end
		
		local difficultyTable
		local race = Player_GetRace(player)
		if race == 0 then
			difficultyTable = AlliesDifficultyTable
		elseif race == 1 then
			difficultyTable = AxisDifficultyTable
		else
			error("Setup_Difficulty: invalid race for player " .. i)
		end
		
		--[[ Infantry ]]
		local healthModifier = difficultyTable.INF.health_modifiers[dif + 1]
		if healthModifier == nil then
			error("Setup_Difficulty: invalid difficulty ( " .. dif .. " )")
		end
		
		-- modify health for all specified entities
		for j = 1, table.getn(difficultyTable.INF.EBP) do
			
			local filename = difficultyTable.EBP_prefix .. difficultyTable.INF.EBP[j]
			local ebp = Util_GetEntityBlueprintID(filename)
			
			local modifier = Modifier_Create(MAT_EntityType, "modifiers\\health_maximum_modifier.lua", MUT_Multiplication, false, healthModifier, ebp)
			Modifier_ApplyToPlayer(modifier, player)
			
		end
		
		--[[ Vehicles ]]
		local healthModifier = difficultyTable.VEH.health_modifiers[dif + 1]
		if healthModifier == nil then
			error("Setup_Difficulty: invalid difficulty ( " .. dif .. " )")
		end
		
		-- modify health for all specified entities
		for j = 1, table.getn(difficultyTable.VEH.EBP) do
			
			local filename = difficultyTable.EBP_prefix .. difficultyTable.VEH.EBP[j]
			local ebp = Util_GetEntityBlueprintID(filename)
			
			local modifier = Modifier_Create(MAT_EntityType, "modifiers\\health_maximum_modifier.lua", MUT_Multiplication, false, healthModifier, ebp)
			Modifier_ApplyToPlayer(modifier, player)
			
		end
		
	--end
	
end

--? @group Util
--? @shortdesc Takes in a table and chooses the right variable for the difficulty setting. 1-4 elements.
--? @args Table difficultyVariables
--? @result Variable
function Util_DifVar(tablename)

	if scartype(tablename) ~= ST_TABLE then
		fatal( "Util_DifVar: invalid table")
	end
	local dif = Game_GetSPDifficulty()
	local count = table.getn(tablename)
	if count == 0 then
		fatal( "Util_DifVar: table.getn cannot equal zero")
	else
		if (dif+1) > count then
			return tablename[count]
		else
			return tablename[dif+1]
		end
	end
	
end

-- private impl

-- is called by the game. clears stuff in preparation for starting a counterattack mission.
function __SetupCounterattack()

	print("__SetupCounterattack")
	Rule_RemoveAll()
	Game_SkipEvent()
	
	Player_ClearAvailabilities(player1)
	Player_RevertChosenCommanderTree(player1)
	
end
