/**
  * Evaluate a potential cashin mission for a given unit and path.
  *
  * @param aiUnit The <code>AIUnit</code> to do the mission.
  * @param path A <code>PathNode</code> to take to the target.
  * @return A score for the proposed mission.
  */
 public static int scorePath(AIUnit aiUnit, PathNode path) {
   Location loc;
   if (path == null
       || (loc = extractTarget(aiUnit, path)) == null
       || (loc instanceof Colony && invalidFullColonyReason(aiUnit, loc.getColony()) != null))
     return Integer.MIN_VALUE;
   return aiUnit.getUnit().getTreasureAmount() / (path.getTotalTurns() + 1);
 }
 /**
  * Extract a valid target for this mission from a path.
  *
  * @param aiUnit A <code>AIUnit</code> to perform the mission.
  * @param path A <code>PathNode</code> to extract a target from, (uses the unit location if null).
  * @return A target for this mission, or null if none found.
  */
 public static Location extractTarget(AIUnit aiUnit, PathNode path) {
   if (path == null) return null;
   final Location loc = path.getLastNode().getLocation();
   Colony colony = loc.getColony();
   return (loc instanceof Europe && invalidReason(aiUnit, loc) == null)
       ? loc
       : (colony != null && invalidReason(aiUnit, colony) == null) ? colony : null;
 }
  /** {@inheritDoc} */
  @Override
  protected void writeAttributes(FreeColXMLWriter xw) throws XMLStreamException {
    super.writeAttributes(xw);

    if (target != null) {
      xw.writeAttribute(TARGET_TAG, target.getId());
    }
  }
示例#4
0
文件: Wish.java 项目: vardars/FreeCol
  /** {@inheritDoc} */
  @Override
  protected void writeAttributes(FreeColXMLWriter xw) throws XMLStreamException {
    super.writeAttributes(xw);

    // Write identifier, Location will match Object
    if (destination != null) {
      xw.writeAttribute(DESTINATION_TAG, destination.getId());

      if (transportable != null) {
        xw.writeAttribute(TRANSPORTABLE_TAG, transportable.getId());
      }
    }
  }
