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_reset_foodbox(struct city *pcity, int new_size,
907 bool shrink)
908{
909 int savings_pct = ( shrink
912
914}
915
916/**********************************************************************/
921static bool city_increase_size(struct city *pcity)
922{
923 int new_food;
925 bool have_square = FALSE;
926 bool rapture_grow = city_rapture_grow(pcity); /* Check before size increase! */
927 struct tile *pcenter = city_tile(pcity);
928 struct player *powner = city_owner(pcity);
929 const struct impr_type *pimprove = pcity->production.value.building;
930 const struct civ_map *nmap = &(wld.map);
931
932 if (!city_can_grow_to(pcity, city_size_get(pcity) + 1)) {
933 /* Need improvement */
937 _("%s needs %s (being built) to grow beyond size %d."),
938 city_link(pcity),
940 city_size_get(pcity));
941 } else {
943 _("%s needs an improvement to grow beyond size %d."),
944 city_link(pcity), city_size_get(pcity));
945 }
946 /* Granary can only hold so much */
948 * (100 * 100 - game.server.aqueductloss * (100 - savings_pct))
949 / (100 * 100));
950 pcity->food_stock = MIN(pcity->food_stock, new_food);
951
952 return FALSE;
953 }
954
955 city_size_add(pcity, 1);
956
957 /* Do not empty food stock if city is growing by celebrating */
958 if (rapture_grow) {
960 } else {
962 }
963 pcity->food_stock = MIN(pcity->food_stock, new_food);
964
965 /* If there is enough food, and the city is big enough,
966 * make new citizens into scientists or taxmen -- Massimo */
967
968 /* Ignore food if no square can be worked */
970 ptile, _index, _x, _y) {
971 if (tile_worked(ptile) != pcity /* Quick test */
972 && city_can_work_tile(pcity, ptile)) {
974 }
976
977 if ((pcity->surplus[O_FOOD] >= 2 || !have_square)
979 pcity->specialists[best_specialist(O_SCIENCE, pcity)]++;
980 } else if ((pcity->surplus[O_FOOD] >= 2 || !have_square)
982 pcity->specialists[best_specialist(O_GOLD, pcity)]++;
983 } else {
984 pcity->specialists[DEFAULT_SPECIALIST]++; /* or else city is !sane */
985 }
986
987 /* Deprecated signal. Connect your lua functions to "city_size_change" that's
988 * emitted from calling functions which know the 'reason' of the increase. */
989 script_server_signal_emit("city_growth", pcity,
990 (lua_Integer)city_size_get(pcity));
991
992 return TRUE;
993}
994
995/**********************************************************************/
999 struct player *nationality)
1000{
1001 struct player *powner = city_owner(pcity);
1002
1003 /* Update citizens. */
1004 citizens_update(pcity, nationality);
1005
1006 /* Refresh the city data; this also checks the squared city radius. */
1007 city_refresh(pcity);
1008
1009 auto_arrange_workers(pcity);
1010
1011 /* Update cities that have trade routes with us */
1013 if (city_refresh(pcity2)) {
1014 /* This should never happen, but if it does, make sure not to
1015 * leave workers outside city radius. */
1017 }
1019
1021 _("%s grows to size %d."),
1022 city_link(pcity), city_size_get(pcity));
1023
1024 sanity_check_city(pcity);
1025
1026 sync_cities();
1027}
1028
1029/**********************************************************************/
1033 struct player *nationality, const char *reason)
1034{
1035 int change = size - city_size_get(pcity);
1036
1037 if (change > 0) {
1038 int old_size = city_size_get(pcity);
1039 int real_change;
1040 int current_size = city_size_get(pcity);
1041 int id = pcity->id;
1042
1043 /* Increase city size until size reached, or increase fails */
1044 while (size > current_size && city_increase_size(pcity)) {
1045 /* TODO: This is currently needed only because there's
1046 * deprecated script signal "city_growth" emitted.
1047 * Check the need after signal has been dropped completely. */
1048 if (!city_exist(id)) {
1049 return FALSE;
1050 }
1051
1052 current_size++;
1053 }
1054
1055 city_refresh_after_city_size_increase(pcity, nationality);
1056
1058
1059 if (real_change != 0 && reason != NULL) {
1060 script_server_signal_emit("city_size_change", pcity,
1062
1063 if (!city_exist(id)) {
1064 return FALSE;
1065 }
1066 }
1067 } else if (change < 0) {
1068 /* We assume that city_change_size() is never called because
1069 * of enemy actions. If that changes, enemy must be passed
1070 * to city_reduce_size() */
1071 return city_reduce_size(pcity, -change, NULL, reason);
1072 }
1073
1074 map_claim_border(pcity->tile, pcity->owner, -1);
1075
1076 return TRUE;
1077}
1078
1079/**********************************************************************/
1083static void city_populate(struct city *pcity, struct player *nationality)
1084{
1085 int saved_id = pcity->id;
1086 int granary_size = city_granary_size(city_size_get(pcity));
1087
1088 pcity->food_stock += pcity->surplus[O_FOOD];
1089 if (pcity->food_stock >= granary_size || city_rapture_grow(pcity)) {
1090 if (city_had_recent_plague(pcity)) {
1091 notify_player(city_owner(pcity), city_tile(pcity),
1093 _("A recent plague outbreak prevents growth in %s."),
1094 city_link(pcity));
1095 /* Lose excess food */
1096 pcity->food_stock = MIN(pcity->food_stock, granary_size);
1097 } else {
1098 bool success;
1099
1100 success = city_increase_size(pcity);
1101 map_claim_border(pcity->tile, pcity->owner, -1);
1102
1103 if (success) {
1104 city_refresh_after_city_size_increase(pcity, nationality);
1105 script_server_signal_emit("city_size_change", pcity,
1106 (lua_Integer)1, "growth");
1107 }
1108 }
1109 } else if (pcity->food_stock < 0) {
1110 /* FIXME: should this depend on units with ability to build
1111 * cities or on units that require food in upkeep?
1112 * I'll assume citybuilders (units that 'contain' 1 pop) -- sjolie
1113 * The above may make more logical sense, but in game terms
1114 * you want to disband a unit that is draining your food
1115 * reserves. Hence, I'll assume food upkeep > 0 units. -- jjm
1116 */
1118 if (punit->upkeep[O_FOOD] > 0) {
1119 const char *punit_link = unit_tile_link(punit);
1120
1123 notify_player(city_owner(pcity), city_tile(pcity),
1125 _("Famine feared in %s, %s lost!"),
1126 city_link(pcity), punit_link);
1127 }
1128
1129 if (city_exist(saved_id)) {
1130 city_reset_foodbox(pcity, city_size_get(pcity), TRUE);
1131 }
1132
1133 return;
1134 }
1136 if (city_size_get(pcity) > 1) {
1137 notify_player(city_owner(pcity), city_tile(pcity),
1139 _("Famine causes population loss in %s."),
1140 city_link(pcity));
1141 } else {
1142 notify_player(city_owner(pcity), city_tile(pcity),
1144 _("Famine destroys %s entirely."),
1145 city_link(pcity));
1146 }
1147 city_reset_foodbox(pcity, city_size_get(pcity) - 1, TRUE);
1148 if (city_reduce_size(pcity, 1, NULL, "famine")) {
1149 pcity->had_famine = TRUE;
1150 }
1151 }
1152}
1153
1154/**********************************************************************/
1161 struct city *pcity,
1162 struct player *pplayer,
1163 int saved_id)
1164{
1165 const void *ptarget;
1166 const char *tgt_name;
1167 const struct requirement_vector *build_reqs;
1168 const char *signal_name;
1169 const struct req_context city_ctxt = {
1170 .player = pplayer,
1171 .city = pcity,
1172 .tile = city_tile(pcity)
1173 /* FIXME: Setting .unittype is currently redundant,
1174 * but can_city_build_unit_direct() does it */
1175 };
1176 bool purge = FALSE;
1177 bool known = FALSE;
1178
1179 if (pcity->wlcb == WLCB_ALWAYS_PURGE) {
1180 return TRUE;
1181 }
1182
1183 switch (target->kind) {
1184 case VUT_UTYPE:
1185 ptarget = target->value.utype;
1186 build_reqs = &target->value.utype->build_reqs;
1188 signal_name = "unit_cant_be_built";
1189 break;
1190 case VUT_IMPROVEMENT:
1191 ptarget = target->value.building;
1192 build_reqs = &target->value.building->reqs;
1194 signal_name = "building_cant_be_built";
1195 break;
1196 default:
1198 || target->kind == VUT_UTYPE), FALSE);
1199 return FALSE;
1200 }
1201
1202 if (pcity->wlcb == WLCB_ALWAYS_POSTPONE) {
1203 notify_player(pplayer, city_tile(pcity),
1205 _("%s can't build %s from the worklist. "
1206 "Postponing..."),
1207 city_link(pcity),
1208 tgt_name);
1209 return FALSE;
1210 }
1211
1212 requirement_vector_iterate(build_reqs, preq) {
1214 known = TRUE;
1215 switch (preq->source.kind) {
1216 case VUT_COUNTER:
1217 if (preq->present) {
1218 notify_player(pplayer, city_tile(pcity),
1220 _("%s can't build %s from the worklist; "
1221 "counter %s value's checkpoint do not met "
1222 "Postponing..."),
1223 city_link(pcity),
1224 tgt_name,
1226 (preq->source.value.counter));
1227 } else {
1228 purge = TRUE;
1229 }
1230 break;
1231 case VUT_ADVANCE:
1232 if (preq->present) {
1233 notify_player(pplayer, city_tile(pcity),
1235 _("%s can't build %s from the worklist; "
1236 "tech %s not yet available. Postponing..."),
1237 city_link(pcity),
1238 tgt_name,
1240 (preq->source.value.advance));
1242 pcity, "need_tech");
1243 } else {
1244 /* While techs can be unlearned, this isn't useful feedback */
1245 purge = TRUE;
1246 }
1247 break;
1248 case VUT_TECHFLAG:
1249 if (preq->present) {
1250 notify_player(pplayer, city_tile(pcity),
1252 _("%s can't build %s from the worklist; "
1253 "no tech with flag \"%s\" yet available. "
1254 "Postponing..."),
1255 city_link(pcity),
1256 tgt_name,
1257 tech_flag_id_name(preq->source.value.techflag));
1259 pcity, "need_techflag");
1260 } else {
1261 /* While techs can be unlearned, this isn't useful feedback */
1262 purge = TRUE;
1263 }
1264 break;
1265 case VUT_IMPROVEMENT:
1266 case VUT_SITE:
1267 if (preq->range == REQ_RANGE_LOCAL) {
1268 /* Building itself is never going to change */
1269 purge = TRUE;
1270 } else {
1271 if (preq->present) {
1272 notify_player(pplayer, city_tile(pcity),
1274 _("%s can't build %s from the worklist; "
1275 "need to have %s first. Postponing..."),
1276 city_link(pcity),
1277 tgt_name,
1279 preq->source.value.building));
1281 pcity, "need_building");
1282 } else {
1283 notify_player(pplayer, city_tile(pcity),
1285 _("%s can't build %s from the worklist; "
1286 "need to not have %s. Postponing..."),
1287 city_link(pcity),
1288 tgt_name,
1290 preq->source.value.building));
1292 pcity, "have_building");
1293 }
1294 }
1295 break;
1296 case VUT_IMPR_GENUS:
1297 if (preq->range == REQ_RANGE_LOCAL) {
1298 /* Building's own genus is never going to change */
1299 purge = TRUE;
1300 } else {
1301 if (preq->present) {
1302 notify_player(pplayer, city_tile(pcity),
1304 _("%s can't build %s from the worklist; "
1305 "need to have %s first. Postponing..."),
1306 city_link(pcity),
1307 tgt_name,
1309 preq->source.value.impr_genus));
1311 pcity, "need_building_genus");
1312 } else {
1313 notify_player(pplayer, city_tile(pcity),
1315 _("%s can't build %s from the worklist; "
1316 "need to not have %s. Postponing..."),
1317 city_link(pcity),
1318 tgt_name,
1320 preq->source.value.impr_genus));
1322 pcity, "have_building_genus");
1323 }
1324 }
1325 break;
1326 case VUT_IMPR_FLAG:
1327 if (preq->range == REQ_RANGE_LOCAL) {
1328 /* Building's own flags are never going to change */
1329 purge = TRUE;
1330 } else {
1331 if (preq->present) {
1332 notify_player(pplayer, city_tile(pcity),
1334 _("%s can't build %s from the worklist; "
1335 "need to have %s first. Postponing..."),
1336 city_link(pcity),
1337 tgt_name,
1339 preq->source.value.impr_flag));
1341 pcity, "need_building_flag");
1342 } else {
1343 notify_player(pplayer, city_tile(pcity),
1345 _("%s can't build %s from the worklist; "
1346 "need to not have %s. Postponing..."),
1347 city_link(pcity),
1348 tgt_name,
1350 preq->source.value.impr_flag));
1352 pcity, "have_building_flag");
1353 }
1354 }
1355 break;
1356 case VUT_PLAYER_FLAG:
1357 if (preq->present) {
1358 notify_player(pplayer, city_tile(pcity),
1360 _("%s can't build %s from the worklist; "
1361 "need to have %s first. Postponing..."),
1362 city_link(pcity),
1363 tgt_name,
1365 preq->source.value.plr_flag));
1367 pcity, "need_player_flag");
1368 } else {
1369 notify_player(pplayer, city_tile(pcity),
1371 _("%s can't build %s from the worklist; "
1372 "need to not have %s. Postponing..."),
1373 city_link(pcity),
1374 tgt_name,
1376 preq->source.value.plr_flag));
1378 pcity, "have_player_flag");
1379 }
1380 break;
1381 case VUT_PLAYER_STATE:
1382 purge = TRUE;
1383 break;
1384 case VUT_GOVERNMENT:
1385 if (preq->present) {
1386 notify_player(pplayer, city_tile(pcity),
1388 _("%s can't build %s from the worklist; "
1389 "it needs %s government. Postponing..."),
1390 city_link(pcity),
1391 tgt_name,
1392 government_name_translation(preq->source.value.govern));
1394 pcity, "need_government");
1395 } else {
1396 notify_player(pplayer, city_tile(pcity),
1398 _("%s can't build %s from the worklist; "
1399 "it cannot have %s government. Postponing..."),
1400 city_link(pcity),
1401 tgt_name,
1402 government_name_translation(preq->source.value.govern));
1404 pcity, "have_government");
1405 }
1406 break;
1407 case VUT_ACHIEVEMENT:
1408 if (preq->present) {
1409 notify_player(pplayer, city_tile(pcity),
1411 _("%s can't build %s from the worklist; "
1412 "it needs \"%s\" achievement. Postponing..."),
1413 city_link(pcity),
1414 tgt_name,
1415 achievement_name_translation(preq->source.value.achievement));
1417 pcity, "need_achievement");
1418 } else {
1419 /* Can't unachieve things. */
1420 purge = TRUE;
1421 }
1422 break;
1423 case VUT_EXTRA:
1424 if (preq->present) {
1425 notify_player(pplayer, city_tile(pcity),
1427 Q_("?extra:%s can't build %s from the worklist; "
1428 "%s is required. Postponing..."),
1429 city_link(pcity),
1430 tgt_name,
1431 extra_name_translation(preq->source.value.extra));
1433 pcity, "need_extra");
1434 } else {
1435 notify_player(pplayer, city_tile(pcity),
1437 Q_("?extra:%s can't build %s from the worklist; "
1438 "%s is prohibited. Postponing..."),
1439 city_link(pcity),
1440 tgt_name,
1441 extra_name_translation(preq->source.value.extra));
1443 pcity, "have_extra");
1444 }
1445 break;
1446 case VUT_GOOD:
1447 if (preq->present) {
1448 notify_player(pplayer, city_tile(pcity),
1450 Q_("?extra:%s can't build %s from the worklist; "
1451 "%s is required. Postponing..."),
1452 city_link(pcity),
1453 tgt_name,
1454 goods_name_translation(preq->source.value.good));
1456 pcity, "need_good");
1457 } else {
1458 notify_player(pplayer, city_tile(pcity),
1460 Q_("?extra:%s can't build %s from the worklist; "
1461 "%s is prohibited. Postponing..."),
1462 city_link(pcity),
1463 tgt_name,
1464 goods_name_translation(preq->source.value.good));
1466 pcity, "have_good");
1467 }
1468 break;
1469 case VUT_TERRAIN:
1470 if (preq->present) {
1471 notify_player(pplayer, city_tile(pcity),
1473 Q_("?terrain:%s can't build %s from the worklist; "
1474 "%s terrain is required. Postponing..."),
1475 city_link(pcity),
1476 tgt_name,
1477 terrain_name_translation(preq->source.value.terrain));
1479 pcity, "need_terrain");
1480 } else {
1481 notify_player(pplayer, city_tile(pcity),
1483 Q_("?terrain:%s can't build %s from the worklist; "
1484 "%s terrain is prohibited. Postponing..."),
1485 city_link(pcity),
1486 tgt_name,
1487 terrain_name_translation(preq->source.value.terrain));
1489 pcity, "have_terrain");
1490 }
1491 break;
1492 case VUT_NATION:
1493 if (preq->range < REQ_RANGE_TRADE_ROUTE
1494 || preq->range == REQ_RANGE_PLAYER) {
1495 /* At higher ranges, new players with their nations may arrive */
1496 purge = TRUE;
1497 } else {
1498 if (preq->present) {
1499 notify_player(pplayer, city_tile(pcity),
1501 /* TRANS: "%s nation" is adjective */
1502 Q_("?nation:%s can't build %s from the worklist; "
1503 "%s nation is required. Postponing..."),
1504 city_link(pcity),
1505 tgt_name,
1506 nation_adjective_translation(preq->source.value.nation));
1508 pcity, "need_nation");
1509 } else {
1510 notify_player(pplayer, city_tile(pcity),
1512 Q_("?nation:%s can't build %s from the worklist; "
1513 "%s nation is prohibited. Postponing..."),
1514 city_link(pcity),
1515 tgt_name,
1516 nation_adjective_translation(preq->source.value.nation));
1518 pcity, "have_nation");
1519 }
1520 }
1521 break;
1522 case VUT_NATIONGROUP:
1523 if (preq->range < REQ_RANGE_TRADE_ROUTE
1524 || preq->range == REQ_RANGE_PLAYER) {
1525 /* At higher ranges, new players with their nations may arrive */
1526 purge = TRUE;
1527 } else {
1528 if (preq->present) {
1529 notify_player(pplayer, city_tile(pcity),
1531 /* TRANS: "%s nation" is adjective */
1532 Q_("?ngroup:%s can't build %s from the worklist; "
1533 "%s nation is required. Postponing..."),
1534 city_link(pcity),
1535 tgt_name,
1536 nation_group_name_translation(preq->source.value.nationgroup));
1538 pcity, "need_nationgroup");
1539 } else {
1540 notify_player(pplayer, city_tile(pcity),
1542 Q_("?ngroup:%s can't build %s from the worklist; "
1543 "%s nation is prohibited. Postponing..."),
1544 city_link(pcity),
1545 tgt_name,
1546 nation_group_name_translation(preq->source.value.nationgroup));
1548 pcity, "have_nationgroup");
1549 }
1550 }
1551 break;
1552 case VUT_STYLE:
1553 /* FIXME: City styles sometimes change over time, but it isn't
1554 * entirely under player control. Probably better to purge
1555 * with useful explanation. */
1556 if (preq->present) {
1557 notify_player(pplayer, city_tile(pcity),
1559 _("%s can't build %s from the worklist; "
1560 "only %s style cities may build this. Postponing..."),
1561 city_link(pcity),
1562 tgt_name,
1563 style_name_translation(preq->source.value.style));
1565 pcity, "need_style");
1566 } else {
1567 notify_player(pplayer, city_tile(pcity),
1569 _("%s can't build %s from the worklist; "
1570 "%s style cities may not build this. Postponing..."),
1571 city_link(pcity),
1572 tgt_name,
1573 style_name_translation(preq->source.value.style));
1575 pcity, "have_style");
1576 }
1577 break;
1578 case VUT_NATIONALITY:
1579 /* FIXME: Changing citizen nationality is hard: purging might be
1580 * more useful in this case. */
1581 if (preq->present) {
1582 notify_player(pplayer, city_tile(pcity),
1584 /* TRANS: Latter %s is citizen nationality */
1585 _("%s can't build %s from the worklist; "
1586 "only city with %s may build this. Postponing..."),
1587 city_link(pcity),
1588 tgt_name,
1589 nation_plural_translation(preq->source.value.nationality));
1591 pcity, "need_nationality");
1592 } else {
1593 notify_player(pplayer, city_tile(pcity),
1595 /* TRANS: Latter %s is citizen nationality */
1596 _("%s can't build %s from the worklist; "
1597 "only city without %s may build this. Postponing..."),
1598 city_link(pcity),
1599 tgt_name,
1600 nation_plural_translation(preq->source.value.nationality));
1602 pcity, "have_nationality");
1603 }
1604 break;
1605 case VUT_ORIGINAL_OWNER:
1606 /* Original owner of this specific city won't change.
1607 * Update this when supporting ranges other than REQ_RANGE_CITY. */
1608 purge = TRUE;
1609 break;
1610 case VUT_DIPLREL:
1611 case VUT_DIPLREL_TILE: /* The tile owner is the city owner */
1612 case VUT_DIPLREL_TILE_O: /* The tile owner is the city owner */
1613 if (preq->present) {
1614 const char *reason;
1615
1616 notify_player(pplayer, city_tile(pcity),
1618 /* TRANS: '%s' is a wide range of relationships;
1619 * e.g., 'Peace', 'Never met', 'Foreign',
1620 * 'Hosts embassy', 'Provided Casus Belli' */
1621 _("%s can't build %s from the worklist; "
1622 "the relationship '%s' is required."
1623 " Postponing..."),
1624 city_link(pcity),
1625 tgt_name,
1627 preq->source.value.diplrel));
1628
1629 if (preq->source.kind == VUT_DIPLREL_TILE) {
1630 reason = "need_diplrel_tile";
1631 } else if (preq->source.kind == VUT_DIPLREL_TILE_O) {
1632 reason = "need_diplrel_tile_o";
1633 } else {
1634 fc_assert(preq->source.kind == VUT_DIPLREL);
1635 reason = "need_diplrel";
1636 }
1637
1639 pcity, reason);
1640 } else {
1641 const char *reason;
1642
1643 notify_player(pplayer, city_tile(pcity),
1645 _("%s can't build %s from the worklist; "
1646 "the relationship '%s' is prohibited."
1647 " Postponing..."),
1648 city_link(pcity),
1649 tgt_name,
1651 preq->source.value.diplrel));
1652
1653 if (preq->source.kind == VUT_DIPLREL_TILE) {
1654 reason = "have_diplrel_tile";
1655 } else if (preq->source.kind == VUT_DIPLREL_TILE_O) {
1656 reason = "have_diplrel_tile_o";
1657 } else {
1658 fc_assert(preq->source.kind == VUT_DIPLREL);
1659 reason = "have_diplrel";
1660 }
1661
1663 pcity, reason);
1664 }
1665 break;
1668 if (preq->present) {
1669 const char *reason;
1670
1671 notify_player(pplayer, city_tile(pcity),
1673 /* TRANS: '%s' is a wide range of relationships;
1674 * e.g., 'Peace', 'Never met', 'Foreign',
1675 * 'Hosts embassy', 'Provided Casus Belli' */
1676 _("%s can't build %s from the worklist; "
1677 "unit with the relationship '%s' is required."
1678 " Postponing..."),
1679 city_link(pcity),
1680 tgt_name,
1682 preq->source.value.diplrel));
1683
1684 if (preq->source.kind == VUT_DIPLREL_UNITANY) {
1685 reason = "need_diplrel_unitany";
1686 } else {
1687 fc_assert(preq->source.kind == VUT_DIPLREL_UNITANY_O);
1688 reason = "need_diplrel_unitany_o";
1689 }
1690
1692 pcity, reason);
1693 } else {
1694 const char *reason;
1695
1696 notify_player(pplayer, city_tile(pcity),
1698 _("%s can't build %s from the worklist; "
1699 "unit with the relationship '%s' is prohibited."
1700 " Postponing..."),
1701 city_link(pcity),
1702 tgt_name,
1704 preq->source.value.diplrel));
1705
1706 if (preq->source.kind == VUT_DIPLREL_UNITANY) {
1707 reason = "have_diplrel_unitany";
1708 } else {
1709 fc_assert(preq->source.kind == VUT_DIPLREL_UNITANY_O);
1710 reason = "have_diplrel_unitany_o";
1711 }
1712
1714 pcity, reason);
1715 }
1716 break;
1717 case VUT_MINSIZE:
1718 if (preq->present) {
1719 notify_player(pplayer, city_tile(pcity),
1721 _("%s can't build %s from the worklist; "
1722 "city must be of size %d or larger. "
1723 "Postponing..."),
1724 city_link(pcity),
1725 tgt_name,
1726 preq->source.value.minsize);
1728 pcity, "need_minsize");
1729 } else {
1730 notify_player(pplayer, city_tile(pcity),
1732 _("%s can't build %s from the worklist; "
1733 "city must be of size %d or smaller."
1734 "Postponing..."),
1735 city_link(pcity),
1736 tgt_name,
1737 (preq->source.value.minsize - 1));
1739 pcity, "need_minsize");
1740 }
1741 break;
1742 case VUT_MINCULTURE:
1743 if (preq->present) {
1744 notify_player(pplayer, city_tile(pcity),
1746 _("%s can't build %s from the worklist; "
1747 "city must have culture of %d. Postponing..."),
1748 city_link(pcity),
1749 tgt_name,
1750 preq->source.value.minculture);
1752 pcity, "need_minculture");
1753 } else {
1754 /* What has been written may not be unwritten. */
1755 purge = TRUE;
1756 }
1757 break;
1758 case VUT_MINFOREIGNPCT:
1759 if (preq->present) {
1760 notify_player(pplayer, city_tile(pcity),
1762 _("%s can't build %s from the worklist; "
1763 "city must have %d%% foreign population. Postponing..."),
1764 city_link(pcity),
1765 tgt_name,
1766 preq->source.value.minforeignpct);
1768 pcity, "need_minforeignpct");
1769 } else {
1770 notify_player(pplayer, city_tile(pcity),
1772 _("%s can't build %s from the worklist; "
1773 "city must have %d%% native population. Postponing..."),
1774 city_link(pcity),
1775 tgt_name,
1776 100 - preq->source.value.minforeignpct);
1778 pcity, "need_minforeignpct");
1779 }
1780 break;
1781 case VUT_MINTECHS:
1782 if (preq->present) {
1783 notify_player(pplayer, city_tile(pcity),
1785 _("%s can't build %s from the worklist; "
1786 "%d techs must be known. Postponing..."),
1787 city_link(pcity),
1788 tgt_name,
1789 preq->source.value.min_techs);
1791 pcity, "need_mintechs");
1792 } else {
1793 purge = TRUE;
1794 }
1795 break;
1796 case VUT_MAXTILEUNITS:
1797 if (preq->present) {
1798 notify_player(pplayer, city_tile(pcity),
1800 PL_("%s can't build %s from the worklist; "
1801 "more than %d unit on tile."
1802 " Postponing...",
1803 "%s can't build %s from the worklist; "
1804 "more than %d units on tile."
1805 " Postponing...",
1806 preq->source.value.max_tile_units),
1807 city_link(pcity),
1808 tgt_name,
1809 preq->source.value.max_tile_units);
1811 pcity, "need_tileunits");
1812 } else {
1813 notify_player(pplayer, city_tile(pcity),
1815 PL_("%s can't build %s from the worklist; "
1816 "fewer than %d unit on tile."
1817 " Postponing...",
1818 "%s can't build %s from the worklist; "
1819 "fewer than %d units on tile."
1820 " Postponing...",
1821 preq->source.value.max_tile_units + 1),
1822 city_link(pcity),
1823 tgt_name,
1824 preq->source.value.max_tile_units + 1);
1826 pcity, "need_tileunits");
1827 }
1828 break;
1829 case VUT_AI_LEVEL:
1830 /* Can't change AI level. */
1831 purge = TRUE;
1832 break;
1833 case VUT_TERRAINCLASS:
1834 /* Change of terrain class is expected to be very unlikely. Purge!
1835 * TODO: Analyze ruleset to see how unlikely terrain class change actually is. */
1836 purge = TRUE;
1837 break;
1838 case VUT_TERRFLAG:
1839 if (preq->present) {
1840 notify_player(pplayer, city_tile(pcity),
1842 _("%s can't build %s from the worklist; "
1843 "terrain with \"%s\" flag is required. "
1844 "Postponing..."),
1845 city_link(pcity),
1846 tgt_name,
1847 terrain_flag_id_name(preq->source.value.terrainflag));
1849 pcity, "need_terrainflag");
1850 } else {
1851 notify_player(pplayer, city_tile(pcity),
1853 _("%s can't build %s from the worklist; "
1854 "terrain with \"%s\" flag is prohibited. "
1855 "Postponing..."),
1856 city_link(pcity),
1857 tgt_name,
1858 terrain_flag_id_name(preq->source.value.terrainflag));
1860 pcity, "have_terrainflag");
1861 }
1862 break;
1863 case VUT_ROADFLAG:
1864 if (preq->present) {
1865 notify_player(pplayer, city_tile(pcity),
1867 _("%s can't build %s from the worklist; "
1868 "road with \"%s\" flag is required. "
1869 "Postponing..."),
1870 city_link(pcity),
1871 tgt_name,
1872 road_flag_id_name(preq->source.value.roadflag));
1874 pcity, "need_roadflag");
1875 } else {
1876 notify_player(pplayer, city_tile(pcity),
1878 _("%s can't build %s from the worklist; "
1879 "road with \"%s\" flag is prohibited. "
1880 "Postponing..."),
1881 city_link(pcity),
1882 tgt_name,
1883 road_flag_id_name(preq->source.value.roadflag));
1885 pcity, "have_roadflag");
1886 }
1887 break;
1888 case VUT_EXTRAFLAG:
1889 if (preq->present) {
1890 notify_player(pplayer, city_tile(pcity),
1892 _("%s can't build %s from the worklist; "
1893 "extra with \"%s\" flag is required. "
1894 "Postponing..."),
1895 city_link(pcity),
1896 tgt_name,
1897 extra_flag_id_translated_name(preq->source.value.extraflag));
1899 pcity, "need_extraflag");
1900 } else {
1901 notify_player(pplayer, city_tile(pcity),
1903 _("%s can't build %s from the worklist; "
1904 "extra with \"%s\" flag is prohibited. "
1905 "Postponing..."),
1906 city_link(pcity),
1907 tgt_name,
1908 extra_flag_id_translated_name(preq->source.value.extraflag));
1910 pcity, "have_extraflag");
1911 }
1912 break;
1913 case VUT_MINLATITUDE:
1914 case VUT_MAXLATITUDE:
1915 /* Can't change where the city is located. */
1916 purge = TRUE;
1917 break;
1918 case VUT_CITYTILE:
1919 if (CITYT_BORDERING_TCLASS_REGION == preq->source.value.citytile
1920 && (preq->range == REQ_RANGE_CADJACENT
1921 || preq->range == REQ_RANGE_ADJACENT)) {
1922 if (preq->present) {
1923 notify_player(pplayer, city_tile(pcity),
1925 _("%s can't build %s from the worklist; "
1926 "different terrain class nearby is required. "
1927 "Postponing..."),
1928 city_link(pcity),
1929 tgt_name);
1931 pcity, "need_different_terrainclass");
1932 } else {
1933 notify_player(pplayer, city_tile(pcity),
1935 _("%s can't build %s from the worklist; "
1936 "different terrain class nearby is prohibited. "
1937 "Postponing..."),
1938 city_link(pcity),
1939 tgt_name);
1941 pcity, "have_different_terrainclass");
1942 }
1943 break;
1944 }
1945 /* Other values should not present in build reqs */
1947
1948 case VUT_UTYPE:
1949 case VUT_UTFLAG:
1950 case VUT_UCLASS:
1951 case VUT_UCFLAG:
1952 case VUT_MINVETERAN:
1953 case VUT_UNITSTATE:
1954 case VUT_ACTIVITY:
1955 case VUT_MINMOVES:
1956 case VUT_MINHP:
1957 case VUT_ACTION:
1958 case VUT_OTYPE:
1959 case VUT_SPECIALIST:
1960 case VUT_TERRAINALTER: /* XXX could do this in principle */
1961 /* Will only happen with a bogus ruleset. */
1962 log_error("worklist_change_build_target() has bogus preq");
1963 break;
1964 case VUT_CITYSTATUS:
1965 if (preq->source.value.citystatus == CITYS_TRANSFERRED) {
1966 /* If there's a change, it will invalidate worklist anyway. */
1967 purge = TRUE;
1968 } else if (preq->source.value.citystatus == CITYS_OWNED_BY_ORIGINAL) {
1969 if (preq->range == REQ_RANGE_CITY || preq->range == REQ_RANGE_TILE) {
1970 /* Can't change at these ranges */
1971 purge = TRUE;
1972 } else {
1973 if (preq->present) {
1974 notify_player(pplayer, city_tile(pcity),
1976 /* TRANS: last %s is a CityStatus ("OwnedByOriginal") */
1977 _("%s can't build %s from the worklist; "
1978 "only available when city in range %s \"%s\". "
1979 "Postponing..."),
1980 city_link(pcity),
1981 tgt_name, req_range_name(preq->range),
1982 citystatus_type_name(preq->source.value.citystatus));
1984 pcity, "need_citystatus");
1985 } else {
1986 notify_player(pplayer, city_tile(pcity),
1988 /* TRANS: last %s is a CityStatus ("OwnedByOriginal") */
1989 _("%s can't build %s from the worklist; "
1990 "not available when city in range %s is \"%s\". "
1991 "Postponing..."),
1992 city_link(pcity),
1993 tgt_name, req_range_name(preq->range),
1994 citystatus_type_name(preq->source.value.citystatus));
1996 pcity, "have_citystatus");
1997 }
1998 }
1999 } else {
2000 /* Other status types will only happen with a bogus ruleset. */
2001 log_error("worklist_change_build_target() has bogus citystatus preq");
2002 }
2003 break;
2004 case VUT_MINYEAR:
2005 if (preq->present) {
2006 notify_player(pplayer, city_tile(pcity),
2008 /* TRANS: last %s is a date */
2009 _("%s can't build %s from the worklist; "
2010 "only available from %s. Postponing..."),
2011 city_link(pcity),
2012 tgt_name,
2013 textyear(preq->source.value.minyear));
2015 pcity, "need_minyear");
2016 } else {
2017 /* Can't go back in time. */
2018 purge = TRUE;
2019 }
2020 break;
2021 case VUT_MINCALFRAG:
2022 /* Unlike VUT_MINYEAR, a requirement in either direction is
2023 * likely to be fulfilled sooner or later. */
2024 if (preq->present) {
2025 notify_player(pplayer, city_tile(pcity),
2027 /* TRANS: last %s is a calendar fragment from
2028 * the ruleset; may be a bare number */
2029 _("%s can't build %s from the worklist; "
2030 "only available from %s. Postponing..."),
2031 city_link(pcity),
2032 tgt_name,
2033 textcalfrag(preq->source.value.mincalfrag));
2035 pcity, "need_mincalfrag");
2036 } else {
2037 fc_assert_action(preq->source.value.mincalfrag > 0, break);
2038 notify_player(pplayer, city_tile(pcity),
2040 /* TRANS: last %s is a calendar fragment from
2041 * the ruleset; may be a bare number */
2042 _("%s can't build %s from the worklist; "
2043 "not available after %s. Postponing..."),
2044 city_link(pcity),
2045 tgt_name,
2046 textcalfrag(preq->source.value.mincalfrag - 1));
2048 pcity, "have_mincalfrag");
2049 }
2050 break;
2051 case VUT_TOPO:
2052 if (preq->present) {
2053 notify_player(pplayer, city_tile(pcity),
2055 /* TRANS: third %s is topology flag name
2056 * ("Hex", "ISO" */
2057 _("%s can't build %s from the worklist; "
2058 "only available in worlds with %s map."),
2059 city_link(pcity),
2060 tgt_name,
2061 _(topo_flag_name(preq->source.value.topo_property)));
2063 pcity, "need_topo");
2064 }
2065 purge = TRUE;
2066 break;
2067 case VUT_WRAP:
2068 if (preq->present) {
2069 notify_player(pplayer, city_tile(pcity),
2071 /* TRANS: third %s is wrap flag name
2072 * ("WrapX", "Wrapy") */
2073 _("%s can't build %s from the worklist; "
2074 "only available in worlds with %s map."),
2075 city_link(pcity),
2076 tgt_name,
2077 _(wrap_flag_name(preq->source.value.wrap_property)));
2079 pcity, "need_wrap");
2080 }
2081 purge = TRUE;
2082 break;
2083 case VUT_SERVERSETTING:
2084 notify_player(pplayer, city_tile(pcity),
2086 /* TRANS: %s is a server setting, its value and
2087 * if it is required to be present or absent.
2088 * The string's format is specified in
2089 * ssetv_human_readable().
2090 * Example: "killstack is enabled". */
2091 _("%s can't build %s from the worklist; "
2092 "only available when the server setting "
2093 "%s."),
2094 city_link(pcity),
2095 tgt_name,
2096 ssetv_human_readable(preq->source.value.ssetval,
2097 preq->present));
2099 pcity, "need_setting");
2100 /* Don't assume that the server setting will be changed. */
2101 purge = TRUE;
2102 break;
2103 case VUT_AGE:
2104 if (preq->present) {
2105 notify_player(pplayer, city_tile(pcity),
2107 _("%s can't build %s from the worklist; "
2108 "only available once %d turns old. Postponing..."),
2109 city_link(pcity),
2110 tgt_name,
2111 preq->source.value.age);
2113 pcity, "need_age");
2114 } else {
2115 /* Can't go back in time. */
2116 purge = TRUE;
2117 }
2118 break;
2119 case VUT_FORM_AGE:
2120 if (preq->present) {
2121 notify_player(pplayer, city_tile(pcity),
2123 _("%s can't build %s from the worklist; "
2124 "only available once %d turns old form. Postponing..."),
2125 city_link(pcity),
2126 tgt_name,
2127 preq->source.value.age);
2129 pcity, "need_form_age");
2130 } else {
2131 /* Can't go back in time. */
2132 purge = TRUE;
2133 }
2134 break;
2135 case VUT_NONE:
2136 case VUT_COUNT:
2138 "worklist_change_build_target() "
2139 "called with invalid preq");
2140 break;
2141 /* No default handling here, as we want compiler warning
2142 * if new requirement type is added to enum and it's not handled
2143 * here. */
2144 };
2145 break;
2146 }
2147
2148 /* Almost all cases emit signal in the end, so city check needed. */
2149 if (!city_exist(saved_id)) {
2150 /* Some script has removed city */
2151 return TRUE;
2152 }
2153
2155
2156 if (!known) {
2157 /* FIXME: make can_city_build_improvement_now() return a reason enum,
2158 * so we can notify user with it.
2159 * Likely the building already exist. */
2160 purge = TRUE;
2161 }
2162
2163 return purge;
2164}
2165
2166/**********************************************************************/
2172static bool worklist_change_build_target(struct player *pplayer,
2173 struct city *pcity)
2174{
2175 struct universal target;
2176 bool success = FALSE;
2177 int i;
2178 int saved_id = pcity->id;
2179 bool city_checked = TRUE; /* This is used to avoid spurious city_exist() calls */
2180 struct worklist *pwl = &pcity->worklist;
2181 const struct civ_map *nmap = &(wld.map);
2182
2183 if (worklist_is_empty(pwl)) {
2184 /* Nothing in the worklist; bail now. */
2185 return FALSE;
2186 }
2187
2188 i = 0;
2189 while (!success && i < worklist_length(pwl)) {
2190
2191 if (!city_checked) {
2192 if (!city_exist(saved_id)) {
2193 /* Some script has removed useless city that cannot build
2194 * what it is told to! */
2195 return FALSE;
2196 }
2198 }
2199
2200 if (worklist_peek_ith(pwl, &target, i)) {
2201 success = can_city_build_now(nmap, pcity, &target);
2202 } else {
2203 success = FALSE;
2204 }
2205 i++;
2206
2207 if (success) {
2208 break; /* while */
2209 }
2210
2211 switch (target.kind) {
2212 case VUT_UTYPE:
2213 {
2214 const struct unit_type *ptarget = target.value.utype;
2215 const struct unit_type *pupdate = unit_upgrades_to(pcity, ptarget);
2216 bool purge;
2217
2218 /* Maybe we can just upgrade the target to what the city /can/ build. */
2219 if (U_NOT_OBSOLETED == pupdate) {
2220 /* Nope, we're stuck. Skip this item from the worklist. */
2221 struct research *presearch = research_get(pplayer);
2222 struct advance *missing = NULL;
2223 bool multiple = FALSE;
2224
2227 if (missing != NULL) {
2228 multiple = TRUE;
2229 } else {
2230 missing = padv;
2231 }
2232 }
2234
2235
2236 if (missing != NULL) {
2237 if (!multiple) {
2238 notify_player(pplayer, city_tile(pcity),
2240 _("%s can't build %s from the worklist; "
2241 "tech %s not yet available. Postponing..."),
2244 } else {
2245 notify_player(pplayer, city_tile(pcity),
2247 _("%s can't build %s from the worklist; "
2248 "multiple techs still needed. Postponing..."),
2250 }
2251
2252 script_server_signal_emit("unit_cant_be_built", ptarget, pcity,
2253 "need_tech");
2254 } else {
2255 /* Unknown or requirement from vector. */
2256 purge = worklist_item_postpone_req_vec(&target, pcity, pplayer,
2257 saved_id);
2258 }
2260 break;
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) {
2910 /* TRANS: <city> is finished building <unit/building>. */
2911 _("%s is finished building %s."),
2912 city_link(pcity), utype_name_translation(utype));
2913 }
2914
2915 /* After we created the unit remove the citizen. This will also
2916 * rearrange the worker to take into account the extra resources
2917 * (food) needed. */
2918 if (pop_cost > 0) {
2919 /* This won't disband city due to pop_cost, but script might
2920 * still destroy city. */
2922 /* If the city has changed its nationalities during
2923 * "unit_built" signal, we take some other citizens instead */
2924 if (!city_reduce_size(pcity, pop_cost, NULL, "unit_built")) {
2925 break;
2926 }
2927 }
2928
2929 /* to eliminate micromanagement, we only subtract the unit's cost */
2930 /* signals could change the prod stock! */
2931 if ((pcity->before_change_shields -= unit_shield_cost) < 0) {
2932 pcity->before_change_shields = 0;
2933 }
2934 if ((pcity->shield_stock -= unit_shield_cost) < 0) {
2935 log_normal("City %s (%s) has built %s but has no %d shields "
2936 "for it, nullifying shield stock", city_name_get(pcity),
2937 player_name(pplayer), utype_rule_name(utype),
2939 pcity->shield_stock = 0;
2940 }
2941
2942 if (pop_cost > 0) {
2943 /* Additional message if the unit has population cost. */
2945 ftc_server,
2946 /* TRANS: "<unit> cost... <city> shrinks..."
2947 * Plural in "%d population", not "size %d". */
2948 PL_("%s cost %d population. %s shrinks to size %d.",
2949 "%s cost %d population. %s shrinks to size %d.",
2950 pop_cost),
2951 utype_name_translation(utype), pop_cost,
2952 city_link(pcity), city_size_get(pcity));
2953 }
2954
2955 if (i != 0 && worklist_length(pwl) > 0) {
2956 /* remove the build unit from the worklist; it has to be one less
2957 * than units build to preserve the next build target from the
2958 * worklist */
2959 worklist_remove(pwl, 0);
2960 }
2961 } /* for */
2962
2964 if (pcity->rally_point.length && !pcity->rally_point.persistent) {
2966 }
2967
2968 /* Done building this unit; time to move on to the next. */
2969 choose_build_target(pplayer, pcity);
2970 }
2971 } /* if */
2972
2973 return city_exist(saved_city_id);
2974}
2975
2976/**********************************************************************/
2979static bool city_build_stuff(struct player *pplayer, struct city *pcity)
2980{
2981 if (!city_distribute_surplus_shields(pplayer, pcity)) {
2982 return FALSE;
2983 }
2984
2987
2988 switch (pcity->production.kind) {
2989 case VUT_IMPROVEMENT:
2990 return city_build_building(pplayer, pcity);
2991 case VUT_UTYPE:
2992 return city_build_unit(pplayer, pcity);
2993 default:
2994 /* must never happen! */
2996 break;
2997 };
2998 return FALSE;
2999}
3000
3001/**********************************************************************/
3011static bool sell_random_building(struct player *pplayer,
3012 struct cityimpr_list *imprs)
3013{
3014 struct cityimpr *pcityimpr;
3015 int r;
3016
3017 fc_assert_ret_val(pplayer != NULL, FALSE);
3018
3019 if (!imprs || cityimpr_list_size(imprs) == 0) {
3020 return FALSE;
3021 }
3022
3025
3027 ftc_server,
3028 _("Can't afford to maintain %s in %s, building sold!"),
3030 city_link(pcityimpr->pcity));
3031 log_debug("%s: sold building (%s)", player_name(pplayer),
3033
3034 do_sell_building(pplayer, pcityimpr->pcity, pcityimpr->pimprove, "cant_maintain");
3035
3037
3038 /* Get back the gold upkeep that was already paid this turn. */
3040 pcityimpr->pimprove);
3041
3043
3045
3046 return TRUE;
3047}
3048
3049/**********************************************************************/
3057static void uk_rem_gold_callback(struct unit *punit)
3058{
3059 int gold_upkeep;
3060
3061 /* Remove the unit from uk_rem_gold. */
3063
3064 gold_upkeep = punit->server.upkeep_paid[O_GOLD];
3065
3066 /* All units in uk_rem_gold should have gold upkeep! */
3067 fc_assert_ret_msg(gold_upkeep > 0, "%s has %d gold upkeep",
3068 unit_rule_name(punit), gold_upkeep);
3069
3070 /* Get the upkeep gold back. */
3071 unit_owner(punit)->economic.gold += gold_upkeep;
3072}
3073
3074/**********************************************************************/
3078static void uk_rem_gold_append(struct unit *punit)
3079{
3080 /* Make the unit aware that it is on the uk_rem_gold list. */
3082
3083 /* Add the unit to the list. */
3085}
3086
3087/**********************************************************************/
3091static void unit_list_referred_destroy(struct unit_list *punitlist)
3092{
3094 /* Clear the unit's knowledge of the list. */
3097
3098 /* Destroy the list it self. */
3100}
3101
3102/**********************************************************************/
3114static struct unit *sell_random_unit(struct player *pplayer,
3115 struct unit_list *punitlist)
3116{
3117 struct unit *punit;
3118 int r;
3119 struct unit_list *cargo;
3120
3121 fc_assert_ret_val(pplayer != NULL, NULL);
3122
3123 if (!punitlist || unit_list_size(punitlist) == 0) {
3124 return NULL;
3125 }
3126
3129
3130 cargo = unit_list_new();
3131
3132 /* Check if unit is transporting other units from punitlist,
3133 * and sell one of those (recursively) instead.
3134 * Note that in case of recursive transports we have to iterate
3135 * also through those middle transports that themselves are not in
3136 * punitlist. */
3138 /* Optimization, do not iterate over punitlist
3139 * if we are sure that pcargo is not in it. */
3140 if (pcargo->server.upkeep_paid[O_GOLD] > 0) {
3142 if (pcargo == p2) {
3143 unit_list_append(cargo, pcargo);
3144 }
3146 }
3148
3149 if (unit_list_size(cargo) > 0) {
3150 /* Recursively sell. Note that cargo list has both
3151 * leaf units and middle transports in case of
3152 * recursive transports. */
3153 struct unit *ret = sell_random_unit(pplayer, cargo);
3154
3155 unit_list_destroy(cargo);
3156
3158
3159 return ret;
3160 }
3161
3162 unit_list_destroy(cargo);
3163
3164 {
3165 const char *punit_link = unit_tile_link(punit);
3166#ifdef FREECIV_DEBUG
3167 const char *punit_logname = unit_rule_name(punit);
3168#endif /* FREECIV_DEBUG */
3169 struct tile *utile = unit_tile(punit);
3170
3174
3175 /* The gold was paid back when the unit removal made
3176 * uk_rem_gold_callback() run as the unit's removal call back. */
3177
3178 notify_player(pplayer, utile, E_UNIT_LOST_MISC, ftc_server,
3179 _("Not enough gold. %s disbanded."),
3180 punit_link);
3181 log_debug("%s: unit sold (%s)", player_name(pplayer),
3183 } else {
3184 /* Not able to get rid of punit */
3185 return NULL;
3186 }
3187 }
3188
3190
3191 return punit;
3192}
3193
3194/**********************************************************************/
3198{
3199 struct cityimpr_list *pimprlist;
3200 bool sell_unit = TRUE;
3201
3202 if (!pplayer) {
3203 return FALSE;
3204 }
3205
3208
3209 city_list_iterate(pplayer->cities, pcity) {
3210 city_built_iterate(pcity, pimprove) {
3211 if (can_city_sell_building(pcity, pimprove)) {
3212 struct cityimpr *ci = fc_malloc(sizeof(*ci));
3213
3214 ci->pcity = pcity;
3215 ci->pimprove = pimprove;
3217 }
3220
3221 unit_list_iterate(pplayer->units, punit) {
3222 if (punit->server.upkeep_paid[O_GOLD] > 0) {
3224 }
3226
3227 while (pplayer->economic.gold < 0
3229 || unit_list_size(uk_rem_gold) > 0)) {
3231 || unit_list_size(uk_rem_gold) == 0) {
3233 } else {
3234 sell_random_unit(pplayer, uk_rem_gold);
3235 }
3237 }
3238
3239 /* Free remaining entries from list */
3241 FC_FREE(pimpr);
3243
3244 if (pplayer->economic.gold < 0) {
3245 /* If we get here it means the player has
3246 * negative gold. This should never happen. */
3247 fc_assert_msg(FALSE, "Player %s (nb %d) cannot have negative gold!",
3248 player_name(pplayer), player_number(pplayer));
3249 }
3250
3253
3254 return pplayer->economic.gold >= 0;
3255}
3256
3257/**********************************************************************/
3261{
3262 if (!pplayer) {
3263 return FALSE;
3264 }
3265
3267
3268 unit_list_iterate(pplayer->units, punit) {
3269 if (punit->server.upkeep_paid[O_GOLD] > 0) {
3271 }
3273
3274 while (pplayer->economic.gold < 0
3275 && sell_random_unit(pplayer, uk_rem_gold)) {
3276 /* All done in sell_random_unit() */
3277 }
3278
3279 if (pplayer->economic.gold < 0) {
3280 /* If we get here it means the player has
3281 * negative gold. This should never happen. */
3282 fc_assert_msg(FALSE, "Player %s (nb %d) cannot have negative gold!",
3283 player_name(pplayer), player_number(pplayer));
3284 }
3285
3287
3288 return pplayer->economic.gold >= 0;
3289}
3290
3291/**********************************************************************/
3295{
3296 struct player *pplayer;
3297 struct cityimpr_list *pimprlist;
3298
3299 if (!pcity) {
3300 return TRUE;
3301 }
3302
3303 pplayer = city_owner(pcity);
3305
3306 /* Create a vector of all buildings that can be sold. */
3307 city_built_iterate(pcity, pimprove) {
3308 if (can_city_sell_building(pcity, pimprove)) {
3309 struct cityimpr *ci = fc_malloc(sizeof(*ci));
3310
3311 ci->pcity = pcity;
3312 ci->pimprove = pimprove;
3314 }
3316
3317 /* Try to sell some buildings. */
3318 while (pplayer->economic.gold < 0
3319 && sell_random_building(pplayer, pimprlist)) {
3320 /* all done in sell_random_building */
3321 }
3322
3323 /* Free remaining entries from list */
3325 FC_FREE(pimpr);
3327
3329
3330 return pplayer->economic.gold >= 0;
3331}
3332
3333/**********************************************************************/
3342{
3343 struct player *pplayer;
3344
3345 if (!pcity) {
3346 return TRUE;
3347 }
3348
3349 pplayer = city_owner(pcity);
3351
3352 /* Create a vector of all supported units with gold upkeep. */
3354 if (punit->server.upkeep_paid[O_GOLD] > 0) {
3356 }
3358
3359 /* Still not enough gold, so try "selling" some units. */
3360 while (pplayer->economic.gold < 0
3361 && sell_random_unit(pplayer, uk_rem_gold)) {
3362 /* All done in sell_random_unit() */
3363 }
3364
3365 /* If we get here the player has negative gold, but hopefully
3366 * another city will be able to pay the deficit, so continue. */
3367
3369
3370 return pplayer->economic.gold >= 0;
3371}
3372
3373/**********************************************************************/
3376static bool place_pollution(struct city *pcity, enum extra_cause cause)
3377{
3378 struct tile *ptile;
3379 struct tile *pcenter = city_tile(pcity);
3380 int city_radius_sq = city_map_radius_sq_get(pcity);
3381 int k = 100;
3382 const struct civ_map *nmap = &(wld.map);
3383
3384 while (k > 0) {
3385 /* Place pollution on a random city tile */
3386 int cx, cy;
3387 int tile_id = fc_rand(city_map_tiles(city_radius_sq));
3388 struct extra_type *pextra;
3389
3390 city_tile_index_to_xy(&cx, &cy, tile_id, city_radius_sq);
3391
3392 /* Check for a real map position */
3393 if (!(ptile = city_map_to_tile(nmap, pcenter, city_radius_sq, cx, cy))) {
3394 continue;
3395 }
3396
3397 pextra = rand_extra_for_tile(ptile, cause, FALSE);
3398
3399 if (pextra != NULL && !tile_has_extra(ptile, pextra)) {
3400 tile_add_extra(ptile, pextra);
3401 update_tile_knowledge(ptile);
3402
3403 return TRUE;
3404 }
3405 k--;
3406 }
3407 log_debug("pollution not placed: city: %s", city_name_get(pcity));
3408
3409 return FALSE;
3410}
3411
3412/**********************************************************************/
3415static void check_pollution(struct city *pcity)
3416{
3417 if (fc_rand(100) < pcity->pollution) {
3418 if (place_pollution(pcity, EC_POLLUTION)) {
3420 _("Pollution near %s."), city_link(pcity));
3421 }
3422 }
3423}
3424
3425/**********************************************************************/
3432int city_incite_cost(struct player *pplayer, struct city *pcity)
3433{
3434 int dist, size;
3435 double cost; /* Intermediate values can get very large */
3436
3437 /* Gold factor */
3438 cost = city_owner(pcity)->economic.gold + game.server.base_incite_cost;
3439
3440 unit_list_iterate(pcity->tile->units, punit) {
3444
3445 /* Buildings */
3446 city_built_iterate(pcity, pimprove) {
3447 cost += impr_build_shield_cost(pcity, pimprove)
3450
3451 /* Stability bonuses */
3452 if (!city_unhappy(pcity)) {
3453 cost *= 2;
3454 }
3455 if (city_celebrating(pcity)) {
3456 cost *= 2;
3457 }
3458
3459 /* Buy back is cheap, conquered cities are also cheap */
3461 if (city_owner(pcity) != pcity->original) {
3462 if (pplayer == pcity->original) {
3463 cost /= 2; /* Buy back: 50% price reduction */
3464 } else {
3465 cost = cost * 2 / 3; /* Buy conquered: 33% price reduction */
3466 }
3467 }
3468 }
3469
3470 /* Distance from capital */
3471 /* Max penalty. Applied if there is no capital, or it's even further away. */
3472 dist = 32;
3473 city_list_iterate(city_owner(pcity)->cities, capital) {
3474 if (is_capital(capital)) {
3475 int tmp = map_distance(capital->tile, pcity->tile);
3476
3477 if (tmp < dist) {
3478 dist = tmp;
3479 }
3480 }
3482
3483 size = MAX(1, city_size_get(pcity)
3486 - pcity->feel[CITIZEN_ANGRY][FEELING_FINAL] * 3);
3487 cost *= size;
3489 cost = cost / (dist + 3);
3490
3492 int cost_per_citizen = cost / pcity->size;
3493 int natives = citizens_nation_get(pcity, city_owner(pcity)->slot);
3494 int tgt_cit = citizens_nation_get(pcity, pplayer->slot);
3495 int third_party = pcity->size - natives - tgt_cit;
3496
3497 cost = cost_per_citizen * (natives + 0.7 * third_party + 0.5 * tgt_cit);
3498 }
3499
3500 cost += (cost * get_city_bonus(pcity, EFT_INCITE_COST_PCT)) / 100;
3501 cost /= 100;
3502
3505 } else {
3506 return cost;
3507 }
3508}
3509
3510/**********************************************************************/
3513static void define_orig_production_values(struct city *pcity)
3514{
3515 /* Remember what this city is building last turn, so that on the next turn
3516 * the player can switch production to something else and then change it
3517 * back without penalty. This has to be updated _before_ production for
3518 * this turn is calculated, so that the penalty will apply if the player
3519 * changes production away from what has just been completed. This makes
3520 * sense if you consider what this value means: all the shields in the
3521 * city have been dedicated toward the project that was chosen last turn,
3522 * so the player shouldn't be penalized if the governor has to pick
3523 * something different. See city_change_production_penalty(). */
3524 pcity->changed_from = pcity->production;
3525
3526 log_debug("In %s, building %s. Beg of Turn shields = %d",
3528 pcity->before_change_shields);
3529}
3530
3531/**********************************************************************/
3534static void nullify_caravan_and_disband_plus(struct city *pcity)
3535{
3536 pcity->disbanded_shields = 0;
3537 pcity->caravan_shields = 0;
3538}
3539
3540/**********************************************************************/
3545{
3547 pcity->before_change_shields = 0;
3548}
3549
3550/**********************************************************************/
3553static void update_city_activity(struct city *pcity)
3554{
3555 struct player *pplayer;
3556 struct government *gov;
3557 bool is_happy;
3558 bool is_celebrating;
3559
3560 if (!pcity) {
3561 return;
3562 }
3563
3564 pplayer = city_owner(pcity);
3565 gov = government_of_city(pcity);
3566 is_happy = city_happy(pcity);
3568
3569 if (city_refresh(pcity)) {
3570 auto_arrange_workers(pcity);
3571 }
3572
3573 /* Reporting of celebrations rewritten, copying the treatment of disorder below,
3574 with the added rapture rounds count. 991219 -- Jing */
3575 if (city_build_stuff(pplayer, pcity)) {
3576 int saved_id;
3577 int revolution_turns;
3578
3579 pcity->history += city_history_gain(pcity);
3580
3581 /* History can decrease, but never go below zero */
3582 pcity->history = MAX(pcity->history, 0);
3583
3584 /* Keep old behaviour when building new improvement could keep
3585 city celebrating */
3586 if (!is_happy) {
3587 is_happy = city_happy(pcity);
3588 }
3589
3590 if (city_celebrating(pcity) || is_celebrating) {
3591 pcity->rapture++;
3592
3593 /* Update city's celebrating counters */
3595 if (pcount->type == CB_CITY_CELEBRATION_TURNS) {
3596 pcity->counter_values[pcount->index]++;
3597 }
3599
3600 if (pcity->rapture == 1) {
3602 _("Celebrations in your honor in %s."),
3603 city_link(pcity));
3604 }
3605 } else {
3606 if (pcity->rapture != 0) {
3608 _("Celebrations canceled in %s."),
3609 city_link(pcity));
3610 }
3611
3612 /* Update city's celebrating counters */
3614 if (pcount->type == CB_CITY_CELEBRATION_TURNS) {
3615 pcity->counter_values[pcount->index] = 0;
3616 }
3618 pcity->rapture = 0;
3619 }
3620 pcity->was_happy = is_happy;
3621
3622 /* Handle the illness. */
3623 if (game.info.illness_on) {
3624 /* Recalculate city illness; illness due to trade has to be saved
3625 * within the city struct, as the client does not have all
3626 * the data to calculate it */
3627 pcity->server.illness
3628 = city_illness_calc(pcity, NULL, NULL, &(pcity->illness_trade), NULL);
3629
3630 if (city_illness_check(pcity)) {
3631 if (!city_illness_strike(pcity)) {
3632 /* Illness destroyed the city */
3633 return;
3634 }
3635 }
3636 }
3637
3638 /* City population updated here, after the rapture stuff above. --Jing */
3639 saved_id = pcity->id;
3640 pcity->had_famine = FALSE;
3641 city_populate(pcity, pplayer);
3642 if (NULL == player_city_by_number(pplayer, saved_id)) {
3643 return;
3644 }
3645
3646 pcity->did_sell = FALSE;
3647 pcity->did_buy = FALSE;
3648 pcity->airlift = city_airlift_max(pcity);
3649 update_bulbs(pplayer, pcity->prod[O_SCIENCE], FALSE, FALSE);
3650
3652
3653 /* Update the treasury. */
3654 pplayer->economic.gold += pcity->surplus[O_GOLD];
3655
3656 /* FIXME: Nation level upkeep should be paid after ALL cities
3657 * have been processed, not after each individual city. */
3659 /* Unit upkeep was not included in city balance ->
3660 * not reduced from the city surplus. */
3661 pplayer->economic.gold -= city_total_unit_gold_upkeep(pcity);
3662
3664 /* Building upkeep was not included in city balance ->
3665 * not reduced from the city surplus. */
3666 pplayer->economic.gold -= city_total_impr_gold_upkeep(pcity);
3667 }
3668 }
3669
3670 /* Remember how much gold upkeep each unit was paid. */
3674
3675 if (pplayer->economic.gold < 0) {
3676 /* Not enough gold - we have to sell some buildings, and if that
3677 * is not enough, disband units with gold upkeep, taking into
3678 * account the setting of 'game.info.gold_upkeep_style':
3679 * GOLD_UPKEEP_CITY: Cities pay for buildings and units.
3680 * GOLD_UPKEEP_MIXED: Cities pay only for buildings; the nation pays
3681 * for units.
3682 * GOLD_UPKEEP_NATION: The nation pays for buildings and units. */
3683 switch (game.info.gold_upkeep_style) {
3684 case GOLD_UPKEEP_CITY:
3685 case GOLD_UPKEEP_MIXED:
3689 }
3690 break;
3691 case GOLD_UPKEEP_NATION:
3692 break;
3693 }
3694 }
3695
3697 if (city_unhappy(pcity)) {
3698 const char *revomsg;
3699
3700 pcity->anarchy++;
3701
3703 if (pcount->type == CB_CITY_DISORDER_TURNS) {
3704 pcity->counter_values[pcount->index]++;
3705 }
3707
3708 if (pcity->anarchy == revolution_turns) {
3709 /* Revolution next turn if not dealt with */
3710 /* TRANS: preserve leading space; this string will be appended to
3711 * another sentence */
3712 revomsg = _(" Unrest threatens to spread beyond the city.");
3713 } else {
3714 revomsg = "";
3715 }
3716 if (pcity->anarchy == 1) {
3718 /* TRANS: second %s is an optional extra sentence */
3719 _("Civil disorder in %s.%s"),
3720 city_link(pcity), revomsg);
3721 } else {
3723 /* TRANS: second %s is an optional extra sentence */
3724 _("CIVIL DISORDER CONTINUES in %s.%s"),
3725 city_link(pcity), revomsg);
3726 }
3727 } else {
3728 if (pcity->anarchy != 0) {
3730 _("Order restored in %s."),
3731 city_link(pcity));
3732 }
3733 pcity->anarchy = 0;
3734
3736 if (pcount->type == CB_CITY_DISORDER_TURNS) {
3737 pcity->counter_values[pcount->index] = 0;
3738 }
3740 }
3741 check_pollution(pcity);
3742
3743 send_city_info(NULL, pcity);
3744
3745 if (revolution_turns > 0 && pcity->anarchy > revolution_turns) {
3746 notify_player(pplayer, city_tile(pcity), E_ANARCHY, ftc_server,
3747 /* TRANS: %s - government form, e.g., Democracy */
3748 _("The people have overthrown your %s, "
3749 "your country is in turmoil."),
3752 }
3753 if (city_refresh(pcity)) {
3754 auto_arrange_workers(pcity);
3755 }
3756 sanity_check_city(pcity);
3757 }
3758}
3759
3760/**********************************************************************/
3763static bool city_illness_check(const struct city * pcity)
3764{
3765 if (fc_rand(1000) < pcity->server.illness) {
3766 return TRUE;
3767 }
3768
3769 return FALSE;
3770}
3771
3772/**********************************************************************/
3776static bool disband_city(struct city *pcity)
3777{
3778 struct player *pplayer = city_owner(pcity);
3779 struct tile *ptile = pcity->tile;
3780 struct city *rcity = NULL;
3781 const struct unit_type *utype = pcity->production.value.utype;
3782 struct unit *punit;
3783 int saved_id = pcity->id;
3784
3785 /* find closest city other than pcity */
3786 rcity = find_closest_city(ptile, pcity, pplayer, FALSE, FALSE, FALSE, TRUE,
3787 FALSE, NULL);
3788
3789 if (!rcity) {
3790 /* What should we do when we try to disband our only city? */
3791 notify_player(pplayer, ptile, E_CITY_CANTBUILD, ftc_server,
3792 _("%s can't build %s yet, "
3793 "as we can't disband our only city."),
3795 script_server_signal_emit("unit_cant_be_built", utype, pcity,
3796 "pop_cost");
3797 if (!city_exist(saved_id)) {
3798 /* Script decided to remove even the last city */
3799 return TRUE;
3800 } else {
3801 return FALSE;
3802 }
3803 }
3804
3805 punit = city_create_unit(pcity, utype, NULL);
3806
3807 /* "unit_built" script handler may have destroyed city. If so, we
3808 * assume something sensible happened to its units, and that the
3809 * script took care of announcing unit creation if required. */
3810 if (city_exist(saved_id)) {
3811 /* Shift all the units supported by pcity (including the new unit)
3812 * to rcity. transfer_city_units does not make sure no units are
3813 * left floating without a transport, but since all units are
3814 * transferred this is not a problem. */
3815 transfer_city_units(pplayer, pplayer, pcity->units_supported, rcity,
3816 pcity, -1, TRUE);
3817
3818 if (punit) {
3819 notify_player(pplayer, ptile, E_UNIT_BUILT, ftc_server,
3820 /* TRANS: "<city> is disbanded into Settler." */
3821 _("%s is disbanded into %s."),
3823 }
3824
3825 script_server_signal_emit("city_destroyed", pcity, pcity->owner, NULL);
3826
3827 if (!city_exist(saved_id)) {
3828 /* Already removed during the script */
3829 return TRUE;
3830 }
3831 remove_city(pcity);
3832
3833 /* Since we've removed the city, we don't need to worry about
3834 * charging for production, disabling rally points, etc. */
3835 }
3836
3837 return TRUE;
3838}
3839
3840/**********************************************************************/
3888static float city_migration_score(struct city *pcity)
3889{
3890 float score = 0.0;
3891 int build_shield_cost = 0;
3892 bool has_wonder = FALSE;
3893
3894 if (!pcity) {
3895 return score;
3896 }
3897
3898 if (pcity->server.mgr_score_calc_turn == game.info.turn) {
3899 /* up-to-date migration score */
3900 return pcity->server.migration_score;
3901 }
3902
3903 /* feeling of the citizens */
3904 score = (city_size_get(pcity)
3905 + 1.00 * pcity->feel[CITIZEN_HAPPY][FEELING_FINAL]
3906 + 0.00 * pcity->feel[CITIZEN_CONTENT][FEELING_FINAL]
3907 - 0.25 * pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL]
3908 - 0.50 * pcity->feel[CITIZEN_ANGRY][FEELING_FINAL]);
3909
3910 /* calculate shield build cost for all buildings */
3911 city_built_iterate(pcity, pimprove) {
3912 build_shield_cost += impr_build_shield_cost(pcity, pimprove);
3913 if (is_wonder(pimprove)) {
3914 /* this city has a wonder */
3915 has_wonder = TRUE;
3916 }
3918
3919 /* take shield costs of all buidings into account; normalized by 1000 */
3920 score *= (1 + (1 - exp(- (float) MAX(0, build_shield_cost) / 1000)) / 5);
3921 /* take trade into account; normalized by 100 */
3922 score *= (1 + (1 - exp(- (float) MAX(0, pcity->surplus[O_TRADE]) / 100))
3923 / 5);
3924 /* take luxury into account; normalized by 100 */
3925 score *= (1 + (1 - exp(- (float) MAX(0, pcity->surplus[O_LUXURY]) / 100))
3926 / 5);
3927 /* take science into account; normalized by 100 */
3928 score *= (1 + (1 - exp(- (float) MAX(0, pcity->surplus[O_SCIENCE]) / 100))
3929 / 5);
3930
3931 score += city_culture(pcity) * game.info.culture_migration_pml / 1000;
3932
3933 /* Take food into account; the food surplus is clipped to values between
3934 * -10..20 and normalize by 10. Thus, the factor is between 0.9 and 1.2. */
3935 score *= (1 + (float) CLIP(-10, pcity->surplus[O_FOOD], 20) / 10 );
3936
3937 /* Reduce the score due to city illness (plague). The illness is given in
3938 * tenth of percent (0..1000) and normalized by 25. Thus, this factor is
3939 * between 0.6 (ill city) and 1.0 (health city). */
3940 score *= (100 - (float)city_illness_calc(pcity, NULL, NULL, NULL, NULL)
3941 / 25);
3942
3943 if (has_wonder) {
3944 /* people like wonders */
3945 score *= 1.25;
3946 }
3947
3948 if (is_capital(pcity)) {
3949 /* the capital is a magnet for the citizens */
3950 score *= 1.25;
3951 }
3952
3953 /* take into account effects */
3954 score *= (1.0 + get_city_bonus(pcity, EFT_MIGRATION_PCT) / 100.0);
3955
3956 log_debug("[M] %s score: %.3f", city_name_get(pcity), score);
3957
3958 /* set migration score for the city */
3959 pcity->server.migration_score = score;
3960 /* set the turn, when the score was calculated */
3962
3963 return score;
3964}
3965
3966/**********************************************************************/
3973 struct city *pcity_to)
3974{
3976 struct tile *ptile_from, *ptile_to;
3978 const char *nation_from, *nation_to;
3979 struct city *rcity = NULL;
3980 int to_id = pcity_to->id;
3981 const struct civ_map *nmap = &(wld.map);
3982
3983 if (!pcity_from || !pcity_to) {
3984 return FALSE;
3985 }
3986
3990 /* We copy that, because city_link always returns the same pointer. */
3997
3998 /* Check food supply in the receiver city */
4000 bool migration = FALSE;
4001
4002 if (pcity_to->surplus[O_FOOD] >= game.info.food_cost) {
4003 migration = TRUE;
4004 } else {
4005 /* Check if there is a free tile for the new citizen which, when worked,
4006 * leads to zero or positive food surplus for the enlarged city */
4007 int max_food_tile = -1; /* no free tile */
4008
4010 city_tile(pcity_to), ptile) {
4011 if (city_can_work_tile(pcity_to, ptile)
4012 && tile_worked(ptile) != pcity_to) {
4013 /* Safest assumption is that city won't be celebrating once an
4014 * additional citizen is added */
4017 }
4019 if (max_food_tile >= 0
4020 && pcity_to->surplus[O_FOOD] + max_food_tile >= game.info.food_cost) {
4021 migration = TRUE;
4022 }
4023 }
4024
4025 if (!migration) {
4026 /* insufficiency food in receiver city; no additional citizens */
4027 if (pplayer_from == pplayer_to) {
4028 /* migration between one nation */
4030 /* TRANS: From <city1> to <city2>. */
4031 _("Migrants from %s can't go to %s because there is "
4032 "not enough food available!"),
4034 } else {
4035 /* migration between different nations */
4037 /* TRANS: From <city1> to <city2> (<city2 nation adjective>). */
4038 _("Migrants from %s can't go to %s (%s) because there "
4039 "is not enough food available!"),
4042 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
4043 _("Migrants from %s (%s) can't go to %s because there "
4044 "is not enough food available!"),
4046 }
4047
4048 return FALSE;
4049 }
4050 }
4051
4053 /* receiver city can't grow */
4054 if (pplayer_from == pplayer_to) {
4055 /* migration between one nation */
4057 /* TRANS: From <city1> to <city2>. */
4058 _("Migrants from %s can't go to %s because it needs "
4059 "an improvement to grow!"),
4061 } else {
4062 /* migration between different nations */
4064 /* TRANS: From <city1> to <city2> of <city2 nation adjective>. */
4065 _("Migrants from %s can't go to %s (%s) because it "
4066 "needs an improvement to grow!"),
4069 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
4070 _("Migrants from %s (%s) can't go to %s because it "
4071 "needs an improvement to grow!"),
4073 }
4074
4075 return FALSE;
4076 }
4077
4078 /* reduce size of giver */
4079 if (city_size_get(pcity_from) == 1) {
4080
4082 /* Preserve nationality of city's only citizen */
4084 }
4085
4086 /* do not destroy wonders */
4087 city_built_iterate(pcity_from, pimprove) {
4088 if (is_wonder(pimprove)) {
4089 return FALSE;
4090 }
4092
4093 /* find closest city other of the same player than pcity_from */
4095 FALSE, FALSE, TRUE, FALSE, NULL);
4096
4097 if (rcity) {
4098 int id = pcity_from->id;
4099
4100 /* transfer all units to the closest city */
4102 pcity_from->units_supported, rcity, pcity_from,
4103 -1, TRUE);
4105
4106 script_server_signal_emit("city_size_change", pcity_from,
4107 (lua_Integer)(-1), "migration_from");
4108
4109 if (city_exist(id)) {
4110 script_server_signal_emit("city_destroyed", pcity_from,
4111 pcity_from->owner, NULL);
4112
4113 if (city_exist(id)) {
4115 }
4116 }
4117
4119 _("%s was disbanded by its citizens."),
4120 name_from);
4121 } else {
4122 /* it's the only city of the nation */
4123 return FALSE;
4124 }
4125 } else {
4126 /* the migrants take half of the food box with them (this prevents
4127 * migration -> grow -> migration -> ... cycles) */
4128 pcity_from->food_stock /= 2;
4129
4131 /* Those citizens that are from the target nation are most
4132 * ones migrating. */
4133 if (citizens_nation_get(pcity_from, pplayer_to->slot) > 0) {
4135 } else if (!citizens_nation_get(pcity_from, pplayer_citizen->slot)) {
4136 /* No native citizens at all in the city, choose random foreigner */
4138
4140 }
4141 /* This should be followed by city_reduce_size(). */
4143 }
4144 city_reduce_size(pcity_from, 1, pplayer_from, "migration_from");
4146 if (city_refresh(pcity_from)) {
4148 }
4149 }
4150
4151 /* This should be _before_ the size of the city is increased. Thus, the
4152 * order of the messages is correct (1: migration; 2: increased size). */
4153 if (pplayer_from == pplayer_to) {
4154 /* migration between one nation */
4156 /* TRANS: From <city1> to <city2>. */
4157 _("Migrants from %s moved to %s in search of a better "
4158 "life."), name_from, name_to);
4159 } else {
4160 /* migration between different nations */
4162 /* TRANS: From <city1> to <city2> (<city2 nation adjective>). */
4163 _("Migrants from %s moved to %s (%s) in search of a "
4164 "better life."),
4167 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
4168 _("Migrants from %s (%s) moved to %s in search of a "
4169 "better life."),
4171 }
4172
4173 /* Increase size of receiver city */
4174 if (city_exist(to_id)) {
4176
4177 if (city_exist(to_id)) {
4180 if (city_refresh(pcity_to)) {
4182 }
4183 if (incr_success) {
4184 script_server_signal_emit("city_size_change", pcity_to,
4185 (lua_Integer)1, "migration_to");
4186 }
4187 }
4188 }
4189
4190 log_debug("[M] T%d migration successful (%s -> %s)",
4192
4193 return TRUE;
4194}
4195
4196/**********************************************************************/
4218{
4219 bool internat = FALSE;
4220
4221 if (!game.server.migration) {
4222 return FALSE;
4223 }
4224
4226 || (game.server.mgr_worldchance <= 0
4227 && game.server.mgr_nationchance <= 0)) {
4228 return FALSE;
4229 }
4230
4231 /* check for migration */
4232 players_iterate(pplayer) {
4233 if (!pplayer->cities) {
4234 continue;
4235 }
4236
4237 if (check_city_migrations_player(pplayer)) {
4238 internat = TRUE;
4239 }
4241
4242 return internat;
4243}
4244
4245/**********************************************************************/
4249bool city_empty_food_stock(struct city *pcity) {
4250 struct player *pplayer = city_owner(pcity);
4251 struct tile *ptile = city_tile(pcity);
4252
4253 fc_assert_ret_val(pcity != NULL, FALSE);
4254
4255 if (pcity->food_stock > 0) {
4256 pcity->food_stock = 0;
4257
4258 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4259 /* TRANS: %s is a city name */
4260 _("All stored food destroyed in %s."), city_link(pcity));
4261
4262 return TRUE;
4263 }
4264
4265 return FALSE;
4266}
4267
4268/**********************************************************************/
4271static void apply_disaster(struct city *pcity, struct disaster_type *pdis)
4272{
4273 struct player *pplayer = city_owner(pcity);
4274 struct tile *ptile = city_tile(pcity);
4276
4277 log_debug("%s at %s", disaster_rule_name(pdis), city_name_get(pcity));
4278
4279 notify_player(pplayer, ptile, E_DISASTER,
4280 ftc_server,
4281 /* TRANS: Disasters such as Earthquake */
4282 _("%s was hit by %s."), city_name_get(pcity),
4284
4286 if (place_pollution(pcity, EC_POLLUTION)) {
4287 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4288 _("Pollution near %s."), city_link(pcity));
4290 }
4291 }
4292
4294 if (place_pollution(pcity, EC_FALLOUT)) {
4295 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4296 _("Fallout near %s."), city_link(pcity));
4298 }
4299 }
4300
4303 && pcity->size > 1)) {
4304 if (!city_reduce_size(pcity, 1, NULL, "disaster")) {
4305 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4306 /* TRANS: "Industrial Accident destroys Bogota entirely." */
4307 _("%s destroys %s entirely."),
4309 pcity = NULL;
4310 } else {
4311 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4312 /* TRANS: "Nuclear Accident ... Montreal." */
4313 _("%s causes population loss in %s."),
4315 }
4316
4318 }
4319
4321 int total = 0;
4322 struct impr_type *imprs[B_LAST];
4323
4324 city_built_iterate(pcity, pimprove) {
4325 if (is_improvement(pimprove)
4326 && !improvement_has_flag(pimprove, IF_DISASTER_PROOF)) {
4327 imprs[total++] = pimprove;
4328 }
4330
4331 if (total > 0) {
4332 int num = fc_rand(total);
4333
4334 building_lost(pcity, imprs[num], "disaster", NULL);
4335
4336 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4337 /* TRANS: second %s is the name of a city improvement */
4338 _("%s destroys %s in %s."),
4341 city_link(pcity));
4342
4344 }
4345 }
4346
4348 if (city_empty_food_stock(pcity)) {
4350 }
4351 }
4352
4354 if (pcity->shield_stock > 0) {
4355 char prod[256];
4356
4357 pcity->shield_stock = 0;
4358 nullify_prechange_production(pcity); /* Make it impossible to recover */
4359
4360 universal_name_translation(&pcity->production, prod, sizeof(prod));
4361 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4362 /* TRANS: "Production of Colossus in Rhodes destroyed." */
4363 _("Production of %s in %s destroyed."),
4364 prod, city_link(pcity));
4365
4367 }
4368 }
4369
4370 script_server_signal_emit("disaster_occurred", pdis, pcity,
4372 script_server_signal_emit("disaster", pdis, pcity);
4373}
4374
4375/**********************************************************************/
4379{
4380 if (game.info.disasters == 0) {
4381 /* Shortcut out as no disaster is possible. */
4382 return;
4383 }
4384
4385 players_iterate(pplayer) {
4386 /* Safe city iterator needed as disaster may destroy city */
4387 city_list_iterate_safe(pplayer->cities, pcity) {
4388 int id = pcity->id;
4389
4391 if (city_exist(id)) {
4392 /* City survived earlier disasters. */
4393 int probability = game.info.disasters * pdis->frequency;
4394 int result = fc_rand(DISASTER_BASE_RARITY);
4395
4396 if (result < probability) {
4397 if (can_disaster_happen(pdis, pcity)) {
4398 apply_disaster(pcity, pdis);
4399 }
4400 }
4401 }
4405}
4406
4407/**********************************************************************/
4415static bool check_city_migrations_player(const struct player *pplayer)
4416{
4420 float score_from, score_tmp, weight;
4421 int dist, mgr_dist;
4422 bool internat = FALSE;
4423
4424 /* check for each city
4425 * city_list_iterate_safe_end must be used because we could
4426 * remove one city from the list */
4427 city_list_iterate_safe(pplayer->cities, pcity) {
4428 /* no migration out of the capital */
4429 if (is_capital(pcity)) {
4430 continue;
4431 }
4432
4433 /* check only each (game.server.mgr_turninterval) turn
4434 * (counted from the funding turn) and do not migrate
4435 * the same turn a city is founded */
4436 if (game.info.turn == pcity->turn_founded
4437 || ((game.info.turn - pcity->turn_founded)
4438 % game.server.mgr_turninterval) != 0) {
4439 continue;
4440 }
4441
4446
4447 /* score of the actual city
4448 * taking into account a persistence factor of 3 */
4449 score_from = city_migration_score(pcity) * 3;
4450
4451 log_debug("[M] T%d check city: %s score: %6.3f (%s)",
4453 player_name(pplayer));
4454
4455 /* consider all cities within the maximal possible distance
4456 * (= CITY_MAP_MAX_RADIUS + GAME_MAX_MGR_DISTANCE) */
4457 iterate_outward(&(wld.map), city_tile(pcity),
4459 acity = tile_city(ptile);
4460
4461 if (!acity || acity == pcity) {
4462 /* no city or the city in the center */
4463 continue;
4464 }
4465
4466 /* Calculate the migration distance. The value of
4467 * game.server.mgr_distance is added to the current city radius. If the
4468 * distance between both cities is lower or equal than this value,
4469 * migration is possible. */
4472
4473 /* distance between the two cities */
4474 dist = real_map_distance(city_tile(pcity), city_tile(acity));
4475
4476 if (dist > mgr_dist) {
4477 /* to far away */
4478 continue;
4479 }
4480
4481 /* score of the second city, weighted by the distance */
4482 weight = ((float) (mgr_dist + 1 - dist) / (float) (mgr_dist + 1));
4484
4485 log_debug("[M] T%d - compare city: %s (%s) dist: %d mgr_dist: %d "
4486 "score: %6.3f", game.info.turn, city_name_get(acity),
4488
4489 if (game.server.mgr_nationchance > 0 && city_owner(acity) == pplayer) {
4490 /* migration between cities of the same owner */
4492 /* select the best! */
4495
4496 log_debug("[M] T%d - best city (player): %s (%s) score: "
4497 "%6.3f (> %6.3f)", game.info.turn,
4500 }
4501 } else if (game.server.mgr_worldchance > 0
4502 && city_owner(acity) != pplayer) {
4503 /* migration between cities of different owners */
4505 /* Modify the score if citizens could migrate to a city of their
4506 * original nation. */
4507 if (citizens_nation_get(pcity, city_owner(acity)->slot) > 0) {
4508 score_tmp *= 2;
4509 }
4510 }
4511
4513 /* select the best! */
4516
4517 log_debug("[M] T%d - best city (world): %s (%s) score: "
4518 "%6.3f (> %6.3f)", game.info.turn,
4522 }
4523 }
4525
4526 if (best_city_player != NULL) {
4527 /* First, do the migration within one nation */
4528 if (fc_rand(100) >= game.server.mgr_nationchance) {
4529 /* No migration */
4530 /* N.B.: city_link always returns the same pointer. */
4533 _("Citizens of %s are thinking about migrating to %s "
4534 "for a better life."),
4536 } else {
4538 }
4539
4540 /* Stop here */
4541 continue;
4542 }
4543
4544 if (best_city_world != NULL) {
4545 /* Second, do the migration between all nations */
4546 if (fc_rand(100) >= game.server.mgr_worldchance) {
4547 const char *nname;
4548
4550 /* No migration */
4551 /* N.B.: city_link always returns the same pointer. */
4554 /* TRANS: <city1> to <city2> (<city2 nation adjective>). */
4555 _("Citizens of %s are thinking about migrating to %s "
4556 "(%s) for a better life."),
4558 } else {
4560 internat = TRUE;
4561 }
4562
4563 /* Stop here */
4564 continue;
4565 }
4567
4568 return internat;
4569}
4570
4571/**********************************************************************/
4574void city_style_refresh(struct city *pcity)
4575{
4576 pcity->style = city_style(pcity);
4577}
4578
4579/**********************************************************************/
4583void city_counters_refresh(struct city *pcity)
4584{
4586 struct packet_city_update_counters packet;
4587
4588 packet.city = pcity->id;
4589
4591
4592 packet.count = counter_count;
4593 for (i = 0; i < counter_count; i++) {
4594 packet.counters[i] = pcity->counter_values[i];
4595 }
4596
4599}
4600
4601/**********************************************************************/
4604void city_tc_effect_refresh(struct player *pplayer)
4605{
4606 const struct civ_map *nmap = &(wld.map);
4607
4608 city_list_iterate(pplayer->cities, pcity) {
4609 bool changed = FALSE;
4610
4612 city_tile(pcity), ptile, idx, x, y) {
4613 if (ptile->worked == pcity
4614 && get_city_tile_output_bonus(pcity, ptile, NULL, EFT_TILE_WORKABLE) <= 0) {
4615 city_map_update_empty(pcity, ptile);
4616 pcity->specialists[DEFAULT_SPECIALIST]++;
4617 changed = TRUE;
4618 }
4620
4621 if (changed) {
4622 auto_arrange_workers(pcity);
4623 send_city_info(NULL, pcity);
4624 }
4626}
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:3763
static void uk_rem_gold_append(struct unit *punit)
Definition cityturn.c:3078
static bool worklist_change_build_target(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2172
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:2979
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:4271
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:3091
static bool place_pollution(struct city *pcity, enum extra_cause cause)
Definition cityturn.c:3376
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:3057
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:3294
static void update_city_activity(struct city *pcity)
Definition cityturn.c:3553
static void upgrade_unit_prod(struct city *pcity)
Definition cityturn.c:2500
static float city_migration_score(struct city *pcity)
Definition cityturn.c:3888
void nullify_prechange_production(struct city *pcity)
Definition cityturn.c:3544
bool city_empty_food_stock(struct city *pcity)
Definition cityturn.c:4249
bool check_city_migrations(void)
Definition cityturn.c:4217
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:3197
bool city_change_size(struct city *pcity, citizens size, struct player *nationality, const char *reason)
Definition cityturn.c:1032
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:1083
static void city_refresh_after_city_size_increase(struct city *pcity, struct player *nationality)
Definition cityturn.c:998
static void define_orig_production_values(struct city *pcity)
Definition cityturn.c:3513
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:4583
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:3114
int city_incite_cost(struct player *pplayer, struct city *pcity)
Definition cityturn.c:3432
static void nullify_caravan_and_disband_plus(struct city *pcity)
Definition cityturn.c:3534
static bool sell_random_building(struct player *pplayer, struct cityimpr_list *imprs)
Definition cityturn.c:3011
static bool disband_city(struct city *pcity)
Definition cityturn.c:3776
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 bool city_increase_size(struct city *pcity)
Definition cityturn.c:921
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:3260
void city_style_refresh(struct city *pcity)
Definition cityturn.c:4574
static bool city_balance_treasury_units(struct city *pcity)
Definition cityturn.c:3341
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:3972
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:4604
static void check_pollution(struct city *pcity)
Definition cityturn.c:3415
static void city_reset_foodbox(struct city *pcity, int new_size, bool shrink)
Definition cityturn.c:906
void check_disasters(void)
Definition cityturn.c:4378
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:1160
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:4415
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
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:114
#define tile_has_extra(ptile, pextra)
Definition tile.h:147
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:2253
#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:1763
bool place_unit(struct unit *punit, struct player *pplayer, struct city *pcity, struct unit *ptrans, bool force)
Definition unittools.c:1705
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:1657
void unit_unset_removal_callback(struct unit *punit)
Definition unittools.c:1777
void wipe_unit(struct unit *punit, enum unit_loss_reason reason, struct player *killer)
Definition unittools.c:2131
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