Complete.Org: Mailing Lists: Archives: freeciv-dev: September 2001:
[Freeciv-Dev] Re: [Patch] Map coord. to city map transformation and bac
Home

[Freeciv-Dev] Re: [Patch] Map coord. to city map transformation and bac

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: rf13@xxxxxxxxxxxxxxxxxxxxxx
Cc: freeciv development list <freeciv-dev@xxxxxxxxxxx>
Subject: [Freeciv-Dev] Re: [Patch] Map coord. to city map transformation and back
From: "Ross W. Wetmore" <rwetmore@xxxxxxxxxxxx>
Date: Wed, 26 Sep 2001 00:17:52 -0400

These utility routines are truly messterpieces of obfuscation. Are
you sure you couldn't have added a few more levels of function calls
to transform the argument type and order a couple more times. I like
cup and pea games.

And you've only used half a line for the function names and nothing
like that for the arguments. The fact is that you declared this
function in only 3 lines of code when clearly you could have done much
better than that. Why not "local_nearest_to_real_close_by_city_map_x" as
a replacement variable name to keep the typing fingers warm in winter
as well as making sure that people really get the message.

It is also the case that one can get rid of the O(n) iteration in 
get_citymap_xy() (sample below, was in corecleanup_07e), but perhaps you 
would rather add some sort of loop to the other routines to balance 
things out more elegantly.

All I can say is .... WOW (Where O Where ...) :-)

Actually, other than this stuff it was pretty good.

Cheers,
RossW
=====


/**************************************************************************
...
**************************************************************************/
int is_valid_city_coords(const int city_x, const int city_y)
{
  if ((city_x == 0 || city_x == CITY_MAP_SIZE-1)
   && (city_y == 0 || city_y == CITY_MAP_SIZE-1))
    return 0;
  if ( !RANGE_CHECK_0(CITY_MAP_SIZE, city_x)
    || !RANGE_CHECK_0(CITY_MAP_SIZE, city_y) )
    return 0;

  return 1;
}

/**************************************************************************
Given a real map position and a city, finds the city map coordinates of the
tiles.
Returns TRUE (== 1) if the real map position was inside the city map.
**************************************************************************/
int get_citymap_xy(const struct city *pcity, const int x, const int y,
           int *city_x, int *city_y)
{
  /* Get x,y relative to the NW corner of pcity */
  *city_x= (x - pcity->x + CITY_MAP_SIZE/2);
  *city_y= (y - pcity->y + CITY_MAP_SIZE/2);

  /* If coords can wrap, we can be off by +/- xsize,ysize. */
  if( has_mapwrap(MAP_TYPE_WRAPX) )
    *city_x= map_wrap_x(*city_x);
  if( has_mapwrap(MAP_TYPE_WRAPY) )
    *city_y= map_wrap_y(*city_y);

  /* Now validate */
  return is_valid_city_coords(*city_x, *city_y);
}
   

/**************************************************************************
  Convert map coordinate into position in city map
  These should be replaced by get_citymap_xy, just as map_adjust_* are
  replaced by normalize_map_pos.
**************************************************************************/
int map_to_city_x(struct city *pcity, int x)
{
  x = x - pcity->x + CITY_MAP_SIZE/2;

  /* If coords can wrap, we can be off by +/- xsize. */
  if( has_mapwrap(MAP_TYPE_WRAPX) )
    x= map_wrap_x(x);

  return x;
}
int map_to_city_y(struct city *pcity, int y)
{
  y = y - pcity->y + CITY_MAP_SIZE/2;

  /* If coords can wrap, we can be off by +/- ysize. */
  if( has_mapwrap(MAP_TYPE_WRAPY) )
    y= map_wrap_y(y);

  return y;
}
   

At 10:42 PM 01/09/24 +0200, Raimar Falke wrote:
>
>The attached patch should remove any code which transforms map
>positions to city map positions and back by replacing them with some
>new methods or the existing city_map_checked_iterate.
>
>The server doesn't core dump in autogame mode. I haven't yet checked
>for changes int savegames.
>
>Does anybody see any other instances?
>
>       Raimar
>
>-- 
> email: rf13@xxxxxxxxxxxxxxxxx
> "The Internet is really just a series of bottlenecks 
>  joined by high speed networks."
>    -- Sam Wilson
>
>Attachment Converted: "c:\program files\eudora\attach\coord_transform1.diff"

 /**************************************************************************
-Given a real map position and a city finds the city map coordinates of the
-tiles.
-Returns whether the real map position was inside the city map.
-**************************************************************************/
-int get_citymap_xy(const struct city *pcity, const int x, const int y,
-                  int *city_x, int *city_y)
-{
-  assert(is_real_tile(x, y));
-  city_map_iterate(x1, y1) {
-    int map_x = pcity->x + x1 - CITY_MAP_SIZE/2;
-    int map_y = pcity->y + y1 - CITY_MAP_SIZE/2;
-    if (normalize_map_pos(&map_x, &map_y)
-       && map_x == x && map_y == y) {
-      *city_x = x1;
-      *city_y = y1;
+Given a real map position and a city finds the city map coordinates of
+the tiles. Returns whether the real map position was inside the city
+map.
+**************************************************************************/
+int base_map_to_local_city_map(int *local_city_map_x,
+                              int *local_city_map_y, int city_center_x,
+                              int city_center_y, int map_x, int map_y)
+{
+  assert(is_real_tile(map_x, map_y));
+  city_map_iterate(test_local_city_map_x, test_local_city_map_y) {
+    int test_map_x =
+       city_center_x + test_local_city_map_x - CITY_MAP_SIZE / 2;
+    int test_map_y =
+       city_center_y + test_local_city_map_y - CITY_MAP_SIZE / 2;
+    if (normalize_map_pos(&test_map_x, &test_map_y)
+       && test_map_x == map_x && test_map_y == map_y) {
+      *local_city_map_x = test_local_city_map_x;
+      *local_city_map_y = test_local_city_map_y;
       return 1;
     }
   } city_map_iterate_end;
-
   return 0;
 }
 
+int map_to_local_city_map(int *local_city_map_x, int *local_city_map_y,
+                         const struct city *const pcity, int map_x,
+                         int map_y)
+{
+  return base_map_to_local_city_map(local_city_map_x,
+                                   local_city_map_y, pcity->x, pcity->y,
+                                   map_x, map_y);
+}
+
+int base_local_city_map_to_map(int *map_x, int *map_y,
+                              int city_center_x, int city_center_y,
+                              int local_city_map_x, int local_city_map_y)
+{
+  assert(is_valid_city_coords(local_city_map_x, local_city_map_y));
+  *map_x = city_center_x + local_city_map_x - CITY_MAP_SIZE / 2;
+  *map_y = city_center_y + local_city_map_y - CITY_MAP_SIZE / 2;
+  return normalize_map_pos(map_x, map_y);
+}
+
+int local_city_map_to_map(int *map_x, int *map_y,
+                         const struct city *const pcity,
+                         int local_city_map_x, int local_city_map_y)
+{
+  return base_local_city_map_to_map(map_x, map_y,
+                                   pcity->x, pcity->y,
+                                   local_city_map_x, local_city_map_y);
+}
+




[Prev in Thread] Current Thread [Next in Thread]