Complete.Org: Mailing Lists: Archives: freeciv-dev: January 2003:
[Freeciv-Dev] (PR#2836) cleanup to client/goto
Home

[Freeciv-Dev] (PR#2836) cleanup to client/goto

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients:;
Subject: [Freeciv-Dev] (PR#2836) cleanup to client/goto
From: "Jason Short via RT" <rt@xxxxxxxxxxxxxx>
Date: Wed, 15 Jan 2003 22:43:28 -0800
Reply-to: rt@xxxxxxxxxxxxxx

The attached patch provides a cleanup to the client_goto_map.

The primary advantage is that indexing using map_inx() (aka 
map_to_index() in Ross's corecleanups) is safe under any topology.

It also removes a few lines of code, makes some accesses more legible 
(IMO), and moves the declaration of "struct client_goto_map" into goto.c 
to provide further encapsulation.

jason

Index: client//goto.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/goto.c,v
retrieving revision 1.44
diff -u -r1.44 goto.c
--- client//goto.c      2003/01/09 02:36:36     1.44
+++ client//goto.c      2003/01/16 06:38:24
@@ -30,6 +30,14 @@
 
 #include "goto.h"
 
+struct client_goto_map {
+  short *move_cost;
+  char *vector;
+  unsigned char (*drawn)[4];
+  int unit_id; /* The unit of the goto map */
+  int src_x, src_y;
+};
+
 static void undraw_line(void);
 static unsigned char *get_drawn_char(int x, int y, int dir);
 
@@ -64,7 +72,7 @@
 
 static bool is_init = FALSE;
 static int old_xsize;
-struct client_goto_map goto_map;
+static struct client_goto_map goto_map;
 
 /* These are used for all GOTO's */
 
@@ -201,7 +209,8 @@
     *x = our_array->pos[our_array->first_pos].x;
     *y = our_array->pos[our_array->first_pos].y;
     our_array->first_pos++;
-    freelog(LOG_DEBUG, "got %i,%i, at cost %i", *x, *y, 
goto_map.move_cost[*x][*y]);
+    freelog(LOG_DEBUG, "got %i,%i, at cost %i", *x, *y,
+           goto_map.move_cost[map_inx(*x, *y)]);
     return TRUE;
   }
   return FALSE;
@@ -212,8 +221,6 @@
 ***********************************************************************/
 void init_client_goto(void)
 {
-  int x_itr;
-
   if (!goto_array) {
     goto_array = fc_malloc(INITIAL_ARRAY_LENGTH
                                            * sizeof(struct map_position));
@@ -224,21 +231,20 @@
     free_client_goto();
   }
 
-  goto_map.move_cost = fc_malloc(map.xsize * sizeof(short *));
-  goto_map.vector = fc_malloc(map.xsize * sizeof(char *));
-  goto_map.drawn = fc_malloc(map.xsize * sizeof(char *));
-  for (x_itr = 0; x_itr < map.xsize; x_itr++) {
-    goto_map.move_cost[x_itr] = fc_malloc(map.ysize * sizeof(short));
-    goto_map.vector[x_itr] = fc_malloc(map.ysize * sizeof(char));
-    goto_map.drawn[x_itr] = fc_malloc(map.ysize * sizeof(char) * 4);
-  }
+  goto_map.move_cost = fc_malloc(map.xsize * map.ysize
+                                * sizeof(*goto_map.move_cost));
+  goto_map.vector = fc_malloc(map.xsize * map.ysize
+                             * sizeof(*goto_map.vector));
+  goto_map.drawn = fc_malloc(map.xsize * map.ysize
+                            * sizeof(*goto_map.drawn));
+
   goto_map.unit_id = -1;
   goto_map.src_x = -1;
   goto_map.src_y = -1;
   whole_map_iterate(x, y) {
     int dir;
     for (dir=0; dir<4; dir++)
-      *(goto_map.drawn[x] + y * 4 + dir) = 0;
+      goto_map.drawn[map_inx(x, y)][dir] = 0;
   } whole_map_iterate_end;
   initialize_move_costs();
 
@@ -249,12 +255,6 @@
 void free_client_goto()
 {
   if (is_init) {
-    int x_itr;
-    for (x_itr = 0; x_itr < old_xsize; x_itr++) {
-      free(goto_map.move_cost[x_itr]);
-      free(goto_map.vector[x_itr]);
-      free(goto_map.drawn[x_itr]);
-    }
     free(goto_map.move_cost);
     free(goto_map.vector);
     free(goto_map.drawn);
@@ -279,12 +279,12 @@
 {
   goto_map.unit_id = punit->id;
   whole_map_iterate(x, y) {
-    goto_map.move_cost[x][y] = MAXCOST;
-    goto_map.vector[x][y] = 0;
+    goto_map.move_cost[map_inx(x, y)] = MAXCOST;
+    goto_map.vector[map_inx(x, y)] = 0;
   } whole_map_iterate_end;
   goto_map.src_x = src_x;
   goto_map.src_y = src_y;
-  goto_map.move_cost[src_x][src_y] = 0;
+  goto_map.move_cost[map_inx(src_x, src_y)] = 0;
 }
 
 /**************************************************************************
@@ -354,7 +354,8 @@
       pdesttile = map_get_tile(x1, y1);
       add_to_queue = TRUE;
 
-      if (goto_map.move_cost[x1][y1] <= goto_map.move_cost[x][y]) {
+      if (goto_map.move_cost[map_inx(x1, y1)]
+         <= goto_map.move_cost[map_inx(x, y)]) {
        /* No need for all the calculations. Note that this also excludes
         * RR loops, ie you can't create a cycle with the same move_cost */
        continue;
@@ -441,13 +442,13 @@
       } /****** end switch ******/
 
       /* Add the route to our warmap if it is worth keeping */
-      total_cost = move_cost + goto_map.move_cost[x][y];
-      if (goto_map.move_cost[x1][y1] > total_cost) {
-       goto_map.move_cost[x1][y1] = total_cost;
+      total_cost = move_cost + goto_map.move_cost[map_inx(x, y)];
+      if (goto_map.move_cost[map_inx(x1, y1)] > total_cost) {
+       goto_map.move_cost[map_inx(x1, y1)] = total_cost;
        if (add_to_queue) {
          add_to_mapqueue(total_cost, x1, y1);
        }
-       goto_map.vector[x1][y1] = 1 << DIR_REVERSE(dir);
+       goto_map.vector[map_inx(x1, y1)] = 1 << DIR_REVERSE(dir);
        freelog(LOG_DEBUG,
                "Candidate: %s from (%d, %d) to (%d, %d), cost %d",
                dir_get_name(dir), x, y, x1, y1, total_cost);
@@ -786,7 +787,7 @@
   }
 
   adjc_dir_iterate(x, y, new_x, new_y, dir) {
-    if (TEST_BIT(goto_map.vector[x][y], dir)) {
+    if (TEST_BIT(goto_map.vector[map_inx(x, y)], dir)) {
       /* expand array as neccesary */
       if (route_index == route_length) {
        route_length *= 2;
@@ -821,7 +822,7 @@
   assert(is_real_tile(dest_x, dest_y));
   normalize_map_pos(&dest_x, &dest_y);
 
-  if (goto_map.vector[dest_x][dest_y] == 0) {
+  if (goto_map.vector[map_inx(dest_x, dest_y)] == 0) {
     undraw_line();
     return;
   }
Index: client//goto.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/goto.h,v
retrieving revision 1.7
diff -u -r1.7 goto.h
--- client//goto.h      2002/12/06 22:25:12     1.7
+++ client//goto.h      2003/01/16 06:38:24
@@ -15,16 +15,6 @@
 
 #include "map.h"
 
-struct client_goto_map {
-  short **move_cost;
-  char **vector;
-  unsigned char **drawn; /* Should not be modified directly. */
-  int unit_id; /* The unit of the goto map */
-  int src_x, src_y;
-};
-
-extern struct client_goto_map goto_map;
-
 void init_client_goto(void);
 void free_client_goto(void);
 void enter_goto_state(struct unit *punit);

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#2836) cleanup to client/goto, Jason Short via RT <=