Search in sources :

Example 1 with Mode

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

the class BattlefieldThaumaturgeSpellsCostReductionEffect method getMaxPossibleTargetCreatures.

private int getMaxPossibleTargetCreatures(Ability ability, Game game) {
    // checks only one mode, so it can be wrong in rare use cases with multi-modes (example: mode one gives +2 and mode two gives another +1 -- total +3)
    int maxAmount = 0;
    for (Mode mode : ability.getModes().values()) {
        for (Target target : mode.getTargets()) {
            if (target.isNotTarget()) {
                continue;
            }
            Set<UUID> possibleList = target.possibleTargets(ability.getSourceId(), ability.getControllerId(), game);
            possibleList.removeIf(id -> {
                Permanent permanent = game.getPermanent(id);
                return permanent == null || !permanent.isCreature(game);
            });
            int possibleAmount = Math.min(possibleList.size(), target.getMaxNumberOfTargets());
            maxAmount = Math.max(maxAmount, possibleAmount);
        }
    }
    return maxAmount;
}
Also used : Target(mage.target.Target) Permanent(mage.game.permanent.Permanent) Mode(mage.abilities.Mode) UUID(java.util.UUID)

Example 2 with Mode

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

the class BeamsplitterMageApplier method checkTrigger.

@Override
public boolean checkTrigger(GameEvent event, Game game) {
    if (!isControlledBy(event.getPlayerId())) {
        return false;
    }
    Spell spell = game.getSpellOrLKIStack(event.getTargetId());
    if (spell == null || !spell.isInstantOrSorcery(game)) {
        return false;
    }
    if (spell.getSpellAbilities().stream().map(AbilityImpl::getModes).flatMap(m -> m.getSelectedModes().stream().map(m::get)).filter(Objects::nonNull).map(Mode::getTargets).flatMap(Collection::stream).filter(t -> !t.isNotTarget()).map(Target::getTargets).flatMap(Collection::stream).anyMatch(uuid -> !getSourceId().equals(uuid) && uuid != null)) {
        return false;
    }
    this.getEffects().setValue("spellCast", spell);
    return true;
}
Also used : Target(mage.target.Target) java.util(java.util) Zone(mage.constants.Zone) Predicate(mage.filter.predicate.Predicate) MageObjectReference(mage.MageObjectReference) MageObjectReferencePredicate(mage.filter.predicate.mageobject.MageObjectReferencePredicate) SubType(mage.constants.SubType) FilterPermanent(mage.filter.FilterPermanent) Mode(mage.abilities.Mode) Player(mage.players.Player) StackObjectCopyApplier(mage.util.functions.StackObjectCopyApplier) CardType(mage.constants.CardType) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) StackObject(mage.game.stack.StackObject) Spell(mage.game.stack.Spell) StaticFilters(mage.filter.StaticFilters) Outcome(mage.constants.Outcome) OneShotEffect(mage.abilities.effects.OneShotEffect) MageInt(mage.MageInt) CardSetInfo(mage.cards.CardSetInfo) TriggeredAbilityImpl(mage.abilities.TriggeredAbilityImpl) Game(mage.game.Game) AbilityImpl(mage.abilities.AbilityImpl) GameEvent(mage.game.events.GameEvent) CardImpl(mage.cards.CardImpl) Permanent(mage.game.permanent.Permanent) AnotherPredicate(mage.filter.predicate.mageobject.AnotherPredicate) TargetPermanent(mage.target.TargetPermanent) Ability(mage.abilities.Ability) TriggeredAbilityImpl(mage.abilities.TriggeredAbilityImpl) AbilityImpl(mage.abilities.AbilityImpl) Target(mage.target.Target) Spell(mage.game.stack.Spell)

Example 3 with Mode

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

the class CemeteryDesecratorRemoveCountersEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        TargetCardInGraveyard target = new TargetCardInGraveyard(filter);
        target.setNotTarget(true);
        controller.choose(outcome, target, source.getSourceId(), game);
        Card card = game.getCard(target.getFirstTarget());
        if (card != null) {
            int manaValue = card.getManaValue();
            if (controller.moveCards(card, Zone.EXILED, source, game)) {
                ReflexiveTriggeredAbility ability = new ReflexiveTriggeredAbility(new CemeteryDesecratorRemoveCountersEffect(manaValue), false, triggerText);
                ability.addTarget(new TargetPermanent());
                Mode mode = new Mode(new BoostTargetEffect(-manaValue, -manaValue, Duration.EndOfTurn).setText("Target creature an opponent controls gets -X/-X until end of turn, where X is the mana value of the exiled card"));
                mode.addTarget(new TargetOpponentsCreaturePermanent());
                ability.addMode(mode);
                game.fireReflexiveTriggeredAbility(ability, source);
                return true;
            }
        }
    }
    return false;
}
Also used : Player(mage.players.Player) TargetCardInGraveyard(mage.target.common.TargetCardInGraveyard) Mode(mage.abilities.Mode) BoostTargetEffect(mage.abilities.effects.common.continuous.BoostTargetEffect) TargetOpponentsCreaturePermanent(mage.target.common.TargetOpponentsCreaturePermanent) ReflexiveTriggeredAbility(mage.abilities.common.delayed.ReflexiveTriggeredAbility) TargetPermanent(mage.target.TargetPermanent) FilterCard(mage.filter.FilterCard) Card(mage.cards.Card)

