Exemplo n.º 1
0
  private Card chooseCardOnStack(SpellAbility sa, Player ai, Game game) {
    for (SpellAbilityStackInstance si : game.getStack()) {
      final Card source = si.getSourceCard();
      final SpellAbility abilityOnStack = si.getSpellAbility(true);

      if (sa.hasParam("Choices")
          && !abilityOnStack.getHostCard().isValid(sa.getParam("Choices"), ai, sa.getHostCard())) {
        continue;
      }
      final ApiType threatApi = abilityOnStack.getApi();
      if (threatApi != ApiType.DealDamage && threatApi != ApiType.DamageAll) {
        continue;
      }

      List<? extends GameObject> objects = getTargets(abilityOnStack);

      if (!abilityOnStack.usesTargeting()
          && !abilityOnStack.hasParam("Defined")
          && abilityOnStack.hasParam("ValidPlayers"))
        objects =
            AbilityUtils.getDefinedPlayers(
                source, abilityOnStack.getParam("ValidPlayers"), abilityOnStack);

      if (!objects.contains(ai) || abilityOnStack.hasParam("NoPrevention")) {
        continue;
      }
      int dmg =
          AbilityUtils.calculateAmount(source, abilityOnStack.getParam("NumDmg"), abilityOnStack);
      if (ComputerUtilCombat.predictDamageTo(ai, dmg, source, false) <= 0) {
        continue;
      }
      return source;
    }
    return null;
  }
Exemplo n.º 2
0
  @Override
  public void resolve(SpellAbility sa) {
    CardCollectionView tgtCards;
    final Game game = sa.getActivatingPlayer().getGame();
    final Card source = sa.getHostCard();
    final boolean phaseInOrOut = sa.hasParam("PhaseInOrOutAll");

    if (sa.hasParam("AllValid")) {
      if (phaseInOrOut) {
        tgtCards = game.getCardsIncludePhasingIn(ZoneType.Battlefield);
      } else {
        tgtCards = game.getCardsIn(ZoneType.Battlefield);
      }
      tgtCards = AbilityUtils.filterListByType(tgtCards, sa.getParam("AllValid"), sa);
    } else if (sa.hasParam("Defined")) {
      tgtCards = AbilityUtils.getDefinedCards(source, sa.getParam("Defined"), sa);
    } else {
      tgtCards = getTargetCards(sa);
    }
    if (phaseInOrOut) { // Time and Tide
      for (final Card tgtC : tgtCards) {
        tgtC.phase();
      }
    } else { // just phase out
      for (final Card tgtC : tgtCards) {
        if (!tgtC.isPhasedOut()) {
          tgtC.phase();
        }
      }
    }
  }
Exemplo n.º 3
0
  @Override
  public boolean chkAIDrawback(SpellAbility sa, Player ai) {

    final Card source = sa.getHostCard();

    final String numDefense = sa.hasParam("NumDef") ? sa.getParam("NumDef") : "";
    final String numAttack = sa.hasParam("NumAtt") ? sa.getParam("NumAtt") : "";

    int defense;
    if (numDefense.contains("X") && source.getSVar("X").equals("Count$xPaid")) {
      defense = Integer.parseInt(source.getSVar("PayX"));
    } else {
      defense = AbilityUtils.calculateAmount(sa.getHostCard(), numDefense, sa);
    }

    int attack;
    if (numAttack.contains("X") && source.getSVar("X").equals("Count$xPaid")) {
      if (source.getSVar("PayX").equals("")) {
        // X is not set yet
        final int xPay = ComputerUtilMana.determineLeftoverMana(sa.getRootAbility(), ai);
        source.setSVar("PayX", Integer.toString(xPay));
        attack = xPay;
      } else {
        attack = Integer.parseInt(source.getSVar("PayX"));
      }
    } else {
      attack = AbilityUtils.calculateAmount(sa.getHostCard(), numAttack, sa);
    }

    if ((sa.getTargetRestrictions() == null) || !sa.getTargetRestrictions().doesTarget()) {
      if (source.isCreature()) {
        if (!source.hasKeyword("Indestructible")
            && ((source.getNetDefense() + defense) <= source.getDamage())) {
          return false;
        }
        if ((source.getNetDefense() + defense) <= 0) {
          return false;
        }
      }
    } else {
      // Targeted
      if (!this.pumpTgtAI(ai, sa, defense, attack, false)) {
        return false;
      }
    }

    return true;
  } // pumpDrawbackAI()
