// 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::registerroundswitchdvar( level.gametype, 0, 0, 9 );
        maps\mp\_utility::registertimelimitdvar( level.gametype, 10 );
        maps\mp\_utility::registerscorelimitdvar( level.gametype, 500 );
        maps\mp\_utility::registerroundlimitdvar( level.gametype, 1 );
        maps\mp\_utility::registerwinlimitdvar( level.gametype, 1 );
        maps\mp\_utility::registernumlivesdvar( level.gametype, 0 );
        maps\mp\_utility::registerhalftimedvar( level.gametype, 0 );
    }

    level.teambased = 1;
    level.onstartgametype = ::onstartgametype;
    level.getspawnpoint = ::getspawnpoint;
    level.onnormaldeath = ::onnormaldeath;
    level.onprecachegametype = ::onprecachegametype;
    level.onspawnplayer = ::onspawnplayer;
}

onprecachegametype()
{
    level.flagbasefxid["friendly"] = loadfx( "vfx/gameplay/mp/core/vfx_marker_base_cyan" );
}

initializematchrules()
{
    maps\mp\_utility::setcommonrulesfrommatchrulesdata();
    setdynamicdvar( "scr_grind_roundswitch", 0 );
    maps\mp\_utility::registerroundswitchdvar( "grind", 0, 0, 9 );
    setdynamicdvar( "scr_grind_roundlimit", 1 );
    maps\mp\_utility::registerroundlimitdvar( "grind", 1 );
    setdynamicdvar( "scr_grind_winlimit", 1 );
    maps\mp\_utility::registerwinlimitdvar( "grind", 1 );
    setdynamicdvar( "scr_grind_halftime", 0 );
    maps\mp\_utility::registerhalftimedvar( "grind", 0 );
    setdynamicdvar( "scr_grind_promode", 0 );
}

onstartgametype()
{
    setclientnamemode( "auto_change" );

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

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

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

    maps\mp\_utility::setobjectivehinttext( "allies", &"OBJECTIVES_WAR_HINT" );
    maps\mp\_utility::setobjectivehinttext( "axis", &"OBJECTIVES_WAR_HINT" );
    initspawns();
    createtags();
    var_0[0] = level.gametype;
    maps\mp\gametypes\_gameobjects::main( var_0 );
    createzones();
    level thread onplayerconnect();
    level thread runzones();
    level thread removetagsongameended();
}

onspawnplayer()
{
    if ( isdefined( self.tagscarried ) )
        self setclientomnvar( "ui_grind_tags", self.tagscarried );
}

createtags()
{
    level.dogtags = [];

    for ( var_0 = 0; var_0 < 50; var_0++ )
    {
        var_1 = spawn( "script_model", ( 0, 0, 0 ) );
        var_1 setmodel( "prop_dogtags_foe_iw6" );
        var_1.baseorigin = var_1.origin;
        var_1 scriptmodelplayanim( "mp_dogtag_spin" );
        var_1 hide();
        var_2 = spawn( "trigger_radius", ( 0, 0, 0 ), 0, 32, 32 );
        var_2.targetname = "trigger_dogtag";
        var_2 hide();
        var_3 = spawnstruct();
        var_3.type = "useObject";
        var_3.curorigin = var_2.origin;
        var_3.entnum = var_2 getentitynumber();
        var_3.lastusedtime = 0;
        var_3.visual = var_1;
        var_3.offset3d = ( 0, 0, 16 );
        var_3.trigger = var_2;
        var_3.triggertype = "proximity";
        var_3 maps\mp\gametypes\_gameobjects::allowuse( "none" );
        level.dogtags[level.dogtags.size] = var_3;
    }
}

gettag()
{
    var_0 = level.dogtags[0];
    var_1 = gettime();

    foreach ( var_3 in level.dogtags )
    {
        if ( var_3.interactteam == "none" )
        {
            var_0 = var_3;
            break;
        }

        if ( var_3.lastusedtime < var_1 )
        {
            var_1 = var_3.lastusedtime;
            var_0 = var_3;
        }
    }

    var_0 notify( "reset" );
    var_0 maps\mp\gametypes\_gameobjects::initializetagpathvariables();
    var_0.lastusedtime = gettime();
    return var_0;
}

