Author: cazfi
Date: Sun Nov 22 14:16:12 2015
New Revision: 30737

URL: http://svn.gna.org/viewcvs/freeciv?rev=30737&view=rev
Log:
Limit number of units assigned to city defense duty.
As the wanted minimum defense power was solely function of potential
attack against the city, cities were often just building more and more 
defenders,
as units one player build as defenders were considered potential attackers
by the other side.

See patch #6612

Modified:
    branches/S2_6/ai/default/aiunit.c

Modified: branches/S2_6/ai/default/aiunit.c
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/ai/default/aiunit.c?rev=30737&r1=30736&r2=30737&view=diff
==============================================================================
--- branches/S2_6/ai/default/aiunit.c   (original)
+++ branches/S2_6/ai/default/aiunit.c   Sun Nov 22 14:16:12 2015
@@ -2624,6 +2624,7 @@
     int martless_unhappy = pcity->feel[CITIZEN_UNHAPPY][FEELING_NATIONALITY]
       + pcity->feel[CITIZEN_ANGRY][FEELING_NATIONALITY];
     int entertainers = 0;
+    bool enough = FALSE;
 
     specialist_type_iterate(sp) {
       if (get_specialist_output(pcity, sp, O_LUXURY) > 0) {
@@ -2634,9 +2635,10 @@
     martless_unhappy += entertainers; /* We want to use martial law instead
                                        * of entertainers. */
 
-    while (total_defense <= total_attack
-           || (count < mart_max && mart_each > 0
-               && martless_unhappy > mart_each * count)) {
+    while (!enough
+           && (total_defense <= total_attack
+               || (count < mart_max && mart_each > 0
+                   && martless_unhappy > mart_each * count))) {
       int best_want = 0;
       struct unit *best = NULL;
       bool defense_needed = total_defense <= total_attack; /* Defense or 
martial */
@@ -2655,6 +2657,7 @@
           }
         }
       } unit_list_iterate_end;
+      
       if (best == NULL) {
         if (defense_needed) {
           /* Ooops - try to grab any unit as defender! */
@@ -2667,12 +2670,27 @@
           break;
         }
       } else {
-        int loglevel = pcity->server.debug ? LOG_AI_TEST : LOG_DEBUG;
-
-        total_defense += best_want;
-        UNIT_LOG(loglevel, best, "Defending city");
-        dai_unit_new_task(ait, best, AIUNIT_DEFEND_HOME, pcity->tile);
-        count++;
+        struct unit_type *btype = unit_type_get(best);
+
+        if ((martless_unhappy < mart_each * count
+             || count >= mart_max || mart_each <= 0)
+            && ((count >= 2
+                 && btype->attack_strength > btype->defense_strength)
+                || (count >= 4
+                    && btype->attack_strength == btype->defense_strength))) {
+          /* In this case attack would be better defense than fortifying
+           * to city. */
+          log_normal("%s not assigning more defenders after %d",
+                     city_name(pcity), count);
+          enough = TRUE;
+        } else {
+          int loglevel = pcity->server.debug ? LOG_AI_TEST : LOG_DEBUG;
+
+          total_defense += best_want;
+          UNIT_LOG(loglevel, best, "Defending city");
+          dai_unit_new_task(ait, best, AIUNIT_DEFEND_HOME, pcity->tile);
+          count++;
+        }
       }
     }
     CITY_LOG(LOG_DEBUG, pcity, "Evaluating defense: %d defense, %d incoming"


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to