From 868fd367a3b844dd83b53ecc41afe0938ca02309 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C5=82awomir=20Lach?= <slawek@lach.art.pl>
Date: Sun, 17 Apr 2022 10:34:26 +0200
Subject: [PATCH 2/3] =?UTF-8?q?OSDN!41121=20S=C5=82awomir=20Lach=20<slawek?=
 =?UTF-8?q?@lach.art.pl>=20-=20Add=20routines=20to=20load=20counters=20fro?=
 =?UTF-8?q?m=20ruleset=20Basic=20routines=20to=20load=20counter=20definiti?=
 =?UTF-8?q?ons=20from=20ruleset=20are=20added=20by=20this=20patch?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 common/counters.c | 19 +++++-----
 common/counters.h |  1 +
 server/ruleset.c  | 94 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 105 insertions(+), 9 deletions(-)

diff --git a/common/counters.c b/common/counters.c
index ade1ddc755..afc22d960f 100644
--- a/common/counters.c
+++ b/common/counters.c
@@ -40,20 +40,21 @@ void counters_init(void)
 
  number_city_counters = 0;
 
- name_set(&counters[0].name, NULL, N_("?counter:Owned"));
-
  for (i = 0; i < MAX_COUNTERS; i++) {
 
-   if (counters[i].type == CB_CITY_OWNED_TURNS) {
-     /* City counter type */
-     counters_city[number_city_counters] = &counters[i];
-     counters[i].index = number_city_counters;
-     counters[i].target = CTGT_CITY;
-     number_city_counters++;
-   }
+   name_init(&counters[i].name);
  }
 }
 
+/************************************************************************//**
+  Part of counters system initialization - it is used to add counter
+  to related counter-type array
+****************************************************************************/
+void attach_city_counter(struct counter *counter)
+{
+  counters_city[number_city_counters] = counter;
+  ++number_city_counters;
+}
 
 /************************************************************************//**
   Free resources allocated by counters system
diff --git a/common/counters.h b/common/counters.h
index b5271888ca..b6fc96c68d 100644
--- a/common/counters.h
+++ b/common/counters.h
@@ -65,6 +65,7 @@ struct counter *counter_by_translated_name(const char *name);
 int counter_index(struct counter *pcount);
 struct counter *counter_by_index(int index, enum counter_target target);
 int counters_get_city_counters_count(void);
+void attach_city_counter(struct counter *counter);
 
 #define city_counters_iterate(pcount) { \
    int _i_##pcount; \
diff --git a/server/ruleset.c b/server/ruleset.c
index 4de97522a4..96735094d2 100644
--- a/server/ruleset.c
+++ b/server/ruleset.c
@@ -38,6 +38,7 @@
 #include "base.h"
 #include "capability.h"
 #include "city.h"
+#include "counters.h"
 #include "effects.h"
 #include "extras.h"
 #include "fc_types.h"
@@ -108,6 +109,7 @@
 #define ACHIEVEMENT_SECTION_PREFIX "achievement_"
 #define ACTION_ENABLER_SECTION_PREFIX "actionenabler_"
 #define MULTIPLIER_SECTION_PREFIX "multiplier_"
+#define COUNTER_SECTION_PREFIX "counter_"
 
 #define check_name(name) (check_strlen(name, MAX_LEN_NAME, NULL))
 #define check_cityname(name) (check_strlen(name, MAX_LEN_CITYNAME, NULL))
@@ -1410,6 +1412,41 @@ static bool load_game_names(struct section_file *file,
     section_list_destroy(sec);
   }
 
+  if (ok) {
+
+    sec = secfile_sections_by_name_prefix(file, COUNTER_SECTION_PREFIX);
+
+    nval = (NULL != sec ? section_list_size(sec) : 0);
+    if (nval > MAX_COUNTERS) {
+      int num = nval; /* No "size_t" to printf */
+
+      ruleset_error(LOG_ERROR,
+                    "\"%s\": Too many counters (%d, max %d)",
+                    filename, num, MAX_GOODS_TYPES);
+      section_list_destroy(sec);
+      ok = FALSE;
+    } else {
+    }
+
+    if (ok) {
+      int count_idx;
+
+      for (count_idx = 0; count_idx < nval; ++count_idx) {
+        struct counter *pcount = counter_by_id(count_idx);
+        const char *sec_name
+        = section_name(section_list_get(sec, counter_index(pcount)));
+
+        if (!ruleset_load_names(&pcount->name, NULL, file, sec_name)) {
+          ruleset_error(LOG_ERROR, "\"%s\": Cannot load counters names",
+                        filename);
+          ok = FALSE;
+          break;
+        }
+      }
+    }
+    section_list_destroy(sec);
+  }
+
   return ok;
 }
 
@@ -7548,6 +7585,63 @@ static bool load_ruleset_game(struct section_file *file, bool act,
     section_list_destroy(sec);
   }
 
+  if (ok) {
+    sec = secfile_sections_by_name_prefix(file, COUNTER_SECTION_PREFIX);
+
+    if (sec != NULL) {
+      int num = section_list_size(sec);
+      int curr_;
+
+      for (curr_ = 0; curr_ < num; ++curr_) {
+
+        struct counter *pcount = counter_by_id(curr_);
+        const char *sec_name = section_name(section_list_get(sec, curr_));
+        const char *counter_type = secfile_lookup_str_default(file, NULL,
+                                                             "%s.type",
+                                                              sec_name);
+        const char *counter_target = secfile_lookup_str_default(file, NULL,
+                                                              "%s.range",
+                                                              sec_name);
+
+        enum counter_behaviour cb = counter_behaviour_by_name(counter_type,
+                                        fc_strcasecmp);
+        if (!counter_behaviour_is_valid(cb)) {
+          ruleset_error(LOG_ERROR, "\"%s\" unknown counter type \"%s\".",
+                        filename, counter_type);
+          ok = FALSE;
+          break;
+        }
+
+        enum counter_target tg = counter_target_by_name(counter_target,
+                                                        fc_strcasecmp);
+        if (!counter_target_is_valid(tg)) {
+          ruleset_error(LOG_ERROR, "\"%s\" unknown counter range \"%s\".",
+                        filename, counter_target);
+          ok = FALSE;
+          break;
+        }
+
+        if (!ruleset_load_names(&pcount->name, NULL, file, sec_name)) {
+          ruleset_error(LOG_ERROR, "\"%s\": Cannot load counter names",
+                        filename);
+          ok = FALSE;
+          break;
+        }
+
+        pcount->type = cb;
+        pcount->checkpoint = secfile_lookup_int_default(file, 1,
+                                                     "%s.checkpoint",
+                                                     sec_name);
+        pcount->target = CTGT_CITY;
+        pcount->index = curr_;
+        pcount->def = secfile_lookup_int_default(file, 0,
+                                                 "%s.def",
+                                                 sec_name);
+        attach_city_counter(pcount);
+      }
+    }
+  }
+
   /* secfile_check_unused() is not here, but only after also settings section
    * has been loaded. */
 
-- 
2.35.1

