Esempio n. 1
0
  @Override
  public boolean perform(Game game, Event event, Map<Parameter, MagicSet> parameters) {
    Answer[] possibleResults = {Answer.WIN, Answer.LOSE};
    if (parameters.containsKey(Parameter.TYPE)) {
      Set<Answer> typeParameter = parameters.get(Parameter.TYPE).getAll(Answer.class);
      if (typeParameter.size() != 2)
        throw new UnsupportedOperationException(
            "Coin flip type " + typeParameter + " does not contain exactly two Answer objects!");
      possibleResults = typeParameter.toArray(possibleResults);
    }

    Answer flipResult = possibleResults[this.generator.nextBoolean() ? 1 : 0];
    if (game.noRandom) {
      Player player = parameters.get(Parameter.PLAYER).getOne(Player.class);
      flipResult =
          player
              .choose(
                  1,
                  Arrays.asList(possibleResults),
                  PlayerInterface.ChoiceType.COIN_FLIP,
                  PlayerInterface.ChooseReason.MANIPULATE_COIN_FLIP)
              .get(0);
    }

    event.setResult(Identity.instance(flipResult));

    // This returns false if the flip resulted in Answer.LOSE, but true
    // for everything else (including Answer.WIN, and any specified
    // Answers)
    return (flipResult != Answer.LOSE);
  }
Esempio n. 2
0
          @Override
          public boolean perform(Game game, Event event, java.util.Map<Parameter, Set> parameters) {
            Set cards = new Set();

            for (Player player : parameters.get(Parameter.PLAYER).getAll(Player.class))
              cards.addAll(player.getHand(game.actualState).objects);

            java.util.Map<Parameter, Set> discardParameters =
                new java.util.HashMap<Parameter, Set>();
            discardParameters.put(EventType.Parameter.CAUSE, parameters.get(Parameter.CAUSE));
            discardParameters.put(EventType.Parameter.CARD, cards);
            Event discardEvent =
                createEvent(
                    game,
                    "Each player discards his or her hand",
                    EventType.DISCARD_CARDS,
                    discardParameters);
            discardEvent.perform(event, true);

            java.util.Map<Parameter, Set> drawParameters = new java.util.HashMap<Parameter, Set>();
            drawParameters.put(EventType.Parameter.CAUSE, parameters.get(Parameter.CAUSE));
            drawParameters.put(EventType.Parameter.PLAYER, parameters.get(Parameter.PLAYER));
            drawParameters.put(EventType.Parameter.NUMBER, new Set(4));
            Event drawEvent =
                createEvent(game, "and draws four cards.", EventType.DRAW_CARDS, drawParameters);
            drawEvent.perform(event, true);

            event.setResult(Empty.set);

            return true;
          }
Esempio n. 3
0
  @Override
  public boolean perform(Game game, Event event, java.util.Map<Parameter, Set> parameters) {
    org.rnd.jmagic.sanitized.SanitizedEvent sanitized =
        new org.rnd.jmagic.sanitized.SanitizedEvent(event);
    for (Player player : game.actualState.players) player.alert(sanitized);

    event.setResult(Empty.set);
    return true;
  }
Esempio n. 4
0
  @Override
  public boolean perform(Game game, Event event, Map<Parameter, MagicSet> parameters) {
    int lifeGain = Sum.get(parameters.get(Parameter.NUMBER));
    if (lifeGain <= 0) {
      event.setResult(Empty.set);
      return true;
    }

    MagicSet result = new MagicSet();
    MagicSet players = parameters.get(Parameter.PLAYER);
    for (Player player : players.getAll(Player.class)) {
      HashMap<Parameter, MagicSet> newParameters = new HashMap<Parameter, MagicSet>(parameters);
      newParameters.put(Parameter.PLAYER, new MagicSet(player));
      Event gain =
          createEvent(
              game, player + " gains " + lifeGain + " life", GAIN_LIFE_ONE_PLAYER, newParameters);
      gain.perform(event, false);
      result.addAll(gain.getResult());
    }

    event.setResult(result);
    return true;
  }
