net.sf.freecol.server.ai
Class AIColony

java.lang.Object
  extended by net.sf.freecol.common.model.FreeColObject
      extended by net.sf.freecol.server.ai.AIObject
          extended by net.sf.freecol.server.ai.AIColony
All Implemented Interfaces:
java.beans.PropertyChangeListener, java.util.EventListener

public class AIColony
extends AIObject
implements java.beans.PropertyChangeListener

Objects of this class contains AI-information for a single Colony.


Nested Class Summary
private static class AIColony.ExperienceUpgrade
           
 
Field Summary
private  java.util.ArrayList<AIGoods> aiGoods
           
private  Colony colony
          The FreeColGameObject this AIObject contains AI-information for.
private  ColonyPlan colonyPlan
           
private static java.util.logging.Logger logger
           
private  boolean rearrangeWorkers
          Records whether the workers in this Colony need to be rearranged.
private  java.util.ArrayList<TileImprovementPlan> tileImprovementPlans
           
private  java.util.ArrayList<Wish> wishes
           
 
Fields inherited from class net.sf.freecol.server.ai.AIObject
uninitialized
 
Fields inherited from class net.sf.freecol.common.model.FreeColObject
ARRAY_SIZE, ID_ATTRIBUTE, ID_ATTRIBUTE_TAG, INFINITY, NO_ID, PARTIAL_ATTRIBUTE, UNDEFINED, VALUE_TAG
 
Constructor Summary
AIColony(AIMain aiMain, Colony colony)
          Creates a new AIColony.
AIColony(AIMain aiMain, org.w3c.dom.Element element)
          Creates a new AIColony.
AIColony(AIMain aiMain, java.lang.String id)
          Creates a new AIColony.
AIColony(AIMain aiMain, javax.xml.stream.XMLStreamReader in)
          Creates a new AIColony.
 
Method Summary
 void addGoodsWish(GoodsWish gw)
          Add a GoodsWish to the wish list.
static Unit bestUnitForWorkLocation(java.util.Collection<Unit> units, WorkLocation workLocation, GoodsType goodsType)
           
 boolean canBuildEquipment(EquipmentType equipmentType)
          Returns true if this AIColony can build the given type of equipment.
(package private)  void checkConditionsForHorseBreed()
          Verifies if the Colony has conditions for breeding horses, and un-mounts a mounted Unit if available, to have horses to breed.
private  void checkForUnarmedExpertSoldier()
          Checks if the colony has an unarmed expert soldier inside If there are conditions to arm it, put it outside for later equip
private  void checkForUnequippedExpertPioneer()
           
private  boolean colonyCouldProduce(GoodsType goodsType)
           
 void createAIGoods()
          Creates a list of the goods which should be shipped out of this colony.
private  void createGoodsWishes()
           
 void createTileImprovementPlans()
          Creates a list of the Tile-improvements which will increase the production by this Colony.
private  void createWishes()
          Creates the wishes for the Colony.
private  void createWorkerWishes()
           
private  void decideBuildable(Connection connection)
          Decides what to build in the Colony.
 void dispose()
          Disposes this AIColony.
 java.util.Iterator<AIGoods> getAIGoodsIterator()
          Returns an Iterator of the goods to be shipped from this colony.
protected  AIPlayer getAIOwner()
           
protected  AIUnit getAIUnit(Unit unit)
           
 int getAvailableGoods(GoodsType goodsType)
          Returns the available amount of the GoodsType given.
static UnitType getBestDefender(Colony colony)
          Returns the best defender for the given colony.
private  ColonyTile getBestVacantTile(Unit unit, GoodsType goodsType)
          Find a colony's best tile to put a unit to produce a type of goods.
 Colony getColony()
          Gets the Colony this AIColony controls.
 ColonyPlan getColonyPlan()
           
protected  Connection getConnection()
           
private static AIColony.ExperienceUpgrade getExperienceUpgrade(Unit unit, UnitType expert)
           
private  int getHammersRequired(BuildableType buildableType)
           
private  UnitType getNextExpert(boolean onlyFood)
           
 java.util.Iterator<TileImprovementPlan> getTileImprovementPlanIterator()
          Returns an Iterator over all the TileImprovementPlans needed by this colony.
private  int getToolsRequired(BuildableType buildableType)
           
 java.util.Iterator<Wish> getWishIterator()
          Gets an Iterator for every Wish the Colony has.
 java.util.List<WorkerWish> getWorkerWishes()
           
static java.lang.String getXMLElementTagName()
          Returns the tag name of the root element representing this object.
 boolean isBadlyDefended()
           
private  void placeExpertsInWorkPlaces(java.util.List<Unit> units, java.util.List<WorkLocationPlan> workLocationPlans)
           
 void propertyChange(java.beans.PropertyChangeEvent event)
           
