////////////////////////////////////////////////////////////////////////////////
// rts_ex_control - Control Point Capture Victory condition monitor
//------------------------------------------------------------------------------
// By Deva B. Winblood   10/22/2006
////////////////////////////////////////////////////////////////////////////////

#include "dla_i0_strings"
#include "header_sounds"
#include "powres_h"

void fnMessageAllPlayers(string sMsg,string sSound="")
{ // PURPOSE: Send a message and if sound is needed play it
    object oPC=GetFirstPC();
    while(GetIsObjectValid(oPC))
    { // all PCs
        SendMessageToPC(oPC,sMsg);
        if (GetStringLength(sSound)>0) AssignCommand(oPC,fnSoundAlert(sSound));
        oPC=GetNextPC();
    } // all PCs
} // fnMessageAllPlayers()

string fnTeam(string sID)
{ // PURPOSE: send text team back
    if (sID=="SPID") return "Spider Cultists";
    else if (sID=="UND") return "Undead";
    else if (sID=="UNC") return "Unclean";
    else if (sID=="DWF") return "Dwarves";
    return "Unknown";
} // fnTeam()


void main()
{
    object oMod=GetModule();
    object oPC;
    object oOb;
    int nSPID;
    int nUNC;
    int nUND;
    int nDWF;
    int nN;
    int nWarnCount;
    string sMsg;
    string sLastWinner=GetLocalString(oMod,"sControlVictoryTeam");
    int nCountDown;
    int bConditionEngaged=FALSE;
    string sTeam;
    nN=0;
    oOb=GetObjectByTag("CONTROLSPID",nN);
    while(GetIsObjectValid(oOb))
    { // count
        nSPID++;
        nN++;
        oOb=GetObjectByTag("CONTROLSPID",nN);
    } // count
    if (nSPID>3) { sTeam="SPID"; bConditionEngaged=TRUE; }
    nN=0;
    oOb=GetObjectByTag("CONTROLDWF",nN);
    while(GetIsObjectValid(oOb))
    { // count
        nDWF++;
        nN++;
        oOb=GetObjectByTag("CONTROLDWF",nN);
    } // count
    if (nDWF>3) { sTeam="DWF"; bConditionEngaged=TRUE; }
    nN=0;
    oOb=GetObjectByTag("CONTROLUNC",nN);
    while(GetIsObjectValid(oOb))
    { // count
        nUNC++;
        nN++;
        oOb=GetObjectByTag("CONTROLUNC",nN);
    } // count
    if (nUNC>3) { sTeam="UNC"; bConditionEngaged=TRUE; }
    nN=0;
    oOb=GetObjectByTag("CONTROLUND",nN);
    while(GetIsObjectValid(oOb))
    { // count
        nUND++;
        nN++;
        oOb=GetObjectByTag("CONTROLUND",nN);
    } // count
    if (nUND>3) { sTeam="UND"; bConditionEngaged=TRUE; }
    if (bConditionEngaged)
    { // some team is on the path to victory
        if (sLastWinner!=sTeam)
        { // new controllers
            SetLocalString(oMod,"sControlVictoryTeam",sTeam);
            nCountDown=GetLocalInt(oMod,"nVictoryCountLength");
            SetLocalInt(oMod,"nControlCountDown",nCountDown);
            //fnMessageAllPlayers(DLA_SetStringColor(fnTeam(sTeam)+" has claimed all 4 control points and will win the game in "+IntToString(nCountDown)+" seconds!!","636"),"as_cv_bellship2");
            sMsg=DLA_SetStringColor(fnTeam(sTeam)+" has claimed all 4 control points and will win the game in "+IntToString(nCountDown)+" seconds!!","636");
            fnPowerNotify(5,sMsg);
        } // new controllers
        else
        { // existing - do count down
            nCountDown=GetLocalInt(oMod,"nControlCountDown");
            nCountDown=nCountDown-6;
            if (nCountDown<1)
            { // victory
                oPC=GetLocalObject(oMod,"oTeamLead"+sLastWinner);
                SetLocalObject(oMod,"oWinner",oPC);
                ExecuteScript("rts_end_game",oMod);
            } // victory
            else
            { // message
                if (nCountDown>60)
                { // not urgent
                     nWarnCount=GetLocalInt(oMod,"nControlWarnCount");
                     if (nWarnCount>3)
                     { // message
                         nWarnCount=0;
                         fnMessageAllPlayers(DLA_SetStringColor(fnTeam(sTeam)+" will win by control point victory in "+IntToString(nCountDown)+" seconds!!","633"),"");
                     } // message
                     else { nWarnCount++; }
                     SetLocalInt(oMod,"nControlWarnCount",nWarnCount);
                } // not urgent
                else if (nCountDown>30)
                { // urgent
                    fnMessageAllPlayers(DLA_SetStringColor(fnTeam(sTeam)+" will win by control point victory in "+IntToString(nCountDown)+" seconds!!","266"),"gui_dm_alert");
                } // urgent
                else
                { // imminent
                    fnMessageAllPlayers(DLA_SetStringColor(fnTeam(sTeam)+" is about to win!  You have "+IntToString(nCountDown)+" seconds!!","363"),"as_cv_boomdist1");
                } // imminent
                SetLocalInt(oMod,"nControlCountDown",nCountDown);
            } // message
        } // existing - do count down
    } // some team is on the path to victory
    else
    { // make sure no victors
        if (GetStringLength(sLastWinner)>0)
        { // send victory aborted message
            fnMessageAllPlayers(DLA_SetStringColor("Control point victory has been thwarted!","666"),"as_cv_eulpipe2");
            DeleteLocalString(oMod,"sControlVictoryTeam");
        } // send victory aborted message
    } // make sure no victors
}