net.sf.freecol.server.ai
Class EuropeanAIPlayer

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.EuropeanAIPlayer
Direct Known Subclasses:
REFAIPlayer

public class EuropeanAIPlayer
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.


Field Summary
private static java.util.logging.Logger logger
           
private  java.util.HashMap<java.lang.String,java.lang.Integer> sessionRegister
          Stores temporary information for sessions (trading with another player etc).
 
Fields inherited from class net.sf.freecol.server.ai.AIPlayer
MAX_DISTANCE_TO_BRING_GIFT, MAX_DISTANCE_TO_MAKE_DEMANDS, MAX_NUMBER_OF_DEMANDS, MAX_NUMBER_OF_GIFTS_BEING_DELIVERED
 
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
EuropeanAIPlayer(AIMain aiMain, org.w3c.dom.Element element)
          Creates a new AIPlayer and reads the information from the given Element.
EuropeanAIPlayer(AIMain aiMain, ServerPlayer player)
          Creates a new AIPlayer.
EuropeanAIPlayer(AIMain aiMain, javax.xml.stream.XMLStreamReader in)
          Creates a new AIPlayer.
 
Method Summary
 boolean acceptDiplomaticTrade(DiplomaticTrade agreement)
           
 boolean acceptMercenaries()
          Decides to accept an offer of mercenaries 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, Settlement settlement, Goods goods, int gold)
          Called when another Player proposes to buy.
private  void cheat()
          Cheats for the AI.
 Ownable chooseMilitaryTarget(AIUnit aiUnit)
          Chooses the best target for an AI unit to attack.
private  void createAIGoodsInColonies()
          Calls AIColony.createAIGoods() for every colony this player owns.
private  void createTransportLists()
          Assign transportable units and goods to available carriers transport lists.
private  void demandTribute()
          Demands goods from players with nearby colonies.
private  void ensureColonyMissions()
          Ensures that all workers inside a colony gets a WorkInsideColonyMission.
private  Unit getBestTreasureTrain(Tile tile)
          Finds the treasure train carrying the largest treasure located on the given Tile.
protected  int getColonyDefenders(Colony colony)
          Counts the number of defenders in a colony.
 int getDefendColonyMissionValue(Unit unit, Colony colony, int turns)
          Evaluate allocating a unit to the defence of a colony.
private  java.util.List<AIUnit> getPlayerPioneers()
          Takes the necessary actions to secure a european colony
private  int getScaledTurns(java.util.Map<Location,java.lang.Integer> distances, Location destination)
           
 java.util.List<TileImprovementPlan> getTileImprovementPlans()
          Gets a list of all the player's tile improvement plans required by the colonies.
private  int getTurns(Transportable t, TransportMission tm)
           
 int getUnitSeekAndDestroyMissionValue(Unit unit, Tile newTile, int turns)
          Evaluate a potential seek and destroy mission for a given unit to a given tile.
 java.util.List<Wish> getWishes()
          Gets the wishes for all this player's colonies, sorted by the value.
private  void giveColonistMission(AIUnit aiUnit, boolean fewColonies, java.util.Map<UnitType,java.util.ArrayList<Wish>> workerWishes)
           
 void giveMilitaryMission(AIUnit aiUnit)
          Gives a military mission to the given unit.
protected  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.
 boolean indianDemand(Unit unit, Colony colony, Goods goods, int gold)
          Decides whether to accept an Indian demand, or not.
private  void rearrangeWorkersInColonies()
          Calls AIColony.rearrangeWorkers() for every colony this player owns.
 AIUnit recruitAIUnitInEurope(int index)
          Asks the server to recruit a unit in Europe on behalf of the AIPlayer.
 void registerSellGoods(Goods goods)
          Called after another Player sends a trade message
 void removeTileImprovementPlan(TileImprovementPlan plan)
          Remove a TileImprovementPlan from the relevant colony.
 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.
 AIUnit trainAIUnitInEurope(UnitType unitType)
          Helper function for server communication - Ask the server to train a unit in Europe on behalf of the AIGetPlayer().
 
Methods inherited from class net.sf.freecol.server.ai.AIPlayer
abortInvalidAndOneTimeMissions, abortInvalidMissions, clearAIUnits, determineStance, determineStances, doMissions, getAIAdvantage, getAIColonies, getAIColony, getAIRandom, getAIUnit, getAIUnitIterator, getAIUnits, getConnection, getId, getPlayer, getXMLElementTagName, isTargetValidForSeekAndDestroy, readFromXMLImpl, setDebuggingConnection, setPlayer, toXMLImpl
 
Methods inherited from class net.sf.freecol.server.ai.AIObject
dispose, getAIMain, getGame, getSpecification, 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, getPropertyChangeListeners, getPropertyChangeListeners, hasAbility, hasAttribute, hasListeners, readAttributes, readAttributes, readChild, readChild, readChildren, readChildren, readFromArrayElement, readFromArrayElement, readFromListElement, readFromXMLElement, readFromXMLImpl, readFromXMLPartialImpl, removePropertyChangeListener, removePropertyChangeListener, save, save, setId, setSpecification, 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

