* Fixed possible game exception with empty library and commander movement (fixes #6952).

This commit is contained in:
LevelX2 2020-08-10 17:25:48 +02:00
parent b16d30b79b
commit 12c50420bf
2 changed files with 84 additions and 43 deletions

View file

@ -1,6 +1,8 @@
package org.mage.test.commander.duel;
import java.util.ArrayList;
import mage.cards.Card;
import mage.constants.PhaseStep;
import mage.constants.Zone;
import mage.game.permanent.Permanent;
@ -93,4 +95,41 @@ public class CastCommanderTest extends CardTestCommanderDuelBase {
assertGraveyardCount(playerA, "Coiled Tinviper", 1);
assertTappedCount("Forest", false, 3);
}
// https://github.com/magefree/mage/issues/6952
@Test
public void testCastCommanderAndUnexpectedlyAbsent() {
setStrictChooseMode(true);
playerA.getLibrary().clear();
addCard(Zone.BATTLEFIELD, playerA, "Swamp", 2);
addCard(Zone.BATTLEFIELD, playerA, "Plains", 5);
// Put target nonland permanent into its owner's library just beneath the top X cards of that library.
addCard(Zone.HAND, playerA, "Unexpectedly Absent"); // Instant {X}{W}{W}
castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Ob Nixilis of the Black Oath"); // {3}{B}{B}
castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Unexpectedly Absent", "Ob Nixilis of the Black Oath");
setChoice(playerA, "X=0");
setChoice(playerA, "Yes"); // Move Commander to command zone instead library?
setStopAt(1, PhaseStep.BEGIN_COMBAT);
execute();
assertAllCommandsUsed();
assertPermanentCount(playerA, "Ob Nixilis of the Black Oath", 0);
assertLibraryCount(playerA, "Ob Nixilis of the Black Oath", 0);
assertCommandZoneCount(playerA, "Ob Nixilis of the Black Oath", 1);
assertLibraryCount(playerA, 0);
assertGraveyardCount(playerA, "Unexpectedly Absent", 1);
assertLife(playerA, 40);
assertLife(playerB, 40);
}
}

View file

