Freeciv-3.3
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 "counters.h"
33#include "effects.h"
34#include "game.h"
35#include "government.h"
36#include "improvement.h"
37#include "map.h"
38#include "movement.h"
39#include "packets.h"
40#include "specialist.h"
41#include "traderoutes.h"
42#include "unit.h"
43
44/* aicore */
45#include "cm.h"
46
47#include "city.h"
48
49/* Define this to add in extra (very slow) assertions for the city code. */
50#undef CITY_DEBUGGING
51
52static char *citylog_map_line(int y, int city_radius_sq, int *city_map_data);
53#ifdef FREECIV_DEBUG
54/* Only used for debugging */
55static void citylog_map_index(enum log_level level);
56static void citylog_map_radius_sq(enum log_level level);
57#endif /* FREECIV_DEBUG */
58
59/* Get city tile information using the city tile index. */
61/* Get city tile information using the city tile coordinates. This is an
62 * [x][y] array of integer values corresponding to city_map_index. The
63 * coordinates x and y are in the range [0, CITY_MAP_MAX_SIZE] */
65
66/* Number of tiles of a city; depends on the squared city radius */
68
69/* Definitions and functions for the tile_cache */
70struct tile_cache {
72};
73
74static inline void city_tile_cache_update(const struct civ_map *nmap,
75 struct city *pcity);
76static inline int city_tile_cache_get_output(const struct city *pcity,
78 enum output_type_id o);
79
80static const struct city *nearest_gov_center(const struct city *pcity,
81 int *min_dist)
82 fc__attribute((nonnull (1, 2)));
83
85
86/* One day these values may be read in from the ruleset. In the meantime
87 * they're just an easy way to access information about each output type. */
89 {O_FOOD, N_("Food"), "food", TRUE, UNHAPPY_PENALTY_SURPLUS},
90 {O_SHIELD, N_("Shield"), "shield", TRUE, UNHAPPY_PENALTY_SURPLUS},
91 {O_TRADE, N_("Trade"), "trade", TRUE, UNHAPPY_PENALTY_NONE},
92 {O_GOLD, N_("Gold"), "gold", FALSE, UNHAPPY_PENALTY_ALL_PRODUCTION},
93 {O_LUXURY, N_("Luxury"), "luxury", FALSE, UNHAPPY_PENALTY_NONE},
94 {O_SCIENCE, N_("Science"), "science", FALSE, UNHAPPY_PENALTY_ALL_PRODUCTION}
95};
96
97/**********************************************************************/
102 int city_tile_index, int city_radius_sq)
103{
106
107 /* tile indices are sorted from smallest to largest city radius */
108 if (city_tile_index < 0
109 || city_tile_index >= city_map_tiles(city_radius_sq)) {
110 return FALSE;
111 }
112
115
116 return TRUE;
117}
118
119/**********************************************************************/
124 int city_radius_sq)
125{
126 fc_assert_ret_val(city_radius_sq >= CITY_MAP_MIN_RADIUS_SQ, 0);
127 fc_assert_ret_val(city_radius_sq <= CITY_MAP_MAX_RADIUS_SQ, 0);
129 city_map_y), 0);
130
132}
133
134/**********************************************************************/
138{
139 /* a save return value is only the minimal squared radius */
141
142 return pcity->city_radius_sq;
143}
144
145/**********************************************************************/
148void city_map_radius_sq_set(struct city *pcity, int radius_sq)
149{
152
153 pcity->city_radius_sq = radius_sq;
154}
155
156/**********************************************************************/
166
167/**********************************************************************/
171int city_map_tiles(int city_radius_sq)
172{
173 if (city_radius_sq == CITY_MAP_CENTER_RADIUS_SQ) {
174 /* special case: city center; first tile of the city map */
175 return 0;
176 }
177
178 fc_assert_ret_val(city_radius_sq >= CITY_MAP_MIN_RADIUS_SQ, -1);
179 fc_assert_ret_val(city_radius_sq <= CITY_MAP_MAX_RADIUS_SQ, -1);
180
181 return city_map_numtiles[city_radius_sq];
182}
183
184/**********************************************************************/
188bool is_valid_city_coords(const int city_radius_sq, const int city_map_x,
189 const int city_map_y)
190{
191 /* The city's valid positions are in a circle around the city center.
192 * Depending on the value for the squared city radius the circle will be:
193 *
194 * - rectangular (max radius = 5; max squared radius = 26)
195 *
196 * 0 1 2 3 4 5 6 7 8 9 10
197 *
198 * 0 26 25 26 -5
199 * 1 25 20 17 16 17 20 25 -4
200 * 2 25 18 13 10 9 10 13 18 25 -3
201 * 3 20 13 8 5 4 5 8 13 20 -2
202 * 4 26 17 10 5 2 1 2 5 10 17 26 -1
203 * 5 25 16 9 4 1 0 1 4 9 16 25 +0
204 * 6 26 17 10 5 2 1 2 5 10 17 26 +1
205 * 7 20 13 8 5 4 5 8 13 20 +2
206 * 8 25 18 13 10 9 10 13 18 25 +3
207 * 9 25 20 17 16 17 20 25 +4
208 * 10 26 25 26 +5
209 *
210 * -5 -4 -3 -2 -1 +0 +1 +2 +3 +4 +5
211 *
212 * - hexagonal (max radius = 5; max squared radius = 26)
213 *
214 * 0 1 2 3 4 5 6 7 8 9 10
215 *
216 * 0 25 25 25 25 25 25 -5
217 * 1 25 16 16 16 16 16 25 -4
218 * 2 25 16 9 9 9 9 16 25 -3
219 * 3 25 16 9 4 4 4 9 16 25 -2
220 * 4 25 16 9 4 1 1 4 9 16 25 -1
221 * 5 25 16 9 4 1 0 1 4 9 16 25 +0
222 * 6 25 16 9 4 1 1 4 9 16 25 +1
223 * 7 25 16 9 4 4 4 9 16 25 +2
224 * 8 25 16 9 9 9 9 16 25 +3
225 * 9 25 16 16 16 16 16 25 +4
226 * 10 25 25 25 25 25 25 +5
227 *
228 * -5 -4 -3 -2 -1 +0 +1 +2 +3 +4 +5
229 *
230 * The following tables show the tiles per city radii / squared city radii.
231 * '-' indicates no change compared to the previous value
232 *
233 * radius | 0 | 1 | | | 2 | | | | | 3
234 * radius_sq | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10
235 * ------------------+----+----+----+----+----+----+----+----+----+----
236 * tiles rectangular | 5 | 9 | - | 13 | 21 | - | - | 25 | 29 | 37
237 * tiles hexagonal | 7 | - | - | 19 | - | - | - | - | 37 | -
238 *
239 * radius | | | | | | | 4 | | |
240 * radius_sq | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20
241 * ------------------+----+----+----+----+----+----+----+----+----+----
242 * tiles rectangular | - | - | 45 | - | - | 49 | 57 | 61 | - | 69
243 * tiles hexagonal | - | - | - | - | - | 61 | - | - | - | -
244 *
245 * radius | | | | | | 5
246 * radius_sq | 21 | 22 | 23 | 24 | 25 | 26
247 * ------------------+----+----+----+----+----+----
248 * tiles rectangular | - | - | - | - | 81 | 89
249 * tiles hexagonal | - | - | - | - | 91 | -
250 *
251 * So radius_sq == 5 (radius == 2) corresponds to the "traditional"
252 * used city map.
253 */
256
257 return dist <= city_radius_sq;
258}
259
260/**********************************************************************/
265 const int city_radius_sq,
266 const struct tile *city_center,
267 const struct tile *map_tile)
268{
270
273
274 return is_valid_city_coords(city_radius_sq, *city_map_x, *city_map_y);
275}
276
277/**********************************************************************/
282 const struct city *const pcity,
283 const struct tile *map_tile)
284{
287 map_tile);
288}
289
290/**********************************************************************/
293bool city_map_includes_tile(const struct city *const pcity,
294 const struct tile *map_tile)
295{
296 int tmp_x, tmp_y;
297
299}
300
301/**********************************************************************/
305struct tile *city_map_to_tile(const struct civ_map *nmap,
306 const struct tile *city_center,
307 int city_radius_sq, int city_map_x,
308 int city_map_y)
309{
310 int tile_x, tile_y;
311
313 city_map_y), NULL);
314
315 index_to_map_pos(&tile_x, &tile_y, tile_index(city_center));
318
320}
321
322/**********************************************************************/
325static int cmp(int v1, int v2)
326{
327 if (v1 == v2) {
328 return 0;
329 } else if (v1 > v2) {
330 return 1;
331 } else {
332 return -1;
333 }
334}
335
336/**********************************************************************/
343int compare_iter_index(const void *a, const void *b)
344{
345 const struct iter_index *index1 = a, *index2 = b;
346 int value;
347
348 value = cmp(index1->dist, index2->dist);
349 if (value != 0) {
350 return value;
351 }
352
353 value = cmp(index1->dx, index2->dx);
354 if (value != 0) {
355 return value;
356 }
357
358 value = cmp(index1->dy, index2->dy);
359 fc_assert(0 != value);
360 return value;
361}
362
363/**********************************************************************/
368#define CITYLOG_MAX_VAL 9999 /* maximal value displayed in the citylog */
369static char *citylog_map_line(int y, int city_radius_sq, int *city_map_data)
370{
371 int x, mindex;
372 static char citylog[128], tmp[8];
373
375
376 /* print y coordinates (absolute) */
377 fc_snprintf(citylog, sizeof(citylog), "%2d ", y);
378
379 /* print values */
380 for (x = 0; x < CITY_MAP_MAX_SIZE; x++) {
381 if (is_valid_city_coords(city_radius_sq, x, y)) {
382 mindex = city_tile_xy_to_index(x, y, city_radius_sq);
383 /* show values between -10000 and +10000 */
386 fc_snprintf(tmp, sizeof(tmp), "%5d", city_map_data[mindex]);
388 } else {
389 fc_snprintf(tmp, sizeof(tmp), " ####");
391 }
392 } else {
393 fc_snprintf(tmp, sizeof(tmp), " ");
395 }
396 }
397
398 /* print y coordinates (relative) */
399 fc_snprintf(tmp, sizeof(tmp), " %+4d", CITY_ABS2REL(y));
401
402 return citylog;
403}
404#undef CITYLOG_MAX_VAL
405
406/**********************************************************************/
411void citylog_map_data(enum log_level level, int radius_sq, int *map_data)
412{
413 int x, y;
414 char line[128], tmp[8];
415
417 return;
418 }
419
420 log_base(level, "(max squared city radius = %d)", CITY_MAP_MAX_RADIUS_SQ);
421
422 /* print x coordinates (absolute) */
423 fc_snprintf(line, sizeof(line), " ");
424 for (x = 0; x < CITY_MAP_MAX_SIZE; x++) {
425 fc_snprintf(tmp, sizeof(tmp), "%+5d", x);
427 }
428 log_base(level, "%s", line);
429
430 for (y = 0; y < CITY_MAP_MAX_SIZE; y++) {
431 log_base(level, "%s", citylog_map_line(y, radius_sq, map_data));
432 }
433
434 /* print x coordinates (relative) */
435 fc_snprintf(line, sizeof(line), " ");
436 for (x = 0; x < CITY_MAP_MAX_SIZE; x++) {
437 fc_snprintf(tmp, sizeof(tmp), "%+5d", CITY_ABS2REL(x));
439 }
440 log_base(level, "%s", line);
441}
442
443/**********************************************************************/
447{
448 int *city_map_data = NULL;
449 const struct civ_map *nmap = &(wld.map);
450
452
454 return;
455 }
456
458 sizeof(*city_map_data));
459
461 struct tile *ptile = city_map_to_tile(nmap,
464 x, y);
465 city_map_data[cindex] = (ptile && tile_worked(ptile) == pcity)
466 ? (is_free_worked_index(cindex) ? 2 : 1) : 0;
468
469 log_base(level, "[%s (%d)] workers map:", city_name_get(pcity), pcity->id);
472}
473
474#ifdef FREECIV_DEBUG
475/**********************************************************************/
478static void citylog_map_index(enum log_level level)
479{
480 int *city_map_data = NULL;
481
483 return;
484 }
485
487 sizeof(*city_map_data));
488
490 city_map_data[cindex] = cindex;
492
493 log_debug("city map index:");
496}
497
498/**********************************************************************/
501static void citylog_map_radius_sq(enum log_level level)
502{
503 int *city_map_data = NULL;
504
506 return;
507 }
508
510 sizeof(*city_map_data));
511
514 CITY_ABS2REL(y));
516
517 log_debug("city map squared radius:");
520}
521#endif /* FREECIV_DEBUG */
522
523/**********************************************************************/
528{
529 int i, dx, dy, city_x, city_y, dist, city_count_tiles = 0;
532
533 /* initialise map information for each city radii */
534 for (i = 0; i <= CITY_MAP_MAX_RADIUS_SQ; i++) {
535 city_map_numtiles[i] = 0; /* will be set below */
536 }
537
538 /* We don't use city-map iterators in this function because they may
539 * rely on the indices that have not yet been generated. Furthermore,
540 * we don't know the number of tiles within the city radius, so we need
541 * an temporary city_map_index array. Its content will be copied into
542 * the real array below. */
546
551
552 for (i = CITY_MAP_MAX_RADIUS_SQ; i >= 0; i--) {
553 if (dist <= i) {
554 /* increase number of tiles within this squared city radius */
556 }
557 }
558
560 }
561
562 /* Initialise city_map_xy. -1 defines a invalid city map positions. */
564 }
565 }
566
569
570 /* copy the index numbers from city_map_index_tmp into city_map_index */
571 for (i = 0; i < city_count_tiles; i++) {
573 }
574
577
578 /* set the static variable city_map_xy */
579 for (i = 0; i < city_count_tiles; i++) {
583 }
584
585#ifdef FREECIV_DEBUG
588
590 log_debug("radius_sq = %2d, tiles = %2d", i, city_map_tiles(i));
591 }
592
593 for (i = 0; i < city_count_tiles; i++) {
596 log_debug("[%2d]: (dx,dy) = (%+2d,%+2d), (x,y) = (%2d,%2d), "
597 "dist = %2d, check = %2d", i,
600 }
601#endif /* FREECIV_DEBUG */
602
604}
605
606/**********************************************************************/
610{
612}
613
614/**********************************************************************/
620{
621 fc_assert_ret_val(output >= 0 && output < O_LAST, NULL);
622 return output_types[output].id;
623}
624
625/**********************************************************************/
630{
631 fc_assert_ret_val(output >= 0 && output < O_LAST, NULL);
632 return _(output_types[output].name);
633}
634
635/**********************************************************************/
639{
640 fc_assert_ret_val(output >= 0 && output < O_LAST, NULL);
641 return &output_types[output];
642}
643
644/**********************************************************************/
648{
650
651 for (o = 0; o < O_LAST; o++) {
652 if (fc_strcasecmp(output_types[o].id, id) == 0) {
653 return o;
654 }
655 }
656
657 return O_LAST;
658}
659
660/**********************************************************************/
664 const struct impr_type *pimprove)
665{
666 static char buffer[256];
667 const char *state = NULL;
668
669 if (is_great_wonder(pimprove)) {
670 if (great_wonder_is_available(pimprove)) {
671 state = Q_("?wonder:W");
672 } else if (great_wonder_is_destroyed(pimprove)) {
673 state = Q_("?destroyed:D");
674 } else {
675 state = Q_("?built:B");
676 }
677 }
678 if (pcity) {
679 struct player *pplayer = city_owner(pcity);
680
681 if (improvement_obsolete(pplayer, pimprove, pcity)) {
682 state = Q_("?obsolete:O");
683 } else if (is_improvement_redundant(pcity, pimprove)) {
684 state = Q_("?redundant:*");
685 }
686 }
687
688 if (state) {
689 fc_snprintf(buffer, sizeof(buffer), "%s(%s)",
690 improvement_name_translation(pimprove), state);
691 return buffer;
692 } else {
693 return improvement_name_translation(pimprove);
694 }
695}
696
697/**********************************************************************/
701{
702 static char buffer[256];
703
704 switch (pcity->production.kind) {
705 case VUT_IMPROVEMENT:
706 return city_improvement_name_translation(pcity, pcity->production.value.building);
707 default:
708 /* fallthru */
709 break;
710 };
711 return universal_name_translation(&pcity->production, buffer, sizeof(buffer));
712}
713
714/**********************************************************************/
718 enum impr_genus_id genus)
719{
720 return VUT_IMPROVEMENT == pcity->production.kind
721 && (pcity->production.value.building->genus == genus);
722}
723
724/**********************************************************************/
728 enum impr_flag_id flag)
729{
730 return VUT_IMPROVEMENT == pcity->production.kind
731 && improvement_has_flag(pcity->production.value.building, flag);
732}
733
734/**********************************************************************/
738{
739 return universal_build_shield_cost(pcity, &pcity->production);
740}
741
742/**********************************************************************/
748 bool add_production, int *num_units)
749{
750 const struct unit_type *utype;
751 struct universal target;
753 int shields_left = pcity->shield_stock;
754 int unit_shield_cost, i;
755
757 (*num_units) = 0;
758
759 if (pcity->production.kind != VUT_UTYPE) {
760 /* not a unit as the current production */
761 return FALSE;
762 }
763
764 utype = pcity->production.value.utype;
765 if (utype_pop_value(utype, pcity) != 0 || utype_has_flag(utype, UTYF_UNIQUE)) {
766 /* unit with population cost or unique unit means that only one unit can
767 * be build */
768 (*num_units)++;
769 return FALSE;
770 }
771
772 if (add_production) {
773 shields_left += pcity->prod[O_SHIELD];
774 }
775
777
778 for (i = 0; i < build_slots; i++) {
780 /* not enough shields */
781 break;
782 }
783
784 (*num_units)++;
786
787 if (worklist_length(&pcity->worklist) > i) {
788 (void) worklist_peek_ith(&pcity->worklist, &target, i);
789 if (target.kind != VUT_UTYPE
790 || utype_index(target.value.utype) != utype_index(utype)) {
791 /* stop if there is a build target in the worklist not equal to the
792 * unit we build */
793 break;
794 }
795 }
796 }
797
798 return TRUE;
799}
800
801/************************************************************************/
805 const struct unit_type *punittype)
806{
810
811 levels = CLIP(0, levels, max_levels);
812
813 return levels;
814}
815
816/**********************************************************************/
822{
824}
825
826/**********************************************************************/
831 const struct impr_type *pimprove)
832{
834 return FALSE;
835 }
836
837 if (city_has_building(pcity, pimprove)) {
838 return FALSE;
839 }
840
841 return are_reqs_active(&(const struct req_context) {
842 .player = city_owner(pcity),
843 .city = pcity,
844 .tile = pcity->tile,
845 },
846 NULL,
847 &(pimprove->reqs), RPT_CERTAIN);
848}
849
850/**********************************************************************/
855 const struct impr_type *pimprove)
856{
858 return FALSE;
859 }
860 if (improvement_obsolete(city_owner(pcity), pimprove, pcity)) {
861 return FALSE;
862 }
863
864 return TRUE;
865}
866
867/**********************************************************************/
872 const struct impr_type *pimprove)
873{
874 const struct req_context city_ctxt = {
876 .city = pcity,
877 .tile = city_tile(pcity),
878 };
879
880 /* Can the _player_ ever build this improvement? */
881 /* NOTE: It checks for obsoletion player-level. What aboult checking
882 * for it city-level? That may unlist from a worklist some things
883 * we'll be able to switch to after e.g. selling something else */
885 return FALSE;
886 }
887
888 /* Check for requirements that aren't met and that are unchanging (so
889 * they can never be met). */
892 return FALSE;
893 }
895
896 return TRUE;
897}
898
899/**********************************************************************/
904 const struct city *pcity,
905 const struct unit_type *punittype)
906{
908 return FALSE;
909 }
910
911 /* Check unit build requirements.
912 * Above player level check already checked anything with range >= REQ_RANGE_PLAYER.
913 * Don't recheck those. Not only for optimization, but also not to override the
914 * special handling of tech requirements for barbarians */
915 if (!are_reqs_active_ranges(0, /* The lowest range; REQ_RANGE_LOCAL */
917 &(const struct req_context) {
918 .player = city_owner(pcity),
919 .city = pcity,
920 .tile = city_tile(pcity),
921 .unittype = punittype,
922 },
923 NULL,
924 &punittype->build_reqs, RPT_CERTAIN)) {
925 return FALSE;
926 }
927
928 /* You can't build naval units inland. */
931 pcity->tile)) {
932 return FALSE;
933 }
934
935 if (punittype->city_slots > 0
936 && city_unit_slots_available(pcity) < punittype->city_slots) {
937 return FALSE;
938 }
939
940 return TRUE;
941}
942
943/**********************************************************************/
948 const struct city *pcity,
949 const struct unit_type *punittype)
950{
952 return FALSE;
953 }
954
955 while ((punittype = punittype->obsoleted_by) != U_NOT_OBSOLETED) {
956 /* TODO: Decide if fulfilled impr_req is needed to make unit obsolete,
957 * i.e., should the 'consider_reg_impr_req' be TRUE or FALSE. */
959 return FALSE;
960 }
961 }
962
963 return TRUE;
964}
965
966/**********************************************************************/
971 const struct city *pcity,
972 const struct unit_type *punittype)
973{
974 /* Can the _player_ ever build this unit? */
976 return FALSE;
977 }
978
979 /* Some units can be built only in certain cities -- for instance,
980 ships may be built only in cities adjacent to ocean. */
983 pcity->tile)) {
984 return FALSE;
985 }
986
987 return TRUE;
988}
989
990/**********************************************************************/
995 const struct city *pcity,
996 const struct universal *target)
997{
998 switch (target->kind) {
999 case VUT_UTYPE:
1001 case VUT_IMPROVEMENT:
1003 default:
1004 break;
1005 };
1006 return FALSE;
1007}
1008
1009/**********************************************************************/
1014 const struct city *pcity,
1015 const struct universal *target)
1016{
1017 switch (target->kind) {
1018 case VUT_UTYPE:
1019 return can_city_build_unit_now(nmap, pcity, target->value.utype);
1020 case VUT_IMPROVEMENT:
1022 default:
1023 break;
1024 };
1025 return FALSE;
1026}
1027
1028/**********************************************************************/
1032 const struct city *pcity,
1033 const struct universal *target)
1034{
1035 switch (target->kind) {
1036 case VUT_UTYPE:
1038 case VUT_IMPROVEMENT:
1040 default:
1041 break;
1042 };
1043 return FALSE;
1044}
1045
1046/**********************************************************************/
1050{
1052 int current;
1053
1054 current = 0;
1055 unit_list_iterate(pcity->units_supported, punit) {
1056 current += unit_type_get(punit)->city_slots;
1058
1059 return max - current;
1060}
1061
1062/**********************************************************************/
1067{
1068 return are_reqs_active(&(const struct req_context) {
1069 .player = city_owner(pcity),
1070 .city = pcity,
1071 },
1072 NULL,
1074}
1075
1076/**********************************************************************/
1080{
1081 return !pcity->did_buy || pcity->shield_stock <= 0;
1082}
1083
1084/**********************************************************************/
1088{
1089 if (NULL == city_tile(pcity)) {
1090 /* When a "dummy" city is created with no tile, then choosing a build
1091 * target could fail. This currently might happen during map editing.
1092 * FIXME: assumes the first unit is always "valid", so check for
1093 * obsolete units elsewhere. */
1094 pcity->production.kind = VUT_UTYPE;
1095 pcity->production.value.utype = utype_by_number(0);
1096 } else {
1098
1099 if (u) {
1100 pcity->production.kind = VUT_UTYPE;
1101 pcity->production.value.utype = u;
1102 } else {
1103 bool found = FALSE;
1104
1105 /* Just pick the first available item. */
1106 improvement_iterate(pimprove) {
1108 found = TRUE;
1109 pcity->production.kind = VUT_IMPROVEMENT;
1110 pcity->production.value.building = pimprove;
1111 break;
1112 }
1114
1115 if (!found) {
1118#ifndef FREECIV_NDEBUG
1119 /* Later than this, 'found' is only needed in an fc_assert() */
1120 found = TRUE;
1121#endif /* FREECIV_NDEBUG */
1122 pcity->production.kind = VUT_UTYPE;
1123 pcity->production.value.utype = punittype;
1124 }
1126 }
1127
1128 fc_assert_msg(found, "No production found for city %s!",
1130 }
1131 }
1132}
1133
1134/**********************************************************************/
1137const char *city_name_get(const struct city *pcity)
1138{
1139 return (pcity->name != NULL) ? pcity->name : "City missing a name";
1140}
1141
1142/**********************************************************************/
1145void city_name_set(struct city *pcity, const char *new_name)
1146{
1147 if (pcity->name != NULL) {
1148 free(pcity->name);
1149 }
1150
1151 if (strlen(new_name) < MAX_LEN_CITYNAME) {
1152 pcity->name = fc_strdup(new_name);
1153 } else {
1154 log_warn(_("City name \"%s\" too long"), new_name);
1156 sz_strlcpy(pcity->name, new_name);
1157 }
1158}
1159
1160/**********************************************************************/
1164void city_size_add(struct city *pcity, int add)
1165{
1167
1170
1171 /* Client sets size to zero to start stacking citizens in */
1172 fc_assert_ret(size > -add || (!is_server() && size == add));
1173
1174 city_size_set(pcity, size + add);
1175}
1176
1177/**********************************************************************/
1181{
1183
1184 /* Set city size. */
1185 pcity->size = size;
1186}
1187
1188/**********************************************************************/
1191int city_population(const struct city *pcity)
1192{
1193 /* Sum_{i=1}^{n} i == n*(n+1)/2 */
1194 return city_size_get(pcity) * (city_size_get(pcity) + 1) * 5;
1195}
1196
1197/**********************************************************************/
1202{
1203 int gold_needed = 0;
1204
1205 if (pcity == NULL) {
1206 return 0;
1207 }
1208
1209 city_built_iterate(pcity, pimprove) {
1210 gold_needed += city_improvement_upkeep(pcity, pimprove);
1212
1214
1215 return MAX(gold_needed, 0);
1216}
1217
1218/**********************************************************************/
1223{
1224 int gold_needed = 0;
1225
1226 if (pcity == NULL || pcity->units_supported == NULL) {
1227 return 0;
1228 }
1229
1230 unit_list_iterate(pcity->units_supported, punit) {
1231 gold_needed += punit->upkeep[O_GOLD];
1233
1234 return gold_needed;
1235}
1236
1237/**********************************************************************/
1240bool city_has_building(const struct city *pcity,
1241 const struct impr_type *pimprove)
1242{
1243 if (NULL == pimprove) {
1244 /* Callers should ensure that any external data is tested with
1245 * valid_improvement_by_number() */
1246 return FALSE;
1247 }
1248 return (pcity->built[improvement_index(pimprove)].turn > I_NEVER);
1249}
1250
1251/**********************************************************************/
1256 const struct impr_type *b)
1257{
1258 int upkeep;
1259
1260 if (NULL == b) {
1261 return 0;
1262 }
1263 if (!is_building_sellable(b)) {
1264 return 0;
1265 }
1266
1267 upkeep = b->upkeep;
1269 return 0;
1270 }
1271
1272 return upkeep;
1273}
1274
1275/**********************************************************************/
1283int city_tile_output(const struct city *pcity, const struct tile *ptile,
1285{
1286 int prod;
1287 const struct req_context city_ctxt = {
1289 .city = pcity,
1290 .tile = ptile,
1291 };
1292 struct terrain *pterrain = tile_terrain(ptile);
1293 const struct output_type *output = &output_types[otype];
1294
1295 fc_assert_ret_val(otype >= 0 && otype < O_LAST, 0);
1296
1297 if (T_UNKNOWN == pterrain) {
1298 /* Special case for the client. The server doesn't allow unknown tiles
1299 * to be worked but we don't necessarily know what player is involved. */
1300 return 0;
1301 }
1302
1303 prod = pterrain->output[otype];
1304 if (tile_resource_is_valid(ptile)) {
1305 prod += tile_resource(ptile)->data.resource->output[otype];
1306 }
1307
1308 switch (otype) {
1309 case O_SHIELD:
1310 if (pterrain->mining_shield_incr != 0) {
1311 prod += pterrain->mining_shield_incr
1313 / 100;
1314 }
1315 break;
1316 case O_FOOD:
1317 if (pterrain->irrigation_food_incr != 0) {
1318 prod += pterrain->irrigation_food_incr
1320 EFT_IRRIGATION_PCT) / 100;
1321 }
1322 break;
1323 case O_TRADE:
1324 case O_GOLD:
1325 case O_SCIENCE:
1326 case O_LUXURY:
1327 case O_LAST:
1328 break;
1329 }
1330
1331 prod += tile_roads_output_incr(ptile, otype);
1332 prod += (prod * tile_roads_output_bonus(ptile, otype) / 100);
1333
1334 prod += get_tile_output_bonus(pcity, ptile, output, EFT_OUTPUT_ADD_TILE);
1335 if (prod > 0) {
1336 int penalty_limit = get_tile_output_bonus(pcity, ptile, output,
1338
1339 if (prod >= game.info.granularity) {
1340 prod += get_tile_output_bonus(pcity, ptile, output,
1342
1343 if (is_celebrating) {
1344 prod += get_tile_output_bonus(pcity, ptile, output,
1346 }
1347 }
1348
1349 prod += (prod
1350 * get_tile_output_bonus(pcity, ptile, output,
1352 / 100;
1353 if (penalty_limit > 0 && prod > penalty_limit) {
1354 if (prod <= game.info.granularity) {
1355 prod = 0;
1356 } else {
1357 prod -= game.info.granularity;
1358 }
1359 }
1360 }
1361
1362 prod -= (prod
1363 * get_tile_output_bonus(pcity, ptile, output,
1365 / 100;
1366
1367 if (NULL != pcity && is_city_center(pcity, ptile)) {
1368 prod = MAX(prod, game.info.min_city_center_output[otype]);
1369 }
1370
1371 return prod;
1372}
1373
1374/**********************************************************************/
1384int city_tile_output_now(const struct city *pcity, const struct tile *ptile,
1386{
1388}
1389
1390/**********************************************************************/
1402 const struct city *pcity,
1403 const struct tile *ptile)
1404{
1405 struct player *powner = city_owner(pcity);
1407 struct player *towner;
1408
1409 if (NULL == ptile) {
1410 return FALSE;
1411 }
1412
1414 return FALSE;
1415 }
1416
1417 if (NULL != restriction
1418 && TILE_UNKNOWN == tile_get_known(ptile, restriction)) {
1419 return FALSE;
1420 }
1421
1422 towner = tile_owner(ptile);
1423 if (NULL != towner && towner != powner
1424 && !gives_shared_tiles(towner, powner)) {
1425 return FALSE;
1426 }
1427 /* TODO: civ3-like option for borders */
1428
1429 if (NULL != tile_worked(ptile) && tile_worked(ptile) != pcity) {
1430 return FALSE;
1431 }
1432
1433 if (powner == restriction
1434 && TILE_KNOWN_SEEN != tile_get_known(ptile, powner)) {
1435 return FALSE;
1436 }
1437
1438 if (!is_free_worked(pcity, ptile)
1439 && NULL != unit_occupies_tile(ptile, powner)) {
1440 return FALSE;
1441 }
1442
1444 return FALSE;
1445 }
1446
1447 return TRUE;
1448}
1449
1450/**********************************************************************/
1456bool city_can_work_tile(const struct city *pcity, const struct tile *ptile)
1457{
1459}
1460
1461/**********************************************************************/
1466 const struct tile *ptile)
1467{
1468 /* citymindist minimum is 1, meaning adjacent is okay */
1469 int citymindist = game.info.citymindist;
1470
1471 square_iterate(nmap, ptile, citymindist - 1, ptile1) {
1472 if (tile_city(ptile1)) {
1473 return TRUE;
1474 }
1476
1477 return FALSE;
1478}
1479
1480/**********************************************************************/
1488 const struct tile *ptile,
1489 const struct unit *punit,
1490 bool hut_test)
1491{
1492 if (!city_can_be_built_tile_only(nmap, ptile)) {
1493 return FALSE;
1494 }
1495
1496 if (punit == NULL) {
1497 /* The remaining checks tests if punit can found a city here */
1498 return TRUE;
1499 }
1500
1501 if (hut_test) {
1502 struct player *towner;
1503
1504 /* Huts can be found only from native tiles, owned by the unit owner.
1505 * Unlike actual city building, this behavior is not affected
1506 * by the ruleset. */
1507 if (!can_unit_exist_at_tile(nmap, punit, ptile)) {
1508 return FALSE;
1509 }
1510
1511 towner = tile_owner(ptile);
1512
1513 if (towner == NULL || towner == unit_owner(punit)) {
1514 return TRUE;
1515 }
1516
1517 return FALSE;
1518 }
1519
1522 /* This action can't be done by this unit type at all. */
1523 continue;
1524 }
1525
1526 /* Non native tile detection */
1527 if (!can_unit_exist_at_tile(nmap, punit, ptile)
1528 /* The ruleset may allow founding cities on non native terrain. */
1531 /* Many rulesets allow land units to build land cities and sea units
1532 * to build ocean cities. Air units can build cities anywhere. */
1533 continue;
1534 }
1535
1536 /* Foreign tile detection. */
1537 if (tile_owner(ptile) && tile_owner(ptile) != unit_owner(punit)
1538 /* The ruleset may allow founding cities on foreign terrain. */
1540 paction->id,
1541 DRO_FOREIGN, TRUE)) {
1542 /* Cannot steal borders by settling. This has to be settled by
1543 * force of arms. */
1544 continue;
1545 }
1546
1547 return TRUE;
1549
1550 return FALSE;
1551}
1552
1553/**********************************************************************/
1561 const struct tile *ptile)
1562{
1564 /* No cities on this terrain. */
1565 return FALSE;
1566 }
1567
1569 return FALSE;
1570 }
1571
1572 return TRUE;
1573}
1574
1575/**********************************************************************/
1579bool is_capital(const struct city *pcity)
1580{
1581 return pcity->capital != CAPITAL_NOT;
1582}
1583
1584/**********************************************************************/
1587bool is_gov_center(const struct city *pcity)
1588{
1589 return (get_city_bonus(pcity, EFT_GOV_CENTER) > 0);
1590}
1591
1592/**********************************************************************/
1597 const struct unit_type *attacker)
1598{
1599 if (!attacker) {
1600 /* Any defense building will do */
1602 }
1603
1604 return get_unittype_bonus(city_owner(pcity), pcity->tile, attacker,
1605 NULL, EFT_DEFEND_BONUS) > 0;
1606}
1607
1608/**********************************************************************/
1614bool city_happy(const struct city *pcity)
1615{
1617 && pcity->feel[CITIZEN_ANGRY][FEELING_FINAL] == 0
1618 && pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL] == 0
1619 && pcity->feel[CITIZEN_HAPPY][FEELING_FINAL] >= (city_size_get(pcity) + 1) / 2);
1620}
1621
1622/**********************************************************************/
1626bool city_unhappy(const struct city *pcity)
1627{
1628 return (pcity->feel[CITIZEN_HAPPY][FEELING_FINAL]
1630 + 2 * pcity->feel[CITIZEN_ANGRY][FEELING_FINAL]);
1631}
1632
1633/**********************************************************************/
1638{
1639 return (city_size_get(pcity) >= game.info.celebratesize && pcity->was_happy);
1640}
1641
1642/**********************************************************************/
1645bool city_celebrating(const struct city *pcity)
1646{
1648}
1649
1650/**********************************************************************/
1653bool city_rapture_grow(const struct city *pcity)
1654{
1655 /* .rapture is checked instead of city_celebrating() because this
1656 function is called after .was_happy was updated. */
1657 return (pcity->rapture > 0 && pcity->surplus[O_FOOD] > 0
1658 && (pcity->rapture % game.info.rapturedelay) == 0
1660}
1661
1662/**********************************************************************/
1665bool city_is_occupied(const struct city *pcity)
1666{
1667 if (is_server()) {
1668 /* The server sees the units inside the city. */
1669 return (unit_list_size(city_tile(pcity)->units) > 0);
1670 } else {
1671 /* The client gets the occupied property from the server. */
1672 return pcity->client.occupied;
1673 }
1674}
1675
1676/**********************************************************************/
1679struct city *city_list_find_number(struct city_list *This, int id)
1680{
1681 if (id != 0) {
1683 if (pcity->id == id) {
1684 return pcity;
1685 }
1687 }
1688
1689 return NULL;
1690}
1691
1692/**********************************************************************/
1695struct city *city_list_find_name(struct city_list *This, const char *name)
1696{
1698 if (fc_strcasecmp(name, pcity->name) == 0) {
1699 return pcity;
1700 }
1702
1703 return NULL;
1704}
1705
1706/**********************************************************************/
1711int city_name_compare(const void *p1, const void *p2)
1712{
1713 return fc_strcasecmp((*(const struct city **) p1)->name,
1714 (*(const struct city **) p2)->name);
1715}
1716
1717/**********************************************************************/
1722{
1723 int i;
1724
1725 for (i = 0; i < game.control.num_city_styles; i++) {
1726 if (0 == strcmp(city_style_name_translation(i), s)) {
1727 return i;
1728 }
1729 }
1730
1731 return -1;
1732}
1733
1734/**********************************************************************/
1738int city_style_by_rule_name(const char *s)
1739{
1740 const char *qs = Qn_(s);
1741 int i;
1742
1743 for (i = 0; i < game.control.num_city_styles; i++) {
1744 if (0 == fc_strcasecmp(city_style_rule_name(i), qs)) {
1745 return i;
1746 }
1747 }
1748
1749 return -1;
1750}
1751
1752/**********************************************************************/
1757{
1759}
1760
1761/**********************************************************************/
1765const char *city_style_rule_name(const int style)
1766{
1768}
1769
1770/* Cache of what city production caravan shields are allowed to help. */
1773
1774/**********************************************************************/
1779{
1780 struct requirement prod_as_req;
1781
1782#define log_ca_s_init log_debug
1783
1784 /* Remove old data. */
1787
1788 /* Common for all production kinds. */
1790 prod_as_req.survives = FALSE;
1791 prod_as_req.present = TRUE;
1792
1793 /* Check improvements */
1794 prod_as_req.source.kind = VUT_IMPROVEMENT;
1795
1797 /* Check this improvement. */
1798 prod_as_req.source.value.building = itype;
1799
1802 enabler) {
1804 &(enabler->target_reqs))
1805 && !req_vec_wants_type(&(enabler->target_reqs), VUT_UTYPE)
1806 && !req_vec_wants_type(&(enabler->target_reqs), VUT_UCLASS)
1807 && !req_vec_wants_type(&(enabler->target_reqs), VUT_UTFLAG)
1808 && !req_vec_wants_type(&(enabler->target_reqs), VUT_UCFLAG)) {
1809 /* This improvement kind can receive caravan shields. */
1810
1812
1813 /* Move on to the next improvement */
1814 break;
1815 }
1817
1818 log_ca_s_init("Help Wonder: %s for %s",
1820 ? "possible" : "impossible"),
1823
1824 /* Check units. */
1825 prod_as_req.source.kind = VUT_UTYPE;
1826
1828 /* Check this utype. */
1829 prod_as_req.source.value.utype = putype;
1830
1833 enabler) {
1835 &(enabler->target_reqs))
1836 && !req_vec_wants_type(&(enabler->target_reqs), VUT_IMPROVEMENT)
1837 && !req_vec_wants_type(&(enabler->target_reqs), VUT_IMPR_GENUS)) {
1838 /* This unit type kind can receive caravan shields. */
1839
1841
1842 /* Move on to the next unit type */
1843 break;
1844 }
1846
1847 log_ca_s_init("Help Wonder: %s for %s",
1849 ? "possible" : "impossible"),
1852
1853#undef log_ca_s_init
1854}
1855
1856/**********************************************************************/
1861{
1862 switch (tgt->kind) {
1863 case VUT_IMPROVEMENT:
1866 case VUT_UTYPE:
1868 utype_index(tgt->value.utype));
1869 default:
1871 return FALSE;
1872 };
1873}
1874
1875/**********************************************************************/
1887 const struct universal *target)
1888{
1893
1894 switch (pcity->changed_from.kind) {
1895 case VUT_IMPROVEMENT:
1896 if (is_wonder(pcity->changed_from.value.building)) {
1898 } else {
1900 }
1901 break;
1902 case VUT_UTYPE:
1904 break;
1905 default:
1907 break;
1908 };
1909
1910 switch (target->kind) {
1911 case VUT_IMPROVEMENT:
1912 if (is_wonder(target->value.building)) {
1914 } else {
1916 }
1917 break;
1918 case VUT_UTYPE:
1920 break;
1921 default:
1923 break;
1924 };
1925
1926 /* Changing production is penalized under certain circumstances. */
1927 if (orig_class == new_class
1928 || orig_class == PCT_LAST) {
1929 /* There's never a penalty for building something of the same class. */
1930 unpenalized_shields = pcity->before_change_shields;
1931 } else if (city_built_last_turn(pcity)) {
1932 /* Surplus shields from the previous production won't be penalized if
1933 * you change production on the very next turn. But you can only use
1934 * up to the city's surplus amount of shields in this way. */
1935 unpenalized_shields = MIN(pcity->last_turns_shield_surplus,
1936 pcity->before_change_shields);
1937 penalized_shields = pcity->before_change_shields - unpenalized_shields;
1938 } else {
1939 /* Penalize 50% of the production. */
1940 penalized_shields = pcity->before_change_shields;
1941 }
1942
1943 /* Do not put penalty on these. It shouldn't matter whether you disband unit
1944 before or after changing production...*/
1945 unpenalized_shields += pcity->disbanded_shields;
1946
1947 /* Caravan shields are penalized (just as if you disbanded the caravan)
1948 * if you're not building a wonder. */
1950 unpenalized_shields += pcity->caravan_shields;
1951 } else {
1952 penalized_shields += pcity->caravan_shields;
1953 }
1954
1957
1959}
1960
1961/**********************************************************************/
1966 const struct universal *target,
1968{
1969 int city_shield_surplus = pcity->surplus[O_SHIELD];
1972 int cost = universal_build_shield_cost(pcity, target);
1973
1974 if (target->kind == VUT_IMPROVEMENT
1975 && is_great_wonder(target->value.building)
1977 return FC_INFINITY;
1978 }
1979
1981 return 1;
1982 } else if (city_shield_surplus > 0) {
1983 return (cost - city_shield_stock - 1) / city_shield_surplus + 1;
1984 } else {
1985 return FC_INFINITY;
1986 }
1987}
1988
1989/**********************************************************************/
1997{
1998 int food_stock = pcity->food_stock;
1999 int surplus = pcity->surplus[O_FOOD];
2000 int granary_size = city_granary_size(city_size_get(pcity));
2001
2002 if (food_stock + surplus >= granary_size) {
2004 return 1;
2005 } else {
2006 return 0;
2007 }
2008 }
2009 if (surplus > 0) {
2010 /* It was possible for the numerator in this calculation to be
2011 * negative, e.g. the city just lost pop due to a disaster and now
2012 * the granary_size is smaller than the food_stock. In that case, the
2013 * returned value was wrong, so now we do that first test. */
2014 return (granary_size - food_stock + surplus - 1) / surplus;
2015 }
2016 if (surplus < 0) {
2017 /* Turns before famine loss */
2018 return -1 + (food_stock / surplus);
2019 }
2020 return FC_INFINITY;
2021}
2022
2023/**********************************************************************/
2026bool city_can_grow_to(const struct city *pcity, int pop_size)
2027{
2030}
2031
2032/**********************************************************************/
2035struct city *tile_enemy_city(const struct tile *ptile,
2036 const struct player *pplayer)
2037{
2038 struct city *pcity = tile_city(ptile);
2039
2040 if (pcity != NULL && pplayers_at_war(pplayer, city_owner(pcity))) {
2041 return pcity;
2042 }
2043
2044 return NULL;
2045}
2046
2047/**********************************************************************/
2050struct city *tile_allied_city(const struct tile *ptile,
2051 const struct player *pplayer)
2052{
2053 struct city *pcity = tile_city(ptile);
2054
2055 if (pcity != NULL && pplayers_allied(pplayer, city_owner(pcity))) {
2056 return pcity;
2057 }
2058
2059 return NULL;
2060}
2061
2062/**********************************************************************/
2065struct city *tile_non_attack_city(const struct tile *ptile,
2066 const struct player *pplayer)
2067{
2068 struct city *pcity = tile_city(ptile);
2069
2070 if (pcity != NULL && pplayers_non_attack(pplayer, city_owner(pcity))) {
2071 return pcity;
2072 }
2073
2074 return NULL;
2075}
2076
2077/**********************************************************************/
2080struct city *tile_non_allied_city(const struct tile *ptile,
2081 const struct player *pplayer)
2082{
2083 struct city *pcity = tile_city(ptile);
2084
2085 if (pcity != NULL && !pplayers_allied(pplayer, city_owner(pcity))) {
2086 return pcity;
2087 }
2088
2089 return NULL;
2090}
2091
2092/**********************************************************************/
2096 const struct unit *punit,
2097 int distance)
2098{
2100 distance);
2101}
2102
2103/**********************************************************************/
2107 const struct player *owner,
2108 const struct tile *ptile,
2109 int distance)
2110{
2111 square_iterate(nmap, ptile, distance, ptile1) {
2112 struct city *pcity = tile_city(ptile1);
2113
2115 return TRUE;
2116 }
2118
2119 return FALSE;
2120}
2121
2122/**********************************************************************/
2127 const struct tile *ptile,
2128 bool may_be_on_center)
2129{
2131 if (may_be_on_center || !same_pos(ptile, ptile1)) {
2132 if (tile_city(ptile1)) {
2133 return TRUE;
2134 }
2135 }
2137
2138 return FALSE;
2139}
2140
2141/**********************************************************************/
2146int city_granary_size(int city_size)
2147{
2150 int base_value;
2151
2152 /* If the city has no citizens, there is no granary. */
2153 if (city_size == 0) {
2154 return 0;
2155 }
2156
2157 /* Granary sizes for the first food_inis citizens are given directly.
2158 * After that we increase the granary size by food_inc per citizen. */
2159 if (city_size > food_inis) {
2160 base_value = game.info.granary_food_ini[food_inis - 1];
2161 base_value += food_inc * (city_size - food_inis);
2162 } else {
2163 base_value = game.info.granary_food_ini[city_size - 1];
2164 }
2165
2166 return MAX(base_value * game.info.foodbox / 100, 1);
2167}
2168
2169/**********************************************************************/
2174static int player_base_citizen_happiness(const struct player *pplayer)
2175{
2176 int cities = city_list_size(pplayer->cities);
2177 int content = get_player_bonus(pplayer, EFT_CITY_UNHAPPY_SIZE);
2180
2181 if (basis + step <= 0) {
2182 /* Value of zero means effect is inactive */
2183 return content;
2184 }
2185
2186 if (cities > basis) {
2187 content--;
2188 if (step != 0) {
2189 /* the first penalty is at (basis + 1) cities;
2190 the next is at (basis + step + 1), _not_ (basis + step) */
2191 content -= (cities - basis - 1) / step;
2192 }
2193 }
2194 return content;
2195}
2196
2197/**********************************************************************/
2201{
2202 int content = player_base_citizen_happiness(pplayer);
2203
2204 return CLIP(0, content, MAX_CITY_SIZE);
2205}
2206
2207/**********************************************************************/
2211{
2212 if (!game.info.angrycitizen) {
2213 return 0;
2214 } else {
2215 /* Create angry citizens only if we have a negative number of possible
2216 * content citizens. This can happen when empires grow really big. */
2217 int content = player_base_citizen_happiness(pplayer);
2218
2219 return CLIP(0, -content, MAX_CITY_SIZE);
2220 }
2221}
2222
2223/**********************************************************************/
2227{
2228 struct output_type *output = &output_types[otype];
2229 int bonus1 = 100 + get_city_tile_output_bonus(pcity, NULL, output,
2231 int bonus2 = 100 + get_city_tile_output_bonus(pcity, NULL, output,
2233
2234 return MAX(bonus1 * bonus2 / 100, 0);
2235}
2236
2237/**********************************************************************/
2242{
2243 int tithes_bonus = 0;
2244
2246 return 0;
2247 }
2248
2251
2252 return tithes_bonus;
2253}
2254
2255/**********************************************************************/
2259void add_tax_income(const struct player *pplayer, int trade, int *output)
2260{
2261 const int SCIENCE = 0, TAX = 1, LUXURY = 2;
2262 unsigned rates[3];
2263 int result[3];
2264
2265 if (game.info.changable_tax) {
2266 rates[SCIENCE] = pplayer->economic.science;
2267 rates[LUXURY] = pplayer->economic.luxury;
2268 rates[TAX] = 100 - rates[SCIENCE] - rates[LUXURY];
2269 } else {
2273 }
2274
2275 /* ANARCHY */
2277 rates[SCIENCE] = 0;
2278 rates[LUXURY] = 100;
2279 rates[TAX] = 0;
2280 }
2281
2282 distribute(trade, 3, rates, result);
2283
2284 output[O_SCIENCE] += result[SCIENCE];
2285 output[O_GOLD] += result[TAX];
2286 output[O_LUXURY] += result[LUXURY];
2287}
2288
2289/**********************************************************************/
2294{
2295 return pcity->turn_last_built + 1 >= game.info.turn;
2296}
2297
2298/**********************************************************************/
2301static const struct city *nearest_gov_center(const struct city *pcity,
2302 int *min_dist)
2303{
2304 const struct city *gov_center = NULL;
2305
2307
2308 /* Check the special case that city itself is gov center
2309 * before expensive iteration through all cities. */
2310 if (is_gov_center(pcity)) {
2311 *min_dist = 0;
2312 return pcity;
2313 } else {
2315 /* Do not recheck current city */
2316 if (gc != pcity && is_gov_center(gc)) {
2317 int dist = real_map_distance(gc->tile, pcity->tile);
2318
2319 if (dist < *min_dist) {
2320 gov_center = gc;
2321 *min_dist = dist;
2322 }
2323 }
2325 }
2326
2327 return gov_center;
2328}
2329
2330/**********************************************************************/
2338static inline void get_worked_tile_output(const struct civ_map *nmap,
2339 const struct city *pcity,
2340 int *output, bool *workers_map)
2341{
2342 bool is_worked;
2343#ifdef CITY_DEBUGGING
2345#endif
2346 struct tile *pcenter = city_tile(pcity);
2347
2348 memset(output, 0, O_LAST * sizeof(*output));
2349
2352 if (workers_map == NULL) {
2353 struct city *pwork = tile_worked(ptile);
2354
2355 is_worked = (NULL != pwork && pwork == pcity);
2356 } else {
2357 is_worked = workers_map[city_tile_index];
2358 }
2359
2360 if (is_worked) {
2362#ifdef CITY_DEBUGGING
2363 /* This assertion never fails, but it's so slow that we disable
2364 * it by default. */
2366 == city_tile_output(pcity, ptile, is_celebrating, o));
2367#endif /* CITY_DEBUGGING */
2370 }
2372}
2373
2374/**********************************************************************/
2378void add_specialist_output(const struct city *pcity, int *output)
2379{
2381 int count = pcity->specialists[sp];
2382
2385
2386 output[stat_index] += count * amount;
2389}
2390
2391/**********************************************************************/
2400static inline void set_city_bonuses(struct city *pcity)
2401{
2404 pcity->abs_bonus[o] = get_city_output_bonus(pcity,
2405 &output_types[o],
2407 /* Total bonus cannot be negative, as that could lead to unresolvable
2408 * negative balance. */
2409 pcity->abs_bonus[o] = MIN(pcity->abs_bonus[o], 0);
2411}
2412
2413/**********************************************************************/
2423static inline void city_tile_cache_update(const struct civ_map *nmap,
2424 struct city *pcity)
2425{
2427 int radius_sq = city_map_radius_sq_get(pcity);
2428
2429 /* Initialize tile_cache if needed */
2430 if (pcity->tile_cache == NULL || pcity->tile_cache_radius_sq == -1
2431 || pcity->tile_cache_radius_sq != radius_sq) {
2432 pcity->tile_cache = fc_realloc(pcity->tile_cache,
2433 city_map_tiles(radius_sq)
2434 * sizeof(*(pcity->tile_cache)));
2435 pcity->tile_cache_radius_sq = radius_sq;
2436 }
2437
2438 /* Any unreal tiles are skipped - these values should have been memset
2439 * to 0 when the city was created. */
2440 city_tile_iterate_index(nmap, radius_sq, pcity->tile, ptile, city_tile_index) {
2442 (pcity->tile_cache[city_tile_index]).output[o]
2446}
2447
2448/**********************************************************************/
2452static inline int city_tile_cache_get_output(const struct city *pcity,
2453 int city_tile_index,
2454 enum output_type_id o)
2455{
2456 fc_assert_ret_val(pcity->tile_cache_radius_sq
2459
2460 return (pcity->tile_cache[city_tile_index]).output[o];
2461}
2462
2463/**********************************************************************/
2466static void set_surpluses(struct city *pcity)
2467{
2469 int surplus = pcity->prod[o] - pcity->usage[o];
2470
2471 /* Add 'surplus' waste to 'usage'. */
2472 if (surplus > 0) {
2473 struct output_type *output = get_output_type(o);
2477
2478 if (waste_by_rel_dist > 0) {
2479 int min_dist;
2480 const struct city *gov_center = nearest_gov_center(pcity, &min_dist);
2481
2482 if (gov_center == NULL) {
2483 /* No gov center - no income */
2484 waste_level = 100;
2485 } else {
2486 waste_level += waste_by_rel_dist * 50 * min_dist / 100
2488 }
2489 }
2490
2491 if (waste_level > 0) {
2492 if (waste_level < 100) {
2493 pcity->usage[o] += (surplus * waste_level / 100);
2494 } else {
2495 pcity->usage[o] = pcity->prod[o];
2496 }
2497 }
2498 }
2499
2500 pcity->surplus[o] = pcity->prod[o] - pcity->usage[o];
2502}
2503
2504/**********************************************************************/
2507static void happy_copy(struct city *pcity, enum citizen_feeling i)
2508{
2509 int c = 0;
2510
2511 for (; c < CITIZEN_LAST; c++) {
2512 pcity->feel[c][i] = pcity->feel[c][i - 1];
2513 }
2514}
2515
2516/**********************************************************************/
2519static void citizen_base_mood(struct city *pcity)
2520{
2521 struct player *pplayer = city_owner(pcity);
2522 citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_BASE];
2523 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_BASE];
2524 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_BASE];
2525 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_BASE];
2528
2529 /* This is the number of citizens that may start out content, depending
2530 * on empire size and game's city unhappysize. This may be bigger than
2531 * the size of the city, since this is a potential. */
2533 /* Similarly, this is the potential number of angry citizens. */
2535
2536 /* Create content citizens. Take specialists from their ranks. */
2537 *content = MAX(0, MIN(size, base_content) - spes);
2538
2539 /* Create angry citizens. Specialists never become angry. */
2540 fc_assert_action(base_content == 0 || base_angry == 0, *content = 0);
2541 *angry = MIN(base_angry, size - spes);
2542
2543 /* Create unhappy citizens. In the beginning, all who are not content,
2544 * specialists or angry are unhappy. This is changed by luxuries and
2545 * buildings later. */
2546 *unhappy = (size - spes - *content - *angry);
2547
2548 /* No one is born happy. */
2549 *happy = 0;
2550}
2551
2552/**********************************************************************/
2558static inline void citizen_luxury_happy(struct city *pcity, int *luxuries)
2559{
2560 citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_LUXURY];
2561 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_LUXURY];
2562 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_LUXURY];
2563 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_LUXURY];
2564
2565 while (*luxuries >= game.info.happy_cost && *angry > 0) {
2566 /* Upgrade angry to unhappy: costs HAPPY_COST each. */
2567 (*angry)--;
2568 (*unhappy)++;
2570 }
2571 while (*luxuries >= game.info.happy_cost && *content > 0) {
2572 /* Upgrade content to happy: costs HAPPY_COST each. */
2573 (*content)--;
2574 (*happy)++;
2576 }
2577 while (*luxuries >= 2 * game.info.happy_cost && *unhappy > 0) {
2578 /* Upgrade unhappy to happy. Note this is a 2-level upgrade with
2579 * double the cost. */
2580 (*unhappy)--;
2581 (*happy)++;
2582 *luxuries -= 2 * game.info.happy_cost;
2583 }
2584 if (*luxuries >= game.info.happy_cost && *unhappy > 0) {
2585 /* Upgrade unhappy to content: costs HAPPY_COST each. */
2586 (*unhappy)--;
2587 (*content)++;
2589 }
2590}
2591
2592/**********************************************************************/
2595static inline void citizen_happy_luxury(struct city *pcity)
2596{
2597 int x = pcity->prod[O_LUXURY];
2598
2600}
2601
2602/**********************************************************************/
2605static inline void citizen_content_buildings(struct city *pcity)
2606{
2607 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_EFFECT];
2608 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_EFFECT];
2609 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_EFFECT];
2611
2612 /* make people content (but not happy):
2613 get rid of angry first, then make unhappy content. */
2614 while (faces > 0 && *angry > 0) {
2615 (*angry)--;
2616 (*unhappy)++;
2617 faces--;
2618 }
2619 while (faces > 0 && *unhappy > 0) {
2620 (*unhappy)--;
2621 (*content)++;
2622 faces--;
2623 }
2624}
2625
2626/**********************************************************************/
2629static inline void citizen_happiness_nationality(struct city *pcity)
2630{
2634
2637
2638 if (pct > 0) {
2639 int enemies = 0;
2640 int unhappy_inc;
2641 struct player *owner = city_owner(pcity);
2642
2643 citizens_foreign_iterate(pcity, pslot, nationality) {
2645 enemies += nationality;
2646 }
2648
2649 unhappy_inc = enemies * pct / 100;
2650
2651 /* First make content => unhappy, then happy => unhappy,
2652 * then happy => content. No-one becomes angry. */
2653 while (unhappy_inc > 0 && *content > 0) {
2654 (*content)--;
2655 (*unhappy)++;
2656 unhappy_inc--;
2657 }
2658 while (unhappy_inc > 1 && *happy > 0) {
2659 (*happy)--;
2660 (*unhappy)++;
2661 unhappy_inc -= 2;
2662 }
2663 while (unhappy_inc > 0 && *happy > 0) {
2664 (*happy)--;
2665 (*content)++;
2666 unhappy_inc--;
2667 }
2668 }
2669 }
2670}
2671
2672/**********************************************************************/
2678static inline void citizen_happy_units(struct city *pcity)
2679{
2680 citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_MARTIAL];
2681 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_MARTIAL];
2682 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_MARTIAL];
2683 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_MARTIAL];
2684 citizens amt = pcity->martial_law;
2685
2686 /* Pacify discontent citizens through martial law. First convert
2687 * angry => unhappy, then unhappy => content. */
2688 while (amt > 0 && *angry > 0) {
2689 (*angry)--;
2690 (*unhappy)++;
2691 amt--;
2692 }
2693 while (amt > 0 && *unhappy > 0) {
2694 (*unhappy)--;
2695 (*content)++;
2696 amt--;
2697 }
2698
2699 /* Now make citizens unhappier because of military units away from home.
2700 * First make content => unhappy, then happy => unhappy,
2701 * then happy => content. */
2702 amt = pcity->unit_happy_upkeep;
2703 while (amt > 0 && *content > 0) {
2704 (*content)--;
2705 (*unhappy)++;
2706 amt--;
2707 }
2708 while (amt > 1 && *happy > 0) {
2709 (*happy)--;
2710 (*unhappy)++;
2711 amt -= 2;
2712 }
2713 while (amt > 0 && *happy > 0) {
2714 (*happy)--;
2715 (*content)++;
2716 amt--;
2717 }
2718 /* Any remaining unhappiness is lost since angry citizens aren't created
2719 * here. */
2720 /* FIXME: Why not? - Per */
2721}
2722
2723/**********************************************************************/
2726static inline void citizen_happy_wonders(struct city *pcity)
2727{
2728 citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_FINAL];
2729 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_FINAL];
2730 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL];
2731 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_FINAL];
2732 int bonus = get_city_bonus(pcity, EFT_MAKE_HAPPY);
2733
2734 /* First create happy citizens from content, then from unhappy
2735 * citizens; we cannot help angry citizens here. */
2736 while (bonus > 0 && *content > 0) {
2737 (*content)--;
2738 (*happy)++;
2739 bonus--;
2740 }
2741 while (bonus > 1 && *unhappy > 0) {
2742 (*unhappy)--;
2743 (*happy)++;
2744 bonus -= 2;
2745 }
2746 /* The rest falls through and lets unhappy people become content. */
2747
2749 *content += *unhappy + *angry;
2750 *unhappy = 0;
2751 *angry = 0;
2752 return;
2753 }
2754
2756
2757 /* get rid of angry first, then make unhappy content */
2758 while (bonus > 0 && *angry > 0) {
2759 (*angry)--;
2760 (*unhappy)++;
2761 bonus--;
2762 }
2763 while (bonus > 0 && *unhappy > 0) {
2764 (*unhappy)--;
2765 (*content)++;
2766 bonus--;
2767 }
2768}
2769
2770/**********************************************************************/
2774static inline void unhappy_city_check(struct city *pcity)
2775{
2776 if (city_unhappy(pcity)) {
2778 switch (output_types[o].unhappy_penalty) {
2780 pcity->unhappy_penalty[o] = 0;
2781 break;
2783 pcity->unhappy_penalty[o] = MAX(pcity->prod[o] - pcity->usage[o], 0);
2784 break;
2786 pcity->unhappy_penalty[o] = pcity->prod[o];
2787 break;
2788 }
2789
2790 pcity->prod[o] -= pcity->unhappy_penalty[o];
2792 } else {
2793 memset(pcity->unhappy_penalty, 0,
2794 O_LAST * sizeof(*pcity->unhappy_penalty));
2795 }
2796}
2797
2798/**********************************************************************/
2802 int *pollu_prod, int *pollu_pop, int *pollu_mod)
2803{
2804 int prod, pop, mod;
2805
2806 /* Add one one pollution per shield, multiplied by the bonus. */
2808 prod = shield_total * MAX(prod, 0) / 100;
2809
2810 /* Add one pollution per citizen for baseline combined bonus (100%). */
2813 / 100;
2814 pop = (city_size_get(pcity) * MAX(pop, 0)) / 100;
2815
2816 /* Then there is base pollution (usually a negative number). */
2817 mod = game.info.base_pollution;
2818
2819 if (pollu_prod) {
2820 *pollu_prod = prod;
2821 }
2822 if (pollu_pop) {
2823 *pollu_pop = pop;
2824 }
2825 if (pollu_mod) {
2826 *pollu_mod = mod;
2827 }
2828 return MAX(prod + pop + mod, 0);
2829}
2830
2831/**********************************************************************/
2835int city_pollution(const struct city *pcity, int shield_total)
2836{
2838}
2839
2840/**********************************************************************/
2847static int get_trade_illness(const struct city *pcity)
2848{
2849 float illness_trade = 0.0;
2850
2852 if (trade_city->turn_plague != -1
2853 && game.info.turn - trade_city->turn_plague < 5) {
2854 illness_trade += (float)game.info.illness_trade_infection
2855 * sqrt(1.0 * city_size_get(pcity)
2856 * city_size_get(trade_city)) / 100.0;
2857 }
2859
2860 return (int)illness_trade;
2861}
2862
2863/**********************************************************************/
2867static int get_city_health(const struct city *pcity)
2868{
2870}
2871
2872/**********************************************************************/
2884int city_illness_calc(const struct city *pcity, int *ill_base,
2885 int *ill_size, int *ill_trade, int *ill_pollution)
2886{
2887 int illness_size = 0, illness_trade = 0, illness_pollution = 0;
2889
2890 if (game.info.illness_on
2892 /* offset the city size by game.info.illness_min_size */
2894
2895 illness_size = (int)((1.0 - exp(- (float)use_size / 10.0))
2896 * 10.0 * game.info.illness_base_factor);
2897 if (is_server()) {
2898 /* On the server we recalculate the illness due to trade as we have
2899 * all the information */
2900 illness_trade = get_trade_illness(pcity);
2901 } else {
2902 /* On the client we have to rely on the value saved within the city
2903 * struct */
2904 illness_trade = pcity->illness_trade;
2905 }
2906
2907 illness_pollution = pcity->pollution
2909 }
2910
2911 illness_base = illness_size + illness_trade + illness_pollution;
2913
2914 /* returning other data */
2915 if (ill_size) {
2917 }
2918
2919 if (ill_trade) {
2920 *ill_trade = illness_trade;
2921 }
2922
2923 if (ill_pollution) {
2925 }
2926
2927 if (ill_base) {
2929 }
2930
2931 return CLIP(0, illness_base * illness_percent / 100 , 999);
2932}
2933
2934/**********************************************************************/
2938{
2939 /* Correctly handles special case turn_plague == -1 (never) */
2940 return (pcity->turn_plague == game.info.turn);
2941}
2942
2943/**********************************************************************/
2946int city_build_slots(const struct city *pcity)
2947{
2949}
2950
2951/**********************************************************************/
2956int city_airlift_max(const struct city *pcity)
2957{
2959}
2960
2961/**********************************************************************/
2967inline void set_city_production(struct city *pcity)
2968{
2969 int trade;
2970
2971 /* Calculate city production!
2972 *
2973 * This is a rather complicated process if we allow rules to become
2974 * more generalized. We can assume that there are no recursive dependency
2975 * loops, but there are some dependencies that do not follow strict
2976 * ordering. For instance corruption must be calculated before
2977 * trade taxes can be counted up, which must occur before the science bonus
2978 * is added on. But the calculation of corruption must include the
2979 * trade bonus. To do this without excessive special casing means that in
2980 * this case the bonuses are multiplied on twice (but only saved the second
2981 * time).
2982 */
2983
2985 pcity->prod[o] = pcity->citizen_base[o];
2987
2988 /* Add on special extra incomes: trade routes and tithes. */
2990 struct city *tcity = game_city_by_number(proute->partner);
2991 bool can_trade;
2992
2993 /* Partner city may have not yet been sent to the client, or
2994 * there's just a placeholder city with a placeholder owner
2995 * created for some tile->worked. */
2996 if (!is_server()
2997 && (tcity == NULL
2998 || city_owner(tcity)->slot == NULL)) {
2999 continue;
3000 }
3001
3002 fc_assert_action(tcity != NULL, continue);
3003
3005
3006 if (!can_trade) {
3009
3010 if (settings->cancelling == TRI_ACTIVE) {
3011 can_trade = TRUE;
3012 }
3013 }
3014
3015 if (can_trade) {
3016 int value;
3017
3018 value =
3020 proute->value = trade_from_route(pcity, proute, value);
3021 pcity->prod[O_TRADE] += proute->value
3022 * (100 + get_city_bonus(pcity, EFT_TRADE_ROUTE_PCT)) / 100;
3023 } else {
3024 proute->value = 0;
3025 }
3028
3029 /* Account for waste. Note that waste is calculated before tax income is
3030 * calculated, so if you had "science waste" it would not include taxed
3031 * science. However waste is calculated after the bonuses are multiplied
3032 * on, so shield waste will include shield bonuses. */
3034 int prod = pcity->prod[o] * pcity->bonus[o] / 100 + pcity->abs_bonus[o];
3035
3036 prod = MAX(prod, 0);
3037 pcity->waste[o] = city_waste(pcity, o, prod, NULL);
3039
3040 /* Convert trade into science/luxury/gold, and add this on to whatever
3041 * science/luxury/gold is already there. */
3042 trade = pcity->prod[O_TRADE] * pcity->bonus[O_TRADE] / 100
3043 + pcity->abs_bonus[O_TRADE];
3044 trade = MAX(trade, 0);
3046 trade - pcity->waste[O_TRADE] - pcity->usage[O_TRADE],
3047 pcity->prod);
3048
3049 /* Add on effect bonuses and waste. Note that the waste calculation
3050 * (above) already includes the bonuses. */
3052 int prod = pcity->prod[o] * pcity->bonus[o] / 100 + pcity->abs_bonus[o];
3053
3054 prod = MAX(prod, 0);
3055 pcity->prod[o] = prod;
3056 pcity->prod[o] -= pcity->waste[o];
3058}
3059
3060/**********************************************************************/
3064 struct unit *punit, int *free_unhappy)
3065{
3066 struct city *pcity;
3067 const struct unit_type *ut;
3068 struct player *plr;
3069 int happy_cost;
3070
3071 if (punit == NULL || free_unhappy == NULL) {
3072 return 0;
3073 }
3074
3076 if (pcity == NULL) {
3077 return 0;
3078 }
3079
3081 plr = unit_owner(punit);
3082 happy_cost = utype_happy_cost(ut, plr);
3083
3084 if (happy_cost <= 0) {
3085 return 0;
3086 }
3087
3089
3091 return 0;
3092 }
3093
3095 if (happy_cost <= 0) {
3096 return 0;
3097 }
3098
3099 if (*free_unhappy >= happy_cost) {
3100 *free_unhappy -= happy_cost;
3101 return 0;
3102 } else {
3103 happy_cost -= *free_unhappy;
3104 *free_unhappy = 0;
3105 }
3106
3107 return happy_cost;
3108}
3109
3110/**********************************************************************/
3114static inline void city_support(const struct civ_map *nmap,
3115 struct city *pcity)
3116{
3117 int free_unhappy;
3118 int max_mart_units;
3119
3120 /* Clear all usage values. */
3121 memset(pcity->usage, 0, O_LAST * sizeof(*pcity->usage));
3122 pcity->martial_law = 0;
3123 pcity->unit_happy_upkeep = 0;
3124
3125 /* Building and unit gold upkeep depends on the setting
3126 * 'game.info.gold_upkeep_style':
3127 * GOLD_UPKEEP_CITY: The upkeep for buildings and units is paid by the
3128 * city.
3129 * GOLD_UPKEEP_MIXED: The upkeep for buildings is paid by the city.
3130 * The upkeep for units is paid by the nation.
3131 * GOLD_UPKEEP_NATION: The upkeep for buildings and units is paid by the
3132 * nation. */
3134 "Invalid gold_upkeep_style %d", game.info.gold_upkeep_style);
3135 switch (game.info.gold_upkeep_style) {
3136 case GOLD_UPKEEP_CITY:
3138 fc__fallthrough; /* No break */
3139 case GOLD_UPKEEP_MIXED:
3141 break;
3142 case GOLD_UPKEEP_NATION:
3143 /* nothing */
3144 break;
3145 }
3146 /* Food consumption by citizens. */
3148
3150 if (max_mart_units > 0) {
3152 int sel_count = 0;
3153 int i;
3154
3157 && unit_owner(punit) == city_owner(pcity)) {
3158 int current = get_target_bonus_effects(NULL,
3159 &(const struct req_context) {
3160 .player = city_owner(pcity),
3161 .city = pcity,
3162 .tile = city_tile(pcity),
3163 .unit = punit,
3164 .unittype = unit_type_get(punit)
3165 },
3167 if (current > 0) {
3168 if (sel_count < max_mart_units) {
3169 best_units[sel_count++] = current;
3170 } else if (current > best_units[max_mart_units - 1]) {
3171 for (i = max_mart_units - 1; i >= 0 && current > best_units[i]; i--) {
3172 if (i + 1 < max_mart_units) {
3173 best_units[i + 1] = best_units[i];
3174 }
3175 }
3176
3177 best_units[i + 1] = current;
3178 }
3179 }
3180 }
3182
3183 pcity->martial_law = 0;
3184 for (i = 0; i < sel_count; i++) {
3185 pcity->martial_law += best_units[i];
3186 }
3187
3188 pcity->martial_law = MIN(pcity->martial_law, MAX_CITY_SIZE);
3189 }
3190
3192 unit_list_iterate(pcity->units_supported, punit) {
3193 pcity->unit_happy_upkeep += city_unit_unhappiness(nmap, punit, &free_unhappy);
3195 if (O_GOLD != o) {
3196 /* O_GOLD is handled with "game.info.gold_upkeep_style", see over. */
3197 pcity->usage[o] += punit->upkeep[o];
3198 }
3201}
3202
3203/**********************************************************************/
3216 struct city *pcity, bool *workers_map)
3217{
3218 if (workers_map == NULL) {
3219 /* do a full refresh */
3220
3221 /* Calculate the bonus[] array values. */
3223 /* Calculate the tile_cache[] values. */
3225 /* Manage settlers, and units */
3227 }
3228
3229 /* Calculate output from citizens (uses city_tile_cache_get_output()). */
3230 get_worked_tile_output(nmap, pcity, pcity->citizen_base, workers_map);
3231 add_specialist_output(pcity, pcity->citizen_base);
3232
3235 /* Note that pollution is calculated before unhappy_city_check() makes
3236 * deductions for disorder; so a city in disorder still causes pollution */
3237 pcity->pollution = city_pollution(pcity, pcity->prod[O_SHIELD]);
3238
3240 citizen_happy_luxury(pcity); /* With our new found luxuries */
3241
3244
3247
3248 /* Martial law & unrest from units */
3251
3252 /* Building (including wonder) happiness effects */
3255
3258}
3259
3260/**********************************************************************/
3267int city_waste(const struct city *pcity, Output_type_id otype, int total,
3268 int *breakdown)
3269{
3270 int penalty_waste = 0;
3271 int penalty_size = 0; /* Separate notradesize/fulltradesize from normal
3272 * corruption */
3273 int total_eft = total; /* Normal corruption calculated on total reduced by
3274 * possible size penalty */
3275 const struct output_type *output = get_output_type(otype);
3277 bool waste_all = FALSE;
3278
3279 if (otype == O_TRADE) {
3280 /* FIXME: special case for trade: it is affected by notradesize and
3281 * fulltradesize server settings.
3282 *
3283 * If notradesize and fulltradesize are equal then the city gets no
3284 * trade at that size. */
3285 int notradesize = MIN(game.info.notradesize, game.info.fulltradesize);
3286 int fulltradesize = MAX(game.info.notradesize, game.info.fulltradesize);
3287
3288 if (city_size_get(pcity) <= notradesize) {
3289 penalty_size = total_eft; /* Then no trade income. */
3290 } else if (city_size_get(pcity) >= fulltradesize) {
3291 penalty_size = 0;
3292 } else {
3293 penalty_size = total_eft * (fulltradesize - city_size_get(pcity))
3294 / (fulltradesize - notradesize);
3295 }
3296 }
3297
3298 /* Apply corruption only to anything left after tradesize */
3300
3301 /* Distance-based waste.
3302 * Don't bother calculating if there's nothing left to lose. */
3303 if (total_eft > 0) {
3308 if (waste_by_dist > 0 || waste_by_rel_dist > 0) {
3309 int min_dist;
3310 const struct city *gov_center = nearest_gov_center(pcity, &min_dist);
3311
3312 if (gov_center == NULL) {
3313 waste_all = TRUE; /* No gov center - no income */
3314 } else {
3316 if (waste_by_rel_dist > 0) {
3317 /* Multiply by 50 as an "standard size" for which EFT_OUTPUT_WASTE_BY_DISTANCE
3318 * and EFT_OUTPUT_WASTE_BY_REL_DISTANCE would give same result. */
3319 waste_level += waste_by_rel_dist * 50 * min_dist / 100
3321 }
3322 }
3323 }
3324 }
3325
3326 if (waste_all) {
3328 } else {
3331
3332 /* Corruption/waste calculated only for the actually produced amount */
3333 if (waste_level > 0) {
3335 }
3336
3337 /* Bonus calculated only for the actually produced amount */
3339
3340 /* Clip */
3342 }
3343
3344 if (breakdown) {
3347 }
3348
3349 /* Add up total penalty */
3350 return penalty_waste + penalty_size;
3351}
3352
3353/**********************************************************************/
3357{
3358 citizens count = 0;
3359
3361 fc_assert_ret_val(MAX_CITY_SIZE - count > pcity->specialists[sp], 0);
3362 count += pcity->specialists[sp];
3364
3365 return count;
3366}
3367
3368/**********************************************************************/
3374 const struct city *pcity)
3375{
3376 int best = DEFAULT_SPECIALIST;
3377 int val = get_specialist_output(pcity, best, otype);
3378
3382
3383 if (val2 > val) {
3384 best = i;
3385 val = val2;
3386 }
3387 }
3389
3390 return best;
3391}
3392
3393/**********************************************************************/
3397 const struct impr_type *pimprove)
3398{
3399 pcity->built[improvement_index(pimprove)].turn = game.info.turn; /*I_ACTIVE*/
3400
3401 if (is_server() && is_wonder(pimprove)) {
3402 /* Client just read the info from the packets. */
3403 wonder_built(pcity, pimprove);
3404 }
3405}
3406
3407/**********************************************************************/
3411 const struct impr_type *pimprove)
3412{
3413 log_debug("Improvement %s removed from city %s",
3414 improvement_rule_name(pimprove), pcity->name);
3415
3416 pcity->built[improvement_index(pimprove)].turn = I_DESTROYED;
3417
3418 if (is_server() && is_wonder(pimprove)) {
3419 /* Client just read the info from the packets. */
3420 wonder_destroyed(pcity, pimprove);
3421 }
3422}
3423
3424/**********************************************************************/
3428{
3429 return BV_ISSET(pcity->city_options, option);
3430}
3431
3432/**********************************************************************/
3436{
3437 int i;
3438
3439 city_styles = fc_calloc(num, sizeof(*city_styles));
3441
3442 for (i = 0; i < game.control.num_city_styles; i++) {
3444 }
3445}
3446
3447/**********************************************************************/
3451{
3452 int i;
3453
3454 for (i = 0; i < game.control.num_city_styles; i++) {
3456 }
3457
3459 city_styles = NULL;
3461}
3462
3463/**********************************************************************/
3469struct city *create_city_virtual(struct player *pplayer,
3470 struct tile *ptile, const char *name)
3471{
3472 int i,len;
3473
3474 /* Make sure that contents of city structure are correctly initialized,
3475 * if you ever allocate it by some other mean than fc_calloc() */
3476 struct city *pcity = fc_calloc(1, sizeof(*pcity));
3477
3478 fc_assert_ret_val(NULL != name, NULL); /* No unnamed cities! */
3479
3480 /* Do this early, so any logging later will have the city name */
3482
3483 pcity->tile = ptile;
3484 fc_assert_ret_val(NULL != pplayer, NULL); /* No unowned cities! */
3485 pcity->owner = pplayer;
3486 pcity->acquire_t = CACQ_FOUNDED;
3487
3488 if (is_server()) {
3489 pcity->original = pplayer;
3490 }
3491
3492 /* City structure was allocated with fc_calloc(), so contents are initially
3493 * zero. There is no need to initialize it a second time. */
3494
3495 /* Now set some useful default values. */
3496 pcity->capital = CAPITAL_NOT;
3497 city_size_set(pcity, 1);
3498 pcity->specialists[DEFAULT_SPECIALIST] = 1;
3499 pcity->wlcb = WLCB_SMART;
3500
3502 pcity->bonus[o] = 100;
3503 pcity->abs_bonus[o] = 0;
3505
3506 pcity->turn_plague = -1; /* -1 = never */
3507 pcity->did_buy = FALSE;
3508 pcity->city_radius_sq = game.info.init_city_radius_sq;
3509 pcity->turn_founded = game.info.turn;
3510 pcity->turn_last_built = game.info.turn;
3511
3512 pcity->tile_cache_radius_sq = -1; /* -1 = tile_cache must be initialised */
3513
3514 /* pcity->ai.act_cache: worker activities on the city map */
3515
3516 /* Initialise improvements list */
3517 for (i = 0; i < ARRAY_SIZE(pcity->built); i++) {
3518 pcity->built[i].turn = I_NEVER;
3519 }
3520
3521 /* Set up the worklist */
3522 worklist_init(&pcity->worklist);
3523
3524 pcity->units_supported = unit_list_new();
3525 pcity->routes = trade_route_list_new();
3526 pcity->task_reqs = worker_task_list_new();
3527
3528 if (is_server()) {
3529 pcity->server.mgr_score_calc_turn = -1; /* -1 = never */
3530
3531 CALL_FUNC_EACH_AI(city_alloc, pcity);
3532 } else {
3533 pcity->client.info_units_supported =
3535 pcity->client.info_units_present =
3537 /* collecting_info_units_supported set by fc_calloc().
3538 * collecting_info_units_present set by fc_calloc(). */
3539 }
3540
3542 pcity->counter_values = fc_malloc(sizeof(int) * len);
3543
3544 for (i = 0; i < len; i++) {
3545 pcity->counter_values[i] = counter_by_index(i, CTGT_CITY)->def;
3546 }
3547
3548 return pcity;
3549}
3550
3551/**********************************************************************/
3556{
3557 CALL_FUNC_EACH_AI(city_free, pcity);
3558
3560
3561 /* Free worker tasks */
3562 while (worker_task_list_size(pcity->task_reqs) > 0) {
3563 struct worker_task *ptask = worker_task_list_get(pcity->task_reqs, 0);
3564
3565 worker_task_list_remove(pcity->task_reqs, ptask);
3566
3567 free(ptask);
3568 }
3569 worker_task_list_destroy(pcity->task_reqs);
3570
3571 /* Free rally points */
3573
3574 unit_list_destroy(pcity->units_supported);
3576 if (pcity->tile_cache != NULL) {
3577 free(pcity->tile_cache);
3578 }
3579
3580 if (pcity->cm_parameter) {
3581 free(pcity->cm_parameter);
3582 }
3583
3584 if (pcity->counter_values) {
3585 free(pcity->counter_values);
3586 }
3587
3588 if (!is_server()) {
3589 unit_list_destroy(pcity->client.info_units_supported);
3590 unit_list_destroy(pcity->client.info_units_present);
3591 /* Handle a rare case where the game is freed in the middle of a
3592 * spy/diplomat investigate cycle. */
3593 if (pcity->client.collecting_info_units_supported != NULL) {
3594 unit_list_destroy(pcity->client.collecting_info_units_supported);
3595 }
3596 if (pcity->client.collecting_info_units_present != NULL) {
3597 unit_list_destroy(pcity->client.collecting_info_units_present);
3598 }
3599 }
3600
3601 free(pcity->name);
3602
3603 memset(pcity, 0, sizeof(*pcity)); /* ensure no pointers remain */
3604 free(pcity);
3605}
3606
3607/**********************************************************************/
3611bool city_exist(int id)
3612{
3613 /* Check if city exist in game */
3614 if (game_city_by_number(id)) {
3615 return TRUE;
3616 }
3617
3618 return FALSE;
3619}
3620
3621/**********************************************************************/
3628bool city_is_virtual(const struct city *pcity)
3629{
3630 if (!pcity) {
3631 return FALSE;
3632 }
3633
3634 return pcity != game_city_by_number(pcity->id);
3635}
3636
3637/**********************************************************************/
3640bool is_free_worked(const struct city *pcity, const struct tile *ptile)
3641{
3642 return is_city_center(pcity, ptile);
3643}
3644
3645/**********************************************************************/
3648void *city_ai_data(const struct city *pcity, const struct ai_type *ai)
3649{
3650 return pcity->server.ais[ai_type_number(ai)];
3651}
3652
3653/**********************************************************************/
3656void city_set_ai_data(struct city *pcity, const struct ai_type *ai,
3657 void *data)
3658{
3659 pcity->server.ais[ai_type_number(ai)] = data;
3660}
3661
3662/**********************************************************************/
3666{
3667 /* Free rally points */
3668 if (pcity->rally_point.length > 0) {
3669 pcity->rally_point.length = 0;
3670 pcity->rally_point.persistent = FALSE;
3671 pcity->rally_point.vigilant = FALSE;
3672 free(pcity->rally_point.orders);
3673 pcity->rally_point.orders = NULL;
3674 }
3675}
3676
3677/**********************************************************************/
3681 struct city *pcity)
3682{
3683 struct unit_order *checked_orders;
3684 const struct civ_map *nmap = &(wld.map);
3685
3686 if (NULL == pcity) {
3687 /* Probably lost. */
3688 log_verbose("handle_city_rally_point() bad city number %d.",
3689 packet->id);
3690 return;
3691 }
3692
3693 if (0 > packet->length || MAX_LEN_ROUTE < packet->length) {
3694 /* Shouldn't happen */
3695 log_error("city_rally_point_receive() invalid packet length %d (max %d)",
3696 packet->length, MAX_LEN_ROUTE);
3697 return;
3698 }
3699
3700 pcity->rally_point.length = packet->length;
3701
3702 if (packet->length == 0) {
3703 pcity->rally_point.vigilant = FALSE;
3704 pcity->rally_point.persistent = FALSE;
3705 if (pcity->rally_point.orders) {
3706 free(pcity->rally_point.orders);
3707 pcity->rally_point.orders = NULL;
3708 }
3709 } else {
3711 packet->orders);
3712 if (!checked_orders) {
3713 pcity->rally_point.length = 0;
3714 log_error("invalid rally point orders for %s.",
3716 return;
3717 }
3718
3719 pcity->rally_point.persistent = packet->persistent;
3720 pcity->rally_point.vigilant = packet->vigilant;
3721 pcity->rally_point.orders = checked_orders;
3722 }
3723}
struct action_enabler_list * action_enablers_for_action(action_id action)
Definition actions.c:1559
#define action_by_result_iterate(_paction_, _result_)
Definition actions.h:245
#define action_enabler_list_iterate_end
Definition actions.h:194
#define action_by_result_iterate_end
Definition actions.h:249
#define action_enabler_list_iterate(action_enabler_list, aenabler)
Definition actions.h:192
#define action_id(_act_)
Definition actions.h:426
int ai_type_number(const struct ai_type *ai)
Definition ai.c:278
#define CALL_FUNC_EACH_AI(_func,...)
Definition ai.h:387
#define BV_CLR_ALL(bv)
Definition bitvector.h:103
#define BV_SET(bv, bit)
Definition bitvector.h:89
#define BV_ISSET(bv, bit)
Definition bitvector.h:86
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:1637
bool city_is_virtual(const struct city *pcity)
Definition city.c:3628
int city_turns_to_build(const struct city *pcity, const struct universal *target, bool include_shield_stock)
Definition city.c:1965
static void citizen_luxury_happy(struct city *pcity, int *luxuries)
Definition city.c:2558
const char * city_improvement_name_translation(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:663
static void citizen_content_buildings(struct city *pcity)
Definition city.c:2605
bool is_free_worked(const struct city *pcity, const struct tile *ptile)
Definition city.c:3640
bool city_production_gets_caravan_shields(const struct universal *tgt)
Definition city.c:1860
static bv_imprs caravan_helped_impr
Definition city.c:1771
void city_map_radius_sq_set(struct city *pcity, int radius_sq)
Definition city.c:148
int city_production_build_shield_cost(const struct city *pcity)
Definition city.c:737
void city_name_set(struct city *pcity, const char *new_name)
Definition city.c:1145
static int player_base_citizen_happiness(const struct player *pplayer)
Definition city.c:2174
int city_granary_size(int city_size)
Definition city.c:2146
static void get_worked_tile_output(const struct civ_map *nmap, const struct city *pcity, int *output, bool *workers_map)
Definition city.c:2338
citizens player_angry_citizens(const struct player *pplayer)
Definition city.c:2210
int city_pollution_types(const struct city *pcity, int shield_total, int *pollu_prod, int *pollu_pop, int *pollu_mod)
Definition city.c:2801
void city_set_ai_data(struct city *pcity, const struct ai_type *ai, void *data)
Definition city.c:3656
static void citizen_happy_wonders(struct city *pcity)
Definition city.c:2726
bool is_friendly_city_near(const struct civ_map *nmap, const struct player *owner, const struct tile *ptile, int distance)
Definition city.c:2106
bool city_built_last_turn(const struct city *pcity)
Definition city.c:2293
void city_choose_build_default(const struct civ_map *nmap, struct city *pcity)
Definition city.c:1087
int city_waste(const struct city *pcity, Output_type_id otype, int total, int *breakdown)
Definition city.c:3267
static void city_support(const struct civ_map *nmap, struct city *pcity)
Definition city.c:3114
void generate_city_map_indices(void)
Definition city.c:527
int city_build_slots(const struct city *pcity)
Definition city.c:2946
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:305
bool city_has_building(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:1240
const char * city_style_name_translation(const int style)
Definition city.c:1756
void city_styles_free(void)
Definition city.c:3450
static int city_tile_cache_get_output(const struct city *pcity, int city_tile_index, enum output_type_id o)
Definition city.c:2452
Output_type_id output_type_by_identifier(const char *id)
Definition city.c:647
bool is_capital(const struct city *pcity)
Definition city.c:1579
void city_styles_alloc(int num)
Definition city.c:3435
const char * city_name_get(const struct city *pcity)
Definition city.c:1137
bool base_city_can_work_tile(const struct player *restriction, const struct city *pcity, const struct tile *ptile)
Definition city.c:1401
int city_unit_slots_available(const struct city *pcity)
Definition city.c:1049
void city_production_caravan_shields_init(void)
Definition city.c:1778
void city_remove_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3410
int city_improvement_upkeep(const struct city *pcity, const struct impr_type *b)
Definition city.c:1255
int city_airlift_max(const struct city *pcity)
Definition city.c:2956
bool citymindist_prevents_city_on_tile(const struct civ_map *nmap, const struct tile *ptile)
Definition city.c:1465
bool can_city_build_later(const struct civ_map *nmap, const struct city *pcity, const struct universal *target)
Definition city.c:1031
struct output_type * get_output_type(Output_type_id output)
Definition city.c:638
bool is_city_option_set(const struct city *pcity, enum city_options option)
Definition city.c:3427
int city_population(const struct city *pcity)
Definition city.c:1191
static struct iter_index * city_map_index
Definition city.c:60
const char * city_style_rule_name(const int style)
Definition city.c:1765
void city_size_add(struct city *pcity, int add)
Definition city.c:1164
bool city_can_use_specialist(const struct city *pcity, Specialist_type_id type)
Definition city.c:1065
bool city_production_has_flag(const struct city *pcity, enum impr_flag_id flag)
Definition city.c:727
bool city_is_occupied(const struct city *pcity)
Definition city.c:1665
void free_city_map_index(void)
Definition city.c:609
int city_production_unit_veteran_level(struct city *pcity, const struct unit_type *punittype)
Definition city.c:804
void add_tax_income(const struct player *pplayer, int trade, int *output)
Definition city.c:2259
bool can_city_build_improvement_now(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:854
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:101
struct city * tile_non_allied_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:2080
int city_unit_unhappiness(const struct civ_map *nmap, struct unit *punit, int *free_unhappy)
Definition city.c:3063
bool city_got_defense_effect(const struct city *pcity, const struct unit_type *attacker)
Definition city.c:1596
citizens player_content_citizens(const struct player *pplayer)
Definition city.c:2200
bool city_map_includes_tile(const struct city *const pcity, const struct tile *map_tile)
Definition city.c:293
static void city_tile_cache_update(const struct civ_map *nmap, struct city *pcity)
Definition city.c:2423
int get_city_tithes_bonus(const struct city *pcity)
Definition city.c:2241
bool is_unit_near_a_friendly_city(const struct civ_map *nmap, const struct unit *punit, int distance)
Definition city.c:2095
void citylog_map_data(enum log_level level, int radius_sq, int *map_data)
Definition city.c:411
static int city_map_numtiles[CITY_MAP_MAX_RADIUS_SQ+1]
Definition city.c:67
bool city_rapture_grow(const struct city *pcity)
Definition city.c:1653
int city_production_turns_to_build(const struct city *pcity, bool include_shield_stock)
Definition city.c:820
bool city_unhappy(const struct city *pcity)
Definition city.c:1626
Specialist_type_id best_specialist(Output_type_id otype, const struct city *pcity)
Definition city.c:3373
struct city * create_city_virtual(struct player *pplayer, struct tile *ptile, const char *name)
Definition city.c:3469
void set_city_production(struct city *pcity)
Definition city.c:2967
struct city * city_list_find_number(struct city_list *This, int id)
Definition city.c:1679
static bv_unit_types caravan_helped_utype
Definition city.c:1772
int get_final_city_output_bonus(const struct city *pcity, Output_type_id otype)
Definition city.c:2226
bool city_celebrating(const struct city *pcity)
Definition city.c:1645
bool city_can_be_built_tile_only(const struct civ_map *nmap, const struct tile *ptile)
Definition city.c:1560
bool can_city_build_improvement_direct(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:830
const char * get_output_identifier(Output_type_id output)
Definition city.c:619
int city_illness_calc(const struct city *pcity, int *ill_base, int *ill_size, int *ill_trade, int *ill_pollution)
Definition city.c:2884
const char * get_output_name(Output_type_id output)
Definition city.c:629
bool city_can_grow_to(const struct city *pcity, int pop_size)
Definition city.c:2026
int city_change_production_penalty(const struct city *pcity, const struct universal *target)
Definition city.c:1886
static int get_city_health(const struct city *pcity)
Definition city.c:2867
bool is_valid_city_coords(const int city_radius_sq, const int city_map_x, const int city_map_y)
Definition city.c:188
void city_refresh_from_main_map(const struct civ_map *nmap, struct city *pcity, bool *workers_map)
Definition city.c:3215
bool can_city_build_direct(const struct civ_map *nmap, const struct city *pcity, const struct universal *target)
Definition city.c:994
static void set_city_bonuses(struct city *pcity)
Definition city.c:2400
static void set_surpluses(struct city *pcity)
Definition city.c:2466
static const struct city * nearest_gov_center(const struct city *pcity, int *min_dist) fc__attribute((nonnull(1
Definition city.c:2301
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:1487
int city_pollution(const struct city *pcity, int shield_total)
Definition city.c:2835
bool city_happy(const struct city *pcity)
Definition city.c:1614
void city_size_set(struct city *pcity, citizens size)
Definition city.c:1180
void city_add_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3396
int city_tile_output_now(const struct city *pcity, const struct tile *ptile, Output_type_id otype)
Definition city.c:1384
int city_map_radius_sq_get(const struct city *pcity)
Definition city.c:137
struct city * tile_allied_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:2050
struct city * tile_enemy_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:2035
void citylog_map_workers(enum log_level level, struct city *pcity)
Definition city.c:446
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:281
void destroy_city_virtual(struct city *pcity)
Definition city.c:3555
int rs_max_city_radius_sq(void)
Definition city.c:159
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:264
citizens city_specialists(const struct city *pcity)
Definition city.c:3356
static char * citylog_map_line(int y, int city_radius_sq, int *city_map_data)
Definition city.c:369
void city_rally_point_receive(const struct packet_city_rally_point *packet, struct city *pcity)
Definition city.c:3680
static int cmp(int v1, int v2)
Definition city.c:325
#define CITYLOG_MAX_VAL
Definition city.c:368
int city_style_by_translated_name(const char *s)
Definition city.c:1721
void add_specialist_output(const struct city *pcity, int *output)
Definition city.c:2378
static const struct city struct citystyle * city_styles
Definition city.c:84
void * city_ai_data(const struct city *pcity, const struct ai_type *ai)
Definition city.c:3648
struct city * city_list_find_name(struct city_list *This, const char *name)
Definition city.c:1695
static void citizen_happiness_nationality(struct city *pcity)
Definition city.c:2629
int city_tile_output(const struct city *pcity, const struct tile *ptile, bool is_celebrating, Output_type_id otype)
Definition city.c:1283
static int city_map_xy[CITY_MAP_MAX_SIZE][CITY_MAP_MAX_SIZE]
Definition city.c:64
int city_tile_xy_to_index(int city_map_x, int city_map_y, int city_radius_sq)
Definition city.c:123
bool can_city_build_unit_direct(const struct civ_map *nmap, const struct city *pcity, const struct unit_type *punittype)
Definition city.c:903
bool is_gov_center(const struct city *pcity)
Definition city.c:1587
#define log_ca_s_init
bool can_city_build_now(const struct civ_map *nmap, const struct city *pcity, const struct universal *target)
Definition city.c:1013
static void citizen_happy_luxury(struct city *pcity)
Definition city.c:2595
bool can_city_build_unit_now(const struct civ_map *nmap, const struct city *pcity, const struct unit_type *punittype)
Definition city.c:947
int city_map_tiles(int city_radius_sq)
Definition city.c:171
static void unhappy_city_check(struct city *pcity)
Definition city.c:2774
struct city * tile_non_attack_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:2065
bool city_exist(int id)
Definition city.c:3611
bool city_can_work_tile(const struct city *pcity, const struct tile *ptile)
Definition city.c:1456
bool city_production_is_genus(const struct city *pcity, enum impr_genus_id genus)
Definition city.c:717
int compare_iter_index(const void *a, const void *b)
Definition city.c:343
bool city_production_build_units(const struct city *pcity, bool add_production, int *num_units)
Definition city.c:747
int city_turns_to_grow(const struct city *pcity)
Definition city.c:1996
void city_rally_point_clear(struct city *pcity)
Definition city.c:3665
bool can_city_build_improvement_later(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:871
struct output_type output_types[O_LAST]
Definition city.c:88
bool city_exists_within_max_city_map(const struct civ_map *nmap, const struct tile *ptile, bool may_be_on_center)
Definition city.c:2126
bool city_had_recent_plague(const struct city *pcity)
Definition city.c:2937
static int get_trade_illness(const struct city *pcity)
Definition city.c:2847
bool city_can_change_build(const struct city *pcity)
Definition city.c:1079
bool can_city_build_unit_later(const struct civ_map *nmap, const struct city *pcity, const struct unit_type *punittype)
Definition city.c:970
int city_total_unit_gold_upkeep(const struct city *pcity)
Definition city.c:1222
int city_style_by_rule_name(const char *s)
Definition city.c:1738
static void citizen_base_mood(struct city *pcity)
Definition city.c:2519
int city_name_compare(const void *p1, const void *p2)
Definition city.c:1711
int city_total_impr_gold_upkeep(const struct city *pcity)
Definition city.c:1201
static void citizen_happy_units(struct city *pcity)
Definition city.c:2678
static void happy_copy(struct city *pcity, enum citizen_feeling i)
Definition city.c:2507
const char * city_production_name_translation(const struct city *pcity)
Definition city.c:700
#define city_list_iterate(citylist, pcity)
Definition city.h:505
#define city_tile(_pcity_)
Definition city.h:561
@ UNHAPPY_PENALTY_ALL_PRODUCTION
Definition city.h:253
@ UNHAPPY_PENALTY_NONE
Definition city.h:251
@ UNHAPPY_PENALTY_SURPLUS
Definition city.h:252
#define city_tile_iterate_index(_nmap, _radius_sq, _city_tile, _tile, _index)
Definition city.h:198
#define CITY_MAP_MAX_RADIUS_SQ
Definition city.h:83
static citizens city_size_get(const struct city *pcity)
Definition city.h:566
#define CITY_MAP_MAX_SIZE
Definition city.h:91
#define city_tile_iterate_index_end
Definition city.h:206
@ CITIZEN_LAST
Definition city.h:269
@ CITIZEN_ANGRY
Definition city.h:268
@ CITIZEN_HAPPY
Definition city.h:265
@ CITIZEN_CONTENT
Definition city.h:266
@ CITIZEN_UNHAPPY
Definition city.h:267
#define CITY_MAP_CENTER_RADIUS_SQ
Definition city.h:86
#define CITY_MAP_MIN_RADIUS_SQ
Definition city.h:81
#define output_type_iterate(output)
Definition city.h:842
#define CITY_REL2ABS(_coor)
Definition city.h:112
#define city_owner(_pcity_)
Definition city.h:560
#define MAX_CITY_SIZE
Definition city.h:103
static bool is_city_center(const struct city *pcity, const struct tile *ptile)
Definition city.h:865
#define city_list_iterate_end
Definition city.h:507
#define I_DESTROYED
Definition city.h:245
#define I_NEVER
Definition city.h:244
#define city_tile_iterate(_nmap, _radius_sq, _city_tile, _tile)
Definition city.h:227
citizen_feeling
Definition city.h:275
@ FEELING_EFFECT
Definition city.h:278
@ FEELING_LUXURY
Definition city.h:277
@ FEELING_FINAL
Definition city.h:281
@ FEELING_BASE
Definition city.h:276
@ FEELING_NATIONALITY
Definition city.h:279
@ FEELING_MARTIAL
Definition city.h:280
@ OLOSS_SIZE
Definition city.h:288
@ OLOSS_WASTE
Definition city.h:287
#define city_map_iterate_end
Definition city.h:174
#define city_map_iterate(_radius_sq, _index, _x, _y)
Definition city.h:170
#define city_tile_iterate_end
Definition city.h:235
#define city_built_iterate(_pcity, _p)
Definition city.h:831
#define is_free_worked_index(city_tile_index)
Definition city.h:877
#define city_map_tiles_from_city(_pcity)
Definition city.h:124
#define CITY_ABS2REL(_coor)
Definition city.h:113
#define city_built_iterate_end
Definition city.h:837
#define PCT_LAST
Definition city.h:40
#define output_type_iterate_end
Definition city.h:848
void cm_init_citymap(void)
Definition cm.c:314
char * incite_cost
Definition comments.c:76
#define MAX_LEN_ROUTE
Definition conn_types.h:38
struct counter * counter_by_index(int index, enum counter_target target)
Definition counters.c:183
int counters_get_city_counters_count(void)
Definition counters.c:74
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:74
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 const struct action *paction struct unit struct city * pcity
Definition dialogs_g.h:78
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:74
void distribute(int number, unsigned groups, const unsigned *ratios, int *result)
Definition distribute.c:34
struct @22::@23 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:936
int get_city_bonus(const struct city *pcity, enum effect_type effect_type)
Definition effects.c:842
int get_city_output_bonus(const struct city *pcity, const struct output_type *poutput, enum effect_type effect_type)
Definition effects.c:980
int get_building_bonus(const struct city *pcity, const struct impr_type *building, enum effect_type effect_type)
Definition effects.c:1004
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:912
int get_target_bonus_effects(struct effect_list *plist, const struct req_context *context, const struct req_context *other_context, enum effect_type effect_type)
Definition effects.c:744
int effect_cumulative_max(enum effect_type type, struct universal *unis, size_t n_unis)
Definition effects.c:388
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:1031
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Definition effects.c:824
static bool is_server(void)
unsigned char citizens
Definition fc_types.h:248
@ RPT_CERTAIN
Definition fc_types.h:534
@ RPT_POSSIBLE
Definition fc_types.h:533
int Specialist_type_id
Definition fc_types.h:235
@ CTGT_CITY
Definition fc_types.h:127
#define CITY_MAP_MAX_RADIUS
Definition fc_types.h:84
output_type_id
Definition fc_types.h:101
@ O_SHIELD
Definition fc_types.h:102
@ O_FOOD
Definition fc_types.h:102
@ O_TRADE
Definition fc_types.h:102
@ O_SCIENCE
Definition fc_types.h:102
@ O_LUXURY
Definition fc_types.h:102
@ O_GOLD
Definition fc_types.h:102
@ O_LAST
Definition fc_types.h:102
#define MAX_LEN_CITYNAME
Definition fc_types.h:68
enum output_type_id Output_type_id
Definition fc_types.h:238
#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:61
struct world wld
Definition game.c:62
struct city * game_city_by_number(int id)
Definition game.c:106
struct government * government_of_player(const struct player *pplayer)
Definition government.c:114
struct city * owner
Definition citydlg.c:226
GType type
Definition repodlgs.c:1313
bool is_building_sellable(const struct impr_type *pimprove)
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:182
#define fc_assert_ret(condition)
Definition log.h:192
#define log_warn(message,...)
Definition log.h:106
#define log_verbose(message,...)
Definition log.h:110
#define fc_assert(condition)
Definition log.h:177
#define fc_assert_ret_val(condition, val)
Definition log.h:195
#define log_do_output_for_level(level)
Definition log.h:90
#define fc_assert_action(condition, action)
Definition log.h:188
#define log_debug(message,...)
Definition log.h:116
#define log_base(level, message,...)
Definition log.h:95
log_level
Definition log.h:29
@ LOG_DEBUG
Definition log.h:35
#define log_error(message,...)
Definition log.h:104
struct tile * map_pos_to_tile(const struct civ_map *nmap, int map_x, int map_y)
Definition map.c:434
int map_vector_to_sq_distance(int dx, int dy)
Definition map.c:659
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Definition map.c:1085
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:675
void map_distance_vector(int *dx, int *dy, const struct tile *tile0, const struct tile *tile1)
Definition map.c:1222
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition map.h:397
#define square_iterate_end
Definition map.h:400
#define index_to_map_pos(pmap_x, pmap_y, mindex)
Definition map.h:229
#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:350
bool is_native_near_tile(const struct civ_map *nmap, const struct unit_class *uclass, const struct tile *ptile)
Definition movement.c:496
static const char * rule_name_get(const struct name_translation *ptrans)
static const char * name_translation_get(const struct name_translation *ptrans)
struct city_list * cities
Definition packhand.c:119
int len
Definition packhand.c:127
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1388
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1409
bool pplayers_non_attack(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1463
struct player * player_slot_get_player(const struct player_slot *pslot)
Definition player.c:437
bool gives_shared_tiles(const struct player *me, const struct player *them)
Definition player.c:1497
int universal_build_shield_cost(const struct city *pcity, const struct universal *target)
bool are_reqs_active(const struct req_context *context, const struct req_context *other_context, const struct requirement_vector *reqs, const enum req_problem_type prob_type)
enum req_unchanging_status is_req_preventing(const struct req_context *context, const struct req_context *other_context, const struct requirement *req, enum req_problem_type prob_type)
bool are_reqs_active_ranges(const enum req_range min_range, const enum req_range max_range, const struct req_context *context, const struct req_context *other_context, 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)
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:1474
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
#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
struct sprite int int y
Definition sprite_g.h:31
struct sprite int x
Definition sprite_g.h:31
Definition ai.h:50
Definition city.h:317
int surplus[O_LAST]
Definition city.h:352
bv_city_options city_options
Definition city.h:400
int style
Definition city.h:324
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 def
Definition counters.h:34
struct requirement_vector reqs
Definition improvement.h:58
int dist
Definition city.h:107
int dx
Definition city.h:107
int dy
Definition city.h:107
const char * id
Definition city.h:259
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
struct requirement_vector reqs
Definition specialist.h:38
int irrigation_food_incr
Definition terrain.h:114
int output[O_LAST]
Definition terrain.h:95
int mining_shield_incr
Definition terrain.h:117
int output[O_LAST]
Definition city.c:71
Definition tile.h:50
struct unit_list * units
Definition tile.h:58
int city_slots
Definition unittype.h:557
int upkeep[O_LAST]
Definition unittype.h:543
Definition unit.h:140
int length
Definition unit.h:198
int upkeep[O_LAST]
Definition unit.h:150
bool occupied
Definition unit.h:222
int id
Definition unit.h:147
struct unit::@83 orders
bool vigilant
Definition unit.h:200
struct unit::@84::@87 server
struct tile * tile
Definition unit.h:142
struct unit::@84::@86 client
enum unit_activity changed_from
Definition unit.h:171
void * ais[FREECIV_AI_MOD_LAST]
Definition unit.h:240
int homecity
Definition unit.h:148
const struct unit_type * utype
Definition unit.h:141
struct player * owner
Definition unit.h:145
enum universals_n kind
Definition fc_types.h:609
universals_u value
Definition fc_types.h:608
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:960
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:186
#define sz_strlcpy(dest, src)
Definition support.h:195
#define fc__attribute(x)
Definition support.h:99
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
#define sz_strlcat(dest, src)
Definition support.h:196
#define fc__fallthrough
Definition support.h:119
#define T_UNKNOWN
Definition terrain.h:62
#define terrain_has_flag(terr, flag)
Definition terrain.h:177
int tile_roads_output_bonus(const struct tile *ptile, enum output_type_id o)
Definition tile.c:291
int tile_roads_output_incr(const struct tile *ptile, enum output_type_id o)
Definition tile.c:271
enum known_type tile_get_known(const struct tile *ptile, const struct player *pplayer)
Definition tile.c:392
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
#define tile_index(_pt_)
Definition tile.h:89
static bool tile_resource_is_valid(const struct tile *ptile)
Definition tile.h:108
#define tile_worked(_tile)
Definition tile.h:119
#define tile_resource(_tile)
Definition tile.h:103
@ TILE_UNKNOWN
Definition tile.h:36
@ TILE_KNOWN_SEEN
Definition tile.h:38
#define tile_terrain(_tile)
Definition tile.h:115
#define tile_owner(_tile)
Definition tile.h:97
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:554
const struct impr_type * building
Definition fc_types.h:547
struct unit_order * create_unit_orders(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2937
bool unit_being_aggressive(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:1581
bool is_field_unit(const struct unit *punit)
Definition unit.c:456
bool is_martial_law_unit(const struct unit *punit)
Definition unit.c:334
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1792
struct unit * unit_occupies_tile(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1423
#define unit_tile(_pu)
Definition unit.h:407
#define unit_owner(_pu)
Definition unit.h:406
#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:2275
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, bool consider_reg_impr_req)
Definition unittype.c:1972
int utype_veteran_levels(const struct unit_type *punittype)
Definition unittype.c:2629
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:959
const char * utype_rule_name(const struct unit_type *punittype)
Definition unittype.c:1582
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:1532
bool can_player_build_unit_later(const struct player *p, const struct unit_type *punittype)
Definition unittype.c:2103
int utype_build_shield_cost(const struct city *pcity, const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1442
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:1021
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:375
int utype_happy_cost(const struct unit_type *ut, const struct player *pplayer)
Definition unittype.c:185
static bool uclass_has_flag(const struct unit_class *punitclass, enum unit_class_flag_id flag)
Definition unittype.h:771
#define utype_class(_t_)
Definition unittype.h:754
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:622
#define unit_type_iterate(_p)
Definition unittype.h:860
#define unit_type_iterate_end
Definition unittype.h:867
#define U_NOT_OBSOLETED
Definition unittype.h:533
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
#define MAP_NATIVE_WIDTH
#define MAP_NATIVE_HEIGHT