patch to make the AI be more defensive

Jordi Negrevernis i Font <[email protected]>
Newsgroups gmane.games.freeciv.ai
Message-ID <[email protected]>
    This patch makes 2 thinks:

    - Considers defensive units those with flag L_DEFEND_GOOD amb 
L_DEFEND_OK. I don't know why the L-DEFEND_OK units are not considered 
for when choosing a bodyguard.

    - Before attacking, after rampage, the unit checks to see if it can 
find a weak city or unit to protect.

    I run an autogame and did not see empty cities near enemy 
territory... maybe is some luck!
ai_protect.diff (text/plain, 1.5 KB)
diff -u -r -b -Xfreeciv-cvs-Aug-16/diff_ignore freeciv-cvs-Aug-16/ai/aiunit.c freeciv-cvs-Aug-16-protect/ai/aiunit.c
--- freeciv-cvs-Aug-16/ai/aiunit.c	2004-08-29 17:11:39.000000000 +0200
+++ freeciv-cvs-Aug-16-protect/ai/aiunit.c	2004-09-13 13:20:33.000000000 +0200
@@ -889,7 +889,7 @@
   if (unit_types[type].move_type != LAND_MOVING) {
     return FALSE; /* temporary kluge */
   }
-  return (unit_has_role(type, L_DEFEND_GOOD));
+  return (unit_has_role(type, L_DEFEND_GOOD) || unit_has_role(type, L_DEFEND_OK));
 }
 
 /*************************************************************************
@@ -1735,6 +1735,9 @@
   int id = punit->id;
   int ct = 10;
   struct city *pcity = NULL;
+  struct city *acity = NULL;
+  struct unit *aunit = NULL;
+  int val = 0;
 
   CHECK_UNIT(punit);
 
@@ -1753,6 +1756,22 @@
     return;
   }
 
+  /* now, if we are a good defender, we try to find a some gays to escort */
+  if (unit_role_defender(punit->type)) {
+    val = look_for_charge(pplayer, punit, &aunit, &acity);
+    if (val > 0) {
+      if (acity) {
+        ai_unit_new_role(punit, AIUNIT_ESCORT, acity->x, acity->y);
+        punit->ai.charge = acity->id;
+        BODYGUARD_LOG(LOG_DEBUG, punit, "going to defend city (was going to attack)");
+      } else if (aunit) {
+        ai_unit_new_role(punit, AIUNIT_ESCORT, aunit->x, aunit->y);
+        punit->ai.charge = aunit->id;
+        BODYGUARD_LOG(LOG_DEBUG, punit, "going to defend unit (was going to attack)");
+      }
+    }
+  }
+
   /* Main attack loop */
   do {
     if (stay_and_defend(punit)) {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.