Complete.Org: Mailing Lists: Archives: freeciv-ai: May 2004:
[freeciv-ai] (PR#8805) Cleaning up build system
Home

[freeciv-ai] (PR#8805) Cleaning up build system

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [freeciv-ai] (PR#8805) Cleaning up build system
From: "Gregory Berkolaiko" <Gregory.Berkolaiko@xxxxxxxxxxxxx>
Date: Sat, 22 May 2004 22:46:48 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=8805 >

> [glip - Sun May 23 02:23:42 2004]:
> 
> Applied the first patch.  Not closing the ticket hoping that more would
> follow.

Here is another patch, this time cleaning ai_manage_cities,
domestic_advisor_choose_build and touching functions around a bit.

? settle4.gz
? settle5.gz
? stuck.gz
? ai/aisettler.c
? ai/aisettler.h
? common/aicore/citymap.c
? common/aicore/citymap.h
Index: ai/advdomestic.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/advdomestic.c,v
retrieving revision 1.107
diff -u -r1.107 advdomestic.c
--- ai/advdomestic.c    19 May 2004 14:40:15 -0000      1.107
+++ ai/advdomestic.c    23 May 2004 05:38:39 -0000
@@ -847,9 +847,10 @@
  * advanced enough to build caravans, the corresponding tech will be 
  * stimulated.
  ***************************************************************************/
-static void ai_choose_help_wonder(struct player *pplayer, struct city *pcity,
-                           struct ai_choice *choice)
+static void ai_choose_help_wonder(struct city *pcity,
+                                 struct ai_choice *choice)
 {
+  struct player *pplayer = city_owner(pcity);
   /* Continent where the city is --- we won't be aiding any wonder 
    * construction on another continent */
   int continent = map_get_continent(pcity->x, pcity->y);
@@ -936,12 +937,11 @@
   } city_list_iterate_end;
 }
 
-/********************************************************************** 
-This function should assign a value, want and type to choice (that means what
-to build and how important it is).
+/************************************************************************** 
+  This function should fill the supplied choice structure.
 
-If want is 0, this advisor doesn't want anything.
-***********************************************************************/
+  If want is 0, this advisor doesn't want anything.
+***************************************************************************/
 void domestic_advisor_choose_build(struct player *pplayer, struct city *pcity,
                                   struct ai_choice *choice)
 {
@@ -957,21 +957,19 @@
 
   init_choice(choice);
 
-  /* Find out desire for settlers */
-
+  /* Find out desire for settlers (terrain improvers) */
   unit_type = best_role_unit(pcity, F_SETTLERS);
 
   if (unit_type != U_LAST
       && est_food > utype_food_cost(get_unit_type(unit_type), gov)) {
-    /* settler_want calculated in settlers.c called from ai_manage_city() */
+    /* settler_want calculated in settlers.c called from ai_manage_cities() */
     int want = pcity->ai.settler_want;
 
     /* Allowing multiple settlers per city now. I think this is correct.
      * -- Syela */
     
     if (want > 0) {
-      freelog(LOG_DEBUG, "%s (%d, %d) desires settlers with passion %d",
-              pcity->name, pcity->x, pcity->y, want);
+      CITY_LOG(LOG_DEBUG, pcity, "desires settlers with passion %d", want);
       choice->want = want;
       choice->type = CT_NONMIL;
       ai_choose_role_unit(pplayer, pcity, choice, F_SETTLERS, want);
@@ -979,21 +977,22 @@
     } else if (want < 0) {
       /* Negative value is a hack to tell us that we need boats to colonize.
        * abs(want) is desire for the boats. */
+      CITY_LOG(LOG_DEBUG, pcity, "desires settlers with passion %d and asks"
+              " for a boat", want);
       choice->want = 0 - want;
       choice->type = CT_NONMIL;
       choice->choice = unit_type; /* default */
-      ai_choose_ferryboat(pplayer, pcity, choice);
+      ai_choose_role_unit(pplayer, pcity, choice, L_FERRYBOAT, -want);
     }
   }
 
   /* Find out desire for city founders */
   /* Basically, copied from above and adjusted. -- jjm */
-
   unit_type = best_role_unit(pcity, F_CITIES);
 
   if (unit_type != U_LAST
       && est_food >= utype_food_cost(get_unit_type(unit_type), gov)) {
-    /* founder_want calculated in settlers.c, called from ai_manage_city(). */
+    /* founder_want calculated in settlers.c, called from ai_manage_cities(). 
*/
     int want = pcity->ai.founder_want;
 
     if (want > choice->want) {
@@ -1005,42 +1004,34 @@
       
     } else if (want < -choice->want) {
       /* We need boats to colonize! */
+      CITY_LOG(LOG_DEBUG, pcity, "desires settlers with passion %d and asks"
+              " for a boat", want);
       choice->want = 0 - want;
       choice->type = CT_NONMIL;
       choice->choice = unit_type; /* default */
-      ai_choose_ferryboat(pplayer, pcity, choice);
+      ai_choose_role_unit(pplayer, pcity, choice, L_FERRYBOAT, -want);
     }
   }
 
-  /* Consider building caravan-type units to aid wonder construction */  
-  ai_choose_help_wonder(pplayer, pcity, choice);
-
   {
     struct ai_choice cur;
 
     init_choice(&cur);
+    /* Consider building caravan-type units to aid wonder construction */  
+    ai_choose_help_wonder(pcity, &cur);
+    copy_if_better_choice(&cur, choice);
+
+    init_choice(&cur);
+    /* Consider city improvments */
     ai_advisor_choose_building(pcity, &cur);
     copy_if_better_choice(&cur, choice);
   }
 
-  if (choice->want == 0) {
-    /* Oh dear, better think of something! */
-    unit_type = best_role_unit(pcity, F_TRADE_ROUTE);
-    
-    choice->want = 1;
-    if (unit_type != U_LAST) {
-      choice->type = CT_NONMIL;
-      choice->choice = unit_type;
-    } else {
-      /* Capitalization is last resort */
-      choice->type = CT_BUILDING;
-      choice->choice = B_CAPITAL;
-    }
+  if (choice->want >= 200) {
+    /* If we don't do following, we buy caravans in city X when we should be
+     * saving money to buy defenses for city Y. -- Syela */
+    choice->want = 199;
   }
 
-  /* If we don't do following, we buy caravans in city X when we should be
-   * saving money to buy defenses for city Y. -- Syela */
-  if (choice->want >= 200) choice->want = 199;
-
   return;
 }
Index: ai/advmilitary.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/advmilitary.c,v
retrieving revision 1.167
diff -u -r1.167 advmilitary.c
--- ai/advmilitary.c    23 May 2004 02:18:25 -0000      1.167
+++ ai/advmilitary.c    23 May 2004 05:38:45 -0000
@@ -1076,7 +1076,7 @@
     /* We want attacker more that what we have selected before */
     copy_if_better_choice(&best_choice, choice);
     if (go_by_boat && !ferryboat) {
-      ai_choose_ferryboat(pplayer, pcity, choice);
+      ai_choose_role_unit(pplayer, pcity, choice, L_FERRYBOAT, choice->want);
     }
     freelog(LOG_DEBUG, "%s has chosen attacker, %s, want=%d",
             pcity->name, unit_types[choice->choice].name, choice->want);
Index: ai/aicity.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aicity.c,v
retrieving revision 1.152
diff -u -r1.152 aicity.c
--- ai/aicity.c 23 May 2004 02:07:58 -0000      1.152
+++ ai/aicity.c 23 May 2004 05:38:48 -0000
@@ -59,7 +59,7 @@
 #define LOG_BUY LOG_DEBUG
 
 static void resolve_city_emergency(struct player *pplayer, struct city *pcity);
-static void ai_manage_city(struct player *pplayer, struct city *pcity);
+static void ai_sell_obsolete_buildings(struct city *pcity);
 
 /**************************************************************************
   This calculates the usefulness of pcity to us. Note that you can pass
@@ -557,74 +557,78 @@
 }
 
 /**************************************************************************
- cities, build order and worker allocation stuff here..
+  One of the top level AI functions.  It does (by calling other functions):
+  worker allocations,
+  build choices,
+  extra gold spending.
+
+  TODO: Treat ai_gov_tech_hints somewhere else
 **************************************************************************/
 void ai_manage_cities(struct player *pplayer)
 {
   int i;
   pplayer->ai.maxbuycost = 0;
 
-  city_list_iterate(pplayer->cities, pcity)
+  city_list_iterate(pplayer->cities, pcity) {
     if (CITY_EMERGENCY(pcity)) {
       /* Fix critical shortages or unhappiness */
       resolve_city_emergency(pplayer, pcity);
     }
-    ai_manage_city(pplayer, pcity);
-  city_list_iterate_end;
+    auto_arrange_workers(pcity);
+    ai_sell_obsolete_buildings(pcity);
+    sync_cities();
+  } city_list_iterate_end;
 
   ai_manage_buildings(pplayer);
 
-  city_list_iterate(pplayer->cities, pcity)
+  city_list_iterate(pplayer->cities, pcity) {
+    /* Note that this function mungs the seamap, but we don't care */
     military_advisor_choose_build(pplayer, pcity, &pcity->ai.choice);
-/* note that m_a_c_b mungs the seamap, but we don't care */
-    establish_city_distances(pplayer, pcity); /* in advmilitary for warmap */
-/* e_c_d doesn't even look at the seamap */
-/* determines downtown and distance_to_wondercity, which a_c_c_b will need */
+    /* because establish_city_distances doesn't need the seamap
+     * it determines downtown and distance_to_wondercity, 
+     * which ai_city_choose_build will need */
+    establish_city_distances(pplayer, pcity);
+    /* Will record its findings in pcity->settler_want */ 
     contemplate_terrain_improvements(pcity);
-    contemplate_new_city(pcity); /* while we have the warmap handy */
-/* seacost may have been munged if we found a boat, but if we found a boat
-we don't rely on the seamap being current since we will recalculate. -- Syela 
*/
-
-  city_list_iterate_end;
+    /* Will record its findings in pcity->founder_want */ 
+    contemplate_new_city(pcity);
+  } city_list_iterate_end;
 
-  city_list_iterate(pplayer->cities, pcity)
+  city_list_iterate(pplayer->cities, pcity) {
     ai_city_choose_build(pplayer, pcity);
-  city_list_iterate_end;
+  } city_list_iterate_end;
 
   ai_spend_gold(pplayer);
 
   /* use ai_gov_tech_hints: */
   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)
+    
+    if (hint->tech == A_LAST) {
       break;
+    }
+    
     if (get_invention(pplayer, hint->tech) != TECH_KNOWN) {
       pplayer->ai.tech_want[hint->tech] +=
-         city_list_size(&pplayer->cities) * (hint->turns_factor *
-                                             num_unknown_techs_for_goal
-                                             (pplayer,
-                                              hint->tech) +
-                                             hint->const_factor);
-      if (hint->get_first)
+       city_list_size(&pplayer->cities) 
+       * (hint->turns_factor 
+          * num_unknown_techs_for_goal(pplayer, hint->tech) 
+          + hint->const_factor);
+      if (hint->get_first) {
        break;
+      }
     } else {
-      if (hint->done)
+      if (hint->done) {
        break;
+      }
     }
   }
 }
 
 /************************************************************************** 
-...
-**************************************************************************/
-void ai_choose_ferryboat(struct player *pplayer, struct city *pcity, struct 
ai_choice *choice)
-{
-  ai_choose_role_unit(pplayer, pcity, choice, L_FERRYBOAT,  choice->want);
-}
+  Choose the best unit the city can build to defend against attacker v.
 
-/************************************************************************** 
-  ...
+  TODO: Relocate to advmilitary.c
 **************************************************************************/
 Unit_Type_id ai_choose_defender_versus(struct city *pcity, Unit_Type_id v)
 {
@@ -657,7 +661,7 @@
 }
 
 /**************************************************************************
-...
+  Sell an obsolete building if there are any in the city.
 **************************************************************************/
 static void ai_sell_obsolete_buildings(struct city *pcity)
 {
@@ -678,16 +682,6 @@
 }
 
 /**************************************************************************
- cities, build order and worker allocation stuff here..
-**************************************************************************/
-static void ai_manage_city(struct player *pplayer, struct city *pcity)
-{
-  auto_arrange_workers(pcity);
-  ai_sell_obsolete_buildings(pcity);
-  sync_cities();
-}
-
-/**************************************************************************
   This function tries desperately to save a city from going under by
   revolt or starvation of food or resources. We do this by taking
   over resources held by nearby cities and disbanding units.
Index: ai/aicity.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aicity.h,v
retrieving revision 1.21
diff -u -r1.21 aicity.h
--- ai/aicity.h 22 Sep 2003 14:18:46 -0000      1.21
+++ ai/aicity.h 23 May 2004 05:38:49 -0000
@@ -23,7 +23,7 @@
 int ai_eval_calc_city(struct city *pcity, struct ai_data *ai);
 
 void ai_manage_cities(struct player *pplayer);
-void ai_choose_ferryboat(struct player *pplayer, struct city *pcity, struct 
ai_choice *choice);
+
 Unit_Type_id ai_choose_defender_versus(struct city *pcity, Unit_Type_id v);
 
 enum ai_city_task { AICITY_NONE, AICITY_TECH, AICITY_TAX, AICITY_PROD};

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