// 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();
    setguns();

    if ( isusingmatchrulesdata() )
    {
        level.initializematchrules = ::initializematchrules;
        [[ level.initializematchrules ]]();
        level thread maps\mp\_utility::reinitializematchrulesonmigration();
    }
    else
    {
        maps\mp\_utility::registertimelimitdvar( level.gametype, 10 );
        var_0 = level.gun_guns.size;
        setdynamicdvar( "scr_gun_scorelimit", var_0 );
        maps\mp\_utility::registerscorelimitdvar( level.gametype, var_0 );
        maps\mp\_utility::registerroundlimitdvar( level.gametype, 1 );
        maps\mp\_utility::registerwinlimitdvar( level.gametype, 0 );
        maps\mp\_utility::registernumlivesdvar( level.gametype, 0 );
        maps\mp\_utility::registerhalftimedvar( level.gametype, 0 );
        level.matchrules_randomize = 0;
        level.matchrules_damagemultiplier = 0;
        level.matchrules_vampirism = 0;
    }

    setspecialloadout();
    setteammode( "ffa" );
    level.teambased = 0;
    level.doprematch = 1;
    level.killstreakrewards = 0;
    level.supportintel = 0;
    level.supportnuke = 0;
    level.assists_disabled = 1;
    level.onprecachegametype = ::onprecachegametype;
    level.onstartgametype = ::onstartgametype;
    level.onspawnplayer = ::onspawnplayer;
    level.getspawnpoint = ::getspawnpoint;
    level.onplayerkilled = ::onplayerkilled;
    level.ontimelimit = ::ontimelimit;
    level.onplayerscore = ::onplayerscore;

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

initializematchrules()
{
    maps\mp\_utility::setcommonrulesfrommatchrulesdata( 1 );
    level.matchrules_randomize = getmatchrulesdata( "gunData", "randomize" );
    var_0 = level.gun_guns.size;
    setdynamicdvar( "scr_gun_scorelimit", var_0 );
    maps\mp\_utility::registerscorelimitdvar( level.gametype, var_0 );
    setdynamicdvar( "scr_gun_winlimit", 1 );
    maps\mp\_utility::registerwinlimitdvar( "gun", 1 );
    setdynamicdvar( "scr_gun_roundlimit", 1 );
    maps\mp\_utility::registerroundlimitdvar( "gun", 1 );
    setdynamicdvar( "scr_gun_halftime", 0 );
    maps\mp\_utility::registerhalftimedvar( "gun", 0 );
    setdynamicdvar( "scr_gun_playerrespawndelay", 0 );
    setdynamicdvar( "scr_gun_waverespawndelay", 0 );
    setdynamicdvar( "scr_player_forcerespawn", 1 );
    setdynamicdvar( "scr_gun_promode", 0 );
}

onprecachegametype()
{

}

onstartgametype()
{
    var_0 = maps\mp\gametypes\_rank::getscoreinfovalue( "gained_gun_score" );
    maps\mp\gametypes\_rank::registerscoreinfo( "dropped_gun_score", -1 * var_0 );
    setgunsfinal();
    setclientnamemode( "auto_change" );
    maps\mp\_utility::setobjectivetext( "allies", &"OBJECTIVES_DM" );
    maps\mp\_utility::setobjectivetext( "axis", &"OBJECTIVES_DM" );

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

    maps\mp\_utility::setobjectivehinttext( "allies", &"OBJECTIVES_DM_HINT" );
    maps\mp\_utility::setobjectivehinttext( "axis", &"OBJECTIVES_DM_HINT" );
    level.spawnmins = ( 0, 0, 0 );
    level.spawnmaxs = ( 0, 0, 0 );
    maps\mp\gametypes\_spawnlogic::addspawnpoints( "allies", "mp_dm_spawn" );
    maps\mp\gametypes\_spawnlogic::addspawnpoints( "axis", "mp_dm_spawn" );
    level.mapcenter = maps\mp\gametypes\_spawnlogic::findboxcenter( level.spawnmins, level.spawnmaxs );
    setmapcenter( level.mapcenter );
    var_1 = [];
    maps\mp\gametypes\_gameobjects::main( var_1 );
    level.quickmessagetoall = 1;
    level.blockweapondrops = 1;
    level thread onplayerconnect();
    level.killstreakrewards = 0;
}

