<URL: http://bugs.freeciv.org/Ticket/Display.html?id=39462 >

This is the final patch, which include a new get_line_dest() function
(now, it would really return the destination tile and not the first unit
destination which could be his own tile). I tried to optimize the code
in time.

Sorry, I know you don't like static variables, but I inserted a new one :)

--- client/control.c.old	2007-08-15 20:28:54.000000000 +0200
+++ client/control.c	2007-08-17 03:21:36.000000000 +0200
@@ -2035,15 +2035,12 @@
 **************************************************************************/
 void do_unit_goto(struct tile *ptile)
 {
-  struct tile *dest_tile;
-
   if (hover_state != HOVER_GOTO && hover_state != HOVER_NUKE) {
     return;
   }
 
   draw_line(ptile);
-  dest_tile = get_line_dest();
-  if (ptile == dest_tile) {
+  if (is_valid_goto_destination(ptile)) {
     send_goto_route();
   } else {
     create_event(ptile, E_BAD_COMMAND,
@@ -2072,15 +2069,12 @@
 **************************************************************************/
 void do_unit_patrol_to(struct tile *ptile)
 {
-  struct tile *dest_tile;
-
   draw_line(ptile);
-  dest_tile = get_line_dest();
-  if (ptile == dest_tile
+  if (is_valid_goto_destination(ptile)
       && !is_non_allied_unit_tile(ptile, game.player_ptr)) {
     send_patrol_route();
   } else {
-    create_event(dest_tile, E_BAD_COMMAND,
+    create_event(ptile, E_BAD_COMMAND,
 		 _("Didn't find a route to the destination!"));
   }
 
@@ -2093,11 +2087,8 @@
 void do_unit_connect(struct tile *ptile,
 		     enum unit_activity activity)
 {
-  struct tile *dest_tile;
-
   draw_line(ptile);
-  dest_tile = get_line_dest();
-  if (same_pos(dest_tile, ptile)) {
+  if (is_valid_goto_destination(ptile)) {
     send_connect_route(activity);
   } else {
     create_event(ptile, E_BAD_COMMAND,
--- client/goto.c.old	2007-08-15 20:29:08.000000000 +0200
+++ client/goto.c	2007-08-17 03:16:18.000000000 +0200
@@ -112,6 +112,7 @@
 static bool is_init = FALSE;
 static bool is_goto_valid = FALSE;
 static int connect_initial;
+static struct tile *goto_tile = NULL;
 
 /****************************************************************************
   Create a new goto map.
@@ -190,8 +191,9 @@
 /********************************************************************** 
   Change the destination of the last part to the given position if a
   path can be found. If not the destination is set to the start.
+  Return TRUE if the new path is found.
 ***********************************************************************/
-static void update_last_part(struct goto_map *goto_map, struct tile *ptile)
+static bool update_last_part(struct goto_map *goto_map, struct tile *ptile)
 {
   struct part *p = &goto_map->parts[goto_map->num_parts - 1];
   struct pf_path *new_path;
@@ -202,12 +204,10 @@
           TILE_XY(ptile), TILE_XY(p->start_tile), TILE_XY(p->end_tile));
   new_path = pf_get_path(p->map, ptile);
 
-  is_goto_valid = (new_path != NULL);
-
   if (!new_path) {
     freelog(PATH_LOG_LEVEL, "  no path found");
     reset_last_part(goto_map);
-    return;
+    return FALSE;
   }
 
   freelog(PATH_LOG_LEVEL, "  path found:");
@@ -277,6 +277,8 @@
   /* Refresh tiles so turn information is shown. */
   refresh_tile_mapcanvas(old_tile, FALSE, FALSE);
   refresh_tile_mapcanvas(ptile, FALSE, FALSE);
+
+  return TRUE;
 }
 
 /********************************************************************** 
@@ -802,6 +804,7 @@
     goto_map_list_append(goto_maps, goto_map);
   } unit_list_iterate_end;
   is_active = TRUE;
+  is_goto_valid = TRUE;
 }
 
 /********************************************************************** 
@@ -821,6 +824,8 @@
   } goto_map_list_iterate_end;
   goto_map_list_unlink_all(goto_maps);
 
+  is_goto_valid = FALSE;
+  goto_tile = NULL;
   is_active = FALSE;
 }
 
@@ -837,14 +842,7 @@
 ***********************************************************************/
 struct tile *get_line_dest(void)
 {
-  if (is_active) {
-    struct goto_map *goto_map = goto_map_list_get(goto_maps, 0);
-    struct part *p = &goto_map->parts[goto_map->num_parts - 1];
-
-    return p->end_tile;
-  } else {
-    return NULL;
-  }
+  return goto_tile;
 }
 
 /**************************************************************************
@@ -885,8 +883,12 @@
 {
   assert(is_active);
 
+  goto_tile = dest_tile;
+  is_goto_valid = FALSE;
   goto_map_iterate(goto_maps, goto_map, punit) {
-    update_last_part(goto_map, dest_tile);
+    if (update_last_part(goto_map, dest_tile)) {
+      is_goto_valid = TRUE;
+    }
   } goto_map_iterate_end;
 
   /* Update goto data in info label. */
@@ -1026,10 +1028,16 @@
   assert(is_active);
   goto_map_iterate(goto_maps, goto_map, punit) {
     int i;
+    struct part *last_part = &goto_map->parts[goto_map->num_parts - 1];
     struct pf_path *path = NULL, *return_path;
     struct pf_parameter parameter = goto_map->template;
     struct pf_map *map;
 
+    if (last_part->end_tile != goto_tile) {
+      /* Cannot move there */
+      continue;
+    }
+
     parameter.start_tile = goto_map->parts[goto_map->num_parts - 1].end_tile;
     parameter.moves_left_initially
       = goto_map->parts[goto_map->num_parts - 1].end_moves_left;
@@ -1063,11 +1071,17 @@
 {
   assert(is_active);
   goto_map_iterate(goto_maps, goto_map, punit) {
+    struct part *last_part = &goto_map->parts[goto_map->num_parts - 1];
     struct pf_path *path = NULL;
     int i;
     struct packet_unit_orders p;
     struct tile *old_tile;
 
+    if (last_part->end_tile != goto_tile) {
+      /* Cannot move there */
+      continue;
+    }
+
     memset(&p, 0, sizeof(p));
 
     for (i = 0; i < goto_map->num_parts; i++) {
@@ -1144,9 +1158,15 @@
 {
   assert(is_active);
   goto_map_iterate(goto_maps, goto_map, punit) {
+    struct part *last_part = &goto_map->parts[goto_map->num_parts - 1];
     struct pf_path *path = NULL;
     int i;
 
+    if (last_part->end_tile != goto_tile) {
+      /* Cannot move there */
+      continue;
+    }
+
     for (i = 0; i < goto_map->num_parts; i++) {
       path = pft_concat(path, goto_map->parts[i].path);
     }
--- client/mapctrl_common.c.old	2007-08-17 03:12:28.000000000 +0200
+++ client/mapctrl_common.c	2007-08-17 03:13:37.000000000 +0200
@@ -659,7 +659,7 @@
     }
 
     old_tile = get_line_dest();
-    if (!same_pos(old_tile, ptile)) {
+    if (!old_tile || !same_pos(old_tile, ptile)) {
       draw_line(ptile);
     }
   }
_______________________________________________
Freeciv-dev mailing list
Freeciv-dev@gna.org
https://mail.gna.org/listinfo/freeciv-dev

Reply via email to