Merge pull request #4225 from Zzooouhh/master

Implemented cards, overflow check & other stuff
This commit is contained in:
Zzooouhh 2017-12-24 00:17:08 +01:00 committed by GitHub
commit e6fa563919
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
81 changed files with 4910 additions and 86 deletions

View file

@ -0,0 +1,67 @@
/*
* Copyright 2011 BetaSteward_at_googlemail.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package mage.abilities.common;
import mage.abilities.MageSingleton;
import mage.abilities.StaticAbility;
import java.io.ObjectStreamException;
import mage.constants.AbilityType;
import mage.constants.Zone;
/**
* @author L_J
*/
public class ControllerDivideCombatDamageAbility extends StaticAbility implements MageSingleton {
private static final ControllerDivideCombatDamageAbility instance = new ControllerDivideCombatDamageAbility();
private Object readResolve() throws ObjectStreamException {
return instance;
}
public static ControllerDivideCombatDamageAbility getInstance() {
return instance;
}
private ControllerDivideCombatDamageAbility() {
super(AbilityType.STATIC, Zone.BATTLEFIELD);
}
@Override
public String getRule() {
return "You may assign {this}'s combat damage divided as you choose among defending player and/or any number of creatures he or she controls.";
}
@Override
public ControllerDivideCombatDamageAbility copy() {
return instance;
}
}

View file

@ -0,0 +1,172 @@
/*
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package mage.abilities.effects.common;
import mage.MageObject;
import mage.abilities.Ability;
import mage.abilities.Mode;
import mage.abilities.costs.Cost;
import mage.abilities.costs.mana.GenericManaCost;
import mage.abilities.dynamicvalue.DynamicValue;
import mage.abilities.effects.ContinuousEffect;
import mage.abilities.effects.Effect;
import mage.abilities.effects.Effects;
import mage.abilities.effects.OneShotEffect;
import mage.constants.Outcome;
import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.players.Player;
import mage.util.CardUtil;
/**
*
* @author MarcoMarin & L_J
*/
public class DoUnlessTargetPlayerOrTargetsControllerPaysEffect extends OneShotEffect {
protected Effects executingEffects = new Effects();
private Effect otherwiseEffect;
private Cost cost;
private DynamicValue genericMana;
private String chooseUseText;
public DoUnlessTargetPlayerOrTargetsControllerPaysEffect(Effect effect, Cost cost) {
this(effect, cost, null);
}
public DoUnlessTargetPlayerOrTargetsControllerPaysEffect(Effect effect, Cost cost, String chooseUseText) {
this(effect, null, cost, chooseUseText);
}
public DoUnlessTargetPlayerOrTargetsControllerPaysEffect(Effect effect, Effect otherwiseEffect, Cost cost, String chooseUseText) {
super(Outcome.Detriment);
this.executingEffects.add(effect);
this.otherwiseEffect = otherwiseEffect;
this.cost = cost;
this.chooseUseText = chooseUseText;
}
public DoUnlessTargetPlayerOrTargetsControllerPaysEffect(Effect effect, DynamicValue genericMana) {
super(Outcome.Detriment);
this.executingEffects.add(effect);
this.genericMana = genericMana;
}
public DoUnlessTargetPlayerOrTargetsControllerPaysEffect(final DoUnlessTargetPlayerOrTargetsControllerPaysEffect effect) {
super(effect);
this.executingEffects = effect.executingEffects.copy();
this.otherwiseEffect = effect.otherwiseEffect;
if (effect.cost != null) {
this.cost = effect.cost.copy();
}
if (effect.genericMana != null) {
this.genericMana = effect.genericMana.copy();
}
this.chooseUseText = effect.chooseUseText;
}
public void addEffect(Effect effect) {
executingEffects.add(effect);
}
@Override
public boolean apply(Game game, Ability source) {
Player targetController = game.getPlayer(this.getTargetPointer().getFirst(game, source));
Permanent targetPermanent = game.getPermanentOrLKIBattlefield(this.getTargetPointer().getFirst(game, source));
if (targetPermanent != null) {
targetController = game.getPlayer(targetPermanent.getControllerId());
}
if (targetController != null) {
MageObject sourceObject = game.getObject(source.getSourceId());
if (targetController != null && sourceObject != null) {
Cost costToPay;
if (cost != null) {
costToPay = cost.copy();
} else {
costToPay = new GenericManaCost(genericMana.calculate(game, source, this));
}
String message;
if (chooseUseText == null) {
String effectText = executingEffects.getText(source.getModes().getMode());
message = "Pay " + costToPay.getText() + " to prevent (" + effectText.substring(0, effectText.length() - 1) + ")?";
} else {
message = chooseUseText;
}
message = CardUtil.replaceSourceName(message, sourceObject.getName());
boolean result = true;
boolean doEffect = true;
// check if targetController is willing to pay
if (costToPay.canPay(source, source.getSourceId(), targetController.getId(), game) && targetController.chooseUse(Outcome.Detriment, message, source, game)) {
costToPay.clearPaid();
if (costToPay.pay(source, game, source.getSourceId(), targetController.getId(), false, null)) {
if (!game.isSimulation()) {
game.informPlayers(targetController.getLogName() + " pays the cost to prevent the effect");
}
doEffect = false;
}
}
// do the effects if not paid
if (doEffect) {
for (Effect effect : executingEffects) {
effect.setTargetPointer(this.targetPointer);
if (effect instanceof OneShotEffect) {
result &= effect.apply(game, source);
} else {
game.addEffect((ContinuousEffect) effect, source);
}
}
} else if (otherwiseEffect != null) {
otherwiseEffect.setTargetPointer(this.targetPointer);
if (otherwiseEffect instanceof OneShotEffect) {
result &= otherwiseEffect.apply(game, source);
} else {
game.addEffect((ContinuousEffect) otherwiseEffect, source);
}
}
return result;
}
}
return false;
}
@Override
public String getText(Mode mode) {
if (!staticText.isEmpty()) {
return staticText;
}
String effectsText = executingEffects.getText(mode);
return effectsText.substring(0, effectsText.length() - 1) + " unless its controller pays " + (cost != null ? cost.getText() : "{X}");
}
@Override
public DoUnlessTargetPlayerOrTargetsControllerPaysEffect copy() {
return new DoUnlessTargetPlayerOrTargetsControllerPaysEffect(this);
}
}

