Package net.sf.freecol.server.ai
Class AIColony
- java.lang.Object
-
- net.sf.freecol.common.model.FreeColObject
-
- net.sf.freecol.server.ai.AIObject
-
- net.sf.freecol.server.ai.AIColony
-
- All Implemented Interfaces:
java.beans.PropertyChangeListener,java.lang.Comparable<FreeColObject>,java.util.EventListener,ObjectWithId
public final class AIColony extends AIObject implements java.beans.PropertyChangeListener
Objects of this class contains AI-information for a singleColony.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringAI_GOODS_LIST_TAGprivate ColonycolonyThe colony this AIColony is managing.private ColonyPlancolonyPlanThe current plan for the colony.private static intEXPORT_MINIMUMDo not bother trying to ship out less than this amount of goods.private java.util.List<AIGoods>exportGoodsGoods to export from the colony.private static intFOREST_MINIMUMDo not perform tile improvements that would leave less than this amount of forested work locations available to the colony.private static java.util.Set<GoodsType>fullExportGoods that should be completely exported and only exported to prevent the warehouse filling.private static java.lang.StringGOODS_WISH_LIST_TAGprivate static java.lang.StringLIST_ELEMENTprivate static java.util.logging.Loggerloggerprivate static java.lang.StringOLD_TILE_IMPROVEMENT_PLAN_LIST_TAGprivate static java.util.Set<GoodsType>partExportprivate static java.util.Comparator<Unit>pioneerComparatorComparator to choose the best pioneers.private TurnrearrangeTurnWhen should the workers in this Colony be rearranged?private static java.util.Comparator<Unit>scoutComparatorComparator to choose the best scouts.static java.lang.StringTAGprivate static java.lang.StringTILE_IMPROVEMENT_PLAN_LIST_TAGprivate java.util.List<TileImprovementPlan>tileImprovementPlansPlans to improve neighbouring tiles.private java.util.List<Wish>wishesUseful things for the colony.private static java.lang.StringWORKER_WISH_LIST_TAG-
Fields inherited from class net.sf.freecol.server.ai.AIObject
initialized
-
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 AIColony(AIMain aiMain, java.lang.String id)Creates a new uninitializedAIColony.AIColony(AIMain aiMain, FreeColXMLReader xr)Creates a newAIColonyfrom the given XML-representation.AIColony(AIMain aiMain, Colony colony)Creates a newAIColony.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidaddExportGoods(AIGoods aiGoods)Add to the export goods list, and resort.private voidaddTileImprovementPlan(TileImprovementPlan plan)Add a tile improvement plan.voidaddWish(Wish wish)Adds aWishto the wishes list.private booleanavertAutoDestruction()Something bad happened, there is no remaining unit working in the colony.private voidbaseInitialize()Initialize the basic fields.Constants.IntegrityTypecheckIntegrity(boolean fix, LogBuilder lb)AIObjects need integrity checking too.private voidclearExportGoods()Clear the export goods.private voidclearTileImprovementPlans()Clear the tile improvement plans.private voidclearWishes()Clear the wishes.booleancompleteWish(Goods goods, LogBuilder lb)Tries to complete any wishes for some goods that have just arrived.booleancompleteWish(Unit unit, LogBuilder lb)Tries to complete any wishes for a unit that has just arrived.booleancompleteWish(TransportableAIObject t, LogBuilder lb)Tries to complete any wishes for a transportable that has just arrived.booleancompleteWish(Wish wish, java.lang.String reason, LogBuilder lb)Tries to complete a supplied wish.voiddispose()Disposes thisAIColony.private voiddropExportGoods(AIGoods ag)Drops some goods from the goods list, and cancels any transport.private voidexploreLCRs()Explores any neighbouring LCRs.protected AIPlayergetAIOwner()protected AIUnitgetAIUnit(Unit unit)ColonygetColony()Gets theColonythisAIColonycontrols.java.util.List<AIGoods>getExportGoods()Gets the goods to be exported from this AI colony.java.util.List<GoodsWish>getGoodsWishes()Gets the goods wishes this colony has.private TileImprovementPlangetPlanFor(Tile tile, java.util.List<TileImprovementPlan> plans)Gets the first plan for a specified tile from a list of tile improvement plans.java.util.List<BuildableType>getPlannedBuildableTypes()Get the list of buildables in the colony plan.java.util.List<TileImprovementPlan>getTileImprovementPlans()Gets the tile improvements planned for this colony.java.util.List<Wish>getWishes()Gets the wishes this colony has.java.util.List<WorkerWish>getWorkerWishes()Gets the worker wishes this colony has.java.lang.StringgetXMLTagName()Get the serialization tag for this object.private voidgoodsLog(AIGoods ag, java.lang.String action, LogBuilder lb)Emits a standard message regarding the state of AIGoods.booleanisBadlyDefended()Is this AI colony badly defended?java.lang.StringplanToString()Summarize the colony plan as a string.voidpropertyChange(java.beans.PropertyChangeEvent event)Handle REARRANGE_WORKERS property change events.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.java.util.Set<AIUnit>rearrangeColony(LogBuilder lb)Rearranges the workers within this colony using theColonyPlan.voidremoveExportGoods(AIGoods ag)Removes the givenAIGoodsfrom the export goods for this colony.booleanremoveTileImprovementPlan(TileImprovementPlan plan)Removes aTileImprovementPlanfrom the listvoidrequestRearrange()Sets the rearrangeTurn variable such that rearrangeColony will run fully next time it is invoked.private voidrequireGoodsWish(GoodsType type, int amount, int value, LogBuilder lb)Requires a goods wish for a specified goods type be present at this AI colony.private voidrequireWorkerWish(UnitType type, boolean expertNeeded, int value, LogBuilder lb)Requires a worker wish for a unit type to be provided to this AIColony.private voidresetExports()Reset the export settings.private voidsetExportGoods(java.util.List<AIGoods> aiGoods)Set the export goods list.voidsetInitialized()Set the initialized flag in this object.private voidsortExportGoods()Sort the export goods.private voidstealTiles(LogBuilder lb)Steals neighbouring tiles but only if the colony has some defence.voidstopUsing(WorkLocation wl)Stop using a work location.voidupdate(LogBuilder lb)Update any relatively static properties of the colony: - export state - disposition of export goods in this colony - tile improvements (might ignore freshly grabbed tiles) - wishesprivate voidupdateExportGoods(LogBuilder lb)Creates a list of the goods which should be shipped out of this colony.private voidupdateGoodsWishes(LogBuilder lb)Updates the goods wishes.voidupdateTileImprovementPlans(LogBuilder lb)Creates a list of theTile-improvements which will increase the production by thisColony.private voidupdateWishes(LogBuilder lb)Updates the wishes for theColony.private voidupdateWorkerWishes(LogBuilder lb)Updates the worker wishes.protected voidwriteChildren(FreeColXMLWriter xw)Write the children of this object to a stream.-
Methods inherited from class net.sf.freecol.server.ai.AIObject
addAIObjectWithId, checkIntegrity, copyIn, getAIMain, getGame, getSpecification, isDisposed, setGame, setSpecification
-
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, setId, toString, toXML, toXML, toXMLPartial, toXMLPartial, writeAttributes
-
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
TAG
public static final java.lang.String TAG
- See Also:
- Constant Field Values
-
LIST_ELEMENT
private static final java.lang.String LIST_ELEMENT
- See Also:
- Constant Field Values
-
FOREST_MINIMUM
private static final int FOREST_MINIMUM
Do not perform tile improvements that would leave less than this amount of forested work locations available to the colony.- See Also:
- Constant Field Values
-
EXPORT_MINIMUM
private static final int EXPORT_MINIMUM
Do not bother trying to ship out less than this amount of goods.- See Also:
- Constant Field Values
-
pioneerComparator
private static final java.util.Comparator<Unit> pioneerComparator
Comparator to choose the best pioneers.
-
scoutComparator
private static final java.util.Comparator<Unit> scoutComparator
Comparator to choose the best scouts.
-
fullExport
private static final java.util.Set<GoodsType> fullExport
Goods that should be completely exported and only exported to prevent the warehouse filling.
-
partExport
private static final java.util.Set<GoodsType> partExport
-
colony
private Colony colony
The colony this AIColony is managing.
-
colonyPlan
private ColonyPlan colonyPlan
The current plan for the colony. Does not need to be serialized.
-
exportGoods
private java.util.List<AIGoods> exportGoods
Goods to export from the colony.
-
wishes
private java.util.List<Wish> wishes
Useful things for the colony.
-
tileImprovementPlans
private java.util.List<TileImprovementPlan> tileImprovementPlans
Plans to improve neighbouring tiles.
-
rearrangeTurn
private Turn rearrangeTurn
When should the workers in this Colony be rearranged?
-
AI_GOODS_LIST_TAG
private static final java.lang.String AI_GOODS_LIST_TAG
- See Also:
- Constant Field Values
-
GOODS_WISH_LIST_TAG
private static final java.lang.String GOODS_WISH_LIST_TAG
- See Also:
- Constant Field Values
-
TILE_IMPROVEMENT_PLAN_LIST_TAG
private static final java.lang.String TILE_IMPROVEMENT_PLAN_LIST_TAG
- See Also:
- Constant Field Values
-
WORKER_WISH_LIST_TAG
private static final java.lang.String WORKER_WISH_LIST_TAG
- See Also:
- Constant Field Values
-
OLD_TILE_IMPROVEMENT_PLAN_LIST_TAG
private static final java.lang.String OLD_TILE_IMPROVEMENT_PLAN_LIST_TAG
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
AIColony
public AIColony(AIMain aiMain, java.lang.String id)
Creates a new uninitializedAIColony.- Parameters:
aiMain- The main AI-object.id- The object identifier.
-
AIColony
public AIColony(AIMain aiMain, Colony colony)
Creates a newAIColony.- Parameters:
aiMain- The main AI-object.colony- The colony to make anAIObjectfor.
-
AIColony
public AIColony(AIMain aiMain, FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Creates a newAIColonyfrom the given XML-representation.- Parameters:
aiMain- The main AI-object.xr- The input stream containing the XML.- Throws:
javax.xml.stream.XMLStreamException- if a problem was encountered during parsing.
-
-
Method Detail
-
baseInitialize
private void baseInitialize()
Initialize the basic fields.
-
setInitialized
public void setInitialized()
Set the initialized flag in this object. To be implemented by leaf classes, and called in their constructors plus the special case in readChild below where we resolve forward references.- Specified by:
setInitializedin classAIObject
-
getColony
public final Colony getColony()
Gets theColonythisAIColonycontrols.- Returns:
- The
Colony.
-
getAIOwner
protected AIPlayer getAIOwner()
-
isBadlyDefended
public boolean isBadlyDefended()
Is this AI colony badly defended?- Returns:
- True if this colony needs more defenders.
-
update
public void update(LogBuilder lb)
Update any relatively static properties of the colony: - export state - disposition of export goods in this colony - tile improvements (might ignore freshly grabbed tiles) - wishes- Parameters:
lb- ALogBuilderto log to.
-
rearrangeColony
public java.util.Set<AIUnit> rearrangeColony(LogBuilder lb)
Rearranges the workers within this colony using theColonyPlan. FIXME: Detect military threats and boost defence.- Parameters:
lb- ALogBuilderto log to.- Returns:
- A set of
AIUnits that changed their work state.
-
resetExports
private void resetExports()
Reset the export settings. This is always needed even when there is no customs house, because updateExportGoods needs to know what to export by transport. FIXME: consider market prices?
-
exploreLCRs
private void exploreLCRs()
Explores any neighbouring LCRs. Choose non-expert persons for the exploration.
-
stealTiles
private void stealTiles(LogBuilder lb)
Steals neighbouring tiles but only if the colony has some defence. Grab everything if at war with the owner, otherwise just take the tile that best helps with the currently required raw building materials, with a lesser interest in food.- Parameters:
lb- ALogBuilderto log to.
-
avertAutoDestruction
private boolean avertAutoDestruction()
Something bad happened, there is no remaining unit working in the colony. Throwing an exception stalls the AI and wrecks the colony in a weird way. Try to recover by hopefully finding a unit outside the colony and stuffing it into the town hall.- Returns:
- True if autodestruction has been averted.
-
stopUsing
public void stopUsing(WorkLocation wl)
Stop using a work location. Called from BuildColonyMission to clear a colony tile that is about to have a colony built on it.- Parameters:
wl- TheWorkLocationto stop using.
-
getExportGoods
public java.util.List<AIGoods> getExportGoods()
Gets the goods to be exported from this AI colony.- Returns:
- A copy of the exportGoods list.
-
clearExportGoods
private void clearExportGoods()
Clear the export goods.
-
addExportGoods
private void addExportGoods(AIGoods aiGoods)
Add to the export goods list, and resort.- Parameters:
aiGoods- TheAIGoodsto add.
-
setExportGoods
private void setExportGoods(java.util.List<AIGoods> aiGoods)
Set the export goods list.- Parameters:
aiGoods- The new list ofAIGoods.
-
sortExportGoods
private void sortExportGoods()
Sort the export goods.
-
removeExportGoods
public void removeExportGoods(AIGoods ag)
Removes the givenAIGoodsfrom the export goods for this colony. TheAIGoods-object is not disposed as part of this operation. Use dropExportGoods instead to remove the object completely (this method would then be called indirectly).- Parameters:
ag- TheAIGoodsto be removed.- See Also:
AIGoods.dispose()
-
dropExportGoods
private void dropExportGoods(AIGoods ag)
Drops some goods from the goods list, and cancels any transport.- Parameters:
ag- TheAIGoodsto drop.
-
goodsLog
private void goodsLog(AIGoods ag, java.lang.String action, LogBuilder lb)
Emits a standard message regarding the state of AIGoods.- Parameters:
ag- TheAIGoodsto log.action- The state of the goods.lb- ALogBuilderto log to.
-
updateExportGoods
private void updateExportGoods(LogBuilder lb)
Creates a list of the goods which should be shipped out of this colony.- Parameters:
lb- ALogBuilderto log to.
-
clearWishes
private void clearWishes()
Clear the wishes.
-
addWish
public void addWish(Wish wish)
Adds aWishto the wishes list.- Parameters:
wish- TheWishto be added.
-
completeWish
public boolean completeWish(Wish wish, java.lang.String reason, LogBuilder lb)
Tries to complete a supplied wish.- Parameters:
wish- TheWishto complete.reason- A reason for wish completion.lb- ALogBuilderto log to.- Returns:
- True if this wish was successfully completed.
-
completeWish
public boolean completeWish(Goods goods, LogBuilder lb)
Tries to complete any wishes for some goods that have just arrived.- Parameters:
goods- SomeGoodsthat are arriving in this colony.lb- ALogBuilderto log to.- Returns:
- True if a wish was successfully completed.
-
completeWish
public boolean completeWish(Unit unit, LogBuilder lb)
Tries to complete any wishes for a unit that has just arrived.- Parameters:
unit- AUnitthat is arriving in this colony.lb- ALogBuilderto log to.- Returns:
- True if a wish was successfully completed.
-
completeWish
public boolean completeWish(TransportableAIObject t, LogBuilder lb)
Tries to complete any wishes for a transportable that has just arrived.- Parameters:
t- The arrivingTransportableAIObject.lb- ALogBuilderto log to.- Returns:
- True if a wish was successfully completed.
-
getWishes
public java.util.List<Wish> getWishes()
Gets the wishes this colony has.- Returns:
- A copy of the wishes list.
-
getGoodsWishes
public java.util.List<GoodsWish> getGoodsWishes()
Gets the goods wishes this colony has.- Returns:
- A copy of the wishes list with non-goods wishes removed.
-
getWorkerWishes
public java.util.List<WorkerWish> getWorkerWishes()
Gets the worker wishes this colony has.- Returns:
- A copy of the wishes list with non-worker wishes removed.
-
requireGoodsWish
private void requireGoodsWish(GoodsType type, int amount, int value, LogBuilder lb)
Requires a goods wish for a specified goods type be present at this AI colony. If one is already present, the amount specified here takes precedence as it is more likely to be up to date. The value is treated as a minimum requirement.- Parameters:
type- TheGoodsTypeto wish for.amount- The amount of goods wished for.value- The urgency of the wish.lb- ALogBuilderto log to.
-
requireWorkerWish
private void requireWorkerWish(UnitType type, boolean expertNeeded, int value, LogBuilder lb)
Requires a worker wish for a unit type to be provided to this AIColony. If a suitable wish is already present, the expert and value parameters take precedence as they are more likely to be up to date.- Parameters:
type- TheUnitTypeto wish for.expertNeeded- Is an expert unit required?value- The urgency of the wish.lb- ALogBuilderto log to.
-
updateWishes
private void updateWishes(LogBuilder lb)
Updates the wishes for theColony.- Parameters:
lb- ALogBuilderto log to.
-
updateWorkerWishes
private void updateWorkerWishes(LogBuilder lb)
Updates the worker wishes.- Parameters:
lb- ALogBuilderto log to.
-
updateGoodsWishes
private void updateGoodsWishes(LogBuilder lb)
Updates the goods wishes.- Parameters:
lb- ALogBuilderto log to.
-
clearTileImprovementPlans
private void clearTileImprovementPlans()
Clear the tile improvement plans.
-
addTileImprovementPlan
private void addTileImprovementPlan(TileImprovementPlan plan)
Add a tile improvement plan.- Parameters:
plan- The newTileImprovementPlanto add.
-
getTileImprovementPlans
public java.util.List<TileImprovementPlan> getTileImprovementPlans()
Gets the tile improvements planned for this colony.- Returns:
- A copy of the tile improvement plan list.
-
removeTileImprovementPlan
public boolean removeTileImprovementPlan(TileImprovementPlan plan)
Removes aTileImprovementPlanfrom the list- Parameters:
plan- TheTileImprovementPlanto remove.- Returns:
- True if it was successfully deleted, false otherwise
-
getPlanFor
private TileImprovementPlan getPlanFor(Tile tile, java.util.List<TileImprovementPlan> plans)
Gets the first plan for a specified tile from a list of tile improvement plans.- Parameters:
tile- TheTileto look for.plans- A list ofTileImprovementPlans to search.- Returns:
- A matching plan, or null if not found.
-
updateTileImprovementPlans
public void updateTileImprovementPlans(LogBuilder lb)
Creates a list of theTile-improvements which will increase the production by thisColony. Public for the test suite.- Parameters:
lb- ALogBuilderto log to.- See Also:
TileImprovementPlan
-
getPlannedBuildableTypes
public java.util.List<BuildableType> getPlannedBuildableTypes()
Get the list of buildables in the colony plan. Public for the test suite.- Returns:
- A list of planned
BuildableType.
-
planToString
public java.lang.String planToString()
Summarize the colony plan as a string.- Returns:
- A summary of the colony plan.
-
propertyChange
public void propertyChange(java.beans.PropertyChangeEvent event)
Handle REARRANGE_WORKERS property change events.- Specified by:
propertyChangein interfacejava.beans.PropertyChangeListener- Parameters:
event- ThePropertyChangeEvent.
-
requestRearrange
public void requestRearrange()
Sets the rearrangeTurn variable such that rearrangeColony will run fully next time it is invoked.
-
checkIntegrity
public Constants.IntegrityType checkIntegrity(boolean fix, LogBuilder lb)
AIObjects need integrity checking too.- Overrides:
checkIntegrityin classAIObject- 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.
-
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.
-
-