Class ZOI

java.lang.Object
squidpony.squidai.ZOI
All Implemented Interfaces:
Serializable

public class ZOI
extends Object
implements Serializable
Calculates the Zone of Influence, also known as Zone of Control, for different points on a map. Uses CoordPacker for more memory-efficient storage and manipulation of zones; it's recommended if you use this class to be somewhat familiar with the methods for manipulating packed data in that class. This class is very similar in API and implementation to ZOI, but should be slightly faster on large maps at the expense of usually using more memory. The main reason to choose between ZOI and GreasedZOI is whether your existing code uses GreasedRegions, like GreasedZOI, or uses CoordPacker, like this class. If you don't currently use either, GreasedZOI is probably preferable because the GreasedZOI.calculate() method produces a value that can be reasonably consumed by Collection-based APIs, while calculate() produces a harder-to-use short[] that must be read by CoordPacker; GreasedZOI is probably not significantly faster for most applications, and the memory usage difference is probably under a megabyte.
Created by Tommy Ettinger on 10/27/2015.
See Also:
Serialized Form
  • Constructor Summary

    Constructors 
    Constructor Description
    ZOI​(Coord[][] influences, char[][] map, Radius radiusStrategy)
    Constructs a Zone of Influence map.
    ZOI​(Coord[] influences, char[][] map, Radius radiusStrategy)
    Constructs a Zone of Influence map.
  • Method Summary

    Modifier and Type Method Description
    short[][] calculate()
    Finds the zones of influence for each of the influences (inner arrays of Coord) this was constructed with, and returns them as packed data (using CoordPacker, which can also be used to unpack the data, merge zones, get shared borders, and all sorts of other tricks).
    Coord[][] getInfluences()
    Gets the influencing groups; ideally the result should not be changed without setting it back with setInfluences.
    protected boolean[][] increasing​(double[][] dm, Coord[] inf)  
    int[] nearestInfluences​(short[][] 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 int array.
    int[] nearestInfluences​(Coord point)
    This can be given a Coord to check in the results of the latest calculate() call.
    void setInfluences​(Coord[][] influences)
    Changes the influencing groups.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ZOI

      public ZOI​(Coord[][] influences, char[][] map, Radius radiusStrategy)
      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.
      Call calculate() when you want information out of this.
      Parameters:
      influences - an outer array containing influencing groups, each an array containing Coords that influence
      map - a char[][] that is used as a map; should be bounded
      radiusStrategy - a Radius enum that corresponds to how distance should be measured
    • ZOI

      public ZOI​(Coord[] influences, char[][] map, Radius radiusStrategy)
      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.
      Call calculate() when you want information out of this.
      Parameters:
      influences - an array containing Coords that each have their own independent influence
      map - a char[][] that is used as a map; should be bounded
      radiusStrategy - a Radius enum that corresponds to how distance should be measured
      See Also:
      for a good way to generate evenly spaced Coords that can be used here
  • Method Details

    • calculate

      public short[][] calculate()
      Finds the zones of influence for each of the influences (inner arrays of Coord) this was constructed with, and returns them as packed data (using CoordPacker, which can also be used to unpack the data, merge zones, get shared borders, and all sorts of other tricks). This has each zone of influence overlap with its neighbors; this is useful to find borders using CoordPacker.intersectPacked(), and borders are typically between 1 and 2 cells wide. You can get a different region as packed data if you want region A without the overlapping areas it shares with region B by using short[] different = CoordPacker.differencePacked(A, B). Merging two zones A and B can be done with short[] merged = CoordPacker.unionPacked(A, B) . You can unpack the data into a boolean[][] easily with CoordPacker.unpack(), where true is contained in the zone and false is not. The CoordPacker methods fringe(), expand(), singleRandom(), randomSample(), and randomPortion() are also potentially useful for this sort of data. You should save the short[][] for later use if you want to call nearestInfluences() in this class.
      The first short[] in the returned short[][] 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 short[][] this returns will equal the number of influence groups.
      Returns:
      an array of short[] storing the zones' areas; each can be used as packed data with CoordPacker
    • increasing

      protected boolean[][] increasing​(double[][] dm, Coord[] inf)
    • nearestInfluences

      public int[] nearestInfluences​(short[][] 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 int array.
      Parameters:
      zones - a short[][] returned by calculate; not a multi-packed short[][] from CoordPacker !
      point - the Coord to test
      Returns:
      an int[] where each element is the index of an influencing group in zones
    • nearestInfluences

      public int[] nearestInfluences​(Coord point)
      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 an int array.
      Parameters:
      point - the Coord to test
      Returns:
      an int[] where each element is the index of an influencing group in zones
    • getInfluences

      public Coord[][] 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

      public void setInfluences​(Coord[][] influences)
      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