// IW6 GSC SOURCE
// Generated by https://github.com/xensik/gsc-tool

main()
{
    if ( getdvar( "mapname" ) == "mp_background" )
        return;

    maps\mp\gametypes\_globallogic::init();
    maps\mp\gametypes\_callbacksetup::setupcallbacks();
    maps\mp\gametypes\_globallogic::setupcallbacks();

    if ( isusingmatchrulesdata() )
    {
        level.initializematchrules = ::initializematchrules;
        [[ level.initializematchrules ]]();
        level thread maps\mp\_utility::reinitializematchrulesonmigration();
    }
    else
    {
        maps\mp\_utility::registerroundswitchdvar( level.gametype, 3, 0, 9 );
        maps\mp\_utility::registertimelimitdvar( level.gametype, 5 );
        maps\mp\_utility::registerscorelimitdvar( level.gametype, 1 );
        maps\mp\_utility::registerroundlimitdvar( level.gametype, 0 );
        maps\mp\_utility::registerwinlimitdvar( level.gametype, 4 );
        maps\mp\_utility::registernumlivesdvar( level.gametype, 1 );
        maps\mp\_utility::registerhalftimedvar( level.gametype, 0 );
        level.matchrules_damagemultiplier = 0;
        level.matchrules_vampirism = 0;
    }

    level.objectivebased = 1;
    level.teambased = 1;
    level.nobuddyspawns = 1;
    level.gamehasstarted = 0;
    level.onstartgametype = ::onstartgametype;
    level.getspawnpoint = ::getspawnpoint;
    level.onspawnplayer = ::onspawnplayer;
    level.onplayerkilled = ::onplayerkilled;
    level.ondeadevent = ::ondeadevent;
    level.ononeleftevent = ::ononeleftevent;
    level.ontimelimit = ::ontimelimit;
    level.initgametypeawards = ::initgametypeawards;
    level.lastcaptime = gettime();
    level.alliesprevflagcount = 0;
    level.axisprevflagcount = 0;
    level.allowlatecomers = 0;
    level.gametimerbeeps = 0;
    level.siegeflagcapturing = [];

    if ( level.matchrules_damagemultiplier || level.matchrules_vampirism )
        level.modifyplayerdamage = maps\mp\gametypes\_damage::gamemodemodifyplayerdamage;

    game["dialog"]["offense_obj"] = "capture_objs";
    game["dialog"]["defense_obj"] = "capture_objs";
    game["dialog"]["revived"] = "sr_rev";
    thread onplayerconnect();
    thread onplayerswitchteam();
}

initializematchrules()
{
    maps\mp\_utility::setcommonrulesfrommatchrulesdata();
    var_0 = getmatchrulesdata( "siegeData", "roundLength" );
    setdynamicdvar( "scr_siege_timelimit", var_0 );
    maps\mp\_utility::registertimelimitdvar( "siege", var_0 );
    var_1 = getmatchrulesdata( "siegeData", "roundSwitch" );
    setdynamicdvar( "scr_siege_roundswitch", var_1 );
    maps\mp\_utility::registerroundswitchdvar( "siege", var_1, 0, 9 );
    var_2 = getmatchrulesdata( "commonOption", "scoreLimit" );
    setdynamicdvar( "scr_siege_winlimit", var_2 );
    maps\mp\_utility::registerwinlimitdvar( "siege", var_2 );
    var_3 = getmatchrulesdata( "siegeData", "capRate" );
    setdynamicdvar( "scr_siege_caprate", var_3 );
    var_4 = getmatchrulesdata( "siegeData", "rushTimer" );
    setdynamicdvar( "scr_siege_rushtimer", var_4 );
    var_5 = getmatchrulesdata( "siegeData", "rushTimerAmount" );
    setdynamicdvar( "scr_siege_rushtimeramount", var_5 );
    var_6 = getmatchrulesdata( "siegeData", "preCapPoints" );
    setdynamicdvar( "scr_siege_precap", var_6 );
    setdynamicdvar( "scr_siege_roundlimit", 0 );
    maps\mp\_utility::registerroundlimitdvar( "siege", 0 );
    setdynamicdvar( "scr_siege_scorelimit", 1 );
    maps\mp\_utility::registerscorelimitdvar( "siege", 1 );
    setdynamicdvar( "scr_siege_halftime", 0 );
    maps\mp\_utility::registerhalftimedvar( "siege", 0 );
    setdynamicdvar( "scr_siege_promode", 0 );
}

onstartgametype()
{
    if ( !isdefined( game["switchedsides"] ) )
        game["switchedsides"] = 0;

    if ( game["switchedsides"] )
    {
        var_0 = game["attackers"];
        var_1 = game["defenders"];
        game["attackers"] = var_1;
        game["defenders"] = var_0;
    }

    maps\mp\_utility::setobjectivetext( "allies", &"OBJECTIVES_DOM" );
    maps\mp\_utility::setobjectivetext( "axis", &"OBJECTIVES_DOM" );

    if ( level.splitscreen )
    {
        maps\mp\_utility::setobjectivescoretext( "allies", &"OBJECTIVES_DOM" );
        maps\mp\_utility::setobjectivescoretext( "axis", &"OBJECTIVES_DOM" );
    }
    else
    {
        maps\mp\_utility::setobjectivescoretext( "allies", &"OBJECTIVES_DOM_SCORE" );
        maps\mp\_utility::setobjectivescoretext( "axis", &"OBJECTIVES_DOM_SCORE" );
    }

    maps\mp\_utility::setobjectivehinttext( "allies", &"OBJECTIVES_DOM_HINT" );
    maps\mp\_utility::setobjectivehinttext( "axis", &"OBJECTIVES_DOM_HINT" );
    initspawns();
    var_2[0] = "dom";
    maps\mp\gametypes\_gameobjects::main( var_2 );
    level.flagbasefxid["neutral"] = loadfx( "vfx/gameplay/mp/core/vfx_marker_base_grey" );
    level.flagbasefxid["friendly"] = loadfx( "vfx/gameplay/mp/core/vfx_marker_base_cyan" );
    level.flagbasefxid["enemy"] = loadfx( "vfx/gameplay/mp/core/vfx_marker_base_orange" );
    thread domflags();
    thread watchflagtimerpause();
    thread watchflagtimerreset();
    thread watchflagenduse();
    thread watchgameinactive();
    thread watchgamestart();
}

