Search in sources :

Example 36 with Game

use of mage.game.Game in project mage by magefree.

the class SparkFiendUpkeepEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        int roll = controller.rollDice(outcome, source, game, 6, 2, 0).stream().mapToInt(x -> x).sum();
        MageObject mageObject = game.getObject(source.getSourceId());
        if (mageObject instanceof Permanent) {
            Permanent sourcePermanent = (Permanent) mageObject;
            if (roll == 2 || roll == 3 || roll == 12) {
                // sacrifice
                sourcePermanent.sacrifice(source, game);
            } else if (roll == 7 || roll == 11) {
                // don't roll again
                game.getState().setValue("SparkFiend" + source.getSourceId().toString(), 0);
                // might apply if this ability was copied
                sourcePermanent.addInfo("roll counter", CardUtil.addToolTipMarkTags(""), game);
            } else {
                // note that total
                game.getState().setValue("SparkFiend" + source.getSourceId().toString(), roll);
                sourcePermanent.addInfo("roll counter", CardUtil.addToolTipMarkTags("Noted roll: " + roll), game);
            }
        }
        return true;
    }
    return false;
}
Also used : EntersBattlefieldTriggeredAbility(mage.abilities.common.EntersBattlefieldTriggeredAbility) Outcome(mage.constants.Outcome) OneShotEffect(mage.abilities.effects.OneShotEffect) CardUtil(mage.util.CardUtil) UUID(java.util.UUID) MageInt(mage.MageInt) SubType(mage.constants.SubType) TargetController(mage.constants.TargetController) Player(mage.players.Player) CardSetInfo(mage.cards.CardSetInfo) BeginningOfUpkeepTriggeredAbility(mage.abilities.common.BeginningOfUpkeepTriggeredAbility) Game(mage.game.Game) CardImpl(mage.cards.CardImpl) Permanent(mage.game.permanent.Permanent) CardType(mage.constants.CardType) MageObject(mage.MageObject) Ability(mage.abilities.Ability) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) MageObject(mage.MageObject)

Example 37 with Game

use of mage.game.Game in project mage by magefree.

the class VesuvanDoppelgangerCopyEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Permanent permanent = game.getPermanent(source.getSourceId());
    if (permanent == null) {
        permanent = game.getPermanentEntering(source.getSourceId());
    }
    final Permanent sourcePermanent = permanent;
    if (controller != null && sourcePermanent != null) {
        Target target = new TargetPermanent(new FilterCreaturePermanent("target creature (you copy from)"));
        target.setRequired(true);
        if (source instanceof SimpleStaticAbility) {
            target = new TargetPermanent(new FilterCreaturePermanent("creature (you copy from)"));
            target.setRequired(false);
            target.setNotTarget(true);
        }
        if (target.canChoose(source.getSourceId(), source.getControllerId(), game)) {
            controller.choose(Outcome.Copy, target, source.getSourceId(), game);
            Permanent copyFromPermanent = game.getPermanent(target.getFirstTarget());
            if (copyFromPermanent != null) {
                game.copyPermanent(copyFromPermanent, sourcePermanent.getId(), source, new CopyApplier() {

                    @Override
                    public boolean apply(Game game, MageObject blueprint, Ability source, UUID copyToObjectId) {
                        blueprint.getColor().setColor(sourcePermanent.getColor(game));
                        blueprint.getAbilities().add(new BeginningOfUpkeepTriggeredAbility(Zone.BATTLEFIELD, new VesuvanDoppelgangerCopyEffect(), TargetController.YOU, true, false, rule2));
                        return true;
                    }
                });
                return true;
            }
        }
    }
    return false;
}
Also used : SimpleStaticAbility(mage.abilities.common.SimpleStaticAbility) BeginningOfUpkeepTriggeredAbility(mage.abilities.common.BeginningOfUpkeepTriggeredAbility) Ability(mage.abilities.Ability) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) TargetPermanent(mage.target.TargetPermanent) SimpleStaticAbility(mage.abilities.common.SimpleStaticAbility) MageObject(mage.MageObject) BeginningOfUpkeepTriggeredAbility(mage.abilities.common.BeginningOfUpkeepTriggeredAbility) Target(mage.target.Target) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) Game(mage.game.Game) CopyApplier(mage.util.functions.CopyApplier) TargetPermanent(mage.target.TargetPermanent) UUID(java.util.UUID)

Example 38 with Game

use of mage.game.Game in project mage by magefree.

