From 12a2d020e7a4dad50239d1dcb917470e96375d4b Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Mon, 14 Jul 2014 09:02:06 +0200 Subject: [PATCH] * Changed handling of getting source object. Needed some change to adjust methods. --- Mage.Common/src/mage/view/GameView.java | 5 +++- Mage/src/mage/abilities/AbilityImpl.java | 30 +++++++++---------- Mage/src/mage/game/GameImpl.java | 25 +++++++++------- .../mage/game/permanent/PermanentCard.java | 16 ++++++++++ Mage/src/mage/game/stack/Spell.java | 19 ++++++++++-- Mage/src/mage/game/stack/StackAbility.java | 8 ++++- 6 files changed, 73 insertions(+), 30 deletions(-) diff --git a/Mage.Common/src/mage/view/GameView.java b/Mage.Common/src/mage/view/GameView.java index bdf6779a6b6..31c3e571a8a 100644 --- a/Mage.Common/src/mage/view/GameView.java +++ b/Mage.Common/src/mage/view/GameView.java @@ -48,6 +48,7 @@ import mage.players.Player; import java.io.Serializable; import java.util.*; +import org.apache.log4j.Logger; /** @@ -57,6 +58,8 @@ import java.util.*; public class GameView implements Serializable { private static final long serialVersionUID = 1L; + private static final transient Logger logger = Logger.getLogger(GameView.class); + private final int priorityTime; private final List players = new ArrayList<>(); private SimpleCardsView hand; @@ -121,7 +124,7 @@ public class GameView implements Serializable { checkPaid(stackObject.getId(), ((StackAbility)stackObject)); } } else { - + logger.error("Stack Object for stack ability not found: " + stackObject.toString()); } } else { diff --git a/Mage/src/mage/abilities/AbilityImpl.java b/Mage/src/mage/abilities/AbilityImpl.java index e9ddbbb07c6..f32feae3180 100644 --- a/Mage/src/mage/abilities/AbilityImpl.java +++ b/Mage/src/mage/abilities/AbilityImpl.java @@ -217,9 +217,9 @@ public abstract class AbilityImpl implements Ability { } - Card card = game.getCard(sourceId); - if (card != null) { - card.adjustChoices(this, game); + MageObject sourceObject = game.getObject(sourceId); + if (sourceObject != null) { + sourceObject.adjustChoices(this, game); } for (UUID modeId :this.getModes().getSelectedModes()) { this.getModes().setMode(this.getModes().get(modeId)); @@ -247,9 +247,9 @@ public abstract class AbilityImpl implements Ability { // as buyback, kicker, or convoke costs (see rules 117.8 and 117.9), the player announces his // or her intentions to pay any or all of those costs (see rule 601.2e). // A player can't apply two alternative methods of casting or two alternative costs to a single spell. - if (card != null && !(this instanceof FlashbackAbility)) { + if (sourceObject != null && !(this instanceof FlashbackAbility)) { boolean alternativeCostisUsed = false; - for (Ability ability : card.getAbilities()) { + for (Ability ability : sourceObject.getAbilities()) { // if cast for noMana no Alternative costs are allowed if (!noMana && ability instanceof AlternativeSourceCosts) { AlternativeSourceCosts alternativeSpellCosts = (AlternativeSourceCosts) ability; @@ -305,12 +305,12 @@ public abstract class AbilityImpl implements Ability { // and/or zones become the target of a spell trigger at this point; they'll wait to be put on // the stack until the spell has finished being cast.) - if (card != null) { - card.adjustTargets(this, game); + if (sourceObject != null) { + sourceObject.adjustTargets(this, game); } if (getTargets().size() > 0 && getTargets().chooseTargets(getEffects().get(0).getOutcome(), this.controllerId, this, game) == false) { if (variableManaCost != null || announceString != null) { - game.informPlayer(controller, new StringBuilder(card != null ? card.getName(): "").append(": no valid targets with this value of X").toString()); + game.informPlayer(controller, new StringBuilder(sourceObject != null ? sourceObject.getLogName(): "").append(": no valid targets with this value of X").toString()); } else { logger.debug("activate failed - target"); } @@ -328,9 +328,9 @@ public abstract class AbilityImpl implements Ability { } } //20100716 - 601.2e - if (card != null) { - card.adjustCosts(this, game); - for (Ability ability : card.getAbilities()) { + if (sourceObject != null) { + sourceObject.adjustCosts(this, game); + for (Ability ability : sourceObject.getAbilities()) { if (ability instanceof AdjustingSourceCosts) { ((AdjustingSourceCosts)ability).adjustCosts(this, game); } @@ -824,6 +824,9 @@ public abstract class AbilityImpl implements Ability { return ""; } MageObject object = game.getObject(this.sourceId); + if (object == null) { // e.g. sacrificed token + logger.warn("Could get no object: " + this.toString()); + } return new StringBuilder(" activates: ") .append(object != null ? this.formatRule(modes.getText(), object.getName()) :modes.getText()) .append(" from ") @@ -833,9 +836,6 @@ public abstract class AbilityImpl implements Ability { protected String getMessageText(Game game) { StringBuilder sb = new StringBuilder(); MageObject object = game.getObject(this.sourceId); - if (object == null) { - object = game.getLastKnownInformation(this.sourceId, Zone.BATTLEFIELD); - } if (object != null) { if (object instanceof StackAbility) { Card card = game.getCard(((StackAbility) object).getSourceId()); @@ -854,7 +854,7 @@ public abstract class AbilityImpl implements Ability { } sb.append(getOptionalTextSuffix(game, spell)); } else { - sb.append(object.getName()); + sb.append(object.getLogName()); } } } else { diff --git a/Mage/src/mage/game/GameImpl.java b/Mage/src/mage/game/GameImpl.java index b4bc62c2460..a4178c91484 100644 --- a/Mage/src/mage/game/GameImpl.java +++ b/Mage/src/mage/game/GameImpl.java @@ -284,7 +284,7 @@ public abstract class GameImpl implements Game, Serializable { return state.getPlayer(playerId); } - @Override + @Override public MageObject getObject(UUID objectId) { if (objectId == null) { return null; @@ -304,13 +304,13 @@ public abstract class GameImpl implements Game, Serializable { return item; } } - + for (CommandObject commandObject : state.getCommand()) { if (commandObject instanceof Commander && commandObject.getId().equals(objectId)) { return commandObject; } } - + object = getCard(objectId); if (object == null) { @@ -319,6 +319,8 @@ public abstract class GameImpl implements Game, Serializable { return commandObject; } } + // can be an ability of a sacrificed Token trying to get it's source object + object = getLastKnownInformation(objectId, Zone.BATTLEFIELD); } return object; @@ -415,8 +417,8 @@ public abstract class GameImpl implements Game, Serializable { } /** - * Starts check if game over or if playerId is given - * let the player concede. + * Starts check if game is over or + * if playerId is given let the player concede. * * @param playerId * @return @@ -817,24 +819,27 @@ public abstract class GameImpl implements Game, Serializable { } protected UUID findWinnersAndLosers() { - UUID winner = null; + logger.debug(new StringBuilder("GameImpl.findWinnersAndLosers start gameId ").append(this.getId())); + UUID winnerIdFound = null; for (Player player: state.getPlayers().values()) { if (player.hasWon()) { - winner = player.getId(); + logger.debug(new StringBuilder("GameImpl.findWinnersAndLosers playerHasWon ").append(player.getId())); + winnerIdFound = player.getId(); break; } if (!player.hasLost() && !player.hasLeft()) { + logger.debug(new StringBuilder("GameImpl.findWinnersAndLosers player ").append(player.getId())); player.won(this); - winner = player.getId(); + winnerIdFound = player.getId(); break; } } for (Player player: state.getPlayers().values()) { - if (winner != null && !player.getId().equals(winner) && !player.hasLost()) { + if (winnerIdFound != null && !player.getId().equals(winnerIdFound) && !player.hasLost()) { player.lost(this); } } - return winner; + return winnerIdFound; } protected void endOfTurn() { diff --git a/Mage/src/mage/game/permanent/PermanentCard.java b/Mage/src/mage/game/permanent/PermanentCard.java index a18916a29fd..eb7ae5fff46 100644 --- a/Mage/src/mage/game/permanent/PermanentCard.java +++ b/Mage/src/mage/game/permanent/PermanentCard.java @@ -30,6 +30,7 @@ package mage.game.permanent; import java.util.ArrayList; import java.util.UUID; +import mage.abilities.Ability; import mage.cards.Card; import mage.cards.LevelerCard; import mage.constants.Zone; @@ -222,5 +223,20 @@ public class PermanentCard extends PermanentImpl { } return false; } + + @Override + public void adjustTargets(Ability ability, Game game) { + card.adjustTargets(ability, game); + } + + @Override + public void adjustCosts(Ability ability, Game game) { + card.adjustCosts(ability, game); + } + + @Override + public void adjustChoices(Ability ability, Game game) { + card.adjustChoices(ability, game); + } } diff --git a/Mage/src/mage/game/stack/Spell.java b/Mage/src/mage/game/stack/Spell.java index cb4a34753af..df1fc8dea24 100644 --- a/Mage/src/mage/game/stack/Spell.java +++ b/Mage/src/mage/game/stack/Spell.java @@ -599,6 +599,7 @@ public class Spell implements StackObject, Card { * 202.3b When calculating the converted mana cost of an object with an {X} in its * mana cost, X is treated as 0 while the object is not on the stack, and X is * treated as the number chosen for it while the object is on the stack. + * @return */ public int getConvertedManaCost() { int cmc = 0; @@ -753,13 +754,25 @@ public class Spell implements StackObject, Card { } @Override - public void adjustChoices(Ability ability, Game game) {} + public void adjustChoices(Ability ability, Game game) { + if (card != null) { + card.adjustChoices(ability, game); + } + } @Override - public void adjustCosts(Ability ability, Game game) {} + public void adjustCosts(Ability ability, Game game) { + if (card != null) { + card.adjustCosts(ability, game); + } + } @Override - public void adjustTargets(Ability ability, Game game) {} + public void adjustTargets(Ability ability, Game game) { + if (card != null) { + card.adjustTargets(ability, game); + } + } @Override diff --git a/Mage/src/mage/game/stack/StackAbility.java b/Mage/src/mage/game/stack/StackAbility.java index 00d0ca0278b..2ed2bbd8371 100644 --- a/Mage/src/mage/game/stack/StackAbility.java +++ b/Mage/src/mage/game/stack/StackAbility.java @@ -54,6 +54,7 @@ import mage.target.Targets; import java.util.ArrayList; import java.util.List; import java.util.UUID; +import mage.cards.Card; import mage.constants.AbilityWord; /** @@ -323,7 +324,12 @@ public class StackAbility implements StackObject, Ability { public void adjustCosts(Ability ability, Game game) {} @Override - public void adjustTargets(Ability ability, Game game) {} + public void adjustTargets(Ability ability, Game game) { + Card card = game.getCard(ability.getSourceId()); + if (card != null) { + card.adjustTargets(ability, game); + } + } @Override public Costs getOptionalCosts() {