1 /***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
15 #include <fc_config.h>
20 #include <math.h> /* pow, sqrt, exp */
23 #include "distribute.h"
34 #include "government.h"
35 #include "improvement.h"
39 #include "specialist.h"
40 #include "traderoutes.h"
48 /* Define this to add in extra (very slow) assertions for the city code. */
51 static char *citylog_map_line(int y
, int city_radius_sq
, int *city_map_data
);
53 /* only used for debugging */
54 static void citylog_map_index(enum log_level level
);
55 static void citylog_map_radius_sq(enum log_level level
);
56 #endif /* FREECIV_DEBUG */
58 /* Get city tile informations using the city tile index. */
59 static struct iter_index
*city_map_index
= NULL
;
60 /* Get city tile informations using the city tile coordinates. This is an
61 * [x][y] array of integer values corresponding to city_map_index. The
62 * coordinates x and y are in the range [0, CITY_MAP_MAX_SIZE] */
63 static int city_map_xy
[CITY_MAP_MAX_SIZE
][CITY_MAP_MAX_SIZE
];
65 /* number of tiles of a city; depends on the squared city radius */
66 static int city_map_numtiles
[CITY_MAP_MAX_RADIUS_SQ
+ 1];
68 /* definitions and functions for the tile_cache */
73 static inline void city_tile_cache_update(struct city
*pcity
);
74 static inline int city_tile_cache_get_output(const struct city
*pcity
,
76 enum output_type_id o
);
78 struct citystyle
*city_styles
= NULL
;
80 /* One day these values may be read in from the ruleset. In the meantime
81 * they're just an easy way to access information about each output type. */
82 struct output_type output_types
[O_LAST
] = {
83 {O_FOOD
, N_("Food"), "food", TRUE
, UNHAPPY_PENALTY_SURPLUS
},
84 {O_SHIELD
, N_("Shield"), "shield", TRUE
, UNHAPPY_PENALTY_SURPLUS
},
85 {O_TRADE
, N_("Trade"), "trade", TRUE
, UNHAPPY_PENALTY_NONE
},
86 {O_GOLD
, N_("Gold"), "gold", FALSE
, UNHAPPY_PENALTY_ALL_PRODUCTION
},
87 {O_LUXURY
, N_("Luxury"), "luxury", FALSE
, UNHAPPY_PENALTY_NONE
},
88 {O_SCIENCE
, N_("Science"), "science", FALSE
, UNHAPPY_PENALTY_ALL_PRODUCTION
}
91 /**************************************************************************
92 Returns the coordinates for the given city tile index taking into account
93 the squared city radius.
94 **************************************************************************/
95 bool city_tile_index_to_xy(int *city_map_x
, int *city_map_y
,
96 int city_tile_index
, int city_radius_sq
)
98 fc_assert_ret_val(city_radius_sq
>= CITY_MAP_MIN_RADIUS_SQ
, FALSE
);
99 fc_assert_ret_val(city_radius_sq
<= CITY_MAP_MAX_RADIUS_SQ
, FALSE
);
101 /* tile indices are sorted from smallest to largest city radius */
102 if (city_tile_index
< 0
103 || city_tile_index
>= city_map_tiles(city_radius_sq
)) {
107 *city_map_x
= CITY_REL2ABS(city_map_index
[city_tile_index
].dx
);
108 *city_map_y
= CITY_REL2ABS(city_map_index
[city_tile_index
].dy
);
113 /**************************************************************************
114 Returns the index for the given city tile coordinates taking into account
115 the squared city radius.
116 **************************************************************************/
117 int city_tile_xy_to_index(int city_map_x
, int city_map_y
,
120 fc_assert_ret_val(city_radius_sq
>= CITY_MAP_MIN_RADIUS_SQ
, 0);
121 fc_assert_ret_val(city_radius_sq
<= CITY_MAP_MAX_RADIUS_SQ
, 0);
122 fc_assert_ret_val(is_valid_city_coords(city_radius_sq
, city_map_x
,
125 return city_map_xy
[city_map_x
][city_map_y
];
128 /**************************************************************************
129 Returns the current squared radius of the city.
130 **************************************************************************/
131 int city_map_radius_sq_get(const struct city
*pcity
)
133 /* a save return value is only the minimal squared radius */
134 fc_assert_ret_val(pcity
!= NULL
, CITY_MAP_MIN_RADIUS_SQ
);
136 return pcity
->city_radius_sq
;
139 /**************************************************************************
140 Returns the current squared radius of the city.
141 **************************************************************************/
142 void city_map_radius_sq_set(struct city
*pcity
, int radius_sq
)
144 fc_assert_ret(radius_sq
>= CITY_MAP_MIN_RADIUS_SQ
);
145 fc_assert_ret(radius_sq
<= CITY_MAP_MAX_RADIUS_SQ
);
147 pcity
->city_radius_sq
= radius_sq
;
150 /**************************************************************************
151 Maximum city radius in this ruleset.
152 **************************************************************************/
153 int rs_max_city_radius_sq(void)
155 int max_rad
= game
.info
.init_city_radius_sq
156 + effect_cumulative_max(EFT_CITY_RADIUS_SQ
, NULL
);
158 return MIN(max_rad
, CITY_MAP_MAX_RADIUS_SQ
);
161 /**************************************************************************
162 Return the number of tiles for the given city radius. Special case is
163 the value -1 for no city tiles.
164 **************************************************************************/
165 int city_map_tiles(int city_radius_sq
)
167 if (city_radius_sq
== CITY_MAP_CENTER_RADIUS_SQ
) {
168 /* special case: city center; first tile of the city map */
172 fc_assert_ret_val(city_radius_sq
>= CITY_MAP_MIN_RADIUS_SQ
, -1);
173 fc_assert_ret_val(city_radius_sq
<= CITY_MAP_MAX_RADIUS_SQ
, -1);
175 return city_map_numtiles
[city_radius_sq
];
178 /**************************************************************************
179 Return TRUE if the given city coordinate pair is "valid"; that is, if it
180 is a part of the citymap and thus is workable by the city.
181 **************************************************************************/
182 bool is_valid_city_coords(const int city_radius_sq
, const int city_map_x
,
183 const int city_map_y
)
185 /* The city's valid positions are in a circle around the city center.
186 * Depending on the value for the squared city radius the circle will be:
188 * - rectangular (max radius = 5; max squared radius = 26)
190 * 0 1 2 3 4 5 6 7 8 9 10
193 * 1 25 20 17 16 17 20 25 -4
194 * 2 25 18 13 10 9 10 13 18 25 -3
195 * 3 20 13 8 5 4 5 8 13 20 -2
196 * 4 26 17 10 5 2 1 2 5 10 17 26 -1
197 * 5 25 16 9 4 1 0 1 4 9 16 25 +0
198 * 6 26 17 10 5 2 1 2 5 10 17 26 +1
199 * 7 20 13 8 5 4 5 8 13 20 +2
200 * 8 25 18 13 10 9 10 13 18 25 +3
201 * 9 25 20 17 16 17 20 25 +4
204 * -5 -4 -3 -2 -1 +0 +1 +2 +3 +4 +5
206 * - hexagonal (max radius = 5; max squared radius = 26)
208 * 0 1 2 3 4 5 6 7 8 9 10
210 * 0 25 25 25 25 25 25 -5
211 * 1 25 16 16 16 16 16 25 -4
212 * 2 25 16 9 9 9 9 16 25 -3
213 * 3 25 16 9 4 4 4 9 16 25 -2
214 * 4 25 16 9 4 1 1 4 9 16 25 -1
215 * 5 25 16 9 4 1 0 1 4 9 16 25 +0
216 * 6 25 16 9 4 1 1 4 9 16 25 +1
217 * 7 25 16 9 4 4 4 9 16 25 +2
218 * 8 25 16 9 9 9 9 16 25 +3
219 * 9 25 16 16 16 16 16 25 +4
220 * 10 25 25 25 25 25 25 +5
222 * -5 -4 -3 -2 -1 +0 +1 +2 +3 +4 +5
224 * The following tabes show the tiles per city radii / squared city radii.
225 * '-' indicates no change compared to the previous value
227 * radius | 0 | 1 | | | 2 | | | | | 3
228 * radius_sq | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10
229 * ------------------+----+----+----+----+----+----+----+----+----+----
230 * tiles rectangular | 5 | 9 | - | 13 | 21 | - | - | 25 | 29 | 37
231 * tiles hexagonal | 7 | - | - | 19 | - | - | - | - | 37 | -
233 * radius | | | | | | | 4 | | |
234 * radius_sq | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20
235 * ------------------+----+----+----+----+----+----+----+----+----+----
236 * tiles rectangular | - | - | 45 | - | - | - | 49 | 57 | 61 | 69
237 * tiles hexagonal | - | - | - | - | - | 61 | - | - | - | -
239 * radius | | | | | | 5
240 * radius_sq | 21 | 22 | 23 | 24 | 25 | 26
241 * ------------------+----+----+----+----+----+----
242 * tiles rectangular | - | - | - | - | 81 | 89
243 * tiles hexagonal | - | - | - | - | 91 | -
245 * So radius_sq == 5 (radius == 2) corresponds to the "traditional"
248 int dist
= map_vector_to_sq_distance(CITY_ABS2REL(city_map_x
),
249 CITY_ABS2REL(city_map_y
));
251 return dist
<= city_radius_sq
;
254 /**************************************************************************
255 Finds the city map coordinate for a given map position and a city
256 center. Returns whether the map position is inside of the city map.
257 **************************************************************************/
258 bool city_tile_to_city_map(int *city_map_x
, int *city_map_y
,
259 const int city_radius_sq
,
260 const struct tile
*city_center
,
261 const struct tile
*map_tile
)
263 map_distance_vector(city_map_x
, city_map_y
, city_center
, map_tile
);
265 *city_map_x
+= CITY_MAP_MAX_RADIUS
;
266 *city_map_y
+= CITY_MAP_MAX_RADIUS
;
268 return is_valid_city_coords(city_radius_sq
, *city_map_x
, *city_map_y
);
271 /**************************************************************************
272 Finds the city map coordinate for a given map position and a
273 city. Returns whether the map position is inside of the city map.
274 **************************************************************************/
275 bool city_base_to_city_map(int *city_map_x
, int *city_map_y
,
276 const struct city
*const pcity
,
277 const struct tile
*map_tile
)
279 return city_tile_to_city_map(city_map_x
, city_map_y
,
280 city_map_radius_sq_get(pcity
), pcity
->tile
,
284 /**************************************************************************
285 Finds the map position for a given city map coordinate of a certain
286 city. Returns true if the map position found is real.
287 **************************************************************************/
288 struct tile
*city_map_to_tile(const struct tile
*city_center
,
289 int city_radius_sq
, int city_map_x
,
294 fc_assert_ret_val(is_valid_city_coords(city_radius_sq
, city_map_x
,
297 index_to_map_pos(&tile_x
, &tile_y
, tile_index(city_center
));
298 tile_x
+= CITY_ABS2REL(city_map_x
);
299 tile_y
+= CITY_ABS2REL(city_map_y
);
301 return map_pos_to_tile(&(wld
.map
), tile_x
, tile_y
);
304 /**************************************************************************
305 Compare two integer values, as required by qsort.
306 ***************************************************************************/
307 static int cmp(int v1
, int v2
)
311 } else if (v1
> v2
) {
318 /**************************************************************************
319 Compare two iter_index values from the city_map_index.
321 This function will be passed to qsort(). It should never return zero,
322 or the sort order will be left up to qsort and will be undefined. This
323 would mean that server execution would not be reproducable.
324 ***************************************************************************/
325 int compare_iter_index(const void *a
, const void *b
)
327 const struct iter_index
*index1
= a
, *index2
= b
;
330 value
= cmp(index1
->dist
, index2
->dist
);
335 value
= cmp(index1
->dx
, index2
->dx
);
340 value
= cmp(index1
->dy
, index2
->dy
);
341 fc_assert(0 != value
);
345 /****************************************************************************
346 Return one line (y coordinate) of a city map. *city_map_data is a pointer
347 to an array containing the data which should be printed. Its size is
348 defined by city_map_tiles(city_radius_sq).
349 *****************************************************************************/
350 #define CITYLOG_MAX_VAL 9999 /* maximal value displayed in the citylog */
351 static char *citylog_map_line(int y
, int city_radius_sq
, int *city_map_data
)
354 static char citylog
[128], tmp
[8];
356 fc_assert_ret_val(city_map_data
!= NULL
, NULL
);
358 /* print y coordinates (absolut) */
359 fc_snprintf(citylog
, sizeof(citylog
), "%2d ", y
);
362 for (x
= 0; x
< CITY_MAP_MAX_SIZE
; x
++) {
363 if (is_valid_city_coords(city_radius_sq
, x
, y
)) {
364 mindex
= city_tile_xy_to_index(x
, y
, city_radius_sq
);
365 /* show values between -10000 and +10000 */
366 if (city_map_data
[mindex
] >= -CITYLOG_MAX_VAL
367 && city_map_data
[mindex
] <= CITYLOG_MAX_VAL
) {
368 fc_snprintf(tmp
, sizeof(tmp
), "%5d", city_map_data
[mindex
]);
369 sz_strlcat(citylog
, tmp
);
371 fc_snprintf(tmp
, sizeof(tmp
), " ####");
372 sz_strlcat(citylog
, tmp
);
375 fc_snprintf(tmp
, sizeof(tmp
), " ");
376 sz_strlcat(citylog
, tmp
);
380 /* print y coordinates (relativ) */
381 fc_snprintf(tmp
, sizeof(tmp
), " %+4d", CITY_ABS2REL(y
));
382 sz_strlcat(citylog
, tmp
);
386 #undef CITYLOG_MAX_VAL
388 /****************************************************************************
389 Display 'map_data' on a city map with the given radius 'radius_sq' for the
390 requested log level. The size of 'map_data' is defined by
391 city_map_tiles(radius_sq).
392 *****************************************************************************/
393 void citylog_map_data(enum log_level level
, int radius_sq
, int *map_data
)
396 char line
[128], tmp
[8];
398 if (!log_do_output_for_level(level
)) {
402 log_base(level
, "(max squared city radius = %d)", CITY_MAP_MAX_RADIUS_SQ
);
404 /* print x coordinates (absolut) */
405 fc_snprintf(line
, sizeof(line
), " ");
406 for (x
= 0; x
< CITY_MAP_MAX_SIZE
; x
++) {
407 fc_snprintf(tmp
, sizeof(tmp
), "%+5d", x
);
408 sz_strlcat(line
, tmp
);
410 log_base(level
, "%s", line
);
412 for (y
= 0; y
< CITY_MAP_MAX_SIZE
; y
++) {
413 log_base(level
, "%s", citylog_map_line(y
, radius_sq
, map_data
));
416 /* print x coordinates (relativ) */
417 fc_snprintf(line
, sizeof(line
), " ");
418 for (x
= 0; x
< CITY_MAP_MAX_SIZE
; x
++) {
419 fc_snprintf(tmp
, sizeof(tmp
), "%+5d", CITY_ABS2REL(x
));
420 sz_strlcat(line
, tmp
);
422 log_base(level
, "%s", line
);
425 /****************************************************************************
426 Display the location of the workers within the city map of pcity.
427 *****************************************************************************/
428 void citylog_map_workers(enum log_level level
, struct city
*pcity
)
430 int *city_map_data
= NULL
;
432 fc_assert_ret(pcity
!= NULL
);
434 if (!log_do_output_for_level(level
)) {
438 city_map_data
= fc_calloc(city_map_tiles(city_map_radius_sq_get(pcity
)),
439 sizeof(*city_map_data
));
441 city_map_iterate(city_map_radius_sq_get(pcity
), cindex
, x
, y
) {
442 struct tile
*ptile
= city_map_to_tile(city_tile(pcity
),
443 city_map_radius_sq_get(pcity
),
445 city_map_data
[cindex
] = (ptile
&& tile_worked(ptile
) == pcity
)
446 ? (is_free_worked_index(cindex
) ? 2 : 1) : 0;
447 } city_map_iterate_end
;
449 log_base(level
, "[%s (%d)] workers map:", city_name_get(pcity
), pcity
->id
);
450 citylog_map_data(level
, city_map_radius_sq_get(pcity
), city_map_data
);
451 FC_FREE(city_map_data
);
455 /****************************************************************************
456 Log the index of all tiles of the city map.
457 *****************************************************************************/
458 static void citylog_map_index(enum log_level level
)
460 int *city_map_data
= NULL
;
462 if (!log_do_output_for_level(level
)) {
466 city_map_data
= fc_calloc(city_map_tiles(CITY_MAP_MAX_RADIUS_SQ
),
467 sizeof(*city_map_data
));
469 city_map_iterate(CITY_MAP_MAX_RADIUS_SQ
, cindex
, x
, y
) {
470 city_map_data
[cindex
] = cindex
;
471 } city_map_iterate_end
;
473 log_debug("city map index:");
474 citylog_map_data(level
, CITY_MAP_MAX_RADIUS_SQ
, city_map_data
);
475 FC_FREE(city_map_data
);
478 /****************************************************************************
479 Log the radius of all tiles of the city map.
480 *****************************************************************************/
481 static void citylog_map_radius_sq(enum log_level level
)
483 int *city_map_data
= NULL
;
485 if (!log_do_output_for_level(level
)) {
489 city_map_data
= fc_calloc(city_map_tiles(CITY_MAP_MAX_RADIUS_SQ
),
490 sizeof(*city_map_data
));
492 city_map_iterate(CITY_MAP_MAX_RADIUS_SQ
, cindex
, x
, y
) {
493 city_map_data
[cindex
] = map_vector_to_sq_distance(CITY_ABS2REL(x
),
495 } city_map_iterate_end
;
497 log_debug("city map squared radius:");
498 citylog_map_data(level
, CITY_MAP_MAX_RADIUS_SQ
, city_map_data
);
499 FC_FREE(city_map_data
);
501 #endif /* FREECIV_DEBUG */
503 /**************************************************************************
504 Fill the arrays city_map_index, city_map_xy and city_map_numtiles. This
505 may depend on topology and ruleset settings.
506 ***************************************************************************/
507 void generate_city_map_indices(void)
509 int i
, dx
, dy
, city_x
, city_y
, dist
, city_count_tiles
= 0;
510 struct iter_index city_map_index_tmp
[CITY_MAP_MAX_SIZE
511 * CITY_MAP_MAX_SIZE
];
513 /* initialise map information for each city radii */
514 for (i
= 0; i
<= CITY_MAP_MAX_RADIUS_SQ
; i
++) {
515 city_map_numtiles
[i
] = 0; /* will be set below */
518 /* We don't use city-map iterators in this function because they may
519 * rely on the indices that have not yet been generated. Furthermore,
520 * we don't know the number of tiles within the city radius, so we need
521 * an temporary city_map_index array. Its content will be copied into
522 * the real array below. */
523 for (dx
= -CITY_MAP_MAX_RADIUS
; dx
<= CITY_MAP_MAX_RADIUS
; dx
++) {
524 for (dy
= -CITY_MAP_MAX_RADIUS
; dy
<= CITY_MAP_MAX_RADIUS
; dy
++) {
525 dist
= map_vector_to_sq_distance(dx
, dy
);
527 if (dist
<= CITY_MAP_MAX_RADIUS_SQ
) {
528 city_map_index_tmp
[city_count_tiles
].dx
= dx
;
529 city_map_index_tmp
[city_count_tiles
].dy
= dy
;
530 city_map_index_tmp
[city_count_tiles
].dist
= dist
;
532 for (i
= CITY_MAP_MAX_RADIUS_SQ
; i
>= 0; i
--) {
534 /* increase number of tiles within this squared city radius */
535 city_map_numtiles
[i
]++;
542 /* Initialise city_map_xy. -1 defines a invalid city map positions. */
543 city_map_xy
[CITY_REL2ABS(dx
)][CITY_REL2ABS(dy
)] = -1;
547 fc_assert(NULL
== city_map_index
);
548 city_map_index
= fc_malloc(city_count_tiles
* sizeof(*city_map_index
));
550 /* copy the index numbers from city_map_index_tmp into city_map_index */
551 for (i
= 0; i
< city_count_tiles
; i
++) {
552 city_map_index
[i
] = city_map_index_tmp
[i
];
555 qsort(city_map_index
, city_count_tiles
, sizeof(*city_map_index
),
558 /* set the static variable city_map_xy */
559 for (i
= 0; i
< city_count_tiles
; i
++) {
560 city_x
= CITY_REL2ABS(city_map_index
[i
].dx
);
561 city_y
= CITY_REL2ABS(city_map_index
[i
].dy
);
562 city_map_xy
[city_x
][city_y
] = i
;
566 citylog_map_radius_sq(LOG_DEBUG
);
567 citylog_map_index(LOG_DEBUG
);
569 for (i
= CITY_MAP_MIN_RADIUS_SQ
; i
<= CITY_MAP_MAX_RADIUS_SQ
; i
++) {
570 log_debug("radius_sq = %2d, tiles = %2d", i
, city_map_tiles(i
));
573 for (i
= 0; i
< city_count_tiles
; i
++) {
574 city_x
= CITY_REL2ABS(city_map_index
[i
].dx
);
575 city_y
= CITY_REL2ABS(city_map_index
[i
].dy
);
576 log_debug("[%2d]: (dx,dy) = (%+2d,%+2d), (x,y) = (%2d,%2d), "
577 "dist = %2d, check = %2d", i
,
578 city_map_index
[i
].dx
, city_map_index
[i
].dy
, city_x
, city_y
,
579 city_map_index
[i
].dist
, city_map_xy
[city_x
][city_y
]);
581 #endif /* FREECIV_DEBUG */
586 /****************************************************************************
587 Free memory allocated by generate_citymap_index
588 *****************************************************************************/
589 void free_city_map_index(void)
591 FC_FREE(city_map_index
);
594 /****************************************************************************
595 Return an id string for the output type. This string can be used
596 internally by rulesets and tilesets and should not be changed or
598 *****************************************************************************/
599 const char *get_output_identifier(Output_type_id output
)
601 fc_assert_ret_val(output
>= 0 && output
< O_LAST
, NULL
);
602 return output_types
[output
].id
;
605 /****************************************************************************
606 Return a translated name for the output type. This name should only be
607 used for user display.
608 *****************************************************************************/
609 const char *get_output_name(Output_type_id output
)
611 fc_assert_ret_val(output
>= 0 && output
< O_LAST
, NULL
);
612 return _(output_types
[output
].name
);
615 /****************************************************************************
616 Return the output type for this index.
617 ****************************************************************************/
618 struct output_type
*get_output_type(Output_type_id output
)
620 fc_assert_ret_val(output
>= 0 && output
< O_LAST
, NULL
);
621 return &output_types
[output
];
624 /**************************************************************************
625 Find the output type for this output identifier.
626 **************************************************************************/
627 Output_type_id
output_type_by_identifier(const char *id
)
631 for (o
= 0; o
< O_LAST
; o
++) {
632 if (fc_strcasecmp(output_types
[o
].name
, id
) == 0) {
640 /**************************************************************************
641 Return the extended name of the building.
642 **************************************************************************/
643 const char *city_improvement_name_translation(const struct city
*pcity
,
644 struct impr_type
*pimprove
)
646 static char buffer
[256];
647 const char *state
= NULL
;
649 if (is_great_wonder(pimprove
)) {
650 if (great_wonder_is_available(pimprove
)) {
651 state
= Q_("?wonder:W");
652 } else if (great_wonder_is_destroyed(pimprove
)) {
653 state
= Q_("?destroyed:D");
655 state
= Q_("?built:B");
659 struct player
*pplayer
= city_owner(pcity
);
661 if (improvement_obsolete(pplayer
, pimprove
, pcity
)) {
662 state
= Q_("?obsolete:O");
663 } else if (is_improvement_redundant(pcity
, pimprove
)) {
664 state
= Q_("?redundant:*");
669 fc_snprintf(buffer
, sizeof(buffer
), "%s(%s)",
670 improvement_name_translation(pimprove
), state
);
673 return improvement_name_translation(pimprove
);
677 /**************************************************************************
678 Return the extended name of the current production.
679 **************************************************************************/
680 const char *city_production_name_translation(const struct city
*pcity
)
682 static char buffer
[256];
684 switch (pcity
->production
.kind
) {
685 case VUT_IMPROVEMENT
:
686 return city_improvement_name_translation(pcity
, pcity
->production
.value
.building
);
691 return universal_name_translation(&pcity
->production
, buffer
, sizeof(buffer
));
694 /**************************************************************************
695 Return TRUE when the current production has this flag.
696 **************************************************************************/
697 bool city_production_has_flag(const struct city
*pcity
,
698 enum impr_flag_id flag
)
700 return VUT_IMPROVEMENT
== pcity
->production
.kind
701 && improvement_has_flag(pcity
->production
.value
.building
, flag
);
704 /**************************************************************************
705 Return the number of shields it takes to build current city production.
706 **************************************************************************/
707 int city_production_build_shield_cost(const struct city
*pcity
)
709 return universal_build_shield_cost(&pcity
->production
);
712 /**************************************************************************
713 Return TRUE if the city could use the additional build slots provided by
714 the effect City_Build_Slots. Within 'num_units' the total number of units
715 the city can build considering the current shield stock is returned.
716 **************************************************************************/
717 bool city_production_build_units(const struct city
*pcity
,
718 bool add_production
, int *num_units
)
720 struct unit_type
*utype
;
721 struct universal target
;
722 int build_slots
= city_build_slots(pcity
);
723 int shields_left
= pcity
->shield_stock
;
724 int unit_shield_cost
, i
;
726 fc_assert_ret_val(num_units
!= NULL
, FALSE
);
729 if (pcity
->production
.kind
!= VUT_UTYPE
) {
730 /* not a unit as the current production */
734 utype
= pcity
->production
.value
.utype
;
735 if (utype_pop_value(utype
) != 0 || utype_has_flag(utype
, UTYF_UNIQUE
)) {
736 /* unit with population cost or unique unit means that only one unit can
742 if (add_production
) {
743 shields_left
+= pcity
->prod
[O_SHIELD
];
746 unit_shield_cost
= utype_build_shield_cost(utype
);
748 for (i
= 0; i
< build_slots
; i
++) {
749 if (shields_left
< unit_shield_cost
) {
750 /* not enough shields */
755 shields_left
-= unit_shield_cost
;
757 if (worklist_length(&pcity
->worklist
) > i
) {
758 (void) worklist_peek_ith(&pcity
->worklist
, &target
, i
);
759 if (target
.kind
!= VUT_UTYPE
760 || utype_index(target
.value
.utype
) != utype_index(utype
)) {
761 /* stop if there is a build target in the worklist not equal to the
771 /**************************************************************************
772 Return the cost (gold) to buy the current city production.
773 **************************************************************************/
774 int city_production_buy_gold_cost(const struct city
*pcity
)
776 int build
= pcity
->shield_stock
;
778 switch (pcity
->production
.kind
) {
779 case VUT_IMPROVEMENT
:
780 return impr_buy_gold_cost(pcity
->production
.value
.building
,
783 return utype_buy_gold_cost(pcity
->production
.value
.utype
,
791 /**************************************************************************
792 Calculates the turns which are needed to build the requested
793 production in the city. GUI Independent.
794 **************************************************************************/
795 int city_production_turns_to_build(const struct city
*pcity
,
796 bool include_shield_stock
)
798 return city_turns_to_build(pcity
, &pcity
->production
, include_shield_stock
);
801 /**************************************************************************
802 Return whether given city can build given building, ignoring whether
804 **************************************************************************/
805 bool can_city_build_improvement_direct(const struct city
*pcity
,
806 struct impr_type
*pimprove
)
808 if (!can_player_build_improvement_direct(city_owner(pcity
), pimprove
)) {
812 if (city_has_building(pcity
, pimprove
)) {
816 return are_reqs_active(city_owner(pcity
), NULL
, pcity
, NULL
,
817 pcity
->tile
, NULL
, NULL
, NULL
, NULL
, NULL
,
818 &(pimprove
->reqs
), RPT_CERTAIN
);
821 /**************************************************************************
822 Return whether given city can build given building; returns FALSE if
823 the building is obsolete.
824 **************************************************************************/
825 bool can_city_build_improvement_now(const struct city
*pcity
,
826 struct impr_type
*pimprove
)
828 if (!can_city_build_improvement_direct(pcity
, pimprove
)) {
831 if (improvement_obsolete(city_owner(pcity
), pimprove
, pcity
)) {
838 /**************************************************************************
839 Return whether player can eventually build given building in the city;
840 returns FALSE if improvement can never possibly be built in this city.
841 **************************************************************************/
842 bool can_city_build_improvement_later(const struct city
*pcity
,
843 struct impr_type
*pimprove
)
845 /* Can the _player_ ever build this improvement? */
846 if (!can_player_build_improvement_later(city_owner(pcity
), pimprove
)) {
850 /* Check for requirements that aren't met and that are unchanging (so
851 * they can never be met). */
852 requirement_vector_iterate(&pimprove
->reqs
, preq
) {
853 if (is_req_unchanging(preq
)
854 && !is_req_active(city_owner(pcity
), NULL
, pcity
, NULL
,
855 pcity
->tile
, NULL
, NULL
, NULL
, NULL
, NULL
,
856 preq
, RPT_POSSIBLE
)) {
859 } requirement_vector_iterate_end
;
864 /**************************************************************************
865 Return whether given city can build given unit, ignoring whether unit
867 **************************************************************************/
868 bool can_city_build_unit_direct(const struct city
*pcity
,
869 const struct unit_type
*punittype
)
871 if (!can_player_build_unit_direct(city_owner(pcity
), punittype
)) {
875 /* Check to see if the unit has a building requirement. */
876 if (punittype
->need_improvement
877 && !city_has_building(pcity
, punittype
->need_improvement
)) {
881 /* You can't build naval units inland. */
882 if (!uclass_has_flag(utype_class(punittype
), UCF_BUILD_ANYWHERE
)
883 && !is_native_near_tile(&(wld
.map
), utype_class(punittype
),
888 if (punittype
->city_slots
> 0
889 && city_unit_slots_available(pcity
) < punittype
->city_slots
) {
896 /**************************************************************************
897 Return whether given city can build given unit; returns FALSE if unit is
899 **************************************************************************/
900 bool can_city_build_unit_now(const struct city
*pcity
,
901 const struct unit_type
*punittype
)
903 if (!can_city_build_unit_direct(pcity
, punittype
)) {
906 while ((punittype
= punittype
->obsoleted_by
) != U_NOT_OBSOLETED
) {
907 if (can_player_build_unit_direct(city_owner(pcity
), punittype
)) {
914 /**************************************************************************
915 Returns whether player can eventually build given unit in the city;
916 returns FALSE if unit can never possibly be built in this city.
917 **************************************************************************/
918 bool can_city_build_unit_later(const struct city
*pcity
,
919 const struct unit_type
*punittype
)
921 /* Can the _player_ ever build this unit? */
922 if (!can_player_build_unit_later(city_owner(pcity
), punittype
)) {
926 /* Some units can be built only in certain cities -- for instance,
927 ships may be built only in cities adjacent to ocean. */
928 if (!uclass_has_flag(utype_class(punittype
), UCF_BUILD_ANYWHERE
)
929 && !is_native_near_tile(&(wld
.map
), utype_class(punittype
),
937 /**************************************************************************
938 Returns whether city can immediately build given target,
939 unit or improvement. This considers obsolete targets still buildable.
940 **************************************************************************/
941 bool can_city_build_direct(const struct city
*pcity
,
942 const struct universal
*target
)
944 switch (target
->kind
) {
946 return can_city_build_unit_direct(pcity
, target
->value
.utype
);
947 case VUT_IMPROVEMENT
:
948 return can_city_build_improvement_direct(pcity
, target
->value
.building
);
955 /**************************************************************************
956 Returns whether city can immediately build given target,
957 unit or improvement. This considers obsolete targets no longer buildable.
958 **************************************************************************/
959 bool can_city_build_now(const struct city
*pcity
,
960 const struct universal
*target
)
962 switch (target
->kind
) {
964 return can_city_build_unit_now(pcity
, target
->value
.utype
);
965 case VUT_IMPROVEMENT
:
966 return can_city_build_improvement_now(pcity
, target
->value
.building
);
973 /**************************************************************************
974 Returns whether city can ever build given target, unit or improvement.
975 **************************************************************************/
976 bool can_city_build_later(const struct city
*pcity
,
977 const struct universal
*target
)
979 switch (target
->kind
) {
981 return can_city_build_unit_later(pcity
, target
->value
.utype
);
982 case VUT_IMPROVEMENT
:
983 return can_city_build_improvement_later(pcity
, target
->value
.building
);
990 /****************************************************************************
991 Return number of free unit slots in a city.
992 ****************************************************************************/
993 int city_unit_slots_available(const struct city
*pcity
)
995 int max
= get_city_bonus(pcity
, EFT_UNIT_SLOTS
);
999 unit_list_iterate(pcity
->units_supported
, punit
) {
1000 current
+= unit_type_get(punit
)->city_slots
;
1001 } unit_list_iterate_end
;
1003 return max
- current
;
1006 /****************************************************************************
1007 Returns TRUE iff the given city can use this kind of specialist.
1008 ****************************************************************************/
1009 bool city_can_use_specialist(const struct city
*pcity
,
1010 Specialist_type_id type
)
1012 return are_reqs_active(city_owner(pcity
), NULL
, pcity
, NULL
,
1013 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
1014 &specialist_by_number(type
)->reqs
, RPT_POSSIBLE
);
1017 /****************************************************************************
1018 Returns TRUE iff the given city can change what it is building
1019 ****************************************************************************/
1020 bool city_can_change_build(const struct city
*pcity
)
1022 return !pcity
->did_buy
|| pcity
->shield_stock
<= 0;
1025 /**************************************************************************
1026 Always tile_set_owner(ptile, pplayer) sometime before this!
1027 **************************************************************************/
1028 void city_choose_build_default(struct city
*pcity
)
1030 if (NULL
== city_tile(pcity
)) {
1031 /* When a "dummy" city is created with no tile, then choosing a build
1032 * target could fail. This currently might happen during map editing.
1033 * FIXME: assumes the first unit is always "valid", so check for
1034 * obsolete units elsewhere. */
1035 pcity
->production
.kind
= VUT_UTYPE
;
1036 pcity
->production
.value
.utype
= utype_by_number(0);
1038 struct unit_type
*u
= best_role_unit(pcity
, L_FIRSTBUILD
);
1041 pcity
->production
.kind
= VUT_UTYPE
;
1042 pcity
->production
.value
.utype
= u
;
1046 /* Just pick the first available item. */
1048 improvement_iterate(pimprove
) {
1049 if (can_city_build_improvement_direct(pcity
, pimprove
)) {
1051 pcity
->production
.kind
= VUT_IMPROVEMENT
;
1052 pcity
->production
.value
.building
= pimprove
;
1055 } improvement_iterate_end
;
1058 unit_type_iterate(punittype
) {
1059 if (can_city_build_unit_direct(pcity
, punittype
)) {
1061 pcity
->production
.kind
= VUT_UTYPE
;
1062 pcity
->production
.value
.utype
= punittype
;
1064 } unit_type_iterate_end
;
1067 fc_assert_msg(found
, "No production found for city %s!",
1068 city_name_get(pcity
));
1073 #ifndef city_name_get
1074 /**************************************************************************
1075 Return the name of the city.
1076 **************************************************************************/
1077 const char *city_name_get(const struct city
*pcity
)
1081 #endif /* city_name_get */
1083 /**************************************************************************
1084 Return the owner of the city.
1085 **************************************************************************/
1086 struct player
*city_owner(const struct city
*pcity
)
1088 fc_assert_ret_val(NULL
!= pcity
, NULL
);
1089 fc_assert(NULL
!= pcity
->owner
);
1090 return pcity
->owner
;
1094 /**************************************************************************
1095 Return the tile location of the city.
1096 Not (yet) always used, mostly for debugging.
1097 **************************************************************************/
1098 struct tile
*city_tile(const struct city
*pcity
)
1105 /*****************************************************************************
1107 *****************************************************************************/
1108 citizens
city_size_get(const struct city
*pcity
)
1110 fc_assert_ret_val(pcity
!= NULL
, 0);
1115 /*****************************************************************************
1116 Add a (positive or negative) value to the city size. As citizens is an
1117 unsigned value use int for the parameter 'add'.
1118 *****************************************************************************/
1119 void city_size_add(struct city
*pcity
, int add
)
1121 citizens size
= city_size_get(pcity
);
1123 fc_assert_ret(pcity
!= NULL
);
1124 fc_assert_ret(MAX_CITY_SIZE
- size
> add
);
1125 fc_assert_ret(size
>= -add
);
1127 city_size_set(pcity
, city_size_get(pcity
) + add
);
1130 /*****************************************************************************
1132 *****************************************************************************/
1133 void city_size_set(struct city
*pcity
, citizens size
)
1135 fc_assert_ret(pcity
!= NULL
);
1137 /* Set city size. */
1141 /**************************************************************************
1142 Returns how many thousand citizen live in this city.
1143 **************************************************************************/
1144 int city_population(const struct city
*pcity
)
1146 /* Sum_{i=1}^{n} i == n*(n+1)/2 */
1147 return city_size_get(pcity
) * (city_size_get(pcity
) + 1) * 5;
1150 /**************************************************************************
1151 Returns the total amount of gold needed to pay for all buildings in the
1153 **************************************************************************/
1154 int city_total_impr_gold_upkeep(const struct city
*pcity
)
1156 int gold_needed
= 0;
1162 city_built_iterate(pcity
, pimprove
) {
1163 gold_needed
+= city_improvement_upkeep(pcity
, pimprove
);
1164 } city_built_iterate_end
;
1169 /***************************************************************************
1170 Get the total amount of gold needed to pay upkeep costs for all supported
1171 units of the city. Takes into account EFT_UNIT_UPKEEP_FREE_PER_CITY.
1172 ***************************************************************************/
1173 int city_total_unit_gold_upkeep(const struct city
*pcity
)
1175 int gold_needed
= 0;
1177 if (!pcity
|| !pcity
->units_supported
1178 || unit_list_size(pcity
->units_supported
) < 1) {
1182 unit_list_iterate(pcity
->units_supported
, punit
) {
1183 gold_needed
+= punit
->upkeep
[O_GOLD
];
1184 } unit_list_iterate_end
;
1189 /**************************************************************************
1190 Return TRUE iff the city has this building in it.
1191 **************************************************************************/
1192 bool city_has_building(const struct city
*pcity
,
1193 const struct impr_type
*pimprove
)
1195 if (NULL
== pimprove
) {
1196 /* callers should ensure that any external data is tested with
1197 * valid_improvement_by_number() */
1200 return (pcity
->built
[improvement_index(pimprove
)].turn
> I_NEVER
);
1203 /**************************************************************************
1204 Return the upkeep (gold) needed each turn to upkeep the given improvement
1206 **************************************************************************/
1207 int city_improvement_upkeep(const struct city
*pcity
,
1208 const struct impr_type
*b
)
1218 if (upkeep
<= get_building_bonus(pcity
, b
, EFT_UPKEEP_FREE
)) {
1225 /**************************************************************************
1226 Calculate the output for the tile.
1228 is_celebrating may be speculative.
1229 otype is the output type (generally O_FOOD, O_TRADE, or O_SHIELD).
1231 This can be used to calculate the benefits celebration would give.
1232 **************************************************************************/
1233 int city_tile_output(const struct city
*pcity
, const struct tile
*ptile
,
1234 bool is_celebrating
, Output_type_id otype
)
1237 struct terrain
*pterrain
= tile_terrain(ptile
);
1238 const struct output_type
*output
= &output_types
[otype
];
1239 struct player
*pplayer
= NULL
;
1241 fc_assert_ret_val(otype
>= 0 && otype
< O_LAST
, 0);
1243 if (T_UNKNOWN
== pterrain
) {
1244 /* Special case for the client. The server doesn't allow unknown tiles
1245 * to be worked but we don't necessarily know what player is involved. */
1249 prod
= pterrain
->output
[otype
];
1250 if (tile_resource_is_valid(ptile
)) {
1251 prod
+= tile_resource(ptile
)->data
.resource
->output
[otype
];
1254 if (pcity
!= NULL
) {
1255 pplayer
= city_owner(pcity
);
1260 if (pterrain
->mining_shield_incr
!= 0) {
1261 prod
+= pterrain
->mining_shield_incr
1262 * get_target_bonus_effects(NULL
, pplayer
, NULL
, pcity
, NULL
,
1263 ptile
, NULL
, NULL
, NULL
, NULL
, NULL
,
1269 if (pterrain
->irrigation_food_incr
!= 0) {
1270 prod
+= pterrain
->irrigation_food_incr
1271 * get_target_bonus_effects(NULL
, pplayer
, NULL
, pcity
, NULL
,
1272 ptile
, NULL
, NULL
, NULL
, NULL
, NULL
,
1285 prod
+= tile_roads_output_incr(ptile
, otype
);
1286 prod
+= (prod
* tile_roads_output_bonus(ptile
, otype
) / 100);
1289 prod
+= get_city_tile_output_bonus(pcity
, ptile
, output
,
1290 EFT_OUTPUT_ADD_TILE
);
1292 int penalty_limit
= get_city_tile_output_bonus(pcity
, ptile
, output
,
1293 EFT_OUTPUT_PENALTY_TILE
);
1295 if (is_celebrating
) {
1296 prod
+= get_city_tile_output_bonus(pcity
, ptile
, output
,
1297 EFT_OUTPUT_INC_TILE_CELEBRATE
);
1298 penalty_limit
= 0; /* no penalty if celebrating */
1300 prod
+= get_city_tile_output_bonus(pcity
, ptile
, output
,
1301 EFT_OUTPUT_INC_TILE
);
1303 * get_city_tile_output_bonus(pcity
, ptile
, output
,
1304 EFT_OUTPUT_PER_TILE
))
1306 if (!is_celebrating
&& penalty_limit
> 0 && prod
> penalty_limit
) {
1313 * get_target_bonus_effects(NULL
, pplayer
, NULL
, pcity
, NULL
,
1314 ptile
, NULL
, NULL
, output
, NULL
, NULL
,
1315 EFT_OUTPUT_TILE_PUNISH_PCT
))
1318 if (NULL
!= pcity
&& is_city_center(pcity
, ptile
)) {
1319 prod
= MAX(prod
, game
.info
.min_city_center_output
[otype
]);
1325 /**************************************************************************
1326 Calculate the production output the given tile is capable of producing
1327 for the city. The output type is given by 'otype' (generally O_FOOD,
1328 O_SHIELD, or O_TRADE).
1329 **************************************************************************/
1330 int city_tile_output_now(const struct city
*pcity
, const struct tile
*ptile
,
1331 Output_type_id otype
)
1333 return city_tile_output(pcity
, ptile
, city_celebrating(pcity
), otype
);
1336 /****************************************************************************
1337 Returns TRUE when a tile is available to be worked, or the city itself is
1338 currently working the tile (and can continue).
1340 The paramter 'restriction', which is usually client_player(), allow a
1341 player to handle with its real knownledge to guess it the work of this
1344 This function shouldn't be called directly, but with city_can_work_tile()
1345 (common/city.[ch]) or client_city_can_work_tile() (client/climap.[ch]).
1346 ****************************************************************************/
1347 bool base_city_can_work_tile(const struct player
*restriction
,
1348 const struct city
*pcity
,
1349 const struct tile
*ptile
)
1351 struct player
*powner
= city_owner(pcity
);
1352 int city_map_x
, city_map_y
;
1354 if (NULL
== ptile
) {
1358 if (!city_base_to_city_map(&city_map_x
, &city_map_y
, pcity
, ptile
)) {
1362 if (NULL
!= restriction
1363 && TILE_UNKNOWN
== tile_get_known(ptile
, restriction
)) {
1367 if (NULL
!= tile_owner(ptile
) && tile_owner(ptile
) != powner
) {
1370 /* TODO: civ3-like option for borders */
1372 if (NULL
!= tile_worked(ptile
) && tile_worked(ptile
) != pcity
) {
1376 if (powner
== restriction
1377 && TILE_KNOWN_SEEN
!= tile_get_known(ptile
, powner
)) {
1381 if (!is_free_worked(pcity
, ptile
)
1382 && NULL
!= unit_occupies_tile(ptile
, powner
)) {
1386 if (get_city_tile_output_bonus(pcity
, ptile
, NULL
, EFT_TILE_WORKABLE
) <= 0) {
1393 /**************************************************************************
1394 Returns TRUE when a tile is available to be worked, or the city itself is
1395 currently working the tile (and can continue).
1397 See also client_city_can_work_tile() (client/climap.[ch]).
1398 **************************************************************************/
1399 bool city_can_work_tile(const struct city
*pcity
, const struct tile
*ptile
)
1401 return base_city_can_work_tile(city_owner(pcity
), pcity
, ptile
);
1404 /****************************************************************************
1405 Returns TRUE if the given unit can build a city at the given map
1408 punit is the founding unit. It may be NULL if a city is built out of the
1409 blue (e.g., through editing).
1410 ****************************************************************************/
1411 bool city_can_be_built_here(const struct tile
*ptile
,
1412 const struct unit
*punit
)
1414 return (CB_OK
== city_build_here_test(ptile
, punit
));
1417 /**************************************************************************
1418 Return TRUE iff the ruleset allows founding a city at a tile claimed by
1421 Since a local DiplRel requirement can be against something else than a
1422 tile an unclaimed tile can't always contradict a local DiplRel
1423 requirement. With knowledge about what entities each requirement in a
1424 requirement vector is evaluated against a contradiction can be
1427 TODO: Get rid of this together with CB_BAD_BORDERS.
1428 Maybe get rid of it before if the problem above is solved.
1429 **************************************************************************/
1430 static bool city_on_foreign_tile_is_legal(struct unit_type
*punit_type
)
1432 struct requirement tile_is_claimed
;
1433 struct requirement tile_is_foreign
;
1435 if (!utype_may_act_at_all(punit_type
)) {
1436 /* Not an actor unit type. */
1440 /* Tile is claimed as a requirement. */
1441 tile_is_claimed
.range
= REQ_RANGE_LOCAL
;
1442 tile_is_claimed
.survives
= FALSE
;
1443 tile_is_claimed
.source
.kind
= VUT_CITYTILE
;
1444 tile_is_claimed
.present
= TRUE
;
1445 tile_is_claimed
.source
.value
.citytile
= CITYT_CLAIMED
;
1447 /* Tile is foreign as a requirement. */
1448 tile_is_foreign
.range
= REQ_RANGE_LOCAL
;
1449 tile_is_foreign
.survives
= FALSE
;
1450 tile_is_foreign
.source
.kind
= VUT_DIPLREL
;
1451 tile_is_foreign
.present
= TRUE
;
1452 tile_is_foreign
.source
.value
.diplrel
= DRO_FOREIGN
;
1454 action_enabler_list_iterate(
1455 action_enablers_for_action(ACTION_FOUND_CITY
), enabler
) {
1456 if (!requirement_fulfilled_by_unit_type(punit_type
,
1457 &(enabler
->actor_reqs
))) {
1458 /* This action enabler isn't for this unit type at all. */
1462 if (!(does_req_contradicts_reqs(&tile_is_claimed
,
1463 &(enabler
->target_reqs
))
1464 || does_req_contradicts_reqs(&tile_is_foreign
,
1465 &(enabler
->actor_reqs
)))) {
1466 /* This ruleset permits city founding on foreign tiles. */
1469 } action_enabler_list_iterate_end
;
1471 /* This ruleset forbids city founding on foreign tiles. */
1475 /****************************************************************************
1476 Returns CB_OK if the given unit can build a city at the given map
1477 coordinates. Else, returns the reason of the failure.
1479 punit is the founding unit. It may be NULL if a city is built out of the
1480 blue (e.g., through editing).
1481 ****************************************************************************/
1482 enum city_build_result
city_build_here_test(const struct tile
*ptile
,
1483 const struct unit
*punit
)
1487 if (terrain_has_flag(tile_terrain(ptile
), TER_NO_CITIES
)) {
1488 /* No cities on this terrain. */
1489 return CB_BAD_CITY_TERRAIN
;
1492 if (punit
&& !can_unit_exist_at_tile(&(wld
.map
), punit
, ptile
)
1493 /* TODO: remove CB_BAD_UNIT_TERRAIN and CB_BAD_UNIT_TERRAIN when it
1494 * can be done without regressions. */
1495 /* The ruleset may allow founding cities on non native terrain. */
1496 && !utype_can_do_act_when_ustate(unit_type_get(punit
), ACTION_FOUND_CITY
,
1497 USP_LIVABLE_TILE
, FALSE
)) {
1498 /* Many rulesets allow land units to build land cities and sea units to
1499 * build ocean cities. Air units can build cities anywhere. */
1500 return CB_BAD_UNIT_TERRAIN
;
1503 if (punit
&& tile_owner(ptile
) && tile_owner(ptile
) != unit_owner(punit
)
1504 /* TODO: remove CB_BAD_BORDERS when it can be done without
1506 /* The ruleset may allow founding cities on foreign terrain. */
1507 && !city_on_foreign_tile_is_legal(unit_type_get(punit
))) {
1508 /* Cannot steal borders by settling. This has to be settled by
1510 return CB_BAD_BORDERS
;
1513 /* citymindist minimum is 1, meaning adjacent is okay */
1514 citymindist
= game
.info
.citymindist
;
1515 square_iterate(&(wld
.map
), ptile
, citymindist
- 1, ptile1
) {
1516 if (tile_city(ptile1
)) {
1517 return CB_NO_MIN_DIST
;
1519 } square_iterate_end
;
1524 /**************************************************************************
1525 Return TRUE iff this city is its nation's capital. The capital city is
1526 special-cased in a number of ways.
1527 **************************************************************************/
1528 bool is_capital(const struct city
*pcity
)
1530 return (get_city_bonus(pcity
, EFT_CAPITAL_CITY
) > 0);
1533 /**************************************************************************
1534 Return TRUE iff this city is governmental center.
1535 **************************************************************************/
1536 bool is_gov_center(const struct city
*pcity
)
1538 return (get_city_bonus(pcity
, EFT_GOV_CENTER
) > 0);
1541 /**************************************************************************
1542 This can be City Walls, Coastal defense... depending on attacker type.
1543 If attacker type is not given, just any defense effect will do.
1544 **************************************************************************/
1545 bool city_got_defense_effect(const struct city
*pcity
,
1546 const struct unit_type
*attacker
)
1549 /* Any defense building will do */
1550 return get_city_bonus(pcity
, EFT_DEFEND_BONUS
) > 0;
1553 return get_unittype_bonus(city_owner(pcity
), pcity
->tile
, attacker
,
1554 EFT_DEFEND_BONUS
) > 0;
1557 /**************************************************************************
1558 Return TRUE iff the city is happy. A happy city will start celebrating
1560 A city can only be happy if half or more of the population is happy,
1561 none of the population is unhappy or angry, and it has sufficient size.
1562 **************************************************************************/
1563 bool city_happy(const struct city
*pcity
)
1565 return (city_size_get(pcity
) >= game
.info
.celebratesize
1566 && pcity
->feel
[CITIZEN_ANGRY
][FEELING_FINAL
] == 0
1567 && pcity
->feel
[CITIZEN_UNHAPPY
][FEELING_FINAL
] == 0
1568 && pcity
->feel
[CITIZEN_HAPPY
][FEELING_FINAL
] >= (city_size_get(pcity
) + 1) / 2);
1571 /**************************************************************************
1572 Return TRUE iff the city is unhappy. An unhappy city will fall
1574 **************************************************************************/
1575 bool city_unhappy(const struct city
*pcity
)
1577 return (pcity
->feel
[CITIZEN_HAPPY
][FEELING_FINAL
]
1578 < pcity
->feel
[CITIZEN_UNHAPPY
][FEELING_FINAL
]
1579 + 2 * pcity
->feel
[CITIZEN_ANGRY
][FEELING_FINAL
]);
1582 /**************************************************************************
1583 Return TRUE if the city was celebrating at the start of the turn,
1584 and it still has sufficient size to be in rapture.
1585 **************************************************************************/
1586 bool base_city_celebrating(const struct city
*pcity
)
1588 return (city_size_get(pcity
) >= game
.info
.celebratesize
&& pcity
->was_happy
);
1591 /**************************************************************************
1592 cities celebrate only after consecutive happy turns
1593 **************************************************************************/
1594 bool city_celebrating(const struct city
*pcity
)
1596 return base_city_celebrating(pcity
) && city_happy(pcity
);
1599 /**************************************************************************
1600 Returns whether city is growing by rapture.
1601 **************************************************************************/
1602 bool city_rapture_grow(const struct city
*pcity
)
1604 /* .rapture is checked instead of city_celebrating() because this
1605 function is called after .was_happy was updated. */
1606 return (pcity
->rapture
> 0 && pcity
->surplus
[O_FOOD
] > 0
1607 && (pcity
->rapture
% game
.info
.rapturedelay
) == 0
1608 && get_city_bonus(pcity
, EFT_RAPTURE_GROW
) > 0);
1611 /**************************************************************************
1612 Returns TRUE iff the city is occupied.
1613 **************************************************************************/
1614 bool city_is_occupied(const struct city
*pcity
)
1617 /* The server sees the units inside the city. */
1618 return (unit_list_size(city_tile(pcity
)->units
) > 0);
1620 /* The client gets the occupied property from the server. */
1621 return pcity
->client
.occupied
;
1625 /**************************************************************************
1626 Find city with given id from list.
1627 **************************************************************************/
1628 struct city
*city_list_find_number(struct city_list
*This
, int id
)
1631 city_list_iterate(This
, pcity
) {
1632 if (pcity
->id
== id
) {
1635 } city_list_iterate_end
;
1641 /**************************************************************************
1642 Find city with given name from list.
1643 **************************************************************************/
1644 struct city
*city_list_find_name(struct city_list
*This
, const char *name
)
1646 city_list_iterate(This
, pcity
) {
1647 if (fc_strcasecmp(name
, pcity
->name
) == 0) {
1650 } city_list_iterate_end
;
1655 /**************************************************************************
1656 Comparison function for qsort for city _pointers_, sorting by city name.
1657 Args are really (struct city**), to sort an array of pointers.
1658 (Compare with old_city_name_compare() in game.c, which use city_id's)
1659 **************************************************************************/
1660 int city_name_compare(const void *p1
, const void *p2
)
1662 return fc_strcasecmp((*(const struct city
**) p1
)->name
,
1663 (*(const struct city
**) p2
)->name
);
1666 /****************************************************************************
1667 Returns the city style that has the given (translated) name.
1668 Returns -1 if none match.
1669 ****************************************************************************/
1670 int city_style_by_translated_name(const char *s
)
1674 for (i
= 0; i
< game
.control
.styles_count
; i
++) {
1675 if (0 == strcmp(city_style_name_translation(i
), s
)) {
1683 /****************************************************************************
1684 Returns the city style that has the given (untranslated) rule name.
1685 Returns -1 if none match.
1686 ****************************************************************************/
1687 int city_style_by_rule_name(const char *s
)
1689 const char *qs
= Qn_(s
);
1692 for (i
= 0; i
< game
.control
.styles_count
; i
++) {
1693 if (0 == fc_strcasecmp(city_style_rule_name(i
), qs
)) {
1701 /****************************************************************************
1702 Return the (translated) name of the given city style.
1703 You don't have to free the return pointer.
1704 ****************************************************************************/
1705 const char *city_style_name_translation(const int style
)
1707 return name_translation_get(&city_styles
[style
].name
);
1710 /****************************************************************************
1711 Return the (untranslated) rule name of the city style.
1712 You don't have to free the return pointer.
1713 ****************************************************************************/
1714 const char *city_style_rule_name(const int style
)
1716 return rule_name_get(&city_styles
[style
].name
);
1719 /* Cache of what city production caravan shields are allowed to help. */
1720 static bv_imprs caravan_helped_impr
;
1721 static bv_unit_types caravan_helped_utype
;
1723 /**************************************************************************
1724 Initialize the cache of what city production can use shields from
1726 **************************************************************************/
1727 void city_production_caravan_shields_init(void)
1729 struct requirement prod_as_req
;
1731 /* Remove old data. */
1732 BV_CLR_ALL(caravan_helped_impr
);
1733 BV_CLR_ALL(caravan_helped_utype
);
1735 /* Common for all production kinds. */
1736 prod_as_req
.range
= REQ_RANGE_LOCAL
;
1737 prod_as_req
.survives
= FALSE
;
1738 prod_as_req
.present
= TRUE
;
1740 /* Check improvements */
1741 prod_as_req
.source
.kind
= VUT_IMPROVEMENT
;
1743 improvement_iterate(itype
) {
1744 if (!is_wonder(itype
)) {
1745 /* Only wonders can currently use caravan shields. Next! */
1749 /* Check this improvement. */
1750 prod_as_req
.source
.value
.building
= itype
;
1752 action_enabler_list_iterate(action_enablers_for_action(
1753 ACTION_HELP_WONDER
),
1755 if (!does_req_contradicts_reqs(&prod_as_req
,
1756 &(enabler
->target_reqs
))) {
1757 /* This improvement kind can receive caravan shields. */
1759 BV_SET(caravan_helped_impr
, improvement_index(itype
));
1761 /* Move on to the next improvment */
1764 } action_enabler_list_iterate_end
;
1765 } improvement_iterate_end
;
1767 /* Units can't currently use caravan shields. */
1770 /**************************************************************************
1771 Returns TRUE iff the specified production should get shields from
1772 units that has done ACTION_HELP_WONDER.
1773 **************************************************************************/
1774 bool city_production_gets_caravan_shields(const struct universal
*tgt
)
1776 switch (tgt
->kind
) {
1777 case VUT_IMPROVEMENT
:
1778 return BV_ISSET(caravan_helped_impr
,
1779 improvement_index(tgt
->value
.building
));
1781 return BV_ISSET(caravan_helped_utype
,
1782 utype_index(tgt
->value
.utype
));
1789 /**************************************************************************
1790 Compute and optionally apply the change-production penalty for the given
1791 production change (to target) in the given city (pcity).
1792 Always returns the number of shields which would be in the stock if
1793 the penalty had been applied.
1795 If we switch the "class" of the target sometime after a city has produced
1796 (i.e., not on the turn immediately after), then there's a shield loss.
1797 But only on the first switch that turn. Also, if ever change back to
1798 original improvement class of this turn, restore lost production.
1799 **************************************************************************/
1800 int city_change_production_penalty(const struct city
*pcity
,
1801 const struct universal
*target
)
1803 int shield_stock_after_adjustment
;
1804 enum production_class_type orig_class
;
1805 enum production_class_type new_class
;
1806 int unpenalized_shields
= 0, penalized_shields
= 0;
1808 switch (pcity
->changed_from
.kind
) {
1809 case VUT_IMPROVEMENT
:
1810 if (is_wonder(pcity
->changed_from
.value
.building
)) {
1811 orig_class
= PCT_WONDER
;
1813 orig_class
= PCT_NORMAL_IMPROVEMENT
;
1817 orig_class
= PCT_UNIT
;
1820 orig_class
= PCT_LAST
;
1824 switch (target
->kind
) {
1825 case VUT_IMPROVEMENT
:
1826 if (is_wonder(target
->value
.building
)) {
1827 new_class
= PCT_WONDER
;
1829 new_class
= PCT_NORMAL_IMPROVEMENT
;
1833 new_class
= PCT_UNIT
;
1836 new_class
= PCT_LAST
;
1840 /* Changing production is penalized under certain circumstances. */
1841 if (orig_class
== new_class
1842 || orig_class
== PCT_LAST
) {
1843 /* There's never a penalty for building something of the same class. */
1844 unpenalized_shields
= pcity
->before_change_shields
;
1845 } else if (city_built_last_turn(pcity
)) {
1846 /* Surplus shields from the previous production won't be penalized if
1847 * you change production on the very next turn. But you can only use
1848 * up to the city's surplus amount of shields in this way. */
1849 unpenalized_shields
= MIN(pcity
->last_turns_shield_surplus
,
1850 pcity
->before_change_shields
);
1851 penalized_shields
= pcity
->before_change_shields
- unpenalized_shields
;
1853 /* Penalize 50% of the production. */
1854 penalized_shields
= pcity
->before_change_shields
;
1857 /* Do not put penalty on these. It shouldn't matter whether you disband unit
1858 before or after changing production...*/
1859 unpenalized_shields
+= pcity
->disbanded_shields
;
1861 /* Caravan shields are penalized (just as if you disbanded the caravan)
1862 * if you're not building a wonder. */
1863 if (city_production_gets_caravan_shields(target
)) {
1864 unpenalized_shields
+= pcity
->caravan_shields
;
1866 penalized_shields
+= pcity
->caravan_shields
;
1869 shield_stock_after_adjustment
=
1870 unpenalized_shields
+ penalized_shields
/ 2;
1872 return shield_stock_after_adjustment
;
1875 /**************************************************************************
1876 Calculates the turns which are needed to build the requested
1877 improvement in the city. GUI Independent.
1878 **************************************************************************/
1879 int city_turns_to_build(const struct city
*pcity
,
1880 const struct universal
*target
,
1881 bool include_shield_stock
)
1883 int city_shield_surplus
= pcity
->surplus
[O_SHIELD
];
1884 int city_shield_stock
= include_shield_stock
?
1885 city_change_production_penalty(pcity
, target
) : 0;
1886 int cost
= universal_build_shield_cost(target
);
1888 if (target
->kind
== VUT_IMPROVEMENT
1889 && is_great_wonder(target
->value
.building
)
1890 && !great_wonder_is_available(target
->value
.building
)) {
1894 if (include_shield_stock
&& (city_shield_stock
>= cost
)) {
1896 } else if (city_shield_surplus
> 0) {
1897 return (cost
- city_shield_stock
- 1) / city_shield_surplus
+ 1;
1903 /**************************************************************************
1904 Calculates the turns which are needed for the city to grow. A value
1905 of FC_INFINITY means the city will never grow. A value of 0 means
1906 city growth is blocked. A negative value of -x means the city will
1907 shrink in x turns. A positive value of x means the city will grow in
1909 **************************************************************************/
1910 int city_turns_to_grow(const struct city
*pcity
)
1912 if (pcity
->surplus
[O_FOOD
] > 0) {
1913 return (city_granary_size(city_size_get(pcity
)) - pcity
->food_stock
+
1914 pcity
->surplus
[O_FOOD
] - 1) / pcity
->surplus
[O_FOOD
];
1915 } else if (pcity
->surplus
[O_FOOD
] < 0) {
1916 /* turns before famine loss */
1917 return -1 + (pcity
->food_stock
/ pcity
->surplus
[O_FOOD
]);
1923 /****************************************************************************
1924 Return TRUE iff the city can grow to the given size.
1925 ****************************************************************************/
1926 bool city_can_grow_to(const struct city
*pcity
, int pop_size
)
1928 return (get_city_bonus(pcity
, EFT_SIZE_UNLIMIT
) > 0
1929 || pop_size
<= get_city_bonus(pcity
, EFT_SIZE_ADJ
));
1932 /**************************************************************************
1933 is there an enemy city on this tile?
1934 **************************************************************************/
1935 struct city
*is_enemy_city_tile(const struct tile
*ptile
,
1936 const struct player
*pplayer
)
1938 struct city
*pcity
= tile_city(ptile
);
1940 if (pcity
&& pplayers_at_war(pplayer
, city_owner(pcity
)))
1946 /**************************************************************************
1947 is there an friendly city on this tile?
1948 **************************************************************************/
1949 struct city
*is_allied_city_tile(const struct tile
*ptile
,
1950 const struct player
*pplayer
)
1952 struct city
*pcity
= tile_city(ptile
);
1954 if (pcity
&& pplayers_allied(pplayer
, city_owner(pcity
)))
1960 /**************************************************************************
1961 is there an enemy city on this tile?
1962 **************************************************************************/
1963 struct city
*is_non_attack_city_tile(const struct tile
*ptile
,
1964 const struct player
*pplayer
)
1966 struct city
*pcity
= tile_city(ptile
);
1968 if (pcity
&& pplayers_non_attack(pplayer
, city_owner(pcity
)))
1974 /**************************************************************************
1975 is there an non_allied city on this tile?
1976 **************************************************************************/
1977 struct city
*is_non_allied_city_tile(const struct tile
*ptile
,
1978 const struct player
*pplayer
)
1980 struct city
*pcity
= tile_city(ptile
);
1982 if (pcity
&& !pplayers_allied(pplayer
, city_owner(pcity
)))
1988 /**************************************************************************
1989 Return TRUE if there is a friendly city near to this unit (within 3
1991 **************************************************************************/
1992 bool is_unit_near_a_friendly_city(const struct unit
*punit
)
1994 return is_friendly_city_near(unit_owner(punit
), unit_tile(punit
));
1997 /**************************************************************************
1998 Return TRUE if there is a friendly city near to this tile (within 3
2000 **************************************************************************/
2001 bool is_friendly_city_near(const struct player
*owner
,
2002 const struct tile
*ptile
)
2004 square_iterate(&(wld
.map
), ptile
, 3, ptile1
) {
2005 struct city
*pcity
= tile_city(ptile1
);
2006 if (pcity
&& pplayers_allied(owner
, city_owner(pcity
))) {
2009 } square_iterate_end
;
2014 /**************************************************************************
2015 Return true iff a city exists within a city radius of the given
2016 location. may_be_on_center determines if a city at x,y counts.
2017 **************************************************************************/
2018 bool city_exists_within_max_city_map(const struct tile
*ptile
,
2019 bool may_be_on_center
)
2021 city_tile_iterate(CITY_MAP_MAX_RADIUS_SQ
, ptile
, ptile1
) {
2022 if (may_be_on_center
|| !same_pos(ptile
, ptile1
)) {
2023 if (tile_city(ptile1
)) {
2027 } city_tile_iterate_end
;
2032 /****************************************************************************
2033 Generalized formula used to calculate granary size.
2035 The AI may not deal well with non-default settings. See food_weighting().
2036 ****************************************************************************/
2037 int city_granary_size(int city_size
)
2039 int food_inis
= game
.info
.granary_num_inis
;
2040 int food_inc
= game
.info
.granary_food_inc
;
2043 /* If the city has no citizens, there is no granary. */
2044 if (city_size
== 0) {
2048 /* Granary sizes for the first food_inis citizens are given directly.
2049 * After that we increase the granary size by food_inc per citizen. */
2050 if (city_size
> food_inis
) {
2051 base_value
= game
.info
.granary_food_ini
[food_inis
- 1];
2052 base_value
+= food_inc
* (city_size
- food_inis
);
2054 base_value
= game
.info
.granary_food_ini
[city_size
- 1];
2057 return MAX(base_value
* game
.info
.foodbox
/ 100, 1);
2060 /****************************************************************************
2061 Give base happiness in any city owned by pplayer.
2062 A positive number is a number of content citizens. A negative number is
2063 a number of angry citizens (a city never starts with both).
2064 ****************************************************************************/
2065 static int player_base_citizen_happiness(const struct player
*pplayer
)
2067 int cities
= city_list_size(pplayer
->cities
);
2068 int content
= get_player_bonus(pplayer
, EFT_CITY_UNHAPPY_SIZE
);
2069 int basis
= get_player_bonus(pplayer
, EFT_EMPIRE_SIZE_BASE
);
2070 int step
= get_player_bonus(pplayer
, EFT_EMPIRE_SIZE_STEP
);
2072 if (basis
+ step
<= 0) {
2073 /* Value of zero means effect is inactive */
2077 if (cities
> basis
) {
2080 /* the first penalty is at (basis + 1) cities;
2081 the next is at (basis + step + 1), _not_ (basis + step) */
2082 content
-= (cities
- basis
- 1) / step
;
2088 /****************************************************************************
2089 Give base number of content citizens in any city owned by pplayer.
2090 ****************************************************************************/
2091 citizens
player_content_citizens(const struct player
*pplayer
)
2093 int content
= player_base_citizen_happiness(pplayer
);
2095 return CLIP(0, content
, MAX_CITY_SIZE
);
2098 /****************************************************************************
2099 Give base number of angry citizens in any city owned by pplayer.
2100 ****************************************************************************/
2101 citizens
player_angry_citizens(const struct player
*pplayer
)
2103 if (!game
.info
.angrycitizen
) {
2106 /* Create angry citizens only if we have a negative number of possible
2107 * content citizens. This can happen when empires grow really big. */
2108 int content
= player_base_citizen_happiness(pplayer
);
2110 return CLIP(0, -content
, MAX_CITY_SIZE
);
2114 /**************************************************************************
2115 Return the factor (in %) by which the city's output should be multiplied.
2116 **************************************************************************/
2117 int get_final_city_output_bonus(const struct city
*pcity
, Output_type_id otype
)
2119 struct output_type
*output
= &output_types
[otype
];
2120 int bonus1
= 100 + get_city_tile_output_bonus(pcity
, NULL
, output
,
2122 int bonus2
= 100 + get_city_tile_output_bonus(pcity
, NULL
, output
,
2123 EFT_OUTPUT_BONUS_2
);
2125 return MAX(bonus1
* bonus2
/ 100, 0);
2128 /**************************************************************************
2129 Return the amount of gold generated by buildings under "tithe" attribute
2131 **************************************************************************/
2132 int get_city_tithes_bonus(const struct city
*pcity
)
2134 int tithes_bonus
= 0;
2136 if (get_city_bonus(pcity
, EFT_HAPPINESS_TO_GOLD
) <= 0) {
2140 tithes_bonus
+= get_city_bonus(pcity
, EFT_MAKE_CONTENT
);
2141 tithes_bonus
+= get_city_bonus(pcity
, EFT_FORCE_CONTENT
);
2143 return tithes_bonus
;
2146 /**************************************************************************
2147 Add the incomes of a city according to the taxrates (ignore # of
2148 specialists). trade should be in output[O_TRADE].
2149 **************************************************************************/
2150 void add_tax_income(const struct player
*pplayer
, int trade
, int *output
)
2152 const int SCIENCE
= 0, TAX
= 1, LUXURY
= 2;
2153 int rates
[3], result
[3];
2155 if (game
.info
.changable_tax
) {
2156 rates
[SCIENCE
] = pplayer
->economic
.science
;
2157 rates
[LUXURY
] = pplayer
->economic
.luxury
;
2158 rates
[TAX
] = 100 - rates
[SCIENCE
] - rates
[LUXURY
];
2160 rates
[SCIENCE
] = game
.info
.forced_science
;
2161 rates
[LUXURY
] = game
.info
.forced_luxury
;
2162 rates
[TAX
] = game
.info
.forced_gold
;
2166 if (government_of_player(pplayer
) == game
.government_during_revolution
) {
2168 rates
[LUXURY
] = 100;
2172 distribute(trade
, 3, rates
, result
);
2174 output
[O_SCIENCE
] += result
[SCIENCE
];
2175 output
[O_GOLD
] += result
[TAX
];
2176 output
[O_LUXURY
] += result
[LUXURY
];
2179 /**************************************************************************
2180 Return TRUE if the city built something last turn (meaning production
2181 was completed between last turn and this).
2182 **************************************************************************/
2183 bool city_built_last_turn(const struct city
*pcity
)
2185 return pcity
->turn_last_built
+ 1 >= game
.info
.turn
;
2188 /****************************************************************************
2189 Calculate output (food, trade and shields) generated by the worked tiles
2190 of a city. This will completely overwrite the output[] array.
2192 'workers_map' is an boolean array which defines the placement of the
2193 workers within the city map. It uses the tile index and its size is
2194 defined by city_map_tiles_from_city(_pcity). See also cm_state_init().
2195 ****************************************************************************/
2196 static inline void get_worked_tile_output(const struct city
*pcity
,
2197 int *output
, bool *workers_map
)
2200 #ifdef CITY_DEBUGGING
2201 bool is_celebrating
= base_city_celebrating(pcity
);
2203 struct tile
*pcenter
= city_tile(pcity
);
2205 memset(output
, 0, O_LAST
* sizeof(*output
));
2207 city_tile_iterate_index(city_map_radius_sq_get(pcity
), pcenter
, ptile
,
2209 if (workers_map
== NULL
) {
2210 struct city
*pwork
= tile_worked(ptile
);
2212 is_worked
= (NULL
!= pwork
&& pwork
== pcity
);
2214 is_worked
= workers_map
[city_tile_index
];
2218 output_type_iterate(o
) {
2219 #ifdef CITY_DEBUGGING
2220 /* This assertion never fails, but it's so slow that we disable
2222 fc_assert(city_tile_cache_get_output(pcity
, city_tile_index
, o
)
2223 == city_tile_output(pcity
, ptile
, is_celebrating
, o
));
2224 #endif /* CITY_DEBUGGING */
2225 output
[o
] += city_tile_cache_get_output(pcity
, city_tile_index
, o
);
2226 } output_type_iterate_end
;
2228 } city_tile_iterate_index_end
;
2231 /****************************************************************************
2232 Calculate output (gold, science, and luxury) generated by the specialists
2233 of a city. The output[] array is not cleared but is just added to.
2234 ****************************************************************************/
2235 void add_specialist_output(const struct city
*pcity
, int *output
)
2237 specialist_type_iterate(sp
) {
2238 int count
= pcity
->specialists
[sp
];
2240 output_type_iterate(stat_index
) {
2241 int amount
= get_specialist_output(pcity
, sp
, stat_index
);
2243 output
[stat_index
] += count
* amount
;
2244 } output_type_iterate_end
;
2245 } specialist_type_iterate_end
;
2248 /****************************************************************************
2249 This function sets all the values in the pcity->bonus[] array.
2250 Called near the beginning of city_refresh_from_main_map().
2252 It doesn't depend on anything else in the refresh and doesn't change
2253 as workers are moved around, but does change when buildings are built,
2255 ****************************************************************************/
2256 static inline void set_city_bonuses(struct city
*pcity
)
2258 output_type_iterate(o
) {
2259 pcity
->bonus
[o
] = get_final_city_output_bonus(pcity
, o
);
2260 } output_type_iterate_end
;
2263 /****************************************************************************
2264 This function sets the cache for the tile outputs, the pcity->tile_cache[]
2265 array. It is called near the beginning of city_refresh_from_main_map().
2267 It doesn't depend on anything else in the refresh and doesn't change
2268 as workers are moved around, but does change when buildings are built,
2271 TODO: use the cached values elsethere in the code!
2272 ****************************************************************************/
2273 static inline void city_tile_cache_update(struct city
*pcity
)
2275 bool is_celebrating
= base_city_celebrating(pcity
);
2276 int radius_sq
= city_map_radius_sq_get(pcity
);
2278 /* initialize tile_cache if needed */
2279 if (pcity
->tile_cache
== NULL
|| pcity
->tile_cache_radius_sq
== -1
2280 || pcity
->tile_cache_radius_sq
!= radius_sq
) {
2281 pcity
->tile_cache
= fc_realloc(pcity
->tile_cache
,
2282 city_map_tiles(radius_sq
)
2283 * sizeof(*(pcity
->tile_cache
)));
2284 pcity
->tile_cache_radius_sq
= radius_sq
;
2287 /* Any unreal tiles are skipped - these values should have been memset
2288 * to 0 when the city was created. */
2289 city_tile_iterate_index(radius_sq
, pcity
->tile
, ptile
, city_tile_index
) {
2290 output_type_iterate(o
) {
2291 (pcity
->tile_cache
[city_tile_index
]).output
[o
]
2292 = city_tile_output(pcity
, ptile
, is_celebrating
, o
);
2293 } output_type_iterate_end
;
2294 } city_tile_iterate_index_end
;
2297 /****************************************************************************
2298 This function returns the output of 'o' for the city tile 'city_tile_index'
2300 ****************************************************************************/
2301 static inline int city_tile_cache_get_output(const struct city
*pcity
,
2302 int city_tile_index
,
2303 enum output_type_id o
)
2305 fc_assert_ret_val(pcity
->tile_cache_radius_sq
2306 == city_map_radius_sq_get(pcity
), 0);
2307 fc_assert_ret_val(city_tile_index
< city_map_tiles_from_city(pcity
), 0);
2309 return (pcity
->tile_cache
[city_tile_index
]).output
[o
];
2312 /**************************************************************************
2313 Set the final surplus[] array from the prod[] and usage[] values.
2314 **************************************************************************/
2315 static void set_surpluses(struct city
*pcity
)
2317 output_type_iterate(o
) {
2318 pcity
->surplus
[o
] = pcity
->prod
[o
] - pcity
->usage
[o
];
2319 } output_type_iterate_end
;
2322 /**************************************************************************
2323 Copy the happyness array in the city to index i from index i-1.
2324 **************************************************************************/
2325 static void happy_copy(struct city
*pcity
, enum citizen_feeling i
)
2329 for (; c
< CITIZEN_LAST
; c
++) {
2330 pcity
->feel
[c
][i
] = pcity
->feel
[c
][i
- 1];
2334 /**************************************************************************
2335 Create content, unhappy and angry citizens.
2336 **************************************************************************/
2337 static void citizen_base_mood(struct city
*pcity
)
2339 struct player
*pplayer
= city_owner(pcity
);
2340 citizens
*happy
= &pcity
->feel
[CITIZEN_HAPPY
][FEELING_BASE
];
2341 citizens
*content
= &pcity
->feel
[CITIZEN_CONTENT
][FEELING_BASE
];
2342 citizens
*unhappy
= &pcity
->feel
[CITIZEN_UNHAPPY
][FEELING_BASE
];
2343 citizens
*angry
= &pcity
->feel
[CITIZEN_ANGRY
][FEELING_BASE
];
2344 citizens size
= city_size_get(pcity
);
2345 citizens spes
= city_specialists(pcity
);
2347 /* This is the number of citizens that may start out content, depending
2348 * on empire size and game's city unhappysize. This may be bigger than
2349 * the size of the city, since this is a potential. */
2350 citizens base_content
= player_content_citizens(pplayer
);
2351 /* Similarly, this is the potential number of angry citizens. */
2352 citizens base_angry
= player_angry_citizens(pplayer
);
2354 /* Create content citizens. Take specialists from their ranks. */
2355 *content
= MAX(0, MIN(size
, base_content
) - spes
);
2357 /* Create angry citizens. Specialists never become angry. */
2358 fc_assert_action(base_content
== 0 || base_angry
== 0, *content
= 0);
2359 *angry
= MIN(base_angry
, size
- spes
);
2361 /* Create unhappy citizens. In the beginning, all who are not content,
2362 * specialists or angry are unhappy. This is changed by luxuries and
2363 * buildings later. */
2364 *unhappy
= (size
- spes
- *content
- *angry
);
2366 /* No one is born happy. */
2370 /**************************************************************************
2372 * angry citizen are eliminated first
2373 * then content are made happy, then unhappy content, etc.
2374 * each conversions costs 2 or 4 luxuries.
2375 **************************************************************************/
2376 static inline void citizen_luxury_happy(struct city
*pcity
, int *luxuries
)
2378 citizens
*happy
= &pcity
->feel
[CITIZEN_HAPPY
][FEELING_LUXURY
];
2379 citizens
*content
= &pcity
->feel
[CITIZEN_CONTENT
][FEELING_LUXURY
];
2380 citizens
*unhappy
= &pcity
->feel
[CITIZEN_UNHAPPY
][FEELING_LUXURY
];
2381 citizens
*angry
= &pcity
->feel
[CITIZEN_ANGRY
][FEELING_LUXURY
];
2383 while (*luxuries
>= game
.info
.happy_cost
&& *angry
> 0) {
2384 /* Upgrade angry to unhappy: costs HAPPY_COST each. */
2387 *luxuries
-= game
.info
.happy_cost
;
2389 while (*luxuries
>= game
.info
.happy_cost
&& *content
> 0) {
2390 /* Upgrade content to happy: costs HAPPY_COST each. */
2393 *luxuries
-= game
.info
.happy_cost
;
2395 while (*luxuries
>= 2 * game
.info
.happy_cost
&& *unhappy
> 0) {
2396 /* Upgrade unhappy to happy. Note this is a 2-level upgrade with
2397 * double the cost. */
2400 *luxuries
-= 2 * game
.info
.happy_cost
;
2402 if (*luxuries
>= game
.info
.happy_cost
&& *unhappy
> 0) {
2403 /* Upgrade unhappy to content: costs HAPPY_COST each. */
2406 *luxuries
-= game
.info
.happy_cost
;
2410 /**************************************************************************
2411 Make citizens happy due to luxury.
2412 **************************************************************************/
2413 static inline void citizen_happy_luxury(struct city
*pcity
)
2415 int x
= pcity
->prod
[O_LUXURY
];
2417 citizen_luxury_happy(pcity
, &x
);
2420 /**************************************************************************
2421 Make citizens content due to city improvements.
2422 **************************************************************************/
2423 static inline void citizen_content_buildings(struct city
*pcity
)
2425 citizens
*content
= &pcity
->feel
[CITIZEN_CONTENT
][FEELING_EFFECT
];
2426 citizens
*unhappy
= &pcity
->feel
[CITIZEN_UNHAPPY
][FEELING_EFFECT
];
2427 citizens
*angry
= &pcity
->feel
[CITIZEN_ANGRY
][FEELING_EFFECT
];
2428 int faces
= get_city_bonus(pcity
, EFT_MAKE_CONTENT
);
2430 /* make people content (but not happy):
2431 get rid of angry first, then make unhappy content. */
2432 while (faces
> 0 && *angry
> 0) {
2437 while (faces
> 0 && *unhappy
> 0) {
2444 /**************************************************************************
2445 Apply effects of citizen nationality to happiness
2446 **************************************************************************/
2447 static inline void citizen_happiness_nationality(struct city
*pcity
)
2449 citizens
*happy
= &pcity
->feel
[CITIZEN_HAPPY
][FEELING_NATIONALITY
];
2450 citizens
*content
= &pcity
->feel
[CITIZEN_CONTENT
][FEELING_NATIONALITY
];
2451 citizens
*unhappy
= &pcity
->feel
[CITIZEN_UNHAPPY
][FEELING_NATIONALITY
];
2453 if (game
.info
.citizen_nationality
) {
2454 int pct
= get_city_bonus(pcity
, EFT_ENEMY_CITIZEN_UNHAPPY_PCT
);
2459 struct player
*owner
= city_owner(pcity
);
2461 citizens_foreign_iterate(pcity
, pslot
, nationality
) {
2462 if (pplayers_at_war(owner
, player_slot_get_player(pslot
))) {
2463 enemies
+= nationality
;
2465 } citizens_foreign_iterate_end
;
2467 unhappy_inc
= enemies
* pct
/ 100;
2469 /* First make content => unhappy, then happy => unhappy,
2470 * then happy => content. No-one becomes angry. */
2471 while (unhappy_inc
> 0 && *content
> 0) {
2476 while (unhappy_inc
> 1 && *happy
> 0) {
2481 while (unhappy_inc
> 0 && *happy
> 0) {
2490 /**************************************************************************
2491 Make citizens happy/unhappy due to units.
2493 This function requires that pcity->martial_law and
2494 pcity->unit_happy_cost have already been set in city_support().
2495 **************************************************************************/
2496 static inline void citizen_happy_units(struct city
*pcity
)
2498 citizens
*happy
= &pcity
->feel
[CITIZEN_HAPPY
][FEELING_MARTIAL
];
2499 citizens
*content
= &pcity
->feel
[CITIZEN_CONTENT
][FEELING_MARTIAL
];
2500 citizens
*unhappy
= &pcity
->feel
[CITIZEN_UNHAPPY
][FEELING_MARTIAL
];
2501 citizens
*angry
= &pcity
->feel
[CITIZEN_ANGRY
][FEELING_MARTIAL
];
2502 citizens amt
= pcity
->martial_law
;
2504 /* Pacify discontent citizens through martial law. First convert
2505 * angry => unhappy, then unhappy => content. */
2506 while (amt
> 0 && *angry
> 0) {
2511 while (amt
> 0 && *unhappy
> 0) {
2517 /* Now make citizens unhappier because of military units away from home.
2518 * First make content => unhappy, then happy => unhappy,
2519 * then happy => content. */
2520 amt
= pcity
->unit_happy_upkeep
;
2521 while (amt
> 0 && *content
> 0) {
2526 while (amt
> 1 && *happy
> 0) {
2531 while (amt
> 0 && *happy
> 0) {
2536 /* Any remaining unhappiness is lost since angry citizens aren't created
2538 /* FIXME: Why not? - Per */
2541 /**************************************************************************
2542 Make citizens happy due to wonders.
2543 **************************************************************************/
2544 static inline void citizen_happy_wonders(struct city
*pcity
)
2546 citizens
*happy
= &pcity
->feel
[CITIZEN_HAPPY
][FEELING_FINAL
];
2547 citizens
*content
= &pcity
->feel
[CITIZEN_CONTENT
][FEELING_FINAL
];
2548 citizens
*unhappy
= &pcity
->feel
[CITIZEN_UNHAPPY
][FEELING_FINAL
];
2549 citizens
*angry
= &pcity
->feel
[CITIZEN_ANGRY
][FEELING_FINAL
];
2550 int bonus
= get_city_bonus(pcity
, EFT_MAKE_HAPPY
);
2552 /* First create happy citizens from content, then from unhappy
2553 * citizens; we cannot help angry citizens here. */
2554 while (bonus
> 0 && *content
> 0) {
2559 while (bonus
> 1 && *unhappy
> 0) {
2564 /* The rest falls through and lets unhappy people become content. */
2566 if (get_city_bonus(pcity
, EFT_NO_UNHAPPY
) > 0) {
2567 *content
+= *unhappy
+ *angry
;
2573 bonus
+= get_city_bonus(pcity
, EFT_FORCE_CONTENT
);
2575 /* get rid of angry first, then make unhappy content */
2576 while (bonus
> 0 && *angry
> 0) {
2581 while (bonus
> 0 && *unhappy
> 0) {
2588 /**************************************************************************
2589 Set food, tax, science and shields production to zero if city is in
2591 **************************************************************************/
2592 static inline void unhappy_city_check(struct city
*pcity
)
2594 if (city_unhappy(pcity
)) {
2595 output_type_iterate(o
) {
2596 switch (output_types
[o
].unhappy_penalty
) {
2597 case UNHAPPY_PENALTY_NONE
:
2598 pcity
->unhappy_penalty
[o
] = 0;
2600 case UNHAPPY_PENALTY_SURPLUS
:
2601 pcity
->unhappy_penalty
[o
] = MAX(pcity
->prod
[o
] - pcity
->usage
[o
], 0);
2603 case UNHAPPY_PENALTY_ALL_PRODUCTION
:
2604 pcity
->unhappy_penalty
[o
] = pcity
->prod
[o
];
2608 pcity
->prod
[o
] -= pcity
->unhappy_penalty
[o
];
2609 } output_type_iterate_end
;
2611 memset(pcity
->unhappy_penalty
, 0,
2612 O_LAST
* sizeof(*pcity
->unhappy_penalty
));
2616 /**************************************************************************
2617 Calculate the pollution from production and population in the city.
2618 **************************************************************************/
2619 int city_pollution_types(const struct city
*pcity
, int shield_total
,
2620 int *pollu_prod
, int *pollu_pop
, int *pollu_mod
)
2624 /* Add one one pollution per shield, multipled by the bonus. */
2625 prod
= 100 + get_city_bonus(pcity
, EFT_POLLU_PROD_PCT
);
2626 prod
= shield_total
* MAX(prod
, 0) / 100;
2628 /* Add one pollution per citizen for baseline combined bonus (100%). */
2629 pop
= (100 + get_city_bonus(pcity
, EFT_POLLU_POP_PCT
))
2630 * (100 + get_city_bonus(pcity
, EFT_POLLU_POP_PCT_2
))
2632 pop
= (city_size_get(pcity
) * MAX(pop
, 0)) / 100;
2634 /* Then there is base pollution (usually a negative number). */
2635 mod
= game
.info
.base_pollution
;
2646 return MAX(prod
+ pop
+ mod
, 0);
2649 /**************************************************************************
2650 Calculate pollution for the city. The shield_total must be passed in
2651 (most callers will want to pass pcity->shield_prod).
2652 **************************************************************************/
2653 int city_pollution(const struct city
*pcity
, int shield_total
)
2655 return city_pollution_types(pcity
, shield_total
, NULL
, NULL
, NULL
);
2658 /**************************************************************************
2659 Gets whether cities that pcity trades with had the plague. If so, it
2660 returns the health penalty in tenth of percent which depends on the size
2661 of both cities. The health penalty is given as the product of the ruleset
2662 option 'game.info.illness_trade_infection' (in percent) and the square
2663 root of the product of the size of both cities.
2664 *************************************************************************/
2665 static int get_trade_illness(const struct city
*pcity
)
2667 float illness_trade
= 0.0;
2669 trade_partners_iterate(pcity
, trade_city
) {
2670 if (trade_city
->turn_plague
!= -1
2671 && game
.info
.turn
- trade_city
->turn_plague
< 5) {
2672 illness_trade
+= (float)game
.info
.illness_trade_infection
2673 * sqrt(1.0 * city_size_get(pcity
)
2674 * city_size_get(trade_city
)) / 100.0;
2676 } trade_partners_iterate_end
;
2678 return (int)illness_trade
;
2681 /**************************************************************************
2682 Get any effects regarding health from the buildings of the city. The
2683 effect defines the reduction of the possibility of an illness in percent.
2684 **************************************************************************/
2685 static int get_city_health(const struct city
*pcity
)
2687 return get_city_bonus(pcity
, EFT_HEALTH_PCT
);
2690 /**************************************************************************
2691 Calculate city's illness in tenth of percent:
2693 base illness (the maximum value for illness in percent is given by
2694 'game.info.illness_base_factor')
2695 + trade illness (see get_trade_illness())
2696 + pollution illness (the pollution in the city times
2697 'game.info.illness_pollution_factor')
2699 The illness is reduced by the percentage given by the health effect.
2700 Illness cannot exceed 999 (= 99.9%), or be less then 0
2701 *************************************************************************/
2702 int city_illness_calc(const struct city
*pcity
, int *ill_base
,
2703 int *ill_size
, int *ill_trade
, int *ill_pollution
)
2705 int illness_size
= 0, illness_trade
= 0, illness_pollution
= 0;
2706 int illness_base
, illness_percent
;
2708 if (game
.info
.illness_on
2709 && city_size_get(pcity
) > game
.info
.illness_min_size
) {
2710 /* offset the city size by game.info.illness_min_size */
2711 int use_size
= city_size_get(pcity
) - game
.info
.illness_min_size
;
2713 illness_size
= (int)((1.0 - exp(- (float)use_size
/ 10.0))
2714 * 10.0 * game
.info
.illness_base_factor
);
2716 /* on the server we recalculate the illness due to trade as we have
2717 * all informations */
2718 illness_trade
= get_trade_illness(pcity
);
2720 /* on the client we have to rely on the value saved within the city
2722 illness_trade
= pcity
->illness_trade
;
2725 illness_pollution
= pcity
->pollution
2726 * game
.info
.illness_pollution_factor
/ 100;
2729 illness_base
= illness_size
+ illness_trade
+ illness_pollution
;
2730 illness_percent
= 100 - get_city_health(pcity
);
2732 /* returning other data */
2734 *ill_size
= illness_size
;
2738 *ill_trade
= illness_trade
;
2741 if (ill_pollution
) {
2742 *ill_pollution
= illness_pollution
;
2746 *ill_base
= illness_base
;
2749 return CLIP(0, illness_base
* illness_percent
/ 100 , 999);
2752 /****************************************************************************
2753 Returns whether city had a plague outbreak this turn.
2754 ****************************************************************************/
2755 bool city_had_recent_plague(const struct city
*pcity
)
2757 /* Correctly handles special case turn_plague == -1 (never) */
2758 return (pcity
->turn_plague
== game
.info
.turn
);
2761 /****************************************************************************
2762 The maximum number of units a city can build per turn.
2763 ****************************************************************************/
2764 int city_build_slots(const struct city
*pcity
)
2766 return get_city_bonus(pcity
, EFT_CITY_BUILD_SLOTS
);
2769 /****************************************************************************
2770 A city's maximum airlift capacity.
2771 (Note, this still returns a finite number even if airliftingstyle allows
2773 ****************************************************************************/
2774 int city_airlift_max(const struct city
*pcity
)
2776 return get_city_bonus(pcity
, EFT_AIRLIFT
);
2779 /**************************************************************************
2780 Set food, trade and shields production in a city.
2782 This initializes the prod[] and waste[] arrays. It assumes that
2783 the bonus[] and citizen_base[] arrays are alread built.
2784 **************************************************************************/
2785 inline void set_city_production(struct city
*pcity
)
2787 /* Calculate city production!
2789 * This is a rather complicated process if we allow rules to become
2790 * more generalized. We can assume that there are no recursive dependency
2791 * loops, but there are some dependencies that do not follow strict
2792 * ordering. For instance corruption must be calculated before
2793 * trade taxes can be counted up, which must occur before the science bonus
2794 * is added on. But the calculation of corruption must include the
2795 * trade bonus. To do this without excessive special casing means that in
2796 * this case the bonuses are multiplied on twice (but only saved the second
2800 output_type_iterate(o
) {
2801 pcity
->prod
[o
] = pcity
->citizen_base
[o
];
2802 } output_type_iterate_end
;
2804 /* Add on special extra incomes: trade routes and tithes. */
2805 trade_routes_iterate(pcity
, proute
) {
2806 struct city
*tcity
= game_city_by_number(proute
->partner
);
2809 fc_assert_action(tcity
!= NULL
, continue);
2811 can_trade
= can_cities_trade(pcity
, tcity
);
2814 enum trade_route_type type
= cities_trade_route_type(pcity
, tcity
);
2815 struct trade_route_settings
*settings
= trade_route_settings_by_type(type
);
2817 if (settings
->cancelling
== TRI_ACTIVE
) {
2826 trade_base_between_cities(pcity
, game_city_by_number(proute
->partner
));
2827 proute
->value
= trade_from_route(pcity
, proute
, value
);
2828 pcity
->prod
[O_TRADE
] += proute
->value
2829 * (100 + get_city_bonus(pcity
, EFT_TRADEROUTE_PCT
)) / 100;
2833 } trade_routes_iterate_end
;
2834 pcity
->prod
[O_GOLD
] += get_city_tithes_bonus(pcity
);
2836 /* Account for waste. Note that waste is calculated before tax income is
2837 * calculated, so if you had "science waste" it would not include taxed
2838 * science. However waste is calculated after the bonuses are multiplied
2839 * on, so shield waste will include shield bonuses. */
2840 output_type_iterate(o
) {
2841 pcity
->waste
[o
] = city_waste(pcity
, o
,
2842 pcity
->prod
[o
] * pcity
->bonus
[o
] / 100,
2844 } output_type_iterate_end
;
2846 /* Convert trade into science/luxury/gold, and add this on to whatever
2847 * science/luxury/gold is already there. */
2848 add_tax_income(city_owner(pcity
),
2849 pcity
->prod
[O_TRADE
] * pcity
->bonus
[O_TRADE
] / 100
2850 - pcity
->waste
[O_TRADE
] - pcity
->usage
[O_TRADE
],
2853 /* Add on effect bonuses and waste. Note that the waste calculation
2854 * (above) already includes the bonus multiplier. */
2855 output_type_iterate(o
) {
2856 pcity
->prod
[o
] = pcity
->prod
[o
] * pcity
->bonus
[o
] / 100;
2857 pcity
->prod
[o
] -= pcity
->waste
[o
];
2858 } output_type_iterate_end
;
2861 /**************************************************************************
2862 Query unhappiness caused by a given unit.
2863 **************************************************************************/
2864 int city_unit_unhappiness(struct unit
*punit
, int *free_unhappy
)
2867 struct unit_type
*ut
;
2871 if (!punit
|| !free_unhappy
) {
2875 pcity
= game_city_by_number(punit
->homecity
);
2876 if (pcity
== NULL
) {
2880 ut
= unit_type_get(punit
);
2881 plr
= unit_owner(punit
);
2882 happy_cost
= utype_happy_cost(ut
, plr
);
2884 if (happy_cost
<= 0) {
2888 fc_assert_ret_val(0 <= *free_unhappy
, 0);
2890 if (!unit_being_aggressive(punit
) && !is_field_unit(punit
)) {
2894 happy_cost
-= get_city_bonus(pcity
, EFT_MAKE_CONTENT_MIL_PER
);
2895 if (happy_cost
<= 0) {
2899 if (*free_unhappy
>= happy_cost
) {
2900 *free_unhappy
-= happy_cost
;
2903 happy_cost
-= *free_unhappy
;
2910 /**************************************************************************
2911 Calculate upkeep costs. This builds the pcity->usage[] array as well
2912 as setting some happiness values.
2913 **************************************************************************/
2914 static inline void city_support(struct city
*pcity
)
2916 int free_unhappy
, martial_law_each
;
2918 /* Clear all usage values. */
2919 memset(pcity
->usage
, 0, O_LAST
* sizeof(*pcity
->usage
));
2920 pcity
->martial_law
= 0;
2921 pcity
->unit_happy_upkeep
= 0;
2923 /* Building and unit gold upkeep depends on the setting
2924 * 'game.info.gold_upkeep_style':
2925 * GOLD_UPKEEP_CITY: The upkeep for buildings and units is paid by the
2927 * GOLD_UPKEEP_MIXED: The upkeep for buildings is paid by the city.
2928 * The upkeep for units is paid by the nation.
2929 * GOLD_UPKEEP_NATION: The upkeep for buildings and units is paid by the
2931 fc_assert_msg(gold_upkeep_style_is_valid(game
.info
.gold_upkeep_style
),
2932 "Invalid gold_upkeep_style %d", game
.info
.gold_upkeep_style
);
2933 switch (game
.info
.gold_upkeep_style
) {
2934 case GOLD_UPKEEP_CITY
:
2935 pcity
->usage
[O_GOLD
] += city_total_unit_gold_upkeep(pcity
);
2937 case GOLD_UPKEEP_MIXED
:
2938 pcity
->usage
[O_GOLD
] += city_total_impr_gold_upkeep(pcity
);
2940 case GOLD_UPKEEP_NATION
:
2944 /* Food consumption by citizens. */
2945 pcity
->usage
[O_FOOD
] += game
.info
.food_cost
* city_size_get(pcity
);
2947 /* military units in this city (need _not_ be home city) can make
2948 * unhappy citizens content */
2949 martial_law_each
= get_city_bonus(pcity
, EFT_MARTIAL_LAW_EACH
);
2950 if (martial_law_each
> 0) {
2952 int martial_law_max
= get_city_bonus(pcity
, EFT_MARTIAL_LAW_MAX
);
2954 unit_list_iterate(pcity
->tile
->units
, punit
) {
2955 if ((count
< martial_law_max
|| martial_law_max
== 0)
2956 && is_military_unit(punit
)
2957 && unit_owner(punit
) == city_owner(pcity
)) {
2960 } unit_list_iterate_end
;
2962 pcity
->martial_law
= CLIP(0, count
* martial_law_each
, MAX_CITY_SIZE
);
2965 free_unhappy
= get_city_bonus(pcity
, EFT_MAKE_CONTENT_MIL
);
2966 unit_list_iterate(pcity
->units_supported
, punit
) {
2967 pcity
->unit_happy_upkeep
+= city_unit_unhappiness(punit
, &free_unhappy
);
2968 output_type_iterate(o
) {
2970 /* O_GOLD is handled with "game.info.gold_upkeep_style", see over. */
2971 pcity
->usage
[o
] += punit
->upkeep
[o
];
2973 } output_type_iterate_end
;
2974 } unit_list_iterate_end
;
2977 /**************************************************************************
2978 Refreshes the internal cached data in the city structure.
2980 !full_refresh will not update tile_cache[] or bonus[]. These two
2981 values do not need to be recalculated for AI CMA testing.
2983 'workers_map' is an boolean array which defines the placement of the
2984 workers within the city map. It uses the tile index and its size is
2985 defined by city_map_tiles_from_city(_pcity). See also cm_state_init().
2987 If 'workers_map' is set, only basic updates are needed.
2988 **************************************************************************/
2989 void city_refresh_from_main_map(struct city
*pcity
, bool *workers_map
)
2991 if (workers_map
== NULL
) {
2992 /* do a full refresh */
2994 /* Calculate the bonus[] array values. */
2995 set_city_bonuses(pcity
);
2996 /* Calculate the tile_cache[] values. */
2997 city_tile_cache_update(pcity
);
2998 /* manage settlers, and units */
2999 city_support(pcity
);
3002 /* Calculate output from citizens (uses city_tile_cache_get_output()). */
3003 get_worked_tile_output(pcity
, pcity
->citizen_base
, workers_map
);
3004 add_specialist_output(pcity
, pcity
->citizen_base
);
3006 set_city_production(pcity
);
3007 citizen_base_mood(pcity
);
3008 /* Note that pollution is calculated before unhappy_city_check() makes
3009 * deductions for disorder; so a city in disorder still causes pollution */
3010 pcity
->pollution
= city_pollution(pcity
, pcity
->prod
[O_SHIELD
]);
3012 happy_copy(pcity
, FEELING_LUXURY
);
3013 citizen_happy_luxury(pcity
); /* with our new found luxuries */
3015 happy_copy(pcity
, FEELING_EFFECT
);
3016 citizen_content_buildings(pcity
);
3018 happy_copy(pcity
, FEELING_NATIONALITY
);
3019 citizen_happiness_nationality(pcity
);
3021 /* Martial law & unrest from units */
3022 happy_copy(pcity
, FEELING_MARTIAL
);
3023 citizen_happy_units(pcity
);
3025 /* Building (including wonder) happiness effects */
3026 happy_copy(pcity
, FEELING_FINAL
);
3027 citizen_happy_wonders(pcity
);
3029 unhappy_city_check(pcity
);
3030 set_surpluses(pcity
);
3033 /**************************************************************************
3034 Give corruption/waste generated by city. otype gives the output type
3035 (O_SHIELD/O_TRADE). 'total' gives the total output of this type in the
3036 city. If non-NULL, 'breakdown' should be an OLOSS_LAST-sized array
3037 which will be filled in with a breakdown of the kinds of waste
3039 **************************************************************************/
3040 int city_waste(const struct city
*pcity
, Output_type_id otype
, int total
,
3043 int penalty_waste
= 0;
3044 int penalty_size
= 0; /* separate notradesize/fulltradesize from normal
3046 int total_eft
= total
; /* normal corruption calculated on total reduced by
3047 * possible size penalty */
3048 int waste_level
= get_city_output_bonus(pcity
, get_output_type(otype
),
3050 bool waste_all
= FALSE
;
3052 if (otype
== O_TRADE
) {
3053 /* FIXME: special case for trade: it is affected by notradesize and
3054 * fulltradesize server settings.
3056 * If notradesize and fulltradesize are equal then the city gets no
3057 * trade at that size. */
3058 int notradesize
= MIN(game
.info
.notradesize
, game
.info
.fulltradesize
);
3059 int fulltradesize
= MAX(game
.info
.notradesize
, game
.info
.fulltradesize
);
3061 if (city_size_get(pcity
) <= notradesize
) {
3062 penalty_size
= total_eft
; /* Then no trade income. */
3063 } else if (city_size_get(pcity
) >= fulltradesize
) {
3066 penalty_size
= total_eft
* (fulltradesize
- city_size_get(pcity
))
3067 / (fulltradesize
- notradesize
);
3071 /* Apply corruption only to anything left after tradesize */
3072 total_eft
-= penalty_size
;
3074 /* Distance-based waste.
3075 * Don't bother calculating if there's nothing left to lose. */
3076 if (total_eft
> 0) {
3077 int waste_by_dist
= get_city_output_bonus(pcity
, get_output_type(otype
),
3078 EFT_OUTPUT_WASTE_BY_DISTANCE
);
3079 int waste_by_rel_dist
= get_city_output_bonus(pcity
, get_output_type(otype
),
3080 EFT_OUTPUT_WASTE_BY_REL_DISTANCE
);
3081 if (waste_by_dist
> 0 || waste_by_rel_dist
> 0) {
3082 const struct city
*gov_center
= NULL
;
3083 int min_dist
= FC_INFINITY
;
3085 /* Check the special case that city itself is gov center
3086 * before expensive iteration through all cities. */
3087 if (is_gov_center(pcity
)) {
3091 city_list_iterate(city_owner(pcity
)->cities
, gc
) {
3092 /* Do not recheck current city */
3093 if (gc
!= pcity
&& is_gov_center(gc
)) {
3094 int dist
= real_map_distance(gc
->tile
, pcity
->tile
);
3096 if (dist
< min_dist
) {
3101 } city_list_iterate_end
;
3104 if (gov_center
== NULL
) {
3105 waste_all
= TRUE
; /* no gov center - no income */
3107 waste_level
+= waste_by_dist
* min_dist
;
3108 if (waste_by_rel_dist
> 0) {
3109 /* Multiply by 50 as an "standard size" for which EFT_OUTPUT_WASTE_BY_DISTANCE
3110 * and EFT_OUTPUT_WASTE_BY_REL_DISTANCE would give same result. */
3111 waste_level
+= waste_by_rel_dist
* 50 * min_dist
3112 / MAX(wld
.map
.xsize
, wld
.map
.ysize
);
3119 penalty_waste
= total_eft
;
3121 int waste_pct
= get_city_output_bonus(pcity
, get_output_type(otype
),
3122 EFT_OUTPUT_WASTE_PCT
);
3124 /* corruption/waste calculated only for the actually produced amount */
3125 if (waste_level
> 0) {
3126 penalty_waste
= total_eft
* waste_level
/ 100;
3129 /* bonus calculated only for the actually produced amount */
3130 penalty_waste
-= penalty_waste
* waste_pct
/ 100;
3133 penalty_waste
= MIN(MAX(penalty_waste
, 0), total_eft
);
3137 breakdown
[OLOSS_WASTE
] = penalty_waste
;
3138 breakdown
[OLOSS_SIZE
] = penalty_size
;
3141 /* add up total penalty */
3142 return penalty_waste
+ penalty_size
;
3145 /**************************************************************************
3146 Give the number of specialists in a city.
3147 **************************************************************************/
3148 citizens
city_specialists(const struct city
*pcity
)
3152 specialist_type_iterate(sp
) {
3153 fc_assert_ret_val(MAX_CITY_SIZE
- count
> pcity
->specialists
[sp
], 0);
3154 count
+= pcity
->specialists
[sp
];
3155 } specialist_type_iterate_end
;
3160 /****************************************************************************
3161 Return the "best" specialist available in the game. This specialist will
3162 have the most of the given type of output. If pcity is given then only
3163 specialists usable by pcity will be considered.
3164 ****************************************************************************/
3165 Specialist_type_id
best_specialist(Output_type_id otype
,
3166 const struct city
*pcity
)
3168 int best
= DEFAULT_SPECIALIST
;
3169 int val
= get_specialist_output(pcity
, best
, otype
);
3171 specialist_type_iterate(i
) {
3172 if (!pcity
|| city_can_use_specialist(pcity
, i
)) {
3173 int val2
= get_specialist_output(pcity
, i
, otype
);
3180 } specialist_type_iterate_end
;
3185 /**************************************************************************
3186 Adds an improvement (and its effects) to a city.
3187 **************************************************************************/
3188 void city_add_improvement(struct city
*pcity
,
3189 const struct impr_type
*pimprove
)
3191 pcity
->built
[improvement_index(pimprove
)].turn
= game
.info
.turn
; /*I_ACTIVE*/
3193 if (is_server() && is_wonder(pimprove
)) {
3194 /* Client just read the info from the packets. */
3195 wonder_built(pcity
, pimprove
);
3199 /**************************************************************************
3200 Removes an improvement (and its effects) from a city.
3201 **************************************************************************/
3202 void city_remove_improvement(struct city
*pcity
,
3203 const struct impr_type
*pimprove
)
3205 log_debug("Improvement %s removed from city %s",
3206 improvement_rule_name(pimprove
), pcity
->name
);
3208 pcity
->built
[improvement_index(pimprove
)].turn
= I_DESTROYED
;
3210 if (is_server() && is_wonder(pimprove
)) {
3211 /* Client just read the info from the packets. */
3212 wonder_destroyed(pcity
, pimprove
);
3216 /**************************************************************************
3217 Returns TRUE iff the city has set the given option.
3218 **************************************************************************/
3219 bool is_city_option_set(const struct city
*pcity
, enum city_options option
)
3221 return BV_ISSET(pcity
->city_options
, option
);
3224 /**************************************************************************
3225 Allocate memory for this amount of city styles.
3226 **************************************************************************/
3227 void city_styles_alloc(int num
)
3231 city_styles
= fc_calloc(num
, sizeof(*city_styles
));
3232 game
.control
.styles_count
= num
;
3234 for (i
= 0; i
< game
.control
.styles_count
; i
++) {
3235 requirement_vector_init(&city_styles
[i
].reqs
);
3239 /**************************************************************************
3240 De-allocate the memory used by the city styles.
3241 **************************************************************************/
3242 void city_styles_free(void)
3246 for (i
= 0; i
< game
.control
.styles_count
; i
++) {
3247 requirement_vector_free(&city_styles
[i
].reqs
);
3252 game
.control
.styles_count
= 0;
3255 /**************************************************************************
3256 Create virtual skeleton for a city.
3257 Values are mostly sane defaults.
3259 Always tile_set_owner(ptile, pplayer) sometime after this!
3260 **************************************************************************/
3261 struct city
*create_city_virtual(struct player
*pplayer
,
3262 struct tile
*ptile
, const char *name
)
3266 /* Make sure that contents of city structure are correctly initialized,
3267 * if you ever allocate it by some other mean than fc_calloc() */
3268 struct city
*pcity
= fc_calloc(1, sizeof(*pcity
));
3270 fc_assert_ret_val(NULL
!= name
, NULL
); /* No unnamed cities! */
3271 sz_strlcpy(pcity
->name
, name
);
3273 pcity
->tile
= ptile
;
3274 fc_assert_ret_val(NULL
!= pplayer
, NULL
); /* No unowned cities! */
3275 pcity
->owner
= pplayer
;
3276 pcity
->original
= pplayer
;
3278 /* City structure was allocated with fc_calloc(), so contents are initially
3279 * zero. There is no need to initialize it a second time. */
3281 /* Now set some usefull default values. */
3282 city_size_set(pcity
, 1);
3283 pcity
->specialists
[DEFAULT_SPECIALIST
] = 1;
3285 output_type_iterate(o
) {
3286 pcity
->bonus
[o
] = 100;
3287 } output_type_iterate_end
;
3289 pcity
->turn_plague
= -1; /* -1 = never */
3290 pcity
->did_buy
= FALSE
;
3291 pcity
->city_radius_sq
= game
.info
.init_city_radius_sq
;
3292 pcity
->turn_founded
= game
.info
.turn
;
3293 pcity
->turn_last_built
= game
.info
.turn
;
3295 pcity
->tile_cache_radius_sq
= -1; /* -1 = tile_cache must be initialised */
3297 /* pcity->ai.act_cache: worker activities on the city map */
3299 /* Initialise improvements list */
3300 for (i
= 0; i
< ARRAY_SIZE(pcity
->built
); i
++) {
3301 pcity
->built
[i
].turn
= I_NEVER
;
3304 /* Set up the worklist */
3305 worklist_init(&pcity
->worklist
);
3307 pcity
->units_supported
= unit_list_new();
3308 pcity
->routes
= trade_route_list_new();
3309 pcity
->task_reqs
= worker_task_list_new();
3312 pcity
->server
.mgr_score_calc_turn
= -1; /* -1 = never */
3314 CALL_FUNC_EACH_AI(city_alloc
, pcity
);
3316 pcity
->client
.info_units_supported
=
3317 unit_list_new_full(unit_virtual_destroy
);
3318 pcity
->client
.info_units_present
=
3319 unit_list_new_full(unit_virtual_destroy
);
3320 /* collecting_info_units_supported set by fc_calloc().
3321 * collecting_info_units_present set by fc_calloc(). */
3327 /**************************************************************************
3328 Removes the virtual skeleton of a city. You should already have removed
3329 all buildings and units you have added to the city before this.
3330 **************************************************************************/
3331 void destroy_city_virtual(struct city
*pcity
)
3333 CALL_FUNC_EACH_AI(city_free
, pcity
);
3335 citizens_free(pcity
);
3337 worker_task_list_destroy(pcity
->task_reqs
);
3339 unit_list_destroy(pcity
->units_supported
);
3340 trade_route_list_destroy(pcity
->routes
);
3341 if (pcity
->tile_cache
!= NULL
) {
3342 free(pcity
->tile_cache
);
3346 unit_list_destroy(pcity
->client
.info_units_supported
);
3347 unit_list_destroy(pcity
->client
.info_units_present
);
3348 /* Handle a rare case where the game is freed in the middle of a
3349 * spy/diplomat investigate cycle. */
3350 if (pcity
->client
.collecting_info_units_supported
!= NULL
) {
3351 unit_list_destroy(pcity
->client
.collecting_info_units_supported
);
3353 if (pcity
->client
.collecting_info_units_present
!= NULL
) {
3354 unit_list_destroy(pcity
->client
.collecting_info_units_present
);
3358 memset(pcity
, 0, sizeof(*pcity
)); /* ensure no pointers remain */
3362 /**************************************************************************
3363 Check if city with given id still exist. Use this before using
3364 old city pointers when city might have disappeared.
3365 **************************************************************************/
3366 bool city_exist(int id
)
3368 /* Check if city exist in game */
3369 if (game_city_by_number(id
)) {
3376 /**************************************************************************
3377 Return TRUE if the city is a virtual city. That is, it is a valid city
3378 pointer but does not correspond to a city that exists in the game.
3380 NB: A return value of FALSE implies that either the pointer is NULL or
3381 that the city exists in the game.
3382 **************************************************************************/
3383 bool city_is_virtual(const struct city
*pcity
)
3389 return pcity
!= game_city_by_number(pcity
->id
);
3392 /**************************************************************************
3393 Return TRUE if the city is centered at the given tile.
3395 NB: This doesn't simply check whether pcity->tile == ptile because that
3396 would miss virtual clones made of city center tiles, which are used by
3397 autosettler to judge whether improvements are worthwhile. The upshot is
3398 that city centers would appear to lose their irrigation/farmland bonuses
3399 as well as their minimum outputs of one food and one shield, and thus
3400 autosettler would rarely transform or mine them.
3401 **************************************************************************/
3402 bool is_city_center(const struct city
*pcity
, const struct tile
*ptile
)
3404 if (!pcity
|| !pcity
->tile
|| !ptile
) {
3408 return tile_index(city_tile(pcity
)) == tile_index(ptile
);
3411 /**************************************************************************
3412 Return TRUE if the city is worked without using up a citizen.
3413 **************************************************************************/
3414 bool is_free_worked(const struct city
*pcity
, const struct tile
*ptile
)
3416 return is_city_center(pcity
, ptile
);
3419 /**************************************************************************
3420 Return pointer to ai data of given city and ai type.
3421 **************************************************************************/
3422 void *city_ai_data(const struct city
*pcity
, const struct ai_type
*ai
)
3424 return pcity
->server
.ais
[ai_type_number(ai
)];
3427 /**************************************************************************
3428 Attach ai data to city
3429 **************************************************************************/
3430 void city_set_ai_data(struct city
*pcity
, const struct ai_type
*ai
,
3433 pcity
->server
.ais
[ai_type_number(ai
)] = data
;