Search in sources :

Example 96 with Target

use of mage.target.Target in project mage by magefree.

the class AmassEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    int xValue = amassNumber.calculate(game, source, this);
    Player player = game.getPlayer(source.getControllerId());
    if (player == null) {
        return false;
    }
    if (!game.getBattlefield().containsControlled(filter, source, game, 1)) {
        new CreateTokenEffect(new ZombieArmyToken()).apply(game, source);
    }
    Target target = new TargetPermanent(filter);
    target.setNotTarget(true);
    if (!player.choose(outcome, target, source.getSourceId(), game)) {
        return false;
    }
    Permanent permanent = game.getPermanent(target.getFirstTarget());
    if (permanent == null) {
        return false;
    }
    permanent.addCounters(CounterType.P1P1.createInstance(xValue), source.getControllerId(), source, game);
    this.amassedCreatureId = permanent.getId();
    return true;
}
Also used : Player(mage.players.Player) Target(mage.target.Target) FilterPermanent(mage.filter.FilterPermanent) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) Permanent(mage.game.permanent.Permanent) TargetPermanent(mage.target.TargetPermanent) CreateTokenEffect(mage.abilities.effects.common.CreateTokenEffect) TargetPermanent(mage.target.TargetPermanent) ZombieArmyToken(mage.game.permanent.token.ZombieArmyToken)

Example 97 with Target

use of mage.target.Target 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 98 with Target

use of mage.target.Target in project mage by magefree.

the class DiscardEachPlayerEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    // Store for each player the cards to discard, that's important because all discard shall happen at the same time
    Map<UUID, Cards> cardsToDiscard = new HashMap<>();
    if (controller == null) {
        return true;
    }
    int toDiscard = amount.calculate(game, source, this);
    // choose cards to discard
    for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
        Player player = game.getPlayer(playerId);
        if (player == null) {
            continue;
        }
        switch(targetController) {
            case NOT_YOU:
                if (playerId.equals(source.getControllerId())) {
                    continue;
                }
                break;
            case OPPONENT:
                if (!game.getOpponents(source.getControllerId()).contains(playerId)) {
                    continue;
                }
                break;
        }
        if (randomDiscard) {
            player.discard(toDiscard, true, false, source, game);
            continue;
        }
        int numberOfCardsToDiscard = Math.min(toDiscard, player.getHand().size());
        Cards cards = new CardsImpl();
        if (numberOfCardsToDiscard > 0) {
            Target target = new TargetDiscard(numberOfCardsToDiscard, numberOfCardsToDiscard, StaticFilters.FILTER_CARD, playerId);
            player.chooseTarget(outcome, target, source, game);
            cards.addAll(target.getTargets());
        }
        cardsToDiscard.put(playerId, cards);
    }
    if (randomDiscard) {
        return true;
    }
    // discard all choosen cards
    for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
        Player player = game.getPlayer(playerId);
        if (player == null) {
            continue;
        }
        player.discard(cardsToDiscard.get(playerId), false, source, game);
    }
    return true;
}
Also used : Player(mage.players.Player) Target(mage.target.Target) HashMap(java.util.HashMap) UUID(java.util.UUID) TargetDiscard(mage.target.common.TargetDiscard) Cards(mage.cards.Cards) CardsImpl(mage.cards.CardsImpl)

Example 99 with Target

use of mage.target.Target 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 100 with Target

use of mage.target.Target in project mage by magefree.

the class ComputerPlayer6 method listTargets.

/**
 * Return info about targets list (targeting objects)
 *
 * @param game
 * @param targets
 * @param format    example: my %s in data
 * @param emptyText default text for empty targets list
 * @return
 */
protected String listTargets(Game game, Targets targets, String format, String emptyText) {
    List<String> res = new ArrayList<>();
    for (Target target : targets) {
        for (UUID id : target.getTargets()) {
            MageObject object = game.getObject(id);
            if (object != null) {
                String prefix = "";
                if (target instanceof TargetAmount) {
                    prefix = " " + target.getTargetAmount(id) + "x ";
                }
                res.add(prefix + object.getIdName());
            }
        }
    }
    String info = String.join("; ", res);
    if (info.isEmpty()) {
        return emptyText;
    } else {
        return String.format(format, info);
    }
}
Also used : Target(mage.target.Target) MageObject(mage.MageObject) TargetAmount(mage.target.TargetAmount)

Aggregations

Target (mage.target.Target)385 Player (mage.players.Player)291 Permanent (mage.game.permanent.Permanent)223 UUID (java.util.UUID)155 Card (mage.cards.Card)86 TargetPermanent (mage.target.TargetPermanent)80 FilterCard (mage.filter.FilterCard)62 FilterPermanent (mage.filter.FilterPermanent)56 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)54 FixedTarget (mage.target.targetpointer.FixedTarget)49 TargetControlledCreaturePermanent (mage.target.common.TargetControlledCreaturePermanent)45 TargetControlledPermanent (mage.target.common.TargetControlledPermanent)45 MageObject (mage.MageObject)43 FilterControlledCreaturePermanent (mage.filter.common.FilterControlledCreaturePermanent)42 TargetCreaturePermanent (mage.target.common.TargetCreaturePermanent)38 TargetOpponent (mage.target.common.TargetOpponent)35 CardsImpl (mage.cards.CardsImpl)32 ArrayList (java.util.ArrayList)31 ContinuousEffect (mage.abilities.effects.ContinuousEffect)31 FilterControlledPermanent (mage.filter.common.FilterControlledPermanent)30