Class DungeonTools
java.lang.Object
com.github.yellowstonegames.place.DungeonTools
A static class that can be used to modify the char[][] dungeons that other generators produce.
Includes constants to describe environment types encountered in possible dungeons, which are used elsewhere.
Here, 2D char arrays are always indexed with x, then y.
Has methods to open and close doors represented by
The earlier DungeonUtility class in SquidLib had various methods that have since been moved to
'+' and '/'. Can double the width of a 2D char
array while respecting both ASCII and Unicode box drawing chars for walls, or undo that operation. Can simplify 2D
char arrays to use '#' for any wall and '.' for any floor. Can add random paths to existing maps to
guarantee they can be traversed from one point to another. Can wrap a 2D char array with '#' chars for walls.
The earlier DungeonUtility class in SquidLib had various methods that have since been moved to
LineTools,
such as the often-used LineTools.hashesToLines(char[][]). This class still has debugPrint(char[][]),
but new code may want to prefer StringTools.printChar2D(char[][]), which calls the same code.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intBit flag used by other constants to indicate they are floors, or if absent, that they are walls.static final intA bit flag mask that can be used to isolate the bits that indicate whether an environment int value is a room, natural area, or corridor, regardless of floor/wall status.static final intA bit flag mask that can be used to isolate only the bits used in constants defined by DungeonTools.static final intConstant for environment tiles that are floors for a corridor.static final intConstant for environment tiles that are walls near a corridor.static final intA lock for the optional lock-and-key system that indicates an area can only be accessed by creatures who can swim or otherwise move through deep water.static final intA lock for the optional lock-and-key system that indicates an area can only be accessed by creatures who can move without risking death through intense heat and/or fire, such as a path blocked by lava.static final intA lock for the optional lock-and-key system that indicates an area can only be accessed by creatures who can jump or fly some higher-than-normal amount vertically, such as to get up and out of a pit.static final intA lock for the optional lock-and-key system that indicates an area can only be accessed by creatures who can jump or fly some longer-than-normal amount horizontally, such as to get over a low hazard.static final intA lock for the optional lock-and-key system that indicates an area can only be accessed by creatures who can open doors with a free hand.static final int[]All bit-flag constants that represent locks.static final intConstant for environment tiles that are floors for a cave or other natural part of a map.static final intConstant for environment tiles that are walls near a cave or other natural part of a map.static final intConstant for environment tiles that are floors for a room.static final intConstant for environment tiles that are walls near a room.static final intConstant for environment tiles that are not near a cave, room, or corridor. -
Method Summary
Modifier and TypeMethodDescriptionstatic com.github.tommyettinger.ds.ObjectList<com.github.yellowstonegames.grid.Coord> allMatching(char[][] map, char... matching) booleancheckLock(int environment, int lock) For the optional lock-and-key puzzle system, checks if an environment cell has access prohibited by a given lock.static List<com.github.yellowstonegames.grid.Coord> 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 DungeonProcessor with addDoors enabled, different chars are used for vertical and horizontal doors ('+' for vertical and '/' for horizontal).static char[][]closeDoorsInPlace(char[][] map, int[][] environment) When a map is generated by DungeonProcessor with addDoors enabled, different chars are used for vertical and horizontal doors ('+' for vertical and '/' for horizontal).static intcountCells(char[][] level, char match) Quickly counts the number of char elements in level that are equal to match.static voiddebugPrint(char[][] level) Prints a 2D char array without padding.static char[][]doubleWidth(char[][] map) Takes a dungeon map with either '#' as the only wall character or the Unicode box drawing characters used byLineTools.hashesToLines(char[][]).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 (usingpointPath(int, int, EnhancedRandom)with the given EnhancedRandom), then finding chars in blocking that are on that path and replacing them with replacement.intgetLevel(int environment) Gets the abstract "level" concept that can mean anything and is associated with a given environment cell.static booleaninLevel(char[][] level, int x, int y) static booleaninLevel(char[][] level, com.github.yellowstonegames.grid.Coord c) static booleaninLevel(float[][] level, int x, int y) static booleaninLevel(float[][] level, com.github.yellowstonegames.grid.Coord c) static <T> booleaninLevel(T[][] level, int x, int y) static <T> booleaninLevel(T[][] level, com.github.yellowstonegames.grid.Coord c) booleanisCorridor(int environment) Checks an environment int and returns true if it represents any type of corridor cell (floor or wall).booleanisFloor(int environment) Checks an environment int and returns true if it represents any type of floor (passable cell).booleanisNatural(int environment) Checks an environment int and returns true if it represents any type of natural cell (floor or wall).booleanisRoom(int environment) Checks an environment int and returns true if it represents any type of room cell (floor or wall).booleanisWall(int environment) Checks an environment int and returns true if it represents any type of wall (impassable cell).static char[][]openDoors(char[][] map) When a map is generated by DungeonProcessor with addDoors enabled, different chars are used for vertical and horizontal doors ('+' for vertical and '/' for horizontal).static char[][]openDoorsInPlace(char[][] map, int[][] environment) When a map is generated by DungeonProcessor 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, '#'.
-
Field Details
-
UNTOUCHED
public static final int UNTOUCHEDConstant 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.
This isn't really a bit flag, but if used as one in conjunction withANY_FLOOR, it is treated as a wall.- See Also:
-
ANY_FLOOR
public static final int ANY_FLOORBit flag used by other constants to indicate they are floors, or if absent, that they are walls. All floors are considered possible to pass through for pathfinding purposes, and all walls are considered impassable. This constant should not be used on its own in an environment 2D array. You can check if an environment value is any type of floor with(env & ANY_FLOOR) == ANY_FLOOR, or is any type of wall with(env & ANY_FLOOR) != ANY_FLOOR.- See Also:
-
ROOM_WALL
public static final int ROOM_WALLConstant for environment tiles that are walls near a room. Value is 2. Used by several classes that distinguish types of dungeon environment.
This is a bit flag. It can be used withenv == ROOM_WALLto check if an environment int value is a wall in a room (and nothing else). Use(env & ROOM_WALL) == ROOM_WALLto check if an environment int value is any kind of room value (room wall or room floor, even if other flags are set). Use(env & ROOM_FLOOR) == ROOM_WALLto check if env represents a wall in a room, potentially with other flags.- See Also:
-
ROOM_FLOOR
public static final int ROOM_FLOORConstant for environment tiles that are floors for a room. Value is 3. Used by several classes that distinguish types of dungeon environment.
This is a bit flag. It can be treated asROOM_WALL | ANY_FLOOR. You can compare an environment int value to this withenv == ROOM_FLOORto check if it represents a floor in a room (and nothing else). Use(env & ROOM_FLOOR) == ROOM_FLOORto check if env represents a floor in a room, potentially with other flags. Use(env & ROOM_FLOOR) == ROOM_WALLto check if env represents a wall in a room, potentially with other flags.- See Also:
-
NATURAL_WALL
public static final int NATURAL_WALLConstant 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 byWildernessGeneratorfor ledges and other natural obstacles.
This is a bit flag. It can be used withenv == NATURAL_WALLto check if an environment int value is a wall in a natural area (and nothing else). Use(env & NATURAL_WALL) == NATURAL_WALLto check if an environment int value is any kind of natural area (wall or floor, even if other flags are set). Use(env & NATURAL_FLOOR) == NATURAL_WALLto check if env represents a wall in a natural area, potentially with other flags.- See Also:
-
NATURAL_FLOOR
public static final int NATURAL_FLOORConstant for environment tiles that are floors for a cave or other natural part of a map. Value is 5. Used by several classes that distinguish types of dungeon environment. Also used byWildernessGeneratorfor almost everything it generates.
This is a bit flag. It can be treated asNATURAL_WALL | ANY_FLOOR. You can compare an environment int value to this withenv == NATURAL_FLOORto check if it represents a floor in a natural area (and nothing else). Use(env & NATURAL_FLOOR) == NATURAL_FLOORto check if env represents a floor in a natural area, potentially with other flags. Use(env & NATURAL_FLOOR) == NATURAL_WALLto check if env represents a wall in a natural area, potentially with other flags.- See Also:
-
CORRIDOR_WALL
public static final int CORRIDOR_WALLConstant for environment tiles that are walls near a corridor. Value is 8. Used by several classes that distinguish types of dungeon environment.
This is a bit flag. It can be treated asCORRIDOR_WALL | ANY_FLOOR. You can compare an environment int value to this withenv == CORRIDOR_FLOORto check if it represents a floor in a corridor (and nothing else). Use(env & CORRIDOR_FLOOR) == CORRIDOR_FLOORto check if env represents a floor in a corridor, potentially with other flags. Use(env & CORRIDOR_FLOOR) == CORRIDOR_WALLto check if env represents a wall in a corridor, potentially with other flags.- See Also:
-
CORRIDOR_FLOOR
public static final int CORRIDOR_FLOORConstant for environment tiles that are floors for a corridor. Value is 9. Used by several classes that distinguish types of dungeon environment.
This is a bit flag. It can be treated asCORRIDOR_WALL | ANY_FLOOR. You can compare an environment int value to this withenv == CORRIDOR_FLOORto check if it represents a floor in a corridor (and nothing else). Use(env & CORRIDOR_FLOOR) == CORRIDOR_FLOORto check if env represents a floor in a corridor, potentially with other flags. Use(env & CORRIDOR_FLOOR) == CORRIDOR_WALLto check if env represents a wall in a corridor, potentially with other flags.- See Also:
-
AREA_MASK
public static final int AREA_MASKA bit flag mask that can be used to isolate the bits that indicate whether an environment int value is a room, natural area, or corridor, regardless of floor/wall status. Use(env & AREA_MASK) == wallConstant, wherewallConstantis your choice ofROOM_WALL,NATURAL_WALL, orCORRIDOR_WALL, to identify if that int is your selected area.- See Also:
-
CORE_ENVIRONMENT_MASK
public static final int CORE_ENVIRONMENT_MASKA bit flag mask that can be used to isolate only the bits used in constants defined by DungeonTools. Use(env & CORE_ENVIRONMENT_MASK) == someConstant, wheresomeConstantis any constant from DungeonTools, to tell which one it is even if there are other flags present. Note that this will identify bit flags with no bits from DungeonTools constants asUNTOUCHED.- See Also:
-
LOCK_NEEDS_HAND
public static final int LOCK_NEEDS_HANDA lock for the optional lock-and-key system that indicates an area can only be accessed by creatures who can open doors with a free hand. This should block animals from entering areas that require opening a door to access. It may also temporarily block people who are frozen numb, for instance, or creatures like vampires who aren't allowed to open a door themselves in some versions.
This is lock 0.- See Also:
-
LOCK_DEEP_WATER
public static final int LOCK_DEEP_WATERA lock for the optional lock-and-key system that indicates an area can only be accessed by creatures who can swim or otherwise move through deep water.
This is lock 1.- See Also:
-
LOCK_FIRE
public static final int LOCK_FIREA lock for the optional lock-and-key system that indicates an area can only be accessed by creatures who can move without risking death through intense heat and/or fire, such as a path blocked by lava.
This is lock 2.- See Also:
-
LOCK_HIGH_JUMP
public static final int LOCK_HIGH_JUMPA lock for the optional lock-and-key system that indicates an area can only be accessed by creatures who can jump or fly some higher-than-normal amount vertically, such as to get up and out of a pit.
This is lock 3.- See Also:
-
LOCK_LONG_JUMP
public static final int LOCK_LONG_JUMPA lock for the optional lock-and-key system that indicates an area can only be accessed by creatures who can jump or fly some longer-than-normal amount horizontally, such as to get over a low hazard.
This is lock 4.- See Also:
-
LOCKS
public static final int[] LOCKSAll bit-flag constants that represent locks. There are 20 constants here, and any one can be passed tocheckLock(int, int)as itslockparameter.
-
-
Method Details
-
isFloor
public boolean isFloor(int environment) Checks an environment int and returns true if it represents any type of floor (passable cell).- Parameters:
environment- an environment int, typically taken fromPlaceGenerator.getEnvironment()- Returns:
- true if the int represents any passable terrain cell
-
isWall
public boolean isWall(int environment) Checks an environment int and returns true if it represents any type of wall (impassable cell).- Parameters:
environment- an environment int, typically taken fromPlaceGenerator.getEnvironment()- Returns:
- true if the int represents any impassable terrain cell
-
isRoom
public boolean isRoom(int environment) Checks an environment int and returns true if it represents any type of room cell (floor or wall).- Parameters:
environment- an environment int, typically taken fromPlaceGenerator.getEnvironment()- Returns:
- true if the int represents any room cell
-
isCorridor
public boolean isCorridor(int environment) Checks an environment int and returns true if it represents any type of corridor cell (floor or wall).- Parameters:
environment- an environment int, typically taken fromPlaceGenerator.getEnvironment()- Returns:
- true if the int represents any corridor cell
-
isNatural
public boolean isNatural(int environment) Checks an environment int and returns true if it represents any type of natural cell (floor or wall).- Parameters:
environment- an environment int, typically taken fromPlaceGenerator.getEnvironment()- Returns:
- true if the int represents any natural cell (such as cave walls or floors)
-
checkLock
public boolean checkLock(int environment, int lock) For the optional lock-and-key puzzle system, checks if an environment cell has access prohibited by a given lock. There are 20 locks, all stored inLOCKSand some additionally available as constants in this class, such asLOCK_NEEDS_HANDorLOCK_DEEP_WATER.- Parameters:
environment- an environment int, typically taken fromPlaceGenerator.getEnvironment()lock- either fromLOCKSor a constant from this class such asLOCK_NEEDS_HAND- Returns:
- true if the environment cell has locked access by the given lock
-
getLevel
public int getLevel(int environment) Gets the abstract "level" concept that can mean anything and is associated with a given environment cell. The "level" starts at 0 and can go up to 255, inclusive. It can be used to mean the approximate power of enemies in an area, as one meaning, or a linear score of some kind needed or preferred to enter the area. It is separate from the optional lock-and-key system, which allows any locks to be passed independently of any others.- Parameters:
environment- an environment int, typically taken fromPlaceGenerator.getEnvironment()- Returns:
- an int between 0 and 255, drawn from the given environment cell
-
closeDoors
public static char[][] closeDoors(char[][] map) When a map is generated by DungeonProcessor 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
-
closeDoorsInPlace
public static char[][] closeDoorsInPlace(char[][] map, int[][] environment) When a map is generated by DungeonProcessor 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. This also takes and modifies an int 2D array, which is often generated byPlaceGenerator.getEnvironment(), and sets the optional lock value on all doors to enableLOCK_NEEDS_HAND. That lock is used to mean an area that can only be opened by someone with an available and working hand, but not an animal or mindless creature. This does not set the lock value on areas past closed doors, since that would need to know an entry point.- Parameters:
map- a char[][] that may have both '+' and '/' for doors, which will be modified in-placeenvironment- an environment int[][], which will be modified in-place; must be at least as large as map- Returns:
- map, after changes in-place
-
openDoors
public static char[][] openDoors(char[][] map) When a map is generated by DungeonProcessor 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
-
openDoorsInPlace
public static char[][] openDoorsInPlace(char[][] map, int[][] environment) When a map is generated by DungeonProcessor 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. This also takes and modifies an int 2D array, which is often generated byPlaceGenerator.getEnvironment(), and sets the optional lock value on all doors to disableLOCK_NEEDS_HAND. That lock is used to mean an area that can only be opened by someone with an available and working hand, but not an animal or mindless creature. This does not change the lock value on areas past open doors, since that would need to know an entry point.- Parameters:
map- a char[][] that may have both '+' and '/' for doors, which will be modified in-placeenvironment- an environment int[][], which will be modified in-place; must be at least as large as map- Returns:
- map, after changes in-place
-
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 byLineTools.hashesToLines(char[][]). Returns a new char[][] dungeon map with two characters per cell, mostly filling the spaces next to non-walls with space characters. Only does 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 indexedc- Coord to check- Returns:
trueifcis valid inlevel,falseotherwise.
-
inLevel
public static boolean inLevel(char[][] level, int x, int y) - Parameters:
level- dungeon/map level as 2D char array. x,y indexedx- x coordinate to checky- y coordinate to check- Returns:
trueifcis valid inlevel,falseotherwise.
-
inLevel
public static boolean inLevel(float[][] level, com.github.yellowstonegames.grid.Coord c) - Parameters:
level- dungeon/map level as 2D float array. x,y indexedc- Coord to check- Returns:
trueifcis valid inlevel,falseotherwise.
-
inLevel
public static boolean inLevel(float[][] level, int x, int y) - Parameters:
level- dungeon/map level as 2D float array. x,y indexedx- x coordinate to checky- y coordinate to check- Returns:
trueifcis valid inlevel,falseotherwise.
-
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 indexedc- Coord to check- Returns:
trueifcis valid inlevel,falseotherwise.
-
inLevel
public static <T> boolean inLevel(T[][] level, int x, int y) - Parameters:
level- a dungeon/map level as 2D array. x,y indexedx- x coordinate to checky- y coordinate to check- Returns:
trueifcis valid inlevel,falseotherwise.
-
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 inmatch- the char to search for- Returns:
- the number of cells that matched
-
debugPrint
public static void debugPrint(char[][] level) Prints a 2D char array without padding. Prints on multiple lines, with a trailing newline. To match how libGDX usually 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.
This delegates toStringTools.printChar2D(char[][]).- 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 (usingpointPath(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 choicereplacement- 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 useRadius.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 generalRadius.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 circley- center y of the circleradius- inclusive radius to extend from the center; radius 0 gives just the centerbuf- 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:
-