net.sf.freecol.server.ai
Class ColonialAIPlayer

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.AIPlayer
              extended by net.sf.freecol.server.ai.ColonialAIPlayer

public class ColonialAIPlayer
extends AIPlayer

Objects of this class contains AI-information for a single Player and is used for controlling this player.

The method startWorking() gets called by the AIInGameInputHandler when it is this player's turn.


Nested Class Summary
private static class ColonialAIPlayer.AIStrategy
           
 
Field Summary
private static java.util.logging.Logger logger
           
private  ManageMissionariesGoal mGoal
          Goal to manage missionary units of this player.
private  java.util.ArrayList<AIUnit> myAIUnits
          Used in temporary override of AIUnit iterator methods
private  ColonialAIPlayer.AIStrategy strategy
          The strategy of this player.
 
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, PARTIAL_ATTRIBUTE, UNDEFINED, VALUE_TAG
 
Constructor Summary
ColonialAIPlayer(AIMain aiMain, org.w3c.dom.Element element)
          Creates a new AIPlayer and reads the information from the given Element.
ColonialAIPlayer(AIMain aiMain, ServerPlayer player)
          Creates a new AIPlayer.
ColonialAIPlayer(AIMain aiMain, javax.xml.stream.XMLStreamReader in)
          Creates a new AIPlayer.
 
Method Summary
private  void abortInvalidAndOneTimeMissions()
          Aborts all the missions which are no longer valid.
private  void abortInvalidMissions()
          Aborts all the missions which are no longer valid.
 boolean acceptDiplomaticTrade(DiplomaticTrade agreement)
           
 boolean acceptIndianDemand(Unit unit, Colony colony, Goods goods, int gold)
          Decides whether to accept an Indian demand, or not.
 boolean acceptMercenaryOffer()
          Decides whether to accept a mercenary offer, or not.
 boolean acceptTax(int tax)
          Decides whether to accept the monarch's tax raise or not.
 int buyProposition(Unit unit, Settlement settlement, Goods goods, int gold)
          Called when another Player proposes a trade.
private  void cheat()
          Cheats for the AI :-)
protected  void clearAIUnits()
          Helper method to let implementing subclasses clear aiUnits.
private  void createAIGoodsInColonies()
          Calls AIColony.createAIGoods() for every colony this player owns.
private  void createTransportLists()
          Maps Transportables to carrier's using a TransportMission.
private  void determineStances()
          Determines the stances towards each player.
private  void doMissions()
          Makes every unit perform their mission.
private  void ensureCorrectMissions()
          Ensures that all workers inside a colony gets a WorkInsideColonyMission.
protected  java.util.Iterator<AIUnit> getAIUnitIterator()
          Returns an iterator over all AIUnits owned by this player, which currently do _not_ have a goal.
private  Unit getBestTreasureTrain(Tile tile)
          Returns the treasure train carrying the largest treasure located on the given Tile.
(package private)  int getDefendColonyMissionValue(Unit u, Colony colony, int turns)
           
 java.util.Iterator<TileImprovementPlan> getTileImprovementPlanIterator()
          Returns an Iterator over all the TileImprovements needed by all of this player's colonies.
(package private)  int getUnitSeekAndDestroyMissionValue(Unit unit, Tile newTile, int turns)
           
 java.util.Iterator<Wish> getWishIterator()
          Returns an Iterator for all the wishes.
static java.lang.String getXMLElementTagName()
          Returns the tag name of the root element representing this object.
(package private)  void giveMilitaryMission(AIUnit aiUnit)
          Gives a military Mission to the given unit.
private  void giveNavalMissions()
          Gives missions to all the naval units this player owns.
private  void giveNormalMissions()
          Gives a mission to non-naval units.
 boolean hasFewColonies()
          This is a temporary method which are used for forcing the computer players into building more colonies.
(package private)  boolean isTargetValidForSeekAndDestroy(Unit attacker, Unit defender)
          TODO: Package for access by a test only - necessary?
private  void manageMissionaries()
           
protected  void readFromXMLImpl(javax.xml.stream.XMLStreamReader in)
          Reads information for this object from an XML stream.
private  void rearrangeWorkersInColonies()
          Calls AIColony.rearrangeWorkers for every colony this player owns.
 void registerSellGoods(Goods goods)
          Called after another Player sends a trade message
 void removeTileImprovementPlan(TileImprovementPlan plan)
          Remove a TileImprovementPlan from the list
 FoundingFather selectFoundingFather(java.util.List<FoundingFather> foundingFathers)
          Selects the most useful founding father offered.
 int sellProposition(Unit unit, Settlement settlement, Goods goods, int gold)
          Called when another Player proposes a sale.
 void startWorking()
          Tells this AIPlayer to make decisions.
