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 city *pcity,
889 const struct unit_type *punittype)
890{
891 const struct civ_map *nmap = &(wld.map);
892
893 if (!can_player_build_unit_direct(city_owner(pcity), punittype)) {
894 return FALSE;
895 }
896
897 /* Check unit build requirements. */
898 if (!are_reqs_active(&(const struct req_context) {
899 .player = city_owner(pcity),
900 .city = pcity,
901 .tile = city_tile(pcity),
902 .unittype = punittype,
903 },
904 NULL,
905 &punittype->build_reqs, RPT_CERTAIN)) {
906 return FALSE;
907 }
908
909 /* You can't build naval units inland. */
910 if (!uclass_has_flag(utype_class(punittype), UCF_BUILD_ANYWHERE)
911 && !is_native_near_tile(nmap, utype_class(punittype),
912 pcity->tile)) {
913 return FALSE;
914 }
915
916 if (punittype->city_slots > 0
917 && city_unit_slots_available(pcity) < punittype->city_slots) {
918 return FALSE;
919 }
920
921 return TRUE;
922}
923
924/**********************************************************************/
928bool can_city_build_unit_now(const struct city *pcity,
929 const struct unit_type *punittype)
930{
931 if (!can_city_build_unit_direct(pcity, punittype)) {
932 return FALSE;
933 }
934 while ((punittype = punittype->obsoleted_by) != U_NOT_OBSOLETED) {
935 if (can_player_build_unit_direct(city_owner(pcity), punittype)) {
936 return FALSE;
937 }
938 }
939 return TRUE;
940}
941
942/**********************************************************************/
946bool can_city_build_unit_later(const struct city *pcity,
947 const struct unit_type *punittype)
948{
949 const struct civ_map *nmap = &(wld.map);
950
951 /* Can the _player_ ever build this unit? */
952 if (!can_player_build_unit_later(city_owner(pcity), punittype)) {
953 return FALSE;
954 }
955
956 /* Some units can be built only in certain cities -- for instance,
957 ships may be built only in cities adjacent to ocean. */
958 if (!uclass_has_flag(utype_class(punittype), UCF_BUILD_ANYWHERE)
959 && !is_native_near_tile(nmap, utype_class(punittype),
960 pcity->tile)) {
961 return FALSE;
962 }
963
964 return TRUE;
965}
966
967/**********************************************************************/
971bool can_city_build_direct(const struct city *pcity,
972 const struct universal *target)
973{
974 switch (target->kind) {
975 case VUT_UTYPE:
976 return can_city_build_unit_direct(pcity, target->value.utype);
977 case VUT_IMPROVEMENT:
978 return can_city_build_improvement_direct(pcity, target->value.building);
979 default:
980 break;
981 };
982 return FALSE;
983}
984
985/**********************************************************************/
989bool can_city_build_now(const struct city *pcity,
990 const struct universal *target)
991{
992 switch (target->kind) {
993 case VUT_UTYPE:
994 return can_city_build_unit_now(pcity, target->value.utype);
995 case VUT_IMPROVEMENT:
996 return can_city_build_improvement_now(pcity, target->value.building);
997 default:
998 break;
999 };
1000 return FALSE;
1001}
1002
1003/**********************************************************************/
1006bool can_city_build_later(const struct city *pcity,
1007 const struct universal *target)
1008{
1009 switch (target->kind) {
1010 case VUT_UTYPE:
1011 return can_city_build_unit_later(pcity, target->value.utype);
1012 case VUT_IMPROVEMENT:
1013 return can_city_build_improvement_later(pcity, target->value.building);
1014 default:
1015 break;
1016 };
1017 return FALSE;
1018}
1019
1020/**********************************************************************/
1023int city_unit_slots_available(const struct city *pcity)
1024{
1025 int max = get_city_bonus(pcity, EFT_UNIT_SLOTS);
1026 int current;
1027
1028 current = 0;
1030 current += unit_type_get(punit)->city_slots;
1032
1033 return max - current;
1034}
1035
1036/**********************************************************************/
1039bool city_can_use_specialist(const struct city *pcity,
1041{
1042 return are_reqs_active(&(const struct req_context) {
1043 .player = city_owner(pcity),
1044 .city = pcity,
1045 },
1046 NULL,
1048}
1049
1050/**********************************************************************/
1053bool city_can_change_build(const struct city *pcity)
1054{
1055 return !pcity->did_buy || pcity->shield_stock <= 0;
1056}
1057
1058/**********************************************************************/
1062{
1063 if (NULL == city_tile(pcity)) {
1064 /* When a "dummy" city is created with no tile, then choosing a build
1065 * target could fail. This currently might happen during map editing.
1066 * FIXME: assumes the first unit is always "valid", so check for
1067 * obsolete units elsewhere. */
1068 pcity->production.kind = VUT_UTYPE;
1070 } else {
1071 struct unit_type *u = best_role_unit(pcity, L_FIRSTBUILD);
1072
1073 if (u) {
1074 pcity->production.kind = VUT_UTYPE;
1075 pcity->production.value.utype = u;
1076 } else {
1077 bool found = FALSE;
1078
1079 /* Just pick the first available item. */
1080 improvement_iterate(pimprove) {
1081 if (can_city_build_improvement_direct(pcity, pimprove)) {
1082 found = TRUE;
1083 pcity->production.kind = VUT_IMPROVEMENT;
1084 pcity->production.value.building = pimprove;
1085 break;
1086 }
1088
1089 if (!found) {
1090 unit_type_iterate(punittype) {
1091 if (can_city_build_unit_direct(pcity, punittype)) {
1092#ifndef FREECIV_NDEBUG
1093 /* Later than this, 'found' is only needed in an fc_assert() */
1094 found = TRUE;
1095#endif /* FREECIV_NDEBUG */
1096 pcity->production.kind = VUT_UTYPE;
1097 pcity->production.value.utype = punittype;
1098 }
1100 }
1101
1102 fc_assert_msg(found, "No production found for city %s!",
1103 city_name_get(pcity));
1104 }
1105 }
1106}
1107
1108/**********************************************************************/
1111const char *city_name_get(const struct city *pcity)
1112{
1113 return (pcity->name != NULL) ? pcity->name : "City missing a name";
1114}
1115
1116/**********************************************************************/
1119void city_name_set(struct city *pcity, const char *new_name)
1120{
1121 if (pcity->name != NULL) {
1122 free(pcity->name);
1123 }
1124
1125 if (strlen(new_name) < MAX_LEN_CITYNAME) {
1126 pcity->name = fc_strdup(new_name);
1127 } else {
1128 log_warn(_("City name \"%s\" too long"), new_name);
1130 sz_strlcpy(pcity->name, new_name);
1131 }
1132}
1133
1134/**********************************************************************/
1138void city_size_add(struct city *pcity, int add)
1139{
1140 citizens size = city_size_get(pcity);
1141
1142 fc_assert_ret(pcity != NULL);
1144
1145 /* Client sets size to zero to start stacking citizens in */
1146 fc_assert_ret(size >= -add);
1147
1148 city_size_set(pcity, size + add);
1149}
1150
1151/**********************************************************************/
1154void city_size_set(struct city *pcity, citizens size)
1155{
1156 fc_assert_ret(pcity != NULL);
1157
1158 /* Set city size. */
1159 pcity->size = size;
1160}
1161
1162/**********************************************************************/
1165int city_population(const struct city *pcity)
1166{
1167 /* Sum_{i=1}^{n} i == n*(n+1)/2 */
1168 return city_size_get(pcity) * (city_size_get(pcity) + 1) * 5;
1169}
1170
1171/**********************************************************************/
1175int city_total_impr_gold_upkeep(const struct city *pcity)
1176{
1177 int gold_needed = 0;
1178
1179 if (pcity == NULL) {
1180 return 0;
1181 }
1182
1183 city_built_iterate(pcity, pimprove) {
1184 gold_needed += city_improvement_upkeep(pcity, pimprove);
1186
1187 return gold_needed;
1188}
1189
1190/**********************************************************************/
1194int city_total_unit_gold_upkeep(const struct city *pcity)
1195{
1196 int gold_needed = 0;
1197
1198 if (pcity == NULL || pcity->units_supported == NULL) {
1199 return 0;
1200 }
1201
1203 gold_needed += punit->upkeep[O_GOLD];
1205
1206 return gold_needed;
1207}
1208
1209/**********************************************************************/
1212bool city_has_building(const struct city *pcity,
1213 const struct impr_type *pimprove)
1214{
1215 if (NULL == pimprove) {
1216 /* Callers should ensure that any external data is tested with
1217 * valid_improvement_by_number() */
1218 return FALSE;
1219 }
1220 return (pcity->built[improvement_index(pimprove)].turn > I_NEVER);
1221}
1222
1223/**********************************************************************/
1227int city_improvement_upkeep(const struct city *pcity,
1228 const struct impr_type *b)
1229{
1230 int upkeep;
1231
1232 if (NULL == b) {
1233 return 0;
1234 }
1235 if (is_wonder(b)) {
1236 return 0;
1237 }
1238
1239 upkeep = b->upkeep;
1240 if (upkeep <= get_building_bonus(pcity, b, EFT_UPKEEP_FREE)) {
1241 return 0;
1242 }
1243
1244 return upkeep;
1245}
1246
1247/**********************************************************************/
1255int city_tile_output(const struct city *pcity, const struct tile *ptile,
1256 bool is_celebrating, Output_type_id otype)
1257{
1258 int prod;
1259 const struct req_context city_ctxt = {
1260 .player = pcity ? city_owner(pcity) : NULL,
1261 .city = pcity,
1262 .tile = ptile,
1263 };
1264 struct terrain *pterrain = tile_terrain(ptile);
1265 const struct output_type *output = &output_types[otype];
1266
1267 fc_assert_ret_val(otype >= 0 && otype < O_LAST, 0);
1268
1269 if (T_UNKNOWN == pterrain) {
1270 /* Special case for the client. The server doesn't allow unknown tiles
1271 * to be worked but we don't necessarily know what player is involved. */
1272 return 0;
1273 }
1274
1275 prod = pterrain->output[otype];
1276 if (tile_resource_is_valid(ptile)) {
1277 prod += tile_resource(ptile)->data.resource->output[otype];
1278 }
1279
1280 switch (otype) {
1281 case O_SHIELD:
1282 if (pterrain->mining_shield_incr != 0) {
1283 prod += pterrain->mining_shield_incr
1284 * get_target_bonus_effects(NULL, &city_ctxt, NULL, EFT_MINING_PCT)
1285 / 100;
1286 }
1287 break;
1288 case O_FOOD:
1289 if (pterrain->irrigation_food_incr != 0) {
1290 prod += pterrain->irrigation_food_incr
1291 * get_target_bonus_effects(NULL, &city_ctxt, NULL,
1292 EFT_IRRIGATION_PCT) / 100;
1293 }
1294 break;
1295 case O_TRADE:
1296 case O_GOLD:
1297 case O_SCIENCE:
1298 case O_LUXURY:
1299 case O_LAST:
1300 break;
1301 }
1302
1303 prod += tile_roads_output_incr(ptile, otype);
1304 prod += (prod * tile_roads_output_bonus(ptile, otype) / 100);
1305
1306 prod += get_tile_output_bonus(pcity, ptile, output, EFT_OUTPUT_ADD_TILE);
1307 if (prod > 0) {
1308 int penalty_limit = get_tile_output_bonus(pcity, ptile, output,
1309 EFT_OUTPUT_PENALTY_TILE);
1310
1311 if (prod >= game.info.granularity) {
1312 prod += get_tile_output_bonus(pcity, ptile, output,
1313 EFT_OUTPUT_INC_TILE);
1314
1315 if (is_celebrating) {
1316 prod += get_tile_output_bonus(pcity, ptile, output,
1317 EFT_OUTPUT_INC_TILE_CELEBRATE);
1318 }
1319 }
1320
1321 prod += (prod
1322 * get_tile_output_bonus(pcity, ptile, output,
1323 EFT_OUTPUT_PER_TILE))
1324 / 100;
1325 if (!is_celebrating && penalty_limit > 0 && prod > penalty_limit) {
1326 if (prod <= game.info.granularity) {
1327 prod = 0;
1328 } else {
1329 prod -= game.info.granularity;
1330 }
1331 }
1332 }
1333
1334 prod -= (prod
1335 * get_tile_output_bonus(pcity, ptile, output,
1336 EFT_OUTPUT_TILE_PUNISH_PCT))
1337 / 100;
1338
1339 if (NULL != pcity && is_city_center(pcity, ptile)) {
1340 prod = MAX(prod, game.info.min_city_center_output[otype]);
1341 }
1342
1343 return prod;
1344}
1345
1346/**********************************************************************/
1356int city_tile_output_now(const struct city *pcity, const struct tile *ptile,
1357 Output_type_id otype)
1358{
1359 return city_tile_output(pcity, ptile, city_celebrating(pcity), otype);
1360}
1361
1362/**********************************************************************/
1373bool base_city_can_work_tile(const struct player *restriction,
1374 const struct city *pcity,
1375 const struct tile *ptile)
1376{
1377 struct player *powner = city_owner(pcity);
1378 int city_map_x, city_map_y;
1379
1380 if (NULL == ptile) {
1381 return FALSE;
1382 }
1383
1384 if (!city_base_to_city_map(&city_map_x, &city_map_y, pcity, ptile)) {
1385 return FALSE;
1386 }
1387
1388 if (NULL != restriction
1389 && TILE_UNKNOWN == tile_get_known(ptile, restriction)) {
1390 return FALSE;
1391 }
1392
1393 if (NULL != tile_owner(ptile) && tile_owner(ptile) != powner) {
1394 return FALSE;
1395 }
1396 /* TODO: civ3-like option for borders */
1397
1398 if (NULL != tile_worked(ptile) && tile_worked(ptile) != pcity) {
1399 return FALSE;
1400 }
1401
1402 if (powner == restriction
1403 && TILE_KNOWN_SEEN != tile_get_known(ptile, powner)) {
1404 return FALSE;
1405 }
1406
1407 if (!is_free_worked(pcity, ptile)
1408 && NULL != unit_occupies_tile(ptile, powner)) {
1409 return FALSE;
1410 }
1411
1412 if (get_city_tile_output_bonus(pcity, ptile, NULL, EFT_TILE_WORKABLE) <= 0) {
1413 return FALSE;
1414 }
1415
1416 return TRUE;
1417}
1418
1419/**********************************************************************/
1425bool city_can_work_tile(const struct city *pcity, const struct tile *ptile)
1426{
1427 return base_city_can_work_tile(city_owner(pcity), pcity, ptile);
1428}
1429
1430/**********************************************************************/
1435 const struct tile *ptile)
1436{
1437 /* citymindist minimum is 1, meaning adjacent is okay */
1438 int citymindist = game.info.citymindist;
1439
1440 square_iterate(nmap, ptile, citymindist - 1, ptile1) {
1441 if (tile_city(ptile1)) {
1442 return TRUE;
1443 }
1445
1446 return FALSE;
1447}
1448
1449/**********************************************************************/
1456bool city_can_be_built_here(const struct civ_map *nmap,
1457 const struct tile *ptile,
1458 const struct unit *punit,
1459 bool hut_test)
1460{
1461 if (!city_can_be_built_tile_only(nmap, ptile)) {
1462 return FALSE;
1463 }
1464
1465 if (punit == NULL) {
1466 /* The remaining checks tests if punit can found a city here */
1467 return TRUE;
1468 }
1469
1470 if (hut_test) {
1471 struct player *towner;
1472
1473 /* Huts can be found only from native tiles, owned by the unit owner.
1474 * Unlike actual city building, this behavior is not affected
1475 * by the ruleset. */
1476 if (!can_unit_exist_at_tile(nmap, punit, ptile)) {
1477 return FALSE;
1478 }
1479
1480 towner = tile_owner(ptile);
1481
1482 if (towner == NULL || towner == unit_owner(punit)) {
1483 return TRUE;
1484 }
1485
1486 return FALSE;
1487 }
1488
1489 action_by_result_iterate(paction, ACTRES_FOUND_CITY) {
1490 if (!utype_can_do_action(unit_type_get(punit), paction->id)) {
1491 /* This action can't be done by this unit type at all. */
1492 continue;
1493 }
1494
1495 /* Non native tile detection */
1496 if (!can_unit_exist_at_tile(nmap, punit, ptile)
1497 /* The ruleset may allow founding cities on non native terrain. */
1499 USP_LIVABLE_TILE, FALSE)) {
1500 /* Many rulesets allow land units to build land cities and sea units
1501 * to build ocean cities. Air units can build cities anywhere. */
1502 continue;
1503 }
1504
1505 /* Foreign tile detection. */
1506 if (tile_owner(ptile) && tile_owner(ptile) != unit_owner(punit)
1507 /* The ruleset may allow founding cities on foreign terrain. */
1509 paction->id,
1510 DRO_FOREIGN, TRUE)) {
1511 /* Cannot steal borders by settling. This has to be settled by
1512 * force of arms. */
1513 continue;
1514 }
1515
1516 return TRUE;
1518
1519 return FALSE;
1520}
1521
1522/**********************************************************************/
1529bool city_can_be_built_tile_only(const struct civ_map *nmap,
1530 const struct tile *ptile)
1531{
1532 if (terrain_has_flag(tile_terrain(ptile), TER_NO_CITIES)) {
1533 /* No cities on this terrain. */
1534 return FALSE;
1535 }
1536
1537 if (citymindist_prevents_city_on_tile(nmap, ptile)) {
1538 return FALSE;
1539 }
1540
1541 return TRUE;
1542}
1543
1544/**********************************************************************/
1548bool is_capital(const struct city *pcity)
1549{
1550 return pcity->capital != CAPITAL_NOT;
1551}
1552
1553/**********************************************************************/
1556bool is_gov_center(const struct city *pcity)
1557{
1558 return (get_city_bonus(pcity, EFT_GOV_CENTER) > 0);
1559}
1560
1561/**********************************************************************/
1565bool city_got_defense_effect(const struct city *pcity,
1566 const struct unit_type *attacker)
1567{
1568 if (!attacker) {
1569 /* Any defense building will do */
1570 return get_city_bonus(pcity, EFT_DEFEND_BONUS) > 0;
1571 }
1572
1573 return get_unittype_bonus(city_owner(pcity), pcity->tile, attacker,
1574 NULL, EFT_DEFEND_BONUS) > 0;
1575}
1576
1577/**********************************************************************/
1583bool city_happy(const struct city *pcity)
1584{
1585 return (city_size_get(pcity) >= game.info.celebratesize
1586 && pcity->feel[CITIZEN_ANGRY][FEELING_FINAL] == 0
1587 && pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL] == 0
1588 && pcity->feel[CITIZEN_HAPPY][FEELING_FINAL] >= (city_size_get(pcity) + 1) / 2);
1589}
1590
1591/**********************************************************************/
1595bool city_unhappy(const struct city *pcity)
1596{
1597 return (pcity->feel[CITIZEN_HAPPY][FEELING_FINAL]
1599 + 2 * pcity->feel[CITIZEN_ANGRY][FEELING_FINAL]);
1600}
1601
1602/**********************************************************************/
1606bool base_city_celebrating(const struct city *pcity)
1607{
1608 return (city_size_get(pcity) >= game.info.celebratesize && pcity->was_happy);
1609}
1610
1611/**********************************************************************/
1614bool city_celebrating(const struct city *pcity)
1615{
1616 return base_city_celebrating(pcity) && city_happy(pcity);
1617}
1618
1619/**********************************************************************/
1622bool city_rapture_grow(const struct city *pcity)
1623{
1624 /* .rapture is checked instead of city_celebrating() because this
1625 function is called after .was_happy was updated. */
1626 return (pcity->rapture > 0 && pcity->surplus[O_FOOD] > 0
1627 && (pcity->rapture % game.info.rapturedelay) == 0
1628 && get_city_bonus(pcity, EFT_RAPTURE_GROW) > 0);
1629}
1630
1631/**********************************************************************/
1634bool city_is_occupied(const struct city *pcity)
1635{
1636 if (is_server()) {
1637 /* The server sees the units inside the city. */
1638 return (unit_list_size(city_tile(pcity)->units) > 0);
1639 } else {
1640 /* The client gets the occupied property from the server. */
1641 return pcity->client.occupied;
1642 }
1643}
1644
1645/**********************************************************************/
1648struct city *city_list_find_number(struct city_list *This, int id)
1649{
1650 if (id != 0) {
1651 city_list_iterate(This, pcity) {
1652 if (pcity->id == id) {
1653 return pcity;
1654 }
1656 }
1657
1658 return NULL;
1659}
1660
1661/**********************************************************************/
1664struct city *city_list_find_name(struct city_list *This, const char *name)
1665{
1666 city_list_iterate(This, pcity) {
1667 if (fc_strcasecmp(name, pcity->name) == 0) {
1668 return pcity;
1669 }
1671
1672 return NULL;
1673}
1674
1675/**********************************************************************/
1680int city_name_compare(const void *p1, const void *p2)
1681{
1682 return fc_strcasecmp((*(const struct city **) p1)->name,
1683 (*(const struct city **) p2)->name);
1684}
1685
1686/**********************************************************************/
1691{
1692 int i;
1693
1694 for (i = 0; i < game.control.styles_count; i++) {
1695 if (0 == strcmp(city_style_name_translation(i), s)) {
1696 return i;
1697 }
1698 }
1699
1700 return -1;
1701}
1702
1703/**********************************************************************/
1707int city_style_by_rule_name(const char *s)
1708{
1709 const char *qs = Qn_(s);
1710 int i;
1711
1712 for (i = 0; i < game.control.styles_count; i++) {
1713 if (0 == fc_strcasecmp(city_style_rule_name(i), qs)) {
1714 return i;
1715 }
1716 }
1717
1718 return -1;
1719}
1720
1721/**********************************************************************/
1726{
1728}
1729
1730/**********************************************************************/
1734const char *city_style_rule_name(const int style)
1735{
1737}
1738
1739/* Cache of what city production caravan shields are allowed to help. */
1740static bv_imprs caravan_helped_impr;
1741static bv_unit_types caravan_helped_utype;
1742
1743/**********************************************************************/
1748{
1749 struct requirement prod_as_req;
1750
1751#define log_ca_s_init log_debug
1752
1753 /* Remove old data. */
1756
1757 /* Common for all production kinds. */
1758 prod_as_req.range = REQ_RANGE_LOCAL;
1759 prod_as_req.survives = FALSE;
1760 prod_as_req.present = TRUE;
1761
1762 /* Check improvements */
1763 prod_as_req.source.kind = VUT_IMPROVEMENT;
1764
1765 improvement_iterate(itype) {
1766 /* Check this improvement. */
1767 prod_as_req.source.value.building = itype;
1768
1770 ACTION_HELP_WONDER),
1771 enabler) {
1772 if (!does_req_contradicts_reqs(&prod_as_req,
1773 &(enabler->target_reqs))
1774 && !req_vec_wants_type(&(enabler->target_reqs), VUT_UTYPE)
1775 && !req_vec_wants_type(&(enabler->target_reqs), VUT_UCLASS)
1776 && !req_vec_wants_type(&(enabler->target_reqs), VUT_UTFLAG)
1777 && !req_vec_wants_type(&(enabler->target_reqs), VUT_UCFLAG)) {
1778 /* This improvement kind can receive caravan shields. */
1779
1781
1782 /* Move on to the next improvement */
1783 break;
1784 }
1786
1787 log_ca_s_init("Help Wonder: %s for %s",
1789 ? "possible" : "impossible"),
1790 improvement_rule_name(itype));
1792
1793 /* Check units. */
1794 prod_as_req.source.kind = VUT_UTYPE;
1795
1796 unit_type_iterate(putype) {
1797 /* Check this utype. */
1798 prod_as_req.source.value.utype = putype;
1799
1801 ACTION_HELP_WONDER),
1802 enabler) {
1803 if (!does_req_contradicts_reqs(&prod_as_req,
1804 &(enabler->target_reqs))
1805 && !req_vec_wants_type(&(enabler->target_reqs), VUT_IMPROVEMENT)
1806 && !req_vec_wants_type(&(enabler->target_reqs), VUT_IMPR_GENUS)) {
1807 /* This unit type kind can receive caravan shields. */
1808
1810
1811 /* Move on to the next unit type */
1812 break;
1813 }
1815
1816 log_ca_s_init("Help Wonder: %s for %s",
1818 ? "possible" : "impossible"),
1819 utype_rule_name(putype));
1821
1822#undef log_ca_s_init
1823}
1824
1825/**********************************************************************/
1830{
1831 switch (tgt->kind) {
1832 case VUT_IMPROVEMENT:
1835 case VUT_UTYPE:
1837 utype_index(tgt->value.utype));
1838 default:
1840 return FALSE;
1841 };
1842}
1843
1844/**********************************************************************/
1855int city_change_production_penalty(const struct city *pcity,
1856 const struct universal *target)
1857{
1858 int shield_stock_after_adjustment;
1859 enum production_class_type orig_class;
1860 enum production_class_type new_class;
1861 int unpenalized_shields = 0, penalized_shields = 0;
1862
1863 switch (pcity->changed_from.kind) {
1864 case VUT_IMPROVEMENT:
1865 if (is_wonder(pcity->changed_from.value.building)) {
1866 orig_class = PCT_WONDER;
1867 } else {
1868 orig_class = PCT_NORMAL_IMPROVEMENT;
1869 }
1870 break;
1871 case VUT_UTYPE:
1872 orig_class = PCT_UNIT;
1873 break;
1874 default:
1875 orig_class = PCT_LAST;
1876 break;
1877 };
1878
1879 switch (target->kind) {
1880 case VUT_IMPROVEMENT:
1881 if (is_wonder(target->value.building)) {
1882 new_class = PCT_WONDER;
1883 } else {
1884 new_class = PCT_NORMAL_IMPROVEMENT;
1885 }
1886 break;
1887 case VUT_UTYPE:
1888 new_class = PCT_UNIT;
1889 break;
1890 default:
1891 new_class = PCT_LAST;
1892 break;
1893 };
1894
1895 /* Changing production is penalized under certain circumstances. */
1896 if (orig_class == new_class
1897 || orig_class == PCT_LAST) {
1898 /* There's never a penalty for building something of the same class. */
1899 unpenalized_shields = pcity->before_change_shields;
1900 } else if (city_built_last_turn(pcity)) {
1901 /* Surplus shields from the previous production won't be penalized if
1902 * you change production on the very next turn. But you can only use
1903 * up to the city's surplus amount of shields in this way. */
1904 unpenalized_shields = MIN(pcity->last_turns_shield_surplus,
1905 pcity->before_change_shields);
1906 penalized_shields = pcity->before_change_shields - unpenalized_shields;
1907 } else {
1908 /* Penalize 50% of the production. */
1909 penalized_shields = pcity->before_change_shields;
1910 }
1911
1912 /* Do not put penalty on these. It shouldn't matter whether you disband unit
1913 before or after changing production...*/
1914 unpenalized_shields += pcity->disbanded_shields;
1915
1916 /* Caravan shields are penalized (just as if you disbanded the caravan)
1917 * if you're not building a wonder. */
1919 unpenalized_shields += pcity->caravan_shields;
1920 } else {
1921 penalized_shields += pcity->caravan_shields;
1922 }
1923
1924 shield_stock_after_adjustment =
1925 unpenalized_shields + penalized_shields / 2;
1926
1927 return shield_stock_after_adjustment;
1928}
1929
1930/**********************************************************************/
1934int city_turns_to_build(const struct city *pcity,
1935 const struct universal *target,
1936 bool include_shield_stock)
1937{
1938 int city_shield_surplus = pcity->surplus[O_SHIELD];
1939 int city_shield_stock = include_shield_stock ?
1940 city_change_production_penalty(pcity, target) : 0;
1941 int cost = universal_build_shield_cost(pcity, target);
1942
1943 if (target->kind == VUT_IMPROVEMENT
1944 && is_great_wonder(target->value.building)
1946 return FC_INFINITY;
1947 }
1948
1949 if (include_shield_stock && (city_shield_stock >= cost)) {
1950 return 1;
1951 } else if (city_shield_surplus > 0) {
1952 return (cost - city_shield_stock - 1) / city_shield_surplus + 1;
1953 } else {
1954 return FC_INFINITY;
1955 }
1956}
1957
1958/**********************************************************************/
1965int city_turns_to_grow(const struct city *pcity)
1966{
1967 if (pcity->surplus[O_FOOD] > 0) {
1968 return (city_granary_size(city_size_get(pcity)) - pcity->food_stock +
1969 pcity->surplus[O_FOOD] - 1) / pcity->surplus[O_FOOD];
1970 } else if (pcity->surplus[O_FOOD] < 0) {
1971 /* turns before famine loss */
1972 return -1 + (pcity->food_stock / pcity->surplus[O_FOOD]);
1973 } else {
1974 return FC_INFINITY;
1975 }
1976}
1977
1978/**********************************************************************/
1981bool city_can_grow_to(const struct city *pcity, int pop_size)
1982{
1983 return (get_city_bonus(pcity, EFT_SIZE_UNLIMIT) > 0
1984 || pop_size <= get_city_bonus(pcity, EFT_SIZE_ADJ));
1985}
1986
1987/**********************************************************************/
1990struct city *tile_enemy_city(const struct tile *ptile,
1991 const struct player *pplayer)
1992{
1993 struct city *pcity = tile_city(ptile);
1994
1995 if (pcity != NULL && pplayers_at_war(pplayer, city_owner(pcity))) {
1996 return pcity;
1997 }
1998
1999 return NULL;
2000}
2001
2002/**********************************************************************/
2005struct city *tile_allied_city(const struct tile *ptile,
2006 const struct player *pplayer)
2007{
2008 struct city *pcity = tile_city(ptile);
2009
2010 if (pcity != NULL && pplayers_allied(pplayer, city_owner(pcity))) {
2011 return pcity;
2012 }
2013
2014 return NULL;
2015}
2016
2017/**********************************************************************/
2020struct city *tile_non_attack_city(const struct tile *ptile,
2021 const struct player *pplayer)
2022{
2023 struct city *pcity = tile_city(ptile);
2024
2025 if (pcity != NULL && pplayers_non_attack(pplayer, city_owner(pcity))) {
2026 return pcity;
2027 }
2028
2029 return NULL;
2030}
2031
2032/**********************************************************************/
2035struct city *tile_non_allied_city(const struct tile *ptile,
2036 const struct player *pplayer)
2037{
2038 struct city *pcity = tile_city(ptile);
2039
2040 if (pcity != NULL && !pplayers_allied(pplayer, city_owner(pcity))) {
2041 return pcity;
2042 }
2043
2044 return NULL;
2045}
2046
2047/**********************************************************************/
2052 const struct unit *punit)
2053{
2055}
2056
2057/**********************************************************************/
2061bool is_friendly_city_near(const struct civ_map *nmap,
2062 const struct player *owner,
2063 const struct tile *ptile)
2064{
2065 square_iterate(nmap, ptile, 3, ptile1) {
2066 struct city *pcity = tile_city(ptile1);
2067
2068 if (pcity && pplayers_allied(owner, city_owner(pcity))) {
2069 return TRUE;
2070 }
2072
2073 return FALSE;
2074}
2075
2076/**********************************************************************/
2080bool city_exists_within_max_city_map(const struct tile *ptile,
2081 bool may_be_on_center)
2082{
2083 const struct civ_map *nmap = &(wld.map);
2084
2085 city_tile_iterate(nmap, CITY_MAP_MAX_RADIUS_SQ, ptile, ptile1) {
2086 if (may_be_on_center || !same_pos(ptile, ptile1)) {
2087 if (tile_city(ptile1)) {
2088 return TRUE;
2089 }
2090 }
2092
2093 return FALSE;
2094}
2095
2096/**********************************************************************/
2101int city_granary_size(int city_size)
2102{
2103 int food_inis = game.info.granary_num_inis;
2104 int food_inc = game.info.granary_food_inc;
2105 int base_value;
2106
2107 /* If the city has no citizens, there is no granary. */
2108 if (city_size == 0) {
2109 return 0;
2110 }
2111
2112 /* Granary sizes for the first food_inis citizens are given directly.
2113 * After that we increase the granary size by food_inc per citizen. */
2114 if (city_size > food_inis) {
2115 base_value = game.info.granary_food_ini[food_inis - 1];
2116 base_value += food_inc * (city_size - food_inis);
2117 } else {
2118 base_value = game.info.granary_food_ini[city_size - 1];
2119 }
2120
2121 return MAX(base_value * game.info.foodbox / 100, 1);
2122}
2123
2124/**********************************************************************/
2129static int player_base_citizen_happiness(const struct player *pplayer)
2130{
2131 int cities = city_list_size(pplayer->cities);
2132 int content = get_player_bonus(pplayer, EFT_CITY_UNHAPPY_SIZE);
2133 int basis = get_player_bonus(pplayer, EFT_EMPIRE_SIZE_BASE);
2134 int step = get_player_bonus(pplayer, EFT_EMPIRE_SIZE_STEP);
2135
2136 if (basis + step <= 0) {
2137 /* Value of zero means effect is inactive */
2138 return content;
2139 }
2140
2141 if (cities > basis) {
2142 content--;
2143 if (step != 0) {
2144 /* the first penalty is at (basis + 1) cities;
2145 the next is at (basis + step + 1), _not_ (basis + step) */
2146 content -= (cities - basis - 1) / step;
2147 }
2148 }
2149 return content;
2150}
2151
2152/**********************************************************************/
2156{
2157 int content = player_base_citizen_happiness(pplayer);
2158
2159 return CLIP(0, content, MAX_CITY_SIZE);
2160}
2161
2162/**********************************************************************/
2166{
2167 if (!game.info.angrycitizen) {
2168 return 0;
2169 } else {
2170 /* Create angry citizens only if we have a negative number of possible
2171 * content citizens. This can happen when empires grow really big. */
2172 int content = player_base_citizen_happiness(pplayer);
2173
2174 return CLIP(0, -content, MAX_CITY_SIZE);
2175 }
2176}
2177
2178/**********************************************************************/
2181int get_final_city_output_bonus(const struct city *pcity, Output_type_id otype)
2182{
2183 struct output_type *output = &output_types[otype];
2184 int bonus1 = 100 + get_city_tile_output_bonus(pcity, NULL, output,
2185 EFT_OUTPUT_BONUS);
2186 int bonus2 = 100 + get_city_tile_output_bonus(pcity, NULL, output,
2187 EFT_OUTPUT_BONUS_2);
2188
2189 return MAX(bonus1 * bonus2 / 100, 0);
2190}
2191
2192/**********************************************************************/
2196int get_city_tithes_bonus(const struct city *pcity)
2197{
2198 int tithes_bonus = 0;
2199
2200 if (get_city_bonus(pcity, EFT_HAPPINESS_TO_GOLD) <= 0) {
2201 return 0;
2202 }
2203
2204 tithes_bonus += get_city_bonus(pcity, EFT_MAKE_CONTENT);
2205 tithes_bonus += get_city_bonus(pcity, EFT_FORCE_CONTENT);
2206
2207 return tithes_bonus;
2208}
2209
2210/**********************************************************************/
2214void add_tax_income(const struct player *pplayer, int trade, int *output)
2215{
2216 const int SCIENCE = 0, TAX = 1, LUXURY = 2;
2217 unsigned rates[3];
2218 int result[3];
2219
2220 if (game.info.changable_tax) {
2221 rates[SCIENCE] = pplayer->economic.science;
2222 rates[LUXURY] = pplayer->economic.luxury;
2223 rates[TAX] = 100 - rates[SCIENCE] - rates[LUXURY];
2224 } else {
2225 rates[SCIENCE] = game.info.forced_science;
2226 rates[LUXURY] = game.info.forced_luxury;
2227 rates[TAX] = game.info.forced_gold;
2228 }
2229
2230 /* ANARCHY */
2232 rates[SCIENCE] = 0;
2233 rates[LUXURY] = 100;
2234 rates[TAX] = 0;
2235 }
2236
2237 distribute(trade, 3, rates, result);
2238
2239 output[O_SCIENCE] += result[SCIENCE];
2240 output[O_GOLD] += result[TAX];
2241 output[O_LUXURY] += result[LUXURY];
2242}
2243
2244/**********************************************************************/
2248bool city_built_last_turn(const struct city *pcity)
2249{
2250 return pcity->turn_last_built + 1 >= game.info.turn;
2251}
2252
2253/**********************************************************************/
2261static inline void get_worked_tile_output(const struct city *pcity,
2262 int *output, bool *workers_map)
2263{
2264 bool is_worked;
2265#ifdef CITY_DEBUGGING
2266 bool is_celebrating = base_city_celebrating(pcity);
2267#endif
2268 struct tile *pcenter = city_tile(pcity);
2269 const struct civ_map *nmap = &(wld.map);
2270
2271 memset(output, 0, O_LAST * sizeof(*output));
2272
2273 city_tile_iterate_index(nmap, city_map_radius_sq_get(pcity), pcenter, ptile,
2274 city_tile_index) {
2275 if (workers_map == NULL) {
2276 struct city *pwork = tile_worked(ptile);
2277
2278 is_worked = (NULL != pwork && pwork == pcity);
2279 } else {
2280 is_worked = workers_map[city_tile_index];
2281 }
2282
2283 if (is_worked) {
2285#ifdef CITY_DEBUGGING
2286 /* This assertion never fails, but it's so slow that we disable
2287 * it by default. */
2288 fc_assert(city_tile_cache_get_output(pcity, city_tile_index, o)
2289 == city_tile_output(pcity, ptile, is_celebrating, o));
2290#endif /* CITY_DEBUGGING */
2291 output[o] += city_tile_cache_get_output(pcity, city_tile_index, o);
2293 }
2295}
2296
2297/**********************************************************************/
2301void add_specialist_output(const struct city *pcity, int *output)
2302{
2304 int count = pcity->specialists[sp];
2305
2306 output_type_iterate(stat_index) {
2307 int amount = get_specialist_output(pcity, sp, stat_index);
2308
2309 output[stat_index] += count * amount;
2312}
2313
2314/**********************************************************************/
2322static inline void set_city_bonuses(struct city *pcity)
2323{
2325 pcity->bonus[o] = get_final_city_output_bonus(pcity, o);
2327}
2328
2329/**********************************************************************/
2339static inline void city_tile_cache_update(const struct civ_map *nmap,
2340 struct city *pcity)
2341{
2342 bool is_celebrating = base_city_celebrating(pcity);
2343 int radius_sq = city_map_radius_sq_get(pcity);
2344
2345 /* Initialize tile_cache if needed */
2346 if (pcity->tile_cache == NULL || pcity->tile_cache_radius_sq == -1
2347 || pcity->tile_cache_radius_sq != radius_sq) {
2348 pcity->tile_cache = fc_realloc(pcity->tile_cache,
2349 city_map_tiles(radius_sq)
2350 * sizeof(*(pcity->tile_cache)));
2351 pcity->tile_cache_radius_sq = radius_sq;
2352 }
2353
2354 /* Any unreal tiles are skipped - these values should have been memset
2355 * to 0 when the city was created. */
2356 city_tile_iterate_index(nmap, radius_sq, pcity->tile, ptile, city_tile_index) {
2358 (pcity->tile_cache[city_tile_index]).output[o]
2359 = city_tile_output(pcity, ptile, is_celebrating, o);
2362}
2363
2364/**********************************************************************/
2368static inline int city_tile_cache_get_output(const struct city *pcity,
2369 int city_tile_index,
2370 enum output_type_id o)
2371{
2373 == city_map_radius_sq_get(pcity), 0);
2374 fc_assert_ret_val(city_tile_index < city_map_tiles_from_city(pcity), 0);
2375
2376 return (pcity->tile_cache[city_tile_index]).output[o];
2377}
2378
2379/**********************************************************************/
2382static void set_surpluses(struct city *pcity)
2383{
2385 pcity->surplus[o] = pcity->prod[o] - pcity->usage[o];
2387}
2388
2389/**********************************************************************/
2392static void happy_copy(struct city *pcity, enum citizen_feeling i)
2393{
2394 int c = 0;
2395
2396 for (; c < CITIZEN_LAST; c++) {
2397 pcity->feel[c][i] = pcity->feel[c][i - 1];
2398 }
2399}
2400
2401/**********************************************************************/
2404static void citizen_base_mood(struct city *pcity)
2405{
2406 struct player *pplayer = city_owner(pcity);
2407 citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_BASE];
2408 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_BASE];
2409 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_BASE];
2410 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_BASE];
2411 citizens size = city_size_get(pcity);
2412 citizens spes = city_specialists(pcity);
2413
2414 /* This is the number of citizens that may start out content, depending
2415 * on empire size and game's city unhappysize. This may be bigger than
2416 * the size of the city, since this is a potential. */
2417 citizens base_content = player_content_citizens(pplayer);
2418 /* Similarly, this is the potential number of angry citizens. */
2419 citizens base_angry = player_angry_citizens(pplayer);
2420
2421 /* Create content citizens. Take specialists from their ranks. */
2422 *content = MAX(0, MIN(size, base_content) - spes);
2423
2424 /* Create angry citizens. Specialists never become angry. */
2425 fc_assert_action(base_content == 0 || base_angry == 0, *content = 0);
2426 *angry = MIN(base_angry, size - spes);
2427
2428 /* Create unhappy citizens. In the beginning, all who are not content,
2429 * specialists or angry are unhappy. This is changed by luxuries and
2430 * buildings later. */
2431 *unhappy = (size - spes - *content - *angry);
2432
2433 /* No one is born happy. */
2434 *happy = 0;
2435}
2436
2437/**********************************************************************/
2443static inline void citizen_luxury_happy(struct city *pcity, int *luxuries)
2444{
2445 citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_LUXURY];
2446 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_LUXURY];
2447 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_LUXURY];
2448 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_LUXURY];
2449
2450 while (*luxuries >= game.info.happy_cost && *angry > 0) {
2451 /* Upgrade angry to unhappy: costs HAPPY_COST each. */
2452 (*angry)--;
2453 (*unhappy)++;
2454 *luxuries -= game.info.happy_cost;
2455 }
2456 while (*luxuries >= game.info.happy_cost && *content > 0) {
2457 /* Upgrade content to happy: costs HAPPY_COST each. */
2458 (*content)--;
2459 (*happy)++;
2460 *luxuries -= game.info.happy_cost;
2461 }
2462 while (*luxuries >= 2 * game.info.happy_cost && *unhappy > 0) {
2463 /* Upgrade unhappy to happy. Note this is a 2-level upgrade with
2464 * double the cost. */
2465 (*unhappy)--;
2466 (*happy)++;
2467 *luxuries -= 2 * game.info.happy_cost;
2468 }
2469 if (*luxuries >= game.info.happy_cost && *unhappy > 0) {
2470 /* Upgrade unhappy to content: costs HAPPY_COST each. */
2471 (*unhappy)--;
2472 (*content)++;
2473 *luxuries -= game.info.happy_cost;
2474 }
2475}
2476
2477/**********************************************************************/
2480static inline void citizen_happy_luxury(struct city *pcity)
2481{
2482 int x = pcity->prod[O_LUXURY];
2483
2484 citizen_luxury_happy(pcity, &x);
2485}
2486
2487/**********************************************************************/
2490static inline void citizen_content_buildings(struct city *pcity)
2491{
2492 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_EFFECT];
2493 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_EFFECT];
2494 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_EFFECT];
2495 int faces = get_city_bonus(pcity, EFT_MAKE_CONTENT);
2496
2497 /* make people content (but not happy):
2498 get rid of angry first, then make unhappy content. */
2499 while (faces > 0 && *angry > 0) {
2500 (*angry)--;
2501 (*unhappy)++;
2502 faces--;
2503 }
2504 while (faces > 0 && *unhappy > 0) {
2505 (*unhappy)--;
2506 (*content)++;
2507 faces--;
2508 }
2509}
2510
2511/**********************************************************************/
2514static inline void citizen_happiness_nationality(struct city *pcity)
2515{
2517 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_NATIONALITY];
2518 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_NATIONALITY];
2519
2521 int pct = get_city_bonus(pcity, EFT_ENEMY_CITIZEN_UNHAPPY_PCT);
2522
2523 if (pct > 0) {
2524 int enemies = 0;
2525 int unhappy_inc;
2526 struct player *owner = city_owner(pcity);
2527
2528 citizens_foreign_iterate(pcity, pslot, nationality) {
2530 enemies += nationality;
2531 }
2533
2534 unhappy_inc = enemies * pct / 100;
2535
2536 /* First make content => unhappy, then happy => unhappy,
2537 * then happy => content. No-one becomes angry. */
2538 while (unhappy_inc > 0 && *content > 0) {
2539 (*content)--;
2540 (*unhappy)++;
2541 unhappy_inc--;
2542 }
2543 while (unhappy_inc > 1 && *happy > 0) {
2544 (*happy)--;
2545 (*unhappy)++;
2546 unhappy_inc -= 2;
2547 }
2548 while (unhappy_inc > 0 && *happy > 0) {
2549 (*happy)--;
2550 (*content)++;
2551 unhappy_inc--;
2552 }
2553 }
2554 }
2555}
2556
2557/**********************************************************************/
2563static inline void citizen_happy_units(struct city *pcity)
2564{
2565 citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_MARTIAL];
2566 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_MARTIAL];
2567 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_MARTIAL];
2568 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_MARTIAL];
2569 citizens amt = pcity->martial_law;
2570
2571 /* Pacify discontent citizens through martial law. First convert
2572 * angry => unhappy, then unhappy => content. */
2573 while (amt > 0 && *angry > 0) {
2574 (*angry)--;
2575 (*unhappy)++;
2576 amt--;
2577 }
2578 while (amt > 0 && *unhappy > 0) {
2579 (*unhappy)--;
2580 (*content)++;
2581 amt--;
2582 }
2583
2584 /* Now make citizens unhappier because of military units away from home.
2585 * First make content => unhappy, then happy => unhappy,
2586 * then happy => content. */
2587 amt = pcity->unit_happy_upkeep;
2588 while (amt > 0 && *content > 0) {
2589 (*content)--;
2590 (*unhappy)++;
2591 amt--;
2592 }
2593 while (amt > 1 && *happy > 0) {
2594 (*happy)--;
2595 (*unhappy)++;
2596 amt -= 2;
2597 }
2598 while (amt > 0 && *happy > 0) {
2599 (*happy)--;
2600 (*content)++;
2601 amt--;
2602 }
2603 /* Any remaining unhappiness is lost since angry citizens aren't created
2604 * here. */
2605 /* FIXME: Why not? - Per */
2606}
2607
2608/**********************************************************************/
2611static inline void citizen_happy_wonders(struct city *pcity)
2612{
2613 citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_FINAL];
2614 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_FINAL];
2615 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL];
2616 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_FINAL];
2617 int bonus = get_city_bonus(pcity, EFT_MAKE_HAPPY);
2618
2619 /* First create happy citizens from content, then from unhappy
2620 * citizens; we cannot help angry citizens here. */
2621 while (bonus > 0 && *content > 0) {
2622 (*content)--;
2623 (*happy)++;
2624 bonus--;
2625 }
2626 while (bonus > 1 && *unhappy > 0) {
2627 (*unhappy)--;
2628 (*happy)++;
2629 bonus -= 2;
2630 }
2631 /* The rest falls through and lets unhappy people become content. */
2632
2633 if (get_city_bonus(pcity, EFT_NO_UNHAPPY) > 0) {
2634 *content += *unhappy + *angry;
2635 *unhappy = 0;
2636 *angry = 0;
2637 return;
2638 }
2639
2640 bonus += get_city_bonus(pcity, EFT_FORCE_CONTENT);
2641
2642 /* get rid of angry first, then make unhappy content */
2643 while (bonus > 0 && *angry > 0) {
2644 (*angry)--;
2645 (*unhappy)++;
2646 bonus--;
2647 }
2648 while (bonus > 0 && *unhappy > 0) {
2649 (*unhappy)--;
2650 (*content)++;
2651 bonus--;
2652 }
2653}
2654
2655/**********************************************************************/
2659static inline void unhappy_city_check(struct city *pcity)
2660{
2661 if (city_unhappy(pcity)) {
2663 switch (output_types[o].unhappy_penalty) {
2665 pcity->unhappy_penalty[o] = 0;
2666 break;
2668 pcity->unhappy_penalty[o] = MAX(pcity->prod[o] - pcity->usage[o], 0);
2669 break;
2671 pcity->unhappy_penalty[o] = pcity->prod[o];
2672 break;
2673 }
2674
2675 pcity->prod[o] -= pcity->unhappy_penalty[o];
2677 } else {
2678 memset(pcity->unhappy_penalty, 0,
2679 O_LAST * sizeof(*pcity->unhappy_penalty));
2680 }
2681}
2682
2683/**********************************************************************/
2686int city_pollution_types(const struct city *pcity, int shield_total,
2687 int *pollu_prod, int *pollu_pop, int *pollu_mod)
2688{
2689 int prod, pop, mod;
2690
2691 /* Add one one pollution per shield, multipled by the bonus. */
2692 prod = 100 + get_city_bonus(pcity, EFT_POLLU_PROD_PCT);
2693 prod = shield_total * MAX(prod, 0) / 100;
2694
2695 /* Add one pollution per citizen for baseline combined bonus (100%). */
2696 pop = (100 + get_city_bonus(pcity, EFT_POLLU_POP_PCT))
2697 * (100 + get_city_bonus(pcity, EFT_POLLU_POP_PCT_2))
2698 / 100;
2699 pop = (city_size_get(pcity) * MAX(pop, 0)) / 100;
2700
2701 /* Then there is base pollution (usually a negative number). */
2702 mod = game.info.base_pollution;
2703
2704 if (pollu_prod) {
2705 *pollu_prod = prod;
2706 }
2707 if (pollu_pop) {
2708 *pollu_pop = pop;
2709 }
2710 if (pollu_mod) {
2711 *pollu_mod = mod;
2712 }
2713 return MAX(prod + pop + mod, 0);
2714}
2715
2716/**********************************************************************/
2720int city_pollution(const struct city *pcity, int shield_total)
2721{
2722 return city_pollution_types(pcity, shield_total, NULL, NULL, NULL);
2723}
2724
2725/**********************************************************************/
2732static int get_trade_illness(const struct city *pcity)
2733{
2734 float illness_trade = 0.0;
2735
2737 if (trade_city->turn_plague != -1
2738 && game.info.turn - trade_city->turn_plague < 5) {
2739 illness_trade += (float)game.info.illness_trade_infection
2740 * sqrt(1.0 * city_size_get(pcity)
2741 * city_size_get(trade_city)) / 100.0;
2742 }
2744
2745 return (int)illness_trade;
2746}
2747
2748/**********************************************************************/
2752static int get_city_health(const struct city *pcity)
2753{
2754 return get_city_bonus(pcity, EFT_HEALTH_PCT);
2755}
2756
2757/**********************************************************************/
2769int city_illness_calc(const struct city *pcity, int *ill_base,
2770 int *ill_size, int *ill_trade, int *ill_pollution)
2771{
2772 int illness_size = 0, illness_trade = 0, illness_pollution = 0;
2773 int illness_base, illness_percent;
2774
2775 if (game.info.illness_on
2777 /* offset the city size by game.info.illness_min_size */
2778 int use_size = city_size_get(pcity) - game.info.illness_min_size;
2779
2780 illness_size = (int)((1.0 - exp(- (float)use_size / 10.0))
2781 * 10.0 * game.info.illness_base_factor);
2782 if (is_server()) {
2783 /* on the server we recalculate the illness due to trade as we have
2784 * all informations */
2785 illness_trade = get_trade_illness(pcity);
2786 } else {
2787 /* on the client we have to rely on the value saved within the city
2788 * struct */
2789 illness_trade = pcity->illness_trade;
2790 }
2791
2792 illness_pollution = pcity->pollution
2794 }
2795
2796 illness_base = illness_size + illness_trade + illness_pollution;
2797 illness_percent = 100 - get_city_health(pcity);
2798
2799 /* returning other data */
2800 if (ill_size) {
2801 *ill_size = illness_size;
2802 }
2803
2804 if (ill_trade) {
2805 *ill_trade = illness_trade;
2806 }
2807
2808 if (ill_pollution) {
2809 *ill_pollution = illness_pollution;
2810 }
2811
2812 if (ill_base) {
2813 *ill_base = illness_base;
2814 }
2815
2816 return CLIP(0, illness_base * illness_percent / 100 , 999);
2817}
2818
2819/**********************************************************************/
2822bool city_had_recent_plague(const struct city *pcity)
2823{
2824 /* Correctly handles special case turn_plague == -1 (never) */
2825 return (pcity->turn_plague == game.info.turn);
2826}
2827
2828/**********************************************************************/
2831int city_build_slots(const struct city *pcity)
2832{
2833 return get_city_bonus(pcity, EFT_CITY_BUILD_SLOTS);
2834}
2835
2836/**********************************************************************/
2841int city_airlift_max(const struct city *pcity)
2842{
2843 return get_city_bonus(pcity, EFT_AIRLIFT);
2844}
2845
2846/**********************************************************************/
2852inline void set_city_production(struct city *pcity)
2853{
2854 /* Calculate city production!
2855 *
2856 * This is a rather complicated process if we allow rules to become
2857 * more generalized. We can assume that there are no recursive dependency
2858 * loops, but there are some dependencies that do not follow strict
2859 * ordering. For instance corruption must be calculated before
2860 * trade taxes can be counted up, which must occur before the science bonus
2861 * is added on. But the calculation of corruption must include the
2862 * trade bonus. To do this without excessive special casing means that in
2863 * this case the bonuses are multiplied on twice (but only saved the second
2864 * time).
2865 */
2866
2868 pcity->prod[o] = pcity->citizen_base[o];
2870
2871 /* Add on special extra incomes: trade routes and tithes. */
2872 trade_routes_iterate(pcity, proute) {
2873 struct city *tcity = game_city_by_number(proute->partner);
2874 bool can_trade;
2875
2876 /* Partner city may have not yet been sent to the client, or
2877 * there's just a placeholder city with a placeholder owner
2878 * created for some tile->worked. */
2879 if (!is_server()
2880 && (tcity == NULL
2881 || city_owner(tcity)->slot == NULL)) {
2882 continue;
2883 }
2884
2885 fc_assert_action(tcity != NULL, continue);
2886
2887 can_trade = can_cities_trade(pcity, tcity);
2888
2889 if (!can_trade) {
2890 enum trade_route_type type = cities_trade_route_type(pcity, tcity);
2892
2893 if (settings->cancelling == TRI_ACTIVE) {
2894 can_trade = TRUE;
2895 }
2896 }
2897
2898 if (can_trade) {
2899 int value;
2900
2901 value =
2902 trade_base_between_cities(pcity, game_city_by_number(proute->partner));
2903 proute->value = trade_from_route(pcity, proute, value);
2904 pcity->prod[O_TRADE] += proute->value
2905 * (100 + get_city_bonus(pcity, EFT_TRADE_ROUTE_PCT)) / 100;
2906 } else {
2907 proute->value = 0;
2908 }
2910 pcity->prod[O_GOLD] += get_city_tithes_bonus(pcity);
2911
2912 /* Account for waste. Note that waste is calculated before tax income is
2913 * calculated, so if you had "science waste" it would not include taxed
2914 * science. However waste is calculated after the bonuses are multiplied
2915 * on, so shield waste will include shield bonuses. */
2917 pcity->waste[o] = city_waste(pcity, o,
2918 pcity->prod[o] * pcity->bonus[o] / 100,
2919 NULL);
2921
2922 /* Convert trade into science/luxury/gold, and add this on to whatever
2923 * science/luxury/gold is already there. */
2925 pcity->prod[O_TRADE] * pcity->bonus[O_TRADE] / 100
2926 - pcity->waste[O_TRADE] - pcity->usage[O_TRADE],
2927 pcity->prod);
2928
2929 /* Add on effect bonuses and waste. Note that the waste calculation
2930 * (above) already includes the bonus multiplier. */
2932 pcity->prod[o] = pcity->prod[o] * pcity->bonus[o] / 100;
2933 pcity->prod[o] -= pcity->waste[o];
2935}
2936
2937/**********************************************************************/
2940int city_unit_unhappiness(struct unit *punit, int *free_unhappy)
2941{
2942 struct city *pcity;
2943 const struct unit_type *ut;
2944 struct player *plr;
2945 int happy_cost;
2946
2947 if (!punit || !free_unhappy) {
2948 return 0;
2949 }
2950
2952 if (pcity == NULL) {
2953 return 0;
2954 }
2955
2956 ut = unit_type_get(punit);
2957 plr = unit_owner(punit);
2958 happy_cost = utype_happy_cost(ut, plr);
2959
2960 if (happy_cost <= 0) {
2961 return 0;
2962 }
2963
2964 fc_assert_ret_val(0 <= *free_unhappy, 0);
2965
2967 return 0;
2968 }
2969
2970 happy_cost -= get_city_bonus(pcity, EFT_MAKE_CONTENT_MIL_PER);
2971 if (happy_cost <= 0) {
2972 return 0;
2973 }
2974
2975 if (*free_unhappy >= happy_cost) {
2976 *free_unhappy -= happy_cost;
2977 return 0;
2978 } else {
2979 happy_cost -= *free_unhappy;
2980 *free_unhappy = 0;
2981 }
2982
2983 return happy_cost;
2984}
2985
2986/**********************************************************************/
2990static inline void city_support(struct city *pcity)
2991{
2992 int free_unhappy, martial_law_each;
2993
2994 /* Clear all usage values. */
2995 memset(pcity->usage, 0, O_LAST * sizeof(*pcity->usage));
2996 pcity->martial_law = 0;
2997 pcity->unit_happy_upkeep = 0;
2998
2999 /* Building and unit gold upkeep depends on the setting
3000 * 'game.info.gold_upkeep_style':
3001 * GOLD_UPKEEP_CITY: The upkeep for buildings and units is paid by the
3002 * city.
3003 * GOLD_UPKEEP_MIXED: The upkeep for buildings is paid by the city.
3004 * The upkeep for units is paid by the nation.
3005 * GOLD_UPKEEP_NATION: The upkeep for buildings and units is paid by the
3006 * nation. */
3007 fc_assert_msg(gold_upkeep_style_is_valid(game.info.gold_upkeep_style),
3008 "Invalid gold_upkeep_style %d", game.info.gold_upkeep_style);
3009 switch (game.info.gold_upkeep_style) {
3010 case GOLD_UPKEEP_CITY:
3011 pcity->usage[O_GOLD] += city_total_unit_gold_upkeep(pcity);
3012 fc__fallthrough; /* No break */
3013 case GOLD_UPKEEP_MIXED:
3014 pcity->usage[O_GOLD] += city_total_impr_gold_upkeep(pcity);
3015 break;
3016 case GOLD_UPKEEP_NATION:
3017 /* nothing */
3018 break;
3019 }
3020 /* Food consumption by citizens. */
3021 pcity->usage[O_FOOD] += game.info.food_cost * city_size_get(pcity);
3022
3023 /* military units in this city (need _not_ be home city) can make
3024 * unhappy citizens content */
3025 martial_law_each = get_city_bonus(pcity, EFT_MARTIAL_LAW_EACH);
3026 if (martial_law_each > 0) {
3027 int count = 0;
3028 int martial_law_max = get_city_bonus(pcity, EFT_MARTIAL_LAW_MAX);
3029
3030 unit_list_iterate(pcity->tile->units, punit) {
3031 if ((count < martial_law_max || martial_law_max == 0)
3033 && unit_owner(punit) == city_owner(pcity)) {
3034 count++;
3035 }
3037
3038 pcity->martial_law = CLIP(0, count * martial_law_each, MAX_CITY_SIZE);
3039 }
3040
3041 free_unhappy = get_city_bonus(pcity, EFT_MAKE_CONTENT_MIL);
3043 pcity->unit_happy_upkeep += city_unit_unhappiness(punit, &free_unhappy);
3045 if (O_GOLD != o) {
3046 /* O_GOLD is handled with "game.info.gold_upkeep_style", see over. */
3047 pcity->usage[o] += punit->upkeep[o];
3048 }
3051}
3052
3053/**********************************************************************/
3065void city_refresh_from_main_map(struct city *pcity, bool *workers_map)
3066{
3067 const struct civ_map *nmap = &(wld.map);
3068
3069 if (workers_map == NULL) {
3070 /* do a full refresh */
3071
3072 /* Calculate the bonus[] array values. */
3073 set_city_bonuses(pcity);
3074 /* Calculate the tile_cache[] values. */
3075 city_tile_cache_update(nmap, pcity);
3076 /* manage settlers, and units */
3077 city_support(pcity);
3078 }
3079
3080 /* Calculate output from citizens (uses city_tile_cache_get_output()). */
3081 get_worked_tile_output(pcity, pcity->citizen_base, workers_map);
3082 add_specialist_output(pcity, pcity->citizen_base);
3083
3084 set_city_production(pcity);
3085 citizen_base_mood(pcity);
3086 /* Note that pollution is calculated before unhappy_city_check() makes
3087 * deductions for disorder; so a city in disorder still causes pollution */
3088 pcity->pollution = city_pollution(pcity, pcity->prod[O_SHIELD]);
3089
3090 happy_copy(pcity, FEELING_LUXURY);
3091 citizen_happy_luxury(pcity); /* with our new found luxuries */
3092
3093 happy_copy(pcity, FEELING_EFFECT);
3095
3098
3099 /* Martial law & unrest from units */
3101 citizen_happy_units(pcity);
3102
3103 /* Building (including wonder) happiness effects */
3104 happy_copy(pcity, FEELING_FINAL);
3105 citizen_happy_wonders(pcity);
3106
3107 unhappy_city_check(pcity);
3108 set_surpluses(pcity);
3109}
3110
3111/**********************************************************************/
3118int city_waste(const struct city *pcity, Output_type_id otype, int total,
3119 int *breakdown)
3120{
3121 int penalty_waste = 0;
3122 int penalty_size = 0; /* separate notradesize/fulltradesize from normal
3123 * corruption */
3124 int total_eft = total; /* normal corruption calculated on total reduced by
3125 * possible size penalty */
3126 int waste_level = get_city_output_bonus(pcity, get_output_type(otype),
3127 EFT_OUTPUT_WASTE);
3128 bool waste_all = FALSE;
3129
3130 if (otype == O_TRADE) {
3131 /* FIXME: special case for trade: it is affected by notradesize and
3132 * fulltradesize server settings.
3133 *
3134 * If notradesize and fulltradesize are equal then the city gets no
3135 * trade at that size. */
3136 int notradesize = MIN(game.info.notradesize, game.info.fulltradesize);
3137 int fulltradesize = MAX(game.info.notradesize, game.info.fulltradesize);
3138
3139 if (city_size_get(pcity) <= notradesize) {
3140 penalty_size = total_eft; /* Then no trade income. */
3141 } else if (city_size_get(pcity) >= fulltradesize) {
3142 penalty_size = 0;
3143 } else {
3144 penalty_size = total_eft * (fulltradesize - city_size_get(pcity))
3145 / (fulltradesize - notradesize);
3146 }
3147 }
3148
3149 /* Apply corruption only to anything left after tradesize */
3150 total_eft -= penalty_size;
3151
3152 /* Distance-based waste.
3153 * Don't bother calculating if there's nothing left to lose. */
3154 if (total_eft > 0) {
3155 int waste_by_dist = get_city_output_bonus(pcity, get_output_type(otype),
3156 EFT_OUTPUT_WASTE_BY_DISTANCE);
3157 int waste_by_rel_dist = get_city_output_bonus(pcity, get_output_type(otype),
3158 EFT_OUTPUT_WASTE_BY_REL_DISTANCE);
3159 if (waste_by_dist > 0 || waste_by_rel_dist > 0) {
3160 const struct city *gov_center = NULL;
3161 int min_dist = FC_INFINITY;
3162
3163 /* Check the special case that city itself is gov center
3164 * before expensive iteration through all cities. */
3165 if (is_gov_center(pcity)) {
3166 gov_center = pcity;
3167 min_dist = 0;
3168 } else {
3169 city_list_iterate(city_owner(pcity)->cities, gc) {
3170 /* Do not recheck current city */
3171 if (gc != pcity && is_gov_center(gc)) {
3172 int dist = real_map_distance(gc->tile, pcity->tile);
3173
3174 if (dist < min_dist) {
3175 gov_center = gc;
3176 min_dist = dist;
3177 }
3178 }
3180 }
3181
3182 if (gov_center == NULL) {
3183 waste_all = TRUE; /* no gov center - no income */
3184 } else {
3185 waste_level += waste_by_dist * min_dist / 100;
3186 if (waste_by_rel_dist > 0) {
3187 /* Multiply by 50 as an "standard size" for which EFT_OUTPUT_WASTE_BY_DISTANCE
3188 * and EFT_OUTPUT_WASTE_BY_REL_DISTANCE would give same result. */
3189 waste_level += waste_by_rel_dist * 50 * min_dist / 100
3190 / MAX(wld.map.xsize, wld.map.ysize);
3191 }
3192 }
3193 }
3194 }
3195
3196 if (waste_all) {
3197 penalty_waste = total_eft;
3198 } else {
3199 int waste_pct = get_city_output_bonus(pcity, get_output_type(otype),
3200 EFT_OUTPUT_WASTE_PCT);
3201
3202 /* corruption/waste calculated only for the actually produced amount */
3203 if (waste_level > 0) {
3204 penalty_waste = total_eft * waste_level / 100;
3205 }
3206
3207 /* bonus calculated only for the actually produced amount */
3208 penalty_waste -= penalty_waste * waste_pct / 100;
3209
3210 /* Clip */
3211 penalty_waste = MIN(MAX(penalty_waste, 0), total_eft);
3212 }
3213
3214 if (breakdown) {
3215 breakdown[OLOSS_WASTE] = penalty_waste;
3216 breakdown[OLOSS_SIZE] = penalty_size;
3217 }
3218
3219 /* add up total penalty */
3220 return penalty_waste + penalty_size;
3221}
3222
3223/**********************************************************************/
3226citizens city_specialists(const struct city *pcity)
3227{
3228 citizens count = 0;
3229
3231 fc_assert_ret_val(MAX_CITY_SIZE - count > pcity->specialists[sp], 0);
3232 count += pcity->specialists[sp];
3234
3235 return count;
3236}
3237
3238/**********************************************************************/
3244 const struct city *pcity)
3245{
3246 int best = DEFAULT_SPECIALIST;
3247 int val = get_specialist_output(pcity, best, otype);
3248
3250 if (!pcity || city_can_use_specialist(pcity, i)) {
3251 int val2 = get_specialist_output(pcity, i, otype);
3252
3253 if (val2 > val) {
3254 best = i;
3255 val = val2;
3256 }
3257 }
3259
3260 return best;
3261}
3262
3263/**********************************************************************/
3266void city_add_improvement(struct city *pcity,
3267 const struct impr_type *pimprove)
3268{
3269 pcity->built[improvement_index(pimprove)].turn = game.info.turn; /*I_ACTIVE*/
3270
3271 if (is_server() && is_wonder(pimprove)) {
3272 /* Client just read the info from the packets. */
3273 wonder_built(pcity, pimprove);
3274 }
3275}
3276
3277/**********************************************************************/
3281 const struct impr_type *pimprove)
3282{
3283 log_debug("Improvement %s removed from city %s",
3284 improvement_rule_name(pimprove), pcity->name);
3285
3286 pcity->built[improvement_index(pimprove)].turn = I_DESTROYED;
3287
3288 if (is_server() && is_wonder(pimprove)) {
3289 /* Client just read the info from the packets. */
3290 wonder_destroyed(pcity, pimprove);
3291 }
3292}
3293
3294/**********************************************************************/
3297bool is_city_option_set(const struct city *pcity, enum city_options option)
3298{
3299 return BV_ISSET(pcity->city_options, option);
3300}
3301
3302/**********************************************************************/
3306{
3307 int i;
3308
3309 city_styles = fc_calloc(num, sizeof(*city_styles));
3311
3312 for (i = 0; i < game.control.styles_count; i++) {
3313 requirement_vector_init(&city_styles[i].reqs);
3314 }
3315}
3316
3317/**********************************************************************/
3321{
3322 int i;
3323
3324 for (i = 0; i < game.control.styles_count; i++) {
3325 requirement_vector_free(&city_styles[i].reqs);
3326 }
3327
3328 free(city_styles);
3329 city_styles = NULL;
3331}
3332
3333/**********************************************************************/
3339struct city *create_city_virtual(struct player *pplayer,
3340 struct tile *ptile, const char *name)
3341{
3342 int i;
3343
3344 /* Make sure that contents of city structure are correctly initialized,
3345 * if you ever allocate it by some other mean than fc_calloc() */
3346 struct city *pcity = fc_calloc(1, sizeof(*pcity));
3347
3348 fc_assert_ret_val(NULL != name, NULL); /* No unnamed cities! */
3349
3350 /* Do this early, so any logging later will have the city name */
3351 city_name_set(pcity, name);
3352
3353 pcity->tile = ptile;
3354 fc_assert_ret_val(NULL != pplayer, NULL); /* No unowned cities! */
3355 pcity->owner = pplayer;
3356
3357 if (is_server()) {
3358 pcity->original = pplayer;
3359 }
3360
3361 /* City structure was allocated with fc_calloc(), so contents are initially
3362 * zero. There is no need to initialize it a second time. */
3363
3364 /* Now set some useful default values. */
3365 pcity->capital = CAPITAL_NOT;
3366 city_size_set(pcity, 1);
3367 pcity->specialists[DEFAULT_SPECIALIST] = 1;
3368
3370 pcity->bonus[o] = 100;
3372
3373 pcity->turn_plague = -1; /* -1 = never */
3374 pcity->did_buy = FALSE;
3376 pcity->turn_founded = game.info.turn;
3377 pcity->turn_last_built = game.info.turn;
3378
3379 pcity->tile_cache_radius_sq = -1; /* -1 = tile_cache must be initialised */
3380
3381 /* pcity->ai.act_cache: worker activities on the city map */
3382
3383 /* Initialise improvements list */
3384 for (i = 0; i < ARRAY_SIZE(pcity->built); i++) {
3385 pcity->built[i].turn = I_NEVER;
3386 }
3387
3388 /* Set up the worklist */
3389 worklist_init(&pcity->worklist);
3390
3391 pcity->units_supported = unit_list_new();
3392 pcity->routes = trade_route_list_new();
3393 pcity->task_reqs = worker_task_list_new();
3394
3395 if (is_server()) {
3396 pcity->server.mgr_score_calc_turn = -1; /* -1 = never */
3397
3398 CALL_FUNC_EACH_AI(city_alloc, pcity);
3399 } else {
3401 unit_list_new_full(unit_virtual_destroy);
3402 pcity->client.info_units_present =
3403 unit_list_new_full(unit_virtual_destroy);
3404 /* collecting_info_units_supported set by fc_calloc().
3405 * collecting_info_units_present set by fc_calloc(). */
3406 }
3407
3408 return pcity;
3409}
3410
3411/**********************************************************************/
3415void destroy_city_virtual(struct city *pcity)
3416{
3417 CALL_FUNC_EACH_AI(city_free, pcity);
3418
3419 citizens_free(pcity);
3420
3421 /* Free worker tasks */
3422 while (worker_task_list_size(pcity->task_reqs) > 0) {
3423 struct worker_task *ptask = worker_task_list_get(pcity->task_reqs, 0);
3424
3425 worker_task_list_remove(pcity->task_reqs, ptask);
3426
3427 free(ptask);
3428 }
3429 worker_task_list_destroy(pcity->task_reqs);
3430
3431 /* Free rally points */
3433
3434 unit_list_destroy(pcity->units_supported);
3435 trade_route_list_destroy(pcity->routes);
3436 if (pcity->tile_cache != NULL) {
3437 free(pcity->tile_cache);
3438 }
3439
3440 if (pcity->cm_parameter) {
3441 free(pcity->cm_parameter);
3442 }
3443
3444 if (!is_server()) {
3445 unit_list_destroy(pcity->client.info_units_supported);
3446 unit_list_destroy(pcity->client.info_units_present);
3447 /* Handle a rare case where the game is freed in the middle of a
3448 * spy/diplomat investigate cycle. */
3449 if (pcity->client.collecting_info_units_supported != NULL) {
3450 unit_list_destroy(pcity->client.collecting_info_units_supported);
3451 }
3452 if (pcity->client.collecting_info_units_present != NULL) {
3453 unit_list_destroy(pcity->client.collecting_info_units_present);
3454 }
3455 }
3456
3457 free(pcity->name);
3458
3459 memset(pcity, 0, sizeof(*pcity)); /* ensure no pointers remain */
3460 free(pcity);
3461}
3462
3463/**********************************************************************/
3467bool city_exist(int id)
3468{
3469 /* Check if city exist in game */
3470 if (game_city_by_number(id)) {
3471 return TRUE;
3472 }
3473
3474 return FALSE;
3475}
3476
3477/**********************************************************************/
3484bool city_is_virtual(const struct city *pcity)
3485{
3486 if (!pcity) {
3487 return FALSE;
3488 }
3489
3490 return pcity != game_city_by_number(pcity->id);
3491}
3492
3493/**********************************************************************/
3496bool is_free_worked(const struct city *pcity, const struct tile *ptile)
3497{
3498 return is_city_center(pcity, ptile);
3499}
3500
3501/**********************************************************************/
3504void *city_ai_data(const struct city *pcity, const struct ai_type *ai)
3505{
3506 return pcity->server.ais[ai_type_number(ai)];
3507}
3508
3509/**********************************************************************/
3512void city_set_ai_data(struct city *pcity, const struct ai_type *ai,
3513 void *data)
3514{
3515 pcity->server.ais[ai_type_number(ai)] = data;
3516}
3517
3518/**********************************************************************/
3521void city_rally_point_clear(struct city *pcity)
3522{
3523 /* Free rally points */
3524 if (pcity->rally_point.length > 0) {
3525 pcity->rally_point.length = 0;
3526 pcity->rally_point.persistent = FALSE;
3527 pcity->rally_point.vigilant = FALSE;
3528 free(pcity->rally_point.orders);
3529 pcity->rally_point.orders = NULL;
3530 }
3531}
3532
3533/**********************************************************************/
3537 struct city *pcity)
3538{
3539 struct unit_order *checked_orders;
3540 const struct civ_map *nmap = &(wld.map);
3541
3542 if (pcity == NULL) {
3543 /* Probably lost. */
3544 log_verbose("handle_city_rally_point() bad city number %d.",
3545 packet->city_id32);
3546 return;
3547 }
3548
3549 if (0 > packet->length || MAX_LEN_ROUTE < packet->length) {
3550 /* Shouldn't happen */
3551 log_error("city_rally_point_receive() invalid packet length %d (max %d)",
3552 packet->length, MAX_LEN_ROUTE);
3553 return;
3554 }
3555
3556 pcity->rally_point.length = packet->length;
3557
3558 if (packet->length == 0) {
3559 pcity->rally_point.vigilant = FALSE;
3560 pcity->rally_point.persistent = FALSE;
3561 if (pcity->rally_point.orders) {
3562 free(pcity->rally_point.orders);
3563 pcity->rally_point.orders = NULL;
3564 }
3565 } else {
3566 checked_orders = create_unit_orders(nmap, packet->length,
3567 packet->orders);
3568 if (!checked_orders) {
3569 pcity->rally_point.length = 0;
3570 log_error("invalid rally point orders for city number %d.",
3571 packet->city_id32);
3572 return;
3573 }
3574
3575 pcity->rally_point.persistent = packet->persistent;
3576 pcity->rally_point.vigilant = packet->vigilant;
3577 pcity->rally_point.orders = checked_orders;
3578 }
3579}
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:1606
bool city_is_virtual(const struct city *pcity)
Definition city.c:3484
int city_turns_to_build(const struct city *pcity, const struct universal *target, bool include_shield_stock)
Definition city.c:1934
static void citizen_luxury_happy(struct city *pcity, int *luxuries)
Definition city.c:2443
const char * city_improvement_name_translation(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:658
bool city_exists_within_max_city_map(const struct tile *ptile, bool may_be_on_center)
Definition city.c:2080
static void citizen_content_buildings(struct city *pcity)
Definition city.c:2490
bool is_free_worked(const struct city *pcity, const struct tile *ptile)
Definition city.c:3496
bool city_production_gets_caravan_shields(const struct universal *tgt)
Definition city.c:1829
static bv_imprs caravan_helped_impr
Definition city.c:1740
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:1119
static int player_base_citizen_happiness(const struct player *pplayer)
Definition city.c:2129
bool can_city_build_unit_later(const struct city *pcity, const struct unit_type *punittype)
Definition city.c:946
int city_granary_size(int city_size)
Definition city.c:2101
citizens player_angry_citizens(const struct player *pplayer)
Definition city.c:2165
int city_pollution_types(const struct city *pcity, int shield_total, int *pollu_prod, int *pollu_pop, int *pollu_mod)
Definition city.c:2686
void city_set_ai_data(struct city *pcity, const struct ai_type *ai, void *data)
Definition city.c:3512
static void get_worked_tile_output(const struct city *pcity, int *output, bool *workers_map)
Definition city.c:2261
static void citizen_happy_wonders(struct city *pcity)
Definition city.c:2611
static void city_support(struct city *pcity)
Definition city.c:2990
bool city_built_last_turn(const struct city *pcity)
Definition city.c:2248
bool can_city_build_now(const struct city *pcity, const struct universal *target)
Definition city.c:989
int city_waste(const struct city *pcity, Output_type_id otype, int total, int *breakdown)
Definition city.c:3118
int city_unit_unhappiness(struct unit *punit, int *free_unhappy)
Definition city.c:2940
void generate_city_map_indices(void)
Definition city.c:522
int city_build_slots(const struct city *pcity)
Definition city.c:2831
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:1212
const char * city_style_name_translation(const int style)
Definition city.c:1725
void city_styles_free(void)
Definition city.c:3320
static int city_tile_cache_get_output(const struct city *pcity, int city_tile_index, enum output_type_id o)
Definition city.c:2368
Output_type_id output_type_by_identifier(const char *id)
Definition city.c:642
bool is_capital(const struct city *pcity)
Definition city.c:1548
struct citystyle * city_styles
Definition city.c:79
void city_styles_alloc(int num)
Definition city.c:3305
const char * city_name_get(const struct city *pcity)
Definition city.c:1111
bool base_city_can_work_tile(const struct player *restriction, const struct city *pcity, const struct tile *ptile)
Definition city.c:1373
int city_unit_slots_available(const struct city *pcity)
Definition city.c:1023
void city_production_caravan_shields_init(void)
Definition city.c:1747
void city_remove_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3280
int city_improvement_upkeep(const struct city *pcity, const struct impr_type *b)
Definition city.c:1227
int city_airlift_max(const struct city *pcity)
Definition city.c:2841
bool citymindist_prevents_city_on_tile(const struct civ_map *nmap, const struct tile *ptile)
Definition city.c:1434
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:3297
int city_population(const struct city *pcity)
Definition city.c:1165
static struct iter_index * city_map_index
Definition city.c:59
const char * city_style_rule_name(const int style)
Definition city.c:1734
void city_size_add(struct city *pcity, int add)
Definition city.c:1138
bool city_can_use_specialist(const struct city *pcity, Specialist_type_id type)
Definition city.c:1039
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:1634
void free_city_map_index(void)
Definition city.c:604
void city_refresh_from_main_map(struct city *pcity, bool *workers_map)
Definition city.c:3065
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:2214
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:2035
bool city_got_defense_effect(const struct city *pcity, const struct unit_type *attacker)
Definition city.c:1565
citizens player_content_citizens(const struct player *pplayer)
Definition city.c:2155
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:2339
int get_city_tithes_bonus(const struct city *pcity)
Definition city.c:2196
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:1622
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:1595
bool is_friendly_city_near(const struct civ_map *nmap, const struct player *owner, const struct tile *ptile)
Definition city.c:2061
Specialist_type_id best_specialist(Output_type_id otype, const struct city *pcity)
Definition city.c:3243
struct city * create_city_virtual(struct player *pplayer, struct tile *ptile, const char *name)
Definition city.c:3339
void set_city_production(struct city *pcity)
Definition city.c:2852
struct city * city_list_find_number(struct city_list *This, int id)
Definition city.c:1648
static bv_unit_types caravan_helped_utype
Definition city.c:1741
int get_final_city_output_bonus(const struct city *pcity, Output_type_id otype)
Definition city.c:2181
bool city_celebrating(const struct city *pcity)
Definition city.c:1614
bool city_can_be_built_tile_only(const struct civ_map *nmap, const struct tile *ptile)
Definition city.c:1529
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:2769
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:1981
int city_change_production_penalty(const struct city *pcity, const struct universal *target)
Definition city.c:1855
static int get_city_health(const struct city *pcity)
Definition city.c:2752
bool is_valid_city_coords(const int city_radius_sq, const int city_map_x, const int city_map_y)
Definition city.c:183
static void set_city_bonuses(struct city *pcity)
Definition city.c:2322
static void set_surpluses(struct city *pcity)
Definition city.c:2382
bool city_can_be_built_here(const struct civ_map *nmap, const struct tile *ptile, const struct unit *punit, bool hut_test)
Definition city.c:1456
int city_pollution(const struct city *pcity, int shield_total)
Definition city.c:2720
bool city_happy(const struct city *pcity)
Definition city.c:1583
void city_size_set(struct city *pcity, citizens size)
Definition city.c:1154
void city_add_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3266
int city_tile_output_now(const struct city *pcity, const struct tile *ptile, Output_type_id otype)
Definition city.c:1356
int city_map_radius_sq_get(const struct city *pcity)
Definition city.c:132
bool can_city_build_direct(const struct city *pcity, const struct universal *target)
Definition city.c:971
struct city * tile_allied_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:2005
struct city * tile_enemy_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:1990
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:3415
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:3226
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:3536
static int cmp(int v1, int v2)
Definition city.c:320
#define CITYLOG_MAX_VAL
Definition city.c:363
void city_choose_build_default(struct city *pcity)
Definition city.c:1061
int city_style_by_translated_name(const char *s)
Definition city.c:1690
void add_specialist_output(const struct city *pcity, int *output)
Definition city.c:2301
void * city_ai_data(const struct city *pcity, const struct ai_type *ai)
Definition city.c:3504
struct city * city_list_find_name(struct city_list *This, const char *name)
Definition city.c:1664
static void citizen_happiness_nationality(struct city *pcity)
Definition city.c:2514
bool can_city_build_later(const struct city *pcity, const struct universal *target)
Definition city.c:1006
int city_tile_output(const struct city *pcity, const struct tile *ptile, bool is_celebrating, Output_type_id otype)
Definition city.c:1255
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 is_gov_center(const struct city *pcity)
Definition city.c:1556
#define log_ca_s_init
static void citizen_happy_luxury(struct city *pcity)
Definition city.c:2480
int city_map_tiles(int city_radius_sq)
Definition city.c:166
static void unhappy_city_check(struct city *pcity)
Definition city.c:2659
bool can_city_build_unit_direct(const struct city *pcity, const struct unit_type *punittype)
Definition city.c:888
struct city * tile_non_attack_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:2020
bool city_exist(int id)
Definition city.c:3467
bool city_can_work_tile(const struct city *pcity, const struct tile *ptile)
Definition city.c:1425
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:2051
int city_turns_to_grow(const struct city *pcity)
Definition city.c:1965
void city_rally_point_clear(struct city *pcity)
Definition city.c:3521
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_had_recent_plague(const struct city *pcity)
Definition city.c:2822
static int get_trade_illness(const struct city *pcity)
Definition city.c:2732
bool city_can_change_build(const struct city *pcity)
Definition city.c:1053
int city_total_unit_gold_upkeep(const struct city *pcity)
Definition city.c:1194
int city_style_by_rule_name(const char *s)
Definition city.c:1707
static void citizen_base_mood(struct city *pcity)
Definition city.c:2404
int city_name_compare(const void *p1, const void *p2)
Definition city.c:1680
int city_total_impr_gold_upkeep(const struct city *pcity)
Definition city.c:1175
bool can_city_build_unit_now(const struct city *pcity, const struct unit_type *punittype)
Definition city.c:928
static void citizen_happy_units(struct city *pcity)
Definition city.c:2563
static void happy_copy(struct city *pcity, enum citizen_feeling i)
Definition city.c:2392
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:812
#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:835
#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:801
#define is_free_worked_index(city_tile_index)
Definition city.h:847
#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:807
#define output_type_iterate_end
Definition city.h:818
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:161
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
#define sz_strlcat(dest, src)
Definition support.h:162
#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:2827
bool unit_being_aggressive(const struct unit *punit)
Definition unit.c:1525
bool is_military_unit(const struct unit *punit)
Definition unit.c:322
bool is_field_unit(const struct unit *punit)
Definition unit.c:386
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1715
struct unit * unit_occupies_tile(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1387
#define unit_tile(_pu)
Definition unit.h:388
#define unit_owner(_pu)
Definition unit.h:387
#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:2661
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:838
#define unit_type_iterate_end
Definition unittype.h:845
#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