Complete.Org: Mailing Lists: Archives: freeciv-dev: February 2002:
[Freeciv-Dev] Make use of do_unit_goto return value. (PR#1253)
Home

[Freeciv-Dev] Make use of do_unit_goto return value. (PR#1253)

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv-dev@xxxxxxxxxxx
Cc: bugs@xxxxxxxxxxxxxxxxxxx
Subject: [Freeciv-Dev] Make use of do_unit_goto return value. (PR#1253)
From: Gregory Berkolaiko <gberkolaiko@xxxxxxxxxxx>
Date: Fri, 8 Feb 2002 08:16:35 -0800 (PST)

The attached patch makes use of the return value of the functions
do_unit_goto (several occurences) and goto_route_execute (one occurence).
So as to not introduce any sturctural changes, not all occurences of
do_unit_goto were changed.

The immediate result of this is that Raimar's Spliff package won't
complain as much.  Also the code is slightly more readable. 
More extensive use of the return values will hopefully come with the
cleanup of the caller functions.

Weird formatting in .diff file is caused by tabulation that emacs insists
on using.  If anyone can explain me how to make emacs pad with
whitespace, I will be grateful.

Patch is done against todays CVS (08/08/2002).
The savegames are identical.
Affected files:
 ai/aiunit.c
 server/unittools.c


Best,
G.

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com
? auto.rc
? saves
? use_do_unit_goto.diff
Index: ai/aiunit.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aiunit.c,v
retrieving revision 1.162
diff -u -r1.162 aiunit.c
--- ai/aiunit.c 2002/02/07 17:23:30     1.162
+++ ai/aiunit.c 2002/02/08 16:02:47
@@ -231,9 +231,9 @@
       punit->goto_dest_x = best_x;
       punit->goto_dest_y = best_y;
       set_unit_activity(punit, ACTIVITY_GOTO);
-      do_unit_goto(punit, GOTO_MOVE_ANY, 0);
-      if (!player_find_unit_by_id(pplayer, id))
-       return 0; /* died */
+      if (do_unit_goto(punit, GOTO_MOVE_ANY, 0) == GR_DIED) {
+       return 0;
+      }
 
       if (punit->moves_left) {
        if (punit->x == best_x && punit->y == best_y) {
@@ -332,6 +332,7 @@
       punit->goto_dest_y = best_y;
       handle_unit_activity_request(punit, ACTIVITY_GOTO);
       do_unit_goto(punit, GOTO_MOVE_ANY, 0);
+      /* FIXME: check if unit is still alive */
       if (punit->moves_left) {
        if (punit->x != best_x || punit->y != best_y) {
          handle_unit_activity_request(punit, ACTIVITY_IDLE);
@@ -880,8 +881,9 @@
         punit->goto_dest_x = bx;
         punit->goto_dest_y = by;
        set_unit_activity(punit, ACTIVITY_GOTO);
-        do_unit_goto(punit, GOTO_MOVE_ANY, 0);
-        if (!player_find_unit_by_id(pplayer, id)) return(-1); /* died */
+        if (do_unit_goto(punit, GOTO_MOVE_ANY, 0) == GR_DIED) {
+         return(-1);
+       }
       }
       ptile = map_get_tile(punit->x, punit->y);
       ferryboat = unit_list_find(&ptile->units, punit->ai.ferryboat);
@@ -910,8 +912,9 @@
                 if (def) set_unit_activity(def, ACTIVITY_SENTRY);
               }
             unit_list_iterate_end; /* passengers are safely stowed away */
-            do_unit_goto(ferryboat, GOTO_MOVE_ANY, 0);
-           if (!player_find_unit_by_id(pplayer, boatid)) return(-1); /* died */
+            if (do_unit_goto(ferryboat, GOTO_MOVE_ANY, 0) == GR_DIED) {
+             return(-1);
+           }
             set_unit_activity(punit, ACTIVITY_IDLE);
           } /* else wait, we can GOTO later. */
         }
@@ -956,7 +959,9 @@
                punit->x, punit->y, dest_x, dest_y);
       }
       set_unit_activity(punit, ACTIVITY_GOTO);
-      do_unit_goto(punit, GOTO_MOVE_ANY, 0);
+      if (do_unit_goto(punit, GOTO_MOVE_ANY, 0) == GR_DIED) {
+       return -1;
+      }
       /* liable to bump into someone that will kill us.  Should avoid? */
     } else {
       freelog(LOG_DEBUG, "%s#%d@(%d,%d) not moving -> (%d, %d)",
@@ -964,12 +969,12 @@
                    punit->x, punit->y, dest_x, dest_y);
     }
   }
