Complete.Org: Mailing Lists: Archives: freeciv-dev: November 2004:
[Freeciv-Dev] (PR#11224) change tile output into an array
Home

[Freeciv-Dev] (PR#11224) change tile output into an array

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#11224) change tile output into an array
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 27 Nov 2004 13:10:48 -0800
Reply-to: rt@xxxxxxxxxxx

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

This patch changes tile output data (currently ptype->food, 
ptype->shields, ptype->trade) as well as the same data for specials 
(ptype->special[0].food, etc.) into array form.  The two arrays are 
ptype->output[] and ptype->special[].output.  The array, naturally, is 
indexed by output type.

The advantage of this is that it allows some merging of code that is 
otherwise identical.  Just as an example get_tile_xxx_base() will all be 
identical except for a different array index and can easily be merged. 
We can then work upward toward higher-level functions to unify special 
cases and cut down on duplicated code.

jason

Index: client/packhand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/packhand.c,v
retrieving revision 1.424
diff -u -r1.424 packhand.c
--- client/packhand.c   26 Nov 2004 22:42:37 -0000      1.424
+++ client/packhand.c   27 Nov 2004 21:06:12 -0000
@@ -2505,19 +2505,22 @@
   sz_strlcpy(t->graphic_alt, p->graphic_alt);
   t->movement_cost = p->movement_cost;
   t->defense_bonus = p->defense_bonus;
-  t->food = p->food;
-  t->shield = p->shield;
-  t->trade = p->trade;
+  memset(t->output, 0, O_COUNT * sizeof(*t->output));
+  t->output[O_FOOD] = p->food;
+  t->output[O_SHIELD] = p->shield;
+  t->output[O_TRADE] = p->trade;
   sz_strlcpy(t->special[0].name_orig, p->special_1_name);
   t->special[0].name = t->special[0].name_orig;
-  t->special[0].food = p->food_special_1;
-  t->special[0].shield = p->shield_special_1;
-  t->special[0].trade = p->trade_special_1;
+  memset(t->special[0].output, 0, O_COUNT * sizeof(*t->special[0].output));
+  t->special[0].output[O_FOOD] = p->food_special_1;
+  t->special[0].output[O_SHIELD] = p->shield_special_1;
+  t->special[0].output[O_TRADE] = p->trade_special_1;
   sz_strlcpy(t->special[1].name_orig, p->special_2_name);
   t->special[1].name = t->special[1].name_orig;
-  t->special[1].food = p->food_special_2;
-  t->special[1].shield = p->shield_special_2;
-  t->special[1].trade = p->trade_special_2;
+  memset(t->special[1].output, 0, O_COUNT * sizeof(*t->special[1].output));
+  t->special[1].output[O_FOOD] = p->food_special_2;
+  t->special[1].output[O_SHIELD] = p->shield_special_2;
+  t->special[1].output[O_TRADE] = p->trade_special_2;
 
   sz_strlcpy(t->special[0].graphic_str, p->graphic_str_special_1);
   sz_strlcpy(t->special[0].graphic_alt, p->graphic_alt_special_1);
Index: client/gui-gtk/helpdlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/helpdlg.c,v
retrieving revision 1.71
diff -u -r1.71 helpdlg.c
--- client/gui-gtk/helpdlg.c    25 Nov 2004 05:34:34 -0000      1.71
+++ client/gui-gtk/helpdlg.c    27 Nov 2004 21:06:12 -0000
@@ -927,9 +927,9 @@
     gtk_set_label(help_tlabel[0][1], buf);
 
     sprintf(buf, "%d/%d/%d",
-           tile_types[i].food,
-           tile_types[i].shield,
-           tile_types[i].trade);
+           tile_types[i].output[O_FOOD],
+           tile_types[i].output[O_SHIELD],
+           tile_types[i].output[O_TRADE]);
     gtk_set_label(help_tlabel[0][4], buf);
 
     if (*(tile_types[i].special[0].name)) {
@@ -937,9 +937,9 @@
               tile_types[i].special[0].name);
       gtk_set_label(help_tlabel[1][0], buf);
       sprintf(buf, "%d/%d/%d",
-             tile_types[i].special[0].food,
-             tile_types[i].special[0].shield,
-             tile_types[i].special[0].trade);
+             tile_types[i].special[0].output[O_FOOD],
+             tile_types[i].special[0].output[O_SHIELD],
+             tile_types[i].special[0].output[O_TRADE]);
       gtk_set_label(help_tlabel[1][1], buf);
     } else {
       gtk_set_label(help_tlabel[1][0], "");
@@ -951,9 +951,9 @@
              tile_types[i].special[1].name);
       gtk_set_label(help_tlabel[1][3], buf);
       sprintf(buf, "%d/%d/%d",
-             tile_types[i].special[1].food,
-             tile_types[i].special[1].shield,
-             tile_types[i].special[1].trade);
+             tile_types[i].special[1].output[O_FOOD],
+             tile_types[i].special[1].output[O_SHIELD],
+             tile_types[i].special[1].output[O_TRADE]);
       gtk_set_label(help_tlabel[1][4], buf);
     } else {
       gtk_set_label(help_tlabel[1][3], "");
Index: client/gui-gtk-2.0/helpdlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/helpdlg.c,v
retrieving revision 1.41
diff -u -r1.41 helpdlg.c
--- client/gui-gtk-2.0/helpdlg.c        25 Nov 2004 05:34:34 -0000      1.41
+++ client/gui-gtk-2.0/helpdlg.c        27 Nov 2004 21:06:12 -0000
@@ -988,9 +988,9 @@
     gtk_label_set_text(GTK_LABEL(help_tlabel[0][1]), buf);
 
     sprintf(buf, "%d/%d/%d",
-           tile_types[i].food,
-           tile_types[i].shield,
-           tile_types[i].trade);
+           tile_types[i].output[O_FOOD],
+           tile_types[i].output[O_SHIELD],
+           tile_types[i].output[O_TRADE]);
     gtk_label_set_text(GTK_LABEL(help_tlabel[0][4]), buf);
 
     if (*(tile_types[i].special[0].name)) {
@@ -998,9 +998,9 @@
               tile_types[i].special[0].name);
       gtk_label_set_text(GTK_LABEL(help_tlabel[1][0]), buf);
       sprintf(buf, "%d/%d/%d",
-             tile_types[i].special[0].food,
-             tile_types[i].special[0].shield,
-             tile_types[i].special[0].trade);
+             tile_types[i].special[0].output[O_FOOD],
+             tile_types[i].special[0].output[O_SHIELD],
+             tile_types[i].special[0].output[O_TRADE]);
       gtk_label_set_text(GTK_LABEL(help_tlabel[1][1]), buf);
     } else {
       gtk_label_set_text(GTK_LABEL(help_tlabel[1][0]), "");
@@ -1012,9 +1012,9 @@
              tile_types[i].special[1].name);
       gtk_label_set_text(GTK_LABEL(help_tlabel[1][3]), buf);
       sprintf(buf, "%d/%d/%d",
-             tile_types[i].special[1].food,
-             tile_types[i].special[1].shield,
-             tile_types[i].special[1].trade);
+             tile_types[i].special[1].output[O_FOOD],
+             tile_types[i].special[1].output[O_SHIELD],
+             tile_types[i].special[1].output[O_TRADE]);
       gtk_label_set_text(GTK_LABEL(help_tlabel[1][4]), buf);
     } else {
       gtk_label_set_text(GTK_LABEL(help_tlabel[1][3]), "");
Index: client/gui-mui/helpdlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-mui/helpdlg.c,v
retrieving revision 1.36
diff -u -r1.36 helpdlg.c
--- client/gui-mui/helpdlg.c    25 Nov 2004 05:34:34 -0000      1.36
+++ client/gui-mui/helpdlg.c    27 Nov 2004 21:06:12 -0000
@@ -868,7 +868,9 @@
 
     help_terrain_dynamic_group = VGroup,End;
 
-    my_snprintf(buf,sizeof(buf),_("Food:   %d\nShield: %d\nTrade:  
%d"),tile->food, tile->shield, tile->trade);
+    my_snprintf(buf,sizeof(buf),_("Food:   %d\nShield: %d\nTrade:  %d"),
+               tile->output[O_FOOD], tile->output[O_SHIELD],
+               tile->output[O_TRADE]);
     if((o = HGroup,
               Child, HSpace(0),
              Child, TextObject, MUIA_Text_Contents, "", End,
@@ -885,8 +887,10 @@
 
     g = HGroup, Child, HSpace(0), End;
 
-    my_snprintf(buf,sizeof(buf),_("Food:   %d\nShield: %d\nTrade:  
%d"),tile->food_special_1, tile->shield_special_1,
-    tile->trade_special_1);
+    my_snprintf(buf,sizeof(buf),_("Food:   %d\nShield: %d\nTrade:  %d"),
+               tile->special[0].output[O_FOOD],
+               tile->special[0].output[O_SHIELD],
+               tile->special[0].output[O_TRADE]);
     if((o = HGroup,
               Child, HSpace(0),
              Child, TextObject, MUIA_Text_Contents, tile->special_1_name, End,
@@ -903,7 +907,10 @@
       DoMethod(g, OM_ADDMEMBER, o);
     }
 
-    my_snprintf(buf,sizeof(buf),_("Food:   %d\nShield: %d\nTrade:  
%d"),tile->food_special_2, tile->shield_special_2,
+    my_snprintf(buf,sizeof(buf),_("Food:   %d\nShield: %d\nTrade:  %d"),
+               tile->special[1].output[O_FOOD],
+               tile->special[1].output[O_SHIELD],
+               tile->special[1].output[O_TRADE]);
     tile->trade_special_2);
     if((o = HGroup,
               Child, HSpace(0),
Index: client/gui-win32/helpdlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-win32/helpdlg.c,v
retrieving revision 1.23
diff -u -r1.23 helpdlg.c
--- client/gui-win32/helpdlg.c  25 Nov 2004 05:34:34 -0000      1.23
+++ client/gui-win32/helpdlg.c  27 Nov 2004 21:06:12 -0000
@@ -554,9 +554,9 @@
     SetWindowText (help_tlabel[0][1], buf);
 
     sprintf(buf, "%d/%d/%d",
-           tile_types[i].food,
-           tile_types[i].shield,
-           tile_types[i].trade);
+           tile_types[i].output[O_FOOD],
+           tile_types[i].output[O_SHIELD],
+           tile_types[i].output[O_TRADE]);
     SetWindowText(help_tlabel[0][4], buf);
 
     if (*(tile_types[i].special[0].name)) {
@@ -564,9 +564,9 @@
              tile_types[i].special[0].name);
       SetWindowText(help_tlabel[1][0], buf);
       sprintf(buf, "%d/%d/%d",
-             tile_types[i].special[0].food,
-             tile_types[i].special[0].shield,
-             tile_types[i].special[0].trade);
+             tile_types[i].special[0].output[O_FOOD],
+             tile_types[i].special[0].output[O_SHIELD],
+             tile_types[i].special[0].output[O_TRADE]);
       SetWindowText(help_tlabel[1][1], buf);
     } else {
       SetWindowText(help_tlabel[1][0], " ");
@@ -578,9 +578,9 @@
              tile_types[i].special[1].name);
       SetWindowText(help_tlabel[1][3], buf);
       sprintf(buf, "%d/%d/%d",
-             tile_types[i].special[1].food,
-             tile_types[i].special[1].shield,
-             tile_types[i].special[1].trade);
+             tile_types[i].special[1].output[O_FOOD],
+             tile_types[i].special[1].output[O_SHIELD],
+             tile_types[i].special[1].output[O_TRADE]);
       SetWindowText(help_tlabel[1][4], buf);
     } else {
       SetWindowText(help_tlabel[1][3], " ");
Index: client/gui-xaw/helpdlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/helpdlg.c,v
retrieving revision 1.55
diff -u -r1.55 helpdlg.c
--- client/gui-xaw/helpdlg.c    25 Nov 2004 05:34:34 -0000      1.55
+++ client/gui-xaw/helpdlg.c    27 Nov 2004 21:06:12 -0000
@@ -952,9 +952,9 @@
       xaw_set_label (help_terrain_movement_defense_data, buf);
 
       sprintf (buf, "%d/%d/%d",
-              tile_types[i].food,
-              tile_types[i].shield,
-              tile_types[i].trade);
+              tile_types[i].output[O_FOOD],
+              tile_types[i].output[O_SHIELD],
+              tile_types[i].output[O_TRADE]);
       xaw_set_label (help_terrain_food_shield_trade_data, buf);
 
       if (*(tile_types[i].special[0].name))
@@ -963,9 +963,9 @@
                   tile_types[i].special[0].name);
          xaw_set_label (help_terrain_special_1, buf);
          sprintf (buf, "%d/%d/%d",
-                  tile_types[i].special[0].food,
-                  tile_types[i].special[0].shield,
-                  tile_types[i].special[0].trade);
+                  tile_types[i].special[0].output[O_FOOD],
+                  tile_types[i].special[0].output[O_SHIELD],
+                  tile_types[i].special[0].output[O_TRADE]);
          xaw_set_label (help_terrain_special_1_data, buf);
        } else {
          xaw_set_label (help_terrain_special_1, "");
@@ -978,9 +978,9 @@
                   tile_types[i].special[1].name);
          xaw_set_label (help_terrain_special_2, buf);
          sprintf (buf, "%d/%d/%d",
-                  tile_types[i].special[1].food,
-                  tile_types[i].special[1].shield,
-                  tile_types[i].special[1].trade);
+                  tile_types[i].special[1].output[O_FOOD],
+                  tile_types[i].special[1].output[O_SHIELD],
+                  tile_types[i].special[1].output[O_TRADE]);
          xaw_set_label (help_terrain_special_2_data, buf);
        } else {
          xaw_set_label (help_terrain_special_2, "");
Index: common/map.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/map.c,v
retrieving revision 1.206
diff -u -r1.206 map.c
--- common/map.c        25 Nov 2004 05:31:59 -0000      1.206
+++ common/map.c        27 Nov 2004 21:06:14 -0000
@@ -729,11 +729,11 @@
 int get_tile_food_base(const struct tile *ptile)
 {
   if (tile_has_special(ptile, S_SPECIAL_1)) 
-    return tile_types[ptile->terrain].special[0].food;
+    return tile_types[ptile->terrain].special[0].output[O_FOOD];
   else if (tile_has_special(ptile, S_SPECIAL_2))
-    return tile_types[ptile->terrain].special[1].food;
+    return tile_types[ptile->terrain].special[1].output[O_FOOD];
   else
-    return tile_types[ptile->terrain].food;
+    return tile_types[ptile->terrain].output[O_FOOD];
 }
 
 /***************************************************************
@@ -742,11 +742,11 @@
 int get_tile_shield_base(const struct tile *ptile)
 {
   if (tile_has_special(ptile, S_SPECIAL_1))
-    return tile_types[ptile->terrain].special[0].shield;
+    return tile_types[ptile->terrain].special[0].output[O_SHIELD];
   else if(tile_has_special(ptile, S_SPECIAL_2))
-    return tile_types[ptile->terrain].special[1].shield;
+    return tile_types[ptile->terrain].special[1].output[O_SHIELD];
   else
-    return tile_types[ptile->terrain].shield;
+    return tile_types[ptile->terrain].output[O_SHIELD];
 }
 
 /***************************************************************
@@ -755,11 +755,11 @@
 int get_tile_trade_base(const struct tile *ptile)
 {
   if (tile_has_special(ptile, S_SPECIAL_1))
-    return tile_types[ptile->terrain].special[0].trade;
+    return tile_types[ptile->terrain].special[0].output[O_TRADE];
   else if (tile_has_special(ptile, S_SPECIAL_2))
-    return tile_types[ptile->terrain].special[1].trade;
+    return tile_types[ptile->terrain].special[1].output[O_TRADE];
   else
-    return tile_types[ptile->terrain].trade;
+    return tile_types[ptile->terrain].output[O_TRADE];
 }
 
 /***************************************************************
Index: common/map.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/map.h,v
retrieving revision 1.227
diff -u -r1.227 map.h
--- common/map.h        22 Nov 2004 19:31:30 -0000      1.227
+++ common/map.h        27 Nov 2004 21:06:14 -0000
@@ -78,15 +78,13 @@
   int movement_cost;
   int defense_bonus;
 
-  int food;
-  int shield;
-  int trade;
+  int output[O_MAX];
 
 #define MAX_NUM_SPECIALS 2
   struct {
     const char *name; /* Translated string - doesn't need freeing. */
     char name_orig[MAX_LEN_NAME];
-    int food, shield, trade;
+    int output[O_MAX];
     char graphic_str[MAX_LEN_NAME];
     char graphic_alt[MAX_LEN_NAME];
   } special[MAX_NUM_SPECIALS];
