Complete.Org: Mailing Lists: Archives: freeciv-dev: May 2005:
[Freeciv-Dev] (PR#13137) ai "stats" aren't initialized
Home

[Freeciv-Dev] (PR#13137) ai "stats" aren't initialized

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: jdorje@xxxxxxxxx
Subject: [Freeciv-Dev] (PR#13137) ai "stats" aren't initialized
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 18 May 2005 10:14:48 -0700
Reply-to: bugs@xxxxxxxxxxx

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

This patch fixes it.

I have no idea if the other fields in "stats" are initialized however.

-jason

Index: ai/aicity.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aicity.c,v
retrieving revision 1.224
diff -u -r1.224 aicity.c
--- ai/aicity.c 10 May 2005 19:08:54 -0000      1.224
+++ ai/aicity.c 18 May 2005 17:13:38 -0000
@@ -397,7 +397,7 @@
        case EFT_AIRLIFT:
          /* FIXME: We need some smart algorithm here. The below is 
           * totally braindead. */
-         v += c + MIN(ai->stats.units[UCL_LAND], 13);
+         v += c + MIN(ai->stats.units.land, 13);
          break;
        case EFT_ANY_GOVERNMENT:
          if (!can_change_to_government(pplayer, ai->goal.govt.idx)) {
@@ -407,7 +407,7 @@
          break;
        case EFT_ENABLE_NUKE:
          /* Treat nuke as a Cruise Missile upgrade */
-         v += 20 + ai->stats.units[UCL_MISSILE] * 5;
+         v += 20 + ai->stats.units.missiles * 5;
          break;
        case EFT_ENABLE_SPACE:
          if (game.info.spacerace) {
@@ -429,7 +429,7 @@
        case EFT_NO_ANARCHY:
          break;  /* Useless for AI */
        case EFT_NO_SINK_DEEP:
-         v += 15 + ai->stats.triremes * 5;
+         v += 15 + ai->stats.units.triremes * 5;
          break;
        case EFT_NUKE_PROOF:
          if (ai->threats.nuclear) {
@@ -476,34 +476,34 @@
          /* Uhm, problem: City Wall has -50% here!! */
          break;
        case EFT_SEA_MOVE:
-         v += ai->stats.units[UCL_SEA] * 8 * amount;
+         v += ai->stats.units.sea * 8 * amount;
          break;
        case EFT_UNIT_NO_LOSE_POP:
          v += unit_list_size(ptile->units) * 2;
          break;
        case EFT_LAND_REGEN:
-         v += 5 * c + ai->stats.units[UCL_LAND] * 3;
+         v += 5 * c + ai->stats.units.land * 3;
          break;
        case EFT_SEA_REGEN:
-         v += 5 * c + ai->stats.units[UCL_SEA] * 3;
+         v += 5 * c + ai->stats.units.sea * 3;
          break;
        case EFT_AIR_REGEN:
-         v += 5 * c + ai->stats.units[UCL_AIR] * 3;
+         v += 5 * c + ai->stats.units.air * 3;
          break;
        case EFT_LAND_VET_COMBAT:
-         v += 2 * c + ai->stats.units[UCL_LAND] * 2;
+         v += 2 * c + ai->stats.units.land * 2;
          break;
        case EFT_LAND_VETERAN:
-         v += 3 * c + ai->stats.units[UCL_LAND];
+         v += 3 * c + ai->stats.units.land;
          break;
        case EFT_SEA_VETERAN:
-         v += 5 * c + ai->stats.units[UCL_SEA];
+         v += 5 * c + ai->stats.units.sea;
          break;
        case EFT_AIR_VETERAN:
-         v += 5 * c + ai->stats.units[UCL_AIR];
+         v += 5 * c + ai->stats.units.air;
          break;
        case EFT_UPGRADE_UNIT:
-         v += ai->stats.units[UCL_LAST];
+         v += ai->stats.units.upgradeable;
          if (amount == 1) {
            v *= 2;
          } else if (amount == 2) {
Index: ai/aidata.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aidata.c,v
retrieving revision 1.68
diff -u -r1.68 aidata.c
--- ai/aidata.c 7 May 2005 18:58:02 -0000       1.68
+++ ai/aidata.c 18 May 2005 17:13:38 -0000
@@ -370,6 +370,41 @@
 }
 
 /**************************************************************************
+  This function is called each turn to initialize pplayer->ai.stats.units.
+**************************************************************************/
+static void count_my_units(struct player *pplayer)
+{
+  struct ai_data *ai = ai_data_get(pplayer);
+
+  memset(&ai->stats.units, 0, sizeof(ai->stats.units));
+
+  unit_list_iterate(pplayer->units, punit) {
+    switch (unit_type(punit)->move_type) {
+    case LAND_MOVING:
+      ai->stats.units.land++;
+      break;
+    case SEA_MOVING:
+      ai->stats.units.sea++;
+      break;
+    case HELI_MOVING:
+    case AIR_MOVING:
+      ai->stats.units.air++;
+      break;
+    }
+
+    if (unit_flag(punit, F_TRIREME)) {
+      ai->stats.units.triremes++;
+    }
+    if (unit_flag(punit, F_MISSILE)) {
+      ai->stats.units.missiles++;
+    }
+    if (can_upgrade_unittype(pplayer, punit->type) >= 0) {
+      ai->stats.units.upgradeable++;
+    }
+  } unit_list_iterate_end;
+}
+
+/**************************************************************************
   Make and cache lots of calculations needed for other functions.
 
   Note: We use map.num_continents here rather than pplayer->num_continents
@@ -651,6 +686,8 @@
     ai->wants_no_science = FALSE;
   }
 
+  count_my_units(pplayer);
+
   TIMING_LOG(AIT_AIDATA, TIMER_STOP);
 
   /* Government */
Index: ai/aidata.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aidata.h,v
retrieving revision 1.28
diff -u -r1.28 aidata.h
--- ai/aidata.h 5 May 2005 20:00:41 -0000       1.28
+++ ai/aidata.h 18 May 2005 17:13:38 -0000
@@ -120,8 +120,17 @@
   /* This struct is used for statistical unit building, eg to ensure
    * that we don't build too few or too many units of a given type. */
   struct {
-    int triremes;
-    int units[UCL_LAST + 1]; /* no. units by class */
+    /* Counts of specific types of units. */
+    struct {
+      /* Unit-flag counts. */
+      int triremes, missiles;
+
+      /* Move-type counts (air includes helicoptor here). */
+      int land, sea, air;
+
+      /* Upgradeable units */
+      int upgradeable;
+    } units;
     int *workers;     /* cities to workers on continent*/
     int *cities;      /* number of cities we have on continent */
     int passengers;   /* number of passengers waiting for boats */

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