Exemplo n.º 4
0
  @Override
  public PaymentDecision visit(CostGainControl cost) {
    if (cost.payCostFromSource()) return PaymentDecision.card(source);

    Integer c = cost.convertAmount();
    if (c == null) {
      c = AbilityUtils.calculateAmount(source, cost.getAmount(), ability);
    }

    final List<Card> typeList =
        CardLists.getValidCards(
            player.getGame().getCardsIn(ZoneType.Battlefield),
            cost.getType().split(";"),
            player,
            source);

    if (typeList.size() < c) {
      return null;
    }

    CardLists.sortByPowerAsc(typeList);
    final List<Card> res = new ArrayList<Card>();

    for (int i = 0; i < c; i++) {
      res.add(typeList.get(i));
    }
    return res.isEmpty() ? null : PaymentDecision.card(res);
  }
Exemplo n.º 5
0
  @Override
  public PaymentDecision visit(CostExiledMoveToGrave cost) {
    Integer c = cost.convertAmount();
    List<Card> chosen = new ArrayList<Card>();

    if (c == null) {
      c = AbilityUtils.calculateAmount(source, cost.getAmount(), ability);
    }

    List<Card> typeList = player.getGame().getCardsIn(ZoneType.Exile);

    typeList = CardLists.getValidCards(typeList, cost.getType().split(";"), player, source);

    if (typeList.size() < c) {
      return null;
    }

    CardLists.sortByPowerAsc(typeList);
    Collections.reverse(typeList);

    for (int i = 0; i < c; i++) {
      chosen.add(typeList.get(i));
    }

    return chosen.isEmpty() ? null : PaymentDecision.card(chosen);
  }
Exemplo n.º 6
0
  @Override
  public PaymentDecision visit(CostUntapType cost) {
    final String amount = cost.getAmount();
    Integer c = cost.convertAmount();
    if (c == null) {
      final String sVar = ability.getSVar(amount);
      if (sVar.equals("XChoice")) {
        List<Card> typeList = player.getGame().getCardsIn(ZoneType.Battlefield);
        typeList =
            CardLists.getValidCards(
                typeList, cost.getType().split(";"), player, ability.getHostCard());
        if (!cost.canUntapSource) {
          typeList.remove(source);
        }
        typeList = CardLists.filter(typeList, Presets.TAPPED);
        c = typeList.size();
        source.setSVar("ChosenX", "Number$" + Integer.toString(c));
      } else {
        c = AbilityUtils.calculateAmount(source, amount, ability);
      }
    }

    List<Card> list =
        ComputerUtil.chooseUntapType(player, cost.getType(), source, cost.canUntapSource, c);

    if (list == null) {
      System.out.println("Couldn't find a valid card to untap for: " + source.getName());
      return null;
    }

    return PaymentDecision.card(list);
  }
  /*
   * (non-Javadoc)
   *
   * @see
   * forge.card.cost.CostPart#canPay(forge.card.spellability.SpellAbility,
   * forge.Card, forge.Player, forge.card.cost.Cost)
   */
  @Override
  public final boolean canPay(final SpellAbility ability) {
    final Player activator = ability.getActivatingPlayer();
    final Card source = ability.getHostCard();
    CardCollectionView validCards = activator.getCardsIn(ZoneType.Battlefield);
    validCards = CardLists.getValidCards(validCards, this.getType().split(";"), activator, source);
    validCards =
        CardLists.filter(
            validCards,
            new Predicate<Card>() {
              @Override
              public boolean apply(final Card c) {
                return c.hasCounters();
              }
            });
    if (validCards.isEmpty()) {
      return false;
    }
    Integer i = this.convertAmount();

    if (i == null) {
      i = AbilityUtils.calculateAmount(source, this.getAmount(), ability);
    }
    int allCounters = 0;
    for (Card c : validCards) {
      final Map<CounterType, Integer> tgtCounters = c.getCounters();
      for (Integer value : tgtCounters.values()) {
        allCounters += value;
      }
    }

    return i <= allCounters;
  }
