From: "Enrico Weigelt, metux IT consult" <enrico.weig...@gr13.net>
--- .../sf/freecol/client/gui/panel/ColonyPanel.java | 2 +- .../freecol/client/gui/panel/QuickActionMenu.java | 2 +- src/net/sf/freecol/common/model/Colony.java | 170 ++++++++++++++++----- .../sf/freecol/common/model/ProductionType.java | 12 ++ src/net/sf/freecol/common/model/WorkLocation.java | 7 + src/net/sf/freecol/server/ai/ColonyPlan.java | 4 +- src/net/sf/freecol/server/ai/mission/Mission.java | 2 +- src/net/sf/freecol/server/model/ServerColony.java | 11 +- .../net/sf/freecol/common/model/ColonyTest.java | 2 +- 9 files changed, 165 insertions(+), 47 deletions(-) diff --git a/src/net/sf/freecol/client/gui/panel/ColonyPanel.java b/src/net/sf/freecol/client/gui/panel/ColonyPanel.java index 4e4887b05b8..03a46e7f8bc 100644 --- a/src/net/sf/freecol/client/gui/panel/ColonyPanel.java +++ b/src/net/sf/freecol/client/gui/panel/ColonyPanel.java @@ -614,7 +614,7 @@ public final class ColonyPanel extends PortPanel if (colony == null) return; // Check for non-producing locations that can now produce. - for (WorkLocation wl : colony.getCurrentWorkLocationsList()) { + for (WorkLocation wl : colony.getCurrentWorkLocations()) { boolean change = false, check = wl.getProductionType() == null; for (Unit unit : transform(wl.getUnits(), u -> (check || !wl.produces(u.getWorkType())))) { diff --git a/src/net/sf/freecol/client/gui/panel/QuickActionMenu.java b/src/net/sf/freecol/client/gui/panel/QuickActionMenu.java index 8e59b38087b..62c7de17e91 100644 --- a/src/net/sf/freecol/client/gui/panel/QuickActionMenu.java +++ b/src/net/sf/freecol/client/gui/panel/QuickActionMenu.java @@ -333,7 +333,7 @@ public final class QuickActionMenu extends JPopupMenu { int bestOwnedProd = bonus + bonusChange, bestUnownedProd = bonus + bonusChange; WorkLocation bestOwned = null, bestUnowned = null; - for (WorkLocation wl : colony.getAllWorkLocationsList()) { + for (WorkLocation wl : colony.getAllWorkLocations()) { int prod = 0; switch (wl.getNoAddReason(unit)) { case NONE: diff --git a/src/net/sf/freecol/common/model/Colony.java b/src/net/sf/freecol/common/model/Colony.java index a84496bbe5e..a71c95ad293 100644 --- a/src/net/sf/freecol/common/model/Colony.java +++ b/src/net/sf/freecol/common/model/Colony.java @@ -47,7 +47,7 @@ import static net.sf.freecol.common.util.CollectionUtils.*; import net.sf.freecol.common.option.GameOptions; import net.sf.freecol.common.util.LogBuilder; import net.sf.freecol.common.util.RandomChoice; - +import net.sf.freecol.common.util.Utils; /** * Represents a colony. A colony contains {@link Building}s and @@ -455,7 +455,7 @@ public class Colony extends Settlement implements Nameable, TradeLocation { Occupation best = new Occupation(null, null, null); int bestAmount = 0; - for (WorkLocation wl : getCurrentWorkLocationsList()) { + for (WorkLocation wl : getCurrentWorkLocations()) { bestAmount = best.improve(unit, wl, bestAmount, workTypes, lb); } @@ -538,7 +538,7 @@ public class Colony extends Settlement implements Nameable, TradeLocation { * * @return The list of work locations. */ - public List<WorkLocation> getAllWorkLocationsList() { + public List<WorkLocation> getAllWorkLocations() { List<WorkLocation> ret = new ArrayList<>(); synchronized (this.colonyTiles) { ret.addAll(this.colonyTiles); @@ -550,19 +550,22 @@ public class Colony extends Settlement implements Nameable, TradeLocation { } /** - * Gets a stream of every work location in this colony. + * Gets a list of every work location in this colony. * - * @return The stream of work locations. + * @return The list of work locations. */ - public Stream<WorkLocation> getAllWorkLocations() { - Stream<WorkLocation> ret = Stream.<WorkLocation>empty(); + public WorkLocation findWorkLocationById(String id) { synchronized (this.colonyTiles) { - ret = concat(ret, map(this.colonyTiles, ct -> (WorkLocation)ct)); + for (ColonyTile walk : this.colonyTiles) + if (Utils.equals(id, walk.getId())) + return walk; } synchronized (this.buildingMap) { - ret = concat(ret, map(this.buildingMap.values(), b -> (WorkLocation)b)); + for (Building walk : this.buildingMap.values()) + if (Utils.equals(id, walk.getId())) + return walk; } - return ret; + return null; } /** @@ -571,36 +574,82 @@ public class Colony extends Settlement implements Nameable, TradeLocation { * * @return The list of available {@code WorkLocation}s. */ - public List<WorkLocation> getAvailableWorkLocationsList() { - return transform(getAllWorkLocations(), WorkLocation::isAvailable); + public int countAvailableWorkLocations() { + int result = 0; + synchronized (this.colonyTiles) { + for (ColonyTile walk : this.colonyTiles) + if (walk.isAvailable()) + result++; + } + synchronized (this.buildingMap) { + for (Building walk : this.buildingMap.values()) + if (walk.isAvailable()) + result++; + } + return result; } /** - * Get a stream of all freely available work locations in this - * colony. + * Count the freely available work locations in this colony. * - * @return The stream of available {@code WorkLocation}s. + * @return The list of available {@code WorkLocation}s. */ - public Stream<WorkLocation> getAvailableWorkLocations() { - return getAvailableWorkLocationsList().stream(); + public List<WorkLocation> getAvailableWorkLocations() { + List<WorkLocation> result = new ArrayList<>(); + synchronized (this.colonyTiles) { + for (ColonyTile walk : this.colonyTiles) + if (walk.isAvailable()) + result.add(walk); + } + synchronized (this.buildingMap) { + for (Building walk : this.buildingMap.values()) + if (walk.isAvailable()) + result.add(walk); + } + return result; } /** - * Gets a list of all current work locations in this colony. + * Gets a list of all freely available work locations + * in this colony, that can add given unit. * - * @return The list of current {@code WorkLocation}s. + * @param unit The {@code Unit} about to be added. + * @param skip An optional WorkLocation to skip + * @return The list of available {@code WorkLocation}s. */ - public List<WorkLocation> getCurrentWorkLocationsList() { - return transform(getAllWorkLocations(), WorkLocation::isCurrent); + public int getAvailableWorkLocationsCanAdd(Unit unit, WorkLocation skip) { + int result = 0; + synchronized (this.colonyTiles) { + for (ColonyTile walk : this.colonyTiles) + if (walk != skip && walk.isAvailable() && walk.canAdd(unit)) + result++; + } + synchronized (this.buildingMap) { + for (Building walk : this.buildingMap.values()) + if (walk != skip && walk.isAvailable() && walk.canAdd(unit)) + result++; + } + return result; } /** - * Get a stream of all current work locations in this colony. + * Gets a list of all current work locations in this colony. * - * @return The stream of current {@code WorkLocation}s. + * @return The list of current {@code WorkLocation}s. */ - public Stream<WorkLocation> getCurrentWorkLocations() { - return getCurrentWorkLocationsList().stream(); + public List<WorkLocation> getCurrentWorkLocations() { + List<WorkLocation> result = new ArrayList<>(); + synchronized (this.colonyTiles) { + for (ColonyTile walk : this.colonyTiles) + if (walk.isCurrent()) + result.add(walk); + } + synchronized (this.buildingMap) { + for (Building walk : this.buildingMap.values()) + if (walk.isCurrent()) + result.add(walk); + } + return result; } /** @@ -664,7 +713,17 @@ public class Colony extends Settlement implements Nameable, TradeLocation { * {@code Ability}, or null if not found. */ public WorkLocation getWorkLocationWithAbility(String ability) { - return find(getCurrentWorkLocations(), wl -> wl.hasAbility(ability)); + synchronized (this.colonyTiles) { + for (ColonyTile ct : this.colonyTiles) + if (ct.isCurrent() && ct.hasAbility(ability)) + return ct; + } + synchronized (this.buildingMap) { + for (Building b : this.buildingMap.values()) + if (b.isCurrent() && b.hasAbility(ability)) + return b; + } + return null; } /** @@ -693,7 +752,17 @@ public class Colony extends Settlement implements Nameable, TradeLocation { * {@code Modifier}, or null if not found. */ public WorkLocation getWorkLocationWithModifier(String modifier) { - return find(getCurrentWorkLocations(), wl -> wl.hasModifier(modifier)); + synchronized (this.colonyTiles) { + for (WorkLocation walk : this.colonyTiles) + if (walk.isCurrent() && walk.hasModifier(modifier)) + return walk; + } + synchronized (this.buildingMap) { + for (WorkLocation walk : this.buildingMap.values()) + if (walk.isCurrent() && walk.hasModifier(modifier)) + return walk; + } + return null; } /** @@ -720,8 +789,18 @@ public class Colony extends Settlement implements Nameable, TradeLocation { * the given type of goods. */ public List<WorkLocation> getWorkLocationsForConsuming(GoodsType goodsType) { - return transform(getCurrentWorkLocations(), - wl -> AbstractGoods.anyIsType(wl.getInputs(), goodsType)); + List<WorkLocation> ret = new ArrayList<>(); + synchronized (this.colonyTiles) { + for (WorkLocation walk : this.colonyTiles) + if (walk.isCurrent() && AbstractGoods.anyIsType(walk.getInputs(), goodsType)) + ret.add(walk); + } + synchronized (this.buildingMap) { + for (WorkLocation walk : this.buildingMap.values()) + if (walk.isCurrent() && AbstractGoods.anyIsType(walk.getInputs(), goodsType)) + ret.add(walk); + } + return ret; } /** @@ -732,8 +811,18 @@ public class Colony extends Settlement implements Nameable, TradeLocation { * the given type of goods. */ public List<WorkLocation> getWorkLocationsForProducing(GoodsType goodsType) { - return transform(getCurrentWorkLocations(), - wl -> AbstractGoods.anyIsType(wl.getOutputs(), goodsType)); + List<WorkLocation> result = new ArrayList<>(); + synchronized (this.colonyTiles) { + for (WorkLocation walk : this.colonyTiles) + if (walk.isCurrent() && AbstractGoods.anyIsType(walk.getOutputs(), goodsType)) + result.add(walk); + } + synchronized (this.buildingMap) { + for (WorkLocation walk : this.buildingMap.values()) + if (walk.isCurrent() && AbstractGoods.anyIsType(walk.getOutputs(), goodsType)) + result.add(walk); + } + return result; } /** @@ -746,7 +835,17 @@ public class Colony extends Settlement implements Nameable, TradeLocation { * the given type of goods, or null if not found. */ public WorkLocation getWorkLocationForProducing(GoodsType goodsType) { - return first(getWorkLocationsForProducing(goodsType)); + synchronized (this.colonyTiles) { + for (WorkLocation walk : this.colonyTiles) + if (walk.isCurrent() && AbstractGoods.anyIsType(walk.getOutputs(), goodsType)) + return walk; + } + synchronized (this.buildingMap) { + for (WorkLocation walk : this.buildingMap.values()) + if (walk.isCurrent() && AbstractGoods.anyIsType(walk.getOutputs(), goodsType)) + return walk; + } + return null; } /** @@ -1712,7 +1811,7 @@ public class Colony extends Settlement implements Nameable, TradeLocation { int result, v; if (player.owns(this)) { result = 0; - for (WorkLocation wl : getAvailableWorkLocationsList()) { + for (WorkLocation wl : getAvailableWorkLocations()) { v = wl.evaluateFor(player); if (v == Integer.MIN_VALUE) return Integer.MIN_VALUE; result += v; @@ -2033,7 +2132,7 @@ public class Colony extends Settlement implements Nameable, TradeLocation { * present. */ public void updateProductionTypes() { - for (WorkLocation wl : getAvailableWorkLocationsList()) { + for (WorkLocation wl : getAvailableWorkLocations()) { wl.updateProductionType(); } } @@ -2394,8 +2493,7 @@ loop: for (WorkLocation wl : getWorkLocationsForProducing(goodsType)) { public <T extends FreeColObject> T getCorresponding(T fco) { final String id = fco.getId(); return (fco instanceof WorkLocation) - ? (T)find(getAllWorkLocations(), - matchKeyEquals(id, WorkLocation::getId)) + ? (T)findWorkLocationById(id) : (fco instanceof Tile) ? (T)((getTile().getId().equals(id)) ? getTile() : find(map(getColonyTiles(), ColonyTile::getWorkTile), diff --git a/src/net/sf/freecol/common/model/ProductionType.java b/src/net/sf/freecol/common/model/ProductionType.java index c5f965205bf..2911fe78bde 100644 --- a/src/net/sf/freecol/common/model/ProductionType.java +++ b/src/net/sf/freecol/common/model/ProductionType.java @@ -155,6 +155,18 @@ public class ProductionType extends FreeColSpecObject { } /** + * Check whether this production type can consume an {@code AbstractGoods} + */ + public final boolean canConsume(AbstractGoods goods) { + if (inputs != null) + for (AbstractGoods walk : inputs) + if (walk.isType(goods.getType())) + return true; + + return false; + } + + /** * Set the input goods. * * @param newInputs The new list of input {@code AbstractGoods}. diff --git a/src/net/sf/freecol/common/model/WorkLocation.java b/src/net/sf/freecol/common/model/WorkLocation.java index 40d69537f04..da1b7b157f7 100644 --- a/src/net/sf/freecol/common/model/WorkLocation.java +++ b/src/net/sf/freecol/common/model/WorkLocation.java @@ -418,6 +418,13 @@ public abstract class WorkLocation extends UnitLocation } /** + * Check whether this WorkLocation can consume an {@code AbstractGoods} + */ + public final boolean canConsume(AbstractGoods ag) { + return (productionType == null ? false : productionType.canConsume(ag)); + } + + /** * Get the {@code AbstractGoods} produced by this work location. * * @return A stream of {@code AbstractGoods} produced. diff --git a/src/net/sf/freecol/server/ai/ColonyPlan.java b/src/net/sf/freecol/server/ai/ColonyPlan.java index 1de387b5019..5db72192068 100644 --- a/src/net/sf/freecol/server/ai/ColonyPlan.java +++ b/src/net/sf/freecol/server/ai/ColonyPlan.java @@ -394,7 +394,7 @@ public class ColonyPlan { */ private Map<GoodsType, Map<WorkLocation, Integer>> createProductionMap() { Map<GoodsType, Map<WorkLocation, Integer>> production = new HashMap<>(); - for (WorkLocation wl : colony.getAvailableWorkLocationsList()) { + for (WorkLocation wl : colony.getAvailableWorkLocations()) { for (GoodsType g : spec().getGoodsTypeList()) { int p = wl.getGenericPotential(g); if (p > 0) { @@ -1377,7 +1377,7 @@ public class ColonyPlan { // starvation and add one worker. if (col.getUnitCount() == 0) { if (getFoodPlans().isEmpty()) { -locations: for (WorkLocation wl : col.getAvailableWorkLocationsList()) { +locations: for (WorkLocation wl : col.getAvailableWorkLocations()) { for (Unit u : new ArrayList<>(workers)) { for (GoodsType type : libertyGoodsTypes) { if (wl.canAdd(u) diff --git a/src/net/sf/freecol/server/ai/mission/Mission.java b/src/net/sf/freecol/server/ai/mission/Mission.java index 32772c60fbd..ab9a28966e3 100644 --- a/src/net/sf/freecol/server/ai/mission/Mission.java +++ b/src/net/sf/freecol/server/ai/mission/Mission.java @@ -613,7 +613,7 @@ public abstract class Mission extends AIObject { Colony colony = (Colony)s; // Favour coastal value += ((colony.isConnectedPort()) ? 10 : 0) - + colony.getAvailableWorkLocationsList().size(); + + colony.getAvailableWorkLocationCount(); } return value; }); diff --git a/src/net/sf/freecol/server/model/ServerColony.java b/src/net/sf/freecol/server/model/ServerColony.java index 5c047e3c631..5b09be3c743 100644 --- a/src/net/sf/freecol/server/model/ServerColony.java +++ b/src/net/sf/freecol/server/model/ServerColony.java @@ -195,10 +195,11 @@ public class ServerColony extends Colony implements ServerModelObject { public boolean ejectUnits(WorkLocation workLocation, List<Unit> units) { if (units == null || units.isEmpty()) return false; unit: for (Unit u : units) { - for (WorkLocation wl : transform(getAvailableWorkLocations(), - w -> w != workLocation && w.canAdd(u))) { - u.setLocation(wl);//-vis: safe/colony - continue unit; + for (WorkLocation wl : getAvailableWorkLocations()) { + if ((wl != workLocation) && wl.canAdd(u)) { + u.setLocation(wl);//-vis: safe/colony + continue unit; + } } u.setLocation(getTile());//-vis: safe/colony } @@ -547,7 +548,7 @@ public class ServerColony extends Colony implements ServerModelObject { container.saveState(); // Check for learning by experience - for (WorkLocation workLocation : getCurrentWorkLocationsList()) { + for (WorkLocation workLocation : getCurrentWorkLocations()) { ((ServerModelObject)workLocation).csNewTurn(random, lb, cs); ProductionInfo productionInfo = getProductionInfo(workLocation); if (productionInfo == null) continue; diff --git a/test/src/net/sf/freecol/common/model/ColonyTest.java b/test/src/net/sf/freecol/common/model/ColonyTest.java index 2d246878463..71b1827cb34 100644 --- a/test/src/net/sf/freecol/common/model/ColonyTest.java +++ b/test/src/net/sf/freecol/common/model/ColonyTest.java @@ -508,7 +508,7 @@ public class ColonyTest extends FreeColTestCase { assertEquals(oct.getColony(), copied); assertEquals(oct.getOwningSettlement(), copied); - for (WorkLocation wl : colony.getAllWorkLocationsList()) { + for (WorkLocation wl : colony.getAllWorkLocations()) { WorkLocation owl = copied.getCorresponding(wl); assertNotNull(owl); assertFalse(wl == owl); -- 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