From 43755cb1ab8a98b6e975ec1e97539fae04b06090 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Thu, 15 Jun 2023 07:40:46 +0300 Subject: [PATCH 37/37] Move NATURAL_WIDTH and friends to world_object.h Move and prefix with "MAP_" following macros: - MAP_INDEX_SIZE - NATIVE_WIDTH - NATIVE_HEIGHT - NATURAL_WIDTH - NATURAL_HEIGHT All of them refer directly to the main map, to query properties that currently are assumed to be shared between all active maps. See osdn #48244 Signed-off-by: Marko Lindqvist --- client/gui-qt/mapview.cpp | 4 +-- client/mapview_common.c | 11 ++++--- client/overview_common.c | 49 +++++++++++++-------------- common/map.c | 8 ++--- common/map.h | 69 +++++++++++++++------------------------ common/world_object.h | 18 ++++++++++ 6 files changed, 82 insertions(+), 77 deletions(-) diff --git a/client/gui-qt/mapview.cpp b/client/gui-qt/mapview.cpp index ced8ee57c2..ddfe171b78 100644 --- a/client/gui-qt/mapview.cpp +++ b/client/gui-qt/mapview.cpp @@ -616,14 +616,14 @@ static void gui_to_overview(int *ovr_x, int *ovr_y, int gui_x, int gui_y) * OVERVIEW_TILE_SIZE); if (current_wrap_has_flag(WRAP_X)) { - *ovr_x = FC_WRAP(*ovr_x, NATURAL_WIDTH * OVERVIEW_TILE_SIZE); + *ovr_x = FC_WRAP(*ovr_x, MAP_NATURAL_WIDTH * OVERVIEW_TILE_SIZE); } else { if (MAP_IS_ISOMETRIC) { *ovr_x -= OVERVIEW_TILE_SIZE; } } if (current_wrap_has_flag(WRAP_Y)) { - *ovr_y = FC_WRAP(*ovr_y, NATURAL_HEIGHT * OVERVIEW_TILE_SIZE); + *ovr_y = FC_WRAP(*ovr_y, MAP_NATURAL_HEIGHT * OVERVIEW_TILE_SIZE); } } diff --git a/client/mapview_common.c b/client/mapview_common.c index 44a4726401..ad6c6c7235 100644 --- a/client/mapview_common.c +++ b/client/mapview_common.c @@ -3262,10 +3262,10 @@ static bool can_do_cached_drawing(void) * We divide by 4 below because we have to divide by 2 twice. The * first division by 2 is because the square must be half the size * of the (width+height). The second division by two is because for - * an iso-map, NATURAL_XXX has a scale of 2, whereas for iso-view + * an iso-map, MAP_NATURAL_XXX has a scale of 2, whereas for iso-view * NORMAL_TILE_XXX has a scale of 2. */ - return (w <= (NATURAL_WIDTH + NATURAL_HEIGHT) * W / 4 - && h <= (NATURAL_WIDTH + NATURAL_HEIGHT) * H / 4); + return (w <= (MAP_NATURAL_WIDTH + MAP_NATURAL_HEIGHT) * W / 4 + && h <= (MAP_NATURAL_WIDTH + MAP_NATURAL_HEIGHT) * H / 4); } else { /* Matching. */ const int isofactor = (tileset_is_isometric(tileset) ? 2 : 1); @@ -3274,13 +3274,14 @@ static bool can_do_cached_drawing(void) /* Now we can use the full width and height, with the exception of a small * area on each side. */ if (current_wrap_has_flag(WRAP_X) - && w > (NATURAL_WIDTH - isodiff) * W / isofactor) { + && w > (MAP_NATURAL_WIDTH - isodiff) * W / isofactor) { return FALSE; } if (current_wrap_has_flag(WRAP_Y) - && h > (NATURAL_HEIGHT - isodiff) * H / isofactor) { + && h > (MAP_NATURAL_HEIGHT - isodiff) * H / isofactor) { return FALSE; } + return TRUE; } } diff --git a/client/overview_common.c b/client/overview_common.c index f872a6d3e0..78154616a6 100644 --- a/client/overview_common.c +++ b/client/overview_common.c @@ -73,10 +73,9 @@ static void gui_to_natural_pos(const struct tileset *t, map_y = gui_yd / H; } - /* Now convert to natural positions. Note this assumes the macro form + /* Now convert to natural positions. Note this assumes the macro form * of the conversion will work with floating-point values. */ MAP_TO_NATURAL_POS(ntl_x, ntl_y, map_x, map_y); - } /************************************************************************//** @@ -94,22 +93,22 @@ static void gui_to_overview_pos(const struct tileset *t, *ovr_x = floor((ntl_x - (double)gui_options.overview.map_x0) * OVERVIEW_TILE_SIZE); *ovr_y = floor((ntl_y - (double)gui_options.overview.map_y0) * OVERVIEW_TILE_SIZE); - /* Now do additional adjustments. See map_to_overview_pos(). */ + /* Now do additional adjustments. See map_to_overview_pos(). */ if (current_wrap_has_flag(WRAP_X)) { - *ovr_x = FC_WRAP(*ovr_x, NATURAL_WIDTH * OVERVIEW_TILE_SIZE); + *ovr_x = FC_WRAP(*ovr_x, MAP_NATURAL_WIDTH * OVERVIEW_TILE_SIZE); } else { if (MAP_IS_ISOMETRIC) { /* HACK: For iso-maps that don't wrap in the X direction we clip - * a half-tile off of the left and right of the overview. This - * means some tiles only are halfway shown. However it means we - * don't show any unreal tiles, which we'd otherwise be doing. The - * rest of the code can't handle unreal tiles in the overview. */ + * a half-tile off of the left and right of the overview. + * This means some tiles only are halfway shown. However it means we + * don't show any unreal tiles, which we'd otherwise be doing. + * The rest of the code can't handle unreal tiles in the overview. */ *ovr_x -= OVERVIEW_TILE_SIZE; } } if (current_wrap_has_flag(WRAP_Y)) { - *ovr_y = FC_WRAP(*ovr_y, NATURAL_HEIGHT * OVERVIEW_TILE_SIZE); + *ovr_y = FC_WRAP(*ovr_y, MAP_NATURAL_HEIGHT * OVERVIEW_TILE_SIZE); } } @@ -294,18 +293,20 @@ void center_tile_overviewcanvas(void) mapview.gui_x0 + mapview.width / 2, mapview.gui_y0 + mapview.height / 2); - /* NOTE: this embeds the map wrapping in the overview code. This is + /* NOTE: this embeds the map wrapping in the overview code. This is * basically necessary for the overview to be efficiently * updated. */ if (current_wrap_has_flag(WRAP_X)) { - gui_options.overview.map_x0 = wrap_double(ntl_x - (double)NATURAL_WIDTH / 2.0, - NATURAL_WIDTH); + gui_options.overview.map_x0 + = wrap_double(ntl_x - (double)MAP_NATURAL_WIDTH / 2.0, + MAP_NATURAL_WIDTH); } else { gui_options.overview.map_x0 = 0; } if (current_wrap_has_flag(WRAP_Y)) { - gui_options.overview.map_y0 = wrap_double(ntl_y - (double)NATURAL_HEIGHT / 2.0, - NATURAL_HEIGHT); + gui_options.overview.map_y0 + = wrap_double(ntl_y - (double)MAP_NATURAL_HEIGHT / 2.0, + MAP_NATURAL_HEIGHT); } else { gui_options.overview.map_y0 = 0; } @@ -332,19 +333,19 @@ void map_to_overview_pos(int *overview_x, int *overview_y, int ovr_y = ntl_y - gui_options.overview.map_y0; if (current_wrap_has_flag(WRAP_X)) { - ovr_x = FC_WRAP(ovr_x, NATURAL_WIDTH); + ovr_x = FC_WRAP(ovr_x, MAP_NATURAL_WIDTH); } else { if (MAP_IS_ISOMETRIC) { /* HACK: For iso-maps that don't wrap in the X direction we clip - * a half-tile off of the left and right of the overview. This - * means some tiles only are halfway shown. However it means we - * don't show any unreal tiles, which we'd otherwise be doing. The - * rest of the code can't handle unreal tiles in the overview. */ + * a half-tile off of the left and right of the overview. + * This means some tiles only are halfway shown. However it means we + * don't show any unreal tiles, which we'd otherwise be doing. + * The rest of the code can't handle unreal tiles in the overview. */ ovr_x--; } } if (current_wrap_has_flag(WRAP_Y)) { - ovr_y = FC_WRAP(ovr_y, NATURAL_HEIGHT); + ovr_y = FC_WRAP(ovr_y, MAP_NATURAL_HEIGHT); } *overview_x = OVERVIEW_TILE_SIZE * ovr_x; *overview_y = OVERVIEW_TILE_SIZE * ovr_y; @@ -361,7 +362,7 @@ void overview_to_map_pos(int *map_x, int *map_y, int ntl_y = overview_y / OVERVIEW_TILE_SIZE + gui_options.overview.map_y0; if (MAP_IS_ISOMETRIC && !current_wrap_has_flag(WRAP_X)) { - /* Clip half tile left and right. See comment in map_to_overview_pos. */ + /* Clip half tile left and right. See comment in map_to_overview_pos(). */ ntl_x++; } @@ -430,7 +431,7 @@ void overview_update_tile(struct tile *ptile) } } else { /* Clip half tile left and right. - * See comment in map_to_overview_pos. */ + * See comment in map_to_overview_pos(). */ overview_x -= OVERVIEW_TILE_SIZE; } } @@ -453,7 +454,7 @@ void calculate_overview_dimensions(void) static int recursion = 0; /* Just to be safe. */ - /* Clip half tile left and right. See comment in map_to_overview_pos. */ + /* Clip half tile left and right. See comment in map_to_overview_pos(). */ int shift = (MAP_IS_ISOMETRIC && !current_wrap_has_flag(WRAP_X)) ? -1 : 0; if (recursion > 0 || wld.map.xsize <= 0 || wld.map.ysize <= 0) { @@ -464,7 +465,7 @@ void calculate_overview_dimensions(void) get_overview_area_dimensions(&w, &h); get_overview_area_dimensions(&w, &h); - /* Set the scale of the overview map. This attempts to limit the + /* Set the scale of the overview map. This attempts to limit the * overview to the size of the area available. * * It rounds up since this gives good results with the default settings. diff --git a/common/map.c b/common/map.c index cb52685058..239a17d39b 100644 --- a/common/map.c +++ b/common/map.c @@ -1437,8 +1437,8 @@ static double map_relative_southness(const struct tile *ptile) do_in_natural_pos(ntl_x, ntl_y, tile_x, tile_y) { /* Compute natural coordinates relative to world size. * x and y are in the range [0.0, 1.0] */ - x = (double)ntl_x / (NATURAL_WIDTH - 1); - y = (double)ntl_y / (NATURAL_HEIGHT - 1); + x = (double)ntl_x / (MAP_NATURAL_WIDTH - 1); + y = (double)ntl_y / (MAP_NATURAL_HEIGHT - 1); } do_in_natural_pos_end; if (!current_wrap_has_flag(WRAP_Y)) { @@ -1618,9 +1618,9 @@ bool is_singular_tile(const struct tile *ptile, int dist) dist *= MAP_IS_ISOMETRIC ? 2 : 1; return ((!current_wrap_has_flag(WRAP_X) - && (ntl_x < dist || ntl_x >= NATURAL_WIDTH - dist)) + && (ntl_x < dist || ntl_x >= MAP_NATURAL_WIDTH - dist)) || (!current_wrap_has_flag(WRAP_Y) - && (ntl_y < dist || ntl_y >= NATURAL_HEIGHT - dist))); + && (ntl_y < dist || ntl_y >= MAP_NATURAL_HEIGHT - dist))); } do_in_natural_pos_end; } diff --git a/common/map.h b/common/map.h index 0198cc9c77..73a5a82de8 100644 --- a/common/map.h +++ b/common/map.h @@ -39,11 +39,6 @@ static const bool C_CARDINAL = TRUE; static const bool C_NUMBER = FALSE; static const bool C_PERCENT = TRUE; -#define MAP_IS_ISOMETRIC (CURRENT_TOPOLOGY & (TF_ISO + TF_HEX)) - -#define CURRENT_TOPOLOGY (wld.map.topology_id) -#define CURRENT_WRAP (wld.map.wrap_id) - #define topo_has_flag(topo, flag) (((topo) & (flag)) != 0) #define current_topo_has_flag(flag) topo_has_flag((CURRENT_TOPOLOGY), (flag)) @@ -133,14 +128,12 @@ struct iterator *map_startpos_iter_init(struct map_startpos_iter *iter); #define map_startpos_iterate_end generic_iterate_end -/* Number of index coordinates (for sanity checks and allocations) */ -#define MAP_INDEX_SIZE (wld.map.xsize * wld.map.ysize) - #ifdef FREECIV_DEBUG #define CHECK_MAP_POS(x,y) \ fc_assert(is_normal_map_pos((x),(y))) #define CHECK_NATIVE_POS(x, y) \ - fc_assert((x) >= 0 && (x) < wld.map.xsize && (y) >= 0 && (y) < wld.map.ysize) + fc_assert((x) >= 0 && (x) < MAP_NATIVE_WIDTH && \ + (y) >= 0 && (y) < MAP_NATIVE_HEIGHT) #define CHECK_INDEX(mindex) \ fc_assert((mindex) >= 0 && (mindex) < MAP_INDEX_SIZE) #else /* FREECIV_DEBUG */ @@ -150,7 +143,7 @@ struct iterator *map_startpos_iter_init(struct map_startpos_iter *iter); #endif /* FREECIV_DEBUG */ #define native_pos_to_index_nocheck(nat_x, nat_y) \ - ((nat_x) + (nat_y) * wld.map.xsize) + ((nat_x) + (nat_y) * MAP_NATIVE_WIDTH) #define native_pos_to_index(nat_x, nat_y) \ (CHECK_NATIVE_POS((nat_x), (nat_y)), \ native_pos_to_index_nocheck(nat_x, nat_y)) @@ -158,32 +151,32 @@ struct iterator *map_startpos_iter_init(struct map_startpos_iter *iter); (*(pnat_x) = index_to_native_pos_x(mindex), \ *(pnat_y) = index_to_native_pos_y(mindex)) #define index_to_native_pos_x(mindex) \ - ((mindex) % wld.map.xsize) + ((mindex) % MAP_NATIVE_WIDTH) #define index_to_native_pos_y(mindex) \ - ((mindex) / wld.map.xsize) + ((mindex) / MAP_NATIVE_WIDTH) /* Obscure math. See explanation in doc/HACKING. */ #define NATIVE_TO_MAP_POS(pmap_x, pmap_y, nat_x, nat_y) \ (MAP_IS_ISOMETRIC \ ? (*(pmap_x) = ((nat_y) + ((nat_y) & 1)) / 2 + (nat_x), \ - *(pmap_y) = (nat_y) - *(pmap_x) + wld.map.xsize) \ + *(pmap_y) = (nat_y) - *(pmap_x) + MAP_NATIVE_WIDTH) \ : (*(pmap_x) = (nat_x), *(pmap_y) = (nat_y))) #define MAP_TO_NATIVE_POS(pnat_x, pnat_y, map_x, map_y) \ (MAP_IS_ISOMETRIC \ - ? (*(pnat_y) = (map_x) + (map_y) - wld.map.xsize, \ + ? (*(pnat_y) = (map_x) + (map_y) - MAP_NATIVE_WIDTH, \ *(pnat_x) = (2 * (map_x) - *(pnat_y) - (*(pnat_y) & 1)) / 2) \ : (*(pnat_x) = (map_x), *(pnat_y) = (map_y))) #define NATURAL_TO_MAP_POS(pmap_x, pmap_y, nat_x, nat_y) \ (MAP_IS_ISOMETRIC \ ? (*(pmap_x) = ((nat_y) + (nat_x)) / 2, \ - *(pmap_y) = (nat_y) - *(pmap_x) + wld.map.xsize) \ + *(pmap_y) = (nat_y) - *(pmap_x) + MAP_NATIVE_WIDTH) \ : (*(pmap_x) = (nat_x), *(pmap_y) = (nat_y))) #define MAP_TO_NATURAL_POS(pnat_x, pnat_y, map_x, map_y) \ (MAP_IS_ISOMETRIC \ - ? (*(pnat_y) = (map_x) + (map_y) - wld.map.xsize, \ + ? (*(pnat_y) = (map_x) + (map_y) - MAP_NATIVE_WIDTH, \ *(pnat_x) = 2 * (map_x) - *(pnat_y)) \ : (*(pnat_x) = (map_x), *(pnat_y) = (map_y))) @@ -218,14 +211,6 @@ struct iterator *map_startpos_iter_init(struct map_startpos_iter *iter); } \ } -/* Width and height of the map, in native coordinates. */ -#define NATIVE_WIDTH wld.map.xsize -#define NATIVE_HEIGHT wld.map.ysize - -/* Width and height of the map, in natural coordinates. */ -#define NATURAL_WIDTH (MAP_IS_ISOMETRIC ? 2 * wld.map.xsize : wld.map.xsize) -#define NATURAL_HEIGHT wld.map.ysize - static inline int map_pos_to_index(struct civ_map *nmap, int map_x, int map_y); @@ -327,10 +312,10 @@ bool terrain_surroundings_allow_change(const struct tile *ptile, extern struct terrain_misc terrain_control; -/* This iterates outwards from the starting point. Every tile within max_dist +/* This iterates outwards from the starting point. Every tile within max_dist * (including the starting tile) will show up exactly once, in an outward - * (based on real map distance) order. The returned values are always real - * and are normalized. The starting position must be normal. + * (based on real map distance) order. The returned values are always real + * and are normalized. The starting position must be normal. * * See also iterate_outward() */ #define iterate_outward_dxy(nmap, start_tile, max_dist, _tile, _x, _y) \ @@ -392,7 +377,7 @@ extern struct terrain_misc terrain_control; /* * Iterate through all tiles in a circle with given center and squared - * radius. Positions returned will have adjusted (x, y); unreal + * radius. Positions returned will have adjusted (x, y); unreal * positions will be automatically discarded. */ #define circle_iterate(nmap, center_tile, sq_radius, tile_itr) \ @@ -420,7 +405,7 @@ extern struct terrain_misc terrain_control; } /* Iterate itr_tile through all map tiles adjacent to the given center map - * position, with normalization. Does not include the center position. + * position, with normalization. Does not include the center position. * The order of positions is unspecified. */ #define adjc_iterate(nmap, center_tile, itr_tile) \ { \ @@ -448,8 +433,8 @@ extern struct terrain_misc terrain_control; adjc_dirlist_base_iterate_end /* Iterate itr_tile through all map tiles cardinally adjacent to the given - * center map position, with normalization. Does not include the center - * position. The order of positions is unspecified. */ + * center map position, with normalization. Does not include the center + * position. The order of positions is unspecified. */ #define cardinal_adjc_iterate(nmap, center_tile, itr_tile) \ adjc_dirlist_iterate(nmap, center_tile, itr_tile, _dir_itr##itr_tile, \ wld.map.cardinal_dirs, wld.map.num_cardinal_dirs) @@ -483,11 +468,11 @@ extern struct terrain_misc terrain_control; } cardinal_adjc_iterate_end; /* Iterate through all tiles adjacent to a tile using the given list of - * directions. _dir is the directional value, (center_x, center_y) is - * the center tile (which must be normalized). The center tile is not + * directions. _dir is the directional value, (center_x, center_y) is + * the center tile (which must be normalized). The center tile is not * included in the iteration. * - * This macro should not be used directly. Instead, use adjc_iterate, + * This macro should not be used directly. Instead, use adjc_iterate, * cardinal_adjc_iterate, or related iterators. */ #define adjc_dirlist_iterate(nmap, center_tile, _tile, _dir, \ dirlist, dircount) \ @@ -555,7 +540,7 @@ extern struct terrain_misc terrain_control; BV_DEFINE(dir_vector, 8); -/* return the reverse of the direction */ +/* Return the reverse of the direction */ #define DIR_REVERSE(dir) (7 - (dir)) enum direction8 dir_cw(enum direction8 dir); @@ -696,7 +681,7 @@ moves. Includes MAP_MAX_LINEAR_SIZE because a map can be non wrapping. */ #define MAP_DEFAULT_TEAM_PLACEMENT TEAM_PLACEMENT_CLOSEST /* - * Inline function definitions. These are at the bottom because they may use + * Inline function definitions. These are at the bottom because they may use * elements defined above. */ @@ -730,13 +715,13 @@ static inline int index_to_map_pos_y(int mindex) /**************************************************************************** A "border position" is any map position that _may have_ positions within - real map distance dist that are non-normal. To see its correctness, + real map distance dist that are non-normal. To see its correctness, consider the case where dist is 1 or 0. ****************************************************************************/ static inline bool is_border_tile(const struct tile *ptile, int dist) { /* HACK: An iso-map compresses the value in the X direction but not in - * the Y direction. Hence (x+1,y) is 1 tile away while (x,y+2) is also + * the Y direction. Hence (x+1,y) is 1 tile away while (x,y+2) is also * one tile away. */ int xdist = dist; int ydist = (MAP_IS_ISOMETRIC ? (2 * dist) : dist); @@ -744,10 +729,10 @@ static inline bool is_border_tile(const struct tile *ptile, int dist) index_to_native_pos(&nat_x, &nat_y, tile_index(ptile)); - return (nat_x < xdist + return (nat_x < xdist || nat_y < ydist - || nat_x >= wld.map.xsize - xdist - || nat_y >= wld.map.ysize - ydist); + || nat_x >= MAP_NATIVE_WIDTH - xdist + || nat_y >= MAP_NATIVE_HEIGHT - ydist); } enum direction8 rand_direction(void); @@ -757,4 +742,4 @@ enum direction8 opposite_direction(enum direction8 dir); } #endif /* __cplusplus */ -#endif /* FC__MAP_H */ +#endif /* FC__MAP_H */ diff --git a/common/world_object.h b/common/world_object.h index 2bd51c3bcf..3fd7a29d92 100644 --- a/common/world_object.h +++ b/common/world_object.h @@ -42,6 +42,24 @@ struct world extern struct world wld; /* In game.c */ + +#define MAP_IS_ISOMETRIC (CURRENT_TOPOLOGY & (TF_ISO + TF_HEX)) + +#define CURRENT_TOPOLOGY (wld.map.topology_id) +#define CURRENT_WRAP (wld.map.wrap_id) + +/* Number of index coordinates (for sanity checks and allocations) */ +#define MAP_INDEX_SIZE (wld.map.xsize * wld.map.ysize) + +/* Width and height of the map, in native coordinates. */ +#define MAP_NATIVE_WIDTH wld.map.xsize +#define MAP_NATIVE_HEIGHT wld.map.ysize + +/* Width and height of the map, in natural coordinates. */ +#define MAP_NATURAL_WIDTH (MAP_IS_ISOMETRIC ? 2 * wld.map.xsize : wld.map.xsize) +#define MAP_NATURAL_HEIGHT wld.map.ysize + + #ifdef __cplusplus } #endif /* __cplusplus */ -- 2.39.2