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

main()
{
    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::registerscorelimitdvar( level.gametype, 16 );
        maps\mp\_utility::registertimelimitdvar( level.gametype, 5 );
        maps\mp\_utility::registerroundlimitdvar( level.gametype, 2 );
        maps\mp\_utility::registerroundswitchdvar( level.gametype, 1, 0, 1 );
        maps\mp\_utility::registerwinlimitdvar( level.gametype, 0 );
        maps\mp\_utility::registernumlivesdvar( level.gametype, 0 );
        level.matchrules_damagemultiplier = 0;
    }

    level.teambased = 1;
    level.objectivebased = 0;
    level.supportbuddyspawn = 0;
    level.onstartgametype = ::onstartgametype;
    level.getspawnpoint = ::getspawnpoint;
    level.ontimelimit = ::ontimelimit;
    level.onnormaldeath = ::onnormaldeath;
    level.onplayerkilled = ::onplayerkilled;
    level.onspawnplayer = ::onspawnplayer;
    level.initgametypeawards = ::initgametypeawards;
    level.spawnnodetype = common_scripts\utility::ter_op( getdvarint( "scr_altBlitzSpawns", 0 ) == 1, "mp_tdm_spawn", "mp_blitz_spawn" );

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

    game["dialog"]["gametype"] = "blitz";

    if ( getdvarint( "g_hardcore" ) )
        game["dialog"]["gametype"] = "hc_" + game["dialog"]["gametype"];

    game["dialog"]["offense_obj"] = "bltz_hint";
    game["dialog"]["defense_obj"] = "bltz_hint";
    game["dialog"]["bltz_e_scored"] = "bltz_e_scored";
    game["dialog"]["bltz_scored"] = "bltz_scored";
}

initializematchrules()
{
    maps\mp\_utility::setcommonrulesfrommatchrulesdata();
    setdynamicdvar( "scr_blitz_roundswitch", 1 );
    maps\mp\_utility::registerroundswitchdvar( "blitz", 1, 0, 1 );
    setdynamicdvar( "scr_blitz_roundlimit", 2 );
    maps\mp\_utility::registerroundlimitdvar( "blitz", 2 );
    setdynamicdvar( "scr_blitz_winlimit", 0 );
    maps\mp\_utility::registerwinlimitdvar( "blitz", 0 );
    setdynamicdvar( "scr_blitz_promode", 0 );
}

initeffects()
{
    level._effect["portal_fx_defend"] = loadfx( "vfx/gameplay/mp/core/vfx_marker_base_cyan" );
    level._effect["portal_fx_goal"] = loadfx( "vfx/gameplay/mp/core/vfx_marker_base_orange" );
    level._effect["portal_fx_closed"] = loadfx( "vfx/gameplay/mp/core/vfx_marker_base_grey" );
    level._effect["blitz_teleport"] = loadfx( "vfx/gameplay/mp/core/vfx_teleport_player" );
}

onstartgametype()
{
    maps\mp\_utility::setobjectivetext( "allies", &"OBJECTIVES_BLITZ" );
    maps\mp\_utility::setobjectivetext( "axis", &"OBJECTIVES_BLITZ" );

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

    maps\mp\_utility::setobjectivehinttext( "allies", &"OBJECTIVES_BLITZ_HINT" );
    maps\mp\_utility::setobjectivehinttext( "axis", &"OBJECTIVES_BLITZ_HINT" );
    setclientnamemode( "auto_change" );

    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;
    }

    initspawns();
    initeffects();
    var_2[0] = level.gametype;
    maps\mp\gametypes\_gameobjects::main( var_2 );
    createportals();
    level thread onplayerconnect();
    assignteamspawns();
    level thread runblitz();
}

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

    for (;;)
    {
        level waittill( "connected", var_0 );
        var_0 showspectatorportalfx();
        var_0 thread refreshfreecamportalfx();
        var_0 thread onplayerspawned();
    }
}

onplayerspawned()
{
    self waittill( "spawned_player" );
    showteamportalfx();
    thread refreshteamportalfx();
    thread refreshspectatorportalfx();
    thread clearfxondisconnect();
}

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

