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

init()
{
    var_0 = spawnstruct();
    var_0.id = "deployable_weapon_crate";
    var_0.weaponinfo = "deployable_weapon_crate_marker_mp";
    var_0.modelbase = "mp_weapon_crate";
    var_0.modelbombsquad = "mp_weapon_crate_bombsquad";
    var_0.hintstring = &"KILLSTREAKS_HINTS_DEPLOYABLE_AMMO_USE";
    var_0.capturingstring = &"KILLSTREAKS_DEPLOYABLE_AMMO_TAKING";
    var_0.event = "deployable_ammo_taken";
    var_0.streakname = "deployable_ammo";
    var_0.splashname = "used_deployable_ammo";
    var_0.shadername = "compass_objpoint_deploy_ammo_friendly";
    var_0.headiconoffset = 20;
    var_0.lifespan = 90.0;
    var_0.vogone = "ammocrate_gone";
    var_0.usexp = 50;
    var_0.xppopup = "destroyed_ammo";
    var_0.vodestroyed = "ammocrate_destroyed";
    var_0.deployedsfx = "mp_vest_deployed_ui";
    var_0.onusesfx = "ammo_crate_use";
    var_0.onusecallback = ::onusedeployable;
    var_0.canusecallback = ::canusedeployable;
    var_0.nousekillstreak = 1;
    var_0.usetime = 1000;
    var_0.maxhealth = 150;
    var_0.damagefeedback = "deployable_bag";
    var_0.deathvfx = loadfx( "vfx/gameplay/mp/killstreaks/vfx_ballistic_vest_death" );
    var_0.allowmeleedamage = 1;
    var_0.allowgrenadedamage = 0;
    var_0.maxuses = 4;
    var_0.minigunchance = 20;
    var_0.minigunweapon = "iw6_minigun_mp";
    var_0.ammorestockcheckfreq = 0.5;
    var_0.ammorestocktime = 10.0;
    var_0.triggerradius = 200;
    var_0.triggerheight = 64;
    var_0.ondeploycallback = ::onboxdeployed;
    var_0.canuseotherboxes = 0;
    level.boxsettings["deployable_ammo"] = var_0;
    level.killstreakfuncs["deployable_ammo"] = ::tryusedeployable;
    level.deployablegunbox_bonusinxuses = randomintrange( 1, var_0.minigunchance + 1 );
    level.deployable_box["deployable_ammo"] = [];
    maps\mp\gametypes\sotf::definechestweapons();
}

tryusedeployable( var_0, var_1 )
{
    var_2 = maps\mp\killstreaks\_deployablebox::begindeployableviamarker( var_0, "deployable_ammo" );

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

    if ( !maps\mp\_utility::is_aliens() )
        maps\mp\_matchdata::logkillstreakevent( "deployable_ammo", self.origin );

    return 1;
}

onusedeployable( var_0 )
{
    level.deployablegunbox_bonusinxuses--;

    if ( level.deployablegunbox_bonusinxuses == 0 )
    {
        var_1 = level.boxsettings[var_0.boxtype];

        if ( isdefined( level.deployableboxgiveweaponfunc ) )
            [[ level.deployableboxgiveweaponfunc ]]( 1 );
        else
            givegun( self, var_1.minigunweapon );

        maps\mp\gametypes\_missions::processchallenge( "ch_guninabox" );
        level.deployablegunbox_bonusinxuses = randomintrange( var_1.minigunchance, var_1.minigunchance + 1 );
    }
    else
        giverandomgun( self );
}

onboxdeployed( var_0 )
{
    thread restockammoaura( var_0 );
}

giverandomgun( var_0 )
{
    var_1 = [];

    foreach ( var_3 in var_0 getweaponslistprimaries() )
        var_1[var_1.size] = getweaponbasename( var_3 );

    var_5 = undefined;

    for (;;)
    {
        var_5 = maps\mp\gametypes\sotf::getrandomweapon( level.weaponarray );
        var_6 = var_5["name"];

        if ( !common_scripts\utility::array_contains( var_1, var_6 ) )
            break;
    }

    var_5 = maps\mp\gametypes\sotf::getrandomattachments( var_5 );
    givegun( var_0, var_5 );
}

givegun( var_0, var_1 )
{
    var_2 = var_0 getweaponslistprimaries();
    var_3 = 0;

    foreach ( var_5 in var_2 )
    {
        if ( !maps\mp\gametypes\_weapons::isaltmodeweapon( var_5 ) )
            var_3++;
    }

    if ( var_3 > 1 )
    {
        var_7 = var_0.lastdroppableweapon;

        if ( isdefined( var_7 ) && var_7 != "none" )
            var_0 dropitem( var_7 );
    }

    var_0 maps\mp\_utility::_giveweapon( var_1 );
    var_0 switchtoweapon( var_1 );
    var_0 givestartammo( var_1 );
}

restockammoaura( var_0 )
{
    self endon( "death" );
    level endon( "game_eneded" );
    var_1 = spawn( "trigger_radius", self.origin, 0, var_0.triggerradius, var_0.triggerheight );
    var_1.owner = self;
    thread maps\mp\gametypes\_weapons::deleteondeath( var_1 );

    if ( isdefined( self.moving_platform ) )
    {
        var_1 enablelinkto();
        var_1 linkto( self.moving_platform );
    }

    var_2 = var_0.triggerradius * var_0.triggerradius;
    var_3 = undefined;

    for (;;)
    {
        var_4 = var_1 getistouchingentities( level.players );

        foreach ( var_3 in var_4 )
        {
            if ( isdefined( var_3 ) && !self.owner maps\mp\_utility::isenemy( var_3 ) && shouldaddammo( var_3 ) )
                addammo( var_3, var_0.ammorestocktime );
        }

        wait( var_0.ammorestockcheckfreq );
    }
}

shouldaddammo( var_0 )
{
    return !isdefined( var_0.deployablegunnextammotime ) || gettime() >= var_0.deployablegunnextammotime;
}

addammo( var_0, var_1 )
{
    var_0.deployablegunnextammotime = gettime() + var_1 * 1000;
    maps\mp\gametypes\_weapons::scavengergiveammo( var_0 );
    var_0 maps\mp\gametypes\_damagefeedback::hudicontype( "boxofguns" );
}

addammoovertime( var_0, var_1, var_2 )
{
    self endon( "death" );
    var_0 endon( "death" );
    var_0 endon( "disconnect" );
    level endon( "game_ended" );

    for (;;)
    {
        addammo( var_0 );
        wait( var_2 );

        if ( distancesquared( var_0.origin, self.origin ) > var_1 )
            break;
    }
}

canusedeployable( var_0 )
{
    if ( maps\mp\_utility::is_aliens() && isdefined( var_0 ) && var_0.owner == self && !isdefined( var_0.air_dropped ) )
        return 0;

    if ( !maps\mp\_utility::is_aliens() )
        return !maps\mp\_utility::isjuggernaut();
    else
        return 1;
}
