public void testIsTargetValidForSeekAndDestroy() {
    Game game = ServerTestHelper.startServerGame(getTestMap());
    Map map = game.getMap();
    AIMain aiMain = ServerTestHelper.getServer().getAIMain();

    // Create player and unit
    ServerPlayer incaPlayer = (ServerPlayer) game.getPlayerByNationId("model.nation.inca");
    NativeAIPlayer aiInca = (NativeAIPlayer) aiMain.getAIPlayer(incaPlayer);
    ServerPlayer dutchPlayer = (ServerPlayer) game.getPlayerByNationId("model.nation.dutch");

    Tile dutchUnitTile = map.getTile(9, 9);
    Tile braveUnitTile = map.getTile(9, 8);
    ;

    Unit brave = new ServerUnit(game, braveUnitTile, incaPlayer, braveType);
    Unit soldier = new ServerUnit(game, dutchUnitTile, dutchPlayer, veteranType);

    Player.makeContact(incaPlayer, dutchPlayer);

    assertFalse(
        "Target should NOT be valid for UnitSeekAndDestroyMission",
        aiInca.isTargetValidForSeekAndDestroy(brave, soldier.getTile()));

    incaPlayer.setTension(dutchPlayer, new Tension(Tension.Level.HATEFUL.getLimit()));
    assertTrue(
        "Target should be valid for UnitSeekAndDestroyMission",
        aiInca.isTargetValidForSeekAndDestroy(brave, soldier.getTile()));

    incaPlayer.setStance(dutchPlayer, Stance.WAR);
    dutchPlayer.setStance(incaPlayer, Stance.WAR);
    assertTrue(
        "Target should be valid for UnitSeekAndDestroyMission",
        aiInca.isTargetValidForSeekAndDestroy(brave, soldier.getTile()));
  }
Exemplo n.º 2
0
  /**
   * Handle a "buildColony"-message.
   *
   * @param server The <code>FreeColServer</code> handling the request.
   * @param player The <code>Player</code> building the colony.
   * @param connection The <code>Connection</code> the message is from.
   * @return An update <code>Element</code> defining the new colony and updating its surrounding
   *     tiles, or an error <code>Element</code> on failure.
   */
  public Element handle(FreeColServer server, Player player, Connection connection) {
    Game game = server.getGame();
    ServerPlayer serverPlayer = server.getPlayer(connection);

    Unit unit;
    try {
      unit = player.getFreeColGameObject(builderId, Unit.class);
    } catch (Exception e) {
      return DOMMessage.clientError(e.getMessage());
    }
    if (!unit.canBuildColony()) {
      return DOMMessage.clientError("Unit " + builderId + " can not build colony.");
    }

    if (colonyName == null) {
      return DOMMessage.clientError("Null colony name");
    } else if (Player.ASSIGN_SETTLEMENT_NAME.equals(colonyName)) {; // ok
    } else if (game.getSettlement(colonyName) != null) {
      return DOMMessage.clientError("Non-unique colony name " + colonyName);
    }

    Tile tile = unit.getTile();
    if (!player.canClaimToFoundSettlement(tile)) {
      return DOMMessage.clientError("Can not build colony on tile: " + tile);
    }

    // Build can proceed.
    return server.getInGameController().buildSettlement(serverPlayer, unit, colonyName);
  }
  /**
   * Invoked when a mouse button was pressed.
   *
   * @param e The MouseEvent that holds all the information.
   */
  @Override
  public void mousePressed(MouseEvent e) {
    if (!e.getComponent().isEnabled()) return;

    int me = e.getButton();
    if (e.isPopupTrigger()) me = MouseEvent.BUTTON3;
    Tile tile = canvas.convertToMapTile(e.getX(), e.getY());

    switch (me) {
      case MouseEvent.BUTTON1:
        // Record initial click point for purposes of dragging
        canvas.setDragPoint(e.getX(), e.getY());
        if (canvas.isGotoStarted()) {
          PathNode path = canvas.getGotoPath();
          if (path != null) {
            canvas.stopGoto();
            // Move the unit
            freeColClient
                .getInGameController()
                .goToTile(canvas.getActiveUnit(), path.getLastNode().getTile());
          }
        } else if (doubleClickTimer.isRunning()) {
          doubleClickTimer.stop();
        } else {
          centerX = e.getX();
          centerY = e.getY();
          doubleClickTimer.start();
        }
        canvas.requestFocus();
        break;
      case MouseEvent.BUTTON2:
        if (tile != null) {
          Unit unit = canvas.getActiveUnit();
          if (unit != null && unit.getTile() != tile) {
            PathNode dragPath = unit.findPath(tile);
            canvas.startGoto();
            canvas.setGotoPath(dragPath);
          }
        }
        break;
      case MouseEvent.BUTTON3:
        // Cancel goto if one is active
        if (canvas.isGotoStarted()) canvas.stopGoto();
        canvas.showTilePopup(tile, e.getX(), e.getY());
        break;
      default:
        break;
    }
  }
 /** {@inheritDoc} */
 @Override
 public void actionPerformed(ActionEvent ae) {
   doubleClickTimer.stop();
   Tile tile = canvas.convertToMapTile(centerX, centerY);
   if (canvas.getViewMode() == GUI.MOVE_UNITS_MODE) {
     // Clear goto order when active unit is on the tile
     Unit unit = canvas.getActiveUnit();
     if (unit != null && unit.getTile() == tile) {
       freeColClient.getInGameController().clearGotoOrders(unit);
       canvas.updateCurrentPathForActiveUnit();
     } else {
       if (tile != null && tile.hasSettlement()) {
         freeColClient.getGUI().showSettlement(tile.getSettlement());
         return;
       }
     }
   }
   freeColClient.getGUI().setSelectedTile(tile);
 }
  /** {@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");
    }
  }