net.sf.freecol.common.model
Class Colony

java.lang.Object
  extended by net.sf.freecol.common.model.FreeColGameObject
      extended by net.sf.freecol.common.model.Settlement
          extended by net.sf.freecol.common.model.Colony
All Implemented Interfaces:
Location, Nameable, Ownable

public final class Colony
extends Settlement
implements Location, Nameable

Represents a colony. A colony contains Buildings and ColonyTiles. The latter represents the tiles around the Colony where working is possible.


Field Summary
static int BUILDING_UNIT_ADDITION
           
static java.lang.String COPYRIGHT
           
static java.lang.String LICENSE
           
static java.lang.String REVISION
           
 
Fields inherited from class net.sf.freecol.common.model.Settlement
goodsContainer, owner, RADIUS, tile
 
Constructor Summary
Colony(Game game, org.w3c.dom.Element e)
          Initiates a new Colony from an XML representation.
Colony(Game game, Player owner, java.lang.String name, Tile tile)
          Creates a new Colony.
Colony(Game game, java.lang.String id)
          Initiates a new Colony with the given ID.
Colony(Game game, javax.xml.stream.XMLStreamReader in)
          Initiates a new Colony from an XML representation.
 
Method Summary
 void add(Locatable locatable)
          Adds a Locatable to this Location.
 void addBells(int amount)
          Adds to the bell count of the colony.
 void addGoods(int type, int amount)
           
 void addHammers(int amount)
          Adds to the hammer count of the colony.
 void addSoL(int amount)
          Adds to the bell count of the colony.
 void bombard(Unit defender, int result)
          Bombard a unit with the given outcome.
 boolean canAdd(Locatable locatable)
          Checks wether or not the specified locatable may be added to this Location.
 boolean canBuildUnit(int unitType)
          Checks if this colony may build the given unit type.
 boolean contains(Locatable locatable)
          Checks if this Location contains the specified Locatable.
 void damageAllShips()
          Damages all ship located on this Colony's Tile.
 void dispose()
          Disposes this Colony.
 int getBells()
          Returns the bell count of the colony.
 java.util.Iterator<java.lang.Integer> getBuildableUnitIterator()
          Returns an Iterator of every unit type this colony may build.
 Building getBuilding(int type)
          Gets a Building of the specified type.
 Building getBuildingForConsuming(int goodsType)
          Returns the colony's existing building for the given goods type.
 Building getBuildingForProducing(int goodsType)
          Returns the building for producing the given type of goods.
 java.util.Iterator<Building> getBuildingIterator()
          Gets an Iterator of every Building in this Colony.
 ColonyTile getColonyTile(int x, int y)
          Gets the specified ColonyTile.
 ColonyTile getColonyTile(Tile t)
          Returns the ColonyTile matching the given Tile.
 java.util.Iterator<ColonyTile> getColonyTileIterator()
          Gets an Iterator of every ColonyTile in this Colony.
 java.util.Iterator<Goods> getCompactGoodsIterator()
          Gets an Iterator of every Goods in this Colony.
 int getCurrentlyBuilding()
          Returns the type of building currently being built.
 Unit getDefendingUnit(Unit attacker)
          Gets the Unit that is currently defending this Colony.
 int[] getExportLevel()
          Get the ExportLevel value.
 boolean getExports(Goods goods)
          Returns true if the custom house should export these goods.
 boolean getExports(int type)
          Returns true if the custom house should export this type of goods.
 int getFoodConsumption()
          Gives the food needed to keep all current colonists alive in this colony.
 int getFoodProduction()
          Gets the production of food.
 int getGoodsCount(int type)
          Gets the amount of one type of Goods at this Colony.
 java.util.Iterator<Goods> getGoodsIterator()
           
 int getHammers()
          Returns the hammer count of the colony.
 int[] getHighLevel()
          Get the HighLevel value.
 int getHorseProduction()
          Gets the production of horses in this Colony.
 java.lang.String getLocationName()
          Returns the name of this location.
 int[] getLowLevel()
          Get the LowLevel value.
 int getMembers()
          Return the number of sons of liberty
 java.lang.String getName()
          Gets the name of this Colony.
 int getPotentialHorseProduction()
          Returns the horse production (given that enough food is being produced and a sufficient storage capacity).
 int getPriceForBuilding()
          Returns the price for the remaining hammers and tools for the Building that is currently being built.
 int getProductionBonus()
          Returns the production bonus, if any, of the colony.
 int getProductionNetOf(int goodsType)
          Returns how much of a Good will be produced by this colony this turn, taking into account how much is consumed - by workers, horses, etc.
 int getProductionOf(int goodsType)
          Returns the production of the given type of goods.
 Unit getRandomUnit()
          Returns a random unit from this colony.
 int getSoL()
          Returns the current SoL membership of the colony.
 int getTory()
          Returns the Tory membership of the colony.
 int getUnitCount()
          Gets the amount of Units at this Location.
 java.util.Iterator<Unit> getUnitIterator()
          Gets a Iterator of every Unit directly located on this Location.
 java.util.List<Unit> getUnitList()
          Returns a list containing all the Units present at this Location.
 ColonyTile getVacantColonyTileFor(Unit unit, int goodsType)
          Returns a vacant ColonyTile where the given unit produces the maximum output of the given goodsType.
 int getVacantColonyTileProductionFor(Unit unit, int goodsType)
          Returns the production of a vacant ColonyTile where the given unit produces the maximum output of the given goodsType.
 WorkLocation getVacantWorkLocationFor(Unit locatable)
          Gets a vacant WorkLocation for the given Unit.
 int getWarehouseCapacity()
          Returns the capacity of this colony's warehouse.
 java.util.Iterator<WorkLocation> getWorkLocationIterator()
          Gets an Iterator of every location in this Colony where a Unit can work.
