* Fixed some corner cases for Worl Enchantment State-Based actions (704.5k).

This commit is contained in:
LevelX2 2020-07-18 12:23:19 +02:00
parent fdae577cef
commit a2ae232b43
7 changed files with 161 additions and 54 deletions

View file

@ -1,5 +1,6 @@
package mage.abilities.effects;
import java.util.UUID;
import mage.MageObject;
import mage.abilities.Ability;
import mage.abilities.SpellAbility;
@ -17,8 +18,6 @@ import mage.players.Player;
import mage.target.Target;
import mage.target.common.TargetCardInGraveyard;
import java.util.UUID;
/**
* Cards with the Aura subtype don't change the zone they are in, if there is no
* valid target on the battlefield. Also, when entering the battlefield and it
@ -163,7 +162,7 @@ public class AuraReplacementEffect extends ReplacementEffectImpl {
PermanentCard permanent = new PermanentCard(card, (controllingPlayer == null ? card.getOwnerId() : controllingPlayer.getId()), game);
ZoneChangeEvent zoneChangeEvent = new ZoneChangeEvent(permanent, controllerId, fromZone, Zone.BATTLEFIELD);
permanent.updateZoneChangeCounter(game, zoneChangeEvent);
game.getBattlefield().addPermanent(permanent);
game.addPermanent(permanent, 0);
card.setZone(Zone.BATTLEFIELD, game);
if (permanent.entersBattlefield(event.getSourceId(), game, fromZone, true)) {
if (targetCard != null) {
@ -196,9 +195,9 @@ public class AuraReplacementEffect extends ReplacementEffectImpl {
return card != null && (card.isEnchantment() && card.hasSubtype(SubType.AURA, game)
|| // in case of transformable enchantments
(game.getState().getValue(TransformAbility.VALUE_KEY_ENTER_TRANSFORMED + card.getId()) != null
&& card.getSecondCardFace() != null
&& card.getSecondCardFace().isEnchantment()
&& card.getSecondCardFace().hasSubtype(SubType.AURA, game)));
&& card.getSecondCardFace() != null
&& card.getSecondCardFace().isEnchantment()
&& card.getSecondCardFace().hasSubtype(SubType.AURA, game)));
}
return false;
}

View file

@ -373,7 +373,16 @@ public interface Game extends MageItem, Serializable {
void addCommander(Commander commander);
void addPermanent(Permanent permanent);
/**
* Adds a permanent to the battlefield
*
* @param permanent
* @param createOrder upcounting number from state about the create order of
* all permanents. Can equal for multiple permanents, if
* they go to battlefield at the same time. If the value
* is set to 0, a next number will be set automatically.
*/
void addPermanent(Permanent permanent, int createOrder);
// priority method
void sendPlayerAction(PlayerAction playerAction, UUID playerId, Object data);

View file

