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

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
Direct Known Subclasses:
BuildColonyMission, CashInTreasureTrainMission, DefendSettlementMission, IdleAtColonyMission, IndianBringGiftMission, IndianDemandMission, PioneeringMission, PrivateerMission, ScoutingMission, TransportMission, UnitSeekAndDestroyMission, UnitWanderHostileMission, UnitWanderMission, WishRealizationMission, WorkInsideColonyMission

public abstract class Mission
extends AIObject

A mission describes what a unit should do; attack, build colony, wander etc. Every AIUnit should have a mission. By extending this class, you create different missions.


Field Summary
private  AIUnit aiUnit
           
private static java.util.logging.Logger logger
           
protected static int MINIMUM_TRANSPORT_PRIORITY
           
protected static int NO_MORE_MOVES_LEFT
           
protected static int NO_PATH_TO_TARGET
           
protected static int 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
Mission(AIMain aiMain)
          Creates a mission.
Mission(AIMain aiMain, AIUnit aiUnit)
          Creates a mission for the given AIUnit.
 
Method Summary
 void dispose()
          Disposes this mission by removing any references to it.
abstract  void doMission(Connection connection)
          Performs the mission.
protected  PathNode findNearestOtherSettlement(Unit unit)
          Find the nearest reachable settlement to a unit (owned by the same player) excepting the any on the current tile.
protected  PathNode findTarget(int maxTurns)
          Finds the best target to attack within the given range.
protected  java.util.Random getAIRandom()
          Convenience accessor for the unit/player PRNG.
 AIUnit getAIUnit()
          Gets the AI-unit this mission has been created for.
 java.lang.String getDebuggingInfo()
          Gets debugging information about this mission.
protected  EuropeanAIPlayer getEuropeanAIPlayer()
          Convenience accessor for the owning European AI player.
protected static Tile getPathStartTile(Unit unit)
          Gets a suitable tile to start path searches from for a unit.
 Location getTransportDestination()
          Gets the transport destination of the unit associated with this mission.
 int getTransportPriority()
          Gets the priority of getting the unit to the transport destination.
 Unit getUnit()
          Gets the unit this mission has been created for.
 boolean isOneTime()
          Checks if this Mission should only be carried out once.
 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.
protected  boolean moveButDontAttack(Map.Direction direction)
          Move in a specified direction, but do not attack.
protected  void moveRandomly()
          Move a unit randomly.
protected  Map.Direction moveTowards(PathNode pathNode)
          Moves the unit owning this mission using the given path.
protected  Map.Direction moveTowards(Tile tile)
          Moves the unit owning this mission towards the given Tile.
protected  void moveUnitToAmerica()
           
protected  void moveUnitToEurope()
           
protected  void readAttributes(javax.xml.stream.XMLStreamReader in)
          
protected  boolean sellCargoInEurope(Goods goods)
          Sell some goods in Europe.
protected  void setAIUnit(AIUnit aiUnit)
          Sets the AI-unit this mission has been created for.
protected  boolean shouldTakeTransportToTile(Tile tile)
          Should the unit use transport to get to a specified tile? True if: - The unit is not there already AND - the unit already has transport, this will always be faster (TODO: actually, mounted units on good roads might be faster, check for this) - if not on the map - if on the map but can not find a path to the tile, unless adjacent to the destination which usually means the path finding failed due to a temporary blockage such as an enemy unit - if the path to the tile will take more than MAX_TURNS
protected  Unit.MoveType travelToTarget(java.lang.String logMe, Location target)
          Tries to move this mission's unit to a target location.
protected  boolean unitLeavesShip(AIUnit aiUnit)
          Unload a unit.
protected  boolean unloadCargoInColony(Goods goods)
          Unload some goods in a colony.
