Freeciv21
Develop your civilization from humble roots to a global empire
citytools.cpp
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 
14 #include <QBitArray>
15 
16 #include "bitvector.h"
17 #include "fcintl.h"
18 #include "log.h"
19 #include "rand.h"
20 #include "shared.h"
21 #include "support.h"
22 
23 // common
24 #include "ai.h"
25 #include "citizens.h"
26 #include "city.h"
27 #include "culture.h"
28 #include "game.h"
29 #include "government.h"
30 #include "idex.h"
31 #include "improvement.h"
32 #include "map.h"
33 #include "movement.h"
34 #include "player.h"
35 #include "requirements.h"
36 #include "research.h"
37 #include "road.h"
38 #include "specialist.h"
39 #include "tech.h"
40 #include "traderoutes.h"
41 #include "unit.h"
42 #include "unitlist.h"
43 #include "vision.h"
44 
45 /* common/aicore */
46 #include "cm.h"
47 
48 /* common/scriptcore */
49 #include "luascript_types.h"
50 
51 // server
52 #include "barbarian.h"
53 #include "citizenshand.h"
54 #include "cityturn.h"
55 #include "gamehand.h" // send_game_info()
56 #include "maphand.h"
57 #include "notify.h"
58 #include "plrhand.h"
59 #include "sanitycheck.h"
60 #include "sernet.h"
61 #include "spacerace.h"
62 #include "srv_main.h"
63 #include "techtools.h"
64 #include "unithand.h"
65 #include "unittools.h"
66 
67 /* server/advisors */
68 #include "advbuilding.h"
69 #include "advgoto.h"
70 #include "infracache.h"
71 
72 /* server/scripting */
73 #include "script_server.h"
74 
75 // ai
76 #include "handicaps.h"
77 
78 #include "citytools.h"
79 
80 // Queue for pending auto_arrange_workers()
81 static struct city_list *arrange_workers_queue = nullptr;
82 
83 // Suppress sending cities during game_load() and end_phase()
84 static bool send_city_suppressed = false;
85 
86 static bool city_workers_queue_remove(struct city *pcity);
87 
88 static void announce_trade_route_removal(struct city *pc1, struct city *pc2,
89  bool source_gone);
90 
118 void city_freeze_workers(struct city *pcity)
119 {
120  pcity->server.workers_frozen++;
121 }
122 
128 void city_thaw_workers(struct city *pcity)
129 {
130  pcity->server.workers_frozen--;
131  fc_assert(pcity->server.workers_frozen >= 0);
132  if (pcity->server.workers_frozen == 0 && pcity->server.needs_arrange) {
133  city_refresh(pcity); // Citizen count sanity
134  auto_arrange_workers(pcity);
135  }
136 }
137 
141 void city_freeze_workers_queue(struct city *pcity)
142 {
143  if (nullptr == arrange_workers_queue) {
144  arrange_workers_queue = city_list_new();
145  } else if (city_list_find_number(arrange_workers_queue, pcity->id)) {
146  return;
147  }
148 
149  city_list_prepend(arrange_workers_queue, pcity);
150  city_freeze_workers(pcity);
151  pcity->server.needs_arrange = true;
152 }
153 
158 static bool city_workers_queue_remove(struct city *pcity)
159 {
160  if (arrange_workers_queue == nullptr) {
161  return false;
162  }
163 
164  return city_list_remove(arrange_workers_queue, pcity);
165 }
166 
172 {
173  if (nullptr == arrange_workers_queue) {
174  return;
175  }
176 
178  {
179  city_thaw_workers(pcity);
180  }
182 
183  city_list_destroy(arrange_workers_queue);
184  arrange_workers_queue = nullptr;
185 }
186 
195 static int evaluate_city_name_priority(struct tile *ptile,
196  const struct nation_city *pncity,
197  int default_priority)
198 {
199  // Lower values mean higher priority.
200  float priority = static_cast<float>(default_priority);
201  enum nation_city_preference goodness;
202 
203  /* Increasing this value will increase the difference caused by
204  (non-)matching terrain. A matching terrain is mult_factor
205  "better" than an unlisted terrain, which is mult_factor
206  "better" than a non-matching terrain. */
207  const float mult_factor = 1.4;
208  bool river = false;
209 
210  /*
211  * If natural city names aren't being used, we just return the
212  * base value. This will have the effect of the first-listed
213  * city being used. We do this here rather than special-casing
214  * it elewhere because this localizes everything to this
215  * function, even though it's a bit inefficient.
216  */
217  if (!game.server.natural_city_names) {
218  return default_priority;
219  }
220 
221  /*
222  * Assuming we're using the natural city naming system, we use
223  * an internal alorithm to calculate the priority of each name.
224  * It's a pretty fuzzy algorithm; we basically do the following:
225  * - Change the priority scale from 0..n to 10..n+10. This means
226  * each successive city is 10% lower priority than the first.
227  * - Multiply by a semi-random number. This has the effect of
228  * decreasing rounding errors in the successive calculations,
229  * as well as introducing a smallish random effect.
230  * - Check over all the applicable terrains, and
231  * multiply or divide the priority based on whether or not
232  * the terrain matches. See comment below.
233  */
234 
235  priority += 10.0;
236  priority *= 10.0 + fc_rand(5);
237 
238  /*
239  * The terrain priority in the struct nation_city will be either
240  * -1, 0, or 1. We therefore take this as-is if the terrain is
241  * present, or negate it if not.
242  *
243  * The reason we multiply as well as divide the value is so
244  * that cities that don't care what terrain they are on (which
245  * is the default) will be left in the middle of the pack. If
246  * we _only_ multiplied (or divided), then cities that had more
247  * terrain labels would have their priorities hurt (or helped).
248  */
249  goodness = nation_city_river_preference(pncity);
250  extra_type_by_cause_iterate(EC_ROAD, priver)
251  {
252  if (tile_has_extra(ptile, priver)
253  && road_has_flag(extra_road_get(priver), RF_RIVER)) {
254  river = true;
255  break;
256  }
257  }
259  if (!river) {
260  goodness = nation_city_preference_revert(goodness);
261  }
262 
263  switch (goodness) {
264  case NCP_DISLIKE:
265  priority *= mult_factor;
266  break;
267  case NCP_NONE:
268  break;
269  case NCP_LIKE:
270  priority /= mult_factor;
271  break;
272  }
273 
274  terrain_type_iterate(pterrain)
275  {
276  // Now we do the same for every available terrain.
277  goodness = nation_city_terrain_preference(pncity, pterrain);
278  if (!is_terrain_near_tile(ptile, pterrain, true)) {
279  goodness = nation_city_preference_revert(goodness);
280  }
281  switch (goodness) {
282  case NCP_DISLIKE:
283  priority *= mult_factor;
284  break;
285  case NCP_NONE:
286  break;
287  case NCP_LIKE:
288  priority /= mult_factor;
289  }
290  }
292 
293  return static_cast<int>(priority);
294 }
295 
300 static bool is_default_city_name(const char *name, struct player *pplayer)
301 {
303  {
304  if (0 == fc_strcasecmp(name, nation_city_name(pncity))) {
305  return true;
306  }
307  }
309  return false;
310 }
311 
319 static const char *
321  const struct nation_city_list *default_cities,
322  struct player *pplayer)
323 {
324  int choice = 0, priority, best_priority = -1;
325  const char *name, *best_name = nullptr;
326 
327  nation_city_list_iterate(default_cities, pncity)
328  {
329  name = nation_city_name(pncity);
330  if (nullptr == game_city_by_name(name)
331  && is_allowed_city_name(pplayer, name, nullptr, 0)) {
332  priority = evaluate_city_name_priority(ptile, pncity, choice++);
333  if (-1 == best_priority || priority < best_priority) {
334  best_priority = priority;
335  best_name = name;
336  }
337  }
338  }
340 
341  return best_name;
342 }
343 
356 bool is_allowed_city_name(struct player *pplayer, const char *cityname,
357  char *error_buf, size_t bufsz)
358 {
359  struct connection *pconn = conn_by_user(pplayer->username);
360 
361  // Disallow leading, trailing, and double spaces
362  if (QString(cityname).simplified() != cityname) {
363  if (error_buf) {
364  fc_snprintf(error_buf, bufsz,
365  _("City names may not contain leading, trailing, or "
366  "consecutive spaces."));
367  }
368  return false;
369  }
370 
371  // Mode 1: A city name has to be unique for each player.
372  if (CNM_PLAYER_UNIQUE == game.server.allowed_city_names
373  && city_list_find_name(pplayer->cities, cityname)) {
374  if (error_buf) {
375  fc_snprintf(error_buf, bufsz, _("You already have a city called %s."),
376  cityname);
377  }
378  return false;
379  }
380 
381  // Modes 2,3: A city name has to be globally unique.
382  if ((CNM_GLOBAL_UNIQUE == game.server.allowed_city_names
383  || CNM_NO_STEALING == game.server.allowed_city_names)
384  && game_city_by_name(cityname)) {
385  if (error_buf) {
386  fc_snprintf(error_buf, bufsz, _("A city called %s already exists."),
387  cityname);
388  }
389  return false;
390  }
391 
392  // General rule: any name in our ruleset is allowed.
393  if (is_default_city_name(cityname, pplayer)) {
394  return true;
395  }
396 
397  /*
398  * Mode 3: Check that the proposed city name is not in another
399  * player's default city names. Note the name will already have been
400  * allowed if it is in this player's default city names list.
401  */
402  if (CNM_NO_STEALING == game.server.allowed_city_names) {
403  struct player *pother = nullptr;
404 
405  players_iterate(player2)
406  {
407  if (player2 != pplayer && is_default_city_name(cityname, player2)) {
408  pother = player2;
409  break;
410  }
411  }
413 
414  if (pother != nullptr) {
415  if (error_buf) {
416  fc_snprintf(error_buf, bufsz,
417  _("Can't use %s as a city name. It is reserved for %s."),
418  cityname, nation_plural_for_player(pother));
419  }
420  return false;
421  }
422  }
423 
424  /* To prevent abuse, only players with HACK access (usually local
425  * connections) can use non-ascii names. Otherwise players could use
426  * confusing garbage names in multi-player games.
427  *
428  * We can even reach here for an AI player, if all the cities of the
429  * original nation are exhausted and the backup nations have non-ascii
430  * names in them. */
431  if (!is_ascii_name(cityname)
432  && (!pconn || pconn->access_level != ALLOW_HACK)) {
433  if (error_buf) {
434  fc_snprintf(error_buf, bufsz,
435  _("%s is not a valid name. Only ASCII or "
436  "ruleset names are allowed for cities."),
437  cityname);
438  }
439  return false;
440  }
441 
442  return true;
443 }
444 
451 const char *city_name_suggestion(struct player *pplayer, struct tile *ptile)
452 {
453  struct nation_type *pnation = nation_of_player(pplayer);
454  const char *name;
455 
456  qDebug("Suggesting city name for %s at (%d,%d)", player_name(pplayer),
457  TILE_XY(ptile));
458 
459  // First try default city names.
460  name = search_for_city_name(ptile, nation_cities(pnation), pplayer);
461  if (nullptr != name) {
462  log_debug("Default city name found: %s.", name);
463  return name;
464  }
465 
466  /* Not found... Let's try a straightforward algorithm to look through
467  * nations to find a city name.
468  *
469  * We start by adding the player's nation to the queue. Then we proceed:
470  * - Pick a random nation from the queue.
471  * - If it has a valid city name, use that.
472  * - Otherwise, add all parent and child nations to the queue.
473  * - If the queue is empty, add all remaining nations to it and continue.
474  *
475  * Variables used:
476  * - nation_list is a queue of nations to look through.
477  * - nations_selected tells whether each nation is in the queue already
478  * - queue_size gives the size of the queue (number of nations in it).
479  * - i is the current position in the queue.
480  * Note that nations aren't removed from the queue after they're processed.
481  * New nations are just added onto the end. */
482  {
483  struct nation_type *nation_list[game.control.nation_count];
484  bool nations_selected[game.control.nation_count];
485  int queue_size = 1, i = 0, idx;
486 
487  memset(nations_selected, 0, sizeof(nations_selected));
488  nation_list[0] = pnation;
489  nations_selected[nation_index(pnation)] = true;
490 
491  while (i < game.control.nation_count) {
492  for (; i < queue_size; i++) {
493  if (0 < i) {
494  // Pick a random nation from the queue.
495  const int which = i + fc_rand(queue_size - i);
496  struct nation_type *tmp = nation_list[i];
497 
498  nation_list[i] = nation_list[which];
499  nation_list[which] = tmp;
500 
501  pnation = nation_list[i];
502  log_debug("Looking through %s.", nation_rule_name(pnation));
503  name =
504  search_for_city_name(ptile, nation_cities(pnation), pplayer);
505 
506  if (nullptr != name) {
507  return name;
508  }
509  }
510 
511  // Append the nation's civil war nations into the search tree.
512  nation_list_iterate(pnation->server.civilwar_nations, n)
513  {
514  idx = nation_index(n);
515  if (!nations_selected[idx]) {
516  nation_list[queue_size] = n;
517  nations_selected[idx] = true;
518  queue_size++;
519  log_debug("Child %s.", nation_rule_name(n));
520  }
521  }
523 
524  // Append the nation's parent nations into the search tree.
525  nation_list_iterate(pnation->server.parent_nations, n)
526  {
527  idx = nation_index(n);
528  if (!nations_selected[idx]) {
529  nation_list[queue_size] = n;
530  nations_selected[idx] = true;
531  queue_size++;
532  log_debug("Parent %s.", nation_rule_name(n));
533  }
534  }
536  }
537 
538  // Still not found; append all remaining nations.
539  for (auto &n : nations) {
540  if (nation_is_in_current_set(&n)) {
541  idx = nation_index(&n);
542  if (!nations_selected[idx]) {
543  nation_list[queue_size] = &n;
544  nations_selected[nation_index(&n)] = true;
545  queue_size++;
546  log_debug("Misc nation %s.", nation_rule_name(&n));
547  }
548  }
549  } // iterate over nations - n
550  }
551  }
552 
553  // Not found in rulesets, make a default name.
554  {
555  static char tempname[MAX_LEN_CITYNAME];
556  int i;
557 
558  log_debug("City name not found in rulesets.");
559  for (i = 1; i <= map_num_tiles(); i++) {
560  fc_snprintf(tempname, MAX_LEN_CITYNAME, _("City no. %d"), i);
561  if (nullptr == game_city_by_name(tempname)) {
562  return tempname;
563  }
564  }
565 
566  fc_assert_msg(false, "Failed to generate a city name.");
567  sz_strlcpy(tempname, _("A poorly-named city"));
568  return tempname;
569  }
570 }
571 
575 int build_points_left(struct city *pcity)
576 {
577  int cost = city_production_build_shield_cost(pcity);
578 
579  return cost - pcity->shield_stock;
580 }
581 
589 static void transfer_unit(struct unit *punit, struct city *tocity,
590  bool rehome, bool verbose)
591 {
592  struct player *from_player = unit_owner(punit);
593  struct player *to_player = city_owner(tocity);
594 
595  /* Transferring a dying GameLoss unit as part of the loot for
596  * killing it caused gna bug #23676. */
597  fc_assert_ret_msg(!punit->server.dying,
598  "Tried to transfer the dying unit %d.", punit->id);
599 
600  if (from_player == to_player) {
601  fc_assert_ret(rehome);
602  qDebug("Changed homecity of %s %s to %s",
603  nation_rule_name(nation_of_player(from_player)),
604  unit_rule_name(punit), city_name_get(tocity));
605  if (verbose) {
606  notify_player(from_player, unit_tile(punit), E_UNIT_RELOCATED,
607  ftc_server, _("Changed homecity of %s to %s."),
608  unit_link(punit), city_link(tocity));
609  }
610  } else {
611  struct tile *utile = unit_tile(punit);
612  struct city *in_city = tile_city(utile);
613 
615  unit_type_get(punit))) {
616  /* This is a unique unit that to_player already has. A transfer would
617  * break the rule that a player only may have one unit of each unique
618  * unit type. */
619 
620  log_debug("%s already have a %s. Can't transfer from %s",
622  unit_rule_name(punit),
623  nation_rule_name(nation_of_player(from_player)));
624 
625  if (utype_has_flag(unit_type_get(punit), UTYF_GAMELOSS)) {
626  // Try to save game loss units.
627  bounce_unit(punit, verbose);
628  } else {
629  // Kill the unique unit.
630 
631  if (verbose) {
633  from_player, unit_tile(punit), E_UNIT_LOST_MISC, ftc_server,
634  // TRANS: Americans ... Leader
635  _("The %s already have a %s. Can't transfer yours."),
636  nation_plural_for_player(to_player), unit_tile_link(punit));
637  }
638 
639  wipe_unit(punit, ULR_CITY_LOST, nullptr);
640  }
641 
642  return;
643  }
644 
645  if (in_city) {
646  qDebug("Transferred %s in %s from %s to %s", unit_rule_name(punit),
647  city_name_get(in_city),
648  nation_rule_name(nation_of_player(from_player)),
649  nation_rule_name(nation_of_player(to_player)));
650  if (verbose) {
651  notify_player(from_player, unit_tile(punit), E_UNIT_RELOCATED,
652  ftc_server, _("Transferred %s in %s from %s to %s."),
653  unit_link(punit), city_link(in_city),
654  nation_plural_for_player(from_player),
655  nation_plural_for_player(to_player));
656  }
657  } else if (can_unit_exist_at_tile(&(wld.map), punit, tocity->tile)) {
658  qDebug("Transferred %s from %s to %s", unit_rule_name(punit),
659  nation_rule_name(nation_of_player(from_player)),
660  nation_rule_name(nation_of_player(to_player)));
661  if (verbose) {
662  notify_player(from_player, unit_tile(punit), E_UNIT_RELOCATED,
663  ftc_server, _("Transferred %s from %s to %s."),
664  unit_link(punit),
665  nation_plural_for_player(from_player),
666  nation_plural_for_player(to_player));
667  }
668  } else {
669  qDebug("Could not transfer %s from %s to %s", unit_rule_name(punit),
670  nation_rule_name(nation_of_player(from_player)),
671  nation_rule_name(nation_of_player(to_player)));
672  if (verbose) {
674  from_player, unit_tile(punit), E_UNIT_LOST_MISC, ftc_server,
675  // TRANS: Polish Destroyer ... German <city>
676  _("%s %s lost in transfer to %s %s"),
677  nation_adjective_for_player(from_player), unit_tile_link(punit),
678  nation_adjective_for_player(to_player), city_link(tocity));
679  }
680  wipe_unit(punit, ULR_CITY_LOST, nullptr);
681  return;
682  }
683 
684  maybe_make_contact(utile, to_player);
685  }
686  unit_change_homecity_handling(punit, tocity, rehome);
687 }
688 
708 void transfer_city_units(struct player *pplayer, struct player *pvictim,
709  struct unit_list *units, struct city *pcity,
710  struct city *exclude_city, int kill_outside,
711  bool verbose)
712 {
713  struct tile *ptile = pcity->tile;
714  int saved_id = pcity->id;
715  const char *name = city_name_get(pcity);
716 
717  /* Transfer enemy units in the city to the new owner.
718  * Only relevant if we are transferring to another player. */
719  if (pplayer != pvictim) {
720  unit_list_iterate_safe((ptile)->units, vunit)
721  {
722  if (vunit->server.dying) {
723  /* Don't transfer or bounce a dying unit. It will soon be gone
724  * anyway.
725  *
726  * Bouncing a dying unit isn't a good idea.
727  * Remaining death handling may do things meant for its current
728  * location to the new location. (Example: Stack death)
729  * bounce_unit() will wipe the unit if it can't be bounced. Wiping
730  * the dying unit isn't a good idea. The remaining death handling
731  * code will try to read from it.
732  *
733  * Transferring a dying GameLoss unit as part of the loot for
734  * killing it caused gna bug #23676. */
735  continue;
736  }
737 
738  // Don't transfer units already owned by new city-owner --wegge
739  if (unit_owner(vunit) == pvictim) {
740  /* Determine whether unit was homeless. If it was, we don't give
741  * it a homecity, only change ownership.
742  * We have to search the transferred city's former units because
743  * the unit may have been made only temporarily homeless during
744  * city transfer. */
745  bool homeless =
746  (vunit->homecity == 0) && !unit_list_search(units, vunit);
747 
748  /* vunit may die during transfer_unit().
749  * unit_list_remove() is still safe using vunit pointer, as
750  * pointer is not used for dereferencing, only as value.
751  * Not sure if it would be safe to unlink first and transfer only
752  * after that. Not sure if it is correct to unlink at all in
753  * some cases, depending which list 'units' points to. */
754  transfer_unit(vunit, pcity, !homeless, verbose);
755  unit_list_remove(units, vunit);
756  } else if (!pplayers_allied(pplayer, unit_owner(vunit))) {
757  // the owner of vunit is allied to pvictim but not to pplayer
758  bounce_unit(vunit, verbose);
759  }
760  }
762  }
763 
764  if (!city_exist(saved_id)) {
765  saved_id = 0;
766  }
767 
768  /* Any remaining units supported by the city are either given new home
769  cities or maybe destroyed */
771  {
772  struct city *new_home_city = tile_city(unit_tile(vunit));
773 
774  if (vunit->server.dying) {
775  /* Don't transfer or destroy a dying unit. It will soon be gone
776  * anyway.
777  *
778  * Transfering a dying GameLoss unit as part of the loot for
779  * killing it caused gna bug #23676. */
780  continue;
781  }
782 
783  if (new_home_city && new_home_city != exclude_city
784  && city_owner(new_home_city) == unit_owner(vunit)) {
785  /* unit is in another city: make that the new homecity,
786  * unless that city is actually the same city (happens if disbanding)
787  * Unit had a homecity since it was supported, so rehome. */
788  transfer_unit(vunit, new_home_city, true, verbose);
789  } else if ((kill_outside == -1
790  || real_map_distance(unit_tile(vunit), ptile)
791  <= kill_outside)
792  && saved_id) {
793  // else transfer to specified city.
794  transfer_unit(vunit, pcity, true, verbose);
795  if (unit_tile(vunit) == ptile && !pplayers_allied(pplayer, pvictim)) {
796  // Unit is inside city being transferred, bounce it
797  bounce_unit(vunit, true);
798  }
799  } else {
800  /* The unit is lost. Call notify_player (in all other cases it is
801  * called automatically). */
802  qDebug("Lost %s %s at (%d,%d) when %s was lost.",
804  TILE_XY(unit_tile(vunit)), name);
805  if (verbose) {
806  notify_player(unit_owner(vunit), unit_tile(vunit), E_UNIT_LOST_MISC,
807  ftc_server, _("%s lost along with control of %s."),
808  unit_tile_link(vunit), name);
809  }
810  wipe_unit(vunit, ULR_CITY_LOST, nullptr);
811  }
812  }
814 
815 #ifdef FREECIV_DEBUG
816  unit_list_iterate(pcity->units_supported, punit)
817  {
818  if (punit->server.dying) {
819  // Leave the dying alone.
820  continue;
821  }
822 
823  fc_assert(punit->homecity == pcity->id);
824  fc_assert(unit_owner(punit) == pplayer);
825  }
827 #endif // FREECIV_DEBUG
828 }
829 
849 struct city *find_closest_city(const struct tile *ptile,
850  const struct city *pexclcity,
851  const struct player *pplayer, bool only_ocean,
852  bool only_continent, bool only_known,
853  bool only_player, bool only_enemy,
854  const struct unit_class *pclass)
855 {
856  Continent_id con;
857  struct city *best_city = nullptr;
858  int best_dist = -1;
859 
860  fc_assert_ret_val(ptile != nullptr, nullptr);
861  if (only_known || only_player || only_enemy) {
862  fc_assert_ret_val(pplayer != nullptr, nullptr);
863  }
864 
865  if (pplayer != nullptr && only_player && only_enemy) {
866  qCritical("Non of my own cities will be at war with me!");
867  return nullptr;
868  }
869 
870  con = tile_continent(ptile);
871 
872  players_iterate(aplayer)
873  {
874  if (pplayer != nullptr && only_player && pplayer != aplayer) {
875  // only cities of player 'pplayer'
876  continue;
877  }
878 
879  if (pplayer != nullptr && only_enemy
880  && !pplayers_at_war(pplayer, aplayer)) {
881  // only cities of players at war with player 'pplayer'
882  continue;
883  }
884 
885  city_list_iterate(aplayer->cities, pcity)
886  {
887  int city_dist;
888 
889  if (pexclcity && pexclcity == pcity) {
890  // not this city
891  continue;
892  }
893 
894  city_dist = real_map_distance(ptile, city_tile(pcity));
895 
896  /* Find the closest city matching the requirements.
897  * - closer than the current best city
898  * - (if required) on the same continent
899  * - (if required) adjacent to ocean
900  * - (if required) only cities known by the player
901  * - (if required) only cities native to the class */
902  if ((best_dist == -1 || city_dist < best_dist)
903  && (!only_continent || con == tile_continent(pcity->tile))
904  && (!only_ocean
905  || is_terrain_class_near_tile(city_tile(pcity), TC_OCEAN))
906  && (!only_known
907  || (map_is_known(city_tile(pcity), pplayer)
908  && map_get_player_site(city_tile(pcity), pplayer)->identity
910  && (pclass == nullptr
911  || is_native_near_tile(&(wld.map), pclass,
912  city_tile(pcity)))) {
913  best_dist = city_dist;
914  best_city = pcity;
915  }
916  }
918  }
920 
921  return best_city;
922 }
923 
929 static void raze_city(struct city *pcity)
930 {
931  int razechance = game.server.razechance;
932  bool city_remains = true;
933 
934  // land barbarians are more likely to destroy city improvements
935  if (is_land_barbarian(city_owner(pcity))) {
936  razechance += 30;
937  }
938 
939  city_built_iterate(pcity, pimprove)
940  {
941  /* Small wonders should have already been removed by
942  * transfer_city() (with 100% probability). */
943  fc_assert(!is_small_wonder(pimprove));
944  if (is_improvement(pimprove) && (fc_rand(100) < razechance)) {
945  /* FIXME: Should maybe have conquering unit instead of nullptr as
946  * destoryer */
947  city_remains = building_removed(pcity, pimprove, "razed", nullptr);
948  if (!city_remains) {
949  break;
950  }
951  }
952  }
954 
955  if (city_remains) {
957  pcity->shield_stock = 0;
958  }
959 }
960 
965 static void reestablish_city_trade_routes(struct city *pcity)
966 {
967  trade_routes_iterate_safe(pcity, proute)
968  {
969  bool keep_route;
970  struct trade_route *back;
971  struct city *partner = game_city_by_number(proute->partner);
972 
973  /* Remove the city's trade routes (old owner).
974  * Do not announce removal as we might restore the route immediately
975  * below */
976  back = remove_trade_route(pcity, proute, false, false);
977 
978  keep_route = can_cities_trade(pcity, partner)
979  && can_establish_trade_route(pcity, partner);
980 
981  if (!keep_route) {
982  enum trade_route_type type = cities_trade_route_type(pcity, partner);
985 
986  if (settings->cancelling != TRI_CANCEL) {
987  keep_route = true;
988  }
989  }
990 
991  // Readd the city's trade route (new owner)
992  if (keep_route) {
993  trade_route_list_append(pcity->routes, proute);
994  trade_route_list_append(partner->routes, back);
995  } else {
996  delete proute;
997  delete back;
998  proute = nullptr;
999  back = nullptr;
1000 
1001  // Now announce the traderoute removal
1002  announce_trade_route_removal(pcity, partner, false);
1003  }
1004 
1005  /* refresh regardless; either it lost a trade route or the trade
1006  * route revenue changed. */
1007  city_refresh(partner);
1008  send_city_info(city_owner(partner), partner);
1009 
1010  /* Give the new owner infos about the city which has a trade route
1011  * with the transferred city. */
1012  reality_check_city(city_owner(pcity), partner->tile);
1013  update_dumb_city(city_owner(pcity), partner);
1014  send_city_info(city_owner(pcity), partner);
1015  }
1017 }
1018 
1024 static void build_free_small_wonders(struct player *pplayer,
1025  bv_imprs *had_small_wonders)
1026 {
1027  int size = city_list_size(pplayer->cities);
1028 
1029  if (!game.server.savepalace) {
1030  // Nothing to do
1031  return;
1032  }
1033 
1034  if (size == 0) {
1035  /* The last city was removed or transferred to the enemy.
1036  * If the victim survives to found or acquire another city, they'll
1037  * get any savepalace initial buildings then. */
1038  return;
1039  }
1040 
1041  improvement_iterate(pimprove)
1042  {
1043  if (improvement_has_flag(pimprove, IF_SAVE_SMALL_WONDER)
1044  && BV_ISSET(*had_small_wonders, improvement_index(pimprove))) {
1045  // FIXME: instead, find central city
1046  struct city *pnew_city = city_list_get(pplayer->cities, fc_rand(size));
1047 
1048  fc_assert_action(nullptr == city_from_small_wonder(pplayer, pimprove),
1049  continue);
1050 
1051  city_add_improvement(pnew_city, pimprove);
1052 
1053  /*
1054  * send_player_cities will recalculate all cities and send them to
1055  * the client.
1056  */
1057  send_player_cities(pplayer);
1058 
1059  notify_player(pplayer, city_tile(pnew_city), E_IMP_BUILD, ftc_server,
1060  // FIXME: should already be notified about city loss?
1061  // TRANS: <building> ... <city>
1062  _("A replacement %s was built in %s."),
1063  improvement_name_translation(pimprove),
1064  city_link(pnew_city));
1065  /*
1066  * The enemy want to see the new capital in his intelligence
1067  * report.
1068  */
1069  send_city_info(nullptr, pnew_city);
1070  }
1071  }
1073 }
1074 
1084 bool transfer_city(struct player *ptaker, struct city *pcity,
1085  int kill_outside, bool transfer_unit_verbose,
1086  bool resolve_stack, bool raze, bool build_free)
1087 {
1088  char old_city_name[MAX_LEN_CITYNAME];
1089  bv_imprs had_small_wonders;
1090  struct vision *old_vision, *new_vision;
1091  struct unit_list *old_city_units;
1092  struct player *pgiver = city_owner(pcity);
1093  struct tile *pcenter = city_tile(pcity);
1094  int saved_id = pcity->id;
1095  bool city_remains = true;
1096  bool had_great_wonders = false;
1097  const citizens old_taker_content_citizens =
1098  player_content_citizens(ptaker);
1099  const citizens old_giver_content_citizens =
1100  player_content_citizens(pgiver);
1101  const citizens old_taker_angry_citizens = player_angry_citizens(ptaker);
1102  const citizens old_giver_angry_citizens = player_angry_citizens(pgiver);
1103  bool taker_had_no_cities = (city_list_size(ptaker->cities) == 0);
1104  bool new_extras;
1105  const int units_num = unit_list_size(pcenter->units);
1106  int i;
1107 
1108  fc_assert_ret_val(pgiver != ptaker, true);
1109 
1110  bv_player *could_see_unit =
1111  (units_num > 0 ? new bv_player[units_num] : nullptr);
1112  old_city_units = unit_list_new();
1113 
1114  // Remember what player see what unit.
1115  i = 0;
1116  unit_list_iterate(pcenter->units, aunit)
1117  {
1118  BV_CLR_ALL(could_see_unit[i]);
1119  players_iterate(aplayer)
1120  {
1121  if (can_player_see_unit(aplayer, aunit)) {
1122  BV_SET(could_see_unit[i], player_index(aplayer));
1123  }
1124  }
1126  i++;
1127  }
1129  fc_assert(i == units_num);
1130 
1131  // Remove AI control of the old owner.
1132  CALL_PLR_AI_FUNC(city_lost, pcity->owner, pcity->owner, pcity);
1133 
1134  // Forget old tasks
1135  clear_worker_tasks(pcity);
1136 
1137  // Activate AI control of the new owner.
1138  CALL_PLR_AI_FUNC(city_got, ptaker, ptaker, pcity);
1139 
1140  city_freeze_workers(pcity);
1141 
1142  unit_list_iterate(pcity->units_supported, punit)
1143  {
1144  unit_list_prepend(old_city_units, punit);
1145  /* Mark unit to have no homecity at all.
1146  * 1. We remove unit from units_supported list here,
1147  * real_change_unit_homecity() should not attempt it.
1148  * 2. Otherwise we might delete the homecity from under the unit
1149  * in the client */
1150  punit->homecity = 0;
1151  send_unit_info(nullptr, punit);
1152  }
1154  unit_list_clear(pcity->units_supported);
1155 
1156  /* Remove all global improvement effects that this city confers (but
1157  then restore the local improvement list - we need this to restore the
1158  global effects for the new city owner) */
1159  BV_CLR_ALL(had_small_wonders);
1160  city_built_iterate(pcity, pimprove)
1161  {
1162  if (is_small_wonder(pimprove)) {
1163  // Small wonders are really removed (not restored later).
1164  building_removed(pcity, pimprove, "conquered", nullptr);
1165  BV_SET(had_small_wonders, improvement_index(pimprove));
1166  } else {
1167  city_remove_improvement(pcity, pimprove);
1168  if (is_great_wonder(pimprove)) {
1169  had_great_wonders = true;
1170  }
1171  // note: internal turn here, next city_built_iterate().
1172  pcity->built[improvement_index(pimprove)].turn =
1173  game.info.turn; /*I_ACTIVE*/
1174  }
1175  }
1177 
1178  give_citymap_from_player_to_player(pcity, pgiver, ptaker);
1179  old_vision = pcity->server.vision;
1180  new_vision = vision_new(ptaker, pcenter);
1181  pcity->server.vision = new_vision;
1182  vision_reveal_tiles(new_vision, game.server.vision_reveal_tiles);
1183  vision_change_sight(new_vision, old_vision->radius_sq);
1184 
1185  ASSERT_VISION(new_vision);
1186 
1187  sz_strlcpy(old_city_name, city_name_get(pcity));
1188  if (CNM_PLAYER_UNIQUE == game.server.allowed_city_names
1189  && city_list_find_name(ptaker->cities, city_name_get(pcity))) {
1190  sz_strlcpy(pcity->name, city_name_suggestion(ptaker, pcenter));
1191  notify_player(ptaker, pcenter, E_BAD_COMMAND, ftc_server,
1192  _("You already had a city called %s."
1193  " The city was renamed to %s."),
1194  old_city_name, city_link(pcity));
1195  }
1196 
1197  // Has to follow the unfog call above.
1198  city_list_remove(pgiver->cities, pcity);
1199  map_clear_border(pcenter);
1200  // city_thaw_workers_queue() later
1201 
1202  pcity->owner = ptaker;
1203  pcity->capital = CAPITAL_NOT;
1204  map_claim_ownership(pcenter, ptaker, pcenter, true);
1205  city_list_prepend(ptaker->cities, pcity);
1206 
1207  /* Hide/reveal units. Do it after vision have been given to taker, city
1208  * owner has been changed, and before any script could be spawned. */
1209  i = 0;
1210  unit_list_iterate(pcenter->units, aunit)
1211  {
1212  players_iterate(aplayer)
1213  {
1214  if (can_player_see_unit(aplayer, aunit)) {
1215  if (!BV_ISSET(could_see_unit[i], player_index(aplayer))
1216  && !aunit->server.dying) {
1217  // Reveal 'aunit'.
1218  send_unit_info(aplayer->connections, aunit);
1219  }
1220  } else {
1221  if (BV_ISSET(could_see_unit[i], player_index(aplayer))) {
1222  // Hide 'aunit'.
1223  unit_goes_out_of_sight(aplayer, aunit);
1224  }
1225  }
1226  }
1228  i++;
1229  }
1231  fc_assert(i == units_num);
1232  delete[] could_see_unit;
1233  could_see_unit = nullptr;
1234 
1235  transfer_city_units(ptaker, pgiver, old_city_units, pcity, nullptr,
1236  kill_outside, transfer_unit_verbose);
1237  // The units themselves are allready freed by transfer_city_units.
1238  unit_list_destroy(old_city_units);
1239 
1240  if (resolve_stack) {
1241  resolve_unit_stacks(pgiver, ptaker, transfer_unit_verbose);
1242  }
1243 
1244  if (!city_exist(saved_id)) {
1245  city_remains = false;
1246  }
1247 
1248  if (city_remains) {
1249  // Update the city's trade routes.
1251 
1252  // Clear CMA.
1253  if (pcity->cm_parameter) {
1254  delete pcity->cm_parameter;
1255  pcity->cm_parameter = nullptr;
1256  }
1257 
1258  city_refresh(pcity);
1259  }
1260 
1261  /*
1262  * maybe_make_contact() MUST be called before city_map_update_all(),
1263  * since the diplomacy status can influence whether a tile is available.
1264  */
1265  maybe_make_contact(pcenter, ptaker);
1266 
1267  if (city_remains) {
1268  struct extra_type *upgradet;
1269 
1270  if (raze) {
1271  raze_city(pcity);
1272  }
1273 
1274  if (taker_had_no_cities) {
1275  /* If conqueror previously had no cities, we might need to give
1276  * them a palace etc */
1277  if (build_free) {
1279  } // else caller should probably ensure palace is built
1280  ptaker->server.got_first_city = true;
1281  }
1282 
1284 
1285  // Restore any global improvement effects that this city confers
1286  city_built_iterate(pcity, pimprove)
1287  {
1288  city_add_improvement(pcity, pimprove);
1289  }
1291 
1292  /* Set production to something valid for pplayer, if not.
1293  * (previously allowed building obsolete units.) */
1294  if (!can_city_build_now(pcity, &pcity->production)) {
1295  advisor_choose_build(ptaker, pcity);
1296  }
1297 
1298  // Update action timestamp inherited by any units being produced
1299  city_did_prod_change(pcity);
1300 
1301  // What wasn't obsolete for the old owner may be so now.
1302  remove_obsolete_buildings_city(pcity, true);
1303 
1304  new_extras = upgrade_city_extras(pcity, &upgradet);
1305 
1306  if (new_extras) {
1307  const char *clink = city_link(pcity);
1308 
1309  notify_player(ptaker, pcenter, E_CITY_TRANSFER, ftc_server,
1310  _("The people in %s are stunned by your "
1311  "technological insight!"),
1312  clink);
1313 
1314  if (upgradet != nullptr) {
1315  notify_player(ptaker, pcenter, E_CITY_TRANSFER, ftc_server,
1316  _("Workers spontaneously gather and upgrade "
1317  "%s with %s."),
1318  clink, extra_name_translation(upgradet));
1319  } else {
1320  notify_player(ptaker, pcenter, E_CITY_TRANSFER, ftc_server,
1321  _("Workers spontaneously gather and upgrade "
1322  "%s infrastructure."),
1323  clink);
1324  }
1325  update_tile_knowledge(pcenter);
1326  }
1327 
1328  /* Build a new palace for free if the player lost her capital and
1329  savepalace is on. */
1330  build_free_small_wonders(pgiver, &had_small_wonders);
1331 
1332  /* Refresh the city's vision range, since it might be different
1333  * under the new owner. */
1334  city_refresh_vision(pcity);
1335 
1336  /* Update the national borders, within the current vision and culture.
1337  * This could leave a border ring around the city, updated later by
1338  * map_calculate_borders() at the next turn.
1339  */
1340  map_claim_border(pcenter, ptaker, -1);
1341  // city_thaw_workers_queue() later
1342 
1343  auto_arrange_workers(pcity); // does city_map_update_all()
1344  city_thaw_workers(pcity);
1345  city_thaw_workers_queue(); // after old city has a chance to work!
1346  city_refresh_queue_add(pcity);
1347  // no sanity check here as the city is not refreshed!
1348  }
1349 
1350  if (city_remains) {
1351  /* Send city with updated owner information to giver and to everyone
1352  * having shared vision pact with him/her before (s)he may
1353  * lose vision to it. When we later send info to everybody seeing the
1354  * city, (s)he may not be included. */
1355  send_city_info(nullptr, pcity);
1356  }
1357 
1358  // Remove the sight points from the giver.
1359  vision_clear_sight(old_vision);
1360  vision_free(old_vision);
1361 
1362  // Send wonder infos.
1363  if (had_great_wonders) {
1364  send_game_info(nullptr);
1365  if (city_remains) {
1366  send_player_info_c(ptaker, nullptr);
1367 
1368  /* Refresh all cities of the taker to account for possible changes due
1369  * to player wide effects. */
1370  city_list_iterate(ptaker->cities, acity)
1371  {
1372  city_refresh_queue_add(acity);
1373  }
1375  } else {
1376  // Refresh all cities to account for possible global effects.
1377  cities_iterate(acity) { city_refresh_queue_add(acity); }
1379  }
1380  }
1381  if (BV_ISSET_ANY(had_small_wonders) || had_great_wonders) {
1382  // No need to send to detached connections.
1383  send_player_info_c(pgiver, nullptr);
1384 
1385  /* Refresh all cities of the giver to account for possible changes due
1386  * to player wide effects. */
1387  city_list_iterate(pgiver->cities, acity)
1388  {
1389  city_refresh_queue_add(acity);
1390  }
1392  }
1393 
1394  // Refresh all cities in the queue.
1396  // After the refresh the sanity check can be done.
1397  sanity_check_city(pcity);
1398 
1399  if (city_remains) {
1400  // Send information about conquered city to all players.
1401  send_city_info(nullptr, pcity);
1402  }
1403 
1404  /* We may cross the EFT_EMPIRE_SIZE_* effects, then we will have to
1405  * refresh all cities for the player. */
1406  if (old_taker_content_citizens != player_content_citizens(ptaker)
1407  || old_taker_angry_citizens != player_angry_citizens(ptaker)) {
1408  city_refresh_for_player(ptaker);
1409  }
1410  if (old_giver_content_citizens != player_content_citizens(pgiver)
1411  || old_giver_angry_citizens != player_angry_citizens(pgiver)) {
1412  city_refresh_for_player(pgiver);
1413  }
1414 
1415  if (pgiver->primary_capital_id == saved_id) {
1416  update_capital(pgiver);
1417  }
1418 
1419  sync_cities();
1420 
1421  CALL_FUNC_EACH_AI(city_info, pcity);
1422 
1423  return city_remains;
1424 }
1425 
1433 void city_build_free_buildings(struct city *pcity)
1434 {
1435  struct player *pplayer;
1436  struct nation_type *nation;
1437  int i;
1438  bool has_small_wonders, has_great_wonders;
1439  bool first_city;
1440 
1441  fc_assert_ret(nullptr != pcity);
1442  pplayer = city_owner(pcity);
1443  fc_assert_ret(nullptr != pplayer);
1444  nation = nation_of_player(pplayer);
1445  fc_assert_ret(nullptr != nation);
1446 
1447  /* If this isn't the first city a player has ever had, they only get
1448  * any initial buildings with the SaveSmallWonder flag, and then only
1449  * if savepalace is enabled. */
1450  first_city = !pplayer->server.got_first_city;
1451 
1452  has_small_wonders = false;
1453  has_great_wonders = false;
1454 
1455  // Global free buildings.
1456  for (i = 0; i < MAX_NUM_BUILDING_LIST; i++) {
1458  struct impr_type *pimprove;
1459 
1460  if (n == B_LAST) {
1461  break;
1462  }
1463 
1464  pimprove = improvement_by_number(n);
1465  fc_assert_action(!is_great_wonder(pimprove), continue);
1466  if (first_city
1467  || (game.server.savepalace
1468  && improvement_has_flag(pimprove, IF_SAVE_SMALL_WONDER))) {
1469  city_add_improvement(pcity, pimprove);
1470  if (is_small_wonder(pimprove)) {
1471  has_small_wonders = true;
1472  }
1473  }
1474  }
1475 
1476  // Nation specific free buildings.
1477  for (i = 0; i < MAX_NUM_BUILDING_LIST; i++) {
1478  Impr_type_id n = nation->init_buildings[i];
1479  struct impr_type *pimprove;
1480 
1481  if (n == B_LAST) {
1482  break;
1483  }
1484 
1485  pimprove = improvement_by_number(n);
1486  if (first_city
1487  || (game.server.savepalace
1488  && improvement_has_flag(pimprove, IF_SAVE_SMALL_WONDER))) {
1489  city_add_improvement(pcity, pimprove);
1490  if (is_small_wonder(pimprove)) {
1491  has_small_wonders = true;
1492  } else if (is_great_wonder(pimprove)) {
1493  has_great_wonders = true;
1494  }
1495  }
1496  }
1497 
1498  // Update wonder infos.
1499  if (has_great_wonders) {
1500  send_game_info(nullptr);
1501  // No need to send to detached connections.
1502  send_player_info_c(pplayer, nullptr);
1503  } else if (has_small_wonders) {
1504  // No need to send to detached connections.
1505  send_player_info_c(pplayer, nullptr);
1506  }
1507 }
1508 
1512 void create_city(struct player *pplayer, struct tile *ptile,
1513  const char *name, struct player *nationality)
1514 {
1515  struct player *saved_owner = tile_owner(ptile);
1516  struct tile *saved_claimer = tile_claimer(ptile);
1517  struct city *pwork = tile_worked(ptile);
1518  struct city *pcity;
1519  const citizens old_content_citizens = player_content_citizens(pplayer);
1520  const citizens old_angry_citizens = player_angry_citizens(pplayer);
1521 
1522  log_debug("create_city() %s", name);
1523 
1524  pcity = create_city_virtual(pplayer, ptile, name);
1525 
1526  /* Remove units no more seen. Do it before city is really put into the
1527  * game. */
1528  players_iterate(other_player)
1529  {
1530  if (can_player_see_units_in_city(other_player, pcity)
1531  || !map_is_known_and_seen(ptile, other_player, V_MAIN)) {
1532  continue;
1533  }
1534  unit_list_iterate(ptile->units, punit)
1535  {
1536  if (can_player_see_unit(other_player, punit)) {
1537  unit_goes_out_of_sight(other_player, punit);
1538  }
1539  }
1541  }
1543 
1544  adv_city_alloc(pcity);
1545 
1546  tile_set_owner(ptile, pplayer, ptile); // temporarily
1548  pcity->id = identity_number();
1549 
1550  game.server.mutexes.city_list->lock();
1551  idex_register_city(&wld, pcity);
1552  game.server.mutexes.city_list->unlock();
1553 
1554  if (city_list_size(pplayer->cities) == 0) {
1555  /* Free initial buildings, or at least a palace if they were
1556  * previously careless enough to lose all their cities */
1558  pplayer->server.got_first_city = true;
1559  }
1560 
1561  // Set up citizens nationality.
1562  citizens_init(pcity);
1563 
1564  /* Place a worker at the city center
1565  * It is possible to build a city on a tile that is already worked;
1566  * this will displace the worker on the newly-built city's tile -- Syela */
1567  tile_set_worked(ptile, pcity); // instead of city_map_update_worker()
1568 
1569  if (nullptr != pwork) {
1570  // was previously worked by another city
1571  // Turn citizen into specialist.
1572  pwork->specialists[DEFAULT_SPECIALIST]++;
1573  /* One less citizen. Citizen sanity will be handled later in
1574  * city_thaw_workers_queue() */
1575  pwork->server.synced = false;
1577  }
1578 
1579  // Update citizens.
1580  citizens_update(pcity, nationality);
1581 
1582  /* Restore the old-owner information so removal
1583  * of territory claiming bases can work relative to it. */
1584  tile_set_owner(ptile, saved_owner, saved_claimer);
1585 
1586  // Destroy any extras that don't belong in the city.
1587  extra_type_iterate(pextra)
1588  {
1589  if (tile_has_extra(ptile, pextra)
1590  && !is_native_tile_to_extra(pextra, ptile)) {
1591  destroy_extra(ptile, pextra);
1592  }
1593  }
1595 
1596  // Build any extras that the city should have.
1597  upgrade_city_extras(pcity, nullptr);
1598 
1599  // Claim the ground we stand on
1600  map_claim_ownership(ptile, pplayer, ptile, true);
1601 
1602  // Before arranging workers to show unknown land
1603  pcity->server.vision = vision_new(pplayer, ptile);
1604  vision_reveal_tiles(pcity->server.vision, game.server.vision_reveal_tiles);
1605  city_refresh_vision(pcity);
1606  city_list_prepend(pplayer->cities, pcity);
1607 
1608  /* This is dependent on the current vision, so must be done after
1609  * vision is prepared and before arranging workers. */
1610  map_claim_border(ptile, pplayer, -1);
1611  // city_thaw_workers_queue() later
1612 
1613  /* Refresh the city. First a city refresh is done (this shouldn't
1614  * send any packets to the client because the city has no supported units)
1615  * then rearrange the workers. Note that auto_arrange_workers does its
1616  * own refresh call; it is safest to do our own controlled city_refresh
1617  * first. */
1618  city_refresh(pcity);
1619  auto_arrange_workers(pcity);
1620  city_thaw_workers_queue(); // after new city has a chance to work!
1622 
1623  /* Bases destroyed earlier may have had watchtower effect. Refresh
1624  * unit vision. */
1626 
1627  update_tile_knowledge(ptile);
1628 
1629  if (old_content_citizens != player_content_citizens(pplayer)
1630  || old_angry_citizens != player_angry_citizens(pplayer)) {
1631  /* We crossed the EFT_EMPIRE_SIZE_* effects, we have to refresh all
1632  * cities for the player. */
1633  city_refresh_for_player(pplayer);
1634  }
1635 
1636  pcity->server.synced = false;
1637  send_city_info(nullptr, pcity);
1638  sync_cities(); // Will also send pwork.
1639 
1640  notify_player(pplayer, ptile, E_CITY_BUILD, ftc_server,
1641  _("You have founded %s."), city_link(pcity));
1642  maybe_make_contact(ptile, city_owner(pcity));
1643 
1644  unit_list_iterate((ptile)->units, punit)
1645  {
1646  struct city *home = game_city_by_number(punit->homecity);
1647 
1648  // Catch fortress building, transforming into ocean, etc.
1649  if (!can_unit_continue_current_activity(punit)) {
1650  unit_activity_handling(punit, ACTIVITY_IDLE);
1651  }
1652 
1653  // Update happiness (the unit may no longer cause unrest).
1654  if (home) {
1655  if (city_refresh(home)) {
1656  // Shouldn't happen, but better be safe than sorry.
1657  auto_arrange_workers(home);
1658  }
1659  sanity_check_city(home);
1660  send_city_info(city_owner(home), home);
1661  }
1662  }
1664 
1665  sanity_check_city(pcity);
1666 
1667  script_server_signal_emit("city_built", pcity);
1668 
1669  CALL_FUNC_EACH_AI(city_created, pcity);
1670  CALL_PLR_AI_FUNC(city_got, pplayer, pplayer, pcity);
1671 }
1672 
1676 void remove_city(struct city *pcity)
1677 {
1678  struct player *powner = city_owner(pcity);
1679  struct tile *pcenter = city_tile(pcity);
1680  bv_imprs had_small_wonders;
1681  struct vision *old_vision;
1682  int id = pcity->id; // We need this even after memory has been freed
1683  bool had_great_wonders = false;
1684  const citizens old_content_citizens = player_content_citizens(powner);
1685  const citizens old_angry_citizens = player_angry_citizens(powner);
1686  QBitArray tile_processed;
1687  struct tile_list *process_queue;
1688  const char *ctl = city_tile_link(pcity);
1689 
1690  CALL_PLR_AI_FUNC(city_lost, powner, powner, pcity);
1691  CALL_FUNC_EACH_AI(city_destroyed, pcity);
1692 
1693  BV_CLR_ALL(had_small_wonders);
1694  city_built_iterate(pcity, pimprove)
1695  {
1696  building_removed(pcity, pimprove, "city_destroyed", nullptr);
1697 
1698  if (is_small_wonder(pimprove)) {
1699  BV_SET(had_small_wonders, improvement_index(pimprove));
1700  } else if (is_great_wonder(pimprove)) {
1701  had_great_wonders = true;
1702  }
1703  }
1705 
1706  // Rehome units in other cities
1708  {
1709  struct city *new_home_city = tile_city(unit_tile(punit));
1710 
1711  if (new_home_city && new_home_city != pcity
1712  && city_owner(new_home_city) == powner && !punit->server.dying) {
1713  transfer_unit(punit, new_home_city, true, true);
1714  }
1715  }
1717 
1718  // make sure ships are not left on land when city is removed.
1719  unit_list_iterate_safe(pcenter->units, punit)
1720  {
1721  bool moved;
1722  const struct unit_type *punittype = unit_type_get(punit);
1723 
1724  if (is_native_tile(punittype, pcenter)) {
1725  continue;
1726  }
1727 
1728  unit_activity_handling(punit, ACTIVITY_IDLE);
1729  moved = false;
1730  adjc_iterate(&(wld.map), pcenter, tile1)
1731  {
1732  struct unit *ptrans;
1733  if (!moved && is_native_tile(punittype, tile1)) {
1734  if (adv_could_unit_move_to_tile(punit, tile1) == 1) {
1735  // Move
1736  if (!can_unit_survive_at_tile(&(wld.map), punit, tile1)
1737  && ((ptrans = transporter_for_unit_at(punit, tile1)))
1738  && is_action_enabled_unit_on_unit(ACTION_TRANSPORT_EMBARK,
1739  punit, ptrans)) {
1740  // "Transport Embark".
1742  unit_owner(punit), punit->id, ptrans->id, 0, "",
1743  ACTION_TRANSPORT_EMBARK, ACT_REQ_RULES);
1744  } else {
1745  moved = unit_move_handling(punit, tile1, false, true);
1746  }
1747  if (moved) {
1748  notify_player(unit_owner(punit), tile1, E_UNIT_RELOCATED,
1749  ftc_server,
1750  _("Moved %s out of disbanded city %s "
1751  "since it cannot stay on %s."),
1752  unit_link(punit), ctl,
1754  break;
1755  }
1756  }
1757  }
1758  }
1760  if (!moved) {
1761  notify_player(unit_owner(punit), unit_tile(punit), E_UNIT_LOST_MISC,
1762  ftc_server,
1763  _("When %s was disbanded your %s could not "
1764  "get out, and it was therefore lost."),
1765  ctl, unit_tile_link(punit));
1766  wipe_unit(punit, ULR_CITY_LOST, nullptr);
1767  }
1768  }
1770 
1771  process_queue = tile_list_new();
1772  tile_processed.resize(map_num_tiles());
1773  for (tile_list_append(process_queue, pcenter);
1774  tile_list_size(process_queue) > 0;) {
1775  struct tile *ptile = tile_list_front(process_queue);
1776  tile_list_pop_front(process_queue);
1777  tile_processed.setBit(tile_index(ptile));
1778  adjc_iterate(&(wld.map), ptile, piter)
1779  {
1780  struct city *other_city;
1781  if (tile_processed.at(tile_index(piter))) {
1782  continue;
1783  }
1784  other_city = tile_city(piter);
1785  if (other_city != nullptr) {
1786  /* Adjacent tile has a city that may have been part of same channel
1787  */
1788  tile_processed.setBit(tile_index(piter));
1789  tile_list_append(process_queue, piter);
1790  unit_list_iterate_safe(piter->units, punit)
1791  {
1792  struct unit_class *pclass = utype_class(punit->utype);
1793 
1794  if (!uclass_has_flag(pclass, UCF_BUILD_ANYWHERE)
1795  && !is_native_tile(punit->utype, piter)
1796  && !is_city_channel_tile(pclass, piter, pcenter)) {
1797  notify_player(
1798  unit_owner(punit), unit_tile(punit), E_UNIT_LOST_MISC,
1799  ftc_server,
1800  _("When %s was disbanded your %s in %s was trapped, "
1801  "and it was therefore lost."),
1802  ctl, unit_tile_link(punit), city_link(other_city));
1803  wipe_unit(punit, ULR_CITY_LOST, nullptr);
1804  }
1805  }
1807  } else {
1808  tile_processed.setBit(tile_index(piter));
1809  }
1810  }
1812  }
1813 
1814  tile_list_destroy(process_queue);
1815 
1816  if (!city_exist(id)) {
1817  // Wiping trapped units caused city to disappear.
1818  return;
1819  }
1820 
1821  // Any remaining supported units are destroyed
1823  {
1824  wipe_unit(punit, ULR_CITY_LOST, nullptr);
1825  }
1827 
1828  if (!city_exist(id)) {
1829  // Wiping supported units caused city to disappear.
1830  return;
1831  }
1832 
1833  trade_routes_iterate_safe(pcity, proute)
1834  {
1835  struct trade_route *pback =
1836  remove_trade_route(pcity, proute, true, true);
1837 
1838  delete proute;
1839  delete pback;
1840  proute = nullptr;
1841  pback = nullptr;
1842  }
1844 
1845  map_clear_border(pcenter);
1849 
1850  // idex_unregister_city() is called in game_remove_city() below
1851 
1852  /* identity_number_release(pcity->id) is *NOT* done! The cities may
1853  still be alive in the client, or in the player map. The number of
1854  removed cities is small, so the loss is acceptable.
1855  */
1856 
1857  old_vision = pcity->server.vision;
1858  pcity->server.vision = nullptr;
1860  adv_city_free(pcity);
1861 
1862  // Remove city from the map.
1863  tile_set_worked(pcenter, nullptr);
1864 
1865  // Reveal units.
1866  players_iterate(other_player)
1867  {
1868  if (can_player_see_units_in_city(other_player, pcity)
1869  || !map_is_known_and_seen(pcenter, other_player, V_MAIN)) {
1870  continue;
1871  }
1872  unit_list_iterate(pcenter->units, punit)
1873  {
1874  if (can_player_see_unit(other_player, punit)) {
1875  send_unit_info(other_player->connections, punit);
1876  }
1877  }
1879  }
1881 
1882  game.server.mutexes.city_list->lock();
1883  game_remove_city(&wld, pcity);
1884  game.server.mutexes.city_list->unlock();
1885 
1886  // Remove any extras that were only there because the city was there.
1887  extra_type_iterate(pextra)
1888  {
1889  if (tile_has_extra(pcenter, pextra)
1890  && !is_native_tile_to_extra(pextra, pcenter)) {
1891  tile_extra_rm_apply(pcenter, pextra);
1892  }
1893  }
1895 
1896  players_iterate(other_player)
1897  {
1898  if (map_is_known_and_seen(pcenter, other_player, V_MAIN)) {
1899  reality_check_city(other_player, pcenter);
1900  }
1901  }
1903 
1905  {
1906  if (nullptr == pconn->playing && pconn->observer) {
1907  /* For detached observers we have to send a specific packet. This is
1908  * a hack necessitated by the private map that exists for players but
1909  * not observers. */
1910  dsend_packet_city_remove(pconn, id);
1911  }
1912  }
1914 
1915  vision_clear_sight(old_vision);
1916  vision_free(old_vision);
1917 
1918  // Infrastructures may have changed.
1919  send_tile_info(nullptr, pcenter, false);
1920 
1921  /* Build a new palace for free if the player lost her capital and
1922  savepalace is on. */
1923  build_free_small_wonders(powner, &had_small_wonders);
1924 
1925  // Update wonder infos.
1926  if (had_great_wonders) {
1927  send_game_info(nullptr);
1928  // No need to send to detached connections.
1929  send_player_info_c(powner, nullptr);
1930  } else if (BV_ISSET_ANY(had_small_wonders)) {
1931  // No need to send to detached connections.
1932  send_player_info_c(powner, nullptr);
1933  }
1934 
1935  if (old_content_citizens != player_content_citizens(powner)
1936  || old_angry_citizens != player_angry_citizens(powner)) {
1937  /* We crossed the EFT_EMPIRE_SIZE_* effects, we have to refresh all
1938  * cities for the player. */
1939  city_refresh_for_player(powner);
1940  }
1941 
1942  sync_cities();
1943 }
1944 
1961 bool unit_conquer_city(struct unit *punit, struct city *pcity)
1962 {
1963  bool try_civil_war = false;
1964  bool city_remains;
1965  struct player *pplayer = unit_owner(punit);
1966  struct player *cplayer = city_owner(pcity);
1967 
1968  // If not at war, may peacefully enter city.
1969  fc_assert_ret_val_msg(pplayers_at_war(pplayer, cplayer), false,
1970  "Can't conquer city during peace.");
1971 
1972  /* If we cannot occupy the city, this unit entering will not trigger the
1973  * effects below. */
1975  unit_can_take_over(punit)
1976  || utype_can_do_action(unit_type_get(punit), ACTION_PARADROP),
1977  false, "Bad unit for city occupation.");
1978 
1979  /* A transported unit trying to conquer a city should already have been
1980  * unloaded. */
1981  fc_assert_ret_val_msg(punit->transporter == nullptr, false,
1982  "Can't conquer city while transported.");
1983 
1984  /* Okay, we're at war - invader captures/destroys city... */
1985 
1986  /* If a capital is captured, then spark off a civil war
1987  - Kris Bubendorfer
1988  Also check spaceships --dwp
1989  */
1990 
1991  if (is_capital(pcity)
1992  && (cplayer->spaceship.state == SSHIP_STARTED
1993  || cplayer->spaceship.state == SSHIP_LAUNCHED)) {
1994  spaceship_lost(cplayer);
1995  }
1996 
1997  if (is_capital(pcity) && civil_war_possible(cplayer, true, true)
1999  && civil_war_triggered(cplayer)) {
2000  try_civil_war = true;
2001  }
2002 
2003  /*
2004  * We later remove a citizen. Lets check if we can save this since
2005  * the city will be destroyed.
2006  */
2007  if (city_size_get(pcity) <= 1) {
2008  int saved_id = pcity->id;
2009 
2010  notify_player(pplayer, city_tile(pcity), E_UNIT_WIN_ATT, ftc_server,
2011  _("You destroy %s completely."), city_tile_link(pcity));
2012  notify_player(cplayer, city_tile(pcity), E_CITY_LOST, ftc_server,
2013  _("%s has been destroyed by %s."), city_tile_link(pcity),
2014  player_name(pplayer));
2015  script_server_signal_emit("city_destroyed", pcity, cplayer, pplayer);
2016 
2017  // We cant't be sure of city existence after running some script
2018  if (city_exist(saved_id)) {
2019  remove_city(pcity);
2020  }
2021 
2022  if (try_civil_war) {
2023  (void) civil_war(cplayer);
2024  }
2025  return true;
2026  }
2027 
2028  auto coins = cplayer->economic.gold;
2029  coins = MIN(coins, int(fc_rand((coins / 20) + 1))
2030  + (coins * (city_size_get(pcity))) / 200);
2031  pplayer->economic.gold += coins;
2032  cplayer->economic.gold -= coins;
2033  send_player_info_c(pplayer, pplayer->connections);
2034  send_player_info_c(cplayer, cplayer->connections);
2035  if (pcity->original != pplayer) {
2036  if (coins > 0) {
2037  notify_player(pplayer, city_tile(pcity), E_UNIT_WIN_ATT, ftc_server,
2038  PL_("You conquer %s; your lootings accumulate"
2039  " to %d gold!",
2040  "You conquer %s; your lootings accumulate"
2041  " to %d gold!",
2042  coins),
2043  city_link(pcity), coins);
2044  notify_player(cplayer, city_tile(pcity), E_CITY_LOST, ftc_server,
2045  PL_("%s conquered %s and looted %d gold"
2046  " from the city.",
2047  "%s conquered %s and looted %d gold"
2048  " from the city.",
2049  coins),
2050  player_name(pplayer), city_link(pcity), coins);
2051  } else {
2052  notify_player(pplayer, city_tile(pcity), E_UNIT_WIN_ATT, ftc_server,
2053  _("You conquer %s."), city_link(pcity));
2054  notify_player(cplayer, city_tile(pcity), E_CITY_LOST, ftc_server,
2055  _("%s conquered %s."), player_name(pplayer),
2056  city_link(pcity));
2057  }
2058  } else {
2059  if (coins > 0) {
2060  notify_player(pplayer, city_tile(pcity), E_UNIT_WIN_ATT, ftc_server,
2061  PL_("You have liberated %s!"
2062  " Lootings accumulate to %d gold.",
2063  "You have liberated %s!"
2064  " Lootings accumulate to %d gold.",
2065  coins),
2066  city_link(pcity), coins);
2067  notify_player(cplayer, city_tile(pcity), E_CITY_LOST, ftc_server,
2068  PL_("%s liberated %s and looted %d gold"
2069  " from the city.",
2070  "%s liberated %s and looted %d gold"
2071  " from the city.",
2072  coins),
2073  player_name(pplayer), city_link(pcity), coins);
2074  } else {
2075  notify_player(pplayer, city_tile(pcity), E_UNIT_WIN_ATT, ftc_server,
2076  _("You have liberated %s!"), city_link(pcity));
2077  notify_player(cplayer, city_tile(pcity), E_CITY_LOST, ftc_server,
2078  _("%s liberated %s."), player_name(pplayer),
2079  city_link(pcity));
2080  }
2081  }
2082 
2083  if (fc_rand(100) < get_unit_bonus(punit, EFT_CONQUEST_TECH_PCT)) {
2084  // Just try to steal. Ignore failures to get tech
2085  steal_a_tech(pplayer, cplayer, A_UNSET);
2086  }
2087 
2088  /* We transfer the city first so that it is in a consistent state when
2089  * the size is reduced. */
2090  /* FIXME: maybe it should be a ruleset option whether barbarians get
2091  * free buildings such as palaces? */
2092  city_remains = transfer_city(pplayer, pcity, 0, true, true, true,
2093  !is_barbarian(pplayer));
2094 
2095  if (city_remains) {
2096  // reduce size should not destroy this city
2097  fc_assert(city_size_get(pcity) > 1);
2098  city_reduce_size(pcity, 1, pplayer, "conquest");
2099  }
2100 
2101  if (try_civil_war) {
2102  (void) civil_war(cplayer);
2103  }
2104 
2105  if (city_remains) {
2106  script_server_signal_emit("city_transferred", pcity, cplayer, pplayer,
2107  "conquest");
2108  script_server_signal_emit("city_lost", pcity, cplayer, pplayer);
2109  }
2110 
2111  return true;
2112 }
2113 
2117 static int city_got_citywalls(const struct city *pcity)
2118 {
2119  int walls = get_city_bonus(pcity, EFT_VISIBLE_WALLS);
2120 
2121  return walls > 0 ? walls : 0;
2122 }
2123 
2128 {
2129  bool formerly = send_city_suppressed;
2130 
2131  send_city_suppressed = now;
2132  return formerly;
2133 }
2134 
2138 static void package_dumb_city(struct player *pplayer, struct tile *ptile,
2139  struct packet_city_short_info *packet)
2140 {
2141  const vision_site *pdcity = map_get_player_city(ptile, pplayer);
2142 
2143  fc_assert_ret(pdcity != nullptr);
2144  packet->id = pdcity->identity;
2145  packet->owner = player_number(vision_site_owner(pdcity));
2146  packet->tile = tile_index(ptile);
2147  sz_strlcpy(packet->name, pdcity->name);
2148  packet->size = vision_site_size_get(pdcity);
2149  packet->occupied = pdcity->occupied;
2150  packet->walls = pdcity->walls;
2151  packet->style = pdcity->style;
2152  packet->city_image = pdcity->city_image;
2153  packet->capital = pdcity->capital;
2154  packet->happy = pdcity->happy;
2155  packet->unhappy = pdcity->unhappy;
2156  packet->improvements = pdcity->improvements;
2157 }
2158 
2163 void refresh_dumb_city(struct city *pcity)
2164 {
2165  players_iterate(pplayer)
2166  {
2167  if (player_can_see_city_externals(pplayer, pcity)) {
2168  if (update_dumb_city(pplayer, pcity)) {
2169  struct packet_city_short_info packet;
2170 
2171  if (city_owner(pcity) != pplayer) {
2172  /* Don't send the short_city information to someone who can see the
2173  * city's internals. Doing so would really confuse the client. */
2174  package_dumb_city(pplayer, pcity->tile, &packet);
2175  lsend_packet_city_short_info(pplayer->connections, &packet);
2176  }
2177  }
2178  }
2179  }
2181 
2182  /* Don't send to non-player observers since they don't have 'dumb city'
2183  * information. */
2184 }
2185 
2194 static void broadcast_city_info(struct city *pcity)
2195 {
2196  struct packet_city_info packet;
2197  struct packet_city_short_info sc_pack;
2198  struct player *powner = city_owner(pcity);
2199  struct traderoute_packet_list *routes = traderoute_packet_list_new();
2200 
2201  // Send to everyone who can see the city.
2202  package_city(pcity, &packet, routes, false);
2203  players_iterate(pplayer)
2204  {
2205  if (can_player_see_city_internals(pplayer, pcity)) {
2206  if (!send_city_suppressed || pplayer != powner) {
2207  update_dumb_city(powner, pcity);
2208  lsend_packet_city_info(powner->connections, &packet, false);
2209  traderoute_packet_list_iterate(routes, route_packet)
2210  {
2211  lsend_packet_traderoute_info(powner->connections, route_packet);
2212  }
2214  }
2215  } else {
2216  if (player_can_see_city_externals(pplayer, pcity)) {
2217  reality_check_city(pplayer, pcity->tile);
2218  update_dumb_city(pplayer, pcity);
2219  package_dumb_city(pplayer, pcity->tile, &sc_pack);
2220  lsend_packet_city_short_info(pplayer->connections, &sc_pack);
2221  }
2222  }
2223  }
2225 
2226  // Send to global observers.
2228  {
2229  if (conn_is_global_observer(pconn)) {
2230  send_packet_city_info(pconn, &packet, false);
2231  }
2232  }
2234 
2235  traderoute_packet_list_iterate(routes, route_packet)
2236  {
2237  delete route_packet;
2238  route_packet = nullptr;
2239  }
2241  traderoute_packet_list_destroy(routes);
2242 }
2243 
2248 void send_all_known_cities(struct conn_list *dest)
2249 {
2250  conn_list_do_buffer(dest);
2251  conn_list_iterate(dest, pconn)
2252  {
2253  struct player *pplayer = pconn->playing;
2254 
2255  if (!pplayer && !pconn->observer) {
2256  continue;
2257  }
2258  whole_map_iterate(&(wld.map), ptile)
2259  {
2260  if (!pplayer || nullptr != map_get_player_site(ptile, pplayer)) {
2261  send_city_info_at_tile(pplayer, pconn->self, nullptr, ptile);
2262  }
2263  }
2265  }
2267  conn_list_do_unbuffer(dest);
2268  flush_packets();
2269 }
2270 
2274 void send_player_cities(struct player *pplayer)
2275 {
2276  city_list_iterate(pplayer->cities, pcity)
2277  {
2278  if (city_refresh(pcity)) {
2279  qCritical("%s radius changed while sending to player.",
2280  city_name_get(pcity));
2281 
2282  // Make sure that no workers in illegal position outside radius.
2283  auto_arrange_workers(pcity);
2284  }
2285  send_city_info(pplayer, pcity);
2286  }
2288 }
2289 
2295 void send_city_info(struct player *dest, struct city *pcity)
2296 {
2297  struct player *powner = city_owner(pcity);
2298 
2299  if (S_S_RUNNING != server_state() && S_S_OVER != server_state()) {
2300  return;
2301  }
2302 
2303  if (dest == powner && send_city_suppressed) {
2304  return;
2305  }
2306 
2307  if (!dest || dest == powner) {
2308  pcity->server.synced = true;
2309  }
2310 
2311  if (!dest) {
2312  broadcast_city_info(pcity);
2313  } else {
2314  send_city_info_at_tile(dest, dest->connections, pcity, pcity->tile);
2315  }
2316 
2317  if (game.info.team_pooled_research
2318  && player_list_size(team_members(powner->team)) > 1) {
2319  // We want to send the new total bulbs production of the team.
2320  send_research_info(research_get(powner), nullptr);
2321  }
2322 }
2323 
2344 void send_city_info_at_tile(struct player *pviewer, struct conn_list *dest,
2345  struct city *pcity, struct tile *ptile)
2346 {
2347  struct packet_city_info packet;
2348  struct packet_city_short_info sc_pack;
2349  struct player *powner = nullptr;
2350  struct traderoute_packet_list *routes = nullptr;
2351 
2352  if (!pcity) {
2353  pcity = tile_city(ptile);
2354  }
2355  if (pcity) {
2356  powner = city_owner(pcity);
2357  }
2358  if (powner && powner == pviewer) {
2359  // send info to owner
2360  // This case implies powner non-nullptr which means pcity non-nullptr
2361  if (!send_city_suppressed) {
2362  routes = traderoute_packet_list_new();
2363 
2364  // send all info to the owner
2365  update_dumb_city(powner, pcity);
2366  package_city(pcity, &packet, routes, false);
2367  lsend_packet_city_info(dest, &packet, false);
2368  traderoute_packet_list_iterate(routes, route_packet)
2369  {
2370  lsend_packet_traderoute_info(dest, route_packet);
2371  }
2373  if (dest == powner->connections) {
2374  // HACK: send also a copy to global observers.
2376  {
2377  if (conn_is_global_observer(pconn)) {
2378  send_packet_city_info(pconn, &packet, false);
2379  traderoute_packet_list_iterate(routes, route_packet)
2380  {
2381  send_packet_traderoute_info(pconn, route_packet);
2382  }
2384  }
2385  }
2387  }
2388  }
2389  } else {
2390  // send info to non-owner
2391  if (!pviewer) { // observer
2392  if (pcity) {
2393  routes = traderoute_packet_list_new();
2394 
2395  package_city(pcity, &packet, routes,
2396  false); // should be dumb_city info?
2397  lsend_packet_city_info(dest, &packet, false);
2398  traderoute_packet_list_iterate(routes, route_packet)
2399  {
2400  lsend_packet_traderoute_info(dest, route_packet);
2401  }
2403  }
2404  } else {
2405  if (!map_is_known(ptile, pviewer)) {
2406  // Without the conditional we'd have an infinite loop here.
2407  map_show_tile(pviewer, ptile);
2408  }
2409  if (map_is_known_and_seen(ptile, pviewer, V_MAIN)) {
2410  if (pcity) { // it's there and we see it; update and send
2411  update_dumb_city(pviewer, pcity);
2412  package_dumb_city(pviewer, ptile, &sc_pack);
2413  lsend_packet_city_short_info(dest, &sc_pack);
2414  }
2415  } else { // not seen; send old info
2416  if (nullptr != map_get_player_site(ptile, pviewer)) {
2417  package_dumb_city(pviewer, ptile, &sc_pack);
2418  lsend_packet_city_short_info(dest, &sc_pack);
2419  }
2420  }
2421  }
2422  }
2423 
2424  if (routes != nullptr) {
2425  traderoute_packet_list_iterate(routes, route_packet)
2426  {
2427  delete route_packet;
2428  route_packet = nullptr;
2429  }
2431  traderoute_packet_list_destroy(routes);
2432  }
2433 }
2434 
2438 void package_city(struct city *pcity, struct packet_city_info *packet,
2439  struct traderoute_packet_list *routes, bool dipl_invest)
2440 {
2441  int i;
2442  int ppl = 0;
2443 
2444  packet->id = pcity->id;
2445  packet->owner = player_number(city_owner(pcity));
2446  packet->tile = tile_index(city_tile(pcity));
2447  sz_strlcpy(packet->name, city_name_get(pcity));
2448 
2449  packet->size = city_size_get(pcity);
2450  for (i = 0; i < FEELING_LAST; i++) {
2451  packet->ppl_happy[i] = pcity->feel[CITIZEN_HAPPY][i];
2452  packet->ppl_content[i] = pcity->feel[CITIZEN_CONTENT][i];
2453  packet->ppl_unhappy[i] = pcity->feel[CITIZEN_UNHAPPY][i];
2454  packet->ppl_angry[i] = pcity->feel[CITIZEN_ANGRY][i];
2455  if (i == 0) {
2456  ppl += packet->ppl_happy[i];
2457  ppl += packet->ppl_content[i];
2458  ppl += packet->ppl_unhappy[i];
2459  ppl += packet->ppl_angry[i];
2460  }
2461  }
2462  // The number of data in specialists[] array
2463  packet->specialists_size = specialist_count();
2465  {
2466  packet->specialists[sp] = pcity->specialists[sp];
2467  ppl += packet->specialists[sp];
2468  }
2470 
2471  // The nationality of the citizens.
2472  packet->nationalities_count = 0;
2473  if (game.info.citizen_nationality) {
2474  int cit = 0;
2475 
2476  player_slots_iterate(pslot)
2477  {
2478  citizens nationality = citizens_nation_get(pcity, pslot);
2479  if (nationality != 0) {
2480  // This player should exist!
2481  fc_assert(player_slot_get_player(pslot) != nullptr);
2482 
2483  packet->nation_id[packet->nationalities_count] =
2484  player_slot_index(pslot);
2485  packet->nation_citizens[packet->nationalities_count] = nationality;
2486  packet->nationalities_count++;
2487 
2488  cit += nationality;
2489  }
2490  }
2492 
2493  fc_assert(cit == packet->size);
2494  }
2495 
2496  packet->history = pcity->history;
2497  packet->culture = city_culture(pcity);
2498  packet->buy_cost = city_production_buy_gold_cost(pcity);
2499 
2500  if (packet->size != ppl) {
2501  static bool recursion = false;
2502 
2503  if (recursion) {
2504  /* Recursion didn't help. Do not enter infinite recursive loop.
2505  * Package city as it is. */
2506  qCritical("Failed to fix inconsistent city size.");
2507  recursion = false;
2508  } else {
2509  /* Note: If you get this error and try to debug the cause, you may find
2510  * using sanity_check_feelings() in some key points useful. */
2511  /* Have this as an fc_assert() first, so one can use '-F' to caught
2512  * these in debugger. */
2513  fc_assert(packet->size == ppl);
2514 
2515  // In all builds have an error message shown.
2516  qCritical("City size %d, citizen count %d for %s", packet->size, ppl,
2517  city_name_get(pcity));
2518 
2519  // Try to fix
2520  city_refresh(pcity);
2521  auto_arrange_workers(pcity);
2522 
2523  // And repackage
2524  recursion = true;
2525  package_city(pcity, packet, routes, dipl_invest);
2526  recursion = false;
2527 
2528  return;
2529  }
2530  }
2531 
2532  packet->city_radius_sq = pcity->city_radius_sq;
2533 
2534  i = 0;
2535  trade_routes_iterate(pcity, proute)
2536  {
2537  auto *tri_packet = new packet_traderoute_info;
2538 
2539  tri_packet->city = pcity->id;
2540  tri_packet->index = i;
2541  tri_packet->partner = proute->partner;
2542  tri_packet->value = proute->value;
2543  tri_packet->direction = proute->dir;
2544  tri_packet->goods = goods_number(proute->goods);
2545 
2546  traderoute_packet_list_append(routes, tri_packet);
2547 
2548  i++;
2549  }
2551 
2552  packet->traderoute_count = i;
2553 
2555  {
2556  packet->surplus[o] = pcity->surplus[o];
2557  packet->waste[o] = pcity->waste[o];
2558  packet->unhappy_penalty[o] = pcity->unhappy_penalty[o];
2559  packet->prod[o] = pcity->prod[o];
2560  packet->citizen_base[o] = pcity->citizen_base[o];
2561  packet->usage[o] = pcity->usage[o];
2562  }
2564 
2565  packet->food_stock = pcity->food_stock;
2566  packet->shield_stock = pcity->shield_stock;
2567  packet->pollution = pcity->pollution;
2568  packet->illness_trade = pcity->illness_trade;
2569  packet->city_options = pcity->city_options;
2570 
2571  packet->production_kind = pcity->production.kind;
2572  packet->production_value = universal_number(&pcity->production);
2573 
2574  packet->turn_last_built = pcity->turn_last_built;
2575  packet->turn_founded = pcity->turn_founded;
2576 
2577  packet->changed_from_kind = pcity->changed_from.kind;
2578  packet->changed_from_value = universal_number(&pcity->changed_from);
2579 
2580  packet->before_change_shields = pcity->before_change_shields;
2581  packet->disbanded_shields = pcity->disbanded_shields;
2582  packet->caravan_shields = pcity->caravan_shields;
2583  packet->last_turns_shield_surplus = pcity->last_turns_shield_surplus;
2584  packet->bought_shields = pcity->bought_shields;
2585 
2586  worklist_copy(&packet->worklist, &pcity->worklist);
2587  packet->diplomat_investigate = dipl_invest;
2588 
2589  packet->airlift = pcity->airlift;
2590  packet->did_buy = pcity->did_buy;
2591  packet->did_buy_production = pcity->bought_shields > 0;
2592  packet->did_sell = pcity->did_sell;
2593  packet->was_happy = pcity->was_happy;
2594 
2595  packet->walls = city_got_citywalls(pcity);
2596  packet->style = pcity->style;
2597  packet->city_image = get_city_bonus(pcity, EFT_CITY_IMAGE);
2598  packet->capital = pcity->capital;
2599  packet->steal = pcity->steal;
2600 
2601  packet->rally_point_length = pcity->rally_point.length;
2602  packet->rally_point_persistent = pcity->rally_point.persistent;
2603  packet->rally_point_vigilant = pcity->rally_point.vigilant;
2604  if (pcity->rally_point.length) {
2605  memcpy(packet->rally_point_orders, pcity->rally_point.orders,
2606  pcity->rally_point.length * sizeof(struct unit_order));
2607  }
2608 
2609  if (pcity->cm_parameter) {
2610  packet->cma_enabled = true;
2611  cm_copy_parameter(&packet->cm_parameter, pcity->cm_parameter);
2612  } else {
2613  packet->cma_enabled = false;
2614  memset(&packet->cm_parameter, 0, sizeof(packet->cm_parameter));
2615  }
2616 
2617  BV_CLR_ALL(packet->improvements);
2618  improvement_iterate(pimprove)
2619  {
2620  if (city_has_building(pcity, pimprove)) {
2621  BV_SET(packet->improvements, improvement_index(pimprove));
2622  }
2623  }
2625 }
2626 
2637 bool update_dumb_city(struct player *pplayer, struct city *pcity)
2638 {
2639  bv_imprs improvements;
2640  struct tile *pcenter = city_tile(pcity);
2641  struct vision_site *pdcity = map_get_player_city(pcenter, pplayer);
2642  /* pcity->client.occupied isn't used at the server, so we go straight to
2643  * the unit list to check the occupied status. */
2644  bool occupied = (unit_list_size(pcenter->units) > 0);
2645  bool walls = city_got_citywalls(pcity);
2646  bool happy = city_happy(pcity);
2647  bool unhappy = city_unhappy(pcity);
2648  int style = pcity->style;
2649  int city_image = get_city_bonus(pcity, EFT_CITY_IMAGE);
2650  enum capital_type capital = pcity->capital;
2651 
2653  improvement_iterate(pimprove)
2654  {
2655  if (is_improvement_visible(pimprove)
2656  && city_has_building(pcity, pimprove)) {
2658  }
2659  }
2661 
2662  if (nullptr == pdcity) {
2663  pdcity = vision_site_new_from_city(pcity);
2664  change_playertile_site(map_get_player_tile(pcenter, pplayer), pdcity);
2665  } else if (pdcity->location != pcenter) {
2666  qCritical("Trying to update bad city (wrong location) "
2667  "at %i,%i for player %s",
2668  TILE_XY(pcity->tile), player_name(pplayer));
2669  fc_assert(pdcity->location == pcenter);
2670  pdcity->location = pcenter; // ??
2671  } else if (pdcity->identity != pcity->id) {
2672  qCritical("Trying to update old city (wrong identity) "
2673  "at %i,%i for player %s",
2674  TILE_XY(city_tile(pcity)), player_name(pplayer));
2675  fc_assert(pdcity->identity == pcity->id);
2676  pdcity->identity = pcity->id; // ??
2677  } else if (pdcity->occupied == occupied && pdcity->walls == walls
2678  && pdcity->happy == happy && pdcity->unhappy == unhappy
2679  && pdcity->style == style && pdcity->city_image == city_image
2680  && pdcity->capital == capital
2682  && vision_site_size_get(pdcity) == city_size_get(pcity)
2683  && vision_site_owner(pdcity) == city_owner(pcity)
2684  && 0 == strcmp(pdcity->name, city_name_get(pcity))) {
2685  return false;
2686  }
2687 
2688  vision_site_update_from_city(pdcity, pcity);
2689  pdcity->occupied = occupied;
2690  pdcity->walls = walls;
2691  pdcity->style = style;
2692  pdcity->city_image = city_image;
2693  pdcity->capital = capital;
2694  pdcity->happy = happy;
2695  pdcity->unhappy = unhappy;
2696  pdcity->improvements = improvements;
2697 
2698  return true;
2699 }
2700 
2704 void reality_check_city(struct player *pplayer, struct tile *ptile)
2705 {
2706  auto playtile = map_get_player_tile(ptile, pplayer);
2707 
2708  if (playtile->site && playtile->site->location == ptile) {
2709  struct city *pcity = tile_city(ptile);
2710 
2711  if (!pcity || pcity->id != playtile->site->identity) {
2712  dlsend_packet_city_remove(pplayer->connections,
2713  playtile->site->identity);
2714  playtile->site = nullptr;
2715  }
2716  }
2717 }
2718 
2722 void remove_dumb_city(struct player *pplayer, struct tile *ptile)
2723 {
2724  auto playtile = map_get_player_tile(ptile, pplayer);
2725 
2726  if (playtile->site && playtile->site->location == ptile) {
2727  dlsend_packet_city_remove(pplayer->connections,
2728  playtile->site->identity);
2729  playtile->site = nullptr;
2730  }
2731 }
2732 
2737 static void announce_trade_route_removal(struct city *pc1, struct city *pc2,
2738  bool source_gone)
2739 {
2740  struct player *plr1 = city_owner(pc1);
2741  struct player *plr2 = city_owner(pc2);
2742  char city1_link[MAX_LEN_LINK];
2743  char city2_link[MAX_LEN_LINK];
2744 
2745  sz_strlcpy(city1_link, city_link(pc1));
2746  sz_strlcpy(city2_link, city_link(pc2));
2747 
2748  if (plr1 == plr2) {
2749  if (source_gone) {
2750  notify_player(plr2, city_tile(pc2), E_CARAVAN_ACTION, ftc_server,
2751  _("Trade between %s and %s lost along with city."),
2752  city1_link, city2_link);
2753  } else {
2754  notify_player(plr1, city_tile(pc1), E_CARAVAN_ACTION, ftc_server,
2755  _("Trade route between %s and %s canceled."), city1_link,
2756  city2_link);
2757  }
2758  } else {
2759  if (source_gone) {
2760  notify_player(plr2, city_tile(pc2), E_CARAVAN_ACTION, ftc_server,
2761  // TRANS: "...between Spanish city Madrid and Paris..."
2762  _("Trade between %s city %s and %s lost along with "
2763  "their city."),
2764  nation_adjective_for_player(plr1), city1_link,
2765  city2_link);
2766  /* It's implicit to removed city's owner that that city no longer
2767  * has trade routes, so say nothing in that case */
2768  } else {
2769  notify_player(plr2, city_tile(pc2), E_CARAVAN_ACTION, ftc_server,
2770  _("Sorry, the %s canceled the trade route "
2771  "from %s to your city %s."),
2772  nation_plural_for_player(plr1), city1_link, city2_link);
2773  notify_player(plr1, city_tile(pc1), E_CARAVAN_ACTION, ftc_server,
2774  // TRANS: "...from Paris to Spanish city Madrid."
2775  _("We canceled the trade route "
2776  "from %s to %s city %s."),
2777  city1_link, nation_adjective_for_player(plr2),
2778  city2_link);
2779  }
2780  }
2781 }
2782 
2792  struct trade_route *proute,
2793  bool announce, bool source_gone)
2794 {
2795  struct city *pc2 = game_city_by_number(proute->partner);
2796  struct trade_route *back_route = nullptr;
2797 
2798  fc_assert_ret_val(pc1 && proute, nullptr);
2799 
2800  trade_route_list_remove(pc1->routes, proute);
2801 
2802  if (pc2 != nullptr) {
2803  trade_routes_iterate(pc2, pback)
2804  {
2805  if (pc1->id == pback->partner) {
2806  back_route = pback;
2807  }
2808  }
2810 
2811  if (back_route != nullptr) {
2812  trade_route_list_remove(pc2->routes, back_route);
2813  }
2814  }
2815 
2816  if (announce && pc2) {
2817  announce_trade_route_removal(pc1, pc2, source_gone);
2818 
2819  city_refresh(pc2);
2820  send_city_info(city_owner(pc2), pc2);
2821  }
2822 
2823  return back_route;
2824 }
2825 
2829 void city_illness_strike(struct city *pcity)
2830 {
2831  notify_player(city_owner(pcity), city_tile(pcity), E_CITY_PLAGUE,
2832  ftc_server,
2833  _("%s has been struck by a plague! Population lost!"),
2834  city_link(pcity));
2835  city_reduce_size(pcity, 1, nullptr, "plague");
2836  pcity->turn_plague = game.info.turn;
2837 
2838  // recalculate illness
2839  pcity->server.illness = city_illness_calc(
2840  pcity, nullptr, nullptr, &(pcity->illness_trade), nullptr);
2841 }
2842 
2849 void do_sell_building(struct player *pplayer, struct city *pcity,
2850  struct impr_type *pimprove, const char *reason)
2851 {
2852  if (can_city_sell_building(pcity, pimprove)) {
2853  pplayer->economic.gold += impr_sell_gold(pimprove);
2854  building_lost(pcity, pimprove, reason, nullptr);
2855  }
2856 }
2857 
2861 bool building_removed(struct city *pcity, const struct impr_type *pimprove,
2862  const char *reason, struct unit *destroyer)
2863 {
2864  int backup = pcity->id;
2865 
2866  city_remove_improvement(pcity, pimprove);
2867 
2868  script_server_signal_emit("building_lost", pcity, pimprove, reason,
2869  destroyer);
2870 
2871  return city_exist(backup);
2872 }
2873 
2878 void building_lost(struct city *pcity, const struct impr_type *pimprove,
2879  const char *reason, struct unit *destroyer)
2880 {
2881  struct player *owner = city_owner(pcity);
2882  bool was_capital = is_capital(pcity);
2883  bool city_remains;
2884 
2885  city_remains = building_removed(pcity, pimprove, reason, destroyer);
2886 
2887  if ((was_capital && (!city_remains || !is_capital(pcity)))
2888  && (owner->spaceship.state == SSHIP_STARTED
2889  || owner->spaceship.state == SSHIP_LAUNCHED)) {
2890  /* If the capital was lost (by destruction of the palace) production on
2891  * the spaceship is lost. */
2892  spaceship_lost(owner);
2893  }
2894 
2895  if (city_remains) {
2896  // update city; influence of effects (buildings, ...) on unit upkeep
2897  if (city_refresh(pcity)) {
2898  auto_arrange_workers(pcity);
2899  }
2900 
2901  /* Re-update the city's visible area. This updates fog if the vision
2902  * range increases or decreases. */
2903  city_refresh_vision(pcity);
2904  }
2905 }
2906 
2927 void city_units_upkeep(const struct city *pcity)
2928 {
2929  int free_uk[O_LAST];
2930  int cost;
2931  const struct unit_type *ut;
2932  struct player *plr;
2933  bool update;
2934 
2935  if (!pcity || !pcity->units_supported
2936  || unit_list_size(pcity->units_supported) < 1) {
2937  return;
2938  }
2939 
2940  memset(free_uk, 0, O_LAST * sizeof(*free_uk));
2942  {
2943  free_uk[o] = get_city_output_bonus(pcity, get_output_type(o),
2944  EFT_UNIT_UPKEEP_FREE_PER_CITY);
2945  }
2947 
2948  // save the upkeep for all units in the corresponding punit struct
2949  unit_list_iterate(pcity->units_supported, punit)
2950  {
2951  ut = unit_type_get(punit);
2952  plr = unit_owner(punit);
2953  update = false;
2954 
2956  {
2957  cost = utype_upkeep_cost(ut, plr, o);
2958  if (cost > 0) {
2959  if (free_uk[o] > cost) {
2960  free_uk[o] -= cost;
2961  cost = 0;
2962  } else {
2963  cost -= free_uk[o];
2964  free_uk[o] = 0;
2965  }
2966  }
2967 
2968  if (cost != punit->upkeep[o]) {
2969  update = true;
2970  punit->upkeep[o] = cost;
2971  }
2972  }
2974 
2975  if (update) {
2976  // Update unit information to the player and global observers.
2977  send_unit_info(nullptr, punit);
2978  }
2979  }
2981 }
2982 
2986 void change_build_target(struct player *pplayer, struct city *pcity,
2987  struct universal *target, enum event_type event)
2988 {
2989  const char *name;
2990  const char *source;
2991 
2992  // If the city is already building this thing, don't do anything
2993  if (are_universals_equal(&pcity->production, target)) {
2994  return;
2995  }
2996 
2997  // Is the city no longer building a wonder?
2998  if (VUT_IMPROVEMENT == pcity->production.kind
3000  && event != E_IMP_AUTO && event != E_WORKLIST) {
3001  /* If the build target is changed because of an advisor's suggestion or
3002  because the worklist advances, then the wonder was completed --
3003  don't announce that the player has *stopped* building that wonder.
3004  */
3005  notify_player(nullptr, city_tile(pcity), E_WONDER_STOPPED, ftc_server,
3006  _("The %s have stopped building The %s in %s."),
3007  nation_plural_for_player(pplayer),
3009  }
3010 
3011  /* Manage the city change-production penalty.
3012  (May penalize, restore or do nothing to the shield_stock.) */
3013  if (!is_ai(pplayer) || has_handicap(pplayer, H_PRODCHGPEN)) {
3014  pcity->shield_stock = city_change_production_penalty(pcity, target);
3015  }
3016 
3017  // Change build target.
3018  pcity->production = *target;
3019 
3020  // Update action timestamp inherited by any units being produced
3021  city_did_prod_change(pcity);
3022 
3023  // What's the name of the target?
3025 
3026  switch (event) {
3027  case E_WORKLIST:
3028  /* TRANS: Possible 'source' of the production change
3029  * (in "<city> is building ..." sentence). Preserve leading space. */
3030  source = _(" from the worklist");
3031  break;
3032  case E_IMP_AUTO:
3033  /* TRANS: Possible 'source' of the production change
3034  * (in "<city> is building ..." sentence). Preserve leading space. */
3035  source = _(" as suggested by the advisor");
3036  break;
3037  default:
3038  source = "";
3039  break;
3040  }
3041 
3042  log_base(LOG_BUILD_TARGET, "%s started building %s%s.",
3043  city_name_get(pcity), name, source);
3044 
3045  // Tell the player what's up.
3046  /* FIXME: this may give bad grammar when translated if the 'source'
3047  * string can have multiple values. */
3048  notify_player(pplayer, city_tile(pcity), event, ftc_server,
3049  /* TRANS: "<city> is building <production><source>."
3050  * 'source' might be an empty string, or a clause like
3051  * " from the worklist". */
3052  _("%s is building %s%s."), city_link(pcity), name, source);
3053 
3054  /* If the city is building a wonder, tell the rest of the world
3055  about it. */
3056  if (VUT_IMPROVEMENT == pcity->production.kind
3058  notify_player(nullptr, city_tile(pcity), E_WONDER_STARTED, ftc_server,
3059  _("The %s have started building The %s in %s."),
3060  nation_plural_for_player(pplayer), name, city_link(pcity));
3061  }
3062 }
3063 
3068 void city_did_prod_change(struct city *pcity)
3069 {
3070  pcity->server.prod_change_timestamp = time(nullptr);
3071  pcity->server.prod_change_turn = game.info.turn;
3072 }
3073 
3079 void city_map_update_empty(struct city *pcity, struct tile *ptile)
3080 {
3081  tile_set_worked(ptile, nullptr);
3082  send_tile_info(nullptr, ptile, false);
3083  pcity->server.synced = false;
3084 }
3085 
3091 void city_map_update_worker(struct city *pcity, struct tile *ptile)
3092 {
3093  tile_set_worked(ptile, pcity);
3094  send_tile_info(nullptr, ptile, false);
3095  pcity->server.synced = false;
3096 }
3097 
3103 static bool city_map_update_tile_direct(struct tile *ptile, bool queued)
3104 {
3105  struct city *pwork = tile_worked(ptile);
3106 
3107  if (nullptr != pwork && !is_city_center(pwork, ptile)
3108  && !city_can_work_tile(pwork, ptile)) {
3109  tile_set_worked(ptile, nullptr);
3110  send_tile_info(nullptr, ptile, false);
3111 
3112  pwork->specialists[DEFAULT_SPECIALIST]++; // keep city sanity
3113  pwork->server.synced = false;
3114 
3115  if (queued) {
3116  city_freeze_workers_queue(pwork); // place the displaced later
3117  } else {
3118  city_refresh(pwork); // Specialist added, keep citizen count sanity
3119  auto_arrange_workers(pwork);
3120  send_city_info(nullptr, pwork);
3121  }
3122  return true;
3123  }
3124 
3125  return false;
3126 }
3127 
3134 {
3135  return city_map_update_tile_direct(ptile, true);
3136 }
3137 
3141 bool city_map_update_tile_now(struct tile *ptile)
3142 {
3143  return city_map_update_tile_direct(ptile, false);
3144 }
3145 
3151 {
3152  if (send_city_suppressed) {
3153  return;
3154  }
3155 
3156  players_iterate(pplayer)
3157  {
3158  city_list_iterate(pplayer->cities, pcity)
3159  {
3160  if (!pcity->server.synced) {
3161  // sending will set to TRUE.
3162  send_city_info(pplayer, pcity);
3163  }
3164  }
3166  }
3168 }
3169 
3173 void city_map_update_all(struct city *pcity)
3174 {
3175  struct tile *pcenter = city_tile(pcity);
3176 
3178  ptile, _index, _x, _y)
3179  {
3180  // bypass city_map_update_tile_now() for efficiency
3181  city_map_update_tile_direct(ptile, false);
3182  }
3184 }
3185 
3190 {
3191  city_list_iterate(pplayer->cities, pcity)
3192  {
3193  city_freeze_workers(pcity);
3194  city_map_update_all(pcity);
3195  city_thaw_workers(pcity);
3196  }
3198 }
3199 
3211 {
3212  adjc_iterate(&(wld.map), ptile, tile1)
3213  {
3214  struct city *pcity = tile_city(tile1);
3215 
3216  if (pcity && !is_terrain_class_near_tile(tile1, TC_OCEAN)) {
3217  struct player *pplayer = city_owner(pcity);
3218 
3219  /* Sell all buildings (but not Wonders) that must be next to the ocean
3220  */
3221  city_built_iterate(pcity, pimprove)
3222  {
3223  if (!can_city_sell_building(pcity, pimprove)) {
3224  continue;
3225  }
3226 
3227  requirement_vector_iterate(&pimprove->reqs, preq)
3228  {
3229  if ((VUT_TERRAIN == preq->source.kind
3230  || VUT_TERRAINCLASS == preq->source.kind)
3231  && !is_req_active(city_owner(pcity), nullptr, pcity, nullptr,
3232  nullptr, nullptr, nullptr, nullptr, nullptr,
3233  nullptr, preq, RPT_CERTAIN)) {
3234  int price = impr_sell_gold(pimprove);
3235 
3236  do_sell_building(pplayer, pcity, pimprove, "landlocked");
3237  notify_player(pplayer, tile1, E_IMP_SOLD, ftc_server,
3238  PL_("You sell %s in %s (now landlocked)"
3239  " for %d gold.",
3240  "You sell %s in %s (now landlocked)"
3241  " for %d gold.",
3242  price),
3243  improvement_name_translation(pimprove),
3244  city_link(pcity), price);
3245  }
3246  }
3248  }
3250  }
3251  }
3253 }
3254 
3261 void city_refresh_vision(struct city *pcity)
3262 {
3263  v_radius_t vision_radius_sq = V_RADIUS(
3264  (short) get_city_bonus(pcity, EFT_CITY_VISION_RADIUS_SQ, V_MAIN),
3265  (short) get_city_bonus(pcity, EFT_CITY_VISION_RADIUS_SQ, V_INVIS),
3266  (short) get_city_bonus(pcity, EFT_CITY_VISION_RADIUS_SQ,
3267  V_SUBSURFACE));
3268 
3269  vision_change_sight(pcity->server.vision, vision_radius_sq);
3270  ASSERT_VISION(pcity->server.vision);
3271 }
3272 
3278 {
3279  city_list_iterate(pplayer->cities, pcity) { city_refresh_vision(pcity); }
3281 }
3282 
3286 bool city_map_update_radius_sq(struct city *pcity)
3287 {
3288  fc_assert_ret_val(pcity != nullptr, false);
3289 
3290  int city_tiles_old, city_tiles_new;
3291  int city_radius_sq_old = city_map_radius_sq_get(pcity);
3292  int city_radius_sq_new = game.info.init_city_radius_sq
3293  + get_city_bonus(pcity, EFT_CITY_RADIUS_SQ);
3294 
3295  /* check minimum / maximum allowed city radii */
3296  city_radius_sq_new = CLIP(CITY_MAP_MIN_RADIUS_SQ, city_radius_sq_new,
3298 
3299  if (city_radius_sq_new == city_radius_sq_old) {
3300  // no change
3301  return false;
3302  }
3303 
3304  // get number of city tiles for each radii
3305  city_tiles_old = city_map_tiles(city_radius_sq_old);
3306  city_tiles_new = city_map_tiles(city_radius_sq_new);
3307 
3308  if (city_tiles_old == city_tiles_new) {
3309  /* a change of the squared city radius but no change of the number of
3310  * city tiles */
3311  return false;
3312  ;
3313  }
3314 
3315  log_debug("[%s (%d)] city_map_radius_sq: %d => %d", city_name_get(pcity),
3316  pcity->id, city_radius_sq_old, city_radius_sq_new);
3317 
3318  // workers map before
3319  log_debug("[%s (%d)] city size: %d; specialists: %d (before change)",
3320  city_name_get(pcity), pcity->id, city_size_get(pcity),
3321  city_specialists(pcity));
3323 
3324  city_map_radius_sq_set(pcity, city_radius_sq_new);
3325 
3326  if (city_tiles_old < city_tiles_new) {
3327  // increased number of city tiles
3328  city_refresh_vision(pcity);
3329  } else {
3330  // reduced number of city tiles
3331  int workers = 0;
3332 
3333  // remove workers from the tiles removed rom the city map
3334  city_map_iterate_radius_sq(city_radius_sq_new, city_radius_sq_old,
3335  city_x, city_y)
3336  {
3337  struct tile *ptile = city_map_to_tile(
3338  city_tile(pcity), city_radius_sq_old, city_x, city_y);
3339 
3340  if (ptile && pcity == tile_worked(ptile)) {
3341  city_map_update_empty(pcity, ptile);
3342  workers++;
3343  }
3344  }
3346 
3347  // add workers to free city tiles
3348  if (workers > 0) {
3349  int radius_sq = city_map_radius_sq_get(pcity);
3350  city_map_iterate_without_index(radius_sq, city_x, city_y)
3351  {
3352  struct tile *ptile =
3353  city_map_to_tile(city_tile(pcity), radius_sq, city_x, city_y);
3354 
3355  if (ptile && !is_city_center(pcity, ptile)
3356  && tile_worked(ptile) != pcity
3357  && city_can_work_tile(pcity, ptile)) {
3358  city_map_update_worker(pcity, ptile);
3359  workers--;
3360  }
3361 
3362  if (workers <= 0) {
3363  break;
3364  }
3365  }
3367  }
3368 
3369  // if there are still workers they will be updated to specialists
3370  if (workers > 0) {
3371  pcity->specialists[DEFAULT_SPECIALIST] += workers;
3372  }
3373 
3374  city_refresh_vision(pcity);
3375  }
3376 
3377  // if city is under AI control update it
3378  adv_city_update(pcity);
3379 
3380  notify_player(
3381  city_owner(pcity), city_tile(pcity), E_CITY_RADIUS_SQ, ftc_server,
3382  _("The size of the city map of %s is %s."), city_name_get(pcity),
3383  city_tiles_old < city_tiles_new ? _("increased") : _("reduced"));
3384 
3385  // workers map after
3386  log_debug("[%s (%d)] city size: %d; specialists: %d (after change)",
3387  city_name_get(pcity), pcity->id, city_size_get(pcity),
3388  city_specialists(pcity));
3390 
3391  return true;
3392 }
3393 
3397 void clear_worker_task(struct city *pcity, struct worker_task *ptask)
3398 {
3399  struct packet_worker_task packet;
3400 
3401  if (ptask == nullptr) {
3402  return;
3403  }
3404 
3405  worker_task_list_remove(pcity->task_reqs, ptask);
3406 
3407  packet.city_id = pcity->id;
3408  packet.tile_id = tile_index(ptask->ptile);
3409  packet.activity = ACTIVITY_LAST;
3410  packet.tgt = 0;
3411  packet.want = 0;
3412 
3413  delete ptask;
3414  ptask = nullptr;
3415 
3416  lsend_packet_worker_task(city_owner(pcity)->connections, &packet);
3417  lsend_packet_worker_task(game.glob_observers, &packet);
3418 }
3419 
3423 void clear_worker_tasks(struct city *pcity)
3424 {
3425  while (worker_task_list_size(pcity->task_reqs) > 0) {
3426  clear_worker_task(pcity, worker_task_list_get(pcity->task_reqs, 0));
3427  }
3428 }
3429 
3434 {
3435  struct packet_worker_task packet;
3436 
3437  packet.city_id = pcity->id;
3438 
3439  worker_task_list_iterate(pcity->task_reqs, ptask)
3440  {
3441  packet.tile_id = tile_index(ptask->ptile);
3442  packet.activity = ptask->act;
3443  if (ptask->tgt == nullptr) {
3444  packet.tgt = -1;
3445  } else {
3446  packet.tgt = extra_number(ptask->tgt);
3447  }
3448  packet.want = ptask->want;
3449 
3450  lsend_packet_worker_task(city_owner(pcity)->connections, &packet);
3451  lsend_packet_worker_task(game.glob_observers, &packet);
3452  }
3454 }
3455 
3459 int city_production_buy_gold_cost(const struct city *pcity)
3460 {
3461  int build = pcity->shield_stock;
3462 
3463  switch (pcity->production.kind) {
3464  case VUT_IMPROVEMENT:
3465  return impr_buy_gold_cost(pcity, pcity->production.value.building,
3466  build);
3467  case VUT_UTYPE:
3468  return utype_buy_gold_cost(pcity, pcity->production.value.utype, build);
3469  default:
3470  break;
3471  };
3472 
3473  return FC_INFINITY;
3474 }
bool is_action_enabled_unit_on_unit(const action_id wanted_action, const struct unit *actor_unit, const struct unit *target_unit)
Returns TRUE if actor_unit can do wanted_action to target_unit as far as action enablers are concerne...
Definition: actions.cpp:4007
void advisor_choose_build(struct player *pplayer, struct city *pcity)
Setup improvement building.
int adv_could_unit_move_to_tile(struct unit *punit, struct tile *dest_tile)
returns: 0 if can't move 1 if zoc_ok -1 if zoc could be ok?
Definition: advgoto.cpp:252
#define CALL_FUNC_EACH_AI(_func,...)
Definition: ai.h:393
#define CALL_PLR_AI_FUNC(_func, _player,...)
Definition: ai.h:383
bool is_land_barbarian(struct player *pplayer)
Is player a land barbarian?
Definition: barbarian.cpp:60
#define BV_CLR_ALL(bv)
Definition: bitvector.h:62
#define BV_SET(bv, bit)
Definition: bitvector.h:44
bool BV_ISSET(const BV &bv, int bit)
Definition: bitvector.h:37
#define BV_ARE_EQUAL(vec1, vec2)
Definition: bitvector.h:80
#define BV_ISSET_ANY(vec)
Definition: bitvector.h:76
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
void citizens_init(struct city *pcity)
Initialise citizens data.
Definition: citizens.cpp:26
void citizens_convert_conquest(struct city *pcity)
Convert citizens to the nationality of the one conquering the city.
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
void city_map_radius_sq_set(struct city *pcity, int radius_sq)
Returns the current squared radius of the city.
Definition: city.cpp:141
int city_production_build_shield_cost(const struct city *pcity)
Return the number of shields it takes to build current city production.
Definition: city.cpp:701
citizens player_angry_citizens(const struct player *pplayer)
Give base number of angry citizens in any city owned by pplayer.
Definition: city.cpp:2098
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 citylog_map_workers(QtMsgType level, struct city *pcity)
Display the location of the workers within the city map of pcity.
Definition: city.cpp:435
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
struct tile * city_tile(const struct city *pcity)
Return the tile location of the city.
Definition: city.cpp:1095
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
citizens player_content_citizens(const struct player *pplayer)
Give base number of content citizens in any city owned by pplayer.
Definition: city.cpp:2088
const char * city_name_get(const struct city *pcity)
Return the name of the city.
Definition: city.cpp:1077
bool city_unhappy(const struct city *pcity)
Return TRUE iff the city is unhappy.
Definition: city.cpp:1544
int city_illness_calc(const struct city *pcity, int *ill_base, int *ill_size, int *ill_trade, int *ill_pollution)
Calculate city's illness in tenth of percent:
Definition: city.cpp:2746
int city_change_production_penalty(const struct city *pcity, const struct universal *target)
Compute and optionally apply the change-production penalty for the given production change (to target...
Definition: city.cpp:1777
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
citizens city_specialists(const struct city *pcity)
Give the number of specialists in a city.
Definition: city.cpp:3228
struct city * city_list_find_name(struct city_list *This, const char *name)
Find city with given name from list.
Definition: city.cpp:1616
void city_choose_build_default(struct city *pcity)
Always tile_set_owner(ptile, pplayer) sometime before this!
Definition: city.cpp:1024
int city_map_tiles(int city_radius_sq)
Return the number of tiles for the given city radius.
Definition: city.cpp:164
bool city_exist(int id)
Check if city with given id still exist.
Definition: city.cpp:3465
bool is_city_center(const struct city *pcity, const struct tile *ptile)
Return TRUE if the city is centered at the given tile.
Definition: city.cpp:3497
bool city_can_work_tile(const struct city *pcity, const struct tile *ptile)
Returns TRUE when a tile is available to be worked, or the city itself is currently working the tile ...
Definition: city.cpp:1392
struct city * create_city_virtual(struct player *pplayer, struct tile *ptile, const char *name)
Create virtual skeleton for a city.
Definition: city.cpp:3346
citizens city_size_get(const struct city *pcity)
Get the city size.
Definition: city.cpp:1101
const char * city_production_name_translation(const struct city *pcity)
Return the extended name of the current production.
Definition: city.cpp:672
#define cities_iterate_end
Definition: city.h:493
#define city_list_iterate(citylist, pcity)
Definition: city.h:482
#define cities_iterate(pcity)
Definition: city.h:486
#define CITY_MAP_MAX_RADIUS_SQ
Definition: city.h:59
@ CITIZEN_ANGRY
Definition: city.h:243
@ CITIZEN_HAPPY
Definition: city.h:240
@ CITIZEN_CONTENT
Definition: city.h:241
@ CITIZEN_UNHAPPY
Definition: city.h:242
#define CITY_MAP_MIN_RADIUS_SQ
Definition: city.h:57
#define city_map_iterate_radius_sq(_radius_sq_min, _radius_sq_max, _x, _y)
Definition: city.h:158
#define output_type_iterate(output)
Definition: city.h:764
#define city_list_iterate_end
Definition: city.h:484
#define city_map_iterate_radius_sq_end
Definition: city.h:161
@ FEELING_LAST
Definition: city.h:257
#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_built_iterate(_pcity, _p)
Definition: city.h:752
#define city_map_iterate_without_index_end
Definition: city.h:154
#define city_built_iterate_end
Definition: city.h:759
#define city_map_iterate_without_index(_radius_sq, _x, _y)
Definition: city.h:150
#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
void city_freeze_workers_queue(struct city *pcity)
Queue pending auto_arrange_workers() for later.
Definition: citytools.cpp:141
bool city_map_update_tile_now(struct tile *ptile)
Updates the worked status of a tile immediately.
Definition: citytools.cpp:3141
static bool city_map_update_tile_direct(struct tile *ptile, bool queued)
Updates the worked status of a tile.
Definition: citytools.cpp:3103
void package_and_send_worker_tasks(struct city *pcity)
Send city worker task to owner.
Definition: citytools.cpp:3433
int city_production_buy_gold_cost(const struct city *pcity)
Return the cost (gold) to buy the current city production.
Definition: citytools.cpp:3459
void city_did_prod_change(struct city *pcity)
City did something to change production.
Definition: citytools.cpp:3068
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
bool update_dumb_city(struct player *pplayer, struct city *pcity)
Updates a players knowledge about a city.
Definition: citytools.cpp:2637
void city_freeze_workers(struct city *pcity)
Freeze the workers (citizens on tiles) for the city.
Definition: citytools.cpp:118
static void raze_city(struct city *pcity)
Called when a player conquers a city, remove buildings (not wonders and always palace) with game....
Definition: citytools.cpp:929
void city_build_free_buildings(struct city *pcity)
Give to a new city the free (initial) buildings.
Definition: citytools.cpp:1433
void city_thaw_workers_queue()
Process the frozen workers.
Definition: citytools.cpp:171
void city_illness_strike(struct city *pcity)
Give the city a plague.
Definition: citytools.cpp:2829
static bool city_workers_queue_remove(struct city *pcity)
Remove a city from the queue for later calls to auto_arrange_workers().
Definition: citytools.cpp:158
const char * city_name_suggestion(struct player *pplayer, struct tile *ptile)
Come up with a default name when a new city is about to be built.
Definition: citytools.cpp:451
void refresh_dumb_city(struct city *pcity)
Update plrtile information about the city, and send out information to the clients if it has changed.
Definition: citytools.cpp:2163
static void package_dumb_city(struct player *pplayer, struct tile *ptile, struct packet_city_short_info *packet)
This fills out a package from a player's vision_site.
Definition: citytools.cpp:2138
void create_city(struct player *pplayer, struct tile *ptile, const char *name, struct player *nationality)
Creates real city.
Definition: citytools.cpp:1512
bool send_city_suppression(bool now)
Suppress sending cities during game_load() and end_phase()
Definition: citytools.cpp:2127
static void broadcast_city_info(struct city *pcity)
Broadcast info about a city to all players who observe the tile.
Definition: citytools.cpp:2194
static bool send_city_suppressed
Definition: citytools.cpp:84
static int city_got_citywalls(const struct city *pcity)
Which wall gfx city should display?
Definition: citytools.cpp:2117
void refresh_player_cities_vision(struct player *pplayer)
Refresh the vision of all cities owned by a player, for empire-wide effects.
Definition: citytools.cpp:3277
static void build_free_small_wonders(struct player *pplayer, bv_imprs *had_small_wonders)
Create saved small wonders in random cities.
Definition: citytools.cpp:1024
bool city_map_update_radius_sq(struct city *pcity)
Updates the squared city radius.
Definition: citytools.cpp:3286
static void announce_trade_route_removal(struct city *pc1, struct city *pc2, bool source_gone)
Announce to the owners of the cities that trade route has been canceled between them.
Definition: citytools.cpp:2737
int build_points_left(struct city *pcity)
Calculate the remaining build points.
Definition: citytools.cpp:575
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
bool building_removed(struct city *pcity, const struct impr_type *pimprove, const char *reason, struct unit *destroyer)
Remove building from the city.
Definition: citytools.cpp:2861
void city_map_update_all_cities_for_player(struct player *pplayer)
Update worked map of all cities of given player.
Definition: citytools.cpp:3189
bool city_map_update_tile_frozen(struct tile *ptile)
Updates the worked status of a tile.
Definition: citytools.cpp:3133
void city_thaw_workers(struct city *pcity)
Thaw (unfreeze) the workers (citizens on tiles) for the city.
Definition: citytools.cpp:128
void remove_dumb_city(struct player *pplayer, struct tile *ptile)
Removes a dumb city.
Definition: citytools.cpp:2722
static bool is_default_city_name(const char *name, struct player *pplayer)
Checks if a city name belongs to default city names of a particular player.
Definition: citytools.cpp:300
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
bool unit_conquer_city(struct unit *punit, struct city *pcity)
Handle unit conquering a city.
Definition: citytools.cpp:1961
void clear_worker_task(struct city *pcity, struct worker_task *ptask)
Clear worker task from the city and inform owner.
Definition: citytools.cpp:3397
static void reestablish_city_trade_routes(struct city *pcity)
The following has to be called every time AFTER a city (pcity) has changed owner to update the city's...
Definition: citytools.cpp:965
void city_units_upkeep(const struct city *pcity)
Recalculate the upkeep needed for all units supported by the city.
Definition: citytools.cpp:2927
void clear_worker_tasks(struct city *pcity)
Clear all worker tasks from the city and inform owner.
Definition: citytools.cpp:3423
bool is_allowed_city_name(struct player *pplayer, const char *cityname, char *error_buf, size_t bufsz)
Checks, if a city name is allowed for a player.
Definition: citytools.cpp:356
static void transfer_unit(struct unit *punit, struct city *tocity, bool rehome, bool verbose)
Change player that owns a unit and, if appropriate, its home city, with verbose output.
Definition: citytools.cpp:589
void city_landlocked_sell_coastal_improvements(struct tile *ptile)
For each city adjacent to ptile, check all the buildings in the city.
Definition: citytools.cpp:3210
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
static struct city_list * arrange_workers_queue
Definition: citytools.cpp:81
void send_city_info_at_tile(struct player *pviewer, struct conn_list *dest, struct city *pcity, struct tile *ptile)
Send info about a city, as seen by pviewer, to dest (usually dest will be pviewer->connections).
Definition: citytools.cpp:2344
static int evaluate_city_name_priority(struct tile *ptile, const struct nation_city *pncity, int default_priority)
Returns the priority of the city name at the given position, using its own internal algorithm.
Definition: citytools.cpp:195
bool transfer_city(struct player *ptaker, struct city *pcity, int kill_outside, bool transfer_unit_verbose, bool resolve_stack, bool raze, bool build_free)
Handles all transactions in relation to transferring a city.
Definition: citytools.cpp:1084
void package_city(struct city *pcity, struct packet_city_info *packet, struct traderoute_packet_list *routes, bool dipl_invest)
Fill city info packet with information about given city.
Definition: citytools.cpp:2438
void send_player_cities(struct player *pplayer)
Send information about all his/her cities to player.
Definition: citytools.cpp:2274
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
static const char * search_for_city_name(struct tile *ptile, const struct nation_city_list *default_cities, struct player *pplayer)
Searches through a city name list (a struct nation_city array) to pick the best available city name,...
Definition: citytools.cpp:320
void reality_check_city(struct player *pplayer, struct tile *ptile)
Removes outdated (nonexistant) cities from a player.
Definition: citytools.cpp:2704
void send_all_known_cities(struct conn_list *dest)
Send to each client information about the cities it knows about.
Definition: citytools.cpp:2248
void city_refresh_vision(struct city *pcity)
Refresh the city's vision.
Definition: citytools.cpp:3261
#define LOG_BUILD_TARGET
Definition: citytools.h:21
#define traderoute_packet_list_iterate_end
Definition: citytools.h:28
#define traderoute_packet_list_iterate(ptrlist, ptr)
Definition: citytools.h:26
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
void nullify_prechange_production(struct city *pcity)
Initialize all variables containing information about production before it was changed.
Definition: cityturn.cpp:3254
void city_refresh_queue_processing()
Refresh the listed cities.
Definition: cityturn.cpp:204
bool city_reduce_size(struct city *pcity, citizens pop_loss, struct player *destroyer, const char *reason)
Reduce the city size.
Definition: cityturn.cpp:815
bool city_refresh(struct city *pcity)
Updates unit upkeeps and city internal cached data.
Definition: cityturn.cpp:147
void city_refresh_for_player(struct player *pplayer)
Called on government change or wonder completion or stuff like that – Syela.
Definition: cityturn.cpp:171
void remove_obsolete_buildings_city(struct city *pcity, bool refresh)
Automatically sells obsolete buildings from city.
Definition: cityturn.cpp:228
enum announce_type announce
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 conn_list_do_unbuffer(struct conn_list *dest)
Convenience functions to unbuffer a list of connections.
Definition: connection.cpp:319
void conn_list_do_buffer(struct conn_list *dest)
Convenience functions to buffer a list of connections.
Definition: connection.cpp:310
bool conn_is_global_observer(const struct connection *pconn)
Returns TRUE if the given connection is a global observer.
Definition: connection.cpp:683
struct connection * conn_by_user(const char *user_name)
Find connection by exact user name, from game.all_connections, case-insensitve.
Definition: connection.cpp:329
#define conn_list_iterate(connlist, pconn)
Definition: connection.h:99
#define conn_list_iterate_end
Definition: connection.h:101
int city_culture(const struct city *pcity)
Return current culture score of the city.
Definition: culture.cpp:23
int get_unit_bonus(const struct unit *punit, enum effect_type effect_type)
Returns the effect bonus at a unit.
Definition: effects.cpp:869
int get_city_output_bonus(const struct city *pcity, const struct output_type *poutput, enum effect_type effect_type)
Returns the player effect bonus of an output.
Definition: effects.cpp:800
int get_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
enum event_type event
Definition: events.cpp:68
int extra_number(const struct extra_type *pextra)
Return the extra id.
Definition: extras.cpp:132
bool is_native_tile_to_extra(const struct extra_type *pextra, const struct tile *ptile)
Is tile native to extra?
Definition: extras.cpp:559
const char * extra_name_translation(const struct extra_type *pextra)
Return the (translated) name of the extra type.
Definition: extras.cpp:165
#define extra_type_iterate(_p)
Definition: extras.h:279
#define extra_type_iterate_end
Definition: extras.h:285
#define extra_road_get(_e_)
Definition: extras.h:171
#define extra_type_by_cause_iterate_end
Definition: extras.h:307
#define extra_type_by_cause_iterate(_cause, _extra)
Definition: extras.h:299
#define MAX_NUM_BUILDING_LIST
Definition: fc_types.h:38
unsigned char citizens
Definition: fc_types.h:305
int Impr_type_id
Definition: fc_types.h:293
#define MAX_NUM_PLAYERS
Definition: fc_types.h:28
@ RPT_CERTAIN
Definition: fc_types.h:568
@ O_LAST
Definition: fc_types.h:91
#define MAX_LEN_CITYNAME
Definition: fc_types.h:62
signed short Continent_id
Definition: fc_types.h:289
#define IDENTITY_NUMBER_ZERO
Definition: fc_types.h:76
#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_name(const char *name)
Find city with given name from any player.
Definition: game.cpp:83
void game_remove_city(struct world *gworld, struct city *pcity)
Remove city from game.
Definition: game.cpp:166
struct city * game_city_by_number(int id)
Often used function to get a city pointer from a city ID.
Definition: game.cpp:103
@ CNM_PLAYER_UNIQUE
Definition: game.h:26
@ CNM_GLOBAL_UNIQUE
Definition: game.h:27
@ CNM_NO_STEALING
Definition: game.h:28
void send_game_info(struct conn_list *dest)
Send game_info packet; some server options and various stuff...
Definition: gamehand.cpp:905
bool has_handicap(const struct player *pplayer, enum handicap_type htype)
AI players may have handicaps - allowing them to cheat or preventing them from using certain algorith...
Definition: handicaps.cpp:62
@ H_PRODCHGPEN
Definition: handicaps.h:34
void idex_register_city(struct world *iworld, struct city *pcity)
Register a city into idex, with current pcity->id.
Definition: idex.cpp:61
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.
struct city * city_from_small_wonder(const struct player *pplayer, const struct impr_type *pimprove)
Get the player city with this small wonder.
bool is_improvement(const struct impr_type *pimprove)
Is this building a regular improvement?
struct impr_type * improvement_by_number(const Impr_type_id id)
Returns the improvement type for the given index/ID.
bool is_improvement_visible(const struct impr_type *pimprove)
Return TRUE if the improvement should be visible to others without spying.
Impr_type_id improvement_index(const struct impr_type *pimprove)
Return the improvement index.
int impr_buy_gold_cost(const struct city *pcity, const struct impr_type *pimprove, int shields_in_stock)
Returns the amount of gold it takes to rush this improvement.
bool is_great_wonder(const struct impr_type *pimprove)
Is this building a great wonder?
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 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 improvement_iterate_end
Definition: improvement.h:199
#define improvement_iterate(_p)
Definition: improvement.h:193
#define B_LAST
Definition: improvement.h:33
void adv_city_free(struct city *pcity)
Free advisors related city data.
Definition: infracache.cpp:510
void adv_city_update(struct city *pcity)
Update the memory allocated for AI city handling.
Definition: infracache.cpp:472
void adv_city_alloc(struct city *pcity)
Allocate advisors related city data.
Definition: infracache.cpp:498
const char * name
Definition: inputfile.cpp:118
#define fc_assert_msg(condition, message,...)
Definition: log.h:96
constexpr auto LOG_DEBUG
Definition: log.h:27
#define fc_assert_ret(condition)
Definition: log.h:112
#define fc_assert(condition)
Definition: log.h:89
#define fc_assert_ret_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 map_num_tiles()
Returns the total number of (real) positions (or tiles) on the map.
Definition: map.cpp:954
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Return real distance between two tiles.
Definition: map.cpp:599
#define adjc_iterate_end
Definition: map.h:358
#define adjc_iterate(nmap, center_tile, itr_tile)
Definition: map.h:351
#define whole_map_iterate(_map, _tile)
Definition: map.h:473
#define whole_map_iterate_end
Definition: map.h:480
void vision_clear_sight(struct vision *vision)
Clear all sight points from this vision source.
Definition: maphand.cpp:2395
void map_claim_ownership(struct tile *ptile, struct player *powner, struct tile *psource, bool claim_bases)
Claim ownership of a single tile.
Definition: maphand.cpp:2070
bool map_is_known(const struct tile *ptile, const struct player *pplayer)
Return whether the player knows the tile.
Definition: maphand.cpp:884
void destroy_extra(struct tile *ptile, struct extra_type *pextra)
Remove extra from tile.
Definition: maphand.cpp:2478
void send_tile_info(struct conn_list *dest, struct tile *ptile, bool send_unknown)
Send tile information to all the clients in dest which know and see the tile.
Definition: maphand.cpp:485
void map_show_tile(struct player *src_player, struct tile *ptile)
Shows the area to the player.
Definition: maphand.cpp:743
void give_citymap_from_player_to_player(struct city *pcity, struct player *pfrom, struct player *pdest)
Give information about tiles within city radius from player to player.
Definition: maphand.cpp:408
bool map_is_known_and_seen(const struct tile *ptile, const struct player *pplayer, enum vision_layer vlayer)
Returns whether the layer 'vlayer' of the tile 'ptile' is known and seen by the player 'pplayer'.
Definition: maphand.cpp:894
void change_playertile_site(struct player_tile *ptile, struct vision_site *new_site)
Changes site information for player tile.
Definition: maphand.cpp:1122
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 player_tile * map_get_player_tile(const struct tile *ptile, const struct player *pplayer)
Players' information of tiles is tracked so that fogged area can be kept consistent even when the cli...
Definition: maphand.cpp:1341
struct vision_site * map_get_player_site(const struct tile *ptile, const struct player *pplayer)
Returns site located at given tile from player map.
Definition: maphand.cpp:1330
struct vision_site * map_get_player_city(const struct tile *ptile, const struct player *pplayer)
Returns city located at given tile from player map.
Definition: maphand.cpp:1317
bool upgrade_city_extras(struct city *pcity, struct extra_type **gained)
Check city for extra upgrade.
Definition: maphand.cpp:214
void map_clear_border(struct tile *ptile)
Remove border for this source.
Definition: maphand.cpp:2102
void vision_change_sight(struct vision *vision, const v_radius_t radius_sq)
Change the sight points for the vision source, fogging or unfogging tiles as needed.
Definition: maphand.cpp:2383
bool is_city_channel_tile(const struct unit_class *punitclass, const struct tile *ptile, const struct tile *pexclude)
Check for a city channel.
Definition: movement.cpp:192
bool can_unit_exist_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Return TRUE iff the unit can "exist" at this location.
Definition: movement.cpp:267
bool is_native_tile(const struct unit_type *punittype, const struct tile *ptile)
This tile is native to unit.
Definition: movement.cpp:279
bool can_unit_survive_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Return TRUE iff the unit can "survive" at this location.
Definition: movement.cpp:452
bool is_native_near_tile(const struct civ_map *nmap, const struct unit_class *uclass, const struct tile *ptile)
Is there native tile adjacent to given tile.
Definition: movement.cpp:424
enum nation_city_preference nation_city_preference_revert(enum nation_city_preference prefer)
Reverts the nation city preference.
Definition: nation.cpp:347
enum nation_city_preference nation_city_terrain_preference(const struct nation_city *pncity, const struct terrain *pterrain)
Return the default nation city preference for the terrain.
Definition: nation.cpp:398
const char * nation_rule_name(const struct nation_type *pnation)
Return the (untranslated) rule name of the nation (adjective form).
Definition: nation.cpp:115
std::vector< nation_type > nations
Definition: nation.cpp:38
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
struct nation_type * nation_of_unit(const struct unit *punit)
Return the nation of the player who owns the unit.
Definition: nation.cpp:438
enum nation_city_preference nation_city_river_preference(const struct nation_city *pncity)
Return the default nation city preference for rivers.
Definition: nation.cpp:410
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
Nation_type_id nation_index(const struct nation_type *pnation)
Return the nation index.
Definition: nation.cpp:464
struct nation_type * nation_of_player(const struct player *pplayer)
Return the nation of a player.
Definition: nation.cpp:419
const char * nation_city_name(const struct nation_city *pncity)
Return the name of the default nation city.
Definition: nation.cpp:388
const struct nation_city_list * nation_cities(const struct nation_type *pnation)
Return the default cities of the nation (server only function).
Definition: nation.cpp:307
#define nation_list_iterate(nationlist, pnation)
Definition: nation.h:72
nation_city_preference
Definition: nation.h:31
@ NCP_NONE
Definition: nation.h:31
@ NCP_DISLIKE
Definition: nation.h:31
@ NCP_LIKE
Definition: nation.h:31
#define nation_city_list_iterate(citylist, pncity)
Definition: nation.h:36
#define nation_list_iterate_end
Definition: nation.h:74
#define nation_city_list_iterate_end
Definition: nation.h:38
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
int player_number(const struct player *pplayer)
Return the player index/number/id.
Definition: player.cpp:756
bool can_player_see_unit(const struct player *pplayer, const struct unit *punit)
Checks if a unit can be seen by pplayer at its current location.
Definition: player.cpp:1016
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players can attack each other.
Definition: player.cpp:1317
int player_slot_index(const struct player_slot *pslot)
Returns the index of the player slot.
Definition: player.cpp:373
int player_index(const struct player *pplayer)
Return the player index.
Definition: player.cpp:748
struct player * player_slot_get_player(const struct player_slot *pslot)
Returns the team corresponding to the slot.
Definition: player.cpp:384
bool player_can_see_city_externals(const struct player *pow_player, const struct city *target_city)
Returns TRUE iff pow_player can see externally visible features of target_city.
Definition: player.cpp:1074
const char * player_name(const struct player *pplayer)
Return the leader name of the player.
Definition: player.cpp:816
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players are allied.
Definition: player.cpp:1334
bool can_player_see_units_in_city(const struct player *pplayer, const struct city *pcity)
Return TRUE iff the player can see units in the city.
Definition: player.cpp:1045
bool can_player_see_city_internals(const struct player *pplayer, const struct city *pcity)
Return TRUE iff the player can see the city's internals.
Definition: player.cpp:1060
#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 player_slots_iterate(_pslot)
Definition: player.h:505
#define is_ai(plr)
Definition: player.h:227
#define player_slots_iterate_end
Definition: player.h:509
struct player * civil_war(struct player *pplayer)
Capturing a nation's primary capital is a devastating blow.
Definition: plrhand.cpp:2942
bool civil_war_triggered(struct player *pplayer)
civil_war_triggered: The capture of a primary capital is not a sure fire way to throw and empire into...
Definition: plrhand.cpp:2890
void maybe_make_contact(struct tile *ptile, struct player *pplayer)
Check if we make contact with anyone.
Definition: plrhand.cpp:2282
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
bool civil_war_possible(struct player *pplayer, bool conquering_city, bool honour_server_option)
Check if civil war is possible for a player.
Definition: plrhand.cpp:2843
int normal_player_count()
Return the number of non-barbarian players.
Definition: plrhand.cpp:3140
void update_capital(struct player *pplayer)
Recalculate what city is the named capital.
Definition: plrhand.cpp:590
bool nation_is_in_current_set(const struct nation_type *pnation)
Is the nation in the currently selected nationset? If not, it's not allowed to appear in the game.
Definition: plrhand.cpp:2520
#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.
bool are_universals_equal(const struct universal *psource1, const struct universal *psource2)
Return TRUE iff the two sources are equivalent.
int universal_number(const struct universal *source)
Return the universal number of the constituent.
#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
bool road_has_flag(const struct road_type *proad, enum road_flag_id flag)
Check if road provides effect.
Definition: road.cpp:367
#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.
void script_server_remove_exported_object(void *object)
Mark any, if exported, full userdata representing 'object' in the current script state as 'Nonexisten...
static struct connection connections[MAX_NUM_CONNECTIONS]
Definition: sernet.cpp:57
void flush_packets()
Attempt to flush all information in the send buffers for upto 'netwait' seconds.
Definition: sernet.cpp:187
static struct setting settings[]
Definition: settings.cpp:1338
bool is_ascii_name(const char *name)
This is used in sundry places to make sure that names of cities, players etc.
Definition: shared.cpp:223
#define CLIP(lower, current, upper)
Definition: shared.h:51
#define MIN(x, y)
Definition: shared.h:49
#define FC_INFINITY
Definition: shared.h:32
void spaceship_lost(struct player *pplayer)
Handle spaceship loss.
Definition: spacerace.cpp:422
@ SSHIP_STARTED
Definition: spaceship.h:79
@ SSHIP_LAUNCHED
Definition: spaceship.h:80
Specialist_type_id specialist_count()
Return the number of specialist_types.
Definition: specialist.cpp:63
#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
SPECPQ_PRIORITY_TYPE priority
Definition: specpq.h:76
size_t size
Definition: specvec.h:64
static int recursion[AIT_LAST]
Definition: srv_log.cpp:35
enum server_states server_state()
Return current server state.
Definition: srv_main.cpp:238
int identity_number()
Identity ids wrap at IDENTITY_NUMBER_SIZE, skipping IDENTITY_NUMBER_ZERO Setup in server_game_init()
Definition: srv_main.cpp:1993
int turn
Definition: city.h:218
Definition: city.h:291
struct worker_task_list * task_reqs
Definition: city.h:381
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 built_status built[B_LAST]
Definition: city.h:366
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 last_turns_shield_surplus
Definition: city.h:364
enum capital_type capital
Definition: city.h:298
int disbanded_shields
Definition: city.h:363
int waste[O_LAST]
Definition: city.h:325
int turn_plague
Definition: city.h:345
bv_city_options city_options
Definition: city.h:375
int city_radius_sq
Definition: city.h:346
bool was_happy
Definition: city.h:353
struct player * owner
Definition: city.h:294
int turn_founded
Definition: city.h:357
int airlift
Definition: city.h:349
int citizen_base[O_LAST]
Definition: city.h:328
int caravan_shields
Definition: city.h:362
bool did_buy
Definition: city.h:351
struct trade_route_list * routes
Definition: city.h:313
int usage[O_LAST]
Definition: city.h:329
struct worklist worklist
Definition: city.h:373
struct universal production
Definition: city.h:368
struct unit_order * orders
Definition: city.h:391
citizens * nationality
Definition: city.h:310
bool vigilant
Definition: city.h:390
int steal
Definition: city.h:383
int unhappy_penalty[O_LAST]
Definition: city.h:326
char name[MAX_LEN_CITYNAME]
Definition: city.h:292
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
int prod[O_LAST]
Definition: city.h:327
struct cm_parameter * cm_parameter
Definition: city.h:394
struct universal changed_from
Definition: city.h:371
struct unit_list * units_supported
Definition: city.h:377
int illness_trade
Definition: city.h:341
bool persistent
Definition: city.h:388
struct civ_game::@28::@32 server
struct conn_list * glob_observers
Definition: game.h:89
struct packet_ruleset_control control
Definition: game.h:74
struct conn_list * est_connections
Definition: game.h:88
struct packet_game_info info
Definition: game.h:80
int global_init_buildings[MAX_NUM_BUILDING_LIST]
Definition: game.h:99
struct civ_game::@27 rgame
enum cmdlevel access_level
Definition: connection.h:164
Nation default cities.
Definition: nation.cpp:297
int init_buildings[MAX_NUM_BUILDING_LIST]
Definition: nation.h:102
struct nation_type::@48::@50 server
enum spaceship_state state
Definition: spaceship.h:105
Definition: player.h:231
struct city_list * cities
Definition: player.h:263
int primary_capital_id
Definition: player.h:257
char username[MAX_LEN_NAME]
Definition: player.h:234
struct player::@65::@67 server
struct team * team
Definition: player.h:243
struct conn_list * connections
Definition: player.h:280
struct player_economic economic
Definition: player.h:266
struct player_spaceship spaceship
Definition: player.h:268
Definition: tile.h:42
struct unit_list * units
Definition: tile.h:50
Definition: unit.h:134
int id
Definition: unit.h:141
bool moved
Definition: unit.h:170
struct unit::@76::@79 server
struct unit * transporter
Definition: unit.h:180
enum universals_n kind
Definition: fc_types.h:740
universals_u value
Definition: fc_types.h:739
char name[MAX_LEN_NAME]
Definition: vision.h:114
bool happy
Definition: vision.h:123
bv_imprs improvements
Definition: vision.h:129
struct tile * location
Definition: vision.h:115
int identity
Definition: vision.h:118
bool walls
Definition: vision.h:122
int style
Definition: vision.h:125
bool unhappy
Definition: vision.h:124
int city_image
Definition: vision.h:126
bool occupied
Definition: vision.h:121
enum capital_type capital
Definition: vision.h:127
Definition: vision.h:83
v_radius_t radius_sq
Definition: vision.h:90
struct tile * ptile
Definition: workertask.h:16
struct civ_map map
Definition: world_object.h:21
int fc_snprintf(char *str, size_t n, const char *format,...)
See also fc_utf8_snprintf_trunc(), fc_utf8_snprintf_rep().
Definition: support.cpp:537
int fc_strcasecmp(const char *str0, const char *str1)
Compare strings like strcmp(), but ignoring case.
Definition: support.cpp:89
#define sz_strlcpy(dest, src)
Definition: support.h:140
const struct player_list * team_members(const struct team *pteam)
Returns the member list of the team.
Definition: team.cpp:427
#define A_UNSET
Definition: tech.h:41
Tech_type_id steal_a_tech(struct player *pplayer, struct player *victim, Tech_type_id preferred)
If victim has a tech which pplayer doesn't have, pplayer will get it.
Definition: techtools.cpp:1227
void send_research_info(const struct research *presearch, const struct conn_list *dest)
Send research info for 'presearch' to 'dest'.
Definition: techtools.cpp:273
bool is_terrain_near_tile(const struct tile *ptile, const struct terrain *pterrain, bool check_self)
Returns TRUE iff any adjacent tile contains the given terrain.
Definition: terrain.cpp:277
bool is_terrain_class_near_tile(const struct tile *ptile, enum terrain_class tclass)
Is there terrain of the given class near tile? (Does not check ptile itself.)
Definition: terrain.cpp:495
const char * terrain_name_translation(const struct terrain *pterrain)
Return the (translated) name of the terrain.
Definition: terrain.cpp:175
#define terrain_type_iterate(_p)
Definition: terrain.h:331
#define terrain_type_iterate_end
Definition: terrain.h:337
void tile_set_owner(struct tile *ptile, struct player *pplayer, struct tile *claimer)
Set the owner of a tile (may be nullptr).
Definition: tile.cpp:58
bool tile_extra_rm_apply(struct tile *ptile, struct extra_type *tgt)
Remove extra and adjust other extras accordingly.
Definition: tile.cpp:605
void tile_set_worked(struct tile *ptile, struct city *pcity)
Set the city/worker on the tile (may be nullptr).
Definition: tile.cpp:96
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_claimer(_tile)
Definition: tile.h:82
#define tile_index(_pt_)
Definition: tile.h:70
#define tile_worked(_tile)
Definition: tile.h:97
#define tile_terrain(_tile)
Definition: tile.h:93
#define TILE_XY(ptile)
Definition: tile.h:36
#define tile_continent(_tile)
Definition: tile.h:74
#define tile_has_extra(ptile, pextra)
Definition: tile.h:130
#define tile_owner(_tile)
Definition: tile.h:78
bool can_cities_trade(const struct city *pc1, const struct city *pc2)
Return TRUE iff the two cities are capable of trade; i.e., if a caravan from one city can enter the o...
enum trade_route_type cities_trade_route_type(const struct city *pcity1, const struct city *pcity2)
What is type of the traderoute between two cities.
Definition: traderoutes.cpp:51
Goods_type_id goods_number(const struct goods_type *pgood)
Return the goods id.
struct trade_route_settings * trade_route_settings_by_type(enum trade_route_type type)
Get trade route settings related to type.
bool can_establish_trade_route(const struct city *pc1, const struct city *pc2)
Returns TRUE iff the two cities can establish a trade route.
@ TRI_CANCEL
Definition: traderoutes.h:26
#define trade_routes_iterate_safe_end
Definition: traderoutes.h:148
#define trade_routes_iterate_end
Definition: traderoutes.h:127
#define trade_routes_iterate_safe(c, proute)
Definition: traderoutes.h:133
#define trade_routes_iterate(c, proute)
Definition: traderoutes.h:122
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 can_unit_continue_current_activity(struct unit *punit)
Check if the unit's current activity is actually legal.
Definition: unit.cpp:780
struct unit * transporter_for_unit_at(const struct unit *pcargo, const struct tile *ptile)
Find the best transporter at the given location for the unit.
Definition: unit.cpp:1789
#define unit_tile(_pu)
Definition: unit.h:371
#define unit_owner(_pu)
Definition: unit.h:370
bool unit_move_handling(struct unit *punit, struct tile *pdesttile, bool igzoc, bool move_do_not_act)
Will try to move to/attack the tile dest_x,dest_y.
Definition: unithand.cpp:4654
bool unit_activity_handling(struct unit *punit, enum unit_activity new_activity)
Handle request for changing activity.
Definition: unithand.cpp:5485
bool unit_perform_action(struct player *pplayer, const int actor_id, const int target_id, const int sub_tgt_id_incoming, const char *name, const action_id action_type, const enum action_requester requester)
Execute a request to perform an action and let the caller know if it was performed or not.
Definition: unithand.cpp:2737
void unit_change_homecity_handling(struct unit *punit, struct city *new_pcity, bool rehome)
Transfer a unit from one city (and possibly player) to another.
Definition: unithand.cpp:3239
#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
void bounce_unit(struct unit *punit, bool verbose, bounce_reason reason, int max_distance)
Move or remove a unit due to stack conflicts.
Definition: unittools.cpp:1327
void resolve_unit_stacks(struct player *pplayer, struct player *aplayer, bool verbose)
When in civil war or an alliance breaks there will potentially be units from both sides coexisting on...
Definition: unittools.cpp:1537
void send_unit_info(struct conn_list *dest, struct unit *punit)
Send the unit to the players who need the info.
Definition: unittools.cpp:2808
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
void unit_goes_out_of_sight(struct player *pplayer, const unit *punit)
Handle situation where unit goes out of player sight.
Definition: unittools.cpp:2795
void unit_list_refresh_vision(struct unit_list *punitlist)
Refresh the vision of all units in the list - see unit_refresh_vision.
Definition: unittools.cpp:4834
int utype_buy_gold_cost(const struct city *pcity, const struct unit_type *punittype, int shields_in_stock)
Returns the amount of gold it takes to rush this unit.
Definition: unittype.cpp:1193
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_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
bool utype_player_already_has_this_unique(const struct player *pplayer, const struct unit_type *putype)
Returns TRUE iff the unit type is unique and the player already has one.
Definition: unittype.cpp:1599
bool unit_can_take_over(const struct unit *punit)
Return whether the unit can take over enemy cities.
Definition: unittype.cpp:203
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Return TRUE iff units of the given type can do the specified generalized (ruleset defined) action ena...
Definition: unittype.cpp:386
static bool uclass_has_flag(const struct unit_class *punitclass, enum unit_class_flag_id flag)
Definition: unittype.h:704
#define utype_class(_t_)
Definition: unittype.h:691
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition: unittype.h:584
void vision_site_update_from_city(struct vision_site *psite, const struct city *pcity)
Returns the basic structure filled with current elements.
Definition: vision.cpp:96
struct vision * vision_new(struct player *pplayer, struct tile *ptile)
Create a new vision source.
Definition: vision.cpp:27
citizens vision_site_size_get(const struct vision_site *psite)
Get the city size.
Definition: vision.cpp:112
bool vision_reveal_tiles(struct vision *vision, bool reveal_tiles)
Sets the can_reveal_tiles flag.
Definition: vision.cpp:56
struct vision_site * vision_site_new_from_city(const struct city *pcity)
Returns the basic structure filled with initial elements.
Definition: vision.cpp:82
void vision_free(struct vision *vision)
Free the vision source.
Definition: vision.cpp:44
#define ASSERT_VISION(v)
Definition: vision.h:99
#define V_RADIUS(main_sq, invis_sq, subs_sq)
Definition: vision.h:94
#define vision_site_owner(v)
Definition: vision.h:132
short int v_radius_t[V_COUNT]
Definition: vision.h:81
#define worker_task_list_iterate(tasklist, ptask)
Definition: workertask.h:27
#define worker_task_list_iterate_end
Definition: workertask.h:29
void worklist_copy(struct worklist *dst, const struct worklist *src)
Copy contents from worklist src to worklist dst.
Definition: worklist.cpp:103