From dab0c782a56cf6c758bdf6bc2690e7a2f6ffcf22 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C5=82awomir=20Lach?= <slawek@lach.art.pl>
Date: Mon, 1 Aug 2022 12:18:03 +0200
Subject: [PATCH 1/2] =?UTF-8?q?!OSDN=2041122:=20S=C5=82awomir=20Lach=20<sl?=
 =?UTF-8?q?awek@lach.art.pl>?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Multiple counter of the same type are supported
---
 common/counters.c             | 18 +++++++++++++++---
 common/counters.h             |  2 +-
 common/networking/packets.def |  1 +
 server/ruleset.c              |  6 +++++-
 4 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/common/counters.c b/common/counters.c
index 5c50bcff7e..26f38d972c 100644
--- a/common/counters.c
+++ b/common/counters.c
@@ -22,9 +22,14 @@
 #include "fcintl.h"
 
 #include "counters.h"
+#include "game.h"
 
+/* All (of each type) counters array + related data.
+ * The number of length of data in this array
+ * game kept in game control packet/struct */
 static struct counter counters[MAX_COUNTERS];
 
+/* City counters array + related data */
 static struct counter *counters_city[MAX_COUNTERS];
 static int number_city_counters;
 
@@ -33,6 +38,7 @@ static int number_city_counters;
 ****************************************************************************/
 void counters_init(void)
 {
+  game.control.num_counters = 0;
   number_city_counters = 0;
 }
 
@@ -42,6 +48,11 @@ void counters_init(void)
 ****************************************************************************/
 void counters_free(void)
 {
+  /* TODO: Is freeing translated name needed? If is, write the right
+   * code here
+   */
+
+  game.control.num_counters = 0;
   number_city_counters = 0;
 }
 
@@ -58,7 +69,7 @@ int counters_get_city_counters_count(void)
 ****************************************************************************/
 struct counter *counter_by_id(int id)
 {
-  fc_assert_ret_val(id < MAX_COUNTERS, NULL);
+  fc_assert_ret_val(id < game.control.num_counters, NULL);
 
   return &counters[id];
 }
@@ -71,6 +82,7 @@ struct counter *counter_by_id(int id)
 void attach_city_counter(struct counter *counter)
 {
   counters_city[number_city_counters] = counter;
+  counters_city[number_city_counters]->index = number_city_counters;
   number_city_counters++;
 }
 
@@ -93,7 +105,7 @@ struct counter *counter_by_rule_name(const char *name)
   fc_assert_ret_val(NULL != name, NULL);
   fc_assert_ret_val('\0' != name[0], NULL);
 
-  for (i = 0; i < MAX_COUNTERS; i++)
+  for (i = 0; i < game.control.num_counters; i++)
   {
     if (0 == fc_strcasecmp(name, counter_rule_name(&counters[i])))
     {
@@ -114,7 +126,7 @@ struct counter *counter_by_translated_name(const char *name)
   fc_assert_ret_val(NULL != name, NULL);
   fc_assert_ret_val('\0' != name[0], NULL);
 
-  for (i = 0; i < MAX_COUNTERS; i++)
+  for (i = 0; i < game.control.num_counters; i++)
   {
     if (0 == fc_strcasecmp(name,
                            counter_name_translation(&counters[i])))
diff --git a/common/counters.h b/common/counters.h
index 394c1e1e83..a5ea3f7286 100644
--- a/common/counters.h
+++ b/common/counters.h
@@ -29,7 +29,7 @@ extern "C" {
 
 enum counter_target { CTGT_CITY };
 
-#define MAX_COUNTERS COUNTER_BEHAVIOUR_LAST
+#define MAX_COUNTERS 20
 
 struct counter
 {
diff --git a/common/networking/packets.def b/common/networking/packets.def
index 9370cb2723..bd3c177015 100644
--- a/common/networking/packets.def
+++ b/common/networking/packets.def
@@ -1937,6 +1937,7 @@ PACKET_RULESET_CONTROL = 155; sc, lsend
   STRING version[MAX_LEN_NAME];
   STRING alt_dir[MAX_LEN_NAME];
   UINT16 desc_length;
+  UINT16 num_counters;
 end
 
 PACKET_RULESET_SUMMARY = 251; sc, lsend
diff --git a/server/ruleset.c b/server/ruleset.c
index 8184e506e4..fdb6808d0a 100644
--- a/server/ruleset.c
+++ b/server/ruleset.c
@@ -1432,10 +1432,13 @@ static bool load_game_names(struct section_file *file,
     if (ok) {
       int count_idx;
 
+      game.control.num_counters = nval;
+
       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)));
+        = section_name(section_list_get(sec, count_idx));
 
         if (!ruleset_load_names(&pcount->name, NULL, file, sec_name)) {
           ruleset_error(LOG_ERROR, "\"%s\": Cannot load counters names",
@@ -1445,6 +1448,7 @@ static bool load_game_names(struct section_file *file,
         }
       }
     }
+
     section_list_destroy(sec);
   }
 
-- 
2.37.1

