diff --git a/docs/src/progress/renderertodo.md b/docs/src/progress/renderertodo.md index 4523ff4f..1227db1c 100644 --- a/docs/src/progress/renderertodo.md +++ b/docs/src/progress/renderertodo.md @@ -2076,6 +2076,7 @@ Reorganizing macro classes Start work on macro pathfinding storage Town constructs nav graph of road nodes Render pathing nodes (still needs some work) +Pathing construction between town buildings and road nodes diff --git a/src/main/java/electrosphere/server/macro/civilization/town/TownLayout.java b/src/main/java/electrosphere/server/macro/civilization/town/TownLayout.java index b9e674ec..bf18e9e7 100644 --- a/src/main/java/electrosphere/server/macro/civilization/town/TownLayout.java +++ b/src/main/java/electrosphere/server/macro/civilization/town/TownLayout.java @@ -295,7 +295,17 @@ public class TownLayout { //build road and structures between curr and next node if(closedSet.contains(newHash)){ if(nearPoint.distance(townCenter) < TOWN_CENTER_RADIUS && currPoint.distance(townCenter) < TOWN_CENTER_RADIUS){ - TownLayout.generateStructuresAlongRoad(realm, town, nearPoint, currPoint, Road.DEFAULT_RADIUS, allowedStructures); + MacroPathNode roadPoint1 = positionNodeMap.get(openHash); + MacroPathNode roadPoint2 = positionNodeMap.get(newHash); + if(roadPoint1 == null || roadPoint2 == null){ + throw new Error("Failed to resolve road points! " + roadPoint1 + " " + roadPoint2); + } + TownLayout.generateStructuresAlongRoad( + realm, town, + roadPoint1, roadPoint2, + nearPoint, currPoint, Road.DEFAULT_RADIUS, + allowedStructures + ); } } } @@ -379,8 +389,14 @@ public class TownLayout { * @param road The road * @param allowedStructures The list of allowed structure types */ - private static void generateStructuresAlongRoad(Realm realm, Town town, Vector3d startPoint, Vector3d endPoint, double radius, List allowedStructures){ + private static void generateStructuresAlongRoad( + Realm realm, Town town, + MacroPathNode roadPoint1, MacroPathNode roadPoint2, + Vector3d startPoint, Vector3d endPoint, double radius, + List allowedStructures + ){ MacroData macroData = realm.getMacroData(); + MacroPathCache pathCache = macroData.getPathCache(); //get values to scan along int len = (int)startPoint.distance(endPoint); @@ -449,6 +465,10 @@ public class TownLayout { if(!macroData.intersectsStruct(aabb)){ VirtualStructure struct = VirtualStructure.createStructure(macroData, structureData, currPos, rotation1); town.addStructure(struct); + //create pathing node for structure and link it to nearest town centers + MacroPathNode structNode = MacroPathNode.create(pathCache, struct, new Vector3d(currPos)); + structNode.addNeighbor(roadPoint1); + structNode.addNeighbor(roadPoint2); //TODO: once have successfully placed this structure type, pick a new one to place } } @@ -477,6 +497,10 @@ public class TownLayout { if(!macroData.intersectsStruct(aabb)){ VirtualStructure struct = VirtualStructure.createStructure(macroData, structureData, currPos, rotation2); town.addStructure(struct); + //create pathing node for structure and link it to nearest town centers + MacroPathNode structNode = MacroPathNode.create(pathCache, struct, new Vector3d(currPos)); + structNode.addNeighbor(roadPoint1); + structNode.addNeighbor(roadPoint2); //TODO: once have successfully placed this structure type, pick a new one to place } } diff --git a/src/main/java/electrosphere/server/macro/spatial/path/MacroPathCache.java b/src/main/java/electrosphere/server/macro/spatial/path/MacroPathCache.java index 284c7ef7..b445e292 100644 --- a/src/main/java/electrosphere/server/macro/spatial/path/MacroPathCache.java +++ b/src/main/java/electrosphere/server/macro/spatial/path/MacroPathCache.java @@ -50,4 +50,14 @@ public class MacroPathCache { return idNodeMap.get(id); } + /** + * Registers a node with the pathing cache + * @param node The node + */ + public void registerNode(MacroPathNode node){ + node.setId(nodes.size()); + nodes.add(node); + idNodeMap.put(node.getId(),node); + } + } diff --git a/src/main/java/electrosphere/server/macro/spatial/path/MacroPathNode.java b/src/main/java/electrosphere/server/macro/spatial/path/MacroPathNode.java index 4c7c69a2..4c315098 100644 --- a/src/main/java/electrosphere/server/macro/spatial/path/MacroPathNode.java +++ b/src/main/java/electrosphere/server/macro/spatial/path/MacroPathNode.java @@ -102,8 +102,7 @@ public class MacroPathNode { */ public static MacroPathNode create(MacroPathCache cache, Object correspondingObject, Vector3d position){ MacroPathNode rVal = new MacroPathNode(); - rVal.setId(cache.getNodes().size()); - cache.getNodes().add(rVal); + cache.registerNode(rVal); //set data on the node rVal.position = position; @@ -132,8 +131,7 @@ public class MacroPathNode { */ public static MacroPathNode createRoadNode(MacroPathCache cache, Vector3d position){ MacroPathNode rVal = new MacroPathNode(); - rVal.setId(cache.getNodes().size()); - cache.getNodes().add(rVal); + cache.registerNode(rVal); //set data on the node rVal.position = position; @@ -207,8 +205,12 @@ public class MacroPathNode { * @param neighbor The neighbor */ public void addNeighbor(MacroPathNode neighbor){ + if(neighbor == null){ + throw new Error("Invalid neighbor! " + neighbor); + } if(!this.neighborNodes.contains(neighbor.getId())){ this.neighborNodes.add(neighbor.getId()); + neighbor.addNeighbor(this); } }