From 6cc671365045ab3d6e56a978c8f412d2c0b5ac42 Mon Sep 17 00:00:00 2001 From: FatBoy-DOTC Date: Fri, 27 Oct 2023 20:03:15 -0500 Subject: [PATCH] bonus code cleanup --- src/engine/math/Bounds.java | 2 +- .../mobileAI/utilities/MovementUtilities.java | 14 +++++------ src/engine/objects/Building.java | 1 - src/engine/objects/MeshBounds.java | 9 ++++++- src/engine/objects/Regions.java | 10 ++++++++ src/engine/objects/Zone.java | 25 ++++++++++++++++++- 6 files changed, 50 insertions(+), 11 deletions(-) diff --git a/src/engine/math/Bounds.java b/src/engine/math/Bounds.java index cfa3afe6..7a5379f7 100644 --- a/src/engine/math/Bounds.java +++ b/src/engine/math/Bounds.java @@ -41,7 +41,7 @@ public class Bounds { private boolean flipExtents; private ArrayList regions = new ArrayList<>(); - private ArrayList colliders = new ArrayList<>(); + public ArrayList colliders = new ArrayList<>(); // Default constructor diff --git a/src/engine/mobileAI/utilities/MovementUtilities.java b/src/engine/mobileAI/utilities/MovementUtilities.java index c8ca5fac..0750292a 100644 --- a/src/engine/mobileAI/utilities/MovementUtilities.java +++ b/src/engine/mobileAI/utilities/MovementUtilities.java @@ -18,6 +18,7 @@ import engine.exception.MsgSendException; import engine.gameManager.BuildingManager; import engine.gameManager.ChatManager; import engine.gameManager.MovementManager; +import engine.gameManager.ZoneManager; import engine.math.Bounds; import engine.math.Vector3fImmutable; import engine.mobileAI.Threads.MobAIThread; @@ -432,20 +433,19 @@ private static final int cellGap = 4; public static boolean pointIsBlocked(Vector3fImmutable point) { - //TODO figure out best way to decide if a walking point intersects a mesh collider from a building Building building = BuildingManager.getBuildingAtLocation(point); - if(building == null) return false;//no building at this location means nothing obstructing the walking path - if(!collidesWithBuilding(building,point)) - return false; - if(Regions.getRegionAtLocation(point) != null) return false; - - return false; + Zone currentZone = ZoneManager.findSmallestZone(point); + if(currentZone == null) + return false; + else + return currentZone.navMesh.contains(point.x,point.z); + //return false; } private static boolean collidesWithBuilding(Building building, Vector3fImmutable end){ diff --git a/src/engine/objects/Building.java b/src/engine/objects/Building.java index b288f0e2..0e1d9245 100644 --- a/src/engine/objects/Building.java +++ b/src/engine/objects/Building.java @@ -33,7 +33,6 @@ import engine.net.client.msg.ApplyBuildingEffectMsg; import engine.net.client.msg.UpdateObjectMsg; import engine.server.MBServerStatics; import org.pmw.tinylog.Logger; - import java.sql.ResultSet; import java.sql.SQLException; import java.time.LocalDateTime; diff --git a/src/engine/objects/MeshBounds.java b/src/engine/objects/MeshBounds.java index 10688e32..e6eff752 100644 --- a/src/engine/objects/MeshBounds.java +++ b/src/engine/objects/MeshBounds.java @@ -12,6 +12,8 @@ package engine.objects; import engine.gameManager.DbManager; import engine.math.Bounds; +import java.awt.*; +import java.awt.geom.Area; import java.sql.ResultSet; import java.sql.SQLException; @@ -45,5 +47,10 @@ public class MeshBounds { Bounds.meshBoundsCache = DbManager.BuildingQueries.LOAD_MESH_BOUNDS(); } - + public Area getArea(){ + Polygon area = new Polygon(); + area.addPoint((int) this.minX, (int) this.minZ); + area.addPoint((int) this.maxX, (int) this.maxZ); + return new Area(area); + } } diff --git a/src/engine/objects/Regions.java b/src/engine/objects/Regions.java index aa7a5866..36a2fcff 100644 --- a/src/engine/objects/Regions.java +++ b/src/engine/objects/Regions.java @@ -17,6 +17,8 @@ import engine.math.Vector3f; import engine.math.Vector3fImmutable; import engine.server.MBServerStatics; +import java.awt.*; +import java.awt.geom.Area; import java.util.ArrayList; import java.util.HashMap; @@ -360,4 +362,12 @@ public class Regions { public boolean isExit() { return exit; } + + public Area getArea(){ + Polygon area = new Polygon(); + for( Vector3f point : this.regionPoints){ + area.addPoint((int) point.x, (int) point.z); + } + return new Area(area); + } } diff --git a/src/engine/objects/Zone.java b/src/engine/objects/Zone.java index e380a3fd..3c45a9ef 100644 --- a/src/engine/objects/Zone.java +++ b/src/engine/objects/Zone.java @@ -9,17 +9,19 @@ package engine.objects; +import java.awt.geom.Area; import engine.Enum; import engine.InterestManagement.Terrain; import engine.db.archive.DataWarehouse; import engine.gameManager.ZoneManager; import engine.math.Bounds; import engine.math.Vector2f; +import engine.math.Vector3f; import engine.math.Vector3fImmutable; import engine.net.ByteBufferWriter; import engine.server.MBServerStatics; import org.pmw.tinylog.Logger; - +import java.awt.*; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; @@ -65,6 +67,8 @@ public class Zone extends AbstractWorldObject { public Terrain terrain = null; + public Area navMesh; + /** * ResultSet Constructor */ @@ -181,8 +185,27 @@ public class Zone extends AbstractWorldObject { ZoneManager.populateZoneCollections(this); + this.bakeNavMesh(); + } + public void bakeNavMesh(){ + int xPoint = (int)(this.loc.x - this.bounds.getHalfExtents().x); + int zPoint = (int) (this.loc.z - this.bounds.getHalfExtents().y); + int extentsX = (int) (this.bounds.getHalfExtents().x * 2); + int extentsZ = (int) (this.bounds.getHalfExtents().y * 2); + + this.navMesh = new Area(new Rectangle(xPoint, zPoint, extentsX, extentsZ)); + + for(Building building : this.zoneBuildingSet){ + for(Regions region : building.getBounds().getRegions()) + this.navMesh.add(region.getArea()); + + MeshBounds meshBounds = Bounds.meshBoundsCache.get(building.getMeshUUID()); + if(meshBounds != null) + this.navMesh.subtract(meshBounds.getArea()); + } + } /* Method sets a default value for player cities * otherwise using values derived from the loadnum * field in the obj_zone database table.