
import("ScarUtil.scar")

-- you create this file
--import( "battle_info.scar" )

-- example list of battles to be fought - this would could be output by the BattleSimulator UI
--[[battles = 
{
	-- marker_name, squad1, squad2
	{"no_cover", "no_cover_30","SBPs/Races/Axis/soldiers/pioneer_squad.lua","SBPs/Races/Axis/soldiers/pioneer_squad.lua"},
	{"no_cover", "no_cover_15","SBPs/Races/Axis/soldiers/pioneer_squad.lua","SBPs/Races/Axis/soldiers/pioneer_squad.lua"},
}]]

-------------------
-- constants

-- 2 minute max battle time (could be added to battle info)
kMaxBattleTime = 60*2

-------------------
-- global vars

-- save the current battle number
g_current_battle_number = 1
g_current_battle_iteration = 1
-- the time the current battle started at
g_battle_start_time = 0
--
-- e.g. "BattleScripts/dobattle.lua"
function BATTLE_Start( battescript )

	FOW_RevealAll()
	
	dofile( battescript )

	--header
	local result  = "marker,unit1,start1,left1,eff1,unit2,start2,left2,eff2,time,"
	BattleSim_WriteOneResult(result)

	BATTLE_Loop()

end
	--trim the unit name off its path name
function TrimPath(path)
	local temp = path
	
	while true do
		local pos = string.find(temp, "\\")
		if pos == nil then
			break
		end
		temp = string.sub(temp, pos+1)
	end
	
	return temp
end

--get unit number of a group
function UnitsOfGroup(group)
	battleSquadCount = 0
    local countSquad = function( sgroupid, itemindex, squadID )
        battleSquadCount = battleSquadCount + Squad_Count( squadID )
    end

    SGroup_ForEach( group, countSquad )
	return battleSquadCount
end

-- start a new battle
function BATTLE_Loop()

	-- only advance to next battle if there are more battles to be fought
	if (g_current_battle_number > table.getn( battles ) ) then
		-- could exit app here
		BattleSim_DumpResults()
		return 0
	end
	
	-- get the info for the next battle
	local next_battle = battles[g_current_battle_number]

	-- get the markers needed for this battle
	local markerA = Marker_FromName(next_battle.marker.."_a", "basic_marker")
	local markerB = Marker_FromName(next_battle.marker.."_b", "basic_marker")
	local marker0 = Marker_FromName(next_battle.center.."_0", "basic_marker")

	-- need to work cost into squad creation - need to expose more functions
	
	Rule_Add( BATTLE_IsDone )
		
	-- spawn squads at markers
	g_groupA = SGroup_CreateIfNotFound("groupA")
	g_groupB = SGroup_CreateIfNotFound("groupB")
		
	local bpID1 = Util_GetEntityBlueprintID(next_battle.unit1)
	local bpID2 = Util_GetEntityBlueprintID(next_battle.unit2)
	
	Util_CreateSquadsAtMarker( World_GetPlayerAt(1), g_groupA, bpID1, markerA, next_battle.size1 )
	Util_CreateSquadsAtMarker( World_GetPlayerAt(2), g_groupB, bpID2, markerB, next_battle.size2 )
	g_startSize1 = UnitsOfGroup( g_groupA )
	g_startSize2 = UnitsOfGroup( g_groupB )
	SGroup_SnapFaceEachOther(g_groupA, g_groupB)
	-- give orders to each group to move to a pre-specified location
	Cmd_AttackMoveMarker( g_groupA, marker0 )
	Cmd_AttackMoveMarker( g_groupB, marker0 )
	
	-- get the time that this battle started
	g_battle_start_time = World_GetGameTime()
	
	-- start the timer to determine when the battle is over
	Scar_AddInit( BATTLE_IsDone )

	-- continue
	return 1
end
--create one battle result string
function CreateResults()

	--record results
	local next_battle = battles[g_current_battle_number]
	local result  = next_battle.marker..","
	local timeElapsed = World_GetGameTime() - g_battle_start_time
	local size1 = 0.0 + UnitsOfGroup( g_groupA )
	local eff1 = 0.0
	if g_startSize1 > 0 then
		eff1 = size1 / g_startSize1;
	end

	local size2 = 0.0 + UnitsOfGroup( g_groupB )
	local eff2 = 0.0
	if g_startSize2 > 0 then
		eff2 = size2 / g_startSize2;
	end
	local u1 = TrimPath(next_battle.unit1)
	local u2 = TrimPath(next_battle.unit2)
	result = result..u1..","
	result = result..g_startSize1..","
	result = result..size1..","
	result = result..eff1..","
	result = result..u2..","
	result = result..g_startSize2..","
	result = result..size2..","
	result = result..eff2..","
	result = result..timeElapsed..","
	
	BattleSim_WriteOneResult(result)

end

function BATTLE_Over()

	CreateResults()
	-- delete the timer
	Rule_Remove( BATTLE_IsDone )
		
	-- WRITE ALL RECORDED INFORMATION TO A FILE, OR APPEND IT TO A LARGE FILE

	-- destroy the two squad groups
	--[[if(SGroup_Count( g_groupA ) > 0 ) then
		Squad_SetHealth(g_squadA, 0.0)
	end]]
	
--~ 	if (SGroup_Exists( "groupA" ) ) then
--~ 		SGroup_DestroyAllSquads(g_groupA)
--~ 		SGroup_Destroy( g_groupA )
--~ 	end
--~ 	if (SGroup_Exists( "groupB" )) then
--~ 		SGroup_DestroyAllSquads(g_groupB)
--~ 		SGroup_Destroy( g_groupB )
--~ 	end
	
	local egroup1 = Player_GetEntities(World_GetPlayerAt(1))
	EGroup_DestroyAllEntities( egroup1 )
	local egroup2 = Player_GetEntities(World_GetPlayerAt(2))
	EGroup_DestroyAllEntities( egroup2 )
	
	-- destroy all entity from both players - destroys any remaining bits and pieces (casualities)
	local sgroup1 = Player_GetSquads(World_GetPlayerAt(1))
	SGroup_DestroyAllSquads( sgroup1 )
	local sgroup2 = Player_GetSquads(World_GetPlayerAt(2))
	SGroup_DestroyAllSquads( sgroup2 )
	
	g_current_battle_iteration = g_current_battle_iteration + 1
	if(g_current_battle_iteration > iterations) then
		g_current_battle_number = g_current_battle_number + 1
		g_current_battle_iteration = 1
	end

	
	-- go to next battle
	BATTLE_Loop()
	
end

function BATTLE_IsDone()

	-- check to see if squadA or squadB are still alive
	if (SGroup_Count( g_groupA ) == 0 or SGroup_Count( g_groupB ) == 0 ) then
		BATTLE_Over()
	end
	
	local timeElapsed = World_GetGameTime() - g_battle_start_time
	
	-- or if a timer has expired
	if ( timeElapsed > kMaxBattleTime ) then
		BATTLE_Over()
	end
	
end
