transporttotal=24;
transportsaway=0
transportsdestroyed=0
timercount=0

GUID = 
    { 110, 91, 157, 190, 18, 23, 250, 78, 144, 20, 41, 246, 181, 128, 214, 12, }
GameRulesName = "Hoth Test"
Directories =
{
    Levels = "data:LevelData\\Multiplayer\\Hoth\\",
}
Description = "Hoth campaign\nWarlords Evolution by TelQuessir"
GameSetupOptions = 
    { 
    {
         name = "startingfleetoption",
         locName = "Starting Assets",
         tooltip = "Select start option: Begin the game with a Shipyard/Patrol Flotilla, or a full expeditionary force. Jin Vagyr and Sobani are player-only commands.",
         default = 0,
         visible = 0,
         choices =
            { "1", "duel", }, },
    { 
        name = "resstart", 
        locName = "$3205", 
        tooltip = "Select starting RU levels. Starting off with some RUs in the supply chain is proper for a prepared engagement.", 
        default = 0, 
        visible = 0, 
        choices = 
            { "$3209", "0", }, }, 
    { 
        name = "lockteams", 
        locName = "$3220", 
        tooltip = "$3235", 
        default = 0, 
        visible = 0, 
        choices = 
            { "$3221", "yes", "$3222", "no", }, }, 
    { 
        name = "startlocation", 
        locName = "$3225", 
        tooltip = "Lock starting locations: As PDS is meant as a wargame and not a free-for-all, Fixed is recommended for pseudo-scenario-based gameplay.", 
        default = 1, 
        visible = 0, 
        choices = 
            { "$3226", "random", "$3227", "fixed", }, }, 
    }
dofilepath("data:scripts/scar/restrict.lua")

Events = {}
Events.endGame = 
    { 
        { 
            { "wID = Wait_Start(250)", "Wait_End(wID)", }, 
        }, 
    }
BattleRadius = 10000

function OnInit()
	ATI_LoadTemplates("data:LevelData\\Hoth_ATI.lua")
	MPRestrict()
	local startf = GetGameSettingAsString("startingfleetoption")
	SetStartFleetSuffix("duel")
	Rule_Add("SetupRule")
	printorders()
	Rule_AddInterval("Rule_SurvivorCount", 2)
	--ATI_LoadTemplates("data:LevelData\\ATI.lua")
end


function printorders()
	--ATI_LoadTemplates("data:LevelData\\ATI.lua")
	local message = "get the transports to safety before the imperial reinforcements arrive: Destroy the Interdictors"
	local rect = {0.1, 0.2, 0, 0}
	local colour = {1, 1, 0, 1}
	Display_Message(message, rect, colour)
end

function Rule_SurvivorCount()
	DisplaySurvivorCount()
end


function DisplaySurvivorCount()
	
	local iSurvivorCount = GetSurvivorCount()
	local message = tostring(iSurvivorCount)..":"..tostring(transporttotal).." Transports";
	local rect = {.1, 0.9, 0, 0};
	local colour = {1, 1, 1, 1};
	Display_Message(message, rect, colour);
end

function GetSurvivorCount()
	SobGroup_Create("transports")
	SobGroup_Clear("transports")
	SobGroup_FillShipsByType("transports", "Player_Ships0", "reb_galofree_transport")
	local iSurvivorCount = SobGroup_Count("transports")
	return iSurvivorCount
end

function SetupRule()
    pingid = 0
    Sensors_SetCameraMinMax (0, 50000)
    Rule_Add("MainRule")
	Rule_Add("AISetup")
--    Rule_AddInterval("BattleRule",1)
Rule_AddInterval("pickontransports",30)
    Rule_Remove("SetupRule")

end

function pickontransports()
SobGroup_SetTactics("transports", PassiveTactics)
SobGroup_Attack(1, "Player_Ships1", "transports")
end


function AISetup()
   local playerIndex = 0;
    local playerCount = Universe_PlayerCount(); 
    while (playerIndex < playerCount) do
        Player_SetRU(playerIndex, 0)
        Player_SetDefaultShipTactic(playerIndex, AggressiveTactics)
        playerIndex = playerIndex+1
    end
    Rule_Remove("AISetup")
end

function MainRule()
    local playerIndex = 0
    local playerCount = Universe_PlayerCount()
    while  playerIndex<playerCount do
        if  Player_IsAlive(playerIndex)==1 then
            if  Player_NumberOfAwakeShips(playerIndex)==0 then
                Player_Kill(playerIndex)
            end 
        end 
        playerIndex = (playerIndex + 1)
    end 
    
    local numAlive = 0
    local numEnemies = 0
    local gameOver = 1
    
    playerIndex = 0
    while (playerIndex < playerCount) do
        if (Player_IsAlive(playerIndex)==1) then
            local otherPlayerIndex = 0;
            while (otherPlayerIndex < playerCount) do
                if (AreAllied(playerIndex, otherPlayerIndex) == 0) then
                    if (Player_IsAlive(otherPlayerIndex)==1) then
                        gameOver = 0
                    else
                        numEnemies = numEnemies + 1
                    end
                end
                otherPlayerIndex = otherPlayerIndex+1
            end
            numAlive = numAlive + 1
        end
        
        playerIndex = playerIndex+1
    end

        
    if (numEnemies == 0 and numAlive>0) then
        gameOver = 0;
    end
	if(GetSurvivorCount()<5 and SobGroup_AreAllInHyperspace("transports")==0) then
			gameOver=1;
			Rule_Remove("Rule_SurvivorCount")
			local message = "Too Many Transports Destroyed"
				local rect = {.5, 0.25, 0, 0}
				local colour = {1, 1, 1, 1}
				Display_Message(message, rect, colour)
	end
	if(SobGroup_AreAllInHyperspace("transports")==1) then
    			gameOver=1;
			Rule_Remove("Rule_SurvivorCount")
			local message = "All Transports Away"
				local rect = {.5, 0.25, 0, 0}
				local colour = {1, 1, 1, 1}
				Display_Message(message, rect, colour)
	end
    if  gameOver==1 then
        Event_Start("endGame")
        Rule_Add("waitForEnd")
        Rule_Remove("MainRule")
    end 
end


function waitForEnd()
    if  Event_IsDone("endGame") then
        setGameOver()
        Rule_Remove("waitForEnd")
    end 

end

function Display_Message(message, rect, colour)
	ATI_Clear()
	ATI_CreateParameters(2)
	ATI_AddString(0, message)
	ATI_AddColour(1, colour)
	ATI_Display2D("Message", rect, 0)
	print (message)
end
function Display_Timer(timer, rect, colour)
	ATI_Clear()
	ATI_CreateParameters(2)
	ATI_AddString(0, timer)
	ATI_AddColour(1, colour)
	ATI_Display2D("Timer", rect, 0)
	print (timer)
end
