Class DungeonTools

java.lang.Object
com.github.yellowstonegames.place.DungeonTools

public class DungeonTools extends Object
A static class that can be used to modify the char[][] dungeons that other generators produce. Includes various utilities for random floor-finding, but also provides ways to take dungeons that use '#' for walls and make a copy that uses unicode box drawing characters.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Constant for environment tiles that are floors for a corridor.
    static final int
    Constant for environment tiles that are walls near a corridor.
    static final int
    Constant for environment tiles that are floors for a cave or other natural part of a map.
    static final int
    Constant for environment tiles that are walls near a cave or other natural part of a map.
    static final int
    Constant for environment tiles that are floors for a room.
    static final int
    Constant for environment tiles that are walls near a room.
    static final int
    Constant for environment tiles that are not near a cave, room, or corridor.
  • Method Summary

    Modifier and Type
    Method
    Description
    static com.github.tommyettinger.ds.ObjectList<com.github.yellowstonegames.grid.Coord>
    allMatching(char[][] map, char... matching)
     
    static List<com.github.yellowstonegames.grid.Coord>
    circle(int x, int y, int radius, List<com.github.yellowstonegames.grid.Coord> buf)
    Gets a List of Coord that are within radius distance of (x,y), and appends them to buf if it is non-null or makes a fresh List to append to otherwise.
    static char[][]
    closeDoors(char[][] map)
    When a map is generated by DungeonGenerator with addDoors enabled, different chars are used for vertical and horizontal doors ('+' for vertical and '/' for horizontal).
    static int
    countCells(char[][] level, char match)
    Quickly counts the number of char elements in level that are equal to match.
    static void
    debugPrint(char[][] level)
    For when you want to print a 2D char array.
    static char[][]
    doubleWidth(char[][] map)
    Takes a dungeon map with either '#' as the only wall character or the unicode box drawing characters used by hashesToLines(), and returns a new char[][] dungeon map with two characters per cell, mostly filling the spaces next to non-walls with space characters, and only doing anything different if a box-drawing character would continue into an adjacent cell, or if a '#' wall needs another '#' wall next to it.
    static com.github.tommyettinger.ds.ObjectList<com.github.yellowstonegames.grid.Coord>
    ensurePath(char[][] map, com.github.tommyettinger.random.EnhancedRandom rng, char replacement, char... blocking)
    Ensures a path exists in a rough ring around the map by first creating the path (using pointPath(int, int, EnhancedRandom) with the given EnhancedRandom), then finding chars in blocking that are on that path and replacing them with replacement.
    static boolean
    inLevel(char[][] level, int x, int y)
     
    static boolean
    inLevel(char[][] level, com.github.yellowstonegames.grid.Coord c)
     
    static boolean
    inLevel(float[][] level, int x, int y)
     
    static boolean
    inLevel(float[][] level, com.github.yellowstonegames.grid.Coord c)
     
    static <T> boolean
    inLevel(T[][] level, int x, int y)
     
    static <T> boolean
    inLevel(T[][] level, com.github.yellowstonegames.grid.Coord c)
     
    static char[][]
    openDoors(char[][] map)
    When a map is generated by DungeonGenerator with addDoors enabled, different chars are used for vertical and horizontal doors ('+' for vertical and '/' for horizontal).
    static com.github.tommyettinger.ds.ObjectList<com.github.yellowstonegames.grid.Coord>
    pointPath(int width, int height, com.github.tommyettinger.random.EnhancedRandom rng)
     
    static char[][]
    simplifyDungeon(char[][] map)
    Takes a char[][] dungeon map and returns a copy with all box drawing chars, special placeholder chars, or '#' chars changed to '#' and everything else changed to '.' .
    static char[][]
    unDoubleWidth(char[][] map)
    Takes a dungeon map that uses two characters per cell, and condenses it to use only the left (lower index) character in each cell.
    static char[][]
    wallWrap(char[][] map)
    Changes the outer edge of a char[][] to the wall char, '#'.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • UNTOUCHED

      public static final int UNTOUCHED
      Constant for environment tiles that are not near a cave, room, or corridor. Value is 0. Used by several classes that distinguish types of dungeon environment.
      See Also:
    • ROOM_FLOOR

      public static final int ROOM_FLOOR
      Constant for environment tiles that are floors for a room. Value is 1. Used by several classes that distinguish types of dungeon environment.
      See Also:
    • ROOM_WALL

      public static final int ROOM_WALL
      Constant for environment tiles that are walls near a room. Value is 2. Used by several classes that distinguish types of dungeon environment.
      See Also:
    • NATURAL_FLOOR

      public static final int NATURAL_FLOOR
      Constant for environment tiles that are floors for a cave or other natural part of a map. Value is 3. Used by several classes that distinguish types of dungeon environment. Also used by WildernessGenerator for almost everything it generates.
      See Also:
    • NATURAL_WALL

      public static final int NATURAL_WALL
      Constant for environment tiles that are walls near a cave or other natural part of a map. Value is 4. Used by several classes that distinguish types of dungeon environment. May be used by WildernessGenerator for ledges and other natural obstacles.
      See Also:
    • CORRIDOR_FLOOR

      public static final int CORRIDOR_FLOOR
      Constant for environment tiles that are floors for a corridor. Value is 5. Used by several classes that distinguish types of dungeon environment.
      See Also:
    • CORRIDOR_WALL

      public static final int CORRIDOR_WALL
      Constant for environment tiles that are walls near a corridor. Value is 6. Used by several classes that distinguish types of dungeon environment.
      See Also:
  • Method Details

    • closeDoors

      public static char[][] closeDoors(char[][] map)
      When a map is generated by DungeonGenerator with addDoors enabled, different chars are used for vertical and horizontal doors ('+' for vertical and '/' for horizontal). This makes all doors '+', which is useful if you want '/' to be used for a different purpose and/or to distinguish open and closed doors.
      Parameters:
      map - a char[][] that may have both '+' and '/' for doors
      Returns:
      a char[][] that only uses '+' for all doors
    • openDoors

      public static char[][] openDoors(char[][] map)
      When a map is generated by DungeonGenerator with addDoors enabled, different chars are used for vertical and horizontal doors ('+' for vertical and '/' for horizontal). This makes all doors '/', which is useful if you want '+' to be used for a different purpose and/or to distinguish open and closed doors.
      Parameters:
      map - a char[][] that may have both '+' and '/' for doors
      Returns:
      a char[][] that only uses '/' for all doors
    • simplifyDungeon

      public static char[][] simplifyDungeon(char[][] map)
      Takes a char[][] dungeon map and returns a copy with all box drawing chars, special placeholder chars, or '#' chars changed to '#' and everything else changed to '.' .
      Parameters:
      map - a char[][] with different characters that can be simplified to "wall" or "floor"
      Returns:
      a copy of map with all box-drawing, placeholder, wall or space characters as '#' and everything else '.'
    • doubleWidth

      public static char[][] doubleWidth(char[][] map)
      Takes a dungeon map with either '#' as the only wall character or the unicode box drawing characters used by hashesToLines(), and returns a new char[][] dungeon map with two characters per cell, mostly filling the spaces next to non-walls with space characters, and only doing anything different if a box-drawing character would continue into an adjacent cell, or if a '#' wall needs another '#' wall next to it. The recommended approach is to keep both the original non-double-width map and the newly-returned double-width map, since the single-width maps can be used more easily for pathfinding. If you need to undo this function, call unDoubleWidth().
      Parameters:
      map - a char[][] that uses either '#' or box-drawing characters for walls, but one per cell
      Returns:
      a widened copy of map that uses two characters for every cell, connecting box-drawing chars correctly
    • unDoubleWidth

      public static char[][] unDoubleWidth(char[][] map)
      Takes a dungeon map that uses two characters per cell, and condenses it to use only the left (lower index) character in each cell. This should (probably) only be called on the result of doubleWidth(), and will throw an exception if called on a map with an odd number of characters for width, such as "#...#" .
      Parameters:
      map - a char[][] that has been widened by doubleWidth()
      Returns:
      a copy of map that uses only one char per cell
    • inLevel

      public static boolean inLevel(char[][] level, com.github.yellowstonegames.grid.Coord c)
      Parameters:
      level - dungeon/map level as 2D char array. x,y indexed
      c - Coord to check
      Returns:
      true if c is valid in level, false otherwise.
    • inLevel

      public static boolean inLevel(char[][] level, int x, int y)
      Parameters:
      level - dungeon/map level as 2D char array. x,y indexed
      x - x coordinate to check
      y - y coordinate to check
      Returns:
      true if c is valid in level, false otherwise.
    • inLevel

      public static boolean inLevel(float[][] level, com.github.yellowstonegames.grid.Coord c)
      Parameters:
      level - dungeon/map level as 2D double array. x,y indexed
      c - Coord to check
      Returns:
      true if c is valid in level, false otherwise.
    • inLevel

      public static boolean inLevel(float[][] level, int x, int y)
      Parameters:
      level - dungeon/map level as 2D double array. x,y indexed
      x - x coordinate to check
      y - y coordinate to check
      Returns:
      true if c is valid in level, false otherwise.
    • inLevel

      public static <T> boolean inLevel(T[][] level, com.github.yellowstonegames.grid.Coord c)
      Parameters:
      level - a dungeon/map level as 2D array. x,y indexed
      c - Coord to check
      Returns:
      true if c is valid in level, false otherwise.
    • inLevel

      public static <T> boolean inLevel(T[][] level, int x, int y)
      Parameters:
      level - a dungeon/map level as 2D array. x,y indexed
      x - x coordinate to check
      y - y coordinate to check
      Returns:
      true if c is valid in level, false otherwise.
    • countCells

      public static int countCells(char[][] level, char match)
      Quickly counts the number of char elements in level that are equal to match.
      Parameters:
      level - the 2D char array to count cells in
      match - the char to search for
      Returns:
      the number of cells that matched
    • debugPrint

      public static void debugPrint(char[][] level)
      For when you want to print a 2D char array. Prints on multiple lines, with a trailing newline. To match how libGDX displays on the screen, this prints with the y-axis pointing up, that is, row 0 is at the bottom and the highest y-value is at the top.
      Parameters:
      level - a 2D char array to print with a trailing newline
    • wallWrap

      public static char[][] wallWrap(char[][] map)
      Changes the outer edge of a char[][] to the wall char, '#'.
      Parameters:
      map - A char[][] that stores map data; will be modified in place
      Returns:
      the modified-in-place map with its edge replaced with '#'
    • pointPath

      public static com.github.tommyettinger.ds.ObjectList<com.github.yellowstonegames.grid.Coord> pointPath(int width, int height, com.github.tommyettinger.random.EnhancedRandom rng)
    • ensurePath

      public static com.github.tommyettinger.ds.ObjectList<com.github.yellowstonegames.grid.Coord> ensurePath(char[][] map, com.github.tommyettinger.random.EnhancedRandom rng, char replacement, char... blocking)
      Ensures a path exists in a rough ring around the map by first creating the path (using pointPath(int, int, EnhancedRandom) with the given EnhancedRandom), then finding chars in blocking that are on that path and replacing them with replacement. Modifies map in-place and returns an ObjectList of Coord points that will always be on the path.
      Parameters:
      map - a 2D char array, x then y, etc. that will be modified directly; this is the "returned map"
      rng - used for random factors in the path choice
      replacement - the char that will fill be used where a path needs to be carved out; usually '.'
      blocking - an array or vararg of char that are considered blocking for the path and will be replaced if they are in the way
      Returns:
      the ObjectList of Coord points that are on the carved path, including existing non-blocking cells; will be empty if any parameters are invalid
    • allMatching

      public static com.github.tommyettinger.ds.ObjectList<com.github.yellowstonegames.grid.Coord> allMatching(char[][] map, char... matching)
    • circle

      public static List<com.github.yellowstonegames.grid.Coord> circle(int x, int y, int radius, List<com.github.yellowstonegames.grid.Coord> buf)
      Gets a List of Coord that are within radius distance of (x,y), and appends them to buf if it is non-null or makes a fresh List to append to otherwise. Returns buf if non-null, else the fresh List of Coord. May produce Coord values that are not within the boundaries of a map, such as (-5,-4), if the center is too close to the edge or radius is too high. You can use Radius.inCircle(int, int, int, boolean, int, int, List) with surpassEdges as false if you want to limit Coords to within the map, or the more general Radius.pointsInside(int, int, int, boolean, int, int, List) on a Radius.SQUARE or Radius.DIAMOND enum value if you want a square or diamond shape.
      Parameters:
      x - center x of the circle
      y - center y of the circle
      radius - inclusive radius to extend from the center; radius 0 gives just the center
      buf - Where to add the coordinates, or null for this method to allocate a fresh list.
      Returns:
      The coordinates of a circle centered (x, y), whose diameter is (radius * 2) + 1.
      See Also:
      • if you want to keep the Coords within the bounds of the map