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

init()
{
    level.killcam = maps\mp\gametypes\_tweakables::gettweakablevalue( "game", "allowkillcam" );
}

setcinematiccamerastyle( var_0, var_1, var_2 )
{
    self setclientomnvar( "cam_scene_name", var_0 );
    self setclientomnvar( "cam_scene_lead", var_1 );
    self setclientomnvar( "cam_scene_support", var_2 );
}

setkillcamerastyle( var_0, var_1, var_2, var_3, var_4, var_5 )
{
    var_5.camerastyle = "unknown";

    if ( isdefined( var_1 ) && isdefined( var_1.agent_type ) )
    {
        if ( var_1.agent_type == "dog" || var_1.agent_type == "wolf" )
        {
            setcinematiccamerastyle( "killcam_dog", var_0 getentitynumber(), var_3 getentitynumber() );
            var_5.camerastyle = "killcam_dog";
        }
        else if ( var_1.agent_type == "beastmen" )
        {
            setcinematiccamerastyle( "killcam_agent_firstperson", var_0 getentitynumber(), var_3 getentitynumber() );
            var_5.camerastyle = "killcam_agent_firstperson";
        }
        else
        {
            setcinematiccamerastyle( "killcam_agent", var_0 getentitynumber(), var_3 getentitynumber() );
            var_5.camerastyle = "killcam_agent";
        }

        return 1;
    }
    else if ( var_4 > 0 )
    {
        setcinematiccamerastyle( "unknown", -1, -1 );
        return 0;
    }
    else
    {
        setcinematiccamerastyle( "unknown", -1, -1 );
        return 0;
    }

    return 0;
}

trimkillcamtime( var_0, var_1, var_2, var_3, var_4, var_5, var_6, var_7, var_8 )
{
    var_9 = var_5 + var_6;

    if ( isdefined( var_8 ) && var_9 > var_8 )
    {
        if ( var_8 < 2 )
            return;

        if ( var_8 - var_5 >= 1 )
            var_6 = var_8 - var_5;
        else
        {
            var_6 = 1;
            var_5 = var_8 - 1;
        }

        var_9 = var_5 + var_6;
    }

    var_10 = var_5 + var_7;

    if ( isdefined( var_1 ) && isdefined( var_1.lastspawntime ) )
        var_11 = var_1.lastspawntime;
    else
    {
        var_11 = var_2.lastspawntime;

        if ( isdefined( var_2.deathtime ) )
        {
            if ( gettime() - var_2.deathtime < var_6 * 1000.0 )
            {
                var_6 = 1.0;
                var_6 = var_6 - 0.05;
                var_9 = var_5 + var_6;
            }
        }
    }

    var_12 = ( gettime() - var_11 ) / 1000.0;

    if ( var_10 > var_12 && var_12 > var_7 )
    {
        var_13 = var_12 - var_7;

        if ( var_5 > var_13 )
        {
            var_5 = var_13;
            var_9 = var_5 + var_6;
            var_10 = var_5 + var_7;
        }
    }

    var_14 = spawnstruct();
    var_14.camtime = var_5;
    var_14.postdelay = var_6;
    var_14.killcamlength = var_9;
    var_14.killcamoffset = var_10;
    return var_14;
}

prekillcamnotify( var_0, var_1 )
{
    if ( isdefined( var_1 ) && !isagent( var_1 ) )
        self loadcustomizationplayerview( var_1 );
}