initspawns()
{
    level.spawnmins = ( 0, 0, 0 );
    level.spawnmaxs = ( 0, 0, 0 );
    maps\mp\gametypes\_spawnlogic::addstartspawnpoints( "mp_dom_spawn_allies_start" );
    maps\mp\gametypes\_spawnlogic::addstartspawnpoints( "mp_dom_spawn_axis_start" );
    maps\mp\gametypes\_spawnlogic::addspawnpoints( "allies", "mp_dom_spawn" );
    maps\mp\gametypes\_spawnlogic::addspawnpoints( "axis", "mp_dom_spawn" );
    level.mapcenter = maps\mp\gametypes\_spawnlogic::findboxcenter( level.spawnmins, level.spawnmaxs );
    setmapcenter( level.mapcenter );
}

getspawnpoint()
{
    var_0 = self.pers["team"];
    var_1 = maps\mp\_utility::getotherteam( var_0 );

    if ( level.usestartspawns )
    {
        if ( game["switchedsides"] )
        {
            var_2 = maps\mp\gametypes\_spawnlogic::getspawnpointarray( "mp_dom_spawn_" + var_1 + "_start" );
            var_3 = maps\mp\gametypes\_spawnlogic::getspawnpoint_startspawn( var_2 );
        }
        else
        {
            var_2 = maps\mp\gametypes\_spawnlogic::getspawnpointarray( "mp_dom_spawn_" + var_0 + "_start" );
            var_3 = maps\mp\gametypes\_spawnlogic::getspawnpoint_startspawn( var_2 );
        }
    }
    else
    {
        var_4 = getteamdompoints( var_0 );
        var_5 = maps\mp\_utility::getotherteam( var_0 );
        var_6 = getteamdompoints( var_5 );
        var_7 = getperfereddompoints( var_4, var_6 );
        var_2 = maps\mp\gametypes\_spawnlogic::getteamspawnpoints( var_0 );
        var_3 = maps\mp\gametypes\_spawnscoring::getspawnpoint_domination( var_2, var_7 );
    }

    return var_3;
}

getteamdompoints( var_0 )
{
    var_1 = [];

    foreach ( var_3 in level.domflags )
    {
        if ( var_3.ownerteam == var_0 )
            var_1[var_1.size] = var_3;
    }

    return var_1;
}

getperfereddompoints( var_0, var_1 )
{
    var_2 = [];
    var_2[0] = 0;
    var_2[1] = 0;
    var_2[2] = 0;
    var_3 = self.pers["team"];

    if ( var_0.size == level.domflags.size )
    {
        var_4 = var_3;
        var_5 = level.bestspawnflag[var_3];
        var_2[var_5.useobj.dompointnumber] = 1;
        return var_2;
    }

    if ( var_0.size > 0 )
    {
        foreach ( var_7 in var_0 )
            var_2[var_7.dompointnumber] = 1;

        return var_2;
    }

    if ( var_0.size == 0 )
    {
        var_4 = var_3;
        var_5 = level.bestspawnflag[var_4];

        if ( var_1.size > 0 && var_1.size < level.domflags.size )
        {
            var_5 = getunownedflagneareststart( var_4, undefined );
            level.bestspawnflag[var_4] = var_5;
        }

        var_2[var_5.useobj.dompointnumber] = 1;
        return var_2;
    }

    return var_2;
}

gettimesincedompointcapture( var_0 )
{
    return gettime() - var_0.capturetime;
}

onplayerconnect()
{
    for (;;)
    {
        level waittill( "connected", var_0 );
        var_0._domflageffect = [];
        var_0 thread onplayerdisconnect();
        var_0 thread refreshfreecambasefx();
        var_0.siegelatecomer = 1;
    }
}

onplayerswitchteam()
{
    level endon( "game_ended" );

    for (;;)
    {
        level waittill( "joined_team", var_0 );

        if ( maps\mp\_utility::gamehasstarted() )
            var_0.siegelatecomer = 1;
    }
}

onspawnplayer()
{
    maps\mp\_utility::setextrascore0( 0 );

    if ( isdefined( self.pers["captures"] ) )
        maps\mp\_utility::setextrascore0( self.pers["captures"] );

    level notify( "spawned_player" );
}

onplayerdisconnect()
{
    self waittill( "disconnect" );

    foreach ( var_1 in self._domflageffect )
    {
        if ( isdefined( var_1 ) )
            var_1 delete();
    }
}

checkallowspectating()
{
    wait 0.05;
    var_0 = 0;

    if ( !level.alivecount[game["attackers"]] )
    {
        level.spectateoverride[game["attackers"]].allowenemyspectate = 1;
        var_0 = 1;
    }

    if ( !level.alivecount[game["defenders"]] )
    {
        level.spectateoverride[game["defenders"]].allowenemyspectate = 1;
        var_0 = 1;
    }

    if ( var_0 )
        maps\mp\gametypes\_spectating::updatespectatesettings();
}

initgametypeawards()
{
    maps\mp\_awards::initstataward( "pointscaptured", 0, maps\mp\_awards::highestwins );
}

updategametypedvars()
{

}

