Search in sources :

Example 26 with StackObject

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

the class TargetsHaveToTargetPermanentIfAbleEffect method applies.

@Override
public boolean applies(GameEvent event, Ability source, Game game) {
    Player controller = game.getPlayer(source.getControllerId());
    Player targetingPlayer = game.getPlayer(event.getPlayerId());
    if (controller != null && // TODO: This target handling does only work for non AI players so AI logic
    targetingPlayer.isHuman() && controller.hasOpponent(event.getPlayerId(), game)) {
        StackObject stackObject = game.getStack().getStackObject(event.getSourceId());
        if (stackObject.isCopy()) {
            return false;
        }
        Ability stackAbility = stackObject.getStackAbility();
        // Ensure that this ability is activated or a cast spell, because Flag Bearer effects don't require triggered abilities to choose a Standard Bearer
        if (!(stackAbility instanceof ActivatedAbility) && !(stackAbility instanceof SpellAbility)) {
            return false;
        }
        // Also check that targeting player controls the ability
        if (!stackAbility.isControlledBy(targetingPlayer.getId())) {
            return false;
        }
        Ability ability = (Ability) getValue("targetAbility");
        if (ability != null) {
            // Get all the allowed permanents on the battlefield in range of the abilities controller
            List<Permanent> allowedPermanents = game.getBattlefield().getActivePermanents(filter, event.getPlayerId(), event.getSourceId(), game);
            if (!allowedPermanents.isEmpty()) {
                boolean canTargetAllowedPermanent = false;
                for (UUID modeId : ability.getModes().getSelectedModes()) {
                    ability.getModes().setActiveMode(modeId);
                    for (Target target : ability.getTargets()) {
                        // Check if already targeted
                        for (Permanent allowedPermanent : allowedPermanents) {
                            if (target.getTargets().contains(allowedPermanent.getId())) {
                                return false;
                            }
                            if (target.canTarget(stackObject.getControllerId(), allowedPermanent.getId(), source, game)) {
                                canTargetAllowedPermanent = true;
                            }
                        }
                    }
                }
                return canTargetAllowedPermanent;
            }
        }
    }
    return false;
}
Also used : SpellAbility(mage.abilities.SpellAbility) ActivatedAbility(mage.abilities.ActivatedAbility) Ability(mage.abilities.Ability) Player(mage.players.Player) Target(mage.target.Target) FilterPermanent(mage.filter.FilterPermanent) Permanent(mage.game.permanent.Permanent) StackObject(mage.game.stack.StackObject) ActivatedAbility(mage.abilities.ActivatedAbility) SpellAbility(mage.abilities.SpellAbility) UUID(java.util.UUID)

Example 27 with StackObject

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

the class ComputerPlayer6 method resolve.