createportals()
{
    level.portals = [];

    if ( game["switchedsides"] )
    {
        var_0 = getent( "allies_portal", "targetname" );
        var_1 = getent( "axis_portal", "targetname" );
    }
    else
    {
        var_0 = getent( "axis_portal", "targetname" );
        var_1 = getent( "allies_portal", "targetname" );
    }

    level.portallist["axis"] = createportal( var_0, "axis" );
    level.portallist["allies"] = createportal( var_1, "allies" );
}

createportal( var_0, var_1 )
{
    var_2 = spawnstruct();
    var_2.origin = var_0.origin;
    var_2.ownerteam = var_1;
    var_2.open = 1;
    var_2.guarded = 0;
    var_2.trigger = var_0;

    if ( isdefined( level.matchrecording_generateid ) && isdefined( level.matchrecording_logevent ) )
    {
        if ( !isdefined( game["blitzPortalLogIDs"] ) )
            game["blitzPortalLogIDs"] = [];

        if ( !isdefined( game["blitzPortalLogIDs"][var_1] ) )
            game["blitzPortalLogIDs"][var_1] = [[ level.matchrecording_generateid ]]();

        var_3 = common_scripts\utility::ter_op( var_1 == "allies", 0, 1 );
        [[ level.matchrecording_logevent ]]( game["blitzPortalLogIDs"][var_1], undefined, "PORTAL", var_2.origin[0], var_2.origin[1], gettime(), var_3 );
    }

    return var_2;
}

assignteamspawns()
{
    var_0 = maps\mp\gametypes\_spawnlogic::getspawnpointarray( level.spawnnodetype );
    var_1 = maps\mp\gametypes\_spawnlogic::ispathdataavailable();
    level.teamspawnpoints["axis"] = [];
    level.teamspawnpoints["allies"] = [];
    level.teamspawnpoints["neutral"] = [];

    if ( getdvarint( "scr_altBlitzSpawns", 0 ) == 1 && level.portallist.size == 2 )
    {
        var_2 = level.portallist["allies"];
        var_3 = level.portallist["axis"];
        var_4 = ( var_2.origin[0], var_2.origin[1], 0 );
        var_5 = ( var_3.origin[0], var_3.origin[1], 0 );
        var_6 = var_5 - var_4;
        var_7 = length2d( var_6 );

        foreach ( var_9 in var_0 )
        {
            var_10 = ( var_9.origin[0], var_9.origin[1], 0 );
            var_11 = var_10 - var_4;
            var_12 = vectordot( var_11, var_6 );
            var_13 = var_12 / ( var_7 * var_7 );

            if ( var_13 < 0.33 )
            {
                var_9.teambase = var_2.ownerteam;
                level.teamspawnpoints[var_9.teambase][level.teamspawnpoints[var_9.teambase].size] = var_9;
                continue;
            }

            if ( var_13 > 0.67 )
            {
                var_9.teambase = var_3.ownerteam;
                level.teamspawnpoints[var_9.teambase][level.teamspawnpoints[var_9.teambase].size] = var_9;
                continue;
            }

            var_14 = undefined;
            var_15 = undefined;

            if ( var_1 )
                var_14 = getpathdist( var_9.origin, var_2.origin, 999999 );

            if ( isdefined( var_14 ) && var_14 != -1 )
                var_15 = getpathdist( var_9.origin, var_3.origin, 999999 );

            if ( !isdefined( var_15 ) || var_15 == -1 )
            {
                var_14 = distance2d( var_2.origin, var_9.origin );
                var_15 = distance2d( var_3.origin, var_9.origin );
            }

            var_16 = max( var_14, var_15 );
            var_17 = min( var_14, var_15 );
            var_18 = var_17 / var_16;

            if ( var_18 > 0.5 )
                level.teamspawnpoints["neutral"][level.teamspawnpoints["neutral"].size] = var_9;
        }
    }
    else
    {
        foreach ( var_9 in var_0 )
        {
            var_9.teambase = getnearestportalteam( var_9 );

            if ( var_9.teambase == "axis" )
            {
                level.teamspawnpoints["axis"][level.teamspawnpoints["axis"].size] = var_9;
                continue;
            }

            level.teamspawnpoints["allies"][level.teamspawnpoints["allies"].size] = var_9;
        }
    }
}

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

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

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

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

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

    return var_2.ownerteam;
}

