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.String
AI_GOODS_LIST_TAG
private Colony
colony
The colony this AIColony is managing.private ColonyPlan
colonyPlan
The current plan for the colony.private static int
EXPORT_MINIMUM
Do not bother trying to ship out less than this amount of goods.private java.util.List<AIGoods>
exportGoods
Goods to export from the colony.private static int
FOREST_MINIMUM
Do 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>
fullExport
Goods that should be completely exported and only exported to prevent the warehouse filling.private static java.lang.String
GOODS_WISH_LIST_TAG
private static java.lang.String
LIST_ELEMENT
private static java.util.logging.Logger
logger
private static java.lang.String
OLD_TILE_IMPROVEMENT_PLAN_LIST_TAG
private static java.util.Set<GoodsType>
partExport
private static java.util.Comparator<Unit>
pioneerComparator
Comparator to choose the best pioneers.private Turn
rearrangeTurn
When should the workers in this Colony be rearranged?private static java.util.Comparator<Unit>
scoutComparator
Comparator to choose the best scouts.static java.lang.String
TAG
private static java.lang.String
TILE_IMPROVEMENT_PLAN_LIST_TAG
private java.util.List<TileImprovementPlan>
tileImprovementPlans
Plans to improve neighbouring tiles.private java.util.List<Wish>
wishes
Useful things for the colony.private static java.lang.String
WORKER_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 newAIColony
from 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 void
addExportGoods(AIGoods aiGoods)
Add to the export goods list, and resort.private void
addTileImprovementPlan(TileImprovementPlan plan)
Add a tile improvement plan.void
addWish(Wish wish)
Adds aWish
to the wishes list.private boolean
avertAutoDestruction()
Something bad happened, there is no remaining unit working in the colony.private void
baseInitialize()
Initialize the basic fields.Constants.IntegrityType
checkIntegrity(boolean fix, LogBuilder lb)
AIObjects need integrity checking too.private void
clearExportGoods()
Clear the export goods.private void
clearTileImprovementPlans()
Clear the tile improvement plans.private void
clearWishes()
Clear the wishes.boolean
completeWish(Goods goods, LogBuilder lb)
Tries to complete any wishes for some goods that have just arrived.boolean
completeWish(Unit unit, LogBuilder lb)
Tries to complete any wishes for a unit that has just arrived.boolean
completeWish(TransportableAIObject t, LogBuilder lb)
Tries to complete any wishes for a transportable that has just arrived.boolean
completeWish(Wish wish, java.lang.String reason, LogBuilder lb)
Tries to complete a supplied wish.void
dispose()
Disposes thisAIColony
.private void
dropExportGoods(AIGoods ag)
Drops some goods from the goods list, and cancels any transport.private void
exploreLCRs()
Explores any neighbouring LCRs.protected AIPlayer
getAIOwner()
protected AIUnit
getAIUnit(Unit unit)
Colony
getColony()
Gets theColony
thisAIColony
controls.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 TileImprovementPlan
getPlanFor(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.String
getXMLTagName()
Get the serialization tag for this object.private void
goodsLog(AIGoods ag, java.lang.String action, LogBuilder lb)
Emits a standard message regarding the state of AIGoods.boolean
isBadlyDefended()
Is this AI colony badly defended?java.lang.String
planToString()
Summarize the colony plan as a string.void
propertyChange(java.beans.PropertyChangeEvent event)
Handle REARRANGE_WORKERS property change events.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.java.util.Set<AIUnit>
rearrangeColony(LogBuilder lb)
Rearranges the workers within this colony using theColonyPlan
.void
removeExportGoods(AIGoods ag)
Removes the givenAIGoods
from the export goods for this colony.boolean
removeTileImprovementPlan(TileImprovementPlan plan)
Removes aTileImprovementPlan
from the listvoid
requestRearrange()
Sets the rearrangeTurn variable such that rearrangeColony will run fully next time it is invoked.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.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.private void
resetExports()
Reset the export settings.private void
setExportGoods(java.util.List<AIGoods> aiGoods)
Set the export goods list.void
setInitialized()
Set the initialized flag in this object.private void
sortExportGoods()
Sort the export goods.private void
stealTiles(LogBuilder lb)
Steals neighbouring tiles but only if the colony has some defence.void
stopUsing(WorkLocation wl)
Stop using a work location.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) - wishesprivate void
updateExportGoods(LogBuilder lb)
Creates a list of the goods which should be shipped out of this colony.private void
updateGoodsWishes(LogBuilder lb)
Updates the goods wishes.void
updateTileImprovementPlans(LogBuilder lb)
Creates a list of theTile
-improvements which will increase the production by thisColony
.private void
updateWishes(LogBuilder lb)
Updates the wishes for theColony
.private void
updateWorkerWishes(LogBuilder lb)
Updates the worker wishes.protected void
writeChildren(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 anAIObject
for.
-
AIColony
public AIColony(AIMain aiMain, FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Creates a newAIColony
from 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:
setInitialized
in classAIObject
-
getColony
public final Colony getColony()
Gets theColony
thisAIColony
controls.- 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
- ALogBuilder
to 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
- ALogBuilder
to log to.- Returns:
- A set of
AIUnit
s 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
- ALogBuilder
to 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
- TheWorkLocation
to 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
- TheAIGoods
to 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 givenAIGoods
from 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
- TheAIGoods
to 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
- TheAIGoods
to drop.
-
goodsLog
private void goodsLog(AIGoods ag, java.lang.String action, LogBuilder lb)
Emits a standard message regarding the state of AIGoods.- Parameters:
ag
- TheAIGoods
to log.action
- The state of the goods.lb
- ALogBuilder
to log to.
-
updateExportGoods
private void updateExportGoods(LogBuilder lb)
Creates a list of the goods which should be shipped out of this colony.- Parameters:
lb
- ALogBuilder
to log to.
-
clearWishes
private void clearWishes()
Clear the wishes.
-
addWish
public void addWish(Wish wish)
Adds aWish
to the wishes list.- Parameters:
wish
- TheWish
to be added.
-
completeWish
public boolean completeWish(Wish wish, java.lang.String reason, LogBuilder lb)
Tries to complete a supplied wish.- Parameters:
wish
- TheWish
to complete.reason
- A reason for wish completion.lb
- ALogBuilder
to 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
- SomeGoods
that are arriving in this colony.lb
- ALogBuilder
to 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
- AUnit
that is arriving in this colony.lb
- ALogBuilder
to 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
- ALogBuilder
to 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
- TheGoodsType
to wish for.amount
- The amount of goods wished for.value
- The urgency of the wish.lb
- ALogBuilder
to 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
- TheUnitType
to wish for.expertNeeded
- Is an expert unit required?value
- The urgency of the wish.lb
- ALogBuilder
to log to.
-
updateWishes
private void updateWishes(LogBuilder lb)
Updates the wishes for theColony
.- Parameters:
lb
- ALogBuilder
to log to.
-
updateWorkerWishes
private void updateWorkerWishes(LogBuilder lb)
Updates the worker wishes.- Parameters:
lb
- ALogBuilder
to log to.
-
updateGoodsWishes
private void updateGoodsWishes(LogBuilder lb)
Updates the goods wishes.- Parameters:
lb
- ALogBuilder
to 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 newTileImprovementPlan
to 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 aTileImprovementPlan
from the list- Parameters:
plan
- TheTileImprovementPlan
to 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
- TheTile
to look for.plans
- A list ofTileImprovementPlan
s 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
- ALogBuilder
to 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:
propertyChange
in 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:
checkIntegrity
in classAIObject
- 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.
-
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.
-
-