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

init()
{
    if ( getdvar( "r_reflectionProbeGenerate" ) == "1" )
        return;

    movable_cover_precache();
    common_scripts\utility::waitframe();
    var_0 = getentarray( "movable_cover", "targetname" );
    var_1 = common_scripts\utility::getstructarray( "movable_cover", "targetname" );

    foreach ( var_3 in var_1 )
    {
        if ( isdefined( var_3.target ) )
        {
            var_4 = getent( var_3.target, "targetname" );

            if ( isdefined( var_4 ) )
                var_0[var_0.size] = var_4;
        }
    }

    common_scripts\utility::array_thread( var_0, ::movable_cover_init );
}

movable_cover_precache()
{
    level.movable_cover_use_icons = [];
    level.movable_cover_use_icons["dumpster"] = "hud_icon_push";
    level.movable_cover_move_sounds = [];
    level.movable_cover_move_sounds["dumpster"]["start"] = "mp_dumpster_start";
    level.movable_cover_move_sounds["dumpster"]["move"] = "mp_dumpster_mvmt_loop";
    level.movable_cover_move_sounds["dumpster"]["stop"] = "mp_dumpster_end";
    level.movable_cover_move_anim = [];
    level.movable_cover_move_anim["dumpster"] = [];
    level.movable_cover_move_anim["dumpster"]["move"] = "mp_lonestar_dumpster_jitter";
    level.movable_cover_move_anim["dumpster"]["idle"] = "mp_lonestar_dumpster_jitter_idle";
    level.movable_cover_default_parameters = [];
    level.movable_cover_default_parameters["dumpster"] = "goal_radius=1;max_speed=80;accel_time=.8;decel_time=.8;stances=crouch,stand";

    foreach ( var_1 in level.movable_cover_move_anim )
    {
        foreach ( var_3 in var_1 )
            precachempanim( var_3 );
    }
}

movable_cover_init()
{
    self.moving = 0;
    self.stay = 0;
    self.updatepaths = self.spawnflags & 1;
    self.movable_type = "default";

    if ( isdefined( self.script_noteworthy ) )
        self.movable_type = self.script_noteworthy;

    movable_cover_parse_parameters();
    var_0 = [];
    var_1 = common_scripts\utility::get_links();

    foreach ( var_3 in var_1 )
    {
        var_4 = common_scripts\utility::getstruct( var_3, "script_linkname" );

        if ( isdefined( var_4 ) && isdefined( var_4.script_label ) )
        {
            var_4.auto = isdefined( var_4.script_parameters ) && var_4.script_parameters == "auto";
            var_4.stay = isdefined( var_4.script_parameters ) && var_4.script_parameters == "stay";
            var_0[var_4.script_label] = var_4;
        }
    }

    var_6 = [];

    if ( isdefined( self.target ) )
    {
        var_7 = common_scripts\utility::getstructarray( self.target, "targetname" );
        var_8 = getentarray( self.target, "targetname" );
        var_6 = common_scripts\utility::array_combine( var_7, var_8 );
    }

    var_9 = self;
    var_10 = undefined;

    if ( isdefined( level.movable_cover_move_anim[self.movable_type] ) )
    {
        self.animate_ent = spawn( "script_model", self.origin );
        self.animate_ent setmodel( "generic_prop_raven" );
        self.animate_ent.angles = self.angles;
        self.animate_ent linkto( self );
        var_9 = self.animate_ent;
        var_10 = "j_prop_1";
    }

    self.linked_ents = [];

    foreach ( var_12 in var_6 )
    {
        if ( !isdefined( var_12.script_noteworthy ) )
            continue;

        switch ( var_12.script_noteworthy )
        {
            case "move_trigger":
                if ( !isdefined( var_12.script_label ) || !isdefined( var_0[var_12.script_label] ) )
                    break;

                var_12 enablelinkto();
                var_12 linkto( self );
                thread movable_cover_trigger( var_12, var_0[var_12.script_label] );
                thread movable_cover_update_use_icon( var_12, var_0[var_12.script_label] );
                break;
            case "link":
                self.linked_ents[self.linked_ents.size] = var_12;
                break;
            case "angels":
                if ( isdefined( var_12.angles ) && isdefined( self.animate_ent ) )
                    self.animate_ent.angles = var_12.angles;

                break;
            case "mantlebrush":
                self.linked_ents[self.linked_ents.size] = var_12;
                break;
            default:
                break;
        }
    }

    foreach ( var_15 in self.linked_ents )
    {
        if ( isdefined( var_10 ) )
        {
            var_15 linkto( var_9, var_10 );
            continue;
        }

        var_15 linkto( var_9 );
    }

    var_17 = maps\mp\_utility::getlinknamenodes();
    self.traverse_nodes = [];

    foreach ( var_19 in var_17 )
    {
        if ( !isdefined( var_19.type ) )
            continue;

        var_19.node_type = var_19.script_noteworthy;

        if ( !isdefined( var_19.node_type ) )
            var_19.node_type = "closest";

        var_20 = 0;

        switch ( var_19.node_type )
        {
            case "closest":
                var_20 = 1;
                break;
            case "radius2d":
            case "radius3d":
            case "radius":
                if ( isdefined( var_19.target ) )
                {
                    var_12 = common_scripts\utility::getstruct( var_19.target, "targetname" );

                    if ( isdefined( var_12 ) && isdefined( var_12.radius ) )
                    {
                        var_19.test_origin = var_12.origin;
                        var_19.test_radius = var_12.radius;
                        var_20 = 1;
                    }
                }

                break;
            default:
                var_20 = 0;
                break;
        }

        if ( !var_20 )
            continue;

        if ( var_19.type == "Begin" || var_19.type == "End" )
        {
            if ( var_19.type == "Begin" )
                var_19.connected_nodes = getnodearray( var_19.target, "targetname" );
            else
                var_19.connected_nodes = getnodearray( var_19.targetname, "target" );

            foreach ( var_22 in var_19.connected_nodes )
                disconnectnodepair( var_19, var_22 );

            self.traverse_nodes[self.traverse_nodes.size] = var_19;
        }
    }

    movable_cover_connect_traversals();
}

