(svn r1186) -Fix: [Network] You can now join a company on a server where a company
authortruelight
Mon, 20 Dec 2004 16:02:01 +0000
changeset 734 f4ad2f5805fd
parent 733 ac3d7e1b786e
child 735 995f97df3bd7
(svn r1186) -Fix: [Network] You can now join a company on a server where a company
went bankrupt, without joining the wrong company (or even failing to do so)
network.c
network_client.c
network_gui.c
network_server.c
--- a/network.c	Mon Dec 20 15:26:19 2004 +0000
+++ b/network.c	Mon Dec 20 16:02:01 2004 +0000
@@ -762,6 +762,7 @@
 	// Clean the client_info memory
 	memset(_network_client_info, 0, sizeof(_network_client_info));
 	memset(_network_player_info, 0, sizeof(_network_player_info));
+	_network_lobby_company_count = 0;
 
 	_sync_frame = 0;
 	_network_first_time = true;
--- a/network_client.c	Mon Dec 20 15:26:19 2004 +0000
+++ b/network_client.c	Mon Dec 20 16:02:01 2004 +0000
@@ -275,16 +275,17 @@
 		byte current;
 
 		total = NetworkRecv_uint8(p);
-		_network_lobby_company_count = total;
 
 		// There is no data at all..
 		if (total == 0)
 			return NETWORK_RECV_STATUS_CLOSE_QUERY;
 
-		current = NetworkRecv_uint8(p) - 1;
+		current = NetworkRecv_uint8(p);
 		if (current >= MAX_PLAYERS)
 			return NETWORK_RECV_STATUS_CLOSE_QUERY;
 
+		_network_lobby_company_count++;
+
 		NetworkRecv_string(p, _network_player_info[current].company_name, sizeof(_network_player_info[current].company_name));
 		_network_player_info[current].inaugurated_year = NetworkRecv_uint8(p);
 		_network_player_info[current].company_value = NetworkRecv_uint64(p);
@@ -300,11 +301,7 @@
 
 		InvalidateWindow(WC_NETWORK_WINDOW, 0);
 
-		if (total == current + 1)
-			// This was the last one
-			return NETWORK_RECV_STATUS_CLOSE_QUERY;
-		else
-			return NETWORK_RECV_STATUS_OKAY;
+		return NETWORK_RECV_STATUS_OKAY;
 	}
 
 	return NETWORK_RECV_STATUS_CLOSE_QUERY;
--- a/network_gui.c	Mon Dec 20 15:26:19 2004 +0000
+++ b/network_gui.c	Mon Dec 20 16:02:01 2004 +0000
@@ -614,6 +614,21 @@
 	WP(w,querystr_d).buf = _edit_str_buf;
 }
 
+static byte NetworkLobbyFindCompanyIndex(byte pos)
+{
+	byte i;
+	/* Scroll through all _network_player_info and get the 'pos' item
+	    that is not empty */
+	for (i = 0; i < MAX_PLAYERS; i++) {
+		if (_network_player_info[i].company_name[0] != '\0') {
+			if (pos-- == 0)
+				return i;
+		}
+	}
+
+	return 0;
+}
+
 static void NetworkLobbyWindowWndProc(Window *w, WindowEvent *e)
 {
 	switch(e->event) {
@@ -638,10 +653,11 @@
 		GfxFillRect(11, 41, 139, 165, 0xD7);
 		pos = w->vscroll.pos;
 		while (pos < _network_lobby_company_count) {
-			if (_selected_company_item == pos)
+			byte index = NetworkLobbyFindCompanyIndex(pos);
+			if (_selected_company_item == index)
 				GfxFillRect(11, y - 1, 139, y + 10, 155); // show highlighted item with a different colour
 
-			DoDrawString(_network_player_info[pos].company_name, 13, y, 2);
+			DoDrawString(_network_player_info[index].company_name, 13, y, 2);
 
 			pos++;
 			y += NET_PRC__SIZE_OF_ROW_COMPANY;
@@ -728,6 +744,8 @@
 				return;
 			}
 
+			_selected_company_item = NetworkLobbyFindCompanyIndex(_selected_company_item);
+
 			SetWindowDirty(w);
 			break;
 		case 7: /* Join company */
--- a/network_server.c	Mon Dec 20 15:26:19 2004 +0000
+++ b/network_server.c	Mon Dec 20 16:02:01 2004 +0000
@@ -67,8 +67,6 @@
 	Packet *p;
 
 	byte active = 0;
-	byte current = 0;
-
 
 	FOR_ALL_PLAYERS(player) {
 		if (player->is_active)
@@ -91,13 +89,11 @@
 		if (!player->is_active)
 			continue;
 
-		current++;
-
 		p = NetworkSend_Init(PACKET_SERVER_COMPANY_INFO);
 
 		NetworkSend_uint8 (p, NETWORK_COMPANY_INFO_VERSION);
 		NetworkSend_uint8 (p, active);
-		NetworkSend_uint8 (p, current);
+		NetworkSend_uint8 (p, player->index);
 
 		NetworkSend_string(p, _network_player_info[player->index].company_name);
 		NetworkSend_uint8 (p, _network_player_info[player->index].inaugurated_year);
@@ -119,6 +115,13 @@
 
 		NetworkSend_Packet(p, cs);
 	}
+
+	p = NetworkSend_Init(PACKET_SERVER_COMPANY_INFO);
+
+	NetworkSend_uint8 (p, NETWORK_COMPANY_INFO_VERSION);
+	NetworkSend_uint8 (p, 0);
+
+	NetworkSend_Packet(p, cs);
 }
 
 DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR)(NetworkClientState *cs, NetworkErrorCode error)