rails.game
Class MapHex

java.lang.Object
  extended by java.util.Observable
      extended by rails.game.model.ModelObject
          extended by rails.game.MapHex
All Implemented Interfaces:
ConfigurableComponentI, MoveableHolder, StationHolder, TokenHolder

public class MapHex
extends ModelObject
implements ConfigurableComponentI, StationHolder, TokenHolder

Represents a Hex on the Map from the Model side.

Tile orientations. Tiles can be oriented NS or EW; the directions refer to the "flat" hex sides.

The term "rotation" is used to indicate the amount of rotation (in 60 degree units) from the standard orientation of the tile (sometimes the term orientation is also used to refer to rotation).

Rotation is always relative to the standard orientation, which has the printed tile number on the S edge for NS oriented tiles, or on the SW edge for EW oriented tiles. The rotation numbers are indicated in the below picture for an NS-oriented tile:

____3____ / \ 2 4 / NS \ \ / 1 5 \____0____/

For EW-oriented tiles the above picture should be rotated 30 degrees clockwise.


Nested Class Summary
static class MapHex.Run
           
 
Field Summary
protected  java.util.List<City> cities
           
protected  java.lang.String cityName
           
protected  int column
           
protected  TileI currentTile
           
protected  int currentTileRotation
           
protected  java.util.List<PublicCompanyI> destinations
           
static int EW
           
protected  java.util.Map<PublicCompanyI,City> homes
           
protected  java.lang.String impassable
           
protected  java.util.List<java.lang.Integer> impassableSides
           
protected  java.lang.String infoText
           
protected  int letter
           
protected static org.apache.log4j.Logger log
           
protected  MapManager mapManager
           
protected  java.util.Map<java.lang.Integer,City> mCities
           
protected  java.lang.String name
           
protected  MapHex[] neighbours
          Neighbouring hexes to which track may be laid.
static int NS
           
protected  int number
           
protected  int[] offBoardValues
          Values if this is an off-board hex
protected  java.util.List<TokenI> offStationTokens
          Tokens that are not bound to a Station (City), such as Bonus tokens
protected  boolean[] openHexSides
          Any open sides against which track may be laid even at board edges (1825)
protected  int preprintedTileId
           
protected  int preprintedTileRotation
           
protected  java.lang.String reservedForCompany
           
protected  java.util.List<RevenueBonusTemplate> revenueBonuses
          Storage of revenueBonus that are bound to the hex
protected  int row
           
protected  MapHex.Run runThroughAllowed
          Run-through status of any "city"-type stations on the hex (whether visible or not).
protected  MapHex.Run runToAllowed
          Run-to status of any "city"-type stations on the hex (whether visible or not).
protected  int[] tileCost
           
protected  java.lang.String tileFileName
           
protected  int x
           
protected  int y
           
 
Fields inherited from class rails.game.model.ModelObject
dependents, option
 
