Package mage.abilities.costs.common

Source Code of mage.abilities.costs.common.RemoveCounterCost

/*
*  Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
*
*  Redistribution and use in source and binary forms, with or without modification, are
*  permitted provided that the following conditions are met:
*
*     1. Redistributions of source code must retain the above copyright notice, this list of
*        conditions and the following disclaimer.
*
*     2. Redistributions in binary form must reproduce the above copyright notice, this list
*        of conditions and the following disclaimer in the documentation and/or other materials
*        provided with the distribution.
*
*  THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
*  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
*  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
*  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
*  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
*  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
*  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
*  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*  The views and conclusions contained in the software and documentation are those of the
*  authors and should not be interpreted as representing official policies, either expressed
*  or implied, of BetaSteward_at_googlemail.com.
*/

package mage.abilities.costs.common;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import mage.abilities.Ability;
import mage.abilities.costs.CostImpl;
import mage.choices.Choice;
import mage.choices.ChoiceImpl;
import mage.constants.Outcome;
import mage.counters.Counter;
import mage.counters.CounterType;
import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.players.Player;
import mage.target.TargetPermanent;
import mage.util.CardUtil;

/**
*
* @author LevelX
*/
public class RemoveCounterCost extends CostImpl {

    private TargetPermanent target;
    private String name;
    private CounterType counterTypeToRemove;
    private int countersToRemove;
   
    public RemoveCounterCost(TargetPermanent target) {
        this(target, null);
    }

    public RemoveCounterCost(TargetPermanent target, CounterType counterTypeToRemove) {
        this(target, counterTypeToRemove, 1);
    }

    public RemoveCounterCost(TargetPermanent target, CounterType counterTypeToRemove, int countersToRemove) {
        this.target = target;
        this.counterTypeToRemove = counterTypeToRemove;
        this.countersToRemove = countersToRemove;

        this.text = setText();
    }

    public RemoveCounterCost(final RemoveCounterCost cost) {
        super(cost);
        this.target = cost.target.copy();
        this.name = cost.name;
        this.countersToRemove = cost.countersToRemove;
        this.counterTypeToRemove = cost.counterTypeToRemove;
    }

    @Override
    public boolean pay(Ability ability, Game game, UUID sourceId, UUID controllerId, boolean noMana) {
        paid = false;
        int countersRemoved = 0;
        Player controller = game.getPlayer(controllerId);
        if (controller != null) {
            target.clearChosen();
            if (target.choose(Outcome.UnboostCreature, controllerId, sourceId, game)) {
                for (UUID targetId: (List<UUID>)target.getTargets()) {
                    Permanent permanent = game.getPermanent(targetId);
                    if (permanent != null) {
                        if (permanent.getCounters().size() > 0 && (counterTypeToRemove == null || permanent.getCounters().containsKey(counterTypeToRemove))) {
                            String counterName = null;

                            if (counterTypeToRemove != null) {
                                counterName = counterTypeToRemove.getName();
                            } else {
                                if (permanent.getCounters().size() > 1 && counterTypeToRemove == null) {
                                    Choice choice = new ChoiceImpl(true);
                                    Set<String> choices = new HashSet<>();
                                    for (Counter counter : permanent.getCounters().values()) {
                                        if (permanent.getCounters().getCount(counter.getName()) > 0) {
                                            choices.add(counter.getName());
                                        }
                                    }
                                    choice.setChoices(choices);
                                    choice.setMessage("Choose a counter to remove from " + permanent.getLogName());
                                    controller.choose(Outcome.UnboostCreature, choice, game);
                                    counterName = choice.getChoice();
                                } else {
                                    for (Counter counter : permanent.getCounters().values()) {
                                        if (counter.getCount() > 0) {
                                            counterName = counter.getName();
                                        }
                                    }
                                }
                            }
                            if (counterName != null) {
                                int countersLeft = countersToRemove - countersRemoved;
                                int countersOnPermanent = permanent.getCounters().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.getLogName()).toString(),  game);
                                }
                                permanent.removeCounters(counterName, numberOfCountersSelected, game);
                                if (permanent.getCounters().getCount(counterName) == 0 ){
                                    permanent.getCounters().removeCounter(counterName);
                                }
                                countersRemoved += numberOfCountersSelected;
                                game.informPlayers(new StringBuilder(controller.getName())
                                        .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;
    }

    @Override
    public boolean canPay(Ability ability, UUID sourceId, UUID controllerId, Game game) {
        return target.canChoose(controllerId, game);
    }

    private String setText() {
        StringBuilder sb = new StringBuilder("Remove ");
        sb.append(CardUtil.numberToText(countersToRemove, "a")).append(" ");
        if (counterTypeToRemove != null) {
            sb.append(counterTypeToRemove.getName());
        }
        sb.append(countersToRemove == 1 ? " counter from ":" counters from ").append(target.getMaxNumberOfTargets() == 1 ? "a ":"").append(target.getTargetName());
        return sb.toString();
    }

    @Override
    public RemoveCounterCost copy() {
        return new RemoveCounterCost(this);
    }
}
TOP

Related Classes of mage.abilities.costs.common.RemoveCounterCost

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.