Skip to content

Commit

Permalink
Fix IsSinglePlayer
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-v authored and pchote committed Jun 27, 2017
1 parent 8a508ec commit 78bedb0
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 24 deletions.
9 changes: 7 additions & 2 deletions OpenRA.Game/Network/Session.cs
Expand Up @@ -90,9 +90,14 @@ public string FirstEmptyBotSlot()
return Slots.FirstOrDefault(s => !s.Value.Closed && ClientInSlot(s.Key) == null && s.Value.AllowBots).Key;
}

public bool IsSinglePlayer
public IEnumerable<Client> NonBotClients
{
get { return Clients.Count(c => c.Bot == null) == 1; }
get { return Clients.Where(c => c.Bot == null); }
}

public IEnumerable<Client> NonBotPlayers
{
get { return Clients.Where(c => c.Bot == null && c.Slot != null); }
}

public enum ClientState { NotReady, Invalid, Ready, Disconnected = 1000 }
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Network/UnitOrders.cs
Expand Up @@ -118,7 +118,7 @@ public static void ProcessOrder(OrderManager orderManager, World world, int clie
if (client != null)
{
var pause = order.TargetString == "Pause";
if (orderManager.World.Paused != pause && world != null && !world.LobbyInfo.IsSinglePlayer)
if (orderManager.World.Paused != pause && world != null && world.LobbyInfo.NonBotClients.Count() > 1)
{
var pausetext = "The game is {0} by {1}".F(pause ? "paused" : "un-paused", client.Name);
Game.AddChatLine(Color.White, ServerChatName, pausetext);
Expand Down
6 changes: 3 additions & 3 deletions OpenRA.Game/Server/Server.cs
Expand Up @@ -375,7 +375,7 @@ void ValidateClient(Connection newConn, string data)
Log.Write("server", "{0} ({1}) has joined the game.",
client.Name, newConn.Socket.RemoteEndPoint);

if (Dedicated || !LobbyInfo.IsSinglePlayer)
if (LobbyInfo.NonBotClients.Count() > 1)
SendMessage("{0} has joined the game.".F(client.Name));

// Send initial ping
Expand All @@ -392,7 +392,7 @@ void ValidateClient(Connection newConn, string data)
SendOrderTo(newConn, "Message", motd);
}

if (!LobbyInfo.IsSinglePlayer && Map.DefinesUnsafeCustomRules)
if (Map.DefinesUnsafeCustomRules)
SendOrderTo(newConn, "Message", "This map contains custom rules. Game experience may change.");

if (!LobbyInfo.GlobalSettings.EnableSingleplayer)
Expand Down Expand Up @@ -679,7 +679,7 @@ public void StartGame()
}

// HACK: Turn down the latency if there is only one real player
if (LobbyInfo.IsSinglePlayer)
if (LobbyInfo.NonBotClients.Count() == 1)
LobbyInfo.GlobalSettings.OrderLatency = 1;

SyncLobbyInfo();
Expand Down
3 changes: 2 additions & 1 deletion OpenRA.Game/Traits/Player/DeveloperMode.cs
Expand Up @@ -10,6 +10,7 @@
#endregion

using System.Collections.Generic;
using System.Linq;

namespace OpenRA.Traits
{
Expand Down Expand Up @@ -113,7 +114,7 @@ public DeveloperMode(DeveloperModeInfo info)

void INotifyCreated.Created(Actor self)
{
Enabled = self.World.LobbyInfo.IsSinglePlayer || self.World.LobbyInfo.GlobalSettings
Enabled = self.World.LobbyInfo.NonBotPlayers.Count() == 1 || self.World.LobbyInfo.GlobalSettings
.OptionOrDefault("cheats", info.Enabled);
}

Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Scripting/Global/MapGlobal.cs
Expand Up @@ -108,7 +108,7 @@ public string TerrainType(CPos cell)
}

[Desc("Returns true if there is only one human player.")]
public bool IsSinglePlayer { get { return Context.World.LobbyInfo.IsSinglePlayer; } }
public bool IsSinglePlayer { get { return Context.World.LobbyInfo.NonBotPlayers.Count() == 1; } }

[Desc("Returns the difficulty selected by the player before starting the mission.")]
public string Difficulty
Expand Down
19 changes: 9 additions & 10 deletions OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs
Expand Up @@ -58,19 +58,19 @@ public static bool ValidateCommand(S server, Connection conn, Session.Client cli

static void CheckAutoStart(S server)
{
// A spectating admin is included for checking these rules
var playerClients = server.LobbyInfo.Clients.Where(c => (c.Bot == null && c.Slot != null) || c.IsAdmin);
var nonBotPlayers = server.LobbyInfo.NonBotPlayers;

// Are all players ready?
if (!playerClients.Any() || playerClients.Any(c => c.State != Session.ClientState.Ready))
// Are all players and admin (could be spectating) ready?
if (nonBotPlayers.Any(c => c.State != Session.ClientState.Ready) ||
server.LobbyInfo.Clients.First(c => c.IsAdmin).State != Session.ClientState.Ready)
return;

// Are the map conditions satisfied?
if (server.LobbyInfo.Slots.Any(sl => sl.Value.Required && server.LobbyInfo.ClientInSlot(sl.Key) == null))
// Does server have at least 2 human players?
if (!server.LobbyInfo.GlobalSettings.EnableSingleplayer && nonBotPlayers.Count() < 2)
return;

// Does server have only one player?
if (!server.LobbyInfo.GlobalSettings.EnableSingleplayer && playerClients.Count() == 1)
// Are the map conditions satisfied?
if (server.LobbyInfo.Slots.Any(sl => sl.Value.Required && server.LobbyInfo.ClientInSlot(sl.Key) == null))
return;

server.StartGame();
Expand Down Expand Up @@ -121,8 +121,7 @@ public bool InterpretCommand(S server, Connection conn, Session.Client client, s
return true;
}
if (!server.LobbyInfo.GlobalSettings.EnableSingleplayer &&
server.LobbyInfo.Clients.Where(c => c.Bot == null && c.Slot != null).Count() == 1)
if (!server.LobbyInfo.GlobalSettings.EnableSingleplayer && server.LobbyInfo.NonBotPlayers.Count() < 2)
{
server.SendOrderTo(conn, "Message", server.TwoHumansRequiredText);
return true;
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/ServerTraits/PlayerPinger.cs
Expand Up @@ -36,7 +36,7 @@ public void Tick(S server)
lastPing = Game.RunTime;

// Ignore client timeout in singleplayer games to make debugging easier
if (server.LobbyInfo.IsSinglePlayer && !server.Dedicated)
if (server.LobbyInfo.NonBotClients.Count() < 2 && !server.Dedicated)
foreach (var c in server.Conns.ToList())
server.SendOrderTo(c, "Ping", Game.RunTime.ToString());
else
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Widgets/Logic/Ingame/IngameChatLogic.cs
Expand Up @@ -47,7 +47,7 @@ public IngameChatLogic(Widget widget, OrderManager orderManager, World world, Mo
chatTraits = world.WorldActor.TraitsImplementing<INotifyChat>().ToArray();

var players = world.Players.Where(p => p != world.LocalPlayer && !p.NonCombatant && !p.IsBot);
disableTeamChat = world.IsReplay || world.LobbyInfo.IsSinglePlayer || (world.LocalPlayer != null && !players.Any(p => p.IsAlliedWith(world.LocalPlayer)));
disableTeamChat = world.IsReplay || world.LobbyInfo.NonBotClients.Count() == 1 || (world.LocalPlayer != null && !players.Any(p => p.IsAlliedWith(world.LocalPlayer)));
teamChat = !disableTeamChat;

tabCompletion.Commands = chatTraits.OfType<ChatCommands>().SelectMany(x => x.Commands.Keys).ToList();
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Widgets/Logic/Ingame/IngameMenuLogic.cs
Expand Up @@ -97,7 +97,7 @@ public IngameMenuLogic(Widget widget, ModData modData, World world, Action onExi
var iop = world.WorldActor.TraitsImplementing<IObjectivesPanel>().FirstOrDefault();
var exitDelay = iop != null ? iop.ExitDelay : 0;
if (world.LobbyInfo.IsSinglePlayer)
if (world.LobbyInfo.NonBotClients.Count() == 1)
{
restartAction = () =>
{
Expand Down
Expand Up @@ -102,7 +102,7 @@ void OpenMenuPanel(MenuButtonWidget button, WidgetArgs widgetArgs = null)
worldRoot.IsVisible = () => false;
}

if (button.Pause && world.LobbyInfo.IsSinglePlayer)
if (button.Pause && world.LobbyInfo.NonBotClients.Count() == 1)
world.SetPauseState(true);

var cachedDisableWorldSounds = Game.Sound.DisableWorldSounds;
Expand All @@ -118,7 +118,7 @@ void OpenMenuPanel(MenuButtonWidget button, WidgetArgs widgetArgs = null)
if (button.DisableWorldSounds)
Game.Sound.DisableWorldSounds = cachedDisableWorldSounds;
if (button.Pause && world.LobbyInfo.IsSinglePlayer)
if (button.Pause && world.LobbyInfo.NonBotClients.Count() == 1)
world.SetPauseState(cachedPause);
menuRoot.RemoveChild(currentWidget);
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Widgets/Logic/Lobby/LobbyLogic.cs
Expand Up @@ -322,7 +322,7 @@ void ConnectionStateChanged(OrderManager om)
{
startGameButton.IsDisabled = () => configurationDisabled() || map.Status != MapStatus.Available ||
orderManager.LobbyInfo.Slots.Any(sl => sl.Value.Required && orderManager.LobbyInfo.ClientInSlot(sl.Key) == null) ||
(!orderManager.LobbyInfo.GlobalSettings.EnableSingleplayer && orderManager.LobbyInfo.IsSinglePlayer);
(!orderManager.LobbyInfo.GlobalSettings.EnableSingleplayer && orderManager.LobbyInfo.NonBotPlayers.Count() < 2);

startGameButton.OnClick = () =>
{
Expand Down

0 comments on commit 78bedb0

Please sign in to comment.