protected  void toXMLImpl(javax.xml.stream.XMLStreamWriter out)
          Writes this object to an XML stream.
 
Methods inherited from class net.sf.freecol.server.ai.AIPlayer
determineStance, getAIColony, getAIColonyIterator, getAIUnit, getAIUnits, getConnection, getId, getPlayer, recruitAIUnitInEurope, setDebuggingConnection, setPlayer, trainAIUnitInEurope
 
Methods inherited from class net.sf.freecol.server.ai.AIObject
dispose, 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, getPropertyChangeListeners, getPropertyChangeListeners, getSpecification, hasAbility, hasAttribute, hasListeners, readAttributes, readAttributes, readChildren, readChildren, readFromArrayElement, readFromArrayElement, readFromListElement, readFromXMLElement, readFromXMLImpl, readFromXMLPartialImpl, removePropertyChangeListener, removePropertyChangeListener, save, save, setId, toXML, toXML, toXML, toXML, toXMLElement, toXMLElement, toXMLElement, toXMLElement, toXMLElementPartial, toXMLPartialImpl, writeAttribute, writeAttributes, writeChildren, writeFreeColGameObject
 
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

strategy

private ColonialAIPlayer.AIStrategy strategy
The strategy of this player.


myAIUnits

private java.util.ArrayList<AIUnit> myAIUnits
Used in temporary override of AIUnit iterator methods


mGoal

private ManageMissionariesGoal mGoal
Goal to manage missionary units of this player.

Constructor Detail

ColonialAIPlayer

public ColonialAIPlayer(AIMain aiMain,
                        ServerPlayer player)
Creates a new AIPlayer.

Parameters:
aiMain - The main AI-class.
player - The player that should be associated with this AIPlayer.

ColonialAIPlayer

public ColonialAIPlayer(AIMain aiMain,
                        org.w3c.dom.Element element)
Creates a new AIPlayer and reads the information from the given Element.

Parameters:
aiMain - The main AI-class.
element - The XML-element containing information.

ColonialAIPlayer

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

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.
Method Detail

startWorking

public void startWorking()
Tells this AIPlayer to make decisions. The AIPlayer is done doing work this turn when this method returns.

Specified by:
startWorking in class AIPlayer

getTileImprovementPlanIterator

public java.util.Iterator<TileImprovementPlan> getTileImprovementPlanIterator()
Returns an Iterator over all the TileImprovements needed by all of this player's colonies.

Specified by:
getTileImprovementPlanIterator in class AIPlayer
Returns:
The Iterator.
See Also:
TileImprovement

removeTileImprovementPlan

public void removeTileImprovementPlan(TileImprovementPlan plan)
Remove a TileImprovementPlan from the list

Specified by:
removeTileImprovementPlan in class AIPlayer

hasFewColonies

public boolean hasFewColonies()
This is a temporary method which are used for forcing the computer players into building more colonies. The method will be removed after the proper code for deciding whether a colony should be built or not has been implemented.

Specified by:
hasFewColonies in class AIPlayer
Returns:
true if the AI should build more colonies.

getWishIterator

public java.util.Iterator<Wish> getWishIterator()
Returns an Iterator for all the wishes. The items are sorted by the value, with the item having the highest value appearing first in the Iterator.

Specified by:
getWishIterator in class AIPlayer
Returns:
The Iterator.
See Also:
Wish

selectFoundingFather

public FoundingFather selectFoundingFather(java.util.List<FoundingFather> foundingFathers)
Selects the most useful founding father offered.

Specified by:
selectFoundingFather in class AIPlayer
Parameters:
foundingFathers - The founding fathers on offer.
Returns:
The founding father selected.

acceptTax

public boolean acceptTax(int tax)
Decides whether to accept the monarch's tax raise or not.

Specified by:
acceptTax in class AIPlayer
Parameters:
tax - The new tax rate to be considered.
Returns:
true if the tax raise should be accepted.

acceptIndianDemand

public boolean acceptIndianDemand(Unit unit,
                                  Colony colony,
                                  Goods goods,
                                  int gold)
Decides whether to accept an Indian demand, or not.

Specified by:
acceptIndianDemand in class AIPlayer
Parameters:
unit - The unit making demands.
colony - The colony where demands are being made.
goods - The goods demanded.
gold - The amount of gold demanded.
Returns:
true if this AIPlayer accepts the indian demand and false otherwise.

