use of mage.abilities.SpellAbility in project mage by magefree.
the class OldStickfingersEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Object obj = getValue(CastSourceTriggeredAbility.SOURCE_CAST_SPELL_ABILITY);
if (!(obj instanceof SpellAbility)) {
return false;
}
int xValue = ((SpellAbility) obj).getManaCostsToPay().getX();
if (xValue < 1) {
return false;
}
Player controller = game.getPlayer(source.getControllerId());
Cards revealed = new CardsImpl();
Cards otherCards = new CardsImpl();
Set<Card> creatureCards = new LinkedHashSet<>();
for (Card card : controller.getLibrary().getCards(game)) {
revealed.add(card);
if (card.isCreature(game)) {
creatureCards.add(card);
if (creatureCards.size() == xValue) {
break;
}
} else {
otherCards.add(card);
}
}
controller.revealCards(source, revealed, game);
controller.moveCards(creatureCards, Zone.GRAVEYARD, source, game);
controller.putCardsOnBottomOfLibrary(otherCards, game, source, false);
return true;
}
use of mage.abilities.SpellAbility in project mage by magefree.
the class RakdosLordOfRiotsCostReductionEffect method apply.
@Override
public boolean apply(Game game, Ability source, Ability abilityToModify) {
Ability spellAbility = abilityToModify;
if (spellAbility != null) {
int amount = OpponentsLostLifeCount.instance.calculate(game, source, this);
CardUtil.reduceCost(spellAbility, amount);
return true;
}
return false;
}
use of mage.abilities.SpellAbility in project mage by magefree.
the class TerrorOfThePeaksCostIncreaseEffect method applies.
@Override
public boolean applies(Ability abilityToModify, Ability source, Game game) {
if (!(abilityToModify instanceof SpellAbility)) {
return false;
}
if (!game.getOpponents(source.getControllerId()).contains(abilityToModify.getControllerId())) {
return false;
}
Spell spell = (Spell) game.getStack().getStackObject(abilityToModify.getId());
Set<UUID> allTargets;
if (spell != null) {
// real cast
allTargets = CardUtil.getAllSelectedTargets(abilityToModify, game);
} else {
// playable
allTargets = CardUtil.getAllPossibleTargets(abilityToModify, game);
// can target without cost increase
if (allTargets.stream().anyMatch(target -> !isTargetCompatible(target, source, game))) {
return false;
}
;
}
return allTargets.stream().anyMatch(target -> isTargetCompatible(target, source, game));
}
use of mage.abilities.SpellAbility in project mage by magefree.
the class WordOfCommandTestFlashEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player sourceController = game.getPlayer(source.getControllerId());
Player targetPlayer = game.getPlayer(source.getFirstTarget());
MageObject sourceObject = game.getObject(source.getSourceId());
Card card = null;
if (sourceController != null && targetPlayer != null && sourceObject != null) {
Player controller = null;
Spell wordOfCommand = game.getSpell(source.getSourceId());
if (wordOfCommand != null) {
if (wordOfCommand.getCommandedBy() != null) {
controller = game.getPlayer(wordOfCommand.getCommandedBy());
} else {
controller = game.getPlayer(sourceController.getTurnControlledBy());
}
}
if (controller == null) {
// reset the controller to avoid NPE
controller = sourceController;
}
// Look at target opponent's hand and choose a card from it
TargetCard targetCard = new TargetCard(Zone.HAND, new FilterCard());
if (controller.choose(Outcome.Discard, targetPlayer.getHand(), targetCard, game)) {
card = game.getCard(targetCard.getFirstTarget());
}
// You control that player until Word of Command finishes resolving
CardUtil.takeControlUnderPlayerStart(game, controller, targetPlayer, true);
// The player plays that card if able
if (card != null) {
// While doing so, the player can activate mana abilities only if they're from lands that player controls
RestrictionEffect effect = new WordOfCommandCantActivateEffect();
effect.setTargetPointer(new FixedTarget(targetPlayer.getId()));
game.addEffect(effect, source);
// and only if mana they produce is spent to activate other mana abilities of lands they control and/or play that card
ManaPool manaPool = targetPlayer.getManaPool();
manaPool.setForcedToPay(true);
manaPool.storeMana();
int bookmark = game.bookmarkState();
boolean canPlay = checkPlayability(card, targetPlayer, game, source);
while (canPlay && targetPlayer.canRespond() && !targetPlayer.playCard(card, game, false, new ApprovingObject(source, game))) {
SpellAbility spellAbility = card.getSpellAbility();
if (spellAbility != null) {
spellAbility.getManaCostsToPay().clear();
spellAbility.getManaCostsToPay().addAll(spellAbility.getManaCosts());
// force rollback if card was deemed playable
((ManaCostsImpl) spellAbility.getManaCostsToPay()).forceManaRollback(game, manaPool);
canPlay = checkPlayability(card, targetPlayer, game, source);
} else {
break;
}
}
if (!canPlay) {
game.informPlayers(targetPlayer.getLogName() + " didn't play " + card.getLogName() + " (card can't be played)");
}
// duplicate in case of a new mana pool existing - probably not necessary, but just in case
manaPool.setForcedToPay(false);
// a rollback creates a new mana pool for the player, so it's necessary to find it again
manaPool = targetPlayer.getManaPool();
manaPool.setForcedToPay(false);
game.removeBookmark(bookmark);
targetPlayer.resetStoredBookmark(game);
for (RestrictionEffect eff : game.getContinuousEffects().getRestrictionEffects()) {
if (eff instanceof WordOfCommandCantActivateEffect) {
eff.discard();
break;
}
}
game.getContinuousEffects().removeInactiveEffects(game);
Spell spell = game.getSpell(card.getId());
if (spell != null) {
// If the chosen card is cast as a spell, you control the player while that spell is resolving
spell.setCommandedBy(controller.getId());
}
}
wordOfCommand = game.getSpell(source.getSourceId());
if (wordOfCommand != null) {
// You control the player until Word of Command finishes resolving
wordOfCommand.setCommandedBy(controller.getId());
} else {
CardUtil.takeControlUnderPlayerEnd(game, controller, targetPlayer);
}
return true;
}
return false;
}
use of mage.abilities.SpellAbility in project mage by magefree.
the class WizardsSpellbookEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player player = game.getPlayer(source.getControllerId());
Card card = game.getCard(getTargetPointer().getFirst(game, source));
if (player == null || card == null) {
return false;
}
UUID exileId = CardUtil.getExileZoneId(game, source);
player.moveCardsToExile(card, source, game, true, exileId, CardUtil.getSourceName(game, source));
if (level < 3) {
Card copiedCard = game.copyCard(card, source, source.getControllerId());
if (!player.chooseUse(Outcome.Benefit, "Cast " + copiedCard.getName() + (level == 1 ? "?" : " by paying {1}?"), source, game)) {
return false;
}
SpellAbility spellAbility = player.chooseAbilityForCast(copiedCard, game, level == 2);
if (spellAbility == null) {
return false;
}
game.getState().setValue("PlayFromNotOwnHandZone" + copiedCard.getId(), Boolean.TRUE);
if (level == 2) {
player.setCastSourceIdWithAlternateMana(copiedCard.getId(), new ManaCostsImpl<>("{1}"), null);
}
player.cast(spellAbility, game, false, new ApprovingObject(source, game));
game.getState().setValue("PlayFromNotOwnHandZone" + copiedCard.getId(), null);
return true;
}
ExileZone exile = game.getExile().getExileZone(exileId);
if (exile == null || exile.isEmpty()) {
return true;
}
Set<Card> cards = new HashSet<>();
for (Card exiledCard : exile.getCards(game)) {
cards.add(game.copyCard(exiledCard, source, source.getControllerId()));
}
while (!cards.isEmpty()) {
for (Card copiedCard : cards) {
if (!player.chooseUse(Outcome.PlayForFree, "Cast " + copiedCard.getName() + " without paying its mana cost?", source, game)) {
continue;
}
game.getState().setValue("PlayFromNotOwnHandZone" + copiedCard.getId(), Boolean.TRUE);
player.cast(player.chooseAbilityForCast(copiedCard, game, true), game, true, new ApprovingObject(source, game));
game.getState().setValue("PlayFromNotOwnHandZone" + copiedCard.getId(), null);
}
if (!player.chooseUse(Outcome.Neutral, "Continue casting exiled cards?", source, game)) {
break;
}
cards.removeIf(c -> game.getState().getZone(c.getId()) != Zone.EXILED);
}
return true;
}
Aggregations