From 80e1fccf9e2b311ce4f145248f79908412e0ad93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20Lach?= Date: Sun, 28 Mar 2021 12:44:55 +0200 Subject: [PATCH] - Added routines to obtain number of counters for given scope - Added default value for counter - Added routine declaration for obtaining number of city counters - Moving counter part of city structure from server-only part to common part - Initialize city counters by default values - Added routines for manipulating city counter values - Remove unneded code from server --- common/city.c | 29 +++++++++++++++++++++++++++++ common/city.h | 9 +++++++-- common/counters.c | 16 +++++++++++++--- common/counters.h | 12 +++--------- server/citytools.c | 3 --- 5 files changed, 52 insertions(+), 17 deletions(-) diff --git a/common/city.c b/common/city.c index 05d83a118c..40c7350ffc 100644 --- a/common/city.c +++ b/common/city.c @@ -29,6 +29,7 @@ /* common */ #include "ai.h" #include "citizens.h" +#include "counters.h" #include "effects.h" #include "game.h" #include "government.h" @@ -3251,6 +3252,28 @@ void city_styles_free(void) game.control.styles_count = 0; } +/**********************************************************************//** + Set given counter of a City. +**************************************************************************/ +void city_set_counter_value(struct city *pcity, + struct counter *pcount, int value) +{ + fc_assert_exit_msg(pcount != NULL, "Counter pointer is NULL"); + fc_assert_exit_msg(pcity != NULL, "City pointer is NULL"); + pcity->counter_values[pcount->index] = value; +} + +/**********************************************************************//** + Retrieve value of given counter of given City. +**************************************************************************/ +int city_read_counter_value(struct city *pcity, + struct counter *pcount) +{ + fc_assert_exit_msg(pcount != NULL, "Counter pointer is NULL"); + fc_assert_exit_msg(pcity != NULL, "City pointer is NULL"); + return pcity->counter_values[pcount->index]; +} + /**********************************************************************//** Create virtual skeleton for a city. Values are mostly sane defaults. @@ -3320,6 +3343,12 @@ struct city *create_city_virtual(struct player *pplayer, /* collecting_info_units_supported set by fc_calloc(). * collecting_info_units_present set by fc_calloc(). */ } + + pcity->counter_values = fc_malloc(sizeof(int) * counters_get_city_counters_count()); + + for (i = 0; i < counters_get_city_counters_count(); i++) { + pcity->counter_values[i] = counter_by_index(i, CTGT_CITY)->def; + } return pcity; } diff --git a/common/city.h b/common/city.h index f2588df0ba..5d6d03b851 100644 --- a/common/city.h +++ b/common/city.h @@ -380,6 +380,8 @@ struct city { bv_city_options city_options; struct unit_list *units_supported; + + int *counter_values; int history; /* Cumulative culture */ @@ -428,8 +430,6 @@ struct city { struct vision *vision; - struct counters_of_this_vector counters; - } server; struct { @@ -798,6 +798,11 @@ void *city_ai_data(const struct city *pcity, const struct ai_type *ai); void city_set_ai_data(struct city *pcity, const struct ai_type *ai, void *data); +void city_set_counter_value(struct city *pcity, + struct counter *pcount, int value); +int city_read_counter_value(struct city *pcity, + struct counter *pcount); + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/common/counters.c b/common/counters.c index ec7a9ae4bb..77f282493a 100644 --- a/common/counters.c +++ b/common/counters.c @@ -28,6 +28,7 @@ static struct counter counters[MAX_COUNTERS] = static struct counter *counters_city[MAX_COUNTERS]; +static int number_city_counters; /************************************************************************//** Initialize counters system ****************************************************************************/ @@ -44,10 +45,21 @@ void counters_init(void) counters_city[city_i] = &counters[i]; counters[i].index = city_i++; counters[i].target = CTGT_CITY; + number_city_counters++; } } } + +/************************************************************************//** + Return number of city counters. +****************************************************************************/ +int counters_get_city_counters_count(void) +{ + return number_city_counters; +} + + /************************************************************************//** Free resources allocated by counters system ****************************************************************************/ @@ -59,9 +71,7 @@ void counters_free(void) Return counter by given id ****************************************************************************/ struct counter *counter_by_id(int id) -{ - int i; - +{; fc_assert_ret_val(id < MAX_COUNTERS, NULL); return &counters[id]; diff --git a/common/counters.h b/common/counters.h index eb5e498b37..7e068dae8c 100644 --- a/common/counters.h +++ b/common/counters.h @@ -32,19 +32,12 @@ struct counter const char *rule_name; enum counter_type type; enum counter_target target; + int def; /* default value for each entity of given type + * for this counter */ int id; /* id in global counters array */ int index; /* index in specific (city/player/world) array */ }; -struct counter_actor_value -{ - int value; -}; - -#define SPECVEC_TAG counters_of_this -#define SPECVEC_TYPE struct counter_actor_value -#include "specvec.h" - void counters_init(void); void counters_free(void); @@ -57,6 +50,7 @@ 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); +int counters_get_city_counters_count(void); #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/server/citytools.c b/server/citytools.c index 1c37d2b902..c2cdde6095 100644 --- a/server/citytools.c +++ b/server/citytools.c @@ -1609,9 +1609,6 @@ void create_city(struct player *pplayer, struct tile *ptile, send_city_info(city_owner(home), home); } } unit_list_iterate_end; - - - counters_of_this_vector_init(&pcity->server.counters); sanity_check_city(pcity); -- 2.30.2