/*************************************************
 * Decompiled by Serious and Edited by SyndiShanX
 * Script: zm\_zm_weap_elemental_bow_demongate.gsc
*************************************************/

#using scripts\codescripts\struct;
#using scripts\shared\ai\systems\gib;
#using scripts\shared\ai\zombie_shared;
#using scripts\shared\ai\zombie_utility;
#using scripts\shared\ai_shared;
#using scripts\shared\array_shared;
#using scripts\shared\callbacks_shared;
#using scripts\shared\clientfield_shared;
#using scripts\shared\flag_shared;
#using scripts\shared\fx_shared;
#using scripts\shared\math_shared;
#using scripts\shared\scene_shared;
#using scripts\shared\spawner_shared;
#using scripts\shared\system_shared;
#using scripts\shared\util_shared;
#using scripts\zm\_util;
#using scripts\zm\_zm_bgb;
#using scripts\zm\_zm_score;
#using scripts\zm\_zm_spawner;
#using scripts\zm\_zm_utility;
#using scripts\zm\_zm_weap_elemental_bow;
#using scripts\zm\_zm_weapons;
#namespace _zm_weap_elemental_bow_demongate;

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

function __init__() {
  level.var_edf1e590 = getweapon("elemental_bow_demongate");
  level.var_e106fba5 = getweapon("elemental_bow_demongate4");
  level.var_ecd0c077 = [];
  clientfield::register("toplayer", "elemental_bow_demongate" + "_ambient_bow_fx", 5000, 1, "int");
  clientfield::register("missile", "elemental_bow_demongate" + "_arrow_impact_fx", 5000, 1, "int");
  clientfield::register("missile", "elemental_bow_demongate4" + "_arrow_impact_fx", 5000, 1, "int");
  clientfield::register("scriptmover", "demongate_portal_fx", 5000, 1, "int");
  clientfield::register("toplayer", "demongate_portal_rumble", 1, 1, "int");
  clientfield::register("scriptmover", "demongate_wander_locomotion_anim", 5000, 1, "int");
  clientfield::register("scriptmover", "demongate_attack_locomotion_anim", 5000, 1, "int");
  clientfield::register("scriptmover", "demongate_chomper_fx", 5000, 1, "int");
  clientfield::register("scriptmover", "demongate_chomper_bite_fx", 5000, 1, "counter");
}

function __main__() {
  callback::on_connect(&function_71c07c9d);
}

function function_71c07c9d() {
  self thread zm_weap_elemental_bow::function_982419bb("elemental_bow_demongate");
  self thread zm_weap_elemental_bow::function_ececa597("elemental_bow_demongate", "elemental_bow_demongate4");
  self thread zm_weap_elemental_bow::function_7bc6b9d("elemental_bow_demongate", "elemental_bow_demongate4", &function_7e0efd7);
}

function function_7e0efd7(weapon, position, radius, attacker, normal) {
  if(weapon.name == "elemental_bow_demongate4") {
    self thread function_19575106(weapon, position, attacker, normal);
  } else {
    attacker clientfield::set("elemental_bow_demongate" + "_arrow_impact_fx", 1);
    self thread function_ee626351(position, attacker);
  }
}

function function_95ad1040(var_be00572f, var_c211b5bb) {
  if(abs(var_c211b5bb[2]) < 0.2) {
    var_be00572f = var_be00572f + (var_c211b5bb * 16);
    a_trace = bulletTrace(var_be00572f, var_be00572f + vectorscale((0, 0, 1), 64), 0, undefined);
    if(a_trace["fraction"] < 1) {
      var_be00572f = a_trace["position"] - vectorscale((0, 0, 1), 64);
    }
    a_trace = bulletTrace(var_be00572f, var_be00572f - vectorscale((0, 0, 1), 64), 0, undefined);
    if(a_trace["fraction"] < 1) {
      var_be00572f = a_trace["position"] + vectorscale((0, 0, 1), 64);
    }
  } else {
    var_85a9344d = var_c211b5bb[2] * 64;
    var_be00572f = var_be00572f + (0, 0, var_85a9344d);
  }
  return var_be00572f;
}

