net.sf.freecol.server.ai
Class AIPlayer

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
Direct Known Subclasses:
EuropeanAIPlayer, NativeAIPlayer

public abstract class AIPlayer
extends AIObject

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  java.util.List<AIUnit> aiUnits
          Temporary variable, used to hold all AIUnit objects belonging to this AI.
private  Connection debuggingConnection
          Temporary variable, used for debugging purposes only.
private static java.util.logging.Logger logger
           
static int MAX_DISTANCE_TO_BRING_GIFT
           
static int MAX_DISTANCE_TO_MAKE_DEMANDS
           
static int MAX_NUMBER_OF_DEMANDS
           
static int MAX_NUMBER_OF_GIFTS_BEING_DELIVERED
           
private  ServerPlayer player
          The FreeColGameObject this AIObject contains AI-information for.
 
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
AIPlayer(AIMain aiMain, java.lang.String id)
           
 
Method Summary
protected  void abortInvalidAndOneTimeMissions()
          Aborts all the missions which are no longer valid.
protected  void abortInvalidMissions()
          Aborts all the missions which are no longer valid.
abstract  boolean acceptDiplomaticTrade(DiplomaticTrade agreement)
          Returns an Iterator for all the wishes.
abstract  int buyProposition(Unit unit, Settlement settlement, Goods goods, int gold)
          Called when another Player proposes to buy.
protected  void clearAIUnits()
          Clears the cache of AI units.
private  void createAIUnits()
          Build the cache of AI units.
protected  Player.Stance determineStance(Player other)
          Standard stance change determination.
protected  void determineStances()
          Determines the stances towards each player.
protected  void doMissions()
          Makes every unit perform their mission.
protected  AIColony getAIColony(Colony colony)
          Gets the AI colony corresponding to a given colony, if any.
protected  java.util.Iterator<AIColony> getAIColonyIterator()
          Returns an iterator over all the AIColonys owned by this player.
protected  AIUnit getAIUnit(Unit unit)
          Gets the AI unit corresponding to a given unit, if any.
protected  java.util.Iterator<AIUnit> getAIUnitIterator()
          Returns an iterator over all the AIUnits owned by this player.
protected  java.util.List<AIUnit> getAIUnits()
          Gets a list of AIUnits for the player.
 Connection getConnection()
          Gets the connection to the server.
 java.lang.String getId()
          Returns the ID for this AIPlayer.
 Player getPlayer()
          Returns the Player this AIPlayer is controlling.
static java.lang.String getXMLElementTagName()
          Returns the tag name of the root element representing this object.
 boolean isTargetValidForSeekAndDestroy(Unit attacker, Tile tile)
          Find out if a tile contains a suitable target for seek-and-destroy.
protected  void readFromXMLImpl(javax.xml.stream.XMLStreamReader in)
          Reads information for this object from an XML stream.
abstract  void registerSellGoods(Goods goods)
          Called after another Player sends a trade message
abstract  int sellProposition(Unit unit, Settlement settlement, Goods goods, int gold)
          Called when another Player proposes a sale.
 void setDebuggingConnection(Connection debuggingConnection)
          Sets the Connection to be used while communicating with the server.
protected  void setPlayer(ServerPlayer p)
          Sets the ServerPlayer this AIPlayer is controlling.
abstract  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.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, getAttribute, 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

MAX_DISTANCE_TO_BRING_GIFT

public static final int MAX_DISTANCE_TO_BRING_GIFT
See Also:
Constant Field Values

MAX_NUMBER_OF_GIFTS_BEING_DELIVERED

public static final int MAX_NUMBER_OF_GIFTS_BEING_DELIVERED
See Also:
Constant Field Values

MAX_DISTANCE_TO_MAKE_DEMANDS

public static final int MAX_DISTANCE_TO_MAKE_DEMANDS
See Also:
Constant Field Values

MAX_NUMBER_OF_DEMANDS

public static final int MAX_NUMBER_OF_DEMANDS
See Also:
Constant Field Values

