From 14c5e5242f69203d518f7708c1c19dbafac8d8a9 Mon Sep 17 00:00:00 2001 From: drdev Date: Thu, 9 Oct 2014 01:09:54 +0000 Subject: [PATCH] Support updating mana for player view --- .../main/java/forge/game/mana/ManaPool.java | 138 ++++++------------ .../main/java/forge/game/player/Player.java | 3 + 2 files changed, 51 insertions(+), 90 deletions(-) diff --git a/forge-game/src/main/java/forge/game/mana/ManaPool.java b/forge-game/src/main/java/forge/game/mana/ManaPool.java index 393bf19ea06..9ea63d4f45c 100644 --- a/forge-game/src/main/java/forge/game/mana/ManaPool.java +++ b/forge-game/src/main/java/forge/game/mana/ManaPool.java @@ -53,20 +53,9 @@ import java.util.List; * @version $Id$ */ public class ManaPool implements Iterable { - + private final Player owner; private final Multimap floatingMana = ArrayListMultimap.create(); - /** Constant map. */ - private final Player owner; - - /** - *

- * Constructor for ManaPool. - *

- * - * @param player - * a {@link forge.game.player.Player} object. - */ public ManaPool(final Player player) { owner = player; restoreColorReplacements(); @@ -79,20 +68,13 @@ public class ManaPool implements Iterable { public void addMana(final Mana mana) { floatingMana.put(mana.getColor(), mana); + owner.updateManaForView(); owner.getGame().fireEvent(new GameEventManaPool(owner, EventValueChangeType.Added, mana)); } - /** - *

- * addManaToFloating. - *

- * - * @param manaList - * a {@link java.util.ArrayList} object. - */ public final void add(final Iterable manaList) { for (final Mana m : manaList) { - this.addMana(m); + addMana(m); } } @@ -121,17 +103,9 @@ public class ManaPool implements Iterable { if (totalMana() == safeMana) { return false; //won't lose floating mana if all mana is of colors that aren't going to be emptied } - return true; } - /** - *

- * clearPool. - * - * @return - the amount of mana removed this way - *

- */ public final List clearPool(boolean isEndOfPhase) { // isEndOfPhase parameter: true = end of phase, false = mana drain effect List cleared = new ArrayList(); @@ -157,75 +131,62 @@ public class ManaPool implements Iterable { } for (Byte b : keys) { + Collection cm = floatingMana.get(b); if (isEndOfPhase && !owner.getGame().getPhaseHandler().is(PhaseType.CLEANUP)) { final List pMana = new ArrayList(); - for (final Mana mana : floatingMana.get(b)) { + for (final Mana mana : cm) { if (mana.getManaAbility()!= null && mana.getManaAbility().isPersistentMana()) { pMana.add(mana); } } - floatingMana.get(b).removeAll(pMana); + cm.removeAll(pMana); if (convertToColorless) { convertManaColor(b, MagicColor.COLORLESS); - floatingMana.get(b).addAll(pMana); - } else { - cleared.addAll(floatingMana.get(b)); - floatingMana.get(b).clear(); + cm.addAll(pMana); + } + else { + cleared.addAll(cm); + cm.clear(); floatingMana.putAll(b, pMana); } } else { if (convertToColorless) { convertManaColor(b, MagicColor.COLORLESS); - } else { - cleared.addAll(floatingMana.get(b)); - floatingMana.get(b).clear(); + } + else { + cleared.addAll(cm); + cm.clear(); } } } + owner.updateManaForView(); owner.getGame().fireEvent(new GameEventManaPool(owner, EventValueChangeType.Cleared, null)); return cleared; } private void convertManaColor(final byte originalColor, final byte toColor) { List convert = new ArrayList(); - for (Mana m : floatingMana.get(originalColor)) { + Collection cm = floatingMana.get(originalColor); + for (Mana m : cm) { convert.add(new Mana(toColor, m.getSourceCard(), m.getManaAbility())); } - floatingMana.get(originalColor).clear(); + cm.clear(); floatingMana.putAll(toColor, convert); + owner.updateManaForView(); } - /** - *

- * removeManaFrom. - *

- * - * @param mana - * a {@link forge.game.mana.Mana} object. - */ private void removeMana(final Mana mana) { Collection cm = floatingMana.get(mana.getColor()); if (cm.remove(mana)) { + owner.updateManaForView(); owner.getGame().fireEvent(new GameEventManaPool(owner, EventValueChangeType.Removed, mana)); } } - - /** - *

- * subtractManaFromAbility. - *

- * - * @param saPaidFor - * a {@link forge.game.spellability.SpellAbility} object. - * @param manaCost - * a {@link forge.game.mana.ManaCostBeingPaid} object. - * @return a {@link forge.game.mana.ManaCostBeingPaid} object. - */ + public final void payManaFromAbility(final SpellAbility saPaidFor, ManaCostBeingPaid manaCost, final SpellAbility saPayment) { // Mana restriction must be checked before this method is called - final List paidAbs = saPaidFor.getPayingManaAbilities(); AbilityManaPart abManaPart = saPayment.getManaPartRecursive(); @@ -240,9 +201,9 @@ public class ManaPool implements Iterable { public boolean tryPayCostWithColor(byte colorCode, SpellAbility saPaidFor, ManaCostBeingPaid manaCost) { Mana manaFound = null; String restriction = manaCost.getSourceRestriction(); - Collection coloredMana = this.floatingMana.get(colorCode); + Collection cm = floatingMana.get(colorCode); - for (final Mana mana : coloredMana) { + for (final Mana mana : cm) { if (mana.getManaAbility() != null && !mana.getManaAbility().meetsManaRestrictions(saPaidFor)) { continue; } @@ -254,12 +215,12 @@ public class ManaPool implements Iterable { manaFound = mana; break; } - - - boolean result = manaFound != null && tryPayCostWithMana(saPaidFor, manaCost, manaFound); - if(result) + + if (manaFound != null && tryPayCostWithMana(saPaidFor, manaCost, manaFound)) { saPaidFor.getPayingMana().add(0, manaFound); - return result; + return true; + } + return false; } public boolean tryPayCostWithMana(final SpellAbility sa, ManaCostBeingPaid manaCost, final Mana mana) { @@ -267,7 +228,7 @@ public class ManaPool implements Iterable { return false; } manaCost.payMana(mana, this); - this.removeMana(mana); + removeMana(mana); if (mana.addsNoCounterMagic(sa) && sa.getHostCard() != null) { sa.getHostCard().setCanCounter(false); } @@ -312,13 +273,6 @@ public class ManaPool implements Iterable { return floatingMana.isEmpty(); } - /** - *

- * totalMana. - *

- * - * @return a int. - */ public final int totalMana() { return floatingMana.values().size(); } @@ -328,7 +282,7 @@ public class ManaPool implements Iterable { if (ma == null) { return false; } - if (this.floatingMana.isEmpty()) { + if (floatingMana.isEmpty()) { return false; } @@ -337,7 +291,7 @@ public class ManaPool implements Iterable { boolean manaNotAccountedFor = false; // loop over mana produced by mana ability for (Mana mana : ma.getLastManaProduced()) { - Collection poolLane = this.floatingMana.get(mana.getColor()); + Collection poolLane = floatingMana.get(mana.getColor()); if (poolLane != null && poolLane.contains(mana)) { removeFloating.add(mana); @@ -355,7 +309,7 @@ public class ManaPool implements Iterable { } for (int k = 0; k < removeFloating.size(); k++) { - this.removeMana(removeFloating.get(k)); + removeMana(removeFloating.get(k)); } return true; } @@ -369,7 +323,7 @@ public class ManaPool implements Iterable { sa.getHostCard().setCanCounter(true); } for (final Mana m : manaPaid) { - this.addMana(m); + addMana(m); } manaPaid.clear(); @@ -381,7 +335,7 @@ public class ManaPool implements Iterable { for (final SpellAbility am : payingAbilities) { // Recursively refund abilities that were used. - this.refundManaPaid(am); + refundManaPaid(am); } payingAbilities.clear(); @@ -401,18 +355,21 @@ public class ManaPool implements Iterable { // Fix the index without hardcodes int rowIdx = MagicColor.getIndexOfFirstColor(originalColor); rowIdx = rowIdx < 0 ? identityMatrix.length - 1 : rowIdx; - if (additive) + if (additive) { colorConversionMatrix[rowIdx] |= replacementColor; - else + } + else { colorRestrictionMatrix[rowIdx] &= replacementColor; + } } - - public void restoreColorReplacements() { - for(int i = 0; i < colorConversionMatrix.length; i++) - colorConversionMatrix[i] = identityMatrix[i]; - for(int i = 0; i < colorRestrictionMatrix.length; i++) + public void restoreColorReplacements() { + for (int i = 0; i < colorConversionMatrix.length; i++) { + colorConversionMatrix[i] = identityMatrix[i]; + } + for (int i = 0; i < colorRestrictionMatrix.length; i++) { colorRestrictionMatrix[i] = MagicColor.ALL_COLORS; + } } public byte getPossibleColorUses(byte color) { @@ -427,10 +384,11 @@ public class ManaPool implements Iterable { public boolean canPayForShardWithColor(ManaCostShard shard, byte color) { byte line = getPossibleColorUses(color); - for(int i = 0; i < MagicColor.NUMBER_OR_COLORS; i++) { + for (int i = 0; i < MagicColor.NUMBER_OR_COLORS; i++) { byte outColor = MagicColor.WUBRG[i]; - if (( line & outColor) != 0 && shard.canBePaidWithManaOfColor(outColor)) + if ((line & outColor) != 0 && shard.canBePaidWithManaOfColor(outColor)) { return true; + } } return shard.canBePaidWithManaOfColor((byte)0); } diff --git a/forge-game/src/main/java/forge/game/player/Player.java b/forge-game/src/main/java/forge/game/player/Player.java index 4f9f3ecf46c..3f507bc7754 100644 --- a/forge-game/src/main/java/forge/game/player/Player.java +++ b/forge-game/src/main/java/forge/game/player/Player.java @@ -1424,6 +1424,9 @@ public class Player extends GameEntity implements Comparable { public final ManaPool getManaPool() { return manaPool; } + public void updateManaForView() { + view.updateMana(this); + } public final int getNumPowerSurgeLands() { return numPowerSurgeLands;