Mulligan code moved to GameAction

UI will no longer ask you to keep or re-deal in ai-vs-ai games
This commit is contained in:
Maxmtg
2013-05-21 10:49:22 +00:00
parent 763e51e645
commit 7b8efdd38e
11 changed files with 187 additions and 325 deletions

View File

@@ -21,18 +21,11 @@ import java.util.ArrayList;
import java.util.List;
import forge.Card;
import forge.FThreads;
import forge.game.GameState;
import forge.game.GameType;
import forge.game.MatchController;
import forge.game.ai.ComputerUtil;
import forge.game.player.Player;
import forge.game.zone.Zone;
import forge.game.zone.ZoneType;
import forge.gui.GuiDialog;
import forge.gui.framework.SDisplayUtil;
import forge.gui.match.CMatchUI;
import forge.gui.match.nonsingleton.VField;
import forge.gui.match.views.VMessage;
import forge.util.Lang;
import forge.view.ButtonUtil;
@@ -44,24 +37,23 @@ import forge.view.ButtonUtil;
* @author Forge
* @version $Id$
*/
public class InputMulligan extends InputBase {
public class InputConfirmMulligan extends InputSyncronizedBase {
/** Constant <code>serialVersionUID=-8112954303001155622L</code>. */
private static final long serialVersionUID = -8112954303001155622L;
private final MatchController match;
boolean keepHand = false;
final boolean isCommander;
final List<Card> selected = new ArrayList<Card>();
public InputMulligan(MatchController match0, Player humanPlayer) {
public InputConfirmMulligan(Player humanPlayer, boolean commander) {
super(humanPlayer);
match = match0;
isCommander = commander;
}
/** {@inheritDoc} */
@Override
public final void showMessage() {
ButtonUtil.setButtonText("Keep", "Mulligan");
ButtonUtil.enableAllFocusOk();
GameState game = match.getCurrentGame();
GameState game = player.getGame();
Player startingPlayer = game.getPhaseHandler().getPlayerTurn();
StringBuilder sb = new StringBuilder();
@@ -72,73 +64,78 @@ public class InputMulligan extends InputBase {
sb.append("You are going ").append(Lang.getOrdinal(game.getPosition(player, startingPlayer))).append(".\n");
}
sb.append("Do you want to keep your hand?");
if ( isCommander ) {
ButtonUtil.setButtonText("Keep", "Exile");
ButtonUtil.enableOnlyOk();
sb.append("Will you keep your hand or choose some cards to exile those and draw one less card?");
} else {
ButtonUtil.setButtonText("Keep", "Mulligan");
ButtonUtil.enableAllFocusOk();
sb.append("Do you want to keep your hand?");
}
showMessage(sb.toString());
}
/** {@inheritDoc} */
@Override
public final void selectButtonOK() {
this.end();
protected final void onOk() {
keepHand = true;
done();
}
/** {@inheritDoc} */
@Override
public final void selectButtonCancel() {
player.doMulligan();
if (player.getCardsIn(ZoneType.Hand).isEmpty()) {
this.end();
} else {
ButtonUtil.enableAllFocusOk();
}
protected final void onCancel() {
keepHand = false;
done();
}
final void end() {
// Computer mulligan
final GameState game = match.getCurrentGame();
for (Player p : game.getPlayers()) {
if (!p.isComputer()) {
continue;
}
while (ComputerUtil.wantMulligan(p)) {
p.doMulligan();
}
}
private void done() {
ButtonUtil.reset();
Player next = game.getPhaseHandler().getPlayerTurn();
if(game.getType() == GameType.Planechase)
{
next.initPlane();
}
//Set Field shown to current player.
VField nextField = CMatchUI.SINGLETON_INSTANCE.getFieldViewFor(next);
SDisplayUtil.showTab(nextField);
FThreads.invokeInNewThread( new Runnable() {
@Override
public void run() {
match.afterMulligans();
}
});
stop();
}
@Override
public void selectCard(Card c0, boolean isMetaDown) {
Zone z0 = match.getCurrentGame().getZoneOf(c0);
if (c0.getName().equals("Serum Powder") && z0.is(ZoneType.Hand)) {
if (GuiDialog.confirm(c0, "Use " + c0.getName() + "'s ability?")) {
List<Card> hand = new ArrayList<Card>(c0.getController().getCardsIn(ZoneType.Hand));
for (Card c : hand) {
match.getCurrentGame().getAction().exile(c);
}
c0.getController().drawCards(hand.size());
}
} else {
protected void onCardSelected(Card c0) { // the only place that would cause troubles - input is supposed only to confirm, not to fire abilities
boolean fromHand = player.getZone(ZoneType.Hand).contains(c0);
boolean isSerumPowder = c0.getName().equals("Serum Powder");
boolean isLegalChoice = fromHand && (isCommander || isSerumPowder);
if ( !isLegalChoice ) {
SDisplayUtil.remind(VMessage.SINGLETON_INSTANCE);
return;
}
if (isSerumPowder && GuiDialog.confirm(c0, "Use " + c0.getName() + "'s ability?")) {
List<Card> hand = new ArrayList<Card>(c0.getController().getCardsIn(ZoneType.Hand));
for (Card c : hand) {
player.getGame().getAction().exile(c);
}
c0.getController().drawCards(hand.size());
return;
}
if ( isCommander ) { // allow to choose cards for partial paris
if(selected.contains(c0)) {
c0.setUsedToPay(false);
selected.remove(c0);
} else {
c0.setUsedToPay(true);
selected.add(c0);
}
if( selected.isEmpty())
ButtonUtil.enableOnlyOk();
else
ButtonUtil.enableAllFocusOk();
}
}
public final boolean isKeepHand() {
return keepHand;
}
public List<Card> getSelectedCards() {
return selected;
}
}

View File

@@ -1,215 +0,0 @@
/*
* Forge: Play Magic: the Gathering.
* Copyright (C) 2011 Forge Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package forge.control.input;
import java.util.ArrayList;
import java.util.List;
import forge.Card;
import forge.FThreads;
import forge.game.GameState;
import forge.game.GameType;
import forge.game.MatchController;
import forge.game.ai.ComputerUtil;
import forge.game.player.Player;
import forge.game.zone.Zone;
import forge.game.zone.ZoneType;
import forge.gui.GuiDialog;
import forge.gui.framework.SDisplayUtil;
import forge.gui.match.CMatchUI;
import forge.gui.match.nonsingleton.VField;
import forge.util.Lang;
import forge.view.ButtonUtil;
/**
* <p>
* InputMulligan class.
* </p>
*
* @author Forge
* @version $Id: InputMulligan.java 20698 2013-04-01 09:56:12Z Max mtg $
*/
public class InputMulliganPartialParis extends InputBase {
/** Constant <code>serialVersionUID=-8112954303001155622L</code>. */
private static final long serialVersionUID = -8112954303001155622L;
private final MatchController match;
private final GameState game;
private final List<Card> lastExiled = new ArrayList<Card>();
private final List<Card> allExiled = new ArrayList<Card>();
public InputMulliganPartialParis(MatchController match0, Player humanPlayer) {
super(humanPlayer);
match = match0;
game = match.getCurrentGame();
}
/** {@inheritDoc} */
@Override
public final void showMessage() {
ButtonUtil.setButtonText("Done", "Exile");
ButtonUtil.enableOnlyOk();
GameState game = match.getCurrentGame();
Player startingPlayer = game.getPhaseHandler().getPlayerTurn();
StringBuilder sb = new StringBuilder();
if( startingPlayer == player ) {
sb.append("You are going first.\n");
} else {
sb.append(startingPlayer.getName()).append(" is going first. ");
sb.append("You are going ").append(Lang.getOrdinal(game.getPosition(player, startingPlayer))).append(".\n");
}
sb.append("Do you want to Mulligan?");
showMessage(sb.toString());
}
/** {@inheritDoc} */
@Override
public final void selectButtonOK() {
this.end();
}
/** {@inheritDoc} */
@Override
public final void selectButtonCancel() {
for(Card c : lastExiled)
{
game.action.exile(c);
}
player.drawCards(lastExiled.size()-1);
allExiled.addAll(lastExiled);
lastExiled.clear();
final List<Card> aiAllExiled = new ArrayList<Card>();
for (Player p : game.getPlayers()) {
if (!(p.isComputer())) {
continue;
}
int ppcandidates = -1;
while (ComputerUtil.wantMulligan(p) && ppcandidates != 0) {
List<Card> cand = ComputerUtil.getPartialParisCandidates(p);
ppcandidates = cand.size();
if(ppcandidates != 0)
{
aiAllExiled.addAll(cand);
for(Card c : cand)
{
game.action.exile(c);
}
p.drawCards(ppcandidates-1);
}
}
}
if (player.getCardsIn(ZoneType.Hand).isEmpty()) {
this.end();
} else {
ButtonUtil.enableAllFocusOk();
}
}
final void end() {
for(Card c : allExiled)
{
game.action.moveToLibrary(c);
}
player.shuffle();
// Computer mulligan
final List<Card> aiAllExiled = new ArrayList<Card>();
for (Player p : game.getPlayers()) {
if (!(p.isComputer())) {
continue;
}
int ppcandidates = -1;
while (ComputerUtil.wantMulligan(p) && ppcandidates != 0) {
List<Card> cand = ComputerUtil.getPartialParisCandidates(p);
ppcandidates = cand.size();
if(ppcandidates != 0)
{
aiAllExiled.addAll(cand);
for(Card c : cand)
{
game.action.exile(c);
}
p.drawCards(ppcandidates-1);
}
}
}
// Human Leylines & Chancellors
ButtonUtil.reset();
Player next = game.getPhaseHandler().getPlayerTurn();
if(game.getType() == GameType.Planechase)
{
next.initPlane();
}
//Set Field shown to current player.
VField nextField = CMatchUI.SINGLETON_INSTANCE.getFieldViewFor(next);
SDisplayUtil.showTab(nextField);
FThreads.invokeInNewThread( new Runnable() {
@Override
public void run() {
match.afterMulligans();
}
});
}
@Override
public void selectCard(Card c0, boolean isMetaDown) {
if(lastExiled.contains(c0))
{
lastExiled.remove(c0);
c0.setUsedToPay(false);
}
else
{
Zone z0 = match.getCurrentGame().getZoneOf(c0);
if (c0.getName().equals("Serum Powder") && z0.is(ZoneType.Hand)) {
if (GuiDialog.confirm(c0, "Use " + c0.getName() + "'s ability?")) {
List<Card> hand = new ArrayList<Card>(c0.getController().getCardsIn(ZoneType.Hand));
for (Card c : hand) {
game.getAction().exile(c);
}
c0.getController().drawCards(hand.size());
}
else
{
lastExiled.add(c0);
c0.setUsedToPay(true);
}
} else {
lastExiled.add(c0);
c0.setUsedToPay(true);
}
}
if(lastExiled.size() > 0)
ButtonUtil.enableAllFocusOk();
else
ButtonUtil.enableOnlyOk();
}
}

View File

@@ -19,12 +19,8 @@ package forge.control.input;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import forge.Singletons;
import forge.game.GameAge;
import forge.game.GameState;
import forge.game.GameType;
import forge.game.MatchController;
import forge.game.phase.PhaseHandler;
import forge.game.phase.PhaseType;
import forge.game.player.Player;
@@ -46,10 +42,8 @@ public class InputQueue extends MyObservable implements java.io.Serializable {
private final BlockingDeque<Input> inputStack = new LinkedBlockingDeque<Input>();
private final MatchController match;
public InputQueue(MatchController matchController) {
match = matchController;
}
public InputQueue() {}
/**
* <p>
@@ -125,12 +119,7 @@ public class InputQueue extends MyObservable implements java.io.Serializable {
*/
public final Input getActualInput(GameState game) {
GameAge age = game.getAge();
if ( age == GameAge.Mulligan ) {
Player human = Singletons.getControl().getPlayer();
return game.getType() == GameType.Commander ? new InputMulliganPartialParis(match, human) : new InputMulligan(match, human);
}
if ( age != GameAge.Play )
if ( age != GameAge.Play && age != GameAge.Mulligan)
return inputLock;
Input topMost = inputStack.peek(); // incoming input to Control

View File

@@ -18,15 +18,17 @@
package forge.game;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import forge.Card;
import forge.CardCharacteristicName;
@@ -61,13 +63,15 @@ import forge.game.event.CardSacrificedEvent;
import forge.game.player.GameLossReason;
import forge.game.player.HumanPlay;
import forge.game.player.Player;
import forge.game.player.PlayerType;
import forge.game.zone.PlayerZone;
import forge.game.zone.PlayerZoneBattlefield;
import forge.game.zone.Zone;
import forge.game.zone.ZoneType;
import forge.gui.GuiChoose;
import forge.gui.GuiDialog;
import forge.util.maps.CollectionSuppliers;
import forge.util.maps.HashMapOfLists;
import forge.util.maps.MapOfLists;
/**
* Methods for common actions performed during a game.
@@ -1466,13 +1470,56 @@ public class GameAction {
game.getAction().checkStateEffects();
}
/**
* <p>
* playCardWithoutManaCost.
* </p>
*
* @param c
* a {@link forge.Card} object.
*/
public void performMulligans(final Player firstPlayer, final boolean isCommander) {
List<Player> whoCanMulligan = Lists.newArrayList(game.getPlayers());
int offset = whoCanMulligan.indexOf(firstPlayer);
// Have to cycle-shift the list to get the first player on index 0
for( int i = 0; i < offset; i++ ) {
whoCanMulligan.add(whoCanMulligan.remove(0));
}
MapOfLists<Player, Card> exiledDuringMulligans = new HashMapOfLists<Player, Card>(CollectionSuppliers.<Card>arrayLists());
do {
for (int i = 0; i < whoCanMulligan.size(); i++) {
Player p = whoCanMulligan.get(i);
List<Card> toMulligan = p.canMulligan() ? p.getController().getCardsToMulligan(isCommander) : null;
if ( toMulligan != null ) {
if( !isCommander ) {
toMulligan = new ArrayList<Card>(p.getCardsIn(ZoneType.Hand));
for (final Card c : toMulligan) {
moveToLibrary(c);
}
p.shuffle();
p.drawCards(toMulligan.size() - 1);
} else if ( !toMulligan.isEmpty() ){
List<Card> toExile = Lists.newArrayList(toMulligan);
for(Card c : toExile) {
exile(c);
}
exiledDuringMulligans.addAll(p, toExile);
p.drawCards(toExile.size() - 1);
} else
continue;
p.onMulliganned();
} else {
whoCanMulligan.remove(i--);
}
}
} while( !whoCanMulligan.isEmpty() );
if( isCommander )
for(Entry<Player, Collection<Card>> kv : exiledDuringMulligans.entrySet() ) {
Player p = kv.getKey();
Collection<Card> cc = kv.getValue();
for(Card c : cc) {
moveToLibrary(c);
}
p.shuffle();
}
}
}

View File

@@ -138,7 +138,7 @@ public class MatchController {
*/
public void startRound() {
inputQueue = new InputQueue(this);
inputQueue = new InputQueue();
currentGame = new GameState(players, gameType, this);
try {
@@ -146,16 +146,38 @@ public class MatchController {
final boolean canRandomFoil = Singletons.getModel().getPreferences().getPrefBoolean(FPref.UI_RANDOM_FOIL) && gameType == GameType.Constructed;
GameNew.newGame(currentGame, canRandomFoil);
determineFirstTurnPlayer(getLastGameOutcome(), currentGame);
currentGame.setAge(GameAge.Mulligan);
getInput().clearInput();
// Update observers
currentGame.getGameLog().updateObservers();
} catch (Exception e) {
BugReporter.reportException(e);
}
final Player firstPlayer = determineFirstTurnPlayer(getLastGameOutcome(), currentGame);
getInput().clearInput();
if(currentGame.getType() == GameType.Planechase)
firstPlayer.initPlane();
//Set Field shown to current player.
VField nextField = CMatchUI.SINGLETON_INSTANCE.getFieldViewFor(firstPlayer);
SDisplayUtil.showTab(nextField);
// Update observers
currentGame.getGameLog().updateObservers();
// This code was run from EDT.
FThreads.invokeInNewThread( new Runnable() {
@Override
public void run() {
currentGame.getAction().performMulligans(firstPlayer, currentGame.getType() == GameType.Commander);
currentGame.getAction().handleLeylinesAndChancellors();
// Run Trigger beginning of the game
final HashMap<String, Object> runParams = new HashMap<String, Object>();
currentGame.getTriggerHandler().runTrigger(TriggerType.NewGame, runParams, false);
currentGame.setAge(GameAge.Play);
getInput().clearInput();
}
});
}
public static void attachUiToMatch(MatchController match, LobbyPlayerHuman humanLobbyPlayer) {
@@ -328,22 +350,12 @@ public class MatchController {
return 10;
}
public void afterMulligans()
{
currentGame.getAction().handleLeylinesAndChancellors();
// Run Trigger beginning of the game
final HashMap<String, Object> runParams = new HashMap<String, Object>();
currentGame.getTriggerHandler().runTrigger(TriggerType.NewGame, runParams, false);
currentGame.setAge(GameAge.Play);
getInput().clearInput();
}
/**
* TODO: Write javadoc for this method.
* @param match
* @param game
*/
private void determineFirstTurnPlayer(final GameOutcome lastGameOutcome, final GameState game) {
private Player determineFirstTurnPlayer(final GameOutcome lastGameOutcome, final GameState game) {
// Only cut/coin toss if it's the first game of the match
Player goesFirst;
Player humanPlayer = Singletons.getControl().getPlayer();
@@ -360,6 +372,7 @@ public class MatchController {
}
goesFirst = willPlay ? goesFirst : goesFirst.getOpponent();
game.getPhaseHandler().setPlayerTurn(goesFirst);
return goesFirst;
}
// decides who goes first when starting another game, used by newGame()

View File

@@ -1205,6 +1205,10 @@ public class Player extends GameEntity implements Comparable<Player> {
return this.drawCards(1);
}
public boolean canMulligan() {
return !getZone(ZoneType.Hand).isEmpty();
}
/**
*
* TODO Write javadoc for this method.
@@ -3123,4 +3127,15 @@ public class Player extends GameEntity implements Comparable<Player> {
public final void setHighlited(boolean value) { highlited = value; }
public final boolean isHighlited() { return highlited; }
/**
* TODO: Write javadoc for this method.
*/
public void onMulliganned() {
game.getEvents().post(new MulliganEvent(this)); // quest listener may interfere here
final int newHand = getCardsIn(ZoneType.Hand).size();
game.getGameLog().add("Mulligan", this + " has mulliganed down to " + newHand + " cards.", 0);
stats.notifyHasMulliganed();
stats.notifyOpeningHandSize(newHand);
}
}

View File

@@ -139,4 +139,5 @@ public abstract class PlayerController {
public abstract String chooseSomeType(String kindOfType, String aiLogic, List<String> validTypes, List<String> invalidTypes);
public abstract boolean confirmReplacementEffect(ReplacementEffect replacementEffect, SpellAbility effectSA, String question);
public abstract List<Card> getCardsToMulligan(boolean isCommander);
}

View File

@@ -287,5 +287,14 @@ public class PlayerControllerAi extends PlayerController {
return ReplacementEffect.aiShouldRun(replacementEffect, effectSA, player);
}
@Override
public List<Card> getCardsToMulligan(boolean isCommander) {
if( !ComputerUtil.wantMulligan(player) )
return null;
if (!isCommander)
return player.getCardsIn(ZoneType.Hand);
else
return ComputerUtil.getPartialParisCandidates(player);
}
}

View File

@@ -22,6 +22,7 @@ import forge.card.spellability.TargetSelection;
import forge.control.input.Input;
import forge.control.input.InputBlock;
import forge.control.input.InputCleanup;
import forge.control.input.InputConfirmMulligan;
import forge.control.input.InputPassPriority;
import forge.control.input.InputSelectCards;
import forge.control.input.InputSelectCardsFromList;
@@ -467,4 +468,11 @@ public class PlayerControllerHuman extends PlayerController {
return GuiDialog.confirm(replacementEffect.getHostCard(), question);
}
@Override
public List<Card> getCardsToMulligan(boolean isCommander) {
final InputConfirmMulligan inp = new InputConfirmMulligan(player, isCommander);
FThreads.setInputAndWait(inp);
return inp.isKeepHand() ? null : inp.getSelectedCards();
}
}

View File

@@ -18,7 +18,6 @@
package forge.gui.match;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;