Index: manual/civmanual.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/manual/civmanual.c,v
retrieving revision 1.3
diff -u -r1.3 civmanual.c
--- manual/civmanual.c  13 Nov 2004 08:34:17 -0000      1.3
+++ manual/civmanual.c  27 Nov 2004 21:06:14 -0000
@@ -237,15 +237,17 @@
                ptype->graphic_str,
                IMAGE_END, get_terrain_name(id));
        fprintf(doc, "<td>%d / %d / %d</td>",
-               ptype->food, ptype->shield, ptype->trade);
+               ptype->output[O_FOOD], ptype->output[O_SHIELD],
+               ptype->output[O_TRADE]);
 
        for (s = 0; s < MAX_NUM_SPECIALS; s++) {
          fprintf(doc, "<td>%s%s%s %s</td>", IMAGE_BEGIN,
                  ptype->special[s].graphic_str, IMAGE_END,
                  ptype->special[s].name);
          fprintf(doc, "<td>%d / %d / %d</td>",
-                 ptype->special[s].food, ptype->special[s].shield,
-                 ptype->special[s].trade);
+                 ptype->special[s].output[O_FOOD],
+                 ptype->special[s].output[O_SHIELD],
+                 ptype->special[s].output[O_TRADE]);
        }
 
        fprintf(doc, "<td>%d</td>\n", ptype->movement_cost);
