summaryrefslogtreecommitdiff
path: root/network.c
diff options
context:
space:
mode:
authorpeter1138 <peter1138@openttd.org>2006-10-03 16:15:34 +0000
committerpeter1138 <peter1138@openttd.org>2006-10-03 16:15:34 +0000
commit53744881a409f3d1624d0e60e3823370a3900cbe (patch)
tree482f18a4ccfe72d4d8e699b988e5e3ef336eaf86 /network.c
parent950ef8f2373a2bf561d256f42c07606c3ce00854 (diff)
downloadopenttd-53744881a409f3d1624d0e60e3823370a3900cbe.tar.xz
(svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
Diffstat (limited to 'network.c')
-rw-r--r--network.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/network.c b/network.c
index 85d838ea2..7034e65c7 100644
--- a/network.c
+++ b/network.c
@@ -286,6 +286,42 @@ char *GetNetworkErrorMsg(char *buf, NetworkErrorCode err)
return GetString(buf, network_error_strings[err]);
}
+/* Count the number of active clients connected */
+static uint NetworkCountPlayers(void)
+{
+ NetworkClientState *cs;
+ uint count = 0;
+
+ FOR_ALL_CLIENTS(cs) {
+ NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
+ if (ci->client_playas > 0 && ci->client_playas <= MAX_PLAYERS) count++;
+ }
+
+ return count;
+}
+
+static bool _min_players_paused = false;
+
+/* Check if the minimum number of players has been reached and pause or unpause the game as appropriate */
+void CheckMinPlayers(void)
+{
+ if (!_network_dedicated) return;
+
+ if (NetworkCountPlayers() < _network_min_players) {
+ if (_min_players_paused) return;
+
+ _min_players_paused = true;
+ DoCommandP(0, 1, 0, NULL, CMD_PAUSE);
+ NetworkServer_HandleChat(NETWORK_ACTION_CHAT, DESTTYPE_BROADCAST, 0, "Game paused (not enough players)", NETWORK_SERVER_INDEX);
+ } else {
+ if (!_min_players_paused) return;
+
+ _min_players_paused = false;
+ DoCommandP(0, 0, 0, NULL, CMD_PAUSE);
+ NetworkServer_HandleChat(NETWORK_ACTION_CHAT, DESTTYPE_BROADCAST, 0, "Game unpaused (enough players)", NETWORK_SERVER_INDEX);
+ }
+}
+
// Find all IP-aliases for this host
static void NetworkFindIPs(void)
{
@@ -626,6 +662,8 @@ void NetworkCloseClient(NetworkClientState *cs)
cs->status = STATUS_INACTIVE;
cs->index = NETWORK_EMPTY_INDEX;
ci->client_index = NETWORK_EMPTY_INDEX;
+
+ CheckMinPlayers();
}
// A client wants to connect to a server
@@ -1041,6 +1079,9 @@ bool NetworkServerStart(void)
// if the server is dedicated ... add some other script
if (_network_dedicated) IConsoleCmdExec("exec scripts/on_dedicated.scr 0");
+ _min_players_paused = false;
+ CheckMinPlayers();
+
/* Try to register us to the master server */
_network_last_advertise_frame = 0;
_network_need_advertise = true;