Complete.Org: Mailing Lists: Archives: freeciv-dev: November 2003:
[Freeciv-Dev] (PR#6921) Sort tech list if spy steals
Home

[Freeciv-Dev] (PR#6921) Sort tech list if spy steals

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#6921) Sort tech list if spy steals
From: "Raimar Falke" <i-freeciv-lists@xxxxxxxxxxxxx>
Date: Fri, 21 Nov 2003 23:41:35 -0800
Reply-to: rt@xxxxxxxxxxx

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


 - sort the techs according to the translates names
 - unify the code (gtk1 and xaw, I have to idea what the gtk2 code does here)
 - add extra information (number of steps if you research it
 "classical") (only gtk1)

        Raimar

-- 
 email: rf13@xxxxxxxxxxxxxxxxx
 "That's fundamental game play!  My main enemy is *ALWAYS* fighting 
  a 4-front war.  I make sure of it!"
    -- Tony Stuckey, freeciv-dev

Index: client/climisc.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/climisc.c,v
retrieving revision 1.123
diff -u -u -r1.123 climisc.c
--- client/climisc.c    2003/11/19 17:30:51     1.123
+++ client/climisc.c    2003/11/22 07:41:11
@@ -36,6 +36,7 @@
 #include "game.h"
 #include "log.h"
 #include "map.h"
+#include "mem.h"
 #include "packets.h"
 #include "spaceship.h"
 #include "support.h"
@@ -56,6 +57,10 @@
 
 #include "climisc.h"
 
+struct tech_steal_item tech_steal_list[A_LAST + 1];
+int tech_steal_count;
+static bool tech_steal_initialized = FALSE;
+
 /**************************************************************************
 ...
 **************************************************************************/
@@ -1169,5 +1174,66 @@
                _("Game: %s costs %d gold and you only have %d gold."),
                name, value, game.player_ptr->economic.gold);
     append_output_window(buf);
+  }
+}
+
+/**************************************************************************
+  Helper for fill_advances_list.
+**************************************************************************/
+static int tech_name_cmp(const void *p1, const void *p2)
+{
+  const struct tech_steal_item *pt1 = (const struct tech_steal_item *) p1;
+  const struct tech_steal_item *pt2 = (const struct tech_steal_item *) p2;
+  return strcmp(advances[pt1->tech].name, advances[pt2->tech].name);
+}
+
+/**************************************************************************
+  Fills the tech_steal_list list and sets tech_steal_count with the
+  techs which pplayer can steal from pvictim.
+**************************************************************************/
+void fill_advances_list(struct player *pplayer, struct player *pvictim)
+{
+  int i;
+
+  if (!tech_steal_initialized) {
+    tech_steal_count = 0;
+    tech_steal_initialized = TRUE;
+  }
+
+  for (i = 0; i < tech_steal_count; i++) {
+    free(tech_steal_list[i].unknown);
+  }
+  tech_steal_count = 0;
+
+  tech_type_iterate(i) {
+    if (get_invention(pvictim, i) == TECH_KNOWN &&
+       (get_invention(pplayer, i) == TECH_UNKNOWN ||
+        get_invention(pplayer, i) == TECH_REACHABLE)) {
+      char buffer[10];
+
+      my_snprintf(buffer, sizeof(buffer), "%d",
+                 num_unknown_techs_for_goal(pplayer, i));
+      tech_steal_list[tech_steal_count].tech = i;
+      tech_steal_list[tech_steal_count].name = advances[i].name;
+      tech_steal_list[tech_steal_count].unknown = mystrdup(buffer);
+      tech_steal_count++;
+    }
+  } tech_type_iterate_end;
+
+  qsort(tech_steal_list, tech_steal_count, sizeof(*tech_steal_list),
+       tech_name_cmp);
+
+  if (tech_steal_count > 0) {
+    tech_steal_list[tech_steal_count].tech = game.num_tech_types;
+    tech_steal_list[tech_steal_count].name = _("At Spy's Discretion");
+    tech_steal_list[tech_steal_count].unknown = mystrdup("");
+    tech_steal_count++;
+  }
+
+  if (tech_steal_count == 0) {
+    tech_steal_list[tech_steal_count].tech = -1;
+    tech_steal_list[tech_steal_count].name = _("NONE");
+    tech_steal_list[tech_steal_count].unknown = mystrdup("");
+    tech_steal_count++;
   }
 }
Index: client/climisc.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/climisc.h,v
retrieving revision 1.45
diff -u -u -r1.45 climisc.h
--- client/climisc.h    2003/11/19 04:40:12     1.45
+++ client/climisc.h    2003/11/22 07:41:11
@@ -126,4 +126,14 @@
 
 void cityrep_buy(struct city *pcity);
 
