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

#using scripts\codescripts\struct;
#using scripts\shared\clientfield_shared;
#using scripts\shared\fx_shared;
#using scripts\shared\postfx_shared;
#using scripts\shared\system_shared;
#using scripts\shared\vehicle_shared;
#using scripts\shared\visionset_mgr_shared;
#using scripts\zm\_zm;
#namespace zm_ai_spiders;

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

function __init__() {
  level._effect["spider_round"] = "dlc2/island/fx_spider_round_tell";
  level._effect["spider_web_grenade_stuck"] = "dlc2/island/fx_web_grenade_tell";
  level._effect["spider_web_bgb_tear"] = "dlc2/island/fx_web_bgb_tearing";
  level._effect["spider_web_bgb_tear_complete"] = "dlc2/island/fx_web_bgb_reveal";
  level._effect["spider_web_perk_machine_tear"] = "dlc2/island/fx_web_perk_machine_tearing";
  level._effect["spider_web_perk_machine_tear_complete"] = "dlc2/island/fx_web_perk_machine_reveal";
  level._effect["spider_web_doorbuy_tear"] = "dlc2/island/fx_web_barrier_tearing";
  level._effect["spider_web_doorbuy_tear_complete"] = "dlc2/island/fx_web_barrier_reveal";
  level._effect["spider_web_tear_explosive"] = "dlc2/island/fx_web_impact_rocket";
  register_clientfields();
  vehicle::add_vehicletype_callback("spider", &function_7c1ef59b);
  visionset_mgr::register_visionset_info("zm_isl_parasite_spider_visionset", 9000, 16, undefined, "zm_isl_parasite_spider");
}

function __main__() {}

function register_clientfields() {
  clientfield::register("toplayer", "spider_round_fx", 9000, 1, "counter", &spider_round_fx, 0, 0);
  clientfield::register("toplayer", "spider_round_ring_fx", 9000, 1, "counter", &spider_round_ring_fx, 0, 0);
  clientfield::register("toplayer", "spider_end_of_round_reset", 9000, 1, "counter", &spider_end_of_round_reset, 0, 0);
  clientfield::register("scriptmover", "set_fade_material", 9000, 1, "int", &set_fade_material, 0, 0);
  clientfield::register("scriptmover", "web_fade_material", 9000, 3, "float", &web_fade_material, 0, 0);
  clientfield::register("missile", "play_grenade_stuck_in_web_fx", 9000, 1, "int", &play_grenade_stuck_in_web_fx, 0, 0);
  clientfield::register("scriptmover", "play_spider_web_tear_fx", 9000, getminbitcountfornum(4), "int", &play_spider_web_tear_fx, 0, 0);
  clientfield::register("scriptmover", "play_spider_web_tear_complete_fx", 9000, getminbitcountfornum(4), "int", &play_spider_web_tear_complete_fx, 0, 0);
}

function function_7c1ef59b(localclientnum) {
  self.str_tag_tesla_death_fx = "J_SpineUpper";
  self.str_tag_tesla_shock_eyes_fx = "J_SpineUpper";
}

function spider_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");
  setworldfogactivebank(n_local_client, 8);
  if(isspectating(n_local_client)) {
    return;
  }
  self.var_d5173f21 = playfxoncamera(n_local_client, level._effect["spider_round"]);
  playSound(0, "zmb_spider_round_webup", (0, 0, 0));
  wait(0.016);
  self thread postfx::playpostfxbundle("pstfx_parasite_spider");
  wait(3.5);
  deletefx(n_local_client, self.var_d5173f21);
}

function spider_end_of_round_reset(localclientnum, oldval, newval, bnewent, binitialsnap, fieldname, bwastimejump) {
  if(newval == 1) {
    setworldfogactivebank(localclientnum, 1);
  }
}

