From 3b7443cb461948b4a82a8ec02721f0a5ea3fce72 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 4 Nov 2023 03:14:16 +0200 Subject: [PATCH 8/8] Unhardcode road.[ch] wld.map references See osdn #48933 Signed-off-by: Marko Lindqvist --- client/control.c | 15 ++++++++---- client/gui-gtk-3.0/menu.c | 2 +- client/gui-gtk-3.22/menu.c | 2 +- client/gui-gtk-4.0/menu.c | 2 +- client/gui-qt/hudwidget.cpp | 4 +++- client/gui-qt/menu.cpp | 2 +- common/actions.c | 2 +- common/requirements.c | 7 ++++-- common/road.c | 47 ++++++++++++++++++++++--------------- common/road.h | 36 ++++++++++++++++++---------- common/unitlist.c | 5 ++-- common/unitlist.h | 3 ++- server/generator/mapgen.c | 15 ++++++------ server/maphand.c | 2 +- 14 files changed, 88 insertions(+), 56 deletions(-) diff --git a/client/control.c b/client/control.c index 1d95805b7a..2989ac85e7 100644 --- a/client/control.c +++ b/client/control.c @@ -1302,11 +1302,14 @@ static bool is_activity_on_tile(struct tile *ptile, Fill orders to build recursive roads. This modifies ptile, so virtual copy of the real tile should be passed. **************************************************************************/ -int check_recursive_road_connect(struct tile *ptile, const struct extra_type *pextra, - const struct unit *punit, const struct player *pplayer, int rec) +int check_recursive_road_connect(struct tile *ptile, + const struct extra_type *pextra, + const struct unit *punit, + const struct player *pplayer, int rec) { int activity_mc = 0; struct terrain *pterrain = tile_terrain(ptile); + const struct civ_map *nmap = &(wld.map); if (rec > MAX_EXTRA_TYPES) { return -1; @@ -1320,7 +1323,8 @@ int check_recursive_road_connect(struct tile *ptile, const struct extra_type *pe if (!tile_has_extra(ptile, pdep)) { int single_mc; - single_mc = check_recursive_road_connect(ptile, pdep, punit, pplayer, rec + 1); + single_mc = check_recursive_road_connect(ptile, pdep, punit, + pplayer, rec + 1); if (single_mc < 0) { return -1; @@ -1332,11 +1336,12 @@ int check_recursive_road_connect(struct tile *ptile, const struct extra_type *pe /* Can build road after that? */ if (punit != NULL) { - if (!can_build_road(extra_road_get(pextra), punit, ptile)) { + if (!can_build_road(nmap, extra_road_get(pextra), punit, ptile)) { return -1; } } else if (pplayer != NULL) { - if (!player_can_build_road(extra_road_get(pextra), pplayer, ptile)) { + if (!player_can_build_road(nmap, extra_road_get(pextra), + pplayer, ptile)) { return -1; } } diff --git a/client/gui-gtk-3.0/menu.c b/client/gui-gtk-3.0/menu.c index 7963c5e7b9..a63a1ec0c0 100644 --- a/client/gui-gtk-3.0/menu.c +++ b/client/gui-gtk-3.0/menu.c @@ -2492,7 +2492,7 @@ void real_menus_update(void) (can_units_do(punits, unit_can_add_or_build_city) || can_units_do(punits, unit_can_help_build_wonder_here))); menu_entry_set_sensitive("BUILD_ROAD", - (can_units_do_any_road(punits) + (can_units_do_any_road(&(wld.map), punits) || can_units_do(punits, unit_can_est_trade_route_here))); menu_entry_set_sensitive("BUILD_IRRIGATION", diff --git a/client/gui-gtk-3.22/menu.c b/client/gui-gtk-3.22/menu.c index 6f0b48e4f2..b93efe3c6a 100644 --- a/client/gui-gtk-3.22/menu.c +++ b/client/gui-gtk-3.22/menu.c @@ -2519,7 +2519,7 @@ void real_menus_update(void) (can_units_do(punits, unit_can_add_or_build_city) || can_units_do(punits, unit_can_help_build_wonder_here))); menu_entry_set_sensitive("BUILD_ROAD", - (can_units_do_any_road(punits) + (can_units_do_any_road(&(wld.map), punits) || can_units_do(punits, unit_can_est_trade_route_here))); menu_entry_set_sensitive("BUILD_IRRIGATION", diff --git a/client/gui-gtk-4.0/menu.c b/client/gui-gtk-4.0/menu.c index c2c30532cc..d796dbae3d 100644 --- a/client/gui-gtk-4.0/menu.c +++ b/client/gui-gtk-4.0/menu.c @@ -3766,7 +3766,7 @@ void real_menus_update(void) units_can_do_action(punits, ACTION_ANY, TRUE)); menu_entry_set_sensitive(map, "BUILD_ROAD", - (can_units_do_any_road(punits) + (can_units_do_any_road(&(wld.map), punits) || can_units_do(punits, unit_can_est_trade_route_here))); menu_entry_set_sensitive(map, "BUILD_IRRIGATION", diff --git a/client/gui-qt/hudwidget.cpp b/client/gui-qt/hudwidget.cpp index 007d49d14b..b50adc1f55 100644 --- a/client/gui-qt/hudwidget.cpp +++ b/client/gui-qt/hudwidget.cpp @@ -1039,9 +1039,11 @@ int unit_actions::update_actions() /* Road */ { bool ok = false; + extra_type_by_cause_iterate(EC_ROAD, pextra) { struct road_type *proad = extra_road_get(pextra); - if (can_build_road(proad, current_unit, unit_tile(current_unit))) { + + if (can_build_road(&(wld.map), proad, current_unit, unit_tile(current_unit))) { ok = true; } } extra_type_by_cause_iterate_end; diff --git a/client/gui-qt/menu.cpp b/client/gui-qt/menu.cpp index 52c656a8fe..1a5f74b0f8 100644 --- a/client/gui-qt/menu.cpp +++ b/client/gui-qt/menu.cpp @@ -2364,7 +2364,7 @@ void mr_menu::menus_sensitive() { struct extra_type *pextra = nullptr; - if (can_units_do_any_road(punits)) { + if (can_units_do_any_road(&(wld.map), punits)) { i.value()->setEnabled(true); } unit_list_iterate(punits, punit) { diff --git a/common/actions.c b/common/actions.c index 252bd6924a..8c1f02bd10 100644 --- a/common/actions.c +++ b/common/actions.c @@ -4193,7 +4193,7 @@ is_action_possible(const action_id wanted_action, /* Reason: This is not a road. */ return TRI_NO; } - if (!can_build_road(extra_road_get(target_extra), actor->unit, + if (!can_build_road(nmap, extra_road_get(target_extra), actor->unit, target->tile)) { return TRI_NO; } diff --git a/common/requirements.c b/common/requirements.c index 6fae9d4bab..2361ab06ec 100644 --- a/common/requirements.c +++ b/common/requirements.c @@ -2732,6 +2732,7 @@ is_roadflag_req_active(const struct req_context *context, { enum road_flag_id roadflag; enum fc_tristate ret; + struct civ_map *nmap = &(wld.map); IS_REQ_ACTIVE_VARIANT_ASSERT(VUT_ROADFLAG); @@ -2749,13 +2750,15 @@ is_roadflag_req_active(const struct req_context *context, return TRI_MAYBE; } return BOOL_TO_TRISTATE(tile_has_road_flag(context->tile, roadflag) - || is_road_flag_card_near(context->tile, roadflag)); + || is_road_flag_card_near(nmap, context->tile, + roadflag)); case REQ_RANGE_ADJACENT: if (!context->tile) { return TRI_MAYBE; } return BOOL_TO_TRISTATE(tile_has_road_flag(context->tile, roadflag) - || is_road_flag_near_tile(context->tile, roadflag)); + || is_road_flag_near_tile(nmap, context->tile, + roadflag)); case REQ_RANGE_CITY: if (!context->city) { return TRI_MAYBE; diff --git a/common/road.c b/common/road.c index 68b64b785d..bba5506bcf 100644 --- a/common/road.c +++ b/common/road.c @@ -221,7 +221,8 @@ bool road_can_be_built(const struct road_type *proad, const struct tile *ptile) Tells if player and optionally unit have road building requirements fulfilled. ****************************************************************************/ -static bool are_road_reqs_fulfilled(const struct road_type *proad, +static bool are_road_reqs_fulfilled(const struct civ_map *nmap, + const struct road_type *proad, const struct player *pplayer, const struct unit *punit, const struct tile *ptile) @@ -241,14 +242,14 @@ static bool are_road_reqs_fulfilled(const struct road_type *proad, /* FIXME: mixing cardinal and non-cardinal roads as integrators is * probably not a good idea. */ if (is_cardinal_only_road(iroad)) { - cardinal_adjc_iterate(&(wld.map), ptile, adjc_tile) { + cardinal_adjc_iterate(nmap, ptile, adjc_tile) { if (tile_has_extra(adjc_tile, iroad)) { beginning = FALSE; break; } } cardinal_adjc_iterate_end; } else { - adjc_iterate(&(wld.map), ptile, adjc_tile) { + adjc_iterate(nmap, ptile, adjc_tile) { if (tile_has_extra(adjc_tile, iroad)) { beginning = FALSE; break; @@ -276,7 +277,8 @@ static bool are_road_reqs_fulfilled(const struct road_type *proad, /************************************************************************//** Tells if player can build road to tile with suitable unit. ****************************************************************************/ -bool player_can_build_road(const struct road_type *proad, +bool player_can_build_road(const struct civ_map *nmap, + const struct road_type *proad, const struct player *pplayer, const struct tile *ptile) { @@ -284,13 +286,14 @@ bool player_can_build_road(const struct road_type *proad, return FALSE; } - return are_road_reqs_fulfilled(proad, pplayer, NULL, ptile); + return are_road_reqs_fulfilled(nmap, proad, pplayer, NULL, ptile); } /************************************************************************//** Tells if unit can build road on tile. ****************************************************************************/ -bool can_build_road(struct road_type *proad, +bool can_build_road(const struct civ_map *nmap, + struct road_type *proad, const struct unit *punit, const struct tile *ptile) { @@ -300,14 +303,15 @@ bool can_build_road(struct road_type *proad, return FALSE; } - return are_road_reqs_fulfilled(proad, pplayer, punit, ptile); + return are_road_reqs_fulfilled(nmap, proad, pplayer, punit, ptile); } /************************************************************************//** Count tiles with specified road near the tile. Can be called with NULL road. ****************************************************************************/ -int count_road_near_tile(const struct tile *ptile, const struct road_type *proad) +int count_road_near_tile(struct civ_map *nmap, const struct tile *ptile, + const struct road_type *proad) { int count = 0; @@ -315,7 +319,7 @@ int count_road_near_tile(const struct tile *ptile, const struct road_type *proad return 0; } - adjc_iterate(&(wld.map), ptile, adjc_tile) { + adjc_iterate(nmap, ptile, adjc_tile) { if (tile_has_road(adjc_tile, proad)) { count++; } @@ -327,12 +331,13 @@ int count_road_near_tile(const struct tile *ptile, const struct road_type *proad /************************************************************************//** Count tiles with any river near the tile. ****************************************************************************/ -int count_river_near_tile(const struct tile *ptile, +int count_river_near_tile(struct civ_map *nmap, + const struct tile *ptile, const struct extra_type *priver) { int count = 0; - cardinal_adjc_iterate(&(wld.map), ptile, adjc_tile) { + cardinal_adjc_iterate(nmap, ptile, adjc_tile) { if (priver == NULL && tile_has_river(adjc_tile)) { /* Some river */ count++; @@ -348,7 +353,8 @@ int count_river_near_tile(const struct tile *ptile, /************************************************************************//** Count tiles with river of specific type cardinally adjacent to the tile. ****************************************************************************/ -int count_river_type_tile_card(const struct tile *ptile, +int count_river_type_tile_card(struct civ_map *nmap, + const struct tile *ptile, const struct extra_type *priver, bool percentage) { @@ -357,7 +363,7 @@ int count_river_type_tile_card(const struct tile *ptile, fc_assert(priver != NULL); - cardinal_adjc_iterate(&(wld.map), ptile, adjc_tile) { + cardinal_adjc_iterate(nmap, ptile, adjc_tile) { if (tile_has_extra(adjc_tile, priver)) { count++; } @@ -374,7 +380,8 @@ int count_river_type_tile_card(const struct tile *ptile, /************************************************************************//** Count tiles with river of specific type near the tile. ****************************************************************************/ -int count_river_type_near_tile(const struct tile *ptile, +int count_river_type_near_tile(struct civ_map *nmap, + const struct tile *ptile, const struct extra_type *priver, bool percentage) { @@ -383,7 +390,7 @@ int count_river_type_near_tile(const struct tile *ptile, fc_assert(priver != NULL); - adjc_iterate(&(wld.map), ptile, adjc_tile) { + adjc_iterate(nmap, ptile, adjc_tile) { if (tile_has_extra(adjc_tile, priver)) { count++; } @@ -409,11 +416,12 @@ bool road_has_flag(const struct road_type *proad, enum road_flag_id flag) Returns TRUE iff any cardinally adjacent tile contains a road with the given flag (does not check ptile itself). ****************************************************************************/ -bool is_road_flag_card_near(const struct tile *ptile, enum road_flag_id flag) +bool is_road_flag_card_near(struct civ_map *nmap, const struct tile *ptile, + enum road_flag_id flag) { extra_type_by_cause_iterate(EC_ROAD, pextra) { if (road_has_flag(extra_road_get(pextra), flag)) { - cardinal_adjc_iterate(&(wld.map), ptile, adjc_tile) { + cardinal_adjc_iterate(nmap, ptile, adjc_tile) { if (tile_has_extra(adjc_tile, pextra)) { return TRUE; } @@ -428,11 +436,12 @@ bool is_road_flag_card_near(const struct tile *ptile, enum road_flag_id flag) Returns TRUE iff any adjacent tile contains a road with the given flag (does not check ptile itself). ****************************************************************************/ -bool is_road_flag_near_tile(const struct tile *ptile, enum road_flag_id flag) +bool is_road_flag_near_tile(struct civ_map *nmap, const struct tile *ptile, + enum road_flag_id flag) { extra_type_by_cause_iterate(EC_ROAD, pextra) { if (road_has_flag(extra_road_get(pextra), flag)) { - adjc_iterate(&(wld.map), ptile, adjc_tile) { + adjc_iterate(nmap, ptile, adjc_tile) { if (tile_has_extra(adjc_tile, pextra)) { return TRUE; } diff --git a/common/road.h b/common/road.h index bf4d28a38a..9e648fdccc 100644 --- a/common/road.h +++ b/common/road.h @@ -17,6 +17,10 @@ extern "C" { #endif /* __cplusplus */ +/* common */ +#include "world_object.h" + + /* Used in the network protocol. */ #define SPECENUM_NAME road_flag_id #define SPECENUM_VALUE0 RF_RIVER @@ -105,30 +109,36 @@ struct road_type *road_by_compat_special(enum road_compat compat); struct road_type *road_by_gui_type(enum road_gui_type gui_type); -int count_road_near_tile(const struct tile *ptile, const struct road_type *proad); -int count_river_near_tile(const struct tile *ptile, +int count_road_near_tile(struct civ_map *nmap, const struct tile *ptile, + const struct road_type *proad); +int count_river_near_tile(struct civ_map *nmap, + const struct tile *ptile, const struct extra_type *priver); -int count_river_type_tile_card(const struct tile *ptile, +int count_river_type_tile_card(struct civ_map *nmap, + const struct tile *ptile, const struct extra_type *priver, bool percentage); -int count_river_type_near_tile(const struct tile *ptile, +int count_river_type_near_tile(struct civ_map *nmap, + const struct tile *ptile, const struct extra_type *priver, bool percentage); /* Functions to operate on a road flag. */ bool road_has_flag(const struct road_type *proad, enum road_flag_id flag); -bool is_road_flag_card_near(const struct tile *ptile, +bool is_road_flag_card_near(struct civ_map *nmap, const struct tile *ptile, enum road_flag_id flag); -bool is_road_flag_near_tile(const struct tile *ptile, +bool is_road_flag_near_tile(struct civ_map *nmap, const struct tile *ptile, enum road_flag_id flag); bool road_can_be_built(const struct road_type *proad, const struct tile *ptile); -bool can_build_road(struct road_type *proad, - const struct unit *punit, - const struct tile *ptile); -bool player_can_build_road(const struct road_type *proad, - const struct player *pplayer, - const struct tile *ptile); +bool can_build_road(const struct civ_map *nmap, + struct road_type *proad, + const struct unit *punit, + const struct tile *ptile); +bool player_can_build_road(const struct civ_map *nmap, + const struct road_type *proad, + const struct player *pplayer, + const struct tile *ptile); bool is_native_tile_to_road(const struct road_type *proad, const struct tile *ptile); @@ -164,4 +174,4 @@ void road_types_free(void); } #endif /* __cplusplus */ -#endif /* FC__ROAD_H */ +#endif /* FC__ROAD_H */ diff --git a/common/unitlist.c b/common/unitlist.c index d1c8afe078..85c206194b 100644 --- a/common/unitlist.c +++ b/common/unitlist.c @@ -144,13 +144,14 @@ bool can_units_do_activity_targeted(const struct civ_map *nmap, /************************************************************************//** Returns TRUE if any of the units can build any road. ****************************************************************************/ -bool can_units_do_any_road(const struct unit_list *punits) +bool can_units_do_any_road(const struct civ_map *nmap, + const struct unit_list *punits) { unit_list_iterate(punits, punit) { extra_type_by_cause_iterate(EC_ROAD, pextra) { struct road_type *proad = extra_road_get(pextra); - if (can_build_road(proad, punit, unit_tile(punit))) { + if (can_build_road(nmap, proad, punit, unit_tile(punit))) { return TRUE; } } extra_type_by_cause_iterate_end; diff --git a/common/unitlist.h b/common/unitlist.h index 65f97d0401..3be7b3680e 100644 --- a/common/unitlist.h +++ b/common/unitlist.h @@ -78,7 +78,8 @@ bool can_units_do_activity_targeted(const struct civ_map *nmap, const struct unit_list *punits, enum unit_activity activity, struct extra_type *pextra); -bool can_units_do_any_road(const struct unit_list *punits); +bool can_units_do_any_road(const struct civ_map *nmap, + const struct unit_list *punits); bool can_units_do_base_gui(const struct unit_list *punits, enum base_gui_type base_gui); bool units_have_type_flag(const struct unit_list *punits, diff --git a/server/generator/mapgen.c b/server/generator/mapgen.c index 1989ce8ac9..fb64c0a93e 100644 --- a/server/generator/mapgen.c +++ b/server/generator/mapgen.c @@ -573,7 +573,8 @@ static int river_test_rivergrid(struct river_map *privermap, struct tile *ptile, struct extra_type *priver) { - return (count_river_type_tile_card(ptile, priver, FALSE) > 1) ? 1 : 0; + return (count_river_type_tile_card(&(wld.map), ptile, priver, FALSE) > 1) + ? 1 : 0; } /**********************************************************************//** @@ -603,7 +604,7 @@ static int river_test_adjacent_river(struct river_map *privermap, struct tile *ptile, struct extra_type *priver) { - return 100 - count_river_type_tile_card(ptile, priver, TRUE); + return 100 - count_river_type_tile_card(&(wld.map), ptile, priver, TRUE); } /**********************************************************************//** @@ -801,7 +802,7 @@ static bool make_river(struct river_map *privermap, struct tile *ptile, /* Test if the river is done. */ /* We arbitrarily make rivers end at the poles. */ - if (count_river_near_tile(ptile, priver) > 0 + if (count_river_near_tile(&(wld.map), ptile, priver) > 0 || count_terrain_class_near_tile(ptile, TRUE, TRUE, TC_OCEAN) > 0 || (tile_terrain(ptile)->property[MG_FROZEN] > 0 && map_colatitude(ptile) < 0.8 * COLD_LEVEL)) { @@ -954,7 +955,7 @@ static void make_rivers(void) /* Don't start a river on a tile is surrounded by > 1 river + ocean tile. */ - && (count_river_near_tile(ptile, NULL) + && (count_river_near_tile(&(wld.map), ptile, NULL) + count_terrain_class_near_tile(ptile, TRUE, FALSE, TC_OCEAN) <= 1) /* Don't start a river on a tile that is surrounded by hills or @@ -1731,7 +1732,7 @@ static bool island_river_mouth_suitability(const struct tile *ptile, TC_OCEAN); pct_adj_ocean = count_terrain_class_near_tile(ptile, C_ADJACENT, C_PERCENT, TC_OCEAN); - num_adj_river = count_river_type_tile_card(ptile, priver, FALSE); + num_adj_river = count_river_type_tile_card(&(wld.map), ptile, priver, FALSE); return (num_card_ocean == 1 && pct_adj_ocean <= 35 && num_adj_river == 0); @@ -1746,12 +1747,12 @@ static bool island_river_suitability(const struct tile *ptile, { int pct_adj_ocean, num_card_ocean, pct_adj_river, num_card_river; - num_card_river = count_river_type_tile_card(ptile, priver, FALSE); + num_card_river = count_river_type_tile_card(&(wld.map), ptile, priver, FALSE); num_card_ocean = count_terrain_class_near_tile(ptile, C_CARDINAL, C_NUMBER, TC_OCEAN); pct_adj_ocean = count_terrain_class_near_tile(ptile, C_ADJACENT, C_PERCENT, TC_OCEAN); - pct_adj_river = count_river_type_near_tile(ptile, priver, TRUE); + pct_adj_river = count_river_type_near_tile(&(wld.map), ptile, priver, TRUE); return (num_card_river == 1 && num_card_ocean == 0 && pct_adj_ocean < 20 && pct_adj_river < 35 diff --git a/server/maphand.c b/server/maphand.c index 35cc3db6d1..e8dabf4c70 100644 --- a/server/maphand.c +++ b/server/maphand.c @@ -94,7 +94,7 @@ static bool is_terrain_ecologically_wet(struct tile *ptile) { return (is_terrain_class_near_tile(ptile, TC_OCEAN) || tile_has_river(ptile) - || count_river_near_tile(ptile, NULL) > 0); + || count_river_near_tile(&(wld.map), ptile, NULL) > 0); } /**********************************************************************//** -- 2.42.0