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

[freeciv-ai] (PR#11777) Cleanup of AI tech code

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: per@xxxxxxxxxxx
Subject: [freeciv-ai] (PR#11777) Cleanup of AI tech code
From: "Gregory Berkolaiko" <Gregory.Berkolaiko@xxxxxxxxxxxxx>
Date: Sun, 23 Jan 2005 14:11:51 -0800
Reply-to: bugs@xxxxxxxxxxx

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

> [per - Sat Jan 22 21:42:21 2005]:
> 
> Fixed a bug in the patch I sent. New one attached.

I fixed a couple of places (not sure if bugs):
1. get_invention != TECH_KNOWN as oppposed to 
get_invention == TECH_UNKNOWN  (because TECH_REACHABLE is also unknown)
and just
get_invention (this didn't make sense -- explain if this was correct)
2. itech should be compare with A_LAST, not B_LAST.

Moved the ai_wants_role_unit to aitech.c and ai_choose_role_unit to
aitools (to all the functions dealing with choice).

Removed some declarations of nonexistent functions from advdomestic.h

I feel the patch is ready.
Index: ai/advdomestic.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/advdomestic.c,v
retrieving revision 1.125
diff -u -r1.125 advdomestic.c
--- ai/advdomestic.c    6 Jan 2005 15:44:30 -0000       1.125
+++ ai/advdomestic.c    23 Jan 2005 21:59:33 -0000
@@ -34,11 +34,13 @@
 #include "aicity.h"
 #include "aidata.h"
 #include "ailog.h"
+#include "aitech.h"
 #include "aitools.h"
 #include "aiunit.h"
 
 #include "advdomestic.h"
 
+
 /***************************************************************************
  * Evaluate the need for units (like caravans) that aid wonder construction.
  * If another city is building wonder and needs help but pplayer is not
Index: ai/advdomestic.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/advdomestic.h,v
retrieving revision 1.8
diff -u -r1.8 advdomestic.h
--- ai/advdomestic.h    13 Sep 2004 15:54:49 -0000      1.8
+++ ai/advdomestic.h    23 Jan 2005 21:59:33 -0000
@@ -17,8 +17,6 @@
 
 struct ai_choice;
 
-void ai_eval_threat_init(struct player *pplayer);
-void ai_eval_threat_done(struct player *pplayer);
 void domestic_advisor_choose_build(struct player *pplayer, struct city *pcity,
                                   struct ai_choice *choice);
 
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    23 Jan 2005 21:59:33 -0000
@@ -37,6 +37,7 @@
 #include "aihand.h"
 #include "aihunt.h"
 #include "ailog.h"
+#include "aitech.h"
 #include "aitools.h"
 #include "aiunit.h"
 
@@ -663,7 +664,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 +674,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? */
+
+      /* Only consider proper defenders - otherwise waste CPU and
+       * bump tech want needlessly. */
+      if (!unit_has_role(unit_type, L_DEFEND_GOOD)) {
+        continue;
+      }
 
-      /* How much we want the unit? */
-      int desire = ai_unit_defence_desirability(unit_type);
+      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 +709,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. */
Index: ai/aitech.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aitech.c,v
retrieving revision 1.51
diff -u -r1.51 aitech.c
--- ai/aitech.c 22 Jan 2005 21:12:10 -0000      1.51
+++ ai/aitech.c 23 Jan 2005 21:59:33 -0000
@@ -197,3 +197,68 @@
     choose_tech_goal(pplayer, goal.choice);
   }
 }
+
+/**************************************************************************
+  Returns the best unit we can build, or U_LAST if none.  "Best" here
+  means last in the unit list as defined in the ruleset.  Assigns 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)
+{
+  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--) {
+    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)) {
+      build_unit = iunit;
+      break;
+    } else if (can_eventually_build_unit(pcity, iunit)) {
+      int cost = 0;
+
+      if (itech != A_LAST && get_invention(pplayer, itech) != TECH_KNOWN) {
+        /* See if we want to invent this. */
+        cost = total_bulbs_required_for_goal(pplayer, itech);
+      }
+      if (iimpr != B_LAST 
+          && get_invention(pplayer, iimprtech) != TECH_KNOWN) {
+        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;
+      }
+    }
+  }
+
+  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/aitech.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aitech.h,v
retrieving revision 1.6
diff -u -r1.6 aitech.h
--- ai/aitech.h 3 Sep 2004 04:22:36 -0000       1.6
+++ ai/aitech.h 23 Jan 2005 21:59:33 -0000
@@ -17,5 +17,7 @@
 
 void ai_manage_tech(struct player *pplayer); 
 void ai_next_tech_goal(struct player *pplayer);
