Search in sources :

Example 16 with StackAbility

use of mage.game.stack.StackAbility in project mage by magefree.

the class PowerleechTriggeredAbility method checkTrigger.

@Override
public boolean checkTrigger(GameEvent event, Game game) {
    Player player = game.getPlayer(controllerId);
    if (player == null) {
        return false;
    }
    if (event.getType() == GameEvent.EventType.ACTIVATED_ABILITY) {
        Permanent permanent = game.getPermanentOrLKIBattlefield(event.getSourceId());
        if (permanent == null || !permanent.isArtifact(game)) {
            return false;
        }
        StackAbility stackAbility = (StackAbility) game.getStack().getStackObject(event.getSourceId());
        if (stackAbility == null) {
            return false;
        }
        return !stackAbility.hasTapCost() && player.hasOpponent(permanent.getControllerId(), game);
    }
    if (event.getType() == GameEvent.EventType.TAPPED) {
        Permanent permanent = game.getPermanentOrLKIBattlefield(event.getTargetId());
        if (permanent == null || !permanent.isArtifact(game)) {
            return false;
        }
        return player.hasOpponent(permanent.getControllerId(), game);
    }
    return false;
}
Also used : Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) StackAbility(mage.game.stack.StackAbility)

Example 17 with StackAbility

use of mage.game.stack.StackAbility in project mage by magefree.

the class SpellskiteEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    StackObject stackObject = game.getStack().getStackObject(source.getFirstTarget());
    MageObject sourceObject = game.getObject(source.getSourceId());
    if (stackObject != null && sourceObject != null) {
        Targets targets = new Targets();
        Ability sourceAbility;
        String oldTargetName = null;
        if (stackObject instanceof Spell) {
            Spell spell = (Spell) stackObject;
            sourceAbility = spell.getSpellAbility();
        } else if (stackObject instanceof StackAbility) {
            StackAbility stackAbility = (StackAbility) stackObject;
            sourceAbility = stackAbility;
        } else {
            return false;
        }
        for (UUID modeId : sourceAbility.getModes().getSelectedModes()) {
            Mode mode = sourceAbility.getModes().get(modeId);
            targets.addAll(mode.getTargets());
        }
        boolean twoTimesTarget = false;
        if (targets.size() == 1 && targets.get(0).getTargets().size() == 1) {
            Target target = targets.get(0);
            if (target.getFirstTarget().equals(source.getSourceId())) {
                // Target was already the same source, so no change / target event to create
                return true;
            }
            if (target.canTarget(stackObject.getControllerId(), source.getSourceId(), sourceAbility, game)) {
                oldTargetName = getTargetName(targets.getFirstTarget(), game);
                target.clearChosen();
                // The source is still the spell on the stack
                target.addTarget(source.getSourceId(), stackObject.getStackAbility(), game);
            }
        } else {
            // needed for targeted source's with multiple targets
            Player controller = game.getPlayer(source.getControllerId());
            boolean validTargets = false;
            do {
                for (Target target : targets) {
                    if (target.getTargets().contains(source.getSourceId())) {
                        // You can't change a traget to Spellskite, if Spellskite is already targeted by this target
                        continue;
                    }
                    for (UUID targetId : target.getTargets()) {
                        String name = getTargetName(targetId, game);
                        if (target.canTarget(stackObject.getControllerId(), source.getSourceId(), sourceAbility, game)) {
                            validTargets = true;
                            if (name != null && controller.chooseUse(Outcome.Neutral, "Change target from " + name + " to " + sourceObject.getLogName() + '?', source, game)) {
                                oldTargetName = getTargetName(targetId, game);
                                int damageAmount = target.getTargetAmount(targetId);
                                target.remove(targetId);
                                // The source is still the spell on the stack
                                // add the source permanent id and set the damage if any
                                target.addTarget(source.getSourceId(), stackObject.getStackAbility(), game);
                                target.setTargetAmount(source.getSourceId(), damageAmount, game);
                                break;
                            }
                        }
                    }
                    if (oldTargetName != null) {
                        break;
                    }
                }
                if (oldTargetName == null) {
                    game.informPlayer(controller, "You have to select at least one target to change to " + sourceObject.getIdName() + '!');
                }
            } while (validTargets && oldTargetName == null);
        }
        if (oldTargetName != null) {
            game.informPlayers(sourceObject.getLogName() + ": Changed target of " + stackObject.getLogName() + " from " + oldTargetName + " to " + sourceObject.getLogName());
        } else if (twoTimesTarget) {
            game.informPlayers(sourceObject.getLogName() + ": Target not changed to " + sourceObject.getLogName() + " because its not valid to target it twice for " + stackObject.getLogName());
        } else {
            game.informPlayers(sourceObject.getLogName() + ": Target not changed to " + sourceObject.getLogName() + " because its no valid target for " + stackObject.getLogName());
        }
        return true;
    }
    return false;
}
Also used : SimpleActivatedAbility(mage.abilities.common.SimpleActivatedAbility) StackAbility(mage.game.stack.StackAbility) Ability(mage.abilities.Ability) Player(mage.players.Player) Mode(mage.abilities.Mode) TargetStackObject(mage.target.TargetStackObject) StackObject(mage.game.stack.StackObject) MageObject(mage.MageObject) Targets(mage.target.Targets) Spell(mage.game.stack.Spell) StackAbility(mage.game.stack.StackAbility) Target(mage.target.Target) UUID(java.util.UUID)