function function_19575106(weapon, position, attacker, normal) {
  position = function_95ad1040(position, normal);
  var_884470ed = vectortoangles(normal);
  var_884470ed = var_884470ed + vectorscale((0, 1, 0), 90);
  var_884470ed = var_884470ed * (0, 1, 0);
  var_e1784c2b = util::spawn_model("tag_origin", position, var_884470ed);
  var_e1784c2b clientfield::set("demongate_portal_fx", 1);
  var_e1784c2b.var_a5e3922a = 1;
  radiusdamage(position, 96, level.zombie_health, level.zombie_health, self, "MOD_UNKNOWN", level.var_e106fba5);
  wait(0.25);
  var_e1784c2b thread function_292e5297();
  if(getdvarint("splitscreen_playerCount") > 2) {
    var_76d7100b = 4 * level.zombie_health;
  } else {
    var_76d7100b = 2 * level.zombie_health;
  }
  if(level.var_ecd0c077.size > 12) {
    var_33efb6fa = 2;
  } else {
    var_33efb6fa = int(((zombie_utility::get_current_zombie_count() + level.zombie_total) * level.zombie_health) / var_76d7100b);
    if(getdvarint("splitscreen_playerCount") > 2) {
      var_33efb6fa = math::clamp(var_33efb6fa, 4, 4);
    } else {
      var_33efb6fa = math::clamp(var_33efb6fa, 4, 6);
    }
  }
  var_c93049d7 = 0;
  for(i = 0; i < var_33efb6fa; i++) {
    var_963441bd[i] = function_a9eddc64(position, var_884470ed - vectorscale((0, 1, 0), 90), i);
    var_963441bd[i] thread function_a3e439a5(self, position);
    n_wait_time = 0.1;
    var_c93049d7 = var_c93049d7 + n_wait_time;
    wait(n_wait_time);
  }
  if(var_c93049d7 < 2) {
    wait(2 - var_c93049d7);
  }
  wait(2.5);
  var_e1784c2b clientfield::set("demongate_portal_fx", 0);
  wait(2);
  var_e1784c2b notify("hash_d6e52217");
  var_e1784c2b.var_a5e3922a = 0;
  wait(1.6);
  var_e1784c2b delete();
}

function function_292e5297() {
  self endon("hash_d6e52217");
  while(true) {
    foreach(e_player in level.activeplayers) {
      if(isDefined(e_player) && (!(isDefined(e_player.var_fbdab725) && e_player.var_fbdab725))) {
        if(distancesquared(e_player.origin, self.origin) < 9216) {
          e_player thread function_3f55c7ab(self);
        }
      }
    }
    wait(0.05);
  }
}

function function_3f55c7ab(var_e1784c2b) {
  self endon("disconnect");
  self endon("bled_out");
  self.var_fbdab725 = 1;
  self clientfield::set_to_player("demongate_portal_rumble", 1);
  while(distancesquared(self.origin, var_e1784c2b.origin) < 9216 && (isDefined(var_e1784c2b.var_a5e3922a) && var_e1784c2b.var_a5e3922a)) {
    wait(0.05);
  }
  self.var_fbdab725 = 0;
  self clientfield::set_to_player("demongate_portal_rumble", 0);
}

function function_ee626351(position, attacker) {
  v_angles = anglesToForward(attacker.angles) * -1;
  var_7119a95d = function_a9eddc64(position, v_angles);
  wait(0.1);
  var_7119a95d thread function_ab77890b(self);
}

