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

1398 lines
51 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
2023-04-17 07:20:39 -04:00
import engine.Enum;
2022-04-30 09:41:17 -04:00
import engine.Enum.DispatchChannel;
import engine.InterestManagement.WorldGrid;
2023-11-05 10:45:35 -06:00
import engine.exception.MsgSendException;
import engine.gameManager.*;
2023-04-17 20:51:34 -05:00
import engine.math.Vector3f;
import engine.math.Vector3fImmutable;
2023-08-02 09:10:51 -04:00
import engine.mobileAI.Threads.MobAIThread;
2023-10-23 00:32:59 -04:00
import engine.mobileAI.Threads.Respawner;
2023-08-02 09:10:51 -04:00
import engine.mobileAI.utilities.CombatUtilities;
2023-11-14 22:22:24 -06:00
import engine.mobileAI.utilities.PathingUtilities;
2022-04-30 09:41:17 -04:00
import engine.net.DispatchMessage;
2023-11-05 10:45:35 -06:00
import engine.net.client.msg.MoveToPointMsg;
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;
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
2023-04-20 21:23:42 -05: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
2023-09-02 16:04:40 -04:00
if (target.getObjectType().equals(Enum.GameObjectType.PlayerCharacter) &&
!mob.canSee((AbstractCharacter) target)) {
mob.setCombatTarget(null);
return;
}
2023-08-05 23:10:06 -05:00
if (target.getObjectType() == Enum.GameObjectType.PlayerCharacter && canCast(mob)) {
2023-08-18 11:33:27 -04:00
2023-09-05 14:25:24 -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
2023-08-01 18:52:44 -05:00
if (!CombatUtilities.inRangeToAttack(mob, target))
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;
AttackPlayer(mob, targetPlayer);
break;
case Building:
Building targetBuilding = (Building) target;
AttackBuilding(mob, targetBuilding);
break;
case Mob:
Mob targetMob = (Mob) target;
AttackMob(mob, targetMob);
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
2023-04-23 20:38:28 -05:00
public static void AttackPlayer(Mob mob, PlayerCharacter target) {
2023-08-02 09:10:51 -04:00
try {
if (!mob.canSee(target)) {
mob.setCombatTarget(null);
return;
}
2023-08-25 15:14:33 -04:00
if (mob.behaviourType.callsForHelp)
2023-08-02 09:10:51 -04:00
MobCallForHelp(mob);
2023-11-05 10:45:35 -06:00
if (inRangeDropAggro(mob, target)) {
2023-08-02 09:10:51 -04:00
mob.setCombatTarget(null);
return;
2023-08-02 09:10:51 -04:00
}
if (CombatUtilities.inRange2D(mob, target, mob.getRange())) {
//no weapons, default mob attack speed 3 seconds.
if (System.currentTimeMillis() < mob.getLastAttackTime())
return;
// ranged mobs cant attack while running. skip until they finally stop.
if (mob.isMoving() && mob.getRange() > 20)
return;
// add timer for last attack.
ItemBase mainHand = mob.getWeaponItemBase(true);
ItemBase offHand = mob.getWeaponItemBase(false);
if (mainHand == null && offHand == null) {
CombatUtilities.combatCycle(mob, target, true, null);
int delay = 3000;
if (mob.isSiege())
delay = 11000;
mob.setLastAttackTime(System.currentTimeMillis() + delay);
} else if (mob.getWeaponItemBase(true) != null) {
int delay = 3000;
if (mob.isSiege())
delay = 11000;
CombatUtilities.combatCycle(mob, target, true, mob.getWeaponItemBase(true));
mob.setLastAttackTime(System.currentTimeMillis() + delay);
} else if (mob.getWeaponItemBase(false) != null) {
int attackDelay = 3000;
if (mob.isSiege())
attackDelay = 11000;
CombatUtilities.combatCycle(mob, target, false, mob.getWeaponItemBase(false));
mob.setLastAttackTime(System.currentTimeMillis() + attackDelay);
}
}
if (target.getPet() != null)
if (target.getPet().getCombatTarget() == null && target.getPet().assist == true)
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());
}
}
public static void AttackBuilding(Mob mob, Building target) {
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)
2023-09-01 20:57:09 -05:00
if (guard.agentType.equals(Enum.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);
2023-04-20 21:23:42 -05:00
ItemBase mainHand = mob.getWeaponItemBase(true);
ItemBase offHand = mob.getWeaponItemBase(false);
2023-08-02 09:10:51 -04:00
2022-04-30 09:41:17 -04:00
if (mainHand == null && offHand == null) {
2023-04-20 21:23:42 -05:00
CombatUtilities.combatCycle(mob, target, true, null);
2022-04-30 09:41:17 -04:00
int delay = 3000;
2023-04-20 21:23:42 -05:00
if (mob.isSiege())
2023-08-02 09:10:51 -04:00
delay = 15000;
2023-04-20 21:23:42 -05:00
mob.setLastAttackTime(System.currentTimeMillis() + delay);
} else if (mob.getWeaponItemBase(true) != null) {
2023-08-02 09:10:51 -04:00
int attackDelay = 3000;
2023-04-20 21:23:42 -05:00
if (mob.isSiege())
2023-08-02 09:10:51 -04:00
attackDelay = 15000;
2023-04-20 21:23:42 -05:00
CombatUtilities.combatCycle(mob, target, true, mob.getWeaponItemBase(true));
2023-08-02 09:10:51 -04:00
mob.setLastAttackTime(System.currentTimeMillis() + attackDelay);
2023-04-20 21:23:42 -05:00
} else if (mob.getWeaponItemBase(false) != null) {
int attackDelay = 3000;
if (mob.isSiege())
2023-08-02 09:10:51 -04:00
attackDelay = 15000;
2023-04-20 21:23:42 -05:00
CombatUtilities.combatCycle(mob, target, false, mob.getWeaponItemBase(false));
mob.setLastAttackTime(System.currentTimeMillis() + attackDelay);
}
2023-08-02 09:10:51 -04:00
if (mob.isSiege()) {
PowerProjectileMsg ppm = new PowerProjectileMsg(mob, target);
ppm.setRange(50);
DispatchMessage.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
2023-04-23 20:38:28 -05:00
public static void AttackMob(Mob mob, Mob target) {
2023-08-02 09:10:51 -04:00
try {
if (mob.getRange() >= 30 && mob.isMoving())
return;
//no weapons, default mob attack speed 3 seconds.
ItemBase mainHand = mob.getWeaponItemBase(true);
ItemBase offHand = mob.getWeaponItemBase(false);
if (mainHand == null && offHand == null) {
CombatUtilities.combatCycle(mob, target, true, null);
int delay = 3000;
if (mob.isSiege())
delay = 11000;
mob.setLastAttackTime(System.currentTimeMillis() + delay);
} else if (mob.getWeaponItemBase(true) != null) {
int attackDelay = 3000;
if (mob.isSiege())
attackDelay = 11000;
CombatUtilities.combatCycle(mob, target, true, mob.getWeaponItemBase(true));
mob.setLastAttackTime(System.currentTimeMillis() + attackDelay);
} else if (mob.getWeaponItemBase(false) != null) {
int attackDelay = 3000;
if (mob.isSiege())
attackDelay = 11000;
CombatUtilities.combatCycle(mob, target, false, mob.getWeaponItemBase(false));
mob.setLastAttackTime(System.currentTimeMillis() + attackDelay);
2023-08-08 13:36:46 -05:00
if (target.getCombatTarget() == null) {
target.setCombatTarget(mob);
2023-08-02 09:10:51 -04:00
}
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: AttackMob" + " " + e.getMessage());
}
}
2023-07-15 09:23:48 -04:00
2023-04-20 21:23:42 -05: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
boolean forced = mob.agentType.equals(Enum.AIAgentType.GUARDMINION) &&
mob.guardCaptain.isAlive();
2023-08-02 09:10:51 -04:00
if (mob.stopPatrolTime + (patrolDelay * 1000) > 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(Enum.AIAgentType.GUARDCAPTAIN) || mob.agentType.equals(Enum.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
}
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(Enum.AIAgentType.GUARDMINION)) {
2023-09-07 12:07:39 -04:00
Mob captain = (Mob) mob.guardCaptain;
2023-11-05 10:45:35 -06:00
mob.destination = captain.destination.add(Formation.getOffset(2, mob.guardCaptain.minions.indexOf(mob.getObjectUUID()) + 3));
2023-09-07 12:07:39 -04:00
mob.lastPatrolPointIndex = captain.lastPatrolPointIndex;
} else {
2023-11-07 21:45:16 -06:00
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(Enum.AIAgentType.GUARDCAPTAIN))
for (Integer minionUUID : mob.minions) {
Mob minion = Mob.getMob(minionUUID);
2023-09-07 12:42:43 -04:00
if (minion.isAlive() && minion.combatTarget == null)
2023-09-07 12:41:20 -04:00
MobAI.Patrol(minion);
}
2023-11-05 10:45:35 -06:00
aiMove(mob, true,0);
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-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;
2023-09-02 15:30:10 -04:00
if (mob.isPlayerGuard() == true) {
2023-08-18 11:33:27 -04:00
2023-09-09 09:52:47 -05:00
if(mob.agentType.equals(Enum.AIAgentType.GUARDWALLARCHER))
return false; //wall archers don't cast
2023-09-02 15:30:10 -04:00
if (mob.agentType.equals(Enum.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
if (Enum.MinionType.ContractToMinionMap.get(contractID).isMage() == false && 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..
if (PowersManager.getPowersForRune(mob.getMobBaseID()).isEmpty() &&
2023-09-02 15:30:10 -04:00
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
2023-02-04 21:26:04 -06: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)
2023-08-02 09:10:51 -04:00
MobCallForHelp(mob);
// 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(Enum.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
//check for hit-roll
2023-08-18 11:33:27 -04:00
if (mobPower.requiresHitRoll)
2023-08-02 09:10:51 -04:00
if (CombatUtilities.triggerDefense(mob, mob.getCombatTarget()))
return false;
// Cast the spell
2023-08-05 23:10:06 -05:00
if (CombatUtilities.inRange2D(mob, mob.getCombatTarget(), mobPower.getRange())) {
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);
2023-09-05 15:56:49 -04:00
long randomCooldown = (long) ((ThreadLocalRandom.current().nextInt(10, 15) * 1000) * 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
2023-02-04 21:26:04 -06: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!");
Zone mobCamp = mob.getParentZone();
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
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;
if (mob.getTimestamps().containsKey("lastExecution") == false)
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) {
CheckForRespawn(mob);
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
2023-09-01 20:57:09 -05:00
if (mob.agentType.equals(Enum.AIAgentType.GUARDMINION)) {
2023-08-26 11:55:18 -04:00
if (mob.guardCaptain.isAlive() == false || ((Mob) mob.guardCaptain).despawned == true) {
2023-08-02 09:10:51 -04:00
//minions don't respawn while guard captain is dead
if (mob.isAlive() == false) {
mob.deathTime = System.currentTimeMillis();
return;
}
2023-05-07 11:53:00 -05:00
}
}
2023-08-02 09:10:51 -04:00
CheckForRespawn(mob);
//check to send mob home for player guards to prevent exploit of dragging guards away and then teleporting
2023-09-01 20:57:09 -05:00
if (!mob.agentType.equals(Enum.AIAgentType.PET))
2023-08-02 09:10:51 -04:00
CheckToSendMobHome(mob);
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()) {
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(Enum.GameObjectType.PlayerCharacter))
2023-10-27 23:17:31 -05:00
if(mob.combatTarget.loc.distanceSquared(mob.loc) > 62500)
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
2023-09-01 20:57:09 -05:00
if (mob.agentType.equals(Enum.AIAgentType.PET) == false)
2023-08-02 09:10:51 -04:00
CheckToSendMobHome(mob);
2023-08-08 13:36:46 -05:00
if (mob.getCombatTarget() != null) {
2023-08-18 11:33:27 -04:00
2023-08-02 09:10:51 -04:00
if (mob.getCombatTarget().isAlive() == false) {
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
if (mob.playerAgroMap.containsKey(target.getObjectUUID()) == false) {
mob.setCombatTarget(null);
return;
}
if (mob.canSee((PlayerCharacter) mob.getCombatTarget()) == false) {
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:
2023-09-09 10:37:40 -05: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:
2023-08-02 09:10:51 -04:00
PetLogic(mob);
break;
case HamletGuard:
HamletGuardLogic(mob);
break;
default:
DefaultLogic(mob);
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
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
for (Entry playerEntry : loadedPlayers.entrySet()) {
int playerID = (int) 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
if (aiAgent.notEnemy.size() > 0 && aiAgent.notEnemy.contains(loadedPlayer.getRace().getRaceType().getMonsterType()) == true)
continue;
//mob has enemies and this player race is not it
if (aiAgent.enemy.size() > 0 && aiAgent.enemy.contains(loadedPlayer.getRace().getRaceType().getMonsterType()) == false)
continue;
2023-11-05 10:45:35 -06:00
if (inRangeToAggro(aiAgent, loadedPlayer)) {
2023-08-02 09:10:51 -04:00
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
private static void CheckMobMovement(Mob mob) {
2023-08-02 09:10:51 -04:00
try {
2023-11-05 10:45:35 -06:00
if (!canMove(mob))
2023-08-02 09:10:51 -04:00
return;
mob.updateLocation();
2023-08-25 15:14:33 -04:00
switch (mob.behaviourType) {
2023-08-02 09:10:51 -04:00
2023-08-25 12:32:36 -04:00
case Pet1:
2023-08-27 21:04:34 -05:00
2023-09-05 15:56:49 -04:00
if (mob.guardCaptain == null)
2023-04-30 16:54:39 -05:00
return;
2023-08-02 09:10:51 -04:00
2023-09-07 12:24:45 -04:00
//mob no longer has its owner loaded, translate pet to owner
2023-08-02 09:10:51 -04:00
2023-09-07 12:24:45 -04:00
if (!mob.playerAgroMap.containsKey(mob.guardCaptain.getObjectUUID())) {
2023-09-13 19:56:54 -05:00
MovementManager.translocate(mob, mob.guardCaptain.getLoc());
2023-08-02 09:10:51 -04:00
return;
}
2023-09-07 12:24:45 -04:00
2023-08-02 09:10:51 -04:00
if (mob.getCombatTarget() == null) {
//move back to owner
2023-09-05 15:56:49 -04:00
if (CombatUtilities.inRange2D(mob, mob.guardCaptain, 6))
2023-08-02 09:10:51 -04:00
return;
2023-11-07 21:45:16 -06:00
mob.destination = mob.guardCaptain.getLoc();
2023-11-05 10:45:35 -06:00
aiMove(mob, false,5);
2023-08-02 09:10:51 -04:00
} else
2023-05-06 15:53:35 -05:00
chaseTarget(mob);
2023-08-02 09:10:51 -04:00
break;
default:
if (mob.getCombatTarget() == null) {
2023-09-07 12:24:45 -04:00
if (!mob.isMoving()) {
// Minions only patrol on their own if captain is dead.
if (mob.agentType.equals(Enum.AIAgentType.GUARDMINION) == false)
Patrol(mob);
else if (mob.guardCaptain.isAlive() == false)
Patrol(mob);
} else
2023-08-02 09:10:51 -04:00
mob.stopPatrolTime = System.currentTimeMillis();
2023-07-15 09:23:48 -04:00
} else {
2023-08-02 09:10:51 -04:00
chaseTarget(mob);
2023-04-30 16:54:39 -05:00
}
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: CheckMobMovement" + " " + e.getMessage());
}
}
2023-07-15 09:23:48 -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) {
if (aiAgent.getCharItemManager().getInventoryCount() > 0) {
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-07-25 21:35:09 -05:00
return;
2023-04-21 15:17:37 -05:00
}
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();
return;
}
//Mob never had Loot.
} else {
if (System.currentTimeMillis() > aiAgent.deathTime + MBServerStatics.DESPAWN_TIMER) {
aiAgent.despawn();
aiAgent.deathTime = System.currentTimeMillis();
return;
}
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);
2023-10-23 00:32:59 -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
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
if (mob.isAlive() == false)
return;
if (mob.getCombatTarget() == null)
return;
2023-11-05 10:45:35 -06:00
if (mob.getCombatTarget().getObjectType().equals(Enum.GameObjectType.PlayerCharacter) && inRangeDropAggro(mob, (PlayerCharacter) mob.getCombatTarget()) &&
2023-09-01 20:57:09 -05:00
mob.agentType.equals(Enum.AIAgentType.PET) == false) {
2023-08-02 09:10:51 -04:00
mob.setCombatTarget(null);
return;
}
if (System.currentTimeMillis() > mob.getLastAttackTime())
AttackTarget(mob, mob.getCombatTarget());
} 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
private static void CheckToSendMobHome(Mob mob) {
2023-08-02 09:10:51 -04:00
try {
if (mob.getCombatTarget() != null && CombatUtilities.inRange2D(mob, mob.getCombatTarget(), 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()) == false) {
PowersBase recall = PowersManager.getPowerByToken(-1994153779);
PowersManager.useMobPower(mob, mob, recall, 40);
mob.setCombatTarget(null);
2023-09-01 20:57:09 -05:00
if (mob.agentType.equals(Enum.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);
minion.setCombatTarget(null);
2023-08-02 09:10:51 -04:00
}
}
}
2023-11-05 10:45:35 -06:00
} else if (inRangeOfBindLocation(mob) == false) {
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);
2023-08-29 21:01:00 -05:00
for (Integer playerEntry : mob.playerAgroMap.keySet())
2023-09-05 15:56:49 -04:00
mob.playerAgroMap.put(playerEntry, 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 {
2023-10-16 14:33:09 -05:00
if(mob.getTimestamps().containsKey("lastChase") == false)
mob.getTimestamps().put("lastChase",System.currentTimeMillis());
2023-10-16 15:07:39 -05:00
else if(System.currentTimeMillis() < mob.getTimestamps().get("lastChase").longValue() + (750 + ThreadLocalRandom.current().nextInt(0,500)))
2023-10-16 14:33:09 -05:00
return;
2023-10-16 14:37:17 -05:00
mob.getTimestamps().put("lastChase",System.currentTimeMillis());
2023-09-07 10:40:27 -04:00
if (CombatUtilities.inRange2D(mob, mob.getCombatTarget(), mob.getRange()) == false) {
2023-08-02 09:10:51 -04:00
if (mob.getRange() > 15) {
2023-11-07 21:45:16 -06:00
mob.destination = mob.getCombatTarget().getLoc();
2023-11-05 10:45:35 -06:00
aiMove(mob, false,0);
2023-08-02 09:10:51 -04:00
} else {
//check if building
switch (mob.getCombatTarget().getObjectType()) {
case PlayerCharacter:
case Mob:
2023-11-07 22:55:34 -06:00
mob.destination = mob.combatTarget.loc;//GetDestinationToCharacter(mob, (AbstractCharacter) mob.getCombatTarget());
2023-11-05 10:45:35 -06:00
aiMove(mob, false,mob.getRange() + 1);
2023-08-02 09:10:51 -04:00
break;
case Building:
2023-11-05 10:45:35 -06:00
mob.destination = mob.getCombatTarget().getLoc();
aiMove(mob, false,0);
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
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.
if (mob.equals(awoMob) || (mob.agentType.equals(Enum.AIAgentType.GUARDCAPTAIN)) == true)
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
2023-08-02 09:10:51 -04:00
if ((aggroMob.agentType.equals(Enum.AIAgentType.GUARDCAPTAIN)))
2023-08-02 09:10:51 -04:00
continue;
2023-08-18 11:33:27 -04:00
2023-09-01 20:57:09 -05:00
if (aggroMob.agentType.equals(Enum.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
2023-09-07 10:36:18 -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) {
2023-08-02 09:10:51 -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
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
2023-09-09 10:37:40 -05:00
if (newTarget.getObjectType().equals(Enum.GameObjectType.PlayerCharacter)) {
if (GuardCanAggro(mob, (PlayerCharacter) newTarget))
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
}
2023-09-09 10:37:40 -05:00
if(mob.behaviourType.canRoam)
CheckMobMovement(mob);//all guards that can move check to move
2023-07-15 09:23:48 -04:00
2023-09-09 10:37:40 -05:00
if(mob.combatTarget != null)
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
2023-04-23 20:38:28 -05:00
private static void PetLogic(Mob mob) {
2023-08-02 09:10:51 -04:00
try {
2023-08-18 11:33:27 -04:00
if (mob.guardCaptain == null && mob.isNecroPet() == false && mob.isSiege() == false)
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-11-05 10:45:35 -06:00
if (canMove(mob) && mob.behaviourType.canRoam)
2023-08-02 09:10:51 -04:00
CheckMobMovement(mob);
CheckForAttack(mob);
//recover health
if (mob.getTimestamps().containsKey("HEALTHRECOVERED") == false)
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
2023-08-02 09:10:51 -04:00
float recoveredHealth = mob.getHealthMax() * ((1 + mob.getBonuses().getFloatPercentAll(Enum.ModType.HealthRecoverRate, Enum.SourceType.None)) * 0.01f);
mob.setHealth(mob.getHealth() + recoveredHealth);
mob.getTimestamps().put("HEALTHRECOVERED", System.currentTimeMillis());
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
2023-04-23 20:38:28 -05:00
private static void HamletGuardLogic(Mob mob) {
2023-08-02 09:10:51 -04:00
try {
2023-04-21 10:02:09 -05:00
//safehold guard
2023-08-02 09:10:51 -04:00
if (mob.getCombatTarget() == null)
2023-05-28 11:02:37 -05:00
SafeGuardAggro(mob);
2023-08-08 13:36:46 -05:00
else if (mob.getCombatTarget().isAlive() == false)
2023-08-02 09:10:51 -04:00
SafeGuardAggro(mob);
CheckForAttack(mob);
} 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
2023-04-23 20:38:28 -05: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
if (mob.getCombatTarget() != null && mob.playerAgroMap.containsKey(mob.getCombatTarget().getObjectUUID()) == false)
mob.setCombatTarget(null);
2023-08-25 15:14:33 -04:00
if (mob.behaviourType.isAgressive) {
2023-08-02 09:10:51 -04:00
AbstractWorldObject newTarget = ChangeTargetFromHateValue(mob);
if (newTarget != null)
mob.setCombatTarget(newTarget);
else {
if (mob.getCombatTarget() == null) {
2023-08-25 15:14:33 -04:00
if (mob.behaviourType == Enum.MobBehaviourType.HamletGuard)
2023-08-02 09:12:13 -04:00
SafeGuardAggro(mob); //safehold guard
2023-08-02 09:10:51 -04:00
else
2023-08-02 09:12:13 -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)
2023-08-02 09:10:51 -04:00
CheckMobMovement(mob);
//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)
2023-08-02 09:10:51 -04:00
CheckForAttack(mob);
} 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
2023-04-16 21:48:21 -05: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
if (mob.agentType.equals(Enum.AIAgentType.GUARDMINION) &&
2023-09-05 15:56:49 -04:00
mob.guardCaptain.isAlive()
2023-09-05 15:51:14 -04:00
&& mob.guardCaptain.combatTarget != null) {
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
for (Entry playerEntry : loadedPlayers.entrySet()) {
int playerID = (int) 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
if (GuardCanAggro(mob, loadedPlayer) == false)
continue;
2023-11-05 10:45:35 -06:00
if (inRangeToAggro(mob, loadedPlayer) && mob.getCombatTarget() == null) {
2023-08-02 09:10:51 -04:00
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;
}
}
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
public static Boolean GuardCanAggro(Mob mob, AbstractCharacter target) {
2023-08-02 09:10:51 -04:00
try {
2023-09-05 12:55:12 -04:00
if (mob.guardedCity.cityOutlaws.contains(target.getObjectUUID()) == true)
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)
if (ZoneManager.getCityAtLocation(mob.getLoc()).getTOL().reverseKOS) {
for (Entry<Integer, Condemned> entry : ZoneManager.getCityAtLocation(mob.getLoc()).getTOL().getCondemned().entrySet()) {
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
for (Entry<Integer, Condemned> entry : ZoneManager.getCityAtLocation(mob.getLoc()).getTOL().getCondemned().entrySet()) {
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
if (mob.isMoving() == true) {
mob.stopPatrolTime = System.currentTimeMillis();
return;
}
//wait between 10 and 15 seconds after reaching patrol point before moving
int patrolDelay = ThreadLocalRandom.current().nextInt(10000) + 5000;
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();
2023-11-07 21:45:16 -06:00
mob.destination = new Vector3fImmutable(TreePos.x + xPoint, TreePos.y, TreePos.z + zPoint);
2023-08-02 09:10:51 -04:00
2023-11-05 10:45:35 -06:00
aiMove(mob, true,0);
2023-08-02 09:10:51 -04:00
2023-09-01 20:57:09 -05:00
if (mob.agentType.equals(Enum.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
if (minion.despawned == false) {
2023-11-05 10:45:35 -06:00
if (canMove(minion)) {
Vector3f minionOffset = Formation.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);
2023-11-05 10:45:35 -06:00
minion.destination = formationPatrolPoint;
aiMove(minion, true,0);
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
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(Enum.GameObjectType.PlayerCharacter))
2023-08-29 21:01:00 -05:00
CurrentHateValue = mob.playerAgroMap.get(mob.combatTarget.getObjectUUID()).floatValue();
2023-08-02 09:10:51 -04:00
AbstractWorldObject mostHatedTarget = null;
for (Entry playerEntry : mob.playerAgroMap.entrySet()) {
2023-10-20 09:48:24 -04:00
PlayerCharacter potentialTarget = PlayerCharacter.getPlayerCharacter((int) playerEntry.getKey());
2023-08-02 09:10:51 -04:00
if (potentialTarget.equals(mob.getCombatTarget()))
continue;
2023-11-05 10:45:35 -06:00
if (potentialTarget != null && mob.playerAgroMap.get(potentialTarget.getObjectUUID()).floatValue() > CurrentHateValue && inRangeToAggro(mob, potentialTarget)) {
2023-08-29 21:01:00 -05:00
CurrentHateValue = mob.playerAgroMap.get(potentialTarget.getObjectUUID()).floatValue();
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
}
2023-11-05 10:45:35 -06:00
public static boolean inRangeDropAggro(Mob agent, AbstractCharacter target) {
float rangeSquared = MobAIThread.AI_BASE_AGGRO_RANGE * MobAIThread.AI_BASE_AGGRO_RANGE;
if(agent.isPlayerGuard())
rangeSquared = 62500;
if(agent.isSiege())
rangeSquared = agent.getRange() * agent.getRange();
return agent.loc.distanceSquared(target.loc) > rangeSquared;
}
public static boolean inRangeToAggro(Mob agent, PlayerCharacter target) {
return MobAIThread.AI_BASE_AGGRO_RANGE * MobAIThread.AI_BASE_AGGRO_RANGE > agent.loc.distanceSquared(target.loc) ;
}
public static boolean inRangeOfBindLocation(Mob agent) {
if(agent.isPlayerGuard() && agent.guardedCity.isLocationOnCityZone(agent.loc))
return true;
float bindRangeSquared = (agent.parentZone.getBounds().getHalfExtents().x * agent.parentZone.getBounds().getHalfExtents().x) + (MobAIThread.AI_DROP_AGGRO_RANGE * MobAIThread.AI_DROP_AGGRO_RANGE);
return agent.loc.distanceSquared(agent.bindLoc) < bindRangeSquared;
}
public static boolean canMove(Mob agent) {
if (!agent.behaviourType.canRoam)
return false;
return (agent.isAlive() && !agent.getBonuses().getBool(Enum.ModType.Stunned, Enum.SourceType.None) && !agent.getBonuses().getBool(Enum.ModType.CannotMove, Enum.SourceType.None));
}
public static Vector3fImmutable GetDestinationToCharacter(Mob aiAgent, AbstractCharacter character) {
if (!character.isMoving())
return character.getLoc();
float agentDistanceEndLoc = aiAgent.getLoc().distanceSquared2D(character.getEndLoc());
float characterDistanceEndLoc = character.getLoc().distanceSquared2D(character.getEndLoc());
if (agentDistanceEndLoc > characterDistanceEndLoc)
return character.getEndLoc();
return character.getLoc();
}
public static void aiMove(Mob mob, boolean isWalking, float offset) {
2023-11-07 22:55:34 -06:00
if(mob.isMoving()) {
2023-11-07 21:45:16 -06:00
return;
}
2023-11-14 22:22:24 -06:00
if(!mob.isPathing){
2023-11-14 22:50:37 -06:00
ArrayList<PathingUtilities.Node> path = PathingUtilities.getPath(mob, mob.destination);
2023-11-14 22:22:24 -06:00
if(path != null && path.size() > 0)
PathingUtilities.followPath(mob,path);
2023-11-14 20:54:29 -06:00
}
}
2023-11-14 22:22:24 -06:00
public static void directMove(AbstractCharacter mob,boolean isWalking){
2023-11-14 20:54:29 -06:00
//update our walk/run state.
if (isWalking && !mob.isWalk()) {
mob.setWalkMode(true);
MovementManager.sendRWSSMsg(mob);
} else if (!isWalking && mob.isWalk()) {
mob.setWalkMode(false);
MovementManager.sendRWSSMsg(mob);
}
mob.endLoc = mob.destination;
MoveToPointMsg msg = new MoveToPointMsg();
msg.setSourceType(Enum.GameObjectType.Mob.ordinal());
msg.setSourceID(mob.getObjectUUID());
msg.setStartCoord(mob.loc);
msg.setEndCoord(mob.destination);
Regions region = Regions.getRegionAtLocation(mob.destination);
if(region != null){
msg.setInBuildingFloor(region.room);
msg.setInBuilding(region.level);
msg.setStartLocType(0);
msg.setInBuildingUUID(region.parentBuildingID);
} else{
msg.setInBuildingFloor(-1);
msg.setInBuilding(-1);
msg.setStartLocType(0);
msg.setInBuildingUUID(0);
}
2023-11-05 10:45:35 -06:00
try {
MovementManager.movement(msg, mob);
} catch (MsgSendException e) {
// TODO Figure out how we want to handle the msg send exception
e.printStackTrace();
}
}
}