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>
aiObjects
Contains mappings betweenFreeColGameObject
s andAIObject
s.private FreeColServer
freeColServer
The server that this AI is operating within.private static java.util.logging.Logger
logger
private static java.lang.String
NEXT_ID_TAG
private int
nextId
The next AI identifier index.private static java.lang.String
OLD_GOODS_WISH_TAG
private static java.lang.String
OLD_TILE_IMPROVEMENT_PLAN_TAG
static java.lang.String
TAG
-
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 newAIMain
and searches the current game forFreeColGameObject
s.AIMain(FreeColServer freeColServer, FreeColXMLReader xr)
Creates a newAIMain
from a reader.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addAIObject(java.lang.String id, AIObject aiObject)
Adds a reference to the givenAIObject
.Constants.IntegrityType
checkIntegrity(boolean fix, LogBuilder lb)
Check the AI integrity too.java.lang.String
checkSortConsistency()
Check sort consistency.<T extends FreeColObject>
booleancopyIn(T other)
Copy another FreeColObject into this one if it is compatible.void
findNewObjects(boolean overwrite)
Searches for newFreeColGameObjects
.AIColony
getAIColony(Colony colony)
Gets the AI colony corresponding to a given colony.AIObject
getAIObject(java.lang.String id)
Gets theAIObject
for a given object identifier.<T extends AIObject>
TgetAIObject(java.lang.String id, java.lang.Class<T> returnClass)
Gets theAIObject
with the specified object identifier and class.private AIObject
getAIObject(FreeColGameObject fcgo)
Gets theAIObject
for the givenFreeColGameObject
.private java.util.List<AIObject>
getAIObjects()
Get a copy of the list of all AI objects.AIPlayer
getAIPlayer(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 timeAIUnit
getAIUnit(Unit unit)
Gets the AI unit corresponding to a given unit.FreeColServer
getFreeColServer()
Gets the main controller object for the server.Game
getGame()
Convenience accessor for the Game.java.lang.String
getNextId()
Gets a unique identifier for anAIObject
.int
getRandomSeed(java.lang.String logMe)
Gets a random value from the server to use for individual AI player PRNG seeds.Specification
getSpecification()
Get the specification.java.lang.String
getXMLTagName()
Get the serialization tag for this object.void
ownerChanged(FreeColGameObject source, Player oldOwner, Player newOwner)
Replaces the AI object when ownership changes.protected void
readAttributes(FreeColXMLReader xr)
Reads the attributes of this object from an XML stream.protected void
readChild(FreeColXMLReader xr)
Reads a single child object.protected void
readChildren(FreeColXMLReader xr)
Reads the children of this object from an XML stream.boolean
removeAIObject(java.lang.String id)
Removes a reference to the givenAIObject
.void
removeFreeColGameObject(java.lang.String id)
Removes theAIObject
for a given AI identifier.void
setFreeColGameObject(java.lang.String id, FreeColGameObject fcgo)
Creates a newAIObject
for a givenFreeColGameObject
.private boolean
shouldHaveAIObject(FreeColGameObject fcgo)
Should aFreeColGameObject
have a corresponding AI object? Strictly true only for AI players and their units and colonies (not (yet) for native settlements).protected void
writeAttributes(FreeColXMLWriter xw)
Write the attributes of this object to a stream.protected void
writeChildren(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 betweenFreeColGameObject
s andAIObject
s.
-
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 newAIMain
and searches the current game forFreeColGameObject
s.- Parameters:
freeColServer
- The main controller object for the server.
-
AIMain
public AIMain(FreeColServer freeColServer, FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Creates a newAIMain
from 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:
getGame
in classFreeColObject
- Returns:
- The
Game
this 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 aFreeColGameObject
have 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
- TheFreeColGameObject
to 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 oldAIObject
should be overwritten or not.
-
getAIObject
private AIObject getAIObject(FreeColGameObject fcgo)
Gets theAIObject
for the givenFreeColGameObject
.- Parameters:
fcgo
- TheFreeColGameObject
to find theAIObject
for.- Returns:
- The
AIObject
. - See Also:
getAIObject(String)
-
getAIObject
public AIObject getAIObject(java.lang.String id)
Gets theAIObject
for 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
- TheAIObject
to 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
AIObject
s.
-
getAIObject
public <T extends AIObject> T getAIObject(java.lang.String id, java.lang.Class<T> returnClass)
Gets theAIObject
with 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
AIObject
found, or null if not.
-
getAIColony
public AIColony getAIColony(Colony colony)
Gets the AI colony corresponding to a given colony.- Parameters:
colony
- TheColony
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
- ThePlayer
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
- TheUnit
to 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
- ALogBuilder
to 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 newAIObject
for a givenFreeColGameObject
. This method gets called whenever a new object gets added to theGame
.- Specified by:
setFreeColGameObject
in interfaceFreeColGameObjectListener
- Parameters:
id
- The object identifier.fcgo
- TheFreeColGameObject
to add.- See Also:
AIObject
,FreeColGameObject
,FreeColObject.getId()
-
removeFreeColGameObject
public void removeFreeColGameObject(java.lang.String id)
Removes theAIObject
for a given AI identifier. Needed for interface FreeColGameObjectListener.- Specified by:
removeFreeColGameObject
in 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:
ownerChanged
in interfaceFreeColGameObjectListener
- Parameters:
source
- TheFreeColGameObject
that has changed.oldOwner
- The old owningPlayer
.newOwner
- The new owningPlayer
.
-
getSpecification
public Specification getSpecification()
Get the specification.- Overrides:
getSpecification
in classFreeColObject
- Returns:
- The
Specification
used by this object.
-
copyIn
public <T extends FreeColObject> boolean copyIn(T other)
Copy another FreeColObject into this one if it is compatible.- Overrides:
copyIn
in classFreeColObject
- Type Parameters:
T
- TheFreeColObject
subclass 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:
writeAttributes
in classFreeColObject
- Parameters:
xw
- TheFreeColXMLWriter
to 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:
writeChildren
in classFreeColObject
- Parameters:
xw
- TheFreeColXMLWriter
to 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:
readAttributes
in classFreeColObject
- Parameters:
xr
- TheFreeColXMLReader
to 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:
readChildren
in classFreeColObject
- Parameters:
xr
- TheFreeColXMLReader
to 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:
readChild
in classFreeColObject
- Parameters:
xr
- TheFreeColXMLReader
to 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:
getXMLTagName
in classFreeColObject
- Returns:
- The tag.
-
-