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

init()
{
    level.killstreakspawnshield = 5000;
    level.forcebuddyspawn = 0;
    level.supportbuddyspawn = 1;
    level.spawnmins = ( 0, 0, 0 );
    level.spawnmaxs = ( 0, 0, 0 );
    level.clienttracespawnclass = undefined;
    level.disableclientspawntraces = 0;
    level.numplayerswaitingtospawn = 0;
    level.numplayerswaitingtoenterkillcam = 0;
    level.players = [];
    level.participants = [];
    level.characters = [];
    level.spawnpointarray = [];
    level.grenades = [];
    level.missiles = [];
    level.carepackages = [];
    level.helis = [];
    level.turrets = [];
    level.tanks = [];
    level.scramblers = [];
    level.ims = [];
    level.ugvs = [];
    level.balldrones = [];
    level thread onplayerconnect();
    level thread spawnpointupdate();
    level thread trackgrenades();
    level thread trackmissiles();
    level thread trackcarepackages();
    level thread trackhostmigrationend();

    if ( getdvarint( "scr_frontlineSpawns", 0 ) == 1 )
    {
        if ( level.gametype == "war" || level.gametype == "conf" || level.gametype == "cranked" )
            level thread maps\mp\gametypes\_spawnfactor::spawnfrontlinethink();
    }
    else
    {
        var_0 = int( getdvar( "scr_anchorSpawns" ) );

        if ( level.gametype == "war" || level.gametype == "conf" || level.gametype == "cranked" )
            level thread maps\mp\gametypes\_spawnfactor::correcthomogenization();
    }

    for ( var_1 = 0; var_1 < level.teamnamelist.size; var_1++ )
        level.teamspawnpoints[level.teamnamelist[var_1]] = [];

    maps\mp\gametypes\_spawnfactor::init_spawn_factors();
}

trackhostmigrationend()
{
    for (;;)
    {
        self waittill( "host_migration_end" );

        foreach ( var_1 in level.participants )
            var_1.canperformclienttraces = canperformclienttraces( var_1 );
    }
}

onplayerconnect()
{
    for (;;)
    {
        level waittill( "connected", var_0 );
        level thread startclientspawnpointtraces( var_0 );
        level thread eyesonsightchecks( var_0 );
    }
}

eyesonsightchecks( var_0 )
{
    var_0 endon( "disconnect" );

    for (;;)
    {
        if ( var_0.sessionstate == "playing" && maps\mp\_utility::isreallyalive( var_0 ) && !var_0 maps\mp\_utility::_hasperk( "specialty_gpsjammer" ) )
        {
            var_1 = 0;
            var_2 = var_0 getplayerssightingme();

            foreach ( var_4 in var_2 )
            {
                if ( level.teambased && var_4.team == var_0.team )
                    continue;

                if ( var_0.sessionstate != "playing" || !maps\mp\_utility::isreallyalive( var_0 ) )
                    continue;

                var_1 = 1;
                var_0 notify( "eyesOn" );
                break;
            }

            var_0 markforeyeson( var_1 );
        }
        else
        {
            var_0 markforeyeson( 0 );
            var_0 notify( "eyesOff" );
        }

        wait 0.05;
    }
}

startclientspawnpointtraces( var_0 )
{
    var_0 endon( "disconnect" );
    var_0.canperformclienttraces = canperformclienttraces( var_0 );

    if ( !var_0.canperformclienttraces )
        return;

    wait 0.05;
    var_0 setclientspawnsighttraces( level.clienttracespawnclass );
}

canperformclienttraces( var_0 )
{
    if ( level.disableclientspawntraces )
        return 0;

    if ( !isdefined( level.clienttracespawnclass ) )
        return 0;

    if ( isai( var_0 ) )
        return 0;

    if ( var_0 ishost() )
        return 0;

    return 1;
}

addstartspawnpoints( var_0 )
{
    var_1 = getspawnpointarray( var_0 );

    if ( !var_1.size )
        return;

    if ( !isdefined( level.startspawnpoints ) )
        level.startspawnpoints = [];

    for ( var_2 = 0; var_2 < var_1.size; var_2++ )
    {
        var_1[var_2] spawnpointinit();
        var_1[var_2].selected = 0;
        level.startspawnpoints[level.startspawnpoints.size] = var_1[var_2];
    }

    foreach ( var_4 in var_1 )
    {
        var_4.infront = 1;
        var_5 = anglestoforward( var_4.angles );

        foreach ( var_7 in var_1 )
        {
            if ( var_4 == var_7 )
                continue;

            var_8 = vectornormalize( var_7.origin - var_4.origin );
            var_9 = vectordot( var_5, var_8 );

            if ( var_9 > 0.86 )
            {
                var_4.infront = 0;
                break;
            }
        }
    }
}

