Author: cazfi
Date: Sat Dec  6 23:13:54 2014
New Revision: 27202

URL: http://svn.gna.org/viewcvs/freeciv?rev=27202&view=rev
Log:
Made AI settler_want to consider unit pop_cost in a generic way, and not 
assuming that it's always 1.

See patch #5440

Modified:
    trunk/ai/default/aicity.c

Modified: trunk/ai/default/aicity.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/ai/default/aicity.c?rev=27202&r1=27201&r2=27202&view=diff
==============================================================================
--- trunk/ai/default/aicity.c   (original)
+++ trunk/ai/default/aicity.c   Sat Dec  6 23:13:54 2014
@@ -634,22 +634,35 @@
 **************************************************************************/
 static int unit_foodbox_cost(struct unit *punit)
 {
-  int cost = 30;
+  int pop_cost = unit_type(punit)->pop_cost;
+
+  if (pop_cost <= 0) {
+    return 0;
+  }
 
   if (punit->id == 0) {
     /* It is a virtual unit, so must start in a city... */
     struct city *pcity = tile_city(unit_tile(punit));
+    int size = city_size_get(pcity);
+    int cost = 0;
+    int i;
 
     /* The default is to lose 100%.  The growth bonus reduces this. */
     int foodloss_pct = 100 - get_city_bonus(pcity, EFT_GROWTH_FOOD);
 
     foodloss_pct = CLIP(0, foodloss_pct, 100);
     fc_assert_ret_val(pcity != NULL, -1);
-    cost = city_granary_size(city_size_get(pcity));
+    fc_assert(size >= pop_cost);
+
+    for (i = pop_cost; i > 0 ; i--) {
+      cost += city_granary_size(size--);
+    }
     cost = cost * foodloss_pct / 100;
-  }
-
-  return cost;
+
+    return cost;
+  }
+
+  return 30;
 }
 
 /**************************************************************************
@@ -670,26 +683,30 @@
   struct player *pplayer = city_owner(pcity);
   struct adv_data *adv = adv_data_get(pplayer, NULL);
   struct ai_plr *ai = dai_plr_data_get(ait, pplayer, NULL);
-  struct unit_type *unit_type
+  struct unit_type *utype
            = dai_role_utype_for_terrain_class(pcity, UTYF_SETTLERS, TC_LAND);
   Continent_id place = tile_continent(pcenter);
   struct ai_city *city_data = def_ai_city_data(pcity, ait);
 
   city_data->settler_want = 0; /* Make sure old want does not stay if we don't 
want now */
 
-  if (unit_type == NULL) {
+  if (utype == NULL) {
     log_debug("No UTYF_SETTLERS role unit available");
     return;
   }
 
   /* Create a localized "virtual" unit to do operations with. */
-  virtualunit = unit_virtual_create(pplayer, pcity, unit_type, 0);
+  virtualunit = unit_virtual_create(pplayer, pcity, utype, 0);
   /* Advisors data space not allocated as it's not needed in the
      lifetime of the virtualunit. */
   unit_tile_set(virtualunit, pcenter);
   want = settler_evaluate_improvements(virtualunit, &best_act, &best_target,
                                        &best_tile,
                                        NULL, NULL);
+  if (unit_type(virtualunit)->pop_cost >= city_size_get(pcity)) {
+    /* We don't like disbanding the city as a side effect */
+    return;
+  }
   /* We consider unit_food_upkeep with only half FOOD_WEIGHTING to
    * balance the fact that unit can improve many tiles during its
    * lifetime, and want is calculated for just one of them.
@@ -709,7 +726,7 @@
 
   CITY_LOG(LOG_DEBUG, pcity, "wants %s with want %d to do %s at (%d,%d), "
            "we have %d workers and %d cities on the continent",
-          utype_rule_name(unit_type),
+          utype_rule_name(utype),
           want,
           get_activity_text(best_act),
           TILE_XY(best_tile),


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to