From 603526458206dde67ffabccd92d117209f778e68 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Tue, 30 Jan 2024 19:35:04 +0200
Subject: [PATCH 8/8] AI: Stop attempts to recover Helicopter HP in the open

They would be losing HP instead.

See RM #207

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 ai/default/aiunit.c | 27 ++++++++++++++-------------
 common/unit.c       | 12 ++++++++++++
 common/unit.h       |  1 +
 3 files changed, 27 insertions(+), 13 deletions(-)

diff --git a/ai/default/aiunit.c b/ai/default/aiunit.c
index 0f0e30cfe9..9fdbe753e0 100644
--- a/ai/default/aiunit.c
+++ b/ai/default/aiunit.c
@@ -2406,24 +2406,24 @@ static void dai_manage_hitpoint_recovery(struct ai_type *ait,
 
   CHECK_UNIT(punit);
 
-  if (pcity) {
-    /* rest in city until the hitpoints are recovered, but attempt
-       to protect city from attack (and be opportunistic too)*/
+  if (pcity != NULL) {
+    /* Rest in the city until the hitpoints are recovered, but attempt
+     * to protect city from attack (and be opportunistic too)*/
     if (dai_military_rampage(punit, RAMPAGE_ANYTHING, 
                              RAMPAGE_FREE_CITY_OR_BETTER)) {
       UNIT_LOG(LOGLEVEL_RECOVERY, punit, "recovering hit points.");
     } else {
-      return; /* we died heroically defending our city */
+      return; /* We died heroically defending our city */
     }
   } else {
-    /* goto to nearest city to recover hit points */
-    /* just before, check to see if we can occupy an undefended enemy city */
+    /* Goto to nearest city to recover hit points */
+    /* Just before, check to see if we can occupy an undefended enemy city */
     if (!dai_military_rampage(punit, RAMPAGE_FREE_CITY_OR_BETTER, 
                               RAMPAGE_FREE_CITY_OR_BETTER)) { 
-      return; /* oops, we died */
+      return; /* Oops, we died */
     }
 
-    /* find city to stay and go there */
+    /* Find a city to stay and go there */
     safe = find_nearest_safe_city(punit);
     if (safe) {
       UNIT_LOG(LOGLEVEL_RECOVERY, punit, "going to %s to recover",
@@ -2433,7 +2433,7 @@ static void dai_manage_hitpoint_recovery(struct ai_type *ait,
         return;
       }
     } else {
-      /* oops */
+      /* Oops */
       UNIT_LOG(LOGLEVEL_RECOVERY, punit, "didn't find a city to recover in!");
       dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL);
       dai_military_attack(ait, pplayer, punit);
@@ -2441,14 +2441,15 @@ static void dai_manage_hitpoint_recovery(struct ai_type *ait,
     }
   }
 
-  /* is the unit still damaged? if true recover hit points, if not idle */
-  if (punit->hp == punittype->hp) {
-    /* we are ready to go out and kick ass again */
+  /* Is the unit still damaged? If true, and could recover hit points, do so.
+   * Don't wait if would be losing hitpoints that way! */
+  if (punit->hp == punittype->hp || !is_gaining_hp(punit)) {
+    /* We are ready to go out and kick ass again */
     UNIT_LOG(LOGLEVEL_RECOVERY, punit, "ready to kick ass again!");
     dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL);  
     return;
   } else {
-    def_ai_unit_data(punit, ait)->done = TRUE; /* sit tight */
+    def_ai_unit_data(punit, ait)->done = TRUE; /* Sit tight */
   }
 }
 
diff --git a/common/unit.c b/common/unit.c
index a218a1ff02..db2d51725f 100644
--- a/common/unit.c
+++ b/common/unit.c
@@ -2155,6 +2155,18 @@ bool is_losing_hp(const struct unit *punit)
        utype_class(punittype)->hp_loss_pct / 100);
 }
 
+/**************************************************************************
+  Does unit gain hitpoints each turn?
+**************************************************************************/
+bool is_gaining_hp(const struct unit *punit)
+{
+  const struct unit_type *punittype = unit_type_get(punit);
+
+  return get_unit_bonus(punit, EFT_UNIT_RECOVER)
+    > (punittype->hp *
+       utype_class(punittype)->hp_loss_pct / 100);
+}
+
 /**************************************************************************
   Does unit lose hitpoints each turn?
 **************************************************************************/
diff --git a/common/unit.h b/common/unit.h
index 8f7cc68210..7376cc1793 100644
--- a/common/unit.h
+++ b/common/unit.h
@@ -454,6 +454,7 @@ int hp_gain_coord(struct unit *punit);
 bool is_losing_hp(const struct unit *punit);
 bool unit_type_is_losing_hp(const struct player *pplayer,
                             const struct unit_type *punittype);
+bool is_gaining_hp(const struct unit *punit);
 
 bool unit_is_alive(int id);
 
-- 
2.43.0