Example 18 with StackAbility

use of mage.game.stack.StackAbility in project mage by magefree.

the class PlayerImpl method playAbility.

protected boolean playAbility(ActivatedAbility ability, Game game) {
    // 20091005 - 602.2a
    if (ability.isUsesStack()) {
        if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.ACTIVATE_ABILITY, ability.getId(), ability, playerId))) {
            int bookmark = game.bookmarkState();
            // move global bookmark to current state (if you activated mana before then you can't rollback it)
            setStoredBookmark(bookmark);
            ability.newId();
            ability.setControllerId(playerId);
            game.getStack().push(new StackAbility(ability, playerId));
            if (ability.activate(game, false)) {
                game.fireEvent(GameEvent.getEvent(GameEvent.EventType.ACTIVATED_ABILITY, ability.getId(), ability, playerId));
                if (!game.isSimulation()) {
                    game.informPlayers(getLogName() + ability.getGameLogMessage(game));
                }
                game.removeBookmark(bookmark);
                resetStoredBookmark(game);
                return true;
            }
            restoreState(bookmark, ability.getRule(), game);
        }
    } else {
        int bookmark = game.bookmarkState();
        if (ability.activate(game, false)) {
            ability.resolve(game);
            game.removeBookmark(bookmark);
            resetStoredBookmark(game);
            return true;
        }
        restoreState(bookmark, ability.getRule(), game);
    }
    return false;
}
Also used : StackAbility(mage.game.stack.StackAbility)

Example 19 with StackAbility

use of mage.game.stack.StackAbility in project mage by magefree.

the class RowanKenrithEmblemTriggeredAbility method checkTrigger.

@Override
public boolean checkTrigger(GameEvent event, Game game) {
    if (!event.getPlayerId().equals(getControllerId())) {
        return false;
    }
    StackAbility stackAbility = (StackAbility) game.getStack().getStackObject(event.getSourceId());
    if (stackAbility == null || stackAbility.getStackAbility() instanceof ActivatedManaAbilityImpl) {
        return false;
    }
    this.getEffects().setValue("stackAbility", stackAbility);
    return true;
}
Also used : ActivatedManaAbilityImpl(mage.abilities.mana.ActivatedManaAbilityImpl) StackAbility(mage.game.stack.StackAbility)

Example 20 with StackAbility

use of mage.game.stack.StackAbility in project mage by magefree.

the class RevelInSilenceEffect method checkTrigger.

@Override
public boolean checkTrigger(GameEvent event, Game game) {
    if (!game.getOpponents(event.getPlayerId()).contains(getControllerId())) {
        return false;
    }
    StackAbility stackAbility = (StackAbility) game.getStack().getStackObject(event.getSourceId());
    if (stackAbility.getStackAbility() instanceof ActivatedManaAbilityImpl) {
        return false;
    }
    getEffects().setTargetPointer(new FixedTarget(event.getPlayerId()));
    return true;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) ActivatedManaAbilityImpl(mage.abilities.mana.ActivatedManaAbilityImpl) StackAbility(mage.game.stack.StackAbility)

Aggregations

StackAbility (mage.game.stack.StackAbility)42 Permanent (mage.game.permanent.Permanent)15 StackObject (mage.game.stack.StackObject)12 Player (mage.players.Player)11 Target (mage.target.Target)9 MageObject (mage.MageObject)8 Ability (mage.abilities.Ability)7 Spell (mage.game.stack.Spell)7 ActivatedManaAbilityImpl (mage.abilities.mana.ActivatedManaAbilityImpl)6 GameEvent (mage.game.events.GameEvent)6 Game (mage.game.Game)5 UUID (java.util.UUID)4 MageObjectReference (mage.MageObjectReference)4 PassAbility (mage.abilities.common.PassAbility)4 Mode (mage.abilities.Mode)3 Card (mage.cards.Card)3 FixedTarget (mage.target.targetpointer.FixedTarget)3 ActivatedAbility (mage.abilities.ActivatedAbility)2 LoyaltyAbility (mage.abilities.LoyaltyAbility)2 SpellAbility (mage.abilities.SpellAbility)2