Fixed more CPU load caused by calculation of available mana.

This commit is contained in:
LevelX2 2015-02-14 00:22:17 +01:00
parent a4a36ff961
commit ad012ebd02
3 changed files with 51 additions and 13 deletions

View file

@ -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 "";

View file

@ -176,6 +176,7 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
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<Mana>, Serializable, Copyable<Mana> {
black--;
colorless++;
}
if (any > 0) {
any--;
colorless++;
}
if (oldColorless == colorless) {
break; // to prevent endless loop -> should not be possible, but who knows
}
}
}

View file

@ -131,8 +131,8 @@ public class ManaOptions extends ArrayList<Mana> {
}
}
}
}
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<Mana> {
}
}
}
}
else if (abilities.size() > 1) {
} else if (abilities.size() > 1) {
//perform a union of all existing options and the new options
List<Mana> copy = copy();
this.clear();
@ -166,18 +165,16 @@ public class ManaOptions extends ArrayList<Mana> {
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<Mana> {
}
}
}
}
}
}