From 16d8233560d779f735c9589a8f30171cb54d69cc Mon Sep 17 00:00:00 2001 From: Hanmac Date: Tue, 8 Nov 2016 06:15:30 +0000 Subject: [PATCH] ControlGainAi: some fixes about using LoseControl parameter --- .../java/forge/ai/ability/ControlGainAi.java | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/forge-ai/src/main/java/forge/ai/ability/ControlGainAi.java b/forge-ai/src/main/java/forge/ai/ability/ControlGainAi.java index f42e320eb0a..6ed6dbfd189 100644 --- a/forge-ai/src/main/java/forge/ai/ability/ControlGainAi.java +++ b/forge-ai/src/main/java/forge/ai/ability/ControlGainAi.java @@ -17,6 +17,8 @@ */ package forge.ai.ability; +import java.util.List; + import com.google.common.base.Predicate; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; @@ -32,15 +34,13 @@ import forge.game.card.CardCollectionView; import forge.game.card.CardLists; import forge.game.phase.PhaseType; import forge.game.player.Player; +import forge.game.player.PlayerPredicates; import forge.game.spellability.SpellAbility; import forge.game.spellability.TargetRestrictions; import forge.game.zone.ZoneType; import forge.util.Aggregates; import forge.util.collect.FCollectionView; -import java.util.Arrays; -import java.util.List; - //AB:GainControl|ValidTgts$Creature|TgtPrompt$Select target legendary creature|LoseControl$Untap,LoseControl|SpellDescription$Gain control of target xxxxxxx @@ -69,7 +69,11 @@ public class ControlGainAi extends SpellAbilityAi { @Override protected boolean canPlayAI(final Player ai, final SpellAbility sa) { - final List lose = sa.hasParam("LoseControl") ? Arrays.asList(sa.getParam("LoseControl").split(",")) : null; + final List lose = Lists.newArrayList(); + + if (sa.hasParam("LoseControl")) { + lose.addAll(Lists.newArrayList(sa.getParam("LoseControl").split(","))); + } final TargetRestrictions tgt = sa.getTargetRestrictions(); final Game game = ai.getGame(); @@ -96,22 +100,20 @@ public class ControlGainAi extends SpellAbilityAi { sa.getTargets().add(Aggregates.random(tgt.getAllCandidates(sa, false))); } if (tgt.canOnlyTgtOpponent()) { - boolean found = false; - for (final Player opp : opponents) { - if (opp.canBeTargetedBy(sa)) { - sa.getTargets().add(opp); - break; - } - } - if (!found) { + List oppList = Lists + .newArrayList(Iterables.filter(opponents, PlayerPredicates.isTargetableBy(sa))); + + if (oppList.isEmpty()) { return false; } + + sa.getTargets().add(oppList.get(0)); } } // Don't steal something if I can't Attack without, or prevent it from // blocking at least - if (lose != null && lose.contains("EOT") + if (lose.contains("EOT") && ai.getGame().getPhaseHandler().getPhase().isAfter(PhaseType.COMBAT_DECLARE_ATTACKERS) && !sa.isTrigger()) { return false; @@ -276,9 +278,13 @@ public class ControlGainAi extends SpellAbilityAi { return false; } } + final List lose = Lists.newArrayList(); - final List lose = sa.hasParam("LoseControl") ? Arrays.asList(sa.getParam("LoseControl").split(",")) : null; - if ((lose != null) && lose.contains("EOT") + if (sa.hasParam("LoseControl")) { + lose.addAll(Lists.newArrayList(sa.getParam("LoseControl").split(","))); + } + + if (lose.contains("EOT") && game.getPhaseHandler().getPhase().isAfter(PhaseType.COMBAT_DECLARE_ATTACKERS)) { return false; }