Class AIPlayer

    • Field Detail

      • logger

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

        private static final java.lang.Runnable nullRunnable
        Do nothing!
      • aiUnitLocationComparator

        private static final java.util.Comparator<AIUnit> aiUnitLocationComparator
        A comparator to sort AI units by location.
      • player

        private Player player
        The FreeColGameObject this AIObject contains AI-information for.
      • aiRandom

        private java.util.Random aiRandom
        The PRNG to use for this AI player.
      • serverAPI

        private AIServerAPI serverAPI
        The wrapper for the server.
      • RANDOM_STATE_TAG

        private static final java.lang.String RANDOM_STATE_TAG
        See Also:
        Constant Field Values
    • Constructor Detail

      • AIPlayer

        protected AIPlayer​(AIMain aiMain,
                           Player player)
        Creates a new AI player.
        Parameters:
        aiMain - The AIMain the player exists within.
        player - The Player to associate this AI player with.
      • AIPlayer

        public AIPlayer​(AIMain aiMain,
                        FreeColXMLReader xr)
                 throws javax.xml.stream.XMLStreamException
        Creates a new AIPlayer 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 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
      • getPlayer

        public Player getPlayer()
        Gets the Player this AIPlayer is controlling.
        Returns:
        The Player.
      • getAIRandom

        public java.util.Random getAIRandom()
        Gets the PRNG to use for this player.
        Returns:
        A Random to use for this player.
      • getAIAdvantage

        protected java.lang.String getAIAdvantage()
        Gets the advantage of this AI player from the nation type.
        Returns:
        A short string stating the national advantage.
      • getConnection

        public Connection getConnection()
        Gets the connection to the server.
        Returns:
        The connection that can be used when communication with the server.
      • askServer

        public AIServerAPI askServer()
        Meaningfully named access to the server API.
        Returns:
        The AIServerAPI wrapper.
      • removeAIObject

        public void removeAIObject​(AIObject ao)
        Remove one of our owned objects. Subclasses to override.
        Parameters:
        ao - The AIObject to remove.
      • getAIColony

        public AIColony getAIColony​(Colony colony)
        Gets the AI colony corresponding to a given colony, if any.
        Parameters:
        colony - The Colony to look up.
        Returns:
        The corresponding AI colony or null if not found.
      • getAIColonies

        public java.util.List<AIColony> getAIColonies()
        Gets a list of the players AI colonies.
        Returns:
        A list of AI colonies.
      • getAIUnit

        protected AIUnit getAIUnit​(Unit unit)
        Gets the AI unit corresponding to a given unit, if any.
        Parameters:
        unit - The Unit to look up.
        Returns:
        The corresponding AI unit or null if not found.
      • getAIUnits

        protected java.util.List<AIUnit> getAIUnits()
        Gets a list of AIUnits for the player.
        Returns:
        A list of AIUnits.
      • determineStance

        protected Stance determineStance​(Player other)
        Standard stance change determination.
        Parameters:
        other - The Player wrt consider stance.
        Returns:
        The new Stance.
      • 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.
      • invoke

        protected void invoke​(java.lang.Runnable runnable)
        Do some work in a subthread.
        Parameters:
        runnable - The Runnable work to do.
      • chooseFoundingFatherHandler

        public void chooseFoundingFatherHandler​(java.util.List<FoundingFather> fathers)
        Choose a founding father.
        Parameters:
        fathers - A list of FoundingFathers to choose from.
      • diplomacyHandler

        public void diplomacyHandler​(FreeColGameObject our,
                                     FreeColGameObject other,
                                     DiplomaticTrade agreement)
        Handle a diplomacy message.
        Parameters:
        our - Our FreeColGameObject.
        other - Their FreeColGameObject.
        agreement - The DiplomaticTrade being negotiated.
      • firstContactHandler

        public void firstContactHandler​(Player contactor,
                                        Player contactee,
                                        Tile tile)
        Handle a first contact.
        Parameters:
        contactor - The contacting Player.
        contactee - The contacted Player.
        tile - The Tile where contact occurs.
      • fountainOfYouthHandler

        public void fountainOfYouthHandler​(int n)
        Handle a fountain of youth.
        Parameters:
        n - The number of emigrating units.
      • indianDemandHandler

        public void indianDemandHandler​(Unit unit,
                                        Colony colony,
                                        GoodsType type,
                                        int amount,
                                        Constants.IndianDemandAction initial)
        Handle a native demand.
        Parameters:
        unit - The Unit making demands.
        colony - The Colony where demands are being made.
        type - The GoodsType demanded.
        amount - The amount of gold demanded.
        initial - The acceptance state of the demand.
      • lootCargoHandler

        public void lootCargoHandler​(Unit unit,
                                     java.util.List<Goods> initialGoods,
                                     java.lang.String defenderId)
        Handle a looting.
        Parameters:
        unit - The looting Unit.
        initialGoods - A list of Goods to choose from.
        defenderId - The identifier for the defending unit (may be gone!).
      • monarchActionHandler

        public void monarchActionHandler​(Monarch.MonarchAction action,
                                         int tax)
        Handle the monarch.
        Parameters:
        action - The MonarchAction to respond to.
        tax - The tax change if any.
      • nationSummaryHandler

        public void nationSummaryHandler​(Player other,
                                         NationSummary ns)
        Handle an incoming nation summary.
        Parameters:
        other - The Player the summary applies to.
        ns - The NationSummary itself.
      • nativeTradeHandler

        public void nativeTradeHandler​(NativeTrade.NativeTradeAction action,
                                       NativeTrade nt)
        Handle a native trade.
        Parameters:
        action - The NativeTradeAction to perform.
        nt - The NativeTrade itself.
      • newLandNameHandler

        public void newLandNameHandler​(Unit unit,
                                       java.lang.String name)
        Handle a new land name.
        Parameters:
        unit - The Unit discovering the new world.
        name - The default name.
      • newRegionNameHandler

        public void newRegionNameHandler​(Region region,
                                         Tile tile,
                                         Unit unit,
                                         java.lang.String name)
        Handle a new region naming.
        Parameters:
        region - The Region to name.
        tile - The Tile the tile where the region was found.
        unit - The Unit that discovers the region.
        name - The default name.
      • reconnectHandler

        public void reconnectHandler()
        Handle reconnect.
      • setAIHandler

        public void setAIHandler​(Player player,
                                 boolean ai)
        Handle setting the AI state of a player.
        Parameters:
        player - The Player to set.
        ai - The new AI state.
      • setCurrentPlayerHandler

        public void setCurrentPlayerHandler​(Player currentPlayer)
        Handle a current player setting.
        Parameters:
        currentPlayer - The new current Player.
      • startWorking

        public abstract void startWorking()
        Tells this AIPlayer to make decisions. The AIPlayer is done doing work this turn when this method returns.
      • indianDemand

        public Constants.IndianDemandAction indianDemand​(Unit unit,
                                                         Colony colony,
                                                         GoodsType type,
                                                         int amount,
                                                         Constants.IndianDemandAction accept)
        Decide whether to accept an Indian demand, or not. Or for native players, return the result of the demand.
        Parameters:
        unit - The Unit making demands.
        colony - The Colony where demands are being made.
        type - The GoodsType demanded.
        amount - The amount of gold demanded.
        accept - The acceptance state of the demand.
        Returns:
        The result of the demand.
      • acceptDiplomaticTrade

        public DiplomaticTrade.TradeStatus acceptDiplomaticTrade​(DiplomaticTrade agreement)
        Resolves a diplomatic trade offer.
        Parameters:
        agreement - The proposed DiplomaticTrade.
        Returns:
        The TradeStatus to apply to the agreement.
      • acceptTax

        public boolean acceptTax​(int tax)
        Decides to accept a tax raise or not. Overridden by the European player.
        Parameters:
        tax - The tax raise.
        Returns:
        True if the raise is accepted.
      • acceptMercenaries

        public boolean acceptMercenaries()
        Decides to accept an offer of mercenaries or not. Overridden by the European player.
        Returns:
        True if the mercenaries are accepted.
      • selectFoundingFather

        public FoundingFather selectFoundingFather​(java.util.List<FoundingFather> ffs)
        Selects the most useful founding father offered. Overridden by EuropeanAIPlayers.
        Parameters:
        ffs - The founding fathers on offer.
        Returns:
        The founding father selected.
      • getNeededWagons

        public abstract int getNeededWagons​(Tile tile)
        Gets the needed wagons for a tile/contiguity.
        Parameters:
        tile - The Tile to derive the contiguity from.
        Returns:
        The number of wagons needed.
      • pioneersNeeded

        public abstract int pioneersNeeded()
        How many pioneers should we have? This is the desired total number, not the actual number which would take into account the number of existing PioneeringMissions.
        Returns:
        The desired number of pioneers for this player.
      • scoutsNeeded

        public abstract int scoutsNeeded()
        How many scouts should we have? This is the desired total number, not the actual number which would take into account the number of existing ScoutingMissions. Current scheme for European AIs is to use up to three scouts in the early part of the game, then one.
        Returns:
        The desired number of scouts for this player.
      • completeWish

        public abstract void completeWish​(Wish w)
        Notify that a wish has been completed. Called from AIColony.
        Parameters:
        w - The Wish to complete.
      • 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.
      • getXMLTagName

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