From cee400551357875e4aef00aa998b16d0ab706d86 Mon Sep 17 00:00:00 2001 From: SenseiKiwi Date: Sun, 29 Dec 2013 21:31:10 -0400 Subject: [PATCH] Reduced Floor Doorways Added code to minimize the number of doorways that involve dropping through the floor. Added a DisjointSet class as part of the implementation. I also split the maze construction process into two classes (MazeDesigner and MazeBuilder) to make it clearer. --- .../experimental/DisjointSet.java | 129 +++++++++++++ .../experimental/MazeBuilder.java | 137 +++++++++++++ .../experimental/MazeDesign.java | 48 +++++ .../{MazeGenerator.java => MazeDesigner.java} | 181 ++++++++---------- .../mod_pocketDim/world/PocketBuilder.java | 4 +- 5 files changed, 396 insertions(+), 103 deletions(-) create mode 100644 src/main/java/StevenDimDoors/experimental/DisjointSet.java create mode 100644 src/main/java/StevenDimDoors/experimental/MazeBuilder.java create mode 100644 src/main/java/StevenDimDoors/experimental/MazeDesign.java rename src/main/java/StevenDimDoors/experimental/{MazeGenerator.java => MazeDesigner.java} (74%) diff --git a/src/main/java/StevenDimDoors/experimental/DisjointSet.java b/src/main/java/StevenDimDoors/experimental/DisjointSet.java new file mode 100644 index 00000000..fc15269a --- /dev/null +++ b/src/main/java/StevenDimDoors/experimental/DisjointSet.java @@ -0,0 +1,129 @@ +package StevenDimDoors.experimental; + +import java.util.HashMap; + +public class DisjointSet +{ + // This class implements a disjoint set data structure that associates objects with sets. + + private static class SetNode

+ { + private int rank; + private SetNode

parent; + private P data; + + public SetNode(P data) + { + this.data = data; + this.rank = 0; + this.parent = null; + } + } + + private HashMap> mapping; + + public DisjointSet(int initialCapacity) + { + mapping = new HashMap>(initialCapacity); + } + + public boolean makeSet(T element) + { + if (!mapping.containsKey(element)) + { + mapping.put(element, new SetNode(element)); + return true; + } + else + { + return false; + } + } + + private SetNode findRootNode(T element) + { + SetNode node = mapping.get(element); + if (node == null) + { + return null; + } + if (node.parent != null) + { + node.parent = findRootNode(node.parent); + return node.parent; + } + else + { + return node; + } + } + + private SetNode findRootNode(SetNode node) + { + if (node.parent != null) + { + node.parent = findRootNode(node.parent); + return node.parent; + } + else + { + return node; + } + } + + public boolean mergeSets(T first, T second) + { + SetNode firstRoot = findRootNode(first); + SetNode secondRoot = findRootNode(second); + + if (firstRoot == null || secondRoot == null || + firstRoot == secondRoot) + { + return false; + } + + if (firstRoot.rank < secondRoot.rank) + { + firstRoot.parent = secondRoot; + } + else if (firstRoot.rank > secondRoot.rank) + { + secondRoot.parent = firstRoot; + } + else + { + secondRoot.parent = firstRoot; + firstRoot.rank++; + } + return true; + } + + public T find(T element) + { + SetNode root = findRootNode(element); + + if (root != null) + { + return root.data; + } + else + { + return null; + } + } + + public boolean haveSameSet(T first, T second) + { + SetNode firstRoot = findRootNode(first); + SetNode secondRoot = findRootNode(second); + + if (firstRoot == null || secondRoot == null) + { + return false; + } + else + { + return (firstRoot == secondRoot); + } + } +} diff --git a/src/main/java/StevenDimDoors/experimental/MazeBuilder.java b/src/main/java/StevenDimDoors/experimental/MazeBuilder.java new file mode 100644 index 00000000..dd4142cb --- /dev/null +++ b/src/main/java/StevenDimDoors/experimental/MazeBuilder.java @@ -0,0 +1,137 @@ +package StevenDimDoors.experimental; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Random; + +import net.minecraft.block.Block; +import net.minecraft.util.MathHelper; +import net.minecraft.world.World; +import net.minecraft.world.chunk.Chunk; +import net.minecraft.world.chunk.storage.ExtendedBlockStorage; +import StevenDimDoors.mod_pocketDim.Point3D; + +public class MazeBuilder +{ + private MazeBuilder() { } + + public static void generate(World world, int x, int y, int z, Random random) + { + MazeDesign design = MazeDesigner.generate(random); + + buildRooms(design.getRoomGraph(), world, + new Point3D(x - design.width() / 2, y - design.height() - 1, z - design.length() / 2)); + } + + private static void buildRooms(DirectedGraph roomGraph, World world, Point3D offset) + { + for (IGraphNode node : roomGraph.nodes()) + { + PartitionNode room = node.data(); + buildBox(world, offset, room.minCorner(), room.maxCorner(), Block.stoneBrick.blockID, 0); + } + + // TESTING!!! + // This code carves out cheap doorways + // The final system will be better + // This has to happen after all the rooms have been built or the passages will be overwritten sometimes + for (IGraphNode node : roomGraph.nodes()) + { + for (IEdge doorway : node.outbound()) + { + char axis = doorway.data().axis(); + Point3D lower = doorway.data().minCorner(); + + if (axis == DoorwayData.Z_AXIS) + { + setBlockDirectly(world, offset.getX() + lower.getX() + 1, offset.getY() + lower.getY() + 1, offset.getZ() + lower.getZ(), 0, 0); + setBlockDirectly(world, offset.getX() + lower.getX() + 1, offset.getY() + lower.getY() + 2, offset.getZ() + lower.getZ(), 0, 0); + setBlockDirectly(world, offset.getX() + lower.getX() + 1, offset.getY() + lower.getY() + 1, offset.getZ() + lower.getZ() + 1, 0, 0); + setBlockDirectly(world, offset.getX() + lower.getX() + 1, offset.getY() + lower.getY() + 2, offset.getZ() + lower.getZ() + 1, 0, 0); + } + else if (axis == DoorwayData.X_AXIS) + { + setBlockDirectly(world, offset.getX() + lower.getX(), offset.getY() + lower.getY() + 1, offset.getZ() + lower.getZ() + 1, 0, 0); + setBlockDirectly(world, offset.getX() + lower.getX(), offset.getY() + lower.getY() + 2, offset.getZ() + lower.getZ() + 1, 0, 0); + setBlockDirectly(world, offset.getX() + lower.getX() + 1, offset.getY() + lower.getY() + 1, offset.getZ() + lower.getZ() + 1, 0, 0); + setBlockDirectly(world, offset.getX() + lower.getX() + 1, offset.getY() + lower.getY() + 2, offset.getZ() + lower.getZ() + 1, 0, 0); + } + else + { + setBlockDirectly(world, offset.getX() + lower.getX() + 1, offset.getY() + lower.getY(), offset.getZ() + lower.getZ() + 1, 0, 0); + setBlockDirectly(world, offset.getX() + lower.getX() + 1, offset.getY() + lower.getY(), offset.getZ() + lower.getZ() + 1, 0, 0); + setBlockDirectly(world, offset.getX() + lower.getX() + 1, offset.getY() + lower.getY() + 1, offset.getZ() + lower.getZ() + 1, 0, 0); + setBlockDirectly(world, offset.getX() + lower.getX() + 1, offset.getY() + lower.getY() + 1, offset.getZ() + lower.getZ() + 1, 0, 0); + } + } + } + } + + private static void buildBox(World world, Point3D offset, Point3D minCorner, Point3D maxCorner, int blockID, int metadata) + { + int minX = minCorner.getX() + offset.getX(); + int minY = minCorner.getY() + offset.getY(); + int minZ = minCorner.getZ() + offset.getZ(); + + int maxX = maxCorner.getX() + offset.getX(); + int maxY = maxCorner.getY() + offset.getY(); + int maxZ = maxCorner.getZ() + offset.getZ(); + + int x, y, z; + + for (x = minX; x <= maxX; x++) + { + for (z = minZ; z <= maxZ; z++) + { + setBlockDirectly(world, x, minY, z, blockID, metadata); + setBlockDirectly(world, x, maxY, z, blockID, metadata); + } + } + for (x = minX; x <= maxX; x++) + { + for (y = minY; y <= maxY; y++) + { + setBlockDirectly(world, x, y, minZ, blockID, metadata); + setBlockDirectly(world, x, y, maxZ, blockID, metadata); + } + } + for (z = minZ; z <= maxZ; z++) + { + for (y = minY; y <= maxY; y++) + { + setBlockDirectly(world, minX, y, z, blockID, metadata); + setBlockDirectly(world, maxX, y, z, blockID, metadata); + } + } + } + + private static void setBlockDirectly(World world, int x, int y, int z, int blockID, int metadata) + { + if (blockID != 0 && Block.blocksList[blockID] == null) + { + return; + } + + int cX = x >> 4; + int cZ = z >> 4; + int cY = y >> 4; + Chunk chunk; + + int localX = (x % 16) < 0 ? (x % 16) + 16 : (x % 16); + int localZ = (z % 16) < 0 ? (z % 16) + 16 : (z % 16); + ExtendedBlockStorage extBlockStorage; + + chunk = world.getChunkFromChunkCoords(cX, cZ); + extBlockStorage = chunk.getBlockStorageArray()[cY]; + if (extBlockStorage == null) + { + extBlockStorage = new ExtendedBlockStorage(cY << 4, !world.provider.hasNoSky); + chunk.getBlockStorageArray()[cY] = extBlockStorage; + } + extBlockStorage.setExtBlockID(localX, y & 15, localZ, blockID); + extBlockStorage.setExtBlockMetadata(localX, y & 15, localZ, metadata); + } +} diff --git a/src/main/java/StevenDimDoors/experimental/MazeDesign.java b/src/main/java/StevenDimDoors/experimental/MazeDesign.java new file mode 100644 index 00000000..d71b9566 --- /dev/null +++ b/src/main/java/StevenDimDoors/experimental/MazeDesign.java @@ -0,0 +1,48 @@ +package StevenDimDoors.experimental; + +import java.util.ArrayList; + +public class MazeDesign +{ + private PartitionNode root; + private DirectedGraph rooms; + private ArrayList> cores; + + public MazeDesign(PartitionNode root, DirectedGraph rooms, + ArrayList> cores) + { + this.root = root; + this.rooms = rooms; + this.cores = cores; + } + + public PartitionNode getRootPartition() + { + return root; + } + + public DirectedGraph getRoomGraph() + { + return rooms; + } + + public ArrayList> getCoreNodes() + { + return cores; + } + + public int width() + { + return root.width(); + } + + public int height() + { + return root.height(); + } + + public int length() + { + return root.length(); + } +} diff --git a/src/main/java/StevenDimDoors/experimental/MazeGenerator.java b/src/main/java/StevenDimDoors/experimental/MazeDesigner.java similarity index 74% rename from src/main/java/StevenDimDoors/experimental/MazeGenerator.java rename to src/main/java/StevenDimDoors/experimental/MazeDesigner.java index 0911c9a7..d74db285 100644 --- a/src/main/java/StevenDimDoors/experimental/MazeGenerator.java +++ b/src/main/java/StevenDimDoors/experimental/MazeDesigner.java @@ -6,29 +6,26 @@ import java.util.HashMap; import java.util.LinkedList; import java.util.Queue; import java.util.Random; +import java.util.Stack; -import net.minecraft.block.Block; import net.minecraft.util.MathHelper; -import net.minecraft.world.World; -import net.minecraft.world.chunk.Chunk; -import net.minecraft.world.chunk.storage.ExtendedBlockStorage; import StevenDimDoors.mod_pocketDim.Point3D; -public class MazeGenerator +public class MazeDesigner { - public static final int ROOT_WIDTH = 40; - public static final int ROOT_LENGTH = 40; - public static final int ROOT_HEIGHT = 20; + private static final int MAZE_WIDTH = 40; + private static final int MAZE_LENGTH = 40; + private static final int MAZE_HEIGHT = 20; private static final int MIN_HEIGHT = 4; private static final int MIN_SIDE = 3; private static final int SPLIT_COUNT = 9; - private MazeGenerator() { } + private MazeDesigner() { } - public static void generate(World world, int x, int y, int z, Random random) + public static MazeDesign generate(Random random) { // Construct a random binary space partitioning of our maze volume - PartitionNode root = partitionRooms(ROOT_WIDTH, ROOT_HEIGHT, ROOT_LENGTH, SPLIT_COUNT, random); + PartitionNode root = partitionRooms(MAZE_WIDTH, MAZE_HEIGHT, MAZE_LENGTH, SPLIT_COUNT, random); // List all the leaf nodes of the partition tree, which denote individual rooms ArrayList partitions = new ArrayList(1 << SPLIT_COUNT); @@ -42,10 +39,15 @@ public class MazeGenerator // Cut out random subgraphs from the adjacency graph ArrayList> cores = createMazeSections(rooms, random); + // Remove unnecessary passages through floors/ceilings + for (IGraphNode core : cores) + { + minimizeVerticalPassages(core, rooms, random); + } - buildRooms(rooms, world, new Point3D(x - ROOT_WIDTH / 2, y - ROOT_HEIGHT - 1, z - ROOT_WIDTH / 2)); + return new MazeDesign(root, rooms, cores); } - + private static void listRoomPartitions(PartitionNode node, ArrayList partitions) { if (node.isLeaf()) @@ -59,7 +61,7 @@ public class MazeGenerator } } - private static void removeRoom(PartitionNode node) + private static void removeRoomPartitions(PartitionNode node) { // Remove a node and any of its ancestors that become leaf nodes PartitionNode parent; @@ -448,119 +450,96 @@ public class MazeGenerator } // Remove all the nodes that were listed for removal + // Also remove unused partitions from the partition tree for (IGraphNode node : removals) { + removeRoomPartitions(node.data()); roomGraph.removeNode(node); } return cores; } - private static void buildRooms(DirectedGraph roomGraph, World world, Point3D offset) + private static void minimizeVerticalPassages(IGraphNode core, + DirectedGraph rooms, Random random) { - for (IGraphNode node : roomGraph.nodes()) + // We receive a node for one of the rooms in a section of the maze + // and we need to remove as many floor doorways as possible while + // still allowing any room to be reachable from any other room. + // In technical terms, we receive a node from a connected subgraph + // and we need to remove as many Y_AXIS-type edges as possible while + // preserving connectedness. + + // An efficient solution is to assign nodes to disjoint sets based + // on their components, ignoring all Y_AXIS edges, then iterate over + // a list of those edges and remove them if they connect two nodes + // in the same set. Otherwise, merge their sets and keep the edge. + // This is similar to algorithms for spanning trees. + + // First, list all nodes in the subgraph + IGraphNode current; + IGraphNode neighbor; + + Stack> ordering = new Stack>(); + ArrayList> subgraph = new ArrayList>(64); + DisjointSet> components = new DisjointSet>(128); + + ordering.add(core); + components.makeSet(core); + while (!ordering.isEmpty()) { - PartitionNode room = node.data(); - buildBox(world, offset, room.minCorner(), room.maxCorner(), Block.stoneBrick.blockID, 0); + current = ordering.pop(); + subgraph.add(current); + + for (IEdge edge : current.inbound()) + { + neighbor = edge.head(); + if (components.makeSet(neighbor)) + { + ordering.add(neighbor); + } + } + for (IEdge edge : current.outbound()) + { + neighbor = edge.tail(); + if (components.makeSet(neighbor)) + { + ordering.add(neighbor); + } + } } - // TESTING!!! - // This code carves out cheap doorways - // The final system will be better - // This has to happen after all the rooms have been built or the passages will be overwritten sometimes - for (IGraphNode node : roomGraph.nodes()) + // Now iterate over the list of nodes and merge their sets + // We only have to look at outbound edges since inbound edges mirror them + // Also list any Y_AXIS doorways we come across + ArrayList> targets = + new ArrayList>(); + + for (IGraphNode room : subgraph) { - for (IEdge doorway : node.outbound()) + for (IEdge passage : room.outbound()) { - char axis = doorway.data().axis(); - Point3D lower = doorway.data().minCorner(); - - if (axis == DoorwayData.Z_AXIS) + if (passage.data().axis() != DoorwayData.Y_AXIS) { - setBlockDirectly(world, offset.getX() + lower.getX() + 1, offset.getY() + lower.getY() + 1, offset.getZ() + lower.getZ(), 0, 0); - setBlockDirectly(world, offset.getX() + lower.getX() + 1, offset.getY() + lower.getY() + 2, offset.getZ() + lower.getZ(), 0, 0); - setBlockDirectly(world, offset.getX() + lower.getX() + 1, offset.getY() + lower.getY() + 1, offset.getZ() + lower.getZ() + 1, 0, 0); - setBlockDirectly(world, offset.getX() + lower.getX() + 1, offset.getY() + lower.getY() + 2, offset.getZ() + lower.getZ() + 1, 0, 0); - } - else if (axis == DoorwayData.X_AXIS) - { - setBlockDirectly(world, offset.getX() + lower.getX(), offset.getY() + lower.getY() + 1, offset.getZ() + lower.getZ() + 1, 0, 0); - setBlockDirectly(world, offset.getX() + lower.getX(), offset.getY() + lower.getY() + 2, offset.getZ() + lower.getZ() + 1, 0, 0); - setBlockDirectly(world, offset.getX() + lower.getX() + 1, offset.getY() + lower.getY() + 1, offset.getZ() + lower.getZ() + 1, 0, 0); - setBlockDirectly(world, offset.getX() + lower.getX() + 1, offset.getY() + lower.getY() + 2, offset.getZ() + lower.getZ() + 1, 0, 0); + components.mergeSets(passage.head(), passage.tail()); } else { - setBlockDirectly(world, offset.getX() + lower.getX() + 1, offset.getY() + lower.getY(), offset.getZ() + lower.getZ() + 1, 0, 0); - setBlockDirectly(world, offset.getX() + lower.getX() + 1, offset.getY() + lower.getY(), offset.getZ() + lower.getZ() + 1, 0, 0); - setBlockDirectly(world, offset.getX() + lower.getX() + 1, offset.getY() + lower.getY() + 1, offset.getZ() + lower.getZ() + 1, 0, 0); - setBlockDirectly(world, offset.getX() + lower.getX() + 1, offset.getY() + lower.getY() + 1, offset.getZ() + lower.getZ() + 1, 0, 0); + targets.add(passage); } } } - } - - private static void buildBox(World world, Point3D offset, Point3D minCorner, Point3D maxCorner, int blockID, int metadata) - { - int minX = minCorner.getX() + offset.getX(); - int minY = minCorner.getY() + offset.getY(); - int minZ = minCorner.getZ() + offset.getZ(); - int maxX = maxCorner.getX() + offset.getX(); - int maxY = maxCorner.getY() + offset.getY(); - int maxZ = maxCorner.getZ() + offset.getZ(); + // Shuffle the list of doorways to randomize which ones are removed + Collections.shuffle(targets, random); - int x, y, z; - - for (x = minX; x <= maxX; x++) + // Merge sets together and remove unnecessary doorways + for (IEdge passage : targets) { - for (z = minZ; z <= maxZ; z++) + if (!components.mergeSets(passage.head(), passage.tail())) { - setBlockDirectly(world, x, minY, z, blockID, metadata); - setBlockDirectly(world, x, maxY, z, blockID, metadata); - } - } - for (x = minX; x <= maxX; x++) - { - for (y = minY; y <= maxY; y++) - { - setBlockDirectly(world, x, y, minZ, blockID, metadata); - setBlockDirectly(world, x, y, maxZ, blockID, metadata); - } - } - for (z = minZ; z <= maxZ; z++) - { - for (y = minY; y <= maxY; y++) - { - setBlockDirectly(world, minX, y, z, blockID, metadata); - setBlockDirectly(world, maxX, y, z, blockID, metadata); + rooms.removeEdge(passage); } } } - private static void setBlockDirectly(World world, int x, int y, int z, int blockID, int metadata) - { - if (blockID != 0 && Block.blocksList[blockID] == null) - { - return; - } - - int cX = x >> 4; - int cZ = z >> 4; - int cY = y >> 4; - Chunk chunk; - - int localX = (x % 16) < 0 ? (x % 16) + 16 : (x % 16); - int localZ = (z % 16) < 0 ? (z % 16) + 16 : (z % 16); - ExtendedBlockStorage extBlockStorage; - - chunk = world.getChunkFromChunkCoords(cX, cZ); - extBlockStorage = chunk.getBlockStorageArray()[cY]; - if (extBlockStorage == null) - { - extBlockStorage = new ExtendedBlockStorage(cY << 4, !world.provider.hasNoSky); - chunk.getBlockStorageArray()[cY] = extBlockStorage; - } - extBlockStorage.setExtBlockID(localX, y & 15, localZ, blockID); - extBlockStorage.setExtBlockMetadata(localX, y & 15, localZ, metadata); - } } diff --git a/src/main/java/StevenDimDoors/mod_pocketDim/world/PocketBuilder.java b/src/main/java/StevenDimDoors/mod_pocketDim/world/PocketBuilder.java index f807bde0..061f2921 100644 --- a/src/main/java/StevenDimDoors/mod_pocketDim/world/PocketBuilder.java +++ b/src/main/java/StevenDimDoors/mod_pocketDim/world/PocketBuilder.java @@ -8,7 +8,7 @@ import net.minecraft.world.World; import net.minecraft.world.chunk.Chunk; import net.minecraft.world.chunk.storage.ExtendedBlockStorage; import net.minecraftforge.common.DimensionManager; -import StevenDimDoors.experimental.MazeGenerator; +import StevenDimDoors.experimental.MazeBuilder; import StevenDimDoors.mod_pocketDim.DDProperties; import StevenDimDoors.mod_pocketDim.Point3D; import StevenDimDoors.mod_pocketDim.blocks.IDimDoor; @@ -484,7 +484,7 @@ public class PocketBuilder } */ - MazeGenerator.generate(world, x, y, z, random); + MazeBuilder.generate(world, x, y, z, random); //Build the door int doorOrientation = BlockRotator.transformMetadata(BlockRotator.EAST_DOOR_METADATA, orientation - BlockRotator.EAST_DOOR_METADATA + 2, properties.DimensionalDoorID);