use of mage.game.ExileZone in project mage by magefree.
the class ReturnFromExileForSourceEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller == null) {
return false;
}
// effect uses in two use cases:
// * on battlefield
// * after leaves the battlefield
// so ZCC must be different in different use cases
UUID exileId;
Permanent permanentLeftBattlefield = (Permanent) getValue("permanentLeftBattlefield");
if (permanentLeftBattlefield != null) {
exileId = CardUtil.getExileZoneId(game, source.getSourceId(), permanentLeftBattlefield.getZoneChangeCounter(game));
} else {
exileId = CardUtil.getExileZoneId(game, source.getSourceId(), source.getSourceObjectZoneChangeCounter());
}
ExileZone exile = game.getExile().getExileZone(exileId);
if (exile != null) {
// null is valid if source left battlefield before enters the battlefield effect resolved
if (Zone.BATTLEFIELD.equals(returnToZone)) {
controller.moveCards(exile.getCards(game), returnToZone, source, game, false, false, true, null);
} else {
controller.moveCards(exile, returnToZone, source, game);
}
}
return true;
}
use of mage.game.ExileZone in project mage by magefree.
the class FreeForAllLeavesBattlefieldEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player player = game.getPlayer(game.getActivePlayerId());
ExileZone exZone = game.getExile().getExileZone(CardUtil.getExileZoneId(game, source));
if (player == null || exZone == null || exZone.isEmpty()) {
return false;
}
Cards exiledCards = new CardsImpl(exZone.getCards(game));
return player.moveCards(exiledCards.getRandom(game), Zone.BATTLEFIELD, source, game);
}
use of mage.game.ExileZone in project mage by magefree.
the class IzzetChemisterCastFromExileEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
ExileZone exile = game.getExile().getExileZone(exileId);
Player controller = game.getPlayer(source.getControllerId());
FilterCard filter = new FilterCard();
if (controller != null && exile != null) {
Cards cardsToExile = new CardsImpl();
cardsToExile.addAll(exile.getCards(game));
OuterLoop: while (cardsToExile.count(filter, game) > 0) {
if (!controller.canRespond()) {
return false;
}
TargetCardInExile target = new TargetCardInExile(0, 1, filter, exileId, false);
target.setNotTarget(true);
while (controller.canRespond() && cardsToExile.count(filter, game) > 0 && controller.choose(Outcome.PlayForFree, cardsToExile, target, game)) {
Card card = game.getCard(target.getFirstTarget());
if (card != null) {
game.getState().setValue("PlayFromNotOwnHandZone" + card.getId(), Boolean.TRUE);
controller.cast(controller.chooseAbilityForCast(card, game, true), game, true, new ApprovingObject(source, game));
game.getState().setValue("PlayFromNotOwnHandZone" + card.getId(), null);
cardsToExile.remove(card);
} else {
break OuterLoop;
}
target.clearChosen();
}
}
return true;
}
return false;
}
use of mage.game.ExileZone in project mage by magefree.
the class KarnPlayerExileEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
MageObject sourceObject = source.getSourceObject(game);
if (sourceObject == null) {
return false;
}
List<Card> cards = new ArrayList<>();
for (ExileZone zone : game.getExile().getExileZones()) {
exileId = CardUtil.getExileZoneId(game, source.getSourceId(), source.getSourceObjectZoneChangeCounter());
if (zone.getId().equals(exileId)) {
for (Card card : zone.getCards(game)) {
if (!card.hasSubtype(SubType.AURA, game) && card.isPermanent(game)) {
cards.add(card);
}
}
}
}
// dirty hack for game restart, can cause bugs due strange clear code (some data like ZCC keeping on new game)
// see testCommanderRestoredToBattlefieldAfterKarnUltimate for more details
game.getState().clearOnGameRestart();
// default watchers init, TODO: remove all restart/init code to game
((GameImpl) game).initGameDefaultWatchers();
for (Card card : game.getCards()) {
game.getState().addCard(card);
}
for (Player player : game.getPlayers().values()) {
if (player.canRespond()) {
// only players alive are in the restarted game
player.getGraveyard().clear();
player.getHand().clear();
player.getLibrary().clear();
for (Card card : game.getCards()) {
if (// no copies
card.isOwnedBy(player.getId()) && !card.isCopy() && !player.getSideboard().contains(card.getId()) && !cards.contains(card)) {
// not the exiled cards
if (game.getCommandersIds(player, CommanderCardType.ANY, false).contains(card.getId())) {
// TODO: check restart and init
game.addCommander(new Commander(card));
// no needs in initCommander call -- it's used on game startup (init)
game.setZone(card.getId(), Zone.COMMAND);
} else {
player.getLibrary().putOnTop(card, game);
}
}
}
((GameImpl) game).initPlayerDefaultWatchers(player.getId());
player.init(game);
}
}
for (Card card : cards) {
game.getState().setZone(card.getId(), Zone.EXILED);
game.getExile().add(exileId, sourceObject.getIdName(), card);
}
game.addDelayedTriggeredAbility(new KarnLiberatedDelayedTriggeredAbility(exileId), source);
game.setStartingPlayerId(source.getControllerId());
game.start(null);
return true;
}
use of mage.game.ExileZone in project mage by magefree.
the class MournersShieldEffect method init.
@Override
public void init(Ability source, Game game) {
ObjectColor colorsAmongImprinted = new ObjectColor();
Permanent sourceObject = game.getPermanent(source.getSourceId());
ExileZone exileZone = game.getExile().getExileZone(CardUtil.getCardExileZoneId(game, source.getSourceId()));
if (sourceObject == null || sourceObject.getImprinted() == null) {
noneExiled = true;
return;
}
for (UUID imprinted : sourceObject.getImprinted()) {
if (imprinted != null && exileZone.contains(imprinted)) {
Card card = game.getCard(imprinted);
if (card != null) {
colorsAmongImprinted = colorsAmongImprinted.union(card.getColor(game));
}
}
}
FilterObject filterObject = new FilterObject("a source of your choice that shares a color with the exiled card");
filterObject.add(new SharesColorPredicate(colorsAmongImprinted));
this.target = new TargetSource(filterObject);
this.target.choose(Outcome.PreventDamage, source.getControllerId(), source.getSourceId(), game);
if (target.getFirstTarget() != null) {
mageObjectReference = new MageObjectReference(target.getFirstTarget(), game);
} else {
mageObjectReference = null;
}
}
Aggregations