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

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

    level.allow_level_killstreak = maps\mp\_utility::allowlevelkillstreaks();

    if ( !level.allow_level_killstreak || game["player_holding_level_killstrek"] )
        return;

    maps\mp\killstreaks\_airdrop::addcratetype( "airdrop_assault", "warhawk_mortars", 85, maps\mp\killstreaks\_airdrop::killstreakcratethink, maps\mp\killstreaks\_airdrop::get_friendly_crate_model(), maps\mp\killstreaks\_airdrop::get_enemy_crate_model(), &"KILLSTREAKS_HINTS_WARHAWK_MORTARS" );
    level thread watch_for_redriver_nuke_crate();
}

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

        if ( isdefined( var_0 ) && isdefined( var_0.cratetype ) && var_0.cratetype == "warhawk_mortars" )
        {
            disable_redriver_nuke();
            var_1 = wait_for_capture( var_0 );

            if ( !var_1 )
            {
                enable_redriver_nuke();
                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;
}

enable_redriver_nuke()
{
    maps\mp\killstreaks\_airdrop::changecrateweight( "airdrop_assault", "warhawk_mortars", 85 );
}

disable_redriver_nuke()
{
    maps\mp\killstreaks\_airdrop::changecrateweight( "airdrop_assault", "warhawk_mortars", 0 );
}

bridge_device_init()
{
    self.device_model = getent( "bridge_device_model", "targetname" );

    if ( !isdefined( self.device_model ) )
        return;

    self.mortar_delay_range = 3.0;
    self.end_of_match_volley = 0;
    self.device_det_radius = 600;
    self.device_det_splash = 200;
    var_0 = ( -1191, -1126, 394 );
    self.device_model.killcament = spawn( "script_model", var_0 );
    self.device_model.killcament setscriptmoverkillcam( "explosive" );
    thread bridge_device_sequence_waitforhit();
}

bridge_extras_init( var_0 )
{
    thread bridge_event_handle_glass( var_0 );
    thread bridge_event_handle_churchbells( var_0.device_model.origin );
    thread bridge_event_handle_bell_sounds();
    var_1 = get_bridge_vehicles();

    if ( var_1.size > 0 )
    {
        thread bridge_event_handle_vehicles( var_1, var_0 );
        thread bridge_event_handle_caralarms( var_1 );
    }

    thread bridge_device_scramble_radar( var_0 );
}

bridge_device_scramble_radar( var_0 )
{
    level endon( "bridge_fully_exploded" );
    var_1 = common_scripts\utility::getstruct( "device_scrambler", "targetname" );

    if ( !isdefined( var_1 ) )
        return;

    thread bridge_device_radioactive_sound( var_1.origin + ( -5, 0, -155 ) );
    level.device_scrambler_active = 1;

    for (;;)
    {
        level waittill( "connected", var_2 );

        if ( !isbot( var_2 ) )
            var_2 thread run_func_after_spawn( ::bridge_device_static, var_1.origin, var_0.device_det_radius );
    }
}

bridge_device_radioactive_sound( var_0 )
{
    var_1 = common_scripts\utility::play_loopsound_in_space( "emt_geiger_level1", var_0 );
    level waittill( "bridge_fully_exploded" );
    var_1 stoploopsound();
    var_1 delete();
}

run_func_after_spawn( var_0, var_1, var_2 )
{
    self endon( "disconnect" );
    self endon( "death" );
    self waittill( "spawned_player" );
    self thread [[ var_0 ]]( var_1, var_2 );
}

bridge_device_sequence_waitforhit()
{
    self.device_model playloopsound( "device_stage1_loop" );
    wait 2.0;
    var_0 = maps\mp\gametypes\_gameobjects::getnextobjid();
    objective_add( var_0, "active", self.device_model.origin, "waypoint_radioactive" );
    objective_onentity( var_0, self.device_model );
    self.curobjid = var_0;
    thread bridge_device_activate_at_end_of_match();
    level waittill( "bridge_device_activate", var_1 );
    disable_redriver_nuke();
    wait 2.0;
    wait 2.0;
    bridge_device_sequence_volley( var_1 );
}

bridge_device_sequence_volley( var_0 )
{
    var_1 = common_scripts\utility::getstruct( "mortar_launch_start", "targetname" );
    var_2 = common_scripts\utility::getstruct( var_1.target, "targetname" );

    for (;;)
    {
        thread bridge_device_mortar_attack( var_1, var_2, var_0 );

        if ( !isdefined( var_2.target ) )
            break;

        var_2 = common_scripts\utility::getstruct( var_2.target, "targetname" );
    }
}

bridge_device_activate_at_end_of_match()
{
    level endon( "bridge_device_activate" );
    level waittill( "spawning_intermission" );
    self.mortar_delay_range = 0.5;
    self.end_of_match_volley = 1;
    bridge_device_sequence_volley( level.players[0] );
}

random_mortars_incoming_sound( var_0 )
{
    playsoundatpos( var_0, "mortar_incoming" );
}

bridge_device_mortar_attack( var_0, var_1, var_2 )
{
    wait( randomfloatrange( 0.0, self.mortar_delay_range ) );
    var_3 = randomfloatrange( 4.0, 5.0 );

    if ( self.end_of_match_volley )
        var_3 = 2.5;

    var_4 = ( 0, 0, -800 );
    var_5 = trajectorycalculateinitialvelocity( var_0.origin, var_1.origin, var_4, var_3 );
    var_6 = 1;
    var_7 = 350;
    var_8 = random_mortars_get_model( var_0.origin );
    var_8.origin = var_0.origin;
    var_8.in_use = 1;
    common_scripts\utility::waitframe();
    playfxontag( common_scripts\utility::getfx( "random_mortars_trail" ), var_8, "tag_fx" );
    var_8.angles = vectortoangles( var_5 ) * ( -1, 1, 1 );
    playsoundatpos( var_0.origin, "mortar_launch" );
    maps\mp\_utility::delaythread( var_3 - 2.0, ::random_mortars_incoming_sound, var_1.origin );
    var_8 movegravity( var_5, var_3 );
    var_8 waittill( "movedone" );

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

    if ( !isdefined( var_2 ) )
        var_2 = undefined;

    var_8 radiusdamage( var_1.origin, 350, 750, 500, var_2, "MOD_EXPLOSIVE", "warhawk_mortar_mp" );
    playrumbleonposition( "artillery_rumble", var_1.origin );
    var_9 = var_7 * var_7;

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

        if ( distancesquared( var_1.origin, var_11.origin ) > var_9 )
            continue;

        if ( var_11 damageconetrace( var_1.origin ) )
            var_11 thread maps\mp\gametypes\_shellshock::dirteffect( var_1.origin );
    }

    if ( var_6 )
        playfx( common_scripts\utility::getfx( "mortar_impact_00" ), var_1.origin );

    stopfxontag( common_scripts\utility::getfx( "random_mortars_trail" ), var_8, "tag_fx" );

    if ( isdefined( var_1.script_noteworthy ) && var_1.script_noteworthy == "device_target" )
        thread bridge_device_mortar_hit_nuke( var_2, var_8 );

    wait 0.05;
    var_8 delete();
}

