Complete.Org: Mailing Lists: Archives: freeciv-dev: April 2005:
[Freeciv-Dev] Re: (PR#12861) evaluate_improvements simplification
Home

[Freeciv-Dev] Re: (PR#12861) evaluate_improvements simplification

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] Re: (PR#12861) evaluate_improvements simplification
From: "Brian Dunstan" <bdunstan149@xxxxxxxxx>
Date: Fri, 22 Apr 2005 11:48:50 -0700
Reply-to: bugs@xxxxxxxxxxx

<URL: http://bugs.freeciv.org/Ticket/Display.html?id=12861 >

--- Benoit Hudson <bh@xxxxxxxxxxxxxxxxxxx> wrote:
> 
> <URL:
> http://bugs.freeciv.org/Ticket/Display.html?id=12861
> >
> 
> On Thu, Apr 21, 2005 at 02:32:20PM -0700, Brian
> Dunstan wrote:
> > 1. makes an iterator for unit activities
> > 2. changes evaluate_improvements from a laundry
> list
> > to an iteration over activity types
> > 6. Shortens the representation of activity values
> in
> > struct ai_city to a single array.  
> 
> These are a great idea; can you cut them out into
> their own patch?

I am attaching a patch that does 1., 2., and 6.  This
patch does not change autosettler behavior; it
replicates the 2-step search done for roads and
railroads.

-Brian

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
diff -Nur -Xfreeciv/diff_ignore freeciv/common/city.h 
freeciv_altered4/common/city.h
--- freeciv/common/city.h       2005-04-22 09:21:00.081943408 -0400
+++ freeciv_altered4/common/city.h      2005-04-22 11:09:12.844893408 -0400
@@ -180,16 +180,9 @@
                                    avoiding paradox */
   bool celebrate;               /* try to celebrate in this city */
 
-  /* Used for caching when settlers evalueate which tile to improve,
-     and when we place workers. */
-  signed short int detox[CITY_MAP_SIZE][CITY_MAP_SIZE];
-  signed short int derad[CITY_MAP_SIZE][CITY_MAP_SIZE];
-  signed short int mine[CITY_MAP_SIZE][CITY_MAP_SIZE];
-  signed short int irrigate[CITY_MAP_SIZE][CITY_MAP_SIZE];
-  signed short int road[CITY_MAP_SIZE][CITY_MAP_SIZE];
-  signed short int railroad[CITY_MAP_SIZE][CITY_MAP_SIZE];
-  signed short int transform[CITY_MAP_SIZE][CITY_MAP_SIZE];
-  signed short int tile_value[CITY_MAP_SIZE][CITY_MAP_SIZE];
+  /* Used for caching change in value from a worker performing
+     a particular activity on a particular tile */
+  int act_value[ACTIVITY_LAST][CITY_MAP_SIZE][CITY_MAP_SIZE];
 
   /* so we can contemplate with warmap fresh and decide later */
   /* These values are for builder (F_SETTLERS) and founder (F_CITIES) units.
diff -Nur -Xfreeciv/diff_ignore freeciv/common/fc_types.h 
freeciv_altered4/common/fc_types.h
--- freeciv/common/fc_types.h   2005-04-22 09:21:00.073944624 -0400
+++ freeciv_altered4/common/fc_types.h  2005-04-22 11:09:12.844893408 -0400
@@ -46,6 +46,7 @@
 typedef int Specialist_type_id;
 typedef int Impr_Type_id;
 typedef enum output_type Output_type_id;
+typedef enum unit_activity Activity_type_id;
 typedef int Nation_Type_id;
 typedef int Team_Type_id;
 
diff -Nur -Xfreeciv/diff_ignore freeciv/common/unit.h 
freeciv_altered4/common/unit.h
--- freeciv/common/unit.h       2005-04-22 09:21:00.080943560 -0400
+++ freeciv_altered4/common/unit.h      2005-04-22 11:09:12.845893256 -0400
@@ -209,6 +209,19 @@
   } \
 }
 
+
+/* Iterates over the types of unit activity. */
+#define activity_type_iterate(act)                                         \
+{                                                                          \
+  Activity_type_id act;                                                        
    \
+                                                                           \
+  for (act = 0; act < ACTIVITY_LAST; act++) {
+
+#define activity_type_iterate_end                                          \
+  }                                                                        \
+}
+
+
 struct unit *unit_list_find(const struct unit_list *This, int id);
 
 void unit_list_sort_ord_map(struct unit_list *This);
diff -Nur -Xfreeciv/diff_ignore freeciv/server/settlers.c 
freeciv_altered4/server/settlers.c
--- freeciv/server/settlers.c   2005-04-22 10:04:54.371470528 -0400
+++ freeciv_altered4/server/settlers.c  2005-04-22 11:26:05.442955104 -0400
@@ -214,6 +214,8 @@
   /* FIXME: need a better way to guarantee pollution is cleaned up. */
   goodness = (goodness + best + 50) * 2;
 
+  goodness += city_owner(pcity)->ai.warmth;
+
   return goodness;
 }
 
@@ -246,6 +248,8 @@
     goodness = (goodness + best + 50) * 2;
   }
 