Constructor Summary
MapHex(MapManager mapManager)
           
 
Method Summary
 void addDestination(PublicCompanyI company)
           
 void addHome(PublicCompanyI company, int cityNumber)
           
 void addImpassableSide(int orientation)
           
 boolean addObject(Moveable object, int[] position)
          Add an object to its list at a certain position.
 boolean addToken(TokenI token, int position)
           
 void addX(int offset)
          Add an X offset.
 void addY(int offset)
          Add an Y offset.
 void configureFromXML(Tag tag)
          Instructs the component to configure itself from the provided XML element.
 boolean equals(MapHex hex)
           
 void finishConfiguration(GameManagerI gameManager)
          This method is intended to be called for each configurable component, to perforn any initialisation activities that require any other components to be initialised first.
 java.util.List<BaseToken> getBaseTokens()
           
 java.util.List<City> getCities()
           
 City getCity(int cityNumber)
           
 java.lang.String getCityName()
           
 int getCityOfBaseToken(PublicCompanyI company)
          Return the city number (1,...) where a company has a base token.
 int getColumn()
           
 java.lang.String getConnectionString(int cityNumber)
           
 java.lang.String getConnectionString(TileI tile, int rotation, int stationNumber)
          Get a String describing one stations's connection directions of a laid tile, taking into account the current tile rotation.
 int getCurrentOffBoardValue(PhaseI phase)
           
 TileI getCurrentTile()
           
 int getCurrentTileRotation()
           
 java.util.List<PublicCompanyI> getDestinations()
           
 java.util.Map<PublicCompanyI,City> getHomes()
           
 java.util.List<java.lang.Integer> getImpassableSides()
           
 java.lang.String getInfo()
           
 int[] getListIndex(Moveable object)
           
 MapManager getMapManager()
           
 java.lang.String getName()
          Each station must have a name, which includes the tile Id (if on a tile) or the hex name (if on a MapHex).
 MapHex getNeighbor(int orientation)
           
 MapHex[] getNeighbors()
           
 int[] getOffBoardValues()
           
 java.lang.String getOrientationName(int orientation)
           
 int getPreprintedTileId()
           
 int getPreprintedTileRotation()
           
 City getRelatedCity(Station station)
           
 java.lang.String getReservedForCompany()
           
 java.util.List<RevenueBonusTemplate> getRevenueBonuses()
           
 int getRow()
           
 java.lang.String getText()
          The string sent to the GUIHex as it is notified.
 int getTileCost()
           
 int getTileCost(int index)
           
 int[] getTileCostAsArray()
           
 java.lang.String getTileFileName()
           
 int getTileOrientation()
           
 java.util.List<TokenI> getTokens()
           
 java.util.List<TokenI> getTokens(int cityNumber)
           
 int[] getTrackEndPoints(TileI tile, int rotation, Station station)
           
 int getX()
           
 int getY()
           
 boolean hasNeighbour(int orientation)
           
 boolean hasOffBoardValues()
           
 boolean hasTokenOfCompany(PublicCompanyI company)
          Check if the tile already has a token of a company in any station
 boolean hasTokens()
          Do we have any tokens?
 boolean hasTokenSlotsLeft()
           
 boolean hasTokenSlotsLeft(int station)
           
 boolean isBlockedForTileLays()
           
 boolean isBlockedForTokenLays(PublicCompanyI company, int cityNumber)
           
 boolean isDestination(PublicCompanyI company)
           
 boolean isHomeFor(PublicCompanyI company)
           
 boolean isImpassable(MapHex neighbour)
           
 boolean isNeighbour(MapHex neighbour, int direction)
           
 boolean isOpenSide(int side)
           
 boolean isReservedForCompany()
           
 MapHex.Run isRunThroughAllowed()
           
 MapHex.Run isRunToAllowed()
           
 boolean isUpgradeableNow()
           
 boolean isUpgradeableNow(PhaseI currentPhase)
           
 boolean layBaseToken(PublicCompanyI company, int station)
           
 boolean layBonusToken(BonusToken token, PhaseManager phaseManager)
          Lay a bonus token.
 boolean letterAHasEvenNumbers()
           
 boolean lettersGoHorizontal()
           
 boolean removeObject(Moveable object)
           
 boolean removeToken(TokenI token)
          Remove a token.
 void replaceTile(TileI oldTile, TileI newTile, int newTileOrientation, java.util.List<City> newCities)
          Execute a tile replacement.
 void setBlockedForTileLays(boolean isBlocked)
           
 void setBlockedForTokenLays(boolean isBlocked)
           
 void setNeighbor(int orientation, MapHex neighbour)
           
 java.lang.String toString()
           
 void upgrade(LayTile action)
          new wrapper function for the LayTile action that calls the actual upgrade mehod
 void upgrade(TileI newTile, int newRotation, java.util.Map<java.lang.String,java.lang.Integer> relaidTokens)
          Prepare a tile upgrade.
 
Methods inherited from class rails.game.model.ModelObject
addDependent, addObserver, getUpdate, removeDependent, resetOption, setOption, update
 
Methods inherited from class java.util.Observable
clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

EW

public static final int EW
See Also:
Constant Field Values

NS

public static final int NS
See Also:
Constant Field Values

x

protected int x

y

protected int y

name

protected java.lang.String name

row

protected int row

column

protected int column

letter

protected int letter

number

protected int number

tileFileName

protected java.lang.String tileFileName