domflags()
{
    level endon( "game_ended" );
    level.laststatus["allies"] = 0;
    level.laststatus["axis"] = 0;
    game["flagmodels"] = [];
    game["flagmodels"]["neutral"] = "prop_flag_neutral";
    game["flagmodels"]["allies"] = maps\mp\gametypes\_teams::getteamflagmodel( "allies" );
    game["flagmodels"]["axis"] = maps\mp\gametypes\_teams::getteamflagmodel( "axis" );
    var_0 = getentarray( "flag_primary", "targetname" );
    var_1 = getentarray( "flag_secondary", "targetname" );

    if ( var_0.size + var_1.size < 2 )
        return;

    level.flags = [];
    var_2 = "mp/siegeFlagPos.csv";
    var_3 = maps\mp\_utility::getmapname();
    var_4 = 1;

    for ( var_5 = 2; var_5 < 11; var_5++ )
    {
        var_6 = tablelookup( var_2, var_4, var_3, var_5 );

        if ( var_6 != "" )
            setflagpositions( var_5, float( var_6 ) );
    }

    for ( var_7 = 0; var_7 < var_0.size; var_7++ )
        level.flags[level.flags.size] = var_0[var_7];

    for ( var_7 = 0; var_7 < var_1.size; var_7++ )
        level.flags[level.flags.size] = var_1[var_7];

    level.domflags = [];

    for ( var_7 = 0; var_7 < level.flags.size; var_7++ )
    {
        var_8 = level.flags[var_7];
        var_8.origin = getflagpos( var_8.script_label, var_8.origin );

        if ( isdefined( var_8.target ) )
            var_9[0] = getent( var_8.target, "targetname" );
        else
        {
            var_9[0] = spawn( "script_model", var_8.origin );
            var_9[0].angles = var_8.angles;
        }

        var_10 = maps\mp\gametypes\_gameobjects::createuseobject( "neutral", var_8, var_9, ( 0, 0, 100 ) );
        var_10 maps\mp\gametypes\_gameobjects::allowuse( "enemy" );
        var_10 maps\mp\gametypes\_gameobjects::setusetime( getdvarfloat( "scr_siege_caprate" ) );
        var_10 maps\mp\gametypes\_gameobjects::setusetext( &"MP_SECURING_POSITION" );
        var_11 = var_10 maps\mp\gametypes\_gameobjects::getlabel();
        var_10.label = var_11;
        var_10 maps\mp\gametypes\_gameobjects::set2dicon( "friendly", "waypoint_defend" + var_11 );
        var_10 maps\mp\gametypes\_gameobjects::set3dicon( "friendly", "waypoint_defend" + var_11 );
        var_10 maps\mp\gametypes\_gameobjects::set2dicon( "enemy", "waypoint_captureneutral" + var_11 );
        var_10 maps\mp\gametypes\_gameobjects::set3dicon( "enemy", "waypoint_captureneutral" + var_11 );
        var_10 maps\mp\gametypes\_gameobjects::setvisibleteam( "any" );
        var_10.onuse = ::onuse;
        var_10.onbeginuse = ::onbeginuse;
        var_10.onuseupdate = ::onuseupdate;
        var_10.onenduse = ::onenduse;
        var_10.nousebar = 1;
        var_10.id = "domFlag";
        var_10.firstcapture = 1;
        var_10.prevteam = "neutral";
        var_10.flagcapsuccess = 0;
        var_12 = var_9[0].origin + ( 0, 0, 32 );
        var_13 = var_9[0].origin + ( 0, 0, -32 );
        var_14 = bullettrace( var_12, var_13, 0, undefined );
        var_10.baseeffectpos = var_14["position"];
        var_15 = vectortoangles( var_14["normal"] );
        var_10.baseeffectforward = anglestoforward( var_15 );
        var_10 thread setflagneutral();
        level.flags[var_7].useobj = var_10;
        var_10.levelflag = level.flags[var_7];
        level.domflags[level.domflags.size] = var_10;
    }

    var_16 = maps\mp\gametypes\_spawnlogic::getspawnpointarray( "mp_dom_spawn_axis_start" );
    var_17 = maps\mp\gametypes\_spawnlogic::getspawnpointarray( "mp_dom_spawn_allies_start" );
    level.startpos["allies"] = var_17[0].origin;
    level.startpos["axis"] = var_16[0].origin;
    level.bestspawnflag = [];
    level.bestspawnflag["allies"] = getunownedflagneareststart( "allies", undefined );
    level.bestspawnflag["axis"] = getunownedflagneareststart( "axis", level.bestspawnflag["allies"] );

    if ( getdvarint( "scr_siege_precap" ) )
    {
        storecenterflag();
        var_18 = [];
        var_18[var_18.size] = level.centerflag;

        if ( game["switchedsides"] )
        {
            level.closestalliesflag = getunownedflagneareststart( "axis", level.centerflag );
            var_18[var_18.size] = level.closestalliesflag;
            level.closestaxisflag = getunownedflagneareststart( "allies", var_18 );
        }
        else
        {
            level.closestalliesflag = getunownedflagneareststart( "allies", level.centerflag );
            var_18[var_18.size] = level.closestalliesflag;
            level.closestaxisflag = getunownedflagneareststart( "axis", var_18 );
        }

        level.closestalliesflag.useobj setflagcaptured( "allies", "neutral", undefined, 1 );
        level.closestaxisflag.useobj setflagcaptured( "axis", "neutral", undefined, 1 );
    }

    flagsetup();
}

setflagpositions( var_0, var_1 )
{
    switch ( var_0 )
    {
        case 2:
            level.siege_a_xpos = var_1;
            break;
        case 3:
            level.siege_a_ypos = var_1;
            break;
        case 4:
            level.siege_a_zpos = var_1;
            break;
        case 5:
            level.siege_b_xpos = var_1;
            break;
        case 6:
            level.siege_b_ypos = var_1;
            break;
        case 7:
            level.siege_b_zpos = var_1;
            break;
        case 8:
            level.siege_c_xpos = var_1;
            break;
        case 9:
            level.siege_c_ypos = var_1;
            break;
        case 10:
            level.siege_c_zpos = var_1;
            break;
    }
}

