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

Meant to do this months ago.  Found some in unittools and citytools, too.

Added some error logging for actual handle_* routines.  Never trust
network data!

Index: server/citytools.c
===================================================================
--- server/citytools.c  (revision 14241)
+++ server/citytools.c  (working copy)
@@ -557,7 +557,7 @@
       return;
     }
   }
-  real_unit_change_homecity(punit, tocity);
+  unit_change_homecity_handling(punit, tocity);
 }
 
 /*********************************************************************
@@ -1073,7 +1073,7 @@
 
     /* Catch fortress building, transforming into ocean, etc. */
     if (!can_unit_continue_current_activity(punit)) {
-      handle_unit_activity_request(punit, ACTIVITY_IDLE);
+      unit_activity_handling(punit, ACTIVITY_IDLE);
     }
 
     /* Update happiness (the unit may no longer cause unrest). */
@@ -1129,12 +1129,12 @@
       continue;
     }
 
-    handle_unit_activity_request(punit, ACTIVITY_IDLE);
+    unit_activity_handling(punit, ACTIVITY_IDLE);
     moved = FALSE;
     adjc_iterate(ptile, tile1) {
       if (!moved && is_native_tile(punittype, tile1)) {
        if (could_unit_move_to_tile(punit, tile1) == 1) {
-         moved = handle_unit_move_request(punit, tile1, FALSE, TRUE);
+         moved = unit_move_handling(punit, tile1, FALSE, TRUE);
          if (moved) {
            notify_player(unit_owner(punit), NULL, E_UNIT_RELOCATED,
                           _("Moved %s out of disbanded city %s "
@@ -1225,7 +1225,7 @@
 /**************************************************************************
 ...
 **************************************************************************/
-void handle_unit_enter_city(struct unit *punit, struct city *pcity)
+void unit_enter_city(struct unit *punit, struct city *pcity)
 {
   bool do_civil_war = FALSE;
   int coins;
Index: server/citytools.h
===================================================================
--- server/citytools.h  (revision 14241)
+++ server/citytools.h  (working copy)
@@ -45,7 +45,7 @@
                                     struct tile *ptile,
                                     bool sea_required,
                                     struct city *pexclcity);
-void handle_unit_enter_city(struct unit *punit, struct city *pcity);
+void unit_enter_city(struct unit *punit, struct city *pcity);
 
 void send_city_info(struct player *dest, struct city *pcity);
 void send_city_info_at_tile(struct player *pviewer, struct conn_list *dest,
Index: server/settlers.c
===================================================================
--- server/settlers.c   (revision 14241)
+++ server/settlers.c   (working copy)
@@ -80,7 +80,7 @@
   struct city *pcity;
 
   assert(pplayer == unit_owner(punit));
-  handle_unit_activity_request(punit, ACTIVITY_IDLE);
+  unit_activity_handling(punit, ACTIVITY_IDLE);
 
   /* Free city reservations */
   ai_unit_new_role(punit, AIUNIT_NONE, NULL);
@@ -1119,8 +1119,8 @@
       if (alive && same_pos(punit->tile, best_tile)
          && punit->moves_left > 0) {
        /* Reached destination and can start working immediately */
-        handle_unit_activity_request(punit, best_act);
-        send_unit_info(NULL, punit);
+        unit_activity_handling(punit, best_act);
+        send_unit_info(NULL, punit); /* FIXME: probably duplicate */
       }
 
       pf_destroy_path(path);
@@ -1270,10 +1270,10 @@
              nation_rule_name(nation_of_player(pplayer)),
              TILE_XY(punit->tile)); 
       if (punit->activity == ACTIVITY_SENTRY) {
-       handle_unit_activity_request(punit, ACTIVITY_IDLE);
+       unit_activity_handling(punit, ACTIVITY_IDLE);
       }
       if (punit->activity == ACTIVITY_GOTO && punit->moves_left > 0) {
-        handle_unit_activity_request(punit, ACTIVITY_IDLE);
+        unit_activity_handling(punit, ACTIVITY_IDLE);
       }
       if (punit->activity == ACTIVITY_IDLE) {
         auto_settler_findwork(pplayer, punit, state, 0);
Index: server/barbarian.c
===================================================================
--- server/barbarian.c  (revision 14241)
+++ server/barbarian.c  (working copy)
@@ -234,7 +234,7 @@
          do {
            utile = rand_neighbour(ptile);
          } while (!is_free_land(utile, barbarians));
-        } while (!handle_unit_move_request(punit2, utile, TRUE, FALSE));
+        } while (!unit_move_handling(punit2, utile, TRUE, FALSE));
         freelog(LOG_DEBUG, "Moved barbarian unit from %d %d to %d, %d", 
                 ptile->x, ptile->y, utile->x, utile->y);
       }
@@ -264,7 +264,7 @@
              break;
            }
           }
-          (void) handle_unit_move_request(punit2, utile, TRUE, FALSE);
+          (void) unit_move_handling(punit2, utile, TRUE, FALSE);
         }
       } unit_list_iterate_end;
     } else {             /* The village is surrounded! Kill the explorer. */
Index: server/maphand.c
===================================================================
--- server/maphand.c    (revision 14241)
+++ server/maphand.c    (working copy)
@@ -395,7 +395,7 @@
       update_tile_knowledge(ptile);
       unit_list_iterate(ptile->units, punit) {
        if (!can_unit_continue_current_activity(punit)) {
-         handle_unit_activity_request(punit, ACTIVITY_IDLE);
+         unit_activity_handling(punit, ACTIVITY_IDLE);
        }
       } unit_list_iterate_end;
     } else if (old == new) {
@@ -439,7 +439,7 @@
       update_tile_knowledge(ptile);
       unit_list_iterate(ptile->units, punit) {
        if (!can_unit_continue_current_activity(punit)) {
-         handle_unit_activity_request(punit, ACTIVITY_IDLE);
+         unit_activity_handling(punit, ACTIVITY_IDLE);
        }
       } unit_list_iterate_end;
     } else if (old == new) {
@@ -1622,7 +1622,7 @@
                           unit_name_translation(punit));
          unit_alive = move_unit(punit, ptile2, 0);
          if (unit_alive && punit->activity == ACTIVITY_SENTRY) {
-           handle_unit_activity_request(punit, ACTIVITY_IDLE);
+           unit_activity_handling(punit, ACTIVITY_IDLE);
          }
          break;
        }
Index: server/diplomats.c
===================================================================
--- server/diplomats.c  (revision 14241)
+++ server/diplomats.c  (working copy)
@@ -478,7 +478,7 @@
 
   /* Now, try to move the briber onto the victim's square. */
   diplomat_id = pdiplomat->id;
