Package squidpony.squidgrid.mapping
Class DungeonUtility
java.lang.Object
squidpony.squidgrid.mapping.DungeonUtility
public class DungeonUtility 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.
- Author:
- Tommy Ettinger - https://github.com/tommyettinger
- See Also:
DungeonGenerator uses this class a fair amount Created by Tommy Ettinger on 4/1/2015.
-
Field Summary
Fields Modifier and Type Field Description static int
CAVE_FLOOR
Constant for environment tiles that are floors for a cave.static int
CAVE_WALL
Constant for environment tiles that are walls near a cave.static int
CORRIDOR_FLOOR
Constant for environment tiles that are floors for a corridor.static int
CORRIDOR_WALL
Constant for environment tiles that are walls near a corridor.IStatefulRNG
rng
The random number generator that will be used for all methods in this class with a random component.static int
ROOM_FLOOR
Constant for environment tiles that are floors for a room.static int
ROOM_WALL
Constant for environment tiles that are walls near a room.static int
UNTOUCHED
Constant for environment tiles that are not near a cave, room, or corridor. -
Constructor Summary
Constructors Constructor Description DungeonUtility()
DungeonUtility(IRNG rng)
DungeonUtility(IStatefulRNG rng)
-
Method Summary
Modifier and Type Method Description static ArrayList<Coord>
allMatching(char[][] map, char... matching)
static List<Coord>
circle(int x, int y, int radius, List<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 ArrayList<Coord>
ensurePath(char[][] map, IRNG rng, char replacement, char... blocking)
Ensures a path exists in a rough ring around the map by first creating the path (usingpointPath(int, int, IRNG)
with the given IRNG), then finding chars in blocking that are on that path and replacing them with replacement.static double[][]
generateAStarCostMap(char[][] map)
Given a char[][] for the map, produces a double[][] that can be used as a map by AStarSearch.static double[][]
generateAStarCostMap(char[][] map, Map<Character,Double> costs, double defaultValue)
Given a char[][] for the map, a Map of Character keys to Double values that will be used to determine costs, and a double value for unhandled characters, produces a double[][] that can be used as a map by AStarSearch.static double[][]
generateCostMap(char[][] map, Map<Character,Double> costs, double defaultValue)
Given a char[][] for the map, a Map of Character keys to Double values that will be used to determine costs, and a double value for unhandled characters, produces a double[][] that can be used as a costMap by DijkstraMap.static double[][]
generateResistances(char[][] map)
Given a char[][] for the map, produces a double[][] that can be used with FOV.calculateFOV().static double[][]
generateResistances3x3(char[][] map)
Given a char[][] for the map that should use box drawing characters (as produced byhashesToLines(char[][], boolean)
), produces a double[][] with triple width and triple height that can be used with FOV.calculateFOV() in classes that use subcell lighting.static double[][]
generateSimpleResistances(char[][] map)
Given a char[][] for the map, produces a double[][] that can be used with FOV.calculateFOV(), but does not treat any cells as partly transparent, only fully-blocking or fully-permitting light.static double[][]
generateSimpleResistances3x3(char[][] map)
Given a char[][] for the map that should use box drawing characters (as produced byhashesToLines(char[][], boolean)
), produces a double[][] with triple width and triple height that can be used with FOV.calculateFOV() in classes that use subcell lighting.static Coord
getRandomCell(IRNG rng, char[][] map, Set<Character> acceptable, int frustration)
static char[][]
hashesToLines(char[][] map)
Takes a char[][] dungeon map that uses '#' to represent walls, and returns a new char[][] that uses unicode box drawing characters to draw straight, continuous lines for walls, filling regions between walls (that were filled with more walls before) with space characters, ' '.static char[][]
hashesToLines(char[][] map, boolean keepSingleHashes)
Takes a char[][] dungeon map that uses '#' to represent walls, and returns a new char[][] that uses unicode box drawing characters to draw straight, continuous lines for walls, filling regions between walls (that were filled with more walls before) with space characters, ' '.static boolean
inLevel(char[][] level, int x, int y)
static boolean
inLevel(char[][] level, Coord c)
static boolean
inLevel(double[][] level, int x, int y)
static boolean
inLevel(double[][] level, Coord c)
static <T> boolean
inLevel(T[][] level, int x, int y)
static <T> boolean
inLevel(T[][] level, Coord c)
static char[][]
linesToHashes(char[][] map)
Reverses most of the effects of hashesToLines().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 short[]
packedFloors(char[][] map)
A convenience wrapper for getting a packed-data representation of all floors ('.') in map, for randomCell().static ArrayList<Coord>
pointPath(int width, int height, IRNG rng)
Coord
randomCell(short[] packed)
Finds a random Coord where the x and y match up to a [x][y] location that is encoded as "on" in packed.Coord
randomFloor(char[][] map)
Finds a random Coord where the x and y match up to a [x][y] location on map that has '.' as a value.Coord
randomFloorLarge(char[][] map, int size)
Finds a random Coord where the x and y match up to a [x][y] location on map that has '.' as a value, and a square of cells extending in the positive x and y directions with a side length of size must also have '.' as their values.Coord
randomMatchingTile(char[][] map, char tile)
Finds a random Coord where the x and y match up to a [x][y] location on map that has the same value as the parameter tile.Coord
randomStep(char[][] map, Coord start, boolean eightWay)
Gets a random Coord that is adjacent to start, validating whether the position can exist on the given map.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 double[][]
translateAStarToDijkstra(double[][] astar)
static double[][]
translateDijkstraToAStar(double[][] dijkstra)
static char[][]
transposeLines(char[][] map)
If you call hashesToLines() on a map that uses [y][x] conventions instead of [x][y], it will have the lines not connect as you expect.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
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, likeSectionDungeonGenerator
.- See Also:
- Constant Field Values
-
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, likeSectionDungeonGenerator
.- See Also:
- Constant Field Values
-
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, likeSectionDungeonGenerator
.- See Also:
- Constant Field Values
-
CAVE_FLOOR
Constant for environment tiles that are floors for a cave. Value is 3. Used by several classes that distinguish types of dungeon environment, likeSectionDungeonGenerator
.- See Also:
- Constant Field Values
-
CAVE_WALL
Constant for environment tiles that are walls near a cave. Value is 4. Used by several classes that distinguish types of dungeon environment, likeSectionDungeonGenerator
.- See Also:
- Constant Field Values
-
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, likeSectionDungeonGenerator
.- See Also:
- Constant Field Values
-
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, likeSectionDungeonGenerator
.- See Also:
- Constant Field Values
-
rng
The random number generator that will be used for all methods in this class with a random component.
-
-
Constructor Details
-
Method Details
-
randomFloor
Finds a random Coord where the x and y match up to a [x][y] location on map that has '.' as a value. Uses this class' rng field for pseudo-random number generation. May fail if there are very few floor tiles; userandomCell(short[])
givenpackedFloors(char[][])
to get a floor cell guaranteed if there is at least one, or better still, make aGreasedRegion(char[][], char)
and get a single Coord from it withGreasedRegion.singleRandom(IRNG)
. You can reuse a GreasedRegion with modifications, like removing cells you already picked, but using packedFloors() needs to make new short arrays each time.- Parameters:
map
- a char[][] that should contain a '.' floor tile- Returns:
- a Coord that corresponds to a '.' in map, or null if a '.' cannot be found or if map is too small
-
randomCell
Finds a random Coord where the x and y match up to a [x][y] location that is encoded as "on" in packed. This is useful when you have usedDungeonUtility.packedFloors(char[][] map)
to encode all floors in map, orCoordPacker.pack(char[][] map, char... yes)
to encode all cells in a char[][] map that match a particular type, like '.' for floors or '~' for deep water, and want to efficiently get one randomly-chosen tile from it. Calling pack() is likely slightly less efficient than using randomFloor(), but it only needs to be done once per map and cell type, and this method should be substantially more efficient when the type of cell is uncommon on the map. Uses this class' rng field for pseudo-random number generation.- Parameters:
packed
- a packed array produced by CoordPacker encoding the cells to choose from as "on"- Returns:
- a Coord that corresponds to a '.' in map, or (-1, -1) if a '.' cannot be found or if map is too small
-
packedFloors
A convenience wrapper for getting a packed-data representation of all floors ('.') in map, for randomCell(). If you want other chars or more chars than just the period, you can use CoordPacker.pack() with a char[][] map and one or more chars to find as the parameters. This is the same as callingCoordPacker.pack(map, '.')
.- Parameters:
map
- a char[][] that uses '.' to represent floors- Returns:
- all floors in map in packed data format (a special short array) that can be given to randomCell()
-
randomMatchingTile
Finds a random Coord where the x and y match up to a [x][y] location on map that has the same value as the parameter tile. Uses this class' rng field for pseudo-random number generation.- Parameters:
map
- a char[][] that should contain the desired tiletile
- the char to search for- Returns:
- a Coord that corresponds to a map element equal to tile, or null if tile cannot be found or if map is too small.
-
randomStep
Gets a random Coord that is adjacent to start, validating whether the position can exist on the given map. Adjacency defaults to four-way cardinal directions unless eightWay is true, in which case it uses Chebyshev. This can step into walls, and should NOT be used for movement. It is meant for things like sound that can exist in walls, or for assigning decor to floors or walls that are adjacent to floors.- Parameters:
map
- a char[][] map that this will only use for its width and height; contents are ignoredstart
- the starting positioneightWay
- true to choose a random orthogonal or diagonal direction; false to only choose from orthogonal- Returns:
- a Coord that is adjacent to start on the map, or null if start is off the map or the map is very small
-
randomFloorLarge
Finds a random Coord where the x and y match up to a [x][y] location on map that has '.' as a value, and a square of cells extending in the positive x and y directions with a side length of size must also have '.' as their values. Uses this class' rng field for pseudo-random number generation.- Parameters:
map
- a char[][] that should contain at least one floor represented by '.'size
- the side length of a square that must be completely filled with floors for this to return it- Returns:
- a Coord that corresponds to a '.' in map, or null if a '.' cannot be found or if map is too small.
-
hashesToLines
Takes a char[][] dungeon map that uses '#' to represent walls, and returns a new char[][] that uses unicode box drawing characters to draw straight, continuous lines for walls, filling regions between walls (that were filled with more walls before) with space characters, ' '. If the lines "point the wrong way," such as having multiple horizontally adjacent vertical lines where there should be horizontal lines, call transposeLines() on the returned map, which will keep the dimensions of the map the same and only change the line chars. You will also need to call transposeLines if you call hashesToLines on a map that already has "correct" line-drawing characters, which means hashesToLines should only be called on maps that use '#' for walls. If you have a jumbled map that contains two or more of the following: "correct" line-drawing characters, "incorrect" line-drawing characters, and '#' characters for walls, you can reset by calling linesToHashes() and then potentially calling hashesToLines() again.- Parameters:
map
- a 2D char array indexed with x,y that uses '#' for walls- Returns:
- a copy of the map passed as an argument with box-drawing characters replacing '#' walls
-
hashesToLines
Takes a char[][] dungeon map that uses '#' to represent walls, and returns a new char[][] that uses unicode box drawing characters to draw straight, continuous lines for walls, filling regions between walls (that were filled with more walls before) with space characters, ' '. If keepSingleHashes is true, then '#' will be used if a wall has no orthogonal wall neighbors; if it is false, then a horizontal line will be used for stand-alone wall cells. If the lines "point the wrong way," such as having multiple horizontally adjacent vertical lines where there should be horizontal lines, call transposeLines() on the returned map, which will keep the dimensions of the map the same and only change the line chars. You will also need to call transposeLines if you call hashesToLines on a map that already has "correct" line-drawing characters, which means hashesToLines should only be called on maps that use '#' for walls. If you have a jumbled map that contains two or more of the following: "correct" line-drawing characters, "incorrect" line-drawing characters, and '#' characters for walls, you can reset by calling linesToHashes() and then potentially calling hashesToLines() again.- Parameters:
map
- a 2D char array indexed with x,y that uses '#' for wallskeepSingleHashes
- true if walls that are not orthogonally adjacent to other walls should stay as '#'- Returns:
- a copy of the map passed as an argument with box-drawing characters replacing '#' walls
-
linesToHashes
Reverses most of the effects of hashesToLines(). The only things that will not be reversed are the placement of space characters in unreachable wall-cells-behind-wall-cells, which remain as spaces. This is useful if you have a modified map that contains wall characters of conflicting varieties, as described in hashesToLines().- Parameters:
map
- a 2D char array indexed with x,y that uses box-drawing characters for walls- Returns:
- a copy of the map passed as an argument with '#' replacing box-drawing characters for walls
-
transposeLines
If you call hashesToLines() on a map that uses [y][x] conventions instead of [x][y], it will have the lines not connect as you expect. Use this function to change the directions of the box-drawing characters only, without altering the dimensions in any way. This returns a new char[][], instead of modifying the parameter in place. transposeLines is also needed if the lines in a map have become transposed when they were already correct; calling this method on an incorrectly transposed map will change the directions on all of its lines.- Parameters:
map
- a 2D char array indexed with y,x that uses box-drawing characters for walls- Returns:
- a copy of map that uses box-drawing characters for walls that will be correct when indexed with x,y
-
closeDoors
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
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
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
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
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
-
generateResistances
Given a char[][] for the map, produces a double[][] that can be used with FOV.calculateFOV(). It expects any doors to be represented by '+' if closed or '/' if open (which can be caused by calling DungeonUtility.closeDoors() ), any walls to be '#' or box drawing characters, and it doesn't care what other chars are used (only doors, including open ones, and walls obscure light and thus have a resistance by default).
This is here for backwards-compatibility; this method delegates toFOV.generateResistances(char[][])
.- Parameters:
map
- a dungeon, width by height, with any closed doors as '+' and open doors as '/' as per closeDoors()- Returns:
- a resistance map suitable for use with the FOV class, with clear cells assigned 0.0 and blocked ones 1.0
-
generateResistances3x3
Given a char[][] for the map that should use box drawing characters (as produced byhashesToLines(char[][], boolean)
), produces a double[][] with triple width and triple height that can be used with FOV.calculateFOV() in classes that use subcell lighting. Importantly, this only considers a "thin line" of wall to be blocking (matching the box drawing character), instead of the whole 3x3 area. This expects any doors to be represented by '+' if closed or '/' if open (which can be caused by callingcloseDoors(char[][])
), thick vegetation or other concealing obstructions to be '"', any normal walls to be box drawing characters, any cells that block all subcells to be '#', and it doesn't care what other chars are used (only doors, including open ones, vegetation, and walls obscure light and thus have a resistance normally).
This is here for backwards-compatibility; this method delegates toFOV.generateResistances3x3(char[][])
- Parameters:
map
- a dungeon, width by height, with any closed doors as '+' and open doors as '/' as per closeDoors()- Returns:
- a resistance map suitable for use with the FOV class and subcell lighting, with triple width/height
-
generateSimpleResistances
Given a char[][] for the map, produces a double[][] that can be used with FOV.calculateFOV(), but does not treat any cells as partly transparent, only fully-blocking or fully-permitting light. This is mainly useful if you expect the FOV radius to be very high or (effectively) infinite, since anything less than complete blockage would be passed through by infinite-radius FOV. This expects any doors to be represented by '+' if closed or '/' if open (most door placement defaults to a mix of '+' and '/', so by callingcloseDoors(char[][])
you can close all doors at the start), and any walls to be '#' or box drawing characters. This will assign 1.0 resistance to walls and closed doors or 0.0 for any other cell.
This is here for backwards-compatibility; this method delegates toFOV.generateSimpleResistances(char[][])
.- Parameters:
map
- a dungeon, width by height, with any closed doors as '+' and open doors as '/' as per closeDoors()- Returns:
- a resistance map suitable for use with the FOV class, but with no partially transparent cells
-
generateSimpleResistances3x3
Given a char[][] for the map that should use box drawing characters (as produced byhashesToLines(char[][], boolean)
), produces a double[][] with triple width and triple height that can be used with FOV.calculateFOV() in classes that use subcell lighting. This expects any doors to be represented by '+' if closed or '/' if open (most door placement defaults to a mix of '+' and '/', so by callingcloseDoors(char[][])
you can close all doors at the start), any walls to be box drawing characters, and any cells that block all subcells within their area to be '#'. This will assign 1.0 resistance to walls and closed doors where a line of the box drawing char would block light, or 0.0 for any other subcell.
This is here for backwards-compatibility; this method delegates toFOV.generateSimpleResistances3x3(char[][])
.- Parameters:
map
- a dungeon, width by height, with any closed doors as '+' and open doors as '/' as per closeDoors()- Returns:
- a resistance map suitable for use with the FOV class and subcell lighting, with triple width/height
-
generateCostMap
public static double[][] generateCostMap(char[][] map, Map<Character,Double> costs, double defaultValue)Given a char[][] for the map, a Map of Character keys to Double values that will be used to determine costs, and a double value for unhandled characters, produces a double[][] that can be used as a costMap by DijkstraMap. It expects any doors to be represented by '+' if closed or '/' if open (which can be caused by calling DungeonUtility.closeDoors() ) and any walls to be '#' or box drawing characters. In the parameter costs, there does not need to be an entry for '#' or any box drawing characters, but if one is present for '#' it will apply that cost to both '#' and all box drawing characters, and if one is not present it will default to a very high number. For any other entry in costs, a char in the 2D char array that matches the key will correspond (at the same x,y position in the returned 2D double array) to that key's value in costs. If a char is used in the map but does not have a corresponding key in costs, it will be given the value of the parameter defaultValue. The values in costs are multipliers, so should not be negative, should only be 0.0 in cases where you want infinite movement across all adjacent squares of that kind, should be higher than 1.0 for difficult terrain (2.0 and 3.0 are reasonable), should be between 0.0 and 1.0 for easy terrain, and should be 1.0 for normal terrain. If a cell should not be possible to enter for this character, 999.0 should be a reasonable value for a cost. An example use for this would be to make a creature unable to enter any non-water cell (like a fish), unable to enter doorways (like some mythological versions of vampires), or to make a wheeled vehicle take more time to move across rubble or rough terrain. A potentially common case that needs to be addressed is NPC movement onto staircases in games that have them; some games may find it desirable for NPCs to block staircases and others may not, but in either case you should give both '>' and '<', the standard characters for staircases, the same value in costs.- Parameters:
map
- a dungeon, width by height, with any closed doors as '+' and open doors as '/' as per closeDoors() .costs
- a Map of Character keys representing possible elements in map, and Double values for their cost.defaultValue
- a double that will be used as the cost for any characters that don't have a key in costs.- Returns:
- a cost map suitable for use with DijkstraMap
-
generateAStarCostMap
Given a char[][] for the map, produces a double[][] that can be used as a map by AStarSearch. It expects any doors to be represented by '+' if closed or '/' if open (which can be ensured by callingcloseDoors(char[][])
) and any walls to be '#' or box drawing characters. Any wall or closed door will be assigned a negative number, meaning it is impassable for AStarSearch, and all other chars will be assigned 1.0, giving them a normal cost.- Parameters:
map
- a dungeon, width by height, with any closed doors as '+' and open doors as '/' as per closeDoors() .- Returns:
- a cost map suitable for use with AStarSearch
-
generateAStarCostMap
public static double[][] generateAStarCostMap(char[][] map, Map<Character,Double> costs, double defaultValue)Given a char[][] for the map, a Map of Character keys to Double values that will be used to determine costs, and a double value for unhandled characters, produces a double[][] that can be used as a map by AStarSearch. It expects any doors to be represented by '+' if closed or '/' if open (which can be caused by calling DungeonUtility.closeDoors() ) and any walls to be '#' or box drawing characters. In the parameter costs, there does not need to be an entry for '#' or any box drawing characters, but if one is present for '#' it will apply that cost to both '#' and all box drawing characters, and if one is not present it will default to a negative number, meaning it is impassable for AStarSearch. For any other entry in costs, a char in the 2D char array that matches the key will correspond (at the same x,y position in the returned 2D double array) to that key's value in costs. If a char is used in the map but does not have a corresponding key in costs, it will be given the value of the parameter defaultValue, which is typically 1 unless a creature is limited to only moving in some terrain. The values in costs are different from those expected for DijkstraMap; negative numbers are impassable, 1 is the cost for a normal walkable tile, and higher numbers are harder to enter. An example use for this would be to make a creature unable to enter any non-water cell (like a fish), unable to enter doorways (like some mythological versions of vampires), or to make a wheeled vehicle take more time to move across rubble or rough terrain. A potentially common case that needs to be addressed is NPC movement onto staircases in games that have them; some games may find it desirable for NPCs to block staircases and others may not, but in either case you should give both '>' and '<', the standard characters for staircases, the same value in costs.- Parameters:
map
- a dungeon, width by height, with any closed doors as '+' and open doors as '/' as per closeDoors() .costs
- a Map of Character keys representing possible elements in map, and Double values for their cost.defaultValue
- a double that will be used as the cost for any characters that don't have a key in costs.- Returns:
- a cost map suitable for use with AStarSearch
-
translateAStarToDijkstra
-
translateDijkstraToAStar
-
getRandomCell
public static Coord getRandomCell(IRNG rng, char[][] map, Set<Character> acceptable, int frustration)- Parameters:
rng
-map
-acceptable
-frustration
- The number of trials that this method can do. Usually 16 or 32.- Returns:
- A random cell in
map
whose symbol is inacceptable
. Ornull
if not found.
-
inLevel
- Parameters:
level
- dungeon/map level as 2D char array. x,y indexedc
- Coord to check- Returns:
true
ifc
is valid inlevel
,false
otherwise.
-
inLevel
- Parameters:
level
- dungeon/map level as 2D char array. x,y indexedx
- x coordinate to checky
- y coordinate to check- Returns:
true
ifc
is valid inlevel
,false
otherwise.
-
inLevel
- Parameters:
level
- dungeon/map level as 2D double array. x,y indexedc
- Coord to check- Returns:
true
ifc
is valid inlevel
,false
otherwise.
-
inLevel
- Parameters:
level
- dungeon/map level as 2D double array. x,y indexedx
- x coordinate to checky
- y coordinate to check- Returns:
true
ifc
is valid inlevel
,false
otherwise.
-
inLevel
- Parameters:
level
- a dungeon/map level as 2D array. x,y indexedc
- Coord to check- Returns:
true
ifc
is valid inlevel
,false
otherwise.
-
inLevel
- Parameters:
level
- a dungeon/map level as 2D array. x,y indexedx
- x coordinate to checky
- y coordinate to check- Returns:
true
ifc
is valid inlevel
,false
otherwise.
-
countCells
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
For when you want to print a 2D char array. Prints on multiple lines, with a trailing newline.- Parameters:
level
- a 2D char array to print with a trailing newline
-
wallWrap
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
-
ensurePath
public static ArrayList<Coord> ensurePath(char[][] map, IRNG rng, char replacement, char... blocking)Ensures a path exists in a rough ring around the map by first creating the path (usingpointPath(int, int, IRNG)
with the given IRNG), then finding chars in blocking that are on that path and replacing them with replacement. Modifies map in-place (!) and returns an ArrayList 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 ArrayList of Coord points that are on the carved path, including existing non-blocking cells; will be empty if any parameters are invalid
-
allMatching
-
circle
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:
if you want to keep the Coords within the bounds of the map
-