/*************************************************
 * Decompiled by Serious and Edited by SyndiShanX
 * Script: shared\ai_puppeteer_shared.gsc
*************************************************/

#using scripts\shared\ai\archetype_utility;
#using scripts\shared\ai\systems\init;
#using scripts\shared\array_shared;
#using scripts\shared\colors_shared;
#using scripts\shared\spawner_shared;
#using scripts\shared\system_shared;
#using scripts\shared\util_shared;
#namespace ai_puppeteer;

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

function __init__() {
  level thread ai_puppeteer_think();
}

function ai_puppeteer_think() {
  while(true) {
    if(getdvarstring("") == "" && (!isDefined(level.ai_puppeteer_active) || level.ai_puppeteer_active == 0)) {
      level.ai_puppeteer_active = 1;
      level notify("hash_23dbb5b");
      adddebugcommand("");
      thread ai_puppeteer();
    } else if(getdvarstring("") == "" && isDefined(level.ai_puppeteer_active) && level.ai_puppeteer_active == 1) {
      level.ai_puppeteer_active = 0;
      adddebugcommand("");
      level notify("hash_23dbb5b");
    }
    wait(0.05);
  }
}

function ai_puppeteer() {
  player = undefined;
  while(!isPlayer(player)) {
    player = getplayers()[0];
    wait(0.05);
  }
  ai_puppeteer_create_hud();
  level.ai_puppet_highlighting = 0;
  player thread ai_puppet_cursor_tracker();
  player thread ai_puppet_manager();
  player.ignoreme = 1;
  level waittill("hash_23dbb5b");
  player.ignoreme = 0;
  ai_puppet_release(1);
  if(isDefined(level.ai_puppet_target)) {
    level.ai_puppet_target delete();
  }
  ai_puppeteer_destroy_hud();
}

