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: Thu, 6 Jan 2005 07:53:20 -0800
Reply-to: bugs@xxxxxxxxxxx

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

I've committed the logging and obvious bugfixes. The attached patch is the
remainder. Now much easier to read for you, Greg ;)

  - Per

Index: ai/advmilitary.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/advmilitary.c,v
retrieving revision 1.183
diff -u -r1.183 advmilitary.c
--- ai/advmilitary.c    6 Jan 2005 15:44:30 -0000       1.183
+++ ai/advmilitary.c    6 Jan 2005 15:49:52 -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,16 @@
   
   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);
+      Tech_Type_id tech_req = 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 +709,9 @@
           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 (tech_req != A_LAST
+                 && get_invention(pplayer, tech_req) == TECH_REACHABLE
+                 && 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/aihand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aihand.c,v
retrieving revision 1.98
diff -u -r1.98 aihand.c
--- ai/aihand.c 6 Jan 2005 15:44:30 -0000       1.98
+++ ai/aihand.c 6 Jan 2005 15:49:52 -0000
@@ -351,11 +351,19 @@
   /* Crank up tech want */
   if (get_invention(pplayer, ai->goal.govt.req) == TECH_KNOWN) {
     return; /* already got it! */
+  } else if (ai->goal.govt.val > 0) {
+    /* We have few cities in the beginning, compensate for this to ensure
+     * that we are sufficiently forward-looking. */
+    int want = MAX(ai->goal.govt.val, 100);
+
+    if (pplayer->government == game.default_government) {
+      want += 25 * game.turn;
+    }
+    pplayer->ai.tech_want[ai->goal.govt.req] += want;
+    TECH_LOG(LOG_DEBUG, pplayer, ai->goal.govt.req, "+ %d for %s in "
+             "ai_manage_government", want,
+             get_government_name(ai->goal.govt.idx));
   }
-  pplayer->ai.tech_want[ai->goal.govt.req] += ai->goal.govt.val;
-  TECH_LOG(LOG_DEBUG, pplayer, ai->goal.govt.req, "+ %d for %s in "
-           "ai_manage_government", ai->goal.govt.val,
-           get_government_name(ai->goal.govt.idx));
 }
 
 /**************************************************************************
Index: ai/aitech.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aitech.c,v
retrieving revision 1.49
diff -u -r1.49 aitech.c
--- ai/aitech.c 6 Jan 2005 15:44:30 -0000       1.49
+++ ai/aitech.c 6 Jan 2005 15:49:52 -0000
@@ -39,67 +39,6 @@
 };
 
 /**************************************************************************
-  Returns tech corresponding to players wonder goal from nations[],
-  if it makes sense, and wonder is not already built and not obsolete.
-  Otherwise returns A_UNSET.
-**************************************************************************/
-static Tech_Type_id get_wonder_tech(struct player *plr)
-{
-  Impr_Type_id building = get_nation_by_plr(plr)->goals.wonder;
-  
-  if (improvement_exists(building)
-      && is_great_wonder(building)
-      && !great_wonder_was_built(building)
-      && !improvement_obsolete(plr, building)) {
-    Tech_Type_id tech = improvement_types[building].tech_req;
-
-    if (tech_is_available(plr, tech) 
-        && get_invention(plr, tech) != TECH_KNOWN) {
-      return tech;
-    }
-  }
-  return A_UNSET;
-}
-
-/**************************************************************************
-  Puts into choice the closest (in terms of unknown techs needed) of
-    - national tech goals,
-    - requirements for the national wonder goal
-**************************************************************************/
-static Tech_Type_id ai_next_tech_goal_default(struct player *pplayer)
-{
-  struct nation_type *prace = get_nation_by_plr(pplayer);
-  int bestdist = A_LAST + 1;
-  int dist, i;
-  Tech_Type_id goal = A_UNSET;
-  Tech_Type_id tech;
-
-  /* Find the closest of the national tech goals */
-  for (i = 0 ; i < MAX_NUM_TECH_GOALS; i++) {
-    Tech_Type_id j = prace->goals.tech[i];
-    if (!tech_is_available(pplayer, j) 
-        || get_invention(pplayer, j) == TECH_KNOWN) {
-      continue;
-    }
-    dist = num_unknown_techs_for_goal(pplayer, j);
-    if (dist < bestdist) { 
-      bestdist = dist;
-      goal = j;
-      break; /* remove this to restore old functionality -- Syela */
-    }
-  } 
-
-  /* Find the requirement for the national wonder */
-  tech = get_wonder_tech(pplayer);
-  if (tech != A_UNSET 
-      && num_unknown_techs_for_goal(pplayer, tech) < bestdist) {
-    goal = tech;
-  }
-
-  return goal;
-}
-
-/**************************************************************************
   Massage the numbers provided to us by ai.tech_want into unrecognizable 
   pulp.
 
@@ -215,64 +154,6 @@
 }
 
 /**************************************************************************
-  Choose our next tech goal.  Called by the server when a new tech is 
-  discovered to determine new goal and, from it, the new tech to be 
-  researched, which is quite stupid since ai_manage_tech sets a goal in 
-  ai.tech_goal and we should either respect it or not bother doing it.
-
-  TODO: Kill this function.
-**************************************************************************/
-void ai_next_tech_goal(struct player *pplayer)
-{
-  struct ai_tech_choice goal_choice = {0, 0, 0};
-
-  ai_select_tech(pplayer, NULL, &goal_choice);
-
-  if (goal_choice.want == 0) {
-    goal_choice.choice = ai_next_tech_goal_default(pplayer);
-  }
-  if (goal_choice.choice != A_UNSET) {
-    pplayer->ai.tech_goal = goal_choice.choice;
-    freelog(LOG_DEBUG, "next_tech_goal for %s is set to %s",
-           pplayer->name, get_tech_name(pplayer, goal_choice.choice));
-  }
-}
-
-/**************************************************************************
-  Use AI tech hints provided in governments.ruleset to up corresponding 
-  tech wants.
-
-  TODO: The hints structure is too complicated, simplify.
-**************************************************************************/
-static void ai_use_gov_tech_hint(struct player *pplayer)
-{
-  int i;
-
-  for (i = 0; i < MAX_NUM_TECH_LIST; i++) {
-    struct ai_gov_tech_hint *hint = &ai_gov_tech_hints[i];
-    
-    if (hint->tech == A_LAST) {
-      break;
-    }
-    
-    if (get_invention(pplayer, hint->tech) != TECH_KNOWN) {
-      int steps = num_unknown_techs_for_goal(pplayer, hint->tech);
-
-      pplayer->ai.tech_want[hint->tech] += 
-       city_list_size(&pplayer->cities)
-       * (hint->turns_factor * steps + hint->const_factor);
-      if (hint->get_first) {
-       break;
-      }
-    } else {
-      if (hint->done) {
-       break;
-      }
-    }
-  }
-}
-
-/**************************************************************************
   Key AI research function. Disable if we are in a team with human team
   mates in a research pool.
 **************************************************************************/
