@Override
 public boolean apply(Game game, Ability source) {
   Player controller = game.getPlayer(source.getControllerId());
   if (controller != null && source.getTargets().size() > 0) {
     Target multiTarget = source.getTargets().get(0);
     for (UUID target : multiTarget.getTargets()) {
       Permanent permanent = game.getPermanent(target);
       if (permanent != null) {
         permanent.addCounters(
             CounterType.P1P1.createInstance(multiTarget.getTargetAmount(target)), game);
         game.informPlayers(
             new StringBuilder(controller.getLogName())
                 .append(" puts ")
                 .append(multiTarget.getTargetAmount(target))
                 .append(" ")
                 .append(CounterType.P1P1.getName().toLowerCase())
                 .append(" counter on ")
                 .append(permanent.getName())
                 .toString());
       }
     }
     return true;
   }
   return false;
 }
Example #2
0
  private void exileCards(Player player, int count, Ability source, Game game) {
    int amount =
        Math.min(
            count,
            player.getHand().size()
                + game.getBattlefield().getAllActivePermanents(player.getId()).size());

    while (amount > 0) {
      Target target = new TargetControlledPermanent(0, 1, filter, true);
      if (target.canChoose(player.getId(), game)
          && player.choose(Outcome.Exile, target, source.getSourceId(), game)) {

        for (UUID targetId : target.getTargets()) {
          Permanent chosen = game.getPermanent(targetId);
          if (chosen != null) {
            chosen.moveToExile(source.getId(), "Descent into Madness", source.getSourceId(), game);
            amount--;
          }
        }
      }

      if (amount > 0) {
        TargetCard targetInHand = new TargetCard(Zone.HAND, filterInHand);
        if (targetInHand.canChoose(player.getId(), game)
            && player.choose(Outcome.Exile, player.getHand(), targetInHand, game)) {

          Card card = player.getHand().get(targetInHand.getFirstTarget(), game);
          if (card != null) {
            card.moveToExile(source.getId(), "Descent into Madness", source.getSourceId(), game);
            amount--;
          }
        }
      }
    }
  }
Example #3
0
 @Override
 public boolean apply(Game game, Ability source, Ability abilityToModify) {
   // Target target = abilityToModify.getTargets().get(0);
   for (Target target : abilityToModify.getTargets()) {
     if (target.getMaxNumberOfTargets() == Integer.MAX_VALUE) {
       int additionalTargets = target.getTargets().size() - 1;
       for (int i = 0; i < additionalTargets; i++) {
         abilityToModify.getManaCostsToPay().add(striveCosts.copy());
       }
       return true;
     }
   }
   return false;
 }
Example #4
0
 @Override
 public boolean applies(Ability abilityToModify, Ability source, Game game) {
   if (abilityToModify instanceof SpellAbility) {
     if (game.getOpponents(source.getControllerId()).contains(abilityToModify.getControllerId())) {
       for (Target target : abilityToModify.getTargets()) {
         for (UUID targetUUID : target.getTargets()) {
           if (targetUUID.equals(source.getSourceId())) {
             return true;
           }
         }
       }
     }
   }
   return false;
 }
 @Override
 public boolean apply(MageObject input, Game game) {
   Spell spell = game.getStack().getSpell(input.getId());
   if (spell != null) {
     Targets spellTargets = spell.getSpellAbility().getTargets();
     int numberOfTargets = 0;
     for (Target target : spellTargets) {
       numberOfTargets += target.getTargets().size();
     }
     if (numberOfTargets == targets) {
       return true;
     }
   }
   return false;
 }
Example #6
0
  @Override
  public boolean apply(ObjectPlayer<StackObject> input, Game game) {
    UUID controllerId = input.getPlayerId();
    if (controllerId == null) {
      return false;
    }

    for (Target target : input.getObject().getStackAbility().getTargets()) {
      for (UUID targetId : target.getTargets()) {
        if (controllerId.equals(targetId)) {
          return true;
        }
      }
    }
    return false;
  }
Example #7
0
 @Override
 public boolean applies(Ability abilityToModify, Ability source, Game game) {
   if (abilityToModify instanceof SpellAbility) {
     if (source.getControllerId().equals(abilityToModify.getControllerId())) {
       for (UUID modeId : abilityToModify.getModes().getSelectedModes()) {
         Mode mode = abilityToModify.getModes().get(modeId);
         for (Target target : mode.getTargets()) {
           for (UUID targetUUID : target.getTargets()) {
             Permanent enchantment = game.getPermanent(source.getSourceId());
             UUID attachedTo = enchantment.getAttachedTo();
             if (targetUUID.equals(attachedTo)) {
               return true;
             }
           }
         }
       }
     }
   }
   return false;
 }
