Complete.Org: Mailing Lists: Archives: freeciv-dev: March 2002:
[Freeciv-Dev] unit_type_iterate (PR#1302)
Home

[Freeciv-Dev] unit_type_iterate (PR#1302)

[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] unit_type_iterate (PR#1302)
From: jdorje@xxxxxxxxxxxxxxxxxxxxx
Date: Tue, 5 Mar 2002 21:22:30 -0800 (PST)

Now that the impr_type_iterate change is in, here's an updated unit_type_iterate.

In most cases I've avoided renaming the variable of iteration, even though they are often badly named and may shadow function-wide definitions.

In only one case should the compiled code be changed: send_ruleset_units in server/ruleset.c. This was the sole loop that iterated over the unit_types pointer rather than with an integer.

jason
Index: ai/advmilitary.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/advmilitary.c,v
retrieving revision 1.96
diff -u -r1.96 advmilitary.c
--- ai/advmilitary.c    2002/03/05 10:20:23     1.96
+++ ai/advmilitary.c    2002/03/06 05:20:08
@@ -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] != 0 && is_ai_simple_military(i)) {
       tech_req = unit_types[i].tech_requirement;
       n = desire[i] * unit_types[bestid].build_cost / best;
@@ -524,7 +523,8 @@
       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 +540,7 @@
                            struct city *pcity, int b, Unit_Type_id n,
                             bool vet, int x, int y, bool 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 +548,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 +654,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.106
diff -u -r1.106 aicity.c
--- ai/aicity.c 2002/03/06 03:03:03     1.106
+++ ai/aicity.c 2002/03/06 05:20:08
@@ -592,12 +592,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);
@@ -608,7 +607,7 @@
         bestid = i;
       }
     }
-  }
+  } unit_type_iterate_end;
   return bestid;
 }
 
@@ -624,12 +623,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)) {
@@ -640,7 +638,7 @@
         bestid = i;
       }
     }
-  }
+  } unit_type_iterate_end;
   return bestid;
 }
 
@@ -678,14 +676,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 &&
@@ -701,7 +698,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.87
diff -u -r1.87 climisc.c
--- client/climisc.c    2002/03/06 03:03:04     1.87
+++ client/climisc.c    2002/03/06 05:20:09
@@ -869,7 +869,7 @@
  */
 int collect_cids4(cid * dest_cids, struct city *pcity, bool advanced_tech)
 {
-  int id, cids_used = 0;
+  int cids_used = 0;
 
   impr_type_iterate(id) {
     bool can_build = can_player_build_improvement(game.player_ptr, id);
@@ -890,7 +890,7 @@
     }
   } impr_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);
@@ -907,7 +907,8 @@
       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.50
diff -u -r1.50 helpdata.c
--- client/helpdata.c   2002/03/06 03:03:04     1.50
+++ client/helpdata.c   2002/03/06 05:20:09
@@ -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.54
diff -u -r1.54 helpdlg.c
--- client/gui-gtk/helpdlg.c    2002/03/06 03:03:06     1.54
+++ client/gui-gtk/helpdlg.c    2002/03/06 05:20:10
@@ -843,7 +843,7 @@
       }
     } impr_type_iterate_end;
 
-    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);
@@ -853,7 +853,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.53
diff -u -r1.53 repodlgs.c
--- client/gui-gtk/repodlgs.c   2002/03/06 03:03:06     1.53
+++ client/gui-gtk/repodlgs.c   2002/03/06 05:20:11
@@ -1021,7 +1021,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)) {
        can = (can_upgrade_unittype(game.player_ptr, i) != -1);
         my_snprintf(buf[0], sizeof(buf[0]), "%-27s", unit_name(i));
@@ -1040,7 +1040,7 @@
        unittotals.upkeep_food += unitarray[i].upkeep_food;
        unittotals.building_count += unitarray[i].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.49
diff -u -r1.49 citydlg.c
--- client/gui-mui/citydlg.c    2002/03/06 03:03:07     1.49
+++ client/gui-mui/citydlg.c    2002/03/06 05:20:12
@@ -1350,8 +1350,7 @@
     }
     DoMethod(pcprod->available_listview, MUIM_NList_InsertSingle, 20000, 
MUIV_NList_Insert_Bottom);
 
-    for (i = 0; i < game.num_unit_types; i++)
-    {
+    unit_type_iterate(i) {
       if (can_build_unit(pcity, i))
       {
         DoMethod(pcprod->available_listview, MUIM_NList_InsertSingle, i + 
10000, MUIV_NList_Insert_Bottom);
@@ -1361,7 +1360,7 @@
 
         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/06 05:20:12
@@ -133,13 +133,12 @@
       }
     }
 
-    for (i = 0; i < game.num_unit_types; i++)
-    {
+    unit_type_iterate(i) {
       if (can_build_unit(pcity, i))
       {
        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.27
diff -u -r1.27 helpdlg.c
--- client/gui-mui/helpdlg.c    2002/03/06 03:03:08     1.27
+++ client/gui-mui/helpdlg.c    2002/03/06 05:20:12
@@ -752,8 +752,7 @@
            DoMethod(help_tech_group, OM_ADDMEMBER, o);
        } impr_type_iterate_end;
 
-       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;
@@ -768,7 +767,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.20
diff -u -r1.20 repodlgs.c
--- client/gui-mui/repodlgs.c   2002/03/06 03:03:08     1.20
+++ client/gui-mui/repodlgs.c   2002/03/06 05:20:13
@@ -940,8 +940,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;
@@ -952,7 +951,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.19
diff -u -r1.19 worklistclass.c
--- client/gui-mui/worklistclass.c      2002/03/06 03:03:08     1.19
+++ client/gui-mui/worklistclass.c      2002/03/06 05:20:13
@@ -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.19
diff -u -r1.19 citydlg.c
--- client/gui-win32/citydlg.c  2002/03/06 03:03:08     1.19
+++ client/gui-win32/citydlg.c  2002/03/06 05:20:14
@@ -1148,13 +1148,14 @@
        
       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)) {
          get_city_dialog_production_row(row, sizeof(buf[0]), i,
                                         TRUE, pdialog->pcity);
          fcwin_listview_add_row(lv,n,4,row);
          pdialog->change_list_ids[n++]=i;
        }
+      } 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.7
diff -u -r1.7 helpdlg.c
--- client/gui-win32/helpdlg.c  2002/03/06 03:03:09     1.7
+++ client/gui-win32/helpdlg.c  2002/03/06 05:20:14
@@ -758,7 +758,7 @@
       }
     } impr_type_iterate_end;
 
