Example #1
0
 protected boolean chooseRandomTarget(Target target, Ability source, Game game) {
   Set<UUID> possibleTargets =
       target.possibleTargets(source == null ? null : source.getSourceId(), playerId, game);
   if (possibleTargets.isEmpty()) return false;
   if (!target.isRequired()) {
     if (rnd.nextInt(possibleTargets.size() + 1) == 0) {
       return false;
     }
   }
   if (possibleTargets.size() == 1) {
     target.addTarget(possibleTargets.iterator().next(), source, game);
     return true;
   }
   Iterator<UUID> it = possibleTargets.iterator();
   int targetNum = rnd.nextInt(possibleTargets.size());
   UUID targetId = it.next();
   for (int i = 0; i < targetNum; i++) {
     targetId = it.next();
   }
   target.addTarget(targetId, source, game);
   return true;
 }
Example #2
0
  /**
   * @param game
   * @param playerId
   * @param forceChange - does only work for targets with maximal one targetId
   * @param onlyOneTarget - 114.6b one target must be changed to another target
   * @return
   */
  public boolean chooseNewTargets(
      Game game, UUID playerId, boolean forceChange, boolean onlyOneTarget) {
    Player player = game.getPlayer(playerId);
    if (player != null) {
      for (SpellAbility spellAbility : spellAbilities) {
        for (Target target : spellAbility.getTargets()) {
          Target newTarget = target.copy();
          newTarget.clearChosen();
          for (UUID targetId : target.getTargets()) {
            MageObject object = game.getObject(targetId);
            String name = null;
            if (object == null) {
              Player targetPlayer = game.getPlayer(targetId);
              if (targetPlayer != null) {
                name = targetPlayer.getName();
              }
            } else {
              name = object.getName();
            }
            if (name != null
                && (forceChange
                    || player.chooseUse(
                        spellAbility.getEffects().get(0).getOutcome(),
                        "Change target from " + name + "?",
                        game))) {
              if (forceChange
                  && target.possibleTargets(this.getSourceId(), playerId, game).size() > 1) {
                int iteration = 0;
                do {
                  if (iteration > 0) {
                    game.informPlayer(
                        player,
                        "You may only select exactly one target that must be different from the origin target!");
                  }
                  iteration++;
                  newTarget.clearChosen();
                  player.chooseTarget(
                      spellAbility.getEffects().get(0).getOutcome(), newTarget, spellAbility, game);
                } while (targetId.equals(newTarget.getFirstTarget())
                    || newTarget.getTargets().size() != 1);

              } else {
                if (!player.chooseTarget(
                    spellAbility.getEffects().get(0).getOutcome(), newTarget, spellAbility, game)) {
                  newTarget.addTarget(targetId, spellAbility, game, false);
                }
              }
            } else {
              newTarget.addTarget(targetId, spellAbility, game, false);
            }
          }
          target.clearChosen();
          for (UUID newTargetId : newTarget.getTargets()) {
            target.addTarget(newTargetId, spellAbility, game, false);
          }
        }
      }
      return true;
    }
    return false;
  }