preprintedTileId

protected int preprintedTileId

currentTile

protected TileI currentTile

currentTileRotation

protected int currentTileRotation

preprintedTileRotation

protected int preprintedTileRotation

tileCost

protected int[] tileCost

cityName

protected java.lang.String cityName

infoText

protected java.lang.String infoText

reservedForCompany

protected java.lang.String reservedForCompany

neighbours

protected MapHex[] neighbours
Neighbouring hexes to which track may be laid.


offBoardValues

protected int[] offBoardValues
Values if this is an off-board hex


impassable

protected java.lang.String impassable

impassableSides

protected java.util.List<java.lang.Integer> impassableSides

cities

protected java.util.List<City> cities

mCities

protected java.util.Map<java.lang.Integer,City> mCities

homes

protected java.util.Map<PublicCompanyI,City> homes

destinations

protected java.util.List<PublicCompanyI> destinations

offStationTokens

protected java.util.List<TokenI> offStationTokens
Tokens that are not bound to a Station (City), such as Bonus tokens


revenueBonuses

protected java.util.List<RevenueBonusTemplate> revenueBonuses
Storage of revenueBonus that are bound to the hex


openHexSides

protected boolean[] openHexSides
Any open sides against which track may be laid even at board edges (1825)


runThroughAllowed

protected MapHex.Run runThroughAllowed
Run-through status of any "city"-type stations on the hex (whether visible or not). Indicates whether or not a single train can run through such stations, i.e. both enter and leave it. Has no meaning if no "city"-type stations exist on this hex.

Values (see Run below for definitions):
- "yes" (default for all except off-map hexes) means that trains of all companies may run through this station, unless it is completely filled with foreign base tokens.
- "tokenOnly" means that trains may only run through the station if it contains a base token of the operating company.
- "no" (default for off-map hexes) means that no train may run through this hex.


runToAllowed

protected MapHex.Run runToAllowed
Run-to status of any "city"-type stations on the hex (whether visible or not). Indicates whether or not a single train can run from or to such stations, i.e. either enter or leave it. Has no meaning if no "city"-type stations exist on this hex.

Values (see Run below for definitions):
- "yes" (default) means that trains of all companies may run to/from this station.
- "tokenOnly" means that trains may only access the station if it contains a base token of the operating company.
- "no" would mean that the hex is inaccessible (like 18AL Birmingham in the early game), but this option is not yet useful as there is no provision yet to change this setting in an undoable way (no state variable).


mapManager

protected MapManager mapManager

log

protected static org.apache.log4j.Logger log
Constructor Detail

MapHex

public MapHex(MapManager mapManager)
Method Detail

configureFromXML

public void configureFromXML(Tag tag)
                      throws ConfigurationException
Description copied from interface: ConfigurableComponentI
Instructs the component to configure itself from the provided XML element.

Specified by:
configureFromXML in interface ConfigurableComponentI
Throws:
ConfigurationException
See Also:
rails.game.ConfigurableComponentI#configureFromXML(org.w3c.dom.Element)

finishConfiguration

public void finishConfiguration(GameManagerI gameManager)
Description copied from interface: ConfigurableComponentI
This method is intended to be called for each configurable component, to perforn any initialisation activities that require any other components to be initialised first. This includes creating any required relationships to other configured components and objects.

This method should be called where necessary after all XML file parsing has completed, so that all objects that need to be related to do exist.

Specified by:
finishConfiguration in interface ConfigurableComponentI
Parameters:
gameManager - The 'parent' configurable component is passed to allow the 'child' to access any other object without the need to resort to static calls where possible.

addImpassableSide

public void addImpassableSide(int orientation)

getImpassableSides

public java.util.List<java.lang.Integer> getImpassableSides()

isImpassable

public boolean isImpassable(MapHex neighbour)

isNeighbour

public boolean isNeighbour(MapHex neighbour,
                           int direction)

isOpenSide

public boolean isOpenSide(int side)

getTileOrientation

public int getTileOrientation()

letterAHasEvenNumbers

public boolean letterAHasEvenNumbers()
Returns:
Returns the letterAHasEvenNumbers.

lettersGoHorizontal

