From c9ae595f54f9a327355879b5d8dd814bc501f19e Mon Sep 17 00:00:00 2001
From: Ihnatus <ignatus31oct@mail.ru>
Date: Sun, 18 Jul 2021 19:57:14 +0300
Subject: [PATCH] AI: Manage caravans that only can enter marketplaces

See OSDN#42518

Signed-off-by: Ihnatus <ignatus31oct@mail.ru>
---
 common/actions.c        |  2 ++
 common/aicore/caravan.c | 14 +++++++++-----
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/common/actions.c b/common/actions.c
index 17f8985cff..51a5f28daf 100644
--- a/common/actions.c
+++ b/common/actions.c
@@ -1570,6 +1570,8 @@ struct act_prob action_speculate_unit_on_city(const action_id act_id,
       return ACTPROB_IMPOSSIBLE;
     }
   } else {
+    /* FIXME: this branch result depends _directly_ on actor's position.
+     * I.e., like, not adjacent, no action. Other branch ignores radius. */
     return action_prob_vs_city_full(actor, actor_home, actor_tile,
                                     act_id, target);
   }
diff --git a/common/aicore/caravan.c b/common/aicore/caravan.c
index 7ad60ca478..41f68a502e 100644
--- a/common/aicore/caravan.c
+++ b/common/aicore/caravan.c
@@ -163,6 +163,7 @@ static void caravan_result_init(struct caravan_result *result,
 
   result->value = 0;
   result->help_wonder = FALSE;
+  /* FIXME: required_boat field is never used. */
   if ((src != NULL) && (dest != NULL)) {
     if (tile_continent(src->tile) != tile_continent(dest->tile)) {
       result->required_boat = TRUE;
@@ -262,7 +263,7 @@ static double windfall_benefit(const struct unit *caravan,
     int bonus = get_caravan_enter_city_trade_bonus(src, dest,
                                                    can_establish);
 
-    /* bonus goes to both sci and gold. */
+    /* bonus goes to both sci and gold FIXME: not always */
     bonus *= 2;
 
     return bonus;
@@ -499,8 +500,6 @@ static bool get_discounted_reward(const struct unit *caravan,
     return FALSE;
   }
 
-  trade = trade_benefit(pplayer_src, src, dest, parameter);
-  windfall = windfall_benefit(caravan, src, dest, parameter);
   if (consider_wonder) {
     wonder = wonder_benefit(caravan, arrival_time, dest, parameter);
     /* we want to aid for wonder building */
@@ -512,6 +511,7 @@ static bool get_discounted_reward(const struct unit *caravan,
   }
 
   if (consider_trade) {
+    trade = trade_benefit(pplayer_src, src, dest, parameter);
     if (parameter->horizon == FC_INFINITY) {
       trade = perpetuity(trade, discount);
     } else {
@@ -523,12 +523,16 @@ static bool get_discounted_reward(const struct unit *caravan,
   }
 
   if (consider_windfall) {
+    windfall = windfall_benefit(caravan, src, dest, parameter);
     windfall = presentvalue(windfall, arrival_time, discount);
   } else {
     windfall = 0.0;
   }
-
-  if (consider_trade && trade + windfall >= wonder) {
+  /* Try founding a trade route
+   * unless the caravan can only enter marketplaces */
+  if ((consider_trade
+       || (consider_windfall && !parameter->consider_trade)
+      ) && trade + windfall >= wonder) {
     result->value = trade + windfall;
     result->help_wonder = FALSE;
   } else if (consider_wonder) {
-- 
2.27.0