spawntag( var_0, var_1 )
{
    var_2 = var_0 + ( 0, 0, 14 );

    if ( isdefined( var_1 ) && var_1 )
    {
        var_3 = ( 0, randomfloat( 360 ), 0 );
        var_4 = anglestoforward( var_3 );
        var_5 = randomfloatrange( 40, 300 );
        var_6 = var_2 + var_5 * var_4;
        var_2 = playerphysicstrace( var_2, var_6 );
    }

    var_7 = gettag();
    var_7.curorigin = var_2;
    var_7.trigger.origin = var_2;
    var_7.visual.origin = var_2;
    var_7.trigger show();
    var_7.visual show();
    var_7 maps\mp\gametypes\_gameobjects::allowuse( "any" );
    playsoundatpos( var_2, "mp_grind_token_drop" );
    return var_7;
}

monitortaguse( var_0 )
{
    level endon( "game_ended" );
    var_0 endon( "deleted" );
    var_0 endon( "reset" );

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

        if ( !maps\mp\_utility::isreallyalive( var_1 ) )
            continue;

        if ( var_1 maps\mp\_utility::isusingremote() || isdefined( var_1.spawningafterremotedeath ) )
            continue;

        if ( isdefined( var_1.classname ) && var_1.classname == "script_vehicle" )
            continue;

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

        var_0.visual hide();
        var_0.trigger hide();
        var_0.curorigin = ( 0, 0, 1000 );
        var_0.trigger.origin = ( 0, 0, 1000 );
        var_0.visual.origin = ( 0, 0, 1000 );
        var_0 maps\mp\gametypes\_gameobjects::allowuse( "none" );
        var_1 thread maps\mp\gametypes\_rank::giverankxp( "tag" );
        var_1 playersettagcount( var_1.tagscarried + 1 );
        var_1 playsound( "mp_killconfirm_tags_pickup" );
        playsoundatpos( var_1.origin, "mp_grind_token_pickup" );
        break;
    }
}

onplayerconnect()
{
    for (;;)
    {
        level waittill( "connected", var_0 );
        var_0.isscoring = 0;
        var_0 thread monitorjointeam();
    }
}

playersettagcount( var_0 )
{
    self.tagscarried = var_0;
    self.game_extrainfo = var_0;

    if ( var_0 > 999 )
        var_0 = 999;

    self setclientomnvar( "ui_grind_tags", var_0 );
}

monitorjointeam()
{
    self endon( "disconnect" );

    for (;;)
    {
        common_scripts\utility::waittill_any( "joined_team", "joined_spectators" );

        if ( level.ingraceperiod )
        {
            playersettagcount( 1 );
            continue;
        }

        playersettagcount( 0 );
    }
}

hidehudelementongameend( var_0 )
{
    level waittill( "game_ended" );

    if ( isdefined( var_0 ) )
        var_0.alpha = 0;
}

createzones()
{
    level.zonelist = [];
    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( "grind_location", "targetname" );

    foreach ( var_2 in var_0 )
        level.zonelist[level.zonelist.size] = addzone( var_2 );
}

addzone( var_0 )
{
    var_1 = spawnstruct();
    var_1.origin = var_0.origin;
    var_1.angles = var_0.angles;
    var_1.trigger = var_0;
    var_1.ownerteam = "neutral";
    var_2 = var_1.origin + ( 0, 0, 32 );
    var_3 = var_1.origin + ( 0, 0, -32 );
    var_4 = bullettrace( var_2, var_3, 0, undefined );
    var_1.origin = var_4["position"];
    var_1.upangles = vectortoangles( var_4["normal"] );
    var_1.forward = anglestoforward( var_1.upangles );
    var_1.right = anglestoright( var_1.upangles );
    var_1.visuals[0] = spawn( "script_model", var_1.origin );
    var_1.visuals[0].angles = var_1.angles;
    var_1.visuals[0] setmodel( game["flagmodels"]["neutral"] );
    return var_1;
}

runzones()
{
    foreach ( var_1 in level.zonelist )
        level thread startzone( var_1, var_1.trigger.script_label );
}

