From 47ad1ea0eefd5f2cab1832043957bfa541009cdc Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Thu, 19 Apr 2018 21:02:34 +0200 Subject: [PATCH] Fixed a AI target selecting problem. --- .../java/mage/player/ai/ComputerPlayer.java | 36 +++++++++++++++++++ .../common/TargetPlayerOrPlaneswalker.java | 5 +++ 2 files changed, 41 insertions(+) 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 54a6f78f78..c53b0f71a9 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 @@ -576,6 +576,41 @@ public class ComputerPlayer extends PlayerImpl implements Player { } } } + } + + if (target.getOriginalTarget() instanceof TargetPlayerOrPlaneswalker) { + List targets; + TargetPlayerOrPlaneswalker t = ((TargetPlayerOrPlaneswalker) target); + if (outcome.isGood()) { + targets = threats(abilityControllerId, source.getSourceId(), ((FilterPermanentOrPlayer) t.getFilter()).getPermanentFilter(), game, target.getTargets()); + } else { + targets = threats(randomOpponentId, source.getSourceId(), ((FilterPermanentOrPlayer) t.getFilter()).getPermanentFilter(), game, target.getTargets()); + } + + if (targets.isEmpty()) { + if (outcome.isGood()) { + if (target.canTarget(getId(), abilityControllerId, source, game)) { + target.addTarget(abilityControllerId, source, game); + return true; + } + } else if (target.canTarget(getId(), randomOpponentId, source, game)) { + target.addTarget(randomOpponentId, source, game); + return true; + } + } + + if (targets.isEmpty() && target.isRequired(source)) { + targets = game.getBattlefield().getActivePermanents(((TargetPlayerOrPlaneswalker) t.getFilter()).getPlaneswalkerFilter(), playerId, game); + } + for (Permanent permanent : targets) { + List alreadyTargetted = target.getTargets(); + if (t.canTarget(abilityControllerId, permanent.getId(), source, game)) { + if (alreadyTargetted != null && !alreadyTargetted.contains(permanent.getId())) { + target.addTarget(permanent.getId(), source, game); + return true; + } + } + } if (outcome.isGood()) { if (target.canTarget(getId(), abilityControllerId, source, game)) { @@ -753,6 +788,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { } return target.isChosen(); } + throw new IllegalStateException("Target wasn't handled. class:" + target.getClass().toString()); } //end of chooseTarget method diff --git a/Mage/src/main/java/mage/target/common/TargetPlayerOrPlaneswalker.java b/Mage/src/main/java/mage/target/common/TargetPlayerOrPlaneswalker.java index 5de959572c..5151043155 100644 --- a/Mage/src/main/java/mage/target/common/TargetPlayerOrPlaneswalker.java +++ b/Mage/src/main/java/mage/target/common/TargetPlayerOrPlaneswalker.java @@ -12,6 +12,7 @@ import mage.MageObject; import mage.abilities.Ability; import mage.constants.Zone; import mage.filter.Filter; +import mage.filter.common.FilterPlaneswalkerPermanent; import mage.filter.common.FilterPlayerOrPlaneswalker; import mage.game.Game; import mage.game.permanent.Permanent; @@ -213,6 +214,10 @@ public class TargetPlayerOrPlaneswalker extends TargetImpl { return sb.toString(); } + public FilterPlaneswalkerPermanent getPlaneswalkerFilter() { + return filter.getPlaneswalkerFilter(); + } + @Override public TargetPlayerOrPlaneswalker copy() { return new TargetPlayerOrPlaneswalker(this);