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

createmortar( var_0 )
{
    level.mortarconfig = var_0;
    var_0 thread update_mortars();
    level.air_raid_active = 0;
    level.mapcustomcratefunc = ::mortarcustomcratefunc;
    level.mapcustomkillstreakfunc = ::mortarcustomkillstreakfunc;
    level.mapcustombotkillstreakfunc = ::mortarcustombotkillstreakfunc;
}

mortarcustomcratefunc()
{
    if ( !isdefined( game["player_holding_level_killstrek"] ) )
        game["player_holding_level_killstrek"] = 0;

    if ( !maps\mp\_utility::allowlevelkillstreaks() || game["player_holding_level_killstrek"] )
        return;

    maps\mp\killstreaks\_airdrop::addcratetype( "airdrop_assault", level.mortarconfig.id, level.mortarconfig.crateweight, maps\mp\killstreaks\_airdrop::killstreakcratethink, maps\mp\killstreaks\_airdrop::get_friendly_crate_model(), maps\mp\killstreaks\_airdrop::get_enemy_crate_model(), level.mortarconfig.cratehint );
    level thread watch_for_mortars_crate();
}

watch_for_mortars_crate()
{
    for (;;)
    {
        level waittill( "createAirDropCrate", var_0 );

        if ( isdefined( var_0 ) && isdefined( var_0.cratetype ) && var_0.cratetype == level.mortarconfig.id )
        {
            maps\mp\killstreaks\_airdrop::changecrateweight( "airdrop_assault", level.mortarconfig.id, 0 );
            var_1 = wait_for_capture( var_0 );

            if ( !var_1 )
            {
                maps\mp\killstreaks\_airdrop::changecrateweight( "airdrop_assault", level.mortarconfig.id, level.mortarconfig.crateweight );
                continue;
            }

            game["player_holding_level_killstrek"] = 1;
            break;
        }
    }
}

wait_for_capture( var_0 )
{
    var_1 = watch_for_air_drop_death( var_0 );
    return !isdefined( var_1 );
}

watch_for_air_drop_death( var_0 )
{
    var_0 endon( "captured" );
    var_0 waittill( "death" );
    waittillframeend;
    return 1;
}

mortarcustomkillstreakfunc()
{
    level.killstreakfuncs[level.mortarconfig.id] = ::tryusemortars;
    level.killstreakweildweapons[level.mortarconfig.weaponname] = level.mortarconfig.id;
}

mortarcustombotkillstreakfunc()
{
    maps\mp\bots\_bots_ks::bot_register_killstreak_func( level.mortarconfig.id, maps\mp\bots\_bots_ks::bot_killstreak_simple_use );
}

tryusemortars( var_0, var_1 )
{
    if ( level.air_raid_active )
    {
        self iprintlnbold( &"KILLSTREAKS_AIR_SPACE_TOO_CROWDED" );
        return 0;
    }

    game["player_holding_level_killstrek"] = 0;
    level notify( "mortar_killstreak_used", self );

    if ( isdefined( level.mortarconfig.splashname ) )
        thread maps\mp\_utility::teamplayercardsplash( level.mortarconfig.splashname, self );

    return 1;
}

mortars_activate_at_end_of_match()
{
    level endon( "mortar_killstreak_used" );
    level waittill( "spawning_intermission" );
    level.ending_flourish = 1;
    mortar_fire( 0.1, 0.3, 2.5, 2.5, 6, level.players[0] );
}

update_mortars()
{
    level endon( "stop_dynamic_events" );
    common_scripts\utility::waitframe();

    if ( isdefined( self.sourcestructs ) )
        self.mortar_sources = common_scripts\utility::getstructarray( self.sourcestructs, "targetname" );
    else if ( isdefined( self.sourceents ) )
        self.mortar_sources = getentarray( self.sourceents, "targetname" );

    foreach ( var_1 in self.mortar_sources )
    {
        if ( !isdefined( var_1.radius ) )
            var_1.radius = 300;
    }

    self.mortar_targets = common_scripts\utility::getstructarray( self.targetstructs, "targetname" );

    foreach ( var_4 in self.mortar_targets )
    {
        if ( !isdefined( var_4.radius ) )
            var_4.radius = 100;
    }

    for (;;)
    {
        level.air_raid_active = 0;
        level.air_raid_team_called = "none";
        thread mortars_activate_at_end_of_match();
        level waittill( "mortar_killstreak_used", var_6 );
        level.air_raid_active = 1;
        level.air_raid_team_called = var_6.team;
        thread warning_audio();
        wait( self.launchdelay );
        mortar_fire( self.launchdelaymin, self.launchdelaymax, self.launchairtimemin, self.launchairtimemax, self.strikeduration, var_6 );
        level notify( "mortar_killstreak_end" );
    }
}

