Package net.sf.freecol.server.ai
Class Cargo
- java.lang.Object
-
- net.sf.freecol.server.ai.Cargo
-
public final class Cargo extends java.lang.ObjectAn class describing the action needed to make progress in a transportation action for a specific transportable.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCargo.CargoModeThe actions to perform at the target.static classCargo.CargoPlanContainer for a plan for a transportable to get to TWAIT where a carrier collects it from CWAIT (may equal TWAIT) and takes it to CDST, whence the transportable goes to TDST (may equal CDST).
-
Field Summary
Fields Modifier and Type Field Description private UnitcarrierThe carrier that is providing transportation.private static java.lang.StringCARRIER_TAGprivate static java.lang.StringCDST_TAGprivate static java.lang.StringCWAIT_TAGprivate static java.lang.StringFALLBACK_TAGprivate static java.util.logging.Loggerloggerprivate static intMAX_TRYAbandon cargo after three blockages.private static java.lang.StringMODE_TAGprivate Cargo.CargoPlanplanThe plan to execute the transport.private intspaceLeftSpace left on the carrier.private static java.lang.StringSPACELEFT_TAGstatic java.lang.StringTAGprivate static java.lang.StringTDST_TAGprivate TransportableAIObjecttransportableThe AI object to be transported.private inttriesCounter for failed tries.private static java.lang.StringTRIES_TAGprivate static java.lang.StringTURNS_TAGprivate static java.lang.StringTWAIT_TAGprivate java.util.List<Cargo>wrappedWrap location for cargoes of the same destination.
-
Constructor Summary
Constructors Modifier Constructor Description Cargo(AIMain aiMain, FreeColXMLReader xr)Create a new cargo from a stream.privateCargo(TransportableAIObject transportable, Unit carrier, Cargo.CargoPlan plan)Create a new cargo.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancanQueueAt(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.java.lang.Stringcheck(AIUnit aiCarrier)Check the integrity of this cargo.voidclear()booleancouldWrap(Cargo other)TransportableAIObjects can be `wrapped' if they have the same target and advancing them reduces the space on the carrier.java.lang.Stringdump()Reset this cargo to dump to the nearest available location.UnitgetCarrier()LocationgetCarrierTarget()DirectiongetJoinDirection()Get the movement direction to join the carrier.DirectiongetLeaveDirection()Get the movement direction to leave the carrier.Cargo.CargoModegetMode()java.lang.StringgetModeString()intgetNewSpace()How much space would be needed to add this transportable?intgetSpaceLeft()TransportableAIObjectgetTransportable()Get the transportable.LocationgetTransportTarget()intgetTries()intgetTurns()java.lang.StringgetXMLTagName()booleanhasPath()Does this cargo have a potential delivery path?booleanhasWrapped()Does this cargo wrap others?private java.lang.Stringinitialize(Location destination, boolean allowFallback)Initialize this cargo.booleanisCarried()Is the transportable on board the carrier?booleanisCollectable()Is this cargo collectable? That is, is it and the carrier at their collection points, and in a collectable mode.booleanisDeliverable()Is this cargo deliverable? That is, has it arrived at the target on board the carrier in a deliverable mode.booleanisDelivered()Is this cargo delivered, or otherwise removed? That is, is the cargo not on board the carrier in a deliverable mode?booleanisEuropeanTrade(GoodsType type)Does this cargo involve trade with Europe in a given goods type?booleanisFallback()booleanisValid()static CargonewCargo(TransportableAIObject t, Unit carrier)Make a new cargo with given transportable and carrier.static CargonewCargo(TransportableAIObject t, Unit carrier, Location destination, boolean allowFallback)Make a new cargo with given transportable, carrier and explicit destination and fallback state.voidreadFromXML(AIMain aiMain, FreeColXMLReader xr)voidresetTries()Reset the tries counter.booleanretry()Should thisCargobe retried after encountering a blockage? For now, just tries three times.voidsetSpaceLeft(int spaceLeft)java.lang.StringtoShortString()Abbreviated string representation for this cargo.java.lang.StringtoString()voidtoXML(FreeColXMLWriter xw)java.util.List<Cargo>unwrap()Unwrap this cargo.java.lang.Stringupdate()Update the current delivery at the current fallback tolerance.voidwrap(Cargo other)Wrap a Cargo into this one.
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
TAG
public static final java.lang.String TAG
- See Also:
- Constant Field Values
-
MAX_TRY
private static final int MAX_TRY
Abandon cargo after three blockages.- See Also:
- Constant Field Values
-
transportable
private TransportableAIObject transportable
The AI object to be transported.
-
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.
-
plan
private Cargo.CargoPlan plan
The plan to execute the transport.
-
CDST_TAG
private static final java.lang.String CDST_TAG
- See Also:
- Constant Field Values
-
CWAIT_TAG
private static final java.lang.String CWAIT_TAG
- See Also:
- Constant Field Values
-
CARRIER_TAG
private static final java.lang.String CARRIER_TAG
- See Also:
- Constant Field Values
-
FALLBACK_TAG
private static final java.lang.String FALLBACK_TAG
- See Also:
- Constant Field Values
-
MODE_TAG
private static final java.lang.String MODE_TAG
- See Also:
- Constant Field Values
-
SPACELEFT_TAG
private static final java.lang.String SPACELEFT_TAG
- See Also:
- Constant Field Values
-
TDST_TAG
private static final java.lang.String TDST_TAG
- See Also:
- Constant Field Values
-
TRIES_TAG
private static final java.lang.String TRIES_TAG
- See Also:
- Constant Field Values
-
TURNS_TAG
private static final java.lang.String TURNS_TAG
- See Also:
- Constant Field Values
-
TWAIT_TAG
private static final java.lang.String TWAIT_TAG
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Cargo
private Cargo(TransportableAIObject transportable, Unit carrier, Cargo.CargoPlan plan)
Create a new cargo.- Parameters:
transportable- TheTransportableAIObjectto transport.carrier- The carrierUnit.plan- TheCargoPlanto perform.
-
Cargo
public Cargo(AIMain aiMain, FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Create a new cargo from a stream.- Parameters:
aiMain- TheAIMainroot.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 destinationLocationto 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- TheTransportableAIObjectto transport.carrier- The carrierUnitto 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- TheTransportableAIObjectto transport.carrier- The carrierUnitto perform the transport.destination- The destinationLocationfor 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
TransportableAIObjectto transport.
-
getCarrier
public Unit getCarrier()
-
getTries
public int getTries()
-
getSpaceLeft
public int getSpaceLeft()
-
setSpaceLeft
public void setSpaceLeft(int spaceLeft)
-
isValid
public boolean isValid()
-
getMode
public Cargo.CargoMode getMode()
-
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
Directionto join by.
-
getLeaveDirection
public Direction getLeaveDirection()
Get the movement direction to leave the carrier.- Returns:
- The
Directionto 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 otherTransportableAIObjectto consider.- Returns:
- True if the transportables can be wrapped.
-
wrap
public void wrap(Cargo other)
Wrap a Cargo into this one.- Parameters:
other- The otherCargoto wrap.
-
unwrap
public java.util.List<Cargo> unwrap()
Unwrap this cargo.- Returns:
- The cargoes that were wrapped.
-
retry
public boolean retry()
Should thisCargobe retried after encountering a blockage? For now, just tries three times. FIXME: be smarter.- Returns:
- True if the
Cargoshould 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- TheGoodsTypeto 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- TheAIUnitversion 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- TheUnitto queue to.index- The queuing position to test.cargoes- A list ofCargos.- 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:
toStringin classjava.lang.Object
-
-