Search in sources :

Example 1 with CommanderReplacementEffect

use of mage.abilities.effects.common.continuous.CommanderReplacementEffect in project mage by magefree.

the class ContinuousEffects method getApplicableReplacementEffects.

/**
 * @param event
 * @param game
 * @return a list of all {@link ReplacementEffect} that apply to the current
 * event
 */
private Map<ReplacementEffect, Set<Ability>> getApplicableReplacementEffects(GameEvent event, Game game) {
    Map<ReplacementEffect, Set<Ability>> replaceEffects = new HashMap<>();
    if (auraReplacementEffect.checksEventType(event, game) && auraReplacementEffect.applies(event, null, game)) {
        replaceEffects.put(auraReplacementEffect, null);
    }
    // get all applicable transient Replacement effects
    for (Iterator<ReplacementEffect> iterator = replacementEffects.iterator(); iterator.hasNext(); ) {
        ReplacementEffect effect = iterator.next();
        if (!effect.checksEventType(event, game)) {
            continue;
        }
        if (event.getAppliedEffects() != null && event.getAppliedEffects().contains(effect.getId())) {
            if (!(effect instanceof CommanderReplacementEffect)) {
                // TODO: Handle also gained effect that are connected to different abilities.
                continue;
            }
        }
        Set<Ability> abilities = replacementEffects.getAbility(effect.getId());
        Set<Ability> applicableAbilities = new HashSet<>();
        for (Ability ability : abilities) {
            // for replacment effects of static abilities do not use LKI to check if to apply
            if (ability.getAbilityType() != AbilityType.STATIC || ability.isInUseableZone(game, null, event)) {
                if (!effect.isUsed()) {
                    if (!game.getScopeRelevant() || effect.hasSelfScope() || !event.getTargetId().equals(ability.getSourceId())) {
                        if (effect.applies(event, ability, game)) {
                            applicableAbilities.add(ability);
                        }
                    }
                }
            }
        }
        if (!applicableAbilities.isEmpty()) {
            replaceEffects.put(effect, applicableAbilities);
        }
    }
    for (Iterator<PreventionEffect> iterator = preventionEffects.iterator(); iterator.hasNext(); ) {
        PreventionEffect effect = iterator.next();
        if (!effect.checksEventType(event, game)) {
            continue;
        }
        if (event.getAppliedEffects() != null && event.getAppliedEffects().contains(effect.getId())) {
            // TODO: Handle also gained effect that are connected to different abilities.
            continue;
        }
        Set<Ability> abilities = preventionEffects.getAbility(effect.getId());
        Set<Ability> applicableAbilities = new HashSet<>();
        for (Ability ability : abilities) {
            if (ability.getAbilityType() != AbilityType.STATIC || ability.isInUseableZone(game, null, event)) {
                if (effect.getDuration() != Duration.OneUse || !effect.isUsed()) {
                    if (effect.applies(event, ability, game)) {
                        applicableAbilities.add(ability);
                    }
                }
            }
        }
        if (!applicableAbilities.isEmpty()) {
            replaceEffects.put(effect, applicableAbilities);
        }
    }
    return replaceEffects;
}
Also used : StaticAbility(mage.abilities.StaticAbility) Ability(mage.abilities.Ability) CommanderReplacementEffect(mage.abilities.effects.common.continuous.CommanderReplacementEffect) CommanderReplacementEffect(mage.abilities.effects.common.continuous.CommanderReplacementEffect)

Example 2 with CommanderReplacementEffect

use of mage.abilities.effects.common.continuous.CommanderReplacementEffect in project mage by magefree.

the class DefaultCommander method init.

@Override
protected void init(UUID choosingPlayerId) {
    // move tiny leader to command zone
    for (UUID playerId : state.getPlayerList(startingPlayerId)) {
        Player player = getPlayer(playerId);
        if (player != null) {
            String commanderName = player.getMatchPlayer().getDeck().getName();
            Card commander = findCommander(this, player, commanderName);
            if (commander != null) {
                // already exists - just move to zone (example: game restart by Karn Liberated)
                commander.moveToZone(Zone.COMMAND, null, this, true);
            } else {
                // create new commander
                commander = getCommanderCard(commanderName, player.getId());
                if (commander != null) {
                    Set<Card> cards = new HashSet<>();
                    cards.add(commander);
                    this.loadCards(cards, playerId);
                    player.addCommanderId(commander.getId());
                    commander.moveToZone(Zone.COMMAND, null, this, true);
                    Ability ability = new SimpleStaticAbility(Zone.COMMAND, new InfoEffect("Commander effects"));
                    ability.addEffect(new CommanderReplacementEffect(commander.getId(), alsoHand, alsoLibrary, false, "Commander"));
                    ability.addEffect(new CommanderCostModification(commander));
                    // Commander rule #4 was removed Jan. 18, 2016
                    // ability.addEffect(new CommanderManaReplacementEffect(player.getId(), CardUtil.getColorIdentity(commander)));
                    CommanderInfoWatcher watcher = new CommanderInfoWatcher("Commander", commander.getId(), false);
                    getState().addWatcher(watcher);
                    watcher.addCardInfoToCommander(this);
                    this.getState().addAbility(ability, null);
                } else {
                    // Test use case: create tiny game with random generated deck - game freezes with empty battlefield
                    throw new IllegalStateException("Commander card could not be created. Name: [" + player.getMatchPlayer().getDeck().getName() + ']');
                }
            }
        }
    }
    super.init(choosingPlayerId);
    if (startingPlayerSkipsDraw) {
        state.getTurnMods().add(new TurnMod(startingPlayerId, PhaseStep.DRAW));
    }
}
Also used : SimpleStaticAbility(mage.abilities.common.SimpleStaticAbility) Ability(mage.abilities.Ability) Player(mage.players.Player) SimpleStaticAbility(mage.abilities.common.SimpleStaticAbility) CommanderReplacementEffect(mage.abilities.effects.common.continuous.CommanderReplacementEffect) TurnMod(mage.game.turn.TurnMod) Card(mage.cards.Card) InfoEffect(mage.abilities.effects.common.InfoEffect) CommanderCostModification(mage.abilities.effects.common.cost.CommanderCostModification) UUID(java.util.UUID) CommanderInfoWatcher(mage.watchers.common.CommanderInfoWatcher) HashSet(java.util.HashSet)