protected  void readFromXMLImpl(javax.xml.stream.XMLStreamReader in)
          Reads information for this object from an XML stream.
 boolean rearrangeWorkers(Connection connection)
          Rearranges the workers within this colony.
 void removeAIGoods(AIGoods ag)
          Removes the given AIGoods from this colony's list.
 boolean removeTileImprovementPlan(TileImprovementPlan plan)
          Removes a TileImprovementPlan from the list
 void removeWish(Wish w)
           
protected  void toXMLImpl(javax.xml.stream.XMLStreamWriter out)
          Writes this object to an XML stream.
private  boolean tryUseTile(Tile tile)
          Try to use a tile.
 
Methods inherited from class net.sf.freecol.server.ai.AIObject
getAIMain, getAIRandom, getGame, isUninitialized, readFromXML
 
Methods inherited from class net.sf.freecol.common.model.FreeColObject
addPropertyChangeListener, addPropertyChangeListener, dumpObject, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAttribute, getAttribute, getAttribute, getAttribute, getAttribute, getId, getPropertyChangeListeners, getPropertyChangeListeners, getSpecification, hasAbility, hasAttribute, hasListeners, readAttributes, readAttributes, readChild, readChild, readChildren, readChildren, readFromArrayElement, readFromArrayElement, readFromListElement, readFromXMLElement, readFromXMLImpl, readFromXMLPartialImpl, removePropertyChangeListener, removePropertyChangeListener, save, save, setId, toXML, toXML, toXML, toXMLElement, toXMLElement, toXMLElement, toXMLElement, toXMLElementPartial, toXMLPartialImpl, writeAttribute, writeAttributes, writeChildren
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

private static final java.util.logging.Logger logger

colony

private Colony colony
The FreeColGameObject this AIObject contains AI-information for.


colonyPlan

private ColonyPlan colonyPlan

aiGoods

private java.util.ArrayList<AIGoods> aiGoods

wishes

private java.util.ArrayList<Wish> wishes

tileImprovementPlans

private java.util.ArrayList<TileImprovementPlan> tileImprovementPlans

rearrangeWorkers

private boolean rearrangeWorkers
Records whether the workers in this Colony need to be rearranged.

Constructor Detail

AIColony

public AIColony(AIMain aiMain,
                Colony colony)
Creates a new AIColony.

Parameters:
aiMain - The main AI-object.
colony - The colony to make an AIObject for.

AIColony

public AIColony(AIMain aiMain,
                org.w3c.dom.Element element)
Creates a new AIColony.

Parameters:
aiMain - The main AI-object.
element - An Element containing an XML-representation of this object.

AIColony

public AIColony(AIMain aiMain,
                javax.xml.stream.XMLStreamReader in)
         throws javax.xml.stream.XMLStreamException
Creates a new AIColony.

Parameters:
aiMain - The main AI-object.
in - The input stream containing the XML.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.

AIColony

public AIColony(AIMain aiMain,
                java.lang.String id)
Creates a new AIColony.

Parameters:
aiMain - The main AI-object.
id -
Method Detail

getAIUnit

protected AIUnit getAIUnit(Unit unit)

getAIOwner

protected AIPlayer getAIOwner()

getConnection

protected Connection getConnection()

getColony

public Colony getColony()
Gets the Colony this AIColony controls.

Returns:
The Colony.

dispose

public void dispose()
Disposes this AIColony.

Overrides:
dispose in class AIObject

getAIGoodsIterator

public java.util.Iterator<AIGoods> getAIGoodsIterator()
Returns an Iterator of the goods to be shipped from this colony. The item with the highest transport priority gets returned first by this Iterator.

Returns:
The Iterator.

getWishIterator

public java.util.Iterator<Wish> getWishIterator()
Gets an Iterator for every Wish the Colony has.

Returns:
The Iterator. The items with the highest value appears first in the Iterator
See Also:
Wish

getWorkerWishes

public java.util.List<WorkerWish> getWorkerWishes()

createTileImprovementPlans

public void createTileImprovementPlans()
Creates a list of the Tile-improvements which will increase the production by this Colony.

See Also:
TileImprovementPlan

getTileImprovementPlanIterator

public java.util.Iterator<TileImprovementPlan> getTileImprovementPlanIterator()
Returns an Iterator over all the TileImprovementPlans needed by this colony.

Returns:
The Iterator.
See Also:
TileImprovementPlan

removeTileImprovementPlan

public boolean removeTileImprovementPlan(TileImprovementPlan plan)
Removes a TileImprovementPlan from the list

Returns:
True if it was successfully deleted, false otherwise

createWishes

