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