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

init()
{
    if ( !isdefined( level.portableaoegeneratorsettings ) )
    {
        level.portableaoegeneratorsettings = [];
        level.generators = [];
    }
}

setweapon( var_0 )
{
    var_1 = level.portableaoegeneratorsettings[var_0];
    self setoffhandsecondaryclass( "flash" );
    maps\mp\_utility::_giveweapon( var_1.weaponname, 0 );
    self givestartammo( var_1.weaponname );

    if ( !isdefined( self.deployedgenerators ) )
        self.deployedgenerators = [];

    thread monitorgeneratoruse( var_0 );
}

unsetweapon( var_0 )
{
    self notify( "end_monitorUse_" + var_0 );
}

deletegenerator( var_0, var_1 )
{
    if ( !isdefined( var_0 ) )
        return;

    foreach ( var_3 in level.players )
    {
        if ( isdefined( var_3 ) && isdefined( var_3.ingeneratoraoe ) )
            var_3.ingeneratoraoe[var_1] = undefined;
    }

    registergenerator( var_0, var_1, undefined );
    var_0 notify( "death" );
    var_0 delete();
}

registergenerator( var_0, var_1, var_2 )
{
    if ( isdefined( var_2 ) && var_2 )
        self.deployedgenerators[var_1] = var_0;
    else
    {
        self.deployedgenerators[var_1] = undefined;
        var_2 = undefined;
    }

    var_3 = level.generators[var_1];

    if ( !isdefined( var_3 ) )
    {
        level.generators[var_1] = [];
        var_3 = level.generators[var_1];
    }

    var_4 = getid( var_0 );
    var_3[var_4] = var_2;
}

monitorgeneratoruse( var_0 )
{
    self notify( "end_monitorUse_" + var_0 );
    self endon( "end_monitorUse_" + var_0 );
    self endon( "disconnect" );
    level endon( "game_ended" );
    var_1 = level.portableaoegeneratorsettings[var_0];

    for (;;)
    {
        self waittill( "grenade_fire", var_2, var_3 );

        if ( var_3 == var_1.weaponname || var_3 == var_0 )
        {
            if ( !isalive( self ) )
            {
                var_2 delete();
                return;
            }

            if ( checkgeneratorplacement( var_2, var_1.placementztolerance ) )
            {
                var_4 = self.deployedgenerators[var_0];

                if ( isdefined( var_4 ) )
                    deletegenerator( var_4, var_0 );

                var_5 = spawnnewgenerator( var_0, var_2.origin );
                var_6 = var_2 getlinkedparent();

                if ( isdefined( var_6 ) )
                    var_5 linkto( var_6 );

                if ( isdefined( var_2 ) )
                    var_2 delete();

                continue;
            }

            self setweaponammostock( var_1.weaponname, self getweaponammostock( "trophy_mp" ) + 1 );
        }
    }
}

checkgeneratorplacement( var_0, var_1 )
{
    var_0 hide();
    var_0 waittill( "missile_stuck", var_2 );

    if ( var_1 * var_1 < distancesquared( var_0.origin, self.origin ) )
    {
        var_3 = bullettrace( self.origin, self.origin - ( 0, 0, var_1 ), 0, self );

        if ( var_3["fraction"] == 1 )
        {
            var_0 delete();
            return 0;
        }

        var_0.origin = var_3["position"];
    }

    var_0 show();
    return 1;
}

spawnnewgenerator( var_0, var_1 )
{
    var_2 = level.portableaoegeneratorsettings[var_0];
    var_3 = spawn( "script_model", var_1 );
    var_3.health = var_2.health;
    var_3.team = self.team;
    var_3.owner = self;
    var_3 setcandamage( 1 );
    var_3 setmodel( var_2.placedmodel );

    if ( level.teambased )
        var_3 maps\mp\_entityheadicons::setteamheadicon( self.team, ( 0, 0, var_2.headiconheight ) );
    else
        var_3 maps\mp\_entityheadicons::setplayerheadicon( self, ( 0, 0, var_2.headiconheight ) );

    var_3 thread watchowner( self, var_0 );
    var_3 thread watchdamage( self, var_0 );
    var_3 thread watchuse( self, var_0 );
    var_3 thread maps\mp\_utility::notusableforjoiningplayers( self );

    if ( isdefined( var_2.ondeploycallback ) )
        var_3 [[ var_2.ondeploycallback ]]( self, var_0 );

    var_3 thread maps\mp\gametypes\_weapons::createbombsquadmodel( var_2.bombsquadmodel, "tag_origin", self );
    registergenerator( var_3, var_0, 1 );
    self.changingweapon = undefined;
    wait 0.05;

    if ( isdefined( var_3 ) && var_3 maps\mp\_utility::touchingbadtrigger() )
        var_3 notify( "death" );

    return var_3;
}