function ai_puppet_manager() {
  level endon("hash_23dbb5b");
  self endon("death");
  while(true) {
    if(isDefined(level.playercursor[""]) && isDefined(level.ai_puppet) && isDefined(level.ai_puppet.debuglookatenabled) && level.ai_puppet.debuglookatenabled == 1) {
      level.ai_puppet lookatpos(level.playercursor[""]);
    }
    if(self buttonPressed("") && self buttonPressed("")) {
      if(isDefined(level.ai_puppet)) {
        level.ai_puppet forceteleport(level.playercursor[""], level.ai_puppet.angles);
      }
      wait(0.2);
    } else {
      if(self buttonPressed("")) {
        if(isDefined(level.ai_puppet)) {
          if(isDefined(level.ai_puppet_target)) {
            if(isai(level.ai_puppet_target)) {
              self thread ai_puppeteer_highlight_ai(level.ai_puppet_target, (1, 0, 0));
              level.ai_puppet clearentitytarget();
              level.ai_puppet_target = undefined;
            } else {
              self thread ai_puppeteer_highlight_point(level.ai_puppet_target.origin, level.ai_puppet_target_normal, anglesToForward(self getplayerangles()), (1, 0, 0));
              level.ai_puppet clearentitytarget();
              level.ai_puppet_target delete();
            }
          } else {
            if(isDefined(level.playercursorai)) {
              if(level.playercursorai != level.ai_puppet) {
                level.ai_puppet setentitytarget(level.playercursorai);
                level.ai_puppet_target = level.playercursorai;
                level.ai_puppet getperfectinfo(level.ai_puppet_target);
                self thread ai_puppeteer_highlight_ai(level.playercursorai, (1, 0, 0));
              }
            } else {
              level.ai_puppet_target = spawn("", level.playercursor[""]);
              level.ai_puppet_target_normal = level.playercursor[""];
              level.ai_puppet setentitytarget(level.ai_puppet_target);
              self thread ai_puppeteer_highlight_point(level.ai_puppet_target.origin, level.ai_puppet_target_normal, anglesToForward(self getplayerangles()), (1, 0, 0));
            }
          }
        }
        wait(0.2);
      } else {
        if(self buttonPressed("")) {
          if(isDefined(level.ai_puppet)) {
            if(isDefined(level.playercursorai) && level.playercursorai != level.ai_puppet) {
              level.ai_puppet setgoal(level.playercursorai);
              level.ai_puppet.goalradius = 64;
              self thread ai_puppeteer_highlight_ai(level.playercursorai, (0, 1, 0));
            } else {
              if(isDefined(level.playercursornode)) {
                level.ai_puppet setgoal(level.playercursornode);
                self thread ai_puppeteer_highlight_node(level.playercursornode);
              } else {
                if(isDefined(level.ai_puppet.scriptenemy)) {
                  to_target = level.ai_puppet.scriptenemy.origin - level.ai_puppet.origin;
                } else {
                  to_target = level.playercursor[""] - level.ai_puppet.origin;
                }
                angles = vectortoangles(to_target);
                level.ai_puppet setgoal(level.playercursor[""]);
                self thread ai_puppeteer_highlight_point(level.playercursor[""], level.playercursor[""], anglesToForward(self getplayerangles()), (0, 1, 0));
              }
            }
          }
          wait(0.2);
        } else {
          if(self buttonPressed("") && self buttonPressed("")) {
            if(isDefined(level.ai_puppet)) {
              if(isDefined(level.playercursorai) && level.playercursorai != level.ai_puppet) {
                level.ai_puppet setgoal(level.playercursorai);
                level.ai_puppet.goalradius = 64;
                self thread ai_puppeteer_highlight_ai(level.playercursorai, (0, 1, 0));
              } else {
                if(isDefined(level.playercursornode)) {
                  level.ai_puppet setgoal(level.playercursornode, 1);
                  self thread ai_puppeteer_highlight_node(level.playercursornode);
                } else {
                  if(isDefined(level.ai_puppet.scriptenemy)) {
                    to_target = level.ai_puppet.scriptenemy.origin - level.ai_puppet.origin;
                  } else {
                    to_target = level.playercursor[""] - level.ai_puppet.origin;
                  }
                  angles = vectortoangles(to_target);
                  level.ai_puppet setgoal(level.playercursor[""], 1);
                  self thread ai_puppeteer_highlight_point(level.playercursor[""], level.playercursor[""], anglesToForward(self getplayerangles()), (0, 1, 0));
                }
              }
            }
            wait(0.2);
          } else {
            if(self buttonPressed("")) {
              if(isDefined(level.playercursorai)) {
                if(isDefined(level.ai_puppet) && level.playercursorai == level.ai_puppet) {
                  ai_puppet_release(1);
                } else {
                  if(isDefined(level.ai_puppet)) {
                    ai_puppet_release(0);
                  }
                  ai_puppet_set();
                  self thread ai_puppeteer_highlight_ai(level.ai_puppet, (0, 1, 1));
                }
              }
              wait(0.2);
            } else if(self buttonPressed("")) {
              if(isDefined(level.ai_puppet)) {
                level.ai_puppet clearforcedgoal();
              }
              wait(0.2);
            }
          }
        }
      }
    }
    if(isDefined(level.ai_puppet)) {
      ai_puppeteer_render_ai(level.ai_puppet, (0, 1, 1));
      if(isDefined(level.ai_puppet.scriptenemy) && !level.ai_puppet_highlighting) {
        if(isai(level.ai_puppet.scriptenemy)) {
          ai_puppeteer_render_ai(level.ai_puppet.scriptenemy, (1, 0, 0));
        } else if(isDefined(level.ai_puppet_target)) {
          self thread ai_puppeteer_render_point(level.ai_puppet_target.origin, level.ai_puppet_target_normal, anglesToForward(self getplayerangles()), (1, 0, 0));
        }
      }
    }
    if(isDefined(level.ai_puppet)) {
      if(self buttonPressed("")) {
        level.ai_puppet.goalradius = level.ai_puppet.goalradius + 64;
      } else {
        if(self buttonPressed("")) {
          radius = level.ai_puppet.goalradius - 64;
          if(radius < 16) {
            radius = 16;
          }
          level.ai_puppet.goalradius = radius;
        } else if(self buttonPressed("")) {
          level.ai_puppet.goalradius = 16;
        }
      }
    }
    if(isDefined(level.ai_puppet)) {
      if(getdvarstring("") == "") {
        level.ai_puppet.fixednode = 1;
        ai_puppeteer_render_ai(level.ai_puppet, (1, 1, 1));
      } else {
        level.ai_puppet.fixednode = 0;
      }
    }
    wait(0.05);
  }
}

