Complete.Org: Mailing Lists: Archives: freeciv-dev: March 2005:
[Freeciv-Dev] Re: (PR#12434) Fix two wonder city bugs
Home

[Freeciv-Dev] Re: (PR#12434) Fix two wonder city bugs

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] Re: (PR#12434) Fix two wonder city bugs
From: "Per I. Mathisen" <per@xxxxxxxxxxx>
Date: Sat, 5 Mar 2005 16:07:03 -0800
Reply-to: bugs@xxxxxxxxxxx

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

On Sat, 5 Mar 2005, Jason Short wrote:
> 2.In this particular case referring to the city by ID rather than
> pointer will allow checking for an invalidated city.

Good idea. Patch attached.

  - Per

Index: ai/advdomestic.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/advdomestic.c,v
retrieving revision 1.127
diff -u -r1.127 advdomestic.c
--- ai/advdomestic.c    2 Mar 2005 10:43:56 -0000       1.127
+++ ai/advdomestic.c    6 Mar 2005 00:02:48 -0000
@@ -58,17 +58,18 @@
   int caravans = 0;
   /* The type of the caravan */
   Unit_Type_id unit_type;
+  struct city *wonder_city = find_city_by_id(ai->wonder_city);
 
   if (num_role_units(F_HELP_WONDER) == 0) {
     /* No such units available in the ruleset */
     return;
   }
 
-  if (pcity == ai->wonder_city 
-      || ai->wonder_city == NULL
+  if (pcity == wonder_city 
+      || wonder_city == NULL
       || pcity->ai.distance_to_wonder_city <= 0
-      || ai->wonder_city->is_building_unit
-      || !is_wonder(ai->wonder_city->currently_building)) {
+      || wonder_city->is_building_unit
+      || !is_wonder(wonder_city->currently_building)) {
     /* A distance of zero indicates we are very far away, possibly
      * on another continent. */
     return;
@@ -99,16 +100,16 @@
   }
 
   /* Check if wonder needs a little help. */
-  if (build_points_left(ai->wonder_city) 
+  if (build_points_left(wonder_city) 
       > unit_build_shield_cost(unit_type) * caravans) {
-    Impr_Type_id wonder = ai->wonder_city->currently_building;
-    int want = ai->wonder_city->ai.building_want[wonder];
+    Impr_Type_id wonder = wonder_city->currently_building;
+    int want = wonder_city->ai.building_want[wonder];
     int dist = pcity->ai.distance_to_wonder_city /
                get_unit_type(unit_type)->move_rate;
 
     want /= MAX(dist, 1);
     CITY_LOG(LOG_DEBUG, pcity, "want %s to help wonder in %s with %d", 
-             unit_name(unit_type), ai->wonder_city->name, want);
+             unit_name(unit_type), wonder_city->name, want);
     if (want > choice->want) {
       /* This sets our tech want in cases where we cannot actually build
        * the unit. */
@@ -145,14 +146,14 @@
   unit_type = best_role_unit(pcity, F_SETTLERS);
 
   if (unit_type != U_LAST
-      && (pcity != ai->wonder_city
+      && (pcity->id != ai->wonder_city
           || get_unit_type(unit_type)->pop_cost == 0)
       && pcity->surplus[O_FOOD] > utype_upkeep_cost(get_unit_type(unit_type),
                                                    gov, O_FOOD)) {
     /* settler_want calculated in settlers.c called from ai_manage_cities() */
     int want = pcity->ai.settler_want;
 
-    if (ai->wonder_city == pcity) {
+    if (ai->wonder_city == pcity->id) {
       want /= 5;
     }
 
@@ -174,14 +175,14 @@
   unit_type = best_role_unit(pcity, F_CITIES);
 
   if (unit_type != U_LAST
-      && (pcity != ai->wonder_city
+      && (pcity->id != ai->wonder_city
           || get_unit_type(unit_type)->pop_cost == 0)
       && pcity->surplus[O_FOOD] >= utype_upkeep_cost(get_unit_type(unit_type),
                                                     gov, O_FOOD)) {
     /* founder_want calculated in settlers.c, called from ai_manage_cities(). 
*/
     int want = pcity->ai.founder_want;
 
-    if (ai->wonder_city == pcity) {
+    if (ai->wonder_city == pcity->id) {
       want /= 5;
     }
 
Index: ai/advmilitary.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/advmilitary.c,v
retrieving revision 1.186
diff -u -r1.186 advmilitary.c
--- ai/advmilitary.c    2 Mar 2005 10:43:56 -0000       1.186
+++ ai/advmilitary.c    6 Mar 2005 00:02:48 -0000
@@ -1253,7 +1253,7 @@
   freelog(LOG_DEBUG, "%s: danger = %d, grave_danger = %d, our_def = %d",
           pcity->name, pcity->ai.danger, pcity->ai.grave_danger, our_def);
 
-  if (pcity == ai->wonder_city && pcity->ai.grave_danger == 0) {
+  if (pcity->id == ai->wonder_city && pcity->ai.grave_danger == 0) {
     return; /* Other cities can build our defenders, thank you! */
   }
 
@@ -1358,7 +1358,7 @@
 
   if (pcity->surplus[O_SHIELD] <= 0 
       || pcity->ppl_unhappy[4] > pcity->ppl_unhappy[2]
-      || pcity == ai->wonder_city) {
+      || pcity->id == ai->wonder_city) {
     /* Things we consider below are not life-saving so we don't want to 
      * build them if our populace doesn't feel like it */
     return;
Index: ai/aicity.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aicity.c,v
retrieving revision 1.198
diff -u -r1.198 aicity.c
--- ai/aicity.c 3 Mar 2005 23:11:55 -0000       1.198
+++ ai/aicity.c 6 Mar 2005 00:02:48 -0000
@@ -595,7 +595,7 @@
   }
 
   /* Are we wonder city? Try to avoid building non-wonders very much. */
-  if (pcity == ai->wonder_city && !is_wonder(id)) {
+  if (pcity->id == ai->wonder_city && !is_wonder(id)) {
     v /= 5;
   }
 
@@ -668,12 +668,17 @@
   Unit_Type_id unittype;
   struct unit *ghost;
   int maxrange;
+  struct city *wonder_city = find_city_by_id(ai->wonder_city);
 
   city_list_iterate(pplayer->cities, acity) {
     acity->ai.distance_to_wonder_city = 0; /* unavailable */
   } city_list_iterate_end;
 
-  if (ai->wonder_city == NULL) {
+  if (wonder_city == NULL) {
+    return;
+  }
+  if (wonder_city->owner != pplayer->player_no) {
+    ai->wonder_city = 0;
     return;
   }
 
@@ -681,7 +686,7 @@
   if (unittype == U_LAST) {
     return;
   }
-  ghost = create_unit_virtual(pplayer, ai->wonder_city, unittype, 0);
+  ghost = create_unit_virtual(pplayer, wonder_city, unittype, 0);
   maxrange = unit_move_rate(ghost) * 7;
 
   pft_fill_unit_parameter(&parameter, ghost);
@@ -712,20 +717,27 @@
 #define RECALC_SPEED 5
 {
   struct ai_data *ai = ai_data_get(pplayer);
+  struct city *wonder_city = find_city_by_id(ai->wonder_city);
+
+  if (wonder_city && wonder_city->owner != pplayer->player_no) {
+    /* We lost it to the enemy! */
+    ai->wonder_city = 0;
+    wonder_city = NULL;
+  }
 
   /* Preliminary analysis - find our Wonder City. Also check if it
    * is sane to continue building the wonder in it. If either does
    * not check out, make a Wonder City. */
-  if (!(ai->wonder_city != NULL
-        && ai->wonder_city->surplus[O_SHIELD] > 0
-        && !ai->wonder_city->is_building_unit
-        && is_wonder(ai->wonder_city->currently_building)
-        && can_build_improvement(ai->wonder_city, 
-                                 ai->wonder_city->currently_building)
-        && !improvement_obsolete(pplayer, ai->wonder_city->currently_building)
-        && !is_building_replaced(ai->wonder_city, 
-                                 ai->wonder_city->currently_building))
-      || ai->wonder_city == NULL) {
+  if (!(wonder_city != NULL
+        && wonder_city->surplus[O_SHIELD] > 0
+        && !wonder_city->is_building_unit
+        && is_wonder(wonder_city->currently_building)
+        && can_build_improvement(wonder_city, 
+                                 wonder_city->currently_building)
+        && !improvement_obsolete(pplayer, wonder_city->currently_building)
+        && !is_building_replaced(wonder_city, 
+                                 wonder_city->currently_building))
+      || wonder_city == NULL) {
     /* Find a new wonder city! */
     int best_candidate_value = 0;
     struct city *best_candidate = NULL;
@@ -765,7 +777,8 @@
     } city_list_iterate_end;
     if (best_candidate) {
       CITY_LOG(LOG_DEBUG, best_candidate, "chosen as wonder-city!");
-      ai->wonder_city = best_candidate;
+      ai->wonder_city = best_candidate->id;
+      wonder_city = best_candidate;
     }
   }
   calculate_wonder_helpers(pplayer, ai);
@@ -781,7 +794,7 @@
       continue;
     }
     city_list_iterate(pplayer->cities, pcity) {
-      if (pcity != ai->wonder_city && is_wonder(id)) {
+      if (pcity != wonder_city && is_wonder(id)) {
         /* Only wonder city should build wonders! */
         continue;
       }
Index: ai/aidata.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aidata.c,v
retrieving revision 1.53
diff -u -r1.53 aidata.c
--- ai/aidata.c 5 Mar 2005 13:37:04 -0000       1.53
+++ ai/aidata.c 6 Mar 2005 00:02:49 -0000
@@ -450,10 +450,6 @@
     ai_data_phase_done(pplayer);
     ai_data_phase_init(pplayer, FALSE);
   }
-  if (ai->wonder_city && ai->wonder_city->owner != pplayer->player_no) {
-    /* We lost our wonder city :( */
-    ai->wonder_city = NULL;
-  }
   return ai;
 }
 
@@ -472,7 +468,7 @@
   memset(ai->government_want, 0,
         (game.government_count + 1) * sizeof(*ai->government_want));
 
-  ai->wonder_city = NULL;
+  ai->wonder_city = 0;
   ai->diplomacy.target = NULL;
   ai->diplomacy.strategy = WIN_OPEN;
   ai->diplomacy.timer = 0;
Index: ai/aidata.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aidata.h,v
retrieving revision 1.23
diff -u -r1.23 aidata.h
--- ai/aidata.h 2 Mar 2005 10:43:57 -0000       1.23
+++ ai/aidata.h 6 Mar 2005 00:02:49 -0000
@@ -57,7 +57,7 @@
 BV_DEFINE(bv_id, MAX_NUM_ID);
 struct ai_data {
   /* The Wonder City */
-  struct city *wonder_city;
+  int wonder_city;
 
   /* Precalculated info about city improvements */
   enum ai_improvement_status impr_calc[MAX_NUM_ITEMS];
Index: server/citytools.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/citytools.c,v
retrieving revision 1.306
diff -u -r1.306 citytools.c
--- server/citytools.c  5 Mar 2005 13:37:04 -0000       1.306
+++ server/citytools.c  6 Mar 2005 00:02:49 -0000
@@ -49,7 +49,6 @@
 #include "unittools.h"
 
 #include "aicity.h"
-#include "aidata.h"
 #include "aiunit.h"
 
 #include "citytools.h"
@@ -1035,11 +1034,6 @@
   struct tile *ptile = pcity->tile;
   bool had_palace = is_capital(pcity);
   char *city_name = mystrdup(pcity->name);
-  struct ai_data *ai = ai_data_get(pplayer);
-
-  if (ai->wonder_city == pcity) {
-    ai->wonder_city = NULL;
-  }
 
   built_impr_iterate(pcity, i) {
     city_remove_improvement(pcity, i);

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