Exemplo n.º 8
0
  @Override
  protected boolean canPlayAI(Player aiPlayer, SpellAbility sa) {
    // This still needs to be fleshed out
    final TargetRestrictions tgt = sa.getTargetRestrictions();
    final Card source = sa.getHostCard();

    final Random r = MyRandom.getRandom();
    boolean randomReturn = r.nextFloat() <= Math.pow(.6667, sa.getActivationsThisTurn());

    List<Card> tgtCards;
    if (tgt == null) {
      tgtCards = AbilityUtils.getDefinedCards(source, sa.getParam("Defined"), sa);
      if (tgtCards.contains(source)) {
        // Protect it from something
      } else {
        // Card def = tgtCards.get(0);
        // Phase this out if it might attack me, or before it can be
        // declared as a blocker
      }

      return false;
    } else {
      if (!phasesPrefTargeting(tgt, sa, false)) {
        return false;
      }
    }

    return randomReturn;
  }
Exemplo n.º 9
0
  @Override
  public PaymentDecision visit(CostSacrifice cost) {
    if (cost.payCostFromSource()) {
      return PaymentDecision.card(source);
    }
    if (cost.getAmount().equals("All")) {
      /*List<Card> typeList = new ArrayList<Card>(activator.getCardsIn(ZoneType.Battlefield));
      typeList = CardLists.getValidCards(typeList, cost.getType().split(";"), activator, source);
      if (activator.hasKeyword("You can't sacrifice creatures to cast spells or activate abilities.")) {
          typeList = CardLists.getNotType(typeList, "Creature");
      }*/
      // Does the AI want to use Sacrifice All?
      return null;
    }

    Integer c = cost.convertAmount();
    if (c == null) {
      if (ability.getSVar(cost.getAmount()).equals("XChoice")) {
        return null;
      }

      c = AbilityUtils.calculateAmount(source, cost.getAmount(), ability);
    }
    List<Card> list =
        ComputerUtil.chooseSacrificeType(
            player, cost.getType(), source, ability.getTargetCard(), c);
    return PaymentDecision.card(list);
  }
Exemplo n.º 10
0
  @Override
  public PaymentDecision visit(CostReveal cost) {

    final String type = cost.getType();
    List<Card> hand = new ArrayList<Card>(player.getCardsIn(ZoneType.Hand));

    if (cost.payCostFromSource()) {
      if (!hand.contains(source)) {
        return null;
      }
      return PaymentDecision.card(source);
    }

    if (cost.getType().equals("Hand"))
      return PaymentDecision.card(player.getCardsIn(ZoneType.Hand));

    if (cost.getType().equals("SameColor")) {
      return null;
    }

    hand = CardLists.getValidCards(hand, type.split(";"), player, source);
    Integer c = cost.convertAmount();
    if (c == null) {
      final String sVar = ability.getSVar(cost.getAmount());
      if (sVar.equals("XChoice")) {
        c = hand.size();
      } else {
        c = AbilityUtils.calculateAmount(source, cost.getAmount(), ability);
      }
    }

    final AiController aic = ((PlayerControllerAi) player.getController()).getAi();
    return PaymentDecision.card(aic.getCardsToDiscard(c, type.split(";"), ability));
  }
Exemplo n.º 11
0
  @Override
  public PaymentDecision visit(CostTapType cost) {
    final String amount = cost.getAmount();
    Integer c = cost.convertAmount();
    if (c == null) {
      final String sVar = ability.getSVar(amount);
      if (sVar.equals("XChoice")) {
        List<Card> typeList =
            CardLists.getValidCards(
                player.getCardsIn(ZoneType.Battlefield),
                cost.getType().split(";"),
                ability.getActivatingPlayer(),
                ability.getHostCard());
        typeList = CardLists.filter(typeList, Presets.UNTAPPED);
        c = typeList.size();
        source.setSVar("ChosenX", "Number$" + Integer.toString(c));
      } else {
        c = AbilityUtils.calculateAmount(source, amount, ability);
      }
    }
    if (cost.getType().contains("sharesCreatureTypeWith")
        || cost.getType().contains("withTotalPowerGE")) {
      return null;
    }

    List<Card> totap =
        ComputerUtil.chooseTapType(player, cost.getType(), source, !cost.canTapSource, c);

    if (totap == null) {
      System.out.println("Couldn't find a valid card to tap for: " + source.getName());
      return null;
    }

    return PaymentDecision.card(totap);
  }
