* Changed handling of getting source object. Needed some change to adjust methods.

This commit is contained in:
LevelX2 2014-07-14 09:02:06 +02:00
parent 7ca60078a0
commit 12a2d020e7
6 changed files with 73 additions and 30 deletions

View file

@ -48,6 +48,7 @@ import mage.players.Player;
import java.io.Serializable; import java.io.Serializable;
import java.util.*; import java.util.*;
import org.apache.log4j.Logger;
/** /**
@ -57,6 +58,8 @@ import java.util.*;
public class GameView implements Serializable { public class GameView implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private static final transient Logger logger = Logger.getLogger(GameView.class);
private final int priorityTime; private final int priorityTime;
private final List<PlayerView> players = new ArrayList<>(); private final List<PlayerView> players = new ArrayList<>();
private SimpleCardsView hand; private SimpleCardsView hand;
@ -121,7 +124,7 @@ public class GameView implements Serializable {
checkPaid(stackObject.getId(), ((StackAbility)stackObject)); checkPaid(stackObject.getId(), ((StackAbility)stackObject));
} }
} else { } else {
logger.error("Stack Object for stack ability not found: " + stackObject.toString());
} }
} }
else { else {

View file

@ -217,9 +217,9 @@ public abstract class AbilityImpl implements Ability {
} }
Card card = game.getCard(sourceId); MageObject sourceObject = game.getObject(sourceId);
if (card != null) { if (sourceObject != null) {
card.adjustChoices(this, game); sourceObject.adjustChoices(this, game);
} }
for (UUID modeId :this.getModes().getSelectedModes()) { for (UUID modeId :this.getModes().getSelectedModes()) {
this.getModes().setMode(this.getModes().get(modeId)); 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 // 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). // 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. // 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; boolean alternativeCostisUsed = false;
for (Ability ability : card.getAbilities()) { for (Ability ability : sourceObject.getAbilities()) {
// if cast for noMana no Alternative costs are allowed // if cast for noMana no Alternative costs are allowed
if (!noMana && ability instanceof AlternativeSourceCosts) { if (!noMana && ability instanceof AlternativeSourceCosts) {
AlternativeSourceCosts alternativeSpellCosts = (AlternativeSourceCosts) ability; 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 // 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.) // the stack until the spell has finished being cast.)
if (card != null) { if (sourceObject != null) {
card.adjustTargets(this, game); sourceObject.adjustTargets(this, game);
} }
if (getTargets().size() > 0 && getTargets().chooseTargets(getEffects().get(0).getOutcome(), this.controllerId, this, game) == false) { if (getTargets().size() > 0 && getTargets().chooseTargets(getEffects().get(0).getOutcome(), this.controllerId, this, game) == false) {
if (variableManaCost != null || announceString != null) { 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 { } else {
logger.debug("activate failed - target"); logger.debug("activate failed - target");
} }
@ -328,9 +328,9 @@ public abstract class AbilityImpl implements Ability {
} }
} }
//20100716 - 601.2e //20100716 - 601.2e
if (card != null) { if (sourceObject != null) {
card.adjustCosts(this, game); sourceObject.adjustCosts(this, game);
for (Ability ability : card.getAbilities()) { for (Ability ability : sourceObject.getAbilities()) {
if (ability instanceof AdjustingSourceCosts) { if (ability instanceof AdjustingSourceCosts) {
((AdjustingSourceCosts)ability).adjustCosts(this, game); ((AdjustingSourceCosts)ability).adjustCosts(this, game);
} }
@ -824,6 +824,9 @@ public abstract class AbilityImpl implements Ability {
return ""; return "";
} }
MageObject object = game.getObject(this.sourceId); 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: ") return new StringBuilder(" activates: ")
.append(object != null ? this.formatRule(modes.getText(), object.getName()) :modes.getText()) .append(object != null ? this.formatRule(modes.getText(), object.getName()) :modes.getText())
.append(" from ") .append(" from ")
@ -833,9 +836,6 @@ public abstract class AbilityImpl implements Ability {
protected String getMessageText(Game game) { protected String getMessageText(Game game) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
MageObject object = game.getObject(this.sourceId); MageObject object = game.getObject(this.sourceId);
if (object == null) {
object = game.getLastKnownInformation(this.sourceId, Zone.BATTLEFIELD);
}
if (object != null) { if (object != null) {
if (object instanceof StackAbility) { if (object instanceof StackAbility) {
Card card = game.getCard(((StackAbility) object).getSourceId()); Card card = game.getCard(((StackAbility) object).getSourceId());
@ -854,7 +854,7 @@ public abstract class AbilityImpl implements Ability {
} }
sb.append(getOptionalTextSuffix(game, spell)); sb.append(getOptionalTextSuffix(game, spell));
} else { } else {
sb.append(object.getName()); sb.append(object.getLogName());
} }
} }
} else { } else {

View file

@ -284,7 +284,7 @@ public abstract class GameImpl implements Game, Serializable {
return state.getPlayer(playerId); return state.getPlayer(playerId);
} }
@Override @Override
public MageObject getObject(UUID objectId) { public MageObject getObject(UUID objectId) {
if (objectId == null) { if (objectId == null) {
return null; return null;
@ -304,13 +304,13 @@ public abstract class GameImpl implements Game, Serializable {
return item; return item;
} }
} }
for (CommandObject commandObject : state.getCommand()) { for (CommandObject commandObject : state.getCommand()) {
if (commandObject instanceof Commander && commandObject.getId().equals(objectId)) { if (commandObject instanceof Commander && commandObject.getId().equals(objectId)) {
return commandObject; return commandObject;
} }
} }
object = getCard(objectId); object = getCard(objectId);
if (object == null) { if (object == null) {
@ -319,6 +319,8 @@ public abstract class GameImpl implements Game, Serializable {
return commandObject; return commandObject;
} }
} }
// can be an ability of a sacrificed Token trying to get it's source object
object = getLastKnownInformation(objectId, Zone.BATTLEFIELD);
} }
return object; return object;
@ -415,8 +417,8 @@ public abstract class GameImpl implements Game, Serializable {
} }
/** /**
* Starts check if game over or if playerId is given * Starts check if game is over or
* let the player concede. * if playerId is given let the player concede.
* *
* @param playerId * @param playerId
* @return * @return
@ -817,24 +819,27 @@ public abstract class GameImpl implements Game, Serializable {
} }
protected UUID findWinnersAndLosers() { 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()) { for (Player player: state.getPlayers().values()) {
if (player.hasWon()) { if (player.hasWon()) {
winner = player.getId(); logger.debug(new StringBuilder("GameImpl.findWinnersAndLosers playerHasWon ").append(player.getId()));
winnerIdFound = player.getId();
break; break;
} }
if (!player.hasLost() && !player.hasLeft()) { if (!player.hasLost() && !player.hasLeft()) {
logger.debug(new StringBuilder("GameImpl.findWinnersAndLosers player ").append(player.getId()));
player.won(this); player.won(this);
winner = player.getId(); winnerIdFound = player.getId();
break; break;
} }
} }
for (Player player: state.getPlayers().values()) { 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); player.lost(this);
} }
} }
return winner; return winnerIdFound;
} }
protected void endOfTurn() { protected void endOfTurn() {

View file

@ -30,6 +30,7 @@ package mage.game.permanent;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.UUID; import java.util.UUID;
import mage.abilities.Ability;
import mage.cards.Card; import mage.cards.Card;
import mage.cards.LevelerCard; import mage.cards.LevelerCard;
import mage.constants.Zone; import mage.constants.Zone;
@ -222,5 +223,20 @@ public class PermanentCard extends PermanentImpl {
} }
return false; 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);
}
} }

View file

@ -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 * 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 * 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. * treated as the number chosen for it while the object is on the stack.
* @return
*/ */
public int getConvertedManaCost() { public int getConvertedManaCost() {
int cmc = 0; int cmc = 0;
@ -753,13 +754,25 @@ public class Spell implements StackObject, Card {
} }
@Override @Override
public void adjustChoices(Ability ability, Game game) {} public void adjustChoices(Ability ability, Game game) {
if (card != null) {
card.adjustChoices(ability, game);
}
}
@Override @Override
public void adjustCosts(Ability ability, Game game) {} public void adjustCosts(Ability ability, Game game) {
if (card != null) {
card.adjustCosts(ability, game);
}
}
@Override @Override
public void adjustTargets(Ability ability, Game game) {} public void adjustTargets(Ability ability, Game game) {
if (card != null) {
card.adjustTargets(ability, game);
}
}
@Override @Override

View file

@ -54,6 +54,7 @@ import mage.target.Targets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import mage.cards.Card;
import mage.constants.AbilityWord; import mage.constants.AbilityWord;
/** /**
@ -323,7 +324,12 @@ public class StackAbility implements StackObject, Ability {
public void adjustCosts(Ability ability, Game game) {} public void adjustCosts(Ability ability, Game game) {}
@Override @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 @Override
public Costs<Cost> getOptionalCosts() { public Costs<Cost> getOptionalCosts() {