@@ -291,18 +172,16 @@
     }
   } players_iterate_end;
 
-  ai_use_gov_tech_hint(pplayer);
-
   ai_select_tech(pplayer, &choice, &goal);
   if (choice.choice != pplayer->research.researching) {
     /* changing */
     if ((choice.want - choice.current_want) > penalty &&
        penalty + pplayer->research.bulbs_researched <=
        total_bulbs_required(pplayer)) {
-      freelog(LOG_DEBUG, "%s switching from %s to %s with penalty of %d.",
-             pplayer->name,
-             get_tech_name(pplayer, pplayer->research.researching),
-             get_tech_name(pplayer, choice.choice), penalty);
+      TECH_LOG(LOG_DEBUG, pplayer, choice.choice, "new research, was %s, "
+               "penalty was %d", 
+               get_tech_name(pplayer, pplayer->research.researching),
+               penalty);
       choose_tech(pplayer, choice.choice);
     }
   }
Index: ai/aiunit.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aiunit.c,v
retrieving revision 1.343
diff -u -r1.343 aiunit.c
--- ai/aiunit.c 3 Jan 2005 17:48:23 -0000       1.343
+++ ai/aiunit.c 6 Jan 2005 15:49:53 -0000
@@ -2218,30 +2218,64 @@
 }
 
 /**************************************************************************
- 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;
+
     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 (get_invention(pplayer, itech) == TECH_REACHABLE) {
+      /* See if we want to invent this. */
+      int cost = total_bulbs_required_for_goal(pplayer, itech);
+
+      if (cost < best_cost) {
+        best_tech = itech;
+        best_cost = cost;
+        best_unit = iunit;
+      }
+    } else if (iimpr != B_LAST) {
+      itech = get_improvement_type(iimpr)->tech_req;
+
+      if (get_invention(pplayer, itech) == TECH_REACHABLE) {
+        /* See if we want to invent this. */
+        int cost = total_bulbs_required_for_goal(pplayer, itech);
+
+        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: data/trident/units.png
===================================================================
RCS file: /home/freeciv/CVS/freeciv/data/trident/units.png,v
retrieving revision 1.3
diff -u -r1.3 units.png
Binary files /tmp/cvsF1etcu and units.png differ
Index: server/plrhand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/plrhand.c,v
retrieving revision 1.349
diff -u -r1.349 plrhand.c
--- server/plrhand.c    1 Jan 2005 21:48:10 -0000       1.349
+++ server/plrhand.c    6 Jan 2005 15:49:54 -0000
@@ -550,20 +550,8 @@
 {
   int sub_goal;
 
-  if (plr->ai.control) {
-    ai_next_tech_goal(plr);    /* tech-AI has been changed */
-  }
   sub_goal = get_next_tech(plr, plr->ai.tech_goal);
 
-  if (sub_goal == A_UNSET) {
-    if (plr->ai.control || plr->research.techs_researched == 1) {
-      ai_next_tech_goal(plr);
-      sub_goal = get_next_tech(plr, plr->ai.tech_goal);
-    } else {
-      plr->ai.tech_goal = A_UNSET;     /* clear goal when it is achieved */
-    }
-  }
-
   if (sub_goal != A_UNSET) {
     plr->research.researching = sub_goal;
     return TRUE;

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