adjustedCost.add((VariableManaCost) vCost);
}
}
Mana reduceMana = new Mana();
for (ManaCost manaCost : manaCostsToReduce) {
reduceMana.add(manaCost.getMana());
}
// subtract colored mana
for (ManaCost newManaCost : previousCost) {
Mana mana = newManaCost.getMana();
if (mana.getColorless() > 0) {
continue;
}
if (mana.getBlack() > 0 && reduceMana.getBlack() > 0) {
if (reduceMana.getBlack() > mana.getBlack()) {
reduceMana.setBlack(reduceMana.getBlack()-mana.getBlack());
mana.setBlack(0);
} else {
mana.setBlack(mana.getBlack()-reduceMana.getBlack());
reduceMana.setBlack(0);
}
}
if (mana.getRed() > 0 && reduceMana.getRed() > 0) {
if (reduceMana.getRed() > mana.getRed()) {
reduceMana.setRed(reduceMana.getRed()-mana.getRed());
mana.setRed(0);
} else {
mana.setRed(mana.getRed()-reduceMana.getRed());
reduceMana.setRed(0);
}
}
if (mana.getBlue() > 0 && reduceMana.getBlue() > 0) {
if (reduceMana.getBlue() > mana.getBlue()) {
reduceMana.setBlue(reduceMana.getBlue()-mana.getBlue());
mana.setBlue(0);
} else {
mana.setBlue(mana.getBlue()-reduceMana.getBlue());
reduceMana.setBlue(0);
}
}
if (mana.getGreen() > 0 && reduceMana.getGreen() > 0) {
if (reduceMana.getGreen() > mana.getGreen()) {
reduceMana.setGreen(reduceMana.getGreen()-mana.getGreen());
mana.setGreen(0);
} else {
mana.setGreen(mana.getGreen()-reduceMana.getGreen());
reduceMana.setGreen(0);
}
}
if (mana.getWhite() > 0 && reduceMana.getWhite() > 0) {
if (reduceMana.getWhite() > mana.getWhite()) {
reduceMana.setWhite(reduceMana.getWhite()-mana.getWhite());
mana.setWhite(0);
} else {
mana.setWhite(mana.getWhite()-reduceMana.getWhite());
reduceMana.setWhite(0);
}
}
if (newManaCost instanceof HybridManaCost) {
if (mana.count() > 1) {
adjustedCost.add(newManaCost);
}
} else if (mana.count() > 0) {
adjustedCost.add(newManaCost);
}
}
// subtract colorless mana, use all mana that is left
int reduceAmount;
if (convertToGeneric) {
reduceAmount = reduceMana.count();
} else {
reduceAmount = reduceMana.getColorless();
}
for (ManaCost newManaCost : previousCost) {
Mana mana = newManaCost.getMana();
if (mana.getColorless() == 0) {
continue;
}
if (mana.getColorless() > 0 && reduceAmount > 0) {
if (reduceAmount > mana.getColorless()) {
reduceAmount -= mana.getColorless();
mana.setColorless(0);
} else {
mana.setColorless(mana.getColorless() - reduceAmount);
reduceAmount = 0;
}
}
if (mana.count() > 0) {
adjustedCost.add(0, new GenericManaCost(mana.count()));
}
}
adjustedCost.setSourceFilter(previousCost.getSourceFilter()); // keep mana source restrictions
spellAbility.getManaCostsToPay().clear();
spellAbility.getManaCostsToPay().addAll(adjustedCost);