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
163 pcity->server.needs_refresh = FALSE;
164
166 city_units_upkeep(pcity); /* update unit upkeep */
168 city_style_refresh(pcity);
169
170 if (retval) {
171 /* Force a sync of the city after the change. */
172 send_city_info(city_owner(pcity), pcity);
173 }
174
175 return retval;
176}
177
178/**********************************************************************/
182void city_refresh_for_player(struct player *pplayer)
183{
185 city_list_iterate(pplayer->cities, pcity) {
186 if (city_refresh(pcity)) {
188 }
189 send_city_info(pplayer, pcity);
192}
193
194/**********************************************************************/
197void city_refresh_queue_add(struct city *pcity)
198{
199 if (NULL == city_refresh_queue) {
201 } else if (city_list_find_number(city_refresh_queue, pcity->id)) {
202 return;
203 }
204
206 pcity->server.needs_refresh = TRUE;
207}
208
209/**********************************************************************/
214{
215 if (NULL == city_refresh_queue) {
216 return;
217 }
218
220 if (pcity->server.needs_refresh) {
221 if (city_refresh(pcity)) {
223 }
224 send_city_info(city_owner(pcity), pcity);
225 }
227
230}
231
232/**********************************************************************/
235void remove_obsolete_buildings_city(struct city *pcity, bool refresh)
236{
237 struct player *pplayer = city_owner(pcity);
238 bool sold = FALSE;
239
240 city_built_iterate(pcity, pimprove) {
241 if (improvement_obsolete(pplayer, pimprove, pcity)
242 && can_city_sell_building(pcity, pimprove)) {
243 int sgold;
244
245 do_sell_building(pplayer, pcity, pimprove, "obsolete");
246 sgold = impr_sell_gold(pimprove);
248 PL_("%s is selling %s (obsolete) for %d.",
249 "%s is selling %s (obsolete) for %d.",
250 sgold),
251 city_link(pcity),
253 sgold);
254 sold = TRUE;
255 }
257
258 if (sold && refresh) {
259 if (city_refresh(pcity)) {
261 }
262 send_city_info(pplayer, pcity);
263 send_player_info_c(pplayer, NULL); /* Send updated gold to all */
264 }
265}
266
267/**********************************************************************/
271{
272 city_list_iterate(pplayer->cities, pcity) {
275}
276
277/**********************************************************************/
281void apply_cmresult_to_city(struct city *pcity,
282 const struct cm_result *cmr)
283{
284 struct tile *pcenter = city_tile(pcity);
285 const struct civ_map *nmap = &(wld.map);
286
287 /* Now apply results */
289 ptile, idx, x, y) {
290 struct city *pwork = tile_worked(ptile);
291
292 if (cmr->worker_positions[idx]) {
293 if (NULL == pwork) {
294 city_map_update_worker(pcity, ptile);
295 } else {
296 fc_assert(pwork == pcity);
297 }
298 } else {
299 if (pwork == pcity) {
300 city_map_update_empty(pcity, ptile);
301 }
302 }
304
306 pcity->specialists[sp] = cmr->specialists[sp];
308}
309
310/**********************************************************************/
314 struct city *pcity)
315{
316 int csize = city_size_get(pcity);
318
319 cmp->require_happy = FALSE;
320 cmp->allow_disorder = FALSE;
321 cmp->allow_specialists = TRUE;
322
323 /* We used to look at pplayer->ai.xxx_priority to determine the values
324 * to be used here. However that doesn't work at all because those values
325 * are on a different scale. Later the AI may wish to adjust its
326 * priorities - this should be done via a separate set of variables. */
327 if (csize > 1) {
328 if (csize <= game.info.notradesize) {
329 cmp->factor[O_FOOD] = 15;
330 } else {
331 if (gsize == pcity->food_stock) {
332 /* We don't need more food if the granary is full. */
333 cmp->factor[O_FOOD] = 0;
334 } else {
335 cmp->factor[O_FOOD] = 10;
336 }
337 }
338 } else {
339 /* Growing to size 2 is the highest priority. */
340 cmp->factor[O_FOOD] = 20;
341 }
342
343 cmp->factor[O_SHIELD] = 5;
344 cmp->factor[O_TRADE] = 0; /* Trade only provides gold/science. */
345 cmp->factor[O_GOLD] = 2;
346 cmp->factor[O_LUXURY] = 0; /* Luxury only influences happiness. */
347 cmp->factor[O_SCIENCE] = 2;
348 cmp->happy_factor = 0;
349
350 if (gsize == pcity->food_stock) {
351 cmp->minimal_surplus[O_FOOD] = 0;
352 } else {
353 cmp->minimal_surplus[O_FOOD] = 1;
354 }
355
356 cmp->minimal_surplus[O_SHIELD] = 1;
357 cmp->minimal_surplus[O_TRADE] = 0;
358 cmp->minimal_surplus[O_GOLD] = -FC_INFINITY;
359 cmp->minimal_surplus[O_LUXURY] = 0;
360 cmp->minimal_surplus[O_SCIENCE] = 0;
361}
362
363/**********************************************************************/
366void auto_arrange_workers(struct city *pcity)
367{
368 struct cm_parameter cmp;
369 struct cm_parameter *pcmp;
370 struct cm_result *cmr;
371 bool broadcast_needed;
372
373 /* See comment in freeze_workers(): we can't rearrange while
374 * workers are frozen (i.e. multiple updates need to be done). */
375 if (pcity->server.workers_frozen > 0) {
376 if (pcity->server.needs_arrange == CNA_NOT) {
378 }
379 return;
380 }
382
384
385 /* Freeze the workers and make sure all the tiles around the city
386 * are up to date. Then thaw, but hackishly make sure that thaw
387 * doesn't call us recursively, which would waste time. */
388 city_freeze_workers(pcity);
390
391 city_map_update_all(pcity);
392
394 city_thaw_workers(pcity);
395
396 /* Now start actually rearranging. */
397 city_refresh(pcity);
398
399 sanity_check_city(pcity);
400 cm_clear_cache(pcity);
401
402 if (pcity->cm_parameter) {
403 pcmp = pcity->cm_parameter;
404 } else {
405 pcmp = &cmp;
408 }
409
410 /* This must be after city_refresh() so that the result gets created for the right
411 * city radius */
412 cmr = cm_result_new(pcity);
413 cm_query_result(pcity, pcmp, cmr, FALSE);
414
415 if (!cmr->found_a_valid) {
416 if (pcity->cm_parameter) {
417 /* If player-defined parameters fail, cancel and notify player. */
418 free(pcity->cm_parameter);
419 pcity->cm_parameter = NULL;
420
421 notify_player(city_owner(pcity), city_tile(pcity),
423 _("The citizen governor can't fulfill the requirements "
424 "for %s. Passing back control."),
425 city_link(pcity));
426
427 /* Switch to default parameters, and try with them */
428 pcmp = &cmp;
431 cm_query_result(pcity, pcmp, cmr, FALSE);
432 }
433
434 if (!cmr->found_a_valid) {
435 /* Drop surpluses and try again. */
437 cmp.minimal_surplus[o] = 0;
439 cmp.minimal_surplus[O_GOLD] = -FC_INFINITY;
440 cm_query_result(pcity, pcmp, cmr, FALSE);
441 }
442 }
443 if (!cmr->found_a_valid) {
444 /* Emergency management. Get _some_ result. This doesn't use
445 * cm_init_emergency_parameter() so we can keep the factors from
446 * above. */
448 cmp.minimal_surplus[o] = MIN(cmp.minimal_surplus[o],
449 MIN(pcity->surplus[o], 0));
451 cmp.require_happy = FALSE;
452 cmp.allow_disorder = is_ai(city_owner(pcity)) ? FALSE : TRUE;
453 cm_query_result(pcity, pcmp, cmr, FALSE);
454 }
455 if (!cmr->found_a_valid) {
456 CITY_LOG(LOG_DEBUG, pcity, "emergency management");
457 pcmp = &cmp;
459 cm_query_result(pcity, pcmp, cmr, TRUE);
460 }
461 fc_assert_ret(cmr->found_a_valid);
462
464
465 if (pcity->server.debug) {
466 /* Print debug output if requested. */
467 cm_print_city(pcity);
469 }
470
471 if (city_refresh(pcity)) {
472 log_error("%s radius changed when already arranged workers.",
473 city_name_get(pcity));
474 /* Can't do anything - don't want to enter infinite recursive loop
475 * by trying to arrange workers more. */
476 }
477 sanity_check_city(pcity);
478
479 if (broadcast_needed) {
480 broadcast_city_info(pcity);
481 }
482
485}
486
487/**********************************************************************/
490static void city_global_turn_notify(struct conn_list *dest)
491{
492 cities_iterate(pcity) {
493 const struct impr_type *pimprove = pcity->production.value.building;
494
495 if (VUT_IMPROVEMENT == pcity->production.kind
496 && is_great_wonder(pimprove)
497 && (1 >= city_production_turns_to_build(pcity, TRUE)
498 && can_city_build_improvement_now(pcity, pimprove))) {
499 notify_conn(dest, city_tile(pcity),
501 _("Notice: Wonder %s in %s will be finished next turn."),
502 improvement_name_translation(pimprove), city_link(pcity));
503 }
505}
506
507/**********************************************************************/
511static void city_turn_notify(const struct city *pcity,
512 struct conn_list *dest,
513 const struct player *cache_for_player)
514{
515 const struct impr_type *pimprove = pcity->production.value.building;
516 struct packet_chat_msg packet;
518
519 if (0 < pcity->surplus[O_FOOD]) {
521 - pcity->food_stock - 1) / pcity->surplus[O_FOOD];
522
523 if (get_city_bonus(pcity, EFT_GROWTH_FOOD) <= 0
526 && 0 < pcity->surplus[O_SHIELD]) {
527 /* From the check above, the surplus must always be positive. */
528 turns_granary = (impr_build_shield_cost(pcity, pimprove)
529 - pcity->shield_stock) / pcity->surplus[O_SHIELD];
530 /* If growth and granary completion occur simultaneously, granary
531 * preserves food. -AJS. */
532 if (5 > turns_growth && 5 > turns_granary
534 package_event(&packet, city_tile(pcity),
536 _("Suggest throttling growth in %s to use %s "
537 "(being built) more effectively."),
538 city_link(pcity),
540 lsend_packet_chat_msg(dest, &packet);
541 if (NULL != cache_for_player) {
543 }
544 }
545 }
546
547 if (0 >= turns_growth && !city_celebrating(pcity)
548 && city_can_grow_to(pcity, city_size_get(pcity) + 1)) {
549 package_event(&packet, city_tile(pcity),
551 _("%s may soon grow to size %i."),
552 city_link(pcity), city_size_get(pcity) + 1);
553 lsend_packet_chat_msg(dest, &packet);
554 if (NULL != cache_for_player) {
556 }
557 }
558 } else {
559 if (0 >= pcity->food_stock + pcity->surplus[O_FOOD]
560 && 0 > pcity->surplus[O_FOOD]) {
561 package_event(&packet, city_tile(pcity),
563 _("Warning: Famine feared in %s."), city_link(pcity));
564 lsend_packet_chat_msg(dest, &packet);
565 if (NULL != cache_for_player) {
567 }
568 }
569 }
570}
571
572/**********************************************************************/
577{
578 if (NULL != pconn) {
579 struct player *pplayer = conn_get_player(pconn);
580
581 if (NULL != pplayer) {
582 city_list_iterate(pplayer->cities, pcity) {
583 city_turn_notify(pcity, pconn->self, NULL);
585 }
587 } else {
588 players_iterate(pplayer) {
589 city_list_iterate(pplayer->cities, pcity) {
590 city_turn_notify(pcity, pplayer->connections, pplayer);
593 /* NB: notifications to 'game.est_connections' are automatically
594 * cached. */
596 }
597}
598
599/**********************************************************************/
602void update_city_activities(struct player *pplayer)
603{
604 int n;
605
606 fc_assert(NULL != pplayer->cities);
607
608 n = city_list_size(pplayer->cities);
609
610 if (n > 0) {
611 struct city *cities[n];
612 int i = 0, r;
613
614 city_list_iterate(pplayer->cities, pcity) {
615
616 citizens_convert(pcity);
617
618 /* Cancel trade routes that cannot exist any more */
620 struct city *tcity = game_city_by_number(proute->partner);
621
622 if (tcity != NULL) {
623 bool cancel = FALSE;
624
625 if (proute->dir != RDIR_FROM && goods_has_flag(proute->goods, GF_DEPLETES)
626 && !goods_can_be_provided(tcity, proute->goods, NULL)) {
627 cancel = TRUE;
628 }
629 if (!cancel && !can_cities_trade(pcity, tcity)) {
632
633 if (settings->cancelling == TRI_CANCEL) {
634 cancel = TRUE;
635 }
636 }
637
638 if (cancel) {
639 struct trade_route *back;
640
642 free(proute);
643 free(back);
644 }
645 }
647
648 /* Add cities to array for later random order handling */
649 cities[i++] = pcity;
651
652 /* How gold upkeep is handled depends on the setting
653 * 'game.info.gold_upkeep_style':
654 * GOLD_UPKEEP_CITY: Each city tries to balance its upkeep individually
655 * (this is done in update_city_activity()).
656 * GOLD_UPKEEP_MIXED: Each city tries to balance its upkeep for
657 * buildings individually; the upkeep for units is
658 * paid by the nation.
659 * GOLD_UPKEEP_NATION: The nation as a whole balances the treasury. If
660 * the treasury is not balance units and buildings
661 * are sold. */
662
663 /* Iterate over cities in a random order. */
664 while (i > 0) {
665 r = fc_rand(i);
666 /* update unit upkeep */
669 cities[r] = cities[--i];
670 }
671 }
672}
673
674/**********************************************************************/
688 bool wipe_in_the_end)
689{
690 int punit_id;
691
694 /* Can't get rid of this unit. It is undisbandable for the current
695 * situation. */
696 return FALSE;
697 }
698
699 punit_id = punit->id;
700
701 /* Try to perform this unit's can't upkeep actions. */
704 NULL, NULL, NULL, NULL);
705
707 /* No forced action was able to kill the unit. Finish the job. */
709 }
710
711 return !unit_is_alive(punit_id);
712}
713
714/**********************************************************************/
718static citizens city_reduce_specialists(struct city *pcity, citizens change)
719{
720 citizens want = change;
721
722 fc_assert_ret_val(0 < change, 0);
723
725 citizens fix = MIN(want, pcity->specialists[sp]);
726
727 pcity->specialists[sp] -= fix;
728 want -= fix;
730
731 return change - want;
732}
733
734/**********************************************************************/
738static citizens city_reduce_workers(struct city *pcity, citizens change)
739{
740 struct tile *pcenter = city_tile(pcity);
741 int want = change;
742 const struct civ_map *nmap = &(wld.map);
743
744 fc_assert_ret_val(0 < change, 0);
745
747 ptile, _index, _x, _y) {
748 if (0 < want && tile_worked(ptile) == pcity) {
749 city_map_update_empty(pcity, ptile);
750 want--;
751 }
753
754 return change - want;
755}
756
757/**********************************************************************/
763 struct player *destroyer, const char *reason)
764{
766 int old_radius_sq;
767
768 if (pop_loss == 0) {
769 return TRUE;
770 }
771
772 if (city_size_get(pcity) <= pop_loss) {
773 int id = pcity->id;
774
775 citizens_update(pcity, NULL); /* To avoid warnings during the script */
776 /* Won't refresh a doomed city, or should we? */
777 script_server_signal_emit("city_destroyed", pcity, pcity->owner,
778 destroyer);
779
780 if (city_exist(id)) {
781 remove_city(pcity);
782 }
783 return FALSE;
784 }
786 city_size_add(pcity, -pop_loss);
789
790 /* Cap the food stock at the new granary size. */
791 if (pcity->food_stock > city_granary_size(city_size_get(pcity))) {
793 }
794
795 /* First try to kill off the specialists */
797
798 if (loss_remain > 0) {
799 /* Take it out on workers */
800#ifndef FREECIV_NDEBUG
801 loss_remain -=
802#endif /* FREECIV_NDEBUG */
804 }
805
806 /* Update citizens. */
807 citizens_update(pcity, NULL);
808
809 /* Update number of people in each feelings category.
810 * This also updates the city radius if needed. */
811 city_refresh(pcity);
812
814
815 /* Send city data. */
816 sync_cities();
817
819 "city_reduce_size() has remaining"
820 "%d of %d for \"%s\"[%d]",
822 city_name_get(pcity), city_size_get(pcity));
823
824 /* Update cities that have trade routes with us */
826 if (city_refresh(pcity2)) {
827 /* This should never happen, but if it does, make sure not to
828 * leave workers outside city radius. */
830 }
832
833 sanity_check_city(pcity);
834
835 if (reason != NULL) {
836 int id = pcity->id;
837
838 script_server_signal_emit("city_size_change", pcity,
840
841 return city_exist(id);
842 }
843
844 return TRUE;
845}
846
847/**********************************************************************/
851void city_repair_size(struct city *pcity, int change)
852{
853 if (change > 0) {
854 pcity->specialists[DEFAULT_SPECIALIST] += change;
855 } else if (change < 0) {
856 int need = change + city_reduce_specialists(pcity, -change);
857
858 if (0 > need) {
859#ifndef FREECIV_NDEBUG
860 need +=
861#endif /* FREECIV_NDEBUG */
862 city_reduce_workers(pcity, -need);
863 }
864
865 fc_assert_msg(0 == need,
866 "city_repair_size() has remaining %d of %d for \"%s\"[%d]",
867 need, change, city_name_get(pcity), city_size_get(pcity));
868 }
869}
870
871/**********************************************************************/
878int city_growth_granary_savings(const struct city *pcity)
879{
881
882 return CLIP(0, savings, 100);
883}
884
885/**********************************************************************/
892int city_shrink_granary_savings(const struct city *pcity)
893{
895
896 return CLIP(0, savings, 100);
897}
898
899/**********************************************************************/
905static void city_reset_foodbox(struct city *pcity, int new_size,
906 bool shrink)
907{
908 int savings_pct = ( shrink
911
913}
914
915/**********************************************************************/
920static bool city_increase_size(struct city *pcity)
921{
922 int new_food;
924 bool have_square = FALSE;
925 bool rapture_grow = city_rapture_grow(pcity); /* Check before size increase! */
926 struct tile *pcenter = city_tile(pcity);
927 struct player *powner = city_owner(pcity);
928 const struct impr_type *pimprove = pcity->production.value.building;
929 const struct civ_map *nmap = &(wld.map);
930
931 if (!city_can_grow_to(pcity, city_size_get(pcity) + 1)) {
932 /* Need improvement */
936 _("%s needs %s (being built) to grow beyond size %d."),
937 city_link(pcity),
939 city_size_get(pcity));
940 } else {
942 _("%s needs an improvement to grow beyond size %d."),
943 city_link(pcity), city_size_get(pcity));
944 }
945 /* Granary can only hold so much */
947 * (100 * 100 - game.server.aqueductloss * (100 - savings_pct))
948 / (100 * 100));
949 pcity->food_stock = MIN(pcity->food_stock, new_food);
950
951 return FALSE;
952 }
953
954 city_size_add(pcity, 1);
955
956 /* Do not empty food stock if city is growing by celebrating */
957 if (rapture_grow) {
959 } else {
961 }
962 pcity->food_stock = MIN(pcity->food_stock, new_food);
963
964 /* If there is enough food, and the city is big enough,
965 * make new citizens into scientists or taxmen -- Massimo */
966
967 /* Ignore food if no square can be worked */
969 ptile, _index, _x, _y) {
970 if (tile_worked(ptile) != pcity /* Quick test */
971 && city_can_work_tile(pcity, ptile)) {
973 }
975
976 if ((pcity->surplus[O_FOOD] >= 2 || !have_square)
978 pcity->specialists[best_specialist(O_SCIENCE, pcity)]++;
979 } else if ((pcity->surplus[O_FOOD] >= 2 || !have_square)
981 pcity->specialists[best_specialist(O_GOLD, pcity)]++;
982 } else {
983 pcity->specialists[DEFAULT_SPECIALIST]++; /* or else city is !sane */
984 }
985
986 /* Deprecated signal. Connect your lua functions to "city_size_change" that's
987 * emitted from calling functions which know the 'reason' of the increase. */
988 script_server_signal_emit("city_growth", pcity,
989 (lua_Integer)city_size_get(pcity));
990
991 return TRUE;
992}
993
994/**********************************************************************/
998 struct player *nationality)
999{
1000 struct player *powner = city_owner(pcity);
1001
1002 /* Update citizens. */
1003 citizens_update(pcity, nationality);
1004
1005 /* Refresh the city data; this also checks the squared city radius. */
1006 city_refresh(pcity);
1007
1008 auto_arrange_workers(pcity);
1009
1010 /* Update cities that have trade routes with us */
1012 if (city_refresh(pcity2)) {
1013 /* This should never happen, but if it does, make sure not to
1014 * leave workers outside city radius. */
1016 }
1018
1020 _("%s grows to size %d."),
1021 city_link(pcity), city_size_get(pcity));
1022
1023 sanity_check_city(pcity);
1024
1025 sync_cities();
1026}
1027
1028/**********************************************************************/
1032 struct player *nationality, const char *reason)
1033{
1034 int change = size - city_size_get(pcity);
1035
1036 if (change > 0) {
1037 int old_size = city_size_get(pcity);
1038 int real_change;
1039 int current_size = city_size_get(pcity);
1040 int id = pcity->id;
1041
1042 /* Increase city size until size reached, or increase fails */
1043 while (size > current_size && city_increase_size(pcity)) {
1044 /* TODO: This is currently needed only because there's
1045 * deprecated script signal "city_growth" emitted.
1046 * Check the need after signal has been dropped completely. */
1047 if (!city_exist(id)) {
1048 return FALSE;
1049 }
1050
1051 current_size++;
1052 }
1053
1054 city_refresh_after_city_size_increase(pcity, nationality);
1055
1057
1058 if (real_change != 0 && reason != NULL) {
1059 script_server_signal_emit("city_size_change", pcity,
1061
1062 if (!city_exist(id)) {
1063 return FALSE;
1064 }
1065 }
1066 } else if (change < 0) {
1067 /* We assume that city_change_size() is never called because
1068 * of enemy actions. If that changes, enemy must be passed
1069 * to city_reduce_size() */
1070 return city_reduce_size(pcity, -change, NULL, reason);
1071 }
1072
1073 map_claim_border(pcity->tile, pcity->owner, -1);
1074
1075 return TRUE;
1076}
1077
1078/**********************************************************************/
1082static void city_populate(struct city *pcity, struct player *nationality)
1083{
1084 int saved_id = pcity->id;
1085 int granary_size = city_granary_size(city_size_get(pcity));
1086
1087 pcity->food_stock += pcity->surplus[O_FOOD];
1088 if (pcity->food_stock >= granary_size || city_rapture_grow(pcity)) {
1089 if (city_had_recent_plague(pcity)) {
1090 notify_player(city_owner(pcity), city_tile(pcity),
1092 _("A recent plague outbreak prevents growth in %s."),
1093 city_link(pcity));
1094 /* Lose excess food */
1095 pcity->food_stock = MIN(pcity->food_stock, granary_size);
1096 } else {
1097 bool success;
1098
1099 success = city_increase_size(pcity);
1100 map_claim_border(pcity->tile, pcity->owner, -1);
1101
1102 if (success) {
1103 city_refresh_after_city_size_increase(pcity, nationality);
1104 script_server_signal_emit("city_size_change", pcity,
1105 (lua_Integer)1, "growth");
1106 }
1107 }
1108 } else if (pcity->food_stock < 0) {
1109 /* FIXME: should this depend on units with ability to build
1110 * cities or on units that require food in upkeep?
1111 * I'll assume citybuilders (units that 'contain' 1 pop) -- sjolie
1112 * The above may make more logical sense, but in game terms
1113 * you want to disband a unit that is draining your food
1114 * reserves. Hence, I'll assume food upkeep > 0 units. -- jjm
1115 */
1117 if (punit->upkeep[O_FOOD] > 0) {
1118 const char *punit_link = unit_tile_link(punit);
1119
1122 notify_player(city_owner(pcity), city_tile(pcity),
1124 _("Famine feared in %s, %s lost!"),
1125 city_link(pcity), punit_link);
1126 }
1127
1128 if (city_exist(saved_id)) {
1129 city_reset_foodbox(pcity, city_size_get(pcity), TRUE);
1130 }
1131
1132 return;
1133 }
1135 if (city_size_get(pcity) > 1) {
1136 notify_player(city_owner(pcity), city_tile(pcity),
1138 _("Famine causes population loss in %s."),
1139 city_link(pcity));
1140 } else {
1141 notify_player(city_owner(pcity), city_tile(pcity),
1143 _("Famine destroys %s entirely."),
1144 city_link(pcity));
1145 }
1146 city_reset_foodbox(pcity, city_size_get(pcity) - 1, TRUE);
1147 if (city_reduce_size(pcity, 1, NULL, "famine")) {
1148 pcity->had_famine = TRUE;
1149 }
1150 }
1151}
1152
1153/**********************************************************************/
1160 struct city *pcity,
1161 struct player *pplayer,
1162 int saved_id)
1163{
1164 const void *ptarget;
1165 const char *tgt_name;
1166 const struct requirement_vector *build_reqs;
1167 const char *signal_name;
1168 const struct req_context city_ctxt = {
1169 .player = pplayer,
1170 .city = pcity,
1171 .tile = city_tile(pcity)
1172 /* FIXME: Setting .unittype is currently redundant
1173 * but can_city_build_unit_direct() does it */
1174 };
1175 bool purge = FALSE;
1176 bool known = FALSE;
1177
1178 if (pcity->wlcb == WLCB_ALWAYS_PURGE) {
1179 return TRUE;
1180 }
1181
1182 switch (target->kind) {
1183 case VUT_UTYPE:
1184 ptarget = target->value.utype;
1185 build_reqs = &target->value.utype->build_reqs;
1187 signal_name = "unit_cant_be_built";
1188 break;
1189 case VUT_IMPROVEMENT:
1190 ptarget = target->value.building;
1191 build_reqs = &target->value.building->reqs;
1193 signal_name = "building_cant_be_built";
1194 break;
1195 default:
1197 || target->kind == VUT_UTYPE), FALSE);
1198 return FALSE;
1199 }
1200
1201 if (pcity->wlcb == WLCB_ALWAYS_POSTPONE) {
1202 notify_player(pplayer, city_tile(pcity),
1204 _("%s can't build %s from the worklist. "
1205 "Postponing..."),
1206 city_link(pcity),
1207 tgt_name);
1208 return FALSE;
1209 }
1210
1211 requirement_vector_iterate(build_reqs, preq) {
1213 known = TRUE;
1214 switch (preq->source.kind) {
1215 case VUT_COUNTER:
1216 if (preq->present) {
1217 notify_player(pplayer, city_tile(pcity),
1219 _("%s can't build %s from the worklist; "
1220 "counter %s value's checkpoint do not met "
1221 "Postponing..."),
1222 city_link(pcity),
1223 tgt_name,
1225 (preq->source.value.counter));
1226 } else {
1227 purge = TRUE;
1228 }
1229 break;
1230 case VUT_ADVANCE:
1231 if (preq->present) {
1232 notify_player(pplayer, city_tile(pcity),
1234 _("%s can't build %s from the worklist; "
1235 "tech %s not yet available. Postponing..."),
1236 city_link(pcity),
1237 tgt_name,
1239 (preq->source.value.advance));
1241 pcity, "need_tech");
1242 } else {
1243 /* While techs can be unlearned, this isn't useful feedback */
1244 purge = TRUE;
1245 }
1246 break;
1247 case VUT_TECHFLAG:
1248 if (preq->present) {
1249 notify_player(pplayer, city_tile(pcity),
1251 _("%s can't build %s from the worklist; "
1252 "no tech with flag \"%s\" yet available. "
1253 "Postponing..."),
1254 city_link(pcity),
1255 tgt_name,
1256 tech_flag_id_name(preq->source.value.techflag));
1258 pcity, "need_techflag");
1259 } else {
1260 /* While techs can be unlearned, this isn't useful feedback */
1261 purge = TRUE;
1262 }
1263 break;
1264 case VUT_IMPROVEMENT:
1265 if (preq->range == REQ_RANGE_LOCAL) {
1266 /* Building itself is never going to change */
1267 purge = TRUE;
1268 } else {
1269 if (preq->present) {
1270 notify_player(pplayer, city_tile(pcity),
1272 _("%s can't build %s from the worklist; "
1273 "need to have %s first. Postponing..."),
1274 city_link(pcity),
1275 tgt_name,
1277 preq->source.value.building));
1279 pcity, "need_building");
1280 } else {
1281 notify_player(pplayer, city_tile(pcity),
1283 _("%s can't build %s from the worklist; "
1284 "need to not have %s. Postponing..."),
1285 city_link(pcity),
1286 tgt_name,
1288 preq->source.value.building));
1290 pcity, "have_building");
1291 }
1292 }
1293 break;
1294 case VUT_IMPR_GENUS:
1295 if (preq->range == REQ_RANGE_LOCAL) {
1296 /* Building's own genus is never going to change */
1297 purge = TRUE;
1298 } else {
1299 if (preq->present) {
1300 notify_player(pplayer, city_tile(pcity),
1302 _("%s can't build %s from the worklist; "
1303 "need to have %s first. Postponing..."),
1304 city_link(pcity),
1305 tgt_name,
1307 preq->source.value.impr_genus));
1309 pcity, "need_building_genus");
1310 } else {
1311 notify_player(pplayer, city_tile(pcity),
1313 _("%s can't build %s from the worklist; "
1314 "need to not have %s. Postponing..."),
1315 city_link(pcity),
1316 tgt_name,
1318 preq->source.value.impr_genus));
1320 pcity, "have_building_genus");
1321 }
1322 }
1323 break;
1324 case VUT_IMPR_FLAG:
1325 if (preq->range == REQ_RANGE_LOCAL) {
1326 /* Building's own flags are never going to change */
1327 purge = TRUE;
1328 } else {
1329 if (preq->present) {
1330 notify_player(pplayer, city_tile(pcity),
1332 _("%s can't build %s from the worklist; "
1333 "need to have %s first. Postponing..."),
1334 city_link(pcity),
1335 tgt_name,
1337 preq->source.value.impr_flag));
1339 pcity, "need_building_flag");
1340 } else {
1341 notify_player(pplayer, city_tile(pcity),
1343 _("%s can't build %s from the worklist; "
1344 "need to not have %s. Postponing..."),
1345 city_link(pcity),
1346 tgt_name,
1348 preq->source.value.impr_flag));
1350 pcity, "have_building_flag");
1351 }
1352 }
1353 break;
1354 case VUT_PLAYER_FLAG:
1355 if (preq->present) {
1356 notify_player(pplayer, city_tile(pcity),
1358 _("%s can't build %s from the worklist; "
1359 "need to have %s first. Postponing..."),
1360 city_link(pcity),
1361 tgt_name,
1363 preq->source.value.plr_flag));
1365 pcity, "need_player_flag");
1366 } else {
1367 notify_player(pplayer, city_tile(pcity),
1369 _("%s can't build %s from the worklist; "
1370 "need to not have %s. Postponing..."),
1371 city_link(pcity),
1372 tgt_name,
1374 preq->source.value.plr_flag));
1376 pcity, "have_player_flag");
1377 }
1378 break;
1379 case VUT_PLAYER_STATE:
1380 purge = TRUE;
1381 break;
1382 case VUT_GOVERNMENT:
1383 if (preq->present) {
1384 notify_player(pplayer, city_tile(pcity),
1386 _("%s can't build %s from the worklist; "
1387 "it needs %s government. Postponing..."),
1388 city_link(pcity),
1389 tgt_name,
1390 government_name_translation(preq->source.value.govern));
1392 pcity, "need_government");
1393 } else {
1394 notify_player(pplayer, city_tile(pcity),
1396 _("%s can't build %s from the worklist; "
1397 "it cannot have %s government. Postponing..."),
1398 city_link(pcity),
1399 tgt_name,
1400 government_name_translation(preq->source.value.govern));
1402 pcity, "have_government");
1403 }
1404 break;
1405 case VUT_ACHIEVEMENT:
1406 if (preq->present) {
1407 notify_player(pplayer, city_tile(pcity),
1409 _("%s can't build %s from the worklist; "
1410 "it needs \"%s\" achievement. Postponing..."),
1411 city_link(pcity),
1412 tgt_name,
1413 achievement_name_translation(preq->source.value.achievement));
1415 pcity, "need_achievement");
1416 } else {
1417 /* Can't unachieve things. */
1418 purge = TRUE;
1419 }
1420 break;
1421 case VUT_EXTRA:
1422 if (preq->present) {
1423 notify_player(pplayer, city_tile(pcity),
1425 Q_("?extra:%s can't build %s from the worklist; "
1426 "%s is required. Postponing..."),
1427 city_link(pcity),
1428 tgt_name,
1429 extra_name_translation(preq->source.value.extra));
1431 pcity, "need_extra");
1432 } else {
1433 notify_player(pplayer, city_tile(pcity),
1435 Q_("?extra:%s can't build %s from the worklist; "
1436 "%s is prohibited. Postponing..."),
1437 city_link(pcity),
1438 tgt_name,
1439 extra_name_translation(preq->source.value.extra));
1441 pcity, "have_extra");
1442 }
1443 break;
1444 case VUT_GOOD:
1445 if (preq->present) {
1446 notify_player(pplayer, city_tile(pcity),
1448 Q_("?extra:%s can't build %s from the worklist; "
1449 "%s is required. Postponing..."),
1450 city_link(pcity),
1451 tgt_name,
1452 goods_name_translation(preq->source.value.good));
1454 pcity, "need_good");
1455 } else {
1456 notify_player(pplayer, city_tile(pcity),
1458 Q_("?extra:%s can't build %s from the worklist; "
1459 "%s is prohibited. Postponing..."),
1460 city_link(pcity),
1461 tgt_name,
1462 goods_name_translation(preq->source.value.good));
1464 pcity, "have_good");
1465 }
1466 break;
1467 case VUT_TERRAIN:
1468 if (preq->present) {
1469 notify_player(pplayer, city_tile(pcity),
1471 Q_("?terrain:%s can't build %s from the worklist; "
1472 "%s terrain is required. Postponing..."),
1473 city_link(pcity),
1474 tgt_name,
1475 terrain_name_translation(preq->source.value.terrain));
1477 pcity, "need_terrain");
1478 } else {
1479 notify_player(pplayer, city_tile(pcity),
1481 Q_("?terrain:%s can't build %s from the worklist; "
1482 "%s terrain is prohibited. Postponing..."),
1483 city_link(pcity),
1484 tgt_name,
1485 terrain_name_translation(preq->source.value.terrain));
1487 pcity, "have_terrain");
1488 }
1489 break;
1490 case VUT_NATION:
1491 if (preq->range < REQ_RANGE_TRADE_ROUTE
1492 || preq->range == REQ_RANGE_PLAYER) {
1493 /* At higher ranges, new players with their nations may arrive */
1494 purge = TRUE;
1495 } else {
1496 if (preq->present) {
1497 notify_player(pplayer, city_tile(pcity),
1499 /* TRANS: "%s nation" is adjective */
1500 Q_("?nation:%s can't build %s from the worklist; "
1501 "%s nation is required. Postponing..."),
1502 city_link(pcity),
1503 tgt_name,
1504 nation_adjective_translation(preq->source.value.nation));
1506 pcity, "need_nation");
1507 } else {
1508 notify_player(pplayer, city_tile(pcity),
1510 Q_("?nation:%s can't build %s from the worklist; "
1511 "%s nation is prohibited. Postponing..."),
1512 city_link(pcity),
1513 tgt_name,
1514 nation_adjective_translation(preq->source.value.nation));
1516 pcity, "have_nation");
1517 }
1518 }
1519 break;
1520 case VUT_NATIONGROUP:
1521 if (preq->range < REQ_RANGE_TRADE_ROUTE
1522 || preq->range == REQ_RANGE_PLAYER) {
1523 /* At higher ranges, new players with their nations may arrive */
1524 purge = TRUE;
1525 } else {
1526 if (preq->present) {
1527 notify_player(pplayer, city_tile(pcity),
1529 /* TRANS: "%s nation" is adjective */
1530 Q_("?ngroup:%s can't build %s from the worklist; "
1531 "%s nation is required. Postponing..."),
1532 city_link(pcity),
1533 tgt_name,
1534 nation_group_name_translation(preq->source.value.nationgroup));
1536 pcity, "need_nationgroup");
1537 } else {
1538 notify_player(pplayer, city_tile(pcity),
1540 Q_("?ngroup:%s can't build %s from the worklist; "
1541 "%s nation is prohibited. Postponing..."),
1542 city_link(pcity),
1543 tgt_name,
1544 nation_group_name_translation(preq->source.value.nationgroup));
1546 pcity, "have_nationgroup");
1547 }
1548 }
1549 break;
1550 case VUT_STYLE:
1551 /* FIXME: City styles sometimes change over time, but it isn't
1552 * entirely under player control. Probably better to purge
1553 * with useful explanation. */
1554 if (preq->present) {
1555 notify_player(pplayer, city_tile(pcity),
1557 _("%s can't build %s from the worklist; "
1558 "only %s style cities may build this. Postponing..."),
1559 city_link(pcity),
1560 tgt_name,
1561 style_name_translation(preq->source.value.style));
1563 pcity, "need_style");
1564 } else {
1565 notify_player(pplayer, city_tile(pcity),
1567 _("%s can't build %s from the worklist; "
1568 "%s style cities may not build this. Postponing..."),
1569 city_link(pcity),
1570 tgt_name,
1571 style_name_translation(preq->source.value.style));
1573 pcity, "have_style");
1574 }
1575 break;
1576 case VUT_NATIONALITY:
1577 /* FIXME: Changing citizen nationality is hard: purging might be
1578 * more useful in this case. */
1579 if (preq->present) {
1580 notify_player(pplayer, city_tile(pcity),
1582 /* TRANS: Latter %s is citizen nationality */
1583 _("%s can't build %s from the worklist; "
1584 "only city with %s may build this. Postponing..."),
1585 city_link(pcity),
1586 tgt_name,
1587 nation_plural_translation(preq->source.value.nationality));
1589 pcity, "need_nationality");
1590 } else {
1591 notify_player(pplayer, city_tile(pcity),
1593 /* TRANS: Latter %s is citizen nationality */
1594 _("%s can't build %s from the worklist; "
1595 "only city without %s may build this. Postponing..."),
1596 city_link(pcity),
1597 tgt_name,
1598 nation_plural_translation(preq->source.value.nationality));
1600 pcity, "have_nationality");
1601 }
1602 break;
1603 case VUT_ORIGINAL_OWNER:
1604 /* Original owner of this specific city won't change.
1605 * Update this when supporting ranges other than REQ_RANGE_CITY. */
1606 purge = TRUE;
1607 break;
1608 case VUT_DIPLREL:
1609 case VUT_DIPLREL_TILE: /* The tile owner is the city owner */
1610 case VUT_DIPLREL_TILE_O: /* The tile owner is the city owner */
1611 if (preq->present) {
1612 const char *reason;
1613
1614 notify_player(pplayer, city_tile(pcity),
1616 /* TRANS: '%s' is a wide range of relationships;
1617 * e.g., 'Peace', 'Never met', 'Foreign',
1618 * 'Hosts embassy', 'Provided Casus Belli' */
1619 _("%s can't build %s from the worklist; "
1620 "the relationship '%s' is required."
1621 " Postponing..."),
1622 city_link(pcity),
1623 tgt_name,
1625 preq->source.value.diplrel));
1626
1627 if (preq->source.kind == VUT_DIPLREL_TILE) {
1628 reason = "need_diplrel_tile";
1629 } else if (preq->source.kind == VUT_DIPLREL_TILE_O) {
1630 reason = "need_diplrel_tile_o";
1631 } else {
1632 fc_assert(preq->source.kind == VUT_DIPLREL);
1633 reason = "need_diplrel";
1634 }
1635
1637 pcity, reason);
1638 } else {
1639 const char *reason;
1640
1641 notify_player(pplayer, city_tile(pcity),
1643 _("%s can't build %s from the worklist; "
1644 "the relationship '%s' is prohibited."
1645 " Postponing..."),
1646 city_link(pcity),
1647 tgt_name,
1649 preq->source.value.diplrel));
1650
1651 if (preq->source.kind == VUT_DIPLREL_TILE) {
1652 reason = "have_diplrel_tile";
1653 } else if (preq->source.kind == VUT_DIPLREL_TILE_O) {
1654 reason = "have_diplrel_tile_o";
1655 } else {
1656 fc_assert(preq->source.kind == VUT_DIPLREL);
1657 reason = "have_diplrel";
1658 }
1659
1661 pcity, reason);
1662 }
1663 break;
1666 if (preq->present) {
1667 const char *reason;
1668
1669 notify_player(pplayer, city_tile(pcity),
1671 /* TRANS: '%s' is a wide range of relationships;
1672 * e.g., 'Peace', 'Never met', 'Foreign',
1673 * 'Hosts embassy', 'Provided Casus Belli' */
1674 _("%s can't build %s from the worklist; "
1675 "unit with the relationship '%s' is required."
1676 " Postponing..."),
1677 city_link(pcity),
1678 tgt_name,
1680 preq->source.value.diplrel));
1681
1682 if (preq->source.kind == VUT_DIPLREL_UNITANY) {
1683 reason = "need_diplrel_unitany";
1684 } else {
1685 fc_assert(preq->source.kind == VUT_DIPLREL_UNITANY_O);
1686 reason = "need_diplrel_unitany_o";
1687 }
1688
1690 pcity, reason);
1691 } else {
1692 const char *reason;
1693
1694 notify_player(pplayer, city_tile(pcity),
1696 _("%s can't build %s from the worklist; "
1697 "unit with the relationship '%s' is prohibited."
1698 " Postponing..."),
1699 city_link(pcity),
1700 tgt_name,
1702 preq->source.value.diplrel));
1703
1704 if (preq->source.kind == VUT_DIPLREL_UNITANY) {
1705 reason = "have_diplrel_unitany";
1706 } else {
1707 fc_assert(preq->source.kind == VUT_DIPLREL_UNITANY_O);
1708 reason = "have_diplrel_unitany_o";
1709 }
1710
1712 pcity, reason);
1713 }
1714 break;
1715 case VUT_MINSIZE:
1716 if (preq->present) {
1717 notify_player(pplayer, city_tile(pcity),
1719 _("%s can't build %s from the worklist; "
1720 "city must be of size %d or larger. "
1721 "Postponing..."),
1722 city_link(pcity),
1723 tgt_name,
1724 preq->source.value.minsize);
1726 pcity, "need_minsize");
1727 } else {
1728 notify_player(pplayer, city_tile(pcity),
1730 _("%s can't build %s from the worklist; "
1731 "city must be of size %d or smaller."
1732 "Postponing..."),
1733 city_link(pcity),
1734 tgt_name,
1735 (preq->source.value.minsize - 1));
1737 pcity, "need_minsize");
1738 }
1739 break;
1740 case VUT_MINCULTURE:
1741 if (preq->present) {
1742 notify_player(pplayer, city_tile(pcity),
1744 _("%s can't build %s from the worklist; "
1745 "city must have culture of %d. Postponing..."),
1746 city_link(pcity),
1747 tgt_name,
1748 preq->source.value.minculture);
1750 pcity, "need_minculture");
1751 } else {
1752 /* What has been written may not be unwritten. */
1753 purge = TRUE;
1754 }
1755 break;
1756 case VUT_MINFOREIGNPCT:
1757 if (preq->present) {
1758 notify_player(pplayer, city_tile(pcity),
1760 _("%s can't build %s from the worklist; "
1761 "city must have %d%% foreign population. Postponing..."),
1762 city_link(pcity),
1763 tgt_name,
1764 preq->source.value.minforeignpct);
1766 pcity, "need_minforeignpct");
1767 } else {
1768 notify_player(pplayer, city_tile(pcity),
1770 _("%s can't build %s from the worklist; "
1771 "city must have %d%% native population. Postponing..."),
1772 city_link(pcity),
1773 tgt_name,
1774 100 - preq->source.value.minforeignpct);
1776 pcity, "need_minforeignpct");
1777 }
1778 break;
1779 case VUT_MINTECHS:
1780 if (preq->present) {
1781 notify_player(pplayer, city_tile(pcity),
1783 _("%s can't build %s from the worklist; "
1784 "%d techs must be known. Postponing..."),
1785 city_link(pcity),
1786 tgt_name,
1787 preq->source.value.min_techs);
1789 pcity, "need_mintechs");
1790 } else {
1791 purge = TRUE;
1792 }
1793 break;
1794 case VUT_MAXTILEUNITS:
1795 if (preq->present) {
1796 notify_player(pplayer, city_tile(pcity),
1798 PL_("%s can't build %s from the worklist; "
1799 "more than %d unit on tile."
1800 " Postponing...",
1801 "%s can't build %s from the worklist; "
1802 "more than %d units on tile."
1803 " Postponing...",
1804 preq->source.value.max_tile_units),
1805 city_link(pcity),
1806 tgt_name,
1807 preq->source.value.max_tile_units);
1809 pcity, "need_tileunits");
1810 } else {
1811 notify_player(pplayer, city_tile(pcity),
1813 PL_("%s can't build %s from the worklist; "
1814 "fewer than %d unit on tile."
1815 " Postponing...",
1816 "%s can't build %s from the worklist; "
1817 "fewer than %d units on tile."
1818 " Postponing...",
1819 preq->source.value.max_tile_units + 1),
1820 city_link(pcity),
1821 tgt_name,
1822 preq->source.value.max_tile_units + 1);
1824 pcity, "need_tileunits");
1825 }
1826 break;
1827 case VUT_AI_LEVEL:
1828 /* Can't change AI level. */
1829 purge = TRUE;
1830 break;
1831 case VUT_TERRAINCLASS:
1832 /* Change of terrain class is expected to be very unlikely. Purge!
1833 * TODO: Analyze ruleset to see how unlikely terrain class change actually is. */
1834 purge = TRUE;
1835 break;
1836 case VUT_TERRFLAG:
1837 if (preq->present) {
1838 notify_player(pplayer, city_tile(pcity),
1840 _("%s can't build %s from the worklist; "
1841 "terrain with \"%s\" flag is required. "
1842 "Postponing..."),
1843 city_link(pcity),
1844 tgt_name,
1845 terrain_flag_id_name(preq->source.value.terrainflag));
1847 pcity, "need_terrainflag");
1848 } else {
1849 notify_player(pplayer, city_tile(pcity),
1851 _("%s can't build %s from the worklist; "
1852 "terrain with \"%s\" flag is prohibited. "
1853 "Postponing..."),
1854 city_link(pcity),
1855 tgt_name,
1856 terrain_flag_id_name(preq->source.value.terrainflag));
1858 pcity, "have_terrainflag");
1859 }
1860 break;
1861 case VUT_ROADFLAG:
1862 if (preq->present) {
1863 notify_player(pplayer, city_tile(pcity),
1865 _("%s can't build %s from the worklist; "
1866 "road with \"%s\" flag is required. "
1867 "Postponing..."),
1868 city_link(pcity),
1869 tgt_name,
1870 road_flag_id_name(preq->source.value.roadflag));
1872 pcity, "need_roadflag");
1873 } else {
1874 notify_player(pplayer, city_tile(pcity),
1876 _("%s can't build %s from the worklist; "
1877 "road with \"%s\" flag is prohibited. "
1878 "Postponing..."),
1879 city_link(pcity),
1880 tgt_name,
1881 road_flag_id_name(preq->source.value.roadflag));
1883 pcity, "have_roadflag");
1884 }
1885 break;
1886 case VUT_EXTRAFLAG:
1887 if (preq->present) {
1888 notify_player(pplayer, city_tile(pcity),
1890 _("%s can't build %s from the worklist; "
1891 "extra with \"%s\" flag is required. "
1892 "Postponing..."),
1893 city_link(pcity),
1894 tgt_name,
1895 extra_flag_id_translated_name(preq->source.value.extraflag));
1897 pcity, "need_extraflag");
1898 } else {
1899 notify_player(pplayer, city_tile(pcity),
1901 _("%s can't build %s from the worklist; "
1902 "extra with \"%s\" flag is prohibited. "
1903 "Postponing..."),
1904 city_link(pcity),
1905 tgt_name,
1906 extra_flag_id_translated_name(preq->source.value.extraflag));
1908 pcity, "have_extraflag");
1909 }
1910 break;
1911 case VUT_MINLATITUDE:
1912 case VUT_MAXLATITUDE:
1913 /* Can't change where the city is located. */
1914 purge = TRUE;
1915 break;
1916 case VUT_CITYTILE:
1917 if (CITYT_BORDERING_TCLASS_REGION == preq->source.value.citytile
1918 && (preq->range == REQ_RANGE_CADJACENT
1919 || preq->range == REQ_RANGE_ADJACENT)) {
1920 if (preq->present) {
1921 notify_player(pplayer, city_tile(pcity),
1923 _("%s can't build %s from the worklist; "
1924 "different terrain class nearby is required. "
1925 "Postponing..."),
1926 city_link(pcity),
1927 tgt_name);
1929 pcity, "need_different_terrainclass");
1930 } else {
1931 notify_player(pplayer, city_tile(pcity),
1933 _("%s can't build %s from the worklist; "
1934 "different terrain class nearby is prohibited. "
1935 "Postponing..."),
1936 city_link(pcity),
1937 tgt_name);
1939 pcity, "have_different_terrainclass");
1940 }
1941 break;
1942 }
1943 /* Other values should not present in build reqs */
1945
1946 case VUT_UTYPE:
1947 case VUT_UTFLAG:
1948 case VUT_UCLASS:
1949 case VUT_UCFLAG:
1950 case VUT_MINVETERAN:
1951 case VUT_UNITSTATE:
1952 case VUT_ACTIVITY:
1953 case VUT_MINMOVES:
1954 case VUT_MINHP:
1955 case VUT_ACTION:
1956 case VUT_OTYPE:
1957 case VUT_SPECIALIST:
1958 case VUT_TERRAINALTER: /* XXX could do this in principle */
1959 /* Will only happen with a bogus ruleset. */
1960 log_error("worklist_change_build_target() has bogus preq");
1961 break;
1962 case VUT_CITYSTATUS:
1963 if (preq->source.value.citystatus == CITYS_TRANSFERRED) {
1964 /* If there's a change, it will invalidate worklist anyway. */
1965 purge = TRUE;
1966 } else if (preq->source.value.citystatus == CITYS_OWNED_BY_ORIGINAL) {
1967 if (preq->range == REQ_RANGE_CITY || preq->range == REQ_RANGE_TILE) {
1968 /* Can't change at these ranges */
1969 purge = TRUE;
1970 } else {
1971 if (preq->present) {
1972 notify_player(pplayer, city_tile(pcity),
1974 /* TRANS: last %s is a CityStatus ("OwnedByOriginal") */
1975 _("%s can't build %s from the worklist; "
1976 "only available when city in range %s \"%s\". "
1977 "Postponing..."),
1978 city_link(pcity),
1979 tgt_name, req_range_name(preq->range),
1980 citystatus_type_name(preq->source.value.citystatus));
1982 pcity, "need_citystatus");
1983 } else {
1984 notify_player(pplayer, city_tile(pcity),
1986 /* TRANS: last %s is a CityStatus ("OwnedByOriginal") */
1987 _("%s can't build %s from the worklist; "
1988 "not available when city in range %s is \"%s\". "
1989 "Postponing..."),
1990 city_link(pcity),
1991 tgt_name, req_range_name(preq->range),
1992 citystatus_type_name(preq->source.value.citystatus));
1994 pcity, "have_citystatus");
1995 }
1996 }
1997 } else {
1998 /* Other status types will only happen with a bogus ruleset. */
1999 log_error("worklist_change_build_target() has bogus citystatus preq");
2000 }
2001 break;
2002 case VUT_MINYEAR:
2003 if (preq->present) {
2004 notify_player(pplayer, city_tile(pcity),
2006 /* TRANS: last %s is a date */
2007 _("%s can't build %s from the worklist; "
2008 "only available from %s. Postponing..."),
2009 city_link(pcity),
2010 tgt_name,
2011 textyear(preq->source.value.minyear));
2013 pcity, "need_minyear");
2014 } else {
2015 /* Can't go back in time. */
2016 purge = TRUE;
2017 }
2018 break;
2019 case VUT_MINCALFRAG:
2020 /* Unlike VUT_MINYEAR, a requirement in either direction is
2021 * likely to be fulfilled sooner or later. */
2022 if (preq->present) {
2023 notify_player(pplayer, city_tile(pcity),
2025 /* TRANS: last %s is a calendar fragment from
2026 * the ruleset; may be a bare number */
2027 _("%s can't build %s from the worklist; "
2028 "only available from %s. Postponing..."),
2029 city_link(pcity),
2030 tgt_name,
2031 textcalfrag(preq->source.value.mincalfrag));
2033 pcity, "need_mincalfrag");
2034 } else {
2035 fc_assert_action(preq->source.value.mincalfrag > 0, break);
2036 notify_player(pplayer, city_tile(pcity),
2038 /* TRANS: last %s is a calendar fragment from
2039 * the ruleset; may be a bare number */
2040 _("%s can't build %s from the worklist; "
2041 "not available after %s. Postponing..."),
2042 city_link(pcity),
2043 tgt_name,
2044 textcalfrag(preq->source.value.mincalfrag - 1));
2046 pcity, "have_mincalfrag");
2047 }
2048 break;
2049 case VUT_TOPO:
2050 if (preq->present) {
2051 notify_player(pplayer, city_tile(pcity),
2053 /* TRANS: third %s is topology flag name
2054 * ("Hex", "ISO" */
2055 _("%s can't build %s from the worklist; "
2056 "only available in worlds with %s map."),
2057 city_link(pcity),
2058 tgt_name,
2059 _(topo_flag_name(preq->source.value.topo_property)));
2061 pcity, "need_topo");
2062 }
2063 purge = TRUE;
2064 break;
2065 case VUT_WRAP:
2066 if (preq->present) {
2067 notify_player(pplayer, city_tile(pcity),
2069 /* TRANS: third %s is wrap flag name
2070 * ("WrapX", "Wrapy") */
2071 _("%s can't build %s from the worklist; "
2072 "only available in worlds with %s map."),
2073 city_link(pcity),
2074 tgt_name,
2075 _(wrap_flag_name(preq->source.value.wrap_property)));
2077 pcity, "need_wrap");
2078 }
2079 purge = TRUE;
2080 break;
2081 case VUT_SERVERSETTING:
2082 notify_player(pplayer, city_tile(pcity),
2084 /* TRANS: %s is a server setting, its value and
2085 * if it is required to be present or absent.
2086 * The string's format is specified in
2087 * ssetv_human_readable().
2088 * Example: "killstack is enabled". */
2089 _("%s can't build %s from the worklist; "
2090 "only available when the server setting "
2091 "%s."),
2092 city_link(pcity),
2093 tgt_name,
2094 ssetv_human_readable(preq->source.value.ssetval,
2095 preq->present));
2097 pcity, "need_setting");
2098 /* Don't assume that the server setting will be changed. */
2099 purge = TRUE;
2100 break;
2101 case VUT_AGE:
2102 if (preq->present) {
2103 notify_player(pplayer, city_tile(pcity),
2105 _("%s can't build %s from the worklist; "
2106 "only available once %d turns old. Postponing..."),
2107 city_link(pcity),
2108 tgt_name,
2109 preq->source.value.age);
2111 pcity, "need_age");
2112 } else {
2113 /* Can't go back in time. */
2114 purge = TRUE;
2115 }
2116 break;
2117 case VUT_FORM_AGE:
2118 if (preq->present) {
2119 notify_player(pplayer, city_tile(pcity),
2121 _("%s can't build %s from the worklist; "
2122 "only available once %d turns old form. Postponing..."),
2123 city_link(pcity),
2124 tgt_name,
2125 preq->source.value.age);
2127 pcity, "need_form_age");
2128 } else {
2129 /* Can't go back in time. */
2130 purge = TRUE;
2131 }
2132 break;
2133 case VUT_NONE:
2134 case VUT_COUNT:
2136 "worklist_change_build_target() "
2137 "called with invalid preq");
2138 break;
2139 /* No default handling here, as we want compiler warning
2140 * if new requirement type is added to enum and it's not handled
2141 * here. */
2142 };
2143 break;
2144 }
2145
2146 /* Almost all cases emit signal in the end, so city check needed. */
2147 if (!city_exist(saved_id)) {
2148 /* Some script has removed city */
2149 return TRUE;
2150 }
2151
2153
2154 if (!known) {
2155 /* FIXME: make can_city_build_improvement_now() return a reason enum,
2156 * so we can notify user with it.
2157 * Likely the building already exist. */
2158 purge = TRUE;
2159 }
2160
2161 return purge;
2162}
2163
2164/**********************************************************************/
2170static bool worklist_change_build_target(struct player *pplayer,
2171 struct city *pcity)
2172{
2173 struct universal target;
2174 bool success = FALSE;
2175 int i;
2176 int saved_id = pcity->id;
2177 bool city_checked = TRUE; /* This is used to avoid spurious city_exist() calls */
2178 struct worklist *pwl = &pcity->worklist;
2179
2180 if (worklist_is_empty(pwl)) {
2181 /* Nothing in the worklist; bail now. */
2182 return FALSE;
2183 }
2184
2185 i = 0;
2186 while (!success && i < worklist_length(pwl)) {
2187
2188 if (!city_checked) {
2189 if (!city_exist(saved_id)) {
2190 /* Some script has removed useless city that cannot build
2191 * what it is told to! */
2192 return FALSE;
2193 }
2195 }
2196
2197 if (worklist_peek_ith(pwl, &target, i)) {
2198 success = can_city_build_now(pcity, &target);
2199 } else {
2200 success = FALSE;
2201 }
2202 i++;
2203
2204 if (success) {
2205 break; /* while */
2206 }
2207
2208 switch (target.kind) {
2209 case VUT_UTYPE:
2210 {
2211 const struct unit_type *ptarget = target.value.utype;
2212 const struct unit_type *pupdate = unit_upgrades_to(pcity, ptarget);
2213 bool purge;
2214
2215 /* Maybe we can just upgrade the target to what the city /can/ build. */
2216 if (U_NOT_OBSOLETED == pupdate) {
2217 /* Nope, we're stuck. Skip this item from the worklist. */
2218 struct research *presearch = research_get(pplayer);
2219 struct advance *missing = NULL;
2220 bool multiple = FALSE;
2221
2224 if (missing != NULL) {
2225 multiple = TRUE;
2226 } else {
2227 missing = padv;
2228 }
2229 }
2231
2232
2233 if (missing != NULL) {
2234 if (!multiple) {
2235 notify_player(pplayer, city_tile(pcity),
2237 _("%s can't build %s from the worklist; "
2238 "tech %s not yet available. Postponing..."),
2241 } else {
2242 notify_player(pplayer, city_tile(pcity),
2244 _("%s can't build %s from the worklist; "
2245 "multiple techs still needed. Postponing..."),
2247 }
2248
2249 script_server_signal_emit("unit_cant_be_built", ptarget, pcity,
2250 "need_tech");
2251 } else {
2252 /* Unknown or requirement from vector. */
2253 purge = worklist_item_postpone_req_vec(&target, pcity, pplayer,
2254 saved_id);
2255 }
2257 break;
2258 } else {
2260 }
2261 if (purge) {
2262 /* If the city can never build this unit or its descendants,
2263 * drop it. */
2264 notify_player(pplayer, city_tile(pcity),
2266 _("%s can't build %s from the worklist. Purging..."),
2267 city_link(pcity),
2268 /* Yes, warn about the targets that's actually
2269 in the worklist, not its obsolete-closure
2270 pupdate. */
2272 script_server_signal_emit("unit_cant_be_built", ptarget, pcity,
2273 "never");
2274 if (city_exist(saved_id)) {
2276 /* Purge this worklist item. */
2277 i--;
2279 } else {
2281 }
2282 } else {
2283 /* Yep, we can go after pupdate instead. Joy! */
2284 notify_player(pplayer, city_tile(pcity), E_WORKLIST, ftc_server,
2285 _("Production of %s is upgraded to %s in %s."),
2288 city_link(pcity));
2289 target.value.utype = pupdate;
2290 }
2291 break;
2292 }
2293 case VUT_IMPROVEMENT:
2294 {
2295 const struct impr_type *ptarget = target.value.building;
2296 const struct impr_type *pupdate = building_upgrades_to(pcity, ptarget);
2297 bool purge;
2298
2299 /* If the city can never build this improvement, drop it. */
2301 purge = !success;
2302
2303 /* Maybe this improvement has been obsoleted by something that
2304 we can build. */
2305 if (purge) {
2306 /* Nope, no use. *sigh* */
2307
2308 /* Can it be postponed? */
2310 purge = worklist_item_postpone_req_vec(&target, pcity, pplayer,
2311 saved_id);
2312
2313 /* Almost all cases emit signal in the end, so city check needed. */
2314 if (!city_exist(saved_id)) {
2315 /* Some script has removed city */
2316 return FALSE;
2317 }
2319 }
2320 } else if (success) {
2321 /* Hey, we can upgrade the improvement! */
2322 notify_player(pplayer, city_tile(pcity), E_WORKLIST, ftc_server,
2323 _("Production of %s is upgraded to %s in %s."),
2326 city_link(pcity));
2327 target.value.building = pupdate;
2328 }
2329
2330 if (purge) {
2331 /* Never in a million years. */
2332 notify_player(pplayer, city_tile(pcity),
2334 _("%s can't build %s from the worklist. Purging..."),
2335 city_link(pcity),
2337 script_server_signal_emit("building_cant_be_built", ptarget, pcity,
2338 "never");
2339 if (city_exist(saved_id)) {
2341 /* Purge this worklist item. */
2342 i--;
2344 } else {
2346 }
2347 }
2348 break;
2349 }
2350 default:
2351 /* skip useless target */
2352 log_error("worklist_change_build_target() has unrecognized "
2353 "target kind (%d)", target.kind);
2354 break;
2355 };
2356 } /* while */
2357
2358 if (success) {
2359 /* All okay. Switch targets. */
2360 change_build_target(pplayer, pcity, &target, E_WORKLIST);
2361
2362 /* i is the index immediately _after_ the item we're changing to.
2363 Remove the (i-1)th item from the worklist. */
2364 worklist_remove(pwl, i - 1);
2365 }
2366
2367 if (worklist_is_empty(pwl)) {
2368 /* There *was* something in the worklist, but it's empty now. Bug the
2369 player about it. */
2370 notify_player(pplayer, city_tile(pcity), E_WORKLIST, ftc_server,
2371 /* TRANS: The <city> worklist .... */
2372 _("The %s worklist is now empty."),
2373 city_link(pcity));
2374 }
2375
2376 return success;
2377}
2378
2379/**********************************************************************/
2384void choose_build_target(struct player *pplayer, struct city *pcity)
2385{
2386 /* Pick the next thing off the worklist. */
2387 if (worklist_change_build_target(pplayer, pcity)) {
2388 return;
2389 }
2390
2391 /* Try building the same thing again. Repeat building doesn't require a
2392 * call to change_build_target, so just return. */
2393 switch (pcity->production.kind) {
2394 case VUT_UTYPE:
2395 /* We can build a unit again unless it's unique or we have lost the tech. */
2397 && can_city_build_unit_now(pcity, pcity->production.value.utype)) {
2398 log_base(LOG_BUILD_TARGET, "%s repeats building %s", city_name_get(pcity),
2400 return;
2401 }
2402 break;
2403 case VUT_IMPROVEMENT:
2405 /* We can build space and coinage again, and possibly others. */
2406 log_base(LOG_BUILD_TARGET, "%s repeats building %s", city_name_get(pcity),
2408 return;
2409 }
2410 break;
2411 default:
2412 /* fallthru */
2413 break;
2414 };
2415
2416 /* Find *something* to do! */
2417 log_debug("Trying advisor_choose_build.");
2418 advisor_choose_build(pplayer, pcity);
2419 log_debug("Advisor_choose_build didn't kill us.");
2420}
2421
2422/**********************************************************************/
2427static const struct impr_type *building_upgrades_to(struct city *pcity,
2428 const struct impr_type *pimprove)
2429{
2430 const struct impr_type *check = pimprove;
2431 const struct impr_type *best_upgrade = NULL;
2432
2434 return NULL;
2435 }
2439 }
2440 }
2441
2442 return best_upgrade;
2443}
2444
2445/**********************************************************************/
2448static void upgrade_building_prod(struct city *pcity)
2449{
2450 const struct impr_type *producing = pcity->production.value.building;
2451 const struct impr_type *upgrading = building_upgrades_to(pcity, producing);
2452
2454 notify_player(city_owner(pcity), city_tile(pcity),
2456 _("Production of %s is upgraded to %s in %s."),
2459 city_link(pcity));
2462 }
2463}
2464
2465/**********************************************************************/
2473static const struct unit_type *unit_upgrades_to(struct city *pcity,
2474 const struct unit_type *punittype)
2475{
2476 const struct unit_type *check = punittype;
2477 const struct unit_type *best_upgrade = U_NOT_OBSOLETED;
2478
2480 return U_NOT_OBSOLETED;
2481 }
2482 while ((check = check->obsoleted_by) != U_NOT_OBSOLETED) {
2483 if (can_city_build_unit_direct(pcity, check)) {
2485 }
2486 }
2487
2488 return best_upgrade;
2489}
2490
2491/**********************************************************************/
2494static void upgrade_unit_prod(struct city *pcity)
2495{
2496 const struct unit_type *producing = pcity->production.value.utype;
2497 const struct unit_type *upgrading = unit_upgrades_to(pcity, producing);
2498
2500 notify_player(city_owner(pcity), city_tile(pcity),
2502 _("Production of %s is upgraded to %s in %s."),
2505 city_link(pcity));
2507 }
2508}
2509
2510/**********************************************************************/
2517static bool city_distribute_surplus_shields(struct player *pplayer,
2518 struct city *pcity)
2519{
2520 int size_reduction = 0;
2521 struct unit *sacrifizer;
2522
2523 if (pcity->surplus[O_SHIELD] < 0) {
2525 if (utype_upkeep_cost(unit_type_get(punit), pplayer, O_SHIELD) > 0
2526 && pcity->surplus[O_SHIELD] < 0) {
2527 const char *punit_link = unit_link(punit);
2528
2529 /* TODO: Should the unit try to help cities on adjacent tiles? That
2530 * would be a rules change. (This action is performed by the game
2531 * it self) */
2534 notify_player(pplayer, city_tile(pcity),
2536 _("%s can't upkeep %s, unit disbanded."),
2537 city_link(pcity), punit_link);
2538 }
2539
2540 /* pcity->surplus[O_SHIELD] is automatically updated. */
2541 }
2543 }
2544
2545 if (pcity->surplus[O_SHIELD] < 0) {
2546 /* Special case: MissingXProtected. This nasty unit won't go so easily.
2547 * It'd rather make the citizens pay in blood for their failure to upkeep
2548 * it! If we make it here all normal units are already disbanded, so only
2549 * undisbandable ones remain. */
2552
2553 if (upkeep > 0 && pcity->surplus[O_SHIELD] < 0) {
2554
2556 sacrifizer = punit;
2557
2558 /* No upkeep for the unit this turn. */
2559 pcity->surplus[O_SHIELD] += upkeep;
2560 }
2562 }
2563
2564 /* Now we confirm changes made last turn. */
2565 pcity->shield_stock += pcity->surplus[O_SHIELD];
2566 pcity->before_change_shields = pcity->shield_stock;
2568
2569 /* Previous turn values stored, and they are consistent with
2570 * other previous turn data.
2571 * Now reduce city size, likely messing all the values. */
2572 if (size_reduction > 0) {
2573 if (size_reduction == 1) {
2574 notify_player(pplayer, city_tile(pcity),
2576 _("Citizens in %s perish for their failure to "
2577 "upkeep %s!"),
2578 city_link(pcity), unit_link(sacrifizer));
2579 } else {
2580 notify_player(pplayer, city_tile(pcity),
2582 _("Citizens in %s perish for their failure to "
2583 "upkeep units!"),
2584 city_link(pcity));
2585 }
2586
2587 if (!city_reduce_size(pcity, size_reduction, NULL, "upkeep_failure")) {
2588 return FALSE;
2589 }
2590 }
2591
2592 return TRUE;
2593}
2594
2595/**********************************************************************/
2598static bool city_build_building(struct player *pplayer, struct city *pcity)
2599{
2600 bool space_part;
2601 int mod;
2602 const struct impr_type *pimprove;
2603 int saved_id = pcity->id;
2604
2606 /* Coinage-like improvements that convert production */
2607 fc_assert(pcity->before_change_shields >= 0);
2608
2609 /* pcity->before_change_shields already contains the surplus from
2610 * this turn. */
2611 if (city_production_has_flag(pcity, IF_GOLD)) {
2612 pplayer->economic.gold += pcity->before_change_shields;
2613 }
2614
2615 pcity->before_change_shields = 0;
2616 pcity->shield_stock = 0;
2617 choose_build_target(pplayer, pcity);
2618
2619 return TRUE;
2620 }
2621
2622 upgrade_building_prod(pcity);
2623
2624 /* The final (after upgrade) build target */
2625 pimprove = pcity->production.value.building;
2626
2627 if (!can_city_build_improvement_now(pcity, pimprove)) {
2629 _("%s is building %s, which is no longer available."),
2630 city_link(pcity),
2631 city_improvement_name_translation(pcity, pimprove));
2632 script_server_signal_emit("building_cant_be_built", pimprove, pcity,
2633 "unavailable");
2634 return TRUE;
2635 }
2636 if (pcity->shield_stock >= impr_build_shield_cost(pcity, pimprove)) {
2637 int cost;
2638
2639 if (is_small_wonder(pimprove)) {
2640 city_list_iterate(pplayer->cities, wcity) {
2641 if (city_has_building(wcity, pimprove)) {
2642 city_remove_improvement(wcity, pimprove);
2643 break;
2644 }
2646 }
2647
2648 space_part = TRUE;
2650 RPT_CERTAIN) > 0) {
2651 pplayer->spaceship.structurals++;
2653 RPT_CERTAIN) > 0) {
2654 pplayer->spaceship.components++;
2656 RPT_CERTAIN) > 0) {
2657 pplayer->spaceship.modules++;
2658 } else {
2659 space_part = FALSE;
2661 _("Completion of %s"));
2662 }
2663 cost = impr_build_shield_cost(pcity, pimprove);
2664 pcity->before_change_shields -= cost;
2665 pcity->shield_stock -= cost;
2666 pcity->turn_last_built = game.info.turn;
2667 /* to eliminate micromanagement */
2668 if (is_great_wonder(pimprove)) {
2670 _("The %s have finished building %s in %s."),
2671 nation_plural_for_player(pplayer),
2672 city_improvement_name_translation(pcity, pimprove),
2673 city_link(pcity));
2674 }
2675
2677 _("%s has finished building %s."),
2678 city_link(pcity), improvement_name_translation(pimprove));
2679 script_server_signal_emit("building_built", pimprove, pcity);
2680
2681 if (!city_exist(saved_id)) {
2682 /* Script removed city */
2683 return FALSE;
2684 }
2685
2686 /* Call this function since some buildings may change the
2687 * the vision range of a city */
2688 city_refresh_vision(pcity);
2689
2691 RPT_CERTAIN))) {
2692 struct research *presearch = research_get(pplayer);
2693 char research_name[MAX_LEN_NAME * 2];
2694 int i;
2695 const char *provider = improvement_name_translation(pimprove);
2696
2698 PL_("%s boosts research; you gain %d immediate "
2699 "advance.",
2700 "%s boosts research; you gain %d immediate "
2701 "advances.",
2702 mod), provider, mod);
2703
2705 for (i = 0; i < mod; i++) {
2708
2711 /* TRANS: Tech from building (Darwin's Voyage) */
2712 Q_("?frombldg:Acquired %s from %s."), adv_name,
2713 provider);
2714
2716 /* TRANS: Tech from building (Darwin's
2717 * Voyage) */
2718 Q_("?frombldg:The %s have acquired %s "
2719 "from %s."),
2721 }
2722 }
2723 if (space_part && pplayer->spaceship.state == SSHIP_NONE) {
2725 _("The %s have started building a spaceship!"),
2726 nation_plural_for_player(pplayer));
2727 pplayer->spaceship.state = SSHIP_STARTED;
2728 }
2729 if (space_part) {
2730 /* space ship part build */
2731 send_spaceship_info(pplayer, NULL);
2732 } else {
2733 /* Update city data. */
2734 if (city_refresh(pcity)) {
2735 auto_arrange_workers(pcity);
2736 }
2737 }
2738
2739 /* Move to the next thing in the worklist */
2740 choose_build_target(pplayer, pcity);
2741 }
2742
2743 return TRUE;
2744}
2745
2746/**********************************************************************/
2755static struct unit *city_create_unit(struct city *pcity,
2756 const struct unit_type *utype,
2757 struct citizens_reduction *red)
2758{
2759 struct player *pplayer = city_owner(pcity);
2760 struct unit *punit;
2761 int saved_unit_id;
2762 int pop_cost = utype_pop_value(utype, pcity);
2763
2764 punit = unit_virtual_prepare(pplayer, pcity->tile, utype,
2766 pcity->id, -1, -1);
2767 pplayer->score.units_built++;
2768
2769 if (pop_cost > 0 && pcity->nationality != NULL) {
2770 /* We don't reduce city size in-place to keep it correct and
2771 * existing at all while we call the following callback.
2772 * We want citizens_unit_nationality() to adjust 'red' even when
2773 * we are not setting unit nationality based on the return */
2774 struct player *nat = citizens_unit_nationality(pcity, pop_cost, red);
2775
2778 }
2779 } else if (red) {
2780 red->change = 0;
2781 }
2782
2783 (void) place_unit(punit, pplayer, pcity, NULL, FALSE);
2785
2786 /* If city has a rally point set, give the unit a move order. */
2787 if (pcity->rally_point.length) {
2792 * sizeof(struct unit_order));
2794 pcity->rally_point.length * sizeof(struct unit_order));
2795 }
2796
2797 /* This might destroy pcity and/or punit: */
2798 script_server_signal_emit("unit_built", punit, pcity);
2799
2801 return punit;
2802 } else {
2803 return NULL;
2804 }
2805}
2806
2807/**********************************************************************/
2816static bool city_build_unit(struct player *pplayer, struct city *pcity)
2817{
2818 const struct unit_type *utype;
2819 struct worklist *pwl = &pcity->worklist;
2821 int saved_city_id = pcity->id;
2822
2824
2825 /* If the city has already bought a unit which is now obsolete, don't try
2826 * to upgrade the production. The new unit might require more shields, which
2827 * would be bad if it was bought to urgently defend a city. (Equally it
2828 * might be the same cost or cheaper, but tough; you hurried the unit so
2829 * you miss out on technological advances.) */
2830 if (city_can_change_build(pcity)) {
2831 upgrade_unit_prod(pcity);
2832 }
2833
2834 utype = pcity->production.value.utype;
2836
2837 /* We must make a special case for barbarians here, because they are
2838 so dumb. Really. They don't know the prerequisite techs for units
2839 they build!! - Per */
2840 if (!can_city_build_unit_direct(pcity, utype)
2841 && !is_barbarian(pplayer)) {
2843 _("%s is building %s, which is no longer available."),
2844 city_link(pcity), utype_name_translation(utype));
2845
2846 /* Log before signal emitting, so pointers are certainly valid */
2847 log_verbose("%s %s tried to build %s, which is not available.",
2849 city_name_get(pcity), utype_rule_name(utype));
2850 script_server_signal_emit("unit_cant_be_built", utype, pcity,
2851 "unavailable");
2852 return city_exist(saved_city_id);
2853 }
2854
2855 if (pcity->shield_stock >= unit_shield_cost) {
2856 int pop_cost = utype_pop_value(utype, pcity);
2857 struct unit *punit;
2858
2859 /* Should we disband the city? -- Massimo */
2860 if (city_size_get(pcity) == pop_cost
2861 && is_city_option_set(pcity, CITYO_DISBAND)) {
2862 return !disband_city(pcity);
2863 }
2864
2865 if (city_size_get(pcity) <= pop_cost) {
2867 /* TRANS: city ... utype ... size ... pop_cost */
2868 _("%s can't build %s yet. "
2869 "(city size: %d, unit population cost: %d)"),
2871 city_size_get(pcity), pop_cost);
2872 script_server_signal_emit("unit_cant_be_built", utype, pcity,
2873 "pop_cost");
2874 return city_exist(saved_city_id);
2875 }
2876
2877 fc_assert(pop_cost == 0 || city_size_get(pcity) >= pop_cost);
2878
2879 /* don't update turn_last_built if we returned above */
2880 pcity->turn_last_built = game.info.turn;
2881
2882 /* check if we can build more than one unit (effect City_Build_Slots) */
2884
2885 /* We should be able to build at least one (by checks above) */
2886 fc_assert(num_units >= 1);
2887
2888 for (i = 0; i < num_units; i++) {
2890
2891 punit = city_create_unit(pcity, utype, natred);
2892
2893 /* Check if the city still exists (script might have removed it).
2894 * If not, we assume any effects / announcements done below were
2895 * already replaced by the script if necessary. */
2896 if (!city_exist(saved_city_id)) {
2897 break;
2898 }
2899
2900 if (punit) {
2902 /* TRANS: <city> is finished building <unit/building>. */
2903 _("%s is finished building %s."),
2904 city_link(pcity), utype_name_translation(utype));
2905 }
2906
2907 /* After we created the unit remove the citizen. This will also
2908 * rearrange the worker to take into account the extra resources
2909 * (food) needed. */
2910 if (pop_cost > 0) {
2911 /* This won't disband city due to pop_cost, but script might
2912 * still destroy city. */
2914 /* If the city has changed its nationalities during
2915 * "unit_built" signal, we take some other citizens instead */
2916 if (!city_reduce_size(pcity, pop_cost, NULL, "unit_built")) {
2917 break;
2918 }
2919 }
2920
2921 /* to eliminate micromanagement, we only subtract the unit's cost */
2922 /* signals could change the prod stock! */
2923 if ((pcity->before_change_shields -= unit_shield_cost) < 0) {
2924 pcity->before_change_shields = 0;
2925 }
2926 if ((pcity->shield_stock -= unit_shield_cost) < 0) {
2927 log_normal("City %s (%s) has built %s but has no %d shields "
2928 "for it, nullifying shield stock", city_name_get(pcity),
2929 player_name(pplayer), utype_rule_name(utype),
2931 pcity->shield_stock = 0;
2932 }
2933
2934 if (pop_cost > 0) {
2935 /* Additional message if the unit has population cost. */
2937 ftc_server,
2938 /* TRANS: "<unit> cost... <city> shrinks..."
2939 * Plural in "%d population", not "size %d". */
2940 PL_("%s cost %d population. %s shrinks to size %d.",
2941 "%s cost %d population. %s shrinks to size %d.",
2942 pop_cost),
2943 utype_name_translation(utype), pop_cost,
2944 city_link(pcity), city_size_get(pcity));
2945 }
2946
2947 if (i != 0 && worklist_length(pwl) > 0) {
2948 /* remove the build unit from the worklist; it has to be one less
2949 * than units build to preserve the next build target from the
2950 * worklist */
2951 worklist_remove(pwl, 0);
2952 }
2953 } /* for */
2954
2956 if (pcity->rally_point.length && !pcity->rally_point.persistent) {
2958 }
2959
2960 /* Done building this unit; time to move on to the next. */
2961 choose_build_target(pplayer, pcity);
2962 }
2963 } /* if */
2964
2965 return city_exist(saved_city_id);
2966}
2967
2968/**********************************************************************/
2971static bool city_build_stuff(struct player *pplayer, struct city *pcity)
2972{
2973 if (!city_distribute_surplus_shields(pplayer, pcity)) {
2974 return FALSE;
2975 }
2976
2979
2980 switch (pcity->production.kind) {
2981 case VUT_IMPROVEMENT:
2982 return city_build_building(pplayer, pcity);
2983 case VUT_UTYPE:
2984 return city_build_unit(pplayer, pcity);
2985 default:
2986 /* must never happen! */
2988 break;
2989 };
2990 return FALSE;
2991}
2992
2993/**********************************************************************/
3003static bool sell_random_building(struct player *pplayer,
3004 struct cityimpr_list *imprs)
3005{
3006 struct cityimpr *pcityimpr;
3007 int r;
3008
3009 fc_assert_ret_val(pplayer != NULL, FALSE);
3010
3011 if (!imprs || cityimpr_list_size(imprs) == 0) {
3012 return FALSE;
3013 }
3014
3017
3019 ftc_server,
3020 _("Can't afford to maintain %s in %s, building sold!"),
3022 city_link(pcityimpr->pcity));
3023 log_debug("%s: sold building (%s)", player_name(pplayer),
3025
3026 do_sell_building(pplayer, pcityimpr->pcity, pcityimpr->pimprove, "cant_maintain");
3027
3029
3030 /* Get back the gold upkeep that was already paid this turn. */
3032 pcityimpr->pimprove);
3033
3035
3037
3038 return TRUE;
3039}
3040
3041/**********************************************************************/
3049static void uk_rem_gold_callback(struct unit *punit)
3050{
3051 int gold_upkeep;
3052
3053 /* Remove the unit from uk_rem_gold. */
3055
3056 gold_upkeep = punit->server.upkeep_paid[O_GOLD];
3057
3058 /* All units in uk_rem_gold should have gold upkeep! */
3059 fc_assert_ret_msg(gold_upkeep > 0, "%s has %d gold upkeep",
3060 unit_rule_name(punit), gold_upkeep);
3061
3062 /* Get the upkeep gold back. */
3063 unit_owner(punit)->economic.gold += gold_upkeep;
3064}
3065
3066/**********************************************************************/
3070static void uk_rem_gold_append(struct unit *punit)
3071{
3072 /* Make the unit aware that it is on the uk_rem_gold list. */
3074
3075 /* Add the unit to the list. */
3077}
3078
3079/**********************************************************************/
3083static void unit_list_referred_destroy(struct unit_list *punitlist)
3084{
3086 /* Clear the unit's knowledge of the list. */
3089
3090 /* Destroy the list it self. */
3092}
3093
3094/**********************************************************************/
3106static struct unit *sell_random_unit(struct player *pplayer,
3107 struct unit_list *punitlist)
3108{
3109 struct unit *punit;
3110 int r;
3111 struct unit_list *cargo;
3112
3113 fc_assert_ret_val(pplayer != NULL, NULL);
3114
3115 if (!punitlist || unit_list_size(punitlist) == 0) {
3116 return NULL;
3117 }
3118
3121
3122 cargo = unit_list_new();
3123
3124 /* Check if unit is transporting other units from punitlist,
3125 * and sell one of those (recursively) instead.
3126 * Note that in case of recursive transports we have to iterate
3127 * also through those middle transports that themselves are not in
3128 * punitlist. */
3130 /* Optimization, do not iterate over punitlist
3131 * if we are sure that pcargo is not in it. */
3132 if (pcargo->server.upkeep_paid[O_GOLD] > 0) {
3134 if (pcargo == p2) {
3135 unit_list_append(cargo, pcargo);
3136 }
3138 }
3140
3141 if (unit_list_size(cargo) > 0) {
3142 /* Recursively sell. Note that cargo list has both
3143 * leaf units and middle transports in case of
3144 * recursive transports. */
3145 struct unit *ret = sell_random_unit(pplayer, cargo);
3146
3147 unit_list_destroy(cargo);
3148
3150
3151 return ret;
3152 }
3153
3154 unit_list_destroy(cargo);
3155
3156 {
3157 const char *punit_link = unit_tile_link(punit);
3158#ifdef FREECIV_DEBUG
3159 const char *punit_logname = unit_rule_name(punit);
3160#endif /* FREECIV_DEBUG */
3161 struct tile *utile = unit_tile(punit);
3162
3166
3167 /* The gold was paid back when the unit removal made
3168 * uk_rem_gold_callback() run as the unit's removal call back. */
3169
3170 notify_player(pplayer, utile, E_UNIT_LOST_MISC, ftc_server,
3171 _("Not enough gold. %s disbanded."),
3172 punit_link);
3173 log_debug("%s: unit sold (%s)", player_name(pplayer),
3175 } else {
3176 /* Not able to get rid of punit */
3177 return NULL;
3178 }
3179 }
3180
3182
3183 return punit;
3184}
3185
3186/**********************************************************************/
3190{
3191 struct cityimpr_list *pimprlist;
3192 bool sell_unit = TRUE;
3193
3194 if (!pplayer) {
3195 return FALSE;
3196 }
3197
3200
3201 city_list_iterate(pplayer->cities, pcity) {
3202 city_built_iterate(pcity, pimprove) {
3203 if (can_city_sell_building(pcity, pimprove)) {
3204 struct cityimpr *ci = fc_malloc(sizeof(*ci));
3205
3206 ci->pcity = pcity;
3207 ci->pimprove = pimprove;
3209 }
3212
3213 unit_list_iterate(pplayer->units, punit) {
3214 if (punit->server.upkeep_paid[O_GOLD] > 0) {
3216 }
3218
3219 while (pplayer->economic.gold < 0
3221 || unit_list_size(uk_rem_gold) > 0)) {
3223 || unit_list_size(uk_rem_gold) == 0) {
3225 } else {
3226 sell_random_unit(pplayer, uk_rem_gold);
3227 }
3229 }
3230
3231 /* Free remaining entries from list */
3233 FC_FREE(pimpr);
3235
3236 if (pplayer->economic.gold < 0) {
3237 /* If we get here it means the player has
3238 * negative gold. This should never happen. */
3239 fc_assert_msg(FALSE, "Player %s (nb %d) cannot have negative gold!",
3240 player_name(pplayer), player_number(pplayer));
3241 }
3242
3245
3246 return pplayer->economic.gold >= 0;
3247}
3248
3249/**********************************************************************/
3253{
3254 if (!pplayer) {
3255 return FALSE;
3256 }
3257
3259
3260 unit_list_iterate(pplayer->units, punit) {
3261 if (punit->server.upkeep_paid[O_GOLD] > 0) {
3263 }
3265
3266 while (pplayer->economic.gold < 0
3267 && sell_random_unit(pplayer, uk_rem_gold)) {
3268 /* All done in sell_random_unit() */
3269 }
3270
3271 if (pplayer->economic.gold < 0) {
3272 /* If we get here it means the player has
3273 * negative gold. This should never happen. */
3274 fc_assert_msg(FALSE, "Player %s (nb %d) cannot have negative gold!",
3275 player_name(pplayer), player_number(pplayer));
3276 }
3277
3279
3280 return pplayer->economic.gold >= 0;
3281}
3282
3283/**********************************************************************/
3287{
3288 struct player *pplayer;
3289 struct cityimpr_list *pimprlist;
3290
3291 if (!pcity) {
3292 return TRUE;
3293 }
3294
3295 pplayer = city_owner(pcity);
3297
3298 /* Create a vector of all buildings that can be sold. */
3299 city_built_iterate(pcity, pimprove) {
3300 if (can_city_sell_building(pcity, pimprove)) {
3301 struct cityimpr *ci = fc_malloc(sizeof(*ci));
3302
3303 ci->pcity = pcity;
3304 ci->pimprove = pimprove;
3306 }
3308
3309 /* Try to sell some buildings. */
3310 while (pplayer->economic.gold < 0
3311 && sell_random_building(pplayer, pimprlist)) {
3312 /* all done in sell_random_building */
3313 }
3314
3315 /* Free remaining entries from list */
3317 FC_FREE(pimpr);
3319
3321
3322 return pplayer->economic.gold >= 0;
3323}
3324
3325/**********************************************************************/
3334{
3335 struct player *pplayer;
3336
3337 if (!pcity) {
3338 return TRUE;
3339 }
3340
3341 pplayer = city_owner(pcity);
3343
3344 /* Create a vector of all supported units with gold upkeep. */
3346 if (punit->server.upkeep_paid[O_GOLD] > 0) {
3348 }
3350
3351 /* Still not enough gold, so try "selling" some units. */
3352 while (pplayer->economic.gold < 0
3353 && sell_random_unit(pplayer, uk_rem_gold)) {
3354 /* All done in sell_random_unit() */
3355 }
3356
3357 /* If we get here the player has negative gold, but hopefully
3358 * another city will be able to pay the deficit, so continue. */
3359
3361
3362 return pplayer->economic.gold >= 0;
3363}
3364
3365/**********************************************************************/
3368static bool place_pollution(struct city *pcity, enum extra_cause cause)
3369{
3370 struct tile *ptile;
3371 struct tile *pcenter = city_tile(pcity);
3372 int city_radius_sq = city_map_radius_sq_get(pcity);
3373 int k = 100;
3374 const struct civ_map *nmap = &(wld.map);
3375
3376 while (k > 0) {
3377 /* Place pollution on a random city tile */
3378 int cx, cy;
3379 int tile_id = fc_rand(city_map_tiles(city_radius_sq));
3380 struct extra_type *pextra;
3381
3382 city_tile_index_to_xy(&cx, &cy, tile_id, city_radius_sq);
3383
3384 /* Check for a real map position */
3385 if (!(ptile = city_map_to_tile(nmap, pcenter, city_radius_sq, cx, cy))) {
3386 continue;
3387 }
3388
3389 pextra = rand_extra_for_tile(ptile, cause, FALSE);
3390
3391 if (pextra != NULL && !tile_has_extra(ptile, pextra)) {
3392 tile_add_extra(ptile, pextra);
3393 update_tile_knowledge(ptile);
3394
3395 return TRUE;
3396 }
3397 k--;
3398 }
3399 log_debug("pollution not placed: city: %s", city_name_get(pcity));
3400
3401 return FALSE;
3402}
3403
3404/**********************************************************************/
3407static void check_pollution(struct city *pcity)
3408{
3409 if (fc_rand(100) < pcity->pollution) {
3410 if (place_pollution(pcity, EC_POLLUTION)) {
3412 _("Pollution near %s."), city_link(pcity));
3413 }
3414 }
3415}
3416
3417/**********************************************************************/
3424int city_incite_cost(struct player *pplayer, struct city *pcity)
3425{
3426 int dist, size;
3427 double cost; /* Intermediate values can get very large */
3428
3429 /* Gold factor */
3430 cost = city_owner(pcity)->economic.gold + game.server.base_incite_cost;
3431
3432 unit_list_iterate(pcity->tile->units, punit) {
3436
3437 /* Buildings */
3438 city_built_iterate(pcity, pimprove) {
3439 cost += impr_build_shield_cost(pcity, pimprove)
3442
3443 /* Stability bonuses */
3444 if (!city_unhappy(pcity)) {
3445 cost *= 2;
3446 }
3447 if (city_celebrating(pcity)) {
3448 cost *= 2;
3449 }
3450
3451 /* Buy back is cheap, conquered cities are also cheap */
3453 if (city_owner(pcity) != pcity->original) {
3454 if (pplayer == pcity->original) {
3455 cost /= 2; /* Buy back: 50% price reduction */
3456 } else {
3457 cost = cost * 2 / 3; /* Buy conquered: 33% price reduction */
3458 }
3459 }
3460 }
3461
3462 /* Distance from capital */
3463 /* Max penalty. Applied if there is no capital, or it's even further away. */
3464 dist = 32;
3465 city_list_iterate(city_owner(pcity)->cities, capital) {
3466 if (is_capital(capital)) {
3467 int tmp = map_distance(capital->tile, pcity->tile);
3468
3469 if (tmp < dist) {
3470 dist = tmp;
3471 }
3472 }
3474
3475 size = MAX(1, city_size_get(pcity)
3478 - pcity->feel[CITIZEN_ANGRY][FEELING_FINAL] * 3);
3479 cost *= size;
3481 cost = cost / (dist + 3);
3482
3484 int cost_per_citizen = cost / pcity->size;
3485 int natives = citizens_nation_get(pcity, city_owner(pcity)->slot);
3486 int tgt_cit = citizens_nation_get(pcity, pplayer->slot);
3487 int third_party = pcity->size - natives - tgt_cit;
3488
3489 cost = cost_per_citizen * (natives + 0.7 * third_party + 0.5 * tgt_cit);
3490 }
3491
3492 cost += (cost * get_city_bonus(pcity, EFT_INCITE_COST_PCT)) / 100;
3493 cost /= 100;
3494
3497 } else {
3498 return cost;
3499 }
3500}
3501
3502/**********************************************************************/
3505static void define_orig_production_values(struct city *pcity)
3506{
3507 /* Remember what this city is building last turn, so that on the next turn
3508 * the player can switch production to something else and then change it
3509 * back without penalty. This has to be updated _before_ production for
3510 * this turn is calculated, so that the penalty will apply if the player
3511 * changes production away from what has just been completed. This makes
3512 * sense if you consider what this value means: all the shields in the
3513 * city have been dedicated toward the project that was chosen last turn,
3514 * so the player shouldn't be penalized if the governor has to pick
3515 * something different. See city_change_production_penalty(). */
3516 pcity->changed_from = pcity->production;
3517
3518 log_debug("In %s, building %s. Beg of Turn shields = %d",
3520 pcity->before_change_shields);
3521}
3522
3523/**********************************************************************/
3526static void nullify_caravan_and_disband_plus(struct city *pcity)
3527{
3528 pcity->disbanded_shields = 0;
3529 pcity->caravan_shields = 0;
3530}
3531
3532/**********************************************************************/
3537{
3539 pcity->before_change_shields = 0;
3540}
3541
3542/**********************************************************************/
3545static void update_city_activity(struct city *pcity)
3546{
3547 struct player *pplayer;
3548 struct government *gov;
3549 bool is_happy;
3550 bool is_celebrating;
3551
3552 if (!pcity) {
3553 return;
3554 }
3555
3556 pplayer = city_owner(pcity);
3557 gov = government_of_city(pcity);
3558 is_happy = city_happy(pcity);
3560
3561 if (city_refresh(pcity)) {
3562 auto_arrange_workers(pcity);
3563 }
3564
3565 /* Reporting of celebrations rewritten, copying the treatment of disorder below,
3566 with the added rapture rounds count. 991219 -- Jing */
3567 if (city_build_stuff(pplayer, pcity)) {
3568 int saved_id;
3569 int revolution_turns;
3570
3571 pcity->history += city_history_gain(pcity);
3572
3573 /* History can decrease, but never go below zero */
3574 pcity->history = MAX(pcity->history, 0);
3575
3576 /* Keep old behaviour when building new improvement could keep
3577 city celebrating */
3578 if (!is_happy) {
3579 is_happy = city_happy(pcity);
3580 }
3581
3582 if (city_celebrating(pcity) || is_celebrating) {
3583 pcity->rapture++;
3584
3585 /* Update city's celebrating counters */
3587 if (pcount->type == CB_CITY_CELEBRATION_TURNS) {
3588 pcity->counter_values[pcount->index]++;
3589 }
3591
3592 if (pcity->rapture == 1) {
3594 _("Celebrations in your honor in %s."),
3595 city_link(pcity));
3596 }
3597 } else {
3598 if (pcity->rapture != 0) {
3600 _("Celebrations canceled in %s."),
3601 city_link(pcity));
3602 }
3603
3604 /* Update city's celebrating counters */
3606 if (pcount->type == CB_CITY_CELEBRATION_TURNS) {
3607 pcity->counter_values[pcount->index] = 0;
3608 }
3610 pcity->rapture = 0;
3611 }
3612 pcity->was_happy = is_happy;
3613
3614 /* Handle the illness. */
3615 if (game.info.illness_on) {
3616 /* Recalculate city illness; illness due to trade has to be saved
3617 * within the city struct, as the client does not have all
3618 * the data to calculate it */
3619 pcity->server.illness
3620 = city_illness_calc(pcity, NULL, NULL, &(pcity->illness_trade), NULL);
3621
3622 if (city_illness_check(pcity)) {
3623 if (!city_illness_strike(pcity)) {
3624 /* Illness destroyed the city */
3625 return;
3626 }
3627 }
3628 }
3629
3630 /* City population updated here, after the rapture stuff above. --Jing */
3631 saved_id = pcity->id;
3632 pcity->had_famine = FALSE;
3633 city_populate(pcity, pplayer);
3634 if (NULL == player_city_by_number(pplayer, saved_id)) {
3635 return;
3636 }
3637
3638 pcity->did_sell = FALSE;
3639 pcity->did_buy = FALSE;
3640 pcity->airlift = city_airlift_max(pcity);
3641 update_bulbs(pplayer, pcity->prod[O_SCIENCE], FALSE, FALSE);
3642
3644
3645 /* Update the treasury. */
3646 pplayer->economic.gold += pcity->surplus[O_GOLD];
3647
3648 /* FIXME: Nation level upkeep should be paid after ALL cities
3649 * have been processed, not after each individual city. */
3651 /* Unit upkeep was not included in city balance ->
3652 * not reduced from the city surplus. */
3653 pplayer->economic.gold -= city_total_unit_gold_upkeep(pcity);
3654
3656 /* Building upkeep was not included in city balance ->
3657 * not reduced from the city surplus. */
3658 pplayer->economic.gold -= city_total_impr_gold_upkeep(pcity);
3659 }
3660 }
3661
3662 /* Remember how much gold upkeep each unit was paid. */
3666
3667 if (pplayer->economic.gold < 0) {
3668 /* Not enough gold - we have to sell some buildings, and if that
3669 * is not enough, disband units with gold upkeep, taking into
3670 * account the setting of 'game.info.gold_upkeep_style':
3671 * GOLD_UPKEEP_CITY: Cities pay for buildings and units.
3672 * GOLD_UPKEEP_MIXED: Cities pay only for buildings; the nation pays
3673 * for units.
3674 * GOLD_UPKEEP_NATION: The nation pays for buildings and units. */
3675 switch (game.info.gold_upkeep_style) {
3676 case GOLD_UPKEEP_CITY:
3677 case GOLD_UPKEEP_MIXED:
3681 }
3682 break;
3683 case GOLD_UPKEEP_NATION:
3684 break;
3685 }
3686 }
3687
3689 if (city_unhappy(pcity)) {
3690 const char *revomsg;
3691
3692 pcity->anarchy++;
3693
3695 if (pcount->type == CB_CITY_DISORDER_TURNS) {
3696 pcity->counter_values[pcount->index]++;
3697 }
3699
3700 if (pcity->anarchy == revolution_turns) {
3701 /* Revolution next turn if not dealt with */
3702 /* TRANS: preserve leading space; this string will be appended to
3703 * another sentence */
3704 revomsg = _(" Unrest threatens to spread beyond the city.");
3705 } else {
3706 revomsg = "";
3707 }
3708 if (pcity->anarchy == 1) {
3710 /* TRANS: second %s is an optional extra sentence */
3711 _("Civil disorder in %s.%s"),
3712 city_link(pcity), revomsg);
3713 } else {
3715 /* TRANS: second %s is an optional extra sentence */
3716 _("CIVIL DISORDER CONTINUES in %s.%s"),
3717 city_link(pcity), revomsg);
3718 }
3719 } else {
3720 if (pcity->anarchy != 0) {
3722 _("Order restored in %s."),
3723 city_link(pcity));
3724 }
3725 pcity->anarchy = 0;
3726
3728 if (pcount->type == CB_CITY_DISORDER_TURNS) {
3729 pcity->counter_values[pcount->index] = 0;
3730 }
3732 }
3733 check_pollution(pcity);
3734
3735 send_city_info(NULL, pcity);
3736
3737 if (revolution_turns > 0 && pcity->anarchy > revolution_turns) {
3738 notify_player(pplayer, city_tile(pcity), E_ANARCHY, ftc_server,
3739 /* TRANS: %s - government form, e.g., Democracy */
3740 _("The people have overthrown your %s, "
3741 "your country is in turmoil."),
3744 }
3745 if (city_refresh(pcity)) {
3746 auto_arrange_workers(pcity);
3747 }
3748 sanity_check_city(pcity);
3749 }
3750}
3751
3752/**********************************************************************/
3755static bool city_illness_check(const struct city * pcity)
3756{
3757 if (fc_rand(1000) < pcity->server.illness) {
3758 return TRUE;
3759 }
3760
3761 return FALSE;
3762}
3763
3764/**********************************************************************/
3768static bool disband_city(struct city *pcity)
3769{
3770 struct player *pplayer = city_owner(pcity);
3771 struct tile *ptile = pcity->tile;
3772 struct city *rcity = NULL;
3773 const struct unit_type *utype = pcity->production.value.utype;
3774 struct unit *punit;
3775 int saved_id = pcity->id;
3776
3777 /* find closest city other than pcity */
3778 rcity = find_closest_city(ptile, pcity, pplayer, FALSE, FALSE, FALSE, TRUE,
3779 FALSE, NULL);
3780
3781 if (!rcity) {
3782 /* What should we do when we try to disband our only city? */
3783 notify_player(pplayer, ptile, E_CITY_CANTBUILD, ftc_server,
3784 _("%s can't build %s yet, "
3785 "as we can't disband our only city."),
3787 script_server_signal_emit("unit_cant_be_built", utype, pcity,
3788 "pop_cost");
3789 if (!city_exist(saved_id)) {
3790 /* Script decided to remove even the last city */
3791 return TRUE;
3792 } else {
3793 return FALSE;
3794 }
3795 }
3796
3797 punit = city_create_unit(pcity, utype, NULL);
3798
3799 /* "unit_built" script handler may have destroyed city. If so, we
3800 * assume something sensible happened to its units, and that the
3801 * script took care of announcing unit creation if required. */
3802 if (city_exist(saved_id)) {
3803 /* Shift all the units supported by pcity (including the new unit)
3804 * to rcity. transfer_city_units does not make sure no units are
3805 * left floating without a transport, but since all units are
3806 * transferred this is not a problem. */
3807 transfer_city_units(pplayer, pplayer, pcity->units_supported, rcity,
3808 pcity, -1, TRUE);
3809
3810 if (punit) {
3811 notify_player(pplayer, ptile, E_UNIT_BUILT, ftc_server,
3812 /* TRANS: "<city> is disbanded into Settler." */
3813 _("%s is disbanded into %s."),
3815 }
3816
3817 script_server_signal_emit("city_destroyed", pcity, pcity->owner, NULL);
3818
3819 if (!city_exist(saved_id)) {
3820 /* Already removed during the script */
3821 return TRUE;
3822 }
3823 remove_city(pcity);
3824
3825 /* Since we've removed the city, we don't need to worry about
3826 * charging for production, disabling rally points, etc. */
3827 }
3828
3829 return TRUE;
3830}
3831
3832/**********************************************************************/
3880static float city_migration_score(struct city *pcity)
3881{
3882 float score = 0.0;
3883 int build_shield_cost = 0;
3884 bool has_wonder = FALSE;
3885
3886 if (!pcity) {
3887 return score;
3888 }
3889
3890 if (pcity->server.mgr_score_calc_turn == game.info.turn) {
3891 /* up-to-date migration score */
3892 return pcity->server.migration_score;
3893 }
3894
3895 /* feeling of the citizens */
3896 score = (city_size_get(pcity)
3897 + 1.00 * pcity->feel[CITIZEN_HAPPY][FEELING_FINAL]
3898 + 0.00 * pcity->feel[CITIZEN_CONTENT][FEELING_FINAL]
3899 - 0.25 * pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL]
3900 - 0.50 * pcity->feel[CITIZEN_ANGRY][FEELING_FINAL]);
3901
3902 /* calculate shield build cost for all buildings */
3903 city_built_iterate(pcity, pimprove) {
3904 build_shield_cost += impr_build_shield_cost(pcity, pimprove);
3905 if (is_wonder(pimprove)) {
3906 /* this city has a wonder */
3907 has_wonder = TRUE;
3908 }
3910
3911 /* take shield costs of all buidings into account; normalized by 1000 */
3912 score *= (1 + (1 - exp(- (float) MAX(0, build_shield_cost) / 1000)) / 5);
3913 /* take trade into account; normalized by 100 */
3914 score *= (1 + (1 - exp(- (float) MAX(0, pcity->surplus[O_TRADE]) / 100))
3915 / 5);
3916 /* take luxury into account; normalized by 100 */
3917 score *= (1 + (1 - exp(- (float) MAX(0, pcity->surplus[O_LUXURY]) / 100))
3918 / 5);
3919 /* take science into account; normalized by 100 */
3920 score *= (1 + (1 - exp(- (float) MAX(0, pcity->surplus[O_SCIENCE]) / 100))
3921 / 5);
3922
3923 score += city_culture(pcity) * game.info.culture_migration_pml / 1000;
3924
3925 /* Take food into account; the food surplus is clipped to values between
3926 * -10..20 and normalize by 10. Thus, the factor is between 0.9 and 1.2. */
3927 score *= (1 + (float) CLIP(-10, pcity->surplus[O_FOOD], 20) / 10 );
3928
3929 /* Reduce the score due to city illness (plague). The illness is given in
3930 * tenth of percent (0..1000) and normalized by 25. Thus, this factor is
3931 * between 0.6 (ill city) and 1.0 (health city). */
3932 score *= (100 - (float)city_illness_calc(pcity, NULL, NULL, NULL, NULL)
3933 / 25);
3934
3935 if (has_wonder) {
3936 /* people like wonders */
3937 score *= 1.25;
3938 }
3939
3940 if (is_capital(pcity)) {
3941 /* the capital is a magnet for the citizens */
3942 score *= 1.25;
3943 }
3944
3945 /* take into account effects */
3946 score *= (1.0 + get_city_bonus(pcity, EFT_MIGRATION_PCT) / 100.0);
3947
3948 log_debug("[M] %s score: %.3f", city_name_get(pcity), score);
3949
3950 /* set migration score for the city */
3951 pcity->server.migration_score = score;
3952 /* set the turn, when the score was calculated */
3954
3955 return score;
3956}
3957
3958/**********************************************************************/
3965 struct city *pcity_to)
3966{
3968 struct tile *ptile_from, *ptile_to;
3970 const char *nation_from, *nation_to;
3971 struct city *rcity = NULL;
3972 int to_id = pcity_to->id;
3973 const struct civ_map *nmap = &(wld.map);
3974
3975 if (!pcity_from || !pcity_to) {
3976 return FALSE;
3977 }
3978
3982 /* We copy that, because city_link always returns the same pointer. */
3989
3990 /* Check food supply in the receiver city */
3992 bool migration = FALSE;
3993
3994 if (pcity_to->surplus[O_FOOD] >= game.info.food_cost) {
3995 migration = TRUE;
3996 } else {
3997 /* Check if there is a free tile for the new citizen which, when worked,
3998 * leads to zero or positive food surplus for the enlarged city */
3999 int max_food_tile = -1; /* no free tile */
4000
4002 city_tile(pcity_to), ptile) {
4003 if (city_can_work_tile(pcity_to, ptile)
4004 && tile_worked(ptile) != pcity_to) {
4005 /* Safest assumption is that city won't be celebrating once an
4006 * additional citizen is added */
4009 }
4011 if (max_food_tile >= 0
4012 && pcity_to->surplus[O_FOOD] + max_food_tile >= game.info.food_cost) {
4013 migration = TRUE;
4014 }
4015 }
4016
4017 if (!migration) {
4018 /* insufficiency food in receiver city; no additional citizens */
4019 if (pplayer_from == pplayer_to) {
4020 /* migration between one nation */
4022 /* TRANS: From <city1> to <city2>. */
4023 _("Migrants from %s can't go to %s because there is "
4024 "not enough food available!"),
4026 } else {
4027 /* migration between different nations */
4029 /* TRANS: From <city1> to <city2> (<city2 nation adjective>). */
4030 _("Migrants from %s can't go to %s (%s) because there "
4031 "is not enough food available!"),
4034 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
4035 _("Migrants from %s (%s) can't go to %s because there "
4036 "is not enough food available!"),
4038 }
4039
4040 return FALSE;
4041 }
4042 }
4043
4045 /* receiver city can't grow */
4046 if (pplayer_from == pplayer_to) {
4047 /* migration between one nation */
4049 /* TRANS: From <city1> to <city2>. */
4050 _("Migrants from %s can't go to %s because it needs "
4051 "an improvement to grow!"),
4053 } else {
4054 /* migration between different nations */
4056 /* TRANS: From <city1> to <city2> of <city2 nation adjective>. */
4057 _("Migrants from %s can't go to %s (%s) because it "
4058 "needs an improvement to grow!"),
4061 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
4062 _("Migrants from %s (%s) can't go to %s because it "
4063 "needs an improvement to grow!"),
4065 }
4066
4067 return FALSE;
4068 }
4069
4070 /* reduce size of giver */
4071 if (city_size_get(pcity_from) == 1) {
4072
4074 /* Preserve nationality of city's only citizen */
4076 }
4077
4078 /* do not destroy wonders */
4079 city_built_iterate(pcity_from, pimprove) {
4080 if (is_wonder(pimprove)) {
4081 return FALSE;
4082 }
4084
4085 /* find closest city other of the same player than pcity_from */
4087 FALSE, FALSE, TRUE, FALSE, NULL);
4088
4089 if (rcity) {
4090 int id = pcity_from->id;
4091
4092 /* transfer all units to the closest city */
4094 pcity_from->units_supported, rcity, pcity_from,
4095 -1, TRUE);
4097
4098 script_server_signal_emit("city_size_change", pcity_from,
4099 (lua_Integer)(-1), "migration_from");
4100
4101 if (city_exist(id)) {
4102 script_server_signal_emit("city_destroyed", pcity_from,
4103 pcity_from->owner, NULL);
4104
4105 if (city_exist(id)) {
4107 }
4108 }
4109
4111 _("%s was disbanded by its citizens."),
4112 name_from);
4113 } else {
4114 /* it's the only city of the nation */
4115 return FALSE;
4116 }
4117 } else {
4118 /* the migrants take half of the food box with them (this prevents
4119 * migration -> grow -> migration -> ... cycles) */
4120 pcity_from->food_stock /= 2;
4121
4123 /* Those citizens that are from the target nation are most
4124 * ones migrating. */
4125 if (citizens_nation_get(pcity_from, pplayer_to->slot) > 0) {
4127 } else if (!citizens_nation_get(pcity_from, pplayer_citizen->slot)) {
4128 /* No native citizens at all in the city, choose random foreigner */
4130
4132 }
4133 /* This should be followed by city_reduce_size(). */
4135 }
4136 city_reduce_size(pcity_from, 1, pplayer_from, "migration_from");
4138 if (city_refresh(pcity_from)) {
4140 }
4141 }
4142
4143 /* This should be _before_ the size of the city is increased. Thus, the
4144 * order of the messages is correct (1: migration; 2: increased size). */
4145 if (pplayer_from == pplayer_to) {
4146 /* migration between one nation */
4148 /* TRANS: From <city1> to <city2>. */
4149 _("Migrants from %s moved to %s in search of a better "
4150 "life."), name_from, name_to);
4151 } else {
4152 /* migration between different nations */
4154 /* TRANS: From <city1> to <city2> (<city2 nation adjective>). */
4155 _("Migrants from %s moved to %s (%s) in search of a "
4156 "better life."),
4159 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
4160 _("Migrants from %s (%s) moved to %s in search of a "
4161 "better life."),
4163 }
4164
4165 /* Increase size of receiver city */
4166 if (city_exist(to_id)) {
4168
4169 if (city_exist(to_id)) {
4172 if (city_refresh(pcity_to)) {
4174 }
4175 if (incr_success) {
4176 script_server_signal_emit("city_size_change", pcity_to,
4177 (lua_Integer)1, "migration_to");
4178 }
4179 }
4180 }
4181
4182 log_debug("[M] T%d migration successful (%s -> %s)",
4184
4185 return TRUE;
4186}
4187
4188/**********************************************************************/
4210{
4211 bool internat = FALSE;
4212
4213 if (!game.server.migration) {
4214 return FALSE;
4215 }
4216
4218 || (game.server.mgr_worldchance <= 0
4219 && game.server.mgr_nationchance <= 0)) {
4220 return FALSE;
4221 }
4222
4223 /* check for migration */
4224 players_iterate(pplayer) {
4225 if (!pplayer->cities) {
4226 continue;
4227 }
4228
4229 if (check_city_migrations_player(pplayer)) {
4230 internat = TRUE;
4231 }
4233
4234 return internat;
4235}
4236
4237/**********************************************************************/
4241bool city_empty_food_stock(struct city *pcity) {
4242 struct player *pplayer = city_owner(pcity);
4243 struct tile *ptile = city_tile(pcity);
4244
4245 fc_assert_ret_val(pcity != NULL, FALSE);
4246
4247 if (pcity->food_stock > 0) {
4248 pcity->food_stock = 0;
4249
4250 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4251 /* TRANS: %s is a city name */
4252 _("All stored food destroyed in %s."), city_link(pcity));
4253
4254 return TRUE;
4255 }
4256
4257 return FALSE;
4258}
4259
4260/**********************************************************************/
4263static void apply_disaster(struct city *pcity, struct disaster_type *pdis)
4264{
4265 struct player *pplayer = city_owner(pcity);
4266 struct tile *ptile = city_tile(pcity);
4268
4269 log_debug("%s at %s", disaster_rule_name(pdis), city_name_get(pcity));
4270
4271 notify_player(pplayer, ptile, E_DISASTER,
4272 ftc_server,
4273 /* TRANS: Disasters such as Earthquake */
4274 _("%s was hit by %s."), city_name_get(pcity),
4276
4278 if (place_pollution(pcity, EC_POLLUTION)) {
4279 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4280 _("Pollution near %s."), city_link(pcity));
4282 }
4283 }
4284
4286 if (place_pollution(pcity, EC_FALLOUT)) {
4287 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4288 _("Fallout near %s."), city_link(pcity));
4290 }
4291 }
4292
4295 && pcity->size > 1)) {
4296 if (!city_reduce_size(pcity, 1, NULL, "disaster")) {
4297 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4298 /* TRANS: "Industrial Accident destroys Bogota entirely." */
4299 _("%s destroys %s entirely."),
4301 pcity = NULL;
4302 } else {
4303 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4304 /* TRANS: "Nuclear Accident ... Montreal." */
4305 _("%s causes population loss in %s."),
4307 }
4308
4310 }
4311
4313 int total = 0;
4314 struct impr_type *imprs[B_LAST];
4315
4316 city_built_iterate(pcity, pimprove) {
4317 if (is_improvement(pimprove)
4318 && !improvement_has_flag(pimprove, IF_DISASTER_PROOF)) {
4319 imprs[total++] = pimprove;
4320 }
4322
4323 if (total > 0) {
4324 int num = fc_rand(total);
4325
4326 building_lost(pcity, imprs[num], "disaster", NULL);
4327
4328 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4329 /* TRANS: second %s is the name of a city improvement */
4330 _("%s destroys %s in %s."),
4333 city_link(pcity));
4334
4336 }
4337 }
4338
4340 if (city_empty_food_stock(pcity)) {
4342 }
4343 }
4344
4346 if (pcity->shield_stock > 0) {
4347 char prod[256];
4348
4349 pcity->shield_stock = 0;
4350 nullify_prechange_production(pcity); /* Make it impossible to recover */
4351
4352 universal_name_translation(&pcity->production, prod, sizeof(prod));
4353 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4354 /* TRANS: "Production of Colossus in Rhodes destroyed." */
4355 _("Production of %s in %s destroyed."),
4356 prod, city_link(pcity));
4357
4359 }
4360 }
4361
4362 script_server_signal_emit("disaster_occurred", pdis, pcity,
4364 script_server_signal_emit("disaster", pdis, pcity);
4365}
4366
4367/**********************************************************************/
4371{
4372 if (game.info.disasters == 0) {
4373 /* Shortcut out as no disaster is possible. */
4374 return;
4375 }
4376
4377 players_iterate(pplayer) {
4378 /* Safe city iterator needed as disaster may destroy city */
4379 city_list_iterate_safe(pplayer->cities, pcity) {
4380 int id = pcity->id;
4381
4383 if (city_exist(id)) {
4384 /* City survived earlier disasters. */
4385 int probability = game.info.disasters * pdis->frequency;
4386 int result = fc_rand(DISASTER_BASE_RARITY);
4387
4388 if (result < probability) {
4389 if (can_disaster_happen(pdis, pcity)) {
4390 apply_disaster(pcity, pdis);
4391 }
4392 }
4393 }
4397}
4398
4399/**********************************************************************/
4407static bool check_city_migrations_player(const struct player *pplayer)
4408{
4412 float score_from, score_tmp, weight;
4413 int dist, mgr_dist;
4414 bool internat = FALSE;
4415
4416 /* check for each city
4417 * city_list_iterate_safe_end must be used because we could
4418 * remove one city from the list */
4419 city_list_iterate_safe(pplayer->cities, pcity) {
4420 /* no migration out of the capital */
4421 if (is_capital(pcity)) {
4422 continue;
4423 }
4424
4425 /* check only each (game.server.mgr_turninterval) turn
4426 * (counted from the funding turn) and do not migrate
4427 * the same turn a city is founded */
4428 if (game.info.turn == pcity->turn_founded
4429 || ((game.info.turn - pcity->turn_founded)
4430 % game.server.mgr_turninterval) != 0) {
4431 continue;
4432 }
4433
4438
4439 /* score of the actual city
4440 * taking into account a persistence factor of 3 */
4441 score_from = city_migration_score(pcity) * 3;
4442
4443 log_debug("[M] T%d check city: %s score: %6.3f (%s)",
4445 player_name(pplayer));
4446
4447 /* consider all cities within the maximal possible distance
4448 * (= CITY_MAP_MAX_RADIUS + GAME_MAX_MGR_DISTANCE) */
4449 iterate_outward(&(wld.map), city_tile(pcity),
4451 acity = tile_city(ptile);
4452
4453 if (!acity || acity == pcity) {
4454 /* no city or the city in the center */
4455 continue;
4456 }
4457
4458 /* Calculate the migration distance. The value of
4459 * game.server.mgr_distance is added to the current city radius. If the
4460 * distance between both cities is lower or equal than this value,
4461 * migration is possible. */
4464
4465 /* distance between the two cities */
4466 dist = real_map_distance(city_tile(pcity), city_tile(acity));
4467
4468 if (dist > mgr_dist) {
4469 /* to far away */
4470 continue;
4471 }
4472
4473 /* score of the second city, weighted by the distance */
4474 weight = ((float) (mgr_dist + 1 - dist) / (float) (mgr_dist + 1));
4476
4477 log_debug("[M] T%d - compare city: %s (%s) dist: %d mgr_dist: %d "
4478 "score: %6.3f", game.info.turn, city_name_get(acity),
4480
4481 if (game.server.mgr_nationchance > 0 && city_owner(acity) == pplayer) {
4482 /* migration between cities of the same owner */
4484 /* select the best! */
4487
4488 log_debug("[M] T%d - best city (player): %s (%s) score: "
4489 "%6.3f (> %6.3f)", game.info.turn,
4492 }
4493 } else if (game.server.mgr_worldchance > 0
4494 && city_owner(acity) != pplayer) {
4495 /* migration between cities of different owners */
4497 /* Modify the score if citizens could migrate to a city of their
4498 * original nation. */
4499 if (citizens_nation_get(pcity, city_owner(acity)->slot) > 0) {
4500 score_tmp *= 2;
4501 }
4502 }
4503
4505 /* select the best! */
4508
4509 log_debug("[M] T%d - best city (world): %s (%s) score: "
4510 "%6.3f (> %6.3f)", game.info.turn,
4514 }
4515 }
4517
4518 if (best_city_player != NULL) {
4519 /* First, do the migration within one nation */
4520 if (fc_rand(100) >= game.server.mgr_nationchance) {
4521 /* No migration */
4522 /* N.B.: city_link always returns the same pointer. */
4525 _("Citizens of %s are thinking about migrating to %s "
4526 "for a better life."),
4528 } else {
4530 }
4531
4532 /* Stop here */
4533 continue;
4534 }
4535
4536 if (best_city_world != NULL) {
4537 /* Second, do the migration between all nations */
4538 if (fc_rand(100) >= game.server.mgr_worldchance) {
4539 const char *nname;
4540
4542 /* No migration */
4543 /* N.B.: city_link always returns the same pointer. */
4546 /* TRANS: <city1> to <city2> (<city2 nation adjective>). */
4547 _("Citizens of %s are thinking about migrating to %s "
4548 "(%s) for a better life."),
4550 } else {
4552 internat = TRUE;
4553 }
4554
4555 /* Stop here */
4556 continue;
4557 }
4559
4560 return internat;
4561}
4562
4563/**********************************************************************/
4566void city_style_refresh(struct city *pcity)
4567{
4568 pcity->style = city_style(pcity);
4569}
4570
4571/**********************************************************************/
4575void city_counters_refresh(struct city *pcity)
4576{
4578 struct packet_city_update_counters packet;
4579
4580 packet.city = pcity->id;
4581
4583
4584 packet.count = counter_count;
4585 for (i = 0; i < counter_count; i++) {
4586 packet.counters[i] = pcity->counter_values[i];
4587 }
4588
4591}
4592
4593/**********************************************************************/
4596void city_tc_effect_refresh(struct player *pplayer)
4597{
4598 const struct civ_map *nmap = &(wld.map);
4599
4600 city_list_iterate(pplayer->cities, pcity) {
4601 bool changed = FALSE;
4602
4604 city_tile(pcity), ptile, idx, x, y) {
4605 if (ptile->worked == pcity
4606 && get_city_tile_output_bonus(pcity, ptile, NULL, EFT_TILE_WORKABLE) <= 0) {
4607 city_map_update_empty(pcity, ptile);
4608 pcity->specialists[DEFAULT_SPECIALIST]++;
4609 changed = TRUE;
4610 }
4612
4613 if (changed) {
4614 auto_arrange_workers(pcity);
4615 send_city_info(NULL, pcity);
4616 }
4618}
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:662
bool can_city_build_unit_later(const struct city *pcity, const struct unit_type *punittype)
Definition city.c:965
int city_granary_size(int city_size)
Definition city.c:2123
bool can_city_build_now(const struct city *pcity, const struct universal *target)
Definition city.c:1006
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:304
bool city_has_building(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:1231
bool is_capital(const struct city *pcity)
Definition city.c:1571
const char * city_name_get(const struct city *pcity)
Definition city.c:1128
void city_remove_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3359
int city_improvement_upkeep(const struct city *pcity, const struct impr_type *b)
Definition city.c:1246
int city_airlift_max(const struct city *pcity)
Definition city.c:2933
struct output_type * get_output_type(Output_type_id output)
Definition city.c:637
bool is_city_option_set(const struct city *pcity, enum city_options option)
Definition city.c:3376
void city_size_add(struct city *pcity, int add)
Definition city.c:1155
bool city_production_has_flag(const struct city *pcity, enum impr_flag_id flag)
Definition city.c:726
void city_refresh_from_main_map(struct city *pcity, bool *workers_map)
Definition city.c:3165
int city_production_unit_veteran_level(struct city *pcity, const struct unit_type *punittype)
Definition city.c:803
bool can_city_build_improvement_now(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:853
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:100
bool city_rapture_grow(const struct city *pcity)
Definition city.c:1645
int city_production_turns_to_build(const struct city *pcity, bool include_shield_stock)
Definition city.c:819
bool city_unhappy(const struct city *pcity)
Definition city.c:1618
Specialist_type_id best_specialist(Output_type_id otype, const struct city *pcity)
Definition city.c:3322
struct city * city_list_find_number(struct city_list *This, int id)
Definition city.c:1671
bool city_celebrating(const struct city *pcity)
Definition city.c:1637
bool can_city_build_improvement_direct(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:829
int city_illness_calc(const struct city *pcity, int *ill_base, int *ill_size, int *ill_trade, int *ill_pollution)
Definition city.c:2861
bool city_can_grow_to(const struct city *pcity, int pop_size)
Definition city.c:2004
bool city_happy(const struct city *pcity)
Definition city.c:1606
int city_map_radius_sq_get(const struct city *pcity)
Definition city.c:136
static int cmp(int v1, int v2)
Definition city.c:324
int city_tile_output(const struct city *pcity, const struct tile *ptile, bool is_celebrating, Output_type_id otype)
Definition city.c:1274
int city_map_tiles(int city_radius_sq)
Definition city.c:170
bool can_city_build_unit_direct(const struct city *pcity, const struct unit_type *punittype)
Definition city.c:902
bool city_exist(int id)
Definition city.c:3560
bool city_can_work_tile(const struct city *pcity, const struct tile *ptile)
Definition city.c:1447
bool city_production_build_units(const struct city *pcity, bool add_production, int *num_units)
Definition city.c:746
void city_rally_point_clear(struct city *pcity)
Definition city.c:3614
bool can_city_build_improvement_later(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:870
bool city_had_recent_plague(const struct city *pcity)
Definition city.c:2914
bool city_can_change_build(const struct city *pcity)
Definition city.c:1070
int city_total_unit_gold_upkeep(const struct city *pcity)
Definition city.c:1213
int city_total_impr_gold_upkeep(const struct city *pcity)
Definition city.c:1192
bool can_city_build_unit_now(const struct city *pcity, const struct unit_type *punittype)
Definition city.c:945
#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:833
#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:822
#define city_built_iterate_end
Definition city.h:828
#define output_type_iterate_end
Definition city.h:839
void city_map_update_empty(struct city *pcity, struct tile *ptile)
Definition citytools.c:3239
void send_city_info(struct player *dest, struct city *pcity)
Definition citytools.c:2343
struct city * find_closest_city(const struct tile *ptile, const struct city *pexclcity, const struct player *pplayer, bool only_ocean, bool only_continent, bool only_known, bool only_player, bool only_enemy, const struct unit_class *pclass)
Definition citytools.c:851
void sync_cities(void)
Definition citytools.c:3313
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:3438
void change_build_target(struct player *pplayer, struct city *pcity, struct universal *target, enum event_type event)
Definition citytools.c:3150
bool city_illness_strike(struct city *pcity)
Definition citytools.c:2942
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:3039
void city_units_upkeep(const struct city *pcity)
Definition citytools.c:3127
void remove_city(struct city *pcity)
Definition citytools.c:1699
void do_sell_building(struct player *pplayer, struct city *pcity, struct impr_type *pimprove, const char *reason)
Definition citytools.c:2968
struct trade_route * remove_trade_route(struct city *pc1, struct trade_route *proute, bool announce, bool source_gone)
Definition citytools.c:2907
void broadcast_city_info(struct city *pcity)
Definition citytools.c:2225
void city_add_improvement_with_gov_notice(struct city *pcity, const struct impr_type *pimprove, const char *format)
Definition citytools.c:3632
void city_map_update_all(struct city *pcity)
Definition citytools.c:3332
void transfer_city_units(struct player *pplayer, struct player *pvictim, struct unit_list *units, struct city *pcity, struct city *exclude_city, int kill_outside, bool verbose)
Definition citytools.c:716
void city_map_update_worker(struct city *pcity, struct tile *ptile)
Definition citytools.c:3253
void city_refresh_vision(struct city *pcity)
Definition citytools.c:3413
#define LOG_BUILD_TARGET
Definition citytools.h:21
static bool city_illness_check(const struct city *pcity)
Definition cityturn.c:3755
static void uk_rem_gold_append(struct unit *punit)
Definition cityturn.c:3070
static bool worklist_change_build_target(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2170
void remove_obsolete_buildings(struct player *pplayer)
Definition cityturn.c:270
static bool city_build_stuff(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2971
int city_shrink_granary_savings(const struct city *pcity)
Definition cityturn.c:892
static void apply_disaster(struct city *pcity, struct disaster_type *pdis)
Definition cityturn.c:4263
static bool city_distribute_surplus_shields(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2517
static void unit_list_referred_destroy(struct unit_list *punitlist)
Definition cityturn.c:3083
static bool place_pollution(struct city *pcity, enum extra_cause cause)
Definition cityturn.c:3368
void choose_build_target(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2384
static void uk_rem_gold_callback(struct unit *punit)
Definition cityturn.c:3049
void auto_arrange_workers(struct city *pcity)
Definition cityturn.c:366
void city_refresh_queue_add(struct city *pcity)
Definition cityturn.c:197
static struct unit_list * uk_rem_gold
Definition cityturn.c:94
static bool city_balance_treasury_buildings(struct city *pcity)
Definition cityturn.c:3286
static void update_city_activity(struct city *pcity)
Definition cityturn.c:3545
static void upgrade_unit_prod(struct city *pcity)
Definition cityturn.c:2494
static float city_migration_score(struct city *pcity)
Definition cityturn.c:3880
void nullify_prechange_production(struct city *pcity)
Definition cityturn.c:3536
bool city_empty_food_stock(struct city *pcity)
Definition cityturn.c:4241
bool check_city_migrations(void)
Definition cityturn.c:4209
static void upgrade_building_prod(struct city *pcity)
Definition cityturn.c:2448
bool player_balance_treasury_units_and_buildings(struct player *pplayer)
Definition cityturn.c:3189
bool city_change_size(struct city *pcity, citizens size, struct player *nationality, const char *reason)
Definition cityturn.c:1031
static citizens city_reduce_specialists(struct city *pcity, citizens change)
Definition cityturn.c:718
static void city_global_turn_notify(struct conn_list *dest)
Definition cityturn.c:490
static void city_populate(struct city *pcity, struct player *nationality)
Definition cityturn.c:1082
static void city_refresh_after_city_size_increase(struct city *pcity, struct player *nationality)
Definition cityturn.c:997
static void define_orig_production_values(struct city *pcity)
Definition cityturn.c:3505
void city_repair_size(struct city *pcity, int change)
Definition cityturn.c:851
#define cityimpr_list_iterate(cityimprlist, pcityimpr)
Definition cityturn.c:128
void city_counters_refresh(struct city *pcity)
Definition cityturn.c:4575
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:686
static bool city_build_building(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2598
static struct unit static const struct impr_type * building_upgrades_to(struct city *pcity, const struct impr_type *pimprove)
Definition cityturn.c:2427
void send_city_turn_notifications(struct connection *pconn)
Definition cityturn.c:576
static struct unit * sell_random_unit(struct player *pplayer, struct unit_list *punitlist)
Definition cityturn.c:3106
int city_incite_cost(struct player *pplayer, struct city *pcity)
Definition cityturn.c:3424
static void nullify_caravan_and_disband_plus(struct city *pcity)
Definition cityturn.c:3526
static bool sell_random_building(struct player *pplayer, struct cityimpr_list *imprs)
Definition cityturn.c:3003
static bool disband_city(struct city *pcity)
Definition cityturn.c:3768
void update_city_activities(struct player *pplayer)
Definition cityturn.c:602
bool city_reduce_size(struct city *pcity, citizens pop_loss, struct player *destroyer, const char *reason)
Definition cityturn.c:762
#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:511
static bool city_increase_size(struct city *pcity)
Definition cityturn.c:920
void apply_cmresult_to_city(struct city *pcity, const struct cm_result *cmr)
Definition cityturn.c:281
bool player_balance_treasury_units(struct player *pplayer)
Definition cityturn.c:3252
void city_style_refresh(struct city *pcity)
Definition cityturn.c:4566
static bool city_balance_treasury_units(struct city *pcity)
Definition cityturn.c:3333
static const struct unit_type * unit_upgrades_to(struct city *pcity, const struct unit_type *id)
Definition cityturn.c:2473
static bool do_city_migration(struct city *pcity_from, struct city *pcity_to)
Definition cityturn.c:3964
static struct city_list * city_refresh_queue
Definition cityturn.c:88
void city_refresh_for_player(struct player *pplayer)
Definition cityturn.c:182
void city_tc_effect_refresh(struct player *pplayer)
Definition cityturn.c:4596
static void check_pollution(struct city *pcity)
Definition cityturn.c:3407
static void city_reset_foodbox(struct city *pcity, int new_size, bool shrink)
Definition cityturn.c:905
void check_disasters(void)
Definition cityturn.c:4370
static void set_default_city_manager(struct cm_parameter *cmp, struct city *pcity)
Definition cityturn.c:313
static citizens city_reduce_workers(struct city *pcity, citizens change)
Definition cityturn.c:738
static bool worklist_item_postpone_req_vec(struct universal *target, struct city *pcity, struct player *pplayer, int saved_id)
Definition cityturn.c:1159
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:2755
void city_refresh_queue_processing(void)
Definition cityturn.c:213
int city_growth_granary_savings(const struct city *pcity)
Definition cityturn.c:878
void remove_obsolete_buildings_city(struct city *pcity, bool refresh)
Definition cityturn.c:235
static bool check_city_migrations_player(const struct player *pplayer)
Definition cityturn.c:4407
static bool city_build_unit(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2816
void cm_clear_cache(struct city *pcity)
Definition cm.c:322
void cm_init_parameter(struct cm_parameter *dest)
Definition cm.c:2180
struct cm_result * cm_result_new(struct city *pcity)
Definition cm.c:345
void cm_result_destroy(struct cm_result *result)
Definition cm.c:368
void cm_print_result(const struct cm_result *result)
Definition cm.c:2466
void cm_query_result(struct city *pcity, const struct cm_parameter *param, struct cm_result *result, bool negative_ok)
Definition cm.c:2120
void cm_init_emergency_parameter(struct cm_parameter *dest)
Definition cm.c:2198
void cm_print_city(const struct city *pcity)
Definition cm.c:2428
char * incite_cost
Definition comments.c:74
struct player * conn_get_player(const struct connection *pconn)
Definition connection.c:763
void conn_list_do_unbuffer(struct conn_list *dest)
Definition connection.c:366
void conn_list_do_buffer(struct conn_list *dest)
Definition connection.c:356
const char * counter_name_translation(const struct counter *counter)
Definition counters.c:157
int counters_get_city_counters_count(void)
Definition counters.c:74
#define city_counters_iterate_end
Definition counters.h:64
#define city_counters_iterate(pcount)
Definition counters.h:57
int city_culture(const struct city *pcity)
Definition culture.c:29
int city_history_gain(const struct city *pcity)
Definition culture.c: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:380
unsigned char citizens
Definition fc_types.h:391
@ RPT_CERTAIN
Definition fc_types.h:704
@ RPT_POSSIBLE
Definition fc_types.h:703
#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:381
#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:529
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:2232
void map_claim_border(struct tile *ptile, struct player *owner, int radius_sq)
Definition maphand.c:2264
void update_tile_knowledge(struct tile *ptile)
Definition maphand.c:1436
#define FC_FREE(ptr)
Definition mem.h:41
#define fc_malloc(sz)
Definition mem.h:34
const char * nation_rule_name(const struct nation_type *pnation)
Definition nation.c:138
const char * nation_adjective_for_player(const struct player *pplayer)
Definition nation.c:169
const char * nation_adjective_translation(const struct nation_type *pnation)
Definition nation.c:149
struct nation_type * nation_of_city(const struct city *pcity)
Definition nation.c:454
const char * nation_group_name_translation(const struct nation_group *pgroup)
Definition nation.c:1090
const char * nation_plural_translation(const struct nation_type *pnation)
Definition nation.c:159
const char * nation_plural_for_player(const struct player *pplayer)
Definition nation.c:178
void notify_research(const struct research *presearch, const struct player *exclude, enum event_type event, const struct ft_color color, const char *format,...)
Definition notify.c:394
void notify_player(const struct player *pplayer, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
Definition notify.c:292
void notify_research_embassies(const struct research *presearch, const struct player *exclude, enum event_type event, const struct ft_color color, const char *format,...)
Definition notify.c:434
void notify_conn(struct conn_list *dest, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
Definition notify.c:239
void package_event(struct packet_chat_msg *packet, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
Definition notify.c:168
void event_cache_add_for_player(const struct packet_chat_msg *packet, const struct player *pplayer)
Definition notify.c:646
void lsend_packet_city_update_counters(struct conn_list *dest, const struct packet_city_update_counters *packet)
void lsend_packet_chat_msg(struct conn_list *dest, const struct packet_chat_msg *packet)
struct city_list * cities
Definition packhand.c:119
int player_number(const struct player *pplayer)
Definition player.c:837
const char * player_name(const struct player *pplayer)
Definition player.c:895
struct city * player_city_by_number(const struct player *pplayer, int city_id)
Definition player.c:1203
const char * diplrel_name_translation(int value)
Definition player.c: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:1142
#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:162
int incite_total_factor
Definition game.h:150
int mgr_nationchance
Definition game.h:160
struct conn_list * glob_observers
Definition game.h:98
bool mgr_foodneeded
Definition game.h:159
int base_incite_cost
Definition game.h:134
struct conn_list * est_connections
Definition game.h:97
struct packet_game_info info
Definition game.h:89
int mgr_turninterval
Definition game.h:161
bool migration
Definition game.h:164
int incite_improvement_factor
Definition game.h:149
int aqueductloss
Definition game.h:129
struct civ_game::@31::@35 server
int incite_unit_factor
Definition game.h:151
int mgr_distance
Definition game.h:158
Definition cm.h:52
struct requirement_vector reqs
Definition improvement.h:58
int counters[MAX_COUNTERS]
enum gold_upkeep_style gold_upkeep_style
bool unit_builders_nationality
int infra_points
Definition player.h: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:903
universals_u value
Definition fc_types.h:902
struct civ_map map
int city_style(struct city *pcity)
Definition style.c:241
const char * style_name_translation(const struct nation_style *pstyle)
Definition style.c:99
#define sz_strlcpy(dest, src)
Definition support.h:189
#define fc__attribute(x)
Definition support.h:99
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
#define fc__fallthrough
Definition support.h:119
const char * advance_name_translation(const struct advance *padvance)
Definition tech.c: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:1387
void give_immediate_free_tech(struct research *presearch, Tech_type_id tech)
Definition techtools.c:1406
void update_bulbs(struct player *pplayer, int bulbs, bool check_tech, bool free_bulbs)
Definition techtools.c:654
const char * terrain_name_translation(const struct terrain *pterrain)
Definition terrain.c:238
void tile_add_extra(struct tile *ptile, const struct extra_type *pextra)
Definition tile.c:955
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
#define tile_worked(_tile)
Definition tile.h: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:724
const struct impr_type * building
Definition fc_types.h:717
bool unit_is_alive(int id)
Definition unit.c:2249
#define unit_tile(_pu)
Definition unit.h:390
#define unit_cargo_iterate_end
Definition unit.h:565
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition unit.h:562
#define unit_owner(_pu)
Definition unit.h:389
#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:1767
bool place_unit(struct unit *punit, struct player *pplayer, struct city *pcity, struct unit *ptrans, bool force)
Definition unittools.c:1709
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:1661
void unit_unset_removal_callback(struct unit *punit)
Definition unittools.c:1781
void wipe_unit(struct unit *punit, enum unit_loss_reason reason, struct player *killer)
Definition unittools.c:2135
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:878
#define unit_tech_reqs_iterate(_utype_, _p)
Definition unittype.h:872
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