movable_cover_set_user( var_0, var_1 )
{
    if ( !isdefined( self.user ) && isdefined( var_0 ) )
        self notify( "new_user" );
    else if ( isdefined( self.user ) && isdefined( var_0 ) && self.user != var_0 )
        self notify( "new_user" );
    else if ( isdefined( self.user ) && !isdefined( var_0 ) )
        self notify( "clear_user" );

    self.user = var_0;
    self.user_trigger = var_1;
}

movable_cover_update_use_icon( var_0, var_1 )
{
    if ( !isdefined( level.movable_cover_use_icons[self.movable_type] ) )
        return;

    while ( !isdefined( level.players ) )
    {
        common_scripts\utility::waitframe();
        continue;
    }

    var_2 = 100;
    var_3 = var_2 * var_2;
    var_4 = var_0 getentitynumber();
    var_5 = "trigger_" + var_4;

    for (;;)
    {
        foreach ( var_7 in level.players )
        {
            if ( !isdefined( var_7.movable_cover_huds ) )
                var_7.movable_cover_huds = [];

            var_8 = distancesquared( var_7.origin + ( 0, 0, 30 ), var_0.origin );

            if ( var_8 <= var_3 && !movable_cover_at_goal( var_1.origin ) )
            {
                if ( !isdefined( var_7.movable_cover_huds[var_5] ) )
                {
                    var_7.movable_cover_huds[var_5] = movable_cover_use_icon( var_7, var_0 );
                    var_7.movable_cover_huds[var_5].alpha = 0;
                }

                var_7.movable_cover_huds[var_5] notify( "stop_fade" );
                var_7.movable_cover_huds[var_5] thread movable_cover_fade_in_use_icon();
                continue;
            }

            if ( isdefined( var_7.movable_cover_huds[var_5] ) )
                var_7.movable_cover_huds[var_5] thread movable_cover_fade_out_use_icon();
        }

        wait 0.05;
    }
}

movable_cover_fade_in_use_icon()
{
    self endon( "death" );

    if ( self.alpha == 1 )
        return;

    self fadeovertime( 0.5 );
    self.alpha = 1;
}

movable_cover_fade_out_use_icon()
{
    self endon( "death" );
    self endon( "stop_fade" );

    if ( self.alpha == 0 )
        return;

    var_0 = 0.5;
    self fadeovertime( var_0 );
    self.alpha = 0;
    wait( var_0 );
    self destroy();
}

movable_cover_use_icon( var_0, var_1 )
{
    var_2 = var_0 maps\mp\gametypes\_hud_util::createicon( level.movable_cover_use_icons[self.movable_type], 16, 16 );
    var_2 setwaypoint( 1, 0 );
    var_2 settargetent( var_1 );
    var_2.fading = 0;
    return var_2;
}

