Class EuropeanAIPlayer

    • Field Detail

      • logger

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

        private static final java.util.function.Predicate<Unit> equipPred
        Predicate to select units that can be equipped.
      • partyPred

        private static final java.util.function.Predicate<Modifier> partyPred
        Predicate to select party modifiers.
      • buildingRange

        private static final int buildingRange
        Maximum number of turns to travel to a building site.
        See Also:
        Constant Field Values
      • cashInRange

        private static final int cashInRange
        Maximum number of turns to travel to a cash in location.
        See Also:
        Constant Field Values
      • missionaryRange

        private static final int missionaryRange
        Maximum number of turns to travel to a missionary target.
        See Also:
        Constant Field Values
      • pioneeringRange

        private static final int pioneeringRange
        Maximum number of turns to travel to make progress on pioneering. This is low-ish because it is usually more efficient to ship the tools where they are needed and either create a new pioneer on site or send a hardy pioneer on horseback. The AI is probably smart enough to do the former already, and one day the latter.
        See Also:
        Constant Field Values
      • privateerRange

        private static final int privateerRange
        Maximum number of turns to travel to a privateering target. Low number because of large naval moves.
        See Also:
        Constant Field Values
      • scoutingRange

        private static final int scoutingRange
        Maximum number of turns to travel to a scouting target.
        See Also:
        Constant Field Values
      • builderComparator

        private static final java.util.Comparator<AIUnit> builderComparator
        A comparator to sort units by decreasing builder score.
      • pioneerComparator

        public static final java.util.Comparator<AIUnit> pioneerComparator
        A comparator to sort units by suitability for a PioneeringMission. We do not check if a unit is near to a colony that can provide tools, as that is likely to be too expensive. FIXME: perhaps we should.
      • scoutComparator

        public static final java.util.Comparator<AIUnit> scoutComparator
        A comparator to sort units by suitability for a ScoutingMission. We do not check if a unit is near to a colony that can provide horses, as that is likely to be too expensive. FIXME: perhaps we should.
      • liftBoycottCheatPercent

        private static int liftBoycottCheatPercent
        Cheat chances.
      • equipScoutCheatPercent

        private static int equipScoutCheatPercent
      • equipPioneerCheatPercent

        private static int equipPioneerCheatPercent
      • landUnitCheatPercent

        private static int landUnitCheatPercent
      • offensiveLandUnitCheatPercent

        private static int offensiveLandUnitCheatPercent
      • offensiveNavalUnitCheatPercent

        private static int offensiveNavalUnitCheatPercent
      • transportNavalUnitCheatPercent

        private static int transportNavalUnitCheatPercent
      • pioneerRole

        private static Role pioneerRole
        The pioneer role.
      • scoutRole

        private static Role scoutRole
        The scouting role.
      • tipMap

        private final java.util.Map<Tile,​TileImprovementPlan> tipMap
        A cached map of Tile to best TileImprovementPlan. Used to choose a tile improvement for a pioneer to work on.
      • transportDemand

        private final java.util.Map<Location,​java.util.List<Wish>> transportDemand
        A cached map of destination Location to Wishes awaiting transport.
      • transportSupply

        private final java.util.List<TransportableAIObject> transportSupply
        A cached list of transportables awaiting transport.
      • goodsWishes

        private final java.util.Map<GoodsType,​java.util.List<GoodsWish>> goodsWishes
        A mapping of goods type to the goods wishes where a colony has requested that goods type. Used to retarget goods that have gone astray.
      • workerWishes

        private final java.util.Map<UnitType,​java.util.List<WorkerWish>> workerWishes
        A mapping of unit type to the worker wishes for that type. Used to allocate WishRealizationMissions for units.
      • wagonsNeeded

        private final java.util.Map<java.lang.Integer,​java.lang.Integer> wagonsNeeded
        A mapping of contiguity number to number of wagons needed in that landmass.
      • badlyDefended

        private final java.util.List<AIColony> badlyDefended
        The colonies that start the turn badly defended.
      • nBuilders

        private int nBuilders
        Current estimate of the number of new BuildColonyMissions to create.
      • nPioneers

        private int nPioneers
        Current estimate of the number of new PioneeringMissions to create.
      • nScouts

        private int nScouts
        Current estimate of the number of new ScoutingMissions to create.
      • nNavalCarrier

        private int nNavalCarrier
        Count of the number of transports needing a naval unit.
    • Constructor Detail

      • EuropeanAIPlayer

        public EuropeanAIPlayer​(AIMain aiMain,
                                Player player)
        Creates a new EuropeanAIPlayer.
        Parameters:
        aiMain - The main AI-class.
        player - The player that should be associated with this AIPlayer.
      • EuropeanAIPlayer

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

      • removeAIObject

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

        private void removeAIColony​(AIColony aic)
        Remove one of our colonies.
        Parameters:
        aic - The AIColony to remove.
      • initializeFromSpecification

        private static void initializeFromSpecification​(Specification spec)
        Initialize the static fields that would be final but for needing the specification.
        Parameters:
        spec - The Specification to initialize from.
      • getBadlyDefended

        protected java.util.List<AIColony> getBadlyDefended()
        Get the list of badly defended colonies.
        Returns:
        A list of AIColonys that were badly defended at the start of this turn.
      • initializeMissions

        private void initializeMissions​(LogBuilder lb)
        Simple initialization of AI missions given that we know the starting conditions.
        Parameters:
        lb - A LogBuilder to log to.
      • cheatGold

        public void cheatGold​(int amount,
                              LogBuilder lb)
        Cheat by adding gold to guarantee the player has a minimum amount.
        Parameters:
        amount - The minimum amount of gold required.
        lb - A LogBuilder to log to.
      • cheat

        private void cheat​(LogBuilder lb)
        Cheats for the AI. Please try to centralize cheats here. FIXME: Remove when the AI is good enough.
        Parameters:
        lb - A LogBuilder to log to.
      • cheatUnit

        private AIUnit cheatUnit​(java.util.List<RandomChoice<UnitType>> rc,
                                 java.lang.String what,
                                 LogBuilder lb)
        Cheat-build a unit in Europe.
        Parameters:
        rc - A list of random choices to choose from.
        what - A description of the unit.
        lb - A LogBuilder to log to.
        Returns:
        The AIUnit built.
      • cheatUnit

        private AIUnit cheatUnit​(UnitType unitType,
                                 java.lang.String what,
                                 LogBuilder lb)
        Cheat-build a unit in Europe.
        Parameters:
        unitType - The UnitType to build.
        what - A description of the unit.
        lb - A LogBuilder to log to.
        Returns:
        The AIUnit built.
      • allocateTransportables

        public void allocateTransportables​(java.util.List<TransportableAIObject> transportables,
                                           java.util.List<TransportMission> missions,
                                           LogBuilder lb)
        Assign transportable units and goods to available carriers. These supply driven assignments supplement the demand driven calls inside TransportMission.
        Parameters:
        transportables - A list of TransportableAIObjects to allocated transport for.
        missions - A list of TransportMissions to potentially assign more transportables to.
        lb - A LogBuilder to log to.
      • bringGifts

        private void bringGifts​(LogBuilder lb)
        Brings gifts to nice players with nearby colonies. FIXME: European players can also bring gifts! However, this might be folded into a trade mission, since European gifts are just a special case of trading.
        Parameters:
        lb - A LogBuilder to log to.
      • demandTribute

        private void demandTribute​(LogBuilder lb)
        Demands goods from players with nearby colonies. FIXME: European players can also demand tribute!
        Parameters:
        lb - A LogBuilder to log to.
      • buildTipMap

        public void buildTipMap​(LogBuilder lb)
        Rebuilds a map of locations to TileImprovementPlans. Called by startWorking at the start of every turn. Public for the test suite.
        Parameters:
        lb - A LogBuilder to log to.
      • updateTipMap

        private void updateTipMap​(AIColony aic)
        Update the tip map with tips from a new colony.
        Parameters:
        aic - The new AIColony.
      • getBestPlan

        public TileImprovementPlan getBestPlan​(Tile tile)
        Gets the best plan for a tile from the tipMap.
        Parameters:
        tile - The Tile to lookup.
        Returns:
        The best plan for a tile.
      • getBestPlanTile

        public Tile getBestPlanTile​(Colony colony)
        Gets the best plan for a colony from the tipMap.
        Parameters:
        colony - The Colony to check.
        Returns:
        The tile with the best plan for a colony, or null if none found.
      • removeTileImprovementPlan

        public void removeTileImprovementPlan​(TileImprovementPlan plan)
        Remove a TileImprovementPlan from the relevant colony.
        Parameters:
        plan - The TileImprovementPlan to remove.
      • updateTransport

        public void updateTransport​(AIUnit aiu,
                                    Location oldTarget,
                                    LogBuilder lb)
        Update the transport of a unit following a target change. If the target has changed - drop all non-boarded transport unless the target is the same - dump boarded transport with no target - requeue all boarded transport unless the target is the same
        Parameters:
        aiu - The AIUnit to check.
        oldTarget - The old target Location.
        lb - A LogBuilder to log to.
      • requestsTransport

        private boolean requestsTransport​(TransportableAIObject t)
        Checks if a transportable needs transport.
        Parameters:
        t - The TransportableAIObject to check.
        Returns:
        True if no transport is already present or the transportable is already aboard a carrier, and there is a well defined source and destination location.
      • checkTransport

        private boolean checkTransport​(TransportableAIObject t)
        Checks that the carrier assigned to a transportable is has a transport mission and the transport is queued thereon.
        Parameters:
        t - The TransportableAIObject to check.
        Returns:
        True if all is well.
      • changeNeedWagon

        private void changeNeedWagon​(Tile tile,
                                     int amount)
        Changes the needed wagons map for a specified tile/contiguity. If the change is zero, that is a special flag that a connected port is available, and thus that the map should be initialized for that contiguity.
        Parameters:
        tile - The Tile to derive the contiguity from.
        amount - The change to make.
      • buildTransportMaps

        private void buildTransportMaps​(LogBuilder lb)
        Rebuild the transport maps. Count the number of transports requiring naval/land carriers.
        Parameters:
        lb - A LogBuilder to log to.
      • getUrgentTransportables

        public java.util.List<TransportableAIObject> getUrgentTransportables()
        Gets the most urgent transportables.
        Returns:
        The most urgent 10% of the available transportables.
      • claimTransportable

        public boolean claimTransportable​(TransportableAIObject t)
        Allows a TransportMission to signal that it has taken responsibility for a TransportableAIObject.
        Parameters:
        t - The TransportableAIObject being claimed.
        Returns:
        True if the transportable was claimed from the supply map.
      • rearrangeColonies

        private void rearrangeColonies​(LogBuilder lb)
        Rearrange colonies.
        Parameters:
        lb - A LogBuilder to log to.
      • suppressEuropeanTrade

        private void suppressEuropeanTrade​(GoodsType type,
                                           LogBuilder lb)
        Suppress European trade in a goods type. A goods party and boycott is incoming.
        Parameters:
        type - The GoodsType to suppress.
        lb - A LogBuilder to log to.
      • getWorkerWishesAt

        public java.util.List<WorkerWish> getWorkerWishesAt​(Location loc,
                                                            UnitType type)
        Gets a list of the wishes at a given location for a unit type.
        Parameters:
        loc - The Location to look for wishes at.
        type - The UnitType to look for.
        Returns:
        A list of WorkerWishes.
      • getGoodsWishesAt

        public java.util.List<GoodsWish> getGoodsWishesAt​(Location loc,
                                                          GoodsType type)
        Gets a list of the wishes at a given location for a goods type.
        Parameters:
        loc - The Location to look for wishes at.
        type - The GoodsType to look for.
        Returns:
        A list of GoodsWishes.
      • getBestWorkerWish

        private WorkerWish getBestWorkerWish​(AIUnit aiUnit,
                                             UnitType unitType)
        Gets the best worker wish for a carrier unit.
        Parameters:
        aiUnit - The carrier AIUnit.
        unitType - The UnitType to find a wish for.
        Returns:
        The best worker wish for the unit.
      • getBestGoodsWish

        public GoodsWish getBestGoodsWish​(AIUnit aiUnit,
                                          GoodsType goodsType)
        Gets the best goods wish for a carrier unit.
        Parameters:
        aiUnit - The carrier AIUnit.
        goodsType - The GoodsType to wish for.
        Returns:
        The best GoodsWish for the unit.
      • buildWishMaps

        private void buildWishMaps​(LogBuilder lb)
        Rebuilds the goods and worker wishes maps.
        Parameters:
        lb - A LogBuilder to log to.
      • consumeWorkerWish

        private void consumeWorkerWish​(AIUnit aiUnit,
                                       WorkerWish ww)
        Consume a WorkerWish, yielding a WishRealizationMission for a unit.
        Parameters:
        aiUnit - The AIUnit to check.
        ww - The WorkerWish to consume.
      • consumeGoodsWish

        private void consumeGoodsWish​(AIGoods aig,
                                      GoodsWish gw)
        Consume a GoodsWish.
        Parameters:
        aig - The AIGoods to use.
        gw - The GoodsWish to consume.
      • buildersNeeded

        private int buildersNeeded()
        Gets the number of units that should build a colony. This is the desired total number, not the actual number which would take into account the number of existing BuildColonyMissions.
        Returns:
        The desired number of colony builders for this player.
      • recruitAIUnitInEurope

        private AIUnit recruitAIUnitInEurope​(int slot)
        Asks the server to recruit a unit in Europe on behalf of the AIPlayer. FIXME: Move this to a specialized Handler class (AIEurope?) FIXME: Give protected access?
        Parameters:
        slot - The migration slot to recruit from.
        Returns:
        The new AIUnit created by this action or null on failure.
      • trainAIUnitInEurope

        private AIUnit trainAIUnitInEurope​(UnitType unitType)
        Helper function for server communication - Ask the server to train a unit in Europe on behalf of the AIGetPlayer(). FIXME: Move this to a specialized Handler class (AIEurope?) FIXME: Give protected access?
        Parameters:
        unitType - The UnitType to train.
        Returns:
        the new AIUnit created by this action. May be null.
      • getWishes

        public java.util.List<Wish> getWishes()
        Gets the wishes for all this player's colonies, sorted by the value.
        Returns:
        A list of wishes.
      • determineStances

        private void determineStances​(LogBuilder lb)
        Determines the stances towards each player.
        Parameters:
        lb - A LogBuilder to log to.
      • peaceHolds

        private boolean peaceHolds​(Player p)
        See if a recent peace treaty still has force.
        Parameters:
        p - The Player to check for a peace treaty with.
        Returns:
        True if peace gets another chance.
      • getNationSummary

        protected NationSummary getNationSummary​(Player other)
        Get a nation summary for another player.
        Parameters:
        other - The other Player to get the summary for.
        Returns:
        The current NationSummary for a player.
      • getStrengthRatio

        protected double getStrengthRatio​(Player other)
        Get the land force strength ratio of this player with respect to another.
        Parameters:
        other - The other Player.
        Returns:
        The strength ratio (strength/sum(strengths)).
      • getNavalStrengthRatio

        protected double getNavalStrengthRatio()
        Is this player lagging in naval strength? Calculate the ratio of its naval strength to the average strength of other European colonial powers.
        Returns:
        The naval strength ratio, or negative if there are no other European colonial nations.
      • rejectAgreement

        private DiplomaticTrade.TradeStatus rejectAgreement​(TradeItem stance,
                                                            DiplomaticTrade agreement)
        Reject a trade agreement, except if a Franklin-derived stance is supplied.
        Parameters:
        stance - A stance TradeItem.
        agreement - The DiplomaticTrade to reset.
        Returns:
        The TradeStatus for the agreement.
      • giveNormalMissions

        protected void giveNormalMissions​(LogBuilder lb)
        Ensures all units have a mission.
        Parameters:
        lb - A LogBuilder to log to.
      • getSimpleMission

        private Mission getSimpleMission​(AIUnit aiUnit)
        Choose a mission for an AIUnit.
        Parameters:
        aiUnit - The AIUnit to choose for.
        Returns:
        A suitable Mission, or null if none found.
      • getBuildColonyMission

        private Mission getBuildColonyMission​(AIUnit aiUnit,
                                              Location target)
        Gets a new BuildColonyMission for a unit.
        Parameters:
        aiUnit - The AIUnit to check.
        target - An optional target Location.
        Returns:
        A new mission, or null if impossible.
      • getCashInTreasureTrainMission

        private Mission getCashInTreasureTrainMission​(AIUnit aiUnit)
        Gets a new CashInTreasureTrainMission for a unit.
        Parameters:
        aiUnit - The AIUnit to check.
        Returns:
        A new mission, or null if impossible.
      • getDefendSettlementMission

        private Mission getDefendSettlementMission​(AIUnit aiUnit,
                                                   boolean relaxed)
        Gets a new DefendSettlementMission for a unit.
        Parameters:
        aiUnit - The AIUnit to check.
        relaxed - Use a relaxed cost decider to choose the target.
        Returns:
        A new mission, or null if impossible.
      • getMissionaryMission

        public Mission getMissionaryMission​(AIUnit aiUnit)
        Gets a new MissionaryMission for a unit.
        Parameters:
        aiUnit - The AIUnit to check.
        Returns:
        A new mission, or null if impossible.
      • getPioneeringMission

        public Mission getPioneeringMission​(AIUnit aiUnit,
                                            Location target)
        Gets a new PioneeringMission for a unit. FIXME: pioneers to make roads between colonies
        Parameters:
        aiUnit - The AIUnit to check.
        target - An optional target Location.
        Returns:
        A new mission, or null if impossible.
      • getPrivateerMission

        public Mission getPrivateerMission​(AIUnit aiUnit,
                                           Location target)
        Gets a new PrivateerMission for a unit.
        Parameters:
        aiUnit - The AIUnit to check.
        target - An optional target Location.
        Returns:
        A new mission, or null if impossible.
      • getScoutingMission

        public Mission getScoutingMission​(AIUnit aiUnit)
        Gets a new ScoutingMission for a unit.
        Parameters:
        aiUnit - The AIUnit to check.
        Returns:
        A new mission, or null if impossible.
      • getTransportMission

        public Mission getTransportMission​(AIUnit aiUnit)
        Gets a new TransportMission for a unit.
        Parameters:
        aiUnit - The AIUnit to check.
        Returns:
        A new mission, or null if impossible.
      • getWishRealizationMission

        private Mission getWishRealizationMission​(AIUnit aiUnit,
                                                  WorkerWish wish)
        Gets a new WishRealizationMission for a unit.
        Parameters:
        aiUnit - The AIUnit to check.
        wish - An optional WorkerWish to realize.
        Returns:
        A new mission, or null if impossible.
      • getWorkInsideColonyMission

        public Mission getWorkInsideColonyMission​(AIUnit aiUnit,
                                                  AIColony aiColony)
        Gets a WorkInsideColonyMission for a unit.
        Parameters:
        aiUnit - The AIUnit to check.
        aiColony - An optional AIColony to work at.
        Returns:
        A new mission, or null if impossible.
      • determineStance

        protected Stance determineStance​(Player other)
        Standard stance change determination.
        Overrides:
        determineStance in class AIPlayer
        Parameters:
        other - The Player wrt consider stance.
        Returns:
        The new Stance.
      • startWorking

        public void startWorking()
        Tells this AIPlayer to make decisions. The AIPlayer is done doing work this turn when this method returns.
        Specified by:
        startWorking in class AIPlayer
      • doMissions

        protected java.util.List<AIUnit> doMissions​(java.util.List<AIUnit> aiUnits,
                                                    LogBuilder lb)
        Makes every unit perform their mission.
        Overrides:
        doMissions in class MissionAIPlayer
        Parameters:
        aiUnits - A list of AIUnits to perform missions.
        lb - A LogBuilder to log to.
        Returns:
        A list of AIUnits that have moves left.
      • adjustMission

        public int adjustMission​(AIUnit aiUnit,
                                 PathNode path,
                                 java.lang.Class type,
                                 int value)
        Adjusts the score of this proposed mission for this player type. Subclasses should override and refine this.
        Specified by:
        adjustMission in class MissionAIPlayer
        Parameters:
        aiUnit - The AIUnit to perform the mission.
        path - A PathNode to the target of this mission.
        type - The mission type.
        value - The proposed value.
        Returns:
        A score representing the desirability of this mission.
      • indianDemand

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

        public boolean acceptTax​(int tax)
        Decides to accept a tax raise or not. Overridden by the European player.
        Overrides:
        acceptTax in class AIPlayer
        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.
        Overrides:
        acceptMercenaries in class AIPlayer
        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.
        Overrides:
        selectFoundingFather in class AIPlayer
        Parameters:
        ffs - The founding fathers on offer.
        Returns:
        The founding father selected.
      • getNeededWagons

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

        public 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.
        Specified by:
        pioneersNeeded in class AIPlayer
        Returns:
        The desired number of pioneers for this player.
      • scoutsNeeded

        public 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.
        Specified by:
        scoutsNeeded in class AIPlayer
        Returns:
        The desired number of scouts for this player.
      • completeWish

        public void completeWish​(Wish w)
        Notify that a wish has been completed. Called from AIColony.
        Specified by:
        completeWish in class AIPlayer
        Parameters:
        w - The Wish to complete.