* Fixes to handling of triggered abilities of tokens.

This commit is contained in:
LevelX2 2015-07-12 19:50:20 +02:00
parent 55966f3056
commit c53c09a59b
6 changed files with 102 additions and 86 deletions

View file

@ -34,9 +34,9 @@ import org.mage.test.serverside.base.CardTestPlayerBase;
/** /**
* Checks that a dies triggered ability of a Token works * Checks that a dies triggered ability of a Token works
*
* @author LevelX2 * @author LevelX2
*/ */
public class ReefWormTest extends CardTestPlayerBase { public class ReefWormTest extends CardTestPlayerBase {
@Test @Test
@ -50,16 +50,18 @@ public class ReefWormTest extends CardTestPlayerBase {
addCard(Zone.BATTLEFIELD, playerB, "Reef Worm", 1); addCard(Zone.BATTLEFIELD, playerB, "Reef Worm", 1);
castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Lightning Bolt", "Reef Worm"); castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Lightning Bolt", "Reef Worm");
castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Lightning Bolt", "Fish"); castSpell(1, PhaseStep.BEGIN_COMBAT, playerA, "Lightning Bolt", "Fish");
castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Lightning Bolt", "Whale"); castSpell(1, PhaseStep.POSTCOMBAT_MAIN, playerA, "Lightning Bolt", "Whale");
castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Lightning Bolt", "Whale"); castSpell(1, PhaseStep.POSTCOMBAT_MAIN, playerA, "Lightning Bolt", "Whale");
setStopAt(1, PhaseStep.BEGIN_COMBAT); setStopAt(1, PhaseStep.END_TURN);
execute(); execute();
assertLife(playerA, 20); assertLife(playerA, 20);
assertLife(playerB, 20); assertLife(playerB, 20);
assertGraveyardCount(playerA, "Lightning Bolt", 4);
assertPermanentCount(playerB, "Fish", 0); assertPermanentCount(playerB, "Fish", 0);
assertPermanentCount(playerB, "Whale", 0); assertPermanentCount(playerB, "Whale", 0);
assertPermanentCount(playerB, "Kraken", 1); assertPermanentCount(playerB, "Kraken", 1);

View file

@ -160,6 +160,19 @@ public class TriggeredAbilities extends ConcurrentHashMap<String, TriggeredAbili
sources.clear(); sources.clear();
} }
public void removeAbilitiesOfNonExistingSources(Game game) {
// e.g. Token that had triggered abilities
List<String> keysToRemove = new ArrayList<>();
for (Entry<String, TriggeredAbility> entry : this.entrySet()) {
if (game.getObject(entry.getValue().getSourceId()) == null) {
keysToRemove.add(entry.getKey());
}
}
for (String key : keysToRemove) {
remove(key);
}
}
public TriggeredAbilities copy() { public TriggeredAbilities copy() {
return new TriggeredAbilities(this); return new TriggeredAbilities(this);
} }

View file