Exemplo n.º 12
0
  @Override
  public void resolve(SpellAbility sa) {
    final int numTurns =
        AbilityUtils.calculateAmount(sa.getHostCard(), sa.getParam("NumTurns"), sa);

    List<Player> tgtPlayers = getTargetPlayers(sa);

    for (final Player p : tgtPlayers) {
      if ((sa.getTargetRestrictions() == null) || p.canBeTargetedBy(sa)) {
        for (int i = 0; i < numTurns; i++) {
          ExtraTurn extra = p.getGame().getPhaseHandler().addExtraTurn(p);
          if (sa.hasParam("LoseAtEndStep")) {
            extra.setLoseAtEndStep(true);
          }
          if (sa.hasParam("SkipUntap")) {
            extra.setSkipUntap(true);
          }
          if (sa.hasParam("NoSchemes")) {
            extra.setCantSetSchemesInMotion(true);
          }
          if (sa.hasParam("ShowMessage")) {
            p.getGame().getAction().nofityOfValue(sa, p, p + " takes an extra turn.", null);
          }
        }
      }
    }
  }
Exemplo n.º 13
0
  @Override
  public PaymentDecision visit(CostExile cost) {
    if (cost.payCostFromSource()) {
      return PaymentDecision.card(source);
    }

    if (cost.getType().equals("All")) {
      return PaymentDecision.card(player.getCardsIn(cost.getFrom()));
    } else if (cost.getType().contains("FromTopGrave")) {
      return null;
    }

    Integer c = cost.convertAmount();
    if (c == null) {
      final String sVar = ability.getSVar(cost.getAmount());
      // Generalize cost
      if (sVar.equals("XChoice")) {
        return null;
      }
      c = AbilityUtils.calculateAmount(source, cost.getAmount(), ability);
    }

    if (cost.getFrom().equals(ZoneType.Library)) {
      return PaymentDecision.card(player.getCardsIn(ZoneType.Library, c));
    } else if (cost.sameZone) {
      // TODO Determine exile from same zone for AI
      return null;
    } else {
      List<Card> chosen =
          ComputerUtil.chooseExileFrom(
              player, cost.getFrom(), cost.getType(), source, ability.getTargetCard(), c);
      return null == chosen ? null : PaymentDecision.card(chosen);
    }
  }
Exemplo n.º 14
0
  @Override
  public PaymentDecision visit(CostAddMana cost) {
    Integer c = cost.convertAmount();

    if (c == null) {
      c = AbilityUtils.calculateAmount(source, cost.getAmount(), ability);
    }

    return PaymentDecision.number(c);
  }
Exemplo n.º 15
0
  @Override
  protected boolean doTriggerAINoCost(Player ai, SpellAbility sa, boolean mandatory) {
    final Card source = sa.getHostCard();
    final String numDefense = sa.hasParam("NumDef") ? sa.getParam("NumDef") : "";
    final String numAttack = sa.hasParam("NumAtt") ? sa.getParam("NumAtt") : "";

    int defense;
    if (numDefense.contains("X") && source.getSVar("X").equals("Count$xPaid")) {
      // Set PayX here to maximum value.
      final int xPay = ComputerUtilMana.determineLeftoverMana(sa, ai);
      source.setSVar("PayX", Integer.toString(xPay));
      defense = xPay;
    } else {
      defense = AbilityUtils.calculateAmount(sa.getHostCard(), numDefense, sa);
    }

    int attack;
    if (numAttack.contains("X") && source.getSVar("X").equals("Count$xPaid")) {
      // Set PayX here to maximum value.
      final String toPay = source.getSVar("PayX");

      if (toPay.equals("")) {
        final int xPay = ComputerUtilMana.determineLeftoverMana(sa, ai);
        source.setSVar("PayX", Integer.toString(xPay));
        attack = xPay;
      } else {
        attack = Integer.parseInt(toPay);
      }
    } else {
      attack = AbilityUtils.calculateAmount(sa.getHostCard(), numAttack, sa);
    }

    if (sa.getTargetRestrictions() == null) {
      if (mandatory) {
        return true;
      }
    } else {
      return this.pumpTgtAI(ai, sa, defense, attack, mandatory);
    }

    return true;
  } // pumpTriggerAI
