net.sf.freecol.server.ai
Class StandardAIPlayer

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

public class StandardAIPlayer
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 StandardAIPlayer.AIStrategy
           
 
Field Summary
private static java.util.logging.Logger logger
           
private static int MAX_DISTANCE_TO_BRING_GIFT
           
private static int MAX_DISTANCE_TO_MAKE_DEMANDS
           
private static int MAX_NUMBER_OF_DEMANDS
           
private static int MAX_NUMBER_OF_GIFTS_BEING_DELIVERED
           
private  java.util.HashMap<java.lang.String,java.lang.Integer> sessionRegister
          Stores temporary information for sessions (trading with another player etc).
private  StandardAIPlayer.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, PARTIAL_ATTRIBUTE
 
Constructor Summary
StandardAIPlayer(AIMain aiMain, org.w3c.dom.Element element)
          Creates a new AIPlayer and reads the information from the given Element.
StandardAIPlayer(AIMain aiMain, ServerPlayer player)
          Creates a new AIPlayer.
StandardAIPlayer(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.
private  void bringGifts()
          Brings gifts to nice players with nearby colonies.
 int buyProposition(Unit unit, Goods goods, int gold)
          Called when another Player proposes a trade.
private  void cheat()
          Cheats for the AI :-)
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 demandTribute()
          Demands goods from players with nearby colonies.
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.
(package private)  void equipSoldiersOutsideColony(Colony colony)
           
private  Unit getBestTreasureTrain(Tile tile)
          Returns the treasure train carrying the largest treasure located on the given Tile.
private  AIUnit getBraveForSeekAndDestroy(IndianSettlement indianSettlement)
           
(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  boolean isWorkForREF()
          Checks if this player has work to do (provided it is an REF-player).
protected  void readFromXMLImpl(javax.xml.stream.XMLStreamReader in)
          Reads information for this object from an XML stream.
private  void rearrangeWorkersInColonies()
          Calls AIColony.rearrangeWorkers(net.sf.freecol.common.networking.Connection) 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
(package private)  void reOrganizeSoldiersOfColony(Colony colony)
           
(package private)  void secureIndianSettlement(IndianSettlement is)
          Takes the necessary actions to secure an indian settlement TODO: Package for access by a test only - necessary?
private  void secureSettlements()
          Takes the necessary actions to secure the settlements.
 FoundingFather selectFoundingFather(java.util.List<FoundingFather> foundingFathers)
          Selects the most useful founding father offered.
 void startWorking()
          Tells this AIPlayer to make decisions.
protected  void toXMLImpl(javax.xml.stream.XMLStreamWriter out)
          Writes this object to an XML stream.
 int tradeProposition(Unit unit, Settlement settlement, Goods goods, int gold)
          Called when another Player proposes a trade.
 
Methods inherited from class net.sf.freecol.server.ai.AIPlayer
checkForREFDefeat, clearAIUnits, getAIColonyIterator, getAIUnitIterator, getConnection, getId, getPlayer, recruitAIUnitInEurope, sendAndWaitSafely, sendUpdatedTilesToAll, 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, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAttribute, getAttribute, getAttribute, getAttribute, getPropertyChangeListeners, getPropertyChangeListeners, hasAbility, hasAttribute, hasListeners, readAttributes, readChildren, readFromArrayElement, readFromArrayElement, readFromArrayElement, readFromListElement, readFromXMLElement, readFromXMLPartialImpl, removePropertyChangeListener, removePropertyChangeListener, setId, toArrayElement, toArrayElement, toListElement, 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

MAX_DISTANCE_TO_BRING_GIFT

private static final int MAX_DISTANCE_TO_BRING_GIFT
See Also:
Constant Field Values

MAX_NUMBER_OF_GIFTS_BEING_DELIVERED

private static final int MAX_NUMBER_OF_GIFTS_BEING_DELIVERED
See Also:
Constant Field Values

MAX_DISTANCE_TO_MAKE_DEMANDS

private static final int MAX_DISTANCE_TO_MAKE_DEMANDS
See Also:
Constant Field Values

MAX_NUMBER_OF_DEMANDS

private static final int MAX_NUMBER_OF_DEMANDS
See Also:
Constant Field Values

strategy

private StandardAIPlayer.AIStrategy strategy
The strategy of this player.


sessionRegister

private java.util.HashMap<java.lang.String,java.lang.Integer> sessionRegister
Stores temporary information for sessions (trading with another player etc).

Constructor Detail

StandardAIPlayer

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

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

StandardAIPlayer

public StandardAIPlayer(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.

StandardAIPlayer

public StandardAIPlayer(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,
                          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.
goods - The goods the given Unit is trying to sell.
gold - The suggested price.
Returns:
The price this AIPlayer suggests or NetworkConstants.NO_TRADE.

tradeProposition

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

Specified by:
tradeProposition 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. ATTN: For compatibility, this has the same tag name as its super class, the previous implementation of all AIPlayers. Eventually change this at some point.

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.


isWorkForREF

private boolean isWorkForREF()
Checks if this player has work to do (provided it is an REF-player).

Returns:
true if any of our units are located in the new world or if a puppet-nation has declared independence.

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(net.sf.freecol.common.networking.Connection) for every colony this player owns.


secureSettlements

private void secureSettlements()
Takes the necessary actions to secure the settlements. This is done by making new military units or to give existing units new missions.


equipSoldiersOutsideColony

void equipSoldiersOutsideColony(Colony colony)

reOrganizeSoldiersOfColony

void reOrganizeSoldiersOfColony(Colony colony)

secureIndianSettlement

void secureIndianSettlement(IndianSettlement is)
Takes the necessary actions to secure an indian settlement TODO: Package for access by a test only - necessary?


getBraveForSeekAndDestroy

private AIUnit getBraveForSeekAndDestroy(IndianSettlement indianSettlement)

giveNormalMissions

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


bringGifts

private void bringGifts()
Brings gifts to nice players with nearby colonies.


demandTribute

private void demandTribute()
Demands goods from players with nearby colonies.


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.