Class TileItemContainer

    • Field Detail

      • logger

        private static final java.util.logging.Logger logger
      • tile

        private Tile tile
        The tile owner for which this is the container.
      • tileItems

        private final java.util.List<TileItem> tileItems
        All tile items, sorted by zIndex.
      • OLD_TILE_IMPROVEMENT_TAG

        private static final java.lang.String OLD_TILE_IMPROVEMENT_TAG
        See Also:
        Constant Field Values
    • Constructor Detail

      • TileItemContainer

        public TileItemContainer​(Game game,
                                 Tile tile)
        Create an empty TileItemContainer.
        Parameters:
        game - The enclosing Game.
        tile - The Tile this TileItemContainer contains TileItems for.
      • TileItemContainer

        public TileItemContainer​(Game game,
                                 java.lang.String id)
        Create a new TileItemContainer.
        Parameters:
        game - The enclosing Game.
        id - The object identifier.
    • Method Detail

      • getTile

        public final Tile getTile()
        Get the tile this container belongs to.
        Returns:
        The owning Tile.
      • invalidateCache

        private void invalidateCache()
        Invalidate the production cache of the owning colony if any but only if the tile is actually being used.
      • getTileItems

        private java.util.List<TileItem> getTileItems()
        Get a copy of the tile items list.
        Returns:
        A copy of the TileItems.
      • clearTileItems

        private void clearTileItems()
        Clear the tile items list.
      • setTileItems

        public final void setTileItems​(java.util.List<TileItem> newTileItems)
        Set the tile items.
        Parameters:
        newTileItems - The new tile items list.
      • addTileItem

        private final void addTileItem​(TileItem item)
        Add a tile item to this container.
        Parameters:
        item - The TileItem to add.
      • tryAddTileItem

        public final TileItem tryAddTileItem​(TileItem item)
        Try to add a TileItem to this container.
        Parameters:
        item - The TileItem to add to this container.
        Returns:
        The added TileItem or the existing TileItem if of higher magnitude, or null on error.
      • removeTileItem

        public final <T extends TileItem> T removeTileItem​(T item)
        Removes a TileItem from this container.
        Type Parameters:
        T - The actual TileItem type.
        Parameters:
        item - The TileItem to remove from this container.
        Returns:
        The TileItem that has been removed from this container (if any).
      • removeAll

        public final <T extends TileItem> void removeAll​(java.lang.Class<T> c)
        Removes all tile items of a given class.
        Type Parameters:
        T - The actual TileItem type.
        Parameters:
        c - The Class to remove.
      • getCompleteItems

        public java.util.List<TileItem> getCompleteItems()
        Get the completed tile items.
        Returns:
        A list of complete TileItems.
      • getImprovement

        public TileImprovement getImprovement​(TileImprovementType type)
        Gets the tile improvement of the given type if any.
        Parameters:
        type - The TileImprovementType to look for.
        Returns:
        The TileImprovement of the given type if present, otherwise null.
      • getImprovements

        private java.util.List<TileImprovement> getImprovements​(boolean completedOnly)
        Gets a list of the TileImprovements in this TileItemContainer.
        Parameters:
        completedOnly - If true select only the completed improvements.
        Returns:
        A list of TileImprovements.
      • getImprovements

        public java.util.List<TileImprovement> getImprovements()
        Gets a list of the TileImprovements in this TileItemContainer.
        Returns:
        A list of TileImprovements.
      • getCompleteImprovements

        public java.util.List<TileImprovement> getCompleteImprovements()
        Gets a list of the completed TileImprovements in this TileItemContainer.
        Returns:
        A list of TileImprovements.
      • findTileItem

        private TileItem findTileItem​(java.util.function.Predicate<TileItem> pred)
        Find a tile item matching a predicate.
        Parameters:
        pred - The Predicate to match.
        Returns:
        The TileItem found, or null if none present.
      • getLostCityRumour

        public final LostCityRumour getLostCityRumour()
        Gets any lost city rumour in this container.
        Returns:
        A LostCityRumour item if any, or null if not found.
      • getResource

        public final Resource getResource()
        Gets any resource item.
        Returns:
        A Resource item, or null is none found.
      • getRoad

        public TileImprovement getRoad()
        Gets any road improvement in this container.
        Returns:
        A road TileImprovement if any, or null if not found.
      • getRiver

        public TileImprovement getRiver()
        Gets any river improvement in this container.
        Returns:
        A river TileImprovement if any, or null if not found.
      • hasImprovement

        public boolean hasImprovement​(TileImprovementType type)
        Check whether this tile has a completed improvement of the given type.
        Parameters:
        type - The TileImprovementType to check for.
        Returns:
        Whether the tile has the improvement and the improvement is completed.
      • removeIncompatibleImprovements

        public void removeIncompatibleImprovements()
        Remove improvements incompatible with the given TileType. This method is called whenever the type of the container's tile changes, i.e. due to clearing.
      • getTotalBonusPotential

        public int getTotalBonusPotential​(GoodsType goodsType,
                                          UnitType unitType,
                                          int potential,
                                          boolean onlyNatural)
        Determine the total bonus for a goods type. Checks resources and all improvements, unless onlyNatural is true, in which case only natural improvements will be considered. This is not used for normal production, but is necessary to calculate colony center tile secondary production, which does not profit from artificial improvements such as plowing. It is also used to assess which goods are likely to be most productive on a tile.
        Parameters:
        goodsType - The GoodsType to check.
        unitType - The UnitType to check.
        potential - The base potential production.
        onlyNatural - Only allow natural improvements.
        Returns:
        The resulting production.
      • getProductionModifiers

        public java.util.stream.Stream<Modifier> getProductionModifiers​(GoodsType goodsType,
                                                                        UnitType unitType)
        Gets the production modifiers for the given type of goods and unit.
        Parameters:
        goodsType - The GoodsType to produce.
        unitType - The optional unitType to produce them.
        Returns:
        A stream of the applicable Modifiers.
      • canProduce

        public boolean canProduce​(GoodsType goodsType,
                                  UnitType unitType)
        Does this container contain an item that allows the tile to produce a goods type?
        Parameters:
        goodsType - The GoodsType to produce.
        unitType - The optional unitType to produce with.
        Returns:
        True if this container allows the goods type to be produced.
      • getMoveCost

        public int getMoveCost​(Tile fromTile,
                               Tile targetTile,
                               int basicMoveCost)
        Determine the movement cost to this Tile from another Tile. Does not consider special unit abilities.
        Parameters:
        fromTile - The Tile to move from.
        targetTile - The Tile to move to.
        basicMoveCost - The basic cost.
        Returns:
        The movement cost.
      • copyFrom

        public void copyFrom​(TileItemContainer tic,
                             Map.Layer layer)
        Copy the tile items from another TileItemContainer, observing a layer maximum. Note that some types need to be looked up in the spec as the tic parameter might be an import from a different game.
        Parameters:
        tic - The TileItemContainer to copy from.
        layer - The maximum Layer to import from.
      • contains

        public boolean contains​(TileItem t)
        Checks if the specified TileItem is in this container.
        Parameters:
        t - The TileItem to test the presence of.
        Returns:
        True if the tile item is present.
      • copyIn

        public <T extends FreeColObject> boolean copyIn​(T other)
        Copy another FreeColObject into this one if it is compatible.
        Overrides:
        copyIn in class FreeColGameObject
        Type Parameters:
        T - The FreeColObject 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 class FreeColObject
        Parameters:
        xw - The FreeColXMLWriter 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 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 FreeColGameObject
        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.