Resolve "Avoid bad performance pattern involving Interger.parseInt"

This commit is contained in:
Hans Mackowiak
2020-05-31 14:22:25 +00:00
committed by Michael Kamensky
parent 33e7cde2d6
commit 19c07f5bef
17 changed files with 171 additions and 329 deletions

View File

@@ -24,6 +24,7 @@ import forge.ai.ability.AnimateAi;
import forge.card.CardTypeView; import forge.card.CardTypeView;
import forge.game.GameEntity; import forge.game.GameEntity;
import forge.game.ability.AbilityFactory; import forge.game.ability.AbilityFactory;
import forge.game.ability.AbilityUtils;
import forge.game.ability.ApiType; import forge.game.ability.ApiType;
import forge.game.ability.effects.ProtectEffect; import forge.game.ability.effects.ProtectEffect;
import forge.game.card.*; import forge.game.card.*;
@@ -1135,7 +1136,6 @@ public class AiAttackController {
// TODO Somehow subtract expected damage of other attacking creatures from enemy life total (how? other attackers not yet declared? Can the AI guesstimate which of their creatures will not get blocked?) // TODO Somehow subtract expected damage of other attacking creatures from enemy life total (how? other attackers not yet declared? Can the AI guesstimate which of their creatures will not get blocked?)
if (attacker.getCurrentPower() * Integer.parseInt(attacker.getSVar("NonCombatPriority")) < ai.getOpponentsSmallestLifeTotal()) { if (attacker.getCurrentPower() * Integer.parseInt(attacker.getSVar("NonCombatPriority")) < ai.getOpponentsSmallestLifeTotal()) {
// Check if the card actually has an ability the AI can and wants to play, if not, attacking is fine! // Check if the card actually has an ability the AI can and wants to play, if not, attacking is fine!
boolean wantability = false;
for (SpellAbility sa : attacker.getSpellAbilities()) { for (SpellAbility sa : attacker.getSpellAbilities()) {
// Do not attack if we can afford using the ability. // Do not attack if we can afford using the ability.
if (sa.isAbility()) { if (sa.isAbility()) {
@@ -1365,21 +1365,12 @@ public class AiAttackController {
if (c.hasSVar("AIExertCondition")) { if (c.hasSVar("AIExertCondition")) {
if (!c.getSVar("AIExertCondition").isEmpty()) { if (!c.getSVar("AIExertCondition").isEmpty()) {
final String needsToExert = c.getSVar("AIExertCondition"); final String needsToExert = c.getSVar("AIExertCondition");
int x = 0;
int y = 0;
String sVar = needsToExert.split(" ")[0]; String sVar = needsToExert.split(" ")[0];
String comparator = needsToExert.split(" ")[1]; String comparator = needsToExert.split(" ")[1];
String compareTo = comparator.substring(2); String compareTo = comparator.substring(2);
try {
x = Integer.parseInt(sVar); int x = AbilityUtils.calculateAmount(c, sVar, null);
} catch (final NumberFormatException e) { int y = AbilityUtils.calculateAmount(c, compareTo, null);
x = CardFactoryUtil.xCount(c, c.getSVar(sVar));
}
try {
y = Integer.parseInt(compareTo);
} catch (final NumberFormatException e) {
y = CardFactoryUtil.xCount(c, c.getSVar(compareTo));
}
if (Expressions.compare(x, comparator, y)) { if (Expressions.compare(x, comparator, y)) {
shouldExert = true; shouldExert = true;
} }

View File

@@ -1908,21 +1908,12 @@ public class ComputerUtilCard {
} }
if (card.getSVar(needsToPlayVarName).length() > 0) { if (card.getSVar(needsToPlayVarName).length() > 0) {
final String needsToPlay = card.getSVar(needsToPlayVarName); final String needsToPlay = card.getSVar(needsToPlayVarName);
int x = 0;
int y = 0;
String sVar = needsToPlay.split(" ")[0]; String sVar = needsToPlay.split(" ")[0];
String comparator = needsToPlay.split(" ")[1]; String comparator = needsToPlay.split(" ")[1];
String compareTo = comparator.substring(2); String compareTo = comparator.substring(2);
try { int x = AbilityUtils.calculateAmount(card, sVar, sa);
x = Integer.parseInt(sVar); int y = AbilityUtils.calculateAmount(card, compareTo, sa);
} catch (final NumberFormatException e) {
x = CardFactoryUtil.xCount(card, card.getSVar(sVar));
}
try {
y = Integer.parseInt(compareTo);
} catch (final NumberFormatException e) {
y = CardFactoryUtil.xCount(card, card.getSVar(compareTo));
}
if (!Expressions.compare(x, comparator, y)) { if (!Expressions.compare(x, comparator, y)) {
return AiPlayDecision.NeedsToPlayCriteriaNotMet; return AiPlayDecision.NeedsToPlayCriteriaNotMet;
} }

View File

@@ -973,56 +973,39 @@ public class ComputerUtilCombat {
} }
theTriggers.addAll(attacker.getTriggers()); theTriggers.addAll(attacker.getTriggers());
for (final Trigger trigger : theTriggers) { for (final Trigger trigger : theTriggers) {
final Map<String, String> trigParams = trigger.getMapParams();
final Card source = trigger.getHostCard(); final Card source = trigger.getHostCard();
if (!ComputerUtilCombat.combatTriggerWillTrigger(attacker, blocker, trigger, null)) { if (!ComputerUtilCombat.combatTriggerWillTrigger(attacker, blocker, trigger, null)) {
continue; continue;
} }
Map<String, String> abilityParams = null; SpellAbility sa = trigger.ensureAbility();
if (trigger.getOverridingAbility() != null) { if (sa == null) {
abilityParams = trigger.getOverridingAbility().getMapParams();
} else if (trigParams.containsKey("Execute")) {
final String ability = source.getSVar(trigParams.get("Execute"));
abilityParams = AbilityFactory.getMapParams(ability);
} else {
continue; continue;
} }
if (abilityParams.containsKey("AB") && !abilityParams.get("AB").equals("Pump")) { if (!ApiType.Pump.equals(sa.getApi())) {
continue; continue;
} }
if (abilityParams.containsKey("DB") && !abilityParams.get("DB").equals("Pump")) {
if (sa.usesTargeting()) {
continue; continue;
} }
if (abilityParams.containsKey("ValidTgts") || abilityParams.containsKey("Tgt")) {
continue; // targeted pumping not supported if (!sa.hasParam("NumAtt")) {
continue;
} }
final List<Card> list = AbilityUtils.getDefinedCards(source, abilityParams.get("Defined"), null);
if (abilityParams.containsKey("Defined") && abilityParams.get("Defined").equals("TriggeredBlocker")) { String defined = sa.getParam("Defined");
final List<Card> list = AbilityUtils.getDefinedCards(source, defined, sa);
if ("TriggeredBlocker".equals(defined)) {
list.add(blocker); list.add(blocker);
} }
if (list.isEmpty()) {
continue;
}
if (!list.contains(blocker)) { if (!list.contains(blocker)) {
continue; continue;
} }
if (!abilityParams.containsKey("NumAtt")) {
continue;
}
String att = abilityParams.get("NumAtt"); power += AbilityUtils.calculateAmount(source, sa.getParam("NumAtt"), sa, true);
if (att.startsWith("+")) {
att = att.substring(1);
}
try {
power += Integer.parseInt(att);
} catch (final NumberFormatException nfe) {
// can't parse the number (X for example)
power += 0;
}
} }
if (withoutAbilities) { if (withoutAbilities) {
return power; return power;
@@ -1108,95 +1091,54 @@ public class ComputerUtilCombat {
} }
theTriggers.addAll(attacker.getTriggers()); theTriggers.addAll(attacker.getTriggers());
for (final Trigger trigger : theTriggers) { for (final Trigger trigger : theTriggers) {
final Map<String, String> trigParams = trigger.getMapParams();
final Card source = trigger.getHostCard(); final Card source = trigger.getHostCard();
if (!ComputerUtilCombat.combatTriggerWillTrigger(attacker, blocker, trigger, null)) { if (!ComputerUtilCombat.combatTriggerWillTrigger(attacker, blocker, trigger, null)) {
continue; continue;
} }
Map<String, String> abilityParams = null; SpellAbility sa = trigger.ensureAbility();
if (trigger.getOverridingAbility() != null) { if (sa == null) {
abilityParams = trigger.getOverridingAbility().getMapParams();
} else if (trigParams.containsKey("Execute")) {
final String ability = source.getSVar(trigParams.get("Execute"));
abilityParams = AbilityFactory.getMapParams(ability);
} else {
continue; continue;
} }
String abType = "";
if (abilityParams.containsKey("AB")) {
abType = abilityParams.get("AB");
} else if (abilityParams.containsKey("DB")) {
abType = abilityParams.get("DB");
}
// DealDamage triggers // DealDamage triggers
if (abType.equals("DealDamage")) { if (ApiType.DealDamage.equals(sa.getApi())) {
if (!abilityParams.containsKey("Defined") || !abilityParams.get("Defined").equals("TriggeredBlocker")) { if (!"TriggeredBlocker".equals(sa.getParam("Defined"))) {
continue;
}
int damage = 0;
try {
damage = Integer.parseInt(abilityParams.get("NumDmg"));
} catch (final NumberFormatException nfe) {
// can't parse the number (X for example)
continue; continue;
} }
int damage = AbilityUtils.calculateAmount(source, sa.getParam("NumDmg"), sa);
toughness -= predictDamageTo(blocker, damage, 0, source, false); toughness -= predictDamageTo(blocker, damage, 0, source, false);
continue; } else
}
// -1/-1 PutCounter triggers // -1/-1 PutCounter triggers
if (abType.equals("PutCounter")) { if (ApiType.PutCounter.equals(sa.getApi())) {
if (!abilityParams.containsKey("Defined") || !abilityParams.get("Defined").equals("TriggeredBlocker")) { if (!"TriggeredBlocker".equals(sa.getParam("Defined"))) {
continue; continue;
} }
if (!abilityParams.containsKey("CounterType") || !abilityParams.get("CounterType").equals("M1M1")) { if (!"M1M1".equals(sa.getParam("CounterType"))) {
continue; continue;
} }
int num = 0; toughness -= AbilityUtils.calculateAmount(source, sa.getParam("CounterNum"), sa);
try { } else
num = Integer.parseInt(abilityParams.get("CounterNum"));
} catch (final NumberFormatException nfe) {
// can't parse the number (X for example)
continue;
}
toughness -= num;
continue;
}
// Pump triggers // Pump triggers
if (!abType.equals("Pump")) { if (ApiType.Pump.equals(sa.getApi())) {
continue; if (sa.usesTargeting()) {
} continue; // targeted pumping not supported
if (abilityParams.containsKey("ValidTgts") || abilityParams.containsKey("Tgt")) { }
continue; // targeted pumping not supported final List<Card> list = AbilityUtils.getDefinedCards(source, sa.getParam("Defined"), null);
} if ("TriggeredBlocker".equals(sa.getParam("Defined"))) {
final List<Card> list = AbilityUtils.getDefinedCards(source, abilityParams.get("Defined"), null); list.add(blocker);
if (abilityParams.containsKey("Defined") && abilityParams.get("Defined").equals("TriggeredBlocker")) { }
list.add(blocker); if (list.isEmpty() || !list.contains(blocker)) {
} continue;
if (list.isEmpty()) { }
continue; if (!sa.hasParam("NumDef")) {
} continue;
if (!list.contains(blocker)) { }
continue;
}
if (!abilityParams.containsKey("NumDef")) {
continue;
}
String def = abilityParams.get("NumDef");
if (def.startsWith("+")) {
def = def.substring(1);
}
try {
toughness += Integer.parseInt(def);
} catch (final NumberFormatException nfe) {
// can't parse the number (X for example)
toughness += AbilityUtils.calculateAmount(source, sa.getParam("NumDef"), sa, true);
} }
} }
if (withoutAbilities) { if (withoutAbilities) {
@@ -1521,141 +1463,128 @@ public class ComputerUtilCombat {
final CardCollectionView cardList = game.getCardsIn(ZoneType.Battlefield); final CardCollectionView cardList = game.getCardsIn(ZoneType.Battlefield);
for (final Card card : cardList) { for (final Card card : cardList) {
for (final StaticAbility stAb : card.getStaticAbilities()) { for (final StaticAbility stAb : card.getStaticAbilities()) {
final Map<String, String> params = stAb.getMapParams(); if (!"Continuous".equals(stAb.getParam("Mode"))) {
if (!params.get("Mode").equals("Continuous")) {
continue; continue;
} }
if (params.containsKey("Affected") && params.get("Affected").contains("attacking")) { if (!stAb.hasParam("Affected")) {
final String valid = TextUtil.fastReplace(params.get("Affected"), "attacking", "Creature"); continue;
}
if (!stAb.hasParam("AddToughness")) {
continue;
}
String affected = stAb.getParam("Affected");
String addT = stAb.getParam("AddToughness");
if (affected.contains("attacking")) {
final String valid = TextUtil.fastReplace(affected, "attacking", "Creature");
if (!attacker.isValid(valid, card.getController(), card, null)) { if (!attacker.isValid(valid, card.getController(), card, null)) {
continue; continue;
} }
if (params.containsKey("AddToughness")) { toughness += AbilityUtils.calculateAmount(card, addT, stAb, true);
if (params.get("AddToughness").equals("X")) { } else if (affected.contains("untapped")) {
toughness += CardFactoryUtil.xCount(card, card.getSVar("X")); final String valid = TextUtil.fastReplace(affected, "untapped", "Creature");
} else if (params.get("AddToughness").equals("Y")) {
toughness += CardFactoryUtil.xCount(card, card.getSVar("Y"));
} else {
toughness += Integer.valueOf(params.get("AddToughness"));
}
}
} else if (params.containsKey("Affected") && params.get("Affected").contains("untapped")) {
final String valid = TextUtil.fastReplace(params.get("Affected"), "untapped", "Creature");
if (!attacker.isValid(valid, card.getController(), card, null) if (!attacker.isValid(valid, card.getController(), card, null)
|| attacker.hasKeyword(Keyword.VIGILANCE)) { || attacker.hasKeyword(Keyword.VIGILANCE)) {
continue; continue;
} }
// remove the bonus, because it will no longer be granted // remove the bonus, because it will no longer be granted
if (params.containsKey("AddToughness")) { toughness -= AbilityUtils.calculateAmount(card, addT, stAb, true);
toughness -= Integer.valueOf(params.get("AddToughness"));
}
} }
} }
} }
} }
for (final Trigger trigger : theTriggers) { for (final Trigger trigger : theTriggers) {
final Map<String, String> trigParams = trigger.getMapParams();
final Card source = trigger.getHostCard(); final Card source = trigger.getHostCard();
if (!ComputerUtilCombat.combatTriggerWillTrigger(attacker, blocker, trigger, combat)) { if (!ComputerUtilCombat.combatTriggerWillTrigger(attacker, blocker, trigger, combat)) {
continue; continue;
} }
Map<String, String> abilityParams = null; SpellAbility sa = trigger.ensureAbility();
if (trigger.getOverridingAbility() != null) { if (sa == null) {
abilityParams = trigger.getOverridingAbility().getMapParams();
} else if (trigParams.containsKey("Execute")) {
final String ability = source.getSVar(trigParams.get("Execute"));
abilityParams = AbilityFactory.getMapParams(ability);
} else {
continue; continue;
} }
sa.setActivatingPlayer(source.getController());
if (abilityParams.containsKey("ValidTgts") || abilityParams.containsKey("Tgt")) { if (sa.usesTargeting()) {
continue; // targeted pumping not supported continue; // targeted pumping not supported
} }
// DealDamage triggers // DealDamage triggers
if ((abilityParams.containsKey("AB") && abilityParams.get("AB").equals("DealDamage")) if (ApiType.DealDamage.equals(sa.getApi())) {
|| (abilityParams.containsKey("DB") && abilityParams.get("DB").equals("DealDamage"))) { if ("TriggeredAttacker".equals(sa.getParam("Defined"))) {
if (!abilityParams.containsKey("Defined") || !abilityParams.get("Defined").equals("TriggeredAttacker")) {
continue;
}
int damage = 0;
try {
damage = Integer.parseInt(abilityParams.get("NumDmg"));
} catch (final NumberFormatException nfe) {
// can't parse the number (X for example)
continue; continue;
} }
int damage = AbilityUtils.calculateAmount(source, sa.getParam("NumDmg"), sa);
toughness -= predictDamageTo(attacker, damage, 0, source, false); toughness -= predictDamageTo(attacker, damage, 0, source, false);
continue; continue;
} } else if (ApiType.Pump.equals(sa.getApi())) {
// Pump triggers if (sa.hasParam("Cost")) {
if (abilityParams.containsKey("AB") && !abilityParams.get("AB").equals("Pump") if (!CostPayment.canPayAdditionalCosts(sa.getPayCosts(), sa)) {
&& !abilityParams.get("AB").equals("PumpAll")) { continue;
continue; }
}
if (abilityParams.containsKey("DB") && !abilityParams.get("DB").equals("Pump")
&& !abilityParams.get("DB").equals("PumpAll")) {
continue;
}
if (abilityParams.containsKey("Cost")) {
SpellAbility sa = null;
if (trigger.getOverridingAbility() != null) {
sa = trigger.getOverridingAbility();
} else {
final String ability = source.getSVar(trigParams.get("Execute"));
sa = AbilityFactory.getAbility(ability, source);
} }
if (!sa.hasParam("NumDef")) {
sa.setActivatingPlayer(source.getController());
if (!CostPayment.canPayAdditionalCosts(sa.getPayCosts(), sa)) {
continue; continue;
} }
} CardCollection list = AbilityUtils.getDefinedCards(source, sa.getParam("Defined"), sa);
if ("TriggeredAttacker".equals(sa.getParam("Defined"))) {
List<Card> list = Lists.newArrayList();
if (!abilityParams.containsKey("ValidCards")) {
list = AbilityUtils.getDefinedCards(source, abilityParams.get("Defined"), null);
}
if (abilityParams.containsKey("Defined") && abilityParams.get("Defined").equals("TriggeredAttacker")) {
list.add(attacker);
}
if (abilityParams.containsKey("ValidCards")) {
if (attacker.isValid(abilityParams.get("ValidCards").split(","), source.getController(), source, null)
|| attacker.isValid(abilityParams.get("ValidCards").replace("attacking+", "").split(","),
source.getController(), source, null)) {
list.add(attacker); list.add(attacker);
} }
} if (!list.contains(attacker)) {
if (list.isEmpty()) { continue;
continue; }
}
if (!list.contains(attacker)) { String def = sa.getParam("NumDef");
continue; if (def.startsWith("+")) {
} def = def.substring(1);
if (!abilityParams.containsKey("NumDef")) { }
continue; if (def.matches("[0-9][0-9]?") || def.matches("-" + "[0-9][0-9]?")) {
} toughness += Integer.parseInt(def);
} else {
String def = abilityParams.get("NumDef"); String bonus = AbilityUtils.getSVar(sa, def);
if (def.startsWith("+")) { if (bonus.contains("TriggerCount$NumBlockers")) {
def = def.substring(1); bonus = TextUtil.fastReplace(bonus, "TriggerCount$NumBlockers", "Number$1");
} } else if (bonus.contains("TriggeredPlayersDefenders$Amount")) { // for Melee
if (def.matches("[0-9][0-9]?") || def.matches("-" + "[0-9][0-9]?")) { bonus = TextUtil.fastReplace(bonus, "TriggeredPlayersDefenders$Amount", "Number$1");
toughness += Integer.parseInt(def); }
} else { toughness += CardFactoryUtil.xCount(source, bonus);
String bonus = source.getSVar(def); }
if (bonus.contains("TriggerCount$NumBlockers")) { } else if (ApiType.PumpAll.equals(sa.getApi())) {
bonus = TextUtil.fastReplace(bonus, "TriggerCount$NumBlockers", "Number$1");
} else if (bonus.contains("TriggeredPlayersDefenders$Amount")) { // for Melee if (sa.hasParam("Cost")) {
bonus = TextUtil.fastReplace(bonus, "TriggeredPlayersDefenders$Amount", "Number$1"); if (!CostPayment.canPayAdditionalCosts(sa.getPayCosts(), sa)) {
continue;
}
}
if (!sa.hasParam("ValidCards")) {
continue;
}
if (!sa.hasParam("NumDef")) {
continue;
}
if (!attacker.isValid(sa.getParam("ValidCards").replace("attacking+", "").split(","), source.getController(), source, sa)) {
continue;
}
String def = sa.getParam("NumDef");
if (def.startsWith("+")) {
def = def.substring(1);
}
if (def.matches("[0-9][0-9]?") || def.matches("-" + "[0-9][0-9]?")) {
toughness += Integer.parseInt(def);
} else {
String bonus = AbilityUtils.getSVar(sa, def);
if (bonus.contains("TriggerCount$NumBlockers")) {
bonus = TextUtil.fastReplace(bonus, "TriggerCount$NumBlockers", "Number$1");
} else if (bonus.contains("TriggeredPlayersDefenders$Amount")) { // for Melee
bonus = TextUtil.fastReplace(bonus, "TriggeredPlayersDefenders$Amount", "Number$1");
}
toughness += CardFactoryUtil.xCount(source, bonus);
} }
toughness += CardFactoryUtil.xCount(source, bonus);
} }
} }
if (withoutAbilities) { if (withoutAbilities) {
@@ -1672,18 +1601,19 @@ public class ComputerUtilCombat {
if (ability.usesTargeting() && !ability.canTarget(attacker)) { if (ability.usesTargeting() && !ability.canTarget(attacker)) {
continue; continue;
} }
if (ability.getPayCosts().hasTapCost() && !attacker.hasKeyword(Keyword.VIGILANCE)) {
continue;
}
if (!ComputerUtilCost.canPayCost(ability, attacker.getController())) {
continue;
}
if (ability.getApi() == ApiType.Pump) { if (ability.getApi() == ApiType.Pump) {
if (!ability.hasParam("NumDef")) { if (!ability.hasParam("NumDef")) {
continue; continue;
} }
if (!ability.getPayCosts().hasTapCost() && ComputerUtilCost.canPayCost(ability, attacker.getController())) { toughness += AbilityUtils.calculateAmount(ability.getHostCard(), ability.getParam("NumDef"), ability, true);
int tBonus = AbilityUtils.calculateAmount(ability.getHostCard(), ability.getParam("NumDef"), ability);
if (tBonus > 0) {
toughness += tBonus;
}
}
} else if (ability.getApi() == ApiType.PutCounter) { } else if (ability.getApi() == ApiType.PutCounter) {
if (!ability.hasParam("CounterType") || !ability.getParam("CounterType").equals("P1P1")) { if (!ability.hasParam("CounterType") || !ability.getParam("CounterType").equals("P1P1")) {
continue; continue;
@@ -1697,11 +1627,9 @@ public class ComputerUtilCombat {
continue; continue;
} }
if (!ability.getPayCosts().hasTapCost() && ComputerUtilCost.canPayCost(ability, attacker.getController())) { int tBonus = AbilityUtils.calculateAmount(ability.getHostCard(), ability.getParam("CounterNum"), ability);
int tBonus = AbilityUtils.calculateAmount(ability.getHostCard(), ability.getParam("CounterNum"), ability); if (tBonus > 0) {
if (tBonus > 0) { toughness += tBonus;
toughness += tBonus;
}
} }
} }
} }

View File

@@ -467,9 +467,9 @@ public class ComputerUtilCost {
if(!meetsRestriction) if(!meetsRestriction)
continue; continue;
try { if (StringUtils.isNumeric(parts[0])) {
extraManaNeeded += Integer.parseInt(parts[0]); extraManaNeeded += Integer.parseInt(parts[0]);
} catch (final NumberFormatException e) { } else {
System.out.println("wrong SpellsNeedExtraMana SVar format on " + c); System.out.println("wrong SpellsNeedExtraMana SVar format on " + c);
} }
} }
@@ -480,9 +480,9 @@ public class ComputerUtilCost {
} }
final String snem = c.getSVar("SpellsNeedExtraManaEffect"); final String snem = c.getSVar("SpellsNeedExtraManaEffect");
if (!StringUtils.isBlank(snem)) { if (!StringUtils.isBlank(snem)) {
try { if (StringUtils.isNumeric(snem)) {
extraManaNeeded += Integer.parseInt(snem); extraManaNeeded += Integer.parseInt(snem);
} catch (final NumberFormatException e) { } else {
System.out.println("wrong SpellsNeedExtraManaEffect SVar format on " + c); System.out.println("wrong SpellsNeedExtraManaEffect SVar format on " + c);
} }
} }

View File

@@ -531,12 +531,10 @@ public final class CardRules implements ICardCharacteristics {
public final ManaCostShard next() { public final ManaCostShard next() {
final String unparsed = st.nextToken(); final String unparsed = st.nextToken();
// System.out.println(unparsed); // System.out.println(unparsed);
try { if (StringUtils.isNumeric(unparsed)) {
int iVal = Integer.parseInt(unparsed); this.genericCost += Integer.parseInt(unparsed);
this.genericCost += iVal;
return null; return null;
} }
catch (NumberFormatException nex) { }
return ManaCostShard.parseNonGeneric(unparsed); return ManaCostShard.parseNonGeneric(unparsed);
} }

View File

@@ -5,7 +5,6 @@ import forge.card.mana.ManaAtom;
import forge.game.ability.AbilityUtils; import forge.game.ability.AbilityUtils;
import forge.game.card.Card; import forge.game.card.Card;
import forge.game.card.CardCollection; import forge.game.card.CardCollection;
import forge.game.card.CardFactoryUtil;
import forge.game.card.CardLists; import forge.game.card.CardLists;
import forge.game.card.CardUtil; import forge.game.card.CardUtil;
import forge.game.card.CardView; import forge.game.card.CardView;
@@ -271,13 +270,8 @@ public abstract class CardTraitBase extends GameObject implements IHasCardView {
lifeCompare = params.get("LifeAmount"); lifeCompare = params.get("LifeAmount");
} }
int right = 1;
final String rightString = lifeCompare.substring(2); final String rightString = lifeCompare.substring(2);
try { int right = AbilityUtils.calculateAmount(getHostCard(), rightString, this);
right = Integer.parseInt(rightString);
} catch (final NumberFormatException nfe) {
right = CardFactoryUtil.xCount(this.getHostCard(), this.getHostCard().getSVar(rightString));
}
if (!Expressions.compare(life, lifeCompare, right)) { if (!Expressions.compare(life, lifeCompare, right)) {
return false; return false;
@@ -314,13 +308,9 @@ public abstract class CardTraitBase extends GameObject implements IHasCardView {
} }
list = CardLists.getValidCards(list, sIsPresent.split(","), this.getHostCard().getController(), this.getHostCard(), null); list = CardLists.getValidCards(list, sIsPresent.split(","), this.getHostCard().getController(), this.getHostCard(), null);
int right = 1;
final String rightString = presentCompare.substring(2); final String rightString = presentCompare.substring(2);
try { int right = AbilityUtils.calculateAmount(getHostCard(), rightString, this);
right = Integer.parseInt(rightString);
} catch (final NumberFormatException nfe) {
right = CardFactoryUtil.xCount(this.getHostCard(), this.getHostCard().getSVar(rightString));
}
final int left = list.size(); final int left = list.size();
if (!Expressions.compare(left, presentCompare, right)) { if (!Expressions.compare(left, presentCompare, right)) {
@@ -355,13 +345,8 @@ public abstract class CardTraitBase extends GameObject implements IHasCardView {
list = CardLists.getValidCards(list, sIsPresent.split(","), this.getHostCard().getController(), this.getHostCard(), null); list = CardLists.getValidCards(list, sIsPresent.split(","), this.getHostCard().getController(), this.getHostCard(), null);
int right = 1;
final String rightString = presentCompare.substring(2); final String rightString = presentCompare.substring(2);
try { int right = AbilityUtils.calculateAmount(getHostCard(), rightString, this);
right = Integer.parseInt(rightString);
} catch (final NumberFormatException nfe) {
right = CardFactoryUtil.xCount(this.getHostCard(), this.getHostCard().getSVar(rightString));
}
final int left = list.size(); final int left = list.size();
if (!Expressions.compare(left, presentCompare, right)) { if (!Expressions.compare(left, presentCompare, right)) {

View File

@@ -108,14 +108,9 @@ public class ForgeScript {
} }
return false; return false;
} else if (property.startsWith("cmc")) { } else if (property.startsWith("cmc")) {
int x;
String rhs = property.substring(5); String rhs = property.substring(5);
int y = cardState.getManaCost().getCMC(); int y = cardState.getManaCost().getCMC();
try { int x = AbilityUtils.calculateAmount(source, rhs, spellAbility);
x = Integer.parseInt(rhs);
} catch (final NumberFormatException e) {
x = AbilityUtils.calculateAmount(source, rhs, spellAbility);
}
return Expressions.compare(y, property, x); return Expressions.compare(y, property, x);
} else return cardState.getTypeWithChanges().hasStringType(property); } else return cardState.getTypeWithChanges().hasStringType(property);

View File

@@ -586,14 +586,8 @@ public final class GameActionUtil {
// Mark SAs with subAbilities as undoable. These are generally things like damage, and other stuff // Mark SAs with subAbilities as undoable. These are generally things like damage, and other stuff
// that's hard to track and remove // that's hard to track and remove
sa.setUndoable(false); sa.setUndoable(false);
} else { } else if ((sa.getParam("Amount") != null) && (amount != AbilityUtils.calculateAmount(sa.getHostCard(),sa.getParam("Amount"), sa))) {
try { sa.setUndoable(false);
if ((sa.getParam("Amount") != null) && (amount != Integer.parseInt(sa.getParam("Amount")))) {
sa.setUndoable(false);
}
} catch (final NumberFormatException n) {
sa.setUndoable(false);
}
} }
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();

View File

@@ -13,6 +13,8 @@ import forge.util.Localizer;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import org.apache.commons.lang3.StringUtils;
public class ManaReflectedEffect extends SpellAbilityEffect { public class ManaReflectedEffect extends SpellAbilityEffect {
/* (non-Javadoc) /* (non-Javadoc)
@@ -98,12 +100,12 @@ public class ManaReflectedEffect extends SpellAbilityEffect {
if (amount == 0) { if (amount == 0) {
sb.append("0"); sb.append("0");
} else { } else {
try { if (StringUtils.isNumeric(baseMana)) {
// if baseMana is an integer(colorless), just multiply amount // if baseMana is an integer(colorless), just multiply amount
// and baseMana // and baseMana
final int base = Integer.parseInt(baseMana); final int base = Integer.parseInt(baseMana);
sb.append(base * amount); sb.append(base * amount);
} catch (final NumberFormatException e) { } else {
for (int i = 0; i < amount; i++) { for (int i = 0; i < amount; i++) {
if (i != 0) { if (i != 0) {
sb.append(" "); sb.append(" ");

View File

@@ -5,7 +5,6 @@ import forge.game.ability.AbilityUtils;
import forge.game.ability.SpellAbilityEffect; import forge.game.ability.SpellAbilityEffect;
import forge.game.card.Card; import forge.game.card.Card;
import forge.game.card.CardCollectionView; import forge.game.card.CardCollectionView;
import forge.game.card.CardFactoryUtil;
import forge.game.card.CardLists; import forge.game.card.CardLists;
import forge.game.player.Player; import forge.game.player.Player;
import forge.game.spellability.AbilitySub; import forge.game.spellability.AbilitySub;
@@ -94,15 +93,8 @@ public class RepeatEffect extends SpellAbilityEffect {
} }
list = CardLists.getValidCards(list, repeatPresent.split(","), sa.getActivatingPlayer(), sa.getHostCard(), sa); list = CardLists.getValidCards(list, repeatPresent.split(","), sa.getActivatingPlayer(), sa.getHostCard(), sa);
int right;
final String rightString = repeatCompare.substring(2); final String rightString = repeatCompare.substring(2);
try { // If this is an Integer, just parse it int right = AbilityUtils.calculateAmount(sa.getHostCard(), rightString, sa);
right = Integer.parseInt(rightString);
} catch (final NumberFormatException e) { // Otherwise, grab it from
// the
// SVar
right = CardFactoryUtil.xCount(sa.getHostCard(), sa.getHostCard().getSVar(rightString));
}
final int left = list.size(); final int left = list.size();

View File

@@ -1413,11 +1413,7 @@ public class CardProperty {
rhs = property.substring(10); rhs = property.substring(10);
y = card.getNetPower() + card.getNetToughness(); y = card.getNetPower() + card.getNetToughness();
} }
try { x = AbilityUtils.calculateAmount(source, rhs, spellAbility);
x = Integer.parseInt(rhs);
} catch (final NumberFormatException e) {
x = AbilityUtils.calculateAmount(source, rhs, spellAbility);
}
if (!Expressions.compare(y, property, x)) { if (!Expressions.compare(y, property, x)) {
return false; return false;
@@ -1443,16 +1439,11 @@ public class CardProperty {
// TODO get a working regex out of this pattern so the amount of // TODO get a working regex out of this pattern so the amount of
// digits doesn't matter // digits doesn't matter
int number;
final String[] splitProperty = property.split("_"); final String[] splitProperty = property.split("_");
final String strNum = splitProperty[1].substring(2); final String strNum = splitProperty[1].substring(2);
final String comparator = splitProperty[1].substring(0, 2); final String comparator = splitProperty[1].substring(0, 2);
String counterType; String counterType;
try { int number = AbilityUtils.calculateAmount(source, strNum, spellAbility);
number = Integer.parseInt(strNum);
} catch (final NumberFormatException e) {
number = CardFactoryUtil.xCount(source, source.getSVar(strNum));
}
counterType = splitProperty[2]; counterType = splitProperty[2];
final int actualnumber = card.getCounters(CounterType.getType(counterType)); final int actualnumber = card.getCounters(CounterType.getType(counterType));

View File

@@ -20,7 +20,6 @@ package forge.game.replacement;
import forge.game.ability.AbilityKey; import forge.game.ability.AbilityKey;
import forge.game.ability.AbilityUtils; import forge.game.ability.AbilityUtils;
import forge.game.card.Card; import forge.game.card.Card;
import forge.game.card.CardFactoryUtil;
import forge.game.player.Player; import forge.game.player.Player;
import forge.game.spellability.SpellAbility; import forge.game.spellability.SpellAbility;
import forge.util.Expressions; import forge.util.Expressions;
@@ -97,12 +96,7 @@ public class ReplaceDamage extends ReplacementEffect {
String full = getParam("DamageAmount"); String full = getParam("DamageAmount");
String operator = full.substring(0, 2); String operator = full.substring(0, 2);
String operand = full.substring(2); String operand = full.substring(2);
int intoperand = 0; int intoperand = AbilityUtils.calculateAmount(getHostCard(), operand, this);
try {
intoperand = Integer.parseInt(operand);
} catch (NumberFormatException e) {
intoperand = CardFactoryUtil.xCount(getHostCard(), getHostCard().getSVar(operand));
}
if (!Expressions.compare((Integer) runParams.get(AbilityKey.DamageAmount), operator, intoperand)) { if (!Expressions.compare((Integer) runParams.get(AbilityKey.DamageAmount), operator, intoperand)) {
return false; return false;

View File

@@ -1,8 +1,8 @@
package forge.game.replacement; package forge.game.replacement;
import forge.game.ability.AbilityKey; import forge.game.ability.AbilityKey;
import forge.game.ability.AbilityUtils;
import forge.game.card.Card; import forge.game.card.Card;
import forge.game.card.CardFactoryUtil;
import forge.game.spellability.SpellAbility; import forge.game.spellability.SpellAbility;
import forge.util.Expressions; import forge.util.Expressions;
@@ -43,12 +43,7 @@ public class ReplaceProduceMana extends ReplacementEffect {
String full = getParam("ManaAmount"); String full = getParam("ManaAmount");
String operator = full.substring(0, 2); String operator = full.substring(0, 2);
String operand = full.substring(2); String operand = full.substring(2);
int intoperand = 0; int intoperand = AbilityUtils.calculateAmount(getHostCard(), operand, this);
try {
intoperand = Integer.parseInt(operand);
} catch (NumberFormatException e) {
intoperand = CardFactoryUtil.xCount(getHostCard(), getHostCard().getSVar(operand));
}
int manaAmount = StringUtils.countMatches((String) runParams.get(AbilityKey.Mana), " ") + 1; int manaAmount = StringUtils.countMatches((String) runParams.get(AbilityKey.Mana), " ") + 1;
if (!Expressions.compare(manaAmount, operator, intoperand)) { if (!Expressions.compare(manaAmount, operator, intoperand)) {
return false; return false;

View File

@@ -347,15 +347,8 @@ public class SpellAbilityCondition extends SpellAbilityVariables {
list = CardLists.getValidCards(list, this.getIsPresent().split(","), sa.getActivatingPlayer(), sa.getHostCard(), sa); list = CardLists.getValidCards(list, this.getIsPresent().split(","), sa.getActivatingPlayer(), sa.getHostCard(), sa);
int right;
final String rightString = this.getPresentCompare().substring(2); final String rightString = this.getPresentCompare().substring(2);
try { // If this is an Integer, just parse it int right = AbilityUtils.calculateAmount(host, rightString, sa);
right = Integer.parseInt(rightString);
} catch (final NumberFormatException e) { // Otherwise, grab it from
// the
// SVar
right = CardFactoryUtil.xCount(host, host.getSVar(rightString));
}
final int left = list.size(); final int left = list.size();

View File

@@ -194,7 +194,6 @@ public class SpellAbilityStackInstance implements IIdentifiable, IHasCardView {
// Saved sub-SA needs to be reset on the way out // Saved sub-SA needs to be reset on the way out
if (subInstance != null) { if (subInstance != null) {
ability.setSubAbility((AbilitySub) subInstance.getSpellAbility(true)); ability.setSubAbility((AbilitySub) subInstance.getSpellAbility(true));
ability.getSubAbility().setParent(ability);
} }
// Set Cost specific things here // Set Cost specific things here

View File

@@ -536,10 +536,10 @@ public abstract class Trigger extends TriggerReplacementBase {
} }
super.changeText(); super.changeText();
ensureAbility(); SpellAbility sa = ensureAbility();
if (getOverridingAbility() != null) { if (sa != null) {
getOverridingAbility().changeText(); sa.changeText();
} }
} }
@@ -553,14 +553,14 @@ public abstract class Trigger extends TriggerReplacementBase {
} }
super.changeTextIntrinsic(colorMap, typeMap); super.changeTextIntrinsic(colorMap, typeMap);
ensureAbility(); SpellAbility sa = ensureAbility();
if (getOverridingAbility() != null) { if (sa != null) {
getOverridingAbility().changeTextIntrinsic(colorMap, typeMap); sa.changeTextIntrinsic(colorMap, typeMap);
} }
} }
private SpellAbility ensureAbility() { public SpellAbility ensureAbility() {
SpellAbility sa = getOverridingAbility(); SpellAbility sa = getOverridingAbility();
if (sa == null && hasParam("Execute")) { if (sa == null && hasParam("Execute")) {
sa = AbilityFactory.getAbility(getHostCard(), getParam("Execute")); sa = AbilityFactory.getAbility(getHostCard(), getParam("Execute"));

View File

@@ -148,17 +148,11 @@ public class TriggerChangesZone extends Trigger {
return false; return false;
} }
final Card card; final Card card = (Card) runParams.get(AbilityKey.Card);
final int rightSide;
try {
card = (Card) runParams.get(AbilityKey.Card);
rightSide = Integer.parseInt(cond.substring(2));
} catch (NumberFormatException | ClassCastException e) {
return false;
}
if (card == null) { if (card == null) {
return false; return false;
} }
final int rightSide = AbilityUtils.calculateAmount(getHostCard(), cond.substring(2), this);
// need to check the ChangeZone LKI copy for damage, otherwise it'll return 0 for a new object in the new zone // need to check the ChangeZone LKI copy for damage, otherwise it'll return 0 for a new object in the new zone
Card lkiCard = card.getGame().getChangeZoneLKIInfo(card); Card lkiCard = card.getGame().getChangeZoneLKIInfo(card);