Esempio n. 5
0
        @Override
        public boolean perform(Game game, Event event, Map<Parameter, MagicSet> parameters) {
          MagicSet cause = parameters.get(Parameter.CAUSE);
          event.setResult(Empty.set);

          Set<Color> choices = parameters.get(Parameter.CHOICE).getAll(Color.class);
          Player player = parameters.get(Parameter.PLAYER).getOne(Player.class);

          PlayerInterface.ChooseParameters<Color> chooseParameters =
              new PlayerInterface.ChooseParameters<Color>(
                  1,
                  1,
                  new LinkedList<Color>(choices),
                  PlayerInterface.ChoiceType.COLOR,
                  PlayerInterface.ChooseReason.CHOOSE_COLOR);
          chooseParameters.thisID = cause.getOne(GameObject.class).ID;
          List<Color> chosenList = player.choose(chooseParameters);
          if (chosenList.isEmpty()) return false;

          Color color = chosenList.get(0);

          Class<? extends Protection> ability = Protection.from(color);

          ContinuousEffect.Part part =
              new ContinuousEffect.Part(ContinuousEffectType.ADD_ABILITY_TO_OBJECT);
          part.parameters.put(
              ContinuousEffectType.Parameter.OBJECT,
              Intersect.instance(
                  HasColor.instance(Color.WHITE),
                  Intersect.instance(
                      HasType.instance(Type.CREATURE), ControlledBy.instance(You.instance()))));
          part.parameters.put(
              ContinuousEffectType.Parameter.ABILITY,
              Identity.instance(new SimpleAbilityFactory(ability)));

          Map<Parameter, MagicSet> fceParameters = new HashMap<Parameter, MagicSet>();
          fceParameters.put(Parameter.CAUSE, cause);
          fceParameters.put(Parameter.EFFECT, new MagicSet(part));
          Event protection =
              createEvent(
                  game,
                  "White creatures you control gain protection from the chosen color until end of turn.",
                  EventType.CREATE_FLOATING_CONTINUOUS_EFFECT,
                  fceParameters);
          protection.perform(event, false);

          return true;
        }
Esempio n. 6
0
        @Override
        public boolean perform(Game game, Event event, Map<Parameter, MagicSet> parameters) {
          event.setResult(Empty.set);

          MagicSet cause = parameters.get(Parameter.CAUSE);
          Player you = parameters.get(Parameter.PLAYER).getOne(Player.class);
          Zone library = you.getLibrary(game.actualState);

          Map<Parameter, MagicSet> zombieParameters = new HashMap<Parameter, MagicSet>();
          zombieParameters.put(Parameter.CAUSE, cause);
          zombieParameters.put(Parameter.PLAYER, new MagicSet(you));
          zombieParameters.put(Parameter.NUMBER, ONE);
          zombieParameters.put(Parameter.CARD, new MagicSet(library));
          zombieParameters.put(Parameter.TYPE, new MagicSet(HasSubType.instance(SubType.ZOMBIE)));
          Event zombieSearch =
              createEvent(game, "Search your library for a Zombie card", SEARCH, zombieParameters);
          zombieSearch.perform(event, false);

          Map<Parameter, MagicSet> swampParameters =
              new HashMap<Parameter, MagicSet>(zombieParameters);
          swampParameters.put(Parameter.TYPE, new MagicSet(HasSubType.instance(SubType.SWAMP)));
          Event swampSearch =
              createEvent(game, "Search your library for a Swamp card", SEARCH, swampParameters);
          swampSearch.perform(event, false);

          MagicSet thoseCards = new MagicSet();
          thoseCards.addAll(zombieSearch.getResult());
          thoseCards.addAll(swampSearch.getResult());

          Map<Parameter, MagicSet> moveParameters = new HashMap<Parameter, MagicSet>();
          moveParameters.put(Parameter.CAUSE, cause);
          moveParameters.put(Parameter.TO, new MagicSet(you.getHand(game.actualState)));
          moveParameters.put(Parameter.OBJECT, thoseCards);
          Event move =
              createEvent(game, "Put those cards into your hand", MOVE_OBJECTS, moveParameters);
          move.perform(event, true);

          Map<Parameter, MagicSet> shuffleParameters = new HashMap<Parameter, MagicSet>();
          shuffleParameters.put(Parameter.CAUSE, cause);
          shuffleParameters.put(Parameter.PLAYER, new MagicSet(you));
          Event shuffle =
              createEvent(game, "Shuffle your library", SHUFFLE_LIBRARY, shuffleParameters);
          shuffle.perform(event, true);

          return true;
        }
