Search in sources :

Example 36 with Counter

use of mage.counters.Counter in project mage by magefree.

the class DismantleEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Permanent permanent = game.getPermanent(getTargetPointer().getFirst(game, source));
    if (controller != null) {
        if (permanent != null) {
            int counterCount = 0;
            counterCount = permanent.getCounters(game).values().stream().map((counter) -> counter.getCount()).reduce(counterCount, Integer::sum);
            permanent.destroy(source, game, false);
            if (counterCount > 0) {
                Target target = new TargetControlledPermanent(1, 1, new FilterControlledArtifactPermanent("an artifact you control"), true);
                if (target.canChoose(source.getSourceId(), controller.getId(), game)) {
                    controller.chooseTarget(Outcome.Benefit, target, source, game);
                    Permanent artifact = game.getPermanent(target.getFirstTarget());
                    Counter counter;
                    if (controller.chooseUse(Outcome.BoostCreature, "Choose which kind of counters to add", null, "+1/+1 counters", "Charge counters", source, game)) {
                        counter = CounterType.P1P1.createInstance(counterCount);
                    } else {
                        counter = CounterType.CHARGE.createInstance(counterCount);
                    }
                    if (artifact != null) {
                        artifact.addCounters(counter, source.getControllerId(), source, game);
                    }
                }
            }
        }
        return true;
    }
    return false;
}
Also used : TargetControlledPermanent(mage.target.common.TargetControlledPermanent) Player(mage.players.Player) Target(mage.target.Target) Counter(mage.counters.Counter) FilterControlledArtifactPermanent(mage.filter.common.FilterControlledArtifactPermanent) TargetArtifactPermanent(mage.target.common.TargetArtifactPermanent) Permanent(mage.game.permanent.Permanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) FilterControlledArtifactPermanent(mage.filter.common.FilterControlledArtifactPermanent)

Example 37 with Counter

use of mage.counters.Counter in project mage by magefree.

the class ThornmantleStrikerEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Permanent permanent = game.getPermanent(source.getFirstTarget());
    if (controller == null || permanent == null) {
        return false;
    }
    int elves = xValue.calculate(game, source, this);
    if (elves < 1) {
        return false;
    }
    // Make copy of counters to avoid concurrent modification exception
    Counters counters = permanent.getCounters(game).copy();
    int totalCounters = 0;
    for (Counter counter : counters.values()) {
        totalCounters += counter.getCount();
    }
    if (totalCounters == 0) {
        return false;
    }
    if (totalCounters <= elves) {
        for (Map.Entry<String, Counter> entry : counters.entrySet()) {
            permanent.removeCounters(entry.getKey(), entry.getValue().getCount(), source, game);
        }
        return true;
    }
    if (counters.size() == 1) {
        String counterName = counters.keySet().iterator().next();
        permanent.removeCounters(counterName, elves, source, game);
        return true;
    }
    int remainingCounters = totalCounters;
    int countersLeftToRemove = elves;
    for (Map.Entry<String, Counter> entry : counters.entrySet()) {
        String counterName = entry.getKey();
        int numCounters = entry.getValue().getCount();
        remainingCounters -= numCounters;
        int min = Math.max(0, countersLeftToRemove - remainingCounters);
        int max = Math.min(countersLeftToRemove, numCounters);
        int toRemove = controller.getAmount(min, max, counterName + " counters to remove", game);
        // Sanity check in case of GUI bugs/disconnects
        toRemove = Math.max(toRemove, min);
        toRemove = Math.min(toRemove, max);
        permanent.removeCounters(counterName, toRemove, source, game);
        countersLeftToRemove -= toRemove;
        if (countersLeftToRemove <= 0) {
            break;
        }
    }
    return true;
}
Also used : Player(mage.players.Player) Counter(mage.counters.Counter) TargetOpponentsCreaturePermanent(mage.target.common.TargetOpponentsCreaturePermanent) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) Permanent(mage.game.permanent.Permanent) TargetPermanent(mage.target.TargetPermanent) Counters(mage.counters.Counters) Map(java.util.Map)

Example 38 with Counter

use of mage.counters.Counter in project mage by magefree.

the class VorelOfTheHullCladeEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Permanent target = game.getPermanent(source.getFirstTarget());
    if (target == null) {
        return false;
    }
    for (Counter counter : target.getCounters(game).values()) {
        Counter newCounter = new Counter(counter.getName(), counter.getCount());
        target.addCounters(newCounter, source.getControllerId(), source, game);
    }
    return true;
}
Also used : Counter(mage.counters.Counter) FilterPermanent(mage.filter.FilterPermanent) Permanent(mage.game.permanent.Permanent) TargetPermanent(mage.target.TargetPermanent)

Example 39 with Counter

use of mage.counters.Counter in project mage by magefree.

the class RemoveCounterCost method pay.

