Exemple #1
0
  /** {@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();
      }
    }
  }
Exemple #2
0
 /**
  * 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;
 }
Exemple #3
0
 /**
  * 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();
 }
Exemple #4
0
 /**
  * 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);
   }
 }
Exemple #6
0
 /**
  * Describe <code>addGoods</code> method here.
  *
  * @param goods an <code>AbstractGoods</code> value
  */
 public boolean addGoods(AbstractGoods goods) {
   return addGoods(goods.getType(), goods.getAmount());
 }