Freeciv21
Develop your civilization from humble roots to a global empire
cityturn.cpp
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2023 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 <cmath> // exp, sqrt
15 #include <cstring>
16 
17 // utility
18 #include "fcintl.h"
19 #include "log.h"
20 #include "rand.h"
21 #include "shared.h"
22 #include "support.h"
23 
24 /* common/aicore */
25 #include "cm.h"
26 
27 // common
28 #include "achievements.h"
29 #include "actiontools.h"
30 #include "borders.h"
31 #include "calendar.h"
32 #include "citizens.h"
33 #include "city.h"
34 #include "culture.h"
35 #include "disaster.h"
36 #include "events.h"
37 #include "game.h"
38 #include "government.h"
39 #include "map.h"
40 #include "player.h"
41 #include "research.h"
42 #include "server_settings.h"
43 #include "specialist.h"
44 #include "style.h"
45 #include "tech.h"
46 #include "traderoutes.h"
47 #include "unit.h"
48 #include "unitlist.h"
49 
50 /* common/scriptcore */
51 #include "luascript_types.h"
52 
53 // server
54 #include "citizenshand.h"
55 #include "citytools.h"
56 #include "cityturn.h"
57 #include "maphand.h"
58 #include "notify.h"
59 #include "plrhand.h"
60 #include "sanitycheck.h"
61 #include "spacerace.h"
62 #include "srv_log.h"
63 #include "techtools.h"
64 #include "unittools.h"
65 
66 /* server/advisors */
67 #include "advbuilding.h"
68 #include "advdata.h"
69 
70 /* server/scripting */
71 #include "script_server.h"
72 
73 // Queue for pending city_refresh()
74 static struct city_list *city_refresh_queue = nullptr;
75 
76 /* The game is currently considering to remove the listed units because of
77  * missing gold upkeep. A unit ends up here if it has gold upkeep that
78  * can't be payed. A random unit in the list will be removed until the
79  * problem is solved. */
80 static struct unit_list *uk_rem_gold = nullptr;
81 
82 static void check_pollution(struct city *pcity);
83 static void city_populate(struct city *pcity, struct player *nationality);
84 
85 static bool worklist_change_build_target(struct player *pplayer,
86  struct city *pcity);
87 
88 static bool city_distribute_surplus_shields(struct player *pplayer,
89  struct city *pcity);
90 static void wonder_set_build_turn(struct player *pplayer,
91  const struct impr_type *pimprove);
92 static bool city_build_building(struct player *pplayer, struct city *pcity);
93 static bool city_build_unit(struct player *pplayer, struct city *pcity);
94 static bool city_build_stuff(struct player *pplayer, struct city *pcity);
95 static const struct impr_type *
96 building_upgrades_to(struct city *pcity, const struct impr_type *pimprove);
97 static void upgrade_building_prod(struct city *pcity);
98 static const struct unit_type *unit_upgrades_to(struct city *pcity,
99  const struct unit_type *id);
100 static void upgrade_unit_prod(struct city *pcity);
101 
102 // Helper struct for associating a building to a city.
103 struct cityimpr {
104  struct city *pcity;
106 };
107 
108 #define SPECLIST_TAG cityimpr
109 #define SPECLIST_TYPE struct cityimpr
110 #include "speclist.h"
111 
112 #define cityimpr_list_iterate(cityimprlist, pcityimpr) \
113  TYPED_LIST_ITERATE(struct cityimpr, cityimprlist, pcityimpr)
114 #define cityimpr_list_iterate_end LIST_ITERATE_END
115 
116 static bool sell_random_building(struct player *pplayer,
117  struct cityimpr_list *imprs);
118 static struct unit *sell_random_unit(struct player *pplayer,
119  struct unit_list *punitlist);
120 
121 static citizens city_reduce_specialists(struct city *pcity, citizens change);
122 static citizens city_reduce_workers(struct city *pcity, citizens change);
123 
124 static bool city_balance_treasury_buildings(struct city *pcity);
125 static bool city_balance_treasury_units(struct city *pcity);
126 static bool
128 static bool player_balance_treasury_units(struct player *pplayer);
129 
130 static bool disband_city(struct city *pcity);
131 
132 static void define_orig_production_values(struct city *pcity);
133 static void update_city_activity(struct city *pcity);
134 static void nullify_caravan_and_disband_plus(struct city *pcity);
135 static bool city_illness_check(const struct city *pcity);
136 
137 static float city_migration_score(struct city *pcity);
138 static bool do_city_migration(struct city *pcity_from,
139  struct city *pcity_to);
140 static bool check_city_migrations_player(const struct player *pplayer);
141 static bool city_handle_disorder(struct city *pcity);
142 static bool city_handle_plague_risk(struct city *pcity);
147 bool city_refresh(struct city *pcity)
148 {
149  bool retval;
150 
151  pcity->server.needs_refresh = false;
152 
153  retval = city_map_update_radius_sq(pcity);
154  city_units_upkeep(pcity); // update unit upkeep
155  city_refresh_from_main_map(pcity, nullptr,
157  city_style_refresh(pcity);
158 
159  if (retval) {
160  // Force a sync of the city after the change.
161  send_city_info(city_owner(pcity), pcity);
162  }
163 
164  return retval;
165 }
166 
171 void city_refresh_for_player(struct player *pplayer)
172 {
174  city_list_iterate(pplayer->cities, pcity)
175  {
176  if (city_refresh(pcity)) {
177  auto_arrange_workers(pcity);
178  }
179  send_city_info(pplayer, pcity);
180  }
183 }
184 
188 void city_refresh_queue_add(struct city *pcity)
189 {
190  if (nullptr == city_refresh_queue) {
191  city_refresh_queue = city_list_new();
192  } else if (city_list_find_number(city_refresh_queue, pcity->id)) {
193  return;
194  }
195 
196  city_list_prepend(city_refresh_queue, pcity);
197  pcity->server.needs_refresh = true;
198 }
199 
205 {
206  if (nullptr == city_refresh_queue) {
207  return;
208  }
209 
211  {
212  if (pcity->server.needs_refresh) {
213  if (city_refresh(pcity)) {
214  auto_arrange_workers(pcity);
215  }
216  send_city_info(city_owner(pcity), pcity);
217  }
218  }
220 
221  city_list_destroy(city_refresh_queue);
222  city_refresh_queue = nullptr;
223 }
224 
228 void remove_obsolete_buildings_city(struct city *pcity, bool refresh)
229 {
230  struct player *pplayer = city_owner(pcity);
231  bool sold = false;
232 
233  city_built_iterate(pcity, pimprove)
234  {
235  if (improvement_obsolete(pplayer, pimprove, pcity)
236  && can_city_sell_building(pcity, pimprove)) {
237  int sgold;
238 
239  do_sell_building(pplayer, pcity, pimprove, "obsolete");
240  sgold = impr_sell_gold(pimprove);
241  notify_player(pplayer, city_tile(pcity), E_IMP_SOLD, ftc_server,
242  PL_("%s is selling %s (obsolete) for %d.",
243  "%s is selling %s (obsolete) for %d.", sgold),
244  city_link(pcity), improvement_name_translation(pimprove),
245  sgold);
246  sold = true;
247  }
248  }
250 
251  if (sold && refresh) {
252  if (city_refresh(pcity)) {
253  auto_arrange_workers(pcity);
254  }
255  send_city_info(pplayer, pcity);
256  send_player_info_c(pplayer, nullptr); // Send updated gold to all
257  }
258 }
259 
263 void remove_obsolete_buildings(struct player *pplayer)
264 {
265  city_list_iterate(pplayer->cities, pcity)
266  {
267  remove_obsolete_buildings_city(pcity, false);
268  }
270 }
271 
276 void apply_cmresult_to_city(struct city *pcity,
277  const std::unique_ptr<cm_result> &cmr)
278 {
279  struct tile *pcenter = city_tile(pcity);
280 
281  // Now apply results
283  ptile, idx, x, y)
284  {
285  struct city *pwork = tile_worked(ptile);
286 
287  if (cmr->worker_positions[idx]) {
288  if (nullptr == pwork) {
289  city_map_update_worker(pcity, ptile);
290  } else {
291  fc_assert(pwork == pcity);
292  }
293  } else {
294  if (pwork == pcity) {
295  city_map_update_empty(pcity, ptile);
296  }
297  }
298  }
300 
302  {
303  pcity->specialists[sp] = cmr->specialists[sp];
304  }
306 }
307 
312  struct city *pcity)
313 {
314  cmp->require_happy = false;
315  cmp->allow_disorder = false;
316  cmp->allow_specialists = true;
317 
318  /* We used to look at pplayer->ai.xxx_priority to determine the values
319  * to be used here. However that doesn't work at all because those values
320  * are on a different scale. Later the ai may wish to adjust its
321  * priorities - this should be done via a separate set of variables. */
322  if (city_size_get(pcity) > 1) {
323  if (city_size_get(pcity) <= game.info.notradesize) {
324  cmp->factor[O_FOOD] = 15;
325  } else {
326  if (city_granary_size(city_size_get(pcity)) == pcity->food_stock) {
327  // We don't need more food if the granary is full.
328  cmp->factor[O_FOOD] = 0;
329  } else {
330  cmp->factor[O_FOOD] = 10;
331  }
332  }
333  } else {
334  // Growing to size 2 is the highest priority.
335  cmp->factor[O_FOOD] = 20;
336  }
337  cmp->factor[O_SHIELD] = 5;
338  cmp->factor[O_TRADE] = 0; /* Trade only provides gold/science. */
339  cmp->factor[O_GOLD] = 2;
340  cmp->factor[O_LUXURY] = 0; // Luxury only influences happiness.
341  cmp->factor[O_SCIENCE] = 2;
342  cmp->happy_factor = 0;
343 
344  if (city_granary_size(city_size_get(pcity)) == pcity->food_stock) {
345  cmp->minimal_surplus[O_FOOD] = 0;
346  } else {
347  cmp->minimal_surplus[O_FOOD] = 1;
348  }
349  cmp->minimal_surplus[O_SHIELD] = 1;
350  cmp->minimal_surplus[O_TRADE] = 0;
351  cmp->minimal_surplus[O_GOLD] = -FC_INFINITY;
352  cmp->minimal_surplus[O_LUXURY] = 0;
353  cmp->minimal_surplus[O_SCIENCE] = 0;
354 }
355 
359 void auto_arrange_workers(struct city *pcity)
360 {
361  struct cm_parameter cmp;
362 
363  /* See comment in freeze_workers(): we can't rearrange while
364  * workers are frozen (i.e. multiple updates need to be done). */
365  if (pcity->server.workers_frozen > 0) {
366  pcity->server.needs_arrange = true;
367  return;
368  }
370 
371  /* Freeze the workers and make sure all the tiles around the city
372  * are up to date. Then thaw, but hackishly make sure that thaw
373  * doesn't call us recursively, which would waste time. */
374  city_freeze_workers(pcity);
375  pcity->server.needs_arrange = false;
376 
377  city_map_update_all(pcity);
378 
379  pcity->server.needs_arrange = false;
380  city_thaw_workers(pcity);
381 
382  // Now start actually rearranging.
383  city_refresh(pcity);
384 
385  sanity_check_city(pcity);
386 
388 
389  if (pcity->cm_parameter) {
391  } else {
392  set_default_city_manager(&cmp, pcity);
393  }
394 
395  /* This must be after city_refresh() so that the result gets created for
396  * the right city radius */
397  auto cmr = cm_result_new(pcity);
398  cm_query_result(pcity, &cmp, cmr, false);
399 
400  if (!cmr->found_a_valid) {
401  if (pcity->cm_parameter) {
402  // If player-defined parameters fail, cancel and notify player.
403  delete pcity->cm_parameter;
404  pcity->cm_parameter = nullptr;
405 
406  notify_player(city_owner(pcity), city_tile(pcity), E_CITY_CMA_RELEASE,
407  ftc_server,
408  _("The citizen governor can't fulfill the requirements "
409  "for %s. Passing back control."),
410  city_link(pcity));
411  }
412  // Drop surpluses and try again.
413  cmp.minimal_surplus[O_FOOD] = 0;
414  cmp.minimal_surplus[O_SHIELD] = 0;
415  cmp.minimal_surplus[O_GOLD] = -FC_INFINITY;
416  cm_query_result(pcity, &cmp, cmr, false);
417  }
418  if (!cmr->found_a_valid) {
419  /* Emergency management. Get _some_ result. This doesn't use
420  * cm_init_emergency_parameter so we can keep the factors from
421  * above. */
423  {
424  cmp.minimal_surplus[o] =
425  MIN(cmp.minimal_surplus[o], MIN(pcity->surplus[o], 0));
426  }
428  cmp.require_happy = false;
429  cmp.allow_disorder = !is_ai(city_owner(pcity));
430  cm_query_result(pcity, &cmp, cmr, false);
431  }
432  if (!cmr->found_a_valid) {
433  CITY_LOG(LOG_DEBUG, pcity, "emergency management");
435  cm_query_result(pcity, &cmp, cmr, true);
436  }
437  fc_assert_ret(cmr->found_a_valid);
438 
439  apply_cmresult_to_city(pcity, cmr);
440 
441  if (pcity->server.debug) {
442  // Print debug output if requested.
443  cm_print_city(pcity);
444  cm_print_result(cmr);
445  }
446 
447  if (city_refresh(pcity)) {
448  qCritical("%s radius changed when already arranged workers.",
449  city_name_get(pcity));
450  /* Can't do anything - don't want to enter infinite recursive loop
451  * by trying to arrange workers more. */
452  }
453  sanity_check_city(pcity);
454 
456 }
457 
461 static void city_global_turn_notify(struct conn_list *dest)
462 {
463  cities_iterate(pcity)
464  {
465  const struct impr_type *pimprove = pcity->production.value.building;
466 
467  if (VUT_IMPROVEMENT == pcity->production.kind
468  && is_great_wonder(pimprove)
469  && (1 >= city_production_turns_to_build(pcity, true)
470  && can_city_build_improvement_now(pcity, pimprove))) {
471  notify_conn(dest, city_tile(pcity), E_WONDER_WILL_BE_BUILT, ftc_server,
472  _("Notice: Wonder %s in %s will be finished next turn."),
473  improvement_name_translation(pimprove), city_link(pcity));
474  }
475  }
477 }
478 
483 static void city_turn_notify(const struct city *pcity,
484  struct conn_list *dest,
485  const struct player *cache_for_player)
486 {
487  const struct impr_type *pimprove = pcity->production.value.building;
488  struct packet_chat_msg packet;
489  int turns_growth, turns_granary;
490 
491  if (0 < pcity->surplus[O_FOOD]) {
492  turns_growth =
493  (city_granary_size(city_size_get(pcity)) - pcity->food_stock - 1)
494  / pcity->surplus[O_FOOD];
495 
496  if (0 == get_city_bonus(pcity, EFT_GROWTH_FOOD)
497  && 0 < get_current_construction_bonus(pcity, EFT_GROWTH_FOOD,
498  RPT_CERTAIN)
499  && 0 < pcity->surplus[O_SHIELD]) {
500  // From the check above, the surplus must always be positive.
501  turns_granary =
502  (impr_build_shield_cost(pcity, pimprove) - pcity->shield_stock)
503  / pcity->surplus[O_SHIELD];
504  /* If growth and granary completion occur simultaneously, granary
505  * preserves food. -AJS. */
506  if (5 > turns_growth && 5 > turns_granary
507  && turns_growth < turns_granary) {
509  &packet, city_tile(pcity), E_CITY_GRAN_THROTTLE, ftc_server,
510  _("Suggest throttling growth in %s to use %s "
511  "(being built) more effectively."),
512  city_link(pcity), improvement_name_translation(pimprove));
513  lsend_packet_chat_msg(dest, &packet);
514  if (nullptr != cache_for_player) {
515  event_cache_add_for_player(&packet, cache_for_player);
516  }
517  }
518  }
519 
520  if (0 >= turns_growth && !city_celebrating(pcity)
521  && city_can_grow_to(pcity, city_size_get(pcity) + 1)) {
522  package_event(&packet, city_tile(pcity), E_CITY_MAY_SOON_GROW,
523  ftc_server, _("%s may soon grow to size %i."),
524  city_link(pcity), city_size_get(pcity) + 1);
525  lsend_packet_chat_msg(dest, &packet);
526  if (nullptr != cache_for_player) {
527  event_cache_add_for_player(&packet, cache_for_player);
528  }
529  }
530  } else {
531  if (0 >= pcity->food_stock + pcity->surplus[O_FOOD]
532  && 0 > pcity->surplus[O_FOOD]) {
533  package_event(&packet, city_tile(pcity), E_CITY_FAMINE_FEARED,
534  ftc_server, _("Warning: Famine feared in %s."),
535  city_link(pcity));
536  lsend_packet_chat_msg(dest, &packet);
537  if (nullptr != cache_for_player) {
538  event_cache_add_for_player(&packet, cache_for_player);
539  }
540  }
541  }
542 }
543 
549 {
550  if (nullptr != pconn) {
551  struct player *pplayer = conn_get_player(pconn);
552 
553  if (nullptr != pplayer) {
554  city_list_iterate(pplayer->cities, pcity)
555  {
556  city_turn_notify(pcity, pconn->self, nullptr);
557  }
559  }
561  } else {
562  players_iterate(pplayer)
563  {
564  city_list_iterate(pplayer->cities, pcity)
565  {
566  city_turn_notify(pcity, pplayer->connections, pplayer);
567  }
569  }
571  /* NB: notifications to 'game.est_connections' are automatically
572  * cached. */
574  }
575 }
576 
580 void city_save_surpluses(struct city *pcity)
581 {
582  output_type_iterate(o) { pcity->saved_surplus[o] = pcity->surplus[o]; }
584 }
585 
589 void update_city_activities(struct player *pplayer)
590 {
591  char buf[4 * MAX_LEN_NAME];
592  int n, gold;
593 
594  fc_assert(nullptr != pplayer);
595  fc_assert(nullptr != pplayer->cities);
596 
597  n = city_list_size(pplayer->cities);
598  gold = pplayer->economic.gold;
599  pplayer->server.bulbs_last_turn = 0;
600 
601  if (n > 0) {
602  struct city *cities[n];
603  int i = 0, r;
604  int nation_unit_upkeep = 0;
605  int nation_impr_upkeep = 0;
606 
607  city_list_iterate(pplayer->cities, pcity)
608  {
609  citizens_convert(pcity);
610 
611  // Cancel traderoutes that cannot exist any more
612  trade_routes_iterate_safe(pcity, proute)
613  {
614  struct city *tcity = game_city_by_number(proute->partner);
615 
616  if (tcity != nullptr) {
617  bool cancel = false;
618 
619  if (proute->dir != RDIR_FROM
620  && goods_has_flag(proute->goods, GF_DEPLETES)
621  && !goods_can_be_provided(tcity, proute->goods, nullptr)) {
622  cancel = true;
623  }
624  if (!cancel && !can_cities_trade(pcity, tcity)) {
625  enum trade_route_type type =
626  cities_trade_route_type(pcity, tcity);
629 
630  if (settings->cancelling == TRI_CANCEL) {
631  cancel = true;
632  }
633  }
634 
635  if (cancel) {
636  struct trade_route *back;
637 
638  back = remove_trade_route(pcity, proute, true, false);
639  delete proute;
640  delete back;
641  proute = nullptr;
642  back = nullptr;
643  }
644  }
645  }
647 
648  /* used based on 'gold_upkeep_style', see below */
649  nation_unit_upkeep += city_total_unit_gold_upkeep(pcity);
650  nation_impr_upkeep += city_total_impr_gold_upkeep(pcity);
651 
652  /* New city turn: store surplus values so changes during city
653  * processing don't take effect yet. nation_????_upkeep above
654  * also have similar effect.
655  * Note that changes in effects between players, like international
656  * trade routes and Great Wonders may still take place. */
657  city_save_surpluses(pcity);
658 
659  // Add cities to array for later random order handling
660  cities[i++] = pcity;
661  }
663 
664  // Iterate over cities in a random order.
665  while (i > 0) {
666  r = fc_rand(i);
667  // update unit upkeep
670  cities[r] = cities[--i];
671  }
672 
673  /* How gold upkeep is handled depends on the setting
674  * 'game.info.gold_upkeep_style':
675  * GOLD_UPKEEP_CITY: Each city tries to balance its upkeep individually
676  * (this is done in update_city_activity()).
677  * GOLD_UPKEEP_MIXED: Each city tries to balance its upkeep for
678  * buildings individually; the upkeep for units is
679  * paid by the nation.
680  * GOLD_UPKEEP_NATION: The nation as a whole balances the treasury. If
681  * the treasury is not balance units and buildings
682  * are sold. */
683 
684  if (pplayer->economic.infra_points < 0) {
685  pplayer->economic.infra_points = 0;
686  }
687 
688  switch (game.info.gold_upkeep_style) {
689  case GOLD_UPKEEP_CITY:
690  /* Cities already handled all upkeep costs. */
691  break;
692  case GOLD_UPKEEP_MIXED:
693  // Nation pays for units.
694  pplayer->economic.gold -= nation_unit_upkeep;
695  if (pplayer->economic.gold < 0) {
697  }
698  break;
699  case GOLD_UPKEEP_NATION:
700  // Nation pays for units and buildings.
701  pplayer->economic.gold -= nation_unit_upkeep;
702  pplayer->economic.gold -= nation_impr_upkeep;
703  if (pplayer->economic.gold < 0) {
705  }
706  break;
707  }
708 
709  // Should not happen.
710  fc_assert(pplayer->economic.gold >= 0);
711  }
712 
713  /* This test includes the cost of the units because
714  * units are paid for in update_city_activity() or
715  * player_balance_treasury_units(). */
716  if (gold - (gold - pplayer->economic.gold) * 3 < 0) {
717  notify_player(pplayer, nullptr, E_LOW_ON_FUNDS, ftc_server,
718  _("WARNING, we're LOW on FUNDS %s."),
719  ruler_title_for_player(pplayer, buf, sizeof(buf)));
720  }
721 
723 }
724 
737 static bool upkeep_kill_unit(struct unit *punit, Output_type_id outp,
738  enum unit_loss_reason wipe_reason,
739  bool wipe_in_the_end)
740 {
741  int punit_id;
742 
743  if (!action_auto_perf_unit_sel(AAPC_UNIT_UPKEEP, punit, nullptr,
744  get_output_type(outp))) {
745  /* Can't get rid of this unit. It is undisbandable for the current
746  * situation. */
747  return false;
748  }
749 
750  punit_id = punit->id;
751 
752  // Try to perform this unit's can't upkeep actions.
753  action_auto_perf_unit_do(AAPC_UNIT_UPKEEP, punit, nullptr,
754  get_output_type(outp), nullptr, nullptr, nullptr,
755  nullptr);
756 
757  if (wipe_in_the_end && unit_is_alive(punit_id)) {
758  // No forced action was able to kill the unit. Finish the job.
759  wipe_unit(punit, wipe_reason, nullptr);
760  }
761 
762  return !unit_is_alive(punit_id);
763 }
764 
769 static citizens city_reduce_specialists(struct city *pcity, citizens change)
770 {
771  citizens want = change;
772 
773  fc_assert_ret_val(0 < change, 0);
774 
776  {
777  citizens fix = MIN(want, pcity->specialists[sp]);
778 
779  pcity->specialists[sp] -= fix;
780  want -= fix;
781  }
783 
784  return change - want;
785 }
786 
791 static citizens city_reduce_workers(struct city *pcity, citizens change)
792 {
793  struct tile *pcenter = city_tile(pcity);
794  int want = change;
795 
796  fc_assert_ret_val(0 < change, 0);
797 
799  ptile, _index, _x, _y)
800  {
801  if (0 < want && tile_worked(ptile) == pcity) {
802  city_map_update_empty(pcity, ptile);
803  want--;
804  }
805  }
807 
808  return change - want;
809 }
810 
815 bool city_reduce_size(struct city *pcity, citizens pop_loss,
816  struct player *destroyer, const char *reason)
817 {
818  citizens loss_remain;
819  int old_radius_sq;
820 
821  if (pop_loss == 0) {
822  return true;
823  }
824 
825  if (city_size_get(pcity) <= pop_loss) {
826  script_server_signal_emit("city_destroyed", pcity, pcity->owner,
827  destroyer);
828 
829  remove_city(pcity);
830  return false;
831  }
832  old_radius_sq = tile_border_source_radius_sq(pcity->tile);
833  city_size_add(pcity, -pop_loss);
834  map_update_border(pcity->tile, pcity->owner, old_radius_sq,
836 
837  // Cap the food stock at the new granary size.
838  pcity->food_stock =
839  std::min(pcity->food_stock, city_granary_size(city_size_get(pcity)));
840 
841  // First try to kill off the specialists
842  loss_remain = pop_loss - city_reduce_specialists(pcity, pop_loss);
843 
844  if (loss_remain > 0) {
845  // Take it out on workers
846  loss_remain -= city_reduce_workers(pcity, loss_remain);
847  }
848 
849  // Update citizens.
850  citizens_update(pcity, nullptr);
851 
852  /* Update number of people in each feelings category.
853  * This also updates the city radius if needed. */
854  city_refresh(pcity);
855 
856  auto_arrange_workers(pcity);
857 
858  // Send city data.
859  sync_cities();
860 
861  fc_assert_ret_val_msg(0 == loss_remain, true,
862  "city_reduce_size() has remaining"
863  "%d of %d for \"%s\"[%d]",
864  loss_remain, pop_loss, city_name_get(pcity),
865  city_size_get(pcity));
866 
867  // Update cities that have trade routes with us
868  trade_partners_iterate(pcity, pcity2)
869  {
870  if (city_refresh(pcity2)) {
871  /* This should never happen, but if it does, make sure not to
872  * leave workers outside city radius. */
873  auto_arrange_workers(pcity2);
874  }
875  }
877 
878  sanity_check_city(pcity);
879 
880  if (reason != nullptr) {
881  int id = pcity->id;
882 
883  script_server_signal_emit("city_size_change", pcity, -pop_loss, reason);
884 
885  return city_exist(id);
886  }
887 
888  return true;
889 }
890 
895 void city_repair_size(struct city *pcity, int change)
896 {
897  if (change > 0) {
898  pcity->specialists[DEFAULT_SPECIALIST] += change;
899  } else if (change < 0) {
900  int need = change + city_reduce_specialists(pcity, -change);
901 
902  if (0 > need) {
903  need += city_reduce_workers(pcity, -need);
904  }
905 
906  fc_assert_msg(0 == need,
907  "city_repair_size() has remaining %d of %d for \"%s\"[%d]",
908  need, change, city_name_get(pcity), city_size_get(pcity));
909  }
910 }
911 
918 static int granary_savings(const struct city *pcity)
919 {
920  // Do not empty food stock if city is growing by celebrating
921  if (city_rapture_grow(pcity)) {
922  return 100;
923  }
924  int savings = get_city_bonus(pcity, EFT_GROWTH_FOOD);
925  return CLIP(0, savings, 100);
926 }
927 
934 static int surplus_savings(const struct city *pcity)
935 {
936  int savings = get_city_bonus(pcity, EFT_GROWTH_SURPLUS_PCT);
937  return CLIP(0, savings, 100);
938 }
939 
946 static void city_reset_foodbox(struct city *pcity, int new_size)
947 {
948  fc_assert_ret(pcity != nullptr);
949 
950  const int surplus_food = std::max(
951  pcity->food_stock - city_granary_size(city_size_get(pcity)), 0);
952  const int saved_surplus = (surplus_food * surplus_savings(pcity)) / 100;
953  const int new_granary_size = city_granary_size(new_size);
954  const int saved_by_granary =
955  (new_granary_size * granary_savings(pcity)) / 100;
956  pcity->food_stock =
957  std::min(saved_by_granary + saved_surplus, new_granary_size);
958 }
959 
965 static bool city_increase_size(struct city *pcity,
966  struct player *nationality)
967 {
968  int new_food;
969  int savings_pct = granary_savings(pcity);
970  bool have_square = false;
971  struct tile *pcenter = city_tile(pcity);
972  struct player *powner = city_owner(pcity);
973  const struct impr_type *pimprove = pcity->production.value.building;
974  int saved_id = pcity->id;
975 
976  if (!city_can_grow_to(pcity, city_size_get(pcity) + 1)) {
977  // need improvement
978  if (get_current_construction_bonus(pcity, EFT_SIZE_ADJ, RPT_CERTAIN) > 0
979  || get_current_construction_bonus(pcity, EFT_SIZE_UNLIMIT,
980  RPT_CERTAIN)
981  > 0) {
982  notify_player(powner, city_tile(pcity), E_CITY_IMPROVEMENT_BLDG,
983  ftc_server,
984  _("%s needs %s (being built) to grow beyond size %d."),
985  city_link(pcity), improvement_name_translation(pimprove),
986  city_size_get(pcity));
987  } else {
988  notify_player(powner, city_tile(pcity), E_CITY_IMPROVEMENT, ftc_server,
989  _("%s needs an improvement to grow beyond size %d."),
990  city_link(pcity), city_size_get(pcity));
991  }
992  // Granary can only hold so much
993  new_food =
995  * (100 * 100 - game.server.aqueductloss * (100 - savings_pct))
996  / (100 * 100));
997  pcity->food_stock = std::min(pcity->food_stock, new_food);
998  return false;
999  }
1000 
1001  city_reset_foodbox(pcity, city_size_get(pcity) + 1);
1002  city_size_add(pcity, 1);
1003 
1004  /* If there is enough food, and the city is big enough,
1005  * make new citizens into scientists or taxmen -- Massimo */
1006 
1007  // Ignore food if no square can be worked
1009  ptile, _index, _x, _y)
1010  {
1011  if (tile_worked(ptile) != pcity // quick test
1012  && city_can_work_tile(pcity, ptile)) {
1013  have_square = true;
1014  }
1015  }
1017 
1018  if ((pcity->surplus[O_FOOD] >= 2 || !have_square)
1019  && is_city_option_set(pcity, CITYO_SCIENCE_SPECIALISTS)) {
1020  pcity->specialists[best_specialist(O_SCIENCE, pcity)]++;
1021  } else if ((pcity->surplus[O_FOOD] >= 2 || !have_square)
1022  && is_city_option_set(pcity, CITYO_GOLD_SPECIALISTS)) {
1023  pcity->specialists[best_specialist(O_GOLD, pcity)]++;
1024  } else {
1025  pcity->specialists[DEFAULT_SPECIALIST]++; // or else city is !sane
1026  }
1027 
1028  // Update citizens.
1029  citizens_update(pcity, nationality);
1030 
1031  // Refresh the city data; this also checks the squared city radius.
1032  city_refresh(pcity);
1033 
1034  auto_arrange_workers(pcity);
1035 
1036  // Update cities that have trade routes with us
1037  trade_partners_iterate(pcity, pcity2)
1038  {
1039  if (city_refresh(pcity2)) {
1040  /* This should never happen, but if it does, make sure not to
1041  * leave workers outside city radius. */
1042  auto_arrange_workers(pcity2);
1043  }
1044  }
1046 
1047  notify_player(powner, city_tile(pcity), E_CITY_GROWTH, ftc_server,
1048  _("%s grows to size %d."), city_link(pcity),
1049  city_size_get(pcity));
1050 
1051  /* Deprecated signal. Connect your lua functions to "city_size_change"
1052  * that's emitted from calling functions which know the 'reason' of the
1053  * increase. */
1054  script_server_signal_emit("city_growth", pcity, city_size_get(pcity));
1055  if (city_exist(saved_id)) {
1056  // Script didn't destroy this city
1057  sanity_check_city(pcity);
1058  }
1059  sync_cities();
1060 
1061  return true;
1062 }
1063 
1067 bool city_change_size(struct city *pcity, citizens size,
1068  struct player *nationality, const char *reason)
1069 {
1070  int change = size - city_size_get(pcity);
1071 
1072  if (change > 0) {
1073  int old_size = city_size_get(pcity);
1074  int real_change;
1075 
1076  // Increase city size until size reached, or increase fails
1077  while (size > city_size_get(pcity)
1078  && city_increase_size(pcity, nationality)) {
1079  // city_increase_size() does all the work.
1080  }
1081 
1082  real_change = city_size_get(pcity) - old_size;
1083 
1084  if (real_change != 0 && reason != nullptr) {
1085  int id = pcity->id;
1086 
1087  script_server_signal_emit("city_size_change", pcity, real_change,
1088  reason);
1089 
1090  if (!city_exist(id)) {
1091  return false;
1092  }
1093  }
1094  } else if (change < 0) {
1095  /* We assume that city_change_size() is never called because
1096  * of enemy actions. If that changes, enemy must be passed
1097  * to city_reduce_size() */
1098  return city_reduce_size(pcity, -change, nullptr, reason);
1099  }
1100 
1101  map_claim_border(pcity->tile, pcity->owner, -1);
1102 
1103  return true;
1104 }
1105 
1110 static void city_populate(struct city *pcity, struct player *nationality)
1111 {
1112  int saved_id = pcity->id;
1113  int granary_size = city_granary_size(city_size_get(pcity));
1114 
1115  /* New city turn: Use saved_surplus here, so that changes from building
1116  * units and buildings don't take effect in the middle of city processing.
1117  */
1118  pcity->food_stock += pcity->saved_surplus[O_FOOD];
1119 
1120  if (pcity->food_stock >= granary_size || city_rapture_grow(pcity)) {
1121  if (city_had_recent_plague(pcity)) {
1122  notify_player(city_owner(pcity), city_tile(pcity), E_CITY_PLAGUE,
1123  ftc_server,
1124  _("A recent plague outbreak prevents growth in %s."),
1125  city_link(pcity));
1126  // Lose excess food
1127  pcity->food_stock = std::min(pcity->food_stock, granary_size);
1128  } else {
1129  bool success;
1130 
1131  success = city_increase_size(pcity, nationality); // Handles food_stock
1132  map_claim_border(pcity->tile, pcity->owner, -1);
1133 
1134  if (success) {
1135  script_server_signal_emit("city_size_change", pcity, 1, "growth");
1136  }
1137  }
1138  } else if (pcity->food_stock < 0) {
1139  /* FIXME: should this depend on units with ability to build
1140  * cities or on units that require food in upkeep?
1141  * I'll assume citybuilders (units that 'contain' 1 pop) -- sjolie
1142  * The above may make more logical sense, but in game terms
1143  * you want to disband a unit that is draining your food
1144  * reserves. Hence, I'll assume food upkeep > 0 units. -- jjm
1145  */
1147  {
1148  if (punit->upkeep[O_FOOD] > 0) {
1149  const char *punit_link = unit_tile_link(punit);
1150 
1151  if (upkeep_kill_unit(punit, O_FOOD, ULR_STARVED,
1152  game.info.muuk_food_wipe)) {
1153  notify_player(city_owner(pcity), city_tile(pcity),
1154  E_UNIT_LOST_MISC, ftc_server,
1155  _("Famine feared in %s, %s lost!"), city_link(pcity),
1156  punit_link);
1157  }
1158 
1159  if (city_exist(saved_id)) {
1160  city_reset_foodbox(pcity, city_size_get(pcity));
1161  }
1162  return;
1163  }
1164  }
1166  if (city_size_get(pcity) > 1) {
1167  notify_player(city_owner(pcity), city_tile(pcity), E_CITY_FAMINE,
1168  ftc_server, _("Famine causes population loss in %s."),
1169  city_link(pcity));
1170  } else {
1171  notify_player(city_owner(pcity), city_tile(pcity), E_CITY_FAMINE,
1172  ftc_server, _("Famine destroys %s entirely."),
1173  city_link(pcity));
1174  }
1175  city_reset_foodbox(pcity, city_size_get(pcity) - 1);
1176  city_reduce_size(pcity, 1, nullptr, "famine");
1177  }
1178 }
1179 
1186 static bool worklist_item_postpone_req_vec(struct universal *target,
1187  struct city *pcity,
1188  struct player *pplayer,
1189  int saved_id)
1190 {
1191  const void *ptarget;
1192  const char *tgt_name;
1193  const struct requirement_vector *build_reqs;
1194  const char *signal_name;
1195 
1196  bool purge = false;
1197  bool known = false;
1198 
1199  switch (target->kind) {
1200  case VUT_UTYPE:
1201  ptarget = target->value.utype;
1202  build_reqs = &target->value.utype->build_reqs;
1203  tgt_name =
1204  utype_name_translation(static_cast<const unit_type *>(ptarget));
1205  signal_name = "unit_cant_be_built";
1206  break;
1207  case VUT_IMPROVEMENT:
1208  ptarget = target->value.building;
1209  build_reqs = &target->value.building->reqs;
1211  pcity, static_cast<const impr_type *>(ptarget));
1212  signal_name = "building_cant_be_built";
1213  break;
1214  default:
1216  (target->kind == VUT_IMPROVEMENT || target->kind == VUT_UTYPE),
1217  false);
1218  return false;
1219  break;
1220  }
1221 
1222  requirement_vector_iterate(build_reqs, preq)
1223  {
1224  if (!is_req_active(pplayer, nullptr, pcity, nullptr, nullptr, nullptr,
1225  nullptr, nullptr, nullptr, nullptr, preq,
1226  RPT_POSSIBLE)) {
1227  known = true;
1228  switch (preq->source.kind) {
1229  case VUT_ADVANCE:
1230  if (preq->present) {
1231  notify_player(
1232  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1233  _("%s can't build %s from the worklist; "
1234  "tech %s not yet available. Postponing..."),
1235  city_link(pcity), tgt_name,
1236  advance_name_translation(preq->source.value.advance));
1237  script_server_signal_emit(signal_name, ptarget, pcity,
1238  "need_tech");
1239  } else {
1240  // While techs can be unlearned, this isn't useful feedback
1241  purge = true;
1242  }
1243  break;
1244  case VUT_TECHFLAG:
1245  if (preq->present) {
1246  notify_player(
1247  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1248  _("%s can't build %s from the worklist; "
1249  "no tech with flag \"%s\" yet available. "
1250  "Postponing..."),
1251  city_link(pcity), tgt_name,
1252  tech_flag_id_name(tech_flag_id(preq->source.value.techflag)));
1253  script_server_signal_emit(signal_name, ptarget, pcity,
1254  "need_techflag");
1255  } else {
1256  // While techs can be unlearned, this isn't useful feedback
1257  purge = true;
1258  }
1259  break;
1260  case VUT_IMPROVEMENT:
1261  if (preq->present) {
1262  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD,
1263  ftc_server,
1264  _("%s can't build %s from the worklist; "
1265  "need to have %s first. Postponing..."),
1266  city_link(pcity), tgt_name,
1268  pcity, preq->source.value.building));
1269  script_server_signal_emit(signal_name, ptarget, pcity,
1270  "need_building");
1271  } else {
1272  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD,
1273  ftc_server,
1274  _("%s can't build %s from the worklist; "
1275  "need to not have %s. Postponing..."),
1276  city_link(pcity), tgt_name,
1278  pcity, preq->source.value.building));
1279  script_server_signal_emit(signal_name, ptarget, pcity,
1280  "have_building");
1281  }
1282  break;
1283  case VUT_IMPR_GENUS:
1284  if (preq->present) {
1285  notify_player(
1286  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1287  _("%s can't build %s from the worklist; "
1288  "need to have %s first. Postponing..."),
1289  city_link(pcity), tgt_name,
1290  impr_genus_id_translated_name(preq->source.value.impr_genus));
1291  script_server_signal_emit(signal_name, ptarget, pcity,
1292  "need_building_genus");
1293  } else {
1294  notify_player(
1295  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1296  _("%s can't build %s from the worklist; "
1297  "need to not have %s. Postponing..."),
1298  city_link(pcity), tgt_name,
1299  impr_genus_id_translated_name(preq->source.value.impr_genus));
1300  script_server_signal_emit(signal_name, ptarget, pcity,
1301  "have_building_genus");
1302  }
1303  break;
1304  case VUT_GOVERNMENT:
1305  if (preq->present) {
1306  notify_player(
1307  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1308  _("%s can't build %s from the worklist; "
1309  "it needs %s government. Postponing..."),
1310  city_link(pcity), tgt_name,
1311  government_name_translation(preq->source.value.govern));
1312  script_server_signal_emit(signal_name, ptarget, pcity,
1313  "need_government");
1314  } else {
1315  notify_player(
1316  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1317  _("%s can't build %s from the worklist; "
1318  "it cannot have %s government. Postponing..."),
1319  city_link(pcity), tgt_name,
1320  government_name_translation(preq->source.value.govern));
1321  script_server_signal_emit(signal_name, ptarget, pcity,
1322  "have_government");
1323  }
1324  break;
1325  case VUT_ACHIEVEMENT:
1326  if (preq->present) {
1327  notify_player(
1328  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1329  _("%s can't build %s from the worklist; "
1330  "it needs \"%s\" achievement. Postponing..."),
1331  city_link(pcity), tgt_name,
1332  achievement_name_translation(preq->source.value.achievement));
1333  script_server_signal_emit(signal_name, ptarget, pcity,
1334  "need_achievement");
1335  } else {
1336  // Can't unachieve things.
1337  purge = true;
1338  }
1339  break;
1340  case VUT_EXTRA:
1341  if (preq->present) {
1342  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD,
1343  ftc_server,
1344  Q_("?extra:%s can't build %s from the worklist; "
1345  "%s is required. Postponing..."),
1346  city_link(pcity), tgt_name,
1347  extra_name_translation(preq->source.value.extra));
1348  script_server_signal_emit(signal_name, ptarget, pcity,
1349  "need_extra");
1350  } else {
1351  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD,
1352  ftc_server,
1353  Q_("?extra:%s can't build %s from the worklist; "
1354  "%s is prohibited. Postponing..."),
1355  city_link(pcity), tgt_name,
1356  extra_name_translation(preq->source.value.extra));
1357  script_server_signal_emit(signal_name, ptarget, pcity,
1358  "have_extra");
1359  }
1360  break;
1361  case VUT_GOOD:
1362  if (preq->present) {
1363  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD,
1364  ftc_server,
1365  Q_("?extra:%s can't build %s from the worklist; "
1366  "%s is required. Postponing..."),
1367  city_link(pcity), tgt_name,
1368  goods_name_translation(preq->source.value.good));
1369  script_server_signal_emit(signal_name, ptarget, pcity,
1370  "need_good");
1371  } else {
1372  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD,
1373  ftc_server,
1374  Q_("?extra:%s can't build %s from the worklist; "
1375  "%s is prohibited. Postponing..."),
1376  city_link(pcity), tgt_name,
1377  goods_name_translation(preq->source.value.good));
1378  script_server_signal_emit(signal_name, ptarget, pcity,
1379  "have_good");
1380  }
1381  break;
1382  case VUT_TERRAIN:
1383  if (preq->present) {
1384  notify_player(
1385  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1386  Q_("?terrain:%s can't build %s from the worklist; "
1387  "%s terrain is required. Postponing..."),
1388  city_link(pcity), tgt_name,
1389  terrain_name_translation(preq->source.value.terrain));
1390  script_server_signal_emit(signal_name, ptarget, pcity,
1391  "need_terrain");
1392  } else {
1393  notify_player(
1394  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1395  Q_("?terrain:%s can't build %s from the worklist; "
1396  "%s terrain is prohibited. Postponing..."),
1397  city_link(pcity), tgt_name,
1398  terrain_name_translation(preq->source.value.terrain));
1399  script_server_signal_emit(signal_name, ptarget, pcity,
1400  "have_terrain");
1401  }
1402  break;
1403  case VUT_NATION:
1404  // Nation can be required at Alliance range, which may change.
1405  if (preq->present) {
1406  notify_player(
1407  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1408  // TRANS: "%s nation" is adjective
1409  Q_("?nation:%s can't build %s from the worklist; "
1410  "%s nation is required. Postponing..."),
1411  city_link(pcity), tgt_name,
1412  nation_adjective_translation(preq->source.value.nation));
1413  script_server_signal_emit(signal_name, ptarget, pcity,
1414  "need_nation");
1415  } else {
1416  notify_player(
1417  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1418  Q_("?nation:%s can't build %s from the worklist; "
1419  "%s nation is prohibited. Postponing..."),
1420  city_link(pcity), tgt_name,
1421  nation_adjective_translation(preq->source.value.nation));
1422  script_server_signal_emit(signal_name, ptarget, pcity,
1423  "have_nation");
1424  }
1425  break;
1426  case VUT_NATIONGROUP:
1427  /* Nation group can be required at Alliance range, which may
1428  * change. */
1429  if (preq->present) {
1430  notify_player(
1431  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1432  // TRANS: "%s nation" is adjective
1433  Q_("?ngroup:%s can't build %s from the worklist; "
1434  "%s nation is required. Postponing..."),
1435  city_link(pcity), tgt_name,
1436  nation_group_name_translation(preq->source.value.nationgroup));
1437  script_server_signal_emit(signal_name, ptarget, pcity,
1438  "need_nationgroup");
1439  } else {
1440  notify_player(
1441  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1442  Q_("?ngroup:%s can't build %s from the worklist; "
1443  "%s nation is prohibited. Postponing..."),
1444  city_link(pcity), tgt_name,
1445  nation_group_name_translation(preq->source.value.nationgroup));
1446  script_server_signal_emit(signal_name, ptarget, pcity,
1447  "have_nationgroup");
1448  }
1449  break;
1450  case VUT_STYLE:
1451  /* FIXME: City styles sometimes change over time, but it isn't
1452  * entirely under player control. Probably better to purge
1453  * with useful explanation. */
1454  if (preq->present) {
1455  notify_player(
1456  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1457  _("%s can't build %s from the worklist; "
1458  "only %s style cities may build this. Postponing..."),
1459  city_link(pcity), tgt_name,
1460  style_name_translation(preq->source.value.style));
1461  script_server_signal_emit(signal_name, ptarget, pcity,
1462  "need_style");
1463  } else {
1464  notify_player(
1465  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1466  _("%s can't build %s from the worklist; "
1467  "%s style cities may not build this. Postponing..."),
1468  city_link(pcity), tgt_name,
1469  style_name_translation(preq->source.value.style));
1470  script_server_signal_emit(signal_name, ptarget, pcity,
1471  "have_style");
1472  }
1473  break;
1474  case VUT_NATIONALITY:
1475  /* FIXME: Changing citizen nationality is hard: purging might be
1476  * more useful in this case. */
1477  if (preq->present) {
1478  notify_player(
1479  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1480  // TRANS: Latter %s is citizen nationality
1481  _("%s can't build %s from the worklist; "
1482  "only city with %s may build this. Postponing..."),
1483  city_link(pcity), tgt_name,
1484  nation_plural_translation(preq->source.value.nationality));
1485  script_server_signal_emit(signal_name, ptarget, pcity,
1486  "need_nationality");
1487  } else {
1488  notify_player(
1489  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1490  // TRANS: Latter %s is citizen nationality
1491  _("%s can't build %s from the worklist; "
1492  "only city without %s may build this. Postponing..."),
1493  city_link(pcity), tgt_name,
1494  nation_plural_translation(preq->source.value.nationality));
1495  script_server_signal_emit(signal_name, ptarget, pcity,
1496  "have_nationality");
1497  }
1498  break;
1499  case VUT_DIPLREL:
1500  if (preq->present) {
1501  notify_player(
1502  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1503  /* TRANS: '%s' is a wide range of relationships;
1504  * e.g., 'Peace', 'Never met', 'Foreign',
1505  * 'Hosts embassy', 'Provided Casus Belli' */
1506  _("%s can't build %s from the worklist; "
1507  "the relationship '%s' is required."
1508  " Postponing..."),
1509  city_link(pcity), tgt_name,
1510  diplrel_name_translation(preq->source.value.diplrel));
1511  script_server_signal_emit(signal_name, ptarget, pcity,
1512  "need_diplrel");
1513  } else {
1514  notify_player(
1515  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1516  _("%s can't build %s from the worklist; "
1517  "the relationship '%s' is prohibited."
1518  " Postponing..."),
1519  city_link(pcity), tgt_name,
1520  diplrel_name_translation(preq->source.value.diplrel));
1521  script_server_signal_emit(signal_name, ptarget, pcity,
1522  "have_diplrel");
1523  }
1524  break;
1525  case VUT_MINSIZE:
1526  if (preq->present) {
1527  notify_player(
1528  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1529  _("%s can't build %s from the worklist; "
1530  "city must be of size %d or larger. "
1531  "Postponing..."),
1532  city_link(pcity), tgt_name, preq->source.value.minsize);
1533  script_server_signal_emit(signal_name, ptarget, pcity,
1534  "need_minsize");
1535  } else {
1536  notify_player(
1537  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1538  _("%s can't build %s from the worklist; "
1539  "city must be of size %d or smaller."
1540  "Postponing..."),
1541  city_link(pcity), tgt_name, (preq->source.value.minsize - 1));
1542  script_server_signal_emit(signal_name, ptarget, pcity,
1543  "need_minsize");
1544  }
1545  break;
1546  case VUT_MINCULTURE:
1547  if (preq->present) {
1548  notify_player(
1549  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1550  _("%s can't build %s from the worklist; "
1551  "city must have culture of %d. Postponing..."),
1552  city_link(pcity), tgt_name, preq->source.value.minculture);
1553  script_server_signal_emit(signal_name, ptarget, pcity,
1554  "need_minculture");
1555  } else {
1556  // What has been written may not be unwritten.
1557  purge = true;
1558  }
1559  break;
1560  case VUT_MINFOREIGNPCT:
1561  if (preq->present) {
1562  notify_player(
1563  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1564  _("%s can't build %s from the worklist; "
1565  "city must have %d%% foreign population. Postponing..."),
1566  city_link(pcity), tgt_name, preq->source.value.minforeignpct);
1567  script_server_signal_emit(signal_name, ptarget, pcity,
1568  "need_minforeignpct");
1569  } else {
1570  notify_player(
1571  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1572  _("%s can't build %s from the worklist; "
1573  "city must have %d%% native population. Postponing..."),
1574  city_link(pcity), tgt_name,
1575  100 - preq->source.value.minforeignpct);
1576  script_server_signal_emit(signal_name, ptarget, pcity,
1577  "need_minforeignpct");
1578  }
1579  break;
1580  case VUT_MINTECHS:
1581  if (preq->present) {
1582  notify_player(
1583  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1584  _("%s can't build %s from the worklist; "
1585  "%d techs must be known. Postponing..."),
1586  city_link(pcity), tgt_name, preq->source.value.min_techs);
1587  script_server_signal_emit(signal_name, ptarget, pcity,
1588  "need_mintechs");
1589  } else {
1590  purge = true;
1591  }
1592  break;
1593  case VUT_MAXTILEUNITS:
1594  if (preq->present) {
1595  notify_player(
1596  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1597  PL_("%s can't build %s from the worklist; "
1598  "more than %d unit on tile."
1599  " Postponing...",
1600  "%s can't build %s from the worklist; "
1601  "more than %d units on tile."
1602  " Postponing...",
1603  preq->source.value.max_tile_units),
1604  city_link(pcity), tgt_name, preq->source.value.max_tile_units);
1605  script_server_signal_emit(signal_name, ptarget, pcity,
1606  "need_tileunits");
1607  } else {
1608  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD,
1609  ftc_server,
1610  PL_("%s can't build %s from the worklist; "
1611  "fewer than %d unit on tile."
1612  " Postponing...",
1613  "%s can't build %s from the worklist; "
1614  "fewer than %d units on tile."
1615  " Postponing...",
1616  preq->source.value.max_tile_units + 1),
1617  city_link(pcity), tgt_name,
1618  preq->source.value.max_tile_units + 1);
1619  script_server_signal_emit(signal_name, ptarget, pcity,
1620  "need_tileunits");
1621  }
1622  break;
1623  case VUT_AI_LEVEL:
1624  // Can't change AI level.
1625  purge = true;
1626  break;
1627  case VUT_TERRAINCLASS:
1628  if (preq->present) {
1629  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD,
1630  ftc_server,
1631  Q_("?terrainclass:%s can't build %s from the "
1632  "worklist; %s terrain is required."
1633  " Postponing..."),
1634  city_link(pcity), tgt_name,
1636  terrain_class(preq->source.value.terrainclass)));
1637  script_server_signal_emit(signal_name, ptarget, pcity,
1638  "need_terrainclass");
1639  } else {
1640  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD,
1641  ftc_server,
1642  Q_("?terrainclass:%s can't build %s from the "
1643  "worklist; %s terrain is prohibited."
1644  " Postponing..."),
1645  city_link(pcity), tgt_name,
1647  terrain_class(preq->source.value.terrainclass)));
1648  script_server_signal_emit(signal_name, ptarget, pcity,
1649  "have_terrainclass");
1650  }
1651  break;
1652  case VUT_TERRFLAG:
1653  if (preq->present) {
1654  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD,
1655  ftc_server,
1656  _("%s can't build %s from the worklist; "
1657  "terrain with \"%s\" flag is required. "
1658  "Postponing..."),
1659  city_link(pcity), tgt_name,
1660  terrain_flag_id_name(terrain_flag_id(
1661  preq->source.value.terrainflag)));
1662  script_server_signal_emit(signal_name, ptarget, pcity,
1663  "need_terrainflag");
1664  } else {
1665  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD,
1666  ftc_server,
1667  _("%s can't build %s from the worklist; "
1668  "terrain with \"%s\" flag is prohibited. "
1669  "Postponing..."),
1670  city_link(pcity), tgt_name,
1671  terrain_flag_id_name(terrain_flag_id(
1672  preq->source.value.terrainflag)));
1673  script_server_signal_emit(signal_name, ptarget, pcity,
1674  "have_terrainflag");
1675  }
1676  break;
1677  case VUT_BASEFLAG:
1678  if (preq->present) {
1679  notify_player(
1680  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1681  _("%s can't build %s from the worklist; "
1682  "base with \"%s\" flag is required. "
1683  "Postponing..."),
1684  city_link(pcity), tgt_name,
1685  base_flag_id_name(base_flag_id(preq->source.value.baseflag)));
1686  script_server_signal_emit(signal_name, ptarget, pcity,
1687  "need_baseflag");
1688  } else {
1689  notify_player(
1690  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1691  _("%s can't build %s from the worklist; "
1692  "base with \"%s\" flag is prohibited. "
1693  "Postponing..."),
1694  city_link(pcity), tgt_name,
1695  base_flag_id_name(base_flag_id(preq->source.value.baseflag)));
1696  script_server_signal_emit(signal_name, ptarget, pcity,
1697  "have_baseflag");
1698  }
1699  break;
1700  case VUT_ROADFLAG:
1701  if (preq->present) {
1702  notify_player(
1703  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1704  _("%s can't build %s from the worklist; "
1705  "road with \"%s\" flag is required. "
1706  "Postponing..."),
1707  city_link(pcity), tgt_name,
1708  road_flag_id_name(road_flag_id(preq->source.value.roadflag)));
1709  script_server_signal_emit(signal_name, ptarget, pcity,
1710  "need_roadflag");
1711  } else {
1712  notify_player(
1713  pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1714  _("%s can't build %s from the worklist; "
1715  "road with \"%s\" flag is prohibited. "
1716  "Postponing..."),
1717  city_link(pcity), tgt_name,
1718  road_flag_id_name(road_flag_id(preq->source.value.roadflag)));
1719  script_server_signal_emit(signal_name, ptarget, pcity,
1720  "have_roadflag");
1721  }
1722  break;
1723  case VUT_EXTRAFLAG:
1724  if (preq->present) {
1725  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD,
1726  ftc_server,
1727  _("%s can't build %s from the worklist; "
1728  "extra with \"%s\" flag is required. "
1729  "Postponing..."),
1730  city_link(pcity), tgt_name,
1731  extra_flag_id_translated_name(
1732  extra_flag_id(preq->source.value.extraflag)));
1733  script_server_signal_emit(signal_name, ptarget, pcity,
1734  "need_extraflag");
1735  } else {
1736  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD,
1737  ftc_server,
1738  _("%s can't build %s from the worklist; "
1739  "extra with \"%s\" flag is prohibited. "
1740  "Postponing..."),
1741  city_link(pcity), tgt_name,
1742  extra_flag_id_translated_name(
1743  extra_flag_id(preq->source.value.extraflag)));
1744  script_server_signal_emit(signal_name, ptarget, pcity,
1745  "have_extraflag");
1746  }
1747  break;
1748  case VUT_UTYPE:
1749  case VUT_UTFLAG:
1750  case VUT_UCLASS:
1751  case VUT_UCFLAG:
1752  case VUT_MINVETERAN:
1753  case VUT_UNITSTATE:
1754  case VUT_ACTIVITY:
1755  case VUT_MINMOVES:
1756  case VUT_MINHP:
1757  case VUT_ACTION:
1758  case VUT_OTYPE:
1759  case VUT_SPECIALIST:
1760  case VUT_TERRAINALTER: // XXX could do this in principle
1761  case VUT_CITYTILE:
1762  case VUT_CITYSTATUS:
1763  case VUT_VISIONLAYER:
1764  case VUT_NINTEL:
1765  // Will only happen with a bogus ruleset.
1766  qCritical("worklist_change_build_target() has bogus preq");
1767  break;
1768  case VUT_MINYEAR:
1769  if (preq->present) {
1770  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD,
1771  ftc_server,
1772  // TRANS: last %s is a date
1773  _("%s can't build %s from the worklist; "
1774  "only available from %s. Postponing..."),
1775  city_link(pcity), tgt_name,
1776  textyear(preq->source.value.minyear));
1777  script_server_signal_emit(signal_name, ptarget, pcity,
1778  "need_minyear");
1779  } else {
1780  // Can't go back in time.
1781  purge = true;
1782  }
1783  break;
1784  case VUT_MINCALFRAG:
1785  /* Unlike VUT_MINYEAR, a requirement in either direction is
1786  * likely to be fulfilled sooner or later. */
1787  if (preq->present) {
1788  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD,
1789  ftc_server,
1790  /* TRANS: last %s is a calendar fragment from
1791  * the ruleset; may be a bare number */
1792  _("%s can't build %s from the worklist; "
1793  "only available from %s. Postponing..."),
1794  city_link(pcity), tgt_name,
1795  textcalfrag(preq->source.value.mincalfrag));
1796  script_server_signal_emit(signal_name, ptarget, pcity,
1797  "need_mincalfrag");
1798  } else {
1799  fc_assert_action(preq->source.value.mincalfrag > 0, break);
1800  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD,
1801  ftc_server,
1802  /* TRANS: last %s is a calendar fragment from
1803  * the ruleset; may be a bare number */
1804  _("%s can't build %s from the worklist; "
1805  "not available after %s. Postponing..."),
1806  city_link(pcity), tgt_name,
1807  textcalfrag(preq->source.value.mincalfrag - 1));
1808  script_server_signal_emit(signal_name, ptarget, pcity,
1809  "have_mincalfrag");
1810  }
1811  break;
1812  case VUT_TOPO:
1813  if (preq->present) {
1814  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD,
1815  ftc_server,
1816  /* TRANS: third %s is topology flag name
1817  * ("WrapX", "ISO", etc) */
1818  _("%s can't build %s from the worklist; "
1819  "only available in worlds with %s map."),
1820  city_link(pcity), tgt_name,
1821  _(topo_flag_name(preq->source.value.topo_property)));
1822  script_server_signal_emit(signal_name, ptarget, pcity,
1823  "need_topo");
1824  }
1825  purge = true;
1826  break;
1827  case VUT_SERVERSETTING:
1828  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD,
1829  ftc_server,
1830  /* TRANS: %s is a server setting, its value and
1831  * if it is required to be present or absent.
1832  * The string's format is specified in
1833  * ssetv_human_readable().
1834  * Example: "killstack is enabled". */
1835  _("%s can't build %s from the worklist; "
1836  "only available when the server setting "
1837  "%s."),
1838  city_link(pcity), tgt_name,
1839  qUtf8Printable(ssetv_human_readable(
1840  preq->source.value.ssetval, preq->present)));
1841  script_server_signal_emit(signal_name, ptarget, pcity,
1842  "need_setting");
1843  // Don't assume that the server setting will be changed.
1844  purge = true;
1845  break;
1846  case VUT_AGE:
1847  if (preq->present) {
1848  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD,
1849  ftc_server,
1850  _("%s can't build %s from the worklist; "
1851  "only available once %d turns old. Postponing..."),
1852  city_link(pcity), tgt_name, preq->source.value.age);
1853  script_server_signal_emit(signal_name, ptarget, pcity, "need_age");
1854  } else {
1855  // Can't go back in time.
1856  purge = true;
1857  }
1858  break;
1859  case VUT_NONE:
1860  case VUT_COUNT:
1861  fc_assert_ret_val_msg(false, true,
1862  "worklist_change_build_target() "
1863  "called with invalid preq");
1864  break;
1865  /* No default handling here, as we want compiler warning
1866  * if new requirement type is added to enum and it's not handled
1867  * here. */
1868  };
1869  break;
1870  }
1871 
1872  // Almost all cases emit signal in the end, so city check needed.
1873  if (!city_exist(saved_id)) {
1874  // Some script has removed city
1875  return true;
1876  }
1877  }
1879 
1880  if (!known) {
1881  /* This shouldn't happen...
1882  FIXME: make can_city_build_improvement_now() return a reason enum. */
1883  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
1884  _("%s can't build %s from the worklist; "
1885  "reason unknown! Postponing..."),
1886  city_link(pcity), tgt_name);
1887  }
1888 
1889  return purge;
1890 }
1891 
1898 static bool worklist_change_build_target(struct player *pplayer,
1899  struct city *pcity)
1900 {
1901  struct universal target;
1902  bool success = false;
1903  int i;
1904  int saved_id = pcity->id;
1905  bool city_checked =
1906  true; // This is used to avoid spurious city_exist() calls
1907  struct worklist *pwl = &pcity->worklist;
1908 
1909  if (worklist_is_empty(pwl)) {
1910  // Nothing in the worklist; bail now.
1911  return false;
1912  }
1913 
1914  i = 0;
1915  while (!success && i < worklist_length(pwl)) {
1916  if (!city_checked) {
1917  if (!city_exist(saved_id)) {
1918  /* Some script has removed useless city that cannot build
1919  * what it is told to! */
1920  return false;
1921  }
1922  city_checked = true;
1923  }
1924 
1925  if (worklist_peek_ith(pwl, &target, i)) {
1926  success = can_city_build_now(pcity, &target);
1927  } else {
1928  success = false;
1929  }
1930  i++;
1931 
1932  if (success) {
1933  break; // while
1934  }
1935 
1936  switch (target.kind) {
1937  case VUT_UTYPE: {
1938  const struct unit_type *ptarget = target.value.utype;
1939  const struct unit_type *pupdate = unit_upgrades_to(pcity, ptarget);
1940  bool purge;
1941  /* Maybe we can just upgrade the target to what the city /can/ build.
1942  */
1943  if (U_NOT_OBSOLETED == pupdate) {
1944  // Nope, we're stuck. Skip this item from the worklist.
1945  if (ptarget->require_advance != nullptr
1946  && TECH_KNOWN
1948  research_get(pplayer),
1949  advance_number(ptarget->require_advance))) {
1950  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD,
1951  ftc_server,
1952  _("%s can't build %s from the worklist; "
1953  "tech %s not yet available. Postponing..."),
1954  city_link(pcity), utype_name_translation(ptarget),
1956  script_server_signal_emit("unit_cant_be_built", ptarget, pcity,
1957  "need_tech");
1958  } else {
1959  // Unknown or requirement from vector.
1960  purge = worklist_item_postpone_req_vec(&target, pcity, pplayer,
1961  saved_id);
1962  }
1963  city_checked = false;
1964  break;
1965  } else {
1966  purge = !can_city_build_unit_later(pcity, pupdate);
1967  }
1968  success = can_city_build_unit_later(pcity, pupdate);
1969  if (purge) {
1970  /* If the city can never build this unit or its descendants,
1971  * drop it. */
1972  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD,
1973  ftc_server,
1974  _("%s can't build %s from the worklist. Purging..."),
1975  city_link(pcity),
1976  /* Yes, warn about the targets that's actually
1977  in the worklist, not its obsolete-closure
1978  pupdate. */
1979  utype_name_translation(ptarget));
1980  script_server_signal_emit("unit_cant_be_built", ptarget, pcity,
1981  "never");
1982  if (city_exist(saved_id)) {
1983  city_checked = true;
1984  // Purge this worklist item.
1985  i--;
1986  worklist_remove(pwl, i);
1987  } else {
1988  city_checked = false;
1989  }
1990  } else {
1991  // Yep, we can go after pupdate instead. Joy!
1992  notify_player(pplayer, city_tile(pcity), E_WORKLIST, ftc_server,
1993  _("Production of %s is upgraded to %s in %s."),
1994  utype_name_translation(ptarget),
1995  utype_name_translation(pupdate), city_link(pcity));
1996  target.value.utype = pupdate;
1997  }
1998  break;
1999  }
2000  case VUT_IMPROVEMENT: {
2001  const struct impr_type *ptarget = target.value.building;
2002  const struct impr_type *pupdate = building_upgrades_to(pcity, ptarget);
2003  bool purge;
2004  // If the city can never build this improvement, drop it.
2005  success = can_city_build_improvement_later(pcity, pupdate);
2006  purge = !success;
2007  /* Maybe this improvement has been obsoleted by something that
2008  we can build. */
2009  if (!success) {
2010  // Nope, no use. *sigh*
2011 
2012  // Can it be postponed?
2013  if (can_city_build_improvement_later(pcity, ptarget)) {
2014  success = worklist_item_postpone_req_vec(&target, pcity, pplayer,
2015  saved_id);
2016 
2017  /* Almost all cases emit signal in the end, so city check needed.
2018  */
2019  if (!city_exist(saved_id)) {
2020  // Some script has removed city
2021  return false;
2022  }
2023  city_checked = true;
2024  }
2025  } else if (success) {
2026  // Hey, we can upgrade the improvement!
2027  notify_player(pplayer, city_tile(pcity), E_WORKLIST, ftc_server,
2028  _("Production of %s is upgraded to %s in %s."),
2029  city_improvement_name_translation(pcity, ptarget),
2030  city_improvement_name_translation(pcity, pupdate),
2031  city_link(pcity));
2032  target.value.building = pupdate;
2033  }
2034 
2035  if (purge) {
2036  // Never in a million years.
2037  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD,
2038  ftc_server,
2039  _("%s can't build %s from the worklist. Purging..."),
2040  city_link(pcity),
2041  city_improvement_name_translation(pcity, ptarget));
2042  script_server_signal_emit("building_cant_be_built", ptarget, pcity,
2043  "never");
2044  if (city_exist(saved_id)) {
2045  city_checked = true;
2046  // Purge this worklist item.
2047  i--;
2048  worklist_remove(pwl, i);
2049  } else {
2050  city_checked = false;
2051  }
2052  }
2053  break;
2054  }
2055  default:
2056  // skip useless target
2057  qCritical("worklist_change_build_target() has unrecognized "
2058  "target kind (%d)",
2059  target.kind);
2060  break;
2061  };
2062  } // while
2063 
2064  if (success) {
2065  // All okay. Switch targets.
2066  change_build_target(pplayer, pcity, &target, E_WORKLIST);
2067 
2068  /* i is the index immediately _after_ the item we're changing to.
2069  Remove the (i-1)th item from the worklist. */
2070  worklist_remove(pwl, i - 1);
2071  }
2072 
2073  if (worklist_is_empty(pwl)) {
2074  /* There *was* something in the worklist, but it's empty now. Bug the
2075  player about it. */
2076  notify_player(pplayer, city_tile(pcity), E_WORKLIST, ftc_server,
2077  // TRANS: The <city> worklist ....
2078  _("The %s worklist is now empty."), city_link(pcity));
2079  }
2080 
2081  return success;
2082 }
2083 
2089 void choose_build_target(struct player *pplayer, struct city *pcity)
2090 {
2091  // Pick the next thing off the worklist.
2092  if (worklist_change_build_target(pplayer, pcity)) {
2093  return;
2094  }
2095 
2096  /* Try building the same thing again. Repeat building doesn't require a
2097  * call to change_build_target, so just return. */
2098  switch (pcity->production.kind) {
2099  case VUT_UTYPE:
2100  /* We can build a unit again unless it's unique or we have lost the tech.
2101  */
2102  if (!utype_has_flag(pcity->production.value.utype, UTYF_UNIQUE)
2103  && can_city_build_unit_now(pcity, pcity->production.value.utype)) {
2104  log_base(LOG_BUILD_TARGET, "%s repeats building %s",
2105  city_name_get(pcity),
2107  return;
2108  }
2109  break;
2110  case VUT_IMPROVEMENT:
2112  pcity->production.value.building)) {
2113  // We can build space and coinage again, and possibly others.
2114  log_base(LOG_BUILD_TARGET, "%s repeats building %s",
2115  city_name_get(pcity),
2117  return;
2118  }
2119  break;
2120  default:
2121  // fallthru
2122  break;
2123  };
2124 
2125  // Find *something* to do!
2126  log_debug("Trying advisor_choose_build.");
2127  advisor_choose_build(pplayer, pcity);
2128  log_debug("Advisor_choose_build didn't kill us.");
2129 }
2130 
2136 static const struct impr_type *
2137 building_upgrades_to(struct city *pcity, const struct impr_type *pimprove)
2138 {
2139  const struct impr_type *check = pimprove;
2140  const struct impr_type *best_upgrade = nullptr;
2141 
2142  if (!can_city_build_improvement_direct(pcity, check)) {
2143  return nullptr;
2144  }
2145  while (valid_improvement(check = improvement_replacement(check))) {
2146  if (can_city_build_improvement_direct(pcity, check)) {
2147  best_upgrade = check;
2148  }
2149  }
2150 
2151  return best_upgrade;
2152 }
2153 
2157 static void upgrade_building_prod(struct city *pcity)
2158 {
2159  const struct impr_type *producing = pcity->production.value.building;
2160  const struct impr_type *upgrading = building_upgrades_to(pcity, producing);
2161 
2162  if (upgrading && can_city_build_improvement_now(pcity, upgrading)) {
2163  notify_player(city_owner(pcity), city_tile(pcity), E_UNIT_UPGRADED,
2164  ftc_server, _("Production of %s is upgraded to %s in %s."),
2165  improvement_name_translation(producing),
2166  improvement_name_translation(upgrading), city_link(pcity));
2167  pcity->production.kind = VUT_IMPROVEMENT;
2168  pcity->production.value.building = upgrading;
2169  }
2170 }
2171 
2180 static const struct unit_type *
2181 unit_upgrades_to(struct city *pcity, const struct unit_type *punittype)
2182 {
2183  const struct unit_type *check = punittype;
2184  const struct unit_type *best_upgrade = U_NOT_OBSOLETED;
2185 
2186  if (!can_city_build_unit_direct(pcity, punittype)) {
2187  return U_NOT_OBSOLETED;
2188  }
2189  while ((check = check->obsoleted_by) != U_NOT_OBSOLETED) {
2190  if (can_city_build_unit_direct(pcity, check)) {
2191  best_upgrade = check;
2192  }
2193  }
2194 
2195  return best_upgrade;
2196 }
2197 
2201 static void upgrade_unit_prod(struct city *pcity)
2202 {
2203  const struct unit_type *producing = pcity->production.value.utype;
2204  const struct unit_type *upgrading = unit_upgrades_to(pcity, producing);
2205 
2206  if (upgrading && can_city_build_unit_direct(pcity, upgrading)) {
2207  notify_player(city_owner(pcity), city_tile(pcity), E_UNIT_UPGRADED,
2208  ftc_server, _("Production of %s is upgraded to %s in %s."),
2209  utype_name_translation(producing),
2210  utype_name_translation(upgrading), city_link(pcity));
2211  pcity->production.value.utype = upgrading;
2212  }
2213 }
2214 
2219 static bool city_distribute_surplus_shields(struct player *pplayer,
2220  struct city *pcity)
2221 {
2222  /* New city turn: use saved_surplus[] instead of surplus[] to avoid
2223  * changes possibly made elsewhere from taking effect within the turn
2224  * processing. We do need to update the surplus when units are disbanded,
2225  * however. handle_unit_disband() does that for surplus[], among other
2226  * updates, but do the addition here, to make sure we control that it's
2227  * the only change that happens. */
2228  int surplus = pcity->saved_surplus[O_SHIELD];
2229  if (surplus < 0) {
2231  {
2232  /* Should we look at punit->upkeep[O_SHIELD] here, instead of the
2233  * upkeep for the unit type? That's what the gold upkeep calculations
2234  * do. The difference is with units that happened to get free upkeep
2235  * from EFT_UNIT_UPKEEP_FREE_PER_CITY.
2236  */
2237  int upkeep =
2238  utype_upkeep_cost(unit_type_get(punit), pplayer, O_SHIELD);
2239 
2240  if (upkeep > 0 && surplus < 0) {
2241  const char *punit_link = unit_link(punit);
2242 
2243  /* TODO: Should the unit try to help cities on adjacent tiles? That
2244  * would be a rules change. (This action is performed by the game
2245  * it self) */
2246  if (upkeep_kill_unit(punit, O_SHIELD, ULR_DISBANDED,
2247  game.info.muuk_shield_wipe)) {
2248  notify_player(pplayer, city_tile(pcity), E_UNIT_LOST_MISC,
2249  ftc_server, _("%s can't upkeep %s, unit disbanded."),
2250  city_link(pcity), punit_link);
2251  surplus += upkeep;
2252  // pcity->surplus[O_SHIELD] automatically updated
2253  }
2254  }
2255  }
2257  }
2258 
2259  if (surplus < 0) {
2260  /* Special case: MissingXProtected. This nasty unit won't go so easily.
2261  * It'd rather make the citizens pay in blood for their failure to upkeep
2262  * it! If we make it here all normal units are already disbanded, so only
2263  * undisbandable ones remain. */
2265  {
2266  int upkeep =
2267  utype_upkeep_cost(unit_type_get(punit), pplayer, O_SHIELD);
2268 
2269  if (upkeep > 0 && surplus < 0) {
2270  notify_player(pplayer, city_tile(pcity), E_UNIT_LOST_MISC,
2271  ftc_server,
2272  _("Citizens in %s perish for their failure to "
2273  "upkeep %s!"),
2274  city_link(pcity), unit_link(punit));
2275  if (!city_reduce_size(pcity, 1, nullptr, "upkeep_failure")) {
2276  return false;
2277  }
2278 
2279  // No upkeep for the unit this turn.
2280  surplus += upkeep;
2281  pcity->surplus[O_SHIELD] += upkeep;
2282  }
2283  }
2285  }
2286 
2287  fc_assert(surplus >= 0);
2288 
2289  // Now we confirm changes made last turn.
2290  pcity->shield_stock += surplus;
2291  pcity->before_change_shields = pcity->shield_stock;
2292  pcity->last_turns_shield_surplus = surplus;
2293 
2294  return true;
2295 }
2296 
2301 static void wonder_set_build_turn(struct player *pplayer,
2302  const struct impr_type *pimprove)
2303 {
2304  int windex = improvement_number(pimprove);
2305 
2306  if (!is_wonder(pimprove)) {
2307  return;
2308  }
2309 
2310  pplayer->wonder_build_turn[windex] = game.info.turn;
2311 }
2312 
2316 static bool city_build_building(struct player *pplayer, struct city *pcity)
2317 {
2318  bool space_part;
2319  int mod;
2320  const struct impr_type *pimprove = pcity->production.value.building;
2321  int saved_id = pcity->id;
2322 
2323  if (city_production_has_flag(pcity, IF_GOLD)) {
2324  fc_assert(pcity->surplus[O_SHIELD] >= 0);
2325  /* pcity->before_change_shields already contains the surplus from
2326  * this turn. */
2327  pplayer->economic.gold += pcity->before_change_shields;
2328  pcity->before_change_shields = 0;
2329  pcity->shield_stock = 0;
2330  choose_build_target(pplayer, pcity);
2331  return true;
2332  }
2333  upgrade_building_prod(pcity);
2334 
2335  if (!can_city_build_improvement_now(pcity, pimprove)) {
2336  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
2337  _("%s is building %s, which is no longer available."),
2338  city_link(pcity),
2339  city_improvement_name_translation(pcity, pimprove));
2340  script_server_signal_emit("building_cant_be_built", pimprove, pcity,
2341  "unavailable");
2342  return true;
2343  }
2344  if (pcity->shield_stock >= impr_build_shield_cost(pcity, pimprove)) {
2345  int cost;
2346 
2347  if (is_small_wonder(pimprove)) {
2348  city_list_iterate(pplayer->cities, wcity)
2349  {
2350  if (city_has_building(wcity, pimprove)) {
2351  city_remove_improvement(wcity, pimprove);
2352  break;
2353  }
2354  }
2356  }
2357 
2358  space_part = true;
2359  if (get_current_construction_bonus(pcity, EFT_SS_STRUCTURAL, RPT_CERTAIN)
2360  > 0) {
2361  pplayer->spaceship.structurals++;
2362  } else if (get_current_construction_bonus(pcity, EFT_SS_COMPONENT,
2363  RPT_CERTAIN)
2364  > 0) {
2365  pplayer->spaceship.components++;
2366  } else if (get_current_construction_bonus(pcity, EFT_SS_MODULE,
2367  RPT_CERTAIN)
2368  > 0) {
2369  pplayer->spaceship.modules++;
2370  } else {
2371  space_part = false;
2372  city_add_improvement(pcity, pimprove);
2373  /* New city turn: wonders only take effect next turn */
2374  wonder_set_build_turn(pplayer, pimprove);
2375  }
2376  cost = impr_build_shield_cost(pcity, pimprove);
2377  pcity->bought_shields = 0;
2378  pcity->before_change_shields -= cost;
2379  pcity->shield_stock -= cost;
2380  pcity->turn_last_built = game.info.turn;
2381  // to eliminate micromanagement
2382  if (is_great_wonder(pimprove)) {
2383  notify_player(nullptr, city_tile(pcity), E_WONDER_BUILD, ftc_server,
2384  _("The %s have finished building %s in %s."),
2385  nation_plural_for_player(pplayer),
2386  city_improvement_name_translation(pcity, pimprove),
2387  city_link(pcity));
2388  }
2389 
2390  notify_player(pplayer, city_tile(pcity), E_IMP_BUILD, ftc_server,
2391  _("%s has finished building %s."), city_link(pcity),
2392  improvement_name_translation(pimprove));
2393  script_server_signal_emit("building_built", pimprove, pcity);
2394 
2395  if (!city_exist(saved_id)) {
2396  // Script removed city
2397  return false;
2398  }
2399 
2400  /* Call this function since some buildings may change the
2401  * the vision range of a city */
2402  city_refresh_vision(pcity);
2403 
2404  if ((mod = get_current_construction_bonus(pcity, EFT_GIVE_IMM_TECH,
2405  RPT_CERTAIN))) {
2406  struct research *presearch = research_get(pplayer);
2407  char research_name[MAX_LEN_NAME * 2];
2408  int i;
2409  const char *provider = improvement_name_translation(pimprove);
2410 
2411  notify_research(presearch, nullptr, E_TECH_GAIN, ftc_server,
2412  PL_("%s boosts research; you gain %d immediate "
2413  "advance.",
2414  "%s boosts research; you gain %d immediate "
2415  "advances.",
2416  mod),
2417  provider, mod);
2418 
2419  research_pretty_name(presearch, research_name, sizeof(research_name));
2420  for (i = 0; i < mod; i++) {
2421  Tech_type_id tech = pick_free_tech(presearch);
2422  QString adv_name =
2423  research_advance_name_translation(presearch, tech);
2424 
2425  give_immediate_free_tech(presearch, tech);
2426  notify_research(presearch, nullptr, E_TECH_GAIN, ftc_server,
2427  // TRANS: Tech from building (Darwin's Voyage)
2428  Q_("?frombldg:Acquired %s from %s."),
2429  qUtf8Printable(adv_name), provider);
2430 
2432  presearch, nullptr, E_TECH_EMBASSY, ftc_server,
2433  /* TRANS: Tech from building (Darwin's
2434  * Voyage) */
2435  Q_("?frombldg:The %s have acquired %s "
2436  "from %s."),
2437  research_name, qUtf8Printable(adv_name), provider);
2438  }
2439  }
2440  if (space_part && pplayer->spaceship.state == SSHIP_NONE) {
2441  notify_player(nullptr, city_tile(pcity), E_SPACESHIP, ftc_server,
2442  _("The %s have started building a spaceship!"),
2443  nation_plural_for_player(pplayer));
2444  pplayer->spaceship.state = SSHIP_STARTED;
2445  }
2446  if (space_part) {
2447  // space ship part build
2448  send_spaceship_info(pplayer, nullptr);
2449  } else {
2450  // Update city data.
2451  if (city_refresh(pcity)) {
2452  auto_arrange_workers(pcity);
2453  }
2454  }
2455 
2456  // Move to the next thing in the worklist
2457  choose_build_target(pplayer, pcity);
2458  }
2459 
2460  return true;
2461 }
2462 
2470 static struct unit *city_create_unit(struct city *pcity,
2471  const struct unit_type *utype)
2472 {
2473  struct player *pplayer = city_owner(pcity);
2474  struct unit *punit;
2475  int saved_unit_id;
2476 
2477  punit = create_unit(pplayer, pcity->tile, utype,
2479  pcity->id, -1);
2480  pplayer->score.units_built++;
2481  saved_unit_id = punit->id;
2482 
2483  // If city has a rally point set, give the unit a move order.
2484  if (pcity->rally_point.length) {
2485  punit->has_orders = true;
2486  punit->orders.length = pcity->rally_point.length;
2487  punit->orders.vigilant = pcity->rally_point.vigilant;
2488  punit->orders.list = new unit_order[pcity->rally_point.length];
2489  memcpy(punit->orders.list, pcity->rally_point.orders,
2490  pcity->rally_point.length * sizeof(struct unit_order));
2491  }
2492 
2493  /* Initialize the new unit's action timestamp from when the city
2494  production was changed or the unit bought */
2495  if (game.server.unitwaittime_extended) {
2496  punit->action_timestamp = pcity->server.prod_change_timestamp;
2497  punit->action_turn = pcity->server.prod_change_turn;
2498  }
2499 
2500  /* This might destroy pcity and/or punit: */
2501  script_server_signal_emit("unit_built", punit, pcity);
2502 
2503  if (unit_is_alive(saved_unit_id)) {
2504  return punit;
2505  } else {
2506  return nullptr;
2507  }
2508 }
2509 
2515 static bool city_build_unit(struct player *pplayer, struct city *pcity)
2516 {
2517  const struct unit_type *utype;
2518  struct worklist *pwl = &pcity->worklist;
2519  int unit_shield_cost, num_units, i;
2520  int saved_city_id = pcity->id;
2521 
2522  fc_assert_ret_val(pcity->production.kind == VUT_UTYPE, false);
2523 
2524  /* If the city has already bought a unit which is now obsolete, don't try
2525  * to upgrade the production. The new unit might require more shields,
2526  * which would be bad if it was bought to urgently defend a city. (Equally
2527  * it might be the same cost or cheaper, but tough; you hurried the unit so
2528  * you miss out on technological advances.) */
2529  if (city_can_change_build(pcity)) {
2530  upgrade_unit_prod(pcity);
2531  }
2532 
2533  utype = pcity->production.value.utype;
2534  unit_shield_cost = utype_build_shield_cost(pcity, utype);
2535 
2536  /* We must make a special case for barbarians here, because they are
2537  so dumb. Really. They don't know the prerequisite techs for units
2538  they build!! - Per */
2539  if (!can_city_build_unit_direct(pcity, utype) && !is_barbarian(pplayer)) {
2540  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
2541  _("%s is building %s, which is no longer available."),
2542  city_link(pcity), utype_name_translation(utype));
2543 
2544  // Log before signal emitting, so pointers are certainly valid
2545  qDebug("%s %s tried to build %s, which is not available.",
2547  utype_rule_name(utype));
2548  script_server_signal_emit("unit_cant_be_built", utype, pcity,
2549  "unavailable");
2550  return city_exist(saved_city_id);
2551  }
2552 
2553  if (pcity->shield_stock >= unit_shield_cost) {
2554  int pop_cost = utype_pop_value(utype);
2555  struct unit *punit;
2556 
2557  // Should we disband the city? -- Massimo
2558  if (city_size_get(pcity) == pop_cost
2559  && is_city_option_set(pcity, CITYO_DISBAND)) {
2560  return !disband_city(pcity);
2561  }
2562 
2563  if (city_size_get(pcity) <= pop_cost) {
2564  notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
2565  // TRANS: city ... utype ... size ... pop_cost
2566  _("%s can't build %s yet. "
2567  "(city size: %d, unit population cost: %d)"),
2569  city_size_get(pcity), pop_cost);
2570  script_server_signal_emit("unit_cant_be_built", utype, pcity,
2571  "pop_cost");
2572  return city_exist(saved_city_id);
2573  }
2574 
2575  fc_assert(pop_cost == 0 || city_size_get(pcity) >= pop_cost);
2576 
2577  // don't update turn_last_built if we returned above
2578  pcity->turn_last_built = game.info.turn;
2579  // check if we can build more than one unit (effect City_Build_Slots)
2580  (void) city_production_build_units(pcity, false, &num_units);
2581 
2582  // We should be able to build at least one (by checks above)
2583  fc_assert(num_units >= 1);
2584 
2585  for (i = 0; i < num_units; i++) {
2586  punit = city_create_unit(pcity, utype);
2587 
2588  /* Check if the city still exists (script might have removed it).
2589  * If not, we assume any effects / announcements done below were
2590  * already replaced by the script if necessary. */
2591  if (!city_exist(saved_city_id)) {
2592  break;
2593  }
2594 
2595  if (punit) {
2596  notify_player(
2597  pplayer, city_tile(pcity), E_UNIT_BUILT, ftc_server,
2598  /* TRANS: <city> is finished building <unit/building>. */
2599  _("%s is finished building %s."), city_link(pcity),
2601  }
2602 
2603  /* After we created the unit remove the citizen. This will also
2604  * rearrange the worker to take into account the extra resources
2605  * (food) needed. */
2606  if (pop_cost > 0) {
2607  /* This won't disband city due to pop_cost, but script might
2608  * still destroy city. */
2609  if (!city_reduce_size(pcity, pop_cost, nullptr, "unit_built")) {
2610  break;
2611  }
2612  }
2613 
2614  // to eliminate micromanagement, we only subtract the unit's cost
2615  pcity->before_change_shields -= unit_shield_cost;
2616  pcity->shield_stock -= unit_shield_cost;
2617  pcity->bought_shields = 0;
2618 
2619  if (pop_cost > 0) {
2620  // Additional message if the unit has population cost.
2621  notify_player(
2622  pplayer, city_tile(pcity), E_UNIT_BUILT_POP_COST, ftc_server,
2623  /* TRANS: "<unit> cost... <city> shrinks..."
2624  * Plural in "%d population", not "size %d". */
2625  PL_("%s cost %d population. %s shrinks to size %d.",
2626  "%s cost %d population. %s shrinks to size %d.", pop_cost),
2627  utype_name_translation(utype), pop_cost, city_link(pcity),
2628  city_size_get(pcity));
2629  }
2630 
2631  if (i != 0 && worklist_length(pwl) > 0) {
2632  /* remove the build unit from the worklist; it has to be one less
2633  * than units build to preserve the next build target from the
2634  * worklist */
2635  worklist_remove(pwl, 0);
2636  }
2637  }
2638 
2639  if (city_exist(saved_city_id)) {
2640  if (pcity->rally_point.length && !pcity->rally_point.persistent) {
2641  pcity->rally_point.length = 0;
2642  pcity->rally_point.persistent = false;
2643  pcity->rally_point.vigilant = false;
2644  delete[] pcity->rally_point.orders;
2645  pcity->rally_point.orders = nullptr;
2646  }
2647 
2648  // Done building this unit; time to move on to the next.
2649  choose_build_target(pplayer, pcity);
2650  }
2651  }
2652 
2653  return city_exist(saved_city_id);
2654 }
2655 
2659 static bool city_build_stuff(struct player *pplayer, struct city *pcity)
2660 {
2661  if (!city_distribute_surplus_shields(pplayer, pcity)) {
2662  return false;
2663  }
2664 
2667 
2668  switch (pcity->production.kind) {
2669  case VUT_IMPROVEMENT:
2670  return city_build_building(pplayer, pcity);
2671  case VUT_UTYPE:
2672  return city_build_unit(pplayer, pcity);
2673  default:
2674  // must never happen!
2675  fc_assert(false);
2676  break;
2677  };
2678  return false;
2679 }
2680 
2691 static bool sell_random_building(struct player *pplayer,
2692  struct cityimpr_list *imprs)
2693 {
2694  struct cityimpr *pcityimpr;
2695  int r;
2696 
2697  fc_assert_ret_val(pplayer != nullptr, false);
2698 
2699  if (!imprs || cityimpr_list_size(imprs) == 0) {
2700  return false;
2701  }
2702 
2703  r = fc_rand(cityimpr_list_size(imprs));
2704  pcityimpr = cityimpr_list_get(imprs, r);
2705 
2706  notify_player(pplayer, city_tile(pcityimpr->pcity), E_IMP_AUCTIONED,
2707  ftc_server,
2708  _("Can't afford to maintain %s in %s, building sold!"),
2710  city_link(pcityimpr->pcity));
2711  log_debug("%s: sold building (%s)", player_name(pplayer),
2713 
2714  do_sell_building(pplayer, pcityimpr->pcity, pcityimpr->pimprove,
2715  "cant_maintain");
2716 
2717  cityimpr_list_remove(imprs, pcityimpr);
2718 
2719  // Get back the gold upkeep that was already paid this turn.
2720  pplayer->economic.gold +=
2721  city_improvement_upkeep(pcityimpr->pcity, pcityimpr->pimprove);
2722 
2723  city_refresh_queue_add(pcityimpr->pcity);
2724 
2725  delete pcityimpr;
2726  pcityimpr = nullptr;
2727 
2728  return true;
2729 }
2730 
2739 static void uk_rem_gold_callback(struct unit *punit)
2740 {
2741  int gold_upkeep;
2742 
2743  // Remove the unit from uk_rem_gold.
2744  unit_list_remove(uk_rem_gold, punit);
2745 
2746  gold_upkeep = punit->server.upkeep_payed[O_GOLD];
2747 
2748  // All units in uk_rem_gold should have gold upkeep!
2749  fc_assert_ret_msg(gold_upkeep > 0, "%s has %d gold upkeep",
2750  unit_rule_name(punit), gold_upkeep);
2751 
2752  // Get the upkeep gold back.
2753  unit_owner(punit)->economic.gold += gold_upkeep;
2754 }
2755 
2760 static void uk_rem_gold_append(struct unit *punit)
2761 {
2762  // Make the unit aware that it is on the uk_rem_gold list.
2764 
2765  // Add the unit to the list.
2766  unit_list_append(uk_rem_gold, punit);
2767 }
2768 
2773 static void unit_list_referred_destroy(struct unit_list *punitlist)
2774 {
2775  unit_list_iterate(punitlist, punit)
2776  {
2777  // Clear the unit's knowledge of the list.
2779  }
2781 
2782  // Destroy the list it self.
2783  unit_list_destroy(punitlist);
2784 }
2785 
2796 static struct unit *sell_random_unit(struct player *pplayer,
2797  struct unit_list *punitlist)
2798 {
2799  struct unit *punit;
2800  int r;
2801  struct unit_list *cargo;
2802 
2803  fc_assert_ret_val(pplayer != nullptr, nullptr);
2804 
2805  if (!punitlist || unit_list_size(punitlist) == 0) {
2806  return nullptr;
2807  }
2808 
2809  r = fc_rand(unit_list_size(punitlist));
2810  punit = unit_list_get(punitlist, r);
2811 
2812  cargo = unit_list_new();
2813 
2814  /* Check if unit is transporting other units from punitlist,
2815  * and sell one of those (recursively) instead.
2816  * Note that in case of recursive transports we have to iterate
2817  * also through those middle transports that themselves are not in
2818  * punitlist. */
2819  unit_cargo_iterate(punit, pcargo)
2820  {
2821  /* Optimization, do not iterate over punitlist
2822  * if we are sure that pcargo is not in it. */
2823  if (pcargo->server.upkeep_payed[O_GOLD] > 0) {
2824  unit_list_iterate(punitlist, p2)
2825  {
2826  if (pcargo == p2) {
2827  unit_list_append(cargo, pcargo);
2828  }
2829  }
2831  }
2832  }
2834 
2835  if (unit_list_size(cargo) > 0) {
2836  /* Recursively sell. Note that cargo list has both
2837  * leaf units and middle transports in case of
2838  * recursive transports. */
2839  struct unit *ret = sell_random_unit(pplayer, cargo);
2840 
2841  unit_list_destroy(cargo);
2842 
2843  return ret;
2844  }
2845 
2846  unit_list_destroy(cargo);
2847 
2848  {
2849  const char *punit_link = unit_tile_link(punit);
2850 #ifdef FREECIV_DEBUG
2851  const char *punit_logname = unit_name_translation(punit);
2852 #endif // FREECIV_DEBUG
2853  struct tile *utile = unit_tile(punit);
2854 
2855  if (upkeep_kill_unit(punit, O_GOLD, ULR_SOLD,
2856  game.info.muuk_gold_wipe)) {
2857  unit_list_remove(punitlist, punit);
2858 
2859  /* The gold was payed back when the unit removal made
2860  * uk_rem_gold_callback() run as the unit's removal call back. */
2861 
2862  notify_player(pplayer, utile, E_UNIT_LOST_MISC, ftc_server,
2863  _("Not enough gold. %s disbanded."), punit_link);
2864  log_debug("%s: unit sold (%s)", player_name(pplayer), punit_logname);
2865  } else {
2866  // Not able to get rid of punit
2867  return nullptr;
2868  }
2869  }
2870 
2871  return punit;
2872 }
2873 
2877 static bool
2879 {
2880  struct cityimpr_list *pimprlist;
2881  bool sell_unit = true;
2882 
2883  if (!pplayer) {
2884  return false;
2885  }
2886 
2887  pimprlist = cityimpr_list_new();
2888  uk_rem_gold = unit_list_new();
2889 
2890  city_list_iterate(pplayer->cities, pcity)
2891  {
2892  city_built_iterate(pcity, pimprove)
2893  {
2894  if (can_city_sell_building(pcity, pimprove)) {
2895  auto *ci = new cityimpr;
2896 
2897  ci->pcity = pcity;
2898  ci->pimprove = pimprove;
2899  cityimpr_list_append(pimprlist, ci);
2900  }
2901  }
2903 
2904  unit_list_iterate(pcity->units_supported, punit)
2905  {
2906  if (punit->server.upkeep_payed[O_GOLD] > 0) {
2907  uk_rem_gold_append(punit);
2908  }
2909  }
2911  }
2913 
2914  while (pplayer->economic.gold < 0
2915  && (cityimpr_list_size(pimprlist) > 0
2916  || unit_list_size(uk_rem_gold) > 0)) {
2917  if ((!sell_unit && cityimpr_list_size(pimprlist) > 0)
2918  || unit_list_size(uk_rem_gold) == 0) {
2919  sell_random_building(pplayer, pimprlist);
2920  } else {
2921  sell_random_unit(pplayer, uk_rem_gold);
2922  }
2923  sell_unit = !sell_unit;
2924  }
2925 
2926  // Free remaining entries from list
2927  cityimpr_list_iterate(pimprlist, pimpr)
2928  {
2929  delete pimpr;
2930  pimpr = nullptr;
2931  }
2933 
2934  if (pplayer->economic.gold < 0) {
2935  /* If we get here it means the player has
2936  * negative gold. This should never happen. */
2937  fc_assert_msg(false, "Player %s (nb %d) cannot have negative gold!",
2938  player_name(pplayer), player_number(pplayer));
2939  }
2940 
2941  cityimpr_list_destroy(pimprlist);
2943 
2944  return pplayer->economic.gold >= 0;
2945 }
2946 
2950 static bool player_balance_treasury_units(struct player *pplayer)
2951 {
2952  if (!pplayer) {
2953  return false;
2954  }
2955 
2956  uk_rem_gold = unit_list_new();
2957 
2958  city_list_iterate(pplayer->cities, pcity)
2959  {
2960  unit_list_iterate(pcity->units_supported, punit)
2961  {
2962  if (punit->server.upkeep_payed[O_GOLD] > 0) {
2963  uk_rem_gold_append(punit);
2964  }
2965  }
2967  }
2969 
2970  while (pplayer->economic.gold < 0
2971  && sell_random_unit(pplayer, uk_rem_gold)) {
2972  // all done in sell_random_unit()
2973  }
2974 
2975  if (pplayer->economic.gold < 0) {
2976  /* If we get here it means the player has
2977  * negative gold. This should never happen. */
2978  fc_assert_msg(false, "Player %s (nb %d) cannot have negative gold!",
2979  player_name(pplayer), player_number(pplayer));
2980  }
2981 
2983 
2984  return pplayer->economic.gold >= 0;
2985 }
2986 
2990 static bool city_balance_treasury_buildings(struct city *pcity)
2991 {
2992  struct player *pplayer;
2993  struct cityimpr_list *pimprlist;
2994 
2995  if (!pcity) {
2996  return true;
2997  }
2998 
2999  pplayer = city_owner(pcity);
3000  pimprlist = cityimpr_list_new();
3001 
3002  // Create a vector of all buildings that can be sold.
3003  city_built_iterate(pcity, pimprove)
3004  {
3005  if (can_city_sell_building(pcity, pimprove)) {
3006  auto *ci = new cityimpr;
3007 
3008  ci->pcity = pcity;
3009  ci->pimprove = pimprove;
3010  cityimpr_list_append(pimprlist, ci);
3011  }
3012  }
3014 
3015  // Try to sell some buildings.
3016  while (pplayer->economic.gold < 0
3017  && sell_random_building(pplayer, pimprlist)) {
3018  // all done in sell_random_building
3019  }
3020 
3021  // Free remaining entries from list
3022  cityimpr_list_iterate(pimprlist, pimpr)
3023  {
3024  delete pimpr;
3025  pimpr = nullptr;
3026  }
3028 
3029  cityimpr_list_destroy(pimprlist);
3030 
3031  return pplayer->economic.gold >= 0;
3032 }
3033 
3042 static bool city_balance_treasury_units(struct city *pcity)
3043 {
3044  struct player *pplayer;
3045 
3046  if (!pcity) {
3047  return true;
3048  }
3049 
3050  pplayer = city_owner(pcity);
3051  uk_rem_gold = unit_list_new();
3052 
3053  // Create a vector of all supported units with gold upkeep.
3054  unit_list_iterate(pcity->units_supported, punit)
3055  {
3056  if (punit->server.upkeep_payed[O_GOLD] > 0) {
3057  uk_rem_gold_append(punit);
3058  }
3059  }
3061 
3062  // Still not enough gold, so try "selling" some units.
3063  while (pplayer->economic.gold < 0
3064  && sell_random_unit(pplayer, uk_rem_gold)) {
3065  // all done in sell_random_unit()
3066  }
3067 
3068  /* If we get here the player has negative gold, but hopefully
3069  * another city will be able to pay the deficit, so continue. */
3070 
3072 
3073  return pplayer->economic.gold >= 0;
3074 }
3075 
3079 static bool place_pollution(struct city *pcity, enum extra_cause cause)
3080 {
3081  struct tile *ptile;
3082  struct tile *pcenter = city_tile(pcity);
3083  int city_radius_sq = city_map_radius_sq_get(pcity);
3084  int k = 100;
3085 
3086  while (k > 0) {
3087  // place pollution on a random city tile
3088  int cx, cy;
3089  int tile_id = fc_rand(city_map_tiles(city_radius_sq));
3090  struct extra_type *pextra;
3091 
3093  city_tile_index_to_xy(&cx, &cy, tile_id, city_radius_sq), false);
3094 
3095  // check for a a real map position
3096  if (!(ptile = city_map_to_tile(pcenter, city_radius_sq, cx, cy))) {
3097  continue;
3098  }
3099 
3100  pextra = rand_extra_for_tile(ptile, cause, false);
3101 
3102  if (pextra != nullptr && !tile_has_extra(ptile, pextra)) {
3103  tile_add_extra(ptile, pextra);
3104  update_tile_knowledge(ptile);
3105 
3106  return true;
3107  }
3108  k--;
3109  }
3110  log_debug("pollution not placed: city: %s", city_name_get(pcity));
3111 
3112  return false;
3113 }
3114 
3118 static void check_pollution(struct city *pcity)
3119 {
3120  if (fc_rand(100) < pcity->pollution) {
3121  if (place_pollution(pcity, EC_POLLUTION)) {
3122  notify_player(city_owner(pcity), city_tile(pcity), E_POLLUTION,
3123  ftc_server, _("Pollution near %s."), city_link(pcity));
3124  }
3125  }
3126 }
3127 
3135 int city_incite_cost(struct player *pplayer, struct city *pcity)
3136 {
3137  int dist, size;
3138  double cost; // Intermediate values can get very large
3139 
3140  // Gold factor
3141  cost = city_owner(pcity)->economic.gold + game.server.base_incite_cost;
3142 
3143  unit_list_iterate(pcity->tile->units, punit)
3144  {
3145  cost += (unit_build_shield_cost(pcity, punit)
3146  * game.server.incite_unit_factor);
3147  }
3149 
3150  // Buildings
3151  city_built_iterate(pcity, pimprove)
3152  {
3153  cost += impr_build_shield_cost(pcity, pimprove)
3154  * game.server.incite_improvement_factor;
3155  }
3157 
3158  // Stability bonuses
3159  if (!city_unhappy(pcity)) {
3160  cost *= 2;
3161  }
3162  if (city_celebrating(pcity)) {
3163  cost *= 2;
3164  }
3165 
3166  // Buy back is cheap, conquered cities are also cheap
3167  if (!game.info.citizen_nationality) {
3168  if (city_owner(pcity) != pcity->original) {
3169  if (pplayer == pcity->original) {
3170  cost /= 2; // buy back: 50% price reduction
3171  } else {
3172  cost = cost * 2 / 3; // buy conquered: 33% price reduction
3173  }
3174  }
3175  }
3176 
3177  // Distance from capital
3178  /* Max penalty. Applied if there is no capital, or it's even further away.
3179  */
3180  dist = 32;
3181  city_list_iterate(city_owner(pcity)->cities, capital)
3182  {
3183  if (is_capital(capital)) {
3184  int tmp = map_distance(capital->tile, pcity->tile);
3185 
3186  if (tmp < dist) {
3187  dist = tmp;
3188  }
3189  }
3190  }
3192 
3193  size =
3194  MAX(1, city_size_get(pcity) + pcity->feel[CITIZEN_HAPPY][FEELING_FINAL]
3196  - pcity->feel[CITIZEN_ANGRY][FEELING_FINAL] * 3);
3197  cost *= size;
3198  cost *= game.server.incite_total_factor;
3199  cost = cost / (dist + 3);
3200 
3201  if (game.info.citizen_nationality) {
3202  int cost_per_citizen = cost / pcity->size;
3203  int natives = citizens_nation_get(pcity, city_owner(pcity)->slot);
3204  int tgt_cit = citizens_nation_get(pcity, pplayer->slot);
3205  int third_party = pcity->size - natives - tgt_cit;
3206 
3207  cost = cost_per_citizen * (natives + 0.7 * third_party + 0.5 * tgt_cit);
3208  }
3209 
3210  cost += (cost * get_city_bonus(pcity, EFT_INCITE_COST_PCT)) / 100;
3211  cost /= 100;
3212 
3213  if (cost >= INCITE_IMPOSSIBLE_COST) {
3214  return INCITE_IMPOSSIBLE_COST;
3215  } else {
3216  return cost;
3217  }
3218 }
3219 
3223 static void define_orig_production_values(struct city *pcity)
3224 {
3225  /* Remember what this city is building last turn, so that on the next turn
3226  * the player can switch production to something else and then change it
3227  * back without penalty. This has to be updated _before_ production for
3228  * this turn is calculated, so that the penalty will apply if the player
3229  * changes production away from what has just been completed. This makes
3230  * sense if you consider what this value means: all the shields in the
3231  * city have been dedicated toward the project that was chosen last turn,
3232  * so the player shouldn't be penalized if the governor has to pick
3233  * something different. See city_change_production_penalty(). */
3234  pcity->changed_from = pcity->production;
3235 
3236  log_debug("In %s, building %s. Beg of Turn shields = %d",
3238  pcity->before_change_shields);
3239 }
3240 
3244 static void nullify_caravan_and_disband_plus(struct city *pcity)
3245 {
3246  pcity->disbanded_shields = 0;
3247  pcity->caravan_shields = 0;
3248 }
3249 
3255 {
3257  pcity->before_change_shields = 0;
3258 }
3259 
3260 /* Handle the possibility of plague in city. Return TRUE if plague hit.
3261  city_populate() checks pcity->turn_plague later to prevent growth.
3262  */
3263 static bool city_handle_plague_risk(struct city *pcity)
3264 {
3265  /* ------------------------------------------------------------------------
3266  * Handle plague. Do it before food growth. */
3267  if (game.info.illness_on) {
3268  /* recalculate city illness; illness due to trade has to be saved
3269  * within the city struct as the client has not all data to
3270  * calculate it */
3271  pcity->server.illness = city_illness_calc(
3272  pcity, nullptr, nullptr, &(pcity->illness_trade), nullptr);
3273 
3274  if (city_illness_check(pcity)) {
3275  city_illness_strike(pcity);
3276  return true;
3277  }
3278  }
3279  return false;
3280 }
3281 
3282 static bool city_handle_disorder(struct city *pcity)
3283 {
3284  struct player *pplayer = city_owner(pcity);
3285  int revolution_turns = get_city_bonus(pcity, EFT_REVOLUTION_UNHAPPINESS);
3286  struct government *gov = government_of_city(pcity);
3287  if (city_unhappy(pcity)) {
3288  const char *revomsg;
3289 
3290  pcity->anarchy++;
3291  if (pcity->anarchy == revolution_turns) {
3292  // Revolution next turn if not dealt with
3293  /* TRANS: preserve leading space; this string will be appended to
3294  * another sentence */
3295  revomsg = _(" Unrest threatens to spread beyond the city.");
3296  } else {
3297  revomsg = "";
3298  }
3299  if (pcity->anarchy == 1) {
3300  notify_player(pplayer, city_tile(pcity), E_CITY_DISORDER, ftc_server,
3301  // TRANS: second %s is an optional extra sentence
3302  _("Civil disorder in %s.%s"), city_link(pcity), revomsg);
3303  return true;
3304  } else {
3305  notify_player(pplayer, city_tile(pcity), E_CITY_DISORDER, ftc_server,
3306  // TRANS: second %s is an optional extra sentence
3307  _("CIVIL DISORDER CONTINUES in %s.%s"), city_link(pcity),
3308  revomsg);
3309  return true;
3310  }
3311  } else {
3312  if (pcity->anarchy != 0) {
3313  notify_player(pplayer, city_tile(pcity), E_CITY_NORMAL, ftc_server,
3314  _("Order restored in %s."), city_link(pcity));
3315  }
3316  pcity->anarchy = 0;
3317  }
3318 
3319  if (revolution_turns > 0 && pcity->anarchy > revolution_turns) {
3320  notify_player(pplayer, city_tile(pcity), E_ANARCHY, ftc_server,
3321  // TRANS: %s - government form, e.g., Democracy
3322  _("The people have overthrown your %s, "
3323  "your country is in turmoil."),
3326  return true;
3327  }
3328  return false;
3329 }
3330 
3334 static void update_city_activity(struct city *pcity)
3335 {
3336  struct player *pplayer = city_owner(pcity);
3337  int saved_id;
3338 
3339  if (!pcity) {
3340  return;
3341  }
3342 
3343  if (city_refresh(pcity)) {
3344  auto_arrange_workers(pcity);
3345  }
3346 
3347  /* ------------------------------------------------------------------------
3348  * Calculate history first, before builds and other things change it */
3349  pcity->history += city_history_gain(pcity);
3350 
3351  // History can decrease, but never go below zero
3352  pcity->history = MAX(pcity->history, 0);
3353 
3354  /* ------------------------------------------------------------------------
3355  * Celebration. Surpluses should already have been calculated taking this
3356  * into account, but it makes sense to print the message first.
3357  *
3358  * Happens before building. */
3359 
3360  if (city_celebrating(pcity)) {
3361  pcity->rapture++;
3362  if (pcity->rapture == 1) {
3363  notify_player(pplayer, city_tile(pcity), E_CITY_LOVE, ftc_server,
3364  _("Celebrations in your honor in %s."),
3365  city_link(pcity));
3366  }
3367  } else {
3368  if (pcity->rapture != 0) {
3369  notify_player(pplayer, city_tile(pcity), E_CITY_NORMAL, ftc_server,
3370  _("Celebrations canceled in %s."), city_link(pcity));
3371  }
3372  pcity->rapture = 0;
3373  }
3374  pcity->was_happy = city_happy(pcity);
3375 
3376  /* ------------------------------------------------------------------------
3377  * Add Gold and Science output. Happens before building.
3378  *
3379  * New techs are _not_ invented here yet, so this shouldn't change any
3380  * production values */
3381  update_bulbs(pplayer, pcity->saved_surplus[O_SCIENCE], false);
3382 
3383  pplayer->economic.infra_points += get_city_bonus(pcity, EFT_INFRA_POINTS);
3384 
3385  /* Update the treasury, paying upkeeps and checking running out
3386  pplayer->economic.gold += pcity->prod[O_GOLD];
3387  * of gold based on the ruleset setting 'game.info.gold_upkeep_style':
3388  pplayer->economic.gold -= city_total_impr_gold_upkeep(pcity);
3389  * GOLD_UPKEEP_CITY: Cities pay for buildings and units and deficit
3390  pplayer->economic.gold -= city_total_unit_gold_upkeep(pcity);
3391  * is checked right here.
3392  * GOLD_UPKEEP_MIXED: Cities pay only for buildings; the nation pays
3393  * for units after all cities are processed.
3394  * GOLD_UPKEEP_NATION: The nation pays for buildings and units
3395  * only after all cities are processed.
3396  *
3397  * city_support() in city.c sets pcity->usage[O_GOLD] (and hence
3398  * ->surplus[O_GOLD]) according to the setting.
3399  */
3400  pplayer->economic.gold += pcity->saved_surplus[O_GOLD];
3401 
3402  // Remember how much gold upkeep each unit was payed.
3403  unit_list_iterate(pcity->units_supported, punit)
3404  {
3405  punit->server.upkeep_payed[O_GOLD] = punit->upkeep[O_GOLD];
3406  }
3408 
3409  if (pplayer->economic.gold < 0) {
3410  /* Not enough gold - we have to sell some buildings, and if that
3411  * is not enough, disband units with gold upkeep, taking into
3412  * account the setting of 'game.info.gold_upkeep_style':
3413  * GOLD_UPKEEP_CITY: Cities pay for buildings and units.
3414  * GOLD_UPKEEP_MIXED: Cities pay only for buildings; the nation pays
3415  * for units.
3416  * GOLD_UPKEEP_NATION: The nation pays for buildings and units. */
3417  switch (game.info.gold_upkeep_style) {
3418  case GOLD_UPKEEP_CITY:
3419  /* Paid for both buildings and units, try to sell both in turn. */
3420  if (!city_balance_treasury_buildings(pcity)) {
3422  }
3423 
3424  case GOLD_UPKEEP_MIXED:
3425  /* Ran out of money while paying for buildings, try to sell buildings.
3426  */
3428  break;
3429  case GOLD_UPKEEP_NATION:
3430  /* This shouldn't be possible since all upkeeps are paid later. */
3431  break;
3432  }
3433  }
3434  /* ------------------------------------------------------------------------
3435  * Produce pollution!
3436  * Before building, so that a disbanding city can still pollute... */
3437 
3438  check_pollution(pcity);
3439 
3440  /* ------------------------------------------------------------------------
3441  * Handle shield upkeep and building. All of that is inside
3442  * city_build_stuff(). The city might be destroyed here, if the city is
3443  * disbanded into Settlers, or if there's an undisbandable unit with shield
3444  * upkeep that can't be paid, so potentially return early. This happens
3445  * after trade and gold output has committed, though. */
3446 
3447  if (!city_build_stuff(pplayer, pcity)) {
3448  /* City disbanded into a unit, or was destroyed for lack of upkeep */
3449  return;
3450  }
3451  /* ------------------------------------------------------------------------
3452  * Handle plague. Do it before food growth. */
3453  city_handle_plague_risk(pcity);
3454 
3455  // City population updated here, after the rapture stuff above. --Jing
3456  /*
3457  * Here, make sure to use saved_surplus[] since building _will_ change
3458  * output values. */
3459  saved_id = pcity->id;
3460  city_populate(pcity, pplayer);
3461  if (nullptr == player_city_by_number(pplayer, saved_id)) {
3462  /* If the city is destroyed by famine */
3463  return;
3464  }
3465  /* ------------------------------------------------------------------------
3466  * Check if disorder in city brings the government to anarchy */
3467  city_handle_disorder(pcity);
3468 
3469  pcity->did_sell = false;
3470  pcity->did_buy = false;
3471  pcity->airlift = city_airlift_max(pcity);
3472 
3473  send_city_info(nullptr, pcity);
3474 
3475  if (city_refresh(pcity)) {
3476  auto_arrange_workers(pcity);
3477  }
3478  sanity_check_city(pcity);
3479 }
3480 
3484 static bool city_illness_check(const struct city *pcity)
3485 {
3486  return fc_rand(1000) < pcity->server.illness;
3487 }
3488 
3493 static bool disband_city(struct city *pcity)
3494 {
3495  struct player *pplayer = city_owner(pcity);
3496  struct tile *ptile = pcity->tile;
3497  struct city *rcity = nullptr;
3498  const struct unit_type *utype = pcity->production.value.utype;
3499  struct unit *punit;
3500  int saved_id = pcity->id;
3501 
3502  // find closest city other than pcity
3503  rcity = find_closest_city(ptile, pcity, pplayer, false, false, false, true,
3504  false, nullptr);
3505 
3506  if (!rcity) {
3507  // What should we do when we try to disband our only city?
3508  notify_player(pplayer, ptile, E_CITY_CANTBUILD, ftc_server,
3509  _("%s can't build %s yet, "
3510  "as we can't disband our only city."),
3512  script_server_signal_emit("unit_cant_be_built", utype, pcity,
3513  "pop_cost");
3514  if (!city_exist(saved_id)) {
3515  // Script decided to remove even the last city
3516  return true;
3517  } else {
3518  return false;
3519  }
3520  }
3521 
3522  punit = city_create_unit(pcity, utype);
3523 
3524  /* "unit_built" script handler may have destroyed city. If so, we
3525  * assume something sensible happened to its units, and that the
3526  * script took care of announcing unit creation if required. */
3527  if (city_exist(saved_id)) {
3528  /* Shift all the units supported by pcity (including the new unit)
3529  * to rcity. transfer_city_units does not make sure no units are
3530  * left floating without a transport, but since all units are
3531  * transferred this is not a problem. */
3532  transfer_city_units(pplayer, pplayer, pcity->units_supported, rcity,
3533  pcity, -1, true);
3534 
3535  if (punit) {
3536  notify_player(pplayer, ptile, E_UNIT_BUILT, ftc_server,
3537  // TRANS: "<city> is disbanded into Settler."
3538  _("%s is disbanded into %s."), city_tile_link(pcity),
3540  }
3541 
3542  script_server_signal_emit("city_destroyed", pcity, pcity->owner,
3543  nullptr);
3544 
3545  remove_city(pcity);
3546 
3547  /* Since we've removed the city, we don't need to worry about
3548  * charging for production, disabling rally points, etc. */
3549  }
3550 
3551  return true;
3552 }
3553 
3602 static float city_migration_score(struct city *pcity)
3603 {
3604  float score = 0.0;
3605  int build_shield_cost = 0;
3606  bool has_wonder = false;
3607 
3608  if (!pcity) {
3609  return score;
3610  }
3611 
3612  if (pcity->server.mgr_score_calc_turn == game.info.turn) {
3613  // up-to-date migration score
3614  return pcity->server.migration_score;
3615  }
3616 
3617  // feeling of the citizens
3618  score = (city_size_get(pcity)
3619  + 1.00 * pcity->feel[CITIZEN_HAPPY][FEELING_FINAL]
3620  + 0.00 * pcity->feel[CITIZEN_CONTENT][FEELING_FINAL]
3621  - 0.25 * pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL]
3622  - 0.50 * pcity->feel[CITIZEN_ANGRY][FEELING_FINAL]);
3623 
3624  // calculate shield build cost for all buildings
3625  city_built_iterate(pcity, pimprove)
3626  {
3627  build_shield_cost += impr_build_shield_cost(pcity, pimprove);
3628  if (is_wonder(pimprove)) {
3629  // this city has a wonder
3630  has_wonder = true;
3631  }
3632  }
3634 
3635  // take shield costs of all buidings into account; normalized by 1000
3636  score *= (1
3637  + (1 - exp(-static_cast<float> MAX(0, build_shield_cost) / 1000))
3638  / 5);
3639  // take trade into account; normalized by 100
3640  score *=
3641  (1
3642  + (1 - exp(-static_cast<float> MAX(0, pcity->surplus[O_TRADE]) / 100))
3643  / 5);
3644  // take luxury into account; normalized by 100
3645  score *=
3646  (1
3647  + (1
3648  - exp(-static_cast<float> MAX(0, pcity->surplus[O_LUXURY]) / 100))
3649  / 5);
3650  // take science into account; normalized by 100
3651  score *=
3652  (1
3653  + (1
3654  - exp(-static_cast<float> MAX(0, pcity->surplus[O_SCIENCE]) / 100))
3655  / 5);
3656 
3657  score += city_culture(pcity) * game.info.culture_migration_pml / 1000;
3658 
3659  /* Take food into account; the food surplus is clipped to values between
3660  * -10..20 and normalize by 10. Thus, the factor is between 0.9 and 1.2. */
3661  score *=
3662  (1 + static_cast<float> CLIP(-10, pcity->surplus[O_FOOD], 20) / 10);
3663 
3664  /* Reduce the score due to city illness (plague). The illness is given in
3665  * tenth of percent (0..1000) and normalized by 25. Thus, this factor is
3666  * between 0.6 (ill city) and 1.0 (health city). */
3667  score *= (100
3668  - static_cast<float>(city_illness_calc(pcity, nullptr, nullptr,
3669  nullptr, nullptr))
3670  / 25);
3671 
3672  if (has_wonder) {
3673  // people like wonders
3674  score *= 1.25;
3675  }
3676 
3677  if (is_capital(pcity)) {
3678  // the capital is a magnet for the citizens
3679  score *= 1.25;
3680  }
3681 
3682  // take into account effects
3683  score *= (1.0 + get_city_bonus(pcity, EFT_MIGRATION_PCT) / 100.0);
3684 
3685  log_debug("[M] %s score: %.3f", city_name_get(pcity), score);
3686 
3687  // set migration score for the city
3688  pcity->server.migration_score = score;
3689  // set the turn, when the score was calculated
3690  pcity->server.mgr_score_calc_turn = game.info.turn;
3691 
3692  return score;
3693 }
3694 
3701 static bool do_city_migration(struct city *pcity_from, struct city *pcity_to)
3702 {
3703  struct player *pplayer_from, *pplayer_to, *pplayer_citizen;
3704  struct tile *ptile_from, *ptile_to;
3705  char name_from[MAX_LEN_LINK], name_to[MAX_LEN_LINK];
3706  const char *nation_from, *nation_to;
3707  struct city *rcity = nullptr;
3708  bool incr_success;
3709  int to_id;
3710 
3711  if (!pcity_from || !pcity_to) {
3712  return false;
3713  }
3714  to_id = pcity_to->id;
3715  pplayer_from = city_owner(pcity_from);
3716  pplayer_citizen = pplayer_from;
3717  pplayer_to = city_owner(pcity_to);
3718  // We copy that, because city_link always returns the same pointer.
3719  sz_strlcpy(name_from, city_link(pcity_from));
3720  sz_strlcpy(name_to, city_link(pcity_to));
3721  nation_from = nation_adjective_for_player(pplayer_from);
3722  nation_to = nation_adjective_for_player(pplayer_to);
3723  ptile_from = city_tile(pcity_from);
3724  ptile_to = city_tile(pcity_to);
3725 
3726  // check food supply in the receiver city
3727  if (game.server.mgr_foodneeded) {
3728  bool migration = false;
3729 
3730  if (pcity_to->surplus[O_FOOD] >= game.info.food_cost) {
3731  migration = true;
3732  } else {
3733  /* check if there is a free tile for the new citizen which, when
3734  * worked,
3735  * leads to zero or positive food surplus for the enlarged city */
3736  int max_food_tile = -1; // no free tile
3738  city_tile(pcity_to), ptile)
3739  {
3740  if (city_can_work_tile(pcity_to, ptile)
3741  && tile_worked(ptile) != pcity_to) {
3742  /* Safest assumption is that city won't be celebrating once an
3743  * additional citizen is added */
3744  max_food_tile =
3745  MAX(max_food_tile,
3746  city_tile_output(pcity_to, ptile, false, O_FOOD));
3747  }
3748  }
3750  if (max_food_tile >= 0
3751  && pcity_to->surplus[O_FOOD] + max_food_tile
3752  >= game.info.food_cost) {
3753  migration = true;
3754  }
3755  }
3756 
3757  if (!migration) {
3758  // insufficiency food in receiver city; no additional citizens
3759  if (pplayer_from == pplayer_to) {
3760  // migration between one nation
3761  notify_player(pplayer_to, ptile_to, E_CITY_TRANSFER, ftc_server,
3762  // TRANS: From <city1> to <city2>.
3763  _("Migrants from %s can't go to %s because there is "
3764  "not enough food available!"),
3765  name_from, name_to);
3766  } else {
3767  // migration between different nations
3768  notify_player(
3769  pplayer_from, ptile_to, E_CITY_TRANSFER, ftc_server,
3770  // TRANS: From <city1> to <city2> (<city2 nation adjective>).
3771  _("Migrants from %s can't go to %s (%s) because there "
3772  "is not enough food available!"),
3773  name_from, name_to, nation_to);
3774  notify_player(
3775  pplayer_to, ptile_to, E_CITY_TRANSFER, ftc_server,
3776  // TRANS: From <city1> (<city1 nation adjective>) to <city2>.
3777  _("Migrants from %s (%s) can't go to %s because there "
3778  "is not enough food available!"),
3779  name_from, nation_from, name_to);
3780  }
3781 
3782  return false;
3783  }
3784  }
3785 
3786  if (!city_can_grow_to(pcity_to, city_size_get(pcity_to) + 1)) {
3787  // receiver city can't grow
3788  if (pplayer_from == pplayer_to) {
3789  // migration between one nation
3790  notify_player(pplayer_to, ptile_to, E_CITY_TRANSFER, ftc_server,
3791  // TRANS: From <city1> to <city2>.
3792  _("Migrants from %s can't go to %s because it needs "
3793  "an improvement to grow!"),
3794  name_from, name_to);
3795  } else {
3796  // migration between different nations
3797  notify_player(
3798  pplayer_from, ptile_to, E_CITY_TRANSFER, ftc_server,
3799  // TRANS: From <city1> to <city2> of <city2 nation adjective>.
3800  _("Migrants from %s can't go to %s (%s) because it "
3801  "needs an improvement to grow!"),
3802  name_from, name_to, nation_to);
3803  notify_player(
3804  pplayer_to, ptile_to, E_CITY_TRANSFER, ftc_server,
3805  // TRANS: From <city1> (<city1 nation adjective>) to <city2>.
3806  _("Migrants from %s (%s) can't go to %s because it "
3807  "needs an improvement to grow!"),
3808  name_from, nation_from, name_to);
3809  }
3810 
3811  return false;
3812  }
3813 
3814  // reduce size of giver
3815  if (city_size_get(pcity_from) == 1) {
3816  if (game.info.citizen_nationality) {
3817  // Preserve nationality of city's only citizen
3818  pplayer_citizen = player_slot_get_player(citizens_random(pcity_from));
3819  }
3820 
3821  // do not destroy wonders
3822  city_built_iterate(pcity_from, pimprove)
3823  {
3824  if (is_wonder(pimprove)) {
3825  return false;
3826  }
3827  }
3829 
3830  // find closest city other of the same player than pcity_from
3831  rcity = find_closest_city(ptile_from, pcity_from, pplayer_from, false,
3832  false, false, true, false, nullptr);
3833 
3834  if (rcity) {
3835  int id = pcity_from->id;
3836 
3837  // transfer all units to the closest city
3838  transfer_city_units(pplayer_from, pplayer_from,
3839  pcity_from->units_supported, rcity, pcity_from, -1,
3840  true);
3841  sz_strlcpy(name_from, city_tile_link(pcity_from));
3842 
3843  script_server_signal_emit("city_size_change", pcity_from, -1,
3844  "migration_from");
3845 
3846  if (city_exist(id)) {
3847  script_server_signal_emit("city_destroyed", pcity_from,
3848  pcity_from->owner, nullptr);
3849 
3850  if (city_exist(id)) {
3851  remove_city(pcity_from);
3852  }
3853  }
3854 
3855  notify_player(pplayer_from, ptile_from, E_CITY_LOST, ftc_server,
3856  _("%s was disbanded by its citizens."), name_from);
3857  } else {
3858  // it's the only city of the nation
3859  return false;
3860  }
3861  } else {
3862  /* the migrants take half of the food box with them (this prevents
3863  * migration -> grow -> migration -> ... cycles) */
3864  pcity_from->food_stock /= 2;
3865 
3866  if (game.info.citizen_nationality) {
3867  /* Those citizens that are from the target nation are most
3868  * ones migrating. */
3869  if (citizens_nation_get(pcity_from, pplayer_to->slot) > 0) {
3870  pplayer_citizen = pplayer_to;
3871  } else if (!citizens_nation_get(pcity_from, pplayer_citizen->slot)) {
3872  // No native citizens at all in the city, choose random foreigner
3873  struct player_slot *cit_slot = citizens_random(pcity_from);
3874 
3875  pplayer_citizen = player_slot_get_player(cit_slot);
3876  }
3877  // This should be followed by city_reduce_size().
3878  citizens_nation_add(pcity_from, pplayer_citizen->slot, -1);
3879  }
3880  city_reduce_size(pcity_from, 1, pplayer_from, "migration_from");
3881  city_refresh_vision(pcity_from);
3882  if (city_refresh(pcity_from)) {
3883  auto_arrange_workers(pcity_from);
3884  }
3885  }
3886 
3887  /* This should be _before_ the size of the city is increased. Thus, the
3888  * order of the messages is correct (1: migration; 2: increased size). */
3889  if (pplayer_from == pplayer_to) {
3890  // migration between one nation
3891  notify_player(pplayer_from, ptile_to, E_CITY_TRANSFER, ftc_server,
3892  // TRANS: From <city1> to <city2>.
3893  _("Migrants from %s moved to %s in search of a better "
3894  "life."),
3895  name_from, name_to);
3896  } else {
3897  // migration between different nations
3898  notify_player(
3899  pplayer_from, ptile_to, E_CITY_TRANSFER, ftc_server,
3900  // TRANS: From <city1> to <city2> (<city2 nation adjective>).
3901  _("Migrants from %s moved to %s (%s) in search of a "
3902  "better life."),
3903  name_from, name_to, nation_to);
3904  notify_player(
3905  pplayer_to, ptile_to, E_CITY_TRANSFER, ftc_server,
3906  // TRANS: From <city1> (<city1 nation adjective>) to <city2>.
3907  _("Migrants from %s (%s) moved to %s in search of a "
3908  "better life."),
3909  name_from, nation_from, name_to);
3910  }
3911 
3912  // raise size of receiver city
3913  if (city_exist(to_id)) {
3914  incr_success = city_increase_size(pcity_to, pplayer_citizen);
3915  if (city_exist(to_id)) {
3916  city_refresh_vision(pcity_to);
3917  if (city_refresh(pcity_to)) {
3918  auto_arrange_workers(pcity_to);
3919  }
3920  if (incr_success) {
3921  script_server_signal_emit("city_size_change", pcity_to, 1,
3922  "migration_to");
3923  }
3924  }
3925  }
3926 
3927  log_debug("[M] T%d migration successful (%s -> %s)", game.info.turn,
3928  name_from, name_to);
3929 
3930  return true;
3931 }
3932 
3955 {
3956  bool internat = false;
3957 
3958  if (!game.server.migration) {
3959  return false;
3960  }
3961 
3962  if (game.server.mgr_turninterval <= 0
3963  || (game.server.mgr_worldchance <= 0
3964  && game.server.mgr_nationchance <= 0)) {
3965  return false;
3966  }
3967 
3968  // check for migration
3969  players_iterate(pplayer)
3970  {
3971  if (!pplayer->cities) {
3972  continue;
3973  }
3974 
3975  if (check_city_migrations_player(pplayer)) {
3976  internat = true;
3977  }
3978  }
3980 
3981  return internat;
3982 }
3983 
3988 bool city_empty_food_stock(struct city *pcity)
3989 {
3990  struct player *pplayer = city_owner(pcity);
3991  struct tile *ptile = city_tile(pcity);
3992 
3993  fc_assert_ret_val(pcity != nullptr, false);
3994 
3995  if (pcity->food_stock > 0) {
3996  pcity->food_stock = 0;
3997 
3998  notify_player(pplayer, ptile, E_DISASTER, ftc_server,
3999  // TRANS: %s is a city name
4000  _("All stored food destroyed in %s."), city_link(pcity));
4001 
4002  return true;
4003  }
4004 
4005  return false;
4006 }
4007 
4011 static void apply_disaster(struct city *pcity, struct disaster_type *pdis)
4012 {
4013  struct player *pplayer = city_owner(pcity);
4014  struct tile *ptile = city_tile(pcity);
4015  bool had_internal_effect = false;
4016 
4017  log_debug("%s at %s", disaster_rule_name(pdis), city_name_get(pcity));
4018 
4019  notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4020  // TRANS: Disasters such as Earthquake
4021  _("%s was hit by %s."), city_name_get(pcity),
4023 
4024  if (disaster_has_effect(pdis, DE_POLLUTION)) {
4025  if (place_pollution(pcity, EC_POLLUTION)) {
4026  notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4027  _("Pollution near %s."), city_link(pcity));
4028  had_internal_effect = true;
4029  }
4030  }
4031 
4032  if (disaster_has_effect(pdis, DE_FALLOUT)) {
4033  if (place_pollution(pcity, EC_FALLOUT)) {
4034  notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4035  _("Fallout near %s."), city_link(pcity));
4036  had_internal_effect = true;
4037  }
4038  }
4039 
4040  if (disaster_has_effect(pdis, DE_REDUCE_DESTROY)
4041  || (disaster_has_effect(pdis, DE_REDUCE_POP) && pcity->size > 1)) {
4042  if (!city_reduce_size(pcity, 1, nullptr, "disaster")) {
4043  notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4044  // TRANS: "Industrial Accident destroys Bogota entirely."
4045  _("%s destroys %s entirely."),
4046  disaster_name_translation(pdis), city_link(pcity));
4047  pcity = nullptr;
4048  } else {
4049  notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4050  // TRANS: "Nuclear Accident ... Montreal."
4051  _("%s causes population loss in %s."),
4052  disaster_name_translation(pdis), city_link(pcity));
4053  }
4054 
4055  had_internal_effect = true;
4056  }
4057 
4058  if (pcity && disaster_has_effect(pdis, DE_DESTROY_BUILDING)) {
4059  int total = 0;
4060  struct impr_type *imprs[B_LAST];
4061 
4062  city_built_iterate(pcity, pimprove)
4063  {
4064  if (is_improvement(pimprove)
4065  && !improvement_has_flag(pimprove, IF_DISASTER_PROOF)) {
4066  imprs[total++] = pimprove;
4067  }
4068  }
4070 
4071  if (total > 0) {
4072  int num = fc_rand(total);
4073 
4074  building_lost(pcity, imprs[num], "disaster", nullptr);
4075 
4076  notify_player(
4077  pplayer, ptile, E_DISASTER, ftc_server,
4078  // TRANS: second %s is the name of a city improvement
4079  _("%s destroys %s in %s."), disaster_name_translation(pdis),
4080  improvement_name_translation(imprs[num]), city_link(pcity));
4081 
4082  had_internal_effect = true;
4083  }
4084  }
4085 
4086  if (pcity && disaster_has_effect(pdis, DE_EMPTY_FOODSTOCK)) {
4087  if (city_empty_food_stock(pcity)) {
4088  had_internal_effect = true;
4089  }
4090  }
4091 
4092  if (pcity && disaster_has_effect(pdis, DE_EMPTY_PRODSTOCK)) {
4093  if (pcity->shield_stock > 0) {
4094  char prod[256];
4095 
4096  pcity->shield_stock = 0;
4097  nullify_prechange_production(pcity); // Make it impossible to recover
4098 
4099  universal_name_translation(&pcity->production, prod, sizeof(prod));
4100  notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4101  // TRANS: "Production of Colossus in Rhodes destroyed."
4102  _("Production of %s in %s destroyed."), prod,
4103  city_link(pcity));
4104 
4105  had_internal_effect = true;
4106  }
4107  }
4108 
4109  script_server_signal_emit("disaster_occurred", pdis, pcity,
4110  had_internal_effect);
4111  script_server_signal_emit("disaster", pdis, pcity);
4112 }
4113 
4118 {
4119  if (game.info.disasters == 0) {
4120  // Shortcut out as no disaster is possible.
4121  return;
4122  }
4123 
4124  players_iterate(pplayer)
4125  {
4126  // Safe city iterator needed as disaster may destroy city
4127  city_list_iterate_safe(pplayer->cities, pcity)
4128  {
4129  int id = pcity->id;
4130 
4131  disaster_type_iterate(pdis)
4132  {
4133  if (city_exist(id)) {
4134  // City survived earlier disasters.
4135  int probability = game.info.disasters * pdis->frequency;
4136  int result = fc_rand(DISASTER_BASE_RARITY);
4137 
4138  if (result < probability) {
4139  if (can_disaster_happen(pdis, pcity)) {
4140  apply_disaster(pcity, pdis);
4141  }
4142  }
4143  }
4144  }
4146  }
4148  }
4150 }
4151 
4160 static bool check_city_migrations_player(const struct player *pplayer)
4161 {
4162  char city_link_text[MAX_LEN_LINK];
4163  float best_city_player_score, best_city_world_score;
4164  struct city *best_city_player, *best_city_world, *acity;
4165  float score_from, score_tmp, weight;
4166  int dist, mgr_dist;
4167  bool internat = false;
4168 
4169  /* check for each city
4170  * city_list_iterate_safe_end must be used because we could
4171  * remove one city from the list */
4172  city_list_iterate_safe(pplayer->cities, pcity)
4173  {
4174  // no migration out of the capital
4175  if (is_capital(pcity)) {
4176  continue;
4177  }
4178 
4179  /* check only each (game.server.mgr_turninterval) turn
4180  * (counted from the funding turn) and do not migrate
4181  * the same turn a city is founded */
4182  if (game.info.turn == pcity->turn_founded
4183  || ((game.info.turn - pcity->turn_founded)
4184  % game.server.mgr_turninterval)
4185  != 0) {
4186  continue;
4187  }
4188 
4189  best_city_player_score = 0.0;
4190  best_city_world_score = 0.0;
4191  best_city_player = nullptr;
4192  best_city_world = nullptr;
4193 
4194  /* score of the actual city
4195  * taking into account a persistence factor of 3 */
4196  score_from = city_migration_score(pcity) * 3;
4197 
4198  log_debug("[M] T%d check city: %s score: %6.3f (%s)", game.info.turn,
4199  city_name_get(pcity), score_from, player_name(pplayer));
4200 
4201  /* consider all cities within the maximal possible distance
4202  * (= CITY_MAP_MAX_RADIUS + GAME_MAX_MGR_DISTANCE) */
4203  iterate_outward(&(wld.map), city_tile(pcity),
4205  {
4206  acity = tile_city(ptile);
4207 
4208  if (!acity || acity == pcity) {
4209  // no city or the city in the center
4210  continue;
4211  }
4212 
4213  /* Calculate the migration distance. The value of
4214  * game.server.mgr_distance is added to the current city radius. If the
4215  * distance between both cities is lower or equal than this value,
4216  * migration is possible. */
4217  mgr_dist = static_cast<int>(sqrt(static_cast<double> MAX(
4218  city_map_radius_sq_get(acity), 0)))
4219  + game.server.mgr_distance;
4220 
4221  // distance between the two cities
4222  dist = real_map_distance(city_tile(pcity), city_tile(acity));
4223 
4224  if (dist > mgr_dist) {
4225  // to far away
4226  continue;
4227  }
4228 
4229  // score of the second city, weighted by the distance
4230  weight = (static_cast<float>(mgr_dist + 1 - dist)
4231  / static_cast<float>(mgr_dist + 1));
4232  score_tmp = city_migration_score(acity) * weight;
4233 
4234  log_debug("[M] T%d - compare city: %s (%s) dist: %d mgr_dist: %d "
4235  "score: %6.3f",
4236  game.info.turn, city_name_get(acity),
4237  player_name(city_owner(acity)), dist, mgr_dist, score_tmp);
4238 
4239  if (game.server.mgr_nationchance > 0 && city_owner(acity) == pplayer) {
4240  // migration between cities of the same owner
4241  if (score_tmp > score_from && score_tmp > best_city_player_score) {
4242  // select the best!
4243  best_city_player_score = score_tmp;
4244  best_city_player = acity;
4245 
4246  log_debug("[M] T%d - best city (player): %s (%s) score: "
4247  "%6.3f (> %6.3f)",
4248  game.info.turn, city_name_get(best_city_player),
4249  player_name(pplayer), best_city_player_score,
4250  score_from);
4251  }
4252  } else if (game.server.mgr_worldchance > 0
4253  && city_owner(acity) != pplayer) {
4254  // migration between cities of different owners
4255  if (game.info.citizen_nationality) {
4256  /* Modify the score if citizens could migrate to a city of their
4257  * original nation. */
4258  if (citizens_nation_get(pcity, city_owner(acity)->slot) > 0) {
4259  score_tmp *= 2;
4260  }
4261  }
4262 
4263  if (score_tmp > score_from && score_tmp > best_city_world_score) {
4264  // select the best!
4265  best_city_world_score = score_tmp;
4266  best_city_world = acity;
4267 
4268  log_debug("[M] T%d - best city (world): %s (%s) score: "
4269  "%6.3f (> %6.3f)",
4270  game.info.turn, city_name_get(best_city_world),
4271  player_name(city_owner(best_city_world)),
4272  best_city_world_score, score_from);
4273  }
4274  }
4275  }
4277 
4278  if (best_city_player_score > 0) {
4279  // first, do the migration within one nation
4280  if (fc_rand(100) >= game.server.mgr_nationchance) {
4281  // no migration
4282  // N.B.: city_link always returns the same pointer.
4283  sz_strlcpy(city_link_text, city_link(pcity));
4284  notify_player(pplayer, city_tile(pcity), E_CITY_TRANSFER, ftc_server,
4285  _("Citizens of %s are thinking about migrating to %s "
4286  "for a better life."),
4287  city_link_text, city_link(best_city_player));
4288  } else {
4289  do_city_migration(pcity, best_city_player);
4290  }
4291 
4292  // stop here
4293  continue;
4294  }
4295 
4296  if (best_city_world_score > 0) {
4297  // second, do the migration between all nations
4298  if (fc_rand(100) >= game.server.mgr_worldchance) {
4299  const char *nname;
4300 
4301  nname = nation_adjective_for_player(city_owner(best_city_world));
4302  // no migration
4303  // N.B.: city_link always returns the same pointer.
4304  sz_strlcpy(city_link_text, city_link(pcity));
4305  notify_player(
4306  pplayer, city_tile(pcity), E_CITY_TRANSFER, ftc_server,
4307  // TRANS: <city1> to <city2> (<city2 nation adjective>).
4308  _("Citizens of %s are thinking about migrating to %s "
4309  "(%s) for a better life."),
4310  city_link_text, city_link(best_city_world), nname);
4311  } else {
4312  do_city_migration(pcity, best_city_world);
4313  internat = true;
4314  }
4315 
4316  // stop here
4317  continue;
4318  }
4319  }
4321 
4322  return internat;
4323 }
4324 
4328 void city_style_refresh(struct city *pcity)
4329 {
4330  pcity->style = city_style(pcity);
4331 }
const char * achievement_name_translation(struct achievement *pach)
Return translated name of this achievement type.
const struct action * action_auto_perf_unit_do(const enum action_auto_perf_cause cause, struct unit *actor, const struct player *other_player, const struct output_type *output, const struct tile *target_tile, const struct city *target_city, const struct unit *target_unit, const struct extra_type *target_extra)
Make the specified actor unit perform an action because of cause.
const struct action_auto_perf * action_auto_perf_unit_sel(const enum action_auto_perf_cause cause, const struct unit *actor, const struct player *other_player, const struct output_type *output)
Returns the action auto performer that the specified cause can force the specified actor to perform.
void advisor_choose_build(struct player *pplayer, struct city *pcity)
Setup improvement building.
int tile_border_source_radius_sq(struct tile *ptile)
Border radius sq from given border source tile.
Definition: borders.cpp:27
const char * textcalfrag(int frag)
Produce a statically allocated textual representation of the given calendar fragment.
Definition: calendar.cpp:97
const char * textyear(int year)
Produce a statically allocated textual representation of the given year.
Definition: calendar.cpp:116
void citizens_nation_add(struct city *pcity, const struct player_slot *pslot, int add)
Add a (positive or negative) value to the citizens of the given nationality.
Definition: citizens.cpp:96
citizens citizens_nation_get(const struct city *pcity, const struct player_slot *pslot)
Get the number of citizens with the given nationality.
Definition: citizens.cpp:65
struct player_slot * citizens_random(const struct city *pcity)
Return random citizen from city.
Definition: citizens.cpp:185
void citizens_convert(struct city *pcity)
Convert one (random) foreign citizen to the nationality of the owner.
void citizens_update(struct city *pcity, struct player *plr)
struct output_type * get_output_type(Output_type_id output)
Return the output type for this index.
Definition: city.cpp:611
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
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
struct city * city_list_find_number(struct city_list *This, int id)
Find city with given id from list.
Definition: city.cpp:1598
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 player * city_owner(const struct city *pcity)
Return the owner of the city.
Definition: city.cpp:1083
bool is_capital(const struct city *pcity)
Return TRUE iff this city is its nation's capital.
Definition: city.cpp:1495
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
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
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_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
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
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
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
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
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
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
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
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
bool city_happy(const struct city *pcity)
Return TRUE iff the city is happy.
Definition: city.cpp:1531
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_map_radius_sq_get(const struct city *pcity)
Returns the current squared radius of the city.
Definition: city.cpp:130
static int cmp(int v1, int v2)
Compare two integer values, as required by qsort.
Definition: city.cpp:317
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
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 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
bool city_exist(int id)
Check if city with given id still exist.
Definition: city.cpp:3465
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
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
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
bool city_had_recent_plague(const struct city *pcity)
Returns whether city had a plague outbreak this turn.
Definition: city.cpp:2800
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_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
#define cities_iterate_end
Definition: city.h:493
#define city_list_iterate_safe(citylist, _city)
Definition: city.h:500
#define city_list_iterate(citylist, pcity)
Definition: city.h:482
#define cities_iterate(pcity)
Definition: city.h:486
#define city_tile_iterate(_radius_sq, _city_tile, _tile)
Definition: city.h:202
@ 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 output_type_iterate(output)
Definition: city.h:764
#define INCITE_IMPOSSIBLE_COST
Definition: city.h:69
#define city_list_iterate_end
Definition: city.h:484
#define city_list_iterate_safe_end
Definition: city.h:521
@ FEELING_FINAL
Definition: city.h:256
#define city_tile_iterate_skip_center_end
Definition: city.h:193
#define city_tile_iterate_skip_center(_radius_sq, _city_tile, _tile, _index, _x, _y)
Definition: city.h:183
#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_built_iterate_end
Definition: city.h:759
#define output_type_iterate_end
Definition: city.h:771
void city_map_update_empty(struct city *pcity, struct tile *ptile)
Change from worked to empty.
Definition: citytools.cpp:3079
struct trade_route * remove_trade_route(struct city *pc1, struct trade_route *proute, bool announce, bool source_gone)
Remove the trade route between pc1 and pc2 (if one exists).
Definition: citytools.cpp:2791
struct city * find_closest_city(const struct tile *ptile, const struct city *pexclcity, const struct player *pplayer, bool only_ocean, bool only_continent, bool only_known, bool only_player, bool only_enemy, const struct unit_class *pclass)
Find the city closest to 'ptile'.
Definition: citytools.cpp:849
void send_city_info(struct player *dest, struct city *pcity)
A wrapper, accessing either broadcast_city_info() (dest == nullptr), or a convenience case of send_ci...
Definition: citytools.cpp:2295
void city_freeze_workers(struct city *pcity)
Freeze the workers (citizens on tiles) for the city.
Definition: citytools.cpp:118
void city_illness_strike(struct city *pcity)
Give the city a plague.
Definition: citytools.cpp:2829
bool city_map_update_radius_sq(struct city *pcity)
Updates the squared city radius.
Definition: citytools.cpp:3286
void change_build_target(struct player *pplayer, struct city *pcity, struct universal *target, enum event_type event)
Change the build target.
Definition: citytools.cpp:2986
void city_thaw_workers(struct city *pcity)
Thaw (unfreeze) the workers (citizens on tiles) for the city.
Definition: citytools.cpp:128
void building_lost(struct city *pcity, const struct impr_type *pimprove, const char *reason, struct unit *destroyer)
Destroy the improvement in the city straight-out.
Definition: citytools.cpp:2878
void city_units_upkeep(const struct city *pcity)
Recalculate the upkeep needed for all units supported by the city.
Definition: citytools.cpp:2927
void remove_city(struct city *pcity)
Remove a city from the game.
Definition: citytools.cpp:1676
void do_sell_building(struct player *pplayer, struct city *pcity, struct impr_type *pimprove, const char *reason)
Sell the improvement from the city, and give the player the owner.
Definition: citytools.cpp:2849
void city_map_update_all(struct city *pcity)
Called by auto_arrange_workers() and below.
Definition: citytools.cpp:3173
void transfer_city_units(struct player *pplayer, struct player *pvictim, struct unit_list *units, struct city *pcity, struct city *exclude_city, int kill_outside, bool verbose)
When a city is transferred (bought, incited, disbanded, civil war): Units in a transferred city are t...
Definition: citytools.cpp:708
void city_map_update_worker(struct city *pcity, struct tile *ptile)
Change from empty to worked.
Definition: citytools.cpp:3091
void sync_cities()
Make sure all players (clients) have up-to-date information about all their cities.
Definition: citytools.cpp:3150
void city_refresh_vision(struct city *pcity)
Refresh the city's vision.
Definition: citytools.cpp:3261
#define LOG_BUILD_TARGET
Definition: citytools.h:21
static bool city_illness_check(const struct city *pcity)
Check if city suffers from a plague.
Definition: cityturn.cpp:3484
static void uk_rem_gold_append(struct unit *punit)
Add a unit to uk_rem_gold and make the unit remove it self from it if it dies before it is processed.
Definition: cityturn.cpp:2760
static bool city_handle_disorder(struct city *pcity)
Definition: cityturn.cpp:3282
static bool worklist_change_build_target(struct player *pplayer, struct city *pcity)
Examine the worklist and change the build target.
Definition: cityturn.cpp:1898
void remove_obsolete_buildings(struct player *pplayer)
Sell obsolete buildings from all cities of the player.
Definition: cityturn.cpp:263
static bool city_build_stuff(struct player *pplayer, struct city *pcity)
Returns FALSE when the city is removed, TRUE otherwise.
Definition: cityturn.cpp:2659
static void city_reset_foodbox(struct city *pcity, int new_size)
Reset the foodbox, usually when a city grows or shrinks.
Definition: cityturn.cpp:946
static const struct unit_type * unit_upgrades_to(struct city *pcity, const struct unit_type *id)
Follow the list of obsoleted_by units until we hit something that we can build.
Definition: cityturn.cpp:2181
static void apply_disaster(struct city *pcity, struct disaster_type *pdis)
Disaster has hit a city.
Definition: cityturn.cpp:4011
static bool city_distribute_surplus_shields(struct player *pplayer, struct city *pcity)
Disband units if we don't have enough shields to support them.
Definition: cityturn.cpp:2219
static int surplus_savings(const struct city *pcity)
Return the percentage of the food surplus that is saved in this city.
Definition: cityturn.cpp:934
static void unit_list_referred_destroy(struct unit_list *punitlist)
Destroy a unit list and make the units it contains aware that it no longer refers to them.
Definition: cityturn.cpp:2773
static bool place_pollution(struct city *pcity, enum extra_cause cause)
Add some Pollution if we have waste.
Definition: cityturn.cpp:3079
static bool city_handle_plague_risk(struct city *pcity)
Definition: cityturn.cpp:3263
void choose_build_target(struct player *pplayer, struct city *pcity)
Assuming we just finished building something, find something new to build.
Definition: cityturn.cpp:2089
static void uk_rem_gold_callback(struct unit *punit)
Call back for when a unit in uk_rem_gold dies.
Definition: cityturn.cpp:2739
void auto_arrange_workers(struct city *pcity)
Call sync_cities() to send the affected cities to the clients.
Definition: cityturn.cpp:359
void city_refresh_queue_add(struct city *pcity)
Queue pending city_refresh() for later.
Definition: cityturn.cpp:188
static struct unit_list * uk_rem_gold
Definition: cityturn.cpp:80
static bool city_balance_treasury_buildings(struct city *pcity)
Balance the gold of one city by randomly selling some buildings.
Definition: cityturn.cpp:2990
static void update_city_activity(struct city *pcity)
Called every turn, at end of turn, for every city.
Definition: cityturn.cpp:3334
static void upgrade_unit_prod(struct city *pcity)
Try to upgrade production in pcity.
Definition: cityturn.cpp:2201
static float city_migration_score(struct city *pcity)
Helper function to calculate a "score" of a city.
Definition: cityturn.cpp:3602
void nullify_prechange_production(struct city *pcity)
Initialize all variables containing information about production before it was changed.
Definition: cityturn.cpp:3254
bool city_empty_food_stock(struct city *pcity)
Returns TRUE iff the city's food stock was emptied.
Definition: cityturn.cpp:3988
bool check_city_migrations()
Check for citizens who want to migrate between the cities that overlap.
Definition: cityturn.cpp:3954
static void upgrade_building_prod(struct city *pcity)
Try to upgrade production in pcity.
Definition: cityturn.cpp:2157
void apply_cmresult_to_city(struct city *pcity, const std::unique_ptr< cm_result > &cmr)
Rearrange workers according to a cm_result struct.
Definition: cityturn.cpp:276
bool city_change_size(struct city *pcity, citizens size, struct player *nationality, const char *reason)
Change the city size.
Definition: cityturn.cpp:1067
static citizens city_reduce_specialists(struct city *pcity, citizens change)
Reduce the city specialists by some (positive) value.
Definition: cityturn.cpp:769
static void city_global_turn_notify(struct conn_list *dest)
Notices about cities that should be sent to all players.
Definition: cityturn.cpp:461
void city_refresh_queue_processing()
Refresh the listed cities.
Definition: cityturn.cpp:204
static void city_populate(struct city *pcity, struct player *nationality)
Check whether the population can be increased or if the city is unable to support a 'settler'....
Definition: cityturn.cpp:1110
static void define_orig_production_values(struct city *pcity)
Called every turn, at beginning of turn, for every city.
Definition: cityturn.cpp:3223
void city_repair_size(struct city *pcity, int change)
Repair the city population without affecting city size.
Definition: cityturn.cpp:895
#define cityimpr_list_iterate(cityimprlist, pcityimpr)
Definition: cityturn.cpp:112
static bool upkeep_kill_unit(struct unit *punit, Output_type_id outp, enum unit_loss_reason wipe_reason, bool wipe_in_the_end)
Try to get rid of a unit because of missing upkeep.
Definition: cityturn.cpp:737
static bool city_build_building(struct player *pplayer, struct city *pcity)
Returns FALSE when the city is removed, TRUE otherwise.
Definition: cityturn.cpp:2316
static const struct impr_type * building_upgrades_to(struct city *pcity, const struct impr_type *pimprove)
Follow the list of replacement buildings until we hit something that we can build.
Definition: cityturn.cpp:2137
void send_city_turn_notifications(struct connection *pconn)
Send global and player specific city turn notifications.
Definition: cityturn.cpp:548
static struct unit * sell_random_unit(struct player *pplayer, struct unit_list *punitlist)
Randomly "sell" a unit from the given list.
Definition: cityturn.cpp:2796
int city_incite_cost(struct player *pplayer, struct city *pcity)
Returns the cost to incite a city.
Definition: cityturn.cpp:3135
static struct unit * city_create_unit(struct city *pcity, const struct unit_type *utype)
Helper function to create one unit in a city.
Definition: cityturn.cpp:2470
static bool city_increase_size(struct city *pcity, struct player *nationality)
Increase city size by one.
Definition: cityturn.cpp:965
static void nullify_caravan_and_disband_plus(struct city *pcity)
Let the advisor set up city building target.
Definition: cityturn.cpp:3244
static bool sell_random_building(struct player *pplayer, struct cityimpr_list *imprs)
Randomly sell a building from the given list.
Definition: cityturn.cpp:2691
static bool disband_city(struct city *pcity)
Disband a city into the built unit, supported by the closest city.
Definition: cityturn.cpp:3493
void update_city_activities(struct player *pplayer)
Update all cities of one nation (costs for buildings, unit upkeep, ...).
Definition: cityturn.cpp:589
bool city_reduce_size(struct city *pcity, citizens pop_loss, struct player *destroyer, const char *reason)
Reduce the city size.
Definition: cityturn.cpp:815
#define cityimpr_list_iterate_end
Definition: cityturn.cpp:114
bool city_refresh(struct city *pcity)
Updates unit upkeeps and city internal cached data.
Definition: cityturn.cpp:147
static void city_turn_notify(const struct city *pcity, struct conn_list *dest, const struct player *cache_for_player)
Send turn notifications for specified city to specified connections.
Definition: cityturn.cpp:483
static void wonder_set_build_turn(struct player *pplayer, const struct impr_type *pimprove)
Record the build turn of a wonder.
Definition: cityturn.cpp:2301
void check_disasters()
Check for any disasters hitting any city, and apply those disasters.
Definition: cityturn.cpp:4117
void city_style_refresh(struct city *pcity)
Recheck and store style of the city.
Definition: cityturn.cpp:4328
static bool city_balance_treasury_units(struct city *pcity)
Balance the gold of one city by randomly selling some units which need gold upkeep.
Definition: cityturn.cpp:3042
static bool do_city_migration(struct city *pcity_from, struct city *pcity_to)
Do the migrations between the cities that overlap, if the growth of the target city is not blocked du...
Definition: cityturn.cpp:3701
void city_save_surpluses(struct city *pcity)
Save city surplus values for use during city processing.
Definition: cityturn.cpp:580
static struct city_list * city_refresh_queue
Definition: cityturn.cpp:74
void city_refresh_for_player(struct player *pplayer)
Called on government change or wonder completion or stuff like that – Syela.
Definition: cityturn.cpp:171
static int granary_savings(const struct city *pcity)
Return the percentage of food that is lost in this city.
Definition: cityturn.cpp:918
static void check_pollution(struct city *pcity)
Add some Pollution if we have waste.
Definition: cityturn.cpp:3118
static void set_default_city_manager(struct cm_parameter *cmp, struct city *pcity)
Set default city manager parameter for the city.
Definition: cityturn.cpp:311
static citizens city_reduce_workers(struct city *pcity, citizens change)
Reduce the city workers by some (positive) value.
Definition: cityturn.cpp:791
static bool worklist_item_postpone_req_vec(struct universal *target, struct city *pcity, struct player *pplayer, int saved_id)
Examine an unbuildable build target from a city's worklist to see if it can be postponed.
Definition: cityturn.cpp:1186
static bool player_balance_treasury_units_and_buildings(struct player *pplayer)
Balance the gold of a nation by selling some random units and buildings.
Definition: cityturn.cpp:2878
void remove_obsolete_buildings_city(struct city *pcity, bool refresh)
Automatically sells obsolete buildings from city.
Definition: cityturn.cpp:228
static bool check_city_migrations_player(const struct player *pplayer)
Check for migration for each city of one player.
Definition: cityturn.cpp:4160
static bool city_build_unit(struct player *pplayer, struct city *pcity)
Build city units.
Definition: cityturn.cpp:2515
static bool player_balance_treasury_units(struct player *pplayer)
Balance the gold of a nation by selling some units which need gold upkeep.
Definition: cityturn.cpp:2950
void cm_copy_parameter(struct cm_parameter *dest, const struct cm_parameter *const src)
Copy the parameter from the source to the destination field.
Definition: cm.cpp:2163
void cm_init_parameter(struct cm_parameter *dest)
Initialize the parameter to sane default values.
Definition: cm.cpp:2172
void cm_query_result(struct city *pcity, const struct cm_parameter *param, std::unique_ptr< cm_result > &result, bool negative_ok)
Wrapper that actually runs the branch & bound, and returns the best solution.
Definition: cm.cpp:2115
std::unique_ptr< cm_result > cm_result_new(struct city *pcity)
Create a new cm_result.
Definition: cm.cpp:330
void cm_init_emergency_parameter(struct cm_parameter *dest)
Initialize the parameter to sane default values that will always produce a result.
Definition: cm.cpp:2192
void cm_print_result(const std::unique_ptr< cm_result > &result)
Print debugging information about a full CM result.
Definition: cm.cpp:2472
void cm_print_city(const struct city *pcity)
Debugging routines.
Definition: cm.cpp:2432
void conn_list_do_unbuffer(struct conn_list *dest)
Convenience functions to unbuffer a list of connections.
Definition: connection.cpp:319
struct player * conn_get_player(const struct connection *pconn)
Returns the player that this connection is attached to, or nullptr.
Definition: connection.cpp:693
void conn_list_do_buffer(struct conn_list *dest)
Convenience functions to buffer a list of connections.
Definition: connection.cpp:310
int city_culture(const struct city *pcity)
Return current culture score of the city.
Definition: culture.cpp:23
int city_history_gain(const struct city *pcity)
How much history city gains this turn.
Definition: culture.cpp:31
bool can_disaster_happen(const struct disaster_type *pdis, const struct city *pcity)
Whether disaster can happen in given city.
Definition: disaster.cpp:108
const char * disaster_rule_name(struct disaster_type *pdis)
Return untranslated name of this disaster type.
Definition: disaster.cpp:91
bool disaster_has_effect(const struct disaster_type *pdis, enum disaster_effect_id effect)
Check if disaster provides effect.
Definition: disaster.cpp:99
const char * disaster_name_translation(struct disaster_type *pdis)
Return translated name of this disaster type.
Definition: disaster.cpp:83
#define disaster_type_iterate(_p)
Definition: disaster.h:70
#define DISASTER_BASE_RARITY
Definition: disaster.h:36
#define disaster_type_iterate_end
Definition: disaster.h:76
int get_current_construction_bonus(const struct city *pcity, enum effect_type effect_type, const enum req_problem_type prob_type)
Returns the effect bonus the currently-in-construction-item will provide.
Definition: effects.cpp:933
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
struct extra_type * rand_extra_for_tile(struct tile *ptile, enum extra_cause cause, bool generated)
Return random extra type for given cause that is native to the tile.
Definition: extras.cpp:242
const char * extra_name_translation(const struct extra_type *pextra)
Return the (translated) name of the extra type.
Definition: extras.cpp:165
int Tech_type_id
Definition: fc_types.h:294
unsigned char citizens
Definition: fc_types.h:305
@ RPT_CERTAIN
Definition: fc_types.h:568
@ RPT_POSSIBLE
Definition: fc_types.h:567
#define MAX_LEN_NAME
Definition: fc_types.h:61
@ 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
enum output_type_id Output_type_id
Definition: fc_types.h:295
#define Q_(String)
Definition: fcintl.h:53
#define PL_(String1, String2, n)
Definition: fcintl.h:54
#define _(String)
Definition: fcintl.h:50
const char * city_tile_link(const struct city *pcity)
Get a text link to a city tile (make a clickable link to a tile with the city name as text).
const char * unit_tile_link(const struct unit *punit)
Get a text link to a unit tile (make a clickable link to a tile with the unit type name as text).
const struct ft_color ftc_server
const char * city_link(const struct city *pcity)
Get a text link to a city.
const char * unit_link(const struct unit *punit)
Get a text link to an unit.
#define MAX_LEN_LINK
struct civ_game game
Definition: game.cpp:47
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
#define GAME_MAX_MGR_DISTANCE
Definition: game.h:492
struct government * government_of_city(const struct city *pcity)
Return the government of the player who owns the city.
Definition: government.cpp:116
const char * ruler_title_for_player(const struct player *pplayer, char *buf, size_t buf_len)
Return the ruler title of the player (translated).
Definition: government.cpp:366
Government_type_id government_number(const struct government *pgovern)
Return the government index.
Definition: government.cpp:84
const char * government_name_translation(const struct government *pgovern)
Return the (translated) name of the given government.
Definition: government.cpp:136
int impr_sell_gold(const struct impr_type *pimprove)
Returns the amount of gold received when this improvement is sold.
bool can_city_sell_building(const struct city *pcity, const struct impr_type *pimprove)
Return TRUE iff the city can sell the given improvement.
const char * improvement_rule_name(const struct impr_type *pimprove)
Return the (untranslated) rule name of the improvement.
const struct impr_type * valid_improvement(const struct impr_type *pimprove)
Returns pointer when the improvement_type "exists" in this game, returns nullptr otherwise.
bool is_improvement(const struct impr_type *pimprove)
Is this building a regular improvement?
Impr_type_id improvement_number(const struct impr_type *pimprove)
Return the improvement index.
int impr_build_shield_cost(const struct city *pcity, const struct impr_type *pimprove)
Returns the number of shields it takes to build this improvement.
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.
const struct impr_type * improvement_replacement(const struct impr_type *pimprove)
Try to find a sensible replacement building, based on other buildings that may have caused this one t...
const char * improvement_name_translation(const struct impr_type *pimprove)
Return the (translated) name of the given improvement.
bool is_small_wonder(const struct impr_type *pimprove)
Is this building a small wonder?
#define B_LAST
Definition: improvement.h:33
#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_msg(condition, message,...)
Definition: log.h:129
#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
#define fc_assert_ret_val_msg(condition, val, message,...)
Definition: log.h:132
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Return real distance between two tiles.
Definition: map.cpp:599
int map_distance(const struct tile *tile0, const struct tile *tile1)
Return Manhattan distance between two tiles.
Definition: map.cpp:623
#define iterate_outward(nmap, start_tile, max_dist, itr_tile)
Definition: map.h:288
#define iterate_outward_end
Definition: map.h:291
void map_update_border(struct tile *ptile, struct player *owner, int old_radius_sq, int new_radius_sq)
Update borders for this source.
Definition: maphand.cpp:2121
void map_claim_border(struct tile *ptile, struct player *owner, int radius_sq)
Update borders for this source.
Definition: maphand.cpp:2155
void update_tile_knowledge(struct tile *ptile)
Update playermap knowledge for everybody who sees the tile, and send a packet to everyone whose info ...
Definition: maphand.cpp:1395
struct nation_type * nation_of_city(const struct city *pcity)
Return the nation of the player who owns the city.
Definition: nation.cpp:429
const char * nation_group_name_translation(const struct nation_group *pgroup)
Return the translated name of a nation group.
Definition: nation.cpp:953
const char * nation_rule_name(const struct nation_type *pnation)
Return the (untranslated) rule name of the nation (adjective form).
Definition: nation.cpp:115
const char * nation_adjective_translation(const struct nation_type *pnation)
Return the (translated) adjective for the given nation.
Definition: nation.cpp:126
const char * nation_plural_for_player(const struct player *pplayer)
Return the (translated) plural noun of the given nation of a player.
Definition: nation.cpp:155
const char * nation_adjective_for_player(const struct player *pplayer)
Return the (translated) adjective for the given nation of a player.
Definition: nation.cpp:146
const char * nation_plural_translation(const struct nation_type *pnation)
Return the (translated) plural noun of the given nation.
Definition: nation.cpp:136
void notify_research(const struct research *presearch, const struct player *exclude, enum event_type event, const struct ft_color color, const char *format,...)
Sends a message to all players that share research.
Definition: notify.cpp:386
void notify_player(const struct player *pplayer, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
Similar to notify_conn_packet (see also), but takes player as "destination".
Definition: notify.cpp:284
void notify_research_embassies(const struct research *presearch, const struct player *exclude, enum event_type event, const struct ft_color color, const char *format,...)
Sends a message to all players that have embassies with someone who shares research.
Definition: notify.cpp:426
void notify_conn(struct conn_list *dest, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
See notify_conn_packet - this is just the "non-v" version, with varargs.
Definition: notify.cpp:235
void package_event(struct packet_chat_msg *packet, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
Fill a packet_chat_msg structure for common server event.
Definition: notify.cpp:164
void event_cache_add_for_player(const struct packet_chat_msg *packet, const struct player *pplayer)
Add an event to the cache for one player.
Definition: notify.cpp:636
struct city_list * cities
Definition: packhand.cpp:122
const char * diplrel_name_translation(int value)
Return the translated name of the given diplomatic relation.
Definition: player.cpp:1538
int player_number(const struct player *pplayer)
Return the player index/number/id.
Definition: player.cpp:756
std::vector< city * > player_gov_centers(const struct player *pplayer)
Locate the player's government centers.
Definition: player.cpp:1264
struct city * player_city_by_number(const struct player *pplayer, int city_id)
If the specified player owns the city with the specified id, return pointer to the city struct.
Definition: player.cpp:1113
struct player * player_slot_get_player(const struct player_slot *pslot)
Returns the team corresponding to the slot.
Definition: player.cpp:384
const char * player_name(const struct player *pplayer)
Return the leader name of the player.
Definition: player.cpp:816
#define players_iterate_end
Definition: player.h:520
#define players_iterate(_pplayer)
Definition: player.h:514
static bool is_barbarian(const struct player *pplayer)
Definition: player.h:474
#define is_ai(plr)
Definition: player.h:227
void send_player_info_c(struct player *src, struct conn_list *dest)
Send information about player slot 'src', or all valid (i.e.
Definition: plrhand.cpp:1048
void handle_player_change_government(struct player *pplayer, Government_type_id government)
Called by the client or AI to change government.
Definition: plrhand.cpp:427
#define fc_rand(_size)
Definition: rand.h:16
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.
const char * universal_name_translation(const struct universal *psource, char *buf, size_t bufsz)
Make user-friendly text for the source.
const char * universal_rule_name(const struct universal *psource)
Return the (untranslated) rule name of the universal.
#define requirement_vector_iterate_end
Definition: requirements.h:80
#define requirement_vector_iterate(req_vec, preq)
Definition: requirements.h:78
struct research * research_get(const struct player *pplayer)
Returns the research structure associated with the player.
Definition: research.cpp:110
QString research_advance_name_translation(const struct research *presearch, Tech_type_id tech)
Store the translated name of the given tech (including A_FUTURE) in 'buf'.
Definition: research.cpp:257
enum tech_state research_invention_state(const struct research *presearch, Tech_type_id tech)
Returns state of the tech for current research.
Definition: research.cpp:609
int research_pretty_name(const struct research *presearch, char *buf, size_t buf_len)
Set in 'buf' the name of the research owner.
Definition: research.cpp:163
#define sanity_check_city(x)
Definition: sanitycheck.h:35
void script_server_signal_emit(const char *signal_name,...)
Invoke all the callback functions attached to a given signal.
QString ssetv_human_readable(ssetv val, bool present)
Returns the server setting - value pair formated in a user readable way.
static struct setting settings[]
Definition: settings.cpp:1338
#define CLIP(lower, current, upper)
Definition: shared.h:51
#define MIN(x, y)
Definition: shared.h:49
#define FC_INFINITY
Definition: shared.h:32
#define MAX(x, y)
Definition: shared.h:48
void send_spaceship_info(struct player *src, struct conn_list *dest)
Send details of src's spaceship (or spaceships of all players if src is nullptr) to specified destina...
Definition: spacerace.cpp:120
@ SSHIP_STARTED
Definition: spaceship.h:79
@ SSHIP_NONE
Definition: spaceship.h:78
#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
size_t size
Definition: specvec.h:64
@ AIT_CITIZEN_ARRANGE
Definition: srv_log.h:52
@ TIMER_STOP
Definition: srv_log.h:77
@ TIMER_START
Definition: srv_log.h:77
#define CITY_LOG(_, pcity, msg,...)
Definition: srv_log.h:80
#define TIMING_LOG(timer, activity)
Definition: srv_log.h:121
Definition: city.h:291
int turn_last_built
Definition: city.h:358
struct city::@14 rally_point
int surplus[O_LAST]
Definition: city.h:324
int food_stock
Definition: city.h:338
struct player * original
Definition: city.h:295
int history
Definition: city.h:379
int pollution
Definition: city.h:340
bool did_sell
Definition: city.h:352
int id
Definition: city.h:296
int saved_surplus[O_LAST]
Definition: city.h:332
int last_turns_shield_surplus
Definition: city.h:364
int disbanded_shields
Definition: city.h:363
bool was_happy
Definition: city.h:353
struct player * owner
Definition: city.h:294
int airlift
Definition: city.h:349
int caravan_shields
Definition: city.h:362
bool did_buy
Definition: city.h:351
int anarchy
Definition: city.h:355
struct worklist worklist
Definition: city.h:373
struct universal production
Definition: city.h:368
struct unit_order * orders
Definition: city.h:391
bool vigilant
Definition: city.h:390
citizens size
Definition: city.h:301
int before_change_shields
Definition: city.h:360
int length
Definition: city.h:386
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
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
bool persistent
Definition: city.h:388
int rapture
Definition: city.h:356
struct city * pcity
Definition: cityturn.cpp:104
struct impr_type * pimprove
Definition: cityturn.cpp:105
struct civ_game::@28::@32 server
struct conn_list * est_connections
Definition: game.h:88
struct packet_game_info info
Definition: game.h:80
struct conn_list * self
Definition: connection.h:150
struct requirement_vector reqs
Definition: improvement.h:66
int infra_points
Definition: player.h:57
int units_built
Definition: player.h:98
enum spaceship_state state
Definition: spaceship.h:105
Definition: player.h:231
struct city_list * cities
Definition: player.h:263
struct player::@65::@67 server
int wonder_build_turn[B_LAST]
Definition: player.h:286
struct conn_list * connections
Definition: player.h:280
struct player_economic economic
Definition: player.h:266
struct player_spaceship spaceship
Definition: player.h:268
struct player_score score
Definition: player.h:265
struct player_slot * slot
Definition: player.h:232
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
struct advance * require_advance
Definition: unittype.h:484
int upkeep[O_LAST]
Definition: unittype.h:503
Definition: unit.h:134
time_t action_timestamp
Definition: unit.h:203
int length
Definition: unit.h:192
bool has_orders
Definition: unit.h:190
int id
Definition: unit.h:141
bool vigilant
Definition: unit.h:194
struct unit::@76::@79 server
struct unit_order * list
Definition: unit.h:195
int action_turn
Definition: unit.h:204
struct unit::@75 orders
const struct unit_type * utype
Definition: unit.h:135
enum universals_n kind
Definition: fc_types.h:740
universals_u value
Definition: fc_types.h:739
struct civ_map map
Definition: world_object.h:21
int city_style(struct city *pcity)
Return citystyle of the city.
Definition: style.cpp:234
const char * style_name_translation(const struct nation_style *pstyle)
Return the (translated) name of the style.
Definition: style.cpp:85
#define sz_strlcpy(dest, src)
Definition: support.h:140
Tech_type_id advance_number(const struct advance *padvance)
Return the advance index.
Definition: tech.cpp:85
const char * advance_name_translation(const struct advance *padvance)
Return the (translated) name of the given advance/technology.
Definition: tech.cpp:274
Tech_type_id pick_free_tech(struct research *presearch)
Choose a free tech.
Definition: techtools.cpp:1380
void give_immediate_free_tech(struct research *presearch, Tech_type_id tech)
Give an immediate free tech (probably chosen with pick_free_tech()).
Definition: techtools.cpp:1399
void update_bulbs(struct player *pplayer, int bulbs, bool check_tech)
Adds the given number of bulbs into the player's tech and (if necessary and 'check_tech' is TRUE) com...
Definition: techtools.cpp:650
const char * terrain_class_name_translation(enum terrain_class tclass)
Return the (translated) name of the given terrain class.
Definition: terrain.cpp:546
const char * terrain_name_translation(const struct terrain *pterrain)
Return the (translated) name of the terrain.
Definition: terrain.cpp:175
void tile_add_extra(struct tile *ptile, const struct extra_type *pextra)
Adds extra to tile.
Definition: tile.cpp:974
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_worked(_tile)
Definition: tile.h:97
#define tile_has_extra(ptile, pextra)
Definition: tile.h:130
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
const char * goods_name_translation(struct goods_type *pgood)
Return translated name of this goods type.
bool goods_has_flag(const struct goods_type *pgood, enum goods_flag_id flag)
Check if goods has given flag.
struct trade_route_settings * trade_route_settings_by_type(enum trade_route_type type)
Get trade route settings related to type.
bool goods_can_be_provided(struct city *pcity, struct goods_type *pgood, struct unit *punit)
Can the city provide goods.
@ TRI_CANCEL
Definition: traderoutes.h:26
#define trade_routes_iterate_safe_end
Definition: traderoutes.h:148
#define trade_routes_iterate_safe(c, proute)
Definition: traderoutes.h:133
#define trade_partners_iterate_end
Definition: traderoutes.h:163
#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_is_alive(int id)
Check if unit with given id is still alive.
Definition: unit.cpp:2014
#define unit_tile(_pu)
Definition: unit.h:371
#define unit_cargo_iterate_end
Definition: unit.h:464
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition: unit.h:461
#define unit_owner(_pu)
Definition: unit.h:370
#define unit_list_iterate(unitlist, punit)
Definition: unitlist.h:25
#define unit_list_iterate_safe(unitlist, _unit)
Definition: unitlist.h:33
#define unit_list_iterate_end
Definition: unitlist.h:27
#define unit_list_iterate_safe_end
Definition: unitlist.h:54
struct unit * create_unit(struct player *pplayer, struct tile *ptile, const struct unit_type *type, int veteran_level, int homecity_id, int moves_left)
Wrapper of the below.
Definition: unittools.cpp:1762
void unit_set_removal_callback(struct unit *punit, void(*callback)(struct unit *punit))
Set the call back to run when the server removes the unit.
Definition: unittools.cpp:1872
void unit_unset_removal_callback(struct unit *punit)
Remove the call back so nothing runs when the server removes the unit.
Definition: unittools.cpp:1886
void wipe_unit(struct unit *punit, enum unit_loss_reason reason, struct player *killer)
Remove the unit, and passengers if it is a carrying any.
Definition: unittools.cpp:2248
const char * unit_rule_name(const struct unit *punit)
Return the (untranslated) rule name of the unit.
Definition: unittype.cpp:1283
const struct unit_type * unit_type_get(const struct unit *punit)
Return the unit type for this unit.
Definition: unittype.cpp:114
int utype_pop_value(const struct unit_type *punittype)
How much city shrinks when it builds unit of this type.
Definition: unittype.cpp:1231
int unit_build_shield_cost(const struct city *pcity, const struct unit *punit)
Returns the number of shields it takes to build this unit.
Definition: unittype.cpp:1176
int utype_upkeep_cost(const struct unit_type *ut, struct player *pplayer, Output_type_id otype)
Returns the upkeep of a unit of this type under the given government.
Definition: unittype.cpp:123
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
const char * utype_name_translation(const struct unit_type *punittype)
Return the (translated) name of the unit type.
Definition: unittype.cpp:1256
const char * unit_name_translation(const struct unit *punit)
Return the (translated) name of the unit.
Definition: unittype.cpp:1265
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition: unittype.h:584
#define U_NOT_OBSOLETED
Definition: unittype.h:493
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
bool worklist_is_empty(const struct worklist *pwl)
Returns whether worklist has no elements.
Definition: worklist.cpp:60
void worklist_remove(struct worklist *pwl, int idx)
Remove element from position idx.
Definition: worklist.cpp:113
int worklist_length(const struct worklist *pwl)
Returns the number of entries in the worklist.
Definition: worklist.cpp:51