Complete.Org: Mailing Lists: Archives: freeciv-dev: March 2002:
[Freeciv-Dev] Re: unit type iterate (PR#1297)
Home

[Freeciv-Dev] Re: unit type iterate (PR#1297)

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv-dev@xxxxxxxxxxx
Cc: bugs@xxxxxxxxxxxxxxxxxxx
Subject: [Freeciv-Dev] Re: unit type iterate (PR#1297)
From: jdorje@xxxxxxxxxxxxxxxxxxxxx
Date: Mon, 4 Mar 2002 05:22:33 -0800 (PST)

Raimar Falke wrote:
On Mon, Mar 04, 2002 at 05:28:47AM -0500, Jason Short wrote:

Gregory Berkolaiko wrote:

Hey you iterate wizards.  How about making unit_type_iterate.
Here's grep to help you:

Are we talking about something like

#define unit_type_iterate(id) {                  \
  int id;                                        \
  for (id = 0; id < game.num_unit_types; id++) {

or something a bit more forward-thinking (and slower) like

#define unit_type_iterate(id, type) {            \
  int id;                                        \
  for (id = 0; id < game.num_unit_types; id++) { \
    struct unit_type *type = get_unit_type(id);

or even something completely forward-thinking (and currently useless) like

#define unit_type_iterate(type) {                   \
  int _id;                                          \
  for (_id = 0; _id < game.num_unit_types; _id++) { \
    struct unit_type *type = get_unit_type(_id);


The first for the start. For the others we have to first change the
signatures of the functions in common/unittype.

How about this?

(Not compiled for MUI or win32.)

jason
Index: ai/advmilitary.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/advmilitary.c,v
retrieving revision 1.95
diff -u -r1.95 advmilitary.c
--- ai/advmilitary.c    2002/03/02 19:57:20     1.95
+++ ai/advmilitary.c    2002/03/04 13:19:59
@@ -465,7 +465,6 @@
 static void process_defender_want(struct player *pplayer, struct city *pcity,
                                  int danger, struct ai_choice *choice)
 {
-  Unit_Type_id i;
   Unit_Type_id bestid = 0; /* ??? Zero is legal value! (Settlers by default) */
   Tech_Type_id tech_req;
   int j, k, l, m, n;
@@ -476,7 +475,7 @@
   bool isdef = has_a_normal_defender(pcity);
 
   memset(desire, 0, sizeof(desire));
-  for (i = 0; i < game.num_unit_types; i++) {
+  unit_type_iterate(i) {
     if (!is_ai_simple_military(i)) continue;
     m = unit_types[i].move_type;
     if ((m == LAND_MOVING || m == SEA_MOVING)) {
@@ -506,7 +505,7 @@
         desire[i] = j * danger / (unit_types[i].build_cost + l);
       }
     }
-  }
+  } unit_type_iterate_end;
   if (!walls && unit_types[bestid].move_type == LAND_MOVING) {
     best *= pcity->ai.wallvalue;
     best /= 10;
@@ -516,7 +515,7 @@
   if (best == 0) best = 1;   /* avoid divide-by-zero below */
 
 /* multiply by unit_types[bestid].build_cost / best */
-  for (i = 0; i < game.num_unit_types; i++) {
+  unit_type_iterate(i) {
     if (desire[i] && is_ai_simple_military(i)) {
       tech_req = unit_types[i].tech_requirement;
       n = desire[i] * unit_types[bestid].build_cost / best;
@@ -524,7 +523,7 @@
       freelog(LOG_DEBUG, "%s wants %s for defense with desire %d <%d>",
                    pcity->name, advances[tech_req].name, n, desire[i]);
     }
-  }
+  } unit_type_iterate_end;
   choice->choice = bestid;
   choice->want = danger;
   choice->type = CT_DEFENDER;
@@ -540,8 +539,7 @@
                            struct city *pcity, int b, Unit_Type_id n,
                             bool vet, int x, int y, int unhap, int *e0, int *v,
                             int bx, int by, int boatspeed, int needferry)
-{ 
-  Unit_Type_id i;
+{
   Tech_Type_id j;
   int a, c, d, e, b0, f, g, fprime;
   int k, l, m, q;
@@ -549,7 +547,7 @@
   struct city *acity = map_get_city(x, y);
   int movetype = unit_types[*v].move_type;
 
-  for (i = 0; i < game.num_unit_types; i++) {
+  unit_type_iterate(i) {
     if (!is_ai_simple_military(i)) continue;
     m = unit_types[i].move_type;
     j = unit_types[i].tech_requirement;
@@ -655,7 +653,7 @@
         }
       }
     }
-  }
+  } unit_type_iterate_end;
 }
 
 static void kill_something_with(struct player *pplayer, struct city *pcity, 
Index: ai/aicity.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aicity.c,v
retrieving revision 1.103
diff -u -r1.103 aicity.c
--- ai/aicity.c 2002/03/02 19:57:21     1.103
+++ ai/aicity.c 2002/03/04 13:19:59
@@ -606,12 +606,11 @@
 static Unit_Type_id ai_choose_attacker(struct city *pcity,
                                        enum unit_move_type which)
 {
-  Unit_Type_id i;
   Unit_Type_id bestid = 0; /* ??? Zero is legal value! (Settlers by default) */
   int best = 0;
   int cur;
 
-  for (i = 0; i < game.num_unit_types; i++) {
+  unit_type_iterate(i) {
     /* not dealing with planes yet */
     if (!is_ai_simple_military(i)) continue;
     cur = unit_attack_desirability(i);
@@ -622,7 +621,7 @@
         bestid = i;
       }
     }
-  }
+  } unit_type_iterate_end;
   return bestid;
 }
 
@@ -638,12 +637,11 @@
 
 Unit_Type_id ai_choose_defender_versus(struct city *pcity, Unit_Type_id v)
 {
-  Unit_Type_id i;
   Unit_Type_id bestid = 0; /* ??? Zero is legal value! (Settlers by default) */
   int j, m;
   int best= 0;
 
-  for (i = 0; i < game.num_unit_types; i++) {
+  unit_type_iterate(i) {
     if (!is_ai_simple_military(i)) continue;
     m = unit_types[i].move_type;
     if (can_build_unit(pcity, i) && (m == LAND_MOVING || m == SEA_MOVING)) {
@@ -654,7 +652,7 @@
         bestid = i;
       }
     }
-  }
+  } unit_type_iterate_end;
   return bestid;
 }
 
@@ -692,14 +690,13 @@
 Unit_Type_id ai_choose_defender_limited(struct city *pcity, int n,
                                         enum unit_move_type which)
 {
-  Unit_Type_id i;
   Unit_Type_id bestid = 0; /* ??? Zero is legal value! (Settlers by default) */
   int j, m;
   int best= 0;
   const bool walls = TRUE; /* just assume city_got_citywalls(pcity); in the 
long run -- Syela */
   bool isdef = has_a_normal_defender(pcity);
 
-  for (i = 0; i < game.num_unit_types; i++) {
+  unit_type_iterate(i) {
     if (!is_ai_simple_military(i)) continue;
     m = unit_types[i].move_type;
     if (can_build_unit(pcity, i) && get_unit_type(i)->build_cost <= n &&
@@ -715,7 +712,7 @@
         bestid = i;
       }
     }
-  }
+  } unit_type_iterate_end;
   return bestid;
 }
 
Index: client/climisc.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/climisc.c,v
retrieving revision 1.85
diff -u -r1.85 climisc.c
--- client/climisc.c    2002/02/26 19:57:06     1.85
+++ client/climisc.c    2002/03/04 13:20:00
@@ -868,9 +868,9 @@
  */
 int collect_cids4(cid * dest_cids, struct city *pcity, bool advanced_tech)
 {
-  int id, cids_used = 0;
+  int cids_used = 0;
 
-  for (id = 0; id < game.num_impr_types; id++) {
+  unit_type_iterate(id) {
     bool can_build = can_player_build_improvement(game.player_ptr, id);
     bool can_eventually_build =
        could_player_eventually_build_improvement(game.player_ptr, id);
@@ -887,9 +887,9 @@
       dest_cids[cids_used] = cid_encode(FALSE, id);
       cids_used++;
     }
-  }
+  } unit_type_iterate_end;
 
-  for (id = 0; id < game.num_unit_types; id++) {
+  unit_type_iterate(id) {
     bool can_build = can_player_build_unit(game.player_ptr, id);
     bool can_eventually_build =
        can_player_eventually_build_unit(game.player_ptr, id);
@@ -906,7 +906,7 @@
       dest_cids[cids_used] = cid_encode(TRUE, id);
       cids_used++;
     }
-  }
+  } unit_type_iterate_end;
   return cids_used;
 }
 