private void createWishes()
Creates the wishes for the Colony.


createWorkerWishes

private void createWorkerWishes()

getBestDefender

public static UnitType getBestDefender(Colony colony)
Returns the best defender for the given colony.

Parameters:
colony - a Colony value
Returns:
an UnitType value

createGoodsWishes

private void createGoodsWishes()

colonyCouldProduce

private boolean colonyCouldProduce(GoodsType goodsType)

getNextExpert

private UnitType getNextExpert(boolean onlyFood)

getToolsRequired

private int getToolsRequired(BuildableType buildableType)

getHammersRequired

private int getHammersRequired(BuildableType buildableType)

isBadlyDefended

public boolean isBadlyDefended()

removeWish

public void removeWish(Wish w)

addGoodsWish

public void addGoodsWish(GoodsWish gw)
Add a GoodsWish to the wish list.

Parameters:
gw - The GoodsWish to be added.

removeAIGoods

public void removeAIGoods(AIGoods ag)
Removes the given AIGoods from this colony's list. The AIGoods-object is not disposed as part of this operation. Use that method instead to remove the object completely (this method would then be called indirectly).

Parameters:
ag - The AIGoods to be removed.
See Also:
AIGoods.dispose()

createAIGoods

public void createAIGoods()
Creates a list of the goods which should be shipped out of this colony. This is the list getAIGoodsIterator() returns the Iterator for.


getAvailableGoods

public int getAvailableGoods(GoodsType goodsType)
Returns the available amount of the GoodsType given.

Returns:
The amount of tools not needed for the next thing we are building.

canBuildEquipment

public boolean canBuildEquipment(EquipmentType equipmentType)
Returns true if this AIColony can build the given type of equipment. Unlike the method of the Colony, this takes goods "reserved" for building or breeding purposes into account.

Parameters:
equipmentType - an EquipmentType value
Returns:
a boolean value
See Also:
Settlement.canBuildEquipment(EquipmentType equipmentType)

tryUseTile

private boolean tryUseTile(Tile tile)
Try to use a tile. Steal it if necessary.

Parameters:
tile - The Tile to use.
Returns:
True if the tile can be used.

getBestVacantTile

private ColonyTile getBestVacantTile(Unit unit,
                                     GoodsType goodsType)
Find a colony's best tile to put a unit to produce a type of goods. Steals land from other settlements only when it is free.

Parameters:
unit - The Unit to work the tile.
goodsType - The type of goods to produce.
Returns:
The best choice of available vacant colony tiles, or null if nothing suitable.

rearrangeWorkers

public boolean rearrangeWorkers(Connection connection)
Rearranges the workers within this colony. This is done according to the ColonyPlan, although minor adjustments can be done to increase production.

Parameters:
connection - The Connection to be used when communicating with the server.

checkForUnequippedExpertPioneer

private void checkForUnequippedExpertPioneer()

bestUnitForWorkLocation

public static Unit bestUnitForWorkLocation(java.util.Collection<Unit> units,
                                           WorkLocation workLocation,
                                           GoodsType goodsType)

getExperienceUpgrade

private static AIColony.ExperienceUpgrade getExperienceUpgrade(Unit unit,
                                                               UnitType expert)

checkForUnarmedExpertSoldier

private void checkForUnarmedExpertSoldier()
Checks if the colony has an unarmed expert soldier inside If there are conditions to arm it, put it outside for later equip


checkConditionsForHorseBreed

void checkConditionsForHorseBreed()
Verifies if the Colony has conditions for breeding horses, and un-mounts a mounted Unit if available, to have horses to breed.


placeExpertsInWorkPlaces

private void placeExpertsInWorkPlaces(java.util.List<Unit> units,
                                      java.util.List<WorkLocationPlan> workLocationPlans)

decideBuildable

private void decideBuildable(Connection connection)
Decides what to build in the Colony.

Parameters:
connection - The connection to use when communicating with the server.

propertyChange

public void propertyChange(java.beans.PropertyChangeEvent event)
Specified by:
propertyChange in interface java.beans.PropertyChangeListener

getColonyPlan

public ColonyPlan getColonyPlan()

toXMLImpl

protected void toXMLImpl(javax.xml.stream.XMLStreamWriter out)
                  throws javax.xml.stream.XMLStreamException
Writes this object to an XML stream.

Specified by:
toXMLImpl in class FreeColObject
Parameters:
out - The target stream.
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
Reads information for this object from an XML stream.

Overrides:
readFromXMLImpl in class FreeColObject
Parameters:
in - The input stream with the XML.
Throws:
javax.xml.stream.XMLStreamException - if there are any problems reading from the stream.

getXMLElementTagName

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

Returns:
"aiColony"