From 2162fab114e3bbcfc8ba7c9c26a619475c7225ac Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Sun, 1 Jan 2017 14:56:34 +0100 Subject: [PATCH] * Fixed a problem of AI target handling (e.g. for Precursor Golem) --- .../java/mage/player/ai/ComputerPlayer.java | 57 +++++++++---------- .../CopySpellForEachItCouldTargetEffect.java | 13 +++-- Mage/src/main/java/mage/target/Target.java | 2 + .../src/main/java/mage/target/TargetImpl.java | 5 ++ 4 files changed, 42 insertions(+), 35 deletions(-) diff --git a/Mage.Server.Plugins/Mage.Player.AI/src/main/java/mage/player/ai/ComputerPlayer.java b/Mage.Server.Plugins/Mage.Player.AI/src/main/java/mage/player/ai/ComputerPlayer.java index 3e7ff89435..fa5e8f6cb2 100644 --- a/Mage.Server.Plugins/Mage.Player.AI/src/main/java/mage/player/ai/ComputerPlayer.java +++ b/Mage.Server.Plugins/Mage.Player.AI/src/main/java/mage/player/ai/ComputerPlayer.java @@ -207,11 +207,10 @@ public class ComputerPlayer extends PlayerImpl implements Player { abilityControllerId = target.getAbilityController(); } UUID randomOpponentId = getRandomOpponent(abilityControllerId, game); - if (target instanceof TargetPlayer) { + if (target.getOriginalTarget() instanceof TargetPlayer) { return setTargetPlayer(outcome, target, null, sourceId, abilityControllerId, randomOpponentId, game); } - - if (target instanceof TargetDiscard) { + if (target.getOriginalTarget() instanceof TargetDiscard) { findPlayables(game); if (unplayable.size() > 0) { for (int i = unplayable.size() - 1; i >= 0; i--) { @@ -235,7 +234,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { } return false; } - if (target instanceof TargetControlledPermanent) { + if (target.getOriginalTarget() instanceof TargetControlledPermanent) { List targets; targets = threats(abilityControllerId, sourceId, ((TargetControlledPermanent) target).getFilter(), game, target.getTargets()); if (!outcome.isGood()) { @@ -249,7 +248,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { } return false; } - if (target instanceof TargetPermanent) { + if (target.getOriginalTarget() instanceof TargetPermanent) { List targets; if (outcome.isCanTargetAll()) { targets = threats(null, sourceId, ((TargetPermanent) target).getFilter(), game, target.getTargets()); @@ -288,8 +287,8 @@ public class ComputerPlayer extends PlayerImpl implements Player { return target.isChosen(); } - if (target instanceof TargetCardInHand - || (target.getZone().equals(Zone.HAND) && (target instanceof TargetCard))) { + if (target.getOriginalTarget() instanceof TargetCardInHand + || (target.getZone().equals(Zone.HAND) && (target.getOriginalTarget() instanceof TargetCard))) { List cards = new ArrayList<>(); for (UUID cardId : target.possibleTargets(sourceId, this.getId(), game)) { Card card = game.getCard(cardId); @@ -307,7 +306,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { } return target.isChosen(); } - if (target instanceof TargetCreatureOrPlayer) { + if (target.getOriginalTarget() instanceof TargetCreatureOrPlayer) { List targets; TargetCreatureOrPlayer t = ((TargetCreatureOrPlayer) target); if (outcome.isGood()) { @@ -338,7 +337,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { } } - if (target instanceof TargetPermanentOrPlayer) { + if (target.getOriginalTarget() instanceof TargetPermanentOrPlayer) { List targets; TargetPermanentOrPlayer t = ((TargetPermanentOrPlayer) target); List ownedTargets = threats(abilityControllerId, sourceId, ((FilterPermanentOrPlayer) t.getFilter()).getPermanentFilter(), game, target.getTargets()); @@ -393,7 +392,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { } return false; } - if (target instanceof TargetCardInGraveyard) { + if (target.getOriginalTarget() instanceof TargetCardInGraveyard) { List cards = new ArrayList<>(); for (Player player : game.getPlayers().values()) { for (Card card : player.getGraveyard().getCards(game)) { @@ -411,7 +410,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { return target.isChosen(); } - if (target instanceof TargetCardInYourGraveyard) { + if (target.getOriginalTarget() instanceof TargetCardInYourGraveyard) { List alreadyTargetted = target.getTargets(); List cards = new ArrayList<>(game.getPlayer(abilityControllerId).getGraveyard().getCards(game)); while (!cards.isEmpty()) { @@ -423,7 +422,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { } return false; } - if (target instanceof TargetSource) { + if (target.getOriginalTarget() instanceof TargetSource) { Set targets; TargetSource t = ((TargetSource) target); targets = t.possibleTargets(sourceId, abilityControllerId, game); @@ -459,11 +458,11 @@ public class ComputerPlayer extends PlayerImpl implements Player { abilityControllerId = target.getAbilityController(); } UUID randomOpponentId = getRandomOpponent(abilityControllerId, game); - if (target instanceof TargetPlayer) { + if (target.getOriginalTarget() instanceof TargetPlayer) { return setTargetPlayer(outcome, target, source, source.getSourceId(), abilityControllerId, randomOpponentId, game); } - if (target instanceof TargetDiscard || target instanceof TargetCardInHand) { + if (target.getOriginalTarget() instanceof TargetDiscard || target.getOriginalTarget() instanceof TargetCardInHand) { if (outcome.isGood()) { Cards cards = new CardsImpl(target.possibleTargets(source.getSourceId(), getId(), game)); ArrayList cardsInHand = new ArrayList<>(cards.getCards(game)); @@ -506,7 +505,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { } return false; } - if (target instanceof TargetControlledPermanent) { + if (target.getOriginalTarget() instanceof TargetControlledPermanent) { List targets; targets = threats(abilityControllerId, source.getSourceId(), ((TargetControlledPermanent) target).getFilter(), game, target.getTargets()); if (!outcome.isGood()) { @@ -523,7 +522,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { return target.isChosen(); } - if (target instanceof TargetPermanent) { + if (target.getOriginalTarget() instanceof TargetPermanent) { List targets; boolean outcomeTargets = true; if (outcome.isGood()) { @@ -547,7 +546,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { } return target.isChosen(); } - if (target instanceof TargetCreatureOrPlayer) { + if (target.getOriginalTarget() instanceof TargetCreatureOrPlayer) { List targets; TargetCreatureOrPlayer t = ((TargetCreatureOrPlayer) target); if (outcome.isGood()) { @@ -594,7 +593,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { //if (!target.isRequired()) return false; } - if (target instanceof TargetCardInGraveyard) { + if (target.getOriginalTarget() instanceof TargetCardInGraveyard) { List cards = new ArrayList<>(); for (Player player : game.getPlayers().values()) { cards.addAll(player.getGraveyard().getCards(game)); @@ -607,7 +606,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { //if (!target.isRequired()) return false; } - if (target instanceof TargetCardInLibrary) { + if (target.getOriginalTarget() instanceof TargetCardInLibrary) { List cards = new ArrayList<>(game.getPlayer(abilityControllerId).getLibrary().getCards(game)); Card card = pickTarget(cards, outcome, target, source, game); if (card != null) { @@ -616,7 +615,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { } return false; } - if (target instanceof TargetCardInYourGraveyard) { + if (target.getOriginalTarget() instanceof TargetCardInYourGraveyard) { List cards = new ArrayList<>(game.getPlayer(abilityControllerId).getGraveyard().getCards(game)); while (!target.isChosen() && !cards.isEmpty()) { Card card = pickTarget(cards, outcome, target, source, game); @@ -626,7 +625,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { } return target.isChosen(); } - if (target instanceof TargetSpell) { + if (target.getOriginalTarget() instanceof TargetSpell) { if (game.getStack().size() > 0) { Iterator it = game.getStack().iterator(); while (it.hasNext()) { @@ -639,7 +638,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { } return false; } - if (target instanceof TargetSpellOrPermanent) { + if (target.getOriginalTarget() instanceof TargetSpellOrPermanent) { // TODO: Also check if a spell should be selected List targets; boolean outcomeTargets = true; @@ -675,7 +674,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { } return false; } - if (target instanceof TargetCardInOpponentsGraveyard) { + if (target.getOriginalTarget() instanceof TargetCardInOpponentsGraveyard) { List cards = new ArrayList<>(); for (UUID uuid : game.getOpponents(abilityControllerId)) { Player player = game.getPlayer(uuid); @@ -691,7 +690,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { //if (!target.isRequired()) return false; } - if (target instanceof TargetDefender) { + if (target.getOriginalTarget() instanceof TargetDefender) { // TODO: Improve, now planeswalker is always chosen if it exits List targets; targets = game.getBattlefield().getActivePermanents(new FilterPlaneswalkerPermanent(), randomOpponentId, game); @@ -713,7 +712,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { return target.isChosen(); } - if (target instanceof TargetCardInASingleGraveyard) { + if (target.getOriginalTarget() instanceof TargetCardInASingleGraveyard) { List cards = new ArrayList<>(); for (Player player : game.getPlayers().values()) { cards.addAll(player.getGraveyard().getCards(game)); @@ -727,7 +726,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { return target.isChosen(); } - if (target instanceof TargetCardInExile) { + if (target.getOriginalTarget() instanceof TargetCardInExile) { List cards = new ArrayList<>(); for (UUID uuid : ((TargetCardInExile) target).possibleTargets(source.getSourceId(), source.getControllerId(), game)) { Card card = game.getCard(uuid); @@ -773,7 +772,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { log.debug("chooseTarget: " + outcome.toString() + ":" + target.toString()); } UUID opponentId = game.getOpponents(playerId).iterator().next(); - if (target instanceof TargetCreatureOrPlayerAmount) { + if (target.getOriginalTarget() instanceof TargetCreatureOrPlayerAmount) { if (outcome.equals(Outcome.Damage) && game.getPlayer(opponentId).getLife() <= target.getAmountRemaining()) { target.addTarget(opponentId, target.getAmountRemaining(), source, game); return true; @@ -2148,7 +2147,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { * */ private boolean setTargetPlayer(Outcome outcome, Target target, Ability source, UUID sourceId, UUID abilityControllerId, UUID randomOpponentId, Game game) { - if (target instanceof TargetOpponent) { + if (target.getOriginalTarget() instanceof TargetOpponent) { if (source == null) { if (((TargetOpponent) target).canTarget(randomOpponentId, game)) { target.add(randomOpponentId, game); @@ -2172,7 +2171,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { return false; } - if (target instanceof TargetPlayer) { + if (target.getOriginalTarget() instanceof TargetPlayer) { if (outcome.isGood()) { if (source == null) { if (target.canTarget(abilityControllerId, game)) { diff --git a/Mage/src/main/java/mage/abilities/effects/common/CopySpellForEachItCouldTargetEffect.java b/Mage/src/main/java/mage/abilities/effects/common/CopySpellForEachItCouldTargetEffect.java index 2e5c0cd6d4..9fa78743ac 100644 --- a/Mage/src/main/java/mage/abilities/effects/common/CopySpellForEachItCouldTargetEffect.java +++ b/Mage/src/main/java/mage/abilities/effects/common/CopySpellForEachItCouldTargetEffect.java @@ -261,9 +261,6 @@ class TargetWithAdditionalFilter extends TargetImpl { protected final FilterInPlay additionalFilter; protected final Target originalTarget; - protected static final Integer minNumberOfTargets = null; - protected static final Integer maxNumberOfTargets = null; - protected static final Zone zone = null; public TargetWithAdditionalFilter(final TargetWithAdditionalFilter target) { this(target.originalTarget, target.additionalFilter, false); @@ -274,14 +271,18 @@ class TargetWithAdditionalFilter extends TargetImpl { } public TargetWithAdditionalFilter(Target originalTarget, FilterInPlay additionalFilter, boolean notTarget) { - originalTarget = originalTarget.copy(); - originalTarget.clearChosen(); - this.originalTarget = originalTarget; + this.originalTarget = originalTarget.copy(); + this.originalTarget.clearChosen(); this.targetName = originalTarget.getFilter().getMessage(); this.notTarget = notTarget; this.additionalFilter = additionalFilter; } + @Override + public Target getOriginalTarget() { + return originalTarget; + } + @Override public int getNumberOfTargets() { return originalTarget.getNumberOfTargets(); diff --git a/Mage/src/main/java/mage/target/Target.java b/Mage/src/main/java/mage/target/Target.java index 80b977f4ee..307c5a46d6 100644 --- a/Mage/src/main/java/mage/target/Target.java +++ b/Mage/src/main/java/mage/target/Target.java @@ -155,4 +155,6 @@ public interface Target extends Serializable { int getTargetTag(); void setTargetTag(int tag); + + Target getOriginalTarget(); } diff --git a/Mage/src/main/java/mage/target/TargetImpl.java b/Mage/src/main/java/mage/target/TargetImpl.java index 1de0af8021..4e6f9cc4c7 100644 --- a/Mage/src/main/java/mage/target/TargetImpl.java +++ b/Mage/src/main/java/mage/target/TargetImpl.java @@ -554,4 +554,9 @@ public abstract class TargetImpl implements Target { this.targetTag = targetTag; } + @Override + public Target getOriginalTarget() { + return this; + } + }