Freeciv-3.4
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/**********************************************************************/
1145#define POLLUTION_EFT_NEARMAX -95
1146const char *city_name_getx(const struct city *pcity)
1147{
1148 bool spacer = FALSE;
1149 static char em_city_name[MAX_LEN_CITYNAME + 7];
1150
1151 em_city_name[0] = '\0';
1152
1154 sz_strlcat(em_city_name, "ₓ"); /* ₓ🞪⁻⁰… (alternatives)*/
1155 }
1156
1158
1159 if (get_city_bonus(pcity, EFT_AIRLIFT) > 0) {
1160 if (!spacer) {
1162 spacer = TRUE;
1163 }
1164 sz_strlcat(em_city_name, "🛧"); /* 🛧🛪 (alternatives)*/
1165 }
1166
1168 if (!spacer) {
1170 spacer = TRUE;
1171 }
1172 sz_strlcat(em_city_name, "♺"); /* ♺♻♳ (alternatives)*/
1173 }
1174
1175 return em_city_name;
1176}
1177
1178/**********************************************************************/
1181void city_name_set(struct city *pcity, const char *new_name)
1182{
1183 if (pcity->name != NULL) {
1184 free(pcity->name);
1185 }
1186
1187 if (strlen(new_name) < MAX_LEN_CITYNAME) {
1188 pcity->name = fc_strdup(new_name);
1189 } else {
1190 log_warn(_("City name \"%s\" too long"), new_name);
1192 sz_strlcpy(pcity->name, new_name);
1193 }
1194}
1195
1196/**********************************************************************/
1200void city_size_add(struct city *pcity, int add)
1201{
1203
1206
1207 /* Client sets size to zero to start stacking citizens in */
1208 fc_assert_ret(size > -add || (!is_server() && size == add));
1209
1210 city_size_set(pcity, size + add);
1211}
1212
1213/**********************************************************************/
1217{
1219
1220 /* Set city size. */
1221 pcity->size = size;
1222}
1223
1224/**********************************************************************/
1227int city_population(const struct city *pcity)
1228{
1229 /* Sum_{i=1}^{n} i == n*(n+1)/2 */
1230 return city_size_get(pcity) * (city_size_get(pcity) + 1) * 5;
1231}
1232
1233/**********************************************************************/
1238{
1239 int gold_needed = 0;
1240
1241 if (pcity == NULL) {
1242 return 0;
1243 }
1244
1245 city_built_iterate(pcity, pimprove) {
1246 gold_needed += city_improvement_upkeep(pcity, pimprove);
1248
1250
1251 return MAX(gold_needed, 0);
1252}
1253
1254/**********************************************************************/
1259{
1260 int gold_needed = 0;
1261
1262 if (pcity == NULL || pcity->units_supported == NULL) {
1263 return 0;
1264 }
1265
1266 unit_list_iterate(pcity->units_supported, punit) {
1267 gold_needed += punit->upkeep[O_GOLD];
1269
1270 return gold_needed;
1271}
1272
1273/**********************************************************************/
1276bool city_has_building(const struct city *pcity,
1277 const struct impr_type *pimprove)
1278{
1279 if (NULL == pimprove) {
1280 /* Callers should ensure that any external data is tested with
1281 * valid_improvement_by_number() */
1282 return FALSE;
1283 }
1284 return (pcity->built[improvement_index(pimprove)].turn > I_NEVER);
1285}
1286
1287/**********************************************************************/
1292 const struct impr_type *b)
1293{
1294 int upkeep;
1295
1296 if (NULL == b) {
1297 return 0;
1298 }
1299 if (!is_building_sellable(b)) {
1300 return 0;
1301 }
1302
1303 upkeep = b->upkeep;
1305 return 0;
1306 }
1307
1308 return upkeep;
1309}
1310
1311/**********************************************************************/
1319int city_tile_output(const struct city *pcity, const struct tile *ptile,
1321{
1322 int prod;
1323 const struct req_context city_ctxt = {
1325 .city = pcity,
1326 .tile = ptile,
1327 };
1328 struct terrain *pterrain = tile_terrain(ptile);
1329 const struct output_type *output = &output_types[otype];
1330
1331 fc_assert_ret_val(otype >= 0 && otype < O_LAST, 0);
1332
1333 if (T_UNKNOWN == pterrain) {
1334 /* Special case for the client. The server doesn't allow unknown tiles
1335 * to be worked but we don't necessarily know what player is involved. */
1336 return 0;
1337 }
1338
1339 prod = pterrain->output[otype];
1340 if (tile_resource_is_valid(ptile)) {
1341 prod += tile_resource(ptile)->data.resource->output[otype];
1342 }
1343
1344 switch (otype) {
1345 case O_SHIELD:
1346 if (pterrain->mining_shield_incr != 0) {
1347 prod += pterrain->mining_shield_incr
1349 / 100;
1350 }
1351 break;
1352 case O_FOOD:
1353 if (pterrain->irrigation_food_incr != 0) {
1354 prod += pterrain->irrigation_food_incr
1356 EFT_IRRIGATION_PCT) / 100;
1357 }
1358 break;
1359 case O_TRADE:
1360 case O_GOLD:
1361 case O_SCIENCE:
1362 case O_LUXURY:
1363 case O_LAST:
1364 break;
1365 }
1366
1367 prod += tile_roads_output_incr(ptile, otype);
1368 prod += (prod * tile_roads_output_bonus(ptile, otype) / 100);
1369
1370 prod += get_tile_output_bonus(pcity, ptile, output, EFT_OUTPUT_ADD_TILE);
1371 if (prod > 0) {
1372 int penalty_limit = get_tile_output_bonus(pcity, ptile, output,
1374
1375 if (prod >= game.info.granularity) {
1376 prod += get_tile_output_bonus(pcity, ptile, output,
1378
1379 if (is_celebrating) {
1380 prod += get_tile_output_bonus(pcity, ptile, output,
1382 }
1383 }
1384
1385 prod += (prod
1386 * get_tile_output_bonus(pcity, ptile, output,
1388 / 100;
1389 if (penalty_limit > 0 && prod > penalty_limit) {
1390 if (prod <= game.info.granularity) {
1391 prod = 0;
1392 } else {
1393 prod -= game.info.granularity;
1394 }
1395 }
1396 }
1397
1398 prod -= (prod
1399 * get_tile_output_bonus(pcity, ptile, output,
1401 / 100;
1402
1403 if (NULL != pcity && is_city_center(pcity, ptile)) {
1404 prod = MAX(prod, game.info.min_city_center_output[otype]);
1405 }
1406
1407 return prod;
1408}
1409
1410/**********************************************************************/
1420int city_tile_output_now(const struct city *pcity, const struct tile *ptile,
1422{
1424}
1425
1426/**********************************************************************/
1438 const struct city *pcity,
1439 const struct tile *ptile)
1440{
1441 struct player *powner = city_owner(pcity);
1443 struct player *towner;
1444
1445 if (NULL == ptile) {
1446 return FALSE;
1447 }
1448
1450 return FALSE;
1451 }
1452
1453 if (NULL != restriction
1454 && TILE_UNKNOWN == tile_get_known(ptile, restriction)) {
1455 return FALSE;
1456 }
1457
1458 towner = tile_owner(ptile);
1459 if (NULL != towner && towner != powner
1460 && !gives_shared_tiles(towner, powner)) {
1461 return FALSE;
1462 }
1463 /* TODO: civ3-like option for borders */
1464
1465 if (NULL != tile_worked(ptile) && tile_worked(ptile) != pcity) {
1466 return FALSE;
1467 }
1468
1469 if (powner == restriction
1470 && TILE_KNOWN_SEEN != tile_get_known(ptile, powner)) {
1471 return FALSE;
1472 }
1473
1474 if (!is_free_worked(pcity, ptile)
1475 && NULL != unit_occupies_tile(ptile, powner)) {
1476 return FALSE;
1477 }
1478
1480 return FALSE;
1481 }
1482
1483 return TRUE;
1484}
1485
1486/**********************************************************************/
1492bool city_can_work_tile(const struct city *pcity, const struct tile *ptile)
1493{
1495}
1496
1497/**********************************************************************/
1502 const struct tile *ptile)
1503{
1504 /* citymindist minimum is 1, meaning adjacent is okay */
1505 int citymindist = game.info.citymindist;
1506
1507 square_iterate(nmap, ptile, citymindist - 1, ptile1) {
1508 if (tile_city(ptile1)) {
1509 return TRUE;
1510 }
1512
1513 return FALSE;
1514}
1515
1516/**********************************************************************/
1524 const struct tile *ptile,
1525 const struct unit *punit,
1526 bool hut_test)
1527{
1528 if (!city_can_be_built_tile_only(nmap, ptile)) {
1529 return FALSE;
1530 }
1531
1532 if (punit == NULL) {
1533 /* The remaining checks tests if punit can found a city here */
1534 return TRUE;
1535 }
1536
1537 if (hut_test) {
1538 struct player *towner;
1539
1540 /* Huts can be found only from native tiles, owned by the unit owner.
1541 * Unlike actual city building, this behavior is not affected
1542 * by the ruleset. */
1543 if (!can_unit_exist_at_tile(nmap, punit, ptile)) {
1544 return FALSE;
1545 }
1546
1547 towner = tile_owner(ptile);
1548
1549 if (towner == NULL || towner == unit_owner(punit)) {
1550 return TRUE;
1551 }
1552
1553 return FALSE;
1554 }
1555
1558 /* This action can't be done by this unit type at all. */
1559 continue;
1560 }
1561
1562 /* Non native tile detection */
1563 if (!can_unit_exist_at_tile(nmap, punit, ptile)
1564 /* The ruleset may allow founding cities on non native terrain. */
1567 /* Many rulesets allow land units to build land cities and sea units
1568 * to build ocean cities. Air units can build cities anywhere. */
1569 continue;
1570 }
1571
1572 /* Foreign tile detection. */
1573 if (tile_owner(ptile) && tile_owner(ptile) != unit_owner(punit)
1574 /* The ruleset may allow founding cities on foreign terrain. */
1576 paction->id,
1577 DRO_FOREIGN, TRUE)) {
1578 /* Cannot steal borders by settling. This has to be settled by
1579 * force of arms. */
1580 continue;
1581 }
1582
1583 return TRUE;
1585
1586 return FALSE;
1587}
1588
1589/**********************************************************************/
1597 const struct tile *ptile)
1598{
1600 /* No cities on this terrain. */
1601 return FALSE;
1602 }
1603
1605 return FALSE;
1606 }
1607
1608 return TRUE;
1609}
1610
1611/**********************************************************************/
1615bool is_capital(const struct city *pcity)
1616{
1617 return pcity->capital != CAPITAL_NOT;
1618}
1619
1620/**********************************************************************/
1623bool is_gov_center(const struct city *pcity)
1624{
1625 return (get_city_bonus(pcity, EFT_GOV_CENTER) > 0);
1626}
1627
1628/**********************************************************************/
1633 const struct unit_type *attacker)
1634{
1635 if (!attacker) {
1636 /* Any defense building will do */
1638 }
1639
1640 return get_unittype_bonus(city_owner(pcity), pcity->tile, attacker,
1641 NULL, EFT_DEFEND_BONUS) > 0;
1642}
1643
1644/**********************************************************************/
1650bool city_happy(const struct city *pcity)
1651{
1653 && pcity->feel[CITIZEN_ANGRY][FEELING_FINAL] == 0
1654 && pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL] == 0
1655 && pcity->feel[CITIZEN_HAPPY][FEELING_FINAL] >= (city_size_get(pcity) + 1) / 2);
1656}
1657
1658/**********************************************************************/
1662bool city_unhappy(const struct city *pcity)
1663{
1664 return (pcity->feel[CITIZEN_HAPPY][FEELING_FINAL]
1666 + 2 * pcity->feel[CITIZEN_ANGRY][FEELING_FINAL]);
1667}
1668
1669/**********************************************************************/
1674{
1675 return (city_size_get(pcity) >= game.info.celebratesize && pcity->was_happy);
1676}
1677
1678/**********************************************************************/
1681bool city_celebrating(const struct city *pcity)
1682{
1684}
1685
1686/**********************************************************************/
1689bool city_rapture_grow(const struct city *pcity)
1690{
1691 /* .rapture is checked instead of city_celebrating() because this
1692 function is called after .was_happy was updated. */
1693 return (pcity->rapture > 0 && pcity->surplus[O_FOOD] > 0
1694 && (pcity->rapture % game.info.rapturedelay) == 0
1696}
1697
1698/**********************************************************************/
1701bool city_is_occupied(const struct city *pcity)
1702{
1703 if (is_server()) {
1704 /* The server sees the units inside the city. */
1705 return (unit_list_size(city_tile(pcity)->units) > 0);
1706 } else {
1707 /* The client gets the occupied property from the server. */
1708 return pcity->client.occupied;
1709 }
1710}
1711
1712/**********************************************************************/
1715struct city *city_list_find_number(struct city_list *This, int id)
1716{
1717 if (id != 0) {
1719 if (pcity->id == id) {
1720 return pcity;
1721 }
1723 }
1724
1725 return NULL;
1726}
1727
1728/**********************************************************************/
1731struct city *city_list_find_name(struct city_list *This, const char *name)
1732{
1734 if (fc_strcasecmp(name, pcity->name) == 0) {
1735 return pcity;
1736 }
1738
1739 return NULL;
1740}
1741
1742/**********************************************************************/
1747int city_name_compare(const void *p1, const void *p2)
1748{
1749 return fc_strcasecmp((*(const struct city **) p1)->name,
1750 (*(const struct city **) p2)->name);
1751}
1752
1753/**********************************************************************/
1758{
1759 int i;
1760
1761 for (i = 0; i < game.control.num_city_styles; i++) {
1762 if (0 == strcmp(city_style_name_translation(i), s)) {
1763 return i;
1764 }
1765 }
1766
1767 return -1;
1768}
1769
1770/**********************************************************************/
1774int city_style_by_rule_name(const char *s)
1775{
1776 const char *qs = Qn_(s);
1777 int i;
1778
1779 for (i = 0; i < game.control.num_city_styles; i++) {
1780 if (0 == fc_strcasecmp(city_style_rule_name(i), qs)) {
1781 return i;
1782 }
1783 }
1784
1785 return -1;
1786}
1787
1788/**********************************************************************/
1793{
1795}
1796
1797/**********************************************************************/
1801const char *city_style_rule_name(const int style)
1802{
1804}
1805
1806/* Cache of what city production caravan shields are allowed to help. */
1809
1810/**********************************************************************/
1815{
1816 struct requirement prod_as_req;
1817
1818#define log_ca_s_init log_debug
1819
1820 /* Remove old data. */
1823
1824 /* Common for all production kinds. */
1826 prod_as_req.survives = FALSE;
1827 prod_as_req.present = TRUE;
1828
1829 /* Check improvements */
1830 prod_as_req.source.kind = VUT_IMPROVEMENT;
1831
1833 /* Check this improvement. */
1834 prod_as_req.source.value.building = itype;
1835
1838 enabler) {
1840 &(enabler->target_reqs))
1841 && !req_vec_wants_type(&(enabler->target_reqs), VUT_UTYPE)
1842 && !req_vec_wants_type(&(enabler->target_reqs), VUT_UCLASS)
1843 && !req_vec_wants_type(&(enabler->target_reqs), VUT_UTFLAG)
1844 && !req_vec_wants_type(&(enabler->target_reqs), VUT_UCFLAG)) {
1845 /* This improvement kind can receive caravan shields. */
1846
1848
1849 /* Move on to the next improvement */
1850 break;
1851 }
1853
1854 log_ca_s_init("Help Wonder: %s for %s",
1856 ? "possible" : "impossible"),
1859
1860 /* Check units. */
1861 prod_as_req.source.kind = VUT_UTYPE;
1862
1864 /* Check this utype. */
1865 prod_as_req.source.value.utype = putype;
1866
1869 enabler) {
1871 &(enabler->target_reqs))
1872 && !req_vec_wants_type(&(enabler->target_reqs), VUT_IMPROVEMENT)
1873 && !req_vec_wants_type(&(enabler->target_reqs), VUT_IMPR_GENUS)) {
1874 /* This unit type kind can receive caravan shields. */
1875
1877
1878 /* Move on to the next unit type */
1879 break;
1880 }
1882
1883 log_ca_s_init("Help Wonder: %s for %s",
1885 ? "possible" : "impossible"),
1888
1889#undef log_ca_s_init
1890}
1891
1892/**********************************************************************/
1897{
1898 switch (tgt->kind) {
1899 case VUT_IMPROVEMENT:
1902 case VUT_UTYPE:
1904 utype_index(tgt->value.utype));
1905 default:
1907 return FALSE;
1908 };
1909}
1910
1911/**********************************************************************/
1923 const struct universal *target)
1924{
1929
1930 switch (pcity->changed_from.kind) {
1931 case VUT_IMPROVEMENT:
1932 if (is_wonder(pcity->changed_from.value.building)) {
1934 } else {
1936 }
1937 break;
1938 case VUT_UTYPE:
1940 break;
1941 default:
1943 break;
1944 };
1945
1946 switch (target->kind) {
1947 case VUT_IMPROVEMENT:
1948 if (is_wonder(target->value.building)) {
1950 } else {
1952 }
1953 break;
1954 case VUT_UTYPE:
1956 break;
1957 default:
1959 break;
1960 };
1961
1962 /* Changing production is penalized under certain circumstances. */
1963 if (orig_class == new_class
1964 || orig_class == PCT_LAST) {
1965 /* There's never a penalty for building something of the same class. */
1966 unpenalized_shields = pcity->before_change_shields;
1967 } else if (city_built_last_turn(pcity)) {
1968 /* Surplus shields from the previous production won't be penalized if
1969 * you change production on the very next turn. But you can only use
1970 * up to the city's surplus amount of shields in this way. */
1971 unpenalized_shields = MIN(pcity->last_turns_shield_surplus,
1972 pcity->before_change_shields);
1973 penalized_shields = pcity->before_change_shields - unpenalized_shields;
1974 } else {
1975 /* Penalize 50% of the production. */
1976 penalized_shields = pcity->before_change_shields;
1977 }
1978
1979 /* Do not put penalty on these. It shouldn't matter whether you disband unit
1980 before or after changing production...*/
1981 unpenalized_shields += pcity->disbanded_shields;
1982
1983 /* Caravan shields are penalized (just as if you disbanded the caravan)
1984 * if you're not building a wonder. */
1986 unpenalized_shields += pcity->caravan_shields;
1987 } else {
1988 penalized_shields += pcity->caravan_shields;
1989 }
1990
1993
1995}
1996
1997/**********************************************************************/
2002 const struct universal *target,
2004{
2005 int city_shield_surplus = pcity->surplus[O_SHIELD];
2008 int cost = universal_build_shield_cost(pcity, target);
2009
2010 if (target->kind == VUT_IMPROVEMENT
2011 && is_great_wonder(target->value.building)
2013 return FC_INFINITY;
2014 }
2015
2017 return 1;
2018 } else if (city_shield_surplus > 0) {
2019 return (cost - city_shield_stock - 1) / city_shield_surplus + 1;
2020 } else {
2021 return FC_INFINITY;
2022 }
2023}
2024
2025/**********************************************************************/
2033{
2034 if (pcity->surplus[O_FOOD] > 0) {
2035 return (city_granary_size(city_size_get(pcity)) - pcity->food_stock +
2036 pcity->surplus[O_FOOD] - 1) / pcity->surplus[O_FOOD];
2037 } else if (pcity->surplus[O_FOOD] < 0) {
2038 /* Turns before famine loss */
2039 return -1 + (pcity->food_stock / pcity->surplus[O_FOOD]);
2040 } else {
2041 return FC_INFINITY;
2042 }
2043}
2044
2045/**********************************************************************/
2048bool city_can_grow_to(const struct city *pcity, int pop_size)
2049{
2052}
2053
2054/**********************************************************************/
2057struct city *tile_enemy_city(const struct tile *ptile,
2058 const struct player *pplayer)
2059{
2060 struct city *pcity = tile_city(ptile);
2061
2062 if (pcity != NULL && pplayers_at_war(pplayer, city_owner(pcity))) {
2063 return pcity;
2064 }
2065
2066 return NULL;
2067}
2068
2069/**********************************************************************/
2072struct city *tile_allied_city(const struct tile *ptile,
2073 const struct player *pplayer)
2074{
2075 struct city *pcity = tile_city(ptile);
2076
2077 if (pcity != NULL && pplayers_allied(pplayer, city_owner(pcity))) {
2078 return pcity;
2079 }
2080
2081 return NULL;
2082}
2083
2084/**********************************************************************/
2087struct city *tile_non_attack_city(const struct tile *ptile,
2088 const struct player *pplayer)
2089{
2090 struct city *pcity = tile_city(ptile);
2091
2092 if (pcity != NULL && pplayers_non_attack(pplayer, city_owner(pcity))) {
2093 return pcity;
2094 }
2095
2096 return NULL;
2097}
2098
2099/**********************************************************************/
2102struct city *tile_non_allied_city(const struct tile *ptile,
2103 const struct player *pplayer)
2104{
2105 struct city *pcity = tile_city(ptile);
2106
2107 if (pcity != NULL && !pplayers_allied(pplayer, city_owner(pcity))) {
2108 return pcity;
2109 }
2110
2111 return NULL;
2112}
2113
2114/**********************************************************************/
2118 const struct unit *punit,
2119 int distance)
2120{
2122 distance);
2123}
2124
2125/**********************************************************************/
2129 const struct player *owner,
2130 const struct tile *ptile,
2131 int distance)
2132{
2133 square_iterate(nmap, ptile, distance, ptile1) {
2134 struct city *pcity = tile_city(ptile1);
2135
2137 return TRUE;
2138 }
2140
2141 return FALSE;
2142}
2143
2144/**********************************************************************/
2149 const struct tile *ptile,
2150 bool may_be_on_center)
2151{
2153 if (may_be_on_center || !same_pos(ptile, ptile1)) {
2154 if (tile_city(ptile1)) {
2155 return TRUE;
2156 }
2157 }
2159
2160 return FALSE;
2161}
2162
2163/**********************************************************************/
2168int city_granary_size(int city_size)
2169{
2172 int base_value;
2173
2174 /* If the city has no citizens, there is no granary. */
2175 if (city_size == 0) {
2176 return 0;
2177 }
2178
2179 /* Granary sizes for the first food_inis citizens are given directly.
2180 * After that we increase the granary size by food_inc per citizen. */
2181 if (city_size > food_inis) {
2182 base_value = game.info.granary_food_ini[food_inis - 1];
2183 base_value += food_inc * (city_size - food_inis);
2184 } else {
2185 base_value = game.info.granary_food_ini[city_size - 1];
2186 }
2187
2188 return MAX(base_value * game.info.foodbox / 100, 1);
2189}
2190
2191/**********************************************************************/
2196static int player_base_citizen_happiness(const struct player *pplayer)
2197{
2198 int cities = city_list_size(pplayer->cities);
2199 int content = get_player_bonus(pplayer, EFT_CITY_UNHAPPY_SIZE);
2202
2203 if (basis + step <= 0) {
2204 /* Value of zero means effect is inactive */
2205 return content;
2206 }
2207
2208 if (cities > basis) {
2209 content--;
2210 if (step != 0) {
2211 /* the first penalty is at (basis + 1) cities;
2212 the next is at (basis + step + 1), _not_ (basis + step) */
2213 content -= (cities - basis - 1) / step;
2214 }
2215 }
2216 return content;
2217}
2218
2219/**********************************************************************/
2223{
2224 int content = player_base_citizen_happiness(pplayer);
2225
2226 return CLIP(0, content, MAX_CITY_SIZE);
2227}
2228
2229/**********************************************************************/
2233{
2234 if (!game.info.angrycitizen) {
2235 return 0;
2236 } else {
2237 /* Create angry citizens only if we have a negative number of possible
2238 * content citizens. This can happen when empires grow really big. */
2239 int content = player_base_citizen_happiness(pplayer);
2240
2241 return CLIP(0, -content, MAX_CITY_SIZE);
2242 }
2243}
2244
2245/**********************************************************************/
2249{
2250 struct output_type *output = &output_types[otype];
2251 int bonus1 = 100 + get_city_tile_output_bonus(pcity, NULL, output,
2253 int bonus2 = 100 + get_city_tile_output_bonus(pcity, NULL, output,
2255
2256 return MAX(bonus1 * bonus2 / 100, 0);
2257}
2258
2259/**********************************************************************/
2264{
2265 int tithes_bonus = 0;
2266
2268 return 0;
2269 }
2270
2273
2274 return tithes_bonus;
2275}
2276
2277/**********************************************************************/
2281void add_tax_income(const struct player *pplayer, int trade, int *output)
2282{
2283 const int SCIENCE = 0, TAX = 1, LUXURY = 2;
2284 unsigned rates[3];
2285 int result[3];
2286
2287 if (game.info.changable_tax) {
2288 rates[SCIENCE] = pplayer->economic.science;
2289 rates[LUXURY] = pplayer->economic.luxury;
2290 rates[TAX] = 100 - rates[SCIENCE] - rates[LUXURY];
2291 } else {
2295 }
2296
2297 /* ANARCHY */
2299 rates[SCIENCE] = 0;
2300 rates[LUXURY] = 100;
2301 rates[TAX] = 0;
2302 }
2303
2304 distribute(trade, 3, rates, result);
2305
2306 output[O_SCIENCE] += result[SCIENCE];
2307 output[O_GOLD] += result[TAX];
2308 output[O_LUXURY] += result[LUXURY];
2309}
2310
2311/**********************************************************************/
2316{
2317 return pcity->turn_last_built + 1 >= game.info.turn;
2318}
2319
2320/**********************************************************************/
2323static const struct city *nearest_gov_center(const struct city *pcity,
2324 int *min_dist)
2325{
2326 const struct city *gov_center = NULL;
2327
2329
2330 /* Check the special case that city itself is gov center
2331 * before expensive iteration through all cities. */
2332 if (is_gov_center(pcity)) {
2333 *min_dist = 0;
2334 return pcity;
2335 } else {
2337 /* Do not recheck current city */
2338 if (gc != pcity && is_gov_center(gc)) {
2339 int dist = real_map_distance(gc->tile, pcity->tile);
2340
2341 if (dist < *min_dist) {
2342 gov_center = gc;
2343 *min_dist = dist;
2344 }
2345 }
2347 }
2348
2349 return gov_center;
2350}
2351
2352/**********************************************************************/
2360static inline void get_worked_tile_output(const struct civ_map *nmap,
2361 const struct city *pcity,
2362 int *output, bool *workers_map)
2363{
2364 bool is_worked;
2365#ifdef CITY_DEBUGGING
2367#endif
2368 struct tile *pcenter = city_tile(pcity);
2369
2370 memset(output, 0, O_LAST * sizeof(*output));
2371
2374 if (workers_map == NULL) {
2375 struct city *pwork = tile_worked(ptile);
2376
2377 is_worked = (NULL != pwork && pwork == pcity);
2378 } else {
2379 is_worked = workers_map[city_tile_index];
2380 }
2381
2382 if (is_worked) {
2384#ifdef CITY_DEBUGGING
2385 /* This assertion never fails, but it's so slow that we disable
2386 * it by default. */
2388 == city_tile_output(pcity, ptile, is_celebrating, o));
2389#endif /* CITY_DEBUGGING */
2392 }
2394}
2395
2396/**********************************************************************/
2400void add_specialist_output(const struct city *pcity, int *output)
2401{
2403 int count = pcity->specialists[sp];
2404
2407
2408 output[stat_index] += count * amount;
2411}
2412
2413/**********************************************************************/
2422static inline void set_city_bonuses(struct city *pcity)
2423{
2426 pcity->abs_bonus[o] = get_city_output_bonus(pcity,
2427 &output_types[o],
2429 /* Total bonus cannot be negative, as that could lead to unresolvable
2430 * negative balance. */
2431 pcity->abs_bonus[o] = MIN(pcity->abs_bonus[o], 0);
2433}
2434
2435/**********************************************************************/
2445static inline void city_tile_cache_update(const struct civ_map *nmap,
2446 struct city *pcity)
2447{
2449 int radius_sq = city_map_radius_sq_get(pcity);
2450
2451 /* Initialize tile_cache if needed */
2452 if (pcity->tile_cache == NULL || pcity->tile_cache_radius_sq == -1
2453 || pcity->tile_cache_radius_sq != radius_sq) {
2454 pcity->tile_cache = fc_realloc(pcity->tile_cache,
2455 city_map_tiles(radius_sq)
2456 * sizeof(*(pcity->tile_cache)));
2457 pcity->tile_cache_radius_sq = radius_sq;
2458 }
2459
2460 /* Any unreal tiles are skipped - these values should have been memset
2461 * to 0 when the city was created. */
2462 city_tile_iterate_index(nmap, radius_sq, pcity->tile, ptile, city_tile_index) {
2464 (pcity->tile_cache[city_tile_index]).output[o]
2468}
2469
2470/**********************************************************************/
2474static inline int city_tile_cache_get_output(const struct city *pcity,
2475 int city_tile_index,
2476 enum output_type_id o)
2477{
2478 fc_assert_ret_val(pcity->tile_cache_radius_sq
2481
2482 return (pcity->tile_cache[city_tile_index]).output[o];
2483}
2484
2485/**********************************************************************/
2488static void set_surpluses(struct city *pcity)
2489{
2491 int surplus = pcity->prod[o] - pcity->usage[o];
2492
2493 /* Add 'surplus' waste to 'usage'. */
2494 if (surplus > 0) {
2495 struct output_type *output = get_output_type(o);
2499
2500 if (waste_by_rel_dist > 0) {
2501 int min_dist;
2502 const struct city *gov_center = nearest_gov_center(pcity, &min_dist);
2503
2504 if (gov_center == NULL) {
2505 /* No gov center - no income */
2506 waste_level = 100;
2507 } else {
2508 waste_level += waste_by_rel_dist * 50 * min_dist / 100
2510 }
2511 }
2512
2513 if (waste_level > 0) {
2514 if (waste_level < 100) {
2515 pcity->usage[o] += (surplus * waste_level / 100);
2516 } else {
2517 pcity->usage[o] = pcity->prod[o];
2518 }
2519 }
2520 }
2521
2522 pcity->surplus[o] = pcity->prod[o] - pcity->usage[o];
2524}
2525
2526/**********************************************************************/
2529static void happy_copy(struct city *pcity, enum citizen_feeling i)
2530{
2531 int c = 0;
2532
2533 for (; c < CITIZEN_LAST; c++) {
2534 pcity->feel[c][i] = pcity->feel[c][i - 1];
2535 }
2536}
2537
2538/**********************************************************************/
2541static void citizen_base_mood(struct city *pcity)
2542{
2543 struct player *pplayer = city_owner(pcity);
2544 citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_BASE];
2545 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_BASE];
2546 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_BASE];
2547 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_BASE];
2550
2551 /* This is the number of citizens that may start out content, depending
2552 * on empire size and game's city unhappysize. This may be bigger than
2553 * the size of the city, since this is a potential. */
2555 /* Similarly, this is the potential number of angry citizens. */
2557
2558 /* Create content citizens. Take specialists from their ranks. */
2559 *content = MAX(0, MIN(size, base_content) - spes);
2560
2561 /* Create angry citizens. Specialists never become angry. */
2562 fc_assert_action(base_content == 0 || base_angry == 0, *content = 0);
2563 *angry = MIN(base_angry, size - spes);
2564
2565 /* Create unhappy citizens. In the beginning, all who are not content,
2566 * specialists or angry are unhappy. This is changed by luxuries and
2567 * buildings later. */
2568 *unhappy = (size - spes - *content - *angry);
2569
2570 /* No one is born happy. */
2571 *happy = 0;
2572}
2573
2574/**********************************************************************/
2580static inline void citizen_luxury_happy(struct city *pcity, int *luxuries)
2581{
2582 citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_LUXURY];
2583 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_LUXURY];
2584 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_LUXURY];
2585 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_LUXURY];
2586
2587 while (*luxuries >= game.info.happy_cost && *angry > 0) {
2588 /* Upgrade angry to unhappy: costs HAPPY_COST each. */
2589 (*angry)--;
2590 (*unhappy)++;
2592 }
2593 while (*luxuries >= game.info.happy_cost && *content > 0) {
2594 /* Upgrade content to happy: costs HAPPY_COST each. */
2595 (*content)--;
2596 (*happy)++;
2598 }
2599 while (*luxuries >= 2 * game.info.happy_cost && *unhappy > 0) {
2600 /* Upgrade unhappy to happy. Note this is a 2-level upgrade with
2601 * double the cost. */
2602 (*unhappy)--;
2603 (*happy)++;
2604 *luxuries -= 2 * game.info.happy_cost;
2605 }
2606 if (*luxuries >= game.info.happy_cost && *unhappy > 0) {
2607 /* Upgrade unhappy to content: costs HAPPY_COST each. */
2608 (*unhappy)--;
2609 (*content)++;
2611 }
2612}
2613
2614/**********************************************************************/
2617static inline void citizen_happy_luxury(struct city *pcity)
2618{
2619 int x = pcity->prod[O_LUXURY];
2620
2622}
2623
2624/**********************************************************************/
2627static inline void citizen_content_buildings(struct city *pcity)
2628{
2629 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_EFFECT];
2630 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_EFFECT];
2631 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_EFFECT];
2633
2634 /* make people content (but not happy):
2635 get rid of angry first, then make unhappy content. */
2636 while (faces > 0 && *angry > 0) {
2637 (*angry)--;
2638 (*unhappy)++;
2639 faces--;
2640 }
2641 while (faces > 0 && *unhappy > 0) {
2642 (*unhappy)--;
2643 (*content)++;
2644 faces--;
2645 }
2646}
2647
2648/**********************************************************************/
2651static inline void citizen_happiness_nationality(struct city *pcity)
2652{
2656
2659
2660 if (pct > 0) {
2661 int enemies = 0;
2662 int unhappy_inc;
2663 struct player *owner = city_owner(pcity);
2664
2665 citizens_foreign_iterate(pcity, pslot, nationality) {
2667 enemies += nationality;
2668 }
2670
2671 unhappy_inc = enemies * pct / 100;
2672
2673 /* First make content => unhappy, then happy => unhappy,
2674 * then happy => content. No-one becomes angry. */
2675 while (unhappy_inc > 0 && *content > 0) {
2676 (*content)--;
2677 (*unhappy)++;
2678 unhappy_inc--;
2679 }
2680 while (unhappy_inc > 1 && *happy > 0) {
2681 (*happy)--;
2682 (*unhappy)++;
2683 unhappy_inc -= 2;
2684 }
2685 while (unhappy_inc > 0 && *happy > 0) {
2686 (*happy)--;
2687 (*content)++;
2688 unhappy_inc--;
2689 }
2690 }
2691 }
2692}
2693
2694/**********************************************************************/
2700static inline void citizen_happy_units(struct city *pcity)
2701{
2702 citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_MARTIAL];
2703 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_MARTIAL];
2704 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_MARTIAL];
2705 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_MARTIAL];
2706 citizens amt = pcity->martial_law;
2707
2708 /* Pacify discontent citizens through martial law. First convert
2709 * angry => unhappy, then unhappy => content. */
2710 while (amt > 0 && *angry > 0) {
2711 (*angry)--;
2712 (*unhappy)++;
2713 amt--;
2714 }
2715 while (amt > 0 && *unhappy > 0) {
2716 (*unhappy)--;
2717 (*content)++;
2718 amt--;
2719 }
2720
2721 /* Now make citizens unhappier because of military units away from home.
2722 * First make content => unhappy, then happy => unhappy,
2723 * then happy => content. */
2724 amt = pcity->unit_happy_upkeep;
2725 while (amt > 0 && *content > 0) {
2726 (*content)--;
2727 (*unhappy)++;
2728 amt--;
2729 }
2730 while (amt > 1 && *happy > 0) {
2731 (*happy)--;
2732 (*unhappy)++;
2733 amt -= 2;
2734 }
2735 while (amt > 0 && *happy > 0) {
2736 (*happy)--;
2737 (*content)++;
2738 amt--;
2739 }
2740 /* Any remaining unhappiness is lost since angry citizens aren't created
2741 * here. */
2742 /* FIXME: Why not? - Per */
2743}
2744
2745/**********************************************************************/
2748static inline void citizen_happy_wonders(struct city *pcity)
2749{
2750 citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_FINAL];
2751 citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_FINAL];
2752 citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL];
2753 citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_FINAL];
2754 int bonus = get_city_bonus(pcity, EFT_MAKE_HAPPY);
2755
2756 /* First create happy citizens from content, then from unhappy
2757 * citizens; we cannot help angry citizens here. */
2758 while (bonus > 0 && *content > 0) {
2759 (*content)--;
2760 (*happy)++;
2761 bonus--;
2762 }
2763 while (bonus > 1 && *unhappy > 0) {
2764 (*unhappy)--;
2765 (*happy)++;
2766 bonus -= 2;
2767 }
2768 /* The rest falls through and lets unhappy people become content. */
2769
2771 *content += *unhappy + *angry;
2772 *unhappy = 0;
2773 *angry = 0;
2774 return;
2775 }
2776
2778
2779 /* get rid of angry first, then make unhappy content */
2780 while (bonus > 0 && *angry > 0) {
2781 (*angry)--;
2782 (*unhappy)++;
2783 bonus--;
2784 }
2785 while (bonus > 0 && *unhappy > 0) {
2786 (*unhappy)--;
2787 (*content)++;
2788 bonus--;
2789 }
2790}
2791
2792/**********************************************************************/
2796static inline void unhappy_city_check(struct city *pcity)
2797{
2798 if (city_unhappy(pcity)) {
2800 switch (output_types[o].unhappy_penalty) {
2802 pcity->unhappy_penalty[o] = 0;
2803 break;
2805 pcity->unhappy_penalty[o] = MAX(pcity->prod[o] - pcity->usage[o], 0);
2806 break;
2808 pcity->unhappy_penalty[o] = pcity->prod[o];
2809 break;
2810 }
2811
2812 pcity->prod[o] -= pcity->unhappy_penalty[o];
2814 } else {
2815 memset(pcity->unhappy_penalty, 0,
2816 O_LAST * sizeof(*pcity->unhappy_penalty));
2817 }
2818}
2819
2820/**********************************************************************/
2824 int *pollu_prod, int *pollu_pop, int *pollu_mod)
2825{
2826 int prod, pop, mod;
2827
2828 /* Add one one pollution per shield, multipled by the bonus. */
2830 prod = shield_total * MAX(prod, 0) / 100;
2831
2832 /* Add one pollution per citizen for baseline combined bonus (100%). */
2835 / 100;
2836 pop = (city_size_get(pcity) * MAX(pop, 0)) / 100;
2837
2838 /* Then there is base pollution (usually a negative number). */
2839 mod = game.info.base_pollution;
2840
2841 if (pollu_prod) {
2842 *pollu_prod = prod;
2843 }
2844 if (pollu_pop) {
2845 *pollu_pop = pop;
2846 }
2847 if (pollu_mod) {
2848 *pollu_mod = mod;
2849 }
2850 return MAX(prod + pop + mod, 0);
2851}
2852
2853/**********************************************************************/
2857int city_pollution(const struct city *pcity, int shield_total)
2858{
2860}
2861
2862/**********************************************************************/
2869static int get_trade_illness(const struct city *pcity)
2870{
2871 float illness_trade = 0.0;
2872
2874 if (trade_city->turn_plague != -1
2875 && game.info.turn - trade_city->turn_plague < 5) {
2876 illness_trade += (float)game.info.illness_trade_infection
2877 * sqrt(1.0 * city_size_get(pcity)
2878 * city_size_get(trade_city)) / 100.0;
2879 }
2881
2882 return (int)illness_trade;
2883}
2884
2885/**********************************************************************/
2889static int get_city_health(const struct city *pcity)
2890{
2892}
2893
2894/**********************************************************************/
2906int city_illness_calc(const struct city *pcity, int *ill_base,
2907 int *ill_size, int *ill_trade, int *ill_pollution)
2908{
2909 int illness_size = 0, illness_trade = 0, illness_pollution = 0;
2911
2912 if (game.info.illness_on
2914 /* offset the city size by game.info.illness_min_size */
2916
2917 illness_size = (int)((1.0 - exp(- (float)use_size / 10.0))
2918 * 10.0 * game.info.illness_base_factor);
2919 if (is_server()) {
2920 /* On the server we recalculate the illness due to trade as we have
2921 * all the information */
2922 illness_trade = get_trade_illness(pcity);
2923 } else {
2924 /* On the client we have to rely on the value saved within the city
2925 * struct */
2926 illness_trade = pcity->illness_trade;
2927 }
2928
2929 illness_pollution = pcity->pollution
2931 }
2932
2933 illness_base = illness_size + illness_trade + illness_pollution;
2935
2936 /* returning other data */
2937 if (ill_size) {
2939 }
2940
2941 if (ill_trade) {
2942 *ill_trade = illness_trade;
2943 }
2944
2945 if (ill_pollution) {
2947 }
2948
2949 if (ill_base) {
2951 }
2952
2953 return CLIP(0, illness_base * illness_percent / 100 , 999);
2954}
2955
2956/**********************************************************************/
2960{
2961 /* Correctly handles special case turn_plague == -1 (never) */
2962 return (pcity->turn_plague == game.info.turn);
2963}
2964
2965/**********************************************************************/
2968int city_build_slots(const struct city *pcity)
2969{
2971}
2972
2973/**********************************************************************/
2978int city_airlift_max(const struct city *pcity)
2979{
2981}
2982
2983/**********************************************************************/
2989inline void set_city_production(struct city *pcity)
2990{
2991 int trade;
2992
2993 /* Calculate city production!
2994 *
2995 * This is a rather complicated process if we allow rules to become
2996 * more generalized. We can assume that there are no recursive dependency
2997 * loops, but there are some dependencies that do not follow strict
2998 * ordering. For instance corruption must be calculated before
2999 * trade taxes can be counted up, which must occur before the science bonus
3000 * is added on. But the calculation of corruption must include the
3001 * trade bonus. To do this without excessive special casing means that in
3002 * this case the bonuses are multiplied on twice (but only saved the second
3003 * time).
3004 */
3005
3007 pcity->prod[o] = pcity->citizen_base[o];
3009
3010 /* Add on special extra incomes: trade routes and tithes. */
3012 struct city *tcity = game_city_by_number(proute->partner);
3013 bool can_trade;
3014
3015 /* Partner city may have not yet been sent to the client, or
3016 * there's just a placeholder city with a placeholder owner
3017 * created for some tile->worked. */
3018 if (!is_server()
3019 && (tcity == NULL
3020 || city_owner(tcity)->slot == NULL)) {
3021 continue;
3022 }
3023
3024 fc_assert_action(tcity != NULL, continue);
3025
3027
3028 if (!can_trade) {
3031
3032 if (settings->cancelling == TRI_ACTIVE) {
3033 can_trade = TRUE;
3034 }
3035 }
3036
3037 if (can_trade) {
3038 int value;
3039
3040 value =
3042 proute->value = trade_from_route(pcity, proute, value);
3043 pcity->prod[O_TRADE] += proute->value
3044 * (100 + get_city_bonus(pcity, EFT_TRADE_ROUTE_PCT)) / 100;
3045 } else {
3046 proute->value = 0;
3047 }
3050
3051 /* Account for waste. Note that waste is calculated before tax income is
3052 * calculated, so if you had "science waste" it would not include taxed
3053 * science. However waste is calculated after the bonuses are multiplied
3054 * on, so shield waste will include shield bonuses. */
3056 int prod = pcity->prod[o] * pcity->bonus[o] / 100 + pcity->abs_bonus[o];
3057
3058 prod = MAX(prod, 0);
3059 pcity->waste[o] = city_waste(pcity, o, prod, NULL);
3061
3062 /* Convert trade into science/luxury/gold, and add this on to whatever
3063 * science/luxury/gold is already there. */
3064 trade = pcity->prod[O_TRADE] * pcity->bonus[O_TRADE] / 100
3065 + pcity->abs_bonus[O_TRADE];
3066 trade = MAX(trade, 0);
3068 trade - pcity->waste[O_TRADE] - pcity->usage[O_TRADE],
3069 pcity->prod);
3070
3071 /* Add on effect bonuses and waste. Note that the waste calculation
3072 * (above) already includes the bonuses. */
3074 int prod = pcity->prod[o] * pcity->bonus[o] / 100 + pcity->abs_bonus[o];
3075
3076 prod = MAX(prod, 0);
3077 pcity->prod[o] = prod;
3078 pcity->prod[o] -= pcity->waste[o];
3080}
3081
3082/**********************************************************************/
3086 struct unit *punit, int *free_unhappy)
3087{
3088 struct city *pcity;
3089 const struct unit_type *ut;
3090 struct player *plr;
3091 int happy_cost;
3092
3093 if (punit == NULL || free_unhappy == NULL) {
3094 return 0;
3095 }
3096
3098 if (pcity == NULL) {
3099 return 0;
3100 }
3101
3103 plr = unit_owner(punit);
3104 happy_cost = utype_happy_cost(ut, plr);
3105
3106 if (happy_cost <= 0) {
3107 return 0;
3108 }
3109
3111
3113 return 0;
3114 }
3115
3117 if (happy_cost <= 0) {
3118 return 0;
3119 }
3120
3121 if (*free_unhappy >= happy_cost) {
3122 *free_unhappy -= happy_cost;
3123 return 0;
3124 } else {
3125 happy_cost -= *free_unhappy;
3126 *free_unhappy = 0;
3127 }
3128
3129 return happy_cost;
3130}
3131
3132/**********************************************************************/
3136static inline void city_support(const struct civ_map *nmap,
3137 struct city *pcity)
3138{
3139 int free_unhappy;
3140 int max_mart_units;
3141
3142 /* Clear all usage values. */
3143 memset(pcity->usage, 0, O_LAST * sizeof(*pcity->usage));
3144 pcity->martial_law = 0;
3145 pcity->unit_happy_upkeep = 0;
3146
3147 /* Building and unit gold upkeep depends on the setting
3148 * 'game.info.gold_upkeep_style':
3149 * GOLD_UPKEEP_CITY: The upkeep for buildings and units is paid by the
3150 * city.
3151 * GOLD_UPKEEP_MIXED: The upkeep for buildings is paid by the city.
3152 * The upkeep for units is paid by the nation.
3153 * GOLD_UPKEEP_NATION: The upkeep for buildings and units is paid by the
3154 * nation. */
3156 "Invalid gold_upkeep_style %d", game.info.gold_upkeep_style);
3157 switch (game.info.gold_upkeep_style) {
3158 case GOLD_UPKEEP_CITY:
3160 fc__fallthrough; /* No break */
3161 case GOLD_UPKEEP_MIXED:
3163 break;
3164 case GOLD_UPKEEP_NATION:
3165 /* nothing */
3166 break;
3167 }
3168 /* Food consumption by citizens. */
3170
3172 if (max_mart_units > 0) {
3174 int sel_count = 0;
3175 int i;
3176
3179 && unit_owner(punit) == city_owner(pcity)) {
3180 int current = get_target_bonus_effects(NULL,
3181 &(const struct req_context) {
3182 .player = city_owner(pcity),
3183 .city = pcity,
3184 .tile = city_tile(pcity),
3185 .unit = punit,
3186 .unittype = unit_type_get(punit)
3187 },
3189 if (current > 0) {
3190 if (sel_count < max_mart_units) {
3191 best_units[sel_count++] = current;
3192 } else if (current > best_units[max_mart_units - 1]) {
3193 for (i = max_mart_units - 1; i >= 0 && current > best_units[i]; i--) {
3194 if (i + 1 < max_mart_units) {
3195 best_units[i + 1] = best_units[i];
3196 }
3197 }
3198
3199 best_units[i + 1] = current;
3200 }
3201 }
3202 }
3204
3205 pcity->martial_law = 0;
3206 for (i = 0; i < sel_count; i++) {
3207 pcity->martial_law += best_units[i];
3208 }
3209
3210 pcity->martial_law = MIN(pcity->martial_law, MAX_CITY_SIZE);
3211 }
3212
3214 unit_list_iterate(pcity->units_supported, punit) {
3215 pcity->unit_happy_upkeep += city_unit_unhappiness(nmap, punit, &free_unhappy);
3217 if (O_GOLD != o) {
3218 /* O_GOLD is handled with "game.info.gold_upkeep_style", see over. */
3219 pcity->usage[o] += punit->upkeep[o];
3220 }
3223}
3224
3225/**********************************************************************/
3238 struct city *pcity, bool *workers_map)
3239{
3240 if (workers_map == NULL) {
3241 /* do a full refresh */
3242
3243 /* Calculate the bonus[] array values. */
3245 /* Calculate the tile_cache[] values. */
3247 /* Manage settlers, and units */
3249 }
3250
3251 /* Calculate output from citizens (uses city_tile_cache_get_output()). */
3252 get_worked_tile_output(nmap, pcity, pcity->citizen_base, workers_map);
3253 add_specialist_output(pcity, pcity->citizen_base);
3254
3257 /* Note that pollution is calculated before unhappy_city_check() makes
3258 * deductions for disorder; so a city in disorder still causes pollution */
3259 pcity->pollution = city_pollution(pcity, pcity->prod[O_SHIELD]);
3260
3262 citizen_happy_luxury(pcity); /* With our new found luxuries */
3263
3266
3269
3270 /* Martial law & unrest from units */
3273
3274 /* Building (including wonder) happiness effects */
3277
3280}
3281
3282/**********************************************************************/
3289int city_waste(const struct city *pcity, Output_type_id otype, int total,
3290 int *breakdown)
3291{
3292 int penalty_waste = 0;
3293 int penalty_size = 0; /* Separate notradesize/fulltradesize from normal
3294 * corruption */
3295 int total_eft = total; /* Normal corruption calculated on total reduced by
3296 * possible size penalty */
3297 const struct output_type *output = get_output_type(otype);
3299 bool waste_all = FALSE;
3300
3301 if (otype == O_TRADE) {
3302 /* FIXME: special case for trade: it is affected by notradesize and
3303 * fulltradesize server settings.
3304 *
3305 * If notradesize and fulltradesize are equal then the city gets no
3306 * trade at that size. */
3307 int notradesize = MIN(game.info.notradesize, game.info.fulltradesize);
3308 int fulltradesize = MAX(game.info.notradesize, game.info.fulltradesize);
3309
3310 if (city_size_get(pcity) <= notradesize) {
3311 penalty_size = total_eft; /* Then no trade income. */
3312 } else if (city_size_get(pcity) >= fulltradesize) {
3313 penalty_size = 0;
3314 } else {
3315 penalty_size = total_eft * (fulltradesize - city_size_get(pcity))
3316 / (fulltradesize - notradesize);
3317 }
3318 }
3319
3320 /* Apply corruption only to anything left after tradesize */
3322
3323 /* Distance-based waste.
3324 * Don't bother calculating if there's nothing left to lose. */
3325 if (total_eft > 0) {
3330 if (waste_by_dist > 0 || waste_by_rel_dist > 0) {
3331 int min_dist;
3332 const struct city *gov_center = nearest_gov_center(pcity, &min_dist);
3333
3334 if (gov_center == NULL) {
3335 waste_all = TRUE; /* No gov center - no income */
3336 } else {
3338 if (waste_by_rel_dist > 0) {
3339 /* Multiply by 50 as an "standard size" for which EFT_OUTPUT_WASTE_BY_DISTANCE
3340 * and EFT_OUTPUT_WASTE_BY_REL_DISTANCE would give same result. */
3341 waste_level += waste_by_rel_dist * 50 * min_dist / 100
3343 }
3344 }
3345 }
3346 }
3347
3348 if (waste_all) {
3350 } else {
3353
3354 /* Corruption/waste calculated only for the actually produced amount */
3355 if (waste_level > 0) {
3357 }
3358
3359 /* Bonus calculated only for the actually produced amount */
3361
3362 /* Clip */
3364 }
3365
3366 if (breakdown) {
3369 }
3370
3371 /* Add up total penalty */
3372 return penalty_waste + penalty_size;
3373}
3374
3375/**********************************************************************/
3380{
3381 citizens count = 0;
3382
3384 fc_assert_ret_val(MAX_CITY_SIZE - count > pcity->specialists[sp], 0);
3385 count += pcity->specialists[sp];
3387
3388 return count;
3389}
3390
3391/**********************************************************************/
3395{
3396 int count = 0;
3397
3399 count += pcity->specialists[sp];
3401
3402 return count;
3403}
3404
3405/**********************************************************************/
3411 const struct city *pcity)
3412{
3413 int best = DEFAULT_SPECIALIST;
3414 int val = get_specialist_output(pcity, best, otype);
3415
3419
3420 if (val2 > val) {
3421 best = i;
3422 val = val2;
3423 }
3424 }
3426
3427 return best;
3428}
3429
3430/**********************************************************************/
3434 const struct impr_type *pimprove)
3435{
3436 pcity->built[improvement_index(pimprove)].turn = game.info.turn; /*I_ACTIVE*/
3437
3438 if (is_server() && is_wonder(pimprove)) {
3439 /* Client just read the info from the packets. */
3440 wonder_built(pcity, pimprove);
3441 }
3442}
3443
3444/**********************************************************************/
3448 const struct impr_type *pimprove)
3449{
3450 log_debug("Improvement %s removed from city %s",
3451 improvement_rule_name(pimprove), pcity->name);
3452
3453 pcity->built[improvement_index(pimprove)].turn = I_DESTROYED;
3454
3455 if (is_server() && is_wonder(pimprove)) {
3456 /* Client just read the info from the packets. */
3457 wonder_destroyed(pcity, pimprove);
3458 }
3459}
3460
3461/**********************************************************************/
3465{
3466 return BV_ISSET(pcity->city_options, option);
3467}
3468
3469/**********************************************************************/
3473{
3474 int i;
3475
3476 city_styles = fc_calloc(num, sizeof(*city_styles));
3478
3479 for (i = 0; i < game.control.num_city_styles; i++) {
3481 }
3482}
3483
3484/**********************************************************************/
3488{
3489 int i;
3490
3491 for (i = 0; i < game.control.num_city_styles; i++) {
3493 }
3494
3496 city_styles = NULL;
3498}
3499
3500/**********************************************************************/
3506struct city *create_city_virtual(struct player *pplayer,
3507 struct tile *ptile, const char *name)
3508{
3509 int i,len;
3510
3511 /* Make sure that contents of city structure are correctly initialized,
3512 * if you ever allocate it by some other mean than fc_calloc() */
3513 struct city *pcity = fc_calloc(1, sizeof(*pcity));
3514
3515 fc_assert_ret_val(NULL != name, NULL); /* No unnamed cities! */
3516
3517 /* Do this early, so any logging later will have the city name */
3519
3520 pcity->tile = ptile;
3521 fc_assert_ret_val(NULL != pplayer, NULL); /* No unowned cities! */
3522 pcity->owner = pplayer;
3523 pcity->acquire_t = CACQ_FOUNDED;
3524
3525 if (is_server()) {
3526 pcity->original = pplayer;
3527 }
3528
3529 /* City structure was allocated with fc_calloc(), so contents are initially
3530 * zero. There is no need to initialize it a second time. */
3531
3532 /* Now set some useful default values. */
3533 pcity->capital = CAPITAL_NOT;
3534 city_size_set(pcity, 1);
3535 pcity->specialists[DEFAULT_SPECIALIST] = 1;
3536 pcity->wlcb = WLCB_SMART;
3537
3539 pcity->bonus[o] = 100;
3540 pcity->abs_bonus[o] = 0;
3542
3543 pcity->turn_plague = -1; /* -1 = never */
3544 pcity->did_buy = FALSE;
3545 pcity->city_radius_sq = game.info.init_city_radius_sq;
3546 pcity->turn_founded = game.info.turn;
3547 pcity->turn_last_built = game.info.turn;
3548
3549 pcity->tile_cache_radius_sq = -1; /* -1 = tile_cache must be initialised */
3550
3551 /* pcity->ai.act_cache: worker activities on the city map */
3552
3553 /* Initialise improvements list */
3554 for (i = 0; i < ARRAY_SIZE(pcity->built); i++) {
3555 pcity->built[i].turn = I_NEVER;
3556 }
3557
3558 /* Set up the worklist */
3559 worklist_init(&pcity->worklist);
3560
3561 pcity->units_supported = unit_list_new();
3562 pcity->routes = trade_route_list_new();
3563 pcity->task_reqs = worker_task_list_new();
3564
3565 if (is_server()) {
3566 pcity->server.mgr_score_calc_turn = -1; /* -1 = never */
3567
3568 CALL_FUNC_EACH_AI(city_alloc, pcity);
3569 } else {
3570 pcity->client.info_units_supported =
3572 pcity->client.info_units_present =
3574 /* collecting_info_units_supported set by fc_calloc().
3575 * collecting_info_units_present set by fc_calloc(). */
3576 }
3577
3579 pcity->counter_values = fc_malloc(sizeof(int) * len);
3580
3581 for (i = 0; i < len; i++) {
3582 pcity->counter_values[i] = counter_by_index(i, CTGT_CITY)->def;
3583 }
3584
3585 return pcity;
3586}
3587
3588/**********************************************************************/
3593{
3594 CALL_FUNC_EACH_AI(city_free, pcity);
3595
3597
3598 /* Free worker tasks */
3599 while (worker_task_list_size(pcity->task_reqs) > 0) {
3600 struct worker_task *ptask = worker_task_list_get(pcity->task_reqs, 0);
3601
3602 worker_task_list_remove(pcity->task_reqs, ptask);
3603
3604 free(ptask);
3605 }
3606 worker_task_list_destroy(pcity->task_reqs);
3607
3608 /* Free rally points */
3610
3611 unit_list_destroy(pcity->units_supported);
3613 if (pcity->tile_cache != NULL) {
3614 free(pcity->tile_cache);
3615 }
3616
3617 if (pcity->cm_parameter) {
3618 free(pcity->cm_parameter);
3619 }
3620
3621 if (pcity->counter_values) {
3622 free(pcity->counter_values);
3623 }
3624
3625 if (!is_server()) {
3626 unit_list_destroy(pcity->client.info_units_supported);
3627 unit_list_destroy(pcity->client.info_units_present);
3628 /* Handle a rare case where the game is freed in the middle of a
3629 * spy/diplomat investigate cycle. */
3630 if (pcity->client.collecting_info_units_supported != NULL) {
3631 unit_list_destroy(pcity->client.collecting_info_units_supported);
3632 }
3633 if (pcity->client.collecting_info_units_present != NULL) {
3634 unit_list_destroy(pcity->client.collecting_info_units_present);
3635 }
3636 }
3637
3638 free(pcity->name);
3639
3640 memset(pcity, 0, sizeof(*pcity)); /* ensure no pointers remain */
3641 free(pcity);
3642}
3643
3644/**********************************************************************/
3648bool city_exist(int id)
3649{
3650 /* Check if city exist in game */
3651 if (game_city_by_number(id)) {
3652 return TRUE;
3653 }
3654
3655 return FALSE;
3656}
3657
3658/**********************************************************************/
3665bool city_is_virtual(const struct city *pcity)
3666{
3667 if (!pcity) {
3668 return FALSE;
3669 }
3670
3671 return pcity != game_city_by_number(pcity->id);
3672}
3673
3674/**********************************************************************/
3677bool is_free_worked(const struct city *pcity, const struct tile *ptile)
3678{
3679 return is_city_center(pcity, ptile);
3680}
3681
3682/**********************************************************************/
3685void *city_ai_data(const struct city *pcity, const struct ai_type *ai)
3686{
3687 return pcity->server.ais[ai_type_number(ai)];
3688}
3689
3690/**********************************************************************/
3693void city_set_ai_data(struct city *pcity, const struct ai_type *ai,
3694 void *data)
3695{
3696 pcity->server.ais[ai_type_number(ai)] = data;
3697}
3698
3699/**********************************************************************/
3703{
3704 /* Free rally points */
3705 if (pcity->rally_point.length > 0) {
3706 pcity->rally_point.length = 0;
3707 pcity->rally_point.persistent = FALSE;
3708 pcity->rally_point.vigilant = FALSE;
3709 free(pcity->rally_point.orders);
3710 pcity->rally_point.orders = NULL;
3711 }
3712}
3713
3714/**********************************************************************/
3718 struct city *pcity)
3719{
3720 struct unit_order *checked_orders;
3721 const struct civ_map *nmap = &(wld.map);
3722
3723 if (NULL == pcity) {
3724 /* Probably lost. */
3725 log_verbose("handle_city_rally_point() bad city number %d.",
3726 packet->id);
3727 return;
3728 }
3729
3730 if (0 > packet->length || MAX_LEN_ROUTE < packet->length) {
3731 /* Shouldn't happen */
3732 log_error("city_rally_point_receive() invalid packet length %d (max %d)",
3733 packet->length, MAX_LEN_ROUTE);
3734 return;
3735 }
3736
3737 pcity->rally_point.length = packet->length;
3738
3739 if (packet->length == 0) {
3740 pcity->rally_point.vigilant = FALSE;
3741 pcity->rally_point.persistent = FALSE;
3742 if (pcity->rally_point.orders) {
3743 free(pcity->rally_point.orders);
3744 pcity->rally_point.orders = NULL;
3745 }
3746 } else {
3748 packet->orders);
3749 if (!checked_orders) {
3750 pcity->rally_point.length = 0;
3751 log_error("invalid rally point orders for %s.",
3753 return;
3754 }
3755
3756 pcity->rally_point.persistent = packet->persistent;
3757 pcity->rally_point.vigilant = packet->vigilant;
3758 pcity->rally_point.orders = checked_orders;
3759 }
3760}
struct action_enabler_list * action_enablers_for_action(action_id action)
Definition actions.c:1580
#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:1673
bool city_is_virtual(const struct city *pcity)
Definition city.c:3665
int city_turns_to_build(const struct city *pcity, const struct universal *target, bool include_shield_stock)
Definition city.c:2001
static void citizen_luxury_happy(struct city *pcity, int *luxuries)
Definition city.c:2580
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:2627
bool is_free_worked(const struct city *pcity, const struct tile *ptile)
Definition city.c:3677
bool city_production_gets_caravan_shields(const struct universal *tgt)
Definition city.c:1896
static bv_imprs caravan_helped_impr
Definition city.c:1807
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:1181
static int player_base_citizen_happiness(const struct player *pplayer)
Definition city.c:2196
#define POLLUTION_EFT_NEARMAX
Definition city.c:1145
const char * city_name_getx(const struct city *pcity)
Definition city.c:1146
int city_granary_size(int city_size)
Definition city.c:2168
static void get_worked_tile_output(const struct civ_map *nmap, const struct city *pcity, int *output, bool *workers_map)
Definition city.c:2360
citizens player_angry_citizens(const struct player *pplayer)
Definition city.c:2232
int city_pollution_types(const struct city *pcity, int shield_total, int *pollu_prod, int *pollu_pop, int *pollu_mod)
Definition city.c:2823
void city_set_ai_data(struct city *pcity, const struct ai_type *ai, void *data)
Definition city.c:3693
static void citizen_happy_wonders(struct city *pcity)
Definition city.c:2748
bool is_friendly_city_near(const struct civ_map *nmap, const struct player *owner, const struct tile *ptile, int distance)
Definition city.c:2128
bool city_built_last_turn(const struct city *pcity)
Definition city.c:2315
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:3289
static void city_support(const struct civ_map *nmap, struct city *pcity)
Definition city.c:3136
void generate_city_map_indices(void)
Definition city.c:527
int city_build_slots(const struct city *pcity)
Definition city.c:2968
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:1276
const char * city_style_name_translation(const int style)
Definition city.c:1792
void city_styles_free(void)
Definition city.c:3487
static int city_tile_cache_get_output(const struct city *pcity, int city_tile_index, enum output_type_id o)
Definition city.c:2474
Output_type_id output_type_by_identifier(const char *id)
Definition city.c:647
int city_superspecialists(const struct city *pcity)
Definition city.c:3394
bool is_capital(const struct city *pcity)
Definition city.c:1615
void city_styles_alloc(int num)
Definition city.c:3472
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:1437
int city_unit_slots_available(const struct city *pcity)
Definition city.c:1049
void city_production_caravan_shields_init(void)
Definition city.c:1814
void city_remove_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3447
int city_improvement_upkeep(const struct city *pcity, const struct impr_type *b)
Definition city.c:1291
int city_airlift_max(const struct city *pcity)
Definition city.c:2978
bool citymindist_prevents_city_on_tile(const struct civ_map *nmap, const struct tile *ptile)
Definition city.c:1501
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:3464
int city_population(const struct city *pcity)
Definition city.c:1227
static struct iter_index * city_map_index
Definition city.c:60
const char * city_style_rule_name(const int style)
Definition city.c:1801
void city_size_add(struct city *pcity, int add)
Definition city.c:1200
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:1701
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:2281
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:2102
int city_unit_unhappiness(const struct civ_map *nmap, struct unit *punit, int *free_unhappy)
Definition city.c:3085
bool city_got_defense_effect(const struct city *pcity, const struct unit_type *attacker)
Definition city.c:1632
citizens player_content_citizens(const struct player *pplayer)
Definition city.c:2222
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:2445
int get_city_tithes_bonus(const struct city *pcity)
Definition city.c:2263
bool is_unit_near_a_friendly_city(const struct civ_map *nmap, const struct unit *punit, int distance)
Definition city.c:2117
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:1689
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:1662
Specialist_type_id best_specialist(Output_type_id otype, const struct city *pcity)
Definition city.c:3410
struct city * create_city_virtual(struct player *pplayer, struct tile *ptile, const char *name)
Definition city.c:3506
void set_city_production(struct city *pcity)
Definition city.c:2989
struct city * city_list_find_number(struct city_list *This, int id)
Definition city.c:1715
static bv_unit_types caravan_helped_utype
Definition city.c:1808
int get_final_city_output_bonus(const struct city *pcity, Output_type_id otype)
Definition city.c:2248
bool city_celebrating(const struct city *pcity)
Definition city.c:1681
bool city_can_be_built_tile_only(const struct civ_map *nmap, const struct tile *ptile)
Definition city.c:1596
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:2906
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:2048
int city_change_production_penalty(const struct city *pcity, const struct universal *target)
Definition city.c:1922
static int get_city_health(const struct city *pcity)
Definition city.c:2889
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:3237
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:2422
static void set_surpluses(struct city *pcity)
Definition city.c:2488
static const struct city * nearest_gov_center(const struct city *pcity, int *min_dist) fc__attribute((nonnull(1
Definition city.c:2323
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:1523
int city_pollution(const struct city *pcity, int shield_total)
Definition city.c:2857
bool city_happy(const struct city *pcity)
Definition city.c:1650
void city_size_set(struct city *pcity, citizens size)
Definition city.c:1216
void city_add_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3433
int city_tile_output_now(const struct city *pcity, const struct tile *ptile, Output_type_id otype)
Definition city.c:1420
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:2072
struct city * tile_enemy_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:2057
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:3592
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:3379
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:3717
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:1757
void add_specialist_output(const struct city *pcity, int *output)
Definition city.c:2400
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:3685
struct city * city_list_find_name(struct city_list *This, const char *name)
Definition city.c:1731
static void citizen_happiness_nationality(struct city *pcity)
Definition city.c:2651
int city_tile_output(const struct city *pcity, const struct tile *ptile, bool is_celebrating, Output_type_id otype)
Definition city.c:1319
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:1623
#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:2617
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:2796
struct city * tile_non_attack_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:2087
bool city_exist(int id)
Definition city.c:3648
bool city_can_work_tile(const struct city *pcity, const struct tile *ptile)
Definition city.c:1492
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:2032
void city_rally_point_clear(struct city *pcity)
Definition city.c:3702
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:2148
bool city_had_recent_plague(const struct city *pcity)
Definition city.c:2959
static int get_trade_illness(const struct city *pcity)
Definition city.c:2869
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:1258
int city_style_by_rule_name(const char *s)
Definition city.c:1774
static void citizen_base_mood(struct city *pcity)
Definition city.c:2541
int city_name_compare(const void *p1, const void *p2)
Definition city.c:1747
int city_total_impr_gold_upkeep(const struct city *pcity)
Definition city.c:1237
static void citizen_happy_units(struct city *pcity)
Definition city.c:2700
static void happy_copy(struct city *pcity, enum citizen_feeling i)
Definition city.c:2529
const char * city_production_name_translation(const struct city *pcity)
Definition city.c:700
#define city_list_iterate(citylist, pcity)
Definition city.h:508
#define city_tile(_pcity_)
Definition city.h:565
@ UNHAPPY_PENALTY_ALL_PRODUCTION
Definition city.h:254
@ UNHAPPY_PENALTY_NONE
Definition city.h:252
@ UNHAPPY_PENALTY_SURPLUS
Definition city.h:253
#define city_tile_iterate_index(_nmap, _radius_sq, _city_tile, _tile, _index)
Definition city.h:199
#define CITY_MAP_MAX_RADIUS_SQ
Definition city.h:84
static citizens city_size_get(const struct city *pcity)
Definition city.h:570
#define CITY_MAP_MAX_SIZE
Definition city.h:92
#define city_tile_iterate_index_end
Definition city.h:207
@ CITIZEN_LAST
Definition city.h:270
@ CITIZEN_ANGRY
Definition city.h:269
@ CITIZEN_HAPPY
Definition city.h:266
@ CITIZEN_CONTENT
Definition city.h:267
@ CITIZEN_UNHAPPY
Definition city.h:268
#define CITY_MAP_CENTER_RADIUS_SQ
Definition city.h:87
#define CITY_MAP_MIN_RADIUS_SQ
Definition city.h:82
#define output_type_iterate(output)
Definition city.h:847
#define CITY_REL2ABS(_coor)
Definition city.h:113
#define city_owner(_pcity_)
Definition city.h:564
#define MAX_CITY_SIZE
Definition city.h:104
static bool is_city_center(const struct city *pcity, const struct tile *ptile)
Definition city.h:870
#define city_list_iterate_end
Definition city.h:510
#define I_DESTROYED
Definition city.h:246
#define I_NEVER
Definition city.h:245
#define city_tile_iterate(_nmap, _radius_sq, _city_tile, _tile)
Definition city.h:228
citizen_feeling
Definition city.h:276
@ FEELING_EFFECT
Definition city.h:279
@ FEELING_LUXURY
Definition city.h:278
@ FEELING_FINAL
Definition city.h:282
@ FEELING_BASE
Definition city.h:277
@ FEELING_NATIONALITY
Definition city.h:280
@ FEELING_MARTIAL
Definition city.h:281
@ OLOSS_SIZE
Definition city.h:289
@ OLOSS_WASTE
Definition city.h:288
#define city_map_iterate_end
Definition city.h:175
#define city_map_iterate(_radius_sq, _index, _x, _y)
Definition city.h:171
#define city_tile_iterate_end
Definition city.h:236
#define city_built_iterate(_pcity, _p)
Definition city.h:836
#define is_free_worked_index(city_tile_index)
Definition city.h:882
#define city_map_tiles_from_city(_pcity)
Definition city.h:125
#define CITY_ABS2REL(_coor)
Definition city.h:114
#define city_built_iterate_end
Definition city.h:842
#define PCT_LAST
Definition city.h:41
#define output_type_iterate_end
Definition city.h:853
void cm_init_citymap(void)
Definition cm.c:314
char * incite_cost
Definition comments.c:77
#define MAX_LEN_ROUTE
Definition conn_types.h:38
struct counter * counter_by_index(int index, enum counter_target target)
Definition counters.c:186
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:938
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:983
int get_building_bonus(const struct city *pcity, const struct impr_type *building, enum effect_type effect_type)
Definition effects.c:1008
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:913
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:1036
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:249
@ RPT_CERTAIN
Definition fc_types.h:516
@ RPT_POSSIBLE
Definition fc_types.h:515
int Specialist_type_id
Definition fc_types.h:236
@ CTGT_CITY
Definition fc_types.h:128
#define CITY_MAP_MAX_RADIUS
Definition fc_types.h:85
output_type_id
Definition fc_types.h:102
@ O_SHIELD
Definition fc_types.h:103
@ O_FOOD
Definition fc_types.h:103
@ O_TRADE
Definition fc_types.h:103
@ O_SCIENCE
Definition fc_types.h:103
@ O_LUXURY
Definition fc_types.h:103
@ O_GOLD
Definition fc_types.h:103
@ O_LAST
Definition fc_types.h:103
#define MAX_LEN_CITYNAME
Definition fc_types.h:69
enum output_type_id Output_type_id
Definition fc_types.h:239
#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:62
struct world wld
Definition game.c:63
struct city * game_city_by_number(int id)
Definition game.c:107
struct government * government_of_player(const struct player *pplayer)
Definition government.c:116
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:1076
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:1213
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition map.h:388
#define square_iterate_end
Definition map.h:391
#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:120
int len
Definition packhand.c:128
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1376
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1397
bool pplayers_non_attack(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1451
struct player * player_slot_get_player(const struct player_slot *pslot)
Definition player.c:432
bool gives_shared_tiles(const struct player *me, const struct player *them)
Definition player.c:1485
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:1494
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:110
int get_specialist_output(const struct city *pcity, Specialist_type_id sp, Output_type_id otype)
Definition specialist.c:270
#define specialist_type_iterate_end
Definition specialist.h:85
#define specialist_type_iterate(sp)
Definition specialist.h:79
#define normal_specialist_type_iterate(sp)
Definition specialist.h:89
#define super_specialist_type_iterate_end
Definition specialist.h:105
#define super_specialist_type_iterate(sp)
Definition specialist.h:99
#define DEFAULT_SPECIALIST
Definition specialist.h:43
#define normal_specialist_type_iterate_end
Definition specialist.h:95
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:318
int surplus[O_LAST]
Definition city.h:353
bv_city_options city_options
Definition city.h:401
int style
Definition city.h:325
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:108
int dx
Definition city.h:108
int dy
Definition city.h:108
const char * id
Definition city.h:260
struct unit_order orders[MAX_LEN_ROUTE]
int granary_food_ini[MAX_GRANARY_INIS]
enum gold_upkeep_style gold_upkeep_style
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:559
int upkeep[O_LAST]
Definition unittype.h:545
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:595
universals_u value
Definition fc_types.h:594
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:292
int tile_roads_output_incr(const struct tile *ptile, enum output_type_id o)
Definition tile.c:272
enum known_type tile_get_known(const struct tile *ptile, const struct player *pplayer)
Definition tile.c:393
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:536
const struct impr_type * building
Definition fc_types.h:529
struct unit_order * create_unit_orders(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2945
bool unit_being_aggressive(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:1582
bool is_field_unit(const struct unit *punit)
Definition unit.c:457
bool is_martial_law_unit(const struct unit *punit)
Definition unit.c:335
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1793
struct unit * unit_occupies_tile(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1424
#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:2298
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:126
bool can_player_build_unit_direct(const struct player *p, const struct unit_type *punittype, bool consider_reg_impr_req)
Definition unittype.c:1994
int utype_veteran_levels(const struct unit_type *punittype)
Definition unittype.c:2657
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:980
const char * utype_rule_name(const struct unit_type *punittype)
Definition unittype.c:1604
struct unit_type * utype_by_number(const Unit_type_id id)
Definition unittype.c:114
Unit_type_id utype_index(const struct unit_type *punittype)
Definition unittype.c:93
int utype_pop_value(const struct unit_type *punittype, const struct city *pcity)
Definition unittype.c:1554
bool can_player_build_unit_later(const struct player *p, const struct unit_type *punittype)
Definition unittype.c:2125
int utype_build_shield_cost(const struct city *pcity, const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1463
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:1042
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:396
int utype_happy_cost(const struct unit_type *ut, const struct player *pplayer)
Definition unittype.c:206
static bool uclass_has_flag(const struct unit_class *punitclass, enum unit_class_flag_id flag)
Definition unittype.h:773
#define utype_class(_t_)
Definition unittype.h:756
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:624
#define unit_type_iterate(_p)
Definition unittype.h:863
#define unit_type_iterate_end
Definition unittype.h:870
#define U_NOT_OBSOLETED
Definition unittype.h:535
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