Initial Repository Push
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,104 @@
|
||||
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
|
||||
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
|
||||
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
|
||||
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
|
||||
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
|
||||
// Magicbane Emulator Project © 2013 - 2022
|
||||
// www.magicbane.com
|
||||
|
||||
|
||||
package engine.ai;
|
||||
|
||||
import engine.gameManager.ZoneManager;
|
||||
import engine.objects.Mob;
|
||||
import engine.objects.Zone;
|
||||
import engine.server.MBServerStatics;
|
||||
import engine.util.ThreadUtils;
|
||||
import org.pmw.tinylog.Logger;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
|
||||
public class MobileFSMManager {
|
||||
|
||||
private static final MobileFSMManager INSTANCE = new MobileFSMManager();
|
||||
|
||||
private volatile boolean alive;
|
||||
private long timeOfKill = -1;
|
||||
|
||||
private MobileFSMManager() {
|
||||
|
||||
Runnable worker = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
execution();
|
||||
}
|
||||
};
|
||||
|
||||
alive = true;
|
||||
|
||||
Thread t = new Thread(worker, "MobileFSMManager");
|
||||
t.start();
|
||||
}
|
||||
|
||||
public static MobileFSMManager getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the MobileFSMManager
|
||||
*/
|
||||
public void shutdown() {
|
||||
if (alive) {
|
||||
alive = false;
|
||||
timeOfKill = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public long getTimeOfKill() {
|
||||
return this.timeOfKill;
|
||||
}
|
||||
|
||||
public boolean isAlive() {
|
||||
return this.alive;
|
||||
}
|
||||
|
||||
|
||||
private void execution() {
|
||||
|
||||
//Load zone threshold once.
|
||||
|
||||
long mobPulse = System.currentTimeMillis() + MBServerStatics.AI_PULSE_MOB_THRESHOLD;
|
||||
|
||||
while (alive) {
|
||||
|
||||
ThreadUtils.sleep(1);
|
||||
|
||||
if (System.currentTimeMillis() > mobPulse) {
|
||||
|
||||
HashSet<Integer> auditMobs = new HashSet<Integer>();
|
||||
|
||||
for (Zone zone : ZoneManager.getAllZones()) {
|
||||
|
||||
for (Mob mob : zone.zoneMobSet) {
|
||||
|
||||
if (auditMobs.contains(mob.getObjectUUID()))
|
||||
continue;
|
||||
auditMobs.add(mob.getObjectUUID());
|
||||
try {
|
||||
if (mob != null)
|
||||
MobileFSM.run(mob);
|
||||
} catch (Exception e) {
|
||||
Logger.error(e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mobPulse = System.currentTimeMillis() + MBServerStatics.AI_PULSE_MOB_THRESHOLD;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,413 @@
|
||||
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
|
||||
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
|
||||
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
|
||||
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
|
||||
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
|
||||
// Magicbane Emulator Project © 2013 - 2022
|
||||
// www.magicbane.com
|
||||
|
||||
|
||||
|
||||
package engine.ai.utilities;
|
||||
|
||||
import engine.Enum;
|
||||
import engine.Enum.*;
|
||||
import engine.ai.MobileFSM.STATE;
|
||||
import engine.gameManager.ChatManager;
|
||||
import engine.gameManager.CombatManager;
|
||||
import engine.math.Vector3fImmutable;
|
||||
import engine.net.DispatchMessage;
|
||||
import engine.net.client.msg.TargetedActionMsg;
|
||||
import engine.objects.*;
|
||||
import engine.server.MBServerStatics;
|
||||
import org.pmw.tinylog.Logger;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import static engine.math.FastMath.sqr;
|
||||
|
||||
public class CombatUtilities {
|
||||
|
||||
public static boolean inRangeToAttack(Mob agent,AbstractWorldObject target){
|
||||
|
||||
if (Float.isNaN(agent.getLoc().x))
|
||||
return false;
|
||||
|
||||
try{
|
||||
Vector3fImmutable sl = agent.getLoc();
|
||||
Vector3fImmutable tl = target.getLoc();
|
||||
|
||||
//add Hitbox's to range.
|
||||
float range = agent.getRange();
|
||||
range += CombatManager.calcHitBox(target) + CombatManager.calcHitBox(agent);
|
||||
//if (target instanceof AbstractCharacter)
|
||||
// if (((AbstractCharacter)target).isMoving())
|
||||
// range+= 5;
|
||||
|
||||
return !(sl.distanceSquared(tl) > sqr(range));
|
||||
}catch(Exception e){
|
||||
Logger.error( e.toString());
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static boolean inRangeToAttack2D(Mob agent,AbstractWorldObject target){
|
||||
|
||||
if (Float.isNaN(agent.getLoc().x))
|
||||
return false;
|
||||
|
||||
try{
|
||||
Vector3fImmutable sl = agent.getLoc();
|
||||
Vector3fImmutable tl = target.getLoc();
|
||||
|
||||
//add Hitbox's to range.
|
||||
float range = agent.getRange();
|
||||
range += CombatManager.calcHitBox(target) + CombatManager.calcHitBox(agent);
|
||||
//if (target instanceof AbstractCharacter)
|
||||
// if (((AbstractCharacter)target).isMoving())
|
||||
// range+= 5;
|
||||
|
||||
return !(sl.distanceSquared2D(tl) > sqr(range));
|
||||
}catch(Exception e){
|
||||
Logger.error( e.toString());
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void swingIsBlock(Mob agent,AbstractWorldObject target, int animation) {
|
||||
|
||||
if (!target.isAlive())
|
||||
return;
|
||||
|
||||
TargetedActionMsg msg = new TargetedActionMsg(agent,animation, target, MBServerStatics.COMBAT_SEND_BLOCK);
|
||||
|
||||
if (target.getObjectType() == GameObjectType.PlayerCharacter)
|
||||
DispatchMessage.dispatchMsgToInterestArea(target, msg, DispatchChannel.PRIMARY, MBServerStatics.CHARACTER_LOAD_RANGE, true,false);
|
||||
else
|
||||
DispatchMessage.sendToAllInRange(agent,msg);
|
||||
|
||||
}
|
||||
|
||||
public static void swingIsParry(Mob agent,AbstractWorldObject target, int animation) {
|
||||
|
||||
if (!target.isAlive())
|
||||
return;
|
||||
|
||||
TargetedActionMsg msg = new TargetedActionMsg(agent,animation, target, MBServerStatics.COMBAT_SEND_PARRY);
|
||||
|
||||
if (target.getObjectType() == GameObjectType.PlayerCharacter)
|
||||
DispatchMessage.dispatchMsgToInterestArea(target, msg, DispatchChannel.PRIMARY, MBServerStatics.CHARACTER_LOAD_RANGE, true,false);
|
||||
else
|
||||
DispatchMessage.sendToAllInRange(agent,msg);
|
||||
|
||||
}
|
||||
|
||||
public static void swingIsDodge(Mob agent,AbstractWorldObject target, int animation) {
|
||||
|
||||
if (!target.isAlive())
|
||||
return;
|
||||
|
||||
TargetedActionMsg msg = new TargetedActionMsg(agent,animation, target, MBServerStatics.COMBAT_SEND_DODGE);
|
||||
|
||||
if (target.getObjectType() == GameObjectType.PlayerCharacter)
|
||||
DispatchMessage.dispatchMsgToInterestArea(target, msg, DispatchChannel.PRIMARY, MBServerStatics.CHARACTER_LOAD_RANGE, true,false);
|
||||
else
|
||||
DispatchMessage.sendToAllInRange(agent,msg);
|
||||
}
|
||||
|
||||
public static void swingIsDamage(Mob agent,AbstractWorldObject target, float damage, int animation){
|
||||
float trueDamage = 0;
|
||||
|
||||
if (!target.isAlive())
|
||||
return;
|
||||
|
||||
if (AbstractWorldObject.IsAbstractCharacter(target))
|
||||
trueDamage = ((AbstractCharacter) target).modifyHealth(-damage, agent, false);
|
||||
else if (target.getObjectType() == GameObjectType.Building)
|
||||
trueDamage = ((Building) target).modifyHealth(-damage, agent);
|
||||
|
||||
//Don't send 0 damage kay thanx.
|
||||
|
||||
if (trueDamage == 0)
|
||||
return;
|
||||
|
||||
TargetedActionMsg msg = new TargetedActionMsg(agent,target, damage, animation);
|
||||
|
||||
if (target.getObjectType() == GameObjectType.PlayerCharacter)
|
||||
DispatchMessage.dispatchMsgToInterestArea(target, msg, DispatchChannel.PRIMARY, MBServerStatics.CHARACTER_LOAD_RANGE, true,false);
|
||||
else
|
||||
DispatchMessage.sendToAllInRange(agent,msg);
|
||||
|
||||
//check damage shields
|
||||
if(AbstractWorldObject.IsAbstractCharacter(target) && target.isAlive() && target.getObjectType() != GameObjectType.Mob)
|
||||
CombatManager.handleDamageShields(agent,(AbstractCharacter)target, damage);
|
||||
}
|
||||
|
||||
public static boolean canSwing(Mob agent) {
|
||||
return (agent.isAlive() && !agent.getBonuses().getBool(ModType.Stunned, SourceType.None));
|
||||
}
|
||||
|
||||
public static void swingIsMiss(Mob agent,AbstractWorldObject target, int animation) {
|
||||
|
||||
TargetedActionMsg msg = new TargetedActionMsg(agent,target, 0f, animation);
|
||||
|
||||
if (target.getObjectType() == GameObjectType.PlayerCharacter)
|
||||
DispatchMessage.dispatchMsgToInterestArea(target, msg, DispatchChannel.PRIMARY, MBServerStatics.CHARACTER_LOAD_RANGE, true,false);
|
||||
else
|
||||
DispatchMessage.sendToAllInRange(agent,msg);
|
||||
|
||||
}
|
||||
|
||||
public static boolean triggerDefense(Mob agent, AbstractWorldObject target) {
|
||||
int defenseScore = 0;
|
||||
int attackScore = agent.getAtrHandOne();
|
||||
switch (target.getObjectType()) {
|
||||
case PlayerCharacter:
|
||||
defenseScore = ((AbstractCharacter) target).getDefenseRating();
|
||||
break;
|
||||
case Mob:
|
||||
|
||||
Mob mob = (Mob)target;
|
||||
if (mob.isSiege())
|
||||
defenseScore = attackScore;
|
||||
break;
|
||||
case Building:
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int hitChance;
|
||||
if (attackScore > defenseScore || defenseScore == 0)
|
||||
hitChance = 94;
|
||||
else if (attackScore == defenseScore && target.getObjectType() == GameObjectType.Mob)
|
||||
hitChance = 10;
|
||||
else {
|
||||
float dif = attackScore / defenseScore;
|
||||
if (dif <= 0.8f)
|
||||
hitChance = 4;
|
||||
else
|
||||
hitChance = ((int)(450 * (dif - 0.8f)) + 4);
|
||||
if (target.getObjectType() == GameObjectType.Building)
|
||||
hitChance = 100;
|
||||
}
|
||||
return ThreadLocalRandom.current().nextInt(100) > hitChance;
|
||||
}
|
||||
|
||||
public static boolean triggerBlock(Mob agent,AbstractWorldObject ac) {
|
||||
return triggerPassive(agent,ac, "Block");
|
||||
}
|
||||
|
||||
public static boolean triggerParry(Mob agent,AbstractWorldObject ac) {
|
||||
return triggerPassive(agent,ac, "Parry");
|
||||
}
|
||||
|
||||
public static boolean triggerDodge(Mob agent,AbstractWorldObject ac) {
|
||||
return triggerPassive(agent,ac, "Dodge");
|
||||
}
|
||||
|
||||
public static boolean triggerPassive(Mob agent,AbstractWorldObject ac, String type) {
|
||||
float chance = 0;
|
||||
if (AbstractWorldObject.IsAbstractCharacter(ac))
|
||||
chance = ((AbstractCharacter)ac).getPassiveChance(type, agent.getLevel(), true);
|
||||
|
||||
if (chance > 75f)
|
||||
chance = 75f;
|
||||
if (agent.isSiege() && AbstractWorldObject.IsAbstractCharacter(ac))
|
||||
chance = 100;
|
||||
|
||||
return ThreadLocalRandom.current().nextInt(100) < chance;
|
||||
}
|
||||
|
||||
|
||||
public static void combatCycle(Mob agent,AbstractWorldObject target, boolean mainHand, ItemBase wb) {
|
||||
|
||||
if (!agent.isAlive() || !target.isAlive()) return;
|
||||
|
||||
if (target.getObjectType() == GameObjectType.PlayerCharacter)
|
||||
if (!((PlayerCharacter)target).isActive())
|
||||
return;
|
||||
|
||||
int anim = 75;
|
||||
float speed = 30f;
|
||||
if (mainHand)
|
||||
speed = agent.getSpeedHandOne();
|
||||
else
|
||||
speed = agent.getSpeedHandTwo();
|
||||
|
||||
DamageType dt = DamageType.Crush;
|
||||
if (agent.isSiege())
|
||||
dt = DamageType.Siege;
|
||||
if (wb != null) {
|
||||
anim = CombatManager.getSwingAnimation(wb, null,mainHand);
|
||||
dt = wb.getDamageType();
|
||||
} else if (!mainHand)
|
||||
return;
|
||||
Resists res = null;
|
||||
PlayerBonuses bonus = null;
|
||||
switch(target.getObjectType()){
|
||||
case Building:
|
||||
res = ((Building)target).getResists();
|
||||
break;
|
||||
case PlayerCharacter:
|
||||
res = ((PlayerCharacter)target).getResists();
|
||||
bonus = ((PlayerCharacter)target).getBonuses();
|
||||
break;
|
||||
case Mob:
|
||||
Mob mob = (Mob)target;
|
||||
res = mob.getResists();
|
||||
bonus = ((Mob)target).getBonuses();
|
||||
break;
|
||||
}
|
||||
|
||||
//must not be immune to all or immune to attack
|
||||
|
||||
if (bonus != null && !bonus.getBool(ModType.NoMod, SourceType.ImmuneToAttack))
|
||||
if (res != null &&(res.immuneToAll() || res.immuneToAttacks() || res.immuneTo(dt)))
|
||||
return;
|
||||
|
||||
int passiveAnim = CombatManager.getSwingAnimation(wb, null,mainHand);
|
||||
if(canSwing(agent)) {
|
||||
if(triggerDefense(agent,target))
|
||||
swingIsMiss(agent,target, passiveAnim);
|
||||
else if(triggerDodge(agent,target))
|
||||
swingIsDodge(agent,target, passiveAnim);
|
||||
else if(triggerParry(agent,target))
|
||||
swingIsParry(agent,target, passiveAnim);
|
||||
else if(triggerBlock(agent,target))
|
||||
swingIsBlock(agent,target, passiveAnim);
|
||||
else
|
||||
swingIsDamage(agent,target, determineDamage(agent,target, mainHand, speed, dt), anim);
|
||||
|
||||
if (agent.getWeaponPower() != null)
|
||||
agent.getWeaponPower().attack(target, MBServerStatics.ONE_MINUTE);
|
||||
}
|
||||
|
||||
if (target.getObjectType().equals(GameObjectType.PlayerCharacter)){
|
||||
PlayerCharacter player = (PlayerCharacter)target;
|
||||
if (player.getDebug(64)){
|
||||
ChatManager.chatSayInfo(player, "Debug Combat: Mob UUID " + agent.getObjectUUID() + " || Building ID = " + agent.getBuildingID() + " || Floor = " + agent.getInFloorID() + " || Level = " + agent.getInBuilding() );//combat debug
|
||||
}
|
||||
}
|
||||
|
||||
//SIEGE MONSTERS DO NOT ATTACK GUARDSs
|
||||
if (target.getObjectType() == GameObjectType.Mob)
|
||||
if (((Mob)target).isSiege())
|
||||
return;
|
||||
|
||||
//handle the retaliate
|
||||
|
||||
if (AbstractWorldObject.IsAbstractCharacter(target))
|
||||
CombatManager.handleRetaliate((AbstractCharacter)target, agent);
|
||||
|
||||
if (target.getObjectType() == GameObjectType.Mob){
|
||||
Mob targetMob = (Mob)target;
|
||||
if (targetMob.isSiege())
|
||||
return;
|
||||
|
||||
if (System.currentTimeMillis() < targetMob.getTimeStamp("CallForHelp"))
|
||||
return;
|
||||
CallForHelp(targetMob);
|
||||
targetMob.getTimestamps().put("CallForHelp", System.currentTimeMillis() + 60000);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static void CallForHelp(Mob aiAgent) {
|
||||
|
||||
Set<Mob> zoneMobs = aiAgent.getParentZone().zoneMobSet;
|
||||
|
||||
|
||||
AbstractWorldObject target = aiAgent.getCombatTarget();
|
||||
if (target == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
for (Mob mob: zoneMobs){
|
||||
if (!mob.isAlive())
|
||||
continue;
|
||||
if (mob.isSiege() || mob.isPet() || !Enum.MobFlagType.AGGRESSIVE.elementOf(mob.getMobBase().getFlags()))
|
||||
continue;
|
||||
if (count == 5)
|
||||
continue;
|
||||
|
||||
|
||||
if (mob.getCombatTarget() != null)
|
||||
continue;
|
||||
|
||||
if (!aiAgent.isPlayerGuard() && mob.isPlayerGuard())
|
||||
continue;
|
||||
|
||||
if (aiAgent.isPlayerGuard() && !mob.isPlayerGuard() )
|
||||
continue;
|
||||
|
||||
if (target.getObjectType() == GameObjectType.PlayerCharacter){
|
||||
|
||||
if (!MovementUtilities.inRangeToAggro(mob, (PlayerCharacter)target))
|
||||
continue;
|
||||
count++;
|
||||
|
||||
}else{
|
||||
|
||||
if (count == 5)
|
||||
continue;
|
||||
|
||||
if (aiAgent.getLoc().distanceSquared2D(target.getLoc()) > sqr(aiAgent.getAggroRange()))
|
||||
continue;
|
||||
|
||||
count++;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (mob.getState() == STATE.Awake || mob.getState() == STATE.Patrol){
|
||||
mob.setCombatTarget(target);
|
||||
mob.setState(STATE.Attack);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static float determineDamage(Mob agent,AbstractWorldObject target, boolean mainHand, float speed, DamageType dt) {
|
||||
|
||||
float min = (mainHand) ? agent.getMinDamageHandOne() : agent.getMinDamageHandTwo();
|
||||
float max = (mainHand) ? agent.getMaxDamageHandOne() : agent.getMaxDamageHandTwo();;
|
||||
|
||||
float range = max - min;
|
||||
float damage = min + ((ThreadLocalRandom.current().nextFloat()*range)+(ThreadLocalRandom.current().nextFloat()*range))/2;
|
||||
|
||||
if (AbstractWorldObject.IsAbstractCharacter(target))
|
||||
if (((AbstractCharacter)target).isSit())
|
||||
damage *= 2.5f; //increase damage if sitting
|
||||
|
||||
if (AbstractWorldObject.IsAbstractCharacter(target))
|
||||
return ((AbstractCharacter)target).getResists().getResistedDamage(agent,(AbstractCharacter)target, dt, damage, 0);
|
||||
|
||||
if (target.getObjectType() == GameObjectType.Building){
|
||||
Building building = (Building)target;
|
||||
Resists resists = building.getResists();
|
||||
return damage * (1 - (resists.getResist(dt, 0) / 100));
|
||||
}
|
||||
|
||||
return damage;
|
||||
|
||||
}
|
||||
|
||||
public static boolean RunAIRandom(){
|
||||
int random = ThreadLocalRandom.current().nextInt(4);
|
||||
|
||||
if (random == 0)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,303 @@
|
||||
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
|
||||
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
|
||||
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
|
||||
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
|
||||
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
|
||||
// Magicbane Emulator Project © 2013 - 2022
|
||||
// www.magicbane.com
|
||||
|
||||
|
||||
|
||||
package engine.ai.utilities;
|
||||
|
||||
import engine.Enum;
|
||||
import engine.Enum.GameObjectType;
|
||||
import engine.Enum.ModType;
|
||||
import engine.Enum.SourceType;
|
||||
import engine.exception.MsgSendException;
|
||||
import engine.gameManager.MovementManager;
|
||||
import engine.math.Vector3fImmutable;
|
||||
import engine.net.client.msg.MoveToPointMsg;
|
||||
import engine.objects.*;
|
||||
import engine.server.MBServerStatics;
|
||||
import org.pmw.tinylog.Logger;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import static engine.math.FastMath.sqr;
|
||||
import static engine.math.FastMath.sqrt;
|
||||
|
||||
public class MovementUtilities {
|
||||
|
||||
|
||||
public static boolean inRangeOfBindLocation(Mob agent){
|
||||
|
||||
|
||||
|
||||
if (agent.isPlayerGuard()){
|
||||
|
||||
Mob guardCaptain = null;
|
||||
if (agent.getContract() != null)
|
||||
guardCaptain = agent;
|
||||
else
|
||||
guardCaptain = (Mob) agent.getNpcOwner();
|
||||
|
||||
if (guardCaptain != null){
|
||||
Building barracks = guardCaptain.getBuilding();
|
||||
|
||||
if (barracks != null){
|
||||
City city = barracks.getCity();
|
||||
|
||||
if (city != null){
|
||||
Building tol = city.getTOL();
|
||||
|
||||
//Guards recall distance = 814.
|
||||
if (tol != null){
|
||||
if (agent.getLoc().distanceSquared2D(tol.getLoc()) > sqr(Enum.CityBoundsType.SIEGE.extents)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Vector3fImmutable sl = new Vector3fImmutable(agent.getLoc().getX(), 0, agent.getLoc().getZ());
|
||||
Vector3fImmutable tl = new Vector3fImmutable(agent.getTrueBindLoc().x,0,agent.getTrueBindLoc().z);
|
||||
|
||||
float distanceSquaredToTarget = sl.distanceSquared2D(tl); //distance to center of target
|
||||
float zoneRange = 250;
|
||||
|
||||
if (agent.getParentZone() != null){
|
||||
if (agent.getParentZone().getBounds() != null)
|
||||
zoneRange = agent.getParentZone().getBounds().getHalfExtents().x * 2;
|
||||
}
|
||||
|
||||
if (zoneRange > 300)
|
||||
zoneRange = 300;
|
||||
|
||||
if (agent.getSpawnRadius() > zoneRange)
|
||||
zoneRange = agent.getSpawnRadius();
|
||||
|
||||
|
||||
return distanceSquaredToTarget < sqr(MBServerStatics.AI_DROP_AGGRO_RANGE + zoneRange);
|
||||
|
||||
}
|
||||
|
||||
public static boolean inRangeToAggro(Mob agent,PlayerCharacter target){
|
||||
|
||||
Vector3fImmutable sl = agent.getLoc();
|
||||
Vector3fImmutable tl =target.getLoc();
|
||||
|
||||
float distanceSquaredToTarget = sl.distanceSquared2D(tl) - sqr(agent.calcHitBox() + target.calcHitBox()); //distance to center of target
|
||||
float range = MBServerStatics.AI_BASE_AGGRO_RANGE;
|
||||
|
||||
if (agent.isPlayerGuard())
|
||||
range = 150;
|
||||
|
||||
return distanceSquaredToTarget < sqr(range);
|
||||
|
||||
}
|
||||
|
||||
public static boolean inRangeDropAggro(Mob agent,PlayerCharacter target){
|
||||
|
||||
Vector3fImmutable sl = agent.getLoc();
|
||||
Vector3fImmutable tl = target.getLoc();
|
||||
|
||||
float distanceSquaredToTarget = sl.distanceSquared2D(tl) - sqr(agent.calcHitBox() + target.calcHitBox()); //distance to center of target
|
||||
|
||||
float range = agent.getRange() + 150;
|
||||
|
||||
if (range > 200)
|
||||
range = 200;
|
||||
|
||||
|
||||
return distanceSquaredToTarget < sqr(range);
|
||||
|
||||
}
|
||||
|
||||
public static Vector3fImmutable GetMoveLocation(Mob aiAgent, AbstractCharacter aggroTarget){
|
||||
|
||||
// Player isnt moving and neither is mob. Just return
|
||||
// the mobile's current location. Ain't goin nowhere!
|
||||
// *** Refactor: Check to ensure methods calling us
|
||||
// all don't sent move messages when not moving.
|
||||
|
||||
if ((aggroTarget.isMoving() == false))
|
||||
return aggroTarget.getLoc();
|
||||
|
||||
if (aggroTarget.getEndLoc().x != 0){
|
||||
|
||||
float aggroTargetDistanceSquared = aggroTarget.getLoc().distanceSquared2D(aggroTarget.getEndLoc());
|
||||
float aiAgentDistanceSquared = aiAgent.getLoc().distanceSquared2D(aggroTarget.getEndLoc());
|
||||
|
||||
if (aiAgentDistanceSquared >= aggroTargetDistanceSquared)
|
||||
return aggroTarget.getEndLoc();
|
||||
else{
|
||||
float distanceToMove = sqrt(aggroTargetDistanceSquared + aiAgentDistanceSquared) *.5f;
|
||||
|
||||
return aggroTarget.getFaceDir().scaleAdd(distanceToMove, aggroTarget.getLoc());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// One of us is moving so let's calculate our destination loc for this
|
||||
// simulation frame. We will simply project our position onto the
|
||||
// character's movement vector and return the closest point.
|
||||
|
||||
return aiAgent.getLoc().ClosestPointOnLine(aggroTarget.getLoc(), aggroTarget.getEndLoc());
|
||||
}
|
||||
|
||||
public static void moveToLocation(Mob agent,Vector3fImmutable newLocation, float offset){
|
||||
try {
|
||||
|
||||
//don't move farther than 30 units from player.
|
||||
if (offset > 30)
|
||||
offset = 30;
|
||||
Vector3fImmutable newLoc = Vector3fImmutable.getRandomPointInCircle(newLocation, offset);
|
||||
|
||||
|
||||
agent.setFaceDir(newLoc.subtract2D(agent.getLoc()).normalize());
|
||||
|
||||
aiMove(agent,newLoc,false);
|
||||
} catch (Exception e) {
|
||||
Logger.error( e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static boolean canMove(Mob agent) {
|
||||
if (agent.getMobBase() != null && Enum.MobFlagType.SENTINEL.elementOf(agent.getMobBase().getFlags()))
|
||||
return false;
|
||||
|
||||
return (agent.isAlive() && !agent.getBonuses().getBool(ModType.Stunned,SourceType.None) && !agent.getBonuses().getBool(ModType.CannotMove, SourceType.None));
|
||||
}
|
||||
|
||||
public static Vector3fImmutable randomPatrolLocation(Mob agent,Vector3fImmutable center, float radius){
|
||||
|
||||
//Determing where I want to move.
|
||||
return new Vector3fImmutable((center.x - radius) + ((ThreadLocalRandom.current().nextFloat()+.1f*2)*radius),
|
||||
center.y,
|
||||
(center.z - radius) + ((ThreadLocalRandom.current().nextFloat()+.1f *2)*radius));
|
||||
}
|
||||
public static Long estimateMovementTime(Mob agent) {
|
||||
if(agent.getEndLoc().x == 0 && agent.getEndLoc().y == 0)
|
||||
return 0L;
|
||||
|
||||
return (long) ((agent.getLoc().distance2D(agent.getEndLoc())*1000)/agent.getSpeed());
|
||||
}
|
||||
|
||||
public static void aiMove(Mob agent,Vector3fImmutable vect, boolean isWalking) {
|
||||
|
||||
//update our walk/run state.
|
||||
if (isWalking && !agent.isWalk()){
|
||||
agent.setWalkMode(true);
|
||||
MovementManager.sendRWSSMsg(agent);
|
||||
}else if(!isWalking && agent.isWalk()){
|
||||
agent.setWalkMode(false);
|
||||
MovementManager.sendRWSSMsg(agent);
|
||||
}
|
||||
|
||||
MoveToPointMsg msg = new MoveToPointMsg();
|
||||
|
||||
|
||||
// Regions currentRegion = Mob.InsideBuildingRegion(agent);
|
||||
//
|
||||
// if (currentRegion != null){
|
||||
//
|
||||
//
|
||||
// if (currentRegion.isGroundLevel()){
|
||||
// agent.setInBuilding(0);
|
||||
// agent.setInFloorID(-1);
|
||||
// }else{
|
||||
// agent.setInBuilding(currentRegion.getLevel());
|
||||
// agent.setInFloorID(currentRegion.getRoom());
|
||||
// }
|
||||
// }else{
|
||||
// agent.setInBuilding(-1);
|
||||
// agent.setInFloorID(-1);
|
||||
// agent.setInBuildingID(0);
|
||||
// }
|
||||
// agent.setLastRegion(currentRegion);
|
||||
|
||||
|
||||
|
||||
Vector3fImmutable startLoc = null;
|
||||
Vector3fImmutable endLoc = null;
|
||||
|
||||
// if (agent.getLastRegion() != null){
|
||||
// Building inBuilding = Building.getBuildingFromCache(agent.getInBuildingID());
|
||||
// if (inBuilding != null){
|
||||
// startLoc = ZoneManager.convertWorldToLocal(inBuilding, agent.getLoc());
|
||||
// endLoc = ZoneManager.convertWorldToLocal(inBuilding, vect);
|
||||
// }
|
||||
// }else{
|
||||
// agent.setBuildingID(0);
|
||||
// agent.setInBuildingID(0);
|
||||
// startLoc = agent.getLoc();
|
||||
// endLoc = vect;
|
||||
// }
|
||||
|
||||
startLoc = agent.getLoc();
|
||||
endLoc = vect;
|
||||
|
||||
msg.setSourceType(GameObjectType.Mob.ordinal());
|
||||
msg.setSourceID(agent.getObjectUUID());
|
||||
msg.setStartCoord(startLoc);
|
||||
msg.setEndCoord(endLoc);
|
||||
msg.setUnknown01(-1);
|
||||
msg.setInBuilding(-1);
|
||||
msg.setTargetType(0);
|
||||
msg.setTargetID(0);
|
||||
|
||||
|
||||
try {
|
||||
MovementManager.movement(msg, agent);
|
||||
} catch (MsgSendException e) {
|
||||
// TODO Figure out how we want to handle the msg send exception
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
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 boolean updateMovementToCharacter(Mob aiAgent, AbstractCharacter aggroTarget){
|
||||
|
||||
if (aiAgent.destination.equals(Vector3fImmutable.ZERO))
|
||||
return true;
|
||||
|
||||
if (!aiAgent.isMoving())
|
||||
return true;
|
||||
|
||||
|
||||
|
||||
|
||||
if (aggroTarget.isMoving()){
|
||||
if (!aiAgent.destination.equals(aggroTarget.getEndLoc()) && !aiAgent.destination.equals(aggroTarget.getLoc()))
|
||||
return true;
|
||||
}else{
|
||||
if (aiAgent.destination.equals(aggroTarget.getLoc()))
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
|
||||
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
|
||||
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
|
||||
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
|
||||
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
|
||||
// Magicbane Emulator Project © 2013 - 2022
|
||||
// www.magicbane.com
|
||||
|
||||
|
||||
|
||||
package engine.ai.utilities;
|
||||
|
||||
public class PowerUtilities {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user