public class CoordPacker
extends java.lang.Object
init()
before using this class if you do not already use a SquidLib class that does so. It is a good habit
to call init() in your main() method or primary game entry point if you use CoordPacker. Failure to call init() will
not result in exceptions, but will make results inaccurate.
There's a detailed introduction
on the SquidLib wiki,
which is probably the best way to learn the techniques possible with this class. Most methods in this aren't useful
on their own, but can be mixed and matched to get specific regions from a map, such as all floors not adjacent to a
wall, or all grass within 3 squares of deep or shallow water, with walls blocking the distance measurement. You can
also use packed data that this class produces as keys for RegionMap
to associate values with regions.
GreasedRegion
, and there are times when code will do
better using CoordPacker than GreasedRegion and vice versa. GreasedRegion uses objects and tries to change them
in-place whenever possible instead of allocating new data; CoordPacker uses short[] values that it never changes
in-place and frequently allocates new short[] values to represent data. GreasedRegion stores the full map without any
compression beyond "one bit per cell" and always needs to store all cells on the map (sometimes slightly more);
CoordPacker can compress some data to significantly less than one bit per cell, and doesn't care about the maximum
bounds of the map as long as it fits inside a 256x256 square. GreasedRegion is significantly faster than CoordPacker
when performing any operations that move, shrink, or expand an area, such as expand(short[], int, int, int)
,
retract(short[], int, int, int)
, and translate(short[], int, int, int, int)
. CoordPacker usually
(not always) uses somewhat less memory than GreasedRegion, though both use very little unless the maps are very
large or very numerous. Both can do essentially the same things, except that GreasedRegion has no equivalent to the
packMulti(byte[][], int)
and other Multi kinds of method, no radiate(short[], short[], int)
, and no
reachable(short[], short[], Reach)
, while CoordPacker has no equivalent to
GreasedRegion.expandSeriesToLimit()
and other ToLimit kinds of method, no
GreasedRegion.toggle(int, int)
, no GreasedRegion.deteriorate(RandomnessSource, int)
, no
GreasedRegion.insert(int, int, GreasedRegion)
(though CoordPacker can generally use
unionPacked(short[], short[])
for that), and several other methods don't have equivalents. In general, you
will probably find GreasedRegion more intuitive because it involves working with objects instead of a short[] that is
treated like a particular kind of data, and the methods are also somewhat more clearly-named. GreasedRegion also
implements Collection of Coord, while the short[] data can't implement anything. A way around this for CoordPacker is
the CoordPackerZone
class; both that and GreasedRegion implement the
Zone
interface, which ensures a way to get a List of Coord from the Zone.
DungeonGenerator
that should be typical of
roguelike maps and a diamond-shaped FOV with radius 8, compression of the short[] returned by pack() vs.
the original double[][] (which wastefully represents 2 states with 8 bytes) yields average memory usage ratios
between (with relatively optimal parameters) 0.0001237905030818498 in one of the best cases, and (with some very
poor parameters for the dungeon, but still using a realistic FOV map) 0.003135985198889917 in one of the worst.
Bytes of RAM used, double 2D array | Bytes of RAM used, boolean 2D array | Average Bytes of RAM used, short 1D array (packed) | Compression ratio, packed vs. doubles | Compression ratio, packed vs. booleans | |
---|---|---|---|---|---|
240x240 dungeon map (good size) | 464656 | 61456 | 57.52 | 0.0001237905030818498 | 0.000935954178599323 |
30x70 dungeon map (bad size) | 17296 | 2656 | 54.24 | 0.003135985198889917 | 0.020421686746987953 |
Modifier and Type | Field and Description |
---|---|
static short[] |
ALL_ON |
static short[] |
ALL_WALL |
static int |
DEPTH |
static short[] |
hilbert3Distances |
static short[] |
hilbert3X |
static short[] |
hilbert3Y |
static short[] |
hilbert3Z |
static short[] |
hilbertDistances |
static short[] |
hilbertX |
static short[] |
hilbertY |
static short[] |
mooreDistances |
static short[] |
mooreX |
static short[] |
mooreY |
Modifier and Type | Method and Description |
---|---|
static Coord[] |
allPacked(short[] packed)
Gets all positions that are "on" in the given packed array, without unpacking it, and returns them as a Coord[].
|
static short[] |
allPackedHilbert(short[] packed)
Gets all positions that are "on" in the given packed array, without unpacking it, and returns them as an array of
Hilbert Curve indices.
|
static Coord[] |
apartPacked(short[] packed,
int minDistance)
Gets the positions that are "on" in the given packed array, without unpacking it, keeps only positions that are
at least minDistance apart from other positions this will return, and returns the positions as a Coord[].
|
static Coord[] |
apartPacked(short[] packed,
int minDistance,
boolean eightWay)
Gets the positions that are "on" in the given packed array, without unpacking it, keeps only positions that are
at least minDistance apart from other positions this will return, and returns the positions as a Coord[].
|
static short[] |
apartPackedHilbert(short[] packed,
int minDistance)
Gets the positions that are "on" in the given packed array, without unpacking it, keeps only positions that are
at least minDistance apart from other positions this will return, and returns the positions as an array of
Hilbert Curve indices.
|
static short[] |
apartPackedHilbert(short[] packed,
int minDistance,
boolean eightWay)
Gets the positions that are "on" in the given packed array, without unpacking it, keeps only positions that are
at least minDistance apart from other positions this will return, and returns the positions as an array of
Hilbert Curve indices.
|
static Coord[] |
bounds(short[] packed)
Finds the minimum bounding rectangle for a packed array without unpacking it.
|
static short[] |
circle(Coord center,
int radius,
int width,
int height)
Given a center and radius for a circle, plus the width and height for the map boundaries, returns the packed data
that encodes the circle.
|
static int |
coordToHilbert(Coord pt)
Takes a position as a Coord called pt and returns the length to travel along the 256x256 Hilbert curve to reach
that position.
|
static int |
coordToMoore(Coord pt)
Takes a position as a Coord called pt and returns the length to travel along the 16x16 Moore curve to reach
that position.
|
static int |
count(short[] packed)
Counts the number of "on" cells encoded in a packed array without unpacking it.
|
static int |
count(short[] packed,
boolean wanted)
Counts the number of cells encoding a boolean equal to wanted in a packed array without unpacking it.
|
static int |
covered(short[] packed)
Finds how many cells are encoded in a packed array (both on and off) without unpacking it.
|
static short[] |
decodeASCII(java.lang.String text)
Given a String specifically produced by CoordPacker.encodeASCII(), this will produce a packed data array.
|
static short[] |
decodeBraille(java.lang.String text)
Given a String specifically produced by CoordPacker.encodeBraille(), this will produce a packed data array.
|
static short[] |
differencePacked(short[] left,
short[] right)
Given two packed short arrays, left and right, this produces a packed short array that encodes "on" for any cell
that was "on" in left but "off" in right, and encodes "off" for cells that were "on" in right or "off" in left.
|
static java.lang.String |
encodeASCII(short[] packed)
Encodes a short array of packed data as a (larger, more memory-hungry) ASCII string, which can be decoded using
CoordPacker.decodeASCII() .
|
static java.lang.String |
encodeBraille(short[] packed)
Encodes a short array of packed data as a (larger, slightly more memory-hungry) Unicode string using only Braille
characters, which can be decoded using CoordPacker.decodeBraille().
|
static short[] |
expand(short[] packed,
int expansion,
int width,
int height)
Expand each "on" position in packed to cover a a square with side length equal to 1 + expansion * 2,
centered on the original "on" position, unless the expansion would take a cell further than 0,
width - 1 (for xMove) or height - 1 (for yMove), in which case that cell is stopped at the edge.
|
static short[] |
expand(short[] packed,
int expansion,
int width,
int height,
boolean eightWay)
Expand each "on" position in packed to cover a a square with side length equal to 1 + expansion * 2,
centered on the original "on" position, unless the expansion would take a cell further than 0,
width - 1 (for xMove) or height - 1 (for yMove), in which case that cell is stopped at the edge.
|
static OrderedSet<short[]> |
findManyPacked(int x,
int y,
java.util.Collection<short[]> packed)
Quickly determines if an x,y position is true or false in one of the given packed arrays, without unpacking them,
and returns a List of all packed arrays that contain the position.
|
static OrderedSet<short[]> |
findManyPacked(int x,
int y,
short[]... packed)
Quickly determines if an x,y position is true or false in one of the given packed arrays, without unpacking them,
and returns a List of all packed arrays that contain the position.
|
static java.util.ArrayList<short[]> |
findManyPackedHilbert(short hilbert,
short[]... packed)
Quickly determines if a Hilbert Curve index corresponds to true or false in one of the given packed arrays,
without unpacking them, and returns a List of all packed arrays that contain the position.
|
static short[] |
flood(short[] bounds,
short[] start,
int expansion)
Given a packed array encoding a larger area, a packed array encoding one or more points inside bounds, and an
amount of expansion, expands each cell in start by a Manhattan (diamond) radius equal to expansion, limiting any
expansion to within bounds and returning the final expanded (limited) packed data.
|
static short[] |
flood(short[] bounds,
short[] start,
int expansion,
boolean eightWay)
Given a packed array encoding a larger area, a packed array encoding one or more points inside bounds, and an
amount of expansion, expands each cell in start by a radius (if eightWay is true, it uses Chebyshev distance; if
it is false, it uses Manhattan distance) equal to expansion, limiting any expansion to within bounds and
returning the final expanded (limited) packed data.
|
static Coord[] |
fractionPacked(short[] packed,
int fraction)
Gets the positions that are "on" in the given packed array, without unpacking it, repeatedly goes through a
number of "on" cells equal to fraction and stores one of those cells as a Coord, and returns the accumulated
portion of positions as a Coord[].
|
static short[] |
fractionPackedHilbert(short[] packed,
int fraction)
Gets the positions that are "on" in the given packed array, without unpacking it, repeatedly goes through a
number of "on" cells equal to fraction and stores one of those cells as a Coord, and returns the accumulated
portion of positions as an array of Hilbert Curve indices.
|
static short[] |
fringe(short[] packed,
int expansion,
int width,
int height)
Finds the area around the cells encoded in packed, without including those cells.
|
static short[] |
fringe(short[] packed,
int expansion,
int width,
int height,
boolean eightWay)
Finds the area around the cells encoded in packed, without including those cells.
|
static short[] |
fringe(short[] packed,
int expansion,
int width,
int height,
boolean eightWay,
boolean drop)
Finds the area around the cells encoded in packed, without including those cells.
|
static short[][] |
fringes(short[] packed,
int expansions,
int width,
int height)
Finds the concentric areas around the cells encoded in packed, without including those cells.
|
static short[][] |
fringes(short[] packed,
int expansions,
int width,
int height,
boolean eightWay)
Finds the concentric areas around the cells encoded in packed, without including those cells.
|
static double[] |
generateLightLevels(int totalLevels)
Given a number of total levels to consider separate in a double[][] such as an FOV result, this produces a levels
array that can be passed to unpackMultiDouble() to ensure that the minimum double returned for an "on" cell is
1.0 / totalLevels, and every progressively tighter level in the short[][] being unpacked will be close to a
multiple of that minimum double value.
|
static double[] |
generatePackingLevels(int totalLevels)
Given a number of total levels to consider separate in a double[][] such as an FOV result, this produces a levels
array that can be passed to packMulti() to ensure that you have the requested number of separate levels in the
multi-packed result.
|
static int |
getXMoore3D(int index,
int n)
Gets the x coordinate for a given index into the 16x16x(8*n) Moore curve.
|
static int |
getYMoore3D(int index,
int n)
Gets the y coordinate for a given index into the 16x16x(8*n) Moore curve.
|
static int |
getZMoore3D(int index,
int n)
Gets the z coordinate for a given index into the 16x16x(8*n) Moore curve.
|
static int |
grayDecode(int n)
Decode a number from a Gray code n; Gray codes have a relation to the Hilbert curve and may be useful.
|
static int |
grayEncode(int n)
Encode a number n as a Gray code; Gray codes have a relation to the Hilbert curve and may be useful.
|
static Coord |
hilbertToCoord(int hilbert)
Takes a distance to travel along the 256x256 Hilbert curve and returns a Coord representing the position
in 2D space that corresponds to that point on the Hilbert curve.
|
static int |
hilbertToMorton(int hilbert)
Takes a distance to travel along the 256x256 Hilbert curve and returns a Morton code representing the position
in 2D space that corresponds to that point on the Hilbert Curve; the Morton code will have interleaved x and y
bits and x in the least significant bit.
|
static void |
init() |
static short[] |
insertPacked(short[] original,
int x,
int y)
Given one packed short array, original, and a position as x,y numbers, this produces a packed short array
that encodes "on" for any cell that was "on" in original, always encodes "on" for the position referred
to by x and y, and encodes "off" for cells that were "off" in original and are not the cell x and y refer to.
|
static short[] |
insertPacked(short[] original,
short hilbert)
Given one packed short array, original, and a Hilbert Curve index, hilbert, this produces a packed short array
that encodes "on" for any cell that was "on" in original, always encodes "on" for the position referred
to by hilbert, and encodes "off" for cells that were "off" in original and are not the cell hilbert refers to.
|
static short[] |
insertSeveralPacked(short[] original,
java.util.Collection<Coord> points)
Given one packed short array, original, and a Collection of Coords, points, this produces a packed
short array that encodes "on" for any cell that was "on" in original, always encodes "on" for the position
referred to by any element of points, and encodes "off" for cells that were "off" in original and are not in any
cell points refers to.
|
static short[] |
insertSeveralPacked(short[] original,
Coord... points)
Given one packed short array, original, and a number of Coords, points, this produces a packed
short array that encodes "on" for any cell that was "on" in original, always encodes "on" for the position
referred to by any element of points, and encodes "off" for cells that were "off" in original and are not in any
cell points refers to.
|
static short[] |
insertSeveralPacked(short[] original,
int... hilbert)
Given one packed short array, original, and a number of Hilbert Curve indices, hilbert, this produces a packed
short array that encodes "on" for any cell that was "on" in original, always encodes "on" for the position
referred to by any element of hilbert, and encodes "off" for cells that were "off" in original and are not in any
cell hilbert refers to.
|
static short[] |
intersectPacked(short[] left,
short[] right)
Given two packed short arrays, left and right, this produces a packed short array that encodes "on" for any cell
that was "on" in both left and in right, and encodes "off" for cells that were off in either array.
|
static boolean |
intersects(short[] left,
short[] right)
Given two packed short arrays, left and right, this returns true if they encode any overlapping area (their areas
intersect), or false if they do not overlap at all (they don't intersect).
|
static boolean |
isEmpty(short[] packed)
Checks if no cells are encoded as "on" in packed.
|
static char[][] |
mask(char[][] map,
short[] packed,
char filler)
Given a 2D char array for a map, a piece of packed data defining a region to use from that map, and a filler
char, produces a 2D char array where all positions that are "off" in packed are filled with filler, and the rest
are the same as in map.
|
static Coord |
mooreToCoord(int moore)
Takes a distance to travel along the 16x16 Hilbert curve and returns a Coord representing the position
in 2D space that corresponds to that point on the Hilbert curve.
|
static int |
mortonBitDecode3D(int morton) |
static Coord |
mortonDecode(int morton)
Takes a Morton code, with interleaved x and y bits and x in the least significant bit, and returns the Coord
representing the same x, y position.
|
static Coord3D |
mortonDecode3D(int morton) |
static int |
mortonEncode(int index1,
int index2)
Takes two 8-bit unsigned integers index1 and index2, and returns a Morton code, with interleaved index1 and
index2 bits and index1 in the least significant bit.
|
static int |
mortonEncode3D(int index1,
int index2,
int index3) |
static int |
mortonToHilbert(int morton)
Takes a position as a Morton code, with interleaved x and y bits and x in the least significant bit, and returns
the length to travel along the 256x256 Hilbert Curve to reach that position.
|
static short[] |
negatePacked(short[] original)
Given one packed short array, this produces a packed short array that is the exact opposite of the one passed in,
that is, every "on" cell becomes "off" and every "off" cell becomes "on", including cells that were "off" because
they were beyond the boundaries of the original 2D array passed to pack() or a similar method.
|
static Coord |
nth(short[] packed,
int n)
Gets the nth position that is "on" in the given packed array, without unpacking it, and returns it as a Coord.
|
static short[] |
pack(boolean[][] map)
Compresses a boolean[][], returning a short[] as described in the
CoordPacker class documentation. |
static short[] |
pack(byte[][] map)
Compresses a byte[][] (typically one generated by an FOV-like method) that only stores two
relevant states (one of which should be 0 or less, the other greater than 0), returning a short[] as described in
the
CoordPacker class documentation. |
static short[] |
pack(char[][] map,
char... yes)
Compresses a char[][] (typically one generated by a map generating method) so only the cells that are contained
in the yes parameter will be encoded as "on", returning a short[] as described in
the
CoordPacker class documentation. |
static short[] |
pack(char[][] map,
char yes)
Compresses a char[][] (typically one generated by a map generating method) so only the cells that equal the yes
parameter will be encoded as "on", returning a short[] as described in
the
CoordPacker class documentation. |
static short[] |
pack(double[][] map)
Compresses a double[][] (typically one generated by
FOV ) that only stores two
relevant states (one of which should be 0 or less, the other greater than 0), returning a short[] as described in
the CoordPacker class documentation. |
static short[] |
pack(double[][] map,
double threshold)
Compresses a double[][] (typically one generated by
DijkstraMap ) that only stores two
relevant states (one of which should be equal to or less than threshold, the other greater than threshold),
returning a short[] as described in the CoordPacker class documentation. |
static short[] |
pack(double[][] map,
double lowerBound,
double upperBound)
Compresses a double[][] (typically one generated by
DijkstraMap ) that only stores two
relevant states (a state for values between lowerBound (inclusive) and upperBound (exclusive), and another state
for anything else), returning a short[] as described in the CoordPacker class documentation. |
static short[] |
pack(int[][] map,
int... yes)
Compresses a int[][] (typically one generated by MixedGenerator.getEnvironment()) so only the cells that are
contained in the yes parameter will be encoded as "on", returning a short[] as described in
the
CoordPacker class documentation. |
static short[] |
pack(int[][] map,
int yes)
Compresses a int[][] (typically one generated by MixedGenerator.getEnvironment()) so only the cells that equal
the yes parameter will be encoded as "on", returning a short[] as described in
the
CoordPacker class documentation. |
static short[][] |
packMulti(byte[][] map,
int levelCount)
Compresses a byte[][] that stores any number of states, and an int no more than 63, returning a short[][] as
described in the
CoordPacker class documentation. |
static short[][] |
packMulti(double[][] map,
double[] levels)
Compresses a double[][] (typically one generated by
FOV ) that stores any number of
states and a double[] storing up to 63 states, ordered from lowest to highest, returning a short[][] as described
in the CoordPacker class documentation. |
static short[] |
packOne(Coord point)
Returns a new packed short[] containing the Coord point as "on", and all other cells "off".
|
static short[] |
packOne(int hilbert)
Returns a new packed short[] containing the Hilbert distance hilbert as "on", and all other cells "off".
|
static short[] |
packOne(int x,
int y)
Returns a new packed short[] containing the given x,y cell as "on", and all other cells "off".
|
static short[] |
packSeveral(java.util.Collection<Coord> points)
Returns a new packed short[] containing the Coords in points as "on" cells, and all other cells "off"
|
static short[] |
packSeveral(Coord... points)
Returns a new packed short[] containing the Coords in points as "on" cells, and all other cells "off"
|
static short[] |
packSeveral(int... hilbert)
Returns a new packed short[] containing the Hilbert distances in hilbert as "on" cells, and all other cells "off"
|
static int |
posToHilbert(int x,
int y)
Takes an x, y position and returns the length to travel along the 256x256 Hilbert curve to reach that position.
|
static int |
posToHilbert3D(int x,
int y,
int z)
Takes an x, y, z position and returns the length to travel along the 8x8x8 Hilbert curve to reach that
position.
|
static int |
posToMoore(int x,
int y)
Takes an x, y position and returns the length to travel along the 16x16 Moore curve to reach that position.
|
static void |
printCompressedData(short[] packed) |
static void |
printPacked(short[] packed,
int width,
int height)
Quick utility method for printing packed data as a grid of 1 (on) and/or 0 (off).
|
static boolean |
queryPacked(short[] packed,
int x,
int y)
Quickly determines if an x,y position is true or false in the given packed array, without unpacking it.
|
static boolean |
queryPackedHilbert(short[] packed,
short hilbert)
Quickly determines if a Hilbert Curve index corresponds to true or false in the given packed array, without
unpacking it.
|
static short[] |
radiate(short[] bounds,
short[] start,
int expansion)
Given a packed array encoding a larger area, a packed array encoding one or more points inside bounds, and an
amount of expansion, expands each cell in start by a Manhattan (diamond) radius equal to expansion, limiting any
expansion to within bounds and returning the final expanded (limited) packed data.
|
static short[] |
radiate(short[] bounds,
short[] start,
int expansion,
boolean eightWay)
Given a packed array encoding a larger area, a packed array encoding one or more points inside bounds, and an
amount of expansion, expands each cell in start by a radius, with a square shape if eightWay is true or a diamond
otherwise, equal to expansion, limiting any expansion to within bounds and returning the final expanded (limited)
packed data.
|
static short[] |
radiate(short[] bounds,
short[] start,
int expansion,
Radius metric)
Given a packed array encoding a larger area, a packed array encoding one or more points inside bounds, and an
amount of expansion, expands each cell in start by a radius, with a shape determined by metric, equal to
expansion, limiting any expansion to within bounds and returning the final expanded (limited) packed data.
|
static java.util.ArrayList<Coord> |
randomPortion(short[] packed,
int size,
IRNG rng)
Gets a fixed number of randomly chosen positions that are "on" in the given packed array, without unpacking it,
and returns a List of Coord with a count equal to size (or less if there aren't enough "on" cells).
|
static Coord[] |
randomSample(short[] packed,
double fraction,
IRNG rng)
Gets a random subset of positions that are "on" in the given packed array, without unpacking it, and returns
them as a Coord[].
|
static Coord[] |
randomSeparated(short[] packed,
int separation,
IRNG rng)
Gets the positions that are "on" in the given packed array, without unpacking it, repeatedly goes through a
number of "on" cells equal to fraction and stores a random one of those cells as a Coord, and returns the
accumulated random portion of positions as a Coord[].
|
static short[] |
reachable(short[] bounds,
short[] start,
Reach reach)
Given a packed array encoding a larger area, a packed array encoding one or more points inside bounds, and a
Reach object that determines targeting constraints, gets all cells contained within bounds that can be targeted
from a cell in start using the rules defined by reach.
|
static short[] |
rectangle(int width,
int height)
Given a width and height, returns a packed array that encodes "on" for the rectangle from (0,0) to
(width - 1, height - 1).
|
static short[] |
rectangle(int x,
int y,
int width,
int height)
Given x, y, width and height, returns a packed array that encodes "on" for the rectangle from (x,y) to
(width + x - 1, height + y - 1).
|
static short[] |
rectangleHilbert(int x,
int y,
int width,
int height)
Given x, y, width and height, returns an array of all Hilbert distance within the rectangle from (x,y) to
(width + x - 1, height + y - 1).
|
static boolean |
regionsContain(short[] checking,
java.util.Collection<short[]> packed)
Quickly determines if a region is contained in one of the given packed arrays, without unpacking them, and
returns true if the region checking has some overlap with any of the packed arrays, or false otherwise.
|
static boolean |
regionsContain(short[] checking,
short[]... packed)
Quickly determines if a region is contained in one of the given packed arrays, without unpacking them, and
returns true if the region checking has some overlap with any of the packed arrays, or false otherwise.
|
static short[] |
removeIsolated(short[] packed) |
static short[] |
removePacked(short[] original,
int x,
int y)
Given one packed short array, original, and a position as x,y numbers, this produces a packed short array that
encodes "on" for any cell that was "on" in original, unless it was the position referred to by x and y, and
encodes "off" for cells that were "off" in original or are the cell x and y refer to.
|
static short[] |
removePacked(short[] original,
short hilbert)
Given one packed short array, original, and a Hilbert Curve index, hilbert, this produces a packed short array
that encodes "on" for any cell that was "on" in original, unless it was the position referred to by hilbert, and
encodes "off" for cells that were "off" in original or are the cell hilbert refers to.
|
static short[] |
removeSeveralPacked(short[] original,
java.util.Collection<Coord> points)
Given one packed short array, original, and a number of Coords, points, this produces a packed short
array that encodes "on" for any cell that was "on" in original, unless it was a position referred to by an element
in points, and encodes "off" for cells that were "off" in original and are a cell points refers to.
|
static short[] |
removeSeveralPacked(short[] original,
Coord... points)
Given one packed short array, original, and a number of Coords, points, this produces a packed short
array that encodes "on" for any cell that was "on" in original, unless it was a position referred to by an element
in points, and encodes "off" for cells that were "off" in original and are a cell points refers to.
|
static short[] |
removeSeveralPacked(short[] original,
int... hilbert)
Given one packed short array, original, and a number of Hilbert Curve indices, hilbert, this produces a packed
short array that encodes "on" for any cell that was "on" in original, unless it was a position referred to by
hilbert, and encodes "off" for cells that were "off" in original and are a cell hilbert refers to.
|
static short[] |
retract(short[] packed,
int retraction,
int width,
int height)
Finds the area made by removing the "on" positions in packed that are within the specified retraction distance of
an "off" position or the edge of the map.
|
static short[] |
retract(short[] packed,
int retraction,
int width,
int height,
boolean eightWay)
Finds the area made by removing the "on" positions in packed that are within the specified retraction distance of
an "off" position or the edge of the map.
|
static Coord |
singleRandom(short[] packed,
IRNG rng)
Gets a single randomly chosen position that is "on" in the given packed array, without unpacking it, and returns
it as a Coord or returns null of the array is empty.
|
static short[] |
spill(short[] bounds,
short[] start,
int volume,
IRNG rng)
Given a packed array encoding a larger area, a packed array encoding one or more points inside bounds, an IRNG,
and a volume in cells, expands a random cell in start in a random Manhattan (diamond) direction equal, then
continues to expand from random cells in start or the expanded area until it has filled volume cells, limiting
any expansion to within bounds and returning the final expanded (limited) packed data.
|
static java.util.ArrayList<short[]> |
split(short[] packed)
Given a packed data array that encodes multiple unconnected "on" areas, this finds each isolated area (areas that
are only adjacent diagonally are considered separate from each other) and returns it as an element in an
ArrayList of short[], with one short[] array per isolated area.
|
static int[][] |
sumMany(int width,
int height,
short[]... many)
Takes multiple pieces of packed data as short[], encoded by pack() or another similar method of this class, and
generates a 2D int array with the specified width and height and a starting value of 0 for all elements, then
where every occurrence of a cell as "on" in a piece of packed data increments the cell's value in the returned
array.
|
static short[] |
surface(short[] packed,
int depth,
int width,
int height)
Finds the area consisting of the "on" positions in packed that are within the specified depth distance of an
"off" position or the edge of the map.
|
static short[] |
surface(short[] packed,
int depth,
int width,
int height,
boolean eightWay)
Finds the area consisting of the "on" positions in packed that are within the specified depth distance of an
"off" position or the edge of the map.
|
static short[][] |
surfaces(short[] packed,
int depth,
int width,
int height)
Finds the concentric, progressively-smaller surfaces of packed as if packed was shrinking with each iteration.
|
static short[][] |
surfaces(short[] packed,
int depth,
int width,
int height,
boolean eightWay)
Finds the concentric, progressively-smaller surfaces of packed as if packed was shrinking with each iteration.
|
static short[] |
translate(short[] packed,
int xMove,
int yMove,
int width,
int height)
Move all "on" positions in packed by the number of cells given in xMove and yMove, unless the move
would take them further than 0, width - 1 (for xMove) or height - 1 (for yMove), in which case that
cell is stopped at the edge (moving any shape by an xMove greater than width or yMove greater than
height will move all "on" cells to that edge, in a 1-cell thick line).
|
static short[] |
unionPacked(short[] left,
short[] right)
Given two packed short arrays, left and right, this produces a packed short array that encodes "on" for any cell
that was "on" in either left or in right, and only encodes "off" for cells that were off in both.
|
static boolean[][] |
unpack(short[] packed,
int width,
int height)
Decompresses a short[] returned by pack() or a sub-array of a short[][] returned by packMulti(), as described in
the
CoordPacker class documentation. |
static char[][] |
unpackChar(short[] packed,
char t,
char f)
Given a piece of packed data defining a region to use from that map, a char to use for "on" cells and a char to use
for "off" cells, produces a 2D char array where all positions that are "off" in packed are filled with the char
passed as f, and the cells that are "on" are filled with the char passed as t.
|
static char[][] |
unpackChar(short[] packed,
int width,
int height,
char t,
char f)
Given a piece of packed data defining a region to use from that map, a desired width and height, a char to use for
"on" cells and a char to use for "off" cells, produces a 2D char array where all positions that are "off" in packed
are filled with the char passed as f, and the cells that are "on" are filled with the char passed as t.
|
static double[][] |
unpackDouble(short[] packed,
int width,
int height)
Decompresses a short[] returned by pack() or a sub-array of a short[][] returned by packMulti(), as described in
the
CoordPacker class documentation. |
static double[][] |
unpackDoubleConical(short[] packed,
int width,
int height,
int centerX,
int centerY,
double angle,
double span)
Decompresses a short[] returned by pack() or a sub-array of a short[][] returned by packMulti(), as described in
the
CoordPacker class documentation. |
static GreasedRegion |
unpackGreasedRegion(short[] packed,
int width,
int height)
Utility method that constructs a GreasedRegion (a faster but more-memory-hungry way to encode regions)
from a short array of packed data.
|
static GreasedRegion |
unpackIntoGreasedRegion(short[] packed,
GreasedRegion target)
Utility method that fills an existing GreasedRegion
target with any "on" cells in the packed short array
packed . |
static GreasedRegion |
unpackIntoGreasedRegion(short[] packed,
GreasedRegion target,
int offsetX,
int offsetY)
Utility method that fills an existing GreasedRegion
target with any "on" cells in the packed short array
packed , inserting cells from packed at an offset when they go into target. |
static byte[][] |
unpackMultiByte(short[][] packed,
int width,
int height)
Decompresses a short[][] returned by packMulti() and produces a simple 2D array where the values are bytes
corresponding to 1 + the highest index into levels (that is, the original levels parameter passed to packMulti)
matched by a cell, or 0 if the cell didn't match any levels during compression, as described in the
CoordPacker class documentation. |
static double[][] |
unpackMultiDouble(short[][] packed,
int width,
int height,
double[] levels)
Decompresses a short[][] returned by packMulti() and produces an approximation of the double[][] it compressed
using the given levels double[] as the values to assign, as described in the
CoordPacker class
documentation. |
static double[][] |
unpackMultiDoublePartial(short[][] packed,
int width,
int height,
double[] levels,
int limit)
Decompresses a short[][] returned by packMulti() and produces an approximation of the double[][] it compressed
using the given levels double[] as the values to assign, but only using the innermost indices up to limit, as
described in the
CoordPacker class documentation. |
static double[][] |
unpackMultiDoublePartialConical(short[][] packed,
int width,
int height,
double[] levels,
int limit,
int centerX,
int centerY,
double angle,
double span)
Decompresses a short[][] returned by packMulti() and produces an approximation of the double[][] it compressed
using the given levels double[] as the values to assign, but only using the innermost indices up to limit, as
described in the
CoordPacker class documentation. |
static short[] |
xorPacked(short[] left,
short[] right)
Given two packed short arrays, left and right, this produces a packed short array that encodes "on" for any cell
that was "on" only in left or only in right, but not a cell that was "off" in both or "on" in both.
|
static Coord |
zDecode(short morton)
Takes a Morton code, with interleaved x and y bits and x in the least significant bit, and returns the Coord
representing the same x, y position.
|
static short |
zEncode(short index1,
short index2)
Takes two 8-bit unsigned integers index1 and index2, and returns a Morton code, with interleaved index1 and
index2 bits and index1 in the least significant bit.
|
public static final int DEPTH
public static short[] hilbertX
public static short[] hilbertY
public static short[] hilbertDistances
public static short[] mooreX
public static short[] mooreY
public static short[] mooreDistances
public static short[] hilbert3X
public static short[] hilbert3Y
public static short[] hilbert3Z
public static short[] hilbert3Distances
public static short[] ALL_WALL
public static short[] ALL_ON
public static void init()
public static short[] pack(double[][] map)
FOV
) that only stores two
relevant states (one of which should be 0 or less, the other greater than 0), returning a short[] as described in
the CoordPacker
class documentation. This short[] can be passed to CoordPacker.unpack() to restore the
relevant states and their positions as a boolean[][] (with false meaning 0 or less and true being any double
greater than 0). As stated in the class documentation, the compressed result is intended to use as little memory
as possible for most roguelike FOV maps. To avoid floating-point number comparison issues, this actually needs
doubles to be greater than 0.0001, which should never cause incorrect behavior with FOV's double[][] maps.
map
- a double[][] that probably was returned by FOV. If you obtained a double[][] from DijkstraMap, it
will not meaningfully compress with this method.public static short[] pack(double[][] map, double threshold)
DijkstraMap
) that only stores two
relevant states (one of which should be equal to or less than threshold, the other greater than threshold),
returning a short[] as described in the CoordPacker
class documentation. This short[] can be passed to
CoordPacker.unpack() to restore the relevant states and their positions as a boolean[][] (with true meaning
threshold or less and false being any double greater than threshold). As stated in the class documentation, the
compressed result is intended to use as little memory as possible for most roguelike FOV maps, but here is also
useful for compressing physical maps and gradient maps from DijkstraMap.
map
- a double[][] that probably relates in some way to DijkstraMap.threshold
- upper inclusive; any double greater than this will be off, any equal or less will be onpublic static short[] pack(double[][] map, double lowerBound, double upperBound)
DijkstraMap
) that only stores two
relevant states (a state for values between lowerBound (inclusive) and upperBound (exclusive), and another state
for anything else), returning a short[] as described in the CoordPacker
class documentation. This short[]
can be passed to CoordPacker.unpack() to restore the relevant states and their positions as a boolean[][] (with
true meaning between the bounds and false being anything outside them). As stated in the class documentation, the
compressed result is intended to use as little memory as possible for most roguelike FOV maps, but here is also
useful for compressing physical maps and gradient maps from DijkstraMap.
map
- a double[][] that probably relates in some way to DijkstraMap.lowerBound
- lower inclusive; any double lower than this will be off, any equal to or greater than this,
but less than upper, will be onupperBound
- upper exclusive; any double greater than this will be off, any doubles both less than this
and equal to or greater than lower will be onpublic static short[] pack(byte[][] map)
CoordPacker
class documentation. This short[] can be passed to CoordPacker.unpack() to restore the
relevant states and their positions as a boolean[][] (with false meaning 0 or less and true being any byte
greater than 0). As stated in the class documentation, the compressed result is intended to use as little memory
as possible for most roguelike FOV maps.
map
- a byte[][] that probably was returned by an FOV-like method.public static short[] pack(boolean[][] map)
CoordPacker
class documentation. This
short[] can be passed to CoordPacker.unpack() to restore the relevant states and their positions as a boolean[][]
As stated in the class documentation, the compressed result is intended to use as little memory as possible for
most roguelike FOV maps.map
- a boolean[][] that should ideally be mostly false.public static short[] pack(char[][] map, char yes)
CoordPacker
class documentation. This short[] can be passed to CoordPacker.unpack() to restore the
positions of chars that equal the parameter yes as a boolean[][] (with false meaning not equal and true equal to
yes). As stated in the class documentation, the compressed result is intended to use as little memory
as possible for most roguelike FOV maps, but this will typically not be used for FOV (more typical uses are for
walls, floors, and so on). This can still be useful for certain kinds of processing that can be done more
efficiently on packed data than on 2D arrays, like unions, intersections, and random elements or subsets.map
- a char[][] that may contain some area of cells that you want stored as packed datayes
- the char to encode as "on" in the result; all others are encoded as "off"public static short[] pack(char[][] map, char... yes)
CoordPacker
class documentation. This short[] can be passed to CoordPacker.unpack() to restore the
positions of chars that equal the parameter yes as a boolean[][] (with false meaning not equal and true equal to
yes). As stated in the class documentation, the compressed result is intended to use as little memory
as possible for most roguelike FOV maps, but this will typically not be used for FOV (more typical uses are for
walls, floors, and so on). This can still be useful for certain kinds of processing that can be done more
efficiently on packed data than on 2D arrays, like unions, intersections, and random elements or subsets.map
- a char[][] that may contain some area of cells that you want stored as packed datayes
- the vararg or array of chars to encode as "on" in the result; all others are encoded as "off"public static short[] pack(int[][] map, int yes)
CoordPacker
class documentation. This short[] can be passed to CoordPacker.unpack() to restore the
positions of ints that equal the parameter yes as a boolean[][] (with false meaning not equal and true equal to
yes). As stated in the class documentation, the compressed result is intended to use as little memory
as possible for most roguelike FOV maps, but this will typically not be used for FOV (more typical uses are for
walls, floors, and so on). This can still be useful for certain kinds of processing that can be done more
efficiently on packed data than on 2D arrays, like unions, intersections, and random elements or subsets.map
- a int[][] that may contain some area of cells that you want stored as packed datayes
- the int to encode as "on" in the result; all others are encoded as "off"public static short[] pack(int[][] map, int... yes)
CoordPacker
class documentation. This short[] can be passed to CoordPacker.unpack() to restore the
positions of ints that equal the parameter yes as a boolean[][] (with false meaning not equal and true equal to
yes). As stated in the class documentation, the compressed result is intended to use as little memory
as possible for most roguelike FOV maps, but this will typically not be used for FOV (more typical uses are for
walls, floors, and so on). This can still be useful for certain kinds of processing that can be done more
efficiently on packed data than on 2D arrays, like unions, intersections, and random elements or subsets.map
- a int[][] that may contain some area of cells that you want stored as packed datayes
- the vararg or array of ints to encode as "on" in the result; all others are encoded as "off"public static double[] generatePackingLevels(int totalLevels)
totalLevels
- the number of separate levels to group doubles intopublic static double[] generateLightLevels(int totalLevels)
totalLevels
- the number of separate levels to assign doubles; this MUST match the size of the levels
parameter used to pack a double[][] with packMulti() if this is used to unpack that datapublic static short[][] packMulti(double[][] map, double[] levels)
FOV
) that stores any number of
states and a double[] storing up to 63 states, ordered from lowest to highest, returning a short[][] as described
in the CoordPacker
class documentation. This short[][] can be passed to CoordPacker.unpackMultiDouble()
to restore the state at a position to the nearest state in levels, rounded down, and return a double[][] that
should preserve the states as closely as intended for most purposes. For compressing FOV, you should generate
levels with CoordPacker.generatePackingLevels() instead of manually creating the array, because some
imprecision is inherent in floating point math and comparisons are often incorrect between FOV with multiple
levels and exact levels generated as simply as possible. generatePackingLevels() adds a small correction to the
levels to compensate for floating-point math issues, which shouldn't affect the correctness of the results for
FOV radii under 100.
packMulti(dijkstraArray, new double[]{DijkstraMap.WALL});
Then, you would use only the one sub-element of the returned short[][].map
- a double[][] that probably was returned by FOV. If you obtained a double[][] from DijkstraMap, it
will not meaningfully compress with this method unless you have very specific needs.levels
- a double[] starting with the lowest value that should be counted as "on" (the outermost cells of
an FOV map that has multiple grades of brightness would be counted by this) and ascending until the
last value; the last value should be highest (commonly 1.0 for FOV), and will be used for any cells
higher than all the other levels values. An example is an array of: 0.25, 0.5, 0.75, 1.0public static short[][] packMulti(byte[][] map, int levelCount)
CoordPacker
class documentation. This short[][] can be passed to
unpackMultiByte(short[][], int, int)
to restore the state at a position to the nearest state possible,
capped at levelCount, and return a byte[][] that should preserve the states as closely as intended for most
purposes.
map
- a byte[][] that probably was returned by a specialized FOV.levelCount
- an int expressing how many levels should be present in the output; values greater than
levelCount in map will be treated as the highest level.public static boolean[][] unpack(short[] packed, int width, int height)
CoordPacker
class documentation. This returns a boolean[][] that stores the same values that were
packed if the overload of pack() taking a boolean[][] was used. If a double[][] was compressed with pack(), the
boolean[][] this returns will have true for all values greater than 0 and false for all others. If this is one
of the sub-arrays compressed by packMulti(), the index of the sub-array will correspond to an index in the levels
array passed to packMulti(), and any cells that were at least equal to the corresponding value in levels will be
true, while all others will be false. Width and height do not technically need to match the dimensions of the
original 2D array, but under most circumstances where they don't match, the data produced will be junk.packed
- a short[] encoded by calling one of this class' packing methods on a 2D array.width
- the width of the 2D array that will be returned; should match the unpacked array's width.height
- the height of the 2D array that will be returned; should match the unpacked array's height.public static double[][] unpackDouble(short[] packed, int width, int height)
CoordPacker
class documentation. This returns a double[][] that stores 1.0 for true and 0.0 for
false if the overload of pack() taking a boolean[][] was used. If a double[][] was compressed with pack(), the
double[][] this returns will have 1.0 for all values greater than 0 and 0.0 for all others. If this is one
of the sub-arrays compressed by packMulti(), the index of the sub-array will correspond to an index in the levels
array passed to packMulti(), and any cells that were at least equal to the corresponding value in levels will be
1.0, while all others will be 0.0. Width and height do not technically need to match the dimensions of the
original 2D array, but under most circumstances where they don't match, the data produced will be junk.packed
- a short[] encoded by calling one of this class' packing methods on a 2D array.width
- the width of the 2D array that will be returned; should match the unpacked array's width.height
- the height of the 2D array that will be returned; should match the unpacked array's height.public static double[][] unpackDoubleConical(short[] packed, int width, int height, int centerX, int centerY, double angle, double span)
CoordPacker
class documentation. This returns a double[][] that stores 1.0 for true and 0.0 for
false if the overload of pack() taking a boolean[][] was used. If a double[][] was compressed with pack(), the
double[][] this returns will have 1.0 for all values greater than 0 and 0.0 for all others. If this is one
of the sub-arrays compressed by packMulti(), the index of the sub-array will correspond to an index in the levels
array passed to packMulti(), and any cells that were at least equal to the corresponding value in levels will be
1.0, while all others will be 0.0. Width and height do not technically need to match the dimensions of the
original 2D array, but under most circumstances where they don't match, the data produced will be junk.packed
- a short[] encoded by calling one of this class' packing methods on a 2D array.width
- the width of the 2D array that will be returned; should match the unpacked array's width.height
- the height of the 2D array that will be returned; should match the unpacked array's height.public static double[][] unpackMultiDouble(short[][] packed, int width, int height, double[] levels)
CoordPacker
class
documentation. The length of levels and the length of the outer array of packed must be equal. However, the
levels array passed to this method should not be identical to the levels array passed to packMulti(); for FOV
compression, you should get an array for levels using generatePackingLevels(), but for decompression, you should
create levels using generateLightLevels(), which should more appropriately fit the desired output. Reusing the
levels array used to pack the FOV will usually produce values at the edge of FOV that are less than 0.01 but
greater than 0, and will have a maximum value somewhat less than 1.0; neither are usually desirable, but using a
different array made with generateLightLevels() will produce doubles ranging from 1.0 / levels.length to 1.0 at
the highest. Width and height do not technically need to match the dimensions of the original 2D array, but under
most circumstances where they don't match, the data produced will be junk.packed
- a short[][] encoded by calling this class' packMulti() method on a 2D array.width
- the width of the 2D array that will be returned; should match the unpacked array's width.height
- the height of the 2D array that will be returned; should match the unpacked array's height.levels
- a double[] that must have the same length as packed, and will be used to assign cells in the
returned double[][] based on what levels parameter was used to compress packedpublic static double[][] unpackMultiDoublePartial(short[][] packed, int width, int height, double[] levels, int limit)
CoordPacker
class documentation. The length of levels and the length of the outer array
of packed do not have to be equal. However, the levels array passed to this method should not be identical to the
levels array passed to packMulti(); for FOV compression, you should get an array for levels using
generatePackingLevels(), but for decompression, you should create levels using generateLightLevels(), which
should more appropriately fit the desired output. Reusing the levels array used to pack the FOV will usually
produce values at the edge of FOV that are less than 0.01 but greater than 0, and will have a maximum value
somewhat less than 1.0; neither are usually desirable, but using a different array made with
generateLightLevels() will produce doubles ranging from 1.0 / levels.length to 1.0 at the highest. Width and
height do not technically need to match the dimensions of the original 2D array, but under most circumstances
where they don't match, the data produced will be junk.packed
- a short[][] encoded by calling this class' packMulti() method on a 2D array.width
- the width of the 2D array that will be returned; should match the unpacked array's width.height
- the height of the 2D array that will be returned; should match the unpacked array's height.levels
- a double[] that must have the same length as packed, and will be used to assign cells in the
returned double[][] based on what levels parameter was used to compress packedlimit
- the number of elements to consider from levels and packed, starting from the innermost.public static double[][] unpackMultiDoublePartialConical(short[][] packed, int width, int height, double[] levels, int limit, int centerX, int centerY, double angle, double span)
CoordPacker
class documentation. The length of levels and the length of the outer array
of packed do not have to be equal. However, the levels array passed to this method should not be identical to the
levels array passed to packMulti(); for FOV compression, you should get an array for levels using
generatePackingLevels(), but for decompression, you should create levels using generateLightLevels(), which
should more appropriately fit the desired output. Reusing the levels array used to pack the FOV will usually
produce values at the edge of FOV that are less than 0.01 but greater than 0, and will have a maximum value
somewhat less than 1.0; neither are usually desirable, but using a different array made with
generateLightLevels() will produce doubles ranging from 1.0 / levels.length to 1.0 at the highest. This method
takes an angle and span as well as a centerX and centerY; the only values that will be greater than 0.0 in the
result will be within the round-based conical section that could be produced by traveling from (centerX,centerY)
along angle in a limitless line and expanding the cone to be span degrees broad (circularly), centered on angle.
Width and height do not technically need to match the dimensions of the original 2D array, but under most
circumstances where they don't match, the data produced will be junk.packed
- a short[][] encoded by calling this class' packMulti() method on a 2D array.width
- the width of the 2D array that will be returned; should match the unpacked array's width.height
- the height of the 2D array that will be returned; should match the unpacked array's height.levels
- a double[] that must have the same length as packed, and will be used to assign cells in the
returned double[][] based on what levels parameter was used to compress packedlimit
- the number of elements to consider from levels and packed, starting from the innermost.centerX
- the x position of the corner or origin of the conical FOVcenterY
- the y position of the corner or origin of the conical FOVangle
- the center of the conical area to limit this to, in degreesspan
- the total span of the conical area to limit this to, in degreespublic static byte[][] unpackMultiByte(short[][] packed, int width, int height)
CoordPacker
class documentation. Width and height do not technically need to match the dimensions of
the original 2D array, but under most circumstances where they don't match, the data produced will be junk.packed
- a short[][] encoded by calling this class' packMulti() method on a 2D array.width
- the width of the 2D array that will be returned; should match the unpacked array's width.height
- the height of the 2D array that will be returned; should match the unpacked array's height.public static char[][] unpackChar(short[] packed, char t, char f)
packed
- a packed short array, as produced by pack()t
- the char to use for "on" positions in packedf
- the char to use for "off" positions in packedpublic static char[][] unpackChar(short[] packed, int width, int height, char t, char f)
packed
- a packed short array, as produced by pack()width
- the desired 2D array widthheight
- the desired 2D array heightt
- the char to use for "on" positions in packedf
- the char to use for "off" positions in packedpublic static GreasedRegion unpackGreasedRegion(short[] packed, int width, int height)
packed
- a packed short array, as produced by pack()width
- the desired GreasedRegion's widthheight
- the desired GreasedRegion's heightpublic static GreasedRegion unpackIntoGreasedRegion(short[] packed, GreasedRegion target)
target
with any "on" cells in the packed short array
packed
. This method doesn't allocate unless an argument is null (then it throws a new Exception). It also
won't insert any "on" cells in packed that are outside the width and height of target.packed
- a packed short array, as produced by pack()target
- a GreasedRegion that will be modified in-place to include "on" cells from packedpublic static GreasedRegion unpackIntoGreasedRegion(short[] packed, GreasedRegion target, int offsetX, int offsetY)
target
with any "on" cells in the packed short array
packed
, inserting cells from packed at an offset when they go into target. This method doesn't allocate
unless an argument is null (then it throws a new Exception). It also won't insert any "on" cells in packed that
are outside the width and height of target.packed
- a packed short array, as produced by pack()target
- a GreasedRegion that will be modified in-place to include "on" cells from packedoffsetX
- how much to offset x positions in packed by when they are placed into targetoffsetY
- how much to offset y positions in packed by when they are placed into targetpublic static boolean queryPacked(short[] packed, int x, int y)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti(); must
not be null (this method does not check due to very tight performance constraints).x
- between 0 and 255, inclusivey
- between 0 and 255, inclusivepublic static boolean queryPackedHilbert(short[] packed, short hilbert)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti(); must
not be null (this method does not check due to very tight performance constraints).hilbert
- a Hilbert Curve index, such as one taken directly from a packed short[] without extra processingpublic static OrderedSet<short[]> findManyPacked(int x, int y, short[]... packed)
x
- between 0 and 255, inclusivey
- between 0 and 255, inclusivepacked
- an array or vararg of short[], such as those returned by pack() or one of the sub-arrays in what is
returned by packMulti(); null elements in packed will be skipped.public static OrderedSet<short[]> findManyPacked(int x, int y, java.util.Collection<short[]> packed)
x
- between 0 and 255, inclusivey
- between 0 and 255, inclusivepacked
- a Collection of short[] (as encoded by this class); null elements in packed will be skipped.public static boolean regionsContain(short[] checking, short[]... packed)
checking
- the packed data to check for overlap with the other regionspacked
- an array or vararg of short[], such as those returned by pack() or one of the sub-arrays in what is
returned by packMulti(); null elements in packed will be skippedpublic static boolean regionsContain(short[] checking, java.util.Collection<short[]> packed)
checking
- the packed data to check for overlap with the other regionspacked
- a Collection of short[], as encoded by this class; null elements in packed will be skippedpublic static java.util.ArrayList<short[]> findManyPackedHilbert(short hilbert, short[]... packed)
hilbert
- a Hilbert Curve index, such as one taken directly from a packed short[] without extra processingpacked
- an array or vararg of short[], such as those returned by pack() or one of the sub-arrays in what is
returned by packMulti(); null elements in packed will be skipped.public static Coord[] allPacked(short[] packed)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti(); must
not be null (this method does not check due to very tight performance constraints).public static short[] allPackedHilbert(short[] packed)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti(); must
not be null (this method does not check due to very tight performance constraints).public static Coord nth(short[] packed, int n)
CoordPacker.count()
.
You can technically use nth to iterate over only the Coords that are defined in some packed data, but it's
drastically more efficient to store a Coord array once with allPacked(). Using nth() as an iterator is
essentially running a growing portion of what allPacked() does, over and over again, until the last Coord encoded
in packed takes almost as long to process as one call to allPacked(). That said, for a single Coord this can be
significantly faster than getting an array with allPacked() and fetching only one item from it.packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti(); must
not be null (this method does not check due to very tight performance constraints).n
- the index to get in packedpublic static Coord[] fractionPacked(short[] packed, int fraction)
randomSeparated()
to keep distance between Coords and sample most areas of some packed data. Values for
fraction that are multiples of 4 are likely to show a pattern in large open spaces more easily.packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti(); must
not be null (this method does not check due to very tight performance constraints).fraction
- the denominator of the approximate fraction of "on" cells to usepublic static Coord[] randomSeparated(short[] packed, int separation, IRNG rng)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti(); must
not be null (this method does not check due to very tight performance constraints).separation
- the denominator of the approximate fraction of "on" cells to userng
- the IRNG
, such as an RNG
or GWTRNG
, to use to randomize retrievalpublic static short[] fractionPackedHilbert(short[] packed, int fraction)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti(); must
not be null (this method does not check due to very tight performance constraints).fraction
- the approximate fraction of "on" cells to usepublic static Coord[] apartPacked(short[] packed, int minDistance)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti(); must
not be null (this method does not check due to very tight performance constraints).minDistance
- the minimum distance (measured 8-way, Chebyshev) between any positions this returnspublic static Coord[] apartPacked(short[] packed, int minDistance, boolean eightWay)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti(); must
not be null (this method does not check due to very tight performance constraints).minDistance
- the minimum distance (measurement depends on eightWay) between any positions this returnseightWay
- true if distance should be measured equally in 8 directions, false to use 4 directionspublic static short[] apartPackedHilbert(short[] packed, int minDistance)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti(); must
not be null (this method does not check due to very tight performance constraints).minDistance
- the minimum distance (measured 8-way, Chebyshev) between any positions this returnspublic static short[] apartPackedHilbert(short[] packed, int minDistance, boolean eightWay)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti(); must
not be null (this method does not check due to very tight performance constraints).minDistance
- the minimum distance (measurement depends on eightWay) between any positions this returnseightWay
- true if distance should be measured equally in 8 directions, false to use 4 directionspublic static short[] translate(short[] packed, int xMove, int yMove, int width, int height)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti()xMove
- distance to move the x-coordinate; can be positive or negativeyMove
- distance to move the y-coordinate; can be positive or negativewidth
- the maximum width; if a cell would move to x at least equal to width, it stops at width - 1height
- the maximum height; if a cell would move to y at least equal to height, it stops at height - 1public static short[] expand(short[] packed, int expansion, int width, int height)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti()expansion
- the positive (square) radius, in cells, to expand each cell out bywidth
- the maximum width; if a cell would move to x at least equal to width, it stops at width - 1height
- the maximum height; if a cell would move to y at least equal to height, it stops at height - 1public static short[] expand(short[] packed, int expansion, int width, int height, boolean eightWay)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti()expansion
- the positive (square) radius, in cells, to expand each cell out bywidth
- the maximum width; if a cell would move to x at least equal to width, it stops at width - 1height
- the maximum height; if a cell would move to y at least equal to height, it stops at height - 1eightWay
- true if the expansion should be both diagonal and orthogonal; false for just orthogonalpublic static short[] retract(short[] packed, int retraction, int width, int height)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti()retraction
- the positive (square) radius, in cells, to pull each cell in bywidth
- the maximum width; if a cell would move to x at least equal to width, it stops at width - 1height
- the maximum height; if a cell would move to y at least equal to height, it stops at height - 1public static short[] retract(short[] packed, int retraction, int width, int height, boolean eightWay)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti()retraction
- the positive (square) radius, in cells, to pull each cell in bywidth
- the maximum width; cells outside this are considered "off" for this method's purposesheight
- the maximum height; cells outside this are considered "off" for this method's purposeseightWay
- true if the retraction should be both diagonal and orthogonal; false for just orthogonalpublic static short[] fringe(short[] packed, int expansion, int width, int height)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti()expansion
- the positive (square-shaped) radius, in cells, to expand each cell out bywidth
- the maximum width; if a cell would move to x at least equal to width, it stops at width - 1height
- the maximum height; if a cell would move to y at least equal to height, it stops at height - 1public static short[] fringe(short[] packed, int expansion, int width, int height, boolean eightWay)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti()expansion
- the positive (square-shaped) radius, in cells, to expand each cell out bywidth
- the maximum width; if a cell would move to x at least equal to width, it stops at width - 1height
- the maximum height; if a cell would move to y at least equal to height, it stops at height - 1eightWay
- true if the expansion should be both diagonal and orthogonal; false for just orthogonalpublic static short[] fringe(short[] packed, int expansion, int width, int height, boolean eightWay, boolean drop)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti()expansion
- the positive (square-shaped) radius, in cells, to expand each cell out bywidth
- the maximum width; if a cell would move to x at least equal to width, it stops at width - 1height
- the maximum height; if a cell would move to y at least equal to height, it stops at height - 1eightWay
- true if the expansion should be both diagonal and orthogonal; false for just orthogonaldrop
- true to drop cells that would expand into negative coordinates or past width/height, false to stop
their expansion earlypublic static short[][] fringes(short[] packed, int expansions, int width, int height)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti()expansions
- the positive (square-shaped) radius, in cells, to expand each cell out by, also the length
of the outer array returned by this methodwidth
- the maximum width; if a cell would move to x at least equal to width, it stops at width - 1height
- the maximum height; if a cell would move to y at least equal to height, it stops at height - 1public static short[][] fringes(short[] packed, int expansions, int width, int height, boolean eightWay)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti()expansions
- the positive (square-shaped) radius, in cells, to expand each cell out by, also the length
of the outer array returned by this methodwidth
- the maximum width; if a cell would move to x at least equal to width, it stops at width - 1height
- the maximum height; if a cell would move to y at least equal to height, it stops at height - 1eightWay
- true if the expansion should be both diagonal and orthogonal; false for just orthogonalpublic static short[] surface(short[] packed, int depth, int width, int height)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti()depth
- the positive (square) radius, in cells, to go inward from an "off" cell into the "in" cellswidth
- the maximum width; if a cell would move to x at least equal to width, it stops at width - 1height
- the maximum height; if a cell would move to y at least equal to height, it stops at height - 1public static short[] surface(short[] packed, int depth, int width, int height, boolean eightWay)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti()depth
- the positive (square) radius, in cells, to go inward from an "off" cell into the "in" cellswidth
- the maximum width; if a cell would move to x at least equal to width, it stops at width - 1height
- the maximum height; if a cell would move to y at least equal to height, it stops at height - 1eightWay
- true if the retraction should be both diagonal and orthogonal; false for just orthogonalpublic static short[][] surfaces(short[] packed, int depth, int width, int height)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti()depth
- the positive (square) radius, in cells, to go inward from an "off" cell into the "in" cellswidth
- the maximum width; if a cell would move to x at least equal to width, it stops at width - 1height
- the maximum height; if a cell would move to y at least equal to height, it stops at height - 1public static short[][] surfaces(short[] packed, int depth, int width, int height, boolean eightWay)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti()depth
- the positive (square) radius, in cells, to go inward from an "off" cell into the "in" cellswidth
- the maximum width; if a cell would move to x at least equal to width, it stops at width - 1height
- the maximum height; if a cell would move to y at least equal to height, it stops at height - 1eightWay
- true if the retraction should be both diagonal and orthogonal; false for just orthogonalpublic static short[] flood(short[] bounds, short[] start, int expansion)
bounds
- packed data representing the maximum extent of the region to flood-fill; often floorsstart
- a packed array that encodes position(s) that the flood will spread outward fromexpansion
- the positive (square) radius, in cells, to expand each cell out bypublic static short[] flood(short[] bounds, short[] start, int expansion, boolean eightWay)
bounds
- packed data representing the maximum extent of the region to flood-fill; often floorsstart
- a packed array that encodes position(s) that the flood will spread outward fromexpansion
- the positive (square) radius, in cells, to expand each cell out byeightWay
- true to flood-fill out in all eight directions at each step, false for just orthogonalpublic static short[] spill(short[] bounds, short[] start, int volume, IRNG rng)
bounds
- packed data representing the maximum extent of the region to random-flood-fill; often floorsstart
- a packed array that encodes position(s) that the random-flood will spread outward fromvolume
- the total number of cells to try to fillrng
- the IRNG
, such as an RNG
or GWTRNG
, used to generate random numbers for the floodingpublic static short[] radiate(short[] bounds, short[] start, int expansion)
bounds
- packed data representing the maximum extent of the region to flood-fill; often floorsstart
- a packed array that encodes position(s) that the flood will spread outward fromexpansion
- the positive (square) radius, in cells, to expand each cell out bypublic static short[] radiate(short[] bounds, short[] start, int expansion, Radius metric)
bounds
- packed data representing the maximum extent of the region to flood-fill; often floorsstart
- a packed array that encodes position(s) that the flood will spread outward fromexpansion
- the positive (square) radius, in cells, to expand each cell out bymetric
- a Radius that defines how this should expand, SQUARE for 8-way, DIAMOND for 4-way, CIRCLE for
Euclidean expansion (not guaranteed to be perfectly circular)public static short[] radiate(short[] bounds, short[] start, int expansion, boolean eightWay)
bounds
- packed data representing the maximum extent of the region to flood-fill; often floorsstart
- a packed array that encodes position(s) that the flood will spread outward fromexpansion
- the positive (square) radius, in cells, to expand each cell out byeightWay
- true to flood-fill out in all eight directions at each step, false for just orthogonalpublic static short[] reachable(short[] bounds, short[] start, Reach reach)
bounds
- packed data representing the max extent of the region to check for reach-ability; often floorsstart
- a packed array that encodes position(s) that the flood will spread outward fromreach
- a Reach
object that determines minimum and maximum range, distance metric, and AimLimitpublic static short[] rectangle(int width, int height)
width
- the width of the rectangleheight
- the height of the rectanglepublic static short[] rectangle(int x, int y, int width, int height)
x
- the minimum x coordinatey
- the minimum y coordinatewidth
- the width of the rectangleheight
- the height of the rectanglepublic static short[] rectangleHilbert(int x, int y, int width, int height)
x
- the minimum x coordinatey
- the minimum y coordinatewidth
- the width of the rectangleheight
- the height of the rectanglepublic static short[] circle(Coord center, int radius, int width, int height)
center
- center position of the circleradius
- radius to extend in all directions from centerwidth
- the maximum width of the map (exclusive); the circle will not extend past this or below 0height
- the maximum height of the map (exclusive); the circle will not extend past this or below 0public static int count(short[] packed)
packed
- a packed short array, as produced by pack()public static int count(short[] packed, boolean wanted)
packed
- a packed short array, as produced by pack()wanted
- the boolean you want to count, true for "on" and false for "off"public static int covered(short[] packed)
packed
- a packed short array, as produced by pack()public static Coord[] bounds(short[] packed)
packed
- a packed short array, as produced by pack()public static char[][] mask(char[][] map, short[] packed, char filler)
map
- a 2D char array that will not be modifiedpacked
- a packed short array, as produced by pack()filler
- the char to use for "off" positions in packedpublic static short[] unionPacked(short[] left, short[] right)
left
- A packed array such as one produced by pack()right
- A packed array such as one produced by pack()public static short[] intersectPacked(short[] left, short[] right)
left
- A packed array such as one produced by pack()right
- A packed array such as one produced by pack()public static boolean isEmpty(short[] packed)
packed
- a packed array such as one produced by pack()public static boolean intersects(short[] left, short[] right)
left
- A packed array such as one produced by pack()right
- A packed array such as one produced by pack()public static short[] negatePacked(short[] original)
original
- A packed array such as one produced by pack()public static short[] differencePacked(short[] left, short[] right)
left
- A packed array such as one produced by pack()right
- A packed array such as one produced by pack()public static short[] xorPacked(short[] left, short[] right)
left
- A packed array such as one produced by pack()right
- A packed array such as one produced by pack()public static short[] packOne(int hilbert)
hilbert
- a Hilbert distance that will be encoded as "on"public static short[] packOne(Coord point)
point
- a Coord that will be encoded as "on"public static short[] packOne(int x, int y)
x
- the x component of the point that will be encoded as "on"y
- the y component of the point that will be encoded as "on"public static short[] packSeveral(int... hilbert)
hilbert
- a vararg or array of Hilbert distances that will be encoded as "on"public static short[] packSeveral(Coord... points)
points
- a vararg or array of Coords that will be encoded as "on"public static short[] packSeveral(java.util.Collection<Coord> points)
points
- a Collection of Coords that will be encoded as "on"public static short[] insertPacked(short[] original, short hilbert)
original
- A packed array such as one produced by pack()hilbert
- A Hilbert Curve index that should be inserted into the resultpublic static short[] insertPacked(short[] original, int x, int y)
original
- A packed array such as one produced by pack()x
- The x position at which to insert the "on" celly
- The y position at which to insert the "on" cellpublic static short[] insertSeveralPacked(short[] original, int... hilbert)
original
- A packed array such as one produced by pack()hilbert
- an array or vararg of Hilbert Curve indices that should be inserted into the resultpublic static short[] insertSeveralPacked(short[] original, Coord... points)
original
- A packed array such as one produced by pack()points
- an array or vararg of Coords that should be inserted into the resultpublic static short[] insertSeveralPacked(short[] original, java.util.Collection<Coord> points)
original
- A packed array such as one produced by pack()points
- an array or vararg of Coords that should be inserted into the resultpublic static short[] removePacked(short[] original, short hilbert)
original
- A packed array such as one produced by pack()hilbert
- A Hilbert Curve index that should be removed from the resultpublic static short[] removePacked(short[] original, int x, int y)
original
- A packed array such as one produced by pack()x
- The x position at which to remove any "on" celly
- The y position at which to remove any "on" cellpublic static short[] removeSeveralPacked(short[] original, int... hilbert)
original
- A packed array such as one produced by pack()hilbert
- an array or vararg of Hilbert Curve indices that should be inserted into the resultpublic static short[] removeSeveralPacked(short[] original, Coord... points)
original
- A packed array such as one produced by pack()points
- an array or vararg of Coords that should be removed from the resultpublic static short[] removeSeveralPacked(short[] original, java.util.Collection<Coord> points)
original
- A packed array such as one produced by pack()points
- a Collection of Coords that should be removed from the resultpublic static java.util.ArrayList<short[]> split(short[] packed)
short[] floors = pack(map, '.'),
rooms = flood(floors, retract(floors, 1, WIDTH, HEIGHT, true), 2, false),
corridors = differencePacked(floors, rooms),
doors = intersectPacked(rooms, fringe(corridors, 1, WIDTH, HEIGHT, false));
List<short[]> apart = split(rooms);
, or substitute
split(corridors)
to get the corridors. The room-finding technique works by shrinking floors by a radius
of 1 (8-way), which causes thin areas like corridors of 2 or less width to be removed, then flood-filling the
floors out from the area that produces by 2 cells (4-way this time) to restore the original size of non-corridor
areas (plus some extra to ensure odd shapes are kept). Corridors are obtained by removing the rooms from floors.
The example code also gets the doors (which overlap with rooms, not corridors) by finding where the a room and a
corridor are adjacent. This technique is used with some enhancements in the RoomFinder class.packed
- a packed data array that probably encodes multiple unconnected "on" areasfor a class that uses this technique without exposing CoordPacker
public static short[] removeIsolated(short[] packed)
public static Coord[] randomSample(short[] packed, double fraction, IRNG rng)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti(); must
not be null (this method does not check).fraction
- the likelihood to return one of the "on" cells, from 0.0 to 1.0rng
- the random number generator used to decide random factors.public static Coord singleRandom(short[] packed, IRNG rng)
packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti(); must
not be null (this method does not check).rng
- the random number generator used to decide random factorspublic static java.util.ArrayList<Coord> randomPortion(short[] packed, int size, IRNG rng)
IRNG.shuffleInPlace(List)
with it as a parameter to randomize the order.packed
- a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti(); must
not be null (this method does not check).size
- the desired size of the List to return; may be smaller if there aren't enough elementsrng
- the random number generator used to decide random factors.public static int[][] sumMany(int width, int height, short[]... many)
width
- the width of the 2D array that will be returned; should match the unpacked array's widthheight
- the height of the 2D array that will be returned; should match the unpacked array's heightmany
- a vararg or array of short[] encoded by calling one of this class' packing methods on a 2D arraypublic static void printPacked(short[] packed, int width, int height)
packed
- a packed short[] such as one produced by pack()width
- the width of the packed 2D arrayheight
- the height of the packed 2D arraypublic static void printCompressedData(short[] packed)
public static java.lang.String encodeASCII(short[] packed)
packed
- a packed data item produced by pack() or some other method from this class.public static short[] decodeASCII(java.lang.String text)
text
- a String produced by CoordPacker.encodeASCII(); this will almost certainly fail on other strings.public static java.lang.String encodeBraille(short[] packed)
packed
- a packed data item produced by pack() or some other method from this class.public static short[] decodeBraille(java.lang.String text)
text
- a String produced by CoordPacker.encodeBraille(); this will almost certainly fail on other strings.public static int grayEncode(int n)
n
- any intpublic static int grayDecode(int n)
n
- a gray code, as produced by grayEncodepublic static int posToHilbert(int x, int y)
x
- between 0 and 255 inclusivey
- between 0 and 255 inclusivepublic static int posToHilbert3D(int x, int y, int z)
x
- between 0 and 7 inclusivey
- between 0 and 7 inclusivez
- between 0 and 7 inclusivepublic static int posToMoore(int x, int y)
x
- between 0 and 15 inclusivey
- between 0 and 15 inclusivepublic static int mortonToHilbert(int morton)
morton
- a Morton code that interleaves two 8-bit unsigned numbers, with x as index1 and y as index2.public static int hilbertToMorton(int hilbert)
hilbert
- a distance to travel down the Hilbert Curvepublic static Coord hilbertToCoord(int hilbert)
hilbert
- a distance to travel down the Hilbert Curvepublic static Coord mooreToCoord(int moore)
moore & 255
.moore
- a distance to travel down the Moore Curvepublic static int coordToHilbert(Coord pt)
pt
- a Coord with values between 0 and 255, inclusivepublic static int coordToMoore(Coord pt)
pt
- a Coord with values between 0 and 15, inclusivepublic static int mortonEncode3D(int index1, int index2, int index3)
public static Coord3D mortonDecode3D(int morton)
public static int mortonBitDecode3D(int morton)
public static int getXMoore3D(int index, int n)
index
- the index into the 3D 16x16x(8*n) Moore Curve, must be less than 0x1000n
- the number of 8-deep layers to use as part of the box shape this travels throughpublic static int getYMoore3D(int index, int n)
index
- the index into the 3D 16x16x(8*n) Moore Curve, must be less than 0x1000n
- the number of 8-deep layers to use as part of the box shape this travels throughpublic static int getZMoore3D(int index, int n)
index
- the index into the 3D 16x16x(8*n) Moore Curve, must be less than 0x1000n
- the number of 8-deep layers to use as part of the box shape this travels throughpublic static short zEncode(short index1, short index2)
index1
- a non-negative integer using at most 8 bits, to be placed in the "x" slotsindex2
- a non-negative integer using at most 8 bits, to be placed in the "y" slotspublic static int mortonEncode(int index1, int index2)
index1
- a non-negative integer using at most 8 bits, to be placed in the "x" slotsindex2
- a non-negative integer using at most 8 bits, to be placed in the "y" slotspublic static Coord mortonDecode(int morton)
morton
- an int containing two interleaved numbers, from 0 to 255 eachpublic static Coord zDecode(short morton)
morton
- a short containing two interleaved numbers, from 0 to 255 eachCopyright © Eben Howard 2012–2022. All rights reserved.