function ai_puppet_set() {
  level.ai_puppet = level.playercursorai;
  level.ai_puppet.ispuppet = 1;
  level.ai_puppet.old_goalradius = level.ai_puppet.goalradius;
  level.ai_puppet.goalradius = 16;
  level.ai_puppet stopanimscripted();
}

function ai_puppet_release(restore) {
  if(isDefined(level.ai_puppet)) {
    if(restore) {
      level.ai_puppet.goalradius = level.ai_puppet.old_goalradius;
      level.ai_puppet.ispuppet = 0;
      level.ai_puppet clearentitytarget();
    }
    level.ai_puppet = undefined;
  }
}

function ai_puppet_cursor_tracker() {
  level endon("hash_23dbb5b");
  self endon("death");
  while(true) {
    forward = anglesToForward(self getplayerangles());
    forward_vector = vectorscale(forward, 4000);
    level.playercursor = bulletTrace(self getEye(), self getEye() + forward_vector, 1, self);
    level.playercursorai = undefined;
    level.playercursornode = undefined;
    cursorcolor = (0, 1, 1);
    hitent = level.playercursor[""];
    if(isDefined(hitent) && isai(hitent)) {
      cursorcolor = (1, 0, 0);
      if(isDefined(level.ai_puppet) && level.ai_puppet != hitent) {
        if(!level.ai_puppet_highlighting) {
          ai_puppeteer_render_ai(hitent, cursorcolor);
        }
      }
      level.playercursorai = hitent;
    } else if(isDefined(level.ai_puppet)) {
      nodes = getanynodearray(level.playercursor[""], 24);
      if(nodes.size > 0) {
        node = nodes[0];
        if(node.type != "" && distancesquared(node.origin, level.playercursor[""]) < 576) {
          if(!level.ai_puppet_highlighting) {
            ai_puppeteer_render_node(node, (0, 1, 1));
          }
          level.playercursornode = node;
        }
      }
    }
    if(!level.ai_puppet_highlighting) {
      ai_puppeteer_render_point(level.playercursor[""], level.playercursor[""], forward, cursorcolor);
    }
    wait(0.05);
  }
}

function ai_puppeteer_create_hud() {
  /
  #
  level.puppeteer_hud_select = newdebughudelem();
  level.puppeteer_hud_select.x = 0;
  level.puppeteer_hud_select.y = 180;
  level.puppeteer_hud_select.fontscale = 1;
  level.puppeteer_hud_select.alignx = "";
  level.puppeteer_hud_select.horzalign = "";
  level.puppeteer_hud_select.color = (0, 0, 1);
  level.puppeteer_hud_goto = newdebughudelem();
  level.puppeteer_hud_goto.x = 0;
  level.puppeteer_hud_goto.y = 200;
  level.puppeteer_hud_goto.fontscale = 1;
  level.puppeteer_hud_goto.alignx = "";
  level.puppeteer_hud_goto.horzalign = "";
  level.puppeteer_hud_goto.color = (0, 1, 0);
  level.puppeteer_hud_lookat = newdebughudelem();
  level.puppeteer_hud_lookat.x = 0;
  level.puppeteer_hud_lookat.y = 220;
  level.puppeteer_hud_lookat.fontscale = 1;
  level.puppeteer_hud_lookat.alignx = "";
  level.puppeteer_hud_lookat.horzalign = "";
  level.puppeteer_hud_lookat.color = (0, 1, 1);
  level.puppeteer_hud_shoot = newdebughudelem();
  level.puppeteer_hud_shoot.x = 0;
  level.puppeteer_hud_shoot.y = 240;
  level.puppeteer_hud_shoot.fontscale = 1;
  level.puppeteer_hud_shoot.alignx = "";
  level.puppeteer_hud_shoot.horzalign = "";
  level.puppeteer_hud_shoot.color = (1, 1, 1);
  level.puppeteer_hud_teleport = newdebughudelem();
  level.puppeteer_hud_teleport.x = 0;
  level.puppeteer_hud_teleport.y = 260;
  level.puppeteer_hud_teleport.fontscale = 1;
  level.puppeteer_hud_teleport.alignx = "";
  level.puppeteer_hud_teleport.horzalign = "";
  level.puppeteer_hud_teleport.color = (1, 0, 0);
  level.puppeteer_hud_select settext("");
  level.puppeteer_hud_goto settext("");
  level.puppeteer_hud_lookat settext("");
  level.puppeteer_hud_shoot settext("");
  level.puppeteer_hud_teleport settext("");
}

