From 8ee521bd26e7caa5da61831ab2c2deaa6ad303c7 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 - Initialize city counters by default values - Added routines for manipulating city counter values - Added default value for counter - Added routine declaration for obtaining number of city counters - Coding repaired to match freeciv code styling guide diff --git a/common/city.c b/common/city.c index 05d83a118c..acd738e279 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. @@ -3321,6 +3344,12 @@ struct city *create_city_virtual(struct player *pplayer, * 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 ada4115c6a..7cc7e67238 100644 --- a/common/city.h +++ b/common/city.h @@ -23,6 +23,7 @@ extern "C" { /* common */ #include "fc_types.h" +#include "counters.h" #include "name_translation.h" #include "improvement.h" #include "tile.h" @@ -380,6 +381,8 @@ struct city { struct unit_list *units_supported; + int *counter_values; + int history; /* Cumulative culture */ struct worker_task_list *task_reqs; @@ -793,6 +796,11 @@ bool is_free_worked(const struct city *pcity, const struct tile *ptile); 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 } diff --git a/common/counters.c b/common/counters.c index b988e14345..df170f4a71 100644 --- a/common/counters.c +++ b/common/counters.c @@ -27,6 +27,9 @@ static struct counter counters[MAX_COUNTERS] = static struct counter *counters_city[MAX_COUNTERS]; +static int number_city_counters; + + /************************************************************************//** Initialize counters system ****************************************************************************/ @@ -43,6 +46,7 @@ void counters_init(void) counters_city[city_i] = &counters[i]; counters[i].index = city_i++; counters[i].target = CTGT_CITY; + number_city_counters++; } } } @@ -54,6 +58,14 @@ void counters_free(void) { } +/************************************************************************//** + Return number of city counters. +****************************************************************************/ +int counters_get_city_counters_count(void) +{ + return number_city_counters; +} + /************************************************************************//** Return counter by given id ****************************************************************************/ diff --git a/common/counters.h b/common/counters.h index 6b2f94b8dc..60dc00844b 100644 --- a/common/counters.h +++ b/common/counters.h @@ -29,6 +29,8 @@ 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 */ }; @@ -44,6 +46,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 } -- 2.31.1