getflagpos( var_0, var_1 )
{
    var_2 = var_1;

    if ( var_0 == "_a" )
    {
        if ( isdefined( level.siege_a_xpos ) && isdefined( level.siege_a_ypos ) && isdefined( level.siege_a_zpos ) )
            var_2 = ( level.siege_a_xpos, level.siege_a_ypos, level.siege_a_zpos );
    }
    else if ( var_0 == "_b" )
    {
        if ( isdefined( level.siege_b_xpos ) && isdefined( level.siege_b_ypos ) && isdefined( level.siege_b_zpos ) )
            var_2 = ( level.siege_b_xpos, level.siege_b_ypos, level.siege_b_zpos );
    }
    else if ( isdefined( level.siege_c_xpos ) && isdefined( level.siege_c_ypos ) && isdefined( level.siege_c_zpos ) )
        var_2 = ( level.siege_c_xpos, level.siege_c_ypos, level.siege_c_zpos );

    return var_2;
}

storecenterflag()
{
    var_0 = undefined;

    foreach ( var_2 in level.flags )
    {
        if ( var_2.script_label == "_b" )
            level.centerflag = var_2;
    }
}

watchflagtimerpause()
{
    level endon( "game_ended" );

    for (;;)
    {
        level waittill( "flag_capturing", var_0 );

        if ( getdvarint( "scr_siege_rushtimer" ) )
        {
            var_1 = maps\mp\_utility::getotherteam( var_0.prevteam );

            if ( var_0.prevteam != "neutral" && ( isdefined( level.siegetimerstate ) && level.siegetimerstate != "pause" ) && !iswinningteam( var_1 ) )
            {
                level.gametimerbeeps = 0;
                level.siegetimerstate = "pause";
                pausecountdowntimer();

                if ( !flagownersalive( var_0.prevteam ) )
                    setwinner( var_1, var_0.prevteam + "_eliminated" );
            }
        }
    }
}

iswinningteam( var_0 )
{
    var_1 = 0;
    var_2 = getflagcount( var_0 );

    if ( var_2 == 2 )
        var_1 = 1;

    return var_1;
}

flagownersalive( var_0 )
{
    var_1 = 0;

    foreach ( var_3 in level.participants )
    {
        if ( isdefined( var_3 ) && var_3.team == var_0 && ( maps\mp\_utility::isreallyalive( var_3 ) || var_3.pers["lives"] > 0 ) )
        {
            var_1 = 1;
            break;
        }
    }

    return var_1;
}

pausecountdowntimer()
{
    setgameendtime( 0 );

    foreach ( var_1 in level.players )
        var_1 setclientomnvar( "ui_bomb_timer", 5 );

    level notify( "siege_timer_paused" );
}

watchflagtimerreset()
{
    level endon( "game_ended" );

    for (;;)
    {
        level waittill( "start_flag_captured", var_0 );

        if ( getdvarint( "scr_siege_rushtimer" ) )
        {
            if ( isdefined( level.siegetimerstate ) && level.siegetimerstate != "reset" )
            {
                level.gametimerbeeps = 0;
                level.siegetimeleft = undefined;
                level.siegetimerstate = "reset";
                notifyplayers( "siege_timer_reset" );
            }
        }

        level notify( "flag_end_use", var_0 );
    }
}

watchflagenduse()
{
    level endon( "game_ended" );

    for (;;)
    {
        var_0 = 0;
        var_1 = 0;
        level waittill( "flag_end_use", var_2 );
        var_0 = getflagcount( "allies" );
        var_1 = getflagcount( "axis" );

        if ( var_0 == 2 || var_1 == 2 )
        {
            if ( getdvarint( "scr_siege_rushtimer" ) )
            {
                if ( level.siegeflagcapturing.size == 0 && ( !var_2.flagcapsuccess || !isdefined( level.siegetimerstate ) || level.siegetimerstate != "start" ) )
                {
                    var_3 = getdvarfloat( "scr_siege_rushtimeramount" );

                    if ( isdefined( level.siegetimeleft ) )
                        var_3 = level.siegetimeleft;

                    var_4 = int( gettime() + var_3 * 1000 );

                    foreach ( var_6 in level.players )
                        var_6 setclientomnvar( "ui_bomb_timer", 0 );

                    level.timelimitoverride = 1;
                    maps\mp\gametypes\_gamelogic::pausetimer();
                    setgameendtime( var_4 );

                    if ( !isdefined( level.siegetimerstate ) || level.siegetimerstate == "pause" )
                    {
                        level.siegetimerstate = "start";
                        notifyplayers( "siege_timer_start" );
                    }

                    if ( !level.gametimerbeeps )
                        thread watchgametimer( var_3 );
                }
            }
        }
        else if ( var_0 == 3 )
            setwinner( "allies", "score_limit_reached" );
        else if ( var_1 == 3 )
            setwinner( "axis", "score_limit_reached" );

        var_2.prevteam = var_2.ownerteam;
    }
}

watchgameinactive()
{
    level endon( "game_ended" );
    level endon( "flag_capturing" );
    var_0 = getdvarfloat( "scr_siege_timelimit" );

    if ( var_0 > 0 )
    {
        var_1 = var_0 * 60 - 1;

        while ( var_1 > 0 )
        {
            var_1 = var_1 - 1;
            wait 1;
        }

        level.siegegameinactive = 1;
    }
}

watchgamestart()
{
    level endon( "game_ended" );
    maps\mp\_utility::gameflagwait( "prematch_done" );

    while ( !havespawnedplayers() )
        common_scripts\utility::waitframe();

    level.gamehasstarted = 1;
}

havespawnedplayers()
{
    if ( level.teambased )
        return level.hasspawned["axis"] && level.hasspawned["allies"];

    return level.maxplayercount > 1;
}

