From 6878773c7cd0bdeaa39f5021f8fe9e9ffbebc7a8 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Fri, 9 Jun 2023 03:55:20 +0300 Subject: [PATCH 20/20] utype_pays_mp_for_action_estimate(): Take map as parameter The function was hardcoded to use wld.map See osdn #48196 Signed-off-by: Marko Lindqvist --- ai/default/aiair.c | 35 ++++++++++++++++++----------------- common/unittype.c | 5 +++-- common/unittype.h | 29 ++++++++++++++++------------- 3 files changed, 37 insertions(+), 32 deletions(-) diff --git a/ai/default/aiair.c b/ai/default/aiair.c index 83747c0be8..f88daf4a38 100644 --- a/ai/default/aiair.c +++ b/ai/default/aiair.c @@ -76,7 +76,7 @@ static inline int regen_turns(struct unit *punit, struct tile *ptile, /******************************************************************//** Looks for nearest airbase for punit reachable imediatly. - Returns NULL if not found. The path is stored in the path + Returns NULL if not found. The path is stored in the path argument if not NULL. If the unit is damaged, flies to an airbase that can repair the unit in a minimal number of turns. @@ -114,7 +114,7 @@ static struct tile *find_nearest_airbase(struct unit *punit, best = ptile; break; } else if (!best || regt < best_regt) { - /* regenerates faster */ + /* Regenerates faster */ best_regt = regt; best = ptile; } @@ -142,8 +142,8 @@ static bool dai_should_we_air_attack_tile(struct ai_type *ait, struct city *acity = tile_city(ptile); /* For a virtual unit (punit->id == 0), all targets are good */ - /* TODO: There is a danger of producing too many units that will not - * attack anything. Production should not happen if there is an idle + /* TODO: There is a danger of producing too many units that will not + * attack anything. Production should not happen if there is an idle * unit of the same type nearby */ if (acity && punit->id != 0 && def_ai_city_data(acity, ait)->invasion.occupy == 0 @@ -165,17 +165,17 @@ static adv_want dai_evaluate_tile_for_air_attack(struct unit *punit, struct tile *dst_tile) { struct unit *pdefender; - /* unit costs in shields */ + /* Unit costs in shields */ int balanced_cost, unit_cost, victim_cost = 0; - /* unit stats */ + /* Unit stats */ int unit_attack, victim_defense; - /* final answer */ + /* Final answer */ adv_want profit; - /* time spent in the air */ + /* Time spent in the air */ int sortie_time; struct civ_map *nmap = &(wld.map); -#define PROB_MULTIPLIER 100 /* should unify with those in combat.c */ +#define PROB_MULTIPLIER 100 /* Should unify with those in combat.c */ if (!can_unit_attack_tile(punit, NULL, dst_tile) || !(pdefender = get_defender(nmap, punit, dst_tile))) { @@ -209,13 +209,14 @@ static adv_want dai_evaluate_tile_for_air_attack(struct unit *punit, balanced_cost = build_cost_balanced(unit_type_get(punit)); - sortie_time = (utype_pays_mp_for_action_estimate( - action_by_number(ACTION_ATTACK), - unit_type_get(punit), unit_owner(punit), - /* Assume that dst_tile is closer to the tile the actor - * unit will attack from than its current tile. */ - dst_tile, - dst_tile) >= MAX_MOVE_FRAGS ? 1 : 0); + sortie_time + = (utype_pays_mp_for_action_estimate(nmap, + action_by_number(ACTION_ATTACK), + unit_type_get(punit), unit_owner(punit), + /* Assume that dst_tile is closer to the tile the actor + * unit will attack from than its current tile. */ + dst_tile, + dst_tile) >= MAX_MOVE_FRAGS ? 1 : 0); profit = kill_desire(victim_cost, unit_attack, unit_cost, victim_defense, 1) - SHIELD_WEIGHTING + 2 * TRADE_WEIGHTING; @@ -238,7 +239,7 @@ static adv_want dai_evaluate_tile_for_air_attack(struct unit *punit, /******************************************************************//** Find something to bomb Air-units specific victim search - Returns the want for the best target. The targets are stored in the + Returns the want for the best target. The targets are stored in the path and pptile arguments if not NULL. TODO: take counterattack dangers into account TODO: make separate handicaps for air units seeing targets diff --git a/common/unittype.c b/common/unittype.c index 3cae6afb91..d29be9b207 100644 --- a/common/unittype.c +++ b/common/unittype.c @@ -1442,7 +1442,8 @@ int utype_pays_mp_for_action_base(const struct action *paction, Returns an estimate of the amount of movement points successfully performing the specified action will consume in the actor unit type. **************************************************************************/ -int utype_pays_mp_for_action_estimate(const struct action *paction, +int utype_pays_mp_for_action_estimate(const struct civ_map *nmap, + const struct action *paction, const struct unit_type *putype, const struct player *act_player, const struct tile *act_tile, @@ -1461,7 +1462,7 @@ int utype_pays_mp_for_action_estimate(const struct action *paction, if (utype_pays_for_regular_move_to_tgt(paction, putype)) { /* Add the cost from the move. */ - mpco += map_move_cost(&(wld.map), act_player, putype, + mpco += map_move_cost(nmap, act_player, putype, act_tile, tgt_tile); } diff --git a/common/unittype.h b/common/unittype.h index 9a4e228aab..91c7757e28 100644 --- a/common/unittype.h +++ b/common/unittype.h @@ -30,6 +30,7 @@ struct astring; /* Actually defined in "utility/astring.h". */ struct strvec; /* Actually defined in "utility/string_vector.h". */ struct ai_type; +struct civ_map; /* U_LAST is a value which is guaranteed to be larger than all * actual Unit_type_id values. It is used as a flag value; @@ -480,7 +481,7 @@ struct veteran_system { struct unit_type { Unit_type_id item_number; struct name_translation name; - bool ruledit_disabled; /* Does not really exist - hole in units array */ + bool ruledit_disabled; /* Does not really exist - hole in units array */ void *ruledit_dlg; char graphic_str[MAX_LEN_NAME]; char graphic_alt[MAX_LEN_NAME]; @@ -488,14 +489,15 @@ struct unit_type { char sound_move_alt[MAX_LEN_NAME]; char sound_fight[MAX_LEN_NAME]; char sound_fight_alt[MAX_LEN_NAME]; - int build_cost; /* Use wrappers to access this. */ - int pop_cost; /* number of workers the unit contains (e.g., settlers, engineers)*/ + int build_cost; /* Use wrappers to access this. */ + int pop_cost; /* Number of workers the unit contains + * (e.g., settlers, engineers) */ int attack_strength; int defense_strength; int move_rate; - int unknown_move_cost; /* See utype_unknown_move_cost(). */ + int unknown_move_cost; /* See utype_unknown_move_cost(). */ - struct advance *require_advance; /* may be NULL */ + struct advance *require_advance; /* May be NULL */ struct requirement_vector build_reqs; int vision_radius_sq; @@ -696,7 +698,8 @@ bool utype_pays_for_regular_move_to_tgt(const struct action *paction, int utype_pays_mp_for_action_base(const struct action *paction, const struct unit_type *putype); -int utype_pays_mp_for_action_estimate(const struct action *paction, +int utype_pays_mp_for_action_estimate(const struct civ_map *nmap, + const struct action *paction, const struct unit_type *putype, const struct player *act_player, const struct tile *act_tile, @@ -713,9 +716,9 @@ struct unit_type *role_units_iterate_backwards(int role, role_unit_callback cb, struct unit_type *get_role_unit(int role, int role_index); struct unit_type *best_role_unit(const struct city *pcity, int role); struct unit_type *best_role_unit_for_player(const struct player *pplayer, - int role); + int role); struct unit_type *first_role_unit_for_player(const struct player *pplayer, - int role); + int role); bool role_units_translations(struct astring *astr, int flag, bool alts); /* General unit class routines */ @@ -801,8 +804,8 @@ int utype_happy_cost(const struct unit_type *ut, const struct player *pplayer); const struct unit_type *can_upgrade_unittype(const struct player *pplayer, const struct unit_type *punittype); int unit_upgrade_price(const struct player *pplayer, - const struct unit_type *from, - const struct unit_type *to); + const struct unit_type *from, + const struct unit_type *to); bool utype_player_already_has_this(const struct player *pplayer, const struct unit_type *putype); @@ -810,11 +813,11 @@ bool utype_player_already_has_this_unique(const struct player *pplayer, const struct unit_type *putype); bool can_player_build_unit_direct(const struct player *p, - const struct unit_type *punittype); + const struct unit_type *punittype); bool can_player_build_unit_later(const struct player *p, - const struct unit_type *punittype); + const struct unit_type *punittype); bool can_player_build_unit_now(const struct player *p, - const struct unit_type *punittype); + const struct unit_type *punittype); #define utype_fuel(ptype) (ptype)->fuel -- 2.39.2