From ad012ebd02a23be22c73213110408df0c017f53b Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Sat, 14 Feb 2015 00:22:17 +0100 Subject: [PATCH] Fixed more CPU load caused by calculation of available mana. --- .../org/mage/test/utils/ManaOptionsTest.java | 34 ++++++++++++++++++- Mage/src/mage/Mana.java | 8 +++++ Mage/src/mage/abilities/mana/ManaOptions.java | 22 ++++++------ 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/Mage.Tests/src/test/java/org/mage/test/utils/ManaOptionsTest.java b/Mage.Tests/src/test/java/org/mage/test/utils/ManaOptionsTest.java index c1ae62abce0..eaf0af7d8b2 100644 --- a/Mage.Tests/src/test/java/org/mage/test/utils/ManaOptionsTest.java +++ b/Mage.Tests/src/test/java/org/mage/test/utils/ManaOptionsTest.java @@ -198,7 +198,39 @@ public class ManaOptionsTest extends CardTestPlayerBase { Assert.assertEquals("{G}{G}{G}{G}{G}", getManaOption(0, manaOptions)); Assert.assertEquals("{R}{R}{R}{G}", getManaOption(1, manaOptions)); } - + + @Test + public void testNykthos3() { + addCard(Zone.BATTLEFIELD, playerA, "Sylvan Caryatid", 1); + addCard(Zone.BATTLEFIELD, playerA, "Forest", 1); + addCard(Zone.BATTLEFIELD, playerA, "Nykthos, Shrine to Nyx", 1); + + setStopAt(1, PhaseStep. UPKEEP); + execute(); + + ManaOptions manaOptions = playerA.getAvailableManaTest(currentGame); + + Assert.assertEquals("mana variations don't fit",1, manaOptions.size()); + Assert.assertEquals("{1}{G}{Any}", getManaOption(0, manaOptions)); + } + + @Test + public void testMix1() { + addCard(Zone.BATTLEFIELD, playerA, "Chromatic Star", 1); + addCard(Zone.BATTLEFIELD, playerA, "Chromatic Sphere", 1); + addCard(Zone.BATTLEFIELD, playerA, "Urza's Tower", 1); + addCard(Zone.BATTLEFIELD, playerA, "Grove of the Burnwillows", 1); + + setStopAt(1, PhaseStep. UPKEEP); + execute(); + + ManaOptions manaOptions = playerA.getAvailableManaTest(currentGame); + + Assert.assertEquals("mana variations don't fit",2, manaOptions.size()); + Assert.assertEquals("{Any}{Any}", getManaOption(0, manaOptions)); + Assert.assertEquals("{Any}{Any}", getManaOption(1, manaOptions)); + } + private String getManaOption(int index, ManaOptions manaOptions) { if (manaOptions.size() < index + 1) { return ""; diff --git a/Mage/src/mage/Mana.java b/Mage/src/mage/Mana.java index cf3962faffb..cfce6a295f0 100644 --- a/Mage/src/mage/Mana.java +++ b/Mage/src/mage/Mana.java @@ -176,6 +176,7 @@ public class Mana implements Comparable, Serializable, Copyable { any -= cost.getAny(); colorless -= cost.getColorless(); while (colorless < 0) { + int oldColorless = colorless; if (red > 0) { red--; colorless++; @@ -200,6 +201,13 @@ public class Mana implements Comparable, Serializable, Copyable { black--; colorless++; } + if (any > 0) { + any--; + colorless++; + } + if (oldColorless == colorless) { + break; // to prevent endless loop -> should not be possible, but who knows + } } } diff --git a/Mage/src/mage/abilities/mana/ManaOptions.java b/Mage/src/mage/abilities/mana/ManaOptions.java index 370c9c5471b..dbadf31cfb7 100644 --- a/Mage/src/mage/abilities/mana/ManaOptions.java +++ b/Mage/src/mage/abilities/mana/ManaOptions.java @@ -131,8 +131,8 @@ public class ManaOptions extends ArrayList { } } } - } - else { + } else { + // the ability has mana costs if (netManas.size() == 1) { subtractCostAddMana(ability.getManaCosts().getMana(), netManas.get(0), ability.getCosts().isEmpty()); } else { @@ -148,8 +148,7 @@ public class ManaOptions extends ArrayList { } } } - } - else if (abilities.size() > 1) { + } else if (abilities.size() > 1) { //perform a union of all existing options and the new options List copy = copy(); this.clear(); @@ -166,18 +165,16 @@ public class ManaOptions extends ArrayList { this.add(newMana); } } - } - else { + } else { for (Mana netMana: netManas) { CombineWithExisting: - for (Mana mana: copy) { - Mana newMana = new Mana(); - newMana.add(mana); - if (mana.includesMana(ability.getManaCosts().getMana())) { // costs can be paid + for (Mana previousMana: copy) { + Mana newMana = new Mana(previousMana); + if (previousMana.includesMana(ability.getManaCosts().getMana())) { // costs can be paid newMana.subtractCost(ability.getManaCosts().getMana()); newMana.add(netMana); - // if the new mana is more than another already existing than replace - for(Mana existingMana: this) { + // if the new mana is in all colors more than another already existing than replace + for (Mana existingMana: this) { Mana moreValuable = Mana.getMoreValuableMana(newMana, existingMana); if (moreValuable != null) { existingMana.setToMana(moreValuable); @@ -190,6 +187,7 @@ public class ManaOptions extends ArrayList { } } } + } } }