use of mage.constants.Outcome in project mage by magefree.
the class TokenImpl method putOntoBattlefieldHelper.
private static void putOntoBattlefieldHelper(CreateTokenEvent event, Game game, Ability source, boolean tapped, boolean attacking, UUID attackedPlayer, boolean created) {
Player controller = game.getPlayer(event.getPlayerId());
if (controller == null) {
return;
}
for (Map.Entry<Token, Integer> entry : event.getTokens().entrySet()) {
Token token = entry.getKey();
int amount = entry.getValue();
String setCode = token instanceof TokenImpl ? ((TokenImpl) token).getSetCode(game, event.getSourceId()) : null;
List<Permanent> needTokens = new ArrayList<>();
List<Permanent> allowedTokens = new ArrayList<>();
// prepare tokens to enter
for (int i = 0; i < amount; i++) {
// use event.getPlayerId() as controller cause it can be replaced by replacement effect
PermanentToken newPermanent = new PermanentToken(token, event.getPlayerId(), setCode, game);
game.getState().addCard(newPermanent);
needTokens.add(newPermanent);
game.getPermanentsEntering().put(newPermanent.getId(), newPermanent);
newPermanent.setTapped(tapped);
ZoneChangeEvent emptyEvent = new ZoneChangeEvent(newPermanent, newPermanent.getControllerId(), Zone.OUTSIDE, Zone.BATTLEFIELD);
// tokens zcc must simulate card's zcc too keep copied card/spell settings
// (example: etb's kicker ability of copied creature spell, see tests with Deathforge Shaman)
newPermanent.updateZoneChangeCounter(game, emptyEvent);
}
// check ETB effects
game.setScopeRelevant(true);
for (Permanent permanent : needTokens) {
if (permanent.entersBattlefield(source, game, Zone.OUTSIDE, true)) {
allowedTokens.add(permanent);
} else {
game.getPermanentsEntering().remove(permanent.getId());
}
}
game.setScopeRelevant(false);
// put allowed tokens to play
int createOrder = game.getState().getNextPermanentOrderNumber();
for (Permanent permanent : allowedTokens) {
game.addPermanent(permanent, createOrder);
permanent.setZone(Zone.BATTLEFIELD, game);
game.getPermanentsEntering().remove(permanent.getId());
// keep tokens ids
if (token instanceof TokenImpl) {
((TokenImpl) token).lastAddedTokenIds.add(permanent.getId());
((TokenImpl) token).lastAddedTokenId = permanent.getId();
}
// created token events
ZoneChangeEvent zccEvent = new ZoneChangeEvent(permanent, permanent.getControllerId(), Zone.OUTSIDE, Zone.BATTLEFIELD);
game.addSimultaneousEvent(zccEvent);
if (permanent instanceof PermanentToken && created) {
game.addSimultaneousEvent(new CreatedTokenEvent(source, (PermanentToken) permanent));
}
// code refactored from CopyPermanentEffect
if (permanent.getSubtype().contains(SubType.AURA)) {
Outcome auraOutcome = Outcome.BoostCreature;
Target auraTarget = null;
// attach - search effect in spell ability (example: cast Utopia Sprawl, cast Estrid's Invocation on it)
for (Ability ability : permanent.getAbilities()) {
if (!(ability instanceof SpellAbility)) {
continue;
}
auraOutcome = ability.getEffects().getOutcome(ability);
for (Effect effect : ability.getEffects()) {
if (!(effect instanceof AttachEffect)) {
continue;
}
if (permanent.getSpellAbility().getTargets().size() > 0) {
auraTarget = permanent.getSpellAbility().getTargets().get(0);
}
}
}
// enchant - search in all abilities (example: cast Estrid's Invocation on enchanted creature by Estrid, the Masked second ability, cast Estrid's Invocation on it)
if (auraTarget == null) {
for (Ability ability : permanent.getAbilities()) {
if (!(ability instanceof EnchantAbility)) {
continue;
}
auraOutcome = ability.getEffects().getOutcome(ability);
if (ability.getTargets().size() > 0) {
// Animate Dead don't have targets
auraTarget = ability.getTargets().get(0);
}
}
}
// if this is a copy of a copy, the copy's target has been copied and needs to be cleared
if (auraTarget == null) {
break;
}
// clear selected target
if (auraTarget.getFirstTarget() != null) {
auraTarget.remove(auraTarget.getFirstTarget());
}
// select new target
auraTarget.setNotTarget(true);
if (!controller.choose(auraOutcome, auraTarget, source.getSourceId(), game)) {
break;
}
UUID targetId = auraTarget.getFirstTarget();
Permanent targetPermanent = game.getPermanent(targetId);
Player targetPlayer = game.getPlayer(targetId);
if (targetPermanent != null) {
targetPermanent.addAttachment(permanent.getId(), source, game);
} else if (targetPlayer != null) {
targetPlayer.addAttachment(permanent.getId(), source, game);
}
}
// must attack
if (attacking && game.getCombat() != null && game.getActivePlayerId().equals(permanent.getControllerId())) {
game.getCombat().addAttackingCreature(permanent.getId(), game, attackedPlayer);
}
// game logs
if (created) {
game.informPlayers(controller.getLogName() + " creates a " + permanent.getLogName() + " token");
} else {
game.informPlayers(permanent.getLogName() + " enters the battlefield as a token under " + controller.getLogName() + "'s control'");
}
}
}
// Needed to do it here without LKIReset i.e. do get SwordOfTheMeekTest running correctly.
game.getState().applyEffects(game);
}
use of mage.constants.Outcome in project mage by magefree.
the class GlobalRuinDestroyLandEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Set<UUID> lands = new HashSet<>();
for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
Player player = game.getPlayer(playerId);
if (player != null) {
for (SubType landName : Arrays.stream(SubType.values()).filter(p -> p.getSubTypeSet() == SubTypeSet.BasicLandType).collect(Collectors.toSet())) {
FilterControlledLandPermanent filter = new FilterControlledLandPermanent(landName + " you control");
filter.add(landName.getPredicate());
Target target = new TargetControlledPermanent(1, 1, filter, true);
if (target.canChoose(source.getSourceId(), player.getId(), game)) {
player.chooseTarget(outcome, target, source, game);
lands.add(target.getFirstTarget());
}
}
}
}
for (Permanent permanent : game.getBattlefield().getAllActivePermanents(StaticFilters.FILTER_LAND, game)) {
if (!lands.contains(permanent.getId())) {
permanent.sacrifice(source, game);
}
}
return true;
}
use of mage.constants.Outcome in project mage by magefree.
the class HydradoodleEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Permanent permanent = game.getPermanentEntering(source.getSourceId());
Player controller = game.getPlayer(source.getControllerId());
if (permanent != null && controller != null) {
SpellAbility spellAbility = (SpellAbility) getValue(EntersBattlefieldEffect.SOURCE_CAST_SPELL_ABILITY);
if (spellAbility != null && spellAbility.getSourceId().equals(source.getSourceId()) && permanent.getZoneChangeCounter(game) == spellAbility.getSourceObjectZoneChangeCounter()) {
int amount = spellAbility.getManaCostsToPay().getX();
if (amount > 0) {
int total = controller.rollDice(outcome, source, game, 6, amount, 0).stream().mapToInt(x -> x).sum();
permanent.addCounters(CounterType.P1P1.createInstance(total), source.getControllerId(), source, game);
}
}
return true;
}
return false;
}
use of mage.constants.Outcome in project mage by magefree.
the class RagingRiverEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
List<Permanent> left = new ArrayList<>();
List<Permanent> right = new ArrayList<>();
for (UUID defenderId : game.getCombat().getPlayerDefenders(game)) {
Player defender = game.getPlayer(defenderId);
if (defender != null) {
List<Permanent> leftLog = new ArrayList<>();
List<Permanent> rightLog = new ArrayList<>();
FilterControlledCreaturePermanent filterBlockers = new FilterControlledCreaturePermanent("creatures without flying you control to assign to the \"left\" pile (creatures not chosen will be assigned to the \"right\" pile)");
filterBlockers.add(Predicates.not(new AbilityPredicate(FlyingAbility.class)));
Target target = new TargetControlledCreaturePermanent(0, Integer.MAX_VALUE, filterBlockers, true);
if (target.canChoose(source.getSourceId(), defenderId, game)) {
if (defender.chooseTarget(Outcome.Neutral, target, source, game)) {
for (Permanent permanent : game.getBattlefield().getAllActivePermanents(new FilterCreaturePermanent(), defenderId, game)) {
if (target.getTargets().contains(permanent.getId())) {
left.add(permanent);
leftLog.add(permanent);
} else if (filterBlockers.match(permanent, source.getSourceId(), defenderId, game)) {
right.add(permanent);
rightLog.add(permanent);
}
}
}
// it could be nice to invoke some graphic indicator of which creature is Left or Right in this spot
StringBuilder sb = new StringBuilder("Left pile of ").append(defender.getLogName()).append(": ");
sb.append(leftLog.stream().map(MageObject::getLogName).collect(Collectors.joining(", ")));
game.informPlayers(sb.toString());
sb = new StringBuilder("Right pile of ").append(defender.getLogName()).append(": ");
sb.append(rightLog.stream().map(MageObject::getLogName).collect(Collectors.joining(", ")));
game.informPlayers(sb.toString());
}
}
}
for (UUID attackers : game.getCombat().getAttackers()) {
Permanent attacker = game.getPermanent(attackers);
if (attacker != null && Objects.equals(attacker.getControllerId(), controller.getId())) {
CombatGroup combatGroup = game.getCombat().findGroup(attacker.getId());
if (combatGroup != null) {
FilterCreaturePermanent filter = new FilterCreaturePermanent();
Player defender = game.getPlayer(combatGroup.getDefendingPlayerId());
if (defender != null) {
if (left.isEmpty() && right.isEmpty()) {
// shortcut in case of no labeled blockers available
filter.add(Predicates.not(new AbilityPredicate(FlyingAbility.class)));
} else {
List<Permanent> leftLog = left.stream().filter(permanent -> permanent.getControllerId() != null).filter(permanent -> permanent.isControlledBy(defender.getId())).collect(Collectors.toList());
List<Permanent> rightLog = right.stream().filter(permanent -> permanent.getControllerId() != null).filter(permanent -> permanent.isControlledBy(defender.getId())).collect(Collectors.toList());
if (controller.choosePile(outcome, attacker.getName() + ": attacking " + defender.getName(), leftLog, rightLog, game)) {
filter.add(Predicates.not(Predicates.or(new AbilityPredicate(FlyingAbility.class), new PermanentInListPredicate(left))));
game.informPlayers(attacker.getLogName() + ": attacks left (" + defender.getLogName() + ")");
} else {
filter.add(Predicates.not(Predicates.or(new AbilityPredicate(FlyingAbility.class), new PermanentInListPredicate(right))));
game.informPlayers(attacker.getLogName() + ": attacks right (" + defender.getLogName() + ")");
}
}
RestrictionEffect effect = new CantBeBlockedByAllTargetEffect(filter, Duration.EndOfCombat);
effect.setTargetPointer(new FixedTarget(attacker.getId(), game));
game.addEffect(effect, source);
}
}
}
}
return true;
}
return false;
}
use of mage.constants.Outcome in project mage by magefree.
the class SlipperyBogbonderEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player player = game.getPlayer(source.getControllerId());
Permanent creature = game.getPermanent(source.getFirstTarget());
FilterPermanent filterPermanent = filter.copy();
filterPermanent.add(Predicates.not(new PermanentIdPredicate(source.getFirstTarget())));
if (player == null || creature == null || game.getBattlefield().count(filterPermanent, source.getSourceId(), source.getControllerId(), game) < 1) {
return false;
}
TargetPermanent target = new TargetPermanent(0, Integer.MAX_VALUE, filterPermanent, true);
player.choose(outcome, target, source.getSourceId(), game);
List<Permanent> permanents = target.getTargets().stream().filter(uuid -> !creature.getId().equals(uuid)).map(game::getPermanent).filter(Objects::nonNull).collect(Collectors.toList());
if (permanents.isEmpty()) {
return false;
}
Map<UUID, Map<String, Integer>> counterMap = new HashMap<>();
for (Permanent permanent : permanents) {
permanent.getCounters(game).entrySet().stream().forEach(entry -> {
int num = player.getAmount(0, entry.getValue().getCount(), "Choose how many " + entry.getKey() + " counters to remove from " + permanent.getLogName(), game);
counterMap.computeIfAbsent(permanent.getId(), x -> new HashMap<>()).put(entry.getKey(), num);
});
}
for (Permanent permanent : permanents) {
counterMap.get(permanent.getId()).entrySet().stream().filter(entry -> entry.getValue() > 0).map(entry -> CounterType.findByName(entry.getKey()).createInstance(entry.getValue())).filter(counter -> creature.addCounters(counter, source.getControllerId(), source, game)).forEach(counter -> permanent.removeCounters(counter, source, game));
}
return true;
}
Aggregations