From 97ce7427fddda112565a47c82bac835cfd54490f Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sat, 29 May 2021 14:12:48 +0300
Subject: [PATCH 25/25] AI: Fix dai_goldequiv_clause() division by zero for
 tech clause

In a ruleset where tech upkeep is enabled, there was a division
by zero error when player had researched zero techs.

Reported by kontorotsui

See osdn #42409

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 ai/default/daidiplomacy.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/ai/default/daidiplomacy.c b/ai/default/daidiplomacy.c
index 4afefded1a..81e8c51533 100644
--- a/ai/default/daidiplomacy.c
+++ b/ai/default/daidiplomacy.c
@@ -330,8 +330,16 @@ static int dai_goldequiv_clause(struct ai_type *ait,
          * - AI losses tech due to high upkeep. 
          * FIXME: Is there a better way for this? */
         struct research *research = research_get(pplayer);
-        int limit = MAX(1, player_tech_upkeep(pplayer)
-                           / research->techs_researched);
+        int limit;
+
+        /* Make sure there's no division by zero */
+        if (research->techs_researched > 0) {
+          limit = MAX(1, player_tech_upkeep(pplayer)
+                      / research->techs_researched);
+        } else {
+          limit = 1;
+          fc_assert(player_tech_upkeep(pplayer) == 0);
+        }
 
         if (pplayer->server.bulbs_last_turn < limit) {
           worth /= 2;
-- 
2.30.2