network.c
changeset 4716 8a05bf6d951d
parent 4417 df176ef5fa73
child 4826 6a545d194528
equal deleted inserted replaced
4715:175674c3036f 4716:8a05bf6d951d
   284 	if (err >= lengthof(network_error_strings)) err = 0;
   284 	if (err >= lengthof(network_error_strings)) err = 0;
   285 
   285 
   286 	return GetString(buf, network_error_strings[err]);
   286 	return GetString(buf, network_error_strings[err]);
   287 }
   287 }
   288 
   288 
       
   289 /* Count the number of active clients connected */
       
   290 static uint NetworkCountPlayers(void)
       
   291 {
       
   292 	NetworkClientState *cs;
       
   293 	uint count = 0;
       
   294 
       
   295 	FOR_ALL_CLIENTS(cs) {
       
   296 		NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
       
   297 		if (ci->client_playas > 0 && ci->client_playas <= MAX_PLAYERS) count++;
       
   298 	}
       
   299 
       
   300 	return count;
       
   301 }
       
   302 
       
   303 static bool _min_players_paused = false;
       
   304 
       
   305 /* Check if the minimum number of players has been reached and pause or unpause the game as appropriate */
       
   306 void CheckMinPlayers(void)
       
   307 {
       
   308 	if (!_network_dedicated) return;
       
   309 
       
   310 	if (NetworkCountPlayers() < _network_min_players) {
       
   311 		if (_min_players_paused) return;
       
   312 
       
   313 		_min_players_paused = true;
       
   314 		DoCommandP(0, 1, 0, NULL, CMD_PAUSE);
       
   315 		NetworkServer_HandleChat(NETWORK_ACTION_CHAT, DESTTYPE_BROADCAST, 0, "Game paused (not enough players)", NETWORK_SERVER_INDEX);
       
   316 	} else {
       
   317 		if (!_min_players_paused) return;
       
   318 
       
   319 		_min_players_paused = false;
       
   320 		DoCommandP(0, 0, 0, NULL, CMD_PAUSE);
       
   321 		NetworkServer_HandleChat(NETWORK_ACTION_CHAT, DESTTYPE_BROADCAST, 0, "Game unpaused (enough players)", NETWORK_SERVER_INDEX);
       
   322 	}
       
   323 }
       
   324 
   289 // Find all IP-aliases for this host
   325 // Find all IP-aliases for this host
   290 static void NetworkFindIPs(void)
   326 static void NetworkFindIPs(void)
   291 {
   327 {
   292 	int i;
   328 	int i;
   293 
   329 
   624 	// Reset the status of the last socket
   660 	// Reset the status of the last socket
   625 	cs->socket = INVALID_SOCKET;
   661 	cs->socket = INVALID_SOCKET;
   626 	cs->status = STATUS_INACTIVE;
   662 	cs->status = STATUS_INACTIVE;
   627 	cs->index = NETWORK_EMPTY_INDEX;
   663 	cs->index = NETWORK_EMPTY_INDEX;
   628 	ci->client_index = NETWORK_EMPTY_INDEX;
   664 	ci->client_index = NETWORK_EMPTY_INDEX;
       
   665 
       
   666 	CheckMinPlayers();
   629 }
   667 }
   630 
   668 
   631 // A client wants to connect to a server
   669 // A client wants to connect to a server
   632 static bool NetworkConnect(const char *hostname, int port)
   670 static bool NetworkConnect(const char *hostname, int port)
   633 {
   671 {
  1038 
  1076 
  1039 	// execute server initialization script
  1077 	// execute server initialization script
  1040 	IConsoleCmdExec("exec scripts/on_server.scr 0");
  1078 	IConsoleCmdExec("exec scripts/on_server.scr 0");
  1041 	// if the server is dedicated ... add some other script
  1079 	// if the server is dedicated ... add some other script
  1042 	if (_network_dedicated) IConsoleCmdExec("exec scripts/on_dedicated.scr 0");
  1080 	if (_network_dedicated) IConsoleCmdExec("exec scripts/on_dedicated.scr 0");
       
  1081 
       
  1082 	_min_players_paused = false;
       
  1083 	CheckMinPlayers();
  1043 
  1084 
  1044 	/* Try to register us to the master server */
  1085 	/* Try to register us to the master server */
  1045 	_network_last_advertise_frame = 0;
  1086 	_network_last_advertise_frame = 0;
  1046 	_network_need_advertise = true;
  1087 	_network_need_advertise = true;
  1047 	NetworkUDPAdvertise();
  1088 	NetworkUDPAdvertise();