watchgametimer( var_0 )
{
    level endon( "game_ended" );
    level endon( "siege_timer_paused" );
    level endon( "siege_timer_reset" );
    var_1 = var_0;
    var_2 = spawn( "script_origin", ( 0, 0, 0 ) );
    var_2 hide();
    level.gametimerbeeps = 1;

    while ( var_1 > 0 )
    {
        var_1 = var_1 - 1;
        level.siegetimeleft = var_1;

        if ( var_1 <= 30 )
        {
            if ( var_1 != 0 )
                var_2 playsound( "ui_mp_timer_countdown" );
        }

        wait 1;
    }

    ontimelimit();
}

getflagcount( var_0 )
{
    var_1 = 0;

    foreach ( var_3 in level.domflags )
    {
        if ( var_3.ownerteam == var_0 && !isbeingcaptured( var_3 ) )
            var_1 = var_1 + 1;
    }

    return var_1;
}

isbeingcaptured( var_0 )
{
    var_1 = 0;

    if ( isdefined( var_0 ) )
    {
        if ( level.siegeflagcapturing.size > 0 )
        {
            foreach ( var_3 in level.siegeflagcapturing )
            {
                if ( var_0.label == var_3 )
                    var_1 = 1;
            }
        }
    }

    return var_1;
}

setwinner( var_0, var_1 )
{
    if ( var_0 != "tie" )
        level.finalkillcam_winner = var_0;
    else
        level.finalkillcam_winner = "none";

    foreach ( var_3 in level.players )
    {
        if ( !isai( var_3 ) )
        {
            var_3 setclientomnvar( "ui_dom_securing", 0 );
            var_3 setclientomnvar( "ui_bomb_timer", 0 );
        }
    }

    thread maps\mp\gametypes\_gamelogic::endgame( var_0, game["end_reason"][var_1] );
}

onbeginuse( var_0 )
{
    var_1 = maps\mp\gametypes\_gameobjects::getownerteam();
    self.didstatusnotify = 0;
    maps\mp\gametypes\_gameobjects::setusetime( getdvarfloat( "scr_siege_caprate" ) );
    level.siegeflagcapturing[level.siegeflagcapturing.size] = self.label;
    level notify( "flag_capturing", self );
}

onuse( var_0 )
{
    var_1 = var_0.team;
    var_2 = maps\mp\gametypes\_gameobjects::getownerteam();
    self.capturetime = gettime();
    setflagcaptured( var_1, var_2, var_0 );
    level.usestartspawns = 0;

    if ( var_2 == "neutral" )
    {
        var_3 = maps\mp\_utility::getotherteam( var_1 );
        thread maps\mp\_utility::printandsoundoneveryone( var_1, var_3, undefined, undefined, "mp_dom_flag_captured", undefined, var_0 );

        if ( getteamflagcount( var_1 ) < level.flags.size )
        {
            statusdialog( "secured" + self.label, var_1, 1 );
            statusdialog( "enemy_has" + self.label, var_3, 1 );
        }
    }

    var_0 maps\mp\_events::giveobjectivepointstreaks();
    thread giveflagcapturexp( self.touchlist[var_1] );
}

onuseupdate( var_0, var_1, var_2 )
{
    var_3 = maps\mp\gametypes\_gameobjects::getownerteam();

    if ( var_1 > 0.05 && var_2 && !self.didstatusnotify )
    {
        if ( var_3 == "neutral" )
        {
            statusdialog( "securing" + self.label, var_0 );
            self.prevownerteam = maps\mp\_utility::getotherteam( var_0 );
        }
        else
        {
            statusdialog( "losing" + self.label, var_3, 1 );
            statusdialog( "securing" + self.label, var_0 );
        }

        maps\mp\gametypes\_gameobjects::set2dicon( "enemy", "waypoint_taking" + self.label );
        maps\mp\gametypes\_gameobjects::set3dicon( "enemy", "waypoint_taking" + self.label );
        maps\mp\gametypes\_gameobjects::set2dicon( "friendly", "waypoint_losing" + self.label );
        maps\mp\gametypes\_gameobjects::set3dicon( "friendly", "waypoint_losing" + self.label );
        self.didstatusnotify = 1;
    }
}

onenduse( var_0, var_1, var_2 )
{
    if ( isplayer( var_1 ) )
    {
        var_1 setclientomnvar( "ui_dom_securing", 0 );
        var_1.ui_dom_securing = undefined;
    }

    if ( var_2 )
    {
        self.flagcapsuccess = 1;
        level notify( "start_flag_captured", self );
    }
    else
    {
        self.flagcapsuccess = 0;
        level notify( "flag_end_use", self );
    }

    var_3 = maps\mp\gametypes\_gameobjects::getownerteam();

    if ( var_3 != "neutral" )
    {
        maps\mp\gametypes\_gameobjects::set2dicon( "enemy", "waypoint_capture" + self.label );
        maps\mp\gametypes\_gameobjects::set3dicon( "enemy", "waypoint_capture" + self.label );
        maps\mp\gametypes\_gameobjects::set2dicon( "friendly", "waypoint_defend" + self.label );
        maps\mp\gametypes\_gameobjects::set3dicon( "friendly", "waypoint_defend" + self.label );
    }
    else
    {
        maps\mp\gametypes\_gameobjects::set2dicon( "enemy", "waypoint_captureneutral" + self.label );
        maps\mp\gametypes\_gameobjects::set3dicon( "enemy", "waypoint_captureneutral" + self.label );
        maps\mp\gametypes\_gameobjects::set2dicon( "friendly", "waypoint_captureneutral" + self.label );
        maps\mp\gametypes\_gameobjects::set3dicon( "friendly", "waypoint_captureneutral" + self.label );
    }

    level.siegeflagcapturing = common_scripts\utility::array_remove( level.siegeflagcapturing, self.label );
}

onreset()
{

}

