Complete.Org: Mailing Lists: Archives: freeciv-dev: May 1999:
Re: [Freeciv-Dev] PATCH: New ai_assess_military_unhappiness(): remov.cod
Home

Re: [Freeciv-Dev] PATCH: New ai_assess_military_unhappiness(): remov.cod

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: rizos@xxxxxxxx, freeciv-dev@xxxxxxxxxxx
Subject: Re: [Freeciv-Dev] PATCH: New ai_assess_military_unhappiness(): remov.code dupl.,supports Democracy.
From: David Pfitzner <dwp@xxxxxxxxxxxxxx>
Date: Sat, 29 May 1999 14:02:45 +1000 (EST)

David Pfitzner wrote:

> rizos@xxxxxxxx wrote:
> 
> > The following patch creates a new function to evaluate 
> > military unhappiness for ai purposes. It removes code
> > duplication and adds support for Democracy (to be used
> > when patch for ai_manage_government is included).
> 
> Attached is basically the same patch, with 
> ai_assess_military_unhappiness() re-worked with 
> more standard indentation, and IMO clearer code.
> (And also don't possibly re-calculate 
> city_got_effect(pcity, B_POLICE) and 
> improvement_variant(B_WOMENS) possibly many times.)

Oops, Rizos spotted a bug in my version. 
Here is a hopefully fixed version.

> Regards,
> -- David
diff -u -r --exclude-from exclude freeciv-cvs/ai/advmilitary.c 
fc-adv/ai/advmilitary.c
--- freeciv-cvs/ai/advmilitary.c        Sun Apr 25 12:29:18 1999
+++ fc-adv/ai/advmilitary.c     Sat May 29 14:03:11 1999
@@ -721,19 +721,9 @@
       vet = pdef->veteran;
     } /* end dealing with units */
 
-    if (get_government(pplayer->player_no) == G_REPUBLIC &&
-        (pcity->id == myunit->homecity || !myunit->id)
-       && !(improvement_variant(B_WOMENS)==1
-            && city_got_effect(pcity, B_POLICE))) {
-      unit_list_iterate(pcity->units_supported, punit)
-        if (unit_being_aggressive(punit)) {
-          unhap++;
-        } else if (is_field_unit(punit)) {
-          unhap++;
-        }
-      unit_list_iterate_end;
-      if (city_got_effect(pcity, B_POLICE)) unhap--;
-    } /* handle other governments later */
+    if (pcity->id == myunit->homecity || !myunit->id)
+        unhap = ai_assess_military_unhappiness(pcity, 
+                                   get_government(pplayer->player_no));
 
     if (is_ground_unit(myunit) && !sanity && !boatid)
       needferry = 40; /* cost of ferry */
diff -u -r --exclude-from exclude freeciv-cvs/ai/aitools.c fc-adv/ai/aitools.c
--- freeciv-cvs/ai/aitools.c    Sun Apr 25 12:29:18 1999
+++ fc-adv/ai/aitools.c Sat May 29 14:05:02 1999
@@ -26,6 +26,7 @@
 #include <mapgen.h>
 #include <unittools.h>
 #include <cityhand.h>
+#include <cityturn.h>
 #include <citytools.h>
 #include <plrhand.h>
 
@@ -154,4 +155,57 @@
   choice->want = want;
   choice->choice = id;
   choice->type = 0;
+}
+
+
+/**********************************************************************
+The following evaluates the unhappiness caused by military units
+in the field (or aggressive) at a city when at Republic or Democracy
+**********************************************************************/
+int ai_assess_military_unhappiness(struct city *pcity, int gov)
+{
+  int unhap=0;
+  int have_police;
+  int variant;
+  
+  if (gov < G_REPUBLIC)
+    return 0;
+  
+  have_police = city_got_effect(pcity, B_POLICE);
+  variant = improvement_variant(B_WOMENS);
+  
+  if (gov == G_REPUBLIC) {
+    if (have_police && variant==1 )
+      return 0;
+    
+    unit_list_iterate(pcity->units_supported, punit) {
+      if (unit_being_aggressive(punit) || is_field_unit(punit)) {
+       unhap++;
+      }
+    }
+    unit_list_iterate_end;
+    if (have_police) unhap--;
+  } 
+  else if (gov == G_DEMOCRACY) {
+    unit_list_iterate(pcity->units_supported, punit) {
+      if (have_police && variant==1) {
+       if (unit_being_aggressive(punit)) {
+         unhap++;
+       }
+      } else {
+       if (unit_being_aggressive(punit)) {
+         unhap += 2;
+       } else if (is_field_unit(punit)) {
+         unhap += 1;
+       }
+      }
+    }
+    unit_list_iterate_end;
+    if (have_police && variant==0) {
+      unhap -= 2;
+    }
+  }
+  if (unhap < 0) unhap = 0;
+  
+  return unhap;
 }
diff -u -r --exclude-from exclude freeciv-cvs/ai/aitools.h fc-adv/ai/aitools.h
--- freeciv-cvs/ai/aitools.h    Fri Aug 14 01:40:39 1998
+++ fc-adv/ai/aitools.h Sat May 29 14:03:11 1999
@@ -34,7 +34,7 @@
 void adjust_choice(int type, struct ai_choice *choice);
 void copy_if_better_choice(struct ai_choice *cur, struct ai_choice *best);
 void ai_advisor_choose_building(struct city *pcity, struct ai_choice *choice);
-
+int ai_assess_military_unhappiness(struct city *pcity, int gov);
 
 
 #endif
diff -u -r --exclude-from exclude freeciv-cvs/ai/aiunit.c fc-adv/ai/aiunit.c
--- freeciv-cvs/ai/aiunit.c     Sun May  9 21:15:35 1999
+++ fc-adv/ai/aiunit.c  Sat May 29 14:03:11 1999
@@ -966,19 +966,9 @@
         
   pcity = map_get_city(punit->x, punit->y);
 
-  if (get_government(pplayer->player_no) == G_REPUBLIC &&
-      pcity && (!punit->id || pcity->id == punit->homecity)
-      && !(improvement_variant(B_WOMENS)==1
-          && city_got_effect(pcity, B_POLICE))) {
-    unit_list_iterate(pcity->units_supported, punit)
-      if (unit_being_aggressive(punit)) {
-        unhap++;
-      } else if (is_field_unit(punit)) {
-        unhap++;   
-      }
-    unit_list_iterate_end;
-    if (city_got_effect(pcity, B_POLICE)) unhap--;   
-  } /* handle other governments later */
+  if (pcity && (!punit->id || pcity->id == punit->homecity))
+      unhap = ai_assess_military_unhappiness(pcity, 
+                        get_government(pplayer->player_no));
 
   *x = punit->x; *y = punit->y;
   ab = unit_belligerence_basic(punit);

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