From e1c6bba2ba8076cee6329b6fe2f061e44e8167a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20Lach?= Date: Wed, 3 Feb 2021 17:26:07 +0100 Subject: [PATCH 5/5] - Change counter_by_index declaration, so it takes counter type as parameter - Counter_index reads index field from given counter - Counter_by_index's behavior are now counter type dependent - Added counter target type and field to counter struct diff --git a/common/counters.c b/common/counters.c index 89cdd27471..124c9cf9fa 100644 --- a/common/counters.c +++ b/common/counters.c @@ -42,6 +42,7 @@ void counters_init(void) /* City counter type */ counters_city[city_i] = &counters[i]; counters[i].index = city_i++; + counters[i].target = CTGT_CITY; } } } @@ -113,13 +114,23 @@ const char *counter_rule_name(struct counter *pcount) int counter_index(struct counter *pcount) { fc_assert_ret_val(NULL != pcount, -1); - return pcount - counters; + + return pcount->index; } /************************************************************************//** Return counter by given index ****************************************************************************/ -struct counter *counter_by_index(int index) +struct counter *counter_by_index(int index, enum counter_target target) { - return &counters[index]; + switch (target) { + + case CTGT_CITY: + + return counters_city[index]; + break; + + default: + fc_assert_exit_msg(false, "Bad counter type inside counter_by_index. Exiting"); + } } diff --git a/common/counters.h b/common/counters.h index 11d2136336..6b2f94b8dc 100644 --- a/common/counters.h +++ b/common/counters.h @@ -19,6 +19,8 @@ extern "C" { enum counter_type { COUNTER_OWNED = 0, COUNTER_COUNT }; +enum counter_target { CTGT_CITY }; + /* Space for one counter of each type */ #define MAX_COUNTERS COUNTER_COUNT @@ -26,6 +28,7 @@ struct counter { const char *rule_name; enum counter_type type; + enum counter_target target; int id; /* id in global counters array */ int index; /* index in specific (city/player/world) array */ }; @@ -40,7 +43,7 @@ struct counter *counter_by_rule_name(const char *name); const char *counter_rule_name(struct counter *pcount); int counter_index(struct counter *pcount); -struct counter *counter_by_index(int index); +struct counter *counter_by_index(int index, enum counter_target target); #ifdef __cplusplus } -- 2.30.0