From 36d418c784668b187fd1e7a75b43b2b647c697a4 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Tue, 26 Sep 2023 22:07:49 +0300 Subject: [PATCH 46/46] Autoworkers: Turn tile action values to adv_want See osdn #48421 Signed-off-by: Marko Lindqvist --- server/advisors/autoworkers.c | 28 +++++---- server/advisors/infracache.c | 114 ++++++++++++++++++---------------- server/advisors/infracache.h | 12 ++-- 3 files changed, 82 insertions(+), 72 deletions(-) diff --git a/server/advisors/autoworkers.c b/server/advisors/autoworkers.c index 99a113ba0d..a875cabcfe 100644 --- a/server/advisors/autoworkers.c +++ b/server/advisors/autoworkers.c @@ -309,10 +309,11 @@ static void consider_worker_action(const struct player *pplayer, enum unit_activity act, struct extra_type *target, adv_want extra, - int new_tile_value, int old_tile_value, + adv_want new_tile_value, + adv_want old_tile_value, bool in_use, int delay, adv_want *best_value, - int *best_old_tile_value, + adv_want *best_old_tile_value, int *best_extra, bool *improve_worked, int *best_delay, @@ -322,7 +323,8 @@ static void consider_worker_action(const struct player *pplayer, struct tile *ptile) { bool improves; - int total_value = 0, base_value = 0; + adv_want total_value = 0; + adv_want base_value = 0; int old_improvement_value; fc_assert(act != ACTIVITY_LAST); @@ -371,7 +373,7 @@ static void consider_worker_action(const struct player *pplayer, } total_value += extra * WORKER_FACTOR; - /* use factor to prevent rounding errors */ + /* Use factor to prevent rounding errors */ total_value = amortize(total_value, delay); if (*improve_worked) { @@ -449,10 +451,10 @@ adv_want worker_evaluate_improvements(const struct civ_map *nmap, struct pf_parameter parameter; struct pf_map *pfm; struct pf_position pos; - int oldv; /* Current value of consideration tile. */ - int best_oldv = 9999; /* oldv of best target so far; compared if - * newv == best_newv; not initialized to zero, - * so that newv = 0 activities are not chosen. */ + adv_want oldv; /* Current value of consideration tile. */ + adv_want best_oldv = 9999; /* oldv of best target so far; compared if + * newv == best_newv; not initialized to zero, + * so that newv = 0 activities are not chosen. */ adv_want best_newv = 0; bool improve_worked = FALSE; int best_extra = 0; @@ -554,8 +556,8 @@ adv_want worker_evaluate_improvements(const struct civ_map *nmap, ptile, parameter.omniscience, ptile, target))) { - int base_value = - adv_city_worker_act_get(pcity, cindex, + adv_want base_value + = adv_city_worker_act_get(pcity, cindex, action_id_get_activity(act)); turns = pos.turn @@ -582,7 +584,7 @@ adv_want worker_evaluate_improvements(const struct civ_map *nmap, extra_type_iterate(pextra) { enum unit_activity act = ACTIVITY_LAST; enum unit_activity eval_act = ACTIVITY_LAST; - int base_value; + adv_want base_value; bool removing = tile_has_extra(ptile, pextra); if (removing) { @@ -728,7 +730,7 @@ adv_want worker_evaluate_improvements(const struct civ_map *nmap, = turns + get_turns_for_activity_at(punit, ACTIVITY_GEN_ROAD, ptile, dep_tgt); - int dep_value + adv_want dep_value = base_value + adv_city_worker_extra_get(pcity, cindex, dep_tgt); @@ -771,7 +773,7 @@ adv_want worker_evaluate_improvements(const struct civ_map *nmap, = turns + get_turns_for_activity_at(punit, eval_dep_act, ptile, pdep); - int dep_value + adv_want dep_value = base_value + adv_city_worker_extra_get(pcity, cindex, pdep); diff --git a/server/advisors/infracache.c b/server/advisors/infracache.c index ae00c1a05c..5e8e1f196b 100644 --- a/server/advisors/infracache.c +++ b/server/advisors/infracache.c @@ -31,34 +31,38 @@ #include "infracache.h" -/* cache activities within the city map */ +/* Cache activities within the city map */ struct worker_activity_cache { - int act[ACTIVITY_LAST]; - int extra[MAX_EXTRA_TYPES]; - int rmextra[MAX_EXTRA_TYPES]; + adv_want act[ACTIVITY_LAST]; + adv_want extra[MAX_EXTRA_TYPES]; + adv_want rmextra[MAX_EXTRA_TYPES]; }; -static int adv_calc_cultivate(const struct city *pcity, const struct tile *ptile); -static int adv_calc_plant(const struct city *pcity, const struct tile *ptile); -static int adv_calc_transform(const struct city *pcity, - const struct tile *ptile); -static int adv_calc_extra(const struct city *pcity, const struct tile *ptile, - const struct extra_type *pextra); -static int adv_calc_rmextra(const struct city *pcity, const struct tile *ptile, - const struct extra_type *pextra); +static adv_want adv_calc_cultivate(const struct city *pcity, + const struct tile *ptile); +static adv_want adv_calc_plant(const struct city *pcity, + const struct tile *ptile); +static adv_want adv_calc_transform(const struct city *pcity, + const struct tile *ptile); +static adv_want adv_calc_extra(const struct city *pcity, + const struct tile *ptile, + const struct extra_type *pextra); +static adv_want adv_calc_rmextra(const struct city *pcity, + const struct tile *ptile, + const struct extra_type *pextra); /**********************************************************************//** Calculate the benefit of cultivating the given tile. - The return value is the goodness of the tile after the cultivating. This - should be compared to the goodness of the tile currently (see - city_tile_value(); note that this depends on the AI's weighting - values). + The return value is the goodness of the tile after the cultivating. + This should be compared to the goodness of the tile currently. + (see city_tile_value(); note that this depends on the AI's weighting + values). **************************************************************************/ -static int adv_calc_cultivate(const struct city *pcity, - const struct tile *ptile) +static adv_want adv_calc_cultivate(const struct city *pcity, + const struct tile *ptile) { - int goodness; + adv_want goodness; struct terrain *old_terrain, *new_terrain; fc_assert_ret_val(ptile != NULL, -1); @@ -90,14 +94,15 @@ static int adv_calc_cultivate(const struct city *pcity, /**********************************************************************//** Calculate the benefit of planting to the given tile. - The return value is the goodness of the tile after the planting. This - should be compared to the goodness of the tile currently (see - city_tile_value(); note that this depends on the AI's weighting - values). + The return value is the goodness of the tile after the planting. + This should be compared to the goodness of the tile currently. + (see city_tile_value(); note that this depends on the AI's weighting + values). **************************************************************************/ -static int adv_calc_plant(const struct city *pcity, const struct tile *ptile) +static adv_want adv_calc_plant(const struct city *pcity, + const struct tile *ptile) { - int goodness; + adv_want goodness; struct terrain *old_terrain, *new_terrain; fc_assert_ret_val(ptile != NULL, -1); @@ -129,15 +134,15 @@ static int adv_calc_plant(const struct city *pcity, const struct tile *ptile) /**********************************************************************//** Calculate the benefit of transforming the given tile. - The return value is the goodness of the tile after the transform. This - should be compared to the goodness of the tile currently (see - city_tile_value(); note that this depends on the AI's weighting - values). + The return value is the goodness of the tile after the transform. + This should be compared to the goodness of the tile currently. + (see city_tile_value(); note that this depends on the AI's weighting + values). **************************************************************************/ -static int adv_calc_transform(const struct city *pcity, - const struct tile *ptile) +static adv_want adv_calc_transform(const struct city *pcity, + const struct tile *ptile) { - int goodness; + adv_want goodness; struct tile *vtile; struct terrain *old_terrain, *new_terrain; @@ -171,18 +176,19 @@ static int adv_calc_transform(const struct city *pcity, Calculate the benefit of building an extra at the given tile. The return value is the goodness of the tile after the extra is built. - This should be compared to the goodness of the tile currently (see - city_tile_value(); note that this depends on the AI's weighting - values). + This should be compared to the goodness of the tile currently. + (see city_tile_value(); note that this depends on the AI's weighting + values). This function does not calculate the benefit of being able to quickly - move units (i.e., of connecting the civilization). See road_bonus() for + move units (i.e., of connecting the civilization). See road_bonus() for that calculation. **************************************************************************/ -static int adv_calc_extra(const struct city *pcity, const struct tile *ptile, - const struct extra_type *pextra) +static adv_want adv_calc_extra(const struct city *pcity, + const struct tile *ptile, + const struct extra_type *pextra) { - int goodness = -1; + adv_want goodness = -1; fc_assert_ret_val(ptile != NULL, -1); @@ -209,14 +215,15 @@ static int adv_calc_extra(const struct city *pcity, const struct tile *ptile, Calculate the benefit of removing an extra from the given tile. The return value is the goodness of the tile after the extra is removed. - This should be compared to the goodness of the tile currently (see - city_tile_value(); note that this depends on the AI's weighting - values). + This should be compared to the goodness of the tile currently. + (see city_tile_value(); note that this depends on the AI's weighting + values). **************************************************************************/ -static int adv_calc_rmextra(const struct city *pcity, const struct tile *ptile, - const struct extra_type *pextra) +static adv_want adv_calc_rmextra(const struct city *pcity, + const struct tile *ptile, + const struct extra_type *pextra) { - int goodness = -1; + adv_want goodness = -1; fc_assert_ret_val(ptile != NULL, -1); @@ -290,16 +297,16 @@ void initialize_infrastructure_cache(struct player *pplayer) FIXME: foodneed and prodneed are always 0. **************************************************************************/ -int city_tile_value(const struct city *pcity, const struct tile *ptile, - int foodneed, int prodneed) +adv_want city_tile_value(const struct city *pcity, const struct tile *ptile, + int foodneed, int prodneed) { int food = city_tile_output_now(pcity, ptile, O_FOOD); int shield = city_tile_output_now(pcity, ptile, O_SHIELD); int trade = city_tile_output_now(pcity, ptile, O_TRADE); - int value = 0; + adv_want value = 0; - /* Each food, trade, and shield gets a certain weighting. We also benefit - * tiles that have at least one of an item - this promotes balance and + /* Each food, trade, and shield gets a certain weighting. We also benefit + * tiles that have at least one of an item - this promotes balance and * also accounts for INC_TILE effects. */ value += food * FOOD_WEIGHTING; if (food > 0) { @@ -322,7 +329,7 @@ int city_tile_value(const struct city *pcity, const struct tile *ptile, city 'pcity'. **************************************************************************/ void adv_city_worker_act_set(struct city *pcity, int city_tile_index, - enum unit_activity act_id, int value) + enum unit_activity act_id, adv_want value) { if (pcity->server.adv->act_cache_radius_sq != city_map_radius_sq_get(pcity)) { @@ -347,8 +354,9 @@ void adv_city_worker_act_set(struct city *pcity, int city_tile_index, Return the value for activity 'doing' on tile 'city_tile_index' of city 'pcity'. **************************************************************************/ -int adv_city_worker_act_get(const struct city *pcity, int city_tile_index, - enum unit_activity act_id) +adv_want adv_city_worker_act_get(const struct city *pcity, + int city_tile_index, + enum unit_activity act_id) { fc_assert_ret_val(NULL != pcity, 0); fc_assert_ret_val(NULL != pcity->server.adv, 0); diff --git a/server/advisors/infracache.h b/server/advisors/infracache.h index 104ae153a5..d0de7bee76 100644 --- a/server/advisors/infracache.h +++ b/server/advisors/infracache.h @@ -24,7 +24,7 @@ struct adv_city { struct worker_activity_cache *act_cache; int act_cache_radius_sq; - /* building desirabilities - easiest to handle them here -- Syela */ + /* Building desirabilities - easiest to handle them here -- Syela */ /* The units of building_want are output * (shields/gold/luxuries) multiplied by a priority * (SHIELD_WEIGHTING, etc or ai->shields_priority, etc) @@ -42,13 +42,13 @@ void initialize_infrastructure_cache(struct player *pplayer); void adv_city_update(struct city *pcity); -int city_tile_value(const struct city *pcity, const struct tile *ptile, - int foodneed, int prodneed); +adv_want city_tile_value(const struct city *pcity, const struct tile *ptile, + int foodneed, int prodneed); void adv_city_worker_act_set(struct city *pcity, int city_tile_index, - enum unit_activity act_id, int value); -int adv_city_worker_act_get(const struct city *pcity, int city_tile_index, - enum unit_activity act_id); + enum unit_activity act_id, adv_want value); +adv_want adv_city_worker_act_get(const struct city *pcity, int city_tile_index, + enum unit_activity act_id); void adv_city_worker_extra_set(struct city *pcity, int city_tile_index, const struct extra_type *pextra, int value); int adv_city_worker_extra_get(const struct city *pcity, int city_tile_index, -- 2.40.1