player

private ServerPlayer player
The FreeColGameObject this AIObject contains AI-information for.


debuggingConnection

private Connection debuggingConnection
Temporary variable, used for debugging purposes only. See setDebuggingConnection()


aiUnits

private java.util.List<AIUnit> aiUnits
Temporary variable, used to hold all AIUnit objects belonging to this AI. Any implementation of AIPlayer needs to make sure this list is invalidated as necessary, using clearAIUnits(), so that getAIUnitIterator() will create a new list.

Constructor Detail

AIPlayer

public AIPlayer(AIMain aiMain,
                java.lang.String id)
Method Detail

getPlayer

public Player getPlayer()
Returns the Player this AIPlayer is controlling.

Returns:
The Player.

setPlayer

protected void setPlayer(ServerPlayer p)
Sets the ServerPlayer this AIPlayer is controlling. Used by implementing subclasses.


getId

public java.lang.String getId()
Returns the ID for this AIPlayer. This is the same as the ID for the Player this AIPlayer controls.

Overrides:
getId in class FreeColObject
Returns:
The ID.

getConnection

public Connection getConnection()
Gets the connection to the server.

Returns:
The connection that can be used when communication with the server.

setDebuggingConnection

public void setDebuggingConnection(Connection debuggingConnection)
Sets the Connection to be used while communicating with the server. This method is only used for debugging.

Parameters:
debuggingConnection - The connection to be used for debugging.

clearAIUnits

protected void clearAIUnits()
Clears the cache of AI units.


createAIUnits

private void createAIUnits()
Build the cache of AI units.


getAIUnits

protected java.util.List<AIUnit> getAIUnits()
Gets a list of AIUnits for the player.

Returns:
A list of AIUnits.

getAIUnitIterator

protected java.util.Iterator<AIUnit> getAIUnitIterator()
Returns an iterator over all the AIUnits owned by this player.

Returns:
The Iterator.

getAIColonyIterator

protected java.util.Iterator<AIColony> getAIColonyIterator()
Returns an iterator over all the AIColonys owned by this player.

Returns:
The Iterator.

getAIColony

protected AIColony getAIColony(Colony colony)
Gets the AI colony corresponding to a given colony, if any.

Parameters:
colony - The Colony to look up.
Returns:
The corresponding AI colony or null if not found.

getAIUnit

protected AIUnit getAIUnit(Unit unit)
Gets the AI unit corresponding to a given unit, if any.

Parameters:
unit - The Unit to look up.
Returns:
The corresponding AI unit or null if not found.

determineStance

protected Player.Stance determineStance(Player other)
Standard stance change determination. If a change occurs, contact the server and propagate.

Parameters:
other - The Player wrt consider stance.
Returns:
The stance, which may have been updated.

startWorking

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


acceptDiplomaticTrade

public abstract boolean acceptDiplomaticTrade(DiplomaticTrade agreement)
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.

Returns:
The Iterator.
See Also:
Wish

registerSellGoods

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

Parameters:
goods - The goods which we are going to offer

buyProposition

public abstract int buyProposition(Unit unit,
                                   Settlement settlement,
                                   Goods goods,
                                   int gold)
Called when another Player proposes to buy.

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 abstract int sellProposition(Unit unit,
                                    Settlement settlement,
                                    Goods goods,
                                    int gold)
Called when another Player proposes a sale.

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.

determineStances

protected void determineStances()
Determines the stances towards each player. That is: should we declare war? TODO: something better, that includes peacemaking.


abortInvalidMissions

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


abortInvalidAndOneTimeMissions

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


doMissions

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


isTargetValidForSeekAndDestroy

public boolean isTargetValidForSeekAndDestroy(Unit attacker,
                                              Tile tile)
Find out if a tile contains a suitable target for seek-and-destroy. TODO: Package for access by a test only - necessary?

Parameters:
attacker - The attacking Unit.
tile - The Tile to attack into.
Returns:
True if an attack can be launched.

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:
the tag name.