From 522bd3a40bb99e2a9d2f3e6479928bd817ec94d2 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Thu, 12 Jun 2025 07:01:28 +0300
Subject: [PATCH 93/93] Send government flags to client

See RM #1500

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 client/packhand.c             | 27 +++++++++++++++++++++++++++
 common/generate_packets.py    |  1 +
 common/networking/packets.def | 10 +++++++++-
 server/ruleset/ruleload.c     | 27 +++++++++++++++++++++++++++
 4 files changed, 64 insertions(+), 1 deletion(-)

diff --git a/client/packhand.c b/client/packhand.c
index 29406b5d58..a0db2b55a0 100644
--- a/client/packhand.c
+++ b/client/packhand.c
@@ -4117,6 +4117,7 @@ void handle_ruleset_government(const struct packet_ruleset_government *p)
   sz_strlcpy(gov->sound_str, p->sound_str);
   sz_strlcpy(gov->sound_alt, p->sound_alt);
   sz_strlcpy(gov->sound_alt2, p->sound_alt2);
+  gov->flags = p->flags;
 
   PACKET_STRVEC_EXTRACT(gov->helptext, p->helptext);
 
@@ -4138,6 +4139,32 @@ void handle_ruleset_government_ruler_title
                                     packet->female_title);
 }
 
+/************************************************************************//**
+  Packet ruleset_gov_flag handler.
+****************************************************************************/
+void handle_ruleset_gov_flag(const struct packet_ruleset_gov_flag *p)
+{
+  const char *flagname;
+  const char *helptxt;
+
+  fc_assert_ret_msg(p->id >= GOVF_USER_FLAG_1 && p->id <= GOVF_LAST_USER_FLAG,
+                    "Bad user flag %d.", p->id);
+
+  if (p->name[0] == '\0') {
+    flagname = nullptr;
+  } else {
+    flagname = p->name;
+  }
+
+  if (p->helptxt[0] == '\0') {
+    helptxt = nullptr;
+  } else {
+    helptxt = p->helptxt;
+  }
+
+  set_user_gov_flag_name(p->id, flagname, helptxt);
+}
+
 /************************************************************************//**
   Packet ruleset_terrain handler.
 ****************************************************************************/
diff --git a/common/generate_packets.py b/common/generate_packets.py
index 93e2acddb4..1cfcd09b71 100755
--- a/common/generate_packets.py
+++ b/common/generate_packets.py
@@ -4243,6 +4243,7 @@ def write_common_header(path: "str | Path | None", packets: PacketsDefinition):
 #include "conn_types.h"
 #include "disaster.h"
 #include "events.h"
+#include "government.h"
 #include "player.h"
 #include "tech.h"
 #include "unit.h"
diff --git a/common/networking/packets.def b/common/networking/packets.def
index 4f72d162c5..83ef419d1e 100644
--- a/common/networking/packets.def
+++ b/common/networking/packets.def
@@ -3,7 +3,7 @@
 Max used id:
 ============
 
-Max id: 518
+Max id: 519
 
 Packets are not ordered by their id, but by their category. New packet
 with higher id may get added to existing category, and not to the end of file.
@@ -287,6 +287,7 @@ type BV_CAUSES          = bitvector(bv_causes)
 type BV_RMCAUSES        = bitvector(bv_rmcauses)
 type BV_CITY_OPTIONS    = bitvector(bv_city_options)
 type BV_IMPR_FLAGS      = bitvector(bv_impr_flags)
+type BV_GOV_FLAGS       = bitvector(bv_gov_flags)
 type BV_IMPRS           = bitvector(bv_imprs)
 type BV_PLAYER          = bitvector(bv_player)
 type BV_STARTPOS_NATIONS= bitvector(bv_startpos_nations)
@@ -1567,9 +1568,16 @@ PACKET_RULESET_GOVERNMENT = 145; sc, lsend
   STRING sound_str[MAX_LEN_NAME];
   STRING sound_alt[MAX_LEN_NAME];
   STRING sound_alt2[MAX_LEN_NAME];
+  BV_GOV_FLAGS flags;
   STRVEC helptext;
 end
 
+PACKET_RULESET_GOV_FLAG = 519; sc, lsend
+  UINT8       id;
+  STRING      name[MAX_LEN_NAME];
+  STRING      helptxt[MAX_LEN_PACKET];
+end
+
 PACKET_RULESET_TERRAIN_CONTROL = 146; sc, lsend
   UINT8 ocean_reclaim_requirement_pct; /* # adjacent land tiles for reclaim */
   UINT8 land_channel_requirement_pct; /* # adjacent ocean tiles for channel */
diff --git a/server/ruleset/ruleload.c b/server/ruleset/ruleload.c
index fbbff29bff..a919e00d8b 100644
--- a/server/ruleset/ruleload.c
+++ b/server/ruleset/ruleload.c
@@ -8962,6 +8962,31 @@ static void send_ruleset_governments(struct conn_list *dest)
 {
   struct packet_ruleset_government gov;
   struct packet_ruleset_government_ruler_title title;
+  int i;
+
+  for (i = 0; i < MAX_NUM_USER_GOVERNMENT_FLAGS; i++) {
+    struct packet_ruleset_gov_flag fpacket;
+    const char *flagname;
+    const char *helptxt;
+
+    fpacket.id = i + GOVF_USER_FLAG_1;
+
+    flagname = impr_flag_id_name(i + GOVF_USER_FLAG_1);
+    if (flagname == nullptr) {
+      fpacket.name[0] = '\0';
+    } else {
+      sz_strlcpy(fpacket.name, flagname);
+    }
+
+    helptxt = gov_flag_helptxt(i + GOVF_USER_FLAG_1);
+    if (helptxt == nullptr) {
+      fpacket.helptxt[0] = '\0';
+    } else {
+      sz_strlcpy(fpacket.helptxt, helptxt);
+    }
+
+    lsend_packet_ruleset_gov_flag(dest, &fpacket);
+  }
 
   governments_iterate(g) {
     /* Send one packet_government */
@@ -8970,6 +8995,8 @@ static void send_ruleset_governments(struct conn_list *dest)
     /* Shallow-copy (borrow) requirement vector */
     gov.reqs = g->reqs;
 
+    gov.flags = g->flags;
+
     sz_strlcpy(gov.name, untranslated_name(&g->name));
     sz_strlcpy(gov.rule_name, rule_name_get(&g->name));
     sz_strlcpy(gov.graphic_str, g->graphic_str);
-- 
2.47.2

