Class Cargo


  • public final class Cargo
    extends java.lang.Object
    An class describing the action needed to make progress in a transportation action for a specific transportable.
    • Field Detail

      • logger

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

        private static final int MAX_TRY
        Abandon cargo after three blockages.
        See Also:
        Constant Field Values
      • carrier

        private Unit carrier
        The carrier that is providing transportation.
      • tries

        private int tries
        Counter for failed tries.
      • spaceLeft

        private int spaceLeft
        Space left on the carrier.
      • wrapped

        private java.util.List<Cargo> wrapped
        Wrap location for cargoes of the same destination.
    • Constructor Detail

      • Cargo

        private Cargo​(TransportableAIObject transportable,
                      Unit carrier,
                      Cargo.CargoPlan plan)
        Create a new cargo.
        Parameters:
        transportable - The TransportableAIObject to transport.
        carrier - The carrier Unit.
        plan - The CargoPlan to perform.
      • Cargo

        public Cargo​(AIMain aiMain,
                     FreeColXMLReader xr)
              throws javax.xml.stream.XMLStreamException
        Create a new cargo from a stream.
        Parameters:
        aiMain - The AIMain root.
        xr - The input stream containing the XML.
        Throws:
        javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.
    • Method Detail

      • initialize

        private java.lang.String initialize​(Location destination,
                                            boolean allowFallback)
        Initialize this cargo.
        Parameters:
        destination - The destination Location to take the transportable to, defaulting to the transport destination if null.
        allowFallback - Allow a fallback transport.
        Returns:
        A reason the planning failed, null if it succeeded.
      • update

        public java.lang.String update()
        Update the current delivery at the current fallback tolerance.
        Returns:
        A reason the planning failed, null if it succeeded.
      • newCargo

        public static Cargo newCargo​(TransportableAIObject t,
                                     Unit carrier)
                              throws FreeColException
        Make a new cargo with given transportable and carrier.
        Parameters:
        t - The TransportableAIObject to transport.
        carrier - The carrier Unit to perform the transport.
        Returns:
        The new Cargo.
        Throws:
        FreeColException - on failure of the planning stage.
      • newCargo

        public static Cargo newCargo​(TransportableAIObject t,
                                     Unit carrier,
                                     Location destination,
                                     boolean allowFallback)
                              throws FreeColException
        Make a new cargo with given transportable, carrier and explicit destination and fallback state.
        Parameters:
        t - The TransportableAIObject to transport.
        carrier - The carrier Unit to perform the transport.
        destination - The destination Location for the transportable.
        allowFallback - Allow a fallback destination.
        Returns:
        The new Cargo.
        Throws:
        FreeColException - on failure of the planning stage.
      • dump

        public java.lang.String dump()
        Reset this cargo to dump to the nearest available location.
        Returns:
        A reason for failing to reset, or null on succes.
      • getTransportable

        public TransportableAIObject getTransportable()
        Get the transportable.
        Returns:
        The TransportableAIObject to transport.
      • getCarrier

        public Unit getCarrier()
      • getTries

        public int getTries()
      • getSpaceLeft

        public int getSpaceLeft()
      • setSpaceLeft

        public void setSpaceLeft​(int spaceLeft)
      • isValid

        public boolean isValid()
      • getModeString

        public java.lang.String getModeString()
      • getTurns

        public int getTurns()
      • isFallback

        public boolean isFallback()
      • getTransportTarget

        public Location getTransportTarget()
      • getCarrierTarget

        public Location getCarrierTarget()
      • clear

        public void clear()
      • isCarried

        public boolean isCarried()
        Is the transportable on board the carrier?
        Returns:
        True if the transportable is being carried by the carrier.
      • isCollectable

        public boolean isCollectable()
        Is this cargo collectable? That is, is it and the carrier at their collection points, and in a collectable mode.
        Returns:
        True if the cargo can be collected.
      • isDeliverable

        public boolean isDeliverable()
        Is this cargo deliverable? That is, has it arrived at the target on board the carrier in a deliverable mode.
        Returns:
        True if the cargo can be delivered to the target.
      • isDelivered

        public boolean isDelivered()
        Is this cargo delivered, or otherwise removed? That is, is the cargo not on board the carrier in a deliverable mode?
        Returns:
        True if the cargo has been delivered.
      • hasPath

        public boolean hasPath()
        Does this cargo have a potential delivery path?
        Returns:
        True if the carrier can deliver the cargo.
      • getJoinDirection

        public Direction getJoinDirection()
        Get the movement direction to join the carrier.
        Returns:
        The Direction to join by.
      • getLeaveDirection

        public Direction getLeaveDirection()
        Get the movement direction to leave the carrier.
        Returns:
        The Direction to leave by.
      • getNewSpace

        public int getNewSpace()
        How much space would be needed to add this transportable?
        Returns:
        The extra space required.
      • hasWrapped

        public boolean hasWrapped()
        Does this cargo wrap others?
        Returns:
        True if wrapped transportables are present.
      • couldWrap

        public boolean couldWrap​(Cargo other)
        TransportableAIObjects can be `wrapped' if they have the same target and advancing them reduces the space on the carrier.
        Parameters:
        other - The other TransportableAIObject to consider.
        Returns:
        True if the transportables can be wrapped.
      • wrap

        public void wrap​(Cargo other)
        Wrap a Cargo into this one.
        Parameters:
        other - The other Cargo to wrap.
      • unwrap

        public java.util.List<Cargo> unwrap()
        Unwrap this cargo.
        Returns:
        The cargoes that were wrapped.
      • retry

        public boolean retry()
        Should this Cargo be retried after encountering a blockage? For now, just tries three times. FIXME: be smarter.
        Returns:
        True if the Cargo should be retried.
      • resetTries

        public void resetTries()
        Reset the tries counter.
      • isEuropeanTrade

        public boolean isEuropeanTrade​(GoodsType type)
        Does this cargo involve trade with Europe in a given goods type?
        Parameters:
        type - The GoodsType to check.
        Returns:
        True if this cargo is of the given type and to be collected or delivered to Europe.
      • check

        public java.lang.String check​(AIUnit aiCarrier)
        Check the integrity of this cargo.
        Parameters:
        aiCarrier - The AIUnit version of the carrier.
        Returns:
        A reason for integrity failure, or null if none.
      • canQueueAt

        public boolean canQueueAt​(Unit carrier,
                                  int index,
                                  java.util.List<Cargo> cargoes)
        Can this cargo be queued at the given index in a list of cargoes? TODO: be smarter and break out of the loop if the cargo reaches its delivery point.
        Parameters:
        carrier - The Unit to queue to.
        index - The queuing position to test.
        cargoes - A list of Cargos.
        Returns:
        True if there is space to add the cargo.
      • toShortString

        public java.lang.String toShortString()
        Abbreviated string representation for this cargo.
        Returns:
        A short descriptive string.
      • toXML

        public void toXML​(FreeColXMLWriter xw)
                   throws javax.xml.stream.XMLStreamException
        Throws:
        javax.xml.stream.XMLStreamException
      • readFromXML

        public void readFromXML​(AIMain aiMain,
                                FreeColXMLReader xr)
                         throws javax.xml.stream.XMLStreamException
        Throws:
        javax.xml.stream.XMLStreamException
      • getXMLTagName

        public java.lang.String getXMLTagName()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object