movable_cover_parse_parameters()
{
    self.goal_radius = 1;
    self.max_speed = 50;
    self.accel_time = 1;
    self.decel_time = 1;
    self.requires_push = 1;
    self.start_delay = 0.2;
    self.stances = [ "stand", "crouch" ];

    if ( !isdefined( self.script_parameters ) )
        self.script_parameters = "";

    var_0 = level.movable_cover_default_parameters[self.movable_type];

    if ( isdefined( var_0 ) )
        self.script_parameters = var_0 + self.script_parameters;

    var_1 = strtok( self.script_parameters, ";" );

    foreach ( var_3 in var_1 )
    {
        var_4 = strtok( var_3, "=" );

        if ( var_4.size != 2 )
            continue;

        switch ( var_4[0] )
        {
            case "goal_radius":
                self.goal_radius = float( var_4[1] );
                self.goal_radius = max( 1, self.goal_radius );
                break;
            case "max_speed":
                self.max_speed = float( var_4[1] );
                break;
            case "accel_time":
                self.accel_time = float( var_4[1] );
                break;
            case "decel_time":
                self.decel_time = float( var_4[1] );
                self.decel_time = max( 0.05, self.decel_time );
                break;
            case "stances":
                self.stances = strtok( var_4[1], "," );
                break;
            case "requires_push":
                self.requires_push = int( var_4[1] );
                break;
            case "start_delay":
                self.start_delay = float( var_4[1] );
                break;
            default:
                break;
        }
    }
}

movable_cover_trigger( var_0, var_1 )
{
    var_2 = var_1.auto;
    var_3 = var_1.stay;

    for (;;)
    {
        var_4 = undefined;

        if ( var_2 && !self.stay )
        {
            common_scripts\utility::waitframe();

            if ( isdefined( self.user ) && self.user_trigger != var_0 )
                continue;
        }
        else
        {
            movable_cover_set_user( undefined, undefined );

            for (;;)
            {
                var_0 waittill( "trigger", var_4 );

                if ( isplayer( var_4 ) )
                    break;
            }

            movable_cover_set_user( var_4, var_0 );
        }

        if ( movable_cover_at_goal( var_1.origin ) )
            continue;

        var_5 = vectornormalize( var_1.origin - self.origin );

        if ( !var_2 && !movable_cover_move_delay( self.start_delay, var_4, var_0, var_5 ) )
            continue;

        var_6 = distance( self.origin, var_1.origin );
        var_7 = var_6 / self.max_speed;

        if ( var_2 && self.stay && !isdefined( self.user ) )
            continue;

        if ( self.moving )
            continue;

        if ( self.updatepaths )
            self connectpaths();

        movable_cover_disconnect_traversals();
        self.moving = 1;
        self.stay = 0;
        self notify( "move_start" );
        var_8 = gettime();
        var_9 = min( var_7, self.accel_time );

        if ( var_2 )
            var_9 = var_7;

        var_10 = movable_cover_get_sound( "start" );

        if ( isdefined( var_10 ) )
            self playsound( var_10 );

        var_11 = movable_cover_get_sound( "move" );

        if ( isdefined( var_11 ) )
            self playloopsound( var_11 );

        if ( isdefined( self.animate_ent ) && isdefined( level.movable_cover_move_anim[self.movable_type]["move"] ) )
            self.animate_ent scriptmodelplayanim( level.movable_cover_move_anim[self.movable_type]["move"] );

        self moveto( var_1.origin, var_7, var_9 );

        if ( var_2 )
            movable_cover_wait_for_user_or_timeout( var_7 );
        else
        {
            while ( movable_cover_is_pushed( var_4, var_0, var_5 ) && !movable_cover_at_goal( var_1.origin ) )
                wait 0.05;

            movable_cover_set_user( undefined, undefined );
        }

        if ( !movable_cover_at_goal( var_1.origin ) )
        {
            var_12 = movable_cover_calc_move_speed_scale( ( gettime() - var_8 ) / 1000, var_7, var_9 );
            var_13 = self.max_speed * var_12;
            var_6 = distance( self.origin, var_1.origin );
            var_14 = var_6;

            if ( var_13 > 0 )
                var_14 = min( var_6, var_13 * self.decel_time );

            var_7 = 2 * var_14 / self.max_speed;
            self moveto( self.origin + var_14 * var_5, var_7, 0, var_7 );
            wait( var_7 );
        }

        self stoploopsound();
        var_15 = movable_cover_get_sound( "stop" );

        if ( isdefined( var_15 ) )
            self playsound( var_15 );

        if ( isdefined( self.animate_ent ) && isdefined( level.movable_cover_move_anim[self.movable_type]["idle"] ) )
            self.animate_ent scriptmodelplayanim( level.movable_cover_move_anim[self.movable_type]["idle"] );

        if ( var_3 && movable_cover_at_goal( var_1.origin ) )
            self.stay = 1;

        if ( self.updatepaths )
            self disconnectpaths();

        movable_cover_connect_traversals();
        self.moving = 0;
        self notify( "move_end" );
    }
}

