//  • ▌ ▄ ·.  ▄▄▄·  ▄▄ • ▪   ▄▄· ▄▄▄▄·  ▄▄▄·  ▐▄▄▄  ▄▄▄ .
//  ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
//  ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
//  ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
//  ▀▀  █▪▀▀▀ ▀  ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀  ▀  ▀ ▀▀  █▪ ▀▀▀
//      Magicbane Emulator Project © 2013 - 2026
//                https://www.magicbane.com

package engine.mobileAI;

import engine.InterestManagement.RealmMap;
import engine.InterestManagement.WorldGrid;
import engine.gameManager.*;
import engine.math.Vector3f;
import engine.math.Vector3fImmutable;
import engine.mbEnums;
import engine.mbEnums.DispatchChannel;
import engine.mobileAI.Threads.MobAIThread;
import engine.mobileAI.Threads.ReSpawner;
import engine.mobileAI.utilities.MovementUtilities;
import engine.net.client.msg.PerformActionMsg;
import engine.net.client.msg.PowerProjectileMsg;
import engine.objects.*;
import engine.powers.ActionsBase;
import engine.powers.PowersBase;
import engine.powers.RunePowerEntry;
import engine.server.MBServerStatics;
import org.pmw.tinylog.Logger;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;

import static engine.math.FastMath.sqr;

public class MobAI {

    // MB Dev notes:
    // Class implements mobile AI mechanics for Magicbane.
    //
    // Controls all mob actions from regular mobs to pets and guards.
    // Initiates in the "DetermineAction" method and branches from there
    //
    // CombatManager.class implements shared combat routines for all avatars.

    private static void attackTarget(Mob mob, AbstractWorldObject target) {

        try {

            if (mob == null)
                return;

            if (target == null || !target.isAlive()) {
                mob.setCombatTarget(null);
                return;
            }

            if (target.getObjectType().equals(mbEnums.GameObjectType.PlayerCharacter) && !mob.canSee((AbstractCharacter) target)) {
                mob.setCombatTarget(null);
                return;
            }

            if (target.getObjectType() == mbEnums.GameObjectType.PlayerCharacter && canCast(mob)) {

                if (mobCast(mob)) {
                    mob.updateLocation();
                    return;
                }

            }

            if (mob.getRange() * mob.getRange() < mob.loc.distanceSquared(target.loc))
                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();

        } catch (Exception e) {
            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: AttackTarget" + " " + e.getMessage());
        }
    }

    public static void attackPlayer(Mob mob, PlayerCharacter target) {

        try {
            if (target == null || !target.isAlive() || !target.isActive()) {
                mob.setCombatTarget(null);
                return;
            }
            if (!mob.canSee(target)) {
                mob.setCombatTarget(null);
                return;
            }

            if (mob.behaviourType.callsForHelp)
                mobCallForHelp(mob);

            if (!MovementUtilities.inRangeDropAggro(mob, target)) {
                mob.setCombatTarget(null);
                return;
            }

            if (mob.getRange() * mob.getRange() >= mob.loc.distanceSquared(target.loc)) {


                // ranged mobs cant attack while running. skip until they finally stop.

                if (mob.isMoving() && mob.getRange() > 20)
                    return;

                CombatManager.combatCycle(mob, target);
            }

            if (target.getPet() != null)
                if (target.getPet().getCombatTarget() == null && target.getPet().assist)
                    target.getPet().setCombatTarget(mob);

        } catch (Exception e) {
            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: AttackPlayer" + " " + e.getMessage());
        }

    }