+  goodness += city_owner(pcity)->ai.frost;
+
   return goodness;
 }
 
@@ -861,9 +865,6 @@
   generate_warmap(mycity, punit);
 
   city_list_iterate(pplayer->cities, pcity) {
-#ifdef REALLY_DEBUG_THIS
-    freelog(LOG_DEBUG, "Evaluating improvements for %s...", pcity->name);
-#endif
     /* try to work near the city */
     city_map_checked_iterate(pcity->tile, i, j, ptile) {
       if (get_worker_city(pcity, i, j) == C_TILE_UNAVAILABLE
@@ -886,93 +887,44 @@
 
        /* now, consider various activities... */
 
-       time = mv_turns
-         + get_turns_for_activity_at(punit, ACTIVITY_IRRIGATE, ptile);
-       consider_settler_action(pplayer, ACTIVITY_IRRIGATE, -1,
-                               pcity->ai.irrigate[i][j], oldv, in_use, time,
-                               &best_newv, &best_oldv, best_act, best_tile,
-                               ptile);
-
-       if (unit_flag(punit, F_TRANSFORM)) {
-         time = mv_turns
-           + get_turns_for_activity_at(punit, ACTIVITY_TRANSFORM, ptile);
-         consider_settler_action(pplayer, ACTIVITY_TRANSFORM, -1,
-                                 pcity->ai.transform[i][j], oldv, in_use, time,
-                                 &best_newv, &best_oldv, best_act, best_tile,
-                                 ptile);
-       }
-
-       time = mv_turns
-         + get_turns_for_activity_at(punit, ACTIVITY_MINE, ptile);
-       consider_settler_action(pplayer, ACTIVITY_MINE, -1,
-                               pcity->ai.mine[i][j], oldv, in_use, time,
-                               &best_newv, &best_oldv, best_act, best_tile,
-                               ptile);
-
-       if (!tile_has_special(ptile, S_ROAD)) {
-         time = mv_turns
-           + get_turns_for_activity_at(punit, ACTIVITY_ROAD, ptile);
-         consider_settler_action(pplayer, ACTIVITY_ROAD,
-                                 road_bonus(ptile, S_ROAD) * 5,
-                                 pcity->ai.road[i][j], oldv, in_use, time,
-                                 &best_newv, &best_oldv, best_act, best_tile,
-                                 ptile);
-
-         if (can_rr) {
-           /* Count road time plus rail time. */
-           time += get_turns_for_activity_at(punit, ACTIVITY_RAILROAD, ptile);
-           consider_settler_action(pplayer, ACTIVITY_ROAD,
-                                   road_bonus(ptile, S_RAILROAD) * 3,
-                                   pcity->ai.railroad[i][j], oldv,
-                                   in_use, time,
+       activity_type_iterate(act) {
+
+         if(pcity->ai.act_value[act][i][j] >= 0 &&
+            can_unit_do_activity_targeted_at(punit, act, 
+                                             S_NO_SPECIAL, ptile)) {
+           int extra = 0;
+           time = mv_turns + get_turns_for_activity_at(punit, act, ptile);
+           if(act == ACTIVITY_ROAD) {
+             extra = road_bonus(ptile, S_ROAD) * 5;
+             if (can_rr) {
+
+               /* if we can make railroads eventually, consider making road 
here, and set
+                  extras and time to to consider railroads in main 
consider_settler_action call */
+               consider_settler_action(pplayer, ACTIVITY_ROAD,
+                                       extra,
+                                       
pcity->ai.act_value[ACTIVITY_ROAD][i][j], oldv, in_use, time,
+                                       &best_newv, &best_oldv, best_act, 
best_tile,
+                                       ptile);
+
+               act = ACTIVITY_RAILROAD;
+               
+               /* Count road time plus rail time. */
+               time += get_turns_for_activity_at(punit, ACTIVITY_RAILROAD, 
ptile);
+             }
+           } 
+           if (act == ACTIVITY_RAILROAD) {
+             extra = road_bonus(ptile, S_RAILROAD) * 3;
+           }
+
+           consider_settler_action(pplayer, act,
+                                   extra,
+                                   pcity->ai.act_value[act][i][j], oldv, 
in_use, time,
                                    &best_newv, &best_oldv,
                                    best_act, best_tile,
                                    ptile);
-         }
-       } else if (!tile_has_special(ptile, S_RAILROAD)
-                  && can_rr) {
-         time = mv_turns
-           + get_turns_for_activity_at(punit, ACTIVITY_RAILROAD, ptile);
-         consider_settler_action(pplayer, ACTIVITY_RAILROAD,
-                                 road_bonus(ptile, S_RAILROAD) * 3,
-                                 pcity->ai.railroad[i][j], oldv, in_use, time,
-                                 &best_newv, &best_oldv,
-                                 best_act, best_tile,
-                                 ptile);
-       } /* end S_ROAD else */
-
-       if (tile_has_special(ptile, S_POLLUTION)) {
-         time = mv_turns
-           + get_turns_for_activity_at(punit, ACTIVITY_POLLUTION, ptile);
-         consider_settler_action(pplayer, ACTIVITY_POLLUTION,
-                                 pplayer->ai.warmth,
-                                 pcity->ai.detox[i][j], oldv, in_use, time,
-                                 &best_newv, &best_oldv,
-                                 best_act, best_tile,
-                                 ptile);
-       }
-      
-       if (tile_has_special(ptile, S_FALLOUT)) {
-         time = mv_turns
-           + get_turns_for_activity_at(punit, ACTIVITY_FALLOUT, ptile);
-         consider_settler_action(pplayer, ACTIVITY_FALLOUT,
-                                 pplayer->ai.frost,
-                                 pcity->ai.derad[i][j], oldv, in_use, time,
-                                 &best_newv, &best_oldv,
-                                 best_act, best_tile,
-                                 ptile);
-       }
-
-#ifdef REALLY_DEBUG_THIS
-       freelog(LOG_DEBUG,
-               "(%d %d) I=%+-4d O=%+-4d M=%+-4d R=%+-4d RR=%+-4d P=%+-4d 
N=%+-4d",
-               i, j,
-               pcity->ai.irrigate[i][j], pcity->ai.transform[i][j],
-               pcity->ai.mine[i][j], pcity->ai.road[i][j],
-               pcity->ai.railroad[i][j], pcity->ai.detox[i][j],
-               pcity->ai.derad[i][j]);
-#endif
-      } /* end if we are a legal destination */
+         } /* endif: can the worker perform this action */
+       } activity_type_iterate_end;
+      } /* endif: are we travelling to a legal destination? */
     } city_map_checked_iterate_end;
   } city_list_iterate_end;
 
