From 28c64e19f93119c21e911f848216d4461aa0f711 Mon Sep 17 00:00:00 2001 From: slapshot5 Date: Thu, 1 Sep 2011 04:00:52 +0000 Subject: [PATCH] Checkstyle fixes in AF_Protection --- .../AbilityFactory_Protection.java | 557 ++++++++++-------- 1 file changed, 313 insertions(+), 244 deletions(-) diff --git a/src/main/java/forge/card/abilityFactory/AbilityFactory_Protection.java b/src/main/java/forge/card/abilityFactory/AbilityFactory_Protection.java index 67009b12c8a..9d32a1de3c3 100644 --- a/src/main/java/forge/card/abilityFactory/AbilityFactory_Protection.java +++ b/src/main/java/forge/card/abilityFactory/AbilityFactory_Protection.java @@ -20,7 +20,11 @@ import javax.swing.JOptionPane; * @author dennis.r.friedrichsen (slapshot5 on slightlymagic.net) * @version $Id$ */ -public class AbilityFactory_Protection { +public final class AbilityFactory_Protection { + + private AbilityFactory_Protection() { + throw new AssertionError(); + } /** *

getSpellProtection.

@@ -30,7 +34,7 @@ public class AbilityFactory_Protection { */ public static SpellAbility createSpellProtection(final AbilityFactory af) { SpellAbility spProtect = new Spell(af.getHostCard(), af.getAbCost(), af.getAbTgt()) { - private static final long serialVersionUID = 4678736312735724916L; + private static final long serialVersionUID = 4678736312735724916L; @Override public boolean canPlayAI() { @@ -45,8 +49,8 @@ public class AbilityFactory_Protection { @Override public void resolve() { protectResolve(af, this); - }//resolve - };//SpellAbility + } //resolve + }; //SpellAbility return spProtect; } @@ -74,15 +78,15 @@ public class AbilityFactory_Protection { @Override public void resolve() { protectResolve(af, this); - }//resolve() + } //resolve() @Override - public boolean doTrigger(boolean mandatory) { + public boolean doTrigger(final boolean mandatory) { return protectTriggerAI(af, this, mandatory); } - };//SpellAbility + }; //SpellAbility return abProtect; } @@ -110,7 +114,7 @@ public class AbilityFactory_Protection { @Override public void resolve() { protectResolve(af, this); - }//resolve + } //resolve @Override public boolean chkAI_Drawback() { @@ -118,38 +122,42 @@ public class AbilityFactory_Protection { } @Override - public boolean doTrigger(boolean mandatory) { + public boolean doTrigger(final boolean mandatory) { return protectTriggerAI(af, this, mandatory); } - };//SpellAbility + }; //SpellAbility return dbProtect; } - - private static boolean hasProtectionFrom(Card card, String color) { + + private static boolean hasProtectionFrom(final Card card, final String color) { ArrayList onlyColors = new ArrayList(Arrays.asList(Constant.Color.onlyColors)); - + //make sure we have a valid color - if(!onlyColors.contains(color)) return false; - + if (!onlyColors.contains(color)) { + return false; + } + String protection = "Protection from " + color; - if(card.hasKeyword(protection)) return true; - else return false; + + return card.hasKeyword(protection); } - - private static boolean hasProtectionFromAny(Card card, ArrayList colors) { + + private static boolean hasProtectionFromAny(final Card card, final ArrayList colors) { boolean protect = false; - for(String color : colors) { + for (String color : colors) { protect |= hasProtectionFrom(card, color); } return protect; } - - private static boolean hasProtectionFromAll(Card card, ArrayList colors) { + + private static boolean hasProtectionFromAll(final Card card, final ArrayList colors) { boolean protect = true; - if(colors.size() < 1) return false; - - for(String color : colors) { + if (colors.size() < 1) { + return false; + } + + for (String color : colors) { protect &= hasProtectionFrom(card, color); } return protect; @@ -161,40 +169,51 @@ public class AbilityFactory_Protection { * @param af a {@link forge.card.abilityFactory.AbilityFactory} object. * @return a {@link forge.CardList} object. */ - private static CardList getProtectCreatures(AbilityFactory af, SpellAbility sa) { + private static CardList getProtectCreatures(final AbilityFactory af, final SpellAbility sa) { final Card hostCard = af.getHostCard(); final ArrayList gains = getProtectionList(hostCard, af.getMapParams()); CardList list = AllZoneUtil.getCreaturesInPlay(AllZone.getComputerPlayer()); list = list.filter(new CardListFilter() { - public boolean addCard(Card c) { - if(!CardFactoryUtil.canTarget(hostCard, c)) + public boolean addCard(final Card c) { + if (!CardFactoryUtil.canTarget(hostCard, c)) { return false; + } //Don't add duplicate protections - if(hasProtectionFromAll(c, gains)) return false; + if (hasProtectionFromAll(c, gains)) { + return false; + } //will the creature attack (only relevant for sorcery speed)? - if(CardFactoryUtil.AI_doesCreatureAttack(c) && AllZone.getPhase().isBefore(Constant.Phase.Combat_Declare_Attackers) + if (CardFactoryUtil.AI_doesCreatureAttack(c) + && AllZone.getPhase().isBefore(Constant.Phase.Combat_Declare_Attackers) && AllZone.getPhase().isPlayerTurn(AllZone.getComputerPlayer())) + { return true; + } //is the creature blocking and unable to destroy the attacker or would be destroyed itself? - if(c.isBlocking() && (CombatUtil.blockerWouldBeDestroyed(c) + if (c.isBlocking() && (CombatUtil.blockerWouldBeDestroyed(c) || CombatUtil.attackerWouldBeDestroyed(AllZone.getCombat().getAttackerBlockedBy(c)))) + { return true; + } //is the creature in blocked and the blocker would survive - if(AllZone.getPhase().isAfter(Constant.Phase.Combat_Declare_Blockers) && AllZone.getCombat().isAttacking(c) + if (AllZone.getPhase().isAfter(Constant.Phase.Combat_Declare_Blockers) + && AllZone.getCombat().isAttacking(c) && AllZone.getCombat().isBlocked(c) && CombatUtil.blockerWouldBeDestroyed(AllZone.getCombat().getBlockers(c).get(0))) + { return true; + } return false; } }); return list; - }//getProtectCreatures() + } //getProtectCreatures() /** *

protectCanPlayAI.

@@ -203,57 +222,64 @@ public class AbilityFactory_Protection { * @param sa a {@link forge.card.spellability.SpellAbility} object. * @return a boolean. */ - private static boolean protectCanPlayAI(AbilityFactory af, SpellAbility sa) { - HashMap params = af.getMapParams(); + private static boolean protectCanPlayAI(final AbilityFactory af, final SpellAbility sa) { + HashMap params = af.getMapParams(); Card hostCard = af.getHostCard(); // if there is no target and host card isn't in play, don't activate - if(af.getAbTgt() == null && !AllZoneUtil.isCardInPlay(hostCard)) + if (af.getAbTgt() == null && !AllZoneUtil.isCardInPlay(hostCard)) { return false; + } Cost cost = sa.getPayCosts(); - - // temporarily disabled until better AI - if (!CostUtil.checkLifeCost(cost, hostCard, 4)) - return false; - if (!CostUtil.checkDiscardCost(cost, hostCard)) + // temporarily disabled until better AI + if (!CostUtil.checkLifeCost(cost, hostCard, 4)) { return false; - - if (!CostUtil.checkCreatureSacrificeCost(cost, hostCard)) + } + + if (!CostUtil.checkDiscardCost(cost, hostCard)) { return false; - - if (!CostUtil.checkRemoveCounterCost(cost, hostCard)) + } + + if (!CostUtil.checkCreatureSacrificeCost(cost, hostCard)) { return false; + } + + if (!CostUtil.checkRemoveCounterCost(cost, hostCard)) { + return false; + } // Phase Restrictions - if(AllZone.getStack().size() == 0 && AllZone.getPhase().isBefore(Constant.Phase.Combat_FirstStrikeDamage)) { + if (AllZone.getStack().size() == 0 && AllZone.getPhase().isBefore(Constant.Phase.Combat_FirstStrikeDamage)) { // Instant-speed protections should not be cast outside of combat when the stack is empty - if(!AbilityFactory.isSorcerySpeed(sa)) - return false; + if (!AbilityFactory.isSorcerySpeed(sa)) { + return false; + } } - else if(AllZone.getStack().size() > 0) { - // TODO: protection something only if the top thing on the stack will kill it via damage or destroy + else if (AllZone.getStack().size() > 0) { + // TODO protection something only if the top thing on the stack will kill it via damage or destroy return false; } - if(af.getAbTgt() == null || !af.getAbTgt().doesTarget()) { + if (af.getAbTgt() == null || !af.getAbTgt().doesTarget()) { ArrayList cards = AbilityFactory.getDefinedCards(sa.getSourceCard(), params.get("Defined"), sa); - if(cards.size() == 0) - return false; + if (cards.size() == 0) { + return false; + } /* // when this happens we need to expand AI to consider if its ok for everything? for (Card card : cards) { - // TODO: if AI doesn't control Card and Pump is a Curse, than maybe use? + // TODO if AI doesn't control Card and Pump is a Curse, than maybe use? }*/ + } else { + return protectTgtAI(af, sa, false); } - else - return protectTgtAI(af, sa, false); return false; - }//protectPlayAI() + } //protectPlayAI() /** *

protectTgtAI.

@@ -263,10 +289,11 @@ public class AbilityFactory_Protection { * @param mandatory a boolean. * @return a boolean. */ - private static boolean protectTgtAI(AbilityFactory af, SpellAbility sa, boolean mandatory) { - if (!mandatory && AllZone.getPhase().isAfter(Constant.Phase.Combat_Declare_Blockers_InstantAbility)) + private static boolean protectTgtAI(final AbilityFactory af, final SpellAbility sa, final boolean mandatory) { + if (!mandatory && AllZone.getPhase().isAfter(Constant.Phase.Combat_Declare_Blockers_InstantAbility)) { return false; - + } + Card source = sa.getSourceCard(); Target tgt = af.getAbTgt(); @@ -274,7 +301,7 @@ public class AbilityFactory_Protection { CardList list = getProtectCreatures(af, sa); list = list.getValidCards(tgt.getValidTgts(), sa.getActivatingPlayer(), sa.getSourceCard()); - + /* * TODO - What this should probably do is if it's time for instants and abilities after Human * declares attackers, determine desired protection before assigning blockers. @@ -284,27 +311,34 @@ public class AbilityFactory_Protection { * Or, add protection (to make it unblockable) when Compy is attacking. */ - if(AllZone.getStack().size() == 0) { + if (AllZone.getStack().size() == 0) { // If the cost is tapping, don't activate before declare attack/block - if(sa.getPayCosts() != null && sa.getPayCosts().getTap()) { - if(AllZone.getPhase().isBefore(Constant.Phase.Combat_Declare_Attackers) && AllZone.getPhase().isPlayerTurn(AllZone.getComputerPlayer())) + if (sa.getPayCosts() != null && sa.getPayCosts().getTap()) { + if (AllZone.getPhase().isBefore(Constant.Phase.Combat_Declare_Attackers) + && AllZone.getPhase().isPlayerTurn(AllZone.getComputerPlayer())) { list.remove(sa.getSourceCard()); - if(AllZone.getPhase().isBefore(Constant.Phase.Combat_Declare_Blockers) && AllZone.getPhase().isPlayerTurn(AllZone.getHumanPlayer())) + } + if (AllZone.getPhase().isBefore(Constant.Phase.Combat_Declare_Blockers) + && AllZone.getPhase().isPlayerTurn(AllZone.getHumanPlayer())) + { list.remove(sa.getSourceCard()); + } } } - if(list.isEmpty()) + if (list.isEmpty()) { return mandatory && protectMandatoryTarget(af, sa, mandatory); + } - while(tgt.getNumTargeted() < tgt.getMaxTargets(source, sa)) { + while (tgt.getNumTargeted() < tgt.getMaxTargets(source, sa)) { Card t = null; //boolean goodt = false; - if(list.isEmpty()) { - if(tgt.getNumTargeted() < tgt.getMinTargets(source, sa) || tgt.getNumTargeted() == 0) { - if(mandatory) + if (list.isEmpty()) { + if (tgt.getNumTargeted() < tgt.getMinTargets(source, sa) || tgt.getNumTargeted() == 0) { + if (mandatory) { return protectMandatoryTarget(af, sa, mandatory); + } tgt.resetTargets(); return false; @@ -321,7 +355,7 @@ public class AbilityFactory_Protection { } return true; - }//protectTgtAI() + } //protectTgtAI() /** *

protectMandatoryTarget.

@@ -331,90 +365,99 @@ public class AbilityFactory_Protection { * @param mandatory a boolean. * @return a boolean. */ - private static boolean protectMandatoryTarget(AbilityFactory af, SpellAbility sa, boolean mandatory) { - final HashMap params = af.getMapParams(); + private static boolean protectMandatoryTarget(final AbilityFactory af, final SpellAbility sa, + final boolean mandatory) + { + final HashMap params = af.getMapParams(); final Card host = af.getHostCard(); - + CardList list = AllZoneUtil.getCardsInPlay(); Target tgt = sa.getTarget(); list = list.getValidCards(tgt.getValidTgts(), sa.getActivatingPlayer(), sa.getSourceCard()); - if(list.size() < tgt.getMinTargets(sa.getSourceCard(), sa)) { + if (list.size() < tgt.getMinTargets(sa.getSourceCard(), sa)) { tgt.resetTargets(); return false; } // Remove anything that's already been targeted - for(Card c : tgt.getTargetCards()) + for (Card c : tgt.getTargetCards()) { list.remove(c); + } CardList pref = list.getController(AllZone.getComputerPlayer()); pref = pref.filter(new CardListFilter() { - public boolean addCard(Card c) { + public boolean addCard(final Card c) { return !hasProtectionFromAll(c, getProtectionList(host, params)); } }); CardList pref2 = list.getController(AllZone.getComputerPlayer()); pref = pref.filter(new CardListFilter() { - public boolean addCard(Card c) { + public boolean addCard(final Card c) { return !hasProtectionFromAny(c, getProtectionList(host, params)); } }); CardList forced = list.getController(AllZone.getHumanPlayer()); Card source = sa.getSourceCard(); - while(tgt.getNumTargeted() < tgt.getMaxTargets(source, sa)) { - if(pref.isEmpty()) + while (tgt.getNumTargeted() < tgt.getMaxTargets(source, sa)) { + if (pref.isEmpty()) { break; + } Card c; - if(pref.getNotType("Creature").size() == 0) + if (pref.getNotType("Creature").size() == 0) { c = CardFactoryUtil.AI_getBestCreature(pref); - else + } else { c = CardFactoryUtil.AI_getMostExpensivePermanent(pref, source, true); + } pref.remove(c); tgt.addTarget(c); } - - while(tgt.getNumTargeted() < tgt.getMaxTargets(source, sa)) { - if(pref2.isEmpty()) + + while (tgt.getNumTargeted() < tgt.getMaxTargets(source, sa)) { + if (pref2.isEmpty()) { break; + } Card c; - if(pref2.getNotType("Creature").size() == 0) + if (pref2.getNotType("Creature").size() == 0) { c = CardFactoryUtil.AI_getBestCreature(pref2); - else + } else { c = CardFactoryUtil.AI_getMostExpensivePermanent(pref2, source, true); + } pref2.remove(c); tgt.addTarget(c); } - while(tgt.getNumTargeted() < tgt.getMinTargets(source, sa)) { - if(forced.isEmpty()) + while (tgt.getNumTargeted() < tgt.getMinTargets(source, sa)) { + if (forced.isEmpty()) { break; + } Card c; - if(forced.getNotType("Creature").size() == 0) + if (forced.getNotType("Creature").size() == 0) { c = CardFactoryUtil.AI_getWorstCreature(forced); - else + } else { c = CardFactoryUtil.AI_getCheapestPermanent(forced, source, true); + } forced.remove(c); tgt.addTarget(c); } - if(tgt.getNumTargeted() < tgt.getMinTargets(sa.getSourceCard(), sa)) { + if (tgt.getNumTargeted() < tgt.getMinTargets(sa.getSourceCard(), sa)) { tgt.resetTargets(); return false; } return true; - }//protectMandatoryTarget() + } //protectMandatoryTarget() /** *

protectTriggerAI.

@@ -424,20 +467,22 @@ public class AbilityFactory_Protection { * @param mandatory a boolean. * @return a boolean. */ - private static boolean protectTriggerAI(AbilityFactory af, SpellAbility sa, boolean mandatory) { - if(!ComputerUtil.canPayCost(sa)) + private static boolean protectTriggerAI(final AbilityFactory af, final SpellAbility sa, final boolean mandatory) { + if (!ComputerUtil.canPayCost(sa)) { return false; + } - if(sa.getTarget() == null) { - if(mandatory) + if (sa.getTarget() == null) { + if (mandatory) { return true; + } } else { return protectTgtAI(af, sa, mandatory); } return true; - }//protectTriggerAI + } //protectTriggerAI /** *

protectDrawbackAI.

@@ -446,19 +491,19 @@ public class AbilityFactory_Protection { * @param sa a {@link forge.card.spellability.SpellAbility} object. * @return a boolean. */ - private static boolean protectDrawbackAI(AbilityFactory af, SpellAbility sa) { + private static boolean protectDrawbackAI(final AbilityFactory af, final SpellAbility sa) { Card host = af.getHostCard(); - if(af.getAbTgt() == null || !af.getAbTgt().doesTarget()) { - if(host.isCreature()) { + if (af.getAbTgt() == null || !af.getAbTgt().doesTarget()) { + if (host.isCreature()) { //TODO } - } - else + } else { return protectTgtAI(af, sa, false); + } return true; - }//protectDrawbackAI() + } //protectDrawbackAI() /** *

protectStackDescription.

@@ -467,8 +512,8 @@ public class AbilityFactory_Protection { * @param sa a {@link forge.card.spellability.SpellAbility} object. * @return a {@link java.lang.String} object. */ - private static String protectStackDescription(AbilityFactory af, SpellAbility sa) { - HashMap params = af.getMapParams(); + private static String protectStackDescription(final AbilityFactory af, final SpellAbility sa) { + HashMap params = af.getMapParams(); Card host = af.getHostCard(); final ArrayList gains = getProtectionList(host, params); @@ -479,56 +524,70 @@ public class AbilityFactory_Protection { ArrayList tgtCards; Target tgt = af.getAbTgt(); - if(tgt != null) - tgtCards = tgt.getTargetCards(); - else - tgtCards = AbilityFactory.getDefinedCards(sa.getSourceCard(), params.get("Defined"), sa); + if (tgt != null) { + tgtCards = tgt.getTargetCards(); + } else { + tgtCards = AbilityFactory.getDefinedCards(sa.getSourceCard(), params.get("Defined"), sa); + } - if(tgtCards.size() > 0) { + if (tgtCards.size() > 0) { - if(sa instanceof Ability_Sub) - sb.append(" "); - else - sb.append(host).append(" - "); + if (sa instanceof Ability_Sub) { + sb.append(" "); + } else { + sb.append(host).append(" - "); + } Iterator it = tgtCards.iterator(); while (it.hasNext()) { Card tgtC = it.next(); - if (tgtC.isFaceDown()) sb.append("Morph"); - else sb.append(tgtC); + if (tgtC.isFaceDown()) { + sb.append("Morph"); + } else { + sb.append(tgtC); + } - if (it.hasNext()) sb.append(", "); + if (it.hasNext()) { + sb.append(", "); + } } sb.append(" gain"); - if(tgtCards.size() == 1) sb.append("s"); + if (tgtCards.size() == 1) { + sb.append("s"); + } sb.append(" protection from "); - - if(choose) sb.append("your choice of "); - for(int i = 0; i < gains.size(); i++) { - if (i != 0) - sb.append(", "); - - if (i == gains.size() - 1) - sb.append(joiner).append(" "); - + if (choose) { + sb.append("your choice of "); + } + + for (int i = 0; i < gains.size(); i++) { + if (i != 0) { + sb.append(", "); + } + + if (i == gains.size() - 1) { + sb.append(joiner).append(" "); + } + sb.append(gains.get(i)); } - if(!params.containsKey("Permanent")) - sb.append(" until end of turn"); + if (!params.containsKey("Permanent")) { + sb.append(" until end of turn"); + } sb.append("."); } Ability_Sub abSub = sa.getSubAbility(); - if(abSub != null) { + if (abSub != null) { sb.append(abSub.getStackDescription()); } return sb.toString(); - }//protectStackDescription() + } //protectStackDescription() /** *

protectResolve.

@@ -536,18 +595,20 @@ public class AbilityFactory_Protection { * @param af a {@link forge.card.abilityFactory.AbilityFactory} object. * @param sa a {@link forge.card.spellability.SpellAbility} object. */ - private static void protectResolve(AbilityFactory af, SpellAbility sa) { - HashMap params = af.getMapParams(); + private static void protectResolve(final AbilityFactory af, final SpellAbility sa) { + HashMap params = af.getMapParams(); final Card host = af.getHostCard(); - + boolean isChoice = params.get("Gains").contains("Choice"); ArrayList choices = getProtectionList(host, params); final ArrayList gains = new ArrayList(); - if(isChoice) { - if(sa.getActivatingPlayer().isHuman()) { + if (isChoice) { + if (sa.getActivatingPlayer().isHuman()) { Object o = GuiUtils.getChoice("Choose a protection", choices.toArray()); - if(null == o) return; + if (null == o) { + return; + } String choice = (String) o; gains.add(choice); } @@ -555,15 +616,15 @@ public class AbilityFactory_Protection { //TODO - needs improvement String choice = choices.get(0); gains.add(choice); - JOptionPane.showMessageDialog(null, "Computer chooses "+gains, ""+host, JOptionPane.PLAIN_MESSAGE); + JOptionPane.showMessageDialog(null, "Computer chooses " + gains, "" + host, JOptionPane.PLAIN_MESSAGE); } + } else { + gains.addAll(choices); } - else - gains.addAll(choices); - + ArrayList tgtCards; Target tgt = af.getAbTgt(); - if(tgt != null) { + if (tgt != null) { tgtCards = tgt.getTargetCards(); } else { @@ -571,76 +632,68 @@ public class AbilityFactory_Protection { } int size = tgtCards.size(); - for(int j = 0; j < size; j++) { + for (int j = 0; j < size; j++) { final Card tgtC = tgtCards.get(j); // only pump things in play - if(!AllZoneUtil.isCardInPlay(tgtC)) + if (!AllZoneUtil.isCardInPlay(tgtC)) { continue; - - // if this is a target, make sure we can still target now - if(tgt != null && !CardFactoryUtil.canTarget(host, tgtC)) - continue; - - for(String gain : gains) { - tgtC.addExtrinsicKeyword("Protection from "+gain); } - if(!params.containsKey("Permanent")) { + // if this is a target, make sure we can still target now + if (tgt != null && !CardFactoryUtil.canTarget(host, tgtC)) { + continue; + } + + for (String gain : gains) { + tgtC.addExtrinsicKeyword("Protection from " + gain); + } + + if (!params.containsKey("Permanent")) { // If not Permanent, remove protection at EOT final Command untilEOT = new Command() { private static final long serialVersionUID = 7682700789217703789L; public void execute() { - if(AllZoneUtil.isCardInPlay(tgtC)) { + if (AllZoneUtil.isCardInPlay(tgtC)) { for (String gain : gains) { - tgtC.removeExtrinsicKeyword("Protection from "+gain); + tgtC.removeExtrinsicKeyword("Protection from " + gain); } } } }; - if(params.containsKey("UntilEndOfCombat")) AllZone.getEndOfCombat().addUntil(untilEOT); - else AllZone.getEndOfTurn().addUntil(untilEOT); + if (params.containsKey("UntilEndOfCombat")) { + AllZone.getEndOfCombat().addUntil(untilEOT); + } else { + AllZone.getEndOfTurn().addUntil(untilEOT); + } } } - }//protectResolve() - - private static ArrayList getProtectionList(Card host, HashMap params) { + } //protectResolve() + + private static ArrayList getProtectionList(final Card host, final HashMap params) { final ArrayList gains = new ArrayList(); - - String gainStr = params.get("Gains"); - if(gainStr.equals("Choice")) { + + String gainStr = params.get("Gains"); + if (gainStr.equals("Choice")) { String choices = params.get("Choices"); // Replace AnyColor with the 5 colors - if (choices.contains("AnyColor")){ + if (choices.contains("AnyColor")) { gains.addAll(Arrays.asList(Constant.Color.onlyColors)); choices = choices.replaceAll("AnyColor,?", ""); } // Add any remaining choices - if (choices.length() > 0) - gains.addAll(Arrays.asList(choices.split(","))); + if (choices.length() > 0) { + gains.addAll(Arrays.asList(choices.split(","))); + } } else { gains.addAll(Arrays.asList(gainStr.split(","))); } return gains; } - - - - - - - - - - - - - - - + // ************************************************************************* // ************************** ProtectionAll ******************************** // ************************************************************************* @@ -667,8 +720,8 @@ public class AbilityFactory_Protection { @Override public void resolve() { protectAllResolve(af, this); - }//resolve - };//SpellAbility + } //resolve + }; //SpellAbility return spProtectAll; } @@ -696,15 +749,15 @@ public class AbilityFactory_Protection { @Override public void resolve() { protectAllResolve(af, this); - }//resolve() + } //resolve() @Override - public boolean doTrigger(boolean mandatory) { + public boolean doTrigger(final boolean mandatory) { return protectAllTriggerAI(af, this, mandatory); } - };//SpellAbility + }; //SpellAbility return abProtectAll; } @@ -732,7 +785,7 @@ public class AbilityFactory_Protection { @Override public void resolve() { protectAllResolve(af, this); - }//resolve + } //resolve @Override public boolean chkAI_Drawback() { @@ -740,10 +793,10 @@ public class AbilityFactory_Protection { } @Override - public boolean doTrigger(boolean mandatory) { + public boolean doTrigger(final boolean mandatory) { return protectAllTriggerAI(af, this, mandatory); } - };//SpellAbility + }; //SpellAbility return dbProtectAll; } @@ -755,29 +808,34 @@ public class AbilityFactory_Protection { * @param sa a {@link forge.card.spellability.SpellAbility} object. * @return a boolean. */ - private static boolean protectAllCanPlayAI(AbilityFactory af, SpellAbility sa) { + private static boolean protectAllCanPlayAI(final AbilityFactory af, final SpellAbility sa) { Card hostCard = af.getHostCard(); // if there is no target and host card isn't in play, don't activate - if(af.getAbTgt() == null && !AllZoneUtil.isCardInPlay(hostCard)) + if (af.getAbTgt() == null && !AllZoneUtil.isCardInPlay(hostCard)) { return false; + } Cost cost = sa.getPayCosts(); - - // temporarily disabled until better AI - if (!CostUtil.checkLifeCost(cost, hostCard, 4)) - return false; - if (!CostUtil.checkDiscardCost(cost, hostCard)) + // temporarily disabled until better AI + if (!CostUtil.checkLifeCost(cost, hostCard, 4)) { return false; - - if (!CostUtil.checkSacrificeCost(cost, hostCard)) + } + + if (!CostUtil.checkDiscardCost(cost, hostCard)) { return false; - - if (!CostUtil.checkRemoveCounterCost(cost, hostCard)) + } + + if (!CostUtil.checkSacrificeCost(cost, hostCard)) { return false; + } + + if (!CostUtil.checkRemoveCounterCost(cost, hostCard)) { + return false; + } return false; - }//protectAllCanPlayAI() + } //protectAllCanPlayAI() /** *

protectAllTriggerAI.

@@ -787,12 +845,15 @@ public class AbilityFactory_Protection { * @param mandatory a boolean. * @return a boolean. */ - private static boolean protectAllTriggerAI(AbilityFactory af, SpellAbility sa, boolean mandatory) { - if(!ComputerUtil.canPayCost(sa)) + private static boolean protectAllTriggerAI(final AbilityFactory af, final SpellAbility sa, + final boolean mandatory) + { + if (!ComputerUtil.canPayCost(sa)) { return false; + } return true; - }//protectAllTriggerAI + } //protectAllTriggerAI /** *

protectAllDrawbackAI.

@@ -801,9 +862,9 @@ public class AbilityFactory_Protection { * @param sa a {@link forge.card.spellability.SpellAbility} object. * @return a boolean. */ - private static boolean protectAllDrawbackAI(AbilityFactory af, SpellAbility sa) { + private static boolean protectAllDrawbackAI(final AbilityFactory af, final SpellAbility sa) { return protectAllTriggerAI(af, sa, false); - }//protectAllDrawbackAI() + } //protectAllDrawbackAI() /** *

protectAllStackDescription.

@@ -812,43 +873,46 @@ public class AbilityFactory_Protection { * @param sa a {@link forge.card.spellability.SpellAbility} object. * @return a {@link java.lang.String} object. */ - private static String protectAllStackDescription(AbilityFactory af, SpellAbility sa) { - HashMap params = af.getMapParams(); + private static String protectAllStackDescription(final AbilityFactory af, final SpellAbility sa) { + HashMap params = af.getMapParams(); Card host = af.getHostCard(); StringBuilder sb = new StringBuilder(); ArrayList tgtCards; Target tgt = af.getAbTgt(); - if(tgt != null) - tgtCards = tgt.getTargetCards(); - else - tgtCards = AbilityFactory.getDefinedCards(sa.getSourceCard(), params.get("Defined"), sa); + if (tgt != null) { + tgtCards = tgt.getTargetCards(); + } else { + tgtCards = AbilityFactory.getDefinedCards(sa.getSourceCard(), params.get("Defined"), sa); + } - if(tgtCards.size() > 0) { + if (tgtCards.size() > 0) { - if(sa instanceof Ability_Sub) - sb.append(" "); - else - sb.append(host).append(" - "); + if (sa instanceof Ability_Sub) { + sb.append(" "); + } else { + sb.append(host).append(" - "); + } if (params.containsKey("SpellDescription")) { sb.append(params.get("SpellDescription")); } else { sb.append("Valid card gain protection"); - if(!params.containsKey("Permanent")) - sb.append(" until end of turn"); + if (!params.containsKey("Permanent")) { + sb.append(" until end of turn"); + } sb.append("."); } } Ability_Sub abSub = sa.getSubAbility(); - if(abSub != null) { + if (abSub != null) { sb.append(abSub.getStackDescription()); } return sb.toString(); - }//protectStackDescription() + } //protectStackDescription() /** *

protectAllResolve.

@@ -856,18 +920,20 @@ public class AbilityFactory_Protection { * @param af a {@link forge.card.abilityFactory.AbilityFactory} object. * @param sa a {@link forge.card.spellability.SpellAbility} object. */ - private static void protectAllResolve(AbilityFactory af, SpellAbility sa) { - HashMap params = af.getMapParams(); + private static void protectAllResolve(final AbilityFactory af, final SpellAbility sa) { + HashMap params = af.getMapParams(); final Card host = af.getHostCard(); - + boolean isChoice = params.get("Gains").contains("Choice"); ArrayList choices = getProtectionList(host, params); final ArrayList gains = new ArrayList(); - if(isChoice) { - if(sa.getActivatingPlayer().isHuman()) { + if (isChoice) { + if (sa.getActivatingPlayer().isHuman()) { Object o = GuiUtils.getChoice("Choose a protection", choices.toArray()); - if(null == o) return; + if (null == o) { + return; + } String choice = (String) o; gains.add(choice); } @@ -875,41 +941,44 @@ public class AbilityFactory_Protection { //TODO - needs improvement String choice = choices.get(0); gains.add(choice); - JOptionPane.showMessageDialog(null, "Computer chooses "+gains, ""+host, JOptionPane.PLAIN_MESSAGE); + JOptionPane.showMessageDialog(null, "Computer chooses " + gains, "" + host, JOptionPane.PLAIN_MESSAGE); } + } else { + gains.addAll(choices); } - else - gains.addAll(choices); - + String valid = params.get("ValidCards"); CardList list = AllZoneUtil.getCardsInPlay(); list = list.getValidCards(valid, sa.getActivatingPlayer(), host); - - for(final Card tgtC : list) { - if(AllZoneUtil.isCardInPlay(tgtC)) { - for(String gain : gains) { - tgtC.addExtrinsicKeyword("Protection from "+gain); + + for (final Card tgtC : list) { + if (AllZoneUtil.isCardInPlay(tgtC)) { + for (String gain : gains) { + tgtC.addExtrinsicKeyword("Protection from " + gain); } - if(!params.containsKey("Permanent")) { + if (!params.containsKey("Permanent")) { // If not Permanent, remove protection at EOT final Command untilEOT = new Command() { private static final long serialVersionUID = -6573962672873853565L; public void execute() { - if(AllZoneUtil.isCardInPlay(tgtC)) { + if (AllZoneUtil.isCardInPlay(tgtC)) { for (String gain : gains) { - tgtC.removeExtrinsicKeyword("Protection from "+gain); + tgtC.removeExtrinsicKeyword("Protection from " + gain); } } } }; - if(params.containsKey("UntilEndOfCombat")) AllZone.getEndOfCombat().addUntil(untilEOT); - else AllZone.getEndOfTurn().addUntil(untilEOT); + if (params.containsKey("UntilEndOfCombat")) { + AllZone.getEndOfCombat().addUntil(untilEOT); + } else { + AllZone.getEndOfTurn().addUntil(untilEOT); + } } } } - }//protectAllResolve() - -}//end class AbilityFactory_Protection + } //protectAllResolve() + +} //end class AbilityFactory_Protection