movable_cover_connect_traversals()
{
    movable_cover_disconnect_traversals();

    foreach ( var_1 in self.traverse_nodes )
    {
        switch ( var_1.node_type )
        {
            case "closest":
                var_1.connected_to = common_scripts\utility::getclosest( var_1.origin, var_1.connected_nodes );
                break;
            case "radius3d":
            case "radius":
                var_2 = distance( var_1.origin, var_1.test_origin );

                if ( var_2 <= var_1.test_radius )
                    var_1.connected_to = var_1.connected_nodes[0];

                break;
            case "radius2d":
                var_3 = distance2d( var_1.origin, var_1.test_origin );

                if ( var_3 <= var_1.test_radius )
                    var_1.connected_to = var_1.connected_nodes[0];

                break;
            default:
                break;
        }

        if ( isdefined( var_1.connected_to ) )
            connectnodepair( var_1, var_1.connected_to );
    }
}

movable_cover_disconnect_traversals()
{
    foreach ( var_1 in self.traverse_nodes )
    {
        if ( isdefined( var_1.connected_to ) )
        {
            disconnectnodepair( var_1, var_1.connected_to );
            var_1.connected_to = undefined;
        }
    }
}

movable_cover_get_sound( var_0 )
{
    if ( !isdefined( level.movable_cover_move_sounds[self.movable_type] ) )
        return undefined;

    return level.movable_cover_move_sounds[self.movable_type][var_0];
}

movable_cover_wait_for_user_or_timeout( var_0 )
{
    self endon( "new_user" );
    wait( var_0 );
}

movable_cover_calc_move_speed_scale( var_0, var_1, var_2, var_3 )
{
    if ( !isdefined( var_2 ) )
        var_2 = 0;

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

    if ( var_0 >= var_1 || var_0 <= 0 )
        return 0;
    else if ( var_0 < var_2 )
        return 1 - ( var_2 - var_0 ) / var_2;
    else if ( var_0 > var_1 - var_3 )
        return 1 - ( var_0 - ( var_1 - var_3 ) ) / var_3;
    else
        return 1;
}

movable_cover_is_pushed( var_0, var_1, var_2 )
{
    if ( !isdefined( var_0 ) || !maps\mp\_utility::isreallyalive( var_0 ) || !isplayer( var_0 ) )
        return 0;

    if ( !movable_cover_is_touched( var_1, var_0 ) )
        return 0;

    if ( var_0 ismantling() )
        return 0;

    var_3 = var_0 getstance();

    if ( !common_scripts\utility::array_contains( self.stances, var_3 ) )
        return 0;

    if ( self.requires_push )
    {
        var_4 = var_0 getnormalizedmovement();
        var_4 = rotatevector( var_4, -1 * var_0.angles );
        var_4 = vectornormalize( ( var_4[0], -1 * var_4[1], 0 ) );
        var_5 = vectordot( var_2, var_4 );
        return var_5 > 0.2;
    }
    else
        return 1;
}

movable_cover_is_touched( var_0, var_1 )
{
    return isdefined( var_1 ) && maps\mp\_utility::isreallyalive( var_1 ) && var_1 istouching( var_0 );
}

movable_cover_move_delay( var_0, var_1, var_2, var_3 )
{
    var_4 = var_0 * 1000 + gettime();

    for (;;)
    {
        if ( !isdefined( var_1 ) || !maps\mp\_utility::isreallyalive( var_1 ) )
            return 0;

        if ( var_1 ismantling() )
            return 0;

        if ( !movable_cover_is_pushed( var_1, var_2, var_3 ) )
            return 0;

        if ( self.moving )
            return 0;

        if ( gettime() >= var_4 )
            return 1;

        wait 0.05;
    }
}

movable_cover_at_goal( var_0 )
{
    var_1 = distancesquared( self.origin, var_0 );
    return var_1 <= self.goal_radius * self.goal_radius;
}

movable_cover_mantlebrush_think( var_0 )
{
    self endon( "death" );

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

        if ( isdefined( var_0 ) )
        {
            var_0.old_contents = var_0 setcontents( 0 );
            var_0 hide();
        }

        self waittill( "move_end" );

        if ( isdefined( var_0 ) )
        {
            var_0 show();
            var_0 setcontents( var_0.old_contents );
        }
    }
}