startzone( var_0, var_1 )
{
    level thread runzonefx( var_0 );
    level thread runzonestatus( var_0, var_1 );
    level thread runzonethink( var_0 );
}

runzonefx( var_0 )
{
    maps\mp\_utility::gameflagwait( "prematch_done" );
    var_1 = spawnfx( level.flagbasefxid["friendly"], var_0.origin, var_0.forward, var_0.upangles );
    triggerfx( var_1 );
}

runzonestatus( var_0, var_1 )
{
    var_2 = ( 0, 0, 100 );
    var_0.objid_axis = maps\mp\gametypes\_gameobjects::getnextobjid();
    objective_add( var_0.objid_axis, "active", var_0.origin + var_2, "waypoint_target_" + var_1 );
    objective_team( var_0.objid_axis, "axis" );
    var_0.objid_allies = maps\mp\gametypes\_gameobjects::getnextobjid();
    objective_add( var_0.objid_allies, "active", var_0.origin + var_2, "waypoint_target_" + var_1 );
    objective_team( var_0.objid_allies, "allies" );
    var_3 = 0;
    var_4 = "none";

    for (;;)
    {
        var_5 = "neutral";

        foreach ( var_7 in level.players )
        {
            if ( !maps\mp\_utility::isreallyalive( var_7 ) )
                continue;

            if ( !var_7.tagscarried )
                continue;

            if ( var_7.team == var_5 )
                continue;

            if ( isinzone( var_7, var_0 ) )
            {
                if ( var_5 == "neutral" )
                {
                    var_5 = var_7.team;
                    continue;
                }

                var_5 = "contested";
                break;
            }
        }

        var_0.ownerteam = var_5;

        if ( var_4 == var_5 )
        {
            common_scripts\utility::waitframe();
            continue;
        }

        switch ( var_5 )
        {
            case "neutral":
                if ( var_3 + 1250 > gettime() )
                    break;

                var_4 = var_5;
                var_0.grind_headicon_allies = var_0 maps\mp\_entityheadicons::setheadicon( "allies", "waypoint_bank_" + var_1, var_2, 4, 4, undefined, undefined, undefined, 1, undefined, 0 );
                var_0.grind_headicon_axis = var_0 maps\mp\_entityheadicons::setheadicon( "axis", "waypoint_bank_" + var_1, var_2, 4, 4, undefined, undefined, undefined, 1, undefined, 0 );
                objective_icon( var_0.objid_allies, "waypoint_bank_" + var_1 );
                objective_icon( var_0.objid_axis, "waypoint_bank_" + var_1 );
                break;
            case "contested":
                var_4 = var_5;
                var_0.grind_headicon_allies = var_0 maps\mp\_entityheadicons::setheadicon( "allies", "waypoint_contested_" + var_1, var_2, 4, 4, undefined, undefined, undefined, 1, undefined, 0 );
                var_0.grind_headicon_axis = var_0 maps\mp\_entityheadicons::setheadicon( "axis", "waypoint_contested_" + var_1, var_2, 4, 4, undefined, undefined, undefined, 1, undefined, 0 );
                objective_icon( var_0.objid_allies, "waypoint_contested_" + var_1 );
                objective_icon( var_0.objid_axis, "waypoint_contested_" + var_1 );
                break;
            case "axis":
                var_4 = var_5;
                var_0.grind_headicon_allies = var_0 maps\mp\_entityheadicons::setheadicon( "allies", "waypoint_scoring_foe_" + var_1, var_2, 4, 4, undefined, undefined, undefined, 1, undefined, 0 );
                var_0.grind_headicon_axis = var_0 maps\mp\_entityheadicons::setheadicon( "axis", "waypoint_scoring_friend_" + var_1, var_2, 4, 4, undefined, undefined, undefined, 1, undefined, 0 );
                objective_icon( var_0.objid_allies, "waypoint_scoring_foe_" + var_1 );
                objective_icon( var_0.objid_axis, "waypoint_scoring_friend_" + var_1 );
                var_3 = gettime();
                break;
            case "allies":
                var_4 = var_5;
                var_0.grind_headicon_allies = var_0 maps\mp\_entityheadicons::setheadicon( "allies", "waypoint_scoring_friend_" + var_1, var_2, 4, 4, undefined, undefined, undefined, 1, undefined, 0 );
                var_0.grind_headicon_axis = var_0 maps\mp\_entityheadicons::setheadicon( "axis", "waypoint_scoring_foe_" + var_1, var_2, 4, 4, undefined, undefined, undefined, 1, undefined, 0 );
                objective_icon( var_0.objid_allies, "waypoint_scoring_friend_" + var_1 );
                objective_icon( var_0.objid_axis, "waypoint_scoring_foe_" + var_1 );
                var_3 = gettime();
                break;
        }

        common_scripts\utility::waitframe();
    }
}

