Class Mission

    • 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
      • TARGETOWNERSHIP

        protected static final java.lang.String TARGETOWNERSHIP
        See Also:
        Constant Field Values
      • UNITNOTOFFENSIVE

        protected static final java.lang.String UNITNOTOFFENSIVE
        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 given AIUnit. 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 - The AIUnit this 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:
        setInitialized in class AIObject
      • 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 Player that 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 Random to 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 - The Unit to 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 - The AIUnit to 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 - The AIUnit to 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 target Location to 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 target Location to check.
        owner - A Player that 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 - The TransportableAIObject to 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 - The AIUnit that will attack.
        other - The Player to 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 - The AIUnit to 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 - The AIUnit to check.
        loc - The target Location to 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 - A LogBuilder to log to.
        Returns:
        This Mission.
      • lbAttack

        protected Mission lbAttack​(LogBuilder lb,
                                   Location what)
        State that the unit has made an attack.
        Parameters:
        lb - A LogBuilder to log to.
        what - What is being attacked (a Unit or Settlement).
        Returns:
        This Mission.
      • lbDodge

        protected Mission lbDodge​(LogBuilder lb)
        State that the unit is dodging.
        Parameters:
        lb - A LogBuilder to 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 - A LogBuilder to log to.
        cont - If true, the mission should continue, otherwise drop the mission.
        reasons - Reasons for the successful completion.
        Returns:
        The current Mission of the unit, which may now be different from this.
      • lbDrop

        protected Mission lbDrop​(LogBuilder lb,
                                 java.lang.Object... reasons)
        Drop the current mission.
        Parameters:
        lb - A LogBuilder to log to.
        reasons - Optional reasons to drop the mission.
        Returns:
        Null, which is now the current Mission of this unit.
      • lbFail

        protected Mission lbFail​(LogBuilder lb,
                                 boolean cont,
                                 java.lang.Object... reasons)
        The current mission has failed.
        Parameters:
        lb - A LogBuilder to log to.
        cont - If true, the mission should continue, otherwise drop the mission.
        reasons - Reasons for the successful completion.
        Returns:
        The current Mission of the unit, which may not be different from this.
      • lbMove

        protected Mission lbMove​(LogBuilder lb,
                                 Unit.MoveType mt)
        State that a bad move has occurred.
        Parameters:
        lb - A LogBuilder to log to.
        mt - The bad MoveType.
        Returns:
        This Mission.
      • lbRetarget

        protected Mission lbRetarget​(LogBuilder lb)
        State that the mission has been retargeted.
        Parameters:
        lb - A LogBuilder to 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 - A LogBuilder to 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 - The AIUnit that is searching.
        gd - The GoalDecider that 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 Tile found, 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 - The AIUnit that was blocked.
        target - The target Location.
        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 preferred Direction.
        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 - The Player that 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 destination Location.
        costDecider - The CostDecider to use in any path finding.
        lb - A LogBuilder to 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 - The PathNode to follow.
        lb - A LogBuilder to 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 - A LogBuilder to 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.
        Overrides:
        dispose in class AIObject
      • 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 - A LogBuilder to log to.
        Returns:
        The Mission to 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:
        toXML in class FreeColObject
        Parameters:
        xw - The FreeColXMLWriter to 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:
        writeAttributes in class FreeColObject
        Parameters:
        xw - The FreeColXMLWriter to 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:
        readAttributes in class FreeColObject
        Parameters:
        xr - The FreeColXMLReader to read from.
        Throws:
        javax.xml.stream.XMLStreamException - if there is a problem reading the stream.