watchowner( var_0, var_1 )
{
    self endon( "death" );
    level endon( "game_ended" );

    if ( maps\mp\_utility::bot_is_fireteam_mode() )
        var_0 waittill( "killstreak_disowned" );
    else
        var_0 common_scripts\utility::waittill_either( "killstreak_disowned", "death" );

    var_0 thread deletegenerator( self, var_1 );
}

watchdamage( var_0, var_1 )
{
    self.generatortype = var_1;
    var_2 = level.portableaoegeneratorsettings[var_1];
    maps\mp\gametypes\_damage::monitordamage( var_2.health, var_2.damagefeedback, ::handledeathdamage, ::modifydamage, 0 );
}

modifydamage( var_0, var_1, var_2, var_3 )
{
    var_4 = var_3;
    var_4 = maps\mp\gametypes\_damage::handlemeleedamage( var_1, var_2, var_4 );
    var_4 = maps\mp\gametypes\_damage::handleempdamage( var_1, var_2, var_4 );
    var_4 = maps\mp\gametypes\_damage::handlemissiledamage( var_1, var_2, var_4 );
    var_4 = maps\mp\gametypes\_damage::handlegrenadedamage( var_1, var_2, var_4 );
    var_4 = maps\mp\gametypes\_damage::handleapdamage( var_1, var_2, var_4, var_0 );
    return var_4;
}

handledeathdamage( var_0, var_1, var_2 )
{
    var_3 = self.owner;
    var_4 = level.portableaoegeneratorsettings[self.generatortype];

    if ( isdefined( var_3 ) && var_0 != var_3 )
        var_0 notify( "destroyed_equipment" );

    if ( isdefined( var_4.ondestroycallback ) )
        var_3 [[ var_4.ondestroycallback ]]( self, self.generatortype );

    var_3 thread deletegenerator( self, self.generatortype );
}

watchuse( var_0, var_1 )
{
    self endon( "death" );
    level endon( "game_ended" );
    var_0 endon( "disconnect" );
    var_2 = level.portableaoegeneratorsettings[var_1];
    self setcursorhint( "HINT_NOICON" );
    self sethintstring( var_2.usehintstring );
    maps\mp\_utility::setselfusable( var_0 );

    for (;;)
    {
        self waittill( "trigger", var_3 );
        var_3 playlocalsound( var_2.usesound );

        if ( var_3 getammocount( var_2.weaponname ) == 0 && !var_3 maps\mp\_utility::isjuggernaut() )
            var_3 setweapon( var_1 );

        var_3 thread deletegenerator( self, var_1 );
    }
}

generatoraoetracker()
{
    self endon( "death" );
    self endon( "disconnect" );
    self endon( "faux_spawn" );
    level endon( "game_ended" );
    var_0 = randomfloat( 0.5 );
    wait( var_0 );
    self.ingeneratoraoe = [];

    for (;;)
    {
        wait 0.05;

        if ( level.generators.size > 0 || self.ingeneratoraoe.size > 0 )
        {
            foreach ( var_2 in level.portableaoegeneratorsettings )
                checkallgeneratorsofthistype( var_2.generatortype );
        }
    }
}

checkallgeneratorsofthistype( var_0 )
{
    var_1 = level.generators[var_0];

    if ( isdefined( var_1 ) )
    {
        var_2 = level.portableaoegeneratorsettings[var_0];
        var_3 = var_2.aoeradius * var_2.aoeradius;
        var_4 = undefined;

        foreach ( var_6 in var_1 )
        {
            if ( isdefined( var_6 ) && maps\mp\_utility::isreallyalive( var_6 ) )
            {
                if ( level.teambased && matchestargetteam( var_6.team, self.team, var_2.targettype ) || !level.teambased && matchesowner( var_6.owner, self, var_2.targettype ) )
                {
                    var_7 = distancesquared( var_6.origin, self.origin );

                    if ( var_7 < var_3 )
                    {
                        var_4 = var_6;
                        break;
                    }
                }
            }
        }

        var_9 = isdefined( var_4 );
        var_10 = isdefined( self.ingeneratoraoe[var_0] );

        if ( var_9 && !var_10 )
            self [[ var_2.onentercallback ]]();
        else if ( !var_9 && var_10 )
            self [[ var_2.onexitcallback ]]();

        self.ingeneratoraoe[var_0] = var_4;
    }
}

matchestargetteam( var_0, var_1, var_2 )
{
    return var_2 == "all" || var_2 == "friendly" && var_0 == var_1 || var_2 == "enemy" && var_0 != var_1;
}

matchesowner( var_0, var_1, var_2 )
{
    return var_2 == "all" || var_2 == "friendly" && var_0 == var_1 || var_2 == "enemy" && var_0 != var_1;
}

getid( var_0 )
{
    return var_0.owner.guid + var_0.birthtime;
}
