From: "Enrico Weigelt, metux IT consult" <enrico.weig...@gr13.net>
--- .../client/gui/panel/report/LabourData.java | 2 +- .../report/ReportContinentalCongressPanel.java | 6 +- src/net/sf/freecol/common/debug/DebugUtils.java | 4 +- .../sf/freecol/common/model/DiplomaticTrade.java | 10 +- src/net/sf/freecol/common/model/Game.java | 18 +- src/net/sf/freecol/common/model/HighScore.java | 2 +- src/net/sf/freecol/common/model/Player.java | 201 ++++++++++++++------- src/net/sf/freecol/server/ai/EuropeanAIPlayer.java | 2 +- src/net/sf/freecol/server/model/ServerGame.java | 2 +- src/net/sf/freecol/server/model/ServerPlayer.java | 19 +- 10 files changed, 169 insertions(+), 97 deletions(-) diff --git a/src/net/sf/freecol/client/gui/panel/report/LabourData.java b/src/net/sf/freecol/client/gui/panel/report/LabourData.java index d8f7e215863..c903d0b681f 100644 --- a/src/net/sf/freecol/client/gui/panel/report/LabourData.java +++ b/src/net/sf/freecol/client/gui/panel/report/LabourData.java @@ -430,7 +430,7 @@ public class LabourData { GoodsType expertProduction = unitData.getUnitType() .getExpertProduction(); if (expertProduction != null) { - for (Colony c : player.getColonyList()) { + for (Colony c : player.getColonies()) { int net = c.getNetProductionOf(expertProduction); if (unitData.details.containsKey(c)) { unitData.getLocationData(c).netProduction = net; diff --git a/src/net/sf/freecol/client/gui/panel/report/ReportContinentalCongressPanel.java b/src/net/sf/freecol/client/gui/panel/report/ReportContinentalCongressPanel.java index 5bfbde184bd..0c0eb18d9cd 100644 --- a/src/net/sf/freecol/client/gui/panel/report/ReportContinentalCongressPanel.java +++ b/src/net/sf/freecol/client/gui/panel/report/ReportContinentalCongressPanel.java @@ -37,6 +37,7 @@ import net.sf.freecol.client.gui.ImageLibrary; import net.sf.freecol.client.gui.panel.*; import net.sf.freecol.common.debug.FreeColDebugger; import net.sf.freecol.common.i18n.Messages; +import net.sf.freecol.common.model.Colony; import net.sf.freecol.common.model.FoundingFather; import net.sf.freecol.common.model.FoundingFather.FoundingFatherType; import net.sf.freecol.common.model.GoodsType; @@ -85,8 +86,9 @@ public final class ReportContinentalCongressPanel extends ReportPanel { Messages.getDescription(currentFather)); recruitingPanel.add(currentFatherLabel); for (GoodsType gt : getSpecification().getLibertyGoodsTypeList()) { - int total = sum(player.getColonies(), - c -> c.getNetProductionOf(gt)); + int total = 0; + for (Colony c : player.getColonies()) + total += c.getNetProductionOf(gt); FreeColProgressBar progressBar = new FreeColProgressBar(gt, 0, player.getTotalFoundingFatherCost(), player.getLiberty(), total); diff --git a/src/net/sf/freecol/common/debug/DebugUtils.java b/src/net/sf/freecol/common/debug/DebugUtils.java index 13ddb27a316..c6edfadbd11 100644 --- a/src/net/sf/freecol/common/debug/DebugUtils.java +++ b/src/net/sf/freecol/common/debug/DebugUtils.java @@ -140,7 +140,7 @@ public class DebugUtils { Player.class); List<String> results = new ArrayList<>(); int fails = 0; - for (Colony sColony : sPlayer.getColonyList()) { + for (Colony sColony : sPlayer.getColonies()) { Colony.NoBuildReason reason = sColony.getNoBuildReason(sBuildingType, null); results.add(sColony.getName() + ": " + reason); @@ -293,7 +293,7 @@ public class DebugUtils { } catch (NumberFormatException x) { return; } - for (Colony c : player.getColonyList()) { + for (Colony c : player.getColonies()) { c.addLiberty(liberty); sGame.getFreeColGameObject(c.getId(), Colony.class) .addLiberty(liberty); diff --git a/src/net/sf/freecol/common/model/DiplomaticTrade.java b/src/net/sf/freecol/common/model/DiplomaticTrade.java index a7f636db2d6..ffb78d5805c 100644 --- a/src/net/sf/freecol/common/model/DiplomaticTrade.java +++ b/src/net/sf/freecol/common/model/DiplomaticTrade.java @@ -334,10 +334,12 @@ public class DiplomaticTrade extends FreeColGameObject { * @return A list of {@code Colony}s offered in this trade. */ public List<Colony> getColoniesGivenBy(final Player player) { - return transform(this.items, - ti -> ti instanceof ColonyTradeItem - && ti.getSource() == player, - ti -> ti.getColony(player.getGame())); + final Game game = player.getGame(); + List<Colony> result = new ArrayList<>(); + for (TradeItem ti : this.items) + if (ti instanceof ColonyTradeItem && ti.getSource() == player) + result.add(ti.getColony(game)); + return result; } /** diff --git a/src/net/sf/freecol/common/model/Game.java b/src/net/sf/freecol/common/model/Game.java index 3d4a14b5d6f..4c1c1004beb 100644 --- a/src/net/sf/freecol/common/model/Game.java +++ b/src/net/sf/freecol/common/model/Game.java @@ -33,7 +33,6 @@ import java.util.NoSuchElementException; import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; -import java.util.stream.Stream; import javax.xml.stream.XMLStreamException; @@ -1053,20 +1052,13 @@ public class Game extends FreeColGameObject { * Get all the colonies in the game. * * @param player An optional {@code Player} to omit. - * @return A stream of all the {@code Colony}s in the game. - */ - public Stream<Colony> getAllColonies(Player player) { - return flatten(getLiveEuropeanPlayers(player), Player::getColonies); - } - - /** - * Get a list of all the colonies in the game. - * - * @param player An optional {@code Player} to omit. * @return A list of all the {@code Colony}s in the game. */ - public List<Colony> getAllColoniesList(Player player) { - return toList(getAllColonies(player)); + public List<Colony> getAllColonies(Player player) { + List<Colony> result = new ArrayList<>(); + for (Player p : getLiveEuropeanPlayers(player)) + result.addAll(p.getColonies()); + return result; } /** diff --git a/src/net/sf/freecol/common/model/HighScore.java b/src/net/sf/freecol/common/model/HighScore.java index 8d309bd7e79..09e51f328e7 100644 --- a/src/net/sf/freecol/common/model/HighScore.java +++ b/src/net/sf/freecol/common/model/HighScore.java @@ -164,7 +164,7 @@ public class HighScore extends FreeColObject { this.playerName = player.getName(); this.nationId = player.getNationId(); this.nationTypeId = player.getNationType().getId(); - this.nColonies = count(player.getColonies()); + this.nColonies = player.getSettlementCount(); this.nUnits = player.getUnitCount(); this.independenceTurn = (player.getPlayerType() == Player.PlayerType.INDEPENDENT) ? game.getTurn().getNumber() diff --git a/src/net/sf/freecol/common/model/Player.java b/src/net/sf/freecol/common/model/Player.java index 2ea54dd6ad9..f0354687958 100644 --- a/src/net/sf/freecol/common/model/Player.java +++ b/src/net/sf/freecol/common/model/Player.java @@ -1270,13 +1270,19 @@ public class Player extends FreeColGameObject implements Nameable { * * @return The total immigration production. */ - public int getTotalImmigrationProduction() { + public final int getTotalImmigrationProduction() { if (!isColonial()) return 0; final List<GoodsType> immigrationGoodsTypes = getSpecification() .getImmigrationGoodsTypeList(); - int production = sum(getColonies(), - c -> sum(immigrationGoodsTypes, gt -> c.getTotalProductionOf(gt))); + + int production = 0; + synchronized (this.settlements) { + for (Settlement s : this.settlements) + for (GoodsType gt : immigrationGoodsTypes) + production += s.getTotalProductionOf(gt); + } + final Europe europe = getEurope(); if (europe != null) production += europe.getImmigration(production); return production; @@ -1351,30 +1357,23 @@ public class Player extends FreeColGameObject implements Nameable { } /** - * Gets how much liberty will be produced next turn if no colonies - * are lost and nothing unexpected happens. - * - * @return The total amount of liberty this {@code Player}'s - * {@code Colony}s will make next turn. - */ - public int getLibertyProductionNextTurn() { - final Specification spec = getSpecification(); - final List<GoodsType> goodsTypes = spec.getLibertyGoodsTypeList(); - int nextTurn = sum(getColonies(), c -> - sum(goodsTypes, gt -> c.getTotalProductionOf(gt))); - return (int)applyModifiers((float)nextTurn, getGame().getTurn(), - Modifier.LIBERTY); - } - - /** * Gets the total percentage of rebels in all this player's colonies. * * @return The total percentage of rebels in all this player's colonies. */ - public int getSoL() { - final List<Colony> colonies = getColonyList(); - return (colonies.isEmpty()) ? 0 - : sum(colonies, Colony::getSoL) / colonies.size(); + public final int getSoL() { + synchronized (this.settlements) { + int total = 0; + int count = 0; + + for (Settlement s : this.settlements) + if (s instanceof Colony) { + total += ((Colony)s).getSoL(); + count++; + } + + return (count == 0 ? 0 : total/count); + } } /** @@ -1412,10 +1411,14 @@ public class Player extends FreeColGameObject implements Nameable { * * @param father The {@code FoundingFather} to add. */ - public void addFather(FoundingFather father) { + public final void addFather(FoundingFather father) { foundingFathers.add(father); addFeatures(father); - for (Colony c : getColonyList()) c.invalidateCache(); + synchronized (this.settlements) { + for (Settlement s : this.settlements) + if (s instanceof Colony) + ((Colony)s).invalidateCache(); + } } /** @@ -1612,11 +1615,14 @@ public class Player extends FreeColGameObject implements Nameable { * * @param amount The new tax amount. */ - public void setTax(int amount) { + public final void setTax(int amount) { tax = amount; - if (recalculateBellsBonus()) { - for (Colony c : getColonyList()) c.invalidateCache(); - } + if (recalculateBellsBonus()) + synchronized (this.settlements) { + for (Settlement s : this.settlements) + if (s instanceof Colony) + ((Colony)s).invalidateCache(); + } } /** @@ -2291,9 +2297,16 @@ public class Player extends FreeColGameObject implements Nameable { * * @return The number of port settlements this player has. */ - public int getNumberOfPorts() { - return (!isEuropean()) ? 0 - : count(getColonies(), Colony::isConnectedPort); + public final int getNumberOfPorts() { + if (!isEuropean()) + return 0; + + synchronized (this.settlements) { + int x = 0; + for (Settlement s : this.settlements) + if (s.isConnectedPort()) x++; + return x; + } } /** @@ -2301,10 +2314,18 @@ public class Player extends FreeColGameObject implements Nameable { * * @return A list of port {@code Colony}s. */ - public List<Colony> getPorts() { - return (!isEuropean()) - ? Collections.<Colony>emptyList() - : transform(getColonies(), Colony::isConnectedPort); + public final List<Colony> getPorts() { + if (!isEuropean()) + return Collections.<Colony>emptyList(); + + synchronized (this.settlements) { + List<Colony> result = new ArrayList<>(); + for (Settlement s : this.settlements) + if (s instanceof Colony && s.isConnectedPort()) + result.add((Colony)s); + + return result; + } } /** @@ -2356,8 +2377,29 @@ public class Player extends FreeColGameObject implements Nameable { * * @return The sum of the units currently working in the colonies. */ - public int getColoniesPopulation() { - return sum(getColonies(), Colony::getUnitCount); + public final int getColoniesPopulation() { + synchronized (this.settlements) { + int x = 0; + for (Settlement s : this.settlements) + if (s instanceof Colony) + x += ((Colony)s).getUnitCount(); + return x; + } + } + + /** + * Gets the sum of liberty produced by the player's coloniess. + * + * @return The sum of of liberty produced by the player's colonies.. + */ + public final int getColoniesLiberty() { + synchronized (this.settlements) { + int x = 0; + for (Settlement s : this.settlements) + if (s instanceof Colony) + x += ((Colony)s).getLiberty(); + return x; + } } /** @@ -2367,8 +2409,13 @@ public class Player extends FreeColGameObject implements Nameable { * @return The {@code Colony} with the given name, or null if * not found. */ - public Colony getColonyByName(String name) { - return find(getColonies(), matchKeyEquals(name, Colony::getName)); + public final Colony getColonyByName(String name) { + synchronized (this.settlements) { + for (Settlement s : this.settlements) + if (s instanceof Colony && Utils.equals(name, ((Colony)s).getName())) + return (Colony)s; + } + return null; } /** @@ -2384,22 +2431,48 @@ public class Player extends FreeColGameObject implements Nameable { } /** - * Get a stream of all colonies this player owns. + * Get a fresh list of all colonies this player owns. * - * @return A stream of the {@code Colony}s this player owns. + * @return A list of the {@code Colony}s this player owns. */ - public Stream<Colony> getColonies() { - return getColonyList().stream(); + public final List<Colony> getColonies() { + synchronized (this.settlements) { + List<Colony> result = new ArrayList<>(); + for (Settlement s : this.settlements) + if (s instanceof Colony) + result.add((Colony)s); + return result; + } } /** - * Get a fresh list of all colonies this player owns. + * Get the first colony. * - * @return A list of the {@code Colony}s this player owns. + * @return The first {@code Colony} or null. */ - public List<Colony> getColonyList() { - return transform(getSettlements(), s -> s instanceof Colony, - s -> (Colony)s); + public final Colony getFirstColony() { + synchronized (this.settlements) { + for (Settlement s : this.settlements) + if (s instanceof Colony) + return (Colony)s; + } + return null; + } + + /** + * Get a list of all colonies this player owns, which can + * bombard enemy ships. + * + * @return A list of the {@code Colony}s then can bombard. + */ + public final List<Colony> getColoniesCanBombard() { + synchronized (this.settlements) { + List<Colony> result = new ArrayList<>(); + for (Settlement s : this.settlements) + if ((s instanceof Colony) && s.canBombardEnemyShip()) + result.add((Colony)s); + return result; + } } /** @@ -2408,9 +2481,10 @@ public class Player extends FreeColGameObject implements Nameable { * @param comp A {@code Comparator} to operate on the colony list. * @return A fresh list of the {@code Colony}s this player owns. */ - public List<Colony> getSortedColonies(Comparator<Colony> comp) { - return transform(getSettlements(), s -> s instanceof Colony, - s -> (Colony)s, comp); + public final List<Colony> getSortedColonies(Comparator<Colony> comp) { + List<Colony> result = getColonies(); + Collections.sort(result, comp); + return result; } /** @@ -2428,8 +2502,11 @@ public class Player extends FreeColGameObject implements Nameable { * @return A list of the {@code IndianSettlement}s this player owns. */ public List<IndianSettlement> getIndianSettlementList() { - return transform(getSettlements(), s -> s instanceof IndianSettlement, - s -> (IndianSettlement)s); + List<IndianSettlement> result = new ArrayList<>(); + for (Settlement s : this.settlements) + if (s instanceof IndianSettlement) + result.add((IndianSettlement)s); + return result; } /** @@ -2441,10 +2518,11 @@ public class Player extends FreeColGameObject implements Nameable { * missionary. */ public List<IndianSettlement> getIndianSettlementsWithMissionaryList(Player p) { - final Predicate<Settlement> isPred = s -> - s instanceof IndianSettlement - && ((IndianSettlement)s).hasMissionary(p); - return transform(getSettlements(), isPred, s -> (IndianSettlement)s); + List<IndianSettlement> result = new ArrayList<>(); + for (Settlement s : this.settlements) + if ((s instanceof IndianSettlement) && ((IndianSettlement)s).hasMissionary(p)) + result.add((IndianSettlement)s); + return result; } /** @@ -2658,9 +2736,8 @@ public class Player extends FreeColGameObject implements Nameable { * @return A suitable {@code Tile}. */ public Tile getFallbackTile() { - Settlement settlement = first(getSettlements()); - return (settlement != null) ? settlement.getTile() - : getEntryLocation().getTile(); + return (this.settlements.size() == 0) ? + getEntryLocation().getTile() : this.settlements.get(0).getTile(); } /** @@ -2830,7 +2907,7 @@ public class Player extends FreeColGameObject implements Nameable { // All other European settlements if can see all colonies. if (hasAbility(Ability.SEE_ALL_COLONIES)) - for (Colony c : getGame().getAllColoniesList(this)) + for (Colony c : getGame().getAllColonies(this)) vismap.setVisible(c); } } diff --git a/src/net/sf/freecol/server/ai/EuropeanAIPlayer.java b/src/net/sf/freecol/server/ai/EuropeanAIPlayer.java index be6f59c9eb0..ea707d51be6 100644 --- a/src/net/sf/freecol/server/ai/EuropeanAIPlayer.java +++ b/src/net/sf/freecol/server/ai/EuropeanAIPlayer.java @@ -536,7 +536,7 @@ public class EuropeanAIPlayer extends MissionAIPlayer { enemies.clear(); enemies.addAll(preferred); } - List<Colony> colonies = player.getColonyList(); + List<Colony> colonies = player.getColonies(); // Find a target to attack. Location target = null; // Few colonies? Attack the weakest European port diff --git a/src/net/sf/freecol/server/model/ServerGame.java b/src/net/sf/freecol/server/model/ServerGame.java index 75cc4db8c09..815f7660010 100644 --- a/src/net/sf/freecol/server/model/ServerGame.java +++ b/src/net/sf/freecol/server/model/ServerGame.java @@ -381,7 +381,7 @@ public class ServerGame extends Game implements ServerModelObject { cs.add(See.perhaps().always(strongest), is); } }); - for (Colony c : weakest.getColonyList()) { + for (Colony c : weakest.getColonies()) { updated.addAll(c.getOwnedTiles()); ((ServerColony)c).csChangeOwner(strongest, false, cs);//-vis(both),-til lb.add(" ", c.getName()); diff --git a/src/net/sf/freecol/server/model/ServerPlayer.java b/src/net/sf/freecol/server/model/ServerPlayer.java index 53dc67c6c5c..745401de83a 100644 --- a/src/net/sf/freecol/server/model/ServerPlayer.java +++ b/src/net/sf/freecol/server/model/ServerPlayer.java @@ -35,7 +35,6 @@ import java.util.function.ToIntFunction; import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; -import java.util.stream.Stream; import net.sf.freecol.FreeCol; import net.sf.freecol.common.FreeColException; @@ -483,7 +482,7 @@ public class ServerPlayer extends Player implements ServerModelObject { } // Quick check for a colony. Do not log, this is the common case. - if (any(getColonies())) return IS_ALIVE; + if (hasSettlements()) return IS_ALIVE; // Do not kill the observing player during a debug run. if (!isAI() && FreeColDebugger.getDebugRunTurns() >= 0) return IS_ALIVE; @@ -835,7 +834,7 @@ public class ServerPlayer extends Player implements ServerModelObject { public boolean updateScore() { int oldScore = this.score; this.score = sum(getUnits(), Unit::getScoreValue) - + sum(getColonies(), Colony::getLiberty) + + getColoniesLiberty() + SCORE_FOUNDING_FATHER * count(getFathers()); int gold = getGold(); if (gold != GOLD_NOT_ACCOUNTED) { @@ -1433,7 +1432,7 @@ public class ServerPlayer extends Player implements ServerModelObject { public void csNaturalDisasters(Random random, ChangeSet cs, int probability) { if (randomInt(logger, "Natural disaster", random, 100) < probability) { - List<Colony> colonies = getColonyList(); + List<Colony> colonies = getColonies(); int size = colonies.size(); if (size <= 0) return; // Randomly select a colony to start with, then generate @@ -1868,7 +1867,7 @@ outer: for (Effect effect : effects) { && (atWarWith(u.getOwner()) || u.hasAbility(Ability.PIRACY))); // For all colonies that are able to bombard, search neighbouring // tiles for targets, and fire! - for (Colony c : transform(getColonies(), Colony::canBombardEnemyShip)) { + for (Colony c : getColoniesCanBombard()) { Tile tile = c.getTile(); for (Unit u : transform(flatten(tile.getSurroundingTiles(1, 1), Tile::getUnits), @@ -1933,7 +1932,7 @@ outer: for (Effect effect : effects) { // Check for tiles that are now visible. They need to be // explored, and always updated so that units are visible. // *Requires that canSee[] has **not** been updated yet!* - Stream<Colony> colonies = (hasAbility(Ability.SEE_ALL_COLONIES)) + List<Colony> colonies = (hasAbility(Ability.SEE_ALL_COLONIES)) ? getGame().getAllColonies(null) : getColonies(); Set<Tile> tiles @@ -1947,7 +1946,7 @@ outer: for (Effect effect : effects) { cs.add(See.only(this), tiles); visibilityChange = true; } else if (Modifier.SOL.equals(m.getId())) { - for (Colony c : getColonyList()) { + for (Colony c : getColonies()) { c.addLiberty(0); // Kick the SoL and production bonus c.invalidateCache(); } @@ -1993,14 +1992,14 @@ outer: for (Effect effect : effects) { case "model.event.freeBuilding": BuildingType type = spec.getBuildingType(event.getValue()); - for (Colony c : getColonyList()) { + for (Colony c : getColonies()) { ((ServerColony)c).csFreeBuilding(type, cs); } break; case "model.event.seeAllColonies": visibilityChange = true;//-vis(this), can now see other colonies - for (Colony colony : game.getAllColoniesList(null)) { + for (Colony colony : game.getAllColonies(null)) { final Tile t = colony.getTile(); Set<Tile> tiles = new HashSet<>(); if (exploreTile(t)) { @@ -4077,7 +4076,7 @@ outer: for (Effect effect : effects) { = transform(mercs, au -> au.getType(spec).isNaval()); Tile dst; if (naval.isEmpty()) { // Deliver to first settlement - dst = first(getColonies()).getTile(); + dst = getFirstColony().getTile(); createUnits(mercs, dst);//-vis: safe, in colony cs.add(See.only(this), dst); } else { // Let them sail in -- 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