onnormaldeath( var_0, var_1, var_2 )
{
    var_1 thread maps\mp\gametypes\_rank::xpeventpopup( "kill" );
}

runblitz()
{
    startportal( level.portallist["axis"] );
    startportal( level.portallist["allies"] );
}

startportal( var_0 )
{
    level thread runportalfx( var_0 );
    level thread runportalstatus( var_0 );
    level thread runportalthink( var_0 );
}

runportalfx( var_0 )
{
    level endon( "final_score_teleport" );
    level endon( "halftime_score_teleport" );
    level endon( "time_ended" );
    level endon( "force_end" );
    var_1 = blitzgetteam( var_0 );
    var_0 childthread onportalused( var_1 );
    var_0 childthread onportalready( var_1 );
}

onportalused( var_0 )
{
    for (;;)
    {
        level waittill( "portal_used", var_1 );

        if ( var_1 != var_0 )
            continue;

        hideopenportalfx( var_0 );
        showclosedportalfx( var_0 );
    }
}

onportalready( var_0 )
{
    for (;;)
    {
        level waittill( "portal_ready", var_1 );

        if ( var_1 != var_0 )
            continue;

        if ( !self.open )
            continue;

        hidecloseportalfx( var_0 );
        showopenportalfx( var_0 );
    }
}

hideopenportalfx( var_0 )
{
    foreach ( var_2 in level.players )
    {
        if ( !isdefined( var_2 ) || !isdefined( var_2.team ) )
            continue;

        if ( var_2.team == "allies" || var_2.team == "axis" )
        {
            if ( var_2.team == var_0 )
            {
                if ( isdefined( var_2.defend_fx_ent ) )
                    var_2.defend_fx_ent delete();
            }
            else if ( isdefined( var_2.goal_fx_ent ) )
                var_2.goal_fx_ent delete();

            continue;
        }

        var_3 = var_2 ismlgspectator();

        if ( var_3 && var_0 == var_2 getmlgspectatorteam() || !var_3 && var_0 == "allies" )
        {
            if ( isdefined( var_2.defend_fx_ent ) )
                var_2.defend_fx_ent delete();
        }
        else if ( isdefined( var_2.goal_fx_ent ) )
            var_2.goal_fx_ent delete();
    }
}

showopenportalfx( var_0 )
{
    foreach ( var_2 in level.players )
    {
        if ( !isdefined( var_2 ) || !isdefined( var_2.team ) )
            continue;

        if ( var_2.team == "allies" || var_2.team == "axis" )
        {
            if ( var_2.team == var_0 )
            {
                if ( !isdefined( var_2.defend_fx_ent ) )
                {
                    var_2.defend_fx_ent = spawnfxforclient( level._effect["portal_fx_defend"], self.origin, var_2 );
                    triggerfx( var_2.defend_fx_ent );
                }
            }
            else if ( !isdefined( var_2.goal_fx_ent ) )
            {
                var_2.goal_fx_ent = spawnfxforclient( level._effect["portal_fx_goal"], self.origin, var_2 );
                triggerfx( var_2.goal_fx_ent );
            }

            continue;
        }

        var_3 = var_2 ismlgspectator();

        if ( var_3 && var_0 == var_2 getmlgspectatorteam() || !var_3 && var_0 == "allies" )
        {
            if ( !isdefined( var_2.defend_fx_ent ) )
            {
                var_2.defend_fx_ent = spawnfxforclient( level._effect["portal_fx_defend"], self.origin, var_2 );
                triggerfx( var_2.defend_fx_ent );
            }
        }
        else if ( !isdefined( var_2.goal_fx_ent ) )
        {
            var_2.goal_fx_ent = spawnfxforclient( level._effect["portal_fx_goal"], self.origin, var_2 );
            triggerfx( var_2.goal_fx_ent );
        }
    }
}