Example 4 with Mode

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

the class GripOfChaosEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    StackObject stackObject = game.getStack().getStackObject(this.getTargetPointer().getFirst(game, source));
    if (stackObject != null) {
        for (UUID modeId : stackObject.getStackAbility().getModes().getSelectedModes()) {
            Mode mode = stackObject.getStackAbility().getModes().get(modeId);
            for (Target target : mode.getTargets()) {
                UUID oldTargetId = target.getFirstTarget();
                Set<UUID> possibleTargets = target.possibleTargets(stackObject.getSourceId(), stackObject.getControllerId(), game);
                if (possibleTargets.contains(stackObject.getId())) {
                    // The stackObject can't target itself
                    possibleTargets.remove(stackObject.getId());
                }
                if (!possibleTargets.isEmpty()) {
                    int i = 0;
                    int rnd = RandomUtil.nextInt(possibleTargets.size());
                    Iterator<UUID> it = possibleTargets.iterator();
                    while (i < rnd) {
                        it.next();
                        i++;
                    }
                    UUID newTargetId = it.next();
                    target.remove(oldTargetId);
                    target.add(newTargetId, game);
                }
            }
        }
        return true;
    }
    return false;
}
Also used : Target(mage.target.Target) FixedTarget(mage.target.targetpointer.FixedTarget) Mode(mage.abilities.Mode) StackObject(mage.game.stack.StackObject) UUID(java.util.UUID)

Example 5 with Mode

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

the class RicochetEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Spell spell = game.getStack().getSpell(this.getTargetPointer().getFirst(game, source));
    if (spell != null) {
        Targets targets = new Targets();
        Ability sourceAbility = spell.getSpellAbility();
        for (UUID modeId : sourceAbility.getModes().getSelectedModes()) {
            Mode mode = sourceAbility.getModes().get(modeId);
            targets.addAll(mode.getTargets());
        }
        if (targets.size() != 1 || targets.get(0).getTargets().size() != 1) {
            return false;
        }
        Map<Player, Integer> playerRolls = new HashMap<>();
        for (UUID playerId : game.getPlayerList().copy()) {
            Player player = game.getPlayer(playerId);
            if (player != null) {
                playerRolls.put(player, 7);
            }
        }
        do {
            for (Player player : playerRolls.keySet()) {
                // bad outcome - ai must choose lowest value
                playerRolls.put(player, player.rollDice(Outcome.Detriment, source, game, 6));
            }
            int minValueInMap = Collections.min(playerRolls.values());
            for (Map.Entry<Player, Integer> mapEntry : new HashSet<>(playerRolls.entrySet())) {
                if (mapEntry.getValue() > minValueInMap) {
                    playerRolls.remove(mapEntry.getKey());
                }
            }
        } while (playerRolls.size() > 1);
        if (playerRolls.size() == 1) {
            Player loser = (Player) playerRolls.keySet().toArray()[0];
            UUID loserId = loser.getId();
            Target target = targets.get(0);
            if (target.getFirstTarget().equals(loserId)) {
                return true;
            }
            String oldTargetName = null;
            if (target.canTarget(spell.getControllerId(), loserId, sourceAbility, game)) {
                Player oldPlayer = game.getPlayer(targets.getFirstTarget());
                if (oldPlayer != null) {
                    oldTargetName = oldPlayer.getLogName();
                }
                target.clearChosen();
                target.addTarget(loserId, sourceAbility, game);
            }
            MageObject sourceObject = game.getObject(source.getSourceId());
            if (oldTargetName != null && sourceObject != null) {
                game.informPlayers(sourceObject.getLogName() + ": Changed target of " + spell.getLogName() + " from " + oldTargetName + " to " + loser.getLogName());
            }
        }
        return true;
    }
    return false;
}
Also used : SpellCastAllTriggeredAbility(mage.abilities.common.SpellCastAllTriggeredAbility) Ability(mage.abilities.Ability) Player(mage.players.Player) ObjectSourcePlayer(mage.filter.predicate.ObjectSourcePlayer) HashMap(java.util.HashMap) Mode(mage.abilities.Mode) MageObject(mage.MageObject) Targets(mage.target.Targets) Spell(mage.game.stack.Spell) FilterSpell(mage.filter.FilterSpell) Target(mage.target.Target) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

Mode (mage.abilities.Mode)19 Target (mage.target.Target)16 UUID (java.util.UUID)11 Player (mage.players.Player)10 Spell (mage.game.stack.Spell)8 Ability (mage.abilities.Ability)7 StackObject (mage.game.stack.StackObject)7 Permanent (mage.game.permanent.Permanent)6 Effect (mage.abilities.effects.Effect)5 MageObject (mage.MageObject)4 MageObjectReference (mage.MageObjectReference)4 FixedTarget (mage.target.targetpointer.FixedTarget)4 ArrayList (java.util.ArrayList)3 MageInt (mage.MageInt)3 CardImpl (mage.cards.CardImpl)3 CardSetInfo (mage.cards.CardSetInfo)3 CardType (mage.constants.CardType)3 SubType (mage.constants.SubType)3 java.util (java.util)2 AbilityImpl (mage.abilities.AbilityImpl)2