Search in sources :

Example 36 with StackAbility

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

the class CantBeTargetedAttachedEffect method applies.

@Override
public boolean applies(GameEvent event, Ability source, Game game) {
    Permanent attachment = game.getPermanent(source.getSourceId());
    if (attachment != null && event.getTargetId().equals(attachment.getAttachedTo())) {
        if (targetController == TargetController.OPPONENT && !game.getOpponents(source.getControllerId()).contains(event.getPlayerId())) {
            return false;
        }
        MageObject mageObject = game.getObject(event.getSourceId());
        MageObject sourceObject;
        if (mageObject instanceof StackAbility) {
            sourceObject = ((StackAbility) mageObject).getSourceObject(game);
        } else {
            sourceObject = mageObject;
        }
        if (mageObject != null && filterSource.match(sourceObject, game)) {
            return true;
        }
    }
    return false;
}
Also used : Permanent(mage.game.permanent.Permanent) MageObject(mage.MageObject) StackAbility(mage.game.stack.StackAbility)

Example 37 with StackAbility

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

the class CantBeTargetedAllEffect method applies.

@Override
public boolean applies(GameEvent event, Ability source, Game game) {
    Permanent permanent = game.getPermanent(event.getTargetId());
    if (permanent != null && filterTarget.match(permanent, source.getSourceId(), source.getControllerId(), game)) {
        StackObject stackObject = game.getStack().getStackObject(event.getSourceId());
        MageObject sourceObject;
        if (stackObject instanceof StackAbility) {
            if (filterSource instanceof FilterSpell) {
                // only spells have to be selected
                return false;
            }
            sourceObject = ((StackAbility) stackObject).getSourceObject(game);
        } else {
            sourceObject = stackObject;
        }
        if (filterSource.match(sourceObject, game)) {
            return true;
        }
    }
    return false;
}
Also used : FilterSpell(mage.filter.FilterSpell) FilterPermanent(mage.filter.FilterPermanent) Permanent(mage.game.permanent.Permanent) StackObject(mage.game.stack.StackObject) MageObject(mage.MageObject) StackAbility(mage.game.stack.StackAbility)

Example 38 with StackAbility

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

the class PlayerImpl method triggerAbility.

@Override
public boolean triggerAbility(TriggeredAbility triggeredAbility, Game game) {
    if (triggeredAbility == null) {
        logger.warn("Null source in triggerAbility method");
        throw new IllegalArgumentException("source TriggeredAbility  must not be null");
    }
    // 20091005 - 603.3c, 603.3d
    int bookmark = game.bookmarkState();
    TriggeredAbility ability = triggeredAbility.copy();
    MageObject sourceObject = ability.getSourceObject(game);
    if (sourceObject != null) {
        sourceObject.adjustTargets(ability, game);
    }
    UUID triggerId = null;
    if (ability.canChooseTarget(game, playerId)) {
        if (ability.isUsesStack()) {
            game.getStack().push(new StackAbility(ability, playerId));
        }
        if (ability.activate(game, false)) {
            if ((ability.isUsesStack() || ability.getRuleVisible()) && !game.isSimulation()) {
                game.informPlayers(getLogName() + " - " + ability.getGameLogMessage(game));
            }
            if (!ability.isUsesStack()) {
                ability.resolve(game);
            } else {
                game.fireEvent(new GameEvent(GameEvent.EventType.TRIGGERED_ABILITY, ability.getId(), ability, ability.getControllerId()));
                triggerId = ability.getId();
            }
            game.removeBookmark(bookmark);
            return true;
        }
    }
    // why restore is needed here? (to remove the triggered ability from the stack because of no possible targets)
    restoreState(bookmark, triggeredAbility.getRule(), game);
    GameEvent event = new GameEvent(GameEvent.EventType.ABILITY_TRIGGERED, triggerId, ability, ability.getControllerId());
    game.getState().setValue(event.getId().toString(), ability.getTriggerEvent());
    game.fireEvent(event);
    return false;
}
Also used : AtTheEndOfTurnStepPostDelayedTriggeredAbility(mage.abilities.common.delayed.AtTheEndOfTurnStepPostDelayedTriggeredAbility) StackAbility(mage.game.stack.StackAbility)

Example 39 with StackAbility

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

the class SimulatedPlayer2 method addAbilityNode.

protected void addAbilityNode(SimulationNode2 parent, Ability ability, int depth, Game game) {
    Game sim = game.copy();
    sim.getStack().push(new StackAbility(ability, playerId));
    if (ability.activate(sim, false) && ability.isUsesStack()) {
        game.fireEvent(new GameEvent(GameEvent.EventType.TRIGGERED_ABILITY, ability.getId(), ability, ability.getControllerId()));
    }
    sim.applyEffects();
    SimulationNode2 newNode = new SimulationNode2(parent, sim, depth, playerId);
    logger.debug("simulating -- node #:" + SimulationNode2.getCount() + " triggered ability option");
    for (Target target : ability.getTargets()) {
        for (UUID targetId : target.getTargets()) {
            newNode.getTargets().add(targetId);
        }
    }
    parent.children.add(newNode);
}
Also used : Target(mage.target.Target) Game(mage.game.Game) GameEvent(mage.game.events.GameEvent) StackAbility(mage.game.stack.StackAbility)

Example 40 with StackAbility

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

the class ComputerPlayer2 method resolve.

protected void resolve(SimulationNode node, Game game) {
    StackObject ability = game.getStack().pop();
    if (ability instanceof StackAbility) {
        SearchEffect effect = getSearchEffect((StackAbility) ability);
        if (effect != null && ability.getControllerId().equals(playerId)) {
            Target target = effect.getTarget();
            if (!target.doneChosing()) {
                for (UUID targetId : target.possibleTargets(ability.getSourceId(), ability.getControllerId(), game)) {
                    Game sim = game.copy();
                    StackAbility newAbility = (StackAbility) ability.copy();
                    SearchEffect newEffect = getSearchEffect((StackAbility) newAbility);
                    newEffect.getTarget().addTarget(targetId, newAbility, sim);
                    sim.getStack().push(newAbility);
                    SimulationNode newNode = new SimulationNode(node, sim, ability.getControllerId());
                    node.children.add(newNode);
                    newNode.getTargets().add(targetId);
                    logger.debug(indent(node.depth) + "simulating search -- node#: " + SimulationNode.getCount() + "for player: " + sim.getPlayer(ability.getControllerId()).getName());
                }
                return;
            }
        }
    }
    logger.debug(indent(node.depth) + "simulating resolve ");
    ability.resolve(game);
    game.applyEffects();
    game.getPlayers().resetPassed();
    game.getPlayerList().setCurrent(game.getActivePlayerId());
}
Also used : SearchEffect(mage.abilities.effects.SearchEffect) Target(mage.target.Target) Game(mage.game.Game) StackObject(mage.game.stack.StackObject) 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