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

init()
{
    level.nukevisionset = "aftermath_post";
    level._effect["nuke_player"] = loadfx( "fx/explosions/player_death_nuke" );
    level._effect["nuke_flash"] = loadfx( "fx/explosions/player_death_nuke_flash" );
    level._effect["nuke_aftermath"] = loadfx( "fx/dust/nuke_aftermath_mp" );
    level.killstreakfuncs["nuke"] = ::tryusenuke;
    setdvarifuninitialized( "scr_nukeTimer", 10 );
    setdvarifuninitialized( "scr_nukeCancelMode", 0 );
    level.nuketimer = getdvarint( "scr_nukeTimer" );
    level.cancelmode = getdvarint( "scr_nukeCancelMode" );

    if ( level.multiteambased )
    {
        for ( var_0 = 0; var_0 < level.teamnamelist.size; var_0++ )
            level.teamnukeemped[level.teamnamelist[var_0]] = 0;
    }
    else
    {
        level.teamnukeemped["allies"] = 0;
        level.teamnukeemped["axis"] = 0;
    }

    level.nukeemptimeout = 60.0;
    level.nukeemptimeremaining = int( level.nukeemptimeout );
    level.nukeinfo = spawnstruct();
    level.nukeinfo.xpscalar = 2;
    level.nukedetonated = undefined;
    level thread onplayerconnect();
}

tryusenuke( var_0, var_1, var_2 )
{
    if ( isdefined( level.nukeincoming ) )
    {
        self iprintlnbold( &"KILLSTREAKS_NUKE_ALREADY_INBOUND" );
        return 0;
    }

    if ( maps\mp\_utility::isusingremote() && ( !isdefined( level.gtnw ) || !level.gtnw ) )
        return 0;

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

    thread donuke( var_2 );
    self notify( "used_nuke" );
    maps\mp\_matchdata::logkillstreakevent( "nuke", self.origin );
    return 1;
}

delaythread_nuke( var_0, var_1 )
{
    level endon( "nuke_cancelled" );
    maps\mp\gametypes\_hostmigration::waitlongdurationwithhostmigrationpause( var_0 );
    thread [[ var_1 ]]();
}

donuke( var_0 )
{
    level endon( "nuke_cancelled" );
    level.nukeinfo.player = self;
    level.nukeinfo.team = self.pers["team"];
    level.nukeincoming = 1;
    level.prevuibombtimer = int( getomnvar( "ui_bomb_timer" ) );
    setomnvar( "ui_bomb_timer", 4 );

    if ( level.teambased )
        thread maps\mp\_utility::teamplayercardsplash( "used_nuke", self, self.team );
    else if ( !level.hardcoremode )
        self iprintlnbold( &"KILLSTREAKS_FRIENDLY_TACTICAL_NUKE" );

    if ( !isdefined( level.donuke_fx ) || ![[ level.donuke_fx ]]() )
    {
        if ( !isdefined( level.nuke_soundobject ) )
        {
            level.nuke_soundobject = spawn( "script_origin", ( 0, 0, 1 ) );
            level.nuke_soundobject hide();
        }

        level thread delaythread_nuke( level.nuketimer - 3.3, ::nukesoundincoming );
        level thread delaythread_nuke( level.nuketimer, ::nukesoundexplosion );
        level thread delaythread_nuke( level.nuketimer, ::nukeslowmo );
        level thread delaythread_nuke( level.nuketimer, ::nukeeffects );
        level thread delaythread_nuke( level.nuketimer + 0.25, ::nukevision );
        level thread delaythread_nuke( level.nuketimer + 1.5, ::nukedeath );
        level thread delaythread_nuke( level.nuketimer + 1.5, ::nukeearthquake );
        level thread nukeaftermatheffect();

        if ( level.cancelmode && var_0 )
            level thread cancelnukeondeath( self );
    }

    level thread update_ui_timers();

    if ( !isdefined( level.nuke_clockobject ) )
    {
        level.nuke_clockobject = spawn( "script_origin", ( 0, 0, 0 ) );
        level.nuke_clockobject hide();
    }

    for ( var_1 = level.nuketimer; var_1 > 0; var_1-- )
    {
        level.nuke_clockobject playsound( "ui_mp_kem_timer" );
        wait 1.0;
    }
}

