/* * Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are * permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, this list * of conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * The views and conclusions contained in the software and documentation are those of the * authors and should not be interpreted as representing official policies, either expressed * or implied, of BetaSteward_at_googlemail.com. */ package mage.abilities.effects; import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.UUID; import mage.Constants.AsThoughEffectType; import mage.Constants.Duration; import mage.Constants.EffectType; import mage.Constants.Layer; import mage.Constants.SubLayer; import mage.Constants.Zone; import mage.abilities.Ability; import mage.abilities.StaticAbility; import mage.cards.Card; import mage.game.Game; import mage.game.events.GameEvent; import mage.game.permanent.Permanent; import mage.players.Player; /** * * @author BetaSteward_at_googlemail.com */ public class ContinuousEffects implements Serializable { //transient Continuous effects private final List layeredEffects = new ArrayList(); private final List replacementEffects = new ArrayList(); private final List preventionEffects = new ArrayList(); private final List requirementEffects = new ArrayList(); private final List restrictionEffects = new ArrayList(); private final List asThoughEffects = new ArrayList(); private final List costModificationEffects = new ArrayList(); //map Abilities to Continuous effects private final Map abilityMap = new HashMap(); private final ApplyCountersEffect applyCounters; private final PlaneswalkerRedirectionEffect planeswalkerRedirectionEffect; public ContinuousEffects() { applyCounters = new ApplyCountersEffect(); planeswalkerRedirectionEffect = new PlaneswalkerRedirectionEffect(); } public ContinuousEffects(final ContinuousEffects effect) { this.applyCounters = effect.applyCounters.copy(); this.planeswalkerRedirectionEffect = effect.planeswalkerRedirectionEffect.copy(); for (ContinuousEffect entry: effect.layeredEffects) { layeredEffects.add((ContinuousEffect) entry.copy()); } for (ReplacementEffect entry: effect.replacementEffects) { replacementEffects.add((ReplacementEffect)entry.copy()); } for (PreventionEffect entry: effect.preventionEffects) { preventionEffects.add((PreventionEffect)entry.copy()); } for (RequirementEffect entry: effect.requirementEffects) { requirementEffects.add((RequirementEffect)entry.copy()); } for (RestrictionEffect entry: effect.restrictionEffects) { restrictionEffects.add((RestrictionEffect)entry.copy()); } for (AsThoughEffect entry: effect.asThoughEffects) { asThoughEffects.add((AsThoughEffect)entry.copy()); } for ( CostModificationEffect entry : effect.costModificationEffects ) { costModificationEffects.add(entry); } for (Entry entry: effect.abilityMap.entrySet()) { abilityMap.put(entry.getKey(), entry.getValue().copy()); } } public ContinuousEffects copy() { return new ContinuousEffects(this); } public List getRequirementEffects() { return requirementEffects; } public List getRestrictionEffects() { return restrictionEffects; } public Ability getAbility(UUID effectId) { return abilityMap.get(effectId); } public void removeEndOfTurnEffects() { for (Iterator i = layeredEffects.iterator(); i.hasNext();) { ContinuousEffect entry = i.next(); if (entry.getDuration() == Duration.EndOfTurn) i.remove(); } for (Iterator i = replacementEffects.iterator(); i.hasNext();) { ContinuousEffect entry = i.next(); if (entry.getDuration() == Duration.EndOfTurn) i.remove(); } for (Iterator i = preventionEffects.iterator(); i.hasNext();) { ContinuousEffect entry = i.next(); if (entry.getDuration() == Duration.EndOfTurn) i.remove(); } for (Iterator i = requirementEffects.iterator(); i.hasNext();) { ContinuousEffect entry = i.next(); if (entry.getDuration() == Duration.EndOfTurn) i.remove(); } for (Iterator i = restrictionEffects.iterator(); i.hasNext();) { ContinuousEffect entry = i.next(); if (entry.getDuration() == Duration.EndOfTurn) i.remove(); } for (Iterator i = asThoughEffects.iterator(); i.hasNext();) { ContinuousEffect entry = i.next(); if (entry.getDuration() == Duration.EndOfTurn) i.remove(); } for (Iterator i = costModificationEffects.iterator(); i.hasNext();) { ContinuousEffect entry = i.next(); if (entry.getDuration() == Duration.EndOfTurn) i.remove(); } } public void removeInactiveEffects(Game game) { for (Iterator i = layeredEffects.iterator(); i.hasNext();) { if (isInactive(i.next(), game)) i.remove(); } for (Iterator i = replacementEffects.iterator(); i.hasNext();) { if (isInactive(i.next(), game)) i.remove(); } for (Iterator i = preventionEffects.iterator(); i.hasNext();) { if (isInactive(i.next(), game)) i.remove(); } for (Iterator i = requirementEffects.iterator(); i.hasNext();) { if (isInactive(i.next(), game)) i.remove(); } for (Iterator i = restrictionEffects.iterator(); i.hasNext();) { if (isInactive(i.next(), game)) i.remove(); } for (Iterator i = asThoughEffects.iterator(); i.hasNext();) { if (isInactive(i.next(), game)) i.remove(); } for (Iterator i = costModificationEffects.iterator(); i.hasNext();) { if (isInactive(i.next(), game)) i.remove(); } } private boolean isInactive(ContinuousEffect effect, Game game) { switch(effect.getDuration()) { case WhileOnBattlefield: Permanent permanent = game.getPermanent(abilityMap.get(effect.getId()).getSourceId()); return (permanent == null || !permanent.isPhasedIn()); case OneUse: return effect.isUsed(); case Custom: return effect.isInactive(abilityMap.get(effect.getId()), game); } return false; } private List getLayeredEffects(Game game) { List layerEffects = new ArrayList(layeredEffects); for (Card card: game.getCards()) { if (game.getZone(card.getId()) == Zone.HAND || game.getZone(card.getId()) == Zone.GRAVEYARD) { for (Ability ability: card.getAbilities().getStaticAbilities(game.getZone(card.getId()))) { for (Effect effect: ability.getEffects(game, EffectType.CONTINUOUS)) { layerEffects.add((ContinuousEffect) effect); abilityMap.put(effect.getId(), ability); } } } } for (Permanent permanent: game.getBattlefield().getAllPermanents()) { for (Ability ability: permanent.getAbilities().getStaticAbilities(Zone.BATTLEFIELD)) { for (Effect effect: ability.getEffects(game, EffectType.CONTINUOUS)) { layerEffects.add((ContinuousEffect) effect); abilityMap.put(effect.getId(), ability); } } } Collections.sort(layerEffects, new TimestampSorter()); return layerEffects; } private List filterLayeredEffects(List effects, Layer layer) { List layerEffects = new ArrayList(); for (ContinuousEffect effect: effects) { if (effect.hasLayer(layer)) layerEffects.add(effect); } return layerEffects; } public List getApplicableRequirementEffects(Permanent permanent, Game game) { List effects = new ArrayList(); //get all applicable Requirement effects on the battlefield for (Permanent perm: game.getBattlefield().getActivePermanents(permanent.getControllerId(), game)) { for (StaticAbility ability: perm.getAbilities().getStaticAbilities(Zone.BATTLEFIELD)) { for (Effect effect: ability.getEffects(game, EffectType.REQUIREMENT)) { if (((RequirementEffect)effect).applies(permanent, ability, game)) { effects.add((RequirementEffect) effect); abilityMap.put(effect.getId(), ability); } } } } for (RequirementEffect effect: requirementEffects) { if (effect.applies(permanent, abilityMap.get(effect.getId()), game)) effects.add(effect); } return effects; } public List getApplicableRestrictionEffects(Permanent permanent, Game game) { List effects = new ArrayList(); //get all applicable Restriction effects on the battlefield for (Permanent perm: game.getBattlefield().getActivePermanents(permanent.getControllerId(), game)) { for (StaticAbility ability: perm.getAbilities().getStaticAbilities(Zone.BATTLEFIELD)) { for (Effect effect: ability.getEffects(game, EffectType.RESTRICTION)) { if (((RestrictionEffect)effect).applies(permanent, ability, game)) { effects.add((RestrictionEffect) effect); abilityMap.put(effect.getId(), ability); } } } } for (RestrictionEffect effect: restrictionEffects) { if (effect.applies(permanent, abilityMap.get(effect.getId()), game)) effects.add(effect); } return effects; } /** * * @param event * @param game * @return a list of all {@link ReplacementEffect} that apply to the current event */ private List getApplicableReplacementEffects(GameEvent event, Game game) { List replaceEffects = new ArrayList(); if (planeswalkerRedirectionEffect.applies(event, null, game)) replaceEffects.add(planeswalkerRedirectionEffect); //get all applicable Replacement effects in each players hand and graveyard for (Card card: game.getCards()) { if (game.getZone(card.getId()) == Zone.HAND || game.getZone(card.getId()) == Zone.GRAVEYARD) { for (Ability ability: card.getAbilities().getStaticAbilities(game.getZone(card.getId()))) { for (Effect effect: ability.getEffects(game, EffectType.REPLACEMENT)) { ReplacementEffect rEffect = (ReplacementEffect) effect; if (rEffect.applies(event, ability, game)) { replaceEffects.add(rEffect); abilityMap.put(rEffect.getId(), ability); } } for (Effect effect: ability.getEffects(game, EffectType.PREVENTION)) { ReplacementEffect rEffect = (ReplacementEffect) effect; if (rEffect.applies(event, ability, game)) { replaceEffects.add(rEffect); abilityMap.put(rEffect.getId(), ability); } } } } } //get all applicable Replacement effects on the battlefield for (Permanent permanent: game.getBattlefield().getAllPermanents()) { for (Ability ability: permanent.getAbilities().getStaticAbilities(Zone.BATTLEFIELD)) { for (Effect effect: ability.getEffects(game, EffectType.REPLACEMENT)) { ReplacementEffect rEffect = (ReplacementEffect) effect; if (rEffect.applies(event, ability, game)) { replaceEffects.add(rEffect); abilityMap.put(rEffect.getId(), ability); } } for (Effect effect: ability.getEffects(game, EffectType.PREVENTION)) { ReplacementEffect rEffect = (ReplacementEffect) effect; if (rEffect.applies(event, ability, game)) { replaceEffects.add(rEffect); abilityMap.put(rEffect.getId(), ability); } } } } //get all applicable transient Replacement effects for (ReplacementEffect effect: replacementEffects) { if (effect.getDuration() != Duration.OneUse || !effect.isUsed()) { if (effect.applies(event, abilityMap.get(effect.getId()), game)) { replaceEffects.add(effect); } } } for (PreventionEffect effect: preventionEffects) { if (effect.getDuration() != Duration.OneUse || !effect.isUsed()) { if (effect.applies(event, abilityMap.get(effect.getId()), game)) { replaceEffects.add(effect); } } } return replaceEffects; } public boolean asThough(UUID objectId, AsThoughEffectType type, Game game) { for (Permanent permanent: game.getBattlefield().getAllPermanents()) { for (Ability ability: permanent.getAbilities().getStaticAbilities(Zone.BATTLEFIELD)) { for (Effect effect: ability.getEffects(game, EffectType.ASTHOUGH)) { AsThoughEffect rEffect = (AsThoughEffect) effect; if (rEffect.applies(objectId, ability, game)) { return true; } } } } for (AsThoughEffect entry: asThoughEffects) { AsThoughEffect effect = entry; if (effect.getAsThoughEffectType() == type) { if (effect.getDuration() != Duration.OneUse || !effect.isUsed()) { if (effect.applies(objectId, abilityMap.get(entry.getId()), game)) { return true; } } } } return false; } /** * Inspects all {@link Permanent permanent's} {@link Ability abilities} on the battlefied * for {@link CostModificationEffect cost modification effects} and applies them if necessary. * * @param abilityToModify * @param game * @return */ public void costModification ( Ability abilityToModify, Game game ) { for ( Permanent permanent : game.getBattlefield().getAllPermanents() ) { for ( Ability ability : permanent.getAbilities().getStaticAbilities(Zone.BATTLEFIELD) ) { for ( Effect effect : ability.getEffects(game, EffectType.COSTMODIFICATION) ) { CostModificationEffect rEffect = (CostModificationEffect)effect; if ( rEffect.applies(abilityToModify, ability, game) ) { rEffect.apply(game, ability, abilityToModify); } } } } for ( CostModificationEffect effect : costModificationEffects ) { if ( effect.applies(abilityToModify, abilityMap.get(effect.getId()), game) ) { effect.apply(game, abilityMap.get(effect.getId()), abilityToModify); } } } public boolean replaceEvent(GameEvent event, Game game) { boolean caught = false; List rEffects = getApplicableReplacementEffects(event, game); if (rEffects.size() > 0) { int index; if (rEffects.size() == 1) { index = 0; } else { //20100716 - 616.1c Player player = game.getPlayer(event.getPlayerId()); index = player.chooseEffect(rEffects, game); } ReplacementEffect rEffect = rEffects.get(index); caught = rEffect.replaceEvent(event, abilityMap.get(rEffect.getId()), game); } return caught; } //20091005 - 613 public void apply(Game game) { removeInactiveEffects(game); List layerEffects = getLayeredEffects(game); List layer = filterLayeredEffects(layerEffects, Layer.CopyEffects_1); for (ContinuousEffect effect: layer) { effect.apply(Layer.CopyEffects_1, SubLayer.CharacteristicDefining_7a, abilityMap.get(effect.getId()), game); } for (ContinuousEffect effect: layer) { effect.apply(Layer.CopyEffects_1, SubLayer.NA, abilityMap.get(effect.getId()), game); } layer = filterLayeredEffects(layerEffects, Layer.ControlChangingEffects_2); for (ContinuousEffect effect: layer) { effect.apply(Layer.ControlChangingEffects_2, SubLayer.CharacteristicDefining_7a, abilityMap.get(effect.getId()), game); } for (ContinuousEffect effect: layer) { effect.apply(Layer.ControlChangingEffects_2, SubLayer.NA, abilityMap.get(effect.getId()), game); } layer = filterLayeredEffects(layerEffects, Layer.TextChangingEffects_3); for (ContinuousEffect effect: layer) { effect.apply(Layer.TextChangingEffects_3, SubLayer.CharacteristicDefining_7a, abilityMap.get(effect.getId()), game); } for (ContinuousEffect effect: layer) { effect.apply(Layer.TextChangingEffects_3, SubLayer.NA, abilityMap.get(effect.getId()), game); } layer = filterLayeredEffects(layerEffects, Layer.TypeChangingEffects_4); for (ContinuousEffect effect: layer) { effect.apply(Layer.TypeChangingEffects_4, SubLayer.CharacteristicDefining_7a, abilityMap.get(effect.getId()), game); } for (ContinuousEffect effect: layer) { effect.apply(Layer.TypeChangingEffects_4, SubLayer.NA, abilityMap.get(effect.getId()), game); } layer = filterLayeredEffects(layerEffects, Layer.ColorChangingEffects_5); for (ContinuousEffect effect: layer) { effect.apply(Layer.ColorChangingEffects_5, SubLayer.CharacteristicDefining_7a, abilityMap.get(effect.getId()), game); } for (ContinuousEffect effect: layer) { effect.apply(Layer.ColorChangingEffects_5, SubLayer.NA, abilityMap.get(effect.getId()), game); } layer = filterLayeredEffects(layerEffects, Layer.AbilityAddingRemovingEffects_6); for (ContinuousEffect effect: layer) { effect.apply(Layer.AbilityAddingRemovingEffects_6, SubLayer.CharacteristicDefining_7a, abilityMap.get(effect.getId()), game); } for (ContinuousEffect effect: layer) { effect.apply(Layer.AbilityAddingRemovingEffects_6, SubLayer.NA, abilityMap.get(effect.getId()), game); } layerEffects = getLayeredEffects(game); layer = filterLayeredEffects(layerEffects, Layer.PTChangingEffects_7); for (ContinuousEffect effect: layer) { effect.apply(Layer.PTChangingEffects_7, SubLayer.CharacteristicDefining_7a, abilityMap.get(effect.getId()), game); } for (ContinuousEffect effect: layer) { effect.apply(Layer.PTChangingEffects_7, SubLayer.SetPT_7b, abilityMap.get(effect.getId()), game); } for (ContinuousEffect effect: layer) { effect.apply(Layer.PTChangingEffects_7, SubLayer.ModifyPT_7c, abilityMap.get(effect.getId()), game); } applyCounters.apply(Layer.PTChangingEffects_7, SubLayer.Counters_7d, null, game); for (ContinuousEffect effect: layer) { effect.apply(Layer.PTChangingEffects_7, SubLayer.SwitchPT_e, abilityMap.get(effect.getId()), game); } layer = filterLayeredEffects(layerEffects, Layer.PlayerEffects); for (ContinuousEffect effect: layer) { effect.apply(Layer.PlayerEffects, SubLayer.NA, abilityMap.get(effect.getId()), game); } layer = filterLayeredEffects(layerEffects, Layer.RulesEffects); for (ContinuousEffect effect: layer) { effect.apply(Layer.RulesEffects, SubLayer.NA, abilityMap.get(effect.getId()), game); } } public void addEffect(ContinuousEffect effect, Ability source) { switch (effect.getEffectType()) { case REPLACEMENT: ReplacementEffect newReplacementEffect = (ReplacementEffect)effect; replacementEffects.add(newReplacementEffect); abilityMap.put(newReplacementEffect.getId(), source); break; case PREVENTION: PreventionEffect newPreventionEffect = (PreventionEffect)effect; preventionEffects.add(newPreventionEffect); abilityMap.put(newPreventionEffect.getId(), source); break; case RESTRICTION: RestrictionEffect newRestrictionEffect = (RestrictionEffect)effect; restrictionEffects.add(newRestrictionEffect); abilityMap.put(newRestrictionEffect.getId(), source); break; case REQUIREMENT: RequirementEffect newRequirementEffect = (RequirementEffect)effect; requirementEffects.add(newRequirementEffect); abilityMap.put(newRequirementEffect.getId(), source); break; case ASTHOUGH: AsThoughEffect newAsThoughEffect = (AsThoughEffect)effect; asThoughEffects.add(newAsThoughEffect); abilityMap.put(newAsThoughEffect.getId(), source); break; case COSTMODIFICATION: CostModificationEffect newCostModificationEffect = (CostModificationEffect)effect; costModificationEffects.add(newCostModificationEffect); abilityMap.put(newCostModificationEffect.getId(), source); break; default: ContinuousEffect newEffect = (ContinuousEffect)effect; layeredEffects.add(newEffect); abilityMap.put(newEffect.getId(), source); break; } } } class TimestampSorter implements Comparator { @Override public int compare(ContinuousEffect one, ContinuousEffect two) { return one.getTimestamp().compareTo(two.getTimestamp()); } }