bridge_device_mortar_hit_nuke( var_0, var_1 )
{
    level notify( "bridge_trigger_explode" );
    objective_delete( self.curobjid );
    earthquake( 0.85, 0.5, self.device_model.origin, 2500 );
    var_2 = self.device_det_radius;
    var_3 = var_2 + self.device_det_splash;
    var_4 = var_2 + 3.0 * self.device_det_splash;

    foreach ( var_6 in level.participants )
    {
        if ( isplayer( var_6 ) && var_6.sessionstate != "playing" )
            continue;

        var_6 thread update_bridge_event_player_effects( self.device_model.origin, var_2, var_4, self.end_of_match_volley );
        var_7 = distancesquared( self.device_model.origin, var_6.origin );

        if ( var_7 > var_3 * var_3 )
            continue;

        var_8 = var_0;

        if ( !isdefined( var_8 ) )
            var_8 = undefined;
        else if ( isdefined( var_8.team ) && isdefined( var_6.team ) && var_8.team == var_6.team )
            var_8 = var_1;

        if ( var_7 > var_2 * var_2 )
        {
            var_6 dodamage( var_6.maxhealth * 0.9, self.device_model.origin, var_8, self.device_model, "MOD_EXPLOSIVE" );
            continue;
        }

        var_6 dodamage( 1000, self.device_model.origin, var_8, self.device_model, "MOD_EXPLOSIVE" );
    }

    var_10 = var_3 * var_3;
    var_11 = maps\mp\agents\_agent_utility::getactiveagentsoftype( "dog" );

    foreach ( var_13 in var_11 )
    {
        var_14 = distancesquared( self.device_model.origin, var_13.origin );

        if ( var_14 <= var_10 )
            var_13 maps\mp\agents\_agent_utility::killdog();
    }

    destroytargetarray( self.device_model.origin, var_10, level.remote_uav );

    foreach ( var_17 in level.deployable_box )
        destroytargetarray( self.device_model.origin, var_10, var_17 );

    destroytargetarray( self.device_model.origin, var_10, level.turrets );
    destroytargetarray( self.device_model.origin, var_10, level.uplinks );
    destroytargetarray( self.device_model.origin, var_10, level.placedims );
    destroytargetarray( self.device_model.origin, var_10, level.mines );
    self.device_model stoploopsound( "device_stage1_loop" );
    self.device_model notsolid();
    self.device_model hide();
    self.device_model maps\mp\_movers::notify_moving_platform_invalid();

    if ( !self.end_of_match_volley )
        visionsetnaked( "mp_ca_red_river_exploding", 0.5 );

    physicsexplosionsphere( self.device_model.origin, var_3, var_2, 1.0 );
    wait 0.5;
    level notify( "bridge_fully_exploded" );
    maps\mp\_compass::setupminimap( "compass_map_mp_ca_red_river_exploded" );
    setexpfog( 6.6, 5530, 0.78, 0.81, 0.75, 0.87, 0.77 );

    if ( !self.end_of_match_volley )
    {
        if ( isdefined( level.nukedetonated ) )
        {
            visionsetnaked( "", 3.0 );
            maps\mp\killstreaks\_nuke::setnukeaftermathvision( 0 );
        }
        else
            visionsetnaked( "mp_ca_red_river_exploded", 3.0 );
    }
}

