Complete.Org: Mailing Lists: Archives: freeciv-dev: July 2003:
[Freeciv-Dev] (PR#4674) small clean up in unit vision range calculations
Home

[Freeciv-Dev] (PR#4674) small clean up in unit vision range calculations

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#4674) small clean up in unit vision range calculations
From: "Rafa³ Bursig" <bursig@xxxxxxxxx>
Date: Thu, 24 Jul 2003 15:15:33 -0700
Reply-to: rt@xxxxxxxxxxxxxx

Hi all

This is small clean up that remove some duplicate code.

Rafal
diff -u -r freeciv/ai/aiunit.c fc/ai/aiunit.c
--- freeciv/ai/aiunit.c Sun Jul 20 23:15:59 2003
+++ fc/ai/aiunit.c      Thu Jul 24 22:37:45 2003
@@ -406,7 +406,7 @@
                               struct unit *punit)
 {
   int land_score, ocean_score, known_land_score, known_ocean_score;
-  int range = unit_type(punit)->vision_range;
+  int range = get_unit_vision_range(punit);
   int desirable = 0;
   int unknown = 0;
   int continent;
diff -u -r freeciv/common/unit.c fc/common/unit.c
--- freeciv/common/unit.c       Mon Jul 21 17:05:11 2003
+++ fc/common/unit.c    Thu Jul 24 22:20:52 2003
@@ -1494,3 +1494,26 @@
   }
   free(punit);
 }
+
+/**************************************************************************
+  Get real unit vision range.
+**************************************************************************/
+int get_unit_vision_range(struct unit *punit)
+{
+  int base_vision = unit_type(punit)->vision_range;
+  
+  assert(base_vision > 0);
+    
+  if (map_has_special(punit->x, punit->y, S_FORTRESS)
+     && is_ground_unit(punit)
+     && player_knows_techs_with_flag(unit_owner(punit), TF_WATCHTOWER))
+  {
+    assert(game.watchtower_vision > 0);
+    assert(game.watchtower_extra_vision >= 0);
+    return MAX(base_vision,
+            MAX(game.watchtower_vision,
+                base_vision + game.watchtower_extra_vision));
+  }
+  
+  return base_vision;
+}
diff -u -r freeciv/common/unit.h fc/common/unit.h
--- freeciv/common/unit.h       Thu Jul 17 20:27:19 2003
+++ fc/common/unit.h    Thu Jul 24 22:43:38 2003
@@ -118,9 +118,12 @@
   int upkeep;
   int upkeep_food;
   int upkeep_gold;
-  bool foul;
   int fuel;
   int bribe_cost;
+  bool foul;
+  bool moved;
+  bool paradropped;
+  bool connecting;
   struct unit_ai ai;
   enum unit_activity activity;
   struct {
@@ -133,9 +136,6 @@
   int ord_map, ord_city;
   /* ord_map and ord_city are the order index of this unit in tile.units
      and city.units_supported; they are only used for save/reload */
-  bool moved;
-  bool paradropped;
-  bool connecting;
   int transported_by;
   struct goto_route *pgr;
 };
@@ -283,5 +283,6 @@
 struct unit *create_unit_virtual(struct player *pplayer, struct city *pcity,
                                  Unit_Type_id type, bool make_veteran);
 void destroy_unit_virtual(struct unit *punit);
-
+int get_unit_vision_range(struct unit *punit);
+  
 #endif  /* FC__UNIT_H */
diff -u -r freeciv/server/maphand.c fc/server/maphand.c
--- freeciv/server/maphand.c    Thu Jul 24 12:24:19 2003
+++ fc/server/maphand.c Thu Jul 24 22:56:10 2003
@@ -686,17 +686,10 @@
 **************************************************************************/
 void remove_unit_sight_points(struct unit *punit)
 {
-  int x = punit->x, y = punit->y;
-  struct player *pplayer = unit_owner(punit);
-
   freelog(LOG_DEBUG, "Removing unit sight points at  %i,%i", punit->x,
-         punit->y);
-
-  if (map_has_special(punit->x, punit->y, S_FORTRESS)
-      && unit_profits_of_watchtower(punit))
-    fog_area(pplayer, x, y, get_watchtower_vision(punit));
-  else
-    fog_area(pplayer, x, y, unit_type(punit)->vision_range);
+                                         punit->y);
+  fog_area(unit_owner(punit), punit->x, punit->y,
+                                       get_unit_vision_range(punit));
 }
 
 /**************************************************************************
diff -u -r freeciv/server/savegame.c fc/server/savegame.c
--- freeciv/server/savegame.c   Thu Jul 24 12:24:19 2003
+++ fc/server/savegame.c        Thu Jul 24 22:35:46 2003
@@ -1111,20 +1111,15 @@
     }
 
     {
-      int range = unit_type(punit)->vision_range;
+      int range = get_unit_vision_range(punit);
       square_iterate(punit->x, punit->y, range, x1, y1) {
        map_set_known(x1, y1, plr);
       } square_iterate_end;
     }
 
     /* allocate the unit's contribution to fog of war */
