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

init()
{
    level.healthoverlaycutoff = 0.55;
    var_0 = 5;
    var_0 = maps\mp\gametypes\_tweakables::gettweakablevalue( "player", "healthregentime" );
    level.playerhealth_regularregendelay = var_0;
    level.healthregendisabled = level.playerhealth_regularregendelay <= 0;
    level thread onplayerconnect();
}

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

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

    for (;;)
    {
        self waittill( "spawned_player" );
        thread playerhealthregen();
        self visionsetthermalforplayer( game["thermal_vision"] );
    }
}

playerhealthregen()
{
    self endon( "death" );
    self endon( "disconnect" );
    self endon( "joined_team" );
    self endon( "joined_spectators" );
    self endon( "faux_spawn" );
    level endon( "game_ended" );

    if ( self.health <= 0 )
        return;

    var_0 = 0;
    var_1 = 0;
    thread playerpainbreathingsound( self.maxhealth * 0.55 );

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

        if ( self.health <= 0 )
            return;

        if ( maps\mp\_utility::isjuggernaut() )
            continue;

        var_1 = gettime();
        var_2 = self.health / self.maxhealth;
        self.regenspeed = 1;

        if ( maps\mp\_utility::_hasperk( "specialty_regenfaster" ) )
            self.regenspeed = self.regenspeed * level.regenfastermod;
        else if ( maps\mp\_utility::_hasperk( "specialty_bloodrush" ) )
            self.regenspeed = self.regenspeed * self.bloodrushregenspeedmod;

        if ( var_2 <= level.healthoverlaycutoff )
            self.atbrinkofdeath = 1;

        thread healthregeneration( var_1, var_2 );
        thread breathingmanager( var_1, var_2 );
    }
}

breathingmanager( var_0, var_1 )
{
    self notify( "breathingManager" );
    self endon( "breathingManager" );
    self endon( "death" );
    self endon( "disconnect" );
    self endon( "joined_team" );
    self endon( "joined_spectators" );
    level endon( "game_ended" );

    if ( maps\mp\_utility::isusingremote() )
        return;

    if ( !isplayer( self ) )
        return;

    self.breathingstoptime = var_0 + 6000 * self.regenspeed;
    wait( 6 * self.regenspeed );

    if ( !level.gameended )
    {
        if ( self hasfemalecustomizationmodel() )
            self playlocalsound( "Fem_breathing_better" );
        else
            self playlocalsound( "breathing_better" );
    }
}

healthregeneration( var_0, var_1 )
{
    self notify( "healthRegeneration" );
    self endon( "healthRegeneration" );
    self endon( "death" );
    self endon( "disconnect" );
    self endon( "joined_team" );
    self endon( "joined_spectators" );
    level endon( "game_ended" );
    var_2 = level.healthregendisabled || isdefined( self.healthregendisabled ) && self.healthregendisabled;

    if ( var_2 )
        return;

    childthread maps\mp\perks\_perkfunctions::regenspeedwatcher();
    wait( level.playerhealth_regularregendelay * self.regenspeed );

    if ( var_1 < 0.55 )
        var_3 = 1;
    else
        var_3 = 0;

    for (;;)
    {
        if ( !isdefined( self.regenspeed ) || self.regenspeed == 1 )
        {
            wait 0.05;

            if ( self.health < self.maxhealth )
            {
                self.health = self.health + 1;
                var_1 = self.health / self.maxhealth;
            }
            else
                break;
        }
        else
        {
            wait 0.05;

            if ( self.health < self.maxhealth )
            {
                if ( maps\mp\_utility::_hasperk( "specialty_regenfaster" ) )
                    self.health = self.health + level.regenfasterhealthmod;
                else if ( maps\mp\_utility::_hasperk( "specialty_bloodrush" ) )
                    self.health = self.health + self.bloodrushregenhealthmod;
            }
            else
                break;
        }

        if ( self.health > self.maxhealth )
            self.health = self.maxhealth;
    }

    self notify( "healed" );
    maps\mp\gametypes\_damage::resetattackerlist();

    if ( var_3 )
        maps\mp\gametypes\_missions::healthregenerated();
}

wait_for_not_using_remote()
{
    self notify( "waiting_to_stop_remote" );
    self endon( "waiting_to_stop_remote" );
    self endon( "death" );
    level endon( "game_ended" );
    self waittill( "stopped_using_remote" );
    maps\mp\_utility::restorebasevisionset( 0 );
}

playerpainbreathingsound( var_0 )
{
    level endon( "game_ended" );
    self endon( "death" );
    self endon( "disconnect" );
    self endon( "joined_team" );
    self endon( "joined_spectators" );

    if ( !isplayer( self ) )
        return;

    wait 2;

    for (;;)
    {
        wait 0.2;

        if ( self.health <= 0 )
            return;

        if ( self.health >= var_0 )
            continue;

        var_1 = level.healthregendisabled || isdefined( self.healthregendisabled ) && self.healthregendisabled;

        if ( var_1 && isdefined( self.breathingstoptime ) && gettime() > self.breathingstoptime )
            continue;

        if ( maps\mp\_utility::isusingremote() )
            continue;

        if ( self hasfemalecustomizationmodel() )
            self playlocalsound( "Fem_breathing_hurt" );
        else
            self playlocalsound( "breathing_hurt" );

        wait 0.784;
        wait( 0.1 + randomfloat( 0.8 ) );
    }
}
