From 93d80c0005a1c3d74a53fc1f4e3aa1383cbbb0e0 Mon Sep 17 00:00:00 2001
From: FatBoy-DOTC <justin.chucksinsulating@gmail.com>
Date: Sun, 23 Feb 2025 20:44:56 -0600
Subject: [PATCH] split system mob AI

---
 src/engine/mobileAI/MobBehaviours/Pet.java    |   33 +
 .../mobileAI/MobBehaviours/PlayerGuard.java   |  146 ++
 .../mobileAI/MobBehaviours/SiegeEngine.java   |   27 +
 .../mobileAI/MobBehaviours/Standard.java      |   53 +
 .../MobBehaviours/StaticBehaviours.java       | 1285 +++++++++++++++++
 src/engine/mobileAI/Threads/MobAIThread.java  |    4 +-
 6 files changed, 1547 insertions(+), 1 deletion(-)
 create mode 100644 src/engine/mobileAI/MobBehaviours/Pet.java
 create mode 100644 src/engine/mobileAI/MobBehaviours/PlayerGuard.java
 create mode 100644 src/engine/mobileAI/MobBehaviours/SiegeEngine.java
 create mode 100644 src/engine/mobileAI/MobBehaviours/Standard.java
 create mode 100644 src/engine/mobileAI/MobBehaviours/StaticBehaviours.java

