Code cleanup

This commit is contained in:
drdev
2013-12-08 23:18:40 +00:00
parent 776ac3ee2b
commit abc2118a77

View File

@@ -157,21 +157,24 @@ public class CostExile extends CostPartWithList {
final Card source = ability.getSourceCard(); final Card source = ability.getSourceCard();
final Game game = activator.getGame(); final Game game = activator.getGame();
String type = this.getType(); String type = this.getType();
List<Card> typeList = new ArrayList<Card>(); List<Card> typeList = new ArrayList<Card>();
if (type.equals("All")) { if (type.equals("All")) {
return true; // this will always work return true; // this will always work
} else if (type.contains("FromTopGrave")) { }
else if (type.contains("FromTopGrave")) {
type = type.replace("FromTopGrave", ""); type = type.replace("FromTopGrave", "");
} }
if (this.getFrom().equals(ZoneType.Stack)) { if (this.getFrom().equals(ZoneType.Stack)) {
for (SpellAbilityStackInstance si : game.getStack()) { for (SpellAbilityStackInstance si : game.getStack()) {
typeList.add(si.getSourceCard()); typeList.add(si.getSourceCard());
} }
} else { }
else {
if (this.sameZone) { if (this.sameZone) {
typeList = new ArrayList<Card>(game.getCardsIn(this.getFrom())); typeList = new ArrayList<Card>(game.getCardsIn(this.getFrom()));
} else { }
else {
typeList = new ArrayList<Card>(activator.getCardsIn(this.getFrom())); typeList = new ArrayList<Card>(activator.getCardsIn(this.getFrom()));
} }
} }
@@ -196,7 +199,8 @@ public class CostExile extends CostPartWithList {
return false; return false;
} }
} }
} else if (!typeList.contains(source)) { }
else if (!typeList.contains(source)) {
return false; return false;
} }
@@ -222,12 +226,13 @@ public class CostExile extends CostPartWithList {
type = type.replace("FromTopGrave", ""); type = type.replace("FromTopGrave", "");
fromTopGrave = true; fromTopGrave = true;
} }
List<Card> list; List<Card> list;
if (this.sameZone) { if (this.sameZone) {
list = new ArrayList<Card>(game.getCardsIn(this.getFrom())); list = new ArrayList<Card>(game.getCardsIn(this.getFrom()));
} else { }
else {
list = new ArrayList<Card>(activator.getCardsIn(this.getFrom())); list = new ArrayList<Card>(activator.getCardsIn(this.getFrom()));
} }
@@ -243,44 +248,44 @@ public class CostExile extends CostPartWithList {
// Generalize this // Generalize this
if (sVar.equals("XChoice")) { if (sVar.equals("XChoice")) {
c = Cost.chooseXValue(source, ability, list.size()); c = Cost.chooseXValue(source, ability, list.size());
} else { }
else {
c = AbilityUtils.calculateAmount(source, amount, ability); c = AbilityUtils.calculateAmount(source, amount, ability);
} }
} }
if (this.payCostFromSource()) if (this.payCostFromSource()) {
return source.getZone() == activator.getZone(from) && GuiDialog.confirm(source, source.getName() + " - Exile?") && executePayment(ability, source); return source.getZone() == activator.getZone(from) && GuiDialog.confirm(source, source.getName() + " - Exile?") && executePayment(ability, source);
}
List<Card> validCards = CardLists.getValidCards(activator.getCardsIn(from), type.split(";"), activator, source); List<Card> validCards = CardLists.getValidCards(activator.getCardsIn(from), type.split(";"), activator, source);
if (this.from == ZoneType.Battlefield || this.from == ZoneType.Hand) { if (this.from == ZoneType.Battlefield || this.from == ZoneType.Hand) {
InputSelectCards inp = new InputSelectCardsFromList(c, c, validCards); InputSelectCards inp = new InputSelectCardsFromList(c, c, validCards);
inp.setMessage("Exile %d card(s) from your" + from ); inp.setMessage("Exile %d card(s) from your" + from);
inp.setCancelAllowed(true); inp.setCancelAllowed(true);
Singletons.getControl().getInputQueue().setInputAndWait(inp); Singletons.getControl().getInputQueue().setInputAndWait(inp);
return !inp.hasCancelled() && executePayment(ability, inp.getSelected()); return !inp.hasCancelled() && executePayment(ability, inp.getSelected());
} }
if (this.from == ZoneType.Stack) return exileFromStack(ability, c); if (this.from == ZoneType.Stack) { return exileFromStack(ability, c); }
if (this.from == ZoneType.Library) return exileFromTop(ability, c); if (this.from == ZoneType.Library) { return exileFromTop(ability, c); }
if (fromTopGrave) return exileFromTopGraveType(ability, c, validCards); if (fromTopGrave) { return exileFromTopGraveType(ability, c, validCards); }
if (!this.sameZone) return exileFromMiscZone(ability, c, validCards); if (!this.sameZone) { return exileFromMiscZone(ability, c, validCards); }
List<Player> players = game.getPlayers(); List<Player> players = game.getPlayers();
List<Player> payableZone = new ArrayList<Player>(); List<Player> payableZone = new ArrayList<Player>();
for (Player p : players) { for (Player p : players) {
List<Card> enoughType = CardLists.filter(list, CardPredicates.isOwner(p)); List<Card> enoughType = CardLists.filter(list, CardPredicates.isOwner(p));
if (enoughType.size() < c) { if (enoughType.size() < c) {
list.removeAll(enoughType); list.removeAll(enoughType);
} else { }
else {
payableZone.add(p); payableZone.add(p);
} }
} }
return exileFromSame(list, c, payableZone); return exileFromSame(list, c, payableZone);
} }
// Inputs // Inputs
// Exile<Num/Type{/TypeDescription}> // Exile<Num/Type{/TypeDescription}>
@@ -288,45 +293,31 @@ public class CostExile extends CostPartWithList {
// ExileFromGrave<Num/Type{/TypeDescription}> // ExileFromGrave<Num/Type{/TypeDescription}>
// ExileFromTop<Num/Type{/TypeDescription}> (of library) // ExileFromTop<Num/Type{/TypeDescription}> (of library)
// ExileSameGrave<Num/Type{/TypeDescription}> // ExileSameGrave<Num/Type{/TypeDescription}>
/**
* TODO: Write javadoc for this type.
*
*/
/**
* TODO: Write javadoc for Constructor.
* @param list
* @param part
* @param payment
* @param sa
* @param nNeeded
* @param payableZone
*/
private boolean exileFromSame(List<Card> list, int nNeeded, List<Player> payableZone) { private boolean exileFromSame(List<Card> list, int nNeeded, List<Player> payableZone) {
if (nNeeded == 0) { if (nNeeded == 0) {
return true; return true;
} }
final Player p = GuiChoose.oneOrNone(String.format("Exile from whose %s?", getFrom()), payableZone); final Player p = GuiChoose.oneOrNone(String.format("Exile from whose %s?", getFrom()), payableZone);
if (p == null) { if (p == null) {
return false; return false;
} }
List<Card> typeList = CardLists.filter(list, CardPredicates.isOwner(p)); List<Card> typeList = CardLists.filter(list, CardPredicates.isOwner(p));
for (int i = 0; i < nNeeded; i++) { for (int i = 0; i < nNeeded; i++) {
if (typeList.isEmpty()) { if (typeList.isEmpty()) {
return false; return false;
} }
final Card c = GuiChoose.oneOrNone("Exile from " + getFrom(), typeList); final Card c = GuiChoose.oneOrNone("Exile from " + getFrom(), typeList);
if (c != null) { if (c != null) {
typeList.remove(c); typeList.remove(c);
executePayment(null, c); executePayment(null, c);
} else { }
else {
return false; return false;
} }
} }
@@ -345,11 +336,11 @@ public class CostExile extends CostPartWithList {
if (nNeeded == 0) { if (nNeeded == 0) {
return true; return true;
} }
final Game game = sa.getActivatingPlayer().getGame(); final Game game = sa.getActivatingPlayer().getGame();
ArrayList<SpellAbility> saList = new ArrayList<SpellAbility>(); ArrayList<SpellAbility> saList = new ArrayList<SpellAbility>();
ArrayList<String> descList = new ArrayList<String>(); ArrayList<String> descList = new ArrayList<String>();
for (SpellAbilityStackInstance si : game.getStack()) { for (SpellAbilityStackInstance si : game.getStack()) {
final Card stC = si.getSourceCard(); final Card stC = si.getSourceCard();
final SpellAbility stSA = si.getSpellAbility().getRootAbility(); final SpellAbility stSA = si.getSpellAbility().getRootAbility();
@@ -362,37 +353,38 @@ public class CostExile extends CostPartWithList {
} }
} }
} }
for (int i = 0; i < nNeeded; i++) { for (int i = 0; i < nNeeded; i++) {
if (saList.isEmpty()) { if (saList.isEmpty()) {
return false; return false;
} }
//Have to use the stack descriptions here because some copied spells have no description otherwise //Have to use the stack descriptions here because some copied spells have no description otherwise
final String o = GuiChoose.oneOrNone("Exile from " + getFrom(), descList); final String o = GuiChoose.oneOrNone("Exile from " + getFrom(), descList);
if (o != null) { if (o != null) {
final SpellAbility toExile = saList.get(descList.indexOf(o)); final SpellAbility toExile = saList.get(descList.indexOf(o));
final Card c = toExile.getSourceCard(); final Card c = toExile.getSourceCard();
saList.remove(toExile); saList.remove(toExile);
descList.remove(o); descList.remove(o);
if (!c.isCopiedSpell()) { if (!c.isCopiedSpell()) {
executePayment(sa, c); executePayment(sa, c);
} else { }
else {
addToList(c); addToList(c);
} }
final SpellAbilityStackInstance si = game.getStack().getInstanceFromSpellAbility(toExile); final SpellAbilityStackInstance si = game.getStack().getInstanceFromSpellAbility(toExile);
game.getStack().remove(si); game.getStack().remove(si);
} else { }
else {
return false; return false;
} }
} }
return true; return true;
} }
private boolean exileFromTop(final SpellAbility sa, final int nNeeded) { private boolean exileFromTop(final SpellAbility sa, final int nNeeded) {
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
sb.append("Exile ").append(nNeeded).append(" cards from the top of your library?"); sb.append("Exile ").append(nNeeded).append(" cards from the top of your library?");
@@ -410,44 +402,43 @@ public class CostExile extends CostPartWithList {
executePayment(sa, itr.next()); executePayment(sa, itr.next());
} }
return true; return true;
} else { }
else {
return false; return false;
} }
} }
private boolean exileFromMiscZone(SpellAbility sa, int nNeeded, List<Card> typeList) {
// ExileFromGrave<Num/Type{/TypeDescription}> for (int i = 0; i < nNeeded; i++) {
if (typeList.isEmpty()) {
private boolean exileFromMiscZone(SpellAbility sa, int nNeeded, List<Card> typeList) { return false;
for (int i = 0; i < nNeeded; i++) {
if (typeList.isEmpty()) {
return false;
}
final Card c = GuiChoose.oneOrNone("Exile from " + getFrom(), typeList);
if (c != null) {
typeList.remove(c);
executePayment(sa, c);
} else {
return false;
}
} }
return true;
}
private boolean exileFromTopGraveType(SpellAbility sa, int nNeeded, List<Card> typeList) { final Card c = GuiChoose.oneOrNone("Exile from " + getFrom(), typeList);
Collections.reverse(typeList);
for (int i = 0; i < nNeeded; i++) { if (c != null) {
if (typeList.isEmpty()) {
return false;
}
final Card c = typeList.get(0);
typeList.remove(c); typeList.remove(c);
executePayment(sa, c); executePayment(sa, c);
} else {
return false;
} }
return true;
} }
return true;
}
private boolean exileFromTopGraveType(SpellAbility sa, int nNeeded, List<Card> typeList) {
Collections.reverse(typeList);
for (int i = 0; i < nNeeded; i++) {
if (typeList.isEmpty()) {
return false;
}
final Card c = typeList.get(0);
typeList.remove(c);
executePayment(sa, c);
}
return true;
}
/* (non-Javadoc) /* (non-Javadoc)
* @see forge.card.cost.CostPartWithList#executePayment(forge.card.spellability.SpellAbility, forge.Card) * @see forge.card.cost.CostPartWithList#executePayment(forge.card.spellability.SpellAbility, forge.Card)
*/ */
@@ -472,11 +463,12 @@ public class CostExile extends CostPartWithList {
public PaymentDecision decideAIPayment(Player ai, SpellAbility ability, Card source) { public PaymentDecision decideAIPayment(Player ai, SpellAbility ability, Card source) {
if (this.payCostFromSource()) { if (this.payCostFromSource()) {
return new PaymentDecision(source); return new PaymentDecision(source);
} }
if (this.getType().equals("All")) { if (this.getType().equals("All")) {
return new PaymentDecision(new ArrayList<Card>(ai.getCardsIn(this.getFrom()))); return new PaymentDecision(new ArrayList<Card>(ai.getCardsIn(this.getFrom())));
} else if (this.getType().contains("FromTopGrave")) { }
else if (this.getType().contains("FromTopGrave")) {
return null; return null;
} }
@@ -492,10 +484,12 @@ public class CostExile extends CostPartWithList {
if (this.from.equals(ZoneType.Library)) { if (this.from.equals(ZoneType.Library)) {
return new PaymentDecision(ai.getCardsIn(ZoneType.Library, c)); return new PaymentDecision(ai.getCardsIn(ZoneType.Library, c));
} else if (this.sameZone) { }
else if (this.sameZone) {
// TODO Determine exile from same zone for AI // TODO Determine exile from same zone for AI
return null; return null;
} else { }
else {
List<Card> chosen = ComputerUtil.chooseExileFrom(ai, this.getFrom(), this.getType(), source, ability.getTargetCard(), c); List<Card> chosen = ComputerUtil.chooseExileFrom(ai, this.getFrom(), this.getType(), source, ability.getTargetCard(), c);
return null == chosen ? null : new PaymentDecision(chosen); return null == chosen ? null : new PaymentDecision(chosen);
} }