    public static void attackBuilding(Mob mob, Building target) {

        try {

            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.agentType.equals(mbEnums.AIAgentType.GUARDCAPTAIN))
                        if (guard.getCombatTarget() == null && !guard.getGuild().equals(mob.getGuild()))
                            guard.setCombatTarget(mob);

            if (mob.isSiege())
                MovementManager.sendRWSSMsg(mob);


            CombatManager.combatCycle(mob, target);

            if (mob.isSiege()) {
                PowerProjectileMsg ppm = new PowerProjectileMsg(mob, target);
                ppm.setRange(50);
                DispatchManager.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 == null || target == null)
                return;

            if (mob.getRange() >= 30 && mob.isMoving())
                return;

            //no weapons, default mob attack speed 3 seconds.

            CombatManager.combatCycle(mob, target);

        } catch (Exception e) {
            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: AttackMob" + " " + e.getMessage());
        }
    }

    private static void patrol(Mob mob) {

        try {

            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.
            // Minions are force marched if captain is alive

            boolean forced = mob.agentType.equals(mbEnums.AIAgentType.GUARDMINION) && mob.guardCaptain.isAlive();

            if (mob.stopPatrolTime + (patrolDelay * 1000L) > System.currentTimeMillis())
                if (!forced)
                    return;

            //guards inherit barracks patrol points dynamically

            if (mob.patrolPoints == null || mob.patrolPoints.isEmpty())
                if (mob.agentType.equals(mbEnums.AIAgentType.GUARDCAPTAIN) || mob.agentType.equals(mbEnums.AIAgentType.GUARDMINION)) {

                    Building barracks = mob.building;

                    if (barracks != null && barracks.patrolPoints != null && !barracks.getPatrolPoints().isEmpty()) {
                        mob.patrolPoints = barracks.patrolPoints;
                    } else {
                        randomGuardPatrolPoint(mob);
                        return;
                    }
                }

            assert mob.patrolPoints != null;
            if (mob.lastPatrolPointIndex > mob.patrolPoints.size() - 1)
                mob.lastPatrolPointIndex = 0;

            // Minions are given marching orders by the captain if he is alive

            if (mob.agentType.equals(mbEnums.AIAgentType.GUARDMINION)) {
                Mob captain = (Mob) mob.guardCaptain;
                mob.destination = captain.destination.add(mbEnums.FormationType.getOffset(2, mob.guardCaptain.minions.indexOf(mob.getObjectUUID()) + 3));
                mob.lastPatrolPointIndex = captain.lastPatrolPointIndex;
            } else {
                mob.destination = mob.patrolPoints.get(mob.lastPatrolPointIndex);
                mob.lastPatrolPointIndex += 1;
            }

            // Captain orders minions to patrol

            if (mob.agentType.equals(mbEnums.AIAgentType.GUARDCAPTAIN))
                for (Integer minionUUID : mob.minions) {
                    Mob minion = Mob.getMob(minionUUID);
                    assert minion != null;
                    if (minion.isAlive() && minion.combatTarget == null)
                        MobAI.patrol(minion);
                }

            MovementUtilities.aiMove(mob, mob.destination, true);

        } catch (Exception e) {
            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: AttackTarget" + " " + e.getMessage());
        }
    }

    public static boolean canCast(Mob mob) {

        int contractID = 0;

        try {

            // Performs validation to determine if a
            // mobile in the proper state to cast.

            if (mob == null)
                return false;

            if (mob.isPlayerGuard()) {

                if (mob.agentType.equals(mbEnums.AIAgentType.GUARDWALLARCHER))
                    return false; //wall archers don't cast
                if (mob.agentType.equals(mbEnums.AIAgentType.GUARDMINION))
                    contractID = mob.guardCaptain.contract.getContractID();
                else
                    contractID = mob.contract.getContractID();

                // exception allowing werewolf and werebear guard captains to cast

                if (!mbEnums.MinionType.ContractToMinionMap.get(contractID).isMage() && contractID != 980103 && contractID != 980104)
                    return false;
            }

            // Mobile has no powers defined in mobbase or contract..

            if (PowersManager.getPowersForRune(mob.getMobBaseID()).isEmpty() && PowersManager.getPowersForRune(contractID).isEmpty())
                return false;

            if (mob.nextCastTime == 0)
                mob.nextCastTime = System.currentTimeMillis();

            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<RunePowerEntry> powerEntries;
            ArrayList<RunePowerEntry> purgeEntries;
            AbstractCharacter target = (AbstractCharacter) mob.getCombatTarget();

            if (mob.behaviourType.callsForHelp)
                mobCallForHelp(mob);

            // Generate a list of tokens from the mob powers for this mobile.

            powerEntries = new ArrayList<>(PowersManager.getPowersForRune(mob.getMobBaseID()));
            purgeEntries = new ArrayList<>();

            // Additional powers may come from the contract ID.  This is to support
            // powers for player guards irrespective of the mobbase used.

            if (mob.isPlayerGuard()) {

                ArrayList<RunePowerEntry> contractEntries = new ArrayList<>();

                if (mob.contract != null)
                    contractEntries = PowersManager.getPowersForRune(mob.contractUUID);

                if (mob.agentType.equals(mbEnums.AIAgentType.GUARDMINION))
                    contractEntries = PowersManager.getPowersForRune(mob.guardCaptain.contractUUID);

                powerEntries.addAll(contractEntries);

            }

            // If player has this effect on them currently then remove
            // this token from our list.

            for (RunePowerEntry runePowerEntry : powerEntries) {

                PowersBase powerBase = PowersManager.getPowerByToken(runePowerEntry.token);

                for (ActionsBase actionBase : powerBase.getActions()) {

                    String stackType = actionBase.stackType;

                    if (target.getEffects() != null && target.getEffects().containsKey(stackType))
                        purgeEntries.add(runePowerEntry);
                }
            }

            powerEntries.removeAll(purgeEntries);

            // Sanity check

            if (powerEntries.isEmpty())
                return false;

            // Pick random spell from our list of powers

            RunePowerEntry runePowerEntry = powerEntries.get(ThreadLocalRandom.current().nextInt(powerEntries.size()));

            PowersBase mobPower = PowersManager.getPowerByToken(runePowerEntry.token);
            int powerRank = runePowerEntry.rank;

            if (mob.isPlayerGuard())
                powerRank = getGuardPowerRank(mob);

            // Cast the spell

            if (mob.getRange() * mob.getRange() >= mob.loc.distanceSquared(target.loc)) {

                PerformActionMsg msg;

                if (!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 randomCooldown = (long) ((ThreadLocalRandom.current().nextInt(10, 15) * 1000L) * MobAIThread.AI_CAST_FREQUENCY);

                mob.nextCastTime = System.currentTimeMillis() + randomCooldown;
                return true;
            }
        } catch (Exception e) {
            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: MobCast" + " " + e.getMessage());
        }
        return false;
    }

    public static int getGuardPowerRank(Mob mob) {
        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;
        }
        return powerRank;
    }

    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.parentZone;

            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());
        }
    }

    public static void determineAction(Mob mob) {

        try {

            //always check the respawn que, respawn 1 mob max per second to not flood the client

            if (mob == null)
                return;

            if (!mob.getTimestamps().containsKey("lastExecution"))
                mob.getTimestamps().put("lastExecution", System.currentTimeMillis());

            if (System.currentTimeMillis() < mob.getTimeStamp("lastExecution"))
                return;

            mob.getTimestamps().put("lastExecution", System.currentTimeMillis() + MobAIThread.AI_PULSE_MOB_THRESHOLD);

            //trebuchet spawn handler

            if (mob.despawned && mob.getMobBase().getLoadID() == 13171) {
                checkForRespawn(mob);
                return;
            }

            //override for guards

            if (mob.despawned && mob.isPlayerGuard()) {

                if (mob.agentType.equals(mbEnums.AIAgentType.GUARDMINION)) {
                    if (!mob.guardCaptain.isAlive() || ((Mob) mob.guardCaptain).despawned) {

                        //minions don't respawn while guard captain is dead

                        if (!mob.isAlive()) {
                            mob.deathTime = System.currentTimeMillis();
                            return;
                        }

                    }
                }

                checkForRespawn(mob);

                //check to send mob home for player guards to prevent exploit of dragging guards away and then teleporting

                if (!mob.agentType.equals(mbEnums.AIAgentType.PET))
                    checkToSendMobHome(mob);

                return;
            }

            //no need to continue if mob is dead, check for respawn and move on

            if (!mob.isAlive()) {
                checkForRespawn(mob);
                return;
            }

            //no players loaded, no need to proceed unless it's a player guard
            boolean bypassLoadedPlayerCheck = false;
            if (mob.isPlayerGuard() || mob.isSiege()) {
                bypassLoadedPlayerCheck = true;
                if (mob.combatTarget != null && mob.combatTarget.getObjectType().equals(mbEnums.GameObjectType.PlayerCharacter))
                    if (mob.combatTarget.loc.distanceSquared(mob.loc) > 10000)
                        mob.setCombatTarget(null);
            }

            if (mob.playerAgroMap.isEmpty() && !bypassLoadedPlayerCheck) {
                if (mob.getCombatTarget() != null)
                    mob.setCombatTarget(null);
                return;
            }


            if (!mob.agentType.equals(mbEnums.AIAgentType.PET))
                checkToSendMobHome(mob);

            if (mob.getCombatTarget() != null) {

                if (!mob.getCombatTarget().isAlive()) {
                    mob.setCombatTarget(null);
                    return;
                }

                if (mob.getCombatTarget().getObjectTypeMask() == MBServerStatics.MASK_PLAYER) {

                    PlayerCharacter target = (PlayerCharacter) mob.getCombatTarget();

                    if (!mob.playerAgroMap.containsKey(target.getObjectUUID())) {
                        mob.setCombatTarget(null);
                        return;
                    }

                    if (!mob.canSee((PlayerCharacter) mob.getCombatTarget())) {
                        mob.setCombatTarget(null);
                        return;
                    }

                }
            }

            switch (mob.behaviourType) {
                case GuardCaptain:
                case GuardMinion:
                case GuardWallArcher:
                    guardLogic(mob);
                    break;
                case Pet1:
                case SiegeEngine:
                    petLogic(mob);
                    break;
                case HamletGuard:
                    hamletGuardLogic(mob);
                    break;
                default:
                    defaultLogic(mob);
                    break;
            }
        } catch (Exception e) {
            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: DetermineAction" + " " + e.getMessage());
        }
    }

    private static void checkForAggro(Mob aiAgent) {

        try {

            //looks for and sets mobs combatTarget

            if (!aiAgent.isAlive())
                return;

            ConcurrentHashMap<Integer, Float> loadedPlayers = aiAgent.playerAgroMap;

            for (Entry<Integer, Float> playerEntry : loadedPlayers.entrySet()) {

                int playerID = playerEntry.getKey();
                PlayerCharacter loadedPlayer = PlayerCharacter.getPlayerCharacter(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.isEmpty() && aiAgent.notEnemy.contains(loadedPlayer.race.getRaceType().getMonsterType()))
                    continue;

                //mob has enemies and this player race is not it

                if (!aiAgent.enemy.isEmpty() && !aiAgent.enemy.contains(loadedPlayer.race.getRaceType().getMonsterType()))
                    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());
        }
    }

    private static void checkMobMovement(Mob mob) {

        try {

            if (!MovementUtilities.canMove(mob))
                return;

            mob.updateLocation();

            if (mob.behaviourType == mbEnums.MobBehaviourType.Pet1) {
                if (mob.guardCaptain == null)
                    return;

                //mob no longer has its owner loaded, translate pet to owner

                if (!mob.playerAgroMap.containsKey(mob.guardCaptain.getObjectUUID())) {
                    MovementManager.translocate(mob, mob.guardCaptain.getLoc());
                    return;
                }

                if (mob.getCombatTarget() == null) {

                    //move back to owner

                    if (mob.getRange() * mob.getRange() >= mob.loc.distanceSquared(mob.guardCaptain.loc))
                        return;

                    mob.destination = mob.guardCaptain.getLoc();
                    MovementUtilities.moveToLocation(mob, mob.destination, 5, false);
                } else
                    chaseTarget(mob);
            } else {
                if (mob.getCombatTarget() == null) {

                    if (!mob.isMoving()) {

                        // Minions only patrol on their own if captain is dead.

                        if (!mob.agentType.equals(mbEnums.AIAgentType.GUARDMINION))
                            patrol(mob);
                        else if (!mob.guardCaptain.isAlive())
                            patrol(mob);
                    } else
                        mob.stopPatrolTime = System.currentTimeMillis();
                } else {
                    chaseTarget(mob);
                }
            }
        } 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.charItemManager.getInventoryCount() > 0) {
                    if (System.currentTimeMillis() > aiAgent.deathTime + MBServerStatics.DESPAWN_TIMER_WITH_LOOT) {
                        aiAgent.despawn();
                        aiAgent.deathTime = System.currentTimeMillis();
                    }
                    //No items in inventory.
                } else {
                    //Mob's Loot has been looted.
                    if (aiAgent.isHasLoot()) {
                        if (System.currentTimeMillis() > aiAgent.deathTime + MBServerStatics.DESPAWN_TIMER_ONCE_LOOTED) {
                            aiAgent.despawn();
                            aiAgent.deathTime = System.currentTimeMillis();
                        }
                        //Mob never had Loot.
                    } else {
                        if (System.currentTimeMillis() > aiAgent.deathTime + MBServerStatics.DESPAWN_TIMER) {
                            aiAgent.despawn();
                            aiAgent.deathTime = System.currentTimeMillis();
                        }
                    }
                }
            } else if (System.currentTimeMillis() > aiAgent.deathTime + (aiAgent.spawnDelay * 1000L)) {
                aiAgent.respawnTime = aiAgent.deathTime + (aiAgent.spawnDelay * 1000L);
                ReSpawner.respawnQueue.put(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())
                return;

            if (mob.getCombatTarget() == null)
                return;

            if (mob.getCombatTarget().getObjectType().equals(mbEnums.GameObjectType.PlayerCharacter) && !MovementUtilities.inRangeDropAggro(mob, (PlayerCharacter) mob.getCombatTarget()) && !mob.agentType.equals(mbEnums.AIAgentType.PET)) {

                mob.setCombatTarget(null);
                return;
            }
            attackTarget(mob, mob.getCombatTarget());

        } catch (Exception e) {
            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: CheckForAttack" + " " + e.getMessage());
        }
    }

    private static void checkToSendMobHome(Mob mob) {

        try {

            if (mob.getCombatTarget() != null && mob.getRange() * mob.getRange() >= MobAIThread.AI_BASE_AGGRO_RANGE * 0.5f)
                return;

            if (mob.isPlayerGuard() && !mob.despawned) {

                City current = ZoneManager.getCityAtLocation(mob.getLoc());

                if (current == null || !current.equals(mob.getGuild().getOwnedCity())) {

                    PowersBase recall = PowersManager.getPowerByToken(-1994153779);
                    PowersManager.useMobPower(mob, mob, recall, 40);
                    mob.setCombatTarget(null);

                    if (mob.agentType.equals(mbEnums.AIAgentType.GUARDCAPTAIN) && mob.isAlive()) {

                        //guard captain pulls his minions home with him

                        for (Integer minionUUID : mob.minions) {
                            Mob minion = Mob.getMob(minionUUID);

                            PowersManager.useMobPower(minion, minion, recall, 40);
                            assert minion != null;
                            minion.setCombatTarget(null);
                        }
                    }
                }
            } else if (!MovementUtilities.inRangeOfBindLocation(mob)) {

                PowersBase recall = PowersManager.getPowerByToken(-1994153779);
                PowersManager.useMobPower(mob, mob, recall, 40);
                mob.setCombatTarget(null);

                mob.playerAgroMap.replaceAll((e, v) -> 0f);
            }
        } catch (Exception e) {
            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: CheckToSendMobHome" + " " + e.getMessage());
        }
    }

    private static void chaseTarget(Mob mob) {

        try {

            if (!mob.getTimestamps().containsKey("lastChase"))
                mob.getTimestamps().put("lastChase", System.currentTimeMillis());
            else if (System.currentTimeMillis() < mob.getTimestamps().get("lastChase") + (750 + ThreadLocalRandom.current().nextInt(0, 500)))
                return;

            mob.getTimestamps().put("lastChase", System.currentTimeMillis());

            if (mob.getRange() * mob.getRange() <= mob.loc.distanceSquared(mob.combatTarget.loc)) {
                if (mob.getRange() > 15) {
                    mob.destination = mob.getCombatTarget().getLoc();
                    MovementUtilities.moveToLocation(mob, mob.destination, 0, false);
                } 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, false);
                            break;
                        case Building:
                            mob.destination = mob.getCombatTarget().getLoc();
                            MovementUtilities.moveToLocation(mob, mob.getCombatTarget().getLoc(), 0, false);
                            break;
                    }
                }
            }
            mob.updateMovementState();
            mob.updateLocation();
        } catch (Exception e) {
            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: chaseTarget" + " " + e.getMessage());
        }
    }

    private 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))
                    continue;

                Mob aggroMob = (Mob) awoMob;

                //don't attack other guards
                if (aggroMob.isGuard())
                    continue;

                //don't attack pets
                if (aggroMob.agentType.equals(mbEnums.AIAgentType.PET))
                    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 guardLogic(Mob mob) {

        try {
            if (mob.getCombatTarget() == null) {
                checkForPlayerGuardAggro(mob);
            } else {
                //do not need to look to change target if target is already null
                AbstractWorldObject newTarget = changeTargetFromHateValue(mob);

                if (newTarget != null) {

                    if (newTarget.getObjectType().equals(mbEnums.GameObjectType.PlayerCharacter)) {
                        if (guardCanAggro(mob, (PlayerCharacter) newTarget))
                            mob.setCombatTarget(newTarget);
                    } else
                        mob.setCombatTarget(newTarget);
                }
            }

            if (mob.behaviourType.canRoam)
                checkMobMovement(mob);//all guards that can move check to move

            if (mob.combatTarget != null)
                checkForAttack(mob); //only check to attack if combat target is not null

        } catch (Exception e) {
            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: GuardLogic" + " " + e.getMessage());
        }
    }

    private static void petLogic(Mob mob) {

        try {

            if (mob.guardCaptain == null && !mob.isNecroPet() && !mob.isSiege())
                if (ZoneManager.seaFloor.zoneMobSet.contains(mob))
                    mob.killCharacter("no owner");

            if (MovementUtilities.canMove(mob) && mob.behaviourType.canRoam)
                checkMobMovement(mob);

            checkForAttack(mob);

            //recover health

            if (!mob.getTimestamps().containsKey("HEALTHRECOVERED"))
                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(mbEnums.ModType.HealthRecoverRate, mbEnums.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: PetLogic" + " " + e.getMessage());
        }
    }

    private static void hamletGuardLogic(Mob mob) {

        try {

            if (ConfigManager.MB_RULESET.getValue().equals("LORE")) {
                //lore guard aggro to kill members of opposing charters
                if (mob.getCombatTarget() == null || !mob.combatTarget.isAlive())
                    hamletGuardAggro(mob);
                else if (!mob.getCombatTarget().isAlive())
                    hamletGuardAggro(mob);
            } else {
                //safehold guard

                if (mob.getCombatTarget() == null || !mob.combatTarget.isAlive())
                    safeGuardAggro(mob);
                else if (!mob.getCombatTarget().isAlive())
                    safeGuardAggro(mob);
            }
            checkForAttack(mob);
        } catch (Exception e) {
            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: HamletGuardLogic" + " " + e.getMessage());
        }
    }

    private static void hamletGuardAggro(Mob mob) {
        Realm realm = RealmMap.getRealmAtLocation(mob.loc);
        if (realm.getRealmName().equals("Uthgaard")) {
            HashSet<AbstractWorldObject> loadedMobs = WorldGrid.getObjectsInRangePartial(mob.loc, MobAIThread.AI_BASE_AGGRO_RANGE, MBServerStatics.MASK_MOB);
            for (AbstractWorldObject awo : loadedMobs) {
                Mob targetMob = (Mob) awo;
                if (targetMob.equals(mob))
                    continue;
                if (!targetMob.isAlive() || targetMob.despawned)
                    continue;
                if (targetMob.isPet())
                    continue;
                mob.combatTarget = targetMob;
                return;
            }
            return;
        }
        HashSet<AbstractWorldObject> loadedPlayers = WorldGrid.getObjectsInRangePartial(mob.loc, MobAIThread.AI_BASE_AGGRO_RANGE, MBServerStatics.MASK_PLAYER);
        for (AbstractWorldObject awo : loadedPlayers) {
            PlayerCharacter pc = (PlayerCharacter) awo;
            if (!pc.isAlive() || !pc.isActive())
                continue;
            if (pc.guild.equals(Guild.getErrantGuild())) {
                mob.combatTarget = pc;
                return;
            }
            if (pc.guild.charter.equals(mob.guild.charter))
                continue;
            mob.combatTarget = pc;
            return;
        }
    }

    private static void defaultLogic(Mob mob) {

        try {

            //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 = changeTargetFromHateValue(mob);

                if (newTarget != null)
                    mob.setCombatTarget(newTarget);
                else {
                    if (mob.getCombatTarget() == null) {
                        if (mob.behaviourType == mbEnums.MobBehaviourType.HamletGuard)
                            safeGuardAggro(mob);  //safehold guard
                        else
                            checkForAggro(mob);   //normal aggro
                    }
                }
            }

            //check if mob can move for patrol or moving to target

            if (mob.behaviourType.canRoam)
                checkMobMovement(mob);

            //check if mob can attack if it isn't wimpy

            if (!mob.behaviourType.isWimpy && mob.getCombatTarget() != null)
                checkForAttack(mob);

        } catch (Exception e) {
            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: DefaultLogic" + " " + e.getMessage());
        }
    }

    public static void checkForPlayerGuardAggro(Mob mob) {

        try {

            //looks for and sets mobs combatTarget

            if (!mob.isAlive())
                return;

            // Defer to captain if possible for current target

            if (mob.agentType.equals(mbEnums.AIAgentType.GUARDMINION) && mob.guardCaptain.isAlive() && mob.guardCaptain.combatTarget != null) {
                mob.setCombatTarget(mob.guardCaptain.combatTarget);
                return;
            }

            ConcurrentHashMap<Integer, Float> loadedPlayers = mob.playerAgroMap;

            for (Entry<Integer, Float> playerEntry : loadedPlayers.entrySet()) {

                int playerID = playerEntry.getKey();
                PlayerCharacter loadedPlayer = PlayerCharacter.getPlayerCharacter(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))
                    continue;

                if (MovementUtilities.inRangeToAggro(mob, loadedPlayer) && mob.getCombatTarget() == null) {
                    mob.setCombatTarget(loadedPlayer);
                    return;
                }
            }
            if (mob.getCombatTarget() == null) {

                //look for siege equipment to aggro if no players found to aggro

                HashSet<AbstractWorldObject> awoList = WorldGrid.getObjectsInRangePartial(mob, MobAIThread.AI_BASE_AGGRO_RANGE, MBServerStatics.MASK_SIEGE);

                for (AbstractWorldObject awoMob : awoList) {


                    Mob aggroMob = (Mob) awoMob;
                    if (guardCanAggro(mob, aggroMob)) {
                        mob.setCombatTarget(aggroMob);
                        return;
                    }
                }

            }
        } catch (Exception e) {
            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: CheckForPlayerGuardAggro" + e.getMessage());
        }
    }

    public static Boolean guardCanAggro(Mob mob, AbstractCharacter target) {

        try {

            if (ConfigManager.MB_RULESET.getValue().equals("LORE") && !target.guild.equals(Guild.getErrantGuild())) {
                if (mob.guild.charter.equals(target.guild.charter))
                    return false;
            }

            if (mob.guardedCity != null && mob.guardedCity.cityOutlaws.contains(target.getObjectUUID()))
                return true;

            if (mob.getGuild().getNation().equals(target.getGuild().getNation()))
                return false;

            //first check condemn list for aggro allowed (allies button is checked)

            if (Objects.requireNonNull(ZoneManager.getCityAtLocation(mob.getLoc())).getTOL().reverseKOS) {
                for (Entry<Integer, Condemned> entry : Objects.requireNonNull(ZoneManager.getCityAtLocation(mob.getLoc())).getTOL().getCondemned().entrySet()) {

                    //target is listed individually

                    if (entry.getValue().playerUID == target.getObjectUUID() && entry.getValue().active)
                        return false;

                    //target's guild is listed

                    if (Guild.getGuild(entry.getValue().guildUID) == target.getGuild())
                        return false;

                    //target's nation is listed

                    if (Guild.getGuild(entry.getValue().guildUID) == target.getGuild().getNation())
                        return false;
                }
                return true;
            } else {

                //allies button is not checked

                for (Entry<Integer, Condemned> entry : Objects.requireNonNull(ZoneManager.getCityAtLocation(mob.getLoc())).getTOL().getCondemned().entrySet()) {

                    //target is listed individually

                    if (entry.getValue().playerUID == target.getObjectUUID() && entry.getValue().active)
                        return true;

                    //target's guild is listed

                    if (Guild.getGuild(entry.getValue().guildUID) == target.getGuild())
                        return true;

                    //target's nation is listed

                    if (Guild.getGuild(entry.getValue().guildUID) == target.getGuild().getNation())
                        return true;
                }
            }
        } catch (Exception e) {
            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: GuardCanAggro" + " " + e.getMessage());
        }
        return false;
    }

    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()) {
                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.agentType.equals(mbEnums.AIAgentType.GUARDCAPTAIN)) {
                for (Integer minionUUID : mob.minions) {

                    Mob minion = Mob.getMob(minionUUID);

                    //make sure mob is out of combat stance

                    assert minion != null;
                    if (!minion.despawned) {
                        if (MovementUtilities.canMove(minion)) {
                            Vector3f minionOffset = mbEnums.FormationType.getOffset(2, mob.minions.indexOf(minionUUID) + 3);
                            minion.updateLocation();
                            Vector3fImmutable formationPatrolPoint = new Vector3fImmutable(mob.destination.x + minionOffset.x, mob.destination.y, mob.destination.z + minionOffset.z);
                            MovementUtilities.aiMove(minion, formationPatrolPoint, true);
                        }
                    }
                }
            }
        } catch (Exception e) {
            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: randomGuardPatrolPoints" + " " + e);
        }
    }

    public static AbstractWorldObject changeTargetFromHateValue(Mob mob) {

        try {

            float CurrentHateValue = 0;

            if (mob.getCombatTarget() != null && mob.getCombatTarget().getObjectType().equals(mbEnums.GameObjectType.PlayerCharacter))
                CurrentHateValue = mob.playerAgroMap.get(mob.combatTarget.getObjectUUID());

            AbstractWorldObject mostHatedTarget = null;

            for (Entry<Integer, Float> playerEntry : mob.playerAgroMap.entrySet()) {

                PlayerCharacter potentialTarget = PlayerCharacter.getPlayerCharacter(playerEntry.getKey());

                if (potentialTarget.equals(mob.getCombatTarget()))
                    continue;

                if (ConfigManager.MB_RULESET.getValue().equals("LORE") && !potentialTarget.guild.equals(Guild.getErrantGuild())) {
                    if (mob.guild.charter.equals(potentialTarget.guild.charter))
                        continue;
                }

                if (mob.playerAgroMap.get(potentialTarget.getObjectUUID()) > CurrentHateValue && MovementUtilities.inRangeToAggro(mob, potentialTarget)) {
                    CurrentHateValue = mob.playerAgroMap.get(potentialTarget.getObjectUUID());
                    mostHatedTarget = potentialTarget;
                }

            }
            return mostHatedTarget;
        } catch (Exception e) {
            Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: ChangeTargetFromMostHated" + " " + e.getMessage());
        }
        return null;
    }
}