From d6954148a9c302f4c3aa23772ab668a01498f977 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Wed, 22 Jun 2022 20:57:16 +0300 Subject: [PATCH 23/23] citizenshand.c: Avoid unnecessary work in non-debug builds See osdn #44882 Signed-off-by: Marko Lindqvist --- server/citizenshand.c | 35 ++++++++++++++++++++++++++++++++--- server/unittools.c | 6 +++++- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/server/citizenshand.c b/server/citizenshand.c index 1e27681271..0a5fde29d8 100644 --- a/server/citizenshand.c +++ b/server/citizenshand.c @@ -33,7 +33,21 @@ #include "citizenshand.h" +#define LOG_CITIZENS_DEBUG + +#ifdef LOG_CITIZENS_DEBUG #define log_citizens log_debug +#ifdef FREECIV_DEBUG +#define LOG_CITIZENS +#endif /* FREECIV_DEBUG */ +#else /* LOG_CITIZENS_DEBUG */ +#define log_citizens log_verbose +#define LOG_CITIZENS +#endif /* LOG_CITIZENS_DEBUG */ + +#if !defined(FREECIV_NDEBUG) || defined(LOG_CITIZENS) +#define LOG_OR_ASSERT_CITIZENS +#endif /***************************************************************************** Update the nationality according to the city size. New citiens are added @@ -46,6 +60,7 @@ player_name(city_owner(_pcity)), _delta, \ player_name(_pplayer), \ citizens_nation_get(_pcity, _pplayer->slot)); + void citizens_update(struct city *pcity, struct player *plr) { int delta; @@ -97,9 +112,12 @@ void citizens_update(struct city *pcity, struct player *plr) while (count > 0 && delta < 0) { int selected = fc_rand(count); struct player_slot *pslot = city_nations[selected]; - struct player *pplayer = player_slot_get_player(pslot); citizens nationality = citizens_nation_get(pcity, pslot); +#ifdef LOG_OR_ASSERT_CITIZENS + struct player *pplayer = player_slot_get_player(pslot); +#endif + fc_assert_ret(nationality != 0); fc_assert_ret(pplayer != NULL); @@ -118,6 +136,7 @@ void citizens_update(struct city *pcity, struct player *plr) /* Get the minimal reduction = the maximum value of two negative * numbers. */ int diff = MAX(delta, - nationality / 2); + delta -= diff; citizens_nation_add(pcity, pslot, diff); log_citizens_add(pcity, diff, pplayer); @@ -155,6 +174,7 @@ void citizens_print(const struct city *pcity) city_name_get(pcity), city_size_get(pcity), player_name(city_owner(pcity)), citizens_count(pcity)); +#ifdef LOG_OR_ASSERT_CITIZENS citizens_iterate(pcity, pslot, nationality) { struct player *pplayer = player_slot_get_player(pslot); @@ -165,6 +185,8 @@ void citizens_print(const struct city *pcity) player_name(city_owner(pcity)), nationality, player_name(pplayer)); } citizens_iterate_end; +#endif /* LOG_OR_ASSERT_CITIZENS */ + } /***************************************************************************** @@ -185,7 +207,6 @@ static bool citizen_convert_check(struct city *pcity) void citizens_convert(struct city *pcity) { struct player_slot *city_nations[MAX_NUM_PLAYER_SLOTS], *pslot; - struct player *pplayer; int count = 0; fc_assert_ret(pcity); @@ -212,13 +233,17 @@ void citizens_convert(struct city *pcity) /* Now convert one citizens to the city owners nationality. */ pslot = city_nations[fc_rand(count)]; - pplayer = player_slot_get_player(pslot); + +#ifdef LOG_OR_ASSERT_CITIZENS + struct player *pplayer = player_slot_get_player(pslot); +#endif fc_assert_ret(pplayer != NULL); log_citizens("%s (size %d; %s): convert 1 citizen from %s", city_name_get(pcity), city_size_get(pcity), player_name(city_owner(pcity)), player_name(pplayer)); + citizens_nation_move(pcity, pslot, city_owner(pcity)->slot, 1); } @@ -240,7 +265,10 @@ void citizens_convert_conquest(struct city *pcity) * nationality to the nation of the new owner (but at least 1). */ citizens convert = MAX(1, nat * game.info.conquest_convert_pct / 100); + +#ifdef LOG_OR_ASSERT_CITIZENS struct player *pplayer = player_slot_get_player(pslot); +#endif fc_assert_ret(pplayer != NULL); @@ -248,6 +276,7 @@ void citizens_convert_conquest(struct city *pcity) city_name_get(pcity), city_size_get(pcity), player_name(city_owner(pcity)), convert, player_name(pplayer)); + citizens_nation_move(pcity, pslot, conqueror, convert); } citizens_foreign_iterate_end; } diff --git a/server/unittools.c b/server/unittools.c index 9d4171d806..49a6971107 100644 --- a/server/unittools.c +++ b/server/unittools.c @@ -2116,7 +2116,11 @@ struct unit *unit_change_owner(struct unit *punit, struct player *pplayer, /* Be sure to wipe the converted unit! */ /* Old homecity upkeep is updated in process */ - uco_wipe: wipe_unit(punit, reason, NULL); +#ifndef FREECIV_NDEBUG + uco_wipe: +#endif + + wipe_unit(punit, reason, NULL); if (!unit_is_alive(id)) { /* Destroyed by a script */ -- 2.35.1