hidecloseportalfx( var_0 )
{
    foreach ( var_2 in level.players )
    {
        if ( !isdefined( var_2 ) || !isdefined( var_2.team ) )
            continue;

        if ( var_2.team == "allies" || var_2.team == "axis" )
        {
            if ( var_2.team == var_0 )
            {
                if ( isdefined( var_2.closed_defend_fx_ent ) )
                    var_2.closed_defend_fx_ent delete();
            }
            else if ( isdefined( var_2.closed_goal_fx_ent ) )
                var_2.closed_goal_fx_ent delete();

            continue;
        }

        var_3 = var_2 ismlgspectator();

        if ( var_3 && var_0 == var_2 getmlgspectatorteam() || !var_3 && var_0 == "allies" )
        {
            if ( isdefined( var_2.closed_defend_fx_ent ) )
                var_2.closed_defend_fx_ent delete();
        }
        else if ( isdefined( var_2.closed_goal_fx_ent ) )
            var_2.closed_goal_fx_ent delete();
    }
}

showclosedportalfx( var_0 )
{
    foreach ( var_2 in level.players )
    {
        if ( !isdefined( var_2 ) || !isdefined( var_2.team ) )
            continue;

        if ( var_2.team == "allies" || var_2.team == "axis" )
        {
            if ( var_2.team == var_0 )
            {
                if ( !isdefined( var_2.closed_defend_fx_ent ) )
                {
                    var_2.closed_defend_fx_ent = spawnfxforclient( level._effect["portal_fx_closed"], self.origin, var_2 );
                    triggerfx( var_2.closed_defend_fx_ent );
                }
            }
            else if ( !isdefined( var_2.closed_goal_fx_ent ) )
            {
                var_2.closed_goal_fx_ent = spawnfxforclient( level._effect["portal_fx_closed"], self.origin, var_2 );
                triggerfx( var_2.closed_goal_fx_ent );
            }

            continue;
        }

        var_3 = var_2 ismlgspectator();

        if ( var_3 && var_0 == var_2 getmlgspectatorteam() || !var_3 && var_0 == "allies" )
        {
            if ( !isdefined( var_2.closed_defend_fx_ent ) )
            {
                var_2.closed_defend_fx_ent = spawnfxforclient( level._effect["portal_fx_closed"], self.origin, var_2 );
                triggerfx( var_2.closed_defend_fx_ent );
            }
        }
        else if ( !isdefined( var_2.closed_goal_fx_ent ) )
        {
            var_2.closed_goal_fx_ent = spawnfxforclient( level._effect["portal_fx_closed"], self.origin, var_2 );
            triggerfx( var_2.closed_goal_fx_ent );
        }
    }
}

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

    for (;;)
    {
        self waittill( "joined_team" );
        showteamportalfx();
    }
}

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

    for (;;)
    {
        self waittill( "joined_spectators" );
        showspectatorportalfx();
    }
}

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

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

        if ( var_0 == "mlg_view_change" )
            showspectatorportalfx();
    }
}

clearfxondisconnect()
{
    self waittill( "disconnect" );
    clearportalfx();
}

showteamportalfx()
{
    if ( self.team != "allies" && self.team != "axis" )
        return;

    var_0 = self.team;
    var_1 = maps\mp\_utility::getotherteam( var_0 );
    clearportalfx();

    if ( isdefined( level.portallist[var_0] ) && isdefined( level.portallist[var_1] ) )
    {
        if ( level.portallist[var_0].open && !level.portallist[var_0].guarded )
        {
            self.defend_fx_ent = spawnfxforclient( level._effect["portal_fx_defend"], level.portallist[var_0].origin, self );
            triggerfx( self.defend_fx_ent );
        }
        else
        {
            self.closed_defend_fx_ent = spawnfxforclient( level._effect["portal_fx_closed"], level.portallist[var_0].origin, self );
            triggerfx( self.closed_defend_fx_ent );
        }

        if ( level.portallist[var_1].open && !level.portallist[var_1].guarded )
        {
            self.goal_fx_ent = spawnfxforclient( level._effect["portal_fx_goal"], level.portallist[var_1].origin, self );
            triggerfx( self.goal_fx_ent );
        }
        else
        {
            self.closed_goal_fx_ent = spawnfxforclient( level._effect["portal_fx_closed"], level.portallist[var_1].origin, self );
            triggerfx( self.closed_goal_fx_ent );
        }
    }
}

