1 /***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
13
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 /* utility */
23 #include "bitvector.h"
24 #include "fcintl.h"
25 #include "log.h"
26 #include "mem.h"
27 #include "rand.h"
28 #include "shared.h"
29 #include "support.h"
30
31 /* common */
32 #include "ai.h"
33 #include "base.h"
34 #include "citizens.h"
35 #include "city.h"
36 #include "culture.h"
37 #include "events.h"
38 #include "game.h"
39 #include "government.h"
40 #include "improvement.h"
41 #include "idex.h"
42 #include "map.h"
43 #include "movement.h"
44 #include "player.h"
45 #include "requirements.h"
46 #include "research.h"
47 #include "road.h"
48 #include "specialist.h"
49 #include "tech.h"
50 #include "traderoutes.h"
51 #include "unit.h"
52 #include "unitlist.h"
53 #include "vision.h"
54
55 /* common/scriptcore */
56 #include "luascript_types.h"
57
58 /* server */
59 #include "barbarian.h"
60 #include "citizenshand.h"
61 #include "cityturn.h"
62 #include "gamehand.h" /* send_game_info() */
63 #include "maphand.h"
64 #include "notify.h"
65 #include "plrhand.h"
66 #include "sanitycheck.h"
67 #include "sernet.h"
68 #include "spacerace.h"
69 #include "srv_main.h"
70 #include "techtools.h"
71 #include "unithand.h"
72 #include "unittools.h"
73
74 /* server/advisors */
75 #include "advbuilding.h"
76 #include "advgoto.h"
77 #include "autosettlers.h"
78 #include "infracache.h"
79
80 /* server/scripting */
81 #include "script_server.h"
82
83 /* ai */
84 #include "handicaps.h"
85
86 /* ai */
87 #include "handicaps.h"
88
89 #include "citytools.h"
90
91
92 /* Queue for pending auto_arrange_workers() */
93 static struct city_list *arrange_workers_queue = NULL;
94
95 /* Suppress sending cities during game_load() and end_phase() */
96 static bool send_city_suppressed = FALSE;
97
98 static bool city_workers_queue_remove(struct city *pcity);
99
100 static void announce_trade_route_removal(struct city *pc1, struct city *pc2,
101 bool source_gone);
102
103 /****************************************************************************
104 Freeze the workers (citizens on tiles) for the city. They will not be
105 auto-arranged until unfreeze_workers is called.
106
107 Long explanation:
108
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
117 thaw_workers.
118
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.
124
125 Freezing may safely be done more than once.
126
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 ****************************************************************************/
city_freeze_workers(struct city * pcity)130 void city_freeze_workers(struct city *pcity)
131 {
132 pcity->server.workers_frozen++;
133 }
134
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
138 in freeze_workers().
139 ****************************************************************************/
city_thaw_workers(struct city * pcity)140 void city_thaw_workers(struct city *pcity)
141 {
142 pcity->server.workers_frozen--;
143 fc_assert(pcity->server.workers_frozen >= 0);
144 if (pcity->server.workers_frozen == 0
145 && pcity->server.needs_arrange != CNA_NOT) {
146 city_refresh(pcity); /* Citizen count sanity */
147 auto_arrange_workers(pcity);
148 }
149 }
150
151 /****************************************************************************
152 Queue pending auto_arrange_workers() for later.
153 ****************************************************************************/
city_freeze_workers_queue(struct city * pcity)154 void city_freeze_workers_queue(struct city *pcity)
155 {
156 if (NULL == arrange_workers_queue) {
157 arrange_workers_queue = city_list_new();
158 } else if (city_list_find_number(arrange_workers_queue, pcity->id)) {
159 return;
160 }
161
162 city_list_prepend(arrange_workers_queue, pcity);
163 city_freeze_workers(pcity);
164 if (pcity->server.needs_arrange == CNA_NOT) {
165 pcity->server.needs_arrange = CNA_NORMAL;
166 }
167 }
168
169 /****************************************************************************
170 Remove a city from the queue for later calls to auto_arrange_workers().
171 Reterns TRUE if the city was found in the queue.
172 ****************************************************************************/
city_workers_queue_remove(struct city * pcity)173 static bool city_workers_queue_remove(struct city *pcity)
174 {
175 if (arrange_workers_queue == NULL) {
176 return FALSE;
177 }
178
179 return city_list_remove(arrange_workers_queue, pcity);
180 }
181
182 /****************************************************************************
183 Process the frozen workers.
184 Call sync_cities() to send the affected cities to the clients.
185 ****************************************************************************/
city_thaw_workers_queue(void)186 void city_thaw_workers_queue(void)
187 {
188 if (NULL == arrange_workers_queue) {
189 return;
190 }
191
192 city_list_iterate(arrange_workers_queue, pcity) {
193 city_thaw_workers(pcity);
194 } city_list_iterate_end;
195
196 city_list_destroy(arrange_workers_queue);
197 arrange_workers_queue = NULL;
198 }
199
200 /****************************************************************************
201 Returns the priority of the city name at the given position, using its
202 own internal algorithm. Lower priority values are more desired, and all
203 priorities are non-negative.
204
205 This function takes into account game.natural_city_names, and should be
206 able to deal with any future options we want to add.
207 ****************************************************************************/
evaluate_city_name_priority(struct tile * ptile,const struct nation_city * pncity,int default_priority)208 static int evaluate_city_name_priority(struct tile *ptile,
209 const struct nation_city *pncity,
210 int default_priority)
211 {
212 /* Lower values mean higher priority. */
213 float priority = (float)default_priority;
214 enum nation_city_preference goodness;
215
216 /* Increasing this value will increase the difference caused by
217 (non-)matching terrain. A matching terrain is mult_factor
218 "better" than an unlisted terrain, which is mult_factor
219 "better" than a non-matching terrain. */
220 const float mult_factor = 1.4;
221 bool river = FALSE;
222
223 /*
224 * If natural city names aren't being used, we just return the
225 * base value. This will have the effect of the first-listed
226 * city being used. We do this here rather than special-casing
227 * it elewhere because this localizes everything to this
228 * function, even though it's a bit inefficient.
229 */
230 if (!game.server.natural_city_names) {
231 return default_priority;
232 }
233
234 /*
235 * Assuming we're using the natural city naming system, we use
236 * an internal alorithm to calculate the priority of each name.
237 * It's a pretty fuzzy algorithm; we basically do the following:
238 * - Change the priority scale from 0..n to 10..n+10. This means
239 * each successive city is 10% lower priority than the first.
240 * - Multiply by a semi-random number. This has the effect of
241 * decreasing rounding errors in the successive calculations,
242 * as well as introducing a smallish random effect.
243 * - Check over all the applicable terrains, and
244 * multiply or divide the priority based on whether or not
245 * the terrain matches. See comment below.
246 */
247
248 priority += 10.0;
249 priority *= 10.0 + fc_rand(5);
250
251 /*
252 * The terrain priority in the struct nation_city will be either
253 * -1, 0, or 1. We therefore take this as-is if the terrain is
254 * present, or negate it if not.
255 *
256 * The reason we multiply as well as divide the value is so
257 * that cities that don't care what terrain they are on (which
258 * is the default) will be left in the middle of the pack. If
259 * we _only_ multiplied (or divided), then cities that had more
260 * terrain labels would have their priorities hurt (or helped).
261 */
262 goodness = nation_city_river_preference(pncity);
263 extra_type_by_cause_iterate(EC_ROAD, priver) {
264 if (tile_has_extra(ptile, priver)
265 && road_has_flag(extra_road_get(priver), RF_RIVER)) {
266 river = TRUE;
267 break;
268 }
269 } extra_type_by_cause_iterate_end;
270 if (!river) {
271 goodness = nation_city_preference_revert(goodness);
272 }
273
274 switch (goodness) {
275 case NCP_DISLIKE:
276 priority *= mult_factor;
277 break;
278 case NCP_NONE:
279 break;
280 case NCP_LIKE:
281 priority /= mult_factor;
282 break;
283 }
284
285 terrain_type_iterate(pterrain) {
286 /* Now we do the same for every available terrain. */
287 goodness = nation_city_terrain_preference(pncity, pterrain);
288 if (!is_terrain_near_tile(ptile, pterrain, TRUE)) {
289 goodness = nation_city_preference_revert(goodness);
290 }
291 switch (goodness) {
292 case NCP_DISLIKE:
293 priority *= mult_factor;
294 break;
295 case NCP_NONE:
296 break;
297 case NCP_LIKE:
298 priority /= mult_factor;
299 }
300 } terrain_type_iterate_end;
301
302 return (int) priority;
303 }
304
305 /****************************************************************************
306 Checks if a city name belongs to default city names of a particular
307 player.
308 ****************************************************************************/
is_default_city_name(const char * name,struct player * pplayer)309 static bool is_default_city_name(const char *name, struct player *pplayer)
310 {
311 nation_city_list_iterate(nation_cities(nation_of_player(pplayer)),
312 pncity) {
313 if (0 == fc_strcasecmp(name, nation_city_name(pncity))) {
314 return TRUE;
315 }
316 } nation_city_list_iterate_end;
317 return FALSE;
318 }
319
320 /****************************************************************************
321 Searches through a city name list (a struct nation_city array) to pick
322 the best available city name, and returns a pointer to it. The function
323 checks if the city name is available and calls
324 evaluate_city_name_priority() to determine the priority of the city name.
325 If the list has no valid entries in it, NULL will be returned.
326 ****************************************************************************/
search_for_city_name(struct tile * ptile,const struct nation_city_list * default_cities,struct player * pplayer)327 static const char *search_for_city_name(struct tile *ptile,
328 const struct nation_city_list *
329 default_cities,
330 struct player *pplayer)
331 {
332 int choice = 0, priority, best_priority = -1;
333 const char *name, *best_name = NULL;
334
335 nation_city_list_iterate(default_cities, pncity) {
336 name = nation_city_name(pncity);
337 if (NULL == game_city_by_name(name)
338 && is_allowed_city_name(pplayer, name, NULL, 0)) {
339 priority = evaluate_city_name_priority(ptile, pncity, choice++);
340 if (-1 == best_priority || priority < best_priority) {
341 best_priority = priority;
342 best_name = name;
343 }
344 }
345 } nation_city_list_iterate_end;
346
347 return best_name;
348 }
349
350 /**************************************************************************
351 Checks, if a city name is allowed for a player. If not, reports a
352 reason for rejection. There's 4 different modes:
353 0: no restrictions,
354 1: a city name has to be unique to player
355 2: a city name has to be globally unique
356 3: a city name has to be globally unique, and players can't use names
357 that are in another player's default city names. (E.g., Swedish may not
358 call new cities or rename old cities as Helsinki, because it's in
359 Finns' default city names. Duplicated names may be used by
360 either nation.)
361 **************************************************************************/
is_allowed_city_name(struct player * pplayer,const char * cityname,char * error_buf,size_t bufsz)362 bool is_allowed_city_name(struct player *pplayer, const char *cityname,
363 char *error_buf, size_t bufsz)
364 {
365 struct connection *pconn = conn_by_user(pplayer->username);
366
367 /* Mode 1: A city name has to be unique for each player. */
368 if (CNM_PLAYER_UNIQUE == game.server.allowed_city_names
369 && city_list_find_name(pplayer->cities, cityname)) {
370 if (error_buf) {
371 fc_snprintf(error_buf, bufsz, _("You already have a city called %s."),
372 cityname);
373 }
374 return FALSE;
375 }
376
377 /* Modes 2,3: A city name has to be globally unique. */
378 if ((CNM_GLOBAL_UNIQUE == game.server.allowed_city_names
379 || CNM_NO_STEALING == game.server.allowed_city_names)
380 && game_city_by_name(cityname)) {
381 if (error_buf) {
382 fc_snprintf(error_buf, bufsz,
383 _("A city called %s already exists."), cityname);
384 }
385 return FALSE;
386 }
387
388 /* General rule: any name in our ruleset is allowed. */
389 if (is_default_city_name(cityname, pplayer)) {
390 return TRUE;
391 }
392
393 /*
394 * Mode 3: Check that the proposed city name is not in another
395 * player's default city names. Note the name will already have been
396 * allowed if it is in this player's default city names list.
397 */
398 if (CNM_NO_STEALING == game.server.allowed_city_names) {
399 struct player *pother = NULL;
400
401 players_iterate(player2) {
402 if (player2 != pplayer && is_default_city_name(cityname, player2)) {
403 pother = player2;
404 break;
405 }
406 } players_iterate_end;
407
408 if (pother != NULL) {
409 if (error_buf) {
410 fc_snprintf(error_buf, bufsz,
411 _("Can't use %s as a city name. It is reserved for %s."),
412 cityname, nation_plural_for_player(pother));
413 }
414 return FALSE;
415 }
416 }
417
418 /* To prevent abuse, only players with HACK access (usually local
419 * connections) can use non-ascii names. Otherwise players could use
420 * confusing garbage names in multi-player games.
421 *
422 * We can even reach here for an AI player, if all the cities of the
423 * original nation are exhausted and the backup nations have non-ascii
424 * names in them. */
425 if (!is_ascii_name(cityname)
426 && (!pconn || pconn->access_level != ALLOW_HACK)) {
427 if (error_buf) {
428 fc_snprintf(error_buf, bufsz,
429 _("%s is not a valid name. Only ASCII or "
430 "ruleset names are allowed for cities."),
431 cityname);
432 }
433 return FALSE;
434 }
435
436
437 return TRUE;
438 }
439
440 /****************************************************************************
441 Come up with a default name when a new city is about to be built. Handle
442 running out of names etc. gracefully. Maybe we should keeptrack of which
443 names have been rejected by the player, so that we do not suggest them
444 again?
445 ****************************************************************************/
city_name_suggestion(struct player * pplayer,struct tile * ptile)446 const char *city_name_suggestion(struct player *pplayer, struct tile *ptile)
447 {
448 struct nation_type *pnation = nation_of_player(pplayer);
449 const char *name;
450
451 log_verbose("Suggesting city name for %s at (%d,%d)",
452 player_name(pplayer), TILE_XY(ptile));
453
454 /* First try default city names. */
455 name = search_for_city_name(ptile, nation_cities(pnation), pplayer);
456 if (NULL != name) {
457 log_debug("Default city name found: %s.", name);
458 return name;
459 }
460
461 /* Not found... Let's try a straightforward algorithm to look through
462 * nations to find a city name.
463 *
464 * We start by adding the player's nation to the queue. Then we proceed:
465 * - Pick a random nation from the queue.
466 * - If it has a valid city name, use that.
467 * - Otherwise, add all parent and child nations to the queue.
468 * - If the queue is empty, add all remaining nations to it and continue.
469 *
470 * Variables used:
471 * - nation_list is a queue of nations to look through.
472 * - nations_selected tells whether each nation is in the queue already
473 * - queue_size gives the size of the queue (number of nations in it).
474 * - i is the current position in the queue.
475 * Note that nations aren't removed from the queue after they're processed.
476 * New nations are just added onto the end. */
477 {
478 struct nation_type *nation_list[nation_count()];
479 bool nations_selected[nation_count()];
480 int queue_size = 1, i = 0, idx;
481
482 memset(nations_selected, 0, sizeof(nations_selected));
483 nation_list[0] = pnation;
484 nations_selected[nation_index(pnation)] = TRUE;
485
486 while (i < nation_count()) {
487 for (; i < queue_size; i++) {
488
489 if (0 < i) {
490 /* Pick a random nation from the queue. */
491 const int which = i + fc_rand(queue_size - i);
492 struct nation_type *tmp = nation_list[i];
493
494 nation_list[i] = nation_list[which];
495 nation_list[which] = tmp;
496
497 pnation = nation_list[i];
498 log_debug("Looking through %s.", nation_rule_name(pnation));
499 name = search_for_city_name(ptile, nation_cities(pnation), pplayer);
500
501 if (NULL != name) {
502 return name;
503 }
504 }
505
506 /* Append the nation's civil war nations into the search tree. */
507 nation_list_iterate(pnation->server.civilwar_nations, n) {
508 idx = nation_index(n);
509 if (!nations_selected[idx]) {
510 nation_list[queue_size] = n;
511 nations_selected[idx] = TRUE;
512 queue_size++;
513 log_debug("Child %s.", nation_rule_name(n));
514 }
515 } nation_list_iterate_end;
516
517 /* Append the nation's parent nations into the search tree. */
518 nation_list_iterate(pnation->server.parent_nations, n) {
519 idx = nation_index(n);
520 if (!nations_selected[idx]) {
521 nation_list[queue_size] = n;
522 nations_selected[idx] = TRUE;
523 queue_size++;
524 log_debug("Parent %s.", nation_rule_name(n));
525 }
526 } nation_list_iterate_end;
527 }
528
529 /* Still not found; append all remaining nations. */
530 allowed_nations_iterate(n) {
531 idx = nation_index(n);
532 if (!nations_selected[idx]) {
533 nation_list[queue_size] = n;
534 nations_selected[nation_index(n)] = TRUE;
535 queue_size++;
536 log_debug("Misc nation %s.", nation_rule_name(n));
537 }
538 } allowed_nations_iterate_end;
539 }
540 }
541
542 /* Not found in rulesets, make a default name. */
543 {
544 static char tempname[MAX_LEN_NAME];
545 int i;
546
547 log_debug("City name not found in rulesets.");
548 for (i = 1; i <= map_num_tiles(); i++ ) {
549 fc_snprintf(tempname, MAX_LEN_NAME, _("City no. %d"), i);
550 if (NULL == game_city_by_name(tempname)) {
551 return tempname;
552 }
553 }
554
555 fc_assert_msg(FALSE, "Failed to generate a city name.");
556 sz_strlcpy(tempname, _("A poorly-named city"));
557 return tempname;
558 }
559 }
560
561 /**************************************************************************
562 calculate the remaining build points
563 **************************************************************************/
build_points_left(struct city * pcity)564 int build_points_left(struct city *pcity)
565 {
566 int cost = impr_build_shield_cost(pcity->production.value.building);
567
568 return cost - pcity->shield_stock;
569 }
570
571 /**************************************************************************
572 How many veteran levels will created unit of this type get?
573 **************************************************************************/
do_make_unit_veteran(struct city * pcity,const struct unit_type * punittype)574 int do_make_unit_veteran(struct city *pcity,
575 const struct unit_type *punittype)
576 {
577 int levels = get_unittype_bonus(city_owner(pcity), pcity->tile, punittype,
578 EFT_VETERAN_BUILD);
579 int max_levels = utype_veteran_levels(punittype) - 1;
580
581 levels = CLIP(0, levels, max_levels);
582
583 return levels;
584 }
585
586 /*********************************************************************
587 Change player that owns a unit and, if appropriate, its home city,
588 with verbose output.
589 If 'rehome' is not set, only change the player which owns the unit
590 (the new owner is new_pcity's owner). Otherwise the new unit will be
591 given a homecity, even if it was homeless before.
592 ***********************************************************************/
transfer_unit(struct unit * punit,struct city * tocity,bool rehome,bool verbose)593 static void transfer_unit(struct unit *punit, struct city *tocity,
594 bool rehome, bool verbose)
595 {
596 struct player *from_player = unit_owner(punit);
597 struct player *to_player = city_owner(tocity);
598
599 /* Transferring a dying GameLoss unit as part of the loot for
600 * killing it caused gna bug #23676. */
601 fc_assert_ret_msg(!punit->server.dying,
602 "Tried to transfer the dying unit %d.",
603 punit->id);
604
605 if (from_player == to_player) {
606 fc_assert_ret(rehome);
607 log_verbose("Changed homecity of %s %s to %s",
608 nation_rule_name(nation_of_player(from_player)),
609 unit_rule_name(punit),
610 city_name_get(tocity));
611 if (verbose) {
612 notify_player(from_player, unit_tile(punit),
613 E_UNIT_RELOCATED, ftc_server,
614 _("Changed homecity of %s to %s."),
615 unit_link(punit),
616 city_link(tocity));
617 }
618 } else {
619 struct tile *utile = unit_tile(punit);
620 struct city *in_city = tile_city(utile);
621
622 if (utype_player_already_has_this_unique(to_player,
623 unit_type_get(punit))) {
624 /* This is a unique unit that to_player already has. A transfer would
625 * break the rule that a player only may have one unit of each unique
626 * unit type. */
627
628 log_debug("%s already have a %s. Can't transfer from %s",
629 nation_rule_name(nation_of_player(to_player)),
630 unit_rule_name(punit),
631 nation_rule_name(nation_of_player(from_player)));
632
633 if (utype_has_flag(unit_type_get(punit), UTYF_GAMELOSS)) {
634 /* Try to save game loss units. */
635 bounce_unit(punit, verbose);
636 } else {
637 /* Kill the unique unit. */
638
639 if (verbose) {
640 notify_player(from_player, unit_tile(punit),
641 E_UNIT_LOST_MISC, ftc_server,
642 /* TRANS: Americans ... Leader */
643 _("The %s already have a %s. Can't transfer yours."),
644 nation_plural_for_player(to_player),
645 unit_tile_link(punit));
646 }
647
648 wipe_unit(punit, ULR_CITY_LOST, NULL);
649 }
650
651 return;
652 }
653
654 if (in_city) {
655 log_verbose("Transferred %s in %s from %s to %s",
656 unit_rule_name(punit), city_name_get(in_city),
657 nation_rule_name(nation_of_player(from_player)),
658 nation_rule_name(nation_of_player(to_player)));
659 if (verbose) {
660 notify_player(from_player, unit_tile(punit),
661 E_UNIT_RELOCATED, ftc_server,
662 _("Transferred %s in %s from %s to %s."),
663 unit_link(punit),
664 city_link(in_city),
665 nation_plural_for_player(from_player),
666 nation_plural_for_player(to_player));
667 }
668 } else if (can_unit_exist_at_tile(punit, tocity->tile)) {
669 log_verbose("Transferred %s from %s to %s",
670 unit_rule_name(punit),
671 nation_rule_name(nation_of_player(from_player)),
672 nation_rule_name(nation_of_player(to_player)));
673 if (verbose) {
674 notify_player(from_player, unit_tile(punit),
675 E_UNIT_RELOCATED, ftc_server,
676 _("Transferred %s from %s to %s."),
677 unit_link(punit),
678 nation_plural_for_player(from_player),
679 nation_plural_for_player(to_player));
680 }
681 } else {
682 log_verbose("Could not transfer %s from %s to %s",
683 unit_rule_name(punit),
684 nation_rule_name(nation_of_player(from_player)),
685 nation_rule_name(nation_of_player(to_player)));
686 if (verbose) {
687 notify_player(from_player, unit_tile(punit),
688 E_UNIT_LOST_MISC, ftc_server,
689 /* TRANS: Polish Destroyer ... German <city> */
690 _("%s %s lost in transfer to %s %s"),
691 nation_adjective_for_player(from_player),
692 unit_tile_link(punit),
693 nation_adjective_for_player(to_player),
694 city_link(tocity));
695 }
696 wipe_unit(punit, ULR_CITY_LOST, NULL);
697 return;
698 }
699
700 maybe_make_contact(utile, to_player);
701 }
702 unit_change_homecity_handling(punit, tocity, rehome);
703 }
704
705 /*********************************************************************
706 When a city is transferred (bought, incited, disbanded, civil war):
707 Units in a transferred city are transferred to the new owner; units
708 supported by the city, but held in other cities are updated to
709 reflect those cities as their new homecity. Units supported
710 by the transferred city that are not in a city tile may be deleted.
711
712 - Kris Bubendorfer <Kris.Bubendorfer@MCS.VUW.AC.NZ>
713
714 pplayer: The player receiving the units if they are not disbanded and
715 are not in a city
716 pvictim: The owner of the city the units are transferred from.
717 units: A list of units to be transferred, typically a city's unit list.
718 pcity: Default city the units are transferred to.
719 exclude_city: The units cannot be transferred to this city.
720 kill_outside: Units outside this range are deleted. -1 means no units
721 are deleted.
722 verbose: Messages are sent to the involved parties.
723 ***********************************************************************/
transfer_city_units(struct player * pplayer,struct player * pvictim,struct unit_list * units,struct city * pcity,struct city * exclude_city,int kill_outside,bool verbose)724 void transfer_city_units(struct player *pplayer, struct player *pvictim,
725 struct unit_list *units, struct city *pcity,
726 struct city *exclude_city,
727 int kill_outside, bool verbose)
728 {
729 struct tile *ptile = pcity->tile;
730 int saved_id = pcity->id;
731 const char *name = city_name_get(pcity);
732
733 /* Transfer enemy units in the city to the new owner.
734 * Only relevant if we are transferring to another player. */
735 if (pplayer != pvictim) {
736 unit_list_iterate_safe((ptile)->units, vunit) {
737 if (vunit->server.dying) {
738 /* Don't transfer or bounce a dying unit. It will soon be gone
739 * anyway.
740 *
741 * Bouncing a dying unit isn't a good idea.
742 * Remaining death handling may do things meant for its current
743 * location to the new location. (Example: Stack death)
744 * bounce_unit() will wipe the unit if it can't be bounced. Wiping
745 * the dying unit isn't a good idea. The remaining death handling
746 * code will try to read from it.
747 *
748 * Transferring a dying GameLoss unit as part of the loot for
749 * killing it caused gna bug #23676. */
750 continue;
751 }
752
753 /* Don't transfer units already owned by new city-owner --wegge */
754 if (unit_owner(vunit) == pvictim) {
755 /* Determine whether unit was homeless. If it was, we don't give
756 * it a homecity, only change ownership.
757 * We have to search the transferred city's former units because
758 * the unit may have been made only temporarily homeless during
759 * city transfer. */
760 bool homeless = (vunit->homecity == 0)
761 && !unit_list_search(units, vunit);
762
763 /* vunit may die during transfer_unit().
764 * unit_list_remove() is still safe using vunit pointer, as
765 * pointer is not used for dereferencing, only as value.
766 * Not sure if it would be safe to unlink first and transfer only
767 * after that. Not sure if it is correct to unlink at all in
768 * some cases, depending which list 'units' points to. */
769 transfer_unit(vunit, pcity, !homeless, verbose);
770 unit_list_remove(units, vunit);
771 } else if (!pplayers_allied(pplayer, unit_owner(vunit))) {
772 /* the owner of vunit is allied to pvictim but not to pplayer */
773 bounce_unit(vunit, verbose);
774 }
775 } unit_list_iterate_safe_end;
776 }
777
778 if (!city_exist(saved_id)) {
779 saved_id = 0;
780 }
781
782 /* Any remaining units supported by the city are either given new home
783 cities or maybe destroyed */
784 unit_list_iterate_safe(units, vunit) {
785 struct city *new_home_city = tile_city(unit_tile(vunit));
786
787 if (vunit->server.dying) {
788 /* Don't transfer or destroy a dying unit. It will soon be gone
789 * anyway.
790 *
791 * Transfering a dying GameLoss unit as part of the loot for
792 * killing it caused gna bug #23676. */
793 continue;
794 }
795
796 if (new_home_city && new_home_city != exclude_city
797 && city_owner(new_home_city) == unit_owner(vunit)) {
798 /* unit is in another city: make that the new homecity,
799 * unless that city is actually the same city (happens if disbanding)
800 * Unit had a homecity since it was supported, so rehome. */
801 transfer_unit(vunit, new_home_city, TRUE, verbose);
802 } else if ((kill_outside == -1
803 || real_map_distance(unit_tile(vunit), ptile) <= kill_outside)
804 && saved_id) {
805 /* else transfer to specified city. */
806 transfer_unit(vunit, pcity, TRUE, verbose);
807 if (unit_tile(vunit) == ptile && !pplayers_allied(pplayer, pvictim)) {
808 /* Unit is inside city being transferred, bounce it */
809 bounce_unit(vunit, TRUE);
810 }
811 } else {
812 /* The unit is lost. Call notify_player (in all other cases it is
813 * called automatically). */
814 log_verbose("Lost %s %s at (%d,%d) when %s was lost.",
815 nation_rule_name(nation_of_unit(vunit)),
816 unit_rule_name(vunit), TILE_XY(unit_tile(vunit)), name);
817 if (verbose) {
818 notify_player(unit_owner(vunit), unit_tile(vunit),
819 E_UNIT_LOST_MISC, ftc_server,
820 _("%s lost along with control of %s."),
821 unit_tile_link(vunit), name);
822 }
823 wipe_unit(vunit, ULR_CITY_LOST, NULL);
824 }
825 } unit_list_iterate_safe_end;
826
827 #ifdef DEBUG
828 unit_list_iterate(pcity->units_supported, punit) {
829 if (punit->server.dying) {
830 /* Leave the dying alone. */
831 continue;
832 }
833
834 fc_assert(punit->homecity == pcity->id);
835 fc_assert(unit_owner(punit) == pplayer);
836 } unit_list_iterate_end;
837 #endif /* DEBUG */
838 }
839
840 /****************************************************************************
841 Find the city closest to 'ptile'. Some restrictions can be applied:
842
843 'pexclcity' not this city
844 'pplayer' player to be used by 'only_known', 'only_player' and
845 'only_enemy'.
846 'only_ocean' if set the city must be adjacent to ocean.
847 'only_continent' if set only cities on the same continent as 'ptile' are
848 valid.
849 'only_known' if set only cities known to 'pplayer' are considered.
850 'only_player' if set and 'pplayer' is not NULL only cities of this
851 player are returned.
852 'only_enemy' if set and 'pplayer' is not NULL only cities of players
853 which are at war with 'pplayer' are returned.
854 'pclass' if set, and 'pclass' is not NULL only cities that have
855 adjacent native terrain for that unit class are returned.
856
857 If no city is found NULL is returned.
858 ****************************************************************************/
find_closest_city(const struct tile * ptile,const struct city * pexclcity,const struct player * pplayer,bool only_ocean,bool only_continent,bool only_known,bool only_player,bool only_enemy,const struct unit_class * pclass)859 struct city *find_closest_city(const struct tile *ptile,
860 const struct city *pexclcity,
861 const struct player *pplayer,
862 bool only_ocean, bool only_continent,
863 bool only_known, bool only_player,
864 bool only_enemy, const struct unit_class *pclass)
865 {
866 Continent_id con;
867 struct city *best_city = NULL;
868 int best_dist = -1;
869
870 fc_assert_ret_val(ptile != NULL, NULL);
871
872 if (pplayer != NULL && only_player && only_enemy) {
873 log_error("Non of my own cities will be at war with me!");
874 return NULL;
875 }
876
877 con = tile_continent(ptile);
878
879 players_iterate(aplayer) {
880 if (pplayer != NULL && only_player && pplayer != aplayer) {
881 /* only cities of player 'pplayer' */
882 continue;
883 }
884
885 if (pplayer != NULL && only_enemy
886 && !pplayers_at_war(pplayer, aplayer)) {
887 /* only cities of players at war with player 'pplayer' */
888 continue;
889 }
890
891 city_list_iterate(aplayer->cities, pcity) {
892 int city_dist;
893
894 if (pexclcity && pexclcity == pcity) {
895 /* not this city */
896 continue;
897 }
898
899 city_dist = real_map_distance(ptile, city_tile(pcity));
900
901 /* Find the closest city matching the requirements.
902 * - closer than the current best city
903 * - (if required) on the same continent
904 * - (if required) adjacent to ocean
905 * - (if required) only cities known by the player
906 * - (if required) only cities native to the class */
907 if ((best_dist == -1 || city_dist < best_dist)
908 && (!only_continent || con == tile_continent(pcity->tile))
909 && (!only_ocean || is_terrain_class_near_tile(city_tile(pcity), TC_OCEAN))
910 && (!only_known
911 || (map_is_known(city_tile(pcity), pplayer)
912 && map_get_player_site(city_tile(pcity), pplayer)->identity
913 > IDENTITY_NUMBER_ZERO))
914 && (pclass == NULL
915 || is_native_near_tile(pclass, city_tile(pcity)))) {
916 best_dist = city_dist;
917 best_city = pcity;
918 }
919 } city_list_iterate_end;
920 } players_iterate_end;
921
922 return best_city;
923 }
924
925 /**************************************************************************
926 called when a player conquers a city, remove buildings (not wonders and
927 always palace) with game.server.razechance% chance, barbarians destroy more
928 set the city's shield stock to 0
929 **************************************************************************/
raze_city(struct city * pcity)930 static void raze_city(struct city *pcity)
931 {
932 int razechance = game.server.razechance;
933
934 /* land barbarians are more likely to destroy city improvements */
935 if (is_land_barbarian(city_owner(pcity)))
936 razechance += 30;
937
938 city_built_iterate(pcity, pimprove) {
939 /* Small wonders should have already been removed by
940 * transfer_city() (with 100% probability). */
941 fc_assert(!is_small_wonder(pimprove));
942 if (is_improvement(pimprove) && (fc_rand(100) < razechance)) {
943 city_remove_improvement(pcity, pimprove);
944 }
945 } city_built_iterate_end;
946
947 nullify_prechange_production(pcity);
948 pcity->shield_stock = 0;
949 }
950
951 /***************************************************************************
952 The following has to be called every time AFTER a city (pcity) has changed
953 owner to update the city's trade routes.
954 ***************************************************************************/
reestablish_city_trade_routes(struct city * pcity)955 static void reestablish_city_trade_routes(struct city *pcity)
956 {
957 trade_routes_iterate(pcity, ptrade_city) {
958 bool keep_route;
959
960 /* Remove the city's trade routes (old owner).
961 * Do not announce removal as we might restore the route immediately below */
962 remove_trade_route(ptrade_city, pcity, FALSE, FALSE);
963
964 keep_route = can_cities_trade(pcity, ptrade_city)
965 && can_establish_trade_route(pcity, ptrade_city);
966
967 if (!keep_route) {
968 enum trade_route_type type = cities_trade_route_type(pcity, ptrade_city);
969 struct trade_route_settings *settings = trade_route_settings_by_type(type);
970
971 if (settings->cancelling != TRI_CANCEL) {
972 keep_route = TRUE;
973 }
974 }
975
976 /* Readd the city's trade route (new owner) */
977 if (keep_route) {
978 establish_trade_route(pcity, ptrade_city);
979 } else {
980 /* Now announce the traderoute removal */
981 announce_trade_route_removal(pcity, ptrade_city, FALSE);
982 }
983
984 /* refresh regardless; either it lost a trade route or the trade
985 * route revenue changed. */
986 city_refresh(ptrade_city);
987 send_city_info(city_owner(ptrade_city), ptrade_city);
988
989 /* Give the new owner infos about the city which has a trade route
990 * with the transferred city. */
991 map_show_tile(city_owner(pcity), ptrade_city->tile);
992 update_dumb_city(city_owner(pcity), ptrade_city);
993 send_city_info(city_owner(pcity), ptrade_city);
994 } trade_routes_iterate_end;
995 }
996
997 /**************************************************************************
998 Create saved small wonders in random cities. Usually used to save the
999 palace when the capital was conquered. Respects the 'savepalace'
1000 server setting.
1001 **************************************************************************/
build_free_small_wonders(struct player * pplayer,bv_imprs * had_small_wonders)1002 static void build_free_small_wonders(struct player *pplayer,
1003 bv_imprs *had_small_wonders)
1004 {
1005 int size = city_list_size(pplayer->cities);
1006
1007 if (!game.server.savepalace) {
1008 /* Nothing to do */
1009 return;
1010 }
1011
1012 if (size == 0) {
1013 /* The last city was removed or transferred to the enemy.
1014 * If the victim survives to found or acquire another city, they'll
1015 * get any savepalace initial buildings then. */
1016 return;
1017 }
1018
1019 improvement_iterate(pimprove) {
1020 if (improvement_has_flag(pimprove, IF_SAVE_SMALL_WONDER)
1021 && BV_ISSET(*had_small_wonders, improvement_index(pimprove))) {
1022 /* FIXME: instead, find central city */
1023 struct city *pnew_city = city_list_get(pplayer->cities, fc_rand(size));
1024
1025 fc_assert_action(NULL == city_from_small_wonder(pplayer, pimprove),
1026 continue);
1027
1028 city_add_improvement(pnew_city, pimprove);
1029
1030 /*
1031 * send_player_cities will recalculate all cities and send them to
1032 * the client.
1033 */
1034 send_player_cities(pplayer);
1035
1036 notify_player(pplayer, city_tile(pnew_city), E_IMP_BUILD, ftc_server,
1037 /* FIXME: should already be notified about city loss? */
1038 /* TRANS: <building> ... <city> */
1039 _("A replacement %s was built in %s."),
1040 improvement_name_translation(pimprove),
1041 city_link(pnew_city));
1042 /*
1043 * The enemy want to see the new capital in his intelligence
1044 * report.
1045 */
1046 send_city_info(NULL, pnew_city);
1047 }
1048 } improvement_iterate_end;
1049 }
1050
1051 /**********************************************************************
1052 Handles all transactions in relation to transferring a city.
1053
1054 The kill_outside and transfer_unit_verbose arguments are passed to
1055 transfer_city_units(), which is called in the middle of the function.
1056
1057 Return TRUE iff the city remains after transfering (the city may be
1058 destroyed by a script, notably with bouncing or wiping units).
1059 ***********************************************************************/
transfer_city(struct player * ptaker,struct city * pcity,int kill_outside,bool transfer_unit_verbose,bool resolve_stack,bool raze,bool build_free)1060 bool transfer_city(struct player *ptaker, struct city *pcity,
1061 int kill_outside, bool transfer_unit_verbose,
1062 bool resolve_stack, bool raze, bool build_free)
1063 {
1064 char old_city_name[MAX_LEN_NAME];
1065 bv_imprs had_small_wonders;
1066 struct vision *old_vision, *new_vision;
1067 struct unit_list *old_city_units = unit_list_new();
1068 struct player *pgiver = city_owner(pcity);
1069 struct tile *pcenter = city_tile(pcity);
1070 int saved_id = pcity->id;
1071 bool city_remains = TRUE;
1072 bool had_great_wonders = FALSE;
1073 const citizens old_taker_content_citizens = player_content_citizens(ptaker);
1074 const citizens old_giver_content_citizens = player_content_citizens(pgiver);
1075 const citizens old_taker_angry_citizens = player_angry_citizens(ptaker);
1076 const citizens old_giver_angry_citizens = player_angry_citizens(pgiver);
1077 bool taker_had_no_cities = (city_list_size(ptaker->cities) == 0);
1078 bool new_extras;
1079 const int units_num = unit_list_size(pcenter->units);
1080 bv_player *could_see_unit = (units_num > 0
1081 ? fc_malloc(sizeof(*could_see_unit)
1082 * units_num)
1083 : NULL);
1084 int i;
1085
1086 fc_assert_ret_val(pgiver != ptaker, TRUE);
1087
1088 /* Remember what player see what unit. */
1089 i = 0;
1090 unit_list_iterate(pcenter->units, aunit) {
1091 BV_CLR_ALL(could_see_unit[i]);
1092 players_iterate(aplayer) {
1093 if (can_player_see_unit(aplayer, aunit)) {
1094 BV_SET(could_see_unit[i], player_index(aplayer));
1095 }
1096 } players_iterate_end;
1097 i++;
1098 } unit_list_iterate_end;
1099 fc_assert(i == units_num);
1100
1101 /* Remove AI control of the old owner. */
1102 CALL_PLR_AI_FUNC(city_lost, pcity->owner, pcity->owner, pcity);
1103
1104 /* Forget old tasks */
1105 clear_worker_tasks(pcity);
1106
1107 /* Activate AI control of the new owner. */
1108 CALL_PLR_AI_FUNC(city_got, ptaker, ptaker, pcity);
1109
1110 city_freeze_workers(pcity);
1111
1112 unit_list_iterate(pcity->units_supported, punit) {
1113 unit_list_prepend(old_city_units, punit);
1114 /* Mark unit to have no homecity at all.
1115 * 1. We remove unit from units_supported list here,
1116 * real_change_unit_homecity() should not attempt it.
1117 * 2. Otherwise we might delete the homecity from under the unit
1118 * in the client */
1119 punit->homecity = 0;
1120 send_unit_info(NULL, punit);
1121 } unit_list_iterate_end;
1122 unit_list_clear(pcity->units_supported);
1123
1124 /* Remove all global improvement effects that this city confers (but
1125 then restore the local improvement list - we need this to restore the
1126 global effects for the new city owner) */
1127 BV_CLR_ALL(had_small_wonders);
1128 city_built_iterate(pcity, pimprove) {
1129 city_remove_improvement(pcity, pimprove);
1130
1131 if (is_small_wonder(pimprove)) {
1132 BV_SET(had_small_wonders, improvement_index(pimprove));
1133 } else {
1134 if (is_great_wonder(pimprove)) {
1135 had_great_wonders = TRUE;
1136 }
1137 /* note: internal turn here, next city_built_iterate(). */
1138 pcity->built[improvement_index(pimprove)].turn = game.info.turn; /*I_ACTIVE*/
1139 }
1140 } city_built_iterate_end;
1141
1142 give_citymap_from_player_to_player(pcity, pgiver, ptaker);
1143 old_vision = pcity->server.vision;
1144 new_vision = vision_new(ptaker, pcenter);
1145 pcity->server.vision = new_vision;
1146 vision_reveal_tiles(new_vision, game.server.vision_reveal_tiles);
1147 vision_change_sight(new_vision, old_vision->radius_sq);
1148
1149 ASSERT_VISION(new_vision);
1150
1151 sz_strlcpy(old_city_name, city_name_get(pcity));
1152 if (CNM_PLAYER_UNIQUE == game.server.allowed_city_names
1153 && city_list_find_name(ptaker->cities, city_name_get(pcity))) {
1154 sz_strlcpy(pcity->name,
1155 city_name_suggestion(ptaker, pcenter));
1156 notify_player(ptaker, pcenter, E_BAD_COMMAND, ftc_server,
1157 _("You already had a city called %s."
1158 " The city was renamed to %s."),
1159 old_city_name,
1160 city_link(pcity));
1161 }
1162
1163 /* Has to follow the unfog call above. */
1164 city_list_remove(pgiver->cities, pcity);
1165 map_clear_border(pcenter);
1166 /* city_thaw_workers_queue() later */
1167
1168 pcity->owner = ptaker;
1169 map_claim_ownership(pcenter, ptaker, pcenter, TRUE);
1170 city_list_prepend(ptaker->cities, pcity);
1171
1172 /* Hide/reveal units. Do it after vision have been given to taker, city
1173 * owner has been changed, and before any script could be spawned. */
1174 i = 0;
1175 unit_list_iterate(pcenter->units, aunit) {
1176 players_iterate(aplayer) {
1177 if (can_player_see_unit(aplayer, aunit)) {
1178 if (!BV_ISSET(could_see_unit[i], player_index(aplayer))
1179 && !aunit->server.dying) {
1180 /* Reveal 'aunit'. */
1181 send_unit_info(aplayer->connections, aunit);
1182 }
1183 } else {
1184 if (BV_ISSET(could_see_unit[i], player_index(aplayer))) {
1185 /* Hide 'aunit'. */
1186 unit_goes_out_of_sight(aplayer, aunit);
1187 }
1188 }
1189 } players_iterate_end;
1190 i++;
1191 } unit_list_iterate_end;
1192 fc_assert(i == units_num);
1193 free(could_see_unit);
1194 could_see_unit = NULL;
1195
1196 transfer_city_units(ptaker, pgiver, old_city_units,
1197 pcity, NULL,
1198 kill_outside, transfer_unit_verbose);
1199 /* The units themselves are allready freed by transfer_city_units. */
1200 unit_list_destroy(old_city_units);
1201
1202 if (resolve_stack) {
1203 resolve_unit_stacks(pgiver, ptaker, transfer_unit_verbose);
1204 }
1205
1206 if (! city_exist(saved_id)) {
1207 city_remains = FALSE;
1208 }
1209
1210 if (city_remains) {
1211 /* Update the city's trade routes. */
1212 reestablish_city_trade_routes(pcity);
1213
1214 city_refresh(pcity);
1215 }
1216
1217 /*
1218 * maybe_make_contact() MUST be called before city_map_update_all(),
1219 * since the diplomacy status can influence whether a tile is available.
1220 */
1221 maybe_make_contact(pcenter, ptaker);
1222
1223 if (city_remains) {
1224 struct extra_type *upgradet;
1225
1226 if (raze) {
1227 raze_city(pcity);
1228 }
1229
1230 if (taker_had_no_cities) {
1231 /* If conqueror previously had no cities, we might need to give
1232 * them a palace etc */
1233 if (build_free) {
1234 city_build_free_buildings(pcity);
1235 } /* else caller should probably ensure palace is built */
1236 ptaker->server.got_first_city = TRUE;
1237 }
1238
1239 /* Restore any global improvement effects that this city confers */
1240 city_built_iterate(pcity, pimprove) {
1241 city_add_improvement(pcity, pimprove);
1242 } city_built_iterate_end;
1243
1244 /* Set production to something valid for pplayer, if not.
1245 * (previously allowed building obsolete units.) */
1246 if (!can_city_build_now(pcity, &pcity->production)) {
1247 advisor_choose_build(ptaker, pcity);
1248 }
1249
1250 /* What wasn't obsolete for the old owner may be so now. */
1251 remove_obsolete_buildings_city(pcity, TRUE);
1252
1253 new_extras = upgrade_city_extras(pcity, &upgradet);
1254
1255 if (new_extras) {
1256 const char *clink = city_link(pcity);
1257
1258 notify_player(ptaker, pcenter, E_CITY_TRANSFER, ftc_server,
1259 _("The people in %s are stunned by your "
1260 "technological insight!"),
1261 clink);
1262
1263 if (upgradet != NULL) {
1264 notify_player(ptaker, pcenter, E_CITY_TRANSFER, ftc_server,
1265 _("Workers spontaneously gather and upgrade "
1266 "%s with %s."),
1267 clink, extra_name_translation(upgradet));
1268 } else {
1269 notify_player(ptaker, pcenter, E_CITY_TRANSFER, ftc_server,
1270 _("Workers spontaneously gather and upgrade "
1271 "%s infrastructure."),
1272 clink);
1273 }
1274 update_tile_knowledge(pcenter);
1275 }
1276
1277 /* Build a new palace for free if the player lost her capital and
1278 savepalace is on. */
1279 build_free_small_wonders(pgiver, &had_small_wonders);
1280
1281 /* Refresh the city's vision range, since it might be different
1282 * under the new owner. */
1283 city_refresh_vision(pcity);
1284
1285 /* Update the national borders, within the current vision and culture.
1286 * This could leave a border ring around the city, updated later by
1287 * map_calculate_borders() at the next turn.
1288 */
1289 map_claim_border(pcenter, ptaker, -1);
1290 /* city_thaw_workers_queue() later */
1291
1292 auto_arrange_workers(pcity); /* does city_map_update_all() */
1293 city_thaw_workers(pcity);
1294 city_thaw_workers_queue(); /* after old city has a chance to work! */
1295 city_refresh_queue_add(pcity);
1296 /* no sanity check here as the city is not refreshed! */
1297 }
1298
1299 if (city_remains) {
1300 /* Send city with updated owner information to giver and to everyone
1301 * having shared vision pact with him/her before (s)he may
1302 * lose vision to it. When we later send info to everybody seeing the city,
1303 * (s)he may not be included. */
1304 send_city_info(NULL, pcity);
1305 }
1306
1307 /* Remove the sight points from the giver. */
1308 vision_clear_sight(old_vision);
1309 vision_free(old_vision);
1310
1311 /* Send wonder infos. */
1312 if (had_great_wonders) {
1313 send_game_info(NULL);
1314 if (city_remains) {
1315 send_player_info_c(ptaker, NULL);
1316
1317 /* Refresh all cities of the taker to account for possible changes due
1318 * to player wide effects. */
1319 city_list_iterate(ptaker->cities, acity) {
1320 city_refresh_queue_add(acity);
1321 } city_list_iterate_end;
1322 } else {
1323 /* Refresh all cities to account for possible global effects. */
1324 cities_iterate(acity) {
1325 city_refresh_queue_add(acity);
1326 } cities_iterate_end;
1327 }
1328 }
1329 if (BV_ISSET_ANY(had_small_wonders) || had_great_wonders) {
1330 /* No need to send to detached connections. */
1331 send_player_info_c(pgiver, NULL);
1332
1333 /* Refresh all cities of the giver to account for possible changes due
1334 * to player wide effects. */
1335 city_list_iterate(pgiver->cities, acity) {
1336 city_refresh_queue_add(acity);
1337 } city_list_iterate_end;
1338 }
1339
1340 /* Refresh all cities in the queue. */
1341 city_refresh_queue_processing();
1342 /* After the refresh the sanity check can be done. */
1343 sanity_check_city(pcity);
1344
1345 if (city_remains) {
1346 /* Send information about conquered city to all players. */
1347 send_city_info(NULL, pcity);
1348 }
1349
1350 /* We may cross the EFT_EMPIRE_SIZE_* effects, then we will have to
1351 * refresh all cities for the player. */
1352 if (old_taker_content_citizens != player_content_citizens(ptaker)
1353 || old_taker_angry_citizens != player_angry_citizens(ptaker)) {
1354 city_refresh_for_player(ptaker);
1355 }
1356 if (old_giver_content_citizens != player_content_citizens(pgiver)
1357 || old_giver_angry_citizens != player_angry_citizens(pgiver)) {
1358 city_refresh_for_player(pgiver);
1359 }
1360
1361 sync_cities();
1362
1363 return city_remains;
1364 }
1365
1366 /****************************************************************************
1367 Give to a new city the free (initial) buildings.
1368 Call this when a player has just acquired a city (or batch of cities,
1369 e.g. civil war) after having no cities.
1370 Doesn't check for building uniqueness! -- assumes player has no other
1371 cities which might contain unique buildings.
1372 ****************************************************************************/
city_build_free_buildings(struct city * pcity)1373 void city_build_free_buildings(struct city *pcity)
1374 {
1375 struct player *pplayer;
1376 struct nation_type *nation;
1377 int i;
1378 bool has_small_wonders, has_great_wonders;
1379 bool first_city;
1380
1381 fc_assert_ret(NULL != pcity);
1382 pplayer = city_owner(pcity);
1383 fc_assert_ret(NULL != pplayer);
1384 nation = nation_of_player(pplayer);
1385 fc_assert_ret(NULL != nation);
1386
1387 /* If this isn't the first city a player has ever had, they only get
1388 * any initial buildings with the SaveSmallWonder flag, and then only
1389 * if savepalace is enabled. */
1390 first_city = !pplayer->server.got_first_city;
1391
1392 has_small_wonders = FALSE;
1393 has_great_wonders = FALSE;
1394
1395 /* Global free buildings. */
1396 for (i = 0; i < MAX_NUM_BUILDING_LIST; i++) {
1397 Impr_type_id n = game.rgame.global_init_buildings[i];
1398 struct impr_type *pimprove;
1399
1400 if (n == B_LAST) {
1401 break;
1402 }
1403
1404 pimprove = improvement_by_number(n);
1405 fc_assert_action(!is_great_wonder(pimprove), continue);
1406 if (first_city ||
1407 (game.server.savepalace
1408 && improvement_has_flag(pimprove, IF_SAVE_SMALL_WONDER))) {
1409 city_add_improvement(pcity, pimprove);
1410 if (is_small_wonder(pimprove)) {
1411 has_small_wonders = TRUE;
1412 }
1413 }
1414 }
1415
1416 /* Nation specific free buildings. */
1417 for (i = 0; i < MAX_NUM_BUILDING_LIST; i++) {
1418 Impr_type_id n = nation->init_buildings[i];
1419 struct impr_type *pimprove;
1420
1421 if (n == B_LAST) {
1422 break;
1423 }
1424
1425 pimprove = improvement_by_number(n);
1426 if (first_city ||
1427 (game.server.savepalace
1428 && improvement_has_flag(pimprove, IF_SAVE_SMALL_WONDER))) {
1429 city_add_improvement(pcity, pimprove);
1430 if (is_small_wonder(pimprove)) {
1431 has_small_wonders = TRUE;
1432 } else if (is_great_wonder(pimprove)) {
1433 has_great_wonders = TRUE;
1434 }
1435 }
1436 }
1437
1438 /* Update wonder infos. */
1439 if (has_great_wonders) {
1440 send_game_info(NULL);
1441 /* No need to send to detached connections. */
1442 send_player_info_c(pplayer, NULL);
1443 } else if (has_small_wonders) {
1444 /* No need to send to detached connections. */
1445 send_player_info_c(pplayer, NULL);
1446 }
1447 }
1448
1449 /**************************************************************************
1450 Creates real city.
1451 **************************************************************************/
create_city(struct player * pplayer,struct tile * ptile,const char * name,struct player * nationality)1452 void create_city(struct player *pplayer, struct tile *ptile,
1453 const char *name, struct player *nationality)
1454 {
1455 struct player *saved_owner = tile_owner(ptile);
1456 struct tile *saved_claimer = tile_claimer(ptile);
1457 struct city *pwork = tile_worked(ptile);
1458 struct city *pcity;
1459 const citizens old_content_citizens = player_content_citizens(pplayer);
1460 const citizens old_angry_citizens = player_angry_citizens(pplayer);
1461
1462 log_debug("create_city() %s", name);
1463
1464 pcity = create_city_virtual(pplayer, ptile, name);
1465
1466 /* Remove units no more seen. Do it before city is really put into the
1467 * game. */
1468 players_iterate(other_player) {
1469 if (can_player_see_units_in_city(other_player, pcity)
1470 || !map_is_known_and_seen(ptile, other_player, V_MAIN)) {
1471 continue;
1472 }
1473 unit_list_iterate(ptile->units, punit) {
1474 if (can_player_see_unit(other_player, punit)) {
1475 unit_goes_out_of_sight(other_player, punit);
1476 }
1477 } unit_list_iterate_end;
1478 } players_iterate_end;
1479
1480 adv_city_alloc(pcity);
1481
1482 tile_set_owner(ptile, pplayer, ptile); /* temporarily */
1483 city_choose_build_default(pcity);
1484 pcity->id = identity_number();
1485
1486 fc_allocate_mutex(&game.server.mutexes.city_list);
1487 idex_register_city(pcity);
1488 fc_release_mutex(&game.server.mutexes.city_list);
1489
1490 if (city_list_size(pplayer->cities) == 0) {
1491 /* Free initial buildings, or at least a palace if they were
1492 * previously careless enough to lose all their cities */
1493 city_build_free_buildings(pcity);
1494 pplayer->server.got_first_city = TRUE;
1495 }
1496
1497 /* Set up citizens nationality. */
1498 citizens_init(pcity);
1499
1500 /* Place a worker at the is_city_center() is_free_worked().
1501 * It is possible to build a city on a tile that is already worked;
1502 * this will displace the worker on the newly-built city's tile -- Syela */
1503 tile_set_worked(ptile, pcity); /* instead of city_map_update_worker() */
1504
1505 if (NULL != pwork) {
1506 /* was previously worked by another city */
1507 /* Turn citizen into specialist. */
1508 pwork->specialists[DEFAULT_SPECIALIST]++;
1509 /* One less citizen. Citizen sanity will be handled later in
1510 * city_thaw_workers_queue() */
1511 pwork->server.synced = FALSE;
1512 city_freeze_workers_queue(pwork);
1513 }
1514
1515 /* Update citizens. */
1516 citizens_update(pcity, nationality);
1517
1518 /* Restore the old-owner information so removal
1519 * of territory claiming bases can work relative to it. */
1520 tile_set_owner(ptile, saved_owner, saved_claimer);
1521
1522 /* Destroy any extras that don't belong in the city. */
1523 extra_type_iterate(pextra) {
1524 if (tile_has_extra(ptile, pextra)
1525 && !is_native_tile_to_extra(pextra, ptile)) {
1526 destroy_extra(ptile, pextra);
1527 }
1528 } extra_type_iterate_end;
1529
1530 /* Build any extras that the city should have. */
1531 upgrade_city_extras(pcity, NULL);
1532
1533 /* Claim the ground we stand on */
1534 map_claim_ownership(ptile, pplayer, ptile, TRUE);
1535
1536 /* Before arranging workers to show unknown land */
1537 pcity->server.vision = vision_new(pplayer, ptile);
1538 vision_reveal_tiles(pcity->server.vision, game.server.vision_reveal_tiles);
1539 city_refresh_vision(pcity);
1540 city_list_prepend(pplayer->cities, pcity);
1541
1542 /* This is dependent on the current vision, so must be done after
1543 * vision is prepared and before arranging workers. */
1544 map_claim_border(ptile, pplayer, -1);
1545 /* city_thaw_workers_queue() later */
1546
1547 /* Refresh the city. First a city refresh is done (this shouldn't
1548 * send any packets to the client because the city has no supported units)
1549 * then rearrange the workers. Note that auto_arrange_workers does its
1550 * own refresh call; it is safest to do our own controlled city_refresh
1551 * first. */
1552 city_refresh(pcity);
1553 auto_arrange_workers(pcity);
1554 city_thaw_workers_queue(); /* after new city has a chance to work! */
1555 city_refresh_queue_processing();
1556
1557 /* Bases destroyed earlier may have had watchtower effect. Refresh
1558 * unit vision. */
1559 unit_list_refresh_vision(ptile->units);
1560
1561 update_tile_knowledge(ptile);
1562
1563 if (old_content_citizens != player_content_citizens(pplayer)
1564 || old_angry_citizens != player_angry_citizens(pplayer)) {
1565 /* We crossed the EFT_EMPIRE_SIZE_* effects, we have to refresh all
1566 * cities for the player. */
1567 city_refresh_for_player(pplayer);
1568 }
1569
1570 pcity->server.synced = FALSE;
1571 send_city_info(NULL, pcity);
1572 sync_cities(); /* Will also send pwork. */
1573
1574 notify_player(pplayer, ptile, E_CITY_BUILD, ftc_server,
1575 _("You have founded %s."),
1576 city_link(pcity));
1577 maybe_make_contact(ptile, city_owner(pcity));
1578
1579 unit_list_iterate((ptile)->units, punit) {
1580 struct city *home = game_city_by_number(punit->homecity);
1581
1582 /* Catch fortress building, transforming into ocean, etc. */
1583 if (!can_unit_continue_current_activity(punit)) {
1584 unit_activity_handling(punit, ACTIVITY_IDLE);
1585 }
1586
1587 /* Update happiness (the unit may no longer cause unrest). */
1588 if (home) {
1589 if (city_refresh(home)) {
1590 /* Shouldn't happen, but better be safe than sorry. */
1591 auto_arrange_workers(home);
1592 }
1593 sanity_check_city(home);
1594 send_city_info(city_owner(home), home);
1595 }
1596 } unit_list_iterate_end;
1597
1598 sanity_check_city(pcity);
1599
1600 script_server_signal_emit("city_built", 1,
1601 API_TYPE_CITY, pcity);
1602
1603 CALL_PLR_AI_FUNC(city_got, pplayer, pplayer, pcity);
1604 }
1605
1606 /**************************************************************************
1607 Remove a city from the game.
1608 **************************************************************************/
remove_city(struct city * pcity)1609 void remove_city(struct city *pcity)
1610 {
1611 struct player *powner = city_owner(pcity);
1612 struct tile *pcenter = city_tile(pcity);
1613 bv_imprs had_small_wonders;
1614 struct vision *old_vision;
1615 int id = pcity->id; /* We need this even after memory has been freed */
1616 bool had_great_wonders = FALSE;
1617 const citizens old_content_citizens = player_content_citizens(powner);
1618 const citizens old_angry_citizens = player_angry_citizens(powner);
1619 struct dbv tile_processed;
1620 struct tile_list *process_queue;
1621 const char *ctl = city_tile_link(pcity);
1622
1623 CALL_PLR_AI_FUNC(city_lost, powner, powner, pcity);
1624
1625 BV_CLR_ALL(had_small_wonders);
1626 city_built_iterate(pcity, pimprove) {
1627 city_remove_improvement(pcity, pimprove);
1628
1629 if (is_small_wonder(pimprove)) {
1630 BV_SET(had_small_wonders, improvement_index(pimprove));
1631 } else if (is_great_wonder(pimprove)) {
1632 had_great_wonders = TRUE;
1633 }
1634 } city_built_iterate_end;
1635
1636 /* Rehome units in other cities */
1637 unit_list_iterate_safe(pcity->units_supported, punit) {
1638 struct city *new_home_city = tile_city(unit_tile(punit));
1639
1640 if (new_home_city
1641 && new_home_city != pcity
1642 && city_owner(new_home_city) == powner
1643 && !punit->server.dying) {
1644 transfer_unit(punit, new_home_city, TRUE, TRUE);
1645 }
1646 } unit_list_iterate_safe_end;
1647
1648 /* make sure ships are not left on land when city is removed. */
1649 unit_list_iterate_safe(pcenter->units, punit) {
1650 bool moved;
1651 struct unit_type *punittype = unit_type_get(punit);
1652
1653 if (is_native_tile(punittype, pcenter)) {
1654 continue;
1655 }
1656
1657 unit_activity_handling(punit, ACTIVITY_IDLE);
1658 moved = FALSE;
1659 adjc_iterate(pcenter, tile1) {
1660 if (!moved && is_native_tile(punittype, tile1)) {
1661 if (adv_could_unit_move_to_tile(punit, tile1) == 1) {
1662 /* Move */
1663 moved = unit_move_handling(punit, tile1, FALSE, TRUE, NULL);
1664 if (moved) {
1665 notify_player(unit_owner(punit), tile1,
1666 E_UNIT_RELOCATED, ftc_server,
1667 _("Moved %s out of disbanded city %s "
1668 "since it cannot stay on %s."),
1669 unit_link(punit), ctl,
1670 terrain_name_translation(tile_terrain(pcenter)));
1671 break;
1672 }
1673 }
1674 }
1675 } adjc_iterate_end;
1676 if (!moved) {
1677 notify_player(unit_owner(punit), unit_tile(punit),
1678 E_UNIT_LOST_MISC, ftc_server,
1679 _("When %s was disbanded your %s could not "
1680 "get out, and it was therefore lost."),
1681 ctl,
1682 unit_tile_link(punit));
1683 wipe_unit(punit, ULR_CITY_LOST, NULL);
1684 }
1685 } unit_list_iterate_safe_end;
1686
1687 process_queue = tile_list_new();
1688 dbv_init(&tile_processed, map_num_tiles());
1689 for (tile_list_append(process_queue, pcenter); tile_list_size(process_queue) > 0;) {
1690 struct tile *ptile = tile_list_front(process_queue);
1691 tile_list_pop_front(process_queue);
1692 dbv_set(&tile_processed, tile_index(ptile));
1693 adjc_iterate(ptile, piter) {
1694 struct city *other_city;
1695
1696 if (dbv_isset(&tile_processed, tile_index(piter))) {
1697 continue;
1698 }
1699 other_city = tile_city(piter);
1700 if (other_city != NULL) {
1701 /* Adjacent tile has a city that may have been part of same channel */
1702 dbv_set(&tile_processed, tile_index(piter));
1703 tile_list_append(process_queue, piter);
1704 unit_list_iterate_safe(piter->units, punit) {
1705 struct unit_class *pclass = utype_class(punit->utype);
1706
1707 if (!uclass_has_flag(pclass, UCF_BUILD_ANYWHERE)
1708 && !is_native_tile(punit->utype, piter)
1709 && !is_city_channel_tile(pclass, piter, pcenter)) {
1710 notify_player(unit_owner(punit), unit_tile(punit),
1711 E_UNIT_LOST_MISC, ftc_server,
1712 _("When %s was disbanded your %s in %s was trapped, "
1713 "and it was therefore lost."),
1714 ctl,
1715 unit_tile_link(punit),
1716 city_link(other_city));
1717 wipe_unit(punit, ULR_CITY_LOST, NULL);
1718 }
1719 } unit_list_iterate_safe_end;
1720 } else {
1721 dbv_set(&tile_processed, tile_index(piter));
1722 }
1723 } adjc_iterate_end;
1724 }
1725
1726 dbv_free(&tile_processed);
1727 tile_list_destroy(process_queue);
1728
1729 if (!city_exist(id)) {
1730 /* Wiping trapped units caused city to disappear. */
1731 return;
1732 }
1733
1734 /* Any remaining supported units are destroyed */
1735 unit_list_iterate_safe(pcity->units_supported, punit) {
1736 wipe_unit(punit, ULR_CITY_LOST, NULL);
1737 } unit_list_iterate_safe_end;
1738
1739 if (!city_exist(id)) {
1740 /* Wiping supported units caused city to disappear. */
1741 return;
1742 }
1743
1744 trade_routes_iterate(pcity, pother_city) {
1745 remove_trade_route(pcity, pother_city, TRUE, TRUE);
1746 } trade_routes_iterate_end;
1747
1748 map_clear_border(pcenter);
1749 city_workers_queue_remove(pcity);
1750 city_thaw_workers_queue();
1751 city_refresh_queue_processing();
1752
1753 /* idex_unregister_city() is called in game_remove_city() below */
1754
1755 /* identity_number_release(pcity->id) is *NOT* done! The cities may
1756 still be alive in the client, or in the player map. The number of
1757 removed cities is small, so the loss is acceptable.
1758 */
1759
1760 old_vision = pcity->server.vision;
1761 pcity->server.vision = NULL;
1762 script_server_remove_exported_object(pcity);
1763 adv_city_free(pcity);
1764
1765 /* Remove city from the map. */
1766 tile_set_worked(pcenter, NULL);
1767
1768 /* Reveal units. */
1769 players_iterate(other_player) {
1770 if (can_player_see_units_in_city(other_player, pcity)
1771 || !map_is_known_and_seen(pcenter, other_player, V_MAIN)) {
1772 continue;
1773 }
1774 unit_list_iterate(pcenter->units, punit) {
1775 if (can_player_see_unit(other_player, punit)) {
1776 send_unit_info(other_player->connections, punit);
1777 }
1778 } unit_list_iterate_end;
1779 } players_iterate_end;
1780
1781 fc_allocate_mutex(&game.server.mutexes.city_list);
1782 game_remove_city(pcity);
1783 fc_release_mutex(&game.server.mutexes.city_list);
1784
1785 /* Remove any extras that were only there because the city was there. */
1786 extra_type_iterate(pextra) {
1787 if (tile_has_extra(pcenter, pextra)
1788 && !is_native_tile_to_extra(pextra, pcenter)) {
1789 tile_extra_rm_apply(pcenter, pextra);
1790 }
1791 } extra_type_iterate_end;
1792
1793 players_iterate(other_player) {
1794 if (map_is_known_and_seen(pcenter, other_player, V_MAIN)) {
1795 reality_check_city(other_player, pcenter);
1796 }
1797 } players_iterate_end;
1798
1799 conn_list_iterate(game.est_connections, pconn) {
1800 if (NULL == pconn->playing && pconn->observer) {
1801 /* For detached observers we have to send a specific packet. This is
1802 * a hack necessitated by the private map that exists for players but
1803 * not observers. */
1804 dsend_packet_city_remove(pconn, id);
1805 }
1806 } conn_list_iterate_end;
1807
1808 vision_clear_sight(old_vision);
1809 vision_free(old_vision);
1810
1811 /* Infrastructures may have changed. */
1812 send_tile_info(NULL, pcenter, FALSE);
1813
1814 /* Build a new palace for free if the player lost her capital and
1815 savepalace is on. */
1816 build_free_small_wonders(powner, &had_small_wonders);
1817
1818 /* Update wonder infos. */
1819 if (had_great_wonders) {
1820 send_game_info(NULL);
1821 /* No need to send to detached connections. */
1822 send_player_info_c(powner, NULL);
1823 } else if (BV_ISSET_ANY(had_small_wonders)) {
1824 /* No need to send to detached connections. */
1825 send_player_info_c(powner, NULL);
1826 }
1827
1828 if (old_content_citizens != player_content_citizens(powner)
1829 || old_angry_citizens != player_angry_citizens(powner)) {
1830 /* We crossed the EFT_EMPIRE_SIZE_* effects, we have to refresh all
1831 * cities for the player. */
1832 city_refresh_for_player(powner);
1833 }
1834
1835 sync_cities();
1836 }
1837
1838 /**************************************************************************
1839 Handle unit entering city. During peace may enter peacefully, during
1840 war conquers city.
1841 Transported unit cannot conquer city. If transported unit is seen here,
1842 transport is conquering city. Movement of the transported units is just
1843 handled before transport itself.
1844 **************************************************************************/
unit_enter_city(struct unit * punit,struct city * pcity,bool passenger)1845 void unit_enter_city(struct unit *punit, struct city *pcity, bool passenger)
1846 {
1847 bool try_civil_war = FALSE;
1848 bool city_remains;
1849 int coins;
1850 struct player *pplayer = unit_owner(punit);
1851 struct player *cplayer = city_owner(pcity);
1852
1853 /* If not at war, may peacefully enter city. Or, if we cannot occupy
1854 * the city, this unit entering will not trigger the effects below. */
1855 if (!pplayers_at_war(pplayer, cplayer)
1856 || !unit_can_take_over(punit)
1857 || passenger) {
1858 return;
1859 }
1860
1861 /* Okay, we're at war - invader captures/destroys city... */
1862
1863 /* If a capital is captured, then spark off a civil war
1864 - Kris Bubendorfer
1865 Also check spaceships --dwp
1866 */
1867
1868 if (is_capital(pcity)
1869 && (cplayer->spaceship.state == SSHIP_STARTED
1870 || cplayer->spaceship.state == SSHIP_LAUNCHED)) {
1871 spaceship_lost(cplayer);
1872 }
1873
1874 if (is_capital(pcity)
1875 && civil_war_possible(cplayer, TRUE, TRUE)
1876 && normal_player_count() < MAX_NUM_PLAYERS
1877 && civil_war_triggered(cplayer)) {
1878 try_civil_war = TRUE;
1879 }
1880
1881 /*
1882 * We later remove a citizen. Lets check if we can save this since
1883 * the city will be destroyed.
1884 */
1885 if (city_size_get(pcity) <= 1) {
1886 int saved_id = pcity->id;
1887
1888 notify_player(pplayer, city_tile(pcity), E_UNIT_WIN_ATT, ftc_server,
1889 _("You destroy %s completely."),
1890 city_tile_link(pcity));
1891 notify_player(cplayer, city_tile(pcity), E_CITY_LOST, ftc_server,
1892 _("%s has been destroyed by %s."),
1893 city_tile_link(pcity), player_name(pplayer));
1894 script_server_signal_emit("city_destroyed", 3,
1895 API_TYPE_CITY, pcity,
1896 API_TYPE_PLAYER, cplayer,
1897 API_TYPE_PLAYER, pplayer);
1898
1899 /* We cant't be sure of city existence after running some script */
1900 if (city_exist(saved_id)) {
1901 remove_city(pcity);
1902 }
1903
1904 if (try_civil_war) {
1905 (void) civil_war(cplayer);
1906 }
1907 return;
1908 }
1909
1910 coins = cplayer->economic.gold;
1911 coins = MIN(coins,
1912 fc_rand((coins / 20) + 1)
1913 + (coins * (city_size_get(pcity))) / 200);
1914 pplayer->economic.gold += coins;
1915 cplayer->economic.gold -= coins;
1916 send_player_info_c(pplayer, pplayer->connections);
1917 send_player_info_c(cplayer, cplayer->connections);
1918 if (pcity->original != pplayer) {
1919 if (coins > 0) {
1920 notify_player(pplayer, city_tile(pcity), E_UNIT_WIN_ATT, ftc_server,
1921 PL_("You conquer %s; your lootings accumulate"
1922 " to %d gold!",
1923 "You conquer %s; your lootings accumulate"
1924 " to %d gold!", coins),
1925 city_link(pcity),
1926 coins);
1927 notify_player(cplayer, city_tile(pcity), E_CITY_LOST, ftc_server,
1928 PL_("%s conquered %s and looted %d gold"
1929 " from the city.",
1930 "%s conquered %s and looted %d gold"
1931 " from the city.", coins),
1932 player_name(pplayer),
1933 city_link(pcity),
1934 coins);
1935 } else {
1936 notify_player(pplayer, city_tile(pcity), E_UNIT_WIN_ATT, ftc_server,
1937 _("You conquer %s."),
1938 city_link(pcity));
1939 notify_player(cplayer, city_tile(pcity), E_CITY_LOST, ftc_server,
1940 _("%s conquered %s."),
1941 player_name(pplayer),
1942 city_link(pcity));
1943 }
1944 } else {
1945 if (coins > 0) {
1946 notify_player(pplayer, city_tile(pcity), E_UNIT_WIN_ATT, ftc_server,
1947 PL_("You have liberated %s!"
1948 " Lootings accumulate to %d gold.",
1949 "You have liberated %s!"
1950 " Lootings accumulate to %d gold.", coins),
1951 city_link(pcity),
1952 coins);
1953 notify_player(cplayer, city_tile(pcity), E_CITY_LOST, ftc_server,
1954 PL_("%s liberated %s and looted %d gold"
1955 " from the city.",
1956 "%s liberated %s and looted %d gold"
1957 " from the city.", coins),
1958 player_name(pplayer),
1959 city_link(pcity),
1960 coins);
1961 } else {
1962 notify_player(pplayer, city_tile(pcity), E_UNIT_WIN_ATT, ftc_server,
1963 _("You have liberated %s!"),
1964 city_link(pcity));
1965 notify_player(cplayer, city_tile(pcity), E_CITY_LOST, ftc_server,
1966 _("%s liberated %s."),
1967 player_name(pplayer),
1968 city_link(pcity));
1969 }
1970 }
1971
1972 steal_a_tech(pplayer, cplayer, A_UNSET);
1973
1974 /* We transfer the city first so that it is in a consistent state when
1975 * the size is reduced. */
1976 /* FIXME: maybe it should be a ruleset option whether barbarians get
1977 * free buildings such as palaces? */
1978 city_remains = transfer_city(pplayer, pcity, 0, TRUE, TRUE, TRUE,
1979 !is_barbarian(pplayer));
1980
1981 if (city_remains) {
1982 /* reduce size should not destroy this city */
1983 fc_assert(city_size_get(pcity) > 1);
1984 city_reduce_size(pcity, 1, pplayer, "conquest");
1985 }
1986
1987 if (try_civil_war) {
1988 (void) civil_war(cplayer);
1989 }
1990
1991 if (city_remains) {
1992 script_server_signal_emit("city_transferred", 4,
1993 API_TYPE_CITY, pcity,
1994 API_TYPE_PLAYER, cplayer,
1995 API_TYPE_PLAYER, pplayer,
1996 API_TYPE_STRING, "conquest");
1997 script_server_signal_emit("city_lost", 3,
1998 API_TYPE_CITY, pcity,
1999 API_TYPE_PLAYER, cplayer,
2000 API_TYPE_PLAYER, pplayer);
2001 }
2002 }
2003
2004 /**************************************************************************
2005 Which wall gfx city should display?
2006 **************************************************************************/
city_got_citywalls(const struct city * pcity)2007 static int city_got_citywalls(const struct city *pcity)
2008 {
2009 int walls = get_city_bonus(pcity, EFT_VISIBLE_WALLS);
2010
2011 return walls > 0 ? walls : 0;
2012 }
2013
2014 /**************************************************************************
2015 Suppress sending cities during game_load() and end_phase()
2016 **************************************************************************/
send_city_suppression(bool now)2017 bool send_city_suppression(bool now)
2018 {
2019 bool formerly = send_city_suppressed;
2020
2021 send_city_suppressed = now;
2022 return formerly;
2023 }
2024
2025 /**************************************************************************
2026 This fills out a package from a player's vision_site.
2027 **************************************************************************/
package_dumb_city(struct player * pplayer,struct tile * ptile,struct packet_city_short_info * packet)2028 static void package_dumb_city(struct player* pplayer, struct tile *ptile,
2029 struct packet_city_short_info *packet)
2030 {
2031 struct vision_site *pdcity = map_get_player_city(ptile, pplayer);
2032
2033 packet->id = pdcity->identity;
2034 packet->owner = player_number(vision_site_owner(pdcity));
2035
2036 packet->tile = tile_index(ptile);
2037 sz_strlcpy(packet->name, pdcity->name);
2038
2039 packet->size = vision_site_size_get(pdcity);
2040
2041 packet->occupied = pdcity->occupied;
2042 packet->walls = pdcity->walls;
2043 packet->style = pdcity->style;
2044 packet->city_image = pdcity->city_image;
2045
2046 packet->happy = pdcity->happy;
2047 packet->unhappy = pdcity->unhappy;
2048
2049 packet->improvements = pdcity->improvements;
2050 }
2051
2052 /**************************************************************************
2053 Update plrtile information about the city, and send out information to
2054 the clients if it has changed.
2055 **************************************************************************/
refresh_dumb_city(struct city * pcity)2056 void refresh_dumb_city(struct city *pcity)
2057 {
2058 players_iterate(pplayer) {
2059 if (player_can_see_city_externals(pplayer, pcity)) {
2060 if (update_dumb_city(pplayer, pcity)) {
2061 struct packet_city_short_info packet;
2062
2063 if (city_owner(pcity) != pplayer) {
2064 /* Don't send the short_city information to someone who can see the
2065 * city's internals. Doing so would really confuse the client. */
2066 package_dumb_city(pplayer, pcity->tile, &packet);
2067 lsend_packet_city_short_info(pplayer->connections, &packet);
2068 }
2069 }
2070 }
2071 } players_iterate_end;
2072
2073 /* Don't send to non-player observers since they don't have 'dumb city'
2074 * information. */
2075 }
2076
2077 /**************************************************************************
2078 Broadcast info about a city to all players who observe the tile.
2079 If the player can see the city we update the city info first.
2080 If not we just use the info from the players private map.
2081 See also comments to send_city_info_at_tile().
2082 (Split off from send_city_info_at_tile() because that was getting
2083 too difficult for me to understand... --dwp)
2084 **************************************************************************/
broadcast_city_info(struct city * pcity)2085 void broadcast_city_info(struct city *pcity)
2086 {
2087 struct packet_city_info packet;
2088 struct packet_city_short_info sc_pack;
2089 struct player *powner = city_owner(pcity);
2090
2091 /* Send to everyone who can see the city. */
2092 if (pcity->server.needs_arrange == CNA_NOT) {
2093 /* Send city only when it's in sane state.
2094 * If it's not, it will be sent to everyone once
2095 * rearrangement has been finished. */
2096 package_city(pcity, &packet, FALSE);
2097 } else {
2098 pcity->server.needs_arrange = CNA_BROADCAST_PENDING;
2099
2100 return;
2101 }
2102 players_iterate(pplayer) {
2103 if (can_player_see_city_internals(pplayer, pcity)) {
2104 if (!send_city_suppressed || pplayer != powner) {
2105 update_dumb_city(powner, pcity);
2106 lsend_packet_city_info(powner->connections, &packet, FALSE);
2107 }
2108 } else {
2109 if (player_can_see_city_externals(pplayer, pcity)) {
2110 reality_check_city(pplayer, pcity->tile);
2111 update_dumb_city(pplayer, pcity);
2112 package_dumb_city(pplayer, pcity->tile, &sc_pack);
2113 lsend_packet_city_short_info(pplayer->connections, &sc_pack);
2114 }
2115 }
2116 } players_iterate_end;
2117
2118 /* Send to global observers. */
2119 conn_list_iterate(game.est_connections, pconn) {
2120 if (conn_is_global_observer(pconn)) {
2121 send_packet_city_info(pconn, &packet, FALSE);
2122 }
2123 } conn_list_iterate_end;
2124 }
2125
2126 /**************************************************************************
2127 Send to each client information about the cities it knows about.
2128 dest may not be NULL
2129 **************************************************************************/
send_all_known_cities(struct conn_list * dest)2130 void send_all_known_cities(struct conn_list *dest)
2131 {
2132 conn_list_do_buffer(dest);
2133 conn_list_iterate(dest, pconn) {
2134 struct player *pplayer = pconn->playing;
2135 if (!pplayer && !pconn->observer) {
2136 continue;
2137 }
2138 whole_map_iterate(ptile) {
2139 if (!pplayer || NULL != map_get_player_site(ptile, pplayer)) {
2140 send_city_info_at_tile(pplayer, pconn->self, NULL, ptile);
2141 }
2142 } whole_map_iterate_end;
2143 }
2144 conn_list_iterate_end;
2145 conn_list_do_unbuffer(dest);
2146 flush_packets();
2147 }
2148
2149 /**************************************************************************
2150 Send information about all his/her cities to player
2151 **************************************************************************/
send_player_cities(struct player * pplayer)2152 void send_player_cities(struct player *pplayer)
2153 {
2154 city_list_iterate(pplayer->cities, pcity) {
2155 if (city_refresh(pcity)) {
2156 log_error("%s radius changed while sending to player.",
2157 city_name_get(pcity));
2158
2159 /* Make sure that no workers in illegal position outside radius. */
2160 auto_arrange_workers(pcity);
2161 }
2162 send_city_info(pplayer, pcity);
2163 }
2164 city_list_iterate_end;
2165 }
2166
2167
2168 /**************************************************************************
2169 A wrapper, accessing either broadcast_city_info() (dest==NULL),
2170 or a convenience case of send_city_info_at_tile().
2171 Must specify non-NULL pcity.
2172 **************************************************************************/
send_city_info(struct player * dest,struct city * pcity)2173 void send_city_info(struct player *dest, struct city *pcity)
2174 {
2175 struct player *powner = city_owner(pcity);
2176
2177 if (S_S_RUNNING != server_state() && S_S_OVER != server_state()) {
2178 return;
2179 }
2180
2181 if (dest == powner && send_city_suppressed) {
2182 return;
2183 }
2184
2185 if (!dest || dest == powner) {
2186 pcity->server.synced = TRUE;
2187 }
2188
2189 if (!dest) {
2190 broadcast_city_info(pcity);
2191 } else {
2192 send_city_info_at_tile(dest, dest->connections, pcity, pcity->tile);
2193 }
2194
2195 if (game.info.team_pooled_research
2196 && player_list_size(team_members(powner->team)) > 1) {
2197 /* We want to send the new total bulbs production of the team. */
2198 send_research_info(research_get(powner), NULL);
2199 }
2200 }
2201
2202 /**************************************************************************
2203 Send info about a city, as seen by pviewer, to dest (usually dest will
2204 be pviewer->connections). If pplayer can see the city we update the city
2205 info first. If not we just use the info from the players private map.
2206
2207 If (pviewer == NULL) this is for observers, who see everything (?)
2208 For this function dest may not be NULL. See send_city_info() and
2209 broadcast_city_info().
2210
2211 If pcity is non-NULL it should be same as tile_city(x,y); if pcity
2212 is NULL, this function calls tile_city(x,y) (it is ok if this
2213 returns NULL).
2214
2215 Sometimes a player's map contain a city that doesn't actually exist. Use
2216 reality_check_city(pplayer, ptile) to update that. Remember to NOT send info
2217 about a city to a player who thinks the tile contains another city. If you
2218 want to update the clients info of the tile you must use
2219 reality_check_city(pplayer, ptile) first. This is generally taken care of
2220 automatically when a tile becomes visible.
2221 **************************************************************************/
send_city_info_at_tile(struct player * pviewer,struct conn_list * dest,struct city * pcity,struct tile * ptile)2222 void send_city_info_at_tile(struct player *pviewer, struct conn_list *dest,
2223 struct city *pcity, struct tile *ptile)
2224 {
2225 struct packet_city_info packet;
2226 struct packet_city_short_info sc_pack;
2227 struct player *powner = NULL;
2228
2229 if (!pcity) {
2230 pcity = tile_city(ptile);
2231 }
2232 if (pcity != NULL && pcity->server.needs_arrange != CNA_NOT) {
2233 pcity->server.needs_arrange = CNA_BROADCAST_PENDING;
2234
2235 return;
2236 }
2237 if (pcity) {
2238 powner = city_owner(pcity);
2239 }
2240 if (powner && powner == pviewer) {
2241 /* send info to owner */
2242 /* This case implies powner non-NULL which means pcity non-NULL */
2243 if (!send_city_suppressed) {
2244 /* Send all info to the owner.
2245 * Wait that city has been rearranged, if it's currently
2246 * not in sane state.
2247 */
2248 update_dumb_city(powner, pcity);
2249 package_city(pcity, &packet, FALSE);
2250 lsend_packet_city_info(dest, &packet, FALSE);
2251 if (dest == powner->connections) {
2252 /* HACK: send also a copy to global observers. */
2253 conn_list_iterate(game.est_connections, pconn) {
2254 if (conn_is_global_observer(pconn)) {
2255 send_packet_city_info(pconn, &packet, FALSE);
2256 }
2257 } conn_list_iterate_end;
2258 }
2259 }
2260 } else {
2261 /* send info to non-owner */
2262 if (!pviewer) { /* observer */
2263 if (pcity) {
2264 package_city(pcity, &packet, FALSE); /* should be dumb_city info? */
2265 lsend_packet_city_info(dest, &packet, FALSE);
2266 }
2267 } else {
2268 if (!map_is_known(ptile, pviewer)) {
2269 /* Without the conditional we'd have an infinite loop here. */
2270 map_show_tile(pviewer, ptile);
2271 }
2272 if (map_is_known_and_seen(ptile, pviewer, V_MAIN)) {
2273 if (pcity) { /* it's there and we see it; update and send */
2274 update_dumb_city(pviewer, pcity);
2275 package_dumb_city(pviewer, ptile, &sc_pack);
2276 lsend_packet_city_short_info(dest, &sc_pack);
2277 }
2278 } else { /* not seen; send old info */
2279 if (NULL != map_get_player_site(ptile, pviewer)) {
2280 package_dumb_city(pviewer, ptile, &sc_pack);
2281 lsend_packet_city_short_info(dest, &sc_pack);
2282 }
2283 }
2284 }
2285 }
2286 }
2287
2288 /**************************************************************************
2289 Fill city info packet with information about given city.
2290 **************************************************************************/
package_city(struct city * pcity,struct packet_city_info * packet,bool dipl_invest)2291 void package_city(struct city *pcity, struct packet_city_info *packet,
2292 bool dipl_invest)
2293 {
2294 int i;
2295 int ppl = 0;
2296
2297 packet->id=pcity->id;
2298 packet->owner = player_number(city_owner(pcity));
2299 packet->tile = tile_index(city_tile(pcity));
2300 sz_strlcpy(packet->name, city_name_get(pcity));
2301
2302 packet->size = city_size_get(pcity);
2303 for (i = 0; i < FEELING_LAST; i++) {
2304 packet->ppl_happy[i] = pcity->feel[CITIZEN_HAPPY][i];
2305 packet->ppl_content[i] = pcity->feel[CITIZEN_CONTENT][i];
2306 packet->ppl_unhappy[i] = pcity->feel[CITIZEN_UNHAPPY][i];
2307 packet->ppl_angry[i] = pcity->feel[CITIZEN_ANGRY][i];
2308 if (i == 0) {
2309 ppl += packet->ppl_happy[i];
2310 ppl += packet->ppl_content[i];
2311 ppl += packet->ppl_unhappy[i];
2312 ppl += packet->ppl_angry[i];
2313 }
2314 }
2315 /* The number of data in specialists[] array */
2316 packet->specialists_size = specialist_count();
2317 specialist_type_iterate(sp) {
2318 packet->specialists[sp] = pcity->specialists[sp];
2319 ppl += packet->specialists[sp];
2320 } specialist_type_iterate_end;
2321
2322 /* The nationality of the citizens. */
2323 packet->nationalities_count = 0;
2324 if (game.info.citizen_nationality) {
2325 int cit = 0;
2326
2327 player_slots_iterate(pslot) {
2328 citizens nationality = citizens_nation_get(pcity, pslot);
2329 if (nationality != 0) {
2330 /* This player should exist! */
2331 fc_assert(player_slot_get_player(pslot) != NULL);
2332
2333 packet->nation_id[packet->nationalities_count]
2334 = player_slot_index(pslot);
2335 packet->nation_citizens[packet->nationalities_count]
2336 = nationality;
2337 packet->nationalities_count++;
2338
2339 cit += nationality;
2340 }
2341 } player_slots_iterate_end;
2342
2343 fc_assert(cit == packet->size);
2344 }
2345
2346 packet->history = pcity->history;
2347 packet->culture = city_culture(pcity);
2348
2349 if (packet->size != ppl) {
2350 static bool recursion = FALSE;
2351
2352 if (recursion) {
2353 /* Recursion didn't help. Do not enter infinite recursive loop.
2354 * Package city as it is. */
2355 log_error("Failed to fix inconsistent city size.");
2356 recursion = FALSE;
2357 } else {
2358 /* Note: If you get this error and try to debug the cause, you may find
2359 * using check_city_feelings() in some key points useful. */
2360 /* Have this as an fc_assert() first, so one can use '-F' to caught these in
2361 * debugger. */
2362 fc_assert(packet->size == ppl);
2363
2364 /* In all builds have an error message shown. */
2365 log_error("City size %d, citizen count %d for %s",
2366 packet->size, ppl, city_name_get(pcity));
2367
2368 /* Try to fix */
2369 city_refresh(pcity);
2370 auto_arrange_workers(pcity);
2371
2372 /* And repackage */
2373 recursion = TRUE;
2374 package_city(pcity, packet, dipl_invest);
2375 recursion = FALSE;
2376
2377 return;
2378 }
2379 }
2380
2381 packet->city_radius_sq = pcity->city_radius_sq;
2382
2383 for (i = 0; i < MAX_TRADE_ROUTES; i++) {
2384 packet->trade[i] = pcity->trade[i];
2385 packet->trade_value[i] = pcity->trade_value[i];
2386 }
2387
2388 output_type_iterate(o) {
2389 packet->surplus[o] = pcity->surplus[o];
2390 packet->waste[o] = pcity->waste[o];
2391 packet->unhappy_penalty[o] = pcity->unhappy_penalty[o];
2392 packet->prod[o] = pcity->prod[o];
2393 packet->citizen_base[o] = pcity->citizen_base[o];
2394 packet->usage[o] = pcity->usage[o];
2395 } output_type_iterate_end;
2396
2397 packet->food_stock = pcity->food_stock;
2398 packet->shield_stock = pcity->shield_stock;
2399 packet->pollution = pcity->pollution;
2400 packet->illness_trade = pcity->illness_trade;
2401 packet->city_options = pcity->city_options;
2402
2403 packet->production_kind = pcity->production.kind;
2404 packet->production_value = universal_number(&pcity->production);
2405
2406 packet->turn_last_built=pcity->turn_last_built;
2407 packet->turn_founded = pcity->turn_founded;
2408
2409 packet->changed_from_kind = pcity->changed_from.kind;
2410 packet->changed_from_value = universal_number(&pcity->changed_from);
2411
2412 packet->before_change_shields=pcity->before_change_shields;
2413 packet->disbanded_shields=pcity->disbanded_shields;
2414 packet->caravan_shields=pcity->caravan_shields;
2415 packet->last_turns_shield_surplus = pcity->last_turns_shield_surplus;
2416
2417 worklist_copy(&packet->worklist, &pcity->worklist);
2418 packet->diplomat_investigate=dipl_invest;
2419
2420 packet->airlift = pcity->airlift;
2421 packet->did_buy = pcity->did_buy;
2422 packet->did_sell = pcity->did_sell;
2423 packet->was_happy = pcity->was_happy;
2424
2425 packet->walls = city_got_citywalls(pcity);
2426 packet->style = pcity->style;
2427 packet->city_image = get_city_bonus(pcity, EFT_CITY_IMAGE);
2428
2429 BV_CLR_ALL(packet->improvements);
2430 improvement_iterate(pimprove) {
2431 if (city_has_building(pcity, pimprove)) {
2432 BV_SET(packet->improvements, improvement_index(pimprove));
2433 }
2434 } improvement_iterate_end;
2435 }
2436
2437 /**************************************************************************
2438 updates a players knowledge about a city. If the player_tile already
2439 contains a city it must be the same city (avoid problems by always calling
2440 reality_check_city() first)
2441
2442 Returns TRUE iff anything has changed for the player city (i.e., if the
2443 client needs to be updated with a *short* city packet). This information
2444 is only used in refresh_dumb_cities; elsewhere the data is (of necessity)
2445 broadcast regardless.
2446 **************************************************************************/
update_dumb_city(struct player * pplayer,struct city * pcity)2447 bool update_dumb_city(struct player *pplayer, struct city *pcity)
2448 {
2449 bv_imprs improvements;
2450 struct tile *pcenter = city_tile(pcity);
2451 struct vision_site *pdcity = map_get_player_city(pcenter, pplayer);
2452 /* pcity->client.occupied isn't used at the server, so we go straight to the
2453 * unit list to check the occupied status. */
2454 bool occupied = (unit_list_size(pcenter->units) > 0);
2455 bool walls = city_got_citywalls(pcity);
2456 bool happy = city_happy(pcity);
2457 bool unhappy = city_unhappy(pcity);
2458 int style = pcity->style;
2459 int city_image = get_city_bonus(pcity, EFT_CITY_IMAGE);
2460
2461 BV_CLR_ALL(improvements);
2462 improvement_iterate(pimprove) {
2463 if (is_improvement_visible(pimprove)
2464 && city_has_building(pcity, pimprove)) {
2465 BV_SET(improvements, improvement_index(pimprove));
2466 }
2467 } improvement_iterate_end;
2468
2469 if (NULL == pdcity) {
2470 pdcity = vision_site_new_from_city(pcity);
2471 change_playertile_site(map_get_player_tile(pcenter, pplayer), pdcity);
2472 } else if (pdcity->location != pcenter) {
2473 log_error("Trying to update bad city (wrong location) "
2474 "at %i,%i for player %s",
2475 TILE_XY(pcity->tile), player_name(pplayer));
2476 pdcity->location = pcenter; /* ?? */
2477 } else if (pdcity->identity != pcity->id) {
2478 log_error("Trying to update old city (wrong identity) "
2479 "at %i,%i for player %s",
2480 TILE_XY(city_tile(pcity)), player_name(pplayer));
2481 pdcity->identity = pcity->id; /* ?? */
2482 } else if (pdcity->occupied == occupied
2483 && pdcity->walls == walls
2484 && pdcity->happy == happy
2485 && pdcity->unhappy == unhappy
2486 && pdcity->style == style
2487 && pdcity->city_image == city_image
2488 && BV_ARE_EQUAL(pdcity->improvements, improvements)
2489 && vision_site_size_get(pdcity) == city_size_get(pcity)
2490 && vision_site_owner(pdcity) == city_owner(pcity)
2491 && 0 == strcmp(pdcity->name, city_name_get(pcity))) {
2492 return FALSE;
2493 }
2494
2495 vision_site_update_from_city(pdcity, pcity);
2496 pdcity->occupied = occupied;
2497 pdcity->walls = walls;
2498 pdcity->style = style;
2499 pdcity->city_image = city_image;
2500 pdcity->happy = happy;
2501 pdcity->unhappy = unhappy;
2502 pdcity->improvements = improvements;
2503
2504 return TRUE;
2505 }
2506
2507 /**************************************************************************
2508 Removes outdated (nonexistant) cities from a player
2509 **************************************************************************/
reality_check_city(struct player * pplayer,struct tile * ptile)2510 void reality_check_city(struct player *pplayer, struct tile *ptile)
2511 {
2512 struct vision_site *pdcity = map_get_player_city(ptile, pplayer);
2513
2514 if (pdcity) {
2515 struct city *pcity = tile_city(ptile);
2516
2517 if (!pcity || pcity->id != pdcity->identity) {
2518 struct player_tile *playtile = map_get_player_tile(ptile, pplayer);
2519
2520 dlsend_packet_city_remove(pplayer->connections, pdcity->identity);
2521 fc_assert_ret(playtile->site == pdcity);
2522 playtile->site = NULL;
2523 vision_site_destroy(pdcity);
2524 }
2525 }
2526 }
2527
2528 /**************************************************************************
2529 Removes a dumb city. Called when the vision changed to unknown.
2530 **************************************************************************/
remove_dumb_city(struct player * pplayer,struct tile * ptile)2531 void remove_dumb_city(struct player *pplayer, struct tile *ptile)
2532 {
2533 struct vision_site *pdcity = map_get_player_city(ptile, pplayer);
2534
2535 if (pdcity) {
2536 struct player_tile *playtile = map_get_player_tile(ptile, pplayer);
2537
2538 dlsend_packet_city_remove(pplayer->connections, pdcity->identity);
2539 fc_assert_ret(playtile->site == pdcity);
2540 playtile->site = NULL;
2541 vision_site_destroy(pdcity);
2542 }
2543 }
2544
2545 /**************************************************************************
2546 Announce to the owners of the cities that trade route has been canceled
2547 between them.
2548 **************************************************************************/
announce_trade_route_removal(struct city * pc1,struct city * pc2,bool source_gone)2549 static void announce_trade_route_removal(struct city *pc1, struct city *pc2,
2550 bool source_gone)
2551 {
2552 struct player *plr1 = city_owner(pc1);
2553 struct player *plr2 = city_owner(pc2);
2554 char city1_link[MAX_LEN_LINK];
2555 char city2_link[MAX_LEN_LINK];
2556
2557 sz_strlcpy(city1_link, city_link(pc1));
2558 sz_strlcpy(city2_link, city_link(pc2));
2559
2560 if (plr1 == plr2) {
2561 if (source_gone) {
2562 notify_player(plr2, city_tile(pc2),
2563 E_CARAVAN_ACTION, ftc_server,
2564 _("Trade between %s and %s lost along with city."),
2565 city1_link, city2_link);
2566 } else {
2567 notify_player(plr1, city_tile(pc1),
2568 E_CARAVAN_ACTION, ftc_server,
2569 _("Trade route between %s and %s canceled."),
2570 city1_link, city2_link);
2571 }
2572 } else {
2573 if (source_gone) {
2574 notify_player(plr2, city_tile(pc2),
2575 E_CARAVAN_ACTION, ftc_server,
2576 /* TRANS: "...between Spanish city Madrid and Paris..." */
2577 _("Trade between %s city %s and %s lost along with "
2578 "their city."),
2579 nation_adjective_for_player(plr1), city1_link, city2_link);
2580 /* It's implicit to removed city's owner that that city no longer
2581 * has trade routes, so say nothing in that case */
2582 } else {
2583 notify_player(plr2, city_tile(pc2),
2584 E_CARAVAN_ACTION, ftc_server,
2585 _("Sorry, the %s canceled the trade route "
2586 "from %s to your city %s."),
2587 nation_plural_for_player(plr1), city1_link, city2_link);
2588 notify_player(plr1, city_tile(pc1),
2589 E_CARAVAN_ACTION, ftc_server,
2590 /* TRANS: "...from Paris to Spanish city Madrid." */
2591 _("We canceled the trade route "
2592 "from %s to %s city %s."),
2593 city1_link, nation_adjective_for_player(plr2), city2_link);
2594 }
2595 }
2596 }
2597
2598 /**************************************************************************
2599 Remove the trade route between pc1 and pc2 (if one exists).
2600 source_gone should be TRUE if the reason for removal is the imminent
2601 removal of the source city (pc1) from the game.
2602 **************************************************************************/
remove_trade_route(struct city * pc1,struct city * pc2,bool announce,bool source_gone)2603 void remove_trade_route(struct city *pc1, struct city *pc2,
2604 bool announce, bool source_gone)
2605 {
2606 int i;
2607
2608 fc_assert_ret(pc1 && pc2);
2609
2610 for (i = 0; i < MAX_TRADE_ROUTES; i++) {
2611 if (pc1->trade[i] == pc2->id) {
2612 pc1->trade[i] = 0;
2613 }
2614 if (pc2->trade[i] == pc1->id) {
2615 pc2->trade[i] = 0;
2616 }
2617 }
2618
2619 if (announce) {
2620 announce_trade_route_removal(pc1, pc2, source_gone);
2621
2622 city_refresh(pc2);
2623 send_city_info(city_owner(pc2), pc2);
2624 }
2625 }
2626
2627 /****************************************************************************
2628 Remove/cancel the city's least valuable trade routes.
2629 ****************************************************************************/
remove_smallest_trade_routes(struct city * pcity)2630 static void remove_smallest_trade_routes(struct city *pcity)
2631 {
2632 struct city_list *smallest = city_list_new();
2633
2634 (void) city_trade_removable(pcity, smallest);
2635 city_list_iterate(smallest, pother_city) {
2636 remove_trade_route(pcity, pother_city, TRUE, FALSE);
2637 } city_list_iterate_end;
2638 city_list_destroy(smallest);
2639 }
2640
2641 /**************************************************************************
2642 Establish a trade route. Notice that there has to be space for them,
2643 so you should check can_establish_trade_route first.
2644 **************************************************************************/
establish_trade_route(struct city * pc1,struct city * pc2)2645 void establish_trade_route(struct city *pc1, struct city *pc2)
2646 {
2647 int i;
2648
2649 if (city_num_trade_routes(pc1) >= max_trade_routes(pc1)) {
2650 remove_smallest_trade_routes(pc1);
2651 }
2652
2653 if (city_num_trade_routes(pc2) >= max_trade_routes(pc2)) {
2654 remove_smallest_trade_routes(pc2);
2655 }
2656
2657 for (i = 0; i < MAX_TRADE_ROUTES; i++) {
2658 if (pc1->trade[i] == 0) {
2659 pc1->trade[i] = pc2->id;
2660 break;
2661 }
2662 }
2663 fc_assert(i < MAX_TRADE_ROUTES);
2664 for (i = 0; i < MAX_TRADE_ROUTES; i++) {
2665 if (pc2->trade[i] == 0) {
2666 pc2->trade[i] = pc1->id;
2667 break;
2668 }
2669 }
2670 fc_assert(i < MAX_TRADE_ROUTES);
2671
2672 /* recalculate illness due to trade */
2673 if (game.info.illness_on) {
2674 pc1->server.illness = city_illness_calc(pc1, NULL, NULL,
2675 &(pc1->illness_trade), NULL);
2676 pc2->server.illness = city_illness_calc(pc2, NULL, NULL,
2677 &(pc2->illness_trade), NULL);
2678 }
2679 }
2680
2681 /****************************************************************************
2682 Sell the improvement from the city, and give the player the owner. Not
2683 all buildings can be sold.
2684
2685 I guess the player should always be the city owner?
2686 ****************************************************************************/
do_sell_building(struct player * pplayer,struct city * pcity,struct impr_type * pimprove)2687 void do_sell_building(struct player *pplayer, struct city *pcity,
2688 struct impr_type *pimprove)
2689 {
2690 if (can_city_sell_building(pcity, pimprove)) {
2691 pplayer->economic.gold += impr_sell_gold(pimprove);
2692 building_lost(pcity, pimprove);
2693 }
2694 }
2695
2696 /****************************************************************************
2697 Destroy the improvement in the city straight-out. Note that this is
2698 different from selling a building.
2699 ****************************************************************************/
building_lost(struct city * pcity,const struct impr_type * pimprove)2700 void building_lost(struct city *pcity, const struct impr_type *pimprove)
2701 {
2702 struct player *owner = city_owner(pcity);
2703 bool was_capital = is_capital(pcity);
2704
2705 city_remove_improvement(pcity, pimprove);
2706 if ((was_capital && !is_capital(pcity))
2707 && (owner->spaceship.state == SSHIP_STARTED
2708 || owner->spaceship.state == SSHIP_LAUNCHED)) {
2709 /* If the capital was lost (by destruction of the palace) production on
2710 * the spaceship is lost. */
2711 spaceship_lost(owner);
2712 }
2713
2714 /* update city; influence of effects (buildings, ...) on unit upkeep */
2715 if (city_refresh(pcity)) {
2716 auto_arrange_workers(pcity);
2717 }
2718
2719 /* Re-update the city's visible area. This updates fog if the vision
2720 * range increases or decreases. */
2721 city_refresh_vision(pcity);
2722 }
2723
2724 /**************************************************************************
2725 Recalculate the upkeep needed for all units supported by the city. It has
2726 to be called
2727
2728 - if a save game is loaded (via city_refresh() in game_load_internal())
2729
2730 - if the number of units supported by a city changes
2731 * create a unit (in create_unit_full())
2732 * bride a unit (in diplomat_bribe())
2733 * change homecity (in unit_change_homecity_handling())
2734 * destroy a unit (in wipe_unit())
2735
2736 - if the rules for the upkeep calculation change. This is due to effects
2737 which influence the upkeep calculation.
2738 * tech researched, government change (via city_refresh())
2739 * building destroyed (in building_lost())
2740 * building created (via city_refresh() in in city_build_building())
2741
2742 If the upkeep for a unit changes, an update is send to the player.
2743 **************************************************************************/
city_units_upkeep(const struct city * pcity)2744 void city_units_upkeep(const struct city *pcity)
2745 {
2746 int free_uk[O_LAST];
2747 int cost;
2748 struct unit_type *ut;
2749 struct player *plr;
2750 bool update;
2751
2752 if (!pcity || !pcity->units_supported
2753 || unit_list_size(pcity->units_supported) < 1) {
2754 return;
2755 }
2756
2757 memset(free_uk, 0, O_LAST * sizeof(*free_uk));
2758 output_type_iterate(o) {
2759 free_uk[o] = get_city_output_bonus(pcity, get_output_type(o),
2760 EFT_UNIT_UPKEEP_FREE_PER_CITY);
2761 } output_type_iterate_end;
2762
2763 /* save the upkeep for all units in the corresponding punit struct */
2764 unit_list_iterate(pcity->units_supported, punit) {
2765 ut = unit_type_get(punit);
2766 plr = unit_owner(punit);
2767 update = FALSE;
2768
2769 output_type_iterate(o) {
2770 cost = utype_upkeep_cost(ut, plr, o);
2771 if (cost > 0) {
2772 if (free_uk[o] > cost) {
2773 free_uk[o] -= cost;
2774 cost = 0;
2775 } else {
2776 cost -= free_uk[o];
2777 free_uk[o] = 0;
2778 }
2779 }
2780
2781 if (cost != punit->upkeep[o]) {
2782 update = TRUE;
2783 punit->upkeep[o] = cost;
2784 }
2785 } output_type_iterate_end;
2786
2787 if (update) {
2788 /* Update unit information to the player and global observers. */
2789 send_unit_info(NULL, punit);
2790 }
2791 } unit_list_iterate_end;
2792 }
2793
2794 /**************************************************************************
2795 Change the build target.
2796 **************************************************************************/
change_build_target(struct player * pplayer,struct city * pcity,struct universal * target,enum event_type event)2797 void change_build_target(struct player *pplayer, struct city *pcity,
2798 struct universal *target,
2799 enum event_type event)
2800 {
2801 const char *name;
2802 const char *source;
2803
2804 /* If the city is already building this thing, don't do anything */
2805 if (are_universals_equal(&pcity->production, target)) {
2806 return;
2807 }
2808
2809 /* Is the city no longer building a wonder? */
2810 if (VUT_IMPROVEMENT == pcity->production.kind
2811 && is_great_wonder(pcity->production.value.building)
2812 && event != E_IMP_AUTO
2813 && event != E_WORKLIST) {
2814 /* If the build target is changed because of an advisor's suggestion or
2815 because the worklist advances, then the wonder was completed --
2816 don't announce that the player has *stopped* building that wonder.
2817 */
2818 notify_player(NULL, city_tile(pcity), E_WONDER_STOPPED, ftc_server,
2819 _("The %s have stopped building The %s in %s."),
2820 nation_plural_for_player(pplayer),
2821 city_production_name_translation(pcity),
2822 city_link(pcity));
2823 }
2824
2825 /* Manage the city change-production penalty.
2826 (May penalize, restore or do nothing to the shield_stock.) */
2827 if (!pplayer->ai_controlled || has_handicap(pplayer, H_AWAY)) {
2828 pcity->shield_stock = city_change_production_penalty(pcity, target);
2829 }
2830
2831 /* Change build target. */
2832 pcity->production = *target;
2833
2834 /* What's the name of the target? */
2835 name = city_production_name_translation(pcity);
2836
2837 switch (event) {
2838 case E_WORKLIST:
2839 /* TRANS: Possible 'source' of the production change
2840 * (in "<city> is building ..." sentence). Preserve leading space. */
2841 source = _(" from the worklist");
2842 break;
2843 case E_IMP_AUTO:
2844 /* TRANS: Possible 'source' of the production change
2845 * (in "<city> is building ..." sentence). Preserve leading space. */
2846 source = _(" as suggested by the advisor");
2847 break;
2848 default:
2849 source = "";
2850 break;
2851 }
2852
2853 /* Tell the player what's up. */
2854 /* FIXME: this may give bad grammar when translated if the 'source'
2855 * string can have multiple values. */
2856 notify_player(pplayer, city_tile(pcity), event, ftc_server,
2857 /* TRANS: "<city> is building <production><source>."
2858 * 'source' might be an empty string, or a clause like
2859 * " from the worklist". */
2860 _("%s is building %s%s."),
2861 city_link(pcity),
2862 name, source);
2863
2864 /* If the city is building a wonder, tell the rest of the world
2865 about it. */
2866 if (VUT_IMPROVEMENT == pcity->production.kind
2867 && is_great_wonder(pcity->production.value.building)) {
2868 notify_player(NULL, city_tile(pcity), E_WONDER_STARTED, ftc_server,
2869 _("The %s have started building The %s in %s."),
2870 nation_plural_for_player(pplayer),
2871 name,
2872 city_link(pcity));
2873 }
2874 }
2875
2876 /**************************************************************************
2877 Change from worked to empty.
2878 city_x, city_y are city map coordinates.
2879 Call sync_cities() to send the affected cities to the clients.
2880 **************************************************************************/
city_map_update_empty(struct city * pcity,struct tile * ptile)2881 void city_map_update_empty(struct city *pcity, struct tile *ptile)
2882 {
2883 tile_set_worked(ptile, NULL);
2884 send_tile_info(NULL, ptile, FALSE);
2885 pcity->server.synced = FALSE;
2886 }
2887
2888 /**************************************************************************
2889 Change from empty to worked.
2890 city_x, city_y are city map coordinates.
2891 Call sync_cities() to send the affected cities to the clients.
2892 **************************************************************************/
city_map_update_worker(struct city * pcity,struct tile * ptile)2893 void city_map_update_worker(struct city *pcity, struct tile *ptile)
2894 {
2895 tile_set_worked(ptile, pcity);
2896 send_tile_info(NULL, ptile, FALSE);
2897 pcity->server.synced = FALSE;
2898 }
2899
2900 /**************************************************************************
2901 Updates the worked status of a tile.
2902
2903 If the status changes, auto_arrange_workers() may be called.
2904 **************************************************************************/
city_map_update_tile_direct(struct tile * ptile,bool queued)2905 static bool city_map_update_tile_direct(struct tile *ptile, bool queued)
2906 {
2907 struct city *pwork = tile_worked(ptile);
2908
2909 if (NULL != pwork
2910 && !is_free_worked(pwork, ptile)
2911 && !city_can_work_tile(pwork, ptile)) {
2912 tile_set_worked(ptile, NULL);
2913 send_tile_info(NULL, ptile, FALSE);
2914
2915 pwork->specialists[DEFAULT_SPECIALIST]++; /* keep city sanity */
2916 pwork->server.synced = FALSE;
2917
2918 if (queued) {
2919 city_freeze_workers_queue(pwork); /* place the displaced later */
2920 } else {
2921 city_refresh(pwork); /* Specialist added, keep citizen count sanity */
2922 auto_arrange_workers(pwork);
2923 send_city_info(NULL, pwork);
2924 }
2925 return TRUE;
2926 }
2927
2928 return FALSE;
2929 }
2930
2931 /**************************************************************************
2932 Updates the worked status of a tile.
2933 Call city_thaw_workers_queue() followed by sync_cities() to send the
2934 affected cities to the clients.
2935 **************************************************************************/
city_map_update_tile_frozen(struct tile * ptile)2936 bool city_map_update_tile_frozen(struct tile *ptile)
2937 {
2938 return city_map_update_tile_direct(ptile, TRUE);
2939 }
2940
2941 /**************************************************************************
2942 Updates the worked status of a tile immediately.
2943 **************************************************************************/
city_map_update_tile_now(struct tile * ptile)2944 bool city_map_update_tile_now(struct tile *ptile)
2945 {
2946 return city_map_update_tile_direct(ptile, FALSE);
2947 }
2948
2949 /**************************************************************************
2950 Make sure all players (clients) have up-to-date information about all
2951 their cities.
2952 **************************************************************************/
sync_cities(void)2953 void sync_cities(void)
2954 {
2955 if (send_city_suppressed) {
2956 return;
2957 }
2958
2959 players_iterate(pplayer) {
2960 city_list_iterate(pplayer->cities, pcity) {
2961 if (!pcity->server.synced) {
2962 /* sending will set to TRUE. */
2963 send_city_info(pplayer, pcity);
2964 }
2965 } city_list_iterate_end;
2966 } players_iterate_end;
2967 }
2968
2969 /**************************************************************************
2970 Called by auto_arrange_workers() and below.
2971 **************************************************************************/
city_map_update_all(struct city * pcity)2972 void city_map_update_all(struct city *pcity)
2973 {
2974 struct tile *pcenter = city_tile(pcity);
2975
2976 city_tile_iterate_skip_free_worked(city_map_radius_sq_get(pcity), pcenter,
2977 ptile, _index, _x, _y) {
2978 /* bypass city_map_update_tile_now() for efficiency */
2979 city_map_update_tile_direct(ptile, FALSE);
2980 } city_tile_iterate_skip_free_worked_end;
2981 }
2982
2983 /**************************************************************************
2984 Update worked map of all cities of given player
2985 **************************************************************************/
city_map_update_all_cities_for_player(struct player * pplayer)2986 void city_map_update_all_cities_for_player(struct player *pplayer)
2987 {
2988 city_list_iterate(pplayer->cities, pcity) {
2989 city_freeze_workers(pcity);
2990 city_map_update_all(pcity);
2991 city_thaw_workers(pcity);
2992 } city_list_iterate_end;
2993 }
2994
2995 /**************************************************************************
2996 For each city adjacent to ptile, check all the buildings in the city.
2997 Any which have unmet terrain requirements will be sold. This is called
2998 after any terrain changes (but this may be tied to the default ruleset).
2999
3000 FIXME: This function isn't very general. It would be better to check
3001 each turn to make sure all requirements of all buildings of all cities
3002 are met, and sell any buildings that can't be supported. Terrains aren't
3003 the only requirement that may disappear.
3004 **************************************************************************/
city_landlocked_sell_coastal_improvements(struct tile * ptile)3005 void city_landlocked_sell_coastal_improvements(struct tile *ptile)
3006 {
3007 adjc_iterate(ptile, tile1) {
3008 struct city *pcity = tile_city(tile1);
3009
3010 if (pcity) {
3011 struct player *pplayer = city_owner(pcity);
3012
3013 /* Sell all buildings (but not Wonders) that must be next to the ocean */
3014 city_built_iterate(pcity, pimprove) {
3015 if (!can_city_sell_building(pcity, pimprove)) {
3016 continue;
3017 }
3018
3019 requirement_vector_iterate(&pimprove->reqs, preq) {
3020 if ((VUT_TERRAIN == preq->source.kind
3021 || VUT_TERRAINCLASS == preq->source.kind
3022 || VUT_TERRFLAG == preq->source.kind)
3023 && !is_req_active(city_owner(pcity), NULL, pcity, NULL,
3024 pcity->tile, NULL, NULL, NULL, NULL,
3025 preq, TRUE)) {
3026 int price = impr_sell_gold(pimprove);
3027
3028 do_sell_building(pplayer, pcity, pimprove);
3029 notify_player(pplayer, tile1, E_IMP_SOLD, ftc_server,
3030 PL_("You sell %s in %s (now landlocked)"
3031 " for %d gold.",
3032 "You sell %s in %s (now landlocked)"
3033 " for %d gold.", price),
3034 improvement_name_translation(pimprove),
3035 city_link(pcity), price);
3036 }
3037 } requirement_vector_iterate_end;
3038 } city_built_iterate_end;
3039 }
3040 } adjc_iterate_end;
3041 }
3042
3043 /****************************************************************************
3044 Refresh the city's vision.
3045
3046 This function has very small overhead and can be called any time effects
3047 may have changed the vision range of the city.
3048 ****************************************************************************/
city_refresh_vision(struct city * pcity)3049 void city_refresh_vision(struct city *pcity)
3050 {
3051 v_radius_t vision_radius_sq =
3052 V_RADIUS(get_city_bonus(pcity, EFT_CITY_VISION_RADIUS_SQ), 2);
3053
3054 vision_change_sight(pcity->server.vision, vision_radius_sq);
3055 ASSERT_VISION(pcity->server.vision);
3056 }
3057
3058 /****************************************************************************
3059 Refresh the vision of all cities owned by a player, for empire-wide
3060 effects.
3061 ****************************************************************************/
refresh_player_cities_vision(struct player * pplayer)3062 void refresh_player_cities_vision(struct player *pplayer)
3063 {
3064 city_list_iterate(pplayer->cities, pcity) {
3065 city_refresh_vision(pcity);
3066 } city_list_iterate_end;
3067 }
3068
3069 /**************************************************************************
3070 Updates the squared city radius. Returns if the radius is changed.
3071 **************************************************************************/
city_map_update_radius_sq(struct city * pcity)3072 bool city_map_update_radius_sq(struct city *pcity)
3073 {
3074
3075 fc_assert_ret_val(pcity != NULL, FALSE);
3076
3077 int city_tiles_old, city_tiles_new;
3078 int city_radius_sq_old = city_map_radius_sq_get(pcity);
3079 int city_radius_sq_new = game.info.init_city_radius_sq
3080 + get_city_bonus(pcity, EFT_CITY_RADIUS_SQ);
3081
3082 /* check minimum / maximum allowed city radii */
3083 city_radius_sq_new = CLIP(CITY_MAP_MIN_RADIUS_SQ, city_radius_sq_new,
3084 CITY_MAP_MAX_RADIUS_SQ);
3085
3086 if (city_radius_sq_new == city_radius_sq_old) {
3087 /* no change */
3088 return FALSE;
3089 }
3090
3091 /* get number of city tiles for each radii */
3092 city_tiles_old = city_map_tiles(city_radius_sq_old);
3093 city_tiles_new = city_map_tiles(city_radius_sq_new);
3094
3095 if (city_tiles_old == city_tiles_new) {
3096 /* a change of the squared city radius but no change of the number of
3097 * city tiles */
3098 return FALSE;;
3099 }
3100
3101 log_debug("[%s (%d)] city_map_radius_sq: %d => %d", city_name_get(pcity),
3102 pcity->id, city_radius_sq_old, city_radius_sq_new);
3103
3104 /* workers map before */
3105 log_debug("[%s (%d)] city size: %d; specialists: %d (before change)",
3106 city_name_get(pcity), pcity->id, city_size_get(pcity),
3107 city_specialists(pcity));
3108 citylog_map_workers(LOG_DEBUG, pcity);
3109
3110 city_map_radius_sq_set(pcity, city_radius_sq_new);
3111
3112 if (city_tiles_old < city_tiles_new) {
3113 /* increased number of city tiles */
3114 city_refresh_vision(pcity);
3115 } else {
3116 /* reduced number of city tiles */
3117 int workers = 0;
3118
3119 /* remove workers from the tiles removed rom the city map */
3120 city_map_iterate_radius_sq(city_radius_sq_new, city_radius_sq_old,
3121 city_x, city_y) {
3122 struct tile *ptile = city_map_to_tile(city_tile(pcity),
3123 city_radius_sq_old, city_x,
3124 city_y);
3125
3126 if (ptile && pcity == tile_worked(ptile)) {
3127 city_map_update_empty(pcity, ptile);
3128 workers++;
3129 }
3130 } city_map_iterate_radius_sq_end;
3131
3132 /* add workers to free city tiles */
3133 if (workers > 0) {
3134 int radius_sq = city_map_radius_sq_get(pcity);
3135 city_map_iterate_without_index(radius_sq, city_x, city_y) {
3136 struct tile *ptile = city_map_to_tile(city_tile(pcity), radius_sq,
3137 city_x, city_y);
3138
3139 if (ptile && !is_free_worked(pcity, ptile)
3140 && tile_worked(ptile) != pcity
3141 && city_can_work_tile(pcity, ptile)) {
3142 city_map_update_worker(pcity, ptile);
3143 workers--;
3144 }
3145
3146 if (workers <= 0) {
3147 break;
3148 }
3149 } city_map_iterate_without_index_end;
3150 }
3151
3152 /* if there are still workers they will be updated to specialists */
3153 if (workers > 0) {
3154 pcity->specialists[DEFAULT_SPECIALIST] += workers;
3155 }
3156
3157 city_refresh_vision(pcity);
3158 }
3159
3160 /* if city is under AI control update it */
3161 adv_city_update(pcity);
3162
3163 notify_player(city_owner(pcity), city_tile(pcity), E_CITY_RADIUS_SQ,
3164 ftc_server, _("The size of the city map of %s is %s."),
3165 city_name_get(pcity),
3166 city_tiles_old < city_tiles_new ? _("increased")
3167 : _("reduced"));
3168
3169 /* workers map after */
3170 log_debug("[%s (%d)] city size: %d; specialists: %d (after change)",
3171 city_name_get(pcity), pcity->id, city_size_get(pcity),
3172 city_specialists(pcity));
3173 citylog_map_workers(LOG_DEBUG, pcity);
3174
3175 return TRUE;
3176 }
3177
3178 /**************************************************************************
3179 Clear worker task from the city and inform owner
3180 **************************************************************************/
clear_worker_task(struct city * pcity,struct worker_task * ptask)3181 void clear_worker_task(struct city *pcity, struct worker_task *ptask)
3182 {
3183 struct packet_worker_task packet;
3184
3185 if (ptask == NULL) {
3186 return;
3187 }
3188
3189 worker_task_list_remove(pcity->task_reqs, ptask);
3190
3191 packet.city_id = pcity->id;
3192 packet.tile_id = tile_index(ptask->ptile);
3193 packet.activity = ACTIVITY_LAST;
3194 packet.tgt = 0;
3195 packet.want = 0;
3196
3197 free(ptask);
3198
3199 lsend_packet_worker_task(city_owner(pcity)->connections, &packet);
3200 lsend_packet_worker_task(game.glob_observers, &packet);
3201 }
3202
3203 /**************************************************************************
3204 Clear all worker tasks from the city and inform owner
3205 **************************************************************************/
clear_worker_tasks(struct city * pcity)3206 void clear_worker_tasks(struct city *pcity)
3207 {
3208 while (worker_task_list_size(pcity->task_reqs) > 0) {
3209 clear_worker_task(pcity, worker_task_list_get(pcity->task_reqs, 0));
3210 }
3211 }
3212
3213 /**************************************************************************
3214 Send city worker task to owner
3215 **************************************************************************/
package_and_send_worker_tasks(struct city * pcity)3216 void package_and_send_worker_tasks(struct city *pcity)
3217 {
3218 struct packet_worker_task packet;
3219
3220 packet.city_id = pcity->id;
3221
3222 worker_task_list_iterate(pcity->task_reqs, ptask) {
3223 packet.tile_id = tile_index(ptask->ptile);
3224 packet.activity = ptask->act;
3225 if (ptask->tgt == NULL) {
3226 packet.tgt = -1;
3227 } else {
3228 packet.tgt = extra_number(ptask->tgt);
3229 }
3230 packet.want = ptask->want;
3231
3232 lsend_packet_worker_task(city_owner(pcity)->connections, &packet);
3233 lsend_packet_worker_task(game.glob_observers, &packet);
3234 } worker_task_list_iterate_end;
3235 }
3236