-  if (player_find_unit_by_id(pplayer, id)) { /* didn't die */
-    punit->ai.ai_role = AIUNIT_NONE; /* in case we need to change */
-    if (x != punit->x || y != punit->y) return 1; /* moved */
-    else return 0; /* didn't move, didn't die */
-  }
-  return(-1); /* died */
+  /* Dead unit shouldn't reach this point */
+  assert(player_find_unit_by_id(pplayer, id));
+  
+  punit->ai.ai_role = AIUNIT_NONE; /* in case we need to change */
+  if (x != punit->x || y != punit->y) return 1; /* moved */
+  else return 0; /* didn't move, didn't die */
 }
 
 /*************************************************************************
@@ -1632,7 +1637,6 @@
      It is well constructed of teak, and looks seaworthy. */
   struct city *pcity;
   struct unit *bodyguard;
-  int id = punit->id;
   int best = 4 * unit_type(punit)->move_rate, x = punit->x, y = punit->y;
   int n = 0, p = 0;
 
@@ -1665,9 +1669,11 @@
       do_unit_goto(punit, GOTO_MOVE_ANY, 0);
     else if (!n && !map_get_city(punit->x, punit->y)) { /* rest in a city, for 
unhap */
       x = punit->goto_dest_x; y = punit->goto_dest_y;
-      if (find_nearest_friendly_port(punit))
-       do_unit_goto(punit, GOTO_MOVE_ANY, 0);
-      if (!player_find_unit_by_id(pplayer, id)) return; /* oops! */
+      if (find_nearest_friendly_port(punit)) {
+       if (do_unit_goto(punit, GOTO_MOVE_ANY, 0) == GR_DIED) {
+         return;
+       }
+      }
       punit->goto_dest_x = x; punit->goto_dest_y = y;
       send_unit_info(pplayer, punit); /* to get the crosshairs right -- Syela 
*/
     } else {
@@ -1718,9 +1724,9 @@
     punit->goto_dest_x = x;
     punit->goto_dest_y = y;
     set_unit_activity(punit, ACTIVITY_GOTO);
-    do_unit_goto(punit, GOTO_MOVE_ANY, 0);
-    if ((punit = player_find_unit_by_id(pplayer, id)))
+    if (do_unit_goto(punit, GOTO_MOVE_ANY, 0) != GR_DIED) {
       set_unit_activity(punit, ACTIVITY_IDLE);
+    }
     return;
   }
 
Index: server/unittools.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/unittools.c,v
retrieving revision 1.150
diff -u -r1.150 unittools.c
--- server/unittools.c  2002/02/07 17:23:47     1.150
+++ server/unittools.c  2002/02/08 16:02:48
@@ -791,9 +791,10 @@
 
   if (punit->connecting && !can_unit_do_activity(punit, activity)) {
     punit->activity_count = 0;
-    do_unit_goto(punit, get_activity_move_restriction(activity), 0);
-    if (!player_find_unit_by_id(pplayer, id))
+    if (do_unit_goto(punit, get_activity_move_restriction(activity), 0)
+       == GR_DIED) {
       return;
+    }
   }
 
   /* if connecting, automagically build prerequisities first */
@@ -965,16 +966,18 @@
     send_tile_info(NULL, punit->x, punit->y);
     unit_list_iterate (map_get_tile(punit->x, punit->y)->units, punit2) {
       if (punit2->activity == activity) {
-       int id2 = punit2->id;
+       /* Can initialise to anything but GR_DIED */
+       enum goto_result res = GR_ARRIVED;
        if (punit2->connecting) {
          punit2->activity_count = 0;
-         do_unit_goto(punit2, get_activity_move_restriction(activity), 0);
-       }
-       else {
+         res = do_unit_goto(punit2, 
+                            get_activity_move_restriction(activity), 0);
+       } else {
          set_unit_activity(punit2, ACTIVITY_IDLE);
        }
-       if (find_unit_by_id(id2))
+       if (res != GR_DIED) {
          send_unit_info(NULL, punit2);
+       }
       }
     } unit_list_iterate_end;
   }
@@ -996,9 +999,9 @@
   }
 
   if (punit->activity == ACTIVITY_PATROL) {
-    goto_route_execute(punit);
-    if (!player_find_unit_by_id(pplayer, id))
+    if (goto_route_execute(punit) == GR_DIED) {
       return;
+    }
   }
 
   send_unit_info(NULL, punit);

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] Make use of do_unit_goto return value. (PR#1253), Gregory Berkolaiko <=