net.sf.freecol.server.ai
Class AIMain

java.lang.Object
  extended by net.sf.freecol.common.model.FreeColObject
      extended by net.sf.freecol.server.ai.AIMain
All Implemented Interfaces:
FreeColGameObjectListener

public class AIMain
extends FreeColObject
implements FreeColGameObjectListener

The main AI-class. Keeps references to all other AI-classes.


Field Summary
private  java.util.HashMap<java.lang.String,AIObject> aiObjects
          Contains mappings between FreeColGameObjects and AIObjects.
private  FreeColServer freeColServer
           
private static java.util.logging.Logger logger
           
private  int nextID
           
 
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
AIMain(FreeColServer freeColServer)
          Creates a new AIMain and searches the current game for FreeColGameObjects.
AIMain(FreeColServer freeColServer, org.w3c.dom.Element element)
          Creates a new AIMain and reads the given element.
AIMain(FreeColServer freeColServer, javax.xml.stream.XMLStreamReader in)
          Creates a new AIMain and reads the given element.
 
Method Summary
 void addAIObject(java.lang.String id, AIObject aiObject)
          Adds a reference to the given AIObject.
 boolean checkIntegrity()
          Checks the integrity of this AIMain by checking if there are any uninitialized objects.
private  void findNewObjects()
          Searches for new FreeColGameObjects.
 void findNewObjects(boolean overwrite)
          Searches for new FreeColGameObjects.
 AIColony getAIColony(Colony colony)
          Gets the AI colony corresponding to a given colony.
 AIObject getAIObject(FreeColGameObject fcgo)
          Gets the AIObject for the given FreeColGameObject.
 AIObject getAIObject(java.lang.String id)
          Gets the AIObject identified by the given ID.
 AIPlayer getAIPlayer(Player player)
          Gets the AI player corresponding to a given player.
 java.util.Random getAIRandom()
          Gets the random number generator to be used in the AI.
 java.util.HashMap<java.lang.String,java.lang.String> getAIStatistics()
          Computes how many objects of each class have been created, to track memory leaks over time
 AIUnit getAIUnit(Unit unit)
          Gets the AI unit corresponding to a given unit.
 FreeColGameObject getFreeColGameObject(java.lang.String id)
          Gets the FreeColGameObject with the given ID.
 FreeColServer getFreeColServer()
          Gets the main controller object for the server.
 Game getGame()
          Returns the game.
 java.lang.String getNextID()
          Gets a unique ID for identifying an AIObject.
static java.lang.String getXMLElementTagName()
          Returns the tag name of the root element representing this object.
 void ownerChanged(FreeColGameObject source, Player oldOwner, Player newOwner)
           
protected  void readFromXMLImpl(javax.xml.stream.XMLStreamReader in)
          Reads all the AIObjects and other AI-related information from XML data.
 void removeAIObject(java.lang.String id)
          Removes a reference to the given AIObject.
 void removeFreeColGameObject(java.lang.String id)
          Removes the AIObject for the given FreeColGameObject.
 void setFreeColGameObject(java.lang.String id, FreeColGameObject freeColGameObject)
          Creates a new AIObject for a given FreeColGameObject.
protected  void toXMLImpl(javax.xml.stream.XMLStreamWriter out)
          Writes all of the AIObjects and other AI-related information to an XML-stream.
protected  void writeAttributes(javax.xml.stream.XMLStreamWriter out)
          Write the attributes of this object to a stream.
protected  void writeChildren(javax.xml.stream.XMLStreamWriter out)
          Write the children of this object to a stream.
 
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, readFromXML, readFromXMLElement, readFromXMLImpl, readFromXMLPartialImpl, removePropertyChangeListener, removePropertyChangeListener, save, save, setId, toXML, toXML, toXML, toXMLElement, toXMLElement, toXMLElement, toXMLElement, toXMLElementPartial, toXMLPartialImpl, writeAttribute
 
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

freeColServer

private FreeColServer freeColServer

nextID

private int nextID

aiObjects

private java.util.HashMap<java.lang.String,AIObject> aiObjects
Contains mappings between FreeColGameObjects and AIObjects.

Constructor Detail

AIMain

public AIMain(FreeColServer freeColServer)
Creates a new AIMain and searches the current game for FreeColGameObjects.

Parameters:
freeColServer - The main controller object for the server.
See Also:
findNewObjects()

AIMain

public AIMain(FreeColServer freeColServer,
              org.w3c.dom.Element element)
Creates a new AIMain and reads the given element.

Parameters:
freeColServer - The main controller object for the server.
element - The Element (in a DOM-parsed XML-tree) that describes this object.
See Also:
FreeColObject.readFromXMLElement(org.w3c.dom.Element)

AIMain

public AIMain(FreeColServer freeColServer,
              javax.xml.stream.XMLStreamReader in)
       throws javax.xml.stream.XMLStreamException
Creates a new AIMain and reads the given element.

Parameters:
freeColServer - The main controller object for the server.
in - The input stream containing the XML.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.
See Also:
FreeColObject.readFromXML(javax.xml.stream.XMLStreamReader)
Method Detail