donukesimple()
{
    level.nukeinfo.player = self;
    level.nukeinfo.team = self.pers["team"];
    level.nukeincoming = 1;
    level thread delaythread_nuke( level.nuketimer - 3.3, ::nukesoundincoming );
    level thread delaythread_nuke( level.nuketimer, ::nukesoundexplosion );
    level thread delaythread_nuke( level.nuketimer, ::nukeslowmo );
    level thread delaythread_nuke( level.nuketimer, ::nukeeffects );
    level thread delaythread_nuke( level.nuketimer + 0.25, ::nukevision );
    level thread delaythread_nuke( level.nuketimer + 1.5, ::nukedeathsimple );
    level thread delaythread_nuke( level.nuketimer + 1.5, ::nukeearthquake );

    if ( !isdefined( level.nuke_soundobject ) )
    {
        level.nuke_soundobject = spawn( "script_origin", ( 0, 0, 1 ) );
        level.nuke_soundobject hide();
    }
}

nukedeathsimple()
{
    level notify( "nuke_death" );
}

cancelnukeondeath( var_0 )
{
    var_0 common_scripts\utility::waittill_any( "death", "disconnect" );

    if ( isdefined( var_0 ) && level.cancelmode == 2 )
        var_0 thread maps\mp\killstreaks\_emp::emp_use( 0, 0 );

    nukecleartimer();
    level.nukeincoming = undefined;
    level notify( "nuke_cancelled" );
}

nukesoundincoming()
{
    level endon( "nuke_cancelled" );

    if ( isdefined( level.nuke_soundobject ) )
        level.nuke_soundobject playsound( "nuke_incoming" );
}

nukesoundexplosion()
{
    level endon( "nuke_cancelled" );

    if ( isdefined( level.nuke_soundobject ) )
    {
        level.nuke_soundobject playsound( "nuke_explosion" );
        level.nuke_soundobject playsound( "nuke_wave" );
    }
}

nukecleartimer()
{
    var_0 = 0;

    if ( isdefined( level.prevuibombtimer ) )
        var_0 = level.prevuibombtimer;

    setomnvar( "ui_bomb_timer", var_0 );
}

nukeeffects()
{
    level endon( "nuke_cancelled" );
    nukecleartimer();
    level.nukedetonated = 1;

    foreach ( var_1 in level.players )
        level thread nukeeffect( var_1 );
}

nukeeffect( var_0 )
{
    level endon( "nuke_cancelled" );
    var_0 endon( "disconnect" );
    common_scripts\utility::waitframe();
    var_1 = undefined;
    var_2 = undefined;

    if ( !isdefined( level.nukeloc ) )
    {
        var_3 = ( 0, var_0.angles[1], 0 );
        var_2 = anglestoforward( var_3 );
        var_4 = 5000;
        var_1 = var_0.origin + var_2 * var_4;
    }
    else
        var_1 = level.nukeloc;

    var_5 = undefined;
    var_6 = ( 0, 0, 1 );

    if ( !isdefined( level.nukeangles ) )
        var_5 = anglestoright( ( 0, var_0.angles[1] + 180, 0 ) );
    else
    {
        var_5 = anglestoforward( level.nukeangles );
        var_6 = anglestoup( level.nukeangles );
    }

    var_7 = spawnfxforclient( level._effect["nuke_flash"], var_1, var_0, var_6, var_5 );
    triggerfx( var_7 );
    var_0 thread cleanupnukeeffect( var_7, 30 );
}

cleanupnukeeffect( var_0, var_1 )
{
    maps\mp\_utility::waitfortimeornotify( var_1, "disconnect" );
    var_0 delete();
}

