net.sf.freecol.server.ai.mission
Class TransportMission

java.lang.Object
  extended by net.sf.freecol.common.model.FreeColObject
      extended by net.sf.freecol.server.ai.AIObject
          extended by net.sf.freecol.server.ai.mission.Mission
              extended by net.sf.freecol.server.ai.mission.TransportMission

public class TransportMission
extends Mission

Mission for transporting units and goods on a carrier.

See Also:
Unit

Nested Class Summary
(package private)  class TransportMission.Destination
           
 
Field Summary
private static java.lang.String ELEMENT_TRANSPORTABLE
           
private static java.util.logging.Logger logger
           
private static int MINIMUM_GOLD_TO_STAY_IN_EUROPE
           
private  java.util.List<Transportable> transportList
           
 
Fields inherited from class net.sf.freecol.server.ai.mission.Mission
MINIMUM_TRANSPORT_PRIORITY, NO_MORE_MOVES_LEFT, NO_PATH_TO_TARGET, NORMAL_TRANSPORT_PRIORITY
 
Fields inherited from class net.sf.freecol.server.ai.AIObject
uninitialized
 
Fields inherited from class net.sf.freecol.common.model.FreeColObject
ARRAY_SIZE, ID_ATTRIBUTE, ID_ATTRIBUTE_TAG, INFINITY, NO_ID, PARTIAL_ATTRIBUTE, UNDEFINED, VALUE_TAG
 
Constructor Summary
TransportMission(AIMain aiMain, AIUnit aiUnit)
          Creates a mission for the given AIUnit.
TransportMission(AIMain aiMain, org.w3c.dom.Element element)
          Loads a TransportMission from the given element.
TransportMission(AIMain aiMain, javax.xml.stream.XMLStreamReader in)
          Creates a new TransportMission and reads the given element.
 
Method Summary
 void addToTransportList(Transportable newTransportable)
          Adds the given Transportable to the transport list.
private  boolean attackEnemyShips(Connection connection)
          Attack suitable enemy ships.
private  boolean attackIfEnemyShipIsBlocking(Connection connection, Map.Direction direction)
          Attack blocking ships.
private  void buyCargo(Connection connection)
          Buys cargo (units and goods) when the carrier is in Europe.
 AIGoods buyGoodsInEurope(Connection connection, GoodsType type, int amount, Location destination)
          Buys the given cargo.
private  boolean canAttackEnemyShips()
           
private  boolean canAttackPlayer(Player target)
           
 void dispose()
          Disposes this Mission.
 void doMission(Connection connection)
          Performs the mission.
protected  PathNode findNavalTarget(int maxTurns)
          Finds the best target to attack within the given range.
protected  PathNode findPathToEurope(Tile start)
          Finds the best path to Europe.
 int getAvailableSpace()
          Returns the available space for any type of unit going to any type of location.
 int getAvailableSpace(Transportable t)
          Returns the available space for the given Transportable.
 int getAvailableSpace(UnitType unitType, Location source, Location destination)
          Returns the available space for the given type of Unit at the given Location.
private  AIUnit getCheapestUnitInEurope(Connection connection)
          Returns the cheapest unit which can be bought in Europe.
 java.lang.String getDebuggingInfo()
          Gets debugging information about this mission.
(package private)  TransportMission.Destination getDefaultDestination()
          Gets the current default destination for the unit of this mission.
private  int getDistanceTo(Transportable t, Location start, boolean source)
          Gets the distance to the given Transportable.
 TransportMission.Destination getNextDestination()
          Works out the next destination the carrier should go to to make progress with its transport list.
static int getPlayerNavalTransportMissionCount(AIPlayer aiPlayer, Unit unitExcluded)
          Gives the number of naval units assigned with a Transport Mission
 Tile getTransportDestination()
          Returns the destination of a required transport.
 PathNode getTransportPath(Transportable transportable)
          Returns the path the carrier should use to get/drop the given Transportable.
private  PathNode getTransportPath(Transportable transportable, Location start, boolean collect)
          Returns the path the carrier should use to get/drop the given Transportable.
 int getTransportPriority()
          Returns the priority of getting the unit to the transport destination.
private  AIUnit getUnitInEurope(Connection connection, UnitType unitType)
          Returns the given type of Unit.
static java.lang.String getXMLElementTagName()
          Returns the tag name of the root element representing this object.
