Complete.Org: Mailing Lists: Archives: freeciv-ai: October 2002:
[freeciv-ai] Coordinate naval attacks with enemy attacked cities
Home

[freeciv-ai] Coordinate naval attacks with enemy attacked cities

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv ai <freeciv-ai@xxxxxxxxxxx>
Subject: [freeciv-ai] Coordinate naval attacks with enemy attacked cities
From: Jordi Negrevernis i Font <jorneg@xxxxxxxxxxx>
Date: Sun, 27 Oct 2002 14:39:36 +0100


This patch makes the naval attack units look for an invaded coastal city. And if it find it go there to help allied invasion forces.

I think it follows the style code. It's againt cvs Sep 25 + massiveai2.diff.

I don't use unit_do_goto - or something like this - after finding the cities - find_coastal_enemy_city_attacked -. Surprissintly goes there to attack!!!!

--- aiunit.c.massiveai2 Sat Oct 26 11:50:38 2002
+++ aiunit.c    Sun Oct 27 02:36:41 2002
@@ -1988,6 +1988,50 @@
 }
 
 /*************************************************************************
+  looks for an enemy city attacked by allied ground units
+**************************************************************************/
+static void find_coastal_enemy_city_attacked(struct player *pplayer, struct 
unit *punit, int *x, int *y)
+{
+  int best = 0, dest_x = punit->x, dest_y = punit->y;
+  int current_score;
+  struct city *pcity;
+
+  generate_warmap(map_get_city(punit->x, punit->y), punit);
+
+  /* We're iterating to find the nearest enemy cities. */
+  square_iterate(punit->x, punit->y, 20, x1, y1) {
+    /* The actual map tile */
+    struct tile *ptile = map_get_tile(x1, y1);
+    pcity = is_enemy_city_tile(ptile, pplayer);
+    if ( !pcity ) continue;
+
+    /* we find an enemy city near us, is there any allied unit around them? */
+    current_score = 0;
+    adjc_iterate(x1, y1, ax, ay) {
+      unit_list_iterate(map_get_tile(ax, ay)->units, aunit) {
+        if ( (aunit->owner == pplayer->player_no) || ( 
pplayers_allied(pplayer, unit_owner(aunit)) ) ) {
+            /* that unit is of our own so help it */
+            /* distance * defense */
+            current_score = warmap.cost[x1][y1] * 
assess_defense_quadratic(pcity);
+            goto OK;
+        }
+      } unit_list_iterate_end;
+    } adjc_iterate_end;
+
+    OK:
+    /* we check that the city is accesible from ocean and do the score */
+    if ( (current_score) && ((current_score < best) || (best == 0)) &&
+         is_water_adjacent_to_tile(x1, y1) ) {
+      best = current_score;
+      dest_x = x1; dest_y = y1;
+    }
+  } square_iterate_end;
+
+  /* set results */
+  *x = dest_x; *y = dest_y;
+}
+
+/*************************************************************************
   This does the attack until we have used up all our movement, unless we
   should safeguard a city. First we rampage on adjacent tiles, then we go
   looking for trouble elsewhere. If there is nothing to kill, sailing units 
@@ -2005,6 +2049,26 @@
     return;
   }
 
+  /* first, if we are an attacking sailing unit, find nearby coastal city *
+   * sorrounded by ground units */
+  if ( is_sailing_unit(punit) ) {
+    find_coastal_enemy_city_attacked(pplayer, punit, &dest_x, &dest_y);
+    if (!same_pos(punit->x, punit->y, dest_x, dest_y)) {
+      /* if we find something, go there & attack */
+      freelog(LOG_NORMAL, "%s's %s(%d) at (%d, %d) goes to (%d,%d) to 
coordinate attack with ground units.",
+              pplayer->name, unit_type(punit)->name, punit->id, punit->x, 
punit->y,
+              punit->goto_dest_x, punit->goto_dest_y);
+      if (!(punit = ai_military_rampage(punit, 2))) {
+        return; /* oops, we died */
+      }
+    }
+    else {
+      freelog(LOG_NORMAL, "%s's %s(%d) at (%d, %d) did not find city to 
coordinate attack with ground units.",
+              pplayer->name, unit_type(punit)->name, punit->id, punit->x, 
punit->y);
+
+    }
+  }
+
   /* Main attack loop */
   do {
     /* First find easy adjacent enemies; 2 is better than pillage */

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