the class ViashinoBeyEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    TargetOpponent targetDefender = new TargetOpponent();
    if (controller != null) {
        game.getBattlefield().getAllActivePermanents(CardType.CREATURE, game).stream().filter((permanent) -> (filter.match(permanent, source.getSourceId(), source.getControllerId(), game))).forEachOrdered((permanent) -> {
            if (game.getOpponents(controller.getId()).size() > 1) {
                controller.choose(outcome.Benefit, targetDefender, source.getSourceId(), game);
            } else {
                targetDefender.add(game.getOpponents(controller.getId()).iterator().next(), game);
            }
            if (permanent.canAttack(targetDefender.getFirstTarget(), game)) {
                controller.declareAttacker(permanent.getId(), targetDefender.getFirstTarget(), game, false);
            }
        });
    }
    return false;
}
Also used : TargetOpponent(mage.target.common.TargetOpponent) AttacksTriggeredAbility(mage.abilities.common.AttacksTriggeredAbility) Outcome(mage.constants.Outcome) OneShotEffect(mage.abilities.effects.OneShotEffect) UUID(java.util.UUID) MageInt(mage.MageInt) SubType(mage.constants.SubType) TargetController(mage.constants.TargetController) Player(mage.players.Player) CardSetInfo(mage.cards.CardSetInfo) Game(mage.game.Game) CardImpl(mage.cards.CardImpl) CardType(mage.constants.CardType) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) Ability(mage.abilities.Ability) Player(mage.players.Player) TargetOpponent(mage.target.common.TargetOpponent)

Example 39 with Game

use of mage.game.Game in project mage by magefree.

the class BalanceEffect method choosePermanentsToKeep.

private void choosePermanentsToKeep(Game game, Ability source, Player controller, FilterControlledPermanent filterPermanent) {
    int lowestPermanentsCount = Integer.MAX_VALUE;
    for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
        Player player = game.getPlayer(playerId);
        if (player == null) {
            continue;
        }
        lowestPermanentsCount = Math.min(lowestPermanentsCount, game.getBattlefield().countAll(filterPermanent, player.getId(), game));
    }
    List<Permanent> permanentsToSacrifice = new ArrayList<>();
    for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
        Player player = game.getPlayer(playerId);
        if (player == null) {
            continue;
        }
        List<Permanent> allPermanentsOfType = game.getBattlefield().getActivePermanents(filterPermanent, player.getId(), source.getSourceId(), game);
        List<Permanent> permanentsToKeep = new ArrayList<>();
        if (lowestPermanentsCount > 0) {
            TargetControlledPermanent target = new TargetControlledPermanent(lowestPermanentsCount, lowestPermanentsCount, filterPermanent, true);
            if (target.choose(Outcome.Protect, player.getId(), source.getSourceId(), game)) {
                for (Permanent permanent : allPermanentsOfType) {
                    if (permanent != null && target.getTargets().contains(permanent.getId())) {
                        permanentsToKeep.add(permanent);
                    }
                }
            // Prevent possible cheat by disconnecting.  If no targets are chosen, just pick the first in the list.
            // https://github.com/magefree/mage/issues/4263
            } else {
                int numPermanents = 0;
                for (Permanent permanent : allPermanentsOfType) {
                    if (numPermanents >= lowestPermanentsCount) {
                        break;
                    }
                    if (permanent != null) {
                        permanentsToKeep.add(permanent);
                        numPermanents++;
                    }
                }
            }
        }
        List<Permanent> playerPermanentsToSacrifice = allPermanentsOfType.stream().filter(e -> !permanentsToKeep.contains(e)).collect(Collectors.toList());
        permanentsToSacrifice.addAll(playerPermanentsToSacrifice);
        if (!playerPermanentsToSacrifice.isEmpty()) {
            game.informPlayers(player.getLogName() + " chose permanents to be sacrificed: " + playerPermanentsToSacrifice.stream().map(Permanent::getLogName).collect(Collectors.joining(", ")));
        }
    }
    for (Permanent permanent : permanentsToSacrifice) {
        if (permanent != null) {
            permanent.sacrifice(source, game);
        }
    }
}
Also used : FilterCard(mage.filter.FilterCard) java.util(java.util) TargetCardInHand(mage.target.common.TargetCardInHand) Cards(mage.cards.Cards) Outcome(mage.constants.Outcome) OneShotEffect(mage.abilities.effects.OneShotEffect) CardsImpl(mage.cards.CardsImpl) Collectors(java.util.stream.Collectors) Player(mage.players.Player) Game(mage.game.Game) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) Permanent(mage.game.permanent.Permanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) Card(mage.cards.Card) FilterControlledLandPermanent(mage.filter.common.FilterControlledLandPermanent) Ability(mage.abilities.Ability) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) Player(mage.players.Player) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) Permanent(mage.game.permanent.Permanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) FilterControlledLandPermanent(mage.filter.common.FilterControlledLandPermanent)

