Refactoring snow mana to allow tracking how much is spent (Ready for Review) (#7406)

* refactored mana methods to WUBRG order

* more WUBRG refactoring

* added new mana tracking object

* fixed code error

* fixed missing mana count

* fixed null pointer exception in tests

* fixed issue with equality

* more equality fixes

* some cosmetic changes to ManaTest

* added copy method to setToMana

* fixed some initialization issues

* fixed serialization issue

* [KHM] Implemented Search for Glory

* updated mana handling to track snow

* added tests for snow mana tracking

* updated implementation of setter methods

* updated paramater to use copy methods

* fixed snow mana test to ensure proper mana tapping

* replaced instances of getPayment with getUsedManaToPay

* updated tracking of snow mana

* reverted snow mana tracking removal

* finished reverting change
This commit is contained in:
Evan Kranzler 2021-01-21 18:13:51 -05:00 committed by GitHub
parent 5e4b5239d8
commit e5344b7a96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
99 changed files with 1006 additions and 720 deletions

View file

@ -113,25 +113,25 @@ public class ConditionalMana extends Mana implements Serializable {
return;
}
if (filter.isBlack()) {
black = 0;
black.clear();
}
if (filter.isBlue()) {
blue = 0;
blue.clear();
}
if (filter.isWhite()) {
white = 0;
white.clear();
}
if (filter.isGreen()) {
green = 0;
green.clear();
}
if (filter.isRed()) {
red = 0;
red.clear();
}
if (filter.isColorless()) {
colorless = 0;
colorless.clear();
}
if (filter.isGeneric()) {
generic = 0;
generic.clear();
}
}
@ -154,25 +154,25 @@ public class ConditionalMana extends Mana implements Serializable {
public void clear(ManaType manaType) {
switch (manaType) {
case BLACK:
black = 0;
black.clear();
break;
case BLUE:
blue = 0;
blue.clear();
break;
case GREEN:
green = 0;
green.clear();
break;
case RED:
red = 0;
red.clear();
break;
case WHITE:
white = 0;
white.clear();
break;
case GENERIC:
generic = 0;
generic.clear();
break;
case COLORLESS:
colorless = 0;
colorless.clear();
break;
}
}
@ -186,7 +186,7 @@ public class ConditionalMana extends Mana implements Serializable {
}
super.add(mana);
}
public String getConditionString() {
String condStr = "[";
for (Condition condition : conditions) {
@ -198,25 +198,25 @@ public class ConditionalMana extends Mana implements Serializable {
public void add(ManaType manaType, int amount) {
switch (manaType) {
case BLACK:
black += amount;
black.incrementAmount(amount, false);
break;
case BLUE:
blue += amount;
blue.incrementAmount(amount, false);
break;
case GREEN:
green += amount;
green.incrementAmount(amount, false);
break;
case RED:
red += amount;
red.incrementAmount(amount, false);
break;
case WHITE:
white += amount;
white.incrementAmount(amount, false);
break;
case COLORLESS:
colorless += amount;
colorless.incrementAmount(amount, false);
break;
case GENERIC:
generic += amount;
generic.incrementAmount(amount, false);
break;
}
}

File diff suppressed because it is too large Load diff

View file

@ -33,10 +33,10 @@ public enum AdamantCondition implements Condition {
if (coloredManaSymbol == null) {
return Arrays
.stream(ColoredManaSymbol.values())
.map(source.getManaCostsToPay().getPayment()::getColor)
.map(source.getManaCostsToPay().getUsedManaToPay()::getColor)
.anyMatch(i -> i > 2);
}
return source.getManaCostsToPay().getPayment().getColor(coloredManaSymbol) > 2;
return source.getManaCostsToPay().getUsedManaToPay().getColor(coloredManaSymbol) > 2;
}
ManaSpentToCastWatcher watcher = game.getState().getWatcher(ManaSpentToCastWatcher.class, source.getSourceId());
if (watcher == null) {

View file

@ -28,7 +28,7 @@ public class ManaWasSpentCondition implements Condition {
@Override
public boolean apply(Game game, Ability source) {
if (source.getAbilityType() == AbilityType.SPELL) {
return (source.getManaCostsToPay().getPayment().getColor(coloredManaSymbol) > 0);
return (source.getManaCostsToPay().getUsedManaToPay().getColor(coloredManaSymbol) > 0);
}
ManaSpentToCastWatcher watcher = game.getState().getWatcher(ManaSpentToCastWatcher.class, source.getSourceId());
if (watcher != null) {

View file

@ -73,6 +73,7 @@ public abstract class ManaCostImpl extends CostImpl implements ManaCost {
@Override
public void clearPaid() {
payment.clear();
usedManaToPay.clear();
super.clearPaid();
}
@ -100,33 +101,33 @@ public abstract class ManaCostImpl extends CostImpl implements ManaCost {
protected boolean assignColored(Ability ability, Game game, ManaPool pool, ColoredManaSymbol mana, Cost costToPay) {
// first check special mana
switch (mana) {
case B:
if (pool.pay(ManaType.BLACK, ability, sourceFilter, game, costToPay, usedManaToPay)) {
this.payment.increaseBlack();
case W:
if (pool.pay(ManaType.WHITE, ability, sourceFilter, game, costToPay, usedManaToPay)) {
this.payment.increaseWhite(1, pool.getLastPaymentWasSnow());
return true;
}
break;
case U:
if (pool.pay(ManaType.BLUE, ability, sourceFilter, game, costToPay, usedManaToPay)) {
this.payment.increaseBlue();
this.payment.increaseBlue(1, pool.getLastPaymentWasSnow());
return true;
}
break;
case W:
if (pool.pay(ManaType.WHITE, ability, sourceFilter, game, costToPay, usedManaToPay)) {
this.payment.increaseWhite();
return true;
}
break;
case G:
if (pool.pay(ManaType.GREEN, ability, sourceFilter, game, costToPay, usedManaToPay)) {
this.payment.increaseGreen();
case B:
if (pool.pay(ManaType.BLACK, ability, sourceFilter, game, costToPay, usedManaToPay)) {
this.payment.increaseBlack(1, pool.getLastPaymentWasSnow());
return true;
}
break;
case R:
if (pool.pay(ManaType.RED, ability, sourceFilter, game, costToPay, usedManaToPay)) {
this.payment.increaseRed();
this.payment.increaseRed(1, pool.getLastPaymentWasSnow());
return true;
}
break;
case G:
if (pool.pay(ManaType.GREEN, ability, sourceFilter, game, costToPay, usedManaToPay)) {
this.payment.increaseGreen(1, pool.getLastPaymentWasSnow());
return true;
}
break;
@ -138,7 +139,7 @@ public abstract class ManaCostImpl extends CostImpl implements ManaCost {
int conditionalCount = pool.getConditionalCount(ability, game, null, costToPay);
while (mana > payment.count() && (pool.count() > 0 || conditionalCount > 0)) {
if (pool.pay(ManaType.COLORLESS, ability, sourceFilter, game, costToPay, usedManaToPay)) {
this.payment.increaseColorless();
this.payment.increaseColorless(1, pool.getLastPaymentWasSnow());
}
break;
}
@ -151,38 +152,50 @@ public abstract class ManaCostImpl extends CostImpl implements ManaCost {
// filterMana can be null, uses for spells like "spend only black mana on X"
// {C}
if ((filterMana == null || filterMana.isColorless()) && pool.pay(ManaType.COLORLESS, ability, sourceFilter, game, costToPay, usedManaToPay)) {
this.payment.increaseColorless();
if ((filterMana == null || filterMana.isColorless()) && pool.pay(
ManaType.COLORLESS, ability, sourceFilter, game, costToPay, usedManaToPay
)) {
this.payment.increaseColorless(1, pool.getLastPaymentWasSnow());
continue;
}
// {B}
if ((filterMana == null || filterMana.isBlack()) && pool.pay(ManaType.BLACK, ability, sourceFilter, game, costToPay, usedManaToPay)) {
this.payment.increaseBlack();
if ((filterMana == null || filterMana.isBlack()) && pool.pay(
ManaType.BLACK, ability, sourceFilter, game, costToPay, usedManaToPay
)) {
this.payment.increaseBlack(1, pool.getLastPaymentWasSnow());
continue;
}
// {U}
if ((filterMana == null || filterMana.isBlue()) && pool.pay(ManaType.BLUE, ability, sourceFilter, game, costToPay, usedManaToPay)) {
this.payment.increaseBlue();
if ((filterMana == null || filterMana.isBlue()) && pool.pay(
ManaType.BLUE, ability, sourceFilter, game, costToPay, usedManaToPay
)) {
this.payment.increaseBlue(1, pool.getLastPaymentWasSnow());
continue;
}
// {W}
if ((filterMana == null || filterMana.isWhite()) && pool.pay(ManaType.WHITE, ability, sourceFilter, game, costToPay, usedManaToPay)) {
this.payment.increaseWhite();
if ((filterMana == null || filterMana.isWhite()) && pool.pay(
ManaType.WHITE, ability, sourceFilter, game, costToPay, usedManaToPay
)) {
this.payment.increaseWhite(1, pool.getLastPaymentWasSnow());
continue;
}
// {G}
if ((filterMana == null || filterMana.isGreen()) && pool.pay(ManaType.GREEN, ability, sourceFilter, game, costToPay, usedManaToPay)) {
this.payment.increaseGreen();
if ((filterMana == null || filterMana.isGreen()) && pool.pay(
ManaType.GREEN, ability, sourceFilter, game, costToPay, usedManaToPay
)) {
this.payment.increaseGreen(1, pool.getLastPaymentWasSnow());
continue;
}
// {R}
if ((filterMana == null || filterMana.isRed()) && pool.pay(ManaType.RED, ability, sourceFilter, game, costToPay, usedManaToPay)) {
this.payment.increaseRed();
if ((filterMana == null || filterMana.isRed()) && pool.pay(
ManaType.RED, ability, sourceFilter, game, costToPay, usedManaToPay
)) {
this.payment.increaseRed(1, pool.getLastPaymentWasSnow());
continue;
}

View file

@ -0,0 +1,49 @@
package mage.abilities.dynamicvalue.common;
import mage.Mana;
import mage.abilities.Ability;
import mage.abilities.dynamicvalue.DynamicValue;
import mage.abilities.effects.Effect;
import mage.constants.AbilityType;
import mage.game.Game;
import mage.watchers.common.ManaSpentToCastWatcher;
/**
* @author TheElk801
*/
public enum SnowManaSpentValue implements DynamicValue {
instance;
@Override
public int calculate(Game game, Ability sourceAbility, Effect effect) {
if (sourceAbility.getAbilityType() == AbilityType.SPELL) {
return sourceAbility.getManaCostsToPay().getUsedManaToPay().getSnow();
}
ManaSpentToCastWatcher watcher = game.getState().getWatcher(
ManaSpentToCastWatcher.class, sourceAbility.getSourceId()
);
if (watcher == null) {
return 0;
}
Mana payment = watcher.getAndResetLastPayment();
if (payment == null) {
return 0;
}
return payment.getSnow();
}
@Override
public SnowManaSpentValue copy() {
return instance;
}
@Override
public String toString() {
return "1";
}
@Override
public String getMessage() {
return "{S} spent to cast this spell";
}
}

View file

@ -21,7 +21,7 @@ public enum SunburstCount implements DynamicValue {
if (!game.getStack().isEmpty()) {
StackObject spell = game.getStack().getFirst();
if (spell instanceof Spell && ((Spell) spell).getSourceId().equals(sourceAbility.getSourceId())) {
Mana mana = ((Spell) spell).getSpellAbility().getManaCostsToPay().getPayment();
Mana mana = ((Spell) spell).getSpellAbility().getManaCostsToPay().getUsedManaToPay();
if (mana.getBlack() > 0) {
count++;
}

View file

@ -1,8 +1,7 @@
package mage.players;
import java.io.Serializable;
import java.util.*;
import mage.ConditionalMana;
import mage.MageObject;
import mage.Mana;
import mage.abilities.Ability;
import mage.abilities.costs.Cost;
@ -18,6 +17,9 @@ import mage.game.events.ManaEvent;
import mage.game.events.ManaPaidEvent;
import mage.game.stack.Spell;
import java.io.Serializable;
import java.util.*;
/**
* @author BetaSteward_at_googlemail.com
*/
@ -35,12 +37,25 @@ public class ManaPool implements Serializable {
private final Set<ManaType> doNotEmptyManaTypes = new HashSet<>();
private static final class ConditionalManaInfo {
private final ManaType manaType;
private final MageObject sourceObject;
private ConditionalManaInfo(ManaType conditionalMana, MageObject sourceObject) {
this.manaType = conditionalMana;
this.sourceObject = sourceObject;
}
}
private boolean lastPaymentWasSnow;
public ManaPool(UUID playerId) {
this.playerId = playerId;
autoPayment = true;
autoPaymentRestricted = true;
unlockedManaType = null;
forcedToPay = false;
lastPaymentWasSnow = false;
}
public ManaPool(final ManaPool pool) {
@ -56,6 +71,7 @@ public class ManaPool implements Serializable {
poolBookmark.add(item.copy());
}
this.doNotEmptyManaTypes.addAll(pool.doNotEmptyManaTypes);
this.lastPaymentWasSnow = pool.lastPaymentWasSnow;
}
public int getRed() {
@ -89,6 +105,7 @@ public class ManaPool implements Serializable {
* @return
*/
public boolean pay(ManaType manaType, Ability ability, Filter filter, Game game, Cost costToPay, Mana usedManaToPay) {
lastPaymentWasSnow = false;
if (!isAutoPayment() && manaType != unlockedManaType) {
// if manual payment and the needed mana type was not unlocked, nothing will be paid
return false;
@ -112,12 +129,13 @@ public class ManaPool implements Serializable {
}
}
// first try to pay from conditional mana (the returned manaType can be changed if AsThoughEffects are active)
ManaType conditionalManaType = getConditional(manaType, ability, filter, game, costToPay, possibleAsThoughPoolManaType);
if (conditionalManaType != null) {
removeConditional(conditionalManaType, ability, game, costToPay, usedManaToPay);
ConditionalManaInfo manaInfo = getConditional(manaType, ability, filter, game, costToPay, possibleAsThoughPoolManaType);
if (manaInfo != null) {
removeConditional(manaInfo, ability, game, costToPay, usedManaToPay);
lockManaType(); // pay only one mana if mana payment is set to manually
return true;
}
lastPaymentWasSnow = false;
for (ManaPoolItem mana : manaItems) {
if (filter != null) {
@ -144,8 +162,9 @@ public class ManaPool implements Serializable {
if (mana.get(usableManaType) > 0) {
GameEvent event = new ManaPaidEvent(ability, mana.getSourceId(), mana.getFlag(), mana.getOriginalId());
game.fireEvent(event);
usedManaToPay.increase(usableManaType);
usedManaToPay.increase(usableManaType, mana.getSourceObject().isSnow());
mana.remove(usableManaType);
lastPaymentWasSnow |= mana.getSourceObject().isSnow();
if (mana.count() == 0) { // so no items with count 0 stay in list
manaItems.remove(mana);
}
@ -160,7 +179,7 @@ public class ManaPool implements Serializable {
return getMana().get(manaType);
}
private ManaType getConditional(ManaType manaType, Ability ability, Filter filter, Game game, Cost costToPay, ManaType possibleAsThoughPoolManaType) {
private ConditionalManaInfo getConditional(ManaType manaType, Ability ability, Filter filter, Game game, Cost costToPay, ManaType possibleAsThoughPoolManaType) {
if (ability == null || getConditionalMana().isEmpty()) {
return null;
}
@ -180,7 +199,7 @@ public class ManaPool implements Serializable {
if (manaTypeToUse != null && mana.getConditionalMana().apply(ability, game, mana.getSourceId(), costToPay)) {
if (filter == null
|| filter.match(mana.getSourceObject(), game)) {
return manaTypeToUse;
return new ConditionalManaInfo(manaTypeToUse, mana.getSourceObject());
}
}
}
@ -313,7 +332,7 @@ public class ManaPool implements Serializable {
if (mana instanceof ConditionalMana) {
ManaPoolItem item = new ManaPoolItem((ConditionalMana) mana, source.getSourceObject(game),
((ConditionalMana) mana).getManaProducerOriginalId() != null
? ((ConditionalMana) mana).getManaProducerOriginalId() : source.getOriginalId());
? ((ConditionalMana) mana).getManaProducerOriginalId() : source.getOriginalId());
if (emptyOnTurnsEnd) {
item.setDuration(Duration.EndOfTurn);
}
@ -365,11 +384,11 @@ public class ManaPool implements Serializable {
return new ManaPool(this);
}
private void removeConditional(ManaType manaType, Ability ability, Game game, Cost costToPay, Mana usedManaToPay) {
private void removeConditional(ConditionalManaInfo manaInfo, Ability ability, Game game, Cost costToPay, Mana usedManaToPay) {
for (ConditionalMana mana : getConditionalMana()) {
if (mana.get(manaType) > 0 && mana.apply(ability, game, mana.getManaProducerId(), costToPay)) {
mana.set(manaType, mana.get(manaType) - 1);
usedManaToPay.increase(manaType);
if (mana.get(manaInfo.manaType) > 0 && mana.apply(ability, game, mana.getManaProducerId(), costToPay)) {
mana.set(manaInfo.manaType, mana.get(manaInfo.manaType) - 1);
usedManaToPay.increase(manaInfo.manaType, manaInfo.sourceObject.isSnow());
GameEvent event = new ManaPaidEvent(ability, mana.getManaProducerId(), mana.getFlag(), mana.getManaProducerOriginalId());
game.fireEvent(event);
break;
@ -486,4 +505,8 @@ public class ManaPool implements Serializable {
throw new IllegalArgumentException("Wrong mana type " + manaType);
}
}
public boolean getLastPaymentWasSnow() {
return lastPaymentWasSnow;
}
}

View file

@ -162,7 +162,7 @@ public class ManaPoolItem implements Serializable {
}
public Mana getMana() {
return new Mana(red, green, blue, white, black, 0, 0, colorless);
return new Mana(white, blue, black, red, green, 0, 0, colorless);
}
public int count() {

View file

@ -28,7 +28,7 @@ public class ManaSpentToCastWatcher extends Watcher {
if (event.getType() == GameEvent.EventType.SPELL_CAST) {
Spell spell = (Spell) game.getObject(event.getTargetId());
if (spell != null && this.getSourceId().equals(spell.getSourceId())) {
payment = spell.getSpellAbility().getManaCostsToPay().getPayment();
payment = spell.getSpellAbility().getManaCostsToPay().getUsedManaToPay();
}
}
if (event.getType() == GameEvent.EventType.ZONE_CHANGE

View file

@ -3,13 +3,12 @@ package mage;
import mage.constants.ColoredManaSymbol;
import mage.constants.ManaType;
import mage.filter.FilterMana;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNotSame;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.junit.Assert.*;
/**
* Custom unit tests for {link Mana}.
@ -145,7 +144,7 @@ public class ManaTest {
public void shouldCreateManaFromIntegers() {
// when
Mana mana = new Mana(1, 2, 3, 4, 5, 6, 7, 8);
Mana mana = new Mana(4, 3, 5, 1, 2, 6, 7, 8);
// then
assertEquals(1, mana.getRed());
@ -163,7 +162,7 @@ public class ManaTest {
// given
// when
Mana mana = new Mana(-1, 2, 3, 4, 5, 6, 7, 0);
Mana mana = new Mana(4, 3, 5, -1, 2, 6, 7, 0);
// then
assertEquals(0, mana.getRed());
@ -315,11 +314,11 @@ public class ManaTest {
thisMana.add(thatMana);
// then
assertEquals(2, thisMana.getRed());
assertEquals(4, thisMana.getGreen());
assertEquals(6, thisMana.getBlue());
assertEquals(8, thisMana.getWhite());
assertEquals(10, thisMana.getBlack());
assertEquals(2, thisMana.getWhite());
assertEquals(4, thisMana.getBlue());
assertEquals(6, thisMana.getBlack());
assertEquals(8, thisMana.getRed());
assertEquals(10, thisMana.getGreen());
assertEquals(12, thisMana.getGeneric());
assertEquals(14, thisMana.getAny());
}
@ -420,7 +419,7 @@ public class ManaTest {
public void shouldSubtractCost() {
// given
Mana thisMana = new Mana(2, 2, 2, 2, 2, 2, 2, 0);
Mana thatMana = new Mana(10, 1, 1, 1, 10, 1, 1, 0);
Mana thatMana = new Mana(1, 1, 10, 10, 1, 1, 1, 0);
// when
thisMana.subtractCost(thatMana);
@ -471,7 +470,7 @@ public class ManaTest {
@Test
public void shouldReturnCount() {
// given
Mana mana = new Mana(1, 2, 3, 4, 5, 6, 7, 0);
Mana mana = new Mana(4, 3, 5, 1, 2, 6, 7, 0);
FilterMana filter = new FilterMana();
filter.setBlack(true);
@ -489,7 +488,7 @@ public class ManaTest {
@Test
public void shouldReturnString() {
// given
Mana mana = new Mana(1, 2, 3, 0, 3, 6, 2, 0);
Mana mana = new Mana(0, 3, 3, 1, 2, 6, 2, 0);
// when
String ret = mana.toString();