Freeciv-3.2
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#include "autosettlers.h"
83
84/* server/scripting */
85#include "script_server.h"
86
87/* Queue for pending city_refresh() */
88static struct city_list *city_refresh_queue = NULL;
89
90/* The game is currently considering to remove the listed units because of
91 * missing gold upkeep. A unit ends up here if it has gold upkeep that
92 * can't be paid. A random unit in the list will be removed until the
93 * problem is solved. */
94static struct unit_list *uk_rem_gold = NULL;
95
96static void check_pollution(struct city *pcity);
97static void city_populate(struct city *pcity, struct player *nationality);
98
99static bool worklist_change_build_target(struct player *pplayer,
100 struct city *pcity);
101
102static bool city_distribute_surplus_shields(struct player *pplayer,
103 struct city *pcity);
104static bool city_build_building(struct player *pplayer, struct city *pcity);
105static bool city_build_unit(struct player *pplayer, struct city *pcity);
106static bool city_build_stuff(struct player *pplayer, struct city *pcity);
107static struct unit *city_create_unit(struct city *pcity,
108 const struct unit_type *utype,
109 struct citizens_reduction *red)
110 fc__attribute((nonnull (1, 2)));
111static const struct impr_type *building_upgrades_to(struct city *pcity,
112 const struct impr_type *pimprove);
113static void upgrade_building_prod(struct city *pcity);
114static const struct unit_type *unit_upgrades_to(struct city *pcity,
115 const struct unit_type *id);
116static void upgrade_unit_prod(struct city *pcity);
117
118/* Helper struct for associating a building to a city. */
119struct cityimpr {
120 struct city *pcity;
122};
123
124#define SPECLIST_TAG cityimpr
125#define SPECLIST_TYPE struct cityimpr
126#include "speclist.h"
127
128#define cityimpr_list_iterate(cityimprlist, pcityimpr) \
129 TYPED_LIST_ITERATE(struct cityimpr, cityimprlist, pcityimpr)
130#define cityimpr_list_iterate_end LIST_ITERATE_END
131
132static bool sell_random_building(struct player *pplayer,
133 struct cityimpr_list *imprs);
134static struct unit *sell_random_unit(struct player *pplayer,
135 struct unit_list *punitlist);
136
137static citizens city_reduce_specialists(struct city *pcity, citizens change);
138static citizens city_reduce_workers(struct city *pcity, citizens change);
139
140static bool city_balance_treasury_buildings(struct city *pcity);
141static bool city_balance_treasury_units(struct city *pcity);
142
143static bool disband_city(struct city *pcity);
144
145static void define_orig_production_values(struct city *pcity);
146static void update_city_activity(struct city *pcity);
147static void nullify_caravan_and_disband_plus(struct city *pcity);
148static bool city_illness_check(const struct city * pcity);
149
150static float city_migration_score(struct city *pcity);
151static bool do_city_migration(struct city *pcity_from,
152 struct city *pcity_to);
153static bool check_city_migrations_player(const struct player *pplayer);
154
155/**********************************************************************/
159bool city_refresh(struct city *pcity)
160{
161 bool retval;
162 const struct civ_map *nmap = &(wld.map);
163
164 pcity->server.needs_refresh = FALSE;
165
167 city_units_upkeep(pcity); /* Update unit upkeep */
169 city_style_refresh(pcity);
170
171 if (retval) {
172 /* Force a sync of the city after the change. */
173 send_city_info(city_owner(pcity), pcity);
174 }
175
176 return retval;
177}
178
179/**********************************************************************/
183void city_refresh_for_player(struct player *pplayer)
184{
186 city_list_iterate(pplayer->cities, pcity) {
187 if (city_refresh(pcity)) {
189 }
190 send_city_info(pplayer, pcity);
193}
194
195/**********************************************************************/
198void city_refresh_queue_add(struct city *pcity)
199{
200 if (NULL == city_refresh_queue) {
202 } else if (city_list_find_number(city_refresh_queue, pcity->id)) {
203 return;
204 }
205
207 pcity->server.needs_refresh = TRUE;
208}
209
210/**********************************************************************/
215{
216 if (NULL == city_refresh_queue) {
217 return;
218 }
219
221 if (pcity->server.needs_refresh) {
222 if (city_refresh(pcity)) {
224 }
225 send_city_info(city_owner(pcity), pcity);
226 }
228
231}
232
233/**********************************************************************/
236void remove_obsolete_buildings_city(struct city *pcity, bool refresh)
237{
238 struct player *pplayer = city_owner(pcity);
239 bool sold = FALSE;
240
241 city_built_iterate(pcity, pimprove) {
242 if (improvement_obsolete(pplayer, pimprove, pcity)
243 && can_city_sell_building(pcity, pimprove)) {
244 int sgold;
245
246 do_sell_building(pplayer, pcity, pimprove, "obsolete");
247 sgold = impr_sell_gold(pimprove);
249 PL_("%s is selling %s (obsolete) for %d.",
250 "%s is selling %s (obsolete) for %d.",
251 sgold),
252 city_link(pcity),
254 sgold);
255 sold = TRUE;
256 }
258
259 if (sold && refresh) {
260 if (city_refresh(pcity)) {
262 }
263 send_city_info(pplayer, pcity);
264 send_player_info_c(pplayer, NULL); /* Send updated gold to all */
265 }
266}
267
268/**********************************************************************/
272{
273 city_list_iterate(pplayer->cities, pcity) {
276}
277
278/**********************************************************************/
282void apply_cmresult_to_city(struct city *pcity,
283 const struct cm_result *cmr)
284{
285 struct tile *pcenter = city_tile(pcity);
286 const struct civ_map *nmap = &(wld.map);
287
288 /* Now apply results */
290 ptile, idx, x, y) {
291 struct city *pwork = tile_worked(ptile);
292
293 if (cmr->worker_positions[idx]) {
294 if (NULL == pwork) {
295 city_map_update_worker(pcity, ptile);
296 } else {
297 fc_assert(pwork == pcity);
298 }
299 } else {
300 if (pwork == pcity) {
301 city_map_update_empty(pcity, ptile);
302 }
303 }
305
307 pcity->specialists[sp] = cmr->specialists[sp];
309}
310
311/**********************************************************************/
315 struct city *pcity)
316{
317 int csize = city_size_get(pcity);
319
320 cmp->require_happy = FALSE;
321 cmp->allow_disorder = FALSE;
322 cmp->allow_specialists = TRUE;
323
324 /* We used to look at pplayer->ai.xxx_priority to determine the values
325 * to be used here. However that doesn't work at all because those values
326 * are on a different scale. Later the AI may wish to adjust its
327 * priorities - this should be done via a separate set of variables. */
328 if (csize > 1) {
329 if (csize <= game.info.notradesize) {
330 cmp->factor[O_FOOD] = 15;
331 } else {
332 if (gsize == pcity->food_stock) {
333 /* We don't need more food if the granary is full. */
334 cmp->factor[O_FOOD] = 0;
335 } else {
336 cmp->factor[O_FOOD] = 10;
337 }
338 }
339 } else {
340 /* Growing to size 2 is the highest priority. */
341 cmp->factor[O_FOOD] = 20;
342 }
343
344 cmp->factor[O_SHIELD] = 5;
345 cmp->factor[O_TRADE] = 0; /* Trade only provides gold/science. */
346 cmp->factor[O_GOLD] = 2;
347 cmp->factor[O_LUXURY] = 0; /* Luxury only influences happiness. */
348 cmp->factor[O_SCIENCE] = 2;
349 cmp->happy_factor = 0;
350
351 if (gsize == pcity->food_stock) {
352 cmp->minimal_surplus[O_FOOD] = 0;
353 } else {
354 cmp->minimal_surplus[O_FOOD] = 1;
355 }
356
357 cmp->minimal_surplus[O_SHIELD] = 1;
358 cmp->minimal_surplus[O_TRADE] = 0;
359 cmp->minimal_surplus[O_GOLD] = -FC_INFINITY;
360 cmp->minimal_surplus[O_LUXURY] = 0;
361 cmp->minimal_surplus[O_SCIENCE] = 0;
362}
363
364/**********************************************************************/
367void auto_arrange_workers(struct city *pcity)
368{
369 struct cm_parameter cmp;
370 struct cm_parameter *pcmp;
371 struct cm_result *cmr;
372 bool broadcast_needed;
373
374 /* See comment in freeze_workers(): we can't rearrange while
375 * workers are frozen (i.e. multiple updates need to be done). */
376 if (pcity->server.workers_frozen > 0) {
377 if (pcity->server.needs_arrange == CNA_NOT) {
379 }
380 return;
381 }
383
385
386 /* Freeze the workers and make sure all the tiles around the city
387 * are up to date. Then thaw, but hackishly make sure that thaw
388 * doesn't call us recursively, which would waste time. */
389 city_freeze_workers(pcity);
391
392 city_map_update_all(pcity);
393
395 city_thaw_workers(pcity);
396
397 /* Now start actually rearranging. */
398 city_refresh(pcity);
399
400 sanity_check_city(pcity);
401 cm_clear_cache(pcity);
402
403 if (pcity->cm_parameter) {
404 pcmp = pcity->cm_parameter;
405 } else {
406 pcmp = &cmp;
409 }
410
411 /* This must be after city_refresh() so that the result gets created for the right
412 * city radius */
413 cmr = cm_result_new(pcity);
414 cm_query_result(pcity, pcmp, cmr, FALSE);
415
416 if (!cmr->found_a_valid) {
417 if (pcity->cm_parameter) {
418 /* If player-defined parameters fail, cancel and notify player. */
419 free(pcity->cm_parameter);
420 pcity->cm_parameter = NULL;
421
422 notify_player(city_owner(pcity), city_tile(pcity),
424 _("The citizen governor can't fulfill the requirements "
425 "for %s. Passing back control."),
426 city_link(pcity));
427
428 /* Switch to default parameters, and try with them */
429 pcmp = &cmp;
432 cm_query_result(pcity, pcmp, cmr, FALSE);
433 }
434
435 if (!cmr->found_a_valid) {
436 /* Drop surpluses and try again. */
438 cmp.minimal_surplus[o] = 0;
440 cmp.minimal_surplus[O_GOLD] = -FC_INFINITY;
441 cm_query_result(pcity, pcmp, cmr, FALSE);
442 }
443 }
444 if (!cmr->found_a_valid) {
445 /* Emergency management. Get _some_ result. This doesn't use
446 * cm_init_emergency_parameter() so we can keep the factors from
447 * above. */
449 cmp.minimal_surplus[o] = MIN(cmp.minimal_surplus[o],
450 MIN(pcity->surplus[o], 0));
452 cmp.require_happy = FALSE;
453 cmp.allow_disorder = is_ai(city_owner(pcity)) ? FALSE : TRUE;
454 cm_query_result(pcity, pcmp, cmr, FALSE);
455 }
456 if (!cmr->found_a_valid) {
457 CITY_LOG(LOG_DEBUG, pcity, "emergency management");
458 pcmp = &cmp;
460 cm_query_result(pcity, pcmp, cmr, TRUE);
461 }
462 fc_assert_ret(cmr->found_a_valid);
463
465
466 if (pcity->server.debug) {
467 /* Print debug output if requested. */
468 cm_print_city(pcity);
470 }
471
472 if (city_refresh(pcity)) {
473 log_error("%s radius changed when already arranged workers.",
474 city_name_get(pcity));
475 /* Can't do anything - don't want to enter infinite recursive loop
476 * by trying to arrange workers more. */
477 }
478 sanity_check_city(pcity);
479
480 if (broadcast_needed) {
481 broadcast_city_info(pcity);
482 }
483
486}
487
488/**********************************************************************/
491static void city_global_turn_notify(struct conn_list *dest)
492{
493 cities_iterate(pcity) {
494 const struct impr_type *pimprove = pcity->production.value.building;
495
496 if (VUT_IMPROVEMENT == pcity->production.kind
497 && is_great_wonder(pimprove)
498 && (1 >= city_production_turns_to_build(pcity, TRUE)
499 && can_city_build_improvement_now(pcity, pimprove))) {
500 notify_conn(dest, city_tile(pcity),
502 _("Notice: Wonder %s in %s will be finished next turn."),
503 improvement_name_translation(pimprove), city_link(pcity));
504 }
506}
507
508/**********************************************************************/
512static void city_turn_notify(const struct city *pcity,
513 struct conn_list *dest,
514 const struct player *cache_for_player)
515{
516 const struct impr_type *pimprove = pcity->production.value.building;
517 struct packet_chat_msg packet;
519
520 if (0 < pcity->surplus[O_FOOD]) {
522 - pcity->food_stock - 1) / pcity->surplus[O_FOOD];
523
524 if (get_city_bonus(pcity, EFT_GROWTH_FOOD) <= 0
527 && 0 < pcity->surplus[O_SHIELD]) {
528 /* From the check above, the surplus must always be positive. */
529 turns_granary = (impr_build_shield_cost(pcity, pimprove)
530 - pcity->shield_stock) / pcity->surplus[O_SHIELD];
531 /* If growth and granary completion occur simultaneously, granary
532 * preserves food. -AJS. */
533 if (5 > turns_growth && 5 > turns_granary
535 package_event(&packet, city_tile(pcity),
537 _("Suggest throttling growth in %s to use %s "
538 "(being built) more effectively."),
539 city_link(pcity),
541 lsend_packet_chat_msg(dest, &packet);
542 if (NULL != cache_for_player) {
544 }
545 }
546 }
547
548 if (0 >= turns_growth && !city_celebrating(pcity)
549 && city_can_grow_to(pcity, city_size_get(pcity) + 1)) {
550 package_event(&packet, city_tile(pcity),
552 _("%s may soon grow to size %i."),
553 city_link(pcity), city_size_get(pcity) + 1);
554 lsend_packet_chat_msg(dest, &packet);
555 if (NULL != cache_for_player) {
557 }
558 }
559 } else {
560 if (0 >= pcity->food_stock + pcity->surplus[O_FOOD]
561 && 0 > pcity->surplus[O_FOOD]) {
562 package_event(&packet, city_tile(pcity),
564 _("Warning: Famine feared in %s."), city_link(pcity));
565 lsend_packet_chat_msg(dest, &packet);
566 if (NULL != cache_for_player) {
568 }
569 }
570 }
571}
572
573/**********************************************************************/
578{
579 if (NULL != pconn) {
580 struct player *pplayer = conn_get_player(pconn);
581
582 if (NULL != pplayer) {
583 city_list_iterate(pplayer->cities, pcity) {
584 city_turn_notify(pcity, pconn->self, NULL);
586 }
588 } else {
589 players_iterate(pplayer) {
590 city_list_iterate(pplayer->cities, pcity) {
591 city_turn_notify(pcity, pplayer->connections, pplayer);
594 /* NB: notifications to 'game.est_connections' are automatically
595 * cached. */
597 }
598}
599
600/**********************************************************************/
603void update_city_activities(struct player *pplayer)
604{
605 int n;
606
607 fc_assert(NULL != pplayer->cities);
608
609 n = city_list_size(pplayer->cities);
610
611 if (n > 0) {
612 struct city *cities[n];
613 int i = 0, r;
614
615 city_list_iterate(pplayer->cities, pcity) {
616
617 citizens_convert(pcity);
618
619 /* Cancel trade routes that cannot exist any more */
621 struct city *tcity = game_city_by_number(proute->partner);
622
623 if (tcity != NULL) {
624 bool cancel = FALSE;
625
626 if (proute->dir != RDIR_FROM && goods_has_flag(proute->goods, GF_DEPLETES)
627 && !goods_can_be_provided(tcity, proute->goods, NULL)) {
628 cancel = TRUE;
629 }
630 if (!cancel && !can_cities_trade(pcity, tcity)) {
633
634 if (settings->cancelling == TRI_CANCEL) {
635 cancel = TRUE;
636 }
637 }
638
639 if (cancel) {
640 struct trade_route *back;
641
643 free(proute);
644 free(back);
645 }
646 }
648
649 /* Add cities to array for later random order handling */
650 cities[i++] = pcity;
652
653 /* How gold upkeep is handled depends on the setting
654 * 'game.info.gold_upkeep_style':
655 * GOLD_UPKEEP_CITY: Each city tries to balance its upkeep individually
656 * (this is done in update_city_activity()).
657 * GOLD_UPKEEP_MIXED: Each city tries to balance its upkeep for
658 * buildings individually; the upkeep for units is
659 * paid by the nation.
660 * GOLD_UPKEEP_NATION: The nation as a whole balances the treasury. If
661 * the treasury is not balance units and buildings
662 * are sold. */
663
664 /* Iterate over cities in a random order. */
665 while (i > 0) {
666 r = fc_rand(i);
667 /* update unit upkeep */
670 cities[r] = cities[--i];
671 }
672 }
673}
674
675/**********************************************************************/
689 bool wipe_in_the_end)
690{
691 int punit_id;
692
695 /* Can't get rid of this unit. It is undisbandable for the current
696 * situation. */
697 return FALSE;
698 }
699
700 punit_id = punit->id;
701
702 /* Try to perform this unit's can't upkeep actions. */
705 NULL, NULL, NULL, NULL);
706
708 /* No forced action was able to kill the unit. Finish the job. */
710 }
711
712 return !unit_is_alive(punit_id);
713}
714
715/**********************************************************************/
719static citizens city_reduce_specialists(struct city *pcity, citizens change)
720{
721 citizens want = change;
722
723 fc_assert_ret_val(0 < change, 0);
724
726 citizens fix = MIN(want, pcity->specialists[sp]);
727
728 pcity->specialists[sp] -= fix;
729 want -= fix;
731
732 return change - want;
733}
734
735/**********************************************************************/
739static citizens city_reduce_workers(struct city *pcity, citizens change)
740{
741 struct tile *pcenter = city_tile(pcity);
742 int want = change;
743 const struct civ_map *nmap = &(wld.map);
744
745 fc_assert_ret_val(0 < change, 0);
746
748 ptile, _index, _x, _y) {
749 if (0 < want && tile_worked(ptile) == pcity) {
750 city_map_update_empty(pcity, ptile);
751 want--;
752 }
754
755 return change - want;
756}
757
758/**********************************************************************/
764 struct player *destroyer, const char *reason)
765{
767 int old_radius_sq;
768
769 if (pop_loss == 0) {
770 return TRUE;
771 }
772
773 if (city_size_get(pcity) <= pop_loss) {
774 int id = pcity->id;
775
776 citizens_update(pcity, NULL); /* To avoid warnings during the script */
777 /* Won't refresh a doomed city, or should we? */
778 script_server_signal_emit("city_destroyed", pcity, pcity->owner,
779 destroyer);
780
781 if (city_exist(id)) {
782 remove_city(pcity);
783 }
784 return FALSE;
785 }
787 city_size_add(pcity, -pop_loss);
790
791 /* Cap the food stock at the new granary size. */
792 if (pcity->food_stock > city_granary_size(city_size_get(pcity))) {
794 }
795
796 /* First try to kill off the specialists */
798
799 if (loss_remain > 0) {
800 /* Take it out on workers */
801#ifndef FREECIV_NDEBUG
802 loss_remain -=
803#endif /* FREECIV_NDEBUG */
805 }
806
807 /* Update citizens. */
808 citizens_update(pcity, NULL);
809
810 /* Update number of people in each feelings category.
811 * This also updates the city radius if needed. */
812 city_refresh(pcity);
813
815
816 /* Send city data. */
817 sync_cities();
818
820 "city_reduce_size() has remaining"
821 "%d of %d for \"%s\"[%d]",
823 city_name_get(pcity), city_size_get(pcity));
824
825 /* Update cities that have trade routes with us */
827 if (city_refresh(pcity2)) {
828 /* This should never happen, but if it does, make sure not to
829 * leave workers outside city radius. */
831 }
833
834 sanity_check_city(pcity);
835
836 if (reason != NULL) {
837 int id = pcity->id;
838
839 script_server_signal_emit("city_size_change", pcity,
841
842 return city_exist(id);
843 }
844
845 return TRUE;
846}
847
848/**********************************************************************/
852void city_repair_size(struct city *pcity, int change)
853{
854 if (change > 0) {
855 pcity->specialists[DEFAULT_SPECIALIST] += change;
856 } else if (change < 0) {
857 int need = change + city_reduce_specialists(pcity, -change);
858
859 if (0 > need) {
860#ifndef FREECIV_NDEBUG
861 need +=
862#endif /* FREECIV_NDEBUG */
863 city_reduce_workers(pcity, -need);
864 }
865
866 fc_assert_msg(0 == need,
867 "city_repair_size() has remaining %d of %d for \"%s\"[%d]",
868 need, change, city_name_get(pcity), city_size_get(pcity));
869 }
870}
871
872/**********************************************************************/
879int city_growth_granary_savings(const struct city *pcity)
880{
882
883 return CLIP(0, savings, 100);
884}
885
886/**********************************************************************/
893int city_shrink_granary_savings(const struct city *pcity)
894{
896
897 return CLIP(0, savings, 100);
898}
899
900/**********************************************************************/
906static void city_shrink_reset_foodbox(struct city *pcity, int new_size)
907{
909
911}
912
913/**********************************************************************/
918static bool city_increase_size(struct city *pcity)
919{
920 int new_food;
922 bool have_square = FALSE;
923 bool rapture_grow = city_rapture_grow(pcity); /* Check before size increase! */
924 struct tile *pcenter = city_tile(pcity);
925 struct player *powner = city_owner(pcity);
926 const struct impr_type *pimprove = pcity->production.value.building;
927 const struct civ_map *nmap = &(wld.map);
928
929 if (!city_can_grow_to(pcity, city_size_get(pcity) + 1)) {
930 /* Need improvement */
934 _("%s needs %s (being built) to grow beyond size %d."),
935 city_link(pcity),
937 city_size_get(pcity));
938 } else {
940 _("%s needs an improvement to grow beyond size %d."),
941 city_link(pcity), city_size_get(pcity));
942 }
943 /* Granary can only hold so much */
945 * (100 * 100 - game.server.aqueductloss * (100 - savings_pct))
946 / (100 * 100));
947 pcity->food_stock = MIN(pcity->food_stock, new_food);
948
949 return FALSE;
950 }
951
952 city_size_add(pcity, 1);
953
954 /* Do not empty food stock if city is growing by celebrating */
955 if (rapture_grow) {
957 } else {
959 }
960 pcity->food_stock = MIN(pcity->food_stock, new_food);
961
962 /* If there is enough food, and the city is big enough,
963 * make new citizens into scientists or taxmen -- Massimo */
964
965 /* Ignore food if no square can be worked */
967 ptile, _index, _x, _y) {
968 if (tile_worked(ptile) != pcity /* Quick test */
969 && city_can_work_tile(pcity, ptile)) {
971 }
973
974 if ((pcity->surplus[O_FOOD] >= 2 || !have_square)
976 pcity->specialists[best_specialist(O_SCIENCE, pcity)]++;
977 } else if ((pcity->surplus[O_FOOD] >= 2 || !have_square)
979 pcity->specialists[best_specialist(O_GOLD, pcity)]++;
980 } else {
981 pcity->specialists[DEFAULT_SPECIALIST]++; /* or else city is !sane */
982 }
983
984 /* Deprecated signal. Connect your lua functions to "city_size_change" that's
985 * emitted from calling functions which know the 'reason' of the increase. */
986 script_server_signal_emit("city_growth", pcity,
987 (lua_Integer)city_size_get(pcity));
988
989 return TRUE;
990}
991
992/**********************************************************************/
996 struct player *nationality)
997{
998 struct player *powner = city_owner(pcity);
999
1000 /* Update citizens. */
1001 citizens_update(pcity, nationality);
1002
1003 /* Refresh the city data; this also checks the squared city radius. */
1004 city_refresh(pcity);
1005
1006 auto_arrange_workers(pcity);
1007
1008 /* Update cities that have trade routes with us */
1010 if (city_refresh(pcity2)) {
1011 /* This should never happen, but if it does, make sure not to
1012 * leave workers outside city radius. */
1014 }
1016
1018 _("%s grows to size %d."),
1019 city_link(pcity), city_size_get(pcity));
1020
1021 sanity_check_city(pcity);
1022
1023 sync_cities();
1024}
1025
1026/**********************************************************************/
1030 struct player *nationality, const char *reason)
1031{
1032 int change = size - city_size_get(pcity);
1033
1034 if (change > 0) {
1035 int old_size = city_size_get(pcity);
1036 int real_change;
1037 int current_size = city_size_get(pcity);
1038 int id = pcity->id;
1039
1040 /* Increase city size until size reached, or increase fails */
1041 while (size > current_size && city_increase_size(pcity)) {
1042 /* TODO: This is currently needed only because there's
1043 * deprecated script signal "city_growth" emitted.
1044 * Check the need after signal has been dropped completely. */
1045 if (!city_exist(id)) {
1046 return FALSE;
1047 }
1048
1049 current_size++;
1050 }
1051
1052 city_refresh_after_city_size_increase(pcity, nationality);
1053
1055
1056 if (real_change != 0 && reason != NULL) {
1057 script_server_signal_emit("city_size_change", pcity,
1059
1060 if (!city_exist(id)) {
1061 return FALSE;
1062 }
1063 }
1064 } else if (change < 0) {
1065 /* We assume that city_change_size() is never called because
1066 * of enemy actions. If that changes, enemy must be passed
1067 * to city_reduce_size() */
1068 return city_reduce_size(pcity, -change, NULL, reason);
1069 }
1070
1071 map_claim_border(pcity->tile, pcity->owner, -1);
1072
1073 return TRUE;
1074}
1075
1076/**********************************************************************/
1080static void city_populate(struct city *pcity, struct player *nationality)
1081{
1082 int saved_id = pcity->id;
1083 int granary_size = city_granary_size(city_size_get(pcity));
1084
1085 pcity->food_stock += pcity->surplus[O_FOOD];
1086 if (pcity->food_stock >= granary_size || city_rapture_grow(pcity)) {
1087 if (city_had_recent_plague(pcity)) {
1088 notify_player(city_owner(pcity), city_tile(pcity),
1090 _("A recent plague outbreak prevents growth in %s."),
1091 city_link(pcity));
1092 /* Lose excess food */
1093 pcity->food_stock = MIN(pcity->food_stock, granary_size);
1094 } else {
1095 bool success;
1096
1097 success = city_increase_size(pcity);
1098 map_claim_border(pcity->tile, pcity->owner, -1);
1099
1100 if (success) {
1101 city_refresh_after_city_size_increase(pcity, nationality);
1102 script_server_signal_emit("city_size_change", pcity,
1103 (lua_Integer)1, "growth");
1104 }
1105 }
1106 } else if (pcity->food_stock < 0) {
1107 /* FIXME: should this depend on units with ability to build
1108 * cities or on units that require food in upkeep?
1109 * I'll assume citybuilders (units that 'contain' 1 pop) -- sjolie
1110 * The above may make more logical sense, but in game terms
1111 * you want to disband a unit that is draining your food
1112 * reserves. Hence, I'll assume food upkeep > 0 units. -- jjm
1113 */
1115 if (punit->upkeep[O_FOOD] > 0) {
1116 const char *punit_link = unit_tile_link(punit);
1117
1120 notify_player(city_owner(pcity), city_tile(pcity),
1122 _("Famine feared in %s, %s lost!"),
1123 city_link(pcity), punit_link);
1124 }
1125
1126 if (city_exist(saved_id)) {
1128 }
1129
1130 return;
1131 }
1133 if (city_size_get(pcity) > 1) {
1134 notify_player(city_owner(pcity), city_tile(pcity),
1136 _("Famine causes population loss in %s."),
1137 city_link(pcity));
1138 } else {
1139 notify_player(city_owner(pcity), city_tile(pcity),
1141 _("Famine destroys %s entirely."),
1142 city_link(pcity));
1143 }
1144 city_shrink_reset_foodbox(pcity, city_size_get(pcity) - 1);
1145 if (city_reduce_size(pcity, 1, NULL, "famine")) {
1146 pcity->had_famine = TRUE;
1147 }
1148 }
1149}
1150
1151/**********************************************************************/
1158 struct city *pcity,
1159 struct player *pplayer,
1160 int saved_id)
1161{
1162 const void *ptarget;
1163 const char *tgt_name;
1164 const struct requirement_vector *build_reqs;
1165 const char *signal_name;
1166 const struct req_context city_ctxt = {
1167 .player = pplayer,
1168 .city = pcity,
1169 .tile = city_tile(pcity)
1170 /* FIXME: Setting .unittype is currently redundant,
1171 * but can_city_build_unit_direct() does it */
1172 };
1173 bool purge = FALSE;
1174 bool known = FALSE;
1175
1176 if (pcity->wlcb == WLCB_ALWAYS_PURGE) {
1177 return TRUE;
1178 }
1179
1180 switch (target->kind) {
1181 case VUT_UTYPE:
1182 ptarget = target->value.utype;
1183 build_reqs = &target->value.utype->build_reqs;
1185 signal_name = "unit_cant_be_built";
1186 break;
1187 case VUT_IMPROVEMENT:
1188 ptarget = target->value.building;
1189 build_reqs = &target->value.building->reqs;
1191 signal_name = "building_cant_be_built";
1192 break;
1193 default:
1195 || target->kind == VUT_UTYPE), FALSE);
1196 return FALSE;
1197 }
1198
1199 if (pcity->wlcb == WLCB_ALWAYS_POSTPONE) {
1200 notify_player(pplayer, city_tile(pcity),
1202 _("%s can't build %s from the worklist. "
1203 "Postponing..."),
1204 city_link(pcity),
1205 tgt_name);
1206 return FALSE;
1207 }
1208
1209 requirement_vector_iterate(build_reqs, preq) {
1211 known = TRUE;
1212 switch (preq->source.kind) {
1213 case VUT_COUNTER:
1214 if (preq->present) {
1215 notify_player(pplayer, city_tile(pcity),
1217 _("%s can't build %s from the worklist; "
1218 "counter %s value's checkpoint do not met "
1219 "Postponing..."),
1220 city_link(pcity),
1221 tgt_name,
1223 (preq->source.value.counter));
1224 } else {
1225 purge = TRUE;
1226 }
1227 break;
1228 case VUT_ADVANCE:
1229 if (preq->present) {
1230 notify_player(pplayer, city_tile(pcity),
1232 _("%s can't build %s from the worklist; "
1233 "tech %s not yet available. Postponing..."),
1234 city_link(pcity),
1235 tgt_name,
1237 (preq->source.value.advance));
1239 pcity, "need_tech");
1240 } else {
1241 /* While techs can be unlearned, this isn't useful feedback */
1242 purge = TRUE;
1243 }
1244 break;
1245 case VUT_TECHFLAG:
1246 if (preq->present) {
1247 notify_player(pplayer, city_tile(pcity),
1249 _("%s can't build %s from the worklist; "
1250 "no tech with flag \"%s\" yet available. "
1251 "Postponing..."),
1252 city_link(pcity),
1253 tgt_name,
1254 tech_flag_id_name(preq->source.value.techflag));
1256 pcity, "need_techflag");
1257 } else {
1258 /* While techs can be unlearned, this isn't useful feedback */
1259 purge = TRUE;
1260 }
1261 break;
1262 case VUT_IMPROVEMENT:
1263 case VUT_SITE:
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_MAXTILEUNITS:
1794 if (preq->present) {
1795 notify_player(pplayer, city_tile(pcity),
1797 PL_("%s can't build %s from the worklist; "
1798 "more than %d unit on tile."
1799 " Postponing...",
1800 "%s can't build %s from the worklist; "
1801 "more than %d units on tile."
1802 " Postponing...",
1803 preq->source.value.max_tile_units),
1804 city_link(pcity),
1805 tgt_name,
1806 preq->source.value.max_tile_units);
1808 pcity, "need_tileunits");
1809 } else {
1810 notify_player(pplayer, city_tile(pcity),
1812 PL_("%s can't build %s from the worklist; "
1813 "fewer than %d unit on tile."
1814 " Postponing...",
1815 "%s can't build %s from the worklist; "
1816 "fewer than %d units on tile."
1817 " Postponing...",
1818 preq->source.value.max_tile_units + 1),
1819 city_link(pcity),
1820 tgt_name,
1821 preq->source.value.max_tile_units + 1);
1823 pcity, "need_tileunits");
1824 }
1825 break;
1826 case VUT_AI_LEVEL:
1827 /* Can't change AI level. */
1828 purge = TRUE;
1829 break;
1830 case VUT_TERRAINCLASS:
1831 /* Change of terrain class is expected to be very unlikely. Purge!
1832 * TODO: Analyze ruleset to see how unlikely terrain class change actually is. */
1833 purge = TRUE;
1834 break;
1835 case VUT_TERRFLAG:
1836 if (preq->present) {
1837 notify_player(pplayer, city_tile(pcity),
1839 _("%s can't build %s from the worklist; "
1840 "terrain with \"%s\" flag is required. "
1841 "Postponing..."),
1842 city_link(pcity),
1843 tgt_name,
1844 terrain_flag_id_name(preq->source.value.terrainflag));
1846 pcity, "need_terrainflag");
1847 } else {
1848 notify_player(pplayer, city_tile(pcity),
1850 _("%s can't build %s from the worklist; "
1851 "terrain with \"%s\" flag is prohibited. "
1852 "Postponing..."),
1853 city_link(pcity),
1854 tgt_name,
1855 terrain_flag_id_name(preq->source.value.terrainflag));
1857 pcity, "have_terrainflag");
1858 }
1859 break;
1860 case VUT_ROADFLAG:
1861 if (preq->present) {
1862 notify_player(pplayer, city_tile(pcity),
1864 _("%s can't build %s from the worklist; "
1865 "road with \"%s\" flag is required. "
1866 "Postponing..."),
1867 city_link(pcity),
1868 tgt_name,
1869 road_flag_id_name(preq->source.value.roadflag));
1871 pcity, "need_roadflag");
1872 } else {
1873 notify_player(pplayer, city_tile(pcity),
1875 _("%s can't build %s from the worklist; "
1876 "road with \"%s\" flag is prohibited. "
1877 "Postponing..."),
1878 city_link(pcity),
1879 tgt_name,
1880 road_flag_id_name(preq->source.value.roadflag));
1882 pcity, "have_roadflag");
1883 }
1884 break;
1885 case VUT_EXTRAFLAG:
1886 if (preq->present) {
1887 notify_player(pplayer, city_tile(pcity),
1889 _("%s can't build %s from the worklist; "
1890 "extra with \"%s\" flag is required. "
1891 "Postponing..."),
1892 city_link(pcity),
1893 tgt_name,
1894 extra_flag_id_translated_name(preq->source.value.extraflag));
1896 pcity, "need_extraflag");
1897 } else {
1898 notify_player(pplayer, city_tile(pcity),
1900 _("%s can't build %s from the worklist; "
1901 "extra with \"%s\" flag is prohibited. "
1902 "Postponing..."),
1903 city_link(pcity),
1904 tgt_name,
1905 extra_flag_id_translated_name(preq->source.value.extraflag));
1907 pcity, "have_extraflag");
1908 }
1909 break;
1910 case VUT_MINLATITUDE:
1911 case VUT_MAXLATITUDE:
1912 /* Can't change where the city is located. */
1913 purge = TRUE;
1914 break;
1915 case VUT_CITYTILE:
1916 if (CITYT_BORDERING_TCLASS_REGION == preq->source.value.citytile
1917 && (preq->range == REQ_RANGE_CADJACENT
1918 || preq->range == REQ_RANGE_ADJACENT)) {
1919 if (preq->present) {
1920 notify_player(pplayer, city_tile(pcity),
1922 _("%s can't build %s from the worklist; "
1923 "different terrain class nearby is required. "
1924 "Postponing..."),
1925 city_link(pcity),
1926 tgt_name);
1928 pcity, "need_different_terrainclass");
1929 } else {
1930 notify_player(pplayer, city_tile(pcity),
1932 _("%s can't build %s from the worklist; "
1933 "different terrain class nearby is prohibited. "
1934 "Postponing..."),
1935 city_link(pcity),
1936 tgt_name);
1938 pcity, "have_different_terrainclass");
1939 }
1940 break;
1941 }
1942 /* Other values should not present in build reqs */
1944
1945 case VUT_UTYPE:
1946 case VUT_UTFLAG:
1947 case VUT_UCLASS:
1948 case VUT_UCFLAG:
1949 case VUT_MINVETERAN:
1950 case VUT_UNITSTATE:
1951 case VUT_ACTIVITY:
1952 case VUT_MINMOVES:
1953 case VUT_MINHP:
1954 case VUT_ACTION:
1955 case VUT_OTYPE:
1956 case VUT_SPECIALIST:
1957 case VUT_TERRAINALTER: /* XXX could do this in principle */
1958 /* Will only happen with a bogus ruleset. */
1959 log_error("worklist_change_build_target() has bogus preq");
1960 break;
1961 case VUT_CITYSTATUS:
1962 if (preq->source.value.citystatus == CITYS_TRANSFERRED) {
1963 /* If there's a change, it will invalidate worklist anyway. */
1964 purge = TRUE;
1965 } else if (preq->source.value.citystatus == CITYS_OWNED_BY_ORIGINAL) {
1966 if (preq->range == REQ_RANGE_CITY || preq->range == REQ_RANGE_TILE) {
1967 /* Can't change at these ranges */
1968 purge = TRUE;
1969 } else {
1970 if (preq->present) {
1971 notify_player(pplayer, city_tile(pcity),
1973 /* TRANS: last %s is a CityStatus ("OwnedByOriginal") */
1974 _("%s can't build %s from the worklist; "
1975 "only available when city in range %s \"%s\". "
1976 "Postponing..."),
1977 city_link(pcity),
1978 tgt_name, req_range_name(preq->range),
1979 citystatus_type_name(preq->source.value.citystatus));
1981 pcity, "need_citystatus");
1982 } else {
1983 notify_player(pplayer, city_tile(pcity),
1985 /* TRANS: last %s is a CityStatus ("OwnedByOriginal") */
1986 _("%s can't build %s from the worklist; "
1987 "not available when city in range %s is \"%s\". "
1988 "Postponing..."),
1989 city_link(pcity),
1990 tgt_name, req_range_name(preq->range),
1991 citystatus_type_name(preq->source.value.citystatus));
1993 pcity, "have_citystatus");
1994 }
1995 }
1996 } else {
1997 /* Other status types will only happen with a bogus ruleset. */
1998 log_error("worklist_change_build_target() has bogus citystatus preq");
1999 }
2000 break;
2001 case VUT_MINYEAR:
2002 if (preq->present) {
2003 notify_player(pplayer, city_tile(pcity),
2005 /* TRANS: last %s is a date */
2006 _("%s can't build %s from the worklist; "
2007 "only available from %s. Postponing..."),
2008 city_link(pcity),
2009 tgt_name,
2010 textyear(preq->source.value.minyear));
2012 pcity, "need_minyear");
2013 } else {
2014 /* Can't go back in time. */
2015 purge = TRUE;
2016 }
2017 break;
2018 case VUT_MINCALFRAG:
2019 /* Unlike VUT_MINYEAR, a requirement in either direction is
2020 * likely to be fulfilled sooner or later. */
2021 if (preq->present) {
2022 notify_player(pplayer, city_tile(pcity),
2024 /* TRANS: last %s is a calendar fragment from
2025 * the ruleset; may be a bare number */
2026 _("%s can't build %s from the worklist; "
2027 "only available from %s. Postponing..."),
2028 city_link(pcity),
2029 tgt_name,
2030 textcalfrag(preq->source.value.mincalfrag));
2032 pcity, "need_mincalfrag");
2033 } else {
2034 fc_assert_action(preq->source.value.mincalfrag > 0, break);
2035 notify_player(pplayer, city_tile(pcity),
2037 /* TRANS: last %s is a calendar fragment from
2038 * the ruleset; may be a bare number */
2039 _("%s can't build %s from the worklist; "
2040 "not available after %s. Postponing..."),
2041 city_link(pcity),
2042 tgt_name,
2043 textcalfrag(preq->source.value.mincalfrag - 1));
2045 pcity, "have_mincalfrag");
2046 }
2047 break;
2048 case VUT_TOPO:
2049 if (preq->present) {
2050 notify_player(pplayer, city_tile(pcity),
2052 /* TRANS: third %s is topology flag name
2053 * ("Hex", "ISO" */
2054 _("%s can't build %s from the worklist; "
2055 "only available in worlds with %s map."),
2056 city_link(pcity),
2057 tgt_name,
2058 _(topo_flag_name(preq->source.value.topo_property)));
2060 pcity, "need_topo");
2061 }
2062 purge = TRUE;
2063 break;
2064 case VUT_WRAP:
2065 if (preq->present) {
2066 notify_player(pplayer, city_tile(pcity),
2068 /* TRANS: third %s is wrap flag name
2069 * ("WrapX", "Wrapy") */
2070 _("%s can't build %s from the worklist; "
2071 "only available in worlds with %s map."),
2072 city_link(pcity),
2073 tgt_name,
2074 _(wrap_flag_name(preq->source.value.wrap_property)));
2076 pcity, "need_wrap");
2077 }
2078 purge = TRUE;
2079 break;
2080 case VUT_SERVERSETTING:
2081 notify_player(pplayer, city_tile(pcity),
2083 /* TRANS: %s is a server setting, its value and
2084 * if it is required to be present or absent.
2085 * The string's format is specified in
2086 * ssetv_human_readable().
2087 * Example: "killstack is enabled". */
2088 _("%s can't build %s from the worklist; "
2089 "only available when the server setting "
2090 "%s."),
2091 city_link(pcity),
2092 tgt_name,
2093 ssetv_human_readable(preq->source.value.ssetval,
2094 preq->present));
2096 pcity, "need_setting");
2097 /* Don't assume that the server setting will be changed. */
2098 purge = TRUE;
2099 break;
2100 case VUT_AGE:
2101 if (preq->present) {
2102 notify_player(pplayer, city_tile(pcity),
2104 _("%s can't build %s from the worklist; "
2105 "only available once %d turns old. Postponing..."),
2106 city_link(pcity),
2107 tgt_name,
2108 preq->source.value.age);
2110 pcity, "need_age");
2111 } else {
2112 /* Can't go back in time. */
2113 purge = TRUE;
2114 }
2115 break;
2116 case VUT_FORM_AGE:
2117 if (preq->present) {
2118 notify_player(pplayer, city_tile(pcity),
2120 _("%s can't build %s from the worklist; "
2121 "only available once %d turns old form. Postponing..."),
2122 city_link(pcity),
2123 tgt_name,
2124 preq->source.value.age);
2126 pcity, "need_form_age");
2127 } else {
2128 /* Can't go back in time. */
2129 purge = TRUE;
2130 }
2131 break;
2132 case VUT_NONE:
2133 case VUT_COUNT:
2135 "worklist_change_build_target() "
2136 "called with invalid preq");
2137 break;
2138 /* No default handling here, as we want compiler warning
2139 * if new requirement type is added to enum and it's not handled
2140 * here. */
2141 };
2142 break;
2143 }
2144
2145 /* Almost all cases emit signal in the end, so city check needed. */
2146 if (!city_exist(saved_id)) {
2147 /* Some script has removed city */
2148 return TRUE;
2149 }
2150
2152
2153 if (!known) {
2154 /* FIXME: make can_city_build_improvement_now() return a reason enum,
2155 * so we can notify user with it.
2156 * Likely the building already exist. */
2157 purge = TRUE;
2158 }
2159
2160 return purge;
2161}
2162
2163/**********************************************************************/
2169static bool worklist_change_build_target(struct player *pplayer,
2170 struct city *pcity)
2171{
2172 struct universal target;
2173 bool success = FALSE;
2174 int i;
2175 int saved_id = pcity->id;
2176 bool city_checked = TRUE; /* This is used to avoid spurious city_exist() calls */
2177 struct worklist *pwl = &pcity->worklist;
2178 const struct civ_map *nmap = &(wld.map);
2179
2180 if (worklist_is_empty(pwl)) {
2181 /* Nothing in the worklist; bail now. */
2182 return FALSE;
2183 }
2184
2185 i = 0;
2186 while (!success && i < worklist_length(pwl)) {
2187
2188 if (!city_checked) {
2189 if (!city_exist(saved_id)) {
2190 /* Some script has removed useless city that cannot build
2191 * what it is told to! */
2192 return FALSE;
2193 }
2195 }
2196
2197 if (worklist_peek_ith(pwl, &target, i)) {
2198 success = can_city_build_now(nmap, pcity, &target);
2199 } else {
2200 success = FALSE;
2201 }
2202 i++;
2203
2204 if (success) {
2205 break; /* while */
2206 }
2207
2208 switch (target.kind) {
2209 case VUT_UTYPE:
2210 {
2211 const struct unit_type *ptarget = target.value.utype;
2212 const struct unit_type *pupdate = unit_upgrades_to(pcity, ptarget);
2213 bool purge;
2214
2215 /* Maybe we can just upgrade the target to what the city /can/ build. */
2216 if (U_NOT_OBSOLETED == pupdate) {
2217 /* Nope, we're stuck. Skip this item from the worklist. */
2218 struct research *presearch = research_get(pplayer);
2219 struct advance *missing = NULL;
2220 bool multiple = FALSE;
2221
2224 if (missing != NULL) {
2225 multiple = TRUE;
2226 } else {
2227 missing = padv;
2228 }
2229 }
2231
2232
2233 purge = FALSE;
2234 if (missing != NULL) {
2235 if (!multiple) {
2236 notify_player(pplayer, city_tile(pcity),
2238 _("%s can't build %s from the worklist; "
2239 "tech %s not yet available. Postponing..."),
2242 } else {
2243 notify_player(pplayer, city_tile(pcity),
2245 _("%s can't build %s from the worklist; "
2246 "multiple techs still needed. Postponing..."),
2248 }
2249
2250 script_server_signal_emit("unit_cant_be_built", ptarget, pcity,
2251 "need_tech");
2252 } else {
2253 /* Unknown or requirement from vector. */
2254 purge = worklist_item_postpone_req_vec(&target, pcity, pplayer,
2255 saved_id);
2256 }
2258 if (!purge) {
2259 break;
2260 }
2261 } else {
2263 }
2264 if (purge) {
2265 /* If the city can never build this unit or its descendants,
2266 * drop it. */
2267 notify_player(pplayer, city_tile(pcity),
2269 _("%s can't build %s from the worklist. Purging..."),
2270 city_link(pcity),
2271 /* Yes, warn about the targets that's actually
2272 in the worklist, not its obsolete-closure
2273 pupdate. */
2275 script_server_signal_emit("unit_cant_be_built", ptarget, pcity,
2276 "never");
2277 if (city_exist(saved_id)) {
2279 /* Purge this worklist item. */
2280 i--;
2282 } else {
2284 }
2285 } else {
2286 /* Yep, we can go after pupdate instead. Joy! */
2287 notify_player(pplayer, city_tile(pcity), E_WORKLIST, ftc_server,
2288 _("Production of %s is upgraded to %s in %s."),
2291 city_link(pcity));
2292 target.value.utype = pupdate;
2293 }
2294 break;
2295 }
2296 case VUT_IMPROVEMENT:
2297 {
2298 const struct impr_type *ptarget = target.value.building;
2299 const struct impr_type *pupdate = building_upgrades_to(pcity, ptarget);
2300 bool purge;
2301
2302 /* If the city can never build this improvement, drop it. */
2304 purge = !success;
2305
2306 /* Maybe this improvement has been obsoleted by something that
2307 we can build. */
2308 if (purge) {
2309 /* Nope, no use. *sigh* */
2310
2311 /* Can it be postponed? */
2313 purge = worklist_item_postpone_req_vec(&target, pcity, pplayer,
2314 saved_id);
2315
2316 /* Almost all cases emit signal in the end, so city check needed. */
2317 if (!city_exist(saved_id)) {
2318 /* Some script has removed city */
2319 return FALSE;
2320 }
2322 }
2323 } else if (success) {
2324 /* Hey, we can upgrade the improvement! */
2325 notify_player(pplayer, city_tile(pcity), E_WORKLIST, ftc_server,
2326 _("Production of %s is upgraded to %s in %s."),
2329 city_link(pcity));
2330 target.value.building = pupdate;
2331 }
2332
2333 if (purge) {
2334 /* Never in a million years. */
2335 notify_player(pplayer, city_tile(pcity),
2337 _("%s can't build %s from the worklist. Purging..."),
2338 city_link(pcity),
2340 script_server_signal_emit("building_cant_be_built", ptarget, pcity,
2341 "never");
2342 if (city_exist(saved_id)) {
2344 /* Purge this worklist item. */
2345 i--;
2347 } else {
2349 }
2350 }
2351 break;
2352 }
2353 default:
2354 /* skip useless target */
2355 log_error("worklist_change_build_target() has unrecognized "
2356 "target kind (%d)", target.kind);
2357 break;
2358 };
2359 } /* while */
2360
2361 if (success) {
2362 /* All okay. Switch targets. */
2363 change_build_target(pplayer, pcity, &target, E_WORKLIST);
2364
2365 /* i is the index immediately _after_ the item we're changing to.
2366 Remove the (i-1)th item from the worklist. */
2367 worklist_remove(pwl, i - 1);
2368 }
2369
2370 if (worklist_is_empty(pwl)) {
2371 /* There *was* something in the worklist, but it's empty now. Bug the
2372 player about it. */
2373 notify_player(pplayer, city_tile(pcity), E_WORKLIST, ftc_server,
2374 /* TRANS: The <city> worklist .... */
2375 _("The %s worklist is now empty."),
2376 city_link(pcity));
2377 }
2378
2379 return success;
2380}
2381
2382/**********************************************************************/
2387void choose_build_target(struct player *pplayer, struct city *pcity)
2388{
2389 const struct civ_map *nmap = &(wld.map);
2390
2391 /* Pick the next thing off the worklist. */
2392 if (worklist_change_build_target(pplayer, pcity)) {
2393 return;
2394 }
2395
2396 /* Try building the same thing again. Repeat building doesn't require a
2397 * call to change_build_target, so just return. */
2398 switch (pcity->production.kind) {
2399 case VUT_UTYPE:
2400 /* We can build a unit again unless it's unique or we have lost the tech. */
2402 && can_city_build_unit_now(nmap, pcity, pcity->production.value.utype)) {
2403 log_base(LOG_BUILD_TARGET, "%s repeats building %s", city_name_get(pcity),
2405 return;
2406 }
2407 break;
2408 case VUT_IMPROVEMENT:
2410 /* We can build space and coinage again, and possibly others. */
2411 log_base(LOG_BUILD_TARGET, "%s repeats building %s", city_name_get(pcity),
2413 return;
2414 }
2415 break;
2416 default:
2417 /* fallthru */
2418 break;
2419 };
2420
2421 /* Find *something* to do! */
2422 log_debug("Trying advisor_choose_build.");
2423 advisor_choose_build(pplayer, pcity);
2424 log_debug("Advisor_choose_build didn't kill us.");
2425}
2426
2427/**********************************************************************/
2432static const struct impr_type *building_upgrades_to(struct city *pcity,
2433 const struct impr_type *pimprove)
2434{
2435 const struct impr_type *check = pimprove;
2436 const struct impr_type *best_upgrade = NULL;
2437
2439 return NULL;
2440 }
2444 }
2445 }
2446
2447 return best_upgrade;
2448}
2449
2450/**********************************************************************/
2453static void upgrade_building_prod(struct city *pcity)
2454{
2455 const struct impr_type *producing = pcity->production.value.building;
2456 const struct impr_type *upgrading = building_upgrades_to(pcity, producing);
2457
2459 notify_player(city_owner(pcity), city_tile(pcity),
2461 _("Production of %s is upgraded to %s in %s."),
2464 city_link(pcity));
2467 }
2468}
2469
2470/**********************************************************************/
2478static const struct unit_type *unit_upgrades_to(struct city *pcity,
2479 const struct unit_type *punittype)
2480{
2481 const struct unit_type *check = punittype;
2482 const struct unit_type *best_upgrade = U_NOT_OBSOLETED;
2483 const struct civ_map *nmap = &(wld.map);
2484
2486 return U_NOT_OBSOLETED;
2487 }
2488 while ((check = check->obsoleted_by) != U_NOT_OBSOLETED) {
2489 if (can_city_build_unit_direct(nmap, pcity, check)) {
2491 }
2492 }
2493
2494 return best_upgrade;
2495}
2496
2497/**********************************************************************/
2500static void upgrade_unit_prod(struct city *pcity)
2501{
2502 const struct unit_type *producing = pcity->production.value.utype;
2503 const struct unit_type *upgrading = unit_upgrades_to(pcity, producing);
2504 const struct civ_map *nmap = &(wld.map);
2505
2507 notify_player(city_owner(pcity), city_tile(pcity),
2509 _("Production of %s is upgraded to %s in %s."),
2512 city_link(pcity));
2514 }
2515}
2516
2517/**********************************************************************/
2524static bool city_distribute_surplus_shields(struct player *pplayer,
2525 struct city *pcity)
2526{
2527 int size_reduction = 0;
2528 struct unit *sacrifizer;
2529
2530 if (pcity->surplus[O_SHIELD] < 0) {
2532 if (utype_upkeep_cost(unit_type_get(punit), pplayer, O_SHIELD) > 0
2533 && pcity->surplus[O_SHIELD] < 0) {
2534 const char *punit_link = unit_link(punit);
2535
2536 /* TODO: Should the unit try to help cities on adjacent tiles? That
2537 * would be a rules change. (This action is performed by the game
2538 * it self) */
2541 notify_player(pplayer, city_tile(pcity),
2543 _("%s can't upkeep %s, unit disbanded."),
2544 city_link(pcity), punit_link);
2545 }
2546
2547 /* pcity->surplus[O_SHIELD] is automatically updated. */
2548 }
2550 }
2551
2552 if (pcity->surplus[O_SHIELD] < 0) {
2553 /* Special case: MissingXProtected. This nasty unit won't go so easily.
2554 * It'd rather make the citizens pay in blood for their failure to upkeep
2555 * it! If we make it here all normal units are already disbanded, so only
2556 * undisbandable ones remain. */
2559
2560 if (upkeep > 0 && pcity->surplus[O_SHIELD] < 0) {
2561
2563 sacrifizer = punit;
2564
2565 /* No upkeep for the unit this turn. */
2566 pcity->surplus[O_SHIELD] += upkeep;
2567 }
2569 }
2570
2571 /* Now we confirm changes made last turn. */
2572 pcity->shield_stock += pcity->surplus[O_SHIELD];
2573 pcity->before_change_shields = pcity->shield_stock;
2575
2576 /* Previous turn values stored, and they are consistent with
2577 * other previous turn data.
2578 * Now reduce city size, likely messing all the values. */
2579 if (size_reduction > 0) {
2580 if (size_reduction == 1) {
2581 notify_player(pplayer, city_tile(pcity),
2583 _("Citizens in %s perish for their failure to "
2584 "upkeep %s!"),
2585 city_link(pcity), unit_link(sacrifizer));
2586 } else {
2587 notify_player(pplayer, city_tile(pcity),
2589 _("Citizens in %s perish for their failure to "
2590 "upkeep units!"),
2591 city_link(pcity));
2592 }
2593
2594 if (!city_reduce_size(pcity, size_reduction, NULL, "upkeep_failure")) {
2595 return FALSE;
2596 }
2597 }
2598
2599 return TRUE;
2600}
2601
2602/**********************************************************************/
2605static bool city_build_building(struct player *pplayer, struct city *pcity)
2606{
2607 bool space_part;
2608 int mod;
2609 const struct impr_type *pimprove;
2610 int saved_id = pcity->id;
2611
2613 /* Coinage-like improvements that convert production */
2614 fc_assert(pcity->before_change_shields >= 0);
2615
2616 /* pcity->before_change_shields already contains the surplus from
2617 * this turn. */
2618 if (city_production_has_flag(pcity, IF_GOLD)) {
2619 pplayer->economic.gold += pcity->before_change_shields;
2620 }
2621
2622 pcity->before_change_shields = 0;
2623 pcity->shield_stock = 0;
2624 choose_build_target(pplayer, pcity);
2625
2626 return TRUE;
2627 }
2628
2629 upgrade_building_prod(pcity);
2630
2631 /* The final (after upgrade) build target */
2632 pimprove = pcity->production.value.building;
2633
2634 if (!can_city_build_improvement_now(pcity, pimprove)) {
2636 _("%s is building %s, which is no longer available."),
2637 city_link(pcity),
2638 city_improvement_name_translation(pcity, pimprove));
2639 script_server_signal_emit("building_cant_be_built", pimprove, pcity,
2640 "unavailable");
2641 return TRUE;
2642 }
2643 if (pcity->shield_stock >= impr_build_shield_cost(pcity, pimprove)) {
2644 int cost;
2645
2646 if (is_small_wonder(pimprove)) {
2647 city_list_iterate(pplayer->cities, wcity) {
2648 if (city_has_building(wcity, pimprove)) {
2649 city_remove_improvement(wcity, pimprove);
2650 break;
2651 }
2653 }
2654
2655 space_part = TRUE;
2657 RPT_CERTAIN) > 0) {
2658 pplayer->spaceship.structurals++;
2660 RPT_CERTAIN) > 0) {
2661 pplayer->spaceship.components++;
2663 RPT_CERTAIN) > 0) {
2664 pplayer->spaceship.modules++;
2665 } else {
2666 space_part = FALSE;
2668 _("Completion of %s"));
2669 }
2670 cost = impr_build_shield_cost(pcity, pimprove);
2671 pcity->before_change_shields -= cost;
2672 pcity->shield_stock -= cost;
2673 pcity->turn_last_built = game.info.turn;
2674 /* to eliminate micromanagement */
2675 if (is_great_wonder(pimprove)) {
2677 _("The %s have finished building %s in %s."),
2678 nation_plural_for_player(pplayer),
2679 city_improvement_name_translation(pcity, pimprove),
2680 city_link(pcity));
2681 }
2682
2684 _("%s has finished building %s."),
2685 city_link(pcity), improvement_name_translation(pimprove));
2686 script_server_signal_emit("building_built", pimprove, pcity);
2687
2688 if (!city_exist(saved_id)) {
2689 /* Script removed city */
2690 return FALSE;
2691 }
2692
2693 /* Call this function since some buildings may change the
2694 * the vision range of a city */
2695 city_refresh_vision(pcity);
2696
2698 RPT_CERTAIN))) {
2699 struct research *presearch = research_get(pplayer);
2700 char research_name[MAX_LEN_NAME * 2];
2701 int i;
2702 const char *provider = improvement_name_translation(pimprove);
2703
2705 PL_("%s boosts research; you gain %d immediate "
2706 "advance.",
2707 "%s boosts research; you gain %d immediate "
2708 "advances.",
2709 mod), provider, mod);
2710
2712 for (i = 0; i < mod; i++) {
2715
2718 /* TRANS: Tech from building (Darwin's Voyage) */
2719 Q_("?frombldg:Acquired %s from %s."), adv_name,
2720 provider);
2721
2723 /* TRANS: Tech from building (Darwin's
2724 * Voyage) */
2725 Q_("?frombldg:The %s have acquired %s "
2726 "from %s."),
2728 }
2729 }
2730 if (space_part && pplayer->spaceship.state == SSHIP_NONE) {
2732 _("The %s have started building a spaceship!"),
2733 nation_plural_for_player(pplayer));
2734 pplayer->spaceship.state = SSHIP_STARTED;
2735 }
2736 if (space_part) {
2737 /* space ship part build */
2738 send_spaceship_info(pplayer, NULL);
2739 } else {
2740 /* Update city data. */
2741 if (city_refresh(pcity)) {
2742 auto_arrange_workers(pcity);
2743 }
2744 }
2745
2746 /* Move to the next thing in the worklist */
2747 choose_build_target(pplayer, pcity);
2748 }
2749
2750 return TRUE;
2751}
2752
2753/**********************************************************************/
2762static struct unit *city_create_unit(struct city *pcity,
2763 const struct unit_type *utype,
2764 struct citizens_reduction *red)
2765{
2766 struct player *pplayer = city_owner(pcity);
2767 struct unit *punit;
2768 int saved_unit_id;
2769 int pop_cost = utype_pop_value(utype, pcity);
2770
2771 punit = unit_virtual_prepare(pplayer, pcity->tile, utype,
2773 pcity->id, -1, -1);
2774 pplayer->score.units_built++;
2775
2776 if (pop_cost > 0 && pcity->nationality != NULL) {
2777 /* We don't reduce city size in-place to keep it correct and
2778 * existing at all while we call the following callback.
2779 * We want citizens_unit_nationality() to adjust 'red' even when
2780 * we are not setting unit nationality based on the return */
2781 struct player *nat = citizens_unit_nationality(pcity, pop_cost, red);
2782
2785 }
2786 } else if (red) {
2787 red->change = 0;
2788 }
2789
2790 (void) place_unit(punit, pplayer, pcity, NULL, FALSE);
2792
2793 /* If city has a rally point set, give the unit a move order. */
2794 if (pcity->rally_point.length) {
2799 * sizeof(struct unit_order));
2801 pcity->rally_point.length * sizeof(struct unit_order));
2802 }
2803
2804 /* This might destroy pcity and/or punit: */
2805 script_server_signal_emit("unit_built", punit, pcity);
2806
2808 return punit;
2809 } else {
2810 return NULL;
2811 }
2812}
2813
2814/**********************************************************************/
2823static bool city_build_unit(struct player *pplayer, struct city *pcity)
2824{
2825 const struct unit_type *utype;
2826 struct worklist *pwl = &pcity->worklist;
2828 int saved_city_id = pcity->id;
2829 const struct civ_map *nmap = &(wld.map);
2830
2832
2833 /* If the city has already bought a unit which is now obsolete, don't try
2834 * to upgrade the production. The new unit might require more shields, which
2835 * would be bad if it was bought to urgently defend a city. (Equally it
2836 * might be the same cost or cheaper, but tough; you hurried the unit so
2837 * you miss out on technological advances.) */
2838 if (city_can_change_build(pcity)) {
2839 upgrade_unit_prod(pcity);
2840 }
2841
2842 utype = pcity->production.value.utype;
2844
2845 /* We must make a special case for barbarians here, because they are
2846 so dumb. Really. They don't know the prerequisite techs for units
2847 they build!! - Per */
2848 if (!can_city_build_unit_direct(nmap, pcity, utype)
2849 && !is_barbarian(pplayer)) {
2851 _("%s is building %s, which is no longer available."),
2852 city_link(pcity), utype_name_translation(utype));
2853
2854 /* Log before signal emitting, so pointers are certainly valid */
2855 log_verbose("%s %s tried to build %s, which is not available.",
2857 city_name_get(pcity), utype_rule_name(utype));
2858 script_server_signal_emit("unit_cant_be_built", utype, pcity,
2859 "unavailable");
2860 return city_exist(saved_city_id);
2861 }
2862
2863 if (pcity->shield_stock >= unit_shield_cost) {
2864 int pop_cost = utype_pop_value(utype, pcity);
2865 struct unit *punit;
2866
2867 /* Should we disband the city? -- Massimo */
2868 if (city_size_get(pcity) == pop_cost
2869 && is_city_option_set(pcity, CITYO_DISBAND)) {
2870 return !disband_city(pcity);
2871 }
2872
2873 if (city_size_get(pcity) <= pop_cost) {
2875 /* TRANS: city ... utype ... size ... pop_cost */
2876 _("%s can't build %s yet. "
2877 "(city size: %d, unit population cost: %d)"),
2879 city_size_get(pcity), pop_cost);
2880 script_server_signal_emit("unit_cant_be_built", utype, pcity,
2881 "pop_cost");
2882 return city_exist(saved_city_id);
2883 }
2884
2885 fc_assert(pop_cost == 0 || city_size_get(pcity) >= pop_cost);
2886
2887 /* don't update turn_last_built if we returned above */
2888 pcity->turn_last_built = game.info.turn;
2889
2890 /* check if we can build more than one unit (effect City_Build_Slots) */
2892
2893 /* We should be able to build at least one (by checks above) */
2894 fc_assert(num_units >= 1);
2895
2896 for (i = 0; i < num_units; i++) {
2898
2899 punit = city_create_unit(pcity, utype, natred);
2900
2901 /* Check if the city still exists (script might have removed it).
2902 * If not, we assume any effects / announcements done below were
2903 * already replaced by the script if necessary. */
2904 if (!city_exist(saved_city_id)) {
2905 break;
2906 }
2907
2908 if (punit) {
2909 if (punit->carrying
2912 /* TRANS: <city> is finished building <unit>, carrying <goods>. */
2913 _("%s is finished building %s, carrying %s."),
2914 city_link(pcity), utype_name_translation(utype),
2916 } else {
2918 /* TRANS: <city> is finished building <unit/building>. */
2919 _("%s is finished building %s."),
2920 city_link(pcity), utype_name_translation(utype));
2921 }
2922 }
2923
2924 /* After we created the unit remove the citizen. This will also
2925 * rearrange the worker to take into account the extra resources
2926 * (food) needed. */
2927 if (pop_cost > 0) {
2928 /* This won't disband city due to pop_cost, but script might
2929 * still destroy city. */
2931 /* If the city has changed its nationalities during
2932 * "unit_built" signal, we take some other citizens instead */
2933 if (!city_reduce_size(pcity, pop_cost, NULL, "unit_built")) {
2934 break;
2935 }
2936 }
2937
2938 /* to eliminate micromanagement, we only subtract the unit's cost */
2939 /* signals could change the prod stock! */
2940 if ((pcity->before_change_shields -= unit_shield_cost) < 0) {
2941 pcity->before_change_shields = 0;
2942 }
2943 if ((pcity->shield_stock -= unit_shield_cost) < 0) {
2944 log_normal("City %s (%s) has built %s but has no %d shields "
2945 "for it, nullifying shield stock", city_name_get(pcity),
2946 player_name(pplayer), utype_rule_name(utype),
2948 pcity->shield_stock = 0;
2949 }
2950
2951 if (pop_cost > 0) {
2952 /* Additional message if the unit has population cost. */
2954 ftc_server,
2955 /* TRANS: "<unit> cost... <city> shrinks..."
2956 * Plural in "%d population", not "size %d". */
2957 PL_("%s cost %d population. %s shrinks to size %d.",
2958 "%s cost %d population. %s shrinks to size %d.",
2959 pop_cost),
2960 utype_name_translation(utype), pop_cost,
2961 city_link(pcity), city_size_get(pcity));
2962 }
2963
2964 if (i != 0 && worklist_length(pwl) > 0) {
2965 /* remove the build unit from the worklist; it has to be one less
2966 * than units build to preserve the next build target from the
2967 * worklist */
2968 worklist_remove(pwl, 0);
2969 }
2970 } /* for */
2971
2973 if (pcity->rally_point.length && !pcity->rally_point.persistent) {
2975 }
2976
2977 /* Done building this unit; time to move on to the next. */
2978 choose_build_target(pplayer, pcity);
2979 }
2980 } /* if */
2981
2982 return city_exist(saved_city_id);
2983}
2984
2985/**********************************************************************/
2988static bool city_build_stuff(struct player *pplayer, struct city *pcity)
2989{
2990 if (!city_distribute_surplus_shields(pplayer, pcity)) {
2991 return FALSE;
2992 }
2993
2996
2997 switch (pcity->production.kind) {
2998 case VUT_IMPROVEMENT:
2999 return city_build_building(pplayer, pcity);
3000 case VUT_UTYPE:
3001 return city_build_unit(pplayer, pcity);
3002 default:
3003 /* must never happen! */
3005 break;
3006 };
3007 return FALSE;
3008}
3009
3010/**********************************************************************/
3020static bool sell_random_building(struct player *pplayer,
3021 struct cityimpr_list *imprs)
3022{
3023 struct cityimpr *pcityimpr;
3024 int r;
3025
3026 fc_assert_ret_val(pplayer != NULL, FALSE);
3027
3028 if (!imprs || cityimpr_list_size(imprs) == 0) {
3029 return FALSE;
3030 }
3031
3034
3036 ftc_server,
3037 _("Can't afford to maintain %s in %s, building sold!"),
3039 city_link(pcityimpr->pcity));
3040 log_debug("%s: sold building (%s)", player_name(pplayer),
3042
3043 do_sell_building(pplayer, pcityimpr->pcity, pcityimpr->pimprove, "cant_maintain");
3044
3046
3047 /* Get back the gold upkeep that was already paid this turn. */
3049 pcityimpr->pimprove);
3050
3052
3054
3055 return TRUE;
3056}
3057
3058/**********************************************************************/
3066static void uk_rem_gold_callback(struct unit *punit)
3067{
3068 int gold_upkeep;
3069
3070 /* Remove the unit from uk_rem_gold. */
3072
3073 gold_upkeep = punit->server.upkeep_paid[O_GOLD];
3074
3075 /* All units in uk_rem_gold should have gold upkeep! */
3076 fc_assert_ret_msg(gold_upkeep > 0, "%s has %d gold upkeep",
3077 unit_rule_name(punit), gold_upkeep);
3078
3079 /* Get the upkeep gold back. */
3080 unit_owner(punit)->economic.gold += gold_upkeep;
3081}
3082
3083/**********************************************************************/
3087static void uk_rem_gold_append(struct unit *punit)
3088{
3089 /* Make the unit aware that it is on the uk_rem_gold list. */
3091
3092 /* Add the unit to the list. */
3094}
3095
3096/**********************************************************************/
3100static void unit_list_referred_destroy(struct unit_list *punitlist)
3101{
3103 /* Clear the unit's knowledge of the list. */
3106
3107 /* Destroy the list it self. */
3109}
3110
3111/**********************************************************************/
3123static struct unit *sell_random_unit(struct player *pplayer,
3124 struct unit_list *punitlist)
3125{
3126 struct unit *punit;
3127 int r;
3128 struct unit_list *cargo;
3129
3130 fc_assert_ret_val(pplayer != NULL, NULL);
3131
3132 if (!punitlist || unit_list_size(punitlist) == 0) {
3133 return NULL;
3134 }
3135
3138
3139 cargo = unit_list_new();
3140
3141 /* Check if unit is transporting other units from punitlist,
3142 * and sell one of those (recursively) instead.
3143 * Note that in case of recursive transports we have to iterate
3144 * also through those middle transports that themselves are not in
3145 * punitlist. */
3147 /* Optimization, do not iterate over punitlist
3148 * if we are sure that pcargo is not in it. */
3149 if (pcargo->server.upkeep_paid[O_GOLD] > 0) {
3151 if (pcargo == p2) {
3152 unit_list_append(cargo, pcargo);
3153 }
3155 }
3157
3158 if (unit_list_size(cargo) > 0) {
3159 /* Recursively sell. Note that cargo list has both
3160 * leaf units and middle transports in case of
3161 * recursive transports. */
3162 struct unit *ret = sell_random_unit(pplayer, cargo);
3163
3164 unit_list_destroy(cargo);
3165
3167
3168 return ret;
3169 }
3170
3171 unit_list_destroy(cargo);
3172
3173 {
3174 const char *punit_link = unit_tile_link(punit);
3175#ifdef FREECIV_DEBUG
3176 const char *punit_logname = unit_rule_name(punit);
3177#endif /* FREECIV_DEBUG */
3178 struct tile *utile = unit_tile(punit);
3179
3183
3184 /* The gold was paid back when the unit removal made
3185 * uk_rem_gold_callback() run as the unit's removal call back. */
3186
3187 notify_player(pplayer, utile, E_UNIT_LOST_MISC, ftc_server,
3188 _("Not enough gold. %s disbanded."),
3189 punit_link);
3190 log_debug("%s: unit sold (%s)", player_name(pplayer),
3192 } else {
3193 /* Not able to get rid of punit */
3194 return NULL;
3195 }
3196 }
3197
3199
3200 return punit;
3201}
3202
3203/**********************************************************************/
3207{
3208 struct cityimpr_list *pimprlist;
3209 bool sell_unit = TRUE;
3210
3211 if (!pplayer) {
3212 return FALSE;
3213 }
3214
3217
3218 city_list_iterate(pplayer->cities, pcity) {
3219 city_built_iterate(pcity, pimprove) {
3220 if (can_city_sell_building(pcity, pimprove)) {
3221 struct cityimpr *ci = fc_malloc(sizeof(*ci));
3222
3223 ci->pcity = pcity;
3224 ci->pimprove = pimprove;
3226 }
3229
3230 unit_list_iterate(pplayer->units, punit) {
3231 if (punit->server.upkeep_paid[O_GOLD] > 0) {
3233 }
3235
3236 while (pplayer->economic.gold < 0
3238 || unit_list_size(uk_rem_gold) > 0)) {
3240 || unit_list_size(uk_rem_gold) == 0) {
3242 } else {
3243 sell_random_unit(pplayer, uk_rem_gold);
3244 }
3246 }
3247
3248 /* Free remaining entries from list */
3250 FC_FREE(pimpr);
3252
3253 if (pplayer->economic.gold < 0) {
3254 /* If we get here it means the player has
3255 * negative gold. This should never happen. */
3256 fc_assert_msg(FALSE, "Player %s (nb %d) cannot have negative gold!",
3257 player_name(pplayer), player_number(pplayer));
3258 }
3259
3262
3263 return pplayer->economic.gold >= 0;
3264}
3265
3266/**********************************************************************/
3270{
3271 if (!pplayer) {
3272 return FALSE;
3273 }
3274
3276
3277 unit_list_iterate(pplayer->units, punit) {
3278 if (punit->server.upkeep_paid[O_GOLD] > 0) {
3280 }
3282
3283 while (pplayer->economic.gold < 0
3284 && sell_random_unit(pplayer, uk_rem_gold)) {
3285 /* All done in sell_random_unit() */
3286 }
3287
3288 if (pplayer->economic.gold < 0) {
3289 /* If we get here it means the player has
3290 * negative gold. This should never happen. */
3291 fc_assert_msg(FALSE, "Player %s (nb %d) cannot have negative gold!",
3292 player_name(pplayer), player_number(pplayer));
3293 }
3294
3296
3297 return pplayer->economic.gold >= 0;
3298}
3299
3300/**********************************************************************/
3304{
3305 struct player *pplayer;
3306 struct cityimpr_list *pimprlist;
3307
3308 if (!pcity) {
3309 return TRUE;
3310 }
3311
3312 pplayer = city_owner(pcity);
3314
3315 /* Create a vector of all buildings that can be sold. */
3316 city_built_iterate(pcity, pimprove) {
3317 if (can_city_sell_building(pcity, pimprove)) {
3318 struct cityimpr *ci = fc_malloc(sizeof(*ci));
3319
3320 ci->pcity = pcity;
3321 ci->pimprove = pimprove;
3323 }
3325
3326 /* Try to sell some buildings. */
3327 while (pplayer->economic.gold < 0
3328 && sell_random_building(pplayer, pimprlist)) {
3329 /* all done in sell_random_building */
3330 }
3331
3332 /* Free remaining entries from list */
3334 FC_FREE(pimpr);
3336
3338
3339 return pplayer->economic.gold >= 0;
3340}
3341
3342/**********************************************************************/
3351{
3352 struct player *pplayer;
3353
3354 if (!pcity) {
3355 return TRUE;
3356 }
3357
3358 pplayer = city_owner(pcity);
3360
3361 /* Create a vector of all supported units with gold upkeep. */
3363 if (punit->server.upkeep_paid[O_GOLD] > 0) {
3365 }
3367
3368 /* Still not enough gold, so try "selling" some units. */
3369 while (pplayer->economic.gold < 0
3370 && sell_random_unit(pplayer, uk_rem_gold)) {
3371 /* All done in sell_random_unit() */
3372 }
3373
3374 /* If we get here the player has negative gold, but hopefully
3375 * another city will be able to pay the deficit, so continue. */
3376
3378
3379 return pplayer->economic.gold >= 0;
3380}
3381
3382/**********************************************************************/
3385static bool place_pollution(struct city *pcity, enum extra_cause cause)
3386{
3387 struct tile *ptile;
3388 struct tile *pcenter = city_tile(pcity);
3389 int city_radius_sq = city_map_radius_sq_get(pcity);
3390 int k = 100;
3391 const struct civ_map *nmap = &(wld.map);
3392
3393 while (k > 0) {
3394 /* Place pollution on a random city tile */
3395 int cx, cy;
3396 int tile_id = fc_rand(city_map_tiles(city_radius_sq));
3397 struct extra_type *pextra;
3398
3399 city_tile_index_to_xy(&cx, &cy, tile_id, city_radius_sq);
3400
3401 /* Check for a real map position */
3402 if (!(ptile = city_map_to_tile(nmap, pcenter, city_radius_sq, cx, cy))) {
3403 continue;
3404 }
3405
3406 pextra = rand_extra_for_tile(ptile, cause, FALSE);
3407
3408 if (pextra != NULL && !tile_has_extra(ptile, pextra)) {
3409 tile_add_extra(ptile, pextra);
3410 update_tile_knowledge(ptile);
3411
3412 return TRUE;
3413 }
3414 k--;
3415 }
3416 log_debug("pollution not placed: city: %s", city_name_get(pcity));
3417
3418 return FALSE;
3419}
3420
3421/**********************************************************************/
3424static void check_pollution(struct city *pcity)
3425{
3426 if (fc_rand(100) < pcity->pollution) {
3427 if (place_pollution(pcity, EC_POLLUTION)) {
3429 _("Pollution near %s."), city_link(pcity));
3430 }
3431 }
3432}
3433
3434/**********************************************************************/
3441int city_incite_cost(struct player *pplayer, struct city *pcity)
3442{
3443 int dist, size;
3444 double cost; /* Intermediate values can get very large */
3445
3446 /* Gold factor */
3447 cost = city_owner(pcity)->economic.gold + game.server.base_incite_cost;
3448
3449 unit_list_iterate(pcity->tile->units, punit) {
3453
3454 /* Buildings */
3455 city_built_iterate(pcity, pimprove) {
3456 cost += impr_build_shield_cost(pcity, pimprove)
3459
3460 /* Stability bonuses */
3461 if (!city_unhappy(pcity)) {
3462 cost *= 2;
3463 }
3464 if (city_celebrating(pcity)) {
3465 cost *= 2;
3466 }
3467
3468 /* Buy back is cheap, conquered cities are also cheap */
3470 if (city_owner(pcity) != pcity->original) {
3471 if (pplayer == pcity->original) {
3472 cost /= 2; /* Buy back: 50% price reduction */
3473 } else {
3474 cost = cost * 2 / 3; /* Buy conquered: 33% price reduction */
3475 }
3476 }
3477 }
3478
3479 /* Distance from capital */
3480 /* Max penalty. Applied if there is no capital, or it's even further away. */
3481 dist = 32;
3482 city_list_iterate(city_owner(pcity)->cities, capital) {
3483 if (is_capital(capital)) {
3484 int tmp = map_distance(capital->tile, pcity->tile);
3485
3486 if (tmp < dist) {
3487 dist = tmp;
3488 }
3489 }
3491
3492 size = MAX(1, city_size_get(pcity)
3495 - pcity->feel[CITIZEN_ANGRY][FEELING_FINAL] * 3);
3496 cost *= size;
3498 cost = cost / (dist + 3);
3499
3501 int cost_per_citizen = cost / pcity->size;
3502 int natives = citizens_nation_get(pcity, city_owner(pcity)->slot);
3503 int tgt_cit = citizens_nation_get(pcity, pplayer->slot);
3504 int third_party = pcity->size - natives - tgt_cit;
3505
3506 cost = cost_per_citizen * (natives + 0.7 * third_party + 0.5 * tgt_cit);
3507 }
3508
3509 cost += (cost * get_city_bonus(pcity, EFT_INCITE_COST_PCT)) / 100;
3510 cost /= 100;
3511
3514 } else {
3515 return cost;
3516 }
3517}
3518
3519/**********************************************************************/
3522static void define_orig_production_values(struct city *pcity)
3523{
3524 /* Remember what this city is building last turn, so that on the next turn
3525 * the player can switch production to something else and then change it
3526 * back without penalty. This has to be updated _before_ production for
3527 * this turn is calculated, so that the penalty will apply if the player
3528 * changes production away from what has just been completed. This makes
3529 * sense if you consider what this value means: all the shields in the
3530 * city have been dedicated toward the project that was chosen last turn,
3531 * so the player shouldn't be penalized if the governor has to pick
3532 * something different. See city_change_production_penalty(). */
3533 pcity->changed_from = pcity->production;
3534
3535 log_debug("In %s, building %s. Beg of Turn shields = %d",
3537 pcity->before_change_shields);
3538}
3539
3540/**********************************************************************/
3543static void nullify_caravan_and_disband_plus(struct city *pcity)
3544{
3545 pcity->disbanded_shields = 0;
3546 pcity->caravan_shields = 0;
3547}
3548
3549/**********************************************************************/
3554{
3556 pcity->before_change_shields = 0;
3557}
3558
3559/**********************************************************************/
3562static void update_city_activity(struct city *pcity)
3563{
3564 struct player *pplayer;
3565 struct government *gov;
3566 bool is_happy;
3567 bool is_celebrating;
3568
3569 if (!pcity) {
3570 return;
3571 }
3572
3573 pplayer = city_owner(pcity);
3574 gov = government_of_city(pcity);
3575 is_happy = city_happy(pcity);
3577
3578 if (city_refresh(pcity)) {
3579 auto_arrange_workers(pcity);
3580 }
3581
3582 /* Reporting of celebrations rewritten, copying the treatment of disorder below,
3583 with the added rapture rounds count. 991219 -- Jing */
3584 if (city_build_stuff(pplayer, pcity)) {
3585 int saved_id;
3586 int revolution_turns;
3587
3588 pcity->history += city_history_gain(pcity);
3589
3590 /* History can decrease, but never go below zero */
3591 pcity->history = MAX(pcity->history, 0);
3592
3593 /* Keep old behavior when building new improvement could keep
3594 city celebrating */
3595 if (!is_happy) {
3596 is_happy = city_happy(pcity);
3597 }
3598
3599 if (city_celebrating(pcity) || is_celebrating) {
3600 pcity->rapture++;
3601
3602 /* Update city's celebrating counters */
3604 if (pcount->type == CB_CITY_CELEBRATION_TURNS) {
3605 pcity->counter_values[pcount->index]++;
3606 }
3608
3609 if (pcity->rapture == 1) {
3611 _("Celebrations in your honor in %s."),
3612 city_link(pcity));
3613 }
3614 } else {
3615 if (pcity->rapture != 0) {
3617 _("Celebrations canceled in %s."),
3618 city_link(pcity));
3619 }
3620
3621 /* Update city's celebrating counters */
3623 if (pcount->type == CB_CITY_CELEBRATION_TURNS) {
3624 pcity->counter_values[pcount->index] = 0;
3625 }
3627 pcity->rapture = 0;
3628 }
3629 pcity->was_happy = is_happy;
3630
3631 /* Handle the illness. */
3632 if (game.info.illness_on) {
3633 /* Recalculate city illness; illness due to trade has to be saved
3634 * within the city struct, as the client does not have all
3635 * the data to calculate it */
3636 pcity->server.illness
3637 = city_illness_calc(pcity, NULL, NULL, &(pcity->illness_trade), NULL);
3638
3639 if (city_illness_check(pcity)) {
3640 if (!city_illness_strike(pcity)) {
3641 /* Illness destroyed the city */
3642 return;
3643 }
3644 }
3645 }
3646
3647 /* City population updated here, after the rapture stuff above. --Jing */
3648 saved_id = pcity->id;
3649 pcity->had_famine = FALSE;
3650 city_populate(pcity, pplayer);
3651 if (NULL == player_city_by_number(pplayer, saved_id)) {
3652 return;
3653 }
3654
3655 pcity->did_sell = FALSE;
3656 pcity->did_buy = FALSE;
3657 pcity->airlift = city_airlift_max(pcity);
3658 update_bulbs(pplayer, pcity->prod[O_SCIENCE], FALSE, FALSE);
3659
3661
3662 /* Update the treasury. */
3663 pplayer->economic.gold += pcity->surplus[O_GOLD];
3664
3665 /* FIXME: Nation level upkeep should be paid after ALL cities
3666 * have been processed, not after each individual city. */
3668 /* Unit upkeep was not included in city balance ->
3669 * not reduced from the city surplus. */
3670 pplayer->economic.gold -= city_total_unit_gold_upkeep(pcity);
3671
3673 /* Building upkeep was not included in city balance ->
3674 * not reduced from the city surplus. */
3675 pplayer->economic.gold -= city_total_impr_gold_upkeep(pcity);
3676 }
3677 }
3678
3679 /* Remember how much gold upkeep each unit was paid. */
3683
3684 if (pplayer->economic.gold < 0) {
3685 /* Not enough gold - we have to sell some buildings, and if that
3686 * is not enough, disband units with gold upkeep, taking into
3687 * account the setting of 'game.info.gold_upkeep_style':
3688 * GOLD_UPKEEP_CITY: Cities pay for buildings and units.
3689 * GOLD_UPKEEP_MIXED: Cities pay only for buildings; the nation pays
3690 * for units.
3691 * GOLD_UPKEEP_NATION: The nation pays for buildings and units. */
3692 switch (game.info.gold_upkeep_style) {
3693 case GOLD_UPKEEP_CITY:
3694 case GOLD_UPKEEP_MIXED:
3698 }
3699 break;
3700 case GOLD_UPKEEP_NATION:
3701 break;
3702 }
3703 }
3704
3706 if (city_unhappy(pcity)) {
3707 const char *revomsg;
3708
3709 pcity->anarchy++;
3710
3712 if (pcount->type == CB_CITY_DISORDER_TURNS) {
3713 pcity->counter_values[pcount->index]++;
3714 }
3716
3717 if (pcity->anarchy == revolution_turns) {
3718 /* Revolution next turn if not dealt with */
3719 /* TRANS: preserve leading space; this string will be appended to
3720 * another sentence */
3721 revomsg = _(" Unrest threatens to spread beyond the city.");
3722 } else {
3723 revomsg = "";
3724 }
3725 if (pcity->anarchy == 1) {
3727 /* TRANS: second %s is an optional extra sentence */
3728 _("Civil disorder in %s.%s"),
3729 city_link(pcity), revomsg);
3730 } else {
3732 /* TRANS: second %s is an optional extra sentence */
3733 _("CIVIL DISORDER CONTINUES in %s.%s"),
3734 city_link(pcity), revomsg);
3735 }
3736 } else {
3737 if (pcity->anarchy != 0) {
3739 _("Order restored in %s."),
3740 city_link(pcity));
3741 }
3742 pcity->anarchy = 0;
3743
3745 if (pcount->type == CB_CITY_DISORDER_TURNS) {
3746 pcity->counter_values[pcount->index] = 0;
3747 }
3749 }
3750 check_pollution(pcity);
3751
3752 send_city_info(NULL, pcity);
3753
3754 if (revolution_turns > 0 && pcity->anarchy > revolution_turns) {
3755 notify_player(pplayer, city_tile(pcity), E_ANARCHY, ftc_server,
3756 /* TRANS: %s - government form, e.g., Democracy */
3757 _("The people have overthrown your %s, "
3758 "your country is in turmoil."),
3761 }
3762 if (city_refresh(pcity)) {
3763 auto_arrange_workers(pcity);
3764 }
3765 sanity_check_city(pcity);
3766 }
3767}
3768
3769/**********************************************************************/
3772static bool city_illness_check(const struct city * pcity)
3773{
3774 if (fc_rand(1000) < pcity->server.illness) {
3775 return TRUE;
3776 }
3777
3778 return FALSE;
3779}
3780
3781/**********************************************************************/
3785static bool disband_city(struct city *pcity)
3786{
3787 struct player *pplayer = city_owner(pcity);
3788 struct tile *ptile = pcity->tile;
3789 struct city *rcity = NULL;
3790 const struct unit_type *utype = pcity->production.value.utype;
3791 struct unit *punit;
3792 int saved_id = pcity->id;
3793
3794 /* find closest city other than pcity */
3795 rcity = find_closest_city(ptile, pcity, pplayer, FALSE, FALSE, FALSE, TRUE,
3796 FALSE, NULL);
3797
3798 if (!rcity) {
3799 /* What should we do when we try to disband our only city? */
3800 notify_player(pplayer, ptile, E_CITY_CANTBUILD, ftc_server,
3801 _("%s can't build %s yet, "
3802 "as we can't disband our only city."),
3804 script_server_signal_emit("unit_cant_be_built", utype, pcity,
3805 "pop_cost");
3806 if (!city_exist(saved_id)) {
3807 /* Script decided to remove even the last city */
3808 return TRUE;
3809 } else {
3810 return FALSE;
3811 }
3812 }
3813
3814 punit = city_create_unit(pcity, utype, NULL);
3815
3816 /* "unit_built" script handler may have destroyed city. If so, we
3817 * assume something sensible happened to its units, and that the
3818 * script took care of announcing unit creation if required. */
3819 if (city_exist(saved_id)) {
3820 /* Shift all the units supported by pcity (including the new unit)
3821 * to rcity. transfer_city_units does not make sure no units are
3822 * left floating without a transport, but since all units are
3823 * transferred this is not a problem. */
3824 transfer_city_units(pplayer, pplayer, pcity->units_supported, rcity,
3825 pcity, -1, TRUE);
3826
3827 if (punit) {
3828 notify_player(pplayer, ptile, E_UNIT_BUILT, ftc_server,
3829 /* TRANS: "<city> is disbanded into Settler." */
3830 _("%s is disbanded into %s."),
3832 }
3833
3834 script_server_signal_emit("city_destroyed", pcity, pcity->owner, NULL);
3835
3836 if (!city_exist(saved_id)) {
3837 /* Already removed during the script */
3838 return TRUE;
3839 }
3840 remove_city(pcity);
3841
3842 /* Since we've removed the city, we don't need to worry about
3843 * charging for production, disabling rally points, etc. */
3844 }
3845
3846 return TRUE;
3847}
3848
3849/**********************************************************************/
3897static float city_migration_score(struct city *pcity)
3898{
3899 float score = 0.0;
3900 int build_shield_cost = 0;
3901 bool has_wonder = FALSE;
3902
3903 if (!pcity) {
3904 return score;
3905 }
3906
3907 if (pcity->server.mgr_score_calc_turn == game.info.turn) {
3908 /* up-to-date migration score */
3909 return pcity->server.migration_score;
3910 }
3911
3912 /* feeling of the citizens */
3913 score = (city_size_get(pcity)
3914 + 1.00 * pcity->feel[CITIZEN_HAPPY][FEELING_FINAL]
3915 + 0.00 * pcity->feel[CITIZEN_CONTENT][FEELING_FINAL]
3916 - 0.25 * pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL]
3917 - 0.50 * pcity->feel[CITIZEN_ANGRY][FEELING_FINAL]);
3918
3919 /* calculate shield build cost for all buildings */
3920 city_built_iterate(pcity, pimprove) {
3921 build_shield_cost += impr_build_shield_cost(pcity, pimprove);
3922 if (is_wonder(pimprove)) {
3923 /* this city has a wonder */
3924 has_wonder = TRUE;
3925 }
3927
3928 /* take shield costs of all buidings into account; normalized by 1000 */
3929 score *= (1 + (1 - exp(- (float) MAX(0, build_shield_cost) / 1000)) / 5);
3930 /* take trade into account; normalized by 100 */
3931 score *= (1 + (1 - exp(- (float) MAX(0, pcity->surplus[O_TRADE]) / 100))
3932 / 5);
3933 /* take luxury into account; normalized by 100 */
3934 score *= (1 + (1 - exp(- (float) MAX(0, pcity->surplus[O_LUXURY]) / 100))
3935 / 5);
3936 /* take science into account; normalized by 100 */
3937 score *= (1 + (1 - exp(- (float) MAX(0, pcity->surplus[O_SCIENCE]) / 100))
3938 / 5);
3939
3940 score += city_culture(pcity) * game.info.culture_migration_pml / 1000;
3941
3942 /* Take food into account; the food surplus is clipped to values between
3943 * -10..20 and normalize by 10. Thus, the factor is between 0.9 and 1.2. */
3944 score *= (1 + (float) CLIP(-10, pcity->surplus[O_FOOD], 20) / 10 );
3945
3946 /* Reduce the score due to city illness (plague). The illness is given in
3947 * tenth of percent (0..1000) and normalized by 25. Thus, this factor is
3948 * between 0.6 (ill city) and 1.0 (health city). */
3949 score *= (100 - (float)city_illness_calc(pcity, NULL, NULL, NULL, NULL)
3950 / 25);
3951
3952 if (has_wonder) {
3953 /* people like wonders */
3954 score *= 1.25;
3955 }
3956
3957 if (is_capital(pcity)) {
3958 /* the capital is a magnet for the citizens */
3959 score *= 1.25;
3960 }
3961
3962 /* take into account effects */
3963 score *= (1.0 + get_city_bonus(pcity, EFT_MIGRATION_PCT) / 100.0);
3964
3965 log_debug("[M] %s score: %.3f", city_name_get(pcity), score);
3966
3967 /* set migration score for the city */
3968 pcity->server.migration_score = score;
3969 /* set the turn, when the score was calculated */
3971
3972 return score;
3973}
3974
3975/**********************************************************************/
3982 struct city *pcity_to)
3983{
3985 struct tile *ptile_from, *ptile_to;
3987 const char *nation_from, *nation_to;
3988 struct city *rcity = NULL;
3989 int to_id = pcity_to->id;
3990 const struct civ_map *nmap = &(wld.map);
3991
3992 if (!pcity_from || !pcity_to) {
3993 return FALSE;
3994 }
3995
3999 /* We copy that, because city_link always returns the same pointer. */
4006
4007 /* Check food supply in the receiver city */
4009 bool migration = FALSE;
4010
4011 if (pcity_to->surplus[O_FOOD] >= game.info.food_cost) {
4012 migration = TRUE;
4013 } else {
4014 /* Check if there is a free tile for the new citizen which, when worked,
4015 * leads to zero or positive food surplus for the enlarged city */
4016 int max_food_tile = -1; /* no free tile */
4017
4019 city_tile(pcity_to), ptile) {
4020 if (city_can_work_tile(pcity_to, ptile)
4021 && tile_worked(ptile) != pcity_to) {
4022 /* Safest assumption is that city won't be celebrating once an
4023 * additional citizen is added */
4026 }
4028 if (max_food_tile >= 0
4029 && pcity_to->surplus[O_FOOD] + max_food_tile >= game.info.food_cost) {
4030 migration = TRUE;
4031 }
4032 }
4033
4034 if (!migration) {
4035 /* insufficiency food in receiver city; no additional citizens */
4036 if (pplayer_from == pplayer_to) {
4037 /* migration between one nation */
4039 /* TRANS: From <city1> to <city2>. */
4040 _("Migrants from %s can't go to %s because there is "
4041 "not enough food available!"),
4043 } else {
4044 /* migration between different nations */
4046 /* TRANS: From <city1> to <city2> (<city2 nation adjective>). */
4047 _("Migrants from %s can't go to %s (%s) because there "
4048 "is not enough food available!"),
4051 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
4052 _("Migrants from %s (%s) can't go to %s because there "
4053 "is not enough food available!"),
4055 }
4056
4057 return FALSE;
4058 }
4059 }
4060
4062 /* receiver city can't grow */
4063 if (pplayer_from == pplayer_to) {
4064 /* migration between one nation */
4066 /* TRANS: From <city1> to <city2>. */
4067 _("Migrants from %s can't go to %s because it needs "
4068 "an improvement to grow!"),
4070 } else {
4071 /* migration between different nations */
4073 /* TRANS: From <city1> to <city2> of <city2 nation adjective>. */
4074 _("Migrants from %s can't go to %s (%s) because it "
4075 "needs an improvement to grow!"),
4078 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
4079 _("Migrants from %s (%s) can't go to %s because it "
4080 "needs an improvement to grow!"),
4082 }
4083
4084 return FALSE;
4085 }
4086
4087 /* reduce size of giver */
4088 if (city_size_get(pcity_from) == 1) {
4089
4091 /* Preserve nationality of city's only citizen */
4093 }
4094
4095 /* do not destroy wonders */
4096 city_built_iterate(pcity_from, pimprove) {
4097 if (is_wonder(pimprove)) {
4098 return FALSE;
4099 }
4101
4102 /* find closest city other of the same player than pcity_from */
4104 FALSE, FALSE, TRUE, FALSE, NULL);
4105
4106 if (rcity) {
4107 int id = pcity_from->id;
4108
4109 /* transfer all units to the closest city */
4111 pcity_from->units_supported, rcity, pcity_from,
4112 -1, TRUE);
4114
4115 script_server_signal_emit("city_size_change", pcity_from,
4116 (lua_Integer)(-1), "migration_from");
4117
4118 if (city_exist(id)) {
4119 script_server_signal_emit("city_destroyed", pcity_from,
4120 pcity_from->owner, NULL);
4121
4122 if (city_exist(id)) {
4124 }
4125 }
4126
4128 _("%s was disbanded by its citizens."),
4129 name_from);
4130 } else {
4131 /* it's the only city of the nation */
4132 return FALSE;
4133 }
4134 } else {
4135 /* the migrants take half of the food box with them (this prevents
4136 * migration -> grow -> migration -> ... cycles) */
4137 pcity_from->food_stock /= 2;
4138
4140 /* Those citizens that are from the target nation are most
4141 * ones migrating. */
4142 if (citizens_nation_get(pcity_from, pplayer_to->slot) > 0) {
4144 } else if (!citizens_nation_get(pcity_from, pplayer_citizen->slot)) {
4145 /* No native citizens at all in the city, choose random foreigner */
4147
4149 }
4150 /* This should be followed by city_reduce_size(). */
4152 }
4153 city_reduce_size(pcity_from, 1, pplayer_from, "migration_from");
4155 if (city_refresh(pcity_from)) {
4157 }
4158 }
4159
4160 /* This should be _before_ the size of the city is increased. Thus, the
4161 * order of the messages is correct (1: migration; 2: increased size). */
4162 if (pplayer_from == pplayer_to) {
4163 /* migration between one nation */
4165 /* TRANS: From <city1> to <city2>. */
4166 _("Migrants from %s moved to %s in search of a better "
4167 "life."), name_from, name_to);
4168 } else {
4169 /* migration between different nations */
4171 /* TRANS: From <city1> to <city2> (<city2 nation adjective>). */
4172 _("Migrants from %s moved to %s (%s) in search of a "
4173 "better life."),
4176 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
4177 _("Migrants from %s (%s) moved to %s in search of a "
4178 "better life."),
4180 }
4181
4182 /* Increase size of receiver city */
4183 if (city_exist(to_id)) {
4185
4186 if (city_exist(to_id)) {
4189 if (city_refresh(pcity_to)) {
4191 }
4192 if (incr_success) {
4193 script_server_signal_emit("city_size_change", pcity_to,
4194 (lua_Integer)1, "migration_to");
4195 }
4196 }
4197 }
4198
4199 log_debug("[M] T%d migration successful (%s -> %s)",
4201
4202 return TRUE;
4203}
4204
4205/**********************************************************************/
4227{
4228 bool internat = FALSE;
4229
4230 if (!game.server.migration) {
4231 return FALSE;
4232 }
4233
4235 || (game.server.mgr_worldchance <= 0
4236 && game.server.mgr_nationchance <= 0)) {
4237 return FALSE;
4238 }
4239
4240 /* check for migration */
4241 players_iterate(pplayer) {
4242 if (!pplayer->cities) {
4243 continue;
4244 }
4245
4246 if (check_city_migrations_player(pplayer)) {
4247 internat = TRUE;
4248 }
4250
4251 return internat;
4252}
4253
4254/**********************************************************************/
4258bool city_empty_food_stock(struct city *pcity) {
4259 struct player *pplayer = city_owner(pcity);
4260 struct tile *ptile = city_tile(pcity);
4261
4262 fc_assert_ret_val(pcity != NULL, FALSE);
4263
4264 if (pcity->food_stock > 0) {
4265 pcity->food_stock = 0;
4266
4267 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4268 /* TRANS: %s is a city name */
4269 _("All stored food destroyed in %s."), city_link(pcity));
4270
4271 return TRUE;
4272 }
4273
4274 return FALSE;
4275}
4276
4277/**********************************************************************/
4280static void apply_disaster(struct city *pcity, struct disaster_type *pdis)
4281{
4282 struct player *pplayer = city_owner(pcity);
4283 struct tile *ptile = city_tile(pcity);
4285
4286 log_debug("%s at %s", disaster_rule_name(pdis), city_name_get(pcity));
4287
4288 notify_player(pplayer, ptile, E_DISASTER,
4289 ftc_server,
4290 /* TRANS: Disasters such as Earthquake */
4291 _("%s was hit by %s."), city_name_get(pcity),
4293
4295 if (place_pollution(pcity, EC_POLLUTION)) {
4296 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4297 _("Pollution near %s."), city_link(pcity));
4299 }
4300 }
4301
4303 if (place_pollution(pcity, EC_FALLOUT)) {
4304 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4305 _("Fallout near %s."), city_link(pcity));
4307 }
4308 }
4309
4312 && pcity->size > 1)) {
4313 if (!city_reduce_size(pcity, 1, NULL, "disaster")) {
4314 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4315 /* TRANS: "Industrial Accident destroys Bogota entirely." */
4316 _("%s destroys %s entirely."),
4318 pcity = NULL;
4319 } else {
4320 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4321 /* TRANS: "Nuclear Accident ... Montreal." */
4322 _("%s causes population loss in %s."),
4324 }
4325
4327 }
4328
4330 int total = 0;
4331 struct impr_type *imprs[B_LAST];
4332
4333 city_built_iterate(pcity, pimprove) {
4334 if (is_improvement(pimprove)
4335 && !improvement_has_flag(pimprove, IF_DISASTER_PROOF)) {
4336 imprs[total++] = pimprove;
4337 }
4339
4340 if (total > 0) {
4341 int num = fc_rand(total);
4342
4343 building_lost(pcity, imprs[num], "disaster", NULL);
4344
4345 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4346 /* TRANS: second %s is the name of a city improvement */
4347 _("%s destroys %s in %s."),
4350 city_link(pcity));
4351
4353 }
4354 }
4355
4357 if (city_empty_food_stock(pcity)) {
4359 }
4360 }
4361
4363 if (pcity->shield_stock > 0) {
4364 char prod[256];
4365
4366 pcity->shield_stock = 0;
4367 nullify_prechange_production(pcity); /* Make it impossible to recover */
4368
4369 universal_name_translation(&pcity->production, prod, sizeof(prod));
4370 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4371 /* TRANS: "Production of Colossus in Rhodes destroyed." */
4372 _("Production of %s in %s destroyed."),
4373 prod, city_link(pcity));
4374
4376 }
4377 }
4378
4379 script_server_signal_emit("disaster_occurred", pdis, pcity,
4381 script_server_signal_emit("disaster", pdis, pcity);
4382}
4383
4384/**********************************************************************/
4388{
4389 if (game.info.disasters == 0) {
4390 /* Shortcut out as no disaster is possible. */
4391 return;
4392 }
4393
4394 players_iterate(pplayer) {
4395 /* Safe city iterator needed as disaster may destroy city */
4396 city_list_iterate_safe(pplayer->cities, pcity) {
4397 int id = pcity->id;
4398
4400 if (city_exist(id)) {
4401 /* City survived earlier disasters. */
4402 int probability = game.info.disasters * pdis->frequency;
4403 int result = fc_rand(DISASTER_BASE_RARITY);
4404
4405 if (result < probability) {
4406 if (can_disaster_happen(pdis, pcity)) {
4407 apply_disaster(pcity, pdis);
4408 }
4409 }
4410 }
4414}
4415
4416/**********************************************************************/
4424static bool check_city_migrations_player(const struct player *pplayer)
4425{
4429 float score_from, score_tmp, weight;
4430 int dist, mgr_dist;
4431 bool internat = FALSE;
4432
4433 /* check for each city
4434 * city_list_iterate_safe_end must be used because we could
4435 * remove one city from the list */
4436 city_list_iterate_safe(pplayer->cities, pcity) {
4437 /* no migration out of the capital */
4438 if (is_capital(pcity)) {
4439 continue;
4440 }
4441
4442 /* check only each (game.server.mgr_turninterval) turn
4443 * (counted from the funding turn) and do not migrate
4444 * the same turn a city is founded */
4445 if (game.info.turn == pcity->turn_founded
4446 || ((game.info.turn - pcity->turn_founded)
4447 % game.server.mgr_turninterval) != 0) {
4448 continue;
4449 }
4450
4455
4456 /* score of the actual city
4457 * taking into account a persistence factor of 3 */
4458 score_from = city_migration_score(pcity) * 3;
4459
4460 log_debug("[M] T%d check city: %s score: %6.3f (%s)",
4462 player_name(pplayer));
4463
4464 /* consider all cities within the maximal possible distance
4465 * (= CITY_MAP_MAX_RADIUS + GAME_MAX_MGR_DISTANCE) */
4466 iterate_outward(&(wld.map), city_tile(pcity),
4468 acity = tile_city(ptile);
4469
4470 if (!acity || acity == pcity) {
4471 /* no city or the city in the center */
4472 continue;
4473 }
4474
4475 /* Calculate the migration distance. The value of
4476 * game.server.mgr_distance is added to the current city radius. If the
4477 * distance between both cities is lower or equal than this value,
4478 * migration is possible. */
4481
4482 /* distance between the two cities */
4483 dist = real_map_distance(city_tile(pcity), city_tile(acity));
4484
4485 if (dist > mgr_dist) {
4486 /* to far away */
4487 continue;
4488 }
4489
4490 /* score of the second city, weighted by the distance */
4491 weight = ((float) (mgr_dist + 1 - dist) / (float) (mgr_dist + 1));
4493
4494 log_debug("[M] T%d - compare city: %s (%s) dist: %d mgr_dist: %d "
4495 "score: %6.3f", game.info.turn, city_name_get(acity),
4497
4498 if (game.server.mgr_nationchance > 0 && city_owner(acity) == pplayer) {
4499 /* migration between cities of the same owner */
4501 /* select the best! */
4504
4505 log_debug("[M] T%d - best city (player): %s (%s) score: "
4506 "%6.3f (> %6.3f)", game.info.turn,
4509 }
4510 } else if (game.server.mgr_worldchance > 0
4511 && city_owner(acity) != pplayer) {
4512 /* migration between cities of different owners */
4514 /* Modify the score if citizens could migrate to a city of their
4515 * original nation. */
4516 if (citizens_nation_get(pcity, city_owner(acity)->slot) > 0) {
4517 score_tmp *= 2;
4518 }
4519 }
4520
4522 /* select the best! */
4525
4526 log_debug("[M] T%d - best city (world): %s (%s) score: "
4527 "%6.3f (> %6.3f)", game.info.turn,
4531 }
4532 }
4534
4535 if (best_city_player != NULL) {
4536 /* First, do the migration within one nation */
4537 if (fc_rand(100) >= game.server.mgr_nationchance) {
4538 /* No migration */
4539 /* N.B.: city_link always returns the same pointer. */
4542 _("Citizens of %s are thinking about migrating to %s "
4543 "for a better life."),
4545 } else {
4547 }
4548
4549 /* Stop here */
4550 continue;
4551 }
4552
4553 if (best_city_world != NULL) {
4554 /* Second, do the migration between all nations */
4555 if (fc_rand(100) >= game.server.mgr_worldchance) {
4556 const char *nname;
4557
4559 /* No migration */
4560 /* N.B.: city_link always returns the same pointer. */
4563 /* TRANS: <city1> to <city2> (<city2 nation adjective>). */
4564 _("Citizens of %s are thinking about migrating to %s "
4565 "(%s) for a better life."),
4567 } else {
4569 internat = TRUE;
4570 }
4571
4572 /* Stop here */
4573 continue;
4574 }
4576
4577 return internat;
4578}
4579
4580/**********************************************************************/
4583void city_style_refresh(struct city *pcity)
4584{
4585 pcity->style = city_style(pcity);
4586}
4587
4588/**********************************************************************/
4592void city_counters_refresh(struct city *pcity)
4593{
4595 struct packet_city_update_counters packet;
4596
4597 packet.city = pcity->id;
4598
4600
4601 packet.count = counter_count;
4602 for (i = 0; i < counter_count; i++) {
4603 packet.counters[i] = pcity->counter_values[i];
4604 }
4605
4608}
4609
4610/**********************************************************************/
4613void city_tc_effect_refresh(struct player *pplayer)
4614{
4615 const struct civ_map *nmap = &(wld.map);
4616
4617 city_list_iterate(pplayer->cities, pcity) {
4618 bool changed = FALSE;
4619
4621 city_tile(pcity), ptile, idx, x, y) {
4622 if (ptile->worked == pcity
4623 && get_city_tile_output_bonus(pcity, ptile, NULL, EFT_TILE_WORKABLE) <= 0) {
4624 city_map_update_empty(pcity, ptile);
4625 pcity->specialists[DEFAULT_SPECIALIST]++;
4626 changed = TRUE;
4627 }
4629
4630 if (changed) {
4631 auto_arrange_workers(pcity);
4632 send_city_info(NULL, pcity);
4633 }
4635}
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
int city_granary_size(int city_size)
Definition city.c:2132
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:1240
bool is_capital(const struct city *pcity)
Definition city.c:1579
const char * city_name_get(const struct city *pcity)
Definition city.c:1137
void city_remove_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3371
int city_improvement_upkeep(const struct city *pcity, const struct impr_type *b)
Definition city.c:1255
int city_airlift_max(const struct city *pcity)
Definition city.c:2942
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:3388
void city_size_add(struct city *pcity, int add)
Definition city.c:1164
bool city_production_has_flag(const struct city *pcity, enum impr_flag_id flag)
Definition city.c:727
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:1653
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:1626
Specialist_type_id best_specialist(Output_type_id otype, const struct city *pcity)
Definition city.c:3334
struct city * city_list_find_number(struct city_list *This, int id)
Definition city.c:1679
bool city_celebrating(const struct city *pcity)
Definition city.c:1645
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:2870
bool city_can_grow_to(const struct city *pcity, int pop_size)
Definition city.c:2012
void city_refresh_from_main_map(const struct civ_map *nmap, struct city *pcity, bool *workers_map)
Definition city.c:3176
bool city_happy(const struct city *pcity)
Definition city.c:1614
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:1283
bool can_city_build_unit_direct(const struct civ_map *nmap, const struct city *pcity, const struct unit_type *punittype)
Definition city.c:903
bool can_city_build_now(const struct civ_map *nmap, const struct city *pcity, const struct universal *target)
Definition city.c:1013
bool can_city_build_unit_now(const struct civ_map *nmap, const struct city *pcity, const struct unit_type *punittype)
Definition city.c:947
int city_map_tiles(int city_radius_sq)
Definition city.c:171
bool city_exist(int id)
Definition city.c:3572
bool city_can_work_tile(const struct city *pcity, const struct tile *ptile)
Definition city.c:1456
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:3626
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:2923
bool city_can_change_build(const struct city *pcity)
Definition city.c:1079
bool can_city_build_unit_later(const struct civ_map *nmap, const struct city *pcity, const struct unit_type *punittype)
Definition city.c:970
int city_total_unit_gold_upkeep(const struct city *pcity)
Definition city.c:1222
int city_total_impr_gold_upkeep(const struct city *pcity)
Definition city.c:1201
#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:845
#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:834
#define city_built_iterate_end
Definition city.h:840
#define output_type_iterate_end
Definition city.h:851
void city_map_update_empty(struct city *pcity, struct tile *ptile)
Definition citytools.c:3268
void send_city_info(struct player *dest, struct city *pcity)
Definition citytools.c:2363
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:856
void sync_cities(void)
Definition citytools.c:3342
static void void city_freeze_workers(struct city *pcity)
Definition citytools.c:137
bool city_map_update_radius_sq(struct city *pcity)
Definition citytools.c:3467
void change_build_target(struct player *pplayer, struct city *pcity, struct universal *target, enum event_type event)
Definition citytools.c:3179
bool city_illness_strike(struct city *pcity)
Definition citytools.c:2971
void city_thaw_workers(struct city *pcity)
Definition citytools.c:147
void building_lost(struct city *pcity, const struct impr_type *pimprove, const char *reason, struct unit *destroyer)
Definition citytools.c:3068
void city_units_upkeep(const struct city *pcity)
Definition citytools.c:3156
void remove_city(struct city *pcity)
Definition citytools.c:1708
void do_sell_building(struct player *pplayer, struct city *pcity, struct impr_type *pimprove, const char *reason)
Definition citytools.c:2997
struct trade_route * remove_trade_route(struct city *pc1, struct trade_route *proute, bool announce, bool source_gone)
Definition citytools.c:2936
void broadcast_city_info(struct city *pcity)
Definition citytools.c:2245
void city_add_improvement_with_gov_notice(struct city *pcity, const struct impr_type *pimprove, const char *format)
Definition citytools.c:3661
void city_map_update_all(struct city *pcity)
Definition citytools.c:3361
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:721
void city_map_update_worker(struct city *pcity, struct tile *ptile)
Definition citytools.c:3282
void city_refresh_vision(struct city *pcity)
Definition citytools.c:3442
#define LOG_BUILD_TARGET
Definition citytools.h:21
static bool city_illness_check(const struct city *pcity)
Definition cityturn.c:3772
static void uk_rem_gold_append(struct unit *punit)
Definition cityturn.c:3087
static bool worklist_change_build_target(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2169
void remove_obsolete_buildings(struct player *pplayer)
Definition cityturn.c:271
static bool city_build_stuff(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2988
int city_shrink_granary_savings(const struct city *pcity)
Definition cityturn.c:893
static void apply_disaster(struct city *pcity, struct disaster_type *pdis)
Definition cityturn.c:4280
static bool city_distribute_surplus_shields(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2524
static void unit_list_referred_destroy(struct unit_list *punitlist)
Definition cityturn.c:3100
static bool place_pollution(struct city *pcity, enum extra_cause cause)
Definition cityturn.c:3385
void choose_build_target(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2387
static void uk_rem_gold_callback(struct unit *punit)
Definition cityturn.c:3066
void auto_arrange_workers(struct city *pcity)
Definition cityturn.c:367
void city_refresh_queue_add(struct city *pcity)
Definition cityturn.c:198
static struct unit_list * uk_rem_gold
Definition cityturn.c:94
static bool city_balance_treasury_buildings(struct city *pcity)
Definition cityturn.c:3303
static void update_city_activity(struct city *pcity)
Definition cityturn.c:3562
static void upgrade_unit_prod(struct city *pcity)
Definition cityturn.c:2500
static float city_migration_score(struct city *pcity)
Definition cityturn.c:3897
void nullify_prechange_production(struct city *pcity)
Definition cityturn.c:3553
bool city_empty_food_stock(struct city *pcity)
Definition cityturn.c:4258
bool check_city_migrations(void)
Definition cityturn.c:4226
static void upgrade_building_prod(struct city *pcity)
Definition cityturn.c:2453
bool player_balance_treasury_units_and_buildings(struct player *pplayer)
Definition cityturn.c:3206
bool city_change_size(struct city *pcity, citizens size, struct player *nationality, const char *reason)
Definition cityturn.c:1029
static citizens city_reduce_specialists(struct city *pcity, citizens change)
Definition cityturn.c:719
static void city_global_turn_notify(struct conn_list *dest)
Definition cityturn.c:491
static void city_populate(struct city *pcity, struct player *nationality)
Definition cityturn.c:1080
static void city_refresh_after_city_size_increase(struct city *pcity, struct player *nationality)
Definition cityturn.c:995
static void define_orig_production_values(struct city *pcity)
Definition cityturn.c:3522
void city_repair_size(struct city *pcity, int change)
Definition cityturn.c:852
#define cityimpr_list_iterate(cityimprlist, pcityimpr)
Definition cityturn.c:128
void city_counters_refresh(struct city *pcity)
Definition cityturn.c:4592
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:687
static bool city_build_building(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2605
static struct unit static const struct impr_type * building_upgrades_to(struct city *pcity, const struct impr_type *pimprove)
Definition cityturn.c:2432
void send_city_turn_notifications(struct connection *pconn)
Definition cityturn.c:577
static struct unit * sell_random_unit(struct player *pplayer, struct unit_list *punitlist)
Definition cityturn.c:3123
int city_incite_cost(struct player *pplayer, struct city *pcity)
Definition cityturn.c:3441
static void nullify_caravan_and_disband_plus(struct city *pcity)
Definition cityturn.c:3543
static bool sell_random_building(struct player *pplayer, struct cityimpr_list *imprs)
Definition cityturn.c:3020
static bool disband_city(struct city *pcity)
Definition cityturn.c:3785
void update_city_activities(struct player *pplayer)
Definition cityturn.c:603
bool city_reduce_size(struct city *pcity, citizens pop_loss, struct player *destroyer, const char *reason)
Definition cityturn.c:763
#define cityimpr_list_iterate_end
Definition cityturn.c:130
bool city_refresh(struct city *pcity)
Definition cityturn.c:159
static void city_turn_notify(const struct city *pcity, struct conn_list *dest, const struct player *cache_for_player)
Definition cityturn.c:512
static void city_shrink_reset_foodbox(struct city *pcity, int new_size)
Definition cityturn.c:906
static bool city_increase_size(struct city *pcity)
Definition cityturn.c:918
void apply_cmresult_to_city(struct city *pcity, const struct cm_result *cmr)
Definition cityturn.c:282
bool player_balance_treasury_units(struct player *pplayer)
Definition cityturn.c:3269
void city_style_refresh(struct city *pcity)
Definition cityturn.c:4583
static bool city_balance_treasury_units(struct city *pcity)
Definition cityturn.c:3350
static const struct unit_type * unit_upgrades_to(struct city *pcity, const struct unit_type *id)
Definition cityturn.c:2478
static bool do_city_migration(struct city *pcity_from, struct city *pcity_to)
Definition cityturn.c:3981
static struct city_list * city_refresh_queue
Definition cityturn.c:88
void city_refresh_for_player(struct player *pplayer)
Definition cityturn.c:183
void city_tc_effect_refresh(struct player *pplayer)
Definition cityturn.c:4613
static void check_pollution(struct city *pcity)
Definition cityturn.c:3424
void check_disasters(void)
Definition cityturn.c:4387
static void set_default_city_manager(struct cm_parameter *cmp, struct city *pcity)
Definition cityturn.c:314
static citizens city_reduce_workers(struct city *pcity, citizens change)
Definition cityturn.c:739
static bool worklist_item_postpone_req_vec(struct universal *target, struct city *pcity, struct player *pplayer, int saved_id)
Definition cityturn.c:1157
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:2762
void city_refresh_queue_processing(void)
Definition cityturn.c:214
int city_growth_granary_savings(const struct city *pcity)
Definition cityturn.c:879
void remove_obsolete_buildings_city(struct city *pcity, bool refresh)
Definition cityturn.c:236
static bool check_city_migrations_player(const struct player *pplayer)
Definition cityturn.c:4424
static bool city_build_unit(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2823
void cm_clear_cache(struct city *pcity)
Definition cm.c:322
void cm_init_parameter(struct cm_parameter *dest)
Definition cm.c:2183
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:2469
void cm_query_result(struct city *pcity, const struct cm_parameter *param, struct cm_result *result, bool negative_ok)
Definition cm.c:2122
void cm_init_emergency_parameter(struct cm_parameter *dest)
Definition cm.c:2201
void cm_print_city(const struct city *pcity)
Definition cm.c:2431
char * incite_cost
Definition comments.c:75
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:37
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:80
#define DISASTER_BASE_RARITY
Definition disaster.h:44
#define disaster_type_iterate_end
Definition disaster.h:86
int get_city_bonus(const struct city *pcity, enum effect_type effect_type)
Definition effects.c:846
int get_current_construction_bonus(const struct city *pcity, enum effect_type effect_type, const enum req_problem_type prob_type)
Definition effects.c:1182
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:916
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:377
unsigned char citizens
Definition fc_types.h:388
@ RPT_CERTAIN
Definition fc_types.h:701
@ RPT_POSSIBLE
Definition fc_types.h:700
#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:378
#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:62
struct world wld
Definition game.c:63
struct city * game_city_by_number(int id)
Definition game.c:107
#define GAME_MAX_MGR_DISTANCE
Definition game.h:533
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:566
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:630
int map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:654
#define iterate_outward(nmap, start_tile, max_dist, itr_tile)
Definition map.h:367
#define iterate_outward_end
Definition map.h:371
void map_update_border(struct tile *ptile, struct player *owner, int old_radius_sq, int new_radius_sq)
Definition maphand.c:2254
void map_claim_border(struct tile *ptile, struct player *owner, int radius_sq)
Definition maphand.c:2286
void update_tile_knowledge(struct tile *ptile)
Definition maphand.c:1439
#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:393
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:291
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:433
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:238
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:645
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:1627
struct player * player_slot_get_player(const struct player_slot *pslot)
Definition player.c:437
#define players_iterate_end
Definition player.h:537
#define players_iterate(_pplayer)
Definition player.h:532
static bool is_barbarian(const struct player *pplayer)
Definition player.h:489
#define is_ai(plr)
Definition player.h:230
void send_player_info_c(struct player *src, struct conn_list *dest)
Definition plrhand.c:1146
#define fc_rand(_size)
Definition rand.h:56
bool is_req_active(const struct req_context *context, const struct player *other_player, const struct requirement *req, const enum req_problem_type prob_type)
const char * universal_rule_name(const struct universal *psource)
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:1473
#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:120
struct impr_type * pimprove
Definition cityturn.c:121
int mgr_worldchance
Definition game.h:166
int incite_total_factor
Definition game.h:154
int mgr_nationchance
Definition game.h:164
struct conn_list * glob_observers
Definition game.h:98
bool mgr_foodneeded
Definition game.h:163
int base_incite_cost
Definition game.h:138
struct conn_list * est_connections
Definition game.h:97
struct packet_game_info info
Definition game.h:89
int mgr_turninterval
Definition game.h:165
bool migration
Definition game.h:168
int incite_improvement_factor
Definition game.h:153
int aqueductloss
Definition game.h:133
struct civ_game::@31::@35 server
int incite_unit_factor
Definition game.h:155
int mgr_distance
Definition game.h:162
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:65
int units_built
Definition player.h:102
enum spaceship_state state
Definition spaceship.h:108
struct city_list * cities
Definition player.h:279
struct unit_list * units
Definition player.h:280
struct conn_list * connections
Definition player.h:296
struct player_economic economic
Definition player.h:282
struct player_spaceship spaceship
Definition player.h:284
struct player_score score
Definition player.h:281
struct player_slot * slot
Definition player.h:248
const struct player * player
Definition tile.h:50
struct unit_list * units
Definition tile.h:58
struct requirement_vector build_reqs
Definition unittype.h:520
Definition unit.h:138
int length
Definition unit.h:195
int upkeep[O_LAST]
Definition unit.h:148
bool has_orders
Definition unit.h:193
struct unit::@80 orders
int id
Definition unit.h:145
bool vigilant
Definition unit.h:197
struct unit::@81::@84 server
struct unit_order * list
Definition unit.h:198
struct player * nationality
Definition unit.h:144
int upkeep_paid[O_LAST]
Definition unit.h:256
struct goods_type * carrying
Definition unit.h:186
const struct unit_type * utype
Definition unit.h:139
enum universals_n kind
Definition fc_types.h:902
universals_u value
Definition fc_types.h:901
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:195
#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:290
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:1388
void give_immediate_free_tech(struct research *presearch, Tech_type_id tech)
Definition techtools.c:1407
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:118
#define tile_has_extra(ptile, pextra)
Definition tile.h:151
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_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)
bool goods_can_be_provided(const struct city *pcity, const struct goods_type *pgood, struct unit *punit)
#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:721
const struct impr_type * building
Definition fc_types.h:714
bool unit_is_alive(int id)
Definition unit.c:2261
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:367
#define unit_tile(_pu)
Definition unit.h:397
#define unit_cargo_iterate_end
Definition unit.h:573
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition unit.h:570
#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:1764
bool place_unit(struct unit *punit, struct player *pplayer, struct city *pcity, struct unit *ptrans, bool force)
Definition unittools.c:1706
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:1658
void unit_unset_removal_callback(struct unit *punit)
Definition unittools.c:1778
void wipe_unit(struct unit *punit, enum unit_loss_reason reason, struct player *killer)
Definition unittools.c:2132
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:881
#define unit_tech_reqs_iterate(_utype_, _p)
Definition unittype.h:875
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:617
#define U_NOT_OBSOLETED
Definition unittype.h:528
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