From 79da5d9e68c628b46555db9b1ef02434e07dcbe0 Mon Sep 17 00:00:00 2001 From: Maxmtg Date: Sun, 26 May 2013 16:10:40 +0000 Subject: [PATCH] BugReporter indicates thread in console, ComputerUtilMana - commented out is debug code PhaseHandler: nextPhase & handleBeginPhase are called consequently, no observer updates happen between calls GuiDisplayUtil: generate mana button caused wrong thread exception VMessage - remove useless import --- src/main/java/forge/error/BugReporter.java | 1 + .../java/forge/game/ai/ComputerUtilMana.java | 6 + .../java/forge/game/phase/PhaseHandler.java | 228 ++++++++---------- src/main/java/forge/gui/GuiDisplayUtil.java | 4 +- src/main/java/forge/gui/InputProxy.java | 15 +- .../java/forge/gui/match/views/VMessage.java | 1 - 6 files changed, 113 insertions(+), 142 deletions(-) diff --git a/src/main/java/forge/error/BugReporter.java b/src/main/java/forge/error/BugReporter.java index 3b01511196f..64e0f2d5f49 100644 --- a/src/main/java/forge/error/BugReporter.java +++ b/src/main/java/forge/error/BugReporter.java @@ -79,6 +79,7 @@ public class BugReporter { if (message != null) { System.err.printf("%s > %s%n", FThreads.debugGetCurrThreadId(), message); } + System.err.print( FThreads.debugGetCurrThreadId() + " > " ); ex.printStackTrace(); StringBuilder sb = new StringBuilder(); diff --git a/src/main/java/forge/game/ai/ComputerUtilMana.java b/src/main/java/forge/game/ai/ComputerUtilMana.java index d790b2e509a..65ae9aa5202 100644 --- a/src/main/java/forge/game/ai/ComputerUtilMana.java +++ b/src/main/java/forge/game/ai/ComputerUtilMana.java @@ -106,6 +106,8 @@ public class ComputerUtilMana { MapOfLists sourcesForShards = ComputerUtilMana.groupAndOrderToPayShards(ai, manaAbilityMap, cost); // Loop over mana needed + + //List paymentPlan = new ArrayList(); ManaCostShard toPay = null; while (!cost.isPaid()) { @@ -124,6 +126,8 @@ public class ComputerUtilMana { } } + //paymentPlan.add(String.format("%s : (%s) %s", toPay, saPayment == null ? "LIFE" : saPayment.getSourceCard(), saPayment)); + if( saPayment == null ) { if(!toPay.isPhyrexian() || !ai.canPayLife(2)) break; // cannot pay @@ -172,6 +176,8 @@ public class ComputerUtilMana { } manapool.clearManaPaid(sa, test); + + //System.err.printf("[%s] payment %s for (%s)+%d %s:%n\t%s%n%n", test ? "test" : "PROD", cost.isPaid() ? "*PAID*" : "failed", sa.getSourceCard(), extraMana, sa.toUnsuppressedString(), StringUtils.join(paymentPlan, "\n\t") ); if(!cost.isPaid()) { if( test ) return false; diff --git a/src/main/java/forge/game/phase/PhaseHandler.java b/src/main/java/forge/game/phase/PhaseHandler.java index 66c216d5f31..0ab8a5922d0 100644 --- a/src/main/java/forge/game/phase/PhaseHandler.java +++ b/src/main/java/forge/game/phase/PhaseHandler.java @@ -74,9 +74,8 @@ public class PhaseHandler extends MyObservable implements java.io.Serializable { private Player pPlayerPriority = null; private Player pFirstPriority = null; - private boolean bPhaseEffects = true; private AtomicBoolean bCombat = new AtomicBoolean(false); - private boolean bRepeat = false; + private boolean bRepeatCleanup = false; /** The need to next phase. */ private boolean isPlayerPriorityAllowed = false; @@ -176,29 +175,6 @@ public class PhaseHandler extends MyObservable implements java.io.Serializable { this.setPriority(this.playerTurn); } - /** - *

- * doPhaseEffects. - *

- * - * @return a boolean. - */ - public final boolean hasPhaseEffects() { - return this.bPhaseEffects; - } - - /** - *

- * setPhaseEffects. - *

- * - * @param b - * a boolean. - */ - private final void setPhaseEffects(final boolean b) { - this.bPhaseEffects = b; - } - /** *

* inCombat. @@ -228,14 +204,104 @@ public class PhaseHandler extends MyObservable implements java.io.Serializable { *

*/ public final void repeatPhase() { - this.bRepeat = true; + this.bRepeatCleanup = true; } - public final void handleBeginPhase() { + /** + *

+ * nextPhase. + *

+ */ + private final void nextPhase() { + this.setPlayersPriorityPermission(true); // PlayerPriorityAllowed = false; + + // If the Stack isn't empty why is nextPhase being called? + if (!game.getStack().isEmpty()) { + Log.debug("Phase.nextPhase() is called, but Stack isn't empty."); + return; + } + + for (Player p : game.getPlayers()) { + int burn = p.getManaPool().clearPool(true); + if (Singletons.getModel().getPreferences().getPrefBoolean(FPref.UI_MANABURN)) { + p.loseLife(burn); + + // Play the Mana Burn sound + game.getEvents().post(new ManaBurnEvent()); + } + p.updateObservers(); + } + + switch (this.phase) { + case UNTAP: + this.nCombatsThisTurn = 0; + break; + + case COMBAT_DECLARE_ATTACKERS: + game.getStack().unfreezeStack(); + this.nCombatsThisTurn++; + break; + + case COMBAT_DECLARE_BLOCKERS: + game.getStack().unfreezeStack(); + break; + + case COMBAT_END: + //SDisplayUtil.showTab(EDocID.REPORT_STACK.getDoc()); + game.getCombat().reset(playerTurn); + this.getPlayerTurn().resetAttackedThisCombat(); + this.bCombat.set(false); + + break; + + case CLEANUP: + this.bPreventCombatDamageThisTurn = false; + if (!this.bRepeatCleanup) { + this.setPlayerTurn(this.handleNextTurn()); + } + this.planarDiceRolledthisTurn = 0; + // Play the End Turn sound + game.getEvents().post(new EndOfTurnEvent()); + break; + default: // no action + } + + String phaseType = ""; + if (this.bRepeatCleanup) { // for when Cleanup needs to repeat itself + this.bRepeatCleanup = false; + phaseType = "Repeat "; + } else { + // If the phase that's ending has a stack of additional phases + // Take the LIFO one and move to that instead of the normal one + if (this.extraPhases.containsKey(phase)) { + PhaseType nextPhase = this.extraPhases.get(phase).pop(); + // If no more additional phases are available, remove it from the map + // and let the next add, reput the key + if (this.extraPhases.get(phase).isEmpty()) { + this.extraPhases.remove(phase); + } + this.phase = nextPhase; + phaseType = "Additional "; + } else { + this.phase = phase.getNextPhase(); + } + } + + // **** Anything BELOW Here is actually in the next phase. Maybe move + // this to handleBeginPhase + if (this.phase == PhaseType.UNTAP) { + this.turn++; + game.getGameLog().add(GameEventType.TURN, "Turn " + this.turn + " (" + this.getPlayerTurn() + ")"); + } + + game.getGameLog().add(GameEventType.PHASE, phaseType + Lang.getPossesive(this.getPlayerTurn().getName()) + " " + this.getPhase().Name); + PhaseUtil.visuallyActivatePhase(this.getPlayerTurn(), this.getPhase()); + } + + private final void handleBeginPhase() { if (null == playerTurn) { return; } - this.setPhaseEffects(false); // Handle effects that happen at the beginning of phases game.getAction().checkStateEffects(); @@ -245,7 +311,6 @@ public class PhaseHandler extends MyObservable implements java.io.Serializable { case UNTAP: //SDisplayUtil.showTab(EDocID.REPORT_STACK.getDoc()); game.getPhaseHandler().setPlayersPriorityPermission(false); - //updateObservers(); PhaseUtil.handleUntap(game); break; @@ -399,7 +464,6 @@ public class PhaseHandler extends MyObservable implements java.io.Serializable { runParams.put("Phase", this.getPhase().Name); runParams.put("Player", this.getPlayerTurn()); game.getTriggerHandler().runTrigger(TriggerType.Phase, runParams, false); - } // This line fixes Combat Damage triggers not going off when they should @@ -421,104 +485,6 @@ public class PhaseHandler extends MyObservable implements java.io.Serializable { return this.bPreventCombatDamageThisTurn; } - /** - *

- * nextPhase. - *

- */ - public final void nextPhase() { - - this.setPlayersPriorityPermission(true); // PlayerPriorityAllowed = false; - - // If the Stack isn't empty why is nextPhase being called? - if (!game.getStack().isEmpty()) { - Log.debug("Phase.nextPhase() is called, but Stack isn't empty."); - return; - } - setPhaseEffects(true); - - for (Player p : game.getPlayers()) { - int burn = p.getManaPool().clearPool(true); - if (Singletons.getModel().getPreferences().getPrefBoolean(FPref.UI_MANABURN)) { - p.loseLife(burn); - - // Play the Mana Burn sound - game.getEvents().post(new ManaBurnEvent()); - } - p.updateObservers(); - } - - switch (this.phase) { - case UNTAP: - this.nCombatsThisTurn = 0; - break; - - case COMBAT_DECLARE_ATTACKERS: - game.getStack().unfreezeStack(); - this.nCombatsThisTurn++; - break; - - case COMBAT_DECLARE_BLOCKERS: - game.getStack().unfreezeStack(); - break; - - case COMBAT_END: - //SDisplayUtil.showTab(EDocID.REPORT_STACK.getDoc()); - game.getCombat().reset(playerTurn); - this.getPlayerTurn().resetAttackedThisCombat(); - this.bCombat.set(false); - - break; - - case CLEANUP: - this.bPreventCombatDamageThisTurn = false; - if (!this.bRepeat) { - this.setPlayerTurn(this.handleNextTurn()); - } - this.planarDiceRolledthisTurn = 0; - // Play the End Turn sound - game.getEvents().post(new EndOfTurnEvent()); - break; - default: // no action - } - - String phaseType = ""; - if (this.bRepeat) { // for when Cleanup needs to repeat itself - this.bRepeat = false; - phaseType = "Repeat "; - } else { - // If the phase that's ending has a stack of additional phases - // Take the LIFO one and move to that instead of the normal one - if (this.extraPhases.containsKey(phase)) { - PhaseType nextPhase = this.extraPhases.get(phase).pop(); - // If no more additional phases are available, remove it from the map - // and let the next add, reput the key - if (this.extraPhases.get(phase).isEmpty()) { - this.extraPhases.remove(phase); - } - this.phase = nextPhase; - phaseType = "Additional "; - } else { - this.phase = phase.getNextPhase(); - } - } - - // **** Anything BELOW Here is actually in the next phase. Maybe move - // this to handleBeginPhase - if (this.phase == PhaseType.UNTAP) { - this.turn++; - game.getGameLog().add(GameEventType.TURN, "Turn " + this.turn + " (" + this.getPlayerTurn() + ")"); - } - - game.getGameLog().add(GameEventType.PHASE, phaseType + Lang.getPossesive(this.getPlayerTurn().getName()) + " " + this.getPhase().Name); - PhaseUtil.visuallyActivatePhase(this.getPlayerTurn(), this.getPhase()); - - // When consecutively skipping phases (like in combat) this section - // pushes through that block - this.updateObservers(); - // it no longer does. - } - /** *

* handleNextTurn. @@ -750,7 +716,11 @@ public class PhaseHandler extends MyObservable implements java.io.Serializable { // end phase setPlayersPriorityPermission(true); nextPhase(); - return; + // When consecutively skipping phases (like in combat) this section + // pushes through that block + handleBeginPhase(); + // it no longer does. + updateObservers(); } else if (!game.getStack().hasSimultaneousStackEntries()) { game.getStack().resolveStack(); game.getStack().chooseOrderOfSimultaneousStackEntryAll(); diff --git a/src/main/java/forge/gui/GuiDisplayUtil.java b/src/main/java/forge/gui/GuiDisplayUtil.java index fdd0a2fc179..c72a74579d7 100644 --- a/src/main/java/forge/gui/GuiDisplayUtil.java +++ b/src/main/java/forge/gui/GuiDisplayUtil.java @@ -65,7 +65,9 @@ public final class GuiDisplayUtil { Map produced = new HashMap(); produced.put("Produced", "W W W W W W W U U U U U U U B B B B B B B G G G G G G G R R R R R R R 7"); final AbilityManaPart abMana = new AbilityManaPart(dummy, produced); - abMana.produceMana(null); + getGame().getInputQueue().invokeGameAction(new Runnable() { + @Override public void run() { abMana.produceMana(null); } + }); } public static void devSetupGameState() { diff --git a/src/main/java/forge/gui/InputProxy.java b/src/main/java/forge/gui/InputProxy.java index 21124d5c6b9..c22d77f927e 100644 --- a/src/main/java/forge/gui/InputProxy.java +++ b/src/main/java/forge/gui/InputProxy.java @@ -58,20 +58,13 @@ public class InputProxy implements Observer { FThreads.assertExecutedByEdt(false); final PhaseHandler ph = game.getPhaseHandler(); - - if(INPUT_DEBUG) - System.out.println(FThreads.debugGetStackTraceItem(6, true)); - - if ( game.getInputQueue().isEmpty() && ph.hasPhaseEffects()) { - if(INPUT_DEBUG) - System.out.printf("\t%s > handle begin phase for %s%n", FThreads.debugGetCurrThreadId(), ph.debugPrintState()); - ph.handleBeginPhase(); - } - final Input nextInput = game.getInputQueue().getActualInput(); - if(INPUT_DEBUG) + + if(INPUT_DEBUG) { + System.out.print(FThreads.debugGetStackTraceItem(6, true) + " ... "); System.out.printf("\tinput is %s during %s, \tstack = %s%n", nextInput == null ? "null" : nextInput.getClass().getSimpleName(), ph.debugPrintState(), game.getInputQueue().printInputStack()); + } this.input.set(nextInput); Runnable showMessage = new Runnable() { diff --git a/src/main/java/forge/gui/match/views/VMessage.java b/src/main/java/forge/gui/match/views/VMessage.java index 327f165953c..d2ea9cbd8c6 100644 --- a/src/main/java/forge/gui/match/views/VMessage.java +++ b/src/main/java/forge/gui/match/views/VMessage.java @@ -26,7 +26,6 @@ import javax.swing.SwingConstants; import javax.swing.border.MatteBorder; import net.miginfocom.swing.MigLayout; -import forge.Constant.Preferences; import forge.Singletons; import forge.gui.framework.DragCell; import forge.gui.framework.DragTab;