private  boolean hasCargo()
           
private  void inEurope(Connection connection)
          Unit is in Europe, unload cargo on board, buy required goods and board unit.
private  boolean isCarrying(Transportable t)
          Checks if the carrier using this mission is carrying the given Transportable.
 boolean isOnTransportList(Transportable newTransportable)
          Checks if the given Transportable is on the transport list.
 boolean isValid()
          Checks if this mission is still valid to perform.
static boolean isValid(AIUnit aiUnit)
          Checks if this mission is valid for the given unit.
private  boolean loadCargoAtDestination(Connection connection)
          Loads any Transportables being in range of the carrier.
protected  void readChildren(javax.xml.stream.XMLStreamReader in)
          Reads the children of this object from an XML stream.
 void removeFromTransportList(Transportable transportable)
          Removes the given Transportable from the transport list.
private  boolean restockCargoAtDestination(Connection connection)
          Loads and unloads any Transportable.
 java.lang.String toString()
          Creates a String representation of this mission to be used for debugging purposes.
protected  void toXMLImpl(javax.xml.stream.XMLStreamWriter out)
          Writes all of the AIObjects and other AI-related information to an XML-stream.
private  boolean unloadCargoAtDestination(Connection connection)
          Unloads any Transportables which have reached their destination.
private  void updateTransportList()
          Adds every Goods and Unit onboard the carrier to the transport list.
protected  void writeChildren(javax.xml.stream.XMLStreamWriter out)
          Write the children of this object to a stream.
 
Methods inherited from class net.sf.freecol.server.ai.mission.Mission
findNearestOtherSettlement, findTarget, getAIRandom, getAIUnit, getEuropeanAIPlayer, getPathStartTile, getUnit, isOneTime, moveButDontAttack, moveRandomly, moveTowards, moveTowards, moveUnitToAmerica, moveUnitToEurope, readAttributes, sellCargoInEurope, setAIUnit, shouldTakeTransportToTile, travelToTarget, unitLeavesShip, unloadCargoInColony, writeAttributes
 
Methods inherited from class net.sf.freecol.server.ai.AIObject
getAIMain, getGame, getSpecification, isUninitialized, readFromXML
 
Methods inherited from class net.sf.freecol.common.model.FreeColObject
addPropertyChangeListener, addPropertyChangeListener, dumpObject, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAttribute, getAttribute, getAttribute, getAttribute, getAttribute, getId, getPropertyChangeListeners, getPropertyChangeListeners, hasAbility, hasAttribute, hasListeners, readAttributes, readChild, readChild, readChildren, readFromArrayElement, readFromArrayElement, readFromListElement, readFromXMLElement, readFromXMLImpl, readFromXMLImpl, readFromXMLPartialImpl, removePropertyChangeListener, removePropertyChangeListener, save, save, setId, setSpecification, toXML, toXML, toXML, toXMLElement, toXMLElement, toXMLElement, toXMLElement, toXMLElementPartial, toXMLPartialImpl, writeAttribute
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

logger

private static final java.util.logging.Logger logger

ELEMENT_TRANSPORTABLE

private static final java.lang.String ELEMENT_TRANSPORTABLE
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

transportList

private final java.util.List<Transportable> transportList
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,
                        org.w3c.dom.Element element)
Loads a TransportMission from the given element.

Parameters:
aiMain - The main AI-object.
element - An Element containing an XML-representation of this object.

TransportMission

public TransportMission(AIMain aiMain,
                        javax.xml.stream.XMLStreamReader in)
                 throws javax.xml.stream.XMLStreamException
Creates a new TransportMission and reads the given element.

Parameters:
aiMain - The main AI-object.
in - The input stream containing the XML.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.
See Also:
AIObject.readFromXML(javax.xml.stream.XMLStreamReader)
Method Detail

updateTransportList

private void updateTransportList()
Adds every Goods and Unit onboard the carrier to the transport list.

See Also:
Goods, Unit

isCarrying

private boolean isCarrying(Transportable t)
Checks if the carrier using this mission is carrying the given Transportable.

Parameters:
t - The Transportable.
Returns:
true if the given Transportable is located in the carrier.

dispose

public void dispose()
Disposes this Mission.

Overrides:
dispose in class Mission

isOnTransportList

public boolean isOnTransportList(Transportable newTransportable)
Checks if the given Transportable is on the transport list.

