Exemplo n.º 1
0
  /**
   * Handle a "setBuildQueue"-message.
   *
   * @param server The <code>FreeColServer</code> handling the message.
   * @param player The <code>Player</code> the message applies to.
   * @param connection The <code>Connection</code> message was received on.
   * @return An update containing the new queue or an error <code>Element</code> on failure.
   */
  public Element handle(FreeColServer server, Player player, Connection connection) {
    ServerPlayer serverPlayer = server.getPlayer(connection);
    Game game = server.getGame();
    Specification spec = game.getSpecification();

    Colony colony;
    try {
      colony = player.getOurFreeColGameObject(colonyId, Colony.class);
    } catch (Exception e) {
      return DOMMessage.clientError(e.getMessage());
    }

    if (queue == null) {
      return DOMMessage.clientError("Empty queue");
    }
    List<BuildableType> buildQueue = new ArrayList<BuildableType>();
    for (int i = 0; i < queue.length; i++) {
      try {
        buildQueue.add(i, spec.getType(queue[i], BuildableType.class));
      } catch (Exception cce) {
        return DOMMessage.clientError("Not a buildable type: " + queue[i]);
      }
    }

    // Proceed to set the build queue.
    return server.getInGameController().setBuildQueue(serverPlayer, colony, buildQueue);
  }
Exemplo n.º 2
0
  /**
   * Get a FreeColGameObjectType by identifier from a stream from a specification.
   *
   * @param spec The <code>Specification</code> to look in.
   * @param attributeName the name of the attribute identifying the <code>FreeColGameObjectType
   *     </code>.
   * @param returnClass The expected class of the return value.
   * @param defaultValue A default value to return if the attributeName attribute is not present.
   * @return The <code>FreeColGameObjectType</code> found, or the <code>defaultValue</code>.
   */
  public <T extends FreeColGameObjectType> T getType(
      Specification spec, String attributeName, Class<T> returnClass, T defaultValue) {

    final String attrib =
        // @compat 0.10.7
        (FreeColObject.ID_ATTRIBUTE_TAG.equals(attributeName))
            ? readId()
            :
            // end @compat
            getAttribute(attributeName, (String) null);

    return (attrib == null) ? defaultValue : spec.getType(attrib, returnClass);
  }
Exemplo n.º 3
0
  // @compat 0.10.7
  public <T extends FreeColGameObjectType> T getRole(
      Specification spec, String attributeName, Class<T> returnClass, T defaultValue) {

    String attrib =
        (FreeColObject.ID_ATTRIBUTE_TAG.equals(attributeName))
            ? readId()
            : getAttribute(attributeName, (String) null);

    if (attrib == null) {
      return defaultValue;
    }
    attrib = Role.fixRoleId(attrib);
    return spec.getType(attrib, returnClass);
  }
  /**
   * Selects the most desirable goods from the colony.
   *
   * @param target The colony.
   * @return The goods to demand.
   */
  public Goods selectGoods(Colony target) {
    final Specification spec = getSpecification();
    Tension.Level tension = getUnit().getOwner().getTension(target.getOwner()).getLevel();
    int dx = spec.getInteger(GameOptions.NATIVE_DEMANDS) + 1;
    final GoodsType food = spec.getPrimaryFoodType();
    Goods goods = null;
    int amount = capAmount(target.getGoodsCount(food), dx);
    if (tension.compareTo(Tension.Level.CONTENT) <= 0 && target.getGoodsCount(food) >= amount) {
      return new Goods(getGame(), target, food, amount);
    } else if (tension.compareTo(Tension.Level.DISPLEASED) <= 0) {
      Market market = target.getOwner().getMarket();
      int value = 0;
      for (Goods currentGoods : target.getCompactGoods()) {
        int goodsValue = market.getSalePrice(currentGoods);
        if (currentGoods.getType().isFoodType() || currentGoods.getType().isMilitaryGoods()) {
          continue;
        } else if (goodsValue > value) {
          value = goodsValue;
          goods = currentGoods;
        }
      }
      if (goods != null) {
        goods.setAmount(capAmount(goods.getAmount(), dx));
        return goods;
      }
    } else {
      // Military goods
      for (GoodsType preferred : spec.getGoodsTypeList()) {
        if (preferred.isMilitaryGoods()) {
          amount = target.getGoodsCount(preferred);
          if (amount > 0) {
            return new Goods(getGame(), target, preferred, capAmount(amount, dx));
          }
        }
      }
      // Storable building materials (what do the natives need tools for?)
      for (GoodsType preferred : spec.getStorableGoodsTypeList()) {
        if (preferred.isBuildingMaterial()) {
          amount = target.getGoodsCount(preferred);
          if (amount > 0) {
            return new Goods(getGame(), target, preferred, capAmount(amount, dx));
          }
        }
      }
      // Trade goods
      for (GoodsType preferred : spec.getStorableGoodsTypeList()) {
        if (preferred.isTradeGoods()) {
          amount = target.getGoodsCount(preferred);
          if (amount > 0) {
            return new Goods(getGame(), target, preferred, capAmount(amount, dx));
          }
        }
      }
      // Refined goods
      for (GoodsType preferred : spec.getStorableGoodsTypeList()) {
        if (preferred.isRefined()) {
          amount = target.getGoodsCount(preferred);
          if (amount > 0) {
            return new Goods(getGame(), target, preferred, capAmount(amount, dx));
          }
        }
      }
    }

    // Have not found what we want
    Market market = target.getOwner().getMarket();
    int value = 0;
    for (Goods currentGoods : target.getCompactGoods()) {
      int goodsValue = market.getSalePrice(currentGoods);
      if (goodsValue > value) {
        value = goodsValue;
        goods = currentGoods;
      }
    }
    if (goods != null) {
      goods.setAmount(capAmount(goods.getAmount(), dx));
    }
    return goods;
  }
