reworked/simplified/consolidated effects which exchange life totals, added test (fixes #7668)

This commit is contained in:
Evan Kranzler 2021-03-14 15:56:48 -04:00
parent 1abeec9595
commit d4792e3665
16 changed files with 284 additions and 274 deletions

View file

@ -0,0 +1,44 @@
package mage.abilities.effects.common;
import mage.abilities.Ability;
import mage.abilities.Mode;
import mage.abilities.effects.OneShotEffect;
import mage.constants.Outcome;
import mage.game.Game;
import mage.players.Player;
/**
* @author Styxo
*/
public class ExchangeLifeControllerTargetEffect extends OneShotEffect {
public ExchangeLifeControllerTargetEffect() {
super(Outcome.Neutral);
}
private ExchangeLifeControllerTargetEffect(final ExchangeLifeControllerTargetEffect effect) {
super(effect);
}
@Override
public ExchangeLifeControllerTargetEffect copy() {
return new ExchangeLifeControllerTargetEffect(this);
}
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
Player player = game.getPlayer(source.getFirstTarget());
if (controller == null || player == null) {
return false;
}
controller.exchangeLife(player, source, game);
return true;
}
@Override
public String getText(Mode mode) {
return "Exchange life totals with target " + mode.getTargets().get(0).getTargetName();
}
}

View file

@ -1,65 +0,0 @@
package mage.abilities.effects.common;
import mage.abilities.Ability;
import mage.abilities.Mode;
import mage.abilities.effects.OneShotEffect;
import mage.constants.Outcome;
import mage.game.Game;
import mage.players.Player;
/**
*
* @author Styxo
*/
public class ExchangeLifeTargetEffect extends OneShotEffect {
public ExchangeLifeTargetEffect() {
super(Outcome.Neutral);
}
public ExchangeLifeTargetEffect(final ExchangeLifeTargetEffect effect) {
super(effect);
}
@Override
public ExchangeLifeTargetEffect copy() {
return new ExchangeLifeTargetEffect(this);
}
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
Player player = game.getPlayer(source.getFirstTarget());
if (controller != null && player != null) {
int lifeController = controller.getLife();
int lifePlayer = player.getLife();
if (lifeController == lifePlayer) {
return false;
}
if (!controller.isLifeTotalCanChange() || !player.isLifeTotalCanChange()) {
return false;
}
if (lifeController < lifePlayer && (!controller.isCanGainLife() || !player.isCanLoseLife())) {
return false;
}
if (lifeController > lifePlayer && (!controller.isCanLoseLife() || !player.isCanGainLife())) {
return false;
}
controller.setLife(lifePlayer, game, source);
player.setLife(lifeController, game, source);
return true;
}
return false;
}
@Override
public String getText(Mode mode) {
return "Exchange life totals with target " + mode.getTargets().get(0).getTargetName();
}
}

View file

@ -0,0 +1,41 @@
package mage.abilities.effects.common;
import mage.abilities.Ability;
import mage.abilities.effects.OneShotEffect;
import mage.constants.Outcome;
import mage.game.Game;
import mage.players.Player;
/**
* @author TheElk801
*/
public class ExchangeLifeTwoTargetEffect extends OneShotEffect {
public ExchangeLifeTwoTargetEffect() {
super(Outcome.Neutral);
staticText = "two target players exchange life totals";
}
private ExchangeLifeTwoTargetEffect(final ExchangeLifeTwoTargetEffect effect) {
super(effect);
}
@Override
public ExchangeLifeTwoTargetEffect copy() {
return new ExchangeLifeTwoTargetEffect(this);
}
@Override
public boolean apply(Game game, Ability source) {
if (source.getTargets().get(0).getTargets().size() < 2) {
return false;
}
Player player1 = game.getPlayer(source.getTargets().get(0).getTargets().get(0));
Player player2 = game.getPlayer(source.getTargets().get(0).getTargets().get(1));
if (player1 == null || player2 == null) {
return false;
}
player1.exchangeLife(player2, source, game);
return true;
}
}

View file

@ -11,13 +11,13 @@ import java.util.Arrays;
*/
public final class PhyrexianRebirthHorrorToken extends TokenImpl {
public PhyrexianRebirthHorrorToken() {
public PhyrexianRebirthHorrorToken(int power, int toughness) {
super("Horror", "X/X colorless Horror artifact creature token");
cardType.add(CardType.ARTIFACT);
cardType.add(CardType.CREATURE);
subtype.add(SubType.HORROR);
power = new MageInt(0);
toughness = new MageInt(0);
this.cardType.add(CardType.ARTIFACT);
this.cardType.add(CardType.CREATURE);
this.subtype.add(SubType.HORROR);
this.power = new MageInt(power);
this.toughness = new MageInt(toughness);
availableImageSetCodes = Arrays.asList("C18", "C19", "MBS", "CMR");
}

View file

@ -50,9 +50,10 @@ public interface Player extends MageItem, Copyable<Player> {
/**
* Current player is real life player (human). Try to use in GUI and network engine only.
*
* <p>
* WARNING, you must use isComputer instead isHuman in card's code (for good Human/AI logic testing in unit tests)
* TODO: check combat code and other and replace isHuman to isComputer usage if possible (if AI support that actions)
*
* @return
*/
boolean isHuman();
@ -61,9 +62,9 @@ public interface Player extends MageItem, Copyable<Player> {
/**
* Current player is AI. Use it in card's code and all other places.
*
* <p>
* It help to split Human/AI logic and test both by unit tests.
*
* <p>
* Usage example: AI hint to skip or auto-calculate choices instead call of real choose dialogs
* - unit tests for Human logic: call normal commands
* - unit tests for AI logic: call aiXXX commands
@ -125,6 +126,8 @@ public interface Player extends MageItem, Copyable<Player> {
*/
int gainLife(int amount, Game game, Ability source);
void exchangeLife(Player player, Ability source, Game game);
int damage(int damage, UUID attackerId, Ability source, Game game);
int damage(int damage, UUID attackerId, Ability source, Game game, boolean combatDamage, boolean preventable);

View file

@ -2063,6 +2063,18 @@ public abstract class PlayerImpl implements Player, Serializable {
return 0;
}
@Override
public void exchangeLife(Player player, Ability source, Game game) {
int lifePlayer1 = getLife();
int lifePlayer2 = player.getLife();
if ((lifePlayer1 != lifePlayer2 && this.isLifeTotalCanChange() && player.isLifeTotalCanChange())
&& (lifePlayer1 >= lifePlayer2 || (this.isCanGainLife() && player.isCanLoseLife()))
&& (lifePlayer1 <= lifePlayer2 || (this.isCanLoseLife() && player.isCanGainLife()))) {
this.setLife(lifePlayer2, game, source);
player.setLife(lifePlayer1, game, source);
}
}
@Override
public int damage(int damage, UUID attackerId, Ability source, Game game) {
return doDamage(damage, attackerId, source, game, false, true, null);