Parameters:
newTransportable - The Transportable to be checked
Returns:
true if the given Transportable was on the transport list, and false otherwise.

removeFromTransportList

public void removeFromTransportList(Transportable transportable)
Removes the given Transportable from the transport list. This method calls Transportable.setTransport(AIUnit).

Parameters:
transportable - The Transportable.

addToTransportList

public void addToTransportList(Transportable newTransportable)
Adds the given Transportable to the transport list. The method returns immediately if the Transportable has already be added.

Both the source and destination Location for the Transportable is entered into the transport list if the Transportable is not already loaded onto the transport. If the Transportable is onboard the transport, then only the destination is put on the transport list.

Parameters:
newTransportable - The Transportable.

getDistanceTo

private int getDistanceTo(Transportable t,
                          Location start,
                          boolean source)
Gets the distance to the given Transportable.

Parameters:
start - The Location to check the distance from. Europe is used instead of this location if start.getTile() == null.
source - Sets wether the Transportable's source or destination should be used.
Returns:
The distance from the given Location to the source or destination of the given Transportable.

canAttackEnemyShips

private boolean canAttackEnemyShips()

hasCargo

private boolean hasCargo()

attackIfEnemyShipIsBlocking

private boolean attackIfEnemyShipIsBlocking(Connection connection,
                                            Map.Direction direction)
Attack blocking ships.

Returns:
True if this ship is still capable of its mission.

attackEnemyShips

private boolean attackEnemyShips(Connection connection)
Attack suitable enemy ships.

Returns:
True if this ship is still capable of its mission.

canAttackPlayer

private boolean canAttackPlayer(Player target)

findNavalTarget

protected PathNode findNavalTarget(int maxTurns)
Finds the best target to attack within the given range.

Parameters:
maxTurns - The maximum number of turns the unit is allowed to spend in order to reach the target.
Returns:
The path to the target or null if no target can be found.

doMission

public void doMission(Connection connection)
Performs the mission.

Specified by:
doMission in class Mission
Parameters:
connection - The Connection to the server.

getNextDestination

public TransportMission.Destination getNextDestination()
Works out the next destination the carrier should go to to make progress with its transport list.

Returns:
A new Destination, which may be null if none is available.

getDefaultDestination

TransportMission.Destination getDefaultDestination()
Gets the current default destination for the unit of this mission.

Returns:
The default Destination for the unit.

buyCargo

private void buyCargo(Connection connection)
Buys cargo (units and goods) when the carrier is in Europe.

Warning: This method can only be called when the carrier is located in Europe.

Parameters:
connection - The Connection to the server.

buyGoodsInEurope

public AIGoods buyGoodsInEurope(Connection connection,
                                GoodsType type,
                                int amount,
                                Location destination)
Buys the given cargo.

Warning: This method can only be called when the carrier is located in Europe.

Parameters:
connection - The Connection to use when communicating with the server.
type - The type of goods to buy.
amount - The amount of goods to buy.
destination - The Location to which the goods should be transported.
Returns:
The goods.

getUnitInEurope

private AIUnit getUnitInEurope(Connection connection,
                               UnitType unitType)
Returns the given type of Unit.

Warning: This method can only be called when the carrier is located in Europe.

This sequence is used when trying to get the unit:

  1. Getting the unit from the docks.
  2. Recruiting the unit.
  3. Training the unit.

Parameters:
connection - The Connection to the server.
unitType - The type of Unit to be found/recruited/trained.
Returns:
The AIUnit.

getCheapestUnitInEurope

private AIUnit getCheapestUnitInEurope(Connection connection)
Returns the cheapest unit which can be bought in Europe.

Parameters:
connection - The connection to use when communicating with the server.
Returns:
The AIUnit.

getTransportPath

public PathNode getTransportPath(Transportable transportable)
Returns the path the carrier should use to get/drop the given Transportable.

Parameters:
transportable - The Transportable.
Returns:
The path.

getTransportPath

private PathNode getTransportPath(Transportable transportable,
                                  Location start,
                                  boolean collect)
Returns the path the carrier should use to get/drop the given Transportable.

Parameters:
transportable - The Transportable.
start - The Tile to search from. If start == null or start.getTile() == null then the carrier's entry location is used instead.
collect - True if the transportable must be collected.
Returns:
The path.

getAvailableSpace