getunownedflagneareststart( var_0, var_1 )
{
    var_2 = undefined;
    var_3 = undefined;
    var_4 = undefined;

    foreach ( var_6 in level.flags )
    {
        if ( var_6.useobj getflagteam() != "neutral" )
            continue;

        var_7 = distancesquared( var_6.origin, level.startpos[var_0] );

        if ( isdefined( var_1 ) )
        {
            if ( !isflagexcluded( var_6, var_1 ) && ( !isdefined( var_2 ) || var_7 < var_3 ) )
            {
                var_3 = var_7;
                var_2 = var_6;
            }

            continue;
        }

        if ( !isdefined( var_2 ) || var_7 < var_3 )
        {
            var_3 = var_7;
            var_2 = var_6;
        }
    }

    return var_2;
}

isflagexcluded( var_0, var_1 )
{
    var_2 = 0;

    if ( isarray( var_1 ) )
    {
        foreach ( var_4 in var_1 )
        {
            if ( var_0 == var_4 )
            {
                var_2 = 1;
                break;
            }
        }
    }
    else if ( var_0 == var_1 )
        var_2 = 1;

    return var_2;
}

ondeadevent( var_0 )
{
    if ( maps\mp\_utility::gamehasstarted() )
    {
        if ( var_0 == "all" )
            ontimelimit();
        else if ( var_0 == game["attackers"] )
        {
            if ( getflagcount( var_0 ) == 2 )
                return;

            setwinner( game["defenders"], game["attackers"] + "_eliminated" );
        }
        else if ( var_0 == game["defenders"] )
        {
            if ( getflagcount( var_0 ) == 2 )
                return;

            setwinner( game["attackers"], game["defenders"] + "_eliminated" );
        }
    }
}

ononeleftevent( var_0 )
{
    var_1 = maps\mp\_utility::getlastlivingplayer( var_0 );
    var_1 thread givelastonteamwarning();
}

onplayerkilled( var_0, var_1, var_2, var_3, var_4, var_5, var_6, var_7, var_8, var_9 )
{
    if ( !isplayer( var_1 ) || var_1.team == self.team )
        return;

    var_10 = 0;
    var_11 = 0;
    var_12 = self;

    foreach ( var_14 in var_12.touchtriggers )
    {
        if ( var_14 != level.flags[0] && var_14 != level.flags[1] && var_14 != level.flags[2] )
            continue;

        var_15 = var_14.useobj.ownerteam;
        var_16 = var_12.team;

        if ( var_15 == "neutral" )
            continue;

        if ( var_16 == var_15 )
        {
            var_10 = 1;
            var_1 thread maps\mp\gametypes\_hud_message::splashnotify( "assault", maps\mp\gametypes\_rank::getscoreinfovalue( "assault" ) );
            var_1 thread maps\mp\gametypes\_rank::giverankxp( "assault" );
            maps\mp\gametypes\_gamescore::giveplayerscore( "assault", var_1 );
            thread maps\mp\_matchdata::logkillevent( var_9, "defending" );
            continue;
        }

        var_11 = 1;
        var_1 thread maps\mp\gametypes\_hud_message::splashnotify( "defend", maps\mp\gametypes\_rank::getscoreinfovalue( "defend" ) );
        var_1 thread maps\mp\gametypes\_rank::giverankxp( "defend" );
        maps\mp\gametypes\_gamescore::giveplayerscore( "defend", var_1 );
        var_1 maps\mp\_utility::incpersstat( "defends", 1 );
        var_1 maps\mp\gametypes\_persistence::statsetchild( "round", "defends", var_1.pers["defends"] );
        var_1 maps\mp\gametypes\_missions::processchallenge( "ch_domprotector" );
        thread maps\mp\_matchdata::logkillevent( var_9, "assaulting" );
    }

    foreach ( var_14 in var_1.touchtriggers )
    {
        if ( var_14 != level.flags[0] && var_14 != level.flags[1] && var_14 != level.flags[2] )
            continue;

        var_15 = var_14.useobj.ownerteam;
        var_19 = var_1.team;

        if ( var_15 == "neutral" )
            continue;

        if ( var_19 != var_15 )
        {
            if ( !var_10 )
                var_1 thread maps\mp\gametypes\_hud_message::splashnotify( "assault", maps\mp\gametypes\_rank::getscoreinfovalue( "assault" ) );

            var_1 thread maps\mp\gametypes\_rank::giverankxp( "assault" );
            maps\mp\gametypes\_gamescore::giveplayerscore( "assault", var_1 );
            thread maps\mp\_matchdata::logkillevent( var_9, "defending" );
        }
    }

    foreach ( var_14 in level.flags )
    {
        var_15 = var_14.useobj.ownerteam;
        var_19 = var_1.team;
        var_22 = distancesquared( var_14.origin, var_12.origin );
        var_23 = 90000;

        if ( var_19 == var_15 && var_22 < var_23 )
        {
            if ( !var_11 )
                var_1 thread maps\mp\gametypes\_hud_message::splashnotify( "defend", maps\mp\gametypes\_rank::getscoreinfovalue( "defend" ) );

            var_1 thread maps\mp\gametypes\_rank::giverankxp( "defend" );
            maps\mp\gametypes\_gamescore::giveplayerscore( "defend", var_1 );
            var_1 maps\mp\_utility::incpersstat( "defends", 1 );
            var_1 maps\mp\gametypes\_persistence::statsetchild( "round", "defends", var_1.pers["defends"] );
            thread maps\mp\_matchdata::logkillevent( var_9, "assaulting" );
        }
    }
}

givelastonteamwarning()
{
    self endon( "death" );
    self endon( "disconnect" );
    level endon( "game_ended" );
    maps\mp\_utility::waittillrecoveredhealth( 3 );
    var_0 = maps\mp\_utility::getotherteam( self.pers["team"] );
    level thread maps\mp\_utility::teamplayercardsplash( "callout_lastteammemberalive", self, self.pers["team"] );
    level thread maps\mp\_utility::teamplayercardsplash( "callout_lastenemyalive", self, var_0 );
    level notify( "last_alive", self );
    maps\mp\gametypes\_missions::lastmansd();
}

