Files
BattleBane/src/engine/mobileAI/MobAI.java
T

1274 lines
47 KiB
Java
Raw Normal View History

2022-04-30 09:41:17 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
2023-08-02 09:10:51 -04:00
2023-08-01 20:13:23 -05:00
package engine.mobileAI;
2023-07-15 09:23:48 -04:00
2024-05-17 19:41:26 -05:00
import engine.InterestManagement.RealmMap;
import engine.InterestManagement.WorldGrid;
import engine.gameManager.*;
2023-04-17 20:51:34 -05:00
import engine.math.Vector3f;
import engine.math.Vector3fImmutable;
import engine.mbEnums;
import engine.mbEnums.DispatchChannel;
2023-08-02 09:10:51 -04:00
import engine.mobileAI.Threads.MobAIThread;
2024-05-04 13:18:34 -04:00
import engine.mobileAI.Threads.ReSpawner;
2023-08-02 09:10:51 -04:00
import engine.mobileAI.utilities.MovementUtilities;
2022-04-30 09:41:17 -04:00
import engine.net.client.msg.PerformActionMsg;
import engine.net.client.msg.PowerProjectileMsg;
import engine.objects.*;
import engine.powers.ActionsBase;
2022-04-30 09:41:17 -04:00
import engine.powers.PowersBase;
import engine.powers.RunePowerEntry;
2022-04-30 09:41:17 -04:00
import engine.server.MBServerStatics;
2023-08-01 18:52:44 -05:00
import org.pmw.tinylog.Logger;
import java.util.ArrayList;
import java.util.HashSet;
2022-04-30 09:41:17 -04:00
import java.util.Map.Entry;
2024-05-05 19:45:46 -05:00
import java.util.Objects;
2022-04-30 09:41:17 -04:00
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
2023-07-15 09:23:48 -04:00
2022-04-30 09:41:17 -04:00
import static engine.math.FastMath.sqr;
2023-07-15 09:23:48 -04:00
2023-08-01 20:13:23 -05:00
public class MobAI {
2023-07-27 21:22:20 -05:00
2024-04-28 11:44:28 -05:00
// 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
//
2024-05-04 12:41:45 -04:00
// CombatManager.class implements shared combat routines for all avatars.
2023-07-27 21:22:20 -05:00
2024-04-26 08:14:16 -04:00
private static void attackTarget(Mob mob, AbstractWorldObject target) {
2023-08-02 09:10:51 -04:00
2023-08-01 18:52:44 -05:00
try {
2023-08-02 09:10:51 -04:00
2023-08-01 18:52:44 -05:00
if (mob == null)
return;
2023-08-02 09:10:51 -04:00
2023-08-01 18:52:44 -05:00
if (target == null || !target.isAlive()) {
mob.setCombatTarget(null);
2023-06-25 13:36:24 -05:00
return;
}
2023-08-02 09:10:51 -04:00
2024-05-04 12:41:45 -04:00
if (target.getObjectType().equals(mbEnums.GameObjectType.PlayerCharacter) && !mob.canSee((AbstractCharacter) target)) {
2023-09-02 16:04:40 -04:00
mob.setCombatTarget(null);
return;
}
if (target.getObjectType() == mbEnums.GameObjectType.PlayerCharacter && canCast(mob)) {
2023-08-18 11:33:27 -04:00
2024-04-26 08:14:16 -04:00
if (mobCast(mob)) {
2023-08-01 18:52:44 -05:00
mob.updateLocation();
return;
}
2023-08-18 11:33:27 -04:00
2023-08-05 23:10:06 -05:00
}
2023-08-18 11:33:27 -04:00
2024-03-17 15:56:47 -05:00
if (mob.getRange() * mob.getRange() < mob.loc.distanceSquared(target.loc))
2023-08-01 18:52:44 -05:00
return;
2023-08-02 09:10:51 -04:00
2023-08-01 18:52:44 -05:00
switch (target.getObjectType()) {
case PlayerCharacter:
PlayerCharacter targetPlayer = (PlayerCharacter) target;
2024-04-26 08:14:16 -04:00
attackPlayer(mob, targetPlayer);
2023-08-01 18:52:44 -05:00
break;
case Building:
Building targetBuilding = (Building) target;
2024-04-26 08:14:16 -04:00
attackBuilding(mob, targetBuilding);
2023-08-01 18:52:44 -05:00
break;
case Mob:
Mob targetMob = (Mob) target;
2024-04-26 08:14:16 -04:00
attackMob(mob, targetMob);
2023-08-01 18:52:44 -05:00
break;
}
2023-08-02 09:10:51 -04:00
2023-08-01 18:52:44 -05:00
mob.updateLocation();
2023-08-02 09:10:51 -04:00
} catch (Exception e) {
2023-08-01 18:52:44 -05:00
Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: AttackTarget" + " " + e.getMessage());
2023-06-25 13:36:24 -05:00
}
2022-04-30 09:41:17 -04:00
}
2023-07-15 09:23:48 -04:00
2024-04-26 08:14:16 -04:00
public static void attackPlayer(Mob mob, PlayerCharacter target) {
2023-08-02 09:10:51 -04:00
try {
2024-06-11 13:40:42 -04:00
if (target == null || !target.isAlive() || !target.isActive()) {
2024-05-16 19:41:06 -05:00
mob.setCombatTarget(null);
return;
}
2023-08-02 09:10:51 -04:00
if (!mob.canSee(target)) {
mob.setCombatTarget(null);
return;
}
2023-08-25 15:14:33 -04:00
if (mob.behaviourType.callsForHelp)
2024-04-26 08:14:16 -04:00
mobCallForHelp(mob);
2023-08-02 09:10:51 -04:00
if (!MovementUtilities.inRangeDropAggro(mob, target)) {
mob.setCombatTarget(null);
return;
2023-08-02 09:10:51 -04:00
}
2024-03-17 15:56:47 -05:00
if (mob.getRange() * mob.getRange() >= mob.loc.distanceSquared(target.loc)) {
2023-08-02 09:10:51 -04:00
// ranged mobs cant attack while running. skip until they finally stop.
if (mob.isMoving() && mob.getRange() > 20)
return;
2024-05-16 19:41:06 -05:00
CombatManager.combatCycle(mob, target);
2023-08-02 09:10:51 -04:00
}
if (target.getPet() != null)
2024-05-04 12:41:45 -04:00
if (target.getPet().getCombatTarget() == null && target.getPet().assist)
2023-08-02 09:10:51 -04:00
target.getPet().setCombatTarget(mob);
} catch (Exception e) {
Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: AttackPlayer" + " " + e.getMessage());
}
}
2024-04-26 08:14:16 -04:00
public static void attackBuilding(Mob mob, Building target) {
2023-08-02 09:10:51 -04:00
try {
if (target.getRank() == -1 || !target.isVulnerable() || BuildingManager.getBuildingFromCache(target.getObjectUUID()) == null) {
mob.setCombatTarget(null);
2022-04-30 09:41:17 -04:00
return;
2023-08-02 09:10:51 -04:00
}
City playercity = ZoneManager.getCityAtLocation(mob.getLoc());
if (playercity != null)
for (Mob guard : playercity.getParent().zoneMobSet)
if (guard.agentType.equals(mbEnums.AIAgentType.GUARDCAPTAIN))
2023-08-02 09:10:51 -04:00
if (guard.getCombatTarget() == null && !guard.getGuild().equals(mob.getGuild()))
guard.setCombatTarget(mob);
if (mob.isSiege())
MovementManager.sendRWSSMsg(mob);
2024-04-01 17:00:14 -04:00
CombatManager.combatCycle(mob, target);
2023-08-02 09:10:51 -04:00
if (mob.isSiege()) {
PowerProjectileMsg ppm = new PowerProjectileMsg(mob, target);
ppm.setRange(50);
DispatchManager.dispatchMsgToInterestArea(mob, ppm, DispatchChannel.SECONDARY, MBServerStatics.CHARACTER_LOAD_RANGE, false, false);
2023-07-18 20:01:58 -05:00
}
2023-07-15 09:23:48 -04:00
2023-08-02 09:10:51 -04:00
} catch (Exception e) {
2023-08-01 18:52:44 -05:00
Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: AttackBuilding" + " " + e.getMessage());
}
2023-04-20 21:23:42 -05:00
}
2023-07-15 09:23:48 -04:00
2024-04-26 08:14:16 -04:00
public static void attackMob(Mob mob, Mob target) {
2023-08-02 09:10:51 -04:00
try {
2024-06-11 13:40:42 -04:00
if (mob == null || target == null)
2024-05-31 18:32:53 -05:00
return;
2023-08-02 09:10:51 -04:00
if (mob.getRange() >= 30 && mob.isMoving())
return;
//no weapons, default mob attack speed 3 seconds.
2024-04-01 17:00:14 -04:00
CombatManager.combatCycle(mob, target);
2024-03-27 16:53:44 -05:00
2023-08-02 09:10:51 -04:00
} catch (Exception e) {
2023-08-01 18:52:44 -05:00
Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: AttackMob" + " " + e.getMessage());
}
}
2023-07-15 09:23:48 -04:00
2024-04-26 08:14:16 -04:00
private static void patrol(Mob mob) {
2023-08-02 09:10:51 -04:00
try {
int patrolDelay = ThreadLocalRandom.current().nextInt((int) (MobAIThread.AI_PATROL_DIVISOR * 0.5f), MobAIThread.AI_PATROL_DIVISOR) + MobAIThread.AI_PATROL_DIVISOR;
2023-09-07 13:09:25 -04:00
// early exit while waiting to patrol again.
// Minions are force marched if captain is alive
2024-05-04 12:41:45 -04:00
boolean forced = mob.agentType.equals(mbEnums.AIAgentType.GUARDMINION) && mob.guardCaptain.isAlive();
2023-08-02 09:10:51 -04:00
2024-05-04 12:41:45 -04:00
if (mob.stopPatrolTime + (patrolDelay * 1000L) > System.currentTimeMillis())
2023-09-07 13:09:25 -04:00
if (!forced)
return;
2023-08-02 09:10:51 -04:00
//guards inherit barracks patrol points dynamically
2023-08-02 09:10:51 -04:00
2023-09-09 09:11:43 -04:00
if (mob.patrolPoints == null || mob.patrolPoints.isEmpty())
if (mob.agentType.equals(mbEnums.AIAgentType.GUARDCAPTAIN) || mob.agentType.equals(mbEnums.AIAgentType.GUARDMINION)) {
2023-08-18 11:33:27 -04:00
2023-09-09 09:11:43 -04:00
Building barracks = mob.building;
2023-08-18 11:33:27 -04:00
2023-09-09 09:11:43 -04:00
if (barracks != null && barracks.patrolPoints != null && !barracks.getPatrolPoints().isEmpty()) {
mob.patrolPoints = barracks.patrolPoints;
} else {
randomGuardPatrolPoint(mob);
return;
}
2023-08-02 09:10:51 -04:00
}
2024-05-05 19:45:46 -05:00
assert mob.patrolPoints != null;
2023-08-02 09:10:51 -04:00
if (mob.lastPatrolPointIndex > mob.patrolPoints.size() - 1)
mob.lastPatrolPointIndex = 0;
2023-09-07 12:07:39 -04:00
// Minions are given marching orders by the captain if he is alive
if (mob.agentType.equals(mbEnums.AIAgentType.GUARDMINION)) {
2023-09-07 12:07:39 -04:00
Mob captain = (Mob) mob.guardCaptain;
2024-04-05 19:37:45 -04:00
mob.destination = captain.destination.add(mbEnums.FormationType.getOffset(2, mob.guardCaptain.minions.indexOf(mob.getObjectUUID()) + 3));
2023-09-07 12:07:39 -04:00
mob.lastPatrolPointIndex = captain.lastPatrolPointIndex;
} else {
mob.destination = mob.patrolPoints.get(mob.lastPatrolPointIndex);
2023-09-07 12:07:39 -04:00
mob.lastPatrolPointIndex += 1;
}
2023-08-02 09:10:51 -04:00
2023-09-07 12:41:20 -04:00
// Captain orders minions to patrol
if (mob.agentType.equals(mbEnums.AIAgentType.GUARDCAPTAIN))
2023-09-07 12:41:20 -04:00
for (Integer minionUUID : mob.minions) {
Mob minion = Mob.getMob(minionUUID);
2024-05-05 19:45:46 -05:00
assert minion != null;
2023-09-07 12:42:43 -04:00
if (minion.isAlive() && minion.combatTarget == null)
2024-04-26 08:14:16 -04:00
MobAI.patrol(minion);
2023-09-07 12:41:20 -04:00
}
2023-08-02 09:10:51 -04:00
MovementUtilities.aiMove(mob, mob.destination, true);
} catch (Exception e) {
2023-08-01 18:52:44 -05:00
Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: AttackTarget" + " " + e.getMessage());
}
2023-04-16 12:48:23 -05:00
}
2023-07-15 09:23:48 -04:00
2023-02-04 21:26:04 -06:00
public static boolean canCast(Mob mob) {
2023-09-02 15:33:12 -04:00
int contractID = 0;
2023-08-02 09:10:51 -04:00
try {
// Performs validation to determine if a
// mobile in the proper state to cast.
if (mob == null)
return false;
2024-05-04 12:41:45 -04:00
if (mob.isPlayerGuard()) {
2023-08-18 11:33:27 -04:00
if (mob.agentType.equals(mbEnums.AIAgentType.GUARDWALLARCHER))
2023-09-09 09:52:47 -05:00
return false; //wall archers don't cast
if (mob.agentType.equals(mbEnums.AIAgentType.GUARDMINION))
2023-08-26 11:55:18 -04:00
contractID = mob.guardCaptain.contract.getContractID();
2023-08-25 15:14:33 -04:00
else
2023-08-06 00:04:42 -05:00
contractID = mob.contract.getContractID();
2023-08-18 11:33:27 -04:00
2023-09-02 15:33:12 -04:00
// exception allowing werewolf and werebear guard captains to cast
2023-09-02 15:30:10 -04:00
2024-05-04 12:41:45 -04:00
if (!mbEnums.MinionType.ContractToMinionMap.get(contractID).isMage() && contractID != 980103 && contractID != 980104)
2023-08-06 00:04:42 -05:00
return false;
}
2023-09-02 15:25:19 -04:00
// Mobile has no powers defined in mobbase or contract..
2024-05-04 12:41:45 -04:00
if (PowersManager.getPowersForRune(mob.getMobBaseID()).isEmpty() && PowersManager.getPowersForRune(contractID).isEmpty())
2023-09-02 15:25:19 -04:00
return false;
2023-08-02 09:10:51 -04:00
if (mob.nextCastTime == 0)
mob.nextCastTime = System.currentTimeMillis();
return mob.nextCastTime <= System.currentTimeMillis();
} catch (Exception e) {
2023-08-01 18:52:44 -05:00
Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: canCast" + " " + e.getMessage());
}
return false;
2023-02-02 19:44:23 -06:00
}
2023-07-15 09:23:48 -04:00
2024-04-26 08:14:16 -04:00
public static boolean mobCast(Mob mob) {
2023-08-02 09:10:51 -04:00
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;
2023-08-05 22:54:41 -05:00
AbstractCharacter target = (AbstractCharacter) mob.getCombatTarget();
2023-08-02 09:10:51 -04:00
2023-08-25 15:14:33 -04:00
if (mob.behaviourType.callsForHelp)
2024-04-26 08:14:16 -04:00
mobCallForHelp(mob);
2023-08-02 09:10:51 -04:00
// Generate a list of tokens from the mob powers for this mobile.
powerEntries = new ArrayList<>(PowersManager.getPowersForRune(mob.getMobBaseID()));
2023-08-31 13:42:15 -04:00
purgeEntries = new ArrayList<>();
2023-08-02 09:10:51 -04:00
2023-09-02 14:04:47 -04:00
// Additional powers may come from the contract ID. This is to support
// powers for player guards irrespective of the mobbase used.
2023-09-02 14:04:47 -04:00
if (mob.isPlayerGuard()) {
ArrayList<RunePowerEntry> contractEntries = new ArrayList<>();
if (mob.contract != null)
2023-09-02 15:30:10 -04:00
contractEntries = PowersManager.getPowersForRune(mob.contractUUID);
if (mob.agentType.equals(mbEnums.AIAgentType.GUARDMINION))
2023-09-02 15:30:10 -04:00
contractEntries = PowersManager.getPowersForRune(mob.guardCaptain.contractUUID);
2023-09-02 14:04:47 -04:00
powerEntries.addAll(contractEntries);
}
2023-08-02 09:10:51 -04:00
// If player has this effect on them currently then remove
// this token from our list.
for (RunePowerEntry runePowerEntry : powerEntries) {
2023-08-02 09:10:51 -04:00
PowersBase powerBase = PowersManager.getPowerByToken(runePowerEntry.token);
2023-08-02 09:10:51 -04:00
for (ActionsBase actionBase : powerBase.getActions()) {
String stackType = actionBase.stackType;
if (target.getEffects() != null && target.getEffects().containsKey(stackType))
purgeEntries.add(runePowerEntry);
2023-08-02 09:10:51 -04:00
}
}
2023-08-02 09:10:51 -04:00
2023-08-31 13:42:15 -04:00
powerEntries.removeAll(purgeEntries);
2023-08-02 09:10:51 -04:00
// Sanity check
2023-08-31 13:42:15 -04:00
if (powerEntries.isEmpty())
return false;
2023-08-02 09:10:51 -04:00
// Pick random spell from our list of powers
2023-09-05 14:25:24 -04:00
RunePowerEntry runePowerEntry = powerEntries.get(ThreadLocalRandom.current().nextInt(powerEntries.size()));
PowersBase mobPower = PowersManager.getPowerByToken(runePowerEntry.token);
int powerRank = runePowerEntry.rank;
2023-08-02 09:10:51 -04:00
2023-09-05 14:25:24 -04:00
if (mob.isPlayerGuard())
powerRank = getGuardPowerRank(mob);
2023-08-02 09:10:51 -04:00
// Cast the spell
2024-03-17 15:56:47 -05:00
if (mob.getRange() * mob.getRange() >= mob.loc.distanceSquared(target.loc)) {
2023-08-05 23:10:06 -05:00
PerformActionMsg msg;
if (!mobPower.isHarmful() || mobPower.targetSelf) {
PowersManager.useMobPower(mob, mob, mobPower, powerRank);
msg = PowersManager.createPowerMsg(mobPower, powerRank, mob, mob);
2023-08-18 11:33:27 -04:00
} else {
2023-08-05 23:10:06 -05:00
PowersManager.useMobPower(mob, target, mobPower, powerRank);
msg = PowersManager.createPowerMsg(mobPower, powerRank, mob, target);
}
msg.setUnknown04(2);
PowersManager.finishUseMobPower(msg, mob, 0, 0);
2024-05-04 12:41:45 -04:00
long randomCooldown = (long) ((ThreadLocalRandom.current().nextInt(10, 15) * 1000L) * MobAIThread.AI_CAST_FREQUENCY);
2023-08-18 11:33:27 -04:00
2023-08-08 10:54:36 -05:00
mob.nextCastTime = System.currentTimeMillis() + randomCooldown;
2023-08-05 23:10:06 -05:00
return true;
}
} catch (Exception e) {
Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: MobCast" + " " + e.getMessage());
}
return false;
}
2023-08-06 17:54:01 -05:00
2023-09-09 19:23:46 -05:00
public static int getGuardPowerRank(Mob mob) {
2023-09-05 14:25:24 -04:00
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;
2023-08-01 18:52:44 -05:00
}
2023-09-05 14:25:24 -04:00
return powerRank;
}
2023-07-15 09:23:48 -04:00
2024-04-26 08:14:16 -04:00
public static void mobCallForHelp(Mob mob) {
2023-08-02 09:10:51 -04:00
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!");
2024-05-11 07:51:56 -04:00
Zone mobCamp = mob.parentZone;
2023-08-02 09:10:51 -04:00
for (Mob helper : mobCamp.zoneMobSet) {
2023-08-25 15:14:33 -04:00
if (helper.behaviourType.respondsToCallForHelp && helper.behaviourType.BehaviourHelperType.equals(mob.behaviourType)) {
2023-08-02 09:10:51 -04:00
helper.setCombatTarget(mob.getCombatTarget());
callGotResponse = true;
}
2023-02-02 19:44:23 -06:00
}
2023-08-02 09:10:51 -04:00
//wait 60 seconds to call for help again
2023-08-02 09:10:51 -04:00
if (callGotResponse)
mob.nextCallForHelp = System.currentTimeMillis() + 60000;
} catch (Exception e) {
2023-08-01 18:52:44 -05:00
Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: MobCallForHelp" + " " + e.getMessage());
}
2023-02-02 19:44:23 -06:00
}
2023-07-15 09:23:48 -04:00
2024-04-26 08:14:16 -04:00
public static void determineAction(Mob mob) {
2023-07-27 21:22:20 -05:00
2023-08-02 09:10:51 -04:00
try {
//always check the respawn que, respawn 1 mob max per second to not flood the client
if (mob == null)
return;
2024-05-04 12:41:45 -04:00
if (!mob.getTimestamps().containsKey("lastExecution"))
2023-08-02 09:10:51 -04:00
mob.getTimestamps().put("lastExecution", System.currentTimeMillis());
if (System.currentTimeMillis() < mob.getTimeStamp("lastExecution"))
return;
mob.getTimestamps().put("lastExecution", System.currentTimeMillis() + MobAIThread.AI_PULSE_MOB_THRESHOLD);
2023-04-22 21:55:06 -05:00
//trebuchet spawn handler
2023-08-02 09:10:51 -04:00
if (mob.despawned && mob.getMobBase().getLoadID() == 13171) {
2024-04-26 08:14:16 -04:00
checkForRespawn(mob);
2023-08-02 09:10:51 -04:00
return;
}
2023-04-22 21:55:06 -05:00
//override for guards
2023-08-02 09:10:51 -04:00
if (mob.despawned && mob.isPlayerGuard()) {
2023-08-02 09:10:51 -04:00
if (mob.agentType.equals(mbEnums.AIAgentType.GUARDMINION)) {
2024-05-04 12:41:45 -04:00
if (!mob.guardCaptain.isAlive() || ((Mob) mob.guardCaptain).despawned) {
2023-08-02 09:10:51 -04:00
//minions don't respawn while guard captain is dead
2024-05-04 12:41:45 -04:00
if (!mob.isAlive()) {
2023-08-02 09:10:51 -04:00
mob.deathTime = System.currentTimeMillis();
return;
}
2023-05-07 11:53:00 -05:00
}
}
2023-08-02 09:10:51 -04:00
2024-04-26 08:14:16 -04:00
checkForRespawn(mob);
2023-08-02 09:10:51 -04:00
//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))
2024-04-26 08:14:16 -04:00
checkToSendMobHome(mob);
2023-08-02 09:10:51 -04:00
return;
}
2023-08-02 09:10:51 -04:00
2023-04-20 21:23:42 -05:00
//no need to continue if mob is dead, check for respawn and move on
2023-08-02 09:10:51 -04:00
if (!mob.isAlive()) {
2024-04-26 08:14:16 -04:00
checkForRespawn(mob);
2023-08-02 09:10:51 -04:00
return;
}
//no players loaded, no need to proceed unless it's a player guard
boolean bypassLoadedPlayerCheck = false;
2024-03-24 09:42:27 -04:00
if (mob.isPlayerGuard() || mob.isSiege()) {
bypassLoadedPlayerCheck = true;
if (mob.combatTarget != null && mob.combatTarget.getObjectType().equals(mbEnums.GameObjectType.PlayerCharacter))
2024-03-24 09:42:27 -04:00
if (mob.combatTarget.loc.distanceSquared(mob.loc) > 10000)
mob.setCombatTarget(null);
}
if (mob.playerAgroMap.isEmpty() && !bypassLoadedPlayerCheck) {
2023-09-05 15:56:49 -04:00
if (mob.getCombatTarget() != null)
2023-08-08 13:36:46 -05:00
mob.setCombatTarget(null);
2023-07-27 21:18:52 -05:00
return;
}
2023-08-18 11:33:27 -04:00
2024-05-04 12:41:45 -04:00
if (!mob.agentType.equals(mbEnums.AIAgentType.PET))
2024-04-26 08:14:16 -04:00
checkToSendMobHome(mob);
2023-08-02 09:10:51 -04:00
2023-08-08 13:36:46 -05:00
if (mob.getCombatTarget() != null) {
2023-08-18 11:33:27 -04:00
2024-05-04 12:41:45 -04:00
if (!mob.getCombatTarget().isAlive()) {
2023-07-27 21:18:52 -05:00
mob.setCombatTarget(null);
return;
}
2023-08-02 09:10:51 -04:00
if (mob.getCombatTarget().getObjectTypeMask() == MBServerStatics.MASK_PLAYER) {
2023-08-08 13:36:46 -05:00
PlayerCharacter target = (PlayerCharacter) mob.getCombatTarget();
2023-08-02 09:10:51 -04:00
2024-05-04 12:41:45 -04:00
if (!mob.playerAgroMap.containsKey(target.getObjectUUID())) {
2023-08-02 09:10:51 -04:00
mob.setCombatTarget(null);
return;
}
2024-05-04 12:41:45 -04:00
if (!mob.canSee((PlayerCharacter) mob.getCombatTarget())) {
2023-08-02 09:10:51 -04:00
mob.setCombatTarget(null);
return;
}
2023-07-27 21:18:52 -05:00
}
}
2023-08-02 09:10:51 -04:00
2023-08-25 15:14:33 -04:00
switch (mob.behaviourType) {
2023-08-02 09:10:51 -04:00
case GuardCaptain:
case GuardMinion:
case GuardWallArcher:
2024-04-26 08:14:16 -04:00
guardLogic(mob);
2023-08-02 09:10:51 -04:00
break;
2023-08-25 12:32:36 -04:00
case Pet1:
2023-08-27 20:05:18 -05:00
case SiegeEngine:
2024-04-26 08:14:16 -04:00
petLogic(mob);
2023-08-02 09:10:51 -04:00
break;
case HamletGuard:
2024-04-26 08:14:16 -04:00
hamletGuardLogic(mob);
2023-08-02 09:10:51 -04:00
break;
default:
2024-04-26 08:14:16 -04:00
defaultLogic(mob);
2023-08-02 09:10:51 -04:00
break;
}
} catch (Exception e) {
2023-08-01 18:52:44 -05:00
Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: DetermineAction" + " " + e.getMessage());
}
}
2023-07-15 09:23:48 -04:00
2024-04-26 08:14:16 -04:00
private static void checkForAggro(Mob aiAgent) {
2023-08-02 09:10:51 -04:00
try {
//looks for and sets mobs combatTarget
if (!aiAgent.isAlive())
return;
2023-08-02 09:10:51 -04:00
2023-08-29 21:01:00 -05:00
ConcurrentHashMap<Integer, Float> loadedPlayers = aiAgent.playerAgroMap;
2023-08-02 09:10:51 -04:00
2024-05-04 12:53:06 -04:00
for (Entry<Integer, Float> playerEntry : loadedPlayers.entrySet()) {
2023-08-02 09:10:51 -04:00
2024-05-04 12:53:06 -04:00
int playerID = playerEntry.getKey();
2023-10-20 09:48:24 -04:00
PlayerCharacter loadedPlayer = PlayerCharacter.getPlayerCharacter(playerID);
2023-08-02 09:10:51 -04:00
//Player is null, let's remove them from the list.
if (loadedPlayer == null) {
loadedPlayers.remove(playerID);
2023-07-18 20:01:58 -05:00
continue;
2023-08-02 09:10:51 -04:00
}
//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
2024-05-04 12:53:06 -04:00
if (!aiAgent.notEnemy.isEmpty() && aiAgent.notEnemy.contains(loadedPlayer.race.getRaceType().getMonsterType()))
2023-08-02 09:10:51 -04:00
continue;
//mob has enemies and this player race is not it
2024-05-04 12:53:06 -04:00
if (!aiAgent.enemy.isEmpty() && !aiAgent.enemy.contains(loadedPlayer.race.getRaceType().getMonsterType()))
2023-08-02 09:10:51 -04:00
continue;
if (MovementUtilities.inRangeToAggro(aiAgent, loadedPlayer)) {
aiAgent.setCombatTarget(loadedPlayer);
return;
}
2023-07-18 20:01:58 -05:00
}
2023-08-02 09:10:51 -04:00
2023-08-08 13:36:46 -05:00
if (aiAgent.getCombatTarget() == null) {
2023-08-02 09:10:51 -04:00
//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) {
2023-08-18 11:33:27 -04:00
// exclude self.
2023-08-02 09:10:51 -04:00
if (aiAgent.equals(awoMob))
continue;
Mob aggroMob = (Mob) awoMob;
aiAgent.setCombatTarget(aggroMob);
return;
}
}
} catch (Exception e) {
2023-08-01 18:52:44 -05:00
Logger.info(aiAgent.getObjectUUID() + " " + aiAgent.getName() + " Failed At: CheckForAggro" + " " + e.getMessage());
}
}
2023-07-15 09:23:48 -04:00
2024-04-26 08:14:16 -04:00
private static void checkMobMovement(Mob mob) {
2023-08-02 09:10:51 -04:00
try {
if (!MovementUtilities.canMove(mob))
return;
mob.updateLocation();
2024-05-05 19:45:46 -05:00
if (mob.behaviourType == mbEnums.MobBehaviourType.Pet1) {
if (mob.guardCaptain == null)
return;
2023-08-02 09:10:51 -04:00
2024-05-05 19:45:46 -05:00
//mob no longer has its owner loaded, translate pet to owner
2023-08-27 21:04:34 -05:00
2024-05-05 19:45:46 -05:00
if (!mob.playerAgroMap.containsKey(mob.guardCaptain.getObjectUUID())) {
MovementManager.translocate(mob, mob.guardCaptain.getLoc());
return;
}
if (mob.getCombatTarget() == null) {
2023-08-02 09:10:51 -04:00
2024-05-05 19:45:46 -05:00
//move back to owner
2023-08-02 09:10:51 -04:00
2024-05-05 19:45:46 -05:00
if (mob.getRange() * mob.getRange() >= mob.loc.distanceSquared(mob.guardCaptain.loc))
2023-08-02 09:10:51 -04:00
return;
2023-09-07 12:24:45 -04:00
2024-05-05 19:45:46 -05:00
mob.destination = mob.guardCaptain.getLoc();
MovementUtilities.moveToLocation(mob, mob.destination, 5, false);
} else
chaseTarget(mob);
} else {
if (mob.getCombatTarget() == null) {
2023-08-02 09:10:51 -04:00
2024-05-05 19:45:46 -05:00
if (!mob.isMoving()) {
2023-08-02 09:10:51 -04:00
2024-05-05 19:45:46 -05:00
// Minions only patrol on their own if captain is dead.
2023-08-02 09:10:51 -04:00
2024-05-05 19:45:46 -05:00
if (!mob.agentType.equals(mbEnums.AIAgentType.GUARDMINION))
patrol(mob);
else if (!mob.guardCaptain.isAlive())
patrol(mob);
2023-08-02 09:10:51 -04:00
} else
2024-05-05 19:45:46 -05:00
mob.stopPatrolTime = System.currentTimeMillis();
} else {
chaseTarget(mob);
}
2023-08-02 09:10:51 -04:00
}
} catch (Exception e) {
2023-08-01 18:52:44 -05:00
Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: CheckMobMovement" + " " + e.getMessage());
}
}
2023-07-15 09:23:48 -04:00
2024-04-26 08:14:16 -04:00
private static void checkForRespawn(Mob aiAgent) {
2023-08-02 09:10:51 -04:00
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) {
2024-03-18 10:01:29 -04:00
if (aiAgent.charItemManager.getInventoryCount() > 0) {
2023-08-02 09:10:51 -04:00
if (System.currentTimeMillis() > aiAgent.deathTime + MBServerStatics.DESPAWN_TIMER_WITH_LOOT) {
2023-04-21 15:17:37 -05:00
aiAgent.despawn();
aiAgent.deathTime = System.currentTimeMillis();
}
2023-08-02 09:10:51 -04:00
//No items in inventory.
2023-04-21 15:17:37 -05:00
} else {
2023-08-02 09:10:51 -04:00
//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();
}
2023-04-21 15:17:37 -05:00
}
}
2023-10-23 14:12:20 -04:00
} else if (System.currentTimeMillis() > aiAgent.deathTime + (aiAgent.spawnDelay * 1000L)) {
2023-10-23 14:00:12 -04:00
aiAgent.respawnTime = aiAgent.deathTime + (aiAgent.spawnDelay * 1000L);
2024-05-04 13:18:34 -04:00
ReSpawner.respawnQueue.put(aiAgent);
}
2023-08-02 09:10:51 -04:00
} catch (Exception e) {
2023-08-01 18:52:44 -05:00
Logger.info(aiAgent.getObjectUUID() + " " + aiAgent.getName() + " Failed At: CheckForRespawn" + " " + e.getMessage());
}
}
2023-07-15 09:23:48 -04:00
2024-04-26 08:14:16 -04:00
public static void checkForAttack(Mob mob) {
2023-08-02 09:10:51 -04:00
try {
//checks if mob can attack based on attack timer and range
2024-05-04 12:41:45 -04:00
if (!mob.isAlive())
2023-08-02 09:10:51 -04:00
return;
if (mob.getCombatTarget() == null)
return;
2024-05-04 12:41:45 -04:00
if (mob.getCombatTarget().getObjectType().equals(mbEnums.GameObjectType.PlayerCharacter) && !MovementUtilities.inRangeDropAggro(mob, (PlayerCharacter) mob.getCombatTarget()) && !mob.agentType.equals(mbEnums.AIAgentType.PET)) {
2023-08-02 09:10:51 -04:00
mob.setCombatTarget(null);
return;
}
2024-04-26 08:14:16 -04:00
attackTarget(mob, mob.getCombatTarget());
2024-03-28 22:17:08 -05:00
2023-08-02 09:10:51 -04:00
} catch (Exception e) {
2023-08-01 18:52:44 -05:00
Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: CheckForAttack" + " " + e.getMessage());
}
}
2023-07-15 09:23:48 -04:00
2024-04-26 08:14:16 -04:00
private static void checkToSendMobHome(Mob mob) {
2023-08-02 09:10:51 -04:00
try {
2024-03-24 09:42:27 -04:00
if (mob.getCombatTarget() != null && mob.getRange() * mob.getRange() >= MobAIThread.AI_BASE_AGGRO_RANGE * 0.5f)
2023-08-02 09:10:51 -04:00
return;
if (mob.isPlayerGuard() && !mob.despawned) {
City current = ZoneManager.getCityAtLocation(mob.getLoc());
2024-05-04 12:41:45 -04:00
if (current == null || !current.equals(mob.getGuild().getOwnedCity())) {
2023-08-02 09:10:51 -04:00
PowersBase recall = PowersManager.getPowerByToken(-1994153779);
PowersManager.useMobPower(mob, mob, recall, 40);
mob.setCombatTarget(null);
if (mob.agentType.equals(mbEnums.AIAgentType.GUARDCAPTAIN) && mob.isAlive()) {
2023-08-02 09:10:51 -04:00
//guard captain pulls his minions home with him
for (Integer minionUUID : mob.minions) {
Mob minion = Mob.getMob(minionUUID);
PowersManager.useMobPower(minion, minion, recall, 40);
2024-05-05 19:45:46 -05:00
assert minion != null;
minion.setCombatTarget(null);
2023-08-02 09:10:51 -04:00
}
}
}
2024-05-04 12:41:45 -04:00
} else if (!MovementUtilities.inRangeOfBindLocation(mob)) {
2023-08-02 09:10:51 -04:00
2023-04-17 21:31:56 -05:00
PowersBase recall = PowersManager.getPowerByToken(-1994153779);
PowersManager.useMobPower(mob, mob, recall, 40);
mob.setCombatTarget(null);
2024-05-04 12:53:06 -04:00
mob.playerAgroMap.replaceAll((e, v) -> 0f);
2023-05-28 10:11:20 -05:00
}
2023-08-02 09:10:51 -04:00
} catch (Exception e) {
2023-08-01 18:52:44 -05:00
Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: CheckToSendMobHome" + " " + e.getMessage());
}
}
2023-07-15 09:23:48 -04:00
2023-04-16 13:30:46 -05:00
private static void chaseTarget(Mob mob) {
2023-08-02 09:10:51 -04:00
try {
2024-05-04 12:41:45 -04:00
if (!mob.getTimestamps().containsKey("lastChase"))
2024-03-24 09:42:27 -04:00
mob.getTimestamps().put("lastChase", System.currentTimeMillis());
2024-05-04 12:53:06 -04:00
else if (System.currentTimeMillis() < mob.getTimestamps().get("lastChase") + (750 + ThreadLocalRandom.current().nextInt(0, 500)))
2024-03-24 09:42:27 -04:00
return;
2023-10-16 14:33:09 -05:00
2024-03-24 09:42:27 -04:00
mob.getTimestamps().put("lastChase", System.currentTimeMillis());
2023-10-16 14:37:17 -05:00
2024-03-17 15:56:47 -05:00
if (mob.getRange() * mob.getRange() <= mob.loc.distanceSquared(mob.combatTarget.loc)) {
2023-08-02 09:10:51 -04:00
if (mob.getRange() > 15) {
mob.destination = mob.getCombatTarget().getLoc();
2023-09-09 09:35:03 -05:00
MovementUtilities.moveToLocation(mob, mob.destination, 0, false);
2023-08-02 09:10:51 -04:00
} else {
//check if building
switch (mob.getCombatTarget().getObjectType()) {
case PlayerCharacter:
case Mob:
mob.destination = MovementUtilities.GetDestinationToCharacter(mob, (AbstractCharacter) mob.getCombatTarget());
2023-09-09 09:35:03 -05:00
MovementUtilities.moveToLocation(mob, mob.destination, mob.getRange() + 1, false);
2023-08-02 09:10:51 -04:00
break;
case Building:
mob.destination = mob.getCombatTarget().getLoc();
2023-09-09 09:35:03 -05:00
MovementUtilities.moveToLocation(mob, mob.getCombatTarget().getLoc(), 0, false);
2023-08-02 09:10:51 -04:00
break;
}
2023-08-01 21:26:14 -05:00
}
2023-04-16 13:30:46 -05:00
}
2023-08-08 14:22:49 -05:00
mob.updateMovementState();
mob.updateLocation();
2023-08-02 09:10:51 -04:00
} catch (Exception e) {
2023-08-01 18:52:44 -05:00
Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: chaseTarget" + " " + e.getMessage());
}
2023-04-16 13:30:46 -05:00
}
2023-07-15 09:23:48 -04:00
2024-04-26 08:14:16 -04:00
private static void safeGuardAggro(Mob mob) {
2023-08-02 09:10:51 -04:00
try {
HashSet<AbstractWorldObject> awoList = WorldGrid.getObjectsInRangePartial(mob, 100, MBServerStatics.MASK_MOB);
for (AbstractWorldObject awoMob : awoList) {
//dont scan self.
2024-01-24 19:03:33 -06:00
if (mob.equals(awoMob))
2023-08-02 09:10:51 -04:00
continue;
Mob aggroMob = (Mob) awoMob;
2023-08-18 11:33:27 -04:00
//don't attack other guards
2024-05-04 12:41:45 -04:00
if (aggroMob.isGuard())
2023-08-02 09:10:51 -04:00
continue;
2023-08-18 11:33:27 -04:00
2024-01-24 19:03:33 -06:00
//don't attack pets
if (aggroMob.agentType.equals(mbEnums.AIAgentType.PET))
2023-08-06 17:11:22 -05:00
continue;
2023-08-18 11:33:27 -04:00
2023-08-02 09:10:51 -04:00
if (mob.getLoc().distanceSquared2D(aggroMob.getLoc()) > sqr(50))
continue;
mob.setCombatTarget(aggroMob);
}
} catch (Exception e) {
2023-08-01 18:52:44 -05:00
Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: SafeGuardAggro" + " " + e.getMessage());
}
2023-04-16 18:58:55 -05:00
}
2023-07-15 09:23:48 -04:00
2024-04-26 08:14:16 -04:00
public static void guardLogic(Mob mob) {
2023-08-02 09:10:51 -04:00
try {
2023-09-09 10:37:40 -05:00
if (mob.getCombatTarget() == null) {
2024-04-26 08:14:16 -04:00
checkForPlayerGuardAggro(mob);
2023-09-09 10:37:40 -05:00
} else {
//do not need to look to change target if target is already null
2024-04-26 08:14:16 -04:00
AbstractWorldObject newTarget = changeTargetFromHateValue(mob);
2023-08-08 19:08:51 -04:00
2023-09-09 10:37:40 -05:00
if (newTarget != null) {
2023-08-08 19:08:51 -04:00
if (newTarget.getObjectType().equals(mbEnums.GameObjectType.PlayerCharacter)) {
2024-04-26 08:14:16 -04:00
if (guardCanAggro(mob, (PlayerCharacter) newTarget))
2023-09-09 10:37:40 -05:00
mob.setCombatTarget(newTarget);
} else
2023-08-08 19:08:51 -04:00
mob.setCombatTarget(newTarget);
2023-09-09 10:37:40 -05:00
}
2023-08-08 19:08:51 -04:00
}
2024-03-24 09:42:27 -04:00
if (mob.behaviourType.canRoam)
2024-04-26 08:14:16 -04:00
checkMobMovement(mob);//all guards that can move check to move
2023-07-15 09:23:48 -04:00
2024-03-24 09:42:27 -04:00
if (mob.combatTarget != null)
2024-04-26 08:14:16 -04:00
checkForAttack(mob); //only check to attack if combat target is not null
2023-08-02 09:10:51 -04:00
} catch (Exception e) {
2023-09-09 10:37:40 -05:00
Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: GuardLogic" + " " + e.getMessage());
2023-08-01 18:52:44 -05:00
}
2023-04-18 20:52:33 -05:00
}
2023-07-15 09:23:48 -04:00
2024-04-26 08:14:16 -04:00
private static void petLogic(Mob mob) {
2023-08-02 09:10:51 -04:00
try {
2023-08-18 11:33:27 -04:00
2024-05-04 12:41:45 -04:00
if (mob.guardCaptain == null && !mob.isNecroPet() && !mob.isSiege())
2023-10-09 06:16:25 -04:00
if (ZoneManager.seaFloor.zoneMobSet.contains(mob))
2023-08-02 09:10:51 -04:00
mob.killCharacter("no owner");
2023-08-25 15:14:33 -04:00
if (MovementUtilities.canMove(mob) && mob.behaviourType.canRoam)
2024-04-26 08:14:16 -04:00
checkMobMovement(mob);
2023-08-02 09:10:51 -04:00
2024-04-26 08:14:16 -04:00
checkForAttack(mob);
2023-08-02 09:10:51 -04:00
//recover health
2024-05-04 12:41:45 -04:00
if (!mob.getTimestamps().containsKey("HEALTHRECOVERED"))
2023-08-02 09:10:51 -04:00
mob.getTimestamps().put("HEALTHRECOVERED", System.currentTimeMillis());
2023-08-18 11:33:27 -04:00
if (mob.isSit() && mob.getTimeStamp("HEALTHRECOVERED") < System.currentTimeMillis() + 3000)
2023-08-02 09:10:51 -04:00
if (mob.getHealth() < mob.getHealthMax()) {
2023-08-18 11:33:27 -04:00
float recoveredHealth = mob.getHealthMax() * ((1 + mob.getBonuses().getFloatPercentAll(mbEnums.ModType.HealthRecoverRate, mbEnums.SourceType.None)) * 0.01f);
2023-08-02 09:10:51 -04:00
mob.setHealth(mob.getHealth() + recoveredHealth);
mob.getTimestamps().put("HEALTHRECOVERED", System.currentTimeMillis());
2023-08-18 11:33:27 -04:00
if (mob.getHealth() > mob.getHealthMax())
2023-08-02 09:10:51 -04:00
mob.setHealth(mob.getHealthMax());
2023-07-18 19:51:00 -05:00
}
2023-08-02 09:10:51 -04:00
} catch (Exception e) {
2023-08-01 18:52:44 -05:00
Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: PetLogic" + " " + e.getMessage());
}
2023-04-20 21:23:42 -05:00
}
2023-07-15 09:23:48 -04:00
2024-04-26 08:14:16 -04:00
private static void hamletGuardLogic(Mob mob) {
2023-08-02 09:10:51 -04:00
try {
2024-05-05 19:45:46 -05:00
if (ConfigManager.MB_RULESET.getValue().equals("LORE")) {
2024-05-14 18:49:30 -05:00
//lore guard aggro to kill members of opposing charters
if (mob.getCombatTarget() == null || !mob.combatTarget.isAlive())
2024-05-05 19:45:46 -05:00
hamletGuardAggro(mob);
else if (!mob.getCombatTarget().isAlive())
hamletGuardAggro(mob);
} else {
//safehold guard
2023-08-02 09:10:51 -04:00
2024-05-14 18:49:30 -05:00
if (mob.getCombatTarget() == null || !mob.combatTarget.isAlive())
2024-05-05 19:45:46 -05:00
safeGuardAggro(mob);
else if (!mob.getCombatTarget().isAlive())
safeGuardAggro(mob);
}
2024-04-26 08:14:16 -04:00
checkForAttack(mob);
2023-08-02 09:10:51 -04:00
} catch (Exception e) {
2023-08-01 18:52:44 -05:00
Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: HamletGuardLogic" + " " + e.getMessage());
}
2023-04-21 10:02:09 -05:00
}
2023-07-15 09:23:48 -04:00
2024-05-05 19:45:46 -05:00
private static void hamletGuardAggro(Mob mob) {
2024-06-11 13:40:42 -04:00
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))
2024-05-17 19:41:26 -05:00
continue;
2024-06-11 13:40:42 -04:00
if (!targetMob.isAlive() || targetMob.despawned)
2024-05-17 19:41:26 -05:00
continue;
2024-06-11 13:40:42 -04:00
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())) {
2024-05-14 18:42:38 -05:00
mob.combatTarget = pc;
2024-05-05 19:45:46 -05:00
return;
}
2024-06-11 13:40:42 -04:00
if (pc.guild.charter.equals(mob.guild.charter))
continue;
mob.combatTarget = pc;
return;
}
2024-05-05 19:45:46 -05:00
}
2024-04-26 08:14:16 -04:00
private static void defaultLogic(Mob mob) {
2023-08-02 09:10:51 -04:00
try {
//check for players that can be aggroed if mob is agressive and has no target
2024-05-04 12:41:45 -04:00
if (mob.getCombatTarget() != null && !mob.playerAgroMap.containsKey(mob.getCombatTarget().getObjectUUID()))
2023-08-02 09:10:51 -04:00
mob.setCombatTarget(null);
2023-08-25 15:14:33 -04:00
if (mob.behaviourType.isAgressive) {
2023-08-02 09:10:51 -04:00
2024-04-26 08:14:16 -04:00
AbstractWorldObject newTarget = changeTargetFromHateValue(mob);
2023-08-02 09:10:51 -04:00
if (newTarget != null)
mob.setCombatTarget(newTarget);
else {
if (mob.getCombatTarget() == null) {
if (mob.behaviourType == mbEnums.MobBehaviourType.HamletGuard)
2024-04-26 08:14:16 -04:00
safeGuardAggro(mob); //safehold guard
2023-08-02 09:10:51 -04:00
else
2024-04-26 08:14:16 -04:00
checkForAggro(mob); //normal aggro
2023-08-02 09:10:51 -04:00
}
2023-05-07 11:53:00 -05:00
}
}
2023-08-02 09:10:51 -04:00
//check if mob can move for patrol or moving to target
2023-08-25 15:14:33 -04:00
if (mob.behaviourType.canRoam)
2024-04-26 08:14:16 -04:00
checkMobMovement(mob);
2023-08-02 09:10:51 -04:00
//check if mob can attack if it isn't wimpy
2023-08-25 15:14:33 -04:00
if (!mob.behaviourType.isWimpy && mob.getCombatTarget() != null)
2024-04-26 08:14:16 -04:00
checkForAttack(mob);
2023-08-02 09:10:51 -04:00
} catch (Exception e) {
2023-08-01 18:52:44 -05:00
Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: DefaultLogic" + " " + e.getMessage());
}
2023-04-20 21:23:42 -05:00
}
2023-07-15 09:23:48 -04:00
2024-04-26 08:14:16 -04:00
public static void checkForPlayerGuardAggro(Mob mob) {
2023-08-02 09:10:51 -04:00
try {
//looks for and sets mobs combatTarget
if (!mob.isAlive())
2023-04-16 21:48:21 -05:00
return;
2023-08-02 09:10:51 -04:00
2023-09-05 15:51:14 -04:00
// Defer to captain if possible for current target
2024-05-04 12:41:45 -04:00
if (mob.agentType.equals(mbEnums.AIAgentType.GUARDMINION) && mob.guardCaptain.isAlive() && mob.guardCaptain.combatTarget != null) {
2023-09-05 15:51:14 -04:00
mob.setCombatTarget(mob.guardCaptain.combatTarget);
return;
}
2023-08-29 21:01:00 -05:00
ConcurrentHashMap<Integer, Float> loadedPlayers = mob.playerAgroMap;
2023-08-02 09:10:51 -04:00
2024-05-04 12:53:06 -04:00
for (Entry<Integer, Float> playerEntry : loadedPlayers.entrySet()) {
2023-08-02 09:10:51 -04:00
2024-05-04 12:53:06 -04:00
int playerID = playerEntry.getKey();
2023-10-20 09:48:24 -04:00
PlayerCharacter loadedPlayer = PlayerCharacter.getPlayerCharacter(playerID);
2023-08-02 09:10:51 -04:00
//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
2024-05-04 12:41:45 -04:00
if (!guardCanAggro(mob, loadedPlayer))
2023-08-02 09:10:51 -04:00
continue;
2023-08-08 13:36:46 -05:00
if (MovementUtilities.inRangeToAggro(mob, loadedPlayer) && mob.getCombatTarget() == null) {
2023-08-02 09:10:51 -04:00
mob.setCombatTarget(loadedPlayer);
return;
}
}
2024-03-24 09:42:27 -04:00
if (mob.getCombatTarget() == null) {
2024-03-24 09:42:27 -04:00
//look for siege equipment to aggro if no players found to aggro
2024-03-24 09:42:27 -04:00
HashSet<AbstractWorldObject> awoList = WorldGrid.getObjectsInRangePartial(mob, MobAIThread.AI_BASE_AGGRO_RANGE, MBServerStatics.MASK_SIEGE);
2024-03-24 09:42:27 -04:00
for (AbstractWorldObject awoMob : awoList) {
2024-03-24 09:42:27 -04:00
Mob aggroMob = (Mob) awoMob;
2024-04-26 08:14:16 -04:00
if (guardCanAggro(mob, aggroMob)) {
2024-03-24 09:42:27 -04:00
mob.setCombatTarget(aggroMob);
return;
}
2024-03-24 09:42:27 -04:00
}
2023-04-16 21:48:21 -05:00
}
2023-08-02 09:10:51 -04:00
} catch (Exception e) {
2023-08-01 18:52:44 -05:00
Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: CheckForPlayerGuardAggro" + e.getMessage());
}
2023-04-16 21:48:21 -05:00
}
2023-07-15 09:23:48 -04:00
2024-04-26 08:14:16 -04:00
public static Boolean guardCanAggro(Mob mob, AbstractCharacter target) {
2023-08-02 09:10:51 -04:00
try {
2024-05-04 12:41:45 -04:00
if (ConfigManager.MB_RULESET.getValue().equals("LORE") && !target.guild.equals(Guild.getErrantGuild())) {
if (mob.guild.charter.equals(target.guild.charter))
return false;
}
2024-05-05 19:45:46 -05:00
if (mob.guardedCity != null && mob.guardedCity.cityOutlaws.contains(target.getObjectUUID()))
return true;
2023-08-02 09:10:51 -04:00
2023-09-07 14:14:21 -04:00
if (mob.getGuild().getNation().equals(target.getGuild().getNation()))
return false;
2023-08-02 09:10:51 -04:00
//first check condemn list for aggro allowed (allies button is checked)
2024-05-05 19:45:46 -05:00
if (Objects.requireNonNull(ZoneManager.getCityAtLocation(mob.getLoc())).getTOL().reverseKOS) {
for (Entry<Integer, Condemned> entry : Objects.requireNonNull(ZoneManager.getCityAtLocation(mob.getLoc())).getTOL().getCondemned().entrySet()) {
2023-08-02 09:10:51 -04:00
2023-05-06 14:52:10 -05:00
//target is listed individually
2023-08-02 09:10:51 -04:00
if (entry.getValue().playerUID == target.getObjectUUID() && entry.getValue().active)
2023-08-02 09:10:51 -04:00
return false;
2023-05-06 14:52:10 -05:00
//target's guild is listed
2023-08-02 09:10:51 -04:00
if (Guild.getGuild(entry.getValue().guildUID) == target.getGuild())
2023-08-02 09:10:51 -04:00
return false;
2023-05-06 14:52:10 -05:00
//target's nation is listed
2023-08-02 09:10:51 -04:00
if (Guild.getGuild(entry.getValue().guildUID) == target.getGuild().getNation())
2023-08-02 09:10:51 -04:00
return false;
}
return true;
} else {
//allies button is not checked
2024-05-05 19:45:46 -05:00
for (Entry<Integer, Condemned> entry : Objects.requireNonNull(ZoneManager.getCityAtLocation(mob.getLoc())).getTOL().getCondemned().entrySet()) {
2023-08-02 09:10:51 -04:00
2023-04-16 21:48:21 -05:00
//target is listed individually
2023-08-02 09:10:51 -04:00
if (entry.getValue().playerUID == target.getObjectUUID() && entry.getValue().active)
2023-08-02 09:10:51 -04:00
return true;
2023-04-16 21:48:21 -05:00
//target's guild is listed
2023-08-02 09:10:51 -04:00
if (Guild.getGuild(entry.getValue().guildUID) == target.getGuild())
2023-08-02 09:10:51 -04:00
return true;
2023-04-16 21:48:21 -05:00
//target's nation is listed
2023-08-02 09:10:51 -04:00
if (Guild.getGuild(entry.getValue().guildUID) == target.getGuild().getNation())
2023-08-02 09:10:51 -04:00
return true;
}
2023-04-16 21:48:21 -05:00
}
2023-08-02 09:10:51 -04:00
} catch (Exception e) {
2023-08-01 18:52:44 -05:00
Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: GuardCanAggro" + " " + e.getMessage());
}
2023-04-16 21:48:21 -05:00
return false;
}
2023-07-15 09:23:48 -04:00
public static void randomGuardPatrolPoint(Mob mob) {
2023-08-02 09:10:51 -04:00
try {
2023-05-06 14:52:10 -05:00
//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
2023-08-02 09:10:51 -04:00
2024-05-04 12:41:45 -04:00
if (mob.isMoving()) {
2023-08-02 09:10:51 -04:00
mob.stopPatrolTime = System.currentTimeMillis();
return;
}
//wait between 10 and 15 seconds after reaching patrol point before moving
int patrolDelay = ThreadLocalRandom.current().nextInt(10000) + 5000;
2023-05-06 14:52:10 -05:00
//early exit while waiting to patrol again
2023-08-02 09:10:51 -04:00
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);
2023-08-02 09:10:51 -04:00
//make sure mob is out of combat stance
2024-05-05 19:45:46 -05:00
assert minion != null;
2024-05-04 12:41:45 -04:00
if (!minion.despawned) {
if (MovementUtilities.canMove(minion)) {
2024-04-05 19:37:45 -04:00
Vector3f minionOffset = mbEnums.FormationType.getOffset(2, mob.minions.indexOf(minionUUID) + 3);
minion.updateLocation();
2023-08-02 09:10:51 -04:00
Vector3fImmutable formationPatrolPoint = new Vector3fImmutable(mob.destination.x + minionOffset.x, mob.destination.y, mob.destination.z + minionOffset.z);
MovementUtilities.aiMove(minion, formationPatrolPoint, true);
2023-08-02 09:10:51 -04:00
}
2023-05-03 21:20:30 -05:00
}
2023-05-03 21:19:49 -05:00
}
}
2023-08-02 09:10:51 -04:00
} catch (Exception e) {
2023-10-19 17:01:26 -04:00
Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: randomGuardPatrolPoints" + " " + e);
2023-08-01 18:52:44 -05:00
}
}
2023-07-15 09:23:48 -04:00
2024-04-26 08:14:16 -04:00
public static AbstractWorldObject changeTargetFromHateValue(Mob mob) {
2023-08-02 09:10:51 -04:00
try {
float CurrentHateValue = 0;
if (mob.getCombatTarget() != null && mob.getCombatTarget().getObjectType().equals(mbEnums.GameObjectType.PlayerCharacter))
2024-05-04 12:53:06 -04:00
CurrentHateValue = mob.playerAgroMap.get(mob.combatTarget.getObjectUUID());
2023-08-02 09:10:51 -04:00
AbstractWorldObject mostHatedTarget = null;
2024-05-04 12:53:06 -04:00
for (Entry<Integer, Float> playerEntry : mob.playerAgroMap.entrySet()) {
2023-08-02 09:10:51 -04:00
2024-05-04 12:53:06 -04:00
PlayerCharacter potentialTarget = PlayerCharacter.getPlayerCharacter(playerEntry.getKey());
2023-08-02 09:10:51 -04:00
if (potentialTarget.equals(mob.getCombatTarget()))
continue;
2024-05-04 12:41:45 -04:00
if (ConfigManager.MB_RULESET.getValue().equals("LORE") && !potentialTarget.guild.equals(Guild.getErrantGuild())) {
if (mob.guild.charter.equals(potentialTarget.guild.charter))
continue;
}
2024-05-04 12:53:06 -04:00
if (mob.playerAgroMap.get(potentialTarget.getObjectUUID()) > CurrentHateValue && MovementUtilities.inRangeToAggro(mob, potentialTarget)) {
CurrentHateValue = mob.playerAgroMap.get(potentialTarget.getObjectUUID());
2023-08-02 09:10:51 -04:00
mostHatedTarget = potentialTarget;
}
2023-05-07 11:53:00 -05:00
}
2023-08-02 09:10:51 -04:00
return mostHatedTarget;
} catch (Exception e) {
2023-08-01 18:52:44 -05:00
Logger.info(mob.getObjectUUID() + " " + mob.getName() + " Failed At: ChangeTargetFromMostHated" + " " + e.getMessage());
}
return null;
2023-05-07 11:53:00 -05:00
}
}