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

watershallowfx()
{
    level._effect["water_kick"] = loadfx( "vfx/moments/flood/flood_db_foam_allie_ch_fast_cheap" );
    level._effect["water_splash_emerge"] = loadfx( "vfx/moments/flood/flood_db_foam_allie_vertical_splash_sml" );
    level._effect["water_splash_large"] = loadfx( "vfx/moments/flood/flood_db_foam_allie_vertical_splash_lrg" );
}

watershallowinit( var_0, var_1 )
{
    if ( isdefined( var_0 ) )
        level.waterdeletez = var_0;

    level.trigunderwater = getent( "trigger_underwater", "targetname" );
    level.trigabovewater = getent( "trigger_abovewater", "targetname" );
    level.trigunderwater thread watchplayerenterwater( level.trigabovewater, var_1 );
    level thread clearwatervarsonspawn( level.trigunderwater );

    if ( isdefined( var_0 ) )
        level thread watchentsindeepwater( level.trigunderwater, var_0 );
}

watchentsindeepwater( var_0, var_1 )
{
    level childthread watchentsindeepwater_mines( var_0, var_1 );
    level childthread watchentsindeepwater_ks( var_0, var_1 );
}

watchentsindeepwater_ks( var_0, var_1 )
{
    level endon( "game_ended" );

    for (;;)
    {
        wait 0.05;
        var_2 = level.placedims;
        var_2 = common_scripts\utility::array_combine( var_2, level.uplinks );
        var_2 = common_scripts\utility::array_combine( var_2, level.turrets );

        foreach ( var_4 in var_2 )
        {
            if ( !isdefined( var_4 ) )
                continue;

            var_5 = common_scripts\utility::ter_op( isdefined( var_4.sentrytype ) && var_4.sentrytype == "sentry_minigun", var_1 - 35, var_1 );

            if ( var_4.origin[2] <= var_5 && var_4 istouching( var_0 ) )
                var_4 notify( "death" );
        }

        wait 0.05;

        foreach ( var_8 in level.characters )
        {
            if ( isdefined( var_8 ) && isalive( var_8 ) && isai( var_8 ) && var_8.origin[2] <= var_1 - 35 && var_8 istouching( var_0 ) )
            {
                if ( isagent( var_8 ) && isdefined( var_8.agent_type ) && var_8.agent_type == "dog" )
                {
                    if ( !isdefined( var_8.spawntime ) || gettime() - var_8.spawntime > 2000 )
                        var_8 [[ var_8 maps\mp\agents\_agent_utility::agentfunc( "on_damaged" ) ]]( level, undefined, int( ceil( var_8.maxhealth * 0.08 ) ), 0, "MOD_CRUSH", "none", ( 0, 0, 0 ), ( 0, 0, 0 ), "none", 0 );

                    continue;
                }

                var_8 dodamage( int( ceil( var_8.maxhealth * 0.08 ) ), var_8.origin );
            }
        }
    }
}

watchentsindeepwater_mines( var_0, var_1 )
{
    level endon( "game_ended" );

    for (;;)
    {
        level waittill( "mine_planted" );
        waittillframeend;
        var_2 = level.mines;

        foreach ( var_6, var_4 in var_2 )
        {
            if ( !isdefined( var_4 ) )
                continue;

            var_5 = 0;

            if ( isdefined( var_4.istallforwaterchecks ) )
                var_5 = 28;

            if ( var_4.origin[2] <= var_1 - var_5 && var_4 istouching( var_0 ) )
                var_4 maps\mp\gametypes\_weapons::deleteexplosive();
        }
    }
}

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

    for (;;)
    {
        level waittill( "player_spawned", var_1 );

        if ( !var_1 istouching( var_0 ) )
        {
            var_1.inwater = undefined;
            var_1.underwater = undefined;
            var_1 notify( "out_of_water" );
        }
    }
}

watchplayerenterwater( var_0, var_1 )
{
    level endon( "game_ended" );

    for (;;)
    {
        self waittill( "trigger", var_2 );

        if ( !isplayer( var_2 ) && !isagent( var_2 ) )
            continue;

        if ( !isalive( var_2 ) || isagent( var_2 ) && isdefined( var_2.agent_type ) && var_2.agent_type == "dog" )
            continue;

        if ( !isdefined( var_2.inwater ) )
        {
            var_2.inwater = 1;
            var_2 thread playerinwater( self, var_0, var_1 );
        }
    }
}

playersplash()
{
    self endon( "out_of_water" );
    self endon( "death" );
    self endon( "disconnect" );
    var_0 = self getvelocity();

    if ( var_0[2] > -100 )
        return;

    wait 0.2;
    var_0 = self getvelocity();

    if ( var_0[2] <= -100 )
    {
        self playsound( "watersplash_lrg" );
        playfx( level._effect["water_splash_large"], self.origin + ( 0, 0, 36 ), ( 0, 0, 1 ), anglestoforward( ( 0, self.angles[1], 0 ) ) );
    }
}