killcam( var_0, var_1, var_2, var_3, var_4, var_5, var_6, var_7, var_8, var_9, var_10, var_11, var_12 )
{
    self endon( "disconnect" );
    self endon( "spawned" );
    level endon( "game_ended" );

    if ( var_2 < 0 || !isdefined( var_10 ) )
        return;

    level.numplayerswaitingtoenterkillcam++;

    if ( level.numplayerswaitingtoenterkillcam > 1 )
        wait( 0.05 * ( level.numplayerswaitingtoenterkillcam - 1 ) );

    wait 0.05;
    level.numplayerswaitingtoenterkillcam--;

    if ( getdvar( "scr_killcam_time" ) == "" )
    {
        if ( var_5 == "artillery_mp" || var_5 == "stealth_bomb_mp" || var_5 == "warhawk_mortar_mp" )
            var_13 = ( gettime() - var_4 ) / 1000 - var_6 - 0.1;
        else if ( var_5 == "remote_mortar_missile_mp" )
            var_13 = 6.5;
        else if ( level.showingfinalkillcam )
            var_13 = 4.0;
        else if ( var_5 == "apache_minigun_mp" )
            var_13 = 3.0;
        else if ( var_5 == "javelin_mp" )
            var_13 = 8;
        else if ( issubstr( var_5, "remotemissile_" ) )
            var_13 = 5;
        else if ( isdefined( var_0.sentrytype ) && var_0.sentrytype == "multiturret" )
            var_13 = 2.0;
        else if ( isdefined( var_0.carestrike ) )
            var_13 = 2.0;
        else if ( !var_8 || var_8 > 5.0 )
            var_13 = 5.0;
        else if ( var_5 == "frag_grenade_mp" || var_5 == "frag_grenade_short_mp" || var_5 == "semtex_mp" || var_5 == "semtexproj_mp" || var_5 == "thermobaric_grenade_mp" || var_5 == "mortar_shell__mp" )
            var_13 = 4.25;
        else
            var_13 = 2.5;
    }
    else
        var_13 = getdvarfloat( "scr_killcam_time" );

    if ( isdefined( var_9 ) )
    {
        if ( var_13 > var_9 )
            var_13 = var_9;

        if ( var_13 < 0.05 )
            var_13 = 0.05;
    }

    if ( getdvar( "scr_killcam_posttime" ) == "" )
        var_14 = 2;
    else
    {
        var_14 = getdvarfloat( "scr_killcam_posttime" );

        if ( var_14 < 0.05 )
            var_14 = 0.05;
    }

    if ( var_2 < 0 || !isdefined( var_10 ) )
        return;

    var_15 = trimkillcamtime( var_0, var_1, var_10, var_11, var_3, var_13, var_14, var_6, var_9 );

    if ( !isdefined( var_15 ) )
        return;

    self setclientomnvar( "ui_killcam_end_milliseconds", 0 );

    if ( isplayer( var_10 ) )
    {
        self setclientomnvar( "ui_killcam_killedby_id", var_10 getentitynumber() );
        self setclientomnvar( "ui_killcam_victim_id", var_11 getentitynumber() );
        self loadcustomizationplayerview( var_10 );
    }

    if ( maps\mp\_utility::iskillstreakweapon( var_5 ) )
    {
        if ( var_12 == "MOD_MELEE" && maps\mp\killstreaks\_killstreaks::isairdropmarker( var_5 ) )
        {
            var_16 = tablelookuprownum( "mp/statsTable.csv", 4, "iw6_knifeonly" );
            self setclientomnvar( "ui_killcam_killedby_weapon", var_16 );
            self setclientomnvar( "ui_killcam_killedby_killstreak", -1 );
        }
        else
        {
            var_17 = maps\mp\_utility::getkillstreakrownum( level.killstreakweildweapons[var_5] );
            self setclientomnvar( "ui_killcam_killedby_killstreak", var_17 );
            self setclientomnvar( "ui_killcam_killedby_weapon", -1 );
            self setclientomnvar( "ui_killcam_killedby_attachment1", -1 );
            self setclientomnvar( "ui_killcam_killedby_attachment2", -1 );
            self setclientomnvar( "ui_killcam_killedby_attachment3", -1 );
            self setclientomnvar( "ui_killcam_killedby_attachment4", -1 );
        }
    }
    else
    {
        var_18 = [];
        var_19 = getweaponbasename( var_5 );

        if ( isdefined( var_19 ) )
        {
            if ( var_12 == "MOD_MELEE" && !maps\mp\gametypes\_weapons::isriotshield( var_5 ) )
                var_19 = "iw6_knifeonly";
            else
            {
                var_19 = maps\mp\_utility::weaponmap( var_19 );
                var_19 = maps\mp\_utility::strip_suffix( var_19, "_mp" );
            }

            var_16 = tablelookuprownum( "mp/statsTable.csv", 4, var_19 );
            self setclientomnvar( "ui_killcam_killedby_weapon", var_16 );
            self setclientomnvar( "ui_killcam_killedby_killstreak", -1 );

            if ( var_19 != "iw6_knifeonly" )
                var_18 = getweaponattachments( var_5 );
        }
        else
        {
            self setclientomnvar( "ui_killcam_killedby_weapon", -1 );
            self setclientomnvar( "ui_killcam_killedby_killstreak", -1 );
        }

        for ( var_20 = 0; var_20 < 4; var_20++ )
        {
            if ( isdefined( var_18[var_20] ) )
            {
                var_21 = tablelookuprownum( "mp/attachmentTable.csv", 4, maps\mp\_utility::attachmentmap_tobase( var_18[var_20] ) );
                self setclientomnvar( "ui_killcam_killedby_attachment" + ( var_20 + 1 ), var_21 );
                continue;
            }

            self setclientomnvar( "ui_killcam_killedby_attachment" + ( var_20 + 1 ), -1 );
        }

        var_22 = [ 0, 0 ];
        var_23 = var_10.pers["loadoutPerks"];

        for ( var_20 = 0; var_20 < var_23.size; var_20++ )
        {
            var_24 = int( tablelookup( "mp/killCamAbilitiesBitMaskTable.csv", 1, var_23[var_20], 0 ) );

            if ( var_24 == 0 )
                continue;

            var_25 = int( ( var_24 - 1 ) / 24 );
            var_26 = 1 << ( var_24 - 1 ) % 24;
            var_22[var_25] = var_22[var_25] | var_26;
        }

        self setclientomnvar( "ui_killcam_killedby_abilities1", var_22[0] );
        self setclientomnvar( "ui_killcam_killedby_abilities2", var_22[1] );
    }

    var_27 = getdvarint( "scr_player_forcerespawn" );

    if ( var_8 && !level.gameended || isdefined( self ) && isdefined( self.battlebuddy ) && !level.gameended || var_27 == 0 && !level.gameended )
        self setclientomnvar( "ui_killcam_text", "skip" );
    else if ( !level.gameended )
        self setclientomnvar( "ui_killcam_text", "respawn" );
    else
        self setclientomnvar( "ui_killcam_text", "none" );

    var_28 = gettime();
    self notify( "begin_killcam", var_28 );

    if ( !isagent( var_10 ) && isdefined( var_10 ) )
        var_10 visionsyncwithplayer( var_11 );

    maps\mp\_utility::updatesessionstate( "spectator", "hud_status_dead" );
    self.spectatekillcam = 1;

    if ( isagent( var_10 ) || isagent( var_0 ) )
    {
        var_2 = var_11 getentitynumber();
        var_7 = var_7 - 25;
    }

    self.forcespectatorclient = var_2;
    self.killcamentity = -1;
    var_29 = setkillcamerastyle( var_0, var_1, var_2, var_11, var_3, var_15 );

    if ( !var_29 )
        thread setkillcamentity( var_3, var_15.killcamoffset, var_4 );

    self.archivetime = var_15.killcamoffset;
    self.killcamlength = var_15.killcamlength;
    self.psoffsettime = var_7;
    self allowspectateteam( "allies", 1 );
    self allowspectateteam( "axis", 1 );
    self allowspectateteam( "freelook", 1 );
    self allowspectateteam( "none", 1 );

    if ( level.multiteambased )
    {
        foreach ( var_31 in level.teamnamelist )
            self allowspectateteam( var_31, 1 );
    }

    thread endedkillcamcleanup();
    wait 0.05;

    if ( !isdefined( self ) )
        return;

    if ( self.archivetime < var_15.killcamoffset )
    {
        var_33 = var_15.killcamoffset - self.archivetime;

        if ( game["truncated_killcams"] < 32 )
        {
            setmatchdata( "killcam", game["truncated_killcams"], var_33 );
            game["truncated_killcams"]++;
        }
    }

    var_15.camtime = self.archivetime - 0.05 - var_6;
    var_15.killcamlength = var_15.camtime + var_15.postdelay;
    self.killcamlength = var_15.killcamlength;

    if ( var_15.camtime <= 0 )
    {
        maps\mp\_utility::updatesessionstate( "dead" );
        maps\mp\_utility::clearkillcamstate();
        self notify( "killcam_ended" );
        return;
    }

    var_34 = level.showingfinalkillcam;
    self setclientomnvar( "ui_killcam_end_milliseconds", int( var_15.killcamlength * 1000 ) + gettime() );

    if ( var_34 )
        self setclientomnvar( "ui_killcam_victim_or_attacker", 1 );

    if ( var_34 )
        thread dofinalkillcamfx( var_15, self.killcamentity, var_10, var_11, var_12 );

    self.killcam = 1;

    if ( isdefined( self.battlebuddy ) && !level.gameended )
        self.battlebuddyrespawntimestamp = gettime();

    thread spawnedkillcamcleanup();

    if ( !level.showingfinalkillcam )
        thread waitskipkillcambutton( var_8 );
    else
        self notify( "showing_final_killcam" );

    thread endkillcamifnothingtoshow();
    waittillkillcamover();

    if ( level.showingfinalkillcam )
    {
        thread maps\mp\gametypes\_playerlogic::spawnendofgame();
        return;
    }

    thread calculatekillcamtime( var_28 );
    thread killcamcleanup( 1 );
}