diff --git a/src/engine/mobileAI/MobBehaviours/Pet.java b/src/engine/mobileAI/MobBehaviours/Pet.java
new file mode 100644
index 00000000..4fce81f2
--- /dev/null
+++ b/src/engine/mobileAI/MobBehaviours/Pet.java
@@ -0,0 +1,33 @@
+package engine.mobileAI.MobBehaviours;
+
+import engine.gameManager.ZoneManager;
+import engine.mobileAI.utilities.MovementUtilities;
+import engine.objects.Mob;
+import org.pmw.tinylog.Logger;
+
+public class Pet {
+
+    public static void run(Mob pet){
+
+        try {
+
+            if(StaticBehaviours.EarlyExit(pet))
+                return;
+
+            if (pet.getOwner() == null && pet.isNecroPet() == false && pet.isSiege() == false)
+                if (ZoneManager.getSeaFloor().zoneMobSet.contains(pet))
+                    pet.killCharacter("no owner");
+
+            if(!pet.isSiege())
+                pet.BehaviourType.canRoam = true;
+
+
+            if (MovementUtilities.canMove(pet) && pet.BehaviourType.canRoam)
+                StaticBehaviours.CheckMobMovement(pet);
+
+            StaticBehaviours.CheckForAttack(pet);
+        } catch (Exception e) {
+            Logger.info(pet.getObjectUUID() + " " + pet.getName() + " Failed At: PetLogic" + " " + e.getMessage());
+        }
+    }
+}
diff --git a/src/engine/mobileAI/MobBehaviours/PlayerGuard.java b/src/engine/mobileAI/MobBehaviours/PlayerGuard.java
new file mode 100644
index 00000000..e0bf4ed7
--- /dev/null
+++ b/src/engine/mobileAI/MobBehaviours/PlayerGuard.java
@@ -0,0 +1,146 @@
+package engine.mobileAI.MobBehaviours;
+
+import engine.Enum;
+import engine.objects.AbstractWorldObject;
+import engine.objects.Mob;
+import engine.objects.PlayerCharacter;
+import org.pmw.tinylog.Logger;
+
+public class PlayerGuard {
+
+    public static void run(Mob guard) {
+
+        if(StaticBehaviours.EarlyExit(guard))
+            return;
+
+        if (guard.mobPowers.isEmpty()) {
+            //mele
+            if (guard.BehaviourType.equals(Enum.MobBehaviourType.GuardWallArcher)) {
+                GuardWallArcherLogic(guard);
+            } else {
+                if (guard.BehaviourType.equals(Enum.MobBehaviourType.GuardCaptain)) {
+                    GuardCaptainLogic(guard);
+                } else if (guard.BehaviourType.equals(Enum.MobBehaviourType.GuardMinion)) {
+                    GuardMinionLogic(guard);
+                }
+            }
+        } else {
+            //caster
+            if (guard.BehaviourType.equals(Enum.MobBehaviourType.GuardCaptain)) {
+                MagisterCaptainLogic(guard);
+            } else if (guard.BehaviourType.equals(Enum.MobBehaviourType.GuardMinion)) {
+                MagisterMinionLogic(guard);
+            }
+        }
+    }
+
+    public static void GuardCaptainLogic(Mob mob) {
+
+        try {
+            StaticBehaviours.checkToDropGuardAggro(mob);
+            if (mob.getCombatTarget() == null)
+                StaticBehaviours.CheckForPlayerGuardAggro(mob);
+
+            AbstractWorldObject newTarget = StaticBehaviours.ChangeTargetFromHateValue(mob);
+
+            if (newTarget != null) {
+
+                if (newTarget.getObjectType().equals(Enum.GameObjectType.PlayerCharacter)) {
+                    if (StaticBehaviours.GuardCanAggro(mob, (PlayerCharacter) newTarget))
+                        mob.setCombatTarget(newTarget);
+                } else
+                    mob.setCombatTarget(newTarget);
+
+            }
+            StaticBehaviours.CheckMobMovement(mob);
+            StaticBehaviours.CheckForAttack(mob);
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: GuardCaptainLogic" + " " + e.getMessage());
+        }
+    }
+
+    public static void GuardMinionLogic(Mob mob) {
+
+        try {
+            StaticBehaviours.checkToDropGuardAggro(mob);
+
+            boolean isComanded = mob.npcOwner.isAlive();
+            if (!isComanded) {
+                GuardCaptainLogic(mob);
+            }else {
+                if (mob.npcOwner.getCombatTarget() != null)
+                    mob.setCombatTarget(mob.npcOwner.getCombatTarget());
+                else
+                if (mob.getCombatTarget() != null)
+                    mob.setCombatTarget(null);
+            }
+            StaticBehaviours.CheckMobMovement(mob);
+            StaticBehaviours.CheckForAttack(mob);
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: GuardMinionLogic" + " " + e.getMessage());
+        }
+    }
+
+    public static void GuardWallArcherLogic(Mob mob) {
+
+        try {
+            StaticBehaviours.checkToDropGuardAggro(mob);
+
+            if (mob.getCombatTarget() == null)
+                StaticBehaviours.CheckForPlayerGuardAggro(mob);
+            else
+                StaticBehaviours.CheckForAttack(mob);
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: GuardWallArcherLogic" + " " + e.getMessage());
+        }
+    }
+
+    public static void MagisterCaptainLogic(Mob mob){
+        try {
+            StaticBehaviours.checkToDropGuardAggro(mob);
+            if (mob.getCombatTarget() == null)
+                StaticBehaviours.CheckForPlayerGuardAggro(mob);
+
+            AbstractWorldObject newTarget = StaticBehaviours.ChangeTargetFromHateValue(mob);
+
+            if (newTarget != null) {
+
+                if (newTarget.getObjectType().equals(Enum.GameObjectType.PlayerCharacter)) {
+                    if (StaticBehaviours.GuardCanAggro(mob, (PlayerCharacter) newTarget))
+                        mob.setCombatTarget(newTarget);
+                } else
+                    mob.setCombatTarget(newTarget);
+
+            }
+            StaticBehaviours.CheckMobMovement(mob);
+            CheckForCast(mob);
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: GuardCaptainLogic" + " " + e.getMessage());
+        }
+    }
+
+    public static void MagisterMinionLogic(Mob mob){
+        try {
+            StaticBehaviours.checkToDropGuardAggro(mob);
+
+            boolean isComanded = mob.npcOwner.isAlive();
+            if (!isComanded) {
+                MagisterCaptainLogic(mob);
+            }else {
+                if (mob.npcOwner.getCombatTarget() != null)
+                    mob.setCombatTarget(mob.npcOwner.getCombatTarget());
+                else
+                if (mob.getCombatTarget() != null)
+                    mob.setCombatTarget(null);
+            }
+            StaticBehaviours.CheckMobMovement(mob);
+            CheckForCast(mob);
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: GuardMinionLogic" + " " + e.getMessage());
+        }
+    }
+
+    public static void CheckForCast(Mob mob){
+
+    }
+}
diff --git a/src/engine/mobileAI/MobBehaviours/SiegeEngine.java b/src/engine/mobileAI/MobBehaviours/SiegeEngine.java
new file mode 100644
index 00000000..3841fde8
--- /dev/null
+++ b/src/engine/mobileAI/MobBehaviours/SiegeEngine.java
@@ -0,0 +1,27 @@
+package engine.mobileAI.MobBehaviours;
+
+import engine.Enum;
+import engine.objects.Mob;
+
+public class SiegeEngine {
+
+    public static void run(Mob engine){
+
+        if(StaticBehaviours.EarlyExit(engine))
+            return;
+
+        if(engine.getOwner() == null)
+            return;
+
+        if(engine.combatTarget == null)
+            return;
+
+        if(engine.combatTarget.loc.distanceSquared(engine.loc) > engine.getRange() * engine.getRange())
+            return;
+
+        if(!engine.combatTarget.getObjectType().equals(Enum.GameObjectType.Building))
+            return;
+
+        StaticBehaviours.CheckForAttack(engine);
+    }
+}
diff --git a/src/engine/mobileAI/MobBehaviours/Standard.java b/src/engine/mobileAI/MobBehaviours/Standard.java
new file mode 100644
index 00000000..eed778d0
--- /dev/null
+++ b/src/engine/mobileAI/MobBehaviours/Standard.java
@@ -0,0 +1,53 @@
+package engine.mobileAI.MobBehaviours;
+
+import engine.Enum;
+import engine.objects.AbstractWorldObject;
+import engine.objects.Mob;
+import org.pmw.tinylog.Logger;
+
+public class Standard {
+
+    public static void run(Mob mob){
+
+        try {
+
+            if(StaticBehaviours.EarlyExit(mob))
+                return;
+
+            //check for players that can be aggroed if mob is agressive and has no target
+
+            if (mob.getCombatTarget() != null && !mob.playerAgroMap.containsKey(mob.getCombatTarget().getObjectUUID()))
+                mob.setCombatTarget(null);
+
+            if (mob.BehaviourType.isAgressive) {
+
+                AbstractWorldObject newTarget = StaticBehaviours.ChangeTargetFromHateValue(mob);
+
+                if (newTarget != null)
+                    mob.setCombatTarget(newTarget);
+                else {
+                    if (mob.getCombatTarget() == null) {
+                        if (mob.BehaviourType == Enum.MobBehaviourType.HamletGuard)
+                            StaticBehaviours.SafeGuardAggro(mob);  //safehold guard
+                        else
+                            StaticBehaviours.CheckForAggro(mob);   //normal aggro
+                    }
+                }
+            }
+
+            //check if mob can move for patrol or moving to target
+
+            if (mob.BehaviourType.canRoam)
+                StaticBehaviours.CheckMobMovement(mob);
+
+            //check if mob can attack if it isn't wimpy
+
+            if (!mob.BehaviourType.isWimpy && mob.getCombatTarget() != null)
+                StaticBehaviours.CheckForAttack(mob);
+
+
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: DefaultLogic" + " " + e.getMessage());
+        }
+    }
+}
diff --git a/src/engine/mobileAI/MobBehaviours/StaticBehaviours.java b/src/engine/mobileAI/MobBehaviours/StaticBehaviours.java
new file mode 100644
index 00000000..a1d88841
--- /dev/null
+++ b/src/engine/mobileAI/MobBehaviours/StaticBehaviours.java
@@ -0,0 +1,1285 @@
+package engine.mobileAI.MobBehaviours;
+
+import engine.Enum;
+import engine.InterestManagement.InterestManager;
+import engine.InterestManagement.WorldGrid;
+import engine.gameManager.*;
+import engine.math.Vector3f;
+import engine.math.Vector3fImmutable;
+import engine.mobileAI.Threads.MobAIThread;
+import engine.mobileAI.utilities.CombatUtilities;
+import engine.mobileAI.utilities.MovementUtilities;
+import engine.net.DispatchMessage;
+import engine.net.client.msg.PerformActionMsg;
+import engine.net.client.msg.UpdateStateMsg;
+import engine.objects.*;
+import engine.powers.ActionsBase;
+import engine.powers.PowersBase;
+import engine.server.MBServerStatics;
+import org.pmw.tinylog.Logger;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ThreadLocalRandom;
+
+import static engine.math.FastMath.sqr;
+
+public class StaticBehaviours {
+
+
+    public static void runBehaviour(Mob mob){
+        if(mob.isPlayerGuard())
+            PlayerGuard.run(mob);
+        else if(mob.isPet())
+            Pet.run(mob);
+        else if(mob.isSiege())
+            SiegeEngine.run(mob);
+        else
+            Standard.run(mob);
+    }
+    private static void AttackTarget(Mob mob, AbstractWorldObject target) {
+
+        try {
+
+            if (mob == null)
+                return;
+
+            if (target == null || !target.isAlive()) {
+                mob.setCombatTarget(null);
+                return;
+            }
+
+            if (!CombatUtilities.inRangeToAttack(mob, target))
+                return;
+
+            switch (target.getObjectType()) {
+                case PlayerCharacter:
+                    PlayerCharacter targetPlayer = (PlayerCharacter) target;
+                    AttackPlayer(mob, targetPlayer);
+                    break;
+                case Building:
+                    Building targetBuilding = (Building) target;
+                    AttackBuilding(mob, targetBuilding);
+                    break;
+                case Mob:
+                    Mob targetMob = (Mob) target;
+                    AttackMob(mob, targetMob);
+                    break;
+            }
+
+            mob.updateLocation();
+
+            if(mob.StrongholdGuardian || mob.StrongholdEpic){
+                // attempt to ground all players in attack range
+                for(int i : mob.playerAgroMap.keySet()){
+                    PlayerCharacter tar = PlayerCharacter.getFromCache(i);
+                    if(tar != null && tar.loc.distanceSquared(mob.loc) < 80){
+                        PowersManager.applyPower(mob,tar,tar.loc, 111111,40,false);
+                    }
+                }
+            }
+
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: AttackTarget" + " " + e.getMessage());
+        }
+    }
+
+    public static void AttackPlayer(Mob mob, PlayerCharacter target) {
+
+        try {
+
+            if (!mob.canSee(target)) {
+                mob.setCombatTarget(null);
+                return;
+            }
+
+            if(target.getPet() != null && target.getPet().isAlive() && !target.getPet().isSiege()){
+                mob.setCombatTarget(target.getPet());
+                AttackTarget(mob,mob.combatTarget);
+                return;
+            }
+
+            if (mob.BehaviourType.callsForHelp)
+                MobCallForHelp(mob);
+
+            if (MovementUtilities.outOfAggroRange(mob, target)) {
+                mob.setCombatTarget(null);
+                return;
+            }
+
+            if (CombatUtilities.inRange2D(mob, target, mob.getRange())) {
+
+                //no weapons, default mob attack speed 3 seconds.
+
+                if (System.currentTimeMillis() < mob.getLastAttackTime())
+                    return;
+
+                // ranged mobs cant attack while running. skip until they finally stop.
+
+                if (mob.isMoving() && mob.getRange() > 20)
+                    return;
+
+                // add timer for last attack.
+
+                ItemBase mainHand = mob.getWeaponItemBase(true);
+                ItemBase offHand = mob.getWeaponItemBase(false);
+
+                if (mainHand == null && offHand == null) {
+                    CombatUtilities.combatCycle(mob, target, true, null);
+                    int delay = 3000;
+                    if (mob.isSiege())
+                        delay = 11000;
+                    mob.setLastAttackTime(System.currentTimeMillis() + delay);
+                } else if (mob.getWeaponItemBase(true) != null) {
+                    int delay = 3000;
+                    if (mob.isSiege())
+                        delay = 11000;
+                    CombatUtilities.combatCycle(mob, target, true, mob.getWeaponItemBase(true));
+                    mob.setLastAttackTime(System.currentTimeMillis() + delay);
+                } else if (mob.getWeaponItemBase(false) != null) {
+                    int attackDelay = 3000;
+                    if (mob.isSiege())
+                        attackDelay = 11000;
+                    CombatUtilities.combatCycle(mob, target, false, mob.getWeaponItemBase(false));
+                    mob.setLastAttackTime(System.currentTimeMillis() + attackDelay);
+                }
+            }
+
+            if (target.getPet() != null)
+                if (target.getPet().getCombatTarget() == null && target.getPet().assist == true)
+                    target.getPet().setCombatTarget(mob);
+
+            try{
+                InterestManager.forceLoad(mob);
+            }catch(Exception e){
+
+            }
+
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: AttackPlayer" + " " + e.getMessage());
+        }
+
+    }
+
+    public static void AttackBuilding(Mob mob, Building target) {
+
+        try {
+
+            if(mob == null || target == null)
+                return;
+
+            if (target.getRank() == -1 || !target.isVulnerable() || BuildingManager.getBuildingFromCache(target.getObjectUUID()) == null) {
+                mob.setCombatTarget(null);
+                return;
+            }
+
+            City playercity = ZoneManager.getCityAtLocation(mob.getLoc());
+
+            if (playercity != null)
+                for (Mob guard : playercity.getParent().zoneMobSet)
+                    if (guard.BehaviourType != null && guard.BehaviourType.equals(Enum.MobBehaviourType.GuardCaptain))
+                        if (guard.getCombatTarget() == null && guard.getGuild() != null && mob.getGuild() != null && !guard.getGuild().equals(mob.getGuild()))
+                            guard.setCombatTarget(mob);
+
+            if (mob.isSiege())
+                MovementManager.sendRWSSMsg(mob);
+
+            ItemBase mainHand = mob.getWeaponItemBase(true);
+            ItemBase offHand = mob.getWeaponItemBase(false);
+
+            if (mainHand == null && offHand == null) {
+                CombatUtilities.combatCycle(mob, target, true, null);
+                int delay = 3000;
+                if (mob.isSiege())
+                    delay = 15000;
+                mob.setLastAttackTime(System.currentTimeMillis() + delay);
+            } else if (mob.getWeaponItemBase(true) != null) {
+                int attackDelay = 3000;
+                if (mob.isSiege())
+                    attackDelay = 15000;
+                CombatUtilities.combatCycle(mob, target, true, mob.getWeaponItemBase(true));
+                mob.setLastAttackTime(System.currentTimeMillis() + attackDelay);
+            } else if (mob.getWeaponItemBase(false) != null) {
+                int attackDelay = 3000;
+                if (mob.isSiege())
+                    attackDelay = 15000;
+                CombatUtilities.combatCycle(mob, target, false, mob.getWeaponItemBase(false));
+                mob.setLastAttackTime(System.currentTimeMillis() + attackDelay);
+            }
+
+            //if (mob.isSiege()) {
+            //    PowerProjectileMsg ppm = new PowerProjectileMsg(mob, target);
+            //    ppm.setRange(50);
+            //    DispatchMessage.dispatchMsgToInterestArea(mob, ppm, DispatchChannel.SECONDARY, MBServerStatics.CHARACTER_LOAD_RANGE, false, false);
+            //}
+
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: AttackBuilding" + " " + e.getMessage());
+        }
+    }
+
+    public static void AttackMob(Mob mob, Mob target) {
+
+        try {
+
+            if (mob.getRange() >= 30 && mob.isMoving())
+                return;
+
+            //no weapons, default mob attack speed 3 seconds.
+
+            ItemBase mainHand = mob.getWeaponItemBase(true);
+            ItemBase offHand = mob.getWeaponItemBase(false);
+
+            if (mainHand == null && offHand == null) {
+                CombatUtilities.combatCycle(mob, target, true, null);
+                int delay = 3000;
+                if (mob.isSiege())
+                    delay = 11000;
+                mob.setLastAttackTime(System.currentTimeMillis() + delay);
+            } else if (mob.getWeaponItemBase(true) != null) {
+                int attackDelay = 3000;
+                if (mob.isSiege())
+                    attackDelay = 11000;
+                CombatUtilities.combatCycle(mob, target, true, mob.getWeaponItemBase(true));
+                mob.setLastAttackTime(System.currentTimeMillis() + attackDelay);
+            } else if (mob.getWeaponItemBase(false) != null) {
+                int attackDelay = 3000;
+                if (mob.isSiege())
+                    attackDelay = 11000;
+                CombatUtilities.combatCycle(mob, target, false, mob.getWeaponItemBase(false));
+                mob.setLastAttackTime(System.currentTimeMillis() + attackDelay);
+                if (target.getCombatTarget() == null) {
+                    target.setCombatTarget(mob);
+                }
+            }
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: AttackMob" + " " + e.getMessage());
+        }
+    }
+
+    private static void Patrol(Mob mob) {
+
+        try {
+
+            //make sure mob is out of combat stance
+
+            int patrolDelay = ThreadLocalRandom.current().nextInt((int) (MobAIThread.AI_PATROL_DIVISOR * 0.5f), MobAIThread.AI_PATROL_DIVISOR) + MobAIThread.AI_PATROL_DIVISOR;
+
+            //early exit while waiting to patrol again
+
+            if (mob.stopPatrolTime + (patrolDelay * 1000) > System.currentTimeMillis())
+                return;
+
+            //guard captains inherit barracks patrol points dynamically
+
+            if (mob.BehaviourType.ordinal() == Enum.MobBehaviourType.GuardCaptain.ordinal()) {
+
+                Building barracks = mob.building;
+
+                if (barracks != null && barracks.patrolPoints != null && !barracks.getPatrolPoints().isEmpty()) {
+                    mob.patrolPoints = barracks.patrolPoints;
+                } else {
+                    randomGuardPatrolPoint(mob);
+                    return;
+                }
+            }
+
+            if (mob.lastPatrolPointIndex > mob.patrolPoints.size() - 1)
+                mob.lastPatrolPointIndex = 0;
+
+            mob.destination = mob.patrolPoints.get(mob.lastPatrolPointIndex);
+            mob.lastPatrolPointIndex += 1;
+
+            MovementUtilities.aiMove(mob, mob.destination, true);
+
+            if (mob.BehaviourType.equals(Enum.MobBehaviourType.GuardCaptain))
+                for (Map.Entry<Mob, Integer> minion : mob.siegeMinionMap.entrySet())
+
+                    //make sure mob is out of combat stance
+
+                    if (minion.getKey().despawned == false) {
+                        if (MovementUtilities.canMove(minion.getKey())) {
+                            Vector3f minionOffset = Formation.getOffset(2, minion.getValue() + 3);
+                            minion.getKey().updateLocation();
+                            Vector3fImmutable formationPatrolPoint = new Vector3fImmutable(mob.destination.x + minionOffset.x, mob.destination.y, mob.destination.z + minionOffset.z);
+                            MovementUtilities.aiMove(minion.getKey(), formationPatrolPoint, true);
+                        }
+                    }
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: AttackTarget" + " " + e.getMessage());
+        }
+    }
+
+    public static void randomGuardPatrolPoint(Mob mob) {
+
+        try {
+
+            //early exit for a mob who is already moving to a patrol point
+            //while mob moving, update lastPatrolTime so that when they stop moving the 10 second timer can begin
+
+            if (mob.isMoving() == true) {
+                mob.stopPatrolTime = System.currentTimeMillis();
+                return;
+            }
+
+            //wait between 10 and 15 seconds after reaching patrol point before moving
+
+            int patrolDelay = ThreadLocalRandom.current().nextInt(10000) + 5000;
+
+            //early exit while waiting to patrol again
+
+            if (mob.stopPatrolTime + patrolDelay > System.currentTimeMillis())
+                return;
+
+            float xPoint = ThreadLocalRandom.current().nextInt(400) - 200;
+            float zPoint = ThreadLocalRandom.current().nextInt(400) - 200;
+            Vector3fImmutable TreePos = mob.getGuild().getOwnedCity().getLoc();
+            mob.destination = new Vector3fImmutable(TreePos.x + xPoint, TreePos.y, TreePos.z + zPoint);
+
+            MovementUtilities.aiMove(mob, mob.destination, true);
+
+            if (mob.BehaviourType.ordinal() == Enum.MobBehaviourType.GuardCaptain.ordinal()) {
+                for (Map.Entry<Mob, Integer> minion : mob.siegeMinionMap.entrySet()) {
+
+                    //make sure mob is out of combat stance
+
+                    if (minion.getKey().despawned == false) {
+                        if (MovementUtilities.canMove(minion.getKey())) {
+                            Vector3f minionOffset = Formation.getOffset(2, minion.getValue() + 3);
+                            minion.getKey().updateLocation();
+                            Vector3fImmutable formationPatrolPoint = new Vector3fImmutable(mob.destination.x + minionOffset.x, mob.destination.y, mob.destination.z + minionOffset.z);
+                            MovementUtilities.aiMove(minion.getKey(), formationPatrolPoint, true);
+                        }
+                    }
+                }
+            }
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: randomGuardPatrolPoints" + " " + e.getMessage());
+        }
+    }
+
+    public static void MobCallForHelp(Mob mob) {
+
+        try {
+
+            boolean callGotResponse = false;
+
+            if (mob.nextCallForHelp == 0)
+                mob.nextCallForHelp = System.currentTimeMillis();
+
+            if (mob.nextCallForHelp < System.currentTimeMillis())
+                return;
+
+            //mob sends call for help message
+
+            ChatManager.chatSayInfo(null, mob.getName() + " calls for help!");
+
+            Zone mobCamp = mob.getParentZone();
+
+            for (Mob helper : mobCamp.zoneMobSet) {
+                if (helper.BehaviourType.respondsToCallForHelp && helper.BehaviourType.BehaviourHelperType.equals(mob.BehaviourType)) {
+                    helper.setCombatTarget(mob.getCombatTarget());
+                    callGotResponse = true;
+                }
+            }
+
+            //wait 60 seconds to call for help again
+
+            if (callGotResponse)
+                mob.nextCallForHelp = System.currentTimeMillis() + 60000;
+
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: MobCallForHelp" + " " + e.getMessage());
+        }
+    }
+
+    static void CheckForAggro(Mob aiAgent) {
+
+        try {
+
+            //looks for and sets mobs combatTarget
+
+            if (!aiAgent.isAlive())
+                return;
+
+            ConcurrentHashMap<Integer, Boolean> loadedPlayers = aiAgent.playerAgroMap;
+
+            for (Map.Entry playerEntry : loadedPlayers.entrySet()) {
+
+                int playerID = (int) playerEntry.getKey();
+                PlayerCharacter loadedPlayer = PlayerCharacter.getFromCache(playerID);
+
+                //Player is null, let's remove them from the list.
+
+                if (loadedPlayer == null) {
+                    loadedPlayers.remove(playerID);
+                    continue;
+                }
+
+                //Player is Dead, Mob no longer needs to attempt to aggro. Remove them from aggro map.
+
+                if (!loadedPlayer.isAlive()) {
+                    loadedPlayers.remove(playerID);
+                    continue;
+                }
+
+                //Can't see target, skip aggro.
+
+                if (!aiAgent.canSee(loadedPlayer))
+                    continue;
+
+                // No aggro for this race type
+
+                if (aiAgent.notEnemy.size() > 0 && aiAgent.notEnemy.contains(loadedPlayer.getRace().getRaceType().getMonsterType()) == true)
+                    continue;
+
+                //mob has enemies and this player race is not it
+
+                if (aiAgent.enemy.size() > 0 && aiAgent.enemy.contains(loadedPlayer.getRace().getRaceType().getMonsterType()) == false)
+                    continue;
+
+                if (MovementUtilities.inRangeToAggro(aiAgent, loadedPlayer)) {
+                    aiAgent.setCombatTarget(loadedPlayer);
+                    return;
+                }
+
+            }
+
+            if (aiAgent.getCombatTarget() == null) {
+
+                //look for pets to aggro if no players found to aggro
+
+                HashSet<AbstractWorldObject> awoList = WorldGrid.getObjectsInRangePartial(aiAgent, MobAIThread.AI_BASE_AGGRO_RANGE, MBServerStatics.MASK_PET);
+
+                for (AbstractWorldObject awoMob : awoList) {
+
+                    // exclude self.
+
+                    if (aiAgent.equals(awoMob))
+                        continue;
+
+                    Mob aggroMob = (Mob) awoMob;
+                    aiAgent.setCombatTarget(aggroMob);
+                    return;
+                }
+            }
+        } catch (Exception e) {
+            Logger.info(aiAgent.getObjectUUID() + " " + aiAgent.getName() + " Failed At: CheckForAggro" + " " + e.getMessage());
+        }
+    }
+
+    static void CheckMobMovement(Mob mob) {
+
+        try {
+
+            if (!MovementUtilities.canMove(mob))
+                return;
+
+            mob.updateLocation();
+
+            switch (mob.BehaviourType) {
+
+                case Pet1:
+                    if (mob.getOwner() == null)
+                        return;
+
+                    if (!mob.playerAgroMap.containsKey(mob.getOwner().getObjectUUID())) {
+
+                        //mob no longer has its owner loaded, translocate pet to owner
+
+                        MovementManager.translocate(mob, mob.getOwner().getLoc(), null);
+                        return;
+                    }
+                    if (mob.getCombatTarget() == null) {
+
+                        //move back to owner
+
+                        if (CombatUtilities.inRange2D(mob, mob.getOwner(), 6))
+                            return;
+
+                        mob.destination = mob.getOwner().getLoc();
+                        MovementUtilities.moveToLocation(mob, mob.destination, 5);
+                    } else
+                        chaseTarget(mob);
+                    break;
+                case GuardMinion:
+                    if (!mob.npcOwner.isAlive() && mob.getCombatTarget() == null)
+                        randomGuardPatrolPoint(mob);
+                    else {
+                        if (mob.getCombatTarget() != null) {
+                            chaseTarget(mob);
+                        }
+                    }
+                    break;
+                default:
+                    if (mob.getCombatTarget() == null) {
+                        if (!mob.isMoving())
+                            Patrol(mob);
+                        else {
+                            mob.stopPatrolTime = System.currentTimeMillis();
+                        }
+                    } else {
+                        chaseTarget(mob);
+                    }
+                    break;
+
+            }
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: CheckMobMovement" + " " + e.getMessage());
+        }
+    }
+
+    private static void CheckForRespawn(Mob aiAgent) {
+
+        try {
+
+            if (aiAgent.deathTime == 0) {
+                aiAgent.setDeathTime(System.currentTimeMillis());
+                return;
+            }
+
+            //handles checking for respawn of dead mobs even when no players have mob loaded
+            //Despawn Timer with Loot currently in inventory.
+
+            if (!aiAgent.despawned) {
+
+                if (aiAgent.getCharItemManager().getInventoryCount() > 0) {
+                    if (System.currentTimeMillis() > aiAgent.deathTime + MBServerStatics.DESPAWN_TIMER_WITH_LOOT) {
+                        aiAgent.despawn();
+                        aiAgent.deathTime = System.currentTimeMillis();
+                        return;
+                    }
+                    //No items in inventory.
+                } else if (aiAgent.isHasLoot()) {
+                    if (System.currentTimeMillis() > aiAgent.deathTime + MBServerStatics.DESPAWN_TIMER_ONCE_LOOTED) {
+                        aiAgent.despawn();
+                        aiAgent.deathTime = System.currentTimeMillis();
+                        return;
+                    }
+                    //Mob never had Loot.
+                } else {
+                    if (System.currentTimeMillis() > aiAgent.deathTime + MBServerStatics.DESPAWN_TIMER) {
+                        aiAgent.despawn();
+                        aiAgent.deathTime = System.currentTimeMillis();
+                        return;
+                    }
+                }
+                return;
+            }
+
+            if(Mob.discDroppers.contains(aiAgent))
+                return;
+
+            if(aiAgent.StrongholdGuardian || aiAgent.StrongholdEpic || aiAgent.StrongholdCommander)
+                return;
+
+            if (aiAgent.despawned && System.currentTimeMillis() > (aiAgent.deathTime + (aiAgent.spawnTime * 1000L))) {
+                if (!Zone.respawnQue.contains(aiAgent)) {
+                    Zone.respawnQue.add(aiAgent);
+                }
+            }
+        } catch (Exception e) {
+            Logger.info(aiAgent.getObjectUUID() + " " + aiAgent.getName() + " Failed At: CheckForRespawn" + " " + e.getMessage());
+        }
+    }
+
+    public static void CheckForAttack(Mob mob) {
+        try {
+
+            //checks if mob can attack based on attack timer and range
+
+            if (mob.isAlive() == false)
+                return;
+
+            if (mob.getCombatTarget() == null)
+                return;
+
+            if(!mob.isCombat())
+                enterCombat(mob);
+
+            if (mob.getCombatTarget().getObjectType().equals(Enum.GameObjectType.PlayerCharacter) && MovementUtilities.outOfAggroRange(mob, (PlayerCharacter) mob.getCombatTarget()) && mob.BehaviourType.ordinal() != Enum.MobBehaviourType.Pet1.ordinal()) {
+                mob.setCombatTarget(null);
+                return;
+            }
+            if (System.currentTimeMillis() > mob.getLastAttackTime())
+                AttackTarget(mob, mob.getCombatTarget());
+
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: CheckForAttack" + " " + e.getMessage());
+        }
+    }
+
+    private static void CheckToSendMobHome(Mob mob) {
+
+        if(mob.isNecroPet())
+            return;
+
+        try {
+
+            //trebs dont recall
+            if(mob.isSiege())
+                return;
+
+            if(mob.BehaviourType.equals(Enum.MobBehaviourType.Pet1)){
+                if(mob.loc.distanceSquared(mob.getOwner().loc) > 60 * 60)
+                    mob.teleport(mob.getOwner().loc);
+                return;
+            }
+
+            if (mob.BehaviourType.isAgressive) {
+
+                if (mob.isPlayerGuard()) {
+                    if (mob.BehaviourType.ordinal() == Enum.MobBehaviourType.GuardCaptain.ordinal())
+                        CheckForPlayerGuardAggro(mob);
+                } else {
+                    CheckForAggro(mob);
+                }
+            }
+
+            if (mob.isPlayerGuard() && !mob.despawned) {
+
+                City current = ZoneManager.getCityAtLocation(mob.getLoc());
+
+                if (current == null || current.equals(mob.getGuild().getOwnedCity()) == false) {
+
+                    PowersBase recall = PowersManager.getPowerByToken(-1994153779);
+                    PowersManager.useMobPower(mob, mob, recall, 40);
+                    mob.setCombatTarget(null);
+                    if (mob.BehaviourType.ordinal() == Enum.MobBehaviourType.GuardCaptain.ordinal() && mob.isAlive()) {
+
+                        //guard captain pulls his minions home with him
+
+                        for (Map.Entry<Mob, Integer> minion : mob.siegeMinionMap.entrySet()) {
+                            PowersManager.useMobPower(minion.getKey(), minion.getKey(), recall, 40);
+                            minion.getKey().setCombatTarget(null);
+                        }
+                    }
+                }
+            } else if (!MovementUtilities.inRangeOfBindLocation(mob)) {
+
+                PowersBase recall = PowersManager.getPowerByToken(-1994153779);
+                PowersManager.useMobPower(mob, mob, recall, 40);
+                mob.setCombatTarget(null);
+
+                for (Map.Entry playerEntry : mob.playerAgroMap.entrySet())
+                    PlayerCharacter.getFromCache((int) playerEntry.getKey()).setHateValue(0);
+                mob.setCombatTarget(null);
+            }
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: CheckToSendMobHome" + " " + e.getMessage());
+        }
+    }
+
+    private static void chaseTarget(Mob mob) {
+
+        try {
+
+            if(mob.combatTarget != null && mob.combatTarget.getObjectType().equals(Enum.GameObjectType.PlayerCharacter) && !mob.canSee((PlayerCharacter)mob.combatTarget)){
+                mob.setCombatTarget(null);
+                return;
+            }
+
+            float rangeSquared = mob.getRange() * mob.getRange();
+            float distanceSquared = mob.getLoc().distanceSquared2D(mob.getCombatTarget().getLoc());
+
+            if(mob.isMoving() == true && distanceSquared < rangeSquared - 50) {
+                mob.destination = mob.getLoc();
+                MovementUtilities.moveToLocation(mob, mob.destination, 0);
+            } else if (CombatUtilities.inRange2D(mob, mob.getCombatTarget(), mob.getRange()) == false) {
+                if (mob.getRange() > 15) {
+                    mob.destination = mob.getCombatTarget().getLoc();
+                    MovementUtilities.moveToLocation(mob, mob.destination, 0);
+                } else {
+
+                    //check if building
+
+                    switch (mob.getCombatTarget().getObjectType()) {
+                        case PlayerCharacter:
+                        case Mob:
+                            mob.destination = MovementUtilities.GetDestinationToCharacter(mob, (AbstractCharacter) mob.getCombatTarget());
+                            MovementUtilities.moveToLocation(mob, mob.destination, mob.getRange() + 1);
+                            break;
+                        case Building:
+                            mob.destination = mob.getCombatTarget().getLoc();
+                            MovementUtilities.moveToLocation(mob, mob.getCombatTarget().getLoc(), 0);
+                            break;
+                    }
+                }
+            }
+            mob.updateMovementState();
+            mob.updateLocation();
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: chaseTarget" + " " + e.getMessage());
+        }
+    }
+
+    static void SafeGuardAggro(Mob mob) {
+        try {
+
+            HashSet<AbstractWorldObject> awoList = WorldGrid.getObjectsInRangePartial(mob, 100, MBServerStatics.MASK_MOB);
+
+            for (AbstractWorldObject awoMob : awoList) {
+
+                //dont scan self.
+
+                if (mob.equals(awoMob) || (mob.agentType.equals(Enum.AIAgentType.GUARD)) || (mob.agentType.equals(Enum.AIAgentType.PET)))
+                    continue;
+
+                Mob aggroMob = (Mob) awoMob;
+
+                //don't attack other guards
+
+                if ((aggroMob.agentType.equals(Enum.AIAgentType.GUARD)))
+                    continue;
+
+                if(aggroMob.BehaviourType.equals(Enum.MobBehaviourType.Pet1))
+                    continue;
+
+                if (mob.getLoc().distanceSquared2D(aggroMob.getLoc()) > sqr(50))
+                    continue;
+
+                mob.setCombatTarget(aggroMob);
+            }
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: SafeGuardAggro" + " " + e.getMessage());
+        }
+    }
+
+    public static void checkToDropGuardAggro(Mob mob){
+        City city = mob.guardedCity;
+
+        if(city == null)
+            return;
+        if(mob.combatTarget == null)
+            return;
+
+        //if(city._playerMemory.contains(mob.combatTarget.getObjectUUID()) && mob.combatTarget.getObjectType().equals(Enum.GameObjectType.PlayerCharacter))
+        //    mob.setCombatTarget(null);
+    }
+
+    public static void CheckForPlayerGuardAggro(Mob mob) {
+
+        try {
+
+            //looks for and sets mobs combatTarget
+
+            if (!mob.isAlive())
+                return;
+
+            ConcurrentHashMap<Integer, Boolean> loadedPlayers = mob.playerAgroMap;
+
+            for (Map.Entry playerEntry : loadedPlayers.entrySet()) {
+
+                int playerID = (int) playerEntry.getKey();
+                PlayerCharacter loadedPlayer = PlayerCharacter.getFromCache(playerID);
+
+                //Player is null, let's remove them from the list.
+
+                if (loadedPlayer == null) {
+                    loadedPlayers.remove(playerID);
+                    continue;
+                }
+
+                //Player is Dead, Mob no longer needs to attempt to aggro. Remove them from aggro map.
+
+                if (!loadedPlayer.isAlive()) {
+                    loadedPlayers.remove(playerID);
+                    continue;
+                }
+
+                //Can't see target, skip aggro.
+
+                if (!mob.canSee(loadedPlayer))
+                    continue;
+
+                // No aggro for this player
+
+                if (GuardCanAggro(mob, loadedPlayer) == false)
+                    continue;
+
+                if (MovementUtilities.inRangeToAggro(mob, loadedPlayer) && mob.getCombatTarget() == null) {
+                    mob.setCombatTarget(loadedPlayer);
+                    return;
+                }
+            }
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: CheckForPlayerGuardAggro" + e.getMessage());
+        }
+    }
+
+    public static Boolean GuardCanAggro(Mob mob, PlayerCharacter target) {
+
+        try {
+
+            if (mob.getGuild().getNation().equals(target.getGuild().getNation()))
+                return false;
+
+            if (mob.BehaviourType.ordinal() == Enum.MobBehaviourType.GuardMinion.ordinal()) {
+                if (((Mob) mob.npcOwner).building.getCity().cityOutlaws.contains(target.getObjectUUID()) == true) {
+                    return true;
+                }
+            } else if (mob.building.getCity().cityOutlaws.contains(target.getObjectUUID()) == true) {
+                return true;
+            }
+
+            //first check condemn list for aggro allowed (allies button is checked)
+
+            if (ZoneManager.getCityAtLocation(mob.getLoc()).getTOL().reverseKOS) {
+                for (Map.Entry<Integer, Condemned> entry : ZoneManager.getCityAtLocation(mob.getLoc()).getTOL().getCondemned().entrySet()) {
+
+                    //target is listed individually
+
+                    if (entry.getValue().getPlayerUID() == target.getObjectUUID() && entry.getValue().isActive())
+                        return false;
+
+                    //target's guild is listed
+
+                    if (Guild.getGuild(entry.getValue().getGuildUID()) == target.getGuild())
+                        return false;
+
+                    //target's nation is listed
+
+                    if (Guild.getGuild(entry.getValue().getGuildUID()) == target.getGuild().getNation())
+                        return false;
+                }
+                return true;
+            } else {
+
+                //allies button is not checked
+
+                for (Map.Entry<Integer, Condemned> entry : ZoneManager.getCityAtLocation(mob.getLoc()).getTOL().getCondemned().entrySet()) {
+
+                    //target is listed individually
+
+                    if (entry.getValue().getPlayerUID() == target.getObjectUUID() && entry.getValue().isActive())
+                        return true;
+
+                    //target's guild is listed
+
+                    if (Guild.getGuild(entry.getValue().getGuildUID()) == target.getGuild())
+                        return true;
+
+                    //target's nation is listed
+
+                    if (Guild.getGuild(entry.getValue().getGuildUID()) == target.getGuild().getNation())
+                        return true;
+                }
+            }
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: GuardCanAggro" + " " + e.getMessage());
+        }
+        return false;
+    }
+
+    public static AbstractWorldObject ChangeTargetFromHateValue(Mob mob) {
+
+        try {
+
+            float CurrentHateValue = 0;
+
+            if (mob.getCombatTarget() != null && mob.getCombatTarget().getObjectType().equals(Enum.GameObjectType.PlayerCharacter))
+                CurrentHateValue = ((PlayerCharacter) mob.getCombatTarget()).getHateValue();
+
+            AbstractWorldObject mostHatedTarget = null;
+
+            for (Map.Entry playerEntry : mob.playerAgroMap.entrySet()) {
+
+                PlayerCharacter potentialTarget = PlayerCharacter.getFromCache((int) playerEntry.getKey());
+
+                if (potentialTarget.equals(mob.getCombatTarget()))
+                    continue;
+
+                if (potentialTarget != null && potentialTarget.getHateValue() > CurrentHateValue && MovementUtilities.inRangeToAggro(mob, potentialTarget)) {
+                    CurrentHateValue = potentialTarget.getHateValue();
+                    mostHatedTarget = potentialTarget;
+                }
+
+            }
+            return mostHatedTarget;
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: ChangeTargetFromMostHated" + " " + e.getMessage());
+        }
+        return null;
+    }
+
+    public static void RecoverHealth(Mob mob) {
+        //recover health
+        try {
+            if (mob.getTimestamps().containsKey("HEALTHRECOVERED") == false)
+                mob.getTimestamps().put("HEALTHRECOVERED", System.currentTimeMillis());
+
+            if (mob.isSit() && mob.getTimeStamp("HEALTHRECOVERED") < System.currentTimeMillis() + 3000)
+                if (mob.getHealth() < mob.getHealthMax()) {
+
+                    float recoveredHealth = mob.getHealthMax() * ((1 + mob.getBonuses().getFloatPercentAll(Enum.ModType.HealthRecoverRate, Enum.SourceType.None)) * 0.01f);
+                    mob.setHealth(mob.getHealth() + recoveredHealth);
+                    mob.getTimestamps().put("HEALTHRECOVERED", System.currentTimeMillis());
+
+                    if (mob.getHealth() > mob.getHealthMax())
+                        mob.setHealth(mob.getHealthMax());
+                }
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: RecoverHealth" + " " + e.getMessage());
+        }
+    }
+
+    public static void enterCombat(Mob mob){
+        mob.setCombat(true);
+        UpdateStateMsg rwss = new UpdateStateMsg();
+        rwss.setPlayer(mob);
+        DispatchMessage.sendToAllInRange(mob, rwss);
+    }
+
+    public static boolean canCast(Mob mob) {
+
+        try {
+
+            // Performs validation to determine if a
+            // mobile in the proper state to cast.
+
+            if (mob == null)
+                return false;
+
+            if (mob.nextCastTime == 0)
+                mob.nextCastTime = System.currentTimeMillis() - 1000L;
+
+            if(mob.nextCastTime > System.currentTimeMillis())
+                return false;
+
+            if(mob.isPlayerGuard){
+
+                int contractID = 0;
+
+                if(mob.BehaviourType.equals(Enum.MobBehaviourType.GuardMinion) && mob.npcOwner != null)
+                    contractID = mob.npcOwner.contract.getContractID();
+                else if(mob.contract != null)
+                    contractID = mob.contract.getContractID();
+
+                if(Enum.MinionType.ContractToMinionMap.containsKey(contractID) && !Enum.MinionType.ContractToMinionMap.get(contractID).isMage())
+                    return false;
+            }
+
+            if (mob.mobPowers == null || mob.mobPowers.isEmpty())
+                return false;
+
+            if (!mob.canSee((PlayerCharacter) mob.getCombatTarget())) {
+                mob.setCombatTarget(null);
+                return false;
+            }
+
+            return mob.nextCastTime <= System.currentTimeMillis();
+
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: canCast" + " " + e.getMessage());
+        }
+        return false;
+    }
+
+    public static boolean MobCast(Mob mob) {
+
+        try {
+            // Method picks a random spell from a mobile's list of powers
+            // and casts it on the current target (or itself).  Validation
+            // (including empty lists) is done previously within canCast();
+
+            ArrayList<Integer> powerTokens;
+            ArrayList<Integer> purgeTokens;
+            AbstractCharacter target = (AbstractCharacter) mob.getCombatTarget();
+
+            if (mob.BehaviourType.callsForHelp)
+                MobCallForHelp(mob);
+
+            // Generate a list of tokens from the mob powers for this mobile.
+
+            powerTokens = new ArrayList<>(mob.mobPowers.keySet());
+            purgeTokens = new ArrayList<>();
+
+            // If player has this effect on them currently then remove
+            // this token from our list.
+
+            for (int powerToken : powerTokens) {
+
+                PowersBase powerBase = PowersManager.getPowerByToken(powerToken);
+
+                for (ActionsBase actionBase : powerBase.getActions()) {
+
+                    String stackType = actionBase.stackType;
+
+                    if (target.getEffects() != null && target.getEffects().containsKey(stackType))
+                        purgeTokens.add(powerToken);
+                }
+            }
+
+            powerTokens.removeAll(purgeTokens);
+
+            // Sanity check
+
+            if (powerTokens.isEmpty())
+                return false;
+
+            // Pick random spell from our list of powers
+
+            int powerToken = powerTokens.get(ThreadLocalRandom.current().nextInt(powerTokens.size()));
+            int powerRank = mob.mobPowers.get(powerToken);
+
+            PowersBase mobPower = PowersManager.getPowerByToken(powerToken);
+
+            if(mobPower.powerCategory.equals(Enum.PowerCategoryType.DEBUFF))
+                return false;
+
+            //check for hit-roll
+
+            if (mobPower.requiresHitRoll)
+                if (CombatUtilities.triggerDefense(mob, mob.getCombatTarget()))
+                    return false;
+
+            // Cast the spell
+
+            if (CombatUtilities.inRange2D(mob, mob.getCombatTarget(), mobPower.getRange())) {
+
+                PerformActionMsg msg;
+
+                if (!mob.StrongholdCommander && !mob.StrongholdEpic && (!mobPower.isHarmful() || mobPower.targetSelf)) {
+                    PowersManager.useMobPower(mob, mob, mobPower, powerRank);
+                    msg = PowersManager.createPowerMsg(mobPower, powerRank, mob, mob);
+                } else {
+                    PowersManager.useMobPower(mob, target, mobPower, powerRank);
+                    msg = PowersManager.createPowerMsg(mobPower, powerRank, mob, target);
+                }
+
+                msg.setUnknown04(2);
+
+                PowersManager.finishUseMobPower(msg, mob, 0, 0);
+                long delay = 20000L;
+                mob.nextCastTime = System.currentTimeMillis() + delay;
+
+                return true;
+            }
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: MobCast" + " " + e.getMessage());
+        }
+        return false;
+    }
+
+    public static boolean GuardCast(Mob mob) {
+
+        try {
+            // Method picks a random spell from a mobile's list of powers
+            // and casts it on the current target (or itself).  Validation
+            // (including empty lists) is done previously within canCast();
+
+            ArrayList<Integer> powerTokens;
+            ArrayList<Integer> purgeTokens;
+            AbstractCharacter target = (AbstractCharacter) mob.getCombatTarget();
+
+            if (mob.BehaviourType.callsForHelp)
+                MobCallForHelp(mob);
+
+            // Generate a list of tokens from the mob powers for this mobile.
+
+            powerTokens = new ArrayList<>(mob.mobPowers.keySet());
+            purgeTokens = new ArrayList<>();
+
+            // If player has this effect on them currently then remove
+            // this token from our list.
+
+            for (int powerToken : powerTokens) {
+
+                PowersBase powerBase = PowersManager.getPowerByToken(powerToken);
+
+                for (ActionsBase actionBase : powerBase.getActions()) {
+
+                    String stackType = actionBase.stackType;
+
+                    if (target.getEffects() != null && target.getEffects().containsKey(stackType))
+                        purgeTokens.add(powerToken);
+                }
+            }
+
+            powerTokens.removeAll(purgeTokens);
+
+            // Sanity check
+
+            if (powerTokens.isEmpty())
+                return false;
+
+            int powerToken = 0;
+            int nukeRoll = ThreadLocalRandom.current().nextInt(1,100);
+
+            if (nukeRoll < 55) {
+
+                //use direct damage spell
+                powerToken = powerTokens.get(powerTokens.size() - 1);
+
+            } else {
+                //use random spell
+                powerToken = powerTokens.get(ThreadLocalRandom.current().nextInt(powerTokens.size()));
+            }
+
+            int powerRank = 1;
+
+            switch(mob.getRank()){
+                case 1:
+                    powerRank = 10;
+                    break;
+                case 2:
+                    powerRank = 15;
+                    break;
+                case 3:
+                    powerRank = 20;
+                    break;
+                case 4:
+                    powerRank = 25;
+                    break;
+                case 5:
+                    powerRank = 30;
+                    break;
+                case 6:
+                    powerRank = 35;
+                    break;
+                case 7:
+                    powerRank = 40;
+                    break;
+            }
+
+            PowersBase mobPower = PowersManager.getPowerByToken(powerToken);
+
+            //check for hit-roll
+
+            if (mobPower.requiresHitRoll)
+                if (CombatUtilities.triggerDefense(mob, mob.getCombatTarget()))
+                    return false;
+
+            // Cast the spell
+
+            if (CombatUtilities.inRange2D(mob, mob.getCombatTarget(), mobPower.getRange())) {
+
+                PerformActionMsg msg;
+
+                if (!mobPower.isHarmful() || mobPower.targetSelf) {
+
+                    if (mobPower.category.equals("DISPEL")) {
+                        PowersManager.useMobPower(mob, target, mobPower, powerRank);
+                        msg = PowersManager.createPowerMsg(mobPower, powerRank, mob, target);
+                    } else {
+                        PowersManager.useMobPower(mob, mob, mobPower, powerRank);
+                        msg = PowersManager.createPowerMsg(mobPower, powerRank, mob, mob);
+                    }
+                } else {
+                    PowersManager.useMobPower(mob, target, mobPower, powerRank);
+                    msg = PowersManager.createPowerMsg(mobPower, powerRank, mob, target);
+                }
+
+                msg.setUnknown04(2);
+
+                PowersManager.finishUseMobPower(msg, mob, 0, 0);
+
+                long randomCooldown = (long)((ThreadLocalRandom.current().nextInt(10,15) * 1000) * MobAIThread.AI_CAST_FREQUENCY);
+                return true;
+            }
+        } catch (Exception e) {
+            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: MobCast" + " " + e.getMessage());
+        }
+        return false;
+    }
+
+    public static boolean EarlyExit(Mob mob) {
+        try {
+
+            //always check the respawn que, respawn 1 mob max per second to not flood the client
+
+            if (mob == null)
+                return true;
+
+            if (mob.getTimestamps().containsKey("lastExecution") == false)
+                mob.getTimestamps().put("lastExecution", System.currentTimeMillis());
+
+            if (System.currentTimeMillis() < mob.getTimeStamp("lastExecution"))
+                return true;
+
+            mob.getTimestamps().put("lastExecution", System.currentTimeMillis() + MobAIThread.AI_PULSE_MOB_THRESHOLD);
+
+            //trebuchet spawn handler
+
+            if (mob.despawned && mob.getMobBase().getLoadID() == 13171) {
+                CheckForRespawn(mob);
+                return true;
+            }
+
+            //override for guards
+
+            if (mob.despawned && mob.isPlayerGuard) {
+
+                if (mob.BehaviourType.ordinal() == Enum.MobBehaviourType.GuardMinion.ordinal()) {
+                    if (mob.npcOwner.isAlive() == false || ((Mob) mob.npcOwner).despawned == true) {
+
+                        //minions don't respawn while guard captain is dead
+
+                        if (mob.isAlive() == false) {
+                            mob.deathTime = System.currentTimeMillis();
+                            return true;
+                        }
+
+                    }
+                }
+
+                CheckForRespawn(mob);
+
+                //check to send mob home for player guards to prevent exploit of dragging guards away and then teleporting
+
+                return true;
+            }
+
+            //no need to continue if mob is dead, check for respawn and move on
+
+            if (!mob.isAlive()) {
+                CheckForRespawn(mob);
+                return true;
+            }
+
+            //no players loaded, no need to proceed
+
+            if (mob.playerAgroMap.isEmpty()) {
+                if (mob.getCombatTarget() != null)
+                    mob.setCombatTarget(null);
+                return true;
+            }
+
+            if (mob.isAlive())
+                if (!mob.getMovementLoc().equals(Vector3fImmutable.ZERO))
+                    mob.setLoc(mob.getMovementLoc());
+
+            if (mob.isPet() == false && mob.isPlayerGuard == false)
+                CheckToSendMobHome(mob);
+
+            if (mob.getCombatTarget() != null) {
+
+                if (mob.getCombatTarget().isAlive() == false) {
+                    mob.setCombatTarget(null);
+                    return true;
+                }
+
+                if (mob.getCombatTarget().getObjectTypeMask() == MBServerStatics.MASK_PLAYER) {
+
+                    PlayerCharacter target = (PlayerCharacter) mob.getCombatTarget();
+
+                    if (mob.playerAgroMap.containsKey(target.getObjectUUID()) == false) {
+                        mob.setCombatTarget(null);
+                        return true;
+                    }
+
+                    if (mob.canSee((PlayerCharacter) mob.getCombatTarget()) == false) {
+                        mob.setCombatTarget(null);
+                        return true;
+                    }
+
+                }
+            }
+        }catch(Exception e){
+            Logger.error("FAILED EARLY EXIT CHECK");
+        }
+        return false;
+    }
+}
diff --git a/src/engine/mobileAI/Threads/MobAIThread.java b/src/engine/mobileAI/Threads/MobAIThread.java
index 8a7d8a8a..1221e4aa 100644
--- a/src/engine/mobileAI/Threads/MobAIThread.java
+++ b/src/engine/mobileAI/Threads/MobAIThread.java
@@ -3,6 +3,7 @@ package engine.mobileAI.Threads;
 import engine.gameManager.ConfigManager;
 import engine.mobileAI.MobAI;
 import engine.gameManager.ZoneManager;
+import engine.mobileAI.MobBehaviours.StaticBehaviours;
 import engine.objects.Mob;
 import engine.objects.Zone;
 import engine.server.MBServerStatics;
@@ -33,7 +34,8 @@ public class MobAIThread implements Runnable{
                         for (Mob mob : zone.zoneMobSet) {
                             try {
                                 if (mob != null) {
-                                    MobAI.DetermineAction(mob);
+                                    //MobAI.DetermineAction(mob);
+                                    StaticBehaviours.runBehaviour(mob);
                                 }
                             } catch (Exception e) {
                                 Logger.error("Error processing Mob [Name: {}, UUID: {}]", mob.getName(), mob.getObjectUUID(), e);