Example 3 with CommanderReplacementEffect

use of mage.abilities.effects.common.continuous.CommanderReplacementEffect in project mage by magefree.

the class ContinuousEffects method replaceEvent.

public boolean replaceEvent(GameEvent event, Game game) {
    boolean caught = false;
    Map<UUID, Set<UUID>> consumed = new HashMap<>();
    do {
        Map<ReplacementEffect, Set<Ability>> rEffects = getApplicableReplacementEffects(event, game);
        // Remove all consumed effects (ability dependant)
        for (Iterator<ReplacementEffect> it1 = rEffects.keySet().iterator(); it1.hasNext(); ) {
            ReplacementEffect entry = it1.next();
            if (consumed.containsKey(entry.getId())) /*&& !(entry instanceof CommanderReplacementEffect) */
            {
                // 903.9.
                Set<UUID> consumedAbilitiesIds = consumed.get(entry.getId());
                if (rEffects.get(entry) == null || consumedAbilitiesIds.size() == rEffects.get(entry).size()) {
                    it1.remove();
                } else {
                    Iterator it = rEffects.get(entry).iterator();
                    while (it.hasNext()) {
                        Ability ability = (Ability) it.next();
                        if (consumedAbilitiesIds.contains(ability.getId())) {
                            it.remove();
                        }
                    }
                }
            }
        }
        // no effects left, quit
        if (rEffects.isEmpty()) {
            break;
        }
        int index;
        boolean onlyOne = false;
        if (rEffects.size() == 1) {
            ReplacementEffect effect = rEffects.keySet().iterator().next();
            Set<Ability> abilities;
            if (effect.getEffectType() == EffectType.REPLACEMENT) {
                abilities = replacementEffects.getAbility(effect.getId());
            } else {
                abilities = preventionEffects.getAbility(effect.getId());
            }
            if (abilities == null || abilities.size() == 1) {
                onlyOne = true;
            }
        }
        if (onlyOne) {
            index = 0;
        } else {
            // 20100716 - 616.1c
            Player player = game.getPlayer(event.getPlayerId());
            index = player.chooseReplacementEffect(getReplacementEffectsTexts(rEffects, game), game);
        }
        // get the selected effect
        int checked = 0;
        ReplacementEffect rEffect = null;
        Ability rAbility = null;
        for (Map.Entry<ReplacementEffect, Set<Ability>> entry : rEffects.entrySet()) {
            if (entry.getValue() == null) {
                if (checked == index) {
                    rEffect = entry.getKey();
                    break;
                } else {
                    checked++;
                }
            } else {
                Set<Ability> abilities = entry.getValue();
                int size = abilities.size();
                if (index > (checked + size - 1)) {
                    checked += size;
                } else {
                    rEffect = entry.getKey();
                    Iterator it = abilities.iterator();
                    while (it.hasNext() && rAbility == null) {
                        if (checked == index) {
                            rAbility = (Ability) it.next();
                        } else {
                            it.next();
                            checked++;
                        }
                    }
                    break;
                }
            }
        }
        if (rEffect != null) {
            event.getAppliedEffects().add(rEffect.getId());
            caught = rEffect.replaceEvent(event, rAbility, game);
            if (Duration.OneUse.equals(rEffect.getDuration())) {
                rEffect.discard();
            }
        }
        if (caught) {
            // Event was completely replaced -> stop applying effects to it
            break;
        }
        // add the applied effect to the consumed effects
        if (rEffect != null) {
            if (consumed.containsKey(rEffect.getId())) {
                Set<UUID> set = consumed.get(rEffect.getId());
                if (rAbility != null) {
                    set.add(rAbility.getId());
                }
            } else {
                Set<UUID> set = new HashSet<>();
                if (rAbility != null) {
                    // in case of AuraReplacementEffect or PlaneswalkerReplacementEffect there is no Ability
                    set.add(rAbility.getId());
                }
                consumed.put(rEffect.getId(), set);
            }
        }
    // Must be called here for some effects to be able to work correctly
    // For example: Vesuva copying a Dark Depth (VesuvaTest:testDarkDepth)
    // This call should be removed if possible as replacement effects of EntersTheBattlefield events
    // do no longer work correctly because the entering permanents are not yet on the battlefield (before they were).
    // game.applyEffects();
    } while (true);
    return caught;
}
Also used : StaticAbility(mage.abilities.StaticAbility) Ability(mage.abilities.Ability) Player(mage.players.Player) CommanderReplacementEffect(mage.abilities.effects.common.continuous.CommanderReplacementEffect)