Index: server/ruleset.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/ruleset.c,v
retrieving revision 1.201
diff -u -r1.201 ruleset.c
--- server/ruleset.c    26 Nov 2004 22:42:38 -0000      1.201
+++ server/ruleset.c    27 Nov 2004 21:06:15 -0000
@@ -1617,9 +1617,10 @@
       t->movement_cost = secfile_lookup_int(file, "%s.movement_cost", sec[i]);
       t->defense_bonus = secfile_lookup_int(file, "%s.defense_bonus", sec[i]);
 
-      t->food = secfile_lookup_int(file, "%s.food", sec[i]);
-      t->shield = secfile_lookup_int(file, "%s.shield", sec[i]);
-      t->trade = secfile_lookup_int(file, "%s.trade", sec[i]);
+      output_type_iterate(o) {
+       t->output[o] = secfile_lookup_int(file, "%s.%s", sec[i],
+                                         get_output_identifier(o));
+      } output_type_iterate_end;
 
       for (j = 0; j < MAX_NUM_SPECIALS; j++) {
        char *name = secfile_lookup_str(file, "%s.special_%d_name",
@@ -1630,13 +1631,11 @@
          t->special[j].name_orig[0] = '\0';
        }
        t->special[j].name = t->special[j].name_orig;
-       t->special[j].food = secfile_lookup_int(file, "%s.food_special_%d",
-                                               sec[i], j + 1);
-       t->special[j].shield = secfile_lookup_int(file,
-                                                 "%s.shield_special_%d",
-                                                 sec[i], j + 1);
-       t->special[j].trade = secfile_lookup_int(file, "%s.trade_special_%d",
-                                                sec[i], j + 1);
+       output_type_iterate(o) {
+         t->special[j].output[o]
+           = secfile_lookup_int(file, "%s.%s_special_%d", sec[i],
+                                get_output_identifier(o), j + 1);
+       } output_type_iterate_end;
 
        sz_strlcpy(t->special[j].graphic_str,
                   secfile_lookup_str(file,"%s.graphic_special_%d",
@@ -2941,19 +2940,19 @@
       packet.movement_cost = t->movement_cost;
       packet.defense_bonus = t->defense_bonus;
 
-      packet.food = t->food;
-      packet.shield = t->shield;
-      packet.trade = t->trade;
+      packet.food = t->output[O_FOOD];
+      packet.shield = t->output[O_SHIELD];
+      packet.trade = t->output[O_TRADE];
 
       sz_strlcpy(packet.special_1_name, t->special[0].name_orig);
-      packet.food_special_1 = t->special[0].food;
-      packet.shield_special_1 = t->special[0].shield;
-      packet.trade_special_1 = t->special[0].trade;
+      packet.food_special_1 = t->special[0].output[O_FOOD];
+      packet.shield_special_1 = t->special[0].output[O_SHIELD];
+      packet.trade_special_1 = t->special[0].output[O_TRADE];
 
       sz_strlcpy(packet.special_2_name, t->special[1].name_orig);
-      packet.food_special_2 = t->special[1].food;
-      packet.shield_special_2 = t->special[1].shield;
-      packet.trade_special_2 = t->special[1].trade;
+      packet.food_special_2 = t->special[1].output[O_FOOD];
+      packet.shield_special_2 = t->special[1].output[O_SHIELD];
+      packet.trade_special_2 = t->special[1].output[O_TRADE];
 
       sz_strlcpy(packet.graphic_str_special_1, t->special[0].graphic_str);
       sz_strlcpy(packet.graphic_alt_special_1, t->special[0].graphic_alt);

[Prev in Thread] Current Thread [Next in Thread]