Esempio n. 7
0
  @Override
  public boolean perform(Game game, Event event, java.util.Map<Parameter, Set> parameters) {
    boolean allDiscarded = event.allChoicesMade;
    Set cause = parameters.get(Parameter.CAUSE);
    Set result = new Set();

    for (Player player : parameters.get(Parameter.PLAYER).getAll(Player.class)) {
      Set discardThese = event.getChoices(player);

      java.util.Map<Parameter, Set> discardParameters = new java.util.HashMap<Parameter, Set>();
      discardParameters.put(Parameter.CAUSE, cause);
      discardParameters.put(Parameter.CARD, discardThese);
      Event discard =
          createEvent(
              game, player + " discards " + discardThese + ".", DISCARD_CARDS, discardParameters);
      if (!discard.perform(event, false)) allDiscarded = false;
      result.addAll(discard.getResult());
    }

    event.setResult(Identity.instance(result));
    return allDiscarded;
  }
  @Override
  public boolean perform(Game game, Event event, Map<Parameter, MagicSet> parameters) {
    Event damageEvent =
        createEvent(game, event.getName(), EventType.DEAL_DAMAGE_EVENLY, parameters);
    boolean ret = damageEvent.perform(event, false);

    SetPattern affectedCreatures =
        new SimpleSetPattern(
            Intersect.instance(
                TakerOfDamage.instance(EventDamage.instance(Identity.instance(event))),
                CreaturePermanents.instance()));

    SimpleEventPattern regenerate = new SimpleEventPattern(EventType.REGENERATE);
    regenerate.put(EventType.Parameter.OBJECT, affectedCreatures);

    EventReplacementEffectStopper stopRegen =
        new EventReplacementEffectStopper(
            parameters.get(Parameter.SOURCE).getOne(GameObject.class), null, regenerate);
    ContinuousEffect.Part part =
        new ContinuousEffect.Part(ContinuousEffectType.STOP_REPLACEMENT_EFFECT);
    part.parameters.put(ContinuousEffectType.Parameter.PROHIBITION, Identity.instance(stopRegen));

    Map<EventType.Parameter, MagicSet> stopRegenParameters =
        new HashMap<EventType.Parameter, MagicSet>();
    stopRegenParameters.put(EventType.Parameter.CAUSE, parameters.get(Parameter.SOURCE));
    stopRegenParameters.put(EventType.Parameter.EFFECT, new MagicSet(part));
    Event regenStopper =
        createEvent(
            game,
            "A creature dealt damage this way can't be regenerated this turn.",
            EventType.CREATE_FLOATING_CONTINUOUS_EFFECT,
            stopRegenParameters);
    ret = regenStopper.perform(event, false) && ret;

    event.setResult(Empty.set);

    return ret;
  }
Esempio n. 9
0
  @Override
  public boolean perform(Game game, Event event, Map<Parameter, MagicSet> parameters) {
    GameObject permanent = parameters.get(Parameter.PERMANENT).getOne(GameObject.class);
    Player owner = permanent.getOwner(game.actualState);
    Zone graveyard = owner.getGraveyard(game.actualState);

    Map<Parameter, MagicSet> moveParameters = new HashMap<Parameter, MagicSet>();
    moveParameters.put(Parameter.CAUSE, parameters.get(Parameter.CAUSE));
    moveParameters.put(Parameter.TO, new MagicSet(graveyard));
    moveParameters.put(Parameter.OBJECT, new MagicSet(permanent));

    // move the permanent to the graveyard
    Event move =
        createEvent(
            game,
            "Put " + permanent + " in " + owner + "'s graveyard.",
            MOVE_OBJECTS,
            moveParameters);
    boolean status = move.perform(event, false);

    event.setResult(move.getResultGenerator());
    return status;
  }
