Complete.Org: Mailing Lists: Archives: freeciv-dev: March 2002:
[Freeciv-Dev] [PATCH] try to cleanup assess_danger part2 (PR#1322)
Home

[Freeciv-Dev] [PATCH] try to cleanup assess_danger part2 (PR#1322)

[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] [PATCH] try to cleanup assess_danger part2 (PR#1322)
From: Markus Linnala <maage@xxxxxxxxx>
Date: Sun, 10 Mar 2002 09:32:26 -0800 (PST)

Part 2 of assess_danger cleanup. This is on top of '[PATCH} try to cleanup 
assess_danger (PR#1321)'.

Move common calculations to function.

-/* yes, I know cloning all this code is bad form.  I don't really
-want to write a funct that takes nine ints by reference. -- Syela */

Now we need only 6 ints by reference.

diff -ur -X freeciv/diff_ignore 
freeciv-cleanup-assess-danger-1/ai/advmilitary.c 
freeciv-cleanup-assess-danger-2/ai/advmilitary.c
--- freeciv-cleanup-assess-danger-1/ai/advmilitary.c    Sun Mar 10 19:08:28 2002
+++ freeciv-cleanup-assess-danger-2/ai/advmilitary.c    Sun Mar 10 19:28:02 2002
@@ -220,13 +220,83 @@
     assess_danger(pcity);
   city_list_iterate_end;
 }
-         
+
+static void make_virtual_unit(struct unit *virtualunit, 
+                             int owner, 
+                             int x,
+                             int y,
+                             Unit_Type_id type,
+                             bool veteran,
+                             int hp)
+{
+/* this memset didn't work because of syntax that
+the intrepid David Pfitzner discovered was in error. -- Syela */
+  memset(virtualunit, 0, sizeof(struct unit));
+  virtualunit->owner = owner;
+  virtualunit->x = x;
+  virtualunit->y = y;
+  virtualunit->type = type;
+  virtualunit->veteran = veteran;
+  virtualunit->hp = hp;
+}
+
+static void assess_danger_unit_helper(struct player *pplayer, struct city 
*pcity, struct unit *punit, 
+                                     int boatid, int boatdist, int boatspeed, 
+                                     bool def_against_diplomat, bool pikemen,
+                                     int *badmojo, int *danger, int 
*danger_wall, int *danger_coastal, 
+                                     int *danger_sam, int *danger_sdi)
+{
+  int move_rate;
+  int danger_by_unit, danger_by_unit_scaled;
+  int dist;
+  bool igwall;
+
+  move_rate = unit_type(punit)->move_rate;
+  danger_by_unit = assess_danger_unit(pcity, punit);
+  dist = assess_distance(pcity, punit, move_rate, boatid, boatdist, boatspeed);
+
+  igwall = unit_really_ignores_citywalls(punit);
+  if ((is_ground_unit(punit) && danger_by_unit != 0) ||
+      (is_ground_units_transport(punit))) {
+    if (dist <= move_rate * 3) pcity->ai.urgency++;
+    if (dist <= move_rate) pcity->ai.grave_danger++;
+/* NOTE: This should actually implement grave_danger, which is supposed
+to be a feedback-sensitive formula for immediate danger.  I'm having
+second thoughts about the best implementation, and therefore this
+will have to wait until after 1.7.0.  I don't want to do anything I'm
+not totally sure about and can't thoroughly test in the hours before
+the release.  The AI is in fact vulnerable to gang-attacks, but I'm
+content to let it remain that way for now. -- Syela 980805 */
+  }
+  
+  if (unit_flag(punit, F_HORSE)) {
+    if (pikemen) danger_by_unit /= 2;
+    else ai_wants_role_unit(pplayer, pcity, F_PIKEMEN, (danger_by_unit * 
move_rate / (dist * 2)));
+  }
+  
+  if (unit_flag(punit, F_DIPLOMAT) && (dist <= 2 * move_rate))
+    pcity->ai.diplomat_threat |= !def_against_diplomat;
+  
+  danger_by_unit *= danger_by_unit;
+  danger_by_unit_scaled = danger_by_unit * move_rate / dist;
+
+  if (!igwall) *danger_wall += danger_by_unit_scaled;
+  else if (is_sailing_unit(punit)) *danger_coastal += danger_by_unit_scaled;
+  else if (is_air_unit(punit) && !unit_flag(punit, F_NUCLEAR)) *danger_sam += 
danger_by_unit_scaled;
+  if (unit_flag(punit, F_MISSILE)) *danger_sdi += danger_by_unit * move_rate / 
MAX(move_rate, dist);
+  if (!unit_flag(punit, F_NUCLEAR)) { /* only SDI helps against NUCLEAR */
+    danger_by_unit = dangerfunct(danger_by_unit, move_rate, dist);
+    *danger += danger_by_unit;
+    if (igwall) *badmojo += danger_by_unit;
+  }
+}
+
 /********************************************************************** 
 ...
 ***********************************************************************/
 int assess_danger(struct city *pcity)
 {
-  int i, danger = 0, v, dist, m;
+  int i, danger = 0;
   Unit_Type_id utype;
   int danger_wall = 0; /* linear for walls */
   int danger_coastal = 0; /* linear for coastal */
@@ -236,15 +306,12 @@
   bool pikemen = FALSE;
   bool def_against_diplomat = FALSE; /* TRUE mean that this town can defend
                                      * against diplomats or spies */
-  bool igwall;
   int badmojo = 0;
   int boatspeed;
   int boatid, boatdist;
   int x = pcity->x, y = pcity->y; /* dummy variables */
   struct unit virtualunit;
-  struct unit *funit = &virtualunit; /* saves me a lot of typing. -- Syela */
 
-  memset(&virtualunit, 0, sizeof(struct unit));
   pplayer = city_owner(pcity);
 
   generate_warmap(pcity, NULL);        /* generates both land and sea maps */
@@ -271,94 +338,24 @@
         if (acity->is_building_unit &&
             build_points_left(acity) <= acity->shield_surplus &&
            ai_fuzzy(pplayer, TRUE)) {
-          virtualunit.owner = aplayer->player_no;
-          virtualunit.x = acity->x;
-          virtualunit.y = acity->y;
           utype = acity->currently_building;
-          virtualunit.type = utype;
-          virtualunit.veteran = do_make_unit_veteran(acity, utype);
-          virtualunit.hp = unit_types[utype].hp;
-/* yes, I know cloning all this code is bad form.  I don't really
-want to write a funct that takes nine ints by reference. -- Syela */
-          m = unit_type(funit)->move_rate;
-          v = assess_danger_unit(pcity, funit);
-          dist = assess_distance(pcity, funit, m, boatid, boatdist, boatspeed);
-          igwall = unit_really_ignores_citywalls(funit);
-          if ((is_ground_unit(funit) && v != 0) ||
-              (is_ground_units_transport(funit))) {
-            if (dist <= m * 3) pcity->ai.urgency++;
-            if (dist <= m) pcity->ai.grave_danger++;
-/* NOTE: This should actually implement grave_danger, which is supposed
-to be a feedback-sensitive formula for immediate danger.  I'm having
-second thoughts about the best implementation, and therefore this
-will have to wait until after 1.7.0.  I don't want to do anything I'm
-not totally sure about and can't thoroughly test in the hours before
-the release.  The AI is in fact vulnerable to gang-attacks, but I'm
-content to let it remain that way for now. -- Syela 980805 */
-          }
-
-          if (unit_flag(funit, F_HORSE)) {
-            if (pikemen) v /= 2;
-            else ai_wants_role_unit(pplayer, pcity, F_PIKEMEN,
-                                   (v * m / (dist*2)));
-          }
-
-        if (unit_flag(funit, F_DIPLOMAT) && (dist <= 2 * m)) 
-            pcity->ai.diplomat_threat = !def_against_diplomat;
-
-          v *= v;
-
-          if (!igwall) danger_wall += v * m / dist;
-          else if (is_sailing_unit(funit)) danger_coastal += v * m / dist;
-          else if (is_air_unit(funit) && !unit_flag(funit, F_NUCLEAR)) 
danger_sam += v * m / dist;
-          if (unit_flag(funit, F_MISSILE)) danger_sdi += v * m / MAX(m, dist);
-          if (!unit_flag(funit, F_NUCLEAR)) { /* only SDI helps against 
NUCLEAR */
-            v = dangerfunct(v, m, dist);
-            danger += v;
-            if (igwall) badmojo += v;
-          }
+         make_virtual_unit(&virtualunit, aplayer->player_no,
+                           acity->x,
+                           acity->y,
+                           utype,
+                           do_make_unit_veteran(acity, utype),
+                           unit_types[utype].hp);
+         assess_danger_unit_helper(pplayer, pcity, &virtualunit, boatid, 
boatdist, boatspeed, 
+                                   def_against_diplomat, pikemen, &badmojo,
+                                   &danger, &danger_wall, &danger_coastal, 
&danger_sam, &danger_sdi);
         }
       city_list_iterate_end;
 
       unit_list_iterate(aplayer->units, punit)
-        m = unit_type(punit)->move_rate;
-        v = assess_danger_unit(pcity, punit);
-        dist = assess_distance(pcity, punit, m, boatid, boatdist, boatspeed);
-       igwall = unit_really_ignores_citywalls(punit);
-          
-        if ((is_ground_unit(punit) && v != 0) ||
-            (is_ground_units_transport(punit))) {
-          if (dist <= m * 3) pcity->ai.urgency++;
-          if (dist <= m) pcity->ai.grave_danger++;
-/* NOTE: This should actually implement grave_danger, which is supposed
-to be a feedback-sensitive formula for immediate danger.  I'm having
-second thoughts about the best implementation, and therefore this
-will have to wait until after 1.7.0.  I don't want to do anything I'm
-not totally sure about and can't thoroughly test in the hours before
-the release.  The AI is in fact vulnerable to gang-attacks, but I'm
-content to let it remain that way for now. -- Syela 980805 */
-        }
-
-        if (unit_flag(punit, F_HORSE)) {
-          if (pikemen) v /= 2;
-         else ai_wants_role_unit(pplayer, pcity, F_PIKEMEN,
-                                 (v * m / (dist*2)));
-        }
-
-        if (unit_flag(punit, F_DIPLOMAT) && (dist <= 2 * m))
-            pcity->ai.diplomat_threat = !def_against_diplomat;
-
-        v *= v;
-
-        if (!igwall) danger_wall += v * m / dist;
-        else if (is_sailing_unit(punit)) danger_coastal += v * m / dist;
-        else if (is_air_unit(punit) && !unit_flag(punit, F_NUCLEAR)) 
danger_sam += v * m / dist;
-        if (unit_flag(punit, F_MISSILE)) danger_sdi += v * m / MAX(m, dist);
-        if (!unit_flag(punit, F_NUCLEAR)) { /* only SDI helps against NUCLEAR 
*/
-          v = dangerfunct(v, m, dist);
-          danger += v;
-          if (igwall) badmojo += v;
-        }
+       assess_danger_unit_helper(pplayer, pcity, punit, boatid, boatdist, 
boatspeed, 
+                                 def_against_diplomat, pikemen,
+                                 &badmojo,
+                                 &danger, &danger_wall, &danger_coastal, 
&danger_sam, &danger_sdi);
       unit_list_iterate_end;
     }
   } players_iterate_end;
@@ -1031,17 +1028,14 @@
   if (pcity->shield_surplus <= 0 || /* must be able to upkeep units */
       pcity->ppl_unhappy[4] > pcity->ppl_unhappy[2]) return; /* and no 
disorder! */
 
-  memset(&virtualunit, 0, sizeof(struct unit));
-/* this memset didn't work because of syntax that
-the intrepid David Pfitzner discovered was in error. -- Syela */
-  virtualunit.owner = pplayer->player_no;
-  virtualunit.x = pcity->x;
-  virtualunit.y = pcity->y;
-  virtualunit.id = 0;
   v = ai_choose_defender_by_type(pcity, LAND_MOVING);  /* Temporary -- Syela */
-  virtualunit.type = v;
-  virtualunit.veteran = do_make_unit_veteran(pcity, v);
-  virtualunit.hp = unit_types[v].hp;
+  make_virtual_unit(&virtualunit,
+                   pplayer->player_no,
+                   pcity->x,
+                   pcity->y,
+                   v,
+                   do_make_unit_veteran(pcity, v),
+                   unit_types[v].hp); 
 
   if (choice->want < 100) {
     want = look_for_charge(pplayer, &virtualunit, &aunit, &acity);

-- 
//Markus



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