Complete.Org: Mailing Lists: Archives: freeciv-dev: June 2004:
[Freeciv-Dev] (PR#8889) change network and client structure of nations_u
Home

[Freeciv-Dev] (PR#8889) change network and client structure of nations_u

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#8889) change network and client structure of nations_used
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 1 Jun 2004 18:08:55 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=8889 >

This patch changes the nations_used data sent by the server to the client.

It is still stored the same way in the server.  However now instead of a 
single packet containing an array of all used nations (which is not 
extensible since the number of used nations has no limit) the server 
sends an empty packet to alert the client that it needs to choose a 
nation, then one small packet for each nation that is unavailable (which 
is usually none since all nations are available).

The client code must change slightly.  It has to remember the list of 
nations available, assembled out of the packets it receives.  The GUI 
functions now take a boolean array rather than an array of nation IDs.

I updated and compiled the GTK2, GTK, XAW, and Win32 clients.  The GTK 
client could be simplified further.  I only tested GTK2, GTK, and XAW.

See also PR#8872.

jason

? data/flags/swiss.bmp
? data/flags/swiss.png
? data/nation/swiss.ruleset
Index: client/packhand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/packhand.c,v
retrieving revision 1.374
diff -u -r1.374 packhand.c
--- client/packhand.c   31 May 2004 16:38:54 -0000      1.374
+++ client/packhand.c   2 Jun 2004 00:43:18 -0000
@@ -2069,25 +2069,47 @@
   }
 }
 
+static bool *nations_used;
+
 /**************************************************************************
-...
+  Mark a nation as unavailable, after we've entered the select-race state.
+**************************************************************************/
+void handle_nation_unavailable(Nation_Type_id nation)
+{
+  if (get_client_state() == CLIENT_SELECT_RACE_STATE
+      && nation >= 0 && nation < game.playable_nation_count) {
+    if (!nations_used[nation]) {
+      nations_used[nation] = TRUE;
+      races_toggles_set_sensitive(nations_used);
+    }
+  } else {
+    freelog(LOG_ERROR,
+           "got a select nation packet in an incompatible state");
+  }
+}
+
+/**************************************************************************
+  Enter the select races state.
 **************************************************************************/