Index: client/helpdata.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/helpdata.c,v
retrieving revision 1.49
diff -u -r1.49 helpdata.c
--- client/helpdata.c   2002/02/26 19:57:08     1.49
+++ client/helpdata.c   2002/03/04 13:20:00
@@ -244,7 +244,7 @@
        
        genlist_init(&category_nodes);
        if(current_type==HELP_UNIT) {
-         for(i=0; i<game.num_unit_types; i++) {
+         unit_type_iterate(i) {
            if(unit_type_exists(i)) {
              pitem = new_help_item(current_type);
              my_snprintf(name, sizeof(name), " %s", unit_name(i));
@@ -252,7 +252,7 @@
              pitem->text = mystrdup("");
              genlist_insert(&category_nodes, pitem, -1);
            }
-         }
+         } unit_type_iterate_end;
        } else if(current_type==HELP_TECH) {
          for(i=A_FIRST; i<game.num_tech_types; i++) {  /* skip A_NONE */
            if(tech_exists(i)) {
Index: client/gui-gtk/helpdlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/helpdlg.c,v
retrieving revision 1.53
diff -u -r1.53 helpdlg.c
--- client/gui-gtk/helpdlg.c    2002/02/16 17:28:15     1.53
+++ client/gui-gtk/helpdlg.c    2002/03/04 13:20:01
@@ -842,7 +842,7 @@
         gtk_box_pack_start(GTK_BOX(hbox), w, FALSE, FALSE, 0);
       }
     }
-    for(j=0; j<game.num_unit_types; ++j) {
+    unit_type_iterate(j) {
       if(i!=get_unit_type(j)->tech_requirement) continue;
       hbox = gtk_hbox_new(FALSE, 0);
       gtk_container_add(GTK_CONTAINER(help_vbox), hbox);
@@ -852,7 +852,7 @@
       gtk_box_pack_start(GTK_BOX(hbox), w, FALSE, FALSE, 0);
       w = gtk_label_new(".");
       gtk_box_pack_start(GTK_BOX(hbox), w, FALSE, FALSE, 0);
-    }
+    } unit_type_iterate_end;
 
     for(j=0; j<game.num_tech_types; ++j) {
       if(i==advances[j].req[0]) {
Index: client/gui-gtk/repodlgs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/repodlgs.c,v
retrieving revision 1.52
diff -u -r1.52 repodlgs.c
--- client/gui-gtk/repodlgs.c   2002/02/25 15:24:54     1.52
+++ client/gui-gtk/repodlgs.c   2002/03/04 13:20:01
@@ -1018,26 +1018,26 @@
 
     k = 0;
     memset(&unittotals, '\0', sizeof(unittotals));
-    for (i=0;i<game.num_unit_types;i++) {
-      if ((unitarray[i].active_count > 0) || (unitarray[i].building_count > 
0)) {
-       can = (can_upgrade_unittype(game.player_ptr, i) != -1);
-        my_snprintf(buf[0], sizeof(buf[0]), "%-27s", unit_name(i));
+    unit_type_iterate(id) {
+      if ((unitarray[id].active_count > 0) || (unitarray[id].building_count > 
0)) {
+       can = (can_upgrade_unittype(game.player_ptr, id) != -1);
+        my_snprintf(buf[0], sizeof(buf[0]), "%-27s", unit_name(id));
        my_snprintf(buf[1], sizeof(buf[1]), "%c", can ? '*': '-');
-        my_snprintf(buf[2], sizeof(buf[2]), "%9d", 
unitarray[i].building_count);
-        my_snprintf(buf[3], sizeof(buf[3]), "%9d", unitarray[i].active_count);
-        my_snprintf(buf[4], sizeof(buf[4]), "%9d", unitarray[i].upkeep_shield);
-        my_snprintf(buf[5], sizeof(buf[5]), "%9d", unitarray[i].upkeep_food);
+        my_snprintf(buf[2], sizeof(buf[2]), "%9d", 
unitarray[id].building_count);
+        my_snprintf(buf[3], sizeof(buf[3]), "%9d", unitarray[id].active_count);
+        my_snprintf(buf[4], sizeof(buf[4]), "%9d", 
unitarray[id].upkeep_shield);
+        my_snprintf(buf[5], sizeof(buf[5]), "%9d", unitarray[id].upkeep_food);
 
        gtk_clist_append( GTK_CLIST( activeunits_list ), row );
 
-       activeunits_type[k]=(unitarray[i].active_count > 0) ? i : U_LAST;
+       activeunits_type[k]=(unitarray[id].active_count > 0) ? id : U_LAST;
        k++;
-       unittotals.active_count += unitarray[i].active_count;
-       unittotals.upkeep_shield += unitarray[i].upkeep_shield;
-       unittotals.upkeep_food += unitarray[i].upkeep_food;
-       unittotals.building_count += unitarray[i].building_count;
+       unittotals.active_count += unitarray[id].active_count;
+       unittotals.upkeep_shield += unitarray[id].upkeep_shield;
+       unittotals.upkeep_food += unitarray[id].upkeep_food;
+       unittotals.building_count += unitarray[id].building_count;
       }
-    }
+    } unit_type_iterate_end;
 
     /* horrible kluge, but I can't get gtk_label_set_justify() to work --jjm */
     my_snprintf(activeunits_total, sizeof(activeunits_total),
Index: client/gui-mui/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-mui/citydlg.c,v
retrieving revision 1.48
diff -u -r1.48 citydlg.c
--- client/gui-mui/citydlg.c    2002/03/03 10:33:11     1.48
+++ client/gui-mui/citydlg.c    2002/03/04 13:20:03
@@ -1351,18 +1351,17 @@
     }
     DoMethod(pcprod->available_listview, MUIM_NList_InsertSingle, 20000, 
MUIV_NList_Insert_Bottom);
 
-    for (i = 0; i < game.num_unit_types; i++)
-    {
-      if (can_build_unit(pcity, i))
+    unit_type_iterate(id) {
+      if (can_build_unit(pcity, id))
       {
-        DoMethod(pcprod->available_listview, MUIM_NList_InsertSingle, i + 
10000, MUIV_NList_Insert_Bottom);
+        DoMethod(pcprod->available_listview, MUIM_NList_InsertSingle, id + 
10000, MUIV_NList_Insert_Bottom);
 
-        if(i == pcity->currently_building && pcity->is_building_unit)
+        if(id == pcity->currently_building && pcity->is_building_unit)
          current = ++pos;
 
         pos++;
       }
-    }
+    } unit_type_iterate_end;
 
     set(pcprod->available_listview, MUIA_NList_Quiet, FALSE);
 
Index: client/gui-mui/cityrep.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-mui/cityrep.c,v
retrieving revision 1.17
diff -u -r1.17 cityrep.c
--- client/gui-mui/cityrep.c    2002/02/25 15:24:55     1.17
+++ client/gui-mui/cityrep.c    2002/03/04 13:20:03
@@ -133,13 +133,12 @@
       }
     }
 
-    for (i = 0; i < game.num_unit_types; i++)
-    {
-      if (can_build_unit(pcity, i))
+    unit_type_iterate(id) {
+      if (can_build_unit(pcity, id))
       {
        flag = 1;
       }
-    }
+    } unit_type_iterate_end;
 
 
     if (!flag)
Index: client/gui-mui/helpdlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-mui/helpdlg.c,v
retrieving revision 1.26
diff -u -r1.26 helpdlg.c
--- client/gui-mui/helpdlg.c    2002/02/12 09:36:47     1.26
+++ client/gui-mui/helpdlg.c    2002/03/04 13:20:03
@@ -753,8 +753,7 @@
            DoMethod(help_tech_group, OM_ADDMEMBER, o);
        }
 
-       for (j = 0; j < game.num_unit_types; ++j)
-       {
+       unit_type_iterate(j) {
          Object *o, *button;
          if (i != get_unit_type(j)->tech_requirement)
            continue;
@@ -769,7 +768,7 @@
 
          if (o)
            DoMethod(help_tech_group, OM_ADDMEMBER, o);
-       }
+       } unit_type_iterate_end;
 
 
        for (j = 0; j < game.num_tech_types; ++j)
Index: client/gui-mui/repodlgs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-mui/repodlgs.c,v
retrieving revision 1.19
diff -u -r1.19 repodlgs.c
--- client/gui-mui/repodlgs.c   2002/02/25 15:24:56     1.19
+++ client/gui-mui/repodlgs.c   2002/03/04 13:20:04
@@ -943,8 +943,7 @@
   set(actunit_units_listview, MUIA_NList_Quiet, TRUE);
   DoMethod(actunit_units_listview, MUIM_NList_Clear);
 
-  for (i = 0; i < game.num_unit_types; i++)
-  {
+  unit_type_iterate(i) {
     if (unitarray[i].active_count)
     {
       entry.type = i;
@@ -955,7 +954,7 @@
       entry.building_count = unitarray[i].building_count;
       DoMethod(actunit_units_listview, MUIM_NList_InsertSingle, &entry, 
MUIV_NList_Insert_Bottom);
     }
-  }
+  } unit_type_iterate_end;
 
   set(actunit_units_listview, MUIA_NList_Quiet, FALSE);
 }
