Complete.Org: Mailing Lists: Archives: freeciv-dev: April 2003:
[Freeciv-Dev] (PR#4075) rewrite base_map_to_city_map
Home

[Freeciv-Dev] (PR#4075) rewrite base_map_to_city_map

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients:;
Subject: [Freeciv-Dev] (PR#4075) rewrite base_map_to_city_map
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 24 Apr 2003 16:12:59 -0700
Reply-to: rt@xxxxxxxxxxxxxx

I was looking at city.c today and notice that base_city_map_to_map is 
encoded using a brute-force approach.  I instead rewrote it using 
map_distance_vector (this function was probably not available with the 
current version was written).  This makes the function algorithmically 
faster - O(1) versus O(n) - although constant factors are likely to have 
an even larger effect.  And of course it's more elegant.

When compiling with -O2 I found a small (0.9%) speed improvement in the 
overall server speed.  When compiling with debugging this is negated 
(presumably by the extra CHECK_MAP_POS call done from map_distance_vector).

jason

Index: common/city.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/city.c,v
retrieving revision 1.185
diff -u -r1.185 city.c
--- common/city.c       2003/03/05 08:56:07     1.185
+++ common/city.c       2003/04/24 23:05:39
@@ -106,15 +106,11 @@
                                 int city_center_x, int city_center_y,
                                 int map_x, int map_y)
 {
-  CHECK_MAP_POS(map_x, map_y);
-  city_map_checked_iterate(city_center_x, city_center_y, cx, cy, mx, my) {
-    if (mx == map_x && my == map_y) {
-      *city_map_x = cx;
-      *city_map_y = cy;
-      return TRUE;
-    }
-  } city_map_checked_iterate_end;
-  return FALSE;
+  map_distance_vector(city_map_x, city_map_y,
+                     city_center_x, city_center_y, map_x, map_y);
+  *city_map_x += CITY_MAP_SIZE / 2;
+  *city_map_y += CITY_MAP_SIZE / 2;
+  return is_valid_city_coords(*city_map_x, *city_map_y);
 }
 
 /**************************************************************************

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