protected  void writeAttributes(javax.xml.stream.XMLStreamWriter out)
          
 
Methods inherited from class net.sf.freecol.server.ai.AIObject
getAIMain, getGame, getSpecification, getXMLElementTagName, 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, readChildren, readFromArrayElement, readFromArrayElement, readFromListElement, readFromXMLElement, readFromXMLImpl, readFromXMLImpl, readFromXMLPartialImpl, removePropertyChangeListener, removePropertyChangeListener, save, save, setId, setSpecification, toXML, toXML, toXML, toXMLElement, toXMLElement, toXMLElement, toXMLElement, toXMLElementPartial, toXMLImpl, toXMLPartialImpl, writeAttribute, writeChildren
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

private static final java.util.logging.Logger logger

MINIMUM_TRANSPORT_PRIORITY

protected static final int MINIMUM_TRANSPORT_PRIORITY
See Also:
Constant Field Values

NORMAL_TRANSPORT_PRIORITY

protected static final int NORMAL_TRANSPORT_PRIORITY
See Also:
Constant Field Values

NO_PATH_TO_TARGET

protected static final int NO_PATH_TO_TARGET
See Also:
Constant Field Values

NO_MORE_MOVES_LEFT

protected static final int NO_MORE_MOVES_LEFT
See Also:
Constant Field Values

aiUnit

private AIUnit aiUnit
Constructor Detail

Mission

public Mission(AIMain aiMain)
Creates a mission.

Parameters:
aiMain - The main AI-object.

Mission