isinzone( var_0, var_1 )
{
    if ( maps\mp\_utility::isreallyalive( var_0 ) && var_0 istouching( var_1.trigger ) )
        return 1;

    return 0;
}

runzonethink( var_0 )
{
    level endon( "game_ended" );

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

        if ( isagent( var_1 ) )
            continue;

        if ( !isplayer( var_1 ) )
            continue;

        if ( var_1.isscoring )
            continue;

        var_1.isscoring = 1;
        level thread processscoring( var_1, var_0 );
    }
}

removetagsongameended()
{
    level waittill( "game_ended" );

    foreach ( var_1 in level.players )
    {
        if ( !isdefined( var_1 ) )
            continue;

        if ( !isdefined( var_1.tagscarried ) )
            continue;

        var_1.tagscarried = 0;
    }
}

processscoring( var_0, var_1 )
{
    while ( var_0.tagscarried && isinzone( var_0, var_1 ) )
    {
        var_0 playsoundtoplayer( "mp_grind_token_banked", var_0 );
        var_0 playersettagcount( var_0.tagscarried - 1 );
        maps\mp\gametypes\_gamescore::giveteamscoreforobjective( var_0.team, 1 );
        var_0 maps\mp\_utility::setextrascore0( var_0.extrascore0 + 1 );
        maps\mp\gametypes\_gamescore::giveplayerscore( "tagScore", var_0 );
        var_0 maps\mp\_events::giveobjectivepointstreaks();
        var_0 maps\mp\gametypes\_missions::processchallenge( "ch_grinder" );
        wait 2;
    }

    var_0.isscoring = 0;
}

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

getspawnpoint()
{
    var_0 = self.pers["team"];

    if ( game["switchedsides"] )
        var_0 = maps\mp\_utility::getotherteam( var_0 );

    if ( maps\mp\gametypes\_spawnlogic::shoulduseteamstartspawn() )
    {
        var_1 = maps\mp\gametypes\_spawnlogic::getspawnpointarray( "mp_tdm_spawn_" + var_0 + "_start" );
        var_2 = maps\mp\gametypes\_spawnlogic::getspawnpoint_startspawn( var_1 );
    }
    else
    {
        var_1 = maps\mp\gametypes\_spawnlogic::getteamspawnpoints( var_0 );
        var_2 = maps\mp\gametypes\_spawnscoring::getspawnpoint_nearteam( var_1 );
    }

    return var_2;
}

onnormaldeath( var_0, var_1, var_2 )
{
    level thread droptags( var_0, var_1 );

    if ( game["state"] == "postgame" && game["teamScores"][var_1.team] > game["teamScores"][level.otherteam[var_1.team]] )
        var_1.finalkill = 1;
}

droptags( var_0, var_1 )
{
    if ( isagent( var_0 ) )
        return;

    var_2 = 1;
    var_3 = 0;

    for ( var_4 = 0; var_4 < var_2; var_4++ )
    {
        if ( var_4 > 0 )
            var_3 = 1;

        var_5 = spawntag( var_0.origin, var_3 );
        var_5.victim = var_0;
        var_5.attacker = var_1;
        level notify( "new_tag_spawned", var_5 );
        level thread monitortaguse( var_5 );
    }

    playsoundatpos( var_0.origin, "mp_killconfirm_tags_drop" );
    var_6 = var_0.tagscarried - var_2;
    var_6 = int( max( 0, var_6 ) );
    var_0 playersettagcount( var_6 );
}
