Class 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 single Colony.
    • Field Detail

      • logger

        private static final java.util.logging.Logger logger
      • 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 uninitialized AIColony.
        Parameters:
        aiMain - The main AI-object.
        id - The object identifier.
      • AIColony

        public AIColony​(AIMain aiMain,
                        Colony colony)
        Creates a new AIColony.
        Parameters:
        aiMain - The main AI-object.
        colony - The colony to make an AIObject for.
      • AIColony

        public AIColony​(AIMain aiMain,
                        FreeColXMLReader xr)
                 throws javax.xml.stream.XMLStreamException
        Creates a new AIColony 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 class AIObject
      • getColony

        public final Colony getColony()
        Gets the Colony this AIColony controls.
        Returns:
        The Colony.
      • getAIUnit

        protected AIUnit getAIUnit​(Unit unit)
      • 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 - A LogBuilder to log to.
      • rearrangeColony

        public java.util.Set<AIUnit> rearrangeColony​(LogBuilder lb)
        Rearranges the workers within this colony using the ColonyPlan. FIXME: Detect military threats and boost defence.
        Parameters:
        lb - A LogBuilder to 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 - A LogBuilder 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 - The WorkLocation 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 - The AIGoods to add.
      • setExportGoods

        private void setExportGoods​(java.util.List<AIGoods> aiGoods)
        Set the export goods list.
        Parameters:
        aiGoods - The new list of AIGoods.
      • sortExportGoods

        private void sortExportGoods()
        Sort the export goods.
      • removeExportGoods

        public void removeExportGoods​(AIGoods ag)
        Removes the given AIGoods from the export goods for this colony. The AIGoods-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 - The AIGoods 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 - The AIGoods 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 - The AIGoods to log.
        action - The state of the goods.
        lb - A LogBuilder 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 - A LogBuilder to log to.
      • clearWishes

        private void clearWishes()
        Clear the wishes.
      • addWish

        public void addWish​(Wish wish)
        Adds a Wish to the wishes list.
        Parameters:
        wish - The Wish to be added.
      • completeWish

        public boolean completeWish​(Wish wish,
                                    java.lang.String reason,
                                    LogBuilder lb)
        Tries to complete a supplied wish.
        Parameters:
        wish - The Wish to complete.
        reason - A reason for wish completion.
        lb - A LogBuilder 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 - Some Goods that are arriving in this colony.
        lb - A LogBuilder 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 - A Unit that is arriving in this colony.
        lb - A LogBuilder 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 arriving TransportableAIObject.
        lb - A LogBuilder 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 - The GoodsType to wish for.
        amount - The amount of goods wished for.
        value - The urgency of the wish.
        lb - A LogBuilder 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 - The UnitType to wish for.
        expertNeeded - Is an expert unit required?
        value - The urgency of the wish.
        lb - A LogBuilder to log to.
      • updateWishes

        private void updateWishes​(LogBuilder lb)
        Updates the wishes for the Colony.
        Parameters:
        lb - A LogBuilder to log to.
      • updateWorkerWishes

        private void updateWorkerWishes​(LogBuilder lb)
        Updates the worker wishes.
        Parameters:
        lb - A LogBuilder to log to.
      • updateGoodsWishes

        private void updateGoodsWishes​(LogBuilder lb)
        Updates the goods wishes.
        Parameters:
        lb - A LogBuilder 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 new TileImprovementPlan 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 a TileImprovementPlan from the list
        Parameters:
        plan - The TileImprovementPlan 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 - The Tile to look for.
        plans - A list of TileImprovementPlans to search.
        Returns:
        A matching plan, or null if not found.
      • updateTileImprovementPlans

        public void updateTileImprovementPlans​(LogBuilder lb)
        Creates a list of the Tile-improvements which will increase the production by this Colony. Public for the test suite.
        Parameters:
        lb - A LogBuilder 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 interface java.beans.PropertyChangeListener
        Parameters:
        event - The PropertyChangeEvent.
      • requestRearrange

        public void requestRearrange()
        Sets the rearrangeTurn variable such that rearrangeColony will run fully next time it is invoked.
      • dispose

        public void dispose()
        Disposes this AIColony.
        Overrides:
        dispose in class AIObject
      • checkIntegrity

        public Constants.IntegrityType checkIntegrity​(boolean fix,
                                                      LogBuilder lb)
        AIObjects need integrity checking too.
        Overrides:
        checkIntegrity in class AIObject
        Parameters:
        fix - If true, fix problems if possible.
        lb - A LogBuilder 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 class FreeColObject
        Parameters:
        xw - The FreeColXMLWriter 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 class FreeColObject
        Parameters:
        xr - The FreeColXMLReader 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 class FreeColObject
        Parameters:
        xr - The FreeColXMLReader 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 class FreeColObject
        Parameters:
        xr - The FreeColXMLReader 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 class FreeColObject
        Returns:
        The tag.