Package net.sf.freecol.common.model
Class TileImprovement
- java.lang.Object
-
- net.sf.freecol.common.model.FreeColObject
-
- net.sf.freecol.common.model.FreeColGameObject
-
- net.sf.freecol.common.model.TileItem
-
- net.sf.freecol.common.model.TileImprovement
-
- All Implemented Interfaces:
java.lang.Comparable<FreeColObject>
,Locatable
,Named
,ObjectWithId
public class TileImprovement extends TileItem
Represents a tile improvement, such as a river or road.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
EMPTY_RIVER_STYLE
static java.lang.String
EMPTY_ROAD_STYLE
static int
FJORD_RIVER
static int
LARGE_RIVER
private static java.util.logging.Logger
logger
private int
magnitude
The improvement magnitude.private static java.lang.String
MAGNITUDE_TAG
static int
NO_RIVER
River magnitudesstatic int
SMALL_RIVER
private TileImprovementStyle
style
Image and overlay style information for the improvement.private static java.lang.String
STYLE_TAG
static java.lang.String
TAG
private static java.lang.String
TILE_TAG
private static java.lang.String
TURNS_TAG
private int
turnsToComplete
Turns remaining until the improvement is complete, if any.private TileImprovementType
type
The type of this improvement.private static java.lang.String
TYPE_TAG
private boolean
virtual
Whether this is a virtual improvement granted by some structure on the tile (a Colony, for example).private static java.lang.String
VIRTUAL_TAG
-
Fields inherited from class net.sf.freecol.common.model.FreeColGameObject
initialized
-
Fields inherited from class net.sf.freecol.common.model.FreeColObject
ARRAY_SIZE_TAG, DEFAULT_CLASS_INDEX, fcoComparator, ID_ATTRIBUTE_TAG, PARTIAL_ATTRIBUTE_TAG, VALUE_TAG
-
-
Constructor Summary
Constructors Constructor Description TileImprovement(Game game, java.lang.String id)
Create a newTileImprovement
with the given identifier.TileImprovement(Game game, Tile tile, TileImprovementType type, TileImprovementStyle style)
Creates a standardTileImprovement
-instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
applyBonus(GoodsType goodsType, UnitType unitType, int potential)
Applies the production bonus for the given goods type and unit type to the given potential production.boolean
canProduce(GoodsType goodsType, UnitType unitType)
Does this tile item allow its enclosing tile to produce a given goods type?Constants.IntegrityType
checkIntegrity(boolean fix, LogBuilder lb)
Checks the integrity of this game object.<T extends FreeColObject>
booleancopyIn(T other)
Copy another FreeColObject into this one if it is compatible.TileType
getChange(TileType tileType)
What type of tile does this improvement change a given type to?java.util.List<Direction>
getConnectionDirections()
Gets the directions that a connection can form across for this this type of improvement.java.util.Map<Direction,java.lang.Integer>
getConnections()
Gets a map of connection-direction to magnitude.long
getConnectionsFromStyle()
Updates the connections from the current style.java.util.stream.Stream<RandomChoice<Disaster>>
getDisasterChoices()
Get the disaster choices available for this tile improvement.Map.Layer
getLayer()
Get the layer associated with this tile item.int
getMagnitude()
Gets the magnitude of this improvement.int
getMoveCost(Direction direction, int moveCost)
Calculates the movement cost on the basis of connected tile improvements.java.lang.String
getNameKey()
Gets the name of this named object.private Modifier
getProductionModifier(GoodsType goodsType)
Gets a Modifier for the production bonus this improvement provides for a given type of goods.java.util.stream.Stream<Modifier>
getProductionModifiers(GoodsType goodsType, UnitType unitType)
Gets the production modifiers for the given type of goods and unit.int
getRiverConnection(Direction direction)
Get the magnitude of the river branch in the given direction.TileImprovementStyle
getStyle()
Gets the style of this improvement.int
getTurnsToComplete()
How many turns remain until this improvement is complete?TileImprovementType
getType()
Gets the type of this tile improvement.boolean
getVirtual()
Is this a virtual improvement?java.lang.String
getXMLTagName()
Get the serialization tag for this object.int
getZIndex()
Get theZIndex
value.boolean
isComplete()
Is this improvement complete?boolean
isConnectedTo(Direction direction)
Is this tile improvement connected to a similar improvement on a neighbouring tile? Public for the test suite.boolean
isNatural()
Is this a natural TileItem?boolean
isRiver()
Is this tile improvement a river?boolean
isRoad()
Is this tile improvement a road?boolean
isTileTypeAllowed(TileType tileType)
Is a tile type compatible with this tile item?boolean
isWorkerAllowed(Unit unit)
Can a unit build this improvement?protected void
readAttributes(FreeColXMLReader xr)
Reads the attributes of this object from an XML stream.private void
setConnected(Direction direction, boolean value)
Internal helper method to set the connection status in a given direction.private void
setConnected(Direction direction, boolean value, java.lang.String magnitude)
void
setMagnitude(int magnitude)
Sets the magnitude of this improvement.void
setRiverStyle(java.lang.String conns)
Sets the river style to be as close as possible to the requested style, even when it has to create neighbouring rivers to prevent broken connections or change the magnitude.void
setStyle(TileImprovementStyle style)
Set the style of this improvement.void
setTurnsToComplete(int turns)
Sets the turns required to complete the improvement.void
setVirtual(boolean virtual)
Set the virtual status of this improvement.java.lang.String
toString()
java.lang.String
updateRiverConnections(java.lang.String conns)
Updates the connections from/to this river improvement on the basis of the expected encoded river style, as long as this would not create broken connections.java.lang.String
updateRoadConnections(boolean connect)
Updates the connections from/to this road improvement.protected void
writeAttributes(FreeColXMLWriter xw)
Write the attributes of this object to a stream.-
Methods inherited from class net.sf.freecol.common.model.TileItem
getLocation, getSpaceTaken, getTile, isInEurope, setLocation
-
Methods inherited from class net.sf.freecol.common.model.FreeColGameObject
dispose, disposeResources, equals, fundamentalDispose, getClassIndex, getDisposables, getGame, getLinkTarget, getSpecification, hashCode, intern, internId, isDisposed, isInitialized, isInternable, readFromXML, setGame, setSpecification
-
Methods inherited from class net.sf.freecol.common.model.FreeColObject
addAbility, addFeatures, addModifier, addPropertyChangeListener, addPropertyChangeListener, apply, apply, applyModifiers, applyModifiers, arrayKey, compareIds, compareTo, containsAbilityKey, containsModifierKey, copy, copy, copy, copy, copyInCast, dumpObject, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAbilities, getAbilities, getAbilities, getAbilities, getDefenceModifiers, getDisplayObject, getFeatureContainer, getFreeColObjectClass, getFreeColObjectClassByName, getId, getIdNumber, getIdType, getIdTypeByName, getModifiers, getModifiers, getModifiers, getModifiers, getObjectClassIndex, getPropertyChangeListeners, getPropertyChangeListeners, getPropertyChangeSupport, getSortedAbilities, getSortedModifiers, getSuffix, getSuffix, hasAbility, hasAbility, hasAbility, hasListeners, hasModifier, hasModifier, hasModifier, idEquals, invokeMethod, logFreeColObjects, readChild, readChildren, readFromXMLPartial, removeAbilities, removeAbility, removeFeatures, removeModifier, removeModifiers, removePropertyChangeListener, removePropertyChangeListener, save, save, save, save, serialize, serialize, serialize, serialize, setId, toXML, toXML, toXMLPartial, toXMLPartial, writeChildren
-
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
TAG
public static final java.lang.String TAG
- See Also:
- Constant Field Values
-
EMPTY_RIVER_STYLE
public static final java.lang.String EMPTY_RIVER_STYLE
- See Also:
- Constant Field Values
-
EMPTY_ROAD_STYLE
public static final java.lang.String EMPTY_ROAD_STYLE
- See Also:
- Constant Field Values
-
NO_RIVER
public static final int NO_RIVER
River magnitudes- See Also:
- Constant Field Values
-
SMALL_RIVER
public static final int SMALL_RIVER
- See Also:
- Constant Field Values
-
LARGE_RIVER
public static final int LARGE_RIVER
- See Also:
- Constant Field Values
-
FJORD_RIVER
public static final int FJORD_RIVER
- See Also:
- Constant Field Values
-
type
private TileImprovementType type
The type of this improvement.
-
turnsToComplete
private int turnsToComplete
Turns remaining until the improvement is complete, if any.
-
magnitude
private int magnitude
The improvement magnitude. Default is type.getMagnitude(), but this will override.
-
style
private TileImprovementStyle style
Image and overlay style information for the improvement.
-
virtual
private boolean virtual
Whether this is a virtual improvement granted by some structure on the tile (a Colony, for example). Virtual improvements will be removed along with the structure that granted them.
-
MAGNITUDE_TAG
private static final java.lang.String MAGNITUDE_TAG
- See Also:
- Constant Field Values
-
STYLE_TAG
private static final java.lang.String STYLE_TAG
- See Also:
- Constant Field Values
-
TILE_TAG
private static final java.lang.String TILE_TAG
- See Also:
- Constant Field Values
-
TURNS_TAG
private static final java.lang.String TURNS_TAG
- See Also:
- Constant Field Values
-
TYPE_TAG
private static final java.lang.String TYPE_TAG
- See Also:
- Constant Field Values
-
VIRTUAL_TAG
private static final java.lang.String VIRTUAL_TAG
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
TileImprovement
public TileImprovement(Game game, Tile tile, TileImprovementType type, TileImprovementStyle style)
Creates a standardTileImprovement
-instance. This constructor asserts that the game, tile and type are valid. Does not set the style.- Parameters:
game
- The enclosingGame
.tile
- TheTile
on which this object sits.type
- TheTileImprovementType
of this improvement.style
- TheTileImprovementStyle
of this improvement.
-
TileImprovement
public TileImprovement(Game game, java.lang.String id)
Create a newTileImprovement
with the given identifier. The object should be initialized later.- Parameters:
game
- The enclosingGame
.id
- The object identifier.
-
-
Method Detail
-
getType
public TileImprovementType getType()
Gets the type of this tile improvement.- Returns:
- The type of this improvement.
-
isRiver
public boolean isRiver()
Is this tile improvement a river?- Returns:
- True if this is a river improvement.
-
isRoad
public boolean isRoad()
Is this tile improvement a road?- Returns:
- True if this is a road improvement.
-
getConnectionDirections
public java.util.List<Direction> getConnectionDirections()
Gets the directions that a connection can form across for this this type of improvement. - For rivers, it is just the longSided directions. - For roads, it is all directions. - In other cases, no directions are relevant.- Returns:
- An array of relevant directions, or null if none.
-
getTurnsToComplete
public int getTurnsToComplete()
How many turns remain until this improvement is complete?- Returns:
- The current turns to completion.
-
setTurnsToComplete
public void setTurnsToComplete(int turns)
Sets the turns required to complete the improvement.- Parameters:
turns
- The new turns to completion.
-
getMagnitude
public int getMagnitude()
Gets the magnitude of this improvement.- Returns:
- The magnitude of this immprovement.
-
setMagnitude
public void setMagnitude(int magnitude)
Sets the magnitude of this improvement.- Parameters:
magnitude
- The new magnitude.
-
getStyle
public TileImprovementStyle getStyle()
Gets the style of this improvement.- Returns:
- The style
-
setStyle
public void setStyle(TileImprovementStyle style)
Set the style of this improvement.- Parameters:
style
- The newTileImprovementStyle
.
-
getVirtual
public final boolean getVirtual()
Is this a virtual improvement?- Returns:
- True if this is a virtual improvement.
-
setVirtual
public final void setVirtual(boolean virtual)
Set the virtual status of this improvement. Used for the roads in a colony center tile.- Parameters:
virtual
- The new virtual value.
-
getRiverConnection
public int getRiverConnection(Direction direction)
Get the magnitude of the river branch in the given direction. Precondition: This tile improvement is a river!- Parameters:
direction
- TheDirection
to check.- Returns:
- The magnitude of the river branch or 0 if there is none.
-
isConnectedTo
public boolean isConnectedTo(Direction direction)
Is this tile improvement connected to a similar improvement on a neighbouring tile? Public for the test suite.- Parameters:
direction
- TheDirection
to check.- Returns:
- True if this improvement is connected.
-
setConnected
private void setConnected(Direction direction, boolean value)
Internal helper method to set the connection status in a given direction. There is no check for backwards connections on neighbouring tiles!- Parameters:
direction
- TheDirection
to set.value
- The new status for the connection.
-
setConnected
private void setConnected(Direction direction, boolean value, java.lang.String magnitude)
-
getConnections
public java.util.Map<Direction,java.lang.Integer> getConnections()
Gets a map of connection-direction to magnitude.- Returns:
- A map of the connections.
-
getProductionModifier
private Modifier getProductionModifier(GoodsType goodsType)
Gets a Modifier for the production bonus this improvement provides for a given type of goods.- Parameters:
goodsType
- TheGoodsType
to test.- Returns:
- A production
Modifier
, or null if none applicable.
-
getMoveCost
public int getMoveCost(Direction direction, int moveCost)
Calculates the movement cost on the basis of connected tile improvements.- Parameters:
direction
- TheDirection
to move.moveCost
- The original movement cost.- Returns:
- The movement cost with this improvement.
-
getChange
public TileType getChange(TileType tileType)
What type of tile does this improvement change a given type to?- Parameters:
tileType
- The originalTileType
.- Returns:
- The
TileType
that results from completing this improvement, or null if nothing changes.
-
isWorkerAllowed
public boolean isWorkerAllowed(Unit unit)
Can a unit build this improvement?- Parameters:
unit
- AUnit
to do the building.- Returns:
- True if the supplied unit can build this improvement.
-
getConnectionsFromStyle
public final long getConnectionsFromStyle()
Updates the connections from the current style. Public for the test suite.- Returns:
- The connections implied by the current style.
-
setRiverStyle
public void setRiverStyle(java.lang.String conns)
Sets the river style to be as close as possible to the requested style, even when it has to create neighbouring rivers to prevent broken connections or change the magnitude.- Parameters:
conns
- The river style to set.
-
updateRiverConnections
public java.lang.String updateRiverConnections(java.lang.String conns)
Updates the connections from/to this river improvement on the basis of the expected encoded river style, as long as this would not create broken connections. Uses magnitude, not the connection strengths inside conns, when adding new connections.- Parameters:
conns
- The encoded river connections, or null to disconnect.- Returns:
- The actual encoded connections found.
-
updateRoadConnections
public java.lang.String updateRoadConnections(boolean connect)
Updates the connections from/to this road improvement.- Parameters:
connect
- If true, add connections, otherwise remove them.- Returns:
- A string encoding of the correct connections for this improvement.
-
getDisasterChoices
public java.util.stream.Stream<RandomChoice<Disaster>> getDisasterChoices()
Get the disaster choices available for this tile improvement.- Returns:
- A stream of
Disaster
choices.
-
getNameKey
public java.lang.String getNameKey()
Gets the name of this named object. Try to avoid using this directly except in i18n-related routines or to implement itself in more complext objects.- Specified by:
getNameKey
in interfaceNamed
- Specified by:
getNameKey
in classTileItem
- Returns:
- The name of the
Named
.
-
getZIndex
public final int getZIndex()
Get theZIndex
value.
-
isTileTypeAllowed
public boolean isTileTypeAllowed(TileType tileType)
Is a tile type compatible with this tile item?- Specified by:
isTileTypeAllowed
in classTileItem
- Parameters:
tileType
- TheTileType
to check.- Returns:
- True if the tile type is compatible.
-
applyBonus
public int applyBonus(GoodsType goodsType, UnitType unitType, int potential)
Applies the production bonus for the given goods type and unit type to the given potential production.- Specified by:
applyBonus
in classTileItem
- Parameters:
goodsType
- TheGoodsType
to produce.unitType
- TheUnitType
that is to work.potential
- The base potential production.- Returns:
- The production with resource bonuses.
-
canProduce
public boolean canProduce(GoodsType goodsType, UnitType unitType)
Does this tile item allow its enclosing tile to produce a given goods type?- Specified by:
canProduce
in classTileItem
- Parameters:
goodsType
- TheGoodsType
to produce.unitType
- The optionalunitType
to produce with.- Returns:
- True if this tile item produces the goods.
-
getProductionModifiers
public java.util.stream.Stream<Modifier> getProductionModifiers(GoodsType goodsType, UnitType unitType)
Gets the production modifiers for the given type of goods and unit.- Specified by:
getProductionModifiers
in classTileItem
- Parameters:
goodsType
- TheGoodsType
to produce.unitType
- The optionalunitType
to produce them.- Returns:
- A stream of the applicable modifiers.
-
isNatural
public boolean isNatural()
Is this a natural TileItem?
-
isComplete
public boolean isComplete()
Is this improvement complete?- Specified by:
isComplete
in classTileItem
- Returns:
- True if complete.
-
getLayer
public Map.Layer getLayer()
Get the layer associated with this tile item.
-
checkIntegrity
public Constants.IntegrityType checkIntegrity(boolean fix, LogBuilder lb)
Checks the integrity of this game object.- Overrides:
checkIntegrity
in classTileItem
- Parameters:
fix
- If true, fix problems if possible.lb
- ALogBuilder
to log to.- Returns:
- A suitable
IntegrityType
.
-
copyIn
public <T extends FreeColObject> boolean copyIn(T other)
Copy another FreeColObject into this one if it is compatible.
-
writeAttributes
protected void writeAttributes(FreeColXMLWriter xw) throws javax.xml.stream.XMLStreamException
Write the attributes of this object to a stream. To be overridden if required by any object that has attributes and uses the toXML(FreeColXMLWriter, String) call.- Overrides:
writeAttributes
in classFreeColObject
- Parameters:
xw
- TheFreeColXMLWriter
to write to.- Throws:
javax.xml.stream.XMLStreamException
- if there are any problems writing to the stream.
-
readAttributes
protected void readAttributes(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Reads the attributes of this object from an XML stream.- Overrides:
readAttributes
in classFreeColGameObject
- Parameters:
xr
- TheFreeColXMLReader
to read from.- Throws:
javax.xml.stream.XMLStreamException
- if there is a problem reading the stream.
-
getXMLTagName
public java.lang.String getXMLTagName()
Get the serialization tag for this object.- Specified by:
getXMLTagName
in classFreeColObject
- Returns:
- The tag.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classFreeColObject
-
-