Search in sources :

Example 6 with ActivatedAbility

use of mage.abilities.ActivatedAbility in project mage by magefree.

the class PatchworkCrawlerEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Permanent permanent = source.getSourcePermanentIfItStillExists(game);
    ExileZone exileZone = game.getExile().getExileZone(CardUtil.getExileZoneId(game, source));
    if (permanent == null || exileZone == null || exileZone.isEmpty()) {
        return false;
    }
    for (Card card : exileZone.getCards(StaticFilters.FILTER_CARD_CREATURE, game)) {
        for (Ability ability : card.getAbilities(game)) {
            if (ability instanceof ActivatedAbility) {
                permanent.addAbility(ability, source.getSourceId(), game);
            }
        }
    }
    return true;
}
Also used : SimpleActivatedAbility(mage.abilities.common.SimpleActivatedAbility) SimpleStaticAbility(mage.abilities.common.SimpleStaticAbility) ActivatedAbility(mage.abilities.ActivatedAbility) Ability(mage.abilities.Ability) Permanent(mage.game.permanent.Permanent) SimpleActivatedAbility(mage.abilities.common.SimpleActivatedAbility) ActivatedAbility(mage.abilities.ActivatedAbility) ExileZone(mage.game.ExileZone) Card(mage.cards.Card)

Example 7 with ActivatedAbility

use of mage.abilities.ActivatedAbility in project mage by magefree.

the class GainControlTargetEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller == null) {
        // controller no longer exists
        discard();
        return false;
    }
    boolean oneTargetStillExists = false;
    for (UUID permanentId : getTargetPointer().getTargets(game, source)) {
        Permanent permanent = game.getPermanent(permanentId);
        if (permanent == null) {
            continue;
        }
        oneTargetStillExists = true;
        if (permanent.isControlledBy(controllingPlayerId)) {
            continue;
        }
        boolean controlChanged = false;
        if (controllingPlayerId != null) {
            if (permanent.changeControllerId(controllingPlayerId, game, source)) {
                controlChanged = true;
            }
        } else {
            if (permanent.changeControllerId(source.getControllerId(), game, source)) {
                controlChanged = true;
            }
        }
        if (source instanceof ActivatedAbility && firstControlChange && !controlChanged) {
            // If it was not possible to get control of target permanent by the activated ability the first time it took place
            // the effect failed (e.g. because of Guardian Beast) and must be discarded
            // This does not handle correctly multiple targets at once
            discard();
        }
    }
    // no valid target exists and the controller is no longer in the game, effect can be discarded
    if (!oneTargetStillExists || !controller.isInGame()) {
        discard();
    }
    firstControlChange = false;
    return true;
}
Also used : Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) ActivatedAbility(mage.abilities.ActivatedAbility) UUID(java.util.UUID)

Example 8 with ActivatedAbility

use of mage.abilities.ActivatedAbility 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 9 with ActivatedAbility

use of mage.abilities.ActivatedAbility in project mage by magefree.

the class ComputerPlayer6 method act.

protected void act(Game game) {
    if (actions == null || actions.isEmpty()) {
        pass(game);
    } else {
        boolean usedStack = false;
        while (actions.peek() != null) {
            Ability ability = actions.poll();
            // example: ===> SELECTED ACTION for PlayerA: Play Swamp
            logger.info(String.format("===> SELECTED ACTION for %s: %s", getName(), ability.toString() + listTargets(game, ability.getTargets(), " (targeting %s)", "")));
            if (!ability.getTargets().isEmpty()) {
                for (Target target : ability.getTargets()) {
                    for (UUID id : target.getTargets()) {
                        target.updateTarget(id, game);
                        if (!target.isNotTarget()) {
                            game.addSimultaneousEvent(GameEvent.getEvent(GameEvent.EventType.TARGETED, id, ability, ability.getControllerId()));
                        }
                    }
                }
                Player player = game.getPlayer(ability.getFirstTarget());
                if (player != null) {
                    logger.info("targets = " + player.getName());
                }
            }
            this.activateAbility((ActivatedAbility) ability, game);
            if (ability.isUsesStack()) {
                usedStack = true;
            }
            if (!suggestedActions.isEmpty() && !(ability instanceof PassAbility)) {
                Iterator<String> it = suggestedActions.iterator();
                while (it.hasNext()) {
                    String action = it.next();
                    Card card = game.getCard(ability.getSourceId());
                    if (card != null && action.equals(card.getName())) {
                        logger.info("-> removed from suggested=" + action);
                        it.remove();
                    }
                }
            }
        }
        if (usedStack) {
            pass(game);
        }
    }
}
Also used : StaticAbility(mage.abilities.StaticAbility) ActivatedAbility(mage.abilities.ActivatedAbility) PassAbility(mage.abilities.common.PassAbility) StackAbility(mage.game.stack.StackAbility) SpellAbility(mage.abilities.SpellAbility) Ability(mage.abilities.Ability) Target(mage.target.Target) Player(mage.players.Player) PassAbility(mage.abilities.common.PassAbility) TargetCard(mage.target.TargetCard) Card(mage.cards.Card)

Example 10 with ActivatedAbility

use of mage.abilities.ActivatedAbility in project mage by magefree.

the class SimulatedPlayer2 method simulateOptions.

protected void simulateOptions(Game game) {
    List<ActivatedAbility> playables = game.getPlayer(playerId).getPlayable(game, isSimulatedPlayer);
    playables = filterAbilities(game, playables, suggested);
    for (ActivatedAbility ability : playables) {
        if (ability.getAbilityType() == AbilityType.MANA) {
            continue;
        }
        List<Ability> options = game.getPlayer(playerId).getPlayableOptions(ability, game);
        options = filterOptions(game, options, ability, suggested);
        options = optimizeOptions(game, options, ability);
        if (options.isEmpty()) {
            allActions.add(ability);
        } else {
            for (Ability option : options) {
                allActions.add(option);
            }
        }
    }
}
Also used : PassAbility(mage.abilities.common.PassAbility) StackAbility(mage.game.stack.StackAbility) TriggeredAbility(mage.abilities.TriggeredAbility) ActivatedAbility(mage.abilities.ActivatedAbility) Ability(mage.abilities.Ability) ActivatedAbility(mage.abilities.ActivatedAbility)

Aggregations

ActivatedAbility (mage.abilities.ActivatedAbility)21 Ability (mage.abilities.Ability)17 PassAbility (mage.abilities.common.PassAbility)11 Permanent (mage.game.permanent.Permanent)8 StackAbility (mage.game.stack.StackAbility)8 SpellAbility (mage.abilities.SpellAbility)7 Card (mage.cards.Card)7 Player (mage.players.Player)7 ArrayList (java.util.ArrayList)4 TriggeredAbility (mage.abilities.TriggeredAbility)4 SimpleStaticAbility (mage.abilities.common.SimpleStaticAbility)4 Game (mage.game.Game)4 UUID (java.util.UUID)3 File (java.io.File)2 java.util (java.util)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 MageObject (mage.MageObject)2 PlayLandAbility (mage.abilities.PlayLandAbility)2 StaticAbility (mage.abilities.StaticAbility)2