function ai_puppeteer_destroy_hud() {
  if(isDefined(level.puppeteer_hud_select)) {
    level.puppeteer_hud_select destroy();
  }
  if(isDefined(level.puppeteer_hud_lookat)) {
    level.puppeteer_hud_lookat destroy();
  }
  if(isDefined(level.puppeteer_hud_goto)) {
    level.puppeteer_hud_goto destroy();
  }
  if(isDefined(level.puppeteer_hud_shoot)) {
    level.puppeteer_hud_shoot destroy();
  }
}

function ai_puppeteer_render_point(point, normal, forward, color) {
  surface_vector = vectorcross(forward, normal);
  surface_vector = vectornormalize(surface_vector);
  line(point, point + vectorscale(surface_vector, 5), color, 1, 1);
  line(point, point + (vectorscale(surface_vector, -5)), color, 1, 1);
  surface_vector = vectorcross(normal, surface_vector);
  surface_vector = vectornormalize(surface_vector);
  line(point, point + vectorscale(surface_vector, 5), color, 1, 1);
  line(point, point + (vectorscale(surface_vector, -5)), color, 1, 1);
}

function ai_puppeteer_render_node(node, color) {
  print3d(node.origin, node.type, color, 1, 0.35);
  box(node.origin, vectorscale((-1, -1, 0), 16), vectorscale((1, 1, 1), 16), node.angles[1], color, 1, 1);
  nodeforward = anglesToForward(node.angles);
  nodeforward = vectorscale(nodeforward, 8);
  line(node.origin, node.origin + nodeforward, color, 1, 1);
}

function ai_puppeteer_render_ai(ai, color) {
  circle(ai.goalpos + (0, 0, 1), ai.goalradius, color, 0, 1);
  circle(ai.origin + (0, 0, 1), ai getpathfindingradius(), (1, 0, 0), 0, 1);
  line(ai.goalpos, ai.origin, color, 1, 1);
}

function ai_puppeteer_highlight_point(point, normal, forward, color) {
  level endon("hash_23dbb5b");
  self endon("death");
  level.ai_puppet_highlighting = 1;
  timer = 0;
  while(timer < 0.7) {
    ai_puppeteer_render_point(point, normal, forward, color);
    timer = timer + 0.15;
    wait(0.15);
  }
  level.ai_puppet_highlighting = 0;
}

function ai_puppeteer_highlight_node(node) {
  level endon("hash_23dbb5b");
  self endon("death");
  level.ai_puppet_highlighting = 1;
  timer = 0;
  while(timer < 0.7) {
    ai_puppeteer_render_node(node, (0, 1, 0));
    timer = timer + 0.15;
    wait(0.15);
  }
  level.ai_puppet_highlighting = 0;
}

function ai_puppeteer_highlight_ai(ai, color) {
  level endon("hash_23dbb5b");
  self endon("death");
  level.ai_puppet_highlighting = 1;
  timer = 0;
  while(timer < 0.7 && isDefined(ai)) {
    ai_puppeteer_render_ai(ai, color);
    timer = timer + 0.15;
    wait(0.15);
  }
  level.ai_puppet_highlighting = 0;
}