function function_a9eddc64(position, v_angles, n_count = 1) {
  var_7119a95d = util::spawn_model("c_zom_chomper", position, v_angles);
  var_7119a95d clientfield::set("demongate_chomper_fx", 1);
  var_7119a95d flag::init("chomper_attacking");
  var_7119a95d flag::init("demongate_chomper_despawning");
  if(getdvarint("splitscreen_playerCount") > 2) {
    var_76d7100b = 4 * level.zombie_health;
  } else {
    var_76d7100b = 2 * level.zombie_health;
  }
  var_7119a95d.var_fcd07456 = var_76d7100b;
  var_7119a95d.var_603f1f19 = 1;
  var_7119a95d thread function_1d3c583d();
  var_b194ff34 = 0;
  var_5f2cce52 = level.var_ecd0c077.size - 12;
  if(var_5f2cce52 > 0) {
    foreach(var_c7cc4fbd in level.var_ecd0c077) {
      if(!var_c7cc4fbd flag::get("chomper_attacking") && (!(isDefined(var_c7cc4fbd.var_e3146903) && var_c7cc4fbd.var_e3146903))) {
        var_c7cc4fbd.n_timer = 3;
        var_b194ff34++;
        if(var_b194ff34 > var_5f2cce52) {
          break;
        }
      }
    }
  }
  if(!isDefined(level.var_ecd0c077)) {
    level.var_ecd0c077 = [];
  } else if(!isarray(level.var_ecd0c077)) {
    level.var_ecd0c077 = array(level.var_ecd0c077);
  }
  level.var_ecd0c077[level.var_ecd0c077.size] = var_7119a95d;
  return var_7119a95d;
}

function function_308e6c6f() {
  self flag::set("demongate_chomper_despawning");
  if(!(isDefined(self.var_dd49270d) && self.var_dd49270d)) {
    self.var_dd49270d = 1;
    if(!isDefined(level.var_a9ac7b97)) {
      level.var_a9ac7b97 = gettime();
    } else if(level.var_a9ac7b97 == gettime()) {
      wait(randomfloatrange(0.1, 0.2));
    }
    level.var_a9ac7b97 = gettime();
    self moveto(self.origin + vectorscale((0, 0, 1), 96), 1.4);
    self rotatepitch(-90, 0.4);
    wait(1.4);
    self moveto(self.origin, 0.1);
    self clientfield::set("demongate_chomper_fx", 0);
    wait(3);
    self notify("hash_16664ab4");
    level.var_ecd0c077 = array::exclude(level.var_ecd0c077, self);
    self delete();
  }
}

function function_1d3c583d() {
  self endon("demongate_chomper_despawning");
  self.n_timer = 0;
  while(self.n_timer < 3) {
    if(!self flag::get("chomper_attacking") && (!(isDefined(self.var_e3146903) && self.var_e3146903))) {
      self.n_timer = self.n_timer + 0.05;
    }
    wait(0.05);
  }
  while(self flag::get("chomper_attacking")) {
    wait(0.1);
  }
  self thread function_308e6c6f();
}

function function_a3e439a5(e_player, var_67eb721b) {
  self.var_e3146903 = 1;
  self.origin = self.origin + (0, 0, randomintrange(int(-51.2), int(51.2)));
  self.angles = (self.angles[0] + (randomintrange(-30, 30)), self.angles[1] + (randomintrange(-45, 45)), self.angles[2]);
  var_69a783ad = self.origin + (anglesToForward(self.angles) * 96);
  self.angles = (0, self.angles[1], 0);
  self moveto(var_69a783ad, 0.4);
  wait(0.4);
  self.var_e3146903 = 0;
  self function_ab77890b(e_player);
}

function function_ab77890b(e_player) {
  self function_99d2c8aa(e_player);
  if(isDefined(self.target_enemy)) {
    self function_b6e7ec91(e_player);
  } else {
    self thread function_9fcea3e8(e_player);
  }
}

