Freeciv-3.3
Loading...
Searching...
No Matches
cityturn.c
Go to the documentation of this file.
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#include <math.h> /* exp, sqrt */
22
23/* dependencies/lua */
24#include "lua.h" /* lua_Integer */
25
26/* utility */
27#include "fcintl.h"
28#include "log.h"
29#include "mem.h"
30#include "rand.h"
31#include "shared.h"
32#include "support.h"
33
34/* common/aicore */
35#include "cm.h"
36
37/* common */
38#include "achievements.h"
39#include "actiontools.h"
40#include "borders.h"
41#include "calendar.h"
42#include "citizens.h"
43#include "city.h"
44#include "counters.h"
45#include "culture.h"
46#include "events.h"
47#include "disaster.h"
48#include "game.h"
49#include "government.h"
50#include "map.h"
51#include "player.h"
52#include "research.h"
53#include "road.h"
54#include "server_settings.h"
55#include "specialist.h"
56#include "tech.h"
57#include "traderoutes.h"
58#include "unit.h"
59#include "unitlist.h"
60
61/* common/scriptcore */
62#include "luascript_types.h"
63
64/* server */
65#include "citizenshand.h"
66#include "citytools.h"
67#include "cityturn.h"
68#include "maphand.h"
69#include "notify.h"
70#include "plrhand.h"
71#include "sanitycheck.h"
72#include "spacerace.h"
73#include "srv_log.h"
74#include "srv_main.h"
75#include "techtools.h"
76#include "unittools.h"
77#include "unithand.h"
78
79/* server/advisors */
80#include "advbuilding.h"
81#include "advdata.h"
82
83/* server/scripting */
84#include "script_server.h"
85
86/* Queue for pending city_refresh() */
87static struct city_list *city_refresh_queue = NULL;
88
89/* The game is currently considering to remove the listed units because of
90 * missing gold upkeep. A unit ends up here if it has gold upkeep that
91 * can't be paid. A random unit in the list will be removed until the
92 * problem is solved. */
93static struct unit_list *uk_rem_gold = NULL;
94
95static void check_pollution(struct city *pcity);
96static void city_populate(struct city *pcity, struct player *nationality);
97
98static bool worklist_change_build_target(struct player *pplayer,
99 struct city *pcity);
100
101static bool city_distribute_surplus_shields(struct player *pplayer,
102 struct city *pcity);
103static bool city_build_building(struct player *pplayer, struct city *pcity);
104static bool city_build_unit(struct player *pplayer, struct city *pcity);
105static bool city_build_stuff(struct player *pplayer, struct city *pcity);
106static struct unit *city_create_unit(struct city *pcity,
107 const struct unit_type *utype,
108 struct citizens_reduction *red)
109 fc__attribute((nonnull (1, 2)));
110static const struct impr_type *building_upgrades_to(struct city *pcity,
111 const struct impr_type *pimprove);
112static void upgrade_building_prod(struct city *pcity);
113static const struct unit_type *unit_upgrades_to(struct city *pcity,
114 const struct unit_type *id);
115static void upgrade_unit_prod(struct city *pcity);
116
117/* Helper struct for associating a building to a city. */
118struct cityimpr {
119 struct city *pcity;
121};
122
123#define SPECLIST_TAG cityimpr
124#define SPECLIST_TYPE struct cityimpr
125#include "speclist.h"
126
127#define cityimpr_list_iterate(cityimprlist, pcityimpr) \
128 TYPED_LIST_ITERATE(struct cityimpr, cityimprlist, pcityimpr)
129#define cityimpr_list_iterate_end LIST_ITERATE_END
130
131static bool sell_random_building(struct player *pplayer,
132 struct cityimpr_list *imprs);
133static struct unit *sell_random_unit(struct player *pplayer,
134 struct unit_list *punitlist);
135
136static citizens city_reduce_specialists(struct city *pcity, citizens change);
137static citizens city_reduce_workers(struct city *pcity, citizens change);
138
139static bool city_balance_treasury_buildings(struct city *pcity);
140static bool city_balance_treasury_units(struct city *pcity);
141
142static bool disband_city(struct city *pcity);
143
144static void define_orig_production_values(struct city *pcity);
145static void update_city_activity(struct city *pcity);
146static void nullify_caravan_and_disband_plus(struct city *pcity);
147static bool city_illness_check(const struct city * pcity);
148
149static float city_migration_score(struct city *pcity);
150static bool do_city_migration(struct city *pcity_from,
151 struct city *pcity_to);
152static bool check_city_migrations_player(const struct player *pplayer);
153
154/**********************************************************************/
158bool city_refresh(struct city *pcity)
159{
160 bool retval;
161
162 pcity->server.needs_refresh = FALSE;
163
165 city_units_upkeep(pcity); /* update unit upkeep */
167 city_style_refresh(pcity);
168
169 if (retval) {
170 /* Force a sync of the city after the change. */
171 send_city_info(city_owner(pcity), pcity);
172 }
173
174 return retval;
175}
176
177/**********************************************************************/
181void city_refresh_for_player(struct player *pplayer)
182{
184 city_list_iterate(pplayer->cities, pcity) {
185 if (city_refresh(pcity)) {
187 }
188 send_city_info(pplayer, pcity);
191}
192
193/**********************************************************************/
196void city_refresh_queue_add(struct city *pcity)
197{
198 if (NULL == city_refresh_queue) {
200 } else if (city_list_find_number(city_refresh_queue, pcity->id)) {
201 return;
202 }
203
205 pcity->server.needs_refresh = TRUE;
206}
207
208/**********************************************************************/
213{
214 if (NULL == city_refresh_queue) {
215 return;
216 }
217
219 if (pcity->server.needs_refresh) {
220 if (city_refresh(pcity)) {
222 }
223 send_city_info(city_owner(pcity), pcity);
224 }
226
229}
230
231/**********************************************************************/
234void remove_obsolete_buildings_city(struct city *pcity, bool refresh)
235{
236 struct player *pplayer = city_owner(pcity);
237 bool sold = FALSE;
238
239 city_built_iterate(pcity, pimprove) {
240 if (improvement_obsolete(pplayer, pimprove, pcity)
241 && can_city_sell_building(pcity, pimprove)) {
242 int sgold;
243
244 do_sell_building(pplayer, pcity, pimprove, "obsolete");
245 sgold = impr_sell_gold(pimprove);
247 PL_("%s is selling %s (obsolete) for %d.",
248 "%s is selling %s (obsolete) for %d.",
249 sgold),
250 city_link(pcity),
252 sgold);
253 sold = TRUE;
254 }
256
257 if (sold && refresh) {
258 if (city_refresh(pcity)) {
260 }
261 send_city_info(pplayer, pcity);
262 send_player_info_c(pplayer, NULL); /* Send updated gold to all */
263 }
264}
265
266/**********************************************************************/
270{
271 city_list_iterate(pplayer->cities, pcity) {
274}
275
276/**********************************************************************/
280void apply_cmresult_to_city(struct city *pcity,
281 const struct cm_result *cmr)
282{
283 struct tile *pcenter = city_tile(pcity);
284 const struct civ_map *nmap = &(wld.map);
285
286 /* Now apply results */
288 ptile, idx, x, y) {
289 struct city *pwork = tile_worked(ptile);
290
291 if (cmr->worker_positions[idx]) {
292 if (NULL == pwork) {
293 city_map_update_worker(pcity, ptile);
294 } else {
295 fc_assert(pwork == pcity);
296 }
297 } else {
298 if (pwork == pcity) {
299 city_map_update_empty(pcity, ptile);
300 }
301 }
303
305 pcity->specialists[sp] = cmr->specialists[sp];
307}
308
309/**********************************************************************/
313 struct city *pcity)
314{
315 int csize = city_size_get(pcity);
317
318 cmp->require_happy = FALSE;
319 cmp->allow_disorder = FALSE;
320 cmp->allow_specialists = TRUE;
321
322 /* We used to look at pplayer->ai.xxx_priority to determine the values
323 * to be used here. However that doesn't work at all because those values
324 * are on a different scale. Later the AI may wish to adjust its
325 * priorities - this should be done via a separate set of variables. */
326 if (csize > 1) {
327 if (csize <= game.info.notradesize) {
328 cmp->factor[O_FOOD] = 15;
329 } else {
330 if (gsize == pcity->food_stock) {
331 /* We don't need more food if the granary is full. */
332 cmp->factor[O_FOOD] = 0;
333 } else {
334 cmp->factor[O_FOOD] = 10;
335 }
336 }
337 } else {
338 /* Growing to size 2 is the highest priority. */
339 cmp->factor[O_FOOD] = 20;
340 }
341
342 cmp->factor[O_SHIELD] = 5;
343 cmp->factor[O_TRADE] = 0; /* Trade only provides gold/science. */
344 cmp->factor[O_GOLD] = 2;
345 cmp->factor[O_LUXURY] = 0; /* Luxury only influences happiness. */
346 cmp->factor[O_SCIENCE] = 2;
347 cmp->happy_factor = 0;
348
349 if (gsize == pcity->food_stock) {
350 cmp->minimal_surplus[O_FOOD] = 0;
351 } else {
352 cmp->minimal_surplus[O_FOOD] = 1;
353 }
354
355 cmp->minimal_surplus[O_SHIELD] = 1;
356 cmp->minimal_surplus[O_TRADE] = 0;
357 cmp->minimal_surplus[O_GOLD] = -FC_INFINITY;
358 cmp->minimal_surplus[O_LUXURY] = 0;
359 cmp->minimal_surplus[O_SCIENCE] = 0;
360}
361
362/**********************************************************************/
365void auto_arrange_workers(struct city *pcity)
366{
367 struct cm_parameter cmp;
368 struct cm_parameter *pcmp;
369 struct cm_result *cmr;
370 bool broadcast_needed;
371
372 /* See comment in freeze_workers(): we can't rearrange while
373 * workers are frozen (i.e. multiple updates need to be done). */
374 if (pcity->server.workers_frozen > 0) {
375 if (pcity->server.needs_arrange == CNA_NOT) {
377 }
378 return;
379 }
381
383
384 /* Freeze the workers and make sure all the tiles around the city
385 * are up to date. Then thaw, but hackishly make sure that thaw
386 * doesn't call us recursively, which would waste time. */
387 city_freeze_workers(pcity);
389
390 city_map_update_all(pcity);
391
393 city_thaw_workers(pcity);
394
395 /* Now start actually rearranging. */
396 city_refresh(pcity);
397
398 sanity_check_city(pcity);
399 cm_clear_cache(pcity);
400
401 if (pcity->cm_parameter) {
402 pcmp = pcity->cm_parameter;
403 } else {
404 pcmp = &cmp;
407 }
408
409 /* This must be after city_refresh() so that the result gets created for the right
410 * city radius */
411 cmr = cm_result_new(pcity);
412 cm_query_result(pcity, pcmp, cmr, FALSE);
413
414 if (!cmr->found_a_valid) {
415 if (pcity->cm_parameter) {
416 /* If player-defined parameters fail, cancel and notify player. */
417 free(pcity->cm_parameter);
418 pcity->cm_parameter = NULL;
419
420 notify_player(city_owner(pcity), city_tile(pcity),
422 _("The citizen governor can't fulfill the requirements "
423 "for %s. Passing back control."),
424 city_link(pcity));
425
426 /* Switch to default parameters, and try with them */
427 pcmp = &cmp;
430 cm_query_result(pcity, pcmp, cmr, FALSE);
431 }
432
433 if (!cmr->found_a_valid) {
434 /* Drop surpluses and try again. */
436 cmp.minimal_surplus[o] = 0;
438 cmp.minimal_surplus[O_GOLD] = -FC_INFINITY;
439 cm_query_result(pcity, pcmp, cmr, FALSE);
440 }
441 }
442 if (!cmr->found_a_valid) {
443 /* Emergency management. Get _some_ result. This doesn't use
444 * cm_init_emergency_parameter() so we can keep the factors from
445 * above. */
447 cmp.minimal_surplus[o] = MIN(cmp.minimal_surplus[o],
448 MIN(pcity->surplus[o], 0));
450 cmp.require_happy = FALSE;
451 cmp.allow_disorder = is_ai(city_owner(pcity)) ? FALSE : TRUE;
452 cm_query_result(pcity, pcmp, cmr, FALSE);
453 }
454 if (!cmr->found_a_valid) {
455 CITY_LOG(LOG_DEBUG, pcity, "emergency management");
456 pcmp = &cmp;
458 cm_query_result(pcity, pcmp, cmr, TRUE);
459 }
460 fc_assert_ret(cmr->found_a_valid);
461
463
464 if (pcity->server.debug) {
465 /* Print debug output if requested. */
466 cm_print_city(pcity);
468 }
469
470 if (city_refresh(pcity)) {
471 log_error("%s radius changed when already arranged workers.",
472 city_name_get(pcity));
473 /* Can't do anything - don't want to enter infinite recursive loop
474 * by trying to arrange workers more. */
475 }
476 sanity_check_city(pcity);
477
478 if (broadcast_needed) {
479 broadcast_city_info(pcity);
480 }
481
484}
485
486/**********************************************************************/
489static void city_global_turn_notify(struct conn_list *dest)
490{
491 cities_iterate(pcity) {
492 const struct impr_type *pimprove = pcity->production.value.building;
493
494 if (VUT_IMPROVEMENT == pcity->production.kind
495 && is_great_wonder(pimprove)
496 && (1 >= city_production_turns_to_build(pcity, TRUE)
497 && can_city_build_improvement_now(pcity, pimprove))) {
498 notify_conn(dest, city_tile(pcity),
500 _("Notice: Wonder %s in %s will be finished next turn."),
501 improvement_name_translation(pimprove), city_link(pcity));
502 }
504}
505
506/**********************************************************************/
510static void city_turn_notify(const struct city *pcity,
511 struct conn_list *dest,
512 const struct player *cache_for_player)
513{
514 const struct impr_type *pimprove = pcity->production.value.building;
515 struct packet_chat_msg packet;
517
518 if (0 < pcity->surplus[O_FOOD]) {
520 - pcity->food_stock - 1) / pcity->surplus[O_FOOD];
521
522 if (get_city_bonus(pcity, EFT_GROWTH_FOOD) <= 0
525 && 0 < pcity->surplus[O_SHIELD]) {
526 /* From the check above, the surplus must always be positive. */
527 turns_granary = (impr_build_shield_cost(pcity, pimprove)
528 - pcity->shield_stock) / pcity->surplus[O_SHIELD];
529 /* If growth and granary completion occur simultaneously, granary
530 * preserves food. -AJS. */
531 if (5 > turns_growth && 5 > turns_granary
533 package_event(&packet, city_tile(pcity),
535 _("Suggest throttling growth in %s to use %s "
536 "(being built) more effectively."),
537 city_link(pcity),
539 lsend_packet_chat_msg(dest, &packet);
540 if (NULL != cache_for_player) {
542 }
543 }
544 }
545
546 if (0 >= turns_growth && !city_celebrating(pcity)
547 && city_can_grow_to(pcity, city_size_get(pcity) + 1)) {
548 package_event(&packet, city_tile(pcity),
550 _("%s may soon grow to size %i."),
551 city_link(pcity), city_size_get(pcity) + 1);
552 lsend_packet_chat_msg(dest, &packet);
553 if (NULL != cache_for_player) {
555 }
556 }
557 } else {
558 if (0 >= pcity->food_stock + pcity->surplus[O_FOOD]
559 && 0 > pcity->surplus[O_FOOD]) {
560 package_event(&packet, city_tile(pcity),
562 _("Warning: Famine feared in %s."), city_link(pcity));
563 lsend_packet_chat_msg(dest, &packet);
564 if (NULL != cache_for_player) {
566 }
567 }
568 }
569}
570
571/**********************************************************************/
576{
577 if (NULL != pconn) {
578 struct player *pplayer = conn_get_player(pconn);
579
580 if (NULL != pplayer) {
581 city_list_iterate(pplayer->cities, pcity) {
582 city_turn_notify(pcity, pconn->self, NULL);
584 }
586 } else {
587 players_iterate(pplayer) {
588 city_list_iterate(pplayer->cities, pcity) {
589 city_turn_notify(pcity, pplayer->connections, pplayer);
592 /* NB: notifications to 'game.est_connections' are automatically
593 * cached. */
595 }
596}
597
598/**********************************************************************/
601void update_city_activities(struct player *pplayer)
602{
603 int n;
604
605 fc_assert(NULL != pplayer->cities);
606
607 n = city_list_size(pplayer->cities);
608
609 if (n > 0) {
610 struct city *cities[n];
611 int i = 0, r;
612
613 city_list_iterate(pplayer->cities, pcity) {
614
615 citizens_convert(pcity);
616
617 /* Cancel trade routes that cannot exist any more */
619 struct city *tcity = game_city_by_number(proute->partner);
620
621 if (tcity != NULL) {
622 bool cancel = FALSE;
623
624 if (proute->dir != RDIR_FROM && goods_has_flag(proute->goods, GF_DEPLETES)
625 && !goods_can_be_provided(tcity, proute->goods, NULL)) {
626 cancel = TRUE;
627 }
628 if (!cancel && !can_cities_trade(pcity, tcity)) {
631
632 if (settings->cancelling == TRI_CANCEL) {
633 cancel = TRUE;
634 }
635 }
636
637 if (cancel) {
638 struct trade_route *back;
639
641 free(proute);
642 free(back);
643 }
644 }
646
647 /* Add cities to array for later random order handling */
648 cities[i++] = pcity;
650
651 /* How gold upkeep is handled depends on the setting
652 * 'game.info.gold_upkeep_style':
653 * GOLD_UPKEEP_CITY: Each city tries to balance its upkeep individually
654 * (this is done in update_city_activity()).
655 * GOLD_UPKEEP_MIXED: Each city tries to balance its upkeep for
656 * buildings individually; the upkeep for units is
657 * paid by the nation.
658 * GOLD_UPKEEP_NATION: The nation as a whole balances the treasury. If
659 * the treasury is not balance units and buildings
660 * are sold. */
661
662 /* Iterate over cities in a random order. */
663 while (i > 0) {
664 r = fc_rand(i);
665 /* update unit upkeep */
668 cities[r] = cities[--i];
669 }
670 }
671}
672
673/**********************************************************************/
687 bool wipe_in_the_end)
688{
689 int punit_id;
690
693 /* Can't get rid of this unit. It is undisbandable for the current
694 * situation. */
695 return FALSE;
696 }
697
698 punit_id = punit->id;
699
700 /* Try to perform this unit's can't upkeep actions. */
703 NULL, NULL, NULL, NULL);
704
706 /* No forced action was able to kill the unit. Finish the job. */
708 }
709
710 return !unit_is_alive(punit_id);
711}
712
713/**********************************************************************/
717static citizens city_reduce_specialists(struct city *pcity, citizens change)
718{
719 citizens want = change;
720
721 fc_assert_ret_val(0 < change, 0);
722
724 citizens fix = MIN(want, pcity->specialists[sp]);
725
726 pcity->specialists[sp] -= fix;
727 want -= fix;
729
730 return change - want;
731}
732
733/**********************************************************************/
737static citizens city_reduce_workers(struct city *pcity, citizens change)
738{
739 struct tile *pcenter = city_tile(pcity);
740 int want = change;
741 const struct civ_map *nmap = &(wld.map);
742
743 fc_assert_ret_val(0 < change, 0);
744
746 ptile, _index, _x, _y) {
747 if (0 < want && tile_worked(ptile) == pcity) {
748 city_map_update_empty(pcity, ptile);
749 want--;
750 }
752
753 return change - want;
754}
755
756/**********************************************************************/
762 struct player *destroyer, const char *reason)
763{
765 int old_radius_sq;
766
767 if (pop_loss == 0) {
768 return TRUE;
769 }
770
771 if (city_size_get(pcity) <= pop_loss) {
772 int id = pcity->id;
773
774 citizens_update(pcity, NULL); /* To avoid warnings during the script */
775 /* Won't refresh a doomed city, or should we? */
776 script_server_signal_emit("city_destroyed", pcity, pcity->owner,
777 destroyer);
778
779 if (city_exist(id)) {
780 remove_city(pcity);
781 }
782 return FALSE;
783 }
785 city_size_add(pcity, -pop_loss);
788
789 /* Cap the food stock at the new granary size. */
790 if (pcity->food_stock > city_granary_size(city_size_get(pcity))) {
792 }
793
794 /* First try to kill off the specialists */
796
797 if (loss_remain > 0) {
798 /* Take it out on workers */
799#ifndef FREECIV_NDEBUG
800 loss_remain -=
801#endif /* FREECIV_NDEBUG */
803 }
804
805 /* Update citizens. */
806 citizens_update(pcity, NULL);
807
808 /* Update number of people in each feelings category.
809 * This also updates the city radius if needed. */
810 city_refresh(pcity);
811
813
814 /* Send city data. */
815 sync_cities();
816
818 "city_reduce_size() has remaining"
819 "%d of %d for \"%s\"[%d]",
821 city_name_get(pcity), city_size_get(pcity));
822
823 /* Update cities that have trade routes with us */
825 if (city_refresh(pcity2)) {
826 /* This should never happen, but if it does, make sure not to
827 * leave workers outside city radius. */
829 }
831
832 sanity_check_city(pcity);
833
834 if (reason != NULL) {
835 int id = pcity->id;
836
837 script_server_signal_emit("city_size_change", pcity,
839
840 return city_exist(id);
841 }
842
843 return TRUE;
844}
845
846/**********************************************************************/
850void city_repair_size(struct city *pcity, int change)
851{
852 if (change > 0) {
853 pcity->specialists[DEFAULT_SPECIALIST] += change;
854 } else if (change < 0) {
855 int need = change + city_reduce_specialists(pcity, -change);
856
857 if (0 > need) {
858#ifndef FREECIV_NDEBUG
859 need +=
860#endif /* FREECIV_NDEBUG */
861 city_reduce_workers(pcity, -need);
862 }
863
864 fc_assert_msg(0 == need,
865 "city_repair_size() has remaining %d of %d for \"%s\"[%d]",
866 need, change, city_name_get(pcity), city_size_get(pcity));
867 }
868}
869
870/**********************************************************************/
877int city_growth_granary_savings(const struct city *pcity)
878{
880
881 return CLIP(0, savings, 100);
882}
883
884/**********************************************************************/
891int city_shrink_granary_savings(const struct city *pcity)
892{
894
895 return CLIP(0, savings, 100);
896}
897
898/**********************************************************************/
904static void city_reset_foodbox(struct city *pcity, int new_size,
905 bool shrink)
906{
907 int savings_pct = ( shrink
910
912}
913
914/**********************************************************************/
919static bool city_increase_size(struct city *pcity)
920{
921 int new_food;
923 bool have_square = FALSE;
924 bool rapture_grow = city_rapture_grow(pcity); /* Check before size increase! */
925 struct tile *pcenter = city_tile(pcity);
926 struct player *powner = city_owner(pcity);
927 const struct impr_type *pimprove = pcity->production.value.building;
928 const struct civ_map *nmap = &(wld.map);
929
930 if (!city_can_grow_to(pcity, city_size_get(pcity) + 1)) {
931 /* Need improvement */
935 _("%s needs %s (being built) to grow beyond size %d."),
936 city_link(pcity),
938 city_size_get(pcity));
939 } else {
941 _("%s needs an improvement to grow beyond size %d."),
942 city_link(pcity), city_size_get(pcity));
943 }
944 /* Granary can only hold so much */
946 * (100 * 100 - game.server.aqueductloss * (100 - savings_pct))
947 / (100 * 100));
948 pcity->food_stock = MIN(pcity->food_stock, new_food);
949
950 return FALSE;
951 }
952
953 city_size_add(pcity, 1);
954
955 /* Do not empty food stock if city is growing by celebrating */
956 if (rapture_grow) {
958 } else {
960 }
961 pcity->food_stock = MIN(pcity->food_stock, new_food);
962
963 /* If there is enough food, and the city is big enough,
964 * make new citizens into scientists or taxmen -- Massimo */
965
966 /* Ignore food if no square can be worked */
968 ptile, _index, _x, _y) {
969 if (tile_worked(ptile) != pcity /* Quick test */
970 && city_can_work_tile(pcity, ptile)) {
972 }
974
975 if ((pcity->surplus[O_FOOD] >= 2 || !have_square)
977 pcity->specialists[best_specialist(O_SCIENCE, pcity)]++;
978 } else if ((pcity->surplus[O_FOOD] >= 2 || !have_square)
980 pcity->specialists[best_specialist(O_GOLD, pcity)]++;
981 } else {
982 pcity->specialists[DEFAULT_SPECIALIST]++; /* or else city is !sane */
983 }
984
985 /* Deprecated signal. Connect your lua functions to "city_size_change" that's
986 * emitted from calling functions which know the 'reason' of the increase. */
987 script_server_signal_emit("city_growth", pcity,
988 (lua_Integer)city_size_get(pcity));
989
990 return TRUE;
991}
992
993/**********************************************************************/
997 struct player *nationality)
998{
999 struct player *powner = city_owner(pcity);
1000
1001 /* Update citizens. */
1002 citizens_update(pcity, nationality);
1003
1004 /* Refresh the city data; this also checks the squared city radius. */
1005 city_refresh(pcity);
1006
1007 auto_arrange_workers(pcity);
1008
1009 /* Update cities that have trade routes with us */
1011 if (city_refresh(pcity2)) {
1012 /* This should never happen, but if it does, make sure not to
1013 * leave workers outside city radius. */
1015 }
1017
1019 _("%s grows to size %d."),
1020 city_link(pcity), city_size_get(pcity));
1021
1022 sanity_check_city(pcity);
1023
1024 sync_cities();
1025}
1026
1027/**********************************************************************/
1031 struct player *nationality, const char *reason)
1032{
1033 int change = size - city_size_get(pcity);
1034
1035 if (change > 0) {
1036 int old_size = city_size_get(pcity);
1037 int real_change;
1038 int current_size = city_size_get(pcity);
1039 int id = pcity->id;
1040
1041 /* Increase city size until size reached, or increase fails */
1042 while (size > current_size && city_increase_size(pcity)) {
1043 /* TODO: This is currently needed only because there's
1044 * deprecated script signal "city_growth" emitted.
1045 * Check the need after signal has been dropped completely. */
1046 if (!city_exist(id)) {
1047 return FALSE;
1048 }
1049
1050 current_size++;
1051 }
1052
1053 city_refresh_after_city_size_increase(pcity, nationality);
1054
1056
1057 if (real_change != 0 && reason != NULL) {
1058 script_server_signal_emit("city_size_change", pcity,
1060
1061 if (!city_exist(id)) {
1062 return FALSE;
1063 }
1064 }
1065 } else if (change < 0) {
1066 /* We assume that city_change_size() is never called because
1067 * of enemy actions. If that changes, enemy must be passed
1068 * to city_reduce_size() */
1069 return city_reduce_size(pcity, -change, NULL, reason);
1070 }
1071
1072 map_claim_border(pcity->tile, pcity->owner, -1);
1073
1074 return TRUE;
1075}
1076
1077/**********************************************************************/
1081static void city_populate(struct city *pcity, struct player *nationality)
1082{
1083 int saved_id = pcity->id;
1084 int granary_size = city_granary_size(city_size_get(pcity));
1085
1086 pcity->food_stock += pcity->surplus[O_FOOD];
1087 if (pcity->food_stock >= granary_size || city_rapture_grow(pcity)) {
1088 if (city_had_recent_plague(pcity)) {
1089 notify_player(city_owner(pcity), city_tile(pcity),
1091 _("A recent plague outbreak prevents growth in %s."),
1092 city_link(pcity));
1093 /* Lose excess food */
1094 pcity->food_stock = MIN(pcity->food_stock, granary_size);
1095 } else {
1096 bool success;
1097
1098 success = city_increase_size(pcity);
1099 map_claim_border(pcity->tile, pcity->owner, -1);
1100
1101 if (success) {
1102 city_refresh_after_city_size_increase(pcity, nationality);
1103 script_server_signal_emit("city_size_change", pcity,
1104 (lua_Integer)1, "growth");
1105 }
1106 }
1107 } else if (pcity->food_stock < 0) {
1108 /* FIXME: should this depend on units with ability to build
1109 * cities or on units that require food in upkeep?
1110 * I'll assume citybuilders (units that 'contain' 1 pop) -- sjolie
1111 * The above may make more logical sense, but in game terms
1112 * you want to disband a unit that is draining your food
1113 * reserves. Hence, I'll assume food upkeep > 0 units. -- jjm
1114 */
1116 if (punit->upkeep[O_FOOD] > 0) {
1117 const char *punit_link = unit_tile_link(punit);
1118
1121 notify_player(city_owner(pcity), city_tile(pcity),
1123 _("Famine feared in %s, %s lost!"),
1124 city_link(pcity), punit_link);
1125 }
1126
1127 if (city_exist(saved_id)) {
1128 city_reset_foodbox(pcity, city_size_get(pcity), TRUE);
1129 }
1130
1131 return;
1132 }
1134 if (city_size_get(pcity) > 1) {
1135 notify_player(city_owner(pcity), city_tile(pcity),
1137 _("Famine causes population loss in %s."),
1138 city_link(pcity));
1139 } else {
1140 notify_player(city_owner(pcity), city_tile(pcity),
1142 _("Famine destroys %s entirely."),
1143 city_link(pcity));
1144 }
1145 city_reset_foodbox(pcity, city_size_get(pcity) - 1, TRUE);
1146 if (city_reduce_size(pcity, 1, NULL, "famine")) {
1147 pcity->had_famine = TRUE;
1148 }
1149 }
1150}
1151
1152/**********************************************************************/
1159 struct city *pcity,
1160 struct player *pplayer,
1161 int saved_id)
1162{
1163 const void *ptarget;
1164 const char *tgt_name;
1165 const struct requirement_vector *build_reqs;
1166 const char *signal_name;
1167 const struct req_context city_ctxt = {
1168 .player = pplayer,
1169 .city = pcity,
1170 .tile = city_tile(pcity)
1171 /* FIXME: Setting .unittype is currently redundant
1172 * but can_city_build_unit_direct() does it */
1173 };
1174 bool purge = FALSE;
1175 bool known = FALSE;
1176
1177 if (pcity->wlcb == WLCB_ALWAYS_PURGE) {
1178 return TRUE;
1179 }
1180
1181 switch (target->kind) {
1182 case VUT_UTYPE:
1183 ptarget = target->value.utype;
1184 build_reqs = &target->value.utype->build_reqs;
1186 signal_name = "unit_cant_be_built";
1187 break;
1188 case VUT_IMPROVEMENT:
1189 ptarget = target->value.building;
1190 build_reqs = &target->value.building->reqs;
1192 signal_name = "building_cant_be_built";
1193 break;
1194 default:
1196 || target->kind == VUT_UTYPE), FALSE);
1197 return FALSE;
1198 }
1199
1200 if (pcity->wlcb == WLCB_ALWAYS_POSTPONE) {
1201 notify_player(pplayer, city_tile(pcity),
1203 _("%s can't build %s from the worklist. "
1204 "Postponing..."),
1205 city_link(pcity),
1206 tgt_name);
1207 return FALSE;
1208 }
1209
1210 requirement_vector_iterate(build_reqs, preq) {
1212 known = TRUE;
1213 switch (preq->source.kind) {
1214 case VUT_COUNTER:
1215 if (preq->present) {
1216 notify_player(pplayer, city_tile(pcity),
1218 _("%s can't build %s from the worklist; "
1219 "counter %s value's checkpoint do not met "
1220 "Postponing..."),
1221 city_link(pcity),
1222 tgt_name,
1224 (preq->source.value.counter));
1225 } else {
1226 purge = TRUE;
1227 }
1228 break;
1229 case VUT_ADVANCE:
1230 if (preq->present) {
1231 notify_player(pplayer, city_tile(pcity),
1233 _("%s can't build %s from the worklist; "
1234 "tech %s not yet available. Postponing..."),
1235 city_link(pcity),
1236 tgt_name,
1238 (preq->source.value.advance));
1240 pcity, "need_tech");
1241 } else {
1242 /* While techs can be unlearned, this isn't useful feedback */
1243 purge = TRUE;
1244 }
1245 break;
1246 case VUT_TECHFLAG:
1247 if (preq->present) {
1248 notify_player(pplayer, city_tile(pcity),
1250 _("%s can't build %s from the worklist; "
1251 "no tech with flag \"%s\" yet available. "
1252 "Postponing..."),
1253 city_link(pcity),
1254 tgt_name,
1255 tech_flag_id_name(preq->source.value.techflag));
1257 pcity, "need_techflag");
1258 } else {
1259 /* While techs can be unlearned, this isn't useful feedback */
1260 purge = TRUE;
1261 }
1262 break;
1263 case VUT_IMPROVEMENT:
1264 if (preq->range == REQ_RANGE_LOCAL) {
1265 /* Building itself is never going to change */
1266 purge = TRUE;
1267 } else {
1268 if (preq->present) {
1269 notify_player(pplayer, city_tile(pcity),
1271 _("%s can't build %s from the worklist; "
1272 "need to have %s first. Postponing..."),
1273 city_link(pcity),
1274 tgt_name,
1276 preq->source.value.building));
1278 pcity, "need_building");
1279 } else {
1280 notify_player(pplayer, city_tile(pcity),
1282 _("%s can't build %s from the worklist; "
1283 "need to not have %s. Postponing..."),
1284 city_link(pcity),
1285 tgt_name,
1287 preq->source.value.building));
1289 pcity, "have_building");
1290 }
1291 }
1292 break;
1293 case VUT_IMPR_GENUS:
1294 if (preq->range == REQ_RANGE_LOCAL) {
1295 /* Building's own genus is never going to change */
1296 purge = TRUE;
1297 } else {
1298 if (preq->present) {
1299 notify_player(pplayer, city_tile(pcity),
1301 _("%s can't build %s from the worklist; "
1302 "need to have %s first. Postponing..."),
1303 city_link(pcity),
1304 tgt_name,
1306 preq->source.value.impr_genus));
1308 pcity, "need_building_genus");
1309 } else {
1310 notify_player(pplayer, city_tile(pcity),
1312 _("%s can't build %s from the worklist; "
1313 "need to not have %s. Postponing..."),
1314 city_link(pcity),
1315 tgt_name,
1317 preq->source.value.impr_genus));
1319 pcity, "have_building_genus");
1320 }
1321 }
1322 break;
1323 case VUT_IMPR_FLAG:
1324 if (preq->range == REQ_RANGE_LOCAL) {
1325 /* Building's own flags are never going to change */
1326 purge = TRUE;
1327 } else {
1328 if (preq->present) {
1329 notify_player(pplayer, city_tile(pcity),
1331 _("%s can't build %s from the worklist; "
1332 "need to have %s first. Postponing..."),
1333 city_link(pcity),
1334 tgt_name,
1336 preq->source.value.impr_flag));
1338 pcity, "need_building_flag");
1339 } else {
1340 notify_player(pplayer, city_tile(pcity),
1342 _("%s can't build %s from the worklist; "
1343 "need to not have %s. Postponing..."),
1344 city_link(pcity),
1345 tgt_name,
1347 preq->source.value.impr_flag));
1349 pcity, "have_building_flag");
1350 }
1351 }
1352 break;
1353 case VUT_PLAYER_FLAG:
1354 if (preq->present) {
1355 notify_player(pplayer, city_tile(pcity),
1357 _("%s can't build %s from the worklist; "
1358 "need to have %s first. Postponing..."),
1359 city_link(pcity),
1360 tgt_name,
1362 preq->source.value.plr_flag));
1364 pcity, "need_player_flag");
1365 } else {
1366 notify_player(pplayer, city_tile(pcity),
1368 _("%s can't build %s from the worklist; "
1369 "need to not have %s. Postponing..."),
1370 city_link(pcity),
1371 tgt_name,
1373 preq->source.value.plr_flag));
1375 pcity, "have_player_flag");
1376 }
1377 break;
1378 case VUT_PLAYER_STATE:
1379 purge = TRUE;
1380 break;
1381 case VUT_GOVERNMENT:
1382 if (preq->present) {
1383 notify_player(pplayer, city_tile(pcity),
1385 _("%s can't build %s from the worklist; "
1386 "it needs %s government. Postponing..."),
1387 city_link(pcity),
1388 tgt_name,
1389 government_name_translation(preq->source.value.govern));
1391 pcity, "need_government");
1392 } else {
1393 notify_player(pplayer, city_tile(pcity),
1395 _("%s can't build %s from the worklist; "
1396 "it cannot have %s government. Postponing..."),
1397 city_link(pcity),
1398 tgt_name,
1399 government_name_translation(preq->source.value.govern));
1401 pcity, "have_government");
1402 }
1403 break;
1404 case VUT_ACHIEVEMENT:
1405 if (preq->present) {
1406 notify_player(pplayer, city_tile(pcity),
1408 _("%s can't build %s from the worklist; "
1409 "it needs \"%s\" achievement. Postponing..."),
1410 city_link(pcity),
1411 tgt_name,
1412 achievement_name_translation(preq->source.value.achievement));
1414 pcity, "need_achievement");
1415 } else {
1416 /* Can't unachieve things. */
1417 purge = TRUE;
1418 }
1419 break;
1420 case VUT_EXTRA:
1421 if (preq->present) {
1422 notify_player(pplayer, city_tile(pcity),
1424 Q_("?extra:%s can't build %s from the worklist; "
1425 "%s is required. Postponing..."),
1426 city_link(pcity),
1427 tgt_name,
1428 extra_name_translation(preq->source.value.extra));
1430 pcity, "need_extra");
1431 } else {
1432 notify_player(pplayer, city_tile(pcity),
1434 Q_("?extra:%s can't build %s from the worklist; "
1435 "%s is prohibited. Postponing..."),
1436 city_link(pcity),
1437 tgt_name,
1438 extra_name_translation(preq->source.value.extra));
1440 pcity, "have_extra");
1441 }
1442 break;
1443 case VUT_GOOD:
1444 if (preq->present) {
1445 notify_player(pplayer, city_tile(pcity),
1447 Q_("?extra:%s can't build %s from the worklist; "
1448 "%s is required. Postponing..."),
1449 city_link(pcity),
1450 tgt_name,
1451 goods_name_translation(preq->source.value.good));
1453 pcity, "need_good");
1454 } else {
1455 notify_player(pplayer, city_tile(pcity),
1457 Q_("?extra:%s can't build %s from the worklist; "
1458 "%s is prohibited. Postponing..."),
1459 city_link(pcity),
1460 tgt_name,
1461 goods_name_translation(preq->source.value.good));
1463 pcity, "have_good");
1464 }
1465 break;
1466 case VUT_TERRAIN:
1467 if (preq->present) {
1468 notify_player(pplayer, city_tile(pcity),
1470 Q_("?terrain:%s can't build %s from the worklist; "
1471 "%s terrain is required. Postponing..."),
1472 city_link(pcity),
1473 tgt_name,
1474 terrain_name_translation(preq->source.value.terrain));
1476 pcity, "need_terrain");
1477 } else {
1478 notify_player(pplayer, city_tile(pcity),
1480 Q_("?terrain:%s can't build %s from the worklist; "
1481 "%s terrain is prohibited. Postponing..."),
1482 city_link(pcity),
1483 tgt_name,
1484 terrain_name_translation(preq->source.value.terrain));
1486 pcity, "have_terrain");
1487 }
1488 break;
1489 case VUT_NATION:
1490 if (preq->range < REQ_RANGE_TRADE_ROUTE
1491 || preq->range == REQ_RANGE_PLAYER) {
1492 /* At higher ranges, new players with their nations may arrive */
1493 purge = TRUE;
1494 } else {
1495 if (preq->present) {
1496 notify_player(pplayer, city_tile(pcity),
1498 /* TRANS: "%s nation" is adjective */
1499 Q_("?nation:%s can't build %s from the worklist; "
1500 "%s nation is required. Postponing..."),
1501 city_link(pcity),
1502 tgt_name,
1503 nation_adjective_translation(preq->source.value.nation));
1505 pcity, "need_nation");
1506 } else {
1507 notify_player(pplayer, city_tile(pcity),
1509 Q_("?nation:%s can't build %s from the worklist; "
1510 "%s nation is prohibited. Postponing..."),
1511 city_link(pcity),
1512 tgt_name,
1513 nation_adjective_translation(preq->source.value.nation));
1515 pcity, "have_nation");
1516 }
1517 }
1518 break;
1519 case VUT_NATIONGROUP:
1520 if (preq->range < REQ_RANGE_TRADE_ROUTE
1521 || preq->range == REQ_RANGE_PLAYER) {
1522 /* At higher ranges, new players with their nations may arrive */
1523 purge = TRUE;
1524 } else {
1525 if (preq->present) {
1526 notify_player(pplayer, city_tile(pcity),
1528 /* TRANS: "%s nation" is adjective */
1529 Q_("?ngroup:%s can't build %s from the worklist; "
1530 "%s nation is required. Postponing..."),
1531 city_link(pcity),
1532 tgt_name,
1533 nation_group_name_translation(preq->source.value.nationgroup));
1535 pcity, "need_nationgroup");
1536 } else {
1537 notify_player(pplayer, city_tile(pcity),
1539 Q_("?ngroup:%s can't build %s from the worklist; "
1540 "%s nation is prohibited. Postponing..."),
1541 city_link(pcity),
1542 tgt_name,
1543 nation_group_name_translation(preq->source.value.nationgroup));
1545 pcity, "have_nationgroup");
1546 }
1547 }
1548 break;
1549 case VUT_STYLE:
1550 /* FIXME: City styles sometimes change over time, but it isn't
1551 * entirely under player control. Probably better to purge
1552 * with useful explanation. */
1553 if (preq->present) {
1554 notify_player(pplayer, city_tile(pcity),
1556 _("%s can't build %s from the worklist; "
1557 "only %s style cities may build this. Postponing..."),
1558 city_link(pcity),
1559 tgt_name,
1560 style_name_translation(preq->source.value.style));
1562 pcity, "need_style");
1563 } else {
1564 notify_player(pplayer, city_tile(pcity),
1566 _("%s can't build %s from the worklist; "
1567 "%s style cities may not build this. Postponing..."),
1568 city_link(pcity),
1569 tgt_name,
1570 style_name_translation(preq->source.value.style));
1572 pcity, "have_style");
1573 }
1574 break;
1575 case VUT_NATIONALITY:
1576 /* FIXME: Changing citizen nationality is hard: purging might be
1577 * more useful in this case. */
1578 if (preq->present) {
1579 notify_player(pplayer, city_tile(pcity),
1581 /* TRANS: Latter %s is citizen nationality */
1582 _("%s can't build %s from the worklist; "
1583 "only city with %s may build this. Postponing..."),
1584 city_link(pcity),
1585 tgt_name,
1586 nation_plural_translation(preq->source.value.nationality));
1588 pcity, "need_nationality");
1589 } else {
1590 notify_player(pplayer, city_tile(pcity),
1592 /* TRANS: Latter %s is citizen nationality */
1593 _("%s can't build %s from the worklist; "
1594 "only city without %s may build this. Postponing..."),
1595 city_link(pcity),
1596 tgt_name,
1597 nation_plural_translation(preq->source.value.nationality));
1599 pcity, "have_nationality");
1600 }
1601 break;
1602 case VUT_ORIGINAL_OWNER:
1603 /* Original owner of this specific city won't change.
1604 * Update this when supporting ranges other than REQ_RANGE_CITY. */
1605 purge = TRUE;
1606 break;
1607 case VUT_DIPLREL:
1608 case VUT_DIPLREL_TILE: /* The tile owner is the city owner */
1609 case VUT_DIPLREL_TILE_O: /* The tile owner is the city owner */
1610 if (preq->present) {
1611 const char *reason;
1612
1613 notify_player(pplayer, city_tile(pcity),
1615 /* TRANS: '%s' is a wide range of relationships;
1616 * e.g., 'Peace', 'Never met', 'Foreign',
1617 * 'Hosts embassy', 'Provided Casus Belli' */
1618 _("%s can't build %s from the worklist; "
1619 "the relationship '%s' is required."
1620 " Postponing..."),
1621 city_link(pcity),
1622 tgt_name,
1624 preq->source.value.diplrel));
1625
1626 if (preq->source.kind == VUT_DIPLREL_TILE) {
1627 reason = "need_diplrel_tile";
1628 } else if (preq->source.kind == VUT_DIPLREL_TILE_O) {
1629 reason = "need_diplrel_tile_o";
1630 } else {
1631 fc_assert(preq->source.kind == VUT_DIPLREL);
1632 reason = "need_diplrel";
1633 }
1634
1636 pcity, reason);
1637 } else {
1638 const char *reason;
1639
1640 notify_player(pplayer, city_tile(pcity),
1642 _("%s can't build %s from the worklist; "
1643 "the relationship '%s' is prohibited."
1644 " Postponing..."),
1645 city_link(pcity),
1646 tgt_name,
1648 preq->source.value.diplrel));
1649
1650 if (preq->source.kind == VUT_DIPLREL_TILE) {
1651 reason = "have_diplrel_tile";
1652 } else if (preq->source.kind == VUT_DIPLREL_TILE_O) {
1653 reason = "have_diplrel_tile_o";
1654 } else {
1655 fc_assert(preq->source.kind == VUT_DIPLREL);
1656 reason = "have_diplrel";
1657 }
1658
1660 pcity, reason);
1661 }
1662 break;
1665 if (preq->present) {
1666 const char *reason;
1667
1668 notify_player(pplayer, city_tile(pcity),
1670 /* TRANS: '%s' is a wide range of relationships;
1671 * e.g., 'Peace', 'Never met', 'Foreign',
1672 * 'Hosts embassy', 'Provided Casus Belli' */
1673 _("%s can't build %s from the worklist; "
1674 "unit with the relationship '%s' is required."
1675 " Postponing..."),
1676 city_link(pcity),
1677 tgt_name,
1679 preq->source.value.diplrel));
1680
1681 if (preq->source.kind == VUT_DIPLREL_UNITANY) {
1682 reason = "need_diplrel_unitany";
1683 } else {
1684 fc_assert(preq->source.kind == VUT_DIPLREL_UNITANY_O);
1685 reason = "need_diplrel_unitany_o";
1686 }
1687
1689 pcity, reason);
1690 } else {
1691 const char *reason;
1692
1693 notify_player(pplayer, city_tile(pcity),
1695 _("%s can't build %s from the worklist; "
1696 "unit with the relationship '%s' is prohibited."
1697 " Postponing..."),
1698 city_link(pcity),
1699 tgt_name,
1701 preq->source.value.diplrel));
1702
1703 if (preq->source.kind == VUT_DIPLREL_UNITANY) {
1704 reason = "have_diplrel_unitany";
1705 } else {
1706 fc_assert(preq->source.kind == VUT_DIPLREL_UNITANY_O);
1707 reason = "have_diplrel_unitany_o";
1708 }
1709
1711 pcity, reason);
1712 }
1713 break;
1714 case VUT_MINSIZE:
1715 if (preq->present) {
1716 notify_player(pplayer, city_tile(pcity),
1718 _("%s can't build %s from the worklist; "
1719 "city must be of size %d or larger. "
1720 "Postponing..."),
1721 city_link(pcity),
1722 tgt_name,
1723 preq->source.value.minsize);
1725 pcity, "need_minsize");
1726 } else {
1727 notify_player(pplayer, city_tile(pcity),
1729 _("%s can't build %s from the worklist; "
1730 "city must be of size %d or smaller."
1731 "Postponing..."),
1732 city_link(pcity),
1733 tgt_name,
1734 (preq->source.value.minsize - 1));
1736 pcity, "need_minsize");
1737 }
1738 break;
1739 case VUT_MINCULTURE:
1740 if (preq->present) {
1741 notify_player(pplayer, city_tile(pcity),
1743 _("%s can't build %s from the worklist; "
1744 "city must have culture of %d. Postponing..."),
1745 city_link(pcity),
1746 tgt_name,
1747 preq->source.value.minculture);
1749 pcity, "need_minculture");
1750 } else {
1751 /* What has been written may not be unwritten. */
1752 purge = TRUE;
1753 }
1754 break;
1755 case VUT_MINFOREIGNPCT:
1756 if (preq->present) {
1757 notify_player(pplayer, city_tile(pcity),
1759 _("%s can't build %s from the worklist; "
1760 "city must have %d%% foreign population. Postponing..."),
1761 city_link(pcity),
1762 tgt_name,
1763 preq->source.value.minforeignpct);
1765 pcity, "need_minforeignpct");
1766 } else {
1767 notify_player(pplayer, city_tile(pcity),
1769 _("%s can't build %s from the worklist; "
1770 "city must have %d%% native population. Postponing..."),
1771 city_link(pcity),
1772 tgt_name,
1773 100 - preq->source.value.minforeignpct);
1775 pcity, "need_minforeignpct");
1776 }
1777 break;
1778 case VUT_MINTECHS:
1779 if (preq->present) {
1780 notify_player(pplayer, city_tile(pcity),
1782 _("%s can't build %s from the worklist; "
1783 "%d techs must be known. Postponing..."),
1784 city_link(pcity),
1785 tgt_name,
1786 preq->source.value.min_techs);
1788 pcity, "need_mintechs");
1789 } else {
1790 purge = TRUE;
1791 }
1792 break;
1793 case VUT_MINCITIES:
1794 if (preq->present) {
1795 notify_player(pplayer, city_tile(pcity),
1797 _("%s can't build %s from the worklist; "
1798 "Must own %d cities. Postponing..."),
1799 city_link(pcity),
1800 tgt_name,
1801 preq->source.value.min_cities);
1803 pcity, "need_mincities");
1804 } else {
1805 purge = TRUE;
1806 }
1807 break;
1808 case VUT_MAXTILEUNITS:
1809 if (preq->present) {
1810 notify_player(pplayer, city_tile(pcity),
1812 PL_("%s can't build %s from the worklist; "
1813 "more than %d unit on tile."
1814 " Postponing...",
1815 "%s can't build %s from the worklist; "
1816 "more than %d units on tile."
1817 " Postponing...",
1818 preq->source.value.max_tile_units),
1819 city_link(pcity),
1820 tgt_name,
1821 preq->source.value.max_tile_units);
1823 pcity, "need_tileunits");
1824 } else {
1825 notify_player(pplayer, city_tile(pcity),
1827 PL_("%s can't build %s from the worklist; "
1828 "fewer than %d unit on tile."
1829 " Postponing...",
1830 "%s can't build %s from the worklist; "
1831 "fewer than %d units on tile."
1832 " Postponing...",
1833 preq->source.value.max_tile_units + 1),
1834 city_link(pcity),
1835 tgt_name,
1836 preq->source.value.max_tile_units + 1);
1838 pcity, "need_tileunits");
1839 }
1840 break;
1841 case VUT_AI_LEVEL:
1842 /* Can't change AI level. */
1843 purge = TRUE;
1844 break;
1845 case VUT_TERRAINCLASS:
1846 /* Change of terrain class is expected to be very unlikely. Purge!
1847 * TODO: Analyze ruleset to see how unlikely terrain class change actually is. */
1848 purge = TRUE;
1849 break;
1850 case VUT_TERRFLAG:
1851 if (preq->present) {
1852 notify_player(pplayer, city_tile(pcity),
1854 _("%s can't build %s from the worklist; "
1855 "terrain with \"%s\" flag is required. "
1856 "Postponing..."),
1857 city_link(pcity),
1858 tgt_name,
1859 terrain_flag_id_name(preq->source.value.terrainflag));
1861 pcity, "need_terrainflag");
1862 } else {
1863 notify_player(pplayer, city_tile(pcity),
1865 _("%s can't build %s from the worklist; "
1866 "terrain with \"%s\" flag is prohibited. "
1867 "Postponing..."),
1868 city_link(pcity),
1869 tgt_name,
1870 terrain_flag_id_name(preq->source.value.terrainflag));
1872 pcity, "have_terrainflag");
1873 }
1874 break;
1875 case VUT_ROADFLAG:
1876 if (preq->present) {
1877 notify_player(pplayer, city_tile(pcity),
1879 _("%s can't build %s from the worklist; "
1880 "road with \"%s\" flag is required. "
1881 "Postponing..."),
1882 city_link(pcity),
1883 tgt_name,
1884 road_flag_id_name(preq->source.value.roadflag));
1886 pcity, "need_roadflag");
1887 } else {
1888 notify_player(pplayer, city_tile(pcity),
1890 _("%s can't build %s from the worklist; "
1891 "road with \"%s\" flag is prohibited. "
1892 "Postponing..."),
1893 city_link(pcity),
1894 tgt_name,
1895 road_flag_id_name(preq->source.value.roadflag));
1897 pcity, "have_roadflag");
1898 }
1899 break;
1900 case VUT_EXTRAFLAG:
1901 if (preq->present) {
1902 notify_player(pplayer, city_tile(pcity),
1904 _("%s can't build %s from the worklist; "
1905 "extra with \"%s\" flag is required. "
1906 "Postponing..."),
1907 city_link(pcity),
1908 tgt_name,
1909 extra_flag_id_translated_name(preq->source.value.extraflag));
1911 pcity, "need_extraflag");
1912 } else {
1913 notify_player(pplayer, city_tile(pcity),
1915 _("%s can't build %s from the worklist; "
1916 "extra with \"%s\" flag is prohibited. "
1917 "Postponing..."),
1918 city_link(pcity),
1919 tgt_name,
1920 extra_flag_id_translated_name(preq->source.value.extraflag));
1922 pcity, "have_extraflag");
1923 }
1924 break;
1925 case VUT_MINLATITUDE:
1926 case VUT_MAXLATITUDE:
1927 /* Can't change where the city is located. */
1928 purge = TRUE;
1929 break;
1930 case VUT_CITYTILE:
1931 if (CITYT_BORDERING_TCLASS_REGION == preq->source.value.citytile
1932 && (preq->range == REQ_RANGE_CADJACENT
1933 || preq->range == REQ_RANGE_ADJACENT)) {
1934 if (preq->present) {
1935 notify_player(pplayer, city_tile(pcity),
1937 _("%s can't build %s from the worklist; "
1938 "different terrain class nearby is required. "
1939 "Postponing..."),
1940 city_link(pcity),
1941 tgt_name);
1943 pcity, "need_different_terrainclass");
1944 } else {
1945 notify_player(pplayer, city_tile(pcity),
1947 _("%s can't build %s from the worklist; "
1948 "different terrain class nearby is prohibited. "
1949 "Postponing..."),
1950 city_link(pcity),
1951 tgt_name);
1953 pcity, "have_different_terrainclass");
1954 }
1955 break;
1956 }
1957 /* Other values should not present in build reqs */
1959
1960 case VUT_UTYPE:
1961 case VUT_UTFLAG:
1962 case VUT_UCLASS:
1963 case VUT_UCFLAG:
1964 case VUT_MINVETERAN:
1965 case VUT_UNITSTATE:
1966 case VUT_ACTIVITY:
1967 case VUT_MINMOVES:
1968 case VUT_MINHP:
1969 case VUT_ACTION:
1970 case VUT_OTYPE:
1971 case VUT_SPECIALIST:
1972 case VUT_TERRAINALTER: /* XXX could do this in principle */
1973 /* Will only happen with a bogus ruleset. */
1974 log_error("worklist_change_build_target() has bogus preq");
1975 break;
1976 case VUT_CITYSTATUS:
1977 if (preq->source.value.citystatus == CITYS_TRANSFERRED) {
1978 /* If there's a change, it will invalidate worklist anyway. */
1979 purge = TRUE;
1980 } else if (preq->source.value.citystatus == CITYS_OWNED_BY_ORIGINAL) {
1981 if (preq->range == REQ_RANGE_CITY || preq->range == REQ_RANGE_TILE) {
1982 /* Can't change at these ranges */
1983 purge = TRUE;
1984 } else {
1985 if (preq->present) {
1986 notify_player(pplayer, city_tile(pcity),
1988 /* TRANS: last %s is a CityStatus ("OwnedByOriginal") */
1989 _("%s can't build %s from the worklist; "
1990 "only available when city in range %s \"%s\". "
1991 "Postponing..."),
1992 city_link(pcity),
1993 tgt_name, req_range_name(preq->range),
1994 citystatus_type_name(preq->source.value.citystatus));
1996 pcity, "need_citystatus");
1997 } else {
1998 notify_player(pplayer, city_tile(pcity),
2000 /* TRANS: last %s is a CityStatus ("OwnedByOriginal") */
2001 _("%s can't build %s from the worklist; "
2002 "not available when city in range %s is \"%s\". "
2003 "Postponing..."),
2004 city_link(pcity),
2005 tgt_name, req_range_name(preq->range),
2006 citystatus_type_name(preq->source.value.citystatus));
2008 pcity, "have_citystatus");
2009 }
2010 }
2011 } else {
2012 /* Other status types will only happen with a bogus ruleset. */
2013 log_error("worklist_change_build_target() has bogus citystatus preq");
2014 }
2015 break;
2016 case VUT_MINYEAR:
2017 if (preq->present) {
2018 notify_player(pplayer, city_tile(pcity),
2020 /* TRANS: last %s is a date */
2021 _("%s can't build %s from the worklist; "
2022 "only available from %s. Postponing..."),
2023 city_link(pcity),
2024 tgt_name,
2025 textyear(preq->source.value.minyear));
2027 pcity, "need_minyear");
2028 } else {
2029 /* Can't go back in time. */
2030 purge = TRUE;
2031 }
2032 break;
2033 case VUT_MINCALFRAG:
2034 /* Unlike VUT_MINYEAR, a requirement in either direction is
2035 * likely to be fulfilled sooner or later. */
2036 if (preq->present) {
2037 notify_player(pplayer, city_tile(pcity),
2039 /* TRANS: last %s is a calendar fragment from
2040 * the ruleset; may be a bare number */
2041 _("%s can't build %s from the worklist; "
2042 "only available from %s. Postponing..."),
2043 city_link(pcity),
2044 tgt_name,
2045 textcalfrag(preq->source.value.mincalfrag));
2047 pcity, "need_mincalfrag");
2048 } else {
2049 fc_assert_action(preq->source.value.mincalfrag > 0, break);
2050 notify_player(pplayer, city_tile(pcity),
2052 /* TRANS: last %s is a calendar fragment from
2053 * the ruleset; may be a bare number */
2054 _("%s can't build %s from the worklist; "
2055 "not available after %s. Postponing..."),
2056 city_link(pcity),
2057 tgt_name,
2058 textcalfrag(preq->source.value.mincalfrag - 1));
2060 pcity, "have_mincalfrag");
2061 }
2062 break;
2063 case VUT_TOPO:
2064 if (preq->present) {
2065 notify_player(pplayer, city_tile(pcity),
2067 /* TRANS: third %s is topology flag name
2068 * ("Hex", "ISO" */
2069 _("%s can't build %s from the worklist; "
2070 "only available in worlds with %s map."),
2071 city_link(pcity),
2072 tgt_name,
2073 _(topo_flag_name(preq->source.value.topo_property)));
2075 pcity, "need_topo");
2076 }
2077 purge = TRUE;
2078 break;
2079 case VUT_WRAP:
2080 if (preq->present) {
2081 notify_player(pplayer, city_tile(pcity),
2083 /* TRANS: third %s is wrap flag name
2084 * ("WrapX", "Wrapy") */
2085 _("%s can't build %s from the worklist; "
2086 "only available in worlds with %s map."),
2087 city_link(pcity),
2088 tgt_name,
2089 _(wrap_flag_name(preq->source.value.wrap_property)));
2091 pcity, "need_wrap");
2092 }
2093 purge = TRUE;
2094 break;
2095 case VUT_SERVERSETTING:
2096 notify_player(pplayer, city_tile(pcity),
2098 /* TRANS: %s is a server setting, its value and
2099 * if it is required to be present or absent.
2100 * The string's format is specified in
2101 * ssetv_human_readable().
2102 * Example: "killstack is enabled". */
2103 _("%s can't build %s from the worklist; "
2104 "only available when the server setting "
2105 "%s."),
2106 city_link(pcity),
2107 tgt_name,
2108 ssetv_human_readable(preq->source.value.ssetval,
2109 preq->present));
2111 pcity, "need_setting");
2112 /* Don't assume that the server setting will be changed. */
2113 purge = TRUE;
2114 break;
2115 case VUT_AGE:
2116 if (preq->present) {
2117 notify_player(pplayer, city_tile(pcity),
2119 _("%s can't build %s from the worklist; "
2120 "only available once %d turns old. Postponing..."),
2121 city_link(pcity),
2122 tgt_name,
2123 preq->source.value.age);
2125 pcity, "need_age");
2126 } else {
2127 /* Can't go back in time. */
2128 purge = TRUE;
2129 }
2130 break;
2131 case VUT_FORM_AGE:
2132 if (preq->present) {
2133 notify_player(pplayer, city_tile(pcity),
2135 _("%s can't build %s from the worklist; "
2136 "only available once %d turns old form. Postponing..."),
2137 city_link(pcity),
2138 tgt_name,
2139 preq->source.value.age);
2141 pcity, "need_form_age");
2142 } else {
2143 /* Can't go back in time. */
2144 purge = TRUE;
2145 }
2146 break;
2147 case VUT_NONE:
2148 case VUT_COUNT:
2150 "worklist_change_build_target() "
2151 "called with invalid preq");
2152 break;
2153 /* No default handling here, as we want compiler warning
2154 * if new requirement type is added to enum and it's not handled
2155 * here. */
2156 };
2157 break;
2158 }
2159
2160 /* Almost all cases emit signal in the end, so city check needed. */
2161 if (!city_exist(saved_id)) {
2162 /* Some script has removed city */
2163 return TRUE;
2164 }
2165
2167
2168 if (!known) {
2169 /* FIXME: make can_city_build_improvement_now() return a reason enum,
2170 * so we can notify user with it.
2171 * Likely the building already exist. */
2172 purge = TRUE;
2173 }
2174
2175 return purge;
2176}
2177
2178/**********************************************************************/
2184static bool worklist_change_build_target(struct player *pplayer,
2185 struct city *pcity)
2186{
2187 struct universal target;
2188 bool success = FALSE;
2189 int i;
2190 int saved_id = pcity->id;
2191 bool city_checked = TRUE; /* This is used to avoid spurious city_exist() calls */
2192 struct worklist *pwl = &pcity->worklist;
2193
2194 if (worklist_is_empty(pwl)) {
2195 /* Nothing in the worklist; bail now. */
2196 return FALSE;
2197 }
2198
2199 i = 0;
2200 while (!success && i < worklist_length(pwl)) {
2201
2202 if (!city_checked) {
2203 if (!city_exist(saved_id)) {
2204 /* Some script has removed useless city that cannot build
2205 * what it is told to! */
2206 return FALSE;
2207 }
2209 }
2210
2211 if (worklist_peek_ith(pwl, &target, i)) {
2212 success = can_city_build_now(pcity, &target);
2213 } else {
2214 success = FALSE;
2215 }
2216 i++;
2217
2218 if (success) {
2219 break; /* while */
2220 }
2221
2222 switch (target.kind) {
2223 case VUT_UTYPE:
2224 {
2225 const struct unit_type *ptarget = target.value.utype;
2226 const struct unit_type *pupdate = unit_upgrades_to(pcity, ptarget);
2227 bool purge;
2228
2229 /* Maybe we can just upgrade the target to what the city /can/ build. */
2230 if (U_NOT_OBSOLETED == pupdate) {
2231 /* Nope, we're stuck. Skip this item from the worklist. */
2232 struct research *presearch = research_get(pplayer);
2233 struct advance *missing = NULL;
2234 bool multiple = FALSE;
2235
2238 if (missing != NULL) {
2239 multiple = TRUE;
2240 } else {
2241 missing = padv;
2242 }
2243 }
2245
2246
2247 if (missing != NULL) {
2248 if (!multiple) {
2249 notify_player(pplayer, city_tile(pcity),
2251 _("%s can't build %s from the worklist; "
2252 "tech %s not yet available. Postponing..."),
2255 } else {
2256 notify_player(pplayer, city_tile(pcity),
2258 _("%s can't build %s from the worklist; "
2259 "multiple techs still needed. Postponing..."),
2261 }
2262
2263 script_server_signal_emit("unit_cant_be_built", ptarget, pcity,
2264 "need_tech");
2265 } else {
2266 /* Unknown or requirement from vector. */
2267 purge = worklist_item_postpone_req_vec(&target, pcity, pplayer,
2268 saved_id);
2269 }
2271 break;
2272 } else {
2274 }
2275 if (purge) {
2276 /* If the city can never build this unit or its descendants,
2277 * drop it. */
2278 notify_player(pplayer, city_tile(pcity),
2280 _("%s can't build %s from the worklist. Purging..."),
2281 city_link(pcity),
2282 /* Yes, warn about the targets that's actually
2283 in the worklist, not its obsolete-closure
2284 pupdate. */
2286 script_server_signal_emit("unit_cant_be_built", ptarget, pcity,
2287 "never");
2288 if (city_exist(saved_id)) {
2290 /* Purge this worklist item. */
2291 i--;
2293 } else {
2295 }
2296 } else {
2297 /* Yep, we can go after pupdate instead. Joy! */
2298 notify_player(pplayer, city_tile(pcity), E_WORKLIST, ftc_server,
2299 _("Production of %s is upgraded to %s in %s."),
2302 city_link(pcity));
2303 target.value.utype = pupdate;
2304 }
2305 break;
2306 }
2307 case VUT_IMPROVEMENT:
2308 {
2309 const struct impr_type *ptarget = target.value.building;
2310 const struct impr_type *pupdate = building_upgrades_to(pcity, ptarget);
2311 bool purge;
2312
2313 /* If the city can never build this improvement, drop it. */
2315 purge = !success;
2316
2317 /* Maybe this improvement has been obsoleted by something that
2318 we can build. */
2319 if (purge) {
2320 /* Nope, no use. *sigh* */
2321
2322 /* Can it be postponed? */
2324 purge = worklist_item_postpone_req_vec(&target, pcity, pplayer,
2325 saved_id);
2326
2327 /* Almost all cases emit signal in the end, so city check needed. */
2328 if (!city_exist(saved_id)) {
2329 /* Some script has removed city */
2330 return FALSE;
2331 }
2333 }
2334 } else if (success) {
2335 /* Hey, we can upgrade the improvement! */
2336 notify_player(pplayer, city_tile(pcity), E_WORKLIST, ftc_server,
2337 _("Production of %s is upgraded to %s in %s."),
2340 city_link(pcity));
2341 target.value.building = pupdate;
2342 }
2343
2344 if (purge) {
2345 /* Never in a million years. */
2346 notify_player(pplayer, city_tile(pcity),
2348 _("%s can't build %s from the worklist. Purging..."),
2349 city_link(pcity),
2351 script_server_signal_emit("building_cant_be_built", ptarget, pcity,
2352 "never");
2353 if (city_exist(saved_id)) {
2355 /* Purge this worklist item. */
2356 i--;
2358 } else {
2360 }
2361 }
2362 break;
2363 }
2364 default:
2365 /* skip useless target */
2366 log_error("worklist_change_build_target() has unrecognized "
2367 "target kind (%d)", target.kind);
2368 break;
2369 };
2370 } /* while */
2371
2372 if (success) {
2373 /* All okay. Switch targets. */
2374 change_build_target(pplayer, pcity, &target, E_WORKLIST);
2375
2376 /* i is the index immediately _after_ the item we're changing to.
2377 Remove the (i-1)th item from the worklist. */
2378 worklist_remove(pwl, i - 1);
2379 }
2380
2381 if (worklist_is_empty(pwl)) {
2382 /* There *was* something in the worklist, but it's empty now. Bug the
2383 player about it. */
2384 notify_player(pplayer, city_tile(pcity), E_WORKLIST, ftc_server,
2385 /* TRANS: The <city> worklist .... */
2386 _("The %s worklist is now empty."),
2387 city_link(pcity));
2388 }
2389
2390 return success;
2391}
2392
2393/**********************************************************************/
2398void choose_build_target(struct player *pplayer, struct city *pcity)
2399{
2400 /* Pick the next thing off the worklist. */
2401 if (worklist_change_build_target(pplayer, pcity)) {
2402 return;
2403 }
2404
2405 /* Try building the same thing again. Repeat building doesn't require a
2406 * call to change_build_target, so just return. */
2407 switch (pcity->production.kind) {
2408 case VUT_UTYPE:
2409 /* We can build a unit again unless it's unique or we have lost the tech. */
2411 && can_city_build_unit_now(pcity, pcity->production.value.utype)) {
2412 log_base(LOG_BUILD_TARGET, "%s repeats building %s", city_name_get(pcity),
2414 return;
2415 }
2416 break;
2417 case VUT_IMPROVEMENT:
2419 /* We can build space and coinage again, and possibly others. */
2420 log_base(LOG_BUILD_TARGET, "%s repeats building %s", city_name_get(pcity),
2422 return;
2423 }
2424 break;
2425 default:
2426 /* fallthru */
2427 break;
2428 };
2429
2430 /* Find *something* to do! */
2431 log_debug("Trying advisor_choose_build.");
2432 advisor_choose_build(pplayer, pcity);
2433 log_debug("Advisor_choose_build didn't kill us.");
2434}
2435
2436/**********************************************************************/
2441static const struct impr_type *building_upgrades_to(struct city *pcity,
2442 const struct impr_type *pimprove)
2443{
2444 const struct impr_type *check = pimprove;
2445 const struct impr_type *best_upgrade = NULL;
2446
2448 return NULL;
2449 }
2453 }
2454 }
2455
2456 return best_upgrade;
2457}
2458
2459/**********************************************************************/
2462static void upgrade_building_prod(struct city *pcity)
2463{
2464 const struct impr_type *producing = pcity->production.value.building;
2465 const struct impr_type *upgrading = building_upgrades_to(pcity, producing);
2466
2468 notify_player(city_owner(pcity), city_tile(pcity),
2470 _("Production of %s is upgraded to %s in %s."),
2473 city_link(pcity));
2476 }
2477}
2478
2479/**********************************************************************/
2487static const struct unit_type *unit_upgrades_to(struct city *pcity,
2488 const struct unit_type *punittype)
2489{
2490 const struct unit_type *check = punittype;
2491 const struct unit_type *best_upgrade = U_NOT_OBSOLETED;
2492
2494 return U_NOT_OBSOLETED;
2495 }
2496 while ((check = check->obsoleted_by) != U_NOT_OBSOLETED) {
2497 if (can_city_build_unit_direct(pcity, check)) {
2499 }
2500 }
2501
2502 return best_upgrade;
2503}
2504
2505/**********************************************************************/
2508static void upgrade_unit_prod(struct city *pcity)
2509{
2510 const struct unit_type *producing = pcity->production.value.utype;
2511 const struct unit_type *upgrading = unit_upgrades_to(pcity, producing);
2512
2514 notify_player(city_owner(pcity), city_tile(pcity),
2516 _("Production of %s is upgraded to %s in %s."),
2519 city_link(pcity));
2521 }
2522}
2523
2524/**********************************************************************/
2531static bool city_distribute_surplus_shields(struct player *pplayer,
2532 struct city *pcity)
2533{
2534 int size_reduction = 0;
2535 struct unit *sacrifizer;
2536
2537 if (pcity->surplus[O_SHIELD] < 0) {
2539 if (utype_upkeep_cost(unit_type_get(punit), pplayer, O_SHIELD) > 0
2540 && pcity->surplus[O_SHIELD] < 0) {
2541 const char *punit_link = unit_link(punit);
2542
2543 /* TODO: Should the unit try to help cities on adjacent tiles? That
2544 * would be a rules change. (This action is performed by the game
2545 * it self) */
2548 notify_player(pplayer, city_tile(pcity),
2550 _("%s can't upkeep %s, unit disbanded."),
2551 city_link(pcity), punit_link);
2552 }
2553
2554 /* pcity->surplus[O_SHIELD] is automatically updated. */
2555 }
2557 }
2558
2559 if (pcity->surplus[O_SHIELD] < 0) {
2560 /* Special case: MissingXProtected. This nasty unit won't go so easily.
2561 * It'd rather make the citizens pay in blood for their failure to upkeep
2562 * it! If we make it here all normal units are already disbanded, so only
2563 * undisbandable ones remain. */
2566
2567 if (upkeep > 0 && pcity->surplus[O_SHIELD] < 0) {
2568
2570 sacrifizer = punit;
2571
2572 /* No upkeep for the unit this turn. */
2573 pcity->surplus[O_SHIELD] += upkeep;
2574 }
2576 }
2577
2578 /* Now we confirm changes made last turn. */
2579 pcity->shield_stock += pcity->surplus[O_SHIELD];
2580 pcity->before_change_shields = pcity->shield_stock;
2582
2583 /* Previous turn values stored, and they are consistent with
2584 * other previous turn data.
2585 * Now reduce city size, likely messing all the values. */
2586 if (size_reduction > 0) {
2587 if (size_reduction == 1) {
2588 notify_player(pplayer, city_tile(pcity),
2590 _("Citizens in %s perish for their failure to "
2591 "upkeep %s!"),
2592 city_link(pcity), unit_link(sacrifizer));
2593 } else {
2594 notify_player(pplayer, city_tile(pcity),
2596 _("Citizens in %s perish for their failure to "
2597 "upkeep units!"),
2598 city_link(pcity));
2599 }
2600
2601 if (!city_reduce_size(pcity, size_reduction, NULL, "upkeep_failure")) {
2602 return FALSE;
2603 }
2604 }
2605
2606 return TRUE;
2607}
2608
2609/**********************************************************************/
2612static bool city_build_building(struct player *pplayer, struct city *pcity)
2613{
2614 bool space_part;
2615 int mod;
2616 const struct impr_type *pimprove;
2617 int saved_id = pcity->id;
2618
2620 /* Coinage-like improvements that convert production */
2621 fc_assert(pcity->before_change_shields >= 0);
2622
2623 /* pcity->before_change_shields already contains the surplus from
2624 * this turn. */
2625 if (city_production_has_flag(pcity, IF_GOLD)) {
2626 pplayer->economic.gold += pcity->before_change_shields;
2627 }
2628
2629 pcity->before_change_shields = 0;
2630 pcity->shield_stock = 0;
2631 choose_build_target(pplayer, pcity);
2632
2633 return TRUE;
2634 }
2635
2636 upgrade_building_prod(pcity);
2637
2638 /* The final (after upgrade) build target */
2639 pimprove = pcity->production.value.building;
2640
2641 if (!can_city_build_improvement_now(pcity, pimprove)) {
2643 _("%s is building %s, which is no longer available."),
2644 city_link(pcity),
2645 city_improvement_name_translation(pcity, pimprove));
2646 script_server_signal_emit("building_cant_be_built", pimprove, pcity,
2647 "unavailable");
2648 return TRUE;
2649 }
2650 if (pcity->shield_stock >= impr_build_shield_cost(pcity, pimprove)) {
2651 int cost;
2652
2653 if (is_small_wonder(pimprove)) {
2654 city_list_iterate(pplayer->cities, wcity) {
2655 if (city_has_building(wcity, pimprove)) {
2656 city_remove_improvement(wcity, pimprove);
2657 break;
2658 }
2660 }
2661
2662 space_part = TRUE;
2664 RPT_CERTAIN) > 0) {
2665 pplayer->spaceship.structurals++;
2667 RPT_CERTAIN) > 0) {
2668 pplayer->spaceship.components++;
2670 RPT_CERTAIN) > 0) {
2671 pplayer->spaceship.modules++;
2672 } else {
2673 space_part = FALSE;
2675 _("Completion of %s"));
2676 }
2677 cost = impr_build_shield_cost(pcity, pimprove);
2678 pcity->before_change_shields -= cost;
2679 pcity->shield_stock -= cost;
2680 pcity->turn_last_built = game.info.turn;
2681 /* to eliminate micromanagement */
2682 if (is_great_wonder(pimprove)) {
2684 _("The %s have finished building %s in %s."),
2685 nation_plural_for_player(pplayer),
2686 city_improvement_name_translation(pcity, pimprove),
2687 city_link(pcity));
2688 }
2689
2691 _("%s has finished building %s."),
2692 city_link(pcity), improvement_name_translation(pimprove));
2693 script_server_signal_emit("building_built", pimprove, pcity);
2694
2695 if (!city_exist(saved_id)) {
2696 /* Script removed city */
2697 return FALSE;
2698 }
2699
2700 /* Call this function since some buildings may change the
2701 * the vision range of a city */
2702 city_refresh_vision(pcity);
2703
2705 RPT_CERTAIN))) {
2706 struct research *presearch = research_get(pplayer);
2707 char research_name[MAX_LEN_NAME * 2];
2708 int i;
2709 const char *provider = improvement_name_translation(pimprove);
2710
2712 PL_("%s boosts research; you gain %d immediate "
2713 "advance.",
2714 "%s boosts research; you gain %d immediate "
2715 "advances.",
2716 mod), provider, mod);
2717
2719 for (i = 0; i < mod; i++) {
2722
2725 /* TRANS: Tech from building (Darwin's Voyage) */
2726 Q_("?frombldg:Acquired %s from %s."), adv_name,
2727 provider);
2728
2730 /* TRANS: Tech from building (Darwin's
2731 * Voyage) */
2732 Q_("?frombldg:The %s have acquired %s "
2733 "from %s."),
2735 }
2736 }
2737 if (space_part && pplayer->spaceship.state == SSHIP_NONE) {
2739 _("The %s have started building a spaceship!"),
2740 nation_plural_for_player(pplayer));
2741 pplayer->spaceship.state = SSHIP_STARTED;
2742 }
2743 if (space_part) {
2744 /* space ship part build */
2745 send_spaceship_info(pplayer, NULL);
2746 } else {
2747 /* Update city data. */
2748 if (city_refresh(pcity)) {
2749 auto_arrange_workers(pcity);
2750 }
2751 }
2752
2753 /* Move to the next thing in the worklist */
2754 choose_build_target(pplayer, pcity);
2755 }
2756
2757 return TRUE;
2758}
2759
2760/**********************************************************************/
2769static struct unit *city_create_unit(struct city *pcity,
2770 const struct unit_type *utype,
2771 struct citizens_reduction *red)
2772{
2773 struct player *pplayer = city_owner(pcity);
2774 struct unit *punit;
2775 int saved_unit_id;
2776 int pop_cost = utype_pop_value(utype, pcity);
2777
2778 punit = unit_virtual_prepare(pplayer, pcity->tile, utype,
2780 pcity->id, -1, -1);
2781 pplayer->score.units_built++;
2782
2783 if (pop_cost > 0 && pcity->nationality != NULL) {
2784 /* We don't reduce city size in-place to keep it correct and
2785 * existing at all while we call the following callback.
2786 * We want citizens_unit_nationality() to adjust 'red' even when
2787 * we are not setting unit nationality based on the return */
2788 struct player *nat = citizens_unit_nationality(pcity, pop_cost, red);
2789
2792 }
2793 } else if (red) {
2794 red->change = 0;
2795 }
2796
2797 (void) place_unit(punit, pplayer, pcity, NULL, FALSE);
2799
2800 /* If city has a rally point set, give the unit a move order. */
2801 if (pcity->rally_point.length) {
2806 * sizeof(struct unit_order));
2808 pcity->rally_point.length * sizeof(struct unit_order));
2809 }
2810
2811 /* This might destroy pcity and/or punit: */
2812 script_server_signal_emit("unit_built", punit, pcity);
2813
2815 return punit;
2816 } else {
2817 return NULL;
2818 }
2819}
2820
2821/**********************************************************************/
2830static bool city_build_unit(struct player *pplayer, struct city *pcity)
2831{
2832 const struct unit_type *utype;
2833 struct worklist *pwl = &pcity->worklist;
2835 int saved_city_id = pcity->id;
2836
2838
2839 /* If the city has already bought a unit which is now obsolete, don't try
2840 * to upgrade the production. The new unit might require more shields, which
2841 * would be bad if it was bought to urgently defend a city. (Equally it
2842 * might be the same cost or cheaper, but tough; you hurried the unit so
2843 * you miss out on technological advances.) */
2844 if (city_can_change_build(pcity)) {
2845 upgrade_unit_prod(pcity);
2846 }
2847
2848 utype = pcity->production.value.utype;
2850
2851 /* We must make a special case for barbarians here, because they are
2852 so dumb. Really. They don't know the prerequisite techs for units
2853 they build!! - Per */
2854 if (!can_city_build_unit_direct(pcity, utype)
2855 && !is_barbarian(pplayer)) {
2857 _("%s is building %s, which is no longer available."),
2858 city_link(pcity), utype_name_translation(utype));
2859
2860 /* Log before signal emitting, so pointers are certainly valid */
2861 log_verbose("%s %s tried to build %s, which is not available.",
2863 city_name_get(pcity), utype_rule_name(utype));
2864 script_server_signal_emit("unit_cant_be_built", utype, pcity,
2865 "unavailable");
2866 return city_exist(saved_city_id);
2867 }
2868
2869 if (pcity->shield_stock >= unit_shield_cost) {
2870 int pop_cost = utype_pop_value(utype, pcity);
2871 struct unit *punit;
2872
2873 /* Should we disband the city? -- Massimo */
2874 if (city_size_get(pcity) == pop_cost
2875 && is_city_option_set(pcity, CITYO_DISBAND)) {
2876 return !disband_city(pcity);
2877 }
2878
2879 if (city_size_get(pcity) <= pop_cost) {
2881 /* TRANS: city ... utype ... size ... pop_cost */
2882 _("%s can't build %s yet. "
2883 "(city size: %d, unit population cost: %d)"),
2885 city_size_get(pcity), pop_cost);
2886 script_server_signal_emit("unit_cant_be_built", utype, pcity,
2887 "pop_cost");
2888 return city_exist(saved_city_id);
2889 }
2890
2891 fc_assert(pop_cost == 0 || city_size_get(pcity) >= pop_cost);
2892
2893 /* don't update turn_last_built if we returned above */
2894 pcity->turn_last_built = game.info.turn;
2895
2896 /* check if we can build more than one unit (effect City_Build_Slots) */
2898
2899 /* We should be able to build at least one (by checks above) */
2900 fc_assert(num_units >= 1);
2901
2902 for (i = 0; i < num_units; i++) {
2904
2905 punit = city_create_unit(pcity, utype, natred);
2906
2907 /* Check if the city still exists (script might have removed it).
2908 * If not, we assume any effects / announcements done below were
2909 * already replaced by the script if necessary. */
2910 if (!city_exist(saved_city_id)) {
2911 break;
2912 }
2913
2914 if (punit) {
2916 /* TRANS: <city> is finished building <unit/building>. */
2917 _("%s is finished building %s."),
2918 city_link(pcity), utype_name_translation(utype));
2919 }
2920
2921 /* After we created the unit remove the citizen. This will also
2922 * rearrange the worker to take into account the extra resources
2923 * (food) needed. */
2924 if (pop_cost > 0) {
2925 /* This won't disband city due to pop_cost, but script might
2926 * still destroy city. */
2928 /* If the city has changed its nationalities during
2929 * "unit_built" signal, we take some other citizens instead */
2930 if (!city_reduce_size(pcity, pop_cost, NULL, "unit_built")) {
2931 break;
2932 }
2933 }
2934
2935 /* to eliminate micromanagement, we only subtract the unit's cost */
2936 /* signals could change the prod stock! */
2937 if ((pcity->before_change_shields -= unit_shield_cost) < 0) {
2938 pcity->before_change_shields = 0;
2939 }
2940 if ((pcity->shield_stock -= unit_shield_cost) < 0) {
2941 log_normal("City %s (%s) has built %s but has no %d shields "
2942 "for it, nullifying shield stock", city_name_get(pcity),
2943 player_name(pplayer), utype_rule_name(utype),
2945 pcity->shield_stock = 0;
2946 }
2947
2948 if (pop_cost > 0) {
2949 /* Additional message if the unit has population cost. */
2951 ftc_server,
2952 /* TRANS: "<unit> cost... <city> shrinks..."
2953 * Plural in "%d population", not "size %d". */
2954 PL_("%s cost %d population. %s shrinks to size %d.",
2955 "%s cost %d population. %s shrinks to size %d.",
2956 pop_cost),
2957 utype_name_translation(utype), pop_cost,
2958 city_link(pcity), city_size_get(pcity));
2959 }
2960
2961 if (i != 0 && worklist_length(pwl) > 0) {
2962 /* remove the build unit from the worklist; it has to be one less
2963 * than units build to preserve the next build target from the
2964 * worklist */
2965 worklist_remove(pwl, 0);
2966 }
2967 } /* for */
2968
2970 if (pcity->rally_point.length && !pcity->rally_point.persistent) {
2972 }
2973
2974 /* Done building this unit; time to move on to the next. */
2975 choose_build_target(pplayer, pcity);
2976 }
2977 } /* if */
2978
2979 return city_exist(saved_city_id);
2980}
2981
2982/**********************************************************************/
2985static bool city_build_stuff(struct player *pplayer, struct city *pcity)
2986{
2987 if (!city_distribute_surplus_shields(pplayer, pcity)) {
2988 return FALSE;
2989 }
2990
2993
2994 switch (pcity->production.kind) {
2995 case VUT_IMPROVEMENT:
2996 return city_build_building(pplayer, pcity);
2997 case VUT_UTYPE:
2998 return city_build_unit(pplayer, pcity);
2999 default:
3000 /* must never happen! */
3002 break;
3003 };
3004 return FALSE;
3005}
3006
3007/**********************************************************************/
3017static bool sell_random_building(struct player *pplayer,
3018 struct cityimpr_list *imprs)
3019{
3020 struct cityimpr *pcityimpr;
3021 int r;
3022
3023 fc_assert_ret_val(pplayer != NULL, FALSE);
3024
3025 if (!imprs || cityimpr_list_size(imprs) == 0) {
3026 return FALSE;
3027 }
3028
3031
3033 ftc_server,
3034 _("Can't afford to maintain %s in %s, building sold!"),
3036 city_link(pcityimpr->pcity));
3037 log_debug("%s: sold building (%s)", player_name(pplayer),
3039
3040 do_sell_building(pplayer, pcityimpr->pcity, pcityimpr->pimprove, "cant_maintain");
3041
3043
3044 /* Get back the gold upkeep that was already paid this turn. */
3046 pcityimpr->pimprove);
3047
3049
3051
3052 return TRUE;
3053}
3054
3055/**********************************************************************/
3063static void uk_rem_gold_callback(struct unit *punit)
3064{
3065 int gold_upkeep;
3066
3067 /* Remove the unit from uk_rem_gold. */
3069
3070 gold_upkeep = punit->server.upkeep_paid[O_GOLD];
3071
3072 /* All units in uk_rem_gold should have gold upkeep! */
3073 fc_assert_ret_msg(gold_upkeep > 0, "%s has %d gold upkeep",
3074 unit_rule_name(punit), gold_upkeep);
3075
3076 /* Get the upkeep gold back. */
3077 unit_owner(punit)->economic.gold += gold_upkeep;
3078}
3079
3080/**********************************************************************/
3084static void uk_rem_gold_append(struct unit *punit)
3085{
3086 /* Make the unit aware that it is on the uk_rem_gold list. */
3088
3089 /* Add the unit to the list. */
3091}
3092
3093/**********************************************************************/
3097static void unit_list_referred_destroy(struct unit_list *punitlist)
3098{
3100 /* Clear the unit's knowledge of the list. */
3103
3104 /* Destroy the list it self. */
3106}
3107
3108/**********************************************************************/
3120static struct unit *sell_random_unit(struct player *pplayer,
3121 struct unit_list *punitlist)
3122{
3123 struct unit *punit;
3124 int r;
3125 struct unit_list *cargo;
3126
3127 fc_assert_ret_val(pplayer != NULL, NULL);
3128
3129 if (!punitlist || unit_list_size(punitlist) == 0) {
3130 return NULL;
3131 }
3132
3135
3136 cargo = unit_list_new();
3137
3138 /* Check if unit is transporting other units from punitlist,
3139 * and sell one of those (recursively) instead.
3140 * Note that in case of recursive transports we have to iterate
3141 * also through those middle transports that themselves are not in
3142 * punitlist. */
3144 /* Optimization, do not iterate over punitlist
3145 * if we are sure that pcargo is not in it. */
3146 if (pcargo->server.upkeep_paid[O_GOLD] > 0) {
3148 if (pcargo == p2) {
3149 unit_list_append(cargo, pcargo);
3150 }
3152 }
3154
3155 if (unit_list_size(cargo) > 0) {
3156 /* Recursively sell. Note that cargo list has both
3157 * leaf units and middle transports in case of
3158 * recursive transports. */
3159 struct unit *ret = sell_random_unit(pplayer, cargo);
3160
3161 unit_list_destroy(cargo);
3162
3164
3165 return ret;
3166 }
3167
3168 unit_list_destroy(cargo);
3169
3170 {
3171 const char *punit_link = unit_tile_link(punit);
3172#ifdef FREECIV_DEBUG
3173 const char *punit_logname = unit_rule_name(punit);
3174#endif /* FREECIV_DEBUG */
3175 struct tile *utile = unit_tile(punit);
3176
3180
3181 /* The gold was paid back when the unit removal made
3182 * uk_rem_gold_callback() run as the unit's removal call back. */
3183
3184 notify_player(pplayer, utile, E_UNIT_LOST_MISC, ftc_server,
3185 _("Not enough gold. %s disbanded."),
3186 punit_link);
3187 log_debug("%s: unit sold (%s)", player_name(pplayer),
3189 } else {
3190 /* Not able to get rid of punit */
3191 return NULL;
3192 }
3193 }
3194
3196
3197 return punit;
3198}
3199
3200/**********************************************************************/
3204{
3205 struct cityimpr_list *pimprlist;
3206 bool sell_unit = TRUE;
3207
3208 if (!pplayer) {
3209 return FALSE;
3210 }
3211
3214
3215 city_list_iterate(pplayer->cities, pcity) {
3216 city_built_iterate(pcity, pimprove) {
3217 if (can_city_sell_building(pcity, pimprove)) {
3218 struct cityimpr *ci = fc_malloc(sizeof(*ci));
3219
3220 ci->pcity = pcity;
3221 ci->pimprove = pimprove;
3223 }
3226
3227 unit_list_iterate(pplayer->units, punit) {
3228 if (punit->server.upkeep_paid[O_GOLD] > 0) {
3230 }
3232
3233 while (pplayer->economic.gold < 0
3235 || unit_list_size(uk_rem_gold) > 0)) {
3237 || unit_list_size(uk_rem_gold) == 0) {
3239 } else {
3240 sell_random_unit(pplayer, uk_rem_gold);
3241 }
3243 }
3244
3245 /* Free remaining entries from list */
3247 FC_FREE(pimpr);
3249
3250 if (pplayer->economic.gold < 0) {
3251 /* If we get here it means the player has
3252 * negative gold. This should never happen. */
3253 fc_assert_msg(FALSE, "Player %s (nb %d) cannot have negative gold!",
3254 player_name(pplayer), player_number(pplayer));
3255 }
3256
3259
3260 return pplayer->economic.gold >= 0;
3261}
3262
3263/**********************************************************************/
3267{
3268 if (!pplayer) {
3269 return FALSE;
3270 }
3271
3273
3274 unit_list_iterate(pplayer->units, punit) {
3275 if (punit->server.upkeep_paid[O_GOLD] > 0) {
3277 }
3279
3280 while (pplayer->economic.gold < 0
3281 && sell_random_unit(pplayer, uk_rem_gold)) {
3282 /* All done in sell_random_unit() */
3283 }
3284
3285 if (pplayer->economic.gold < 0) {
3286 /* If we get here it means the player has
3287 * negative gold. This should never happen. */
3288 fc_assert_msg(FALSE, "Player %s (nb %d) cannot have negative gold!",
3289 player_name(pplayer), player_number(pplayer));
3290 }
3291
3293
3294 return pplayer->economic.gold >= 0;
3295}
3296
3297/**********************************************************************/
3301{
3302 struct player *pplayer;
3303 struct cityimpr_list *pimprlist;
3304
3305 if (!pcity) {
3306 return TRUE;
3307 }
3308
3309 pplayer = city_owner(pcity);
3311
3312 /* Create a vector of all buildings that can be sold. */
3313 city_built_iterate(pcity, pimprove) {
3314 if (can_city_sell_building(pcity, pimprove)) {
3315 struct cityimpr *ci = fc_malloc(sizeof(*ci));
3316
3317 ci->pcity = pcity;
3318 ci->pimprove = pimprove;
3320 }
3322
3323 /* Try to sell some buildings. */
3324 while (pplayer->economic.gold < 0
3325 && sell_random_building(pplayer, pimprlist)) {
3326 /* all done in sell_random_building */
3327 }
3328
3329 /* Free remaining entries from list */
3331 FC_FREE(pimpr);
3333
3335
3336 return pplayer->economic.gold >= 0;
3337}
3338
3339/**********************************************************************/
3348{
3349 struct player *pplayer;
3350
3351 if (!pcity) {
3352 return TRUE;
3353 }
3354
3355 pplayer = city_owner(pcity);
3357
3358 /* Create a vector of all supported units with gold upkeep. */
3360 if (punit->server.upkeep_paid[O_GOLD] > 0) {
3362 }
3364
3365 /* Still not enough gold, so try "selling" some units. */
3366 while (pplayer->economic.gold < 0
3367 && sell_random_unit(pplayer, uk_rem_gold)) {
3368 /* All done in sell_random_unit() */
3369 }
3370
3371 /* If we get here the player has negative gold, but hopefully
3372 * another city will be able to pay the deficit, so continue. */
3373
3375
3376 return pplayer->economic.gold >= 0;
3377}
3378
3379/**********************************************************************/
3382static bool place_pollution(struct city *pcity, enum extra_cause cause)
3383{
3384 struct tile *ptile;
3385 struct tile *pcenter = city_tile(pcity);
3386 int city_radius_sq = city_map_radius_sq_get(pcity);
3387 int k = 100;
3388 const struct civ_map *nmap = &(wld.map);
3389
3390 while (k > 0) {
3391 /* Place pollution on a random city tile */
3392 int cx, cy;
3393 int tile_id = fc_rand(city_map_tiles(city_radius_sq));
3394 struct extra_type *pextra;
3395
3396 city_tile_index_to_xy(&cx, &cy, tile_id, city_radius_sq);
3397
3398 /* Check for a real map position */
3399 if (!(ptile = city_map_to_tile(nmap, pcenter, city_radius_sq, cx, cy))) {
3400 continue;
3401 }
3402
3403 pextra = rand_extra_for_tile(ptile, cause, FALSE);
3404
3405 if (pextra != NULL && !tile_has_extra(ptile, pextra)) {
3406 tile_add_extra(ptile, pextra);
3407 update_tile_knowledge(ptile);
3408
3409 return TRUE;
3410 }
3411 k--;
3412 }
3413 log_debug("pollution not placed: city: %s", city_name_get(pcity));
3414
3415 return FALSE;
3416}
3417
3418/**********************************************************************/
3421static void check_pollution(struct city *pcity)
3422{
3423 if (fc_rand(100) < pcity->pollution) {
3424 if (place_pollution(pcity, EC_POLLUTION)) {
3426 _("Pollution near %s."), city_link(pcity));
3427 }
3428 }
3429}
3430
3431/**********************************************************************/
3438int city_incite_cost(struct player *pplayer, struct city *pcity)
3439{
3440 int dist, size;
3441 double cost; /* Intermediate values can get very large */
3442
3443 /* Gold factor */
3444 cost = city_owner(pcity)->economic.gold + game.server.base_incite_cost;
3445
3446 unit_list_iterate(pcity->tile->units, punit) {
3450
3451 /* Buildings */
3452 city_built_iterate(pcity, pimprove) {
3453 cost += impr_build_shield_cost(pcity, pimprove)
3456
3457 /* Stability bonuses */
3458 if (!city_unhappy(pcity)) {
3459 cost *= 2;
3460 }
3461 if (city_celebrating(pcity)) {
3462 cost *= 2;
3463 }
3464
3465 /* Buy back is cheap, conquered cities are also cheap */
3467 if (city_owner(pcity) != pcity->original) {
3468 if (pplayer == pcity->original) {
3469 cost /= 2; /* Buy back: 50% price reduction */
3470 } else {
3471 cost = cost * 2 / 3; /* Buy conquered: 33% price reduction */
3472 }
3473 }
3474 }
3475
3476 /* Distance from capital */
3477 /* Max penalty. Applied if there is no capital, or it's even further away. */
3478 dist = 32;
3479 city_list_iterate(city_owner(pcity)->cities, capital) {
3480 if (is_capital(capital)) {
3481 int tmp = map_distance(capital->tile, pcity->tile);
3482
3483 if (tmp < dist) {
3484 dist = tmp;
3485 }
3486 }
3488
3489 size = MAX(1, city_size_get(pcity)
3492 - pcity->feel[CITIZEN_ANGRY][FEELING_FINAL] * 3);
3493 cost *= size;
3495 cost = cost / (dist + 3);
3496
3498 int cost_per_citizen = cost / pcity->size;
3499 int natives = citizens_nation_get(pcity, city_owner(pcity)->slot);
3500 int tgt_cit = citizens_nation_get(pcity, pplayer->slot);
3501 int third_party = pcity->size - natives - tgt_cit;
3502
3503 cost = cost_per_citizen * (natives + 0.7 * third_party + 0.5 * tgt_cit);
3504 }
3505
3506 cost += (cost * get_city_bonus(pcity, EFT_INCITE_COST_PCT)) / 100;
3507 cost /= 100;
3508
3511 } else {
3512 return cost;
3513 }
3514}
3515
3516/**********************************************************************/
3519static void define_orig_production_values(struct city *pcity)
3520{
3521 /* Remember what this city is building last turn, so that on the next turn
3522 * the player can switch production to something else and then change it
3523 * back without penalty. This has to be updated _before_ production for
3524 * this turn is calculated, so that the penalty will apply if the player
3525 * changes production away from what has just been completed. This makes
3526 * sense if you consider what this value means: all the shields in the
3527 * city have been dedicated toward the project that was chosen last turn,
3528 * so the player shouldn't be penalized if the governor has to pick
3529 * something different. See city_change_production_penalty(). */
3530 pcity->changed_from = pcity->production;
3531
3532 log_debug("In %s, building %s. Beg of Turn shields = %d",
3534 pcity->before_change_shields);
3535}
3536
3537/**********************************************************************/
3540static void nullify_caravan_and_disband_plus(struct city *pcity)
3541{
3542 pcity->disbanded_shields = 0;
3543 pcity->caravan_shields = 0;
3544}
3545
3546/**********************************************************************/
3551{
3553 pcity->before_change_shields = 0;
3554}
3555
3556/**********************************************************************/
3559static void update_city_activity(struct city *pcity)
3560{
3561 struct player *pplayer;
3562 struct government *gov;
3563 bool is_happy;
3564 bool is_celebrating;
3565
3566 if (!pcity) {
3567 return;
3568 }
3569
3570 pplayer = city_owner(pcity);
3571 gov = government_of_city(pcity);
3572 is_happy = city_happy(pcity);
3574
3575 if (city_refresh(pcity)) {
3576 auto_arrange_workers(pcity);
3577 }
3578
3579 /* Reporting of celebrations rewritten, copying the treatment of disorder below,
3580 with the added rapture rounds count. 991219 -- Jing */
3581 if (city_build_stuff(pplayer, pcity)) {
3582 int saved_id;
3583 int revolution_turns;
3584
3585 pcity->history += city_history_gain(pcity);
3586
3587 /* History can decrease, but never go below zero */
3588 pcity->history = MAX(pcity->history, 0);
3589
3590 /* Keep old behaviour when building new improvement could keep
3591 city celebrating */
3592 if (!is_happy) {
3593 is_happy = city_happy(pcity);
3594 }
3595
3596 if (city_celebrating(pcity) || is_celebrating) {
3597 pcity->rapture++;
3598
3599 /* Update city's celebrating counters */
3601 if (pcount->type == CB_CITY_CELEBRATION_TURNS) {
3602 pcity->counter_values[pcount->index]++;
3603 }
3605
3606 if (pcity->rapture == 1) {
3608 _("Celebrations in your honor in %s."),
3609 city_link(pcity));
3610 }
3611 } else {
3612 if (pcity->rapture != 0) {
3614 _("Celebrations canceled in %s."),
3615 city_link(pcity));
3616 }
3617
3618 /* Update city's celebrating counters */
3620 if (pcount->type == CB_CITY_CELEBRATION_TURNS) {
3621 pcity->counter_values[pcount->index] = 0;
3622 }
3624 pcity->rapture = 0;
3625 }
3626 pcity->was_happy = is_happy;
3627
3628 /* Handle the illness. */
3629 if (game.info.illness_on) {
3630 /* Recalculate city illness; illness due to trade has to be saved
3631 * within the city struct, as the client does not have all
3632 * the data to calculate it */
3633 pcity->server.illness
3634 = city_illness_calc(pcity, NULL, NULL, &(pcity->illness_trade), NULL);
3635
3636 if (city_illness_check(pcity)) {
3637 if (!city_illness_strike(pcity)) {
3638 /* Illness destroyed the city */
3639 return;
3640 }
3641 }
3642 }
3643
3644 /* City population updated here, after the rapture stuff above. --Jing */
3645 saved_id = pcity->id;
3646 pcity->had_famine = FALSE;
3647 city_populate(pcity, pplayer);
3648 if (NULL == player_city_by_number(pplayer, saved_id)) {
3649 return;
3650 }
3651
3652 pcity->did_sell = FALSE;
3653 pcity->did_buy = FALSE;
3654 pcity->airlift = city_airlift_max(pcity);
3655 update_bulbs(pplayer, pcity->prod[O_SCIENCE], FALSE, FALSE);
3656
3658
3659 /* Update the treasury. */
3660 pplayer->economic.gold += pcity->surplus[O_GOLD];
3661
3662 /* FIXME: Nation level upkeep should be paid after ALL cities
3663 * have been processed, not after each individual city. */
3665 /* Unit upkeep was not included in city balance ->
3666 * not reduced from the city surplus. */
3667 pplayer->economic.gold -= city_total_unit_gold_upkeep(pcity);
3668
3670 /* Building upkeep was not included in city balance ->
3671 * not reduced from the city surplus. */
3672 pplayer->economic.gold -= city_total_impr_gold_upkeep(pcity);
3673 }
3674 }
3675
3676 /* Remember how much gold upkeep each unit was paid. */
3680
3681 if (pplayer->economic.gold < 0) {
3682 /* Not enough gold - we have to sell some buildings, and if that
3683 * is not enough, disband units with gold upkeep, taking into
3684 * account the setting of 'game.info.gold_upkeep_style':
3685 * GOLD_UPKEEP_CITY: Cities pay for buildings and units.
3686 * GOLD_UPKEEP_MIXED: Cities pay only for buildings; the nation pays
3687 * for units.
3688 * GOLD_UPKEEP_NATION: The nation pays for buildings and units. */
3689 switch (game.info.gold_upkeep_style) {
3690 case GOLD_UPKEEP_CITY:
3691 case GOLD_UPKEEP_MIXED:
3695 }
3696 break;
3697 case GOLD_UPKEEP_NATION:
3698 break;
3699 }
3700 }
3701
3703 if (city_unhappy(pcity)) {
3704 const char *revomsg;
3705
3706 pcity->anarchy++;
3707
3709 if (pcount->type == CB_CITY_DISORDER_TURNS) {
3710 pcity->counter_values[pcount->index]++;
3711 }
3713
3714 if (pcity->anarchy == revolution_turns) {
3715 /* Revolution next turn if not dealt with */
3716 /* TRANS: preserve leading space; this string will be appended to
3717 * another sentence */
3718 revomsg = _(" Unrest threatens to spread beyond the city.");
3719 } else {
3720 revomsg = "";
3721 }
3722 if (pcity->anarchy == 1) {
3724 /* TRANS: second %s is an optional extra sentence */
3725 _("Civil disorder in %s.%s"),
3726 city_link(pcity), revomsg);
3727 } else {
3729 /* TRANS: second %s is an optional extra sentence */
3730 _("CIVIL DISORDER CONTINUES in %s.%s"),
3731 city_link(pcity), revomsg);
3732 }
3733 } else {
3734 if (pcity->anarchy != 0) {
3736 _("Order restored in %s."),
3737 city_link(pcity));
3738 }
3739 pcity->anarchy = 0;
3740
3742 if (pcount->type == CB_CITY_DISORDER_TURNS) {
3743 pcity->counter_values[pcount->index] = 0;
3744 }
3746 }
3747 check_pollution(pcity);
3748
3749 send_city_info(NULL, pcity);
3750
3751 if (revolution_turns > 0 && pcity->anarchy > revolution_turns) {
3752 notify_player(pplayer, city_tile(pcity), E_ANARCHY, ftc_server,
3753 /* TRANS: %s - government form, e.g., Democracy */
3754 _("The people have overthrown your %s, "
3755 "your country is in turmoil."),
3758 }
3759 if (city_refresh(pcity)) {
3760 auto_arrange_workers(pcity);
3761 }
3762 sanity_check_city(pcity);
3763 }
3764}
3765
3766/**********************************************************************/
3769static bool city_illness_check(const struct city * pcity)
3770{
3771 if (fc_rand(1000) < pcity->server.illness) {
3772 return TRUE;
3773 }
3774
3775 return FALSE;
3776}
3777
3778/**********************************************************************/
3782static bool disband_city(struct city *pcity)
3783{
3784 struct player *pplayer = city_owner(pcity);
3785 struct tile *ptile = pcity->tile;
3786 struct city *rcity = NULL;
3787 const struct unit_type *utype = pcity->production.value.utype;
3788 struct unit *punit;
3789 int saved_id = pcity->id;
3790
3791 /* find closest city other than pcity */
3792 rcity = find_closest_city(ptile, pcity, pplayer, FALSE, FALSE, FALSE, TRUE,
3793 FALSE, NULL);
3794
3795 if (!rcity) {
3796 /* What should we do when we try to disband our only city? */
3797 notify_player(pplayer, ptile, E_CITY_CANTBUILD, ftc_server,
3798 _("%s can't build %s yet, "
3799 "as we can't disband our only city."),
3801 script_server_signal_emit("unit_cant_be_built", utype, pcity,
3802 "pop_cost");
3803 if (!city_exist(saved_id)) {
3804 /* Script decided to remove even the last city */
3805 return TRUE;
3806 } else {
3807 return FALSE;
3808 }
3809 }
3810
3811 punit = city_create_unit(pcity, utype, NULL);
3812
3813 /* "unit_built" script handler may have destroyed city. If so, we
3814 * assume something sensible happened to its units, and that the
3815 * script took care of announcing unit creation if required. */
3816 if (city_exist(saved_id)) {
3817 /* Shift all the units supported by pcity (including the new unit)
3818 * to rcity. transfer_city_units does not make sure no units are
3819 * left floating without a transport, but since all units are
3820 * transferred this is not a problem. */
3821 transfer_city_units(pplayer, pplayer, pcity->units_supported, rcity,
3822 pcity, -1, TRUE);
3823
3824 if (punit) {
3825 notify_player(pplayer, ptile, E_UNIT_BUILT, ftc_server,
3826 /* TRANS: "<city> is disbanded into Settler." */
3827 _("%s is disbanded into %s."),
3829 }
3830
3831 script_server_signal_emit("city_destroyed", pcity, pcity->owner, NULL);
3832
3833 if (!city_exist(saved_id)) {
3834 /* Already removed during the script */
3835 return TRUE;
3836 }
3837 remove_city(pcity);
3838
3839 /* Since we've removed the city, we don't need to worry about
3840 * charging for production, disabling rally points, etc. */
3841 }
3842
3843 return TRUE;
3844}
3845
3846/**********************************************************************/
3894static float city_migration_score(struct city *pcity)
3895{
3896 float score = 0.0;
3897 int build_shield_cost = 0;
3898 bool has_wonder = FALSE;
3899
3900 if (!pcity) {
3901 return score;
3902 }
3903
3904 if (pcity->server.mgr_score_calc_turn == game.info.turn) {
3905 /* up-to-date migration score */
3906 return pcity->server.migration_score;
3907 }
3908
3909 /* feeling of the citizens */
3910 score = (city_size_get(pcity)
3911 + 1.00 * pcity->feel[CITIZEN_HAPPY][FEELING_FINAL]
3912 + 0.00 * pcity->feel[CITIZEN_CONTENT][FEELING_FINAL]
3913 - 0.25 * pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL]
3914 - 0.50 * pcity->feel[CITIZEN_ANGRY][FEELING_FINAL]);
3915
3916 /* calculate shield build cost for all buildings */
3917 city_built_iterate(pcity, pimprove) {
3918 build_shield_cost += impr_build_shield_cost(pcity, pimprove);
3919 if (is_wonder(pimprove)) {
3920 /* this city has a wonder */
3921 has_wonder = TRUE;
3922 }
3924
3925 /* take shield costs of all buidings into account; normalized by 1000 */
3926 score *= (1 + (1 - exp(- (float) MAX(0, build_shield_cost) / 1000)) / 5);
3927 /* take trade into account; normalized by 100 */
3928 score *= (1 + (1 - exp(- (float) MAX(0, pcity->surplus[O_TRADE]) / 100))
3929 / 5);
3930 /* take luxury into account; normalized by 100 */
3931 score *= (1 + (1 - exp(- (float) MAX(0, pcity->surplus[O_LUXURY]) / 100))
3932 / 5);
3933 /* take science into account; normalized by 100 */
3934 score *= (1 + (1 - exp(- (float) MAX(0, pcity->surplus[O_SCIENCE]) / 100))
3935 / 5);
3936
3937 score += city_culture(pcity) * game.info.culture_migration_pml / 1000;
3938
3939 /* Take food into account; the food surplus is clipped to values between
3940 * -10..20 and normalize by 10. Thus, the factor is between 0.9 and 1.2. */
3941 score *= (1 + (float) CLIP(-10, pcity->surplus[O_FOOD], 20) / 10 );
3942
3943 /* Reduce the score due to city illness (plague). The illness is given in
3944 * tenth of percent (0..1000) and normalized by 25. Thus, this factor is
3945 * between 0.6 (ill city) and 1.0 (health city). */
3946 score *= (100 - (float)city_illness_calc(pcity, NULL, NULL, NULL, NULL)
3947 / 25);
3948
3949 if (has_wonder) {
3950 /* people like wonders */
3951 score *= 1.25;
3952 }
3953
3954 if (is_capital(pcity)) {
3955 /* the capital is a magnet for the citizens */
3956 score *= 1.25;
3957 }
3958
3959 /* take into account effects */
3960 score *= (1.0 + get_city_bonus(pcity, EFT_MIGRATION_PCT) / 100.0);
3961
3962 log_debug("[M] %s score: %.3f", city_name_get(pcity), score);
3963
3964 /* set migration score for the city */
3965 pcity->server.migration_score = score;
3966 /* set the turn, when the score was calculated */
3968
3969 return score;
3970}
3971
3972/**********************************************************************/
3979 struct city *pcity_to)
3980{
3982 struct tile *ptile_from, *ptile_to;
3984 const char *nation_from, *nation_to;
3985 struct city *rcity = NULL;
3986 int to_id = pcity_to->id;
3987 const struct civ_map *nmap = &(wld.map);
3988
3989 if (!pcity_from || !pcity_to) {
3990 return FALSE;
3991 }
3992
3996 /* We copy that, because city_link always returns the same pointer. */
4003
4004 /* Check food supply in the receiver city */
4006 bool migration = FALSE;
4007
4008 if (pcity_to->surplus[O_FOOD] >= game.info.food_cost) {
4009 migration = TRUE;
4010 } else {
4011 /* Check if there is a free tile for the new citizen which, when worked,
4012 * leads to zero or positive food surplus for the enlarged city */
4013 int max_food_tile = -1; /* no free tile */
4014
4016 city_tile(pcity_to), ptile) {
4017 if (city_can_work_tile(pcity_to, ptile)
4018 && tile_worked(ptile) != pcity_to) {
4019 /* Safest assumption is that city won't be celebrating once an
4020 * additional citizen is added */
4023 }
4025 if (max_food_tile >= 0
4026 && pcity_to->surplus[O_FOOD] + max_food_tile >= game.info.food_cost) {
4027 migration = TRUE;
4028 }
4029 }
4030
4031 if (!migration) {
4032 /* insufficiency food in receiver city; no additional citizens */
4033 if (pplayer_from == pplayer_to) {
4034 /* migration between one nation */
4036 /* TRANS: From <city1> to <city2>. */
4037 _("Migrants from %s can't go to %s because there is "
4038 "not enough food available!"),
4040 } else {
4041 /* migration between different nations */
4043 /* TRANS: From <city1> to <city2> (<city2 nation adjective>). */
4044 _("Migrants from %s can't go to %s (%s) because there "
4045 "is not enough food available!"),
4048 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
4049 _("Migrants from %s (%s) can't go to %s because there "
4050 "is not enough food available!"),
4052 }
4053
4054 return FALSE;
4055 }
4056 }
4057
4059 /* receiver city can't grow */
4060 if (pplayer_from == pplayer_to) {
4061 /* migration between one nation */
4063 /* TRANS: From <city1> to <city2>. */
4064 _("Migrants from %s can't go to %s because it needs "
4065 "an improvement to grow!"),
4067 } else {
4068 /* migration between different nations */
4070 /* TRANS: From <city1> to <city2> of <city2 nation adjective>. */
4071 _("Migrants from %s can't go to %s (%s) because it "
4072 "needs an improvement to grow!"),
4075 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
4076 _("Migrants from %s (%s) can't go to %s because it "
4077 "needs an improvement to grow!"),
4079 }
4080
4081 return FALSE;
4082 }
4083
4084 /* reduce size of giver */
4085 if (city_size_get(pcity_from) == 1) {
4086
4088 /* Preserve nationality of city's only citizen */
4090 }
4091
4092 /* do not destroy wonders */
4093 city_built_iterate(pcity_from, pimprove) {
4094 if (is_wonder(pimprove)) {
4095 return FALSE;
4096 }
4098
4099 /* find closest city other of the same player than pcity_from */
4101 FALSE, FALSE, TRUE, FALSE, NULL);
4102
4103 if (rcity) {
4104 int id = pcity_from->id;
4105
4106 /* transfer all units to the closest city */
4108 pcity_from->units_supported, rcity, pcity_from,
4109 -1, TRUE);
4111
4112 script_server_signal_emit("city_size_change", pcity_from,
4113 (lua_Integer)(-1), "migration_from");
4114
4115 if (city_exist(id)) {
4116 script_server_signal_emit("city_destroyed", pcity_from,
4117 pcity_from->owner, NULL);
4118
4119 if (city_exist(id)) {
4121 }
4122 }
4123
4125 _("%s was disbanded by its citizens."),
4126 name_from);
4127 } else {
4128 /* it's the only city of the nation */
4129 return FALSE;
4130 }
4131 } else {
4132 /* the migrants take half of the food box with them (this prevents
4133 * migration -> grow -> migration -> ... cycles) */
4134 pcity_from->food_stock /= 2;
4135
4137 /* Those citizens that are from the target nation are most
4138 * ones migrating. */
4139 if (citizens_nation_get(pcity_from, pplayer_to->slot) > 0) {
4141 } else if (!citizens_nation_get(pcity_from, pplayer_citizen->slot)) {
4142 /* No native citizens at all in the city, choose random foreigner */
4144
4146 }
4147 /* This should be followed by city_reduce_size(). */
4149 }
4150 city_reduce_size(pcity_from, 1, pplayer_from, "migration_from");
4152 if (city_refresh(pcity_from)) {
4154 }
4155 }
4156
4157 /* This should be _before_ the size of the city is increased. Thus, the
4158 * order of the messages is correct (1: migration; 2: increased size). */
4159 if (pplayer_from == pplayer_to) {
4160 /* migration between one nation */
4162 /* TRANS: From <city1> to <city2>. */
4163 _("Migrants from %s moved to %s in search of a better "
4164 "life."), name_from, name_to);
4165 } else {
4166 /* migration between different nations */
4168 /* TRANS: From <city1> to <city2> (<city2 nation adjective>). */
4169 _("Migrants from %s moved to %s (%s) in search of a "
4170 "better life."),
4173 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
4174 _("Migrants from %s (%s) moved to %s in search of a "
4175 "better life."),
4177 }
4178
4179 /* Increase size of receiver city */
4180 if (city_exist(to_id)) {
4182
4183 if (city_exist(to_id)) {
4186 if (city_refresh(pcity_to)) {
4188 }
4189 if (incr_success) {
4190 script_server_signal_emit("city_size_change", pcity_to,
4191 (lua_Integer)1, "migration_to");
4192 }
4193 }
4194 }
4195
4196 log_debug("[M] T%d migration successful (%s -> %s)",
4198
4199 return TRUE;
4200}
4201
4202/**********************************************************************/
4224{
4225 bool internat = FALSE;
4226
4227 if (!game.server.migration) {
4228 return FALSE;
4229 }
4230
4232 || (game.server.mgr_worldchance <= 0
4233 && game.server.mgr_nationchance <= 0)) {
4234 return FALSE;
4235 }
4236
4237 /* check for migration */
4238 players_iterate(pplayer) {
4239 if (!pplayer->cities) {
4240 continue;
4241 }
4242
4243 if (check_city_migrations_player(pplayer)) {
4244 internat = TRUE;
4245 }
4247
4248 return internat;
4249}
4250
4251/**********************************************************************/
4255bool city_empty_food_stock(struct city *pcity) {
4256 struct player *pplayer = city_owner(pcity);
4257 struct tile *ptile = city_tile(pcity);
4258
4259 fc_assert_ret_val(pcity != NULL, FALSE);
4260
4261 if (pcity->food_stock > 0) {
4262 pcity->food_stock = 0;
4263
4264 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4265 /* TRANS: %s is a city name */
4266 _("All stored food destroyed in %s."), city_link(pcity));
4267
4268 return TRUE;
4269 }
4270
4271 return FALSE;
4272}
4273
4274/**********************************************************************/
4277static void apply_disaster(struct city *pcity, struct disaster_type *pdis)
4278{
4279 struct player *pplayer = city_owner(pcity);
4280 struct tile *ptile = city_tile(pcity);
4282
4283 log_debug("%s at %s", disaster_rule_name(pdis), city_name_get(pcity));
4284
4285 notify_player(pplayer, ptile, E_DISASTER,
4286 ftc_server,
4287 /* TRANS: Disasters such as Earthquake */
4288 _("%s was hit by %s."), city_name_get(pcity),
4290
4292 if (pplayer->economic.gold > 0 && pcity->prod[O_TRADE] > 0) {
4293 int amount = pcity->prod[O_TRADE] * 5;
4294
4295 amount = MIN(pplayer->economic.gold, amount);
4296 pplayer->economic.gold -= amount;
4297 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4298 PL_("Robbery in %s. %d gold stolen.",
4299 "Robbery in %s. %d gold stolen.", amount),
4300 city_link(pcity), amount);
4302 }
4303 }
4304
4306 if (place_pollution(pcity, EC_POLLUTION)) {
4307 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4308 _("Pollution near %s."), city_link(pcity));
4310 }
4311 }
4312
4314 if (place_pollution(pcity, EC_FALLOUT)) {
4315 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4316 _("Fallout near %s."), city_link(pcity));
4318 }
4319 }
4320
4323 && pcity->size > 1)) {
4324 if (!city_reduce_size(pcity, 1, NULL, "disaster")) {
4325 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4326 /* TRANS: "Industrial Accident destroys Bogota entirely." */
4327 _("%s destroys %s entirely."),
4329 pcity = NULL;
4330 } else {
4331 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4332 /* TRANS: "Nuclear Accident ... Montreal." */
4333 _("%s causes population loss in %s."),
4335 }
4336
4338 }
4339
4341 int total = 0;
4342 struct impr_type *imprs[B_LAST];
4343
4344 city_built_iterate(pcity, pimprove) {
4345 if (is_improvement(pimprove)
4346 && !improvement_has_flag(pimprove, IF_DISASTER_PROOF)) {
4347 imprs[total++] = pimprove;
4348 }
4350
4351 if (total > 0) {
4352 int num = fc_rand(total);
4353
4354 building_lost(pcity, imprs[num], "disaster", NULL);
4355
4356 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4357 /* TRANS: second %s is the name of a city improvement */
4358 _("%s destroys %s in %s."),
4361 city_link(pcity));
4362
4364 }
4365 }
4366
4368 if (city_empty_food_stock(pcity)) {
4370 }
4371 }
4372
4374 if (pcity->shield_stock > 0) {
4375 char prod[256];
4376
4377 pcity->shield_stock = 0;
4378 nullify_prechange_production(pcity); /* Make it impossible to recover */
4379
4380 universal_name_translation(&pcity->production, prod, sizeof(prod));
4381 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4382 /* TRANS: "Production of Colossus in Rhodes destroyed." */
4383 _("Production of %s in %s destroyed."),
4384 prod, city_link(pcity));
4385
4387 }
4388 }
4389
4390 script_server_signal_emit("disaster_occurred", pdis, pcity,
4392 script_server_signal_emit("disaster", pdis, pcity);
4393}
4394
4395/**********************************************************************/
4399{
4400 if (game.info.disasters == 0) {
4401 /* Shortcut out as no disaster is possible. */
4402 return;
4403 }
4404
4405 players_iterate(pplayer) {
4406 /* Safe city iterator needed as disaster may destroy city */
4407 city_list_iterate_safe(pplayer->cities, pcity) {
4408 int id = pcity->id;
4409
4411 if (city_exist(id)) {
4412 /* City survived earlier disasters. */
4413 int probability = game.info.disasters * pdis->frequency;
4414 int result = fc_rand(DISASTER_BASE_RARITY);
4415
4416 if (result < probability) {
4417 if (can_disaster_happen(pdis, pcity)) {
4418 apply_disaster(pcity, pdis);
4419 }
4420 }
4421 }
4425}
4426
4427/**********************************************************************/
4435static bool check_city_migrations_player(const struct player *pplayer)
4436{
4440 float score_from, score_tmp, weight;
4441 int dist, mgr_dist;
4442 bool internat = FALSE;
4443
4444 /* check for each city
4445 * city_list_iterate_safe_end must be used because we could
4446 * remove one city from the list */
4447 city_list_iterate_safe(pplayer->cities, pcity) {
4448 /* no migration out of the capital */
4449 if (is_capital(pcity)) {
4450 continue;
4451 }
4452
4453 /* check only each (game.server.mgr_turninterval) turn
4454 * (counted from the funding turn) and do not migrate
4455 * the same turn a city is founded */
4456 if (game.info.turn == pcity->turn_founded
4457 || ((game.info.turn - pcity->turn_founded)
4458 % game.server.mgr_turninterval) != 0) {
4459 continue;
4460 }
4461
4466
4467 /* score of the actual city
4468 * taking into account a persistence factor of 3 */
4469 score_from = city_migration_score(pcity) * 3;
4470
4471 log_debug("[M] T%d check city: %s score: %6.3f (%s)",
4473 player_name(pplayer));
4474
4475 /* consider all cities within the maximal possible distance
4476 * (= CITY_MAP_MAX_RADIUS + GAME_MAX_MGR_DISTANCE) */
4477 iterate_outward(&(wld.map), city_tile(pcity),
4479 acity = tile_city(ptile);
4480
4481 if (!acity || acity == pcity) {
4482 /* no city or the city in the center */
4483 continue;
4484 }
4485
4486 /* Calculate the migration distance. The value of
4487 * game.server.mgr_distance is added to the current city radius. If the
4488 * distance between both cities is lower or equal than this value,
4489 * migration is possible. */
4492
4493 /* distance between the two cities */
4494 dist = real_map_distance(city_tile(pcity), city_tile(acity));
4495
4496 if (dist > mgr_dist) {
4497 /* to far away */
4498 continue;
4499 }
4500
4501 /* score of the second city, weighted by the distance */
4502 weight = ((float) (mgr_dist + 1 - dist) / (float) (mgr_dist + 1));
4504
4505 log_debug("[M] T%d - compare city: %s (%s) dist: %d mgr_dist: %d "
4506 "score: %6.3f", game.info.turn, city_name_get(acity),
4508
4509 if (game.server.mgr_nationchance > 0 && city_owner(acity) == pplayer) {
4510 /* migration between cities of the same owner */
4512 /* select the best! */
4515
4516 log_debug("[M] T%d - best city (player): %s (%s) score: "
4517 "%6.3f (> %6.3f)", game.info.turn,
4520 }
4521 } else if (game.server.mgr_worldchance > 0
4522 && city_owner(acity) != pplayer) {
4523 /* migration between cities of different owners */
4525 /* Modify the score if citizens could migrate to a city of their
4526 * original nation. */
4527 if (citizens_nation_get(pcity, city_owner(acity)->slot) > 0) {
4528 score_tmp *= 2;
4529 }
4530 }
4531
4533 /* select the best! */
4536
4537 log_debug("[M] T%d - best city (world): %s (%s) score: "
4538 "%6.3f (> %6.3f)", game.info.turn,
4542 }
4543 }
4545
4546 if (best_city_player != NULL) {
4547 /* First, do the migration within one nation */
4548 if (fc_rand(100) >= game.server.mgr_nationchance) {
4549 /* No migration */
4550 /* N.B.: city_link always returns the same pointer. */
4553 _("Citizens of %s are thinking about migrating to %s "
4554 "for a better life."),
4556 } else {
4558 }
4559
4560 /* Stop here */
4561 continue;
4562 }
4563
4564 if (best_city_world != NULL) {
4565 /* Second, do the migration between all nations */
4566 if (fc_rand(100) >= game.server.mgr_worldchance) {
4567 const char *nname;
4568
4570 /* No migration */
4571 /* N.B.: city_link always returns the same pointer. */
4574 /* TRANS: <city1> to <city2> (<city2 nation adjective>). */
4575 _("Citizens of %s are thinking about migrating to %s "
4576 "(%s) for a better life."),
4578 } else {
4580 internat = TRUE;
4581 }
4582
4583 /* Stop here */
4584 continue;
4585 }
4587
4588 return internat;
4589}
4590
4591/**********************************************************************/
4594void city_style_refresh(struct city *pcity)
4595{
4596 pcity->style = city_style(pcity);
4597}
4598
4599/**********************************************************************/
4603void city_counters_refresh(struct city *pcity)
4604{
4606 struct packet_city_update_counters packet;
4607
4608 packet.city = pcity->id;
4609
4611
4612 packet.count = counter_count;
4613 for (i = 0; i < counter_count; i++) {
4614 packet.counters[i] = pcity->counter_values[i];
4615 }
4616
4619}
4620
4621/**********************************************************************/
4624void city_tc_effect_refresh(struct player *pplayer)
4625{
4626 const struct civ_map *nmap = &(wld.map);
4627
4628 city_list_iterate(pplayer->cities, pcity) {
4629 bool changed = FALSE;
4630
4632 city_tile(pcity), ptile, idx, x, y) {
4633 if (ptile->worked == pcity
4634 && get_city_tile_output_bonus(pcity, ptile, NULL, EFT_TILE_WORKABLE) <= 0) {
4635 city_map_update_empty(pcity, ptile);
4636 pcity->specialists[DEFAULT_SPECIALIST]++;
4637 changed = TRUE;
4638 }
4640
4641 if (changed) {
4642 auto_arrange_workers(pcity);
4643 send_city_info(NULL, pcity);
4644 }
4646}
const char * achievement_name_translation(struct achievement *pach)
const struct action_auto_perf * action_auto_perf_unit_sel(const enum action_auto_perf_cause cause, const struct unit *actor, const struct player *other_player, const struct output_type *eval_output, const struct action *eval_action)
const struct action * action_auto_perf_unit_do(const enum action_auto_perf_cause cause, struct unit *actor, const struct player *other_player, const struct output_type *eval_output, const struct action *eval_action, const struct tile *target_tile, const struct city *target_city, const struct unit *target_unit, const struct extra_type *target_extra)
void advisor_choose_build(struct player *pplayer, struct city *pcity)
#define n
Definition astring.c:77
int tile_border_source_radius_sq(struct tile *ptile)
Definition borders.c:33
const char * textcalfrag(int frag)
Definition calendar.c:101
const char * textyear(int year)
Definition calendar.c:120
struct player_slot * citizens_random(const struct city *pcity)
Definition citizens.c:190
void citizens_nation_add(struct city *pcity, const struct player_slot *pslot, int add)
Definition citizens.c:104
citizens citizens_nation_get(const struct city *pcity, const struct player_slot *pslot)
Definition citizens.c:74
struct player * citizens_unit_nationality(const struct city *pcity, int pop_cost, struct citizens_reduction *pchange)
void citizens_reduction_apply(struct city *pcity, const struct citizens_reduction *pchange)
void citizens_convert(struct city *pcity)
void citizens_update(struct city *pcity, struct player *plr)
const char * city_improvement_name_translation(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:663
bool can_city_build_unit_later(const struct city *pcity, const struct unit_type *punittype)
Definition city.c:968
int city_granary_size(int city_size)
Definition city.c:2129
bool can_city_build_now(const struct city *pcity, const struct universal *target)
Definition city.c:1011
struct tile * city_map_to_tile(const struct civ_map *nmap, const struct tile *city_center, int city_radius_sq, int city_map_x, int city_map_y)
Definition city.c:305
bool city_has_building(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:1236
bool is_capital(const struct city *pcity)
Definition city.c:1575
const char * city_name_get(const struct city *pcity)
Definition city.c:1133
void city_remove_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3367
int city_improvement_upkeep(const struct city *pcity, const struct impr_type *b)
Definition city.c:1251
int city_airlift_max(const struct city *pcity)
Definition city.c:2939
struct output_type * get_output_type(Output_type_id output)
Definition city.c:638
bool is_city_option_set(const struct city *pcity, enum city_options option)
Definition city.c:3384
void city_size_add(struct city *pcity, int add)
Definition city.c:1160
bool city_production_has_flag(const struct city *pcity, enum impr_flag_id flag)
Definition city.c:727
void city_refresh_from_main_map(struct city *pcity, bool *workers_map)
Definition city.c:3171
int city_production_unit_veteran_level(struct city *pcity, const struct unit_type *punittype)
Definition city.c:804
bool can_city_build_improvement_now(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:854
bool city_tile_index_to_xy(int *city_map_x, int *city_map_y, int city_tile_index, int city_radius_sq)
Definition city.c:101
bool city_rapture_grow(const struct city *pcity)
Definition city.c:1649
int city_production_turns_to_build(const struct city *pcity, bool include_shield_stock)
Definition city.c:820
bool city_unhappy(const struct city *pcity)
Definition city.c:1622
Specialist_type_id best_specialist(Output_type_id otype, const struct city *pcity)
Definition city.c:3330
struct city * city_list_find_number(struct city_list *This, int id)
Definition city.c:1675
bool city_celebrating(const struct city *pcity)
Definition city.c:1641
bool can_city_build_improvement_direct(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:830
int city_illness_calc(const struct city *pcity, int *ill_base, int *ill_size, int *ill_trade, int *ill_pollution)
Definition city.c:2867
bool city_can_grow_to(const struct city *pcity, int pop_size)
Definition city.c:2008
bool city_happy(const struct city *pcity)
Definition city.c:1610
int city_map_radius_sq_get(const struct city *pcity)
Definition city.c:137
static int cmp(int v1, int v2)
Definition city.c:325
int city_tile_output(const struct city *pcity, const struct tile *ptile, bool is_celebrating, Output_type_id otype)
Definition city.c:1279
int city_map_tiles(int city_radius_sq)
Definition city.c:171
bool can_city_build_unit_direct(const struct city *pcity, const struct unit_type *punittype)
Definition city.c:903
bool city_exist(int id)
Definition city.c:3568
bool city_can_work_tile(const struct city *pcity, const struct tile *ptile)
Definition city.c:1452
bool city_production_build_units(const struct city *pcity, bool add_production, int *num_units)
Definition city.c:747
void city_rally_point_clear(struct city *pcity)
Definition city.c:3622
bool can_city_build_improvement_later(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:871
bool city_had_recent_plague(const struct city *pcity)
Definition city.c:2920
bool city_can_change_build(const struct city *pcity)
Definition city.c:1075
int city_total_unit_gold_upkeep(const struct city *pcity)
Definition city.c:1218
int city_total_impr_gold_upkeep(const struct city *pcity)
Definition city.c:1197
bool can_city_build_unit_now(const struct city *pcity, const struct unit_type *punittype)
Definition city.c:948
#define cities_iterate_end
Definition city.h:517
#define city_list_iterate_safe(citylist, _city)
Definition city.h:522
@ CNA_BROADCAST_PENDING
Definition city.h:308
@ CNA_NOT
Definition city.h:306
@ CNA_NORMAL
Definition city.h:307
#define city_list_iterate(citylist, pcity)
Definition city.h:508
#define city_tile(_pcity_)
Definition city.h:564
#define cities_iterate(pcity)
Definition city.h:512
static citizens city_size_get(const struct city *pcity)
Definition city.h:569
#define city_tile_iterate_skip_free_worked(_nmap, _radius_sq, _city_tile, _tile, _index, _x, _y)
Definition city.h:214
@ CITIZEN_ANGRY
Definition city.h:271
@ CITIZEN_HAPPY
Definition city.h:268
@ CITIZEN_CONTENT
Definition city.h:269
@ CITIZEN_UNHAPPY
Definition city.h:270
#define output_type_iterate(output)
Definition city.h:836
#define INCITE_IMPOSSIBLE_COST
Definition city.h:96
#define city_owner(_pcity_)
Definition city.h:563
#define city_tile_iterate_skip_free_worked_end
Definition city.h:222
#define city_list_iterate_end
Definition city.h:510
#define city_tile_iterate(_nmap, _radius_sq, _city_tile, _tile)
Definition city.h:230
#define city_list_iterate_safe_end
Definition city.h:544
@ FEELING_FINAL
Definition city.h:284
#define city_tile_iterate_end
Definition city.h:238
#define city_built_iterate(_pcity, _p)
Definition city.h:825
#define city_built_iterate_end
Definition city.h:831
#define output_type_iterate_end
Definition city.h:842
void city_map_update_empty(struct city *pcity, struct tile *ptile)
Definition citytools.c:3252
void send_city_info(struct player *dest, struct city *pcity)
Definition citytools.c:2351
struct city * find_closest_city(const struct tile *ptile, const struct city *pexclcity, const struct player *pplayer, bool only_ocean, bool only_continent, bool only_known, bool only_player, bool only_enemy, const struct unit_class *pclass)
Definition citytools.c:851
void sync_cities(void)
Definition citytools.c:3326
static void void city_freeze_workers(struct city *pcity)
Definition citytools.c:136
bool city_map_update_radius_sq(struct city *pcity)
Definition citytools.c:3451
void change_build_target(struct player *pplayer, struct city *pcity, struct universal *target, enum event_type event)
Definition citytools.c:3163
bool city_illness_strike(struct city *pcity)
Definition citytools.c:2954
void city_thaw_workers(struct city *pcity)
Definition citytools.c:146
void building_lost(struct city *pcity, const struct impr_type *pimprove, const char *reason, struct unit *destroyer)
Definition citytools.c:3052
void city_units_upkeep(const struct city *pcity)
Definition citytools.c:3140
void remove_city(struct city *pcity)
Definition citytools.c:1702
void do_sell_building(struct player *pplayer, struct city *pcity, struct impr_type *pimprove, const char *reason)
Definition citytools.c:2980
struct trade_route * remove_trade_route(struct city *pc1, struct trade_route *proute, bool announce, bool source_gone)
Definition citytools.c:2918
void broadcast_city_info(struct city *pcity)
Definition citytools.c:2232
void city_add_improvement_with_gov_notice(struct city *pcity, const struct impr_type *pimprove, const char *format)
Definition citytools.c:3645
void city_map_update_all(struct city *pcity)
Definition citytools.c:3345
void transfer_city_units(struct player *pplayer, struct player *pvictim, struct unit_list *units, struct city *pcity, struct city *exclude_city, int kill_outside, bool verbose)
Definition citytools.c:716
void city_map_update_worker(struct city *pcity, struct tile *ptile)
Definition citytools.c:3266
void city_refresh_vision(struct city *pcity)
Definition citytools.c:3426
#define LOG_BUILD_TARGET
Definition citytools.h:21
static bool city_illness_check(const struct city *pcity)
Definition cityturn.c:3769
static void uk_rem_gold_append(struct unit *punit)
Definition cityturn.c:3084
static bool worklist_change_build_target(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2184
void remove_obsolete_buildings(struct player *pplayer)
Definition cityturn.c:269
static bool city_build_stuff(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2985
int city_shrink_granary_savings(const struct city *pcity)
Definition cityturn.c:891
static void apply_disaster(struct city *pcity, struct disaster_type *pdis)
Definition cityturn.c:4277
static bool city_distribute_surplus_shields(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2531
static void unit_list_referred_destroy(struct unit_list *punitlist)
Definition cityturn.c:3097
static bool place_pollution(struct city *pcity, enum extra_cause cause)
Definition cityturn.c:3382
void choose_build_target(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2398
static void uk_rem_gold_callback(struct unit *punit)
Definition cityturn.c:3063
void auto_arrange_workers(struct city *pcity)
Definition cityturn.c:365
void city_refresh_queue_add(struct city *pcity)
Definition cityturn.c:196
static struct unit_list * uk_rem_gold
Definition cityturn.c:93
static bool city_balance_treasury_buildings(struct city *pcity)
Definition cityturn.c:3300
static void update_city_activity(struct city *pcity)
Definition cityturn.c:3559
static void upgrade_unit_prod(struct city *pcity)
Definition cityturn.c:2508
static float city_migration_score(struct city *pcity)
Definition cityturn.c:3894
void nullify_prechange_production(struct city *pcity)
Definition cityturn.c:3550
bool city_empty_food_stock(struct city *pcity)
Definition cityturn.c:4255
bool check_city_migrations(void)
Definition cityturn.c:4223
static void upgrade_building_prod(struct city *pcity)
Definition cityturn.c:2462
bool player_balance_treasury_units_and_buildings(struct player *pplayer)
Definition cityturn.c:3203
bool city_change_size(struct city *pcity, citizens size, struct player *nationality, const char *reason)
Definition cityturn.c:1030
static citizens city_reduce_specialists(struct city *pcity, citizens change)
Definition cityturn.c:717
static void city_global_turn_notify(struct conn_list *dest)
Definition cityturn.c:489
static void city_populate(struct city *pcity, struct player *nationality)
Definition cityturn.c:1081
static void city_refresh_after_city_size_increase(struct city *pcity, struct player *nationality)
Definition cityturn.c:996
static void define_orig_production_values(struct city *pcity)
Definition cityturn.c:3519
void city_repair_size(struct city *pcity, int change)
Definition cityturn.c:850
#define cityimpr_list_iterate(cityimprlist, pcityimpr)
Definition cityturn.c:127
void city_counters_refresh(struct city *pcity)
Definition cityturn.c:4603
static bool upkeep_kill_unit(struct unit *punit, Output_type_id outp, enum unit_loss_reason wipe_reason, bool wipe_in_the_end)
Definition cityturn.c:685
static bool city_build_building(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2612
static struct unit static const struct impr_type * building_upgrades_to(struct city *pcity, const struct impr_type *pimprove)
Definition cityturn.c:2441
void send_city_turn_notifications(struct connection *pconn)
Definition cityturn.c:575
static struct unit * sell_random_unit(struct player *pplayer, struct unit_list *punitlist)
Definition cityturn.c:3120
int city_incite_cost(struct player *pplayer, struct city *pcity)
Definition cityturn.c:3438
static void nullify_caravan_and_disband_plus(struct city *pcity)
Definition cityturn.c:3540
static bool sell_random_building(struct player *pplayer, struct cityimpr_list *imprs)
Definition cityturn.c:3017
static bool disband_city(struct city *pcity)
Definition cityturn.c:3782
void update_city_activities(struct player *pplayer)
Definition cityturn.c:601
bool city_reduce_size(struct city *pcity, citizens pop_loss, struct player *destroyer, const char *reason)
Definition cityturn.c:761
#define cityimpr_list_iterate_end
Definition cityturn.c:129
bool city_refresh(struct city *pcity)
Definition cityturn.c:158
static void city_turn_notify(const struct city *pcity, struct conn_list *dest, const struct player *cache_for_player)
Definition cityturn.c:510
static bool city_increase_size(struct city *pcity)
Definition cityturn.c:919
void apply_cmresult_to_city(struct city *pcity, const struct cm_result *cmr)
Definition cityturn.c:280
bool player_balance_treasury_units(struct player *pplayer)
Definition cityturn.c:3266
void city_style_refresh(struct city *pcity)
Definition cityturn.c:4594
static bool city_balance_treasury_units(struct city *pcity)
Definition cityturn.c:3347
static const struct unit_type * unit_upgrades_to(struct city *pcity, const struct unit_type *id)
Definition cityturn.c:2487
static bool do_city_migration(struct city *pcity_from, struct city *pcity_to)
Definition cityturn.c:3978
static struct city_list * city_refresh_queue
Definition cityturn.c:87
void city_refresh_for_player(struct player *pplayer)
Definition cityturn.c:181
void city_tc_effect_refresh(struct player *pplayer)
Definition cityturn.c:4624
static void check_pollution(struct city *pcity)
Definition cityturn.c:3421
static void city_reset_foodbox(struct city *pcity, int new_size, bool shrink)
Definition cityturn.c:904
void check_disasters(void)
Definition cityturn.c:4398
static void set_default_city_manager(struct cm_parameter *cmp, struct city *pcity)
Definition cityturn.c:312
static citizens city_reduce_workers(struct city *pcity, citizens change)
Definition cityturn.c:737
static bool worklist_item_postpone_req_vec(struct universal *target, struct city *pcity, struct player *pplayer, int saved_id)
Definition cityturn.c:1158
static struct unit * city_create_unit(struct city *pcity, const struct unit_type *utype, struct citizens_reduction *red) fc__attribute((nonnull(1
Definition cityturn.c:2769
void city_refresh_queue_processing(void)
Definition cityturn.c:212
int city_growth_granary_savings(const struct city *pcity)
Definition cityturn.c:877
void remove_obsolete_buildings_city(struct city *pcity, bool refresh)
Definition cityturn.c:234
static bool check_city_migrations_player(const struct player *pplayer)
Definition cityturn.c:4435
static bool city_build_unit(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2830
void cm_clear_cache(struct city *pcity)
Definition cm.c:322
void cm_init_parameter(struct cm_parameter *dest)
Definition cm.c:2180
struct cm_result * cm_result_new(struct city *pcity)
Definition cm.c:345
void cm_result_destroy(struct cm_result *result)
Definition cm.c:368
void cm_print_result(const struct cm_result *result)
Definition cm.c:2466
void cm_query_result(struct city *pcity, const struct cm_parameter *param, struct cm_result *result, bool negative_ok)
Definition cm.c:2120
void cm_init_emergency_parameter(struct cm_parameter *dest)
Definition cm.c:2198
void cm_print_city(const struct city *pcity)
Definition cm.c:2428
char * incite_cost
Definition comments.c:74
struct player * conn_get_player(const struct connection *pconn)
Definition connection.c:763
void conn_list_do_unbuffer(struct conn_list *dest)
Definition connection.c:366
void conn_list_do_buffer(struct conn_list *dest)
Definition connection.c:356
const char * counter_name_translation(const struct counter *counter)
Definition counters.c:157
int counters_get_city_counters_count(void)
Definition counters.c:74
#define city_counters_iterate_end
Definition counters.h:64
#define city_counters_iterate(pcount)
Definition counters.h:57
int city_culture(const struct city *pcity)
Definition culture.c:29
int city_history_gain(const struct city *pcity)
Definition culture.c:39
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:74
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit int cost
Definition dialogs_g.h:74
const char * disaster_rule_name(struct disaster_type *pdis)
Definition disaster.c:105
bool can_disaster_happen(const struct disaster_type *pdis, const struct city *pcity)
Definition disaster.c:139
const char * disaster_name_translation(struct disaster_type *pdis)
Definition disaster.c:97
bool disaster_has_effect(const struct disaster_type *pdis, enum disaster_effect_id effect)
Definition disaster.c:130
#define disaster_type_iterate(_p)
Definition disaster.h:82
#define DISASTER_BASE_RARITY
Definition disaster.h:46
#define disaster_type_iterate_end
Definition disaster.h:88
int get_city_bonus(const struct city *pcity, enum effect_type effect_type)
Definition effects.c:842
int get_current_construction_bonus(const struct city *pcity, enum effect_type effect_type, const enum req_problem_type prob_type)
Definition effects.c:1180
int get_city_tile_output_bonus(const struct city *pcity, const struct tile *ptile, const struct output_type *poutput, enum effect_type effect_type)
Definition effects.c:912
struct extra_type * rand_extra_for_tile(struct tile *ptile, enum extra_cause cause, bool generated)
Definition extras.c:283
const char * extra_name_translation(const struct extra_type *pextra)
Definition extras.c:194
#define MAX_CITY_NATIONALITIES
Definition fc_types.h:89
int Tech_type_id
Definition fc_types.h:381
unsigned char citizens
Definition fc_types.h:392
@ RPT_CERTAIN
Definition fc_types.h:678
@ RPT_POSSIBLE
Definition fc_types.h:677
#define MAX_LEN_NAME
Definition fc_types.h:66
#define CITY_MAP_MAX_RADIUS
Definition fc_types.h:83
@ O_SHIELD
Definition fc_types.h:101
@ O_FOOD
Definition fc_types.h:101
@ O_TRADE
Definition fc_types.h:101
@ O_SCIENCE
Definition fc_types.h:101
@ O_LUXURY
Definition fc_types.h:101
@ O_GOLD
Definition fc_types.h:101
enum output_type_id Output_type_id
Definition fc_types.h:382
#define Q_(String)
Definition fcintl.h:70
#define PL_(String1, String2, n)
Definition fcintl.h:71
#define _(String)
Definition fcintl.h:67
const char * city_tile_link(const struct city *pcity)
const struct ft_color ftc_server
const char * city_link(const struct city *pcity)
const char * unit_link(const struct unit *punit)
const char * unit_tile_link(const struct unit *punit)
#define MAX_LEN_LINK
struct civ_game game
Definition game.c:61
struct world wld
Definition game.c:62
struct city * game_city_by_number(int id)
Definition game.c:106
#define GAME_MAX_MGR_DISTANCE
Definition game.h:530
const char * government_name_translation(const struct government *pgovern)
Definition government.c:143
struct government * government_of_city(const struct city *pcity)
Definition government.c:123
Government_type_id government_number(const struct government *pgovern)
Definition government.c:91
GType type
Definition repodlgs.c:1313
void handle_player_change_government(struct player *pplayer, Government_type_id government)
Definition plrhand.c:565
const struct impr_type * valid_improvement(const struct impr_type *pimprove)
int impr_sell_gold(const struct impr_type *pimprove)
bool can_city_sell_building(const struct city *pcity, const struct impr_type *pimprove)
bool is_improvement(const struct impr_type *pimprove)
const char * improvement_rule_name(const struct impr_type *pimprove)
int impr_build_shield_cost(const struct city *pcity, const struct impr_type *pimprove)
bool is_wonder(const struct impr_type *pimprove)
bool is_great_wonder(const struct impr_type *pimprove)
bool improvement_obsolete(const struct player *pplayer, const struct impr_type *pimprove, const struct city *pcity)
bool improvement_has_flag(const struct impr_type *pimprove, enum impr_flag_id flag)
bool is_convert_improvement(const struct impr_type *pimprove)
const char * improvement_name_translation(const struct impr_type *pimprove)
bool is_small_wonder(const struct impr_type *pimprove)
const struct impr_type * improvement_replacement(const struct impr_type *pimprove)
#define B_LAST
Definition improvement.h:42
#define fc_assert_msg(condition, message,...)
Definition log.h:181
#define fc_assert_ret(condition)
Definition log.h:191
#define log_verbose(message,...)
Definition log.h:109
#define fc_assert(condition)
Definition log.h:176
#define fc_assert_ret_msg(condition, message,...)
Definition log.h:205
#define fc_assert_ret_val(condition, val)
Definition log.h:194
#define fc_assert_action(condition, action)
Definition log.h:187
#define log_debug(message,...)
Definition log.h:115
#define log_normal(message,...)
Definition log.h:107
#define log_base(level, message,...)
Definition log.h:94
@ LOG_DEBUG
Definition log.h:34
#define log_error(message,...)
Definition log.h:103
#define fc_assert_ret_val_msg(condition, val, message,...)
Definition log.h:208
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:636
int map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:660
#define iterate_outward(nmap, start_tile, max_dist, itr_tile)
Definition map.h:353
#define iterate_outward_end
Definition map.h:357
void map_update_border(struct tile *ptile, struct player *owner, int old_radius_sq, int new_radius_sq)
Definition maphand.c:2259
void map_claim_border(struct tile *ptile, struct player *owner, int radius_sq)
Definition maphand.c:2291
void update_tile_knowledge(struct tile *ptile)
Definition maphand.c:1446
#define FC_FREE(ptr)
Definition mem.h:41
#define fc_malloc(sz)
Definition mem.h:34
const char * nation_rule_name(const struct nation_type *pnation)
Definition nation.c:138
const char * nation_adjective_for_player(const struct player *pplayer)
Definition nation.c:169
const char * nation_adjective_translation(const struct nation_type *pnation)
Definition nation.c:149
struct nation_type * nation_of_city(const struct city *pcity)
Definition nation.c:454
const char * nation_group_name_translation(const struct nation_group *pgroup)
Definition nation.c:1090
const char * nation_plural_translation(const struct nation_type *pnation)
Definition nation.c:159
const char * nation_plural_for_player(const struct player *pplayer)
Definition nation.c:178
void notify_research(const struct research *presearch, const struct player *exclude, enum event_type event, const struct ft_color color, const char *format,...)
Definition notify.c:394
void notify_player(const struct player *pplayer, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
Definition notify.c:292
void notify_research_embassies(const struct research *presearch, const struct player *exclude, enum event_type event, const struct ft_color color, const char *format,...)
Definition notify.c:434
void notify_conn(struct conn_list *dest, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
Definition notify.c:239
void package_event(struct packet_chat_msg *packet, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
Definition notify.c:168
void event_cache_add_for_player(const struct packet_chat_msg *packet, const struct player *pplayer)
Definition notify.c:646
void lsend_packet_city_update_counters(struct conn_list *dest, const struct packet_city_update_counters *packet)
void lsend_packet_chat_msg(struct conn_list *dest, const struct packet_chat_msg *packet)
struct city_list * cities
Definition packhand.c:119
int player_number(const struct player *pplayer)
Definition player.c:837
const char * player_name(const struct player *pplayer)
Definition player.c:895
struct city * player_city_by_number(const struct player *pplayer, int city_id)
Definition player.c:1203
const char * diplrel_name_translation(int value)
Definition player.c:1631
struct player * player_slot_get_player(const struct player_slot *pslot)
Definition player.c:437
#define players_iterate_end
Definition player.h:539
#define players_iterate(_pplayer)
Definition player.h:534
static bool is_barbarian(const struct player *pplayer)
Definition player.h:491
#define is_ai(plr)
Definition player.h:232
void send_player_info_c(struct player *src, struct conn_list *dest)
Definition plrhand.c:1146
#define fc_rand(_size)
Definition rand.h:56
const char * universal_rule_name(const struct universal *psource)
bool is_req_active(const struct req_context *context, const struct req_context *other_context, const struct requirement *req, const enum req_problem_type prob_type)
const char * universal_name_translation(const struct universal *psource, char *buf, size_t bufsz)
#define requirement_vector_iterate_end
#define requirement_vector_iterate(req_vec, preq)
const char * research_advance_name_translation(const struct research *presearch, Tech_type_id tech)
Definition research.c:273
struct research * research_get(const struct player *pplayer)
Definition research.c:128
enum tech_state research_invention_state(const struct research *presearch, Tech_type_id tech)
Definition research.c:619
int research_pretty_name(const struct research *presearch, char *buf, size_t buf_len)
Definition research.c:169
#define sanity_check_city(x)
Definition sanitycheck.h:41
void script_server_signal_emit(const char *signal_name,...)
const char * ssetv_human_readable(ssetv val, bool present)
static struct setting settings[]
Definition settings.c:1474
#define CLIP(lower, current, upper)
Definition shared.h:57
#define MIN(x, y)
Definition shared.h:55
#define FC_INFINITY
Definition shared.h:36
#define MAX(x, y)
Definition shared.h:54
void send_spaceship_info(struct player *src, struct conn_list *dest)
Definition spacerace.c:129
@ SSHIP_STARTED
Definition spaceship.h:84
@ SSHIP_NONE
Definition spaceship.h:84
#define specialist_type_iterate_end
Definition specialist.h:79
#define specialist_type_iterate(sp)
Definition specialist.h:73
#define DEFAULT_SPECIALIST
Definition specialist.h:43
size_t size
Definition specvec.h:72
struct sprite int int y
Definition sprite_g.h:31
struct sprite int x
Definition sprite_g.h:31
#define CITY_LOG(loglevel, pcity, msg,...)
Definition srv_log.h:83
@ AIT_CITIZEN_ARRANGE
Definition srv_log.h:50
@ TIMER_STOP
Definition srv_log.h:76
@ TIMER_START
Definition srv_log.h:76
#define TIMING_LOG(timer, activity)
Definition srv_log.h:125
Definition city.h:320
int turn_last_built
Definition city.h:387
int surplus[O_LAST]
Definition city.h:355
enum city_wl_cancel_behavior wlcb
Definition city.h:404
int food_stock
Definition city.h:367
struct player * original
Definition city.h:324
int history
Definition city.h:410
int * counter_values
Definition city.h:408
int pollution
Definition city.h:369
bool did_sell
Definition city.h:380
int id
Definition city.h:326
int last_turns_shield_surplus
Definition city.h:392
int disbanded_shields
Definition city.h:391
int workers_frozen
Definition city.h:437
bool was_happy
Definition city.h:381
struct player * owner
Definition city.h:323
int airlift
Definition city.h:378
int caravan_shields
Definition city.h:390
bool did_buy
Definition city.h:379
int anarchy
Definition city.h:384
enum city_needs_arrange needs_arrange
Definition city.h:441
struct worklist worklist
Definition city.h:401
struct universal production
Definition city.h:396
struct unit_order * orders
Definition city.h:422
struct city::@16 rally_point
citizens * nationality
Definition city.h:341
bool vigilant
Definition city.h:421
citizens size
Definition city.h:332
int illness
Definition city.h:434
int before_change_shields
Definition city.h:389
int style
Definition city.h:327
bool had_famine
Definition city.h:382
size_t length
Definition city.h:417
float migration_score
Definition city.h:431
bool needs_refresh
Definition city.h:445
citizens feel[CITIZEN_LAST][FEELING_LAST]
Definition city.h:333
citizens specialists[SP_MAX]
Definition city.h:336
struct tile * tile
Definition city.h:322
int shield_stock
Definition city.h:368
int prod[O_LAST]
Definition city.h:358
struct city::@17::@19 server
struct cm_parameter * cm_parameter
Definition city.h:425
bool debug
Definition city.h:450
struct universal changed_from
Definition city.h:399
struct unit_list * units_supported
Definition city.h:406
int mgr_score_calc_turn
Definition city.h:432
int illness_trade
Definition city.h:370
bool persistent
Definition city.h:419
int rapture
Definition city.h:385
struct city * pcity
Definition cityturn.c:119
struct impr_type * pimprove
Definition cityturn.c:120
int mgr_worldchance
Definition game.h:162
int incite_total_factor
Definition game.h:150
int mgr_nationchance
Definition game.h:160
struct conn_list * glob_observers
Definition game.h:98
bool mgr_foodneeded
Definition game.h:159
int base_incite_cost
Definition game.h:134
struct conn_list * est_connections
Definition game.h:97
struct packet_game_info info
Definition game.h:89
int mgr_turninterval
Definition game.h:161
bool migration
Definition game.h:164
int incite_improvement_factor
Definition game.h:149
int aqueductloss
Definition game.h:129
struct civ_game::@31::@35 server
int incite_unit_factor
Definition game.h:151
int mgr_distance
Definition game.h:158
Definition cm.h:52
struct requirement_vector reqs
Definition improvement.h:58
int counters[MAX_COUNTERS]
enum gold_upkeep_style gold_upkeep_style
bool unit_builders_nationality
int infra_points
Definition player.h:67
int units_built
Definition player.h:104
enum spaceship_state state
Definition spaceship.h:108
struct city_list * cities
Definition player.h:281
struct unit_list * units
Definition player.h:282
struct conn_list * connections
Definition player.h:298
struct player_economic economic
Definition player.h:284
struct player_spaceship spaceship
Definition player.h:286
struct player_score score
Definition player.h:283
struct player_slot * slot
Definition player.h:250
const struct player * player
Definition tile.h:50
struct unit_list * units
Definition tile.h:58
struct requirement_vector build_reqs
Definition unittype.h:527
Definition unit.h:140
int length
Definition unit.h:198
int upkeep[O_LAST]
Definition unit.h:150
bool has_orders
Definition unit.h:196
struct unit::@80 orders
int id
Definition unit.h:147
bool vigilant
Definition unit.h:200
struct unit::@81::@84 server
struct unit_order * list
Definition unit.h:201
struct player * nationality
Definition unit.h:146
int upkeep_paid[O_LAST]
Definition unit.h:259
const struct unit_type * utype
Definition unit.h:141
enum universals_n kind
Definition fc_types.h:880
universals_u value
Definition fc_types.h:879
struct civ_map map
int city_style(struct city *pcity)
Definition style.c:241
const char * style_name_translation(const struct nation_style *pstyle)
Definition style.c:99
#define sz_strlcpy(dest, src)
Definition support.h:189
#define fc__attribute(x)
Definition support.h:99
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
#define fc__fallthrough
Definition support.h:119
const char * advance_name_translation(const struct advance *padvance)
Definition tech.c:300
Tech_type_id advance_number(const struct advance *padvance)
Definition tech.c:98
Tech_type_id pick_free_tech(struct research *presearch)
Definition techtools.c:1387
void give_immediate_free_tech(struct research *presearch, Tech_type_id tech)
Definition techtools.c:1406
void update_bulbs(struct player *pplayer, int bulbs, bool check_tech, bool free_bulbs)
Definition techtools.c:654
const char * terrain_name_translation(const struct terrain *pterrain)
Definition terrain.c:238
void tile_add_extra(struct tile *ptile, const struct extra_type *pextra)
Definition tile.c:955
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
#define tile_worked(_tile)
Definition tile.h:115
#define tile_has_extra(ptile, pextra)
Definition tile.h:148
bool can_cities_trade(const struct city *pc1, const struct city *pc2)
enum trade_route_type cities_trade_route_type(const struct city *pcity1, const struct city *pcity2)
Definition traderoutes.c:58
bool goods_can_be_provided(const struct city *pcity, const struct goods_type *pgood, const struct unit *punit)
bool goods_has_flag(const struct goods_type *pgood, enum goods_flag_id flag)
struct trade_route_settings * trade_route_settings_by_type(enum trade_route_type type)
const char * goods_name_translation(struct goods_type *pgood)
#define trade_routes_iterate_safe_end
@ TRI_CANCEL
Definition traderoutes.h:32
#define trade_routes_iterate_safe(c, proute)
#define trade_partners_iterate_end
#define trade_partners_iterate(c, p)
trade_route_type
Definition traderoutes.h:37
const struct unit_type * utype
Definition fc_types.h:698
const struct impr_type * building
Definition fc_types.h:691
bool unit_is_alive(int id)
Definition unit.c:2291
#define unit_tile(_pu)
Definition unit.h:397
#define unit_cargo_iterate_end
Definition unit.h:575
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition unit.h:572
#define unit_owner(_pu)
Definition unit.h:396
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_safe(unitlist, _unit)
Definition unitlist.h:39
#define unit_list_iterate_end
Definition unitlist.h:33
#define unit_list_iterate_safe_end
Definition unitlist.h:61
void unit_set_removal_callback(struct unit *punit, void(*callback)(struct unit *punit))
Definition unittools.c:1772
bool place_unit(struct unit *punit, struct player *pplayer, struct city *pcity, struct unit *ptrans, bool force)
Definition unittools.c:1714
struct unit * unit_virtual_prepare(struct player *pplayer, struct tile *ptile, const struct unit_type *type, int veteran_level, int homecity_id, int moves_left, int hp_left)
Definition unittools.c:1666
void unit_unset_removal_callback(struct unit *punit)
Definition unittools.c:1786
void wipe_unit(struct unit *punit, enum unit_loss_reason reason, struct player *killer)
Definition unittools.c:2140
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
int unit_build_shield_cost(const struct city *pcity, const struct unit *punit)
Definition unittype.c:1476
int utype_upkeep_cost(const struct unit_type *ut, struct player *pplayer, Output_type_id otype)
Definition unittype.c:132
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1587
const char * utype_rule_name(const struct unit_type *punittype)
Definition unittype.c:1578
int utype_pop_value(const struct unit_type *punittype, const struct city *pcity)
Definition unittype.c:1528
int utype_build_shield_cost(const struct city *pcity, const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1438
const char * utype_name_translation(const struct unit_type *punittype)
Definition unittype.c:1560
#define unit_tech_reqs_iterate_end
Definition unittype.h:885
#define unit_tech_reqs_iterate(_utype_, _p)
Definition unittype.h:879
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:624
#define U_NOT_OBSOLETED
Definition unittype.h:535
bool worklist_peek_ith(const struct worklist *pwl, struct universal *prod, int idx)
Definition worklist.c:86
bool worklist_is_empty(const struct worklist *pwl)
Definition worklist.c:66
void worklist_remove(struct worklist *pwl, int idx)
Definition worklist.c:122
int worklist_length(const struct worklist *pwl)
Definition worklist.c:57