Refactor: overflow method naming

This commit is contained in:
Oleg Agafonov 2021-02-02 19:26:09 +04:00
parent 2393485320
commit b6f6bac5e0
51 changed files with 139 additions and 138 deletions

View file

@ -80,7 +80,7 @@ public class MageInt implements Serializable, Copyable<MageInt> {
}
public void boostValue(int amount) {
this.boostedValue = CardUtil.addWithOverflowCheck(this.boostedValue, amount);
this.boostedValue = CardUtil.overflowInc(this.boostedValue, amount);
}
public void resetToBaseValue() {

View file

@ -41,20 +41,20 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
}
protected void incrementAmount(ManaColor manaColor) {
this.amount = CardUtil.addWithOverflowCheck(this.amount, manaColor.amount);
this.snowAmount = CardUtil.addWithOverflowCheck(this.snowAmount, manaColor.snowAmount);
this.amount = CardUtil.overflowInc(this.amount, manaColor.amount);
this.snowAmount = CardUtil.overflowInc(this.snowAmount, manaColor.snowAmount);
}
protected void incrementAmount(int amount, boolean snow) {
this.amount = CardUtil.addWithOverflowCheck(this.amount, amount);
this.amount = CardUtil.overflowInc(this.amount, amount);
if (snow) {
this.snowAmount = CardUtil.addWithOverflowCheck(this.snowAmount, amount);
this.snowAmount = CardUtil.overflowInc(this.snowAmount, amount);
}
}
protected void removeAmount(ManaColor manaColor) {
this.amount = CardUtil.subtractWithOverflowCheck(this.amount, manaColor.amount);
this.snowAmount = CardUtil.subtractWithOverflowCheck(this.snowAmount, manaColor.snowAmount);
this.amount = CardUtil.overflowDec(this.amount, manaColor.amount);
this.snowAmount = CardUtil.overflowDec(this.snowAmount, manaColor.snowAmount);
}
protected void clear() {
@ -67,11 +67,11 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
return false;
}
if (manaColor.getSnowAmount() > 0) {
manaColor.snowAmount = CardUtil.subtractWithOverflowCheck(manaColor.snowAmount, 1);
this.snowAmount = CardUtil.addWithOverflowCheck(this.snowAmount, 1);
manaColor.snowAmount = CardUtil.overflowDec(manaColor.snowAmount, 1);
this.snowAmount = CardUtil.overflowInc(this.snowAmount, 1);
}
manaColor.amount = CardUtil.subtractWithOverflowCheck(manaColor.amount, 1);
this.amount = CardUtil.addWithOverflowCheck(this.amount, 1);
manaColor.amount = CardUtil.overflowDec(manaColor.amount, 1);
this.amount = CardUtil.overflowInc(this.amount, 1);
return true;
}
@ -597,25 +597,25 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
}
int count = 0;
if (filter.isWhite()) {
count = CardUtil.addWithOverflowCheck(count, white.getAmount());
count = CardUtil.overflowInc(count, white.getAmount());
}
if (filter.isBlue()) {
count = CardUtil.addWithOverflowCheck(count, blue.getAmount());
count = CardUtil.overflowInc(count, blue.getAmount());
}
if (filter.isBlack()) {
count = CardUtil.addWithOverflowCheck(count, black.getAmount());
count = CardUtil.overflowInc(count, black.getAmount());
}
if (filter.isRed()) {
count = CardUtil.addWithOverflowCheck(count, red.getAmount());
count = CardUtil.overflowInc(count, red.getAmount());
}
if (filter.isGreen()) {
count = CardUtil.addWithOverflowCheck(count, green.getAmount());
count = CardUtil.overflowInc(count, green.getAmount());
}
if (filter.isGeneric()) {
count = CardUtil.addWithOverflowCheck(count, generic.getAmount());
count = CardUtil.overflowInc(count, generic.getAmount());
}
if (filter.isColorless()) {
count = CardUtil.addWithOverflowCheck(count, colorless.getAmount());
count = CardUtil.overflowInc(count, colorless.getAmount());
}
return count;
}
@ -811,13 +811,13 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
}
if (compare.generic.getAmount() < 0) {
int remaining = 0;
remaining = CardUtil.addWithOverflowCheck(remaining, Math.min(0, compare.white.getAmount()));
remaining = CardUtil.addWithOverflowCheck(remaining, Math.min(0, compare.blue.getAmount()));
remaining = CardUtil.addWithOverflowCheck(remaining, Math.min(0, compare.black.getAmount()));
remaining = CardUtil.addWithOverflowCheck(remaining, Math.min(0, compare.red.getAmount()));
remaining = CardUtil.addWithOverflowCheck(remaining, Math.min(0, compare.green.getAmount()));
remaining = CardUtil.addWithOverflowCheck(remaining, Math.min(0, compare.colorless.getAmount()));
remaining = CardUtil.addWithOverflowCheck(remaining, Math.min(0, compare.any.getAmount()));
remaining = CardUtil.overflowInc(remaining, Math.min(0, compare.white.getAmount()));
remaining = CardUtil.overflowInc(remaining, Math.min(0, compare.blue.getAmount()));
remaining = CardUtil.overflowInc(remaining, Math.min(0, compare.black.getAmount()));
remaining = CardUtil.overflowInc(remaining, Math.min(0, compare.red.getAmount()));
remaining = CardUtil.overflowInc(remaining, Math.min(0, compare.green.getAmount()));
remaining = CardUtil.overflowInc(remaining, Math.min(0, compare.colorless.getAmount()));
remaining = CardUtil.overflowInc(remaining, Math.min(0, compare.any.getAmount()));
if (remaining > 0) {
int diff = Math.min(remaining, Math.abs(compare.generic.getAmount()));
compare.generic.incrementAmount(diff, false);
@ -1148,7 +1148,7 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
case GREEN:
return green.getAmount();
case COLORLESS:
return CardUtil.addWithOverflowCheck(generic.getAmount(), colorless.getAmount());
return CardUtil.overflowInc(generic.getAmount(), colorless.getAmount());
}
return 0;
}
@ -1246,7 +1246,7 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
&& this.green.getAmount() >= mana.green.getAmount()
&& this.colorless.getAmount() >= mana.colorless.getAmount()
&& (this.generic.getAmount() >= mana.generic.getAmount()
|| CardUtil.addWithOverflowCheck(this.countColored(), this.colorless.getAmount()) >= mana.count());
|| CardUtil.overflowInc(this.countColored(), this.colorless.getAmount()) >= mana.count());
}
@ -1282,33 +1282,33 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
moreMana = mana1;
lessMana = mana2;
}
int anyDiff = CardUtil.subtractWithOverflowCheck(mana2.getAny(), mana1.getAny());
int anyDiff = CardUtil.overflowDec(mana2.getAny(), mana1.getAny());
if (lessMana.getWhite() > moreMana.getWhite()) {
anyDiff = CardUtil.subtractWithOverflowCheck(anyDiff, CardUtil.subtractWithOverflowCheck(lessMana.getWhite(), moreMana.getWhite()));
anyDiff = CardUtil.overflowDec(anyDiff, CardUtil.overflowDec(lessMana.getWhite(), moreMana.getWhite()));
if (anyDiff < 0) {
return null;
}
}
if (lessMana.getRed() > moreMana.getRed()) {
anyDiff = CardUtil.subtractWithOverflowCheck(anyDiff, CardUtil.subtractWithOverflowCheck(lessMana.getRed(), moreMana.getRed()));
anyDiff = CardUtil.overflowDec(anyDiff, CardUtil.overflowDec(lessMana.getRed(), moreMana.getRed()));
if (anyDiff < 0) {
return null;
}
}
if (lessMana.getGreen() > moreMana.getGreen()) {
anyDiff = CardUtil.subtractWithOverflowCheck(anyDiff, CardUtil.subtractWithOverflowCheck(lessMana.getGreen(), moreMana.getGreen()));
anyDiff = CardUtil.overflowDec(anyDiff, CardUtil.overflowDec(lessMana.getGreen(), moreMana.getGreen()));
if (anyDiff < 0) {
return null;
}
}
if (lessMana.getBlue() > moreMana.getBlue()) {
anyDiff = CardUtil.subtractWithOverflowCheck(anyDiff, CardUtil.subtractWithOverflowCheck(lessMana.getBlue(), moreMana.getBlue()));
anyDiff = CardUtil.overflowDec(anyDiff, CardUtil.overflowDec(lessMana.getBlue(), moreMana.getBlue()));
if (anyDiff < 0) {
return null;
}
}
if (lessMana.getBlack() > moreMana.getBlack()) {
anyDiff = CardUtil.subtractWithOverflowCheck(anyDiff, CardUtil.subtractWithOverflowCheck(lessMana.getBlack(), moreMana.getBlack()));
anyDiff = CardUtil.overflowDec(anyDiff, CardUtil.overflowDec(lessMana.getBlack(), moreMana.getBlack()));
if (anyDiff < 0) {
return null;
}

View file

@ -58,7 +58,7 @@ public class AddConditionalManaEffect extends ManaEffect {
if (amountAvailableMana > 0) {
Mana calculatedMana = mana.copy();
for (ManaType manaType : ManaType.getTrueManaTypes()) {
calculatedMana.set(manaType, CardUtil.multiplyWithOverflowCheck(calculatedMana.get(manaType), amountAvailableMana));
calculatedMana.set(manaType, CardUtil.overflowMultiply(calculatedMana.get(manaType), amountAvailableMana));
}
maxAvailableMana.add(manaBuilder.setMana(calculatedMana, source, game).build());
}

View file

@ -1026,7 +1026,7 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
addCounters(CounterType.M1M1.createInstance(actualDamage), game.getControllerId(attackerId), damageSourceAbility, game);
}
} else {
this.damage = CardUtil.addWithOverflowCheck(this.damage, actualDamage);
this.damage = CardUtil.overflowInc(this.damage, actualDamage);
}
DamagedEvent damagedEvent = new DamagedCreatureEvent(this.getId(), attackerId, this.getControllerId(), actualDamage, combat);
damagedEvent.setExcess(actualDamage - lethal);

