Complete.Org: Mailing Lists: Archives: freeciv-ai: January 2005:
[freeciv-ai] Re: [Freeciv-Dev] (PR#11777) Cleanup of AI tech code
Home

[freeciv-ai] Re: [Freeciv-Dev] (PR#11777) Cleanup of AI tech code

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [freeciv-ai] Re: [Freeciv-Dev] (PR#11777) Cleanup of AI tech code
From: "Per I. Mathisen" <per@xxxxxxxxxxx>
Date: Sat, 22 Jan 2005 13:42:23 -0800
Reply-to: bugs@xxxxxxxxxxx

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

Fixed a bug in the patch I sent. New one attached.

  - Per

Index: ai/aiunit.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aiunit.c,v
retrieving revision 1.344
diff -u -r1.344 aiunit.c
--- ai/aiunit.c 22 Jan 2005 19:45:38 -0000      1.344
+++ ai/aiunit.c 22 Jan 2005 21:42:06 -0000
@@ -2217,30 +2217,66 @@
 }
 
 /**************************************************************************
- Assign tech wants for techs to get better units with given role/flag.
- Returns the best we can build so far, or U_LAST if none.  (dwp)
+  Returns the best we can build so far, or U_LAST if none. "Best" here
+  means last in the unit list as defined in the ruleset.  Assign tech 
+  wants for techs to get better units with given role, but only for the
+  cheapest to research "next" unit up the "chain".
 **************************************************************************/
 Unit_Type_id ai_wants_role_unit(struct player *pplayer, struct city *pcity,
                                 int role, int want)
 {
-  Unit_Type_id iunit;
-  Tech_Type_id itech;
   int i, n;
+  Tech_Type_id best_tech = A_NONE;
+  int best_cost = FC_INFINITY;
+  Unit_Type_id best_unit = U_LAST;
+  Unit_Type_id build_unit = U_LAST;
 
   n = num_role_units(role);
-  for (i=n-1; i>=0; i--) {
-    iunit = get_role_unit(role, i);
+  for (i = n - 1; i >= 0; i--) {
+    Unit_Type_id iunit = get_role_unit(role, i);
+    Tech_Type_id itech = get_unit_type(iunit)->tech_requirement;
+    Impr_Type_id iimpr = get_unit_type(iunit)->impr_requirement;
+    Tech_Type_id iimprtech = get_improvement_type(iimpr)->tech_req;
+
     if (can_build_unit(pcity, iunit)) {
-      return iunit;
-    } else {
-      /* careful; might be unable to build for non-tech reason... */
-      itech = get_unit_type(iunit)->tech_requirement;
-      if (get_invention(pplayer, itech) != TECH_KNOWN) {
-       pplayer->ai.tech_want[itech] += want;
+      build_unit = iunit;
+      break;
+    } else if (can_eventually_build_unit(pcity, iunit)) {
+      int cost = 0;
+
+      if (itech != B_LAST && get_invention(pplayer, itech) == TECH_UNKNOWN) {
+        /* See if we want to invent this. */
+        cost = total_bulbs_required_for_goal(pplayer, itech);
+      }
+      if (iimpr != B_LAST && get_invention(pplayer, iimprtech)) {
+        int imprcost = total_bulbs_required_for_goal(pplayer, iimprtech);
+
+        if (imprcost < cost || cost == 0) {
+          /* If we already have the primary tech (cost==0), or the building's
+           * tech is cheaper, go for the building's required tech. */
+          itech = iimprtech; /* get this first */
+        }
+        cost += imprcost;
+      }
+
+      if (cost < best_cost) {
+        best_tech = itech;
+        best_cost = cost;
+        best_unit = iunit;
       }
     }
   }
-  return U_LAST;
+  if (best_tech != A_NONE) {
+    /* Crank up chosen tech want */
+    if (build_unit != U_LAST) {
+      /* We already have a role unit of this kind */
+      want /= 2;
+    }
+    pplayer->ai.tech_want[best_tech] += want;
+    TECH_LOG(LOG_DEBUG, pplayer, best_tech, "+ %d for %s by role",
+             want, unit_name(best_unit));
+  }
+  return build_unit;
 }
 
 /**************************************************************************
Index: ai/advmilitary.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/advmilitary.c,v
retrieving revision 1.184
diff -u -r1.184 advmilitary.c
--- ai/advmilitary.c    22 Jan 2005 19:45:38 -0000      1.184
+++ ai/advmilitary.c    22 Jan 2005 21:42:06 -0000
@@ -663,7 +663,6 @@
                                   unsigned int danger, struct ai_choice 
*choice)
 {
   bool walls = city_got_citywalls(pcity);
-  bool shore = is_ocean_near_tile(pcity->tile);
   /* Technologies we would like to have. */
   int tech_desire[U_LAST];
   /* Our favourite unit. */
@@ -674,13 +673,15 @@
   
   simple_ai_unit_type_iterate (unit_type) {
       int move_type = unit_types[unit_type].move_type;
-    
-      /* How many technologies away it is? */
-      int tech_dist = num_unknown_techs_for_goal(pplayer,
-                        unit_types[unit_type].tech_requirement);
+      int desire; /* How much we want the unit? */
 
-      /* How much we want the unit? */
-      int desire = ai_unit_defence_desirability(unit_type);
+      /* Only consider proper defenders - otherwise waste CPU and
+       * bump tech want needlessly. */
+      if (!unit_has_role(unit_type, L_DEFEND_GOOD)) {
+        continue;
+      }
+
+      desire = ai_unit_defence_desirability(unit_type);
 
       if (unit_type_flag(unit_type, F_FIELDUNIT)) {
         /* Causes unhappiness even when in defense, so not a good
@@ -707,9 +708,7 @@
           best = desire;
           best_unit_type = unit_type;
         }
-        
-      } else if (tech_dist > 0 && (shore || move_type == LAND_MOVING)
-                 && unit_types[unit_type].tech_requirement != A_LAST) {
+      } else if (can_eventually_build_unit(pcity, unit_type)) {
         /* We first need to develop the tech required by the unit... */
 
         /* Cost (shield equivalent) of gaining these techs. */

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