ontimelimit()
{
    if ( isdefined( level.siegegameinactive ) )
        level thread maps\mp\gametypes\_gamelogic::forceend();
    else
    {
        var_0 = getflagcount( "allies" );
        var_1 = getflagcount( "axis" );

        if ( var_0 > var_1 )
            setwinner( "allies", "time_limit_reached" );
        else if ( var_1 > var_0 )
            setwinner( "axis", "time_limit_reached" );
        else
            setwinner( "tie", "time_limit_reached" );
    }
}

statusdialog( var_0, var_1, var_2 )
{
    var_3 = gettime();

    if ( gettime() < level.laststatus[var_1] + 5000 && ( !isdefined( var_2 ) || !var_2 ) )
        return;

    thread delayedleaderdialog( var_0, var_1 );
    level.laststatus[var_1] = gettime();
}

delayedleaderdialog( var_0, var_1 )
{
    level endon( "game_ended" );
    wait 0.1;
    maps\mp\_utility::waittillslowprocessallowed();
    maps\mp\_utility::leaderdialog( var_0, var_1 );
}

teamrespawn( var_0, var_1 )
{
    foreach ( var_3 in level.participants )
    {
        if ( isdefined( var_3 ) && var_3.team == var_0 && !maps\mp\_utility::isreallyalive( var_3 ) && !common_scripts\utility::array_contains( level.alive_players[var_3.team], var_3 ) && ( !isdefined( var_3.waitingtoselectclass ) || !var_3.waitingtoselectclass ) )
        {
            if ( isdefined( var_3.siegelatecomer ) && var_3.siegelatecomer )
                var_3.siegelatecomer = 0;

            var_3 maps\mp\gametypes\_playerlogic::incrementalivecount( var_3.team );
            var_3.alreadyaddedtoalivecount = 1;
            var_3 thread waitillcanspawnclient();
            var_3 thread maps\mp\gametypes\_hud_message::splashnotify( "sr_respawned" );
            level notify( "sr_player_respawned", var_3 );
            var_3 maps\mp\_utility::leaderdialogonplayer( "revived" );
            var_1 maps\mp\gametypes\_missions::processchallenge( "ch_rescuer" );

            if ( !isdefined( var_1.rescuedplayers ) )
                var_1.rescuedplayers = [];

            var_1.rescuedplayers[var_3.guid] = 1;

            if ( var_1.rescuedplayers.size == 4 )
                var_1 maps\mp\gametypes\_missions::processchallenge( "ch_helpme" );
        }
    }
}

waitillcanspawnclient()
{
    self endon( "started_spawnPlayer" );

    for (;;)
    {
        wait 0.05;

        if ( isdefined( self ) && ( self.sessionstate == "spectator" || !maps\mp\_utility::isreallyalive( self ) ) )
        {
            self.pers["lives"] = 1;
            maps\mp\gametypes\_playerlogic::spawnclient();
            continue;
        }

        return;
    }
}

notifyplayers( var_0 )
{
    foreach ( var_2 in level.players )
        var_2 thread maps\mp\gametypes\_hud_message::splashnotify( var_0 );

    level notify( "match_ending_soon", "time" );
    level notify( var_0 );
}

resetflagbaseeffect()
{
    var_0 = maps\mp\gametypes\_gameobjects::getownerteam();

    if ( var_0 == "neutral" )
        playflagneutralfx();
    else
    {
        foreach ( var_2 in level.players )
            showcapturedbaseeffecttoplayer( var_0, var_2 );
    }
}

refreshfreecambasefx()
{
    self endon( "disconnect" );
    level endon( "game_ended" );

    for (;;)
    {
        self waittill( "luinotifyserver", var_0, var_1 );

        if ( var_0 == "mlg_view_change" )
        {
            foreach ( var_3 in level.domflags )
            {
                if ( var_3.ownerteam != "neutral" )
                    var_3 showcapturedbaseeffecttoplayer( var_3.ownerteam, self );
            }
        }
    }
}

showcapturedbaseeffecttoplayer( var_0, var_1 )
{
    if ( isdefined( var_1._domflageffect[self.label] ) )
        var_1._domflageffect[self.label] delete();

    var_2 = undefined;
    var_3 = var_1.team;
    var_4 = var_1 ismlgspectator();

    if ( var_4 )
        var_3 = var_1 getmlgspectatorteam();
    else if ( var_3 == "spectator" )
        var_3 = "allies";

    if ( var_3 == var_0 )
        var_2 = spawnfxforclient( level.flagbasefxid["friendly"], self.baseeffectpos, var_1, self.baseeffectforward );
    else
        var_2 = spawnfxforclient( level.flagbasefxid["enemy"], self.baseeffectpos, var_1, self.baseeffectforward );

    var_1._domflageffect[self.label] = var_2;
    triggerfx( var_2 );
}

setflagneutral()
{
    self notify( "flag_neutral" );
    maps\mp\gametypes\_gameobjects::setownerteam( "neutral" );
    self.visuals[0] setmodel( game["flagmodels"]["neutral"] );

    foreach ( var_1 in level.players )
    {
        var_2 = var_1._domflageffect[self.label];

        if ( isdefined( var_2 ) )
            var_2 delete();
    }

    playflagneutralfx();
}

playflagneutralfx()
{
    if ( isdefined( self.neutralflagfx ) )
        self.neutralflagfx delete();

    self.neutralflagfx = spawnfx( level.flagbasefxid["neutral"], self.baseeffectpos, self.baseeffectforward );
    triggerfx( self.neutralflagfx );
}

getteamflagcount( var_0 )
{
    var_1 = 0;

    for ( var_2 = 0; var_2 < level.flags.size; var_2++ )
    {
        if ( level.domflags[var_2] maps\mp\gametypes\_gameobjects::getownerteam() == var_0 )
            var_1++;
    }

    return var_1;
}

getflagteam()
{
    return maps\mp\gametypes\_gameobjects::getownerteam();
}