View file

@ -388,7 +388,7 @@ public class ManaPool implements Serializable {
private void removeConditional(ConditionalManaInfo manaInfo, Ability ability, Game game, Cost costToPay, Mana usedManaToPay) {
for (ConditionalMana mana : getConditionalMana()) {
if (mana.get(manaInfo.manaType) > 0 && mana.apply(ability, game, mana.getManaProducerId(), costToPay)) {
mana.set(manaInfo.manaType, CardUtil.subtractWithOverflowCheck(mana.get(manaInfo.manaType), 1));
mana.set(manaInfo.manaType, CardUtil.overflowDec(mana.get(manaInfo.manaType), 1));
usedManaToPay.increase(manaInfo.manaType, manaInfo.sourceObject.isSnow());
GameEvent event = new ManaPaidEvent(ability, mana.getManaProducerId(), mana.getFlag(), mana.getManaProducerOriginalId());
game.fireEvent(event);

View file

@ -2004,7 +2004,7 @@ public abstract class PlayerImpl implements Player, Serializable {
GameEvent event = new GameEvent(GameEvent.EventType.LOSE_LIFE,
playerId, source, playerId, amount, atCombat);
if (!game.replaceEvent(event)) {
this.life = CardUtil.subtractWithOverflowCheck(this.life, event.getAmount());
this.life = CardUtil.overflowDec(this.life, event.getAmount());
if (!game.isSimulation()) {
UUID needId = attackerId;
if (needId == null) {
@ -2048,7 +2048,7 @@ public abstract class PlayerImpl implements Player, Serializable {
// TODO: lock life at Integer.MAX_VALUE if reached, until it's set to a different amount
// (https://magic.wizards.com/en/articles/archive/news/unstable-faqawaslfaqpaftidawabiajtbt-2017-12-06 - "infinite" life total stays infinite no matter how much is gained or lost)
// this.life += event.getAmount();
this.life = CardUtil.addWithOverflowCheck(this.life, event.getAmount());
this.life = CardUtil.overflowInc(this.life, event.getAmount());
if (!game.isSimulation()) {
game.informPlayers(this.getLogName() + " gains " + event.getAmount() + " life" + CardUtil.getSourceLogName(game, source));
}
@ -3244,7 +3244,7 @@ public abstract class PlayerImpl implements Player, Serializable {
restVal = 0;
availableLifeMana -= oldPayOption.get(manaType);
} else {
restVal = CardUtil.subtractWithOverflowCheck(oldPayOption.get(manaType), availableLifeMana);
restVal = CardUtil.overflowDec(oldPayOption.get(manaType), availableLifeMana);
availableLifeMana = 0;
}
manaCopy.set(manaType, restVal);

View file

@ -628,44 +628,49 @@ public final class CardUtil {
return "<font color = 'blue'>" + text + "</font>";
}
public static boolean cardCanBePlayedNow(Card card, UUID playerId, Game game) {
if (card.isLand()) {
return game.canPlaySorcery(playerId) && game.getPlayer(playerId).canPlayLand();
/**
* Integer operation with overflow protection
*
* @param base
* @param increment
* @return
*/
public static int overflowInc(int base, int increment) {
return overflowResult((long) base + increment);
}
/**
* Integer operation with overflow protection
*
* @param base
* @param decrement
* @return
*/
public static int overflowDec(int base, int decrement) {
return overflowResult((long) base - decrement);
}
/**
* Integer operation with overflow protection
*
* @param base
* @param multiply
* @return
*/
public static int overflowMultiply(int base, int multiply) {
return overflowResult((long) base * multiply);
}
private static int overflowResult(long value) {
if (value > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
} else if (value < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
} else {
return card.getSpellAbility() != null && card.getSpellAbility().spellCanBeActivatedRegularlyNow(playerId, game);
return (int) value;
}
}
public static int addWithOverflowCheck(int base, int increment) {
long result = ((long) base) + increment;
if (result > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
} else if (result < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
}
return base + increment;
}
public static int subtractWithOverflowCheck(int base, int decrement) {
long result = ((long) base) - decrement;
if (result > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
} else if (result < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
}
return base - decrement;
}
public static int multiplyWithOverflowCheck(int base, int multiply) {
long result = ((long) base) * multiply;
if (result > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
} else if (result < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
}
return base * multiply;
}
public static String createObjectRealtedWindowTitle(Ability source, Game game, String textSuffix) {
String title;
if (source != null) {