Complete.Org: Mailing Lists: Archives: freeciv-dev: January 2003:
[Freeciv-Dev] (PR#2804) topology cleanup for dist_nearest_city
Home

[Freeciv-Dev] (PR#2804) topology cleanup for dist_nearest_city

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients:;
Subject: [Freeciv-Dev] (PR#2804) topology cleanup for dist_nearest_city
From: "Jason Short via RT" <rt@xxxxxxxxxxxxxx>
Date: Sun, 12 Jan 2003 20:17:06 -0800
Reply-to: rt@xxxxxxxxxxxxxx

I noticed that dist_nearest_city is not gen-topology safe.  The attached 
patch fixes this, and cleans up a few other things in that function.

jason

Index: ai/aitools.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aitools.c,v
retrieving revision 1.73
diff -u -r1.73 aitools.c
--- ai/aitools.c        2003/01/06 00:39:01     1.73
+++ ai/aitools.c        2003/01/13 04:14:45
@@ -405,21 +405,29 @@
                                bool everywhere, bool enemy)
 { 
   struct city *pc=NULL;
-  int dist = MAX(map.xsize / 2, map.ysize);
+  int best_dist = -1;
   int con = map_get_continent(x, y, NULL);
 
   players_iterate(pplay) {
-    if ((enemy) && (pplayer) && (!pplayers_at_war(pplayer,pplay))) continue;
+    /* If "enemy" is set, only consider cities whose owner we're at
+     * war with. */
+    if (enemy && pplayer && !pplayers_at_war(pplayer, pplay)) {
+      continue;
+    }
 
-    city_list_iterate(pplay->cities, pcity)
-      if (real_map_distance(x, y, pcity->x, pcity->y) < dist &&
-         (everywhere || con == 0 || con == 
-          map_get_continent(pcity->x, pcity->y, NULL)) &&
-         (!pplayer || map_get_known(pcity->x, pcity->y, pplayer))) {
-        dist = real_map_distance(x, y, pcity->x, pcity->y);
+    city_list_iterate(pplay->cities, pcity) {
+      int city_dist = real_map_distance(x, y, pcity->x, pcity->y);
+
+      /* Find the closest city known to the player with a matching
+       * continent. */
+      if ((best_dist == -1 || city_dist < best_dist)
+         && (everywhere || con == 0
+             || con == map_get_continent(pcity->x, pcity->y, NULL))
+         && (!pplayer || map_get_known(pcity->x, pcity->y, pplayer))) {
+       best_dist = city_dist;
         pc = pcity;
       }
-    city_list_iterate_end;
+    } city_list_iterate_end;
   } players_iterate_end;
 
   return(pc);

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#2804) topology cleanup for dist_nearest_city, Jason Short via RT <=