flagsetup()
{
    foreach ( var_1 in level.domflags )
    {
        switch ( var_1.label )
        {
            case "_a":
                var_1.dompointnumber = 0;
                break;
            case "_b":
                var_1.dompointnumber = 1;
                break;
            case "_c":
                var_1.dompointnumber = 2;
                break;
        }
    }

    var_3 = maps\mp\gametypes\_spawnlogic::getspawnpointarray( "mp_dom_spawn" );

    foreach ( var_5 in var_3 )
    {
        var_5.dompointa = 0;
        var_5.dompointb = 0;
        var_5.dompointc = 0;
        var_5.nearflagpoint = getnearestflagpoint( var_5 );

        switch ( var_5.nearflagpoint.useobj.dompointnumber )
        {
            case 0:
                var_5.dompointa = 1;
                break;
            case 1:
                var_5.dompointb = 1;
                break;
            case 2:
                var_5.dompointc = 1;
                break;
        }
    }
}

getnearestflagpoint( var_0 )
{
    var_1 = maps\mp\gametypes\_spawnlogic::ispathdataavailable();
    var_2 = undefined;
    var_3 = undefined;

    foreach ( var_5 in level.domflags )
    {
        var_6 = undefined;

        if ( var_1 )
            var_6 = getpathdist( var_0.origin, var_5.levelflag.origin, 999999 );

        if ( !isdefined( var_6 ) || var_6 == -1 )
            var_6 = distancesquared( var_5.levelflag.origin, var_0.origin );

        if ( !isdefined( var_2 ) || var_6 < var_3 )
        {
            var_2 = var_5;
            var_3 = var_6;
        }
    }

    return var_2.levelflag;
}

giveflagcapturexp( var_0 )
{
    level endon( "game_ended" );
    var_1 = maps\mp\gametypes\_gameobjects::getearliestclaimplayer();

    if ( isdefined( var_1.owner ) )
        var_1 = var_1.owner;

    level.lastcaptime = gettime();

    if ( isplayer( var_1 ) )
    {
        level thread maps\mp\_utility::teamplayercardsplash( "callout_securedposition" + self.label, var_1 );
        var_1 thread maps\mp\_matchdata::loggameevent( "capture", var_1.origin );
    }

    var_2 = getarraykeys( var_0 );

    for ( var_3 = 0; var_3 < var_2.size; var_3++ )
    {
        var_4 = var_0[var_2[var_3]].player;

        if ( isdefined( var_4.owner ) )
            var_4 = var_4.owner;

        if ( !isplayer( var_4 ) )
            continue;

        var_4 thread maps\mp\gametypes\_hud_message::splashnotify( "capture", maps\mp\gametypes\_rank::getscoreinfovalue( "capture" ) );
        var_4 thread updatecpm();
        var_4 thread maps\mp\gametypes\_rank::giverankxp( "capture", maps\mp\gametypes\_rank::getscoreinfovalue( "capture" ) * var_4 getcapxpscale() );
        maps\mp\gametypes\_gamescore::giveplayerscore( "capture", var_4 );
        var_4 maps\mp\_utility::incplayerstat( "pointscaptured", 1 );
        var_4 maps\mp\_utility::incpersstat( "captures", 1 );
        var_4 maps\mp\gametypes\_persistence::statsetchild( "round", "captures", var_4.pers["captures"] );
        var_4 maps\mp\gametypes\_missions::processchallenge( "ch_domcap" );
        var_4 maps\mp\_utility::setextrascore0( var_4.pers["captures"] );

        if ( var_4 != var_1 )
            var_4 maps\mp\_events::giveobjectivepointstreaks();

        wait 0.05;
    }
}

getcapxpscale()
{
    if ( self.cpm < 4 )
        return 1;
    else
        return 0.25;
}

updatecpm()
{
    if ( !isdefined( self.cpm ) )
    {
        self.numcaps = 0;
        self.cpm = 0;
    }

    self.numcaps++;

    if ( maps\mp\_utility::getminutespassed() < 1 )
        return;

    self.cpm = self.numcaps / maps\mp\_utility::getminutespassed();
}

setflagcaptured( var_0, var_1, var_2, var_3 )
{
    var_4 = maps\mp\gametypes\_gameobjects::getlabel();
    maps\mp\gametypes\_gameobjects::setownerteam( var_0 );
    maps\mp\gametypes\_gameobjects::set2dicon( "enemy", "waypoint_capture" + var_4 );
    maps\mp\gametypes\_gameobjects::set3dicon( "enemy", "waypoint_capture" + var_4 );
    maps\mp\gametypes\_gameobjects::set2dicon( "friendly", "waypoint_defend" + self.label );
    maps\mp\gametypes\_gameobjects::set3dicon( "friendly", "waypoint_defend" + self.label );
    self.visuals[0] setmodel( game["flagmodels"][var_0] );

    if ( isdefined( self.neutralflagfx ) )
        self.neutralflagfx delete();

    foreach ( var_6 in level.players )
        showcapturedbaseeffecttoplayer( var_0, var_6 );

    if ( !isdefined( var_3 ) )
    {
        if ( var_1 != "neutral" )
        {
            statusdialog( "secured" + self.label, var_0, 1 );
            statusdialog( "lost" + self.label, var_1, 1 );
            maps\mp\_utility::playsoundonplayers( "mp_dom_flag_lost", var_1 );
            level.lastcaptime = gettime();
        }

        teamrespawn( var_0, var_2 );
        self.firstcapture = 0;
    }

    thread baseeffectswaitforjoined();
}

baseeffectswaitforjoined()
{
    level endon( "game_ended" );
    self endon( "flag_neutral" );

    for (;;)
    {
        level waittill( "joined_team", var_0 );

        if ( isdefined( var_0._domflageffect[self.label] ) )
        {
            var_0._domflageffect[self.label] delete();
            var_0._domflageffect[self.label] = undefined;
        }

        if ( var_0.team != "spectator" )
            showcapturedbaseeffecttoplayer( self.ownerteam, var_0 );
    }
}
