Complete.Org: Mailing Lists: Archives: freeciv-dev: July 2006:
[Freeciv-Dev] (PR#18222) [Patch] AI builds caravans
Home

[Freeciv-Dev] (PR#18222) [Patch] AI builds caravans

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#18222) [Patch] AI builds caravans
From: "Marko Lindqvist" <marko.lindqvist@xxxxxxxxxxx>
Date: Sat, 1 Jul 2006 08:38:28 -0700
Reply-to: bugs@xxxxxxxxxxx

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


  AI can handle creating trade routes with existing caravans, but 
currently it builds caravans only as fallback. Further, it does not want 
to research trade.
  With this patch AI builds some caravans for trade routes. It also adds 
incentive to research Trade.
  Current rules make caravans far too powerful, so patch has comments 
like this:
* This value is adjusted for most interesting gameplay.
* For optimal performance AI should build more caravans, but
* we want it to build less valued buildings too. */

  So far I have tested this with my personal ruleset.
I really want this patch in at some point. For my rules this makes 
difference between AI totally stuck before gunpowder and AI playing 
competitively.
  Testing with default rules is still needed. I presume that under 
default rules this really doesn't change much.


  - ML

diff -Nurd -X.diff_ignore freeciv/ai/advdomestic.c freeciv/ai/advdomestic.c
--- freeciv/ai/advdomestic.c    2006-06-30 20:33:15.671875000 +0300
+++ freeciv/ai/advdomestic.c    2006-07-01 18:19:35.953125000 +0300
@@ -130,6 +130,92 @@
   }
 }
 
+/***************************************************************************
+ * Evaluate the need for units (like caravans) that create trade routes.
+ * If pplayer is not advanced enough to build caravans, the corresponding
+ * tech will be stimulated.
+ ***************************************************************************/
+static void ai_choose_trade_route(struct city *pcity,
+                                 struct ai_choice *choice,
+                                  struct ai_data *ai)
+{
+  struct player *pplayer = city_owner(pcity);
+  struct unit_type *unit_type;
+  int want;
+  int income, bonus;
+  int trade_routes;
+  Continent_id continent = tile_get_continent(pcity->tile);
+  bool dest_city_found = FALSE;
+
+  if (city_list_size(pplayer->cities) < 5) {
+    /* Consider trade routes only if enough destination cities.
+     * This is just a quick check. We make more detailed check below. */
+    return;
+  }
+
+  if (num_role_units(F_TRADE_ROUTE) == 0) {
+    /* No such units available in the ruleset */
+    return;
+  }
+
+  /* Look for proper destination city at the same continent. */
+  city_list_iterate(pplayer->cities, acity) {
+    if (can_cities_trade(pcity, acity) && tile_get_continent(acity->tile) == 
continent) {
+      dest_city_found = TRUE;
+      break;
+    }
+  } city_list_iterate_end;
+
+  if(!dest_city_found) {
+    /* No proper destination city at the same continent. */
+    return;
+  }
+
+  trade_routes = city_num_trade_routes(pcity);
+
+  /* We consider only initial benefit from establishing trade route.
+   * We may actually get only initial benefit if both cities already
+   * have four trade routes, or if there already is route between them. */
+
+  /* We assume that we are creating trade route to city with 50% of
+   * pcitys trade 10 squares away. */
+  income = 10 * (1.5 * pcity->surplus[O_TRADE]) * 3;
+  bonus = get_city_bonus(pcity, EFT_TRADE_REVENUE_BONUS);
+  income = (float)income * pow(2.0, (double)bonus / 1000.0);
+
+  want = income * ai->gold_priority + income * ai->science_priority;
+
+  /* We get this income only once after all.
+   * This value is adjusted for most interesting gameplay.
+   * For optimal performance AI should build more caravans, but
+   * we want it to build less valued buildings too. */
+  want /= 450;
+
+  if (trade_routes == 0) {
+    /* If we have no trade routes at all, we are certainly creating a new one. 
*/
+    want += 12;
+  } else if (trade_routes < NUM_TRADEROUTES) {
+    /* Possibly creating a new traderoute */
+    want += 3;
+  }
+
+  CITY_LOG(LOG_DEBUG, pcity, "want for trade route unit is %d.", want);
+
+  if (want > choice->want) {
+    /* This sets our tech want in cases where we cannot actually build
+     * the unit. */
+    unit_type = ai_wants_role_unit(pplayer, pcity, F_TRADE_ROUTE, want);
+    if (unit_type != NULL) {
+      choice->want = want;
+      choice->type = CT_NONMIL;
+      choice->choice = unit_type->index;
+    } else {
+      CITY_LOG(LOG_DEBUG, pcity,
+               "would but could not build trade route unit, bumped reqs");
+    }
+  }
+}
+
 /************************************************************************** 
   This function should fill the supplied choice structure.
 
@@ -224,9 +310,14 @@
     copy_if_better_choice(&cur, choice);
 
     init_choice(&cur);
-    /* Consider city improvments */
+    /* Consider city improvements */
     ai_advisor_choose_building(pcity, &cur);
     copy_if_better_choice(&cur, choice);
+
+    init_choice(&cur);
+    /* Consider building caravan-type units for trade route */
+    ai_choose_trade_route(pcity, &cur, ai);
+    copy_if_better_choice(&cur, choice);
   }
 
   if (choice->want >= 200) {

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#18222) [Patch] AI builds caravans, Marko Lindqvist <=