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

main()
{
    init_lights();
}

init_lights()
{
    common_scripts\utility::array_thread( getentarray( "mp_dart_discoball_light", "targetname" ), ::mp_dart_discoball_light );
    common_scripts\utility::array_thread( getentarray( "mp_dart_discoball_light_reverse", "targetname" ), ::mp_dart_discoball_light_reverse );
    var_0 = getentarray( "mp_dart_pulsing_light", "targetname" );
    common_scripts\utility::array_thread( var_0, ::mp_dart_pulsing_light );
    var_1 = getentarray( "mp_dart_tv_flicker", "targetname" );
    common_scripts\utility::array_thread( var_1, ::mp_dart_tv_flicker );
}

mp_dart_discoball_light()
{
    var_0 = 3;
    var_1 = 150000;
    self rotatevelocity( ( 0, var_0, 0 ), var_1 );
}

mp_dart_discoball_light_reverse()
{
    var_0 = -3;
    var_1 = 150000;
    self rotatevelocity( ( 0, var_0, 0 ), var_1 );
}

mp_dart_restarteffect()
{
    common_scripts\_createfx::restart_fx_looper();
}

ent_flag( var_0 )
{
    return self.ent_flag[var_0];
}

mp_dart_ent_flag_clear( var_0 )
{
    if ( self.ent_flag[var_0] )
    {
        self.ent_flag[var_0] = 0;
        self notify( var_0 );
    }
}

mp_dart_ent_flag_set( var_0 )
{
    self.ent_flag[var_0] = 1;
    self notify( var_0 );
}

mp_dart_ent_flag_init( var_0 )
{
    if ( !isdefined( self.ent_flag ) )
    {
        self.ent_flag = [];
        self.ent_flags_lock = [];
    }

    self.ent_flag[var_0] = 0;
}

mp_dart_is_light_entity( var_0 )
{
    return var_0.classname == "light_spot" || var_0.classname == "light_omni" || var_0.classname == "light";
}

mp_dart_pulsing_light()
{
    self endon( "stop_dynamic_light_behavior" );
    self.linked_models = 0;
    self.lit_models = undefined;
    self.unlit_models = undefined;
    self.linked_lights = 0;
    self.linked_light_ents = [];
    self.linked_prefab_ents = undefined;
    self.linked_things = [];

    if ( isdefined( self.script_linkto ) )
    {
        self.linked_prefab_ents = common_scripts\utility::get_linked_ents();

        foreach ( var_1 in self.linked_prefab_ents )
        {
            if ( isdefined( var_1.script_noteworthy ) && var_1.script_noteworthy == "on" )
            {
                if ( !isdefined( self.lit_models ) )
                    self.lit_models[0] = var_1;
                else
                    self.lit_models[self.lit_models.size] = var_1;

                continue;
            }

            if ( isdefined( var_1.script_noteworthy ) && var_1.script_noteworthy == "off" )
            {
                if ( !isdefined( self.unlit_models ) )
                    self.unlit_models[0] = var_1;
                else
                    self.unlit_models[self.unlit_models.size] = var_1;

                self.unlit_model = var_1;
                continue;
            }

            if ( mp_dart_is_light_entity( var_1 ) )
            {
                self.linked_lights = 1;
                self.linked_light_ents[self.linked_light_ents.size] = var_1;
            }
        }

        self.linked_models = 1;
    }

    thread mp_dart_generic_flicker_msg_watcher();
    thread mp_dart_generic_flicker();
}

mp_dart_generic_flicker_msg_watcher()
{
    mp_dart_ent_flag_init( "flicker_on" );

    if ( isdefined( self.script_light_startnotify ) && self.script_light_startnotify != "nil" )
    {
        for (;;)
        {
            level waittill( self.script_light_startnotify );
            mp_dart_ent_flag_set( "flicker_on" );

            if ( isdefined( self.script_light_stopnotify ) && self.script_light_stopnotify != "nil" )
            {
                level waittill( self.script_light_stopnotify );
                mp_dart_ent_flag_clear( "flicker_on" );
            }
        }
    }
    else
        mp_dart_ent_flag_set( "flicker_on" );
}

