public class DungeonUtility
extends java.lang.Object
Modifier and Type | Field and 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 and Description |
---|
DungeonUtility() |
DungeonUtility(IRNG rng) |
DungeonUtility(IStatefulRNG rng) |
Modifier and Type | Method and Description |
---|---|
static java.util.ArrayList<Coord> |
allMatching(char[][] map,
char... matching) |
static java.util.List<Coord> |
circle(int x,
int y,
int radius,
java.util.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 java.util.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 (using
pointPath(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,
java.util.Map<java.lang.Character,java.lang.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,
java.util.Map<java.lang.Character,java.lang.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 by
hashesToLines(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 by
hashesToLines(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,
java.util.Set<java.lang.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,
Coord c) |
static boolean |
inLevel(char[][] level,
int x,
int y) |
static boolean |
inLevel(double[][] level,
Coord c) |
static boolean |
inLevel(double[][] level,
int x,
int y) |
static <T> boolean |
inLevel(T[][] level,
Coord c) |
static <T> boolean |
inLevel(T[][] level,
int x,
int y) |
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 java.util.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, '#'.
|
public static final int UNTOUCHED
SectionDungeonGenerator
.public static final int ROOM_FLOOR
SectionDungeonGenerator
.public static final int ROOM_WALL
SectionDungeonGenerator
.public static final int CAVE_FLOOR
SectionDungeonGenerator
.public static final int CAVE_WALL
SectionDungeonGenerator
.public static final int CORRIDOR_FLOOR
SectionDungeonGenerator
.public static final int CORRIDOR_WALL
SectionDungeonGenerator
.public IStatefulRNG rng
public DungeonUtility()
public DungeonUtility(IStatefulRNG rng)
public DungeonUtility(IRNG rng)
public Coord randomFloor(char[][] map)
randomCell(short[])
given packedFloors(char[][])
to get a floor cell guaranteed if there is
at least one, or better still, make a GreasedRegion(char[][], char)
and get a single Coord
from it with GreasedRegion.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.map
- a char[][] that should contain a '.' floor tilepublic Coord randomCell(short[] packed)
DungeonUtility.packedFloors(char[][] map)
to encode all floors in map,
or CoordPacker.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.packed
- a packed array produced by CoordPacker encoding the cells to choose from as "on"public static short[] packedFloors(char[][] map)
CoordPacker.pack(map, '.')
.map
- a char[][] that uses '.' to represent floorspublic Coord randomMatchingTile(char[][] map, char tile)
map
- a char[][] that should contain the desired tiletile
- the char to search forpublic Coord randomStep(char[][] map, Coord start, boolean eightWay)
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 orthogonalpublic Coord randomFloorLarge(char[][] map, int size)
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 itpublic static char[][] hashesToLines(char[][] map)
map
- a 2D char array indexed with x,y that uses '#' for wallspublic static char[][] hashesToLines(char[][] map, boolean keepSingleHashes)
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 '#'public static char[][] linesToHashes(char[][] map)
map
- a 2D char array indexed with x,y that uses box-drawing characters for wallspublic static char[][] transposeLines(char[][] map)
map
- a 2D char array indexed with y,x that uses box-drawing characters for wallspublic static char[][] closeDoors(char[][] map)
map
- a char[][] that may have both '+' and '/' for doorspublic static char[][] openDoors(char[][] map)
map
- a char[][] that may have both '+' and '/' for doorspublic static char[][] simplifyDungeon(char[][] map)
map
- a char[][] with different characters that can be simplified to "wall" or "floor"public static char[][] doubleWidth(char[][] map)
map
- a char[][] that uses either '#' or box-drawing characters for walls, but one per cellpublic static char[][] unDoubleWidth(char[][] map)
map
- a char[][] that has been widened by doubleWidth()public static double[][] generateResistances(char[][] map)
FOV.generateResistances(char[][])
.map
- a dungeon, width by height, with any closed doors as '+' and open doors as '/' as per closeDoors()public static double[][] generateResistances3x3(char[][] map)
hashesToLines(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 calling
closeDoors(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).
FOV.generateResistances3x3(char[][])
map
- a dungeon, width by height, with any closed doors as '+' and open doors as '/' as per closeDoors()public static double[][] generateSimpleResistances(char[][] map)
closeDoors(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.
FOV.generateSimpleResistances(char[][])
.map
- a dungeon, width by height, with any closed doors as '+' and open doors as '/' as per closeDoors()public static double[][] generateSimpleResistances3x3(char[][] map)
hashesToLines(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 calling
closeDoors(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.
FOV.generateSimpleResistances3x3(char[][])
.map
- a dungeon, width by height, with any closed doors as '+' and open doors as '/' as per closeDoors()public static double[][] generateCostMap(char[][] map, java.util.Map<java.lang.Character,java.lang.Double> costs, double defaultValue)
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.public static double[][] generateAStarCostMap(char[][] map)
closeDoors(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.map
- a dungeon, width by height, with any closed doors as '+' and open doors as '/' as per closeDoors() .public static double[][] generateAStarCostMap(char[][] map, java.util.Map<java.lang.Character,java.lang.Double> costs, double defaultValue)
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.public static double[][] translateAStarToDijkstra(double[][] astar)
public static double[][] translateDijkstraToAStar(double[][] dijkstra)
public static Coord getRandomCell(IRNG rng, char[][] map, java.util.Set<java.lang.Character> acceptable, int frustration)
rng
- map
- acceptable
- frustration
- The number of trials that this method can do. Usually 16 or
32.map
whose symbol is in
acceptable
. Or null
if not found.public static boolean inLevel(char[][] level, Coord c)
level
- dungeon/map level as 2D char array. x,y indexedc
- Coord to checktrue
if c
is valid in level
, false
otherwise.public static boolean inLevel(char[][] level, int x, int y)
level
- dungeon/map level as 2D char array. x,y indexedx
- x coordinate to checky
- y coordinate to checktrue
if c
is valid in level
, false
otherwise.public static boolean inLevel(double[][] level, Coord c)
level
- dungeon/map level as 2D double array. x,y indexedc
- Coord to checktrue
if c
is valid in level
, false
otherwise.public static boolean inLevel(double[][] level, int x, int y)
level
- dungeon/map level as 2D double array. x,y indexedx
- x coordinate to checky
- y coordinate to checktrue
if c
is valid in level
, false
otherwise.public static <T> boolean inLevel(T[][] level, Coord c)
level
- a dungeon/map level as 2D array. x,y indexedc
- Coord to checktrue
if c
is valid in level
, false
otherwise.public static <T> boolean inLevel(T[][] level, int x, int y)
level
- a dungeon/map level as 2D array. x,y indexedx
- x coordinate to checky
- y coordinate to checktrue
if c
is valid in level
, false
otherwise.public static int countCells(char[][] level, char match)
level
- the 2D char array to count cells inmatch
- the char to search forpublic static void debugPrint(char[][] level)
level
- a 2D char array to print with a trailing newlinepublic static char[][] wallWrap(char[][] map)
map
- A char[][] that stores map data; will be modified in placepublic static java.util.ArrayList<Coord> ensurePath(char[][] map, IRNG rng, char replacement, char... blocking)
pointPath(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.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 waypublic static java.util.ArrayList<Coord> allMatching(char[][] map, char... matching)
public static java.util.List<Coord> circle(int x, int y, int radius, java.util.List<Coord> buf)
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.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.(x, y)
, whose
diameter is (radius * 2) + 1
.if you want to keep the Coords within the bounds of the map
Copyright © Eben Howard 2012–2022. All rights reserved.