showspectatorportalfx()
{
    clearportalfx();
    var_0 = "allies";
    var_1 = "axis";
    var_2 = self ismlgspectator();

    if ( var_2 )
    {
        var_0 = self getmlgspectatorteam();
        var_1 = maps\mp\_utility::getotherteam( var_0 );
    }

    if ( isdefined( level.portallist[var_0] ) && isdefined( level.portallist[var_1] ) )
    {
        if ( level.portallist[var_0].open && !level.portallist[var_0].guarded )
        {
            self.defend_fx_ent = spawnfxforclient( level._effect["portal_fx_defend"], level.portallist[var_0].origin, self );
            triggerfx( self.defend_fx_ent );
        }
        else
        {
            self.closed_defend_fx_ent = spawnfxforclient( level._effect["portal_fx_closed"], level.portallist[var_0].origin, self );
            triggerfx( self.closed_defend_fx_ent );
        }

        if ( level.portallist[var_1].open && !level.portallist[var_1].guarded )
        {
            self.goal_fx_ent = spawnfxforclient( level._effect["portal_fx_goal"], level.portallist[var_1].origin, self );
            triggerfx( self.goal_fx_ent );
        }
        else
        {
            self.closed_goal_fx_ent = spawnfxforclient( level._effect["portal_fx_closed"], level.portallist[var_1].origin, self );
            triggerfx( self.closed_goal_fx_ent );
        }
    }
}

clearportalfx()
{
    if ( isdefined( self.defend_fx_ent ) )
        self.defend_fx_ent delete();

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

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

    if ( isdefined( self.closed_goal_fx_ent ) )
        self.closed_goal_fx_ent delete();
}

runportalstatus( var_0, var_1 )
{
    level endon( "final_score_teleport" );
    level endon( "halftime_score_teleport" );
    level endon( "time_ended" );
    level endon( "force_end" );
    var_2 = ( 0, 0, 72 );
    var_3 = blitzgetteam( var_0 );
    var_4 = maps\mp\_utility::getotherteam( var_3 );
    var_0.ownerteamid = maps\mp\gametypes\_gameobjects::getnextobjid();
    objective_add( var_0.ownerteamid, "active", var_0.origin + var_2, "waypoint_blitz_defend" );
    objective_team( var_0.ownerteamid, var_3 );
    var_0.enemyteamid = maps\mp\gametypes\_gameobjects::getnextobjid();
    objective_add( var_0.enemyteamid, "active", var_0.origin + var_2, "waypoint_blitz_goal" );
    objective_team( var_0.enemyteamid, var_4 );

    for (;;)
    {
        if ( var_0.open )
        {
            var_0.teamheadicon = var_0 maps\mp\_entityheadicons::setheadicon( var_3, "waypoint_blitz_defend", var_2, 4, 4, undefined, undefined, undefined, 1, undefined, 0 );
            var_0.enemyheadicon = var_0 maps\mp\_entityheadicons::setheadicon( var_4, "waypoint_blitz_goal", var_2, 4, 4, undefined, undefined, undefined, 1, undefined, 0 );
            objective_icon( var_0.ownerteamid, "waypoint_blitz_defend" );
            objective_icon( var_0.enemyteamid, "waypoint_blitz_goal" );
        }
        else if ( !isdefined( var_0.waiticonactive ) || !var_0.waiticonactive )
        {
            var_0.waiticonactive = 1;
            var_0 childthread refreshwaiticon( var_0, var_3, var_4, var_2 );
        }

        level common_scripts\utility::waittill_any( "portal_used", "portal_ready" );
    }
}

