From 99a9cd297c3e06621206cb61901e6ba58692fec7 Mon Sep 17 00:00:00 2001 From: FatBoy-DOTC Date: Sat, 18 Jan 2025 03:19:08 -0600 Subject: [PATCH] optimized mob ai thread --- src/engine/mobileAI/Threads/MobAIThread.java | 26 +++++++++++++------- src/engine/workthreads/BaneThread.java | 2 +- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/engine/mobileAI/Threads/MobAIThread.java b/src/engine/mobileAI/Threads/MobAIThread.java index b3699879..8a7d8a8a 100644 --- a/src/engine/mobileAI/Threads/MobAIThread.java +++ b/src/engine/mobileAI/Threads/MobAIThread.java @@ -28,18 +28,26 @@ public class MobAIThread implements Runnable{ AI_BASE_AGGRO_RANGE = (int)(60 * Float.parseFloat(ConfigManager.MB_AI_AGGRO_RANGE.getValue())); while (true) { for (Zone zone : ZoneManager.getAllZones()) { - - for (Mob mob : zone.zoneMobSet) { - - try { - if (mob != null) - MobAI.DetermineAction(mob); - } catch (Exception e) { - Logger.error("Mob: " + mob.getName() + " UUID: " + mob.getObjectUUID() + " ERROR: " + e); - e.printStackTrace(); + if (zone != null && zone.zoneMobSet != null) { + synchronized (zone.zoneMobSet) { + for (Mob mob : zone.zoneMobSet) { + try { + if (mob != null) { + MobAI.DetermineAction(mob); + } + } catch (Exception e) { + Logger.error("Error processing Mob [Name: {}, UUID: {}]", mob.getName(), mob.getObjectUUID(), e); + } + } } } } + try { + Thread.sleep(100); + } catch (InterruptedException e) { + Logger.error("AI Thread interrupted", e); + Thread.currentThread().interrupt(); + } } } public static void startAIThread() { diff --git a/src/engine/workthreads/BaneThread.java b/src/engine/workthreads/BaneThread.java index ddee5959..fe0c0fca 100644 --- a/src/engine/workthreads/BaneThread.java +++ b/src/engine/workthreads/BaneThread.java @@ -60,7 +60,7 @@ public class BaneThread implements Runnable { lastRun = System.currentTimeMillis(); // Update lastRun after processing }else { try { - Thread.sleep(100); // Pause for 10ms to reduce CPU usage + Thread.sleep(100); // Pause for 100ms to reduce CPU usage } catch (InterruptedException e) { Logger.error("Thread interrupted", e); Thread.currentThread().interrupt();