Example 4 with CommanderReplacementEffect

use of mage.abilities.effects.common.continuous.CommanderReplacementEffect in project mage by magefree.

the class GameCommanderImpl method initCommanderEffects.

public void initCommanderEffects(Card commander, Player player, Ability commanderAbility) {
    // all commander effects must be independent from sourceId or controllerId
    commanderAbility.addEffect(new CommanderReplacementEffect(commander.getId(), alsoHand, alsoLibrary, false, "Commander"));
    commanderAbility.addEffect(new CommanderCostModification(commander));
}
Also used : CommanderReplacementEffect(mage.abilities.effects.common.continuous.CommanderReplacementEffect) CommanderCostModification(mage.abilities.effects.common.cost.CommanderCostModification)

Example 5 with CommanderReplacementEffect

use of mage.abilities.effects.common.continuous.CommanderReplacementEffect in project mage by magefree.

the class OathbreakerFreeForAll method initCommanderEffects.

@Override
public void initCommanderEffects(Card commander, Player player, Ability commanderAbility) {
    // all commander effects must be independent from sourceId or controllerId (it's limitation of current commander effects)
    boolean isSignatureSpell = this.playerSignatureSpells.getOrDefault(player.getId(), new HashSet<>()).contains(commander.getId());
    // basic commmander restrict (oathbreaker may ask to move, signature force to move)
    commanderAbility.addEffect(new CommanderReplacementEffect(commander.getId(), alsoHand, alsoLibrary, isSignatureSpell, getCommanderTypeName(commander)));
    commanderAbility.addEffect(new CommanderCostModification(commander));
    // signature spell restrict (spell can be casted on player's commander on battlefield)
    if (isSignatureSpell) {
        OathbreakerOnBattlefieldCondition condition = new OathbreakerOnBattlefieldCondition(this, player.getId(), commander.getId(), this.playerOathbreakers.getOrDefault(player.getId(), new HashSet<>()));
        commanderAbility.addEffect(new SignatureSpellCastOnlyWithOathbreakerEffect(condition, commander.getId()));
        // hint must be added to card, not global ability
        Ability ability = new SimpleStaticAbility(new InfoEffect("Signature spell hint"));
        ability.addHint(new ConditionHint(condition, "Oathbreaker on battlefield (" + condition.getCompatibleNames() + ")"));
        ability.setRuleVisible(false);
        commander.addAbility(ability);
    }
}
Also used : SimpleStaticAbility(mage.abilities.common.SimpleStaticAbility) Ability(mage.abilities.Ability) OathbreakerOnBattlefieldCondition(mage.abilities.condition.common.OathbreakerOnBattlefieldCondition) CommanderReplacementEffect(mage.abilities.effects.common.continuous.CommanderReplacementEffect) SimpleStaticAbility(mage.abilities.common.SimpleStaticAbility) SignatureSpellCastOnlyWithOathbreakerEffect(mage.abilities.common.SignatureSpellCastOnlyWithOathbreakerEffect) ConditionHint(mage.abilities.hint.ConditionHint) CommanderCostModification(mage.abilities.effects.common.cost.CommanderCostModification) InfoEffect(mage.abilities.effects.common.InfoEffect)

Aggregations

CommanderReplacementEffect (mage.abilities.effects.common.continuous.CommanderReplacementEffect)5 Ability (mage.abilities.Ability)4 CommanderCostModification (mage.abilities.effects.common.cost.CommanderCostModification)3 StaticAbility (mage.abilities.StaticAbility)2 SimpleStaticAbility (mage.abilities.common.SimpleStaticAbility)2 InfoEffect (mage.abilities.effects.common.InfoEffect)2 Player (mage.players.Player)2 HashSet (java.util.HashSet)1 UUID (java.util.UUID)1 SignatureSpellCastOnlyWithOathbreakerEffect (mage.abilities.common.SignatureSpellCastOnlyWithOathbreakerEffect)1 OathbreakerOnBattlefieldCondition (mage.abilities.condition.common.OathbreakerOnBattlefieldCondition)1 ConditionHint (mage.abilities.hint.ConditionHint)1 Card (mage.cards.Card)1 TurnMod (mage.game.turn.TurnMod)1 CommanderInfoWatcher (mage.watchers.common.CommanderInfoWatcher)1