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

 This caches unit class move restrictions for AI usage after rulesets
are loaded. No actual users yet.


 - ML

diff -Nurd -X.diff_ignore freeciv/common/unittype.c freeciv/common/unittype.c
--- freeciv/common/unittype.c	2007-01-26 22:51:04.000000000 +0200
+++ freeciv/common/unittype.c	2007-01-29 02:05:34.000000000 +0200
@@ -751,3 +751,56 @@
     unit_classes[i].id = i;
   }
 }
+
+/****************************************************************************
+  Build cached values for AI
+****************************************************************************/
+void unit_class_ai_init(void)
+{
+  bv_special special;
+  BV_CLR_ALL(special); /* Can it move even without road */
+
+  unit_class_iterate(pclass) {
+    bool move_land_enabled  = FALSE; /* Can move at some land terrains */
+    bool move_land_disabled = FALSE; /* Cannot move at some land terrains */
+    bool move_sea_enabled   = FALSE; /* Can move at some ocean terrains */
+    bool move_sea_disabled  = FALSE; /* Cannot move at some ocean terrains */
+
+    terrain_type_iterate(pterrain) {
+      if (is_native_to_class(pclass, pterrain, special)) {
+        /* Can move at terrain */
+        if (is_ocean(pterrain)) {
+          move_sea_enabled = TRUE;
+        } else {
+          move_land_enabled = TRUE;
+        }
+      } else {
+        /* Cannot move at terrain */
+        if (is_ocean(pterrain)) {
+          move_sea_disabled = TRUE;
+        } else {
+          move_land_disabled = TRUE;
+        }
+      }
+    } terrain_type_iterate_end;
+
+    if (move_land_enabled && !move_land_disabled) {
+      pclass->ai.land_move = MOVE_FULL;
+    } else if (move_land_enabled && move_land_disabled) {
+      pclass->ai.land_move = MOVE_PARTIAL;
+    } else {
+      assert(!move_land_enabled);
+      pclass->ai.land_move = MOVE_NONE;
+    }
+
+    if (move_sea_enabled && !move_sea_disabled) {
+      pclass->ai.sea_move = MOVE_FULL;
+    } else if (move_sea_enabled && move_sea_disabled) {
+      pclass->ai.sea_move = MOVE_PARTIAL;
+    } else {
+      assert(!move_sea_enabled);
+      pclass->ai.sea_move = MOVE_NONE;
+    }
+
+  } unit_class_iterate_end;
+}
diff -Nurd -X.diff_ignore freeciv/common/unittype.h freeciv/common/unittype.h
--- freeciv/common/unittype.h	2007-01-26 22:51:04.000000000 +0200
+++ freeciv/common/unittype.h	2007-01-29 02:01:33.000000000 +0200
@@ -41,6 +41,8 @@
 BV_DEFINE(bv_unit_classes, UCL_LAST);
 BV_DEFINE(bv_unit_class_flags, UCF_LAST);
 
+enum move_level { MOVE_NONE, MOVE_PARTIAL, MOVE_FULL };
+
 struct unit_class {
   Unit_Class_id id;
   const char *name;        /* Translated name */
@@ -49,6 +51,11 @@
   int min_speed;           /* Minimum speed after damage and effects */
   int hp_loss_pct;         /* Percentage of hitpoints lost each turn not in city or airbase */
   bv_unit_class_flags flags;
+
+  struct {
+    enum move_level land_move;
+    enum move_level sea_move;
+  } ai;
 };
 
 /* Unit "special effects" flags:
@@ -277,6 +284,9 @@
 
 void unit_classes_init(void);
 
+/* Call this after rulesets are loaded */
+void unit_class_ai_init(void);
+
 #define unit_type_iterate(punittype)					    \
 {									    \
   int _index;								    \
diff -Nurd -X.diff_ignore freeciv/server/ruleset.c freeciv/server/ruleset.c
--- freeciv/server/ruleset.c	2007-01-26 16:37:20.000000000 +0200
+++ freeciv/server/ruleset.c	2007-01-29 02:04:26.000000000 +0200
@@ -3299,6 +3299,9 @@
      * connected clients. */
     send_rulesets(game.all_connections);
   }
+
+  /* Build AI unit class cache corresponding to loaded rulesets */
+  unit_class_ai_init();
 }
 
 /**************************************************************************
_______________________________________________
Freeciv-dev mailing list
Freeciv-dev@gna.org
https://mail.gna.org/listinfo/freeciv-dev

Reply via email to