Index: client/gui-mui/worklistclass.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-mui/worklistclass.c,v
retrieving revision 1.18
diff -u -r1.18 worklistclass.c
--- client/gui-mui/worklistclass.c      2002/02/12 08:59:57     1.18
+++ client/gui-mui/worklistclass.c      2002/03/04 13:20:04
@@ -440,8 +440,7 @@
   entry.type = 3;
   entry.id = 0;
   DoMethod(data->available_listview, MUIM_NList_InsertSingle, &entry, 
MUIV_NList_Insert_Bottom);
-  for(i=0; i<game.num_unit_types; i++)
-  {
+  unit_type_iterate(i) {
     /* Can the player (eventually) build this improvement? */
     can_build = can_player_build_unit(pplr,i);
     can_eventually_build = can_player_eventually_build_unit(pplr,i);
@@ -460,7 +459,7 @@
       entry.id = i;
       DoMethod(data->available_listview, MUIM_NList_InsertSingle, &entry, 
MUIV_NList_Insert_Bottom);
     }
-  }
+  } unit_type_iterate_end;
 
   /*     + Finally, the global worklists. */
   if (data->pcity)
Index: client/gui-win32/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-win32/citydlg.c,v
retrieving revision 1.18
diff -u -r1.18 citydlg.c
--- client/gui-win32/citydlg.c  2002/03/04 09:01:16     1.18
+++ client/gui-win32/citydlg.c  2002/03/04 13:20:05
@@ -1146,13 +1146,14 @@
        
       pdialog->change_list_num_improvements=n;
       
