Complete.Org: Mailing Lists: Archives: freeciv-dev: July 2004:
[Freeciv-Dev] (PR#9423) two new repodlg functions
Home

[Freeciv-Dev] (PR#9423) two new repodlg functions

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#9423) two new repodlg functions
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 18 Jul 2004 17:27:13 -0700
Reply-to: rt@xxxxxxxxxxx

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

This patch makes two new repodlg_common functions:

- sell_all_improvements sells all improvements.  Depending on parameters 
it may only sell obsolete/replaced improvments.

- disband_all_units disbands all supported units.  Depending on 
parameters it may only disband units inside cities.

Both functions return a GUI-friendly message string.

These functions are used in the report dialogs of numerous clients. 
This simplifies the logic, lessens the amount of code, and (possibly) 
lowers the number of translatable strings.

It is also effects-friendly since the eff patch renames 
wonder_replacement() which is used in sell_all_improvements.

It is however a bit hard to test.  Each client has its own muddled logic 
about whether obsolete improvements or all improvements are to be sold; 
it's possible I reversed the logic somewhere.  Also I have no idea how 
to disband all units in the client.  Neither the economy dialog nor the 
military dialog allows this.

jason

? diff
Index: client/repodlgs_common.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/repodlgs_common.c,v
retrieving revision 1.10
diff -u -r1.10 repodlgs_common.c
--- client/repodlgs_common.c    25 Jun 2004 23:35:55 -0000      1.10
+++ client/repodlgs_common.c    19 Jul 2004 00:22:10 -0000
@@ -17,12 +17,16 @@
 
 #include <assert.h>
 
+#include "fcintl.h"
 #include "game.h"
 #include "government.h"
 #include "mem.h"               /* free */
+#include "support.h"           /* my_snprintf */
 
 #include "repodlgs_g.h"
 
+#include "civclient.h"         /* can_client_issue_orders */
+#include "control.h"
 #include "repodlgs_common.h"
 #include "packhand_gen.h"
 
@@ -295,3 +299,81 @@
     popup_settable_options_dialog();
   }
 }
+
+/****************************************************************************
+  Sell all improvements of the given type in all cities.  If "obsolete_only"
+  is specified then only those improvements that are replaced will be sold.
+
+  The "message" string will be filled with a GUI-friendly message about
+  what was sold.
+****************************************************************************/
+void sell_all_improvements(Impr_Type_id impr, bool obsolete_only,
+                          char *message, size_t message_sz)
+{
+  int count = 0, gold = 0;
+
+  if (!can_client_issue_orders()) {
+    my_snprintf(message, message_sz, _("You cannot sell improvements."));
+    return;
+  }
+
+  city_list_iterate(game.player_ptr->cities, pcity) {
+    if (!pcity->did_sell && city_got_building(pcity, impr)
+       && (!obsolete_only
+           || improvement_obsolete(game.player_ptr, impr)
+           || wonder_replacement(pcity, impr))) {
+      count++;
+      gold += impr_sell_gold(impr);
+      city_sell_improvement(pcity, impr);
+    }
+  } city_list_iterate_end;
+
+  if (count > 0) {
+    my_snprintf(message, message_sz, _("Sold %d %s for %d gold."),
+               count, get_improvement_name(impr), gold);
+  } else {
+    my_snprintf(message, message_sz, _("No %s could be sold."),
+               get_improvement_name(impr));
+  }
+}
+
+/****************************************************************************
+  Disband all supported units of the given type.  If in_cities_only is
+  specified then only units inside our cities will be disbanded.
+
+  The "message" string will be filled with a GUI-friendly message about
+  what was sold.
+****************************************************************************/
+void disband_all_units(Unit_Type_id type, bool in_cities_only,
+                      char *message, size_t message_sz)
+{
+  int count = 0;
+
+  if (!can_client_issue_orders()) {
+    my_snprintf(message, message_sz, _("You cannot disband units."));
+    return;
+  }
+
+  city_list_iterate(game.player_ptr->cities, pcity) {
+    /* Only supported units are disbanded.  Units with no homecity have no
+     * cost and are not disbanded. */
+    unit_list_iterate(pcity->units_supported, punit) {
+      struct city *incity = map_get_city(punit->x, punit->y);
+
+      if (punit->type == type
+         && (!in_cities_only
+             || (incity && city_owner(incity) == game.player_ptr))) {
+       count++;
+       request_unit_disband(punit);
+      }
+    } unit_list_iterate_end;
+  } city_list_iterate_end;
+
+  if (count > 0) {
+    my_snprintf(message, message_sz, _("Disbanded %d %s."),
+               count, unit_name(type));
+  } else {
+    my_snprintf(message, message_sz, _("No %s could be disbanded."),
+               unit_name(type));
+  }
+}
Index: client/repodlgs_common.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/repodlgs_common.h,v
retrieving revision 1.6
diff -u -r1.6 repodlgs_common.h
--- client/repodlgs_common.h    12 Apr 2004 14:17:36 -0000      1.6
+++ client/repodlgs_common.h    19 Jul 2004 00:22:10 -0000
@@ -73,4 +73,9 @@
 void settable_options_init(void);
 void settable_options_free(void);
 
