[freeciv-ai] (PR#9615) AI-specific functions in citytools.c
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
<URL: http://rt.freeciv.org/Ticket/Display.html?id=9615 >
> [jdorje - Fri Aug 06 02:13:48 2004]:
>
> > [glip - Thu Aug 05 20:04:08 2004]:
> >
> > Many functions in server/citytools.c are only used by the AI and are not
> > especially tied to the server. Here is a list (probably incomplete):
> > 1. is_worked_here (not used)
> > 2. food_weighting
> > 3. city_tile_value
> > 4. worst_worker_tile_value
> > 5. best_worker_tile_value
> > 6. built_elsewhere
> > 7. is_building_other_wonder
> >
> > In the attached patch I move the more obvious 6 and 7 and remove 1.
>
> Great, except that you didn't actually remove 1.
Ok, second try (6 and 7 are moved already):
remove 1,
move 4 and settler_eats to advdomestic
move 2, 3, 5 to settler.c
fix the obviously wrong comparison in 5.
G.
? saves
? ttt.gz
? ai/aiexplorer.c
? ai/aiexplorer.h
Index: ai/advdomestic.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/advdomestic.c,v
retrieving revision 1.113
diff -u -r1.113 advdomestic.c
--- ai/advdomestic.c 6 Aug 2004 02:13:34 -0000 1.113
+++ ai/advdomestic.c 6 Aug 2004 14:42:59 -0000
@@ -165,31 +165,6 @@
}
/**************************************************************************
-Get value of best usable tile on city map.
-**************************************************************************/
-static int ai_best_tile_value(struct city *pcity)
-{
- int best = 0;
- int food;
-
- /* food = (pcity->size * 2 - get_food_tile(2,2, pcity)) +
settler_eats(pcity); */
- /* Following simply works better, as far as I can tell. */
- food = 0;
-
- city_map_iterate(x, y) {
- if (can_place_worker_here(pcity, x, y)) {
- int value = city_tile_value(pcity, x, y, food, 0);
-
- if (value > best) {
- best = value;
- }
- }
- } city_map_iterate_end;
-
- return best;
-}
-
-/**************************************************************************
Returns the value (desire to build it) of the improvement for keeping
of order in the city.
@@ -414,6 +389,71 @@
}
/**************************************************************************
+ Returns the city_tile_value of the worst tile worked by pcity
+ (not including the city center). Returns 0 if no tiles are worked.
+**************************************************************************/
+static int worst_worker_tile_value(struct city *pcity)
+{
+ int worst = 0;
+
+ city_map_iterate(x, y) {
+ if (is_city_center(x, y)) {
+ continue;
+ }
+ if (get_worker_city(pcity, x, y) == C_TILE_WORKER) {
+ int tmp = city_tile_value(pcity, x, y, 0, 0);
+
+ if (tmp < worst || worst == 0) {
+ worst = tmp;
+ }
+ }
+ } city_map_iterate_end;
+
+ return(worst);
+}
+
+/**************************************************************************
+ Get city_tile_value of best tile available to pcity.
+**************************************************************************/
+static int best_free_tile_value(struct city *pcity)
+{
+ int best = 0;
+
+ city_map_iterate(x, y) {
+ if (get_worker_city(pcity, x, y) == C_TILE_EMPTY) {
+ int tmp = city_tile_value(pcity, x, y, 0, 0);
+
+ if (tmp > best) {
+ best = tmp;
+ }
+ }
+ } city_map_iterate_end;
+
+ return best;
+}
+
+/**************************************************************************
+ Returns the amount of food consumed by pcity's units.
+**************************************************************************/
+static int settler_eats(struct city *pcity)
+{
+ struct government *gov = get_gov_pcity(pcity);
+ int free_food = citygov_free_food(pcity, gov);
+ int eat = 0;
+
+ unit_list_iterate(pcity->units_supported, this_unit) {
+ int food_cost = utype_food_cost(unit_type(this_unit), gov);
+
+ adjust_city_free_cost(&free_food, &food_cost);
+ if (food_cost > 0) {
+ eat += food_cost;
+ }
+ } unit_list_iterate_end;
+
+ return eat;
+}
+
+/**************************************************************************
Evaluate the current desirability of all city improvements for the given
city to update pcity->ai.building_want.
**************************************************************************/
@@ -449,7 +489,7 @@
(pcity->shield_prod * SHIELD_WEIGHTING * 100) / city_shield_bonus(pcity);
needpower = (city_got_building(pcity, B_MFG) ? 2 :
(city_got_building(pcity, B_FACTORY) ? 1 : 0));
- val = ai_best_tile_value(pcity);
+ val = best_free_tile_value(pcity);
wwtv = worst_worker_tile_value(pcity);
/* because the benefit doesn't come immediately, and to stop stupidity */
Index: server/citytools.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/citytools.c,v
retrieving revision 1.268
diff -u -r1.268 citytools.c
--- server/citytools.c 6 Aug 2004 02:13:35 -0000 1.268
+++ server/citytools.c 6 Aug 2004 14:42:59 -0000
@@ -422,134 +422,6 @@
}
/**************************************************************************
-...
-**************************************************************************/
-bool is_worked_here(int x, int y)
-{
- return (map_get_tile(x, y)->worked != NULL); /* saves at least 10% of
runtime CPU usage! */
-}
-
-/**************************************************************************
-The value of excees food is dependent on the amount of food it takes for a
-city to increase in size. This amount is in turn dependent on the citysize,
-hence this function.
-The value returned from this function does not take into account whether
-increasing a city's size is attractive, but only how effective the food
-will be.
-**************************************************************************/
-int food_weighting(int city_size)
-{
- static int cache[MAX_CITY_SIZE];
- static bool cache_valid = FALSE;
-
- if (!cache_valid) {
- int size = 0;
-
- for (size = 1; size < MAX_CITY_SIZE; size++) {
- int food_weighting_is_for = 4; /* FOOD_WEIGHTING applies to city with
- foodbox width of 4 */
- int weighting = (food_weighting_is_for * FOOD_WEIGHTING) /
- (city_granary_size(size) / game.foodbox);
-
- /* If the citysize is 1 we assume it will not be so for long, and
- so adjust the value a little downwards. */
- if (size == 1) {
- weighting = (weighting * 3) / 4;
- }
- cache[size] = weighting;
- }
- cache_valid = TRUE;
- }
-
- assert(city_size > 0 && city_size < MAX_CITY_SIZE);
-
- return cache[city_size];
-}
-
-/**************************************************************************
-...
-**************************************************************************/
-int city_tile_value(struct city *pcity, int x, int y, int foodneed, int
prodneed)
-{
- int i, j, k;
- struct player *plr;
-
- plr = city_owner(pcity);
-
- i = city_get_food_tile(x, y, pcity);
- if (foodneed > 0) i += 9 * (MIN(i, foodneed));
-/* *= 10 led to stupidity with foodneed = 1, mine, and farmland -- Syela */
- i *= food_weighting(MAX(2,pcity->size));
-
- j = city_get_shields_tile(x, y, pcity);
- if (prodneed > 0) j += 9 * (MIN(j, prodneed));
- j *= SHIELD_WEIGHTING * city_shield_bonus(pcity);
- j /= 100;
-
- k = (city_get_trade_tile(x, y, pcity) * pcity->ai.trade_want
- * (city_tax_bonus(pcity) * plr->economic.tax
- + city_luxury_bonus(pcity) * plr->economic.luxury
- + city_science_bonus(pcity) * plr->economic.science)) / 10000;
-
- return(i + j + k);
-}
-
-/**************************************************************************
-...
-**************************************************************************/
-int worst_worker_tile_value(struct city *pcity)
-{
- int worst = 0, tmp;
- city_map_iterate(x, y) {
- if (is_city_center(x, y))
- continue;
- if (get_worker_city(pcity, x, y) == C_TILE_WORKER) {
- tmp = city_tile_value(pcity, x, y, 0, 0);
- if (tmp < worst || worst == 0) worst = tmp;
- }
- } city_map_iterate_end;
- return(worst);
-}
-
-/**************************************************************************
-...
-**************************************************************************/
-int best_worker_tile_value(struct city *pcity)
-{
- int best = 0, tmp;
- city_map_iterate(x, y) {
- if (is_city_center(x, y) ||
- (get_worker_city(pcity, x, y) == C_TILE_WORKER) ||
- can_place_worker_here(pcity, x, y)) {
- tmp = city_tile_value(pcity, x, y, 0, 0);
- if (tmp < best || best == 0) best = tmp;
- }
- } city_map_iterate_end;
- return(best);
-}
-
-/**************************************************************************
-...
-**************************************************************************/
-int settler_eats(struct city *pcity)
-{
- struct government *g = get_gov_pcity(pcity);
- int free_food = citygov_free_food(pcity, g);
- int eat = 0;
-
- unit_list_iterate(pcity->units_supported, this_unit) {
- int food_cost = utype_food_cost(unit_type(this_unit), g);
- adjust_city_free_cost(&free_food, &food_cost);
- if (food_cost > 0) {
- eat += food_cost;
- }
- }
- unit_list_iterate_end;
-
- return eat;
-}
-
-/**************************************************************************
Will unit of this type be created as veteran?
**************************************************************************/
int do_make_unit_veteran(struct city *pcity, Unit_Type_id id)
Index: server/citytools.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/citytools.h,v
retrieving revision 1.54
diff -u -r1.54 citytools.h
--- server/citytools.h 6 Aug 2004 02:13:35 -0000 1.54
+++ server/citytools.h 6 Aug 2004 14:42:59 -0000
@@ -34,18 +34,12 @@
bool can_sell_building(struct city *pcity, Impr_Type_id id);
struct city *find_city_wonder(Impr_Type_id id);
int build_points_left(struct city *pcity);
-bool is_worked_here(int x, int y);
-int food_weighting(int city_size);
-int city_tile_value(struct city *pcity, int x, int y, int foodneed, int
prodneed);
-int settler_eats(struct city *pcity);
int do_make_unit_veteran(struct city *pcity, Unit_Type_id id);
int city_shield_bonus(struct city *pcity);
int city_luxury_bonus(struct city *pcity);
int city_science_bonus(struct city *pcity);
int city_tax_bonus(struct city *pcity);
-int worst_worker_tile_value(struct city *pcity);
-int best_worker_tile_value(struct city *pcity);
void transfer_city_units(struct player *pplayer, struct player *pvictim,
struct unit_list *units, struct city *pcity,
struct city *exclude_city,
Index: server/settlers.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/settlers.c,v
retrieving revision 1.190
diff -u -r1.190 settlers.c
--- server/settlers.c 6 Aug 2004 10:35:49 -0000 1.190
+++ server/settlers.c 6 Aug 2004 14:42:59 -0000
@@ -170,6 +170,81 @@
}
/**************************************************************************
+ The value of excees food is dependent on the amount of food it takes for
+ a city to increase in size. This amount is in turn dependent on the
+ citysize, hence this function.
+
+ The value returned from this function does not take into account whether
+ increasing a city's size is attractive, but only how effective the food
+ will be.
+
+ The return value is simply
+ 4*FOOD_WEIGHTING / (num_of_columns_in_foodbox).
+**************************************************************************/
+int food_weighting(int city_size)
+{
+ static int cache[MAX_CITY_SIZE];
+ static bool cache_valid = FALSE;
+
+ if (!cache_valid) {
+ int size = 0;
+
+ for (size = 1; size < MAX_CITY_SIZE; size++) {
+ int food_weighting_is_for = 4; /* FOOD_WEIGHTING applies to city with
+ foodbox width of 4 */
+ int weighting = (food_weighting_is_for * FOOD_WEIGHTING) /
+ (city_granary_size(size) / game.foodbox);
+
+ /* If the citysize is 1 we assume it will not be so for long, and
+ so adjust the value a little downwards. */
+ if (size == 1) {
+ weighting = (weighting * 3) / 4;
+ }
+ cache[size] = weighting;
+ }
+ cache_valid = TRUE;
+ }
+
+ assert(city_size > 0 && city_size < MAX_CITY_SIZE);
+
+ return cache[city_size];
+}
+
+/**************************************************************************
+ Returns a measure of goodness of a tile to pcity.
+
+ FIXME: foodneed and prodneed are never used.
+**************************************************************************/
+int city_tile_value(struct city *pcity, int x, int y,
+ int foodneed, int prodneed)
+{
+ int food_value, shield_value, trade_value;
+ struct player *plr;
+
+ plr = city_owner(pcity);
+
+ food_value = city_get_food_tile(x, y, pcity);
+ if (foodneed > 0) {
+ food_value += 9 * MIN(food_value, foodneed);
+ }
+ food_value *= food_weighting(MAX(2, pcity->size));
+
+ shield_value = city_get_shields_tile(x, y, pcity);
+ if (prodneed > 0) {
+ shield_value += 9 * (MIN(shield_value, prodneed));
+ }
+ shield_value *= SHIELD_WEIGHTING * city_shield_bonus(pcity);
+ shield_value /= 100;
+
+ trade_value = (city_get_trade_tile(x, y, pcity) * pcity->ai.trade_want
+ * (city_tax_bonus(pcity) * plr->economic.tax
+ + city_luxury_bonus(pcity) * plr->economic.luxury
+ + city_science_bonus(pcity) * plr->economic.science)) / 10000;
+
+ return(food_value + shield_value + trade_value);
+}
+
+/**************************************************************************
Calculates the value of removing pollution at the given tile.
(map_x, map_y) is the map position of the tile.
@@ -1097,6 +1172,27 @@
#undef LOG_SETTLER
/**************************************************************************
+ Returns city_tile_value of the best tile worked by or available to pcity.
+**************************************************************************/
+static int best_worker_tile_value(struct city *pcity)
+{
+ int best = 0;
+
+ city_map_iterate(x, y) {
+ if (is_city_center(x, y)
+ || get_worker_city(pcity, x, y) == C_TILE_WORKER
+ || get_worker_city(pcity, x, y) == C_TILE_EMPTY) {
+ int tmp = city_tile_value(pcity, x, y, 0, 0);
+
+ if (tmp > best) {
+ best = tmp;
+ }
+ }
+ } city_map_iterate_end;
+ return(best);
+}
+
+/**************************************************************************
Do all tile improvement calculations and cache them for later.
These values are used in evaluate_improvements() so this function must
Index: server/settlers.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/settlers.h,v
retrieving revision 1.25
diff -u -r1.25 settlers.h
--- server/settlers.h 5 Aug 2004 11:34:18 -0000 1.25
+++ server/settlers.h 6 Aug 2004 14:42:59 -0000
@@ -29,6 +29,10 @@
void ai_manage_settler(struct player *pplayer, struct unit *punit);
void init_settlers(void);
+
+int food_weighting(int city_size);
+int city_tile_value(struct city *pcity, int x, int y,
+ int foodneed, int prodneed);
void initialize_infrastructure_cache(struct player *pplayer);
void contemplate_terrain_improvements(struct city *pcity);
|
|