From 7d19e67ef74a8ea714b6bc4fe834342c146515d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20Lach?= Date: Mon, 24 May 2021 14:16:42 +0200 Subject: [PATCH] - Initial commit for counters - Remove id filed from counter Id field is not necessary, because we can count index in global array by using C language pointer arithmetic - Repair style of switch ... statement - Remove default from counter_type switch (counter_by_index routine) diff --git a/common/counters.c b/common/counters.c index 20d46caee1..ad4bbf01cb 100644 --- a/common/counters.c +++ b/common/counters.c @@ -15,12 +15,14 @@ #include #endif +/* utility */ +#include "fcintl.h" + #include "counters.h" -static struct counter counters[MAX_COUNTERS] = -{ - { "Owned", COUNTER_OWNED, CTGT_CITY, 0, 0 } +static struct counter counters[MAX_COUNTERS] = { + { "Owned", COUNTER_OWNED, CTGT_CITY, 0 } }; static struct counter *counters_city[MAX_COUNTERS]; @@ -34,7 +36,6 @@ void counters_init(void) int city_i = 0; for (i = 0; i < MAX_COUNTERS; i++) { - counters[i].id = i; if (counters[i].type == COUNTER_OWNED) { /* City counter type */ @@ -51,3 +52,74 @@ void counters_init(void) void counters_free(void) { } + +/************************************************************************//** + Return counter by given id +****************************************************************************/ +struct counter *counter_by_id(int id) +{ + fc_assert_ret_val(id < MAX_COUNTERS, NULL); + + return &counters[id]; +} + +/************************************************************************//** + Return id of a given counter +****************************************************************************/ +int counter_id(struct counter *pcount) +{ + fc_assert_ret_val(NULL != pcount, -1); + return pcount - counters; +} + +/************************************************************************//** + Search for counter by rule name + (return matched counter if found or NULL) +****************************************************************************/ +struct counter *counter_by_rule_name(const char *name) +{ + int i; + fc_assert_ret_val(NULL != name, NULL); + fc_assert_ret_val('\0' != name[0], NULL); + + for (i = 0; i < MAX_COUNTERS; i++) { + if (0 == fc_strcasecmp(name, counters[i].rule_name)) { + return &counters[i]; + } + } + + return NULL; +} + +/************************************************************************//** + Return rule name of a given counter +****************************************************************************/ +const char *counter_rule_name(struct counter *pcount) +{ + fc_assert_ret_val(NULL != pcount, NULL); + return pcount->rule_name; +} + +/************************************************************************//** + Return index in global counter's array +****************************************************************************/ +int counter_index(struct counter *pcount) +{ + fc_assert_ret_val(NULL != pcount, -1); + return pcount->index; +} + +/************************************************************************//** + Return counter by given index +****************************************************************************/ +struct counter *counter_by_index(int index, enum counter_target target) +{ + switch (target) { + case CTGT_CITY: + return counters_city[index]; + break; + } + fc_assert_exit_msg(false, "Bad counter type inside counter_by_index." + "Exiting"); + return NULL; +} diff --git a/common/counters.h b/common/counters.h index e4a160751a..83aaf7a1f1 100644 --- a/common/counters.h +++ b/common/counters.h @@ -15,29 +15,35 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ -enum counter_type { COUNTER_OWNED = 0, COUNTER_COUNT }; + enum counter_type { COUNTER_OWNED = 0, COUNTER_COUNT }; -enum counter_target { CTGT_CITY }; + enum counter_target { CTGT_CITY }; /* Space for one counter of each type */ #define MAX_COUNTERS COUNTER_COUNT -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 */ -}; + struct counter { + const char *rule_name; + enum counter_type type; + enum counter_target target; + int index; /* index in specific (city/player/world) array */ + }; -void counters_init(void); -void counters_free(void); + void counters_init(void); + void counters_free(void); + + struct counter *counter_by_id(int id); + int counter_id(struct counter *pcount); + + 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, enum counter_target target); #ifdef __cplusplus } -#endif /* __cplusplus */ - -#endif /* FC__COUNTERS_H */ +#endif /* __cplusplus */ +#endif /* FC__COUNTERS_H */ -- 2.32.0