-      for(i=0; i<game.num_unit_types; i++)
-       if(can_build_unit(pdialog->pcity, i)) {
-         get_city_dialog_production_row(row, sizeof(buf[0]), i,
+      unit_type_iterate(id) {
+       if(can_build_unit(pdialog->pcity, id)) {
+         get_city_dialog_production_row(row, sizeof(buf[0]), id,
                                         TRUE, pdialog->pcity);
          fcwin_listview_add_row(lv,n,4,row);
-         pdialog->change_list_ids[n++]=i;
+         pdialog->change_list_ids[n++] = id;
        }
+      } unit_type_iterate_end;
       
       ListView_SetColumnWidth(lv,0,LVSCW_AUTOSIZE);
       for(i=1;i<4;i++) {
Index: client/gui-win32/helpdlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-win32/helpdlg.c,v
retrieving revision 1.6
diff -u -r1.6 helpdlg.c
--- client/gui-win32/helpdlg.c  2002/02/23 19:56:15     1.6
+++ client/gui-win32/helpdlg.c  2002/03/04 13:20:05
@@ -756,7 +756,7 @@
                             0,FALSE,FALSE,5);
       }
     }
-    for(j=0; j<game.num_unit_types; ++j) {
+    unit_type_iterate(j) {
       if(i!=get_unit_type(j)->tech_requirement) continue;
       hbox=fcwin_hbox_new(helpdlg_win,FALSE);
       fcwin_box_add_box(helpdlg_page_vbox,hbox,FALSE,FALSE,5);
@@ -764,7 +764,7 @@
       fcwin_box_add_button(hbox,get_unit_type(j)->name,
                           ID_HELP_UNIT_LINK,
                           0,FALSE,FALSE,5);
-    }
+    } unit_type_iterate_end;
     for(j=0; j<game.num_tech_types; ++j) {
       if(i==advances[j].req[0]) {
         if(advances[j].req[1]==A_NONE) {
Index: client/gui-win32/repodlgs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-win32/repodlgs.c,v
retrieving revision 1.9
diff -u -r1.9 repodlgs.c
--- client/gui-win32/repodlgs.c 2002/02/25 15:24:56     1.9
+++ client/gui-win32/repodlgs.c 2002/03/04 13:20:05
@@ -673,25 +673,24 @@
 
     k = 0;
     memset(&unittotals, '\0', sizeof(unittotals));
-    for (i=0;i<game.num_unit_types;i++) {
-      if ((unitarray[i].active_count > 0) || (unitarray[i].building_count > 
0)) 
-{
-        can = (can_upgrade_unittype(game.player_ptr, i) != -1);
-        my_snprintf(buf[0], sizeof(buf[0]), "%s", unit_name(i));
+    unit_type_iterate (id) {
+      if ((unitarray[id].active_count > 0) || (unitarray[id].building_count > 
0)) {
+        can = (can_upgrade_unittype(game.player_ptr, id) != -1);
+        my_snprintf(buf[0], sizeof(buf[0]), "%s", unit_name(id));
         my_snprintf(buf[1], sizeof(buf[1]), "%c", can ? '*': '-');
-        my_snprintf(buf[2], sizeof(buf[2]), "%3d", 
unitarray[i].building_count);
-        my_snprintf(buf[3], sizeof(buf[3]), "%3d", unitarray[i].active_count);
-        my_snprintf(buf[4], sizeof(buf[4]), "%3d", unitarray[i].upkeep_shield);
-        my_snprintf(buf[5], sizeof(buf[5]), "%3d", unitarray[i].upkeep_food);
+        my_snprintf(buf[2], sizeof(buf[2]), "%3d", 
unitarray[id].building_count);
+        my_snprintf(buf[3], sizeof(buf[3]), "%3d", unitarray[id].active_count);
+        my_snprintf(buf[4], sizeof(buf[4]), "%3d", 
unitarray[id].upkeep_shield);
+        my_snprintf(buf[5], sizeof(buf[5]), "%3d", unitarray[id].upkeep_food);
        fcwin_listview_add_row(lv,k,AU_COL,row);
-        activeunits_type[k]=(unitarray[i].active_count > 0) ? i : U_LAST;
+        activeunits_type[k]=(unitarray[id].active_count > 0) ? id : U_LAST;
         k++;
-        unittotals.active_count += unitarray[i].active_count;
-        unittotals.upkeep_shield += unitarray[i].upkeep_shield;
-        unittotals.upkeep_food += unitarray[i].upkeep_food;
-        unittotals.building_count += unitarray[i].building_count;
+        unittotals.active_count += unitarray[id].active_count;
+        unittotals.upkeep_shield += unitarray[id].upkeep_shield;
+        unittotals.upkeep_food += unitarray[id].upkeep_food;
+        unittotals.building_count += unitarray[id].building_count;
       }
-    }
+    } unit_type_iterate_end;
     my_snprintf(buf[0],sizeof(buf[0]),"%s",_("Totals"));
     buf[1][0]='\0';
     my_snprintf(buf[1],sizeof(buf[1]),"%d",unittotals.building_count);
Index: client/gui-xaw/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/citydlg.c,v
retrieving revision 1.69
diff -u -r1.69 citydlg.c
--- client/gui-xaw/citydlg.c    2002/03/01 10:22:57     1.69
+++ client/gui-xaw/citydlg.c    2002/03/04 13:20:06
@@ -2336,7 +2336,7 @@
   pdialog->change_list_num_improvements=n;
 
 
-  for(i=0; i<game.num_unit_types; i++)
+  unit_type_iterate(i) {
     if(can_build_unit(pdialog->pcity, i)) {
       turns = city_turns_to_build(pdialog->pcity, i, TRUE, TRUE);
       my_snprintf(pdialog->change_list_names[n],
@@ -2346,6 +2346,7 @@
       pdialog->change_list_names_ptrs[n]=pdialog->change_list_names[n];
       pdialog->change_list_ids[n++]=i;
     }
+  } unit_type_iterate_end;
   
   pdialog->change_list_names_ptrs[n]=0;
 
Index: client/gui-xaw/cityrep.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/cityrep.c,v
retrieving revision 1.36
diff -u -r1.36 cityrep.c
--- client/gui-xaw/cityrep.c    2002/02/25 15:24:57     1.36
+++ client/gui-xaw/cityrep.c    2002/03/04 13:20:07
@@ -332,12 +332,12 @@
       }
     }
 
-    for (i = 0; i < game.num_unit_types; i++) {
+    unit_type_iterate(i) {
       if (can_build_unit(pcity, i)) {
        cids[cids_used] = cid_encode(TRUE, i);
        cids_used++;
       }
-    }
+    } unit_type_iterate_end;
 
     name_and_sort_items(cids, cids_used, items, TRUE, NULL);
     
Index: client/gui-xaw/helpdlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/helpdlg.c,v
retrieving revision 1.39
diff -u -r1.39 helpdlg.c
--- client/gui-xaw/helpdlg.c    2002/02/12 09:36:51     1.39
+++ client/gui-xaw/helpdlg.c    2002/03/04 13:20:07
@@ -915,11 +915,12 @@
        sprintf(buf+strlen(buf), _("Obsoletes %s.\n"),
                improvement_types[j].name);
     }
-    for(j=0; j<game.num_unit_types; ++j) {
+
+    unit_type_iterate(j) {
       if(i==get_unit_type(j)->tech_requirement) 
        sprintf(buf+strlen(buf), _("Allows %s.\n"), 
                get_unit_type(j)->name);
-    }
+    } unit_type_iterate_end;
 
     for(j=0; j<game.num_tech_types; ++j) {
       if(i==advances[j].req[0]) {
Index: client/gui-xaw/repodlgs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/repodlgs.c,v
retrieving revision 1.33
diff -u -r1.33 repodlgs.c
--- client/gui-xaw/repodlgs.c   2002/02/25 15:24:58     1.33
+++ client/gui-xaw/repodlgs.c   2002/03/04 13:20:08
@@ -1111,7 +1111,7 @@
 
     k = 0;
     memset(&unittotals, '\0', sizeof(unittotals));
-    for (i=0;i<game.num_unit_types;i++) {
+    unit_type_iterate(i) {
       if ((unitarray[i].active_count > 0) || (unitarray[i].building_count > 
0)) {
        my_snprintf
          (
@@ -1133,7 +1133,7 @@
        unittotals.upkeep_food += unitarray[i].upkeep_food;
        unittotals.building_count += unitarray[i].building_count;
       }
-    }
+    } unit_type_iterate_end;
     if (k==0) {
       sz_strlcpy(activeunits_list_names[0], "                                
");
       activeunits_list_names_ptrs[0]=activeunits_list_names[0];
Index: client/gui-xaw/wldlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/wldlg.c,v
retrieving revision 1.17
diff -u -r1.17 wldlg.c
--- client/gui-xaw/wldlg.c      2002/02/26 14:34:01     1.17
+++ client/gui-xaw/wldlg.c      2002/03/04 13:20:08
@@ -1331,7 +1331,7 @@
   pdialog->worklist_avail_num_improvements=n;
 
   /*     + Second, units. */
-  for(i=0; i<game.num_unit_types; i++) {
+  unit_type_iterate(i) {
     /* Can the player (eventually) build this improvement? */
     can_build = can_player_build_unit(pplr,i);
     can_eventually_build = can_player_eventually_build_unit(pplr,i);
@@ -1350,7 +1350,7 @@
       pdialog->worklist_avail_names_ptrs[n]=pdialog->worklist_avail_names[n];
       pdialog->worklist_avail_ids[n++]=i;
     }
-  }
+  } unit_type_iterate_end;
   pdialog->worklist_avail_num_targets=n;
 
   /*     + Finally, the global worklists. */
Index: common/game.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/game.c,v
retrieving revision 1.131
diff -u -r1.131 game.c
--- common/game.c       2002/02/27 11:46:19     1.131
+++ common/game.c       2002/03/04 13:20:09
@@ -939,11 +939,11 @@
     sz_strlcpy(tthis->name_orig, tthis->name);
     name_strlcpy(tthis->name, Q_(tthis->name_orig));
   }
-  for (i=0; i<game.num_unit_types; i++) {
+  unit_type_iterate(i) {
     struct unit_type *tthis = &unit_types[i];
     sz_strlcpy(tthis->name_orig, tthis->name);
     name_strlcpy(tthis->name, Q_(tthis->name_orig));
-  }
+  } unit_type_iterate_end;
   for (i=0; i<game.num_impr_types; i++) {
     struct impr_type *tthis = &improvement_types[i];
     sz_strlcpy(tthis->name_orig, tthis->name);
Index: common/unittype.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/unittype.c,v
retrieving revision 1.14
diff -u -r1.14 unittype.c
--- common/unittype.c   2002/02/26 15:49:52     1.14
+++ common/unittype.c   2002/03/04 13:20:09
@@ -327,12 +327,11 @@
 **************************************************************************/
 Unit_Type_id find_unit_type_by_name(char *s)
 {
-  int i;
+  unit_type_iterate(id) {
+    if (strcmp(unit_types[id].name, s)==0)
+      return id;
+  } unit_type_iterate_end;
 
-  for( i=0; i<game.num_unit_types; i++ ) {
-    if (strcmp(unit_types[i].name, s)==0)
-      return i;
-  }
   return U_LAST;
 }
 
@@ -474,22 +473,23 @@
 **************************************************************************/
 static void precalc_one(int i, bool (*func_has)(Unit_Type_id, int))
 {
-  Unit_Type_id u;
   int j;
 
   /* Count: */
-  for(u=0; u<game.num_unit_types; u++) {
+  unit_type_iterate(u) {
     if(unit_type_exists(u) && func_has(u, i)) {
       n_with_role[i]++;
     }
-  }
+  } unit_type_iterate_end;
+
   if(n_with_role[i] > 0) {
     with_role[i] = fc_malloc(n_with_role[i]*sizeof(Unit_Type_id));
-    for(j=0, u=0; u<game.num_unit_types; u++) {
+    j = 0;
+    unit_type_iterate(u) {
       if(unit_type_exists(u) && func_has(u, i)) {
        with_role[i][j++] = u;
       }
-    }
+    } unit_type_iterate_end;
     assert(j==n_with_role[i]);
   }
 }
Index: common/unittype.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/unittype.h,v
retrieving revision 1.8
diff -u -r1.8 unittype.h
--- common/unittype.h   2002/02/14 15:17:22     1.8
+++ common/unittype.h   2002/03/04 13:20:09
@@ -228,4 +228,13 @@
 Unit_Type_id get_role_unit(int role, int index);
 Unit_Type_id best_role_unit(struct city *pcity, int role);
 
+#define unit_type_iterate(unit_type_id)                                       \
+{                                                                             \
+  Unit_Type_id unit_type_id;                                                  \
+  for (unit_type_id = 0; unit_type_id < game.num_unit_types; unit_type_id++) {
+
+#define unit_type_iterate_end                                                 \
+  }                                                                           \
+}
+
 #endif  /* FC__UNITTYPE_H */
Index: server/ruleset.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/ruleset.c,v
retrieving revision 1.99
diff -u -r1.99 ruleset.c
--- server/ruleset.c    2002/03/02 19:57:25     1.99
+++ server/ruleset.c    2002/03/04 13:20:11
@@ -572,7 +572,7 @@
 static void load_unit_names(struct section_file *file)
 {
   char **sec;
-  int nval, i;
+  int nval;
   const char *filename = secfile_filename(file);
 
   section_file_lookup(file,"datafile.description"); /* unused */
@@ -589,11 +589,13 @@
            nval, U_LAST, filename);
     exit(EXIT_FAILURE);
   }
+
   game.num_unit_types = nval;
-  for (i = 0; i < game.num_unit_types; i++ ) {
-    char *name = secfile_lookup_str(file, "%s.name", sec[i]);
-    name_strlcpy(unit_types[i].name, name);
-  }
+  unit_type_iterate(id) {
+    char *name = secfile_lookup_str(file, "%s.name", sec[id]);
+    name_strlcpy(unit_types[id].name, name);
+  } unit_type_iterate_end;
+
   free(sec);
 }
 
@@ -625,24 +627,25 @@
      we might want to know for other fields.  After this we
      can use unit_type_exists()
   */
-  for( i=0; i<game.num_unit_types; i++ ) {
-    u = &unit_types[i];
-    u->tech_requirement = lookup_tech(file, sec[i], "tech_req",
+  unit_type_iterate(id) {
+    u = &unit_types[id];
+    u->tech_requirement = lookup_tech(file, sec[id], "tech_req",
                                      FALSE, filename, u->name);
-  }
-  for( i=0; i<game.num_unit_types; i++ ) {
-    u = &unit_types[i];
-    if (unit_type_exists(i)) {
-      u->obsoleted_by = lookup_unit_type(file, sec[i],
+  }unit_type_iterate_end;
+
+  unit_type_iterate(id) {
+    u = &unit_types[id];
+    if (unit_type_exists(id)) {
+      u->obsoleted_by = lookup_unit_type(file, sec[id],
                                         "obsolete_by", FALSE, filename, 
u->name);
     } else {
-      section_file_lookup(file, "%s.obsolete_by", sec[i]);  /* unused */
+      section_file_lookup(file, "%s.obsolete_by", sec[id]);  /* unused */
       u->obsoleted_by = -1;
     }
-  }
+  } unit_type_iterate_end;
 
   /* main stats: */
-  for( i=0; i<game.num_unit_types; i++ ) {
+  unit_type_iterate(i) {
     u = &unit_types[i];
     
     sval = secfile_lookup_str(file, "%s.move_type", sec[i]);
@@ -697,10 +700,10 @@
     u->gold_cost   = secfile_lookup_int(file, "%s.uk_gold", sec[i]);
 
     u->helptext = lookup_helptext(file, sec[i]);
-  }
+  } unit_type_iterate_end;
   
   /* flags */
-  for(i=0; i<game.num_unit_types; i++) {
+  unit_type_iterate(i) {
     u = &unit_types[i];
     u->flags = 0;
     
@@ -739,10 +742,10 @@
       }
     }
     free(slist);
-  }
+  } unit_type_iterate_end;
     
   /* roles */
-  for(i=0; i<game.num_unit_types; i++) {
+  unit_type_iterate(i) {
     u = &unit_types[i];
     u->roles = 0;
     
@@ -760,14 +763,14 @@
       u->roles |= (1<<(ival-L_FIRST));
     }
     free(slist);
-  }
+  } unit_type_iterate_end;
 
   lookup_tech_list(file, "u_specials", "partisan_req",
                   game.rtech.partisan_req, filename);
 
 
   /* Some more consistency checking: */
-  for( i=0; i<game.num_unit_types; i++ ) {
+  unit_type_iterate(i) {
     if (unit_type_exists(i)) {
       u = &unit_types[i];
       if (!tech_exists(u->tech_requirement)) {
@@ -783,7 +786,7 @@
        u->obsoleted_by = -1;
       }
     }
-  }
+  } unit_type_iterate_end;
 
   /* Setup roles and flags pre-calcs: */
   role_unit_precalcs();
Index: server/rulesout.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/rulesout.c,v
retrieving revision 1.8
diff -u -r1.8 rulesout.c
--- server/rulesout.c   2002/02/14 15:17:37     1.8
+++ server/rulesout.c   2002/03/04 13:20:11
@@ -37,6 +37,7 @@
 #include "support.h"
 #include "tech.h"
 #include "unit.h"
+#include "unittype.h"
 
 #include "rulesout.h"
 
@@ -104,14 +105,14 @@
   }
     
   /* Units */
-  for(j=0; j<game.num_unit_types; j++ ) {
-    struct unit_type *ut = get_unit_type(j);
+  unit_type_iterate(id) {
+    struct unit_type *ut = get_unit_type(id);
     if (ut->tech_requirement == itech) {
       strcpy(buf, "unit: ");
       sz_strlcat(buf, ut->name);
       secfile_insert_str(file, buf, "%s.effect%d", prefix, ieffect++);
     }
-  }
+  } unit_type_iterate_end;
     
   /* Improvements and Wonders */
   for(j=0; j<game.num_impr_types; j++) {
@@ -128,10 +129,10 @@
   }
   
   /* Obsoleted units: */
-  for(j=0; j<game.num_unit_types; j++ ) {
-    struct unit_type *ut = get_unit_type(j);
+  unit_type_iterate(id) {
+    struct unit_type *ut = get_unit_type(id);
     k = ut->obsoleted_by;
-    if (unit_type_exists(j)
+    if (unit_type_exists(id)
        && k != -1
        && unit_type_exists(k)
        && get_unit_type(k)->tech_requirement == itech) {
@@ -139,7 +140,7 @@
       sz_strlcat(buf, ut->name);
       secfile_insert_str(file, buf, "%s.effect%d", prefix, ieffect++);
     }
-  }
+  } unit_type_iterate_end;
     
   /* Obsoleted buildings: */
   for(j=0; j<game.num_impr_types; j++) {

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