-    if (unit_profits_of_watchtower(punit)
-       && map_has_special(punit->x, punit->y, S_FORTRESS))
-      unfog_area(unit_owner(punit), punit->x, punit->y,
-                get_watchtower_vision(punit));
-    else
-      unfog_area(unit_owner(punit), punit->x, punit->y,
-                unit_type(punit)->vision_range);
+    unfog_area(unit_owner(punit), punit->x, punit->y,
+                                        get_unit_vision_range(punit));
 
     unit_list_insert_back(&plr->units, punit);
 
diff -u -r freeciv/server/unittools.c fc/server/unittools.c
--- freeciv/server/unittools.c  Thu Jul 24 18:10:17 2003
+++ fc/server/unittools.c       Thu Jul 24 22:31:48 2003
@@ -1419,16 +1419,11 @@
 void upgrade_unit(struct unit *punit, Unit_Type_id to_unit)
 {
   struct player *pplayer = unit_owner(punit);
-  int range;
   int old_mr = unit_move_rate(punit), old_hp = unit_type(punit)->hp;
-
   /* save old vision range */
-  if (map_has_special(punit->x, punit->y, S_FORTRESS)
-      && unit_profits_of_watchtower(punit))
-    range = get_watchtower_vision(punit);
-  else
-    range = unit_type(punit)->vision_range;
-
+  int range = get_unit_vision_range(punit);
+  
+  /* upgrade unit */
   punit->type = to_unit;
 
   /* Scale HP and MP, rounding down.  Be careful with integer arithmetic,
@@ -1440,15 +1435,9 @@
   conn_list_do_buffer(&pplayer->connections);
 
   /* apply new vision range */
-  if (map_has_special(punit->x, punit->y, S_FORTRESS)
-      && unit_profits_of_watchtower(punit))
-    unfog_area(pplayer, punit->x, punit->y, get_watchtower_vision(punit));
-  else
-    unfog_area(pplayer, punit->x, punit->y,
-              get_unit_type(to_unit)->vision_range);
-
-  fog_area(pplayer,punit->x,punit->y,range);
-
+  fog_area(pplayer, punit->x, punit->y, range);
+  unfog_area(pplayer, punit->x, punit->y, get_unit_vision_range(punit));
+  
   send_unit_info(NULL, punit);
   conn_list_do_unbuffer(&pplayer->connections);
 }
@@ -1517,13 +1506,7 @@
     send_city_info(pplayer, pcity);
   }
 
-  if (map_has_special(x, y, S_FORTRESS)
-      && unit_profits_of_watchtower(punit)) {
-    unfog_area(pplayer, punit->x, punit->y, get_watchtower_vision(punit));
-  } else {
-    unfog_area(pplayer, x, y, unit_type(punit)->vision_range);
-  }
-
+  unfog_area(pplayer, x, y, get_unit_vision_range(punit));
   send_unit_info(NULL, punit);
   maybe_make_contact(x, y, unit_owner(punit));
   wakeup_neighbor_sentries(punit);
@@ -2611,15 +2594,9 @@
      wake them up if the punit is farther away than 3. */
   square_iterate(punit->x, punit->y, 3, x, y) {
     unit_list_iterate(map_get_tile(x, y)->units, penemy) {
-      int range;
+      int range = get_unit_vision_range(penemy);
       enum unit_move_type move_type = unit_type(penemy)->move_type;
       enum tile_terrain_type terrain = map_get_terrain(x, y);
-
-      if (map_has_special(x, y, S_FORTRESS)
-         && unit_profits_of_watchtower(penemy))
-       range = get_watchtower_vision(penemy);
-      else
-       range = unit_type(penemy)->vision_range;
       
       if (!pplayers_allied(unit_owner(punit), unit_owner(penemy))
          && penemy->activity == ACTIVITY_SENTRY
@@ -2932,13 +2909,7 @@
 static bool maybe_cancel_patrol_due_to_enemy(struct unit *punit)
 {
   bool cancel = FALSE;
-  int range;
-
-  if (map_has_special(punit->x, punit->y, S_FORTRESS)
-      && unit_profits_of_watchtower(punit))
-    range = get_watchtower_vision(punit);
-  else
-    range = unit_type(punit)->vision_range;
+  int range = get_unit_vision_range(punit);
   
   square_iterate(punit->x, punit->y, range, x, y) {
     struct unit *penemy =

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