Complete.Org: Mailing Lists: Archives: freeciv-ai: May 2004:
[freeciv-ai] Re: (PR#6567) AI has too many boats.
Home

[freeciv-ai] Re: (PR#6567) AI has too many boats.

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [freeciv-ai] Re: (PR#6567) AI has too many boats.
From: "Gregory Berkolaiko" <Gregory.Berkolaiko@xxxxxxxxxxxxx>
Date: Mon, 17 May 2004 16:20:38 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=6567 >

The attached patch completes the project started by Joshua Hudson.  If a 
boat cannot find itself a passenger it will look for a city where 
customers can appear before going off exploring.

As a result there are fewer boats built.  But also there is less 
exploration.  It is hard to measure all the effects because the random 
events interfere too much, but with this patch the behaviour is more 
logical.

The problem is wiring the settlers production to signal the boats.  I had 
to do it crudely, since such is the current settlers system.

I think it can be committed since when we kill old settlers we will have 
to clean up the mess anyway, so it doesn't matter if we add a few specks 
of dirt now.

I hope Joshua will have time to comment on the patch.

G.

? ttt.gz
Index: ai/advdomestic.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/advdomestic.c,v
retrieving revision 1.106
diff -u -r1.106 advdomestic.c
--- ai/advdomestic.c    25 Feb 2004 20:23:49 -0000      1.106
+++ ai/advdomestic.c    17 May 2004 22:24:48 -0000
@@ -35,6 +35,7 @@
 #include "advmilitary.h"
 #include "aicity.h"
 #include "aidata.h"
+#include "ailog.h"
 #include "aitools.h"
 #include "aiunit.h"
 
@@ -996,9 +997,9 @@
     int want = pcity->ai.founder_want;
 
     if (want > choice->want) {
-      freelog(LOG_DEBUG, "%s (%d, %d) desires founders with passion %d",
-              pcity->name, pcity->x, pcity->y, want);
+      CITY_LOG(LOG_DEBUG, pcity, "desires founders with passion %d", want);
       choice->want = want;
+      choice->need_boat = pcity->ai.founder_boat;
       choice->type = CT_NONMIL;
       ai_choose_role_unit(pplayer, pcity, choice, F_CITIES, want);
       
Index: ai/aicity.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aicity.c,v
retrieving revision 1.150
diff -u -r1.150 aicity.c
--- ai/aicity.c 22 Apr 2004 23:07:07 -0000      1.150
+++ ai/aicity.c 17 May 2004 22:24:48 -0000
@@ -226,6 +226,7 @@
     pcity->ai.choice.choice = bestchoice.choice; /* we want to spend gold 
later */
     pcity->ai.choice.want = bestchoice.want; /* so that we spend it in the 
right city */
     pcity->ai.choice.type = bestchoice.type; /* instead of the one atop the 
list */
+    pcity->ai.choice.need_boat = bestchoice.need_boat;
   }
 
   if (bestchoice.want != 0) { /* Note - on fallbacks, will NOT get stopped 
building msg */
Index: ai/aitools.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aitools.c,v
retrieving revision 1.103
diff -u -r1.103 aitools.c
--- ai/aitools.c        4 May 2004 17:40:25 -0000       1.103
+++ ai/aitools.c        17 May 2004 22:24:48 -0000
@@ -812,6 +812,7 @@
     best->choice =cur->choice;
     best->want = cur->want;
     best->type = cur->type;
+    best->need_boat = cur->need_boat;
   }
 }
 
Index: ai/aiunit.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aiunit.c,v
retrieving revision 1.314
diff -u -r1.314 aiunit.c
--- ai/aiunit.c 4 May 2004 17:40:25 -0000       1.314
+++ ai/aiunit.c 17 May 2004 22:24:49 -0000
@@ -2308,6 +2308,74 @@
 }
 
 /****************************************************************************
+  A helper for ai_manage_ferryboat.  Finds a city that wants a ferry.  It
+  might signal for the ferry using pcity->ai.choice.need_boat field or
+  it might simply be building a ferry of it's own.
+
+  TODO: lift the path off the map
+  TODO (possible): put this and ai_ferry_findcargo into one PF-loop.  This 
+  will save some code lines but will be faster in the rare cases when there
+  passengers that can not be reached ("false positive").
+****************************************************************************/
+static bool ai_ferry_find_interested_city(struct unit *pferry)
+{
+  /* Path-finding stuff */
+  struct pf_map *map;
+  struct pf_parameter parameter;
+
+  UNIT_LOG(LOGLEVEL_FERRY, pferry, "Ferry looking for a city that needs it");
+
+  pft_fill_unit_parameter(&parameter, pferry);
+  /* We are looking for our own cities, no need to look into the unknown */
+  parameter.get_TB = no_fights_or_unknown;
+  parameter.omniscience = FALSE;
+  
+  map = pf_create_map(&parameter);
+
+  /* We want to consider the place we are currently in too, hence the 
+   * do-while loop */
+  do {
+    struct pf_position pos;
+    struct city *pcity;
+
+    pf_next_get_position(map, &pos);
+
+    pcity = map_get_city(pos.x, pos.y);
+    
+    if (pcity 
+        && (pcity->ai.choice.need_boat 
+            || (pcity->is_building_unit
+               && unit_has_role(pcity->currently_building, L_FERRYBOAT)))) {
+      bool really_needed = TRUE;
+
+      UNIT_LOG(LOGLEVEL_FERRY, pferry, "%s (%d, %d) looks promising, "
+               "checking if it has other ferries already", 
+               pcity->name, pcity->x, pcity->y);
+      unit_list_iterate(map_get_tile(pos.x, pos.y)->units, aunit) {
+       if (aunit != pferry && unit_owner(aunit) == unit_owner(pferry)
+            && unit_has_role(aunit->id, L_FERRYBOAT)) {
+
+          UNIT_LOG(LOGLEVEL_FERRY, pferry, "%s (%d, %d) doesn't "
+               "really want us", pcity->name, pcity->x, pcity->y);
+         really_needed = FALSE;
+         break;
+       }
+      } unit_list_iterate_end;
+
+      if (really_needed) {
+        set_goto_dest(pferry, pos.x, pos.y);
+        pf_destroy_map(map);
+        return TRUE;
+      }
+    }
+  } while (pf_next(map));
+
+  UNIT_LOG(LOGLEVEL_FERRY, pferry, "Ferry didn't find an interested city");
+  pf_destroy_map(map);
+  return FALSE;
+}
+
+/****************************************************************************
   It's about 12 feet square and has a capacity of almost 1000 pounds.
   It is well constructed of teak, and looks seaworthy.
 
@@ -2437,14 +2505,14 @@
     return;
   }
 
-  CHECK_UNIT(punit);
-  
-  if (punit->moves_left == 0 
-      || !is_ocean(map_get_terrain(punit->x, punit->y))) {
+  /* Try to find a city that needs a ferry */
+  if (ai_ferry_find_interested_city(punit)) {
+    (void) ai_unit_goto(punit, goto_dest_x(punit), goto_dest_y(punit));
     return;
   }
-  
+
   (void) ai_manage_explorer(punit);
+  
   if (punit->moves_left > 0) {
     struct city *pcity = find_nearest_safe_city(punit);
     if (pcity) {
Index: common/city.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/city.h,v
retrieving revision 1.141
diff -u -r1.141 city.h
--- common/city.h       22 Apr 2004 22:58:28 -0000      1.141
+++ common/city.h       17 May 2004 22:24:49 -0000
@@ -201,6 +201,7 @@
 
   /* so we can contemplate with warmap fresh and decide later */
   int settler_want, founder_want; /* for builder (F_SETTLERS) and founder 
(F_CITIES) */
+  bool founder_boat; /* if the city founder will need a boat */
   int invasion; /* who's coming to kill us, for attack co-ordination */
   int attack, bcost; /* This is also for invasion - total power and value of
                       * all units coming to kill us. */
Index: server/settlers.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/settlers.c,v
retrieving revision 1.180
diff -u -r1.180 settlers.c
--- server/settlers.c   26 Feb 2004 03:24:16 -0000      1.180
+++ server/settlers.c   17 May 2004 22:24:49 -0000
@@ -1532,6 +1532,10 @@
       want = -199;
   } unit_list_iterate_end;
 
+  /* Did we count on using an existing boat.  If yes we need to keep it
+   * in the city. */
+  pcity->ai.founder_boat = (ferryboat != NULL);
+
   if (gx == -1) {
     pcity->ai.founder_want = -want;
   } else {

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