Player belongs to game, it cannot have references to any concrete PlayerController

This commit is contained in:
Maxmtg
2013-12-04 07:46:30 +00:00
parent d1ef107b48
commit 2ffa1510ed
2 changed files with 15 additions and 16 deletions

View File

@@ -2941,28 +2941,21 @@ public class Player extends GameEntity implements Comparable<Player> {
controllerCreator = ctrlr; controllerCreator = ctrlr;
controller = ctrlr; controller = ctrlr;
} }
/** /**
* Run a procedure using an AI controller * Run a procedure using a different controller
* @param proc * @param proc
* @param tempController
*/ */
public void runAsAi(Runnable proc) { public void runWithController(Runnable proc, PlayerController tempController) {
if (controller instanceof PlayerControllerAi) {
proc.run(); //can just run with current controller if it's an AI controller
return;
}
PlayerController oldController = controller; PlayerController oldController = controller;
controller = new PlayerControllerAi(this.game, this, this.getOriginalLobbyPlayer()); controller = tempController;
try { try {
proc.run(); proc.run();
} finally {
controller = oldController; controller = oldController;
} }
catch (Exception ex) {
//ensure controller restored before throwing exception in case user chooses to continue
controller = oldController;
throw ex;
}
} }
/** /**

View File

@@ -19,6 +19,7 @@ import forge.game.card.CardUtil;
import forge.game.mana.ManaCostBeingPaid; import forge.game.mana.ManaCostBeingPaid;
import forge.game.player.HumanPlay; import forge.game.player.HumanPlay;
import forge.game.player.Player; import forge.game.player.Player;
import forge.game.player.PlayerControllerAi;
import forge.game.replacement.ReplacementEffect; import forge.game.replacement.ReplacementEffect;
import forge.game.spellability.AbilityManaPart; import forge.game.spellability.AbilityManaPart;
import forge.game.spellability.SpellAbility; import forge.game.spellability.SpellAbility;
@@ -48,6 +49,11 @@ public abstract class InputPayMana extends InputSyncronizedBase {
this.game = player.getGame(); this.game = player.getGame();
this.saPaidFor = saToPayFor; this.saPaidFor = saToPayFor;
} }
public void runAsAi(Runnable proc) {
this.player.runWithController(proc, new PlayerControllerAi(this.game, this.player, this.player.getOriginalLobbyPlayer()));
}
@Override @Override
protected void onCardSelected(final Card card, final MouseEvent triggerEvent) { protected void onCardSelected(final Card card, final MouseEvent triggerEvent) {
@@ -289,7 +295,7 @@ public abstract class InputPayMana extends InputSyncronizedBase {
ComputerUtilMana.payManaCost(manaCost, saPaidFor, player); ComputerUtilMana.payManaCost(manaCost, saPaidFor, player);
} }
}; };
this.player.runAsAi(proc); runAsAi(proc);
this.showMessage(); this.showMessage();
} }
} }
@@ -311,7 +317,7 @@ public abstract class InputPayMana extends InputSyncronizedBase {
return ComputerUtilMana.canPayManaCost(manaCost, saPaidFor, player); return ComputerUtilMana.canPayManaCost(manaCost, saPaidFor, player);
} }
}; };
this.player.runAsAi(proc); runAsAi(proc);
canPayManaCost = proc.getResult(); canPayManaCost = proc.getResult();
} }
if (canPayManaCost) { if (canPayManaCost) {