dofinalkillcamfx( var_0, var_1, var_2, var_3, var_4 )
{
    self endon( "killcam_ended" );

    if ( isdefined( level.doingfinalkillcamfx ) )
        return;

    level.doingfinalkillcamfx = 1;
    var_5 = var_0.camtime;
    var_6 = 0;
    var_7 = var_3 getentitynumber();

    if ( !isdefined( var_0.attackernum ) )
        var_0.attackernum = var_2 getentitynumber();

    var_8 = var_5;

    if ( var_8 > 1.0 )
    {
        var_8 = 1.0;
        var_6 = var_6 + 1.0;
        wait( var_5 - var_6 );
    }

    setslowmotion( 1.0, 0.25, var_8 );
    wait( var_8 + 0.5 );
    setslowmotion( 0.25, 1, 1 );
    level.doingfinalkillcamfx = undefined;
}

calculatekillcamtime( var_0 )
{
    var_1 = int( gettime() - var_0 );
    maps\mp\_utility::incplayerstat( "killcamtimewatched", var_1 );
}

waittillkillcamover()
{
    self endon( "abort_killcam" );
    wait( self.killcamlength - 0.05 );
}

setkillcamentity( var_0, var_1, var_2 )
{
    self endon( "disconnect" );
    self endon( "killcam_ended" );
    var_3 = gettime() - var_1 * 1000;

    if ( var_2 > var_3 )
    {
        wait 0.05;
        var_1 = self.archivetime;
        var_3 = gettime() - var_1 * 1000;

        if ( var_2 > var_3 )
            wait( ( var_2 - var_3 ) / 1000 );
    }

    self.killcamentity = var_0;
}