Example 40 with Game

use of mage.game.Game in project mage by magefree.

the class PlayTheTopCardEffect method applies.

@Override
public boolean applies(UUID objectId, Ability source, UUID affectedControllerId, Game game) {
    // main card and all parts are checks in different calls.
    // two modes:
    // * can play cards (must check main card and allows any parts)
    // * can play lands/spells (must check specific part and allows specific part)
    // current card's part
    Card cardToCheck = game.getCard(objectId);
    if (cardToCheck == null) {
        return false;
    }
    if (this.canPlayCardOnly) {
        // check whole card instead part
        cardToCheck = cardToCheck.getMainCard();
    }
    // must be you
    if (!affectedControllerId.equals(source.getControllerId())) {
        return false;
    }
    Player cardOwner = game.getPlayer(cardToCheck.getOwnerId());
    Player controller = game.getPlayer(source.getControllerId());
    if (cardOwner == null || controller == null) {
        return false;
    }
    // must be your or opponents library
    switch(this.targetLibrary) {
        case YOU:
            {
                Card topCard = controller.getLibrary().getFromTop(game);
                if (topCard == null || !topCard.getId().equals(cardToCheck.getMainCard().getId())) {
                    return false;
                }
                break;
            }
        case OPPONENT:
            {
                if (!game.getOpponents(controller.getId()).contains(cardOwner.getId())) {
                    return false;
                }
                Card topCard = cardOwner.getLibrary().getFromTop(game);
                if (topCard == null || !topCard.getId().equals(cardToCheck.getMainCard().getId())) {
                    return false;
                }
                break;
            }
        case SOURCE_TARGETS:
            {
                UUID needCardId = cardToCheck.getMainCard().getId();
                if (CardUtil.getAllSelectedTargets(source, game).stream().map(game::getPlayer).filter(Objects::nonNull).noneMatch(player -> {
                    Card topCard = player.getLibrary().getFromTop(game);
                    return topCard != null && topCard.getId().equals(needCardId);
                })) {
                    return false;
                }
                break;
            }
        default:
            {
                return false;
            }
    }
    // can't cast without mana cost
    if (!cardToCheck.isLand(game) && cardToCheck.getManaCost().isEmpty()) {
        return false;
    }
    // must be correct card
    return filter.match(cardToCheck, source.getSourceId(), affectedControllerId, game);
}
Also used : FilterCard(mage.filter.FilterCard) Outcome(mage.constants.Outcome) CardUtil(mage.util.CardUtil) UUID(java.util.UUID) TargetController(mage.constants.TargetController) Player(mage.players.Player) Objects(java.util.Objects) Duration(mage.constants.Duration) Game(mage.game.Game) AsThoughEffectImpl(mage.abilities.effects.AsThoughEffectImpl) Locale(java.util.Locale) Card(mage.cards.Card) Ability(mage.abilities.Ability) AsThoughEffectType(mage.constants.AsThoughEffectType) Player(mage.players.Player) Objects(java.util.Objects) UUID(java.util.UUID) FilterCard(mage.filter.FilterCard) Card(mage.cards.Card)

Aggregations

Game (mage.game.Game)212 Ability (mage.abilities.Ability)139 Player (mage.players.Player)126 UUID (java.util.UUID)117 OneShotEffect (mage.abilities.effects.OneShotEffect)104 CardSetInfo (mage.cards.CardSetInfo)102 CardImpl (mage.cards.CardImpl)100 CardType (mage.constants.CardType)82 Outcome (mage.constants.Outcome)78 Permanent (mage.game.permanent.Permanent)66 MageInt (mage.MageInt)60 mage.constants (mage.constants)47 Zone (mage.constants.Zone)46 GameEvent (mage.game.events.GameEvent)44 Objects (java.util.Objects)41 StaticFilters (mage.filter.StaticFilters)40 Collectors (java.util.stream.Collectors)35 SubType (mage.constants.SubType)34 Card (mage.cards.Card)32 MageObjectReference (mage.MageObjectReference)30