Checkstyle fixes in AF_Protection

This commit is contained in:
slapshot5
2011-09-01 04:00:52 +00:00
parent cc8e3967a9
commit 28c64e19f9

View File

@@ -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();
}
/**
* <p>getSpellProtection.</p>
@@ -77,7 +81,7 @@ public class AbilityFactory_Protection {
} //resolve()
@Override
public boolean doTrigger(boolean mandatory) {
public boolean doTrigger(final boolean mandatory) {
return protectTriggerAI(af, this, mandatory);
}
@@ -118,7 +122,7 @@ public class AbilityFactory_Protection {
}
@Override
public boolean doTrigger(boolean mandatory) {
public boolean doTrigger(final boolean mandatory) {
return protectTriggerAI(af, this, mandatory);
}
}; //SpellAbility
@@ -126,18 +130,20 @@ public class AbilityFactory_Protection {
return dbProtect;
}
private static boolean hasProtectionFrom(Card card, String color) {
private static boolean hasProtectionFrom(final Card card, final String color) {
ArrayList<String> onlyColors = new ArrayList<String>(Arrays.asList(Constant.Color.onlyColors));
//make sure we have a valid color
if(!onlyColors.contains(color)) return false;
String protection = "Protection from " + color;
if(card.hasKeyword(protection)) return true;
else return false;
if (!onlyColors.contains(color)) {
return false;
}
private static boolean hasProtectionFromAny(Card card, ArrayList<String> colors) {
String protection = "Protection from " + color;
return card.hasKeyword(protection);
}
private static boolean hasProtectionFromAny(final Card card, final ArrayList<String> colors) {
boolean protect = false;
for (String color : colors) {
protect |= hasProtectionFrom(card, color);
@@ -145,9 +151,11 @@ public class AbilityFactory_Protection {
return protect;
}
private static boolean hasProtectionFromAll(Card card, ArrayList<String> colors) {
private static boolean hasProtectionFromAll(final Card card, final ArrayList<String> colors) {
boolean protect = true;
if(colors.size() < 1) return false;
if (colors.size() < 1) {
return false;
}
for (String color : colors) {
protect &= hasProtectionFrom(card, color);
@@ -161,34 +169,45 @@ 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<String> 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)
|| 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;
}
@@ -203,54 +222,61 @@ public class AbilityFactory_Protection {
* @param sa a {@link forge.card.spellability.SpellAbility} object.
* @return a boolean.
*/
private static boolean protectCanPlayAI(AbilityFactory af, SpellAbility sa) {
private static boolean protectCanPlayAI(final AbilityFactory af, final SpellAbility sa) {
HashMap<String, String> 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))
if (!CostUtil.checkLifeCost(cost, hostCard, 4)) {
return false;
}
if (!CostUtil.checkDiscardCost(cost, hostCard))
if (!CostUtil.checkDiscardCost(cost, hostCard)) {
return false;
}
if (!CostUtil.checkCreatureSacrificeCost(cost, hostCard))
if (!CostUtil.checkCreatureSacrificeCost(cost, hostCard)) {
return false;
}
if (!CostUtil.checkRemoveCounterCost(cost, hostCard))
if (!CostUtil.checkRemoveCounterCost(cost, hostCard)) {
return false;
}
// Phase Restrictions
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))
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
// 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()) {
ArrayList<Card> cards = AbilityFactory.getDefinedCards(sa.getSourceCard(), params.get("Defined"), sa);
if(cards.size() == 0)
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
} else {
return protectTgtAI(af, sa, false);
}
return false;
} //protectPlayAI()
@@ -263,9 +289,10 @@ 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();
@@ -287,15 +314,21 @@ public class AbilityFactory_Protection {
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 (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)) {
Card t = null;
@@ -303,8 +336,9 @@ public class AbilityFactory_Protection {
if (list.isEmpty()) {
if (tgt.getNumTargeted() < tgt.getMinTargets(source, sa) || tgt.getNumTargeted() == 0) {
if(mandatory)
if (mandatory) {
return protectMandatoryTarget(af, sa, mandatory);
}
tgt.resetTargets();
return false;
@@ -331,7 +365,9 @@ public class AbilityFactory_Protection {
* @param mandatory a boolean.
* @return a boolean.
*/
private static boolean protectMandatoryTarget(AbilityFactory af, SpellAbility sa, boolean mandatory) {
private static boolean protectMandatoryTarget(final AbilityFactory af, final SpellAbility sa,
final boolean mandatory)
{
final HashMap<String, String> params = af.getMapParams();
final Card host = af.getHostCard();
@@ -345,18 +381,19 @@ public class AbilityFactory_Protection {
}
// 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));
}
});
@@ -364,14 +401,16 @@ public class AbilityFactory_Protection {
Card source = sa.getSourceCard();
while (tgt.getNumTargeted() < tgt.getMaxTargets(source, sa)) {
if(pref.isEmpty())
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);
@@ -379,14 +418,16 @@ public class AbilityFactory_Protection {
}
while (tgt.getNumTargeted() < tgt.getMaxTargets(source, sa)) {
if(pref2.isEmpty())
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);
@@ -394,14 +435,16 @@ public class AbilityFactory_Protection {
}
while (tgt.getNumTargeted() < tgt.getMinTargets(source, sa)) {
if(forced.isEmpty())
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);
@@ -424,14 +467,16 @@ 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 (mandatory) {
return true;
}
}
else {
return protectTgtAI(af, sa, mandatory);
}
@@ -446,16 +491,16 @@ 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()) {
//TODO
}
}
else
} else {
return protectTgtAI(af, sa, false);
}
return true;
} //protectDrawbackAI()
@@ -467,7 +512,7 @@ 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) {
private static String protectStackDescription(final AbilityFactory af, final SpellAbility sa) {
HashMap<String, String> params = af.getMapParams();
Card host = af.getHostCard();
@@ -479,45 +524,59 @@ public class AbilityFactory_Protection {
ArrayList<Card> tgtCards;
Target tgt = af.getAbTgt();
if(tgt != null)
if (tgt != null) {
tgtCards = tgt.getTargetCards();
else
} else {
tgtCards = AbilityFactory.getDefinedCards(sa.getSourceCard(), params.get("Defined"), sa);
}
if (tgtCards.size() > 0) {
if(sa instanceof Ability_Sub)
if (sa instanceof Ability_Sub) {
sb.append(" ");
else
} else {
sb.append(host).append(" - ");
}
Iterator<Card> 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 ");
if (choose) {
sb.append("your choice of ");
}
for (int i = 0; i < gains.size(); i++) {
if (i != 0)
if (i != 0) {
sb.append(", ");
}
if (i == gains.size() - 1)
if (i == gains.size() - 1) {
sb.append(joiner).append(" ");
}
sb.append(gains.get(i));
}
if(!params.containsKey("Permanent"))
if (!params.containsKey("Permanent")) {
sb.append(" until end of turn");
}
sb.append(".");
}
@@ -536,7 +595,7 @@ 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) {
private static void protectResolve(final AbilityFactory af, final SpellAbility sa) {
HashMap<String, String> params = af.getMapParams();
final Card host = af.getHostCard();
@@ -547,7 +606,9 @@ public class AbilityFactory_Protection {
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);
}
@@ -557,9 +618,9 @@ public class AbilityFactory_Protection {
gains.add(choice);
JOptionPane.showMessageDialog(null, "Computer chooses " + gains, "" + host, JOptionPane.PLAIN_MESSAGE);
}
}
else
} else {
gains.addAll(choices);
}
ArrayList<Card> tgtCards;
Target tgt = af.getAbTgt();
@@ -575,12 +636,14 @@ public class AbilityFactory_Protection {
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))
if (tgt != null && !CardFactoryUtil.canTarget(host, tgtC)) {
continue;
}
for (String gain : gains) {
tgtC.addExtrinsicKeyword("Protection from " + gain);
@@ -599,13 +662,16 @@ public class AbilityFactory_Protection {
}
}
};
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<String> getProtectionList(Card host, HashMap<String,String> params) {
private static ArrayList<String> getProtectionList(final Card host, final HashMap<String, String> params) {
final ArrayList<String> gains = new ArrayList<String>();
String gainStr = params.get("Gains");
@@ -618,29 +684,16 @@ public class AbilityFactory_Protection {
choices = choices.replaceAll("AnyColor,?", "");
}
// Add any remaining choices
if (choices.length() > 0)
if (choices.length() > 0) {
gains.addAll(Arrays.asList(choices.split(",")));
}
}
else {
gains.addAll(Arrays.asList(gainStr.split(",")));
}
return gains;
}
// *************************************************************************
// ************************** ProtectionAll ********************************
// *************************************************************************
@@ -699,7 +752,7 @@ public class AbilityFactory_Protection {
} //resolve()
@Override
public boolean doTrigger(boolean mandatory) {
public boolean doTrigger(final boolean mandatory) {
return protectAllTriggerAI(af, this, mandatory);
}
@@ -740,7 +793,7 @@ public class AbilityFactory_Protection {
}
@Override
public boolean doTrigger(boolean mandatory) {
public boolean doTrigger(final boolean mandatory) {
return protectAllTriggerAI(af, this, mandatory);
}
}; //SpellAbility
@@ -755,26 +808,31 @@ 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))
if (!CostUtil.checkLifeCost(cost, hostCard, 4)) {
return false;
}
if (!CostUtil.checkDiscardCost(cost, hostCard))
if (!CostUtil.checkDiscardCost(cost, hostCard)) {
return false;
}
if (!CostUtil.checkSacrificeCost(cost, hostCard))
if (!CostUtil.checkSacrificeCost(cost, hostCard)) {
return false;
}
if (!CostUtil.checkRemoveCounterCost(cost, hostCard))
if (!CostUtil.checkRemoveCounterCost(cost, hostCard)) {
return false;
}
return false;
} //protectAllCanPlayAI()
@@ -787,9 +845,12 @@ 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
@@ -801,7 +862,7 @@ 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()
@@ -812,7 +873,7 @@ 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) {
private static String protectAllStackDescription(final AbilityFactory af, final SpellAbility sa) {
HashMap<String, String> params = af.getMapParams();
Card host = af.getHostCard();
@@ -820,24 +881,27 @@ public class AbilityFactory_Protection {
ArrayList<Card> tgtCards;
Target tgt = af.getAbTgt();
if(tgt != null)
if (tgt != null) {
tgtCards = tgt.getTargetCards();
else
} else {
tgtCards = AbilityFactory.getDefinedCards(sa.getSourceCard(), params.get("Defined"), sa);
}
if (tgtCards.size() > 0) {
if(sa instanceof Ability_Sub)
if (sa instanceof Ability_Sub) {
sb.append(" ");
else
} 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"))
if (!params.containsKey("Permanent")) {
sb.append(" until end of turn");
}
sb.append(".");
}
}
@@ -856,7 +920,7 @@ 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) {
private static void protectAllResolve(final AbilityFactory af, final SpellAbility sa) {
HashMap<String, String> params = af.getMapParams();
final Card host = af.getHostCard();
@@ -867,7 +931,9 @@ public class AbilityFactory_Protection {
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);
}
@@ -877,9 +943,9 @@ public class AbilityFactory_Protection {
gains.add(choice);
JOptionPane.showMessageDialog(null, "Computer chooses " + gains, "" + host, JOptionPane.PLAIN_MESSAGE);
}
}
else
} else {
gains.addAll(choices);
}
String valid = params.get("ValidCards");
CardList list = AllZoneUtil.getCardsInPlay();
@@ -905,8 +971,11 @@ public class AbilityFactory_Protection {
}
}
};
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);
}
}
}
}