waitskipkillcambutton( var_0 )
{
    self endon( "disconnect" );
    self endon( "killcam_ended" );

    if ( !isai( self ) )
    {
        self notifyonplayercommand( "kc_respawn", "+usereload" );
        self notifyonplayercommand( "kc_respawn", "+activate" );
        self waittill( "kc_respawn" );
        self.cancelkillcam = 1;

        if ( !maps\mp\_utility::matchmakinggame() )
            maps\mp\_utility::incplayerstat( "killcamskipped", 1 );

        if ( var_0 <= 0 )
            maps\mp\_utility::clearlowermessage( "kc_info" );

        self notify( "abort_killcam" );
    }
}

endkillcamifnothingtoshow()
{
    self endon( "disconnect" );
    self endon( "killcam_ended" );

    for (;;)
    {
        if ( self.archivetime <= 0 )
            break;

        wait 0.05;
    }

    self notify( "abort_killcam" );
}

spawnedkillcamcleanup()
{
    self endon( "disconnect" );
    self endon( "killcam_ended" );
    self waittill( "spawned" );
    thread killcamcleanup( 0 );
}

endedkillcamcleanup()
{
    self endon( "disconnect" );
    self endon( "killcam_ended" );
    level waittill( "game_ended" );
    thread killcamcleanup( 1 );
}

killcamcleanup( var_0 )
{
    self setclientomnvar( "ui_killcam_end_milliseconds", 0 );
    self.killcam = undefined;
    var_1 = level.showingfinalkillcam;

    if ( !var_1 )
        setcinematiccamerastyle( "unknown", -1, -1 );

    if ( !level.gameended )
        maps\mp\_utility::clearlowermessage( "kc_info" );

    thread maps\mp\gametypes\_spectating::setspectatepermissions();
    self notify( "killcam_ended" );

    if ( !var_0 )
        return;

    maps\mp\_utility::updatesessionstate( "dead" );
    maps\mp\_utility::clearkillcamstate();
}