addspawnpoints( var_0, var_1, var_2 )
{
    if ( !isdefined( level.spawnpoints ) )
        level.spawnpoints = [];

    if ( !isdefined( level.teamspawnpoints[var_0] ) )
        level.teamspawnpoints[var_0] = [];

    if ( !isdefined( var_2 ) )
        var_2 = 0;

    var_3 = [];
    var_3 = getspawnpointarray( var_1 );

    if ( !isdefined( level.clienttracespawnclass ) )
        level.clienttracespawnclass = var_1;

    if ( !var_3.size && !var_2 )
        return;

    foreach ( var_5 in var_3 )
    {
        if ( !isdefined( var_5.inited ) )
        {
            var_5 spawnpointinit();
            level.spawnpoints[level.spawnpoints.size] = var_5;
        }

        level.teamspawnpoints[var_0][level.teamspawnpoints[var_0].size] = var_5;
    }
}

spawnpointinit()
{
    var_0 = self;
    level.spawnmins = expandmins( level.spawnmins, var_0.origin );
    level.spawnmaxs = expandmaxs( level.spawnmaxs, var_0.origin );
    var_0.forward = anglestoforward( var_0.angles );
    var_0.sighttracepoint = var_0.origin + ( 0, 0, 50 );
    var_0.lastspawntime = gettime();
    var_0.outside = 1;
    var_0.inited = 1;
    var_0.alternates = [];
    var_1 = 1024;

    if ( !bullettracepassed( var_0.sighttracepoint, var_0.sighttracepoint + ( 0, 0, var_1 ), 0, undefined ) )
    {
        var_2 = var_0.sighttracepoint + var_0.forward * 100;

        if ( !bullettracepassed( var_2, var_2 + ( 0, 0, var_1 ), 0, undefined ) )
            var_0.outside = 0;
    }

    var_3 = anglestoright( var_0.angles );
    addalternatespawnpoint( var_0, var_0.origin + var_3 * 45 );
    addalternatespawnpoint( var_0, var_0.origin - var_3 * 45 );
    initspawnpointvalues( var_0 );
}

addalternatespawnpoint( var_0, var_1 )
{
    var_2 = playerphysicstrace( var_0.origin, var_0.origin + ( 0, 0, 18 ), 0, undefined );
    var_3 = var_2[2] - var_0.origin[2];
    var_4 = ( var_1[0], var_1[1], var_1[2] + var_3 );
    var_5 = playerphysicstrace( var_2, var_4, 0, undefined );

    if ( var_5 != var_4 )
        return;

    var_6 = playerphysicstrace( var_4, var_1 );
    var_0.alternates[var_0.alternates.size] = var_6;
}

getspawnpointarray( var_0 )
{
    if ( !isdefined( level.spawnpointarray ) )
        level.spawnpointarray = [];

    if ( !isdefined( level.spawnpointarray[var_0] ) )
    {
        level.spawnpointarray[var_0] = [];
        level.spawnpointarray[var_0] = getspawnarray( var_0 );

        foreach ( var_2 in level.spawnpointarray[var_0] )
            var_2.classname = var_0;
    }

    return level.spawnpointarray[var_0];
}

getspawnpoint_random( var_0 )
{
    if ( !isdefined( var_0 ) )
        return undefined;

    var_1 = undefined;
    var_0 = maps\mp\gametypes\_spawnscoring::checkdynamicspawns( var_0 );
    var_0 = common_scripts\utility::array_randomize( var_0 );

    foreach ( var_3 in var_0 )
    {
        var_1 = var_3;

        if ( canspawn( var_1.origin ) && !positionwouldtelefrag( var_1.origin ) )
            break;
    }

    return var_1;
}

