From d4fc77207b274035623367e7c37ebbb6ce21a082 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 - Correct assertions - Assign default value for builtin counters - Get rid of city_i from counters_init diff --git a/common/city.c b/common/city.c index a6fccb9241..f3b1b4f835 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" @@ -3341,6 +3342,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; } @@ -3377,6 +3384,10 @@ void destroy_city_virtual(struct city *pcity) free(pcity->cm_parameter); } + if (pcity->counter_values) { + free(pcity->counter_values); + } + if (!is_server()) { unit_list_destroy(pcity->client.info_units_supported); unit_list_destroy(pcity->client.info_units_present); diff --git a/common/city.h b/common/city.h index d61b60d694..19076ebfa8 100644 --- a/common/city.h +++ b/common/city.h @@ -387,6 +387,8 @@ struct city { struct unit_list *units_supported; + int *counter_values; + int history; /* Cumulative culture */ struct worker_task_list *task_reqs; diff --git a/common/counters.c b/common/counters.c index 6037a8fc04..5bbe8076e1 100644 --- a/common/counters.c +++ b/common/counters.c @@ -29,6 +29,7 @@ static struct counter counters[MAX_COUNTERS] = }; static struct counter *counters_city[MAX_COUNTERS]; +static int number_city_counters; /************************************************************************//** Initialize counters system @@ -36,15 +37,17 @@ static struct counter *counters_city[MAX_COUNTERS]; void counters_init(void) { int i; - int city_i = 0; + + number_city_counters = 0; for (i = 0; i < MAX_COUNTERS; i++) { if (counters[i].type == COUNTER_OWNED) { /* City counter type */ - counters_city[city_i] = &counters[i]; - counters[i].index = city_i++; + counters_city[number_city_counters] = &counters[i]; + counters[i].index = number_city_counters; counters[i].target = CTGT_CITY; + number_city_counters++; } } } @@ -54,6 +57,15 @@ void counters_init(void) ****************************************************************************/ void counters_free(void) { + number_city_counters = 0; +} + +/************************************************************************//** + Return number of city counters. +****************************************************************************/ +int counters_get_city_counters_count(void) +{ + return number_city_counters; } /************************************************************************//** diff --git a/common/counters.h b/common/counters.h index 6224001488..47bfd18ed1 100644 --- a/common/counters.h +++ b/common/counters.h @@ -29,6 +29,9 @@ 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 index; /* index in specific (city/player/world) array */ }; @@ -43,7 +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 } #endif /* __cplusplus */ -- 2.33.1