onplayerconnect()
{
    for (;;)
    {
        level waittill( "connected", var_0 );
        var_0.gun_firstspawn = 1;
        var_0.gungamegunindex = 0;
        var_0.gungameprevgunindex = 0;

        if ( level.matchrules_randomize )
        {
            var_0.gun_nextguns = level.gun_guns;
            var_0.gun_prevguns = [];
        }

        var_0 thread refillammo();
        var_0 thread refillsinglecountammo();
        var_0 maps\mp\_utility::setfakeloadoutweaponslot( level.gun_guns[0], 1 );
    }
}

getspawnpoint()
{
    if ( self.gun_firstspawn )
    {
        self.gun_firstspawn = 0;
        self.pers["class"] = "gamemode";
        self.pers["lastClass"] = "";
        self.class = self.pers["class"];
        self.lastclass = self.pers["lastClass"];

        if ( common_scripts\utility::cointoss() )
            maps\mp\gametypes\_menus::addtoteam( "axis", 1 );
        else
            maps\mp\gametypes\_menus::addtoteam( "allies", 1 );
    }

    var_0 = maps\mp\gametypes\_spawnlogic::getteamspawnpoints( self.pers["team"] );
    var_1 = maps\mp\gametypes\_spawnscoring::getspawnpoint_freeforall( var_0 );
    return var_1;
}

onspawnplayer()
{
    self.pers["gamemodeLoadout"] = level.gun_loadouts[self.pers["team"]];
    thread waitloadoutdone();
    level notify( "spawned_player" );
}

waitloadoutdone()
{
    level endon( "game_ended" );
    self endon( "disconnect" );
    self waittill( "spawned_player" );
    maps\mp\_utility::giveperk( "specialty_bling", 0 );
    givenextgun( 1 );
}

onplayerkilled( var_0, var_1, var_2, var_3, var_4, var_5, var_6, var_7, var_8, var_9 )
{
    if ( var_3 == "MOD_FALLING" || isdefined( var_1 ) && isplayer( var_1 ) )
    {
        var_10 = maps\mp\gametypes\_weapons::isriotshield( var_4 );

        if ( var_3 == "MOD_FALLING" || var_1 == self || var_3 == "MOD_MELEE" && !var_10 )
        {
            self playlocalsound( "mp_war_objective_lost" );
            self.gungameprevgunindex = self.gungamegunindex;
            self.gungamegunindex = int( max( 0, self.gungamegunindex - 1 ) );

            if ( self.gungameprevgunindex > self.gungamegunindex )
            {
                thread maps\mp\gametypes\_rank::xpeventpopup( "dropped_gun_rank" );
                maps\mp\gametypes\_gamescore::giveplayerscore( "dropped_gun_score", self, undefined, 1, 1 );
                maps\mp\_utility::setfakeloadoutweaponslot( level.gun_guns[self.gungamegunindex], 1 );
            }

            if ( var_3 == "MOD_MELEE" )
            {
                if ( self.gungameprevgunindex )
                {
                    var_1 thread maps\mp\gametypes\_rank::xpeventpopup( "dropped_enemy_gun_rank" );
                    var_1 thread maps\mp\gametypes\_rank::giverankxp( "dropped_enemy_gun_rank" );
                }

                var_1.assists++;
                var_1 maps\mp\gametypes\_persistence::statsetchild( "round", "sguardWave", var_1.assists );
            }
        }
        else if ( var_3 == "MOD_PISTOL_BULLET" || var_3 == "MOD_RIFLE_BULLET" || var_3 == "MOD_HEAD_SHOT" || var_3 == "MOD_PROJECTILE" || var_3 == "MOD_PROJECTILE_SPLASH" || var_3 == "MOD_IMPACT" || var_3 == "MOD_GRENADE" || var_3 == "MOD_GRENADE_SPLASH" || var_3 == "MOD_MELEE" && var_10 )
        {
            if ( var_4 != var_1.primaryweapon && !var_1 isvalidthrowingknifekill( var_4 ) )
                return;

            var_1.gungameprevgunindex = var_1.gungamegunindex;
            var_1.gungamegunindex++;
            var_1 thread maps\mp\gametypes\_rank::giverankxp( "gained_gun_rank" );
            maps\mp\gametypes\_gamescore::giveplayerscore( "gained_gun_score", var_1, self, 1, 1 );

            if ( var_1.gungamegunindex == level.gun_guns.size - 1 )
            {
                maps\mp\_utility::playsoundonplayers( "mp_enemy_obj_captured" );
                level thread maps\mp\_utility::teamplayercardsplash( "callout_top_gun_rank", var_1 );
            }

            if ( var_1.gungamegunindex < level.gun_guns.size )
            {
                var_1 thread maps\mp\gametypes\_rank::xpeventpopup( "gained_gun_rank" );
                var_1 playlocalsound( "mp_war_objective_taken" );
                var_1 givenextgun();
                var_1 maps\mp\_utility::setfakeloadoutweaponslot( level.gun_guns[var_1.gungamegunindex], 1 );
            }
        }
    }
}