playerinwater( var_0, var_1, var_2 )
{
    level endon( "game_ended" );
    self endon( "death" );
    self endon( "disconnect" );
    thread inwaterwake( var_2 );
    thread playerwaterclearwait();
    thread playersplash();

    for (;;)
    {
        if ( !self istouching( var_0 ) )
        {
            self.inwater = undefined;
            self.underwater = undefined;
            self notify( "out_of_water" );
            stopwatervisuals();
            break;
        }

        if ( !isdefined( self.underwater ) && !self istouching( var_1 ) )
        {
            if ( self.classname == "script_vehicle" )
                self notify( "death" );
            else
            {
                self.underwater = 1;
                thread playerunderwater();
            }
        }

        if ( isdefined( self.underwater ) && self istouching( var_1 ) )
        {
            self.underwater = undefined;
            self notify( "above_water" );
            stopwatervisuals();

            if ( isplayer( self ) )
            {
                if ( self hasfemalecustomizationmodel() )
                    self playlocalsound( "Fem_breathing_better" );
                else
                    self playlocalsound( "breathing_better" );
            }

            playfx( level._effect["water_splash_emerge"], self.origin + ( 0, 0, 24 ) );
        }

        wait 0.05;
    }
}

isactivekillstreakpoolrestricted( var_0 )
{
    if ( isdefined( var_0.killstreakindexweapon ) )
    {
        var_1 = self.pers["killstreaks"][self.killstreakindexweapon].streakname;

        if ( isdefined( var_1 ) )
        {
            switch ( var_1 )
            {
                case "sam_turret":
                case "remote_tank":
                case "sentry":
                case "ims":
                case "minigun_turret":
                case "remote_mg_turret":
                case "remote_uav":
                    return 1;
            }
        }
    }

    return 0;
}

playerwaterclearwait()
{
    common_scripts\utility::waittill_any( "death", "disconnect", "out_of_water" );

    if ( !isdefined( self ) )
        return;

    self.inwater = undefined;
    self.underwater = undefined;
}

inwaterwake( var_0 )
{
    level endon( "game_ended" );
    self endon( "death" );
    self endon( "disconnect" );
    self endon( "out_of_water" );
    var_1 = common_scripts\utility::ter_op( isdefined( var_0 ), var_0, self.origin[2] );
    var_2 = 0;

    for (;;)
    {
        if ( var_2 )
            wait 0.05;
        else
            wait 0.3;

        if ( !isdefined( level.waterkicktimenext ) )
            level.waterkicktimenext = gettime() + 200;
        else
        {
            var_3 = gettime();

            if ( gettime() < level.waterkicktimenext )
            {
                var_2 = 1;
                continue;
            }
            else
                level.waterkicktimenext = var_3 + 200;
        }

        var_2 = 0;
        var_4 = self getvelocity();

        if ( !isdefined( var_0 ) )
        {
            if ( abs( var_4[2] ) <= 1 )
                var_1 = self.origin[2];
        }

        if ( abs( var_4[2] ) > 30 )
        {
            playfx( level._effect["water_kick"], ( self.origin[0], self.origin[1], min( var_1, self.origin[2] ) ) );
            continue;
        }

        if ( length2dsquared( var_4 ) > 3600 )
        {
            var_5 = vectornormalize( ( var_4[0], var_4[1], 0 ) );
            playfx( level._effect["water_kick"], ( self.origin[0], self.origin[1], min( var_1, self.origin[2] ) ) + var_5 * 36, var_5, ( 0, 0, 1 ) );
        }
    }
}

playerunderwater()
{
    level endon( "game_ended" );
    self endon( "death" );
    self endon( "disconnect" );
    self endon( "above_water" );
    self endon( "out_of_water" );

    if ( !maps\mp\_utility::isusingremote() )
    {
        startwatervisuals();
        thread stopwatervisualsonremote();
    }

    playfx( level._effect["water_splash_emerge"], self geteye() - ( 0, 0, 24 ) );
    wait 2;
    thread onplayerdrowned();

    for (;;)
    {
        self dodamage( 20, self.origin );
        wait 1;
    }
}

onplayerdrowned()
{
    level endon( "game_ended" );
    self endon( "disconnect" );
    self endon( "above_water" );
    self endon( "out_of_water" );
    self waittill( "death" );
    self.inwater = undefined;
    self.underwater = undefined;
}

underwaterbubbles()
{
    level endon( "game_ended" );
    self endon( "death" );
    self endon( "using_remote" );
    self endon( "disconnect" );
    self endon( "above_water" );
    self endon( "out_of_water" );

    for (;;)
    {
        playfx( level._effect["water_bubbles"], self geteye() + anglestoup( self.angles ) * -13 + anglestoforward( self.angles ) * 25 );
        wait 0.75;
    }
}

startwatervisuals()
{
    self shellshock( "mp_flooded_water", 8 );

    if ( isplayer( self ) )
        self setblurforplayer( 10, 0.0 );
}

stopwatervisuals()
{
    self stopshellshock();

    if ( isplayer( self ) )
        self setblurforplayer( 0, 0.85 );
}

stopwatervisualsonremote()
{
    level endon( "game_ended" );
    self endon( "death" );
    self endon( "disconnect" );
    self endon( "above_water" );
    self endon( "out_of_water" );
    self waittill( "using_remote" );
    stopwatervisuals();
}
