Package net.sf.freecol.server.ai
Class AIMain
- java.lang.Object
-
- net.sf.freecol.common.model.FreeColObject
-
- net.sf.freecol.server.ai.AIMain
-
- All Implemented Interfaces:
java.lang.Comparable<FreeColObject>,FreeColGameObjectListener,ObjectWithId
public class AIMain extends FreeColObject implements FreeColGameObjectListener
The main AI-class. Keeps references to all other AI-classes.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.String,AIObject>aiObjectsContains mappings betweenFreeColGameObjects andAIObjects.private FreeColServerfreeColServerThe server that this AI is operating within.private static java.util.logging.Loggerloggerprivate static java.lang.StringNEXT_ID_TAGprivate intnextIdThe next AI identifier index.private static java.lang.StringOLD_GOODS_WISH_TAGprivate static java.lang.StringOLD_TILE_IMPROVEMENT_PLAN_TAGstatic java.lang.StringTAG-
Fields inherited from class net.sf.freecol.common.model.FreeColObject
ARRAY_SIZE_TAG, DEFAULT_CLASS_INDEX, fcoComparator, ID_ATTRIBUTE_TAG, PARTIAL_ATTRIBUTE_TAG, VALUE_TAG
-
-
Constructor Summary
Constructors Constructor Description AIMain(FreeColServer freeColServer)Creates a newAIMainand searches the current game forFreeColGameObjects.AIMain(FreeColServer freeColServer, FreeColXMLReader xr)Creates a newAIMainfrom a reader.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddAIObject(java.lang.String id, AIObject aiObject)Adds a reference to the givenAIObject.Constants.IntegrityTypecheckIntegrity(boolean fix, LogBuilder lb)Check the AI integrity too.java.lang.StringcheckSortConsistency()Check sort consistency.<T extends FreeColObject>
booleancopyIn(T other)Copy another FreeColObject into this one if it is compatible.voidfindNewObjects(boolean overwrite)Searches for newFreeColGameObjects.AIColonygetAIColony(Colony colony)Gets the AI colony corresponding to a given colony.AIObjectgetAIObject(java.lang.String id)Gets theAIObjectfor a given object identifier.<T extends AIObject>
TgetAIObject(java.lang.String id, java.lang.Class<T> returnClass)Gets theAIObjectwith the specified object identifier and class.private AIObjectgetAIObject(FreeColGameObject fcgo)Gets theAIObjectfor the givenFreeColGameObject.private java.util.List<AIObject>getAIObjects()Get a copy of the list of all AI objects.AIPlayergetAIPlayer(Player player)Gets the AI player corresponding to a given player.java.util.Map<java.lang.String,java.lang.String>getAIStatistics()Computes how many objects of each class have been created, to track memory leaks over timeAIUnitgetAIUnit(Unit unit)Gets the AI unit corresponding to a given unit.FreeColServergetFreeColServer()Gets the main controller object for the server.GamegetGame()Convenience accessor for the Game.java.lang.StringgetNextId()Gets a unique identifier for anAIObject.intgetRandomSeed(java.lang.String logMe)Gets a random value from the server to use for individual AI player PRNG seeds.SpecificationgetSpecification()Get the specification.java.lang.StringgetXMLTagName()Get the serialization tag for this object.voidownerChanged(FreeColGameObject source, Player oldOwner, Player newOwner)Replaces the AI object when ownership changes.protected voidreadAttributes(FreeColXMLReader xr)Reads the attributes of this object from an XML stream.protected voidreadChild(FreeColXMLReader xr)Reads a single child object.protected voidreadChildren(FreeColXMLReader xr)Reads the children of this object from an XML stream.booleanremoveAIObject(java.lang.String id)Removes a reference to the givenAIObject.voidremoveFreeColGameObject(java.lang.String id)Removes theAIObjectfor a given AI identifier.voidsetFreeColGameObject(java.lang.String id, FreeColGameObject fcgo)Creates a newAIObjectfor a givenFreeColGameObject.private booleanshouldHaveAIObject(FreeColGameObject fcgo)Should aFreeColGameObjecthave a corresponding AI object? Strictly true only for AI players and their units and colonies (not (yet) for native settlements).protected voidwriteAttributes(FreeColXMLWriter xw)Write the attributes of this object to a stream.protected voidwriteChildren(FreeColXMLWriter xw)Write the children of this object to a stream.-
Methods inherited from class net.sf.freecol.common.model.FreeColObject
addAbility, addFeatures, addModifier, addPropertyChangeListener, addPropertyChangeListener, apply, apply, applyModifiers, applyModifiers, arrayKey, compareIds, compareTo, containsAbilityKey, containsModifierKey, copy, copy, copy, copy, copyInCast, dumpObject, equals, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAbilities, getAbilities, getAbilities, getAbilities, getClassIndex, getDefenceModifiers, getDisplayObject, getFeatureContainer, getFreeColObjectClass, getFreeColObjectClassByName, getId, getIdNumber, getIdType, getIdTypeByName, getModifiers, getModifiers, getModifiers, getModifiers, getObjectClassIndex, getPropertyChangeListeners, getPropertyChangeListeners, getPropertyChangeSupport, getSortedAbilities, getSortedModifiers, getSuffix, getSuffix, hasAbility, hasAbility, hasAbility, hashCode, hasListeners, hasModifier, hasModifier, hasModifier, idEquals, invokeMethod, logFreeColObjects, readFromXML, readFromXMLPartial, removeAbilities, removeAbility, removeFeatures, removeModifier, removeModifiers, removePropertyChangeListener, removePropertyChangeListener, save, save, save, save, serialize, serialize, serialize, serialize, setGame, setId, setSpecification, toString, toXML, toXML, toXMLPartial, toXMLPartial
-
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
TAG
public static final java.lang.String TAG
- See Also:
- Constant Field Values
-
freeColServer
private final FreeColServer freeColServer
The server that this AI is operating within.
-
nextId
private int nextId
The next AI identifier index.
-
aiObjects
private final java.util.Map<java.lang.String,AIObject> aiObjects
Contains mappings betweenFreeColGameObjects andAIObjects.
-
NEXT_ID_TAG
private static final java.lang.String NEXT_ID_TAG
- See Also:
- Constant Field Values
-
OLD_GOODS_WISH_TAG
private static final java.lang.String OLD_GOODS_WISH_TAG
- See Also:
- Constant Field Values
-
OLD_TILE_IMPROVEMENT_PLAN_TAG
private static final java.lang.String OLD_TILE_IMPROVEMENT_PLAN_TAG
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
AIMain
public AIMain(FreeColServer freeColServer)
Creates a newAIMainand searches the current game forFreeColGameObjects.- Parameters:
freeColServer- The main controller object for the server.
-
AIMain
public AIMain(FreeColServer freeColServer, FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Creates a newAIMainfrom a reader.- Parameters:
freeColServer- The main controller object for the server.xr- The input stream containing the XML.- Throws:
javax.xml.stream.XMLStreamException- if a problem was encountered during parsing.
-
-
Method Detail
-
getFreeColServer
public FreeColServer getFreeColServer()
Gets the main controller object for the server.- Returns:
- The
FreeColServer-object.
-
getGame
public Game getGame()
Convenience accessor for the Game.- Overrides:
getGamein classFreeColObject- Returns:
- The
Gamethis AI is operating in.
-
getNextId
public java.lang.String getNextId()
Gets a unique identifier for anAIObject.- Returns:
- A unique identifier.
-
getRandomSeed
public int getRandomSeed(java.lang.String logMe)
Gets a random value from the server to use for individual AI player PRNG seeds.- Parameters:
logMe- A logging string.- Returns:
- A random seed.
-
shouldHaveAIObject
private boolean shouldHaveAIObject(FreeColGameObject fcgo)
Should aFreeColGameObjecthave a corresponding AI object? Strictly true only for AI players and their units and colonies (not (yet) for native settlements). However object initialization is not necessarily complete when we arrive here, which means we can not yet use the Colony or Unit owner fields. So the actual test implemented here is somewhat sloppy.- Parameters:
fcgo- TheFreeColGameObjectto test.- Returns:
- True if a corresponding AI object is needed.
-
findNewObjects
public void findNewObjects(boolean overwrite)
Searches for newFreeColGameObjects. An AI-object is created for each new object.- Parameters:
overwrite- Determines wether any oldAIObjectshould be overwritten or not.
-
getAIObject
private AIObject getAIObject(FreeColGameObject fcgo)
Gets theAIObjectfor the givenFreeColGameObject.- Parameters:
fcgo- TheFreeColGameObjectto find theAIObjectfor.- Returns:
- The
AIObject. - See Also:
getAIObject(String)
-
getAIObject
public AIObject getAIObject(java.lang.String id)
Gets theAIObjectfor a given object identifier.- Parameters:
id- The object identifier.- Returns:
- The
AIObject. - See Also:
getAIObject(FreeColGameObject)
-
addAIObject
public void addAIObject(java.lang.String id, AIObject aiObject)Adds a reference to the givenAIObject. Note: this is called only for new AI objects, thus it is an error for the object to already be present in the aiObjects map.- Parameters:
id- The object identifier.aiObject- TheAIObjectto store a reference for.
-
removeAIObject
public boolean removeAIObject(java.lang.String id)
Removes a reference to the givenAIObject.- Parameters:
id- The object identifier.- Returns:
- True if an object for the identifier is removed.
-
getAIObjects
private java.util.List<AIObject> getAIObjects()
Get a copy of the list of all AI objects.- Returns:
- A list of
AIObjects.
-
getAIObject
public <T extends AIObject> T getAIObject(java.lang.String id, java.lang.Class<T> returnClass)
Gets theAIObjectwith the specified object identifier and class.- Type Parameters:
T- The actual return type.- Parameters:
id- The object identifier.returnClass- The expected class of the object.- Returns:
- The
AIObjectfound, or null if not.
-
getAIColony
public AIColony getAIColony(Colony colony)
Gets the AI colony corresponding to a given colony.- Parameters:
colony- TheColonyto 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- ThePlayerto 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- TheUnitto look up.- Returns:
- The corresponding AI unit, or null if not found.
-
getAIStatistics
public java.util.Map<java.lang.String,java.lang.String> getAIStatistics()
Computes how many objects of each class have been created, to track memory leaks over time- Returns:
- A map of AI statistics.
-
checkSortConsistency
public java.lang.String checkSortConsistency()
Check sort consistency.- Returns:
- A string describing sort consistency failures, or null on success.
-
checkIntegrity
public Constants.IntegrityType checkIntegrity(boolean fix, LogBuilder lb)
Check the AI integrity too.- Parameters:
fix- If true, fix problems if possible.lb- ALogBuilderto log to.- Returns:
- -1 if there are problems remaining, zero if problems were fixed, +1 if no problems found at all.
-
setFreeColGameObject
public void setFreeColGameObject(java.lang.String id, FreeColGameObject fcgo)Creates a newAIObjectfor a givenFreeColGameObject. This method gets called whenever a new object gets added to theGame.- Specified by:
setFreeColGameObjectin interfaceFreeColGameObjectListener- Parameters:
id- The object identifier.fcgo- TheFreeColGameObjectto add.- See Also:
AIObject,FreeColGameObject,FreeColObject.getId()
-
removeFreeColGameObject
public void removeFreeColGameObject(java.lang.String id)
Removes theAIObjectfor a given AI identifier. Needed for interface FreeColGameObjectListener.- Specified by:
removeFreeColGameObjectin interfaceFreeColGameObjectListener- Parameters:
id- The object identifier.
-
ownerChanged
public void ownerChanged(FreeColGameObject source, Player oldOwner, Player newOwner)
Replaces the AI object when ownership changes.- Specified by:
ownerChangedin interfaceFreeColGameObjectListener- Parameters:
source- TheFreeColGameObjectthat has changed.oldOwner- The old owningPlayer.newOwner- The new owningPlayer.
-
getSpecification
public Specification getSpecification()
Get the specification.- Overrides:
getSpecificationin classFreeColObject- Returns:
- The
Specificationused by this object.
-
copyIn
public <T extends FreeColObject> boolean copyIn(T other)
Copy another FreeColObject into this one if it is compatible.- Overrides:
copyInin classFreeColObject- Type Parameters:
T- TheFreeColObjectsubclass of the object to copy in.- Parameters:
other- The other object.- Returns:
- True if the copy in is succesful.
-
writeAttributes
protected void writeAttributes(FreeColXMLWriter xw) throws javax.xml.stream.XMLStreamException
Write the attributes of this object to a stream. To be overridden if required by any object that has attributes and uses the toXML(FreeColXMLWriter, String) call.- Overrides:
writeAttributesin classFreeColObject- Parameters:
xw- TheFreeColXMLWriterto write to.- Throws:
javax.xml.stream.XMLStreamException- if there are any problems writing to the stream.
-
writeChildren
protected void writeChildren(FreeColXMLWriter xw) throws javax.xml.stream.XMLStreamException
Write the children of this object to a stream. To be overridden if required by any object that has children and uses the toXML(FreeColXMLWriter, String) call.- Overrides:
writeChildrenin classFreeColObject- Parameters:
xw- TheFreeColXMLWriterto write to.- Throws:
javax.xml.stream.XMLStreamException- if there are any problems writing to the stream.
-
readAttributes
protected void readAttributes(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Reads the attributes of this object from an XML stream.- Overrides:
readAttributesin classFreeColObject- Parameters:
xr- TheFreeColXMLReaderto read from.- Throws:
javax.xml.stream.XMLStreamException- if there is a problem reading the stream.
-
readChildren
protected void readChildren(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Reads the children of this object from an XML stream.- Overrides:
readChildrenin classFreeColObject- Parameters:
xr- TheFreeColXMLReaderto read from.- Throws:
javax.xml.stream.XMLStreamException- if there is a problem reading the stream.
-
readChild
protected void readChild(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Reads a single child object. Subclasses must override to read their enclosed elements. This particular instance of the routine always throws XMLStreamException because we should never arrive here. However it is very useful to always call super.readChild() when an unexpected tag is encountered, as the exception thrown here provides some useful debugging context.- Overrides:
readChildin classFreeColObject- Parameters:
xr- TheFreeColXMLReaderto read from.- Throws:
javax.xml.stream.XMLStreamException- if there is a problem reading the stream.
-
getXMLTagName
public java.lang.String getXMLTagName()
Get the serialization tag for this object.- Specified by:
getXMLTagNamein classFreeColObject- Returns:
- The tag.
-
-