Handle tokens correctly in the new zone change code.

This commit is contained in:
Samuel Sandeen 2016-09-15 19:44:32 -04:00
parent 377a0edec9
commit 17d4d6c190
4 changed files with 93 additions and 129 deletions

View file

@ -83,79 +83,78 @@ public class ZonesHandler {
ZoneChangeEvent event = info.event; ZoneChangeEvent event = info.event;
Zone toZone = event.getToZone(); Zone toZone = event.getToZone();
Card targetCard = getTargetCard(game, event.getTargetId()); Card targetCard = getTargetCard(game, event.getTargetId());
if (targetCard == null) { Cards cards = null;
// This should never happen. // If we're moving a token it shouldn't be put into any zone as an object.
return; if (!(targetCard instanceof Permanent) && targetCard != null) {
} if (targetCard instanceof MeldCard) {
Cards cards; cards = ((MeldCard) targetCard).getHalves();
if (targetCard instanceof MeldCard) { } else {
cards = ((MeldCard) targetCard).getHalves(); cards = new CardsImpl(targetCard);
} else { }
cards = new CardsImpl(targetCard); Player owner = game.getPlayer(targetCard.getOwnerId());
} switch (toZone) {
Player owner = game.getPlayer(targetCard.getOwnerId()); case HAND:
switch (toZone) { for (Card card : cards.getCards(game)) {
case HAND: game.getPlayer(card.getOwnerId()).getHand().add(card);
for (Card card : cards.getCards(game)) { }
game.getPlayer(card.getOwnerId()).getHand().add(card); break;
} case GRAVEYARD:
break;
case GRAVEYARD:
for (Card card : chooseOrder(
"order to put in graveyard (last chosen will be on top)", cards, owner, game)) {
game.getPlayer(card.getOwnerId()).getGraveyard().add(card);
}
break;
case LIBRARY:
if (info instanceof ZoneChangeInfo.Library && ((ZoneChangeInfo.Library) info).top) {
for (Card card : chooseOrder( for (Card card : chooseOrder(
"order to put on top of library (last chosen will be topmost)", cards, owner, game)) { "order to put in graveyard (last chosen will be on top)", cards, owner, game)) {
game.getPlayer(card.getOwnerId()).getLibrary().putOnTop(card, game); game.getPlayer(card.getOwnerId()).getGraveyard().add(card);
} }
} else { break;
for (Card card : chooseOrder( case LIBRARY:
"order to put on bottom of library (last chosen will be bottommost)", cards, owner, game)) { if (info instanceof ZoneChangeInfo.Library && ((ZoneChangeInfo.Library) info).top) {
game.getPlayer(card.getOwnerId()).getLibrary().putOnBottom(card, game); for (Card card : chooseOrder(
} "order to put on top of library (last chosen will be topmost)", cards, owner, game)) {
} game.getPlayer(card.getOwnerId()).getLibrary().putOnTop(card, game);
break; }
case EXILED:
for (Card card : cards.getCards(game)) {
if (info instanceof ZoneChangeInfo.Exile && ((ZoneChangeInfo.Exile) info).id != null) {
ZoneChangeInfo.Exile exileInfo = (ZoneChangeInfo.Exile) info;
game.getExile().createZone(exileInfo.id, exileInfo.name).add(card);
} else { } else {
game.getExile().getPermanentExile().add(card); for (Card card : chooseOrder(
"order to put on bottom of library (last chosen will be bottommost)", cards, owner, game)) {
game.getPlayer(card.getOwnerId()).getLibrary().putOnBottom(card, game);
}
} }
} break;
break; case EXILED:
case COMMAND: for (Card card : cards.getCards(game)) {
// There should never be more than one card here. if (info instanceof ZoneChangeInfo.Exile && ((ZoneChangeInfo.Exile) info).id != null) {
for (Card card : cards.getCards(game)) { ZoneChangeInfo.Exile exileInfo = (ZoneChangeInfo.Exile) info;
game.addCommander(new Commander(card)); game.getExile().createZone(exileInfo.id, exileInfo.name).add(card);
} } else {
break; game.getExile().getPermanentExile().add(card);
case STACK: }
// There should never be more than one card here.
for (Card card : cards.getCards(game)) {
if (info instanceof ZoneChangeInfo.Stack && ((ZoneChangeInfo.Stack) info).spell != null) {
game.getStack().push(((ZoneChangeInfo.Stack) info).spell);
} else {
game.getStack().push(
new Spell(card, card.getSpellAbility().copy(), card.getOwnerId(), event.getFromZone()));
} }
} break;
break; case COMMAND:
case BATTLEFIELD: // There should never be more than one card here.
Permanent permanent = event.getTarget(); for (Card card : cards.getCards(game)) {
game.addPermanent(permanent); game.addCommander(new Commander(card));
game.getPermanentsEntering().remove(permanent.getId()); }
break; break;
default: case STACK:
throw new UnsupportedOperationException("to Zone" + toZone.toString() + " not supported yet"); // There should never be more than one card here.
for (Card card : cards.getCards(game)) {
if (info instanceof ZoneChangeInfo.Stack && ((ZoneChangeInfo.Stack) info).spell != null) {
game.getStack().push(((ZoneChangeInfo.Stack) info).spell);
} else {
game.getStack().push(
new Spell(card, card.getSpellAbility().copy(), card.getOwnerId(), event.getFromZone()));
}
}
break;
case BATTLEFIELD:
Permanent permanent = event.getTarget();
game.addPermanent(permanent);
game.getPermanentsEntering().remove(permanent.getId());
break;
default:
throw new UnsupportedOperationException("to Zone" + toZone.toString() + " not supported yet");
}
} }
game.setZone(event.getTargetId(), event.getToZone()); game.setZone(event.getTargetId(), event.getToZone());
if (targetCard instanceof MeldCard) { if (targetCard instanceof MeldCard && cards != null) {
if (event.getToZone() != Zone.BATTLEFIELD) { if (event.getToZone() != Zone.BATTLEFIELD) {
((MeldCard) targetCard).setMelded(false); ((MeldCard) targetCard).setMelded(false);
} }

View file

@ -27,7 +27,6 @@
*/ */
package mage.game.permanent; package mage.game.permanent;
import java.util.ArrayList;
import java.util.UUID; import java.util.UUID;
import mage.abilities.Abilities; import mage.abilities.Abilities;
import mage.abilities.Ability; import mage.abilities.Ability;
@ -36,12 +35,8 @@ import mage.abilities.costs.mana.ManaCosts;
import mage.abilities.keyword.TransformAbility; import mage.abilities.keyword.TransformAbility;
import mage.cards.Card; import mage.cards.Card;
import mage.cards.LevelerCard; import mage.cards.LevelerCard;
import mage.constants.Zone;
import mage.game.Game; import mage.game.Game;
import mage.game.ZoneChangeInfo;
import mage.game.ZonesHandler;
import mage.game.events.ZoneChangeEvent; import mage.game.events.ZoneChangeEvent;
import mage.players.Player;
/** /**
* @author BetaSteward_at_googlemail.com * @author BetaSteward_at_googlemail.com
@ -147,31 +142,6 @@ public class PermanentCard extends PermanentImpl {
return card; return card;
} }
@Override
public boolean moveToZone(Zone toZone, UUID sourceId, Game game, boolean flag, ArrayList<UUID> appliedEffects) {
Zone fromZone = game.getState().getZone(objectId);
Player controller = game.getPlayer(controllerId);
if (controller != null) {
ZoneChangeEvent event = new ZoneChangeEvent(this, sourceId, controllerId, fromZone, toZone, appliedEffects);
ZoneChangeInfo zoneChangeInfo;
if (toZone == Zone.LIBRARY) {
zoneChangeInfo = new ZoneChangeInfo.Library(event, flag /* put on top */);
} else {
zoneChangeInfo = new ZoneChangeInfo(event);
}
return ZonesHandler.moveCard(zoneChangeInfo, game);
}
return false;
}
@Override
public boolean moveToExile(UUID exileId, String name, UUID sourceId, Game game, ArrayList<UUID> appliedEffects) {
Zone fromZone = game.getState().getZone(objectId);
ZoneChangeEvent event = new ZoneChangeEvent(this, sourceId, ownerId, fromZone, Zone.EXILED, appliedEffects);
ZoneChangeInfo.Exile info = new ZoneChangeInfo.Exile(event, exileId, name);
return ZonesHandler.moveCard(info, game);
}
@Override @Override
public PermanentCard copy() { public PermanentCard copy() {
return new PermanentCard(this); return new PermanentCard(this);

View file

@ -66,13 +66,10 @@ import mage.counters.CounterType;
import mage.counters.Counters; import mage.counters.Counters;
import mage.game.Game; import mage.game.Game;
import mage.game.GameState; import mage.game.GameState;
import mage.game.ZoneChangeInfo;
import mage.game.ZonesHandler;
import mage.game.command.CommandObject; import mage.game.command.CommandObject;
import mage.game.events.DamageCreatureEvent; import mage.game.events.*;
import mage.game.events.DamagePlaneswalkerEvent;
import mage.game.events.DamagedCreatureEvent;
import mage.game.events.DamagedPlaneswalkerEvent;
import mage.game.events.EntersTheBattlefieldEvent;
import mage.game.events.GameEvent;
import mage.game.events.GameEvent.EventType; import mage.game.events.GameEvent.EventType;
import mage.game.stack.Spell; import mage.game.stack.Spell;
import mage.game.stack.StackObject; import mage.game.stack.StackObject;
@ -1377,4 +1374,29 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
return color; return color;
} }
@Override
public boolean moveToZone(Zone toZone, UUID sourceId, Game game, boolean flag, ArrayList<UUID> appliedEffects) {
Zone fromZone = game.getState().getZone(objectId);
Player controller = game.getPlayer(controllerId);
if (controller != null) {
ZoneChangeEvent event = new ZoneChangeEvent(this, sourceId, controllerId, fromZone, toZone, appliedEffects);
ZoneChangeInfo zoneChangeInfo;
if (toZone == Zone.LIBRARY) {
zoneChangeInfo = new ZoneChangeInfo.Library(event, flag /* put on top */);
} else {
zoneChangeInfo = new ZoneChangeInfo(event);
}
return ZonesHandler.moveCard(zoneChangeInfo, game);
}
return false;
}
@Override
public boolean moveToExile(UUID exileId, String name, UUID sourceId, Game game, ArrayList<UUID> appliedEffects) {
Zone fromZone = game.getState().getZone(objectId);
ZoneChangeEvent event = new ZoneChangeEvent(this, sourceId, ownerId, fromZone, Zone.EXILED, appliedEffects);
ZoneChangeInfo.Exile info = new ZoneChangeInfo.Exile(event, exileId, name);
return ZonesHandler.moveCard(info, game);
}
} }

