From: "Enrico Weigelt, metux IT consult" <enrico.weig...@gr13.net>
--- .../server/ai/mission/BuildColonyMission.java | 36 +++++++++++++++++----- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/net/sf/freecol/server/ai/mission/BuildColonyMission.java b/src/net/sf/freecol/server/ai/mission/BuildColonyMission.java index 761fe2abcf8..f5b28363607 100644 --- a/src/net/sf/freecol/server/ai/mission/BuildColonyMission.java +++ b/src/net/sf/freecol/server/ai/mission/BuildColonyMission.java @@ -20,9 +20,10 @@ package net.sf.freecol.server.ai.mission; import java.util.Comparator; -import java.util.function.Predicate; +import java.util.HashMap; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.Map; import javax.xml.stream.XMLStreamException; @@ -351,6 +352,31 @@ public class BuildColonyMission extends Mission { return invalidReason(getAIUnit(), target); } + private static Colony findNearestSmallerColony(Unit unit, Colony colony, Player player) { + // Go to the nearest smaller colony? + Colony best = null; + int best_turns = Integer.MAX_VALUE; + Map<Colony,Integer> turnsCache = new HashMap<Colony,Integer>(); + for (Colony c : player.getColonies()) { + if (!(c != colony && c.getUnitCount() < colony.getUnitCount())) + continue; + + int turns; + if (turnsCache.containsKey(c)) + turns = turnsCache.get(c); + else { + turns = unit.getTurnsToReach(c); + turnsCache.put(c, turns); + } + + if ((best == null) || (turns < best_turns)) { + best_turns = turns; + best = c; + } + } + return best; + } + /** * {@inheritDoc} */ @@ -440,12 +466,8 @@ public class BuildColonyMission extends Mission { } // Go to the nearest smaller colony? - final Predicate<Colony> smallPred = c -> - c != colony && c.getUnitCount() < colony.getUnitCount(); - final Comparator<Colony> closeComp = cachingIntComparator(c -> - unit.getTurnsToReach(c)); - Colony best = minimize(player.getColonies(), smallPred, - closeComp); + Colony best = findNearestSmallerColony(unit, colony, player); + if (best != null) { lb.add(", going to smaller ", best.getUnitCount(), "<", colony.getUnitCount(), " colony"); -- 2.11.0.rc0.7.gbe5a750 ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, SlashDot.org! http://sdm.link/slashdot _______________________________________________ Freecol-developers mailing list Freecol-developers@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/freecol-developers