示例#5
0
  /** The constructor that will add the items to this panel. */
  public ReportLabourPanel(FreeColClient freeColClient) {
    super(freeColClient, "reportLabourAction");

    this.data = new HashMap<>();
    this.unitCount = new TypeCountMap<>();
    for (Unit unit : getMyPlayer().getUnits()) {
      UnitType type = unit.getType();
      this.unitCount.incrementCount(type, 1);
      Map<Location, Integer> unitMap = this.data.get(type);
      if (unitMap == null) {
        unitMap = new HashMap<>();
        this.data.put(type, unitMap);
      }

      Location location = unit.getLocation();
      if (location == null) {
        logger.warning("Unit has null location: " + unit);
      } else if (location.getSettlement() != null) {
        location = location.getSettlement();
      } else if (unit.isInEurope()) {
        location = getMyPlayer().getEurope();
      } else if (location.getTile() != null) {
        location = location.getTile();
      }
      Integer count = unitMap.get(location);
      if (count == null) {
        unitMap.put(location, 1);
      } else {
        unitMap.put(location, count + 1);
      }
    }

    this.colonies = freeColClient.getMySortedColonies();

    DefaultListModel<LabourUnitPanel> model = new DefaultListModel<>();
    for (UnitType unitType : getSpecification().getUnitTypeList()) {
      if (unitType.isPerson() && unitType.isAvailableTo(getMyPlayer())) {
        int count = this.unitCount.getCount(unitType);
        model.addElement(new LabourUnitPanel(unitType, count));
      }
    }
    Action selectAction =
        new AbstractAction() {
          @Override
          public void actionPerformed(ActionEvent e) {
            showDetails();
          }
        };
    Action quitAction =
        new AbstractAction() {
          @Override
          public void actionPerformed(ActionEvent e) {
            getGUI().removeFromCanvas(ReportLabourPanel.this);
          }
        };

    // Add all the components
    this.panelList = new JList<>(model);
    this.panelList.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "select");
    this.panelList.getActionMap().put("select", selectAction);
    this.panelList.getInputMap().put(KeyStroke.getKeyStroke("ESCAPE"), "quit");
    this.panelList.getActionMap().put("quit", quitAction);
    this.panelList.addMouseListener(
        new MouseAdapter() {
          @Override
          public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
              showDetails();
            }
          }
        });
    this.panelList.setOpaque(false);
    this.panelList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    this.panelList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
    this.panelList.setVisibleRowCount(-1);
    this.panelList.setCellRenderer(new LabourUnitPanelRenderer());

    this.scrollPane.setViewportView(this.panelList);
  }
  /** {@inheritDoc} */
  @Override
  public Mission doMission(LogBuilder lb) {
    lb.add(tag);
    String reason = invalidReason();
    if (reason != null) return lbFail(lb, false, reason);

    final AIUnit aiUnit = getAIUnit();
    final Unit unit = getUnit();
    final IndianSettlement is = unit.getHomeIndianSettlement();
    Direction d;

    while (!this.demanded) {
      Unit.MoveType mt = travelToTarget(getTarget(), null, lb);
      switch (mt) {
        case MOVE_HIGH_SEAS:
        case MOVE_NO_MOVES:
        case MOVE_ILLEGAL:
          return lbWait(lb);

        case MOVE_NO_REPAIR:
          return lbFail(lb, false, AIUNITDIED);

        case MOVE_NO_TILE:
          return this;

        case ATTACK_SETTLEMENT: // Arrived?
          d = unit.getTile().getDirection(getTarget().getTile());
          if (d != null) break; // Yes, arrived at target
          // Fall through
        case ATTACK_UNIT: // Something is blocking our path
          Location blocker = resolveBlockage(aiUnit, getTarget());
          if (blocker == null) {
            moveRandomly(tag, null);
            continue;
          }
          d = unit.getTile().getDirection(blocker.getTile());
          if (AIMessage.askAttack(aiUnit, d)) {
            return lbAttack(lb, blocker);
          }
          continue;

        default:
          return lbMove(lb, mt);
      }

      // Load the goods.
      lbAt(lb);
      Colony colony = (Colony) getTarget();
      Player enemy = colony.getOwner();
      Goods goods = selectGoods(colony);
      GoodsType type = (goods == null) ? null : goods.getType();
      int amount = (goods == null) ? 0 : goods.getAmount();
      if (goods == null) {
        if (!enemy.checkGold(1)) {
          return lbDone(lb, false, "empty handed");
        }
        amount = enemy.getGold() / 20;
        if (amount == 0) amount = enemy.getGold();
      }
      this.demanded = AIMessage.askIndianDemand(aiUnit, colony, type, amount);
      if (this.demanded && (goods == null || hasTribute())) {
        if (goods == null) {
          return lbDone(lb, false, "accepted tribute ", amount, " gold");
        }
        lb.add(", accepted tribute ", goods);
        return lbRetarget(lb);
      }

      // Consider attacking if not content.
      int unitTension = (is == null) ? 0 : is.getAlarm(enemy).getValue();
      int tension = Math.max(unitTension, unit.getOwner().getTension(enemy).getValue());
      d = unit.getTile().getDirection(colony.getTile());
      if (tension >= Tension.Level.CONTENT.getLimit() && d != null) {
        if (AIMessage.askAttack(aiUnit, d)) lbAttack(lb, colony);
      }
      return lbDone(lb, false, "refused at ", colony);
    }

    // Take the goods home
    for (; ; ) {
      Unit.MoveType mt =
          travelToTarget(getTarget(), CostDeciders.avoidSettlementsAndBlockingUnits(), lb);
      switch (mt) {
        case MOVE: // Arrived
          break;

        case MOVE_HIGH_SEAS:
        case MOVE_NO_MOVES:
        case MOVE_ILLEGAL:
          return lbWait(lb);

        case MOVE_NO_REPAIR:
          return lbFail(lb, false, AIUNITDIED);

        case MOVE_NO_TILE:
          return this;

        default:
          return lbMove(lb, mt);
      }

      // Unload the goods
      lbAt(lb);
      GoodsContainer container = unit.getGoodsContainer();
      for (Goods goods : container.getCompactGoods()) {
        Goods tribute = container.removeGoods(goods.getType());
        is.addGoods(tribute);
      }
      return lbDone(lb, false, "unloaded tribute");
    }
  }