Freeciv-3.1: /fast/freeciv/nightly/S3_1/common/city.c Source File

Freeciv-3.1

Loading...
Searching...
No Matches
city.c
Go to the documentation of this file.
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)
6 any later version.
7
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***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include <stdlib.h>
19#include <string.h>
20#include <math.h> /* pow, sqrt, exp */
21
22/* utility */
23#include "distribute.h"
24#include "fcintl.h"
25#include "log.h"
26#include "mem.h"
27#include "support.h"
28
29/* common */
30#include "ai.h"
31#include "citizens.h"
32#include "effects.h"
33#include "game.h"
34#include "government.h"
35#include "improvement.h"
36#include "map.h"
37#include "movement.h"
38#include "packets.h"
39#include "specialist.h"
40#include "traderoutes.h"
41#include "unit.h"
42
43/* aicore */
44#include "cm.h"
45
46#include "city.h"
47
48/* Define this to add in extra (very slow) assertions for the city code. */
49#undef CITY_DEBUGGING
50
51static char *citylog_map_line(int y, int city_radius_sq, int *city_map_data);
52#ifdef FREECIV_DEBUG
53/* only used for debugging */
54static void citylog_map_index(enum log_level level);
55static void citylog_map_radius_sq(enum log_level level);
56#endif /* FREECIV_DEBUG */
57
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];
64
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];
67
68/* definitions and functions for the tile_cache */
69 struct tile_cache {
70 int output[O_LAST];
71};
72
73static inline void city_tile_cache_update(struct city *pcity);
74static inline int city_tile_cache_get_output(const struct city *pcity,
75 int city_tile_index,
76 enum output_type_id o);
77
78 struct citystyle *city_styles = NULL;
79
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}
89};
90
91/**********************************************************************/
95 bool city_tile_index_to_xy(int *city_map_x, int *city_map_y,
96 int city_tile_index, int city_radius_sq)
97{
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);
100
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)) {
104 return FALSE;
105 }
106
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);
109
110 return TRUE;
111}
112
113/**********************************************************************/
117 int city_tile_xy_to_index(int city_map_x, int city_map_y,
118 int city_radius_sq)
119{
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,
123 city_map_y), 0);
124
125 return city_map_xy[city_map_x][city_map_y];
126}
127
128/**********************************************************************/
131 int city_map_radius_sq_get(const struct city *pcity)
132{
133 /* a save return value is only the minimal squared radius */
134 fc_assert_ret_val(pcity != NULL, CITY_MAP_MIN_RADIUS_SQ);
135
136 return pcity->city_radius_sq;
137}
138
139/**********************************************************************/
142 void city_map_radius_sq_set(struct city *pcity, int radius_sq)
143{
144 fc_assert_ret(radius_sq >= CITY_MAP_MIN_RADIUS_SQ);
145 fc_assert_ret(radius_sq <= CITY_MAP_MAX_RADIUS_SQ);
146
147 pcity->city_radius_sq = radius_sq;
148}
149
150/**********************************************************************/
153 int rs_max_city_radius_sq(void)
154{
155 int max_rad = game.info.init_city_radius_sq
156 + effect_cumulative_max(EFT_CITY_RADIUS_SQ, NULL, 0);
157
158 return MIN(max_rad, CITY_MAP_MAX_RADIUS_SQ);
159}
160
161/**********************************************************************/
165 int city_map_tiles(int city_radius_sq)
166{
167 if (city_radius_sq == CITY_MAP_CENTER_RADIUS_SQ) {
168 /* special case: city center; first tile of the city map */
169 return 0;
170 }
171
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);
174
175 return city_map_numtiles[city_radius_sq];
176}
177
178/**********************************************************************/
182 bool is_valid_city_coords(const int city_radius_sq, const int city_map_x,
183 const int city_map_y)
184{
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:
187 *
188 * - rectangular (max radius = 5; max squared radius = 26)
189 *
190 * 0 1 2 3 4 5 6 7 8 9 10
191 *
192 * 0 26 25 26 -5
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
202 * 10 26 25 26 +5
203 *
204 * -5 -4 -3 -2 -1 +0 +1 +2 +3 +4 +5
205 *
206 * - hexagonal (max radius = 5; max squared radius = 26)
207 *
208 * 0 1 2 3 4 5 6 7 8 9 10
209 *
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
221 *
222 * -5 -4 -3 -2 -1 +0 +1 +2 +3 +4 +5
223 *
224 * The following tables show the tiles per city radii / squared city radii.
225 * '-' indicates no change compared to the previous value
226 *
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 | -
232 *
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 | - | - | - | -
238 *
239 * radius | | | | | | 5
240 * radius_sq | 21 | 22 | 23 | 24 | 25 | 26
241 * ------------------+----+----+----+----+----+----
242 * tiles rectangular | - | - | - | - | 81 | 89
243 * tiles hexagonal | - | - | - | - | 91 | -
244 *
245 * So radius_sq == 5 (radius == 2) corresponds to the "traditional"
246 * used city map.
247 */
248 int dist = map_vector_to_sq_distance(CITY_ABS2REL(city_map_x),
249 CITY_ABS2REL(city_map_y));
250
251 return dist <= city_radius_sq;
252}
253
254/**********************************************************************/
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)
262{
263 map_distance_vector(city_map_x, city_map_y, city_center, map_tile);
264
265 *city_map_x += CITY_MAP_MAX_RADIUS;
266 *city_map_y += CITY_MAP_MAX_RADIUS;
267
268 return is_valid_city_coords(city_radius_sq, *city_map_x, *city_map_y);
269}
270
271/**********************************************************************/
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)
278{
279 return city_tile_to_city_map(city_map_x, city_map_y,
280 city_map_radius_sq_get(pcity), pcity->tile,
281 map_tile);
282}
283
284/**********************************************************************/
287 bool city_map_includes_tile(const struct city *const pcity,
288 const struct tile *map_tile)
289{
290 int tmp_x, tmp_y;
291
292 return city_base_to_city_map(&tmp_x, &tmp_y, pcity, map_tile);
293}
294
295/**********************************************************************/
299 struct tile *city_map_to_tile(const struct civ_map *nmap,
300 const struct tile *city_center,
301 int city_radius_sq, int city_map_x,
302 int city_map_y)
303{
304 int tile_x, tile_y;
305
306 fc_assert_ret_val(is_valid_city_coords(city_radius_sq, city_map_x,
307 city_map_y), NULL);
308
309 index_to_map_pos(&tile_x, &tile_y, tile_index(city_center));
310 tile_x += CITY_ABS2REL(city_map_x);
311 tile_y += CITY_ABS2REL(city_map_y);
312
313 return map_pos_to_tile(nmap, tile_x, tile_y);
314}
315
316/**********************************************************************/
319 static int cmp(int v1, int v2)
320{
321 if (v1 == v2) {
322 return 0;
323 } else if (v1 > v2) {
324 return 1;
325 } else {
326 return -1;
327 }
328}
329
330/**********************************************************************/
337 int compare_iter_index(const void *a, const void *b)
338{
339 const struct iter_index *index1 = a, *index2 = b;
340 int value;
341
342 value = cmp(index1->dist, index2->dist);
343 if (value != 0) {
344 return value;
345 }
346
347 value = cmp(index1->dx, index2->dx);
348 if (value != 0) {
349 return value;
350 }
351
352 value = cmp(index1->dy, index2->dy);
353 fc_assert(0 != value);
354 return value;
355}
356
357/**********************************************************************/
362#define CITYLOG_MAX_VAL 9999 /* maximal value displayed in the citylog */
363 static char *citylog_map_line(int y, int city_radius_sq, int *city_map_data)
364{
365 int x, mindex;
366 static char citylog[128], tmp[8];
367
368 fc_assert_ret_val(city_map_data != NULL, NULL);
369
370 /* print y coordinates (absolut) */
371 fc_snprintf(citylog, sizeof(citylog), "%2d ", y);
372
373 /* print values */
374 for (x = 0; x < CITY_MAP_MAX_SIZE; x++) {
375 if (is_valid_city_coords(city_radius_sq, x, y)) {
376 mindex = city_tile_xy_to_index(x, y, city_radius_sq);
377 /* show values between -10000 and +10000 */
378 if (city_map_data[mindex] >= -CITYLOG_MAX_VAL
379 && city_map_data[mindex] <= CITYLOG_MAX_VAL) {
380 fc_snprintf(tmp, sizeof(tmp), "%5d", city_map_data[mindex]);
381 sz_strlcat(citylog, tmp);
382 } else {
383 fc_snprintf(tmp, sizeof(tmp), " ####");
384 sz_strlcat(citylog, tmp);
385 }
386 } else {
387 fc_snprintf(tmp, sizeof(tmp), " ");
388 sz_strlcat(citylog, tmp);
389 }
390 }
391
392 /* print y coordinates (relativ) */
393 fc_snprintf(tmp, sizeof(tmp), " %+4d", CITY_ABS2REL(y));
394 sz_strlcat(citylog, tmp);
395
396 return citylog;
397}
398#undef CITYLOG_MAX_VAL
399
400/**********************************************************************/
405 void citylog_map_data(enum log_level level, int radius_sq, int *map_data)
406{
407 int x, y;
408 char line[128], tmp[8];
409
410 if (!log_do_output_for_level(level)) {
411 return;
412 }
413
414 log_base(level, "(max squared city radius = %d)", CITY_MAP_MAX_RADIUS_SQ);
415
416 /* print x coordinates (absolut) */
417 fc_snprintf(line, sizeof(line), " ");
418 for (x = 0; x < CITY_MAP_MAX_SIZE; x++) {
419 fc_snprintf(tmp, sizeof(tmp), "%+5d", x);
420 sz_strlcat(line, tmp);
421 }
422 log_base(level, "%s", line);
423
424 for (y = 0; y < CITY_MAP_MAX_SIZE; y++) {
425 log_base(level, "%s", citylog_map_line(y, radius_sq, map_data));
426 }
427
428 /* print x coordinates (relativ) */
429 fc_snprintf(line, sizeof(line), " ");
430 for (x = 0; x < CITY_MAP_MAX_SIZE; x++) {
431 fc_snprintf(tmp, sizeof(tmp), "%+5d", CITY_ABS2REL(x));
432 sz_strlcat(line, tmp);
433 }
434 log_base(level, "%s", line);
435}
436
437/**********************************************************************/
440 void citylog_map_workers(enum log_level level, struct city *pcity)
441{
442 int *city_map_data = NULL;
443 const struct civ_map *nmap = &(wld.map);
444
445 fc_assert_ret(pcity != NULL);
446
447 if (!log_do_output_for_level(level)) {
448 return;
449 }
450
451 city_map_data = fc_calloc(city_map_tiles(city_map_radius_sq_get(pcity)),
452 sizeof(*city_map_data));
453
454 city_map_iterate(city_map_radius_sq_get(pcity), cindex, x, y) {
455 struct tile *ptile = city_map_to_tile(nmap,
456 city_tile(pcity),
457 city_map_radius_sq_get(pcity),
458 x, y);
459 city_map_data[cindex] = (ptile && tile_worked(ptile) == pcity)
460 ? (is_free_worked_index(cindex) ? 2 : 1) : 0;
461 } city_map_iterate_end;
462
463 log_base(level, "[%s (%d)] workers map:", city_name_get(pcity), pcity->id);
464 citylog_map_data(level, city_map_radius_sq_get(pcity), city_map_data);
465 FC_FREE(city_map_data);
466}
467
468#ifdef FREECIV_DEBUG
469/**********************************************************************/
472static void citylog_map_index(enum log_level level)
473{
474 int *city_map_data = NULL;
475
476 if (!log_do_output_for_level(level)) {
477 return;
478 }
479
480 city_map_data = fc_calloc(city_map_tiles(CITY_MAP_MAX_RADIUS_SQ),
481 sizeof(*city_map_data));
482
483 city_map_iterate(CITY_MAP_MAX_RADIUS_SQ, cindex, x, y) {
484 city_map_data[cindex] = cindex;
485 } city_map_iterate_end;
486
487 log_debug("city map index:");
488 citylog_map_data(level, CITY_MAP_MAX_RADIUS_SQ, city_map_data);
489 FC_FREE(city_map_data);
490}
491
492/**********************************************************************/
495static void citylog_map_radius_sq(enum log_level level)
496{
497 int *city_map_data = NULL;
498
499 if (!log_do_output_for_level(level)) {
500 return;
501 }
502
503 city_map_data = fc_calloc(city_map_tiles(CITY_MAP_MAX_RADIUS_SQ),
504 sizeof(*city_map_data));
505
506 city_map_iterate(CITY_MAP_MAX_RADIUS_SQ, cindex, x, y) {
507 city_map_data[cindex] = map_vector_to_sq_distance(CITY_ABS2REL(x),
508 CITY_ABS2REL(y));
509 } city_map_iterate_end;
510
511 log_debug("city map squared radius:");
512 citylog_map_data(level, CITY_MAP_MAX_RADIUS_SQ, city_map_data);
513 FC_FREE(city_map_data);
514}
515#endif /* FREECIV_DEBUG */
516
517/**********************************************************************/
521 void generate_city_map_indices(void)
522{
523 int i, dx, dy, city_x, city_y, dist, city_count_tiles = 0;
524 struct iter_index city_map_index_tmp[CITY_MAP_MAX_SIZE
525 * CITY_MAP_MAX_SIZE];
526
527 /* initialise map information for each city radii */
528 for (i = 0; i <= CITY_MAP_MAX_RADIUS_SQ; i++) {
529 city_map_numtiles[i] = 0; /* will be set below */
530 }
531
532 /* We don't use city-map iterators in this function because they may
533 * rely on the indices that have not yet been generated. Furthermore,
534 * we don't know the number of tiles within the city radius, so we need
535 * an temporary city_map_index array. Its content will be copied into
536 * the real array below. */
537 for (dx = -CITY_MAP_MAX_RADIUS; dx <= CITY_MAP_MAX_RADIUS; dx++) {
538 for (dy = -CITY_MAP_MAX_RADIUS; dy <= CITY_MAP_MAX_RADIUS; dy++) {
539 dist = map_vector_to_sq_distance(dx, dy);
540
541 if (dist <= CITY_MAP_MAX_RADIUS_SQ) {
542 city_map_index_tmp[city_count_tiles].dx = dx;
543 city_map_index_tmp[city_count_tiles].dy = dy;
544 city_map_index_tmp[city_count_tiles].dist = dist;
545
546 for (i = CITY_MAP_MAX_RADIUS_SQ; i >= 0; i--) {
547 if (dist <= i) {
548 /* increase number of tiles within this squared city radius */
549 city_map_numtiles[i]++;
550 }
551 }
552
553 city_count_tiles++;
554 }
555
556 /* Initialise city_map_xy. -1 defines a invalid city map positions. */
557 city_map_xy[CITY_REL2ABS(dx)][CITY_REL2ABS(dy)] = -1;
558 }
559 }
560
561 fc_assert(NULL == city_map_index);
562 city_map_index = fc_malloc(city_count_tiles * sizeof(*city_map_index));
563
564 /* copy the index numbers from city_map_index_tmp into city_map_index */
565 for (i = 0; i < city_count_tiles; i++) {
566 city_map_index[i] = city_map_index_tmp[i];
567 }
568
569 qsort(city_map_index, city_count_tiles, sizeof(*city_map_index),
570 compare_iter_index);
571
572 /* set the static variable city_map_xy */
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 city_map_xy[city_x][city_y] = i;
577 }
578
579#ifdef FREECIV_DEBUG
580 citylog_map_radius_sq(LOG_DEBUG);
581 citylog_map_index(LOG_DEBUG);
582
583 for (i = CITY_MAP_MIN_RADIUS_SQ; i <= CITY_MAP_MAX_RADIUS_SQ; i++) {
584 log_debug("radius_sq = %2d, tiles = %2d", i, city_map_tiles(i));
585 }
586
587 for (i = 0; i < city_count_tiles; i++) {
588 city_x = CITY_REL2ABS(city_map_index[i].dx);
589 city_y = CITY_REL2ABS(city_map_index[i].dy);
590 log_debug("[%2d]: (dx,dy) = (%+2d,%+2d), (x,y) = (%2d,%2d), "
591 "dist = %2d, check = %2d", i,
592 city_map_index[i].dx, city_map_index[i].dy, city_x, city_y,
593 city_map_index[i].dist, city_map_xy[city_x][city_y]);
594 }
595#endif /* FREECIV_DEBUG */
596
597 cm_init_citymap();
598}
599
600/**********************************************************************/
603 void free_city_map_index(void)
604{
605 FC_FREE(city_map_index);
606}
607
608/**********************************************************************/
613 const char *get_output_identifier(Output_type_id output)
614{
615 fc_assert_ret_val(output >= 0 && output < O_LAST, NULL);
616 return output_types[output].id;
617}
618
619/**********************************************************************/
623 const char *get_output_name(Output_type_id output)
624{
625 fc_assert_ret_val(output >= 0 && output < O_LAST, NULL);
626 return _(output_types[output].name);
627}
628
629/**********************************************************************/
632 struct output_type *get_output_type(Output_type_id output)
633{
634 fc_assert_ret_val(output >= 0 && output < O_LAST, NULL);
635 return &output_types[output];
636}
637
638/**********************************************************************/
641 Output_type_id output_type_by_identifier(const char *id)
642{
643 Output_type_id o;
644
645 for (o = 0; o < O_LAST; o++) {
646 if (fc_strcasecmp(output_types[o].id, id) == 0) {
647 return o;
648 }
649 }
650
651 return O_LAST;
652}
653
654/**********************************************************************/
657 const char *city_improvement_name_translation(const struct city *pcity,
658 const struct impr_type *pimprove)
659{
660 static char buffer[256];
661 const char *state = NULL;
662
663 if (is_great_wonder(pimprove)) {
664 if (great_wonder_is_available(pimprove)) {
665 state = Q_("?wonder:W");
666 } else if (great_wonder_is_destroyed(pimprove)) {
667 state = Q_("?destroyed:D");
668 } else {
669 state = Q_("?built:B");
670 }
671 }
672 if (pcity) {
673 struct player *pplayer = city_owner(pcity);
674
675 if (improvement_obsolete(pplayer, pimprove, pcity)) {
676 state = Q_("?obsolete:O");
677 } else if (is_improvement_redundant(pcity, pimprove)) {
678 state = Q_("?redundant:*");
679 }
680 }
681
682 if (state) {
683 fc_snprintf(buffer, sizeof(buffer), "%s(%s)",
684 improvement_name_translation(pimprove), state);
685 return buffer;
686 } else {
687 return improvement_name_translation(pimprove);
688 }
689}
690
691/**********************************************************************/
694 const char *city_production_name_translation(const struct city *pcity)
695{
696 static char buffer[256];
697
698 switch (pcity->production.kind) {
699 case VUT_IMPROVEMENT:
700 return city_improvement_name_translation(pcity, pcity->production.value.building);
701 default:
702 /* fallthru */
703 break;
704 };
705 return universal_name_translation(&pcity->production, buffer, sizeof(buffer));
706}
707
708/**********************************************************************/
711 bool city_production_has_flag(const struct city *pcity,
712 enum impr_flag_id flag)
713{
714 return VUT_IMPROVEMENT == pcity->production.kind
715 && improvement_has_flag(pcity->production.value.building, flag);
716}
717
718/**********************************************************************/
721 int city_production_build_shield_cost(const struct city *pcity)
722{
723 return universal_build_shield_cost(pcity, &pcity->production);
724}
725
726/**********************************************************************/
731 bool city_production_build_units(const struct city *pcity,
732 bool add_production, int *num_units)
733{
734 const struct unit_type *utype;
735 struct universal target;
736 int build_slots = city_build_slots(pcity);
737 int shields_left = pcity->shield_stock;
738 int unit_shield_cost, i;
739
740 fc_assert_ret_val(num_units != NULL, FALSE);
741 (*num_units) = 0;
742
743 if (pcity->production.kind != VUT_UTYPE) {
744 /* not a unit as the current production */
745 return FALSE;
746 }
747
748 utype = pcity->production.value.utype;
749 if (utype_pop_value(utype, pcity) != 0 || utype_has_flag(utype, UTYF_UNIQUE)) {
750 /* unit with population cost or unique unit means that only one unit can
751 * be build */
752 (*num_units)++;
753 return FALSE;
754 }
755
756 if (add_production) {
757 shields_left += pcity->prod[O_SHIELD];
758 }
759
760 unit_shield_cost = utype_build_shield_cost(pcity, NULL, utype);
761
762 for (i = 0; i < build_slots; i++) {
763 if (shields_left < unit_shield_cost) {
764 /* not enough shields */
765 break;
766 }
767
768 (*num_units)++;
769 shields_left -= unit_shield_cost;
770
771 if (worklist_length(&pcity->worklist) > i) {
772 (void) worklist_peek_ith(&pcity->worklist, &target, i);
773 if (target.kind != VUT_UTYPE
774 || utype_index(target.value.utype) != utype_index(utype)) {
775 /* stop if there is a build target in the worklist not equal to the
776 * unit we build */
777 break;
778 }
779 }
780 }
781
782 return TRUE;
783}
784
785/************************************************************************/
788 int city_production_unit_veteran_level(struct city *pcity,
789 const struct unit_type *punittype)
790{
791 int levels = get_unittype_bonus(city_owner(pcity), pcity->tile, punittype,
792 NULL, EFT_VETERAN_BUILD);
793 int max_levels = utype_veteran_levels(punittype) - 1;
794
795 levels = CLIP(0, levels, max_levels);
796
797 return levels;
798}
799
800/**********************************************************************/
804 int city_production_turns_to_build(const struct city *pcity,
805 bool include_shield_stock)
806{
807 return city_turns_to_build(pcity, &pcity->production, include_shield_stock);
808}
809
810/**********************************************************************/
814 bool can_city_build_improvement_direct(const struct city *pcity,
815 const struct impr_type *pimprove)
816{
817 if (!can_player_build_improvement_direct(city_owner(pcity), pimprove)) {
818 return FALSE;
819 }
820
821 if (city_has_building(pcity, pimprove)) {
822 return FALSE;
823 }
824
825 return are_reqs_active(&(const struct req_context) {
826 .player = city_owner(pcity),
827 .city = pcity,
828 .tile = pcity->tile,
829 },
830 NULL,
831 &(pimprove->reqs), RPT_CERTAIN);
832}
833
834/**********************************************************************/
838 bool can_city_build_improvement_now(const struct city *pcity,
839 const struct impr_type *pimprove)
840{
841 if (!can_city_build_improvement_direct(pcity, pimprove)) {
842 return FALSE;
843 }
844 if (improvement_obsolete(city_owner(pcity), pimprove, pcity)) {
845 return FALSE;
846 }
847
848 return TRUE;
849}
850
851/**********************************************************************/
855 bool can_city_build_improvement_later(const struct city *pcity,
856 const struct impr_type *pimprove)
857{
858 const struct req_context city_ctxt = {
859 .player = city_owner(pcity),
860 .city = pcity,
861 .tile = city_tile(pcity),
862 };
863
864 /* Can the _player_ ever build this improvement? */
865 /* NOTE: It checks for obsoletion player-level. What aboult checking
866 * for it city-level? That may unlist from a worklist some things
867 * we'll be able to switch to after e.g. selling something else */
868 if (!can_player_build_improvement_later(city_owner(pcity), pimprove)) {
869 return FALSE;
870 }
871
872 /* Check for requirements that aren't met and that are unchanging (so
873 * they can never be met). */
874 requirement_vector_iterate(&pimprove->reqs, preq) {
875 if (is_req_preventing(&city_ctxt, NULL, preq, RPT_POSSIBLE)) {
876 return FALSE;
877 }
878 } requirement_vector_iterate_end;
879
880 return TRUE;
881}
882
883/**********************************************************************/
887 bool can_city_build_unit_direct(const struct city *pcity,
888 const struct unit_type *punittype)
889{
890 if (!can_player_build_unit_direct(city_owner(pcity), punittype)) {
891 return FALSE;
892 }
893
894 /* Check unit build requirements. */
895 if (!are_reqs_active(&(const struct req_context) {
896 .player = city_owner(pcity),
897 .city = pcity,
898 .tile = city_tile(pcity),
899 .unittype = punittype,
900 },
901 NULL,
902 &punittype->build_reqs, RPT_CERTAIN)) {
903 return FALSE;
904 }
905
906 /* You can't build naval units inland. */
907 if (!uclass_has_flag(utype_class(punittype), UCF_BUILD_ANYWHERE)
908 && !is_native_near_tile(&(wld.map), utype_class(punittype),
909 pcity->tile)) {
910 return FALSE;
911 }
912
913 if (punittype->city_slots > 0
914 && city_unit_slots_available(pcity) < punittype->city_slots) {
915 return FALSE;
916 }
917
918 return TRUE;
919}
920
921/**********************************************************************/
925 bool can_city_build_unit_now(const struct city *pcity,
926 const struct unit_type *punittype)
927{
928 if (!can_city_build_unit_direct(pcity, punittype)) {
929 return FALSE;
930 }
931 while ((punittype = punittype->obsoleted_by) != U_NOT_OBSOLETED) {
932 if (can_player_build_unit_direct(city_owner(pcity), punittype)) {
933 return FALSE;
934 }
935 }
936 return TRUE;
937}
938
939/**********************************************************************/
943 bool can_city_build_unit_later(const struct city *pcity,
944 const struct unit_type *punittype)
945{
946 /* Can the _player_ ever build this unit? */
947 if (!can_player_build_unit_later(city_owner(pcity), punittype)) {
948 return FALSE;
949 }
950
951 /* Some units can be built only in certain cities -- for instance,
952 ships may be built only in cities adjacent to ocean. */
953 if (!uclass_has_flag(utype_class(punittype), UCF_BUILD_ANYWHERE)
954 && !is_native_near_tile(&(wld.map), utype_class(punittype),
955 pcity->tile)) {
956 return FALSE;
957 }
958
959 return TRUE;
960}
961
962/**********************************************************************/
966 bool can_city_build_direct(const struct city *pcity,
967 const struct universal *target)
968{
969 switch (target->kind) {
970 case VUT_UTYPE:
971 return can_city_build_unit_direct(pcity, target->value.utype);
972 case VUT_IMPROVEMENT:
973 return can_city_build_improvement_direct(pcity, target->value.building);
974 default:
975 break;
976 };
977 return FALSE;
978}
979
980/**********************************************************************/
984 bool can_city_build_now(const struct city *pcity,
985 const struct universal *target)
986{
987 switch (target->kind) {
988 case VUT_UTYPE:
989 return can_city_build_unit_now(pcity, target->value.utype);
990 case VUT_IMPROVEMENT:
991 return can_city_build_improvement_now(pcity, target->value.building);
992 default:
993 break;
994 };
995 return FALSE;
996}
997
998/**********************************************************************/
1001 bool can_city_build_later(const struct city *pcity,
1002 const struct universal *target)
1003{
1004 switch (target->kind) {
1005 case VUT_UTYPE:
1006 return can_city_build_unit_later(pcity, target->value.utype);
1007 case VUT_IMPROVEMENT:
1008 return can_city_build_improvement_later(pcity, target->value.building);
1009 default:
1010 break;
1011 };
1012 return FALSE;
1013}
1014
1015/**********************************************************************/
1018 int city_unit_slots_available(const struct city *pcity)
1019{
1020 int max = get_city_bonus(pcity, EFT_UNIT_SLOTS);
1021 int current;
1022
1023 current = 0;
1024 unit_list_iterate(pcity->units_supported, punit) {
1025 current += unit_type_get(punit)->city_slots;
1026 } unit_list_iterate_end;
1027
1028 return max - current;
1029}
1030
1031/**********************************************************************/
1034 bool city_can_use_specialist(const struct city *pcity,
1035 Specialist_type_id type)
1036{
1037 return are_reqs_active(&(const struct req_context) {
1038 .player = city_owner(pcity),
1039 .city = pcity,
1040 },
1041 NULL,
1042 &specialist_by_number(type)->reqs, RPT_POSSIBLE);
1043}
1044
1045/**********************************************************************/
1048 bool city_can_change_build(const struct city *pcity)
1049{
1050 return !pcity->did_buy || pcity->shield_stock <= 0;
1051}
1052
1053/**********************************************************************/
1056 void city_choose_build_default(struct city *pcity)
1057{
1058 if (NULL == city_tile(pcity)) {
1059 /* When a "dummy" city is created with no tile, then choosing a build
1060 * target could fail. This currently might happen during map editing.
1061 * FIXME: assumes the first unit is always "valid", so check for
1062 * obsolete units elsewhere. */
1063 pcity->production.kind = VUT_UTYPE;
1064 pcity->production.value.utype = utype_by_number(0);
1065 } else {
1066 struct unit_type *u = best_role_unit(pcity, L_FIRSTBUILD);
1067
1068 if (u) {
1069 pcity->production.kind = VUT_UTYPE;
1070 pcity->production.value.utype = u;
1071 } else {
1072 bool found = FALSE;
1073
1074 /* Just pick the first available item. */
1075 improvement_iterate(pimprove) {
1076 if (can_city_build_improvement_direct(pcity, pimprove)) {
1077 found = TRUE;
1078 pcity->production.kind = VUT_IMPROVEMENT;
1079 pcity->production.value.building = pimprove;
1080 break;
1081 }
1082 } improvement_iterate_end;
1083
1084 if (!found) {
1085 unit_type_iterate(punittype) {
1086 if (can_city_build_unit_direct(pcity, punittype)) {
1087#ifndef FREECIV_NDEBUG
1088 /* Later than this, 'found' is only needed in an fc_assert() */
1089 found = TRUE;
1090#endif /* FREECIV_NDEBUG */
1091 pcity->production.kind = VUT_UTYPE;
1092 pcity->production.value.utype = punittype;
1093 }
1094 } unit_type_iterate_end;
1095 }
1096
1097 fc_assert_msg(found, "No production found for city %s!",
1098 city_name_get(pcity));
1099 }
1100 }
1101}
1102
1103/**********************************************************************/
1106 const char *city_name_get(const struct city *pcity)
1107{
1108 return (pcity->name != NULL) ? pcity->name : "City missing a name";
1109}
1110
1111/**********************************************************************/
1114 void city_name_set(struct city *pcity, const char *new_name)
1115{
1116 if (pcity->name != NULL) {
1117 free(pcity->name);
1118 }
1119
1120 if (strlen(new_name) < MAX_LEN_CITYNAME) {
1121 pcity->name = fc_strdup(new_name);
1122 } else {
1123 log_warn(_("City name \"%s\" too long"), new_name);
1124 pcity->name = fc_malloc(MAX_LEN_CITYNAME);
1125 sz_strlcpy(pcity->name, new_name);
1126 }
1127}
1128
1129/**********************************************************************/
1133 void city_size_add(struct city *pcity, int add)
1134{
1135 citizens size = city_size_get(pcity);
1136
1137 fc_assert_ret(pcity != NULL);
1138 fc_assert_ret(MAX_CITY_SIZE - size >= add);
1139
1140 /* Client sets size to zero to start stacking citizens in */
1141 fc_assert_ret(size >= -add);
1142
1143 city_size_set(pcity, size + add);
1144}
1145
1146/**********************************************************************/
1149 void city_size_set(struct city *pcity, citizens size)
1150{
1151 fc_assert_ret(pcity != NULL);
1152
1153 /* Set city size. */
1154 pcity->size = size;
1155}
1156
1157/**********************************************************************/
1160 int city_population(const struct city *pcity)
1161{
1162 /* Sum_{i=1}^{n} i == n*(n+1)/2 */
1163 return city_size_get(pcity) * (city_size_get(pcity) + 1) * 5;
1164}
1165
1166/**********************************************************************/
1170 int city_total_impr_gold_upkeep(const struct city *pcity)
1171{
1172 int gold_needed = 0;
1173
1174 if (pcity == NULL) {
1175 return 0;
1176 }
1177
1178 city_built_iterate(pcity, pimprove) {
1179 gold_needed += city_improvement_upkeep(pcity, pimprove);
1180 } city_built_iterate_end;
1181
1182 return gold_needed;
1183}
1184
1185/**********************************************************************/
1189 int city_total_unit_gold_upkeep(const struct city *pcity)
1190{
1191 int gold_needed = 0;
1192
1193 if (pcity == NULL || pcity->units_supported == NULL) {
1194 return 0;
1195 }
1196
1197 unit_list_iterate(pcity->units_supported, punit) {
1198 gold_needed += punit->upkeep[O_GOLD];
1199 } unit_list_iterate_end;
1200
1201 return gold_needed;
1202}
1203
1204/**********************************************************************/
1207 bool city_has_building(const struct city *pcity,
1208 const struct impr_type *pimprove)
1209{
1210 if (NULL == pimprove) {
1211 /* Callers should ensure that any external data is tested with
1212 * valid_improvement_by_number() */
1213 return FALSE;
1214 }
1215 return (pcity->built[improvement_index(pimprove)].turn > I_NEVER);
1216}
1217
1218/**********************************************************************/
1222 int city_improvement_upkeep(const struct city *pcity,
1223 const struct impr_type *b)
1224{
1225 int upkeep;
1226
1227 if (NULL == b) {
1228 return 0;
1229 }
1230 if (is_wonder(b)) {
1231 return 0;
1232 }
1233
1234 upkeep = b->upkeep;
1235 if (upkeep <= get_building_bonus(pcity, b, EFT_UPKEEP_FREE)) {
1236 return 0;
1237 }
1238
1239 return upkeep;
1240}
1241
1242/**********************************************************************/
1250 int city_tile_output(const struct city *pcity, const struct tile *ptile,
1251 bool is_celebrating, Output_type_id otype)
1252{
1253 int prod;
1254 const struct req_context city_ctxt = {
1255 .player = pcity ? city_owner(pcity) : NULL,
1256 .city = pcity,
1257 .tile = ptile,
1258 };
1259 struct terrain *pterrain = tile_terrain(ptile);
1260 const struct output_type *output = &output_types[otype];
1261
1262 fc_assert_ret_val(otype >= 0 && otype < O_LAST, 0);
1263
1264 if (T_UNKNOWN == pterrain) {
1265 /* Special case for the client. The server doesn't allow unknown tiles
1266 * to be worked but we don't necessarily know what player is involved. */
1267 return 0;
1268 }
1269
1270 prod = pterrain->output[otype];
1271 if (tile_resource_is_valid(ptile)) {
1272 prod += tile_resource(ptile)->data.resource->output[otype];
1273 }
1274
1275 switch (otype) {
1276 case O_SHIELD:
1277 if (pterrain->mining_shield_incr != 0) {
1278 prod += pterrain->mining_shield_incr
1279 * get_target_bonus_effects(NULL, &city_ctxt, NULL, EFT_MINING_PCT)
1280 / 100;
1281 }
1282 break;
1283 case O_FOOD:
1284 if (pterrain->irrigation_food_incr != 0) {
1285 prod += pterrain->irrigation_food_incr
1286 * get_target_bonus_effects(NULL, &city_ctxt, NULL,
1287 EFT_IRRIGATION_PCT) / 100;
1288 }
1289 break;
1290 case O_TRADE:
1291 case O_GOLD:
1292 case O_SCIENCE:
1293 case O_LUXURY:
1294 case O_LAST:
1295 break;
1296 }
1297
1298 prod += tile_roads_output_incr(ptile, otype);
1299 prod += (prod * tile_roads_output_bonus(ptile, otype) / 100);
1300
1301 prod += get_tile_output_bonus(pcity, ptile, output, EFT_OUTPUT_ADD_TILE);
1302 if (prod > 0) {
1303 int penalty_limit = get_tile_output_bonus(pcity, ptile, output,
1304 EFT_OUTPUT_PENALTY_TILE);
1305
1306 if (prod >= game.info.granularity) {
1307 prod += get_tile_output_bonus(pcity, ptile, output,
1308 EFT_OUTPUT_INC_TILE);
1309
1310 if (is_celebrating) {
1311 prod += get_tile_output_bonus(pcity, ptile, output,
1312 EFT_OUTPUT_INC_TILE_CELEBRATE);
1313 }
1314 }
1315
1316 prod += (prod
1317 * get_tile_output_bonus(pcity, ptile, output,
1318 EFT_OUTPUT_PER_TILE))
1319 / 100;
1320 if (!is_celebrating && penalty_limit > 0 && prod > penalty_limit) {
1321 if (prod <= game.info.granularity) {
1322 prod = 0;
1323 } else {
1324 prod -= game.info.granularity;
1325 }
1326 }
1327 }
1328
1329 prod -= (prod
1330 * get_tile_output_bonus(pcity, ptile, output,
1331 EFT_OUTPUT_TILE_PUNISH_PCT))
1332 / 100;
1333
1334 if (NULL != pcity && is_city_center(pcity, ptile)) {
1335 prod = MAX(prod, game.info.min_city_center_output[otype]);
1336 }
1337
1338 return prod;
1339}
1340
1341/**********************************************************************/
1351 int city_tile_output_now(const struct city *pcity, const struct tile *ptile,
1352 Output_type_id otype)
1353{
1354 return city_tile_output(pcity, ptile, city_celebrating(pcity), otype);
1355}
1356
1357/**********************************************************************/
1368 bool base_city_can_work_tile(const struct player *restriction,
1369 const struct city *pcity,
1370 const struct tile *ptile)
1371{
1372 struct player *powner = city_owner(pcity);
1373 int city_map_x, city_map_y;
1374
1375 if (NULL == ptile) {
1376 return FALSE;
1377 }
1378
1379 if (!city_base_to_city_map(&city_map_x, &city_map_y, pcity, ptile)) {
1380 return FALSE;
1381 }
1382
1383 if (NULL != restriction
1384 && TILE_UNKNOWN == tile_get_known(ptile, restriction)) {
1385 return FALSE;
1386 }
1387
1388 if (NULL != tile_owner(ptile) && tile_owner(ptile) != powner) {
1389 return FALSE;
1390 }
1391 /* TODO: civ3-like option for borders */
1392
1393 if (NULL != tile_worked(ptile) && tile_worked(ptile) != pcity) {
1394 return FALSE;
1395 }
1396
1397 if (powner == restriction
1398 && TILE_KNOWN_SEEN != tile_get_known(ptile, powner)) {
1399 return FALSE;
1400 }
1401
1402 if (!is_free_worked(pcity, ptile)
1403 && NULL != unit_occupies_tile(ptile, powner)) {
1404 return FALSE;
1405 }
1406
1407 if (get_city_tile_output_bonus(pcity, ptile, NULL, EFT_TILE_WORKABLE) <= 0) {
1408 return FALSE;
1409 }
1410
1411 return TRUE;
1412}
1413
1414/**********************************************************************/
1420 bool city_can_work_tile(const struct city *pcity, const struct tile *ptile)
1421{
1422 return base_city_can_work_tile(city_owner(pcity), pcity, ptile);
1423}
1424
1425/**********************************************************************/
1429 bool citymindist_prevents_city_on_tile(const struct civ_map *nmap,
1430 const struct tile *ptile)
1431{
1432 /* citymindist minimum is 1, meaning adjacent is okay */
1433 int citymindist = game.info.citymindist;
1434
1435 square_iterate(nmap, ptile, citymindist - 1, ptile1) {
1436 if (tile_city(ptile1)) {
1437 return TRUE;
1438 }
1439 } square_iterate_end;
1440
1441 return FALSE;
1442}
1443
1444/**********************************************************************/
1451 bool city_can_be_built_here(const struct tile *ptile,
1452 const struct unit *punit,
1453 bool hut_test)
1454{
1455 struct civ_map *nmap = &(wld.map);
1456
1457 if (!city_can_be_built_tile_only(nmap, ptile)) {
1458 return FALSE;
1459 }
1460
1461 if (punit == NULL) {
1462 /* The remaining checks tests if punit can found a city here */
1463 return TRUE;
1464 }
1465
1466 if (hut_test) {
1467 struct player *towner;
1468
1469 /* Huts can be found only from native tiles, owned by the unit owner.
1470 * Unlike actual city building, this behavior is not affected
1471 * by the ruleset. */
1472 if (!can_unit_exist_at_tile(nmap, punit, ptile)) {
1473 return FALSE;
1474 }
1475
1476 towner = tile_owner(ptile);
1477
1478 if (towner == NULL || towner == unit_owner(punit)) {
1479 return TRUE;
1480 }
1481
1482 return FALSE;
1483 }
1484
1485 action_by_result_iterate(paction, ACTRES_FOUND_CITY) {
1486 if (!utype_can_do_action(unit_type_get(punit), paction->id)) {
1487 /* This action can't be done by this unit type at all. */
1488 continue;
1489 }
1490
1491 /* Non native tile detection */
1492 if (!can_unit_exist_at_tile(nmap, punit, ptile)
1493 /* The ruleset may allow founding cities on non native terrain. */
1494 && !utype_can_do_act_when_ustate(unit_type_get(punit), paction->id,
1495 USP_LIVABLE_TILE, FALSE)) {
1496 /* Many rulesets allow land units to build land cities and sea units
1497 * to build ocean cities. Air units can build cities anywhere. */
1498 continue;
1499 }
1500
1501 /* Foreign tile detection. */
1502 if (tile_owner(ptile) && tile_owner(ptile) != unit_owner(punit)
1503 /* The ruleset may allow founding cities on foreign terrain. */
1504 && !can_utype_do_act_if_tgt_diplrel(unit_type_get(punit),
1505 paction->id,
1506 DRO_FOREIGN, TRUE)) {
1507 /* Cannot steal borders by settling. This has to be settled by
1508 * force of arms. */
1509 continue;
1510 }
1511
1512 return TRUE;
1513 } action_by_result_iterate_end;
1514
1515 return FALSE;
1516}
1517
1518/**********************************************************************/
1525 bool city_can_be_built_tile_only(const struct civ_map *nmap,
1526 const struct tile *ptile)
1527{
1528 if (terrain_has_flag(tile_terrain(ptile), TER_NO_CITIES)) {
1529 /* No cities on this terrain. */
1530 return FALSE;
1531 }
1532
1533 if (citymindist_prevents_city_on_tile(nmap, ptile)) {
1534 return FALSE;
1535 }
1536
1537 return TRUE;
1538}
1539
1540/**********************************************************************/
1544 bool is_capital(const struct city *pcity)
1545{
1546 return pcity->capital != CAPITAL_NOT;
1547}
1548
1549/**********************************************************************/
1552 bool is_gov_center(const struct city *pcity)
1553{
1554 return (get_city_bonus(pcity, EFT_GOV_CENTER) > 0);
1555}
1556
1557/**********************************************************************/
1561 bool city_got_defense_effect(const struct city *pcity,
1562 const struct unit_type *attacker)
1563{
1564 if (!attacker) {
1565 /* Any defense building will do */
1566 return get_city_bonus(pcity, EFT_DEFEND_BONUS) > 0;
1567 }
1568
1569 return get_unittype_bonus(city_owner(pcity), pcity->tile, attacker,
1570 NULL, EFT_DEFEND_BONUS) > 0;
1571}
1572
1573/**********************************************************************/
1579 bool city_happy(const struct city *pcity)
1580{
1581 return (city_size_get(pcity) >= game.info.celebratesize
1582 && pcity->feel[CITIZEN_ANGRY][FEELING_FINAL] == 0
1583 && pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL] == 0
1584 && pcity->feel[CITIZEN_HAPPY][FEELING_FINAL] >= (city_size_get(pcity) + 1) / 2);
1585}
1586
1587/**********************************************************************/
1591 bool city_unhappy(const struct city *pcity)
1592{
1593 return (pcity->feel[CITIZEN_HAPPY][FEELING_FINAL]
1594 < pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL]
1595 + 2 * pcity->feel[CITIZEN_ANGRY][FEELING_FINAL]);
1596}
1597
1598/**********************************************************************/
1602 bool base_city_celebrating(const struct city *pcity)
1603{
1604 return (city_size_get(pcity) >= game.info.celebratesize && pcity->was_happy);
1605}
1606
1607/**********************************************************************/
1610 bool city_celebrating(const struct city *pcity)
1611{
1612 return base_city_celebrating(pcity) && city_happy(pcity);
1613}
1614
1615/**********************************************************************/
1618 bool city_rapture_grow(const struct city *pcity)
1619{
1620 /* .rapture is checked instead of city_celebrating() because this
1621 function is called after .was_happy was updated. */
1622 return (pcity->rapture > 0 && pcity->surplus[O_FOOD] > 0
1623 && (pcity->rapture % game.info.rapturedelay) == 0
1624 && get_city_bonus(pcity, EFT_RAPTURE_GROW) > 0);
1625}
1626
1627/**********************************************************************/
1630 bool city_is_occupied(const struct city *pcity)
1631{
1632 if (is_server()) {
1633 /* The server sees the units inside the city. */
1634 return (unit_list_size(city_tile(pcity)->units) > 0);
1635 } else {
1636 /* The client gets the occupied property from the server. */
1637 return pcity->client.occupied;
1638 }
1639}
1640
1641/**********************************************************************/
1644 struct city *city_list_find_number(struct city_list *This, int id)
1645{
1646 if (id != 0) {
1647 city_list_iterate(This, pcity) {
1648 if (pcity->id == id) {
1649 return pcity;
1650 }
1651 } city_list_iterate_end;
1652 }
1653
1654 return NULL;
1655}
1656
1657/**********************************************************************/
1660 struct city *city_list_find_name(struct city_list *This, const char *name)
1661{
1662 city_list_iterate(This, pcity) {
1663 if (fc_strcasecmp(name, pcity->name) == 0) {
1664 return pcity;
1665 }
1666 } city_list_iterate_end;
1667
1668 return NULL;
1669}
1670
1671/**********************************************************************/
1676 int city_name_compare(const void *p1, const void *p2)
1677{
1678 return fc_strcasecmp((*(const struct city **) p1)->name,
1679 (*(const struct city **) p2)->name);
1680}
1681
1682/**********************************************************************/
1686 int city_style_by_translated_name(const char *s)
1687{
1688 int i;
1689
1690 for (i = 0; i < game.control.styles_count; i++) {
1691 if (0 == strcmp(city_style_name_translation(i), s)) {
1692 return i;
1693 }
1694 }
1695
1696 return -1;
1697}
1698
1699/**********************************************************************/
1703 int city_style_by_rule_name(const char *s)
1704{
1705 const char *qs = Qn_(s);
1706 int i;
1707
1708 for (i = 0; i < game.control.styles_count; i++) {
1709 if (0 == fc_strcasecmp(city_style_rule_name(i), qs)) {
1710 return i;
1711 }
1712 }
1713
1714 return -1;
1715}
1716
1717/**********************************************************************/
1721 const char *city_style_name_translation(const int style)
1722{
1723 return name_translation_get(&city_styles[style].name);
1724}
1725
1726/**********************************************************************/
1730 const char *city_style_rule_name(const int style)
1731{
1732 return rule_name_get(&city_styles[style].name);
1733}
1734
1735/* Cache of what city production caravan shields are allowed to help. */
1736 static bv_imprs caravan_helped_impr;
1737 static bv_unit_types caravan_helped_utype;
1738
1739/**********************************************************************/
1743 void city_production_caravan_shields_init(void)
1744{
1745 struct requirement prod_as_req;
1746
1747#define log_ca_s_init log_debug
1748
1749 /* Remove old data. */
1750 BV_CLR_ALL(caravan_helped_impr);
1751 BV_CLR_ALL(caravan_helped_utype);
1752
1753 /* Common for all production kinds. */
1754 prod_as_req.range = REQ_RANGE_LOCAL;
1755 prod_as_req.survives = FALSE;
1756 prod_as_req.present = TRUE;
1757
1758 /* Check improvements */
1759 prod_as_req.source.kind = VUT_IMPROVEMENT;
1760
1761 improvement_iterate(itype) {
1762 /* Check this improvement. */
1763 prod_as_req.source.value.building = itype;
1764
1765 action_enabler_list_iterate(action_enablers_for_action(
1766 ACTION_HELP_WONDER),
1767 enabler) {
1768 if (!does_req_contradicts_reqs(&prod_as_req,
1769 &(enabler->target_reqs))
1770 && !req_vec_wants_type(&(enabler->target_reqs), VUT_UTYPE)
1771 && !req_vec_wants_type(&(enabler->target_reqs), VUT_UCLASS)
1772 && !req_vec_wants_type(&(enabler->target_reqs), VUT_UTFLAG)
1773 && !req_vec_wants_type(&(enabler->target_reqs), VUT_UCFLAG)) {
1774 /* This improvement kind can receive caravan shields. */
1775
1776 BV_SET(caravan_helped_impr, improvement_index(itype));
1777
1778 /* Move on to the next improvement */
1779 break;
1780 }
1781 } action_enabler_list_iterate_end;
1782
1783 log_ca_s_init("Help Wonder: %s for %s",
1784 (BV_ISSET(caravan_helped_impr, improvement_index(itype))
1785 ? "possible" : "impossible"),
1786 improvement_rule_name(itype));
1787 } improvement_iterate_end;
1788
1789 /* Check units. */
1790 prod_as_req.source.kind = VUT_UTYPE;
1791
1792 unit_type_iterate(putype) {
1793 /* Check this utype. */
1794 prod_as_req.source.value.utype = putype;
1795
1796 action_enabler_list_iterate(action_enablers_for_action(
1797 ACTION_HELP_WONDER),
1798 enabler) {
1799 if (!does_req_contradicts_reqs(&prod_as_req,
1800 &(enabler->target_reqs))
1801 && !req_vec_wants_type(&(enabler->target_reqs), VUT_IMPROVEMENT)
1802 && !req_vec_wants_type(&(enabler->target_reqs), VUT_IMPR_GENUS)) {
1803 /* This unit type kind can receive caravan shields. */
1804
1805 BV_SET(caravan_helped_utype, utype_index(putype));
1806
1807 /* Move on to the next unit type */
1808 break;
1809 }
1810 } action_enabler_list_iterate_end;
1811
1812 log_ca_s_init("Help Wonder: %s for %s",
1813 (BV_ISSET(caravan_helped_utype, utype_index(putype))
1814 ? "possible" : "impossible"),
1815 utype_rule_name(putype));
1816 } unit_type_iterate_end;
1817
1818#undef log_ca_s_init
1819}
1820
1821/**********************************************************************/
1825 bool city_production_gets_caravan_shields(const struct universal *tgt)
1826{
1827 switch (tgt->kind) {
1828 case VUT_IMPROVEMENT:
1829 return BV_ISSET(caravan_helped_impr,
1830 improvement_index(tgt->value.building));
1831 case VUT_UTYPE:
1832 return BV_ISSET(caravan_helped_utype,
1833 utype_index(tgt->value.utype));
1834 default:
1835 fc_assert(FALSE);
1836 return FALSE;
1837 };
1838}
1839
1840/**********************************************************************/
1851 int city_change_production_penalty(const struct city *pcity,
1852 const struct universal *target)
1853{
1854 int shield_stock_after_adjustment;
1855 enum production_class_type orig_class;
1856 enum production_class_type new_class;
1857 int unpenalized_shields = 0, penalized_shields = 0;
1858
1859 switch (pcity->changed_from.kind) {
1860 case VUT_IMPROVEMENT:
1861 if (is_wonder(pcity->changed_from.value.building)) {
1862 orig_class = PCT_WONDER;
1863 } else {
1864 orig_class = PCT_NORMAL_IMPROVEMENT;
1865 }
1866 break;
1867 case VUT_UTYPE:
1868 orig_class = PCT_UNIT;
1869 break;
1870 default:
1871 orig_class = PCT_LAST;
1872 break;
1873 };
1874
1875 switch (target->kind) {
1876 case VUT_IMPROVEMENT:
1877 if (is_wonder(target->value.building)) {
1878 new_class = PCT_WONDER;
1879 } else {
1880 new_class = PCT_NORMAL_IMPROVEMENT;
1881 }
1882 break;
1883 case VUT_UTYPE:
1884 new_class = PCT_UNIT;
1885 break;
1886 default:
1887 new_class = PCT_LAST;
1888 break;
1889 };
1890
1891 /* Changing production is penalized under certain circumstances. */
1892 if (orig_class == new_class
1893 || orig_class == PCT_LAST) {
1894 /* There's never a penalty for building something of the same class. */
1895 unpenalized_shields = pcity->before_change_shields;
1896 } else if (city_built_last_turn(pcity)) {
1897 /* Surplus shields from the previous production won't be penalized if
1898 * you change production on the very next turn. But you can only use
1899 * up to the city's surplus amount of shields in this way. */
1900 unpenalized_shields = MIN(pcity->last_turns_shield_surplus,
1901 pcity->before_change_shields);
1902 penalized_shields = pcity->before_change_shields - unpenalized_shields;
1903 } else {
1904 /* Penalize 50% of the production. */
1905 penalized_shields = pcity->before_change_shields;
1906 }
1907
1908 /* Do not put penalty on these. It shouldn't matter whether you disband unit
1909 before or after changing production...*/
1910 unpenalized_shields += pcity->disbanded_shields;
1911
1912 /* Caravan shields are penalized (just as if you disbanded the caravan)
1913 * if you're not building a wonder. */
1914 if (city_production_gets_caravan_shields(target)) {
1915 unpenalized_shields += pcity->caravan_shields;
1916 } else {
1917 penalized_shields += pcity->caravan_shields;
1918 }
1919
1920 shield_stock_after_adjustment =
1921 unpenalized_shields + penalized_shields / 2;
1922
1923 return shield_stock_after_adjustment;
1924}
1925
1926/**********************************************************************/
1930 int city_turns_to_build(const struct city *pcity,
1931 const struct universal *target,
1932 bool include_shield_stock)
1933{
1934 int city_shield_surplus = pcity->surplus[O_SHIELD];
1935 int city_shield_stock = include_shield_stock ?
1936 city_change_production_penalty(pcity, target) : 0;
1937 int cost = universal_build_shield_cost(pcity, target);
1938
1939 if (target->kind == VUT_IMPROVEMENT
1940 && is_great_wonder(target->value.building)
1941 && !great_wonder_is_available(target->value.building)) {
1942 return FC_INFINITY;
1943 }
1944
1945 if (include_shield_stock && (city_shield_stock >= cost)) {
1946 return 1;
1947 } else if (city_shield_surplus > 0) {
1948 return (cost - city_shield_stock - 1) / city_shield_surplus + 1;
1949 } else {
1950 return FC_INFINITY;
1951 }
1952}
1953
1954/**********************************************************************/
1961 int city_turns_to_grow(const struct city *pcity)
1962{
1963 if (pcity->surplus[O_FOOD] > 0) {
1964 return (city_granary_size(city_size_get(pcity)) - pcity->food_stock +
1965 pcity->surplus[O_FOOD] - 1) / pcity->surplus[O_FOOD];
1966 } else if (pcity->surplus[O_FOOD] < 0) {
1967 /* turns before famine loss */
1968 return -1 + (pcity->food_stock / pcity->surplus[O_FOOD]);
1969 } else {
1970 return FC_INFINITY;
1971 }
1972}
1973
1974/**********************************************************************/
1977 bool city_can_grow_to(const struct city *pcity, int pop_size)
1978{
1979 return (get_city_bonus(pcity, EFT_SIZE_UNLIMIT) > 0
1980 || pop_size <= get_city_bonus(pcity, EFT_SIZE_ADJ));
1981}
1982
1983/**********************************************************************/
1986 struct city *tile_enemy_city(const struct tile *ptile,
1987 const struct player *pplayer)
1988{
1989 struct city *pcity = tile_city(ptile);
1990
1991 if (pcity != NULL && pplayers_at_war(pplayer, city_owner(pcity))) {
1992 return pcity;
1993 }
1994
1995 return NULL;
1996}
1997
1998/**********************************************************************/
2001 struct city *tile_allied_city(const struct tile *ptile,
2002 const struct player *pplayer)
2003{
2004 struct city *pcity = tile_city(ptile);
2005
2006 if (pcity != NULL && pplayers_allied(pplayer, city_owner(pcity))) {
2007 return pcity;
2008 }
2009
2010 return NULL;
2011}
2012
2013/**********************************************************************/
2016 struct city *tile_non_attack_city(const struct tile *ptile,
2017 const struct player *pplayer)
2018{
2019 struct city *pcity = tile_city(ptile);
2020
2021 if (pcity != NULL && pplayers_non_attack(pplayer, city_owner(pcity))) {
2022 return pcity;
2023 }
2024
2025 return NULL;
2026}
2027
2028/**********************************************************************/
2031 struct city *tile_non_allied_city(const struct tile *ptile,
2032 const struct player *pplayer)
2033{
2034 struct city *pcity = tile_city(ptile);
2035
2036 if (pcity != NULL && !pplayers_allied(pplayer, city_owner(pcity))) {
2037 return pcity;
2038 }
2039
2040 return NULL;
2041}
2042
2043/**********************************************************************/
2047 bool is_unit_near_a_friendly_city(const struct unit *punit)
2048{
2049 return is_friendly_city_near(unit_owner(punit), unit_tile(punit));
2050}
2051
2052/**********************************************************************/
2056 bool is_friendly_city_near(const struct player *owner,
2057 const struct tile *ptile)
2058{
2059 square_iterate(&(wld.map), ptile, 3, ptile1) {
2060 struct city *pcity = tile_city(ptile1);
2061
2062 if (pcity && pplayers_allied(owner, city_owner(pcity))) {
2063 return TRUE;
2064 }
2065 } square_iterate_end;
2066
2067 return FALSE;
2068}
2069
2070/**********************************************************************/
2074 bool city_exists_within_max_city_map(const struct tile *ptile,
2075 bool may_be_on_center)
2076{
2077 const struct civ_map *nmap = &(wld.map);
2078
2079 city_tile_iterate(nmap, CITY_MAP_MAX_RADIUS_SQ, ptile, ptile1) {
2080 if (may_be_on_center || !same_pos(ptile, ptile1)) {
2081 if (tile_city(ptile1)) {
2082 return TRUE;
2083 }
2084 }
2085 } city_tile_iterate_end;
2086
2087 return FALSE;
2088}
2089
2090/**********************************************************************/
2095 int city_granary_size(int city_size)
2096{
2097 int food_inis = game.info.granary_num_inis;
2098 int food_inc = game.info.granary_food_inc;
2099 int base_value;
2100
2101 /* If the city has no citizens, there is no granary. */
2102 if (city_size == 0) {
2103 return 0;
2104 }
2105
2106 /* Granary sizes for the first food_inis citizens are given directly.
2107 * After that we increase the granary size by food_inc per citizen. */
2108 if (city_size > food_inis) {
2109 base_value = game.info.granary_food_ini[food_inis - 1];
2110 base_value += food_inc * (city_size - food_inis);
2111 } else {
2112 base_value = game.info.granary_food_ini[city_size - 1];
2113 }
2114
2115 return MAX(base_value * game.info.foodbox / 100, 1);
2116}
2117
2118/**********************************************************************/
2123 static int player_base_citizen_happiness(const struct player *pplayer)
2124{
2125 int cities = city_list_size(pplayer->cities);
2126 int content = get_player_bonus(pplayer, EFT_CITY_UNHAPPY_SIZE);
2127 int basis = get_player_bonus(pplayer, EFT_EMPIRE_SIZE_BASE);
2128 int step = get_player_bonus(pplayer, EFT_EMPIRE_SIZE_STEP);
2129
2130 if (basis + step <= 0) {
2131 /* Value of zero means effect is inactive */
2132 return content;
2133 }
2134
2135 if (cities > basis) {
2136 content--;
2137 if (step != 0) {
2138 /* the first penalty is at (basis + 1) cities;
2139 the next is at (basis + step + 1), _not_ (basis + step) */
2140 content -= (cities - basis - 1) / step;
2141 }
2142 }
2143 return content;
2144}
2145
2146/**********************************************************************/
2149 citizens player_content_citizens(const struct player *pplayer)
2150{
2151 int content = player_base_citizen_happiness(pplayer);
2152
2153 return CLIP(0, content, MAX_CITY_SIZE);
2154}
2155
2156/**********************************************************************/
2159 citizens player_angry_citizens(const struct player *pplayer)
2160{
2161 if (!game.info.angrycitizen) {
2162 return 0;
2163 } else {
2164 /* Create angry citizens only if we have a negative number of possible
2165 * content citizens. This can happen when empires grow really big. */
2166 int content = player_base_citizen_happiness(pplayer);
2167
2168 return CLIP(0, -content, MAX_CITY_SIZE);
2169 }
2170}
2171
2172/**********************************************************************/
2175 int get_final_city_output_bonus(const struct city *pcity, Output_type_id otype)
2176{
2177 struct output_type *output = &output_types[otype];
2178 int bonus1 = 100 + get_city_tile_output_bonus(pcity, NULL, output,
2179 EFT_OUTPUT_BONUS);
2180 int bonus2 = 100 + get_city_tile_output_bonus(pcity, NULL, output,
2181 EFT_OUTPUT_BONUS_2);
2182
2183 return MAX(bonus1 * bonus2 / 100, 0);
2184}
2185
2186/**********************************************************************/
2190 int get_city_tithes_bonus(const struct city *pcity)
2191{
2192 int tithes_bonus = 0;
2193
2194 if (get_city_bonus(pcity, EFT_HAPPINESS_TO_GOLD) <= 0) {
2195 return 0;
2196 }
2197
2198 tithes_bonus += get_city_bonus(pcity, EFT_MAKE_CONTENT);
2199 tithes_bonus += get_city_bonus(pcity, EFT_FORCE_CONTENT);
2200
2201 return tithes_bonus;
2202}
2203
2204/**********************************************************************/
2208 void add_tax_income(const struct player *pplayer, int trade, int *output)
2209{
2210 const int SCIENCE = 0, TAX = 1, LUXURY = 2;
2211 unsigned rates[3];
2212 int result[3];
2213
2214 if (game.info.changable_tax) {
2215 rates[SCIENCE] = pplayer->economic.science;
2216 rates[LUXURY] = pplayer->economic.luxury;
2217 rates[TAX] = 100 - rates[SCIENCE] - rates[LUXURY];
2218 } else {
2219 rates[SCIENCE] = game.info.forced_science;
2220 rates[LUXURY] = game.info.forced_luxury;
2221 rates[TAX] = game.info.forced_gold;
2222 }
2223
2224 /* ANARCHY */
2225 if (government_of_player(pplayer) == game.government_during_revolution) {
2226 rates[SCIENCE] = 0;
2227 rates[LUXURY] = 100;
2228 rates[TAX] = 0;
2229 }
2230
2231 distribute(trade, 3, rates, result);
2232
2233 output[O_SCIENCE] += result[SCIENCE];
2234 output[O_GOLD] += result[TAX];
2235 output[O_LUXURY] += result[LUXURY];
2236}
2237
2238/**********************************************************************/
2242 bool city_built_last_turn(const struct city *pcity)
2243{
2244 return pcity->turn_last_built + 1 >= game.info.turn;
2245}
2246
2247/**********************************************************************/
2255 static inline void get_worked_tile_output(const struct city *pcity,
2256 int *output, bool *workers_map)
2257{
2258 bool is_worked;
2259#ifdef CITY_DEBUGGING
2260 bool is_celebrating = base_city_celebrating(pcity);
2261#endif
2262 struct tile *pcenter = city_tile(pcity);
2263 const struct civ_map *nmap = &(wld.map);
2264
2265 memset(output, 0, O_LAST * sizeof(*output));
2266
2267 city_tile_iterate_index(nmap, city_map_radius_sq_get(pcity), pcenter, ptile,
2268 city_tile_index) {
2269 if (workers_map == NULL) {
2270 struct city *pwork = tile_worked(ptile);
2271
2272 is_worked = (NULL != pwork && pwork == pcity);
2273 } else {
2274 is_worked = workers_map[city_tile_index];
2275 }
2276
2277 if (is_worked) {
2278 output_type_iterate(o) {
2279#ifdef CITY_DEBUGGING
2280 /* This assertion never fails, but it's so slow that we disable
2281 * it by default. */
2282 fc_assert(city_tile_cache_get_output(pcity, city_tile_index, o)
2283 == city_tile_output(pcity, ptile, is_celebrating, o));
2284#endif /* CITY_DEBUGGING */
2285 output[o] += city_tile_cache_get_output(pcity, city_tile_index, o);
2286 } output_type_iterate_end;
2287 }
2288 } city_tile_iterate_index_end;
2289}
2290
2291/**********************************************************************/
2295 void add_specialist_output(const struct city *pcity, int *output)
2296{
2297 specialist_type_iterate(sp) {
2298 int count = pcity->specialists[sp];
2299
2300 output_type_iterate(stat_index) {
2301 int amount = get_specialist_output(pcity, sp, stat_index);
2302
2303 output[stat_index] += count * amount;
2304 } output_type_iterate_end;
2305 } specialist_type_iterate_end;
2306}
2307
2308/**********************************************************************/
2316 static inline void set_city_bonuses(struct city *pcity)
2317{
2318 output_type_iterate(o) {
2319 pcity->bonus[o] = get_final_city_output_bonus(pcity, o);
2320 } output_type_iterate_end;
2321}
2322
2323/**********************************************************************/
2333 static inline void city_tile_cache_update(struct city *pcity)
2334{
2335 bool is_celebrating = base_city_celebrating(pcity);
2336 int radius_sq = city_map_radius_sq_get(pcity);
2337 const struct civ_map *nmap = &(wld.map);
2338
2339 /* Initialize tile_cache if needed */
2340 if (pcity->tile_cache == NULL || pcity->tile_cache_radius_sq == -1
2341 || pcity->tile_cache_radius_sq != radius_sq) {
2342 pcity->tile_cache = fc_realloc(pcity->tile_cache,
2343 city_map_tiles(radius_sq)
2344 * sizeof(*(pcity->tile_cache)));
2345 pcity->tile_cache_radius_sq = radius_sq;
2346 }
2347
2348 /* Any unreal tiles are skipped - these values should have been memset
2349 * to 0 when the city was created. */
2350 city_tile_iterate_index(nmap, radius_sq, pcity->tile, ptile, city_tile_index) {
2351 output_type_iterate(o) {
2352 (pcity->tile_cache[city_tile_index]).output[o]
2353 = city_tile_output(pcity, ptile, is_celebrating, o);
2354 } output_type_iterate_end;
2355 } city_tile_iterate_index_end;
2356}
2357
2358/**********************************************************************/
2362 static inline int city_tile_cache_get_output(const struct city *pcity,
2363 int city_tile_index,
2364 enum output_type_id o)
2365{
2366 fc_assert_ret_val(pcity->tile_cache_radius_sq
2367 == city_map_radius_sq_get(pcity), 0);
2368 fc_assert_ret_val(city_tile_index < city_map_tiles_from_city(pcity), 0);
2369
2370 return (pcity->tile_cache[city_tile_index]).output[o];
2371}
2372
2373/**********************************************************************/
2376 static void set_surpluses(struct city *pcity)
2377{
2378 output_type_iterate(o) {
2379 pcity->surplus[o] = pcity->prod[o] - pcity->usage[o];
2380 } output_type_iterate_end;
2381}
2382
2383/**********************************************************************/
2386 static void happy_copy(struct city *pcity, enum citizen_feeling i)
2387{
2388 int c = 0;
2389
2390 for (; c < CITIZEN_LAST; c++) {
2391 pcity->feel[c][i] = pcity->feel[c][i - 1];
2392 }
2393}
2394
2395/**********************************************************************/
2398 static void citizen_base_mood(struct city *pcity)
2399{
2400 struct player *pplayer = city_owner(pcity);
2401 citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_BASE];
2402 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_BASE];
2403 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_BASE];
2404 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_BASE];
2405 citizens size = city_size_get(pcity);
2406 citizens spes = city_specialists(pcity);
2407
2408 /* This is the number of citizens that may start out content, depending
2409 * on empire size and game's city unhappysize. This may be bigger than
2410 * the size of the city, since this is a potential. */
2411 citizens base_content = player_content_citizens(pplayer);
2412 /* Similarly, this is the potential number of angry citizens. */
2413 citizens base_angry = player_angry_citizens(pplayer);
2414
2415 /* Create content citizens. Take specialists from their ranks. */
2416 *content = MAX(0, MIN(size, base_content) - spes);
2417
2418 /* Create angry citizens. Specialists never become angry. */
2419 fc_assert_action(base_content == 0 || base_angry == 0, *content = 0);
2420 *angry = MIN(base_angry, size - spes);
2421
2422 /* Create unhappy citizens. In the beginning, all who are not content,
2423 * specialists or angry are unhappy. This is changed by luxuries and
2424 * buildings later. */
2425 *unhappy = (size - spes - *content - *angry);
2426
2427 /* No one is born happy. */
2428 *happy = 0;
2429}
2430
2431/**********************************************************************/
2437 static inline void citizen_luxury_happy(struct city *pcity, int *luxuries)
2438{
2439 citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_LUXURY];
2440 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_LUXURY];
2441 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_LUXURY];
2442 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_LUXURY];
2443
2444 while (*luxuries >= game.info.happy_cost && *angry > 0) {
2445 /* Upgrade angry to unhappy: costs HAPPY_COST each. */
2446 (*angry)--;
2447 (*unhappy)++;
2448 *luxuries -= game.info.happy_cost;
2449 }
2450 while (*luxuries >= game.info.happy_cost && *content > 0) {
2451 /* Upgrade content to happy: costs HAPPY_COST each. */
2452 (*content)--;
2453 (*happy)++;
2454 *luxuries -= game.info.happy_cost;
2455 }
2456 while (*luxuries >= 2 * game.info.happy_cost && *unhappy > 0) {
2457 /* Upgrade unhappy to happy. Note this is a 2-level upgrade with
2458 * double the cost. */
2459 (*unhappy)--;
2460 (*happy)++;
2461 *luxuries -= 2 * game.info.happy_cost;
2462 }
2463 if (*luxuries >= game.info.happy_cost && *unhappy > 0) {
2464 /* Upgrade unhappy to content: costs HAPPY_COST each. */
2465 (*unhappy)--;
2466 (*content)++;
2467 *luxuries -= game.info.happy_cost;
2468 }
2469}
2470
2471/**********************************************************************/
2474 static inline void citizen_happy_luxury(struct city *pcity)
2475{
2476 int x = pcity->prod[O_LUXURY];
2477
2478 citizen_luxury_happy(pcity, &x);
2479}
2480
2481/**********************************************************************/
2484 static inline void citizen_content_buildings(struct city *pcity)
2485{
2486 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_EFFECT];
2487 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_EFFECT];
2488 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_EFFECT];
2489 int faces = get_city_bonus(pcity, EFT_MAKE_CONTENT);
2490
2491 /* make people content (but not happy):
2492 get rid of angry first, then make unhappy content. */
2493 while (faces > 0 && *angry > 0) {
2494 (*angry)--;
2495 (*unhappy)++;
2496 faces--;
2497 }
2498 while (faces > 0 && *unhappy > 0) {
2499 (*unhappy)--;
2500 (*content)++;
2501 faces--;
2502 }
2503}
2504
2505/**********************************************************************/
2508 static inline void citizen_happiness_nationality(struct city *pcity)
2509{
2510 citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_NATIONALITY];
2511 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_NATIONALITY];
2512 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_NATIONALITY];
2513
2514 if (game.info.citizen_nationality) {
2515 int pct = get_city_bonus(pcity, EFT_ENEMY_CITIZEN_UNHAPPY_PCT);
2516
2517 if (pct > 0) {
2518 int enemies = 0;
2519 int unhappy_inc;
2520 struct player *owner = city_owner(pcity);
2521
2522 citizens_foreign_iterate(pcity, pslot, nationality) {
2523 if (pplayers_at_war(owner, player_slot_get_player(pslot))) {
2524 enemies += nationality;
2525 }
2526 } citizens_foreign_iterate_end;
2527
2528 unhappy_inc = enemies * pct / 100;
2529
2530 /* First make content => unhappy, then happy => unhappy,
2531 * then happy => content. No-one becomes angry. */
2532 while (unhappy_inc > 0 && *content > 0) {
2533 (*content)--;
2534 (*unhappy)++;
2535 unhappy_inc--;
2536 }
2537 while (unhappy_inc > 1 && *happy > 0) {
2538 (*happy)--;
2539 (*unhappy)++;
2540 unhappy_inc -= 2;
2541 }
2542 while (unhappy_inc > 0 && *happy > 0) {
2543 (*happy)--;
2544 (*content)++;
2545 unhappy_inc--;
2546 }
2547 }
2548 }
2549}
2550
2551/**********************************************************************/
2557 static inline void citizen_happy_units(struct city *pcity)
2558{
2559 citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_MARTIAL];
2560 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_MARTIAL];
2561 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_MARTIAL];
2562 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_MARTIAL];
2563 citizens amt = pcity->martial_law;
2564
2565 /* Pacify discontent citizens through martial law. First convert
2566 * angry => unhappy, then unhappy => content. */
2567 while (amt > 0 && *angry > 0) {
2568 (*angry)--;
2569 (*unhappy)++;
2570 amt--;
2571 }
2572 while (amt > 0 && *unhappy > 0) {
2573 (*unhappy)--;
2574 (*content)++;
2575 amt--;
2576 }
2577
2578 /* Now make citizens unhappier because of military units away from home.
2579 * First make content => unhappy, then happy => unhappy,
2580 * then happy => content. */
2581 amt = pcity->unit_happy_upkeep;
2582 while (amt > 0 && *content > 0) {
2583 (*content)--;
2584 (*unhappy)++;
2585 amt--;
2586 }
2587 while (amt > 1 && *happy > 0) {
2588 (*happy)--;
2589 (*unhappy)++;
2590 amt -= 2;
2591 }
2592 while (amt > 0 && *happy > 0) {
2593 (*happy)--;
2594 (*content)++;
2595 amt--;
2596 }
2597 /* Any remaining unhappiness is lost since angry citizens aren't created
2598 * here. */
2599 /* FIXME: Why not? - Per */
2600}
2601
2602/**********************************************************************/
2605 static inline void citizen_happy_wonders(struct city *pcity)
2606{
2607 citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_FINAL];
2608 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_FINAL];
2609 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL];
2610 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_FINAL];
2611 int bonus = get_city_bonus(pcity, EFT_MAKE_HAPPY);
2612
2613 /* First create happy citizens from content, then from unhappy
2614 * citizens; we cannot help angry citizens here. */
2615 while (bonus > 0 && *content > 0) {
2616 (*content)--;
2617 (*happy)++;
2618 bonus--;
2619 }
2620 while (bonus > 1 && *unhappy > 0) {
2621 (*unhappy)--;
2622 (*happy)++;
2623 bonus -= 2;
2624 }
2625 /* The rest falls through and lets unhappy people become content. */
2626
2627 if (get_city_bonus(pcity, EFT_NO_UNHAPPY) > 0) {
2628 *content += *unhappy + *angry;
2629 *unhappy = 0;
2630 *angry = 0;
2631 return;
2632 }
2633
2634 bonus += get_city_bonus(pcity, EFT_FORCE_CONTENT);
2635
2636 /* get rid of angry first, then make unhappy content */
2637 while (bonus > 0 && *angry > 0) {
2638 (*angry)--;
2639 (*unhappy)++;
2640 bonus--;
2641 }
2642 while (bonus > 0 && *unhappy > 0) {
2643 (*unhappy)--;
2644 (*content)++;
2645 bonus--;
2646 }
2647}
2648
2649/**********************************************************************/
2653 static inline void unhappy_city_check(struct city *pcity)
2654{
2655 if (city_unhappy(pcity)) {
2656 output_type_iterate(o) {
2657 switch (output_types[o].unhappy_penalty) {
2658 case UNHAPPY_PENALTY_NONE:
2659 pcity->unhappy_penalty[o] = 0;
2660 break;
2661 case UNHAPPY_PENALTY_SURPLUS:
2662 pcity->unhappy_penalty[o] = MAX(pcity->prod[o] - pcity->usage[o], 0);
2663 break;
2664 case UNHAPPY_PENALTY_ALL_PRODUCTION:
2665 pcity->unhappy_penalty[o] = pcity->prod[o];
2666 break;
2667 }
2668
2669 pcity->prod[o] -= pcity->unhappy_penalty[o];
2670 } output_type_iterate_end;
2671 } else {
2672 memset(pcity->unhappy_penalty, 0,
2673 O_LAST * sizeof(*pcity->unhappy_penalty));
2674 }
2675}
2676
2677/**********************************************************************/
2680 int city_pollution_types(const struct city *pcity, int shield_total,
2681 int *pollu_prod, int *pollu_pop, int *pollu_mod)
2682{
2683 int prod, pop, mod;
2684
2685 /* Add one one pollution per shield, multipled by the bonus. */
2686 prod = 100 + get_city_bonus(pcity, EFT_POLLU_PROD_PCT);
2687 prod = shield_total * MAX(prod, 0) / 100;
2688
2689 /* Add one pollution per citizen for baseline combined bonus (100%). */
2690 pop = (100 + get_city_bonus(pcity, EFT_POLLU_POP_PCT))
2691 * (100 + get_city_bonus(pcity, EFT_POLLU_POP_PCT_2))
2692 / 100;
2693 pop = (city_size_get(pcity) * MAX(pop, 0)) / 100;
2694
2695 /* Then there is base pollution (usually a negative number). */
2696 mod = game.info.base_pollution;
2697
2698 if (pollu_prod) {
2699 *pollu_prod = prod;
2700 }
2701 if (pollu_pop) {
2702 *pollu_pop = pop;
2703 }
2704 if (pollu_mod) {
2705 *pollu_mod = mod;
2706 }
2707 return MAX(prod + pop + mod, 0);
2708}
2709
2710/**********************************************************************/
2714 int city_pollution(const struct city *pcity, int shield_total)
2715{
2716 return city_pollution_types(pcity, shield_total, NULL, NULL, NULL);
2717}
2718
2719/**********************************************************************/
2726 static int get_trade_illness(const struct city *pcity)
2727{
2728 float illness_trade = 0.0;
2729
2730 trade_partners_iterate(pcity, trade_city) {
2731 if (trade_city->turn_plague != -1
2732 && game.info.turn - trade_city->turn_plague < 5) {
2733 illness_trade += (float)game.info.illness_trade_infection
2734 * sqrt(1.0 * city_size_get(pcity)
2735 * city_size_get(trade_city)) / 100.0;
2736 }
2737 } trade_partners_iterate_end;
2738
2739 return (int)illness_trade;
2740}
2741
2742/**********************************************************************/
2746 static int get_city_health(const struct city *pcity)
2747{
2748 return get_city_bonus(pcity, EFT_HEALTH_PCT);
2749}
2750
2751/**********************************************************************/
2763 int city_illness_calc(const struct city *pcity, int *ill_base,
2764 int *ill_size, int *ill_trade, int *ill_pollution)
2765{
2766 int illness_size = 0, illness_trade = 0, illness_pollution = 0;
2767 int illness_base, illness_percent;
2768
2769 if (game.info.illness_on
2770 && city_size_get(pcity) > game.info.illness_min_size) {
2771 /* offset the city size by game.info.illness_min_size */
2772 int use_size = city_size_get(pcity) - game.info.illness_min_size;
2773
2774 illness_size = (int)((1.0 - exp(- (float)use_size / 10.0))
2775 * 10.0 * game.info.illness_base_factor);
2776 if (is_server()) {
2777 /* on the server we recalculate the illness due to trade as we have
2778 * all informations */
2779 illness_trade = get_trade_illness(pcity);
2780 } else {
2781 /* on the client we have to rely on the value saved within the city
2782 * struct */
2783 illness_trade = pcity->illness_trade;
2784 }
2785
2786 illness_pollution = pcity->pollution
2787 * game.info.illness_pollution_factor / 100;
2788 }
2789
2790 illness_base = illness_size + illness_trade + illness_pollution;
2791 illness_percent = 100 - get_city_health(pcity);
2792
2793 /* returning other data */
2794 if (ill_size) {
2795 *ill_size = illness_size;
2796 }
2797
2798 if (ill_trade) {
2799 *ill_trade = illness_trade;
2800 }
2801
2802 if (ill_pollution) {
2803 *ill_pollution = illness_pollution;
2804 }
2805
2806 if (ill_base) {
2807 *ill_base = illness_base;
2808 }
2809
2810 return CLIP(0, illness_base * illness_percent / 100 , 999);
2811}
2812
2813/**********************************************************************/
2816 bool city_had_recent_plague(const struct city *pcity)
2817{
2818 /* Correctly handles special case turn_plague == -1 (never) */
2819 return (pcity->turn_plague == game.info.turn);
2820}
2821
2822/**********************************************************************/
2825 int city_build_slots(const struct city *pcity)
2826{
2827 return get_city_bonus(pcity, EFT_CITY_BUILD_SLOTS);
2828}
2829
2830/**********************************************************************/
2835 int city_airlift_max(const struct city *pcity)
2836{
2837 return get_city_bonus(pcity, EFT_AIRLIFT);
2838}
2839
2840/**********************************************************************/
2846 inline void set_city_production(struct city *pcity)
2847{
2848 /* Calculate city production!
2849 *
2850 * This is a rather complicated process if we allow rules to become
2851 * more generalized. We can assume that there are no recursive dependency
2852 * loops, but there are some dependencies that do not follow strict
2853 * ordering. For instance corruption must be calculated before
2854 * trade taxes can be counted up, which must occur before the science bonus
2855 * is added on. But the calculation of corruption must include the
2856 * trade bonus. To do this without excessive special casing means that in
2857 * this case the bonuses are multiplied on twice (but only saved the second
2858 * time).
2859 */
2860
2861 output_type_iterate(o) {
2862 pcity->prod[o] = pcity->citizen_base[o];
2863 } output_type_iterate_end;
2864
2865 /* Add on special extra incomes: trade routes and tithes. */
2866 trade_routes_iterate(pcity, proute) {
2867 struct city *tcity = game_city_by_number(proute->partner);
2868 bool can_trade;
2869
2870 /* Partner city may have not yet been sent to the client, or
2871 * there's just a placeholder city with a placeholder owner
2872 * created for some tile->worked. */
2873 if (!is_server()
2874 && (tcity == NULL
2875 || city_owner(tcity)->slot == NULL)) {
2876 continue;
2877 }
2878
2879 fc_assert_action(tcity != NULL, continue);
2880
2881 can_trade = can_cities_trade(pcity, tcity);
2882
2883 if (!can_trade) {
2884 enum trade_route_type type = cities_trade_route_type(pcity, tcity);
2885 struct trade_route_settings *settings = trade_route_settings_by_type(type);
2886
2887 if (settings->cancelling == TRI_ACTIVE) {
2888 can_trade = TRUE;
2889 }
2890 }
2891
2892 if (can_trade) {
2893 int value;
2894
2895 value =
2896 trade_base_between_cities(pcity, game_city_by_number(proute->partner));
2897 proute->value = trade_from_route(pcity, proute, value);
2898 pcity->prod[O_TRADE] += proute->value
2899 * (100 + get_city_bonus(pcity, EFT_TRADE_ROUTE_PCT)) / 100;
2900 } else {
2901 proute->value = 0;
2902 }
2903 } trade_routes_iterate_end;
2904 pcity->prod[O_GOLD] += get_city_tithes_bonus(pcity);
2905
2906 /* Account for waste. Note that waste is calculated before tax income is
2907 * calculated, so if you had "science waste" it would not include taxed
2908 * science. However waste is calculated after the bonuses are multiplied
2909 * on, so shield waste will include shield bonuses. */
2910 output_type_iterate(o) {
2911 pcity->waste[o] = city_waste(pcity, o,
2912 pcity->prod[o] * pcity->bonus[o] / 100,
2913 NULL);
2914 } output_type_iterate_end;
2915
2916 /* Convert trade into science/luxury/gold, and add this on to whatever
2917 * science/luxury/gold is already there. */
2918 add_tax_income(city_owner(pcity),
2919 pcity->prod[O_TRADE] * pcity->bonus[O_TRADE] / 100
2920 - pcity->waste[O_TRADE] - pcity->usage[O_TRADE],
2921 pcity->prod);
2922
2923 /* Add on effect bonuses and waste. Note that the waste calculation
2924 * (above) already includes the bonus multiplier. */
2925 output_type_iterate(o) {
2926 pcity->prod[o] = pcity->prod[o] * pcity->bonus[o] / 100;
2927 pcity->prod[o] -= pcity->waste[o];
2928 } output_type_iterate_end;
2929}
2930
2931/**********************************************************************/
2934 int city_unit_unhappiness(struct unit *punit, int *free_unhappy)
2935{
2936 struct city *pcity;
2937 const struct unit_type *ut;
2938 struct player *plr;
2939 int happy_cost;
2940
2941 if (!punit || !free_unhappy) {
2942 return 0;
2943 }
2944
2945 pcity = game_city_by_number(punit->homecity);
2946 if (pcity == NULL) {
2947 return 0;
2948 }
2949
2950 ut = unit_type_get(punit);
2951 plr = unit_owner(punit);
2952 happy_cost = utype_happy_cost(ut, plr);
2953
2954 if (happy_cost <= 0) {
2955 return 0;
2956 }
2957
2958 fc_assert_ret_val(0 <= *free_unhappy, 0);
2959
2960 if (!unit_being_aggressive(punit) && !is_field_unit(punit)) {
2961 return 0;
2962 }
2963
2964 happy_cost -= get_city_bonus(pcity, EFT_MAKE_CONTENT_MIL_PER);
2965 if (happy_cost <= 0) {
2966 return 0;
2967 }
2968
2969 if (*free_unhappy >= happy_cost) {
2970 *free_unhappy -= happy_cost;
2971 return 0;
2972 } else {
2973 happy_cost -= *free_unhappy;
2974 *free_unhappy = 0;
2975 }
2976
2977 return happy_cost;
2978}
2979
2980/**********************************************************************/
2984 static inline void city_support(struct city *pcity)
2985{
2986 int free_unhappy, martial_law_each;
2987
2988 /* Clear all usage values. */
2989 memset(pcity->usage, 0, O_LAST * sizeof(*pcity->usage));
2990 pcity->martial_law = 0;
2991 pcity->unit_happy_upkeep = 0;
2992
2993 /* Building and unit gold upkeep depends on the setting
2994 * 'game.info.gold_upkeep_style':
2995 * GOLD_UPKEEP_CITY: The upkeep for buildings and units is paid by the
2996 * city.
2997 * GOLD_UPKEEP_MIXED: The upkeep for buildings is paid by the city.
2998 * The upkeep for units is paid by the nation.
2999 * GOLD_UPKEEP_NATION: The upkeep for buildings and units is paid by the
3000 * nation. */
3001 fc_assert_msg(gold_upkeep_style_is_valid(game.info.gold_upkeep_style),
3002 "Invalid gold_upkeep_style %d", game.info.gold_upkeep_style);
3003 switch (game.info.gold_upkeep_style) {
3004 case GOLD_UPKEEP_CITY:
3005 pcity->usage[O_GOLD] += city_total_unit_gold_upkeep(pcity);
3006 fc__fallthrough; /* No break */
3007 case GOLD_UPKEEP_MIXED:
3008 pcity->usage[O_GOLD] += city_total_impr_gold_upkeep(pcity);
3009 break;
3010 case GOLD_UPKEEP_NATION:
3011 /* nothing */
3012 break;
3013 }
3014 /* Food consumption by citizens. */
3015 pcity->usage[O_FOOD] += game.info.food_cost * city_size_get(pcity);
3016
3017 /* military units in this city (need _not_ be home city) can make
3018 * unhappy citizens content */
3019 martial_law_each = get_city_bonus(pcity, EFT_MARTIAL_LAW_EACH);
3020 if (martial_law_each > 0) {
3021 int count = 0;
3022 int martial_law_max = get_city_bonus(pcity, EFT_MARTIAL_LAW_MAX);
3023
3024 unit_list_iterate(pcity->tile->units, punit) {
3025 if ((count < martial_law_max || martial_law_max == 0)
3026 && is_military_unit(punit)
3027 && unit_owner(punit) == city_owner(pcity)) {
3028 count++;
3029 }
3030 } unit_list_iterate_end;
3031
3032 pcity->martial_law = CLIP(0, count * martial_law_each, MAX_CITY_SIZE);
3033 }
3034
3035 free_unhappy = get_city_bonus(pcity, EFT_MAKE_CONTENT_MIL);
3036 unit_list_iterate(pcity->units_supported, punit) {
3037 pcity->unit_happy_upkeep += city_unit_unhappiness(punit, &free_unhappy);
3038 output_type_iterate(o) {
3039 if (O_GOLD != o) {
3040 /* O_GOLD is handled with "game.info.gold_upkeep_style", see over. */
3041 pcity->usage[o] += punit->upkeep[o];
3042 }
3043 } output_type_iterate_end;
3044 } unit_list_iterate_end;
3045}
3046
3047/**********************************************************************/
3059 void city_refresh_from_main_map(struct city *pcity, bool *workers_map)
3060{
3061 if (workers_map == NULL) {
3062 /* do a full refresh */
3063
3064 /* Calculate the bonus[] array values. */
3065 set_city_bonuses(pcity);
3066 /* Calculate the tile_cache[] values. */
3067 city_tile_cache_update(pcity);
3068 /* manage settlers, and units */
3069 city_support(pcity);
3070 }
3071
3072 /* Calculate output from citizens (uses city_tile_cache_get_output()). */
3073 get_worked_tile_output(pcity, pcity->citizen_base, workers_map);
3074 add_specialist_output(pcity, pcity->citizen_base);
3075
3076 set_city_production(pcity);
3077 citizen_base_mood(pcity);
3078 /* Note that pollution is calculated before unhappy_city_check() makes
3079 * deductions for disorder; so a city in disorder still causes pollution */
3080 pcity->pollution = city_pollution(pcity, pcity->prod[O_SHIELD]);
3081
3082 happy_copy(pcity, FEELING_LUXURY);
3083 citizen_happy_luxury(pcity); /* with our new found luxuries */
3084
3085 happy_copy(pcity, FEELING_EFFECT);
3086 citizen_content_buildings(pcity);
3087
3088 happy_copy(pcity, FEELING_NATIONALITY);
3089 citizen_happiness_nationality(pcity);
3090
3091 /* Martial law & unrest from units */
3092 happy_copy(pcity, FEELING_MARTIAL);
3093 citizen_happy_units(pcity);
3094
3095 /* Building (including wonder) happiness effects */
3096 happy_copy(pcity, FEELING_FINAL);
3097 citizen_happy_wonders(pcity);
3098
3099 unhappy_city_check(pcity);
3100 set_surpluses(pcity);
3101}
3102
3103/**********************************************************************/
3110 int city_waste(const struct city *pcity, Output_type_id otype, int total,
3111 int *breakdown)
3112{
3113 int penalty_waste = 0;
3114 int penalty_size = 0; /* separate notradesize/fulltradesize from normal
3115 * corruption */
3116 int total_eft = total; /* normal corruption calculated on total reduced by
3117 * possible size penalty */
3118 int waste_level = get_city_output_bonus(pcity, get_output_type(otype),
3119 EFT_OUTPUT_WASTE);
3120 bool waste_all = FALSE;
3121
3122 if (otype == O_TRADE) {
3123 /* FIXME: special case for trade: it is affected by notradesize and
3124 * fulltradesize server settings.
3125 *
3126 * If notradesize and fulltradesize are equal then the city gets no
3127 * trade at that size. */
3128 int notradesize = MIN(game.info.notradesize, game.info.fulltradesize);
3129 int fulltradesize = MAX(game.info.notradesize, game.info.fulltradesize);
3130
3131 if (city_size_get(pcity) <= notradesize) {
3132 penalty_size = total_eft; /* Then no trade income. */
3133 } else if (city_size_get(pcity) >= fulltradesize) {
3134 penalty_size = 0;
3135 } else {
3136 penalty_size = total_eft * (fulltradesize - city_size_get(pcity))
3137 / (fulltradesize - notradesize);
3138 }
3139 }
3140
3141 /* Apply corruption only to anything left after tradesize */
3142 total_eft -= penalty_size;
3143
3144 /* Distance-based waste.
3145 * Don't bother calculating if there's nothing left to lose. */
3146 if (total_eft > 0) {
3147 int waste_by_dist = get_city_output_bonus(pcity, get_output_type(otype),
3148 EFT_OUTPUT_WASTE_BY_DISTANCE);
3149 int waste_by_rel_dist = get_city_output_bonus(pcity, get_output_type(otype),
3150 EFT_OUTPUT_WASTE_BY_REL_DISTANCE);
3151 if (waste_by_dist > 0 || waste_by_rel_dist > 0) {
3152 const struct city *gov_center = NULL;
3153 int min_dist = FC_INFINITY;
3154
3155 /* Check the special case that city itself is gov center
3156 * before expensive iteration through all cities. */
3157 if (is_gov_center(pcity)) {
3158 gov_center = pcity;
3159 min_dist = 0;
3160 } else {
3161 city_list_iterate(city_owner(pcity)->cities, gc) {
3162 /* Do not recheck current city */
3163 if (gc != pcity && is_gov_center(gc)) {
3164 int dist = real_map_distance(gc->tile, pcity->tile);
3165
3166 if (dist < min_dist) {
3167 gov_center = gc;
3168 min_dist = dist;
3169 }
3170 }
3171 } city_list_iterate_end;
3172 }
3173
3174 if (gov_center == NULL) {
3175 waste_all = TRUE; /* no gov center - no income */
3176 } else {
3177 waste_level += waste_by_dist * min_dist / 100;
3178 if (waste_by_rel_dist > 0) {
3179 /* Multiply by 50 as an "standard size" for which EFT_OUTPUT_WASTE_BY_DISTANCE
3180 * and EFT_OUTPUT_WASTE_BY_REL_DISTANCE would give same result. */
3181 waste_level += waste_by_rel_dist * 50 * min_dist / 100
3182 / MAX(wld.map.xsize, wld.map.ysize);
3183 }
3184 }
3185 }
3186 }
3187
3188 if (waste_all) {
3189 penalty_waste = total_eft;
3190 } else {
3191 int waste_pct = get_city_output_bonus(pcity, get_output_type(otype),
3192 EFT_OUTPUT_WASTE_PCT);
3193
3194 /* corruption/waste calculated only for the actually produced amount */
3195 if (waste_level > 0) {
3196 penalty_waste = total_eft * waste_level / 100;
3197 }
3198
3199 /* bonus calculated only for the actually produced amount */
3200 penalty_waste -= penalty_waste * waste_pct / 100;
3201
3202 /* Clip */
3203 penalty_waste = MIN(MAX(penalty_waste, 0), total_eft);
3204 }
3205
3206 if (breakdown) {
3207 breakdown[OLOSS_WASTE] = penalty_waste;
3208 breakdown[OLOSS_SIZE] = penalty_size;
3209 }
3210
3211 /* add up total penalty */
3212 return penalty_waste + penalty_size;
3213}
3214
3215/**********************************************************************/
3218 citizens city_specialists(const struct city *pcity)
3219{
3220 citizens count = 0;
3221
3222 specialist_type_iterate(sp) {
3223 fc_assert_ret_val(MAX_CITY_SIZE - count > pcity->specialists[sp], 0);
3224 count += pcity->specialists[sp];
3225 } specialist_type_iterate_end;
3226
3227 return count;
3228}
3229
3230/**********************************************************************/
3235 Specialist_type_id best_specialist(Output_type_id otype,
3236 const struct city *pcity)
3237{
3238 int best = DEFAULT_SPECIALIST;
3239 int val = get_specialist_output(pcity, best, otype);
3240
3241 specialist_type_iterate(i) {
3242 if (!pcity || city_can_use_specialist(pcity, i)) {
3243 int val2 = get_specialist_output(pcity, i, otype);
3244
3245 if (val2 > val) {
3246 best = i;
3247 val = val2;
3248 }
3249 }
3250 } specialist_type_iterate_end;
3251
3252 return best;
3253}
3254
3255/**********************************************************************/
3258 void city_add_improvement(struct city *pcity,
3259 const struct impr_type *pimprove)
3260{
3261 pcity->built[improvement_index(pimprove)].turn = game.info.turn; /*I_ACTIVE*/
3262
3263 if (is_server() && is_wonder(pimprove)) {
3264 /* Client just read the info from the packets. */
3265 wonder_built(pcity, pimprove);
3266 }
3267}
3268
3269/**********************************************************************/
3272 void city_remove_improvement(struct city *pcity,
3273 const struct impr_type *pimprove)
3274{
3275 log_debug("Improvement %s removed from city %s",
3276 improvement_rule_name(pimprove), pcity->name);
3277
3278 pcity->built[improvement_index(pimprove)].turn = I_DESTROYED;
3279
3280 if (is_server() && is_wonder(pimprove)) {
3281 /* Client just read the info from the packets. */
3282 wonder_destroyed(pcity, pimprove);
3283 }
3284}
3285
3286/**********************************************************************/
3289 bool is_city_option_set(const struct city *pcity, enum city_options option)
3290{
3291 return BV_ISSET(pcity->city_options, option);
3292}
3293
3294/**********************************************************************/
3297 void city_styles_alloc(int num)
3298{
3299 int i;
3300
3301 city_styles = fc_calloc(num, sizeof(*city_styles));
3302 game.control.styles_count = num;
3303
3304 for (i = 0; i < game.control.styles_count; i++) {
3305 requirement_vector_init(&city_styles[i].reqs);
3306 }
3307}
3308
3309/**********************************************************************/
3312 void city_styles_free(void)
3313{
3314 int i;
3315
3316 for (i = 0; i < game.control.styles_count; i++) {
3317 requirement_vector_free(&city_styles[i].reqs);
3318 }
3319
3320 free(city_styles);
3321 city_styles = NULL;
3322 game.control.styles_count = 0;
3323}
3324
3325/**********************************************************************/
3331 struct city *create_city_virtual(struct player *pplayer,
3332 struct tile *ptile, const char *name)
3333{
3334 int i;
3335
3336 /* Make sure that contents of city structure are correctly initialized,
3337 * if you ever allocate it by some other mean than fc_calloc() */
3338 struct city *pcity = fc_calloc(1, sizeof(*pcity));
3339
3340 fc_assert_ret_val(NULL != name, NULL); /* No unnamed cities! */
3341
3342 /* Do this early, so any logging later will have the city name */
3343 city_name_set(pcity, name);
3344
3345 pcity->tile = ptile;
3346 fc_assert_ret_val(NULL != pplayer, NULL); /* No unowned cities! */
3347 pcity->owner = pplayer;
3348
3349 if (is_server()) {
3350 pcity->original = pplayer;
3351 }
3352
3353 /* City structure was allocated with fc_calloc(), so contents are initially
3354 * zero. There is no need to initialize it a second time. */
3355
3356 /* Now set some useful default values. */
3357 pcity->capital = CAPITAL_NOT;
3358 city_size_set(pcity, 1);
3359 pcity->specialists[DEFAULT_SPECIALIST] = 1;
3360
3361 output_type_iterate(o) {
3362 pcity->bonus[o] = 100;
3363 } output_type_iterate_end;
3364
3365 pcity->turn_plague = -1; /* -1 = never */
3366 pcity->did_buy = FALSE;
3367 pcity->city_radius_sq = game.info.init_city_radius_sq;
3368 pcity->turn_founded = game.info.turn;
3369 pcity->turn_last_built = game.info.turn;
3370
3371 pcity->tile_cache_radius_sq = -1; /* -1 = tile_cache must be initialised */
3372
3373 /* pcity->ai.act_cache: worker activities on the city map */
3374
3375 /* Initialise improvements list */
3376 for (i = 0; i < ARRAY_SIZE(pcity->built); i++) {
3377 pcity->built[i].turn = I_NEVER;
3378 }
3379
3380 /* Set up the worklist */
3381 worklist_init(&pcity->worklist);
3382
3383 pcity->units_supported = unit_list_new();
3384 pcity->routes = trade_route_list_new();
3385 pcity->task_reqs = worker_task_list_new();
3386
3387 if (is_server()) {
3388 pcity->server.mgr_score_calc_turn = -1; /* -1 = never */
3389
3390 CALL_FUNC_EACH_AI(city_alloc, pcity);
3391 } else {
3392 pcity->client.info_units_supported =
3393 unit_list_new_full(unit_virtual_destroy);
3394 pcity->client.info_units_present =
3395 unit_list_new_full(unit_virtual_destroy);
3396 /* collecting_info_units_supported set by fc_calloc().
3397 * collecting_info_units_present set by fc_calloc(). */
3398 }
3399
3400 return pcity;
3401}
3402
3403/**********************************************************************/
3407 void destroy_city_virtual(struct city *pcity)
3408{
3409 CALL_FUNC_EACH_AI(city_free, pcity);
3410
3411 citizens_free(pcity);
3412
3413 /* Free worker tasks */
3414 while (worker_task_list_size(pcity->task_reqs) > 0) {
3415 struct worker_task *ptask = worker_task_list_get(pcity->task_reqs, 0);
3416
3417 worker_task_list_remove(pcity->task_reqs, ptask);
3418
3419 free(ptask);
3420 }
3421 worker_task_list_destroy(pcity->task_reqs);
3422
3423 /* Free rally points */
3424 city_rally_point_clear(pcity);
3425
3426 unit_list_destroy(pcity->units_supported);
3427 trade_route_list_destroy(pcity->routes);
3428 if (pcity->tile_cache != NULL) {
3429 free(pcity->tile_cache);
3430 }
3431
3432 if (pcity->cm_parameter) {
3433 free(pcity->cm_parameter);
3434 }
3435
3436 if (!is_server()) {
3437 unit_list_destroy(pcity->client.info_units_supported);
3438 unit_list_destroy(pcity->client.info_units_present);
3439 /* Handle a rare case where the game is freed in the middle of a
3440 * spy/diplomat investigate cycle. */
3441 if (pcity->client.collecting_info_units_supported != NULL) {
3442 unit_list_destroy(pcity->client.collecting_info_units_supported);
3443 }
3444 if (pcity->client.collecting_info_units_present != NULL) {
3445 unit_list_destroy(pcity->client.collecting_info_units_present);
3446 }
3447 }
3448
3449 free(pcity->name);
3450
3451 memset(pcity, 0, sizeof(*pcity)); /* ensure no pointers remain */
3452 free(pcity);
3453}
3454
3455/**********************************************************************/
3459 bool city_exist(int id)
3460{
3461 /* Check if city exist in game */
3462 if (game_city_by_number(id)) {
3463 return TRUE;
3464 }
3465
3466 return FALSE;
3467}
3468
3469/**********************************************************************/
3476 bool city_is_virtual(const struct city *pcity)
3477{
3478 if (!pcity) {
3479 return FALSE;
3480 }
3481
3482 return pcity != game_city_by_number(pcity->id);
3483}
3484
3485/**********************************************************************/
3488 bool is_free_worked(const struct city *pcity, const struct tile *ptile)
3489{
3490 return is_city_center(pcity, ptile);
3491}
3492
3493/**********************************************************************/
3496 void *city_ai_data(const struct city *pcity, const struct ai_type *ai)
3497{
3498 return pcity->server.ais[ai_type_number(ai)];
3499}
3500
3501/**********************************************************************/
3504 void city_set_ai_data(struct city *pcity, const struct ai_type *ai,
3505 void *data)
3506{
3507 pcity->server.ais[ai_type_number(ai)] = data;
3508}
3509
3510/**********************************************************************/
3513 void city_rally_point_clear(struct city *pcity)
3514{
3515 /* Free rally points */
3516 if (pcity->rally_point.length > 0) {
3517 pcity->rally_point.length = 0;
3518 pcity->rally_point.persistent = FALSE;
3519 pcity->rally_point.vigilant = FALSE;
3520 free(pcity->rally_point.orders);
3521 pcity->rally_point.orders = NULL;
3522 }
3523}
3524
3525/**********************************************************************/
3528 void city_rally_point_receive(const struct packet_city_rally_point *packet,
3529 struct city *pcity)
3530{
3531 struct unit_order *checked_orders;
3532
3533 if (pcity == NULL) {
3534 /* Probably lost. */
3535 log_verbose("handle_city_rally_point() bad city number %d.",
3536 packet->city_id32);
3537 return;
3538 }
3539
3540 if (0 > packet->length || MAX_LEN_ROUTE < packet->length) {
3541 /* Shouldn't happen */
3542 log_error("city_rally_point_receive() invalid packet length %d (max %d)",
3543 packet->length, MAX_LEN_ROUTE);
3544 return;
3545 }
3546
3547 pcity->rally_point.length = packet->length;
3548
3549 if (packet->length == 0) {
3550 pcity->rally_point.vigilant = FALSE;
3551 pcity->rally_point.persistent = FALSE;
3552 if (pcity->rally_point.orders) {
3553 free(pcity->rally_point.orders);
3554 pcity->rally_point.orders = NULL;
3555 }
3556 } else {
3557 checked_orders = create_unit_orders(&(wld.map),
3558 packet->length, packet->orders);
3559 if (!checked_orders) {
3560 pcity->rally_point.length = 0;
3561 log_error("invalid rally point orders for city number %d.",
3562 packet->city_id32);
3563 return;
3564 }
3565
3566 pcity->rally_point.persistent = packet->persistent;
3567 pcity->rally_point.vigilant = packet->vigilant;
3568 pcity->rally_point.orders = checked_orders;
3569 }
3570}
action_enablers_for_action
struct action_enabler_list * action_enablers_for_action(action_id action)
Definition actions.c:2475
action_by_result_iterate
#define action_by_result_iterate(_paction_, _result_)
Definition actions.h:488
action_enabler_list_iterate_end
#define action_enabler_list_iterate_end
Definition actions.h:457
action_by_result_iterate_end
#define action_by_result_iterate_end
Definition actions.h:492
action_enabler_list_iterate
#define action_enabler_list_iterate(action_enabler_list, aenabler)
Definition actions.h:455
ai_type_number
int ai_type_number(const struct ai_type *ai)
Definition ai.c:272
ai.h
CALL_FUNC_EACH_AI
#define CALL_FUNC_EACH_AI(_func,...)
Definition ai.h:384
BV_CLR_ALL
#define BV_CLR_ALL(bv)
Definition bitvector.h:95
BV_SET
#define BV_SET(bv, bit)
Definition bitvector.h:81
BV_ISSET
#define BV_ISSET(bv, bit)
Definition bitvector.h:78
citizens_free
void citizens_free(struct city *pcity)
Definition citizens.c:58
citizens.h
citizens_foreign_iterate_end
#define citizens_foreign_iterate_end
Definition citizens.h:63
citizens_foreign_iterate
#define citizens_foreign_iterate(_pcity, _pslot, _nationality)
Definition citizens.h:58
base_city_celebrating
bool base_city_celebrating(const struct city *pcity)
Definition city.c:1602
city_is_virtual
bool city_is_virtual(const struct city *pcity)
Definition city.c:3476
city_turns_to_build
int city_turns_to_build(const struct city *pcity, const struct universal *target, bool include_shield_stock)
Definition city.c:1930
citizen_luxury_happy
static void citizen_luxury_happy(struct city *pcity, int *luxuries)
Definition city.c:2437
city_improvement_name_translation
const char * city_improvement_name_translation(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:657
city_exists_within_max_city_map
bool city_exists_within_max_city_map(const struct tile *ptile, bool may_be_on_center)
Definition city.c:2074
citizen_content_buildings
static void citizen_content_buildings(struct city *pcity)
Definition city.c:2484
is_free_worked
bool is_free_worked(const struct city *pcity, const struct tile *ptile)
Definition city.c:3488
city_production_gets_caravan_shields
bool city_production_gets_caravan_shields(const struct universal *tgt)
Definition city.c:1825
caravan_helped_impr
static bv_imprs caravan_helped_impr
Definition city.c:1736
city_map_radius_sq_set
void city_map_radius_sq_set(struct city *pcity, int radius_sq)
Definition city.c:142
city_production_build_shield_cost
int city_production_build_shield_cost(const struct city *pcity)
Definition city.c:721
city_name_set
void city_name_set(struct city *pcity, const char *new_name)
Definition city.c:1114
player_base_citizen_happiness
static int player_base_citizen_happiness(const struct player *pplayer)
Definition city.c:2123
can_city_build_unit_later
bool can_city_build_unit_later(const struct city *pcity, const struct unit_type *punittype)
Definition city.c:943
city_granary_size
int city_granary_size(int city_size)
Definition city.c:2095
player_angry_citizens
citizens player_angry_citizens(const struct player *pplayer)
Definition city.c:2159
city_pollution_types
int city_pollution_types(const struct city *pcity, int shield_total, int *pollu_prod, int *pollu_pop, int *pollu_mod)
Definition city.c:2680
city_tile_cache_update
static void city_tile_cache_update(struct city *pcity)
Definition city.c:2333
city_set_ai_data
void city_set_ai_data(struct city *pcity, const struct ai_type *ai, void *data)
Definition city.c:3504
get_worked_tile_output
static void get_worked_tile_output(const struct city *pcity, int *output, bool *workers_map)
Definition city.c:2255
citizen_happy_wonders
static void citizen_happy_wonders(struct city *pcity)
Definition city.c:2605
city_support
static void city_support(struct city *pcity)
Definition city.c:2984
city_built_last_turn
bool city_built_last_turn(const struct city *pcity)
Definition city.c:2242
can_city_build_now
bool can_city_build_now(const struct city *pcity, const struct universal *target)
Definition city.c:984
city_waste
int city_waste(const struct city *pcity, Output_type_id otype, int total, int *breakdown)
Definition city.c:3110
city_unit_unhappiness
int city_unit_unhappiness(struct unit *punit, int *free_unhappy)
Definition city.c:2934
generate_city_map_indices
void generate_city_map_indices(void)
Definition city.c:521
city_build_slots
int city_build_slots(const struct city *pcity)
Definition city.c:2825
city_map_to_tile
struct tile * city_map_to_tile(const struct civ_map *nmap, const struct tile *city_center, int city_radius_sq, int city_map_x, int city_map_y)
Definition city.c:299
city_has_building
bool city_has_building(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:1207
city_style_name_translation
const char * city_style_name_translation(const int style)
Definition city.c:1721
city_styles_free
void city_styles_free(void)
Definition city.c:3312
city_tile_cache_get_output
static int city_tile_cache_get_output(const struct city *pcity, int city_tile_index, enum output_type_id o)
Definition city.c:2362
output_type_by_identifier
Output_type_id output_type_by_identifier(const char *id)
Definition city.c:641
is_capital
bool is_capital(const struct city *pcity)
Definition city.c:1544
city_styles
struct citystyle * city_styles
Definition city.c:78
city_styles_alloc
void city_styles_alloc(int num)
Definition city.c:3297
city_name_get
const char * city_name_get(const struct city *pcity)
Definition city.c:1106
base_city_can_work_tile
bool base_city_can_work_tile(const struct player *restriction, const struct city *pcity, const struct tile *ptile)
Definition city.c:1368
city_unit_slots_available
int city_unit_slots_available(const struct city *pcity)
Definition city.c:1018
city_production_caravan_shields_init
void city_production_caravan_shields_init(void)
Definition city.c:1743
city_remove_improvement
void city_remove_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3272
city_improvement_upkeep
int city_improvement_upkeep(const struct city *pcity, const struct impr_type *b)
Definition city.c:1222
city_airlift_max
int city_airlift_max(const struct city *pcity)
Definition city.c:2835
city_can_be_built_here
bool city_can_be_built_here(const struct tile *ptile, const struct unit *punit, bool hut_test)
Definition city.c:1451
citymindist_prevents_city_on_tile
bool citymindist_prevents_city_on_tile(const struct civ_map *nmap, const struct tile *ptile)
Definition city.c:1429
get_output_type
struct output_type * get_output_type(Output_type_id output)
Definition city.c:632
is_city_option_set
bool is_city_option_set(const struct city *pcity, enum city_options option)
Definition city.c:3289
city_population
int city_population(const struct city *pcity)
Definition city.c:1160
city_map_index
static struct iter_index * city_map_index
Definition city.c:59
city_style_rule_name
const char * city_style_rule_name(const int style)
Definition city.c:1730
city_size_add
void city_size_add(struct city *pcity, int add)
Definition city.c:1133
city_can_use_specialist
bool city_can_use_specialist(const struct city *pcity, Specialist_type_id type)
Definition city.c:1034
city_production_has_flag
bool city_production_has_flag(const struct city *pcity, enum impr_flag_id flag)
Definition city.c:711
city_is_occupied
bool city_is_occupied(const struct city *pcity)
Definition city.c:1630
free_city_map_index
void free_city_map_index(void)
Definition city.c:603
city_refresh_from_main_map
void city_refresh_from_main_map(struct city *pcity, bool *workers_map)
Definition city.c:3059
city_production_unit_veteran_level
int city_production_unit_veteran_level(struct city *pcity, const struct unit_type *punittype)
Definition city.c:788
add_tax_income
void add_tax_income(const struct player *pplayer, int trade, int *output)
Definition city.c:2208
can_city_build_improvement_now
bool can_city_build_improvement_now(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:838
city_tile_index_to_xy
bool city_tile_index_to_xy(int *city_map_x, int *city_map_y, int city_tile_index, int city_radius_sq)
Definition city.c:95
tile_non_allied_city
struct city * tile_non_allied_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:2031
city_got_defense_effect
bool city_got_defense_effect(const struct city *pcity, const struct unit_type *attacker)
Definition city.c:1561
player_content_citizens
citizens player_content_citizens(const struct player *pplayer)
Definition city.c:2149
city_map_includes_tile
bool city_map_includes_tile(const struct city *const pcity, const struct tile *map_tile)
Definition city.c:287
get_city_tithes_bonus
int get_city_tithes_bonus(const struct city *pcity)
Definition city.c:2190
citylog_map_data
void citylog_map_data(enum log_level level, int radius_sq, int *map_data)
Definition city.c:405
city_map_numtiles
static int city_map_numtiles[CITY_MAP_MAX_RADIUS_SQ+1]
Definition city.c:66
city_rapture_grow
bool city_rapture_grow(const struct city *pcity)
Definition city.c:1618
city_production_turns_to_build
int city_production_turns_to_build(const struct city *pcity, bool include_shield_stock)
Definition city.c:804
city_unhappy
bool city_unhappy(const struct city *pcity)
Definition city.c:1591
best_specialist
Specialist_type_id best_specialist(Output_type_id otype, const struct city *pcity)
Definition city.c:3235
create_city_virtual
struct city * create_city_virtual(struct player *pplayer, struct tile *ptile, const char *name)
Definition city.c:3331
set_city_production
void set_city_production(struct city *pcity)
Definition city.c:2846
city_list_find_number
struct city * city_list_find_number(struct city_list *This, int id)
Definition city.c:1644
caravan_helped_utype
static bv_unit_types caravan_helped_utype
Definition city.c:1737
get_final_city_output_bonus
int get_final_city_output_bonus(const struct city *pcity, Output_type_id otype)
Definition city.c:2175
city_celebrating
bool city_celebrating(const struct city *pcity)
Definition city.c:1610
city_can_be_built_tile_only
bool city_can_be_built_tile_only(const struct civ_map *nmap, const struct tile *ptile)
Definition city.c:1525
can_city_build_improvement_direct
bool can_city_build_improvement_direct(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:814
get_output_identifier
const char * get_output_identifier(Output_type_id output)
Definition city.c:613
city_illness_calc
int city_illness_calc(const struct city *pcity, int *ill_base, int *ill_size, int *ill_trade, int *ill_pollution)
Definition city.c:2763
get_output_name
const char * get_output_name(Output_type_id output)
Definition city.c:623
city_can_grow_to
bool city_can_grow_to(const struct city *pcity, int pop_size)
Definition city.c:1977
city_change_production_penalty
int city_change_production_penalty(const struct city *pcity, const struct universal *target)
Definition city.c:1851
get_city_health
static int get_city_health(const struct city *pcity)
Definition city.c:2746
is_valid_city_coords
bool is_valid_city_coords(const int city_radius_sq, const int city_map_x, const int city_map_y)
Definition city.c:182
set_city_bonuses
static void set_city_bonuses(struct city *pcity)
Definition city.c:2316
set_surpluses
static void set_surpluses(struct city *pcity)
Definition city.c:2376
city_pollution
int city_pollution(const struct city *pcity, int shield_total)
Definition city.c:2714
city_happy
bool city_happy(const struct city *pcity)
Definition city.c:1579
city_size_set
void city_size_set(struct city *pcity, citizens size)
Definition city.c:1149
city_add_improvement
void city_add_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3258
city_tile_output_now
int city_tile_output_now(const struct city *pcity, const struct tile *ptile, Output_type_id otype)
Definition city.c:1351
city_map_radius_sq_get
int city_map_radius_sq_get(const struct city *pcity)
Definition city.c:131
can_city_build_direct
bool can_city_build_direct(const struct city *pcity, const struct universal *target)
Definition city.c:966
tile_allied_city
struct city * tile_allied_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:2001
tile_enemy_city
struct city * tile_enemy_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:1986
citylog_map_workers
void citylog_map_workers(enum log_level level, struct city *pcity)
Definition city.c:440
city_base_to_city_map
bool city_base_to_city_map(int *city_map_x, int *city_map_y, const struct city *const pcity, const struct tile *map_tile)
Definition city.c:275
destroy_city_virtual
void destroy_city_virtual(struct city *pcity)
Definition city.c:3407
rs_max_city_radius_sq
int rs_max_city_radius_sq(void)
Definition city.c:153
city_tile_to_city_map
bool city_tile_to_city_map(int *city_map_x, int *city_map_y, const int city_radius_sq, const struct tile *city_center, const struct tile *map_tile)
Definition city.c:258
city_specialists
citizens city_specialists(const struct city *pcity)
Definition city.c:3218
citylog_map_line
static char * citylog_map_line(int y, int city_radius_sq, int *city_map_data)
Definition city.c:363
city_rally_point_receive
void city_rally_point_receive(const struct packet_city_rally_point *packet, struct city *pcity)
Definition city.c:3528
cmp
static int cmp(int v1, int v2)
Definition city.c:319
CITYLOG_MAX_VAL
#define CITYLOG_MAX_VAL
Definition city.c:362
city_choose_build_default
void city_choose_build_default(struct city *pcity)
Definition city.c:1056
city_style_by_translated_name
int city_style_by_translated_name(const char *s)
Definition city.c:1686
add_specialist_output
void add_specialist_output(const struct city *pcity, int *output)
Definition city.c:2295
city_ai_data
void * city_ai_data(const struct city *pcity, const struct ai_type *ai)
Definition city.c:3496
city_list_find_name
struct city * city_list_find_name(struct city_list *This, const char *name)
Definition city.c:1660
citizen_happiness_nationality
static void citizen_happiness_nationality(struct city *pcity)
Definition city.c:2508
can_city_build_later
bool can_city_build_later(const struct city *pcity, const struct universal *target)
Definition city.c:1001
city_tile_output
int city_tile_output(const struct city *pcity, const struct tile *ptile, bool is_celebrating, Output_type_id otype)
Definition city.c:1250
city_map_xy
static int city_map_xy[CITY_MAP_MAX_SIZE][CITY_MAP_MAX_SIZE]
Definition city.c:63
city_tile_xy_to_index
int city_tile_xy_to_index(int city_map_x, int city_map_y, int city_radius_sq)
Definition city.c:117
is_gov_center
bool is_gov_center(const struct city *pcity)
Definition city.c:1552
log_ca_s_init
#define log_ca_s_init
citizen_happy_luxury
static void citizen_happy_luxury(struct city *pcity)
Definition city.c:2474
city_map_tiles
int city_map_tiles(int city_radius_sq)
Definition city.c:165
is_unit_near_a_friendly_city
bool is_unit_near_a_friendly_city(const struct unit *punit)
Definition city.c:2047
unhappy_city_check
static void unhappy_city_check(struct city *pcity)
Definition city.c:2653
can_city_build_unit_direct
bool can_city_build_unit_direct(const struct city *pcity, const struct unit_type *punittype)
Definition city.c:887
tile_non_attack_city
struct city * tile_non_attack_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:2016
city_exist
bool city_exist(int id)
Definition city.c:3459
city_can_work_tile
bool city_can_work_tile(const struct city *pcity, const struct tile *ptile)
Definition city.c:1420
compare_iter_index
int compare_iter_index(const void *a, const void *b)
Definition city.c:337
city_production_build_units
bool city_production_build_units(const struct city *pcity, bool add_production, int *num_units)
Definition city.c:731
city_turns_to_grow
int city_turns_to_grow(const struct city *pcity)
Definition city.c:1961
city_rally_point_clear
void city_rally_point_clear(struct city *pcity)
Definition city.c:3513
can_city_build_improvement_later
bool can_city_build_improvement_later(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:855
output_types
struct output_type output_types[O_LAST]
Definition city.c:82
city_had_recent_plague
bool city_had_recent_plague(const struct city *pcity)
Definition city.c:2816
get_trade_illness
static int get_trade_illness(const struct city *pcity)
Definition city.c:2726
city_can_change_build
bool city_can_change_build(const struct city *pcity)
Definition city.c:1048
city_total_unit_gold_upkeep
int city_total_unit_gold_upkeep(const struct city *pcity)
Definition city.c:1189
city_style_by_rule_name
int city_style_by_rule_name(const char *s)
Definition city.c:1703
citizen_base_mood
static void citizen_base_mood(struct city *pcity)
Definition city.c:2398
city_name_compare
int city_name_compare(const void *p1, const void *p2)
Definition city.c:1676
city_total_impr_gold_upkeep
int city_total_impr_gold_upkeep(const struct city *pcity)
Definition city.c:1170
can_city_build_unit_now
bool can_city_build_unit_now(const struct city *pcity, const struct unit_type *punittype)
Definition city.c:925
citizen_happy_units
static void citizen_happy_units(struct city *pcity)
Definition city.c:2557
happy_copy
static void happy_copy(struct city *pcity, enum citizen_feeling i)
Definition city.c:2386
city_production_name_translation
const char * city_production_name_translation(const struct city *pcity)
Definition city.c:694
is_friendly_city_near
bool is_friendly_city_near(const struct player *owner, const struct tile *ptile)
Definition city.c:2056
city.h
city_list_iterate
#define city_list_iterate(citylist, pcity)
Definition city.h:488
city_tile
#define city_tile(_pcity_)
Definition city.h:544
UNHAPPY_PENALTY_ALL_PRODUCTION
@ UNHAPPY_PENALTY_ALL_PRODUCTION
Definition city.h:248
UNHAPPY_PENALTY_NONE
@ UNHAPPY_PENALTY_NONE
Definition city.h:246
UNHAPPY_PENALTY_SURPLUS
@ UNHAPPY_PENALTY_SURPLUS
Definition city.h:247
city_tile_iterate_index
#define city_tile_iterate_index(_nmap, _radius_sq, _city_tile, _tile, _index)
Definition city.h:193
CITY_MAP_MAX_RADIUS_SQ
#define CITY_MAP_MAX_RADIUS_SQ
Definition city.h:78
city_size_get
static citizens city_size_get(const struct city *pcity)
Definition city.h:549
CITY_MAP_MAX_SIZE
#define CITY_MAP_MAX_SIZE
Definition city.h:86
city_tile_iterate_index_end
#define city_tile_iterate_index_end
Definition city.h:201
CITIZEN_LAST
@ CITIZEN_LAST
Definition city.h:264
CITIZEN_ANGRY
@ CITIZEN_ANGRY
Definition city.h:263
CITIZEN_HAPPY
@ CITIZEN_HAPPY
Definition city.h:260
CITIZEN_CONTENT
@ CITIZEN_CONTENT
Definition city.h:261
CITIZEN_UNHAPPY
@ CITIZEN_UNHAPPY
Definition city.h:262
CITY_MAP_CENTER_RADIUS_SQ
#define CITY_MAP_CENTER_RADIUS_SQ
Definition city.h:81
CITY_MAP_MIN_RADIUS_SQ
#define CITY_MAP_MIN_RADIUS_SQ
Definition city.h:76
output_type_iterate
#define output_type_iterate(output)
Definition city.h:809
CITY_REL2ABS
#define CITY_REL2ABS(_coor)
Definition city.h:107
city_owner
#define city_owner(_pcity_)
Definition city.h:543
MAX_CITY_SIZE
#define MAX_CITY_SIZE
Definition city.h:98
is_city_center
static bool is_city_center(const struct city *pcity, const struct tile *ptile)
Definition city.h:832
city_list_iterate_end
#define city_list_iterate_end
Definition city.h:490
I_DESTROYED
#define I_DESTROYED
Definition city.h:240
I_NEVER
#define I_NEVER
Definition city.h:239
city_tile_iterate
#define city_tile_iterate(_nmap, _radius_sq, _city_tile, _tile)
Definition city.h:222
citizen_feeling
citizen_feeling
Definition city.h:270
FEELING_EFFECT
@ FEELING_EFFECT
Definition city.h:273
FEELING_LUXURY
@ FEELING_LUXURY
Definition city.h:272
FEELING_FINAL
@ FEELING_FINAL
Definition city.h:276
FEELING_BASE
@ FEELING_BASE
Definition city.h:271
FEELING_NATIONALITY
@ FEELING_NATIONALITY
Definition city.h:274
FEELING_MARTIAL
@ FEELING_MARTIAL
Definition city.h:275
OLOSS_SIZE
@ OLOSS_SIZE
Definition city.h:283
OLOSS_WASTE
@ OLOSS_WASTE
Definition city.h:282
city_map_iterate_end
#define city_map_iterate_end
Definition city.h:169
production_class_type
production_class_type
Definition city.h:38
PCT_LAST
@ PCT_LAST
Definition city.h:42
PCT_UNIT
@ PCT_UNIT
Definition city.h:39
PCT_NORMAL_IMPROVEMENT
@ PCT_NORMAL_IMPROVEMENT
Definition city.h:40
PCT_WONDER
@ PCT_WONDER
Definition city.h:41
city_map_iterate
#define city_map_iterate(_radius_sq, _index, _x, _y)
Definition city.h:165
city_tile_iterate_end
#define city_tile_iterate_end
Definition city.h:230
CITY_MAP_MAX_RADIUS
#define CITY_MAP_MAX_RADIUS
Definition city.h:71
city_built_iterate
#define city_built_iterate(_pcity, _p)
Definition city.h:798
is_free_worked_index
#define is_free_worked_index(city_tile_index)
Definition city.h:844
city_map_tiles_from_city
#define city_map_tiles_from_city(_pcity)
Definition city.h:119
CITY_ABS2REL
#define CITY_ABS2REL(_coor)
Definition city.h:108
city_built_iterate_end
#define city_built_iterate_end
Definition city.h:804
output_type_iterate_end
#define output_type_iterate_end
Definition city.h:815
trade_city
Definition menu.h:138
cm_init_citymap
void cm_init_citymap(void)
Definition cm.c:312
cm.h
punit
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:73
cost
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit int cost
Definition dialogs_g.h:73
distribute
void distribute(int number, unsigned groups, const unsigned *ratios, int *result)
Definition distribute.c:34
distribute.h
reqs
struct @21::@22 reqs
get_tile_output_bonus
int get_tile_output_bonus(const struct city *pcity, const struct tile *ptile, const struct output_type *poutput, enum effect_type effect_type)
Definition effects.c:862
get_target_bonus_effects
int get_target_bonus_effects(struct effect_list *plist, const struct req_context *context, const struct player *other_player, enum effect_type effect_type)
Definition effects.c:691
get_city_bonus
int get_city_bonus(const struct city *pcity, enum effect_type effect_type)
Definition effects.c:789
get_city_output_bonus
int get_city_output_bonus(const struct city *pcity, const struct output_type *poutput, enum effect_type effect_type)
Definition effects.c:906
get_building_bonus
int get_building_bonus(const struct city *pcity, const struct impr_type *building, enum effect_type effect_type)
Definition effects.c:930
get_city_tile_output_bonus
int get_city_tile_output_bonus(const struct city *pcity, const struct tile *ptile, const struct output_type *poutput, enum effect_type effect_type)
Definition effects.c:838
effect_cumulative_max
int effect_cumulative_max(enum effect_type type, struct universal *unis, size_t n_unis)
Definition effects.c:331
get_unittype_bonus
int get_unittype_bonus(const struct player *pplayer, const struct tile *ptile, const struct unit_type *punittype, const struct action *paction, enum effect_type effect_type)
Definition effects.c:957
get_player_bonus
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Definition effects.c:771
effects.h
is_server
static bool is_server(void)
Definition fc_interface.h:69
citizens
unsigned char citizens
Definition fc_types.h:358
RPT_CERTAIN
@ RPT_CERTAIN
Definition fc_types.h:586
RPT_POSSIBLE
@ RPT_POSSIBLE
Definition fc_types.h:585
Specialist_type_id
int Specialist_type_id
Definition fc_types.h:345
output_type_id
output_type_id
Definition fc_types.h:90
O_SHIELD
@ O_SHIELD
Definition fc_types.h:91
O_FOOD
@ O_FOOD
Definition fc_types.h:91
O_TRADE
@ O_TRADE
Definition fc_types.h:91
O_SCIENCE
@ O_SCIENCE
Definition fc_types.h:91
O_LUXURY
@ O_LUXURY
Definition fc_types.h:91
O_GOLD
@ O_GOLD
Definition fc_types.h:91
O_LAST
@ O_LAST
Definition fc_types.h:91
MAX_LEN_CITYNAME
#define MAX_LEN_CITYNAME
Definition fc_types.h:67
Output_type_id
enum output_type_id Output_type_id
Definition fc_types.h:348
fcintl.h
Q_
#define Q_(String)
Definition fcintl.h:70
_
#define _(String)
Definition fcintl.h:67
Qn_
#define Qn_(String)
Definition fcintl.h:89
N_
#define N_(String)
Definition fcintl.h:69
game
struct civ_game game
Definition game.c:57
wld
struct world wld
Definition game.c:58
game_city_by_number
struct city * game_city_by_number(int id)
Definition game.c:102
game.h
government_of_player
struct government * government_of_player(const struct player *pplayer)
Definition government.c:113
government.h
owner
struct city * owner
Definition citydlg.c:219
type
GType type
Definition repodlgs.c:1312
can_player_build_improvement_direct
bool can_player_build_improvement_direct(const struct player *p, const struct impr_type *pimprove)
Definition improvement.c:659
is_improvement_redundant
bool is_improvement_redundant(const struct city *pcity, const struct impr_type *pimprove)
Definition improvement.c:636
can_player_build_improvement_later
bool can_player_build_improvement_later(const struct player *p, const struct impr_type *pimprove)
Definition improvement.c:734
wonder_built
void wonder_built(const struct city *pcity, const struct impr_type *pimprove)
Definition improvement.c:800
great_wonder_is_destroyed
bool great_wonder_is_destroyed(const struct impr_type *pimprove)
Definition improvement.c:966
improvement_rule_name
const char * improvement_rule_name(const struct impr_type *pimprove)
Definition improvement.c:250
improvement_index
Impr_type_id improvement_index(const struct impr_type *pimprove)
Definition improvement.c:173
is_wonder
bool is_wonder(const struct impr_type *pimprove)
Definition improvement.c:341
is_great_wonder
bool is_great_wonder(const struct impr_type *pimprove)
Definition improvement.c:767
improvement_obsolete
bool improvement_obsolete(const struct player *pplayer, const struct impr_type *pimprove, const struct city *pcity)
Definition improvement.c:409
improvement_has_flag
bool improvement_has_flag(const struct impr_type *pimprove, enum impr_flag_id flag)
Definition improvement.c:381
wonder_destroyed
void wonder_destroyed(const struct city *pcity, const struct impr_type *pimprove)
Definition improvement.c:820
improvement_name_translation
const char * improvement_name_translation(const struct impr_type *pimprove)
Definition improvement.c:241
great_wonder_is_available
bool great_wonder_is_available(const struct impr_type *pimprove)
Definition improvement.c:977
improvement.h
improvement_iterate_end
#define improvement_iterate_end
Definition improvement.h:211
improvement_iterate
#define improvement_iterate(_p)
Definition improvement.h:205
name
const char * name
Definition inputfile.c:127
log.h
fc_assert_msg
#define fc_assert_msg(condition, message,...)
Definition log.h:181
fc_assert_ret
#define fc_assert_ret(condition)
Definition log.h:191
log_warn
#define log_warn(message,...)
Definition log.h:105
log_verbose
#define log_verbose(message,...)
Definition log.h:109
fc_assert
#define fc_assert(condition)
Definition log.h:176
fc_assert_ret_val
#define fc_assert_ret_val(condition, val)
Definition log.h:194
log_do_output_for_level
#define log_do_output_for_level(level)
Definition log.h:89
fc_assert_action
#define fc_assert_action(condition, action)
Definition log.h:187
log_debug
#define log_debug(message,...)
Definition log.h:115
log_base
#define log_base(level, message,...)
Definition log.h:94
log_level
log_level
Definition log.h:28
LOG_DEBUG
@ LOG_DEBUG
Definition log.h:34
log_error
#define log_error(message,...)
Definition log.h:103
map_pos_to_tile
struct tile * map_pos_to_tile(const struct civ_map *nmap, int map_x, int map_y)
Definition map.c:417
map_vector_to_sq_distance
int map_vector_to_sq_distance(int dx, int dy)
Definition map.c:612
same_pos
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Definition map.c:938
real_map_distance
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:628
map_distance_vector
void map_distance_vector(int *dx, int *dy, const struct tile *tile0, const struct tile *tile1)
Definition map.c:1071
map.h
square_iterate
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition map.h:385
square_iterate_end
#define square_iterate_end
Definition map.h:388
index_to_map_pos
#define index_to_map_pos(pmap_x, pmap_y, mindex)
Definition map.h:227
mem.h
fc_calloc
#define fc_calloc(n, esz)
Definition mem.h:38
FC_FREE
#define FC_FREE(ptr)
Definition mem.h:41
fc_strdup
#define fc_strdup(str)
Definition mem.h:43
fc_realloc
#define fc_realloc(ptr, sz)
Definition mem.h:36
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
can_unit_exist_at_tile
bool can_unit_exist_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Definition movement.c:304
is_native_near_tile
bool is_native_near_tile(const struct civ_map *nmap, const struct unit_class *uclass, const struct tile *ptile)
Definition movement.c:450
movement.h
rule_name_get
static const char * rule_name_get(const struct name_translation *ptrans)
Definition name_translation.h:112
name_translation_get
static const char * name_translation_get(const struct name_translation *ptrans)
Definition name_translation.h:121
packets.h
MAX_LEN_ROUTE
#define MAX_LEN_ROUTE
Definition packets.h:44
cities
struct city_list * cities
Definition packhand.c:117
pplayers_at_war
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1364
pplayers_allied
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1381
pplayers_non_attack
bool pplayers_non_attack(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1435
player_slot_get_player
struct player * player_slot_get_player(const struct player_slot *pslot)
Definition player.c:430
universal_build_shield_cost
int universal_build_shield_cost(const struct city *pcity, const struct universal *target)
Definition requirements.c:5819
are_reqs_active
bool are_reqs_active(const struct req_context *context, const struct player *other_player, const struct requirement_vector *reqs, const enum req_problem_type prob_type)
Definition requirements.c:4361
req_vec_wants_type
bool req_vec_wants_type(const struct requirement_vector *reqs, enum universals_n kind)
Definition requirements.c:4667
does_req_contradicts_reqs
bool does_req_contradicts_reqs(const struct requirement *req, const struct requirement_vector *vec)
Definition requirements.c:1539
is_req_preventing
enum req_unchanging_status is_req_preventing(const struct req_context *context, const struct player *other_player, const struct requirement *req, enum req_problem_type prob_type)
Definition requirements.c:4619
universal_name_translation
const char * universal_name_translation(const struct universal *psource, char *buf, size_t bufsz)
Definition requirements.c:5529
requirement_vector_iterate_end
#define requirement_vector_iterate_end
Definition requirements.h:93
requirement_vector_iterate
#define requirement_vector_iterate(req_vec, preq)
Definition requirements.h:91
settings
static struct setting settings[]
Definition settings.c:1378
level
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:183
CLIP
#define CLIP(lower, current, upper)
Definition shared.h:57
ARRAY_SIZE
#define ARRAY_SIZE(x)
Definition shared.h:85
MIN
#define MIN(x, y)
Definition shared.h:55
FC_INFINITY
#define FC_INFINITY
Definition shared.h:36
MAX
#define MAX(x, y)
Definition shared.h:54
specialist_by_number
struct specialist * specialist_by_number(const Specialist_type_id id)
Definition specialist.c:100
get_specialist_output
int get_specialist_output(const struct city *pcity, Specialist_type_id sp, Output_type_id otype)
Definition specialist.c:217
specialist.h
specialist_type_iterate_end
#define specialist_type_iterate_end
Definition specialist.h:79
specialist_type_iterate
#define specialist_type_iterate(sp)
Definition specialist.h:73
DEFAULT_SPECIALIST
#define DEFAULT_SPECIALIST
Definition specialist.h:43
step
int step
Definition specpq.h:92
size
size_t size
Definition specvec.h:72
ai_type
Definition ai.h:50
built_status::turn
int turn
Definition city.h:238
city
Definition city.h:309
city::task_reqs
struct worker_task_list * task_reqs
Definition city.h:395
city::tile_cache
struct tile_cache * tile_cache
Definition city.h:337
city::turn_last_built
int turn_last_built
Definition city.h:373
city::surplus
int surplus[O_LAST]
Definition city.h:343
city::food_stock
int food_stock
Definition city.h:354
city::built
struct built_status built[B_LAST]
Definition city.h:380
city::original
struct player * original
Definition city.h:313
city::pollution
int pollution
Definition city.h:356
city::id
int id
Definition city.h:315
city::last_turns_shield_surplus
int last_turns_shield_surplus
Definition city.h:378
city::capital
enum capital_type capital
Definition city.h:317
city::disbanded_shields
int disbanded_shields
Definition city.h:377
city::waste
int waste[O_LAST]
Definition city.h:344
city::turn_plague
int turn_plague
Definition city.h:361
city::info_units_present
struct unit_list * info_units_present
Definition city.h:457
city::city_options
bv_city_options city_options
Definition city.h:389
city::unit_happy_upkeep
citizens unit_happy_upkeep
Definition city.h:327
city::city_radius_sq
int city_radius_sq
Definition city.h:362
city::was_happy
bool was_happy
Definition city.h:368
city::owner
struct player * owner
Definition city.h:312
city::turn_founded
int turn_founded
Definition city.h:372
city::citizen_base
int citizen_base[O_LAST]
Definition city.h:347
city::caravan_shields
int caravan_shields
Definition city.h:376
city::did_buy
bool did_buy
Definition city.h:366
city::info_units_supported
struct unit_list * info_units_supported
Definition city.h:456
city::name
char * name
Definition city.h:310
city::ais
void * ais[FREECIV_AI_MOD_LAST]
Definition city.h:436
city::routes
struct trade_route_list * routes
Definition city.h:332
city::occupied
bool occupied
Definition city.h:443
city::usage
int usage[O_LAST]
Definition city.h:348
city::worklist
struct worklist worklist
Definition city.h:387
city::tile_cache_radius_sq
int tile_cache_radius_sq
Definition city.h:340
city::production
struct universal production
Definition city.h:382
city::orders
struct unit_order * orders
Definition city.h:405
city::rally_point
struct city::@16 rally_point
city::collecting_info_units_supported
struct unit_list * collecting_info_units_supported
Definition city.h:460
city::bonus
int bonus[O_LAST]
Definition city.h:351
city::vigilant
bool vigilant
Definition city.h:404
city::unhappy_penalty
int unhappy_penalty[O_LAST]
Definition city.h:345
city::size
citizens size
Definition city.h:320
city::before_change_shields
int before_change_shields
Definition city.h:375
city::style
int style
Definition city.h:316
city::length
size_t length
Definition city.h:400
city::collecting_info_units_present
struct unit_list * collecting_info_units_present
Definition city.h:461
city::feel
citizens feel[CITIZEN_LAST][FEELING_LAST]
Definition city.h:321
city::specialists
citizens specialists[SP_MAX]
Definition city.h:324
city::tile
struct tile * tile
Definition city.h:311
city::shield_stock
int shield_stock
Definition city.h:355
city::prod
int prod[O_LAST]
Definition city.h:346
city::server
struct city::@17::@19 server
city::cm_parameter
struct cm_parameter * cm_parameter
Definition city.h:408
city::changed_from
struct universal changed_from
Definition city.h:385
city::units_supported
struct unit_list * units_supported
Definition city.h:391
city::mgr_score_calc_turn
int mgr_score_calc_turn
Definition city.h:415
city::illness_trade
int illness_trade
Definition city.h:357
city::persistent
bool persistent
Definition city.h:402
city::client
struct city::@17::@20 client
city::rapture
int rapture
Definition city.h:371
city::martial_law
citizens martial_law
Definition city.h:326
citystyle
Definition city.h:471
civ_game::control
struct packet_ruleset_control control
Definition game.h:83
civ_game::info
struct packet_game_info info
Definition game.h:89
civ_game::government_during_revolution
struct government * government_during_revolution
Definition game.h:94
civ_map
Definition map_types.h:71
civ_map::xsize
int xsize
Definition map_types.h:77
civ_map::ysize
int ysize
Definition map_types.h:77
impr_type
Definition improvement.h:68
impr_type::upkeep
int upkeep
Definition improvement.h:78
impr_type::reqs
struct requirement_vector reqs
Definition improvement.h:75
iter_index
Definition city.h:101
iter_index::dist
int dist
Definition city.h:102
iter_index::dx
int dx
Definition city.h:102
iter_index::dy
int dy
Definition city.h:102
option
Definition options.c:454
output_type
Definition city.h:251
output_type::id
const char * id
Definition city.h:254
packet_city_rally_point
Definition packets_gen.h:497
packet_city_rally_point::city_id32
int city_id32
Definition packets_gen.h:499
packet_city_rally_point::persistent
bool persistent
Definition packets_gen.h:501
packet_city_rally_point::orders
struct unit_order orders[MAX_LEN_ROUTE]
Definition packets_gen.h:503
packet_city_rally_point::vigilant
bool vigilant
Definition packets_gen.h:502
packet_city_rally_point::length
int length
Definition packets_gen.h:500
packet_game_info::illness_trade_infection
int illness_trade_infection
Definition packets_gen.h:183
packet_game_info::fulltradesize
int fulltradesize
Definition packets_gen.h:158
packet_game_info::celebratesize
int celebratesize
Definition packets_gen.h:140
packet_game_info::illness_base_factor
int illness_base_factor
Definition packets_gen.h:179
packet_game_info::illness_min_size
int illness_min_size
Definition packets_gen.h:180
packet_game_info::forced_science
int forced_science
Definition packets_gen.h:157
packet_game_info::granary_num_inis
int granary_num_inis
Definition packets_gen.h:174
packet_game_info::gold_upkeep_style
enum gold_upkeep_style gold_upkeep_style
Definition packets_gen.h:167
packet_game_info::citizen_nationality
bool citizen_nationality
Definition packets_gen.h:143
packet_game_info::angrycitizen
int angrycitizen
Definition packets_gen.h:126
packet_game_info::granary_food_inc
int granary_food_inc
Definition packets_gen.h:172
packet_game_info::notradesize
int notradesize
Definition packets_gen.h:203
packet_game_info::granary_food_ini
int granary_food_ini[MAX_GRANARY_INIS]
Definition packets_gen.h:173
packet_game_info::changable_tax
bool changable_tax
Definition packets_gen.h:141
packet_game_info::rapturedelay
int rapturedelay
Definition packets_gen.h:216
packet_game_info::init_city_radius_sq
int init_city_radius_sq
Definition packets_gen.h:184
packet_game_info::granularity
int granularity
Definition packets_gen.h:249
packet_game_info::base_pollution
int base_pollution
Definition packets_gen.h:127
packet_game_info::forced_gold
int forced_gold
Definition packets_gen.h:155
packet_game_info::illness_pollution_factor
int illness_pollution_factor
Definition packets_gen.h:182
packet_game_info::happy_cost
int happy_cost
Definition packets_gen.h:176
packet_game_info::food_cost
int food_cost
Definition packets_gen.h:153
packet_game_info::illness_on
bool illness_on
Definition packets_gen.h:181
packet_game_info::turn
int turn
Definition packets_gen.h:241
packet_game_info::foodbox
int foodbox
Definition packets_gen.h:154
packet_game_info::citymindist
int citymindist
Definition packets_gen.h:147
packet_game_info::forced_luxury
int forced_luxury
Definition packets_gen.h:156
packet_game_info::min_city_center_output
int min_city_center_output[O_LAST]
Definition packets_gen.h:199
packet_ruleset_control::styles_count
int styles_count
Definition packets_gen.h:1472
player_economic::luxury
int luxury
Definition player.h:73
player_economic::science
int science
Definition player.h:72
player
Definition player.h:249
player::cities
struct city_list * cities
Definition player.h:281
player::units
struct unit_list * units
Definition player.h:282
player::economic
struct player_economic economic
Definition player.h:284
req_context
Definition requirements.h:99
req_context::player
const struct player * player
Definition requirements.h:100
requirement
Definition requirements.h:80
requirement::survives
bool survives
Definition requirements.h:83
requirement::present
bool present
Definition requirements.h:84
requirement::range
enum req_range range
Definition requirements.h:82
requirement::source
struct universal source
Definition requirements.h:81
specialist::reqs
struct requirement_vector reqs
Definition specialist.h:38
terrain
Definition terrain.h:176
terrain::irrigation_food_incr
int irrigation_food_incr
Definition terrain.h:209
terrain::output
int output[O_LAST]
Definition terrain.h:195
terrain::mining_shield_incr
int mining_shield_incr
Definition terrain.h:212
tile_cache
Definition city.c:69
tile_cache::output
int output[O_LAST]
Definition city.c:70
tile
Definition tile.h:49
tile::units
struct unit_list * units
Definition tile.h:57
trade_route_settings
Definition traderoutes.h:75
unit_order
Definition unit.h:92
unit_type
Definition unittype.h:481
unit_type::build_reqs
struct requirement_vector build_reqs
Definition unittype.h:501
unit_type::obsoleted_by
const struct unit_type * obsoleted_by
Definition unittype.h:510
unit_type::city_slots
int city_slots
Definition unittype.h:533
unit_type::upkeep
int upkeep[O_LAST]
Definition unittype.h:519
unit
Definition unit.h:138
unit::upkeep
int upkeep[O_LAST]
Definition unit.h:148
unit::homecity
int homecity
Definition unit.h:146
universal
Definition fc_types.h:756
universal::kind
enum universals_n kind
Definition fc_types.h:758
universal::value
universals_u value
Definition fc_types.h:757
worker_task
Definition workertask.h:21
worker_task::ptile
struct tile * ptile
Definition workertask.h:22
world::map
struct civ_map map
Definition world_object.h:38
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969
fc_strcasecmp
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:189
support.h
sz_strlcpy
#define sz_strlcpy(dest, src)
Definition support.h:161
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47
sz_strlcat
#define sz_strlcat(dest, src)
Definition support.h:162
fc__fallthrough
#define fc__fallthrough
Definition support.h:109
T_UNKNOWN
#define T_UNKNOWN
Definition terrain.h:57
terrain_has_flag
#define terrain_has_flag(terr, flag)
Definition terrain.h:269
tile_roads_output_bonus
int tile_roads_output_bonus(const struct tile *ptile, enum output_type_id o)
Definition tile.c:285
tile_roads_output_incr
int tile_roads_output_incr(const struct tile *ptile, enum output_type_id o)
Definition tile.c:265
tile_get_known
enum known_type tile_get_known(const struct tile *ptile, const struct player *pplayer)
Definition tile.c:386
tile_city
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
tile_index
#define tile_index(_pt_)
Definition tile.h:87
tile_resource_is_valid
static bool tile_resource_is_valid(const struct tile *ptile)
Definition tile.h:102
tile_worked
#define tile_worked(_tile)
Definition tile.h:113
tile_resource
#define tile_resource(_tile)
Definition tile.h:101
TILE_UNKNOWN
@ TILE_UNKNOWN
Definition tile.h:35
TILE_KNOWN_SEEN
@ TILE_KNOWN_SEEN
Definition tile.h:37
tile_terrain
#define tile_terrain(_tile)
Definition tile.h:109
tile_owner
#define tile_owner(_tile)
Definition tile.h:95
can_cities_trade
bool can_cities_trade(const struct city *pc1, const struct city *pc2)
Definition traderoutes.c:214
cities_trade_route_type
enum trade_route_type cities_trade_route_type(const struct city *pcity1, const struct city *pcity2)
Definition traderoutes.c:58
trade_base_between_cities
int trade_base_between_cities(const struct city *pc1, const struct city *pc2)
Definition traderoutes.c:322
trade_route_settings_by_type
struct trade_route_settings * trade_route_settings_by_type(enum trade_route_type type)
Definition traderoutes.c:201
trade_from_route
int trade_from_route(const struct city *pc1, const struct trade_route *route, int base)
Definition traderoutes.c:358
traderoutes.h
TRI_ACTIVE
@ TRI_ACTIVE
Definition traderoutes.h:30
trade_routes_iterate_end
#define trade_routes_iterate_end
Definition traderoutes.h:133
trade_partners_iterate_end
#define trade_partners_iterate_end
Definition traderoutes.h:165
trade_routes_iterate
#define trade_routes_iterate(c, proute)
Definition traderoutes.h:129
trade_partners_iterate
#define trade_partners_iterate(c, p)
Definition traderoutes.h:160
trade_route_type
trade_route_type
Definition traderoutes.h:37
universals_u::utype
const struct unit_type * utype
Definition fc_types.h:604
universals_u::building
const struct impr_type * building
Definition fc_types.h:598
create_unit_orders
struct unit_order * create_unit_orders(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2821
unit_being_aggressive
bool unit_being_aggressive(const struct unit *punit)
Definition unit.c:1521
is_military_unit
bool is_military_unit(const struct unit *punit)
Definition unit.c:321
is_field_unit
bool is_field_unit(const struct unit *punit)
Definition unit.c:385
unit_virtual_destroy
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1709
unit_occupies_tile
struct unit * unit_occupies_tile(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1383
unit.h
unit_tile
#define unit_tile(_pu)
Definition unit.h:388
unit_owner
#define unit_owner(_pu)
Definition unit.h:387
unit_list_iterate
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
unit_list_iterate_end
#define unit_list_iterate_end
Definition unitlist.h:33
best_role_unit
struct unit_type * best_role_unit(const struct city *pcity, int role)
Definition unittype.c:2319
unit_type_get
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
can_player_build_unit_direct
bool can_player_build_unit_direct(const struct player *p, const struct unit_type *punittype)
Definition unittype.c:2015
utype_veteran_levels
int utype_veteran_levels(const struct unit_type *punittype)
Definition unittype.c:2661
utype_can_do_act_when_ustate
bool utype_can_do_act_when_ustate(const struct unit_type *punit_type, const action_id act_id, const enum ustate_prop prop, const bool is_there)
Definition unittype.c:1007
utype_rule_name
const char * utype_rule_name(const struct unit_type *punittype)
Definition unittype.c:1630
utype_by_number
struct unit_type * utype_by_number(const Unit_type_id id)
Definition unittype.c:112
utype_index
Unit_type_id utype_index(const struct unit_type *punittype)
Definition unittype.c:91
utype_pop_value
int utype_pop_value(const struct unit_type *punittype, const struct city *pcity)
Definition unittype.c:1580
can_player_build_unit_later
bool can_player_build_unit_later(const struct player *p, const struct unit_type *punittype)
Definition unittype.c:2150
utype_build_shield_cost
int utype_build_shield_cost(const struct city *pcity, const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1490
can_utype_do_act_if_tgt_diplrel
bool can_utype_do_act_if_tgt_diplrel(const struct unit_type *punit_type, const action_id act_id, const int prop, const bool is_there)
Definition unittype.c:1069
utype_can_do_action
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:443
utype_happy_cost
int utype_happy_cost(const struct unit_type *ut, const struct player *pplayer)
Definition unittype.c:175
uclass_has_flag
static bool uclass_has_flag(const struct unit_class *punitclass, enum unit_class_flag_id flag)
Definition unittype.h:753
utype_class
#define utype_class(_t_)
Definition unittype.h:736
utype_has_flag
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:604
unit_type_iterate
#define unit_type_iterate(_p)
Definition unittype.h:838
unit_type_iterate_end
#define unit_type_iterate_end
Definition unittype.h:845
U_NOT_OBSOLETED
#define U_NOT_OBSOLETED
Definition unittype.h:509
worklist_init
void worklist_init(struct worklist *pwl)
Definition worklist.c:38
worklist_peek_ith
bool worklist_peek_ith(const struct worklist *pwl, struct universal *prod, int idx)
Definition worklist.c:86
worklist_length
int worklist_length(const struct worklist *pwl)
Definition worklist.c:57