static java.lang.String getXMLElementTagName()
          Gets the tag name of the root element representing this object.
 boolean isLandLocked()
          Returns whether this colony is landlocked, or has access to the ocean.
 boolean isUndead()
          Returns whether this colony has undead units.
 void newTurn()
          Prepares this Colony for a new turn.
 void payForBuilding()
          Buys the remaining hammers and tools for the Building that is currently being built.
protected  void readFromXMLImpl(javax.xml.stream.XMLStreamReader in)
          Initialize this object from an XML-representation of this object.
 void remove(Locatable locatable)
          Removes a Locatable from this Location.
 void removeGoods(Goods goods)
           
 void removeGoods(int type, int amount)
          Removes a specified amount of a type of Goods from this containter.
 void setCurrentlyBuilding(int type)
          Sets the type of building to be built.
 void setExportLevel(int[] newExportLevel)
          Set the ExportLevel value.
 void setExports(Goods goods, boolean value)
          Sets whether the custom house should export these goods.
 void setExports(int type, boolean value)
          Sets whether the custom house should export these goods.
 void setHighLevel(int[] newHighLevel)
          Set the HighLevel value.
 void setLowLevel(int[] newLowLevel)
          Set the LowLevel value.
 void setName(java.lang.String newName)
          Sets the name of this Colony.
 void setOwner(Player owner)
          Sets the owner of this Colony, including all units within, and change main tile nation ownership.
 void setUnitCount(int unitCount)
          Sets the number of units inside the colony, used in enemy colonies
 void stopBuilding()
          Sets the type of building to None, so no building is done.
 java.lang.String toString()
          Gets a string representation of the Colony.
protected  void toXMLImpl(javax.xml.stream.XMLStreamWriter out, Player player, boolean showAll, boolean toSavedGame)
          This method writes an XML-representation of this object to the given stream.
 void updatePopulation()
          Updates SoL and builds stockade if possible.
 
Methods inherited from class net.sf.freecol.common.model.Settlement
getGoodsContainer, getLineOfSight, getOwner, getRadius, getTile, getTile
 
Methods inherited from class net.sf.freecol.common.model.FreeColGameObject
addModelMessage, addModelMessage, equals, equals, getGame, getGameOptions, getID, getIntegerID, hashCode, hasID, isDisposed, isUninitialized, readFromArrayElement, readFromArrayElement, readFromArrayElement, readFromArrayElement, readFromXML, readFromXMLElement, setFakeID, setGame, setID, toArrayElement, toArrayElement, toArrayElement, toArrayElement, toSavedXML, toXML, toXML, toXML, toXMLElement, toXMLElement, updateID
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface net.sf.freecol.common.model.Location
getGoodsContainer, getID, getTile
 

