// T6 GSC SOURCE
// Decompiled by https://github.com/xensik/gsc-tool
#include maps\mp\_utility;
#include maps\mp\mp_vertigo_fx;
#include maps\mp\_load;
#include maps\mp\mp_vertigo_amb;
#include maps\mp\_compass;
#include maps\mp\mp_vertigo_doors;
#include maps\mp\gametypes\_deathicons;

main()
{
    level.levelspawndvars = ::levelspawndvars;
    level.ragdoll_override = ::ragdoll_override;
    maps\mp\mp_vertigo_fx::main();
    maps\mp\_load::main();
    maps\mp\mp_vertigo_amb::main();
    maps\mp\_compass::setupminimap( "compass_map_mp_vertigo" );
    setdvar( "compassmaxrange", "2100" );
    game["strings"]["war_callsign_a"] = &"MPUI_CALLSIGN_MAPNAME_A";
    game["strings"]["war_callsign_b"] = &"MPUI_CALLSIGN_MAPNAME_B";
    game["strings"]["war_callsign_c"] = &"MPUI_CALLSIGN_MAPNAME_C";
    game["strings"]["war_callsign_d"] = &"MPUI_CALLSIGN_MAPNAME_D";
    game["strings"]["war_callsign_e"] = &"MPUI_CALLSIGN_MAPNAME_E";
    game["strings_menu"]["war_callsign_a"] = "@MPUI_CALLSIGN_MAPNAME_A";
    game["strings_menu"]["war_callsign_b"] = "@MPUI_CALLSIGN_MAPNAME_B";
    game["strings_menu"]["war_callsign_c"] = "@MPUI_CALLSIGN_MAPNAME_C";
    game["strings_menu"]["war_callsign_d"] = "@MPUI_CALLSIGN_MAPNAME_D";
    game["strings_menu"]["war_callsign_e"] = "@MPUI_CALLSIGN_MAPNAME_E";
    level thread waitforglassbreak();

    if ( getgametypesetting( "allowMapScripting" ) )
        level maps\mp\mp_vertigo_doors::init();
}

levelspawndvars( reset_dvars )
{
    ss = level.spawnsystem;
    ss.enemy_influencer_radius = set_dvar_float_if_unset( "scr_spawn_enemy_influencer_radius", "2400", reset_dvars );
    ss.hq_objective_influencer_inner_radius = set_dvar_float_if_unset( "scr_spawn_hq_objective_influencer_inner_radius", "1400", reset_dvars );
}

waitforglassbreak()
{
    if ( glassexploderssetupcorrectly( 1000, 3 ) == 0 )
        return;

    if ( glassexploderssetupcorrectly( 1004, 3 ) == 0 )
        return;

    if ( glassexploderssetupcorrectly( 1007, 3 ) == 0 )
        return;

    for (;;)
    {
        level waittill( "glass_smash", origin );

        playglassexploder( origin, 1000, 3 );
        playglassexploder( origin, 1004, 3 );
        playglassexploder( origin, 1007, 3 );
    }
}

playglassexploder( origin, exploderbase, explodercount )
{
    distancesq = distancesquared( origin, level.createfxexploders[exploderbase][0].v["origin"] );

    if ( distancesq > 21000 )
        return;

    nearestpane = exploderbase;

    if ( distancesq > 5000 )
    {
        nearestpane = -1;
        distancesq = 5000;
    }

    nearestpanedistsq = distancesq;

    for ( glassexploderindex = 1; glassexploderindex < explodercount; glassexploderindex++ )
    {
        glassexploder = glassexploderindex + exploderbase;
        currentdistsq = distancesquared( origin, level.createfxexploders[glassexploder][0].v["origin"] );

        if ( currentdistsq < nearestpanedistsq )
        {
            nearestpane = glassexploder;
            nearestpanedistsq = currentdistsq;
        }
    }

    if ( nearestpane != -1 )
    {
        exploder( nearestpane );
        level thread window_smash_wind_sound( origin );
    }
}

window_smash_wind_sound( origin )
{
    wind_ent = spawn( "script_origin", origin );
    wind_ent playloopsound( "evt_window_wind", 1 );

    level waittill( "game_ended" );

    wind_ent stoploopsound( 0.5 );
    wind_ent delete();
}

glassexploderssetupcorrectly( exploderbase, explodercount )
{
    for ( glassexploderindex = 0; glassexploderindex < explodercount; glassexploderindex++ )
    {
        glassexploder = glassexploderindex + exploderbase;

        if ( !isdefined( level.createfxexploders[glassexploder] ) )
        {
/#
            assertmsg( "Glass exploder " + glassexploder + " is undefined" );
#/
            return false;
        }

        if ( !isdefined( level.createfxexploders[glassexploder][0] ) || !isdefined( level.createfxexploders[glassexploder][0].v ) || !isdefined( level.createfxexploders[glassexploder][0].v["origin"] ) )
        {
/#
            assertmsg( "Glass exploder " + glassexploder + " is undefined" );
#/
            return false;
        }
    }

    return true;
}

ragdoll_override( idamage, smeansofdeath, sweapon, shitloc, vdir, vattackerorigin, deathanimduration, einflictor, ragdoll_jib, body )
{
    if ( smeansofdeath == "MOD_FALLING" )
    {
        deathanim = body getcorpseanim();
        startfrac = deathanimduration / 1000;

        if ( animhasnotetrack( deathanim, "start_ragdoll" ) )
        {
            times = getnotetracktimes( deathanim, "start_ragdoll" );

            if ( isdefined( times ) )
                startfrac = times[0];
        }

        self.body = body;

        if ( !isdefined( self.switching_teams ) )
            thread maps\mp\gametypes\_deathicons::adddeathicon( body, self, self.team, 5.0 );

        thread startragdollonground( startfrac );
        return true;
    }

    return false;
}

startragdollonground( deathanimduration )
{
    for ( timer = 0; timer < deathanimduration; timer += 0.05 )
    {
        if ( !isdefined( self ) || !isdefined( self.body ) )
            return;

        if ( self isonground() )
            break;

        wait 0.05;
    }

    if ( !isdefined( self ) || !isdefined( self.body ) )
        return;

    self.body startragdoll();
}