Example #8
0
 @Override
 public boolean apply(Game game, Ability source) {
   if (source.getTargets().size() > 0) {
     Target multiTarget = source.getTargets().get(0);
     for (UUID target : multiTarget.getTargets()) {
       Permanent permanent = game.getPermanent(target);
       if (permanent != null) {
         permanent.damage(
             multiTarget.getTargetAmount(target), source.getSourceId(), game, true, false);
       } else {
         Player player = game.getPlayer(target);
         if (player != null) {
           player.damage(
               multiTarget.getTargetAmount(target), source.getSourceId(), game, false, true);
         }
       }
     }
   }
   return true;
 }
Example #9
0
 protected void addAbilityNode(SimulationNode parent, Ability ability, Game game) {
   Game sim = game.copy();
   sim.getStack().push(new StackAbility(ability, playerId));
   ability.activate(sim, false);
   sim.applyEffects();
   SimulationNode newNode = new SimulationNode(parent, sim, playerId);
   logger.debug(
       indent(newNode.getDepth())
           + "simulating -- node #:"
           + SimulationNode.getCount()
           + " triggered ability option");
   for (Target target : ability.getTargets()) {
     for (UUID targetId : target.getTargets()) {
       newNode.getTargets().add(targetId);
     }
   }
   for (Choice choice : ability.getChoices()) {
     newNode.getChoices().add(choice.getChoice());
   }
   parent.children.add(newNode);
 }
Example #10
0
 protected String getTargetDescriptionForLog(Targets targets, Game game) {
   StringBuilder sb =
       new StringBuilder(); // threadLocal StringBuilder can't be used because calling method
   // already uses it
   if (targets.size() > 0) {
     String usedVerb = null;
     for (Target target : targets) {
       if (!target.getTargets().isEmpty()) {
         if (!target.isNotTarget()) {
           if (usedVerb == null || usedVerb.equals(" choosing ")) {
             usedVerb = " targeting ";
             sb.append(usedVerb);
           }
         } else if (target.isNotTarget() && (usedVerb == null || usedVerb.equals(" targeting "))) {
           usedVerb = " choosing ";
           sb.append(usedVerb);
         }
         sb.append(target.getTargetedName(game));
       }
     }
   }
   return sb.toString();
 }
Example #11
0
  @Override
  public void adjustCosts(Ability ability, Game game) {
    Player player = game.getPlayer(controllerId);
    if (player == null || !(ability instanceof SpellAbility)) {
      return;
    }
    Target target = new TargetControlledCreaturePermanent(1, Integer.MAX_VALUE, filter, true);
    target.setTargetName("creatures to convoke");
    if (!target.canChoose(sourceId, controllerId, game)) {
      return;
    }
    if (player.chooseUse(Outcome.Detriment, "Convoke creatures?", game)) {
      player.chooseTarget(Outcome.Tap, target, ability, game);
      if (target.getTargets().size() > 0) {
        int adjCost = 0;
        for (UUID creatureId : target.getTargets()) {
          Permanent perm = game.getPermanent(creatureId);
          if (perm == null) {
            continue;
          }
          ManaCosts manaCostsCreature = perm.getSpellAbility().getManaCosts();
          if (manaCostsCreature != null
              && manaCostsCreature.convertedManaCost() > 0
              && perm.tap(game)) {
            Choice chooseManaType = buildChoice(manaCostsCreature, ability.getManaCostsToPay());
            if (chooseManaType.getChoices().size() > 0) {
              if (chooseManaType.getChoices().size() > 1) {
                chooseManaType.getChoices().add("Colorless");
                chooseManaType.setMessage("Choose mana color to reduce from " + perm.getName());
                while (!chooseManaType.isChosen()) {
                  player.choose(Outcome.Benefit, chooseManaType, game);
                }
              } else {
                chooseManaType.setChoice(chooseManaType.getChoices().iterator().next());
              }

              ManaCosts manaCostsToReduce = new ManaCostsImpl();
              if (chooseManaType.getChoice().equals("Black")) {
                manaCostsToReduce.load("{B}");
              }
              if (chooseManaType.getChoice().equals("Blue")) {
                manaCostsToReduce.load("{U}");
              }
              if (chooseManaType.getChoice().equals("Green")) {
                manaCostsToReduce.load("{G}");
              }
              if (chooseManaType.getChoice().equals("White")) {
                manaCostsToReduce.load("{W}");
              }
              if (chooseManaType.getChoice().equals("Red")) {
                manaCostsToReduce.load("{R}");
              }
              if (chooseManaType.getChoice().equals("Colorless")) {
                ++adjCost;
              }
              CardUtil.adjustCost((SpellAbility) ability, manaCostsToReduce);
            } else {
              ++adjCost;
            }
          }
        }
        this.getTargets().add(target);
        CardUtil.adjustCost((SpellAbility) ability, adjCost);
      }
    }
  }
