use of mage.abilities.SpellAbility in project mage by magefree.
the class AuraReplacementEffect method replaceEvent.
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
Zone fromZone = ((ZoneChangeEvent) event).getFromZone();
Card card = game.getCard(event.getTargetId());
if (card == null) {
return false;
}
Card firstCardFace = null;
if (game.getState().getValue(TransformAbility.VALUE_KEY_ENTER_TRANSFORMED + card.getId()) != null) {
firstCardFace = card;
card = card.getSecondCardFace();
if (!card.isEnchantment(game) || !card.hasSubtype(SubType.AURA, game)) {
return false;
}
}
// Aura cards that go to battlefield face down (Manifest) don't have to select targets
if (card.isFaceDown(game)) {
return false;
}
// Aura enters the battlefield attached
Object object = game.getState().getValue("attachTo:" + card.getId());
if (object != null) {
if (object instanceof Permanent) {
// Aura is attached to a permanent on the battlefield
return false;
}
if (object instanceof UUID) {
Player player = game.getPlayer((UUID) object);
if (player != null) {
// Aura is attached to a player
return false;
}
}
}
UUID targetId = null;
MageObject sourceObject = game.getObject(event.getSourceId());
boolean enchantCardInGraveyard = false;
if (sourceObject instanceof StackAbility) {
StackAbility stackAbility = (StackAbility) sourceObject;
if (!stackAbility.getEffects().isEmpty()) {
targetId = stackAbility.getEffects().get(0).getTargetPointer().getFirst(game, stackAbility);
}
}
// So continuousEffects are removed if previous effect of the same ability did move objects that cause continuous effects
game.applyEffects();
Player controllingPlayer = null;
if (targetId == null) {
SpellAbility spellAbility = card.getSpellAbility();
if (spellAbility.getTargets().isEmpty()) {
for (Ability ability : card.getAbilities(game)) {
if ((ability instanceof SpellAbility) && SpellAbilityType.BASE_ALTERNATE == ((SpellAbility) ability).getSpellAbilityType() && !ability.getTargets().isEmpty()) {
spellAbility = (SpellAbility) ability;
break;
}
}
}
if (spellAbility.getTargets().isEmpty()) {
return false;
}
Target target = spellAbility.getTargets().get(0).copy();
Outcome auraOutcome = Outcome.BoostCreature;
for (Effect effect : spellAbility.getEffects()) {
if (effect instanceof AttachEffect) {
auraOutcome = effect.getOutcome();
break;
}
}
enchantCardInGraveyard = target instanceof TargetCardInGraveyard;
if (target != null) {
// always not target because this way it's not handled targeted
target.setNotTarget(true);
// necessary if e.g. aura is blinked multiple times
target.clearChosen();
}
if (event.getPlayerId() != null) {
controllingPlayer = game.getPlayer(event.getPlayerId());
} else {
controllingPlayer = game.getPlayer(card.getOwnerId());
}
if (target != null && controllingPlayer != null && controllingPlayer.choose(auraOutcome, target, card.getId(), game)) {
targetId = target.getFirstTarget();
}
}
Card targetCard = null;
Permanent targetPermanent = null;
if (enchantCardInGraveyard) {
targetCard = game.getCard(targetId);
} else {
targetPermanent = game.getPermanent(targetId);
}
Player targetPlayer = game.getPlayer(targetId);
if (targetCard != null || targetPermanent != null || targetPlayer != null) {
if (firstCardFace != null) {
// transforming card. remove first face (original card) from old zone
firstCardFace.removeFromZone(game, fromZone, source);
} else {
card.removeFromZone(game, fromZone, source);
}
PermanentCard permanent = new PermanentCard(card, (controllingPlayer == null ? card.getOwnerId() : controllingPlayer.getId()), game);
ZoneChangeEvent zoneChangeEvent = new ZoneChangeEvent(permanent, event.getPlayerId(), fromZone, Zone.BATTLEFIELD);
permanent.updateZoneChangeCounter(game, zoneChangeEvent);
game.addPermanent(permanent, 0);
card.setZone(Zone.BATTLEFIELD, game);
if (permanent.entersBattlefield(source, game, fromZone, true)) {
String attachToName = null;
if (targetCard != null) {
permanent.attachTo(targetCard.getId(), source, game);
attachToName = targetCard.getLogName();
} else if (targetPermanent != null) {
targetPermanent.addAttachment(permanent.getId(), source, game);
attachToName = targetPermanent.getLogName();
} else if (targetPlayer != null) {
targetPlayer.addAttachment(permanent.getId(), source, game);
attachToName = targetPlayer.getLogName();
}
game.applyEffects();
game.fireEvent(zoneChangeEvent);
if (!game.isSimulation()) {
if (controllingPlayer != null && fromZone != null && permanent != null) {
game.informPlayers(controllingPlayer.getLogName() + " puts " + (card.getLogName()) + " from " + fromZone.toString().toLowerCase(Locale.ENGLISH) + " onto the Battlefield attached to " + attachToName);
}
}
return true;
}
}
return false;
}
use of mage.abilities.SpellAbility in project mage by magefree.
the class AwakenTheBloodAvatarCostReductionEffect method apply.
@Override
public boolean apply(Game game, Ability source, Ability abilityToModify) {
SpellAbility spellAbility = (SpellAbility) abilityToModify;
for (Cost cost : spellAbility.getCosts()) {
if (!(cost instanceof SacrificeXTargetCost)) {
continue;
}
if (game.inCheckPlayableState()) {
// allows to cast in getPlayable
int reduction = ((SacrificeXTargetCost) cost).getMaxValue(spellAbility, game);
CardUtil.adjustCost(spellAbility, reduction * 2);
} else {
// real cast
int reduction = ((SacrificeXTargetCost) cost).getAmount();
CardUtil.adjustCost(spellAbility, reduction * 2);
}
break;
}
return true;
}
use of mage.abilities.SpellAbility in project mage by magefree.
the class HeraldOfWarCostReductionEffect method apply.
@Override
public boolean apply(Game game, Ability source, Ability abilityToModify) {
SpellAbility spellAbility = (SpellAbility) abilityToModify;
Permanent sourcePermanent = game.getPermanent(source.getSourceId());
if (sourcePermanent != null) {
int amount = sourcePermanent.getCounters(game).getCount(CounterType.P1P1);
if (amount > 0) {
CardUtil.adjustCost(spellAbility, amount);
return true;
}
}
return false;
}
use of mage.abilities.SpellAbility in project mage by magefree.
the class KasminaEnigmaticMentorCostModificationEffect 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().map(game::getPermanent).filter(Objects::nonNull).anyMatch(permanent -> !isTargetCompatible(permanent, source, game))) {
return false;
}
;
}
return allTargets.stream().map(game::getPermanent).filter(Objects::nonNull).anyMatch(permanent -> isTargetCompatible(permanent, source, game));
}
use of mage.abilities.SpellAbility in project mage by magefree.
the class KazaRoilChaserWatcher method applies.
@Override
public boolean applies(Ability abilityToModify, Ability source, Game game) {
KazaRoilChaserWatcher watcher = game.getState().getWatcher(KazaRoilChaserWatcher.class);
if (watcher == null) {
return false;
}
if (watcher.getCount(source.getControllerId()) > spellsCast) {
// only one use
discard();
return false;
}
if (!(abilityToModify instanceof SpellAbility) || !abilityToModify.isControlledBy(source.getControllerId())) {
return false;
}
Card spellCard = ((SpellAbility) abilityToModify).getCharacteristics(game);
return spellCard != null && spellCard.isInstantOrSorcery(game);
}
Aggregations