refreshwaiticon( var_0, var_1, var_2, var_3 )
{
    var_4 = 10;

    for ( var_5 = var_4; var_5 > 0; var_5-- )
    {
        if ( var_5 == var_4 )
        {
            var_0.teamheadicon = var_0 maps\mp\_entityheadicons::setheadicon( var_1, "blitz_time_" + var_5 + "_blue", var_3, 4, 4, undefined, undefined, undefined, 1, undefined, 0 );
            objective_icon( var_0.ownerteamid, "blitz_time_" + var_5 + "_blue" );
        }
        else
        {
            var_0.teamheadicon = var_0 maps\mp\_entityheadicons::setheadicon( var_1, "blitz_time_0" + var_5 + "_blue", var_3, 4, 4, undefined, undefined, undefined, 1, undefined, 0 );
            objective_icon( var_0.ownerteamid, "blitz_time_0" + var_5 + "_blue" );
        }

        if ( var_5 == var_4 )
        {
            var_0.enemyheadicon = var_0 maps\mp\_entityheadicons::setheadicon( var_2, "blitz_time_" + var_5 + "_orng", var_3, 4, 4, undefined, undefined, undefined, 1, undefined, 0 );
            objective_icon( var_0.enemyteamid, "blitz_time_" + var_5 + "_orng" );
        }
        else
        {
            var_0.enemyheadicon = var_0 maps\mp\_entityheadicons::setheadicon( var_2, "blitz_time_0" + var_5 + "_orng", var_3, 4, 4, undefined, undefined, undefined, 1, undefined, 0 );
            objective_icon( var_0.enemyteamid, "blitz_time_0" + var_5 + "_orng" );
        }

        maps\mp\gametypes\_hostmigration::waitlongdurationwithhostmigrationpause( 1 );
    }

    var_0.waiticonactive = 0;
}

runportalthink( var_0 )
{
    level endon( "final_score_teleport" );
    level endon( "halftime_score_teleport" );
    level endon( "time_ended" );
    level endon( "force_end" );
    var_1 = blitzgetteam( var_0 );
    var_2 = getdvarfloat( "scr_blitz_scoredelay", 10 );
    var_0 childthread guardwatch( var_1 );

    for (;;)
    {
        var_0.trigger waittill( "trigger", var_3 );

        if ( validscorercheck( var_3, var_0, var_1 ) )
        {
            var_0.open = 0;
            level notify( "portal_used", var_1 );
            playertriggeredportal( var_3, var_0, var_1 );
            maps\mp\gametypes\_hostmigration::waitlongdurationwithhostmigrationpause( var_2 );
            var_0.open = 1;
            level notify( "portal_ready", var_1 );
        }
    }
}

validscorercheck( var_0, var_1, var_2 )
{
    if ( !isplayer( var_0 ) )
        return 0;

    if ( var_0.team == var_2 || var_0.team == "spectator" )
        return 0;

    if ( checkguardedportal( var_1, var_2 ) )
        return 0;

    if ( var_0 islinked() )
        return 0;

    if ( isdefined( var_0.iscarrying ) && var_0.iscarrying )
        return 0;

    return 1;
}

playertriggeredportal( var_0, var_1, var_2 )
{
    maps\mp\_utility::leaderdialog( "bltz_e_scored", var_2 );
    maps\mp\_utility::leaderdialog( "bltz_scored", maps\mp\_utility::getotherteam( var_2 ) );
    maps\mp\gametypes\_gamescore::giveplayerscore( "capture", var_0 );
    var_0 thread maps\mp\gametypes\_rank::giverankxp( "capture" );
    var_0 maps\mp\killstreaks\_killstreaks::giveadrenaline( "capture" );
    giveteamscore( var_0.team );
    var_0 maps\mp\_utility::incplayerstat( "pointscaptured", 1 );
    var_0 maps\mp\_utility::incpersstat( "captures", 1 );
    var_0 maps\mp\gametypes\_persistence::statsetchild( "round", "captures", var_0.pers["captures"] );
    var_0 maps\mp\gametypes\_missions::processchallenge( "ch_blitz_score" );

    if ( var_0 maps\mp\gametypes\_missions::playerissprintsliding() )
        var_0 maps\mp\gametypes\_missions::processchallenge( "ch_saafe" );

    if ( isdefined( var_0.stuckbygrenade ) && isdefined( var_0.stuckbygrenade.owner ) )
        var_0.stuckbygrenade.owner maps\mp\gametypes\_missions::processchallenge( "ch_telefragged" );

    var_0 maps\mp\_utility::setextrascore0( var_0.pers["captures"] );
    var_3 = var_0 getspawnpoint();
    var_0 thread teleport_player( var_3.origin, var_3.angles );
}

