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

 This should fix crash in process_defender_want() when it cannot
select any defender. This patch is for S2_1.


 - ML

diff -Nurd -X.diff_ignore freeciv/ai/advmilitary.c freeciv/ai/advmilitary.c
--- freeciv/ai/advmilitary.c	2007-03-13 17:43:47.000000000 +0200
+++ freeciv/ai/advmilitary.c	2007-03-17 16:53:29.000000000 +0200
@@ -669,7 +669,7 @@
   type in choice. Also sets the technology want for the units we can't 
   build yet.
 **************************************************************************/
-static void process_defender_want(struct player *pplayer, struct city *pcity,
+static bool process_defender_want(struct player *pplayer, struct city *pcity,
                                   unsigned int danger, struct ai_choice *choice)
 {
   /* FIXME: We check if city got defense effect against *some*
@@ -764,9 +764,15 @@
     CITY_LOG(LOG_DEBUG, pcity, "Ooops - we cannot build any defender!");
   }
 
-  if (!walls && best_unit_type->move_type == LAND_MOVING) {
-    best *= pcity->ai.wallvalue;
-    best /= POWER_FACTOR;
+  if (best_unit_type) {
+    if (!walls && best_unit_type->move_type == LAND_MOVING) {
+      best *= pcity->ai.wallvalue;
+      best /= POWER_FACTOR;
+    }
+  } else {
+    best_unit_cost = 100; /* Building impossible is considered costly.
+                           * This should increase want for tech providing
+                           * first defender type. */
   }
 
   if (best <= 0) best = 1; /* Avoid division by zero below. */
@@ -777,19 +783,22 @@
       Tech_type_id tech_req = punittype->tech_requirement;
       /* TODO: Document or fix the algorithm below. I have no idea why
        * it is written this way, and the results seem strange to me. - Per */
-      int desire = tech_desire[punittype->index]
-                   * unit_build_shield_cost(best_unit_type) / best;
-      
+      int desire = tech_desire[punittype->index] * best_unit_cost / best;
+
       pplayer->ai.tech_want[tech_req] += desire;
       TECH_LOG(LOG_DEBUG, pplayer, tech_req, "+ %d for %s to defend %s",
                desire, unit_name(punittype), pcity->name);
     }
   } simple_ai_unit_type_iterate_end;
-  
+
+  if (!best_unit_type) {
+    return FALSE;
+  }
+
   choice->choice = best_unit_type->index;
   choice->want = danger;
   choice->type = CT_DEFENDER;
-  return;
+  return TRUE;
 }
 
 /************************************************************************** 
@@ -1337,21 +1346,25 @@
                choice->want);
     } else if (danger > 0 && num_defenders <= urgency) {
       /* Consider building defensive units units */
-      process_defender_want(pplayer, pcity, danger, choice);
-      if (urgency == 0
-	  && get_unit_type(choice->choice)->defense_strength == 1) {
-	/* FIXME: check other reqs (unit class?) */
-        if (get_city_bonus(pcity, EFT_HP_REGEN) > 0) {
-          /* unlikely */
-          choice->want = MIN(49, danger);
+      if (process_defender_want(pplayer, pcity, danger, choice)) {
+        /* Potential defender found */
+        if (urgency == 0
+            && get_unit_type(choice->choice)->defense_strength == 1) {
+          /* FIXME: check other reqs (unit class?) */
+          if (get_city_bonus(pcity, EFT_HP_REGEN) > 0) {
+            /* unlikely */
+            choice->want = MIN(49, danger);
+          } else {
+            choice->want = MIN(25, danger);
+          }
         } else {
-          choice->want = MIN(25, danger);
+          choice->want = danger;
         }
+        CITY_LOG(LOG_DEBUG, pcity, "m_a_c_d wants %s with desire %d",
+                 get_unit_type(choice->choice)->name, choice->want);
       } else {
-        choice->want = danger;
+        CITY_LOG(LOG_DEBUG, pcity, "m_a_c_d cannot select defender");
       }
-      CITY_LOG(LOG_DEBUG, pcity, "m_a_c_d wants %s with desire %d",
-               get_unit_type(choice->choice)->name, choice->want);
     } else {
       CITY_LOG(LOG_DEBUG, pcity, "m_a_c_d does not want defenders");
     }
_______________________________________________
Freeciv-dev mailing list
Freeciv-dev@gna.org
https://mail.gna.org/listinfo/freeciv-dev

Reply via email to