Complete.Org: Mailing Lists: Archives: freeciv-dev: July 2006:
[Freeciv-Dev] Re: (PR#13384) [Patch] unit_class_transporter_capacity()
Home

[Freeciv-Dev] Re: (PR#13384) [Patch] unit_class_transporter_capacity()

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: marko.lindqvist@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: (PR#13384) [Patch] unit_class_transporter_capacity()
From: "Marko Lindqvist" <cazfi74@xxxxxxxxx>
Date: Tue, 11 Jul 2006 17:30:54 -0700
Reply-to: bugs@xxxxxxxxxxx

<URL: http://bugs.freeciv.org/Ticket/Display.html?id=13384 >


  Minor bits of previous version have gone in, some parts are waiting 
for commit in their own tickets. This is updated against these changes. 
Also some cleanup has taken place.
  Requires #18380, #18400 (which requires #18390), #18520


  - ML



diff -Nurd -X.diff_ignore freeciv/ai/advmilitary.c freeciv/ai/advmilitary.c
--- freeciv/ai/advmilitary.c    2006-07-11 19:04:50.500000000 +0300
+++ freeciv/ai/advmilitary.c    2006-07-12 01:38:10.078125000 +0300
@@ -490,7 +490,7 @@
       unsigned int vulnerability = assess_danger_unit(pcity, punit);
       int dist = assess_distance(pcity, punit, move_rate);
       /* Although enemy units will not be in our cities,
-       * we might stll consider allies to be dangerous,
+       * we might still consider allies to be dangerous,
        * so dist can be 0. */
       bool igwall = unit_really_ignores_citywalls(punit);
 
@@ -498,14 +498,28 @@
         paramove = unit_type(punit)->paratroopers_range;
       }
 
-      if ((is_ground_unit(punit) && vulnerability != 0)
-          || (is_ground_units_transport(punit))) {
+      if (is_ground_unit(punit) && vulnerability != 0) {
         if (dist <= move_rate * 3 || dist <= paramove + move_rate) {
           urgency++;
         }
         if (dist <= move_rate || dist <= paramove + move_rate) {
           pcity->ai.grave_danger++;
         }
+      } else {
+        unit_class_iterate(punitclass) {
+          if (punitclass->move_type == LAND_MOVING
+              && can_unit_type_transport(punit->type, punitclass)) {
+            /* It can transport some land moving units! */
+
+            if (dist <= move_rate * 3 || dist <= paramove + move_rate) {
+              urgency++;
+            }
+            if (dist <= move_rate || dist <= paramove + move_rate) {
+              pcity->ai.grave_danger++;
+            }
+            break;
+          }
+        } unit_class_iterate_end;
       }
       if (paramove > 0 && can_unit_paradrop(punit)) {
         move_rate += paramove; /* gross simplification */
diff -Nurd -X.diff_ignore freeciv/ai/aidata.c freeciv/ai/aidata.c
--- freeciv/ai/aidata.c 2006-07-11 19:04:50.765625000 +0300
+++ freeciv/ai/aidata.c 2006-07-12 01:38:10.125000000 +0300
@@ -269,9 +269,14 @@
       if (is_sailing_unit(punit)) {
         /* If the enemy has not started sailing yet, or we have total
          * control over the seas, don't worry, keep attacking. */
-        if (is_ground_units_transport(punit)) {
-          ai->threats.invasions = TRUE;
-        }
+        unit_class_iterate(punitclass) {
+          if (punitclass->move_type == LAND_MOVING
+              && can_unit_type_transport(punit->type, punitclass)) {
+            /* Enemy can transport some land units! */
+            ai->threats.invasions = TRUE;
+            break;
+          }
+        } unit_class_iterate_end;
 
         /* The idea is that while our enemies don't have any offensive
          * seaborne units, we don't have to worry. Go on the offensive! */
diff -Nurd -X.diff_ignore freeciv/ai/aiferry.c freeciv/ai/aiferry.c
--- freeciv/ai/aiferry.c        2006-07-11 19:04:51.031250000 +0300
+++ freeciv/ai/aiferry.c        2006-07-12 01:45:00.046875000 +0300
@@ -77,11 +77,18 @@
   ai->stats.available_boats = 0;
  
   unit_list_iterate(pplayer->units, punit) {
-    if (is_sailing_unit(punit) && is_ground_units_transport(punit)) {
-      ai->stats.boats++;
-      if (punit->ai.passenger == FERRY_AVAILABLE) {
-       ai->stats.available_boats++;
-      }
+    if (is_sailing_unit(punit)) {
+        unit_class_iterate(punitclass) {
+          if (punitclass->move_type == LAND_MOVING
+              && can_unit_type_transport(punit->type, punitclass)) {
+            /* Can transport some land units. */
+            ai->stats.boats++;
+            if (punit->ai.passenger == FERRY_AVAILABLE) {
+              ai->stats.available_boats++;
+            }
+            break;
+          }
+        } unit_class_iterate_end;
     }
     if (punit->ai.ferryboat == FERRY_WANTED) {
       UNIT_LOG(LOG_DEBUG, punit, "wants a boat.");
@@ -104,10 +111,17 @@
   freelog(LOG_NORMAL, "Registered: %d free out of total %d",
          ai->stats.available_boats, ai->stats.boats);
   unit_list_iterate(pplayer->units, punit) {
-    if (is_sailing_unit(punit) && is_ground_units_transport(punit)) {
-      freelog(LOG_NORMAL, "#%d. %s[%d], psngr=%d", 
-             n, unit_type(punit)->name, punit->id, punit->ai.passenger);
-      n++;
+    if (is_sailing_unit(punit)) {
+        unit_class_iterate(punitclass) {
+          if (punitclass->move_type == LAND_MOVING
+              && can_unit_type_transport(punit->type, punitclass)) {
+            /* Can transport some land units. */
+            freelog(LOG_NORMAL, "#%d. %s[%d], psngr=%d",
+                    n, unit_type(punit)->name, punit->id, punit->ai.passenger);
+            n++;
+            break;
+          }
+        } unit_class_iterate_end;
     }
   } unit_list_iterate_end;
 }
@@ -204,9 +218,16 @@
   int boats = 0;
 
   unit_list_iterate(pplayer->units, punit) {
-    if (is_sailing_unit(punit) && is_ground_units_transport(punit) 
+    if (is_sailing_unit(punit)
        && punit->ai.passenger == FERRY_AVAILABLE) {
-      boats++;
+      unit_class_iterate(punitclass) {
+        if (punitclass->move_type == LAND_MOVING
+            && can_unit_type_transport(punit->type, punitclass)) {
+          /* Can transport some land units. */
+          boats++;
+          break;
+        }
+      } unit_class_iterate_end;
     }
   } unit_list_iterate_end;
 
diff -Nurd -X.diff_ignore freeciv/common/aicore/pf_tools.c 
freeciv/common/aicore/pf_tools.c
--- freeciv/common/aicore/pf_tools.c    2006-07-12 00:49:06.500000000 +0300
+++ freeciv/common/aicore/pf_tools.c    2006-07-12 01:47:15.859375000 +0300
@@ -157,7 +157,7 @@
   int move_cost;
 
   if (!is_native_to_class(param->class, terrain1)) {
-    if (ground_unit_transporter_capacity(ptile1, param->owner) > 0) {
+    if (unit_class_transporter_capacity(ptile1, param->owner, param->class) > 
0) {
       move_cost = SINGLE_MOVE;
     } else {
       move_cost = PF_IMPOSSIBLE_MC;
@@ -190,7 +190,7 @@
   if (!is_native_to_class(param->class, tgt_tile->terrain)) {
 
     /* Any-to-Sea */
-    if (ground_unit_transporter_capacity(tgt_tile, param->owner) > 0) {
+    if (unit_class_transporter_capacity(tgt_tile, param->owner, param->class) 
> 0) {
       move_cost = SINGLE_MOVE;
     } else {
       move_cost = PF_IMPOSSIBLE_MC;
@@ -272,7 +272,7 @@
   int move_cost = PF_IMPOSSIBLE_MC;
 
   if (!is_native_to_class(param->class, terrain1)) {
-    if (ground_unit_transporter_capacity(ptile, param->owner) > 0) {
+    if (unit_class_transporter_capacity(ptile, param->owner, param->class) > 
0) {
       /* Landing */
       move_cost = terrain0->movement_cost * SINGLE_MOVE;
     } else {
@@ -300,7 +300,7 @@
   int move_cost;
 
   if (!is_native_to_class(param->class, ptile1->terrain)) {
-    if (ground_unit_transporter_capacity(ptile1, param->owner) > 0) {
+    if (unit_class_transporter_capacity(ptile1, param->owner, param->class) > 
0) {
       move_cost = MOVE_COST_ROAD;
     } else {
       move_cost = PF_IMPOSSIBLE_MC;
@@ -335,7 +335,7 @@
   int move_cost;
 
   if (!is_native_to_class(param->class, ptile->terrain)) {
-    if (ground_unit_transporter_capacity(ptile, param->owner) > 0) {
+    if (unit_class_transporter_capacity(ptile, param->owner, param->class) > 
0) {
       /* Landing */
       move_cost = MOVE_COST_ROAD;
     } else {
diff -Nurd -X.diff_ignore freeciv/common/movement.c freeciv/common/movement.c
--- freeciv/common/movement.c   2006-07-12 00:49:06.531250000 +0300
+++ freeciv/common/movement.c   2006-07-12 01:53:07.578125000 +0300
@@ -34,9 +34,6 @@
   "Air", "Land", "Sea", "Heli"
 };
 
-static bool can_unit_type_transport(const struct unit_type *transporter,
-                                   const struct unit_type *transported);
-
 /****************************************************************************
   This function calculates the move rate of the unit, taking into 
   account the penalty for reduced hitpoints (affects sea and land 
@@ -370,8 +367,8 @@
 
   if (get_unit_move_type(punittype) == LAND_MOVING) {
     /* 4) */
-    if (is_ocean(dst_tile->terrain) &&
-       ground_unit_transporter_capacity(dst_tile, unit_owner) <= 0) {
+    if (is_ocean(dst_tile->terrain)
+       && unit_class_transporter_capacity(dst_tile, unit_owner, 
punittype->class) <= 0) {
       /* Ground units can't move onto ocean tiles unless there's enough
        * room on transporters for them. */
       return MR_NO_SEA_TRANSPORTER_CAPACITY;
@@ -437,20 +434,20 @@
 bool can_unit_transport(const struct unit *transporter,
                         const struct unit *transported)
 {
-  return can_unit_type_transport(transporter->type, transported->type);
+  return can_unit_type_transport(transporter->type, transported->type->class);
 }
 
 /**************************************************************************
-  Return TRUE iff transporter type has ability to transport transported type.
+  Return TRUE iff transporter type has ability to transport transported class.
 **************************************************************************/
-static bool can_unit_type_transport(const struct unit_type *transporter,
-                                    const struct unit_type *transported)
+bool can_unit_type_transport(const struct unit_type *transporter,
+                             const struct unit_class *transported)
 {
   if (transporter->transport_capacity <= 0) {
     return FALSE;
   }
 
-  if (get_unit_move_type(transported) == LAND_MOVING) {
+  if (transported->move_type == LAND_MOVING) {
     if ((unit_type_flag(transporter, F_CARRIER)
          || unit_type_flag(transporter, F_MISSILE_CARRIER))) {
       return FALSE;
@@ -458,23 +455,23 @@
     return TRUE;
   }
 
-  if (!unit_class_flag(get_unit_class(transported), UCF_MISSILE)
+  if (!unit_class_flag(transported, UCF_MISSILE)
      && unit_type_flag(transporter, F_MISSILE_CARRIER)) {
     return FALSE;
   }
 
-  if (unit_class_flag(get_unit_class(transported), UCF_MISSILE)) {
+  if (unit_class_flag(transported, UCF_MISSILE)) {
     if (!unit_type_flag(transporter, F_MISSILE_CARRIER)
         && !unit_type_flag(transporter, F_CARRIER)) {
       return FALSE;
     }
-  } else if ((get_unit_move_type(transported) == AIR_MOVING
-              || get_unit_move_type(transported) == HELI_MOVING)
+  } else if ((transported->move_type == AIR_MOVING
+              || transported->move_type == HELI_MOVING)
              && !unit_type_flag(transporter, F_CARRIER)) {
     return FALSE;
   }
 
-  if (get_unit_move_type(transported) == SEA_MOVING) {
+  if (transported->move_type == SEA_MOVING) {
     /* No unit can transport sea units at the moment */
     return FALSE;
   }
@@ -514,3 +511,27 @@
 
   return NULL;
 }
+ 
+/**************************************************************************
+ Returns the number of free spaces for units of given class.
+ Can be 0.
+**************************************************************************/
+int unit_class_transporter_capacity(const struct tile *ptile,
+                                    const struct player *pplayer,
+                                    const struct unit_class *pclass)
+{
+  int availability = 0;
+
+  unit_list_iterate(ptile->units, punit) {
+    if (unit_owner(punit) == pplayer
+        || pplayers_allied(unit_owner(punit), pplayer)) {
+
+      if (can_unit_type_transport(punit->type, pclass)) {
+        availability += get_transporter_capacity(punit);
+        availability -= get_transporter_occupancy(punit);
+      }
+    }
+  } unit_list_iterate_end;
+
+  return availability;
+}
diff -Nurd -X.diff_ignore freeciv/common/movement.h freeciv/common/movement.h
--- freeciv/common/movement.h   2006-07-12 00:49:06.546875000 +0300
+++ freeciv/common/movement.h   2006-07-12 01:38:10.250000000 +0300
@@ -51,6 +51,11 @@
                                             const struct tile *dst_tile,
                                             bool igzoc);
 bool can_unit_transport(const struct unit *transporter, const struct unit 
*transported);
+bool can_unit_type_transport(const struct unit_type *transporter,
+                             const struct unit_class *transported);
+int unit_class_transporter_capacity(const struct tile *ptile,
+                                    const struct player *pplayer,
+                                    const struct unit_class *pclass);
 struct unit *find_transport_from_tile(struct unit *punit, struct tile *ptile);
 
 enum unit_move_type move_type_from_str(const char *s);
diff -Nurd -X.diff_ignore freeciv/common/unit.c freeciv/common/unit.c
--- freeciv/common/unit.c       2006-07-11 19:05:54.296875000 +0300
+++ freeciv/common/unit.c       2006-07-12 01:58:48.515625000 +0300
@@ -217,31 +217,6 @@
          && can_cities_trade(phomecity, pdestcity));
 }
 
-
-/**************************************************************************
-Returns the number of free spaces for ground units. Can be 0 or negative.
-**************************************************************************/
-int ground_unit_transporter_capacity(const struct tile *ptile,
-                                    const struct player *pplayer)
-{
-  int availability = 0;
-
-  unit_list_iterate(ptile->units, punit) {
-    if (unit_owner(punit) == pplayer
-        || pplayers_allied(unit_owner(punit), pplayer)) {
-      if (is_ground_units_transport(punit)
-         && !(is_ground_unit(punit) && is_ocean(ptile->terrain))) {
-       availability += get_transporter_capacity(punit);
-      } else if (is_ground_unit(punit)) {
-       availability--;
-      }
-    }
-  }
-  unit_list_iterate_end;
-
-  return availability;
-}
-
 /**************************************************************************
   Return the number of units the transporter can hold (or 0).
 **************************************************************************/
@@ -251,16 +226,6 @@
 }
 
 /**************************************************************************
-  Return TRUE iff the unit is a transporter of ground units.
-**************************************************************************/
-bool is_ground_units_transport(const struct unit *punit)
-{
-  return (get_transporter_capacity(punit) > 0
-         && !unit_flag(punit, F_MISSILE_CARRIER)
-         && !unit_flag(punit, F_CARRIER));
-}
-
-/**************************************************************************
   Is the unit capable of attacking?
 **************************************************************************/
 bool is_attack_unit(const struct unit *punit)
@@ -1552,7 +1517,7 @@
 
   if (get_transporter_occupancy(punit) > to_unittype->transport_capacity) {
     /* TODO: allow transported units to be reassigned.  Check for
-     * ground_unit_transporter_capacity here and make changes to
+     * unit_class_transporter_capacity() here and make changes to
      * upgrade_unit. */
     return UR_NOT_ENOUGH_ROOM;
   }
diff -Nurd -X.diff_ignore freeciv/common/unit.h freeciv/common/unit.h
--- freeciv/common/unit.h       2006-07-11 19:05:54.328125000 +0300
+++ freeciv/common/unit.h       2006-07-12 01:38:10.281250000 +0300
@@ -264,10 +264,7 @@
 bool kills_citizen_after_attack(const struct unit *punit);
 
 const char *unit_activity_text(const struct unit *punit);
-int ground_unit_transporter_capacity(const struct tile *ptile,
-                                    const struct player *pplayer);
 int get_transporter_capacity(const struct unit *punit);
-bool is_ground_units_transport(const struct unit *punit);
 int missile_carrier_capacity(const struct tile *ptile,
                             const struct player *pplayer,
                             bool count_units_with_extra_fuel);
diff -Nurd -X.diff_ignore freeciv/server/gotohand.c freeciv/server/gotohand.c
--- freeciv/server/gotohand.c   2006-07-11 19:08:16.921875000 +0300
+++ freeciv/server/gotohand.c   2006-07-12 01:38:10.375000000 +0300
@@ -311,7 +311,9 @@
          continue; /* No need for all the calculations */
 
         if (is_ocean(tile_get_terrain(tile1))) {
-          if (punit && ground_unit_transporter_capacity(tile1, pplayer) > 0)
+          if (punit &&
+              unit_class_transporter_capacity(tile1, pplayer,
+                                              
get_unit_class(unit_type(punit))) > 0)
            move_cost = SINGLE_MOVE;
           else
            continue;
@@ -657,7 +659,8 @@
                       RR loops, ie you can't create a cycle with the same 
move_cost */
 
        if (is_ocean(pdesttile->terrain)) {
-         if (ground_unit_transporter_capacity(tile1, unit_owner(punit)) <= 0)
+         if (unit_class_transporter_capacity(tile1, unit_owner(punit),
+                                              
get_unit_class(unit_type(punit))) <= 0)
            continue;
          else
            move_cost = 3;
@@ -1253,7 +1256,8 @@
     if (is_ocean(tile_get_terrain(ptile))) {
       /* Going to a sea tile, the target should be next to our continent 
        * and with a boat */
-      if (ground_unit_transporter_capacity(ptile, pplayer) > 0) {
+      if (unit_class_transporter_capacity(ptile, pplayer,
+                                          get_unit_class(unit_type(punit))) > 
0) {
         adjc_iterate(ptile, tmp_tile) {
           if (tile_get_continent(tmp_tile) == tile_get_continent(punit->tile))
             /* The target is adjacent to our continent! */

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