From 6923251a887d25b004effa3d3f73d1a020dbc3c0 Mon Sep 17 00:00:00 2001 From: MagicBot Date: Sun, 20 Aug 2023 18:56:09 -0400 Subject: [PATCH] dirtyflag chosen as solution. --- .../InterestManagement/InterestManager.java | 53 +++++++++++++------ src/engine/objects/PlayerCharacter.java | 5 +- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/src/engine/InterestManagement/InterestManager.java b/src/engine/InterestManagement/InterestManager.java index a2afdaf5..ead4ee32 100644 --- a/src/engine/InterestManagement/InterestManager.java +++ b/src/engine/InterestManagement/InterestManager.java @@ -30,12 +30,13 @@ import org.pmw.tinylog.Logger; import java.util.ArrayList; import java.util.HashSet; +import static engine.math.FastMath.sqr; + public enum InterestManager implements Runnable { INTERESTMANAGER; private static long lastTime; - private static boolean keepGoing = true; InterestManager() { Logger.info(" Interest Management thread is running."); @@ -155,10 +156,6 @@ public enum InterestManager implements Runnable { } } - public void shutdown() { - this.keepGoing = false; - } - @Override public void run() { beginLoadJob(); @@ -168,7 +165,7 @@ public enum InterestManager implements Runnable { InterestManager.lastTime = System.currentTimeMillis(); - while (InterestManager.keepGoing) { + while (true) { try { updateAllPlayers(); } catch (Exception e) { @@ -232,11 +229,26 @@ public enum InterestManager implements Runnable { private void updateStaticList(PlayerCharacter player, ClientConnection origin) { + + // Only update if we've moved far enough to warrant it + + float distanceSquared = player.getLoc().distanceSquared2D(player.getLastStaticLoc()); + + if (distanceSquared > sqr(25)) { + player.setLastStaticLoc(player.getLoc()); + player.dirtyLoad = true; + } + + if (player.dirtyLoad == false) + return; + // Get Statics in range + HashSet toLoad = WorldGrid.getObjectsInRangePartial(player.getLoc(), MBServerStatics.STRUCTURE_LOAD_RANGE, MBServerStatics.MASK_STATIC); // get list of obects loaded that need removed + HashSet loadedStaticObjects = player.getLoadedStaticObjects(); HashSet toRemove = null; @@ -246,11 +258,16 @@ public enum InterestManager implements Runnable { toRemove.removeAll(toLoad); // unload static objects now out of range + if (toRemove.size() > 0) { + UnloadObjectsMsg uom = new UnloadObjectsMsg(); + for (AbstractWorldObject obj : toRemove) { + if (obj.getObjectType().equals(GameObjectType.Building)) InterestManager.HandleSpecialUnload((Building) obj, origin); + if (obj != null && !obj.equals(player)) uom.addObject(obj); } @@ -262,6 +279,7 @@ public enum InterestManager implements Runnable { loadedStaticObjects.removeAll(toRemove); // remove any object to load that are already loaded + toLoad.removeAll(loadedStaticObjects); LoadStructureMsg lsm = new LoadStructureMsg(); @@ -269,6 +287,7 @@ public enum InterestManager implements Runnable { ArrayList lcmList = new ArrayList<>(); for (AbstractWorldObject awo : toLoad) { + if (awo.getObjectType().equals(GameObjectType.Building)) lsm.addObject((Building) awo); else if (awo.getObjectType().equals(GameObjectType.Corpse)) { @@ -278,7 +297,6 @@ public enum InterestManager implements Runnable { Dispatch dispatch = Dispatch.borrow(player, lcm); DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.PRIMARY); - } else if (awo.getObjectType().equals(GameObjectType.NPC)) { NPC npc = (NPC) awo; lcm = new LoadCharacterMsg(npc, PlayerCharacter.hideNonAscii()); @@ -299,6 +317,7 @@ public enum InterestManager implements Runnable { } loadedStaticObjects.addAll(toLoad); + player.dirtyLoad = false; } private void updateMobileList(PlayerCharacter player, ClientConnection origin) { @@ -407,17 +426,20 @@ public enum InterestManager implements Runnable { ArrayList addToList = new ArrayList<>(); for (AbstractWorldObject awo : toLoadToPlayer) { - // dont load yourself + + try { - if (awo.equals(player)) + if (awo.equals(player)) // dont load yourself continue; if ((awo.getObjectTypeMask() & MBServerStatics.MASK_PLAYER) != 0) { // object to load is a player + PlayerCharacter awopc = (PlayerCharacter) awo; // dont load if invis + if (player.getSeeInvis() < awopc.getHidden()) continue; @@ -444,15 +466,14 @@ public enum InterestManager implements Runnable { continue; //removed, interest manager should still load mob corpses + if (awonpc.despawned == true) continue; awonpc.playerAgroMap.put(player.getObjectUUID(), false); - //MobAI.setAwake(awonpc, false); ((Mob) awonpc).setCombatTarget(null); - // IVarController.setVariable(awonpc, "IntelligenceDisableDelay", (double) (System.currentTimeMillis() + 5000)); - // awonpc.enableIntelligence(); lcm = new LoadCharacterMsg(awonpc, PlayerCharacter.hideNonAscii()); + } else if ((awo.getObjectTypeMask() & MBServerStatics.MASK_NPC) != 0) { NPC awonpc = (NPC) awo; lcm = new LoadCharacterMsg(awonpc, PlayerCharacter.hideNonAscii()); @@ -465,10 +486,8 @@ public enum InterestManager implements Runnable { awonpc.playerAgroMap.put(player.getObjectUUID(), false); if ((awonpc.agentType.equals(Enum.AIAgentType.MOBILE))) - //MobAI.setAwake(awonpc, false); ((Mob) awonpc).setCombatTarget(null); - // IVarController.setVariable(awonpc, "IntelligenceDisableDelay", (double) (System.currentTimeMillis() + 5000)); - // awonpc.enableIntelligence(); + lcm = new LoadCharacterMsg(awonpc, PlayerCharacter.hideNonAscii()); } @@ -504,6 +523,7 @@ public enum InterestManager implements Runnable { return; //Update static list + try { updateStaticList(player, origin); } catch (Exception e) { @@ -511,6 +531,7 @@ public enum InterestManager implements Runnable { } //Update mobile list + try { updateMobileList(player, origin); } catch (Exception e) { @@ -529,6 +550,7 @@ public enum InterestManager implements Runnable { return; //Update static list + try { updateStaticList(player, origin); } catch (Exception e) { @@ -536,6 +558,7 @@ public enum InterestManager implements Runnable { } //Update mobile list + try { updateMobileList(player, origin); } catch (Exception e) { diff --git a/src/engine/objects/PlayerCharacter.java b/src/engine/objects/PlayerCharacter.java index ba0f4223..d356f77b 100644 --- a/src/engine/objects/PlayerCharacter.java +++ b/src/engine/objects/PlayerCharacter.java @@ -181,6 +181,8 @@ public class PlayerCharacter extends AbstractCharacter { private boolean lastSwimming = false; private boolean isTeleporting = false; + public boolean dirtyLoad = false; + /** * No Id Constructor */ @@ -5501,8 +5503,10 @@ public class PlayerCharacter extends AbstractCharacter { @Override public final void teleport(final Vector3fImmutable targetLoc) { + Regions targetRegion = Regions.GetRegionForTeleport(targetLoc); locationLock.writeLock().lock(); + try { MovementManager.translocate(this, targetLoc, targetRegion); } catch (Exception e) { @@ -5519,7 +5523,6 @@ public class PlayerCharacter extends AbstractCharacter { public float getBargain() { float bargain = 0; - CharacterSkill bargainSkill = this.getSkills().get(engine.Enum.CharacterSkills.Bargaining.name()); if (bargainSkill != null)