From e1416231d013ca49e6a3a3f0c4e3291de01db84c Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 28 Jan 2023 02:57:52 +0200 Subject: [PATCH 19/19] Stop accepting NULL target to terrain_extra_removal_time() This prepares it to future where removal time depends simultaneously on the terrain and the target extra. See osdn #46448 Signed-off-by: Marko Lindqvist --- common/terrain.c | 30 +++++------------------------- common/terrain.h | 3 ++- common/tile.c | 2 ++ common/unit.c | 19 ++++++++++++++++--- common/unit.h | 1 + 5 files changed, 26 insertions(+), 29 deletions(-) diff --git a/common/terrain.c b/common/terrain.c index f931e712b3..355ccbccb3 100644 --- a/common/terrain.c +++ b/common/terrain.c @@ -742,7 +742,7 @@ int terrain_extra_build_time(const struct terrain *pterrain, } /**********************************************************************//** - Time to complete the extra removal activity on the given terrain. + Time to complete the extra removal activity on the given terrain. **************************************************************************/ int terrain_extra_removal_time(const struct terrain *pterrain, enum unit_activity activity, @@ -750,38 +750,17 @@ int terrain_extra_removal_time(const struct terrain *pterrain, { int factor; - if (tgt != NULL && tgt->removal_time != 0) { + if (tgt->removal_time != 0) { /* Extra specific removal time */ return tgt->removal_time; } - if (tgt == NULL) { - factor = 1; - } else { - factor = tgt->removal_time_factor; - } + factor = tgt->removal_time_factor; /* Terrain and activity specific removal time */ switch (activity) { case ACTIVITY_CLEAN: - { - if (tgt == NULL) { - if (pterrain->_retire.clean_pollution_time > 0 - && pterrain->_retire.clean_fallout_time > 0) { - return MIN(pterrain->_retire.clean_pollution_time, - pterrain->_retire.clean_fallout_time) - * factor; - } - - if (pterrain->_retire.clean_pollution_time > 0) { - return pterrain->_retire.clean_pollution_time * factor; - } - - return pterrain->_retire.clean_fallout_time * factor; - } - - return pterrain->extra_removal_times[extra_index(tgt)] * factor; - } + return pterrain->extra_removal_times[extra_index(tgt)] * factor; case ACTIVITY_POLLUTION: return pterrain->_retire.clean_pollution_time * factor; case ACTIVITY_FALLOUT: @@ -790,6 +769,7 @@ int terrain_extra_removal_time(const struct terrain *pterrain, return pterrain->pillage_time * factor; default: fc_assert(FALSE); + return 0; } } diff --git a/common/terrain.h b/common/terrain.h index ca09b6fe88..ff5ada0f43 100644 --- a/common/terrain.h +++ b/common/terrain.h @@ -343,7 +343,8 @@ int terrain_extra_build_time(const struct terrain *pterrain, const struct extra_type *tgt); int terrain_extra_removal_time(const struct terrain *pterrain, enum unit_activity activity, - const struct extra_type *tgt); + const struct extra_type *tgt) + fc__attribute((nonnull (1, 3))); /* Functions to operate on a terrain class. */ const char *terrain_class_name_translation(enum terrain_class tclass); diff --git a/common/tile.c b/common/tile.c index 9eecb44fe8..82e2d7e01d 100644 --- a/common/tile.c +++ b/common/tile.c @@ -423,6 +423,8 @@ int tile_activity_time(enum unit_activity activity, const struct tile *ptile, .extra = tgt }, NULL, EFT_ACTIVITY_TIME); + fc_assert(tgt != NULL || !is_targeted_activity(activity)); + if (eff > 0) { /* Use effect provided value */ return eff * ACTIVITY_FACTOR; diff --git a/common/unit.c b/common/unit.c index c677802ff9..f1b7f8574c 100644 --- a/common/unit.c +++ b/common/unit.c @@ -529,7 +529,7 @@ int get_activity_rate_this_turn(const struct unit *punit) /**********************************************************************//** Return the estimated number of turns for the worker unit to start and - complete the activity at the given location. This assumes no other + complete the activity at the given location. This assumes no other worker units are helping out, and doesn't take account of any work already done by this unit. **************************************************************************/ @@ -541,10 +541,14 @@ int get_turns_for_activity_at(const struct unit *punit, /* FIXME: This is just an approximation since we don't account for * get_activity_rate_this_turn. */ int speed = get_activity_rate(punit); - int points_needed = tile_activity_time(activity, ptile, tgt); + int points_needed; + + fc_assert(tgt != NULL || !is_targeted_activity(activity)); + + points_needed = tile_activity_time(activity, ptile, tgt); if (points_needed >= 0 && speed > 0) { - return (points_needed - 1) / speed + 1; /* round up */ + return (points_needed - 1) / speed + 1; /* Round up */ } else { return FC_INFINITY; } @@ -1551,6 +1555,15 @@ bool is_tile_activity(enum unit_activity activity) || is_terrain_change_activity(activity); } +/**********************************************************************//** + Returns true if given activity requires target +**************************************************************************/ +bool is_targeted_activity(enum unit_activity activity) +{ + return is_build_activity(activity) + || is_clean_activity(activity); +} + /**********************************************************************//** Create a virtual unit skeleton. pcity can be NULL, but then you need to set tile and homecity yourself. diff --git a/common/unit.h b/common/unit.h index e5aa5beca4..6b92984793 100644 --- a/common/unit.h +++ b/common/unit.h @@ -460,6 +460,7 @@ bool is_build_activity(enum unit_activity activity); bool is_clean_activity(enum unit_activity activity); bool is_terrain_change_activity(enum unit_activity activity); bool is_tile_activity(enum unit_activity activity); +bool is_targeted_activity(enum unit_activity activity); struct unit *unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, -- 2.39.0