+struct tech_steal_item {
+  char *name, *unknown;
+  Tech_Type_id tech;
+};
+
+extern struct tech_steal_item tech_steal_list[A_LAST + 1];
+extern int tech_steal_count;
+
+void fill_advances_list(struct player *pplayer, struct player *pvictim);
+
 #endif  /* FC__CLIMISC_H */
Index: client/gui-gtk/dialogs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/dialogs.c,v
retrieving revision 1.131
diff -u -u -r1.131 dialogs.c
--- client/gui-gtk/dialogs.c    2003/10/03 05:55:15     1.131
+++ client/gui-gtk/dialogs.c    2003/11/22 07:41:13
@@ -36,6 +36,7 @@
 #include "support.h"
 
 #include "civclient.h"
+#include "climisc.h"
 #include "clinet.h"
 #include "control.h"
 #include "options.h"
@@ -98,7 +99,6 @@
 static GtkWidget  *spy_steal_command;
 
 static int         spy_tech_shell_is_modal;
-static int         advance_type[A_LAST+1];
 static int         steal_advance = 0;
 
 /******************************************************************/
@@ -579,8 +579,8 @@
 *****************************************************************/
 static void spy_select_tech_callback(GtkWidget *w, gint row, gint column)
 {
-  if (advance_type[row] != -1){
-    steal_advance = advance_type[row];
+  if (row >= 0 && row < tech_steal_count) {
+    steal_advance = tech_steal_list[row].tech;
     gtk_widget_set_sensitive(spy_steal_command, TRUE);
     return;
   }
@@ -670,23 +670,24 @@
 /****************************************************************
 ...
 *****************************************************************/
-static int create_advances_list(struct player *pplayer,
-                               struct player *pvictim, bool make_modal)
+static void create_advances_list(struct player *pplayer,
+                                struct player *pvictim, bool make_modal)
 {  
   GtkWidget *close_command, *scrolled;
-  int i, j;
-  static const char *title_[1] = { N_("Select Advance to Steal") };
+  int i;
+  static const char *title_[2] =
+      { N_("Select Advance to Steal"), N_("Steps to research") };
   static gchar **title;
   GtkAccelGroup *accel=gtk_accel_group_new();
 
-  if (!title) title = intl_slist(1, title_);
+  if (!title) title = intl_slist(2, title_);
   
   spy_tech_shell = gtk_dialog_new();
   gtk_window_set_title(GTK_WINDOW(spy_tech_shell),_("Steal Technology"));
   gtk_window_set_position (GTK_WINDOW(spy_tech_shell), GTK_WIN_POS_MOUSE);
   gtk_accel_group_attach(accel, GTK_OBJECT(spy_tech_shell));
   
-  spy_advances_list = gtk_clist_new_with_titles(1, title);
+  spy_advances_list = gtk_clist_new_with_titles(2, title);
   gtk_clist_column_titles_passive(GTK_CLIST(spy_advances_list));
 
   scrolled = gtk_scrolled_window_new(NULL,NULL);
@@ -723,46 +724,21 @@
   /* Now populate the list */
   gtk_clist_freeze(GTK_CLIST(spy_advances_list));
 
-  j = 0;
-  advance_type[j] = -1;
+  fill_advances_list(pplayer,pvictim);
+  for (i = 0; i < tech_steal_count; i++) {
+    gchar *row[2];
 
-  if (pvictim) { /* you don't want to know what lag can do -- Syela */
-    gchar *row[1];
-
-    for(i=A_FIRST; i<game.num_tech_types; i++) {
-      if(get_invention(pvictim, i)==TECH_KNOWN && 
-        (get_invention(pplayer, i)==TECH_UNKNOWN || 
-         get_invention(pplayer, i)==TECH_REACHABLE)) {
-
-       row[0] = advances[i].name;
-       gtk_clist_append(GTK_CLIST(spy_advances_list), row);
-        advance_type[j++] = i;
-      }
-    }
-
-    if(j > 0) {
-      row[0] = _("At Spy's Discretion");
-      gtk_clist_append(GTK_CLIST(spy_advances_list), row);
-      advance_type[j++] = game.num_tech_types;
-    }
-  }
-
-  if(j == 0) {
-    static const char *row_[1] = { N_("NONE") };
-    static gchar **row;
-    
-    if (!row) row = intl_slist(1, row_);
-  
+    row[0] = tech_steal_list[i].name;
+    row[1] = tech_steal_list[i].unknown;
     gtk_clist_append(GTK_CLIST(spy_advances_list), row);
-    j++;
   }
   gtk_clist_thaw(GTK_CLIST(spy_advances_list));
+  gtk_clist_columns_autosize(GTK_CLIST(spy_advances_list));
 
   gtk_widget_set_sensitive(spy_steal_command, FALSE);
   
   gtk_widget_show_all(GTK_DIALOG(spy_tech_shell)->vbox);
   gtk_widget_show_all(GTK_DIALOG(spy_tech_shell)->action_area);
-  return j;
 }
 
 /****************************************************************
Index: client/gui-xaw/dialogs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/dialogs.c,v
retrieving revision 1.89
diff -u -u -r1.89 dialogs.c
--- client/gui-xaw/dialogs.c    2003/08/06 07:34:38     1.89
+++ client/gui-xaw/dialogs.c    2003/11/22 07:41:14
@@ -46,6 +46,7 @@
 #include "chatline.h"
 #include "cityrep.h"   /* for popdown_city_report_dialog */
 #include "civclient.h"
+#include "climisc.h"
 #include "clinet.h"
 #include "control.h" /* request_xxx and set_unit_focus */
 #include "graphics.h"
@@ -86,7 +87,6 @@
 static Widget spy_steal_command;
 
 static int spy_tech_shell_is_modal;
-static int advance_type[A_LAST+1];
 static int steal_advance = 0;
 
 /******************************************************************/
@@ -603,11 +603,11 @@
 static void spy_select_tech_callback(Widget w, XtPointer client_data, 
                                     XtPointer call_data)
 {
-  XawListReturnStruct *ret;
-  ret=XawListShowCurrent(spy_advances_list);
+  XawListReturnStruct *ret = XawListShowCurrent(spy_advances_list);
   
-  if(ret->list_index!=XAW_LIST_NONE && advance_type[ret->list_index] != -1){
-    steal_advance = advance_type[ret->list_index];
+  if (ret->list_index != XAW_LIST_NONE
+      && ret->list_index >= 0 && ret->list_index < tech_steal_count) {
+    steal_advance = tech_steal_list[ret->list_index].tech;
     XtSetSensitive(spy_steal_command, TRUE);
     return;
   }
@@ -682,14 +682,15 @@
 /****************************************************************
 ...
 *****************************************************************/
-static int create_advances_list(struct player *pplayer,
-                               struct player *pvictim, bool make_modal)
+static void create_advances_list(struct player *pplayer,
+                                struct player *pvictim, bool make_modal)
 {  
   Widget spy_tech_form;
   Widget close_command;
   Dimension width1, width2; 
-  int i, j;
+  int i;
 
+  /* needs to be static since XawListChange doesn't copy the values */
   static char *advances_can_steal[A_LAST+1]; 
 
   spy_tech_shell =
@@ -729,29 +730,11 @@
   XtRealizeWidget(spy_tech_shell);
 
   /* Now populate the list */
-  
-  j = 0;
-  advances_can_steal[j] = _("NONE");
-  advance_type[j] = -1;
-
-  if (pvictim) { /* you don't want to know what lag can do -- Syela */
-    for(i=A_FIRST; i<game.num_tech_types; i++) {
-      if(get_invention(pvictim, i)==TECH_KNOWN && 
-         (get_invention(pplayer, i)==TECH_UNKNOWN || 
-          get_invention(pplayer, i)==TECH_REACHABLE)) {
-      
-        advances_can_steal[j] = advances[i].name;
-        advance_type[j++] = i;
-      }
-    }
-    if(j > 0) {
-      advances_can_steal[j] = _("At Spy's Discretion");
-      advance_type[j++] = game.num_tech_types;
-    }
+  fill_advances_list(pplayer, pvictim);
+  for (i = 0; i < tech_steal_count; i++) {
+    advances_can_steal[i] = tech_steal_list[i].name;
   }
-
-  if(j == 0) j++;
-  advances_can_steal[j] = NULL; 
+  advances_can_steal[tech_steal_count] = NULL;
   
   XtSetSensitive(spy_steal_command, FALSE);
   
@@ -760,8 +743,6 @@
   XtVaGetValues(spy_advances_list_label, XtNwidth, &width2, NULL);
   XtVaSetValues(spy_advances_list, XtNwidth, MAX(width1,width2), NULL); 
   XtVaSetValues(spy_advances_list_label, XtNwidth, MAX(width1,width2), NULL); 
-
-  return j;
 }
 
 /****************************************************************

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#6921) Sort tech list if spy steals, Raimar Falke <=