Field Detail

COPYRIGHT

public static final java.lang.String COPYRIGHT
See Also:
Constant Field Values

LICENSE

public static final java.lang.String LICENSE
See Also:
Constant Field Values

REVISION

public static final java.lang.String REVISION
See Also:
Constant Field Values

BUILDING_UNIT_ADDITION

public static final int BUILDING_UNIT_ADDITION
See Also:
Constant Field Values
Constructor Detail

Colony

public Colony(Game game,
              Player owner,
              java.lang.String name,
              Tile tile)
Creates a new Colony.

Parameters:
game - The Game in which this object belongs.
owner - The Player owning this Colony.
name - The name of the new Colony.
tile - The location of the Colony.

Colony

public Colony(Game game,
              javax.xml.stream.XMLStreamReader in)
       throws javax.xml.stream.XMLStreamException
Initiates a new Colony from an XML representation.

Parameters:
game - The Game this object belongs to.
in - The input stream containing the XML.
Throws:
javax.xml.stream.XMLStreamException - if an error occured during parsing.

Colony

public Colony(Game game,
              org.w3c.dom.Element e)
Initiates a new Colony from an XML representation.

Parameters:
game - The Game this object belongs to.
e - An XML-element that will be used to initialize this object.

Colony

public Colony(Game game,
              java.lang.String id)
Initiates a new Colony with the given ID. The object should later be initialized by calling either FreeColGameObject.readFromXML(XMLStreamReader) or FreeColGameObject.readFromXMLElement(Element).

Parameters:
game - The Game in which this object belong.
id - The unique identifier for this object.
Method Detail

updatePopulation

public void updatePopulation()
Updates SoL and builds stockade if possible.


getHighLevel

public int[] getHighLevel()
Get the HighLevel value.

Returns:
an int[] value

setHighLevel

public void setHighLevel(int[] newHighLevel)
Set the HighLevel value.

Parameters:
newHighLevel - The new HighLevel value.

getLowLevel

public int[] getLowLevel()
Get the LowLevel value.

Returns:
an int[] value

setLowLevel

public void setLowLevel(int[] newLowLevel)
Set the LowLevel value.

Parameters:
newLowLevel - The new LowLevel value.

getExportLevel

public int[] getExportLevel()
Get the ExportLevel value.

Returns:
an int[] value

setExportLevel

public void setExportLevel(int[] newExportLevel)
Set the ExportLevel value.

Parameters:
newExportLevel - The new ExportLevel value.

isLandLocked

public boolean isLandLocked()
Returns whether this colony is landlocked, or has access to the ocean.

Returns:
true if there are no adjacent tiles to this Colony's tile being ocean tiles.

isUndead

public boolean isUndead()
Returns whether this colony has undead units.

Returns:
whether this colony has undead units.

setOwner

public void setOwner(Player owner)
Sets the owner of this Colony, including all units within, and change main tile nation ownership.

Specified by:
setOwner in interface Ownable
Overrides:
setOwner in class Settlement
Parameters:
owner - The Player that shall own this Settlement.
See Also:
Settlement.getOwner()

setUnitCount

public void setUnitCount(int unitCount)
Sets the number of units inside the colony, used in enemy colonies

Parameters:
unitCount - The units inside the colony
See Also:
getUnitCount()

damageAllShips

public void damageAllShips()
Damages all ship located on this Colony's Tile. That is: they are sent to the closest location for repair.

See Also:
Unit.shipDamaged()

getBuildingForProducing

public Building getBuildingForProducing(int goodsType)
Returns the building for producing the given type of goods.

Parameters:
goodsType - The type of goods.
Returns:
The Building which produces the given type of goods, or null if such a building cannot be found.

getBuildingForConsuming

public Building getBuildingForConsuming(int goodsType)
Returns the colony's existing building for the given goods type.