@ -1,40 +1,40 @@
/* /*
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved. * Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without modification, are * Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met: * permitted provided that the following conditions are met:
* *
* 1. Redistributions of source code must retain the above copyright notice, this list of * 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer. * conditions and the following disclaimer.
* *
* 2. Redistributions in binary form must reproduce the above copyright notice, this list * 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 * of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution. * provided with the distribution.
* *
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED * 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 * 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 * 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 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 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 * 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 * 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 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* The views and conclusions contained in the software and documentation are those of the * 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 * authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com. * or implied, of BetaSteward_at_googlemail.com.
*/ */
package mage.abilities.common; package mage.abilities.common;
import mage.constants.Zone;
import mage.MageObject; import mage.MageObject;
import mage.abilities.effects.Effect; import mage.abilities.effects.Effect;
import mage.constants.Zone;
import mage.game.Game; import mage.game.Game;
import mage.game.events.GameEvent; import mage.game.events.GameEvent;
import mage.game.events.ZoneChangeEvent; import mage.game.events.ZoneChangeEvent;
import mage.game.permanent.Permanent; import mage.game.permanent.Permanent;
import mage.game.permanent.PermanentToken;
/** /**
* *
@ -58,7 +58,7 @@ public class DiesTriggeredAbility extends ZoneChangeTriggeredAbility {
public boolean isInUseableZone(Game game, MageObject source, GameEvent event) { public boolean isInUseableZone(Game game, MageObject source, GameEvent event) {
// check it was previously on battlefield // check it was previously on battlefield
Permanent before = ((ZoneChangeEvent) event).getTarget(); Permanent before = ((ZoneChangeEvent) event).getTarget();
if (!this.hasSourceObjectAbility(game, before, event)) { if (!(before instanceof PermanentToken) && !this.hasSourceObjectAbility(game, before, event)) {
return false; return false;
} }
// check now it is in graveyard // check now it is in graveyard
@ -66,6 +66,14 @@ public class DiesTriggeredAbility extends ZoneChangeTriggeredAbility {
return before != null && after != null && Zone.GRAVEYARD.match(after); return before != null && after != null && Zone.GRAVEYARD.match(after);
} }
@Override
public boolean checkEventType(GameEvent event, Game game) {
if (super.checkEventType(event, game)) {
return ((ZoneChangeEvent) event).getFromZone().equals(Zone.BATTLEFIELD) && ((ZoneChangeEvent) event).getToZone().equals(Zone.GRAVEYARD);
}
return event.getType() == GameEvent.EventType.ZONE_CHANGE;
}
@Override @Override
public DiesTriggeredAbility copy() { public DiesTriggeredAbility copy() {
return new DiesTriggeredAbility(this); return new DiesTriggeredAbility(this);
@ -80,7 +88,7 @@ public class DiesTriggeredAbility extends ZoneChangeTriggeredAbility {
return false; return false;
} }
} }
for (Effect effect: getEffects()) { for (Effect effect : getEffects()) {
effect.setValue("diedPermanent", zEvent.getTarget()); effect.setValue("diedPermanent", zEvent.getTarget());
} }
return true; return true;

View file

@ -1,31 +1,30 @@
/* /*
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved. * Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without modification, are * Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met: * permitted provided that the following conditions are met:
* *
* 1. Redistributions of source code must retain the above copyright notice, this list of * 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer. * conditions and the following disclaimer.
* *
* 2. Redistributions in binary form must reproduce the above copyright notice, this list * 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 * of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution. * provided with the distribution.
* *
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED * 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 * 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 * 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 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 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 * 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 * 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 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* The views and conclusions contained in the software and documentation are those of the * 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 * authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com. * or implied, of BetaSteward_at_googlemail.com.
*/ */
package mage.game.permanent; package mage.game.permanent;
import java.util.UUID; import java.util.UUID;
@ -78,7 +77,7 @@ public class PermanentToken extends PermanentImpl {
} }
this.abilities.setControllerId(this.controllerId); this.abilities.setControllerId(this.controllerId);
this.manaCost.clear(); this.manaCost.clear();
for (ManaCost cost: token.getManaCost()) { for (ManaCost cost : token.getManaCost()) {
this.getManaCost().add(cost.copy()); this.getManaCost().add(cost.copy());
} }
this.cardType = token.getCardType(); this.cardType = token.getCardType();
@ -95,8 +94,7 @@ public class PermanentToken extends PermanentImpl {
game.rememberLKI(objectId, Zone.BATTLEFIELD, this); game.rememberLKI(objectId, Zone.BATTLEFIELD, this);
if (game.getPlayer(controllerId).removeFromBattlefield(this, game)) { if (game.getPlayer(controllerId).removeFromBattlefield(this, game)) {
game.setZone(objectId, zone); // needed for triggered dies abilities game.setZone(objectId, zone); // needed for triggered dies abilities
game.addSimultaneousEvent(new ZoneChangeEvent(this, this.getControllerId(), Zone.BATTLEFIELD, zone)); // causes maybe a problem with Dies trigger of Tokens game.addSimultaneousEvent(new ZoneChangeEvent(this, this.getControllerId(), Zone.BATTLEFIELD, zone));
// game.getState().removeTriggersOfSourceId(this.getId());// if token is gone endless triggered abilities have to be removed
return true; return true;
} }
} }
@ -142,5 +140,4 @@ public class PermanentToken extends PermanentImpl {
} }
} }
} }

