Package net.sf.freecol.server.ai
Class Cargo
- java.lang.Object
-
- net.sf.freecol.server.ai.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Cargo.CargoMode
The actions to perform at the target.static class
Cargo.CargoPlan
Container 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 Unit
carrier
The carrier that is providing transportation.private static java.lang.String
CARRIER_TAG
private static java.lang.String
CDST_TAG
private static java.lang.String
CWAIT_TAG
private static java.lang.String
FALLBACK_TAG
private static java.util.logging.Logger
logger
private static int
MAX_TRY
Abandon cargo after three blockages.private static java.lang.String
MODE_TAG
private Cargo.CargoPlan
plan
The plan to execute the transport.private int
spaceLeft
Space left on the carrier.private static java.lang.String
SPACELEFT_TAG
static java.lang.String
TAG
private static java.lang.String
TDST_TAG
private TransportableAIObject
transportable
The AI object to be transported.private int
tries
Counter for failed tries.private static java.lang.String
TRIES_TAG
private static java.lang.String
TURNS_TAG
private static java.lang.String
TWAIT_TAG
private java.util.List<Cargo>
wrapped
Wrap 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.private
Cargo(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 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.java.lang.String
check(AIUnit aiCarrier)
Check the integrity of this cargo.void
clear()
boolean
couldWrap(Cargo other)
TransportableAIObjects can be `wrapped' if they have the same target and advancing them reduces the space on the carrier.java.lang.String
dump()
Reset this cargo to dump to the nearest available location.Unit
getCarrier()
Location
getCarrierTarget()
Direction
getJoinDirection()
Get the movement direction to join the carrier.Direction
getLeaveDirection()
Get the movement direction to leave the carrier.Cargo.CargoMode
getMode()
java.lang.String
getModeString()
int
getNewSpace()
How much space would be needed to add this transportable?int
getSpaceLeft()
TransportableAIObject
getTransportable()
Get the transportable.Location
getTransportTarget()
int
getTries()
int
getTurns()
java.lang.String
getXMLTagName()
boolean
hasPath()
Does this cargo have a potential delivery path?boolean
hasWrapped()
Does this cargo wrap others?private java.lang.String
initialize(Location destination, boolean allowFallback)
Initialize this cargo.boolean
isCarried()
Is the transportable on board the carrier?boolean
isCollectable()
Is this cargo collectable? That is, is it and the carrier at their collection points, and in a collectable mode.boolean
isDeliverable()
Is this cargo deliverable? That is, has it arrived at the target on board the carrier in a deliverable mode.boolean
isDelivered()
Is this cargo delivered, or otherwise removed? That is, is the cargo not on board the carrier in a deliverable mode?boolean
isEuropeanTrade(GoodsType type)
Does this cargo involve trade with Europe in a given goods type?boolean
isFallback()
boolean
isValid()
static Cargo
newCargo(TransportableAIObject t, Unit carrier)
Make a new cargo with given transportable and carrier.static Cargo
newCargo(TransportableAIObject t, Unit carrier, Location destination, boolean allowFallback)
Make a new cargo with given transportable, carrier and explicit destination and fallback state.void
readFromXML(AIMain aiMain, FreeColXMLReader xr)
void
resetTries()
Reset the tries counter.boolean
retry()
Should thisCargo
be retried after encountering a blockage? For now, just tries three times.void
setSpaceLeft(int spaceLeft)
java.lang.String
toShortString()
Abbreviated string representation for this cargo.java.lang.String
toString()
void
toXML(FreeColXMLWriter xw)
java.util.List<Cargo>
unwrap()
Unwrap this cargo.java.lang.String
update()
Update the current delivery at the current fallback tolerance.void
wrap(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
- TheTransportableAIObject
to transport.carrier
- The carrierUnit
.plan
- TheCargoPlan
to perform.
-
Cargo
public Cargo(AIMain aiMain, FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Create a new cargo from a stream.- Parameters:
aiMain
- TheAIMain
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 destinationLocation
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
- TheTransportableAIObject
to transport.carrier
- The carrierUnit
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
- TheTransportableAIObject
to transport.carrier
- The carrierUnit
to perform the transport.destination
- The destinationLocation
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()
-
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
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 otherTransportableAIObject
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 otherCargo
to wrap.
-
unwrap
public java.util.List<Cargo> unwrap()
Unwrap this cargo.- Returns:
- The cargoes that were wrapped.
-
retry
public boolean retry()
Should thisCargo
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
- TheGoodsType
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
- TheAIUnit
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
- TheUnit
to queue to.index
- The queuing position to test.cargoes
- A list ofCargo
s.- 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 classjava.lang.Object
-
-