[Freeciv-Dev] (PR#9879) macro capitalization
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
<URL: http://rt.freeciv.org/Ticket/Display.html?id=9879 >
The attached patch capitalizes the native_to_map_pos, map_to_native_pos,
map_to_natural_pos, and natural_to_map_pos macros. According to the
macro guidelines this is needed because they evaluate their parameters
multiple times. It's also safer since with the current names this isn't
obvious and it can lead to bugs.
It will quickly go out of date. It can easily be reproduced though with
the attached script and
replace.sh native_to_map_pos NATIVE_TO_MAP_POS
replace.sh map_to_native_pos MAP_TO_NATIVE_POS
replace.sh natural_to_map_pos NATURAL_TO_MAP_POS
replace.sh map_to_natural_pos MAP_TO_NATURAL_POS
I didn't check the callers. However I'm sure some of them will need to
be changed, particularly when the caller is itself a macro.
jason
Index: client/climisc.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/climisc.c,v
retrieving revision 1.136
diff -u -r1.136 climisc.c
--- client/climisc.c 28 Aug 2004 19:15:39 -0000 1.136
+++ client/climisc.c 31 Aug 2004 04:00:09 -0000
@@ -373,7 +373,7 @@
/* Just any known tile will do; search near the middle first. */
int center_map_x, center_map_y;
- native_to_map_pos(¢er_map_x, ¢er_map_y,
+ NATIVE_TO_MAP_POS(¢er_map_x, ¢er_map_y,
map.xsize / 2, map.ysize / 2);
/* Iterate outward from the center tile. We have to give a radius that
Index: client/mapview_common.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/mapview_common.c,v
retrieving revision 1.142
diff -u -r1.142 mapview_common.c
--- client/mapview_common.c 28 Aug 2004 19:15:39 -0000 1.142
+++ client/mapview_common.c 31 Aug 2004 04:00:09 -0000
@@ -318,14 +318,14 @@
/* Perform wrapping without any realness check. It's important that
* we wrap even if the map position is unreal, which normalize_map_pos
* doesn't necessarily do. */
- map_to_native_pos(&nat_x, &nat_y, map_x, map_y);
+ MAP_TO_NATIVE_POS(&nat_x, &nat_y, map_x, map_y);
if (topo_has_flag(TF_WRAPX)) {
nat_x = FC_WRAP(nat_x, map.xsize);
}
if (topo_has_flag(TF_WRAPY)) {
nat_y = FC_WRAP(nat_y, map.ysize);
}
- native_to_map_pos(&map_x, &map_y, nat_x, nat_y);
+ NATIVE_TO_MAP_POS(&map_x, &map_y, nat_x, nat_y);
/* Now convert the wrapped map position back to a GUI position and add the
* offset back on. */
@@ -520,10 +520,10 @@
if (topo_has_flag(TF_ISO) == is_isometric) {
/* If the map and view line up, it's easy. */
- native_to_map_pos(xmin, ymin, 0, 0);
+ NATIVE_TO_MAP_POS(xmin, ymin, 0, 0);
map_to_gui_pos(xmin, ymin, *xmin, *ymin);
- native_to_map_pos(xmax, ymax, map.xsize - 1, map.ysize - 1);
+ NATIVE_TO_MAP_POS(xmax, ymax, map.xsize - 1, map.ysize - 1);
map_to_gui_pos(xmax, ymax, *xmax, *ymax);
*xmax += NORMAL_TILE_WIDTH;
*ymax += NORMAL_TILE_HEIGHT;
@@ -552,16 +552,16 @@
int gui_x1, gui_y1, gui_x2, gui_y2, gui_x3, gui_y3, gui_x4, gui_y4;
int map_x, map_y;
- native_to_map_pos(&map_x, &map_y, 0, 0);
+ NATIVE_TO_MAP_POS(&map_x, &map_y, 0, 0);
map_to_gui_pos(&gui_x1, &gui_y1, map_x, map_y);
- native_to_map_pos(&map_x, &map_y, map.xsize - 1, 0);
+ NATIVE_TO_MAP_POS(&map_x, &map_y, map.xsize - 1, 0);
map_to_gui_pos(&gui_x2, &gui_y2, map_x, map_y);
- native_to_map_pos(&map_x, &map_y, 0, map.ysize - 1);
+ NATIVE_TO_MAP_POS(&map_x, &map_y, 0, map.ysize - 1);
map_to_gui_pos(&gui_x3, &gui_y3, map_x, map_y);
- native_to_map_pos(&map_x, &map_y, map.xsize - 1, map.ysize - 1);
+ NATIVE_TO_MAP_POS(&map_x, &map_y, map.xsize - 1, map.ysize - 1);
map_to_gui_pos(&gui_x4, &gui_y4, map_x, map_y);
*xmin = MIN(gui_x1, MIN(gui_x2, gui_x3)) - mapview_canvas.width / 2;
@@ -2310,7 +2310,7 @@
ntl_x++;
}
- natural_to_map_pos(map_x, map_y, ntl_x, ntl_y);
+ NATURAL_TO_MAP_POS(map_x, map_y, ntl_x, ntl_y);
if (!normalize_map_pos(map_x, map_y)) {
/* All positions on the overview should be valid. */
assert(FALSE);
Index: common/map.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/map.c,v
retrieving revision 1.188
diff -u -r1.188 map.c
--- common/map.c 25 Aug 2004 18:24:19 -0000 1.188
+++ common/map.c 31 Aug 2004 04:00:10 -0000
@@ -241,7 +241,7 @@
* the center of the map to make the min/max values (below) simpler. */
nat_center_x = map.xsize / 2;
nat_center_y = map.ysize / 2;
- native_to_map_pos(&map_center_x, &map_center_y,
+ NATIVE_TO_MAP_POS(&map_center_x, &map_center_y,
nat_center_x, nat_center_y);
/* If we wrap in a particular direction (X or Y) we only need to explore a
@@ -283,7 +283,7 @@
* distance between the two points. Wrapping is ignored at this
* point since the use of native positions means we should always have
* the shortest vector. */
- native_to_map_pos(&map_x, &map_y, nat_x, nat_y);
+ NATIVE_TO_MAP_POS(&map_x, &map_y, nat_x, nat_y);
dx = map_x - map_center_x;
dy = map_y - map_center_y;
@@ -1429,7 +1429,7 @@
int nat_x, nat_y;
/* Normalization is best done in native coordinatees. */
- map_to_native_pos(&nat_x, &nat_y, *x, *y);
+ MAP_TO_NATIVE_POS(&nat_x, &nat_y, *x, *y);
/* If the position is out of range in a non-wrapping direction, it is
* unreal. */
@@ -1447,7 +1447,7 @@
}
/* Now transform things back to map coordinates. */
- native_to_map_pos(x, y, nat_x, nat_y);
+ NATIVE_TO_MAP_POS(x, y, nat_x, nat_y);
return TRUE;
}
@@ -1459,14 +1459,14 @@
{
int nat_x, nat_y;
- map_to_native_pos(&nat_x, &nat_y, *x, *y);
+ MAP_TO_NATIVE_POS(&nat_x, &nat_y, *x, *y);
if (!topo_has_flag(TF_WRAPX)) {
nat_x = CLIP(0, nat_x, map.xsize - 1);
}
if (!topo_has_flag(TF_WRAPY)) {
nat_y = CLIP(0, nat_y, map.ysize - 1);
}
- native_to_map_pos(x, y, nat_x, nat_y);
+ NATIVE_TO_MAP_POS(x, y, nat_x, nat_y);
if (!normalize_map_pos(x, y)) {
assert(FALSE);
@@ -1501,8 +1501,8 @@
{
if (topo_has_flag(TF_WRAPX) || topo_has_flag(TF_WRAPY)) {
/* Wrapping is done in native coordinates. */
- map_to_native_pos(&x0, &y0, x0, y0);
- map_to_native_pos(&x1, &y1, x1, y1);
+ MAP_TO_NATIVE_POS(&x0, &y0, x0, y0);
+ MAP_TO_NATIVE_POS(&x1, &y1, x1, y1);
/* Find the "native" distance vector. This corresponds closely to the
* map distance vector but is easier to wrap. */
@@ -1520,8 +1520,8 @@
/* Convert the native delta vector back to a pair of map positions. */
x1 = x0 + *dx;
y1 = y0 + *dy;
- native_to_map_pos(&x0, &y0, x0, y0);
- native_to_map_pos(&x1, &y1, x1, y1);
+ NATIVE_TO_MAP_POS(&x0, &y0, x0, y0);
+ NATIVE_TO_MAP_POS(&x1, &y1, x1, y1);
}
/* Find the final (map) vector. */
@@ -1570,8 +1570,8 @@
{
int nat_x = myrand(map.xsize), nat_y = myrand(map.ysize);
- /* Don't pass non-deterministic expressions to native_to_map_pos! */
- native_to_map_pos(x, y, nat_x, nat_y);
+ /* Don't pass non-deterministic expressions to NATIVE_TO_MAP_POS! */
+ NATIVE_TO_MAP_POS(x, y, nat_x, nat_y);
CHECK_MAP_POS(*x, *y);
}
Index: common/map.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/map.h,v
retrieving revision 1.210
diff -u -r1.210 map.h
--- common/map.h 26 Aug 2004 18:37:52 -0000 1.210
+++ common/map.h 31 Aug 2004 04:00:10 -0000
@@ -246,25 +246,25 @@
*(pnat_y) = (index) / map.xsize)
/* Obscure math. See explanation in doc/HACKING. */
-#define native_to_map_pos(pmap_x, pmap_y, nat_x, nat_y) \
+#define NATIVE_TO_MAP_POS(pmap_x, pmap_y, nat_x, nat_y) \
((topo_has_flag(TF_ISO) || topo_has_flag(TF_HEX)) \
? (*(pmap_x) = ((nat_y) + ((nat_y) & 1)) / 2 + (nat_x), \
*(pmap_y) = (nat_y) - *(pmap_x) + map.xsize) \
: (*(pmap_x) = (nat_x), *(pmap_y) = (nat_y)))
-#define map_to_native_pos(pnat_x, pnat_y, map_x, map_y) \
+#define MAP_TO_NATIVE_POS(pnat_x, pnat_y, map_x, map_y) \
((topo_has_flag(TF_ISO) || topo_has_flag(TF_HEX)) \
? (*(pnat_y) = (map_x) + (map_y) - map.xsize, \
*(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) \
+#define NATURAL_TO_MAP_POS(pmap_x, pmap_y, nat_x, nat_y) \
(topo_has_flag(TF_ISO) \
? (*(pmap_x) = ((nat_y) + (nat_x)) / 2, \
*(pmap_y) = (nat_y) - *(pmap_x) + map.xsize) \
: (*(pmap_x) = (nat_x), *(pmap_y) = (nat_y)))
-#define map_to_natural_pos(pnat_x, pnat_y, map_x, map_y) \
+#define MAP_TO_NATURAL_POS(pnat_x, pnat_y, map_x, map_y) \
(topo_has_flag(TF_ISO) \
? (*(pnat_y) = (map_x) + (map_y) - map.xsize, \
*(pnat_x) = 2 * (map_x) - *(pnat_y)) \
@@ -278,7 +278,7 @@
#define do_in_native_pos(nat_x, nat_y, map_x, map_y) \
{ \
int _nat_x, _nat_y; \
- map_to_native_pos(&_nat_x, &_nat_y, map_x, map_y); \
+ MAP_TO_NATIVE_POS(&_nat_x, &_nat_y, map_x, map_y); \
{ \
const int nat_x = _nat_x, nat_y = _nat_y;
@@ -293,7 +293,7 @@
#define do_in_natural_pos(ntl_x, ntl_y, map_x, map_y) \
{ \
int _ntl_x, _ntl_y; \
- map_to_natural_pos(&_ntl_x, &_ntl_y, map_x, map_y); \
+ MAP_TO_NATURAL_POS(&_ntl_x, &_ntl_y, map_x, map_y); \
{ \
const int ntl_x = _ntl_x, ntl_y = _ntl_y;
@@ -324,7 +324,7 @@
#define index_to_map_pos(pmap_x, pmap_y, index) \
(CHECK_INDEX(index), \
index_to_native_pos(pmap_x, pmap_y, index), \
- native_to_map_pos(pmap_x, pmap_y, *(pmap_x), *(pmap_y)))
+ NATIVE_TO_MAP_POS(pmap_x, pmap_y, *(pmap_x), *(pmap_y)))
#define DIRSTEP(dest_x, dest_y, dir) \
( (dest_x) = DIR_DX[(dir)], \
@@ -689,7 +689,7 @@
int nat_x, nat_y;
CHECK_MAP_POS(map_x, map_y);
- map_to_native_pos(&nat_x, &nat_y, map_x, map_y);
+ MAP_TO_NATIVE_POS(&nat_x, &nat_y, map_x, map_y);
return native_pos_to_index(nat_x, nat_y);
}
Index: server/gamelog.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/gamelog.c,v
retrieving revision 1.33
diff -u -r1.33 gamelog.c
--- server/gamelog.c 18 Jul 2004 04:08:21 -0000 1.33
+++ server/gamelog.c 31 Aug 2004 04:00:11 -0000
@@ -117,7 +117,7 @@
for (nat_y = 0; nat_y < map.ysize; nat_y++) {
for (nat_x = 0; nat_x < map.xsize; nat_x++) {
- native_to_map_pos(&map_x, &map_y, nat_x, nat_y);
+ NATIVE_TO_MAP_POS(&map_x, &map_y, nat_x, nat_y);
hline[nat_x] = is_ocean(map_get_terrain(map_x, map_y)) ? ' ' : '.';
}
gamelog(GAMELOG_MAP, "%s", hline);
Index: server/mapgen.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/mapgen.c,v
retrieving revision 1.149
diff -u -r1.149 mapgen.c
--- server/mapgen.c 28 Aug 2004 16:50:49 -0000 1.149
+++ server/mapgen.c 31 Aug 2004 04:00:11 -0000
@@ -43,7 +43,7 @@
#define do_in_map_pos(map_x, map_y, nat_x, nat_y) \
{ \
int map_x, map_y; \
- native_to_map_pos(&map_x, &map_y, nat_x, nat_y); \
+ NATIVE_TO_MAP_POS(&map_x, &map_y, nat_x, nat_y); \
{ \
#define do_in_map_pos_end \
@@ -450,7 +450,7 @@
{
int nat_x, nat_y, T;
- map_to_native_pos(&nat_x, &nat_y, map_x, map_y);
+ MAP_TO_NATIVE_POS(&nat_x, &nat_y, map_x, map_y);
T = map_temperature(map_x, map_y);
if (not_placed(map_x, map_y)) {
if (T > 8 * MAX_TEMP / 10
@@ -1876,7 +1876,7 @@
xn = pstate->w + myrand(pstate->e - pstate->w);
yn = pstate->n + myrand(pstate->s - pstate->n);
- native_to_map_pos(x, y, xn, yn);
+ NATIVE_TO_MAP_POS(x, y, xn, yn);
if (!normalize_map_pos(x, y)) {
die("Invalid map operation.");
}
@@ -2012,7 +2012,7 @@
int xo, yo;
rand_map_pos(&xo, &yo);
- map_to_native_pos(&xon, &yon, xo, yo);
+ MAP_TO_NATIVE_POS(&xon, &yon, xo, yo);
}
/* this helps a lot for maps with high landmass */
@@ -2021,7 +2021,7 @@
yn++, xn++) {
int map_x, map_y;
- native_to_map_pos(&map_x, &map_y,
+ NATIVE_TO_MAP_POS(&map_x, &map_y,
xn + xon - pstate->w, yn + yon - pstate->n);
if (!normalize_map_pos(&map_x, &map_y)) {
@@ -2036,7 +2036,7 @@
for (xn = pstate->w; xn < pstate->e; xn++) {
int map_x, map_y;
- native_to_map_pos(&map_x, &map_y,
+ NATIVE_TO_MAP_POS(&map_x, &map_y,
xn + xon - pstate->w, yn + yon - pstate->n);
if (!normalize_map_pos(&map_x, &map_y)) {
@@ -2054,7 +2054,7 @@
int map_x, map_y;
bool is_real;
- native_to_map_pos(&map_x, &map_y,
+ NATIVE_TO_MAP_POS(&map_x, &map_y,
xn + xon - pstate->w, yn + yon - pstate->n);
is_real = normalize_map_pos(&map_x, &map_y);
@@ -2115,7 +2115,7 @@
i = islemass - 1;
while (i > 0 && tries-->0) {
get_random_map_position_from_state(&x, &y, pstate);
- map_to_native_pos(&xn, &yn, x, y);
+ MAP_TO_NATIVE_POS(&xn, &yn, x, y);
if ((!near_singularity(x, y) || myrand(50) < 25 )
&& hmap(x, y) == 0 && count_card_adjc_elevated_tiles(x, y) > 0) {
hmap(x, y) = 1;
@@ -2136,7 +2136,7 @@
if (i < islemass / 10) {
for (yn = pstate->n; yn < pstate->s; yn++) {
for (xn = pstate->w; xn < pstate->e; xn++) {
- native_to_map_pos(&x, &y, xn, yn);
+ NATIVE_TO_MAP_POS(&x, &y, xn, yn);
if (hnat(xn, yn) == 0 && i > 0
&& count_card_adjc_elevated_tiles(x, y) == 4) {
hnat(xn, yn) = 1;
Index: server/savegame.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/savegame.c,v
retrieving revision 1.182
diff -u -r1.182 savegame.c
--- server/savegame.c 29 Aug 2004 17:42:21 -0000 1.182
+++ server/savegame.c 31 Aug 2004 04:00:12 -0000
@@ -79,7 +79,7 @@
\
for (nat_y = 0; nat_y < map.ysize; nat_y++) { \
for (nat_x = 0; nat_x < map.xsize; nat_x++) { \
- native_to_map_pos(&map_x, &map_y, nat_x, nat_y); \
+ NATIVE_TO_MAP_POS(&map_x, &map_y, nat_x, nat_y); \
line[nat_x] = (GET_XY_CHAR); \
if (!my_isprint(line[nat_x] & 0x7f)) { \
die("Trying to write invalid map " \
@@ -165,7 +165,7 @@
int map_x, map_y; \
const char ch = _line[nat_x]; \
\
- native_to_map_pos(&map_x, &map_y, nat_x, nat_y); \
+ NATIVE_TO_MAP_POS(&map_x, &map_y, nat_x, nat_y); \
(SET_XY_CHAR); \
} \
} \
@@ -336,7 +336,7 @@
nat_x = secfile_lookup_int(file, "map.r%dsx", i);
nat_y = secfile_lookup_int(file, "map.r%dsy", i);
- native_to_map_pos(&map.start_positions[i].x, &map.start_positions[i].y,
+ NATIVE_TO_MAP_POS(&map.start_positions[i].x, &map.start_positions[i].y,
nat_x, nat_y);
if (nation) {
@@ -390,7 +390,7 @@
struct tile *ptile = map_get_tile(mx, my);
int nx, ny;
- map_to_native_pos(&nx, &ny, mx, my);
+ MAP_TO_NATIVE_POS(&nx, &ny, mx, my);
ptile->spec_sprite = secfile_lookup_str_default(file, NULL,
"map.spec_sprite_%d_%d", nx, ny);
@@ -579,7 +579,7 @@
if (ptile->spec_sprite) {
int nx, ny;
- map_to_native_pos(&nx, &ny, mx, my);
+ MAP_TO_NATIVE_POS(&nx, &ny, mx, my);
secfile_insert_str(file, ptile->spec_sprite,
"map.spec_sprite_%d_%d", nx, ny);
}
@@ -1250,7 +1250,7 @@
nat_x = secfile_lookup_int(file, "player%d.u%d.x", plrno, i);
nat_y = secfile_lookup_int(file, "player%d.u%d.y", plrno, i);
- native_to_map_pos(&punit->x, &punit->y, nat_x, nat_y);
+ NATIVE_TO_MAP_POS(&punit->x, &punit->y, nat_x, nat_y);
punit->foul
= secfile_lookup_bool_default(file, FALSE, "player%d.u%d.foul",
@@ -1303,7 +1303,7 @@
int nat_y = secfile_lookup_int(file, "player%d.u%d.goto_y", plrno, i);
int map_x, map_y;
- native_to_map_pos(&map_x, &map_y, nat_x, nat_y);
+ NATIVE_TO_MAP_POS(&map_x, &map_y, nat_x, nat_y);
set_goto_dest(punit, map_x, map_y);
} else {
clear_goto_dest(punit);
@@ -1746,7 +1746,7 @@
const char* name;
int id, k;
- native_to_map_pos(&map_x, &map_y, nat_x, nat_y);
+ NATIVE_TO_MAP_POS(&map_x, &map_y, nat_x, nat_y);
pcity = create_city_virtual(plr, map_x, map_y,
secfile_lookup_str(file, "player%d.c%d.name", plrno, i));
@@ -2107,7 +2107,7 @@
pdcity->id = secfile_lookup_int(file, "player%d.dc%d.id", plrno, j);
nat_x = secfile_lookup_int(file, "player%d.dc%d.x", plrno, j);
nat_y = secfile_lookup_int(file, "player%d.dc%d.y", plrno, j);
- native_to_map_pos(&map_x, &map_y, nat_x, nat_y);
+ NATIVE_TO_MAP_POS(&map_x, &map_y, nat_x, nat_y);
sz_strlcpy(pdcity->name, secfile_lookup_str(file, "player%d.dc%d.name",
plrno, j));
pdcity->size = secfile_lookup_int(file, "player%d.dc%d.size", plrno, j);
pdcity->has_walls = secfile_lookup_bool(file,
"player%d.dc%d.has_walls", plrno, j);
Index: server/score.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/score.c,v
retrieving revision 1.5
diff -u -r1.5 score.c
--- server/score.c 27 May 2004 22:14:19 -0000 1.5
+++ server/score.c 31 Aug 2004 04:00:12 -0000
@@ -88,7 +88,7 @@
printf("%d ", nat_y % 10); \
for (nat_x = 0; nat_x < map.xsize; nat_x++) { \
int x, y; \
- native_to_map_pos(&x, &y, nat_x,nat_y); \
+ NATIVE_TO_MAP_POS(&x, &y, nat_x,nat_y); \
printf(type, map_char_expr); \
} \
printf(" %d\n", nat_y % 10); \
replace.sh
Description: Bourne shell script
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Freeciv-Dev] (PR#9879) macro capitalization,
Jason Short <=
|
|