(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.
authorpeter1138
Tue, 03 Oct 2006 16:15:34 +0000
changeset 4716 d9ca1be9d04b
parent 4715 7ab95d47d2eb
child 4717 873a0ee211af
(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.
console_cmds.c
network.c
network.h
network_server.c
settings.c
--- a/console_cmds.c	Tue Oct 03 16:05:11 2006 +0000
+++ b/console_cmds.c	Tue Oct 03 16:15:34 2006 +0000
@@ -612,6 +612,12 @@
 	return true;
 }
 
+DEF_CONSOLE_HOOK(ConHookCheckMinPlayers)
+{
+	CheckMinPlayers();
+	return true;
+}
+
 DEF_CONSOLE_CMD(ConKick)
 {
 	NetworkClientInfo *ci;
@@ -1580,6 +1586,10 @@
 	IConsoleVarRegister("restart_game_year",     &_network_restart_game_year, ICONSOLE_VAR_UINT16, "Auto-restart the server when Jan 1st of the set year is reached. Use '0' to disable this");
 	IConsoleVarHookAdd("restart_game_year",      ICONSOLE_HOOK_ACCESS, ConHookServerOnly);
 
+	IConsoleVarRegister("min_players",           &_network_min_players, ICONSOLE_VAR_BYTE, "Automatically pause the game when the number of active players passes below the given amount");
+	IConsoleVarHookAdd("min_players",            ICONSOLE_HOOK_ACCESS, ConHookServerOnly);
+	IConsoleVarHookAdd("min_players",            ICONSOLE_HOOK_POST_ACTION, ConHookCheckMinPlayers);
+
 #endif /* ENABLE_NETWORK */
 
 	// debugging stuff
--- a/network.c	Tue Oct 03 16:05:11 2006 +0000
+++ b/network.c	Tue Oct 03 16:15:34 2006 +0000
@@ -286,6 +286,42 @@
 	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 @@
 	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 @@
 	// 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;
--- a/network.h	Tue Oct 03 16:05:11 2006 +0000
+++ b/network.h	Tue Oct 03 16:15:34 2006 +0000
@@ -207,6 +207,7 @@
 VARDEF uint8 _network_autoclean_protected;   // Unprotect a company after X months
 
 VARDEF Year _network_restart_game_year;      // If this year is reached, the server automaticly restarts
+VARDEF uint8 _network_min_players;           // Minimum number of players for game to unpause
 
 NetworkGameList *NetworkQueryServer(const char* host, unsigned short port, bool game_info);
 
@@ -221,6 +222,7 @@
 void NetworkRebuildHostList(void);
 bool NetworkChangeCompanyPassword(byte argc, char *argv[]);
 void NetworkPopulateCompanyInfo(void);
+void CheckMinPlayers(void);
 
 #endif /* ENABLE_NETWORK */
 
--- a/network_server.c	Tue Oct 03 16:05:11 2006 +0000
+++ b/network_server.c	Tue Oct 03 16:15:34 2006 +0000
@@ -932,6 +932,8 @@
 		if (new_cs->status > STATUS_AUTH) {
 			SEND_COMMAND(PACKET_SERVER_QUIT)(new_cs, cs->index, str);
 		}
+
+		CheckMinPlayers();
 	}
 
 	cs->quited = true;
--- a/settings.c	Tue Oct 03 16:05:11 2006 +0000
+++ b/settings.c	Tue Oct 03 16:15:34 2006 +0000
@@ -1210,6 +1210,7 @@
 	 SDTG_VAR("max_clients",          SLE_UINT8, S, 0, _network_game_info.clients_max,    10, 0, 10,   0, STR_NULL, NULL),
 	 SDTG_VAR("max_spectators",       SLE_UINT8, S, 0, _network_game_info.spectators_max, 10, 0, 10,   0, STR_NULL, NULL),
 	 SDTG_VAR("restart_game_year",    SLE_INT32, S,D0, _network_restart_game_year,    0, MIN_YEAR, MAX_YEAR, 1, STR_NULL, NULL),
+	 SDTG_VAR("min_players",          SLE_UINT8, S, 0, _network_min_players,               0, 0, 10,   0, STR_NULL, NULL),
 	 SDTG_END()
 };
 #endif /* ENABLE_NETWORK */