-void handle_nations_selected_info(int num_nations_used,
-                                 Nation_Type_id * nations_used)
+void handle_select_races(void)
 {
-  if (get_client_state() == CLIENT_SELECT_RACE_STATE) {
-    races_toggles_set_sensitive(num_nations_used, nations_used);
-  } else if (get_client_state() == CLIENT_PRE_GAME_STATE) {
+  if (get_client_state() == CLIENT_PRE_GAME_STATE) {
+    /* First set the state. */
     set_client_state(CLIENT_SELECT_RACE_STATE);
 
+    /* Then clear the nations used.  They are filled by a
+     * PACKET_NATION_UNAVAILABLE packet that follows. */
+    nations_used = fc_realloc(nations_used,
+                             game.playable_nation_count
+                             * sizeof(nations_used));
+    memset(nations_used, 0,
+          game.playable_nation_count * sizeof(nations_used));
+
     if (!client_is_observer()) {
+      /* Now close the conndlg and popup the races dialog. */
       really_close_connection_dialog();
       popup_races_dialog();
-      races_toggles_set_sensitive(num_nations_used, nations_used);
     }
-  } else {
-    freelog(LOG_ERROR,
-           "got a select nation packet in an incompatible state");
   }
 }
 
Index: client/gui-gtk/dialogs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/dialogs.c,v
retrieving revision 1.142
diff -u -r1.142 dialogs.c
--- client/gui-gtk/dialogs.c    17 May 2004 01:29:47 -0000      1.142
+++ client/gui-gtk/dialogs.c    2 Jun 2004 00:43:18 -0000
@@ -2189,13 +2189,10 @@
 /**************************************************************************
  ...
 **************************************************************************/
-void races_toggles_set_sensitive(int num_nations_used,
-                                Nation_Type_id * nations_used)
+void races_toggles_set_sensitive(bool *nations_used)
 {
   int i, class_id;
 
-  freelog(LOG_DEBUG, "%d nations used:", num_nations_used);
-
   for (class_id = 0; class_id < num_classes; class_id++) {
     int nations_in_class = g_list_length(sorted_races_list[class_id]);
 
@@ -2203,25 +2200,20 @@
       gtk_widget_set_sensitive(races_toggles[class_id][i], TRUE);
     }
 
-    for (i = 0; i < num_nations_used; i++) {
-      int nation = nations_used[i];
-      int index =
-         g_list_index(sorted_races_list[class_id], GINT_TO_POINTER(nation));
-
-      if (index != -1) {
-       gtk_widget_set_sensitive(races_toggles[class_id][index], FALSE);
+    for (i = 0; i < game.playable_nation_count; i++) {
+      if (nations_used[i]) {
+       int index =
+         g_list_index(sorted_races_list[class_id], GINT_TO_POINTER(i));
+
+       if (index != -1) {
+         gtk_widget_set_sensitive(races_toggles[class_id][index], FALSE);
+       }
       }
     }
   }
 
-  for (i = 0; i < num_nations_used; i++) {
-    int nation = nations_used[i];
-
-    freelog(LOG_DEBUG, "  [%d]: %d = %s", i, nation,
-           get_nation_name(nation));
-    if (nation == selected_nation) {
-      select_random_race();
-    }
+  if (nations_used[selected_nation]) {
+    select_random_race();
   }
 }
 
Index: client/gui-gtk-2.0/dialogs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/dialogs.c,v
retrieving revision 1.67
diff -u -r1.67 dialogs.c
--- client/gui-gtk-2.0/dialogs.c        17 May 2004 01:29:47 -0000      1.67
+++ client/gui-gtk-2.0/dialogs.c        2 Jun 2004 00:43:18 -0000
@@ -1988,16 +1988,13 @@
 /**************************************************************************
   ...
  **************************************************************************/
-void races_toggles_set_sensitive(int num_nations_used,
-                                Nation_Type_id * nations_used)
+void races_toggles_set_sensitive(bool *nations_used)
 {
   GtkTreeModel *model;
   GtkTreeIter it;
   GtkTreePath *path;
   gboolean chosen;
 
-  freelog(LOG_DEBUG, "%d nations used:", num_nations_used);
-
   if (!races_shell) {
     return;
   }
@@ -2005,20 +2002,12 @@
   model = gtk_tree_view_get_model(GTK_TREE_VIEW(races_nation_list));
 
   if (gtk_tree_model_get_iter_first(model, &it)) {
-    int i;
-
     do {
       int nation;
 
       gtk_tree_model_get(model, &it, 0, &nation, -1);
 
-      for (i = 0; i < num_nations_used; i++) {
-       if (nations_used[i] == nation) {
-         break;
-       }
-      }
-
-      chosen = (i < num_nations_used);
+      chosen = nations_used[nation];
       gtk_list_store_set(GTK_LIST_STORE(model), &it, 1, chosen, -1);
 
     } while (gtk_tree_model_iter_next(model, &it));
Index: client/gui-win32/dialogs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-win32/dialogs.c,v
retrieving revision 1.46
diff -u -r1.46 dialogs.c
--- client/gui-win32/dialogs.c  15 May 2004 21:30:34 -0000      1.46
+++ client/gui-win32/dialogs.c  2 Jun 2004 00:43:19 -0000
@@ -857,8 +857,7 @@
 /**************************************************************************
 
 **************************************************************************/
-void races_toggles_set_sensitive(int num_nations_used,
-                                Nation_Type_id * nations_used)
+void races_toggles_set_sensitive(bool *nations_used)
 {
   int i;
 
@@ -866,9 +865,12 @@
     EnableWindow(GetDlgItem(races_dlg, ID_RACESDLG_NATION_BASE + i), TRUE);
   }
 
-  freelog(LOG_DEBUG, "%d nations used:", num_nations_used);
-  for (i = 0; i < num_nations_used; i++) {
-    Nation_Type_id nation = nations_used[i];
+  for (i = 0; i < game.playable_nation_count; i++) {
+    Nation_Type_id nation = i;
+
+    if (!nations_used[i]) {
+      continue;
+    }
 
     freelog(LOG_DEBUG, "  [%d]: %d", i, nation);
 
Index: client/gui-xaw/dialogs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/dialogs.c,v
retrieving revision 1.97
diff -u -r1.97 dialogs.c
--- client/gui-xaw/dialogs.c    17 May 2004 01:29:47 -0000      1.97
+++ client/gui-xaw/dialogs.c    2 Jun 2004 00:43:19 -0000
@@ -1974,8 +1974,7 @@
 /**************************************************************************
 ...
 **************************************************************************/
-void races_toggles_set_sensitive(int num_nations_used,
-                                Nation_Type_id * nations_used)
+void races_toggles_set_sensitive(bool *nations_used)
 {
   int i;
 
@@ -1983,9 +1982,12 @@
     XtSetSensitive(races_toggles[nation_to_race_toggle[i]], TRUE);
   }
 
-  freelog(LOG_DEBUG, "%d nations used:", num_nations_used);
-  for (i = 0; i < num_nations_used; i++) {
-    int nation = nations_used[i], selected_nation = -1;
+  for (i = 0; i < game.playable_nation_count; i++) {
+    int nation = i, selected_nation = -1;
+
+    if (!nations_used[i]) {
+      continue;
+    }
 
     if (races_buttons_get_current() != -1) {
       selected_nation =
Index: client/include/dialogs_g.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/include/dialogs_g.h,v
retrieving revision 1.17
diff -u -r1.17 dialogs_g.h
--- client/include/dialogs_g.h  17 May 2004 01:29:48 -0000      1.17
+++ client/include/dialogs_g.h  2 Jun 2004 00:43:19 -0000
@@ -33,8 +33,7 @@
 
 void popup_unit_select_dialog(struct tile *ptile);
 
-void races_toggles_set_sensitive(int num_nations_used,
-                                Nation_Type_id * nations_used);
+void races_toggles_set_sensitive(bool *nations_used);
 
 void popup_revolution_dialog(void);
 void popup_government_dialog(int governments,
Index: common/packets.def
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/packets.def,v
retrieving revision 1.25
diff -u -r1.25 packets.def
--- common/packets.def  31 May 2004 16:38:54 -0000      1.25
+++ common/packets.def  2 Jun 2004 00:43:19 -0000
@@ -212,7 +212,7 @@
   Spaceship
   Ruleset
 
-The last used packet number is 113.
+The last used packet number is 114.
 ****************************************************/
 
 
@@ -264,9 +264,13 @@
 PACKET_SERVER_SHUTDOWN=8;sc,lsend
 end
 
-PACKET_NATIONS_SELECTED_INFO=9;sc,lsend
-  UINT8 num_nations_used;
-  NATION nations_used[MAX_NUM_PLAYERS:num_nations_used];
+PACKET_NATION_UNAVAILABLE=9;sc,lsend
+  NATION nation;
+end
+
+# Sent to tell the client it needs to select a nation.  The unavailable
+# nations will be sent afterward via PACKET_NATION_UNAVAILABLE
+PACKET_SELECT_RACES=114;sc,lsend
 end
 
 PACKET_NATION_SELECT_REQ=10;cs,dsend
Index: server/srv_main.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/srv_main.c,v
retrieving revision 1.164
diff -u -r1.164 srv_main.c
--- server/srv_main.c   30 May 2004 04:21:00 -0000      1.164
+++ server/srv_main.c   2 Jun 2004 00:43:20 -0000
@@ -1034,19 +1034,18 @@
 **************************************************************************/
 static void send_select_nation(struct player *pplayer)
 {
-  struct packet_nations_selected_info packet;
+  struct packet_nation_unavailable packet;
 
-  packet.num_nations_used = 0;
+  lsend_packet_select_races(&pplayer->connections);
 
   players_iterate(other_player) {
     if (other_player->nation == NO_NATION_SELECTED) {
       continue;
     }
-    packet.nations_used[packet.num_nations_used] = other_player->nation;
-    packet.num_nations_used++;
-  } players_iterate_end;
 
-  lsend_packet_nations_selected_info(&pplayer->connections, &packet);
+    packet.nation = other_player->nation;
+    lsend_packet_nation_unavailable(&pplayer->connections, &packet);
+  } players_iterate_end;
 }
 
 /**************************************************************************

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#8889) change network and client structure of nations_used, Jason Short <=