From 744678f3b5a7a63539080b5b576873e374313300 Mon Sep 17 00:00:00 2001
From: Ihnatus <ignatus31oct@mail.ru>
Date: Thu, 10 Nov 2022 15:37:52 +0300
Subject: [PATCH] AI: consider logarithmic trade revenue bonus

See OSDN#46049

Signed-off-by: Ihnatus <ignatus31oct@mail.ru>
---
 ai/default/daidomestic.c | 12 ++++++++++--
 common/traderoutes.c     |  2 +-
 common/traderoutes.h     |  1 +
 3 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/ai/default/daidomestic.c b/ai/default/daidomestic.c
index 31b2385e81..31b19ceba5 100644
--- a/ai/default/daidomestic.c
+++ b/ai/default/daidomestic.c
@@ -351,8 +351,16 @@ static void dai_choose_trade_route(struct ai_type *ait, struct city *pcity,
    * duplicated here because the city traded with is imaginary. */
 
   /* We assume that we are creating trade route to city with 75% of
-   * pcitys trade 10 squares away. */
-  income = (10 + 10) * (1.75 * pcity->surplus[O_TRADE]) / 24;
+   * pcitys trade 10 squares away (FIXME: another estimation?). */
+  if (CBS_LOGARITHMIC == game.info.caravan_bonus_style) {
+    int wd = ((100 - game.info.trade_world_rel_pct) * 10
+               + game.info.trade_world_rel_pct
+                 * (10 * 40 / MAX(wld.map.xsize, wld.map.ysize))) / 100;
+
+    income = pow(log(wd + 20 + 1.75 * max_trade_prod(pcity)) * 2, 2);
+  } else /* assume CBS_CLASSIC */ {
+    income = (10 + 10) * (1.75 * pcity->surplus[O_TRADE]) / 24;
+  }
 
   /* A ruleset may use the Trade_Revenue_Bonus effect to reduce the one
    * time bonus if no trade route is established. Make sure it gets the
diff --git a/common/traderoutes.c b/common/traderoutes.c
index ebe74ddfd5..6900e0e3be 100644
--- a/common/traderoutes.c
+++ b/common/traderoutes.c
@@ -440,7 +440,7 @@ static int max_tile_trade(const struct city *pcity)
 /*********************************************************************//**
   Returns the maximum trade production of a city.
 *************************************************************************/
-static int max_trade_prod(const struct city *pcity)
+int max_trade_prod(const struct city *pcity)
 {
   /* Trade tile base */
   int trade_prod = max_tile_trade(pcity);
diff --git a/common/traderoutes.h b/common/traderoutes.h
index bc4690edd9..7d374c2eb0 100644
--- a/common/traderoutes.h
+++ b/common/traderoutes.h
@@ -118,6 +118,7 @@ int trade_base_between_cities(const struct city *pc1, const struct city *pc2);
 int trade_from_route(const struct city *pc1, const struct trade_route *route,
 		     int base);
 int city_num_trade_routes(const struct city *pcity);
+int max_trade_prod(const struct city *pcity);
 int get_caravan_enter_city_trade_bonus(const struct city *pc1,
                                        const struct city *pc2,
                                        struct goods_type *pgood,
-- 
2.34.1