From 069d4c5d8d53fcbefb8a6fa8b9a7642ff508d6dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20Lach?= Date: Wed, 5 Apr 2023 18:28:27 +0200 Subject: [PATCH] =?UTF-8?q?!OSDN=2047292:=20S=C5=82awomir=20Lach=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Unit counters should be handled properly. No counters made yet. It requires f5f35baa735c37dc328b8aace204c7f35735b995. diff --git a/common/counters.c b/common/counters.c index 341e6f519c..24db70ac55 100644 --- a/common/counters.c +++ b/common/counters.c @@ -32,6 +32,7 @@ static struct counter counters[MAX_COUNTERS]; /* City counters array + related data */ static struct counter *counters_city[MAX_COUNTERS]; +static struct counter *counters_unit[MAX_COUNTERS]; static int number_city_counters; static int number_unit_counters; @@ -68,6 +69,14 @@ int counters_get_city_counters_count(void) return number_city_counters; } +/************************************************************************//** + Return number of unit counters. +****************************************************************************/ +int counters_get_unit_counters_count(void) +{ + return number_unit_counters; +} + /************************************************************************//** Return number of counters. ****************************************************************************/ @@ -106,8 +115,9 @@ void attach_city_counter(struct counter *counter) ****************************************************************************/ void attach_unit_counter(struct counter *counter) { - counter->index = number_unit_counters; - counter->ruledit_disabled = FALSE; + counters_unit[number_unit_counters] = counter; + counters_unit[number_unit_counters]->index = number_unit_counters; + counters_unit[number_unit_counters]->ruledit_disabled = FALSE; number_unit_counters++; } @@ -199,7 +209,7 @@ struct counter *counter_by_index(int index, enum counter_target target) case CTGT_CITY: return counters_city[index]; case CTGT_UNIT: - return NULL; + return counters_unit[index]; case COUNTER_TARGET_LAST: return NULL; } diff --git a/common/counters.h b/common/counters.h index bef0115b9b..453922f171 100644 --- a/common/counters.h +++ b/common/counters.h @@ -51,6 +51,7 @@ struct counter *counter_by_translated_name(const char *name); int counter_index(const struct counter *pcount); struct counter *counter_by_index(int index, enum counter_target target); int counters_get_city_counters_count(void); +int counters_get_unit_counters_count(void); int counters_get_count(void); void attach_city_counter(struct counter *counter); void attach_unit_counter(struct counter *counter); @@ -65,6 +66,16 @@ void attach_unit_counter(struct counter *counter); #define city_counters_iterate_end } \ } +#define unit_counters_iterate(pcount) { \ + int _i_##pcount; \ + struct counter *pcount; \ + int _ccounter_count_##pcount = counters_get_unit_counters_count(); \ + for (_i_##pcount = 0; _i_##pcount < _ccounter_count_##pcount; _i_##pcount++) { \ + pcount = counter_by_index(_i_##pcount, CTGT_UNIT); + +#define unit_counters_iterate_end } \ + } + #define counters_re_iterate(pcount) { \ int _i_##pcount; \ struct counter *pcount; \ diff --git a/common/unit.c b/common/unit.c index 64585cb216..e2a04d626a 100644 --- a/common/unit.c +++ b/common/unit.c @@ -28,6 +28,7 @@ #include "actions.h" #include "base.h" #include "city.h" +#include "counters.h" #include "game.h" #include "log.h" #include "map.h" @@ -1562,6 +1563,7 @@ struct unit *unit_virtual_create(struct player *pplayer, struct city *pcity, { /* Make sure that contents of unit structure are correctly initialized, * if you ever allocate it by some other mean than fc_calloc() */ + int counter_count; struct unit *punit = fc_calloc(1, sizeof(*punit)); int max_vet_lvl; @@ -1652,6 +1654,18 @@ struct unit *unit_virtual_create(struct player *pplayer, struct city *pcity, punit->client.act_prob_cache = NULL; } + counter_count = counters_get_unit_counters_count(); + + punit->counter_values = NULL; + if (0 < counter_count) { + + punit->counter_values = malloc(sizeof(*punit->counter_values) * counter_count); + while (counter_count--) { + + punit->counter_values[counter_count] = counter_by_index(counter_count, CTGT_UNIT)->def; + } + } + return punit; } @@ -1690,6 +1704,11 @@ void unit_virtual_destroy(struct unit *punit) } } + if (NULL != punit->counter_values) { + FC_FREE(punit->counter_values); + punit->counter_values = NULL; + } + if (--punit->refcount <= 0) { FC_FREE(punit); } diff --git a/common/unit.h b/common/unit.h index 87362abb74..8a9aab18e8 100644 --- a/common/unit.h +++ b/common/unit.h @@ -151,6 +151,8 @@ struct unit { int veteran; int fuel; + int *counter_values; + struct tile *goto_tile; /* May be NULL. */ enum unit_activity activity; -- 2.40.0