* Mizzium Meddler - Fixed target change handling

This commit is contained in:
LevelX2 2015-10-09 16:30:28 +02:00
parent 1739bcab56
commit abc6b11c32
3 changed files with 136 additions and 220 deletions

View file

@ -29,23 +29,14 @@ package mage.sets.magicorigins;
import java.util.UUID; import java.util.UUID;
import mage.MageInt; import mage.MageInt;
import mage.MageObject;
import mage.abilities.Ability; import mage.abilities.Ability;
import mage.abilities.common.EntersBattlefieldTriggeredAbility; import mage.abilities.common.EntersBattlefieldTriggeredAbility;
import mage.abilities.effects.OneShotEffect; import mage.abilities.effects.common.ChangeATargetOfTargetSpellAbilityToSourceEffect;
import mage.abilities.keyword.FlashAbility; import mage.abilities.keyword.FlashAbility;
import mage.cards.CardImpl; import mage.cards.CardImpl;
import mage.constants.CardType; import mage.constants.CardType;
import mage.constants.Outcome;
import mage.constants.Rarity; import mage.constants.Rarity;
import mage.game.Game;
import mage.game.stack.Spell;
import mage.game.stack.StackAbility;
import mage.game.stack.StackObject;
import mage.players.Player;
import mage.target.Target;
import mage.target.TargetStackObject; import mage.target.TargetStackObject;
import mage.target.Targets;
/** /**
* *
@ -64,8 +55,8 @@ public class MizziumMeddler extends CardImpl {
// Flash // Flash
this.addAbility(FlashAbility.getInstance()); this.addAbility(FlashAbility.getInstance());
// When Mizzium Meddler enters the battlefield, change a target of target spell or ability to Mizzium Meddler. // When Mizzium Meddler enters the battlefield, you may change a target of target spell or ability to Mizzium Meddler.
Ability ability = new EntersBattlefieldTriggeredAbility(new MizziumMeddlerEffect()); Ability ability = new EntersBattlefieldTriggeredAbility(new ChangeATargetOfTargetSpellAbilityToSourceEffect(), true);
ability.addTarget(new TargetStackObject()); ability.addTarget(new TargetStackObject());
this.addAbility(ability); this.addAbility(ability);
} }
@ -79,92 +70,3 @@ public class MizziumMeddler extends CardImpl {
return new MizziumMeddler(this); return new MizziumMeddler(this);
} }
} }
class MizziumMeddlerEffect extends OneShotEffect {
public MizziumMeddlerEffect() {
super(Outcome.Neutral);
staticText = "Change a target of target spell or ability to {this}";
}
public MizziumMeddlerEffect(final MizziumMeddlerEffect effect) {
super(effect);
}
@Override
public boolean apply(Game game, Ability source) {
StackObject stackObject = game.getStack().getStackObject(source.getFirstTarget());
MageObject sourceObject = game.getObject(source.getSourceId());
if (stackObject != null && sourceObject != null) {
Targets targets;
Ability sourceAbility;
MageObject oldTarget = null;
if (stackObject instanceof Spell) {
Spell spell = (Spell)stackObject;
sourceAbility = spell.getSpellAbility();
targets = spell.getSpellAbility().getTargets();
} else if (stackObject instanceof StackAbility) {
StackAbility stackAbility = (StackAbility)stackObject;
sourceAbility = stackAbility;
targets = stackAbility.getTargets();
} else {
return false;
}
boolean twoTimesTarget = false;
if (targets.size() == 1 && targets.get(0).getTargets().size() == 1) {
Target target = targets.get(0);
if (target.canTarget(stackObject.getControllerId(), source.getSourceId(), sourceAbility, game)) {
oldTarget = game.getObject(targets.getFirstTarget());
target.clearChosen();
// The source is still the spell on the stack
target.addTarget(source.getSourceId(), stackObject.getStackAbility(), game);
}
} else {
Player player = game.getPlayer(source.getControllerId());
for (Target target: targets) {
for (UUID targetId: target.getTargets()) {
MageObject object = game.getObject(targetId);
String name;
if (object == null) {
Player targetPlayer = game.getPlayer(targetId);
name = targetPlayer.getLogName();
} else {
name = object.getName();
}
if (!targetId.equals(source.getSourceId()) && target.getTargets().contains(source.getSourceId())) {
// you can't change this target to MizziumMeddler because MizziumMeddler is already another targetId of that target.
twoTimesTarget = true;
continue;
}
if (name != null && player.chooseUse(Outcome.Neutral, new StringBuilder("Change target from ").append(name).append(" to ").append(sourceObject.getName()).append("?").toString(), source, game)) {
if (target.canTarget(stackObject.getControllerId(), source.getSourceId(), sourceAbility, game)) {
oldTarget = game.getObject(targets.getFirstTarget());
target.remove(targetId);
// The source is still the spell on the stack
target.addTarget(source.getSourceId(), stackObject.getStackAbility(), game);
break;
}
}
}
}
}
if (oldTarget != null) {
game.informPlayers(sourceObject.getLogName() + ": Changed target of " +stackObject.getLogName() + " from " + oldTarget.getLogName() + " to " + sourceObject.getLogName());
} else {
if (twoTimesTarget) {
game.informPlayers(sourceObject.getLogName() + ": Target not changed to " + sourceObject.getLogName() + " because its not valid to target it twice for " + stackObject.getName());
} else {
game.informPlayers(sourceObject.getLogName() + ": Target not changed to " + sourceObject.getLogName() + " because its no valid target for " + stackObject.getName());
}
}
return true;
}
return false;
}
@Override
public MizziumMeddlerEffect copy() {
return new MizziumMeddlerEffect(this);
}
}

View file

@ -29,24 +29,15 @@ package mage.sets.newphyrexia;
import java.util.UUID; import java.util.UUID;
import mage.MageInt; import mage.MageInt;
import mage.MageObject;
import mage.abilities.Ability; import mage.abilities.Ability;
import mage.abilities.common.SimpleActivatedAbility; import mage.abilities.common.SimpleActivatedAbility;
import mage.abilities.costs.mana.ManaCostsImpl; import mage.abilities.costs.mana.ManaCostsImpl;
import mage.abilities.effects.OneShotEffect; import mage.abilities.effects.common.ChangeATargetOfTargetSpellAbilityToSourceEffect;
import mage.cards.CardImpl; import mage.cards.CardImpl;
import mage.constants.CardType; import mage.constants.CardType;
import mage.constants.Outcome;
import mage.constants.Rarity; import mage.constants.Rarity;
import mage.constants.Zone; import mage.constants.Zone;
import mage.game.Game;
import mage.game.stack.Spell;
import mage.game.stack.StackAbility;
import mage.game.stack.StackObject;
import mage.players.Player;
import mage.target.Target;
import mage.target.TargetStackObject; import mage.target.TargetStackObject;
import mage.target.Targets;
/** /**
* *
@ -62,7 +53,7 @@ public class Spellskite extends CardImpl {
this.toughness = new MageInt(4); this.toughness = new MageInt(4);
// {UP}: Change a target of target spell or ability to Spellskite. // {UP}: Change a target of target spell or ability to Spellskite.
Ability ability = new SimpleActivatedAbility(Zone.BATTLEFIELD, new SpellskiteEffect(), new ManaCostsImpl("{UP}")); Ability ability = new SimpleActivatedAbility(Zone.BATTLEFIELD, new ChangeATargetOfTargetSpellAbilityToSourceEffect(), new ManaCostsImpl("{UP}"));
ability.addTarget(new TargetStackObject()); ability.addTarget(new TargetStackObject());
this.addAbility(ability); this.addAbility(ability);
} }
@ -76,110 +67,3 @@ public class Spellskite extends CardImpl {
return new Spellskite(this); return new Spellskite(this);
} }
} }
class SpellskiteEffect extends OneShotEffect {
public SpellskiteEffect() {
super(Outcome.Neutral);
staticText = "Change a target of target spell or ability to {this}";
}
public SpellskiteEffect(final SpellskiteEffect effect) {
super(effect);
}
@Override
public boolean apply(Game game, Ability source) {
StackObject stackObject = game.getStack().getStackObject(source.getFirstTarget());
MageObject sourceObject = game.getObject(source.getSourceId());
if (stackObject != null && sourceObject != null) {
Targets targets = new Targets();
Ability sourceAbility;
String oldTargetName = null;
if (stackObject instanceof Spell) {
Spell spell = (Spell) stackObject;
sourceAbility = spell.getSpellAbility();
} else if (stackObject instanceof StackAbility) {
StackAbility stackAbility = (StackAbility) stackObject;
sourceAbility = stackAbility;
} else {
return false;
}
for (UUID modeId : sourceAbility.getModes().getSelectedModes()) {
sourceAbility.getModes().setActiveMode(modeId);
targets.addAll(sourceAbility.getTargets());
}
boolean twoTimesTarget = false;
if (targets.size() == 1 && targets.get(0).getTargets().size() == 1) {
Target target = targets.get(0);
if (target.canTarget(stackObject.getControllerId(), source.getSourceId(), sourceAbility, game)) {
oldTargetName = getTargetName(targets.getFirstTarget(), game);
target.clearChosen();
// The source is still the spell on the stack
target.addTarget(source.getSourceId(), stackObject.getStackAbility(), game);
}
} else {
Player controller = game.getPlayer(source.getControllerId());
boolean validTargets = false;
do {
for (Target target : targets) {
for (UUID targetId : target.getTargets()) {
String name = getTargetName(targets.getFirstTarget(), game);
if (!targetId.equals(source.getSourceId()) && target.getTargets().contains(source.getSourceId())) {
// you can't change this target to Spellskite because Spellskite is already another targetId of that target.
twoTimesTarget = true;
continue;
}
if (target.canTarget(stackObject.getControllerId(), source.getSourceId(), sourceAbility, game)) {
validTargets = true;
if (name != null
&& controller.chooseUse(Outcome.Neutral, "Change target from " + name + " to " + sourceObject.getLogName() + "?", source, game)) {
oldTargetName = getTargetName(targetId, game);
target.remove(targetId);
// The source is still the spell on the stack
target.addTarget(source.getSourceId(), stackObject.getStackAbility(), game);
break;
}
}
}
if (oldTargetName != null) {
break;
}
}
if (oldTargetName == null) {
game.informPlayer(controller, "You have to select at least one target to change to spellskite!");
}
} while (validTargets && oldTargetName == null);
}
if (oldTargetName != null) {
game.informPlayers(sourceObject.getLogName() + ": Changed target of " + stackObject.getLogName() + " from " + oldTargetName + " to " + sourceObject.getLogName());
} else {
if (twoTimesTarget) {
game.informPlayers(sourceObject.getLogName() + ": Target not changed to " + sourceObject.getLogName() + " because its not valid to target it twice for " + stackObject.getLogName());
} else {
game.informPlayers(sourceObject.getLogName() + ": Target not changed to " + sourceObject.getLogName() + " because its no valid target for " + stackObject.getLogName());
}
}
return true;
}
return false;
}
@Override
public SpellskiteEffect copy() {
return new SpellskiteEffect(this);
}
private String getTargetName(UUID objectId, Game game) {
MageObject object = game.getObject(objectId);
if (object != null) {
return object.getLogName();
}
Player player = game.getPlayer(objectId);
if (player != null) {
return player.getLogName();
}
return null;
}
}

View file

@ -0,0 +1,130 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mage.abilities.effects.common;
import java.util.UUID;
import mage.MageObject;
import mage.abilities.Ability;
import mage.abilities.effects.OneShotEffect;
import mage.constants.Outcome;
import mage.game.Game;
import mage.game.stack.Spell;
import mage.game.stack.StackAbility;
import mage.game.stack.StackObject;
import mage.players.Player;
import mage.target.Target;
import mage.target.Targets;
/**
*
* @author LevelX2
*/
public class ChangeATargetOfTargetSpellAbilityToSourceEffect extends OneShotEffect {
public ChangeATargetOfTargetSpellAbilityToSourceEffect() {
super(Outcome.Neutral);
staticText = "Change a target of target spell or ability to {this}";
}
public ChangeATargetOfTargetSpellAbilityToSourceEffect(final ChangeATargetOfTargetSpellAbilityToSourceEffect effect) {
super(effect);
}
@Override
public boolean apply(Game game, Ability source) {
StackObject stackObject = game.getStack().getStackObject(source.getFirstTarget());
MageObject sourceObject = game.getObject(source.getSourceId());
if (stackObject != null && sourceObject != null) {
Targets targets = new Targets();
Ability sourceAbility;
String oldTargetName = null;
if (stackObject instanceof Spell) {
Spell spell = (Spell) stackObject;
sourceAbility = spell.getSpellAbility();
} else if (stackObject instanceof StackAbility) {
StackAbility stackAbility = (StackAbility) stackObject;
sourceAbility = stackAbility;
} else {
return false;
}
for (UUID modeId : sourceAbility.getModes().getSelectedModes()) {
sourceAbility.getModes().setActiveMode(modeId);
targets.addAll(sourceAbility.getTargets());
}
boolean twoTimesTarget = false;
if (targets.size() == 1 && targets.get(0).getTargets().size() == 1) {
Target target = targets.get(0);
if (target.canTarget(stackObject.getControllerId(), source.getSourceId(), sourceAbility, game)) {
oldTargetName = getTargetName(targets.getFirstTarget(), game);
target.clearChosen();
// The source is still the spell on the stack
target.addTarget(source.getSourceId(), stackObject.getStackAbility(), game);
}
} else {
Player controller = game.getPlayer(source.getControllerId());
boolean validTargets = false;
do {
for (Target target : targets) {
for (UUID targetId : target.getTargets()) {
String name = getTargetName(targets.getFirstTarget(), game);
if (!targetId.equals(source.getSourceId()) && target.getTargets().contains(source.getSourceId())) {
// you can't change this target to source because the source is already another targetId of that target.
twoTimesTarget = true;
continue;
}
if (target.canTarget(stackObject.getControllerId(), source.getSourceId(), sourceAbility, game)) {
validTargets = true;
if (name != null
&& controller.chooseUse(Outcome.Neutral, "Change target from " + name + " to " + sourceObject.getLogName() + "?", source, game)) {
oldTargetName = getTargetName(targetId, game);
target.remove(targetId);
// The source is still the spell on the stack
target.addTarget(source.getSourceId(), stackObject.getStackAbility(), game);
break;
}
}
}
if (oldTargetName != null) {
break;
}
}
if (oldTargetName == null) {
game.informPlayer(controller, "You have to select at least one target to change to " + sourceObject.getIdName() + "!");
}
} while (validTargets && oldTargetName == null);
}
if (oldTargetName != null) {
game.informPlayers(sourceObject.getLogName() + ": Changed target of " + stackObject.getLogName() + " from " + oldTargetName + " to " + sourceObject.getLogName());
} else {
if (twoTimesTarget) {
game.informPlayers(sourceObject.getLogName() + ": Target not changed to " + sourceObject.getLogName() + " because its not valid to target it twice for " + stackObject.getLogName());
} else {
game.informPlayers(sourceObject.getLogName() + ": Target not changed to " + sourceObject.getLogName() + " because its no valid target for " + stackObject.getLogName());
}
}
return true;
}
return false;
}
@Override
public ChangeATargetOfTargetSpellAbilityToSourceEffect copy() {
return new ChangeATargetOfTargetSpellAbilityToSourceEffect(this);
}
private String getTargetName(UUID objectId, Game game) {
MageObject object = game.getObject(objectId);
if (object != null) {
return object.getLogName();
}
Player player = game.getPlayer(objectId);
if (player != null) {
return player.getLogName();
}
return null;
}
}