getspawnpoint_startspawn( var_0 )
{
    if ( !isdefined( var_0 ) )
        return undefined;

    var_1 = undefined;
    var_0 = maps\mp\gametypes\_spawnscoring::checkdynamicspawns( var_0 );

    foreach ( var_3 in var_0 )
    {
        if ( var_3.selected )
            continue;

        if ( var_3.infront )
        {
            var_1 = var_3;
            break;
        }

        var_1 = var_3;
    }

    if ( !isdefined( var_1 ) )
        var_1 = getspawnpoint_random( var_0 );

    var_1.selected = 1;
    return var_1;
}

getspawnpoint_nearteam( var_0, var_1 )
{
    for (;;)
        wait 5;
}

trackgrenades()
{
    for (;;)
    {
        level.grenades = getentarray( "grenade", "classname" );
        wait 0.05;
    }
}

trackmissiles()
{
    for (;;)
    {
        level.missiles = getentarray( "rocket", "classname" );
        wait 0.05;
    }
}

trackcarepackages()
{
    for (;;)
    {
        level.carepackages = getentarray( "care_package", "targetname" );
        wait 0.05;
    }
}

getteamspawnpoints( var_0 )
{
    return level.teamspawnpoints[var_0];
}

ispathdataavailable()
{
    if ( !isdefined( level.pathdataavailable ) )
    {
        var_0 = getallnodes();
        level.pathdataavailable = isdefined( var_0 ) && var_0.size > 150;
    }

    return level.pathdataavailable;
}

addtoparticipantsarray()
{
    level.participants[level.participants.size] = self;
}

removefromparticipantsarray()
{
    var_0 = 0;

    for ( var_1 = 0; var_1 < level.participants.size; var_1++ )
    {
        if ( level.participants[var_1] == self )
        {
            for ( var_0 = 1; var_1 < level.participants.size - 1; var_1++ )
                level.participants[var_1] = level.participants[var_1 + 1];

            level.participants[var_1] = undefined;
            break;
        }
    }
}

addtocharactersarray()
{
    if ( maps\mp\_utility::is_aliens() )
    {
        for ( var_0 = 0; var_0 < level.characters.size; var_0++ )
        {
            if ( level.characters[var_0] == self )
                return;
        }
    }

    level.characters[level.characters.size] = self;
}

removefromcharactersarray()
{
    var_0 = 0;

    for ( var_1 = 0; var_1 < level.characters.size; var_1++ )
    {
        if ( level.characters[var_1] == self )
        {
            for ( var_0 = 1; var_1 < level.characters.size - 1; var_1++ )
                level.characters[var_1] = level.characters[var_1 + 1];

            level.characters[var_1] = undefined;
            break;
        }
    }
}

spawnpointupdate()
{
    while ( !isdefined( level.spawnpoints ) || level.spawnpoints.size == 0 )
        wait 0.05;

    level thread spawnpointsightupdate();
    level thread spawnpointdistanceupdate();

    for (;;)
    {
        level.disableclientspawntraces = getdvarint( "scr_disableClientSpawnTraces" ) > 0;
        wait 0.05;
    }
}

getactiveplayerlist()
{
    var_0 = [];

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

        if ( isplayer( var_2 ) && var_2.sessionstate != "playing" )
            continue;

        if ( var_2 maps\mp\killstreaks\_killstreaks::isusinghelisniper() && isdefined( var_2.chopper ) && ( !isdefined( var_2.chopper.movedlow ) || !var_2.chopper.movedlow ) )
            continue;

        var_2.spawnlogicteam = getspawnteam( var_2 );

        if ( var_2.spawnlogicteam == "spectator" )
            continue;

        if ( isagent( var_2 ) && var_2.agent_type == "dog" )
        {
            var_2.spawnlogictraceheight = getplayertraceheight( var_2, 1 );
            var_2.spawntracelocation = var_2.origin + ( 0, 0, var_2.spawnlogictraceheight );
        }
        else if ( !var_2.canperformclienttraces )
        {
            var_3 = getplayertraceheight( var_2 );
            var_4 = var_2 geteye();
            var_4 = ( var_4[0], var_4[1], var_2.origin[2] + var_3 );
            var_2.spawnlogictraceheight = var_3;
            var_2.spawntracelocation = var_4;
        }

        var_0[var_0.size] = var_2;
    }

    return var_0;
}