function spider_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 function_bea149a5(localclientnum, var_afc7cc94, var_b05b3457, b_on, n_alpha = 1, var_abf03d83 = 0, var_c0ce8db2 = 0) {
  self endon("entityshutdown");
  if(self.b_on === b_on) {
    return;
  }
  self.b_on = b_on;
  if(var_abf03d83) {
    if(b_on) {
      self transition_shader(localclientnum, n_alpha, var_afc7cc94);
    } else {
      self transition_shader(localclientnum, 0, var_afc7cc94);
    }
    return;
  }
  if(b_on) {
    var_24fbb6c6 = 0;
    i = 0;
    while(var_24fbb6c6 <= n_alpha) {
      self transition_shader(localclientnum, var_24fbb6c6, var_afc7cc94);
      if(var_c0ce8db2) {
        var_24fbb6c6 = sqrt(i);
      } else {
        var_24fbb6c6 = i;
      }
      wait(0.01);
      i = i + var_b05b3457;
    }
    self.var_bbfa5d7d = n_alpha;
    self transition_shader(localclientnum, n_alpha, var_afc7cc94);
  } else {
    if(isDefined(self.var_bbfa5d7d)) {
      var_bbfa5d7d = self.var_bbfa5d7d;
    } else {
      var_bbfa5d7d = 1;
    }
    var_24fbb6c6 = var_bbfa5d7d;
    i = var_bbfa5d7d;
    while(var_24fbb6c6 >= 0) {
      self transition_shader(localclientnum, var_24fbb6c6, var_afc7cc94);
      if(var_c0ce8db2) {
        var_24fbb6c6 = sqrt(i);
      } else {
        var_24fbb6c6 = i;
      }
      wait(0.01);
      i = i - var_b05b3457;
    }
    self transition_shader(localclientnum, 0, var_afc7cc94);
  }
}

function transition_shader(localclientnum, n_value, var_afc7cc94) {
  self mapshaderconstant(localclientnum, 0, "scriptVector" + var_afc7cc94, n_value, n_value, 0, 0);
}

function set_fade_material(localclientnum, oldval, newval, bnewent, binitialsnap, fieldname, bwastimejump) {
  self mapshaderconstant(localclientnum, 0, "scriptVector0", newval, 0, 0, 0);
}

function web_fade_material(localclientnum, oldval, newval, bnewent, binitialsnap, fieldname, bwastimejump) {
  var_f2efc20a = 0;
  if(newval <= 0) {
    var_f2efc20a = 0;
    var_32ee3d8b = newval;
  } else {
    var_f2efc20a = 1;
    var_32ee3d8b = newval;
  }
  self thread function_bea149a5(localclientnum, 0, 0.025, var_f2efc20a, var_32ee3d8b);
}

function play_grenade_stuck_in_web_fx(localclientnum, oldval, newval, bnewent, binitialsnap, fieldname, bwastimejump) {
  if(isDefined(self)) {
    playFXOnTag(localclientnum, level._effect["spider_web_grenade_stuck"], self, "tag_origin");
  }
}

function play_spider_web_tear_fx(localclientnum, oldval, newval, bnewent, binitialsnap, fieldname, bwastimejump) {
  switch (newval) {
    case 0: {
      if(isDefined(self) && isDefined(self.var_d5eda36c)) {
        stopfx(localclientnum, self.var_d5eda36c);
        self.var_d5eda36c = undefined;
      }
      if(isDefined(self) && isDefined(self.var_cac11e11)) {
        self stoploopsound(self.var_cac11e11, 0.5);
        self playSound(0, "zmb_spider_web_tear_stop");
        self.var_cac11e11 = undefined;
      }
      return;
    }
    case 1: {
      str_effect = "spider_web_bgb_tear";
      break;
    }
    case 2: {
      str_effect = "spider_web_perk_machine_tear";
      break;
    }
    case 3: {
      str_effect = "spider_web_doorbuy_tear";
      break;
    }
    default: {
      return;
    }
  }
  if(!isDefined(self.var_cac11e11)) {
    self.var_cac11e11 = self playLoopSound("zmb_spider_web_tear_loop", 1);
    self playSound(0, "zmb_spider_web_tear_start");
  }
  if(!isDefined(self.var_d5eda36c)) {
    self.var_d5eda36c = playFX(localclientnum, level._effect[str_effect], self.origin, anglesToForward(self.angles), anglestoup(self.angles));
  }
}

function play_spider_web_tear_complete_fx(localclientnum, oldval, newval, bnewent, binitialsnap, fieldname, bwastimejump) {
  switch (newval) {
    case 1: {
      str_effect = "spider_web_bgb_tear_complete";
      break;
    }
    case 2: {
      str_effect = "spider_web_perk_machine_tear_complete";
      break;
    }
    case 3: {
      str_effect = "spider_web_doorbuy_tear_complete";
      break;
    }
    case 4: {
      str_effect = "spider_web_tear_explosive";
      break;
    }
    default: {
      return;
    }
  }
  playFX(localclientnum, level._effect[str_effect], self.origin, anglesToForward(self.angles), anglestoup(self.angles));
}