+void sell_all_improvements(Impr_Type_id impr, bool obsolete_only,
+                          char *message, size_t message_sz);
+void disband_all_units(Unit_Type_id type, bool in_cities_only,
+                      char *message, size_t message_sz);
+
 #endif /* FC__REPODLGS_COMMON_H */
Index: client/gui-gtk/repodlgs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/repodlgs.c,v
retrieving revision 1.81
diff -u -r1.81 repodlgs.c
--- client/gui-gtk/repodlgs.c   23 Apr 2004 22:58:05 -0000      1.81
+++ client/gui-gtk/repodlgs.c   19 Jul 2004 00:22:10 -0000
@@ -694,9 +694,8 @@
 *****************************************************************/
 void economy_selloff_callback(GtkWidget *w, gpointer data)
 {
-  int count = 0, gold = 0;
   struct economy_row row_type;
-  char str[64];
+  char str[1024];
   GList *selection;
   int row;
 
@@ -706,54 +705,16 @@
     row_type = economy_row_type[row];
     
     if (row_type.is_impr == TRUE) {
-      city_list_iterate(game.player_ptr->cities, pcity) {
-       if (!pcity->did_sell && city_got_building(pcity, row_type.type)
-           && (data
-               || improvement_obsolete(game.player_ptr, row_type.type)
-               || wonder_replacement(pcity, row_type.type))) {
-         count++;
-         gold += impr_sell_gold(row_type.type);
-         city_sell_improvement(pcity, row_type.type);
-       }
-      } city_list_iterate_end;
-      
-      if (count) {
-       my_snprintf(str, sizeof(str), _("Sold %d %s for %d gold"),
-                   count, get_improvement_name(row_type.type), gold);
-      } else {
-       my_snprintf(str, sizeof(str), _("No %s could be sold"),
-               get_improvement_name(row_type.type));
-      }
+      sell_all_improvements(row_type.type, data == NULL,
+                           str, sizeof(str));
     } else {
-      /* With this code only units supported by cities will be disbanded
-       * That's like this because it is a non-sense of selling units with
-       * no upkeep */
-      city_list_iterate(game.player_ptr->cities, pcity) {
-       unit_list_iterate(pcity->units_supported, punit) {
-         /* We don't sell obsolete units when sell obsolete is clicked. 
-          * Indeed, unlike improvements, obsolete units can fight like
-          * up-to-date ones */
-         if (punit->type == row_type.type && data) {
-           count++;
-           request_unit_disband(punit);
-         }
-       } unit_list_iterate_end;
-      } city_list_iterate_end;
-      
-      if (count > 0) {
-       my_snprintf(str, sizeof(str), "Disbanded %d %s", count,
-                   unit_name(row_type.type));
-      } else {
-       my_snprintf(str, sizeof(str), "No %s could be disbanded", 
-                   unit_name(row_type.type));
-      }
+      disband_all_units(row_type.type, FALSE, str, sizeof(str));
     }
  
     gtk_clist_unselect_row(GTK_CLIST(economy_list), row, 0);
     popup_notify_dialog(_("Sell-Off:"),_("Results"), str);
     
   }
-  return;
 }
 
 /****************************************************************
Index: client/gui-gtk-2.0/repodlgs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/repodlgs.c,v
retrieving revision 1.50
diff -u -r1.50 repodlgs.c
--- client/gui-gtk-2.0/repodlgs.c       11 May 2004 17:52:25 -0000      1.50
+++ client/gui-gtk-2.0/repodlgs.c       19 Jul 2004 00:22:10 -0000
@@ -750,9 +750,10 @@
 *****************************************************************/
 static void economy_command_callback(GtkWidget *w, gint response_id)
 {
-  int i, count = 0, gold = 0, is_impr;
+  int i, is_impr;
   gint row;
   GtkWidget *shell;
+  char buf[1024];
 
   if (response_id != ECONOMY_SELL_OBSOLETE
       && response_id != ECONOMY_SELL_ALL) {
@@ -783,59 +784,20 @@
       }
     }
 
