Class SectionMap

java.lang.Object
squidpony.squidgrid.mapping.SectionMap
All Implemented Interfaces:
Serializable

public class SectionMap
extends Object
implements Serializable
A Map-like collection that allows storing subdivisions of a 2D array with names (always Strings) and identifying numbers, then looking up Coords to find the associated name and/or number, or or looking up a subdivision with a name or number to get a GreasedRegion back. This also stores connections between sections, which can be useful as part of a graph-like algorithm. It is fed the information it needs by a RoomFinder instance passed to the constructor or to reinitialize(RoomFinder). A RoomFinder is ready for usage after generating any dungeon with SectionDungeonGenerator or one of its subclasses; the field SectionDungeonGenerator.finder should usually be all that needs to be given to this class. If you don't use SectionDungeonGenerator, there's no reason you can't construct a RoomFinder independently and pass that (they can take a little time to construct on large or very complex maps, but shouldn't be heavy after construction).
If your code uses the Zone interface, then the GreasedRegion objects this can return do implement Zone, MutableZone, and Iterable of Coord. GreasedRegion is significantly faster than the alternatives (CoordPacker and manual Lists of Coord) for the spatial manipulations that RoomFinder needs to do to find room-like shapes, and this just gets its GreasedRegion values from RoomFinder directly.
Not to be confused with RegionMap, which has different functionality and a different pupose; RegionMap simply is a slight extension on OrderedMap to conveniently handle regions as short arrays produced by CoordPacker, while this class offers additional features for labeling and looking up sections of a map that were found by a RoomFinder.
Created by Tommy Ettinger on 11/28/2016.
See Also:
Serialized Form
  • Field Summary

    Fields 
    Modifier and Type Field Description
    protected ArrayList<IntVLA> connections  
    protected int[][] map  
    protected Arrangement<String> names  
    protected ArrayList<GreasedRegion> regions  
  • Constructor Summary

    Constructors 
    Constructor Description
    SectionMap()
    This shouldn't usually be used unless you for some reason need to construct a SectionMap before you have access to a dungeon for it to map.
    SectionMap​(RoomFinder rf)
    The preferred constructor; takes a RoomFinder (often one already created in dungeon generation and available via SectionDungeonGenerator.finder) and uses it to give unique String names and identifying numbers to each room, corridor, and cave area that had been identified by that RoomFinder.
    SectionMap​(SectionMap other)
    Copy constructor; takes an already-initialized SectionMap and deep-copies each element into this one.
  • Method Summary

    Modifier and Type Method Description
    boolean contains​(int number)
    Checks if this contains the given identifying number.
    boolean contains​(int x, int y)
    Checks if this contains the given position (that is, x and y are within map bounds).
    boolean contains​(String name)
    Checks if this contains the given name.
    boolean contains​(Coord position)
    Checks if this contains the given position (that is, it is within map bounds).
    IntVLA nameToConnections​(String name)
    Gets the list of connected sections (by their identifying numbers) given a name of a section.
    int nameToNumber​(String name)
    Gets the identifying number corresponding to the given name.
    GreasedRegion nameToRegion​(String name)
    Gets the GreasedRegion that has the given name.
    IntVLA numberToConnections​(int number)
    Gets the list of connected sections (by their identifying numbers) given an identifying number of a section.
    String numberToName​(int number)
    Gets the name that corresponds to the given identifying number.
    GreasedRegion numberToRegion​(int number)
    Gets the GreasedRegion that has the given identifying number.
    IntVLA positionToConnections​(int x, int y)
    Gets the list of connected sections (by their identifying numbers) given a position inside that section.
    IntVLA positionToConnections​(Coord position)
    Gets the list of connected sections (by their identifying numbers) given a position inside that section.
    GreasedRegion positionToContaining​(int x, int y)
    Gets the GreasedRegion (a group of points as made by the constructor) that contains the given x, y point.
    GreasedRegion positionToContaining​(Coord position)
    Gets the GreasedRegion (a group of points as made by the constructor) that contains the given x, y point.
    String positionToName​(int x, int y)
    Gets the name of the area that contains the given x, y position.
    String positionToName​(Coord position)
    Gets the name of the area that contains the given position.
    int positionToNumber​(int x, int y)
    Gets the identifying number of the area that contains the given x, y position.
    int positionToNumber​(Coord position)
    Gets the identifying number of the area that contains the given position.
    SectionMap reinitialize​(RoomFinder rf)
    If this SectionMap hasn't been initialized or the map has completely changed (such as if the player went to a different floor of a dungeon), then you can call this method to avoid discarding some of the state from an earlier SectionMap.
    int size()
    The number of regions this knows about; includes an entry for "unused cells" so this may be one larger than the amount of GreasedRegions present in a RoomFinder used to construct this.

    Methods inherited from class java.lang.Object

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

  • Constructor Details

    • SectionMap

      public SectionMap()
      This shouldn't usually be used unless you for some reason need to construct a SectionMap before you have access to a dungeon for it to map. If you do need this, then you must call reinitialize(RoomFinder) to get any use out of this object.
      See Also:
      The preferred constructor, which takes a RoomFinder.
    • SectionMap

      public SectionMap​(RoomFinder rf)
      The preferred constructor; takes a RoomFinder (often one already created in dungeon generation and available via SectionDungeonGenerator.finder) and uses it to give unique String names and identifying numbers to each room, corridor, and cave area that had been identified by that RoomFinder. In the rare but possible chance that a room, corridor, or cave overlaps with another such area, the one given the highest identifying number takes precedence, but this should probably only happen if RoomFinder was subclassed or its internal state was modified. Any cells that aren't a room, corridor, or cave (usually this contains all walls) are given identifying number 0, with the corresponding name "unused0." All other cells will then have positive, non-zero identifying numbers. Rooms are named next, starting at "room1" and going up to "room2" and so on until all rooms are named; the 1 in the name corresponds to the identifying number. After the last room has been found, e.g. "room5", then corridors are named, starting after the last room's number, so in the example that would be "corridor6", followed by "corridor7". The numbers in the names still correspond to identifying numbers. After corridors, caves follow the same pattern; in this example "cave8" would be followed by "cave9".
      Parameters:
      rf - a RoomFinder object; usually obtained via SectionDungeonGenerator.finder
    • SectionMap

      public SectionMap​(SectionMap other)
      Copy constructor; takes an already-initialized SectionMap and deep-copies each element into this one. Allows null for other, which will make an empty SectionMap. This shouldn't be needed very often because SectionMap values are immutable, though their contents can in some cases be mutated independently, and this would allow one SectionMap to be copied and then have its items changed without changing the original.
      Parameters:
      other - a SectionMap to deep-copy into this one
  • Method Details

    • reinitialize

      If this SectionMap hasn't been initialized or the map has completely changed (such as if the player went to a different floor of a dungeon), then you can call this method to avoid discarding some of the state from an earlier SectionMap. This does all the same steps SectionMap(RoomFinder) does, so refer to that constructor's documentation for the names and numbers this assigns.
      Parameters:
      rf - a RoomFinder object; usually obtained via SectionDungeonGenerator.finder
      Returns:
      this for chaining.
    • positionToNumber

      public int positionToNumber​(int x, int y)
      Gets the identifying number of the area that contains the given x, y position.
      Parameters:
      x - the x-coordinate to find the identifying number for; should be within bounds of the map
      y - the y-coordinate to find the identifying number for; should be within bounds of the map
      Returns:
      the corresponding identifying number, or -1 if the parameters are invalid
    • positionToNumber

      public int positionToNumber​(Coord position)
      Gets the identifying number of the area that contains the given position.
      Parameters:
      position - the Coord to find the identifying number for; should be within bounds of the map and non-null
      Returns:
      the corresponding identifying number, or -1 if position is invalid or null
    • positionToName

      public String positionToName​(int x, int y)
      Gets the name of the area that contains the given x, y position.
      Parameters:
      x - the x-coordinate to find the name for; should be within bounds of the map
      y - the y-coordinate to find the name for; should be within bounds of the map
      Returns:
      the corresponding name as a String, or null if the parameters are invalid
    • positionToName

      public String positionToName​(Coord position)
      Gets the name of the area that contains the given position.
      Parameters:
      position - a Coord that should be within bounds of the map and non-null
      Returns:
      the corresponding name as a String, or null if position is invalid or null
    • nameToNumber

      public int nameToNumber​(String name)
      Gets the identifying number corresponding to the given name.
      Parameters:
      name - the name to look up, like "room1"
      Returns:
      the corresponding identifying number, or -1 if no such name exists
    • numberToName

      public String numberToName​(int number)
      Gets the name that corresponds to the given identifying number.
      Parameters:
      number - the number to look up, like 1
      Returns:
      the corresponding name as a String, or null if no such number is used
    • numberToRegion

      public GreasedRegion numberToRegion​(int number)
      Gets the GreasedRegion that has the given identifying number.
      Parameters:
      number - the number to look up, like 1
      Returns:
      the corresponding GreasedRegion, or null if no such number is used
    • nameToRegion

      public GreasedRegion nameToRegion​(String name)
      Gets the GreasedRegion that has the given name.
      Parameters:
      name - the name to look up, like "room1"
      Returns:
      the corresponding GreasedRegion, or null if no such name exists
    • positionToContaining

      public GreasedRegion positionToContaining​(int x, int y)
      Gets the GreasedRegion (a group of points as made by the constructor) that contains the given x, y point.
      Parameters:
      x - the x-coordinate to find the containing region for; should be within bounds of the map
      y - the y-coordinate to find the containing region for; should be within bounds of the map
      Returns:
      the GreasedRegion containing the given point, or null if the parameters are invalid
    • positionToContaining

      Gets the GreasedRegion (a group of points as made by the constructor) that contains the given x, y point.
      Parameters:
      position - the Coord to find the containing region for; should be within bounds of the map and non-null
      Returns:
      the GreasedRegion containing the given Coord, or null if position is invalid or null
    • numberToConnections

      public IntVLA numberToConnections​(int number)
      Gets the list of connected sections (by their identifying numbers) given an identifying number of a section.
      Parameters:
      number - an identifying number; should be non-negative and less than size()
      Returns:
      an IntVLA storing the identifying numbers of connected sections, or null if given an invalid parameter
    • nameToConnections

      public IntVLA nameToConnections​(String name)
      Gets the list of connected sections (by their identifying numbers) given a name of a section.
      Parameters:
      name - a String name; should be present in this SectionMap or this will return null
      Returns:
      an IntVLA storing the identifying numbers of connected sections, or null if given an invalid parameter
    • positionToConnections

      public IntVLA positionToConnections​(int x, int y)
      Gets the list of connected sections (by their identifying numbers) given a position inside that section.
      Parameters:
      x - the x-coordinate of the position to look up; should be within bounds
      y - the y-coordinate of the position to look up; should be within bounds
      Returns:
      an IntVLA storing the identifying numbers of connected sections, or null if given invalid parameters
    • positionToConnections

      public IntVLA positionToConnections​(Coord position)
      Gets the list of connected sections (by their identifying numbers) given a position inside that section.
      Parameters:
      position - the Coord position to look up; should be within bounds and non-null
      Returns:
      an IntVLA storing the identifying numbers of connected sections, or null if given an invalid parameter
    • size

      public int size()
      The number of regions this knows about; includes an entry for "unused cells" so this may be one larger than the amount of GreasedRegions present in a RoomFinder used to construct this.
      Returns:
      the size of this SectionMap
    • contains

      public boolean contains​(String name)
      Checks if this contains the given name.
      Parameters:
      name - the name to check
      Returns:
      true if this contains the name, false otherwise
    • contains

      public boolean contains​(int number)
      Checks if this contains the given identifying number.
      Parameters:
      number - the number to check
      Returns:
      true if this contains the identifying number, false otherwise
    • contains

      public boolean contains​(int x, int y)
      Checks if this contains the given position (that is, x and y are within map bounds).
      Parameters:
      x - the x-coordinate of the position to check
      y - the y-coordinate of the position to check
      Returns:
      true if the given position is in bounds, false otherwise
    • contains

      public boolean contains​(Coord position)
      Checks if this contains the given position (that is, it is within map bounds).
      Parameters:
      position - the position to check
      Returns:
      true if position is non-null and is in bounds, false otherwise