Package net.sf.freecol.server.ai.mission
Class Mission
- java.lang.Object
-
- net.sf.freecol.common.model.FreeColObject
-
- net.sf.freecol.server.ai.AIObject
-
- net.sf.freecol.server.ai.mission.Mission
-
- All Implemented Interfaces:
java.lang.Comparable<FreeColObject>,ObjectWithId
- Direct Known Subclasses:
BuildColonyMission,CashInTreasureTrainMission,DefendSettlementMission,IdleAtSettlementMission,IndianBringGiftMission,IndianDemandMission,MissionaryMission,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. EveryAIUnitshould have a mission. By extending this class, you create different missions.
-
-
Field Summary
Fields Modifier and Type Field Description private AIUnitaiUnitThe unit to undertake the mission.protected static java.lang.StringAIUNITDIEDprotected static java.lang.StringAIUNITNULLprivate static java.util.logging.Loggerloggerprotected static intMINIMUM_TRANSPORT_PRIORITYA transport can be used.protected static intNO_MORE_MOVES_LEFTprotected static intNO_PATH_TO_TARGETprotected static intNORMAL_TRANSPORT_PRIORITYTransport is required.protected static java.lang.StringTARGETINVALIDprotected static java.lang.StringTARGETNOTFOUNDprotected static java.lang.StringTARGETNULLprotected static java.lang.StringTARGETOWNERSHIPprotected static java.lang.StringUNITNOTAPERSONprotected static java.lang.StringUNITNOTOFFENSIVEprotected static java.lang.StringUNITNOTONMAP-
Fields inherited from class net.sf.freecol.server.ai.AIObject
initialized
-
Fields inherited from class net.sf.freecol.common.model.FreeColObject
ARRAY_SIZE_TAG, DEFAULT_CLASS_INDEX, fcoComparator, ID_ATTRIBUTE_TAG, PARTIAL_ATTRIBUTE_TAG, VALUE_TAG
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voiddispose()Disposes this mission by removing any references to it.abstract MissiondoMission(LogBuilder lb)Performs the mission.protected static LocationfindCircleTarget(AIUnit aiUnit, GoalDecider gd, int radius, boolean deferOK)Finds a target for a unit without considering its movement abilities.abstract LocationfindTarget()Finds a new target for this mission.private Unit.MoveTypefollowMapPath(PathNode path, LogBuilder lb)Follow a path that is on the map (except perhaps the last node) and does not use a carrier.protected MissionAIPlayergetAIPlayer()Convenience accessor for the owning AI player.protected java.util.RandomgetAIRandom()Convenience accessor for the unit/player PRNG.AIUnitgetAIUnit()Gets the AI-unit this mission has been created for.intgetBaseTransportPriority()Get the base transport priority for the unit performing this mission.protected static SettlementgetBestSettlement(Player player)Finds the best existing settlement to use as a target.protected EuropeanAIPlayergetEuropeanAIPlayer()Convenience accessor for the owning European AI player.protected PlayergetPlayer()Convenience accessor for the owning player.abstract LocationgetTarget()Gets the target of this mission, if any.LocationgetTransportDestination()Gets the destination of a required transport.UnitgetUnit()Gets the unit this mission has been created for.static java.lang.StringinvalidAIUnitReason(AIUnit aiUnit)Is an AI unit able to perform a mission of a particular type?static java.lang.StringinvalidAttackReason(AIUnit aiUnit, Player other)Is another player a valid attack target?static java.lang.StringinvalidMissionReason(AIUnit aiUnit)Is an AI unit able to perform a different mission? AIPlayers will call FooMission.invalidReason(aiUnit) to determine whether it is valid to assign some unit to a FooMission, so `interesting' Mission subclasses with complex validity requirements must implement a routine with this signature.static java.lang.StringinvalidMissionReason(AIUnit aiUnit, Location loc)Is an AI unit able to perform a mission with a specified target? Specific Missions can be invalid for target-related reasons.static java.lang.StringinvalidNewMissionReason(AIUnit aiUnit)Is an AI unable to perform a new mission because it already has a valid, non-onetime mission?abstract java.lang.StringinvalidReason()Why is this mission invalid? Mission subclasses must implement this routine, which probably should start by checking invalidAIUnitReason.static java.lang.StringinvalidTargetReason(Location target)Is a target a valid mission target?static java.lang.StringinvalidTargetReason(Location target, Player owner)Is a target a valid mission target?static java.lang.StringinvalidTransportableReason(TransportableAIObject t)Is there a reason to invalidate mission to move a transportable?private static java.lang.StringinvalidUnitReason(Unit unit)Is a unit able to perform a mission of a particular type?booleanisOneTime()Should this mission be considered a mere fallback to be replaced at will? Missions are not one-time by default, true one-time missions must override this routine.static booleanisTargetReason(java.lang.String reason)Is an invalidity reason due to a target failure?booleanisValid()Is this mission valid?protected MissionlbAt(LogBuilder lb)State where a unit is.protected MissionlbAttack(LogBuilder lb, Location what)State that the unit has made an attack.protected MissionlbDodge(LogBuilder lb)State that the unit is dodging.protected MissionlbDone(LogBuilder lb, boolean cont, java.lang.Object... reasons)State that this mission has completed successfully.protected MissionlbDrop(LogBuilder lb, java.lang.Object... reasons)Drop the current mission.protected MissionlbFail(LogBuilder lb, boolean cont, java.lang.Object... reasons)The current mission has failed.protected MissionlbMove(LogBuilder lb, Unit.MoveType mt)State that a bad move has occurred.protected MissionlbRetarget(LogBuilder lb)State that the mission has been retargeted.protected MissionlbWait(LogBuilder lb, java.lang.Object... reasons)State that the unit is waiting for something.protected DirectionmoveRandomly(java.lang.String logMe, Direction direction)Moves a unit one step randomly.protected voidmoveRandomlyTurn(java.lang.String logMe)Moves a unit randomly for the rest of its turn.protected voidreadAttributes(FreeColXMLReader xr)Reads the attributes of this object from an XML stream.static LocationresolveBlockage(AIUnit aiUnit, Location target)We have been blocked on the way to a target.MissionretargetMission(java.lang.String reason, LogBuilder lb)Retarget a mission because of some problem.voidsetInitialized()Set the initialized flag in this object.abstract voidsetTarget(Location target)Sets the target of this mission, if any.java.lang.StringtoString()voidtoXML(FreeColXMLWriter xw)This method writes an XML-representation of this object to the given stream.protected Unit.MoveTypetravelToTarget(Location target, CostDecider costDecider, LogBuilder lb)Tries to move this mission's unit to a target location.protected voidwriteAttributes(FreeColXMLWriter xw)Write the attributes of this object to a stream.-
Methods inherited from class net.sf.freecol.server.ai.AIObject
addAIObjectWithId, checkIntegrity, checkIntegrity, copyIn, getAIMain, getGame, getSpecification, isDisposed, setGame, setSpecification
-
Methods inherited from class net.sf.freecol.common.model.FreeColObject
addAbility, addFeatures, addModifier, addPropertyChangeListener, addPropertyChangeListener, apply, apply, applyModifiers, applyModifiers, arrayKey, compareIds, compareTo, containsAbilityKey, containsModifierKey, copy, copy, copy, copy, copyInCast, dumpObject, equals, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAbilities, getAbilities, getAbilities, getAbilities, getClassIndex, getDefenceModifiers, getDisplayObject, getFeatureContainer, getFreeColObjectClass, getFreeColObjectClassByName, getId, getIdNumber, getIdType, getIdTypeByName, getModifiers, getModifiers, getModifiers, getModifiers, getObjectClassIndex, getPropertyChangeListeners, getPropertyChangeListeners, getPropertyChangeSupport, getSortedAbilities, getSortedModifiers, getSuffix, getSuffix, getXMLTagName, hasAbility, hasAbility, hasAbility, hashCode, hasListeners, hasModifier, hasModifier, hasModifier, idEquals, invokeMethod, logFreeColObjects, readChild, readChildren, readFromXML, readFromXMLPartial, removeAbilities, removeAbility, removeFeatures, removeModifier, removeModifiers, removePropertyChangeListener, removePropertyChangeListener, save, save, save, save, serialize, serialize, serialize, serialize, setId, toXML, toXMLPartial, toXMLPartial, writeChildren
-
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
MINIMUM_TRANSPORT_PRIORITY
protected static final int MINIMUM_TRANSPORT_PRIORITY
A transport can be used.- See Also:
- Constant Field Values
-
NORMAL_TRANSPORT_PRIORITY
protected static final int NORMAL_TRANSPORT_PRIORITY
Transport is required.- 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
-
AIUNITNULL
protected static final java.lang.String AIUNITNULL
- See Also:
- Constant Field Values
-
AIUNITDIED
protected static final java.lang.String AIUNITDIED
- See Also:
- Constant Field Values
-
TARGETNULL
protected static final java.lang.String TARGETNULL
- See Also:
- Constant Field Values
-
TARGETINVALID
protected static final java.lang.String TARGETINVALID
- See Also:
- Constant Field Values
-
TARGETOWNERSHIP
protected static final java.lang.String TARGETOWNERSHIP
- See Also:
- Constant Field Values
-
TARGETNOTFOUND
protected static final java.lang.String TARGETNOTFOUND
- See Also:
- Constant Field Values
-
UNITNOTAPERSON
protected static final java.lang.String UNITNOTAPERSON
- See Also:
- Constant Field Values
-
UNITNOTOFFENSIVE
protected static final java.lang.String UNITNOTOFFENSIVE
- See Also:
- Constant Field Values
-
UNITNOTONMAP
protected static final java.lang.String UNITNOTONMAP
- See Also:
- Constant Field Values
-
aiUnit
private final AIUnit aiUnit
The unit to undertake the mission.
-
-
Constructor Detail
-
Mission
protected Mission(AIMain aiMain, AIUnit aiUnit)
Creates a mission for the givenAIUnit. Note that missions are attached to their units, and thus do not need AI ids, hence the plain superclass constructor.- Parameters:
aiMain- The main AI-object.aiUnit- TheAIUnitthis mission is created for.
-
-
Method Detail
-
setInitialized
public final void setInitialized()
Set the initialized flag in this object. To be implemented by leaf classes, and called in their constructors plus the special case in readChild below where we resolve forward references.- Specified by:
setInitializedin classAIObject
-
getAIUnit
public final AIUnit getAIUnit()
Gets the AI-unit this mission has been created for.- Returns:
- The
AIUnit.
-
getUnit
public final Unit getUnit()
Gets the unit this mission has been created for.- Returns:
- The
Unit.
-
getPlayer
protected final Player getPlayer()
Convenience accessor for the owning player.- Returns:
- The
Playerthat owns the mission unit.
-
getAIPlayer
protected final MissionAIPlayer getAIPlayer()
Convenience accessor for the owning AI player.- Returns:
- The
AIPlayer.
-
getEuropeanAIPlayer
protected final EuropeanAIPlayer getEuropeanAIPlayer()
Convenience accessor for the owning European AI player.- Returns:
- The
EuropeanAIPlayer.
-
getAIRandom
protected final java.util.Random getAIRandom()
Convenience accessor for the unit/player PRNG.- Returns:
- A
Randomto use.
-
isValid
public final boolean isValid()
Is this mission valid?- Returns:
- True if the mission is valid.
-
isTargetReason
public static boolean isTargetReason(java.lang.String reason)
Is an invalidity reason due to a target failure?- Parameters:
reason- The reason to check.- Returns:
- True if the reason starts with "target-".
-
invalidUnitReason
private static java.lang.String invalidUnitReason(Unit unit)
Is a unit able to perform a mission of a particular type?- Parameters:
unit- TheUnitto check.- Returns:
- A reason for mission invalidity, or null if none found.
-
invalidAIUnitReason
public static java.lang.String invalidAIUnitReason(AIUnit aiUnit)
Is an AI unit able to perform a mission of a particular type?- Parameters:
aiUnit- TheAIUnitto check.- Returns:
- A reason for mission invalidity, or null if none found.
-
invalidNewMissionReason
public static java.lang.String invalidNewMissionReason(AIUnit aiUnit)
Is an AI unable to perform a new mission because it already has a valid, non-onetime mission?- Parameters:
aiUnit- TheAIUnitto test.- Returns:
- "mission-exists" if a valid mission is found, or null if none found.
-
invalidTargetReason
public static java.lang.String invalidTargetReason(Location target)
Is a target a valid mission target?- Parameters:
target- The targetLocationto check.- Returns:
- A reason for the target to be invalid, or null if none found.
-
invalidTargetReason
public static java.lang.String invalidTargetReason(Location target, Player owner)
Is a target a valid mission target?- Parameters:
target- The targetLocationto check.owner- APlayerthat should own the target.- Returns:
- A reason for the target to be invalid, or null if none found.
-
invalidTransportableReason
public static java.lang.String invalidTransportableReason(TransportableAIObject t)
Is there a reason to invalidate mission to move a transportable?- Parameters:
t- TheTransportableAIObjectto check.- Returns:
- A reason for the transport to be invalid, or null if none found.
-
invalidAttackReason
public static java.lang.String invalidAttackReason(AIUnit aiUnit, Player other)
Is another player a valid attack target?- Parameters:
aiUnit- TheAIUnitthat will attack.other- ThePlayerto attack.- Returns:
- A reason why the attack would be invalid, or null if none found.
-
invalidMissionReason
public static java.lang.String invalidMissionReason(AIUnit aiUnit)
Is an AI unit able to perform a different mission? AIPlayers will call FooMission.invalidReason(aiUnit) to determine whether it is valid to assign some unit to a FooMission, so `interesting' Mission subclasses with complex validity requirements must implement a routine with this signature. Conversely, simple Missions that are always possible need not. Implementations should usually start by calling this routine (i.e. Mission.invalidReason(AIUnit)).- Parameters:
aiUnit- TheAIUnitto check.- Returns:
- A reason for mission invalidity, or null if none found.
-
invalidMissionReason
public static java.lang.String invalidMissionReason(AIUnit aiUnit, Location loc)
Is an AI unit able to perform a mission with a specified target? Specific Missions can be invalid for target-related reasons. Such Missions need to implement a routine with this signature, as it will be called by the GoalDeciders in map path find/searches to choose a Mission target. Implementations should usually start by calling either invalidAIUnitReason() or this routine if the target checking is trivial.- Parameters:
aiUnit- TheAIUnitto check.loc- The targetLocationto check.- Returns:
- A reason for mission invalidity, or null if none found.
-
lbAt
protected Mission lbAt(LogBuilder lb)
State where a unit is.- Parameters:
lb- ALogBuilderto log to.- Returns:
- This
Mission.
-
lbAttack
protected Mission lbAttack(LogBuilder lb, Location what)
State that the unit has made an attack.- Parameters:
lb- ALogBuilderto log to.what- What is being attacked (aUnitorSettlement).- Returns:
- This
Mission.
-
lbDodge
protected Mission lbDodge(LogBuilder lb)
State that the unit is dodging.- Parameters:
lb- ALogBuilderto log to.- Returns:
- This
Mission.
-
lbDone
protected Mission lbDone(LogBuilder lb, boolean cont, java.lang.Object... reasons)
State that this mission has completed successfully.- Parameters:
lb- ALogBuilderto log to.cont- If true, the mission should continue, otherwise drop the mission.reasons- Reasons for the successful completion.- Returns:
- The current
Missionof the unit, which may now be different fromthis.
-
lbDrop
protected Mission lbDrop(LogBuilder lb, java.lang.Object... reasons)
Drop the current mission.- Parameters:
lb- ALogBuilderto log to.reasons- Optional reasons to drop the mission.- Returns:
- Null, which is now the current
Missionof this unit.
-
lbFail
protected Mission lbFail(LogBuilder lb, boolean cont, java.lang.Object... reasons)
The current mission has failed.- Parameters:
lb- ALogBuilderto log to.cont- If true, the mission should continue, otherwise drop the mission.reasons- Reasons for the successful completion.- Returns:
- The current
Missionof the unit, which may not be different fromthis.
-
lbMove
protected Mission lbMove(LogBuilder lb, Unit.MoveType mt)
State that a bad move has occurred.- Parameters:
lb- ALogBuilderto log to.mt- The badMoveType.- Returns:
- This
Mission.
-
lbRetarget
protected Mission lbRetarget(LogBuilder lb)
State that the mission has been retargeted.- Parameters:
lb- ALogBuilderto log to.- Returns:
- This
Mission.
-
lbWait
protected Mission lbWait(LogBuilder lb, java.lang.Object... reasons)
State that the unit is waiting for something.- Parameters:
lb- ALogBuilderto log to.reasons- Reasons for the wait.- Returns:
- This
Mission.
-
findCircleTarget
protected static Location findCircleTarget(AIUnit aiUnit, GoalDecider gd, int radius, boolean deferOK)
Finds a target for a unit without considering its movement abilities. This is used by missions when the current unit can not find a target with the normal path finding routines, and thus should consider targets that may require a carrier.- Parameters:
aiUnit- TheAIUnitthat is searching.gd- TheGoalDeciderthat selects targets.radius- A maximum radius from the unit location to search within.deferOK- If true, fall back to the nearest port to Europe.- Returns:
- The best target
Tilefound, or null if none.
-
resolveBlockage
public static Location resolveBlockage(AIUnit aiUnit, Location target)
We have been blocked on the way to a target. Is it valid to attack the blockage, or should it just be avoided?- Parameters:
aiUnit- TheAIUnitthat was blocked.target- The targetLocation.- Returns:
- The blockage to attack, or null if not.
-
moveRandomly
protected Direction moveRandomly(java.lang.String logMe, Direction direction)
Moves a unit one step randomly.- Parameters:
logMe- A string to log the random number generation with.direction- An optional preferredDirection.- Returns:
- The direction of the move, or null if no move was made.
-
moveRandomlyTurn
protected void moveRandomlyTurn(java.lang.String logMe)
Moves a unit randomly for the rest of its turn.- Parameters:
logMe- A string to log the random number generation with.
-
getBestSettlement
protected static Settlement getBestSettlement(Player player)
Finds the best existing settlement to use as a target. Useful for missions where the unit might be in Europe, but should go to a safe spot in the New World and proceed from there.- Parameters:
player- ThePlayerthat is searching.- Returns:
- A good settlement to restart a Mission from.
-
travelToTarget
protected Unit.MoveType travelToTarget(Location target, CostDecider costDecider, LogBuilder lb)
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. These need to be handled by the carrier's TransportMission, not by the unit's Mission. 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. These must just wait. 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, although it may have exhausted its moves - MOVE_HIGH_SEAS if the unit has set sail to/from Europe - MOVE_NO_MOVES is underway but ran out of moves - MOVE_NO_ACCESS_EMBARK if progress depends on a carrier, either currently boarded or due to collect the unit - MOVE_NO_REPAIR if the unit died for whatever reason - MOVE_NO_TILE if there is no path (usually transitory on rivers) - MOVE_ILLEGAL if there is an error or permanent restriction - other legal 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:
target- The destinationLocation.costDecider- TheCostDeciderto use in any path finding.lb- ALogBuilderto log to.- Returns:
- The type of move the unit stopped at.
-
followMapPath
private Unit.MoveType followMapPath(PathNode path, LogBuilder lb)
Follow a path that is on the map (except perhaps the last node) and does not use a carrier.- Parameters:
path- ThePathNodeto follow.lb- ALogBuilderto log to.- Returns:
- The type of move the unit stopped at.
-
retargetMission
public Mission retargetMission(java.lang.String reason, LogBuilder lb)
Retarget a mission because of some problem.- Parameters:
reason- The reason for the retarget.lb- ALogBuilderto log to.- Returns:
- The current
Mission, which has been set to null on failure to retarget.
-
dispose
public void dispose()
Disposes this mission by removing any references to it.
-
getBaseTransportPriority
public int getBaseTransportPriority()
Get the base transport priority for the unit performing this mission.- Returns:
- A base transport priority.
-
getTransportDestination
public Location getTransportDestination()
Gets the destination of a required transport. Override this in the child mission classes if there is a useful intermediate point to deliver the unit distinct from the target.- Returns:
- The mission target, or null if the mission is invalid, otherwise lacks a target (e.g. UnitWanderHostile), or the unit does not need transport.
-
getTarget
public abstract Location getTarget()
Gets the target of this mission, if any.- Returns:
- The target of this mission, or null if none.
-
setTarget
public abstract void setTarget(Location target)
Sets the target of this mission, if any. The actual target is handled in the missions that really have them, this is a helper to make sure the unit is updated.- Parameters:
target- The new target of this mission, or null if none.
-
findTarget
public abstract Location findTarget()
Finds a new target for this mission.- Returns:
- A new target for this mission.
-
isOneTime
public boolean isOneTime()
Should this mission be considered a mere fallback to be replaced at will? Missions are not one-time by default, true one-time missions must override this routine.- Returns:
- False.
-
invalidReason
public abstract java.lang.String invalidReason()
Why is this mission invalid? Mission subclasses must implement this routine, which probably should start by checking invalidAIUnitReason. A mission can be invalid for a number of subclass-specific reasons. For example: a seek-and-destroy mission could be invalid because of a improved stance towards the targeted player.- Returns:
- A reason for mission invalidity, or null if none found.
-
doMission
public abstract Mission doMission(LogBuilder lb)
Performs the mission.- Parameters:
lb- ALogBuilderto log to.- Returns:
- The
Missionto continue with, or null if the current mission has completed.
-
toXML
public final void toXML(FreeColXMLWriter xw) throws javax.xml.stream.XMLStreamException
This method writes an XML-representation of this object to the given stream. All attributes will be made visible.- Overrides:
toXMLin classFreeColObject- Parameters:
xw- TheFreeColXMLWriterto write to.- Throws:
javax.xml.stream.XMLStreamException- if there are any problems writing to the stream.
-
writeAttributes
protected void writeAttributes(FreeColXMLWriter xw) throws javax.xml.stream.XMLStreamException
Write the attributes of this object to a stream. To be overridden if required by any object that has attributes and uses the toXML(FreeColXMLWriter, String) call.- Overrides:
writeAttributesin classFreeColObject- Parameters:
xw- TheFreeColXMLWriterto write to.- Throws:
javax.xml.stream.XMLStreamException- if there are any problems writing to the stream.
-
readAttributes
protected void readAttributes(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Reads the attributes of this object from an XML stream.- Overrides:
readAttributesin classFreeColObject- Parameters:
xr- TheFreeColXMLReaderto read from.- Throws:
javax.xml.stream.XMLStreamException- if there is a problem reading the stream.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classFreeColObject
-
-