From e95af063a358a5ff72dbb8338e126e48c2a89efe Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Tue, 29 Aug 2023 16:30:24 +0300 Subject: [PATCH 6/6] Make path finding map agnostic Take map as parameter, unhardcoding old assumption of the main map being used. See osdn #48556 Signed-off-by: Marko Lindqvist --- ai/default/aiair.c | 10 +++++++--- ai/default/aidiplomat.c | 16 +++++++++------- ai/default/aiferry.c | 16 ++++++++++------ ai/default/daidomestic.c | 3 ++- ai/default/daihunter.c | 5 +++-- ai/default/daimilitary.c | 3 ++- ai/default/daisettler.c | 5 +++-- ai/default/daitools.c | 19 ++++++++++++------- ai/default/daiunit.c | 33 ++++++++++++++++++++------------- client/goto.c | 2 +- common/aicore/caravan.c | 7 ++++--- common/aicore/pf_tools.c | 27 ++++++++++++++++++--------- common/aicore/pf_tools.h | 6 ++++++ server/advisors/advbuilding.c | 11 ++++++----- server/advisors/autoexplorer.c | 14 +++++++++----- server/advisors/autoworkers.c | 7 ++++--- server/unittools.c | 7 ++++--- 17 files changed, 120 insertions(+), 71 deletions(-) diff --git a/ai/default/aiair.c b/ai/default/aiair.c index 3c1df43a43..35d82d5069 100644 --- a/ai/default/aiair.c +++ b/ai/default/aiair.c @@ -93,8 +93,9 @@ static struct tile *find_nearest_airbase(struct unit *punit, struct tile *best = NULL; int lost_hp = unit_type_get(punit)->hp - punit->hp; int best_regt = FC_INFINITY; + const struct civ_map *nmap = &(wld.map); - pft_fill_unit_parameter(¶meter, punit); + pft_fill_unit_parameter(¶meter, nmap, punit); parameter.omniscience = !has_handicap(pplayer, H_MAP); pfm = pf_map_new(¶meter); @@ -252,8 +253,9 @@ static adv_want find_something_to_bomb(struct ai_type *ait, struct unit *punit, struct pf_map *pfm; struct tile *best_tile = NULL; adv_want best = 0; + const struct civ_map *nmap = &(wld.map); - pft_fill_unit_parameter(¶meter, punit); + pft_fill_unit_parameter(¶meter, nmap, punit); parameter.omniscience = !has_handicap(pplayer, H_MAP); pfm = pf_map_new(¶meter); @@ -328,6 +330,7 @@ static struct tile *dai_find_strategic_airbase(struct ai_type *ait, int regen_turns_min = FC_INFINITY; bool defend = FALSE; /* Used only for lost_hp > 0 */ bool refuel_start = FALSE; /* Used for not a "grave danger" start */ + const struct civ_map *nmap = &(wld.map); /* Consider staying at the current position * before we generate the map, maybe we should not */ @@ -372,7 +375,8 @@ static struct tile *dai_find_strategic_airbase(struct ai_type *ait, refuel_start = TRUE; } } - pft_fill_unit_parameter(¶meter, punit); + + pft_fill_unit_parameter(¶meter, nmap, punit); parameter.omniscience = !has_handicap(pplayer, H_MAP); pfm = pf_map_new(¶meter); pf_map_move_costs_iterate(pfm, ptile, move_cost, FALSE) { diff --git a/ai/default/aidiplomat.c b/ai/default/aidiplomat.c index 0af4f906d4..9c08d5b8e3 100644 --- a/ai/default/aidiplomat.c +++ b/ai/default/aidiplomat.c @@ -171,6 +171,7 @@ void dai_choose_diplomat_offensive(struct ai_type *ait, struct unit_type *ut = best_role_unit(pcity, UTYF_DIPLOMAT); struct ai_plr *ai = def_ai_player_data(pplayer, ait); int expenses; + const struct civ_map *nmap = &(wld.map); dai_calc_data(pplayer, NULL, &expenses, NULL); @@ -198,7 +199,7 @@ void dai_choose_diplomat_offensive(struct ai_type *ait, pplayer, pcity, ut, city_production_unit_veteran_level(pcity, ut)); - pft_fill_unit_parameter(¶meter, punit); + pft_fill_unit_parameter(¶meter, nmap, punit); parameter.omniscience = !has_handicap(pplayer, H_MAP); pfm = pf_map_new(¶meter); @@ -709,7 +710,7 @@ static bool dai_diplomat_bribe_nearby(struct ai_type *ait, /**************************************************************************//** If we are the only diplomat in a threatened city, defend against enemy - actions. The passive defense is set by game.diplchance. The active + actions. The passive defense is set by game.diplchance. The active defense is to bribe units which end their move nearby. Our next trick is to look for enemy cities on our continent and do our diplomat things. @@ -725,13 +726,14 @@ void dai_manage_diplomat(struct ai_type *ait, struct player *pplayer, struct pf_map *pfm; struct pf_position pos; struct unit_ai *unit_data; + const struct civ_map *nmap = &(wld.map); CHECK_UNIT(punit); /* Generate map */ - pft_fill_unit_parameter(¶meter, punit); + pft_fill_unit_parameter(¶meter, nmap, punit); parameter.omniscience = !has_handicap(pplayer, H_MAP); - parameter.get_zoc = NULL; /* kludge */ + parameter.get_zoc = NULL; /* Kludge */ parameter.get_TB = no_intermediate_fights; pfm = pf_map_new(¶meter); @@ -791,15 +793,15 @@ void dai_manage_diplomat(struct ai_type *ait, struct player *pplayer, } } - /* We may need a new map now. Both because we cannot get paths from an + /* We may need a new map now. Both because we cannot get paths from an * old map, and we need paths to move, and because fctd below requires * a new map for its iterator. */ if (!same_pos(parameter.start_tile, unit_tile(punit)) || unit_data->task == AIUNIT_NONE) { pf_map_destroy(pfm); - pft_fill_unit_parameter(¶meter, punit); + pft_fill_unit_parameter(¶meter, nmap, punit); parameter.omniscience = !has_handicap(pplayer, H_MAP); - parameter.get_zoc = NULL; /* kludge */ + parameter.get_zoc = NULL; /* Kludge */ parameter.get_TB = no_intermediate_fights; pfm = pf_map_new(¶meter); } diff --git a/ai/default/aiferry.c b/ai/default/aiferry.c index 78ecdbce12..293cbb866b 100644 --- a/ai/default/aiferry.c +++ b/ai/default/aiferry.c @@ -502,6 +502,7 @@ int aiferry_find_boat(struct ai_type *ait, struct unit *punit, struct pf_parameter param; struct pf_map *search_map; struct player *pplayer = unit_owner(punit); + const struct civ_map *nmap = &(wld.map); /* currently assigned ferry */ int ferryboat = def_ai_unit_data(punit, ait)->ferryboat; @@ -526,7 +527,7 @@ int aiferry_find_boat(struct ai_type *ait, struct unit *punit, return 0; } - pft_fill_unit_parameter(¶m, punit); + pft_fill_unit_parameter(¶m, nmap, punit); param.omniscience = !has_handicap(pplayer, H_MAP); param.get_TB = no_fights_or_unknown; param.get_EC = sea_move; @@ -954,8 +955,10 @@ static bool aiferry_findcargo(struct ai_type *ait, struct unit *pferry) /* Path-finding stuff */ struct pf_map *pfm; struct pf_parameter parameter; - int passengers = dai_plr_data_get(ait, unit_owner(pferry), NULL)->stats.passengers; + int passengers = dai_plr_data_get(ait, unit_owner(pferry), + NULL)->stats.passengers; struct player *pplayer; + const struct civ_map *nmap = &(wld.map); if (passengers <= 0) { /* No passangers anywhere */ @@ -965,12 +968,12 @@ static bool aiferry_findcargo(struct ai_type *ait, struct unit *pferry) UNIT_LOG(LOGLEVEL_FERRY, pferry, "Ferryboat is looking for cargo."); pplayer = unit_owner(pferry); - pft_fill_unit_overlap_param(¶meter, pferry); + pft_fill_unit_overlap_param(¶meter, nmap, pferry); parameter.omniscience = !has_handicap(pplayer, H_MAP); /* If we have omniscience, we use it, since paths to some places - * might be "blocked" by unknown. We don't want to fight though */ + * might be "blocked" by unknown. We don't want to fight though */ parameter.get_TB = no_fights; - + pfm = pf_map_new(¶meter); pf_map_tiles_iterate(pfm, ptile, TRUE) { unit_list_iterate(ptile->units, aunit) { @@ -1023,10 +1026,11 @@ static bool aiferry_find_interested_city(struct ai_type *ait, int turns_horizon = FC_INFINITY; /* Future return value */ bool needed = FALSE; + const struct civ_map *nmap = &(wld.map); UNIT_LOG(LOGLEVEL_FERRY, pferry, "Ferry looking for a city that needs it"); - pft_fill_unit_parameter(¶meter, pferry); + pft_fill_unit_parameter(¶meter, nmap, pferry); /* We are looking for our own cities, no need to look into the unknown */ parameter.get_TB = no_fights_or_unknown; parameter.omniscience = FALSE; diff --git a/ai/default/daidomestic.c b/ai/default/daidomestic.c index 2f401ae916..35d35e54d8 100644 --- a/ai/default/daidomestic.c +++ b/ai/default/daidomestic.c @@ -648,6 +648,7 @@ void dai_wonder_city_distance(struct ai_type *ait, struct player *pplayer, struct unit *ghost; int maxrange; struct city *wonder_city = game_city_by_number(adv->wonder_city); + const struct civ_map *nmap = &(wld.map); city_list_iterate(pplayer->cities, acity) { /* Mark unavailable */ @@ -671,7 +672,7 @@ void dai_wonder_city_distance(struct ai_type *ait, struct player *pplayer, ghost = unit_virtual_create(pplayer, wonder_city, punittype, 0); maxrange = unit_move_rate(ghost) * 7; - pft_fill_unit_parameter(¶meter, ghost); + pft_fill_unit_parameter(¶meter, nmap, ghost); parameter.omniscience = !has_handicap(pplayer, H_MAP); pfm = pf_map_new(¶meter); diff --git a/ai/default/daihunter.c b/ai/default/daihunter.c index 039a0945eb..2c1cc46594 100644 --- a/ai/default/daihunter.c +++ b/ai/default/daihunter.c @@ -306,6 +306,7 @@ static void dai_hunter_try_launch(struct ai_type *ait, int target_sanity = target->id; struct pf_parameter parameter; struct pf_map *pfm; + const struct civ_map *nmap = &(wld.map); unit_list_iterate_safe(unit_tile(punit)->units, missile) { struct unit *sucker = NULL; @@ -314,7 +315,7 @@ static void dai_hunter_try_launch(struct ai_type *ait, && utype_can_do_action(unit_type_get(missile), ACTION_SUICIDE_ATTACK)) { UNIT_LOG(LOGLEVEL_HUNT, missile, "checking for hunt targets"); - pft_fill_unit_parameter(¶meter, punit); + pft_fill_unit_parameter(¶meter, nmap, punit); parameter.omniscience = !has_handicap(pplayer, H_MAP); pfm = pf_map_new(¶meter); @@ -441,7 +442,7 @@ int dai_hunter_manage(struct ai_type *ait, struct player *pplayer, fc_assert_ret_val(!is_barbarian(pplayer), 0); fc_assert_ret_val(pplayer->is_alive, 0); - pft_fill_unit_parameter(¶meter, punit); + pft_fill_unit_parameter(¶meter, nmap, punit); parameter.omniscience = !has_handicap(pplayer, H_MAP); pfm = pf_map_new(¶meter); diff --git a/ai/default/daimilitary.c b/ai/default/daimilitary.c index ce2cdb8ffc..6fb41c0241 100644 --- a/ai/default/daimilitary.c +++ b/ai/default/daimilitary.c @@ -1246,6 +1246,7 @@ static void process_attacker_want(struct ai_type *ait, int needferry = 0; bool unhap = dai_assess_military_unhappiness(pcity); struct ai_plr *plr_data = def_ai_player_data(pplayer, ait); + const struct civ_map *nmap = &(wld.map); /* Has to be initialized to make gcc happy */ struct ai_city *acity_data = NULL; @@ -1323,7 +1324,7 @@ static void process_attacker_want(struct ai_type *ait, attack *= attack; - pft_fill_utype_parameter(¶meter, punittype, city_tile(pcity), + pft_fill_utype_parameter(¶meter, nmap, punittype, city_tile(pcity), pplayer); parameter.omniscience = !has_handicap(pplayer, H_MAP); pfm = pf_map_new(¶meter); diff --git a/ai/default/daisettler.c b/ai/default/daisettler.c index 83793cae60..65497bd1f1 100644 --- a/ai/default/daisettler.c +++ b/ai/default/daisettler.c @@ -885,6 +885,7 @@ static struct cityresult *find_best_city_placement(struct ai_type *ait, struct player *pplayer = unit_owner(punit); struct unit *ferry = NULL; struct cityresult *cr1 = NULL, *cr2 = NULL; + const struct civ_map *nmap = &(wld.map); fc_assert_ret_val(is_ai(pplayer), NULL); /* Only virtual units may use virtual boats: */ @@ -892,7 +893,7 @@ static struct cityresult *find_best_city_placement(struct ai_type *ait, /* Phase 1: Consider building cities on our continent */ - pft_fill_unit_parameter(¶meter, punit); + pft_fill_unit_parameter(¶meter, nmap, punit); parameter.omniscience = !has_handicap(pplayer, H_MAP); cr1 = settler_map_iterate(ait, ¶meter, punit, 0); @@ -942,7 +943,7 @@ static struct cityresult *find_best_city_placement(struct ai_type *ait, } fc_assert(dai_is_ferry(ferry, ait)); - pft_fill_unit_overlap_param(¶meter, ferry); + pft_fill_unit_overlap_param(¶meter, nmap, ferry); parameter.omniscience = !has_handicap(pplayer, H_MAP); parameter.get_TB = no_fights_or_unknown; diff --git a/ai/default/daitools.c b/ai/default/daitools.c index 9a4cd2070c..868ce93811 100644 --- a/ai/default/daitools.c +++ b/ai/default/daitools.c @@ -292,13 +292,16 @@ bool dai_gothere(struct ai_type *ait, struct player *pplayer, } /**********************************************************************//** - Returns the destination for a unit moving towards a given final destination. - That is, it gives a suitable way-point, if necessary. + Returns the destination for a unit moving towards a given + final destination. That is, it gives a suitable way-point, + if necessary. For example, aircraft need these way-points to refuel. **************************************************************************/ struct tile *immediate_destination(struct unit *punit, struct tile *dest_tile) { + const struct civ_map *nmap = &(wld.map); + if (!same_pos(unit_tile(punit), dest_tile) && utype_fuel(unit_type_get(punit))) { struct pf_parameter parameter; @@ -307,7 +310,7 @@ struct tile *immediate_destination(struct unit *punit, size_t i; struct player *pplayer = unit_owner(punit); - pft_fill_unit_parameter(¶meter, punit); + pft_fill_unit_parameter(¶meter, nmap, punit); parameter.omniscience = !has_handicap(pplayer, H_MAP); pfm = pf_map_new(¶meter); path = pf_map_path(pfm, punit->goto_tile); @@ -424,6 +427,7 @@ bool dai_unit_goto_constrained(struct ai_type *ait, struct unit *punit, bool goto_is_sane(struct unit *punit, struct tile *ptile) { bool can_get_there = FALSE; + const struct civ_map *nmap = &(wld.map); if (same_pos(unit_tile(punit), ptile)) { can_get_there = TRUE; @@ -431,7 +435,7 @@ bool goto_is_sane(struct unit *punit, struct tile *ptile) struct pf_parameter parameter; struct pf_map *pfm; - pft_fill_unit_attack_param(¶meter, punit); + pft_fill_unit_attack_param(¶meter, nmap, punit); pfm = pf_map_new(¶meter); if (pf_map_move_cost(pfm, ptile) != PF_IMPOSSIBLE_MC) { @@ -472,6 +476,7 @@ void dai_fill_unit_param(struct ai_type *ait, struct pf_parameter *parameter, bool is_ferry; struct unit_ai *unit_data = def_ai_unit_data(punit, ait); struct player *pplayer = unit_owner(punit); + const struct civ_map *nmap = &(wld.map); /* This function is now always omniscient and should not be used * for human players any more. */ @@ -484,7 +489,7 @@ void dai_fill_unit_param(struct ai_type *ait, struct pf_parameter *parameter, if (is_ferry) { /* The destination may be a coastal land tile, * in which case the ferry should stop on an adjacent tile. */ - pft_fill_unit_overlap_param(parameter, punit); + pft_fill_unit_overlap_param(parameter, nmap, punit); } else if (!utype_fuel(unit_type_get(punit)) && utype_can_do_action_result(unit_type_get(punit), ACTRES_ATTACK) @@ -494,9 +499,9 @@ void dai_fill_unit_param(struct ai_type *ait, struct pf_parameter *parameter, || unit_data->task == AIUNIT_HUNTER)) { /* Use attack movement for defenders and escorts so they can * make defensive attacks */ - pft_fill_unit_attack_param(parameter, punit); + pft_fill_unit_attack_param(parameter, nmap, punit); } else { - pft_fill_unit_parameter(parameter, punit); + pft_fill_unit_parameter(parameter, nmap, punit); } parameter->omniscience = !has_handicap(pplayer, H_MAP); diff --git a/ai/default/daiunit.c b/ai/default/daiunit.c index 12b2bbd577..1bd114fbee 100644 --- a/ai/default/daiunit.c +++ b/ai/default/daiunit.c @@ -529,8 +529,9 @@ static struct pf_path *find_rampage_target(struct unit *punit, /* Want of the best target */ int max_want = 0; struct player *pplayer = unit_owner(punit); - - pft_fill_unit_attack_param(¶meter, punit); + const struct civ_map *nmap = &(wld.map); + + pft_fill_unit_attack_param(¶meter, nmap, punit); parameter.omniscience = !has_handicap(pplayer, H_MAP); /* When trying to find rampage targets we ignore risks such as * enemy units because we are looking for trouble! @@ -722,6 +723,7 @@ adv_want look_for_charge(struct ai_type *ait, struct player *pplayer, int def, best_def = -1; /* Arbitrary: 3 turns. */ const int max_move_cost = 3 * unit_move_rate(punit); + const struct civ_map *nmap = &(wld.map); *aunit = NULL; *acity = NULL; @@ -731,7 +733,7 @@ adv_want look_for_charge(struct ai_type *ait, struct player *pplayer, return 0; } - pft_fill_unit_parameter(¶meter, punit); + pft_fill_unit_parameter(¶meter, nmap, punit); parameter.omniscience = !has_handicap(pplayer, H_MAP); pfm = pf_map_new(¶meter); @@ -1276,7 +1278,7 @@ adv_want find_something_to_kill(struct ai_type *ait, struct player *pplayer, bcost = unit_build_shield_cost_base(punit); bcost_bal = build_cost_balanced(punit_type); - pft_fill_unit_attack_param(¶meter, punit); + pft_fill_unit_attack_param(¶meter, nmap, punit); parameter.omniscience = !has_handicap(pplayer, H_MAP); punit_map = pf_map_new(¶meter); @@ -1303,7 +1305,7 @@ adv_want find_something_to_kill(struct ai_type *ait, struct player *pplayer, if (NULL != ferryboat) { boattype = unit_type_get(ferryboat); - pft_fill_unit_overlap_param(¶meter, ferryboat); + pft_fill_unit_overlap_param(¶meter, nmap, ferryboat); parameter.omniscience = !has_handicap(pplayer, H_MAP); ferry_map = pf_map_new(¶meter); } else { @@ -1314,8 +1316,8 @@ adv_want find_something_to_kill(struct ai_type *ait, struct player *pplayer, } if (NULL != boattype && harbor) { /* Let's simulate a boat at 'punit' position. */ - pft_fill_utype_overlap_param(¶meter, boattype, punit_tile, - pplayer); + pft_fill_utype_overlap_param(¶meter, nmap, boattype, + punit_tile, pplayer); parameter.omniscience = !has_handicap(pplayer, H_MAP); ferry_map = pf_map_new(¶meter); } else { @@ -1630,8 +1632,9 @@ struct city *find_nearest_safe_city(struct unit *punit) struct player *pplayer = unit_owner(punit); struct city *pcity, *best_city = NULL; int best = FC_INFINITY, cur; + const struct civ_map *nmap = &(wld.map); - pft_fill_unit_parameter(¶meter, punit); + pft_fill_unit_parameter(¶meter, nmap, punit); parameter.omniscience = !has_handicap(pplayer, H_MAP); pfm = pf_map_new(¶meter); @@ -2928,13 +2931,15 @@ static void dai_manage_barbarian_leader(struct ai_type *ait, int move_cost, best_move_cost; int body_guards; bool alive = TRUE; + const struct civ_map *nmap = &(wld.map); CHECK_UNIT(leader); - if (0 == leader->moves_left + if (leader->moves_left == 0 || (can_unit_survive_at_tile(&(wld.map), leader, leader_tile) && 1 < unit_list_size(leader_tile->units))) { unit_activity_handling(leader, ACTIVITY_SENTRY); + return; } @@ -2975,11 +2980,12 @@ static void dai_manage_barbarian_leader(struct ai_type *ait, } unit_list_iterate_end; if (0 < body_guards) { - pft_fill_unit_parameter(¶meter, leader); + pft_fill_unit_parameter(¶meter, nmap, leader); parameter.omniscience = !has_handicap(pplayer, H_MAP); pfm = pf_map_new(¶meter); - /* Find the closest body guard. FIXME: maybe choose the strongest too? */ + /* Find the closest body guard. + * FIXME: maybe choose the strongest too? */ pf_map_tiles_iterate(pfm, ptile, FALSE) { unit_list_iterate(ptile->units, punit) { if (unit_owner(punit) == pplayer @@ -3028,7 +3034,7 @@ static void dai_manage_barbarian_leader(struct ai_type *ait, return; } - pft_fill_unit_parameter(¶meter, worst_danger); + pft_fill_unit_parameter(¶meter, nmap, worst_danger); parameter.omniscience = !has_handicap(pplayer, H_MAP); pfm = pf_map_new(¶meter); best_move_cost = pf_map_move_cost(pfm, leader_tile); @@ -3415,8 +3421,9 @@ bool dai_unit_can_strike_my_unit(const struct unit *attacker, const struct tile *ptarget = unit_tile(defender); int max_move_cost = attacker->moves_left; bool able_to_strike = FALSE; + const struct civ_map *nmap = &(wld.map); - pft_fill_unit_parameter(¶meter, attacker); + pft_fill_unit_parameter(¶meter, nmap, attacker); parameter.omniscience = !has_handicap(unit_owner(defender), H_MAP); pfm = pf_map_new(¶meter); diff --git a/client/goto.c b/client/goto.c index 1ab8f81963..8ac0743aa1 100644 --- a/client/goto.c +++ b/client/goto.c @@ -888,7 +888,7 @@ no_fights_or_unknown_goto(const struct tile *ptile, static void goto_fill_parameter_base(struct pf_parameter *parameter, const struct unit *punit) { - pft_fill_unit_parameter(parameter, punit); + pft_fill_unit_parameter(parameter, &(wld.map), punit); fc_assert(parameter->get_EC == NULL); fc_assert(parameter->get_TB == NULL); diff --git a/common/aicore/caravan.c b/common/aicore/caravan.c index 5cb6f42bfc..f19c10b44e 100644 --- a/common/aicore/caravan.c +++ b/common/aicore/caravan.c @@ -193,7 +193,7 @@ int caravan_result_compare(const struct caravan_result *a, /************************************************************************//** We use the path finding in several places. - This provides a single implementation of that. It is critical that + This provides a single implementation of that. It is critical that this function be re-entrant since we call it recursively. The callback should return TRUE if it wants to stop searching, @@ -213,11 +213,12 @@ static void caravan_search_from(const struct unit *caravan, struct pf_map *pfm; struct pf_parameter pfparam; int end_time; + const struct civ_map *nmap = &(wld.map); end_time = param->horizon - turns_before; /* Initialize the pf run. */ - pft_fill_unit_parameter(&pfparam, (struct unit *) caravan); + pft_fill_unit_parameter(&pfparam, nmap, (struct unit *) caravan); pfparam.start_tile = start_tile; pfparam.moves_left_initially = moves_left_before; pfparam.omniscience = omniscient; @@ -228,7 +229,7 @@ static void caravan_search_from(const struct unit *caravan, otherwise, run the callback if we're on a city. Do-while loop rather than while loop to make sure to process the start tile. - */ + */ pf_map_positions_iterate(pfm, pos, TRUE) { struct city *pcity; diff --git a/common/aicore/pf_tools.c b/common/aicore/pf_tools.c index 1bc840dcee..fb0304f90e 100644 --- a/common/aicore/pf_tools.c +++ b/common/aicore/pf_tools.c @@ -679,9 +679,10 @@ static bool amphibious_is_pos_dangerous(const struct tile *ptile, ****************************************************************************/ static inline void pft_fill_default_parameter(struct pf_parameter *parameter, + const struct civ_map *nmap, const struct unit_type *punittype) { - parameter->map = &(wld.map); + parameter->map = nmap; parameter->get_TB = NULL; parameter->get_EC = NULL; parameter->is_pos_dangerous = NULL; @@ -729,6 +730,7 @@ pft_enable_default_actions(struct pf_parameter *parameter) ****************************************************************************/ static inline void pft_fill_utype_default_parameter(struct pf_parameter *parameter, + const struct civ_map *nmap, const struct unit_type *punittype, struct tile *pstart_tile, struct player *powner) @@ -740,7 +742,7 @@ pft_fill_utype_default_parameter(struct pf_parameter *parameter, veteran_level = utype_veteran_levels(punittype) - 1; } - pft_fill_default_parameter(parameter, punittype); + pft_fill_default_parameter(parameter, nmap, punittype); parameter->start_tile = pstart_tile; parameter->moves_left_initially = punittype->move_rate; @@ -766,12 +768,13 @@ pft_fill_utype_default_parameter(struct pf_parameter *parameter, ****************************************************************************/ static inline void pft_fill_unit_default_parameter(struct pf_parameter *parameter, + const struct civ_map *nmap, const struct unit *punit) { const struct unit *ptrans = unit_transport_get(punit); const struct unit_type *ptype = unit_type_get(punit); - pft_fill_default_parameter(parameter, ptype); + pft_fill_default_parameter(parameter, nmap, ptype); parameter->start_tile = unit_tile(punit); parameter->moves_left_initially = punit->moves_left; @@ -821,11 +824,12 @@ static inline void pft_fill_parameter(struct pf_parameter *parameter, Fill classic parameters for an unit type. ****************************************************************************/ void pft_fill_utype_parameter(struct pf_parameter *parameter, + const struct civ_map *nmap, const struct unit_type *punittype, struct tile *pstart_tile, struct player *pplayer) { - pft_fill_utype_default_parameter(parameter, punittype, + pft_fill_utype_default_parameter(parameter, nmap, punittype, pstart_tile, pplayer); pft_fill_parameter(parameter, punittype); } @@ -834,9 +838,10 @@ void pft_fill_utype_parameter(struct pf_parameter *parameter, Fill classic parameters for an unit. ****************************************************************************/ void pft_fill_unit_parameter(struct pf_parameter *parameter, + const struct civ_map *nmap, const struct unit *punit) { - pft_fill_unit_default_parameter(parameter, punit); + pft_fill_unit_default_parameter(parameter, nmap, punit); pft_fill_parameter(parameter, unit_type_get(punit)); } @@ -869,11 +874,12 @@ static void pft_fill_overlap_param(struct pf_parameter *parameter, For sea/land bombardment and for ferry types. ****************************************************************************/ void pft_fill_utype_overlap_param(struct pf_parameter *parameter, + const struct civ_map *nmap, const struct unit_type *punittype, struct tile *pstart_tile, struct player *pplayer) { - pft_fill_utype_default_parameter(parameter, punittype, + pft_fill_utype_default_parameter(parameter, nmap, punittype, pstart_tile, pplayer); pft_fill_overlap_param(parameter, punittype); } @@ -883,9 +889,10 @@ void pft_fill_utype_overlap_param(struct pf_parameter *parameter, For sea/land bombardment and for ferries. ****************************************************************************/ void pft_fill_unit_overlap_param(struct pf_parameter *parameter, + const struct civ_map *nmap, const struct unit *punit) { - pft_fill_unit_default_parameter(parameter, punit); + pft_fill_unit_default_parameter(parameter, nmap, punit); pft_fill_overlap_param(parameter, unit_type_get(punit)); } @@ -924,11 +931,12 @@ static void pft_fill_attack_param(struct pf_parameter *parameter, Consider attacking and non-attacking possibilities properly. ****************************************************************************/ void pft_fill_utype_attack_param(struct pf_parameter *parameter, + const struct civ_map *nmap, const struct unit_type *punittype, struct tile *pstart_tile, struct player *pplayer) { - pft_fill_utype_default_parameter(parameter, punittype, + pft_fill_utype_default_parameter(parameter, nmap, punittype, pstart_tile, pplayer); pft_fill_attack_param(parameter, punittype); } @@ -939,9 +947,10 @@ void pft_fill_utype_attack_param(struct pf_parameter *parameter, Consider attacking and non-attacking possibilities properly. ****************************************************************************/ void pft_fill_unit_attack_param(struct pf_parameter *parameter, + const struct civ_map *nmap, const struct unit *punit) { - pft_fill_unit_default_parameter(parameter, punit); + pft_fill_unit_default_parameter(parameter, nmap, punit); pft_fill_attack_param(parameter, unit_type_get(punit)); } diff --git a/common/aicore/pf_tools.h b/common/aicore/pf_tools.h index d13d374e86..3d2508d3ac 100644 --- a/common/aicore/pf_tools.h +++ b/common/aicore/pf_tools.h @@ -44,21 +44,27 @@ struct pft_amphibious void pft_fill_unit_parameter(struct pf_parameter *parameter, + const struct civ_map *nmap, const struct unit *punit); void pft_fill_unit_overlap_param(struct pf_parameter *parameter, + const struct civ_map *nmap, const struct unit *punit); void pft_fill_unit_attack_param(struct pf_parameter *parameter, + const struct civ_map *nmap, const struct unit *punit); void pft_fill_utype_parameter(struct pf_parameter *parameter, + const struct civ_map *nmap, const struct unit_type *punittype, struct tile *pstart_tile, struct player *pplayer); void pft_fill_utype_overlap_param(struct pf_parameter *parameter, + const struct civ_map *nmap, const struct unit_type *punittype, struct tile *pstart_tile, struct player *pplayer); void pft_fill_utype_attack_param(struct pf_parameter *parameter, + const struct civ_map *nmap, const struct unit_type *punittype, struct tile *pstart_tile, struct player *pplayer); diff --git a/server/advisors/advbuilding.c b/server/advisors/advbuilding.c index 0f02993da7..c2c8e5ba08 100644 --- a/server/advisors/advbuilding.c +++ b/server/advisors/advbuilding.c @@ -60,20 +60,21 @@ static void calculate_city_clusters(struct player *pplayer) struct unit_type *punittype; struct unit *ghost; int range; + const struct civ_map *nmap = &(wld.map); city_list_iterate(pplayer->cities, pcity) { pcity->server.adv->downtown = 0; } city_list_iterate_end; if (num_role_units(action_id_get_role(ACTION_HELP_WONDER)) == 0) { - return; /* ruleset has no help wonder unit */ + return; /* Ruleset has no help wonder unit */ } punittype = best_role_unit_for_player(pplayer, action_id_get_role(ACTION_HELP_WONDER)); - if (!punittype) { - /* simulate future unit */ + if (punittype == NULL) { + /* Simulate future unit */ punittype = get_role_unit(action_id_get_role(ACTION_HELP_WONDER), 0); } @@ -89,7 +90,7 @@ static void calculate_city_clusters(struct player *pplayer) struct adv_city *city_data = pcity->server.adv; unit_tile_set(ghost, pcity->tile); - pft_fill_unit_parameter(¶meter, ghost); + pft_fill_unit_parameter(¶meter, nmap, ghost); parameter.omniscience = !has_handicap(pplayer, H_MAP); pfm = pf_map_new(¶meter); @@ -99,7 +100,7 @@ static void calculate_city_clusters(struct player *pplayer) if (move_cost > range) { break; } - if (!acity) { + if (acity == NULL) { continue; } if (city_owner(acity) == pplayer) { diff --git a/server/advisors/autoexplorer.c b/server/advisors/autoexplorer.c index 273bde8ad3..51bdf2a289 100644 --- a/server/advisors/autoexplorer.c +++ b/server/advisors/autoexplorer.c @@ -131,8 +131,9 @@ static bool explorer_goto(struct unit *punit, struct tile *ptile) struct pf_map *pfm; struct pf_path *path; struct player *pplayer = unit_owner(punit); + const struct civ_map *nmap = &(wld.map); - pft_fill_unit_parameter(¶meter, punit); + pft_fill_unit_parameter(¶meter, nmap, punit); parameter.omniscience = !has_handicap(pplayer, H_MAP); parameter.get_TB = explorer_tb; adv_avoid_risks(¶meter, &risk_cost, punit, NORMAL_STACKING_FEARFULNESS); @@ -158,7 +159,7 @@ static bool explorer_goto(struct unit *punit, struct tile *ptile) /**********************************************************************//** Return a value indicating how desirable it is to explore the given tile. In general, we want to discover unknown terrain of the opposite kind to - our natural terrain, i.e. pedestrians like ocean and boats like land. + our natural terrain, i.e., pedestrians like ocean and boats like land. Even if terrain is known, but of opposite kind, we still want it -- so that we follow the shoreline. We also would like discovering tiles which can be harvested by our cities -- @@ -261,7 +262,7 @@ static int explorer_desirable(struct tile *ptile, struct player *pplayer, } /**********************************************************************//** - Handle eXplore mode of a unit (explorers are always in eXplore mode + Handle eXplore mode of a unit (explorers are always in eXplore mode for AI) - explores unknown territory, finds huts. MR_OK: there is more territory to be explored. @@ -285,7 +286,7 @@ enum unit_move_result manage_auto_explorer(struct unit *punit) * order to be better than the current most_desirable tile. */ int max_dist = FC_INFINITY; - /* Coordinates of most desirable tile. Initialized to make + /* Coordinates of most desirable tile. Initialized to make * compiler happy. Also MC to the best tile. */ struct tile *best_tile = NULL; int best_MC = FC_INFINITY; @@ -294,6 +295,8 @@ enum unit_move_result manage_auto_explorer(struct unit *punit) struct pf_map *pfm; struct pf_parameter parameter; + const struct civ_map *nmap = &(wld.map); + #define DIST_FACTOR 0.6 double logDF = log(DIST_FACTOR); @@ -303,12 +306,13 @@ enum unit_move_result manage_auto_explorer(struct unit *punit) if (!is_human(pplayer) && unit_has_type_flag(punit, UTYF_GAMELOSS)) { UNIT_LOG(LOG_DEBUG, punit, "exploration too dangerous!"); + return MR_BAD_ACTIVITY; /* too dangerous */ } TIMING_LOG(AIT_EXPLORER, TIMER_START); - pft_fill_unit_parameter(¶meter, punit); + pft_fill_unit_parameter(¶meter, nmap, punit); parameter.get_TB = no_fights_or_unknown; /* When exploring, even AI should pretend to not cheat. */ parameter.omniscience = FALSE; diff --git a/server/advisors/autoworkers.c b/server/advisors/autoworkers.c index 2c551a3b23..415bd431b4 100644 --- a/server/advisors/autoworkers.c +++ b/server/advisors/autoworkers.c @@ -459,7 +459,7 @@ adv_want settler_evaluate_improvements(const struct civ_map *nmap, /* Closest worker, if any, headed towards target tile */ struct unit *enroute = NULL; - pft_fill_unit_parameter(¶meter, punit); + pft_fill_unit_parameter(¶meter, nmap, punit); parameter.omniscience = !has_handicap(pplayer, H_MAP); parameter.get_TB = autosettler_tile_behavior; pfm = pf_map_new(¶meter); @@ -835,8 +835,9 @@ struct city *settler_evaluate_city_requests(struct unit *punit, struct worker_task *best = NULL; struct city *taskcity = NULL; int dist = FC_INFINITY; + const struct civ_map *nmap = &(wld.map); - pft_fill_unit_parameter(¶meter, punit); + pft_fill_unit_parameter(¶meter, nmap, punit); parameter.omniscience = !has_handicap(pplayer, H_MAP); parameter.get_TB = autosettler_tile_behavior; pfm = pf_map_new(¶meter); @@ -1082,7 +1083,7 @@ bool auto_settler_setup_work(const struct civ_map *nmap, TILE_XY(best_tile)); if (!path) { - pft_fill_unit_parameter(¶meter, punit); + pft_fill_unit_parameter(¶meter, nmap, punit); parameter.omniscience = !has_handicap(pplayer, H_MAP); parameter.get_TB = autosettler_tile_behavior; pfm = pf_map_new(¶meter); diff --git a/server/unittools.c b/server/unittools.c index 4024c4a194..ec6cd96006 100644 --- a/server/unittools.c +++ b/server/unittools.c @@ -476,6 +476,8 @@ static void do_upgrade_effects(struct player *pplayer) **************************************************************************/ void player_restore_units(struct player *pplayer) { + const struct civ_map *nmap = &(wld.map); + /* 1) get Leonardo out of the way first: */ do_upgrade_effects(pplayer); @@ -520,12 +522,11 @@ void player_restore_units(struct player *pplayer) if (carrier) { unit_transport_load_tp_status(punit, carrier, FALSE); } else { - bool alive = true; - struct pf_map *pfm; struct pf_parameter parameter; + bool alive = TRUE; - pft_fill_unit_parameter(¶meter, punit); + pft_fill_unit_parameter(¶meter, nmap, punit); parameter.omniscience = !has_handicap(pplayer, H_MAP); pfm = pf_map_new(¶meter); -- 2.40.1