View file

@ -30,9 +30,7 @@ package mage.game.permanent;
import java.util.UUID; import java.util.UUID;
import mage.abilities.Ability; import mage.abilities.Ability;
import mage.abilities.costs.mana.ManaCost; import mage.abilities.costs.mana.ManaCost;
import mage.constants.Zone;
import mage.game.Game; import mage.game.Game;
import mage.game.events.ZoneChangeEvent;
import mage.game.permanent.token.Token; import mage.game.permanent.token.Token;
/** /**
@ -94,31 +92,6 @@ public class PermanentToken extends PermanentImpl {
this.tokenDescriptor = token.getTokenDescriptor(); this.tokenDescriptor = token.getTokenDescriptor();
} }
@Override
public boolean moveToZone(Zone zone, UUID sourceId, Game game, boolean flag) {
if (!game.replaceEvent(new ZoneChangeEvent(this, this.getControllerId(), Zone.BATTLEFIELD, zone))) {
game.rememberLKI(objectId, Zone.BATTLEFIELD, this);
if (game.getPlayer(controllerId).removeFromBattlefield(this, game)) {
game.setZone(objectId, zone); // needed for triggered dies abilities
game.addSimultaneousEvent(new ZoneChangeEvent(this, this.getControllerId(), Zone.BATTLEFIELD, zone));
return true;
}
}
return false;
}
@Override
public boolean moveToExile(UUID exileId, String name, UUID sourceId, Game game) {
if (!game.replaceEvent(new ZoneChangeEvent(this, sourceId, this.getControllerId(), Zone.BATTLEFIELD, Zone.EXILED))) {
game.rememberLKI(objectId, Zone.BATTLEFIELD, this);
if (game.getPlayer(controllerId).removeFromBattlefield(this, game)) {
game.addSimultaneousEvent(new ZoneChangeEvent(this, sourceId, this.getControllerId(), Zone.BATTLEFIELD, Zone.EXILED));
return true;
}
}
return false;
}
public Token getToken() { public Token getToken() {
return token; return token;
} }