Cost: make Cost better copyable

When adding CostParts, make CostAddMana join together
This commit is contained in:
Hanmac
2016-12-14 20:55:32 +00:00
parent 7a1231eb1a
commit ddb7bcd974
4 changed files with 36 additions and 17 deletions

View File

@@ -134,6 +134,10 @@ public class Cost {
return manapart == null ? ManaCost.ZERO : manapart.getManaToPay();
}
private Cost() {
}
private Cost(int genericMana) {
costParts.add(new CostPartMana(ManaCost.get(genericMana), null));
}
@@ -447,12 +451,21 @@ public class Cost {
return splitStr;
}
public final Cost copy() {
Cost toRet = new Cost();
toRet.isAbility = this.isAbility;
for (CostPart cp : this.costParts) {
toRet.costParts.add(cp.copy());
}
return toRet;
}
public final Cost copyWithNoMana() {
Cost toRet = new Cost(0);
toRet.isAbility = this.isAbility;
for (CostPart cp : this.costParts) {
if (!(cp instanceof CostPartMana))
toRet.costParts.add(cp);
toRet.costParts.add(cp.copy());
}
return toRet;
}
@@ -773,7 +786,8 @@ public class Cost {
} else {
costParts.add(0, new CostPartMana(oldManaCost.toManaCost(), r));
}
} else if (part instanceof CostDiscard || part instanceof CostTapType) {
} else if (part instanceof CostDiscard || part instanceof CostTapType ||
part instanceof CostAddMana) {
boolean alreadyAdded = false;
for (final CostPart other : costParts) {
if (other.getClass().equals(part.getClass()) &&
@@ -786,6 +800,8 @@ public class Cost {
} else if (part instanceof CostTapType) {
CostTapType tappart = (CostTapType)part;
costParts.add(new CostTapType(amount, part.getType(), part.getTypeDescription(), !tappart.canTapSource));
} else if (part instanceof CostAddMana) {
costParts.add(new CostAddMana(amount, part.getType(), part.getTypeDescription()));
}
toRemove.add(other);
alreadyAdded = true;

View File

@@ -71,10 +71,6 @@ public class CostAddMana extends CostPart {
public boolean payAsDecided(Player ai, PaymentDecision decision, SpellAbility sa) {
Card source = sa.getHostCard();
/*ColorSet cid = null;
if (ai.getGame().getRules().hasCommander()) {
cid = ai.getCommander().getRules().getColorIdentity();
}*/
List<Mana> manaProduced = new ArrayList<Mana>();
final String type = this.getType();
for (int n = 0; n < decision.c; n++) {

View File

@@ -1,13 +1,12 @@
package forge.game.cost;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.lang3.StringUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import forge.card.CardStateName;
@@ -44,8 +43,7 @@ public class CostAdjustment {
return cost;
}
// TODO: find better way to copy Cost object
Cost result = new Cost("0", sa.isAbility()).add(cost);
Cost result = cost.copy();
boolean isStateChangeToFaceDown = false;
if (sa.isSpell() && ((Spell) sa).isCastFaceDown()) {
@@ -60,7 +58,7 @@ public class CostAdjustment {
if (!cardsOnBattlefield.contains(host)) {
cardsOnBattlefield.add(host);
}
final List<StaticAbility> raiseAbilities = new ArrayList<StaticAbility>();
final List<StaticAbility> raiseAbilities = Lists.newArrayList();
// Sort abilities to apply them in proper order
for (Card c : cardsOnBattlefield) {
@@ -166,8 +164,8 @@ public class CostAdjustment {
if (!cardsOnBattlefield.contains(originalCard)) {
cardsOnBattlefield.add(originalCard);
}
final List<StaticAbility> reduceAbilities = new ArrayList<StaticAbility>();
final List<StaticAbility> setAbilities = new ArrayList<StaticAbility>();
final List<StaticAbility> reduceAbilities = Lists.newArrayList();
final List<StaticAbility> setAbilities = Lists.newArrayList();
// Sort abilities to apply them in proper order
for (Card c : cardsOnBattlefield) {
@@ -216,7 +214,7 @@ public class CostAdjustment {
if (!delved.isEmpty()) {
final Map<ZoneType, CardCollection> triggerList = Maps.newEnumMap(ZoneType.class);
triggerList.put(ZoneType.Graveyard, delved);
final HashMap<String, Object> runParams = new HashMap<String, Object>();
final Map<String, Object> runParams = Maps.newHashMap();
runParams.put("Cards", triggerList);
runParams.put("Destination", ZoneType.Exile);
game.getTriggerHandler().runTrigger(TriggerType.ChangesZoneAll, runParams, false);

View File

@@ -17,7 +17,6 @@
*/
package forge.game.cost;
import forge.game.CardTraitBase;
import forge.game.ability.AbilityUtils;
import forge.game.card.Card;
@@ -29,7 +28,7 @@ import org.apache.commons.lang3.StringUtils;
/**
* The Class CostPart.
*/
public abstract class CostPart implements Comparable<CostPart> {
public abstract class CostPart implements Comparable<CostPart>, Cloneable {
private String originalAmount;
private String amount;
private final String originalType, originalTypeDescription;
@@ -186,6 +185,16 @@ public abstract class CostPart implements Comparable<CostPart> {
public int paymentOrder() { return 5; }
public CostPart copy() {
CostPart clone = null;
try {
clone = (CostPart) clone();
} catch (final CloneNotSupportedException e) {
System.err.println(e);
}
return clone;
}
@Override
public int compareTo(CostPart o) {
return this.paymentOrder() - o.paymentOrder();