/** * Convenience function to get the best output for a given goods type from a collection of * production types. * * @param goodsType The <code>GoodsType</code> to use. * @param types A collection of <code>ProductionType</code>s to consider. * @return The most productive output that produces the goods type, or null if none found. */ public static AbstractGoods getBestOutputFor( GoodsType goodsType, Collection<ProductionType> types) { AbstractGoods best = null; for (ProductionType productionType : types) { for (AbstractGoods output : productionType.getOutputs()) { if (output.getType() == goodsType && (best == null || output.getAmount() > best.getAmount())) { best = output; } } } return best; }
/** * Get the type of the most productive output. * * @return The <code>GoodsType</code> of the most productive output. */ public GoodsType getBestOutputType() { AbstractGoods best = null; if (outputs != null) { int amount = 0; for (AbstractGoods output : outputs) { if (amount < output.getAmount()) { amount = output.getAmount(); best = output; } } } return (best == null) ? null : best.getType(); }
/** * Convenience function to check if there is an output for a given goods type in a collection of * production types. * * @param goodsType The <code>GoodsType</code> to use. * @param types A list of <code>ProductionType</code>s to consider. * @return The most productive output that produces the goods type, or null if none found. */ public static boolean canProduce(GoodsType goodsType, Collection<ProductionType> types) { for (ProductionType productionType : types) { AbstractGoods output = AbstractGoods.findByType(goodsType, productionType.getOutputs()); if (output != null) return true; } return false; }
/** * Get the goods of the given goods type in this production type. * * @param goodsType The <code>GoodsType</code> to check. * @return The <code>AbstractGoods</code> output if any, otherwise null. */ public AbstractGoods getOutput(GoodsType goodsType) { if (outputs != null) { AbstractGoods output = AbstractGoods.findByType(goodsType, outputs); if (output != null) return output; } return null; }
/** * Calculates how much of the given goods type this settlement wants and should retain. * * @param type The <code>GoodsType</code>. * @return The amount of goods wanted. */ protected int getWantedGoodsAmount(GoodsType type) { if (getUnitCount() <= 0) return 0; final Specification spec = getSpecification(); final UnitType unitType = getFirstUnit().getType(); final Role militaryRole = Role.getAvailableRoles(getOwner(), unitType, spec.getMilitaryRoles()).get(0); if (type.isMilitaryGoods()) { // Retain enough goods to fully arm. int need = 0; for (Unit u : ownedUnits) { if (u.getRole() == militaryRole) continue; List<AbstractGoods> required = u.getGoodsDifference(militaryRole, 1); need += AbstractGoods.getCount(type, required); } return need; } int consumption = getConsumptionOf(type); if (type == spec.getPrimaryFoodType()) { // Food is perishable, do not try to retain that much return Math.max(40, consumption * 3); } if (type.isTradeGoods() || type.isNewWorldLuxuryType() || type.isRefined()) { // Aim for 10 years supply, resupply is doubtful return Math.max(80, consumption * 20); } // Just keep some around return 2 * getUnitCount(); }
/** * Get the production type with the greatest total output of an optional goods type from a * collection of production types * * @param goodsType An optional <code>GoodsType</code> to restrict the choice of outputs with. * @param types A collection of <code>ProductionType</code>s to consider. * @return The most productive <code>ProductionType</code>. */ public static ProductionType getBestProductionType( GoodsType goodsType, Collection<ProductionType> types) { ProductionType best = null; int bestSum = 0; for (ProductionType pt : types) { int sum = 0; for (AbstractGoods output : pt.getOutputs()) { if (goodsType == null || goodsType == output.getType()) { sum += output.getAmount(); } } if (bestSum < sum) { bestSum = sum; best = pt; } } return best; }
/** * Add some initial goods to a newly generated settlement. After all, they have been here for some * time. * * @param random A pseudo-random number source. */ public void addRandomGoods(Random random) { HashMap<GoodsType, Integer> goodsMap = new HashMap<>(); for (Tile t : getOwnedTiles()) { for (AbstractGoods ag : t.getSortedPotential()) { GoodsType type = ag.getType().getStoredAs(); Integer i = goodsMap.get(type); int value = (i == null) ? 0 : i; goodsMap.put(type, value + ag.getAmount()); } } double d = randomInt(logger, "Goods at " + getName(), random, 10) * 0.1 + 1.0; for (Entry<GoodsType, Integer> e : goodsMap.entrySet()) { int i = e.getValue(); if (!e.getKey().isFoodType()) i = (int) Math.round(d * e.getValue()); i = Math.min(i, GoodsContainer.CARGO_SIZE); if (i > 0) addGoods(e.getKey(), i); } }
/** {@inheritDoc} */ @Override protected void writeChildren(FreeColXMLWriter xw) throws XMLStreamException { super.writeChildren(xw); if (inputs != null) { for (AbstractGoods input : inputs) { xw.writeStartElement(INPUT_TAG); xw.writeAttribute(GOODS_TYPE_TAG, input.getType()); xw.writeAttribute(VALUE_TAG, input.getAmount()); xw.writeEndElement(); } } if (outputs != null) { for (AbstractGoods output : outputs) { xw.writeStartElement(OUTPUT_TAG); xw.writeAttribute(GOODS_TYPE_TAG, output.getType()); xw.writeAttribute(VALUE_TAG, output.getAmount()); xw.writeEndElement(); } } }
/** * Describe <code>addGoods</code> method here. * * @param goods an <code>AbstractGoods</code> value */ public boolean addGoods(AbstractGoods goods) { return addGoods(goods.getType(), goods.getAmount()); }