Example #1
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, final SpellAbility sa) {
   Card object1 = null;
   Card object2 = null;
   final Card source = sa.getHostCard();
   final String type = sa.getParam("Type");
   if (sa.hasParam("Object")) {
     object1 = AbilityUtils.getDefinedCards(source, sa.getParam("Object"), sa).get(0);
   } else {
     object1 = source;
   }
   final ZoneType zone1 =
       sa.hasParam("Zone1") ? ZoneType.smartValueOf(sa.getParam("Zone1")) : ZoneType.Battlefield;
   final ZoneType zone2 =
       sa.hasParam("Zone2") ? ZoneType.smartValueOf(sa.getParam("Zone2")) : ZoneType.Hand;
   CardCollection list = new CardCollection(ai.getCardsIn(zone2));
   if (type != null) {
     list = CardLists.getValidCards(list, type, ai, source);
   }
   object2 = ComputerUtilCard.getBestAI(list);
   if (object1 == null
       || object2 == null
       || !object1.isInZone(zone1)
       || !object1.getOwner().equals(ai)) {
     return false;
   }
   if (type.equals("Aura")) {
     Card c = object1.getEnchantingCard();
     if (!c.canBeEnchantedBy(object2)) {
       return false;
     }
   }
   if (object2.getCMC() > object1.getCMC()) {
     return MyRandom.getRandom().nextFloat() <= Math.pow(.6667, sa.getActivationsThisTurn());
   }
   return false;
 }
  @Override
  public void resolve(SpellAbility sa) {
    final Card card = sa.getHostCard();

    AbilityManaPart abMana = sa.getManaPart();

    // Spells are not undoable
    sa.setUndoable(sa.isAbility() && sa.isUndoable());

    final List<Player> tgtPlayers = getTargetPlayers(sa);
    final TargetRestrictions tgt = sa.getTargetRestrictions();
    final boolean optional = sa.hasParam("Optional");
    final Game game = sa.getActivatingPlayer().getGame();

    if (optional
        && !sa.getActivatingPlayer()
            .getController()
            .confirmAction(sa, null, "Do you want to add mana to your mana pool?")) {
      return;
    }
    if (abMana.isComboMana()) {
      for (Player p : tgtPlayers) {
        int amount =
            sa.hasParam("Amount")
                ? AbilityUtils.calculateAmount(card, sa.getParam("Amount"), sa)
                : 1;
        if (tgt == null || p.canBeTargetedBy(sa)) {
          Player activator = sa.getActivatingPlayer();
          // String colorsNeeded = abMana.getExpressChoice();
          String[] colorsProduced = abMana.getComboColors().split(" ");

          final StringBuilder choiceString = new StringBuilder();
          ColorSet colorOptions = null;
          if (!abMana.isAnyMana()) {
            colorOptions = ColorSet.fromNames(colorsProduced);
          } else {
            colorOptions = ColorSet.fromNames(MagicColor.Constant.ONLY_COLORS);
          }
          for (int nMana = 1; nMana <= amount; nMana++) {
            String choice = "";
            byte chosenColor =
                activator.getController().chooseColor("Select Mana to Produce", sa, colorOptions);
            if (chosenColor == 0)
              throw new RuntimeException(
                  "ManaEffect::resolve() /*combo mana*/ - "
                      + activator
                      + " color mana choice is empty for "
                      + card.getName());

            choice = MagicColor.toShortString(chosenColor);
            if (nMana != 1) {
              choiceString.append(" ");
            }
            choiceString.append(choice);
          }
          game.action.nofityOfValue(sa, card, activator + " picked " + choiceString, activator);
          abMana.setExpressChoice(choiceString.toString());
        }
      }
    } else if (abMana.isAnyMana()) {
      for (Player p : tgtPlayers) {
        if (tgt == null || p.canBeTargetedBy(sa)) {
          Player act = sa.getActivatingPlayer();
          // AI color choice is set in ComputerUtils so only human players need to make a choice

          String colorsNeeded = abMana.getExpressChoice();
          String choice = "";

          ColorSet colorMenu = null;
          byte mask = 0;
          // loop through colors to make menu
          for (int nChar = 0; nChar < colorsNeeded.length(); nChar++) {
            mask |= MagicColor.fromName(colorsNeeded.charAt(nChar));
          }
          colorMenu = mask == 0 ? ColorSet.ALL_COLORS : ColorSet.fromMask(mask);
          byte val = act.getController().chooseColor("Select Mana to Produce", sa, colorMenu);
          if (0 == val) {
            throw new RuntimeException(
                "ManaEffect::resolve() /*any mana*/ - "
                    + act
                    + " color mana choice is empty for "
                    + card.getName());
          }
          choice = MagicColor.toShortString(val);

          game.action.nofityOfValue(sa, card, act + " picked " + choice, act);
          abMana.setExpressChoice(choice);
        }
      }
    } else if (abMana.isSpecialMana()) {
      for (Player p : tgtPlayers) {
        if (tgt == null || p.canBeTargetedBy(sa)) {
          String type = abMana.getOrigProduced().split("Special ")[1];

          if (type.equals("EnchantedManaCost")) {
            Card enchanted = card.getEnchantingCard();
            if (enchanted == null) continue;

            StringBuilder sb = new StringBuilder();
            int generic = enchanted.getManaCost().getGenericCost();
            if (generic > 0) sb.append(generic);

            for (ManaCostShard s : enchanted.getManaCost()) {
              ColorSet cs = ColorSet.fromMask(s.getColorMask());
              if (cs.isColorless()) continue;
              sb.append(' ');
              if (cs.isMonoColor()) sb.append(MagicColor.toShortString(s.getColorMask()));
              else /* (cs.isMulticolor()) */ {
                byte chosenColor =
                    sa.getActivatingPlayer()
                        .getController()
                        .chooseColor("Choose a single color from " + s.toString(), sa, cs);
                sb.append(MagicColor.toShortString(chosenColor));
              }
            }
            abMana.setExpressChoice(sb.toString().trim());
          } else if (type.equals("LastNotedType")) {
            Mana manaType = (Mana) Iterables.getFirst(card.getRemembered(), null);
            if (manaType == null) {
              return;
            }
            String cs = manaType.toString();
            abMana.setExpressChoice(cs);
          }

          if (abMana.getExpressChoice().isEmpty()) {
            System.out.println(
                "AbilityFactoryMana::manaResolve() - special mana effect is empty for "
                    + sa.getHostCard().getName());
          }
        }
      }
    }

    for (final Player player : tgtPlayers) {
      abMana.produceMana(GameActionUtil.generatedMana(sa), player, sa);
    }

    // Only clear express choice after mana has been produced
    abMana.clearExpressChoice();

    // resolveDrawback(sa);
  }