sessionRegister

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

Constructor Detail

EuropeanAIPlayer

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

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

EuropeanAIPlayer

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

EuropeanAIPlayer

public EuropeanAIPlayer(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

getTileImprovementPlans

public java.util.List<TileImprovementPlan> getTileImprovementPlans()
Gets a list of all the player's tile improvement plans required by the colonies.

Returns:
A list of tile improvements.
See Also:
TileImprovement

removeTileImprovementPlan

public void removeTileImprovementPlan(TileImprovementPlan plan)
Remove a TileImprovementPlan from the relevant colony.


recruitAIUnitInEurope

public AIUnit recruitAIUnitInEurope(int index)
Asks the server to recruit a unit in Europe on behalf of the AIPlayer. TODO: Move this to a specialized Handler class (AIEurope?) TODO: Give protected access?

Parameters:
index - The index of the unit to recruit in the recruitables list, (if not a valid index, recruit a random unit).
Returns:
The new AIUnit created by this action or null on failure.

trainAIUnitInEurope

public AIUnit trainAIUnitInEurope(UnitType unitType)
Helper function for server communication - Ask the server to train a unit in Europe on behalf of the AIGetPlayer(). TODO: Move this to a specialized Handler class (AIEurope?) TODO: Give protected access?

Returns:
the new AIUnit created by this action. May be null.

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.

Returns:
true if the AI should build more colonies.

getWishes

public java.util.List<Wish> getWishes()
Gets the wishes for all this player's colonies, sorted by the value.

Returns:
A list of wishes.

selectFoundingFather

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

Overrides:
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.

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

indianDemand

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

Overrides:
indianDemand 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 player accepts the demand.

acceptMercenaries

public boolean acceptMercenaries()
Decides to accept an offer of mercenaries or not. TODO: make a better choice.

Overrides:
acceptMercenaries in class AIPlayer
Returns:
True if the mercenaries are accepted.

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 to buy.

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

cheat

private void cheat()
Cheats for the AI. Please try to centralize cheats here. TODO: Remove when the AI is good enough.


ensureColonyMissions

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


rearrangeWorkersInColonies

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


getPlayerPioneers

private java.util.List<AIUnit> getPlayerPioneers()
Takes the necessary actions to secure a european colony


giveNormalMissions

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


giveColonistMission

private void giveColonistMission(AIUnit aiUnit,
                                 boolean fewColonies,
                                 java.util.Map<UnitType,java.util.ArrayList<Wish>> workerWishes)

getScaledTurns

private int getScaledTurns(java.util.Map<Location,java.lang.Integer> distances,
                           Location destination)

bringGifts

private void bringGifts()
Brings gifts to nice players with nearby colonies. TODO: European players can also bring gifts! However, this might be folded into a trade mission, since European gifts are just a special case of trading.


demandTribute

private void demandTribute()
Demands goods from players with nearby colonies. TODO: European players can also demand tribute!


createAIGoodsInColonies

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


getColonyDefenders

protected int getColonyDefenders(Colony colony)
Counts the number of defenders in a colony.

Parameters:
colony - The Colony to examine.
Returns:
The number of defenders.

getDefendColonyMissionValue

public int getDefendColonyMissionValue(Unit unit,
                                       Colony colony,
                                       int turns)
Evaluate allocating a unit to the defence of a colony. Temporary helper method for giveMilitaryMission.

Parameters:
unit - The Unit that is to defend.
colony - The Colony to defend.
turns - The turns for the unit to reach the colony.
Returns:
A value for such a mission.

getUnitSeekAndDestroyMissionValue

public int getUnitSeekAndDestroyMissionValue(Unit unit,
                                             Tile newTile,
                                             int turns)
Evaluate a potential seek and destroy mission for a given unit to a given tile. TODO: revisit and rebalance the mass of magic numbers.

Parameters:
unit - The Unit to do the mission.
newTile - The Tile to go to.
turns - How long to travel to the tile.
Returns:
A score for the proposed mission.

chooseMilitaryTarget

public Ownable chooseMilitaryTarget(AIUnit aiUnit)
Chooses the best target for an AI unit to attack.

Parameters:
aiUnit - The AIUnit that will attack.
Returns:
The best target for a military mission.

giveMilitaryMission

public void giveMilitaryMission(AIUnit aiUnit)
Gives a military mission to the given unit. Old comment: Temporary method for giving a military mission. This method will be removed when "MilitaryStrategy" and the "Tactic"-classes has been implemented.

Parameters:
aiUnit - The AIUnit to give a mission to.

getTurns

private int getTurns(Transportable t,
                     TransportMission tm)

createTransportLists

private void createTransportLists()
Assign transportable units and goods to available carriers transport lists.


getBestTreasureTrain

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

Parameters:
tile - The Tile to check.
Returns:
The best treasure train found or null if none present.