@ -187,6 +187,7 @@ public abstract class GameImpl implements Game, Serializable {
this.startLife = game.startLife;
this.enterWithCounters.putAll(game.enterWithCounters);
this.startingSize = game.startingSize;
this.gameStopped = game.gameStopped;
}
@Override
@ -1419,7 +1420,7 @@ public abstract class GameImpl implements Game, Serializable {
if (spell != null) {
if (spell.getCommandedBy() != null) {
UUID commandedBy = spell.getCommandedBy();
UUID spellControllerId = null;
UUID spellControllerId;
if (commandedBy.equals(spell.getControllerId())) {
spellControllerId = spell.getSpellAbility().getFirstTarget(); // i.e. resolved spell is Word of Command
} else {
@ -1605,9 +1606,12 @@ public abstract class GameImpl implements Game, Serializable {
}
@Override
public void addPermanent(Permanent permanent) {
public void addPermanent(Permanent permanent, int createOrder) {
if (createOrder == 0) {
createOrder = getState().getNextPermanentOrderNumber();
}
permanent.setCreateOrder(createOrder);
getBattlefield().addPermanent(permanent);
permanent.setCreateOrder(getState().getNextPermanentOrderNumber());
}
@Override
@ -2254,30 +2258,38 @@ public abstract class GameImpl implements Game, Serializable {
}
}
}
//704.5m - World Enchantments
//704.5k - World Enchantments
if (worldEnchantment.size() > 1) {
int newestCard = -1;
Set<UUID> controllerIdOfNewest = new HashSet<>();
Permanent newestPermanent = null;
for (Permanent permanent : worldEnchantment) {
if (newestCard == -1) {
newestCard = permanent.getCreateOrder();
newestPermanent = permanent;
controllerIdOfNewest.clear();
controllerIdOfNewest.add(permanent.getControllerId());
} else if (newestCard < permanent.getCreateOrder()) {
newestCard = permanent.getCreateOrder();
newestPermanent = permanent;
controllerIdOfNewest.clear();
controllerIdOfNewest.add(permanent.getControllerId());
} else if (newestCard == permanent.getCreateOrder()) {
// In the event of a tie for the shortest amount of time, all are put into their owners graveyards. This is called the world rule.
newestPermanent = null;
controllerIdOfNewest.add(permanent.getControllerId());
}
}
for (UUID controllerId : controllerIdOfNewest) {
PlayerList newestPermanentControllerRange = state.getPlayersInRange(controllerId, this);
PlayerList newestPermanentControllerRange = state.getPlayersInRange(newestPermanent.getControllerId(), this);
// 801.12 The "world rule" applies to a permanent only if other world permanents are within its controller's range of influence.
for (Permanent permanent : worldEnchantment) {
if (newestPermanentControllerRange.contains(permanent.getControllerId())
&& !Objects.equals(newestPermanent, permanent)) {
movePermanentToGraveyardWithInfo(permanent);
somethingHappened = true;
// 801.12 The "world rule" applies to a permanent only if other world permanents are within its controller's range of influence.
for (Permanent permanent : worldEnchantment) {
if (newestPermanentControllerRange.contains(permanent.getControllerId())
&& !Objects.equals(newestPermanent, permanent)) {
movePermanentToGraveyardWithInfo(permanent);
somethingHappened = true;
}
}
}
}
@ -2788,7 +2800,7 @@ public abstract class GameImpl implements Game, Serializable {
}
if (amountToPrevent != Integer.MAX_VALUE) {
// set remaining amount
result.setRemainingAmount(amountToPrevent -= result.getPreventedDamage());
result.setRemainingAmount(amountToPrevent - result.getPreventedDamage());
}
MageObject damageSource = game.getObject(damageEvent.getSourceId());
MageObject preventionSource = game.getObject(source.getSourceId());
@ -3058,7 +3070,7 @@ public abstract class GameImpl implements Game, Serializable {
PermanentCard newPermanent = new PermanentCard(permanentCard.getCard(), ownerId, this);
getPermanentsEntering().put(newPermanent.getId(), newPermanent);
newPermanent.entersBattlefield(newPermanent.getId(), this, Zone.OUTSIDE, false);
getBattlefield().addPermanent(newPermanent);
addPermanent(newPermanent, getState().getNextPermanentOrderNumber());
getPermanentsEntering().remove(newPermanent.getId());
newPermanent.removeSummoningSickness();
if (permanentCard.isTapped()) {

View file

@ -1,5 +1,6 @@
package mage.game;
import java.util.*;
import mage.cards.Card;
import mage.cards.Cards;
import mage.cards.CardsImpl;
@ -18,8 +19,6 @@ import mage.game.stack.Spell;
import mage.players.Player;
import mage.target.TargetCard;
import java.util.*;
/**
* Created by samuelsandeen on 9/6/16.
*/
@ -27,7 +26,7 @@ public final class ZonesHandler {
public static boolean cast(ZoneChangeInfo info, Game game) {
if (maybeRemoveFromSourceZone(info, game)) {
placeInDestinationZone(info, game);
placeInDestinationZone(info, game, 0);
// create a group zone change event if a card is moved to stack for casting (it's always only one card, but some effects check for group events (one or more xxx))
Set<Card> cards = new HashSet<>();
Set<PermanentToken> tokens = new HashSet<>();
@ -53,7 +52,7 @@ public final class ZonesHandler {
public static List<ZoneChangeInfo> moveCards(List<ZoneChangeInfo> zoneChangeInfos, Game game) {
// Handle Unmelded Meld Cards
for (ListIterator<ZoneChangeInfo> itr = zoneChangeInfos.listIterator(); itr.hasNext(); ) {
for (ListIterator<ZoneChangeInfo> itr = zoneChangeInfos.listIterator(); itr.hasNext();) {
ZoneChangeInfo info = itr.next();
MeldCard card = game.getMeldCard(info.event.getTargetId());
// Copies should be handled as normal cards.
@ -67,8 +66,13 @@ public final class ZonesHandler {
}
}
zoneChangeInfos.removeIf(zoneChangeInfo -> !maybeRemoveFromSourceZone(zoneChangeInfo, game));
int createOrder = 0;
for (ZoneChangeInfo zoneChangeInfo : zoneChangeInfos) {
placeInDestinationZone(zoneChangeInfo, game);
if (createOrder == 0 && Zone.BATTLEFIELD.equals(zoneChangeInfo.event.getToZone())) {
// All permanents go to battlefield at the same time (=create order)
createOrder = game.getState().getNextPermanentOrderNumber();
}
placeInDestinationZone(zoneChangeInfo, game, createOrder);
if (game.getPhase() != null) { // moving cards to zones before game started does not need events
game.addSimultaneousEvent(zoneChangeInfo.event);
}
@ -76,14 +80,14 @@ public final class ZonesHandler {
return zoneChangeInfos;
}
private static void placeInDestinationZone(ZoneChangeInfo info, Game game) {
private static void placeInDestinationZone(ZoneChangeInfo info, Game game, int createOrder) {
// Handle unmelded cards
if (info instanceof ZoneChangeInfo.Unmelded) {
ZoneChangeInfo.Unmelded unmelded = (ZoneChangeInfo.Unmelded) info;
Zone toZone = null;
for (ZoneChangeInfo subInfo : unmelded.subInfo) {
toZone = subInfo.event.getToZone();
placeInDestinationZone(subInfo, game);
placeInDestinationZone(subInfo, game, createOrder);
}
// We arbitrarily prefer the bottom half card. This should never be relevant.
if (toZone != null) {
@ -161,7 +165,7 @@ public final class ZonesHandler {
break;
case BATTLEFIELD:
Permanent permanent = event.getTarget();
game.addPermanent(permanent);
game.addPermanent(permanent, createOrder);
game.getPermanentsEntering().remove(permanent.getId());
break;
default:
@ -197,7 +201,7 @@ public final class ZonesHandler {
if (info instanceof ZoneChangeInfo.Unmelded) {
ZoneChangeInfo.Unmelded unmelded = (ZoneChangeInfo.Unmelded) info;
MeldCard meld = game.getMeldCard(info.event.getTargetId());
for (Iterator<ZoneChangeInfo> itr = unmelded.subInfo.iterator(); itr.hasNext(); ) {
for (Iterator<ZoneChangeInfo> itr = unmelded.subInfo.iterator(); itr.hasNext();) {
ZoneChangeInfo subInfo = itr.next();
if (!maybeRemoveFromSourceZone(subInfo, game)) {
itr.remove();

View file

@ -1,5 +1,9 @@
package mage.game.permanent.token;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import mage.MageObject;
import mage.MageObjectImpl;
import mage.abilities.Ability;
@ -14,11 +18,6 @@ import mage.game.permanent.PermanentToken;
import mage.players.Player;
import mage.util.RandomUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
public abstract class TokenImpl extends MageObjectImpl implements Token {
protected String description;
@ -202,8 +201,9 @@ public abstract class TokenImpl extends MageObjectImpl implements Token {
}
}
game.setScopeRelevant(false);
int createOrder = game.getState().getNextPermanentOrderNumber();
for (Permanent permanent : permanentsEntered) {
game.addPermanent(permanent);
game.addPermanent(permanent, createOrder);
permanent.setZone(Zone.BATTLEFIELD, game);
game.getPermanentsEntering().remove(permanent.getId());
@ -242,8 +242,8 @@ public abstract class TokenImpl extends MageObjectImpl implements Token {
}
/**
* Set token index to search in card-pictures-tok.txt (if set have multiple tokens with same name)
* Default is 1
* Set token index to search in card-pictures-tok.txt (if set have multiple
* tokens with same name) Default is 1
*/
@Override
public void setTokenType(int tokenType) {