getProtectionList static method moved to ProtectEffect.java

ProtectEffect uses game.notifyOfValue to avoid direct calls to Gui
This commit is contained in:
Maxmtg
2013-06-24 16:07:57 +00:00
parent 8e8ee4bb5c
commit 69d5be5bd9
6 changed files with 56 additions and 59 deletions

View File

@@ -1,7 +1,6 @@
package forge.card.ability;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -14,7 +13,6 @@ import com.google.common.collect.Iterables;
import forge.Card;
import forge.CardLists;
import forge.CardUtil;
import forge.Constant;
import forge.CounterType;
import forge.ITargetable;
import forge.card.cardfactory.CardFactoryUtil;
@@ -995,30 +993,6 @@ public class AbilityUtils {
return sas;
}
public static ArrayList<String> getProtectionList(final SpellAbility sa) {
final ArrayList<String> gains = new ArrayList<String>();
final String gainStr = sa.getParam("Gains");
if (gainStr.equals("Choice")) {
String choices = sa.getParam("Choices");
// Replace AnyColor with the 5 colors
if (choices.contains("AnyColor")) {
gains.addAll(Constant.Color.ONLY_COLORS);
choices = choices.replaceAll("AnyColor,?", "");
}
// Add any remaining choices
if (choices.length() > 0) {
gains.addAll(Arrays.asList(choices.split(",")));
}
} else {
gains.addAll(Arrays.asList(gainStr.split(",")));
}
return gains;
}
/////////////////////////////////////////////////////////////////////////////////////
//

View File

