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

[Freeciv-Dev] PATCH: New ai_assess_military_unhappiness(): remov.code du

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] PATCH: New ai_assess_military_unhappiness(): remov.code dupl.,supports Democracy.
From: rizos@xxxxxxxx
Date: Fri, 28 May 1999 04:32:44 -0500 (CDT)

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).

--rizos

diff against the CVS of May 27. Files affected:
        ai/advmilitary.c
        ai/aiunit.c
        ai/aitools.c
        ai/aitools.h



--- advmilitary.c.orig  Thu May 27 12:07:04 1999
+++ advmilitary.c       Fri May 28 04:06:07 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 */
--- aiunit.c.orig       Thu May 27 12:04:34 1999
+++ aiunit.c    Fri May 28 04:06:43 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);
--- aitools.c.orig      Thu May 27 11:59:56 1999
+++ aitools.c   Fri May 28 03:48:05 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,52 @@
   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;
+
+   if (gov < G_REPUBLIC)
+       return(0);
+
+   if (gov == G_REPUBLIC 
+       && !(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--;
+    } 
+
+
+    if (gov == G_DEMOCRACY) {
+        unit_list_iterate(pcity->units_supported, punit) 
+          if (unit_being_aggressive(punit)) {
+              unhap+=2;
+              if (improvement_variant(B_WOMENS)==1
+                  && city_got_effect(pcity, B_POLICE)) 
+                     unhap--;
+          } else if (is_field_unit(punit)
+                    && !(improvement_variant(B_WOMENS)==1
+                    && city_got_effect(pcity, B_POLICE))) 
+                       unhap++;
+        unit_list_iterate_end;
+        if (improvement_variant(B_WOMENS)!=1
+            && city_got_effect(pcity, B_POLICE)) 
+               unhap-=2;
+    }
+
+    if (unhap < 0) unhap = 0;
+
+    return unhap;
 }
--- aitools.h.orig      Thu May 27 12:16:30 1999
+++ aitools.h   Thu May 27 12:17:08 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

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