Parameters:
goodsType - The goods type.
Returns:
The Building for the goodsType, or null if not exists or not fully built.
See Also:
Goods

getWorkLocationIterator

public java.util.Iterator<WorkLocation> getWorkLocationIterator()
Gets an Iterator of every location in this Colony where a Unit can work.

Returns:
The Iterator.
See Also:
WorkLocation

getBuildingIterator

public java.util.Iterator<Building> getBuildingIterator()
Gets an Iterator of every Building in this Colony.

Returns:
The Iterator.
See Also:
Building

getColonyTileIterator

public java.util.Iterator<ColonyTile> getColonyTileIterator()
Gets an Iterator of every ColonyTile in this Colony.

Returns:
The Iterator.
See Also:
ColonyTile

getBuilding

public Building getBuilding(int type)
Gets a Building of the specified type.

Parameters:
type - The type of building to get.
Returns:
The Building.

getColonyTile

public ColonyTile getColonyTile(int x,
                                int y)
Gets the specified ColonyTile.

Parameters:
x - The x-coordinate of the Tile.
y - The y-coordinate of the Tile.
Returns:
The ColonyTile for the Tile returned by Settlement.getTile(int, int).

getColonyTile

public ColonyTile getColonyTile(Tile t)
Returns the ColonyTile matching the given Tile.

Parameters:
t - The Tile to get the ColonyTile for.
Returns:
The ColonyTile

getVacantWorkLocationFor

public WorkLocation getVacantWorkLocationFor(Unit locatable)
Gets a vacant WorkLocation for the given Unit.

Parameters:
locatable - The Unit
Returns:
A vacant WorkLocation for the given Unit or null if there is no such location.

add

public void add(Locatable locatable)
Adds a Locatable to this Location.

Specified by:
add in interface Location
Specified by:
add in class Settlement
Parameters:
locatable - The Locatable to add to this Location.

remove

public void remove(Locatable locatable)
Removes a Locatable from this Location.

Specified by:
remove in interface Location
Specified by:
remove in class Settlement
Parameters:
locatable - The Locatable to remove from this Location.

getUnitCount

public int getUnitCount()
Gets the amount of Units at this Location. These units are located in a WorkLocation in this Colony.

Specified by:
getUnitCount in interface Location
Specified by:
getUnitCount in class Settlement
Returns:
The amount of Units at this Location.

getFoodConsumption

public int getFoodConsumption()
Gives the food needed to keep all current colonists alive in this colony.

Returns:
The amount of food eaten in this colony each this turn.

getGoodsCount

public int getGoodsCount(int type)
Gets the amount of one type of Goods at this Colony.

Parameters:
type - The type of goods to look for.
Returns:
The amount of this type of Goods at this Location.

removeGoods

public void removeGoods(int type,
                        int amount)
Removes a specified amount of a type of Goods from this containter.

Parameters:
type - The type of Goods to remove from this container.
amount - The amount of Goods to remove from this container.

removeGoods

public void removeGoods(Goods goods)

addGoods

public void addGoods(int type,
                     int amount)

getUnitList

public java.util.List<Unit> getUnitList()
Description copied from interface: Location
Returns a list containing all the Units present at this Location. The list is a cloned list, so no risk.

Specified by:
getUnitList in interface Location
Returns:
a list containing the Units present at this location.

getUnitIterator

public java.util.Iterator<Unit> getUnitIterator()
Description copied from interface: Location
Gets a Iterator of every Unit directly located on this Location.

Specified by:
getUnitIterator in interface Location
Returns:
The Iterator.

getGoodsIterator

public java.util.Iterator<Goods> getGoodsIterator()

getCompactGoodsIterator

public java.util.Iterator<Goods> getCompactGoodsIterator()
Gets an Iterator of every Goods in this Colony. There is only one Goods for each type of goods.

Returns:
The Iterator.

getExports

public boolean getExports(int type)
Returns true if the custom house should export this type of goods.

Parameters:
type - The type of goods.
Returns:
True if the custom house should export this type of goods.

getExports

public boolean getExports(Goods goods)
Returns true if the custom house should export these goods.

Parameters:
goods - The goods.
Returns:
True if the custom house should export these goods.

setExports

