From b0f37ca217c9b24b20afe1065dc4f3727f3ea85c Mon Sep 17 00:00:00 2001 From: Hanmac Date: Sat, 30 Sep 2017 14:06:13 +0000 Subject: [PATCH] ComuterUtilAbility: some tweak to find with checking api first before iterate --- .../java/forge/ai/ComputerUtilAbility.java | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/forge-ai/src/main/java/forge/ai/ComputerUtilAbility.java b/forge-ai/src/main/java/forge/ai/ComputerUtilAbility.java index f16b74cb298..f8f7b320c14 100644 --- a/forge-ai/src/main/java/forge/ai/ComputerUtilAbility.java +++ b/forge-ai/src/main/java/forge/ai/ComputerUtilAbility.java @@ -1,6 +1,11 @@ package forge.ai; +import java.util.Iterator; +import java.util.List; + import com.google.common.base.Predicate; +import com.google.common.collect.Lists; + import forge.card.CardStateName; import forge.game.Game; import forge.game.GameActionUtil; @@ -15,10 +20,6 @@ import forge.game.spellability.SpellAbility; import forge.game.spellability.SpellAbilityStackInstance; import forge.game.zone.ZoneType; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - public class ComputerUtilAbility { public static CardCollection getAvailableLandsToPlay(final Game game, final Player player) { if (!game.getStack().isEmpty() || !game.getPhaseHandler().getPhase().isMain()) { @@ -78,7 +79,7 @@ public class ComputerUtilAbility { } public static List getSpellAbilities(final CardCollectionView l, final Player player) { - final List spellAbilities = new ArrayList(); + final List spellAbilities = Lists.newArrayList(); for (final Card c : l) { for (final SpellAbility sa : c.getSpellAbilities()) { spellAbilities.add(sa); @@ -93,7 +94,7 @@ public class ComputerUtilAbility { } public static List getOriginalAndAltCostAbilities(final List originList, final Player player) { - final List newAbilities = new ArrayList(); + final List newAbilities = Lists.newArrayList(); for (SpellAbility sa : originList) { sa.setActivatingPlayer(player); //add alternative costs as additional spell abilities @@ -101,7 +102,7 @@ public class ComputerUtilAbility { newAbilities.addAll(GameActionUtil.getAlternativeCosts(sa, player)); } - final List result = new ArrayList(); + final List result = Lists.newArrayList(); for (SpellAbility sa : newAbilities) { sa.setActivatingPlayer(player); result.addAll(GameActionUtil.getOptionalCosts(sa)); @@ -132,7 +133,8 @@ public class ComputerUtilAbility { } public static String getAbilitySourceName(SpellAbility sa) { - return sa.getOriginalHost() != null ? sa.getOriginalHost().getName() : sa.getHostCard() != null ? sa.getHostCard().getName() : ""; + final Card c = getAbilitySource(sa); + return c != null ? c.getName() : ""; } public static CardCollection getCardsTargetedWithApi(Player ai, CardCollection cardList, SpellAbility sa, ApiType api) { @@ -143,9 +145,9 @@ public class ComputerUtilAbility { if (sa != null) { SpellAbility saSub = sa.getRootAbility(); while (saSub != null) { - for (Card c : cardList) { - if (saSub.getApi() == api) { - if (saSub.getTargets() != null && saSub.getTargets().getTargetCards().contains(c)) { + if (saSub.getApi() == api && saSub.getTargets() != null) { + for (Card c : cardList) { + if (saSub.getTargets().getTargetCards().contains(c)) { // Was already targeted with this API in a parent or sub SA targeted.add(c); } @@ -156,10 +158,10 @@ public class ComputerUtilAbility { } for (SpellAbilityStackInstance si : ai.getGame().getStack()) { SpellAbility ab = si.getSpellAbility(false); - if (ab != null && ab.getApi() == api) { + if (ab != null && ab.getApi() == api && si.getTargetChoices() != null) { for (Card c : cardList) { // TODO: somehow ensure that the detected SA won't be countered - if (si.getTargetChoices() != null && si.getTargetChoices().getTargetCards().contains(c)) { + if (si.getTargetChoices().getTargetCards().contains(c)) { // Was already targeted by a spell ability instance on stack targeted.add(c); }