* Annihilator - Fixed that the triggered ability did not resolve correctly, if the source was removed from battlefield before resolving.

This commit is contained in:
LevelX2 2015-01-31 19:49:37 +01:00
parent e6b85901b8
commit 5d0a94c1b9

View file

@ -27,14 +27,14 @@
*/ */
package mage.abilities.keyword; package mage.abilities.keyword;
import mage.constants.Outcome; import java.util.UUID;
import mage.constants.TargetController;
import mage.constants.Zone;
import mage.abilities.Ability; import mage.abilities.Ability;
import mage.abilities.TriggeredAbilityImpl; import mage.abilities.TriggeredAbilityImpl;
import mage.abilities.effects.Effect;
import mage.abilities.effects.OneShotEffect; import mage.abilities.effects.OneShotEffect;
import mage.constants.Outcome;
import mage.constants.Zone;
import mage.filter.common.FilterControlledPermanent; import mage.filter.common.FilterControlledPermanent;
import mage.filter.predicate.permanent.ControllerPredicate;
import mage.game.Game; import mage.game.Game;
import mage.game.events.GameEvent; import mage.game.events.GameEvent;
import mage.game.events.GameEvent.EventType; import mage.game.events.GameEvent.EventType;
@ -42,8 +42,7 @@ import mage.game.permanent.Permanent;
import mage.players.Player; import mage.players.Player;
import mage.target.Target; import mage.target.Target;
import mage.target.common.TargetControlledPermanent; import mage.target.common.TargetControlledPermanent;
import mage.util.CardUtil;
import java.util.UUID;
/** /**
* 702.84. Annihilator 702.84a Annihilator is a triggered ability. "Annihilator * 702.84. Annihilator 702.84a Annihilator is a triggered ability. "Annihilator
@ -72,14 +71,22 @@ public class AnnihilatorAbility extends TriggeredAbilityImpl {
@Override @Override
public boolean checkTrigger(GameEvent event, Game game) { public boolean checkTrigger(GameEvent event, Game game) {
if (event.getType() == EventType.ATTACKER_DECLARED && event.getSourceId().equals(this.getSourceId())) { if (event.getType() == EventType.ATTACKER_DECLARED && event.getSourceId().equals(this.getSourceId())) {
UUID defendingPlayerId = game.getCombat().getDefendingPlayerId(sourceId, game);
if (defendingPlayerId != null) {
// the id has to be set here because the source can be leave battlefield
for(Effect effect : getEffects()) {
effect.setValue("defendingPlayerId", defendingPlayerId);
}
return true; return true;
} }
}
return false; return false;
} }
@Override @Override
public String getRule() { public String getRule() {
return "Annihilator " + count; return "Annihilator " + count + "<i>(Whenever this creature attacks, defending player sacrifices " +
(count == 1 ? "a permanent": CardUtil.numberToText(count) + " permanents") + ".)</i>";
} }
@Override @Override
@ -106,39 +113,26 @@ class AnnihilatorEffect extends OneShotEffect {
@Override @Override
public boolean apply(Game game, Ability source) { public boolean apply(Game game, Ability source) {
UUID defenderId = game.getCombat().getDefenderId(source.getSourceId()); UUID defendingPlayerId = (UUID) getValue("defendingPlayerId");
Player player = game.getPlayer(defenderId); Player player = null;
if (defendingPlayerId != null) {
//Defender may be a planeswalker. player= game.getPlayer(defendingPlayerId);
if (player == null) {
Permanent permanent = game.getPermanent(defenderId);
if (permanent != null) {
player = game.getPlayer(permanent.getControllerId());
} }
}
if (player != null) { if (player != null) {
int amount = Math.min(count, game.getBattlefield().countAll(filter, player.getId(), game)); int amount = Math.min(count, game.getBattlefield().countAll(filter, player.getId(), game));
Target target = new TargetControlledPermanent(amount, amount, filter, false); Target target = new TargetControlledPermanent(amount, amount, filter, false);
//A spell or ability could have removed the only legal target this player
//had, if thats the case this ability should fizzle.
if (target.canChoose(player.getId(), game)) { if (target.canChoose(player.getId(), game)) {
boolean abilityApplied = false;
while (!target.isChosen() && target.canChoose(player.getId(), game) && player.isInGame()) { while (!target.isChosen() && target.canChoose(player.getId(), game) && player.isInGame()) {
player.choose(Outcome.Sacrifice, target, source.getSourceId(), game); player.choose(Outcome.Sacrifice, target, source.getSourceId(), game);
} }
for (int idx = 0; idx < target.getTargets().size(); idx++) { for (int idx = 0; idx < target.getTargets().size(); idx++) {
Permanent permanent = game.getPermanent((UUID) target.getTargets().get(idx)); Permanent permanent = game.getPermanent((UUID) target.getTargets().get(idx));
if (permanent != null) { if (permanent != null) {
abilityApplied |= permanent.sacrifice(source.getSourceId(), game); permanent.sacrifice(source.getSourceId(), game);
} }
} }
return abilityApplied;
} }
return true;
} }
return false; return false;
} }