From: <bur...@us...> - 2011-02-26 15:20:25
|
Revision: 8052 http://freecol.svn.sourceforge.net/freecol/?rev=8052&view=rev Author: burschik Date: 2011-02-26 15:20:18 +0000 (Sat, 26 Feb 2011) Log Message: ----------- Remove getProductionNetOf(). Modified Paths: -------------- freecol/trunk/src/net/sf/freecol/common/model/Colony.java freecol/trunk/src/net/sf/freecol/server/ai/AIColony.java freecol/trunk/src/net/sf/freecol/server/ai/mission/ScoutingMission.java freecol/trunk/src/net/sf/freecol/server/model/ServerColony.java freecol/trunk/test/src/net/sf/freecol/common/model/BuildingTest.java freecol/trunk/test/src/net/sf/freecol/common/model/ColonyProductionTest.java freecol/trunk/test/src/net/sf/freecol/server/model/ServerColonyTest.java Modified: freecol/trunk/src/net/sf/freecol/common/model/Colony.java =================================================================== --- freecol/trunk/src/net/sf/freecol/common/model/Colony.java 2011-02-26 13:32:32 UTC (rev 8051) +++ freecol/trunk/src/net/sf/freecol/common/model/Colony.java 2011-02-26 15:20:18 UTC (rev 8052) @@ -1424,19 +1424,6 @@ } /** - * Returns how much of a Good will be produced by this colony this turn, - * taking into account how much is consumed - by workers, horses, etc. - * - * @param goodsType The goods' type. - * @return The amount of the given goods currently unallocated for next - * turn. - */ - public int getProductionNetOf(GoodsType goodsType) { - // TODO: we need to cache the production map - return getNetProduction().getCount(goodsType); - } - - /** * Returns <code>true</code> if this Colony can breed the given * type of Goods. Only animals (such as horses) are expected to be * breedable. @@ -2074,6 +2061,7 @@ public TypeCountMap<GoodsType> getNetProduction() { + // TODO: cache productionMap return getNetProduction(getProductionAndConsumption()); } Modified: freecol/trunk/src/net/sf/freecol/server/ai/AIColony.java =================================================================== --- freecol/trunk/src/net/sf/freecol/server/ai/AIColony.java 2011-02-26 13:32:32 UTC (rev 8051) +++ freecol/trunk/src/net/sf/freecol/server/ai/AIColony.java 2011-02-26 15:20:18 UTC (rev 8052) @@ -307,6 +307,7 @@ int goodsWishValue = 50; Map<Object, ProductionInfo> info = colony.getProductionAndConsumption(); + TypeCountMap<GoodsType> netProduction = colony.getNetProduction(info); // for every non-expert, request expert replacement for (Unit unit : colony.getUnitList()) { @@ -356,7 +357,7 @@ // add building materials if (colony.getCurrentlyBuilding() != null) { for (AbstractGoods goods : colony.getCurrentlyBuilding().getGoodsRequired()) { - if (colony.getProductionNetOf(goods.getType()) == 0) { + if (netProduction.getCount(goods.getType()) == 0) { requiredGoods.incrementCount(goods.getType(), goods.getAmount()); } } @@ -580,6 +581,10 @@ } else { + GoodsType toolType = colony.getSpecification().getGoodsType("model.goods.tools"); + GoodsType hammerType = colony.getSpecification().getGoodsType("model.goods.hammers"); + + TypeCountMap<GoodsType> netProduction = colony.getNetProduction(); ArrayList<AIGoods> newAIGoods = new ArrayList<AIGoods>(); List<GoodsType> goodsList = colony.getSpecification().getGoodsTypeList(); @@ -595,9 +600,9 @@ } // Only export military goods if we do not have room for them: if (goodsType.isMilitaryGoods() - && (colony.getProductionOf(goodsType) == 0 + && (netProduction.getCount(goodsType) == 0 || (colony.getGoodsCount(goodsType) - < capacity - colony.getProductionOf(goodsType)))) { + < capacity - netProduction.getCount(goodsType)))) { continue; } @@ -608,9 +613,7 @@ continue loop; } } - Building consumer = colony.getBuildingForConsuming(goodsType); - if (consumer != null - && colony.getProductionOf(goodsType) < consumer.getGoodsInput()) { + if (netProduction.getCount(goodsType) < 0) { continue; } @@ -619,23 +622,23 @@ * sufficient amounts in warehouse: */ // TODO: make this more generic - if (goodsType == colony.getSpecification().getGoodsType("model.goods.tools") && colony.getGoodsCount(colony.getSpecification().getGoodsType("model.goods.tools")) > 0) { - if (colony.getProductionNetOf(colony.getSpecification().getGoodsType("model.goods.tools")) > 0) { + if (goodsType == toolType && colony.getGoodsCount(toolType) > 0) { + if (netProduction.getCount(toolType) > 0) { final BuildableType currentlyBuilding = colony.getCurrentlyBuilding(); int requiredTools = getToolsRequired(currentlyBuilding); int requiredHammers = getHammersRequired(currentlyBuilding); - int buildTurns = (requiredHammers - colony.getGoodsCount(colony.getSpecification().getGoodsType("model.goods.hammers"))) / - (colony.getProductionOf(colony.getSpecification().getGoodsType("model.goods.hammers")) + 1); + int buildTurns = (requiredHammers - colony.getGoodsCount(hammerType)) / + (netProduction.getCount(hammerType) + 1); if (requiredTools > 0) { if (colony.getWarehouseCapacity() > 100) { requiredTools += 100; } - int toolsProductionTurns = requiredTools / colony.getProductionNetOf(colony.getSpecification().getGoodsType("model.goods.tools")); + int toolsProductionTurns = requiredTools / netProduction.getCount(toolType); if (buildTurns <= toolsProductionTurns + 1) { continue; } } else if (colony.getWarehouseCapacity() > 100 - && colony.getGoodsCount(colony.getSpecification().getGoodsType("model.goods.tools")) <= 100) { + && colony.getGoodsCount(toolType) <= 100) { continue; } } else { @@ -1040,9 +1043,10 @@ // Changes the production type of workers producing a cargo there // is no room for. + TypeCountMap<GoodsType> netProduction = colony.getNetProduction(); List<GoodsType> goodsList = colony.getSpecification().getGoodsTypeList(); for (GoodsType goodsType : goodsList) { - int production = colony.getProductionNetOf(goodsType); + int production = netProduction.getCount(goodsType); int in_stock = colony.getGoodsCount(goodsType); if (foodType != goodsType && goodsType.isStorable() @@ -1055,7 +1059,7 @@ final Location oldLocation = unit.getLocation(); unit.putOutsideColony(); boolean working = false; - waste = colony.getGoodsCount(goodsType) + colony.getProductionNetOf(goodsType) + waste = colony.getGoodsCount(goodsType) + netProduction.getCount(goodsType) - colony.getWarehouseCapacity(); int best = 0; for (GoodsType goodsType2 : goodsList) { @@ -1065,7 +1069,7 @@ int production2 = (bestTile == null ? 0 : bestTile.getProductionOf(unit, goodsType2)); if (production2 > best && production2 + colony.getGoodsCount(goodsType2) - + colony.getProductionNetOf(goodsType2) < colony.getWarehouseCapacity()){ + + netProduction.getCount(goodsType2) < colony.getWarehouseCapacity()){ if (working){ unit.putOutsideColony(); } Modified: freecol/trunk/src/net/sf/freecol/server/ai/mission/ScoutingMission.java =================================================================== --- freecol/trunk/src/net/sf/freecol/server/ai/mission/ScoutingMission.java 2011-02-26 13:32:32 UTC (rev 8051) +++ freecol/trunk/src/net/sf/freecol/server/ai/mission/ScoutingMission.java 2011-02-26 15:20:18 UTC (rev 8052) @@ -264,8 +264,8 @@ } else if (scoutEquipment != null && t.getColony() != null && t.getColony().getOwner() == u.getOwner()) { for (AbstractGoods goods : scoutEquipment.getGoodsRequired()) { if (goods.getType().isBreedable() && !t.getColony().canBreed(goods.getType()) && - // TODO: remove assumptions about auto-production implementation - t.getColony().getProductionNetOf(goods.getType()) > 1) { + // TODO: remove assumptions about auto-production implementation + t.getColony().getNetProduction().getCount(goods.getType()) > 1) { return true; } } Modified: freecol/trunk/src/net/sf/freecol/server/model/ServerColony.java =================================================================== --- freecol/trunk/src/net/sf/freecol/server/model/ServerColony.java 2011-02-26 13:32:32 UTC (rev 8051) +++ freecol/trunk/src/net/sf/freecol/server/model/ServerColony.java 2011-02-26 15:20:18 UTC (rev 8052) @@ -365,16 +365,17 @@ if (!(exportData.isExported() && hasAbility("model.ability.export") && owner.canTrade(type, Market.Access.CUSTOM_HOUSE)) - && amount <= limit - && amount + getProductionNetOf(type) > limit) { - int lose = amount + getProductionNetOf(type) - limit; - cs.addMessage(See.only(owner), - new ModelMessage(ModelMessage.MessageType.WAREHOUSE_CAPACITY, - "model.building.warehouseSoonFull", - this, type) - .add("%goods%", goods.getNameKey()) - .addName("%colony%", getName()) - .addName("%amount%", String.valueOf(lose))); + && amount <= limit) { + int loss = amount + netProduction.getCount(type) - limit; + if (loss > 0) { + cs.addMessage(See.only(owner), + new ModelMessage(ModelMessage.MessageType.WAREHOUSE_CAPACITY, + "model.building.warehouseSoonFull", + this, type) + .add("%goods%", goods.getNameKey()) + .addName("%colony%", getName()) + .addAmount("%amount%", loss)); + } } } Modified: freecol/trunk/test/src/net/sf/freecol/common/model/BuildingTest.java =================================================================== --- freecol/trunk/test/src/net/sf/freecol/common/model/BuildingTest.java 2011-02-26 13:32:32 UTC (rev 8051) +++ freecol/trunk/test/src/net/sf/freecol/common/model/BuildingTest.java 2011-02-26 15:20:18 UTC (rev 8052) @@ -337,20 +337,22 @@ weaver.add(worker); assertEquals(worker, weaver.getUnitList().get(0)); + colony.addGoods(cottonType, 2); + TypeCountMap<GoodsType> netProduction = colony.getNetProduction(); assertEquals(2, colony.getProductionOf(cottonType)); - colony.addGoods(cottonType, 2); assertEquals(2, weaver.getProductionOf(clothType)); assertEquals(2, colony.getProductionOf(clothType)); - assertEquals(-1, colony.getProductionNetOf(cottonType)); - assertEquals(3, colony.getProductionNetOf(clothType)); + assertEquals(-1, netProduction.getCount(cottonType)); + assertEquals(3, netProduction.getCount(clothType)); colonist.setWorkType(cottonType); + netProduction = colony.getNetProduction(); assertEquals(4, colony.getProductionOf(cottonType)); colony.addGoods(cottonType, 4); assertEquals(3, colony.getProductionOf(clothType)); - assertEquals(1, colony.getProductionNetOf(cottonType)); - assertEquals(3, colony.getProductionNetOf(clothType)); + assertEquals(1, netProduction.getCount(cottonType)); + assertEquals(3, netProduction.getCount(clothType)); } @@ -368,44 +370,54 @@ assertEquals(grainType, pasture.getGoodsInputType()); assertEquals(horsesType, pasture.getGoodsOutputType()); + TypeCountMap<GoodsType> netProduction = colony.getNetProduction(); + // no horses yet - assertEquals(10, colony.getFoodProduction()); + assertEquals(8, netProduction.getCount(foodType)); assertEquals(0, pasture.getProductionOf(horsesType)); - assertEquals(2, colony.getFoodConsumption()); - assertEquals(0, pasture.getGoodsInputNextTurn()); - //assertEquals(8, colony.getProductionNetOf(foodType)); - assertEquals(0, colony.getProductionNetOf(horsesType)); + assertEquals(0, netProduction.getCount(horsesType)); assertEquals(0, pasture.getMaximumProduction()); colony.addGoods(horsesType, 50); + netProduction = colony.getNetProduction(); assertEquals(2, pasture.getProductionOf(horsesType)); assertEquals(2, pasture.getMaximumProduction()); - assertEquals(2, colony.getProductionNetOf(horsesType)); + assertEquals(2, netProduction.getCount(horsesType)); + colony.addGoods(horsesType, 1); + netProduction = colony.getNetProduction(); assertEquals(4, pasture.getProductionOf(horsesType)); assertEquals(4, pasture.getMaximumProduction()); - assertEquals(4, colony.getProductionNetOf(horsesType)); + assertEquals(4, netProduction.getCount(horsesType)); pasture.upgrade(); colony.removeGoods(horsesType); colony.addGoods(horsesType, 25); + netProduction = colony.getNetProduction(); assertEquals(2, pasture.getProductionOf(horsesType)); assertEquals(2, pasture.getMaximumProduction()); - assertEquals(2, colony.getProductionNetOf(horsesType)); + assertEquals(2, netProduction.getCount(horsesType)); + colony.addGoods(horsesType, 1); + netProduction = colony.getNetProduction(); assertEquals(4, pasture.getProductionOf(horsesType)); assertEquals(4, pasture.getMaximumProduction()); - assertEquals(4, colony.getProductionNetOf(horsesType)); + assertEquals(4, netProduction.getCount(horsesType)); + colony.addGoods(horsesType, 24); + netProduction = colony.getNetProduction(); assertEquals(4, pasture.getProductionOf(horsesType)); assertEquals(4, pasture.getMaximumProduction()); - assertEquals(4, colony.getProductionNetOf(horsesType)); + assertEquals(4, netProduction.getCount(horsesType)); + colony.addGoods(horsesType, 1); + netProduction = colony.getNetProduction(); assertEquals(6, pasture.getProductionOf(horsesType)); assertEquals(6, pasture.getMaximumProduction()); - // TODO: find out why this still fails - // assertEquals(6, colony.getProductionNetOf(horsesType)); + assertEquals("Horse production should equal food surplus.", + netProduction.getCount(foodType), + netProduction.getCount(horsesType)); } public void testTownhallProduction() { Modified: freecol/trunk/test/src/net/sf/freecol/common/model/ColonyProductionTest.java =================================================================== --- freecol/trunk/test/src/net/sf/freecol/common/model/ColonyProductionTest.java 2011-02-26 13:32:32 UTC (rev 8051) +++ freecol/trunk/test/src/net/sf/freecol/common/model/ColonyProductionTest.java 2011-02-26 15:20:18 UTC (rev 8052) @@ -177,7 +177,7 @@ int expectedBellProd = 1; int bellsUpkeep = colony.getConsumptionOf(bellsType); int expectedBellUpkeep = colony.getUnitCount() - 2; - int bellsNetProdPerTurn = colony.getProductionNetOf(bellsType); + int bellsNetProdPerTurn = colony.getNetProduction().getCount(bellsType); int expectedBellNetProd = expectedBellProd - expectedBellUpkeep; assertEquals("Wrong bell count", expectedBellCount, initialBellCount); @@ -194,6 +194,7 @@ game.setMap(getTestMap()); Colony colony = getStandardColony(1); + GoodsType foodType = spec().getGoodsType("model.goods.food"); GoodsType horsesType = spec().getGoodsType("model.goods.horses"); Building pasture = colony.getBuilding(spec().getBuildingType("model.building.country")); @@ -202,19 +203,20 @@ // Still room for more colony.addGoods(horsesType, 99); + TypeCountMap<GoodsType> netProduction = colony.getNetProduction(); + assertTrue(netProduction.getCount(foodType) > 0); - assertTrue(colony.getFoodProduction() > colony.getFoodConsumption()); - assertEquals("Wrong horse production",1, pasture.getProductionOf(horsesType)); assertEquals("Wrong maximum horse production", 4, pasture.getMaximumProduction()); - assertEquals("Wrong net horse production",1, colony.getProductionNetOf(horsesType)); + assertEquals("Wrong net horse production",1, netProduction.getCount(horsesType)); // No more room available colony.addGoods(horsesType, 1); + netProduction = colony.getNetProduction(); assertEquals("Wrong number of horses in colony",colony.getWarehouseCapacity(), colony.getGoodsCount(horsesType)); assertEquals("Wrong horse production",0, pasture.getProductionOf(horsesType)); assertEquals("Wrong maximum horse production", 4, pasture.getMaximumProduction()); - assertEquals("Wrong net horse production",0, colony.getProductionNetOf(horsesType)); + assertEquals("Wrong net horse production",0, netProduction.getCount(horsesType)); } Modified: freecol/trunk/test/src/net/sf/freecol/server/model/ServerColonyTest.java =================================================================== --- freecol/trunk/test/src/net/sf/freecol/server/model/ServerColonyTest.java 2011-02-26 13:32:32 UTC (rev 8051) +++ freecol/trunk/test/src/net/sf/freecol/server/model/ServerColonyTest.java 2011-02-26 15:20:18 UTC (rev 8052) @@ -32,6 +32,7 @@ import net.sf.freecol.common.model.Player; import net.sf.freecol.common.model.Tile; import net.sf.freecol.common.model.TileType; +import net.sf.freecol.common.model.TypeCountMap; import net.sf.freecol.common.model.Unit; import net.sf.freecol.common.model.Unit.UnitState; import net.sf.freecol.common.model.UnitType; @@ -336,10 +337,11 @@ assertEquals(population, colony.getUnitCount()); assertEquals(4, colony.getProductionOf(bellsType)); - assertEquals(population - 2, colony.getConsumptionOf(bellsType)); + assertEquals(population, colony.getConsumptionOf(bellsType)); + TypeCountMap<GoodsType> netProduction = colony.getNetProduction(); int bells = colony.getProductionOf(bellsType) - colony.getConsumptionOf(bellsType); - assertEquals(bells, colony.getProductionNetOf(bellsType)); + assertEquals(bells, netProduction.getCount(bellsType)); assertEquals(bells, colony.getGoodsCount(bellsType)); assertEquals(bells, colony.getLiberty()); @@ -354,7 +356,7 @@ assertEquals(bells, colony.getLiberty()); int crosses = colony.getProductionOf(crossType) - colony.getConsumptionOf(crossType); - assertEquals(crosses, colony.getProductionNetOf(crossType)); + assertEquals(crosses, netProduction.getCount(crossType)); assertEquals(crosses, colony.getGoodsCount(crossType)); assertEquals(crosses, colony.getImmigration()); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |