From 0070ec45e4a0119bbf801004344aadaeb4921ff2 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Tue, 26 Sep 2023 22:17:24 +0300 Subject: [PATCH 20/20] Autosettlers: Turn tile action values to adv_want See osdn #48421 Signed-off-by: Marko Lindqvist --- server/advisors/autosettlers.c | 31 +++++----- server/advisors/infracache.c | 109 +++++++++++++++++---------------- server/advisors/infracache.h | 12 ++-- 3 files changed, 80 insertions(+), 72 deletions(-) diff --git a/server/advisors/autosettlers.c b/server/advisors/autosettlers.c index f8d44907de..9b87e1df83 100644 --- a/server/advisors/autosettlers.c +++ b/server/advisors/autosettlers.c @@ -299,10 +299,11 @@ static void consider_settler_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, @@ -312,7 +313,8 @@ static void consider_settler_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); @@ -361,7 +363,7 @@ static void consider_settler_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) { @@ -437,10 +439,10 @@ adv_want settler_evaluate_improvements(struct unit *punit, 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; @@ -535,7 +537,7 @@ adv_want settler_evaluate_improvements(struct unit *punit, if (adv_city_worker_act_get(pcity, cindex, act) >= 0 && can_unit_do_activity_targeted_at(punit, act, target, ptile)) { - int base_value = adv_city_worker_act_get(pcity, cindex, act); + adv_want base_value = adv_city_worker_act_get(pcity, cindex, act); turns = pos.turn + get_turns_for_activity_at(punit, act, ptile, target); @@ -557,7 +559,7 @@ adv_want settler_evaluate_improvements(struct unit *punit, 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) { @@ -687,7 +689,8 @@ adv_want settler_evaluate_improvements(struct unit *punit, ACTIVITY_GEN_ROAD, ptile, dep_tgt); - int dep_value = base_value + adv_city_worker_extra_get(pcity, cindex, dep_tgt); + adv_want dep_value + = base_value + adv_city_worker_extra_get(pcity, cindex, dep_tgt); consider_settler_action(pplayer, ACTIVITY_GEN_ROAD, dep_tgt, extra, dep_value, @@ -713,9 +716,9 @@ adv_want settler_evaluate_improvements(struct unit *punit, ACTIVITY_BASE, ptile, dep_tgt); - int dep_value = base_value + adv_city_worker_extra_get(pcity, - cindex, - dep_tgt); + adv_want dep_value + = base_value + adv_city_worker_extra_get(pcity, cindex, + dep_tgt); consider_settler_action(pplayer, ACTIVITY_BASE, dep_tgt, 0.0, dep_value, oldv, in_use, diff --git a/server/advisors/infracache.c b/server/advisors/infracache.c index c348f9f8a4..6caf8cf778 100644 --- a/server/advisors/infracache.c +++ b/server/advisors/infracache.c @@ -31,35 +31,36 @@ #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_irrigate_transform(const struct city *pcity, - const struct tile *ptile); -static int adv_calc_mine_transform(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_irrigate_transform(const struct city *pcity, + const struct tile *ptile); +static adv_want adv_calc_mine_transform(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 irrigating the given tile. - The return value is the goodness of the tile after the irrigation. This - should be compared to the goodness of the tile currently (see - city_tile_value(); note that this depends on the AI's weighting + The return value is the goodness of the tile after the irrigation. + 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_irrigate_transform(const struct city *pcity, - const struct tile *ptile) +static adv_want adv_calc_irrigate_transform(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); @@ -91,14 +92,15 @@ static int adv_calc_irrigate_transform(const struct city *pcity, /************************************************************************** Calculate the benefit of mining the given tile. - The return value is the goodness of the tile after the mining. This - should be compared to the goodness of the tile currently (see - city_tile_value(); note that this depends on the AI's weighting + The return value is the goodness of the tile after the mining. + 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_mine_transform(const struct city *pcity, const struct tile *ptile) +static adv_want adv_calc_mine_transform(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); @@ -130,15 +132,15 @@ static int adv_calc_mine_transform(const struct city *pcity, const struct tile * /************************************************************************** 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; @@ -172,18 +174,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); @@ -210,14 +213,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); @@ -289,16 +293,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) { @@ -321,7 +325,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)) { @@ -346,8 +350,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 4b4bc19b00..13542e6b63 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