From c99e723763b1d7e15da6e5216a82beff727e2a8a Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Tue, 14 Apr 2026 00:24:16 +0300
Subject: [PATCH 27/27] AI: Move sanity functionality to new daisanity.[ch]

See RM #1989

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 ai/classic/classicai.c |  1 +
 ai/default/Makefile.am |  2 ++
 ai/default/daiplayer.c | 25 ---------------------
 ai/default/daiplayer.h |  2 --
 ai/default/daisanity.c | 49 ++++++++++++++++++++++++++++++++++++++++++
 ai/default/daisanity.h | 21 ++++++++++++++++++
 ai/tex/texai.c         |  1 +
 meson.build            |  1 +
 8 files changed, 75 insertions(+), 27 deletions(-)
 create mode 100644 ai/default/daisanity.c
 create mode 100644 ai/default/daisanity.h

diff --git a/ai/classic/classicai.c b/ai/classic/classicai.c
index 5923c87055..f539397a6d 100644
--- a/ai/classic/classicai.c
+++ b/ai/classic/classicai.c
@@ -33,6 +33,7 @@
 #include "dailog.h"
 #include "daimilitary.h"
 #include "daiplayer.h"
+#include "daisanity.h"
 #include "daisettler.h"
 #include "daitools.h"
 #include "daiunit.h"
diff --git a/ai/default/Makefile.am b/ai/default/Makefile.am
index 3a90d158e9..d4c5de75db 100644
--- a/ai/default/Makefile.am
+++ b/ai/default/Makefile.am
@@ -47,6 +47,8 @@ libdefaultai_la_SOURCES = \
 		daiparadrop.h	\
 		daiplayer.c	\
 		daiplayer.h	\
+		daisanity.c	\
+		daisanity.h	\
 		daisettler.c	\
 		daisettler.h	\
 		daitech.c	\
diff --git a/ai/default/daiplayer.c b/ai/default/daiplayer.c
index 525325d5c8..86c9ea9ed1 100644
--- a/ai/default/daiplayer.c
+++ b/ai/default/daiplayer.c
@@ -24,7 +24,6 @@
 
 /* server */
 #include "citytools.h"
-#include "sanitycheck.h"
 
 /* ai/default */
 #include "daidata.h"
@@ -187,27 +186,3 @@ void dai_gained_control(struct ai_type *ait, struct player *pplayer)
 
   dai_assess_danger_player(ait, &(wld.map), pplayer);
 }
-
-/**********************************************************************//**
-  Run sanity checking for the AI player.
-**************************************************************************/
-void dai_sanity_check(struct ai_type *ait, struct player *pplayer)
-{
-#ifdef SANITY_CHECKING
-
-#define SANITY_CHECK(check) \
-  fc_assert_full(__FILE__, __FUNCTION__, __FC_LINE__, check, , NOLOGMSG, NOLOGMSG)
-
-  struct player *wt = def_ai_player_data(pplayer, ait)->diplomacy.war_target;
-
-  players_iterate_alive(opponent) {
-    struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, opponent);
-    bool at_war = pplayers_at_war(pplayer, opponent);
-    bool war_target = (wt == opponent);
-
-    /* SANITY_CHECK(adip->countdown < 0 || !at_war); */
-    SANITY_CHECK(adip->countdown >= -1 || at_war || war_target);
-  } players_iterate_alive_end;
-
-#endif /* SANITY_CHECKING */
-}
diff --git a/ai/default/daiplayer.h b/ai/default/daiplayer.h
index b554f971cc..6fae4006e7 100644
--- a/ai/default/daiplayer.h
+++ b/ai/default/daiplayer.h
@@ -39,8 +39,6 @@ void dai_player_copy(struct ai_type *ait,
                      struct player *original, struct player *created);
 void dai_gained_control(struct ai_type *ait, struct player *pplayer);
 
-void dai_sanity_check(struct ai_type *ait, struct player *pplayer);
-
 static inline struct ai_city *def_ai_city_data(const struct city *pcity,
                                                struct ai_type *deftype)
 {
diff --git a/ai/default/daisanity.c b/ai/default/daisanity.c
new file mode 100644
index 0000000000..da3887114d
--- /dev/null
+++ b/ai/default/daisanity.c
@@ -0,0 +1,49 @@
+/***********************************************************************
+ Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+***********************************************************************/
+
+#ifdef HAVE_CONFIG_H
+#include <fc_config.h>
+#endif
+
+/* server */
+#include "sanitycheck.h"
+
+/* ai/default */
+#include "daidata.h"
+#include "daiplayer.h"
+
+#include "daisanity.h"
+
+/**********************************************************************//**
+  Run sanity checking for the AI player.
+**************************************************************************/
+void dai_sanity_check(struct ai_type *ait, struct player *pplayer)
+{
+#ifdef SANITY_CHECKING
+
+#define SANITY_CHECK(check) \
+  fc_assert_full(__FILE__, __FUNCTION__, __FC_LINE__, check, , NOLOGMSG, NOLOGMSG)
+
+  struct player *wt = def_ai_player_data(pplayer, ait)->diplomacy.war_target;
+
+  players_iterate_alive(opponent) {
+    struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, opponent);
+    bool at_war = pplayers_at_war(pplayer, opponent);
+    bool war_target = (wt == opponent);
+
+    /* SANITY_CHECK(adip->countdown < 0 || !at_war); */
+    SANITY_CHECK(adip->countdown >= -1 || at_war || war_target);
+  } players_iterate_alive_end;
+
+#endif /* SANITY_CHECKING */
+}
diff --git a/ai/default/daisanity.h b/ai/default/daisanity.h
new file mode 100644
index 0000000000..89b78280df
--- /dev/null
+++ b/ai/default/daisanity.h
@@ -0,0 +1,21 @@
+/***********************************************************************
+ Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+***********************************************************************/
+#ifndef FC__DAISANITY_H
+#define FC__DAISANITY_H
+
+struct player;
+struct ai_type;
+
+void dai_sanity_check(struct ai_type *ait, struct player *pplayer);
+
+#endif /* FC__DAISANITY_H */
diff --git a/ai/tex/texai.c b/ai/tex/texai.c
index 95d598d666..197f2789c2 100644
--- a/ai/tex/texai.c
+++ b/ai/tex/texai.c
@@ -28,6 +28,7 @@
 #include "dailog.h"
 #include "daimilitary.h"
 #include "daiplayer.h"
+#include "daisanity.h"
 #include "daisettler.h"
 #include "daitools.h"
 #include "daiunit.h"
diff --git a/meson.build b/meson.build
index 63aba5180c..fdfd916426 100644
--- a/meson.build
+++ b/meson.build
@@ -1599,6 +1599,7 @@ ais = static_library('fc_ai',
   'ai/default/daimilitary.c',
   'ai/default/daiparadrop.c',
   'ai/default/daiplayer.c',
+  'ai/default/daisanity.c',
   'ai/default/daisettler.c',
   'ai/default/daitech.c',
   'ai/default/daitools.c',
-- 
2.53.0