public void setExports(int type,
                       boolean value)
Sets whether the custom house should export these goods.

Parameters:
type - the type of goods.
value - a boolean value

setExports

public void setExports(Goods goods,
                       boolean value)
Sets whether the custom house should export these goods.

Parameters:
goods - the goods.
value - a boolean value

contains

public boolean contains(Locatable locatable)
Description copied from interface: Location
Checks if this Location contains the specified Locatable.

Specified by:
contains in interface Location
Specified by:
contains in class Settlement
Parameters:
locatable - The Locatable to test the presence of.
Returns:
  • true if the specified Locatable is on this Location and
  • false otherwise.

canAdd

public boolean canAdd(Locatable locatable)
Description copied from interface: Location
Checks wether or not the specified locatable may be added to this Location.

Specified by:
canAdd in interface Location
Specified by:
canAdd in class Settlement
Parameters:
locatable - The Locatable to add.
Returns:
The result.

getDefendingUnit

public Unit getDefendingUnit(Unit attacker)
Gets the Unit that is currently defending this Colony.

Note! Several callers fail to handle null as a return value. Return an arbitrary unarmed land unit unless Paul Revere is present as founding father, in which case the unit can be armed as well. Also note that the colony would typically be defended by a unit outside it.

Specified by:
getDefendingUnit in class Settlement
Parameters:
attacker - The target that would be attacking this colony.
Returns:
The Unit that has been choosen to defend this colony.
See Also:
Tile.getDefendingUnit(Unit)

addHammers

public void addHammers(int amount)
Adds to the hammer count of the colony.

Parameters:
amount - The number of hammers to add.

getBuildableUnitIterator

public java.util.Iterator<java.lang.Integer> getBuildableUnitIterator()
Returns an Iterator of every unit type this colony may build.

Returns:
An Iterator on Integer -objects where the values are the unit type values.

canBuildUnit

public boolean canBuildUnit(int unitType)
Checks if this colony may build the given unit type.

Parameters:
unitType - The unit type to test against.
Returns:
The result.

getHammers

public int getHammers()
Returns the hammer count of the colony.

Returns:
The current hammer count of the colony.

getCurrentlyBuilding

public int getCurrentlyBuilding()
Returns the type of building currently being built.

Returns:
The type of building currently being built.

setCurrentlyBuilding

public void setCurrentlyBuilding(int type)
Sets the type of building to be built.

Parameters:
type - The type of building to be built.

stopBuilding

public void stopBuilding()
Sets the type of building to None, so no building is done.


addBells

public void addBells(int amount)
Adds to the bell count of the colony.

Parameters:
amount - The number of bells to add.

addSoL

public void addSoL(int amount)
Adds to the bell count of the colony.

Parameters:
amount - The percentage of SoL to add.

getBells

public int getBells()
Returns the bell count of the colony.

Returns:
The current bell count of the colony.

getSoL

public int getSoL()
Returns the current SoL membership of the colony.

Returns:
The current SoL membership of the colony.

getMembers

public int getMembers()
Return the number of sons of liberty


getTory

public int getTory()
Returns the Tory membership of the colony.

Returns:
The current Tory membership of the colony.

getProductionBonus

public int getProductionBonus()
Returns the production bonus, if any, of the colony.

Returns:
The current production bonus of the colony.

toString

public java.lang.String toString()
Gets a string representation of the Colony. Currently this method just returns the name of the Colony, but that may change later.

Overrides:
toString in class FreeColGameObject
Returns:
The name of the colony.
See Also:
getName()

getName

public java.lang.String getName()
Gets the name of this Colony.

Specified by:
getName in interface Nameable
Returns:
The name as a String.

setName

public void setName(java.lang.String newName)
Sets the name of this Colony.

Specified by:
setName in interface Nameable
Parameters:
newName - The new name of this Colony.

getLocationName

public java.lang.String getLocationName()
Returns the name of this location.

Specified by:
getLocationName in interface Location
Returns:
The name of this location.

getFoodProduction

public int getFoodProduction()
Gets the production of food.

Returns:
The same as getProductionOf(Goods.FOOD).

getProductionOf

