/** * Chooses a type of goods for some of the natives in a settlement to manufacture. Simple rule: * choose the refined goods that is the greatest shortage for which there is a surplus of the raw * material. * * @return A <code>GoodsType</code> to manufacture, or null if none suitable. */ private GoodsType goodsToMake() { GoodsType wantGoods = null; int diff, wantAmount = -1; for (GoodsType g : getSpecification().getGoodsTypeList()) { GoodsType produced; if (g.isRawMaterial() && (produced = g.getOutputType()) != null && !produced.isBreedable() && produced.isStorable() && getGoodsCount(g) > getWantedGoodsAmount(g) && (diff = getWantedGoodsAmount(produced) - getGoodsCount(produced)) > wantAmount) { wantGoods = produced; wantAmount = diff; } } return wantGoods; }
/** * Updates the goods wanted by this settlement. * * <p>It is only meaningful to call this method from the server, since the settlement's {@link * GoodsContainer} is hidden from the clients. */ public void updateWantedGoods() { final Specification spec = getSpecification(); final java.util.Map<GoodsType, Integer> prices = new HashMap<>(); for (GoodsType gt : spec.getGoodsTypeList()) { // The natives do not trade military or non-storable goods. if (gt.isMilitaryGoods() || !gt.isStorable()) continue; prices.put(gt, getNormalGoodsPriceToBuy(gt, GoodsContainer.CARGO_SIZE)); } int wantedIndex = 0; for (Entry<GoodsType, Integer> e : mapEntriesByValue(prices, descendingIntegerComparator)) { GoodsType goodsType = e.getKey(); if (e.getValue() <= GoodsContainer.CARGO_SIZE * TRADE_MINIMUM_PRICE || wantedIndex >= wantedGoods.length) break; wantedGoods[wantedIndex] = goodsType; wantedIndex++; } for (; wantedIndex < wantedGoods.length; wantedIndex++) { wantedGoods[wantedIndex] = null; } }