1 /***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
15 #include <fc_config.h>
23 #include "bitvector.h"
39 #include "government.h"
40 #include "improvement.h"
45 #include "requirements.h"
48 #include "specialist.h"
50 #include "traderoutes.h"
55 /* common/scriptcore */
56 #include "luascript_types.h"
59 #include "barbarian.h"
60 #include "citizenshand.h"
62 #include "gamehand.h" /* send_game_info() */
66 #include "sanitycheck.h"
68 #include "spacerace.h"
70 #include "techtools.h"
72 #include "unittools.h"
75 #include "advbuilding.h"
77 #include "autosettlers.h"
78 #include "infracache.h"
80 /* server/scripting */
81 #include "script_server.h"
84 #include "handicaps.h"
87 #include "handicaps.h"
89 #include "citytools.h"
92 /* Queue for pending auto_arrange_workers() */
93 static struct city_list
*arrange_workers_queue
= NULL
;
95 /* Suppress sending cities during game_load() and end_phase() */
96 static bool send_city_suppressed
= FALSE
;
98 static bool city_workers_queue_remove(struct city
*pcity
);
100 static void announce_trade_route_removal(struct city
*pc1
, struct city
*pc2
,
103 /****************************************************************************
104 Freeze the workers (citizens on tiles) for the city. They will not be
105 auto-arranged until unfreeze_workers is called.
109 Historically auto_arrange_workers was called every time a city changed.
110 If the city grew or shrunk, a new tile became available or was removed,
111 the function would be called. However in at least one place this breaks.
112 In some operations (like transfer_city) multiple things may change and
113 the city is not left in a sane state in between. Calling
114 auto_arrange_workers after each change means it's called with an "insane"
115 city. This can lead at best to a failed sanity check with a wasted call,
116 or at worse to a more major bug. The solution is freeze_workers and
119 Call freeze_workers to freeze the auto-arranging of citizens. So long as
120 the freeze is in place no arrangement will be done for this city. Any
121 call to auto_arrange_workers will just queue up an arrangement for later.
122 Later when thaw_workers is called, the freeze is removed and the
123 auto-arrange will be done if there is any arrangement pending.
125 Freezing may safely be done more than once.
127 It is thus always safe to call freeze and thaw around any set of city
128 actions. However this is unlikely to be needed in very many places.
129 ****************************************************************************/
130 void city_freeze_workers(struct city
*pcity
)
132 pcity
->server
.workers_frozen
++;
135 /****************************************************************************
136 Thaw (unfreeze) the workers (citizens on tiles) for the city. The workers
137 will be auto-arranged if there is an arrangement pending. See explanation
139 ****************************************************************************/
140 void city_thaw_workers(struct city
*pcity
)
142 pcity
->server
.workers_frozen
--;
143 fc_assert(pcity
->server
.workers_frozen
>= 0);
144 if (pcity
->server
.workers_frozen
== 0 && pcity
->server
.needs_arrange
) {
145 city_refresh(pcity
); /* Citizen count sanity */
146 auto_arrange_workers(pcity
);
150 /****************************************************************************
151 Queue pending auto_arrange_workers() for later.
152 ****************************************************************************/
153 void city_freeze_workers_queue(struct city
*pcity
)
155 if (NULL
== arrange_workers_queue
) {
156 arrange_workers_queue
= city_list_new();
157 } else if (city_list_find_number(arrange_workers_queue
, pcity
->id
)) {
161 city_list_prepend(arrange_workers_queue
, pcity
);
162 city_freeze_workers(pcity
);
163 pcity
->server
.needs_arrange
= TRUE
;
166 /****************************************************************************
167 Remove a city from the queue for later calls to auto_arrange_workers().
168 Reterns TRUE if the city was found in the queue.
169 ****************************************************************************/
170 static bool city_workers_queue_remove(struct city
*pcity
)
172 if (arrange_workers_queue
== NULL
) {
176 return city_list_remove(arrange_workers_queue
, pcity
);
179 /****************************************************************************
180 Process the frozen workers.
181 Call sync_cities() to send the affected cities to the clients.
182 ****************************************************************************/
183 void city_thaw_workers_queue(void)
185 if (NULL
== arrange_workers_queue
) {
189 city_list_iterate(arrange_workers_queue
, pcity
) {
190 city_thaw_workers(pcity
);
191 } city_list_iterate_end
;
193 city_list_destroy(arrange_workers_queue
);
194 arrange_workers_queue
= NULL
;
197 /****************************************************************************
198 Returns the priority of the city name at the given position, using its
199 own internal algorithm. Lower priority values are more desired, and all
200 priorities are non-negative.
202 This function takes into account game.natural_city_names, and should be
203 able to deal with any future options we want to add.
204 ****************************************************************************/
205 static int evaluate_city_name_priority(struct tile
*ptile
,
206 const struct nation_city
*pncity
,
207 int default_priority
)
209 /* Lower values mean higher priority. */
210 float priority
= (float)default_priority
;
211 enum nation_city_preference goodness
;
213 /* Increasing this value will increase the difference caused by
214 (non-)matching terrain. A matching terrain is mult_factor
215 "better" than an unlisted terrain, which is mult_factor
216 "better" than a non-matching terrain. */
217 const float mult_factor
= 1.4;
221 * If natural city names aren't being used, we just return the
222 * base value. This will have the effect of the first-listed
223 * city being used. We do this here rather than special-casing
224 * it elewhere because this localizes everything to this
225 * function, even though it's a bit inefficient.
227 if (!game
.server
.natural_city_names
) {
228 return default_priority
;
232 * Assuming we're using the natural city naming system, we use
233 * an internal alorithm to calculate the priority of each name.
234 * It's a pretty fuzzy algorithm; we basically do the following:
235 * - Change the priority scale from 0..n to 10..n+10. This means
236 * each successive city is 10% lower priority than the first.
237 * - Multiply by a semi-random number. This has the effect of
238 * decreasing rounding errors in the successive calculations,
239 * as well as introducing a smallish random effect.
240 * - Check over all the applicable terrains, and
241 * multiply or divide the priority based on whether or not
242 * the terrain matches. See comment below.
246 priority
*= 10.0 + fc_rand(5);
249 * The terrain priority in the struct nation_city will be either
250 * -1, 0, or 1. We therefore take this as-is if the terrain is
251 * present, or negate it if not.
253 * The reason we multiply as well as divide the value is so
254 * that cities that don't care what terrain they are on (which
255 * is the default) will be left in the middle of the pack. If
256 * we _only_ multiplied (or divided), then cities that had more
257 * terrain labels would have their priorities hurt (or helped).
259 goodness
= nation_city_river_preference(pncity
);
260 extra_type_by_cause_iterate(EC_ROAD
, priver
) {
261 if (tile_has_extra(ptile
, priver
)
262 && road_has_flag(extra_road_get(priver
), RF_RIVER
)) {
266 } extra_type_by_cause_iterate_end
;
268 goodness
= nation_city_preference_revert(goodness
);
273 priority
*= mult_factor
;
278 priority
/= mult_factor
;
282 terrain_type_iterate(pterrain
) {
283 /* Now we do the same for every available terrain. */
284 goodness
= nation_city_terrain_preference(pncity
, pterrain
);
285 if (!is_terrain_near_tile(ptile
, pterrain
, TRUE
)) {
286 goodness
= nation_city_preference_revert(goodness
);
290 priority
*= mult_factor
;
295 priority
/= mult_factor
;
297 } terrain_type_iterate_end
;
299 return (int) priority
;
302 /****************************************************************************
303 Checks if a city name belongs to default city names of a particular
305 ****************************************************************************/
306 static bool is_default_city_name(const char *name
, struct player
*pplayer
)
308 nation_city_list_iterate(nation_cities(nation_of_player(pplayer
)),
310 if (0 == fc_strcasecmp(name
, nation_city_name(pncity
))) {
313 } nation_city_list_iterate_end
;
317 /****************************************************************************
318 Searches through a city name list (a struct nation_city array) to pick
319 the best available city name, and returns a pointer to it. The function
320 checks if the city name is available and calls
321 evaluate_city_name_priority() to determine the priority of the city name.
322 If the list has no valid entries in it, NULL will be returned.
323 ****************************************************************************/
324 static const char *search_for_city_name(struct tile
*ptile
,
325 const struct nation_city_list
*
327 struct player
*pplayer
)
329 int choice
= 0, priority
, best_priority
= -1;
330 const char *name
, *best_name
= NULL
;
332 nation_city_list_iterate(default_cities
, pncity
) {
333 name
= nation_city_name(pncity
);
334 if (NULL
== game_city_by_name(name
)
335 && is_allowed_city_name(pplayer
, name
, NULL
, 0)) {
336 priority
= evaluate_city_name_priority(ptile
, pncity
, choice
++);
337 if (-1 == best_priority
|| priority
< best_priority
) {
338 best_priority
= priority
;
342 } nation_city_list_iterate_end
;
347 /**************************************************************************
348 Checks, if a city name is allowed for a player. If not, reports a
349 reason for rejection. There's 4 different modes:
351 1: a city name has to be unique to player
352 2: a city name has to be globally unique
353 3: a city name has to be globally unique, and players can't use names
354 that are in another player's default city names. (E.g., Swedish may not
355 call new cities or rename old cities as Helsinki, because it's in
356 Finns' default city names. Duplicated names may be used by
358 **************************************************************************/
359 bool is_allowed_city_name(struct player
*pplayer
, const char *cityname
,
360 char *error_buf
, size_t bufsz
)
362 struct connection
*pconn
= conn_by_user(pplayer
->username
);
364 /* Mode 1: A city name has to be unique for each player. */
365 if (CNM_PLAYER_UNIQUE
== game
.server
.allowed_city_names
366 && city_list_find_name(pplayer
->cities
, cityname
)) {
368 fc_snprintf(error_buf
, bufsz
, _("You already have a city called %s."),
374 /* Modes 2,3: A city name has to be globally unique. */
375 if ((CNM_GLOBAL_UNIQUE
== game
.server
.allowed_city_names
376 || CNM_NO_STEALING
== game
.server
.allowed_city_names
)
377 && game_city_by_name(cityname
)) {
379 fc_snprintf(error_buf
, bufsz
,
380 _("A city called %s already exists."), cityname
);
385 /* General rule: any name in our ruleset is allowed. */
386 if (is_default_city_name(cityname
, pplayer
)) {
391 * Mode 3: Check that the proposed city name is not in another
392 * player's default city names. Note the name will already have been
393 * allowed if it is in this player's default city names list.
395 if (CNM_NO_STEALING
== game
.server
.allowed_city_names
) {
396 struct player
*pother
= NULL
;
398 players_iterate(player2
) {
399 if (player2
!= pplayer
&& is_default_city_name(cityname
, player2
)) {
403 } players_iterate_end
;
405 if (pother
!= NULL
) {
407 fc_snprintf(error_buf
, bufsz
,
408 _("Can't use %s as a city name. It is reserved for %s."),
409 cityname
, nation_plural_for_player(pother
));
415 /* To prevent abuse, only players with HACK access (usually local
416 * connections) can use non-ascii names. Otherwise players could use
417 * confusing garbage names in multi-player games.
419 * We can even reach here for an AI player, if all the cities of the
420 * original nation are exhausted and the backup nations have non-ascii
422 if (!is_ascii_name(cityname
)
423 && (!pconn
|| pconn
->access_level
!= ALLOW_HACK
)) {
425 fc_snprintf(error_buf
, bufsz
,
426 _("%s is not a valid name. Only ASCII or "
427 "ruleset names are allowed for cities."),
437 /****************************************************************************
438 Come up with a default name when a new city is about to be built. Handle
439 running out of names etc. gracefully. Maybe we should keeptrack of which
440 names have been rejected by the player, so that we do not suggest them
442 ****************************************************************************/
443 const char *city_name_suggestion(struct player
*pplayer
, struct tile
*ptile
)
445 struct nation_type
*pnation
= nation_of_player(pplayer
);
448 log_verbose("Suggesting city name for %s at (%d,%d)",
449 player_name(pplayer
), TILE_XY(ptile
));
451 /* First try default city names. */
452 name
= search_for_city_name(ptile
, nation_cities(pnation
), pplayer
);
454 log_debug("Default city name found: %s.", name
);
458 /* Not found... Let's try a straightforward algorithm to look through
459 * nations to find a city name.
461 * We start by adding the player's nation to the queue. Then we proceed:
462 * - Pick a random nation from the queue.
463 * - If it has a valid city name, use that.
464 * - Otherwise, add all parent and child nations to the queue.
465 * - If the queue is empty, add all remaining nations to it and continue.
468 * - nation_list is a queue of nations to look through.
469 * - nations_selected tells whether each nation is in the queue already
470 * - queue_size gives the size of the queue (number of nations in it).
471 * - i is the current position in the queue.
472 * Note that nations aren't removed from the queue after they're processed.
473 * New nations are just added onto the end. */
475 struct nation_type
*nation_list
[nation_count()];
476 bool nations_selected
[nation_count()];
477 int queue_size
= 1, i
= 0, idx
;
479 memset(nations_selected
, 0, sizeof(nations_selected
));
480 nation_list
[0] = pnation
;
481 nations_selected
[nation_index(pnation
)] = TRUE
;
483 while (i
< nation_count()) {
484 for (; i
< queue_size
; i
++) {
487 /* Pick a random nation from the queue. */
488 const int which
= i
+ fc_rand(queue_size
- i
);
489 struct nation_type
*tmp
= nation_list
[i
];
491 nation_list
[i
] = nation_list
[which
];
492 nation_list
[which
] = tmp
;
494 pnation
= nation_list
[i
];
495 log_debug("Looking through %s.", nation_rule_name(pnation
));
496 name
= search_for_city_name(ptile
, nation_cities(pnation
), pplayer
);
503 /* Append the nation's civil war nations into the search tree. */
504 nation_list_iterate(pnation
->server
.civilwar_nations
, n
) {
505 idx
= nation_index(n
);
506 if (!nations_selected
[idx
]) {
507 nation_list
[queue_size
] = n
;
508 nations_selected
[idx
] = TRUE
;
510 log_debug("Child %s.", nation_rule_name(n
));
512 } nation_list_iterate_end
;
514 /* Append the nation's parent nations into the search tree. */
515 nation_list_iterate(pnation
->server
.parent_nations
, n
) {
516 idx
= nation_index(n
);
517 if (!nations_selected
[idx
]) {
518 nation_list
[queue_size
] = n
;
519 nations_selected
[idx
] = TRUE
;
521 log_debug("Parent %s.", nation_rule_name(n
));
523 } nation_list_iterate_end
;
526 /* Still not found; append all remaining nations. */
527 allowed_nations_iterate(n
) {
528 idx
= nation_index(n
);
529 if (!nations_selected
[idx
]) {
530 nation_list
[queue_size
] = n
;
531 nations_selected
[nation_index(n
)] = TRUE
;
533 log_debug("Misc nation %s.", nation_rule_name(n
));
535 } allowed_nations_iterate_end
;
539 /* Not found in rulesets, make a default name. */
541 static char tempname
[MAX_LEN_CITYNAME
];
544 log_debug("City name not found in rulesets.");
545 for (i
= 1; i
<= map_num_tiles(); i
++ ) {
546 fc_snprintf(tempname
, MAX_LEN_CITYNAME
, _("City no. %d"), i
);
547 if (NULL
== game_city_by_name(tempname
)) {
552 fc_assert_msg(FALSE
, "Failed to generate a city name.");
553 sz_strlcpy(tempname
, _("A poorly-named city"));
558 /**************************************************************************
559 calculate the remaining build points
560 **************************************************************************/
561 int build_points_left(struct city
*pcity
)
563 int cost
= impr_build_shield_cost(pcity
->production
.value
.building
);
565 return cost
- pcity
->shield_stock
;
568 /**************************************************************************
569 How many veteran levels will created unit of this type get?
570 **************************************************************************/
571 int do_make_unit_veteran(struct city
*pcity
,
572 const struct unit_type
*punittype
)
574 int levels
= get_unittype_bonus(city_owner(pcity
), pcity
->tile
, punittype
,
576 int max_levels
= utype_veteran_levels(punittype
) - 1;
578 levels
= CLIP(0, levels
, max_levels
);
583 /*********************************************************************
584 Change player that owns a unit and, if appropriate, its home city,
586 If 'rehome' is not set, only change the player which owns the unit
587 (the new owner is new_pcity's owner). Otherwise the new unit will be
588 given a homecity, even if it was homeless before.
589 ***********************************************************************/
590 static void transfer_unit(struct unit
*punit
, struct city
*tocity
,
591 bool rehome
, bool verbose
)
593 struct player
*from_player
= unit_owner(punit
);
594 struct player
*to_player
= city_owner(tocity
);
596 /* Transferring a dying GameLoss unit as part of the loot for
597 * killing it caused gna bug #23676. */
598 fc_assert_ret_msg(!punit
->server
.dying
,
599 "Tried to transfer the dying unit %d.",
602 if (from_player
== to_player
) {
603 fc_assert_ret(rehome
);
604 log_verbose("Changed homecity of %s %s to %s",
605 nation_rule_name(nation_of_player(from_player
)),
606 unit_rule_name(punit
),
607 city_name_get(tocity
));
609 notify_player(from_player
, unit_tile(punit
),
610 E_UNIT_RELOCATED
, ftc_server
,
611 _("Changed homecity of %s to %s."),
616 struct tile
*utile
= unit_tile(punit
);
617 struct city
*in_city
= tile_city(utile
);
619 if (utype_player_already_has_this_unique(to_player
,
620 unit_type_get(punit
))) {
621 /* This is a unique unit that to_player already has. A transfer would
622 * break the rule that a player only may have one unit of each unique
625 log_debug("%s already have a %s. Can't transfer from %s",
626 nation_rule_name(nation_of_player(to_player
)),
627 unit_rule_name(punit
),
628 nation_rule_name(nation_of_player(from_player
)));
630 if (utype_has_flag(unit_type_get(punit
), UTYF_GAMELOSS
)) {
631 /* Try to save game loss units. */
632 bounce_unit(punit
, verbose
);
634 /* Kill the unique unit. */
637 notify_player(from_player
, unit_tile(punit
),
638 E_UNIT_LOST_MISC
, ftc_server
,
639 /* TRANS: Americans ... Leader */
640 _("The %s already have a %s. Can't transfer yours."),
641 nation_plural_for_player(to_player
),
642 unit_tile_link(punit
));
645 wipe_unit(punit
, ULR_CITY_LOST
, NULL
);
652 log_verbose("Transferred %s in %s from %s to %s",
653 unit_rule_name(punit
), city_name_get(in_city
),
654 nation_rule_name(nation_of_player(from_player
)),
655 nation_rule_name(nation_of_player(to_player
)));
657 notify_player(from_player
, unit_tile(punit
),
658 E_UNIT_RELOCATED
, ftc_server
,
659 _("Transferred %s in %s from %s to %s."),
662 nation_plural_for_player(from_player
),
663 nation_plural_for_player(to_player
));
665 } else if (can_unit_exist_at_tile(&(wld
.map
), punit
, tocity
->tile
)) {
666 log_verbose("Transferred %s from %s to %s",
667 unit_rule_name(punit
),
668 nation_rule_name(nation_of_player(from_player
)),
669 nation_rule_name(nation_of_player(to_player
)));
671 notify_player(from_player
, unit_tile(punit
),
672 E_UNIT_RELOCATED
, ftc_server
,
673 _("Transferred %s from %s to %s."),
675 nation_plural_for_player(from_player
),
676 nation_plural_for_player(to_player
));
679 log_verbose("Could not transfer %s from %s to %s",
680 unit_rule_name(punit
),
681 nation_rule_name(nation_of_player(from_player
)),
682 nation_rule_name(nation_of_player(to_player
)));
684 notify_player(from_player
, unit_tile(punit
),
685 E_UNIT_LOST_MISC
, ftc_server
,
686 /* TRANS: Polish Destroyer ... German <city> */
687 _("%s %s lost in transfer to %s %s"),
688 nation_adjective_for_player(from_player
),
689 unit_tile_link(punit
),
690 nation_adjective_for_player(to_player
),
693 wipe_unit(punit
, ULR_CITY_LOST
, NULL
);
697 maybe_make_contact(utile
, to_player
);
699 unit_change_homecity_handling(punit
, tocity
, rehome
);
702 /*********************************************************************
703 When a city is transferred (bought, incited, disbanded, civil war):
704 Units in a transferred city are transferred to the new owner; units
705 supported by the city, but held in other cities are updated to
706 reflect those cities as their new homecity. Units supported
707 by the transferred city that are not in a city tile may be deleted.
709 - Kris Bubendorfer <Kris.Bubendorfer@MCS.VUW.AC.NZ>
711 pplayer: The player receiving the units if they are not disbanded and
713 pvictim: The owner of the city the units are transferred from.
714 units: A list of units to be transferred, typically a city's unit list.
715 pcity: Default city the units are transferred to.
716 exclude_city: The units cannot be transferred to this city.
717 kill_outside: Units outside this range are deleted. -1 means no units
719 verbose: Messages are sent to the involved parties.
720 ***********************************************************************/
721 void transfer_city_units(struct player
*pplayer
, struct player
*pvictim
,
722 struct unit_list
*units
, struct city
*pcity
,
723 struct city
*exclude_city
,
724 int kill_outside
, bool verbose
)
726 struct tile
*ptile
= pcity
->tile
;
727 int saved_id
= pcity
->id
;
728 const char *name
= city_name_get(pcity
);
730 /* Transfer enemy units in the city to the new owner.
731 * Only relevant if we are transferring to another player. */
732 if (pplayer
!= pvictim
) {
733 unit_list_iterate_safe((ptile
)->units
, vunit
) {
734 if (vunit
->server
.dying
) {
735 /* Don't transfer or bounce a dying unit. It will soon be gone
738 * Bouncing a dying unit isn't a good idea.
739 * Remaining death handling may do things meant for its current
740 * location to the new location. (Example: Stack death)
741 * bounce_unit() will wipe the unit if it can't be bounced. Wiping
742 * the dying unit isn't a good idea. The remaining death handling
743 * code will try to read from it.
745 * Transferring a dying GameLoss unit as part of the loot for
746 * killing it caused gna bug #23676. */
750 /* Don't transfer units already owned by new city-owner --wegge */
751 if (unit_owner(vunit
) == pvictim
) {
752 /* Determine whether unit was homeless. If it was, we don't give
753 * it a homecity, only change ownership.
754 * We have to search the transferred city's former units because
755 * the unit may have been made only temporarily homeless during
757 bool homeless
= (vunit
->homecity
== 0)
758 && !unit_list_search(units
, vunit
);
760 /* vunit may die during transfer_unit().
761 * unit_list_remove() is still safe using vunit pointer, as
762 * pointer is not used for dereferencing, only as value.
763 * Not sure if it would be safe to unlink first and transfer only
764 * after that. Not sure if it is correct to unlink at all in
765 * some cases, depending which list 'units' points to. */
766 transfer_unit(vunit
, pcity
, !homeless
, verbose
);
767 unit_list_remove(units
, vunit
);
768 } else if (!pplayers_allied(pplayer
, unit_owner(vunit
))) {
769 /* the owner of vunit is allied to pvictim but not to pplayer */
770 bounce_unit(vunit
, verbose
);
772 } unit_list_iterate_safe_end
;
775 if (!city_exist(saved_id
)) {
779 /* Any remaining units supported by the city are either given new home
780 cities or maybe destroyed */
781 unit_list_iterate_safe(units
, vunit
) {
782 struct city
*new_home_city
= tile_city(unit_tile(vunit
));
784 if (vunit
->server
.dying
) {
785 /* Don't transfer or destroy a dying unit. It will soon be gone
788 * Transfering a dying GameLoss unit as part of the loot for
789 * killing it caused gna bug #23676. */
793 if (new_home_city
&& new_home_city
!= exclude_city
794 && city_owner(new_home_city
) == unit_owner(vunit
)) {
795 /* unit is in another city: make that the new homecity,
796 * unless that city is actually the same city (happens if disbanding)
797 * Unit had a homecity since it was supported, so rehome. */
798 transfer_unit(vunit
, new_home_city
, TRUE
, verbose
);
799 } else if ((kill_outside
== -1
800 || real_map_distance(unit_tile(vunit
), ptile
) <= kill_outside
)
802 /* else transfer to specified city. */
803 transfer_unit(vunit
, pcity
, TRUE
, verbose
);
804 if (unit_tile(vunit
) == ptile
&& !pplayers_allied(pplayer
, pvictim
)) {
805 /* Unit is inside city being transferred, bounce it */
806 bounce_unit(vunit
, TRUE
);
809 /* The unit is lost. Call notify_player (in all other cases it is
810 * called automatically). */
811 log_verbose("Lost %s %s at (%d,%d) when %s was lost.",
812 nation_rule_name(nation_of_unit(vunit
)),
813 unit_rule_name(vunit
), TILE_XY(unit_tile(vunit
)), name
);
815 notify_player(unit_owner(vunit
), unit_tile(vunit
),
816 E_UNIT_LOST_MISC
, ftc_server
,
817 _("%s lost along with control of %s."),
818 unit_tile_link(vunit
), name
);
820 wipe_unit(vunit
, ULR_CITY_LOST
, NULL
);
822 } unit_list_iterate_safe_end
;
825 unit_list_iterate(pcity
->units_supported
, punit
) {
826 if (punit
->server
.dying
) {
827 /* Leave the dying alone. */
831 fc_assert(punit
->homecity
== pcity
->id
);
832 fc_assert(unit_owner(punit
) == pplayer
);
833 } unit_list_iterate_end
;
834 #endif /* FREECIV_DEBUG */
837 /****************************************************************************
838 Find the city closest to 'ptile'. Some restrictions can be applied:
840 'pexclcity' not this city
841 'pplayer' player to be used by 'only_known', 'only_player' and
843 'only_ocean' if set the city must be adjacent to ocean.
844 'only_continent' if set only cities on the same continent as 'ptile' are
846 'only_known' if set only cities known to 'pplayer' are considered.
847 'only_player' if set and 'pplayer' is not NULL only cities of this
849 'only_enemy' if set and 'pplayer' is not NULL only cities of players
850 which are at war with 'pplayer' are returned.
851 'pclass' if set, and 'pclass' is not NULL only cities that have
852 adjacent native terrain for that unit class are returned.
854 If no city is found NULL is returned.
855 ****************************************************************************/
856 struct city
*find_closest_city(const struct tile
*ptile
,
857 const struct city
*pexclcity
,
858 const struct player
*pplayer
,
859 bool only_ocean
, bool only_continent
,
860 bool only_known
, bool only_player
,
861 bool only_enemy
, const struct unit_class
*pclass
)
864 struct city
*best_city
= NULL
;
867 fc_assert_ret_val(ptile
!= NULL
, NULL
);
869 if (pplayer
!= NULL
&& only_player
&& only_enemy
) {
870 log_error("Non of my own cities will be at war with me!");
874 con
= tile_continent(ptile
);
876 players_iterate(aplayer
) {
877 if (pplayer
!= NULL
&& only_player
&& pplayer
!= aplayer
) {
878 /* only cities of player 'pplayer' */
882 if (pplayer
!= NULL
&& only_enemy
883 && !pplayers_at_war(pplayer
, aplayer
)) {
884 /* only cities of players at war with player 'pplayer' */
888 city_list_iterate(aplayer
->cities
, pcity
) {
891 if (pexclcity
&& pexclcity
== pcity
) {
896 city_dist
= real_map_distance(ptile
, city_tile(pcity
));
898 /* Find the closest city matching the requirements.
899 * - closer than the current best city
900 * - (if required) on the same continent
901 * - (if required) adjacent to ocean
902 * - (if required) only cities known by the player
903 * - (if required) only cities native to the class */
904 if ((best_dist
== -1 || city_dist
< best_dist
)
905 && (!only_continent
|| con
== tile_continent(pcity
->tile
))
906 && (!only_ocean
|| is_terrain_class_near_tile(city_tile(pcity
), TC_OCEAN
))
908 || (map_is_known(city_tile(pcity
), pplayer
)
909 && map_get_player_site(city_tile(pcity
), pplayer
)->identity
910 > IDENTITY_NUMBER_ZERO
))
912 || is_native_near_tile(&(wld
.map
), pclass
, city_tile(pcity
)))) {
913 best_dist
= city_dist
;
916 } city_list_iterate_end
;
917 } players_iterate_end
;
922 /**************************************************************************
923 called when a player conquers a city, remove buildings (not wonders and
924 always palace) with game.server.razechance% chance, barbarians destroy more
925 set the city's shield stock to 0
926 **************************************************************************/
927 static void raze_city(struct city
*pcity
)
929 int razechance
= game
.server
.razechance
;
931 /* land barbarians are more likely to destroy city improvements */
932 if (is_land_barbarian(city_owner(pcity
)))
935 city_built_iterate(pcity
, pimprove
) {
936 /* Small wonders should have already been removed by
937 * transfer_city() (with 100% probability). */
938 fc_assert(!is_small_wonder(pimprove
));
939 if (is_improvement(pimprove
) && (fc_rand(100) < razechance
)) {
940 city_remove_improvement(pcity
, pimprove
);
942 } city_built_iterate_end
;
944 nullify_prechange_production(pcity
);
945 pcity
->shield_stock
= 0;
948 /***************************************************************************
949 The following has to be called every time AFTER a city (pcity) has changed
950 owner to update the city's trade routes.
951 ***************************************************************************/
952 static void reestablish_city_trade_routes(struct city
*pcity
)
954 trade_routes_iterate_safe(pcity
, proute
) {
956 struct trade_route
*back
;
957 struct city
*partner
= game_city_by_number(proute
->partner
);
959 /* Remove the city's trade routes (old owner).
960 * Do not announce removal as we might restore the route immediately below */
961 back
= remove_trade_route(pcity
, proute
, FALSE
, FALSE
);
963 keep_route
= can_cities_trade(pcity
, partner
)
964 && can_establish_trade_route(pcity
, partner
);
967 enum trade_route_type type
= cities_trade_route_type(pcity
, partner
);
968 struct trade_route_settings
*settings
= trade_route_settings_by_type(type
);
970 if (settings
->cancelling
!= TRI_CANCEL
) {
975 /* Readd the city's trade route (new owner) */
977 trade_route_list_append(pcity
->routes
, proute
);
978 trade_route_list_append(partner
->routes
, back
);
983 /* Now announce the traderoute removal */
984 announce_trade_route_removal(pcity
, partner
, FALSE
);
987 /* refresh regardless; either it lost a trade route or the trade
988 * route revenue changed. */
989 city_refresh(partner
);
990 send_city_info(city_owner(partner
), partner
);
992 /* Give the new owner infos about the city which has a trade route
993 * with the transferred city. */
994 reality_check_city(city_owner(pcity
), partner
->tile
);
995 update_dumb_city(city_owner(pcity
), partner
);
996 send_city_info(city_owner(pcity
), partner
);
997 } trade_routes_iterate_safe_end
;
1000 /**************************************************************************
1001 Create saved small wonders in random cities. Usually used to save the
1002 palace when the capital was conquered. Respects the 'savepalace'
1004 **************************************************************************/
1005 static void build_free_small_wonders(struct player
*pplayer
,
1006 bv_imprs
*had_small_wonders
)
1008 int size
= city_list_size(pplayer
->cities
);
1010 if (!game
.server
.savepalace
) {
1016 /* The last city was removed or transferred to the enemy.
1017 * If the victim survives to found or acquire another city, they'll
1018 * get any savepalace initial buildings then. */
1022 improvement_iterate(pimprove
) {
1023 if (improvement_has_flag(pimprove
, IF_SAVE_SMALL_WONDER
)
1024 && BV_ISSET(*had_small_wonders
, improvement_index(pimprove
))) {
1025 /* FIXME: instead, find central city */
1026 struct city
*pnew_city
= city_list_get(pplayer
->cities
, fc_rand(size
));
1028 fc_assert_action(NULL
== city_from_small_wonder(pplayer
, pimprove
),
1031 city_add_improvement(pnew_city
, pimprove
);
1034 * send_player_cities will recalculate all cities and send them to
1037 send_player_cities(pplayer
);
1039 notify_player(pplayer
, city_tile(pnew_city
), E_IMP_BUILD
, ftc_server
,
1040 /* FIXME: should already be notified about city loss? */
1041 /* TRANS: <building> ... <city> */
1042 _("A replacement %s was built in %s."),
1043 improvement_name_translation(pimprove
),
1044 city_link(pnew_city
));
1046 * The enemy want to see the new capital in his intelligence
1049 send_city_info(NULL
, pnew_city
);
1051 } improvement_iterate_end
;
1054 /**********************************************************************
1055 Handles all transactions in relation to transferring a city.
1057 The kill_outside and transfer_unit_verbose arguments are passed to
1058 transfer_city_units(), which is called in the middle of the function.
1060 Return TRUE iff the city remains after transfering (the city may be
1061 destroyed by a script, notably with bouncing or wiping units).
1062 ***********************************************************************/
1063 bool transfer_city(struct player
*ptaker
, struct city
*pcity
,
1064 int kill_outside
, bool transfer_unit_verbose
,
1065 bool resolve_stack
, bool raze
, bool build_free
)
1067 char old_city_name
[MAX_LEN_CITYNAME
];
1068 bv_imprs had_small_wonders
;
1069 struct vision
*old_vision
, *new_vision
;
1070 struct unit_list
*old_city_units
= unit_list_new();
1071 struct player
*pgiver
= city_owner(pcity
);
1072 struct tile
*pcenter
= city_tile(pcity
);
1073 int saved_id
= pcity
->id
;
1074 bool city_remains
= TRUE
;
1075 bool had_great_wonders
= FALSE
;
1076 const citizens old_taker_content_citizens
= player_content_citizens(ptaker
);
1077 const citizens old_giver_content_citizens
= player_content_citizens(pgiver
);
1078 const citizens old_taker_angry_citizens
= player_angry_citizens(ptaker
);
1079 const citizens old_giver_angry_citizens
= player_angry_citizens(pgiver
);
1080 bool taker_had_no_cities
= (city_list_size(ptaker
->cities
) == 0);
1082 const int units_num
= unit_list_size(pcenter
->units
);
1083 bv_player
*could_see_unit
= (units_num
> 0
1084 ? fc_malloc(sizeof(*could_see_unit
)
1089 fc_assert_ret_val(pgiver
!= ptaker
, TRUE
);
1091 /* Remember what player see what unit. */
1093 unit_list_iterate(pcenter
->units
, aunit
) {
1094 BV_CLR_ALL(could_see_unit
[i
]);
1095 players_iterate(aplayer
) {
1096 if (can_player_see_unit(aplayer
, aunit
)) {
1097 BV_SET(could_see_unit
[i
], player_index(aplayer
));
1099 } players_iterate_end
;
1101 } unit_list_iterate_end
;
1102 fc_assert(i
== units_num
);
1104 /* Remove AI control of the old owner. */
1105 CALL_PLR_AI_FUNC(city_lost
, pcity
->owner
, pcity
->owner
, pcity
);
1107 /* Forget old tasks */
1108 clear_worker_tasks(pcity
);
1110 /* Activate AI control of the new owner. */
1111 CALL_PLR_AI_FUNC(city_got
, ptaker
, ptaker
, pcity
);
1113 city_freeze_workers(pcity
);
1115 unit_list_iterate(pcity
->units_supported
, punit
) {
1116 unit_list_prepend(old_city_units
, punit
);
1117 /* Mark unit to have no homecity at all.
1118 * 1. We remove unit from units_supported list here,
1119 * real_change_unit_homecity() should not attempt it.
1120 * 2. Otherwise we might delete the homecity from under the unit
1122 punit
->homecity
= 0;
1123 send_unit_info(NULL
, punit
);
1124 } unit_list_iterate_end
;
1125 unit_list_clear(pcity
->units_supported
);
1127 /* Remove all global improvement effects that this city confers (but
1128 then restore the local improvement list - we need this to restore the
1129 global effects for the new city owner) */
1130 BV_CLR_ALL(had_small_wonders
);
1131 city_built_iterate(pcity
, pimprove
) {
1132 city_remove_improvement(pcity
, pimprove
);
1134 if (is_small_wonder(pimprove
)) {
1135 BV_SET(had_small_wonders
, improvement_index(pimprove
));
1137 if (is_great_wonder(pimprove
)) {
1138 had_great_wonders
= TRUE
;
1140 /* note: internal turn here, next city_built_iterate(). */
1141 pcity
->built
[improvement_index(pimprove
)].turn
= game
.info
.turn
; /*I_ACTIVE*/
1143 } city_built_iterate_end
;
1145 give_citymap_from_player_to_player(pcity
, pgiver
, ptaker
);
1146 old_vision
= pcity
->server
.vision
;
1147 new_vision
= vision_new(ptaker
, pcenter
);
1148 pcity
->server
.vision
= new_vision
;
1149 vision_reveal_tiles(new_vision
, game
.server
.vision_reveal_tiles
);
1150 vision_change_sight(new_vision
, old_vision
->radius_sq
);
1152 ASSERT_VISION(new_vision
);
1154 sz_strlcpy(old_city_name
, city_name_get(pcity
));
1155 if (CNM_PLAYER_UNIQUE
== game
.server
.allowed_city_names
1156 && city_list_find_name(ptaker
->cities
, city_name_get(pcity
))) {
1157 sz_strlcpy(pcity
->name
,
1158 city_name_suggestion(ptaker
, pcenter
));
1159 notify_player(ptaker
, pcenter
, E_BAD_COMMAND
, ftc_server
,
1160 _("You already had a city called %s."
1161 " The city was renamed to %s."),
1166 /* Has to follow the unfog call above. */
1167 city_list_remove(pgiver
->cities
, pcity
);
1168 map_clear_border(pcenter
);
1169 /* city_thaw_workers_queue() later */
1171 pcity
->owner
= ptaker
;
1172 map_claim_ownership(pcenter
, ptaker
, pcenter
, TRUE
);
1173 city_list_prepend(ptaker
->cities
, pcity
);
1175 /* Hide/reveal units. Do it after vision have been given to taker, city
1176 * owner has been changed, and before any script could be spawned. */
1178 unit_list_iterate(pcenter
->units
, aunit
) {
1179 players_iterate(aplayer
) {
1180 if (can_player_see_unit(aplayer
, aunit
)) {
1181 if (!BV_ISSET(could_see_unit
[i
], player_index(aplayer
))
1182 && !aunit
->server
.dying
) {
1183 /* Reveal 'aunit'. */
1184 send_unit_info(aplayer
->connections
, aunit
);
1187 if (BV_ISSET(could_see_unit
[i
], player_index(aplayer
))) {
1189 unit_goes_out_of_sight(aplayer
, aunit
);
1192 } players_iterate_end
;
1194 } unit_list_iterate_end
;
1195 fc_assert(i
== units_num
);
1196 free(could_see_unit
);
1197 could_see_unit
= NULL
;
1199 transfer_city_units(ptaker
, pgiver
, old_city_units
,
1201 kill_outside
, transfer_unit_verbose
);
1202 /* The units themselves are allready freed by transfer_city_units. */
1203 unit_list_destroy(old_city_units
);
1205 if (resolve_stack
) {
1206 resolve_unit_stacks(pgiver
, ptaker
, transfer_unit_verbose
);
1209 if (! city_exist(saved_id
)) {
1210 city_remains
= FALSE
;
1214 /* Update the city's trade routes. */
1215 reestablish_city_trade_routes(pcity
);
1217 city_refresh(pcity
);
1221 * maybe_make_contact() MUST be called before city_map_update_all(),
1222 * since the diplomacy status can influence whether a tile is available.
1224 maybe_make_contact(pcenter
, ptaker
);
1227 struct extra_type
*upgradet
;
1233 if (taker_had_no_cities
) {
1234 /* If conqueror previously had no cities, we might need to give
1235 * them a palace etc */
1237 city_build_free_buildings(pcity
);
1238 } /* else caller should probably ensure palace is built */
1239 ptaker
->server
.got_first_city
= TRUE
;
1242 /* Restore any global improvement effects that this city confers */
1243 city_built_iterate(pcity
, pimprove
) {
1244 city_add_improvement(pcity
, pimprove
);
1245 } city_built_iterate_end
;
1247 /* Set production to something valid for pplayer, if not.
1248 * (previously allowed building obsolete units.) */
1249 if (!can_city_build_now(pcity
, &pcity
->production
)) {
1250 advisor_choose_build(ptaker
, pcity
);
1253 /* What wasn't obsolete for the old owner may be so now. */
1254 remove_obsolete_buildings_city(pcity
, TRUE
);
1256 new_extras
= upgrade_city_extras(pcity
, &upgradet
);
1259 const char *clink
= city_link(pcity
);
1261 notify_player(ptaker
, pcenter
, E_CITY_TRANSFER
, ftc_server
,
1262 _("The people in %s are stunned by your "
1263 "technological insight!"),
1266 if (upgradet
!= NULL
) {
1267 notify_player(ptaker
, pcenter
, E_CITY_TRANSFER
, ftc_server
,
1268 _("Workers spontaneously gather and upgrade "
1270 clink
, extra_name_translation(upgradet
));
1272 notify_player(ptaker
, pcenter
, E_CITY_TRANSFER
, ftc_server
,
1273 _("Workers spontaneously gather and upgrade "
1274 "%s infrastructure."),
1277 update_tile_knowledge(pcenter
);
1280 /* Build a new palace for free if the player lost her capital and
1281 savepalace is on. */
1282 build_free_small_wonders(pgiver
, &had_small_wonders
);
1284 /* Refresh the city's vision range, since it might be different
1285 * under the new owner. */
1286 city_refresh_vision(pcity
);
1288 /* Update the national borders, within the current vision and culture.
1289 * This could leave a border ring around the city, updated later by
1290 * map_calculate_borders() at the next turn.
1292 map_claim_border(pcenter
, ptaker
, -1);
1293 /* city_thaw_workers_queue() later */
1295 auto_arrange_workers(pcity
); /* does city_map_update_all() */
1296 city_thaw_workers(pcity
);
1297 city_thaw_workers_queue(); /* after old city has a chance to work! */
1298 city_refresh_queue_add(pcity
);
1299 /* no sanity check here as the city is not refreshed! */
1303 /* Send city with updated owner information to giver and to everyone
1304 * having shared vision pact with him/her before (s)he may
1305 * lose vision to it. When we later send info to everybody seeing the city,
1306 * (s)he may not be included. */
1307 send_city_info(NULL
, pcity
);
1310 /* Remove the sight points from the giver. */
1311 vision_clear_sight(old_vision
);
1312 vision_free(old_vision
);
1314 /* Send wonder infos. */
1315 if (had_great_wonders
) {
1316 send_game_info(NULL
);
1318 send_player_info_c(ptaker
, NULL
);
1320 /* Refresh all cities of the taker to account for possible changes due
1321 * to player wide effects. */
1322 city_list_iterate(ptaker
->cities
, acity
) {
1323 city_refresh_queue_add(acity
);
1324 } city_list_iterate_end
;
1326 /* Refresh all cities to account for possible global effects. */
1327 cities_iterate(acity
) {
1328 city_refresh_queue_add(acity
);
1329 } cities_iterate_end
;
1332 if (BV_ISSET_ANY(had_small_wonders
) || had_great_wonders
) {
1333 /* No need to send to detached connections. */
1334 send_player_info_c(pgiver
, NULL
);
1336 /* Refresh all cities of the giver to account for possible changes due
1337 * to player wide effects. */
1338 city_list_iterate(pgiver
->cities
, acity
) {
1339 city_refresh_queue_add(acity
);
1340 } city_list_iterate_end
;
1343 /* Refresh all cities in the queue. */
1344 city_refresh_queue_processing();
1345 /* After the refresh the sanity check can be done. */
1346 sanity_check_city(pcity
);
1349 /* Send information about conquered city to all players. */
1350 send_city_info(NULL
, pcity
);
1353 /* We may cross the EFT_EMPIRE_SIZE_* effects, then we will have to
1354 * refresh all cities for the player. */
1355 if (old_taker_content_citizens
!= player_content_citizens(ptaker
)
1356 || old_taker_angry_citizens
!= player_angry_citizens(ptaker
)) {
1357 city_refresh_for_player(ptaker
);
1359 if (old_giver_content_citizens
!= player_content_citizens(pgiver
)
1360 || old_giver_angry_citizens
!= player_angry_citizens(pgiver
)) {
1361 city_refresh_for_player(pgiver
);
1366 return city_remains
;
1369 /****************************************************************************
1370 Give to a new city the free (initial) buildings.
1371 Call this when a player has just acquired a city (or batch of cities,
1372 e.g. civil war) after having no cities.
1373 Doesn't check for building uniqueness! -- assumes player has no other
1374 cities which might contain unique buildings.
1375 ****************************************************************************/
1376 void city_build_free_buildings(struct city
*pcity
)
1378 struct player
*pplayer
;
1379 struct nation_type
*nation
;
1381 bool has_small_wonders
, has_great_wonders
;
1384 fc_assert_ret(NULL
!= pcity
);
1385 pplayer
= city_owner(pcity
);
1386 fc_assert_ret(NULL
!= pplayer
);
1387 nation
= nation_of_player(pplayer
);
1388 fc_assert_ret(NULL
!= nation
);
1390 /* If this isn't the first city a player has ever had, they only get
1391 * any initial buildings with the SaveSmallWonder flag, and then only
1392 * if savepalace is enabled. */
1393 first_city
= !pplayer
->server
.got_first_city
;
1395 has_small_wonders
= FALSE
;
1396 has_great_wonders
= FALSE
;
1398 /* Global free buildings. */
1399 for (i
= 0; i
< MAX_NUM_BUILDING_LIST
; i
++) {
1400 Impr_type_id n
= game
.rgame
.global_init_buildings
[i
];
1401 struct impr_type
*pimprove
;
1407 pimprove
= improvement_by_number(n
);
1408 fc_assert_action(!is_great_wonder(pimprove
), continue);
1410 (game
.server
.savepalace
1411 && improvement_has_flag(pimprove
, IF_SAVE_SMALL_WONDER
))) {
1412 city_add_improvement(pcity
, pimprove
);
1413 if (is_small_wonder(pimprove
)) {
1414 has_small_wonders
= TRUE
;
1419 /* Nation specific free buildings. */
1420 for (i
= 0; i
< MAX_NUM_BUILDING_LIST
; i
++) {
1421 Impr_type_id n
= nation
->init_buildings
[i
];
1422 struct impr_type
*pimprove
;
1428 pimprove
= improvement_by_number(n
);
1430 (game
.server
.savepalace
1431 && improvement_has_flag(pimprove
, IF_SAVE_SMALL_WONDER
))) {
1432 city_add_improvement(pcity
, pimprove
);
1433 if (is_small_wonder(pimprove
)) {
1434 has_small_wonders
= TRUE
;
1435 } else if (is_great_wonder(pimprove
)) {
1436 has_great_wonders
= TRUE
;
1441 /* Update wonder infos. */
1442 if (has_great_wonders
) {
1443 send_game_info(NULL
);
1444 /* No need to send to detached connections. */
1445 send_player_info_c(pplayer
, NULL
);
1446 } else if (has_small_wonders
) {
1447 /* No need to send to detached connections. */
1448 send_player_info_c(pplayer
, NULL
);
1452 /**************************************************************************
1454 **************************************************************************/
1455 void create_city(struct player
*pplayer
, struct tile
*ptile
,
1456 const char *name
, struct player
*nationality
)
1458 struct player
*saved_owner
= tile_owner(ptile
);
1459 struct tile
*saved_claimer
= tile_claimer(ptile
);
1460 struct city
*pwork
= tile_worked(ptile
);
1462 const citizens old_content_citizens
= player_content_citizens(pplayer
);
1463 const citizens old_angry_citizens
= player_angry_citizens(pplayer
);
1465 log_debug("create_city() %s", name
);
1467 pcity
= create_city_virtual(pplayer
, ptile
, name
);
1469 /* Remove units no more seen. Do it before city is really put into the
1471 players_iterate(other_player
) {
1472 if (can_player_see_units_in_city(other_player
, pcity
)
1473 || !map_is_known_and_seen(ptile
, other_player
, V_MAIN
)) {
1476 unit_list_iterate(ptile
->units
, punit
) {
1477 if (can_player_see_unit(other_player
, punit
)) {
1478 unit_goes_out_of_sight(other_player
, punit
);
1480 } unit_list_iterate_end
;
1481 } players_iterate_end
;
1483 adv_city_alloc(pcity
);
1485 tile_set_owner(ptile
, pplayer
, ptile
); /* temporarily */
1486 city_choose_build_default(pcity
);
1487 pcity
->id
= identity_number();
1489 fc_allocate_mutex(&game
.server
.mutexes
.city_list
);
1490 idex_register_city(&wld
, pcity
);
1491 fc_release_mutex(&game
.server
.mutexes
.city_list
);
1493 if (city_list_size(pplayer
->cities
) == 0) {
1494 /* Free initial buildings, or at least a palace if they were
1495 * previously careless enough to lose all their cities */
1496 city_build_free_buildings(pcity
);
1497 pplayer
->server
.got_first_city
= TRUE
;
1500 /* Set up citizens nationality. */
1501 citizens_init(pcity
);
1503 /* Place a worker at the is_city_center() is_free_worked().
1504 * It is possible to build a city on a tile that is already worked;
1505 * this will displace the worker on the newly-built city's tile -- Syela */
1506 tile_set_worked(ptile
, pcity
); /* instead of city_map_update_worker() */
1508 if (NULL
!= pwork
) {
1509 /* was previously worked by another city */
1510 /* Turn citizen into specialist. */
1511 pwork
->specialists
[DEFAULT_SPECIALIST
]++;
1512 /* One less citizen. Citizen sanity will be handled later in
1513 * city_thaw_workers_queue() */
1514 pwork
->server
.synced
= FALSE
;
1515 city_freeze_workers_queue(pwork
);
1518 /* Update citizens. */
1519 citizens_update(pcity
, nationality
);
1521 /* Restore the old-owner information so removal
1522 * of territory claiming bases can work relative to it. */
1523 tile_set_owner(ptile
, saved_owner
, saved_claimer
);
1525 /* Destroy any extras that don't belong in the city. */
1526 extra_type_iterate(pextra
) {
1527 if (tile_has_extra(ptile
, pextra
)
1528 && !is_native_tile_to_extra(pextra
, ptile
)) {
1529 destroy_extra(ptile
, pextra
);
1531 } extra_type_iterate_end
;
1533 /* Build any extras that the city should have. */
1534 upgrade_city_extras(pcity
, NULL
);
1536 /* Claim the ground we stand on */
1537 map_claim_ownership(ptile
, pplayer
, ptile
, TRUE
);
1539 /* Before arranging workers to show unknown land */
1540 pcity
->server
.vision
= vision_new(pplayer
, ptile
);
1541 vision_reveal_tiles(pcity
->server
.vision
, game
.server
.vision_reveal_tiles
);
1542 city_refresh_vision(pcity
);
1543 city_list_prepend(pplayer
->cities
, pcity
);
1545 /* This is dependent on the current vision, so must be done after
1546 * vision is prepared and before arranging workers. */
1547 map_claim_border(ptile
, pplayer
, -1);
1548 /* city_thaw_workers_queue() later */
1550 /* Refresh the city. First a city refresh is done (this shouldn't
1551 * send any packets to the client because the city has no supported units)
1552 * then rearrange the workers. Note that auto_arrange_workers does its
1553 * own refresh call; it is safest to do our own controlled city_refresh
1555 city_refresh(pcity
);
1556 auto_arrange_workers(pcity
);
1557 city_thaw_workers_queue(); /* after new city has a chance to work! */
1558 city_refresh_queue_processing();
1560 /* Bases destroyed earlier may have had watchtower effect. Refresh
1562 unit_list_refresh_vision(ptile
->units
);
1564 update_tile_knowledge(ptile
);
1566 if (old_content_citizens
!= player_content_citizens(pplayer
)
1567 || old_angry_citizens
!= player_angry_citizens(pplayer
)) {
1568 /* We crossed the EFT_EMPIRE_SIZE_* effects, we have to refresh all
1569 * cities for the player. */
1570 city_refresh_for_player(pplayer
);
1573 pcity
->server
.synced
= FALSE
;
1574 send_city_info(NULL
, pcity
);
1575 sync_cities(); /* Will also send pwork. */
1577 notify_player(pplayer
, ptile
, E_CITY_BUILD
, ftc_server
,
1578 _("You have founded %s."),
1580 maybe_make_contact(ptile
, city_owner(pcity
));
1582 unit_list_iterate((ptile
)->units
, punit
) {
1583 struct city
*home
= game_city_by_number(punit
->homecity
);
1585 /* Catch fortress building, transforming into ocean, etc. */
1586 if (!can_unit_continue_current_activity(punit
)) {
1587 unit_activity_handling(punit
, ACTIVITY_IDLE
);
1590 /* Update happiness (the unit may no longer cause unrest). */
1592 if (city_refresh(home
)) {
1593 /* Shouldn't happen, but better be safe than sorry. */
1594 auto_arrange_workers(home
);
1596 sanity_check_city(home
);
1597 send_city_info(city_owner(home
), home
);
1599 } unit_list_iterate_end
;
1601 sanity_check_city(pcity
);
1603 script_server_signal_emit("city_built", 1,
1604 API_TYPE_CITY
, pcity
);
1606 CALL_FUNC_EACH_AI(city_created
, pcity
);
1607 CALL_PLR_AI_FUNC(city_got
, pplayer
, pplayer
, pcity
);
1610 /**************************************************************************
1611 Remove a city from the game.
1612 **************************************************************************/
1613 void remove_city(struct city
*pcity
)
1615 struct player
*powner
= city_owner(pcity
);
1616 struct tile
*pcenter
= city_tile(pcity
);
1617 bv_imprs had_small_wonders
;
1618 struct vision
*old_vision
;
1619 int id
= pcity
->id
; /* We need this even after memory has been freed */
1620 bool had_great_wonders
= FALSE
;
1621 const citizens old_content_citizens
= player_content_citizens(powner
);
1622 const citizens old_angry_citizens
= player_angry_citizens(powner
);
1623 struct dbv tile_processed
;
1624 struct tile_list
*process_queue
;
1625 const char *ctl
= city_tile_link(pcity
);
1627 CALL_PLR_AI_FUNC(city_lost
, powner
, powner
, pcity
);
1628 CALL_FUNC_EACH_AI(city_destroyed
, pcity
);
1630 BV_CLR_ALL(had_small_wonders
);
1631 city_built_iterate(pcity
, pimprove
) {
1632 city_remove_improvement(pcity
, pimprove
);
1634 if (is_small_wonder(pimprove
)) {
1635 BV_SET(had_small_wonders
, improvement_index(pimprove
));
1636 } else if (is_great_wonder(pimprove
)) {
1637 had_great_wonders
= TRUE
;
1639 } city_built_iterate_end
;
1641 /* Rehome units in other cities */
1642 unit_list_iterate_safe(pcity
->units_supported
, punit
) {
1643 struct city
*new_home_city
= tile_city(unit_tile(punit
));
1646 && new_home_city
!= pcity
1647 && city_owner(new_home_city
) == powner
1648 && !punit
->server
.dying
) {
1649 transfer_unit(punit
, new_home_city
, TRUE
, TRUE
);
1651 } unit_list_iterate_safe_end
;
1653 /* make sure ships are not left on land when city is removed. */
1654 unit_list_iterate_safe(pcenter
->units
, punit
) {
1656 struct unit_type
*punittype
= unit_type_get(punit
);
1658 if (is_native_tile(punittype
, pcenter
)) {
1662 unit_activity_handling(punit
, ACTIVITY_IDLE
);
1664 adjc_iterate(&(wld
.map
), pcenter
, tile1
) {
1665 if (!moved
&& is_native_tile(punittype
, tile1
)) {
1666 if (adv_could_unit_move_to_tile(punit
, tile1
) == 1) {
1668 moved
= unit_move_handling(punit
, tile1
, FALSE
, TRUE
, NULL
);
1670 notify_player(unit_owner(punit
), tile1
,
1671 E_UNIT_RELOCATED
, ftc_server
,
1672 _("Moved %s out of disbanded city %s "
1673 "since it cannot stay on %s."),
1674 unit_link(punit
), ctl
,
1675 terrain_name_translation(tile_terrain(pcenter
)));
1682 notify_player(unit_owner(punit
), unit_tile(punit
),
1683 E_UNIT_LOST_MISC
, ftc_server
,
1684 _("When %s was disbanded your %s could not "
1685 "get out, and it was therefore lost."),
1687 unit_tile_link(punit
));
1688 wipe_unit(punit
, ULR_CITY_LOST
, NULL
);
1690 } unit_list_iterate_safe_end
;
1692 process_queue
= tile_list_new();
1693 dbv_init(&tile_processed
, map_num_tiles());
1694 for (tile_list_append(process_queue
, pcenter
); tile_list_size(process_queue
) > 0;) {
1695 struct tile
*ptile
= tile_list_front(process_queue
);
1696 tile_list_pop_front(process_queue
);
1697 dbv_set(&tile_processed
, tile_index(ptile
));
1698 adjc_iterate(&(wld
.map
), ptile
, piter
) {
1699 struct city
*other_city
;
1701 if (dbv_isset(&tile_processed
, tile_index(piter
))) {
1704 other_city
= tile_city(piter
);
1705 if (other_city
!= NULL
) {
1706 /* Adjacent tile has a city that may have been part of same channel */
1707 dbv_set(&tile_processed
, tile_index(piter
));
1708 tile_list_append(process_queue
, piter
);
1709 unit_list_iterate_safe(piter
->units
, punit
) {
1710 struct unit_class
*pclass
= utype_class(punit
->utype
);
1712 if (!uclass_has_flag(pclass
, UCF_BUILD_ANYWHERE
)
1713 && !is_native_tile(punit
->utype
, piter
)
1714 && !is_city_channel_tile(pclass
, piter
, pcenter
)) {
1715 notify_player(unit_owner(punit
), unit_tile(punit
),
1716 E_UNIT_LOST_MISC
, ftc_server
,
1717 _("When %s was disbanded your %s in %s was trapped, "
1718 "and it was therefore lost."),
1720 unit_tile_link(punit
),
1721 city_link(other_city
));
1722 wipe_unit(punit
, ULR_CITY_LOST
, NULL
);
1724 } unit_list_iterate_safe_end
;
1726 dbv_set(&tile_processed
, tile_index(piter
));
1731 dbv_free(&tile_processed
);
1732 tile_list_destroy(process_queue
);
1734 if (!city_exist(id
)) {
1735 /* Wiping trapped units caused city to disappear. */
1739 /* Any remaining supported units are destroyed */
1740 unit_list_iterate_safe(pcity
->units_supported
, punit
) {
1741 wipe_unit(punit
, ULR_CITY_LOST
, NULL
);
1742 } unit_list_iterate_safe_end
;
1744 if (!city_exist(id
)) {
1745 /* Wiping supported units caused city to disappear. */
1749 trade_routes_iterate_safe(pcity
, proute
) {
1750 struct trade_route
*pback
= remove_trade_route(pcity
, proute
,
1755 } trade_routes_iterate_safe_end
;
1757 map_clear_border(pcenter
);
1758 city_workers_queue_remove(pcity
);
1759 city_thaw_workers_queue();
1760 city_refresh_queue_processing();
1762 /* idex_unregister_city() is called in game_remove_city() below */
1764 /* identity_number_release(pcity->id) is *NOT* done! The cities may
1765 still be alive in the client, or in the player map. The number of
1766 removed cities is small, so the loss is acceptable.
1769 old_vision
= pcity
->server
.vision
;
1770 pcity
->server
.vision
= NULL
;
1771 script_server_remove_exported_object(pcity
);
1772 adv_city_free(pcity
);
1774 /* Remove city from the map. */
1775 tile_set_worked(pcenter
, NULL
);
1778 players_iterate(other_player
) {
1779 if (can_player_see_units_in_city(other_player
, pcity
)
1780 || !map_is_known_and_seen(pcenter
, other_player
, V_MAIN
)) {
1783 unit_list_iterate(pcenter
->units
, punit
) {
1784 if (can_player_see_unit(other_player
, punit
)) {
1785 send_unit_info(other_player
->connections
, punit
);
1787 } unit_list_iterate_end
;
1788 } players_iterate_end
;
1790 fc_allocate_mutex(&game
.server
.mutexes
.city_list
);
1791 game_remove_city(&wld
, pcity
);
1792 fc_release_mutex(&game
.server
.mutexes
.city_list
);
1794 /* Remove any extras that were only there because the city was there. */
1795 extra_type_iterate(pextra
) {
1796 if (tile_has_extra(pcenter
, pextra
)
1797 && !is_native_tile_to_extra(pextra
, pcenter
)) {
1798 tile_extra_rm_apply(pcenter
, pextra
);
1800 } extra_type_iterate_end
;
1802 players_iterate(other_player
) {
1803 if (map_is_known_and_seen(pcenter
, other_player
, V_MAIN
)) {
1804 reality_check_city(other_player
, pcenter
);
1806 } players_iterate_end
;
1808 conn_list_iterate(game
.est_connections
, pconn
) {
1809 if (NULL
== pconn
->playing
&& pconn
->observer
) {
1810 /* For detached observers we have to send a specific packet. This is
1811 * a hack necessitated by the private map that exists for players but
1813 dsend_packet_city_remove(pconn
, id
);
1815 } conn_list_iterate_end
;
1817 vision_clear_sight(old_vision
);
1818 vision_free(old_vision
);
1820 /* Infrastructures may have changed. */
1821 send_tile_info(NULL
, pcenter
, FALSE
);
1823 /* Build a new palace for free if the player lost her capital and
1824 savepalace is on. */
1825 build_free_small_wonders(powner
, &had_small_wonders
);
1827 /* Update wonder infos. */
1828 if (had_great_wonders
) {
1829 send_game_info(NULL
);
1830 /* No need to send to detached connections. */
1831 send_player_info_c(powner
, NULL
);
1832 } else if (BV_ISSET_ANY(had_small_wonders
)) {
1833 /* No need to send to detached connections. */
1834 send_player_info_c(powner
, NULL
);
1837 if (old_content_citizens
!= player_content_citizens(powner
)
1838 || old_angry_citizens
!= player_angry_citizens(powner
)) {
1839 /* We crossed the EFT_EMPIRE_SIZE_* effects, we have to refresh all
1840 * cities for the player. */
1841 city_refresh_for_player(powner
);
1847 /**************************************************************************
1848 Handle unit conquering a city.
1849 - Can't conquer a city when not at war. (Enters cities peacefully
1850 during peace. At the moment this can happen to domestic, allied and
1852 - A unit can't conquer a city if it is owned by the animal barbarian.
1853 - A unit can't conquer a city if its unit class is missing the
1854 "CanOccupyCity" unit class flag.
1855 - A unit can't conquer a city if its unit type has the "NonMil" unit
1857 - Transported units trying to conquer a city should be unloaded before
1858 this function is called.
1860 Returns TRUE iff action could be done, FALSE if it couldn't. Even if
1861 this returns TRUE, unit may have died during the action.
1862 **************************************************************************/
1863 bool unit_conquer_city(struct unit
*punit
, struct city
*pcity
)
1865 bool try_civil_war
= FALSE
;
1868 struct player
*pplayer
= unit_owner(punit
);
1869 struct player
*cplayer
= city_owner(pcity
);
1871 /* If not at war, may peacefully enter city. */
1872 fc_assert_ret_val_msg(pplayers_at_war(pplayer
, cplayer
), FALSE
,
1873 "Can't conquer city during peace.");
1875 /* If we cannot occupy the city, this unit entering will not trigger the
1877 fc_assert_ret_val_msg(unit_can_take_over(punit
)
1878 || utype_can_do_action(unit_type_get(punit
),
1880 FALSE
, "Bad unit for city occupation.");
1882 /* A transported unit trying to conquer a city should already have been
1884 fc_assert_ret_val_msg(punit
->transporter
== NULL
, FALSE
,
1885 "Can't conquer city while transported.");
1887 /* Okay, we're at war - invader captures/destroys city... */
1889 /* If a capital is captured, then spark off a civil war
1891 Also check spaceships --dwp
1894 if (is_capital(pcity
)
1895 && (cplayer
->spaceship
.state
== SSHIP_STARTED
1896 || cplayer
->spaceship
.state
== SSHIP_LAUNCHED
)) {
1897 spaceship_lost(cplayer
);
1900 if (is_capital(pcity
)
1901 && civil_war_possible(cplayer
, TRUE
, TRUE
)
1902 && normal_player_count() < MAX_NUM_PLAYERS
1903 && civil_war_triggered(cplayer
)) {
1904 try_civil_war
= TRUE
;
1908 * We later remove a citizen. Lets check if we can save this since
1909 * the city will be destroyed.
1911 if (city_size_get(pcity
) <= 1) {
1912 int saved_id
= pcity
->id
;
1914 notify_player(pplayer
, city_tile(pcity
), E_UNIT_WIN_ATT
, ftc_server
,
1915 _("You destroy %s completely."),
1916 city_tile_link(pcity
));
1917 notify_player(cplayer
, city_tile(pcity
), E_CITY_LOST
, ftc_server
,
1918 _("%s has been destroyed by %s."),
1919 city_tile_link(pcity
), player_name(pplayer
));
1920 script_server_signal_emit("city_destroyed", 3,
1921 API_TYPE_CITY
, pcity
,
1922 API_TYPE_PLAYER
, cplayer
,
1923 API_TYPE_PLAYER
, pplayer
);
1925 /* We cant't be sure of city existence after running some script */
1926 if (city_exist(saved_id
)) {
1930 if (try_civil_war
) {
1931 (void) civil_war(cplayer
);
1936 coins
= cplayer
->economic
.gold
;
1938 fc_rand((coins
/ 20) + 1)
1939 + (coins
* (city_size_get(pcity
))) / 200);
1940 pplayer
->economic
.gold
+= coins
;
1941 cplayer
->economic
.gold
-= coins
;
1942 send_player_info_c(pplayer
, pplayer
->connections
);
1943 send_player_info_c(cplayer
, cplayer
->connections
);
1944 if (pcity
->original
!= pplayer
) {
1946 notify_player(pplayer
, city_tile(pcity
), E_UNIT_WIN_ATT
, ftc_server
,
1947 PL_("You conquer %s; your lootings accumulate"
1949 "You conquer %s; your lootings accumulate"
1950 " to %d gold!", coins
),
1953 notify_player(cplayer
, city_tile(pcity
), E_CITY_LOST
, ftc_server
,
1954 PL_("%s conquered %s and looted %d gold"
1956 "%s conquered %s and looted %d gold"
1957 " from the city.", coins
),
1958 player_name(pplayer
),
1962 notify_player(pplayer
, city_tile(pcity
), E_UNIT_WIN_ATT
, ftc_server
,
1963 _("You conquer %s."),
1965 notify_player(cplayer
, city_tile(pcity
), E_CITY_LOST
, ftc_server
,
1966 _("%s conquered %s."),
1967 player_name(pplayer
),
1972 notify_player(pplayer
, city_tile(pcity
), E_UNIT_WIN_ATT
, ftc_server
,
1973 PL_("You have liberated %s!"
1974 " Lootings accumulate to %d gold.",
1975 "You have liberated %s!"
1976 " Lootings accumulate to %d gold.", coins
),
1979 notify_player(cplayer
, city_tile(pcity
), E_CITY_LOST
, ftc_server
,
1980 PL_("%s liberated %s and looted %d gold"
1982 "%s liberated %s and looted %d gold"
1983 " from the city.", coins
),
1984 player_name(pplayer
),
1988 notify_player(pplayer
, city_tile(pcity
), E_UNIT_WIN_ATT
, ftc_server
,
1989 _("You have liberated %s!"),
1991 notify_player(cplayer
, city_tile(pcity
), E_CITY_LOST
, ftc_server
,
1992 _("%s liberated %s."),
1993 player_name(pplayer
),
1998 steal_a_tech(pplayer
, cplayer
, A_UNSET
);
2000 /* We transfer the city first so that it is in a consistent state when
2001 * the size is reduced. */
2002 /* FIXME: maybe it should be a ruleset option whether barbarians get
2003 * free buildings such as palaces? */
2004 city_remains
= transfer_city(pplayer
, pcity
, 0, TRUE
, TRUE
, TRUE
,
2005 !is_barbarian(pplayer
));
2008 /* reduce size should not destroy this city */
2009 fc_assert(city_size_get(pcity
) > 1);
2010 city_reduce_size(pcity
, 1, pplayer
, "conquest");
2013 if (try_civil_war
) {
2014 (void) civil_war(cplayer
);
2018 script_server_signal_emit("city_transferred", 4,
2019 API_TYPE_CITY
, pcity
,
2020 API_TYPE_PLAYER
, cplayer
,
2021 API_TYPE_PLAYER
, pplayer
,
2022 API_TYPE_STRING
, "conquest");
2023 script_server_signal_emit("city_lost", 3,
2024 API_TYPE_CITY
, pcity
,
2025 API_TYPE_PLAYER
, cplayer
,
2026 API_TYPE_PLAYER
, pplayer
);
2032 /**************************************************************************
2033 Which wall gfx city should display?
2034 **************************************************************************/
2035 static int city_got_citywalls(const struct city
*pcity
)
2037 int walls
= get_city_bonus(pcity
, EFT_VISIBLE_WALLS
);
2039 return walls
> 0 ? walls
: 0;
2042 /**************************************************************************
2043 Suppress sending cities during game_load() and end_phase()
2044 **************************************************************************/
2045 bool send_city_suppression(bool now
)
2047 bool formerly
= send_city_suppressed
;
2049 send_city_suppressed
= now
;
2053 /**************************************************************************
2054 This fills out a package from a player's vision_site.
2055 **************************************************************************/
2056 static void package_dumb_city(struct player
* pplayer
, struct tile
*ptile
,
2057 struct packet_city_short_info
*packet
)
2059 struct vision_site
*pdcity
= map_get_player_city(ptile
, pplayer
);
2061 packet
->id
= pdcity
->identity
;
2062 packet
->owner
= player_number(vision_site_owner(pdcity
));
2064 packet
->tile
= tile_index(ptile
);
2065 sz_strlcpy(packet
->name
, pdcity
->name
);
2067 packet
->size
= vision_site_size_get(pdcity
);
2069 packet
->occupied
= pdcity
->occupied
;
2070 packet
->walls
= pdcity
->walls
;
2071 packet
->style
= pdcity
->style
;
2072 packet
->city_image
= pdcity
->city_image
;
2074 packet
->happy
= pdcity
->happy
;
2075 packet
->unhappy
= pdcity
->unhappy
;
2077 packet
->improvements
= pdcity
->improvements
;
2080 /**************************************************************************
2081 Update plrtile information about the city, and send out information to
2082 the clients if it has changed.
2083 **************************************************************************/
2084 void refresh_dumb_city(struct city
*pcity
)
2086 players_iterate(pplayer
) {
2087 if (player_can_see_city_externals(pplayer
, pcity
)) {
2088 if (update_dumb_city(pplayer
, pcity
)) {
2089 struct packet_city_short_info packet
;
2091 if (city_owner(pcity
) != pplayer
) {
2092 /* Don't send the short_city information to someone who can see the
2093 * city's internals. Doing so would really confuse the client. */
2094 package_dumb_city(pplayer
, pcity
->tile
, &packet
);
2095 lsend_packet_city_short_info(pplayer
->connections
, &packet
);
2099 } players_iterate_end
;
2101 /* Don't send to non-player observers since they don't have 'dumb city'
2105 /**************************************************************************
2106 Broadcast info about a city to all players who observe the tile.
2107 If the player can see the city we update the city info first.
2108 If not we just use the info from the players private map.
2109 See also comments to send_city_info_at_tile().
2110 (Split off from send_city_info_at_tile() because that was getting
2111 too difficult for me to understand... --dwp)
2112 **************************************************************************/
2113 static void broadcast_city_info(struct city
*pcity
)
2115 struct packet_city_info packet
;
2116 struct packet_web_city_info_addition web_packet
;
2117 struct packet_city_short_info sc_pack
;
2118 struct player
*powner
= city_owner(pcity
);
2119 struct traderoute_packet_list
*routes
= traderoute_packet_list_new();
2121 /* Send to everyone who can see the city. */
2122 package_city(pcity
, &packet
, &web_packet
, routes
, FALSE
);
2123 players_iterate(pplayer
) {
2124 if (can_player_see_city_internals(pplayer
, pcity
)) {
2125 if (!send_city_suppressed
|| pplayer
!= powner
) {
2126 update_dumb_city(powner
, pcity
);
2127 lsend_packet_city_info(powner
->connections
, &packet
, FALSE
);
2128 web_lsend_packet(city_info_addition
, powner
->connections
, &web_packet
, FALSE
);
2129 traderoute_packet_list_iterate(routes
, route_packet
) {
2130 lsend_packet_traderoute_info(powner
->connections
, route_packet
);
2131 } traderoute_packet_list_iterate_end
;
2134 if (player_can_see_city_externals(pplayer
, pcity
)) {
2135 reality_check_city(pplayer
, pcity
->tile
);
2136 update_dumb_city(pplayer
, pcity
);
2137 package_dumb_city(pplayer
, pcity
->tile
, &sc_pack
);
2138 lsend_packet_city_short_info(pplayer
->connections
, &sc_pack
);
2141 } players_iterate_end
;
2143 /* Send to global observers. */
2144 conn_list_iterate(game
.est_connections
, pconn
) {
2145 if (conn_is_global_observer(pconn
)) {
2146 send_packet_city_info(pconn
, &packet
, FALSE
);
2147 web_send_packet(city_info_addition
, pconn
, &web_packet
, FALSE
);
2149 } conn_list_iterate_end
;
2151 traderoute_packet_list_iterate(routes
, route_packet
) {
2152 FC_FREE(route_packet
);
2153 } traderoute_packet_list_iterate_end
;
2154 traderoute_packet_list_destroy(routes
);
2157 /**************************************************************************
2158 Send to each client information about the cities it knows about.
2159 dest may not be NULL
2160 **************************************************************************/
2161 void send_all_known_cities(struct conn_list
*dest
)
2163 conn_list_do_buffer(dest
);
2164 conn_list_iterate(dest
, pconn
) {
2165 struct player
*pplayer
= pconn
->playing
;
2167 if (!pplayer
&& !pconn
->observer
) {
2170 whole_map_iterate(&(wld
.map
), ptile
) {
2171 if (!pplayer
|| NULL
!= map_get_player_site(ptile
, pplayer
)) {
2172 send_city_info_at_tile(pplayer
, pconn
->self
, NULL
, ptile
);
2174 } whole_map_iterate_end
;
2176 conn_list_iterate_end
;
2177 conn_list_do_unbuffer(dest
);
2181 /**************************************************************************
2182 Send information about all his/her cities to player
2183 **************************************************************************/
2184 void send_player_cities(struct player
*pplayer
)
2186 city_list_iterate(pplayer
->cities
, pcity
) {
2187 if (city_refresh(pcity
)) {
2188 log_error("%s radius changed while sending to player.",
2189 city_name_get(pcity
));
2191 /* Make sure that no workers in illegal position outside radius. */
2192 auto_arrange_workers(pcity
);
2194 send_city_info(pplayer
, pcity
);
2196 city_list_iterate_end
;
2199 /**************************************************************************
2200 A wrapper, accessing either broadcast_city_info() (dest == NULL),
2201 or a convenience case of send_city_info_at_tile().
2202 Must specify non-NULL pcity.
2203 **************************************************************************/
2204 void send_city_info(struct player
*dest
, struct city
*pcity
)
2206 struct player
*powner
= city_owner(pcity
);
2208 if (S_S_RUNNING
!= server_state() && S_S_OVER
!= server_state()) {
2212 if (dest
== powner
&& send_city_suppressed
) {
2216 if (!dest
|| dest
== powner
) {
2217 pcity
->server
.synced
= TRUE
;
2221 broadcast_city_info(pcity
);
2223 send_city_info_at_tile(dest
, dest
->connections
, pcity
, pcity
->tile
);
2226 if (game
.info
.team_pooled_research
2227 && player_list_size(team_members(powner
->team
)) > 1) {
2228 /* We want to send the new total bulbs production of the team. */
2229 send_research_info(research_get(powner
), NULL
);
2233 /**************************************************************************
2234 Send info about a city, as seen by pviewer, to dest (usually dest will
2235 be pviewer->connections). If pplayer can see the city we update the city
2236 info first. If not we just use the info from the players private map.
2238 If (pviewer == NULL) this is for observers, who see everything (?)
2239 For this function dest may not be NULL. See send_city_info() and
2240 broadcast_city_info().
2242 If pcity is non-NULL it should be same as tile_city(x,y); if pcity
2243 is NULL, this function calls tile_city(x,y) (it is ok if this
2246 Sometimes a player's map contain a city that doesn't actually exist. Use
2247 reality_check_city(pplayer, ptile) to update that. Remember to NOT send info
2248 about a city to a player who thinks the tile contains another city. If you
2249 want to update the clients info of the tile you must use
2250 reality_check_city(pplayer, ptile) first. This is generally taken care of
2251 automatically when a tile becomes visible.
2252 **************************************************************************/
2253 void send_city_info_at_tile(struct player
*pviewer
, struct conn_list
*dest
,
2254 struct city
*pcity
, struct tile
*ptile
)
2256 struct packet_city_info packet
;
2257 struct packet_web_city_info_addition web_packet
;
2258 struct packet_city_short_info sc_pack
;
2259 struct player
*powner
= NULL
;
2260 struct traderoute_packet_list
*routes
= NULL
;
2263 pcity
= tile_city(ptile
);
2266 powner
= city_owner(pcity
);
2268 if (powner
&& powner
== pviewer
) {
2269 /* send info to owner */
2270 /* This case implies powner non-NULL which means pcity non-NULL */
2271 if (!send_city_suppressed
) {
2272 routes
= traderoute_packet_list_new();
2274 /* send all info to the owner */
2275 update_dumb_city(powner
, pcity
);
2276 package_city(pcity
, &packet
, &web_packet
, routes
, FALSE
);
2277 lsend_packet_city_info(dest
, &packet
, FALSE
);
2278 web_lsend_packet(city_info_addition
, dest
, &web_packet
, FALSE
);
2279 traderoute_packet_list_iterate(routes
, route_packet
) {
2280 lsend_packet_traderoute_info(dest
, route_packet
);
2281 } traderoute_packet_list_iterate_end
;
2282 if (dest
== powner
->connections
) {
2283 /* HACK: send also a copy to global observers. */
2284 conn_list_iterate(game
.est_connections
, pconn
) {
2285 if (conn_is_global_observer(pconn
)) {
2286 send_packet_city_info(pconn
, &packet
, FALSE
);
2287 traderoute_packet_list_iterate(routes
, route_packet
) {
2288 send_packet_traderoute_info(pconn
, route_packet
);
2289 } traderoute_packet_list_iterate_end
;
2291 } conn_list_iterate_end
;
2295 /* send info to non-owner */
2296 if (!pviewer
) { /* observer */
2298 routes
= traderoute_packet_list_new();
2300 package_city(pcity
, &packet
, &web_packet
, routes
, FALSE
); /* should be dumb_city info? */
2301 lsend_packet_city_info(dest
, &packet
, FALSE
);
2302 web_lsend_packet(city_info_addition
, dest
, &web_packet
, FALSE
);
2303 traderoute_packet_list_iterate(routes
, route_packet
) {
2304 lsend_packet_traderoute_info(dest
, route_packet
);
2305 } traderoute_packet_list_iterate_end
;
2308 if (!map_is_known(ptile
, pviewer
)) {
2309 /* Without the conditional we'd have an infinite loop here. */
2310 map_show_tile(pviewer
, ptile
);
2312 if (map_is_known_and_seen(ptile
, pviewer
, V_MAIN
)) {
2313 if (pcity
) { /* it's there and we see it; update and send */
2314 update_dumb_city(pviewer
, pcity
);
2315 package_dumb_city(pviewer
, ptile
, &sc_pack
);
2316 lsend_packet_city_short_info(dest
, &sc_pack
);
2318 } else { /* not seen; send old info */
2319 if (NULL
!= map_get_player_site(ptile
, pviewer
)) {
2320 package_dumb_city(pviewer
, ptile
, &sc_pack
);
2321 lsend_packet_city_short_info(dest
, &sc_pack
);
2327 if (routes
!= NULL
) {
2328 traderoute_packet_list_iterate(routes
, route_packet
) {
2329 FC_FREE(route_packet
);
2330 } traderoute_packet_list_iterate_end
;
2331 traderoute_packet_list_destroy(routes
);
2335 /**************************************************************************
2336 Fill city info packet with information about given city.
2337 **************************************************************************/
2338 void package_city(struct city
*pcity
, struct packet_city_info
*packet
,
2339 struct packet_web_city_info_addition
*web_packet
,
2340 struct traderoute_packet_list
*routes
,
2346 packet
->id
= pcity
->id
;
2347 packet
->owner
= player_number(city_owner(pcity
));
2348 packet
->tile
= tile_index(city_tile(pcity
));
2349 sz_strlcpy(packet
->name
, city_name_get(pcity
));
2351 packet
->size
= city_size_get(pcity
);
2352 for (i
= 0; i
< FEELING_LAST
; i
++) {
2353 packet
->ppl_happy
[i
] = pcity
->feel
[CITIZEN_HAPPY
][i
];
2354 packet
->ppl_content
[i
] = pcity
->feel
[CITIZEN_CONTENT
][i
];
2355 packet
->ppl_unhappy
[i
] = pcity
->feel
[CITIZEN_UNHAPPY
][i
];
2356 packet
->ppl_angry
[i
] = pcity
->feel
[CITIZEN_ANGRY
][i
];
2358 ppl
+= packet
->ppl_happy
[i
];
2359 ppl
+= packet
->ppl_content
[i
];
2360 ppl
+= packet
->ppl_unhappy
[i
];
2361 ppl
+= packet
->ppl_angry
[i
];
2364 /* The number of data in specialists[] array */
2365 packet
->specialists_size
= specialist_count();
2366 specialist_type_iterate(sp
) {
2367 packet
->specialists
[sp
] = pcity
->specialists
[sp
];
2368 ppl
+= packet
->specialists
[sp
];
2369 } specialist_type_iterate_end
;
2371 /* The nationality of the citizens. */
2372 packet
->nationalities_count
= 0;
2373 if (game
.info
.citizen_nationality
) {
2376 player_slots_iterate(pslot
) {
2377 citizens nationality
= citizens_nation_get(pcity
, pslot
);
2378 if (nationality
!= 0) {
2379 /* This player should exist! */
2380 fc_assert(player_slot_get_player(pslot
) != NULL
);
2382 packet
->nation_id
[packet
->nationalities_count
]
2383 = player_slot_index(pslot
);
2384 packet
->nation_citizens
[packet
->nationalities_count
]
2386 packet
->nationalities_count
++;
2390 } player_slots_iterate_end
;
2392 fc_assert(cit
== packet
->size
);
2395 packet
->history
= pcity
->history
;
2396 packet
->culture
= city_culture(pcity
);
2398 if (packet
->size
!= ppl
) {
2399 static bool recursion
= FALSE
;
2402 /* Recursion didn't help. Do not enter infinite recursive loop.
2403 * Package city as it is. */
2404 log_error("Failed to fix inconsistent city size.");
2407 /* Note: If you get this error and try to debug the cause, you may find
2408 * using sanity_check_feelings() in some key points useful. */
2409 /* Have this as an fc_assert() first, so one can use '-F' to caught these in
2411 fc_assert(packet
->size
== ppl
);
2413 /* In all builds have an error message shown. */
2414 log_error("City size %d, citizen count %d for %s",
2415 packet
->size
, ppl
, city_name_get(pcity
));
2418 city_refresh(pcity
);
2419 auto_arrange_workers(pcity
);
2423 package_city(pcity
, packet
, web_packet
, routes
, dipl_invest
);
2430 packet
->city_radius_sq
= pcity
->city_radius_sq
;
2433 trade_routes_iterate(pcity
, proute
) {
2434 struct packet_traderoute_info
*tri_packet
= fc_malloc(sizeof(struct packet_traderoute_info
));
2436 tri_packet
->city
= pcity
->id
;
2437 tri_packet
->index
= i
;
2438 tri_packet
->partner
= proute
->partner
;
2439 tri_packet
->value
= proute
->value
;
2440 tri_packet
->direction
= proute
->dir
;
2441 tri_packet
->goods
= goods_number(proute
->goods
);
2443 traderoute_packet_list_append(routes
, tri_packet
);
2446 } trade_routes_iterate_end
;
2448 packet
->traderoute_count
= i
;
2450 output_type_iterate(o
) {
2451 packet
->surplus
[o
] = pcity
->surplus
[o
];
2452 packet
->waste
[o
] = pcity
->waste
[o
];
2453 packet
->unhappy_penalty
[o
] = pcity
->unhappy_penalty
[o
];
2454 packet
->prod
[o
] = pcity
->prod
[o
];
2455 packet
->citizen_base
[o
] = pcity
->citizen_base
[o
];
2456 packet
->usage
[o
] = pcity
->usage
[o
];
2457 } output_type_iterate_end
;
2459 packet
->food_stock
= pcity
->food_stock
;
2460 packet
->shield_stock
= pcity
->shield_stock
;
2461 packet
->pollution
= pcity
->pollution
;
2462 packet
->illness_trade
= pcity
->illness_trade
;
2463 packet
->city_options
= pcity
->city_options
;
2465 packet
->production_kind
= pcity
->production
.kind
;
2466 packet
->production_value
= universal_number(&pcity
->production
);
2468 packet
->turn_last_built
=pcity
->turn_last_built
;
2469 packet
->turn_founded
= pcity
->turn_founded
;
2471 packet
->changed_from_kind
= pcity
->changed_from
.kind
;
2472 packet
->changed_from_value
= universal_number(&pcity
->changed_from
);
2474 packet
->before_change_shields
=pcity
->before_change_shields
;
2475 packet
->disbanded_shields
=pcity
->disbanded_shields
;
2476 packet
->caravan_shields
=pcity
->caravan_shields
;
2477 packet
->last_turns_shield_surplus
= pcity
->last_turns_shield_surplus
;
2479 worklist_copy(&packet
->worklist
, &pcity
->worklist
);
2480 packet
->diplomat_investigate
=dipl_invest
;
2482 packet
->airlift
= pcity
->airlift
;
2483 packet
->did_buy
= pcity
->did_buy
;
2484 packet
->did_sell
= pcity
->did_sell
;
2485 packet
->was_happy
= pcity
->was_happy
;
2487 packet
->walls
= city_got_citywalls(pcity
);
2488 packet
->style
= pcity
->style
;
2489 packet
->city_image
= get_city_bonus(pcity
, EFT_CITY_IMAGE
);
2491 BV_CLR_ALL(packet
->improvements
);
2492 improvement_iterate(pimprove
) {
2493 if (city_has_building(pcity
, pimprove
)) {
2494 BV_SET(packet
->improvements
, improvement_index(pimprove
));
2496 } improvement_iterate_end
;
2499 web_packet
->id
= pcity
->id
;
2501 web_packet
->granary_size
= city_granary_size(city_size_get(pcity
));
2502 web_packet
->granary_turns
= city_turns_to_grow(pcity
);
2503 web_packet
->buy_gold_cost
= city_production_buy_gold_cost(pcity
);
2504 #endif /* FREECIV_WEB */
2507 /**************************************************************************
2508 updates a players knowledge about a city. If the player_tile already
2509 contains a city it must be the same city (avoid problems by always calling
2510 reality_check_city() first)
2512 Returns TRUE iff anything has changed for the player city (i.e., if the
2513 client needs to be updated with a *short* city packet). This information
2514 is only used in refresh_dumb_cities; elsewhere the data is (of necessity)
2515 broadcast regardless.
2516 **************************************************************************/
2517 bool update_dumb_city(struct player
*pplayer
, struct city
*pcity
)
2519 bv_imprs improvements
;
2520 struct tile
*pcenter
= city_tile(pcity
);
2521 struct vision_site
*pdcity
= map_get_player_city(pcenter
, pplayer
);
2522 /* pcity->client.occupied isn't used at the server, so we go straight to the
2523 * unit list to check the occupied status. */
2524 bool occupied
= (unit_list_size(pcenter
->units
) > 0);
2525 bool walls
= city_got_citywalls(pcity
);
2526 bool happy
= city_happy(pcity
);
2527 bool unhappy
= city_unhappy(pcity
);
2528 int style
= pcity
->style
;
2529 int city_image
= get_city_bonus(pcity
, EFT_CITY_IMAGE
);
2531 BV_CLR_ALL(improvements
);
2532 improvement_iterate(pimprove
) {
2533 if (is_improvement_visible(pimprove
)
2534 && city_has_building(pcity
, pimprove
)) {
2535 BV_SET(improvements
, improvement_index(pimprove
));
2537 } improvement_iterate_end
;
2539 if (NULL
== pdcity
) {
2540 pdcity
= vision_site_new_from_city(pcity
);
2541 change_playertile_site(map_get_player_tile(pcenter
, pplayer
), pdcity
);
2542 } else if (pdcity
->location
!= pcenter
) {
2543 log_error("Trying to update bad city (wrong location) "
2544 "at %i,%i for player %s",
2545 TILE_XY(pcity
->tile
), player_name(pplayer
));
2546 pdcity
->location
= pcenter
; /* ?? */
2547 } else if (pdcity
->identity
!= pcity
->id
) {
2548 log_error("Trying to update old city (wrong identity) "
2549 "at %i,%i for player %s",
2550 TILE_XY(city_tile(pcity
)), player_name(pplayer
));
2551 pdcity
->identity
= pcity
->id
; /* ?? */
2552 } else if (pdcity
->occupied
== occupied
2553 && pdcity
->walls
== walls
2554 && pdcity
->happy
== happy
2555 && pdcity
->unhappy
== unhappy
2556 && pdcity
->style
== style
2557 && pdcity
->city_image
== city_image
2558 && BV_ARE_EQUAL(pdcity
->improvements
, improvements
)
2559 && vision_site_size_get(pdcity
) == city_size_get(pcity
)
2560 && vision_site_owner(pdcity
) == city_owner(pcity
)
2561 && 0 == strcmp(pdcity
->name
, city_name_get(pcity
))) {
2565 vision_site_update_from_city(pdcity
, pcity
);
2566 pdcity
->occupied
= occupied
;
2567 pdcity
->walls
= walls
;
2568 pdcity
->style
= style
;
2569 pdcity
->city_image
= city_image
;
2570 pdcity
->happy
= happy
;
2571 pdcity
->unhappy
= unhappy
;
2572 pdcity
->improvements
= improvements
;
2577 /**************************************************************************
2578 Removes outdated (nonexistant) cities from a player
2579 **************************************************************************/
2580 void reality_check_city(struct player
*pplayer
, struct tile
*ptile
)
2582 struct vision_site
*pdcity
= map_get_player_city(ptile
, pplayer
);
2585 struct city
*pcity
= tile_city(ptile
);
2587 if (!pcity
|| pcity
->id
!= pdcity
->identity
) {
2588 struct player_tile
*playtile
= map_get_player_tile(ptile
, pplayer
);
2590 dlsend_packet_city_remove(pplayer
->connections
, pdcity
->identity
);
2591 fc_assert_ret(playtile
->site
== pdcity
);
2592 playtile
->site
= NULL
;
2593 vision_site_destroy(pdcity
);
2598 /**************************************************************************
2599 Removes a dumb city. Called when the vision changed to unknown.
2600 **************************************************************************/
2601 void remove_dumb_city(struct player
*pplayer
, struct tile
*ptile
)
2603 struct vision_site
*pdcity
= map_get_player_city(ptile
, pplayer
);
2606 struct player_tile
*playtile
= map_get_player_tile(ptile
, pplayer
);
2608 dlsend_packet_city_remove(pplayer
->connections
, pdcity
->identity
);
2609 fc_assert_ret(playtile
->site
== pdcity
);
2610 playtile
->site
= NULL
;
2611 vision_site_destroy(pdcity
);
2615 /**************************************************************************
2616 Announce to the owners of the cities that trade route has been canceled
2618 **************************************************************************/
2619 static void announce_trade_route_removal(struct city
*pc1
, struct city
*pc2
,
2622 struct player
*plr1
= city_owner(pc1
);
2623 struct player
*plr2
= city_owner(pc2
);
2624 char city1_link
[MAX_LEN_LINK
];
2625 char city2_link
[MAX_LEN_LINK
];
2627 sz_strlcpy(city1_link
, city_link(pc1
));
2628 sz_strlcpy(city2_link
, city_link(pc2
));
2632 notify_player(plr2
, city_tile(pc2
),
2633 E_CARAVAN_ACTION
, ftc_server
,
2634 _("Trade between %s and %s lost along with city."),
2635 city1_link
, city2_link
);
2637 notify_player(plr1
, city_tile(pc1
),
2638 E_CARAVAN_ACTION
, ftc_server
,
2639 _("Trade route between %s and %s canceled."),
2640 city1_link
, city2_link
);
2644 notify_player(plr2
, city_tile(pc2
),
2645 E_CARAVAN_ACTION
, ftc_server
,
2646 /* TRANS: "...between Spanish city Madrid and Paris..." */
2647 _("Trade between %s city %s and %s lost along with "
2649 nation_adjective_for_player(plr1
), city1_link
, city2_link
);
2650 /* It's implicit to removed city's owner that that city no longer
2651 * has trade routes, so say nothing in that case */
2653 notify_player(plr2
, city_tile(pc2
),
2654 E_CARAVAN_ACTION
, ftc_server
,
2655 _("Sorry, the %s canceled the trade route "
2656 "from %s to your city %s."),
2657 nation_plural_for_player(plr1
), city1_link
, city2_link
);
2658 notify_player(plr1
, city_tile(pc1
),
2659 E_CARAVAN_ACTION
, ftc_server
,
2660 /* TRANS: "...from Paris to Spanish city Madrid." */
2661 _("We canceled the trade route "
2662 "from %s to %s city %s."),
2663 city1_link
, nation_adjective_for_player(plr2
), city2_link
);
2668 /**************************************************************************
2669 Remove the trade route between pc1 and pc2 (if one exists).
2670 source_gone should be TRUE if the reason for removal is the imminent
2671 removal of the source city (pc1) from the game.
2673 Does not free the trade route structures, only removes them from the
2675 **************************************************************************/
2676 struct trade_route
*remove_trade_route(struct city
*pc1
, struct trade_route
*proute
,
2677 bool announce
, bool source_gone
)
2679 struct city
*pc2
= game_city_by_number(proute
->partner
);
2680 struct trade_route
*back_route
= NULL
;
2682 fc_assert_ret_val(pc1
&& proute
, NULL
);
2684 trade_route_list_remove(pc1
->routes
, proute
);
2687 trade_routes_iterate(pc2
, pback
) {
2688 if (pc1
->id
== pback
->partner
) {
2691 } trade_routes_iterate_end
;
2693 if (back_route
!= NULL
) {
2694 trade_route_list_remove(pc2
->routes
, back_route
);
2699 announce_trade_route_removal(pc1
, pc2
, source_gone
);
2702 send_city_info(city_owner(pc2
), pc2
);
2708 /****************************************************************************
2709 Remove/cancel the city's least valuable trade routes.
2710 ****************************************************************************/
2711 static void remove_smallest_trade_routes(struct city
*pcity
)
2713 struct trade_route_list
*smallest
= trade_route_list_new();
2715 (void) city_trade_removable(pcity
, smallest
);
2716 trade_route_list_iterate(smallest
, proute
) {
2717 struct trade_route
*back
;
2719 back
= remove_trade_route(pcity
, proute
, TRUE
, FALSE
);
2722 } trade_route_list_iterate_end
;
2723 trade_route_list_destroy(smallest
);
2726 /**************************************************************************
2727 Establish a trade route.
2728 **************************************************************************/
2729 void establish_trade_route(struct city
*pc1
, struct city
*pc2
)
2731 struct trade_route
*proute
;
2733 if (city_num_trade_routes(pc1
) >= max_trade_routes(pc1
)) {
2734 remove_smallest_trade_routes(pc1
);
2737 if (city_num_trade_routes(pc2
) >= max_trade_routes(pc2
)) {
2738 remove_smallest_trade_routes(pc2
);
2741 proute
= fc_malloc(sizeof(struct trade_route
));
2742 proute
->partner
= pc2
->id
;
2743 proute
->dir
= RDIR_FROM
;
2744 trade_route_list_append(pc1
->routes
, proute
);
2746 proute
= fc_malloc(sizeof(struct trade_route
));
2747 proute
->partner
= pc1
->id
;
2748 proute
->dir
= RDIR_TO
;
2749 trade_route_list_append(pc2
->routes
, proute
);
2751 /* recalculate illness due to trade */
2752 if (game
.info
.illness_on
) {
2753 pc1
->server
.illness
= city_illness_calc(pc1
, NULL
, NULL
,
2754 &(pc1
->illness_trade
), NULL
);
2755 pc2
->server
.illness
= city_illness_calc(pc2
, NULL
, NULL
,
2756 &(pc2
->illness_trade
), NULL
);
2760 /****************************************************************************
2761 Sell the improvement from the city, and give the player the owner. Not
2762 all buildings can be sold.
2764 I guess the player should always be the city owner?
2765 ****************************************************************************/
2766 void do_sell_building(struct player
*pplayer
, struct city
*pcity
,
2767 struct impr_type
*pimprove
)
2769 if (can_city_sell_building(pcity
, pimprove
)) {
2770 pplayer
->economic
.gold
+= impr_sell_gold(pimprove
);
2771 building_lost(pcity
, pimprove
);
2775 /****************************************************************************
2776 Destroy the improvement in the city straight-out. Note that this is
2777 different from selling a building.
2778 ****************************************************************************/
2779 void building_lost(struct city
*pcity
, const struct impr_type
*pimprove
)
2781 struct player
*owner
= city_owner(pcity
);
2782 bool was_capital
= is_capital(pcity
);
2784 city_remove_improvement(pcity
, pimprove
);
2785 if ((was_capital
&& !is_capital(pcity
))
2786 && (owner
->spaceship
.state
== SSHIP_STARTED
2787 || owner
->spaceship
.state
== SSHIP_LAUNCHED
)) {
2788 /* If the capital was lost (by destruction of the palace) production on
2789 * the spaceship is lost. */
2790 spaceship_lost(owner
);
2793 /* update city; influence of effects (buildings, ...) on unit upkeep */
2794 if (city_refresh(pcity
)) {
2795 auto_arrange_workers(pcity
);
2798 /* Re-update the city's visible area. This updates fog if the vision
2799 * range increases or decreases. */
2800 city_refresh_vision(pcity
);
2803 /**************************************************************************
2804 Recalculate the upkeep needed for all units supported by the city. It has
2807 - if a save game is loaded (via city_refresh() in game_load_internal())
2809 - if the number of units supported by a city changes
2810 * create a unit (in create_unit_full())
2811 * bride a unit (in diplomat_bribe())
2812 * change homecity (in unit_change_homecity_handling())
2813 * destroy a unit (in wipe_unit())
2815 - if the rules for the upkeep calculation change. This is due to effects
2816 which influence the upkeep calculation.
2817 * tech researched, government change (via city_refresh())
2818 * building destroyed (in building_lost())
2819 * building created (via city_refresh() in in city_build_building())
2821 If the upkeep for a unit changes, an update is send to the player.
2822 **************************************************************************/
2823 void city_units_upkeep(const struct city
*pcity
)
2825 int free_uk
[O_LAST
];
2827 struct unit_type
*ut
;
2831 if (!pcity
|| !pcity
->units_supported
2832 || unit_list_size(pcity
->units_supported
) < 1) {
2836 memset(free_uk
, 0, O_LAST
* sizeof(*free_uk
));
2837 output_type_iterate(o
) {
2838 free_uk
[o
] = get_city_output_bonus(pcity
, get_output_type(o
),
2839 EFT_UNIT_UPKEEP_FREE_PER_CITY
);
2840 } output_type_iterate_end
;
2842 /* save the upkeep for all units in the corresponding punit struct */
2843 unit_list_iterate(pcity
->units_supported
, punit
) {
2844 ut
= unit_type_get(punit
);
2845 plr
= unit_owner(punit
);
2848 output_type_iterate(o
) {
2849 cost
= utype_upkeep_cost(ut
, plr
, o
);
2851 if (free_uk
[o
] > cost
) {
2860 if (cost
!= punit
->upkeep
[o
]) {
2862 punit
->upkeep
[o
] = cost
;
2864 } output_type_iterate_end
;
2867 /* Update unit information to the player and global observers. */
2868 send_unit_info(NULL
, punit
);
2870 } unit_list_iterate_end
;
2873 /**************************************************************************
2874 Change the build target.
2875 **************************************************************************/
2876 void change_build_target(struct player
*pplayer
, struct city
*pcity
,
2877 struct universal
*target
,
2878 enum event_type event
)
2883 /* If the city is already building this thing, don't do anything */
2884 if (are_universals_equal(&pcity
->production
, target
)) {
2888 /* Is the city no longer building a wonder? */
2889 if (VUT_IMPROVEMENT
== pcity
->production
.kind
2890 && is_great_wonder(pcity
->production
.value
.building
)
2891 && event
!= E_IMP_AUTO
2892 && event
!= E_WORKLIST
) {
2893 /* If the build target is changed because of an advisor's suggestion or
2894 because the worklist advances, then the wonder was completed --
2895 don't announce that the player has *stopped* building that wonder.
2897 notify_player(NULL
, city_tile(pcity
), E_WONDER_STOPPED
, ftc_server
,
2898 _("The %s have stopped building The %s in %s."),
2899 nation_plural_for_player(pplayer
),
2900 city_production_name_translation(pcity
),
2904 /* Manage the city change-production penalty.
2905 (May penalize, restore or do nothing to the shield_stock.) */
2906 if (!is_ai(pplayer
) || has_handicap(pplayer
, H_PRODCHGPEN
)) {
2907 pcity
->shield_stock
= city_change_production_penalty(pcity
, target
);
2910 /* Change build target. */
2911 pcity
->production
= *target
;
2913 /* What's the name of the target? */
2914 name
= city_production_name_translation(pcity
);
2918 /* TRANS: Possible 'source' of the production change. */
2919 source
= _(" from the worklist");
2922 /* TRANS: Possible 'source' of the production change. */
2923 source
= _(" as suggested by the advisor");
2930 log_base(LOG_BUILD_TARGET
, "%s started building %s%s.",
2931 city_name_get(pcity
), name
, source
);
2933 /* Tell the player what's up. */
2934 /* FIXME: this may give bad grammar when translated if the 'source'
2935 * string can have multiple values. */
2936 notify_player(pplayer
, city_tile(pcity
), event
, ftc_server
,
2937 /* TRANS: "<city> is building <production><source>."
2938 * 'source' might be an empty string. */
2939 _("%s is building %s%s."),
2943 /* If the city is building a wonder, tell the rest of the world
2945 if (VUT_IMPROVEMENT
== pcity
->production
.kind
2946 && is_great_wonder(pcity
->production
.value
.building
)) {
2947 notify_player(NULL
, city_tile(pcity
), E_WONDER_STARTED
, ftc_server
,
2948 _("The %s have started building The %s in %s."),
2949 nation_plural_for_player(pplayer
),
2955 /**************************************************************************
2956 Change from worked to empty.
2957 city_x, city_y are city map coordinates.
2958 Call sync_cities() to send the affected cities to the clients.
2959 **************************************************************************/
2960 void city_map_update_empty(struct city
*pcity
, struct tile
*ptile
)
2962 tile_set_worked(ptile
, NULL
);
2963 send_tile_info(NULL
, ptile
, FALSE
);
2964 pcity
->server
.synced
= FALSE
;
2967 /**************************************************************************
2968 Change from empty to worked.
2969 city_x, city_y are city map coordinates.
2970 Call sync_cities() to send the affected cities to the clients.
2971 **************************************************************************/
2972 void city_map_update_worker(struct city
*pcity
, struct tile
*ptile
)
2974 tile_set_worked(ptile
, pcity
);
2975 send_tile_info(NULL
, ptile
, FALSE
);
2976 pcity
->server
.synced
= FALSE
;
2979 /**************************************************************************
2980 Updates the worked status of a tile.
2982 If the status changes, auto_arrange_workers() may be called.
2983 **************************************************************************/
2984 static bool city_map_update_tile_direct(struct tile
*ptile
, bool queued
)
2986 struct city
*pwork
= tile_worked(ptile
);
2989 && !is_free_worked(pwork
, ptile
)
2990 && !city_can_work_tile(pwork
, ptile
)) {
2991 tile_set_worked(ptile
, NULL
);
2992 send_tile_info(NULL
, ptile
, FALSE
);
2994 pwork
->specialists
[DEFAULT_SPECIALIST
]++; /* keep city sanity */
2995 pwork
->server
.synced
= FALSE
;
2998 city_freeze_workers_queue(pwork
); /* place the displaced later */
3000 city_refresh(pwork
); /* Specialist added, keep citizen count sanity */
3001 auto_arrange_workers(pwork
);
3002 send_city_info(NULL
, pwork
);
3010 /**************************************************************************
3011 Updates the worked status of a tile.
3012 Call city_thaw_workers_queue() followed by sync_cities() to send the
3013 affected cities to the clients.
3014 **************************************************************************/
3015 bool city_map_update_tile_frozen(struct tile
*ptile
)
3017 return city_map_update_tile_direct(ptile
, TRUE
);
3020 /**************************************************************************
3021 Updates the worked status of a tile immediately.
3022 **************************************************************************/
3023 bool city_map_update_tile_now(struct tile
*ptile
)
3025 return city_map_update_tile_direct(ptile
, FALSE
);
3028 /**************************************************************************
3029 Make sure all players (clients) have up-to-date information about all
3031 **************************************************************************/
3032 void sync_cities(void)
3034 if (send_city_suppressed
) {
3038 players_iterate(pplayer
) {
3039 city_list_iterate(pplayer
->cities
, pcity
) {
3040 if (!pcity
->server
.synced
) {
3041 /* sending will set to TRUE. */
3042 send_city_info(pplayer
, pcity
);
3044 } city_list_iterate_end
;
3045 } players_iterate_end
;
3048 /**************************************************************************
3049 Called by auto_arrange_workers() and below.
3050 **************************************************************************/
3051 void city_map_update_all(struct city
*pcity
)
3053 struct tile
*pcenter
= city_tile(pcity
);
3055 city_tile_iterate_skip_free_worked(city_map_radius_sq_get(pcity
), pcenter
,
3056 ptile
, _index
, _x
, _y
) {
3057 /* bypass city_map_update_tile_now() for efficiency */
3058 city_map_update_tile_direct(ptile
, FALSE
);
3059 } city_tile_iterate_skip_free_worked_end
;
3062 /**************************************************************************
3063 Update worked map of all cities of given player
3064 **************************************************************************/
3065 void city_map_update_all_cities_for_player(struct player
*pplayer
)
3067 city_list_iterate(pplayer
->cities
, pcity
) {
3068 city_freeze_workers(pcity
);
3069 city_map_update_all(pcity
);
3070 city_thaw_workers(pcity
);
3071 } city_list_iterate_end
;
3074 /**************************************************************************
3075 For each city adjacent to ptile, check all the buildings in the city.
3076 Any which have unmet terrain requirements will be sold. This is called
3077 after any terrain changes (but this may be tied to the default ruleset).
3079 FIXME: This function isn't very general. It would be better to check
3080 each turn to make sure all requirements of all buildings of all cities
3081 are met, and sell any buildings that can't be supported. Terrains aren't
3082 the only requirement that may disappear.
3083 **************************************************************************/
3084 void city_landlocked_sell_coastal_improvements(struct tile
*ptile
)
3086 adjc_iterate(&(wld
.map
), ptile
, tile1
) {
3087 struct city
*pcity
= tile_city(tile1
);
3089 if (pcity
&& !is_terrain_class_near_tile(tile1
, TC_OCEAN
)) {
3090 struct player
*pplayer
= city_owner(pcity
);
3092 /* Sell all buildings (but not Wonders) that must be next to the ocean */
3093 city_built_iterate(pcity
, pimprove
) {
3094 if (!can_city_sell_building(pcity
, pimprove
)) {
3098 requirement_vector_iterate(&pimprove
->reqs
, preq
) {
3099 if ((VUT_TERRAIN
== preq
->source
.kind
3100 || VUT_TERRAINCLASS
== preq
->source
.kind
)
3101 && !is_req_active(city_owner(pcity
), NULL
, pcity
, NULL
,
3102 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
3104 int price
= impr_sell_gold(pimprove
);
3106 do_sell_building(pplayer
, pcity
, pimprove
);
3107 notify_player(pplayer
, tile1
, E_IMP_SOLD
, ftc_server
,
3108 PL_("You sell %s in %s (now landlocked)"
3110 "You sell %s in %s (now landlocked)"
3111 " for %d gold.", price
),
3112 improvement_name_translation(pimprove
),
3113 city_link(pcity
), price
);
3115 } requirement_vector_iterate_end
;
3116 } city_built_iterate_end
;
3121 /****************************************************************************
3122 Refresh the city's vision.
3124 This function has very small overhead and can be called any time effects
3125 may have changed the vision range of the city.
3126 ****************************************************************************/
3127 void city_refresh_vision(struct city
*pcity
)
3129 v_radius_t vision_radius_sq
=
3130 V_RADIUS(get_city_bonus(pcity
, EFT_CITY_VISION_RADIUS_SQ
), 2);
3132 vision_change_sight(pcity
->server
.vision
, vision_radius_sq
);
3133 ASSERT_VISION(pcity
->server
.vision
);
3136 /****************************************************************************
3137 Refresh the vision of all cities owned by a player, for empire-wide
3139 ****************************************************************************/
3140 void refresh_player_cities_vision(struct player
*pplayer
)
3142 city_list_iterate(pplayer
->cities
, pcity
) {
3143 city_refresh_vision(pcity
);
3144 } city_list_iterate_end
;
3147 /**************************************************************************
3148 Updates the squared city radius. Returns if the radius is changed.
3149 **************************************************************************/
3150 bool city_map_update_radius_sq(struct city
*pcity
)
3153 fc_assert_ret_val(pcity
!= NULL
, FALSE
);
3155 int city_tiles_old
, city_tiles_new
;
3156 int city_radius_sq_old
= city_map_radius_sq_get(pcity
);
3157 int city_radius_sq_new
= game
.info
.init_city_radius_sq
3158 + get_city_bonus(pcity
, EFT_CITY_RADIUS_SQ
);
3160 /* check minimum / maximum allowed city radii */
3161 city_radius_sq_new
= CLIP(CITY_MAP_MIN_RADIUS_SQ
, city_radius_sq_new
,
3162 CITY_MAP_MAX_RADIUS_SQ
);
3164 if (city_radius_sq_new
== city_radius_sq_old
) {
3169 /* get number of city tiles for each radii */
3170 city_tiles_old
= city_map_tiles(city_radius_sq_old
);
3171 city_tiles_new
= city_map_tiles(city_radius_sq_new
);
3173 if (city_tiles_old
== city_tiles_new
) {
3174 /* a change of the squared city radius but no change of the number of
3179 log_debug("[%s (%d)] city_map_radius_sq: %d => %d", city_name_get(pcity
),
3180 pcity
->id
, city_radius_sq_old
, city_radius_sq_new
);
3182 /* workers map before */
3183 log_debug("[%s (%d)] city size: %d; specialists: %d (before change)",
3184 city_name_get(pcity
), pcity
->id
, city_size_get(pcity
),
3185 city_specialists(pcity
));
3186 citylog_map_workers(LOG_DEBUG
, pcity
);
3188 city_map_radius_sq_set(pcity
, city_radius_sq_new
);
3190 if (city_tiles_old
< city_tiles_new
) {
3191 /* increased number of city tiles */
3192 city_refresh_vision(pcity
);
3194 /* reduced number of city tiles */
3197 /* remove workers from the tiles removed rom the city map */
3198 city_map_iterate_radius_sq(city_radius_sq_new
, city_radius_sq_old
,
3200 struct tile
*ptile
= city_map_to_tile(city_tile(pcity
),
3201 city_radius_sq_old
, city_x
,
3204 if (ptile
&& pcity
== tile_worked(ptile
)) {
3205 city_map_update_empty(pcity
, ptile
);
3208 } city_map_iterate_radius_sq_end
;
3210 /* add workers to free city tiles */
3212 int radius_sq
= city_map_radius_sq_get(pcity
);
3213 city_map_iterate_without_index(radius_sq
, city_x
, city_y
) {
3214 struct tile
*ptile
= city_map_to_tile(city_tile(pcity
), radius_sq
,
3217 if (ptile
&& !is_free_worked(pcity
, ptile
)
3218 && tile_worked(ptile
) != pcity
3219 && city_can_work_tile(pcity
, ptile
)) {
3220 city_map_update_worker(pcity
, ptile
);
3227 } city_map_iterate_without_index_end
;
3230 /* if there are still workers they will be updated to specialists */
3232 pcity
->specialists
[DEFAULT_SPECIALIST
] += workers
;
3235 city_refresh_vision(pcity
);
3238 /* if city is under AI control update it */
3239 adv_city_update(pcity
);
3241 notify_player(city_owner(pcity
), city_tile(pcity
), E_CITY_RADIUS_SQ
,
3242 ftc_server
, _("The size of the city map of %s is %s."),
3243 city_name_get(pcity
),
3244 city_tiles_old
< city_tiles_new
? _("increased")
3247 /* workers map after */
3248 log_debug("[%s (%d)] city size: %d; specialists: %d (after change)",
3249 city_name_get(pcity
), pcity
->id
, city_size_get(pcity
),
3250 city_specialists(pcity
));
3251 citylog_map_workers(LOG_DEBUG
, pcity
);
3256 /**************************************************************************
3257 Clear worker task from the city and inform owner
3258 **************************************************************************/
3259 void clear_worker_task(struct city
*pcity
, struct worker_task
*ptask
)
3261 struct packet_worker_task packet
;
3263 if (ptask
== NULL
) {
3267 worker_task_list_remove(pcity
->task_reqs
, ptask
);
3269 packet
.city_id
= pcity
->id
;
3270 packet
.tile_id
= tile_index(ptask
->ptile
);
3271 packet
.activity
= ACTIVITY_LAST
;
3277 lsend_packet_worker_task(city_owner(pcity
)->connections
, &packet
);
3278 lsend_packet_worker_task(game
.glob_observers
, &packet
);
3281 /**************************************************************************
3282 Clear all worker tasks from the city and inform owner
3283 **************************************************************************/
3284 void clear_worker_tasks(struct city
*pcity
)
3286 while (worker_task_list_size(pcity
->task_reqs
) > 0) {
3287 clear_worker_task(pcity
, worker_task_list_get(pcity
->task_reqs
, 0));
3291 /**************************************************************************
3292 Send city worker task to owner
3293 **************************************************************************/
3294 void package_and_send_worker_tasks(struct city
*pcity
)
3296 struct packet_worker_task packet
;
3298 packet
.city_id
= pcity
->id
;
3300 worker_task_list_iterate(pcity
->task_reqs
, ptask
) {
3301 packet
.tile_id
= tile_index(ptask
->ptile
);
3302 packet
.activity
= ptask
->act
;
3303 if (ptask
->tgt
== NULL
) {
3306 packet
.tgt
= extra_number(ptask
->tgt
);
3308 packet
.want
= ptask
->want
;
3310 lsend_packet_worker_task(city_owner(pcity
)->connections
, &packet
);
3311 lsend_packet_worker_task(game
.glob_observers
, &packet
);
3312 } worker_task_list_iterate_end
;