destroytargetarray( var_0, var_1, var_2 )
{
    foreach ( var_4 in var_2 )
    {
        var_5 = distancesquared( var_0, var_4.origin );

        if ( var_5 < var_1 )
            var_4 notify( "death" );
    }
}

update_bridge_event_player_effects( var_0, var_1, var_2, var_3 )
{
    self playrumbleonentity( "artillery_rumble" );
    wait 0.3;

    if ( !var_3 && !maps\mp\_utility::isusingremote() )
    {
        var_4 = 0.0;
        var_5 = distancesquared( self.origin, var_0 );

        if ( var_5 < var_2 * var_2 )
        {
            var_4 = 8.0;

            if ( var_5 > var_1 * var_1 )
                var_4 = var_4 * ( 1 - ( sqrt( var_5 ) - var_1 ) / ( var_2 - var_1 ) );
        }

        if ( var_4 > 0.0 )
            self shellshock( "mp_ca_red_river_event", var_4 );
    }

    wait 1.0;
    self playrumbleonentity( "artillery_rumble" );
    wait 1.3;
    self playrumbleonentity( "artillery_rumble" );
}

bridge_device_static( var_0, var_1 )
{
    self endon( "disconnect" );
    wait( randomfloat( 0.5 ) );
    maps\mp\killstreaks\_emp_common::staticfieldinit();
    childthread bridge_device_static_update( var_0, var_1 );
    level waittill( "bridge_fully_exploded" );
    maps\mp\killstreaks\_emp_common::staticfieldsetstrength( 0 );
}

bridge_device_static_update( var_0, var_1 )
{
    level endon( "bridge_trigger_explode" );
    var_2 = var_1 * var_1;

    for (;;)
    {
        var_3 = length2dsquared( self.origin - var_0 ) < var_2;
        maps\mp\killstreaks\_emp_common::staticfieldsetstrength( var_3 );
        wait 0.5;
    }
}

random_mortars_get_model( var_0 )
{
    var_1 = spawn( "script_model", var_0 );
    var_1 setmodel( "projectile_rpg7" );
    return var_1;
}

get_bridge_vehicles()
{
    var_0 = getscriptablearray( "vehicle_pickup_destructible_mp_rr", "targetname" );

    if ( var_0.size <= 0 )
    {

    }

    return var_0;
}

bridge_event_handle_vehicles( var_0, var_1 )
{
    var_2 = var_1.device_det_radius + var_1.device_det_splash * 2.0;
    var_2 = var_2 * var_2;
    var_3 = var_1.device_model.origin;

    foreach ( var_5 in var_0 )
    {
        if ( distancesquared( var_5.origin, var_3 ) <= var_2 )
            var_5 thread bridge_event_handle_vehicle();
    }
}

bridge_event_handle_vehicle()
{
    self endon( "death" );
    level waittill( "bridge_fully_exploded" );
    self setscriptablepartstate( 0, 3 );
}

bridge_event_handle_caralarms( var_0 )
{
    common_scripts\utility::array_thread( var_0, ::bridge_event_update_caralarm );
}

bridge_event_update_caralarm()
{
    self endon( "death" );
    level waittill( "bridge_fully_exploded" );
    wait( randomfloatrange( 0.75, 1.25 ) );
    thread bridge_event_play_caralarm();
}

bridge_event_play_caralarm()
{
    var_0 = common_scripts\utility::play_loopsound_in_space( "rr_car_alarm", self gettagorigin( "tag_engine" ) );
    var_1 = randomfloatrange( 12.0, 16.0 );
    common_scripts\utility::waittill_any_timeout( var_1, "death" );
    var_0 stoploopsound( "rr_car_alarm" );
    var_0 playsound( "rr_car_alarm_off" );
    wait 5;
    var_0 delete();
}

