From c1c1913106479025fcc17b2fc80e53c421194011 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C5=82awomir=20Lach?= <slawek@lach.art.pl>
Date: Wed, 14 Dec 2022 17:18:15 +0100
Subject: [PATCH] =?UTF-8?q?!OSDN:=20#TICKET:=2046273=20-=20S=C5=82awomir?=
 =?UTF-8?q?=20Lach=20<slawek@lach.art.pl>?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Introduce sharing city counters' index mechanism.

diff --git a/common/counters.c b/common/counters.c
index f17eaa6d9f..41b1ff3b59 100644
--- a/common/counters.c
+++ b/common/counters.c
@@ -33,6 +33,7 @@ static struct counter counters[MAX_COUNTERS];
 /* City counters array + related data */
 static struct counter *counters_city[MAX_COUNTERS];
 static int number_city_counters;
+static int highest_index_city_counters;
 
 /************************************************************************//**
   Initialize counters system
@@ -41,6 +42,7 @@ void counters_init(void)
 {
   game.control.num_counters = 0;
   number_city_counters = 0;
+  highest_index_city_counters = 0;
 }
 
 
@@ -55,6 +57,7 @@ void counters_free(void)
 
   game.control.num_counters = 0;
   number_city_counters = 0;
+  highest_index_city_counters = 0;
 }
 
 /************************************************************************//**
@@ -62,7 +65,7 @@ void counters_free(void)
 ****************************************************************************/
 int counters_get_city_counters_count(void)
 {
-  return number_city_counters;
+  return highest_index_city_counters;
 }
 
 /************************************************************************//**
@@ -75,6 +78,18 @@ struct counter *counter_by_id(int id)
   return &counters[id];
 }
 
+/************************************************************************//**
+  Added for future uses only. Return true if counter of some target
+  and behavior could share index.
+****************************************************************************/
+
+static bool counter_allow_index_sharing(enum counter_behaviour behaviour, enum counter_target target)
+{
+  (void) behaviour;
+  (void) target;
+  
+  return TRUE;
+}
 /************************************************************************//**
   Attaching given counter type to array containing counter type
   related to cities. Counter must be present in array for
@@ -82,9 +97,23 @@ 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;
+  counters_city[number_city_counters]->index = highest_index_city_counters;
   number_city_counters++;
+  
+  if (counter_allow_index_sharing(counter->type, counter->target)) {
+  
+    city_counters_iterate(pcount) {
+      if (counter->type == pcount->type
+      &&  counter->target == pcount->target) {
+        counters_city[number_city_counters-1]->index = pcount->index ;
+        return;
+      }
+    } city_counters_iterate_end;
+  }
+  
+  ++highest_index_city_counters;
 }
 
 /************************************************************************//**
diff --git a/common/counters.h b/common/counters.h
index d5cadf0205..1d032362cb 100644
--- a/common/counters.h
+++ b/common/counters.h
@@ -51,16 +51,31 @@ 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) { \
+#define city_counters_iterate_low_level(pcount) { \
    int _i_##pcount; \
    struct counter *pcount; \
    int _ccounter_count_##pcount = counters_get_city_counters_count(); \
    for (_i_##pcount = 0; _i_##pcount < _ccounter_count_##pcount; _i_##pcount++) { \
       pcount = counter_by_index(_i_##pcount, CTGT_CITY);
 
-#define city_counters_iterate_end } \
+#define city_counters_iterate_low_level_end } \
    }
 
+   
+#define city_counters_iterate(pcount) { \
+   int _i_##pcount; \
+   struct counter *pcount; \
+   int _next_index_##pcount = 0; \
+   int _ccounter_count_##pcount = counters_get_city_counters_count(); \
+   for (_i_##pcount = 0; _i_##pcount < _ccounter_count_##pcount; _i_##pcount++) { \
+      pcount = counter_by_index(_i_##pcount, CTGT_CITY); \
+      if (pcount->index != _next_index_##pcount) { \
+        continue; \
+      } \
+      _next_index_##pcount = _next_index_##pcount + 1;
+
+#define city_counters_iterate_end } \
+   }
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
-- 
2.38.1