Exemplo n.º 16
0
  @Override
  public PaymentDecision visit(CostRemoveCounter cost) {
    final String amount = cost.getAmount();
    Integer c = cost.convertAmount();
    final String type = cost.getType();

    if (c == null) {
      final String sVar = ability.getSVar(amount);
      if (sVar.equals("XChoice")) {
        c = AbilityUtils.calculateAmount(source, "ChosenX", ability);
      } else if (amount.equals("All")) {
        c = source.getCounters(cost.counter);
      } else {
        c = AbilityUtils.calculateAmount(source, amount, ability);
      }
    }

    if (!cost.payCostFromSource()) {
      List<Card> typeList;
      if (type.equals("OriginalHost")) {
        typeList = Lists.newArrayList(ability.getOriginalHost());
      } else {
        typeList =
            CardLists.getValidCards(player.getCardsIn(cost.zone), type.split(";"), player, source);
      }
      for (Card card : typeList) {
        if (card.getCounters(cost.counter) >= c) {
          return PaymentDecision.card(card, c);
        }
      }
      return null;
    }

    if (c > source.getCounters(cost.counter)) {
      System.out.println("Not enough " + cost.counter + " on " + source.getName());
      return null;
    }

    return PaymentDecision.card(source, c);
  }
Exemplo n.º 17
0
  /* (non-Javadoc)
   * @see forge.card.abilityfactory.SpellAiLogic#canPlayAI(forge.game.player.Player, java.util.Map, forge.card.spellability.SpellAbility)
   */
  @Override
  protected boolean canPlayAI(Player ai, SpellAbility sa) {
    final TargetRestrictions tgt = sa.getTargetRestrictions();

    if (tgt != null && sa.canTarget(ai.getOpponent())) {
      sa.resetTargets();
      sa.getTargets().add(ai.getOpponent());
    }

    final String damage = sa.getParam("NumDmg");
    final int iDmg = AbilityUtils.calculateAmount(sa.getHostCard(), damage, sa);
    return this.shouldTgtP(ai, sa, iDmg, false);
  }
Exemplo n.º 18
0
 @Override
 public PaymentDecision visit(CostFlipCoin cost) {
   Integer c = cost.convertAmount();
   if (c == null) {
     final String sVar = ability.getSVar(cost.getAmount());
     // Generalize cost
     if (sVar.equals("XChoice")) {
       return null;
     }
     c = AbilityUtils.calculateAmount(source, cost.getAmount(), ability);
   }
   return PaymentDecision.number(c);
 }
Exemplo n.º 19
0
  @Override
  public PaymentDecision visit(CostReturn cost) {
    if (cost.payCostFromSource()) return PaymentDecision.card(source);

    Integer c = cost.convertAmount();
    if (c == null) {
      c = AbilityUtils.calculateAmount(source, cost.getAmount(), ability);
    }

    List<Card> res =
        ComputerUtil.chooseReturnType(player, cost.getType(), source, ability.getTargetCard(), c);
    return res.isEmpty() ? null : PaymentDecision.card(res);
  }
Exemplo n.º 20
0
  @Override
  public void resolve(SpellAbility sa) {
    Game game = sa.getActivatingPlayer().getGame();

    for (Player p : game.getPlayers()) {
      p.leaveCurrentPlane();
    }
    if (sa.hasParam("Defined")) {
      CardCollectionView destinations =
          AbilityUtils.getDefinedCards(sa.getHostCard(), sa.getParam("Defined"), sa);
      sa.getActivatingPlayer().planeswalkTo(destinations);
    } else {
      sa.getActivatingPlayer().planeswalk();
    }
  }
 @Override
 public boolean payAsDecided(Player ai, PaymentDecision decision, SpellAbility ability) {
   final String amount = this.getAmount();
   final Card source = ability.getHostCard();
   Integer c = this.convertAmount();
   if (c == null) {
     c = AbilityUtils.calculateAmount(source, amount, ability);
   }
   Card valid = decision.cards.get(0);
   counterType = decision.ct;
   for (int i = 0; i < c; i++) {
     executePayment(ability, valid);
   }
   source.setSVar("CostCountersRemoved", Integer.toString(c));
   return true;
 }
Exemplo n.º 22
0
  /*
   * (non-Javadoc)
   *
   * @see
   * forge.card.cost.CostPart#canPay(forge.card.spellability.SpellAbility,
   * forge.Card, forge.Player, forge.card.cost.Cost)
   */
  @Override
  public final boolean canPay(final SpellAbility ability) {
    final Player activator = ability.getActivatingPlayer();
    final Card source = ability.getHostCard();
    CardCollectionView typeList = activator.getGame().getCardsIn(ZoneType.Battlefield);
    typeList = CardLists.getValidCards(typeList, this.getType().split(";"), activator, source);

    Integer amount = this.convertAmount();
    if (amount == null) {
      amount = AbilityUtils.calculateAmount(source, this.getAmount(), ability);
    }
    if (typeList.size() < amount) {
      return false;
    }
    return true;
  }