Example #12
0
 @Override
 public boolean apply(Game game, Ability source) {
   Player controller = game.getPlayer(source.getControllerId());
   if (controller != null) {
     // Each player loses a third of his or her life,
     for (UUID playerId : controller.getInRange()) {
       Player player = game.getPlayer(playerId);
       if (player != null) {
         int lifeToLose = (int) Math.ceil(player.getLife() / 3.0);
         player.loseLife(lifeToLose, game);
       }
     }
     // then discards a third of the cards in his or her hand,
     for (UUID playerId : controller.getInRange()) {
       Player player = game.getPlayer(playerId);
       if (player != null) {
         int cardsToDiscard = (int) Math.ceil(player.getHand().size() / 3.0);
         if (cardsToDiscard > 0) {
           player.discard(cardsToDiscard, false, source, game);
         }
       }
     }
     // then sacrifices a third of the creatures he or she controls,
     for (UUID playerId : controller.getInRange()) {
       Player player = game.getPlayer(playerId);
       if (player != null) {
         FilterControlledCreaturePermanent filter = new FilterControlledCreaturePermanent();
         int creaturesToSacrifice =
             (int)
                 Math.ceil(
                     game.getBattlefield()
                             .count(filter, source.getSourceId(), player.getId(), game)
                         / 3.0);
         if (creaturesToSacrifice > 0) {
           Target target =
               new TargetControlledCreaturePermanent(
                   creaturesToSacrifice, creaturesToSacrifice, filter, true);
           target.chooseTarget(Outcome.Sacrifice, playerId, source, game);
           for (UUID permanentId : target.getTargets()) {
             Permanent permanent = game.getPermanent(permanentId);
             if (permanent != null) {
               permanent.sacrifice(source.getSourceId(), game);
             }
           }
         }
       }
     }
     // then sacrifices a third of the lands he or she controls.
     for (UUID playerId : controller.getInRange()) {
       Player player = game.getPlayer(playerId);
       if (player != null) {
         FilterControlledLandPermanent filter = new FilterControlledLandPermanent();
         int landsToSacrifice =
             (int)
                 Math.ceil(
                     game.getBattlefield()
                             .count(filter, source.getSourceId(), player.getId(), game)
                         / 3.0);
         if (landsToSacrifice > 0) {
           Target target =
               new TargetControlledPermanent(landsToSacrifice, landsToSacrifice, filter, true);
           target.chooseTarget(Outcome.Sacrifice, playerId, source, game);
           for (UUID permanentId : target.getTargets()) {
             Permanent permanent = game.getPermanent(permanentId);
             if (permanent != null) {
               permanent.sacrifice(source.getSourceId(), game);
             }
           }
         }
       }
     }
     return true;
   }
   return false;
 }
Example #13
0
  public String getValue(boolean useHidden, Game game) {
    StringBuilder sb = new StringBuilder(1024);

    sb.append(turnNum)
        .append(turn.getPhaseType())
        .append(turn.getStepType())
        .append(activePlayerId)
        .append(priorityPlayerId);

    for (Player player : players.values()) {
      sb.append("player").append(player.isPassed()).append(player.getLife()).append("hand");
      if (useHidden) {
        sb.append(player.getHand());
      } else {
        sb.append(player.getHand().size());
      }
      sb.append("library").append(player.getLibrary().size());
      sb.append("graveyard");
      for (Card card : player.getGraveyard().getCards(game)) {
        sb.append(card.getName());
      }
    }

    sb.append("permanents");
    List<String> perms = new ArrayList<String>();
    for (Permanent permanent : battlefield.getAllPermanents()) {
      perms.add(permanent.getValue());
    }
    Collections.sort(perms);
    sb.append(perms);

    sb.append("spells");
    for (StackObject spell : stack) {
      sb.append(spell.getControllerId()).append(spell.getName());
      sb.append(spell.getStackAbility().toString());
      for (Mode mode : spell.getStackAbility().getModes().values()) {
        if (!mode.getTargets().isEmpty()) {
          sb.append("targets");
          for (Target target : mode.getTargets()) {
            sb.append(target.getTargets());
          }
        }
        if (!mode.getChoices().isEmpty()) {
          sb.append("choices");
          for (Choice choice : mode.getChoices()) {
            sb.append(choice.getChoice());
          }
        }
      }
    }

    for (ExileZone zone : exile.getExileZones()) {
      sb.append("exile").append(zone.getName()).append(zone);
    }

    sb.append("combat");
    for (CombatGroup group : combat.getGroups()) {
      sb.append(group.getDefenderId()).append(group.getAttackers()).append(group.getBlockers());
    }

    return sb.toString();
  }
Example #14
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;
  }