GUID = 
    { 110, 91, 157, 190, 18, 23, 250, 78, 144, 20, 41, 246, 181, 128, 214, 12, }
GameRulesName = "SW:W-Evolution"
Directories =
{
    Levels = "data:LevelData\\Multiplayer\\duel\\",
}
Description = "Instant Action\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(5)", "Wait_End(wID)", }, 
        }, 
    }
BattleRadius = 10000

function OnInit()
    MPRestrict()
    local startf = GetGameSettingAsString("startingfleetoption")
    SetStartFleetSuffix("duel")
    Rule_Add("SetupRule")
end

function SetupRule()
    pingid = 0
    Sensors_SetCameraMinMax (0, 50000)
    Rule_Add("MainRule")
    Rule_Add("AISetup")
--    Rule_AddInterval("BattleRule",1)
    Rule_Remove("SetupRule")
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  gameOver==1 then
        Event_Start("endGame")
        Rule_Add("waitForEnd")
        Rule_Remove("MainRule")
    end 
end

function BattleRule()
    local playerCount = Universe_PlayerCount()
    local temp =0
    local playerIndex = 0
    local tempInt = 0
    local TeamNum = 0
    PlayerToTeam = {}
    
    SobGroup_Create("Universe_AllShips")
    SobGroup_Clear("Universe_AllShips")
    
    while (playerIndex < Universe_PlayerCount()) do 
        SobGroup_SobGroupAdd ("Universe_AllShips","Player_Ships"..playerIndex)
        PlayerToTeam[playerIndex] = 99
        tempInt = 0
        while (tempInt < playerIndex) do
            if (AreAllied(playerIndex,tempInt)==1)then
                PlayerToTeam[playerIndex] = PlayerToTeam[tempInt]
                SobGroup_SobGroupAdd ("Universe_TeamShips" .. PlayerToTeam[playerIndex],"Player_Ships"..playerIndex)
                tempInt = playerIndex
            end
            tempInt = tempInt + 1
        end
        if (PlayerToTeam[playerIndex] == 99) then
            SobGroup_Create("Universe_TeamShips"..TeamNum)
            SobGroup_Clear("Universe_TeamShips"..TeamNum)
            SobGroup_SobGroupAdd ("Universe_TeamShips"..TeamNum,"Player_Ships"..playerIndex)
            PlayerToTeam[playerIndex] = TeamNum
            TeamNum = TeamNum + 1
        end
        playerIndex = playerIndex + 1
    end
    
    local currentteam = 0
    SobGroup_Create("allEnemySob")
    while (currentteam < TeamNum) do
        SobGroup_GetAttackers ("Universe_TeamShips"..currentteam,"allEnemySob")
        if (SobGroup_Empty("allEnemySob") == 0)then
            local numAttackers = SobGroup_SplitGroup("AttackerSob","allEnemySob",SobGroup_Count("allEnemySob"));
            tempInt = 0
            numBattles = 0
            SobGroup_Create("Battle0")
            SobGroup_Create("TempSob")
            while (tempInt < numAttackers) do
                SobGroup_Clear("TempSob")
                if (SobGroup_FillProximitySobGroup("TempSob", "Universe_TeamShips"..currentteam,"AttackerSob"..tempInt, BattleRadius)==1)then
                    local tempInt2 = 0
                    local match = 0
                    while (tempInt2 < numBattles) do
                        if ( SobGroup_GroupInGroup ("Battle"..tempInt2,"TempSob") == 1)then
                            match = 1
                            SobGroup_SobGroupAdd ("Battle"..tempInt2, "TempSob")
                        end
                        tempInt2 = tempInt2 + 1
                    end
                    if (match == 0) then
                        SobGroup_Create("Battle"..tempInt2)
                        SobGroup_Clear("Battle"..tempInt2)
                        SobGroup_SobGroupAdd ("Battle"..tempInt2, "TempSob")
                        numBattles = numBattles + 1
                    end
                end
                tempInt = tempInt + 1
            end             
            tempInt = 0
            while (tempInt < numBattles) do
                BattleProcess("Battle"..tempInt);
                tempInt  = tempInt + 1
            end
        end
        currentteam = currentteam + 1
    end
    
    
end

