Class Board

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

public class Board extends Object implements Serializable
See Also:
  • Field Details

  • Constructor Details

    • Board

      public Board()
      Creates a new board with zero as its width and height parameters.
    • Board

      public Board(int width, int height)
      Creates a new board of the specified dimensions. All hexes in the board will be null until otherwise set.
      Parameters:
      width - the width dimension.
      height - the height dimension.
    • Board

      public Board(int width, int height, Hex... data)
      Creates a new board of the specified dimensions and specified hex data. Note that the number of Hexes given should be equal to width * height to avoid null Hexes in the board.
      Parameters:
      width - the width dimension
      height - the height dimension
      data - the Hexes of the new board
  • Method Details

    • getSkyBoard

      public static Board getSkyBoard(int width, int height)
      Returns a new atmospheric (low altitude) board with no terrain (sky map) of the given size.
      Parameters:
      width - the width of the board
      height - the height of the board
      Returns:
      the new board, ready to be used
    • getSpaceBoard

      public static Board getSpaceBoard(int width, int height)
      Returns a new space board of the given size.
      Parameters:
      width - the width of the board
      height - the height of the board
      Returns:
      the new board, ready to be used
    • getHeight

      public int getHeight()
      Returns:
      Map height in hexes
    • getWidth

      public int getWidth()
      Returns:
      Map width in hexes
    • getCenter

      public Coords getCenter()
    • newData

      public void newData(int width, int height, Hex[] data, @Nullable List<String> errors)
      Creates a new data set for the board, with the specified dimensions and data; notifies listeners that a new data set has been created.
      Parameters:
      width - the width dimension.
      height - the height dimension.
      data - new hex data appropriate for the board.
      errors - A buffer for storing error messages, if any. This is allowed to be null.
    • getHex

      @Nullable public Hex getHex(int x, int y)
      Determines if this Board contains the (x, y) Coords, and if so, returns the Hex at that position.
      Parameters:
      x - the x Coords.
      y - the y Coords.
      Returns:
      the Hex, if this Board contains the (x, y) location; null otherwise.
    • getHexInDir

      public Hex getHexInDir(Coords c, int dir)
      Parameters:
      c - starting coordinates
      dir - direction
      Returns:
      the hex in the specified direction from the specified starting coordinates.
    • getHexInDir

      public Hex getHexInDir(int x, int y, int dir)
      Gets the hex in the specified direction from the specified starting coordinates. This avoids calls to Coords.translated, and thus, object construction.
      Parameters:
      x - starting x coordinate
      y - starting y coordinate
      dir - direction
      Returns:
      the hex in the specified direction from the specified starting coordinates.
    • initializeAll

      protected void initializeAll(@Nullable List<String> errors)
      Initialize all hexes
    • initializeAround

      public void initializeAround(int x, int y)
      Initialize a hex and the hexes around it
    • initializeHex

      public void initializeHex(int x, int y)
      Initializes a hex in its surroundings. Currently, sets the connects parameter appropriately to the surrounding hexes. If a surrounding hex is off the board, it checks the hex opposite the missing hex.
    • initializeAllAutomaticTerrain

      public void initializeAllAutomaticTerrain()
      Rebuilds automatic terrains for the whole board, such as incline highlighting. Also fires a BOARD_CHANGED_ALL_HEXES event.
    • contains

      public boolean contains(int x, int y)
      Determines whether this Board "contains" the specified Coords.
      Parameters:
      x - the x Coords.
      y - the y Coords.
      Returns:
      true if the board contains the specified coords
    • contains

      public boolean contains(@Nullable Coords coords)
      Determines whether this Board "contains" the specified Coords.
      Parameters:
      coords - the Coords.
      Returns:
      true if the board contains the specified coords
    • contains

      public boolean contains(@Nullable BoardLocation location)
      Determines whether this Board "contains" the specified location. True only when the board IDs match and the coords of the location are within the borders of the board.
      Parameters:
      location - the location to test
      Returns:
      true if the board contains the specified location
    • getHex

      @Nullable public Hex getHex(@Nullable Coords coords)
      Returns the Hex at the given Coords, both of which may be null.
      Parameters:
      coords - the Coords to look for the Hex
      Returns:
      the Hex at the specified Coords, or null if there is not a hex there
    • getHexes

      public List<Hex> getHexes(@Nullable Collection<Coords> coords)
      Returns a list of Hexes at the given coords. The list will never be null but may be empty depending on the given Coords collection. If the given Coords collection is null, the returned list will be empty.
      Parameters:
      coords - the Coords to query
      Returns:
      the Hexes at the specified Coords
    • setHex

      public void setHex(int x, int y, Hex hex)
      Determines if this Board contains the (x, y) Coords, and if so, sets the specified Hex into that position and initializes it.
      Parameters:
      x - the x Coords.
      y - the y Coords.
      hex - the hex to be set into position.
    • setHexes

      public void setHexes(Map<BoardLocation,Hex> changedHexes)
      Copies in the given hexes, overwriting any affected hexes that are on this board. For simplicity, this method ignores BoardLocation's that are not on this board, i.e., it can be called without first filtering the locations for board ID.
      Parameters:
      changedHexes - A map of locations and hexes; the locations need not all (or any) match this board
    • setHex

      public void setHex(Coords c, Hex hex)
      Sets the hex into the location specified by the Coords.
      Parameters:
      c - the Coords.
      hex - the hex to be set into position.
    • boardIsSize

      public static boolean boardIsSize(File filepath, BoardDimensions size)
      Checks if a board file is the specified size.
      Parameters:
      filepath - The path to the board file.
      size - The dimensions of the board to test.
      Returns:
      true if the dimensions match.
    • getSize

      public static BoardDimensions getSize(File filepath)
      Inspect specified board file and return its dimensions.
      Parameters:
      filepath - The path to the board file.
      Returns:
      A BoardDimensions object containing the dimension.
    • getTags

      public static Set<String> getTags(File filepath)
      Inspects the given board file and returns a set of its tags.
    • isValid

      public static boolean isValid(String board)
    • isLegalDeployment

      public boolean isLegalDeployment(Coords c, Player p)
      Can the given player deploy at these coordinates?
    • isLegalDeployment

      public boolean isLegalDeployment(Coords c, Entity e)
      Can the given entity be deployed at these coordinates
    • isLegalDeployment

      public boolean isLegalDeployment(Coords c, int zoneType, int startingWidth, int startingOffset, int startingAnyNWx, int startingAnyNWy, int startingAnySEx, int startingAnySEy)
      Can an object be deployed at these coordinates, given a starting zone, width of starting zone and offset from edge of board?
    • getOppositeEdge

      public int getOppositeEdge(int cardinalEdge)
      Determine the opposite edge from the given edge Returns START_NONE for non-cardinal edges (North, South, West, East)
      Parameters:
      cardinalEdge - The edge to return the opposite of
      Returns:
      Constant representing the opposite edge
    • load

      public void load(File filepath)
      Load board data from a file.
      Parameters:
      filepath - The path to the file.
    • load

      public void load(InputStream is)
      Loads this board from an InputStream
    • load

      public void load(String boardString, @Nullable List<String> errors)
    • load

      public void load(InputStream is, @Nullable List<String> errors, boolean continueLoadOnError)
    • isValid

      public boolean isValid()
    • isValid

      public boolean isValid(@Nullable List<String> errors)
    • save

      public void save(OutputStream os)
      Writes data for the board, as text to the OutputStream
    • addInfernoTo

      public void addInfernoTo(Coords coords, Inferno round, int hits)
      Record that the given coordinates have received a hit from an inferno.
      Parameters:
      coords - the Coords of the hit.
      round - the kind of round that hit the hex.
      hits - the int number of rounds that hit
      Throws:
      IllegalArgumentException - if the hits number is negative
    • removeInfernoFrom

      public void removeInfernoFrom(Coords coords)
      Extinguish inferno at the target hex.
      Parameters:
      coords - the Coords of the hit.
    • removeBombIconsFrom

      public void removeBombIconsFrom(Coords coords)
    • clearBombIcons

      public void clearBombIcons()
    • isInfernoBurning

      public boolean isInfernoBurning(Coords coords)
      Determine if the given coordinates has a burning inferno.
      Parameters:
      coords - - the Coords being checked.
      Returns:
      true if those coordinates have a burning inferno round. false if no inferno has hit those coordinates or if it has burned out.
    • getInfernoBurningCoords

      public Enumeration<Coords> getInfernoBurningCoords()
      Get an enumeration of all coordinates with infernos still burning.
      Returns:
      an Enumeration of Coords that have infernos still burning.
    • getBuildings

      public Enumeration<Building> getBuildings()
      Returns:
      an Enumeration of Buildings on the Board
    • getBuildingsVector

      public Vector<Building> getBuildingsVector()
      Returns:
      the Vector of all the board's buildings
    • getBuildingAt

      @Nullable public Building getBuildingAt(Coords coords)
      Get the building at the given coordinates.
      Parameters:
      coords - the Coords being examined.
      Returns:
      a Building object, if there is one at the given coordinates, otherwise a null will be returned.
    • collapseBuilding

      public void collapseBuilding(Vector<Coords> coords)
      Collapse a vector of building hexes.
      Parameters:
      coords - the Vector of Coords objects to be collapsed.
    • collapseBuilding

      public void collapseBuilding(Coords coords)
      The given building hex has collapsed. Remove it from the board and replace it with rubble.
      Parameters:
      coords - the Building that has collapsed.
    • collapseBuilding

      public void collapseBuilding(Building bldg)
      The given building has collapsed. Remove it from the board and replace it with rubble.
      Parameters:
      bldg - the Building that has collapsed.
    • updateBuilding

      public void updateBuilding(Building receivedBuilding)
      Update a locally stored building with CF and other values from a building received from the server.
      Parameters:
      receivedBuilding - The Building received from the server
    • getRoadsAutoExit

      public boolean getRoadsAutoExit()
      Get the current value of the "road auto-exit" option.
      Returns:
      true if roads should automatically exit onto all adjacent pavement hexes. false otherwise.
    • setRoadsAutoExit

      public void setRoadsAutoExit(boolean value)
      Set the value of the "road auto-exit" option.
      Parameters:
      value - The value to set for the option; true if roads should automatically exit onto all adjacent pavement hexes. false otherwise.
    • addBoardListener

      public void addBoardListener(BoardListener listener)
      Adds the specified board listener to receive board events from this board.
      Parameters:
      listener - the board listener.
    • removeBoardListener

      public void removeBoardListener(BoardListener listener)
      Removes the specified board listener.
      Parameters:
      listener - the board listener.
    • processBoardEvent

      public void processBoardEvent(BoardEvent event)
      Fires a board event which typically leads to the BoardView and minimap being redrawn. This is public as the boards and minimaps show some data that is not part of the Board class and Board has no way of knowing when a change happens. An example of this is arty auto hexes which are stored in the player class
    • getInfernos

      public Hashtable<Coords,InfernoTracker> getInfernos()
      Returns:
      an Hashtable of InfernoTrackers on the board.
    • setBridgeCF

      public void setBridgeCF(int value)
      Set the CF of bridges
      Parameters:
      value - The value to set the bridge CF to.
    • setRandomBasementsOff

      public void setRandomBasementsOff()
    • getSpecialHexDisplay

      public Collection<SpecialHexDisplay> getSpecialHexDisplay(Coords coords)
      Returns:
      Special events that should be marked on hexes, such as artillery fire as well as notes players can leave manually on hexes. Always returns at least an empty list, never null.
    • addSpecialHexDisplay

      public void addSpecialHexDisplay(Coords coords, SpecialHexDisplay shd, boolean fireEvent)
      Adds the given SHD at the given coords to this board. An event can be fired for this board change (this should not be done on the Server).
      Parameters:
      coords - The position of the SHD on this board
      shd - The SpecialHexDisplay to add
      fireEvent - When true, a BoardEvent is fired for the affected coords
    • addSpecialHexDisplay

      public void addSpecialHexDisplay(Coords coords, SpecialHexDisplay shd)
      Adds the given SHD at the given coords to this board. This method does not fire an event for the change.
      Parameters:
      coords - The position of the SHD on this board
      shd - The SpecialHexDisplay to add
    • removeSpecialHexDisplay

      public void removeSpecialHexDisplay(Coords coords, SpecialHexDisplay shd)
      Removes the given SHD from the given coords.
      Parameters:
      coords - The position of the SHD on this board
      shd - The SpecialHexDisplay to remove
    • removeSpecialHexDisplay

      public void removeSpecialHexDisplay(Coords coords, SpecialHexDisplay shd, boolean fireEvent)
      Removes the given SHD from the given coords.
      Parameters:
      coords - The position of the SHD on this board
      shd - The SpecialHexDisplay to remove
    • getSpecialHexDisplayTable

      public Map<Coords,Collection<SpecialHexDisplay>> getSpecialHexDisplayTable()
    • setSpecialHexDisplayTable

      public void setSpecialHexDisplayTable(Map<Coords,Collection<SpecialHexDisplay>> shd)
      Sets this board's specialHexes to a new set. This method should be used by the client when receiving an update from the server.
      Parameters:
      shd - The new map of SpecialHexDisplays
    • setType

      public void setType(int t)
    • getMaxElevation

      public int getMaxElevation()
      Returns:
      the highest elevation hex on the board.
    • getMinElevation

      public int getMinElevation()
      Returns:
      the lowest elevation hex on the board.
    • resetStoredElevation

      public void resetStoredElevation()
      Resets the Min and Max elevations to their default values.
    • containsBridges

      public boolean containsBridges()
    • getDescription

      @Nullable public String getDescription()
      Gets the description of the map.
      Returns:
      The description of the map, if one exists, otherwise null.
    • setDescription

      public void setDescription(@Nullable String s)
      Sets the description of the map.
      Parameters:
      s - The description of the map; may be null.
    • getAnnotations

      public Map<Coords,Collection<String>> getAnnotations()
      Gets every annotation on the map.
      Returns:
      A read-only map of per-hex annotations.
    • getAnnotations

      public Collection<String> getAnnotations(Coords c)
      Gets the annotations associated with a hex.
      Parameters:
      c - Coordinates of the hex.
      Returns:
      A collection of annotations for the hex.
    • setAnnotations

      public void setAnnotations(Coords c, @Nullable Collection<String> a)
      Sets annotations on a given hex.
      Parameters:
      c - Coordinates of the hex to apply the annotations to.
      a - A collection of annotations to assign to the hex. This may be null.
    • setTheme

      public void setTheme(@Nullable String newTheme)
      Sets a tileset theme for all hexes of the board. Passing null as newTheme resets the theme to the theme specified in the board file.
    • isOnBoardEdge

      public boolean isOnBoardEdge(Coords coords)
      Returns:
      true when the given Coords coords is on the edge of the board.
    • createEmptyBoard

      public static Board createEmptyBoard(int width, int height)
    • addTag

      public void addTag(String newTag)
      Add the given tag string to the board's tags list.
    • removeTag

      public void removeTag(String tag)
      Removes the given tag string from the board's tags list.
    • getTags

      public Set<String> getTags()
      Returns:
      the board's tags list. The list is unmodifiable. Use addTag and removeTag to change it.
    • getBoardName

      public String getBoardName()
      Returns:
      The name of this map; this is meant to be displayed in the GUI.
    • setMapName

      public void setMapName(String mapName)
    • exitsAsIntList

      public static List<Integer> exitsAsIntList(int exits)
      Returns:
      Given an "exits" value, returns it in a list form. (i.e. exits value of 4 returns {3}, exit value of 5 returns {1, 3}
    • IntListAsExits

      public static int IntListAsExits(List<Integer> list)
      Given a list of integers, returns them in a bit-packed form.
    • addDeploymentZone

      public void addDeploymentZone(int zoneId, HexArea hexArea)
      Adds a deployment zone with the given ID and the hexes described by the given HexArea to this board, replacing the previously present zone of that ID, if there had been one. Note that the zone ID can be outside those reachable by board files; e.g. the zone ID can be 1000. Note however that zone IDs in the range of 0 to 50 should be avoided as they'll overwrite terrain deployment zones.
      Parameters:
      zoneId - The zone Id
      hexArea - The hexes comprising this deployment zone
    • resetDeploymentZones

      public void resetDeploymentZones()
      Resets the "intermediate" deployment zones associated with this board, in case the deployment zones change
    • getCustomDeploymentZones

      public Set<Integer> getCustomDeploymentZones()
      Gets the IDs of all custom deployment zones defined for this board.
    • getCustomDeploymentZone

      public Set<Coords> getCustomDeploymentZone(int zoneID)
      Gets all the coordinates in the given custom deployment zone
    • decodeCustomDeploymentZoneID

      public static int decodeCustomDeploymentZoneID(int zoneID)
      Use this method to convert a deployment zone ID as represented in the UI zone selectors (e.g. in the PlayerSettingsDialog) to a deployment zone ID as stored in the board.
    • encodeCustomDeploymentZoneID

      public static int encodeCustomDeploymentZoneID(int zoneID)
      Use this method to convert a deployment zone ID as stored in the board to a number suitable for representation in the UI zone selectors (e.g. PlayerSettingsDialog)
    • setBoardId

      public void setBoardId(int boardId)
      Sets the board's ID. Within an MM game, the ID must be unique. To preserve "normal" games, 0 is the default.
    • getBoardId

      public int getBoardId()
    • setEnclosingBoard

      public void setEnclosingBoard(int enclosingBoardId)
    • getEnclosingBoardId

      public int getEnclosingBoardId()
      Returns:
      The ID of the enclosing board of this board, or -1 if it has no enclosing board.
    • setEmbeddedBoard

      public void setEmbeddedBoard(int boardId, Coords coords)
      Sets the given board ID as an embedded board at the given coords of this board. The board ID is not checked nor the board type. The coords are checked against the size of this board.
      Parameters:
      boardId - The board ID to embed
      coords - The location to place the given board
      Throws:
      IllegalArgumentException - When this board does not contain the given coords
    • embeddedBoardCoords

      public Set<Coords> embeddedBoardCoords()
    • embeddedBoardPosition

      @Nullable public Coords embeddedBoardPosition(int boardId)
    • getEmbeddedBoardAt

      public int getEmbeddedBoardAt(Coords coords)
    • getEmbeddedBoardHexes

      public Set<Coords> getEmbeddedBoardHexes()
    • isGround

      public boolean isGround()
    • isLowAltitude

      public boolean isLowAltitude()
      Returns:
      True if this board is a low altitude (a.k.a. atmospheric) board, either with terrain or without terrain ("sky").
    • isSky

      public boolean isSky()
      Returns:
      True if this board is a low altitude (a.k.a. atmospheric) board without terrain ("sky").
    • isSpace

      public boolean isSpace()
      Returns:
      True if this board is a space board, either close to a planet with some atmospheric hexes ("high altitude") or in deeper space.
    • isHighAltitude

      public boolean isHighAltitude()
      Returns:
      True if this board is a high altitude board, i.e. a space board close to a planet with some atmospheric hexes.
    • setBoardType

      public void setBoardType(BoardType boardType)
    • getBoardType

      public BoardType getBoardType()
    • toString

      public String toString()
      Overrides:
      toString in class Object