function function_9fcea3e8(e_player) {
  self endon("demongate_chomper_despawning");
  self endon("death");
  if(!isDefined(self)) {
    return;
  }
  if(self flag::get("demongate_chomper_despawning")) {
    return;
  }
  self flag::clear("chomper_attacking");
  self clientfield::set("demongate_wander_locomotion_anim", 1);
  var_13fe538c = randomfloatrange(5, 15);
  var_3a00cdf5 = randomfloatrange(15, 45);
  var_6003485e = randomfloatrange(15, 45);
  var_13fe538c = (randomint(100) < 50 ? var_13fe538c : var_13fe538c * -1);
  var_3a00cdf5 = (randomint(100) < 50 ? var_3a00cdf5 : var_3a00cdf5 * -1);
  var_6003485e = (randomint(100) < 50 ? var_6003485e : var_6003485e * -1);
  if(zm_utility::is_player_valid(e_player)) {
    var_6073ac1c = e_player.angles;
    var_150aaea = e_player getEye();
  } else {
    var_6073ac1c = self.angles;
    var_150aaea = self.origin;
  }
  var_9ef67615 = (var_6073ac1c[0] + var_13fe538c, var_6073ac1c[1] + var_3a00cdf5, var_6073ac1c[2] + var_6003485e);
  var_962558f1 = vectornormalize(anglesToForward(var_9ef67615));
  a_trace = physicstraceex(var_150aaea, var_150aaea + (var_962558f1 * 512), vectorscale((-1, -1, -1), 16), vectorscale((1, 1, 1), 16));
  var_69a783ad = a_trace["position"] + (var_962558f1 * -32);
  n_dist = distance(self.origin, var_69a783ad);
  n_time = n_dist / 48;
  v_rotate = var_69a783ad - self.origin;
  v_rotate = (0, v_rotate[1], 0);
  if(!isDefined(level.var_a9ac7b97)) {
    level.var_a9ac7b97 = gettime();
  } else if(level.var_a9ac7b97 == gettime()) {
    wait(randomfloatrange(0.1, 0.2));
  }
  level.var_a9ac7b97 = gettime();
  self moveto(var_69a783ad, n_time);
  self rotateto(vectortoangles(v_rotate), n_time * 0.5);
  self thread function_f9dd2ee2(e_player);
  self util::waittill_any_timeout(n_time * 2, "movedone", "demongate_chomper_found_target", "demongate_chomper_despawning", "death");
  if(isDefined(self.target_enemy)) {
    self clientfield::set("demongate_wander_locomotion_anim", 0);
    self function_b6e7ec91(e_player);
  } else {
    self thread function_9fcea3e8(e_player);
  }
}

function function_f9dd2ee2(e_player) {
  self endon("demongate_chomper_despawning");
  self endon("demongate_chomper_found_target");
  self endon("movedone");
  self endon("death");
  while(!isDefined(self.target_enemy)) {
    wait(0.2);
    self thread function_99d2c8aa(e_player);
  }
}

function function_b6e7ec91(e_player) {
  var_c32fcb8f = self.target_enemy.health;
  self function_5dee133f();
  if(zm_weap_elemental_bow::function_5aec3adc(self.target_enemy)) {
    var_aad66aa4 = randomintrange(1, 7);
    var_7364b0dd = self.target_enemy.missinglegs;
    self.target_enemy.var_98056717 = 1;
    self.var_603f1f19 = 0;
    self.var_fcd07456 = self.var_fcd07456 - var_c32fcb8f;
    self thread function_3c03253d(var_aad66aa4, var_7364b0dd);
    self thread function_3a5a56c7();
    self thread function_67903a83(e_player);
    if(isDefined(self.target_enemy.isdog) && self.target_enemy.isdog || isvehicle(self.target_enemy)) {
      n_wait_time = 0.8;
    } else {
      if(self.target_enemy.archetype === "mechz") {
        n_wait_time = 2.6;
        self.var_fcd07456 = 0;
      } else {
        n_wait_time = randomfloatrange(2, 3);
        self.target_enemy setplayercollision(0);
      }
    }
    self.target_enemy util::waittill_notify_or_timeout("death", n_wait_time);
    self notify("hash_368634cd");
    self function_43415734(var_aad66aa4, var_7364b0dd);
    if(self.var_fcd07456 < 1) {
      self thread function_308e6c6f();
      return;
    }
  } else if(isDefined(self.target_enemy)) {
    self.target_enemy.var_bc9b5fbd = 0;
  }
  self flag::clear("chomper_attacking");
  self thread function_ab77890b(e_player);
}

function function_3a5a56c7() {
  self endon("death");
  self endon("hash_368634cd");
  if(self.target_enemy.archetype === "mechz") {
    while(true) {
      self clientfield::increment("demongate_chomper_bite_fx", 1);
      wait(1);
    }
  } else {
    while(true) {
      self waittill("hash_48e4a902");
      self clientfield::increment("demongate_chomper_bite_fx", 1);
    }
  }
}