public int getAvailableSpace(Transportable t)
Returns the available space for the given Transportable.

Parameters:
t - The Transportable
Returns:
The space available for Transportables with the same source and destination.

getAvailableSpace

public int getAvailableSpace(UnitType unitType,
                             Location source,
                             Location destination)
Returns the available space for the given type of Unit at the given Location.

Parameters:
unitType - The type of Unit or null for Goods
source - The source for the unit. This is where the unit is presently located.
destination - The destination for the unit.
Returns:
The space available

getAvailableSpace

public int getAvailableSpace()
Returns the available space for any type of unit going to any type of location.

Returns:
The space available

restockCargoAtDestination

private boolean restockCargoAtDestination(Connection connection)
Loads and unloads any Transportable.

Parameters:
connection - The Connection to the server.
Returns:
true if something has been loaded/unloaded and falseotherwise.

unloadCargoAtDestination

private boolean unloadCargoAtDestination(Connection connection)
Unloads any Transportables which have reached their destination.

Parameters:
connection - The Connection to the server.
Returns:
true if something has been unloaded and falseotherwise.

loadCargoAtDestination

private boolean loadCargoAtDestination(Connection connection)
Loads any Transportables being in range of the carrier.

Parameters:
connection - The Connection to the server.
Returns:
true if something has been unloaded and falseotherwise.

isValid

public static boolean isValid(AIUnit aiUnit)
Checks if this mission is valid for the given unit.

Parameters:
aiUnit - The unit.
Returns:
true if this mission is valid to perform and false otherwise.

isValid

public boolean isValid()
Checks if this mission is still valid to perform.

Overrides:
isValid in class Mission
Returns:
true

getTransportDestination

public Tile getTransportDestination()
Returns the destination of a required transport.

Overrides:
getTransportDestination in class Mission
Returns:
null

getTransportPriority

public int getTransportPriority()
Returns the priority of getting the unit to the transport destination.

Overrides:
getTransportPriority in class Mission
Returns:
0.

inEurope

private void inEurope(Connection connection)
Unit is in Europe, unload cargo on board, buy required goods and board unit.

Parameters:
connection - The Connection to the server.

findPathToEurope

protected PathNode findPathToEurope(Tile start)
Finds the best path to Europe.

Parameters:
start - The starting Tile.
Returns:
The path to the target or null if no target can be found.
See Also:
Europe

getPlayerNavalTransportMissionCount

public static int getPlayerNavalTransportMissionCount(AIPlayer aiPlayer,
                                                      Unit unitExcluded)
Gives the number of naval units assigned with a Transport Mission


getDebuggingInfo

public java.lang.String getDebuggingInfo()
Gets debugging information about this mission. This string is a short representation of this object's state.

Overrides:
getDebuggingInfo in class Mission
Returns:
The String: "(x, y) z" or "(x, y) z!" where x and y is the coordinates of the target tile for this mission, and z is the value of building the colony. The exclamation mark is added if the unit should continue searching for a colony site if the targeted site is lost.

toXMLImpl

protected void toXMLImpl(javax.xml.stream.XMLStreamWriter out)
                  throws javax.xml.stream.XMLStreamException
Writes all of the AIObjects and other AI-related information to an XML-stream.

Specified by:
toXMLImpl in class FreeColObject
Parameters:
out - The target stream.
Throws:
javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.

writeChildren

protected void writeChildren(javax.xml.stream.XMLStreamWriter out)
                      throws javax.xml.stream.XMLStreamException
Description copied from class: FreeColObject
Write the children of this object to a stream. To be overridden by any object that has children and uses the toXML(XMLStreamWriter, String) call.

Overrides:
writeChildren in class FreeColObject
Parameters:
out - The target stream.
Throws:
javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.

readChildren

protected void readChildren(javax.xml.stream.XMLStreamReader in)
                     throws javax.xml.stream.XMLStreamException
Description copied from class: FreeColObject
Reads the children of this object from an XML stream.

Overrides:
readChildren in class FreeColObject
Parameters:
in - The XML input stream.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.

toString

public java.lang.String toString()
Creates a String representation of this mission to be used for debugging purposes.

Overrides:
toString in class java.lang.Object

getXMLElementTagName

public static java.lang.String getXMLElementTagName()
Returns the tag name of the root element representing this object.

Returns:
"transportMission".