Package squidpony.squidai
Class GreasedZOI
java.lang.Object
squidpony.squidai.GreasedZOI
- All Implemented Interfaces:
Serializable
public class GreasedZOI extends Object implements Serializable
Calculates the Zone of Influence, also known as Zone of Control, for different points on a map.
Uses GreasedRegion for faster storage and manipulation of zones; it's suggested if you use this class to be
somewhat familiar with the methods for manipulating data in that class, though a GreasedRegion can also be used just
like a Collection of Coord values. This class is very similar in API and implementation to
Created by Tommy Ettinger on 1/14/2018.
ZOI
, but should be
faster on large maps and produces much less garbage. The main reason to choose between ZOI and
GreasedZOI is whether your existing code uses GreasedRegions, like this class, or uses CoordPacker, like ZOI. If you
don't currently use either, GreasedZOI is probably preferable because the calculate()
method produces a
value that can be reasonably consumed by Collection-based APIs, while ZOI.calculate()
produces a
harder-to-use short[]
that must be read by CoordPacker. Repeated operations on CoordPacker's short[]
data will have to keep allocating more arrays and temporary internal data structures, while a GreasedRegion can be
reused many times and only rarely allocates more space.
Created by Tommy Ettinger on 1/14/2018.
- See Also:
- Serialized Form
-
Constructor Summary
Constructors Constructor Description GreasedZOI(Collection<Coord> influences, char[][] map, Radius radiusStrategy)
Constructs a Zone of Influence map.GreasedZOI(Coord[][] influences, char[][] map, Radius radiusStrategy)
Constructs a Zone of Influence map.GreasedZOI(Coord[] influences, char[][] map, Radius radiusStrategy)
Constructs a Zone of Influence map. -
Method Summary
Modifier and Type Method Description GreasedRegion[]
calculate()
Finds the zones of influence for each of the influences (inner arrays of Coord) this was constructed with, and returns all zones as a GreasedRegion array.Coord[][]
getInfluences()
Gets the influencing groups; ideally the result should not be changed without setting it back with setInfluences.protected GreasedRegion
increasing(double[][] dm, Coord[] inf)
IntVLA
nearestInfluences(Coord point)
This can be given a Coord to check in the results of the latest calculate() call.IntVLA
nearestInfluences(GreasedRegion[] zones, Coord point)
Given the zones resulting from this class'calculate()
method and a Coord to check, finds the indices of all influencing groups in zones that have the Coord in their area, and returns all such indices as an IntVLA.void
setInfluences(Coord[][] influences)
Changes the influencing groups.
-
Constructor Details
-
GreasedZOI
Constructs a Zone of Influence map. Takes a (quite possibly jagged) array of arrays of Coord influences, where the elements of the outer array represent different groups of influencing "factions" or groups that exert control over nearby areas, and the Coord elements of the inner array represent individual spots that are part of those groups and share influence with all Coord in the same inner array. Also takes a char[][] for a map, which can be the simplified map with only '#' for walls and '.' for floors, or the final map (with chars like '~' for deep water as well as walls and floors), and a Radius enum that will be used to determine how distance is calculated.
Callcalculate()
when you want information out of this.- Parameters:
influences
- an outer array containing influencing groups, each an array containing Coords that influencemap
- a char[][] that is used as an area map; should be boundedradiusStrategy
- a Radius enum that corresponds to how distance should be measured
-
GreasedZOI
Constructs a Zone of Influence map. Takes an arrays of Coord influences, where each Coord is treated as both a one-element group of influencing "factions" or groups that exert control over nearby areas, and the individual spot that makes up one of those groups and spreads influence. Also takes a char[][] for a map, which can be the simplified map with only '#' for walls and '.' for floors, or the final map (with chars like '~' for deep water as well as walls and floors), and a Radius enum that will be used to determine how distance is calculated.
Essentially, this is the same as constructing a ZOI with a Coord[][] where each inner array has only one element.
Callcalculate()
when you want information out of this.- Parameters:
influences
- an array containing Coords that each have their own independent influencemap
- a char[][] that is used as an area map; should be boundedradiusStrategy
- a Radius enum that corresponds to how distance should be measured- See Also:
PoissonDisk provides a good way to generate evenly spaced Coords
-
GreasedZOI
Constructs a Zone of Influence map. Takes a Collection of Coord influences, where each Coord is treated as both a one-element group of influencing "factions" or groups that exert control over nearby areas, and the individual spot that makes up one of those groups and spreads influence. Also takes a char[][] for a map, which can be the simplified map with only '#' for walls and '.' for floors, or the final map (with chars like '~' for deep water as well as walls and floors), and a Radius enum that will be used to determine how distance is calculated.
Essentially, this is the same as constructing a ZOI with a Coord[][] where each inner array has only one element, drawn from a Collection. It often makes sense to use a GreasedRegion as the Collection of Coord.
Callcalculate()
when you want information out of this.- Parameters:
influences
- A Collection of Coord, such as a GreasedRegion, where each Coord has independent influencemap
- a char[][] that is used as an area map; should be boundedradiusStrategy
- a Radius enum that corresponds to how distance should be measured
-
-
Method Details
-
calculate
Finds the zones of influence for each of the influences (inner arrays of Coord) this was constructed with, and returns all zones as a GreasedRegion array. This has each zone of influence overlap with its neighbors; this is useful to find borders usingGreasedRegion.and(GreasedRegion)
, and borders are typically between 1 and 2 cells wide. You can get a different region if you want region A without the overlapping areas it shares with region B by usingGreasedRegion.andNot(GreasedRegion)
. Merging two zones A and B can be done withGreasedRegion.or(GreasedRegion)
. You can transform the data into a boolean[][] easily withGreasedRegion.decode()
, where true is contained in the zone and false is not. The methodsGreasedRegion.fringe()
,GreasedRegion.expand()
,GreasedRegion.singleRandom(IRNG)
, andGreasedRegion.separatedBlue(double)
are also potentially useful for this sort of data. You should save theGreasedRegion[]
for later use if you want to callnearestInfluences(GreasedRegion[], Coord)
.
The first GreasedRegion in the returned GreasedRegion[] will correspond to the area influenced by the first Coord[] in the nested array passed to the constructor (or the first Coord if a non-nested array was passed); the second will correspond to the second, and so on. The length of the GreasedRegion[] this returns will equal the number of influence groups.- Returns:
- a GreasedRegion array, with each item storing a zone's area
-
increasing
-
nearestInfluences
Given the zones resulting from this class'calculate()
method and a Coord to check, finds the indices of all influencing groups in zones that have the Coord in their area, and returns all such indices as an IntVLA.- Parameters:
zones
- a GreasedRegion[] returned by calculatepoint
- the Coord to test- Returns:
- an IntVLA where each element is the index of an influencing group in zones
-
nearestInfluences
This can be given a Coord to check in the results of the latest calculate() call. Finds the indices of all influencing groups in zones that have the Coord in their area, and returns all such indices as anIntVLA
. You can convert the IntVLA to an array withIntVLA.toArray()
if you want to match ZOI's behavior.- Parameters:
point
- the Coord to test- Returns:
- an IntVLA where each element is the index of an influencing group in zones
-
getInfluences
Gets the influencing groups; ideally the result should not be changed without setting it back with setInfluences.- Returns:
- influences a jagged array of Coord arrays, where the inner arrays are groups of influences
-
setInfluences
Changes the influencing groups. This also invalidates the last calculation for the purposes of nearestInfluences, at least for the overload that takes only a Coord.- Parameters:
influences
- a jagged array of Coord arrays, where the inner arrays are groups of influences
-