warning_audio()
{
    if ( !isdefined( self.warningsfxentname ) || !isdefined( self.warningsfx ) )
        return;

    if ( !isdefined( self.warning_sfx_ent ) )
        self.warning_sfx_ent = getent( self.warningsfxentname, "targetname" );

    if ( isdefined( self.warning_sfx_ent ) )
    {
        self.warning_sfx_ent playsound( self.warningsfx );
        wait( self.warningsfxduration );
        self.warning_sfx_ent stopsounds();
    }
}

mortar_fire( var_0, var_1, var_2, var_3, var_4, var_5 )
{
    var_6 = gettime() + var_4 * 1000;
    var_7 = random_mortars_get_source_structs( level.air_raid_team_called );

    if ( var_7.size <= 0 )
        return;

    level notify( "mortar_killstreak_start" );
    var_8 = 0;

    while ( var_6 > gettime() )
    {
        var_9 = self.projectileperloop;
        var_10 = 0;

        foreach ( var_12 in level.players )
        {
            if ( !maps\mp\_utility::isreallyalive( var_12 ) )
                continue;

            if ( level.teambased )
            {
                if ( var_12.team == level.air_raid_team_called )
                    continue;
            }
            else if ( isdefined( var_5 ) && var_12 == var_5 )
                continue;

            if ( var_12.spawntime + 8000 > gettime() )
                continue;

            var_13 = var_12 getvelocity();
            var_14 = var_2;

            if ( var_3 > var_2 )
                var_14 = randomfloatrange( var_2, var_3 );

            var_15 = var_12.origin + var_13 * var_14;
            var_16 = getnodesinradiussorted( var_15, 100, 0, 60 );

            foreach ( var_18 in var_16 )
            {
                if ( nodeexposedtosky( var_18 ) )
                {
                    var_19 = common_scripts\utility::random( var_7 );

                    if ( random_mortars_fire( var_19.origin, var_18.origin, var_14, var_5, 1, 1, var_19 ) )
                    {
                        wait( randomfloatrange( var_0, var_1 ) );
                        var_10++;
                        break;
                    }
                }
            }
        }

        if ( self.mortar_targets.size > 0 )
        {
            for ( var_7 = common_scripts\utility::array_randomize( var_7 ); var_10 < var_9; var_10++ )
            {
                var_19 = var_7[var_8];
                var_8++;

                if ( var_8 >= var_7.size )
                    var_8 = 0;

                var_22 = common_scripts\utility::random( self.mortar_targets );
                var_14 = var_2;

                if ( var_3 > var_2 )
                    var_14 = randomfloatrange( var_2, var_3 );

                var_23 = random_point_in_circle( var_19.origin, var_19.radius );
                var_24 = random_point_in_circle( var_22.origin, var_22.radius );
                thread random_mortars_fire( var_23, var_24, var_14, var_5, 0, 1, var_19 );
                wait( randomfloatrange( var_0, var_1 ) );
            }
        }
        else
            break;

        if ( isdefined( self.delaybetweenvolleys ) )
        {
            level notify( "mortar_volleyFinished" );
            wait( self.delaybetweenvolleys );
        }
    }
}

random_point_in_circle( var_0, var_1 )
{
    if ( var_1 > 0 )
    {
        var_2 = anglestoforward( ( 0, randomfloatrange( 0, 360 ), 0 ) );
        var_3 = randomfloatrange( 0, var_1 );
        var_0 = var_0 + var_2 * var_3;
    }

    return var_0;
}

