Class WorkLocation

  • All Implemented Interfaces:
    java.lang.Comparable<FreeColObject>, Location, Ownable, ObjectWithId
    Direct Known Subclasses:
    Building, ColonyTile

    public abstract class WorkLocation
    extends UnitLocation
    implements Ownable
    The WorkLocation is a place in a Colony where Units can work. The unit capacity of a WorkLocation is likely to be limited. ColonyTiles can only hold a single worker, and Buildings can hold no more than three workers, for example. WorkLocations do not store any Goods. They take any Goods they consume from the Colony, and put all Goods they produce there, too. Although the WorkLocation implements Ownable, its owner can not be changed directly, as it is always owned by the owner of the Colony.
    • Field Detail

      • logger

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

        public static final java.util.List<AbstractGoods> EMPTY_LIST
      • colony

        protected Colony colony
        The colony that contains this work location.
      • productionType

        private ProductionType productionType
        The production type of this WorkLocation.
    • Constructor Detail

      • WorkLocation

        protected WorkLocation​(Game game)
        Constructor for ServerWorkLocation.
        Parameters:
        game - The enclosing Game.
      • WorkLocation

        public WorkLocation​(Game game,
                            java.lang.String id)
        Creates a new WorkLocation with the given identifier. The object should be initialized later.
        Parameters:
        game - The enclosing Game.
        id - The object identifier.
    • Method Detail

      • setColony

        public void setColony​(Colony colony)
      • getProductionType

        public final ProductionType getProductionType()
        Get the production type.
        Returns:
        The ProductionType for this work location.
      • setProductionType

        public final void setProductionType​(ProductionType newProductionType)
        Set the prodution type.
        Parameters:
        newProductionType - The new ProductionType value.
      • getCurrentWorkType

        public GoodsType getCurrentWorkType()
        Get the current work type of any unit present. This assumes that all units in a work location are doing the same work, which is true for now.
        Returns:
        The current GoodsType being produced, or null if none.
      • updateProductionType

        public void updateProductionType()
        Update production type on the basis of the current work location type (which might change due to an upgrade) and the work type of units present.
      • getBestProductionType

        public ProductionType getBestProductionType​(boolean unattended,
                                                    GoodsType workType)
        Get the best available production type at this work location.
        Parameters:
        unattended - Whether to require unattended production.
        workType - An optional work type to require.
        Returns:
        The best available ProductionType given the argument constraints.
      • getOccupation

        public Occupation getOccupation​(Unit unit,
                                        boolean userMode)
        Gets the best occupation for a given unit at this work location.
        Parameters:
        unit - The Unit to find an Occupation for.
        userMode - If a user requested this, favour the current work type, if not favour goods that the unit requires.
        Returns:
        An Occupation for the given unit, or null if none found.
      • getOccupation

        public Occupation getOccupation​(UnitType unitType)
        Gets the best occupation for a given unit type at this work location.
        Parameters:
        unitType - An optional UnitType to find an Occupation for. If null, use the default unit type.
        Returns:
        An Occupation for the given unit, or null if none found.
      • getWorkFor

        public GoodsType getWorkFor​(Unit unit)
        Get the best work type for a unit at this work location, favouring the existing work.
        Parameters:
        unit - The Unit to find a work type for.
        Returns:
        The best work GoodsType for the unit, or null if none found.
      • setWorkFor

        public boolean setWorkFor​(Unit unit)
        Install a unit at the best occupation for it at this work location.
        Parameters:
        unit - The Unit to install.
        Returns:
        True if the installation succeeds.
      • getSuggestion

        private WorkLocation.Suggestion getSuggestion​(Unit unit,
                                                      ProductionType productionType,
                                                      GoodsType goodsType)
        Is it a good idea to produce a goods type at this work location using a better unit type?
        Parameters:
        unit - The Unit that is doing the job at present, which may be null if none is at work.
        productionType - The ProductionType to use.
        goodsType - The GoodsType to produce.
        Returns:
        A Suggestion for a better worker, or null if improvement is not worthwhile.
      • goodSuggestionCheck

        protected boolean goodSuggestionCheck​(UnitType unitType,
                                              Unit unit,
                                              GoodsType goodsType)
        Determines whether a WorkLocation is a good suggestion
        Parameters:
        unitType - The type of Unit to check.
        unit - The Unit itself.
        goodsType - The GoodsType to check.
        Returns:
        True or false, depending on whether the suggestion is valid.
      • goodSuggestionCheck

        protected boolean goodSuggestionCheck​(UnitType unitType,
                                              Unit unit,
                                              GoodsType goodsType,
                                              WorkLocation workLocation)
        Determines whether a WorkLocation is a good suggestion
        Parameters:
        unitType - The type of Unit to check.
        unit - The Unit itself.
        goodsType - The GoodsType to check.
        workLocation - The WorkLocation to check. This can be overloaded with a default parameter of "this" to call the WorkLocation instance currently in in reference.
        Returns:
        True or false, depending on whether the suggestion is valid.
      • getSuggestions

        public java.util.Map<Unit,​WorkLocation.Suggestion> getSuggestions()
        Get a map of suggestions for better or additional units.
        Returns:
        A mapping of either existing units or null (denoting adding a unit) to a Suggestion.
      • getInputs

        public java.util.stream.Stream<AbstractGoods> getInputs()
        Get the AbstractGoods consumed by this work location.
        Returns:
        A stream of AbstractGoods consumed.
      • getOutputs

        public java.util.stream.Stream<AbstractGoods> getOutputs()
        Get the AbstractGoods produced by this work location.
        Returns:
        A stream of AbstractGoods produced.
      • produces

        public boolean produces​(GoodsType goodsType)
        Does this work location produce a given type of goods?
        Parameters:
        goodsType - The GoodsType to check.
        Returns:
        True if this WorkLocation produces the given GoodsType.
      • hasInputs

        public boolean hasInputs()
        Does this work location have any inputs.
        Returns:
        True if there are any inputs.
      • hasOutputs

        public boolean hasOutputs()
        Does this work location have any outputs.
        Returns:
        True if there are any outputs.
      • canBeWorked

        public boolean canBeWorked()
        Checks if this work location can actually be worked.
        Returns:
        True if the work location can be worked.
      • canTeach

        public boolean canTeach()
        Does this work location have teaching capability?
        Returns:
        True if this is a teaching location.
        See Also:
        Ability.TEACH
      • getProductionInfo

        public ProductionInfo getProductionInfo()
        Gets the ProductionInfo for this WorkLocation from the Colony's cache.
        Returns:
        The work location ProductionInfo.
      • getProduction

        public java.util.List<AbstractGoods> getProduction()
        Gets the production at this work location.
        Returns:
        The work location production.
      • getTotalProductionOf

        public int getTotalProductionOf​(GoodsType goodsType)
        Gets the total production of a specified goods type at this work location.
        Parameters:
        goodsType - The GoodsType to check.
        Returns:
        The amount of production.
      • getMaximumProductionOf

        public int getMaximumProductionOf​(GoodsType goodsType)
        Gets the maximum production of this work location for a given goods type, assuming the current workers and input goods.
        Parameters:
        goodsType - The GoodsType to check.
        Returns:
        The maximum production of the goods at this work location.
      • getExpertUnitType

        public UnitType getExpertUnitType()
        Gets the unit type that is the expert for this work location using its first output for which an expert type can be found.
        Returns:
        The expert UnitType or null if none found.
      • getGenericPotential

        public int getGenericPotential​(GoodsType goodsType)
        Get the potential production of a given goods type using the default unit. This is useful for planning.
        Parameters:
        goodsType - The GoodsType to check.
        Returns:
        The potential production.
      • getUnitProduction

        public int getUnitProduction​(Unit unit,
                                     GoodsType goodsType)
        Gets the productivity of a unit working in this work location, considering *only* the contribution of the unit, exclusive of that of the work location. Used below, only public for the test suite.
        Parameters:
        unit - The Unit to check.
        goodsType - The GoodsType to check the production of.
        Returns:
        The maximum return from this unit.
      • getProductionOf

        public int getProductionOf​(Unit unit,
                                   GoodsType goodsType)
        Gets the production of a unit of the given type of goods.
        Parameters:
        unit - The unit to do the work.
        goodsType - The type of goods to get the production of.
        Returns:
        The production of the given type of goods.
      • getPotentialProduction

        public int getPotentialProduction​(GoodsType goodsType,
                                          UnitType unitType)
        Get the potential production of a given goods type at this location. An optional unit type to do the production may be specified, however if null the unattended production will be calculated. Usually if a unit type is specified and a unit can not be added to the work location, zero production is returned. However, this routine is intended to be used for planning purposes, so some exceptions are allowed --- the calculation proceeds: - for unclaimed tiles - when the location is currently full of units which are conditions that an AI might plausibly be able and willing to change (a case could be made for including the OCCUPIED_BY_ENEMY condition).
        Parameters:
        goodsType - The GoodsType to produce.
        unitType - The optional UnitType to do the work.
        Returns:
        The potential production with the given goods type and unit type.
      • getProductionDeficit

        public AbstractGoods getProductionDeficit​(GoodsType goodsType)
        Helper to get the production deficit for a type of goods.
        Parameters:
        goodsType - The GoodsType to check.
        Returns:
        The production deficit as AbstractGoods, or null if not in deficit.
      • getTile

        public final Tile getTile()
        Gets the Tile associated with this Location.
        Specified by:
        getTile in interface Location
        Overrides:
        getTile in class UnitLocation
        Returns:
        The Tile associated with this Location, or null if none found.
      • add

        public boolean add​(Locatable locatable)
        Adds a Locatable to this Location.
        Specified by:
        add in interface Location
        Overrides:
        add in class UnitLocation
        Parameters:
        locatable - The Locatable to add to this Location.
        Returns:
        True if the locatable was added.
      • remove

        public boolean remove​(Locatable locatable)
        Removes a Locatable from this Location.
        Specified by:
        remove in interface Location
        Overrides:
        remove in class UnitLocation
        Parameters:
        locatable - The Locatable to remove from this Location.
        Returns:
        True if the locatable was removed.
      • getColony

        public final Colony getColony()
        Get the colony at this location.
        Specified by:
        getColony in interface Location
        Overrides:
        getColony in class UnitLocation
        Returns:
        A Colony at this location if any, or null if none found.
      • getRank

        public final int getRank()
        Get a integer for this location, for the benefit of location comparators.
        Specified by:
        getRank in interface Location
        Returns:
        A suitable integer.
      • getNoAddReason

        public UnitLocation.NoAddReason getNoAddReason​(Locatable locatable)
        Gets the reason why a given Locatable can not be added to this Location. Be careful to test for unit presence last before success (NoAddReason.NONE) except perhaps for the capacity test, so that we can treat ALREADY_PRESENT as success in some cases (e.g. if the unit changes type --- does it still have a required skill?) FIXME: consider moving this up to Location?
        Overrides:
        getNoAddReason in class UnitLocation
        Parameters:
        locatable - The Locatable to test.
        Returns:
        The reason why adding would fail.
      • getLabel

        public abstract StringTemplate getLabel()
        Get a description of the work location, with any expected extra detail.
        Returns:
        A label StringTemplate for this work location.
      • isAvailable

        public abstract boolean isAvailable()
        Is this work location available?
        Returns:
        True if the work location is either current or can be claimed.
      • isCurrent

        public abstract boolean isCurrent()
        Is this a current work location of this colony?
        Returns:
        True if the work location is current.
      • getNoWorkReason

        public abstract UnitLocation.NoAddReason getNoWorkReason()
        Checks if this work location is available to the colony to be worked.
        Returns:
        The reason why/not the work location can be worked.
      • getWorkTile

        public abstract Tile getWorkTile()
        Get an associated tile, if any.
        Returns:
        The underlying Tile which is worked, if any.
      • getLevel

        public abstract int getLevel()
        Get the "level" of this work location. Note: only meaningful for buildings at present.
        Returns:
        The work location level.
      • canAutoProduce

        public abstract boolean canAutoProduce()
        Can this work location can produce goods without workers?
        Returns:
        True if this work location can produce goods without workers.
      • canProduce

        public abstract boolean canProduce​(GoodsType goodsType,
                                           UnitType unitType)
        Can this work location produce a given goods type with an optional unit.
        Parameters:
        goodsType - The GoodsType to produce.
        unitType - An optional UnitType, if null the unattended production is considered.
        Returns:
        True if this location can produce the goods.
      • getBaseProduction

        public abstract int getBaseProduction​(ProductionType productionType,
                                              GoodsType goodsType,
                                              UnitType unitType)
        Gets the base production of a given goods type optionally using a unit of a given type in this work location. That is, the production exclusive of any modifiers. If no unit type is specified, the unattended production is calculated.
        Parameters:
        productionType - An optional ProductionType to use, if null the best available one is used.
        goodsType - The GoodsType to produce.
        unitType - An optional UnitType to produce the goods.
        Returns:
        The amount of goods potentially produced.
      • getProductionModifiers

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

        public abstract java.util.List<ProductionType> getAvailableProductionTypes​(boolean unattended)
        Get the production types available for this work location.
        Parameters:
        unattended - If true, get unattended production types.
        Returns:
        A list of suitable ProductionTypes.
      • getCompetenceFactor

        public abstract float getCompetenceFactor()
        Get a work location specific factor to multiply any goods-specific unit production bonuses by.
        Returns:
        The competence factor.
      • getRebelFactor

        public abstract float getRebelFactor()
        Get a work location specific factor to multiply the colony rebel bonus by.
        Returns:
        The rebel factor.
      • evaluateFor

        public int evaluateFor​(Player player)
        Evaluate this work location for a given player. To be overridden by subclasses.
        Parameters:
        player - The Player to evaluate for.
        Returns:
        A value for the player.
      • getClaimTemplate

        public StringTemplate getClaimTemplate()
        Gets a template describing whether this work location can/needs-to be claimed. To be overridden by classes where this is meaningful. This is a default null implementation.
        Returns:
        A suitable template.
      • getOwner

        public Player getOwner()
        Gets the owner of this Ownable.
        Specified by:
        getOwner in interface Ownable
        Returns:
        The Player controlling this Ownable.
      • setOwner

        public void setOwner​(Player p)
        Sets the owner of this Ownable. Do not call this method, ever. The owner of this WorkLocation is the owner of the Colony, you must set the owner of the Colony instead.
        Specified by:
        setOwner in interface Ownable
        Parameters:
        p - The Player that should take ownership of this Ownable.
        Throws:
        java.lang.UnsupportedOperationException - is always thrown by this method.
      • copyIn

        public <T extends FreeColObject> boolean copyIn​(T other)
        Copy another FreeColObject into this one if it is compatible.
        Overrides:
        copyIn in class UnitLocation
        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 UnitLocation
        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.
      • readChild

        public 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 UnitLocation
        Parameters:
        xr - The FreeColXMLReader to read from.
        Throws:
        javax.xml.stream.XMLStreamException - if there is a problem reading the stream.