Complete.Org: Mailing Lists: Archives: freeciv-dev: July 2006:
[Freeciv-Dev] Re: (PR#18441) [Patch] Better warmap caching
Home

[Freeciv-Dev] Re: (PR#18441) [Patch] Better warmap caching

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: cazfi74@xxxxxxxxx
Subject: [Freeciv-Dev] Re: (PR#18441) [Patch] Better warmap caching
From: "Per I. Mathisen" <per@xxxxxxxxxxx>
Date: Sun, 16 Jul 2006 06:54:17 -0700
Reply-to: bugs@xxxxxxxxxxx

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

On Tue, 11 Jul 2006, Marko Lindqvist wrote:
>>   Also, there is still two users for gotohand.c:do_unit_goto().
>> grep -r do_unit_goto server/*.c
>
>  do_unit_goto() is only place that needs find_the_shortest_path().
> find_the_shortest_path() implements its own warmap filling method
> distinct from generate_warmap(), in theory requiring maintenance
> separately from generate_warmap() (as find_the_shortest_path() is not
> fatally broken, I'm not fixing it). Getting rid of
> find_the_shortest_path() would be good.

It should be almost trivial to replace these two uses of do_unit_goto() 
and then remove both that function and find_the_shortest_path().

Attached is an untested proof-of-concept patch for the air return part.

   - Per
Index: server/unittools.c
===================================================================
--- server/unittools.c  (revision 12092)
+++ server/unittools.c  (working copy)
@@ -40,6 +40,9 @@
 #include "unit.h"
 #include "unitlist.h"
 
+#include "path_finding.h"
+#include "pf_tools.h"
+
 #include "barbarian.h"
 #include "citytools.h"
 #include "cityturn.h"
@@ -387,23 +390,58 @@
 
       if (punit->fuel == 1
          && !is_airunit_refuel_point(punit->tile,
-                                     unit_owner(punit), punit->type, TRUE)) {
-       iterate_outward(punit->tile, punit->moves_left/3, itr_tile) {
-         if (is_airunit_refuel_point(itr_tile, unit_owner(punit),
-                                     punit->type, FALSE)
-             &&(air_can_move_between(punit->moves_left / 3, punit->tile,
-                                     itr_tile, unit_owner(punit)) >= 0)) {
-           punit->goto_tile = itr_tile;
-           set_unit_activity(punit, ACTIVITY_GOTO);
-           (void) do_unit_goto(punit, GOTO_MOVE_ANY, FALSE);
-           notify_player(pplayer, punit->tile, E_UNIT_ORDERS, 
-                            _("Your %s has returned to refuel."),
-                            unit_name(punit->type));
-           goto OUT;
-         }
-       } iterate_outward_end;
+                                     pplayer, punit->type, TRUE)) {
+        struct pf_map *map;
+        struct pf_parameter parameter;
+
+        pft_fill_unit_parameter(&parameter, punit);
+        map = pf_create_map(&parameter);
+
+        pf_iterator(map, pos) {
+          if (pos.total_MC > punit->moves_left) {
+            break; /* too far */
+          }
+          if (is_airunit_refuel_point(pos.tile, pplayer, punit->type, FALSE)) {
+            int i;
+            struct pf_path *path;
+
+            punit->goto_tile = pos.tile;
+            set_unit_activity(punit, ACTIVITY_GOTO);
+            path = pf_get_path(map, pos.tile);
+
+            /* We start with i = 1 for i = 0 is our present position */
+            for (i = 1; i < path->length; i++) {
+              struct tile *ptile = path->positions[i].tile;
+              int id = punit->id;
+
+              if (same_pos(punit->tile, ptile)) {
+                freelog(LOG_ERROR, "rescue plane: unit %d waiting this 
turn?!", punit->id);
+                break;
+              }
+
+              (void) handle_unit_move_request(punit, ptile, FALSE, TRUE);
+              if (!find_unit_by_id(id)) {
+                freelog(LOG_ERROR, "rescue plane: unit %d died enroute?!", id);
+                break;
+              }
+              if (!same_pos(punit->tile, ptile) || punit->moves_left <= 0) {
+                /* Something odd happened */
+                freelog(LOG_ERROR, "rescue plane: unit %d could not move?!", 
punit->id);
+                break;
+              }
+            }
+            handle_unit_activity_request(punit, ACTIVITY_IDLE);
+            send_unit_info(NULL, punit);
+            punit->goto_tile = NULL;
+            notify_player(pplayer, punit->tile, E_UNIT_ORDERS,
+                          _("Your %s has returned to refuel."),
+                          unit_name(punit->type));
+            pf_destroy_path(path);
+            break;
+          }
+        } pf_iterator_end;
+        pf_destroy_map(map);
       }
-    OUT:
 
       /* 6) Update fuel */
       punit->fuel--;

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