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. */
59static 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] */
64
65/* number of tiles of a city; depends on the squared city radius */
67
68/* definitions and functions for the tile_cache */
69struct tile_cache {
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
79struct 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. */
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/**********************************************************************/
96bool city_tile_index_to_xy(int *city_map_x, int *city_map_y,
97 int city_tile_index, int city_radius_sq)
98{
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/**********************************************************************/
118int 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/**********************************************************************/
132int city_map_radius_sq_get(const struct city *pcity)
133{
134 /* a save return value is only the minimal squared radius */
136
137 return pcity->city_radius_sq;
138}
139
140/**********************************************************************/
143void city_map_radius_sq_set(struct city *pcity, int radius_sq)
144{
147
148 pcity->city_radius_sq = radius_sq;
149}
150
151/**********************************************************************/
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/**********************************************************************/
166int 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/**********************************************************************/
183bool 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/**********************************************************************/
259bool 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/**********************************************************************/
276bool 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/**********************************************************************/
288bool 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/**********************************************************************/
300struct 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/**********************************************************************/
320static 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/**********************************************************************/
338int 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 */
364static 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/**********************************************************************/
406void 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
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/**********************************************************************/
441void 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
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),
459 x, y);
460 city_map_data[cindex] = (ptile && tile_worked(ptile) == pcity)
461 ? (is_free_worked_index(cindex) ? 2 : 1) : 0;
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
478 return;
479 }
480
482 sizeof(*city_map_data));
483
485 city_map_data[cindex] = cindex;
487
488 log_debug("city map index:");
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
501 return;
502 }
503
505 sizeof(*city_map_data));
506
508 city_map_data[cindex] = map_vector_to_sq_distance(CITY_ABS2REL(x),
509 CITY_ABS2REL(y));
511
512 log_debug("city map squared radius:");
514 FC_FREE(city_map_data);
515}
516#endif /* FREECIV_DEBUG */
517
518/**********************************************************************/
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
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. */
541
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 */
551 }
552 }
553
554 city_count_tiles++;
555 }
556
557 /* Initialise city_map_xy. -1 defines a invalid city map positions. */
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),
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
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
599}
600
601/**********************************************************************/
605{
607}
608
609/**********************************************************************/
615{
616 fc_assert_ret_val(output >= 0 && output < O_LAST, NULL);
617 return output_types[output].id;
618}
619
620/**********************************************************************/
625{
626 fc_assert_ret_val(output >= 0 && output < O_LAST, NULL);
627 return _(output_types[output].name);
628}
629
630/**********************************************************************/
634{
635 fc_assert_ret_val(output >= 0 && output < O_LAST, NULL);
636 return &output_types[output];
637}
638
639/**********************************************************************/
643{
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/**********************************************************************/
658const 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/**********************************************************************/
695const 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:
702 default:
703 /* fallthru */
704 break;
705 };
706 return universal_name_translation(&pcity->production, buffer, sizeof(buffer));
707}
708
709/**********************************************************************/
712bool city_production_has_flag(const struct city *pcity,
713 enum impr_flag_id flag)
714{
715 return VUT_IMPROVEMENT == pcity->production.kind
717}
718
719/**********************************************************************/
723{
724 return universal_build_shield_cost(pcity, &pcity->production);
725}
726
727/**********************************************************************/
732bool 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/************************************************************************/
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/**********************************************************************/
805int 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/**********************************************************************/
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/**********************************************************************/
839bool 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/**********************************************************************/
856bool 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 }
880
881 return TRUE;
882}
883
884/**********************************************************************/
888bool can_city_build_unit_direct(const struct civ_map *nmap,
889 const struct city *pcity,
890 const struct unit_type *punittype)
891{
892 if (!can_player_build_unit_direct(city_owner(pcity), punittype)) {
893 return FALSE;
894 }
895
896 /* Check unit build requirements. */
897 if (!are_reqs_active(&(const struct req_context) {
898 .player = city_owner(pcity),
899 .city = pcity,
900 .tile = city_tile(pcity),
901 .unittype = punittype,
902 },
903 NULL,
904 &punittype->build_reqs, RPT_CERTAIN)) {
905 return FALSE;
906 }
907
908 /* You can't build naval units inland. */
909 if (!uclass_has_flag(utype_class(punittype), UCF_BUILD_ANYWHERE)
910 && !is_native_near_tile(nmap, utype_class(punittype),
911 pcity->tile)) {
912 return FALSE;
913 }
914
915 if (punittype->city_slots > 0
916 && city_unit_slots_available(pcity) < punittype->city_slots) {
917 return FALSE;
918 }
919
920 return TRUE;
921}
922
923/**********************************************************************/
927bool can_city_build_unit_now(const struct civ_map *nmap,
928 const struct city *pcity,
929 const struct unit_type *punittype)
930{
931 if (!can_city_build_unit_direct(nmap, pcity, punittype)) {
932 return FALSE;
933 }
934
935 while ((punittype = punittype->obsoleted_by) != U_NOT_OBSOLETED) {
936 if (can_player_build_unit_direct(city_owner(pcity), punittype)) {
937 return FALSE;
938 }
939 }
940
941 return TRUE;
942}
943
944/**********************************************************************/
948bool can_city_build_unit_later(const struct civ_map *nmap,
949 const struct city *pcity,
950 const struct unit_type *punittype)
951{
952 /* Can the _player_ ever build this unit? */
953 if (!can_player_build_unit_later(city_owner(pcity), punittype)) {
954 return FALSE;
955 }
956
957 /* Some units can be built only in certain cities -- for instance,
958 ships may be built only in cities adjacent to ocean. */
959 if (!uclass_has_flag(utype_class(punittype), UCF_BUILD_ANYWHERE)
960 && !is_native_near_tile(nmap, utype_class(punittype),
961 pcity->tile)) {
962 return FALSE;
963 }
964
965 return TRUE;
966}
967
968/**********************************************************************/
972bool can_city_build_direct(const struct civ_map *nmap,
973 const struct city *pcity,
974 const struct universal *target)
975{
976 switch (target->kind) {
977 case VUT_UTYPE:
978 return can_city_build_unit_direct(nmap, pcity, target->value.utype);
979 case VUT_IMPROVEMENT:
980 return can_city_build_improvement_direct(pcity, target->value.building);
981 default:
982 break;
983 };
984 return FALSE;
985}
986
987/**********************************************************************/
991bool can_city_build_now(const struct civ_map *nmap,
992 const struct city *pcity,
993 const struct universal *target)
994{
995 switch (target->kind) {
996 case VUT_UTYPE:
997 return can_city_build_unit_now(nmap, pcity, target->value.utype);
998 case VUT_IMPROVEMENT:
999 return can_city_build_improvement_now(pcity, target->value.building);
1000 default:
1001 break;
1002 };
1003 return FALSE;
1004}
1005
1006/**********************************************************************/
1009bool can_city_build_later(const struct civ_map *nmap,
1010 const struct city *pcity,
1011 const struct universal *target)
1012{
1013 switch (target->kind) {
1014 case VUT_UTYPE:
1015 return can_city_build_unit_later(nmap, pcity, target->value.utype);
1016 case VUT_IMPROVEMENT:
1017 return can_city_build_improvement_later(pcity, target->value.building);
1018 default:
1019 break;
1020 };
1021 return FALSE;
1022}
1023
1024/**********************************************************************/
1027int city_unit_slots_available(const struct city *pcity)
1028{
1029 int max = get_city_bonus(pcity, EFT_UNIT_SLOTS);
1030 int current;
1031
1032 current = 0;
1034 current += unit_type_get(punit)->city_slots;
1036
1037 return max - current;
1038}
1039
1040/**********************************************************************/
1043bool city_can_use_specialist(const struct city *pcity,
1045{
1046 return are_reqs_active(&(const struct req_context) {
1047 .player = city_owner(pcity),
1048 .city = pcity,
1049 },
1050 NULL,
1052}
1053
1054/**********************************************************************/
1057bool city_can_change_build(const struct city *pcity)
1058{
1059 return !pcity->did_buy || pcity->shield_stock <= 0;
1060}
1061
1062/**********************************************************************/
1065void city_choose_build_default(const struct civ_map *nmap, struct city *pcity)
1066{
1067 if (NULL == city_tile(pcity)) {
1068 /* When a "dummy" city is created with no tile, then choosing a build
1069 * target could fail. This currently might happen during map editing.
1070 * FIXME: assumes the first unit is always "valid", so check for
1071 * obsolete units elsewhere. */
1072 pcity->production.kind = VUT_UTYPE;
1074 } else {
1075 struct unit_type *u = best_role_unit(pcity, L_FIRSTBUILD);
1076
1077 if (u) {
1078 pcity->production.kind = VUT_UTYPE;
1079 pcity->production.value.utype = u;
1080 } else {
1081 bool found = FALSE;
1082
1083 /* Just pick the first available item. */
1084 improvement_iterate(pimprove) {
1085 if (can_city_build_improvement_direct(pcity, pimprove)) {
1086 found = TRUE;
1087 pcity->production.kind = VUT_IMPROVEMENT;
1088 pcity->production.value.building = pimprove;
1089 break;
1090 }
1092
1093 if (!found) {
1094 unit_type_iterate(punittype) {
1095 if (can_city_build_unit_direct(nmap, pcity, punittype)) {
1096#ifndef FREECIV_NDEBUG
1097 /* Later than this, 'found' is only needed in an fc_assert() */
1098 found = TRUE;
1099#endif /* FREECIV_NDEBUG */
1100 pcity->production.kind = VUT_UTYPE;
1101 pcity->production.value.utype = punittype;
1102 }
1104 }
1105
1106 fc_assert_msg(found, "No production found for city %s!",
1107 city_name_get(pcity));
1108 }
1109 }
1110}
1111
1112/**********************************************************************/
1115const char *city_name_get(const struct city *pcity)
1116{
1117 return (pcity->name != NULL) ? pcity->name : "City missing a name";
1118}
1119
1120/**********************************************************************/
1123void city_name_set(struct city *pcity, const char *new_name)
1124{
1125 if (pcity->name != NULL) {
1126 free(pcity->name);
1127 }
1128
1129 if (strlen(new_name) < MAX_LEN_CITYNAME) {
1130 pcity->name = fc_strdup(new_name);
1131 } else {
1132 log_warn(_("City name \"%s\" too long"), new_name);
1134 sz_strlcpy(pcity->name, new_name);
1135 }
1136}
1137
1138/**********************************************************************/
1142void city_size_add(struct city *pcity, int add)
1143{
1144 citizens size = city_size_get(pcity);
1145
1146 fc_assert_ret(pcity != NULL);
1148
1149 /* Client sets size to zero to start stacking citizens in */
1150 fc_assert_ret(size >= -add);
1151
1152 city_size_set(pcity, size + add);
1153}
1154
1155/**********************************************************************/
1158void city_size_set(struct city *pcity, citizens size)
1159{
1160 fc_assert_ret(pcity != NULL);
1161
1162 /* Set city size. */
1163 pcity->size = size;
1164}
1165
1166/**********************************************************************/
1169int city_population(const struct city *pcity)
1170{
1171 /* Sum_{i=1}^{n} i == n*(n+1)/2 */
1172 return city_size_get(pcity) * (city_size_get(pcity) + 1) * 5;
1173}
1174
1175/**********************************************************************/
1179int city_total_impr_gold_upkeep(const struct city *pcity)
1180{
1181 int gold_needed = 0;
1182
1183 if (pcity == NULL) {
1184 return 0;
1185 }
1186
1187 city_built_iterate(pcity, pimprove) {
1188 gold_needed += city_improvement_upkeep(pcity, pimprove);
1190
1191 return gold_needed;
1192}
1193
1194/**********************************************************************/
1198int city_total_unit_gold_upkeep(const struct city *pcity)
1199{
1200 int gold_needed = 0;
1201
1202 if (pcity == NULL || pcity->units_supported == NULL) {
1203 return 0;
1204 }
1205
1207 gold_needed += punit->upkeep[O_GOLD];
1209
1210 return gold_needed;
1211}
1212
1213/**********************************************************************/
1216bool city_has_building(const struct city *pcity,
1217 const struct impr_type *pimprove)
1218{
1219 if (NULL == pimprove) {
1220 /* Callers should ensure that any external data is tested with
1221 * valid_improvement_by_number() */
1222 return FALSE;
1223 }
1224 return (pcity->built[improvement_index(pimprove)].turn > I_NEVER);
1225}
1226
1227/**********************************************************************/
1231int city_improvement_upkeep(const struct city *pcity,
1232 const struct impr_type *b)
1233{
1234 int upkeep;
1235
1236 if (NULL == b) {
1237 return 0;
1238 }
1239 if (is_wonder(b)) {
1240 return 0;
1241 }
1242
1243 upkeep = b->upkeep;
1244 if (upkeep <= get_building_bonus(pcity, b, EFT_UPKEEP_FREE)) {
1245 return 0;
1246 }
1247
1248 return upkeep;
1249}
1250
1251/**********************************************************************/
1259int city_tile_output(const struct city *pcity, const struct tile *ptile,
1260 bool is_celebrating, Output_type_id otype)
1261{
1262 int prod;
1263 const struct req_context city_ctxt = {
1264 .player = pcity ? city_owner(pcity) : NULL,
1265 .city = pcity,
1266 .tile = ptile,
1267 };
1268 struct terrain *pterrain = tile_terrain(ptile);
1269 const struct output_type *output = &output_types[otype];
1270
1271 fc_assert_ret_val(otype >= 0 && otype < O_LAST, 0);
1272
1273 if (T_UNKNOWN == pterrain) {
1274 /* Special case for the client. The server doesn't allow unknown tiles
1275 * to be worked but we don't necessarily know what player is involved. */
1276 return 0;
1277 }
1278
1279 prod = pterrain->output[otype];
1280 if (tile_resource_is_valid(ptile)) {
1281 prod += tile_resource(ptile)->data.resource->output[otype];
1282 }
1283
1284 switch (otype) {
1285 case O_SHIELD:
1286 if (pterrain->mining_shield_incr != 0) {
1287 prod += pterrain->mining_shield_incr
1288 * get_target_bonus_effects(NULL, &city_ctxt, NULL, EFT_MINING_PCT)
1289 / 100;
1290 }
1291 break;
1292 case O_FOOD:
1293 if (pterrain->irrigation_food_incr != 0) {
1294 prod += pterrain->irrigation_food_incr
1295 * get_target_bonus_effects(NULL, &city_ctxt, NULL,
1296 EFT_IRRIGATION_PCT) / 100;
1297 }
1298 break;
1299 case O_TRADE:
1300 case O_GOLD:
1301 case O_SCIENCE:
1302 case O_LUXURY:
1303 case O_LAST:
1304 break;
1305 }
1306
1307 prod += tile_roads_output_incr(ptile, otype);
1308 prod += (prod * tile_roads_output_bonus(ptile, otype) / 100);
1309
1310 prod += get_tile_output_bonus(pcity, ptile, output, EFT_OUTPUT_ADD_TILE);
1311 if (prod > 0) {
1312 int penalty_limit = get_tile_output_bonus(pcity, ptile, output,
1313 EFT_OUTPUT_PENALTY_TILE);
1314
1315 if (prod >= game.info.granularity) {
1316 prod += get_tile_output_bonus(pcity, ptile, output,
1317 EFT_OUTPUT_INC_TILE);
1318
1319 if (is_celebrating) {
1320 prod += get_tile_output_bonus(pcity, ptile, output,
1321 EFT_OUTPUT_INC_TILE_CELEBRATE);
1322 }
1323 }
1324
1325 prod += (prod
1326 * get_tile_output_bonus(pcity, ptile, output,
1327 EFT_OUTPUT_PER_TILE))
1328 / 100;
1329 if (!is_celebrating && penalty_limit > 0 && prod > penalty_limit) {
1330 if (prod <= game.info.granularity) {
1331 prod = 0;
1332 } else {
1333 prod -= game.info.granularity;
1334 }
1335 }
1336 }
1337
1338 prod -= (prod
1339 * get_tile_output_bonus(pcity, ptile, output,
1340 EFT_OUTPUT_TILE_PUNISH_PCT))
1341 / 100;
1342
1343 if (NULL != pcity && is_city_center(pcity, ptile)) {
1344 prod = MAX(prod, game.info.min_city_center_output[otype]);
1345 }
1346
1347 return prod;
1348}
1349
1350/**********************************************************************/
1360int city_tile_output_now(const struct city *pcity, const struct tile *ptile,
1361 Output_type_id otype)
1362{
1363 return city_tile_output(pcity, ptile, city_celebrating(pcity), otype);
1364}
1365
1366/**********************************************************************/
1377bool base_city_can_work_tile(const struct player *restriction,
1378 const struct city *pcity,
1379 const struct tile *ptile)
1380{
1381 struct player *powner = city_owner(pcity);
1382 int city_map_x, city_map_y;
1383
1384 if (NULL == ptile) {
1385 return FALSE;
1386 }
1387
1388 if (!city_base_to_city_map(&city_map_x, &city_map_y, pcity, ptile)) {
1389 return FALSE;
1390 }
1391
1392 if (NULL != restriction
1393 && TILE_UNKNOWN == tile_get_known(ptile, restriction)) {
1394 return FALSE;
1395 }
1396
1397 if (NULL != tile_owner(ptile) && tile_owner(ptile) != powner) {
1398 return FALSE;
1399 }
1400 /* TODO: civ3-like option for borders */
1401
1402 if (NULL != tile_worked(ptile) && tile_worked(ptile) != pcity) {
1403 return FALSE;
1404 }
1405
1406 if (powner == restriction
1407 && TILE_KNOWN_SEEN != tile_get_known(ptile, powner)) {
1408 return FALSE;
1409 }
1410
1411 if (!is_free_worked(pcity, ptile)
1412 && NULL != unit_occupies_tile(ptile, powner)) {
1413 return FALSE;
1414 }
1415
1416 if (get_city_tile_output_bonus(pcity, ptile, NULL, EFT_TILE_WORKABLE) <= 0) {
1417 return FALSE;
1418 }
1419
1420 return TRUE;
1421}
1422
1423/**********************************************************************/
1429bool city_can_work_tile(const struct city *pcity, const struct tile *ptile)
1430{
1431 return base_city_can_work_tile(city_owner(pcity), pcity, ptile);
1432}
1433
1434/**********************************************************************/
1439 const struct tile *ptile)
1440{
1441 /* citymindist minimum is 1, meaning adjacent is okay */
1442 int citymindist = game.info.citymindist;
1443
1444 square_iterate(nmap, ptile, citymindist - 1, ptile1) {
1445 if (tile_city(ptile1)) {
1446 return TRUE;
1447 }
1449
1450 return FALSE;
1451}
1452
1453/**********************************************************************/
1460bool city_can_be_built_here(const struct civ_map *nmap,
1461 const struct tile *ptile,
1462 const struct unit *punit,
1463 bool hut_test)
1464{
1465 if (!city_can_be_built_tile_only(nmap, ptile)) {
1466 return FALSE;
1467 }
1468
1469 if (punit == NULL) {
1470 /* The remaining checks tests if punit can found a city here */
1471 return TRUE;
1472 }
1473
1474 if (hut_test) {
1475 struct player *towner;
1476
1477 /* Huts can be found only from native tiles, owned by the unit owner.
1478 * Unlike actual city building, this behavior is not affected
1479 * by the ruleset. */
1480 if (!can_unit_exist_at_tile(nmap, punit, ptile)) {
1481 return FALSE;
1482 }
1483
1484 towner = tile_owner(ptile);
1485
1486 if (towner == NULL || towner == unit_owner(punit)) {
1487 return TRUE;
1488 }
1489
1490 return FALSE;
1491 }
1492
1493 action_by_result_iterate(paction, ACTRES_FOUND_CITY) {
1494 if (!utype_can_do_action(unit_type_get(punit), paction->id)) {
1495 /* This action can't be done by this unit type at all. */
1496 continue;
1497 }
1498
1499 /* Non native tile detection */
1500 if (!can_unit_exist_at_tile(nmap, punit, ptile)
1501 /* The ruleset may allow founding cities on non native terrain. */
1503 USP_LIVABLE_TILE, FALSE)) {
1504 /* Many rulesets allow land units to build land cities and sea units
1505 * to build ocean cities. Air units can build cities anywhere. */
1506 continue;
1507 }
1508
1509 /* Foreign tile detection. */
1510 if (tile_owner(ptile) && tile_owner(ptile) != unit_owner(punit)
1511 /* The ruleset may allow founding cities on foreign terrain. */
1513 paction->id,
1514 DRO_FOREIGN, TRUE)) {
1515 /* Cannot steal borders by settling. This has to be settled by
1516 * force of arms. */
1517 continue;
1518 }
1519
1520 return TRUE;
1522
1523 return FALSE;
1524}
1525
1526/**********************************************************************/
1533bool city_can_be_built_tile_only(const struct civ_map *nmap,
1534 const struct tile *ptile)
1535{
1536 if (terrain_has_flag(tile_terrain(ptile), TER_NO_CITIES)) {
1537 /* No cities on this terrain. */
1538 return FALSE;
1539 }
1540
1541 if (citymindist_prevents_city_on_tile(nmap, ptile)) {
1542 return FALSE;
1543 }
1544
1545 return TRUE;
1546}
1547
1548/**********************************************************************/
1552bool is_capital(const struct city *pcity)
1553{
1554 return pcity->capital != CAPITAL_NOT;
1555}
1556
1557/**********************************************************************/
1560bool is_gov_center(const struct city *pcity)
1561{
1562 return (get_city_bonus(pcity, EFT_GOV_CENTER) > 0);
1563}
1564
1565/**********************************************************************/
1569bool city_got_defense_effect(const struct city *pcity,
1570 const struct unit_type *attacker)
1571{
1572 if (!attacker) {
1573 /* Any defense building will do */
1574 return get_city_bonus(pcity, EFT_DEFEND_BONUS) > 0;
1575 }
1576
1577 return get_unittype_bonus(city_owner(pcity), pcity->tile, attacker,
1578 NULL, EFT_DEFEND_BONUS) > 0;
1579}
1580
1581/**********************************************************************/
1587bool city_happy(const struct city *pcity)
1588{
1589 return (city_size_get(pcity) >= game.info.celebratesize
1590 && pcity->feel[CITIZEN_ANGRY][FEELING_FINAL] == 0
1591 && pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL] == 0
1592 && pcity->feel[CITIZEN_HAPPY][FEELING_FINAL] >= (city_size_get(pcity) + 1) / 2);
1593}
1594
1595/**********************************************************************/
1599bool city_unhappy(const struct city *pcity)
1600{
1601 return (pcity->feel[CITIZEN_HAPPY][FEELING_FINAL]
1603 + 2 * pcity->feel[CITIZEN_ANGRY][FEELING_FINAL]);
1604}
1605
1606/**********************************************************************/
1610bool base_city_celebrating(const struct city *pcity)
1611{
1612 return (city_size_get(pcity) >= game.info.celebratesize && pcity->was_happy);
1613}
1614
1615/**********************************************************************/
1618bool city_celebrating(const struct city *pcity)
1619{
1620 return base_city_celebrating(pcity) && city_happy(pcity);
1621}
1622
1623/**********************************************************************/
1626bool city_rapture_grow(const struct city *pcity)
1627{
1628 /* .rapture is checked instead of city_celebrating() because this
1629 function is called after .was_happy was updated. */
1630 return (pcity->rapture > 0 && pcity->surplus[O_FOOD] > 0
1631 && (pcity->rapture % game.info.rapturedelay) == 0
1632 && get_city_bonus(pcity, EFT_RAPTURE_GROW) > 0);
1633}
1634
1635/**********************************************************************/
1638bool city_is_occupied(const struct city *pcity)
1639{
1640 if (is_server()) {
1641 /* The server sees the units inside the city. */
1642 return (unit_list_size(city_tile(pcity)->units) > 0);
1643 } else {
1644 /* The client gets the occupied property from the server. */
1645 return pcity->client.occupied;
1646 }
1647}
1648
1649/**********************************************************************/
1652struct city *city_list_find_number(struct city_list *This, int id)
1653{
1654 if (id != 0) {
1655 city_list_iterate(This, pcity) {
1656 if (pcity->id == id) {
1657 return pcity;
1658 }
1660 }
1661
1662 return NULL;
1663}
1664
1665/**********************************************************************/
1668struct city *city_list_find_name(struct city_list *This, const char *name)
1669{
1670 city_list_iterate(This, pcity) {
1671 if (fc_strcasecmp(name, pcity->name) == 0) {
1672 return pcity;
1673 }
1675
1676 return NULL;
1677}
1678
1679/**********************************************************************/
1684int city_name_compare(const void *p1, const void *p2)
1685{
1686 return fc_strcasecmp((*(const struct city **) p1)->name,
1687 (*(const struct city **) p2)->name);
1688}
1689
1690/**********************************************************************/
1695{
1696 int i;
1697
1698 for (i = 0; i < game.control.styles_count; i++) {
1699 if (0 == strcmp(city_style_name_translation(i), s)) {
1700 return i;
1701 }
1702 }
1703
1704 return -1;
1705}
1706
1707/**********************************************************************/
1711int city_style_by_rule_name(const char *s)
1712{
1713 const char *qs = Qn_(s);
1714 int i;
1715
1716 for (i = 0; i < game.control.styles_count; i++) {
1717 if (0 == fc_strcasecmp(city_style_rule_name(i), qs)) {
1718 return i;
1719 }
1720 }
1721
1722 return -1;
1723}
1724
1725/**********************************************************************/
1730{
1732}
1733
1734/**********************************************************************/
1738const char *city_style_rule_name(const int style)
1739{
1741}
1742
1743/* Cache of what city production caravan shields are allowed to help. */
1744static bv_imprs caravan_helped_impr;
1745static bv_unit_types caravan_helped_utype;
1746
1747/**********************************************************************/
1752{
1753 struct requirement prod_as_req;
1754
1755#define log_ca_s_init log_debug
1756
1757 /* Remove old data. */
1760
1761 /* Common for all production kinds. */
1762 prod_as_req.range = REQ_RANGE_LOCAL;
1763 prod_as_req.survives = FALSE;
1764 prod_as_req.present = TRUE;
1765
1766 /* Check improvements */
1767 prod_as_req.source.kind = VUT_IMPROVEMENT;
1768
1769 improvement_iterate(itype) {
1770 /* Check this improvement. */
1771 prod_as_req.source.value.building = itype;
1772
1774 ACTION_HELP_WONDER),
1775 enabler) {
1776 if (!does_req_contradicts_reqs(&prod_as_req,
1777 &(enabler->target_reqs))
1778 && !req_vec_wants_type(&(enabler->target_reqs), VUT_UTYPE)
1779 && !req_vec_wants_type(&(enabler->target_reqs), VUT_UCLASS)
1780 && !req_vec_wants_type(&(enabler->target_reqs), VUT_UTFLAG)
1781 && !req_vec_wants_type(&(enabler->target_reqs), VUT_UCFLAG)) {
1782 /* This improvement kind can receive caravan shields. */
1783
1785
1786 /* Move on to the next improvement */
1787 break;
1788 }
1790
1791 log_ca_s_init("Help Wonder: %s for %s",
1793 ? "possible" : "impossible"),
1794 improvement_rule_name(itype));
1796
1797 /* Check units. */
1798 prod_as_req.source.kind = VUT_UTYPE;
1799
1800 unit_type_iterate(putype) {
1801 /* Check this utype. */
1802 prod_as_req.source.value.utype = putype;
1803
1805 ACTION_HELP_WONDER),
1806 enabler) {
1807 if (!does_req_contradicts_reqs(&prod_as_req,
1808 &(enabler->target_reqs))
1809 && !req_vec_wants_type(&(enabler->target_reqs), VUT_IMPROVEMENT)
1810 && !req_vec_wants_type(&(enabler->target_reqs), VUT_IMPR_GENUS)) {
1811 /* This unit type kind can receive caravan shields. */
1812
1814
1815 /* Move on to the next unit type */
1816 break;
1817 }
1819
1820 log_ca_s_init("Help Wonder: %s for %s",
1822 ? "possible" : "impossible"),
1823 utype_rule_name(putype));
1825
1826#undef log_ca_s_init
1827}
1828
1829/**********************************************************************/
1834{
1835 switch (tgt->kind) {
1836 case VUT_IMPROVEMENT:
1839 case VUT_UTYPE:
1841 utype_index(tgt->value.utype));
1842 default:
1844 return FALSE;
1845 };
1846}
1847
1848/**********************************************************************/
1859int city_change_production_penalty(const struct city *pcity,
1860 const struct universal *target)
1861{
1862 int shield_stock_after_adjustment;
1863 enum production_class_type orig_class;
1864 enum production_class_type new_class;
1865 int unpenalized_shields = 0, penalized_shields = 0;
1866
1867 switch (pcity->changed_from.kind) {
1868 case VUT_IMPROVEMENT:
1869 if (is_wonder(pcity->changed_from.value.building)) {
1870 orig_class = PCT_WONDER;
1871 } else {
1872 orig_class = PCT_NORMAL_IMPROVEMENT;
1873 }
1874 break;
1875 case VUT_UTYPE:
1876 orig_class = PCT_UNIT;
1877 break;
1878 default:
1879 orig_class = PCT_LAST;
1880 break;
1881 };
1882
1883 switch (target->kind) {
1884 case VUT_IMPROVEMENT:
1885 if (is_wonder(target->value.building)) {
1886 new_class = PCT_WONDER;
1887 } else {
1888 new_class = PCT_NORMAL_IMPROVEMENT;
1889 }
1890 break;
1891 case VUT_UTYPE:
1892 new_class = PCT_UNIT;
1893 break;
1894 default:
1895 new_class = PCT_LAST;
1896 break;
1897 };
1898
1899 /* Changing production is penalized under certain circumstances. */
1900 if (orig_class == new_class
1901 || orig_class == PCT_LAST) {
1902 /* There's never a penalty for building something of the same class. */
1903 unpenalized_shields = pcity->before_change_shields;
1904 } else if (city_built_last_turn(pcity)) {
1905 /* Surplus shields from the previous production won't be penalized if
1906 * you change production on the very next turn. But you can only use
1907 * up to the city's surplus amount of shields in this way. */
1908 unpenalized_shields = MIN(pcity->last_turns_shield_surplus,
1909 pcity->before_change_shields);
1910 penalized_shields = pcity->before_change_shields - unpenalized_shields;
1911 } else {
1912 /* Penalize 50% of the production. */
1913 penalized_shields = pcity->before_change_shields;
1914 }
1915
1916 /* Do not put penalty on these. It shouldn't matter whether you disband unit
1917 before or after changing production...*/
1918 unpenalized_shields += pcity->disbanded_shields;
1919
1920 /* Caravan shields are penalized (just as if you disbanded the caravan)
1921 * if you're not building a wonder. */
1923 unpenalized_shields += pcity->caravan_shields;
1924 } else {
1925 penalized_shields += pcity->caravan_shields;
1926 }
1927
1928 shield_stock_after_adjustment =
1929 unpenalized_shields + penalized_shields / 2;
1930
1931 return shield_stock_after_adjustment;
1932}
1933
1934/**********************************************************************/
1938int city_turns_to_build(const struct city *pcity,
1939 const struct universal *target,
1940 bool include_shield_stock)
1941{
1942 int city_shield_surplus = pcity->surplus[O_SHIELD];
1943 int city_shield_stock = include_shield_stock ?
1944 city_change_production_penalty(pcity, target) : 0;
1945 int cost = universal_build_shield_cost(pcity, target);
1946
1947 if (target->kind == VUT_IMPROVEMENT
1948 && is_great_wonder(target->value.building)
1950 return FC_INFINITY;
1951 }
1952
1953 if (include_shield_stock && (city_shield_stock >= cost)) {
1954 return 1;
1955 } else if (city_shield_surplus > 0) {
1956 return (cost - city_shield_stock - 1) / city_shield_surplus + 1;
1957 } else {
1958 return FC_INFINITY;
1959 }
1960}
1961
1962/**********************************************************************/
1969int city_turns_to_grow(const struct city *pcity)
1970{
1971 if (pcity->surplus[O_FOOD] > 0) {
1972 return (city_granary_size(city_size_get(pcity)) - pcity->food_stock +
1973 pcity->surplus[O_FOOD] - 1) / pcity->surplus[O_FOOD];
1974 } else if (pcity->surplus[O_FOOD] < 0) {
1975 /* turns before famine loss */
1976 return -1 + (pcity->food_stock / pcity->surplus[O_FOOD]);
1977 } else {
1978 return FC_INFINITY;
1979 }
1980}
1981
1982/**********************************************************************/
1985bool city_can_grow_to(const struct city *pcity, int pop_size)
1986{
1987 return (get_city_bonus(pcity, EFT_SIZE_UNLIMIT) > 0
1988 || pop_size <= get_city_bonus(pcity, EFT_SIZE_ADJ));
1989}
1990
1991/**********************************************************************/
1994struct city *tile_enemy_city(const struct tile *ptile,
1995 const struct player *pplayer)
1996{
1997 struct city *pcity = tile_city(ptile);
1998
1999 if (pcity != NULL && pplayers_at_war(pplayer, city_owner(pcity))) {
2000 return pcity;
2001 }
2002
2003 return NULL;
2004}
2005
2006/**********************************************************************/
2009struct city *tile_allied_city(const struct tile *ptile,
2010 const struct player *pplayer)
2011{
2012 struct city *pcity = tile_city(ptile);
2013
2014 if (pcity != NULL && pplayers_allied(pplayer, city_owner(pcity))) {
2015 return pcity;
2016 }
2017
2018 return NULL;
2019}
2020
2021/**********************************************************************/
2024struct city *tile_non_attack_city(const struct tile *ptile,
2025 const struct player *pplayer)
2026{
2027 struct city *pcity = tile_city(ptile);
2028
2029 if (pcity != NULL && pplayers_non_attack(pplayer, city_owner(pcity))) {
2030 return pcity;
2031 }
2032
2033 return NULL;
2034}
2035
2036/**********************************************************************/
2039struct city *tile_non_allied_city(const struct tile *ptile,
2040 const struct player *pplayer)
2041{
2042 struct city *pcity = tile_city(ptile);
2043
2044 if (pcity != NULL && !pplayers_allied(pplayer, city_owner(pcity))) {
2045 return pcity;
2046 }
2047
2048 return NULL;
2049}
2050
2051/**********************************************************************/
2056 const struct unit *punit)
2057{
2059}
2060
2061/**********************************************************************/
2065bool is_friendly_city_near(const struct civ_map *nmap,
2066 const struct player *owner,
2067 const struct tile *ptile)
2068{
2069 square_iterate(nmap, ptile, 3, ptile1) {
2070 struct city *pcity = tile_city(ptile1);
2071
2072 if (pcity && pplayers_allied(owner, city_owner(pcity))) {
2073 return TRUE;
2074 }
2076
2077 return FALSE;
2078}
2079
2080/**********************************************************************/
2085 const struct tile *ptile,
2086 bool may_be_on_center)
2087{
2088 city_tile_iterate(nmap, CITY_MAP_MAX_RADIUS_SQ, ptile, ptile1) {
2089 if (may_be_on_center || !same_pos(ptile, ptile1)) {
2090 if (tile_city(ptile1)) {
2091 return TRUE;
2092 }
2093 }
2095
2096 return FALSE;
2097}
2098
2099/**********************************************************************/
2104int city_granary_size(int city_size)
2105{
2106 int food_inis = game.info.granary_num_inis;
2107 int food_inc = game.info.granary_food_inc;
2108 int base_value;
2109
2110 /* If the city has no citizens, there is no granary. */
2111 if (city_size == 0) {
2112 return 0;
2113 }
2114
2115 /* Granary sizes for the first food_inis citizens are given directly.
2116 * After that we increase the granary size by food_inc per citizen. */
2117 if (city_size > food_inis) {
2118 base_value = game.info.granary_food_ini[food_inis - 1];
2119 base_value += food_inc * (city_size - food_inis);
2120 } else {
2121 base_value = game.info.granary_food_ini[city_size - 1];
2122 }
2123
2124 return MAX(base_value * game.info.foodbox / 100, 1);
2125}
2126
2127/**********************************************************************/
2132static int player_base_citizen_happiness(const struct player *pplayer)
2133{
2134 int cities = city_list_size(pplayer->cities);
2135 int content = get_player_bonus(pplayer, EFT_CITY_UNHAPPY_SIZE);
2136 int basis = get_player_bonus(pplayer, EFT_EMPIRE_SIZE_BASE);
2137 int step = get_player_bonus(pplayer, EFT_EMPIRE_SIZE_STEP);
2138
2139 if (basis + step <= 0) {
2140 /* Value of zero means effect is inactive */
2141 return content;
2142 }
2143
2144 if (cities > basis) {
2145 content--;
2146 if (step != 0) {
2147 /* the first penalty is at (basis + 1) cities;
2148 the next is at (basis + step + 1), _not_ (basis + step) */
2149 content -= (cities - basis - 1) / step;
2150 }
2151 }
2152 return content;
2153}
2154
2155/**********************************************************************/
2159{
2160 int content = player_base_citizen_happiness(pplayer);
2161
2162 return CLIP(0, content, MAX_CITY_SIZE);
2163}
2164
2165/**********************************************************************/
2169{
2170 if (!game.info.angrycitizen) {
2171 return 0;
2172 } else {
2173 /* Create angry citizens only if we have a negative number of possible
2174 * content citizens. This can happen when empires grow really big. */
2175 int content = player_base_citizen_happiness(pplayer);
2176
2177 return CLIP(0, -content, MAX_CITY_SIZE);
2178 }
2179}
2180
2181/**********************************************************************/
2184int get_final_city_output_bonus(const struct city *pcity, Output_type_id otype)
2185{
2186 struct output_type *output = &output_types[otype];
2187 int bonus1 = 100 + get_city_tile_output_bonus(pcity, NULL, output,
2188 EFT_OUTPUT_BONUS);
2189 int bonus2 = 100 + get_city_tile_output_bonus(pcity, NULL, output,
2190 EFT_OUTPUT_BONUS_2);
2191
2192 return MAX(bonus1 * bonus2 / 100, 0);
2193}
2194
2195/**********************************************************************/
2199int get_city_tithes_bonus(const struct city *pcity)
2200{
2201 int tithes_bonus = 0;
2202
2203 if (get_city_bonus(pcity, EFT_HAPPINESS_TO_GOLD) <= 0) {
2204 return 0;
2205 }
2206
2207 tithes_bonus += get_city_bonus(pcity, EFT_MAKE_CONTENT);
2208 tithes_bonus += get_city_bonus(pcity, EFT_FORCE_CONTENT);
2209
2210 return tithes_bonus;
2211}
2212
2213/**********************************************************************/
2217void add_tax_income(const struct player *pplayer, int trade, int *output)
2218{
2219 const int SCIENCE = 0, TAX = 1, LUXURY = 2;
2220 unsigned rates[3];
2221 int result[3];
2222
2223 if (game.info.changable_tax) {
2224 rates[SCIENCE] = pplayer->economic.science;
2225 rates[LUXURY] = pplayer->economic.luxury;
2226 rates[TAX] = 100 - rates[SCIENCE] - rates[LUXURY];
2227 } else {
2228 rates[SCIENCE] = game.info.forced_science;
2229 rates[LUXURY] = game.info.forced_luxury;
2230 rates[TAX] = game.info.forced_gold;
2231 }
2232
2233 /* ANARCHY */
2235 rates[SCIENCE] = 0;
2236 rates[LUXURY] = 100;
2237 rates[TAX] = 0;
2238 }
2239
2240 distribute(trade, 3, rates, result);
2241
2242 output[O_SCIENCE] += result[SCIENCE];
2243 output[O_GOLD] += result[TAX];
2244 output[O_LUXURY] += result[LUXURY];
2245}
2246
2247/**********************************************************************/
2251bool city_built_last_turn(const struct city *pcity)
2252{
2253 return pcity->turn_last_built + 1 >= game.info.turn;
2254}
2255
2256/**********************************************************************/
2264static inline void get_worked_tile_output(const struct civ_map *nmap,
2265 const struct city *pcity,
2266 int *output, bool *workers_map)
2267{
2268 bool is_worked;
2269#ifdef CITY_DEBUGGING
2270 bool is_celebrating = base_city_celebrating(pcity);
2271#endif
2272 struct tile *pcenter = city_tile(pcity);
2273
2274 memset(output, 0, O_LAST * sizeof(*output));
2275
2276 city_tile_iterate_index(nmap, city_map_radius_sq_get(pcity), pcenter, ptile,
2277 city_tile_index) {
2278 if (workers_map == NULL) {
2279 struct city *pwork = tile_worked(ptile);
2280
2281 is_worked = (NULL != pwork && pwork == pcity);
2282 } else {
2283 is_worked = workers_map[city_tile_index];
2284 }
2285
2286 if (is_worked) {
2288#ifdef CITY_DEBUGGING
2289 /* This assertion never fails, but it's so slow that we disable
2290 * it by default. */
2291 fc_assert(city_tile_cache_get_output(pcity, city_tile_index, o)
2292 == city_tile_output(pcity, ptile, is_celebrating, o));
2293#endif /* CITY_DEBUGGING */
2294 output[o] += city_tile_cache_get_output(pcity, city_tile_index, o);
2296 }
2298}
2299
2300/**********************************************************************/
2304void add_specialist_output(const struct city *pcity, int *output)
2305{
2307 int count = pcity->specialists[sp];
2308
2309 output_type_iterate(stat_index) {
2310 int amount = get_specialist_output(pcity, sp, stat_index);
2311
2312 output[stat_index] += count * amount;
2315}
2316
2317/**********************************************************************/
2325static inline void set_city_bonuses(struct city *pcity)
2326{
2328 pcity->bonus[o] = get_final_city_output_bonus(pcity, o);
2330}
2331
2332/**********************************************************************/
2342static inline void city_tile_cache_update(const struct civ_map *nmap,
2343 struct city *pcity)
2344{
2345 bool is_celebrating = base_city_celebrating(pcity);
2346 int radius_sq = city_map_radius_sq_get(pcity);
2347
2348 /* Initialize tile_cache if needed */
2349 if (pcity->tile_cache == NULL || pcity->tile_cache_radius_sq == -1
2350 || pcity->tile_cache_radius_sq != radius_sq) {
2351 pcity->tile_cache = fc_realloc(pcity->tile_cache,
2352 city_map_tiles(radius_sq)
2353 * sizeof(*(pcity->tile_cache)));
2354 pcity->tile_cache_radius_sq = radius_sq;
2355 }
2356
2357 /* Any unreal tiles are skipped - these values should have been memset
2358 * to 0 when the city was created. */
2359 city_tile_iterate_index(nmap, radius_sq, pcity->tile, ptile, city_tile_index) {
2361 (pcity->tile_cache[city_tile_index]).output[o]
2362 = city_tile_output(pcity, ptile, is_celebrating, o);
2365}
2366
2367/**********************************************************************/
2371static inline int city_tile_cache_get_output(const struct city *pcity,
2372 int city_tile_index,
2373 enum output_type_id o)
2374{
2376 == city_map_radius_sq_get(pcity), 0);
2377 fc_assert_ret_val(city_tile_index < city_map_tiles_from_city(pcity), 0);
2378
2379 return (pcity->tile_cache[city_tile_index]).output[o];
2380}
2381
2382/**********************************************************************/
2385static void set_surpluses(struct city *pcity)
2386{
2388 pcity->surplus[o] = pcity->prod[o] - pcity->usage[o];
2390}
2391
2392/**********************************************************************/
2395static void happy_copy(struct city *pcity, enum citizen_feeling i)
2396{
2397 int c = 0;
2398
2399 for (; c < CITIZEN_LAST; c++) {
2400 pcity->feel[c][i] = pcity->feel[c][i - 1];
2401 }
2402}
2403
2404/**********************************************************************/
2407static void citizen_base_mood(struct city *pcity)
2408{
2409 struct player *pplayer = city_owner(pcity);
2410 citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_BASE];
2411 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_BASE];
2412 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_BASE];
2413 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_BASE];
2414 citizens size = city_size_get(pcity);
2415 citizens spes = city_specialists(pcity);
2416
2417 /* This is the number of citizens that may start out content, depending
2418 * on empire size and game's city unhappysize. This may be bigger than
2419 * the size of the city, since this is a potential. */
2420 citizens base_content = player_content_citizens(pplayer);
2421 /* Similarly, this is the potential number of angry citizens. */
2422 citizens base_angry = player_angry_citizens(pplayer);
2423
2424 /* Create content citizens. Take specialists from their ranks. */
2425 *content = MAX(0, MIN(size, base_content) - spes);
2426
2427 /* Create angry citizens. Specialists never become angry. */
2428 fc_assert_action(base_content == 0 || base_angry == 0, *content = 0);
2429 *angry = MIN(base_angry, size - spes);
2430
2431 /* Create unhappy citizens. In the beginning, all who are not content,
2432 * specialists or angry are unhappy. This is changed by luxuries and
2433 * buildings later. */
2434 *unhappy = (size - spes - *content - *angry);
2435
2436 /* No one is born happy. */
2437 *happy = 0;
2438}
2439
2440/**********************************************************************/
2446static inline void citizen_luxury_happy(struct city *pcity, int *luxuries)
2447{
2448 citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_LUXURY];
2449 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_LUXURY];
2450 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_LUXURY];
2451 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_LUXURY];
2452
2453 while (*luxuries >= game.info.happy_cost && *angry > 0) {
2454 /* Upgrade angry to unhappy: costs HAPPY_COST each. */
2455 (*angry)--;
2456 (*unhappy)++;
2457 *luxuries -= game.info.happy_cost;
2458 }
2459 while (*luxuries >= game.info.happy_cost && *content > 0) {
2460 /* Upgrade content to happy: costs HAPPY_COST each. */
2461 (*content)--;
2462 (*happy)++;
2463 *luxuries -= game.info.happy_cost;
2464 }
2465 while (*luxuries >= 2 * game.info.happy_cost && *unhappy > 0) {
2466 /* Upgrade unhappy to happy. Note this is a 2-level upgrade with
2467 * double the cost. */
2468 (*unhappy)--;
2469 (*happy)++;
2470 *luxuries -= 2 * game.info.happy_cost;
2471 }
2472 if (*luxuries >= game.info.happy_cost && *unhappy > 0) {
2473 /* Upgrade unhappy to content: costs HAPPY_COST each. */
2474 (*unhappy)--;
2475 (*content)++;
2476 *luxuries -= game.info.happy_cost;
2477 }
2478}
2479
2480/**********************************************************************/
2483static inline void citizen_happy_luxury(struct city *pcity)
2484{
2485 int x = pcity->prod[O_LUXURY];
2486
2487 citizen_luxury_happy(pcity, &x);
2488}
2489
2490/**********************************************************************/
2493static inline void citizen_content_buildings(struct city *pcity)
2494{
2495 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_EFFECT];
2496 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_EFFECT];
2497 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_EFFECT];
2498 int faces = get_city_bonus(pcity, EFT_MAKE_CONTENT);
2499
2500 /* make people content (but not happy):
2501 get rid of angry first, then make unhappy content. */
2502 while (faces > 0 && *angry > 0) {
2503 (*angry)--;
2504 (*unhappy)++;
2505 faces--;
2506 }
2507 while (faces > 0 && *unhappy > 0) {
2508 (*unhappy)--;
2509 (*content)++;
2510 faces--;
2511 }
2512}
2513
2514/**********************************************************************/
2517static inline void citizen_happiness_nationality(struct city *pcity)
2518{
2520 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_NATIONALITY];
2521 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_NATIONALITY];
2522
2524 int pct = get_city_bonus(pcity, EFT_ENEMY_CITIZEN_UNHAPPY_PCT);
2525
2526 if (pct > 0) {
2527 int enemies = 0;
2528 int unhappy_inc;
2529 struct player *owner = city_owner(pcity);
2530
2531 citizens_foreign_iterate(pcity, pslot, nationality) {
2533 enemies += nationality;
2534 }
2536
2537 unhappy_inc = enemies * pct / 100;
2538
2539 /* First make content => unhappy, then happy => unhappy,
2540 * then happy => content. No-one becomes angry. */
2541 while (unhappy_inc > 0 && *content > 0) {
2542 (*content)--;
2543 (*unhappy)++;
2544 unhappy_inc--;
2545 }
2546 while (unhappy_inc > 1 && *happy > 0) {
2547 (*happy)--;
2548 (*unhappy)++;
2549 unhappy_inc -= 2;
2550 }
2551 while (unhappy_inc > 0 && *happy > 0) {
2552 (*happy)--;
2553 (*content)++;
2554 unhappy_inc--;
2555 }
2556 }
2557 }
2558}
2559
2560/**********************************************************************/
2566static inline void citizen_happy_units(struct city *pcity)
2567{
2568 citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_MARTIAL];
2569 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_MARTIAL];
2570 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_MARTIAL];
2571 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_MARTIAL];
2572 citizens amt = pcity->martial_law;
2573
2574 /* Pacify discontent citizens through martial law. First convert
2575 * angry => unhappy, then unhappy => content. */
2576 while (amt > 0 && *angry > 0) {
2577 (*angry)--;
2578 (*unhappy)++;
2579 amt--;
2580 }
2581 while (amt > 0 && *unhappy > 0) {
2582 (*unhappy)--;
2583 (*content)++;
2584 amt--;
2585 }
2586
2587 /* Now make citizens unhappier because of military units away from home.
2588 * First make content => unhappy, then happy => unhappy,
2589 * then happy => content. */
2590 amt = pcity->unit_happy_upkeep;
2591 while (amt > 0 && *content > 0) {
2592 (*content)--;
2593 (*unhappy)++;
2594 amt--;
2595 }
2596 while (amt > 1 && *happy > 0) {
2597 (*happy)--;
2598 (*unhappy)++;
2599 amt -= 2;
2600 }
2601 while (amt > 0 && *happy > 0) {
2602 (*happy)--;
2603 (*content)++;
2604 amt--;
2605 }
2606 /* Any remaining unhappiness is lost since angry citizens aren't created
2607 * here. */
2608 /* FIXME: Why not? - Per */
2609}
2610
2611/**********************************************************************/
2614static inline void citizen_happy_wonders(struct city *pcity)
2615{
2616 citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_FINAL];
2617 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_FINAL];
2618 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL];
2619 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_FINAL];
2620 int bonus = get_city_bonus(pcity, EFT_MAKE_HAPPY);
2621
2622 /* First create happy citizens from content, then from unhappy
2623 * citizens; we cannot help angry citizens here. */
2624 while (bonus > 0 && *content > 0) {
2625 (*content)--;
2626 (*happy)++;
2627 bonus--;
2628 }
2629 while (bonus > 1 && *unhappy > 0) {
2630 (*unhappy)--;
2631 (*happy)++;
2632 bonus -= 2;
2633 }
2634 /* The rest falls through and lets unhappy people become content. */
2635
2636 if (get_city_bonus(pcity, EFT_NO_UNHAPPY) > 0) {
2637 *content += *unhappy + *angry;
2638 *unhappy = 0;
2639 *angry = 0;
2640 return;
2641 }
2642
2643 bonus += get_city_bonus(pcity, EFT_FORCE_CONTENT);
2644
2645 /* get rid of angry first, then make unhappy content */
2646 while (bonus > 0 && *angry > 0) {
2647 (*angry)--;
2648 (*unhappy)++;
2649 bonus--;
2650 }
2651 while (bonus > 0 && *unhappy > 0) {
2652 (*unhappy)--;
2653 (*content)++;
2654 bonus--;
2655 }
2656}
2657
2658/**********************************************************************/
2662static inline void unhappy_city_check(struct city *pcity)
2663{
2664 if (city_unhappy(pcity)) {
2666 switch (output_types[o].unhappy_penalty) {
2668 pcity->unhappy_penalty[o] = 0;
2669 break;
2671 pcity->unhappy_penalty[o] = MAX(pcity->prod[o] - pcity->usage[o], 0);
2672 break;
2674 pcity->unhappy_penalty[o] = pcity->prod[o];
2675 break;
2676 }
2677
2678 pcity->prod[o] -= pcity->unhappy_penalty[o];
2680 } else {
2681 memset(pcity->unhappy_penalty, 0,
2682 O_LAST * sizeof(*pcity->unhappy_penalty));
2683 }
2684}
2685
2686/**********************************************************************/
2689int city_pollution_types(const struct city *pcity, int shield_total,
2690 int *pollu_prod, int *pollu_pop, int *pollu_mod)
2691{
2692 int prod, pop, mod;
2693
2694 /* Add one one pollution per shield, multipled by the bonus. */
2695 prod = 100 + get_city_bonus(pcity, EFT_POLLU_PROD_PCT);
2696 prod = shield_total * MAX(prod, 0) / 100;
2697
2698 /* Add one pollution per citizen for baseline combined bonus (100%). */
2699 pop = (100 + get_city_bonus(pcity, EFT_POLLU_POP_PCT))
2700 * (100 + get_city_bonus(pcity, EFT_POLLU_POP_PCT_2))
2701 / 100;
2702 pop = (city_size_get(pcity) * MAX(pop, 0)) / 100;
2703
2704 /* Then there is base pollution (usually a negative number). */
2705 mod = game.info.base_pollution;
2706
2707 if (pollu_prod) {
2708 *pollu_prod = prod;
2709 }
2710 if (pollu_pop) {
2711 *pollu_pop = pop;
2712 }
2713 if (pollu_mod) {
2714 *pollu_mod = mod;
2715 }
2716 return MAX(prod + pop + mod, 0);
2717}
2718
2719/**********************************************************************/
2723int city_pollution(const struct city *pcity, int shield_total)
2724{
2725 return city_pollution_types(pcity, shield_total, NULL, NULL, NULL);
2726}
2727
2728/**********************************************************************/
2735static int get_trade_illness(const struct city *pcity)
2736{
2737 float illness_trade = 0.0;
2738
2740 if (trade_city->turn_plague != -1
2741 && game.info.turn - trade_city->turn_plague < 5) {
2742 illness_trade += (float)game.info.illness_trade_infection
2743 * sqrt(1.0 * city_size_get(pcity)
2744 * city_size_get(trade_city)) / 100.0;
2745 }
2747
2748 return (int)illness_trade;
2749}
2750
2751/**********************************************************************/
2755static int get_city_health(const struct city *pcity)
2756{
2757 return get_city_bonus(pcity, EFT_HEALTH_PCT);
2758}
2759
2760/**********************************************************************/
2772int city_illness_calc(const struct city *pcity, int *ill_base,
2773 int *ill_size, int *ill_trade, int *ill_pollution)
2774{
2775 int illness_size = 0, illness_trade = 0, illness_pollution = 0;
2776 int illness_base, illness_percent;
2777
2778 if (game.info.illness_on
2780 /* offset the city size by game.info.illness_min_size */
2781 int use_size = city_size_get(pcity) - game.info.illness_min_size;
2782
2783 illness_size = (int)((1.0 - exp(- (float)use_size / 10.0))
2784 * 10.0 * game.info.illness_base_factor);
2785 if (is_server()) {
2786 /* on the server we recalculate the illness due to trade as we have
2787 * all informations */
2788 illness_trade = get_trade_illness(pcity);
2789 } else {
2790 /* on the client we have to rely on the value saved within the city
2791 * struct */
2792 illness_trade = pcity->illness_trade;
2793 }
2794
2795 illness_pollution = pcity->pollution
2797 }
2798
2799 illness_base = illness_size + illness_trade + illness_pollution;
2800 illness_percent = 100 - get_city_health(pcity);
2801
2802 /* returning other data */
2803 if (ill_size) {
2804 *ill_size = illness_size;
2805 }
2806
2807 if (ill_trade) {
2808 *ill_trade = illness_trade;
2809 }
2810
2811 if (ill_pollution) {
2812 *ill_pollution = illness_pollution;
2813 }
2814
2815 if (ill_base) {
2816 *ill_base = illness_base;
2817 }
2818
2819 return CLIP(0, illness_base * illness_percent / 100 , 999);
2820}
2821
2822/**********************************************************************/
2825bool city_had_recent_plague(const struct city *pcity)
2826{
2827 /* Correctly handles special case turn_plague == -1 (never) */
2828 return (pcity->turn_plague == game.info.turn);
2829}
2830
2831/**********************************************************************/
2834int city_build_slots(const struct city *pcity)
2835{
2836 return get_city_bonus(pcity, EFT_CITY_BUILD_SLOTS);
2837}
2838
2839/**********************************************************************/
2844int city_airlift_max(const struct city *pcity)
2845{
2846 return get_city_bonus(pcity, EFT_AIRLIFT);
2847}
2848
2849/**********************************************************************/
2855inline void set_city_production(struct city *pcity)
2856{
2857 /* Calculate city production!
2858 *
2859 * This is a rather complicated process if we allow rules to become
2860 * more generalized. We can assume that there are no recursive dependency
2861 * loops, but there are some dependencies that do not follow strict
2862 * ordering. For instance corruption must be calculated before
2863 * trade taxes can be counted up, which must occur before the science bonus
2864 * is added on. But the calculation of corruption must include the
2865 * trade bonus. To do this without excessive special casing means that in
2866 * this case the bonuses are multiplied on twice (but only saved the second
2867 * time).
2868 */
2869
2871 pcity->prod[o] = pcity->citizen_base[o];
2873
2874 /* Add on special extra incomes: trade routes and tithes. */
2875 trade_routes_iterate(pcity, proute) {
2876 struct city *tcity = game_city_by_number(proute->partner);
2877 bool can_trade;
2878
2879 /* Partner city may have not yet been sent to the client, or
2880 * there's just a placeholder city with a placeholder owner
2881 * created for some tile->worked. */
2882 if (!is_server()
2883 && (tcity == NULL
2884 || city_owner(tcity)->slot == NULL)) {
2885 continue;
2886 }
2887
2888 fc_assert_action(tcity != NULL, continue);
2889
2890 can_trade = can_cities_trade(pcity, tcity);
2891
2892 if (!can_trade) {
2893 enum trade_route_type type = cities_trade_route_type(pcity, tcity);
2895
2896 if (settings->cancelling == TRI_ACTIVE) {
2897 can_trade = TRUE;
2898 }
2899 }
2900
2901 if (can_trade) {
2902 int value;
2903
2904 value =
2905 trade_base_between_cities(pcity, game_city_by_number(proute->partner));
2906 proute->value = trade_from_route(pcity, proute, value);
2907 pcity->prod[O_TRADE] += proute->value
2908 * (100 + get_city_bonus(pcity, EFT_TRADE_ROUTE_PCT)) / 100;
2909 } else {
2910 proute->value = 0;
2911 }
2913 pcity->prod[O_GOLD] += get_city_tithes_bonus(pcity);
2914
2915 /* Account for waste. Note that waste is calculated before tax income is
2916 * calculated, so if you had "science waste" it would not include taxed
2917 * science. However waste is calculated after the bonuses are multiplied
2918 * on, so shield waste will include shield bonuses. */
2920 pcity->waste[o] = city_waste(pcity, o,
2921 pcity->prod[o] * pcity->bonus[o] / 100,
2922 NULL);
2924
2925 /* Convert trade into science/luxury/gold, and add this on to whatever
2926 * science/luxury/gold is already there. */
2928 pcity->prod[O_TRADE] * pcity->bonus[O_TRADE] / 100
2929 - pcity->waste[O_TRADE] - pcity->usage[O_TRADE],
2930 pcity->prod);
2931
2932 /* Add on effect bonuses and waste. Note that the waste calculation
2933 * (above) already includes the bonus multiplier. */
2935 pcity->prod[o] = pcity->prod[o] * pcity->bonus[o] / 100;
2936 pcity->prod[o] -= pcity->waste[o];
2938}
2939
2940/**********************************************************************/
2943int city_unit_unhappiness(const struct civ_map *nmap,
2944 struct unit *punit, int *free_unhappy)
2945{
2946 struct city *pcity;
2947 const struct unit_type *ut;
2948 struct player *plr;
2949 int happy_cost;
2950
2951 if (punit == NULL || free_unhappy == NULL) {
2952 return 0;
2953 }
2954
2956 if (pcity == NULL) {
2957 return 0;
2958 }
2959
2960 ut = unit_type_get(punit);
2961 plr = unit_owner(punit);
2962 happy_cost = utype_happy_cost(ut, plr);
2963
2964 if (happy_cost <= 0) {
2965 return 0;
2966 }
2967
2968 fc_assert_ret_val(0 <= *free_unhappy, 0);
2969
2971 return 0;
2972 }
2973
2974 happy_cost -= get_city_bonus(pcity, EFT_MAKE_CONTENT_MIL_PER);
2975 if (happy_cost <= 0) {
2976 return 0;
2977 }
2978
2979 if (*free_unhappy >= happy_cost) {
2980 *free_unhappy -= happy_cost;
2981 return 0;
2982 } else {
2983 happy_cost -= *free_unhappy;
2984 *free_unhappy = 0;
2985 }
2986
2987 return happy_cost;
2988}
2989
2990/**********************************************************************/
2994static inline void city_support(const struct civ_map *nmap,
2995 struct city *pcity)
2996{
2997 int free_unhappy, martial_law_each;
2998
2999 /* Clear all usage values. */
3000 memset(pcity->usage, 0, O_LAST * sizeof(*pcity->usage));
3001 pcity->martial_law = 0;
3002 pcity->unit_happy_upkeep = 0;
3003
3004 /* Building and unit gold upkeep depends on the setting
3005 * 'game.info.gold_upkeep_style':
3006 * GOLD_UPKEEP_CITY: The upkeep for buildings and units is paid by the
3007 * city.
3008 * GOLD_UPKEEP_MIXED: The upkeep for buildings is paid by the city.
3009 * The upkeep for units is paid by the nation.
3010 * GOLD_UPKEEP_NATION: The upkeep for buildings and units is paid by the
3011 * nation. */
3012 fc_assert_msg(gold_upkeep_style_is_valid(game.info.gold_upkeep_style),
3013 "Invalid gold_upkeep_style %d", game.info.gold_upkeep_style);
3014 switch (game.info.gold_upkeep_style) {
3015 case GOLD_UPKEEP_CITY:
3016 pcity->usage[O_GOLD] += city_total_unit_gold_upkeep(pcity);
3017 fc__fallthrough; /* No break */
3018 case GOLD_UPKEEP_MIXED:
3019 pcity->usage[O_GOLD] += city_total_impr_gold_upkeep(pcity);
3020 break;
3021 case GOLD_UPKEEP_NATION:
3022 /* nothing */
3023 break;
3024 }
3025 /* Food consumption by citizens. */
3026 pcity->usage[O_FOOD] += game.info.food_cost * city_size_get(pcity);
3027
3028 /* military units in this city (need _not_ be home city) can make
3029 * unhappy citizens content */
3030 martial_law_each = get_city_bonus(pcity, EFT_MARTIAL_LAW_EACH);
3031 if (martial_law_each > 0) {
3032 int count = 0;
3033 int martial_law_max = get_city_bonus(pcity, EFT_MARTIAL_LAW_MAX);
3034
3035 unit_list_iterate(pcity->tile->units, punit) {
3036 if ((count < martial_law_max || martial_law_max == 0)
3038 && unit_owner(punit) == city_owner(pcity)) {
3039 count++;
3040 }
3042
3043 pcity->martial_law = CLIP(0, count * martial_law_each, MAX_CITY_SIZE);
3044 }
3045
3046 free_unhappy = get_city_bonus(pcity, EFT_MAKE_CONTENT_MIL);
3048 pcity->unit_happy_upkeep += city_unit_unhappiness(nmap, punit, &free_unhappy);
3050 if (O_GOLD != o) {
3051 /* O_GOLD is handled with "game.info.gold_upkeep_style", see over. */
3052 pcity->usage[o] += punit->upkeep[o];
3053 }
3056}
3057
3058/**********************************************************************/
3070void city_refresh_from_main_map(const struct civ_map *nmap,
3071 struct city *pcity, bool *workers_map)
3072{
3073 if (workers_map == NULL) {
3074 /* do a full refresh */
3075
3076 /* Calculate the bonus[] array values. */
3077 set_city_bonuses(pcity);
3078 /* Calculate the tile_cache[] values. */
3079 city_tile_cache_update(nmap, pcity);
3080 /* manage settlers, and units */
3081 city_support(nmap, pcity);
3082 }
3083
3084 /* Calculate output from citizens (uses city_tile_cache_get_output()). */
3085 get_worked_tile_output(nmap, pcity, pcity->citizen_base, workers_map);
3086 add_specialist_output(pcity, pcity->citizen_base);
3087
3088 set_city_production(pcity);
3089 citizen_base_mood(pcity);
3090 /* Note that pollution is calculated before unhappy_city_check() makes
3091 * deductions for disorder; so a city in disorder still causes pollution */
3092 pcity->pollution = city_pollution(pcity, pcity->prod[O_SHIELD]);
3093
3094 happy_copy(pcity, FEELING_LUXURY);
3095 citizen_happy_luxury(pcity); /* With our new found luxuries */
3096
3097 happy_copy(pcity, FEELING_EFFECT);
3099
3102
3103 /* Martial law & unrest from units */
3105 citizen_happy_units(pcity);
3106
3107 /* Building (including wonder) happiness effects */
3108 happy_copy(pcity, FEELING_FINAL);
3109 citizen_happy_wonders(pcity);
3110
3111 unhappy_city_check(pcity);
3112 set_surpluses(pcity);
3113}
3114
3115/**********************************************************************/
3122int city_waste(const struct city *pcity, Output_type_id otype, int total,
3123 int *breakdown)
3124{
3125 int penalty_waste = 0;
3126 int penalty_size = 0; /* separate notradesize/fulltradesize from normal
3127 * corruption */
3128 int total_eft = total; /* normal corruption calculated on total reduced by
3129 * possible size penalty */
3130 int waste_level = get_city_output_bonus(pcity, get_output_type(otype),
3131 EFT_OUTPUT_WASTE);
3132 bool waste_all = FALSE;
3133
3134 if (otype == O_TRADE) {
3135 /* FIXME: special case for trade: it is affected by notradesize and
3136 * fulltradesize server settings.
3137 *
3138 * If notradesize and fulltradesize are equal then the city gets no
3139 * trade at that size. */
3140 int notradesize = MIN(game.info.notradesize, game.info.fulltradesize);
3141 int fulltradesize = MAX(game.info.notradesize, game.info.fulltradesize);
3142
3143 if (city_size_get(pcity) <= notradesize) {
3144 penalty_size = total_eft; /* Then no trade income. */
3145 } else if (city_size_get(pcity) >= fulltradesize) {
3146 penalty_size = 0;
3147 } else {
3148 penalty_size = total_eft * (fulltradesize - city_size_get(pcity))
3149 / (fulltradesize - notradesize);
3150 }
3151 }
3152
3153 /* Apply corruption only to anything left after tradesize */
3154 total_eft -= penalty_size;
3155
3156 /* Distance-based waste.
3157 * Don't bother calculating if there's nothing left to lose. */
3158 if (total_eft > 0) {
3159 int waste_by_dist = get_city_output_bonus(pcity, get_output_type(otype),
3160 EFT_OUTPUT_WASTE_BY_DISTANCE);
3161 int waste_by_rel_dist = get_city_output_bonus(pcity, get_output_type(otype),
3162 EFT_OUTPUT_WASTE_BY_REL_DISTANCE);
3163 if (waste_by_dist > 0 || waste_by_rel_dist > 0) {
3164 const struct city *gov_center = NULL;
3165 int min_dist = FC_INFINITY;
3166
3167 /* Check the special case that city itself is gov center
3168 * before expensive iteration through all cities. */
3169 if (is_gov_center(pcity)) {
3170 gov_center = pcity;
3171 min_dist = 0;
3172 } else {
3173 city_list_iterate(city_owner(pcity)->cities, gc) {
3174 /* Do not recheck current city */
3175 if (gc != pcity && is_gov_center(gc)) {
3176 int dist = real_map_distance(gc->tile, pcity->tile);
3177
3178 if (dist < min_dist) {
3179 gov_center = gc;
3180 min_dist = dist;
3181 }
3182 }
3184 }
3185
3186 if (gov_center == NULL) {
3187 waste_all = TRUE; /* no gov center - no income */
3188 } else {
3189 waste_level += waste_by_dist * min_dist / 100;
3190 if (waste_by_rel_dist > 0) {
3191 /* Multiply by 50 as an "standard size" for which EFT_OUTPUT_WASTE_BY_DISTANCE
3192 * and EFT_OUTPUT_WASTE_BY_REL_DISTANCE would give same result. */
3193 waste_level += waste_by_rel_dist * 50 * min_dist / 100
3194 / MAX(wld.map.xsize, wld.map.ysize);
3195 }
3196 }
3197 }
3198 }
3199
3200 if (waste_all) {
3201 penalty_waste = total_eft;
3202 } else {
3203 int waste_pct = get_city_output_bonus(pcity, get_output_type(otype),
3204 EFT_OUTPUT_WASTE_PCT);
3205
3206 /* corruption/waste calculated only for the actually produced amount */
3207 if (waste_level > 0) {
3208 penalty_waste = total_eft * waste_level / 100;
3209 }
3210
3211 /* bonus calculated only for the actually produced amount */
3212 penalty_waste -= penalty_waste * waste_pct / 100;
3213
3214 /* Clip */
3215 penalty_waste = MIN(MAX(penalty_waste, 0), total_eft);
3216 }
3217
3218 if (breakdown) {
3219 breakdown[OLOSS_WASTE] = penalty_waste;
3220 breakdown[OLOSS_SIZE] = penalty_size;
3221 }
3222
3223 /* add up total penalty */
3224 return penalty_waste + penalty_size;
3225}
3226
3227/**********************************************************************/
3230citizens city_specialists(const struct city *pcity)
3231{
3232 citizens count = 0;
3233
3235 fc_assert_ret_val(MAX_CITY_SIZE - count > pcity->specialists[sp], 0);
3236 count += pcity->specialists[sp];
3238
3239 return count;
3240}
3241
3242/**********************************************************************/
3248 const struct city *pcity)
3249{
3250 int best = DEFAULT_SPECIALIST;
3251 int val = get_specialist_output(pcity, best, otype);
3252
3254 if (!pcity || city_can_use_specialist(pcity, i)) {
3255 int val2 = get_specialist_output(pcity, i, otype);
3256
3257 if (val2 > val) {
3258 best = i;
3259 val = val2;
3260 }
3261 }
3263
3264 return best;
3265}
3266
3267/**********************************************************************/
3270void city_add_improvement(struct city *pcity,
3271 const struct impr_type *pimprove)
3272{
3273 pcity->built[improvement_index(pimprove)].turn = game.info.turn; /*I_ACTIVE*/
3274
3275 if (is_server() && is_wonder(pimprove)) {
3276 /* Client just read the info from the packets. */
3277 wonder_built(pcity, pimprove);
3278 }
3279}
3280
3281/**********************************************************************/
3285 const struct impr_type *pimprove)
3286{
3287 log_debug("Improvement %s removed from city %s",
3288 improvement_rule_name(pimprove), pcity->name);
3289
3290 pcity->built[improvement_index(pimprove)].turn = I_DESTROYED;
3291
3292 if (is_server() && is_wonder(pimprove)) {
3293 /* Client just read the info from the packets. */
3294 wonder_destroyed(pcity, pimprove);
3295 }
3296}
3297
3298/**********************************************************************/
3301bool is_city_option_set(const struct city *pcity, enum city_options option)
3302{
3303 return BV_ISSET(pcity->city_options, option);
3304}
3305
3306/**********************************************************************/
3310{
3311 int i;
3312
3313 city_styles = fc_calloc(num, sizeof(*city_styles));
3315
3316 for (i = 0; i < game.control.styles_count; i++) {
3317 requirement_vector_init(&city_styles[i].reqs);
3318 }
3319}
3320
3321/**********************************************************************/
3325{
3326 int i;
3327
3328 for (i = 0; i < game.control.styles_count; i++) {
3329 requirement_vector_free(&city_styles[i].reqs);
3330 }
3331
3332 free(city_styles);
3333 city_styles = NULL;
3335}
3336
3337/**********************************************************************/
3343struct city *create_city_virtual(struct player *pplayer,
3344 struct tile *ptile, const char *name)
3345{
3346 int i;
3347
3348 /* Make sure that contents of city structure are correctly initialized,
3349 * if you ever allocate it by some other mean than fc_calloc() */
3350 struct city *pcity = fc_calloc(1, sizeof(*pcity));
3351
3352 fc_assert_ret_val(NULL != name, NULL); /* No unnamed cities! */
3353
3354 /* Do this early, so any logging later will have the city name */
3355 city_name_set(pcity, name);
3356
3357 pcity->tile = ptile;
3358 fc_assert_ret_val(NULL != pplayer, NULL); /* No unowned cities! */
3359 pcity->owner = pplayer;
3360
3361 if (is_server()) {
3362 pcity->original = pplayer;
3363 }
3364
3365 /* City structure was allocated with fc_calloc(), so contents are initially
3366 * zero. There is no need to initialize it a second time. */
3367
3368 /* Now set some useful default values. */
3369 pcity->capital = CAPITAL_NOT;
3370 city_size_set(pcity, 1);
3371 pcity->specialists[DEFAULT_SPECIALIST] = 1;
3372
3374 pcity->bonus[o] = 100;
3376
3377 pcity->turn_plague = -1; /* -1 = never */
3378 pcity->did_buy = FALSE;
3380 pcity->turn_founded = game.info.turn;
3381 pcity->turn_last_built = game.info.turn;
3382
3383 pcity->tile_cache_radius_sq = -1; /* -1 = tile_cache must be initialised */
3384
3385 /* pcity->ai.act_cache: worker activities on the city map */
3386
3387 /* Initialise improvements list */
3388 for (i = 0; i < ARRAY_SIZE(pcity->built); i++) {
3389 pcity->built[i].turn = I_NEVER;
3390 }
3391
3392 /* Set up the worklist */
3393 worklist_init(&pcity->worklist);
3394
3395 pcity->units_supported = unit_list_new();
3396 pcity->routes = trade_route_list_new();
3397 pcity->task_reqs = worker_task_list_new();
3398
3399 if (is_server()) {
3400 pcity->server.mgr_score_calc_turn = -1; /* -1 = never */
3401
3402 CALL_FUNC_EACH_AI(city_alloc, pcity);
3403 } else {
3405 unit_list_new_full(unit_virtual_destroy);
3406 pcity->client.info_units_present =
3407 unit_list_new_full(unit_virtual_destroy);
3408 /* collecting_info_units_supported set by fc_calloc().
3409 * collecting_info_units_present set by fc_calloc(). */
3410 }
3411
3412 return pcity;
3413}
3414
3415/**********************************************************************/
3419void destroy_city_virtual(struct city *pcity)
3420{
3421 CALL_FUNC_EACH_AI(city_free, pcity);
3422
3423 citizens_free(pcity);
3424
3425 /* Free worker tasks */
3426 while (worker_task_list_size(pcity->task_reqs) > 0) {
3427 struct worker_task *ptask = worker_task_list_get(pcity->task_reqs, 0);
3428
3429 worker_task_list_remove(pcity->task_reqs, ptask);
3430
3431 free(ptask);
3432 }
3433 worker_task_list_destroy(pcity->task_reqs);
3434
3435 /* Free rally points */
3437
3438 unit_list_destroy(pcity->units_supported);
3439 trade_route_list_destroy(pcity->routes);
3440 if (pcity->tile_cache != NULL) {
3441 free(pcity->tile_cache);
3442 }
3443
3444 if (pcity->cm_parameter) {
3445 free(pcity->cm_parameter);
3446 }
3447
3448 if (!is_server()) {
3449 unit_list_destroy(pcity->client.info_units_supported);
3450 unit_list_destroy(pcity->client.info_units_present);
3451 /* Handle a rare case where the game is freed in the middle of a
3452 * spy/diplomat investigate cycle. */
3453 if (pcity->client.collecting_info_units_supported != NULL) {
3454 unit_list_destroy(pcity->client.collecting_info_units_supported);
3455 }
3456 if (pcity->client.collecting_info_units_present != NULL) {
3457 unit_list_destroy(pcity->client.collecting_info_units_present);
3458 }
3459 }
3460
3461 free(pcity->name);
3462
3463 memset(pcity, 0, sizeof(*pcity)); /* ensure no pointers remain */
3464 free(pcity);
3465}
3466
3467/**********************************************************************/
3471bool city_exist(int id)
3472{
3473 /* Check if city exist in game */
3474 if (game_city_by_number(id)) {
3475 return TRUE;
3476 }
3477
3478 return FALSE;
3479}
3480
3481/**********************************************************************/
3488bool city_is_virtual(const struct city *pcity)
3489{
3490 if (!pcity) {
3491 return FALSE;
3492 }
3493
3494 return pcity != game_city_by_number(pcity->id);
3495}
3496
3497/**********************************************************************/
3500bool is_free_worked(const struct city *pcity, const struct tile *ptile)
3501{
3502 return is_city_center(pcity, ptile);
3503}
3504
3505/**********************************************************************/
3508void *city_ai_data(const struct city *pcity, const struct ai_type *ai)
3509{
3510 return pcity->server.ais[ai_type_number(ai)];
3511}
3512
3513/**********************************************************************/
3516void city_set_ai_data(struct city *pcity, const struct ai_type *ai,
3517 void *data)
3518{
3519 pcity->server.ais[ai_type_number(ai)] = data;
3520}
3521
3522/**********************************************************************/
3525void city_rally_point_clear(struct city *pcity)
3526{
3527 /* Free rally points */
3528 if (pcity->rally_point.length > 0) {
3529 pcity->rally_point.length = 0;
3530 pcity->rally_point.persistent = FALSE;
3531 pcity->rally_point.vigilant = FALSE;
3532 free(pcity->rally_point.orders);
3533 pcity->rally_point.orders = NULL;
3534 }
3535}
3536
3537/**********************************************************************/
3541 struct city *pcity)
3542{
3543 struct unit_order *checked_orders;
3544 const struct civ_map *nmap = &(wld.map);
3545
3546 if (pcity == NULL) {
3547 /* Probably lost. */
3548 log_verbose("handle_city_rally_point() bad city number %d.",
3549 packet->city_id32);
3550 return;
3551 }
3552
3553 if (0 > packet->length || MAX_LEN_ROUTE < packet->length) {
3554 /* Shouldn't happen */
3555 log_error("city_rally_point_receive() invalid packet length %d (max %d)",
3556 packet->length, MAX_LEN_ROUTE);
3557 return;
3558 }
3559
3560 pcity->rally_point.length = packet->length;
3561
3562 if (packet->length == 0) {
3563 pcity->rally_point.vigilant = FALSE;
3564 pcity->rally_point.persistent = FALSE;
3565 if (pcity->rally_point.orders) {
3566 free(pcity->rally_point.orders);
3567 pcity->rally_point.orders = NULL;
3568 }
3569 } else {
3570 checked_orders = create_unit_orders(nmap, packet->length,
3571 packet->orders);
3572 if (!checked_orders) {
3573 pcity->rally_point.length = 0;
3574 log_error("invalid rally point orders for city number %d.",
3575 packet->city_id32);
3576 return;
3577 }
3578
3579 pcity->rally_point.persistent = packet->persistent;
3580 pcity->rally_point.vigilant = packet->vigilant;
3581 pcity->rally_point.orders = checked_orders;
3582 }
3583}
struct action_enabler_list * action_enablers_for_action(action_id action)
Definition actions.c:2475
#define action_by_result_iterate(_paction_, _result_)
Definition actions.h:488
#define action_enabler_list_iterate_end
Definition actions.h:457
#define action_by_result_iterate_end
Definition actions.h:492
#define action_enabler_list_iterate(action_enabler_list, aenabler)
Definition actions.h:455
int ai_type_number(const struct ai_type *ai)
Definition ai.c:272
#define CALL_FUNC_EACH_AI(_func,...)
Definition ai.h:384
#define BV_CLR_ALL(bv)
Definition bitvector.h:95
#define BV_SET(bv, bit)
Definition bitvector.h:81
#define BV_ISSET(bv, bit)
Definition bitvector.h:78
void citizens_free(struct city *pcity)
Definition citizens.c:58
#define citizens_foreign_iterate_end
Definition citizens.h:63
#define citizens_foreign_iterate(_pcity, _pslot, _nationality)
Definition citizens.h:58
bool base_city_celebrating(const struct city *pcity)
Definition city.c:1610
bool city_is_virtual(const struct city *pcity)
Definition city.c:3488
int city_turns_to_build(const struct city *pcity, const struct universal *target, bool include_shield_stock)
Definition city.c:1938
static void citizen_luxury_happy(struct city *pcity, int *luxuries)
Definition city.c:2446
const char * city_improvement_name_translation(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:658
static void citizen_content_buildings(struct city *pcity)
Definition city.c:2493
bool is_free_worked(const struct city *pcity, const struct tile *ptile)
Definition city.c:3500
bool city_production_gets_caravan_shields(const struct universal *tgt)
Definition city.c:1833
static bv_imprs caravan_helped_impr
Definition city.c:1744
void city_map_radius_sq_set(struct city *pcity, int radius_sq)
Definition city.c:143
int city_production_build_shield_cost(const struct city *pcity)
Definition city.c:722
void city_name_set(struct city *pcity, const char *new_name)
Definition city.c:1123
static int player_base_citizen_happiness(const struct player *pplayer)
Definition city.c:2132
int city_granary_size(int city_size)
Definition city.c:2104
static void get_worked_tile_output(const struct civ_map *nmap, const struct city *pcity, int *output, bool *workers_map)
Definition city.c:2264
citizens player_angry_citizens(const struct player *pplayer)
Definition city.c:2168
int city_pollution_types(const struct city *pcity, int shield_total, int *pollu_prod, int *pollu_pop, int *pollu_mod)
Definition city.c:2689
void city_set_ai_data(struct city *pcity, const struct ai_type *ai, void *data)
Definition city.c:3516
static void citizen_happy_wonders(struct city *pcity)
Definition city.c:2614
bool city_built_last_turn(const struct city *pcity)
Definition city.c:2251
void city_choose_build_default(const struct civ_map *nmap, struct city *pcity)
Definition city.c:1065
int city_waste(const struct city *pcity, Output_type_id otype, int total, int *breakdown)
Definition city.c:3122
static void city_support(const struct civ_map *nmap, struct city *pcity)
Definition city.c:2994
void generate_city_map_indices(void)
Definition city.c:522
int city_build_slots(const struct city *pcity)
Definition city.c:2834
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
bool city_has_building(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:1216
const char * city_style_name_translation(const int style)
Definition city.c:1729
void city_styles_free(void)
Definition city.c:3324
static int city_tile_cache_get_output(const struct city *pcity, int city_tile_index, enum output_type_id o)
Definition city.c:2371
Output_type_id output_type_by_identifier(const char *id)
Definition city.c:642
bool is_capital(const struct city *pcity)
Definition city.c:1552
struct citystyle * city_styles
Definition city.c:79
void city_styles_alloc(int num)
Definition city.c:3309
const char * city_name_get(const struct city *pcity)
Definition city.c:1115
bool base_city_can_work_tile(const struct player *restriction, const struct city *pcity, const struct tile *ptile)
Definition city.c:1377
int city_unit_slots_available(const struct city *pcity)
Definition city.c:1027
void city_production_caravan_shields_init(void)
Definition city.c:1751
void city_remove_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3284
int city_improvement_upkeep(const struct city *pcity, const struct impr_type *b)
Definition city.c:1231
int city_airlift_max(const struct city *pcity)
Definition city.c:2844
bool citymindist_prevents_city_on_tile(const struct civ_map *nmap, const struct tile *ptile)
Definition city.c:1438
bool can_city_build_later(const struct civ_map *nmap, const struct city *pcity, const struct universal *target)
Definition city.c:1009
struct output_type * get_output_type(Output_type_id output)
Definition city.c:633
bool is_city_option_set(const struct city *pcity, enum city_options option)
Definition city.c:3301
int city_population(const struct city *pcity)
Definition city.c:1169
static struct iter_index * city_map_index
Definition city.c:59
const char * city_style_rule_name(const int style)
Definition city.c:1738
void city_size_add(struct city *pcity, int add)
Definition city.c:1142
bool city_can_use_specialist(const struct city *pcity, Specialist_type_id type)
Definition city.c:1043
bool city_production_has_flag(const struct city *pcity, enum impr_flag_id flag)
Definition city.c:712
bool city_is_occupied(const struct city *pcity)
Definition city.c:1638
void free_city_map_index(void)
Definition city.c:604
int city_production_unit_veteran_level(struct city *pcity, const struct unit_type *punittype)
Definition city.c:789
void add_tax_income(const struct player *pplayer, int trade, int *output)
Definition city.c:2217
bool can_city_build_improvement_now(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:839
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
struct city * tile_non_allied_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:2039
int city_unit_unhappiness(const struct civ_map *nmap, struct unit *punit, int *free_unhappy)
Definition city.c:2943
bool city_got_defense_effect(const struct city *pcity, const struct unit_type *attacker)
Definition city.c:1569
citizens player_content_citizens(const struct player *pplayer)
Definition city.c:2158
bool city_map_includes_tile(const struct city *const pcity, const struct tile *map_tile)
Definition city.c:288
static void city_tile_cache_update(const struct civ_map *nmap, struct city *pcity)
Definition city.c:2342
int get_city_tithes_bonus(const struct city *pcity)
Definition city.c:2199
void citylog_map_data(enum log_level level, int radius_sq, int *map_data)
Definition city.c:406
static int city_map_numtiles[CITY_MAP_MAX_RADIUS_SQ+1]
Definition city.c:66
bool city_rapture_grow(const struct city *pcity)
Definition city.c:1626
int city_production_turns_to_build(const struct city *pcity, bool include_shield_stock)
Definition city.c:805
bool city_unhappy(const struct city *pcity)
Definition city.c:1599
bool is_friendly_city_near(const struct civ_map *nmap, const struct player *owner, const struct tile *ptile)
Definition city.c:2065
Specialist_type_id best_specialist(Output_type_id otype, const struct city *pcity)
Definition city.c:3247
struct city * create_city_virtual(struct player *pplayer, struct tile *ptile, const char *name)
Definition city.c:3343
void set_city_production(struct city *pcity)
Definition city.c:2855
struct city * city_list_find_number(struct city_list *This, int id)
Definition city.c:1652
static bv_unit_types caravan_helped_utype
Definition city.c:1745
int get_final_city_output_bonus(const struct city *pcity, Output_type_id otype)
Definition city.c:2184
bool city_celebrating(const struct city *pcity)
Definition city.c:1618
bool city_can_be_built_tile_only(const struct civ_map *nmap, const struct tile *ptile)
Definition city.c:1533
bool can_city_build_improvement_direct(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:815
const char * get_output_identifier(Output_type_id output)
Definition city.c:614
int city_illness_calc(const struct city *pcity, int *ill_base, int *ill_size, int *ill_trade, int *ill_pollution)
Definition city.c:2772
const char * get_output_name(Output_type_id output)
Definition city.c:624
bool city_can_grow_to(const struct city *pcity, int pop_size)
Definition city.c:1985
int city_change_production_penalty(const struct city *pcity, const struct universal *target)
Definition city.c:1859
static int get_city_health(const struct city *pcity)
Definition city.c:2755
bool is_valid_city_coords(const int city_radius_sq, const int city_map_x, const int city_map_y)
Definition city.c:183
void city_refresh_from_main_map(const struct civ_map *nmap, struct city *pcity, bool *workers_map)
Definition city.c:3070
bool can_city_build_direct(const struct civ_map *nmap, const struct city *pcity, const struct universal *target)
Definition city.c:972
static void set_city_bonuses(struct city *pcity)
Definition city.c:2325
static void set_surpluses(struct city *pcity)
Definition city.c:2385
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:1460
int city_pollution(const struct city *pcity, int shield_total)
Definition city.c:2723
bool city_happy(const struct city *pcity)
Definition city.c:1587
void city_size_set(struct city *pcity, citizens size)
Definition city.c:1158
void city_add_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3270
int city_tile_output_now(const struct city *pcity, const struct tile *ptile, Output_type_id otype)
Definition city.c:1360
int city_map_radius_sq_get(const struct city *pcity)
Definition city.c:132
struct city * tile_allied_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:2009
struct city * tile_enemy_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:1994
void citylog_map_workers(enum log_level level, struct city *pcity)
Definition city.c:441
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
void destroy_city_virtual(struct city *pcity)
Definition city.c:3419
int rs_max_city_radius_sq(void)
Definition city.c:154
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
citizens city_specialists(const struct city *pcity)
Definition city.c:3230
static char * citylog_map_line(int y, int city_radius_sq, int *city_map_data)
Definition city.c:364
void city_rally_point_receive(const struct packet_city_rally_point *packet, struct city *pcity)
Definition city.c:3540
static int cmp(int v1, int v2)
Definition city.c:320
#define CITYLOG_MAX_VAL
Definition city.c:363
int city_style_by_translated_name(const char *s)
Definition city.c:1694
void add_specialist_output(const struct city *pcity, int *output)
Definition city.c:2304
void * city_ai_data(const struct city *pcity, const struct ai_type *ai)
Definition city.c:3508
struct city * city_list_find_name(struct city_list *This, const char *name)
Definition city.c:1668
static void citizen_happiness_nationality(struct city *pcity)
Definition city.c:2517
int city_tile_output(const struct city *pcity, const struct tile *ptile, bool is_celebrating, Output_type_id otype)
Definition city.c:1259
static int city_map_xy[CITY_MAP_MAX_SIZE][CITY_MAP_MAX_SIZE]
Definition city.c:63
int city_tile_xy_to_index(int city_map_x, int city_map_y, int city_radius_sq)
Definition city.c:118
bool can_city_build_unit_direct(const struct civ_map *nmap, const struct city *pcity, const struct unit_type *punittype)
Definition city.c:888
bool is_gov_center(const struct city *pcity)
Definition city.c:1560
#define log_ca_s_init
bool can_city_build_now(const struct civ_map *nmap, const struct city *pcity, const struct universal *target)
Definition city.c:991
static void citizen_happy_luxury(struct city *pcity)
Definition city.c:2483
bool can_city_build_unit_now(const struct civ_map *nmap, const struct city *pcity, const struct unit_type *punittype)
Definition city.c:927
int city_map_tiles(int city_radius_sq)
Definition city.c:166
static void unhappy_city_check(struct city *pcity)
Definition city.c:2662
struct city * tile_non_attack_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:2024
bool city_exist(int id)
Definition city.c:3471
bool city_can_work_tile(const struct city *pcity, const struct tile *ptile)
Definition city.c:1429
int compare_iter_index(const void *a, const void *b)
Definition city.c:338
bool city_production_build_units(const struct city *pcity, bool add_production, int *num_units)
Definition city.c:732
bool is_unit_near_a_friendly_city(const struct civ_map *nmap, const struct unit *punit)
Definition city.c:2055
int city_turns_to_grow(const struct city *pcity)
Definition city.c:1969
void city_rally_point_clear(struct city *pcity)
Definition city.c:3525
bool can_city_build_improvement_later(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:856
struct output_type output_types[O_LAST]
Definition city.c:83
bool city_exists_within_max_city_map(const struct civ_map *nmap, const struct tile *ptile, bool may_be_on_center)
Definition city.c:2084
bool city_had_recent_plague(const struct city *pcity)
Definition city.c:2825
static int get_trade_illness(const struct city *pcity)
Definition city.c:2735
bool city_can_change_build(const struct city *pcity)
Definition city.c:1057
bool can_city_build_unit_later(const struct civ_map *nmap, const struct city *pcity, const struct unit_type *punittype)
Definition city.c:948
int city_total_unit_gold_upkeep(const struct city *pcity)
Definition city.c:1198
int city_style_by_rule_name(const char *s)
Definition city.c:1711
static void citizen_base_mood(struct city *pcity)
Definition city.c:2407
int city_name_compare(const void *p1, const void *p2)
Definition city.c:1684
int city_total_impr_gold_upkeep(const struct city *pcity)
Definition city.c:1179
static void citizen_happy_units(struct city *pcity)
Definition city.c:2566
static void happy_copy(struct city *pcity, enum citizen_feeling i)
Definition city.c:2395
const char * city_production_name_translation(const struct city *pcity)
Definition city.c:695
#define city_list_iterate(citylist, pcity)
Definition city.h:488
#define city_tile(_pcity_)
Definition city.h:544
@ UNHAPPY_PENALTY_ALL_PRODUCTION
Definition city.h:248
@ UNHAPPY_PENALTY_NONE
Definition city.h:246
@ UNHAPPY_PENALTY_SURPLUS
Definition city.h:247
#define city_tile_iterate_index(_nmap, _radius_sq, _city_tile, _tile, _index)
Definition city.h:193
#define CITY_MAP_MAX_RADIUS_SQ
Definition city.h:78
static citizens city_size_get(const struct city *pcity)
Definition city.h:549
#define CITY_MAP_MAX_SIZE
Definition city.h:86
#define city_tile_iterate_index_end
Definition city.h:201
@ CITIZEN_LAST
Definition city.h:264
@ CITIZEN_ANGRY
Definition city.h:263
@ CITIZEN_HAPPY
Definition city.h:260
@ CITIZEN_CONTENT
Definition city.h:261
@ CITIZEN_UNHAPPY
Definition city.h:262
#define CITY_MAP_CENTER_RADIUS_SQ
Definition city.h:81
#define CITY_MAP_MIN_RADIUS_SQ
Definition city.h:76
#define output_type_iterate(output)
Definition city.h:821
#define CITY_REL2ABS(_coor)
Definition city.h:107
#define city_owner(_pcity_)
Definition city.h:543
#define MAX_CITY_SIZE
Definition city.h:98
static bool is_city_center(const struct city *pcity, const struct tile *ptile)
Definition city.h:844
#define city_list_iterate_end
Definition city.h:490
#define I_DESTROYED
Definition city.h:240
#define I_NEVER
Definition city.h:239
#define city_tile_iterate(_nmap, _radius_sq, _city_tile, _tile)
Definition city.h:222
citizen_feeling
Definition city.h:270
@ FEELING_EFFECT
Definition city.h:273
@ FEELING_LUXURY
Definition city.h:272
@ FEELING_FINAL
Definition city.h:276
@ FEELING_BASE
Definition city.h:271
@ FEELING_NATIONALITY
Definition city.h:274
@ FEELING_MARTIAL
Definition city.h:275
@ OLOSS_SIZE
Definition city.h:283
@ OLOSS_WASTE
Definition city.h:282
#define city_map_iterate_end
Definition city.h:169
production_class_type
Definition city.h:38
@ PCT_LAST
Definition city.h:42
@ PCT_UNIT
Definition city.h:39
@ PCT_NORMAL_IMPROVEMENT
Definition city.h:40
@ PCT_WONDER
Definition city.h:41
#define city_map_iterate(_radius_sq, _index, _x, _y)
Definition city.h:165
#define city_tile_iterate_end
Definition city.h:230
#define CITY_MAP_MAX_RADIUS
Definition city.h:71
#define city_built_iterate(_pcity, _p)
Definition city.h:810
#define is_free_worked_index(city_tile_index)
Definition city.h:856
#define city_map_tiles_from_city(_pcity)
Definition city.h:119
#define CITY_ABS2REL(_coor)
Definition city.h:108
#define city_built_iterate_end
Definition city.h:816
#define output_type_iterate_end
Definition city.h:827
void cm_init_citymap(void)
Definition cm.c:312
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
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
void distribute(int number, unsigned groups, const unsigned *ratios, int *result)
Definition distribute.c:34
struct @21::@22 reqs
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
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
int get_city_bonus(const struct city *pcity, enum effect_type effect_type)
Definition effects.c:789
int get_city_output_bonus(const struct city *pcity, const struct output_type *poutput, enum effect_type effect_type)
Definition effects.c:906
int get_building_bonus(const struct city *pcity, const struct impr_type *building, enum effect_type effect_type)
Definition effects.c:930
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
int effect_cumulative_max(enum effect_type type, struct universal *unis, size_t n_unis)
Definition effects.c:331
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
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Definition effects.c:771
static bool is_server(void)
unsigned char citizens
Definition fc_types.h:358
@ RPT_CERTAIN
Definition fc_types.h:586
@ RPT_POSSIBLE
Definition fc_types.h:585
int Specialist_type_id
Definition fc_types.h:345
output_type_id
Definition fc_types.h:90
@ O_SHIELD
Definition fc_types.h:91
@ O_FOOD
Definition fc_types.h:91
@ O_TRADE
Definition fc_types.h:91
@ O_SCIENCE
Definition fc_types.h:91
@ O_LUXURY
Definition fc_types.h:91
@ O_GOLD
Definition fc_types.h:91
@ O_LAST
Definition fc_types.h:91
#define MAX_LEN_CITYNAME
Definition fc_types.h:67
enum output_type_id Output_type_id
Definition fc_types.h:348
#define Q_(String)
Definition fcintl.h:70
#define _(String)
Definition fcintl.h:67
#define Qn_(String)
Definition fcintl.h:89
#define N_(String)
Definition fcintl.h:69
struct civ_game game
Definition game.c:57
struct world wld
Definition game.c:58
struct city * game_city_by_number(int id)
Definition game.c:102
struct government * government_of_player(const struct player *pplayer)
Definition government.c:113
struct city * owner
Definition citydlg.c:219
GType type
Definition repodlgs.c:1312
bool can_player_build_improvement_direct(const struct player *p, const struct impr_type *pimprove)
bool is_improvement_redundant(const struct city *pcity, const struct impr_type *pimprove)
bool can_player_build_improvement_later(const struct player *p, const struct impr_type *pimprove)
void wonder_built(const struct city *pcity, const struct impr_type *pimprove)
bool great_wonder_is_destroyed(const struct impr_type *pimprove)
const char * improvement_rule_name(const struct impr_type *pimprove)
Impr_type_id improvement_index(const struct impr_type *pimprove)
bool is_wonder(const struct impr_type *pimprove)
bool is_great_wonder(const struct impr_type *pimprove)
bool improvement_obsolete(const struct player *pplayer, const struct impr_type *pimprove, const struct city *pcity)
bool improvement_has_flag(const struct impr_type *pimprove, enum impr_flag_id flag)
void wonder_destroyed(const struct city *pcity, const struct impr_type *pimprove)
const char * improvement_name_translation(const struct impr_type *pimprove)
bool great_wonder_is_available(const struct impr_type *pimprove)
#define improvement_iterate_end
#define improvement_iterate(_p)
const char * name
Definition inputfile.c:127
#define fc_assert_msg(condition, message,...)
Definition log.h:181
#define fc_assert_ret(condition)
Definition log.h:191
#define log_warn(message,...)
Definition log.h:105
#define log_verbose(message,...)
Definition log.h:109
#define fc_assert(condition)
Definition log.h:176
#define fc_assert_ret_val(condition, val)
Definition log.h:194
#define log_do_output_for_level(level)
Definition log.h:89
#define fc_assert_action(condition, action)
Definition log.h:187
#define log_debug(message,...)
Definition log.h:115
#define log_base(level, message,...)
Definition log.h:94
log_level
Definition log.h:28
@ LOG_DEBUG
Definition log.h:34
#define log_error(message,...)
Definition log.h:103
struct tile * map_pos_to_tile(const struct civ_map *nmap, int map_x, int map_y)
Definition map.c:417
int map_vector_to_sq_distance(int dx, int dy)
Definition map.c:612
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Definition map.c:938
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:628
void map_distance_vector(int *dx, int *dy, const struct tile *tile0, const struct tile *tile1)
Definition map.c:1071
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition map.h:385
#define square_iterate_end
Definition map.h:388
#define index_to_map_pos(pmap_x, pmap_y, mindex)
Definition map.h:227
#define fc_calloc(n, esz)
Definition mem.h:38
#define FC_FREE(ptr)
Definition mem.h:41
#define fc_strdup(str)
Definition mem.h:43
#define fc_realloc(ptr, sz)
Definition mem.h:36
#define fc_malloc(sz)
Definition mem.h:34
bool can_unit_exist_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Definition movement.c:304
bool is_native_near_tile(const struct civ_map *nmap, const struct unit_class *uclass, const struct tile *ptile)
Definition movement.c:450
static const char * rule_name_get(const struct name_translation *ptrans)
static const char * name_translation_get(const struct name_translation *ptrans)
#define MAX_LEN_ROUTE
Definition packets.h:44
struct city_list * cities
Definition packhand.c:117
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1364
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1381
bool pplayers_non_attack(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1435
struct player * player_slot_get_player(const struct player_slot *pslot)
Definition player.c:430
int universal_build_shield_cost(const struct city *pcity, const struct universal *target)
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)
bool req_vec_wants_type(const struct requirement_vector *reqs, enum universals_n kind)
bool does_req_contradicts_reqs(const struct requirement *req, const struct requirement_vector *vec)
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)
const char * universal_name_translation(const struct universal *psource, char *buf, size_t bufsz)
#define requirement_vector_iterate_end
#define requirement_vector_iterate(req_vec, preq)
static struct setting settings[]
Definition settings.c:1378
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:183
#define CLIP(lower, current, upper)
Definition shared.h:57
#define ARRAY_SIZE(x)
Definition shared.h:85
#define MIN(x, y)
Definition shared.h:55
#define FC_INFINITY
Definition shared.h:36
#define MAX(x, y)
Definition shared.h:54
struct specialist * specialist_by_number(const Specialist_type_id id)
Definition specialist.c:100
int get_specialist_output(const struct city *pcity, Specialist_type_id sp, Output_type_id otype)
Definition specialist.c:217
#define specialist_type_iterate_end
Definition specialist.h:79
#define specialist_type_iterate(sp)
Definition specialist.h:73
#define DEFAULT_SPECIALIST
Definition specialist.h:43
int step
Definition specpq.h:92
size_t size
Definition specvec.h:72
Definition ai.h:50
int turn
Definition city.h:238
Definition city.h:309
struct worker_task_list * task_reqs
Definition city.h:395
struct tile_cache * tile_cache
Definition city.h:337
int turn_last_built
Definition city.h:373
int surplus[O_LAST]
Definition city.h:343
int food_stock
Definition city.h:354
struct built_status built[B_LAST]
Definition city.h:380
struct player * original
Definition city.h:313
int pollution
Definition city.h:356
int id
Definition city.h:315
int last_turns_shield_surplus
Definition city.h:378
enum capital_type capital
Definition city.h:317
int disbanded_shields
Definition city.h:377
int waste[O_LAST]
Definition city.h:344
int turn_plague
Definition city.h:361
struct unit_list * info_units_present
Definition city.h:457
bv_city_options city_options
Definition city.h:389
citizens unit_happy_upkeep
Definition city.h:327
int city_radius_sq
Definition city.h:362
bool was_happy
Definition city.h:368
struct player * owner
Definition city.h:312
int turn_founded
Definition city.h:372
int citizen_base[O_LAST]
Definition city.h:347
int caravan_shields
Definition city.h:376
bool did_buy
Definition city.h:366
struct unit_list * info_units_supported
Definition city.h:456
char * name
Definition city.h:310
void * ais[FREECIV_AI_MOD_LAST]
Definition city.h:436
struct trade_route_list * routes
Definition city.h:332
bool occupied
Definition city.h:443
int usage[O_LAST]
Definition city.h:348
struct worklist worklist
Definition city.h:387
int tile_cache_radius_sq
Definition city.h:340
struct universal production
Definition city.h:382
struct unit_order * orders
Definition city.h:405
struct city::@16 rally_point
struct unit_list * collecting_info_units_supported
Definition city.h:460
int bonus[O_LAST]
Definition city.h:351
bool vigilant
Definition city.h:404
int unhappy_penalty[O_LAST]
Definition city.h:345
citizens size
Definition city.h:320
int before_change_shields
Definition city.h:375
int style
Definition city.h:316
size_t length
Definition city.h:400
struct unit_list * collecting_info_units_present
Definition city.h:461
citizens feel[CITIZEN_LAST][FEELING_LAST]
Definition city.h:321
citizens specialists[SP_MAX]
Definition city.h:324
struct tile * tile
Definition city.h:311
int shield_stock
Definition city.h:355
int prod[O_LAST]
Definition city.h:346
struct city::@17::@19 server
struct cm_parameter * cm_parameter
Definition city.h:408
struct universal changed_from
Definition city.h:385
struct unit_list * units_supported
Definition city.h:391
int mgr_score_calc_turn
Definition city.h:415
int illness_trade
Definition city.h:357
bool persistent
Definition city.h:402
struct city::@17::@20 client
int rapture
Definition city.h:371
citizens martial_law
Definition city.h:326
struct packet_ruleset_control control
Definition game.h:83
struct packet_game_info info
Definition game.h:89
struct government * government_during_revolution
Definition game.h:94
int xsize
Definition map_types.h:77
int ysize
Definition map_types.h:77
struct requirement_vector reqs
Definition improvement.h:75
int dist
Definition city.h:102
int dx
Definition city.h:102
int dy
Definition city.h:102
const char * id
Definition city.h:254
struct unit_order orders[MAX_LEN_ROUTE]
enum gold_upkeep_style gold_upkeep_style
int granary_food_ini[MAX_GRANARY_INIS]
int min_city_center_output[O_LAST]
struct city_list * cities
Definition player.h:281
struct unit_list * units
Definition player.h:282
struct player_economic economic
Definition player.h:284
const struct player * player
enum req_range range
struct universal source
struct requirement_vector reqs
Definition specialist.h:38
int irrigation_food_incr
Definition terrain.h:209
int output[O_LAST]
Definition terrain.h:195
int mining_shield_incr
Definition terrain.h:212
int output[O_LAST]
Definition city.c:70
Definition tile.h:49
struct unit_list * units
Definition tile.h:57
struct requirement_vector build_reqs
Definition unittype.h:501
const struct unit_type * obsoleted_by
Definition unittype.h:510
int city_slots
Definition unittype.h:533
int upkeep[O_LAST]
Definition unittype.h:519
Definition unit.h:138
int upkeep[O_LAST]
Definition unit.h:148
int homecity
Definition unit.h:146
enum universals_n kind
Definition fc_types.h:758
universals_u value
Definition fc_types.h:757
struct tile * ptile
Definition workertask.h:22
struct civ_map map
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:189
#define sz_strlcpy(dest, src)
Definition support.h:167
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
#define sz_strlcat(dest, src)
Definition support.h:168
#define fc__fallthrough
Definition support.h:109
#define T_UNKNOWN
Definition terrain.h:57
#define terrain_has_flag(terr, flag)
Definition terrain.h:269
int tile_roads_output_bonus(const struct tile *ptile, enum output_type_id o)
Definition tile.c:285
int tile_roads_output_incr(const struct tile *ptile, enum output_type_id o)
Definition tile.c:265
enum known_type tile_get_known(const struct tile *ptile, const struct player *pplayer)
Definition tile.c:386
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
#define tile_index(_pt_)
Definition tile.h:87
static bool tile_resource_is_valid(const struct tile *ptile)
Definition tile.h:102
#define tile_worked(_tile)
Definition tile.h:113
#define tile_resource(_tile)
Definition tile.h:101
@ TILE_UNKNOWN
Definition tile.h:35
@ TILE_KNOWN_SEEN
Definition tile.h:37
#define tile_terrain(_tile)
Definition tile.h:109
#define tile_owner(_tile)
Definition tile.h:95
bool can_cities_trade(const struct city *pc1, const struct city *pc2)
enum trade_route_type cities_trade_route_type(const struct city *pcity1, const struct city *pcity2)
Definition traderoutes.c:58
int trade_base_between_cities(const struct city *pc1, const struct city *pc2)
struct trade_route_settings * trade_route_settings_by_type(enum trade_route_type type)
int trade_from_route(const struct city *pc1, const struct trade_route *route, int base)
@ TRI_ACTIVE
Definition traderoutes.h:30
#define trade_routes_iterate_end
#define trade_partners_iterate_end
#define trade_routes_iterate(c, proute)
#define trade_partners_iterate(c, p)
trade_route_type
Definition traderoutes.h:37
const struct unit_type * utype
Definition fc_types.h:604
const struct impr_type * building
Definition fc_types.h:598
struct unit_order * create_unit_orders(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2825
bool unit_being_aggressive(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:1524
bool is_military_unit(const struct unit *punit)
Definition unit.c:319
bool is_field_unit(const struct unit *punit)
Definition unit.c:383
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1713
struct unit * unit_occupies_tile(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1386
#define unit_tile(_pu)
Definition unit.h:395
#define unit_owner(_pu)
Definition unit.h:394
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_end
Definition unitlist.h:33
struct unit_type * best_role_unit(const struct city *pcity, int role)
Definition unittype.c:2319
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
bool can_player_build_unit_direct(const struct player *p, const struct unit_type *punittype)
Definition unittype.c:2015
int utype_veteran_levels(const struct unit_type *punittype)
Definition unittype.c:2673
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
const char * utype_rule_name(const struct unit_type *punittype)
Definition unittype.c:1630
struct unit_type * utype_by_number(const Unit_type_id id)
Definition unittype.c:112
Unit_type_id utype_index(const struct unit_type *punittype)
Definition unittype.c:91
int utype_pop_value(const struct unit_type *punittype, const struct city *pcity)
Definition unittype.c:1580
bool can_player_build_unit_later(const struct player *p, const struct unit_type *punittype)
Definition unittype.c:2150
int utype_build_shield_cost(const struct city *pcity, const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1490
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
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:443
int utype_happy_cost(const struct unit_type *ut, const struct player *pplayer)
Definition unittype.c:175
static bool uclass_has_flag(const struct unit_class *punitclass, enum unit_class_flag_id flag)
Definition unittype.h:753
#define utype_class(_t_)
Definition unittype.h:736
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:604
#define unit_type_iterate(_p)
Definition unittype.h:841
#define unit_type_iterate_end
Definition unittype.h:848
#define U_NOT_OBSOLETED
Definition unittype.h:509
void worklist_init(struct worklist *pwl)
Definition worklist.c:38
bool worklist_peek_ith(const struct worklist *pwl, struct universal *prod, int idx)
Definition worklist.c:86
int worklist_length(const struct worklist *pwl)
Definition worklist.c:57