Class TransportMission

  • All Implemented Interfaces:
    java.lang.Comparable<FreeColObject>, ObjectWithId

    public final class TransportMission
    extends Mission
    Mission for transporting units and goods on a carrier.
    See Also:
    Unit
    • Field Detail

      • logger

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

        private static final int DESTINATION_UPPER_BOUND
        Insist transport lists remain simple by imposing an upper bound on the distinct destination locations to visit.
        See Also:
        Constant Field Values
      • MINIMUM_GOLD_TO_STAY_IN_EUROPE

        private static final int MINIMUM_GOLD_TO_STAY_IN_EUROPE
        See Also:
        Constant Field Values
      • cargoes

        private final java.util.List<Cargo> cargoes
        A list of Cargos to work on.
      • target

        private Location target
        The current target location to travel to.
    • Constructor Detail

      • TransportMission

        public TransportMission​(AIMain aiMain,
                                AIUnit aiUnit)
        Creates a mission for the given AIUnit.
        Parameters:
        aiMain - The main AI-object.
        aiUnit - The AIUnit this mission is created for.
      • TransportMission

        public TransportMission​(AIMain aiMain,
                                AIUnit aiUnit,
                                FreeColXMLReader xr)
                         throws javax.xml.stream.XMLStreamException
        Creates a new TransportMission from a reader.
        Parameters:
        aiMain - The main AI-object.
        aiUnit - The AIUnit this mission is created for.
        xr - The input stream containing the XML.
        Throws:
        javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.
        See Also:
        FreeColObject.readFromXML(net.sf.freecol.common.io.FreeColXMLReader)
    • Method Detail

      • dispose

        public void dispose()
        Disposes of this Mission.
        Overrides:
        dispose in class Mission
      • isCarrying

        private boolean isCarrying​(TransportableAIObject t)
        Checks if the carrier using this mission is carrying the given TransportableAIObject.
        Parameters:
        t - The TransportableAIObject to check.
        Returns:
        True if the carrier is carrying the transportable.
      • isTransporting

        public boolean isTransporting​(TransportableAIObject t)
        Is a transportable waiting for delivery on the cargoes list?
        Parameters:
        t - The TransportableAIObject to check.
        Returns:
        True if the transportable is queued in this mission.
      • shouldAttack

        private boolean shouldAttack​(Unit other)
        Decide if this unit has a good chance of defeating another. If there is cargo aboard, be more conservative. FIXME: magic numbers to the spec.
        Parameters:
        other - The other Unit to attack.
        Returns:
        True if the attack should proceed.
      • tCopy

        private java.util.List<Cargo> tCopy()
        Gets the cargoes.
        Returns:
        A copy of the list of cargoes.
      • tClear

        private java.util.List<Cargo> tClear()
        Clears the cargoes list.
        Returns:
        The old cargoes list.
      • tSet

        private java.util.List<Cargo> tSet​(java.util.List<Cargo> nxt,
                                           boolean setSpace)
        Sets the cargoes to a new list.
        Parameters:
        nxt - The new cargoes list.
        setSpace - If true, call tSpace to reset the space left values.
        Returns:
        The old cargoes list.
      • tSize

        private int tSize()
        Gets the size of the cargoes.
        Returns:
        The size of the cargoes.
      • tFind

        private Cargo tFind​(TransportableAIObject t)
        Find a Cargo with the given TransportableAIObject.
        Parameters:
        t - The TransportableAIObject to look for.
        Returns:
        The Cargo found, or null if none found.
      • tFirst

        private Cargo tFirst()
        Gets the first cargo.
        Returns:
        The first valid cargo, or null if none found.
      • tAdd

        private boolean tAdd​(Cargo cargo,
                             int index)
        Adds a cargo to the cargoes list.
        Parameters:
        cargo - The Cargo to add.
        index - The position to add it.
        Returns:
        True if the addition succeeded or the cargo was already present.
      • tRemove

        private boolean tRemove​(Cargo cargo)
        Remove a cargo from the cargoes list.
        Parameters:
        cargo - The Cargo to remove.
        Returns:
        True if the remove succeeded.
      • tSpace

        private void tSpace()
        Sets the spaceLeft fields in the cargoes. To be called with synchronized (cargoes).
      • tRetarget

        private void tRetarget()
        Reset the carrier target after a change to the first cargo.
      • destinationCount

        private int destinationCount()
        Count distinct non-adjacent destinations in a list of cargoes.
        Returns:
        The number of distinct destinations.
      • destinationCapacity

        public int destinationCapacity()
        How many more destinations are desirable on the current cargoes list? Must be public! This is checked in European AI player.
        Returns:
        The number of desired extra destinations.
      • dropTransportable

        private void dropTransportable​(TransportableAIObject t)
        If this carrier is the current carrier of a transportable, drop it.
        Parameters:
        t - The TransportableAIObject to check.
      • takeTransportable

        private void takeTransportable​(TransportableAIObject t)
        If this carrier is the not the current carrier of a transportable, make it so.
        Parameters:
        t - The TransportableAIObject to check.
      • getTransportTarget

        public Location getTransportTarget​(TransportableAIObject t)
        Get the collection location for an uncollected transportable. Public so that mobile transportables (units) can move to the collection point.
        Parameters:
        t - The TransportableAIObject to collect.
        Returns:
        The collection Location, or null if not found.
      • getTransportTurns

        public int getTransportTurns​(TransportableAIObject t)
        Get the expected turns for an uncollected transport Public so that mobile transportables (units) can renege on transport if they find themselves better able to get there themselves.
        Parameters:
        t - The TransportableAIObject to collect.
        Returns:
        The expected transport turns.
      • wrapCargoes

        private java.util.List<Cargo> wrapCargoes()
        Wrap up the compatible cargoes in a list. O(N^2) alas.
        Returns:
        A wrapped list of cargoes.
      • unwrapCargoes

        private java.util.List<Cargo> unwrapCargoes​(java.util.List<Cargo> ts)
        Unwrap a wrapped list of cargoes.
        Parameters:
        ts - The list of Cargos to unwrap.
        Returns:
        The unwrapped list of cargoes.
      • clearCargoes

        private java.lang.String clearCargoes()
        Clears all the cargoes.
        Returns:
        A message about the cargoes being cleared.
      • isEmpty

        public boolean isEmpty()
        Is there nothing currently queued for this carrier?
        Returns:
        True if there is no work allocated to this carrier.
      • makeCargo

        public Cargo makeCargo​(TransportableAIObject t,
                               LogBuilder lb)
        For a given transportable, work out where the carrier has to go to advance the cargo (target), and what to do there (mode), allowing a new Cargo to be defined. AIUnit cargo is harder than AIGoods, because AIUnits might have their own inland paths, and thus we need to consider drop nodes.
        Parameters:
        t - The TransportableAIObject to consider.
        lb - A LogBuilder to log to.
        Returns:
        A new Cargo defining the action to take with the TransportableAIObject, or null if impossible.
      • addCargo

        private boolean addCargo​(Cargo cargo,
                                 int index,
                                 LogBuilder lb)
        Add the given Cargo to the cargoes list.
        Parameters:
        cargo - The Cargo to add.
        index - The index of where to add the cargo.
        lb - A LogBuilder to log to.
        Returns:
        True if the cargo was added.
      • removeCargo

        private void removeCargo​(Cargo cargo)
        Removes the given Cargo from the cargoes list.
        Parameters:
        cargo - The Cargo to remove.
      • spaceAvailable

        public boolean spaceAvailable​(Cargo cargo)
        Is there space available for a new cargo?
        Parameters:
        cargo - The Cargo to check.
        Returns:
        True if there is space available for this cargo.
      • spaceAvailable

        public boolean spaceAvailable​(TransportableAIObject t)
        Is there space available for a new cargo?
        Parameters:
        t - The TransportableAIObject to check.
        Returns:
        True if there is space available for this transportable.
      • queueCargo

        private boolean queueCargo​(Cargo cargo,
                                   boolean requireMatch,
                                   LogBuilder lb)
        Incrementally queue a cargo to the cargoes list. If the carrier is at the collection point favour immediate collection. Otherwise try to place it with other cargoes with the same target, but do not break the space restrictions. If this does not work, it has to go at the end.
        Parameters:
        cargo - The new Cargo to add.
        requireMatch - Fail if an existing destination is not matched.
        lb - A LogBuilder to log to.
        Returns:
        True if the cargo was queued.
      • dumpCargo

        private boolean dumpCargo​(Cargo cargo,
                                  LogBuilder lb)
        Dump a currently carried cargo.
        Parameters:
        cargo - The Cargo to dump.
        lb - A LogBuilder to log to.
        Returns:
        True if the cargo is no longer on board and not on the transport list, or is on board but is scheduled to be dumped.
      • requeueCargo

        private boolean requeueCargo​(Cargo cargo,
                                     LogBuilder lb)
        Requeue an existing cargo. Typically done when the target changes.
        Parameters:
        cargo - The Cargo to requeue.
        lb - A LogBuilder to log to.
        Returns:
        True if the queuing succeeded.
      • checkCargoes

        private void checkCargoes​(LogBuilder lb)
        Checks for invalid cargoes, and units and goods on board but not in the cargoes list. On exit from this routine, every cargo on board should be on the cargoes list but the list is not necessarily going to be in a sensible order.
        Parameters:
        lb - A LogBuilder to log to.
      • tryCargo

        private TransportMission.CargoResult tryCargo​(Cargo cargo,
                                                      LogBuilder lb)
        Check a Cargo for continued validity and whether action is needed at the current location.
        Parameters:
        cargo - The Cargo to check.
        lb - A LogBuilder to log to.
        Returns:
        TCONTINUE if the Cargo should continue, TDONE if it has completed, TFAIL if it has failed, TNEXT if it has progressed to the next stage, TRETRY if a blockage has occurred and it should be retried,
      • doTransport

        private void doTransport​(LogBuilder lb)
        Perform the transport load/unload operations on arrival at the target for the top cargo.
        Parameters:
        lb - A LogBuilder to log to.
      • scoreCargoOrder

        private float scoreCargoOrder​(Location initialLocation,
                                      java.util.List<Cargo> order)
        Calculates a score for a proposed list of Cargos using the current unit. Disallows routes that would overfill the carrier. Useful for comparing proposed cargo delivery routes. The score is based primarily on the number of turns it takes, but to break ties we also consider the hold*turn product to reduce the risk of losses due to enemy action.
        Parameters:
        initialLocation - The initial Location.
        order - An ordering of Cargos.
        Returns:
        A score for the cargo ordering.
      • optimizeCargoes

        private void optimizeCargoes​(LogBuilder lb)
        Sets the current target. Tries all permutations of cargoes and picks the fastest/safest one. Leaves the cargoes in the order they are expected to execute, with valid spaceLeft values.
        Parameters:
        lb - A LogBuilder to log to.
      • getBestCargo

        private Cargo getBestCargo​(Unit carrier)
        What is the best transportable for a carrier to collect?
        Parameters:
        carrier - The carrier Unit to consider.
        Returns:
        The best available new Cargo, or null if none found.
      • invalidUnitReason

        private static java.lang.String invalidUnitReason​(AIUnit aiUnit)
        Why would an TransportMission be invalid with the given unit?
        Parameters:
        aiUnit - The AIUnit to test.
        Returns:
        A reason why the mission would be invalid with the unit, or null if none found.
      • invalidCargoReason

        private static java.lang.String invalidCargoReason​(Cargo cargo)
        Why would this mission be invalid with a given cargo? Checks the cargo locations. A location becomes invalid if: - step is a null location - step is disposed - step is a captured settlement
        Parameters:
        cargo - The Cargo to test.
        Returns:
        A reason why the mission would be invalid, or null if none found.
      • invalidMissionReason

        public static java.lang.String invalidMissionReason​(AIUnit aiUnit)
        Why would this mission be invalid with the given AI unit?
        Parameters:
        aiUnit - The AIUnit to check.
        Returns:
        A reason for mission invalidity, or null if none found.
      • invalidMissionReason

        public static java.lang.String invalidMissionReason​(AIUnit aiUnit,
                                                            Location loc)
        Why would this mission be invalid with the given AI unit and location?
        Parameters:
        aiUnit - The AIUnit to check.
        loc - The Location to check.
        Returns:
        A reason for invalidity, or null if none found.
      • removeTransportable

        public boolean removeTransportable​(TransportableAIObject t)
        Removes the given TransportableAIObject from the cargo list.
        Parameters:
        t - The TransportableAIObject to remove.
        Returns:
        True if the removal succeeded.
      • requeueTransportable

        public boolean requeueTransportable​(TransportableAIObject t,
                                            LogBuilder lb)
        Retargets a transportable that should already be on board the carrier.
        Parameters:
        t - The TransportableAIObject to retarget.
        lb - A LogBuilder to log to.
        Returns:
        True if the retargeting succeeded.
      • queueTransportable

        public boolean queueTransportable​(TransportableAIObject t,
                                          boolean requireMatch,
                                          LogBuilder lb)
        Wrapper for queueCargo. Public for the benefit of EuropeanAIPlayer.allocateTransportables and CashInTreasureTrain.doMission.
        Parameters:
        t - The TransportableAIObject to add.
        requireMatch - Fail if an existing destination is not matched.
        lb - A LogBuilder to log to.
        Returns:
        True if the transportable was queued.
      • dumpTransportable

        public boolean dumpTransportable​(TransportableAIObject t,
                                         LogBuilder lb)
        Dump a transportable.
        Parameters:
        t - The TransportableAIObject to dump.
        lb - A LogBuilder to log to.
        Returns:
        True if the transportable is no longer on board, queued, or was reset to be dumped at the next stop.
      • forceCollection

        public boolean forceCollection​(AIUnit aiu,
                                       LogBuilder lb)
        Drop all collections so that cargo is delivered only, then collect this unit. Useful for prioritizing treasure collection.
        Parameters:
        aiu - The AIUnit to collect.
        lb - A LogBuilder to log to.
        Returns:
        True if the unit was queued.
      • suppressEuropeanTrade

        public void suppressEuropeanTrade​(GoodsType type,
                                          LogBuilder lb)
        Suppress European trade in a type of goods which is about to be boycotted.
        Parameters:
        type - The GoodsType to suppress.
        lb - A LogBuilder to log to.
      • getTransportDestination

        public Tile getTransportDestination()
        Gets the destination of a required transport. Override this in the child mission classes if there is a useful intermediate point to deliver the unit distinct from the target.
        Overrides:
        getTransportDestination in class Mission
        Returns:
        The mission target, or null if the mission is invalid, otherwise lacks a target (e.g. UnitWanderHostile), or the unit does not need transport.
      • getTarget

        public Location getTarget()
        Gets the target of this mission, if any.
        Specified by:
        getTarget in class Mission
        Returns:
        The target of this mission, or null if none.
      • setTarget

        public void setTarget​(Location target)
        Sets the target of this mission, if any. The actual target is handled in the missions that really have them, this is a helper to make sure the unit is updated.
        Specified by:
        setTarget in class Mission
        Parameters:
        target - The new target of this mission, or null if none.
      • findTarget

        public Location findTarget()
        Finds a new target for this mission.
        Specified by:
        findTarget in class Mission
        Returns:
        A new target for this mission.
      • invalidReason

        public java.lang.String invalidReason()
        Why is this mission invalid? Mission subclasses must implement this routine, which probably should start by checking invalidAIUnitReason. A mission can be invalid for a number of subclass-specific reasons. For example: a seek-and-destroy mission could be invalid because of a improved stance towards the targeted player.
        Specified by:
        invalidReason in class Mission
        Returns:
        A reason for mission invalidity, or null if none found.
      • doMission

        public Mission doMission​(LogBuilder lb)
        Performs the mission.
        Specified by:
        doMission in class Mission
        Parameters:
        lb - A LogBuilder to log to.
        Returns:
        The Mission to continue with, or null if the current mission has completed.
      • toFullString

        public java.lang.String toFullString()
        More verbose version of toString().
        Returns:
        A summary of this mission including its transportables.
      • 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 Mission
        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 Mission
        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.