-    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);
@@ -766,7 +766,8 @@
       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/06 05:20:15
@@ -673,7 +673,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)) 
 {
         can = (can_upgrade_unittype(game.player_ptr, i) != -1);
@@ -691,7 +691,7 @@
         unittotals.upkeep_food += unitarray[i].upkeep_food;
         unittotals.building_count += unitarray[i].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.70
diff -u -r1.70 citydlg.c
--- client/gui-xaw/citydlg.c    2002/03/06 03:03:10     1.70
+++ client/gui-xaw/citydlg.c    2002/03/06 05:20:16
@@ -2339,7 +2339,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],
@@ -2349,6 +2349,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.37
diff -u -r1.37 cityrep.c
--- client/gui-xaw/cityrep.c    2002/03/06 03:03:10     1.37
+++ client/gui-xaw/cityrep.c    2002/03/06 05:20:16
@@ -332,12 +332,12 @@
       }
     } impr_type_iterate_end;
 
-    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.40
diff -u -r1.40 helpdlg.c
--- client/gui-xaw/helpdlg.c    2002/03/06 03:03:10     1.40
+++ client/gui-xaw/helpdlg.c    2002/03/06 05:20:16
@@ -916,11 +916,11 @@
                improvement_types[j].name);
     } impr_type_iterate_end;
 
-    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.34
diff -u -r1.34 repodlgs.c
--- client/gui-xaw/repodlgs.c   2002/03/06 03:03:11     1.34
+++ client/gui-xaw/repodlgs.c   2002/03/06 05:20:17
@@ -1114,7 +1114,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
          (
@@ -1136,7 +1136,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.18
diff -u -r1.18 wldlg.c
--- client/gui-xaw/wldlg.c      2002/03/06 03:03:11     1.18
+++ client/gui-xaw/wldlg.c      2002/03/06 05:20:17
@@ -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.133
diff -u -r1.133 game.c
--- common/game.c       2002/03/06 03:03:12     1.133
+++ common/game.c       2002/03/06 05:20:18
@@ -937,11 +937,12 @@
     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;
 
   impr_type_iterate(i) {
     struct impr_type *tthis = &improvement_types[i];
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/06 05:20:18
@@ -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/06 05:20:18
@@ -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.102
diff -u -r1.102 ruleset.c
--- server/ruleset.c    2002/03/06 03:03:16     1.102
+++ server/ruleset.c    2002/03/06 05:20:20
@@ -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);
 
   (void) 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,12 +627,13 @@
      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++ ) {
+  unit_type_iterate(i) {
     u = &unit_types[i];
     u->tech_requirement = lookup_tech(file, sec[i], "tech_req",
                                      FALSE, filename, u->name);
-  }
-  for( i=0; i<game.num_unit_types; i++ ) {
+  } unit_type_iterate_end;
+
+  unit_type_iterate(i) {
     u = &unit_types[i];
     if (unit_type_exists(i)) {
       u->obsoleted_by = lookup_unit_type(file, sec[i],
@@ -639,10 +642,10 @@
       (void) section_file_lookup(file, "%s.obsolete_by", sec[i]); /* 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();
@@ -2291,9 +2294,10 @@
 static void send_ruleset_units(struct conn_list *dest)
 {
   struct packet_ruleset_unit packet;
-  struct unit_type *u;
 
-  for(u=unit_types; u<unit_types+game.num_unit_types; u++) {
+  unit_type_iterate(utype_id) {
+    struct unit_type *u = get_unit_type(utype_id);
+
     packet.id = u-unit_types;
     sz_strlcpy(packet.name, u->name_orig);
     sz_strlcpy(packet.graphic_str, u->graphic_str);
@@ -2323,7 +2327,7 @@
     packet.helptext = u->helptext;   /* pointer assignment */
 
     lsend_packet_ruleset_unit(dest, &packet);
-  }
+  } unit_type_iterate_end;
 }
 
 /**************************************************************************
Index: server/rulesout.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/rulesout.c,v
retrieving revision 1.9
diff -u -r1.9 rulesout.c
--- server/rulesout.c   2002/03/06 03:03:17     1.9
+++ server/rulesout.c   2002/03/06 05:20:20
@@ -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 */
   impr_type_iterate(j) {
@@ -128,10 +129,10 @@
   } impr_type_iterate_end;
   
   /* 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: */
   impr_type_iterate(j) {

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