diff --git a/.gitattributes b/.gitattributes index b31f27e88f6..0efa1ef4547 100644 --- a/.gitattributes +++ b/.gitattributes @@ -16013,6 +16013,7 @@ forge-m-base/src/forge/screens/constructed/ConstructedScreen.java -text forge-m-base/src/forge/screens/draft/DraftScreen.java -text forge-m-base/src/forge/screens/guantlet/GuantletScreen.java -text forge-m-base/src/forge/screens/home/HomeScreen.java -text +forge-m-base/src/forge/screens/match/MatchController.java -text forge-m-base/src/forge/screens/match/MatchScreen.java -text forge-m-base/src/forge/screens/quest/QuestScreen.java -text forge-m-base/src/forge/screens/sealed/SealedScreen.java -text diff --git a/forge-m-base/src/forge/player/LobbyPlayerHuman.java b/forge-m-base/src/forge/player/LobbyPlayerHuman.java index f4eeee454f5..93376807b17 100644 --- a/forge-m-base/src/forge/player/LobbyPlayerHuman.java +++ b/forge-m-base/src/forge/player/LobbyPlayerHuman.java @@ -1,5 +1,6 @@ package forge.player; +import forge.ai.PlayerControllerAi; import forge.game.Game; import forge.game.player.LobbyPlayer; import forge.game.player.Player; @@ -17,15 +18,15 @@ public class LobbyPlayerHuman extends LobbyPlayer { @Override public PlayerController createControllerFor(Player human) { - return null; //TODO new PlayerControllerHuman(human.getGame(), human, this); + return new PlayerControllerAi(human.getGame(), human, this); //TODO new PlayerControllerHuman(human.getGame(), human, this); } @Override public Player createIngamePlayer(Game game) { //TODO Player player = new Player(/*GuiDisplayUtil.personalizeHuman(*/getName()/*)*/, game); - /*player.setFirstController(new PlayerControllerHuman(game, player, this)); + player.setFirstController(new PlayerControllerAi(game, player, this)/*new PlayerControllerHuman(game, player, this)*/); - if (ForgePreferences.DEV_MODE && Singletons.getModel().getPreferences().getPrefBoolean(FPref.DEV_UNLIMITED_LAND)) { + /*if (ForgePreferences.DEV_MODE && Singletons.getModel().getPreferences().getPrefBoolean(FPref.DEV_UNLIMITED_LAND)) { player.canCheatPlayUnlimitedLands = true; }*/ diff --git a/forge-m-base/src/forge/screens/match/MatchController.java b/forge-m-base/src/forge/screens/match/MatchController.java new file mode 100644 index 00000000000..7a42b7d9cdc --- /dev/null +++ b/forge-m-base/src/forge/screens/match/MatchController.java @@ -0,0 +1,130 @@ +package forge.screens.match; + +import java.util.ArrayList; +import java.util.List; +import forge.Forge; +import forge.game.Game; +import forge.game.Match; +import forge.game.player.LobbyPlayer; +import forge.game.player.Player; + +public class MatchController { + private final MatchScreen view; + + private Game game; + private List sortedPlayers; + + public MatchController(MatchScreen view0) { + view = view0; + } + + public final void startGameWithUi(final Match match) { + game = match.createGame(); + + /*if (game.getRules().getGameType() == GameType.Quest) { + QuestController qc = Singletons.getModel().getQuest(); + // Reset new list when the Match round starts, not when each game starts + if (game.getMatch().getPlayedGames().isEmpty()) { + qc.getCards().resetNewList(); + } + game.subscribeToEvents(qc); // this one listens to player's mulligans ATM + }*/ + + //inputQueue = new InputQueue(); + + //game.subscribeToEvents(Singletons.getControl().getSoundSystem()); + + LobbyPlayer humanLobbyPlayer = game.getRegisteredPlayers().get(0).getLobbyPlayer(); //FServer.instance.getLobby().getGuiPlayer(); + // The UI controls should use these game data as models + initMatch(game.getRegisteredPlayers(), humanLobbyPlayer); + + // It's important to run match in a different thread to allow GUI inputs to be invoked from inside game. + // Game is set on pause while gui player takes decisions + game.getAction().invoke(new Runnable() { + @Override + public void run() { + match.startGame(game); + } + }); + } + + public final void endCurrentGame() { + if (this.game == null) { return; } + + Forge.back(); + this.game = null; + } + + public void initMatch(final List players, LobbyPlayer localPlayer) { + // TODO fix for use with multiplayer + + final String[] indices = new String[] { "1", "2" }; //Singletons.getModel().getPreferences().getPref(FPref.UI_AVATARS).split(","); + + // Instantiate all required field slots (user at 0) + sortedPlayers = shiftPlayersPlaceLocalFirst(players, localPlayer); + + /*final List fields = new ArrayList(); + final List commands = new ArrayList(); + + int i = 0; + for (Player p : sortedPlayers) { + // A field must be initialized after it's instantiated, to update player info. + // No player, no init. + VField f = new VField(EDocID.Fields[i], p, localPlayer); + VCommand c = new VCommand(EDocID.Commands[i], p); + fields.add(f); + commands.add(c); + + //setAvatar(f, new ImageIcon(FSkin.getAvatars().get())); + setAvatar(f, getPlayerAvatar(p, Integer.parseInt(indices[i > 2 ? 1 : 0]))); + f.getLayoutControl().initialize(); + c.getLayoutControl().initialize(); + i++; + } + + // Replace old instances + view.setCommandViews(commands); + view.setFieldViews(fields); + + VPlayers.SINGLETON_INSTANCE.init(players);*/ + + initHandViews(localPlayer); + } + + public void initHandViews(LobbyPlayer localPlayer) { + /*final List hands = new ArrayList(); + + int i = 0; + for (Player p : sortedPlayers) { + if (p.getLobbyPlayer() == localPlayer) { + VHand newHand = new VHand(EDocID.Hands[i], p); + newHand.getLayoutControl().initialize(); + hands.add(newHand); + } + i++; + } + + if (hands.isEmpty()) { // add empty hand for matches without human + VHand newHand = new VHand(EDocID.Hands[0], null); + newHand.getLayoutControl().initialize(); + hands.add(newHand); + } + view.setHandViews(hands);*/ + } + + private List shiftPlayersPlaceLocalFirst(final List players, LobbyPlayer localPlayer) { + // get an arranged list so that the first local player is at index 0 + List sortedPlayers = new ArrayList(players); + int ixFirstHuman = -1; + for (int i = 0; i < players.size(); i++) { + if (sortedPlayers.get(i).getLobbyPlayer() == localPlayer) { + ixFirstHuman = i; + break; + } + } + if (ixFirstHuman > 0) { + sortedPlayers.add(0, sortedPlayers.remove(ixFirstHuman)); + } + return sortedPlayers; + } +} diff --git a/forge-m-base/src/forge/screens/match/MatchScreen.java b/forge-m-base/src/forge/screens/match/MatchScreen.java index 8548705ce7f..1b9dc9b5135 100644 --- a/forge-m-base/src/forge/screens/match/MatchScreen.java +++ b/forge-m-base/src/forge/screens/match/MatchScreen.java @@ -5,10 +5,13 @@ import forge.game.Match; public class MatchScreen extends FScreen { private final Match match; + private final MatchController controller; public MatchScreen(Match match0) { super(false, null, true); - this.match = match0; + match = match0; + controller = new MatchController(this); + controller.startGameWithUi(match0); } @Override