function BattleProcess (Defenders)
    local temp
    SobGroup_Create("processTemp")
    SobGroup_Clear("processTemp")
    
    SobGroup_Create("processEnemy")
    SobGroup_Clear("processEnemy")
    SobGroup_FillProximitySobGroup ("processTemp", "Universe_AllShips", Defenders, 2*BattleRadius)
    SobGroup_FillSubstract ("processEnemy", "processTemp", Defenders)
    SobGroup_Clear("processTemp")
    
    SobGroup_Create("defenderCore")
    SobGroup_Clear("defenderCore")  
    SobGroup_FillShipsByType ("processTemp", Defenders, "Hgn_Mothership")
    SobGroup_SobGroupAdd("defenderCore","processTemp")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Vgr_Mothership")
    SobGroup_SobGroupAdd("defenderCore","processTemp")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Hgn_Shipyard")
    SobGroup_SobGroupAdd("defenderCore","processTemp")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Vgr_Shipyard")
    SobGroup_SobGroupAdd("defenderCore","processTemp")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Hgn_Carrier")
    SobGroup_SobGroupAdd("defenderCore","processTemp")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Hgn_Carrier_Soban")
    SobGroup_SobGroupAdd("defenderCore","processTemp")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Hgn_ScoutCarrier")
    SobGroup_SobGroupAdd("defenderCore","processTemp")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Hgn_DroneCarrier")
    SobGroup_SobGroupAdd("defenderCore","processTemp")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Vgr_Carrier")
    SobGroup_SobGroupAdd("defenderCore","processTemp")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Hgn_Battlecruiser")
    SobGroup_SobGroupAdd("defenderCore","processTemp")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Hgn_BattleCarrier")
    SobGroup_SobGroupAdd("defenderCore","processTemp")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Vgr_Battlecruiser")
    SobGroup_SobGroupAdd("defenderCore","processTemp")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Hgn_Destroyer")
    SobGroup_SobGroupAdd("defenderCore","processTemp")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Vgr_Destroyer")
    SobGroup_SobGroupAdd("defenderCore","processTemp")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Hgn_IonCannonFrigate")
    SobGroup_SobGroupAdd("defenderCore","processTemp")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Vgr_HeavyMissileFrigate")
    SobGroup_SobGroupAdd("defenderCore","processTemp")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Hgn_TorpedoFrigate")
    SobGroup_SobGroupAdd("defenderCore","processTemp")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Vgr_StrikeCarrier_named")
    SobGroup_SobGroupAdd("defenderCore","processTemp")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Vgr_StrikeCarrier")
    SobGroup_SobGroupAdd("defenderCore","processTemp")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Vgr_ScoutCarrier")
    SobGroup_SobGroupAdd("defenderCore","processTemp")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Vgr_J5")
    SobGroup_SobGroupAdd("defenderCore","processTemp")
    SobGroup_Clear("processTemp")
    
    SobGroup_Create("battleAntiCapShips")
    SobGroup_Clear("battleAntiCapShips")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Hgn_Battlecruiser")
    SobGroup_SobGroupAdd("battleAntiCapShips","processTemp")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Hgn_Destroyer")
    SobGroup_SobGroupAdd("battleAntiCapShips","processTemp")
    SobGroup_FillShipsByType ("processTemp", Defenders, "Hgn_IonCannonFrigate")
    SobGroup_SobGroupAdd("battleAntiCapShips","processTemp")
    SobGroup_Clear("processTemp")
    local decision = 0
    if ( SobGroup_AreAnyOfTheseTypes ("processEnemy","Hgn_Battlecruiser, Vgr_Battlecruiser") == 1) then
        SobGroup_Create("battleEnemyBCs")
        SobGroup_Clear("battleEnemyBCs")
        SobGroup_FillShipsByType ("processTemp", "processEnemy", "Hgn_Battlecruiser")
        SobGroup_SobGroupAdd("battleEnemyBCs","processTemp")
        SobGroup_FillShipsByType ("processTemp", "processEnemy", "Vgr_Battlecruiser")
        SobGroup_SobGroupAdd("battleEnemyBCs","processTemp")
    end
    if ( decision == 1) then
        SobGroup_SplitGroupReference("battleEnemyBCToAttack", "battleEnemyBCs", "battleAntiCapShips", 1)
    else if ( SobGroup_AreAnyOfTheseTypes ("processEnemy","Hgn_Destroyer,Vgr_Destroyer") == 1) then
            SobGroup_Create("battleEnemyBCs")
            SobGroup_Clear("battleEnemyBCs")
            SobGroup_FillShipsByType ("processTemp", "processEnemy", "Hgn_Destroyer")
            SobGroup_SobGroupAdd("battleEnemyBCs","processTemp")
            SobGroup_FillShipsByType ("processTemp", "processEnemy", "Vgr_Destroyer")
            SobGroup_SobGroupAdd("battleEnemyBCs","processTemp")
            SobGroup_SplitGroupReference("battleEnemyBCToAttack", "battleEnemyBCs", "battleAntiCapShips", 1)
        else if ( SobGroup_AreAnyOfTheseTypes ("processEnemy","Hgn_IonCannonFrigate,Vgr_HeavyMissileFrigate") == 1) then
                SobGroup_Create("battleEnemyBCs")
                SobGroup_Clear("battleEnemyBCs")
                SobGroup_FillShipsByType ("processTemp", "processEnemy", "Hgn_IonCannonFrigate")
                SobGroup_SobGroupAdd("battleEnemyBCs","processTemp")
                SobGroup_FillShipsByType ("processTemp", "processEnemy", "Vgr_HeavyMissileFrigate")
                SobGroup_SobGroupAdd("battleEnemyBCs","processTemp")
                SobGroup_SplitGroupReference("battleEnemyBCToAttack", "battleEnemyBCs", "battleAntiCapShips", 1)
            end
        end
    end
    
    temp = SobGroup_SplitGroup("BattleIndivShips",Defenders,SobGroup_Count(Defenders));
    local index = 0
    while (index < temp) do
        SobGroup_Clear("processTemp")
        
        if ((SobGroup_AreAnyFromTheseAttackFamilies ("BattleIndivShips"..index, "Fighter,Corvette")==1) and (SobGroup_AreAnyFromTheseAttackFamilies ("BattleIndivShips"..index, "SmallCapitalShip, BigCapitalShip, Mothership")==0)) then
            if ( SobGroup_HealthPercentage ("BattleIndivShips"..index) < .5) then
                if (SobGroup_AreAnyOfTheseTypes (Defenders,"Hgn_Battlecruiser, Hgn_BattleCarrier, Hgn_Carrier, Hgn_Mothership, Hgn_Shipyard, Vgr_Battlecruiser, Vgr_Carrier, Vgr_StrikeCarrier, Vgr_StrikeCarrier_named, Vgr_Mothership, Vgr_Shipyard, Vgr_J5")==1) then
                    SobGroup_DockSobGroup ("BattleIndivShips"..index, Defenders)
                end
            end
        end
        
        SobGroup_GetAttackers ("BattleIndivShips"..index, "processTemp")
        index = index +1
    end
    

    
