Freeciv21
Develop your civilization from humble roots to a global empire
city.cpp
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 
14 #include <array>
15 #include <cmath> // pow, sqrt, exp
16 #include <cstdlib>
17 #include <cstring>
18 #include <vector>
19 
20 // utility
21 #include "distribute.h"
22 #include "fcintl.h"
23 #include "log.h"
24 #include "player.h"
25 #include "support.h"
26 
27 // common
28 #include "ai.h"
29 #include "citizens.h"
30 #include "effects.h"
31 #include "game.h"
32 #include "government.h"
33 #include "improvement.h"
34 #include "map.h"
35 #include "movement.h"
36 #include "packets.h"
37 #include "requirements.h"
38 #include "specialist.h"
39 #include "traderoutes.h"
40 #include "unit.h"
41 #include "workertask.h"
42 
43 // aicore
44 #include "cm.h"
45 
46 #include "city.h"
47 
48 // Define this to add in extra (very slow) assertions for the city code.
49 #undef CITY_DEBUGGING
50 
51 static char *citylog_map_line(int y, int city_radius_sq, int *city_map_data);
52 #ifdef FREECIV_DEBUG
53 // only used for debugging
54 static void citylog_map_index(QtMsgType level);
55 static void citylog_map_radius_sq(QtMsgType level);
56 #endif // FREECIV_DEBUG
57 
58 // Get city tile informations using the city tile index.
59 static struct iter_index *city_map_index = nullptr;
60 /* Get city tile informations using the city tile coordinates. This is an
61  * [x][y] array of integer values corresponding to city_map_index. The
62  * coordinates x and y are in the range [0, CITY_MAP_MAX_SIZE] */
64 
65 // number of tiles of a city; depends on the squared city radius
67 
68 // definitions and functions for the tile_cache
69 struct tile_cache {
70  int output[O_LAST];
71 };
72 
73 static inline void city_tile_cache_update(struct city *pcity);
74 static inline int city_tile_cache_get_output(const struct city *pcity,
75  int city_tile_index,
76  enum output_type_id o);
77 
78 struct citystyle *city_styles = nullptr;
79 
80 /* One day these values may be read in from the ruleset. In the meantime
81  * they're just an easy way to access information about each output type. */
83  {O_FOOD, N_("Food"), "food", true, UNHAPPY_PENALTY_SURPLUS},
84  {O_SHIELD, N_("Shield"), "shield", true, UNHAPPY_PENALTY_SURPLUS},
85  {O_TRADE, N_("Trade"), "trade", true, UNHAPPY_PENALTY_NONE},
86  {O_GOLD, N_("Gold"), "gold", false, UNHAPPY_PENALTY_ALL_PRODUCTION},
87  {O_LUXURY, N_("Luxury"), "luxury", false, UNHAPPY_PENALTY_NONE},
88  {O_SCIENCE, N_("Science"), "science", false,
90 
95 bool city_tile_index_to_xy(int *city_map_x, int *city_map_y,
96  int city_tile_index, int city_radius_sq)
97 {
98  fc_assert_ret_val(city_radius_sq >= CITY_MAP_MIN_RADIUS_SQ, false);
99  fc_assert_ret_val(city_radius_sq <= CITY_MAP_MAX_RADIUS_SQ, false);
100 
101  // tile indices are sorted from smallest to largest city radius
102  if (city_tile_index < 0
103  || city_tile_index >= city_map_tiles(city_radius_sq)) {
104  return false;
105  }
106 
107  *city_map_x = CITY_REL2ABS(city_map_index[city_tile_index].dx);
108  *city_map_y = CITY_REL2ABS(city_map_index[city_tile_index].dy);
109 
110  return true;
111 }
112 
117 int city_tile_xy_to_index(int city_map_x, int city_map_y, int city_radius_sq)
118 {
119  fc_assert_ret_val(city_radius_sq >= CITY_MAP_MIN_RADIUS_SQ, 0);
120  fc_assert_ret_val(city_radius_sq <= CITY_MAP_MAX_RADIUS_SQ, 0);
122  is_valid_city_coords(city_radius_sq, city_map_x, city_map_y), 0);
123 
124  return city_map_xy[city_map_x][city_map_y];
125 }
126 
130 int city_map_radius_sq_get(const struct city *pcity)
131 {
132  // a save return value is only the minimal squared radius
133  fc_assert_ret_val(pcity != nullptr, CITY_MAP_MIN_RADIUS_SQ);
134 
135  return pcity->city_radius_sq;
136 }
137 
141 void city_map_radius_sq_set(struct city *pcity, int radius_sq)
142 {
145 
146  pcity->city_radius_sq = radius_sq;
147 }
148 
153 {
154  int max_rad = game.info.init_city_radius_sq
155  + effect_cumulative_max(EFT_CITY_RADIUS_SQ, nullptr);
156 
157  return MIN(max_rad, CITY_MAP_MAX_RADIUS_SQ);
158 }
159 
164 int city_map_tiles(int city_radius_sq)
165 {
166  if (city_radius_sq == CITY_MAP_CENTER_RADIUS_SQ) {
167  // special case: city center; first tile of the city map
168  return 0;
169  }
170 
171  fc_assert_ret_val(city_radius_sq >= CITY_MAP_MIN_RADIUS_SQ, 0);
172  fc_assert_ret_val(city_radius_sq <= CITY_MAP_MAX_RADIUS_SQ, 0);
173 
174  return city_map_numtiles[city_radius_sq];
175 }
176 
181 bool is_valid_city_coords(const int city_radius_sq, const int city_map_x,
182  const int city_map_y)
183 {
184  /* The city's valid positions are in a circle around the city center.
185  * Depending on the value for the squared city radius the circle will be:
186  *
187  * - rectangular (max radius = 5; max squared radius = 26)
188  *
189  * 0 1 2 3 4 5 6 7 8 9 10
190  *
191  * 0 26 25 26 -5
192  * 1 25 20 17 16 17 20 25 -4
193  * 2 25 18 13 10 9 10 13 18 25 -3
194  * 3 20 13 8 5 4 5 8 13 20 -2
195  * 4 26 17 10 5 2 1 2 5 10 17 26 -1
196  * 5 25 16 9 4 1 0 1 4 9 16 25 +0
197  * 6 26 17 10 5 2 1 2 5 10 17 26 +1
198  * 7 20 13 8 5 4 5 8 13 20 +2
199  * 8 25 18 13 10 9 10 13 18 25 +3
200  * 9 25 20 17 16 17 20 25 +4
201  * 10 26 25 26 +5
202  *
203  * -5 -4 -3 -2 -1 +0 +1 +2 +3 +4 +5
204  *
205  * - hexagonal (max radius = 5; max squared radius = 26)
206  *
207  * 0 1 2 3 4 5 6 7 8 9 10
208  *
209  * 0 25 25 25 25 25 25 -5
210  * 1 25 16 16 16 16 16 25 -4
211  * 2 25 16 9 9 9 9 16 25 -3
212  * 3 25 16 9 4 4 4 9 16 25 -2
213  * 4 25 16 9 4 1 1 4 9 16 25 -1
214  * 5 25 16 9 4 1 0 1 4 9 16 25 +0
215  * 6 25 16 9 4 1 1 4 9 16 25 +1
216  * 7 25 16 9 4 4 4 9 16 25 +2
217  * 8 25 16 9 9 9 9 16 25 +3
218  * 9 25 16 16 16 16 16 25 +4
219  * 10 25 25 25 25 25 25 +5
220  *
221  * -5 -4 -3 -2 -1 +0 +1 +2 +3 +4 +5
222  *
223  * The following tables show the tiles per city radii / squared city radii.
224  * '-' indicates no change compared to the previous value
225  *
226  * radius | 0 | 1 | | | 2 | | | | | 3
227  * radius_sq | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10
228  * ------------------+----+----+----+----+----+----+----+----+----+----
229  * tiles rectangular | 5 | 9 | - | 13 | 21 | - | - | 25 | 29 | 37
230  * tiles hexagonal | 7 | - | - | 19 | - | - | - | - | 37 | -
231  *
232  * radius | | | | | | | 4 | | |
233  * radius_sq | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20
234  * ------------------+----+----+----+----+----+----+----+----+----+----
235  * tiles rectangular | - | - | 45 | - | - | 49 | 57 | 61 | - | 69
236  * tiles hexagonal | - | - | - | - | - | 61 | - | - | - | -
237  *
238  * radius | | | | | | 5
239  * radius_sq | 21 | 22 | 23 | 24 | 25 | 26
240  * ------------------+----+----+----+----+----+----
241  * tiles rectangular | - | - | - | - | 81 | 89
242  * tiles hexagonal | - | - | - | - | 91 | -
243  *
244  * So radius_sq == 5 (radius == 2) corresponds to the "traditional"
245  * used city map.
246  */
247  int dist = map_vector_to_sq_distance(CITY_ABS2REL(city_map_x),
248  CITY_ABS2REL(city_map_y));
249 
250  return dist <= city_radius_sq;
251 }
252 
257 bool city_tile_to_city_map(int *city_map_x, int *city_map_y,
258  const int city_radius_sq,
259  const struct tile *city_center,
260  const struct tile *map_tile)
261 {
262  map_distance_vector(city_map_x, city_map_y, city_center, map_tile);
263 
264  *city_map_x += CITY_MAP_MAX_RADIUS;
265  *city_map_y += CITY_MAP_MAX_RADIUS;
266 
267  return is_valid_city_coords(city_radius_sq, *city_map_x, *city_map_y);
268 }
269 
274 bool city_base_to_city_map(int *city_map_x, int *city_map_y,
275  const struct city *const pcity,
276  const struct tile *map_tile)
277 {
278  return city_tile_to_city_map(city_map_x, city_map_y,
279  city_map_radius_sq_get(pcity), pcity->tile,
280  map_tile);
281 }
282 
286 bool city_map_includes_tile(const struct city *const pcity,
287  const struct tile *map_tile)
288 {
289  int tmp_x, tmp_y;
290 
291  return city_base_to_city_map(&tmp_x, &tmp_y, pcity, map_tile);
292 }
293 
298 struct tile *city_map_to_tile(const struct tile *city_center,
299  int city_radius_sq, int city_map_x,
300  int city_map_y)
301 {
302  int tile_x, tile_y;
303 
305  is_valid_city_coords(city_radius_sq, city_map_x, city_map_y), nullptr);
306 
307  index_to_map_pos(&tile_x, &tile_y, tile_index(city_center));
308  tile_x += CITY_ABS2REL(city_map_x);
309  tile_y += CITY_ABS2REL(city_map_y);
310 
311  return map_pos_to_tile(&(wld.map), tile_x, tile_y);
312 }
313 
317 static int cmp(int v1, int v2)
318 {
319  if (v1 == v2) {
320  return 0;
321  } else if (v1 > v2) {
322  return 1;
323  } else {
324  return -1;
325  }
326 }
327 
335 int compare_iter_index(const void *a, const void *b)
336 {
337  const iter_index *index1 = static_cast<const iter_index *>(a),
338  *index2 = static_cast<const iter_index *>(b);
339  int value;
340 
341  value = cmp(index1->dist, index2->dist);
342  if (value != 0) {
343  return value;
344  }
345 
346  value = cmp(index1->dx, index2->dx);
347  if (value != 0) {
348  return value;
349  }
350 
351  value = cmp(index1->dy, index2->dy);
352  fc_assert(0 != value);
353  return value;
354 }
355 
361 #define CITYLOG_MAX_VAL 9999 // maximal value displayed in the citylog
362 static char *citylog_map_line(int y, int city_radius_sq, int *city_map_data)
363 {
364  int x, mindex;
365  static char citylog[128], tmp[8];
366 
367  fc_assert_ret_val(city_map_data != nullptr, nullptr);
368 
369  // print y coordinates (absolut)
370  fc_snprintf(citylog, sizeof(citylog), "%2d ", y);
371 
372  // print values
373  for (x = 0; x < CITY_MAP_MAX_SIZE; x++) {
374  if (is_valid_city_coords(city_radius_sq, x, y)) {
375  mindex = city_tile_xy_to_index(x, y, city_radius_sq);
376  // show values between -10000 and +10000
377  if (city_map_data[mindex] >= -CITYLOG_MAX_VAL
378  && city_map_data[mindex] <= CITYLOG_MAX_VAL) {
379  fc_snprintf(tmp, sizeof(tmp), "%5d", city_map_data[mindex]);
380  sz_strlcat(citylog, tmp);
381  } else {
382  fc_snprintf(tmp, sizeof(tmp), " ####");
383  sz_strlcat(citylog, tmp);
384  }
385  } else {
386  fc_snprintf(tmp, sizeof(tmp), " ");
387  sz_strlcat(citylog, tmp);
388  }
389  }
390 
391  // print y coordinates (relativ)
392  fc_snprintf(tmp, sizeof(tmp), " %+4d", CITY_ABS2REL(y));
393  sz_strlcat(citylog, tmp);
394 
395  return citylog;
396 }
397 #undef CITYLOG_MAX_VAL
398 
404 void citylog_map_data(QtMsgType level, int radius_sq, int *map_data)
405 {
406  int x, y;
407  char line[128], tmp[8];
408 
409  log_base(level, "(max squared city radius = %d)", CITY_MAP_MAX_RADIUS_SQ);
410 
411  // print x coordinates (absolut)
412  fc_snprintf(line, sizeof(line), " ");
413  for (x = 0; x < CITY_MAP_MAX_SIZE; x++) {
414  fc_snprintf(tmp, sizeof(tmp), "%+5d", x);
415  sz_strlcat(line, tmp);
416  }
417  log_base(level, "%s", line);
418 
419  for (y = 0; y < CITY_MAP_MAX_SIZE; y++) {
420  log_base(level, "%s", citylog_map_line(y, radius_sq, map_data));
421  }
422 
423  // print x coordinates (relativ)
424  fc_snprintf(line, sizeof(line), " ");
425  for (x = 0; x < CITY_MAP_MAX_SIZE; x++) {
426  fc_snprintf(tmp, sizeof(tmp), "%+5d", CITY_ABS2REL(x));
427  sz_strlcat(line, tmp);
428  }
429  log_base(level, "%s", line);
430 }
431 
435 void citylog_map_workers(QtMsgType level, struct city *pcity)
436 {
437  int *city_map_data = nullptr;
438 
439  fc_assert_ret(pcity != nullptr);
440 
441  city_map_data = new int[city_map_tiles(city_map_radius_sq_get(pcity))]();
442 
443  city_map_iterate(city_map_radius_sq_get(pcity), cindex, x, y)
444  {
445  struct tile *ptile = city_map_to_tile(
446  city_tile(pcity), city_map_radius_sq_get(pcity), x, y);
447  city_map_data[cindex] = (ptile && tile_worked(ptile) == pcity)
448  ? (is_city_center_index(cindex) ? 2 : 1)
449  : 0;
450  }
452 
453  log_base(level, "[%s (%d)] workers map:", city_name_get(pcity), pcity->id);
454  citylog_map_data(level, city_map_radius_sq_get(pcity), city_map_data);
455  delete[] city_map_data;
456 }
457 
458 #ifdef FREECIV_DEBUG
462 static void citylog_map_index(QtMsgType level)
463 {
464  std::vector<int> city_map_data(city_map_tiles(CITY_MAP_MAX_RADIUS_SQ));
465 
467  {
468  city_map_data[cindex] = cindex;
469  }
471 
472  log_debug("city map index:");
473  citylog_map_data(level, CITY_MAP_MAX_RADIUS_SQ, city_map_data.data());
474 }
475 
479 static void citylog_map_radius_sq(QtMsgType level)
480 {
481  std::vector<int> city_map_data(city_map_tiles(CITY_MAP_MAX_RADIUS_SQ));
482 
484  {
485  city_map_data[cindex] =
487  }
489 
490  log_debug("city map squared radius:");
491  citylog_map_data(level, CITY_MAP_MAX_RADIUS_SQ, city_map_data.data());
492 }
493 #endif // FREECIV_DEBUG
494 
500 {
501  int i, dx, dy, city_x, city_y, dist, city_count_tiles = 0;
502  struct iter_index
503  city_map_index_tmp[CITY_MAP_MAX_SIZE * CITY_MAP_MAX_SIZE];
504 
505  // initialise map information for each city radii
506  for (i = 0; i <= CITY_MAP_MAX_RADIUS_SQ; i++) {
507  city_map_numtiles[i] = 0; // will be set below
508  }
509 
510  /* We don't use city-map iterators in this function because they may
511  * rely on the indices that have not yet been generated. Furthermore,
512  * we don't know the number of tiles within the city radius, so we need
513  * an temporary city_map_index array. Its content will be copied into
514  * the real array below. */
518 
519  if (dist <= CITY_MAP_MAX_RADIUS_SQ) {
520  city_map_index_tmp[city_count_tiles].dx = dx;
521  city_map_index_tmp[city_count_tiles].dy = dy;
522  city_map_index_tmp[city_count_tiles].dist = dist;
523 
524  for (i = CITY_MAP_MAX_RADIUS_SQ; i >= 0; i--) {
525  if (dist <= i) {
526  // increase number of tiles within this squared city radius
527  city_map_numtiles[i]++;
528  }
529  }
530 
531  city_count_tiles++;
532  }
533 
534  // Initialise city_map_xy. -1 defines a invalid city map positions.
536  }
537  }
538 
539  fc_assert(nullptr == city_map_index);
540  city_map_index = new iter_index[city_count_tiles];
541 
542  // copy the index numbers from city_map_index_tmp into city_map_index
543  for (i = 0; i < city_count_tiles; i++) {
544  city_map_index[i] = city_map_index_tmp[i];
545  }
546 
547  qsort(city_map_index, city_count_tiles, sizeof(*city_map_index),
549 
550  // set the static variable city_map_xy
551  for (i = 0; i < city_count_tiles; i++) {
552  city_x = CITY_REL2ABS(city_map_index[i].dx);
553  city_y = CITY_REL2ABS(city_map_index[i].dy);
554  city_map_xy[city_x][city_y] = i;
555  }
556 
557 #ifdef FREECIV_DEBUG
558  citylog_map_radius_sq(LOG_DEBUG);
559  citylog_map_index(LOG_DEBUG);
560 
561  for (i = CITY_MAP_MIN_RADIUS_SQ; i <= CITY_MAP_MAX_RADIUS_SQ; i++) {
562  log_debug("radius_sq = %2d, tiles = %2d", i, city_map_tiles(i));
563  }
564 
565  for (i = 0; i < city_count_tiles; i++) {
566  city_x = CITY_REL2ABS(city_map_index[i].dx);
567  city_y = CITY_REL2ABS(city_map_index[i].dy);
568  log_debug("[%2d]: (dx,dy) = (%+2d,%+2d), (x,y) = (%2d,%2d), "
569  "dist = %2d, check = %2d",
570  i, city_map_index[i].dx, city_map_index[i].dy, city_x, city_y,
571  city_map_index[i].dist, city_map_xy[city_x][city_y]);
572  }
573 #endif // FREECIV_DEBUG
574 
575  cm_init_citymap();
576 }
577 
582 {
583  delete[] city_map_index;
584  city_map_index = nullptr;
585 }
586 
593 {
594  fc_assert_ret_val(output >= 0 && output < O_LAST, nullptr);
595  return output_types[output].id;
596 }
597 
602 const char *get_output_name(Output_type_id output)
603 {
604  fc_assert_ret_val(output >= 0 && output < O_LAST, nullptr);
605  return _(output_types[output].name);
606 }
607 
612 {
613  fc_assert_ret_val(output >= 0 && output < O_LAST, nullptr);
614  return &output_types[output];
615 }
616 
621 {
622  for (int o = 0; o < O_LAST; o++) {
623  if (fc_strcasecmp(output_types[o].id, id) == 0) {
624  return output_type_id(o);
625  }
626  }
627 
628  return O_LAST;
629 }
630 
634 const char *
636  const struct impr_type *pimprove)
637 {
638  static char buffer[256];
639  const char *state = nullptr;
640 
641  if (is_great_wonder(pimprove)) {
642  if (great_wonder_is_available(pimprove)) {
643  state = Q_("?wonder:W");
644  } else if (great_wonder_is_destroyed(pimprove)) {
645  state = Q_("?destroyed:D");
646  } else {
647  state = Q_("?built:B");
648  }
649  }
650  if (pcity) {
651  struct player *pplayer = city_owner(pcity);
652 
653  if (improvement_obsolete(pplayer, pimprove, pcity)) {
654  state = Q_("?obsolete:O");
655  } else if (is_improvement_redundant(pcity, pimprove)) {
656  state = Q_("?redundant:*");
657  }
658  }
659 
660  if (state) {
661  fc_snprintf(buffer, sizeof(buffer), "%s(%s)",
662  improvement_name_translation(pimprove), state);
663  return buffer;
664  } else {
665  return improvement_name_translation(pimprove);
666  }
667 }
668 
672 const char *city_production_name_translation(const struct city *pcity)
673 {
674  static char buffer[256];
675 
676  switch (pcity->production.kind) {
677  case VUT_IMPROVEMENT:
679  pcity, pcity->production.value.building);
680  default:
681  // fallthru
682  break;
683  };
684  return universal_name_translation(&pcity->production, buffer,
685  sizeof(buffer));
686 }
687 
691 bool city_production_has_flag(const struct city *pcity,
692  enum impr_flag_id flag)
693 {
694  return VUT_IMPROVEMENT == pcity->production.kind
696 }
697 
701 int city_production_build_shield_cost(const struct city *pcity)
702 {
703  return universal_build_shield_cost(pcity, &pcity->production);
704 }
705 
711 bool city_production_build_units(const struct city *pcity,
712  bool add_production, int *num_units)
713 {
714  const struct unit_type *utype;
715  struct universal target;
716  int build_slots = city_build_slots(pcity);
717  int shields_left = pcity->shield_stock;
718  int unit_shield_cost, i;
719 
720  fc_assert_ret_val(num_units != nullptr, false);
721  (*num_units) = 0;
722 
723  if (pcity->production.kind != VUT_UTYPE) {
724  // not a unit as the current production
725  return false;
726  }
727 
728  utype = pcity->production.value.utype;
729  if (utype_pop_value(utype) != 0 || utype_has_flag(utype, UTYF_UNIQUE)) {
730  /* unit with population cost or unique unit means that only one unit can
731  * be build */
732  (*num_units)++;
733  return false;
734  }
735 
736  if (add_production) {
737  shields_left += pcity->prod[O_SHIELD];
738  }
739 
740  unit_shield_cost = utype_build_shield_cost(pcity, utype);
741 
742  for (i = 0; i < build_slots; i++) {
743  if (shields_left < unit_shield_cost) {
744  // not enough shields
745  break;
746  }
747 
748  (*num_units)++;
749  shields_left -= unit_shield_cost;
750 
751  if (worklist_length(&pcity->worklist) > i) {
752  (void) worklist_peek_ith(&pcity->worklist, &target, i);
753  if (target.kind != VUT_UTYPE
754  || utype_index(target.value.utype) != utype_index(utype)) {
755  /* stop if there is a build target in the worklist not equal to the
756  * unit we build */
757  break;
758  }
759  }
760  }
761 
762  return true;
763 }
764 
769  const struct unit_type *punittype)
770 {
771  int levels = get_unittype_bonus(city_owner(pcity), pcity->tile, punittype,
772  EFT_VETERAN_BUILD);
773  int max_levels = utype_veteran_levels(punittype) - 1;
774 
775  levels = CLIP(0, levels, max_levels);
776 
777  return levels;
778 }
779 
784 int city_production_turns_to_build(const struct city *pcity,
785  bool include_shield_stock)
786 {
787  return city_turns_to_build(pcity, &pcity->production,
788  include_shield_stock);
789 }
790 
795 bool can_city_build_improvement_direct(const struct city *pcity,
796  const struct impr_type *pimprove)
797 {
798  if (!can_player_build_improvement_direct(city_owner(pcity), pimprove)) {
799  return false;
800  }
801 
802  if (city_has_building(pcity, pimprove)) {
803  return false;
804  }
805 
806  return are_reqs_active(city_owner(pcity), nullptr, pcity, nullptr,
807  pcity->tile, nullptr, nullptr, nullptr, nullptr,
808  nullptr, &(pimprove->reqs), RPT_CERTAIN);
809 }
810 
815 bool can_city_build_improvement_now(const struct city *pcity,
816  const struct impr_type *pimprove)
817 {
818  if (!can_city_build_improvement_direct(pcity, pimprove)) {
819  return false;
820  }
821  if (improvement_obsolete(city_owner(pcity), pimprove, pcity)) {
822  return false;
823  }
824 
825  return true;
826 }
827 
832 bool can_city_build_improvement_later(const struct city *pcity,
833  const struct impr_type *pimprove)
834 {
835  // Can the _player_ ever build this improvement?
836  if (!can_player_build_improvement_later(city_owner(pcity), pimprove)) {
837  return false;
838  }
839 
840  /* Check for requirements that aren't met and that are unchanging (so
841  * they can never be met). */
842  requirement_vector_iterate(&pimprove->reqs, preq)
843  {
844  if (is_req_unchanging(preq)
845  && !is_req_active(city_owner(pcity), nullptr, pcity, nullptr,
846  pcity->tile, nullptr, nullptr, nullptr, nullptr,
847  nullptr, preq, RPT_POSSIBLE)) {
848  return false;
849  }
850  }
852 
853  return true;
854 }
855 
860 bool can_city_build_unit_direct(const struct city *pcity,
861  const struct unit_type *punittype)
862 {
863  if (!can_player_build_unit_direct(city_owner(pcity), punittype)) {
864  return false;
865  }
866 
867  // Check unit build requirements.
868  if (!are_reqs_active(city_owner(pcity), nullptr, pcity, nullptr,
869  city_tile(pcity), nullptr, punittype, nullptr,
870  nullptr, nullptr, &punittype->build_reqs,
871  RPT_CERTAIN)) {
872  return false;
873  }
874 
875  // You can't build naval units inland.
876  if (!uclass_has_flag(utype_class(punittype), UCF_BUILD_ANYWHERE)
877  && !is_native_near_tile(&(wld.map), utype_class(punittype),
878  pcity->tile)) {
879  return false;
880  }
881 
882  if (punittype->city_slots > 0
883  && city_unit_slots_available(pcity) < punittype->city_slots) {
884  return false;
885  }
886 
887  return true;
888 }
889 
894 bool can_city_build_unit_now(const struct city *pcity,
895  const struct unit_type *punittype)
896 {
897  if (!can_city_build_unit_direct(pcity, punittype)) {
898  return false;
899  }
900  while ((punittype = punittype->obsoleted_by) != U_NOT_OBSOLETED) {
901  if (can_player_build_unit_direct(city_owner(pcity), punittype)) {
902  return false;
903  }
904  }
905  return true;
906 }
907 
912 bool can_city_build_unit_later(const struct city *pcity,
913  const struct unit_type *punittype)
914 {
915  // Can the _player_ ever build this unit?
916  if (!can_player_build_unit_later(city_owner(pcity), punittype)) {
917  return false;
918  }
919 
920  /* Some units can be built only in certain cities -- for instance,
921  ships may be built only in cities adjacent to ocean. */
922  if (!uclass_has_flag(utype_class(punittype), UCF_BUILD_ANYWHERE)
923  && !is_native_near_tile(&(wld.map), utype_class(punittype),
924  pcity->tile)) {
925  return false;
926  }
927 
928  return true;
929 }
930 
935 bool can_city_build_direct(const struct city *pcity,
936  const struct universal *target)
937 {
938  switch (target->kind) {
939  case VUT_UTYPE:
940  return can_city_build_unit_direct(pcity, target->value.utype);
941  case VUT_IMPROVEMENT:
942  return can_city_build_improvement_direct(pcity, target->value.building);
943  default:
944  break;
945  };
946  return false;
947 }
948 
953 bool can_city_build_now(const struct city *pcity,
954  const struct universal *target)
955 {
956  switch (target->kind) {
957  case VUT_UTYPE:
958  return can_city_build_unit_now(pcity, target->value.utype);
959  case VUT_IMPROVEMENT:
960  return can_city_build_improvement_now(pcity, target->value.building);
961  default:
962  break;
963  };
964  return false;
965 }
966 
970 bool can_city_build_later(const struct city *pcity,
971  const struct universal *target)
972 {
973  switch (target->kind) {
974  case VUT_UTYPE:
975  return can_city_build_unit_later(pcity, target->value.utype);
976  case VUT_IMPROVEMENT:
977  return can_city_build_improvement_later(pcity, target->value.building);
978  default:
979  break;
980  };
981  return false;
982 }
983 
987 int city_unit_slots_available(const struct city *pcity)
988 {
989  int max = get_city_bonus(pcity, EFT_UNIT_SLOTS);
990  int current;
991 
992  current = 0;
993  unit_list_iterate(pcity->units_supported, punit)
994  {
995  current += unit_type_get(punit)->city_slots;
996  }
998 
999  return max - current;
1000 }
1001 
1005 bool city_can_use_specialist(const struct city *pcity,
1006  Specialist_type_id type)
1007 {
1008  return are_reqs_active(city_owner(pcity), nullptr, pcity, nullptr, nullptr,
1009  nullptr, nullptr, nullptr, nullptr, nullptr,
1011 }
1012 
1016 bool city_can_change_build(const struct city *pcity)
1017 {
1018  return !pcity->did_buy || pcity->shield_stock <= 0;
1019 }
1020 
1024 void city_choose_build_default(struct city *pcity)
1025 {
1026  if (nullptr == city_tile(pcity)) {
1027  /* When a "dummy" city is created with no tile, then choosing a build
1028  * target could fail. This currently might happen during map editing.
1029  * FIXME: assumes the first unit is always "valid", so check for
1030  * obsolete units elsewhere. */
1031  pcity->production.kind = VUT_UTYPE;
1032  pcity->production.value.utype = utype_by_number(0);
1033  } else {
1034  struct unit_type *u = best_role_unit(pcity, L_FIRSTBUILD);
1035 
1036  if (u) {
1037  pcity->production.kind = VUT_UTYPE;
1038  pcity->production.value.utype = u;
1039  } else {
1040  bool found = false;
1041 
1042  // Just pick the first available item.
1043 
1044  improvement_iterate(pimprove)
1045  {
1046  if (can_city_build_improvement_direct(pcity, pimprove)) {
1047  found = true;
1048  pcity->production.kind = VUT_IMPROVEMENT;
1049  pcity->production.value.building = pimprove;
1050  break;
1051  }
1052  }
1054 
1055  if (!found) {
1056  unit_type_iterate(punittype)
1057  {
1058  if (can_city_build_unit_direct(pcity, punittype)) {
1059  found = true;
1060  pcity->production.kind = VUT_UTYPE;
1061  pcity->production.value.utype = punittype;
1062  }
1063  }
1065  }
1066 
1067  fc_assert_msg(found, "No production found for city %s!",
1068  city_name_get(pcity));
1069  }
1070  }
1071 }
1072 
1073 #ifndef city_name_get
1077 const char *city_name_get(const struct city *pcity) { return pcity->name; }
1078 #endif // city_name_get
1079 
1083 struct player *city_owner(const struct city *pcity)
1084 {
1085  fc_assert_ret_val(nullptr != pcity, nullptr);
1086  fc_assert(nullptr != pcity->owner);
1087  return pcity->owner;
1088 }
1089 
1090 #ifndef city_tile
1095 struct tile *city_tile(const struct city *pcity) { return pcity->tile; }
1096 #endif
1097 
1101 citizens city_size_get(const struct city *pcity)
1102 {
1103  fc_assert_ret_val(pcity != nullptr, 0);
1104 
1105  return pcity->size;
1106 }
1107 
1112 void city_size_add(struct city *pcity, int add)
1113 {
1114  citizens size = city_size_get(pcity);
1115 
1116  fc_assert_ret(pcity != nullptr);
1117  fc_assert_ret(MAX_CITY_SIZE - size > add);
1118  fc_assert_ret(size >= -add);
1119 
1120  city_size_set(pcity, city_size_get(pcity) + add);
1121 }
1122 
1126 void city_size_set(struct city *pcity, citizens size)
1127 {
1128  fc_assert_ret(pcity != nullptr);
1129 
1130  // Set city size.
1131  pcity->size = size;
1132 }
1133 
1137 int city_population(const struct city *pcity)
1138 {
1139  /* Sum_{i=1}^{n} i == n*(n+1)/2 */
1140  return city_size_get(pcity) * (city_size_get(pcity) + 1) * 5;
1141 }
1142 
1147 int city_total_impr_gold_upkeep(const struct city *pcity)
1148 {
1149  int gold_needed = 0;
1150 
1151  if (!pcity) {
1152  return 0;
1153  }
1154 
1155  city_built_iterate(pcity, pimprove)
1156  {
1157  gold_needed += city_improvement_upkeep(pcity, pimprove);
1158  }
1160 
1161  return gold_needed;
1162 }
1163 
1168 int city_total_unit_gold_upkeep(const struct city *pcity)
1169 {
1170  int gold_needed = 0;
1171 
1172  if (!pcity || !pcity->units_supported
1173  || unit_list_size(pcity->units_supported) < 1) {
1174  return 0;
1175  }
1176 
1177  unit_list_iterate(pcity->units_supported, punit)
1178  {
1179  gold_needed += punit->upkeep[O_GOLD];
1180  }
1182 
1183  return gold_needed;
1184 }
1185 
1189 bool city_has_building(const struct city *pcity,
1190  const struct impr_type *pimprove)
1191 {
1192  if (nullptr == pimprove) {
1193  return false;
1194  }
1195  return (pcity->built[improvement_index(pimprove)].turn > I_NEVER);
1196 }
1197 
1202 int city_improvement_upkeep(const struct city *pcity,
1203  const struct impr_type *b)
1204 {
1205  int upkeep;
1206 
1207  if (nullptr == b) {
1208  return 0;
1209  }
1210  if (is_wonder(b)) {
1211  return 0;
1212  }
1213 
1214  upkeep = b->upkeep;
1215  if (upkeep <= get_building_bonus(pcity, b, EFT_UPKEEP_FREE)) {
1216  return 0;
1217  }
1218 
1219  return upkeep;
1220 }
1221 
1230 int city_tile_output(const struct city *pcity, const struct tile *ptile,
1231  bool is_celebrating, Output_type_id otype)
1232 {
1233  int prod;
1234  struct terrain *pterrain = tile_terrain(ptile);
1235  const struct output_type *output = &output_types[otype];
1236  struct player *pplayer = nullptr;
1237 
1238  fc_assert_ret_val(otype >= 0 && otype < O_LAST, 0);
1239 
1240  if (T_UNKNOWN == pterrain) {
1241  /* Special case for the client. The server doesn't allow unknown tiles
1242  * to be worked but we don't necessarily know what player is involved. */
1243  return 0;
1244  }
1245 
1246  prod = pterrain->output[otype];
1247  if (tile_resource_is_valid(ptile)) {
1248  prod += tile_resource(ptile)->data.resource->output[otype];
1249  }
1250 
1251  if (pcity != nullptr) {
1252  pplayer = city_owner(pcity);
1253  }
1254 
1255  switch (otype) {
1256  case O_SHIELD:
1257  if (pterrain->mining_shield_incr != 0) {
1258  prod += pterrain->mining_shield_incr
1260  nullptr, pplayer, nullptr, pcity, nullptr, ptile, nullptr,
1261  nullptr, nullptr, nullptr, nullptr, EFT_MINING_PCT)
1262  / 100;
1263  }
1264  break;
1265  case O_FOOD:
1266  if (pterrain->irrigation_food_incr != 0) {
1267  prod += pterrain->irrigation_food_incr
1269  nullptr, pplayer, nullptr, pcity, nullptr, ptile, nullptr,
1270  nullptr, nullptr, nullptr, nullptr, EFT_IRRIGATION_PCT)
1271  / 100;
1272  }
1273  break;
1274  case O_TRADE:
1275  case O_GOLD:
1276  case O_SCIENCE:
1277  case O_LUXURY:
1278  case O_LAST:
1279  break;
1280  }
1281 
1282  prod += tile_roads_output_incr(ptile, otype);
1283  prod += (prod * tile_roads_output_bonus(ptile, otype) / 100);
1284 
1285  prod += get_tile_output_bonus(pcity, ptile, output, EFT_OUTPUT_ADD_TILE);
1286  if (prod > 0) {
1287  int penalty_limit =
1288  get_tile_output_bonus(pcity, ptile, output, EFT_OUTPUT_PENALTY_TILE);
1289 
1290  if (is_celebrating) {
1291  prod += get_tile_output_bonus(pcity, ptile, output,
1292  EFT_OUTPUT_INC_TILE_CELEBRATE);
1293  penalty_limit = 0; // no penalty if celebrating
1294  }
1295  prod += get_tile_output_bonus(pcity, ptile, output, EFT_OUTPUT_INC_TILE);
1296  prod +=
1297  (prod
1298  * get_tile_output_bonus(pcity, ptile, output, EFT_OUTPUT_PER_TILE))
1299  / 100;
1300  if (!is_celebrating && penalty_limit > 0 && prod > penalty_limit) {
1301  prod--;
1302  }
1303  }
1304 
1305  prod -= (prod
1306  * get_tile_output_bonus(pcity, ptile, output,
1307  EFT_OUTPUT_TILE_PUNISH_PCT))
1308  / 100;
1309 
1310  if (nullptr != pcity && is_city_center(pcity, ptile)) {
1311  prod = MAX(prod, game.info.min_city_center_output[otype]);
1312  }
1313 
1314  return prod;
1315 }
1316 
1322 int city_tile_output_now(const struct city *pcity, const struct tile *ptile,
1323  Output_type_id otype)
1324 {
1325  return city_tile_output(pcity, ptile, city_celebrating(pcity), otype);
1326 }
1327 
1339 bool base_city_can_work_tile(const struct player *restriction,
1340  const struct city *pcity,
1341  const struct tile *ptile)
1342 {
1343  struct player *powner = city_owner(pcity);
1344  int city_map_x, city_map_y;
1345 
1346  if (nullptr == ptile) {
1347  return false;
1348  }
1349 
1350  if (!city_base_to_city_map(&city_map_x, &city_map_y, pcity, ptile)) {
1351  return false;
1352  }
1353 
1354  if (nullptr != restriction
1355  && TILE_UNKNOWN == tile_get_known(ptile, restriction)) {
1356  return false;
1357  }
1358 
1359  if (nullptr != tile_owner(ptile) && tile_owner(ptile) != powner) {
1360  return false;
1361  }
1362  // TODO: civ3-like option for borders
1363 
1364  if (nullptr != tile_worked(ptile) && tile_worked(ptile) != pcity) {
1365  return false;
1366  }
1367 
1368  if (powner == restriction
1369  && TILE_KNOWN_SEEN != tile_get_known(ptile, powner)) {
1370  return false;
1371  }
1372 
1373  if (!is_city_center(pcity, ptile)
1374  && nullptr != unit_occupies_tile(ptile, powner)) {
1375  return false;
1376  }
1377 
1378  if (get_city_tile_output_bonus(pcity, ptile, nullptr, EFT_TILE_WORKABLE)
1379  <= 0) {
1380  return false;
1381  }
1382 
1383  return true;
1384 }
1385 
1392 bool city_can_work_tile(const struct city *pcity, const struct tile *ptile)
1393 {
1394  return base_city_can_work_tile(city_owner(pcity), pcity, ptile);
1395 }
1396 
1401 bool citymindist_prevents_city_on_tile(const struct tile *ptile)
1402 {
1403  // citymindist minimum is 1, meaning adjacent is okay
1404  int citymindist = game.info.citymindist;
1405  square_iterate(&(wld.map), ptile, citymindist - 1, ptile1)
1406  {
1407  if (tile_city(ptile1)) {
1408  return true;
1409  }
1410  }
1412 
1413  return false;
1414 }
1415 
1423 bool city_can_be_built_here(const struct tile *ptile,
1424  const struct unit *punit)
1425 {
1426  if (!city_can_be_built_tile_only(ptile)) {
1427  return false;
1428  }
1429 
1430  if (punit == nullptr) {
1431  // The reamining checks tests if punit can found a city here
1432  return true;
1433  }
1434 
1435  action_by_result_iterate(paction, act_id, ACTRES_FOUND_CITY)
1436  {
1437  if (!utype_can_do_action(unit_type_get(punit), act_id)) {
1438  // This action can't be done by this unit type at all.
1439  continue;
1440  }
1441 
1442  // Non native tile detection
1443  if (!can_unit_exist_at_tile(&(wld.map), punit, ptile)
1444  // The ruleset may allow founding cities on non native terrain.
1445  && !utype_can_do_act_when_ustate(unit_type_get(punit), paction->id,
1446  USP_LIVABLE_TILE, false)) {
1447  /* Many rulesets allow land units to build land cities and sea units
1448  * to build ocean cities. Air units can build cities anywhere. */
1449  continue;
1450  }
1451 
1452  // Foreign tile detection.
1453  if (tile_owner(ptile)
1454  && tile_owner(ptile) != unit_owner(punit)
1455  // The ruleset may allow founding cities on foreign terrain.
1457  unit_type_get(punit), paction->id, DRO_FOREIGN, true)) {
1458  /* Cannot steal borders by settling. This has to be settled by
1459  * force of arms. */
1460  continue;
1461  }
1462 
1463  return true;
1464  }
1466 
1467  return false;
1468 }
1469 
1477 bool city_can_be_built_tile_only(const struct tile *ptile)
1478 {
1479  if (terrain_has_flag(tile_terrain(ptile), TER_NO_CITIES)) {
1480  // No cities on this terrain.
1481  return false;
1482  }
1483 
1484  if (citymindist_prevents_city_on_tile(ptile)) {
1485  return false;
1486  }
1487 
1488  return true;
1489 }
1490 
1495 bool is_capital(const struct city *pcity)
1496 {
1497  return pcity->capital != CAPITAL_NOT;
1498 }
1499 
1503 bool is_gov_center(const struct city *pcity)
1504 {
1505  return (get_city_bonus(pcity, EFT_GOV_CENTER) > 0);
1506 }
1507 
1512 bool city_got_defense_effect(const struct city *pcity,
1513  const struct unit_type *attacker)
1514 {
1515  if (!attacker) {
1516  // Any defense building will do
1517  return get_city_bonus(pcity, EFT_DEFEND_BONUS) > 0;
1518  }
1519 
1520  return get_unittype_bonus(city_owner(pcity), pcity->tile, attacker,
1521  EFT_DEFEND_BONUS)
1522  > 0;
1523 }
1524 
1531 bool city_happy(const struct city *pcity)
1532 {
1533  return (city_size_get(pcity) >= game.info.celebratesize
1534  && pcity->feel[CITIZEN_ANGRY][FEELING_FINAL] == 0
1535  && pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL] == 0
1536  && pcity->feel[CITIZEN_HAPPY][FEELING_FINAL]
1537  >= (city_size_get(pcity) + 1) / 2);
1538 }
1539 
1544 bool city_unhappy(const struct city *pcity)
1545 {
1546  return (pcity->feel[CITIZEN_HAPPY][FEELING_FINAL]
1548  + 2 * pcity->feel[CITIZEN_ANGRY][FEELING_FINAL]);
1549 }
1550 
1555 bool base_city_celebrating(const struct city *pcity)
1556 {
1557  return (city_size_get(pcity) >= game.info.celebratesize
1558  && pcity->was_happy);
1559 }
1560 
1564 bool city_celebrating(const struct city *pcity)
1565 {
1566  return base_city_celebrating(pcity) && city_happy(pcity);
1567 }
1568 
1572 bool city_rapture_grow(const struct city *pcity)
1573 {
1574  /* .rapture is checked instead of city_celebrating() because this
1575  function is called after .was_happy was updated. */
1576  return (pcity->rapture > 0 && pcity->surplus[O_FOOD] > 0
1577  && (pcity->rapture % game.info.rapturedelay) == 0
1578  && get_city_bonus(pcity, EFT_RAPTURE_GROW) > 0);
1579 }
1580 
1584 bool city_is_occupied(const struct city *pcity)
1585 {
1586  if (is_server()) {
1587  // The server sees the units inside the city.
1588  return (unit_list_size(city_tile(pcity)->units) > 0);
1589  } else {
1590  // The client gets the occupied property from the server.
1591  return pcity->client.occupied;
1592  }
1593 }
1594 
1598 struct city *city_list_find_number(struct city_list *This, int id)
1599 {
1600  if (id != 0) {
1601  city_list_iterate(This, pcity)
1602  {
1603  if (pcity->id == id) {
1604  return pcity;
1605  }
1606  }
1608  }
1609 
1610  return nullptr;
1611 }
1612 
1616 struct city *city_list_find_name(struct city_list *This, const char *name)
1617 {
1618  city_list_iterate(This, pcity)
1619  {
1620  if (fc_strcasecmp(name, pcity->name) == 0) {
1621  return pcity;
1622  }
1623  }
1625 
1626  return nullptr;
1627 }
1628 
1633 int city_style_by_rule_name(const char *s)
1634 {
1635  const char *qs = Qn_(s);
1636  int i;
1637 
1638  for (i = 0; i < game.control.styles_count; i++) {
1639  if (0 == fc_strcasecmp(city_style_rule_name(i), qs)) {
1640  return i;
1641  }
1642  }
1643 
1644  return -1;
1645 }
1646 
1651 const char *city_style_rule_name(const int style)
1652 {
1653  return rule_name_get(&city_styles[style].name);
1654 }
1655 
1656 // Cache of what city production caravan shields are allowed to help.
1657 static bv_imprs caravan_helped_impr;
1658 static bv_unit_types caravan_helped_utype;
1659 
1665 {
1666  struct requirement prod_as_req;
1667 
1668 #define log_ca_s_init log_debug
1669 
1670  // Remove old data.
1673 
1674  // Common for all production kinds.
1675  prod_as_req.range = REQ_RANGE_LOCAL;
1676  prod_as_req.survives = false;
1677  prod_as_req.present = true;
1678 
1679  // Check improvements
1680  prod_as_req.source.kind = VUT_IMPROVEMENT;
1681 
1682  improvement_iterate(itype)
1683  {
1684  // Check this improvement.
1685  prod_as_req.source.value.building = itype;
1686 
1688  action_enablers_for_action(ACTION_HELP_WONDER), enabler)
1689  {
1690  if (!does_req_contradicts_reqs(&prod_as_req, &(enabler->target_reqs))
1691  && !req_vec_wants_type(&(enabler->target_reqs), VUT_UTYPE)
1692  && !req_vec_wants_type(&(enabler->target_reqs), VUT_UCLASS)
1693  && !req_vec_wants_type(&(enabler->target_reqs), VUT_UTFLAG)
1694  && !req_vec_wants_type(&(enabler->target_reqs), VUT_UCFLAG)) {
1695  // This improvement kind can receive caravan shields.
1696 
1698 
1699  // Move on to the next improvment
1700  break;
1701  }
1702  }
1704 
1705  log_ca_s_init("Help Wonder: %s for %s",
1707  ? "possible"
1708  : "impossible"),
1709  improvement_rule_name(itype));
1710  }
1712 
1713  // Check units.
1714  prod_as_req.source.kind = VUT_UTYPE;
1715 
1716  unit_type_iterate(putype)
1717  {
1718  // Check this utype.
1719  prod_as_req.source.value.utype = putype;
1720 
1722  action_enablers_for_action(ACTION_HELP_WONDER), enabler)
1723  {
1724  if (!does_req_contradicts_reqs(&prod_as_req, &(enabler->target_reqs))
1725  && !req_vec_wants_type(&(enabler->target_reqs), VUT_IMPROVEMENT)
1726  && !req_vec_wants_type(&(enabler->target_reqs), VUT_IMPR_GENUS)) {
1727  // This unit type kind can receive caravan shields.
1728 
1730 
1731  // Move on to the next unit type
1732  break;
1733  }
1734  }
1736 
1737  log_ca_s_init("Help Wonder: %s for %s",
1739  ? "possible"
1740  : "impossible"),
1741  utype_rule_name(putype));
1742  }
1744 
1745 #undef log_ca_s_init
1746 }
1747 
1753 {
1754  switch (tgt->kind) {
1755  case VUT_IMPROVEMENT:
1758  case VUT_UTYPE:
1760  default:
1761  fc_assert(false);
1762  return false;
1763  };
1764 }
1765 
1777 int city_change_production_penalty(const struct city *pcity,
1778  const struct universal *target)
1779 {
1780  int shield_stock_after_adjustment;
1781  enum production_class_type orig_class;
1782  enum production_class_type new_class;
1783  int unpenalized_shields = 0, penalized_shields = 0;
1784 
1785  switch (pcity->changed_from.kind) {
1786  case VUT_IMPROVEMENT:
1787  if (is_wonder(pcity->changed_from.value.building)) {
1788  orig_class = PCT_WONDER;
1789  } else {
1790  orig_class = PCT_NORMAL_IMPROVEMENT;
1791  }
1792  break;
1793  case VUT_UTYPE:
1794  orig_class = PCT_UNIT;
1795  break;
1796  default:
1797  orig_class = PCT_LAST;
1798  break;
1799  };
1800 
1801  switch (target->kind) {
1802  case VUT_IMPROVEMENT:
1803  if (is_wonder(target->value.building)) {
1804  new_class = PCT_WONDER;
1805  } else {
1806  new_class = PCT_NORMAL_IMPROVEMENT;
1807  }
1808  break;
1809  case VUT_UTYPE:
1810  new_class = PCT_UNIT;
1811  break;
1812  default:
1813  new_class = PCT_LAST;
1814  break;
1815  };
1816 
1817  // Changing production is penalized under certain circumstances.
1818  if (orig_class == new_class || orig_class == PCT_LAST) {
1819  // There's never a penalty for building something of the same class.
1820  unpenalized_shields = pcity->before_change_shields;
1821  } else if (city_built_last_turn(pcity)) {
1822  /* Surplus shields from the previous production won't be penalized if
1823  * you change production on the very next turn. But you can only use
1824  * up to the city's surplus amount of shields in this way. */
1825  unpenalized_shields =
1827  penalized_shields = pcity->before_change_shields - unpenalized_shields;
1828  } else {
1829  // Penalize 50% of the production.
1830  penalized_shields = pcity->before_change_shields;
1831  }
1832 
1833  // Penalize 50% if you bought a unit but didn't build it
1834  if ((pcity->bought_shields > 0
1835  && ((target->kind == VUT_UTYPE
1836  && target->value.utype != pcity->changed_from.value.utype)))) {
1837  unpenalized_shields -= pcity->bought_shields;
1838  penalized_shields += pcity->bought_shields;
1839  } else {
1840  unpenalized_shields += pcity->bought_shields;
1841  }
1842 
1843  /* Do not put penalty on these. It shouldn't matter whether you disband
1844  unit before or after changing production...*/
1845  unpenalized_shields += pcity->disbanded_shields;
1846 
1847  /* Caravan shields are penalized (just as if you disbanded the caravan)
1848  * if you're not building a wonder. */
1850  unpenalized_shields += pcity->caravan_shields;
1851  } else {
1852  penalized_shields += pcity->caravan_shields;
1853  }
1854 
1855  shield_stock_after_adjustment =
1856  unpenalized_shields + penalized_shields / 2;
1857 
1858  return shield_stock_after_adjustment;
1859 }
1860 
1865 int city_turns_to_build(const struct city *pcity,
1866  const struct universal *target,
1867  bool include_shield_stock)
1868 {
1869  int city_shield_surplus = pcity->surplus[O_SHIELD];
1870  int city_shield_stock = include_shield_stock
1871  ? city_change_production_penalty(pcity, target)
1872  : 0;
1873  int cost = universal_build_shield_cost(pcity, target);
1874 
1875  if (target->kind == VUT_IMPROVEMENT
1876  && is_great_wonder(target->value.building)
1877  && !great_wonder_is_available(target->value.building)) {
1878  return FC_INFINITY;
1879  }
1880 
1881  if (include_shield_stock && (city_shield_stock >= cost)) {
1882  return 1;
1883  } else if (city_shield_surplus > 0) {
1884  return (cost - city_shield_stock - 1) / city_shield_surplus + 1;
1885  } else {
1886  return FC_INFINITY;
1887  }
1888 }
1889 
1897 int city_turns_to_grow(const struct city *pcity)
1898 {
1899  if (pcity->surplus[O_FOOD] > 0) {
1900  return (city_granary_size(city_size_get(pcity)) - pcity->food_stock
1901  + pcity->surplus[O_FOOD] - 1)
1902  / pcity->surplus[O_FOOD];
1903  } else if (pcity->surplus[O_FOOD] < 0) {
1904  // turns before famine loss
1905  return -1 + (pcity->food_stock / pcity->surplus[O_FOOD]);
1906  } else {
1907  return FC_INFINITY;
1908  }
1909 }
1910 
1914 bool city_can_grow_to(const struct city *pcity, int pop_size)
1915 {
1916  return (get_city_bonus(pcity, EFT_SIZE_UNLIMIT) > 0
1917  || pop_size <= get_city_bonus(pcity, EFT_SIZE_ADJ));
1918 }
1919 
1923 struct city *is_enemy_city_tile(const struct tile *ptile,
1924  const struct player *pplayer)
1925 {
1926  struct city *pcity = tile_city(ptile);
1927 
1928  if (pcity && pplayers_at_war(pplayer, city_owner(pcity))) {
1929  return pcity;
1930  } else {
1931  return nullptr;
1932  }
1933 }
1934 
1938 struct city *is_allied_city_tile(const struct tile *ptile,
1939  const struct player *pplayer)
1940 {
1941  struct city *pcity = tile_city(ptile);
1942 
1943  if (pcity && pplayers_allied(pplayer, city_owner(pcity))) {
1944  return pcity;
1945  } else {
1946  return nullptr;
1947  }
1948 }
1949 
1953 struct city *is_non_attack_city_tile(const struct tile *ptile,
1954  const struct player *pplayer)
1955 {
1956  struct city *pcity = tile_city(ptile);
1957 
1958  if (pcity && pplayers_non_attack(pplayer, city_owner(pcity))) {
1959  return pcity;
1960  } else {
1961  return nullptr;
1962  }
1963 }
1964 
1968 struct city *is_non_allied_city_tile(const struct tile *ptile,
1969  const struct player *pplayer)
1970 {
1971  struct city *pcity = tile_city(ptile);
1972 
1973  if (pcity && !pplayers_allied(pplayer, city_owner(pcity))) {
1974  return pcity;
1975  } else {
1976  return nullptr;
1977  }
1978 }
1979 
1984 bool is_unit_near_a_friendly_city(const struct unit *punit)
1985 {
1986  return is_friendly_city_near(unit_owner(punit), unit_tile(punit));
1987 }
1988 
1994  const struct tile *ptile)
1995 {
1996  square_iterate(&(wld.map), ptile, 3, ptile1)
1997  {
1998  struct city *pcity = tile_city(ptile1);
1999 
2000  if (pcity && pplayers_allied(owner, city_owner(pcity))) {
2001  return true;
2002  }
2003  }
2005 
2006  return false;
2007 }
2008 
2013 bool city_exists_within_max_city_map(const struct tile *ptile,
2014  bool may_be_on_center)
2015 {
2017  {
2018  if (may_be_on_center || !same_pos(ptile, ptile1)) {
2019  if (tile_city(ptile1)) {
2020  return true;
2021  }
2022  }
2023  }
2025 
2026  return false;
2027 }
2028 
2034 int city_granary_size(int city_size)
2035 {
2036  int food_inis = game.info.granary_num_inis;
2037  int food_inc = game.info.granary_food_inc;
2038  int base_value;
2039 
2040  // If the city has no citizens, there is no granary.
2041  if (city_size == 0) {
2042  return 0;
2043  }
2044 
2045  /* Granary sizes for the first food_inis citizens are given directly.
2046  * After that we increase the granary size by food_inc per citizen. */
2047  if (city_size > food_inis) {
2048  base_value = game.info.granary_food_ini[food_inis - 1];
2049  base_value += food_inc * (city_size - food_inis);
2050  } else {
2051  base_value = game.info.granary_food_ini[city_size - 1];
2052  }
2053 
2054  return MAX(base_value * game.info.foodbox / 100, 1);
2055 }
2056 
2062 int player_base_citizen_happiness(const struct player *pplayer)
2063 {
2064  int cities = city_list_size(pplayer->cities);
2065  int content = get_player_bonus(pplayer, EFT_CITY_UNHAPPY_SIZE);
2066  int basis = get_player_bonus(pplayer, EFT_EMPIRE_SIZE_BASE);
2067  int step = get_player_bonus(pplayer, EFT_EMPIRE_SIZE_STEP);
2068 
2069  if (basis + step <= 0) {
2070  // Value of zero means effect is inactive
2071  return content;
2072  }
2073 
2074  if (cities > basis) {
2075  content--;
2076  if (step > 0) {
2077  /* the first penalty is at (basis + 1) cities;
2078  the next is at (basis + step + 1), _not_ (basis + step) */
2079  content -= (cities - basis - 1) / step;
2080  }
2081  }
2082  return content;
2083 }
2084 
2089 {
2090  int content = player_base_citizen_happiness(pplayer);
2091 
2092  return CLIP(0, content, MAX_CITY_SIZE);
2093 }
2094 
2098 citizens player_angry_citizens(const struct player *pplayer)
2099 {
2100  if (!game.info.angrycitizen) {
2101  return 0;
2102  } else {
2103  /* Create angry citizens only if we have a negative number of possible
2104  * content citizens. This can happen when empires grow really big. */
2105  int content = player_base_citizen_happiness(pplayer);
2106 
2107  return CLIP(0, -content, MAX_CITY_SIZE);
2108  }
2109 }
2110 
2114 int get_final_city_output_bonus(const struct city *pcity,
2115  Output_type_id otype)
2116 {
2117  struct output_type *output = &output_types[otype];
2118  int bonus1 = 100
2119  + get_city_tile_output_bonus(pcity, pcity->tile, output,
2120  EFT_OUTPUT_BONUS);
2121  int bonus2 = 100
2122  + get_city_tile_output_bonus(pcity, pcity->tile, output,
2123  EFT_OUTPUT_BONUS_2);
2124 
2125  return MAX(bonus1 * bonus2 / 100, 0);
2126 }
2127 
2132 int get_city_tithes_bonus(const struct city *pcity)
2133 {
2134  int tithes_bonus = 0;
2135 
2136  if (get_city_bonus(pcity, EFT_HAPPINESS_TO_GOLD) <= 0) {
2137  return 0;
2138  }
2139 
2140  tithes_bonus += get_city_bonus(pcity, EFT_MAKE_CONTENT);
2141  tithes_bonus += get_city_bonus(pcity, EFT_FORCE_CONTENT);
2142 
2143  return tithes_bonus;
2144 }
2145 
2150 void add_tax_income(const struct player *pplayer, int trade, int *output)
2151 {
2152  const int SCIENCE = 0, TAX = 1, LUXURY = 2;
2153  int rates[3], result[3];
2154 
2155  if (game.info.changeable_budget) {
2156  rates[SCIENCE] = pplayer->economic.science;
2157  rates[LUXURY] = pplayer->economic.luxury;
2158  rates[TAX] = 100 - rates[SCIENCE] - rates[LUXURY];
2159  } else {
2160  rates[SCIENCE] = game.info.forced_science;
2161  rates[LUXURY] = game.info.forced_luxury;
2162  rates[TAX] = game.info.forced_gold;
2163  }
2164 
2165  // ANARCHY
2167  rates[SCIENCE] = 0;
2168  rates[LUXURY] = 100;
2169  rates[TAX] = 0;
2170  }
2171 
2172  distribute(trade, 3, rates, result);
2173 
2174  output[O_SCIENCE] += result[SCIENCE];
2175  output[O_GOLD] += result[TAX];
2176  output[O_LUXURY] += result[LUXURY];
2177 }
2178 
2183 bool city_built_last_turn(const struct city *pcity)
2184 {
2185  return pcity->turn_last_built + 1 >= game.info.turn;
2186 }
2187 
2196 static inline void get_worked_tile_output(const struct city *pcity,
2197  int *output, bool *workers_map)
2198 {
2199  bool is_worked;
2200 #ifdef CITY_DEBUGGING
2201  bool is_celebrating = base_city_celebrating(pcity);
2202 #endif
2203  struct tile *pcenter = city_tile(pcity);
2204 
2205  memset(output, 0, O_LAST * sizeof(*output));
2206 
2207  city_tile_iterate_index(city_map_radius_sq_get(pcity), pcenter, ptile,
2208  city_tile_index)
2209  {
2210  if (workers_map == nullptr) {
2211  struct city *pwork = tile_worked(ptile);
2212 
2213  is_worked = (nullptr != pwork && pwork == pcity);
2214  } else {
2215  is_worked = workers_map[city_tile_index];
2216  }
2217 
2218  if (is_worked) {
2220  {
2221 #ifdef CITY_DEBUGGING
2222  /* This assertion never fails, but it's so slow that we disable
2223  * it by default. */
2224  fc_assert(city_tile_cache_get_output(pcity, city_tile_index, o)
2225  == city_tile_output(pcity, ptile, is_celebrating, o));
2226 #endif // CITY_DEBUGGING
2227  output[o] += city_tile_cache_get_output(pcity, city_tile_index, o);
2228  }
2230  }
2231  }
2233 }
2234 
2240  const struct city *pcity, int *output,
2241  const std::vector<std::array<int, O_LAST>> *pcsoutputs)
2242 {
2244  {
2245  int count = pcity->specialists[sp];
2246 
2247  // This is more than just an optimization. For governors that forbid
2248  // specialists, the cache may not be filled.
2249  if (count > 0) {
2250  // If there is a cache it must not be empty.
2251  fc_assert_action(!pcsoutputs || !pcsoutputs->empty(),
2252  pcsoutputs = nullptr);
2253 
2254  output_type_iterate(stat_index)
2255  {
2256  int amount = pcsoutputs
2257  ? pcsoutputs->at(sp)[stat_index]
2258  : get_specialist_output(pcity, sp, stat_index);
2259 
2260  output[stat_index] += count * amount;
2261  }
2263  }
2264  }
2266 }
2267 
2276 static inline void set_city_bonuses(struct city *pcity)
2277 {
2279  {
2280  pcity->bonus[o] = get_final_city_output_bonus(pcity, o);
2281  }
2283 }
2284 
2295 static inline void city_tile_cache_update(struct city *pcity)
2296 {
2297  bool is_celebrating = base_city_celebrating(pcity);
2298  int radius_sq = city_map_radius_sq_get(pcity);
2299 
2300  // initialize tile_cache if needed
2301  if (pcity->tile_cache == nullptr || pcity->tile_cache_radius_sq == -1
2302  || pcity->tile_cache_radius_sq != radius_sq) {
2303  pcity->tile_cache = static_cast<tile_cache *>(
2304  fc_realloc(pcity->tile_cache, city_map_tiles(radius_sq)
2305  * sizeof(*(pcity->tile_cache))));
2306  pcity->tile_cache_radius_sq = radius_sq;
2307  }
2308 
2309  /* Any unreal tiles are skipped - these values should have been memset
2310  * to 0 when the city was created. */
2311  city_tile_iterate_index(radius_sq, pcity->tile, ptile, city_tile_index)
2312  {
2314  {
2315  (pcity->tile_cache[city_tile_index]).output[o] =
2316  city_tile_output(pcity, ptile, is_celebrating, o);
2317  }
2319  }
2321 }
2322 
2327 static inline int city_tile_cache_get_output(const struct city *pcity,
2328  int city_tile_index,
2329  enum output_type_id o)
2330 {
2332  pcity->tile_cache_radius_sq == city_map_radius_sq_get(pcity), 0);
2333  fc_assert_ret_val(city_tile_index < city_map_tiles_from_city(pcity), 0);
2334 
2335  return (pcity->tile_cache[city_tile_index]).output[o];
2336 }
2337 
2341 static void set_surpluses(struct city *pcity)
2342 {
2344  {
2345  pcity->surplus[o] = pcity->prod[o] - pcity->usage[o];
2346  }
2348 }
2349 
2353 static void happy_copy(struct city *pcity, enum citizen_feeling i)
2354 {
2355  int c = 0;
2356 
2357  for (; c < CITIZEN_LAST; c++) {
2358  pcity->feel[c][i] = pcity->feel[c][i - 1];
2359  }
2360 }
2361 
2365 static void citizen_base_mood(struct city *pcity)
2366 {
2367  struct player *pplayer = city_owner(pcity);
2368  citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_BASE];
2369  citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_BASE];
2370  citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_BASE];
2371  citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_BASE];
2372  citizens size = city_size_get(pcity);
2373  citizens spes = city_specialists(pcity);
2374 
2375  /* This is the number of citizens that may start out content, depending
2376  * on empire size and game's city unhappysize. This may be bigger than
2377  * the size of the city, since this is a potential. */
2378  citizens base_content = player_content_citizens(pplayer);
2379  // Similarly, this is the potential number of angry citizens.
2380  citizens base_angry = player_angry_citizens(pplayer);
2381 
2382  // Create content citizens. Take specialists from their ranks.
2383  *content = MAX(0, MIN(size, base_content) - spes);
2384 
2385  // Create angry citizens. Specialists never become angry.
2386  fc_assert_action(base_content == 0 || base_angry == 0, *content = 0);
2387  *angry = MIN(base_angry, size - spes);
2388 
2389  /* Create unhappy citizens. In the beginning, all who are not content,
2390  * specialists or angry are unhappy. This is changed by luxuries and
2391  * buildings later. */
2392  *unhappy = (size - spes - *content - *angry);
2393 
2394  // No one is born happy.
2395  *happy = 0;
2396 }
2397 
2404 static inline void citizen_luxury_happy(struct city *pcity, int *luxuries)
2405 {
2406  citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_LUXURY];
2407  citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_LUXURY];
2408  citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_LUXURY];
2409  citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_LUXURY];
2410 
2411  while (*luxuries >= game.info.happy_cost && *angry > 0) {
2412  // Upgrade angry to unhappy: costs HAPPY_COST each.
2413  (*angry)--;
2414  (*unhappy)++;
2415  *luxuries -= game.info.happy_cost;
2416  }
2417  while (*luxuries >= game.info.happy_cost && *content > 0) {
2418  // Upgrade content to happy: costs HAPPY_COST each.
2419  (*content)--;
2420  (*happy)++;
2421  *luxuries -= game.info.happy_cost;
2422  }
2423  while (*luxuries >= 2 * game.info.happy_cost && *unhappy > 0) {
2424  /* Upgrade unhappy to happy. Note this is a 2-level upgrade with
2425  * double the cost. */
2426  (*unhappy)--;
2427  (*happy)++;
2428  *luxuries -= 2 * game.info.happy_cost;
2429  }
2430  if (*luxuries >= game.info.happy_cost && *unhappy > 0) {
2431  // Upgrade unhappy to content: costs HAPPY_COST each.
2432  (*unhappy)--;
2433  (*content)++;
2434  *luxuries -= game.info.happy_cost;
2435  }
2436 }
2437 
2441 static inline void citizen_happy_luxury(struct city *pcity)
2442 {
2443  int x = pcity->prod[O_LUXURY];
2444 
2445  citizen_luxury_happy(pcity, &x);
2446 }
2447 
2451 static inline void citizen_content_buildings(struct city *pcity)
2452 {
2453  citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_EFFECT];
2454  citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_EFFECT];
2455  citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_EFFECT];
2456  int faces = get_city_bonus(pcity, EFT_MAKE_CONTENT);
2457 
2458  /* make people content (but not happy):
2459  get rid of angry first, then make unhappy content. */
2460  while (faces > 0 && *angry > 0) {
2461  (*angry)--;
2462  (*unhappy)++;
2463  faces--;
2464  }
2465  while (faces > 0 && *unhappy > 0) {
2466  (*unhappy)--;
2467  (*content)++;
2468  faces--;
2469  }
2470 }
2471 
2475 static inline void citizen_happiness_nationality(struct city *pcity)
2476 {
2477  citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_NATIONALITY];
2478  citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_NATIONALITY];
2479  citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_NATIONALITY];
2480 
2481  if (game.info.citizen_nationality) {
2482  int pct = get_city_bonus(pcity, EFT_ENEMY_CITIZEN_UNHAPPY_PCT);
2483 
2484  if (pct > 0) {
2485  int enemies = 0;
2486  int unhappy_inc;
2487  struct player *owner = city_owner(pcity);
2488 
2489  citizens_foreign_iterate(pcity, pslot, nationality)
2490  {
2491  if (pplayers_at_war(owner, player_slot_get_player(pslot))) {
2492  enemies += nationality;
2493  }
2494  }
2496 
2497  unhappy_inc = enemies * pct / 100;
2498 
2499  /* First make content => unhappy, then happy => unhappy,
2500  * then happy => content. No-one becomes angry. */
2501  while (unhappy_inc > 0 && *content > 0) {
2502  (*content)--;
2503  (*unhappy)++;
2504  unhappy_inc--;
2505  }
2506  while (unhappy_inc > 1 && *happy > 0) {
2507  (*happy)--;
2508  (*unhappy)++;
2509  unhappy_inc -= 2;
2510  }
2511  while (unhappy_inc > 0 && *happy > 0) {
2512  (*happy)--;
2513  (*content)++;
2514  unhappy_inc--;
2515  }
2516  }
2517  }
2518 }
2519 
2526 static inline void citizen_happy_units(struct city *pcity)
2527 {
2528  citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_MARTIAL];
2529  citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_MARTIAL];
2530  citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_MARTIAL];
2531  citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_MARTIAL];
2532  citizens amt = pcity->martial_law;
2533 
2534  /* Pacify discontent citizens through martial law. First convert
2535  * angry => unhappy, then unhappy => content. */
2536  while (amt > 0 && *angry > 0) {
2537  (*angry)--;
2538  (*unhappy)++;
2539  amt--;
2540  }
2541  while (amt > 0 && *unhappy > 0) {
2542  (*unhappy)--;
2543  (*content)++;
2544  amt--;
2545  }
2546 
2547  /* Now make citizens unhappier because of military units away from home.
2548  * First make content => unhappy, then happy => unhappy,
2549  * then happy => content. */
2550  amt = pcity->unit_happy_upkeep;
2551  while (amt > 0 && *content > 0) {
2552  (*content)--;
2553  (*unhappy)++;
2554  amt--;
2555  }
2556  while (amt > 1 && *happy > 0) {
2557  (*happy)--;
2558  (*unhappy)++;
2559  amt -= 2;
2560  }
2561  while (amt > 0 && *happy > 0) {
2562  (*happy)--;
2563  (*content)++;
2564  amt--;
2565  }
2566  /* Any remaining unhappiness is lost since angry citizens aren't created
2567  * here. */
2568  // FIXME: Why not? - Per
2569 }
2570 
2574 static inline void citizen_happy_wonders(struct city *pcity)
2575 {
2576  citizens *happy = &pcity->feel[CITIZEN_HAPPY][FEELING_FINAL];
2577  citizens *content = &pcity->feel[CITIZEN_CONTENT][FEELING_FINAL];
2578  citizens *unhappy = &pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL];
2579  citizens *angry = &pcity->feel[CITIZEN_ANGRY][FEELING_FINAL];
2580  int bonus = get_city_bonus(pcity, EFT_MAKE_HAPPY);
2581 
2582  /* First create happy citizens from content, then from unhappy
2583  * citizens; we cannot help angry citizens here. */
2584  while (bonus > 0 && *content > 0) {
2585  (*content)--;
2586  (*happy)++;
2587  bonus--;
2588  }
2589  while (bonus > 1 && *unhappy > 0) {
2590  (*unhappy)--;
2591  (*happy)++;
2592  bonus -= 2;
2593  }
2594  // The rest falls through and lets unhappy people become content.
2595 
2596  if (get_city_bonus(pcity, EFT_NO_UNHAPPY) > 0) {
2597  *content += *unhappy + *angry;
2598  *unhappy = 0;
2599  *angry = 0;
2600  return;
2601  }
2602 
2603  bonus += get_city_bonus(pcity, EFT_FORCE_CONTENT);
2604 
2605  // get rid of angry first, then make unhappy content
2606  while (bonus > 0 && *angry > 0) {
2607  (*angry)--;
2608  (*unhappy)++;
2609  bonus--;
2610  }
2611  while (bonus > 0 && *unhappy > 0) {
2612  (*unhappy)--;
2613  (*content)++;
2614  bonus--;
2615  }
2616 }
2617 
2622 static inline void unhappy_city_check(struct city *pcity)
2623 {
2624  if (city_unhappy(pcity)) {
2626  {
2627  switch (output_types[o].unhappy_penalty) {
2628  case UNHAPPY_PENALTY_NONE:
2629  pcity->unhappy_penalty[o] = 0;
2630  break;
2632  pcity->unhappy_penalty[o] = MAX(pcity->prod[o] - pcity->usage[o], 0);
2633  break;
2635  pcity->unhappy_penalty[o] = pcity->prod[o];
2636  break;
2637  }
2638 
2639  pcity->prod[o] -= pcity->unhappy_penalty[o];
2640  }
2642  } else {
2643  memset(pcity->unhappy_penalty, 0,
2644  O_LAST * sizeof(*pcity->unhappy_penalty));
2645  }
2646 }
2647 
2651 int city_pollution_types(const struct city *pcity, int shield_total,
2652  int trade_total, int *pollu_prod, int *pollu_trade,
2653  int *pollu_pop, int *pollu_mod)
2654 {
2655  int prod, trade, pop, mod;
2656 
2657  // Add one one pollution per shield, multipled by the bonus.
2658  prod = 100 + get_city_bonus(pcity, EFT_POLLU_PROD_PCT);
2659  prod = shield_total * MAX(prod, 0) / 100;
2660 
2661  // Add pollution per trade, multipled by the effect.
2662  trade = get_city_bonus(pcity, EFT_POLLU_TRADE_PCT);
2663  trade = trade_total * MAX(trade, 0) / 100;
2664 
2665  // Add one pollution per citizen for baseline combined bonus (100%).
2666  pop = (100 + get_city_bonus(pcity, EFT_POLLU_POP_PCT))
2667  * (100 + get_city_bonus(pcity, EFT_POLLU_POP_PCT_2)) / 100;
2668  pop = (city_size_get(pcity) * MAX(pop, 0)) / 100;
2669 
2670  // Then there is base pollution (usually a negative number).
2671  mod = game.info.base_pollution;
2672 
2673  if (pollu_prod) {
2674  *pollu_prod = prod;
2675  }
2676  if (pollu_trade) {
2677  *pollu_trade = trade;
2678  }
2679  if (pollu_pop) {
2680  *pollu_pop = pop;
2681  }
2682  if (pollu_mod) {
2683  *pollu_mod = mod;
2684  }
2685  return MAX(prod + trade + pop + mod, 0);
2686 }
2687 
2692 int city_pollution(const struct city *pcity, int shield_total,
2693  int trade_total)
2694 {
2695  return city_pollution_types(pcity, shield_total, trade_total, nullptr,
2696  nullptr, nullptr, nullptr);
2697 }
2698 
2706 static int get_trade_illness(const struct city *pcity)
2707 {
2708  float illness_trade = 0.0;
2709 
2711  {
2712  if (trade_city->turn_plague != -1
2713  && game.info.turn - trade_city->turn_plague < 5) {
2714  illness_trade +=
2715  static_cast<float>(game.info.illness_trade_infection)
2716  * sqrt(1.0 * city_size_get(pcity) * city_size_get(trade_city))
2717  / 100.0;
2718  }
2719  }
2721 
2722  return static_cast<int>(illness_trade);
2723 }
2724 
2729 static int get_city_health(const struct city *pcity)
2730 {
2731  return get_city_bonus(pcity, EFT_HEALTH_PCT);
2732 }
2733 
2746 int city_illness_calc(const struct city *pcity, int *ill_base, int *ill_size,
2747  int *ill_trade, int *ill_pollution)
2748 {
2749  int illness_size = 0, illness_trade = 0, illness_pollution = 0;
2750  int illness_base, illness_percent;
2751 
2752  if (game.info.illness_on
2753  && city_size_get(pcity) > game.info.illness_min_size) {
2754  // offset the city size by game.info.illness_min_size
2755  int use_size = city_size_get(pcity) - game.info.illness_min_size;
2756 
2757  illness_size =
2758  static_cast<int>((1.0 - exp(-static_cast<float>(use_size) / 10.0))
2759  * 10.0 * game.info.illness_base_factor);
2760  if (is_server()) {
2761  /* on the server we recalculate the illness due to trade as we have
2762  * all informations */
2763  illness_trade = get_trade_illness(pcity);
2764  } else {
2765  /* on the client we have to rely on the value saved within the city
2766  * struct */
2767  illness_trade = pcity->illness_trade;
2768  }
2769 
2770  illness_pollution =
2771  pcity->pollution * game.info.illness_pollution_factor / 100;
2772  }
2773 
2774  illness_base = illness_size + illness_trade + illness_pollution;
2775  illness_percent = 100 - get_city_health(pcity);
2776 
2777  // returning other data
2778  if (ill_size) {
2779  *ill_size = illness_size;
2780  }
2781 
2782  if (ill_trade) {
2783  *ill_trade = illness_trade;
2784  }
2785 
2786  if (ill_pollution) {
2787  *ill_pollution = illness_pollution;
2788  }
2789 
2790  if (ill_base) {
2791  *ill_base = illness_base;
2792  }
2793 
2794  return CLIP(0, illness_base * illness_percent / 100, 999);
2795 }
2796 
2800 bool city_had_recent_plague(const struct city *pcity)
2801 {
2802  // Correctly handles special case turn_plague == -1 (never)
2803  return (pcity->turn_plague == game.info.turn);
2804 }
2805 
2809 int city_build_slots(const struct city *pcity)
2810 {
2811  return get_city_bonus(pcity, EFT_CITY_BUILD_SLOTS);
2812 }
2813 
2819 int city_airlift_max(const struct city *pcity)
2820 {
2821  return get_city_bonus(pcity, EFT_AIRLIFT);
2822 }
2823 
2830 void set_city_production(struct city *pcity,
2831  const std::vector<city *> &gov_centers,
2832  const std::array<cached_waste, O_LAST> *pcwaste)
2833 {
2834  /* Calculate city production!
2835  *
2836  * This is a rather complicated process if we allow rules to become
2837  * more generalized. We can assume that there are no recursive dependency
2838  * loops, but there are some dependencies that do not follow strict
2839  * ordering. For instance corruption must be calculated before
2840  * trade taxes can be counted up, which must occur before the science bonus
2841  * is added on. But the calculation of corruption must include the
2842  * trade bonus. To do this without excessive special casing means that in
2843  * this case the bonuses are multiplied on twice (but only saved the second
2844  * time).
2845  */
2846 
2847  output_type_iterate(o) { pcity->prod[o] = pcity->citizen_base[o]; }
2849 
2850  // Add on special extra incomes: trade routes and tithes.
2851  trade_routes_iterate(pcity, proute)
2852  {
2853  struct city *tcity = game_city_by_number(proute->partner);
2854  bool can_trade;
2855 
2856  fc_assert_action(tcity != nullptr, continue);
2857 
2858  can_trade = can_cities_trade(pcity, tcity);
2859 
2860  if (!can_trade) {
2861  enum trade_route_type type = cities_trade_route_type(pcity, tcity);
2862  struct trade_route_settings *settings =
2864 
2865  if (settings->cancelling == TRI_ACTIVE) {
2866  can_trade = true;
2867  }
2868  }
2869 
2870  if (can_trade) {
2871  int value;
2872 
2873  value = trade_base_between_cities(
2874  pcity, game_city_by_number(proute->partner));
2875  proute->value = trade_from_route(pcity, proute, value);
2876  pcity->prod[O_TRADE] +=
2877  proute->value * (100 + get_city_bonus(pcity, EFT_TRADEROUTE_PCT))
2878  / 100;
2879  } else {
2880  proute->value = 0;
2881  }
2882  }
2884  pcity->prod[O_GOLD] += get_city_tithes_bonus(pcity);
2885 
2886  // If there is a cache it must not be empty.
2887  fc_assert_action(!pcwaste || !pcwaste->empty(), pcwaste = nullptr);
2888 
2889  /* Account for waste. Note that waste is calculated before tax income is
2890  * calculated, so if you had "science waste" it would not include taxed
2891  * science. However waste is calculated after the bonuses are multiplied
2892  * on, so shield waste will include shield bonuses. */
2894  {
2895  pcity->waste[o] =
2896  city_waste(pcity, o, pcity->prod[o] * pcity->bonus[o] / 100, nullptr,
2897  gov_centers, pcwaste ? &pcwaste->at(o) : nullptr);
2898  }
2900 
2901  /* Convert trade into science/luxury/gold, and add this on to whatever
2902  * science/luxury/gold is already there. */
2903  add_tax_income(city_owner(pcity),
2904  pcity->prod[O_TRADE] * pcity->bonus[O_TRADE] / 100
2905  - pcity->waste[O_TRADE] - pcity->usage[O_TRADE],
2906  pcity->prod);
2907 
2908  /* Add on effect bonuses and waste. Note that the waste calculation
2909  * (above) already includes the bonus multiplier. */
2911  {
2912  pcity->prod[o] = pcity->prod[o] * pcity->bonus[o] / 100;
2913  pcity->prod[o] -= pcity->waste[o];
2914  }
2916 }
2917 
2921 int city_unit_unhappiness(const unit *punit, int *free_unhappy)
2922 {
2923  struct city *pcity;
2924  const struct unit_type *ut;
2925  struct player *plr;
2926  int happy_cost;
2927 
2928  if (!punit || !free_unhappy) {
2929  return 0;
2930  }
2931 
2932  pcity = game_city_by_number(punit->homecity);
2933  if (pcity == nullptr) {
2934  return 0;
2935  }
2936 
2937  ut = unit_type_get(punit);
2938  plr = unit_owner(punit);
2939  happy_cost = utype_happy_cost(ut, plr);
2940 
2941  if (happy_cost <= 0) {
2942  return 0;
2943  }
2944 
2945  fc_assert_ret_val(0 <= *free_unhappy, 0);
2946 
2947  if (!unit_being_aggressive(punit) && !is_field_unit(punit)) {
2948  return 0;
2949  }
2950 
2951  happy_cost -= get_city_bonus(pcity, EFT_MAKE_CONTENT_MIL_PER);
2952  if (happy_cost <= 0) {
2953  return 0;
2954  }
2955 
2956  if (*free_unhappy >= happy_cost) {
2957  *free_unhappy -= happy_cost;
2958  return 0;
2959  } else {
2960  happy_cost -= *free_unhappy;
2961  *free_unhappy = 0;
2962  }
2963 
2964  return happy_cost;
2965 }
2966 
2971 static inline void city_support(struct city *pcity)
2972 {
2973  int free_unhappy, martial_law_each;
2974 
2975  // Clear all usage values.
2976  memset(pcity->usage, 0, O_LAST * sizeof(*pcity->usage));
2977  pcity->martial_law = 0;
2978  pcity->unit_happy_upkeep = 0;
2979 
2980  /* Building and unit gold upkeep depends on the setting
2981  * 'game.info.gold_upkeep_style':
2982  * GOLD_UPKEEP_CITY: The upkeep for buildings and units is paid by the
2983  * city.
2984  * GOLD_UPKEEP_MIXED: The upkeep for buildings is paid by the city.
2985  * The upkeep for units is paid by the nation.
2986  * GOLD_UPKEEP_NATION: The upkeep for buildings and units is paid by the
2987  * nation. */
2988  fc_assert_msg(gold_upkeep_style_is_valid(game.info.gold_upkeep_style),
2989  "Invalid gold_upkeep_style %d", game.info.gold_upkeep_style);
2990  switch (game.info.gold_upkeep_style) {
2991  case GOLD_UPKEEP_CITY:
2992  pcity->usage[O_GOLD] += city_total_unit_gold_upkeep(pcity);
2993  fc__fallthrough; // No break
2994  case GOLD_UPKEEP_MIXED:
2995  pcity->usage[O_GOLD] += city_total_impr_gold_upkeep(pcity);
2996  break;
2997  case GOLD_UPKEEP_NATION:
2998  // nothing
2999  break;
3000  }
3001  // Food consumption by citizens.
3002  pcity->usage[O_FOOD] += game.info.food_cost * city_size_get(pcity);
3003 
3004  /* military units in this city (need _not_ be home city) can make
3005  * unhappy citizens content */
3006  martial_law_each = get_city_bonus(pcity, EFT_MARTIAL_LAW_EACH);
3007  if (martial_law_each > 0) {
3008  int count = 0;
3009  int martial_law_max = get_city_bonus(pcity, EFT_MARTIAL_LAW_MAX);
3010 
3011  unit_list_iterate(pcity->tile->units, punit)
3012  {
3013  if ((count < martial_law_max || martial_law_max == 0)
3014  && is_military_unit(punit)
3015  && unit_owner(punit) == city_owner(pcity)) {
3016  count++;
3017  }
3018  }
3020 
3021  pcity->martial_law = CLIP(0, count * martial_law_each, MAX_CITY_SIZE);
3022  }
3023 
3024  free_unhappy = get_city_bonus(pcity, EFT_MAKE_CONTENT_MIL);
3025  unit_list_iterate(pcity->units_supported, punit)
3026  {
3027  pcity->unit_happy_upkeep += city_unit_unhappiness(punit, &free_unhappy);
3029  {
3030  if (O_GOLD != o) {
3031  // O_GOLD is handled with "game.info.gold_upkeep_style", see over.
3032  pcity->usage[o] += punit->upkeep[o];
3033  }
3034  }
3036  }
3038 }
3039 
3053  struct city *pcity, bool *workers_map,
3054  const std::vector<city *> &gov_centers,
3055  const std::array<cached_waste, O_LAST> *pcwaste,
3056  const std::vector<std::array<int, O_LAST>> *pcsoutputs)
3057 {
3058  if (workers_map == nullptr) {
3059  // do a full refresh
3060 
3061  // Calculate the bonus[] array values.
3062  set_city_bonuses(pcity);
3063  // Calculate the tile_cache[] values.
3064  city_tile_cache_update(pcity);
3065  // manage settlers, and units
3066  city_support(pcity);
3067  }
3068 
3069  // Calculate output from citizens (uses city_tile_cache_get_output()).
3070  get_worked_tile_output(pcity, pcity->citizen_base, workers_map);
3071  add_specialist_output(pcity, pcity->citizen_base, pcsoutputs);
3072 
3073  set_city_production(pcity, gov_centers, pcwaste);
3074  citizen_base_mood(pcity);
3075  /* Note that pollution is calculated before unhappy_city_check() makes
3076  * deductions for disorder; so a city in disorder still causes pollution */
3077  pcity->pollution =
3078  city_pollution(pcity, pcity->prod[O_SHIELD], pcity->prod[O_TRADE]);
3079 
3080  happy_copy(pcity, FEELING_LUXURY);
3081  citizen_happy_luxury(pcity); // with our new found luxuries
3082 
3083  happy_copy(pcity, FEELING_EFFECT);
3085 
3088 
3089  // Martial law & unrest from units
3090  happy_copy(pcity, FEELING_MARTIAL);
3091  citizen_happy_units(pcity);
3092 
3093  // Building (including wonder) happiness effects
3094  happy_copy(pcity, FEELING_FINAL);
3095  citizen_happy_wonders(pcity);
3096 
3097  unhappy_city_check(pcity);
3098  set_surpluses(pcity);
3099 }
3100 
3108 int city_waste(const struct city *pcity, Output_type_id otype, int total,
3109  int *breakdown, const std::vector<city *> &gov_centers,
3110  const cached_waste *pcwaste)
3111 {
3112  int penalty_waste = 0;
3113  int penalty_size = 0; /* separate notradesize/fulltradesize from normal
3114  * corruption */
3115  int total_eft = total; /* normal corruption calculated on total reduced by
3116  * possible size penalty */
3117 
3118  cached_waste waste;
3119  if (pcwaste) {
3120  waste = *pcwaste;
3121  } else {
3122  waste.level = get_city_output_bonus(pcity, get_output_type(otype),
3123  EFT_OUTPUT_WASTE);
3124  }
3125  bool waste_all = false;
3126 
3127  if (otype == O_TRADE) {
3128  /* FIXME: special case for trade: it is affected by notradesize and
3129  * fulltradesize server settings.
3130  *
3131  * If notradesize and fulltradesize are equal then the city gets no
3132  * trade at that size. */
3133  int notradesize = MIN(game.info.notradesize, game.info.fulltradesize);
3134  int fulltradesize = MAX(game.info.notradesize, game.info.fulltradesize);
3135 
3136  if (city_size_get(pcity) <= notradesize) {
3137  penalty_size = total_eft; // Then no trade income.
3138  } else if (city_size_get(pcity) >= fulltradesize) {
3139  penalty_size = 0;
3140  } else {
3141  penalty_size = total_eft * (fulltradesize - city_size_get(pcity))
3142  / (fulltradesize - notradesize);
3143  }
3144  }
3145 
3146  // Apply corruption only to anything left after tradesize
3147  total_eft -= penalty_size;
3148 
3149  /* Distance-based waste.
3150  * Don't bother calculating if there's nothing left to lose. */
3151  if (total_eft > 0) {
3152  if (!pcwaste) {
3154  pcity, get_output_type(otype), EFT_OUTPUT_WASTE_BY_DISTANCE);
3156  pcity, get_output_type(otype), EFT_OUTPUT_WASTE_BY_REL_DISTANCE);
3157  }
3158  if (waste.by_distance > 0 || waste.by_rel_distance > 0) {
3159  const struct city *gov_center = nullptr;
3160  int min_dist = FC_INFINITY;
3161 
3162  /* Check the special case that city itself is gov center
3163  * before expensive iteration through all cities. */
3164  if (is_gov_center(pcity)) {
3165  gov_center = pcity;
3166  min_dist = 0;
3167  } else {
3168  for (const auto gc : gov_centers) {
3169  // Do not recheck current city
3170  if (gc != pcity) {
3171  int dist = real_map_distance(gc->tile, pcity->tile);
3172 
3173  if (dist < min_dist) {
3174  gov_center = gc;
3175  min_dist = dist;
3176  }
3177  }
3178  }
3179  }
3180 
3181  if (gov_center == nullptr) {
3182  waste_all = true; // no gov center - no income
3183  } else {
3184  waste.level += waste.by_distance * min_dist / 100;
3185  if (waste.by_rel_distance > 0) {
3186  /* Multiply by 50 as an "standard size" for which
3187  * EFT_OUTPUT_WASTE_BY_DISTANCE and
3188  * EFT_OUTPUT_WASTE_BY_REL_DISTANCE would give same result. */
3189  waste.level += waste.by_rel_distance * 50 * min_dist / 100
3190  / MAX(wld.map.xsize, wld.map.ysize);
3191  }
3192  }
3193  }
3194  }
3195 
3196  if (waste_all) {
3197  penalty_waste = total_eft;
3198  } else {
3199  if (!pcwaste) {
3200  waste.relative = get_city_output_bonus(pcity, get_output_type(otype),
3201  EFT_OUTPUT_WASTE_PCT);
3202  }
3203 
3204  /* corruption/waste calculated only for the actually produced amount */
3205  if (waste.level > 0) {
3206  penalty_waste = total_eft * waste.level / 100;
3207  }
3208 
3209  // bonus calculated only for the actually produced amount
3210  penalty_waste -= penalty_waste * waste.relative / 100;
3211 
3212  // Clip
3213  penalty_waste = MIN(MAX(penalty_waste, 0), total_eft);
3214  }
3215 
3216  if (breakdown) {
3217  breakdown[OLOSS_WASTE] = penalty_waste;
3218  breakdown[OLOSS_SIZE] = penalty_size;
3219  }
3220 
3221  // add up total penalty
3222  return penalty_waste + penalty_size;
3223 }
3224 
3228 citizens city_specialists(const struct city *pcity)
3229 {
3230  citizens count = 0;
3231 
3233  {
3234  fc_assert_ret_val(MAX_CITY_SIZE - count > pcity->specialists[sp], 0);
3235  count += pcity->specialists[sp];
3236  }
3238 
3239  return count;
3240 }
3241 
3248  const struct city *pcity)
3249 {
3250  int best = DEFAULT_SPECIALIST;
3251  int val = get_specialist_output(pcity, best, otype);
3252 
3254  {
3255  if (!pcity || city_can_use_specialist(pcity, i)) {
3256  int val2 = get_specialist_output(pcity, i, otype);
3257 
3258  if (val2 > val) {
3259  best = i;
3260  val = val2;
3261  }
3262  }
3263  }
3265 
3266  return best;
3267 }
3268 
3272 void city_add_improvement(struct city *pcity,
3273  const struct impr_type *pimprove)
3274 {
3275  pcity->built[improvement_index(pimprove)].turn =
3276  game.info.turn; /*I_ACTIVE*/
3277 
3278  if (is_server() && is_wonder(pimprove)) {
3279  // Client just read the info from the packets.
3280  wonder_built(pcity, pimprove);
3281  }
3282 }
3283 
3287 void city_remove_improvement(struct city *pcity,
3288  const struct impr_type *pimprove)
3289 {
3290  log_debug("Improvement %s removed from city %s",
3291  improvement_rule_name(pimprove), pcity->name);
3292 
3293  pcity->built[improvement_index(pimprove)].turn = I_DESTROYED;
3294 
3295  if (is_server() && is_wonder(pimprove)) {
3296  // Client just read the info from the packets.
3297  wonder_destroyed(pcity, pimprove);
3298  }
3299 }
3300 
3304 bool is_city_option_set(const struct city *pcity, enum city_options option)
3305 {
3306  return BV_ISSET(pcity->city_options, option);
3307 }
3308 
3312 void city_styles_alloc(int num)
3313 {
3314  int i;
3315 
3316  city_styles = new citystyle[num]();
3317  game.control.styles_count = num;
3318 
3319  for (i = 0; i < game.control.styles_count; i++) {
3320  requirement_vector_init(&city_styles[i].reqs);
3321  }
3322 }
3323 
3328 {
3329  int i;
3330 
3331  for (i = 0; i < game.control.styles_count; i++) {
3332  requirement_vector_free(&city_styles[i].reqs);
3333  }
3334 
3335  delete[] city_styles;
3336  city_styles = nullptr;
3337  game.control.styles_count = 0;
3338 }
3339 
3346 struct city *create_city_virtual(struct player *pplayer, struct tile *ptile,
3347  const char *name)
3348 {
3349  int i;
3350 
3351  fc_assert_ret_val(nullptr != name, nullptr); // No unnamed cities!
3352  fc_assert_ret_val(nullptr != pplayer, nullptr); // No unowned cities!
3353 
3354  /* Make sure that contents of city structure are correctly initialized,
3355  * if you ever allocate it by some other mean than fc_calloc() */
3356  struct city *pcity = new city[1]();
3357 
3358  sz_strlcpy(pcity->name, name);
3359 
3360  pcity->tile = ptile;
3361  pcity->owner = pplayer;
3362  pcity->original = pplayer;
3363 
3364  /* City structure was allocated with fc_calloc(), so contents are initially
3365  * zero. There is no need to initialize it a second time. */
3366 
3367  // Now set some usefull default values.
3368  pcity->capital = CAPITAL_NOT;
3369  city_size_set(pcity, 1);
3370  pcity->specialists[DEFAULT_SPECIALIST] = 1;
3371 
3372  output_type_iterate(o) { pcity->bonus[o] = 100; }
3374 
3375  pcity->turn_plague = -1; // -1 = never
3376  pcity->did_buy = false;
3377  pcity->bought_shields = 0;
3378  pcity->city_radius_sq = game.info.init_city_radius_sq;
3379  pcity->turn_founded = game.info.turn;
3380  pcity->turn_last_built = game.info.turn;
3381 
3382  pcity->tile_cache_radius_sq = -1; // -1 = tile_cache must be initialised
3383 
3384  // pcity->ai.act_cache: worker activities on the city map
3385 
3386  // Initialise improvements list
3387  for (i = 0; i < ARRAY_SIZE(pcity->built); i++) {
3388  pcity->built[i].turn = I_NEVER;
3389  }
3390 
3391  // Set up the worklist
3392  worklist_init(&pcity->worklist);
3393 
3394  pcity->units_supported = unit_list_new();
3395  pcity->routes = trade_route_list_new();
3396  pcity->task_reqs = worker_task_list_new();
3397 
3398  if (is_server()) {
3399  pcity->server.mgr_score_calc_turn = -1; // -1 = never
3400 
3401  pcity->server.prod_change_timestamp = time(NULL);
3402  pcity->server.prod_change_turn = game.info.turn;
3403 
3404  CALL_FUNC_EACH_AI(city_alloc, pcity);
3405  } else {
3406  pcity->client.info_units_supported =
3407  unit_list_new_full(unit_virtual_destroy);
3408  pcity->client.info_units_present =
3409  unit_list_new_full(unit_virtual_destroy);
3410  /* collecting_info_units_supported set by fc_calloc().
3411  * collecting_info_units_present set by fc_calloc(). */
3412  }
3413 
3414  return pcity;
3415 }
3416 
3421 void destroy_city_virtual(struct city *pcity)
3422 {
3423  CALL_FUNC_EACH_AI(city_free, pcity);
3424 
3425  citizens_free(pcity);
3426 
3427  while (worker_task_list_size(pcity->task_reqs) > 0) {
3428  struct worker_task *ptask = worker_task_list_get(pcity->task_reqs, 0);
3429 
3430  worker_task_list_remove(pcity->task_reqs, ptask);
3431 
3432  delete ptask;
3433  }
3434  worker_task_list_destroy(pcity->task_reqs);
3435 
3436  unit_list_destroy(pcity->units_supported);
3437  trade_route_list_destroy(pcity->routes);
3438  if (pcity->tile_cache != nullptr) {
3439  free(pcity->tile_cache); // realloc
3440  }
3441  delete pcity->cm_parameter;
3442  pcity->cm_parameter = nullptr;
3443  if (!is_server()) {
3444  unit_list_destroy(pcity->client.info_units_supported);
3445  unit_list_destroy(pcity->client.info_units_present);
3446  /* Handle a rare case where the game is freed in the middle of a
3447  * spy/diplomat investigate cycle. */
3448  if (pcity->client.collecting_info_units_supported != nullptr) {
3449  unit_list_destroy(pcity->client.collecting_info_units_supported);
3450  }
3451  if (pcity->client.collecting_info_units_present != nullptr) {
3452  unit_list_destroy(pcity->client.collecting_info_units_present);
3453  }
3454  }
3455 
3456  memset(pcity, 0, sizeof(*pcity)); // ensure no pointers remain
3457  delete[] pcity;
3458  pcity = nullptr;
3459 }
3460 
3465 bool city_exist(int id)
3466 {
3467  // Check if city exist in game
3468  return game_city_by_number(id) != nullptr;
3469 }
3470 
3478 bool city_is_virtual(const struct city *pcity)
3479 {
3480  if (!pcity) {
3481  return false;
3482  }
3483 
3484  return pcity != game_city_by_number(pcity->id);
3485 }
3486 
3497 bool is_city_center(const struct city *pcity, const struct tile *ptile)
3498 {
3499  if (!pcity || !pcity->tile || !ptile) {
3500  return false;
3501  }
3502 
3503  return tile_index(city_tile(pcity)) == tile_index(ptile);
3504 }
3505 
3509 void *city_ai_data(const struct city *pcity, const struct ai_type *ai)
3510 {
3511  return pcity->server.ais[ai_type_number(ai)];
3512 }
3513 
3517 void city_set_ai_data(struct city *pcity, const struct ai_type *ai,
3518  void *data)
3519 {
3520  pcity->server.ais[ai_type_number(ai)] = data;
3521 }
struct action_enabler_list * action_enablers_for_action(action_id action)
Get all enablers for an action in the current ruleset.
Definition: actions.cpp:1884
#define action_enabler_list_iterate_end
Definition: actions.h:376
#define action_by_result_iterate_end
Definition: actions.h:396
#define action_by_result_iterate(_paction_, _act_id_, _result_)
Definition: actions.h:387
#define action_enabler_list_iterate(action_enabler_list, aenabler)
Definition: actions.h:374
int ai_type_number(const struct ai_type *ai)
Returns id of the given ai_type.
Definition: ai.cpp:47
#define CALL_FUNC_EACH_AI(_func,...)
Definition: ai.h:393
#define BV_CLR_ALL(bv)
Definition: bitvector.h:62
#define BV_SET(bv, bit)
Definition: bitvector.h:44
bool BV_ISSET(const BV &bv, int bit)
Definition: bitvector.h:37
void citizens_free(struct city *pcity)
Free citizens data.
Definition: citizens.cpp:51
#define citizens_foreign_iterate_end
Definition: citizens.h:55
#define citizens_foreign_iterate(_pcity, _pslot, _nationality)
Definition: citizens.h:49
bool base_city_celebrating(const struct city *pcity)
Return TRUE if the city was celebrating at the start of the turn, and it still has sufficient size to...
Definition: city.cpp:1555
bool city_is_virtual(const struct city *pcity)
Return TRUE if the city is a virtual city.
Definition: city.cpp:3478
bool citymindist_prevents_city_on_tile(const struct tile *ptile)
Returns TRUE iff it is illegal to found a city on the specified tile because of citymindist.
Definition: city.cpp:1401
int city_turns_to_build(const struct city *pcity, const struct universal *target, bool include_shield_stock)
Calculates the turns which are needed to build the requested improvement in the city.
Definition: city.cpp:1865
struct output_type * get_output_type(Output_type_id output)
Return the output type for this index.
Definition: city.cpp:611
static void citizen_luxury_happy(struct city *pcity, int *luxuries)
Make people happy: angry citizen are eliminated first then content are made happy,...
Definition: city.cpp:2404
bool city_exists_within_max_city_map(const struct tile *ptile, bool may_be_on_center)
Return TRUE iff a city exists within a city radius of the given location.
Definition: city.cpp:2013
static void citizen_content_buildings(struct city *pcity)
Make citizens content due to city improvements.
Definition: city.cpp:2451
int rs_max_city_radius_sq()
Maximum city radius in this ruleset.
Definition: city.cpp:152
bool city_production_gets_caravan_shields(const struct universal *tgt)
Returns TRUE iff the specified production should get shields from units that has done ACTION_HELP_WON...
Definition: city.cpp:1752
static bv_imprs caravan_helped_impr
Definition: city.cpp:1657
void city_map_radius_sq_set(struct city *pcity, int radius_sq)
Returns the current squared radius of the city.
Definition: city.cpp:141
int city_production_build_shield_cost(const struct city *pcity)
Return the number of shields it takes to build current city production.
Definition: city.cpp:701
bool can_city_build_unit_later(const struct city *pcity, const struct unit_type *punittype)
Returns whether player can eventually build given unit in the city; returns FALSE if unit can never p...
Definition: city.cpp:912
int city_granary_size(int city_size)
Generalized formula used to calculate granary size.
Definition: city.cpp:2034
citizens player_angry_citizens(const struct player *pplayer)
Give base number of angry citizens in any city owned by pplayer.
Definition: city.cpp:2098
static void city_tile_cache_update(struct city *pcity)
This function sets the cache for the tile outputs, the pcity->tile_cache[] array.
Definition: city.cpp:2295
void city_set_ai_data(struct city *pcity, const struct ai_type *ai, void *data)
Attach ai data to city.
Definition: city.cpp:3517
static void get_worked_tile_output(const struct city *pcity, int *output, bool *workers_map)
Calculate output (food, trade and shields) generated by the worked tiles of a city.
Definition: city.cpp:2196
static void citizen_happy_wonders(struct city *pcity)
Make citizens happy due to wonders.
Definition: city.cpp:2574
void add_specialist_output(const struct city *pcity, int *output, const std::vector< std::array< int, O_LAST >> *pcsoutputs)
Calculate output (gold, science, and luxury) generated by the specialists of a city.
Definition: city.cpp:2239
static void city_support(struct city *pcity)
Calculate upkeep costs.
Definition: city.cpp:2971
bool city_built_last_turn(const struct city *pcity)
Return TRUE if the city built something last turn (meaning production was completed between last turn...
Definition: city.cpp:2183
bool can_city_build_now(const struct city *pcity, const struct universal *target)
Returns whether city can immediately build given target, unit or improvement.
Definition: city.cpp:953
void city_styles_free()
De-allocate the memory used by the city styles.
Definition: city.cpp:3327
struct city * city_list_find_number(struct city_list *This, int id)
Find city with given id from list.
Definition: city.cpp:1598
struct city * is_non_allied_city_tile(const struct tile *ptile, const struct player *pplayer)
Is there an non_allied city on this tile?
Definition: city.cpp:1968
int city_build_slots(const struct city *pcity)
The maximum number of units a city can build per turn.
Definition: city.cpp:2809
bool city_has_building(const struct city *pcity, const struct impr_type *pimprove)
Return TRUE iff the city has this building in it.
Definition: city.cpp:1189
struct city * is_enemy_city_tile(const struct tile *ptile, const struct player *pplayer)
Is there an enemy city on this tile?
Definition: city.cpp:1923
struct player * city_owner(const struct city *pcity)
Return the owner of the city.
Definition: city.cpp:1083
int player_base_citizen_happiness(const struct player *pplayer)
Give base happiness in any city owned by pplayer.
Definition: city.cpp:2062
static int city_tile_cache_get_output(const struct city *pcity, int city_tile_index, enum output_type_id o)
This function returns the output of 'o' for the city tile 'city_tile_index' of 'pcity'.
Definition: city.cpp:2327
struct city * is_allied_city_tile(const struct tile *ptile, const struct player *pplayer)
Is there an friendly city on this tile?
Definition: city.cpp:1938
Output_type_id output_type_by_identifier(const char *id)
Find the output type for this output identifier.
Definition: city.cpp:620
bool is_capital(const struct city *pcity)
Return TRUE iff this city is its nation's capital.
Definition: city.cpp:1495
void citylog_map_workers(QtMsgType level, struct city *pcity)
Display the location of the workers within the city map of pcity.
Definition: city.cpp:435
struct citystyle * city_styles
Definition: city.cpp:78
void city_styles_alloc(int num)
Allocate memory for this amount of city styles.
Definition: city.cpp:3312
struct city * is_non_attack_city_tile(const struct tile *ptile, const struct player *pplayer)
Is there an enemy city on this tile?
Definition: city.cpp:1953
bool base_city_can_work_tile(const struct player *restriction, const struct city *pcity, const struct tile *ptile)
Returns TRUE when a tile is available to be worked, or the city itself is currently working the tile ...
Definition: city.cpp:1339
int city_unit_slots_available(const struct city *pcity)
Return number of free unit slots in a city.
Definition: city.cpp:987
void city_remove_improvement(struct city *pcity, const struct impr_type *pimprove)
Removes an improvement (and its effects) from a city.
Definition: city.cpp:3287
int city_improvement_upkeep(const struct city *pcity, const struct impr_type *b)
Return the upkeep (gold) needed each turn to upkeep the given improvement in the given city.
Definition: city.cpp:1202
int city_airlift_max(const struct city *pcity)
A city's maximum airlift capacity.
Definition: city.cpp:2819
void set_city_production(struct city *pcity, const std::vector< city * > &gov_centers, const std::array< cached_waste, O_LAST > *pcwaste)
Set food, trade and shields production in a city.
Definition: city.cpp:2830
bool is_city_option_set(const struct city *pcity, enum city_options option)
Returns TRUE iff the city has set the given option.
Definition: city.cpp:3304
int city_population(const struct city *pcity)
Returns how many thousand citizen live in this city.
Definition: city.cpp:1137
static struct iter_index * city_map_index
Definition: city.cpp:59
struct tile * city_tile(const struct city *pcity)
Return the tile location of the city.
Definition: city.cpp:1095
void city_size_add(struct city *pcity, int add)
Add a (positive or negative) value to the city size.
Definition: city.cpp:1112
bool city_can_use_specialist(const struct city *pcity, Specialist_type_id type)
Returns TRUE iff the given city can use this kind of specialist.
Definition: city.cpp:1005
bool city_production_has_flag(const struct city *pcity, enum impr_flag_id flag)
Return TRUE when the current production has this flag.
Definition: city.cpp:691
bool city_is_occupied(const struct city *pcity)
Returns TRUE iff the city is occupied.
Definition: city.cpp:1584
struct tile * city_map_to_tile(const struct tile *city_center, int city_radius_sq, int city_map_x, int city_map_y)
Finds the map position for a given city map coordinate of a certain city.
Definition: city.cpp:298
bool city_can_be_built_tile_only(const struct tile *ptile)
Returns TRUE if the given unit can build a city at the given map coordinates.
Definition: city.cpp:1477
int city_production_unit_veteran_level(struct city *pcity, const struct unit_type *punittype)
How many veteran levels will created unit of this type get?
Definition: city.cpp:768
void add_tax_income(const struct player *pplayer, int trade, int *output)
Add the incomes of a city according to the taxrates (ignore # of specialists).
Definition: city.cpp:2150
bool can_city_build_improvement_now(const struct city *pcity, const struct impr_type *pimprove)
Return whether given city can build given building; returns FALSE if the building is obsolete.
Definition: city.cpp:815
bool city_tile_index_to_xy(int *city_map_x, int *city_map_y, int city_tile_index, int city_radius_sq)
Returns the coordinates for the given city tile index taking into account the squared city radius.
Definition: city.cpp:95
bool city_got_defense_effect(const struct city *pcity, const struct unit_type *attacker)
This can be City Walls, Coastal defense...
Definition: city.cpp:1512
bool city_can_be_built_here(const struct tile *ptile, const struct unit *punit)
Returns TRUE if the given unit can build a city at the given map coordinates.
Definition: city.cpp:1423
int city_pollution(const struct city *pcity, int shield_total, int trade_total)
Calculate pollution for the city.
Definition: city.cpp:2692
citizens player_content_citizens(const struct player *pplayer)
Give base number of content citizens in any city owned by pplayer.
Definition: city.cpp:2088
bool city_map_includes_tile(const struct city *const pcity, const struct tile *map_tile)
Returns TRUE iff pcity's city map includes the specified tile.
Definition: city.cpp:286
int get_city_tithes_bonus(const struct city *pcity)
Return the amount of gold generated by buildings under "tithe" attribute governments.
Definition: city.cpp:2132
static int city_map_numtiles[CITY_MAP_MAX_RADIUS_SQ+1]
Definition: city.cpp:66
const char * city_name_get(const struct city *pcity)
Return the name of the city.
Definition: city.cpp:1077
bool city_rapture_grow(const struct city *pcity)
Returns whether city is growing by rapture.
Definition: city.cpp:1572
void * city_ai_data(const struct city *pcity, const struct ai_type *ai)
Return pointer to ai data of given city and ai type.
Definition: city.cpp:3509
int city_production_turns_to_build(const struct city *pcity, bool include_shield_stock)
Calculates the turns which are needed to build the requested production in the city.
Definition: city.cpp:784
bool city_unhappy(const struct city *pcity)
Return TRUE iff the city is unhappy.
Definition: city.cpp:1544
Specialist_type_id best_specialist(Output_type_id otype, const struct city *pcity)
Return the "best" specialist available in the game.
Definition: city.cpp:3247
static bv_unit_types caravan_helped_utype
Definition: city.cpp:1658
int get_final_city_output_bonus(const struct city *pcity, Output_type_id otype)
Return the factor (in %) by which the city's output should be multiplied.
Definition: city.cpp:2114
bool city_celebrating(const struct city *pcity)
cities celebrate only after consecutive happy turns
Definition: city.cpp:1564
const char * city_improvement_name_translation(const struct city *pcity, const struct impr_type *pimprove)
Return the extended name of the building.
Definition: city.cpp:635
bool can_city_build_improvement_direct(const struct city *pcity, const struct impr_type *pimprove)
Return whether given city can build given building, ignoring whether it is obsolete.
Definition: city.cpp:795
void generate_city_map_indices()
Fill the arrays city_map_index, city_map_xy and city_map_numtiles.
Definition: city.cpp:499
int city_illness_calc(const struct city *pcity, int *ill_base, int *ill_size, int *ill_trade, int *ill_pollution)
Calculate city's illness in tenth of percent:
Definition: city.cpp:2746
int city_unit_unhappiness(const unit *punit, int *free_unhappy)
Query unhappiness caused by a given unit.
Definition: city.cpp:2921
bool city_can_grow_to(const struct city *pcity, int pop_size)
Return TRUE iff the city can grow to the given size.
Definition: city.cpp:1914
int city_change_production_penalty(const struct city *pcity, const struct universal *target)
Compute and optionally apply the change-production penalty for the given production change (to target...
Definition: city.cpp:1777
static int get_city_health(const struct city *pcity)
Get any effects regarding health from the buildings of the city.
Definition: city.cpp:2729
bool is_valid_city_coords(const int city_radius_sq, const int city_map_x, const int city_map_y)
Return TRUE if the given city coordinate pair is "valid"; that is, if it is a part of the citymap and...
Definition: city.cpp:181
void citylog_map_data(QtMsgType level, int radius_sq, int *map_data)
Display 'map_data' on a city map with the given radius 'radius_sq' for the requested log level.
Definition: city.cpp:404
static void set_city_bonuses(struct city *pcity)
This function sets all the values in the pcity->bonus[] array.
Definition: city.cpp:2276
static void set_surpluses(struct city *pcity)
Set the final surplus[] array from the prod[] and usage[] values.
Definition: city.cpp:2341
bool city_happy(const struct city *pcity)
Return TRUE iff the city is happy.
Definition: city.cpp:1531
void city_size_set(struct city *pcity, citizens size)
Set the city size.
Definition: city.cpp:1126
void city_add_improvement(struct city *pcity, const struct impr_type *pimprove)
Adds an improvement (and its effects) to a city.
Definition: city.cpp:3272
int city_tile_output_now(const struct city *pcity, const struct tile *ptile, Output_type_id otype)
Calculate the production output the given tile is capable of producing for the city.
Definition: city.cpp:1322
int city_map_radius_sq_get(const struct city *pcity)
Returns the current squared radius of the city.
Definition: city.cpp:130
bool can_city_build_direct(const struct city *pcity, const struct universal *target)
Returns whether city can immediately build given target, unit or improvement.
Definition: city.cpp:935
const char * get_output_identifier(Output_type_id output)
Return an id string for the output type.
Definition: city.cpp:592
bool city_base_to_city_map(int *city_map_x, int *city_map_y, const struct city *const pcity, const struct tile *map_tile)
Finds the city map coordinate for a given map position and a city.
Definition: city.cpp:274
void destroy_city_virtual(struct city *pcity)
Removes the virtual skeleton of a city.
Definition: city.cpp:3421
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)
Finds the city map coordinate for a given map position and a city center.
Definition: city.cpp:257
citizens city_specialists(const struct city *pcity)
Give the number of specialists in a city.
Definition: city.cpp:3228
static char * citylog_map_line(int y, int city_radius_sq, int *city_map_data)
Definition: city.cpp:362
struct city * city_list_find_name(struct city_list *This, const char *name)
Find city with given name from list.
Definition: city.cpp:1616
const char * city_style_rule_name(const int style)
Return the (untranslated) rule name of the city style.
Definition: city.cpp:1651
static int cmp(int v1, int v2)
Compare two integer values, as required by qsort.
Definition: city.cpp:317
#define CITYLOG_MAX_VAL
Return one line (y coordinate) of a city map.
Definition: city.cpp:361
void city_choose_build_default(struct city *pcity)
Always tile_set_owner(ptile, pplayer) sometime before this!
Definition: city.cpp:1024
int city_waste(const struct city *pcity, Output_type_id otype, int total, int *breakdown, const std::vector< city * > &gov_centers, const cached_waste *pcwaste)
Give corruption/waste generated by city.
Definition: city.cpp:3108
static void citizen_happiness_nationality(struct city *pcity)
Apply effects of citizen nationality to happiness.
Definition: city.cpp:2475
bool can_city_build_later(const struct city *pcity, const struct universal *target)
Returns whether city can ever build given target, unit or improvement.
Definition: city.cpp:970
int city_tile_output(const struct city *pcity, const struct tile *ptile, bool is_celebrating, Output_type_id otype)
Calculate the output for the tile.
Definition: city.cpp:1230
static int city_map_xy[CITY_MAP_MAX_SIZE][CITY_MAP_MAX_SIZE]
Definition: city.cpp:63
int city_tile_xy_to_index(int city_map_x, int city_map_y, int city_radius_sq)
Returns the index for the given city tile coordinates taking into account the squared city radius.
Definition: city.cpp:117
bool is_gov_center(const struct city *pcity)
Return TRUE iff this city is governmental center.
Definition: city.cpp:1503
#define log_ca_s_init
static void citizen_happy_luxury(struct city *pcity)
Make citizens happy due to luxury.
Definition: city.cpp:2441
void city_refresh_from_main_map(struct city *pcity, bool *workers_map, const std::vector< city * > &gov_centers, const std::array< cached_waste, O_LAST > *pcwaste, const std::vector< std::array< int, O_LAST >> *pcsoutputs)
Refreshes the internal cached data in the city structure.
Definition: city.cpp:3052
int city_map_tiles(int city_radius_sq)
Return the number of tiles for the given city radius.
Definition: city.cpp:164
bool is_unit_near_a_friendly_city(const struct unit *punit)
Return TRUE if there is a friendly city near to this unit (within 3 steps).
Definition: city.cpp:1984
static void unhappy_city_check(struct city *pcity)
Set food, tax, science and shields production to zero if city is in disorder.
Definition: city.cpp:2622
bool can_city_build_unit_direct(const struct city *pcity, const struct unit_type *punittype)
Return whether given city can build given unit, ignoring whether unit is obsolete.
Definition: city.cpp:860
void free_city_map_index()
Free memory allocated by generate_citymap_index.
Definition: city.cpp:581
int city_pollution_types(const struct city *pcity, int shield_total, int trade_total, int *pollu_prod, int *pollu_trade, int *pollu_pop, int *pollu_mod)
Calculate the pollution from production and population in the city.
Definition: city.cpp:2651
bool city_exist(int id)
Check if city with given id still exist.
Definition: city.cpp:3465
bool is_city_center(const struct city *pcity, const struct tile *ptile)
Return TRUE if the city is centered at the given tile.
Definition: city.cpp:3497
bool city_can_work_tile(const struct city *pcity, const struct tile *ptile)
Returns TRUE when a tile is available to be worked, or the city itself is currently working the tile ...
Definition: city.cpp:1392
struct city * create_city_virtual(struct player *pplayer, struct tile *ptile, const char *name)
Create virtual skeleton for a city.
Definition: city.cpp:3346
const char * get_output_name(Output_type_id output)
Return a translated name for the output type.
Definition: city.cpp:602
int compare_iter_index(const void *a, const void *b)
Compare two iter_index values from the city_map_index.
Definition: city.cpp:335
bool city_production_build_units(const struct city *pcity, bool add_production, int *num_units)
Return TRUE if the city could use the additional build slots provided by the effect City_Build_Slots.
Definition: city.cpp:711
int city_turns_to_grow(const struct city *pcity)
Calculates the turns which are needed for the city to grow.
Definition: city.cpp:1897
citizens city_size_get(const struct city *pcity)
Get the city size.
Definition: city.cpp:1101
bool can_city_build_improvement_later(const struct city *pcity, const struct impr_type *pimprove)
Return whether player can eventually build given building in the city; returns FALSE if improvement c...
Definition: city.cpp:832
const char * city_production_name_translation(const struct city *pcity)
Return the extended name of the current production.
Definition: city.cpp:672
struct output_type output_types[O_LAST]
Definition: city.cpp:82
bool city_had_recent_plague(const struct city *pcity)
Returns whether city had a plague outbreak this turn.
Definition: city.cpp:2800
static int get_trade_illness(const struct city *pcity)
Gets whether cities that pcity trades with had the plague.
Definition: city.cpp:2706
bool city_can_change_build(const struct city *pcity)
Returns TRUE iff the given city can change what it is building.
Definition: city.cpp:1016
int city_total_unit_gold_upkeep(const struct city *pcity)
Get the total amount of gold needed to pay upkeep costs for all supported units of the city.
Definition: city.cpp:1168
int city_style_by_rule_name(const char *s)
Returns the city style that has the given (untranslated) rule name.
Definition: city.cpp:1633
static void citizen_base_mood(struct city *pcity)
Create content, unhappy and angry citizens.
Definition: city.cpp:2365
int city_total_impr_gold_upkeep(const struct city *pcity)
Returns the total amount of gold needed to pay for all buildings in the city.
Definition: city.cpp:1147
bool can_city_build_unit_now(const struct city *pcity, const struct unit_type *punittype)
Return whether given city can build given unit; returns FALSE if unit is obsolete.
Definition: city.cpp:894
static void citizen_happy_units(struct city *pcity)
Make citizens happy/unhappy due to units.
Definition: city.cpp:2526
static void happy_copy(struct city *pcity, enum citizen_feeling i)
Copy the happyness array in the city to index i from index i-1.
Definition: city.cpp:2353
void city_production_caravan_shields_init()
Initialize the cache of what city production can use shields from caravans.
Definition: city.cpp:1664
bool is_friendly_city_near(const struct player *owner, const struct tile *ptile)
Return TRUE if there is a friendly city near to this tile (within 3 steps).
Definition: city.cpp:1993
#define city_list_iterate(citylist, pcity)
Definition: city.h:482
#define city_tile_iterate(_radius_sq, _city_tile, _tile)
Definition: city.h:202
@ UNHAPPY_PENALTY_ALL_PRODUCTION
Definition: city.h:228
@ UNHAPPY_PENALTY_NONE
Definition: city.h:226
@ UNHAPPY_PENALTY_SURPLUS
Definition: city.h:227
#define CITY_MAP_MAX_RADIUS_SQ
Definition: city.h:59
#define CITY_MAP_MAX_SIZE
Definition: city.h:67
#define city_tile_iterate_index_end
Definition: city.h:177
@ CITIZEN_LAST
Definition: city.h:244
@ CITIZEN_ANGRY
Definition: city.h:243
@ CITIZEN_HAPPY
Definition: city.h:240
@ CITIZEN_CONTENT
Definition: city.h:241
@ CITIZEN_UNHAPPY
Definition: city.h:242
#define CITY_MAP_CENTER_RADIUS_SQ
Definition: city.h:62
#define CITY_MAP_MIN_RADIUS_SQ
Definition: city.h:57
#define output_type_iterate(output)
Definition: city.h:764
#define CITY_REL2ABS(_coor)
Definition: city.h:88
#define MAX_CITY_SIZE
Definition: city.h:79
#define city_list_iterate_end
Definition: city.h:484
#define I_DESTROYED
Definition: city.h:220
#define I_NEVER
Definition: city.h:219
citizen_feeling
Definition: city.h:250
@ FEELING_EFFECT
Definition: city.h:253
@ FEELING_LUXURY
Definition: city.h:252
@ FEELING_FINAL
Definition: city.h:256
@ FEELING_BASE
Definition: city.h:251
@ FEELING_NATIONALITY
Definition: city.h:254
@ FEELING_MARTIAL
Definition: city.h:255
@ OLOSS_SIZE
Definition: city.h:263
@ OLOSS_WASTE
Definition: city.h:262
#define city_map_iterate_end
Definition: city.h:148
#define is_city_center_index(city_tile_index)
Definition: city.h:778
production_class_type
Definition: city.h:19
@ PCT_LAST
Definition: city.h:23
@ PCT_UNIT
Definition: city.h:20
@ PCT_NORMAL_IMPROVEMENT
Definition: city.h:21
@ PCT_WONDER
Definition: city.h:22
#define city_map_iterate(_radius_sq, _index, _x, _y)
Definition: city.h:144
#define city_tile_iterate_end
Definition: city.h:209
#define CITY_MAP_MAX_RADIUS
Definition: city.h:52
#define city_built_iterate(_pcity, _p)
Definition: city.h:752
#define city_tile_iterate_index(_radius_sq, _city_tile, _tile, _index)
Definition: city.h:169
#define city_map_tiles_from_city(_pcity)
Definition: city.h:100
#define CITY_ABS2REL(_coor)
Definition: city.h:89
#define city_built_iterate_end
Definition: city.h:759
#define output_type_iterate_end
Definition: city.h:771
void cm_init_citymap()
Initialize the CM citymap data.
Definition: cm.cpp:307
void distribute(int number, int groups, int *ratios, int *result)
Distribute "number" elements into "groups" groups with ratios given by the elements in "ratios".
Definition: distribute.cpp:29
int get_target_bonus_effects(struct effect_list *plist, const struct player *target_player, const struct player *other_player, const struct city *target_city, const struct impr_type *target_building, const struct tile *target_tile, const struct unit *target_unit, const struct unit_type *target_unittype, const struct output_type *target_output, const struct specialist *target_specialist, const struct action *target_action, enum effect_type effect_type, enum vision_layer vision_layer, enum national_intelligence nintel)
Returns the effect bonus of a given type for any target.
Definition: effects.cpp:611
int get_tile_output_bonus(const struct city *pcity, const struct tile *ptile, const struct output_type *poutput, enum effect_type effect_type)
Returns the effect bonus at a tile for given output type (or nullptr for output-type-independent bonu...
Definition: effects.cpp:746
struct @19::@20 reqs
int get_unittype_bonus(const struct player *pplayer, const struct tile *ptile, const struct unit_type *punittype, enum effect_type effect_type, enum vision_layer vision_layer)
Returns the effect bonus that applies at a tile for a given unittype.
Definition: effects.cpp:841
int get_city_output_bonus(const struct city *pcity, const struct output_type *poutput, enum effect_type effect_type)
Returns the player effect bonus of an output.
Definition: effects.cpp:800
int get_building_bonus(const struct city *pcity, const struct impr_type *building, enum effect_type effect_type)
Returns the effect bonus at a building.
Definition: effects.cpp:819
int get_city_tile_output_bonus(const struct city *pcity, const struct tile *ptile, const struct output_type *poutput, enum effect_type effect_type)
Returns the effect bonus at a city tile.
Definition: effects.cpp:728
int effect_cumulative_max(enum effect_type type, struct universal *for_uni)
Get the maximum effect value in this ruleset for the universal (that is, the sum of all positive effe...
Definition: effects.cpp:307
int get_city_bonus(const struct city *pcity, enum effect_type effect_type, enum vision_layer vlayer)
Returns the effect bonus at a city.
Definition: effects.cpp:688
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Returns the effect bonus for a player.
Definition: effects.cpp:673
unsigned char citizens
Definition: fc_types.h:305
@ RPT_CERTAIN
Definition: fc_types.h:568
@ RPT_POSSIBLE
Definition: fc_types.h:567
int Specialist_type_id
Definition: fc_types.h:292
output_type_id
Definition: fc_types.h:84
@ O_SHIELD
Definition: fc_types.h:86
@ O_FOOD
Definition: fc_types.h:85
@ O_TRADE
Definition: fc_types.h:87
@ O_SCIENCE
Definition: fc_types.h:90
@ O_LUXURY
Definition: fc_types.h:89
@ O_GOLD
Definition: fc_types.h:88
@ O_LAST
Definition: fc_types.h:91
enum output_type_id Output_type_id
Definition: fc_types.h:295
#define Q_(String)
Definition: fcintl.h:53
#define _(String)
Definition: fcintl.h:50
#define Qn_(String)
Definition: fcintl.h:66
#define N_(String)
Definition: fcintl.h:52
struct civ_game game
Definition: game.cpp:47
bool is_server()
Is program type server?
Definition: game.cpp:57
struct world wld
Definition: game.cpp:48
struct city * game_city_by_number(int id)
Often used function to get a city pointer from a city ID.
Definition: game.cpp:103
struct government * government_of_player(const struct player *pplayer)
Return the government of a player.
Definition: government.cpp:107
bool can_player_build_improvement_direct(const struct player *p, const struct impr_type *pimprove)
Whether player can build given building somewhere, ignoring whether it is obsolete.
const char * improvement_rule_name(const struct impr_type *pimprove)
Return the (untranslated) rule name of the improvement.
bool is_improvement_redundant(const struct city *pcity, const struct impr_type *pimprove)
Returns TRUE if an improvement in a city is redundant, that is, the city wouldn't lose anything by lo...
bool can_player_build_improvement_later(const struct player *p, const struct impr_type *pimprove)
Whether player can eventually build given building somewhere – i.e., returns TRUE if building is avai...
void wonder_built(const struct city *pcity, const struct impr_type *pimprove)
Build a wonder in the city.
bool great_wonder_is_destroyed(const struct impr_type *pimprove)
Returns whether this wonder has been destroyed.
Impr_type_id improvement_index(const struct impr_type *pimprove)
Return the improvement index.
bool is_wonder(const struct impr_type *pimprove)
Returns whether improvement is some kind of wonder.
bool is_great_wonder(const struct impr_type *pimprove)
Is this building a great wonder?
bool improvement_obsolete(const struct player *pplayer, const struct impr_type *pimprove, const struct city *pcity)
Returns TRUE if the improvement or wonder is obsolete.
bool improvement_has_flag(const struct impr_type *pimprove, enum impr_flag_id flag)
Return TRUE if the impr has this flag otherwise FALSE.
void wonder_destroyed(const struct city *pcity, const struct impr_type *pimprove)
Remove a wonder from a city and destroy it if it's a great wonder.
const char * improvement_name_translation(const struct impr_type *pimprove)
Return the (translated) name of the given improvement.
bool great_wonder_is_available(const struct impr_type *pimprove)
Returns whether this wonder can be currently built.
#define improvement_iterate_end
Definition: improvement.h:199
#define improvement_iterate(_p)
Definition: improvement.h:193
const char * name
Definition: inputfile.cpp:118
#define fc_assert_msg(condition, message,...)
Definition: log.h:96
constexpr auto LOG_DEBUG
Definition: log.h:27
#define fc_assert_ret(condition)
Definition: log.h:112
#define fc_assert(condition)
Definition: log.h:89
#define fc_assert_ret_val(condition, val)
Definition: log.h:114
#define fc_assert_action(condition, action)
Definition: log.h:104
#define log_debug(message,...)
Definition: log.h:65
#define log_base(level, message,...)
Definition: log.h:41
struct tile * map_pos_to_tile(const struct civ_map *nmap, int map_x, int map_y)
Return the tile for the given cartesian (map) position.
Definition: map.cpp:391
int map_vector_to_sq_distance(int dx, int dy)
Return the sq_distance for a given vector.
Definition: map.cpp:583
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Are (x1,y1) and (x2,y2) really the same when adjusted? This function might be necessary ALOT of place...
Definition: map.cpp:887
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Return real distance between two tiles.
Definition: map.cpp:599
void map_distance_vector(int *dx, int *dy, const struct tile *tile0, const struct tile *tile1)
Topology function to find the vector which has the minimum "real" distance between the map positions ...
Definition: map.cpp:1012
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition: map.h:312
#define square_iterate_end
Definition: map.h:315
#define index_to_map_pos(pmap_x, pmap_y, mindex)
Definition: map.h:164
bool can_unit_exist_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Return TRUE iff the unit can "exist" at this location.
Definition: movement.cpp:267
bool is_native_near_tile(const struct civ_map *nmap, const struct unit_class *uclass, const struct tile *ptile)
Is there native tile adjacent to given tile.
Definition: movement.cpp:424
static const char * rule_name_get(const struct name_translation *ptrans)
struct city_list * cities
Definition: packhand.cpp:122
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players can attack each other.
Definition: player.cpp:1317
struct player * player_slot_get_player(const struct player_slot *pslot)
Returns the team corresponding to the slot.
Definition: player.cpp:384
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players are allied.
Definition: player.cpp:1334
bool pplayers_non_attack(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players have peace, cease-fire, or armistice.
Definition: player.cpp:1388
int universal_build_shield_cost(const struct city *pcity, const struct universal *target)
Return the number of shields it takes to build this universal.
bool is_req_active(const struct player *target_player, const struct player *other_player, const struct city *target_city, const struct impr_type *target_building, const struct tile *target_tile, const struct unit *target_unit, const struct unit_type *target_unittype, const struct output_type *target_output, const struct specialist *target_specialist, const struct action *target_action, const struct requirement *req, const enum req_problem_type prob_type, const enum vision_layer vision_layer, const enum national_intelligence nintel)
Checks the requirement to see if it is active on the given target.
bool is_req_unchanging(const struct requirement *req)
Return TRUE if this is an "unchanging" requirement.
bool are_reqs_active(const struct player *target_player, const struct player *other_player, const struct city *target_city, const struct impr_type *target_building, const struct tile *target_tile, const struct unit *target_unit, const struct unit_type *target_unittype, const struct output_type *target_output, const struct specialist *target_specialist, const struct action *target_action, const struct requirement_vector *reqs, const enum req_problem_type prob_type, const enum vision_layer vision_layer, const enum national_intelligence nintel)
Checks the requirement(s) to see if they are active on the given target.
const char * universal_name_translation(const struct universal *psource, char *buf, size_t bufsz)
Make user-friendly text for the source.
bool req_vec_wants_type(const struct requirement_vector *reqs, enum universals_n kind)
Returns TRUE iff the specified requirement vector has a positive requirement of the specified require...
bool does_req_contradicts_reqs(const struct requirement *req, const struct requirement_vector *vec)
Returns TRUE if the given requirement contradicts the given requirement vector.
#define requirement_vector_iterate_end
Definition: requirements.h:80
#define requirement_vector_iterate(req_vec, preq)
Definition: requirements.h:78
static struct setting settings[]
Definition: settings.cpp:1338
struct setting_list * level[OLEVELS_NUM]
Definition: settings.cpp:167
#define CLIP(lower, current, upper)
Definition: shared.h:51
#define ARRAY_SIZE(x)
Definition: shared.h:79
#define MIN(x, y)
Definition: shared.h:49
#define FC_INFINITY
Definition: shared.h:32
#define MAX(x, y)
Definition: shared.h:48
struct specialist * specialist_by_number(const Specialist_type_id id)
Return the specialist pointer for the given number.
Definition: specialist.cpp:92
int get_specialist_output(const struct city *pcity, Specialist_type_id sp, Output_type_id otype)
Return the output for the specialist type with this output type.
Definition: specialist.cpp:218
#define specialist_type_iterate_end
Definition: specialist.h:73
#define specialist_type_iterate(sp)
Definition: specialist.h:67
#define DEFAULT_SPECIALIST
Definition: specialist.h:37
int step
Definition: specpq.h:83
size_t size
Definition: specvec.h:64
Definition: ai.h:42
int turn
Definition: city.h:218
Used to cache the value of waste effects to speed up governors.
Definition: city.h:278
int level
Definition: city.h:279
int by_rel_distance
Definition: city.h:282
int by_distance
Definition: city.h:281
Definition: city.h:291
struct worker_task_list * task_reqs
Definition: city.h:381
struct tile_cache * tile_cache
Definition: city.h:318
struct city::@15::@18 client
int turn_last_built
Definition: city.h:358
int surplus[O_LAST]
Definition: city.h:324
int food_stock
Definition: city.h:338
struct built_status built[B_LAST]
Definition: city.h:366
struct player * original
Definition: city.h:295
int pollution
Definition: city.h:340
int id
Definition: city.h:296
int last_turns_shield_surplus
Definition: city.h:364
enum capital_type capital
Definition: city.h:298
int disbanded_shields
Definition: city.h:363
int waste[O_LAST]
Definition: city.h:325
int turn_plague
Definition: city.h:345
bv_city_options city_options
Definition: city.h:375
citizens unit_happy_upkeep
Definition: city.h:308
int city_radius_sq
Definition: city.h:346
bool was_happy
Definition: city.h:353
struct player * owner
Definition: city.h:294
int turn_founded
Definition: city.h:357
int citizen_base[O_LAST]
Definition: city.h:328
int caravan_shields
Definition: city.h:362
bool did_buy
Definition: city.h:351
struct trade_route_list * routes
Definition: city.h:313
int usage[O_LAST]
Definition: city.h:329
struct worklist worklist
Definition: city.h:373
int tile_cache_radius_sq
Definition: city.h:321
struct universal production
Definition: city.h:368
int bonus[O_LAST]
Definition: city.h:335
int unhappy_penalty[O_LAST]
Definition: city.h:326
char name[MAX_LEN_CITYNAME]
Definition: city.h:292
citizens size
Definition: city.h:301
int before_change_shields
Definition: city.h:360
struct city::@15::@17 server
int bought_shields
Definition: city.h:350
int style
Definition: city.h:297
citizens feel[CITIZEN_LAST][FEELING_LAST]
Definition: city.h:302
citizens specialists[SP_MAX]
Definition: city.h:305
struct tile * tile
Definition: city.h:293
int shield_stock
Definition: city.h:339
int prod[O_LAST]
Definition: city.h:327
struct cm_parameter * cm_parameter
Definition: city.h:394
struct universal changed_from
Definition: city.h:371
struct unit_list * units_supported
Definition: city.h:377
int illness_trade
Definition: city.h:341
int rapture
Definition: city.h:356
citizens martial_law
Definition: city.h:307
struct packet_ruleset_control control
Definition: game.h:74
struct packet_game_info info
Definition: game.h:80
struct government * government_during_revolution
Definition: game.h:85
int xsize
Definition: map_types.h:73
int ysize
Definition: map_types.h:73
int upkeep
Definition: improvement.h:69
struct requirement_vector reqs
Definition: improvement.h:66
int dist
Definition: city.h:83
int dx
Definition: city.h:83
int dy
Definition: city.h:83
The base class for options.
Definition: options.cpp:209
const char * id
Definition: city.h:234
Definition: player.h:231
struct city_list * cities
Definition: player.h:263
struct unit_list * units
Definition: player.h:264
struct player_economic economic
Definition: player.h:266
enum req_range range
Definition: requirements.h:69
struct universal source
Definition: requirements.h:68
int irrigation_food_incr
Definition: terrain.h:201
int output[O_LAST]
Definition: terrain.h:188
int mining_shield_incr
Definition: terrain.h:205
int output[O_LAST]
Definition: city.cpp:70
Definition: tile.h:42
struct unit_list * units
Definition: tile.h:50
struct requirement_vector build_reqs
Definition: unittype.h:485
const struct unit_type * obsoleted_by
Definition: unittype.h:494
int city_slots
Definition: unittype.h:517
Definition: unit.h:134
int homecity
Definition: unit.h:142
enum universals_n kind
Definition: fc_types.h:740
universals_u value
Definition: fc_types.h:739
struct tile * ptile
Definition: workertask.h:16
struct civ_map map
Definition: world_object.h:21
int fc_snprintf(char *str, size_t n, const char *format,...)
See also fc_utf8_snprintf_trunc(), fc_utf8_snprintf_rep().
Definition: support.cpp:537
int fc_strcasecmp(const char *str0, const char *str1)
Compare strings like strcmp(), but ignoring case.
Definition: support.cpp:89
#define sz_strlcpy(dest, src)
Definition: support.h:140
#define fc_realloc(ptr, sz)
Definition: support.h:59
#define sz_strlcat(dest, src)
Definition: support.h:142
#define fc__fallthrough
Definition: support.h:49
#define T_UNKNOWN
Definition: terrain.h:51
#define terrain_has_flag(terr, flag)
Definition: terrain.h:260
int tile_roads_output_bonus(const struct tile *ptile, enum output_type_id o)
Calculate output bonus given by roads.
Definition: tile.cpp:289
int tile_roads_output_incr(const struct tile *ptile, enum output_type_id o)
Calculate output increment given by roads.
Definition: tile.cpp:266
enum known_type tile_get_known(const struct tile *ptile, const struct player *pplayer)
Return a known_type enumeration value for the tile.
Definition: tile.cpp:398
struct city * tile_city(const struct tile *ptile)
Return the city on this tile (or nullptr), checking for city center.
Definition: tile.cpp:72
#define tile_index(_pt_)
Definition: tile.h:70
static bool tile_resource_is_valid(const struct tile *ptile)
Definition: tile.h:85
#define tile_worked(_tile)
Definition: tile.h:97
#define tile_resource(_tile)
Definition: tile.h:84
@ TILE_UNKNOWN
Definition: tile.h:29
@ TILE_KNOWN_SEEN
Definition: tile.h:31
#define tile_terrain(_tile)
Definition: tile.h:93
#define tile_owner(_tile)
Definition: tile.h:78
bool can_cities_trade(const struct city *pc1, const struct city *pc2)
Return TRUE iff the two cities are capable of trade; i.e., if a caravan from one city can enter the o...
enum trade_route_type cities_trade_route_type(const struct city *pcity1, const struct city *pcity2)
What is type of the traderoute between two cities.
Definition: traderoutes.cpp:51
int trade_base_between_cities(const struct city *pc1, const struct city *pc2)
Return the trade that exists between these cities, assuming they have a trade route.
int trade_from_route(const struct city *pc1, const struct trade_route *route, int base)
Get trade income specific to route's good.
struct trade_route_settings * trade_route_settings_by_type(enum trade_route_type type)
Get trade route settings related to type.
@ TRI_ACTIVE
Definition: traderoutes.h:24
#define trade_routes_iterate_end
Definition: traderoutes.h:127
#define trade_partners_iterate_end
Definition: traderoutes.h:163
#define trade_routes_iterate(c, proute)
Definition: traderoutes.h:122
#define trade_partners_iterate(c, p)
Definition: traderoutes.h:153
trade_route_type
Definition: traderoutes.h:30
const struct unit_type * utype
Definition: fc_types.h:585
const struct impr_type * building
Definition: fc_types.h:579
bool unit_being_aggressive(const struct unit *punit)
An "aggressive" unit is a unit which may cause unhappiness under a Republic or Democracy.
Definition: unit.cpp:1399
bool is_military_unit(const struct unit *punit)
Military units are capable of enforcing martial law.
Definition: unit.cpp:300
struct unit * unit_occupies_tile(const struct tile *ptile, const struct player *pplayer)
Is there an occupying unit on this tile?
Definition: unit.cpp:1308
bool is_field_unit(const struct unit *punit)
This checks the "field unit" flag on the unit.
Definition: unit.cpp:355
void unit_virtual_destroy(struct unit *punit)
Free the memory used by virtual unit.
Definition: unit.cpp:1588
#define unit_tile(_pu)
Definition: unit.h:371
#define unit_owner(_pu)
Definition: unit.h:370
#define unit_list_iterate(unitlist, punit)
Definition: unitlist.h:25
#define unit_list_iterate_end
Definition: unitlist.h:27
const struct unit_type * unit_type_get(const struct unit *punit)
Return the unit type for this unit.
Definition: unittype.cpp:114
struct unit_type * best_role_unit(const struct city *pcity, int role)
Return "best" unit this city can build, with given role/flag.
Definition: unittype.cpp:1920
int utype_pop_value(const struct unit_type *punittype)
How much city shrinks when it builds unit of this type.
Definition: unittype.cpp:1231
struct unit_type * utype_by_number(const Unit_type_id id)
Return a pointer for the unit type struct for the given unit type id.
Definition: unittype.cpp:103
const char * utype_rule_name(const struct unit_type *punittype)
Return the (untranslated) rule name of the unit type.
Definition: unittype.cpp:1274
int utype_build_shield_cost(const struct city *pcity, const struct unit_type *punittype)
Returns the number of shields it takes to build this unit type.
Definition: unittype.cpp:1141
bool can_player_build_unit_direct(const struct player *p, const struct unit_type *punittype)
Whether player can build given unit somewhere, ignoring whether unit is obsolete and assuming the pla...
Definition: unittype.cpp:1628
int utype_veteran_levels(const struct unit_type *punittype)
Return veteran levels of the given unit type.
Definition: unittype.cpp:2254
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)
Return TRUE iff the unit type can do the specified (action enabler controlled) action while its unit ...
Definition: unittype.cpp:746
Unit_type_id utype_index(const struct unit_type *punittype)
Return the unit type index.
Definition: unittype.cpp:82
bool can_player_build_unit_later(const struct player *p, const struct unit_type *punittype)
Whether player can eventually build given unit somewhere – ie, returns TRUE if unit is available with...
Definition: unittype.cpp:1761
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)
Return TRUE iff the given (action enabler controlled) action can be performed by a unit of the given ...
Definition: unittype.cpp:784
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Return TRUE iff units of the given type can do the specified generalized (ruleset defined) action ena...
Definition: unittype.cpp:386
int utype_happy_cost(const struct unit_type *ut, const struct player *pplayer)
Return the "happy cost" (the number of citizens who are discontented) for this unit.
Definition: unittype.cpp:167
static bool uclass_has_flag(const struct unit_class *punitclass, enum unit_class_flag_id flag)
Definition: unittype.h:704
#define utype_class(_t_)
Definition: unittype.h:691
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition: unittype.h:584
#define unit_type_iterate(_p)
Definition: unittype.h:785
#define unit_type_iterate_end
Definition: unittype.h:791
#define U_NOT_OBSOLETED
Definition: unittype.h:493
void worklist_init(struct worklist *pwl)
Initialize a worklist to be empty.
Definition: worklist.cpp:32
bool worklist_peek_ith(const struct worklist *pwl, struct universal *prod, int idx)
Fill in the id and is_unit values for the ith element in the worklist.
Definition: worklist.cpp:80
int worklist_length(const struct worklist *pwl)
Returns the number of entries in the worklist.
Definition: worklist.cpp:51