@Override
public boolean pay(Ability ability, Game game, Ability source, UUID controllerId, boolean noMana, Cost costToPay) {
    paid = false;
    int countersRemoved = 0;
    Player controller = game.getPlayer(controllerId);
    if (controller != null) {
        if (countersToRemove == 0) {
            // Can happen when used for X costs where X = 0;
            return paid = true;
        }
        target.clearChosen();
        if (target.choose(Outcome.UnboostCreature, controllerId, source.getSourceId(), game)) {
            for (UUID targetId : target.getTargets()) {
                Permanent permanent = game.getPermanent(targetId);
                if (permanent != null) {
                    if (!permanent.getCounters(game).isEmpty() && (counterTypeToRemove == null || permanent.getCounters(game).containsKey(counterTypeToRemove))) {
                        String counterName = null;
                        if (counterTypeToRemove != null) {
                            counterName = counterTypeToRemove.getName();
                        } else if (permanent.getCounters(game).size() > 1 && counterTypeToRemove == null) {
                            Choice choice = new ChoiceImpl(true);
                            Set<String> choices = new HashSet<>();
                            for (Counter counter : permanent.getCounters(game).values()) {
                                if (permanent.getCounters(game).getCount(counter.getName()) > 0) {
                                    choices.add(counter.getName());
                                }
                            }
                            choice.setChoices(choices);
                            choice.setMessage("Choose a counter to remove from " + permanent.getLogName());
                            if (!controller.choose(Outcome.UnboostCreature, choice, game)) {
                                return false;
                            }
                            counterName = choice.getChoice();
                        } else {
                            for (Counter counter : permanent.getCounters(game).values()) {
                                if (counter.getCount() > 0) {
                                    counterName = counter.getName();
                                }
                            }
                        }
                        if (counterName != null && !counterName.isEmpty()) {
                            int countersLeft = countersToRemove - countersRemoved;
                            int countersOnPermanent = permanent.getCounters(game).getCount(counterName);
                            int numberOfCountersSelected = 1;
                            if (countersLeft > 1 && countersOnPermanent > 1) {
                                numberOfCountersSelected = controller.getAmount(1, Math.min(countersLeft, countersOnPermanent), new StringBuilder("Remove how many counters from ").append(permanent.getIdName()).toString(), game);
                            }
                            permanent.removeCounters(counterName, numberOfCountersSelected, source, game);
                            countersRemoved += numberOfCountersSelected;
                            if (!game.isSimulation()) {
                                game.informPlayers(new StringBuilder(controller.getLogName()).append(" removes ").append(numberOfCountersSelected == 1 ? "a" : numberOfCountersSelected).append(' ').append(counterName).append(numberOfCountersSelected == 1 ? " counter from " : " counters from ").append(permanent.getName()).toString());
                            }
                            if (countersRemoved == countersToRemove) {
                                this.paid = true;
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
    return paid;
}
Also used : Player(mage.players.Player) Choice(mage.choices.Choice) Set(java.util.Set) HashSet(java.util.HashSet) Counter(mage.counters.Counter) Permanent(mage.game.permanent.Permanent) TargetPermanent(mage.target.TargetPermanent) ChoiceImpl(mage.choices.ChoiceImpl) UUID(java.util.UUID)

Example 40 with Counter

use of mage.counters.Counter in project mage by magefree.

the class PlayerImpl method addCounters.

@Override
public boolean addCounters(Counter counter, UUID playerAddingCounters, Ability source, Game game) {
    boolean returnCode = true;
    GameEvent addingAllEvent = GameEvent.getEvent(GameEvent.EventType.ADD_COUNTERS, playerId, source, playerAddingCounters, counter.getName(), counter.getCount());
    if (!game.replaceEvent(addingAllEvent)) {
        int amount = addingAllEvent.getAmount();
        int finalAmount = amount;
        boolean isEffectFlag = addingAllEvent.getFlag();
        for (int i = 0; i < amount; i++) {
            Counter eventCounter = counter.copy();
            eventCounter.remove(eventCounter.getCount() - 1);
            GameEvent addingOneEvent = GameEvent.getEvent(GameEvent.EventType.ADD_COUNTER, playerId, source, playerAddingCounters, counter.getName(), 1);
            addingOneEvent.setFlag(isEffectFlag);
            if (!game.replaceEvent(addingOneEvent)) {
                getCounters().addCounter(eventCounter);
                GameEvent addedOneEvent = GameEvent.getEvent(GameEvent.EventType.COUNTER_ADDED, playerId, source, playerAddingCounters, counter.getName(), 1);
                addedOneEvent.setFlag(addingOneEvent.getFlag());
                game.fireEvent(addedOneEvent);
            } else {
                finalAmount--;
                returnCode = false;
            }
        }
        if (finalAmount > 0) {
            GameEvent addedAllEvent = GameEvent.getEvent(GameEvent.EventType.COUNTERS_ADDED, playerId, source, playerAddingCounters, counter.getName(), amount);
            addedAllEvent.setFlag(addingAllEvent.getFlag());
            game.fireEvent(addedAllEvent);
        }
    } else {
        returnCode = false;
    }
    return returnCode;
}
Also used : Counter(mage.counters.Counter)

Aggregations

Counter (mage.counters.Counter)49 Permanent (mage.game.permanent.Permanent)38 Player (mage.players.Player)28 Choice (mage.choices.Choice)10 ChoiceImpl (mage.choices.ChoiceImpl)10 TargetPermanent (mage.target.TargetPermanent)10 UUID (java.util.UUID)9 HashSet (java.util.HashSet)8 Counters (mage.counters.Counters)8 TargetCreaturePermanent (mage.target.common.TargetCreaturePermanent)8 Card (mage.cards.Card)6 CounterType (mage.counters.CounterType)5 FilterPermanent (mage.filter.FilterPermanent)5 Target (mage.target.Target)4 TargetPermanentOrPlayer (mage.target.common.TargetPermanentOrPlayer)4 ArrayList (java.util.ArrayList)3 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)3 SimpleStaticAbility (mage.abilities.common.SimpleStaticAbility)2 Effect (mage.abilities.effects.Effect)2 OneShotEffect (mage.abilities.effects.OneShotEffect)2