nukeaftermatheffect()
{
    level endon( "nuke_cancelled" );
    level waittill( "spawning_intermission" );
    var_0 = getentarray( "mp_global_intermission", "classname" );
    var_0 = var_0[0];
    var_1 = anglestoup( var_0.angles );
    var_2 = anglestoright( var_0.angles );
    playfx( level._effect["nuke_aftermath"], var_0.origin, var_1, var_2 );
}

nukeslowmo()
{
    level endon( "nuke_cancelled" );
    setslowmotion( 1.0, 0.25, 0.5 );
    level waittill( "nuke_death" );
    setslowmotion( 0.25, 1, 2.0 );
}

nukevision()
{
    level endon( "nuke_cancelled" );
    level.nukevisioninprogress = 1;
    visionsetpostapply( "mpnuke", 3 );
    maps\mp\gametypes\_hostmigration::waitlongdurationwithhostmigrationpause( 2 );
    visionsetpostapply( "nuke_global_flash", 0.1 );
    setexpfog( 0, 956, 0.72, 0.61, 0.39, 0.968, 0.85, 1, 0.298, 0.273, 0.266, 0.25, ( 0, 0, -1 ), 84, 118, 2.75, 0.984, 124, 100 );
    setdvar( "r_materialBloomHQScriptMasterEnable", 0 );
    maps\mp\gametypes\_hostmigration::waitlongdurationwithhostmigrationpause( 0.5 );
    level notify( "nuke_aftermath_post_started" );
    visionsetpostapply( "aftermath_post", 0.5 );
    level waittill( "nuke_death" );
    level thread updatenukevisiononhostmigration();
    level setnukeaftermathvision( 5 );
}

nukedeath()
{
    level endon( "nuke_cancelled" );
    level endon( "game_ended" );
    level notify( "nuke_death" );
    maps\mp\gametypes\_hostmigration::waittillhostmigrationdone();

    foreach ( var_1 in level.characters )
    {
        if ( nukecankill( var_1 ) )
        {
            if ( isplayer( var_1 ) )
            {
                var_1.nuked = 1;

                if ( maps\mp\_utility::isreallyalive( var_1 ) )
                    var_1 thread maps\mp\gametypes\_damage::finishplayerdamagewrapper( level.nukeinfo.player, level.nukeinfo.player, 999999, 0, "MOD_EXPLOSIVE", "nuke_mp", var_1.origin, ( 0, 0, 1 ), "none", 0, 0 );

                continue;
            }

            var_1 maps\mp\agents\_agents::agent_damage_finished( level.nukeinfo.player, level.nukeinfo.player, 999999, 0, "MOD_EXPLOSIVE", "nuke_mp", var_1.origin, ( 0, 0, 1 ), "none", 0 );
        }
    }

    level thread nuke_empjam();
    level.nukeincoming = undefined;
}

nukecankill( var_0 )
{
    if ( !isdefined( level.nukeinfo ) )
        return 0;

    if ( level.teambased )
    {
        if ( isdefined( level.nukeinfo.team ) && var_0.team == level.nukeinfo.team )
            return 0;
    }
    else
    {
        var_1 = isdefined( level.nukeinfo.player ) && var_0 == level.nukeinfo.player;
        var_2 = isdefined( level.nukeinfo.player ) && isdefined( var_0.owner ) && var_0.owner == level.nukeinfo.player;

        if ( var_1 || var_2 )
            return 0;
    }

    return 1;
}

nukeearthquake()
{
    level endon( "nuke_cancelled" );
    level waittill( "nuke_death" );
}

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

    if ( level.teambased )
        level nukeempjamteam( maps\mp\_utility::getotherteam( level.nukeinfo.team ) );
    else
    {
        level.teamnukeemped[level.nukeinfo.team] = 1;
        level.teamnukeemped[maps\mp\_utility::getotherteam( level.nukeinfo.team )] = 1;
        nukeempjamplayers( level.nukeinfo.player );
    }
}