-  if (!handle_unit_move_request(pdiplomat, victim_tile, FALSE, FALSE)) {
+  if (!unit_move_handling(pdiplomat, victim_tile, FALSE, FALSE)) {
     pdiplomat->moves_left = 0;
   }
   if (player_find_unit_by_id(pplayer, diplomat_id)) {
Index: server/unittools.c
===================================================================
--- server/unittools.c  (revision 14241)
+++ server/unittools.c  (working copy)
@@ -401,7 +401,7 @@
               if (alive) {
                 /* Clear activity. Unit info will be sent in the end of
                 * the function. */
-                handle_unit_activity_request(punit, ACTIVITY_IDLE);
+                unit_activity_handling(punit, ACTIVITY_IDLE);
                 punit->goto_tile = NULL;
 
                 if (!is_unit_being_refueled(punit)) {
@@ -551,7 +551,7 @@
   Calculate the total amount of activity performed by all units on a tile
   for a given task.
 **************************************************************************/
-static int total_activity (struct tile *ptile, enum unit_activity act)
+static int total_activity(struct tile *ptile, enum unit_activity act)
 {
   int total = 0;
 
@@ -596,6 +596,15 @@
   return total;
 }
 
+/**************************************************************************
+  Check the total amount of activity performed by all units on a tile
+  for a given task.
+**************************************************************************/
+static bool total_activity_done(struct tile *ptile, enum unit_activity act)
+{
+  return total_activity(ptile, act) >= tile_activity_time(act, ptile);
+}
+
 /***************************************************************************
   Maybe settler/worker gains a veteran level?
 ****************************************************************************/
@@ -647,21 +656,60 @@
   struct tile *ptile = punit->tile;
   bool check_adjacent_units = FALSE;
   
-  if (activity != ACTIVITY_IDLE && activity != ACTIVITY_FORTIFIED
-      && activity != ACTIVITY_GOTO && activity != ACTIVITY_EXPLORE) {
+  switch (activity) {
+  case ACTIVITY_IDLE:
+  case ACTIVITY_EXPLORE:
+  case ACTIVITY_FORTIFIED:
+  case ACTIVITY_GOTO:
+  case ACTIVITY_PATROL_UNUSED:
+  case ACTIVITY_UNKNOWN:
+  case ACTIVITY_LAST:
     /*  We don't need the activity_count for the above */
+    break;
+
+  case ACTIVITY_FORTIFYING:
+  case ACTIVITY_SENTRY:
     punit->activity_count += get_activity_rate_this_turn(punit);
+    break;
 
+  case ACTIVITY_POLLUTION:
+  case ACTIVITY_ROAD:
+  case ACTIVITY_MINE:
+  case ACTIVITY_IRRIGATE:
+  case ACTIVITY_FORTRESS:
+  case ACTIVITY_RAILROAD:
+  case ACTIVITY_PILLAGE:
+  case ACTIVITY_TRANSFORM:
+  case ACTIVITY_AIRBASE:
+  case ACTIVITY_FALLOUT:
+  case ACTIVITY_BASE:
+    punit->activity_count += get_activity_rate_this_turn(punit);
+
     /* settler may become veteran when doing something useful */
-    if (activity != ACTIVITY_FORTIFYING && activity != ACTIVITY_SENTRY
-       && maybe_settler_become_veteran(punit)) {
+    if (maybe_settler_become_veteran(punit)) {
       notify_unit_experience(punit, FALSE);
     }
-  }
+    break;
+  };
 
   unit_restore_movepoints(pplayer, punit);
 
-  if (activity == ACTIVITY_EXPLORE) {
+  switch (activity) {
+  case ACTIVITY_IDLE:
+  case ACTIVITY_FORTIFIED:
+  case ACTIVITY_FORTRESS:
+  case ACTIVITY_SENTRY:
+  case ACTIVITY_GOTO:
+  case ACTIVITY_UNKNOWN:
+  case ACTIVITY_AIRBASE:
+  case ACTIVITY_FORTIFYING:
+  case ACTIVITY_PATROL_UNUSED:
+  case ACTIVITY_LAST:
+    /* no default, ensure all handled */
+    break;
+
+  case ACTIVITY_EXPLORE:
+  {
     bool more_to_explore = ai_manage_explorer(punit);
 
     if (!player_find_unit_by_id(pplayer, id)) {
@@ -672,7 +720,7 @@
     /* ai_manage_explorer isn't supposed to change the activity but we
      * don't count on this. */
     if (punit->activity != ACTIVITY_EXPLORE || !more_to_explore) {
-      handle_unit_activity_request(punit, ACTIVITY_IDLE);
+      unit_activity_handling(punit, ACTIVITY_IDLE);
 
       /* FIXME: When the ai_manage_explorer call changes the activity from
        * EXPLORE to IDLE, then for some reason the ai.control value gets left
@@ -683,7 +731,7 @@
     return;
   }
 
-  if (activity==ACTIVITY_PILLAGE) {
+  case ACTIVITY_PILLAGE:
     if (punit->activity_target == S_LAST) { /* case for old save files */
       if (punit->activity_count >= 1) {
        enum tile_special_type what
@@ -728,25 +776,23 @@
       /* Change vision if effects have changed. */
       unit_list_refresh_vision(ptile->units);
     }
-  }
+    break;
 
-  if (activity == ACTIVITY_POLLUTION) {
-    if (total_activity(ptile, ACTIVITY_POLLUTION)
-       >= tile_activity_time(ACTIVITY_POLLUTION, ptile)) {
+  case ACTIVITY_POLLUTION:
+    if (total_activity_done(ptile, ACTIVITY_POLLUTION)) {
       tile_clear_special(ptile, S_POLLUTION);
       unit_activity_done = TRUE;
     }
-  }
+    break;
 
-  if (activity == ACTIVITY_FALLOUT) {
-    if (total_activity(ptile, ACTIVITY_FALLOUT)
-       >= tile_activity_time(ACTIVITY_FALLOUT, ptile)) {
+  case ACTIVITY_FALLOUT:
+    if (total_activity_done(ptile, ACTIVITY_FALLOUT)) {
       tile_clear_special(ptile, S_FALLOUT);
       unit_activity_done = TRUE;
     }
-  }
+    break;
 
-  if (activity == ACTIVITY_BASE) {
+  case ACTIVITY_BASE:
     if (total_activity_base(ptile, punit->activity_base)
         >= tile_activity_base_time(ptile, punit->activity_base)) {
       tile_add_base(ptile, base_by_number(punit->activity_base));
@@ -763,60 +809,47 @@
 
       unit_activity_done = TRUE;
     }
-  }
-  
-  if (activity == ACTIVITY_IRRIGATE) {
-    if (total_activity (ptile, ACTIVITY_IRRIGATE)
-        >= tile_activity_time(ACTIVITY_IRRIGATE, ptile)) {
+    break;
+
+  case ACTIVITY_IRRIGATE:
+    if (total_activity_done(ptile, ACTIVITY_IRRIGATE)) {
       struct terrain *old = tile_terrain(ptile);
 
       tile_apply_activity(ptile, ACTIVITY_IRRIGATE);
       check_terrain_change(ptile, old);
       unit_activity_done = TRUE;
     }
-  }
+    break;
 
-  if (activity == ACTIVITY_ROAD) {
+  case ACTIVITY_MINE:
+  case ACTIVITY_TRANSFORM:
+    if (total_activity_done(ptile, activity)) {
+      struct terrain *old = tile_terrain(ptile);
+
+      tile_apply_activity(ptile, activity);
+      check_terrain_change(ptile, old);
+      unit_activity_done = TRUE;
+      check_adjacent_units = TRUE;
+    }
+    break;
+
+  case ACTIVITY_ROAD:
     if (total_activity (ptile, ACTIVITY_ROAD)
        + total_activity (ptile, ACTIVITY_RAILROAD)
         >= tile_activity_time(ACTIVITY_ROAD, ptile)) {
       tile_set_special(ptile, S_ROAD);
       unit_activity_done = TRUE;
     }
-  }
+    break;
 
-  if (activity == ACTIVITY_RAILROAD) {
-    if (total_activity (ptile, ACTIVITY_RAILROAD)
-       >= tile_activity_time(ACTIVITY_RAILROAD, ptile)) {
+  case ACTIVITY_RAILROAD:
+    if (total_activity_done(ptile, ACTIVITY_RAILROAD)) {
       tile_set_special(ptile, S_RAILROAD);
       unit_activity_done = TRUE;
     }
-  }
-  
-  if (activity == ACTIVITY_MINE) {
-    if (total_activity (ptile, ACTIVITY_MINE)
-        >= tile_activity_time(ACTIVITY_MINE, ptile)) {
-      struct terrain *old = tile_terrain(ptile);
+    break;
+  };
 
-      tile_apply_activity(ptile, ACTIVITY_MINE);
-      check_terrain_change(ptile, old);
-      unit_activity_done = TRUE;
-      check_adjacent_units = TRUE;
-    }
-  }
-
-  if (activity == ACTIVITY_TRANSFORM) {
-    if (total_activity (ptile, ACTIVITY_TRANSFORM)
-        >= tile_activity_time(ACTIVITY_TRANSFORM, ptile)) {
-      struct terrain *old = tile_terrain(ptile);
-
-      tile_apply_activity(ptile, ACTIVITY_TRANSFORM);
-      check_terrain_change(ptile, old);
-      unit_activity_done = TRUE;
-      check_adjacent_units = TRUE;
-    }
-  }
-
   if (unit_activity_done) {
     update_tile_knowledge(ptile);
     unit_list_iterate (ptile->units, punit2) {
@@ -832,13 +865,13 @@
     adjc_iterate(ptile, ptile2) {
       unit_list_iterate(ptile2->units, punit2) {
         if (!can_unit_continue_current_activity(punit2)) {
-          handle_unit_activity_request(punit2, ACTIVITY_IDLE);
+          unit_activity_handling(punit2, ACTIVITY_IDLE);
         }
       } unit_list_iterate_end;
     } adjc_iterate_end;
   }
 
-  if (activity==ACTIVITY_FORTIFYING) {
+  if (activity == ACTIVITY_FORTIFYING) {
     if (punit->activity_count >= 1) {
       set_unit_activity(punit,ACTIVITY_FORTIFIED);
     }
@@ -858,7 +891,7 @@
   unit_list_iterate(ptile->units, punit2) {
     if (!can_unit_continue_current_activity(punit2))
     {
-      handle_unit_activity_request(punit2, ACTIVITY_IDLE);
+      unit_activity_handling(punit2, ACTIVITY_IDLE);
     }
   } unit_list_iterate_end;
 }
@@ -2366,8 +2399,8 @@
               threshold);
 #endif
 
-      handle_unit_activity_request(penemy, ACTIVITY_IDLE);
-      (void) handle_unit_move_request(penemy, punit->tile, FALSE, FALSE);
+      unit_activity_handling(penemy, ACTIVITY_IDLE);
+      (void) unit_move_handling(penemy, punit->tile, FALSE, FALSE);
     }
 #ifdef REALLY_DEBUG_THIS
       else {
@@ -2458,31 +2491,32 @@
 }
 
 /**************************************************************************
-Does: 1) updates  the units homecity and the city it enters/leaves (the
-         cities happiness varies). This also takes into account if the
+Does: 1) updates the unit's homecity and the city it enters/leaves (the
+         city's happiness varies). This also takes into account when the
         unit enters/leaves a fortress.
-      2) handles any huts at the units destination.
+      2) process any huts at the unit's destination.
       3) updates adjacent cities' unavailable tiles.
 
-FIXME: Sometimes it is not neccesary to send cities because the goverment
-       doesn't care if a unit is away or not.
+FIXME: Sometimes it is not necessary to send cities because the goverment
+       doesn't care whether a unit is away or not.
 **************************************************************************/
-static void handle_unit_move_consequences(struct unit *punit,
-                                         struct tile *src_tile,
-                                         struct tile *dst_tile)
+static void unit_move_consequences(struct unit *punit,
+                                  struct tile *src_tile,
+                                  struct tile *dst_tile)
 {
   struct city *fromcity = tile_city(src_tile);
   struct city *tocity = tile_city(dst_tile);
   struct city *homecity = NULL;
   struct player *pplayer = unit_owner(punit);
-  /*  struct government *g = government_of_player(pplayer);*/
   bool refresh_homecity = FALSE;
   
-  if (punit->homecity != 0)
+  if (0 != punit->homecity) {
     homecity = game_find_city_by_number(punit->homecity);
+  }
 
-  if (tocity)
-    handle_unit_enter_city(punit, tocity);
+  if (tocity) {
+    unit_enter_city(punit, tocity);
+  }
 
   /* We only do this for non-AI players to now make sure the AI turns
      doesn't take too long. Perhaps we should make a special refresh_city
@@ -2565,12 +2599,31 @@
 **************************************************************************/
 static void check_unit_activity(struct unit *punit)
 {
-  if (punit->activity != ACTIVITY_IDLE
-      && punit->activity != ACTIVITY_SENTRY
-      && punit->activity != ACTIVITY_EXPLORE
-      && punit->activity != ACTIVITY_GOTO) {
+  switch (punit->activity) {
+  case ACTIVITY_IDLE:
+  case ACTIVITY_SENTRY:
+  case ACTIVITY_EXPLORE:
+  case ACTIVITY_GOTO:
+    break;
+  case ACTIVITY_POLLUTION:
+  case ACTIVITY_ROAD:
+  case ACTIVITY_MINE:
+  case ACTIVITY_IRRIGATE:
+  case ACTIVITY_FORTIFIED:
+  case ACTIVITY_FORTRESS:
+  case ACTIVITY_RAILROAD:
+  case ACTIVITY_PILLAGE:
+  case ACTIVITY_TRANSFORM:
+  case ACTIVITY_UNKNOWN:
+  case ACTIVITY_AIRBASE:
+  case ACTIVITY_FORTIFYING:
+  case ACTIVITY_FALLOUT:
+  case ACTIVITY_PATROL_UNUSED:
+  case ACTIVITY_BASE:
+  case ACTIVITY_LAST:
     set_unit_activity(punit, ACTIVITY_IDLE);
-  }
+    break;
+  };
 }
 
 /**************************************************************************
@@ -2635,7 +2688,7 @@
       vision_clear_sight(old_vision);
       vision_free(old_vision);
 
-      handle_unit_move_consequences(pcargo, psrctile, pdesttile);
+      unit_move_consequences(pcargo, psrctile, pdesttile);
     } unit_list_iterate_end;
     unit_list_unlink_all(cargo_units);
     unit_list_free(cargo_units);
@@ -2697,8 +2750,7 @@
    * be cities at both src and dest under some rulesets.
    *   If unit is about to take over enemy city, unit is seen by
    * those players seeing inside cities of old city owner. After city
-   * has been transferred, updated info is sent inside
-   * handle_unit_enter_city() */
+   * has been transferred, updated info is sent by unit_enter_city() */
   send_unit_info_to_onlookers(NULL, punit, psrctile, FALSE);
     
   /* Special checks for ground units in the ocean. */
@@ -2760,7 +2812,7 @@
     } players_iterate_end;
   } square_iterate_end;
 
-  handle_unit_move_consequences(punit, psrctile, pdesttile);
+  unit_move_consequences(punit, psrctile, pdesttile);
   script_signal_emit("unit_moved", 3,
                     API_TYPE_UNIT, punit,
                     API_TYPE_TILE, psrctile,
@@ -3010,7 +3062,7 @@
 
       freelog(LOG_DEBUG, "  moving to %d,%d",
              dst_tile->x, dst_tile->y);
-      res = handle_unit_move_request(punit, dst_tile, FALSE, !last_order);
+      res = unit_move_handling(punit, dst_tile, FALSE, !last_order);
       if (!player_find_unit_by_id(pplayer, unitid)) {
        freelog(LOG_DEBUG, "  unit died while moving.");
        /* A player notification should already have been sent. */
Index: server/unithand.c
===================================================================
--- server/unithand.c   (revision 14241)
+++ server/unithand.c   (working copy)
@@ -61,13 +61,11 @@
 static void city_add_unit(struct player *pplayer, struct unit *punit);
 static void city_build(struct player *pplayer, struct unit *punit,
                       char *name);
-static void handle_unit_activity_request_targeted(struct unit *punit,
-                                                 enum unit_activity
-                                                 new_activity,
-                                                 enum tile_special_type
-                                                 new_target);
-static void handle_unit_activity_request_base(struct unit *punit,
-                                              enum base_type_id base);
+static void unit_activity_handling_targeted(struct unit *punit,
+                                           enum unit_activity new_activity,
+                                           enum tile_special_type new_target);
+static void unit_activity_handling_base(struct unit *punit,
+                                        enum base_type_id base);
 static bool base_handle_unit_establish_trade(struct player *pplayer, int 
unit_id, struct city *pcity_dest);
 static bool unit_bombard(struct unit *punit, struct tile *ptile);
 
@@ -79,20 +77,39 @@
   struct unit *punit = player_find_unit_by_id(pplayer, unit_id);
   struct city *pcity = game_find_city_by_number(city_id);
 
-  if (punit && pcity) {
-    (void) do_airline(punit, pcity);
+  if (NULL == punit) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_airlift()"
+           " invalid unit %d",
+           unit_id);
+    return;
   }
+
+  if (NULL == pcity) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_airlift()"
+           " invalid city %d",
+           city_id);
+    return;
+  }
+
+  (void) do_airline(punit, pcity);
 }
 
 /**************************************************************************
  Upgrade all units of a given type.
 **************************************************************************/
-void handle_unit_type_upgrade(struct player *pplayer, Unit_type_id type)
+void handle_unit_type_upgrade(struct player *pplayer, Unit_type_id uti)
 {
-  struct unit_type *from_unittype = utype_by_number(type), *to_unittype;
+  struct unit_type *to_unittype;
+  struct unit_type *from_unittype = utype_by_number(uti);
   int number_of_upgraded_units = 0;
 
-  if (!from_unittype) {
+  if (NULL == from_unittype) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_type_upgrade()"
+           " invalid unit type %d",
+           uti);
     return;
   }
 
@@ -144,10 +161,14 @@
 **************************************************************************/
 void handle_unit_upgrade(struct player *pplayer, int unit_id)
 {
+  char buf[512];
   struct unit *punit = player_find_unit_by_id(pplayer, unit_id);
-  char buf[512];
-  
-  if (!punit) {
+
+  if (NULL == punit) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_upgrade()"
+           " invalid unit %d",
+           unit_id);
     return;
   }
 
@@ -187,10 +208,23 @@
   struct unit *punit = game_find_unit_by_number(target_id);
   struct city *pcity = game_find_city_by_number(target_id);
 
-  if (!pdiplomat || !unit_has_type_flag(pdiplomat, F_DIPLOMAT)) {
+  if (NULL == pdiplomat) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_diplomat_query()"
+           " invalid diplomat %d",
+           diplomat_id);
     return;
   }
 
+  if (!unit_has_type_flag(pdiplomat, F_DIPLOMAT)) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_diplomat_query()"
+           " %s (%d) is not diplomat",
+           unit_rule_name(pdiplomat),
+           diplomat_id);
+    return;
+  }
+
   switch (action_type) {
   case DIPLOMAT_BRIBE:
     if (punit && diplomat_can_do_action(pdiplomat, DIPLOMAT_BRIBE,
@@ -233,25 +267,38 @@
                                 enum diplomat_actions action_type)
 {
   struct unit *pdiplomat = player_find_unit_by_id(pplayer, diplomat_id);
-  struct unit *pvictim = game_find_unit_by_number(target_id);
+  struct unit *punit = game_find_unit_by_number(target_id);
   struct city *pcity = game_find_city_by_number(target_id);
 
-  if (!pdiplomat || !unit_has_type_flag(pdiplomat, F_DIPLOMAT)) {
+  if (NULL == pdiplomat) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_diplomat_action()"
+           " invalid diplomat %d",
+           diplomat_id);
     return;
   }
 
+  if (!unit_has_type_flag(pdiplomat, F_DIPLOMAT)) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_diplomat_action()"
+           " %s (%d) is not diplomat",
+           unit_rule_name(pdiplomat),
+           diplomat_id);
+    return;
+  }
+
   if(pdiplomat->moves_left > 0) {
     switch(action_type) {
     case DIPLOMAT_BRIBE:
-      if(pvictim && diplomat_can_do_action(pdiplomat, DIPLOMAT_BRIBE,
-                                          pvictim->tile)) {
-       diplomat_bribe(pplayer, pdiplomat, pvictim);
+      if (punit && diplomat_can_do_action(pdiplomat, DIPLOMAT_BRIBE,
+                                         punit->tile)) {
+       diplomat_bribe(pplayer, pdiplomat, punit);
       }
       break;
     case SPY_SABOTAGE_UNIT:
-      if(pvictim && diplomat_can_do_action(pdiplomat, SPY_SABOTAGE_UNIT,
-                                          pvictim->tile)) {
-       spy_sabotage_unit(pplayer, pdiplomat, pvictim);
+      if (punit && diplomat_can_do_action(pdiplomat, SPY_SABOTAGE_UNIT,
+                                         punit->tile)) {
+       spy_sabotage_unit(pplayer, pdiplomat, punit);
       }
       break;
      case DIPLOMAT_SABOTAGE:
@@ -288,8 +335,7 @@
     case DIPLOMAT_MOVE:
       if(pcity && diplomat_can_do_action(pdiplomat, DIPLOMAT_MOVE,
                                         pcity->tile)) {
-       (void) handle_unit_move_request(pdiplomat, pcity->tile,
-                                       FALSE, TRUE);
+       (void) unit_move_handling(pdiplomat, pcity->tile, FALSE, TRUE);
       }
       break;
     case DIPLOMAT_STEAL:
@@ -313,7 +359,7 @@
 
   Note that unit may die in the process.
 **************************************************************************/
-void real_unit_change_homecity(struct unit *punit, struct city *new_pcity)
+void unit_change_homecity_handling(struct unit *punit, struct city *new_pcity)
 {
   struct city *old_pcity = game_find_city_by_number(punit->homecity);
   struct player *old_owner = unit_owner(punit);
@@ -383,9 +429,17 @@
   struct unit *punit = player_find_unit_by_id(pplayer, unit_id);
   struct city *pcity = player_find_city_by_id(pplayer, city_id);
 
-  if (punit && pcity && can_unit_change_homecity_to(punit, pcity)) {
-    real_unit_change_homecity(punit, pcity);
+  if (NULL == punit) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_change_homecity()"
+           " invalid unit %d",
+           unit_id);
+    return;
   }
+
+  if (pcity && can_unit_change_homecity_to(punit, pcity)) {
+    unit_change_homecity_handling(punit, pcity);
+  }
 }
 
 /**************************************************************************
@@ -394,10 +448,14 @@
 **************************************************************************/
 void handle_unit_disband(struct player *pplayer, int unit_id)
 {
+  struct city *pcity;
   struct unit *punit = player_find_unit_by_id(pplayer, unit_id);
-  struct city *pcity;
 
-  if (!punit) {
+  if (NULL == punit) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_disband()"
+           " invalid unit %d",
+           unit_id);
     return;
   }
 
@@ -572,10 +630,14 @@
 **************************************************************************/
 void handle_unit_build_city(struct player *pplayer, int unit_id, char *name)
 {
+  enum add_build_city_result res;
   struct unit *punit = player_find_unit_by_id(pplayer, unit_id);
-  enum add_build_city_result res;
 
-  if (!punit) {
+  if (NULL == punit) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_build_city()"
+           " invalid unit %d",
+           unit_id);
     return;
   }
 
@@ -599,7 +661,11 @@
 {
   struct unit *punit = player_find_unit_by_id(pplayer, unit_id);
 
-  if (!punit) {
+  if (NULL == punit) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_change_activity()"
+           " invalid unit %d",
+           unit_id);
     return;
   }
 
@@ -614,13 +680,13 @@
     punit->goto_tile = NULL;
 
     if (activity != ACTIVITY_BASE) {
-      handle_unit_activity_request_targeted(punit, activity, activity_target);
+      unit_activity_handling_targeted(punit, activity, activity_target);
     } else {
       if (!base_by_number(activity_base)) {
         /* Illegal base type */
         return;
       }
-      handle_unit_activity_request_base(punit, activity_base);
+      unit_activity_handling_base(punit, activity_base);
     }
 
     /* Exploring is handled here explicitly, since the player expects to
@@ -651,15 +717,46 @@
   struct unit *punit = player_find_unit_by_id(pplayer, unit_id);
   struct tile *ptile = map_pos_to_tile(x, y);
 
-  if (!ptile || !punit) {
+  if (NULL == punit) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_move()"
+           " invalid unit %d",
+           unit_id);
     return;
   }
 
-  if (!is_tiles_adjacent(punit->tile, ptile)
-      || !is_player_phase(unit_owner(punit), game.info.phase)) {
+  if (NULL == ptile) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_move()"
+           " invalid %s (%d) tile (%d,%d)",
+           unit_rule_name(punit),
+           unit_id,
+           x, y);
     return;
   }
-  (void) handle_unit_move_request(punit, ptile, FALSE, FALSE);
+
+  if (!is_tiles_adjacent(punit->tile, ptile)) {
+    /* Client is out of sync, ignore */
+    freelog(LOG_VERBOSE, "handle_unit_move()"
+           " invalid %s (%d) move (%d,%d)",
+           unit_rule_name(punit),
+           unit_id,
+           x, y);
+    return;
+  }
+
+  if (!is_player_phase(unit_owner(punit), game.info.phase)) {
+    /* Client is out of sync, ignore */
+    freelog(LOG_VERBOSE, "handle_unit_move()"
+           " invalid %s (%d) %s != phase %d",
+           unit_rule_name(punit),
+           unit_id,
+           nation_rule_name(nation_of_unit(punit)),
+           game.info.phase);
+    return;
+  }
+
+  (void) unit_move_handling(punit, ptile, FALSE, FALSE);
 }
 
 /**************************************************************************
@@ -808,9 +905,9 @@
 
 /**************************************************************************
 This function assumes the attack is legal. The calling function should have
-already made all neccesary checks.
+already made all necessary checks.
 **************************************************************************/
-static void handle_unit_attack_request(struct unit *punit, struct unit 
*pdefender)
+static void unit_attack_handling(struct unit *punit, struct unit *pdefender)
 {
   struct unit *plooser, *pwinner;
   struct city *pcity;
@@ -958,7 +1055,7 @@
     int old_moves = punit->moves_left;
     int full_moves = unit_move_rate(punit);
     punit->moves_left = full_moves;
-    if (handle_unit_move_request(punit, def_tile, FALSE, FALSE)) {
+    if (unit_move_handling(punit, def_tile, FALSE, FALSE)) {
       punit->moves_left = old_moves - (full_moves - punit->moves_left);
       if (punit->moves_left < 0) {
        punit->moves_left = 0;
@@ -1038,8 +1135,8 @@
 
   FIXME: This function needs a good cleaning.
 **************************************************************************/
-bool handle_unit_move_request(struct unit *punit, struct tile *pdesttile,
-                              bool igzoc, bool move_diplomat_city)
+bool unit_move_handling(struct unit *punit, struct tile *pdesttile,
+                        bool igzoc, bool move_diplomat_city)
 {
   struct player *pplayer = unit_owner(punit);
   struct city *pcity = tile_city(pdesttile);
@@ -1168,7 +1265,7 @@
         return FALSE;
       }
       
-      handle_unit_attack_request(punit, victim);
+      unit_attack_handling(punit, victim);
       return TRUE;
     } else {
       assert(is_enemy_city_tile(pdesttile, pplayer) != NULL);
@@ -1176,7 +1273,7 @@
       if (unit_has_type_flag(punit, F_NUCLEAR)) {
         if (move_unit(punit, pcity->tile, 0)) {
           /* Survived dangers of moving */
-          handle_unit_attack_request(punit, punit); /* Boom! */
+          unit_attack_handling(punit, punit); /* Boom! */
         }
         return TRUE;
       }
@@ -1227,13 +1324,21 @@
 **************************************************************************/
 void handle_unit_help_build_wonder(struct player *pplayer, int unit_id)
 {
+  const char *text;
+  struct city *pcity_dest;
   struct unit *punit = player_find_unit_by_id(pplayer, unit_id);
-  struct city *pcity_dest;
-  const char *text;
 
-  if (!punit || !unit_has_type_flag(punit, F_HELP_WONDER)) {
+  if (NULL == punit) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_help_build_wonder()"
+           " invalid unit %d",
+           unit_id);
     return;
   }
+
+  if (!unit_has_type_flag(punit, F_HELP_WONDER)) {
+    return;
+  }
   pcity_dest = tile_city(punit->tile);
   
   if (!pcity_dest || !unit_can_help_build_wonder(punit, pcity_dest)) {
@@ -1268,16 +1373,24 @@
 **************************************************************************/
 static bool base_handle_unit_establish_trade(struct player *pplayer, int 
unit_id, struct city *pcity_dest)
 {
+  int revenue, i;
+  bool can_establish, home_full = FALSE, dest_full = FALSE;
+  struct city *pcity_homecity; 
   struct unit *punit = player_find_unit_by_id(pplayer, unit_id);
   struct city *pcity_out_of_home = NULL, *pcity_out_of_dest = NULL;
-  struct city *pcity_homecity; 
-  int revenue, i;
-  bool home_full = FALSE, dest_full = FALSE, can_establish;
-  
-  if (!punit || !unit_has_type_flag(punit, F_TRADE_ROUTE)) {
+
+  if (NULL == punit) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "base_handle_unit_establish_trade()"
+           " invalid unit %d",
+           unit_id);
     return FALSE;
   }
 
+  if (!unit_has_type_flag(punit, F_TRADE_ROUTE)) {
+    return FALSE;
+  }
+
   /* if no destination city is passed in,
    *  check whether the unit is already in the city */
   if (!pcity_dest) { 
@@ -1515,9 +1628,15 @@
 {
   struct unit *punit = player_find_unit_by_id(pplayer, unit_id);
 
-  if (punit) {
-    punit->battlegroup = CLIP(-1, battlegroup, MAX_NUM_BATTLEGROUPS);
+  if (NULL == punit) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_battlegroup()"
+           " invalid unit %d",
+           unit_id);
+    return;
   }
+
+  punit->battlegroup = CLIP(-1, battlegroup, MAX_NUM_BATTLEGROUPS);
 }
 
 /**************************************************************************
@@ -1527,9 +1646,17 @@
 {
   struct unit *punit = player_find_unit_by_id(pplayer, unit_id);
 
-  if (!punit || !can_unit_do_autosettlers(punit))
+  if (NULL == punit) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_autosettlers()"
+           " invalid unit %d",
+           unit_id);
     return;
+  }
 
+  if (!can_unit_do_autosettlers(punit))
+    return;
+
   punit->ai.control = TRUE;
   send_unit_info(pplayer, punit);
 }
@@ -1537,9 +1664,9 @@
 /**************************************************************************
 ...
 **************************************************************************/
-static void handle_unit_activity_dependencies(struct unit *punit,
-                               enum unit_activity old_activity,
-                               enum tile_special_type old_target)
+static void unit_activity_dependencies(struct unit *punit,
+                                      enum unit_activity old_activity,
+                                      enum tile_special_type old_target)
 {
   switch (punit->activity) {
   case ACTIVITY_IDLE:
@@ -1581,8 +1708,8 @@
 /**************************************************************************
 ...
 **************************************************************************/
-void handle_unit_activity_request(struct unit *punit, 
-                                 enum unit_activity new_activity)
+void unit_activity_handling(struct unit *punit,
+                            enum unit_activity new_activity)
 {
   if (can_unit_do_activity(punit, new_activity)) {
     enum unit_activity old_activity = punit->activity;
@@ -1591,18 +1718,16 @@
     free_unit_orders(punit);
     set_unit_activity(punit, new_activity);
     send_unit_info(NULL, punit);
-    handle_unit_activity_dependencies(punit, old_activity, old_target);
+    unit_activity_dependencies(punit, old_activity, old_target);
   }
 }
 
 /**************************************************************************
 ...
 **************************************************************************/
-static void handle_unit_activity_request_targeted(struct unit *punit,
-                                                 enum unit_activity
-                                                 new_activity,
-                                                 enum tile_special_type
-                                                 new_target)
+static void unit_activity_handling_targeted(struct unit *punit,
+                                           enum unit_activity new_activity,
+                                           enum tile_special_type new_target)
 {
   if (can_unit_do_activity_targeted(punit, new_activity, new_target,
                                     BASE_LAST)) {
@@ -1612,15 +1737,15 @@
     free_unit_orders(punit);
     set_unit_activity_targeted(punit, new_activity, new_target);
     send_unit_info(NULL, punit);    
-    handle_unit_activity_dependencies(punit, old_activity, old_target);
+    unit_activity_dependencies(punit, old_activity, old_target);
   }
 }
 
 /**************************************************************************
 ...
 **************************************************************************/
-static void handle_unit_activity_request_base(struct unit *punit, 
-                                              enum base_type_id base)
+static void unit_activity_handling_base(struct unit *punit,
+                                        enum base_type_id base)
 {
   if (can_unit_do_activity_base(punit, base)) {
     enum unit_activity old_activity = punit->activity;
@@ -1629,7 +1754,7 @@
     free_unit_orders(punit);
     set_unit_activity_base(punit, base);
     send_unit_info(NULL, punit);
-    handle_unit_activity_dependencies(punit, old_activity, old_target);
+    unit_activity_dependencies(punit, old_activity, old_target);
   }
 }
 
@@ -1638,16 +1763,28 @@
 ****************************************************************************/
 void handle_unit_load(struct player *pplayer, int cargo_id, int trans_id)
 {
-  /* A player may only load their units, but they may be loaded into
-   * other players transporters (depending on the rules in
-   * can_unit_load). */
   struct unit *pcargo = player_find_unit_by_id(pplayer, cargo_id);
   struct unit *ptrans = game_find_unit_by_number(trans_id);
 
-  if (!pcargo || !ptrans) {
+  if (NULL == pcargo) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_load()"
+           " invalid cargo %d",
+           cargo_id);
     return;
   }
 
+  if (NULL == ptrans) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_load()"
+           " invalid transport %d",
+           trans_id);
+    return;
+  }
+
+  /* A player may only load their units, but they may be loaded into
+   * other player's transporters, depending on the rules in
+   * can_unit_load(). */
   if (!can_unit_load(pcargo, ptrans)) {
     return;
   }
@@ -1665,10 +1802,22 @@
   struct unit *pcargo = game_find_unit_by_number(cargo_id);
   struct unit *ptrans = game_find_unit_by_number(trans_id);
 
-  if (!pcargo || !ptrans) {
+  if (NULL == pcargo) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_unload()"
+           " invalid cargo %d",
+           cargo_id);
     return;
   }
 
+  if (NULL == ptrans) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_unload()"
+           " invalid transport %d",
+           trans_id);
+    return;
+  }
+
   /* You are allowed to unload a unit if it is yours or if the transporter
    * is yours. */
   if (unit_owner(pcargo) != pplayer && unit_owner(ptrans) != pplayer) {
@@ -1694,10 +1843,15 @@
 {
   struct unit *punit = player_find_unit_by_id(pplayer, unit_id);
 
-  if (!punit) {
+  if (NULL == punit) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_nuke()"
+           " invalid unit %d",
+           unit_id);
     return;
   }
-  handle_unit_attack_request(punit, punit);
+
+  unit_attack_handling(punit, punit);
 }
 
 /**************************************************************************
@@ -1708,11 +1862,25 @@
 {
   struct unit *punit = player_find_unit_by_id(pplayer, unit_id);
   struct tile *ptile = map_pos_to_tile(x, y);
-  
-  if (!punit || !ptile) {
+
+  if (NULL == punit) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_paradrop_to()"
+           " invalid unit %d",
+           unit_id);
     return;
   }
 
+  if (NULL == ptile) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_paradrop_to()"
+           " invalid %s (%d) tile (%d,%d)",
+           unit_rule_name(punit),
+           unit_id,
+           x, y);
+    return;
+  }
+
   (void) do_paradrop(punit, ptile);
 }
 
@@ -1722,21 +1890,51 @@
 void handle_unit_orders(struct player *pplayer,
                        struct packet_unit_orders *packet)
 {
+  int i;
   struct unit *punit = player_find_unit_by_id(pplayer, packet->unit_id);
   struct tile *src_tile = map_pos_to_tile(packet->src_x, packet->src_y);
-  int i;
 
-  if (!punit || packet->length < 0 || punit->activity != ACTIVITY_IDLE
-      || packet->length > MAX_LEN_ROUTE) {
+  if (NULL == punit) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_orders()"
+           " invalid unit %d",
+           packet->unit_id);
     return;
   }
 
+  if (0 > packet->length || MAX_LEN_ROUTE < packet->length) {
+    /* Shouldn't happen */
+    freelog(LOG_ERROR, "handle_unit_orders()"
+           " invalid %s (%d) packet length %d (max %d)",
+           unit_rule_name(punit),
+           packet->unit_id,
+           packet->length,
+           MAX_LEN_ROUTE);
+    return;
+  }
+
+  if (ACTIVITY_IDLE != punit->activity) {
+    freelog(LOG_ERROR, "handle_unit_orders()"
+           " invalid %s (%d) activity %d (should be %d)",
+           unit_rule_name(punit),
+           packet->unit_id,
+           punit->activity,
+           ACTIVITY_IDLE);
+    return;
+  }
+
   if (src_tile != punit->tile) {
     /* Failed sanity check.  Usually this happens if the orders were sent
      * in the previous turn, and the client thought the unit was in a
      * different position than it's actually in.  The easy solution is to
      * discard the packet.  We don't send an error message to the client
      * here (though maybe we should?). */
+    freelog(LOG_DEBUG, "handle_unit_orders()"
+           " invalid %s (%d) tile (%d,%d != %d,%d)",
+           unit_rule_name(punit),
+           packet->unit_id,
+           packet->src_x, packet->src_y,
+           TILE_XY(punit->tile));
     return;
   }
 
Index: server/unithand.h
===================================================================
--- server/unithand.h   (revision 14241)
+++ server/unithand.h   (working copy)
@@ -18,10 +18,11 @@
 
 #include "hand_gen.h"
 
-bool handle_unit_move_request(struct unit *punit, struct tile *ptile,
-                            bool igzoc, bool move_diplomat_city);
-void handle_unit_activity_request(struct unit *punit, 
-                                 enum unit_activity new_activity);
-void real_unit_change_homecity(struct unit *punit, struct city *new_pcity);
+void unit_activity_handling(struct unit *punit,
+                            enum unit_activity new_activity);
+void unit_change_homecity_handling(struct unit *punit, struct city *new_pcity);
 
+bool unit_move_handling(struct unit *punit, struct tile *pdesttile,
+                        bool igzoc, bool move_diplomat_city);
+
 #endif  /* FC__UNITHAND_H */
Index: common/unit.c
===================================================================
--- common/unit.c       (revision 14241)
+++ common/unit.c       (working copy)
@@ -112,7 +112,7 @@
     }
   } else { /* Action against a unit at a tile */
     /* If it is made possible to do action against allied units
-       handle_unit_move_request() should be changed so that pdefender
+       unit_move_handling() should be changed so that pdefender
        is also set to allied units */
     struct unit *punit;
 
Index: ai/aiferry.c
===================================================================
--- ai/aiferry.c        (revision 14241)
+++ ai/aiferry.c        (working copy)
@@ -501,7 +501,7 @@
 {
   unit_list_iterate_safe(ferry->tile->units, aunit) {
     if (aunit->transported_by == ferry->id) {
-      handle_unit_activity_request(aunit, ACTIVITY_IDLE);
+      unit_activity_handling(aunit, ACTIVITY_IDLE);
       aunit->ai.done = FALSE;
       ai_manage_unit(unit_owner(aunit), aunit);
     }
@@ -743,7 +743,7 @@
       }
       if (same_pos(punit->tile, dest_tile)) {
         /* Arrived */
-        handle_unit_activity_request(punit, ACTIVITY_IDLE);
+        unit_activity_handling(punit, ACTIVITY_IDLE);
       } else {
         /* We are in still transit */
         punit->ai.done = TRUE;
@@ -1056,7 +1056,7 @@
   UNIT_LOG(LOGLEVEL_FERRY, punit, "Ferryboat is not carrying anyone "
           "(moves left: %d).", punit->moves_left);
   aiferry_make_available(punit);
-  handle_unit_activity_request(punit, ACTIVITY_IDLE);
+  unit_activity_handling(punit, ACTIVITY_IDLE);
   ai_unit_new_role(punit, AIUNIT_NONE, NULL);
   CHECK_UNIT(punit);
 
Index: ai/aitools.c
===================================================================
--- ai/aitools.c        (revision 14241)
+++ ai/aitools.c        (working copy)
@@ -406,12 +406,12 @@
     return TRUE;
   }
   punit->goto_tile = ptile;
-  handle_unit_activity_request(punit, ACTIVITY_GOTO);
+  unit_activity_handling(punit, ACTIVITY_GOTO);
   alive = ai_unit_execute_path(punit, path);
   if (alive) {
-    handle_unit_activity_request(punit, ACTIVITY_IDLE);
-    send_unit_info(NULL, punit);
-    handle_unit_activity_request(punit, activity);
+    unit_activity_handling(punit, ACTIVITY_IDLE);
+    send_unit_info(NULL, punit); /* FIXME: probably duplicate */
+    unit_activity_handling(punit, activity);
     punit->goto_tile = old_tile; /* May be NULL. */
     send_unit_info(NULL, punit);
   }
@@ -840,7 +840,7 @@
 
   if (punit->activity == ACTIVITY_GOTO) {
     /* It would indicate we're going somewhere otherwise */
-    handle_unit_activity_request(punit, ACTIVITY_IDLE);
+    unit_activity_handling(punit, ACTIVITY_IDLE);
   }
 
   if (punit->ai.ai_role == AIUNIT_BUILD_CITY) {
@@ -957,7 +957,7 @@
     return;
   }
 
-  handle_unit_activity_request(bodyguard, ACTIVITY_IDLE);
+  unit_activity_handling(bodyguard, ACTIVITY_IDLE);
   (void) ai_unit_move(bodyguard, ptile);
 }
 
@@ -974,8 +974,8 @@
   assert(unit_owner(punit)->ai.control);
   assert(is_tiles_adjacent(punit->tile, ptile));
 
-  handle_unit_activity_request(punit, ACTIVITY_IDLE);
-  (void) handle_unit_move_request(punit, ptile, FALSE, FALSE);
+  unit_activity_handling(punit, ACTIVITY_IDLE);
+  (void) unit_move_handling(punit, ptile, FALSE, FALSE);
   alive = (game_find_unit_by_number(sanity) != NULL);
 
   if (alive && same_pos(ptile, punit->tile)
@@ -1040,8 +1040,8 @@
   }
 
   /* go */
-  handle_unit_activity_request(punit, ACTIVITY_IDLE);
-  (void) handle_unit_move_request(punit, ptile, FALSE, TRUE);
+  unit_activity_handling(punit, ACTIVITY_IDLE);
+  (void) unit_move_handling(punit, ptile, FALSE, TRUE);
 
   /* handle the results */
   if (game_find_unit_by_number(sanity) && same_pos(ptile, punit->tile)) {
Index: ai/aidiplomat.c
===================================================================
--- ai/aidiplomat.c     (revision 14241)
+++ ai/aidiplomat.c     (working copy)
@@ -285,7 +285,7 @@
     UNIT_LOG(LOG_ERROR, punit, "no moves left in ai_diplomat_city()!");
   }
 
-  handle_unit_activity_request(punit, ACTIVITY_IDLE);
+  unit_activity_handling(punit, ACTIVITY_IDLE);
 
 #define T(my_act,my_val)                                            \
   if (diplomat_can_do_action(punit, my_act, ctarget->tile)) {       \
Index: ai/aiair.c
===================================================================
--- ai/aiair.c  (revision 14241)
+++ ai/aiair.c  (working copy)
@@ -347,10 +347,9 @@
       /* goto would be aborted: "Aborting GOTO for AI attack procedures"
        * now actually need to attack */
       /* We could use ai_military_findvictim here, but I don't trust it... */
-      handle_unit_activity_request(punit, ACTIVITY_IDLE);
+      unit_activity_handling(punit, ACTIVITY_IDLE);
       if (is_tiles_adjacent(punit->tile, punit->goto_tile)) {
-        (void) handle_unit_move_request(punit, punit->goto_tile,
-                                       TRUE, FALSE);
+        (void) unit_move_handling(punit, punit->goto_tile, TRUE, FALSE);
       }
     } else if (ai_find_strategic_airbase(punit, &dst_tile)) {
       freelog(LOG_DEBUG, "%s will fly to (%i, %i) (%s) to fight there",
@@ -366,7 +365,7 @@
       freelog(LOG_DEBUG, "%s cannot find anything to kill and is staying put", 
               unit_rule_name(punit));
       punit->ai.done = TRUE;
-      handle_unit_activity_request(punit, ACTIVITY_IDLE);
+      unit_activity_handling(punit, ACTIVITY_IDLE);
     }
   }
 
Index: ai/aiunit.c
===================================================================
--- ai/aiunit.c (revision 14241)
+++ ai/aiunit.c (working copy)
@@ -944,7 +944,7 @@
     if (can_unit_do_activity(punit, ACTIVITY_PILLAGE)
        && is_land_barbarian(pplayer)) {
       /* land barbarians pillage */
-      handle_unit_activity_request(punit, ACTIVITY_PILLAGE);
+      unit_activity_handling(punit, ACTIVITY_PILLAGE);
     }
     ai_unit_new_role(punit, AIUNIT_NONE, NULL);
     return;
@@ -2092,17 +2092,17 @@
     struct city *pcity = tile_city(punit->tile);
 
     if (unit_list_find(punit->tile->units, punit->ai.ferryboat)) {
-      handle_unit_activity_request(punit, ACTIVITY_SENTRY);
+      unit_activity_handling(punit, ACTIVITY_SENTRY);
     } else if (pcity || punit->activity == ACTIVITY_IDLE) {
       /* We do not need to fortify in cities - we fortify and sentry
        * according to home defense setup, for easy debugging. */
       if (!pcity || punit->ai.ai_role == AIUNIT_DEFEND_HOME) {
         if (punit->activity == ACTIVITY_IDLE
             || punit->activity == ACTIVITY_SENTRY) {
-          handle_unit_activity_request(punit, ACTIVITY_FORTIFYING);
+          unit_activity_handling(punit, ACTIVITY_FORTIFYING);
         }
       } else {
-        handle_unit_activity_request(punit, ACTIVITY_SENTRY);
+        unit_activity_handling(punit, ACTIVITY_SENTRY);
       }
     }
   }
@@ -2376,7 +2376,7 @@
   if (leader->moves_left == 0
       || (can_unit_survive_at_tile(leader, leader->tile)
           && unit_list_size(leader->tile->units) > 1) ) {
-      handle_unit_activity_request(leader, ACTIVITY_SENTRY);
+      unit_activity_handling(leader, ACTIVITY_SENTRY);
       return;
   }
 
@@ -2467,7 +2467,7 @@
   }
 
   if (!closest_unit) {
-    handle_unit_activity_request(leader, ACTIVITY_IDLE);
+    unit_activity_handling(leader, ACTIVITY_IDLE);
     UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader: no enemy.");
     return;
   }
@@ -2497,7 +2497,7 @@
     if (same_pos(leader->tile, safest_tile)) {
       UNIT_LOG(LOG_DEBUG, leader, 
                "Barbarian leader: reached the safest position.");
-      handle_unit_activity_request(leader, ACTIVITY_IDLE);
+      unit_activity_handling(leader, ACTIVITY_IDLE);
       return;
     }
 
_______________________________________________
Freeciv-dev mailing list
Freeciv-dev@gna.org
https://mail.gna.org/listinfo/freeciv-dev

Reply via email to