function function_3c03253d(var_aad66aa4, var_7364b0dd) {
  self.target_enemy endon("death");
  if(isDefined(self.target_enemy.isdog) && self.target_enemy.isdog) {
    self.target_enemy ai::set_ignoreall(1);
  } else {
    if(self.target_enemy.archetype === "mechz") {
      self thread function_c5e710f7();
    } else {
      if(isvehicle(self.target_enemy)) {
        self.target_enemy.ignoreall = 1;
      } else {
        if(isDefined(var_7364b0dd) && var_7364b0dd) {
          self.target_enemy scene::play("ai_zm_dlc1_zombie_demongate_chomper_attack_crawler", array(self.target_enemy, self));
        } else {
          self.target_enemy scene::init("ai_zm_dlc1_zombie_demongate_chomper_attack_0" + var_aad66aa4, array(self.target_enemy, self));
          self.target_enemy scene::play("ai_zm_dlc1_zombie_demongate_chomper_attack_0" + var_aad66aa4, array(self.target_enemy, self));
        }
      }
    }
  }
}

function function_c5e710f7() {
  var_f1d8a1fc = self.target_enemy;
  self endon("death");
  self endon("hash_368634cd");
  var_f1d8a1fc endon("death");
  while(true) {
    var_e388672 = isDefined(var_f1d8a1fc.has_faceplate) && (var_f1d8a1fc.has_faceplate ? 6 : 1);
    var_85b50675 = anglesToForward(self.target_enemy.angles) * var_e388672;
    self.origin = self.target_enemy gettagorigin("j_faceplate") + var_85b50675;
    self.angles = vectortoangles(var_85b50675 * -1);
    wait(0.05);
  }
}

function function_43415734(var_aad66aa4, var_7364b0dd) {
  if(isDefined(self.target_enemy) && self.target_enemy.archetype === "mechz") {
    self.target_enemy thread function_aa5b14d0();
    return;
  }
  if(isDefined(self.target_enemy) && (!(isDefined(self.target_enemy.isdog) && self.target_enemy.isdog))) {
    if(isDefined(var_7364b0dd) && var_7364b0dd) {
      self.target_enemy thread scene::stop("ai_zm_dlc1_zombie_demongate_chomper_attack_crawler");
    } else {
      self.target_enemy thread scene::stop("ai_zm_dlc1_zombie_demongate_chomper_attack_0" + var_aad66aa4);
    }
  }
  if(isDefined(var_7364b0dd) && var_7364b0dd) {
    self thread scene::stop("ai_zm_dlc1_zombie_demongate_chomper_attack_crawler");
  } else {
    self thread scene::stop("ai_zm_dlc1_zombie_demongate_chomper_attack_0" + var_aad66aa4);
  }
}

function function_aa5b14d0() {
  self endon("death");
  self.var_d3c478a0 = 1;
  wait(16);
  self.var_d3c478a0 = 0;
}

function function_5dee133f() {
  self flag::set("chomper_attacking");
  var_b710c4e5 = self.target_enemy getEye();
  n_dist = distance(self.origin, var_b710c4e5);
  n_loop_count = 1;
  var_4e26e12a = (math::cointoss() ? 1 : -1);
  self clientfield::set("demongate_attack_locomotion_anim", 1);
  while(n_dist > 32 && isDefined(self.target_enemy) && isalive(self.target_enemy)) {
    var_b710c4e5 = self.target_enemy getEye();
    n_time = n_dist / 640;
    var_5d76b65c = 1 / n_loop_count;
    var_ef096040 = vectorscale((0, 0, 1), 160) * var_5d76b65c;
    var_c5a0d28e = (anglestoright(vectortoangles(var_b710c4e5 - self.origin))) * 256;
    var_c5a0d28e = var_c5a0d28e * var_5d76b65c;
    var_c5a0d28e = var_c5a0d28e * var_4e26e12a;
    var_74490e48 = (var_b710c4e5 + var_c5a0d28e) + var_ef096040;
    v_rotate = var_74490e48 - self.origin;
    v_rotate = (0, v_rotate[1], 0);
    if(!isDefined(level.var_a9ac7b97)) {
      level.var_a9ac7b97 = gettime();
    } else if(level.var_a9ac7b97 == gettime()) {
      wait(randomfloatrange(0.1, 0.2));
    }
    level.var_a9ac7b97 = gettime();
    self moveto(var_74490e48, n_time);
    self rotateto(vectortoangles(v_rotate), n_time * 0.5);
    n_time = n_time * 0.3;
    n_time = (n_time < 0.1 ? 0.1 : n_time);
    wait(n_time);
    n_loop_count++;
    n_dist = distance(self.origin, var_b710c4e5);
  }
  self clientfield::set("demongate_attack_locomotion_anim", 0);
  if(isDefined(self.target_enemy) && isalive(self.target_enemy)) {
    self.origin = var_b710c4e5;
  }
}