+Unit_Type_id ai_wants_role_unit(struct player *pplayer, struct city *pcity,
+                                int role, int want);
 
 #endif  /* FC__AITECH_H */
Index: ai/aitools.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aitools.c,v
retrieving revision 1.135
diff -u -r1.135 aitools.c
--- ai/aitools.c        22 Jan 2005 19:45:38 -0000      1.135
+++ ai/aitools.c        23 Jan 2005 21:59:33 -0000
@@ -50,6 +50,7 @@
 #include "aidata.h"
 #include "aiferry.h"
 #include "ailog.h"
+#include "aitech.h"
 #include "aiunit.h"
 
 #include "aitools.h"
@@ -685,6 +686,20 @@
 }
 
 /**************************************************************************
+  Calls ai_wants_role_unit to choose the best unit with the given role and 
+  set tech wants.  Sets choice->choice if we can build something.
+**************************************************************************/
+void ai_choose_role_unit(struct player *pplayer, struct city *pcity,
+                        struct ai_choice *choice, int role, int want)
+{
+  Unit_Type_id iunit = ai_wants_role_unit(pplayer, pcity, role, want);
+
+  if (iunit != U_LAST) {
+    choice->choice = iunit;
+  }
+}
+
+/**************************************************************************
   Returns TRUE if pcity's owner is building any wonder in another city on
   the same continent (if so, we may want to build a caravan here).
 **************************************************************************/
Index: ai/aitools.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aitools.h,v
retrieving revision 1.49
diff -u -r1.49 aitools.h
--- ai/aitools.h        24 Nov 2004 19:14:12 -0000      1.49
+++ ai/aitools.h        23 Jan 2005 21:59:33 -0000
@@ -61,6 +61,8 @@
 void init_choice(struct ai_choice *choice);
 void adjust_choice(int value, struct ai_choice *choice);
 void copy_if_better_choice(struct ai_choice *cur, struct ai_choice *best);
+void ai_choose_role_unit(struct player *pplayer, struct city *pcity,
+                         struct ai_choice *choice, int role, int want);
 void ai_advisor_choose_building(struct city *pcity, struct ai_choice *choice);
 bool ai_assess_military_unhappiness(struct city *pcity, struct government *g);
 
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 23 Jan 2005 21:59:34 -0000
@@ -2217,44 +2217,6 @@
 }
 
 /**************************************************************************
- 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)
-**************************************************************************/
-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;
-
-  n = num_role_units(role);
-  for (i=n-1; i>=0; i--) {
-    iunit = get_role_unit(role, i);
-    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;
-      }
-    }
-  }
-  return U_LAST;
-}
-
-/**************************************************************************
- As ai_wants_role_unit, but also set choice->choice if we can build something.
-**************************************************************************/
-void ai_choose_role_unit(struct player *pplayer, struct city *pcity,
-                        struct ai_choice *choice, int role, int want)
-{
-  Unit_Type_id iunit = ai_wants_role_unit(pplayer, pcity, role, want);
-  if (iunit != U_LAST)
-    choice->choice = iunit;
-}
-
-/**************************************************************************
  Whether unit_type test is on the "upgrade path" of unit_type base,
  even if we can't upgrade now.
 **************************************************************************/
Index: ai/aiunit.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aiunit.h,v
retrieving revision 1.56
diff -u -r1.56 aiunit.h
--- ai/aiunit.h 5 Dec 2004 09:08:18 -0000       1.56
+++ ai/aiunit.h 23 Jan 2005 21:59:34 -0000
@@ -77,10 +77,6 @@
 
 bool is_on_unit_upgrade_path(Unit_Type_id test, Unit_Type_id base);
 
-Unit_Type_id ai_wants_role_unit(struct player *pplayer, struct city *pcity,
-                                int role, int want);
-void ai_choose_role_unit(struct player *pplayer, struct city *pcity,
-                         struct ai_choice *choice, int role, int want);
 void update_simple_ai_types(void);
 
 #define simple_ai_unit_type_iterate(m_i)                                      \

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