Files
prestonbane/src/engine/ai/MobileFSMManager.java
T

112 lines
2.8 KiB
Java
Raw Normal View History

2022-04-30 09:41:17 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.ai;
import engine.gameManager.SessionManager;
2022-04-30 09:41:17 -04:00
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;
2023-03-16 12:18:42 -04:00
import java.time.Duration;
import java.time.Instant;
2022-04-30 09:41:17 -04:00
public class MobileFSMManager {
private static final MobileFSMManager INSTANCE = new MobileFSMManager();
private volatile boolean alive;
private long timeOfKill = -1;
2023-03-16 12:40:08 -04:00
public static Duration executionTime = Duration.ofNanos(1);
public static Duration executionMax = Duration.ofNanos(1);
2023-03-16 12:18:42 -04:00
2022-04-30 09:41:17 -04:00
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() {
2023-03-16 11:48:54 -04:00
//Load zone threshold once.
2022-04-30 09:41:17 -04:00
long mobPulse = System.currentTimeMillis() + MBServerStatics.AI_PULSE_MOB_THRESHOLD;
2023-03-16 12:18:42 -04:00
Instant startTime;
2022-04-30 09:41:17 -04:00
while (alive) {
2023-03-16 11:48:54 -04:00
ThreadUtils.sleep(1);
2022-04-30 09:41:17 -04:00
if (System.currentTimeMillis() > mobPulse) {
2023-03-16 12:18:42 -04:00
startTime = Instant.now();
2022-04-30 09:41:17 -04:00
for (Zone zone : ZoneManager.getAllZones()) {
2023-03-16 11:48:54 -04:00
2022-04-30 09:41:17 -04:00
for (Mob mob : zone.zoneMobSet) {
2022-04-30 09:41:17 -04:00
try {
if (mob != null && SessionManager.getActivePlayerCharacterCount() > 0)
MobileFSM.DetermineAction(mob);
2022-04-30 09:41:17 -04:00
} catch (Exception e) {
2023-05-06 15:53:35 -05:00
Logger.error("Mob: " + mob.getName() + " UUID: " + mob.getObjectUUID() + " ERROR: " + e);
2022-04-30 09:41:17 -04:00
e.printStackTrace();
}
}
}
2023-03-16 12:18:42 -04:00
this.executionTime = Duration.between(startTime, Instant.now());
2023-03-16 12:40:08 -04:00
if (executionTime.compareTo(executionMax) > 0)
executionMax = executionTime;
2022-04-30 09:41:17 -04:00
mobPulse = System.currentTimeMillis() + MBServerStatics.AI_PULSE_MOB_THRESHOLD;
}
}
}
}