Exemplo n.º 23
0
  @Override
  public PaymentDecision visit(CostMill cost) {
    Integer c = cost.convertAmount();
    if (c == null) {
      final String sVar = ability.getSVar(cost.getAmount());
      // Generalize cost
      if (sVar.equals("XChoice")) {
        return null;
      }

      c = AbilityUtils.calculateAmount(source, cost.getAmount(), ability);
    }

    List<Card> topLib = player.getCardsIn(ZoneType.Library, c);
    return topLib.size() < c ? null : PaymentDecision.card(topLib);
  }
Exemplo n.º 24
0
 @Override
 public PaymentDecision visit(CostPayLife cost) {
   Integer c = cost.convertAmount();
   if (c == null) {
     final String sVar = ability.getSVar(cost.getAmount());
     // Generalize cost
     if (sVar.equals("XChoice")) {
       return null;
     } else {
       c = AbilityUtils.calculateAmount(source, cost.getAmount(), ability);
     }
   }
   if (!player.canPayLife(c)) {
     return null;
   }
   // activator.payLife(c, null);
   return PaymentDecision.number(c);
 }
Exemplo n.º 25
0
  @Override
  public PaymentDecision visit(CostPutCardToLib cost) {
    Integer c = cost.convertAmount();
    final Game game = player.getGame();
    List<Card> chosen = new ArrayList<Card>();
    List<Card> list;

    if (cost.isSameZone()) {
      list = new ArrayList<Card>(game.getCardsIn(cost.getFrom()));
    } else {
      list = new ArrayList<Card>(player.getCardsIn(cost.getFrom()));
    }

    if (c == null) {
      final String sVar = ability.getSVar(cost.getAmount());
      // Generalize cost
      if (sVar.equals("XChoice")) {
        return null;
      }

      c = AbilityUtils.calculateAmount(source, cost.getAmount(), ability);
    }

    list = CardLists.getValidCards(list, cost.getType().split(";"), player, source);

    if (cost.isSameZone()) {
      // Jotun Grunt
      // TODO: improve AI
      final List<Player> players = game.getPlayers();
      for (Player p : players) {
        List<Card> enoughType = CardLists.filter(list, CardPredicates.isOwner(p));
        if (enoughType.size() >= c) {
          chosen.addAll(enoughType);
          break;
        }
      }
      chosen = chosen.subList(0, c);
    } else {
      chosen =
          ComputerUtil.choosePutToLibraryFrom(
              player, cost.getFrom(), cost.getType(), source, ability.getTargetCard(), c);
    }
    return chosen.isEmpty() ? null : PaymentDecision.card(chosen);
  }
Exemplo n.º 26
0
  @Override
  protected String getStackDescription(SpellAbility sa) {
    final Card host = sa.getHostCard();
    final StringBuilder sb = new StringBuilder();
    final int numToDig = AbilityUtils.calculateAmount(host, sa.getParam("DigNum"), sa);
    final List<Player> tgtPlayers = getTargetPlayers(sa);

    sb.append(host.getController()).append(" looks at the top ");
    sb.append(Lang.nounWithAmount(numToDig, "card")).append(" of ");

    if (tgtPlayers.contains(host.getController())) {
      sb.append("his or her ");
    } else {
      for (final Player p : tgtPlayers) {
        sb.append(Lang.getPossesive(p.getName())).append(" ");
      }
    }
    sb.append("library.");
    return sb.toString();
  }
Exemplo n.º 27
0
  @Override
  public PaymentDecision visit(CostExileFromStack cost) {

    Integer c = cost.convertAmount();
    if (c == null) {
      final String sVar = ability.getSVar(cost.getAmount());
      // Generalize cost
      if (sVar.equals("XChoice")) {
        return null;
      }
      c = AbilityUtils.calculateAmount(source, cost.getAmount(), ability);
    }
    List<SpellAbility> chosen = new ArrayList<SpellAbility>();
    for (SpellAbilityStackInstance si : source.getGame().getStack()) {
      SpellAbility sp = si.getSpellAbility().getRootAbility();
      if (si.getSourceCard().isValid(cost.getType().split(";"), source.getController(), source)) {
        chosen.add(sp);
      }
    }
    return chosen.isEmpty() ? null : PaymentDecision.spellabilities(chosen);
  }