View file

@ -25,7 +25,6 @@
* authors and should not be interpreted as representing official policies, either expressed * authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com. * or implied, of BetaSteward_at_googlemail.com.
*/ */
package mage.game.turn; package mage.game.turn;
import java.io.Serializable; import java.io.Serializable;
@ -57,7 +56,8 @@ public abstract class Phase implements Serializable {
public abstract Phase copy(); public abstract Phase copy();
public Phase() {} public Phase() {
}
public Phase(final Phase phase) { public Phase(final Phase phase) {
this.type = phase.type; this.type = phase.type;
@ -69,7 +69,7 @@ public abstract class Phase implements Serializable {
this.currentStep = phase.currentStep.copy(); this.currentStep = phase.currentStep.copy();
} }
this.count = phase.count; this.count = phase.count;
for (Step step: phase.steps) { for (Step step : phase.steps) {
this.steps.add(step.copy()); this.steps.add(step.copy());
} }
} }
@ -103,7 +103,7 @@ public abstract class Phase implements Serializable {
if (beginPhase(game, activePlayerId)) { if (beginPhase(game, activePlayerId)) {
for (Step step: steps) { for (Step step : steps) {
if (game.isPaused() || game.gameOver(null)) { if (game.isPaused() || game.gameOver(null)) {
return false; return false;
} }
@ -184,6 +184,7 @@ public abstract class Phase implements Serializable {
public void endPhase(Game game, UUID activePlayerId) { public void endPhase(Game game, UUID activePlayerId) {
game.fireEvent(new GameEvent(postEvent, null, null, activePlayerId)); game.fireEvent(new GameEvent(postEvent, null, null, activePlayerId));
game.getState().getTriggers().removeAbilitiesOfNonExistingSources(game); // e.g. tokens that left the battlefield
} }
public void prePriority(Game game, UUID activePlayerId) { public void prePriority(Game game, UUID activePlayerId) {
@ -204,7 +205,7 @@ public abstract class Phase implements Serializable {
prePriority(game, activePlayerId); prePriority(game, activePlayerId);
if (!game.isPaused() && !game.gameOver(null)) { if (!game.isPaused() && !game.gameOver(null)) {
currentStep.priority(game, activePlayerId, false); currentStep.priority(game, activePlayerId, false);
if(game.executingRollback()) { if (game.executingRollback()) {
return; return;
} }
} }
@ -225,8 +226,7 @@ public abstract class Phase implements Serializable {
if (wasPaused) { if (wasPaused) {
currentStep.resumeBeginStep(game, activePlayerId); currentStep.resumeBeginStep(game, activePlayerId);
resuming = false; resuming = false;
} } else {
else {
prePriority(game, activePlayerId); prePriority(game, activePlayerId);
} }
case PRIORITY: case PRIORITY:

View file

@ -116,7 +116,6 @@ import mage.game.events.ZoneChangeGroupEvent;
import mage.game.match.MatchPlayer; import mage.game.match.MatchPlayer;
import mage.game.permanent.Permanent; import mage.game.permanent.Permanent;
import mage.game.permanent.PermanentCard; import mage.game.permanent.PermanentCard;
import mage.game.permanent.PermanentToken;
import mage.game.stack.Spell; import mage.game.stack.Spell;
import mage.game.stack.StackAbility; import mage.game.stack.StackAbility;
import mage.game.stack.StackObject; import mage.game.stack.StackObject;
@ -813,9 +812,6 @@ public abstract class PlayerImpl implements Player, Serializable {
pairedCard.clearPairedCard(); pairedCard.clearPairedCard();
} }
} }
if (permanent instanceof PermanentToken) {
game.getState().getTriggers().removeAbilitiesOfSource(permanent.getId());
}
return true; return true;
} }