public Mission(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.
Throws:
java.lang.NullPointerException - if aiUnit == null.
Method Detail

dispose

public void dispose()
Disposes this mission by removing any references to it.

Overrides:
dispose in class AIObject

getUnit

public Unit getUnit()
Gets the unit this mission has been created for.

Returns:
The Unit.

getAIUnit

public AIUnit getAIUnit()
Gets the AI-unit this mission has been created for.

Returns:
The AIUnit.

setAIUnit

protected void setAIUnit(AIUnit aiUnit)
Sets the AI-unit this mission has been created for.

Parameters:
aiUnit - The AIUnit.

getAIRandom

protected java.util.Random getAIRandom()
Convenience accessor for the unit/player PRNG.

Returns:
A Random to use.

getEuropeanAIPlayer

protected EuropeanAIPlayer getEuropeanAIPlayer()
Convenience accessor for the owning European AI player.

Returns:
The EuropeanAIPlayer.

moveTowards

protected Map.Direction moveTowards(Tile tile)
Moves the unit owning this mission towards the given Tile. This is done in a loop until the tile is reached, there are no moves left, the path to the target cannot be found or that the next step is not a move.

Parameters:
tile - The Tile the unit should move towards.
Returns:
The direction to take the final move or null if the move can not be made.

moveTowards

protected Map.Direction moveTowards(PathNode pathNode)
Moves the unit owning this mission using the given path. This is done in a loop until the end of the path is reached, the next step is not a move or when there are no moves left.

Parameters:
pathNode - The first node of the path.
Returns:
The direction to continue moving the path or null if the move can not be made.

moveRandomly

protected void moveRandomly()
Move a unit randomly.


moveUnitToAmerica

protected void moveUnitToAmerica()

moveUnitToEurope

protected void moveUnitToEurope()

moveButDontAttack

protected boolean moveButDontAttack(Map.Direction direction)
Move in a specified direction, but do not attack. Always check the return from this in case the unit blundered into a lost city and died. The usual idiom is: "if (!moveButDontAttack(unit)) return;"

Parameters:
direction - The Direction to move.
Returns:
True if the unit doing this mission is still valid/alive.

findTarget

protected PathNode findTarget(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.

findNearestOtherSettlement

protected PathNode findNearestOtherSettlement(Unit unit)
Find the nearest reachable settlement to a unit (owned by the same player) excepting the any on the current tile.

Parameters:
unit - The Unit to check.
Returns:
The nearest settlement if any, otherwise null.

unitLeavesShip

protected boolean unitLeavesShip(AIUnit aiUnit)
Unload a unit.

Parameters:
aiUnit - The AIUnit to unload.
Returns:
True if the unit is unloaded.

unloadCargoInColony

protected boolean unloadCargoInColony(Goods goods)
Unload some goods in a colony.

Parameters:
goods - The Goods to unload.
Returns:
True if the goods are unloaded.

sellCargoInEurope

protected boolean sellCargoInEurope(Goods goods)
Sell some goods in Europe.

Parameters:
goods - The Goods to sell.
Returns:
True if the goods are sold.

shouldTakeTransportToTile

protected boolean shouldTakeTransportToTile(Tile tile)
Should the unit use transport to get to a specified tile? True if: - The unit is not there already AND - the unit already has transport, this will always be faster (TODO: actually, mounted units on good roads might be faster, check for this) - if not on the map - if on the map but can not find a path to the tile, unless adjacent to the destination which usually means the path finding failed due to a temporary blockage such as an enemy unit - if the path to the tile will take more than MAX_TURNS

Parameters:
tile - The Tile to go to.
Returns:
True if the unit should use transport.

getPathStartTile

protected static Tile getPathStartTile(Unit unit)
Gets a suitable tile to start path searches from for a unit. Must handle all the cases where the unit is off the map, and take account of the use of a carrier. If the unit is in or heading to Europe, return null because there is no good way to tell where the unit will reappear on the map, which is in question anyway if not on a carrier.

Parameters:
unit - The Unit to check.
Returns:
A suitable starting tile, or null if none found.

travelToTarget

protected Unit.MoveType travelToTarget(java.lang.String logMe,
                                       Location target)
Tries to move this mission's unit to a target location. First check for units in transit: that is units on a carrier that are going to but not yet in Europe, or going to but not yet at a Tile and whose path still requires the carrier. Similarly check for units not in transit but should be: that is units not on a carrier but can not get to their target without one. If there is no impediment to the unit moving towards the target, do so. Return an indicative MoveType for the result of the travel. - MOVE if the unit has arrived at the target. The unit may not have moved, or have exhausted its moves. - MOVE_NO_MOVES if out of moves short of the target - MOVE_ILLEGAL if the unit is unable to proceed for now - MOVE_NO_REPAIR if the unit died for whatever reason - other results (e.g. ENTER_INDIAN_SETTLEMENT*) if that would occur if the unit proceeded--- such moves require special handling and are not performed here, the calling mission code must handle them.

Parameters:
logMe - A prefix string for the log messages.
target - The destination Location.
Returns:
The type of move the unit stopped at.

getTransportDestination

public Location getTransportDestination()
Gets the transport destination of the unit associated with this mission.

Returns:
The destination of a required transport or null if no transport is needed.

getTransportPriority

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

Returns:
The priority.

isValid

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

Parameters:
aiUnit - The AIUnit to check.
Returns:
True if the unit can be usefully assigned this mission.

isValid

public boolean isValid()
Checks if this mission is still valid to perform. At this level, if the unit was killed then the mission becomes invalid. A mission can be invalidated for a number of subclass-specific reasons. For example: a seek-and-destroy mission could be invalidated when the relationship towards the targeted player improves.

Returns:
True if the unit is still intact to perform its mission.

isOneTime

public boolean isOneTime()
Checks if this Mission should only be carried out once. Missions are not one-time by default, true one-time missions must override this routine.

Returns:
False.

doMission

public abstract void doMission(Connection connection)
Performs the mission. Must be implemented by the subclasses.

Parameters:
connection - The Connection to the server.

getDebuggingInfo

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

Returns:
"".

writeAttributes

protected void writeAttributes(javax.xml.stream.XMLStreamWriter out)
                        throws javax.xml.stream.XMLStreamException

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

readAttributes

protected void readAttributes(javax.xml.stream.XMLStreamReader in)
                       throws javax.xml.stream.XMLStreamException

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