random_mortars_fire( var_0, var_1, var_2, var_3, var_4, var_5, var_6 )
{
    if ( isdefined( self.launchsfx ) )
        playsoundatpos( var_0, self.launchsfx );
    else if ( isdefined( self.launchsfxarray ) && self.launchsfxarray.size > 0 && isdefined( self.launchsfxstartid ) )
    {
        if ( self.launchsfxstartid >= self.launchsfxarray.size )
            self.launchsfxstartid = 0;

        playsoundatpos( var_0, self.launchsfxarray[self.launchsfxstartid] );
        self.launchsfxstartid++;
    }

    var_7 = ( 0, 0, -800 );
    var_8 = trajectorycalculateinitialvelocity( var_0, var_1, var_7, var_2 );

    if ( isdefined( self.launchvfx ) )
    {
        if ( isdefined( var_6 ) && var_6.classname != "struct" )
            playfxontag( common_scripts\utility::getfx( self.launchvfx ), var_6, "tag_origin" );
        else
            playfx( common_scripts\utility::getfx( self.launchvfx ), var_0, var_8 );
    }

    if ( isdefined( var_4 ) && var_4 )
    {
        var_9 = trajectorycomputedeltaheightattime( var_8[2], -1 * var_7[2], var_2 / 2 );
        var_10 = ( var_1 - var_0 ) / 2 + var_0 + ( 0, 0, var_9 );

        if ( bullettracepassed( var_10, var_1, 0, undefined ) )
        {
            thread random_mortars_fire_run( var_0, var_1, var_2, var_3, var_8, var_5 );
            return 1;
        }
        else
            return 0;
    }

    random_mortars_fire_run( var_0, var_1, var_2, var_3, var_8, var_5 );
}

random_mortars_fire_run( var_0, var_1, var_2, var_3, var_4, var_5 )
{
    var_6 = 350;
    var_7 = createprojectileentity( var_0, self.model );
    var_7.in_use = 1;
    common_scripts\utility::waitframe();

    if ( isdefined( self.trailvfx ) )
        playfxontag( common_scripts\utility::getfx( self.trailvfx ), var_7, "tag_fx" );

    var_7.angles = vectortoangles( var_4 ) * ( -1, 1, 1 );

    if ( isdefined( self.incomingsfx ) )
        thread playsoundatposintime( self.incomingsfx, var_1, var_2 - 2.0 );

    var_7 movegravity( var_4, var_2 - 0.05 );

    if ( isdefined( self.rotateprojectiles ) && self.rotateprojectiles )
        var_7 rotatevelocity( common_scripts\utility::randomvectorrange( self.minrotatation, self.maxrotation ), var_2, 2, 0 );

    var_7 waittill( "movedone" );

    if ( level.createfx_enabled && !isdefined( level.players ) )
        level.players = [];

    if ( isdefined( var_3 ) )
        var_7 radiusdamage( var_1, 250, 750, 500, var_3, "MOD_EXPLOSIVE", self.weaponname );
    else
        var_7 radiusdamage( var_1, 140, 5, 5, undefined, "MOD_EXPLOSIVE", self.weaponname );

    playrumbleonposition( "artillery_rumble", var_1 );
    var_8 = var_6 * var_6;

    foreach ( var_10 in level.players )
    {
        if ( var_10 maps\mp\_utility::isusingremote() )
            continue;

        if ( distancesquared( var_1, var_10.origin ) > var_8 )
            continue;

        if ( var_10 damageconetrace( var_1 ) )
            var_10 thread maps\mp\gametypes\_shellshock::dirteffect( var_1 );
    }

    if ( var_5 )
    {
        if ( isdefined( self.impactvfx ) )
            playfx( common_scripts\utility::getfx( self.impactvfx ), var_1 );

        if ( isdefined( self.impactsfx ) )
            playsoundatpos( var_1, self.impactsfx );
    }

    var_7 delete();
}

playsoundatposintime( var_0, var_1, var_2 )
{
    wait( var_2 );
    playsoundatpos( var_1, var_0 );
}

createprojectileentity( var_0, var_1 )
{
    var_2 = spawn( "script_model", var_0 );
    var_2 setmodel( var_1 );
    return var_2;
}

random_mortars_get_source_structs( var_0 )
{
    var_1 = [];

    if ( level.teambased )
    {
        foreach ( var_3 in self.mortar_sources )
        {
            if ( isdefined( var_3.script_team ) && var_3.script_team == var_0 )
                var_1[var_1.size] = var_3;
        }
    }

    if ( var_1.size == 0 )
        var_1 = self.mortar_sources;

    return var_1;
}
