Some changes to restrained event handling (simultaneous events) (fixes #897).

This commit is contained in:
LevelX2 2015-04-19 10:43:12 +02:00
parent a5967d9b2a
commit 568f62c66f
8 changed files with 1023 additions and 935 deletions

View file

@ -1,145 +1,146 @@
/*
* 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.sets.championsofkamigawa;
import java.util.UUID;
import mage.abilities.Ability;
import mage.abilities.condition.Condition;
import mage.abilities.decorator.ConditionalOneShotEffect;
import mage.abilities.effects.Effect;
import mage.abilities.effects.common.discard.DiscardControllerEffect;
import mage.abilities.effects.common.DrawCardSourceControllerEffect;
import mage.abilities.effects.common.search.SearchLibraryPutInPlayEffect;
import mage.cards.CardImpl;
import mage.constants.CardType;
import mage.constants.Rarity;
import mage.constants.WatcherScope;
import mage.filter.common.FilterCreatureCard;
import mage.filter.predicate.mageobject.NamePredicate;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.events.GameEvent.EventType;
import mage.game.stack.Spell;
import mage.target.common.TargetCardInLibrary;
import mage.watchers.Watcher;
/**
*
* @author LevelX2
*/
public class SiftThroughSands extends CardImpl {
private static final String rule = "If you've cast a spell named Peer Through Depths and a spell named Reach Through Mists this turn, you may search your library for a card named The Unspeakable, put it onto the battlefield, then shuffle your library";
private static final FilterCreatureCard filter = new FilterCreatureCard("a card named The Unspeakable");
static {
filter.add(new NamePredicate("The Unspeakable"));
}
public SiftThroughSands(UUID ownerId) {
super(ownerId, 84, "Sift Through Sands", Rarity.COMMON, new CardType[]{CardType.INSTANT}, "{1}{U}{U}");
this.expansionSetCode = "CHK";
this.subtype.add("Arcane");
this.color.setBlue(true);
// Draw two cards, then discard a card.
this.getSpellAbility().addEffect(new DrawCardSourceControllerEffect(2));
Effect effect = new DiscardControllerEffect(1);
effect.setText(", then discard a card");
this.getSpellAbility().addEffect(effect);
// If you've cast a spell named Peer Through Depths and a spell named Reach Through Mists this turn, you may search your library for a card named The Unspeakable, put it onto the battlefield, then shuffle your library.
this.getSpellAbility().addEffect(new ConditionalOneShotEffect(new SearchLibraryPutInPlayEffect(new TargetCardInLibrary(filter), false, true), new SiftThroughSandsCondition(), rule));
this.getSpellAbility().addWatcher(new SiftThroughSandsWatcher());
}
public SiftThroughSands(final SiftThroughSands card) {
super(card);
}
@Override
public SiftThroughSands copy() {
return new SiftThroughSands(this);
}
}
class SiftThroughSandsCondition implements Condition {
@Override
public boolean apply(Game game, Ability source) {
SiftThroughSandsWatcher watcher = (SiftThroughSandsWatcher) game.getState().getWatchers().get("SiftThroughSandsWatcher", source.getControllerId());
if (watcher != null) {
return watcher.conditionMet();
}
return false;
}
}
class SiftThroughSandsWatcher extends Watcher {
boolean castPeerThroughDepths = false;
boolean castReachThroughMists = false;
public SiftThroughSandsWatcher() {
super("SiftThroughSandsWatcher", WatcherScope.PLAYER);
}
public SiftThroughSandsWatcher(final SiftThroughSandsWatcher watcher) {
super(watcher);
this.castPeerThroughDepths = watcher.castPeerThroughDepths;
this.castReachThroughMists = watcher.castReachThroughMists;
}
@Override
public SiftThroughSandsWatcher copy() {
return new SiftThroughSandsWatcher(this);
}
@Override
public void watch(GameEvent event, Game game) {
if (condition == true) { //no need to check - condition has already occured
return;
}
if (event.getType() == EventType.SPELL_CAST
&& controllerId == event.getPlayerId()) {
Spell spell = game.getStack().getSpell(event.getTargetId());
if (spell.getCard().getName().equals("Peer Through Depths")) {
castPeerThroughDepths = true;
} else if (spell.getCard().getName().equals("Reach Through Mists")) {
castReachThroughMists = true;
}
condition = castPeerThroughDepths && castReachThroughMists;
}
}
@Override
public void reset() {
super.reset();
this.castPeerThroughDepths = false;
this.castReachThroughMists = false;
}
}
/*
* 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.sets.championsofkamigawa;
import java.util.UUID;
import mage.abilities.Ability;
import mage.abilities.condition.Condition;
import mage.abilities.decorator.ConditionalOneShotEffect;
import mage.abilities.effects.Effect;
import mage.abilities.effects.common.discard.DiscardControllerEffect;
import mage.abilities.effects.common.DrawCardSourceControllerEffect;
import mage.abilities.effects.common.search.SearchLibraryPutInPlayEffect;
import mage.cards.CardImpl;
import mage.constants.CardType;
import mage.constants.Rarity;
import mage.constants.WatcherScope;
import mage.filter.common.FilterCreatureCard;
import mage.filter.predicate.mageobject.NamePredicate;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.events.GameEvent.EventType;
import mage.game.stack.Spell;
import mage.target.common.TargetCardInLibrary;
import mage.watchers.Watcher;
/**
*
* @author LevelX2
*/
public class SiftThroughSands extends CardImpl {
private static final String rule = "If you've cast a spell named Peer Through Depths and a spell named Reach Through Mists this turn, you may search your library for a card named The Unspeakable, put it onto the battlefield, then shuffle your library";
private static final FilterCreatureCard filter = new FilterCreatureCard("a card named The Unspeakable");
static {
filter.add(new NamePredicate("The Unspeakable"));
}
public SiftThroughSands(UUID ownerId) {
super(ownerId, 84, "Sift Through Sands", Rarity.COMMON, new CardType[]{CardType.INSTANT}, "{1}{U}{U}");
this.expansionSetCode = "CHK";
this.subtype.add("Arcane");
this.color.setBlue(true);
// Draw two cards, then discard a card.
this.getSpellAbility().addEffect(new DrawCardSourceControllerEffect(2));
Effect effect = new DiscardControllerEffect(1);
effect.setText(", then discard a card");
this.getSpellAbility().addEffect(effect);
// If you've cast a spell named Peer Through Depths and a spell named Reach Through Mists this turn, you may search your library for a card named The Unspeakable, put it onto the battlefield, then shuffle your library.
this.getSpellAbility().addEffect(new ConditionalOneShotEffect(new SearchLibraryPutInPlayEffect(new TargetCardInLibrary(filter), false, true), new SiftThroughSandsCondition(), rule));
this.getSpellAbility().addWatcher(new SiftThroughSandsWatcher());
}
public SiftThroughSands(final SiftThroughSands card) {
super(card);
}
@Override
public SiftThroughSands copy() {
return new SiftThroughSands(this);
}
}
class SiftThroughSandsCondition implements Condition {
@Override
public boolean apply(Game game, Ability source) {
SiftThroughSandsWatcher watcher = (SiftThroughSandsWatcher) game.getState().getWatchers().get("SiftThroughSandsWatcher", source.getControllerId());
if (watcher != null) {
return watcher.conditionMet();
}
return false;
}
}
class SiftThroughSandsWatcher extends Watcher {
boolean castPeerThroughDepths = false;
boolean castReachThroughMists = false;
public SiftThroughSandsWatcher() {
super("SiftThroughSandsWatcher", WatcherScope.PLAYER);
}
public SiftThroughSandsWatcher(final SiftThroughSandsWatcher watcher) {
super(watcher);
this.castPeerThroughDepths = watcher.castPeerThroughDepths;
this.castReachThroughMists = watcher.castReachThroughMists;
}
@Override
public SiftThroughSandsWatcher copy() {
return new SiftThroughSandsWatcher(this);
}
@Override
public void watch(GameEvent event, Game game) {
if (condition == true) { //no need to check - condition has already occured
return;
}
if (event.getType() == EventType.SPELL_CAST
&& controllerId == event.getPlayerId()) {
Spell spell = game.getStack().getSpell(event.getTargetId());
if (spell.getCard().getName().equals("Peer Through Depths")) {
castPeerThroughDepths = true;
} else if (spell.getCard().getName().equals("Reach Through Mists")) {
castReachThroughMists = true;
}
condition = castPeerThroughDepths && castReachThroughMists;
}
}
@Override
public void reset() {
super.reset();
this.castPeerThroughDepths = false;
this.castReachThroughMists = false;
}
}

View file

@ -70,7 +70,6 @@ public class DungeonGeists extends CardImpl {
this.expansionSetCode = "DKA";
this.subtype.add("Spirit");
this.color.setBlue(true);
this.power = new MageInt(3);
this.toughness = new MageInt(3);
@ -112,40 +111,52 @@ class DungeonGeistsEffect extends ContinuousRuleModifyingEffectImpl {
}
@Override
public boolean apply(Game game, Ability source) {
return false;
public boolean checksEventType(GameEvent event, Game game) {
return event.getType() == GameEvent.EventType.UNTAP || event.getType() == GameEvent.EventType.ZONE_CHANGE || event.getType() == GameEvent.EventType.LOST_CONTROL;
}
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
// Source must be on the battlefield (it's neccessary to check here because if as response to the enter
// the battlefield triggered ability the source dies (or will be exiled), then the ZONE_CHANGE or LOST_CONTROL
// event will happen before this effect is applied ever)
Permanent sourcePermanent = game.getPermanent(source.getSourceId());
Permanent sourcePermanent = (Permanent) source.getSourceObjectIfItStillExists(game);
if (sourcePermanent == null || !sourcePermanent.getControllerId().equals(source.getControllerId())) {
this.used = true;
discard();
return false;
}
if (event.getType() == GameEvent.EventType.LOST_CONTROL) {
if (event.getTargetId().equals(source.getSourceId())) {
discard();
return false;
}
switch(event.getType()) {
case ZONE_CHANGE:
// end effect if source does a zone move
if (event.getTargetId().equals(source.getSourceId())) {
ZoneChangeEvent zEvent = (ZoneChangeEvent)event;
if (zEvent.getFromZone() == Zone.BATTLEFIELD) {
discard();
return false;
}
}
break;
case UNTAP:
// prevent to untap the target creature
if (game.getTurn().getStepType() == PhaseStep.UNTAP && event.getTargetId().equals(targetPointer.getFirst(game, source))) {
Permanent targetCreature = game.getPermanent(targetPointer.getFirst(game, source));
if (targetCreature != null) {
return targetCreature.getControllerId().equals(game.getActivePlayerId());
} else {
discard();
return false;
}
}
break;
case LOST_CONTROL:
// end effect if source control is changed
if (event.getTargetId().equals(source.getSourceId())) {
discard();
return false;
}
break;
}
if (event.getType() == GameEvent.EventType.ZONE_CHANGE && event.getTargetId().equals(source.getSourceId())) {
ZoneChangeEvent zEvent = (ZoneChangeEvent)event;
if (zEvent.getFromZone() == Zone.BATTLEFIELD) {
discard();
return false;
}
}
if (game.getTurn().getStepType() == PhaseStep.UNTAP && event.getType() == GameEvent.EventType.UNTAP) {
if (event.getTargetId().equals(targetPointer.getFirst(game, source))) {
return true;
}
}
return false;
}
}