givenextgun( var_0 )
{
    var_1 = getnextgun();
    self takeallweapons();
    maps\mp\_utility::_giveweapon( var_1 );

    if ( isdefined( var_0 ) )
        self setspawnweapon( var_1 );

    self.pers["primaryWeapon"] = var_1;
    self.primaryweapon = var_1;
    self givestartammo( var_1 );
    self switchtoweapon( var_1 );
    giveortakethrowingknife( var_1 );
    maps\mp\gametypes\_weapons::updatetogglescopestate( var_1 );

    if ( self.gungameprevgunindex > self.gungamegunindex )
        thread maps\mp\gametypes\_rank::xpeventpopup( "dropped_gun_rank" );
    else if ( self.gungameprevgunindex < self.gungamegunindex )
        thread maps\mp\gametypes\_rank::xpeventpopup( "gained_gun_rank" );

    self.gungameprevgunindex = self.gungamegunindex;
}

getnextgun()
{
    var_0 = level.gun_guns[self.gungamegunindex];
    return var_0;
}

ontimelimit()
{
    level.finalkillcam_winner = "none";
    var_0 = gethighestprogressedplayers();

    if ( !isdefined( var_0 ) || !var_0.size )
        thread maps\mp\gametypes\_gamelogic::endgame( "tie", game["end_reason"]["time_limit_reached"] );
    else if ( var_0.size == 1 )
        thread maps\mp\gametypes\_gamelogic::endgame( var_0[0], game["end_reason"]["time_limit_reached"] );
    else if ( var_0[var_0.size - 1].gungamegunindex > var_0[var_0.size - 2].gungamegunindex )
        thread maps\mp\gametypes\_gamelogic::endgame( var_0[var_0.size - 1], game["end_reason"]["time_limit_reached"] );
    else
        thread maps\mp\gametypes\_gamelogic::endgame( "tie", game["end_reason"]["time_limit_reached"] );
}

gethighestprogressedplayers()
{
    var_0 = -1;
    var_1 = [];

    foreach ( var_3 in level.players )
    {
        if ( isdefined( var_3.gungamegunindex ) && var_3.gungamegunindex >= var_0 )
        {
            var_0 = var_3.gungamegunindex;
            var_1[var_1.size] = var_3;
        }
    }

    return var_1;
}

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

    for (;;)
    {
        self waittill( "reload" );
        self givestartammo( self.primaryweapon );
    }
}

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

    for (;;)
    {
        if ( maps\mp\_utility::isreallyalive( self ) && self.team != "spectator" && isdefined( self.primaryweapon ) && self getammocount( self.primaryweapon ) == 0 )
        {
            wait 2;
            self notify( "reload" );
            wait 1;
            continue;
        }

        wait 0.05;
    }
}

