Class Hex

java.lang.Object
megamek.common.Hex
All Implemented Interfaces:
Serializable

public class Hex extends Object implements Serializable
Hex represents a single hex on the board.
See Also:
  • Constructor Details

    • Hex

      public Hex()
      Constructs a clear, plain hex at level 0.
    • Hex

      public Hex(int level)
      Constructs a clean, plain hex at specified level.
    • Hex

      public Hex(int level, Terrain[] terrains, String theme)
    • Hex

      public Hex(int level, Terrain[] terrains, String theme, Coords c)
      Constructs a Hex with all parameters.
    • Hex

      public Hex(int level, String terrain, String theme)
    • Hex

      public Hex(int level, String terrain, String theme, Coords c)
      Constructs a Hex from a combined string terrains format
  • Method Details

    • getLevel

      public int getLevel()
      Returns:
      The level of a hex, as defined in TW. This refers to the height of the ground terrain.
    • setLevel

      public void setLevel(int level)
    • getTheme

      public String getTheme()
      The theme is intended as a tag for the tileset file to indicate a special graphic for the hex
      Returns:
      the theme name
    • setTheme

      public void setTheme(String theme)
    • getTerrainTypes

      public int[] getTerrainTypes()
      Returns:
      An array that contains an id for each terrain present in this hex.
    • getTerrainTypesSet

      public Set<Integer> getTerrainTypesSet()
      Returns:
      A HashSet that contains an id for each terrain present in this hex.
    • resetTheme

      public void resetTheme()
      Resets the theme to what was specified in the board file.
    • clearExits

      public void clearExits()
      Clears the "exits" flag for all terrains in the hex where it is not manually specified.
    • setExits

      public void setExits(Hex other, int direction)
      Sets the "exits" flag appropriately for the neighbouring hex in the provided direction. Does not reset connects in other directions. All Terrain.ROADs will automatically connect to Terrain.PAVEMENT hexes.
      Parameters:
      other - the neighbouring hex in the specified direction
      direction - the int direction of the exit. This value should be between 0 and 5 (inclusive).
      See Also:
    • setExits

      public void setExits(Hex other, int direction, boolean roadsAutoExit)
      Sets the "exits" flag appropriately for the neighbouring hex in the provided direction. Does not reset connects in other directions.
      Parameters:
      other - the neighbouring hex in the specified direction
      direction - the int direction of the exit. This value should be between 0 and 5 (inclusive).
      roadsAutoExit - if to automatically exit onto Pavement hexes
      See Also:
    • containsTerrainExit

      public boolean containsTerrainExit(int terrType, int direction)
      Determine if this Hex contains the indicated terrain that exits in the specified direction.
      Parameters:
      terrType - the int type of the terrain.
      direction - the int direction of the exit. This value should be between 0 and 5 (inclusive).
      Returns:
      true if this Hex contains the indicated terrain that exits in the specified direction, or false if bad input is supplied, if no such terrain exists, or if it doesn't exit in that direction.
      See Also:
    • containsExit

      public boolean containsExit(int direction)
      Determines if this Hex contains any exists in the specified direction.
      Parameters:
      direction - the int direction of the exit. This value should be between 0 and 5 (inclusive).
      Returns:
      true if this Hex contains any terrain that exits in the specified direction. false if bad input is supplied or if no terrain exits in that direction.
      See Also:
    • hasExitableTerrain

      public boolean hasExitableTerrain()
      Returns:
      if this hex contains a terrain type that can have exits
    • ceiling

      public int ceiling()
      Returns:
      the highest level that features in this hex extend to. Above this level is assumed to be air. This assumes a ground map.
    • ceiling

      public int ceiling(boolean inAtmosphere)
      Parameters:
      inAtmosphere - Determines if the ceiling should be determined for an atmospheric map (eg, altitudes) or ground map (eg, levels)
      Returns:
      the highest level or altitude (depending on flag) that features in this hex extend to. Above this level is assumed to be air.
    • ceilingAltitude

      public int ceilingAltitude(boolean inAtmosphere)
      Returns the altitude ceiling, i.e. highest terrain feature influencing aero movement, of this hex. If inAtmosphere is false, the hex is assumed to be a ground map hex and the return value is 0, as airborne aero movement ignores all terrain features of a ground map (TW p.92). Otherwise the hex is assumed to be on a low altitude map and the hex level and a few terrains set an altitude of this hex. Note that the returned value is never below 0 to avoid having to deal with negative altitudes.
      Parameters:
      inAtmosphere - True if the ceiling should be determined for an atmospheric or ground map
      Returns:
      the altitude ceiling, i.e. highest terrain feature influencing aero movement, of this hex
    • maxTerrainFeatureElevation

      public int maxTerrainFeatureElevation(boolean inAtmosphere)
      Parameters:
      inAtmosphere - Determines if the ceiling should be determined for an atmospheric map (eg, altitudes) or ground map (eg, levels)
      Returns:
      the elevation or altitude of the terrain feature that rises the highest above the surface of the hex. For example, if the hex is on the ground map and contains woods, this would return 2.
    • floor

      public int floor()
      Returns the lowest reachable point of this hex, used for terrain types that can extend below the surface of the hex, such as water and basements. Unrevealed basements will not affect this value.
      Returns:
      the lowest level that revealed features in this hex extend to. Below this level is assumed to be bedrock and/or basement. Unrevealed basements will not affect this value.
    • depth

      public int depth()
      Returns:
      a level indicating how far features in this hex extends below the surface level.
    • depth

      public int depth(boolean hidden)
    • hasTerrainFactor

      public boolean hasTerrainFactor()
      Returns:
      true if this hex has a terrain with a non-zero terrain factor
    • containsTerrain

      public boolean containsTerrain(int type)
      Parameters:
      type - the terrain type to check
      Returns:
      true if the specified terrain is represented in the hex at any level.
      See Also:
    • containsTerrain

      public boolean containsTerrain(int type, int level)
      Parameters:
      type - the terrain type to check
      level - level to check the presence of the given terrain at
      Returns:
      if the specified terrain is represented in the hex at given level.
      See Also:
    • containsAnyTerrainOf

      public boolean containsAnyTerrainOf(int... types)
      Parameters:
      types - the terrains to check
      Returns:
      if at least one of the specified terrains are represented in the hex at any level.
      See Also:
    • containsAnyTerrainOf

      public boolean containsAnyTerrainOf(Set<Integer> types)
      Parameters:
      types - the terrains to check
      Returns:
      if at least one of the specified terrains are represented in the hex at any level.
      See Also:
    • containsAllTerrainsOf

      public boolean containsAllTerrainsOf(int... types)
      Parameters:
      types - the terrains to check
      Returns:
      true if all the specified terrains are represented in the hex at any level.
      See Also:
    • hasPavement

      public boolean hasPavement()
      Returns:
      True if there is pavement, a paved road or a bridge in the hex.
    • hasPavedRoad

      public boolean hasPavedRoad()
      If there's a road on this tile and it's paved, return true
      Returns:
    • hasPavementOrRoad

      public boolean hasPavementOrRoad()
      Returns:
      True if there is pavement, a road or a bridge in the hex.
    • hasVegetation

      public boolean hasVegetation()
      Returns:
      True if there is any woods or jungle in this hex, including foliage (i.e., height 1 woods/jungle).
    • vegetationCeiling

      public int vegetationCeiling()
      Returns:
      The height of woods or jungle in this hex (1, 2 or 3), or 0 if there isn't any.
    • hasDepth1WaterOrDeeper

      public boolean hasDepth1WaterOrDeeper()
      Returns:
      True if there is water in this hex of at least depth 1 (i.e., not depth 0)
    • terrainLevel

      public int terrainLevel(int type)
      Returns:
      the level of the terrain specified, or Terrain.LEVEL_NONE if the terrain is not present in the hex
    • getTerrain

      @Nullable public Terrain getTerrain(int type)
      Parameters:
      type - the specified type
      Returns:
      the terrain of the specified type, or null if the terrain is not present in the hex
    • getAnyTerrainOf

      @Nullable public Terrain getAnyTerrainOf(int type, int... types)
      Parameters:
      type - the preferred type to get
      types - the terrain types to check
      Returns:
      Returns the Terrain for the preferred type, then an unspecified one of the given types of Terrain, or null if none are preset.
    • addTerrain

      public void addTerrain(Terrain terrain)
      Parameters:
      terrain - the terrain to add to this hex
    • removeTerrain

      public void removeTerrain(int type)
      Parameters:
      type - the terrain type to remove
    • removeAllTerrains

      public void removeAllTerrains()
      Removes all Terrains from the hex.
    • terrainsPresent

      public int terrainsPresent()
      Returns:
      the number of terrain attributes present
    • duplicate

      public Hex duplicate()
      FIXME : I should be a clone implementation, not this
      Returns:
      new hex which is equal to this
    • applyTerrainPilotingModifiers

      public void applyTerrainPilotingModifiers(EntityMovementMode moveMode, PilotingRollData roll, boolean enteringRubble)
      Adds terrain modifiers to PSRs made in this hex
    • movementCost

      public int movementCost(Entity entity)
      Returns:
      extra movement cost for entering the hex
    • getIgnitionModifier

      public int getIgnitionModifier()
      Returns:
      the modifier to the roll to ignite this hex
    • isIgnitable

      public boolean isIgnitable()
      Returns:
      if this hex is ignitable
    • isClearForTakeoff

      public boolean isClearForTakeoff()
      Returns true if the hex is valid for takeoff - either clear, has pavement, or a road
      Returns:
    • isClearForLanding

      public boolean isClearForLanding()
    • getBaseTerrainType

      public int getBaseTerrainType()
      Returns the "Base Terrain" for the hex, or 0 if it is clear
      Returns:
    • getFireTurn

      public int getFireTurn()
    • incrementFireTurn

      public void incrementFireTurn()
    • resetFireTurn

      public void resetFireTurn()
    • getBogDownModifier

      public int getBogDownModifier(EntityMovementMode moveMode, boolean largeVee)
      get any modifiers to a bog-down roll in this hex. Takes the worst modifier. If there is no bog-down chance in this hex, then it returns TargetRoll.AUTOMATIC_SUCCESS
    • getUnstuckModifier

      public void getUnstuckModifier(int elev, PilotingRollData rollTarget)
      get any modifiers to an unstuck roll in this hex.
    • hasCliffTopTowards

      public boolean hasCliffTopTowards(Hex otherHex)
      True if this hex has a clifftop towards otherHex. This hex must have the terrain CLIFF_TOP, it must have exits specified (exits set to active) for the CLIFF_TOP terrain, and must have an exit in the direction of otherHex.
    • getCoords

      public Coords getCoords()
      Returns the position of this hex on the board.
    • setCoords

      public void setCoords(Coords c)
      Sets the coords of this hex. DO NOT USE outside board.java! WILL NOT MOVE THE HEX. Only the position of the hex in the board's data[] determines the actual location of the hex.
    • isClearHex

      public boolean isClearHex()
      Returns:
      if this hex is "clear", based on the absence of most terrain types.
    • isValid

      public boolean isValid(@Nullable List<String> errors)
      Returns true when the hex has no invalid terrain. When the given list of errors is not null, any error reports from this hex are added to it.
      Parameters:
      errors - A list to add error messages to
      Returns:
      True if the hex is valid
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getClipboardString

      public String getClipboardString()
      Returns a string representation of this Hex to use for copy/paste actions. The string contains the elevation, theme and terrains of this Hex except automatic terrains (such as inclines). The generated string can be parsed to generate a copy of the hex using parseClipboardString(String).
      Returns:
      A string representation to use when copying a hex to the clipboard.
    • parseClipboardString

      @Nullable public static Hex parseClipboardString(String clipboardString)
      Returns a new Hex parsed from a clipboard string representation. Returns null when the clipboard String is not created by getClipboardString() (i.e., when it does not at least start with "MegaMek Hex").
      Parameters:
      clipboardString - The string representation of the Hex to parse
      Returns:
      A hex containing any features that could be parsed from clipboardString