@@ -10,6 +10,7 @@ import forge.CardLists;
import forge.Constant;
import forge.card.ability.AbilityUtils;
import forge.card.ability.SpellAbilityAi;
import forge.card.ability.effects.ProtectEffect;
import forge.card.cost.Cost;
import forge.card.spellability.SpellAbility;
import forge.card.spellability.TargetRestrictions;
@@ -37,7 +38,7 @@ public class ProtectAi extends SpellAbilityAi {
return card.hasKeyword(protection);
}
private static boolean hasProtectionFromAny(final Card card, final ArrayList<String> colors) {
private static boolean hasProtectionFromAny(final Card card, final Iterable<String> colors) {
boolean protect = false;
for (final String color : colors) {
protect |= hasProtectionFrom(card, color);
@@ -45,16 +46,14 @@ public class ProtectAi extends SpellAbilityAi {
return protect;
}
private static boolean hasProtectionFromAll(final Card card, final ArrayList<String> colors) {
private static boolean hasProtectionFromAll(final Card card, final Iterable<String> colors) {
boolean protect = true;
if (colors.isEmpty()) {
return false;
}
boolean isEmpty = true;
for (final String color : colors) {
protect &= hasProtectionFrom(card, color);
isEmpty = false;
}
return protect;
return protect && !isEmpty;
}
/**
@@ -67,7 +66,7 @@ public class ProtectAi extends SpellAbilityAi {
* @return a {@link forge.CardList} object.
*/
private static List<Card> getProtectCreatures(final Player ai, final SpellAbility sa) {
final ArrayList<String> gains = AbilityUtils.getProtectionList(sa);
final List<String> gains = ProtectEffect.getProtectionList(sa);
final Game game = ai.getGame();
final Combat combat = game.getCombat();
@@ -269,14 +268,14 @@ public class ProtectAi extends SpellAbilityAi {
pref = CardLists.filter(pref, new Predicate<Card>() {
@Override
public boolean apply(final Card c) {
return !hasProtectionFromAll(c, AbilityUtils.getProtectionList(sa));
return !hasProtectionFromAll(c, ProtectEffect.getProtectionList(sa));
}
});
final List<Card> pref2 = CardLists.filterControlledBy(list, ai);
pref = CardLists.filter(pref, new Predicate<Card>() {
@Override
public boolean apply(final Card c) {
return !hasProtectionFromAny(c, AbilityUtils.getProtectionList(sa));
return !hasProtectionFromAny(c, ProtectEffect.getProtectionList(sa));
}
});
final List<Card> forced = CardLists.filterControlledBy(list, ai);

View File

@@ -16,7 +16,7 @@ import forge.game.Game;
import forge.game.player.Player;
import forge.game.zone.ZoneType;
import forge.gui.GuiChoose;
import forge.gui.GuiDialog;
import forge.util.Lang;
public class ProtectAllEffect extends SpellAbilityEffect {
@@ -43,10 +43,11 @@ public class ProtectAllEffect extends SpellAbilityEffect {
final Game game = sa.getActivatingPlayer().getGame();
final boolean isChoice = sa.getParam("Gains").contains("Choice");
final ArrayList<String> choices = AbilityUtils.getProtectionList(sa);
final ArrayList<String> gains = new ArrayList<String>();
final List<String> choices = ProtectEffect.getProtectionList(sa);
final List<String> gains = new ArrayList<String>();
if (isChoice) {
if (sa.getActivatingPlayer().isHuman()) {
Player choser = sa.getActivatingPlayer();
if (choser.isHuman()) {
final String choice = GuiChoose.one("Choose a protection", choices);
if (null == choice) {
return;
@@ -56,8 +57,8 @@ public class ProtectAllEffect extends SpellAbilityEffect {
// TODO - needs improvement
final String choice = choices.get(0);
gains.add(choice);
GuiDialog.message("Computer chooses " + gains, host.toString());
}
game.getAction().nofityOfValue(sa, choser, Lang.joinHomogenous(gains), choser);
} else {
if (sa.getParam("Gains").equals("ChosenColor")) {
for (final String color : host.getChosenColor()) {

View File

@@ -1,6 +1,7 @@
package forge.card.ability.effects;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
@@ -8,7 +9,7 @@ import forge.Card;
import forge.CardLists;
import forge.CardUtil;
import forge.Command;
import forge.card.ability.AbilityUtils;
import forge.Constant;
import forge.card.ability.SpellAbilityEffect;
import forge.card.spellability.SpellAbility;
import forge.card.spellability.TargetRestrictions;
@@ -16,7 +17,7 @@ import forge.game.Game;
import forge.game.ai.ComputerUtilCard;
import forge.game.player.Player;
import forge.gui.GuiChoose;
import forge.gui.GuiDialog;
import forge.util.Lang;
public class ProtectEffect extends SpellAbilityEffect {
@@ -27,7 +28,7 @@ public class ProtectEffect extends SpellAbilityEffect {
@Override
protected String getStackDescription(SpellAbility sa) {
final ArrayList<String> gains = AbilityUtils.getProtectionList(sa);
final List<String> gains = getProtectionList(sa);
final boolean choose = (sa.hasParam("Choices")) ? true : false;
final String joiner = choose ? "or" : "and";
@@ -100,35 +101,32 @@ public class ProtectEffect extends SpellAbilityEffect {
final Game game = sa.getActivatingPlayer().getGame();
final boolean isChoice = sa.getParam("Gains").contains("Choice");
final ArrayList<String> choices = AbilityUtils.getProtectionList(sa);
final ArrayList<String> gains = new ArrayList<String>();
if (isChoice) {
if (sa.getActivatingPlayer().isHuman()) {
final List<String> choices = getProtectionList(sa);
final List<String> gains = new ArrayList<String>();
if (isChoice && !choices.isEmpty()) {
Player choser = sa.getActivatingPlayer();
if (choser.isHuman()) {
final String choice = GuiChoose.one("Choose a protection", choices);
if (null == choice) {
return;
}
gains.add(choice);
} else {
Player ai = sa.getActivatingPlayer();
String choice = choices.get(0);
final String logic = sa.getParam("AILogic");
if (logic == null || logic.equals("MostProminentHumanCreatures")) {
List<Card> list = new ArrayList<Card>();
for (Player opp : ai.getOpponents()) {
for (Player opp : choser.getOpponents()) {
list.addAll(opp.getCreaturesInPlay());
}
if (list.isEmpty()) {
list = CardLists.filterControlledBy(game.getCardsInGame(), ai.getOpponents());
list = CardLists.filterControlledBy(game.getCardsInGame(), choser.getOpponents());
}
if (!list.isEmpty()) {
choice = ComputerUtilCard.getMostProminentColor(list);
}
}
gains.add(choice);
GuiDialog.message("Computer chooses " + gains, host.toString());
}
game.getAction().nofityOfValue(sa, choser, Lang.joinHomogenous(gains), choser);
} else {
if (sa.getParam("Gains").equals("ChosenColor")) {
for (final String color : host.getChosenColor()) {
@@ -220,4 +218,29 @@ public class ProtectEffect extends SpellAbilityEffect {
}
}
} // protectResolve()
public static List<String> getProtectionList(final SpellAbility sa) {
final ArrayList<String> gains = new ArrayList<String>();
final String gainStr = sa.getParam("Gains");
if (gainStr.equals("Choice")) {
String choices = sa.getParam("Choices");
// Replace AnyColor with the 5 colors
if (choices.contains("AnyColor")) {
gains.addAll(Constant.Color.ONLY_COLORS);
choices = choices.replaceAll("AnyColor,?", "");
}
// Add any remaining choices
if (choices.length() > 0) {
gains.addAll(Arrays.asList(choices.split(",")));
}
} else {
gains.addAll(Arrays.asList(gainStr.split(",")));
}
return gains;
}
}

View File

@@ -158,7 +158,4 @@ public abstract class PlayerController {
public abstract boolean chooseBinary(SpellAbility sa, String question, boolean isCoin);
public abstract boolean chooseFilpResult(SpellAbility sa, Player flipper, boolean[] results, boolean call);
}

View File

@@ -680,6 +680,9 @@ public class PlayerControllerHuman extends PlayerController {
return sa.hasParam("NoCall")
? String.format("%s flip comes up %s", Lang.getPossesive(flipper), value)
: String.format("%s %s the flip", flipper, Lang.joinVerb(flipper, value));
case Protection:
String choser = StringUtils.capitalize(mayBeYou(target, player));
return String.format("%s %s protection from %s", choser, Lang.joinVerb(choser, "choose"), value);
default:
return String.format("%s effect's value for %s is %s", sa.getSourceCard().getName(), mayBeYou(target, player), value);
}