-    city_list_iterate(game.player_ptr->cities, pcity) {
-      if (!pcity->did_sell && city_got_building(pcity, i ) &&
-         (response_id == ECONOMY_SELL_ALL ||
-          improvement_obsolete(game.player_ptr, i) ||
-          wonder_replacement(pcity, i) )) {
-       count++; 
-       gold += impr_sell_gold(i);
-       city_sell_improvement(pcity, i);
-      }
-    } city_list_iterate_end;
-
-    if (count > 0) {
-      shell = gtk_message_dialog_new(GTK_WINDOW(economy_dialog_shell),
-                                    GTK_DIALOG_DESTROY_WITH_PARENT,
-                                    GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE,
-                                    _("Sold %d %s for %d gold"), count,
-                                    get_improvement_name(i), gold);
-    } else {
-      shell = gtk_message_dialog_new(GTK_WINDOW(economy_dialog_shell),
-                                    GTK_DIALOG_DESTROY_WITH_PARENT,
-                                    GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE,
-                                    _("No %s could be sold"), 
-                                    get_improvement_name(i));
-    }
+    sell_all_improvements(i, response_id != ECONOMY_SELL_ALL,
+                         buf, sizeof(buf));
   } else {
-    city_list_iterate(game.player_ptr->cities, pcity) {
-      unit_list_iterate(pcity->units_supported, punit) {
-       /* We don't sell obsolete units when sell obsolete is clicked
-        * Indeed, unlike improvements, obsolete units can fight like
-        * up-to-dates ones. And they are also all obsolete at the same
-        * time so if the user want to sell them off, he can use the
-        * sell all button */
-       if (punit->type == i && response_id == ECONOMY_SELL_ALL) {
-         count++;
-         request_unit_disband(punit);
-       }
-      } unit_list_iterate_end;
-    } city_list_iterate_end;
-    
-    if (count > 0) {
-      shell = gtk_message_dialog_new(GTK_WINDOW(economy_dialog_shell),
-                                    GTK_DIALOG_DESTROY_WITH_PARENT,
-                                    GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE,
-                                    _("Disbanded %d %s."), count,
-                                    unit_name(i));
-    } else {
-      shell = gtk_message_dialog_new(GTK_WINDOW(economy_dialog_shell),
-                                    GTK_DIALOG_DESTROY_WITH_PARENT,
-                                    GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE,
-                                    _("No %s could be disbanded"),
-                                    unit_name(i));
+    if (response_id == ECONOMY_SELL_OBSOLETE) {
+      return;
     }
+    disband_all_units(i, FALSE, buf, sizeof(buf));
   }
+
+  shell = gtk_message_dialog_new(GTK_WINDOW(economy_dialog_shell),
+                                GTK_DIALOG_DESTROY_WITH_PARENT,
+                                GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE,
+                                buf);
+
   g_signal_connect(shell, "response", G_CALLBACK(gtk_widget_destroy), NULL);
   gtk_window_set_title(GTK_WINDOW(shell), _("Sell-Off: Results"));
   gtk_window_present(GTK_WINDOW(shell));
Index: client/gui-mui/repodlgs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-mui/repodlgs.c,v
retrieving revision 1.35
diff -u -r1.35 repodlgs.c
--- client/gui-mui/repodlgs.c   23 Apr 2004 22:58:05 -0000      1.35
+++ client/gui-mui/repodlgs.c   19 Jul 2004 00:22:12 -0000
@@ -484,31 +484,10 @@
 {
   struct improvement_entry *entry;
   DoMethod(trade_imprv_listview, MUIM_NList_GetEntry, 
MUIV_NList_GetEntry_Active, &entry);
-  if (entry)
-  {
-    Impr_Type_id i = entry->type;
-    int count = 0, gold = 0;
-    char str[128];
-
-    city_list_iterate(game.player_ptr->cities, pcity)
-      if (!pcity->did_sell && city_got_building(pcity, i) &&
-         (*data || improvement_obsolete(game.player_ptr, i) || 
wonder_replacement(pcity, i)))
-    {
-      count++;
-      gold += impr_sell_gold(i);
-
-      city_sell_improvement(pcity, i);
-    }
-    city_list_iterate_end
+  if (entry) {
+    char str[1024];
 
-    if (count)
-    {
-      my_snprintf(str, sizeof(str), _("Sold %d %s for %d gold"), count, 
get_improvement_name(i), gold);
-    }
-    else
-    {
-      my_snprintf(str, sizeof(str), _("No %s could be sold"), 
get_improvement_name(i));
-    }
+    sell_all_improvements(entry->type, data == NULL, str, sizeof(str));
     popup_notify_dialog(_("Sell-Off:"), _("Results"), str);
   }
 }
Index: client/gui-win32/repodlgs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-win32/repodlgs.c,v
retrieving revision 1.37
diff -u -r1.37 repodlgs.c
--- client/gui-win32/repodlgs.c 23 Apr 2004 22:58:06 -0000      1.37
+++ client/gui-win32/repodlgs.c 19 Jul 2004 00:22:13 -0000
@@ -323,40 +323,18 @@
 **************************************************************************/
 static void economy_dlg_sell(HWND hWnd,int data)
 {
-  HWND lv;
-  int n,i,count=0,gold=0;
-  char str[64];
-  int row;
-  lv=GetDlgItem(hWnd,ID_TRADEREP_LIST);
-  n=ListView_GetItemCount(lv);
-  
-  for(row=0;row<n;row++)
-    if (ListView_GetItemState(lv,row,LVIS_SELECTED)) {
-       
-      i=economy_improvement_type[row];
+  HWND lv = GetDlgItem(hWnd, ID_TRADEREP_LIST);
+  char str[1024];
+  int row, n = ListView_GetItemCount(lv);
 
-      city_list_iterate(game.player_ptr->cities, pcity) {      
-       if (!pcity->did_sell && city_got_building(pcity, i)
-           && (data || improvement_obsolete(game.player_ptr,i)
-               || wonder_replacement(pcity, i)))  {
-         count++;
-         gold += impr_sell_gold(i);
-         city_sell_improvement(pcity, i);
-       }            
-      } city_list_iterate_end;
-
-      if(count)  {
-       my_snprintf(str, sizeof(str), _("Sold %d %s for %d gold"),
-                   count, get_improvement_name(i), gold);
-      } else {
-       my_snprintf(str, sizeof(str), _("No %s could be sold"),
-                   get_improvement_name(i));
-      }      
-      
+  for (row = 0; row < n; row++) {
+    if (ListView_GetItemState(lv,row,LVIS_SELECTED)) {
+      sell_all_improvements(economy_improvement_type[row],
+                           data != 0, str, sizeof(str));
       ListView_SetItemState(lv,row,0,LVIS_SELECTED);
       popup_notify_dialog(_("Sell-Off:"),_("Results"),str);
-    }   
-  return ;
+    }
+  }
 }
 
 /**************************************************************************
Index: client/gui-xaw/repodlgs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/repodlgs.c,v
retrieving revision 1.60
diff -u -r1.60 repodlgs.c
--- client/gui-xaw/repodlgs.c   13 May 2004 18:14:22 -0000      1.60
+++ client/gui-xaw/repodlgs.c   19 Jul 2004 00:22:13 -0000
@@ -706,34 +706,16 @@
 void economy_selloff_callback(Widget w, XtPointer client_data, 
                            XtPointer call_data)
 {
-  int i,count=0,gold=0;
-  char str[64];
-  XawListReturnStruct *ret=XawListShowCurrent(economy_list);
-
-  if(ret->list_index==XAW_LIST_NONE) return;
-
-  i=economy_improvement_type[ret->list_index];
-
-  city_list_iterate(game.player_ptr->cities, pcity) {
-    if (!pcity->did_sell && city_got_building(pcity, i)
-       && (client_data
-           || improvement_obsolete(game.player_ptr,i)
-           || wonder_replacement(pcity, i)))  {
-      count++;
-      gold += impr_sell_gold(i);
-      city_sell_improvement(pcity, i);
-    }
-  } city_list_iterate_end;
+  char str[1024];
+  XawListReturnStruct *ret = XawListShowCurrent(economy_list);
 
-  if(count)  {
-    my_snprintf(str, sizeof(str), _("Sold %d %s for %d gold"),
-               count, get_improvement_name(i), gold);
-  } else {
-    my_snprintf(str, sizeof(str), _("No %s could be sold"),
-               get_improvement_name(i));
+  if (ret->list_index == XAW_LIST_NONE) {
+    return;
   }
+
+  sell_all_improvements(economy_improvement_type[ret->list_index],
+                       client_data == NULL, str, sizeof(str));
   popup_notify_dialog(_("Sell-Off:"), _("Results"), str);
-  return;
 }
 
 /****************************************************************

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#9423) two new repodlg functions, Jason Short <=