* 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.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<PlayerView> 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 {

View file

@ -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 {

View file

@ -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() {

View file

@ -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);
}
}

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
* 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

View file

@ -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<Cost> getOptionalCosts() {