spawnpointsightupdate()
{
    var_0 = 18;
    var_1 = 0;
    var_2 = 0;
    var_3 = getactiveplayerlist();

    for (;;)
    {
        if ( var_2 )
        {
            wait 0.05;
            var_1 = 0;
            var_2 = 0;
            var_3 = getactiveplayerlist();
        }

        var_4 = level.spawnpoints;
        var_4 = maps\mp\gametypes\_spawnscoring::checkdynamicspawns( var_4 );
        var_2 = 1;

        foreach ( var_6 in var_4 )
        {
            clearspawnpointsightdata( var_6 );

            foreach ( var_8 in var_3 )
            {
                if ( var_6.fullsights[var_8.spawnlogicteam] )
                    continue;

                if ( var_8.canperformclienttraces )
                    var_9 = var_8 clientspawnsighttracepassed( var_6.index );
                else
                {
                    var_9 = spawnsighttrace( var_6, var_6.origin + ( 0, 0, var_8.spawnlogictraceheight ), var_8.spawntracelocation );
                    var_1++;
                }

                if ( !var_9 )
                    continue;

                if ( var_9 > 0.95 )
                {
                    var_6.fullsights[var_8.spawnlogicteam]++;
                    continue;
                }

                var_6.cornersights[var_8.spawnlogicteam]++;
            }

            additionalsighttraceentities( var_6, level.turrets );
            additionalsighttraceentities( var_6, level.ugvs );

            if ( shouldsighttracewait( var_0, var_1 ) )
            {
                wait 0.05;
                var_1 = 0;
                var_2 = 0;
                var_3 = getactiveplayerlist();
            }
        }
    }
}

shouldsighttracewait( var_0, var_1 )
{
    var_2 = 0;

    foreach ( var_4 in level.participants )
    {
        if ( !var_4.canperformclienttraces )
            var_2++;
    }

    if ( var_1 + var_2 > var_0 )
        return 1;

    return 0;
}

spawnpointdistanceupdate()
{
    var_0 = getactiveplayerlist();
    var_1 = gettime();
    var_2 = 4;
    var_3 = 0;

    for (;;)
    {
        var_4 = level.spawnpoints;
        var_4 = maps\mp\gametypes\_spawnscoring::checkdynamicspawns( var_4 );

        foreach ( var_6 in var_4 )
        {
            clearspawnpointdistancedata( var_6 );
            var_3++;

            foreach ( var_8 in var_0 )
            {
                var_9 = distancesquared( var_8.origin, var_6.origin );

                if ( var_9 < var_6.mindistsquared[var_8.spawnlogicteam] )
                    var_6.mindistsquared[var_8.spawnlogicteam] = var_9;

                if ( var_8.spawnlogicteam == "spectator" )
                    continue;

                var_6.distsumsquared[var_8.spawnlogicteam] = var_6.distsumsquared[var_8.spawnlogicteam] + var_9;
                var_6.distsumsquaredcapped[var_8.spawnlogicteam] = var_6.distsumsquaredcapped[var_8.spawnlogicteam] + min( var_9, maps\mp\gametypes\_spawnfactor::maxplayerspawninfluencedistsquared() );
                var_6.totalplayers[var_8.spawnlogicteam]++;
            }

            if ( isdefined( level.alliesweightorg ) )
            {
                var_11 = 25;

                for ( var_12 = 0; var_12 < var_11; var_12++ )
                {
                    var_9 = distancesquared( level.alliesweightorg, var_6.origin );
                    var_6.totalplayers["allies"]++;
                    var_6.distsumsquared["allies"] = var_6.distsumsquared["allies"] + var_9;
                    var_6.distsumsquaredcapped["allies"] = var_6.distsumsquaredcapped["allies"] + var_9;
                    var_9 = distancesquared( level.axisweightorg, var_6.origin );
                    var_6.totalplayers["axis"]++;
                    var_6.distsumsquared["axis"] = var_6.distsumsquared["axis"] + var_9;
                    var_6.distsumsquaredcapped["axis"] = var_6.distsumsquaredcapped["axis"] + var_9;
                }
            }

            if ( var_3 == var_2 )
            {
                wait 0.05;
                var_0 = getactiveplayerlist();
                var_1 = gettime();
                var_3 = 0;
            }
        }
    }
}

getspawnteam( var_0 )
{
    var_1 = "all";

    if ( level.teambased )
        var_1 = var_0.team;

    return var_1;
}

initspawnpointvalues( var_0 )
{
    clearspawnpointsightdata( var_0 );
    clearspawnpointdistancedata( var_0 );
}