public int getProductionOf(int goodsType)
Returns the production of the given type of goods.

Parameters:
goodsType - The type of goods to get the production for.
Returns:
The production of the given type of goods the current turn by all of the Colony's buildings and tiles.

getVacantColonyTileFor

public ColonyTile getVacantColonyTileFor(Unit unit,
                                         int goodsType)
Returns a vacant ColonyTile where the given unit produces the maximum output of the given goodsType.

Parameters:
unit - The Unit to find a vacant ColonyTile for.
goodsType - The type of goods that should be produced.
Returns:
The ColonyTile giving the highest production of the given goods for the given unit or null if there is no available ColonyTile for producing that goods.

getVacantColonyTileProductionFor

public int getVacantColonyTileProductionFor(Unit unit,
                                            int goodsType)
Returns the production of a vacant ColonyTile where the given unit produces the maximum output of the given goodsType.

Parameters:
unit - The Unit to find the highest possible ColonyTile-production for.
goodsType - The type of goods that should be produced.
Returns:
The highest possible production on a vacant ColonyTile for the given goods and the given unit.

getPotentialHorseProduction

public int getPotentialHorseProduction()
Returns the horse production (given that enough food is being produced and a sufficient storage capacity).

Returns:
The number of producable horses.

getHorseProduction

public int getHorseProduction()
Gets the production of horses in this Colony.

Returns:
The total production of horses in this Colony.

getProductionNetOf

public int getProductionNetOf(int goodsType)
Returns how much of a Good will be produced by this colony this turn, taking into account how much is consumed - by workers, horses, etc.

Parameters:
goodsType - The goods' type.
Returns:
The amount of the given goods currently unallocated for next turn.

getPriceForBuilding

public int getPriceForBuilding()
Returns the price for the remaining hammers and tools for the Building that is currently being built.

Returns:
The price.
See Also:
payForBuilding()

payForBuilding

public void payForBuilding()
Buys the remaining hammers and tools for the Building that is currently being built.

Throws:
java.lang.IllegalStateException - If the owner of this Colony has an insufficient amount of gold.
See Also:
getPriceForBuilding()

bombard

public void bombard(Unit defender,
                    int result)
Bombard a unit with the given outcome.

Parameters:
defender - The Unit defending against bombardment.
result - The result of the bombardment.

getRandomUnit

public Unit getRandomUnit()
Returns a random unit from this colony. At this moment, this method always returns the first unit in the colony.

Returns:
A random unit from this Colony. This Unit will either be working in a Building or a ColonyTile.

newTurn

public void newTurn()
Prepares this Colony for a new turn.

Specified by:
newTurn in class Settlement

getWarehouseCapacity

public int getWarehouseCapacity()
Returns the capacity of this colony's warehouse. All goods above this limit, except Goods.FOOD, will be removed when calling newTurn().

Returns:
The capacity of this Colony's warehouse.

dispose

public void dispose()
Disposes this Colony. All WorkLocations owned by this Colony will also be destroyed.

Overrides:
dispose in class Settlement

toXMLImpl

protected void toXMLImpl(javax.xml.stream.XMLStreamWriter out,
                         Player player,
                         boolean showAll,
                         boolean toSavedGame)
                  throws javax.xml.stream.XMLStreamException
This method writes an XML-representation of this object to the given stream.

Only attributes visible to the given Player will be added to that representation if showAll is set to false.

Specified by:
toXMLImpl in class FreeColGameObject
Parameters:
out - The target stream.
player - The Player this XML-representation should be made for, or null if showAll == true.
showAll - Only attributes visible to player will be added to the representation if showAll is set to false.
toSavedGame - If true then information that is only needed when saving a game is added.
Throws:
javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.

readFromXMLImpl

protected void readFromXMLImpl(javax.xml.stream.XMLStreamReader in)
                        throws javax.xml.stream.XMLStreamException
Initialize this object from an XML-representation of this object.

Specified by:
readFromXMLImpl in class FreeColGameObject
Parameters:
in - The input stream with the XML.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.

getXMLElementTagName

public static java.lang.String getXMLElementTagName()
Gets the tag name of the root element representing this object.

Returns:
"colony".