function function_67903a83(e_player) {
  e_target = self.target_enemy;
  e_target endon("death");
  if(e_target.archetype === "mechz") {
    self thread function_3f57ba41(e_player);
    return;
  }
  n_damage = e_target.health;
  self waittill("hash_368634cd");
  e_target setplayercollision(1);
  e_target.var_bc9b5fbd = 0;
  e_target.var_98056717 = 0;
  if(zm_utility::is_player_valid(e_player)) {
    var_38fe557 = e_player;
  } else {
    var_38fe557 = undefined;
  }
  e_target dodamage(n_damage, e_target.origin, var_38fe557, var_38fe557, undefined, "MOD_UNKNOWN", 0, level.var_edf1e590);
  gibserverutils::gibhead(e_target);
}

function function_3f57ba41(e_player) {
  e_target = self.target_enemy;
  e_target endon("death");
  var_3bb42832 = level.mechz_health;
  if(isDefined(level.var_f4dc2834)) {
    var_3bb42832 = math::clamp(var_3bb42832, 0, level.var_f4dc2834);
  }
  n_damage = (var_3bb42832 * 0.2) / 0.2;
  if(zm_utility::is_player_valid(e_player)) {
    var_38fe557 = e_player;
  } else {
    var_38fe557 = undefined;
  }
  e_target dodamage(n_damage, e_target.origin, var_38fe557, var_38fe557, undefined, "MOD_PROJECTILE_SPLASH", 0, level.var_edf1e590);
  self waittill("hash_368634cd");
  e_target.var_bc9b5fbd = 0;
  e_target.var_98056717 = 0;
}

function function_99d2c8aa(e_player) {
  if(self flag::get("demongate_chomper_despawning")) {
    return;
  }
  self.target_enemy = undefined;
  var_51b6a540 = self.origin;
  var_6f3820ea = 1024;
  if(isDefined(self.var_603f1f19) && self.var_603f1f19) {
    if(zm_utility::is_player_valid(e_player)) {
      var_51b6a540 = e_player.origin;
    }
    var_6f3820ea = 1024;
  }
  a_ai_enemies = getaiteamarray(level.zombie_team);
  var_237d778f = arraysortclosest(a_ai_enemies, var_51b6a540, a_ai_enemies.size, 0, var_6f3820ea);
  var_237d778f = array::filter(var_237d778f, 0, &zm_weap_elemental_bow::function_5aec3adc);
  var_237d778f = array::filter(var_237d778f, 0, &function_c994aef0, self);
  if(var_237d778f.size) {
    var_9183256c = var_237d778f[0];
    var_9183256c.var_bc9b5fbd = 1;
    self.target_enemy = var_9183256c;
    self notify("demongate_chomper_found_target");
  }
}

function function_c994aef0(var_9183256c, var_7119a95d) {
  return !(isDefined(var_9183256c.var_bc9b5fbd) && var_9183256c.var_bc9b5fbd) && (isDefined(var_9183256c.completed_emerging_into_playable_area) && var_9183256c.completed_emerging_into_playable_area || !isDefined(var_9183256c.completed_emerging_into_playable_area)) && (var_9183256c.archetype === "zombie" && (isDefined(var_9183256c.completed_emerging_into_playable_area) && var_9183256c.completed_emerging_into_playable_area) || var_9183256c.archetype !== "zombie") && bullettracepassed(var_9183256c getEye(), var_7119a95d.origin, 0, var_7119a95d);
}