From e277be419141a53c1be1b1a4ddaa1d0c03dbb413 Mon Sep 17 00:00:00 2001 From: Hanmac Date: Mon, 15 May 2017 05:10:48 +0000 Subject: [PATCH] ComputerUtilCombat: add better look for ReplacementDamage --- .../java/forge/ai/ComputerUtilCombat.java | 57 ++++++++++++------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/forge-ai/src/main/java/forge/ai/ComputerUtilCombat.java b/forge-ai/src/main/java/forge/ai/ComputerUtilCombat.java index 5bbffe026c4..f97f3e5d3b1 100644 --- a/forge-ai/src/main/java/forge/ai/ComputerUtilCombat.java +++ b/forge-ai/src/main/java/forge/ai/ComputerUtilCombat.java @@ -17,8 +17,13 @@ */ package forge.ai; +import java.util.List; +import java.util.Map; + import com.google.common.base.Predicate; import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import forge.game.CardTraitBase; import forge.game.Game; @@ -40,6 +45,7 @@ import forge.game.cost.CostPayment; import forge.game.phase.Untap; import forge.game.player.Player; import forge.game.replacement.ReplacementEffect; +import forge.game.replacement.ReplacementLayer; import forge.game.spellability.AbilityActivated; import forge.game.spellability.SpellAbility; import forge.game.staticability.StaticAbility; @@ -49,11 +55,6 @@ import forge.game.trigger.TriggerType; import forge.game.zone.ZoneType; import forge.util.collect.FCollection; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - /** *

@@ -189,10 +190,8 @@ public class ComputerUtilCombat { } if (!attacked.getGame().getStaticEffects().getGlobalRuleChange(GlobalRuleChange.noPrevention)) { - if (attacker.hasKeyword("Prevent all damage that would be dealt by CARDNAME.") - || attacker.hasKeyword("Prevent all damage that would be dealt to and by CARDNAME.") - || attacker.hasKeyword("Prevent all combat damage that would be dealt by CARDNAME.") - || attacker.hasKeyword("Prevent all combat damage that would be dealt to and dealt by CARDNAME.")) { + // ask ReplacementDamage directly + if (ComputerUtilCombat.isCombatDamagePrevented(attacker, attacked, damage)) { return 0; } } @@ -288,7 +287,7 @@ public class ComputerUtilCombat { int damage = 0; final List attackers = combat.getAttackersOf(ai); - final List unblocked = new ArrayList(); + final List unblocked = Lists.newArrayList(); for (final Card attacker : attackers) { @@ -330,7 +329,7 @@ public class ComputerUtilCombat { int poison = 0; final List attackers = combat.getAttackersOf(ai); - final List unblocked = new ArrayList(); + final List unblocked = Lists.newArrayList(); for (final Card attacker : attackers) { @@ -357,7 +356,7 @@ public class ComputerUtilCombat { } public static List getLifeThreateningCommanders(final Player ai, final Combat combat) { - List res = new ArrayList(); + List res = Lists.newArrayList(); for (Card c : combat.getAttackers()) { if (c.isCommander()) { int currentCommanderDamage = ai.getCommanderDamage(c); @@ -1267,7 +1266,7 @@ public class ComputerUtilCombat { } } - List list = new ArrayList(); + List list = Lists.newArrayList(); if (!abilityParams.containsKey("ValidCards")) { list = AbilityUtils.getDefinedCards(source, abilityParams.get("Defined"), null); } @@ -1479,7 +1478,7 @@ public class ComputerUtilCombat { } } - List list = new ArrayList(); + List list = Lists.newArrayList(); if (!abilityParams.containsKey("ValidCards")) { list = AbilityUtils.getDefinedCards(source, abilityParams.get("Defined"), null); } @@ -2034,7 +2033,7 @@ public class ComputerUtilCombat { */ public static Map distributeAIDamage(final Card attacker, final CardCollectionView block, int dmgCanDeal, GameEntity defender, boolean overrideOrder) { // TODO: Distribute defensive Damage (AI controls how damage is dealt to own cards) for Banding and Defensive Formation - Map damageMap = new HashMap(); + Map damageMap = Maps.newHashMap(); boolean isAttacking = defender != null; @@ -2317,10 +2316,7 @@ public class ComputerUtilCombat { } if (!withoutAbilities) { - List keywords = new ArrayList(); - keywords.add("Double Strike"); - keywords.add("First Strike"); - return canGainKeyword(combatant, keywords, combat); + return canGainKeyword(combatant, Lists.newArrayList("Double Strike", "First Strike"), combat); } return false; @@ -2339,9 +2335,7 @@ public class ComputerUtilCombat { return true; } if (!withoutAbilities) { - List keywords = new ArrayList(); - keywords.add(keyword); - return canGainKeyword(combatant, keywords, combat); + return canGainKeyword(combatant, Lists.newArrayList(keyword), combat); } else { return false; } @@ -2406,6 +2400,25 @@ public class ComputerUtilCombat { } return original; } + + private final static boolean isCombatDamagePrevented(final Card attacker, final GameEntity target, final int damage) { + final Game game = attacker.getGame(); + + // first try to replace the damage + final Map repParams = Maps.newHashMap(); + repParams.put("Event", "DamageDone"); + repParams.put("Affected", target); + repParams.put("DamageSource", attacker); + repParams.put("DamageAmount", damage); + repParams.put("IsCombat", true); + repParams.put("Prevention", true); + // repParams.put("PreventMap", preventMap); + + List list = game.getReplacementHandler().getReplacementList(repParams, + ReplacementLayer.None); + + return !list.isEmpty(); + } }