From 9b72e86cf0e6867143339cc41071481a67aaf542 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20Lach?= Date: Thu, 8 Apr 2021 14:14:03 +0200 Subject: [PATCH] - Repair code style to match freeciv style formating guide (spaces in lines containing only spaces) - 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 diff --git a/common/city.c b/common/city.c index 05d83a118c..5143e69d71 100644 --- a/common/city.c +++ b/common/city.c @@ -22,13 +22,14 @@ /* utility */ #include "distribute.h" #include "fcintl.h" -#include "log.h" +#include "log.h"c #include "mem.h" #include "support.h" /* 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,13 @@ 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..6aac32009b 100644 --- a/common/city.h +++ b/common/city.h @@ -380,6 +380,8 @@ struct city { struct unit_list *units_supported; + int *counter_values; + int history; /* Cumulative culture */ struct worker_task_list *task_reqs; @@ -793,7 +795,10 @@ 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 } #endif /* __cplusplus */ diff --git a/common/counters.c b/common/counters.c index 6a5e2a5159..4152b2a625 100644 --- a/common/counters.c +++ b/common/counters.c @@ -27,6 +27,7 @@ static struct counter counters[MAX_COUNTERS] = static struct counter *counters_city[MAX_COUNTERS]; +static int number_city_counters; /************************************************************************//** Initialize counters system ****************************************************************************/ @@ -43,6 +44,7 @@ void counters_init(void) counters_city[city_i] = &counters[i]; counters[i].index = city_i++; counters[i].target = CTGT_CITY; + number_city_counters++; } } } @@ -60,20 +62,28 @@ void counters_free(void) struct counter *counter_by_id(int id) { int i; - + fc_assert_ret_val(id >= MAX_COUNTERS, NULL); - + for (i = 0; i < MAX_COUNTERS; i++) { - + if (counters[i].id == id) { - + return &counters[i]; } } - + return NULL; } +/************************************************************************//** +Return number of city counters. +****************************************************************************/ +int counters_get_city_counters_count(void) +{ + return number_city_counters; +} + /************************************************************************//** Return id of a given counter ****************************************************************************/ @@ -84,7 +94,7 @@ int counter_id(struct counter *pcount) } /************************************************************************//** - Search for counter by rule name + Search for counter by rule name (return matched counter if found or NULL) ****************************************************************************/ struct counter *counter_by_rule_name(const char *name) @@ -92,15 +102,15 @@ 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; } -- 2.30.2