Test and fix for triggered abilities of double faced cards

This commit is contained in:
magenoxx 2012-06-11 20:34:33 +04:00
parent 17dbe0ae57
commit f641ffe77b
2 changed files with 64 additions and 16 deletions

View file

@ -6,7 +6,7 @@ import org.mage.test.serverside.base.CardTestPlayerBase;
/** /**
* *
* @author BetaSteward * @author BetaSteward, noxx
*/ */
public class HuntmasterOfTheFellsTest extends CardTestPlayerBase { public class HuntmasterOfTheFellsTest extends CardTestPlayerBase {
@ -28,5 +28,31 @@ public class HuntmasterOfTheFellsTest extends CardTestPlayerBase {
assertPermanentCount(playerA, "Ravager of the Fells", 1); assertPermanentCount(playerA, "Ravager of the Fells", 1);
assertPermanentCount(playerB, "Ornithopter", 0); assertPermanentCount(playerB, "Ornithopter", 0);
} }
/**
* Tests first trigger happens both on enter battlefield and transform events
*/
@Test
public void testCard2() {
addCard(Constants.Zone.BATTLEFIELD, playerA, "Forest", 3);
addCard(Constants.Zone.BATTLEFIELD, playerA, "Mountain");
addCard(Constants.Zone.HAND, playerA, "Huntmaster of the Fells");
addCard(Constants.Zone.BATTLEFIELD, playerB, "Ornithopter");
addCard(Constants.Zone.BATTLEFIELD, playerB, "Mountain", 2);
addCard(Constants.Zone.HAND, playerB, "Lightning Bolt", 2);
castSpell(1, Constants.PhaseStep.PRECOMBAT_MAIN, playerA, "Huntmaster of the Fells");
castSpell(3, Constants.PhaseStep.PRECOMBAT_MAIN, playerB, "Lightning Bolt", playerA);
castSpell(3, Constants.PhaseStep.PRECOMBAT_MAIN, playerB, "Lightning Bolt", playerA);
setStopAt(4, Constants.PhaseStep.DRAW);
execute();
assertLife(playerA, 18); // -6 damage, +4 life
assertLife(playerB, 18);
assertPermanentCount(playerA, "Wolf", 2);
assertPermanentCount(playerA, "Ravager of the Fells", 0); // transformed back
assertPermanentCount(playerA, "Huntmaster of the Fells", 1);
assertPermanentCount(playerB, "Ornithopter", 0);
}
} }

View file

@ -32,6 +32,7 @@ import mage.MageObject;
import mage.game.Game; import mage.game.Game;
import mage.game.events.GameEvent; import mage.game.events.GameEvent;
import mage.game.permanent.Permanent; import mage.game.permanent.Permanent;
import mage.game.permanent.PermanentCard;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -54,26 +55,47 @@ public class TriggeredAbilities extends HashMap<UUID, TriggeredAbility> {
public void checkTriggers(GameEvent event, Game game) { public void checkTriggers(GameEvent event, Game game) {
for (TriggeredAbility ability: this.values()) { for (TriggeredAbility ability: this.values()) {
if (ability.isInUseableZone(game, true)) { if (ability.isInUseableZone(game, true)) {
MageObject object = game.getPermanent(ability.getSourceId()); MageObject object = getMageObject(event, game, ability);
if (object == null) { if (object != null) {
object = game.getLastKnownInformation(ability.getSourceId(), event.getZone()); if (checkAbilityStillExists(ability, object)) {
if (object == null) { if (object instanceof Permanent) {
object = game.getObject(ability.getSourceId()); ability.setControllerId(((Permanent) object).getControllerId());
} }
} if (ability.checkTrigger(event, game)) {
if (object != null && object.getAbilities().contains(ability)) { UUID controllerId = ability.getControllerId();
if (object instanceof Permanent) { ability.trigger(game, controllerId);
ability.setControllerId(((Permanent) object).getControllerId()); }
}
if (ability.checkTrigger(event, game)) {
UUID controllerId = ability.getControllerId();
ability.trigger(game, controllerId);
} }
} }
} }
} }
} }
private boolean checkAbilityStillExists(TriggeredAbility ability, MageObject object) {
boolean exists = true;
if (!object.getAbilities().contains(ability)) {
exists = false;
if (object instanceof PermanentCard) {
PermanentCard permanent = (PermanentCard)object;
if (permanent.canTransform()) {
exists = permanent.getCard().getAbilities().contains(ability);
}
}
}
return exists;
}
private MageObject getMageObject(GameEvent event, Game game, TriggeredAbility ability) {
MageObject object = game.getPermanent(ability.getSourceId());
if (object == null) {
object = game.getLastKnownInformation(ability.getSourceId(), event.getZone());
if (object == null) {
object = game.getObject(ability.getSourceId());
}
}
return object;
}
public void add(TriggeredAbility ability) { public void add(TriggeredAbility ability) {
this.put(ability.getId(), ability); this.put(ability.getId(), ability);
} }