guardwatch( var_0 )
{
    for (;;)
    {
        if ( checkguardedportal( self, var_0 ) )
        {
            level notify( "portal_used", var_0 );
            self.guarded = 1;
        }
        else
        {
            level notify( "portal_ready", var_0 );
            self.guarded = 0;
        }

        common_scripts\utility::waitframe();
    }
}

checkguardedportal( var_0, var_1 )
{
    foreach ( var_3 in level.participants )
    {
        if ( !isdefined( var_3 ) || !isdefined( var_3.team ) )
            continue;

        if ( var_3.team == var_1 && isalive( var_3 ) && !isdefined( var_3.fauxdead ) )
        {
            if ( distancesquared( var_0.origin, var_3.origin ) < 4300 )
                return 1;
        }
    }

    return 0;
}

giveteamscore( var_0 )
{
    maps\mp\gametypes\_gamescore::giveteamscoreforobjective( var_0, 1 );

    foreach ( var_2 in level.players )
    {
        var_3 = var_2.team;

        if ( var_2.team == "spectator" )
        {
            var_4 = var_2 getspectatingplayer();

            if ( isdefined( var_4 ) )
                var_3 = var_4.team;
            else
            {
                var_2 thread maps\mp\gametypes\_hud_message::splashnotifyurgent( "blitz_score_team" );
                continue;
            }
        }

        if ( var_3 == var_0 )
        {
            var_2 thread maps\mp\gametypes\_hud_message::splashnotifyurgent( "blitz_score_team" );
            continue;
        }

        var_2 thread maps\mp\gametypes\_hud_message::splashnotifyurgent( "blitz_score_enemy" );
    }
}

teleport_player( var_0, var_1 )
{
    self endon( "death" );
    self endon( "disconnect" );
    self.teleporting = 1;
    checkscorelimit();
    var_2 = 1;
    var_3 = create_client_overlay( "white", 1, self );
    var_3 thread fade_over_time( 0, var_2 );
    var_3 thread huddelete( var_2 );
    var_4 = self gettagorigin( "j_SpineUpper" );
    playfx( level._effect["blitz_teleport"], var_4 );

    if ( isdefined( self.setspawnpoint ) )
        maps\mp\perks\_perkfunctions::deleteti( self.setspawnpoint );

    self cancelmantle();
    self setorigin( var_0 );
    self setplayerangles( var_1 );
    self setstance( "stand" );
    wait( var_2 );
    self.teleporting = 0;
}

create_client_overlay( var_0, var_1, var_2 )
{
    if ( isdefined( var_2 ) )
        var_3 = newclienthudelem( var_2 );
    else
        var_3 = newhudelem();

    var_3.x = 0;
    var_3.y = 0;
    var_3 setshader( var_0, 640, 480 );
    var_3.alignx = "left";
    var_3.aligny = "top";
    var_3.sort = 1;
    var_3.horzalign = "fullscreen";
    var_3.vertalign = "fullscreen";
    var_3.alpha = var_1;
    var_3.foreground = 1;
    return var_3;
}

fade_over_time( var_0, var_1 )
{
    if ( isdefined( var_1 ) && var_1 > 0 )
        self fadeovertime( var_1 );

    self.alpha = var_0;

    if ( isdefined( var_1 ) && var_1 > 0 )
        wait( var_1 );
}

huddelete( var_0 )
{
    self endon( "death" );
    wait( var_0 );
    self destroy();
}