Esempio n. 10
0
  @Override
  public boolean perform(Game game, Event event, java.util.Map<Parameter, Set> parameters) {
    Set result = new Set();
    Identified producer = parameters.get(Parameter.SOURCE).getOne(Identified.class);
    java.util.Set<Color> colors = parameters.get(Parameter.MANA).getAll(Color.class);
    java.util.Set<ManaSymbol.ManaType> types =
        parameters.get(Parameter.MANA).getAll(ManaSymbol.ManaType.class);
    ManaPool pool = new ManaPool();

    ManaSymbol addition = new ManaSymbol("");
    addition.colors.addAll(colors);
    for (ManaSymbol.ManaType type : types) {
      if (ManaSymbol.ManaType.COLORLESS == type) {
        addition.colorless = 1;
        addition.name += "1";
      } else {
        Color color = type.getColor();
        addition.colors.add(color);
        addition.name += color.getLetter();
      }
    }
    if (!addition.isZero()) pool.add(addition);

    pool.addAll(parameters.get(Parameter.MANA).getAll(ManaSymbol.class));

    int multiply = 1;
    if (parameters.containsKey(Parameter.MULTIPLY))
      multiply = Sum.get(parameters.get(Parameter.MULTIPLY));

    if (multiply != 1) {
      ManaPool newPool = new ManaPool();

      if (multiply > 1) {
        newPool.addAll(pool);
        for (ManaSymbol symbol : pool)
          for (int i = 1; i < multiply; ++i) newPool.add(symbol.create());
      }

      pool = newPool;
    }

    int number = 1;
    if (parameters.containsKey(Parameter.NUMBER))
      number = Sum.get(parameters.get(Parameter.NUMBER));

    boolean snow = false;
    if (producer.isGameObject())
      snow = ((GameObject) producer).getSuperTypes().contains(SuperType.SNOW);
    for (Player actualPlayer : parameters.get(Parameter.PLAYER).getAll(Player.class)) {
      Player physicalPlayer = actualPlayer.getPhysical();
      java.util.Set<CostCollection> choices = pool.explode("Add");
      // TODO : Ticket 430

      ManaPool chosen = null;
      if (choices.isEmpty()) // empty pool to start with
      chosen = pool;
      else if (choices.size() == 1) chosen = choices.iterator().next().manaCost;
      else {
        // if all of the choices are mana of a single color, present
        // it as a color choice
        boolean allSingleColorSymbols = true;
        for (CostCollection choiceCollection : choices) {
          ManaPool choice = choiceCollection.manaCost;
          if (choice.converted() != 1) {
            allSingleColorSymbols = false;
            break;
          }
          ManaSymbol symbol = choice.first();
          if (symbol.colorless != 0) {
            allSingleColorSymbols = false;
            break;
          }
          if (symbol.colors.size() != 1) {
            allSingleColorSymbols = false;
            break;
          }
        }

        if (allSingleColorSymbols) {
          java.util.Set<Color> colorChoices = java.util.EnumSet.noneOf(Color.class);
          for (CostCollection choice : choices)
            colorChoices.add(choice.manaCost.first().colors.iterator().next());
          Color chosenColor = physicalPlayer.chooseColor(colorChoices, producer.ID);

          ManaSymbol s = pool.first().create();
          s.colors = java.util.EnumSet.of(chosenColor);
          s.colorless = 0;
          chosen = new ManaPool(java.util.Collections.singleton(s));
        } else
          chosen =
              physicalPlayer
                  .sanitizeAndChoose(
                      game.actualState,
                      1,
                      choices,
                      PlayerInterface.ChoiceType.MANA_EXPLOSION,
                      PlayerInterface.ChooseReason.HYBRID_MANA)
                  .iterator()
                  .next()
                  .manaCost;
      }

      for (int i = 0; i < number; ++i) {
        // Use the reference mana to create a new mana to
        // add to the pool
        for (ManaSymbol newMana : chosen) {
          newMana = newMana.create();
          newMana.sourceID = producer.ID;
          newMana.isSnow = snow;
          physicalPlayer.pool.add(newMana);
          result.add(newMana);
          if (game.currentAction != null) game.currentAction.manaProduced.add(newMana);
        }
      }
    }

    GameObject source = event.getSource();
    if (source.isActivatedAbility())
      ((ActivatedAbility) source.getPhysical()).addedMana(result.getAll(ManaSymbol.class));

    event.setResult(Identity.instance(result));
    return true;
  }