end


function SobGroup_SplitGroup(SobGroupOut, SobGroupToSplit, NumberToSplit)

    local index = 0
    local distance = 0
    local bool = 0
    local SobNum = 0
    SobGroup_Create("TempSobGroup")
    SobGroup_Clear ("TempSobGroup")
    SobGroup_Create("TempSobGroup1")
    SobGroup_Clear ("TempSobGroup1")
    SobGroup_SobGroupAdd ("TempSobGroup", SobGroupToSplit)
    if ( SobGroup_Empty (SobGroupToSplit) == 1 ) then
        return 0
    end
    if ( NumberToSplit > SobGroup_Count(SobGroupToSplit) ) then
        NumberToSplit = SobGroup_Count(SobGroupToSplit)
    end
    while (index < NumberToSplit ) do
        bool = 0
        interval = 625.0    
        while (bool == 0) do
            distance = distance + interval
            if (interval > 3000000) then       
                bool =1
                return SobNum
            end
            SobGroup_FillProximitySobGroup ("TempSobGroup1", "TempSobGroup", SobGroupToSplit, distance)             
            if (SobGroup_Empty("TempSobGroup1")==1)then
            else
                if (SobGroup_Count("TempSobGroup1") > 1) then
                    if (interval <= .2) then
                        SobGroup_Create(SobGroupOut .. tostring(SobNum))
                        SobGroup_Clear (SobGroupOut .. tostring(SobNum))
                        SobGroup_SobGroupAdd (SobGroupOut .. tostring(SobNum), "TempSobGroup1")
                        SobGroup_Create("tempsob")
                        SobGroup_FillSubstract("tempsob", "TempSobGroup", SobGroupOut .. tostring(SobNum))
                        SobGroup_Clear ("TempSobGroup")
                        SobGroup_SobGroupAdd ("TempSobGroup", "tempsob")
                        bool = 1
                    else
                        distance = distance - interval
                        interval = interval / 5
                    end
                else
                    SobGroup_Create(SobGroupOut .. tostring(SobNum))
                    SobGroup_Clear (SobGroupOut .. tostring(SobNum))
                    SobGroup_SobGroupAdd (SobGroupOut .. tostring(SobNum), "TempSobGroup1")
                    SobGroup_Create("tempsob")
                    SobGroup_FillSubstract("tempsob", "TempSobGroup", SobGroupOut .. tostring(SobNum))
                    SobGroup_Clear ("TempSobGroup")
                    SobGroup_SobGroupAdd ("TempSobGroup", "tempsob")
                    bool = 1
                end
            end             
        end
        index = index + SobGroup_Count(SobGroupOut .. tostring(SobNum))
        SobNum = SobNum + 1
    end
    return SobNum
