Author: cazfi
Date: Sun Nov 22 14:16:18 2015
New Revision: 30738

URL: http://svn.gna.org/viewcvs/freeciv?rev=30738&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_5/ai/default/aiunit.c

Modified: branches/S2_5/ai/default/aiunit.c
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_5/ai/default/aiunit.c?rev=30738&r1=30737&r2=30738&view=diff
==============================================================================
--- branches/S2_5/ai/default/aiunit.c   (original)
+++ branches/S2_5/ai/default/aiunit.c   Sun Nov 22 14:16:18 2015
@@ -2578,6 +2578,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) {
@@ -2588,9 +2589,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 */
@@ -2609,6 +2611,7 @@
           }
         }
       } unit_list_iterate_end;
+      
       if (best == NULL) {
         if (defense_needed) {
           /* Ooops - try to grab any unit as defender! */
@@ -2621,12 +2624,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(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