Esempio n. 11
0
  @Override
  public boolean perform(Game game, Event event, java.util.Map<Parameter, Set> parameters) {
    GameObject object = parameters.get(Parameter.OBJECT).getOne(GameObject.class);
    Identified newTarget = parameters.get(Parameter.TARGET).getOne(Identified.class);
    java.util.Set<Target> canBeChanged = new java.util.HashSet<Target>();

    java.util.Map<Target, Boolean> wasLegal = new java.util.HashMap<Target, Boolean>();

    java.util.Map<Target, java.util.List<Target>> allChosenTargets =
        new java.util.HashMap<Target, java.util.List<Target>>();
    for (java.util.Map<Target, java.util.List<Target>> chosenTargets : object.getChosenTargets())
      allChosenTargets.putAll(chosenTargets);

    // Introduce this scope because there are two variables named
    // 'restricted'
    {
      java.util.Set<Integer> restricted = new java.util.HashSet<Integer>();
      for (Mode checkMode : object.getSelectedModes()) {
        for (Target checkBaseTarget : checkMode.targets) {
          java.util.Set<Integer> thisBaseTarget = new java.util.HashSet<Integer>();
          for (Target checkTarget : allChosenTargets.get(checkBaseTarget)) {
            Set legalCheck = checkTarget.legalChoicesNow(game, object);
            boolean legal =
                !(restricted.contains(checkTarget.targetID)
                    || !legalCheck.contains(game.actualState.get(checkTarget.targetID)));
            wasLegal.put(checkTarget, legal);
            if (checkTarget.restrictFromLaterTargets) restricted.add(checkTarget.targetID);
            thisBaseTarget.add(checkTarget.targetID);
          }
        }
      }
    }

    for (Mode mode : object.getSelectedModes()) {
      for (Target baseTarget : mode.targets) {
        for (Target target : allChosenTargets.get(baseTarget)) {
          Set targets = target.legalChoicesNow(game, object);
          if (targets.contains(newTarget)) {
            // The target won't "change" if its already set to
            // this target.
            if (target.targetID == newTarget.ID) continue;

            int oldValue = target.targetID;
            target.targetID = newTarget.ID;

            boolean illegal = false;
            java.util.Set<Integer> restricted = new java.util.HashSet<Integer>();
            legalityCheck:
            for (Mode checkMode : object.getSelectedModes()) {
              for (Target checkBaseTarget : checkMode.targets) {
                java.util.Set<Integer> thisBaseTarget = new java.util.HashSet<Integer>();
                for (Target checkTarget : allChosenTargets.get(checkBaseTarget)) {
                  Set legalCheck = checkTarget.legalChoicesNow(game, object);
                  boolean targetWasLegal = wasLegal.get(checkTarget);
                  if (targetWasLegal
                      && (thisBaseTarget.contains(checkTarget.targetID)
                          || restricted.contains(checkTarget.targetID)
                          || !legalCheck.contains(game.actualState.get(checkTarget.targetID)))) {
                    illegal = true;
                    break legalityCheck;
                  }
                  if (checkTarget.restrictFromLaterTargets) restricted.add(checkTarget.targetID);
                  thisBaseTarget.add(checkTarget.targetID);
                }
              }
            }

            target.targetID = oldValue;

            if (!illegal) {
              canBeChanged.add(target);
            }
          }
        }
      }
    }

    Player player = parameters.get(Parameter.PLAYER).getOne(Player.class);
    java.util.List<Target> choice =
        player.sanitizeAndChoose(
            game.actualState,
            1,
            canBeChanged,
            PlayerInterface.ChoiceType.SINGLE_TARGET,
            PlayerInterface.ChooseReason.TARGET);

    if (!choice.isEmpty()) {
      event.setResult(Identity.instance(object));
      Target chosenTarget = choice.get(0);
      chosenTarget.targetID = newTarget.ID;

      // Also set the target on the physical object
      java.util.Map<Target, java.util.List<Target>>[] chosenTargets = object.getChosenTargets();
      setPhysicalTarget:
      for (int sideNumber = 0; sideNumber < object.getChosenTargets().length; ++sideNumber)
        for (java.util.Map.Entry<Target, java.util.List<Target>> entry :
            chosenTargets[sideNumber].entrySet()) {
          for (int targetNumber = 0; targetNumber < entry.getValue().size(); ++targetNumber) {
            if (entry.getValue().get(targetNumber) == chosenTarget) {
              GameObject physical = object.getPhysical();
              physical
                      .getChosenTargets()[sideNumber]
                      .get(entry.getKey())
                      .get(targetNumber)
                      .targetID =
                  newTarget.ID;
              break setPhysicalTarget;
            }
          }
        }
    } else {
      event.setResult(Identity.instance());
    }

    return choice.size() == 1;
  }
Esempio n. 12
0
 @Override
 public boolean perform(Game game, Event event, Map<Parameter, MagicSet> parameters) {
   event.setResult(Empty.set);
   return true;
 }