bridge_event_handle_glass( var_0 )
{
    var_1 = getglassarray( "bridge_event_glass" );

    if ( var_1.size <= 0 )
        return;

    var_2 = var_0.device_model.origin;
    level waittill( "bridge_fully_exploded" );

    foreach ( var_4 in var_1 )
        destroyglass( var_4, getglassorigin( var_4 ) - var_2 );
}

bridge_event_handle_churchbells( var_0 )
{
    var_1 = getentarray( "church_bell", "targetname" );

    if ( var_1.size <= 0 )
        return;

    level.small_bells = 0;
    common_scripts\utility::array_thread( var_1, ::redriver_detecthit_churchbell );
    common_scripts\utility::array_thread( var_1, ::bridge_event_update_churchbell, var_0 );
}

redriver_detecthit_churchbell()
{
    self setcandamage( 1 );
    self.is_swaying = 0;
    var_0 = bell_sound_alias( self.script_noteworthy );

    for (;;)
    {
        self waittill( "damage", var_1, var_2, var_3, var_4, var_5 );

        if ( !isdefined( var_2 ) || !isplayer( var_2 ) )
            continue;

        var_6 = var_2 getcurrentweapon();

        if ( !isdefined( var_6 ) || weaponclass( var_6 ) != "sniper" )
            continue;

        self playsound( var_0 );
        thread redriver_update_hitsway_churchbell( var_2 );
        wait 0.5;
    }
}

redriver_update_hitsway_churchbell( var_0 )
{
    level endon( "bridge_fully_exploded" );

    if ( self.is_swaying )
        return;

    var_1 = anglestoright( self.angles );
    var_2 = vectornormalize( var_0.origin - self.origin );
    var_3 = vectordot( var_1, var_2 ) * 2.0;

    if ( var_3 > 0.0 )
        var_3 = max( 0.3, var_3 );
    else
        var_3 = min( -0.3, var_3 );

    self.is_swaying = 1;
    self rotateroll( 15 * var_3, 1.0, 0, 0.5 );
    wait 1;
    self rotateroll( -25 * var_3, 2.0, 0.5, 0.5 );
    wait 2;
    self rotateroll( 15 * var_3, 1.5, 0.5, 0.5 );
    wait 1.5;
    self rotateroll( -5 * var_3, 1.0, 0.5, 0.5 );
    wait 1.0;
    self.is_swaying = 0;
}

bridge_event_handle_bell_sounds()
{
    level waittill( "bridge_device_activate" );
    playsoundatpos( ( -510, -1617, 556 ), "scn_church_bells_long_large" );
    playsoundatpos( ( -480, -1986, 423 ), "scn_church_bells_long_small" );
}

bridge_event_update_churchbell( var_0 )
{
    var_1 = bell_sound_alias( self.script_noteworthy );
    level waittill( "bridge_device_activate" );
    var_2 = anglestoright( self.angles );
    var_3 = vectornormalize( var_0 - self.origin );
    var_4 = vectordot( var_2, var_3 ) * 2.0;

    if ( var_4 > 0.0 )
        var_4 = max( 0.7, var_4 );
    else
        var_4 = min( -0.7, var_4 );

    var_5 = randomfloatrange( 0.1, 0.8 );
    self.is_swaying = 1;
    self.angles = ( self.angles[0], self.angles[1], 0 );
    self rotateroll( 40 * var_4, var_5, 0, var_5 );
    wait( var_5 );
    self rotateroll( -70 * var_4, 2.0, 0.5, 0.5 );
    wait 1.75;
    wait 0.25;
    self rotateroll( 60 * var_4, 2.0, 0.5, 0.5 );
    wait 1.75;
    wait 0.25;
    self rotateroll( -50 * var_4, 2.0, 0.5, 0.5 );
    wait 1.75;
    wait 0.25;
    self rotateroll( 40 * var_4, 2.0, 0.5, 0.5 );
    wait 1.75;
    wait 0.25;
    self rotateroll( -30 * var_4, 2.0, 0.5, 0.5 );
    wait 1.75;
    wait 0.25;
    self rotateroll( 20 * var_4, 1.5, 0.5, 0.5 );
    wait 1.5;
    self rotateroll( -15 * var_4, 1.3, 0.5, 0.5 );
    wait 1.3;
    self rotateroll( 5 * var_4, 1.0, 0.5, 0.5 );
    wait 1.0;
    self.is_swaying = 0;
}

bell_sound_alias( var_0 )
{
    var_1 = "rr_church_bell";

    if ( isdefined( var_0 ) && var_0 == "small" )
    {
        if ( !isdefined( level.small_bells ) )
            level.small_bells = 0;

        level.small_bells++;

        if ( level.small_bells == 1 )
            var_1 = "rr_church_bell_smallest";
        else if ( level.small_bells == 2 )
            var_1 = "rr_church_bell_smaller";
        else
            var_1 = "rr_church_bell_small";
    }

    return var_1;
}
