Complete.Org: Mailing Lists: Archives: freeciv-dev: April 2004:
[Freeciv-Dev] (PR#8525) duplicated code in unit.c capacity functions
Home

[Freeciv-Dev] (PR#8525) duplicated code in unit.c capacity functions

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#8525) duplicated code in unit.c capacity functions
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 16 Apr 2004 08:00:44 -0700
Reply-to: rt@xxxxxxxxxxx

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

This patch introduces a helper function for missile_carrier_capacity and 
airunit_carrier_capacity.

This is probably only a temporary situation since I'd like to generalize 
the transporter types.  Eventually these two functions and 
ground_unit_transporter_capacity should all become one (simpler) function.

jason

? core.16043
Index: common/unit.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/unit.c,v
retrieving revision 1.203
diff -u -r1.203 unit.c
--- common/unit.c       14 Apr 2004 11:19:45 -0000      1.203
+++ common/unit.c       16 Apr 2004 14:58:05 -0000
@@ -1122,47 +1122,62 @@
   return (&game.players[punit->owner]);
 }
 
-/**************************************************************************
-Returns the number of free spaces for missiles. Can be 0 or negative.
-**************************************************************************/
-int missile_carrier_capacity(int x, int y, struct player *pplayer,
-                            bool count_units_with_extra_fuel)
+/****************************************************************************
+  Measure the carrier (missile + airplane) capacity of the given tile for
+  a player.
+
+  In the future this should probably look at the actual occupancy of the
+  transporters.  However for now we only look at the potential capacity and
+  leave loading up to the caller.
+****************************************************************************/
+static void count_carrier_capacity(int *airall, int *misonly,
+                                  int x, int y, struct player *pplayer,
+                                  bool count_units_with_extra_fuel)
 {
   struct tile *ptile = map_get_tile(x, y);
-  int misonly = 0;
-  int airall = 0;
-  int totalcap;
+
+  *airall = *misonly = 0;
 
   unit_list_iterate(map_get_tile(x, y)->units, punit) {
     if (unit_owner(punit) == pplayer) {
       if (unit_flag(punit, F_CARRIER)
          && !(is_ground_unit(punit) && is_ocean(ptile->terrain))) {
-       airall += get_transporter_capacity(punit);
+       *airall += get_transporter_capacity(punit);
        continue;
       }
       if (unit_flag(punit, F_MISSILE_CARRIER)
          && !(is_ground_unit(punit) && is_ocean(ptile->terrain))) {
-       misonly += get_transporter_capacity(punit);
+       *misonly += get_transporter_capacity(punit);
        continue;
       }
+
       /* Don't count units which have enough fuel (>1) */
       if (is_air_unit(punit)
          && (count_units_with_extra_fuel || punit->fuel <= 1)) {
-       if (unit_flag(punit, F_MISSILE))
-         misonly--;
-       else
-         airall--;
+       if (unit_flag(punit, F_MISSILE)) {
+         (*misonly)--;
+       } else {
+         (*airall)--;
+       }
       }
     }
-  }
-  unit_list_iterate_end;
+  } unit_list_iterate_end;
+}
 
-  if (airall < 0)
-    airall = 0;
+/**************************************************************************
+Returns the number of free spaces for missiles. Can be 0 or negative.
+**************************************************************************/
+int missile_carrier_capacity(int x, int y, struct player *pplayer,
+                            bool count_units_with_extra_fuel)
+{
+  int airall, misonly;
 
-  totalcap = airall + misonly;
+  count_carrier_capacity(&airall, &misonly, x, y, pplayer,
+                        count_units_with_extra_fuel);
 
-  return totalcap;
+  /* Any extra air spaces can be used by missles, but if there aren't enough
+   * air spaces this doesn't bother missiles. */
+  return MAX(airall, 0) + misonly;
 }
 
 /**************************************************************************
@@ -1172,38 +1187,14 @@
 int airunit_carrier_capacity(int x, int y, struct player *pplayer,
                             bool count_units_with_extra_fuel)
 {
-  struct tile *ptile = map_get_tile(x, y);
-  int misonly = 0;
-  int airall = 0;
-
-  unit_list_iterate(map_get_tile(x, y)->units, punit) {
-    if (unit_owner(punit) == pplayer) {
-      if (unit_flag(punit, F_CARRIER)
-         && !(is_ground_unit(punit) && is_ocean(ptile->terrain))) {
-       airall += get_transporter_capacity(punit);
-       continue;
-      }
-      if (unit_flag(punit, F_MISSILE_CARRIER)
-         && !(is_ground_unit(punit) && is_ocean(ptile->terrain))) {
-       misonly += get_transporter_capacity(punit);
-       continue;
-      }
-      /* Don't count units which have enough fuel (>1) */
-      if (is_air_unit(punit)
-         && (count_units_with_extra_fuel || punit->fuel <= 1)) {
-       if (unit_flag(punit, F_MISSILE))
-         misonly--;
-       else
-         airall--;
-      }
-    }
-  }
-  unit_list_iterate_end;
+  int airall, misonly;
 
-  if (misonly < 0)
-    airall += misonly;
+  count_carrier_capacity(&airall, &misonly, x, y, pplayer,
+                        count_units_with_extra_fuel);
 
-  return airall;
+  /* Any extra missile spaces are useless to air units, but if there aren't
+   * enough missile spaces the missles must take up airunit capacity. */
+  return airall + MIN(misonly, 0);
 }
 
 /**************************************************************************

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#8525) duplicated code in unit.c capacity functions, Jason Short <=