use of mage.constants.Outcome in project mage by magefree.
the class BloodForBonesEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player player = game.getPlayer(source.getControllerId());
if (player == null || player.getGraveyard().getCards(game).stream().noneMatch(card -> card.isCreature(game))) {
return false;
}
TargetCardInYourGraveyard target = new TargetCardInYourGraveyard(filter);
if (player.choose(outcome, player.getGraveyard(), target, game)) {
player.moveCards(new CardsImpl(target.getTargets()), Zone.BATTLEFIELD, source, game);
}
if (player.getGraveyard().getCards(game).stream().noneMatch(card -> card.isCreature(game))) {
return true;
}
target = new TargetCardInYourGraveyard(filter2);
if (player.choose(outcome, player.getGraveyard(), target, game)) {
player.moveCards(new CardsImpl(target.getTargets()), Zone.HAND, source, game);
}
return true;
}
use of mage.constants.Outcome in project mage by magefree.
the class DoIfCostPaid method apply.
@Override
public boolean apply(Game game, Ability source) {
Player player = getPayingPlayer(game, source);
MageObject mageObject = game.getObject(source.getSourceId());
if (player != null && mageObject != null) {
String message;
if (chooseUseText == null) {
String effectText = executingEffects.getText(source.getModes().getMode());
if (!effectText.isEmpty() && effectText.charAt(effectText.length() - 1) == '.') {
effectText = effectText.substring(0, effectText.length() - 1);
}
message = CardUtil.addCostVerb(cost.getText()) + (effectText.isEmpty() ? "" : " and " + effectText) + "?";
message = Character.toUpperCase(message.charAt(0)) + message.substring(1);
} else {
message = chooseUseText;
}
message = CardUtil.replaceSourceName(message, mageObject.getName());
boolean result = true;
Outcome payOutcome = executingEffects.getOutcome(source, this.outcome);
if (cost.canPay(source, source, player.getId(), game) && (!optional || player.chooseUse(payOutcome, message, source, game))) {
cost.clearPaid();
int bookmark = game.bookmarkState();
if (cost.pay(source, game, source, player.getId(), false)) {
game.informPlayers(player.getLogName() + " paid for " + mageObject.getLogName() + " - " + message);
if (!executingEffects.isEmpty()) {
for (Effect effect : executingEffects) {
effect.setTargetPointer(this.targetPointer);
if (effect instanceof OneShotEffect) {
result &= effect.apply(game, source);
} else {
game.addEffect((ContinuousEffect) effect, source);
}
}
}
// otherwise you can e.g. undo card drawn with Mentor of the Meek
player.resetStoredBookmark(game);
} else {
// Paying cost was cancels so try to undo payment so far
player.restoreState(bookmark, DoIfCostPaid.class.getName(), game);
if (!otherwiseEffects.isEmpty()) {
for (Effect effect : otherwiseEffects) {
effect.setTargetPointer(this.targetPointer);
if (effect instanceof OneShotEffect) {
result &= effect.apply(game, source);
} else {
game.addEffect((ContinuousEffect) effect, source);
}
}
}
}
} else if (!otherwiseEffects.isEmpty()) {
for (Effect effect : otherwiseEffects) {
effect.setTargetPointer(this.targetPointer);
if (effect instanceof OneShotEffect) {
result &= effect.apply(game, source);
} else {
game.addEffect((ContinuousEffect) effect, source);
}
}
}
return result;
}
return false;
}
use of mage.constants.Outcome in project mage by magefree.
the class DoWhenCostPaid method apply.
@Override
public boolean apply(Game game, Ability source) {
Player player = game.getPlayer(source.getControllerId());
MageObject mageObject = game.getObject(source.getSourceId());
if (player == null || mageObject == null) {
return false;
}
String message = CardUtil.replaceSourceName(chooseUseText, mageObject.getLogName());
Outcome payOutcome = ability.getEffects().getOutcome(source, this.outcome);
if (!cost.canPay(source, source, player.getId(), game) || (optional && !player.chooseUse(payOutcome, message, source, game))) {
return false;
}
cost.clearPaid();
int bookmark = game.bookmarkState();
if (cost.pay(source, game, source, player.getId(), false)) {
if (ability.getTargets().isEmpty()) {
ability.getEffects().setTargetPointer(getTargetPointer());
}
game.fireReflexiveTriggeredAbility(ability, source);
player.resetStoredBookmark(game);
return true;
}
player.restoreState(bookmark, DoWhenCostPaid.class.getName(), game);
return true;
}
use of mage.constants.Outcome in project mage by magefree.
the class GenesisUltimatumEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player player = game.getPlayer(source.getControllerId());
if (player == null) {
return false;
}
Cards toHand = new CardsImpl(player.getLibrary().getTopCards(game, 5));
player.lookAtCards(source, null, toHand, game);
TargetCard targetCard = new TargetCardInLibrary(0, 5, filter);
targetCard.withChooseHint("put to battlefield");
player.choose(outcome, toHand, targetCard, game);
Cards toBattlefield = new CardsImpl(targetCard.getTargets());
if (player.moveCards(toBattlefield, Zone.BATTLEFIELD, source, game)) {
toBattlefield.stream().filter(id -> Zone.BATTLEFIELD.equals(game.getState().getZone(id))).forEach(toHand::remove);
}
player.moveCards(toHand, Zone.HAND, source, game);
return true;
}
use of mage.constants.Outcome in project mage by magefree.
the class StackObjectImpl method chooseNewTarget.
/**
* Handles the change of one target instance of a mode
*
* @param targetController - player that can choose the new target
* @param ability
* @param mode
* @param target
* @param forceChange
* @param newTargetFilterPredicate
* @param game
* @return
*/
private Target chooseNewTarget(Player targetController, Ability ability, Mode mode, Target target, boolean forceChange, Predicate newTargetFilterPredicate, Game game) {
Target newTarget = target.copy();
// filter targets
if (newTargetFilterPredicate != null) {
newTarget.getFilter().add(newTargetFilterPredicate);
// If adding a predicate, there will only be one choice and therefore target can be automatic
newTarget.setRandom(true);
}
newTarget.setEventReporting(false);
if (!targetController.getId().equals(getControllerId())) {
// target controller for the change is different from spell controller
newTarget.setTargetController(targetController.getId());
newTarget.setAbilityController(getControllerId());
}
newTarget.clearChosen();
for (UUID targetId : target.getTargets()) {
String targetNames = getNamesOftargets(targetId, game);
String targetAmount = "";
if (target.getTargetAmount(targetId) > 0) {
targetAmount = " (amount: " + target.getTargetAmount(targetId) + ")";
}
// change the target?
Outcome outcome = mode.getEffects().getOutcome(ability);
if (targetNames != null && (forceChange || targetController.chooseUse(outcome, "Change this target: " + targetNames + targetAmount + '?', ability, game))) {
Set<UUID> possibleTargets = target.possibleTargets(this.getSourceId(), getControllerId(), game);
// choose exactly one other target - already targeted objects are not counted
if (forceChange && possibleTargets != null && possibleTargets.size() > 1) {
// controller of spell must be used (e.g. TargetOpponent)
int iteration = 0;
do {
if (iteration > 0 && !game.isSimulation()) {
game.informPlayer(targetController, "You may only select exactly one target that must be different from the origin target!");
}
iteration++;
newTarget.clearChosen();
newTarget.chooseTarget(outcome, getControllerId(), ability, game);
// workaround to stop infinite AI choose (remove after chooseTarget can be called with extra filter to disable some ids)
if (iteration > 10) {
break;
}
} while (targetController.canRespond() && (targetId.equals(newTarget.getFirstTarget()) || newTarget.getTargets().size() != 1));
// choose a new target
} else {
// build a target definition with exactly one possible target to select that replaces old target
Target tempTarget = target.copy();
if (newTargetFilterPredicate != null) {
tempTarget.getFilter().add(newTargetFilterPredicate);
// If adding a predicate, there will only be one choice and therefore target can be automatic
tempTarget.setRandom(true);
}
tempTarget.setEventReporting(false);
if (target instanceof TargetAmount) {
((TargetAmount) tempTarget).setAmountDefinition(StaticValue.get(target.getTargetAmount(targetId)));
}
tempTarget.setMinNumberOfTargets(1);
tempTarget.setMaxNumberOfTargets(1);
if (!targetController.getId().equals(getControllerId())) {
tempTarget.setTargetController(targetController.getId());
tempTarget.setAbilityController(getControllerId());
}
boolean again;
do {
again = false;
tempTarget.clearChosen();
if (!tempTarget.chooseTarget(outcome, getControllerId(), ability, game)) {
if (targetController.chooseUse(Outcome.Benefit, "No target object selected. Reset to original target?", ability, game)) {
// use previous target no target was selected
newTarget.addTarget(targetId, target.getTargetAmount(targetId), ability, game, true);
} else {
again = true;
}
} else // if possible add the alternate Target - it may not be included in the old definition nor in the already selected targets of the new definition
{
if (newTarget.getTargets().contains(tempTarget.getFirstTarget()) || target.getTargets().contains(tempTarget.getFirstTarget())) {
if (targetController.isHuman()) {
if (targetController.chooseUse(Outcome.Benefit, "This target was already selected from origin spell. Reset to original target?", ability, game)) {
// use previous target no target was selected
newTarget.addTarget(targetId, target.getTargetAmount(targetId), ability, game, true);
} else {
again = true;
}
} else {
newTarget.addTarget(targetId, target.getTargetAmount(targetId), ability, game, true);
}
} else if (!target.canTarget(getControllerId(), tempTarget.getFirstTarget(), ability, game)) {
if (targetController.isHuman()) {
game.informPlayer(targetController, "This target is not valid!");
again = true;
} else {
// keep the old
newTarget.addTarget(targetId, target.getTargetAmount(targetId), ability, game, true);
}
} else {
// valid target was selected, add it to the new target definition
newTarget.addTarget(tempTarget.getFirstTarget(), target.getTargetAmount(targetId), ability, game, true);
}
}
} while (again && targetController.canRespond());
}
} else // keep the target
{
newTarget.addTarget(targetId, target.getTargetAmount(targetId), ability, game, true);
}
}
return newTarget;
}
Aggregations