setguns()
{
    level.gun_guns = [];
    level.selectedweapons = [];
    var_0 = 0;

    if ( isusingmatchrulesdata() )
        var_0 = getmatchrulesdata( "gunData", "numGuns" );

    if ( var_0 > 20 )
        var_0 = 20;

    if ( var_0 )
    {
        for ( var_1 = 0; var_1 < var_0; var_1++ )
            level.gun_guns[var_1] = getmatchrulesdata( "gunData", "guns", var_1 );
    }
    else
    {
        level.gun_guns[0] = "rand_pistol";
        level.gun_guns[1] = "rand_shotgun";
        level.gun_guns[2] = "rand_smg";
        level.gun_guns[3] = "rand_assault";
        level.gun_guns[4] = "rand_lmg";
        level.gun_guns[5] = "rand_dmr";
        level.gun_guns[6] = "rand_smg";
        level.gun_guns[7] = "rand_assault";
        level.gun_guns[8] = "rand_lmg2";
        level.gun_guns[9] = "rand_launcher";
        level.gun_guns[10] = "rand_sniper";
        level.gun_guns[11] = "rand_smg";
        level.gun_guns[12] = "rand_assault2";
        level.gun_guns[13] = "rand_shotgun2";
        level.gun_guns[14] = "rand_dmr";
        level.gun_guns[15] = "rand_sniper2";
        level.gun_guns[16] = "iw6_magnum_mp_acogpistol_akimbo";
        level.gun_guns[17] = "iw6_knifeonly_mp";
    }
}

setgunsfinal()
{
    level.selectedweapons = [];
    buildrandomweapontable();

    for ( var_0 = 0; var_0 < level.gun_guns.size; var_0++ )
    {
        var_1 = level.gun_guns[var_0];

        if ( maps\mp\_utility::isstrstart( var_1, "rand_" ) )
        {
            level.gun_guns[var_0] = getrandomweaponfromcategory( var_1 );
            continue;
        }

        var_2 = maps\mp\_utility::getbaseweaponname( level.gun_guns[var_0] );
        level.selectedweapons[var_2] = 1;
    }

    level.selectedweapons = undefined;
}

setspecialloadout()
{
    level.gun_loadouts["axis"]["loadoutPrimary"] = "iw6_sc2010";
    level.gun_loadouts["axis"]["loadoutPrimaryAttachment"] = "none";
    level.gun_loadouts["axis"]["loadoutPrimaryAttachment2"] = "none";
    level.gun_loadouts["axis"]["loadoutPrimaryBuff"] = "specialty_null";
    level.gun_loadouts["axis"]["loadoutPrimaryCamo"] = "none";
    level.gun_loadouts["axis"]["loadoutPrimaryReticle"] = "none";
    level.gun_loadouts["axis"]["loadoutSecondary"] = "none";
    level.gun_loadouts["axis"]["loadoutSecondaryAttachment"] = "none";
    level.gun_loadouts["axis"]["loadoutSecondaryAttachment2"] = "none";
    level.gun_loadouts["axis"]["loadoutSecondaryBuff"] = "specialty_null";
    level.gun_loadouts["axis"]["loadoutSecondaryCamo"] = "none";
    level.gun_loadouts["axis"]["loadoutSecondaryReticle"] = "none";
    level.gun_loadouts["axis"]["loadoutEquipment"] = "specialty_null";
    level.gun_loadouts["axis"]["loadoutOffhand"] = "none";
    level.gun_loadouts["axis"]["loadoutStreakType"] = "assault";
    level.gun_loadouts["axis"]["loadoutKillstreak1"] = "none";
    level.gun_loadouts["axis"]["loadoutKillstreak2"] = "none";
    level.gun_loadouts["axis"]["loadoutKillstreak3"] = "none";
    level.gun_loadouts["axis"]["loadoutPerks"] = [ "specialty_quickswap", "specialty_marathon" ];
    level.gun_loadouts["axis"]["loadoutJuggernaut"] = 0;
    level.gun_loadouts["allies"] = level.gun_loadouts["axis"];
}

buildrandomweapontable()
{
    level.weaponcategories = [];
    var_0 = 0;

    for (;;)
    {
        var_1 = tablelookupbyrow( "mp/gunGameWeapons.csv", var_0, 0 );

        if ( var_1 == "" )
            break;

        if ( !isdefined( level.weaponcategories[var_1] ) )
            level.weaponcategories[var_1] = [];

        var_2 = tablelookupbyrow( "mp/gunGameWeapons.csv", var_0, 5 );

        if ( var_2 == "" || getdvarint( var_2, 0 ) == 1 )
        {
            var_3 = [];
            var_3["weapon"] = tablelookupbyrow( "mp/gunGameWeapons.csv", var_0, 1 );
            var_3["min"] = int( tablelookupbyrow( "mp/gunGameWeapons.csv", var_0, 2 ) );
            var_3["max"] = int( tablelookupbyrow( "mp/gunGameWeapons.csv", var_0, 3 ) );
            var_3["perk"] = tablelookupbyrow( "mp/gunGameWeapons.csv", var_0, 4 );
            level.weaponcategories[var_1][level.weaponcategories[var_1].size] = var_3;
        }

        var_0++;
    }
}