getspawnpoint()
{
    var_0 = blitzgetteam( self );
    var_1 = maps\mp\_utility::getotherteam( var_0 );

    if ( maps\mp\gametypes\_spawnlogic::shoulduseteamstartspawn() )
    {
        if ( game["switchedsides"] )
        {
            var_2 = maps\mp\gametypes\_spawnlogic::getspawnpointarray( "mp_blitz_spawn_" + var_1 + "_start" );
            var_3 = maps\mp\gametypes\_spawnlogic::getspawnpoint_startspawn( var_2 );
        }
        else
        {
            var_2 = maps\mp\gametypes\_spawnlogic::getspawnpointarray( "mp_blitz_spawn_" + var_0 + "_start" );
            var_3 = maps\mp\gametypes\_spawnlogic::getspawnpoint_startspawn( var_2 );
        }
    }
    else
    {
        var_4 = level.teamspawnpoints["neutral"].size > 0;
        var_2 = maps\mp\gametypes\_spawnlogic::getteamspawnpoints( var_0 );
        var_3 = maps\mp\gametypes\_spawnscoring::getspawnpoint_awayfromenemies( var_2, var_0, var_4 );

        if ( !isdefined( var_3 ) && var_4 )
        {
            var_2 = maps\mp\gametypes\_spawnlogic::getteamspawnpoints( "neutral" );
            var_3 = maps\mp\gametypes\_spawnscoring::getspawnpoint_awayfromenemies( var_2, var_0, 0 );
        }
    }

    return var_3;
}

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

    var_10 = self;
    var_11 = blitzgetteam( var_10 );
    var_12 = blitzgetteam( var_1 );

    if ( var_12 == var_11 )
        return;

    var_13 = level.portallist[var_12].origin;
    var_14 = level.portallist[var_11].origin;
    var_15 = 0;
    var_16 = 0;
    var_17 = 90000;

    if ( distance2dsquared( var_10.origin, var_13 ) < var_17 )
        var_16++;

    if ( distance2dsquared( var_10.origin, var_14 ) < var_17 )
        var_15++;

    if ( var_16 )
    {
        var_1 thread maps\mp\gametypes\_hud_message::splashnotify( "defend", maps\mp\gametypes\_rank::getscoreinfovalue( "defend" ) );
        var_1 maps\mp\_utility::incpersstat( "defends", 1 );
        var_1 thread maps\mp\gametypes\_rank::giverankxp( "defend" );
        maps\mp\gametypes\_gamescore::giveplayerscore( "defend", var_1 );
        var_1 maps\mp\gametypes\_missions::processchallenge( "ch_denied" );
        var_10 thread maps\mp\_matchdata::logkillevent( var_9, "assaulting" );
    }

    if ( var_15 )
    {
        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 );
        var_10 thread maps\mp\_matchdata::logkillevent( var_9, "defending" );
    }
}

blitzgetteam( var_0 )
{
    var_1 = var_0.team;

    if ( !isdefined( var_1 ) )
        var_1 = var_0.ownerteam;

    return var_1;
}

ontimelimit()
{
    level notify( "time_ended" );
    level.finalkillcam_winner = "none";

    if ( game["teamScores"]["axis"] == game["teamScores"]["allies"] )
        var_0 = "tie";
    else if ( game["teamScores"]["axis"] > game["teamScores"]["allies"] )
    {
        level.finalkillcam_winner = "axis";
        var_0 = "axis";
    }
    else
    {
        level.finalkillcam_winner = "allies";
        var_0 = "allies";
    }

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

checkscorelimit()
{
    if ( game["switchedsides"] )
    {
        var_0 = getdvarint( "scr_blitz_scorelimit", 16 );

        if ( getteamscore( self.team ) == var_0 )
        {
            level notify( "final_score_teleport" );
            level.finalkillcam_winner = self.team;
            var_1 = maps\mp\_utility::getotherteam( self.team );

            if ( var_0 >= 16 && getteamscore( var_1 ) == 0 )
                maps\mp\gametypes\_missions::processchallengeforteam( "ch_lockdown", self.team );
        }
    }
    else
    {
        var_0 = getdvarint( "scr_blitz_scorelimit", 16 ) / 2;

        if ( getteamscore( self.team ) == var_0 )
        {
            level notify( "halftime_score_teleport" );
            level.finalkillcam_winner = self.team;
            thread maps\mp\gametypes\_gamelogic::endgame( "roundend", game["end_reason"]["score_limit_reached"] );

            if ( var_0 >= 8.0 )
                maps\mp\gametypes\_missions::processchallengeforteam( "ch_clocking", self.team );
        }
    }
}

onspawnplayer()
{
    self.teleporting = 0;
    maps\mp\_utility::setextrascore0( 0 );

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

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