@ -614,9 +614,9 @@ public abstract class PlayerImpl implements Player, Serializable {
&& this.hasOpponent(sourceControllerId, game)
&& game.getContinuousEffects().asThough(this.getId(), AsThoughEffectType.HEXPROOF, null, sourceControllerId, game) == null
&& abilities.stream()
.filter(HexproofBaseAbility.class::isInstance)
.map(HexproofBaseAbility.class::cast)
.anyMatch(ability -> ability.checkObject(source, game))) {
.filter(HexproofBaseAbility.class::isInstance)
.map(HexproofBaseAbility.class::cast)
.anyMatch(ability -> ability.checkObject(source, game))) {
return false;
}
@ -656,7 +656,7 @@ public abstract class PlayerImpl implements Player, Serializable {
game.informPlayers(getLogName() + " discards down to "
+ this.maxHandSize
+ (this.maxHandSize == 1
? " hand card" : " hand cards"));
? " hand card" : " hand cards"));
}
discard(hand.size() - this.maxHandSize, false, null, game);
}
@ -805,7 +805,7 @@ public abstract class PlayerImpl implements Player, Serializable {
}
GameEvent gameEvent = GameEvent.getEvent(GameEvent.EventType.DISCARD_CARD,
card.getId(), source == null
? null : source.getSourceId(), playerId);
? null : source.getSourceId(), playerId);
gameEvent.setFlag(source != null); // event from effect or from cost (source == null)
if (game.replaceEvent(gameEvent, source)) {
return false;
@ -996,14 +996,16 @@ public abstract class PlayerImpl implements Player, Serializable {
} else {
if (card.moveToZone(Zone.LIBRARY, source.getSourceId(), game, true)
&& !(card instanceof PermanentToken) && !card.isCopy()) {
card = getLibrary().removeFromTop(game);
getLibrary().putCardToTopXPos(card, xFromTheTop, game);
game.informPlayers(card.getLogName()
+ " is put into "
+ getLogName()
+ "'s library "
+ CardUtil.numberToOrdinalText(xFromTheTop)
+ " from the top");
Card cardInLib = getLibrary().getFromTop(game);
if (cardInLib != null && cardInLib.getId().equals(card.getId())) { // check needed because e.g. commander can go to command zone
getLibrary().putCardToTopXPos(cardInLib, xFromTheTop, game);
game.informPlayers(cardInLib.getLogName()
+ " is put into "
+ getLogName()
+ "'s library "
+ CardUtil.numberToOrdinalText(xFromTheTop)
+ " from the top");
}
} else {
return false;
}
@ -1854,9 +1856,9 @@ public abstract class PlayerImpl implements Player, Serializable {
}
private List<Permanent> getPermanentsThatCanBeUntapped(Game game,
List<Permanent> canBeUntapped,
RestrictionUntapNotMoreThanEffect handledEffect,
Map<Entry<RestrictionUntapNotMoreThanEffect, Set<Ability>>, Integer> notMoreThanEffectsUsage) {
List<Permanent> canBeUntapped,
RestrictionUntapNotMoreThanEffect handledEffect,
Map<Entry<RestrictionUntapNotMoreThanEffect, Set<Ability>>, Integer> notMoreThanEffectsUsage) {
List<Permanent> leftForUntap = new ArrayList<>();
// select permanents that can still be untapped
for (Permanent permanent : canBeUntapped) {
@ -2569,7 +2571,7 @@ public abstract class PlayerImpl implements Player, Serializable {
@Override
public boolean searchLibrary(TargetCardInLibrary target, Ability source, Game game, UUID targetPlayerId,
boolean triggerEvents) {
boolean triggerEvents) {
//20091005 - 701.14c
Library searchedLibrary = null;
String searchInfo = null;
@ -2719,7 +2721,7 @@ public abstract class PlayerImpl implements Player, Serializable {
* @param winnable
* @param appliedEffects
* @return if winnable, true if player won the toss, if not winnable, true
* for heads and false for tails
* for heads and false for tails
*/
@Override
public boolean flipCoin(Ability source, Game game, boolean winnable, List<UUID> appliedEffects) {
@ -2813,11 +2815,11 @@ public abstract class PlayerImpl implements Player, Serializable {
* @param numberPlanarSides The number of chaos sides the planar die
* currently has (normally 1)
* @return the outcome that the player rolled. Either ChaosRoll, PlanarRoll
* or NilRoll
* or NilRoll
*/
@Override
public PlanarDieRoll rollPlanarDie(Game game, List<UUID> appliedEffects, int numberChaosSides,
int numberPlanarSides) {
int numberPlanarSides) {
int result = RandomUtil.nextInt(9) + 1;
PlanarDieRoll roll = PlanarDieRoll.NIL_ROLL;
if (numberChaosSides + numberPlanarSides > 9) {
@ -2894,7 +2896,7 @@ public abstract class PlayerImpl implements Player, Serializable {
boolean withCost = false;
Abilities<ActivatedManaAbilityImpl> manaAbilities
= permanent.getAbilities().getAvailableActivatedManaAbilities(Zone.BATTLEFIELD, game);
for (Iterator<ActivatedManaAbilityImpl> it = manaAbilities.iterator(); it.hasNext(); ) {
for (Iterator<ActivatedManaAbilityImpl> it = manaAbilities.iterator(); it.hasNext();) {
ActivatedManaAbilityImpl ability = it.next();
if (canUse == null) {
canUse = permanent.canUseActivatedAbilities(game);
@ -2935,7 +2937,7 @@ public abstract class PlayerImpl implements Player, Serializable {
boolean anAbilityWasUsed = true;
while (anAbilityWasUsed && !sourceWithCosts.isEmpty()) {
anAbilityWasUsed = false;
for (Iterator<Abilities<ActivatedManaAbilityImpl>> iterator = sourceWithCosts.iterator(); iterator.hasNext(); ) {
for (Iterator<Abilities<ActivatedManaAbilityImpl>> iterator = sourceWithCosts.iterator(); iterator.hasNext();) {
Abilities<ActivatedManaAbilityImpl> manaAbilities = iterator.next();
boolean used = availableMana.addManaWithCost(manaAbilities, game);
if (used) {
@ -3619,7 +3621,7 @@ public abstract class PlayerImpl implements Player, Serializable {
*
* @param game
* @return A Set of cardIds that are playable and amount of playable
* abilities
* abilities
*/
@Override
public Map<UUID, Integer> getPlayableObjects(Game game, Zone zone) {
@ -3827,7 +3829,7 @@ public abstract class PlayerImpl implements Player, Serializable {
@Override
public boolean canPaySacrificeCost(Permanent permanent, UUID sourceId,
UUID controllerId, Game game
UUID controllerId, Game game
) {
return sacrificeCostFilter == null || !sacrificeCostFilter.match(permanent, sourceId, controllerId, game);
}
@ -3980,8 +3982,8 @@ public abstract class PlayerImpl implements Player, Serializable {
@Override
public boolean moveCards(Card card, Zone toZone,
Ability source, Game game,
boolean tapped, boolean faceDown, boolean byOwner, List<UUID> appliedEffects
Ability source, Game game,
boolean tapped, boolean faceDown, boolean byOwner, List<UUID> appliedEffects
) {
Set<Card> cardList = new HashSet<>();
if (card != null) {
@ -3992,22 +3994,22 @@ public abstract class PlayerImpl implements Player, Serializable {
@Override
public boolean moveCards(Cards cards, Zone toZone,
Ability source, Game game
Ability source, Game game
) {
return moveCards(cards.getCards(game), toZone, source, game);
}
@Override
public boolean moveCards(Set<Card> cards, Zone toZone,
Ability source, Game game
Ability source, Game game
) {
return moveCards(cards, toZone, source, game, false, false, false, null);
}
@Override
public boolean moveCards(Set<Card> cards, Zone toZone,
Ability source, Game game,
boolean tapped, boolean faceDown, boolean byOwner, List<UUID> appliedEffects
Ability source, Game game,
boolean tapped, boolean faceDown, boolean byOwner, List<UUID> appliedEffects
) {
if (cards.isEmpty()) {
return true;
@ -4109,8 +4111,8 @@ public abstract class PlayerImpl implements Player, Serializable {
@Override
public boolean moveCardsToExile(Card card, Ability source,
Game game, boolean withName, UUID exileId,
String exileZoneName
Game game, boolean withName, UUID exileId,
String exileZoneName
) {
Set<Card> cards = new HashSet<>();
cards.add(card);
@ -4119,8 +4121,8 @@ public abstract class PlayerImpl implements Player, Serializable {
@Override
public boolean moveCardsToExile(Set<Card> cards, Ability source,
Game game, boolean withName, UUID exileId,
String exileZoneName
Game game, boolean withName, UUID exileId,
String exileZoneName
) {
if (cards.isEmpty()) {
return true;
@ -4136,14 +4138,14 @@ public abstract class PlayerImpl implements Player, Serializable {
@Override
public boolean moveCardToHandWithInfo(Card card, UUID sourceId,
Game game
Game game
) {
return this.moveCardToHandWithInfo(card, sourceId, game, true);
}
@Override
public boolean moveCardToHandWithInfo(Card card, UUID sourceId,
Game game, boolean withName
Game game, boolean withName
) {
boolean result = false;
Zone fromZone = game.getState().getZone(card.getId());
@ -4168,7 +4170,7 @@ public abstract class PlayerImpl implements Player, Serializable {
@Override
public Set<Card> moveCardsToGraveyardWithInfo(Set<Card> allCards, Ability source,
Game game, Zone fromZone
Game game, Zone fromZone
) {
UUID sourceId = source == null ? null : source.getSourceId();
Set<Card> movedCards = new LinkedHashSet<>();
@ -4176,7 +4178,7 @@ public abstract class PlayerImpl implements Player, Serializable {
// identify cards from one owner
Cards cards = new CardsImpl();
UUID ownerId = null;
for (Iterator<Card> it = allCards.iterator(); it.hasNext(); ) {
for (Iterator<Card> it = allCards.iterator(); it.hasNext();) {
Card card = it.next();
if (cards.isEmpty()) {
ownerId = card.getOwnerId();
@ -4239,7 +4241,7 @@ public abstract class PlayerImpl implements Player, Serializable {
@Override
public boolean moveCardToGraveyardWithInfo(Card card, UUID sourceId,
Game game, Zone fromZone
Game game, Zone fromZone
) {
if (card == null) {
return false;
@ -4268,8 +4270,8 @@ public abstract class PlayerImpl implements Player, Serializable {
@Override
public boolean moveCardToLibraryWithInfo(Card card, UUID sourceId,
Game game, Zone fromZone,
boolean toTop, boolean withName
Game game, Zone fromZone,
boolean toTop, boolean withName
) {
if (card == null) {
return false;
@ -4334,7 +4336,7 @@ public abstract class PlayerImpl implements Player, Serializable {
@Override
public boolean moveCardToExileWithInfo(Card card, UUID exileId, String exileName, UUID sourceId,
Game game, Zone fromZone, boolean withName) {
Game game, Zone fromZone, boolean withName) {
if (card == null) {
return false;
}
@ -4358,7 +4360,7 @@ public abstract class PlayerImpl implements Player, Serializable {
game.informPlayers(this.getLogName() + " moves " + (withName ? card.getLogName()
+ (card.isCopy() ? " (Copy)" : "") : "a card face down") + ' '
+ (fromZone != null ? "from " + fromZone.toString().toLowerCase(Locale.ENGLISH)
+ ' ' : "") + "to the exile zone");
+ ' ' : "") + "to the exile zone");
}
}