clearspawnpointsightdata( var_0 )
{
    if ( level.teambased )
    {
        foreach ( var_2 in level.teamnamelist )
            clearteamspawnpointsightdata( var_0, var_2 );
    }
    else
        clearteamspawnpointsightdata( var_0, "all" );
}

clearspawnpointdistancedata( var_0 )
{
    if ( level.teambased )
    {
        foreach ( var_2 in level.teamnamelist )
            clearteamspawnpointdistancedata( var_0, var_2 );
    }
    else
        clearteamspawnpointdistancedata( var_0, "all" );
}

clearteamspawnpointsightdata( var_0, var_1 )
{
    var_0.fullsights[var_1] = 0;
    var_0.cornersights[var_1] = 0;
}

clearteamspawnpointdistancedata( var_0, var_1 )
{
    var_0.distsumsquared[var_1] = 0;
    var_0.distsumsquaredcapped[var_1] = 0;
    var_0.mindistsquared[var_1] = 9999999;
    var_0.totalplayers[var_1] = 0;
}

getplayertraceheight( var_0, var_1 )
{
    if ( isdefined( var_1 ) && var_1 )
        return 64;

    var_2 = var_0 getstance();

    if ( var_2 == "stand" )
        return 64;

    if ( var_2 == "crouch" )
        return 44;

    return 32;
}

additionalsighttraceentities( var_0, var_1 )
{
    foreach ( var_3 in var_1 )
    {
        if ( !isdefined( var_3 ) )
            continue;

        var_4 = getspawnteam( var_3 );

        if ( var_0.fullsights[var_4] )
            continue;

        var_5 = spawnsighttrace( var_0, var_0.sighttracepoint, var_3.origin + ( 0, 0, 50 ) );

        if ( !var_5 )
            continue;

        if ( var_5 > 0.95 )
        {
            var_0.fullsights[var_4]++;
            continue;
        }

        var_0.cornersights[var_4]++;
    }
}

finalizespawnpointchoice( var_0 )
{
    var_1 = gettime();
    self.lastspawnpoint = var_0;
    self.lastspawntime = var_1;
    var_0.lastspawntime = var_1;
    var_0.lastspawnteam = self.team;
}

expandspawnpointbounds( var_0 )
{
    var_1 = getspawnpointarray( var_0 );

    for ( var_2 = 0; var_2 < var_1.size; var_2++ )
    {
        level.spawnmins = expandmins( level.spawnmins, var_1[var_2].origin );
        level.spawnmaxs = expandmaxs( level.spawnmaxs, var_1[var_2].origin );
    }
}

expandmins( var_0, var_1 )
{
    if ( var_0[0] > var_1[0] )
        var_0 = ( var_1[0], var_0[1], var_0[2] );

    if ( var_0[1] > var_1[1] )
        var_0 = ( var_0[0], var_1[1], var_0[2] );

    if ( var_0[2] > var_1[2] )
        var_0 = ( var_0[0], var_0[1], var_1[2] );

    return var_0;
}

expandmaxs( var_0, var_1 )
{
    if ( var_0[0] < var_1[0] )
        var_0 = ( var_1[0], var_0[1], var_0[2] );

    if ( var_0[1] < var_1[1] )
        var_0 = ( var_0[0], var_1[1], var_0[2] );

    if ( var_0[2] < var_1[2] )
        var_0 = ( var_0[0], var_0[1], var_1[2] );

    return var_0;
}

findboxcenter( var_0, var_1 )
{
    var_2 = ( 0, 0, 0 );
    var_2 = var_1 - var_0;
    var_2 = ( var_2[0] / 2, var_2[1] / 2, var_2[2] / 2 ) + var_0;
    return var_2;
}

setmapcenterfordev()
{
    level.spawnmins = ( 0, 0, 0 );
    level.spawnmaxs = ( 0, 0, 0 );
    expandspawnpointbounds( "mp_tdm_spawn_allies_start" );
    expandspawnpointbounds( "mp_tdm_spawn_axis_start" );
    level.mapcenter = findboxcenter( level.spawnmins, level.spawnmaxs );
    setmapcenter( level.mapcenter );
}

shoulduseteamstartspawn()
{
    return level.ingraceperiod && ( !isdefined( level.numkills ) || level.numkills == 0 );
}