public boolean lettersGoHorizontal()
Returns:
Returns the lettersGoHorizontal.

getOrientationName

public java.lang.String getOrientationName(int orientation)

getColumn

public int getColumn()
Returns:
Returns the column.

getRow

public int getRow()
Returns:
Returns the row.

getName

public java.lang.String getName()
Description copied from interface: TokenHolder
Each station must have a name, which includes the tile Id (if on a tile) or the hex name (if on a MapHex).

Specified by:
getName in interface MoveableHolder
Specified by:
getName in interface StationHolder
Specified by:
getName in interface TokenHolder

getX

public int getX()

getY

public int getY()

addX

public void addX(int offset)
Add an X offset. Required to avoid negative coordinate values, as arise in 1830 Wabash.


addY

public void addY(int offset)
Add an Y offset. Required to avoid negative coordinate values.


getPreprintedTileId

public int getPreprintedTileId()
Returns:
Returns the preprintedTileId.

getPreprintedTileRotation

public int getPreprintedTileRotation()

getTileFileName

public java.lang.String getTileFileName()
Returns:
Returns the image file name for the tile.

setNeighbor

public void setNeighbor(int orientation,
                        MapHex neighbour)

getNeighbor

public MapHex getNeighbor(int orientation)

getNeighbors

public MapHex[] getNeighbors()

hasNeighbour

public boolean hasNeighbour(int orientation)

getCurrentTile

public TileI getCurrentTile()

getCurrentTileRotation

public int getCurrentTileRotation()

getTileCost

public int getTileCost()

getTileCost

public int getTileCost(int index)

getTileCostAsArray

public int[] getTileCostAsArray()

upgrade

public void upgrade(LayTile action)
new wrapper function for the LayTile action that calls the actual upgrade mehod

Parameters:
action - executed LayTile action

upgrade

public void upgrade(TileI newTile,
                    int newRotation,
                    java.util.Map<java.lang.String,java.lang.Integer> relaidTokens)
Prepare a tile upgrade. The actual tile replacement is done in replaceTile(), via a TileMove object.


replaceTile

public void replaceTile(TileI oldTile,
                        TileI newTile,
                        int newTileOrientation,
                        java.util.List<City> newCities)
Execute a tile replacement. This method should only be called from TileMove objects. It is also used to undo tile lays.

Parameters:
oldTile - The tile to be replaced (only used for validation).
newTile - The new tile to be laid on this hex.
newTileOrientation - The orientation of the new tile (0-5).

layBaseToken

public boolean layBaseToken(PublicCompanyI company,
                            int station)

layBonusToken

public boolean layBonusToken(BonusToken token,
                             PhaseManager phaseManager)
Lay a bonus token.

Parameters:
token - The bonus token object to place
phaseManager - The PhaseManager is also passed in case the token must register itself for removal when a certain phase starts.
Returns:

addToken

public boolean addToken(TokenI token,
                        int position)
Specified by:
addToken in interface TokenHolder

getBaseTokens

public java.util.List<BaseToken> getBaseTokens()

getTokens

public java.util.List<TokenI> getTokens()
Specified by:
getTokens in interface TokenHolder
Returns:
ArrayList of all tokens we have.

hasTokens

public boolean hasTokens()
Description copied from interface: TokenHolder
Do we have any tokens?

Specified by:
hasTokens in interface TokenHolder
Returns:
Boolean

removeToken

public boolean removeToken(TokenI token)
Description copied from interface: TokenHolder
Remove a token. Subclasses may override this method to implement side effects.

Specified by:
removeToken in interface TokenHolder
Parameters:
token - The token object to remove.
Returns:
True if successful.

addObject

public boolean addObject(Moveable object,
                         int[] position)
Description copied from interface: MoveableHolder
Add an object to its list at a certain position.

Specified by:
addObject in interface MoveableHolder
Parameters:
object - The Moveable object to be added.
position - Position to insert object at. O: at front, -1, at end, >0: at that position.
Returns:
True if successful.

removeObject

public boolean removeObject(Moveable object)
Specified by:
removeObject in interface MoveableHolder

getListIndex