@@ -1168,39 +1120,36 @@
     int best = best_worker_tile_value(pcity);
 
     city_map_iterate(city_x, city_y) {
-      pcity->ai.detox[city_x][city_y] = -1;
-      pcity->ai.derad[city_x][city_y] = -1;
-      pcity->ai.mine[city_x][city_y] = -1;
-      pcity->ai.irrigate[city_x][city_y] = -1;
-      pcity->ai.transform[city_x][city_y] = -1;
-      pcity->ai.road[city_x][city_y] = -1;
-      pcity->ai.railroad[city_x][city_y] = -1;
+      activity_type_iterate(act) {
+       pcity->ai.act_value[act][city_x][city_y] = -1;
+      } activity_type_iterate_end;
     } city_map_iterate_end;
 
     city_map_checked_iterate(pcity->tile,
                             city_x, city_y, ptile) {
+      
 #ifndef NDEBUG
       Terrain_type_id old_terrain = ptile->terrain;
       enum tile_special_type old_special = ptile->special;
 #endif
 
-      pcity->ai.detox[city_x][city_y]
+      pcity->ai.act_value[ACTIVITY_POLLUTION][city_x][city_y] 
        = ai_calc_pollution(pcity, city_x, city_y, best, ptile);
-      pcity->ai.derad[city_x][city_y] =
-       ai_calc_fallout(pcity, pplayer, city_x, city_y, best, ptile);
-      pcity->ai.mine[city_x][city_y]
+      pcity->ai.act_value[ACTIVITY_FALLOUT][city_x][city_y]
+       = ai_calc_fallout(pcity, pplayer, city_x, city_y, best, ptile);
+      pcity->ai.act_value[ACTIVITY_MINE][city_x][city_y]
        = ai_calc_mine(pcity, city_x, city_y, ptile);
-      pcity->ai.irrigate[city_x][city_y]
+      pcity->ai.act_value[ACTIVITY_IRRIGATE][city_x][city_y]
         = ai_calc_irrigate(pcity, pplayer, city_x, city_y, ptile);
-      pcity->ai.transform[city_x][city_y]
+      pcity->ai.act_value[ACTIVITY_TRANSFORM][city_x][city_y]
        = ai_calc_transform(pcity, city_x, city_y, ptile);
 
       /* road_bonus() is handled dynamically later; it takes into
        * account settlers that have already been assigned to building
        * roads this turn. */
-      pcity->ai.road[city_x][city_y]
+      pcity->ai.act_value[ACTIVITY_ROAD][city_x][city_y]
        = ai_calc_road(pcity, pplayer, city_x, city_y, ptile);
-      pcity->ai.railroad[city_x][city_y] =
+      pcity->ai.act_value[ACTIVITY_RAILROAD][city_x][city_y] =
        ai_calc_railroad(pcity, pplayer, city_x, city_y, ptile);
 
       /* Make sure nothing was accidentally changed by these calculations. */

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