protected void resolve(SimulationNode2 node, int depth, Game game) {
    StackObject stackObject = game.getStack().getFirst();
    if (stackObject instanceof StackAbility) {
        // AI hint for search effects (calc all possible cards for best score)
        SearchEffect effect = getSearchEffect((StackAbility) stackObject);
        if (effect != null && stackObject.getControllerId().equals(playerId)) {
            Target target = effect.getTarget();
            if (!target.doneChosing()) {
                for (UUID targetId : target.possibleTargets(stackObject.getSourceId(), stackObject.getControllerId(), game)) {
                    Game sim = game.copy();
                    StackAbility newAbility = (StackAbility) stackObject.copy();
                    SearchEffect newEffect = getSearchEffect(newAbility);
                    newEffect.getTarget().addTarget(targetId, newAbility, sim);
                    sim.getStack().push(newAbility);
                    SimulationNode2 newNode = new SimulationNode2(node, sim, depth, stackObject.getControllerId());
                    node.children.add(newNode);
                    newNode.getTargets().add(targetId);
                    logger.trace("Sim search -- node#: " + SimulationNode2.getCount() + " for player: " + sim.getPlayer(stackObject.getControllerId()).getName());
                }
                return;
            }
        }
    }
    stackObject.resolve(game);
    if (stackObject instanceof StackAbility) {
        game.getStack().remove(stackObject, 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)

Example 28 with StackObject

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

the class InterdictCantActivateEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    StackObject stackObject = game.getStack().getStackObject(source.getFirstTarget());
    if (stackObject != null && game.getStack().counter(source.getFirstTarget(), source, game)) {
        Permanent sourcePermanent = stackObject.getStackAbility().getSourcePermanentIfItStillExists(game);
        if (sourcePermanent != null) {
            InterdictCantActivateEffect effect = new InterdictCantActivateEffect();
            effect.setTargetPointer(new FixedTarget(sourcePermanent, game));
            game.getContinuousEffects().addEffect(effect, source);
        }
        return true;
    }
    return false;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) Permanent(mage.game.permanent.Permanent) FilterStackObject(mage.filter.FilterStackObject) StackObject(mage.game.stack.StackObject)

Example 29 with StackObject

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

the class CommanderStormEffect method checkTrigger.

@Override
public boolean checkTrigger(GameEvent event, Game game) {
    if (!event.getSourceId().equals(getSourceId())) {
        return false;
    }
    StackObject spell = game.getStack().getStackObject(getSourceId());
    if (!(spell instanceof Spell)) {
        return false;
    }
    for (Effect effect : this.getEffects()) {
        effect.setValue("StormSpell", spell);
        effect.setValue("StormSpellRef", new MageObjectReference(spell.getId(), game));
    }
    return true;
}
Also used : StackObject(mage.game.stack.StackObject) OneShotEffect(mage.abilities.effects.OneShotEffect) Effect(mage.abilities.effects.Effect) Spell(mage.game.stack.Spell) MageObjectReference(mage.MageObjectReference)

Example 30 with StackObject

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

the class FoldIntoAetherEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    UUID targetId = getTargetPointer().getFirst(game, source);
    StackObject stackObject = game.getStack().getStackObject(targetId);
    Player spellController = null;
    if (stackObject != null) {
        spellController = game.getPlayer(stackObject.getControllerId());
    }
    if (game.getStack().counter(targetId, source, game)) {
        TargetCardInHand target = new TargetCardInHand(StaticFilters.FILTER_CARD_CREATURE);
        if (spellController != null && target.canChoose(source.getSourceId(), spellController.getId(), game) && spellController.chooseUse(Outcome.Neutral, "Put a creature card from your hand in play?", source, game) && spellController.choose(Outcome.PutCreatureInPlay, target, source.getSourceId(), game)) {
            Card card = game.getCard(target.getFirstTarget());
            if (card != null) {
                spellController.moveCards(card, Zone.BATTLEFIELD, source, game);
            }
        }
        return true;
    }
    return false;
}
Also used : Player(mage.players.Player) TargetCardInHand(mage.target.common.TargetCardInHand) StackObject(mage.game.stack.StackObject) UUID(java.util.UUID) Card(mage.cards.Card)

Aggregations

StackObject (mage.game.stack.StackObject)130 Player (mage.players.Player)63 Permanent (mage.game.permanent.Permanent)57 Spell (mage.game.stack.Spell)41 MageObject (mage.MageObject)37 UUID (java.util.UUID)36 Card (mage.cards.Card)29 Target (mage.target.Target)17 TargetPermanent (mage.target.TargetPermanent)15 FilterCard (mage.filter.FilterCard)14 Ability (mage.abilities.Ability)13 FilterPermanent (mage.filter.FilterPermanent)13 StackAbility (mage.game.stack.StackAbility)13 FixedTarget (mage.target.targetpointer.FixedTarget)12 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)11 HashSet (java.util.HashSet)10 Effect (mage.abilities.effects.Effect)9 Game (mage.game.Game)9 MageObjectReference (mage.MageObjectReference)8 Cost (mage.abilities.costs.Cost)8