public int[] getListIndex(Moveable object)
Specified by:
getListIndex in interface MoveableHolder

hasTokenSlotsLeft

public boolean hasTokenSlotsLeft(int station)

hasTokenSlotsLeft

public boolean hasTokenSlotsLeft()

hasTokenOfCompany

public boolean hasTokenOfCompany(PublicCompanyI company)
Check if the tile already has a token of a company in any station


getTokens

public java.util.List<TokenI> getTokens(int cityNumber)

getCityOfBaseToken

public int getCityOfBaseToken(PublicCompanyI company)
Return the city number (1,...) where a company has a base token. If none, return zero.

Parameters:
company -
Returns:

getCities

public java.util.List<City> getCities()

getCity

public City getCity(int cityNumber)

getRelatedCity

public City getRelatedCity(Station station)

addHome

public void addHome(PublicCompanyI company,
                    int cityNumber)
             throws ConfigurationException
Throws:
ConfigurationException

getHomes

public java.util.Map<PublicCompanyI,City> getHomes()

isHomeFor

public boolean isHomeFor(PublicCompanyI company)

addDestination

public void addDestination(PublicCompanyI company)

getDestinations

public java.util.List<PublicCompanyI> getDestinations()

isDestination

public boolean isDestination(PublicCompanyI company)

isBlockedForTileLays

public boolean isBlockedForTileLays()
Returns:
Returns false if no tiles may yet be laid on this hex.

setBlockedForTileLays

public void setBlockedForTileLays(boolean isBlocked)
Parameters:
isBlocked - The isBlocked to set (state variable)

isUpgradeableNow

public boolean isUpgradeableNow()

isUpgradeableNow

public boolean isUpgradeableNow(PhaseI currentPhase)

isBlockedForTokenLays

public boolean isBlockedForTokenLays(PublicCompanyI company,
                                     int cityNumber)
Returns:
Returns false if no base tokens may yet be laid on this hex and station. NOTE: this method currently only checks for prohibitions caused by the presence of unlaid home base tokens. It does NOT (yet) check for free space. There are the following cases to check for each company located there A) City is decided or there is only one city => check if the city has a free slot or not (examples: NYNH in 1830 for a two city tile, NYC for a one city tile) B) City is not decided (example: Erie in 1830) two subcases depending on isHomeBlockedForAllCities - (true): all cities of the hex have remaining slots available - (false): no city of the hex has remaining slots available C) Or the company does not block its home city at all (example:Pr in 1835) then isBlockedForTokenLays attribute is used NOTE: It now deals with more than one company with a home base on the same hex. Previously there was only the variable isBlockedForTokenLays which is set to yes to block the whole hex for the token lays until the (home) company laid their token

setBlockedForTokenLays

public void setBlockedForTokenLays(boolean isBlocked)
Parameters:
isBlocked - The isBlocked to set (state variable)

hasOffBoardValues

public boolean hasOffBoardValues()

getOffBoardValues

public int[] getOffBoardValues()

getCurrentOffBoardValue

public int getCurrentOffBoardValue(PhaseI phase)

getCityName

public java.lang.String getCityName()

getInfo

public java.lang.String getInfo()

getReservedForCompany

public java.lang.String getReservedForCompany()

isReservedForCompany

public boolean isReservedForCompany()

getRevenueBonuses

public java.util.List<RevenueBonusTemplate> getRevenueBonuses()

equals

public boolean equals(MapHex hex)

getMapManager

public MapManager getMapManager()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getText

public java.lang.String getText()
The string sent to the GUIHex as it is notified. Format is tileId/orientation.

Specified by:
getText in class ModelObject
Returns:

getConnectionString

public java.lang.String getConnectionString(TileI tile,
                                            int rotation,
                                            int stationNumber)
Get a String describing one stations's connection directions of a laid tile, taking into account the current tile rotation.

Returns:

getConnectionString

public java.lang.String getConnectionString(int cityNumber)

getTrackEndPoints

public int[] getTrackEndPoints(TileI tile,
                               int rotation,
                               Station station)

isRunThroughAllowed

public MapHex.Run isRunThroughAllowed()

isRunToAllowed

public MapHex.Run isRunToAllowed()