use of mage.target.common.TargetOpponent in project mage by magefree.
the class Modes method choose.
public boolean choose(Game game, Ability source) {
if (this.isResetEachTurn()) {
if (getTurnNum(game, source) != game.getTurnNum()) {
this.clearAlreadySelectedModes(source, game);
setTurnNum(game, source);
}
}
if (this.size() > 1) {
this.clearSelectedModes();
if (this.isRandom) {
List<Mode> modes = getAvailableModes(source, game);
this.addSelectedMode(modes.get(RandomUtil.nextInt(modes.size())).getId());
return true;
}
// check if mode modifying abilities exist
Card card = game.getCard(source.getSourceId());
if (card != null) {
for (Ability modeModifyingAbility : card.getAbilities(game)) {
if (modeModifyingAbility instanceof OptionalAdditionalModeSourceCosts) {
// cost must check activation condition in changeModes
((OptionalAdditionalModeSourceCosts) modeModifyingAbility).changeModes(source, game);
}
}
}
// check if all modes can be activated automatically
if (this.size() == this.getMinModes() && !isEachModeMoreThanOnce()) {
Set<UUID> onceSelectedModes = null;
if (isEachModeOnlyOnce()) {
onceSelectedModes = getAlreadySelectedModes(source, game);
}
for (Mode mode : this.values()) {
if ((!isEachModeOnlyOnce() || onceSelectedModes == null || !onceSelectedModes.contains(mode.getId())) && mode.getTargets().canChoose(source.getSourceId(), source.getControllerId(), game)) {
this.addSelectedMode(mode.getId());
}
}
if (isEachModeOnlyOnce()) {
setAlreadySelectedModes(source, game);
}
return !selectedModes.isEmpty();
}
// 700.2d
// Some spells and abilities specify that a player other than their controller chooses a mode for it.
// In that case, the other player does so when the spell or ability's controller normally would do so.
// If there is more than one other player who could make such a choice, the spell or ability's controller decides which of those players will make the choice.
UUID playerId = null;
if (modeChooser == TargetController.OPPONENT) {
TargetOpponent targetOpponent = new TargetOpponent();
if (targetOpponent.choose(Outcome.Benefit, source.getControllerId(), source.getSourceId(), game)) {
playerId = targetOpponent.getFirstTarget();
}
} else {
playerId = source.getControllerId();
}
if (playerId == null) {
return false;
}
Player player = game.getPlayer(playerId);
// player chooses modes manually
this.currentMode = null;
int currentMaxModes = this.getMaxModes(game, source);
while (this.selectedModes.size() < currentMaxModes) {
Mode choice = player.chooseMode(this, source, game);
if (choice == null) {
if (isEachModeOnlyOnce()) {
setAlreadySelectedModes(source, game);
}
return this.selectedModes.size() >= this.getMinModes() || (this.selectedModes.size() == 0 && mayChooseNone);
}
this.addSelectedMode(choice.getId());
if (currentMode == null) {
currentMode = choice;
}
}
if (isEachModeOnlyOnce()) {
setAlreadySelectedModes(source, game);
}
return true;
} else {
// only one mode available
if (currentMode == null) {
this.clearSelectedModes();
Mode mode = this.values().iterator().next();
this.addSelectedMode(mode.getId());
this.setActiveMode(mode);
}
return true;
}
}
use of mage.target.common.TargetOpponent in project mage by magefree.
the class CankerAbominationEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
Permanent cankerAbomination = game.getPermanentEntering(source.getSourceId());
if (controller != null && cankerAbomination != null) {
Target target = new TargetOpponent();
target.setNotTarget(true);
controller.choose(outcome, target, source.getSourceId(), game);
Player opponent = game.getPlayer(target.getFirstTarget());
if (opponent != null) {
game.informPlayers(cankerAbomination.getName() + ": " + controller.getLogName() + " has chosen " + opponent.getLogName());
int amount = game.getBattlefield().getAllActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, opponent.getId(), game).size();
if (amount > 0) {
cankerAbomination.addCounters(CounterType.M1M1.createInstance(amount), source.getControllerId(), source, game);
}
return true;
}
}
return false;
}
use of mage.target.common.TargetOpponent in project mage by magefree.
the class DawnbreakReclaimerEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
/**
* 04.11.2015 If any opponent has a creature card in their graveyard as
* Dawnbreak Reclaimer's ability resolves, then you must choose one of
* those cards. You can't choose a different opponent with no creature
* cards in their graveyard to avoid returning one of those cards.
*
* 04.11.2015 If there are no creature cards in any opponent's graveyard
* as Dawnbreak Reclaimer's ability resolves, you'll still have the
* option to return a creature card from your graveyard to the
* battlefield. You choose which opponent will choose a creature card in
* your graveyard.
*/
Player controller = game.getPlayer(source.getControllerId());
MageObject sourceObject = source.getSourceObject(game);
if (controller != null && sourceObject != null) {
FilterCreatureCard filter = new FilterCreatureCard("a creature card in an opponent's graveyard");
filter.add(TargetController.OPPONENT.getOwnerPredicate());
TargetCard chosenCreatureOpponentGraveyard = new TargetCard(Zone.GRAVEYARD, filter);
Player opponent = null;
Card cardOpponentGraveyard = null;
chosenCreatureOpponentGraveyard.setNotTarget(true);
if (chosenCreatureOpponentGraveyard.canChoose(source.getSourceId(), source.getControllerId(), game)) {
controller.choose(Outcome.Detriment, chosenCreatureOpponentGraveyard, source.getSourceId(), game);
cardOpponentGraveyard = game.getCard(chosenCreatureOpponentGraveyard.getFirstTarget());
if (cardOpponentGraveyard != null) {
opponent = game.getPlayer(cardOpponentGraveyard.getOwnerId());
game.informPlayers(sourceObject.getLogName() + ": " + controller.getLogName() + " has chosen " + cardOpponentGraveyard.getIdName() + " of " + opponent.getLogName());
}
}
if (opponent == null) {
// if no card from opponent was available controller has to chose an opponent to select a creature card in controllers graveyard
TargetOpponent targetOpponent = new TargetOpponent(true);
controller.choose(outcome, targetOpponent, source.getSourceId(), game);
opponent = game.getPlayer(targetOpponent.getFirstTarget());
if (opponent != null) {
game.informPlayers(sourceObject.getLogName() + ": " + controller.getLogName() + " has chosen " + opponent.getLogName() + " to select a creature card from their graveyard");
}
}
if (opponent != null) {
FilterCreatureCard filterCreatureCard = new FilterCreatureCard("a creature card in " + controller.getName() + "'s the graveyard");
filterCreatureCard.add(new OwnerIdPredicate(controller.getId()));
TargetCardInGraveyard targetControllerGaveyard = new TargetCardInGraveyard(filterCreatureCard);
targetControllerGaveyard.setNotTarget(true);
Card controllerCreatureCard = null;
if (targetControllerGaveyard.canChoose(source.getSourceId(), opponent.getId(), game) && opponent.choose(outcome, targetControllerGaveyard, source.getSourceId(), game)) {
controllerCreatureCard = game.getCard(targetControllerGaveyard.getFirstTarget());
if (controllerCreatureCard != null) {
game.informPlayers(sourceObject.getLogName() + ": " + opponent.getLogName() + " has chosen " + controllerCreatureCard.getIdName() + " of " + controller.getLogName());
}
}
Set<Card> cards = new HashSet<>();
if (cardOpponentGraveyard != null) {
cards.add(cardOpponentGraveyard);
}
if (controllerCreatureCard != null) {
cards.add(controllerCreatureCard);
}
if (!cards.isEmpty()) {
if (controller.chooseUse(outcome, "Return those cards to the battlefield under their owners' control?", "Opponent's creature card: " + (cardOpponentGraveyard == null ? "none" : cardOpponentGraveyard.getLogName()) + ", your creature card: " + (controllerCreatureCard == null ? "none" : controllerCreatureCard.getLogName()), null, null, source, game)) {
controller.moveCards(cards, Zone.BATTLEFIELD, source, game, false, false, true, null);
}
}
}
return true;
}
return false;
}
use of mage.target.common.TargetOpponent in project mage by magefree.
the class EmergentUltimatumTarget method apply.
@Override
public boolean apply(Game game, Ability source) {
Player player = game.getPlayer(source.getControllerId());
if (player == null) {
return false;
}
TargetCardInLibrary targetCardInLibrary = new EmergentUltimatumTarget();
targetCardInLibrary.setNotTarget(true);
boolean searched = player.searchLibrary(targetCardInLibrary, source, game);
Cards cards = new CardsImpl(targetCardInLibrary.getTargets());
player.moveCards(cards, Zone.EXILED, source, game);
if (cards.isEmpty()) {
if (searched) {
player.shuffleLibrary(source, game);
}
return false;
}
TargetOpponent targetOpponent = new TargetOpponent();
targetOpponent.setNotTarget(true);
player.choose(outcome, targetOpponent, source.getSourceId(), game);
Player opponent = game.getPlayer(targetOpponent.getFirstTarget());
if (opponent == null) {
if (searched) {
player.shuffleLibrary(source, game);
}
return false;
}
TargetCardInExile targetCardInExile = new TargetCardInExile(StaticFilters.FILTER_CARD);
targetCardInExile.setNotTarget(true);
opponent.choose(outcome, cards, targetCardInExile, game);
Card toShuffle = game.getCard(targetCardInExile.getFirstTarget());
if (toShuffle != null) {
player.putCardsOnBottomOfLibrary(toShuffle, game, source, false);
player.shuffleLibrary(source, game);
cards.remove(toShuffle);
}
while (!cards.isEmpty()) {
if (!player.chooseUse(Outcome.PlayForFree, "Cast an exiled card without paying its mana cost?", source, game)) {
break;
}
targetCardInExile.clearChosen();
if (!player.choose(Outcome.PlayForFree, cards, targetCardInExile, game)) {
continue;
}
Card card = game.getCard(targetCardInExile.getFirstTarget());
if (card == null) {
continue;
}
game.getState().setValue("PlayFromNotOwnHandZone" + card.getId(), Boolean.TRUE);
Boolean cardWasCast = player.cast(player.chooseAbilityForCast(card, game, true), game, true, new ApprovingObject(source, game));
game.getState().setValue("PlayFromNotOwnHandZone" + card.getId(), null);
// remove on non cast too (infinite freeze fix)
cards.remove(card);
if (cardWasCast) {
cards.remove(card);
} else {
game.informPlayer(player, "You're not able to cast " + card.getIdName() + " or you canceled the casting.");
}
}
return true;
}
use of mage.target.common.TargetOpponent in project mage by magefree.
the class RealmsUnchartedTarget method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
MageObject sourceObject = game.getObject(source.getSourceId());
if (controller == null || sourceObject == null) {
return false;
}
RealmsUnchartedTarget target = new RealmsUnchartedTarget();
if (controller.searchLibrary(target, source, game)) {
if (!target.getTargets().isEmpty()) {
Cards cards = new CardsImpl();
for (UUID cardId : target.getTargets()) {
Card card = controller.getLibrary().getCard(cardId, game);
if (card != null) {
cards.add(card);
}
}
controller.revealCards(sourceObject.getName(), cards, game);
CardsImpl cardsToKeep = new CardsImpl();
if (cards.size() > 2) {
cardsToKeep.addAll(cards);
Player opponent;
Set<UUID> opponents = game.getOpponents(controller.getId());
if (opponents.size() == 1) {
opponent = game.getPlayer(opponents.iterator().next());
} else {
Target targetOpponent = new TargetOpponent(true);
controller.chooseTarget(Outcome.Detriment, targetOpponent, source, game);
opponent = game.getPlayer(targetOpponent.getFirstTarget());
}
TargetCard targetDiscard = new TargetCard(2, Zone.LIBRARY, new FilterCard("cards to put in graveyard"));
if (opponent != null && opponent.choose(Outcome.Discard, cards, targetDiscard, game)) {
cardsToKeep.removeAll(targetDiscard.getTargets());
cards.removeAll(cardsToKeep);
}
}
controller.moveCards(cards, Zone.GRAVEYARD, source, game);
controller.moveCards(cardsToKeep, Zone.HAND, source, game);
}
controller.shuffleLibrary(source, game);
return true;
}
controller.shuffleLibrary(source, game);
return false;
}
Aggregations