mp_dart_generic_flicker_pause()
{
    var_0 = self getlightintensity();

    if ( !ent_flag( "flicker_on" ) )
    {
        if ( self.linked_models )
        {
            if ( isdefined( self.lit_models ) )
            {
                foreach ( var_2 in self.lit_models )
                {
                    if ( isdefined( var_2.effect ) )
                    {
                        var_2.effect delete();
                        var_2.effect = undefined;
                    }

                    var_2 hide();
                }
            }

            if ( isdefined( self.unlit_models ) )
            {
                foreach ( var_5 in self.unlit_models )
                    var_5 show();
            }
        }

        self setlightintensity( 0 );

        if ( self.linked_lights )
        {
            for ( var_7 = 0; var_7 < self.linked_light_ents.size; var_7++ )
                self.linked_light_ents[var_7] setlightintensity( 0 );
        }

        self waittill( "flicker_on" );
        self setlightintensity( var_0 );

        if ( self.linked_lights )
        {
            for ( var_7 = 0; var_7 < self.linked_light_ents.size; var_7++ )
                self.linked_light_ents[var_7] setlightintensity( var_0 );
        }

        if ( self.linked_models )
        {
            if ( isdefined( self.lit_models ) )
            {
                foreach ( var_2 in self.lit_models )
                {
                    var_2 show();

                    if ( !isdefined( var_2.effect ) )
                    {
                        var_2.effect = spawnfx( level._effect["vfx_bulb_single"], var_2.origin );
                        triggerfx( var_2.effect );
                        common_scripts\utility::waitframe();
                    }
                }
            }

            if ( isdefined( self.unlit_models ) )
            {
                foreach ( var_5 in self.unlit_models )
                    var_5 hide();
            }
        }
    }
}

mp_dart_generic_flicker()
{
    self endon( "stop_dynamic_light_behavior" );
    self endon( "death" );
    var_0 = 0.2;
    var_1 = 1.0;
    var_2 = self getlightintensity();
    var_3 = 0;
    var_4 = var_2;
    var_5 = 0;

    while ( isdefined( self ) )
    {
        mp_dart_generic_flicker_pause();

        for ( var_5 = randomintrange( 1, 10 ); var_5; var_5-- )
        {
            mp_dart_generic_flicker_pause();
            wait( randomfloatrange( 0.05, 0.1 ) );

            if ( var_4 > 0.2 )
            {
                var_4 = randomfloatrange( 0, 0.3 );

                if ( self.linked_models )
                {
                    foreach ( var_7 in self.lit_models )
                    {
                        if ( isdefined( var_7.effect ) )
                        {
                            var_7.effect delete();
                            var_7.effect = undefined;
                            common_scripts\utility::waitframe();
                        }

                        var_7 hide();
                    }
                }

                if ( isdefined( self.unlit_models ) )
                {
                    foreach ( var_10 in self.unlit_models )
                        var_10 show();
                }
            }
            else
            {
                var_4 = var_2;

                if ( self.linked_models )
                {
                    if ( isdefined( self.lit_models ) )
                    {
                        foreach ( var_7 in self.lit_models )
                        {
                            var_7 show();

                            if ( !isdefined( var_7.effect ) )
                            {
                                var_7.effect = spawnfx( level._effect["vfx_bulb_single"], var_7.origin );
                                triggerfx( var_7.effect );
                                common_scripts\utility::waitframe();
                            }
                        }
                    }

                    if ( isdefined( self.unlit_models ) )
                    {
                        foreach ( var_10 in self.unlit_models )
                            var_10 hide();
                    }
                }
            }

            self setlightintensity( var_4 );

            if ( self.linked_lights )
            {
                for ( var_16 = 0; var_16 < self.linked_light_ents.size; var_16++ )
                    self.linked_light_ents[var_16] setlightintensity( var_4 );
            }
        }

        mp_dart_generic_flicker_pause();
        self setlightintensity( var_2 );

        if ( self.linked_lights )
        {
            for ( var_16 = 0; var_16 < self.linked_light_ents.size; var_16++ )
                self.linked_light_ents[var_16] setlightintensity( var_2 );
        }

        if ( self.linked_models )
        {
            if ( isdefined( self.lit_models ) )
            {
                foreach ( var_7 in self.lit_models )
                {
                    var_7 show();

                    if ( !isdefined( var_7.effect ) )
                    {
                        var_7.effect = spawnfx( level._effect["vfx_bulb_single"], var_7.origin );
                        triggerfx( var_7.effect );
                        common_scripts\utility::waitframe();
                    }
                }
            }

            if ( isdefined( self.unlit_models ) )
            {
                foreach ( var_10 in self.unlit_models )
                    var_10 hide();
            }
        }

        wait( randomfloatrange( var_0, var_1 ) );
    }
}

mp_dart_tv_flicker()
{
    var_0 = self getlightintensity();
    var_1 = var_0;

    for (;;)
    {
        var_2 = randomfloatrange( var_0 * 0.3, var_0 * 0.9 );
        var_3 = randomfloatrange( 0.05, 0.1 );
        var_3 = var_3 * 15;

        for ( var_4 = 0; var_4 < var_3; var_4++ )
        {
            var_5 = var_2 * ( var_4 / var_3 ) + var_1 * ( ( var_3 - var_4 ) / var_3 );
            self setlightintensity( var_5 );
            wait 0.05;
        }

        var_1 = var_2;
    }
}
