Class AIUnit

    • Field Detail

      • logger

        private static final java.util.logging.Logger logger
      • unit

        private Unit unit
        The Unit this AIObject contains AI-information for.
      • mission

        private Mission mission
        The mission to which this AI unit has been assigned.
    • Constructor Detail

      • AIUnit

        public AIUnit​(AIMain aiMain,
                      java.lang.String id)
        Creates a new uninitialized AIUnit.
        Parameters:
        aiMain - The main AI-object.
        id - The object identifier.
      • AIUnit

        public AIUnit​(AIMain aiMain,
                      Unit unit)
        Creates a new AIUnit.
        Parameters:
        aiMain - The main AI-object.
        unit - The unit to make an AIObject for.
      • AIUnit

        public AIUnit​(AIMain aiMain,
                      FreeColXMLReader xr)
               throws javax.xml.stream.XMLStreamException
        Creates a new AIUnit from the given XML-representation.
        Parameters:
        aiMain - The main AI-object.
        xr - The input stream containing the XML.
        Throws:
        javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.
    • Method Detail

      • setInitialized

        public 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
      • getUnit

        public final Unit getUnit()
        Gets the Unit this AIUnit controls.
        Returns:
        The Unit.
      • setUnit

        private final void setUnit​(Unit unit)
        Set the Unit this AIUnit controls. Note: it is an error to change the unit once it is set.
        Parameters:
        unit - The new Unit.
      • hasMission

        public final boolean hasMission()
        Checks if this unit has been assigned a mission.
        Returns:
        True if this unit has a mission.
      • getMission

        public final Mission getMission()
        Gets the mission this unit has been assigned.
        Returns:
        The Mission.
      • setMission

        public final void setMission​(Mission mission)
        Assigns a mission to unit.
        Parameters:
        mission - The new Mission.
      • requestLocalRearrange

        private void requestLocalRearrange()
        Request a rearrangement of any colony at the current location.
      • takeTransport

        private void takeTransport()
        Take the current carrier as transport, keeping the transport mission/s consistent.
      • getOwner

        public Player getOwner()
        Get the Player that owns this AIUnit.
        Returns:
        The owning Player.
      • getAIOwner

        public AIPlayer getAIOwner()
        Gets the AIPlayer that owns this AIUnit.
        Returns:
        The owning AIPlayer.
      • getAIRandom

        public java.util.Random getAIRandom()
        Gets the PRNG to use with this unit.
        Returns:
        A Random instance.
      • getTrivialTarget

        public Location getTrivialTarget()
        Get a trivial target, usually a safe nearby settlement or Europe.
        Returns:
        A trivial target, or null if none found.
      • hasCargo

        public final boolean hasCargo()
        Is this AI unit carrying any cargo (units or goods).
        Returns:
        True if the unit has cargo aboard.
      • hasMission

        public <T extends Mission> boolean hasMission​(java.lang.Class<T> returnClass)
        Does this unit have a particular class of mission?
        Type Parameters:
        T - The type of the mission.
        Parameters:
        returnClass - The Class of mission to check.
        Returns:
        True if the mission is of the given class.
      • getMission

        public <T extends Mission> T getMission​(java.lang.Class<T> returnClass)
        Get the unit mission if it is of a given class.
        Type Parameters:
        T - The type of the mission.
        Parameters:
        returnClass - The Class of the mission.
        Returns:
        The Mission, or null if it is not of the given class.
      • doMission

        public void doMission​(LogBuilder lb)
        Performs the mission this unit has been assigned. Do *not* check mission validity. The mission itself does that, and has special case error handling.
        Parameters:
        lb - A LogBuilder to log to.
      • changeMission

        public Mission changeMission​(Mission mission)
        Change the mission of a unit.
        Parameters:
        mission - The new Mission.
        Returns:
        The new current Mission.
      • removeMission

        public void removeMission()
      • hasDefendSettlementMission

        public boolean hasDefendSettlementMission()
      • isCompleteWishRealizationMission

        public boolean isCompleteWishRealizationMission​(Colony colony)
      • isAvailableForWork

        public boolean isAvailableForWork​(Colony colony)
      • tryWorkInsideColonyMission

        public boolean tryWorkInsideColonyMission​(AIColony aiColony,
                                                  LogBuilder lb)
      • tryPioneeringMission

        public boolean tryPioneeringMission​(LogBuilder lb)
      • trySomeUsefulMission

        public boolean trySomeUsefulMission​(Colony colony,
                                            LogBuilder lb)
      • removeTransportable

        public void removeTransportable​(AIGoods ag)
      • moveToAmerica

        public boolean moveToAmerica()
        Moves a unit to the new world.
        Returns:
        True if there was no c-s problem.
      • moveToEurope

        public boolean moveToEurope()
        Moves a unit to Europe.
        Returns:
        True if there was no c-s problem.
      • move

        public boolean move​(Direction direction)
        Moves this AI unit.
        Parameters:
        direction - The Direction to move.
        Returns:
        True if the move succeeded.
      • equipForRole

        public boolean equipForRole​(Role role)
        Equips this AI unit for a particular role. The unit must be at a location where the required goods are available (possibly requiring a purchase, which may fail due to lack of gold or boycotts in effect).
        Parameters:
        role - The Role to equip for identifier.
        Returns:
        True if the role change was successful.
      • getBuilderScore

        public int getBuilderScore()
        Score this AI unit with its suitability for building. Favour unequipped freeColonists, and other unskilled over experts. Also slightly favour units on the map.
        Returns:
        An integer score.
      • getPioneerScore

        public int getPioneerScore()
        Score this AI unit with its suitability for pioneering.
        Returns:
        An integer score.
      • getScoutScore

        public int getScoutScore()
        Score this AI unit with its suitability for scouting.
        Returns:
        An integer score.
      • getTransportPriority

        public int getTransportPriority()
        Gets the priority of transporting this object to its destination.
        Overrides:
        getTransportPriority in class TransportableAIObject
        Returns:
        The priority of the transport.
      • getTransportDestination

        public Location getTransportDestination()
        Get the destination location for this transportable AI object. This can be the target Tile of the transport or the target of the mission.
        Specified by:
        getTransportDestination in class TransportableAIObject
        Returns:
        The destination Location.
      • getDeliveryPath

        public PathNode getDeliveryPath​(Unit carrier,
                                        Location dst)
        Get the path to deliver this transportable to its destination with a given carrier.
        Specified by:
        getDeliveryPath in class TransportableAIObject
        Parameters:
        carrier - The carrier Unit to use.
        dst - The destination Location, defaulting to the transport destination if null.
        Returns:
        A path, or null if none found.
      • getIntermediatePath

        public PathNode getIntermediatePath​(Unit carrier,
                                            Location dst)
        Get the path to make progress with this transport, for the carrier to either collect or deliver the transportable, albeit it need only improve the current situation rather than complete the delivery to the destination.
        Specified by:
        getIntermediatePath in class TransportableAIObject
        Parameters:
        carrier - The carrier Unit to use.
        dst - The destination Location, defaulting to the transport destination if null.
        Returns:
        A path, or null if none found.
      • setTransportDestination

        public void setTransportDestination​(Location destination)
        Set the destination location for this transportable AI object.
        Specified by:
        setTransportDestination in class TransportableAIObject
        Parameters:
        destination - The destination Location.
      • carriableBy

        public boolean carriableBy​(Unit carrier)
        Can this transportable be carried by a given carrier unit?
        Specified by:
        carriableBy in class TransportableAIObject
        Parameters:
        carrier - The potential carrier Unit.
        Returns:
        True if the unit can carry this transportable.
      • canMove

        public boolean canMove()
        This transportable can move now. Useful for units that may or may not have enough moves left to join or leave a carrier.
        Specified by:
        canMove in class TransportableAIObject
        Returns:
        True if the transportable can move.
      • leaveTransport

        public boolean leaveTransport()
        This object leaves its current carrier unit by the most suitable means.
        Specified by:
        leaveTransport in class TransportableAIObject
        Returns:
        True if the object successfully left the carrier.
      • leaveTransport

        public boolean leaveTransport​(Direction direction)
        This object leaves its current carrier unit.
        Specified by:
        leaveTransport in class TransportableAIObject
        Parameters:
        direction - The Direction to leave in, null to leave in place.
        Returns:
        True if the object successfully left the carrier.
      • joinTransport

        public boolean joinTransport​(Unit carrier,
                                     Direction direction)
        This object joins a carrier unit.
        Specified by:
        joinTransport in class TransportableAIObject
        Parameters:
        carrier - The carrier Unit to join.
        direction - The Direction to move, null to join a carrier in the same location.
        Returns:
        True if the object has joined tha carrier.
      • invalidReason

        public java.lang.String invalidReason()
        Is there a reason to invalidate transporting this object?
        Specified by:
        invalidReason in class TransportableAIObject
        Returns:
        A reason to abort transport, or null if none found.
      • dispose

        public void dispose()
        Disposes this object and any attached mission.
        Overrides:
        dispose in class AIObject
      • checkIntegrity

        public Constants.IntegrityType checkIntegrity​(boolean fix,
                                                      LogBuilder lb)
        AIObjects need integrity checking too.
        Overrides:
        checkIntegrity in class AIObject
        Parameters:
        fix - If true, fix problems if possible.
        lb - A LogBuilder to log to.
        Returns:
        -1 if there are problems remaining, zero if problems were fixed, +1 if no problems found at all.
      • writeChildren

        protected void writeChildren​(FreeColXMLWriter xw)
                              throws javax.xml.stream.XMLStreamException
        Write the children of this object to a stream. To be overridden if required by any object that has children and uses the toXML(FreeColXMLWriter, String) call.
        Overrides:
        writeChildren 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 TransportableAIObject
        Parameters:
        xr - The FreeColXMLReader to read from.
        Throws:
        javax.xml.stream.XMLStreamException - if there is a problem reading the stream.
      • readChild

        protected void readChild​(FreeColXMLReader xr)
                          throws javax.xml.stream.XMLStreamException
        Reads a single child object. Subclasses must override to read their enclosed elements. This particular instance of the routine always throws XMLStreamException because we should never arrive here. However it is very useful to always call super.readChild() when an unexpected tag is encountered, as the exception thrown here provides some useful debugging context.
        Overrides:
        readChild in class FreeColObject
        Parameters:
        xr - The FreeColXMLReader to read from.
        Throws:
        javax.xml.stream.XMLStreamException - if there is a problem reading the stream.
      • getXMLTagName

        public java.lang.String getXMLTagName()
        Get the serialization tag for this object.
        Specified by:
        getXMLTagName in class FreeColObject
        Returns:
        The tag.