Complete.Org: Mailing Lists: Archives: freeciv-ai: August 2004:
[freeciv-ai] Inprovements in settler.c
Home

[freeciv-ai] Inprovements in settler.c

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv-ai@xxxxxxxxxxx
Subject: [freeciv-ai] Inprovements in settler.c
From: Jordi Negrevernis i Font <jorneg@xxxxxxxxxxx>
Date: Thu, 12 Aug 2004 19:07:01 +0200


Hi, i'm trying to improve the settler code by using the bodyguards with settlers, etc...

   This the first attempt, and can be greatly improved.

   While looking at the code i see that you use code like this:

 if (punit->ai.charge != BODYGUARD_NONE) { /* I am a bodyguard */

Why you don't check to see if its equal to BODYGUARD_WANTED, or just check for a value > 0.

diff -u -r -Xfreeciv-cvs-Aug-06/diff_ignore freeciv-cvs-Aug-06/ai/aitools.c 
freeciv-cvs-Aug-06-settler/ai/aitools.c
--- freeciv-cvs-Aug-06/ai/aitools.c     2004-08-06 07:14:59.000000000 +0200
+++ freeciv-cvs-Aug-06-settler/ai/aitools.c     2004-08-11 01:39:59.000000000 
+0200
@@ -422,7 +422,7 @@
   bodyguard has not. This is an ai_unit_* auxiliary function, do not use 
   elsewhere.
 **************************************************************************/
-static void ai_unit_bodyguard_move(int unitid, int x, int y)
+void ai_unit_bodyguard_move(int unitid, int x, int y)
 {
   struct unit *bodyguard = find_unit_by_id(unitid);
   struct unit *punit;
@@ -456,7 +456,7 @@
   Repair incompletely referenced bodyguards. When the rest of the bodyguard
   mess is cleaned up, this repairing should be replaced with an assert.
 **************************************************************************/
-static bool has_bodyguard(struct unit *punit)
+bool has_bodyguard(struct unit *punit)
 {
   struct unit *guard;
   if (punit->ai.bodyguard > BODYGUARD_NONE) {
diff -u -r -Xfreeciv-cvs-Aug-06/diff_ignore freeciv-cvs-Aug-06/ai/aitools.h 
freeciv-cvs-Aug-06-settler/ai/aitools.h
--- freeciv-cvs-Aug-06/ai/aitools.h     2004-08-06 07:14:59.000000000 +0200
+++ freeciv-cvs-Aug-06-settler/ai/aitools.h     2004-08-11 01:39:49.000000000 
+0200
@@ -52,6 +52,7 @@
 bool ai_unit_make_homecity(struct unit *punit, struct city *pcity);
 bool ai_unit_attack(struct unit *punit, int x, int y);
 bool ai_unit_move(struct unit *punit, int x, int y);
+void ai_unit_bodyguard_move(int unitid, int x, int y);
 
 struct city *dist_nearest_city(struct player *pplayer, int x, int y,
                                bool everywhere, bool enemy);
@@ -70,4 +71,7 @@
 
 bool is_player_dangerous(struct player *pplayer, struct player *aplayer);
 
+bool has_bodyguard(struct unit *punit);
+
+
 #endif  /* FC__AITOOLS_H */
Sólo en freeciv-cvs-Aug-06-settler/server: .kdbgrc.civserver
diff -u -r -Xfreeciv-cvs-Aug-06/diff_ignore 
freeciv-cvs-Aug-06/server/settlers.c 
freeciv-cvs-Aug-06-settler/server/settlers.c
--- freeciv-cvs-Aug-06/server/settlers.c        2004-08-07 07:15:21.000000000 
+0200
+++ freeciv-cvs-Aug-06-settler/server/settlers.c        2004-08-12 
17:03:02.000000000 +0200
@@ -1,4 +1,4 @@
-/********************************************************************** 
+/**********************************************************************
  Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -63,6 +63,8 @@
 {
   int x = punit->x, y = punit->y;
   struct city *pcity;
+  int bodyguard = punit->ai.bodyguard;
+  struct unit *pbody = find_unit_by_id(bodyguard);
 
   handle_unit_activity_request(punit, ACTIVITY_IDLE);
 
@@ -78,6 +80,16 @@
     return FALSE;
   }
 
+  /* If we carried a bodyguard just use it!.
+   *  Its the new defender of the city */
+  if (bodyguard > 0) {
+    if (pbody) {
+      if (ai_unit_make_homecity(pbody, pcity)) {
+       ai_unit_new_role(pbody, AIUNIT_ESCORT, -1, -1);
+      }
+    }
+  }
+
   return TRUE;
 }
 
@@ -137,20 +149,42 @@
   Manages settlers.
 **************************************************************************/
 void ai_manage_settler(struct player *pplayer, struct unit *punit)
-{
+{ bool pos_dangerous = 0;
+
   punit->ai.control = TRUE;
   /* if BUILD_CITY must remain BUILD_CITY, otherwise turn into autosettler */
   if (punit->ai.ai_role == AIUNIT_NONE) {
     ai_unit_new_role(punit, AIUNIT_AUTO_SETTLER, -1, -1);
   }
+
+  /* we check for a bodyguard if our settler unit is in danger */
+
+  /* if goto is set we check for a dangerous position */
+  if (is_goto_dest_set(punit)) {
+    pos_dangerous = enemies_at(punit, goto_dest_x(punit), goto_dest_y(punit));
+  } else {
+    /* otherwise we check for the current location */
+    pos_dangerous = enemies_at(punit, punit->x, punit->y);
+  }
+
+  if (pos_dangerous > 0) {
+    if (!has_bodyguard(punit)) {
+      punit->ai.bodyguard = BODYGUARD_WANTED;
+    }
+  }
+
+  if (has_bodyguard(punit)) {
+    ai_unit_bodyguard_move(punit->ai.bodyguard, punit->x, punit->y);
+  }
+
   return;
 }
 
 /**************************************************************************
- return 1 if there is already a unit on this square or one destined for it 
+ return 1 if there is already a unit on this square or one destined for it
  (via goto)
 **************************************************************************/
-static bool is_already_assigned(struct unit *myunit, struct player *pplayer, 
+static bool is_already_assigned(struct unit *myunit, struct player *pplayer,
     int x, int y)
 {
   if (same_pos(myunit->x, myunit->y, x, y)
@@ -1080,23 +1114,28 @@
       ai_unit_new_role(punit, AIUNIT_NONE, -1, -1);
       return; /* avoid recursion at all cost */
     } else {
-      /* Go there */
-      if ((!ai_gothere(pplayer, punit, x, y) && !find_unit_by_id(sanity))
-          || punit->moves_left <= 0) {
+      if (enemies_at(punit, x, y)) {
+        UNIT_LOG(LOG_SETTLER, punit, "enemies at city founding location!");
         return;
-      }
-      if (same_pos(punit->x, punit->y, x, y)) {
-        if (!ai_do_build_city(pplayer, punit)) {
-          UNIT_LOG(LOG_ERROR, punit, "could not make city on %s",
-                   map_get_tile_info_text(punit->x, punit->y));
-          ai_unit_new_role(punit, AIUNIT_NONE, -1, -1);
+      } else {
+        /* Go there */
+        if ((!ai_gothere(pplayer, punit, x, y) && !find_unit_by_id(sanity))
+            || punit->moves_left <= 0) {
+          return;
+        }
+        if (same_pos(punit->x, punit->y, x, y)) {
+          if (!ai_do_build_city(pplayer, punit)) {
+            UNIT_LOG(LOG_ERROR, punit, "could not make city on %s",
+                     map_get_tile_info_text(punit->x, punit->y));
+            ai_unit_new_role(punit, AIUNIT_NONE, -1, -1);
+          } else {
+            return; /* We came, we saw, we built... */
+          }
         } else {
-          return; /* We came, we saw, we built... */
+          UNIT_LOG(LOG_SETTLER, punit, "could not go to target");
+          /* ai_unit_new_role(punit, AIUNIT_NONE, -1, -1); */
+          return;
         }
-      } else {
-        UNIT_LOG(LOG_SETTLER, punit, "could not go to target");
-        /* ai_unit_new_role(punit, AIUNIT_NONE, -1, -1); */
-        return;
       }
     }
   }

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