From a0c6a3743af1a6acbefecb8285d0a1b8e7e29cf2 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sat, 12 Jul 2025 15:18:23 +0300
Subject: [PATCH 85/85] Load government flags from the ruleset

See RM #1601

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 server/ruleset/ruleload.c | 22 ++++++++++++++++++++++
 tools/ruleutil/rulesave.c | 15 +++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/server/ruleset/ruleload.c b/server/ruleset/ruleload.c
index b63f01a700..108ab5f17e 100644
--- a/server/ruleset/ruleload.c
+++ b/server/ruleset/ruleload.c
@@ -4448,6 +4448,10 @@ static bool load_ruleset_governments(struct section_file *file,
       const char *sec_name = section_name(section_list_get(sec, i));
       struct requirement_vector *reqs
         = lookup_req_list(file, sec_name, "reqs", government_rule_name(g));
+      const char **slist;
+      int j;
+      const char *sval;
+      size_t nval;
 
       if (reqs == NULL) {
         ok = FALSE;
@@ -4479,6 +4483,24 @@ static bool load_ruleset_governments(struct section_file *file,
       sz_strlcpy(g->sound_alt2,
                  secfile_lookup_str_default(file, "-", "%s.sound_alt2", sec_name));
 
+      slist = secfile_lookup_str_vec(file, &nval, "%s.flags", sec_name);
+      BV_CLR_ALL(g->flags);
+      for (j = 0; j < nval; j++) {
+        enum gov_flag_id flag;
+
+        sval = slist[j];
+        flag = gov_flag_id_by_name(sval, fc_strcasecmp);
+        if (!gov_flag_id_is_valid(flag)) {
+          ruleset_error(NULL, LOG_ERROR, "\"%s\" government \"%s\": unknown flag \"%s\".",
+                        filename, government_rule_name(g), sval);
+          ok = FALSE;
+          break;
+        } else {
+          BV_SET(g->flags, flag);
+        }
+      }
+      free(slist);
+
       g->helptext = lookup_strvec(file, sec_name, "helptext");
     } governments_iterate_end;
   }
diff --git a/tools/ruleutil/rulesave.c b/tools/ruleutil/rulesave.c
index d56676c127..7b9eb61a3a 100644
--- a/tools/ruleutil/rulesave.c
+++ b/tools/ruleutil/rulesave.c
@@ -1952,6 +1952,9 @@ static bool save_governments_ruleset(const char *filename, const char *name)
   governments_re_active_iterate(pg) {
     char path[512];
     struct ruler_title *prtitle;
+    const char *flag_names[TF_COUNT];
+    int set_count;
+    int flagi;
 
     fc_snprintf(path, sizeof(path), "government_%d", sect_idx++);
 
@@ -1987,6 +1990,18 @@ static bool save_governments_ruleset(const char *filename, const char *name)
       }
     }
 
+    set_count = 0;
+    for (flagi = 0; flagi < GOVF_USER_FLAG_1 + MAX_NUM_USER_GOVERNMENT_FLAGS; flagi++) {
+      if (BV_ISSET(pg->flags, flagi)) {
+        flag_names[set_count++] = gov_flag_id_name(flagi);
+      }
+    }
+
+    if (set_count > 0) {
+      secfile_insert_str_vec(sfile, flag_names, set_count,
+                             "%s.flags", path);
+    }
+
     save_strvec(sfile, pg->helptext, path, "helptext");
 
   } governments_re_active_iterate_end;
-- 
2.47.2

