/*************************************************
 * Decompiled by Serious and Edited by SyndiShanX
 * Script: zm\_zm_ai_raps.csc
*************************************************/

#using scripts\codescripts\struct;
#using scripts\shared\callbacks_shared;
#using scripts\shared\clientfield_shared;
#using scripts\shared\duplicaterender_mgr;
#using scripts\shared\flag_shared;
#using scripts\shared\postfx_shared;
#using scripts\shared\system_shared;
#using scripts\shared\util_shared;
#using scripts\shared\vehicle_shared;
#using scripts\shared\visionset_mgr_shared;
#using scripts\zm\_util;
#using scripts\zm\_zm_equipment;
#using scripts\zm\_zm_perks;
#using scripts\zm\_zm_utility;
#namespace zm_ai_raps;

function autoexec __init__sytem__() {
  system::register("zm_ai_raps", &__init__, undefined, undefined);
}

function __init__() {
  clientfield::register("toplayer", "elemental_round_fx", 1, 1, "counter", &elemental_round_fx, 0, 0);
  clientfield::register("toplayer", "elemental_round_ring_fx", 1, 1, "counter", &elemental_round_ring_fx, 0, 0);
  visionset_mgr::register_visionset_info("zm_elemental_round_visionset", 1, 31, undefined, "zm_elemental_round_visionset");
  level._effect["elemental_round"] = "zombie/fx_meatball_round_tell_zod_zmb";
  vehicle::add_vehicletype_callback("raps", &_setup_);
}

function _setup_(localclientnum) {
  self.notifyonbulletimpact = 1;
  self thread wait_for_bullet_impact(localclientnum);
  self setanim("ai_zombie_zod_insanity_elemental_idle", 1);
  if(isDefined(level.debug_keyline_zombies) && level.debug_keyline_zombies) {
    self duplicate_render::set_dr_flag("keyline_active", 1);
    self duplicate_render::update_dr_filters(localclientnum);
  }
}

function elemental_round_fx(n_local_client, n_val_old, n_val_new, b_ent_new, b_initial_snap, str_field, b_demo_jump) {
  self endon("disconnect");
  if(isspectating(n_local_client)) {
    return;
  }
  self.n_elemental_round_fx_id = playfxoncamera(n_local_client, level._effect["elemental_round"]);
  wait(3.5);
  deletefx(n_local_client, self.n_elemental_round_fx_id);
}

function elemental_round_ring_fx(localclientnum, oldval, newval, bnewent, binitialsnap, fieldname, bwastimejump) {
  self endon("disconnect");
  if(isspectating(localclientnum)) {
    return;
  }
  self thread postfx::playpostfxbundle("pstfx_ring_loop");
  wait(1.5);
  self postfx::exitpostfxbundle();
}

function wait_for_bullet_impact(localclientnum) {
  self endon("entityshutdown");
  if(isDefined(self.scriptbundlesettings)) {
    settings = struct::get_script_bundle("vehiclecustomsettings", self.scriptbundlesettings);
  } else {
    return;
  }
  while(true) {
    self waittill("damage", attacker, impactpos, effectdir, partname);
    playFX(localclientnum, settings.weakspotfx, impactpos, effectdir);
  }
}