Record Class BoardLocation

java.lang.Object
java.lang.Record
megamek.common.board.BoardLocation
All Implemented Interfaces:
Serializable

public record BoardLocation(Coords coords, int boardId, boolean isNoLocation) extends Record implements Serializable
Represents a location (i.e. Coords) on the game board of a specific ID. With a game having multiple maps, this class needs to replace Coords in many methods to identify a specific position of an Entity or event. The coords cannot be null.

BoardLocation is immutable.

See Also:
  • Field Details

    • NO_LOCATION

      public static final BoardLocation NO_LOCATION
      This location represents a location that has null coords or a negative board ID, i.e. is obviously invalid. This location, when checked, will return false for comparisons and empty results for adjacent hexes and the like. Note that a BoardLocation that is not a NO_LOCATION does not necessarily exist either, its board ID may still be invalid or its coords outside any board.

      The coords or board ID of NO_LOCATION should not be used directly but if used the coords are at Integer .MIN_VALUE and the board ID is Board.BOARD_NONE.

  • Constructor Details

    • BoardLocation

      public BoardLocation(Coords coords, int boardId, boolean isNoLocation)
      Creates an instance of a BoardLocation record class.
      Parameters:
      coords - the value for the coords record component
      boardId - the value for the boardId record component
      isNoLocation - the value for the isNoLocation record component
  • Method Details

    • of

      public static BoardLocation of(Coords coords, int boardId)
      Returns a BoardLocation with the given data. When coords are null or the boardId negative, NO_LOCATION is returned. This means that the created BoardLocation never has null coords. Still, the returned BoardLocation may not represent a valid location, as the board ID and coords are not checked against existing boards.
      Parameters:
      coords - The coords
      boardId - The board ID
      Returns:
      A BoardLocation representing the given position or NO_LOCATION
    • isOn

      public boolean isOn(int boardId)
      Parameters:
      boardId - The board ID to test
      Returns:
      True when this location's board ID is equal to the given board ID, i.e. when this location is on the given board. If this location is a non-location, this will always return false.
      See Also:
    • isAt

      @Deprecated(since="0.51.0", forRemoval=true) public boolean isAt(@Nullable Coords coords)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Parameters:
      coords - The coords to test
      Returns:
      True when this location's coords are equal to the given coords.
    • allAdjacent

      public List<BoardLocation> allAdjacent()
      Returns a list of all six adjacent coordinates (distance = 1). Does not check if those are on the board or if the board of the present boardId exists. This is equivalent to Coords.allAdjacent() with the boardId of the present BoardLocation added in. This is also equivalent to calling allAtDistance(1).
      Returns:
      A list of adjacent BoardLocations
    • allAtDistance

      public List<BoardLocation> allAtDistance(int dist)
      Returns a list of all coordinates at the given distance dist. Does not check if those are on the board or if the board of the present boardId exists. Returns an empty list for dist of less than 0 and the calling BoardLocation itself for dist == 0. This is equivalent to Coords.allAtDistance(int) with the boardId of the present BoardLocation added in.
      Returns:
      A list of BoardLocations centered on this BoardLocation and at the given distance
    • allAtDistanceOrLess

      public List<BoardLocation> allAtDistanceOrLess(int dist)
      Returns a list of all coordinates at the given distance dist and anything less than dist as well.
    • allAtDistances

      public List<BoardLocation> allAtDistances(int minimumDistance, int maximumDistance)
      Returns a list of all coordinates at the given distance dist and anything less than dist as well.
    • translated

      public BoardLocation translated(int dir)
      Returns the coordinate 1 unit in the specified direction dir.
    • toString

      @Nonnull public String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • getBoardNum

      public String getBoardNum()
    • toFriendlyString

      public String toFriendlyString()
    • isSameBoardAs

      public boolean isSameBoardAs(@Nullable BoardLocation other)
    • equals

      public boolean equals(Object o)
      Two BoardLocations are equal when their board ID and coords are equal. Two NO_LOCATION's are equal.
      Specified by:
      equals in class Record
      Parameters:
      o - The object to compare
      Returns:
      True when the two are equal
    • hashCode

      public int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • getX

      public int getX()
    • getY

      public int getY()
    • coords

      public Coords coords()
      Returns the value of the coords record component.
      Returns:
      the value of the coords record component
    • boardId

      public int boardId()
      Returns the value of the boardId record component.
      Returns:
      the value of the boardId record component
    • isNoLocation

      public boolean isNoLocation()
      Returns the value of the isNoLocation record component.
      Returns:
      the value of the isNoLocation record component