View file

@ -3000,5 +3000,4 @@ public abstract class GameImpl implements Game, Serializable {
fireEvent(new GameEvent(GameEvent.EventType.BECOMES_MONARCH, monarchId, source == null ? null : source.getSourceId(), monarchId));
}
}
}

View file

@ -34,6 +34,7 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
import mage.abilities.common.ControllerAssignCombatDamageToBlockersAbility;
import mage.abilities.common.ControllerDivideCombatDamageAbility;
import mage.abilities.common.DamageAsThoughNotBlockedAbility;
import mage.abilities.keyword.CantBlockAloneAbility;
import mage.abilities.keyword.DeathtouchAbility;
@ -41,6 +42,7 @@ import mage.abilities.keyword.DoubleStrikeAbility;
import mage.abilities.keyword.FirstStrikeAbility;
import mage.abilities.keyword.TrampleAbility;
import mage.constants.Outcome;
import mage.filter.StaticFilters;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.permanent.Permanent;
@ -140,21 +142,24 @@ public class CombatGroup implements Serializable, Copyable<CombatGroup> {
public void assignDamageToBlockers(boolean first, Game game) {
if (!attackers.isEmpty() && (!first || hasFirstOrDoubleStrike(game))) {
if (blockers.isEmpty()) {
unblockedDamage(first, game);
} else {
Permanent attacker = game.getPermanent(attackers.get(0));
if (attacker.getAbilities().containsKey(DamageAsThoughNotBlockedAbility.getInstance().getId())) {
Player player = game.getPlayer(attacker.getControllerId());
if (player.chooseUse(Outcome.Damage, "Do you wish to assign damage for " + attacker.getLogName() + " as though it weren't blocked?", null, game)) {
blocked = false;
unblockedDamage(first, game);
}
}
if (blockers.size() == 1) {
singleBlockerDamage(first, game);
Permanent attacker = game.getPermanent(attackers.get(0));
if (!assignsDefendingPlayerAndOrDefendingCreaturesDividedDamage(attacker, attacker.getControllerId(), first, game, true)) {
if (blockers.isEmpty()) {
unblockedDamage(first, game);
return;
} else {
multiBlockerDamage(first, game);
Player player = game.getPlayer(defenderControlsDefensiveFormation(game) ? defendingPlayerId : attacker.getControllerId());
if (attacker.getAbilities().containsKey(DamageAsThoughNotBlockedAbility.getInstance().getId())) { // for handling creatures like Thorn Elemental
if (player.chooseUse(Outcome.Damage, "Do you wish to assign damage for " + attacker.getLogName() + " as though it weren't blocked?", null, game)) {
blocked = false;
unblockedDamage(first, game);
}
}
if (blockers.size() == 1) {
singleBlockerDamage(player, first, game);
} else {
multiBlockerDamage(player, first, game);
}
}
}
}
@ -162,6 +167,18 @@ public class CombatGroup implements Serializable, Copyable<CombatGroup> {
public void assignDamageToAttackers(boolean first, Game game) {
if (!blockers.isEmpty() && (!first || hasFirstOrDoubleStrike(game))) {
// this should only come up if Butcher Orgg is granted the ability to block multiple blockers
boolean altDamageMethod = false;
for (UUID blockerId : blockers) {
Permanent blocker = game.getPermanent(blockerId);
if (assignsDefendingPlayerAndOrDefendingCreaturesDividedDamage(blocker, blocker.getControllerId(), first, game, false)) {
altDamageMethod = true;
}
}
if (altDamageMethod) {
// this could be necessary to remake in the future (banding with Butcher Orgg?)
return;
}
if (attackers.size() == 1) {
singleAttackerDamage(first, game);
} else {
@ -226,7 +243,7 @@ public class CombatGroup implements Serializable, Copyable<CombatGroup> {
}
}
private void singleBlockerDamage(boolean first, Game game) {
private void singleBlockerDamage(Player player, boolean first, Game game) {
//TODO: handle banding
Permanent blocker = game.getPermanent(blockers.get(0));
Permanent attacker = game.getPermanent(attackers.get(0));
@ -244,13 +261,6 @@ public class CombatGroup implements Serializable, Copyable<CombatGroup> {
if (lethalDamage >= damage) {
blocker.markDamage(damage, attacker.getId(), game, true, true);
} else {
Player player = game.getPlayer(attacker.getControllerId());
for (Permanent defensiveFormation : game.getBattlefield().getAllActivePermanents(defendingPlayerId)) { // for handling Defensive Formation
if (defensiveFormation.getAbilities().containsKey(ControllerAssignCombatDamageToBlockersAbility.getInstance().getId())) {
player = game.getPlayer(defendingPlayerId);
break;
}
}
int damageAssigned = player.getAmount(lethalDamage, damage, "Assign damage to " + blocker.getName(), game);
blocker.markDamage(damageAssigned, attacker.getId(), game, true, true);
damage -= damageAssigned;
@ -264,27 +274,21 @@ public class CombatGroup implements Serializable, Copyable<CombatGroup> {
}
if (canDamage(blocker, first)) {
if (blocker.getBlocking() == 1) { // blocking several creatures handled separately
attacker.markDamage(blockerDamage, blocker.getId(), game, true, true);
if (!assignsDefendingPlayerAndOrDefendingCreaturesDividedDamage(blocker, blocker.getControllerId(), first, game, false)) {
attacker.markDamage(blockerDamage, blocker.getId(), game, true, true);
}
}
}
}
}
private void multiBlockerDamage(boolean first, Game game) {
private void multiBlockerDamage(Player player, boolean first, Game game) {
//TODO: handle banding
Permanent attacker = game.getPermanent(attackers.get(0));
if (attacker == null) {
return;
}
boolean oldRuleDamage = false;
Player player = game.getPlayer(attacker.getControllerId());
for (Permanent defensiveFormation : game.getBattlefield().getAllActivePermanents(defendingPlayerId)) { // for handling Defensive Formation
if (defensiveFormation.getAbilities().containsKey(ControllerAssignCombatDamageToBlockersAbility.getInstance().getId())) {
player = game.getPlayer(defendingPlayerId);
oldRuleDamage = true;
break;
}
}
boolean oldRuleDamage = (player.getId() == defendingPlayerId);
int damage = getDamageValueFromPermanent(attacker, game);
if (canDamage(attacker, first)) {
// must be set before attacker damage marking because of effects like Test of Faith
@ -310,9 +314,13 @@ public class CombatGroup implements Serializable, Copyable<CombatGroup> {
lethalDamage = blocker.getToughness().getValue() - blocker.getDamage();
}
if (lethalDamage >= damage) {
assigned.put(blockerId, damage);
damage = 0;
break;
if (!oldRuleDamage) {
assigned.put(blockerId, damage);
damage = 0;
break;
} else if (damage == 0) {
break;
}
}
int damageAssigned = 0;
if (!oldRuleDamage) {
@ -337,7 +345,11 @@ public class CombatGroup implements Serializable, Copyable<CombatGroup> {
for (UUID blockerId : blockerOrder) {
Integer power = blockerPower.get(blockerId);
if (power != null) {
attacker.markDamage(power, blockerId, game, true, true);
// might be missing canDamage condition?
Permanent blocker = game.getPermanent(blockerId);
if (!assignsDefendingPlayerAndOrDefendingCreaturesDividedDamage(blocker, blocker.getControllerId(), first, game, false)) {
attacker.markDamage(power, blockerId, game, true, true);
}
}
}
for (Map.Entry<UUID, Integer> entry : assigned.entrySet()) {
@ -348,7 +360,76 @@ public class CombatGroup implements Serializable, Copyable<CombatGroup> {
for (UUID blockerId : blockerOrder) {
Permanent blocker = game.getPermanent(blockerId);
if (canDamage(blocker, first)) {
attacker.markDamage(getDamageValueFromPermanent(blocker, game), blocker.getId(), game, true, true);
if (!assignsDefendingPlayerAndOrDefendingCreaturesDividedDamage(blocker, blocker.getControllerId(), first, game, false)) {
attacker.markDamage(getDamageValueFromPermanent(blocker, game), blocker.getId(), game, true, true);
}
}
}
}
}
private void defendingPlayerAndOrDefendingCreaturesDividedDamage(Permanent attacker, Player player, boolean first, Game game, boolean isAttacking) {
// for handling Butcher Orgg
if (!((blocked && blockers.isEmpty() && isAttacking) || (attackers.isEmpty() && !isAttacking))) {
if (attacker == null) {
return;
}
int damage = getDamageValueFromPermanent(attacker, game);
if (canDamage(attacker, first)) {
// must be set before attacker damage marking because of effects like Test of Faith
Map<UUID, Integer> blockerPower = new HashMap<>();
for (UUID blockerId : blockerOrder) {
Permanent blocker = game.getPermanent(blockerId);
if (canDamage(blocker, first)) {
if (blocker.getBlocking() == 1) { // blocking several creatures handled separately
blockerPower.put(blockerId, getDamageValueFromPermanent(blocker, game));
}
}
}
Map<UUID, Integer> assigned = new HashMap<>();
for (Permanent defendingCreature : game.getBattlefield().getAllActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, defendingPlayerId, game)) {
if (defendingCreature != null) {
if (!(damage > 0)) {
break;
}
int damageAssigned = 0;
damageAssigned = player.getAmount(0, damage, "Assign damage to " + defendingCreature.getName(), game);
assigned.put(defendingCreature.getId(), damageAssigned);
damage -= damageAssigned;
}
}
if (damage > 0) {
Player defendingPlayer = game.getPlayer(defendingPlayerId);
if (defendingPlayer.isInGame()) {
defendingPlayer.damage(damage, attacker.getId(), game, true, true);
}
}
if (isAttacking) {
for (UUID blockerId : blockerOrder) {
Integer power = blockerPower.get(blockerId);
if (power != null) {
// might be missing canDamage condition?
Permanent blocker = game.getPermanent(blockerId);
if (!assignsDefendingPlayerAndOrDefendingCreaturesDividedDamage(blocker, blocker.getControllerId(), first, game, false)) {
attacker.markDamage(power, blockerId, game, true, true);
}
}
}
}
for (Map.Entry<UUID, Integer> entry : assigned.entrySet()) {
Permanent defendingCreature = game.getPermanent(entry.getKey());
defendingCreature.markDamage(entry.getValue(), attacker.getId(), game, true, true);
}
} else {
if (isAttacking) {
for (UUID blockerId : blockerOrder) {
Permanent blocker = game.getPermanent(blockerId);
if (canDamage(blocker, first)) {
if (!assignsDefendingPlayerAndOrDefendingCreaturesDividedDamage(blocker, blocker.getControllerId(), first, game, false)) {
attacker.markDamage(getDamageValueFromPermanent(blocker, game), blocker.getId(), game, true, true);
}
}
}
}
}
}
@ -491,13 +572,7 @@ public class CombatGroup implements Serializable, Copyable<CombatGroup> {
if (blockers.isEmpty()) {
return;
}
Player player = game.getPlayer(playerId);
for (Permanent defensiveFormation : game.getBattlefield().getAllActivePermanents(defendingPlayerId)) { // for handling Defensive Formation
if (defensiveFormation.getAbilities().containsKey(ControllerAssignCombatDamageToBlockersAbility.getInstance().getId())) {
player = game.getPlayer(defendingPlayerId);
break;
}
}
Player player = game.getPlayer(defenderControlsDefensiveFormation(game) ? defendingPlayerId : playerId);
List<UUID> blockerList = new ArrayList<>(blockers);
blockerOrder.clear();
while (player.canRespond()) {
@ -720,4 +795,30 @@ public class CombatGroup implements Serializable, Copyable<CombatGroup> {
}
return false;
}
public boolean defenderControlsDefensiveFormation(Game game) {
// for handling Defensive Formation
for (Permanent defensiveFormation : game.getBattlefield().getAllActivePermanents(defendingPlayerId)) {
if (defensiveFormation.getAbilities().containsKey(ControllerAssignCombatDamageToBlockersAbility.getInstance().getId())) {
return true;
}
}
return false;
}
public boolean assignsDefendingPlayerAndOrDefendingCreaturesDividedDamage(Permanent creature, UUID playerId, boolean first, Game game, boolean isAttacking) {
// for handling Butcher Orgg
if (creature.getAbilities().containsKey(ControllerDivideCombatDamageAbility.getInstance().getId())) {
Player player = game.getPlayer(defenderControlsDefensiveFormation(game) ? defendingPlayerId : playerId);
// 10/4/2004 If it is blocked but then all of its blockers are removed before combat damage is assigned, then it wont be able to deal combat damage and you wont be able to use its ability.
// (same principle should apply if it's blocking and its blocked attacker is removed from combat)
if (!((blocked && blockers.isEmpty() && isAttacking) || (attackers.isEmpty() && !isAttacking)) && canDamage(creature, first)) {
if (player.chooseUse(Outcome.Damage, "Do you wish to assign " + creature.getLogName() + "'s combat damage divided among defending player and/or any number of defending creatures?", null, game)) {
defendingPlayerAndOrDefendingCreaturesDividedDamage(creature, player, first, game, isAttacking);
return true;
}
}
}
return false;
}
}

View file

@ -56,6 +56,7 @@ import mage.game.permanent.token.SquirrelToken;
import mage.game.stack.Spell;
import mage.game.stack.StackObject;
import mage.players.Player;
import mage.util.CardUtil;
import mage.util.GameLog;
import mage.util.ThreadLocalStringBuilder;
@ -887,7 +888,8 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
addCounters(CounterType.M1M1.createInstance(actualDamage), damageSourceAbility, game);
}
} else {
this.damage += actualDamage;
// this.damage += actualDamage;
this.damage = CardUtil.addWithOverflowCheck(this.damage, actualDamage);
}
game.fireEvent(new DamagedCreatureEvent(objectId, sourceId, controllerId, actualDamage, combat));
return actualDamage;

View file

@ -1759,7 +1759,8 @@ public abstract class PlayerImpl implements Player, Serializable {
}
GameEvent event = new GameEvent(GameEvent.EventType.LOSE_LIFE, playerId, playerId, playerId, amount, atCombat);
if (!game.replaceEvent(event)) {
this.life -= event.getAmount();
// this.life -= event.getAmount();
this.life = CardUtil.subtractWithOverflowCheck(this.life, event.getAmount());
if (!game.isSimulation()) {
game.informPlayers(this.getLogName() + " loses " + event.getAmount() + " life");
}
@ -1788,7 +1789,10 @@ public abstract class PlayerImpl implements Player, Serializable {
}
GameEvent event = new GameEvent(GameEvent.EventType.GAIN_LIFE, playerId, playerId, playerId, amount, false);
if (!game.replaceEvent(event)) {
this.life += event.getAmount();
// 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());
if (!game.isSimulation()) {
game.informPlayers(this.getLogName() + " gains " + event.getAmount() + " life");
}

View file

@ -509,4 +509,24 @@ public final class CardUtil {
}
}
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;
}
}