getrandomweaponfromcategory( var_0 )
{
    var_1 = level.weaponcategories[var_0];

    if ( isdefined( var_1 ) && var_1.size > 0 )
    {
        var_2 = "";
        var_3 = undefined;
        var_4 = 0;

        for (;;)
        {
            var_5 = randomintrange( 0, var_1.size );
            var_3 = var_1[var_5];
            var_6 = maps\mp\_utility::getbaseweaponname( var_3["weapon"] );

            if ( !isdefined( level.selectedweapons[var_6] ) || var_4 > var_1.size )
            {
                level.selectedweapons[var_6] = 1;
                var_2 = var_3["weapon"];
                break;
            }

            var_4++;
        }

        if ( var_2 == var_6 )
        {
            var_7 = randomintrange( var_3["min"], var_3["max"] + 1 );
            var_2 = modifyweapon( var_2, var_7 );
        }

        return var_2;
    }
    else
        return "none";
}

modifyweapon( var_0, var_1 )
{
    var_2 = [];
    var_3 = 0;

    if ( var_1 > 0 )
    {
        var_4 = maps\mp\_utility::getweaponattachmentarrayfromstats( var_0 );

        if ( var_4.size > 0 )
        {
            var_5 = getvalidattachments( var_4 );
            var_6 = var_5.size;

            for ( var_7 = 0; var_7 < var_1; var_7++ )
            {
                var_8 = "";

                while ( var_8 == "" && var_6 > 0 )
                {
                    var_6--;
                    var_9 = randomint( var_5.size );

                    if ( maps\mp\gametypes\sotf::attachmentcheck( var_5[var_9], var_2 ) )
                    {
                        var_8 = maps\mp\_utility::attachmentmap_tounique( var_5[var_9], var_0 );
                        var_2[var_2.size] = var_8;

                        if ( maps\mp\_utility::getattachmenttype( var_8 ) == "rail" )
                            var_3 = 1;
                    }
                }
            }
        }
    }

    var_10 = 0;
    var_11 = 0;
    var_12 = maps\mp\gametypes\_class::buildweaponname( var_0, var_2, var_10, var_11 );
    return var_12;
}

getvalidattachments( var_0 )
{
    var_1 = [];

    foreach ( var_3 in var_0 )
    {
        switch ( var_3 )
        {
            case "silencer":
            case "ammoslug":
            case "gl":
            case "shotgun":
            case "firetypesingle":
            case "firetypeauto":
            case "firetypeburst":
                break;
            default:
                var_1[var_1.size] = var_3;
        }
    }

    return var_1;
}

giveortakethrowingknife( var_0 )
{
    if ( maps\mp\gametypes\_weapons::isknifeonly( var_0 ) )
    {
        maps\mp\_utility::giveperkequipment( "throwingknife_mp", 1 );
        self.loadoutperkequipment = "throwingknife_mp";
        maps\mp\_utility::giveperk( "specialty_extra_deadly", 0 );
        maps\mp\_utility::giveperk( "specialty_scavenger", 0 );
    }
    else
    {
        self takeweapon( "throwingknife_mp" );
        maps\mp\_utility::giveperkequipment( "specialty_null", 1 );
    }
}

isvalidthrowingknifekill( var_0 )
{
    return var_0 == "throwingknife_mp" && isdefined( self.loadoutperkequipment ) && self.loadoutperkequipment == "throwingknife_mp";
}

onplayerscore( var_0, var_1, var_2 )
{
    var_3 = 0;

    if ( var_0 == "gained_gun_score" || var_0 == "dropped_gun_score" )
        var_3 = maps\mp\gametypes\_rank::getscoreinfovalue( var_0 );

    return var_3;
}
