use of mage.abilities.SpellAbility in project mage by magefree.
the class EscapesWithEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Permanent permanent = game.getPermanent(source.getSourceId());
if (permanent == null && source.getAbilityType() == AbilityType.STATIC) {
permanent = game.getPermanentEntering(source.getSourceId());
}
if (permanent == null) {
return false;
}
SpellAbility spellAbility = (SpellAbility) getValue(EntersBattlefieldEffect.SOURCE_CAST_SPELL_ABILITY);
if (!(spellAbility instanceof EscapeAbility) || !spellAbility.getSourceId().equals(source.getSourceId()) || permanent.getZoneChangeCounter(game) != spellAbility.getSourceObjectZoneChangeCounter()) {
return false;
}
List<UUID> appliedEffects = (ArrayList<UUID>) this.getValue("appliedEffects");
permanent.addCounters(CounterType.P1P1.createInstance(counter), source.getControllerId(), source, game, appliedEffects);
if (this.delayedTriggeredAbility != null) {
game.addDelayedTriggeredAbility(this.delayedTriggeredAbility, source);
}
return true;
}
use of mage.abilities.SpellAbility in project mage by magefree.
the class CipherStoreEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
Card cipherCard = game.getCard(cipherCardId);
if (cipherCard != null && controller != null) {
Card copyCard = game.copyCard(cipherCard, source, controller.getId());
SpellAbility ability = copyCard.getSpellAbility();
// remove the cipher effect from the copy
ability.getEffects().removeIf(effect -> effect instanceof CipherEffect);
controller.cast(ability, game, true, new ApprovingObject(source, game));
}
return false;
}
use of mage.abilities.SpellAbility in project mage by magefree.
the class CopyPermanentEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
MageObject sourcePermanent = game.getPermanentEntering(source.getSourceId());
if (sourcePermanent == null) {
sourcePermanent = game.getObject(source.getSourceId());
}
if (controller == null || sourcePermanent == null) {
return false;
}
Permanent copyFromPermanent = null;
if (useTargetOfAbility) {
copyFromPermanent = game.getPermanent(getTargetPointer().getFirst(game, source));
} else {
Target target = new TargetPermanent(filter);
target.setNotTarget(true);
if (target.canChoose(source.getSourceId(), controller.getId(), game)) {
controller.choose(Outcome.Copy, target, source.getSourceId(), game);
copyFromPermanent = game.getPermanent(target.getFirstTarget());
}
}
if (copyFromPermanent == null) {
return true;
}
bluePrintPermanent = game.copyPermanent(duration, copyFromPermanent, sourcePermanent.getId(), source, applier);
if (bluePrintPermanent == null) {
return false;
}
// if object is a copy of an aura, it needs to attach again for new target
if (!bluePrintPermanent.hasSubtype(SubType.AURA, game)) {
return true;
}
// copied from mage.cards.c.CopyEnchantment.java
// permanent can be attached (Estrid's Mask) or enchant (Utopia Sprawl)
// TODO: fix Animate Dead -- it's can't be copied (can't retarget)
Outcome auraOutcome = Outcome.BoostCreature;
Target auraTarget = null;
// attach - search effect in spell ability (example: cast Utopia Sprawl, cast Estrid's Invocation on it)
for (Ability ability : bluePrintPermanent.getAbilities()) {
if (!(ability instanceof SpellAbility)) {
continue;
}
auraOutcome = ability.getEffects().getOutcome(ability);
for (Effect effect : ability.getEffects()) {
if (!(effect instanceof AttachEffect)) {
continue;
}
if (bluePrintPermanent.getSpellAbility().getTargets().size() > 0) {
auraTarget = bluePrintPermanent.getSpellAbility().getTargets().get(0);
}
}
}
// enchant - search in all abilities (example: cast Estrid's Invocation on enchanted creature by Estrid, the Masked second ability, cast Estrid's Invocation on it)
if (auraTarget == null) {
for (Ability ability : bluePrintPermanent.getAbilities()) {
if (!(ability instanceof EnchantAbility)) {
continue;
}
auraOutcome = ability.getEffects().getOutcome(ability);
if (ability.getTargets().size() > 0) {
// Animate Dead don't have targets
auraTarget = ability.getTargets().get(0);
}
}
}
/* if this is a copy of a copy, the copy's target has been
* copied and needs to be cleared
*/
if (auraTarget == null) {
return true;
}
// clear selected target
if (auraTarget.getFirstTarget() != null) {
auraTarget.remove(auraTarget.getFirstTarget());
}
// select new target
auraTarget.setNotTarget(true);
if (!controller.choose(auraOutcome, auraTarget, source.getSourceId(), game)) {
return true;
}
UUID targetId = auraTarget.getFirstTarget();
Permanent targetPermanent = game.getPermanent(targetId);
Player targetPlayer = game.getPlayer(targetId);
if (targetPermanent != null) {
targetPermanent.addAttachment(sourcePermanent.getId(), source, game);
} else if (targetPlayer != null) {
targetPlayer.addAttachment(sourcePermanent.getId(), source, game);
} else {
return false;
}
return true;
}
use of mage.abilities.SpellAbility in project mage by magefree.
the class SpellsCostModificationThatTargetSourceEffect method applies.
@Override
public boolean applies(Ability abilityToModify, Ability source, Game game) {
if (!(abilityToModify instanceof SpellAbility)) {
return false;
}
Player sourceController = game.getPlayer(source.getControllerId());
Player abilityController = game.getPlayer(abilityToModify.getControllerId());
if (sourceController == null || abilityController == null) {
return false;
}
switch(this.targetController) {
case ANY:
break;
case YOU:
if (!sourceController.getId().equals(abilityController.getId())) {
return false;
}
break;
case OPPONENT:
if (!sourceController.hasOpponent(abilityController.getId(), game)) {
return false;
}
break;
default:
return false;
}
Card spellCard = ((SpellAbility) abilityToModify).getCharacteristics(game);
if (!this.spellFilter.match(spellCard, game)) {
return false;
}
if (game.getStack().getStackObject(abilityToModify.getId()) != null) {
// real cast
Set<UUID> allTargets = CardUtil.getAllSelectedTargets(abilityToModify, game);
return allTargets.contains(source.getSourceId());
} else {
// playable
Set<UUID> allTargets = CardUtil.getAllPossibleTargets(abilityToModify, game);
switch(this.getModificationType()) {
case REDUCE_COST:
// must reduce all the time
return allTargets.contains(source.getSourceId());
case INCREASE_COST:
// must increase if can't target another (e.g. user can choose another target without cost increase)
return allTargets.contains(source.getSourceId()) && allTargets.size() <= 1;
}
}
return false;
}
use of mage.abilities.SpellAbility in project mage by magefree.
the class DralnusPetEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
Permanent permanent = game.getPermanent(source.getSourceId());
if (permanent == null && source.getAbilityType() == AbilityType.STATIC) {
permanent = game.getPermanentEntering(source.getSourceId());
}
if (controller != null && permanent != null) {
SpellAbility spellAbility = (SpellAbility) getValue(EntersBattlefieldEffect.SOURCE_CAST_SPELL_ABILITY);
if (spellAbility != null && spellAbility.getSourceId().equals(source.getSourceId()) && permanent.getZoneChangeCounter(game) == spellAbility.getSourceObjectZoneChangeCounter()) {
int cmc = 0;
for (Cost cost : spellAbility.getCosts()) {
if (cost instanceof DiscardCardCost && !((DiscardCardCost) cost).getCards().isEmpty()) {
cmc = ((DiscardCardCost) cost).getCards().get(0).getManaValue();
}
if (cmc > 0) {
return new AddCountersSourceEffect(CounterType.P1P1.createInstance(cmc), true).apply(game, source);
}
}
}
return true;
}
return false;
}
Aggregations