Exemplo n.º 28
0
  @Override
  public void resolve(SpellAbility sa) {
    final Card host = sa.getHostCard();
    final String[] choices = sa.getParam("Choices").split(",");
    final List<SpellAbility> abilities = new ArrayList<SpellAbility>();

    for (String s : choices) {
      abilities.add(AbilityFactory.getAbility(host.getSVar(s), host));
    }

    final List<Player> tgtPlayers = getDefinedPlayersOrTargeted(sa);
    final TargetRestrictions tgt = sa.getTargetRestrictions();

    for (final Player p : tgtPlayers) {
      if (tgt != null && !p.canBeTargetedBy(sa)) {
        continue;
      }

      int idxChosen = 0;
      String chosenName;
      if (sa.hasParam("AtRandom")) {
        idxChosen = MyRandom.getRandom().nextInt(choices.length);
        chosenName = choices[idxChosen];
      } else {
        SpellAbility saChosen =
            p.getController().chooseSingleSpellForEffect(abilities, sa, "Choose one");
        idxChosen = abilities.indexOf(saChosen);
        chosenName = choices[idxChosen];
      }
      SpellAbility chosenSA = AbilityFactory.getAbility(host.getSVar(chosenName), host);
      if (sa.hasParam("ShowChoice")) {
        p.getGame()
            .getAction()
            .nofityOfValue(sa, p, abilities.get(idxChosen).getDescription(), null);
      }
      chosenSA.setActivatingPlayer(sa.getActivatingPlayer());
      ((AbilitySub) chosenSA).setParent(sa);
      AbilityUtils.resolve(chosenSA);
    }
  }
Exemplo n.º 29
0
  @Override
  public PaymentDecision visit(CostDiscard cost) {
    final String type = cost.getType();

    final List<Card> hand = player.getCardsIn(ZoneType.Hand);
    if (type.equals("LastDrawn")) {
      if (!hand.contains(player.getLastDrawnCard())) {
        return null;
      }
      return PaymentDecision.card(player.getLastDrawnCard());
    } else if (cost.payCostFromSource()) {
      if (!hand.contains(source)) {
        return null;
      }

      return PaymentDecision.card(source);
    } else if (type.equals("Hand")) {
      return PaymentDecision.card(hand);
    }

    if (type.contains("WithSameName")) {
      return null;
    }
    Integer c = cost.convertAmount();
    if (c == null) {
      final String sVar = ability.getSVar(cost.getAmount());
      if (sVar.equals("XChoice")) {
        return null;
      }
      c = AbilityUtils.calculateAmount(source, cost.getAmount(), ability);
    }

    if (type.equals("Random")) {
      return PaymentDecision.card(CardLists.getRandomSubList(hand, c));
    } else {
      final AiController aic = ((PlayerControllerAi) player.getController()).getAi();
      return PaymentDecision.card(aic.getCardsToDiscard(c, type.split(";"), ability));
    }
  }
Exemplo n.º 30
0
  @Override
  public PaymentDecision visit(CostRemoveAnyCounter cost) {
    final String amount = cost.getAmount();
    final int c = AbilityUtils.calculateAmount(source, amount, ability);
    final String type = cost.getType();

    List<Card> typeList =
        CardLists.getValidCards(
            player.getCardsIn(ZoneType.Battlefield), type.split(";"), player, source);
    List<Card> hperms =
        CardLists.filter(
            typeList,
            new Predicate<Card>() {
              @Override
              public boolean apply(final Card crd) {
                for (final CounterType c1 : CounterType.values()) {
                  if (crd.getCounters(c1) >= c && ComputerUtil.isNegativeCounter(c1, crd)) {
                    return true;
                  }
                }
                return false;
              }
            });

    if (hperms.isEmpty()) return null;

    PaymentDecision result = PaymentDecision.card(hperms);
    Card valid = hperms.get(0);
    for (CounterType c1 : valid.getCounters().keySet()) {
      if (valid.getCounters(c1) >= c && ComputerUtil.isNegativeCounter(c1, valid)) {
        result.ct = c1;
        break;
      }
    }
    // Only find cards with enough negative counters
    // TODO: add ai for Chisei, Heart of Oceans
    return result;
  }