end



function SobGroup_SplitGroupReference(SobGroupOut, SobGroupToSplit, ReferenceSobGroup, NumberToSplit)

    local index = 0
    local distance = 0
    local bool = 0
    local SobNum = 0
    SobGroup_Create("TempSobGroup")
    SobGroup_Clear ("TempSobGroup")
    SobGroup_Create("TempSobGroup1")
    SobGroup_Clear ("TempSobGroup1")
    SobGroup_SobGroupAdd ("TempSobGroup", SobGroupToSplit)
    if ( SobGroup_Empty (SobGroupToSplit) == 1 ) then
        return 0
    end
    if ( NumberToSplit > SobGroup_Count(SobGroupToSplit) ) then
        NumberToSplit = SobGroup_Count(SobGroupToSplit)
    end
    while (index < NumberToSplit ) do
        bool = 0
        interval = 625.0    
        while (bool == 0) do
            distance = distance + interval
            if (interval > 3000000) then        
                bool =1
                return SobNum
            end
            SobGroup_FillProximitySobGroup ("TempSobGroup1", "TempSobGroup", ReferenceSobGroup, distance)            
            if (SobGroup_Empty("TempSobGroup1")==1)then
            else
                if (SobGroup_Count("TempSobGroup1") > 1) then
                    if (interval <= .2) then
                        SobGroup_Create(SobGroupOut .. tostring(SobNum))
                        SobGroup_Clear (SobGroupOut .. tostring(SobNum))
                        SobGroup_SobGroupAdd (SobGroupOut .. tostring(SobNum), "TempSobGroup1")
                        SobGroup_Create("tempsob")
                        SobGroup_FillSubstract("tempsob", "TempSobGroup", SobGroupOut .. tostring(SobNum))
                        SobGroup_Clear ("TempSobGroup")
                        SobGroup_SobGroupAdd ("TempSobGroup", "tempsob")
                        bool = 1
                    else
                        distance = distance - interval
                        interval = interval / 5
                    end
                else
                    SobGroup_Create(SobGroupOut .. tostring(SobNum))
                    SobGroup_Clear (SobGroupOut .. tostring(SobNum))
                    SobGroup_SobGroupAdd (SobGroupOut .. tostring(SobNum), "TempSobGroup1")
                    SobGroup_Create("tempsob")
                    SobGroup_FillSubstract("tempsob", "TempSobGroup", SobGroupOut .. tostring(SobNum))
                    SobGroup_Clear ("TempSobGroup")
                    SobGroup_SobGroupAdd ("TempSobGroup", "tempsob")
                    bool = 1
                end
            end             
        end
        index = index + SobGroup_Count(SobGroupOut .. tostring(SobNum))
        SobNum = SobNum + 1
    end
    return SobNum
end

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

end
