From 32a1a6fc95b6e4ec8f5989bbecd1050a01eec55a Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 11 Nov 2012 15:27:45 +0000 Subject: [PATCH] - CheckStyle. --- .../card/abilityfactory/ai/AttachAi.java | 191 +++++++++--------- 1 file changed, 97 insertions(+), 94 deletions(-) diff --git a/src/main/java/forge/card/abilityfactory/ai/AttachAi.java b/src/main/java/forge/card/abilityfactory/ai/AttachAi.java index d096c09e6a1..3e10be91111 100644 --- a/src/main/java/forge/card/abilityfactory/ai/AttachAi.java +++ b/src/main/java/forge/card/abilityfactory/ai/AttachAi.java @@ -42,14 +42,14 @@ public class AttachAi extends SpellAiLogic { final Random r = MyRandom.getRandom(); final Cost abCost = sa.getPayCosts(); final Card source = sa.getSourceCard(); - + if (abCost != null) { // No Aura spells have Additional Costs } - + // prevent run-away activations - first time will always return true final boolean chance = r.nextFloat() <= Math.pow(.6667, sa.getActivationsThisTurn()); - + // Attach spells always have a target final Target tgt = sa.getTarget(); if (tgt != null) { @@ -58,24 +58,24 @@ public class AttachAi extends SpellAiLogic { return false; } } - + if (abCost.getTotalMana().contains("X") && source.getSVar("X").equals("Count$xPaid")) { // Set PayX here to maximum value. (Endless Scream and Venarian // Gold) final int xPay = ComputerUtil.determineLeftoverMana(sa, ai); - + if (xPay == 0) { return false; } - + source.setSVar("PayX", Integer.toString(xPay)); } - + if (Singletons.getModel().getGame().getPhaseHandler().getPhase().isAfter(PhaseType.COMBAT_DECLARE_BLOCKERS_INSTANT_ABILITY) && !"Curse".equals(sa.getParam("AILogic"))) { return false; } - + return chance; } @@ -92,7 +92,7 @@ public class AttachAi extends SpellAiLogic { if (mandatory) { return c; } - + // TODO If Not Mandatory, make sure the card is "good enough" if (c.isCreature()) { final int eval = CardFactoryUtil.evaluateCreature(c); @@ -100,7 +100,7 @@ public class AttachAi extends SpellAiLogic { return null; } } - + return c; } @@ -117,7 +117,7 @@ public class AttachAi extends SpellAiLogic { if (!mandatory) { return null; } - + return CardFactoryUtil.getWorstPermanentAI(list, true, true, true, false); } @@ -134,7 +134,7 @@ public class AttachAi extends SpellAiLogic { if (!mandatory) { return null; } - + return CardFactoryUtil.getBestAI(list); } @@ -154,31 +154,31 @@ public class AttachAi extends SpellAiLogic { private static Card attachAIChangeTypePreference(final SpellAbility sa, List list, final boolean mandatory, final Card attachSource) { // AI For Cards like Evil Presence or Spreading Seas - + String type = ""; - + for (final StaticAbility stAb : attachSource.getStaticAbilities()) { final HashMap stab = stAb.getMapParams(); if (stab.get("Mode").equals("Continuous") && stab.containsKey("AddType")) { type = stab.get("AddType"); } } - + list = CardLists.getNotType(list, type); // Filter out Basic Lands that have the // same type as the changing type - + final Card c = CardFactoryUtil.getBestAI(list); - + // TODO Port over some of the existing code, but rewrite most of it. // Ultimately, these spells need to be used to reduce mana base of a // color. So it might be better to choose a Basic over a Nonbasic // Although a nonbasic card with a nasty ability, might be worth it to // cast on - + if (c == null) { return chooseLessPreferred(mandatory, list); } - + return acceptableChoice(c, mandatory); } @@ -204,16 +204,16 @@ public class AttachAi extends SpellAiLogic { if (c.isCreature() && c.hasKeyword("Vigilance") && c.isUntapped()) { return false; } - + if (!c.isEnchanted()) { return true; } - + final ArrayList auras = c.getEnchantedBy(); final Iterator itr = auras.iterator(); while (itr.hasNext()) { final Card aura = itr.next(); - SpellAbility auraSA = aura.getSpellPermanent(); + SpellAbility auraSA = aura.getSpellPermanent(); if (auraSA.getApi() == ApiType.Attach) { if ("KeepTapped".equals(auraSA.getParam("AILogic"))) { // Don't attach multiple KeepTapped Auras to one @@ -222,17 +222,17 @@ public class AttachAi extends SpellAiLogic { } } } - + return true; } }); - + final Card c = CardFactoryUtil.getBestAI(prefList); - + if (c == null) { return chooseLessPreferred(mandatory, list); } - + return acceptableChoice(c, mandatory); } @@ -250,26 +250,26 @@ public class AttachAi extends SpellAiLogic { private static Player attachToPlayerAIPreferences(final Player aiPlayer, final SpellAbility sa, final boolean mandatory) { Player p; - + if (sa.isCurse()) { p = aiPlayer.getOpponent(); } else { p = aiPlayer; } - + if (sa.canTarget(p)) { return p; } - + if (!mandatory) { return null; } - + p = p.getOpponent(); if (sa.canTarget(p)) { return p; } - + return null; } @@ -298,15 +298,15 @@ public class AttachAi extends SpellAiLogic { } }); } - + final Card c = CardFactoryUtil.getMostExpensivePermanentAI(betterList); - + // If Mandatory (brought directly into play without casting) gotta // choose something if (c == null && mandatory) { return chooseLessPreferred(mandatory, list); } - + return c; } @@ -327,23 +327,23 @@ public class AttachAi extends SpellAiLogic { private static Card attachAIControlPreference(final SpellAbility sa, final List list, final boolean mandatory, final Card attachSource) { // AI For choosing a Card to Gain Control of. - + if (sa.getTarget().canTgtPermanent()) { // If can target all Permanents, and Life isn't in eminent danger, // grab Planeswalker first, then Creature // if Life < 5 grab Creature first, then Planeswalker. Lands, // Enchantments and Artifacts are probably "not good enough" - + } - + final Card c = CardFactoryUtil.getBestAI(list); - + // If Mandatory (brought directly into play without casting) gotta // choose something if (c == null) { return chooseLessPreferred(mandatory, list); } - + return acceptableChoice(c, mandatory); } @@ -363,7 +363,7 @@ public class AttachAi extends SpellAiLogic { private static Card attachAICursePreference(final SpellAbility sa, final List list, final boolean mandatory, final Card attachSource) { // AI For choosing a Card to Curse of. - + // TODO Figure out some way to combine The "gathering of data" from // statics used in both Pump and Curse String stCheck = null; @@ -372,21 +372,21 @@ public class AttachAi extends SpellAiLogic { } else if (attachSource.isEquipment()) { stCheck = "EquippedBy"; } - + int totToughness = 0; // int totPower = 0; final ArrayList keywords = new ArrayList(); // boolean grantingAbilities = false; - + for (final StaticAbility stAbility : attachSource.getStaticAbilities()) { final Map stabMap = stAbility.getMapParams(); - + if (!stabMap.get("Mode").equals("Continuous")) { continue; } - + final String affected = stabMap.get("Affected"); - + if (affected == null) { continue; } @@ -394,9 +394,9 @@ public class AttachAi extends SpellAiLogic { totToughness += CardFactoryUtil.parseSVar(attachSource, stabMap.get("AddToughness")); // totPower += CardFactoryUtil.parseSVar(attachSource, // sa.get("AddPower")); - + // grantingAbilities |= sa.containsKey("AddAbility"); - + final String kws = stabMap.get("AddKeyword"); if (kws != null) { for (final String kw : kws.split(" & ")) { @@ -405,7 +405,7 @@ public class AttachAi extends SpellAiLogic { } } } - + List prefList = null; if (totToughness < 0) { // Kill a creature if we can @@ -416,7 +416,7 @@ public class AttachAi extends SpellAiLogic { if (!c.hasKeyword("Indestructible") && (c.getLethalDamage() <= Math.abs(tgh))) { return true; } - + return c.getNetDefense() <= Math.abs(tgh); } }); @@ -430,7 +430,7 @@ public class AttachAi extends SpellAiLogic { return c; } } - + if (!keywords.isEmpty()) { // Don't give Can't Attack or Defender to cards that can't do these // things to begin with @@ -444,18 +444,17 @@ public class AttachAi extends SpellAiLogic { }); } } - + c = CardFactoryUtil.getBestAI(prefList); - + if (c == null) { return chooseLessPreferred(mandatory, list); } - + return acceptableChoice(c, mandatory); } - - + /** * Attach do trigger ai. * @param sa @@ -476,14 +475,14 @@ public class AttachAi extends SpellAiLogic { if (tgt == null) { targets = AbilityFactory.getDefinedObjects(sa.getSourceCard(), sa.getParam("Defined"), sa); } - + if (!mandatory && card.isEquipment() && !targets.isEmpty()) { Card newTarget = (Card) targets.get(0); //don't equip human creatures if (newTarget.getController().isHuman()) { return false; } - + //don't equip a worse creature if (card.isEquipping()) { Card oldTarget = card.getEquipping().get(0); @@ -492,7 +491,7 @@ public class AttachAi extends SpellAiLogic { } } } - + return true; } @@ -518,11 +517,11 @@ public class AttachAi extends SpellAiLogic { } else { o = attachToCardAIPreferences(sa.getActivatingPlayer(), sa, mandatory); } - + if (o == null) { return false; } - + tgt.addTarget(o); return true; } @@ -551,7 +550,9 @@ public class AttachAi extends SpellAiLogic { magnetList = CardLists.filter(list, new Predicate() { @Override public boolean apply(final Card c) { - if ( !c.isCreature() ) return false; + if (!c.isCreature()) { + return false; + } String sVar = c.getSVar("EnchantMe"); return sVar.equals("Multiple") || (sVar.equals("Once") && !c.isEnchanted()); } @@ -561,51 +562,53 @@ public class AttachAi extends SpellAiLogic { magnetList = CardLists.filter(list, new Predicate() { @Override public boolean apply(final Card c) { - if ( !c.isCreature() ) return false; + if (!c.isCreature()) { + return false; + } String sVar = c.getSVar("EquipMe"); return sVar.equals("Multiple") || (sVar.equals("Once") && !c.isEquipped()); } }); } - + if ((magnetList != null) && !magnetList.isEmpty()) { // Always choose something from the Magnet List. // Probably want to "weight" the list by amount of Enchantments and // choose the "lightest" - + magnetList = CardLists.filter(magnetList, new Predicate() { @Override public boolean apply(final Card c) { return CombatUtil.canAttack(c, ai.getOpponent()); } }); - + return CardFactoryUtil.getBestAI(magnetList); } - + int totToughness = 0; int totPower = 0; final ArrayList keywords = new ArrayList(); boolean grantingAbilities = false; - + for (final StaticAbility stAbility : attachSource.getStaticAbilities()) { final Map stabMap = stAbility.getMapParams(); - + if (!stabMap.get("Mode").equals("Continuous")) { continue; } - + final String affected = stabMap.get("Affected"); - + if (affected == null) { continue; } if ((affected.contains(stCheck) || affected.contains("AttachedBy"))) { totToughness += CardFactoryUtil.parseSVar(attachSource, stabMap.get("AddToughness")); totPower += CardFactoryUtil.parseSVar(attachSource, stabMap.get("AddPower")); - + grantingAbilities |= stabMap.containsKey("AddAbility"); - + String kws = stabMap.get("AddKeyword"); if (kws != null) { for (final String kw : kws.split(" & ")) { @@ -620,7 +623,7 @@ public class AttachAi extends SpellAiLogic { } } } - + List prefList = new ArrayList(list); if (totToughness < 0) { // Don't kill my own stuff with Negative toughness Auras @@ -632,7 +635,7 @@ public class AttachAi extends SpellAiLogic { } }); } - + //only add useful keywords unless P/T bonus is significant if (totToughness + totPower < 4 && !keywords.isEmpty()) { prefList = CardLists.filter(prefList, new Predicate() { @@ -642,7 +645,7 @@ public class AttachAi extends SpellAiLogic { } }); } - + // Don't pump cards that will die. prefList = CardLists.filter(prefList, new Predicate() { @Override @@ -650,13 +653,13 @@ public class AttachAi extends SpellAiLogic { return !c.getSVar("Targeting").equals("Dies"); } }); - + if (attachSource.isAura()) { // TODO For Auras like Rancor, that aren't as likely to lead to // card disadvantage, this check should be skipped prefList = CardLists.filter(prefList, Predicates.not(Presets.ENCHANTED)); } - + if (!grantingAbilities) { // Probably prefer to Enchant Creatures that Can Attack // Filter out creatures that can't Attack or have Defender @@ -672,11 +675,11 @@ public class AttachAi extends SpellAiLogic { // Possibly more defensive? c = CardFactoryUtil.getWorstPermanentAI(prefList, false, false, false, false); } - + if (c == null) { return chooseLessPreferred(mandatory, list); } - + return c; } @@ -696,15 +699,15 @@ public class AttachAi extends SpellAiLogic { final Card attachSource = sa.getSourceCard(); // TODO AttachSource is currently set for the Source of the Spell, but // at some point can support attaching a different card - + // Don't equip if already equipping if (attachSource.getEquippingCard() != null && attachSource.getEquippingCard().getController().isComputer()) { return null; } - + List list = Singletons.getModel().getGame().getCardsIn(tgt.getZone()); list = CardLists.getValidCards(list, tgt.getValidTgts(), sa.getActivatingPlayer(), attachSource); - + // TODO If Attaching without casting, don't need to actually target. // I believe this is the only case where mandatory will be true, so just // check that when starting that work @@ -714,7 +717,7 @@ public class AttachAi extends SpellAiLogic { } else { list = CardLists.filter(list, Predicates.not(CardPredicates.isProtectedFrom(attachSource))); } - + if (list.isEmpty()) { return null; } @@ -722,14 +725,14 @@ public class AttachAi extends SpellAiLogic { if (sa.hasParam("AITgts")) { prefList = CardLists.getValidCards(list, sa.getParam("AITgts"), sa.getActivatingPlayer(), attachSource); } - + Card c = attachGeneralAI(aiPlayer, sa, prefList, mandatory, attachSource, sa.getParam("AILogic")); - + if ((c == null) && mandatory) { CardLists.shuffle(list); c = list.get(0); } - + return c; } @@ -751,18 +754,18 @@ public class AttachAi extends SpellAiLogic { private static Card attachGeneralAI(final Player ai, final SpellAbility sa, final List list, final boolean mandatory, final Card attachSource, final String logic) { Player prefPlayer = ai.getOpponent(); - if ("Pump".equals(logic) || "Animate".equals(logic) ) { + if ("Pump".equals(logic) || "Animate".equals(logic)) { prefPlayer = ai; } // Some ChangeType cards are beneficial, and PrefPlayer should be // changed to represent that final List prefList = CardLists.filterControlledBy(list, prefPlayer); - + // If there are no preferred cards, and not mandatory bail out if (prefList.size() == 0) { return chooseUnpreferred(mandatory, list); } - + // Preferred list has at least one card in it to make to the actual // Logic Card c = null; @@ -779,7 +782,7 @@ public class AttachAi extends SpellAiLogic { } else if ("Animate".equals(logic)) { c = attachAIAnimatePreference(sa, prefList, mandatory, attachSource); } - + return c; } @@ -841,7 +844,7 @@ public class AttachAi extends SpellAiLogic { return true; } else if (keyword.endsWith("Deathtouch") || keyword.endsWith("Wither")) { if (card.getNetCombatDamage() <= 0 - || ((!CombatUtil.canBeBlocked(card) || !CombatUtil.canAttackNextTurn(card)) + || ((!CombatUtil.canBeBlocked(card) || !CombatUtil.canAttackNextTurn(card)) && !CombatUtil.canBlock(card, true))) { return false; } @@ -867,7 +870,7 @@ public class AttachAi extends SpellAiLogic { } } else if (keyword.equals("Trample")) { if (card.getNetCombatDamage() <= 1 - || !CombatUtil.canBeBlocked(card) + || !CombatUtil.canBeBlocked(card) || !CombatUtil.canAttackNextTurn(card)) { return false; } @@ -897,5 +900,5 @@ public class AttachAi extends SpellAiLogic { } return true; } - -} \ No newline at end of file + +}