Exemplo n.º 5
0
  @SuppressWarnings("unchecked") // FIXME in Java7
  private void updateUnitList() {
    final Specification spec = getSpecification();
    DefaultListModel units = (DefaultListModel) unitList.getModel();
    units.clear();
    loop:
    for (UnitType unitType : buildableUnits) {
      // compare colony.getNoBuildReason()
      List<String> lockReason = new ArrayList<String>();
      if (unbuildableTypes.contains(unitType)) {
        continue;
      }

      if (unitType.getRequiredPopulation() > unitCount) {
        lockReason.add(
            Messages.message(
                StringTemplate.template("colonyPanel.populationTooSmall")
                    .addAmount("%number%", unitType.getRequiredPopulation())));
      }

      if (unitType.getLimits() != null) {
        for (Limit limit : unitType.getLimits()) {
          if (!limit.evaluate(colony)) {
            lockReason.add(Messages.message(limit.getDescriptionKey()));
          }
        }
      }

      if (!(colony.hasAbility(Ability.BUILD, unitType, getGame().getTurn())
          || FeatureContainer.hasAbility(featureContainer, Ability.BUILD, unitType, null))) {
        boolean builderFound = false;
        for (Ability ability : spec.getAbilities(Ability.BUILD)) {
          if (ability.appliesTo(unitType)
              && ability.getValue()
              && ability.getSource() != null
              && !unbuildableTypes.contains(ability.getSource())) {
            builderFound = true;
            lockReason.add(Messages.getName(ability.getSource()));
            break;
          }
        }
        if (!builderFound) {
          unbuildableTypes.add(unitType);
          continue;
        }
      }

      for (Entry<String, Boolean> entry : unitType.getRequiredAbilities().entrySet()) {
        if (colony.hasAbility(entry.getKey()) != entry.getValue()
            && FeatureContainer.hasAbility(featureContainer, entry.getKey(), null, null)
                != entry.getValue()) {
          List<FreeColGameObjectType> sources =
              spec.getTypesProviding(entry.getKey(), entry.getValue());
          if (sources.isEmpty()) {
            // no type provides the required ability
            unbuildableTypes.add(unitType);
            continue loop;
          } else {
            lockReason.add(Messages.message(sources.get(0).getNameKey()));
          }
        }
      }
      if (lockReason.isEmpty()) {
        lockReasons.put(unitType, null);
      } else {
        lockReasons.put(
            unitType,
            Messages.message(
                StringTemplate.template("colonyPanel.requires")
                    .addName("%string%", Utils.join("/", lockReason))));
      }
      if (lockReason.isEmpty() || showAll.isSelected()) {
        units.addElement(unitType);
      }
    }
  }