From bcbd18bb5291bdea6cae3fcfe2a2a47660103b5e Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 18 Jun 2011 00:39:50 +1200 Subject: [PATCH] Skirmish mode starts with a bot selected. --- OpenRA.Game/Network/Session.cs | 6 ++++++ OpenRA.Game/Server/Server.cs | 9 +-------- OpenRA.Game/Widgets/WidgetUtils.cs | 2 ++ OpenRA.Mods.Cnc/Widgets/Logic/CncLobbyLogic.cs | 16 ++++++++++++++-- OpenRA.Mods.Cnc/Widgets/Logic/CncMenuLogic.cs | 13 +++++++------ 5 files changed, 30 insertions(+), 16 deletions(-) diff --git a/OpenRA.Game/Network/Session.cs b/OpenRA.Game/Network/Session.cs index 5d8dd9d6f6..8e6710b7e3 100644 --- a/OpenRA.Game/Network/Session.cs +++ b/OpenRA.Game/Network/Session.cs @@ -31,6 +31,12 @@ namespace OpenRA.Network return Clients.SingleOrDefault(c => c.Slot == slot.Index); } + public int FirstEmptySlot() + { + return Slots.First(s => !s.Closed && ClientInSlot(s) == null + && s.Bot == null).Index; + } + public enum ClientState { NotReady, diff --git a/OpenRA.Game/Server/Server.cs b/OpenRA.Game/Server/Server.cs index 9a8d009154..bb8b9da479 100644 --- a/OpenRA.Game/Server/Server.cs +++ b/OpenRA.Game/Server/Server.cs @@ -216,7 +216,7 @@ namespace OpenRA.Server // Enforce correct PlayerIndex and Slot client.Index = newConn.PlayerIndex; - client.Slot = ChooseFreeSlot(); + client.Slot = lobbyInfo.FirstEmptySlot(); var slotData = lobbyInfo.Slots.FirstOrDefault( x => x.Index == client.Slot ); if (slotData != null && slotData.MapPlayer != null) @@ -235,13 +235,6 @@ namespace OpenRA.Server } catch (Exception) { DropClient(newConn); } } - - int ChooseFreeSlot() - { - return lobbyInfo.Slots.First(s => !s.Closed && s.Bot == null - && !lobbyInfo.Clients.Any( c => c.Slot == s.Index )).Index; - } - public static void SyncClientToPlayerReference(Session.Client c, PlayerReference pr) { diff --git a/OpenRA.Game/Widgets/WidgetUtils.cs b/OpenRA.Game/Widgets/WidgetUtils.cs index b93ca0f184..d49a1e1cec 100644 --- a/OpenRA.Game/Widgets/WidgetUtils.cs +++ b/OpenRA.Game/Widgets/WidgetUtils.cs @@ -199,6 +199,8 @@ namespace OpenRA.Widgets } return text; } + + public static Action Once( Action a ) { return () => { if (a != null) { a(); a = null; } }; } } [Flags] diff --git a/OpenRA.Mods.Cnc/Widgets/Logic/CncLobbyLogic.cs b/OpenRA.Mods.Cnc/Widgets/Logic/CncLobbyLogic.cs index 833ea0470d..bc70913f0c 100644 --- a/OpenRA.Mods.Cnc/Widgets/Logic/CncLobbyLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/Logic/CncLobbyLogic.cs @@ -50,7 +50,8 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic Game.OpenWindow("SERVER_LOBBY", new WidgetArgs() { { "onExit", onExit }, - { "onStart", OnGameStart } + { "onStart", OnGameStart }, + { "addBots", false } }); }; @@ -86,7 +87,8 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic [ObjectCreator.Param] World world, // Shellmap world [ObjectCreator.Param] OrderManager orderManager, [ObjectCreator.Param] Action onExit, - [ObjectCreator.Param] Action onStart) + [ObjectCreator.Param] Action onStart, + [ObjectCreator.Param] bool addBots) { this.orderManager = orderManager; this.OnGameStart = () => { CloseWindow(); onStart(); }; @@ -222,6 +224,16 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic { "onExit", () => {} }, }); }; + + // Add a bot on the first lobbyinfo update + if (addBots) + Game.LobbyInfoChanged += WidgetUtils.Once(() => + { + var slot = orderManager.LobbyInfo.FirstEmptySlot(); + var bot = Rules.Info["player"].Traits.WithInterface().Select(t => t.Name).FirstOrDefault(); + if (bot != null) + orderManager.IssueOrder(Order.Command("slot_bot {0} {1}".F(slot, bot))); + }); } public void AddChatLine(Color c, string from, string text) diff --git a/OpenRA.Mods.Cnc/Widgets/Logic/CncMenuLogic.cs b/OpenRA.Mods.Cnc/Widgets/Logic/CncMenuLogic.cs index 0ab0a40d0f..85256c4cb0 100644 --- a/OpenRA.Mods.Cnc/Widgets/Logic/CncMenuLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/Logic/CncMenuLogic.cs @@ -68,7 +68,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic Widget.OpenWindow("SERVERBROWSER_PANEL", new WidgetArgs() { { "onExit", () => Menu = MenuType.Multiplayer }, - { "openLobby", () => OpenLobbyPanel(MenuType.Multiplayer) } + { "openLobby", () => OpenLobbyPanel(MenuType.Multiplayer, false) } }); }; @@ -78,7 +78,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic Widget.OpenWindow("CREATESERVER_PANEL", new WidgetArgs() { { "onExit", () => Menu = MenuType.Multiplayer }, - { "openLobby", () => OpenLobbyPanel(MenuType.Multiplayer) } + { "openLobby", () => OpenLobbyPanel(MenuType.Multiplayer, false) } }); }; @@ -88,7 +88,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic Widget.OpenWindow("DIRECTCONNECT_PANEL", new WidgetArgs() { { "onExit", () => Menu = MenuType.Multiplayer }, - { "openLobby", () => OpenLobbyPanel(MenuType.Multiplayer) } + { "openLobby", () => OpenLobbyPanel(MenuType.Multiplayer, false) } }); }; @@ -140,13 +140,14 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic rootMenu.Parent.RemoveChild(rootMenu); } - void OpenLobbyPanel(MenuType menu) + void OpenLobbyPanel(MenuType menu, bool addBots) { Menu = MenuType.None; Game.OpenWindow("SERVER_LOBBY", new WidgetArgs() { { "onExit", () => { Game.Disconnect(); Menu = menu; } }, - { "onStart", RemoveShellmapUI } + { "onStart", RemoveShellmapUI }, + { "addBots", addBots } }); } @@ -156,7 +157,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic var port = Game.CreateLocalServer(map); CncConnectingLogic.Connect(IPAddress.Loopback.ToString(), port, - () => OpenLobbyPanel(MenuType.Main), + () => OpenLobbyPanel(MenuType.Main, true), () => { Game.CloseServer(); Menu = MenuType.Main; }); } }