Search in sources :

Example 1 with StackAbility

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

the class SimulatedPlayerMCTS method triggerAbility.

@Override
public boolean triggerAbility(TriggeredAbility source, Game game) {
    // logger.info("trigger");
    if (source != null && source.canChooseTarget(game, playerId)) {
        Ability ability;
        List<Ability> options = getPlayableOptions(source, game);
        if (options.isEmpty()) {
            ability = source;
        } else {
            if (options.size() == 1) {
                ability = options.get(0);
            } else {
                ability = options.get(RandomUtil.nextInt(options.size()));
            }
        }
        if (ability.isUsesStack()) {
            game.getStack().push(new StackAbility(ability, playerId));
            if (ability.activate(game, false)) {
                game.fireEvent(new GameEvent(GameEvent.EventType.TRIGGERED_ABILITY, ability.getId(), ability, ability.getControllerId()));
                actionCount++;
                return true;
            }
        } else {
            if (ability.activate(game, false)) {
                ability.resolve(game);
                actionCount++;
                return true;
            }
        }
    }
    return false;
}
Also used : PassAbility(mage.abilities.common.PassAbility) StackAbility(mage.game.stack.StackAbility) GameEvent(mage.game.events.GameEvent) StackAbility(mage.game.stack.StackAbility)

Example 2 with StackAbility

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

the class SimulatedPlayer method addAbilityNode.

protected void addAbilityNode(SimulationNode parent, Ability ability, Game game) {
    Game sim = game.copy();
    sim.getStack().push(new StackAbility(ability, playerId));
    ability.activate(sim, false);
    if (ability.activate(sim, false) && ability.isUsesStack()) {
        game.fireEvent(new GameEvent(GameEvent.EventType.TRIGGERED_ABILITY, ability.getId(), ability, ability.getControllerId()));
    }
    sim.applyEffects();
    SimulationNode newNode = new SimulationNode(parent, sim, playerId);
    logger.debug(indent(newNode.getDepth()) + "simulating -- node #:" + SimulationNode.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 3 with StackAbility

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

the class SimulatedPlayer method triggerAbility.

@Override
public boolean triggerAbility(TriggeredAbility source, Game game) {
    Ability ability = source.copy();
    List<Ability> options = getPlayableOptions(ability, game);
    if (options.isEmpty()) {
        if (logger.isDebugEnabled())
            logger.debug("simulating -- triggered ability:" + ability);
        game.getStack().push(new StackAbility(ability, playerId));
        if (ability.activate(game, false) && ability.isUsesStack()) {
            game.fireEvent(new GameEvent(GameEvent.EventType.TRIGGERED_ABILITY, ability.getId(), ability, ability.getControllerId()));
        }
        game.applyEffects();
        game.getPlayers().resetPassed();
    } else {
        SimulationNode parent = (SimulationNode) game.getCustomData();
        if (parent.getDepth() == maxDepth)
            return true;
        logger.debug(indent(parent.getDepth()) + "simulating -- triggered ability - adding children:" + options.size());
        for (Ability option : options) {
            addAbilityNode(parent, option, game);
        }
    }
    return true;
}
Also used : PassAbility(mage.abilities.common.PassAbility) StackAbility(mage.game.stack.StackAbility) TriggeredAbility(mage.abilities.TriggeredAbility) SpellAbility(mage.abilities.SpellAbility) ActivatedAbility(mage.abilities.ActivatedAbility) Ability(mage.abilities.Ability) GameEvent(mage.game.events.GameEvent) StackAbility(mage.game.stack.StackAbility)

Example 4 with StackAbility

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

the class HauntingWindTriggeredAbility method checkTrigger.

@Override
public boolean checkTrigger(GameEvent event, Game game) {
    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;
        }
        String abilityText = stackAbility.getRule(true);
        if (abilityText.contains("{T}:") || abilityText.contains("{T},") || abilityText.contains("{T} or")) {
            return false;
        }
        for (Effect effect : this.getEffects()) {
            effect.setTargetPointer(new FixedTarget(permanent.getControllerId(), game));
        }
        return true;
    }
    if (event.getType() == GameEvent.EventType.TAPPED) {
        Permanent permanent = game.getPermanentOrLKIBattlefield(event.getTargetId());
        if (permanent == null || !permanent.isArtifact(game)) {
            return false;
        }
        for (Effect effect : this.getEffects()) {
            effect.setTargetPointer(new FixedTarget(permanent.getControllerId(), game));
        }
        return true;
    }
    return false;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) Permanent(mage.game.permanent.Permanent) Effect(mage.abilities.effects.Effect) DamageTargetEffect(mage.abilities.effects.common.DamageTargetEffect) StackAbility(mage.game.stack.StackAbility)

Example 5 with StackAbility

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

the class CopyStackAbilityEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    StackAbility ability = (StackAbility) getValue("stackAbility");
    if (controller == null || ability == null) {
        return false;
    }
    ability.createCopyOnStack(game, source, source.getControllerId(), true);
    return true;
}
Also used : Player(mage.players.Player) 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