nukeempjamteam( var_0 )
{
    level endon( "game_ended" );
    level notify( "nuke_EMPJam" );
    level endon( "nuke_EMPJam" );
    level.teamnukeemped[var_0] = 1;

    foreach ( var_2 in level.players )
    {
        if ( var_2 maps\mp\killstreaks\_emp_common::shouldplayerbeaffectedbyemp() )
            var_2 maps\mp\killstreaks\_emp_common::applyperplayerempeffects();
    }

    level notify( "nuke_emp_update" );
    level maps\mp\killstreaks\_jammer::destroygroundobjects( level.nukeinfo.player, var_0 );
    level maps\mp\killstreaks\_air_superiority::destroyactivevehicles( level.nukeinfo.player, var_0 );
    level thread keepnukeemptimeremaining();
    maps\mp\gametypes\_hostmigration::waitlongdurationwithhostmigrationpause( level.nukeemptimeout );
    level.teamnukeemped[var_0] = 0;

    foreach ( var_2 in level.players )
    {
        if ( var_2.team == var_0 && !var_2 maps\mp\killstreaks\_emp_common::shouldplayerbeaffectedbyemp() )
        {
            var_2 maps\mp\killstreaks\_emp_common::removeperplayerempeffects();
            var_2.nuked = undefined;
        }
    }

    level notify( "nuke_emp_update" );
    level notify( "nuke_emp_ended" );
}

nukeempjamplayers( var_0 )
{
    level notify( "nuke_EMPJam" );
    level endon( "nuke_EMPJam" );

    if ( !isdefined( var_0 ) )
        return;

    foreach ( var_2 in level.players )
    {
        if ( var_2 maps\mp\killstreaks\_emp_common::shouldplayerbeaffectedbyemp() )
            var_2 maps\mp\killstreaks\_emp_common::applyperplayerempeffects();
    }

    level notify( "nuke_emp_update" );
    level maps\mp\killstreaks\_jammer::destroygroundobjects( level.nukeinfo.player );
    level maps\mp\killstreaks\_air_superiority::destroyactivevehicles( level.nukeinfo.player );
    level thread keepnukeemptimeremaining();
    maps\mp\gametypes\_hostmigration::waitlongdurationwithhostmigrationpause( level.nukeemptimeout );
    level.nukeinfo.player = undefined;

    foreach ( var_2 in level.players )
    {
        if ( ( !isdefined( var_0 ) || var_2 != var_0 ) && !var_2 maps\mp\killstreaks\_emp_common::shouldplayerbeaffectedbyemp() )
            var_2 maps\mp\killstreaks\_emp_common::removeperplayerempeffects();
    }

    level notify( "nuke_emp_update" );
    level notify( "nuke_emp_ended" );
}

keepnukeemptimeremaining()
{
    level notify( "keepNukeEMPTimeRemaining" );
    level endon( "keepNukeEMPTimeRemaining" );
    level endon( "nuke_emp_ended" );

    for ( level.nukeemptimeremaining = int( level.nukeemptimeout ); level.nukeemptimeremaining; level.nukeemptimeremaining-- )
        wait 1.0;
}

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

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

    for (;;)
    {
        self waittill( "spawned_player" );

        if ( isdefined( level.nukedetonated ) )
            thread setvisionforplayer();
    }
}

setvisionforplayer()
{
    wait 0.1;
    self visionsetpostapplyforplayer( level.nukevisionset, 0 );
}

update_ui_timers()
{
    level endon( "game_ended" );
    level endon( "disconnect" );
    level endon( "nuke_cancelled" );
    level endon( "nuke_death" );
    var_0 = level.nuketimer * 1000 + gettime();
    setomnvar( "ui_nuke_end_milliseconds", var_0 );
    level waittill( "host_migration_begin" );
    var_1 = maps\mp\gametypes\_hostmigration::waittillhostmigrationdone();

    if ( var_1 > 0 )
        setomnvar( "ui_nuke_end_milliseconds", var_0 + var_1 );
}

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

    for (;;)
    {
        level waittill( "host_migration_end" );
        level setnukeaftermathvision( 0 );
    }
}

setnukeaftermathvision( var_0 )
{
    if ( isdefined( level.nukedeathvisionfunc ) )
        level thread [[ level.nukedeathvisionfunc ]]();
    else
        visionsetpostapply( level.nukevisionset, var_0 );
}