getFreeColServer

public FreeColServer getFreeColServer()
Gets the main controller object for the server.

Returns:
The FreeColServer-object.

getNextID

public java.lang.String getNextID()
Gets a unique ID for identifying an AIObject.

Returns:
A unique ID.

checkIntegrity

public boolean checkIntegrity()
Checks the integrity of this AIMain by checking if there are any uninitialized objects. Detected problems gets written to the log.

Returns:
true if the Game has been loaded properly.

getGame

public Game getGame()
Returns the game.

Returns:
The Game.

getAIRandom

public java.util.Random getAIRandom()
Gets the random number generator to be used in the AI.

Returns:
The AI random number generator.

findNewObjects

private void findNewObjects()
Searches for new FreeColGameObjects. An AI-object is created for each object.

Note: Any existing AIObjects will be overwritten.

See Also:
findNewObjects(boolean)

findNewObjects

public void findNewObjects(boolean overwrite)
Searches for new FreeColGameObjects. An AI-object is created for each new object.

Parameters:
overwrite - Determines wether any old AIObject should be overwritten or not.

getAIObject

public AIObject getAIObject(FreeColGameObject fcgo)
Gets the AIObject for the given FreeColGameObject.

Parameters:
fcgo - The FreeColGameObject to find the AIObject for.
Returns:
The AIObject.
See Also:
getAIObject(String)

getAIObject

public AIObject getAIObject(java.lang.String id)
Gets the AIObject identified by the given ID.

Parameters:
id - The ID of the AIObject.
Returns:
The AIObject.
See Also:
getAIObject(FreeColGameObject)

getAIColony

public AIColony getAIColony(Colony colony)
Gets the AI colony corresponding to a given colony.

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

getAIPlayer

public AIPlayer getAIPlayer(Player player)
Gets the AI player corresponding to a given player.

Parameters:
player - The Player to look up.
Returns:
The corresponding AI player, or null if not found.

getAIUnit

public AIUnit getAIUnit(Unit unit)
Gets the AI unit corresponding to a given unit.

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

addAIObject

public void addAIObject(java.lang.String id,
                        AIObject aiObject)
Adds a reference to the given AIObject.

Parameters:
id - The ID of the AIObject.
aiObject - The AIObject to store a reference for.
Throws:
java.lang.IllegalStateException - if an AIObject with the same id has already been created.

removeAIObject

public void removeAIObject(java.lang.String id)
Removes a reference to the given AIObject.

Parameters:
id - The ID of the AIObject.

getFreeColGameObject

public FreeColGameObject getFreeColGameObject(java.lang.String id)
Gets the FreeColGameObject with the given ID. This is just a convenience method for: Game.getFreeColGameObject(java.lang.String)

Parameters:
id - The ID of the FreeColGameObject to find.
Returns:
The FreeColGameObject.

ownerChanged

public void ownerChanged(FreeColGameObject source,
                         Player oldOwner,
                         Player newOwner)
Specified by:
ownerChanged in interface FreeColGameObjectListener

setFreeColGameObject

public void setFreeColGameObject(java.lang.String id,
                                 FreeColGameObject freeColGameObject)
Creates a new AIObject for a given FreeColGameObject. This method gets called whenever a new object gets added to the Game.

Specified by:
setFreeColGameObject in interface FreeColGameObjectListener
Parameters:
id - The ID of the FreeColGameObject to add.
freeColGameObject - The FreeColGameObject to add.
See Also:
AIObject, FreeColGameObject, FreeColObject.getId()

removeFreeColGameObject

public void removeFreeColGameObject(java.lang.String id)
Removes the AIObject for the given FreeColGameObject.

Specified by:
removeFreeColGameObject in interface FreeColGameObjectListener
Parameters:
id - The ID of the FreeColGameObject.

getAIStatistics

public java.util.HashMap<java.lang.String,java.lang.String> getAIStatistics()
Computes how many objects of each class have been created, to track memory leaks over time


toXMLImpl

protected void toXMLImpl(javax.xml.stream.XMLStreamWriter out)
                  throws javax.xml.stream.XMLStreamException
Writes all of the AIObjects and other AI-related information 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.

writeAttributes

protected void writeAttributes(javax.xml.stream.XMLStreamWriter out)
                        throws javax.xml.stream.XMLStreamException
Write the attributes of this object to a stream.

Overrides:
writeAttributes in class FreeColObject
Parameters:
out - The target stream.
Throws:
javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.

writeChildren

protected void writeChildren(javax.xml.stream.XMLStreamWriter out)
                      throws javax.xml.stream.XMLStreamException
Write the children of this object to a stream.

Overrides:
writeChildren 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 all the AIObjects and other AI-related information from XML data.

Overrides:
readFromXMLImpl in class FreeColObject
Parameters:
in - The input stream with the XML.
Throws:
javax.xml.stream.XMLStreamException - if an error occured during parsing.

getXMLElementTagName

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

Returns:
"aiMain"