[Freeciv-Dev] Re: (PR#4718) topology cleanup for client autocenter code
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Actually, the correct solution is probably even easier and certainly less
code change impact.
I can't actually fathom why you are so in love with this anti-pattern,
that you would do a whole_map_iterate with sqr test for something
that seldom requires more than a handful of iterations at most with an
appropriate iterator.
Cheers,
RossW
=====
/* Just any known tile will do; search near the middle first. */
+ int x0, y0, xm = map.xsize/2, ym = map.ysize/2;
+ native_to_map_pos(&x0, &y0, xm, ym);
+ iterate_outward(x0, y0, xm + ym, x, y) {
- iterate_outward(map.xsize / 2, map.ysize / 2,
- MAX(map.xsize / 2, map.ysize / 2), x, y) {
if (tile_get_known(x, y) != TILE_UNKNOWN) {
center_tile_mapcanvas(x, y);
goto OUT;
} iterate_outward_end;
/* If we get here we didn't find a known tile.
Refresh a random place to clear the intro gfx. */
center_tile_mapcanvas(map.xsize / 2, map.ysize / 2);
OUT:
; /* do nothing */
Jason Short wrote:
> The code for centering the mapview when the client first connects has
> lots of fallbacks.
>
> Currently the second-to-last fallback is to iterate outwards around the
> center position on the map to find any known tile, and center on that.
>
> The last fallback is just to center the mapview on the center tile of
> the map.
>
> The problem is (map.xsize/2, map.ysize/2) is not the center map position
> of the map under gen-topologies: it is a native position. And
> iterate_outward can't take map.xsize/2, map.ysize/2 as its dimensions
> since these are native, not map, dimensions.
>
> The solution is pretty easy; just use native_to_map_pos to do the
> conversion and whole_map_iterate instead of iterate_outwards.
>
> An alternative is to have functions to return the dimensions of the map
> in map coordinates. But these functions generally have little use and
> are best avoided IMO.
>
> jason
>
>
>
> ------------------------------------------------------------------------
>
> Index: client/climisc.c
> ===================================================================
> RCS file: /home/freeciv/CVS/freeciv/client/climisc.c,v
> retrieving revision 1.119
> diff -u -r1.119 climisc.c
> --- client/climisc.c 2003/07/28 04:00:29 1.119
> +++ client/climisc.c 2003/07/29 17:08:09
> @@ -359,20 +359,28 @@
> assert(punit != NULL);
> center_tile_mapcanvas(punit->x, punit->y);
> } else {
> - /* Just any known tile will do; search near the middle first. */
> - iterate_outward(map.xsize / 2, map.ysize / 2,
> - MAX(map.xsize / 2, map.ysize / 2), x, y) {
> + int map_x, map_y, center_map_x, center_map_y, dist = -1;
> +
> + /* Find the center position on the map. */
> + native_to_map_pos(&map_x, &map_y, map.xsize / 2, map.ysize / 2);
> + center_map_x = map_x;
> + center_map_y = map_y;
> +
> + /* Look for any known tile - preferably close to the map center. The
> + * default is the map center. */
> + whole_map_iterate(x, y) {
> if (tile_get_known(x, y) != TILE_UNKNOWN) {
> - center_tile_mapcanvas(x, y);
> - goto OUT;
> + int mydist = sq_map_distance(x, y, center_map_x, center_map_y);
> +
> + if (dist == -1 || mydist < dist) {
> + dist = mydist;
> + map_x = x;
> + map_y = y;
> + }
> }
> - }
> - iterate_outward_end;
> - /* If we get here we didn't find a known tile.
> - Refresh a random place to clear the intro gfx. */
> - center_tile_mapcanvas(map.xsize / 2, map.ysize / 2);
> - OUT:
> - ; /* do nothing */
> + } whole_map_iterate_end;
> +
> + center_tile_mapcanvas(map_x, map_y);
> }
> }
>
|
|