acceptMercenaryOffer

public boolean acceptMercenaryOffer()
Decides whether to accept a mercenary offer, or not.

Specified by:
acceptMercenaryOffer in class AIPlayer
Returns:
true if this AIPlayer accepts the offer and false otherwise.

acceptDiplomaticTrade

public boolean acceptDiplomaticTrade(DiplomaticTrade agreement)
Specified by:
acceptDiplomaticTrade in class AIPlayer

registerSellGoods

public void registerSellGoods(Goods goods)
Called after another Player sends a trade message

Specified by:
registerSellGoods in class AIPlayer
Parameters:
goods - The goods which we are going to offer

buyProposition

public int buyProposition(Unit unit,
                          Settlement settlement,
                          Goods goods,
                          int gold)
Called when another Player proposes a trade.

Specified by:
buyProposition in class AIPlayer
Parameters:
unit - The foreign Unit trying to trade.
settlement - The Settlement this player owns and which the given Unit is trying to trade.
goods - The goods the given Unit is trying to sell.
gold - The suggested price.
Returns:
The price this AIPlayer suggests or NetworkConstants.NO_TRADE.

sellProposition

public int sellProposition(Unit unit,
                           Settlement settlement,
                           Goods goods,
                           int gold)
Called when another Player proposes a sale.

Specified by:
sellProposition in class AIPlayer
Parameters:
unit - The foreign Unit trying to trade.
settlement - The Settlement this player owns and which the given Unit if trying to sell goods.
goods - The goods the given Unit is trying to sell.
gold - The suggested price.
Returns:
The price this AIPlayer suggests or NetworkConstants.NO_TRADE.

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 AIPlayer
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.

Specified by:
readFromXMLImpl in class AIPlayer
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:
the tag name.

cheat

private void cheat()
Cheats for the AI :-)


ensureCorrectMissions

private void ensureCorrectMissions()
Ensures that all workers inside a colony gets a WorkInsideColonyMission.


determineStances

private void determineStances()
Determines the stances towards each player. That is: should we declare war?


abortInvalidMissions

private void abortInvalidMissions()
Aborts all the missions which are no longer valid.


abortInvalidAndOneTimeMissions

private void abortInvalidAndOneTimeMissions()
Aborts all the missions which are no longer valid.


giveNavalMissions

private void giveNavalMissions()
Gives missions to all the naval units this player owns.


rearrangeWorkersInColonies

private void rearrangeWorkersInColonies()
Calls AIColony.rearrangeWorkers for every colony this player owns.


giveNormalMissions

private void giveNormalMissions()
Gives a mission to non-naval units.


createAIGoodsInColonies

private void createAIGoodsInColonies()
Calls AIColony.createAIGoods() for every colony this player owns.


doMissions

private void doMissions()
Makes every unit perform their mission.


getDefendColonyMissionValue

int getDefendColonyMissionValue(Unit u,
                                Colony colony,
                                int turns)

getUnitSeekAndDestroyMissionValue

int getUnitSeekAndDestroyMissionValue(Unit unit,
                                      Tile newTile,
                                      int turns)

isTargetValidForSeekAndDestroy

boolean isTargetValidForSeekAndDestroy(Unit attacker,
                                       Unit defender)
TODO: Package for access by a test only - necessary?


giveMilitaryMission

void giveMilitaryMission(AIUnit aiUnit)
Gives a military Mission to the given unit.

This method should only be used on units owned by european players. TODO: Package for access by a test only - necessary?

Parameters:
aiUnit - The unit.

createTransportLists

private void createTransportLists()
Maps Transportables to carrier's using a TransportMission.


getBestTreasureTrain

private Unit getBestTreasureTrain(Tile tile)
Returns the treasure train carrying the largest treasure located on the given Tile.

Parameters:
tile - a Tile value
Returns:
The best treasure train or null if no treasure train is located on this Tile.

manageMissionaries

private void manageMissionaries()

getAIUnitIterator

protected java.util.Iterator<AIUnit> getAIUnitIterator()
Returns an iterator over all AIUnits owned by this player, which currently do _not_ have a goal. This is a temporary override to allow the old AI code to keep working with some units, while exempting those already managed by new code.

Overrides:
getAIUnitIterator in class AIPlayer
Returns:
The Iterator.

clearAIUnits

protected void clearAIUnits()
Helper method to let implementing subclasses clear aiUnits. Override, see above!

Overrides:
clearAIUnits in class AIPlayer