Freeciv-3.1
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 "culture.h"
45#include "events.h"
46#include "disaster.h"
47#include "game.h"
48#include "government.h"
49#include "map.h"
50#include "player.h"
51#include "research.h"
52#include "road.h"
53#include "server_settings.h"
54#include "specialist.h"
55#include "tech.h"
56#include "traderoutes.h"
57#include "unit.h"
58#include "unitlist.h"
59
60/* common/scriptcore */
61#include "luascript_types.h"
62
63/* server */
64#include "citizenshand.h"
65#include "citytools.h"
66#include "cityturn.h"
67#include "maphand.h"
68#include "notify.h"
69#include "plrhand.h"
70#include "sanitycheck.h"
71#include "spacerace.h"
72#include "srv_log.h"
73#include "srv_main.h"
74#include "techtools.h"
75#include "unittools.h"
76#include "unithand.h"
77
78/* server/advisors */
79#include "advbuilding.h"
80#include "advdata.h"
81#include "autosettlers.h"
82
83/* server/scripting */
84#include "script_server.h"
85
86/* Queue for pending city_refresh() */
87static struct city_list *city_refresh_queue = NULL;
88
89/* The game is currently considering to remove the listed units because of
90 * missing gold upkeep. A unit ends up here if it has gold upkeep that
91 * can't be paid. A random unit in the list will be removed until the
92 * problem is solved. */
93static struct unit_list *uk_rem_gold = NULL;
94
95static void check_pollution(struct city *pcity);
96static void city_populate(struct city *pcity, struct player *nationality);
97
98static bool worklist_change_build_target(struct player *pplayer,
99 struct city *pcity);
100
101static bool city_distribute_surplus_shields(struct player *pplayer,
102 struct city *pcity);
103static bool city_build_building(struct player *pplayer, struct city *pcity);
104static bool city_build_unit(struct player *pplayer, struct city *pcity);
105static bool city_build_stuff(struct player *pplayer, struct city *pcity);
106static struct unit *city_create_unit(struct city *pcity,
107 const struct unit_type *utype,
108 struct citizens_reduction *red)
109 fc__attribute((nonnull (1, 2)));
110static const struct impr_type *building_upgrades_to(struct city *pcity,
111 const struct impr_type *pimprove);
112static void upgrade_building_prod(struct city *pcity);
113static const struct unit_type *unit_upgrades_to(struct city *pcity,
114 const struct unit_type *id);
115static void upgrade_unit_prod(struct city *pcity);
116
117/* Helper struct for associating a building to a city. */
118struct cityimpr {
119 struct city *pcity;
121};
122
123#define SPECLIST_TAG cityimpr
124#define SPECLIST_TYPE struct cityimpr
125#include "speclist.h"
126
127#define cityimpr_list_iterate(cityimprlist, pcityimpr) \
128 TYPED_LIST_ITERATE(struct cityimpr, cityimprlist, pcityimpr)
129#define cityimpr_list_iterate_end LIST_ITERATE_END
130
131static bool sell_random_building(struct player *pplayer,
132 struct cityimpr_list *imprs);
133static struct unit *sell_random_unit(struct player *pplayer,
134 struct unit_list *punitlist);
135
136static citizens city_reduce_specialists(struct city *pcity, citizens change);
137static citizens city_reduce_workers(struct city *pcity, citizens change);
138
139static bool city_balance_treasury_buildings(struct city *pcity);
140static bool city_balance_treasury_units(struct city *pcity);
142 (struct player *pplayer);
143static bool player_balance_treasury_units(struct player *pplayer);
144
145static bool disband_city(struct city *pcity);
146
147static void define_orig_production_values(struct city *pcity);
148static void update_city_activity(struct city *pcity);
149static void nullify_caravan_and_disband_plus(struct city *pcity);
150static bool city_illness_check(const struct city * pcity);
151
152static float city_migration_score(struct city *pcity);
153static bool do_city_migration(struct city *pcity_from,
154 struct city *pcity_to);
155static bool check_city_migrations_player(const struct player *pplayer);
156
157/**********************************************************************/
161bool city_refresh(struct city *pcity)
162{
163 bool retval;
164
165 pcity->server.needs_refresh = FALSE;
166
167 retval = city_map_update_radius_sq(pcity);
168 city_units_upkeep(pcity); /* update unit upkeep */
169 city_refresh_from_main_map(pcity, NULL);
170 city_style_refresh(pcity);
171
172 if (retval) {
173 /* Force a sync of the city after the change. */
174 send_city_info(city_owner(pcity), pcity);
175 }
176
177 return retval;
178}
179
180/**********************************************************************/
184void city_refresh_for_player(struct player *pplayer)
185{
187 city_list_iterate(pplayer->cities, pcity) {
188 if (city_refresh(pcity)) {
190 }
191 send_city_info(pplayer, pcity);
194}
195
196/**********************************************************************/
199void city_refresh_queue_add(struct city *pcity)
200{
201 if (NULL == city_refresh_queue) {
202 city_refresh_queue = city_list_new();
203 } else if (city_list_find_number(city_refresh_queue, pcity->id)) {
204 return;
205 }
206
207 city_list_prepend(city_refresh_queue, pcity);
208 pcity->server.needs_refresh = TRUE;
209}
210
211/**********************************************************************/
216{
217 if (NULL == city_refresh_queue) {
218 return;
219 }
220
222 if (pcity->server.needs_refresh) {
223 if (city_refresh(pcity)) {
225 }
226 send_city_info(city_owner(pcity), pcity);
227 }
229
230 city_list_destroy(city_refresh_queue);
231 city_refresh_queue = NULL;
232}
233
234/**********************************************************************/
237void remove_obsolete_buildings_city(struct city *pcity, bool refresh)
238{
239 struct player *pplayer = city_owner(pcity);
240 bool sold = FALSE;
241
242 city_built_iterate(pcity, pimprove) {
243 if (improvement_obsolete(pplayer, pimprove, pcity)
244 && can_city_sell_building(pcity, pimprove)) {
245 int sgold;
246
247 do_sell_building(pplayer, pcity, pimprove, "obsolete");
248 sgold = impr_sell_gold(pimprove);
249 notify_player(pplayer, city_tile(pcity), E_IMP_SOLD, ftc_server,
250 PL_("%s is selling %s (obsolete) for %d.",
251 "%s is selling %s (obsolete) for %d.",
252 sgold),
253 city_link(pcity),
255 sgold);
256 sold = TRUE;
257 }
259
260 if (sold && refresh) {
261 if (city_refresh(pcity)) {
263 }
264 send_city_info(pplayer, pcity);
265 send_player_info_c(pplayer, NULL); /* Send updated gold to all */
266 }
267}
268
269/**********************************************************************/
273{
274 city_list_iterate(pplayer->cities, pcity) {
277}
278
279/**********************************************************************/
283void apply_cmresult_to_city(struct city *pcity,
284 const struct cm_result *cmr)
285{
286 struct tile *pcenter = city_tile(pcity);
287 const struct civ_map *nmap = &(wld.map);
288
289 /* Now apply results */
291 ptile, idx, x, y) {
292 struct city *pwork = tile_worked(ptile);
293
294 if (cmr->worker_positions[idx]) {
295 if (NULL == pwork) {
296 city_map_update_worker(pcity, ptile);
297 } else {
298 fc_assert(pwork == pcity);
299 }
300 } else {
301 if (pwork == pcity) {
302 city_map_update_empty(pcity, ptile);
303 }
304 }
306
308 pcity->specialists[sp] = cmr->specialists[sp];
310}
311
312/**********************************************************************/
316 struct city *pcity)
317{
318 int csize = city_size_get(pcity);
319 int gsize = city_granary_size(csize);
320
321 cmp->require_happy = FALSE;
322 cmp->allow_disorder = FALSE;
323 cmp->allow_specialists = TRUE;
324
325 /* We used to look at pplayer->ai.xxx_priority to determine the values
326 * to be used here. However that doesn't work at all because those values
327 * are on a different scale. Later the AI may wish to adjust its
328 * priorities - this should be done via a separate set of variables. */
329 if (csize > 1) {
330 if (csize <= game.info.notradesize) {
331 cmp->factor[O_FOOD] = 15;
332 } else {
333 if (gsize == pcity->food_stock) {
334 /* We don't need more food if the granary is full. */
335 cmp->factor[O_FOOD] = 0;
336 } else {
337 cmp->factor[O_FOOD] = 10;
338 }
339 }
340 } else {
341 /* Growing to size 2 is the highest priority. */
342 cmp->factor[O_FOOD] = 20;
343 }
344
345 cmp->factor[O_SHIELD] = 5;
346 cmp->factor[O_TRADE] = 0; /* Trade only provides gold/science. */
347 cmp->factor[O_GOLD] = 2;
348 cmp->factor[O_LUXURY] = 0; /* Luxury only influences happiness. */
349 cmp->factor[O_SCIENCE] = 2;
350 cmp->happy_factor = 0;
351
352 if (gsize == pcity->food_stock) {
353 cmp->minimal_surplus[O_FOOD] = 0;
354 } else {
355 cmp->minimal_surplus[O_FOOD] = 1;
356 }
357
358 cmp->minimal_surplus[O_SHIELD] = 1;
359 cmp->minimal_surplus[O_TRADE] = 0;
360 cmp->minimal_surplus[O_GOLD] = -FC_INFINITY;
361 cmp->minimal_surplus[O_LUXURY] = 0;
362 cmp->minimal_surplus[O_SCIENCE] = 0;
363}
364
365/**********************************************************************/
368void auto_arrange_workers(struct city *pcity)
369{
370 struct cm_parameter cmp;
371 struct cm_parameter *pcmp;
372 struct cm_result *cmr;
373 bool broadcast_needed;
374
375 /* See comment in freeze_workers(): we can't rearrange while
376 * workers are frozen (i.e. multiple updates need to be done). */
377 if (pcity->server.workers_frozen > 0) {
378 if (pcity->server.needs_arrange == CNA_NOT) {
380 }
381 return;
382 }
384
385 broadcast_needed = (pcity->server.needs_arrange == CNA_BROADCAST_PENDING);
386
387 /* Freeze the workers and make sure all the tiles around the city
388 * are up to date. Then thaw, but hackishly make sure that thaw
389 * doesn't call us recursively, which would waste time. */
390 city_freeze_workers(pcity);
392
393 city_map_update_all(pcity);
394
396 city_thaw_workers(pcity);
397
398 /* Now start actually rearranging. */
399 city_refresh(pcity);
400
401 sanity_check_city(pcity);
402 cm_clear_cache(pcity);
403
404 if (pcity->cm_parameter) {
405 pcmp = pcity->cm_parameter;
406 } else {
407 pcmp = &cmp;
408 cm_init_parameter(pcmp);
409 set_default_city_manager(pcmp, pcity);
410 }
411
412 /* This must be after city_refresh() so that the result gets created for the right
413 * city radius */
414 cmr = cm_result_new(pcity);
415 cm_query_result(pcity, pcmp, cmr, FALSE);
416
417 if (!cmr->found_a_valid) {
418 if (pcity->cm_parameter) {
419 /* If player-defined parameters fail, cancel and notify player. */
420 free(pcity->cm_parameter);
421 pcity->cm_parameter = NULL;
422
423 notify_player(city_owner(pcity), city_tile(pcity),
424 E_CITY_CMA_RELEASE, ftc_server,
425 _("The citizen governor can't fulfill the requirements "
426 "for %s. Passing back control."),
427 city_link(pcity));
428
429 /* Switch to default parameters, and try with them */
430 pcmp = &cmp;
431 cm_init_parameter(pcmp);
432 set_default_city_manager(pcmp, pcity);
433 cm_query_result(pcity, pcmp, cmr, FALSE);
434 }
435
436 if (!cmr->found_a_valid) {
437 /* Drop surpluses and try again. */
439 cmp.minimal_surplus[o] = 0;
441 cmp.minimal_surplus[O_GOLD] = -FC_INFINITY;
442 cm_query_result(pcity, pcmp, cmr, FALSE);
443 }
444 }
445 if (!cmr->found_a_valid) {
446 /* Emergency management. Get _some_ result. This doesn't use
447 * cm_init_emergency_parameter() so we can keep the factors from
448 * above. */
450 cmp.minimal_surplus[o] = MIN(cmp.minimal_surplus[o],
451 MIN(pcity->surplus[o], 0));
453 cmp.require_happy = FALSE;
454 cmp.allow_disorder = is_ai(city_owner(pcity)) ? FALSE : TRUE;
455 cm_query_result(pcity, pcmp, cmr, FALSE);
456 }
457 if (!cmr->found_a_valid) {
458 CITY_LOG(LOG_DEBUG, pcity, "emergency management");
459 pcmp = &cmp;
461 cm_query_result(pcity, pcmp, cmr, TRUE);
462 }
464
465 apply_cmresult_to_city(pcity, cmr);
466
467 if (pcity->server.debug) {
468 /* Print debug output if requested. */
469 cm_print_city(pcity);
470 cm_print_result(cmr);
471 }
472
473 if (city_refresh(pcity)) {
474 log_error("%s radius changed when already arranged workers.",
475 city_name_get(pcity));
476 /* Can't do anything - don't want to enter infinite recursive loop
477 * by trying to arrange workers more. */
478 }
479 sanity_check_city(pcity);
480
481 if (broadcast_needed) {
482 broadcast_city_info(pcity);
483 }
484
487}
488
489/**********************************************************************/
492static void city_global_turn_notify(struct conn_list *dest)
493{
494 cities_iterate(pcity) {
495 const struct impr_type *pimprove = pcity->production.value.building;
496
497 if (VUT_IMPROVEMENT == pcity->production.kind
498 && is_great_wonder(pimprove)
499 && (1 >= city_production_turns_to_build(pcity, TRUE)
500 && can_city_build_improvement_now(pcity, pimprove))) {
501 notify_conn(dest, city_tile(pcity),
502 E_WONDER_WILL_BE_BUILT, ftc_server,
503 _("Notice: Wonder %s in %s will be finished next turn."),
504 improvement_name_translation(pimprove), city_link(pcity));
505 }
507}
508
509/**********************************************************************/
513static void city_turn_notify(const struct city *pcity,
514 struct conn_list *dest,
515 const struct player *cache_for_player)
516{
517 const struct impr_type *pimprove = pcity->production.value.building;
518 struct packet_chat_msg packet;
519 int turns_growth, turns_granary;
520
521 if (0 < pcity->surplus[O_FOOD]) {
522 turns_growth = (city_granary_size(city_size_get(pcity))
523 - pcity->food_stock - 1) / pcity->surplus[O_FOOD];
524
525 if (get_city_bonus(pcity, EFT_GROWTH_FOOD) <= 0
526 && 0 < get_current_construction_bonus(pcity, EFT_GROWTH_FOOD,
528 && 0 < pcity->surplus[O_SHIELD]) {
529 /* From the check above, the surplus must always be positive. */
530 turns_granary = (impr_build_shield_cost(pcity, pimprove)
531 - pcity->shield_stock) / pcity->surplus[O_SHIELD];
532 /* If growth and granary completion occur simultaneously, granary
533 * preserves food. -AJS. */
534 if (5 > turns_growth && 5 > turns_granary
535 && turns_growth < turns_granary) {
536 package_event(&packet, city_tile(pcity),
537 E_CITY_GRAN_THROTTLE, ftc_server,
538 _("Suggest throttling growth in %s to use %s "
539 "(being built) more effectively."),
540 city_link(pcity),
542 lsend_packet_chat_msg(dest, &packet);
543 if (NULL != cache_for_player) {
544 event_cache_add_for_player(&packet, cache_for_player);
545 }
546 }
547 }
548
549 if (0 >= turns_growth && !city_celebrating(pcity)
550 && city_can_grow_to(pcity, city_size_get(pcity) + 1)) {
551 package_event(&packet, city_tile(pcity),
552 E_CITY_MAY_SOON_GROW, ftc_server,
553 _("%s may soon grow to size %i."),
554 city_link(pcity), city_size_get(pcity) + 1);
555 lsend_packet_chat_msg(dest, &packet);
556 if (NULL != cache_for_player) {
557 event_cache_add_for_player(&packet, cache_for_player);
558 }
559 }
560 } else {
561 if (0 >= pcity->food_stock + pcity->surplus[O_FOOD]
562 && 0 > pcity->surplus[O_FOOD]) {
563 package_event(&packet, city_tile(pcity),
564 E_CITY_FAMINE_FEARED, ftc_server,
565 _("Warning: Famine feared in %s."), city_link(pcity));
566 lsend_packet_chat_msg(dest, &packet);
567 if (NULL != cache_for_player) {
568 event_cache_add_for_player(&packet, cache_for_player);
569 }
570 }
571 }
572}
573
574/**********************************************************************/
579{
580 if (NULL != pconn) {
581 struct player *pplayer = conn_get_player(pconn);
582
583 if (NULL != pplayer) {
584 city_list_iterate(pplayer->cities, pcity) {
585 city_turn_notify(pcity, pconn->self, NULL);
587 }
589 } else {
590 players_iterate(pplayer) {
591 city_list_iterate(pplayer->cities, pcity) {
592 city_turn_notify(pcity, pplayer->connections, pplayer);
595 /* NB: notifications to 'game.est_connections' are automatically
596 * cached. */
598 }
599}
600
601/**********************************************************************/
604void update_city_activities(struct player *pplayer)
605{
606 char buf[4 * MAX_LEN_NAME];
607 int n, gold;
608
609 fc_assert(NULL != pplayer->cities);
610
611 n = city_list_size(pplayer->cities);
612 gold = pplayer->economic.gold;
613 pplayer->server.bulbs_last_turn = 0;
614
615 if (n > 0) {
616 struct city *cities[n];
617 int i = 0, r;
618
619 city_list_iterate(pplayer->cities, pcity) {
620
621 citizens_convert(pcity);
622
623 /* Cancel trade routes that cannot exist any more */
624 trade_routes_iterate_safe(pcity, proute) {
625 struct city *tcity = game_city_by_number(proute->partner);
626
627 if (tcity != NULL) {
628 bool cancel = FALSE;
629
630 if (proute->dir != RDIR_FROM && goods_has_flag(proute->goods, GF_DEPLETES)
631 && !goods_can_be_provided(tcity, proute->goods, NULL)) {
632 cancel = TRUE;
633 }
634 if (!cancel && !can_cities_trade(pcity, tcity)) {
635 enum trade_route_type type = cities_trade_route_type(pcity, tcity);
637
638 if (settings->cancelling == TRI_CANCEL) {
639 cancel = TRUE;
640 }
641 }
642
643 if (cancel) {
644 struct trade_route *back;
645
646 back = remove_trade_route(pcity, proute, TRUE, FALSE);
647 free(proute);
648 free(back);
649 }
650 }
652
653 /* Add cities to array for later random order handling */
654 cities[i++] = pcity;
656
657 /* How gold upkeep is handled depends on the setting
658 * 'game.info.gold_upkeep_style':
659 * GOLD_UPKEEP_CITY: Each city tries to balance its upkeep individually
660 * (this is done in update_city_activity()).
661 * GOLD_UPKEEP_MIXED: Each city tries to balance its upkeep for
662 * buildings individually; the upkeep for units is
663 * paid by the nation.
664 * GOLD_UPKEEP_NATION: The nation as a whole balances the treasury. If
665 * the treasury is not balance units and buildings
666 * are sold. */
667
668 /* Iterate over cities in a random order. */
669 while (i > 0) {
670 r = fc_rand(i);
671 /* update unit upkeep */
674 cities[r] = cities[--i];
675 }
676
677 if (pplayer->economic.infra_points < 0) {
678 pplayer->economic.infra_points = 0;
679 }
680
681 if (pplayer->economic.gold < 0) {
682 switch (game.info.gold_upkeep_style) {
683 case GOLD_UPKEEP_CITY:
684 break;
685 case GOLD_UPKEEP_MIXED:
686 /* Nation pays for units. */
688 break;
689 case GOLD_UPKEEP_NATION:
690 /* Nation pays for units and buildings. */
692 break;
693 }
694 }
695
696 /* Should not happen. */
697 fc_assert(pplayer->economic.gold >= 0);
698 }
699
700 /* This test includes the cost of the units because
701 * units are paid for in update_city_activity() or
702 * player_balance_treasury_units(). */
703 if (gold - (gold - pplayer->economic.gold) * 3 < 0) {
704 notify_player(pplayer, NULL, E_LOW_ON_FUNDS, ftc_server,
705 _("WARNING, we're LOW on FUNDS %s."),
706 ruler_title_for_player(pplayer, buf, sizeof(buf)));
707 }
708
709#if 0
710 /* Uncomment to unbalance the game, like in civ1 (CLG). */
711 if (pplayer->got_tech && pplayer->research->researched > 0) {
712 pplayer->research->researched = 0;
713 }
714#endif
715
717}
718
719/**********************************************************************/
731static bool upkeep_kill_unit(struct unit *punit, Output_type_id outp,
732 enum unit_loss_reason wipe_reason,
733 bool wipe_in_the_end)
734{
735 int punit_id;
736
737 if (!action_auto_perf_unit_sel(AAPC_UNIT_UPKEEP, punit,
738 NULL, get_output_type(outp), NULL)) {
739 /* Can't get rid of this unit. It is undisbandable for the current
740 * situation. */
741 return FALSE;
742 }
743
744 punit_id = punit->id;
745
746 /* Try to perform this unit's can't upkeep actions. */
747 action_auto_perf_unit_do(AAPC_UNIT_UPKEEP, punit,
748 NULL, get_output_type(outp), NULL,
749 NULL, NULL, NULL, NULL);
750
751 if (wipe_in_the_end && unit_is_alive(punit_id)) {
752 /* No forced action was able to kill the unit. Finish the job. */
753 wipe_unit(punit, wipe_reason, NULL);
754 }
755
756 return !unit_is_alive(punit_id);
757}
758
759/**********************************************************************/
763static citizens city_reduce_specialists(struct city *pcity, citizens change)
764{
765 citizens want = change;
766
767 fc_assert_ret_val(0 < change, 0);
768
770 citizens fix = MIN(want, pcity->specialists[sp]);
771
772 pcity->specialists[sp] -= fix;
773 want -= fix;
775
776 return change - want;
777}
778
779/**********************************************************************/
783static citizens city_reduce_workers(struct city *pcity, citizens change)
784{
785 struct tile *pcenter = city_tile(pcity);
786 int want = change;
787 const struct civ_map *nmap = &(wld.map);
788
789 fc_assert_ret_val(0 < change, 0);
790
792 ptile, _index, _x, _y) {
793 if (0 < want && tile_worked(ptile) == pcity) {
794 city_map_update_empty(pcity, ptile);
795 want--;
796 }
798
799 return change - want;
800}
801
802/**********************************************************************/
807bool city_reduce_size(struct city *pcity, citizens pop_loss,
808 struct player *destroyer, const char *reason)
809{
810 citizens loss_remain;
811 int old_radius_sq;
812
813 if (pop_loss == 0) {
814 return TRUE;
815 }
816
817 if (city_size_get(pcity) <= pop_loss) {
818 int id = pcity->id;
819
820 citizens_update(pcity, NULL); /* To avoid warnings during the script */
821 /* Won't refresh a doomed city, or should we? */
822 script_server_signal_emit("city_destroyed", pcity, pcity->owner,
823 destroyer);
824
825 if (city_exist(id)) {
826 remove_city(pcity);
827 }
828 return FALSE;
829 }
830 old_radius_sq = tile_border_source_radius_sq(pcity->tile);
831 city_size_add(pcity, -pop_loss);
832 map_update_border(pcity->tile, pcity->owner, old_radius_sq,
834
835 /* Cap the food stock at the new granary size. */
836 if (pcity->food_stock > city_granary_size(city_size_get(pcity))) {
838 }
839
840 /* First try to kill off the specialists */
841 loss_remain = pop_loss - city_reduce_specialists(pcity, pop_loss);
842
843 if (loss_remain > 0) {
844 /* Take it out on workers */
845#ifndef FREECIV_NDEBUG
846 loss_remain -=
847#endif /* FREECIV_NDEBUG */
848 city_reduce_workers(pcity, loss_remain);
849 }
850
851 /* Update citizens. */
852 citizens_update(pcity, NULL);
853
854 /* Update number of people in each feelings category.
855 * This also updates the city radius if needed. */
856 city_refresh(pcity);
857
859
860 /* Send city data. */
861 sync_cities();
862
863 fc_assert_ret_val_msg(0 == loss_remain, TRUE,
864 "city_reduce_size() has remaining"
865 "%d of %d for \"%s\"[%d]",
866 loss_remain, pop_loss,
867 city_name_get(pcity), city_size_get(pcity));
868
869 /* Update cities that have trade routes with us */
870 trade_partners_iterate(pcity, pcity2) {
871 if (city_refresh(pcity2)) {
872 /* This should never happen, but if it does, make sure not to
873 * leave workers outside city radius. */
874 auto_arrange_workers(pcity2);
875 }
877
878 sanity_check_city(pcity);
879
880 if (reason != NULL) {
881 int id = pcity->id;
882
883 script_server_signal_emit("city_size_change", pcity,
884 (lua_Integer)(-pop_loss), reason);
885
886 return city_exist(id);
887 }
888
889 return TRUE;
890}
891
892/**********************************************************************/
896void city_repair_size(struct city *pcity, int change)
897{
898 if (change > 0) {
899 pcity->specialists[DEFAULT_SPECIALIST] += change;
900 } else if (change < 0) {
901 int need = change + city_reduce_specialists(pcity, -change);
902
903 if (0 > need) {
904#ifndef FREECIV_NDEBUG
905 need +=
906#endif /* FREECIV_NDEBUG */
907 city_reduce_workers(pcity, -need);
908 }
909
910 fc_assert_msg(0 == need,
911 "city_repair_size() has remaining %d of %d for \"%s\"[%d]",
912 need, change, city_name_get(pcity), city_size_get(pcity));
913 }
914}
915
916/**********************************************************************/
923int city_granary_savings(const struct city *pcity)
924{
925 int savings = get_city_bonus(pcity, EFT_GROWTH_FOOD);
926
927 return CLIP(0, savings, 100);
928}
929
930/**********************************************************************/
936static void city_reset_foodbox(struct city *pcity, int new_size)
937{
938 fc_assert_ret(pcity != NULL);
939
940 pcity->food_stock = (city_granary_size(new_size)
941 * city_granary_savings(pcity)) / 100;
942}
943
944/**********************************************************************/
949static bool city_increase_size(struct city *pcity)
950{
951 int new_food;
952 int savings_pct = city_granary_savings(pcity);
953 bool have_square = FALSE;
954 bool rapture_grow = city_rapture_grow(pcity); /* Check before size increase! */
955 struct tile *pcenter = city_tile(pcity);
956 struct player *powner = city_owner(pcity);
957 const struct impr_type *pimprove = pcity->production.value.building;
958 const struct civ_map *nmap = &(wld.map);
959
960 if (!city_can_grow_to(pcity, city_size_get(pcity) + 1)) {
961 /* Need improvement */
962 if (get_current_construction_bonus(pcity, EFT_SIZE_ADJ, RPT_CERTAIN) > 0
963 || get_current_construction_bonus(pcity, EFT_SIZE_UNLIMIT, RPT_CERTAIN) > 0) {
964 notify_player(powner, city_tile(pcity), E_CITY_AQ_BUILDING, ftc_server,
965 _("%s needs %s (being built) to grow beyond size %d."),
966 city_link(pcity),
968 city_size_get(pcity));
969 } else {
970 notify_player(powner, city_tile(pcity), E_CITY_AQUEDUCT, ftc_server,
971 _("%s needs an improvement to grow beyond size %d."),
972 city_link(pcity), city_size_get(pcity));
973 }
974 /* Granary can only hold so much */
975 new_food = (city_granary_size(city_size_get(pcity))
976 * (100 * 100 - game.server.aqueductloss * (100 - savings_pct))
977 / (100 * 100));
978 pcity->food_stock = MIN(pcity->food_stock, new_food);
979
980 return FALSE;
981 }
982
983 city_size_add(pcity, 1);
984
985 /* Do not empty food stock if city is growing by celebrating */
986 if (rapture_grow) {
987 new_food = city_granary_size(city_size_get(pcity));
988 } else {
989 new_food = city_granary_size(city_size_get(pcity)) * savings_pct / 100;
990 }
991 pcity->food_stock = MIN(pcity->food_stock, new_food);
992
993 /* If there is enough food, and the city is big enough,
994 * make new citizens into scientists or taxmen -- Massimo */
995
996 /* Ignore food if no square can be worked */
998 ptile, _index, _x, _y) {
999 if (tile_worked(ptile) != pcity /* Quick test */
1000 && city_can_work_tile(pcity, ptile)) {
1001 have_square = TRUE;
1002 }
1004
1005 if ((pcity->surplus[O_FOOD] >= 2 || !have_square)
1006 && is_city_option_set(pcity, CITYO_SCIENCE_SPECIALISTS)) {
1007 pcity->specialists[best_specialist(O_SCIENCE, pcity)]++;
1008 } else if ((pcity->surplus[O_FOOD] >= 2 || !have_square)
1009 && is_city_option_set(pcity, CITYO_GOLD_SPECIALISTS)) {
1010 pcity->specialists[best_specialist(O_GOLD, pcity)]++;
1011 } else {
1012 pcity->specialists[DEFAULT_SPECIALIST]++; /* or else city is !sane */
1013 }
1014
1015 /* Deprecated signal. Connect your lua functions to "city_size_change" that's
1016 * emitted from calling functions which know the 'reason' of the increase. */
1017 script_server_signal_emit("city_growth", pcity,
1018 (lua_Integer)city_size_get(pcity));
1019
1020 return TRUE;
1021}
1022
1023/**********************************************************************/
1027 struct player *nationality)
1028{
1029 struct player *powner = city_owner(pcity);
1030
1031 /* Update citizens. */
1032 citizens_update(pcity, nationality);
1033
1034 /* Refresh the city data; this also checks the squared city radius. */
1035 city_refresh(pcity);
1036
1037 auto_arrange_workers(pcity);
1038
1039 /* Update cities that have trade routes with us */
1040 trade_partners_iterate(pcity, pcity2) {
1041 if (city_refresh(pcity2)) {
1042 /* This should never happen, but if it does, make sure not to
1043 * leave workers outside city radius. */
1044 auto_arrange_workers(pcity2);
1045 }
1047
1048 notify_player(powner, city_tile(pcity), E_CITY_GROWTH, ftc_server,
1049 _("%s grows to size %d."),
1050 city_link(pcity), city_size_get(pcity));
1051
1052 sanity_check_city(pcity);
1053
1054 sync_cities();
1055}
1056
1057/**********************************************************************/
1061 struct player *nationality, const char *reason)
1062{
1063 int change = size - city_size_get(pcity);
1064
1065 if (change > 0) {
1066 int old_size = city_size_get(pcity);
1067 int real_change;
1068 int current_size = city_size_get(pcity);
1069 int id = pcity->id;
1070
1071 /* Increase city size until size reached, or increase fails */
1072 while (size > current_size && city_increase_size(pcity)) {
1073 /* TODO: This is currently needed only because there's
1074 * deprecated script signal "city_growth" emitted.
1075 * Check the need after signal has been dropped completely. */
1076 if (!city_exist(id)) {
1077 return FALSE;
1078 }
1079
1080 current_size++;
1081 }
1082
1083 city_refresh_after_city_size_increase(pcity, nationality);
1084
1085 real_change = current_size - old_size;
1086
1087 if (real_change != 0 && reason != NULL) {
1088 script_server_signal_emit("city_size_change", pcity,
1089 (lua_Integer)real_change, reason);
1090
1091 if (!city_exist(id)) {
1092 return FALSE;
1093 }
1094 }
1095 } else if (change < 0) {
1096 /* We assume that city_change_size() is never called because
1097 * of enemy actions. If that changes, enemy must be passed
1098 * to city_reduce_size() */
1099 return city_reduce_size(pcity, -change, NULL, reason);
1100 }
1101
1102 map_claim_border(pcity->tile, pcity->owner, -1);
1103
1104 return TRUE;
1105}
1106
1107/**********************************************************************/
1111static void city_populate(struct city *pcity, struct player *nationality)
1112{
1113 int saved_id = pcity->id;
1114 int granary_size = city_granary_size(city_size_get(pcity));
1115
1116 pcity->food_stock += pcity->surplus[O_FOOD];
1117 if (pcity->food_stock >= granary_size || city_rapture_grow(pcity)) {
1118 if (city_had_recent_plague(pcity)) {
1119 notify_player(city_owner(pcity), city_tile(pcity),
1120 E_CITY_PLAGUE, ftc_server,
1121 _("A recent plague outbreak prevents growth in %s."),
1122 city_link(pcity));
1123 /* Lose excess food */
1124 pcity->food_stock = MIN(pcity->food_stock, granary_size);
1125 } else {
1126 bool success;
1127
1128 success = city_increase_size(pcity);
1129 map_claim_border(pcity->tile, pcity->owner, -1);
1130
1131 if (success) {
1132 city_refresh_after_city_size_increase(pcity, nationality);
1133 script_server_signal_emit("city_size_change", pcity,
1134 (lua_Integer)1, "growth");
1135 }
1136 }
1137 } else if (pcity->food_stock < 0) {
1138 /* FIXME: should this depend on units with ability to build
1139 * cities or on units that require food in upkeep?
1140 * I'll assume citybuilders (units that 'contain' 1 pop) -- sjolie
1141 * The above may make more logical sense, but in game terms
1142 * you want to disband a unit that is draining your food
1143 * reserves. Hence, I'll assume food upkeep > 0 units. -- jjm
1144 */
1146 if (punit->upkeep[O_FOOD] > 0) {
1147 const char *punit_link = unit_tile_link(punit);
1148
1149 if (upkeep_kill_unit(punit, O_FOOD, ULR_STARVED,
1151 notify_player(city_owner(pcity), city_tile(pcity),
1152 E_UNIT_LOST_MISC, ftc_server,
1153 _("Famine feared in %s, %s lost!"),
1154 city_link(pcity), punit_link);
1155 }
1156
1157 if (city_exist(saved_id)) {
1158 city_reset_foodbox(pcity, city_size_get(pcity));
1159 }
1160
1161 return;
1162 }
1164 if (city_size_get(pcity) > 1) {
1165 notify_player(city_owner(pcity), city_tile(pcity),
1166 E_CITY_FAMINE, ftc_server,
1167 _("Famine causes population loss in %s."),
1168 city_link(pcity));
1169 } else {
1170 notify_player(city_owner(pcity), city_tile(pcity),
1171 E_CITY_FAMINE, ftc_server,
1172 _("Famine destroys %s entirely."),
1173 city_link(pcity));
1174 }
1175 city_reset_foodbox(pcity, city_size_get(pcity) - 1);
1176 city_reduce_size(pcity, 1, NULL, "famine");
1177 }
1178}
1179
1180/**********************************************************************/
1187 struct city *pcity,
1188 struct player *pplayer,
1189 int saved_id)
1190{
1191 const void *ptarget;
1192 const char *tgt_name;
1193 const struct requirement_vector *build_reqs;
1194 const char *signal_name;
1195 const struct req_context city_ctxt = {
1196 .player = pplayer,
1197 .city = pcity,
1198 .tile = city_tile(pcity)
1199 /* FIXME: Setting .unittype is currently redundant
1200 * but can_city_build_unit_direct() does it */
1201 };
1202 bool purge = FALSE;
1203 bool known = FALSE;
1204
1205 switch (target->kind) {
1206 case VUT_UTYPE:
1207 ptarget = target->value.utype;
1208 build_reqs = &target->value.utype->build_reqs;
1209 tgt_name = utype_name_translation(ptarget);
1210 signal_name = "unit_cant_be_built";
1211 break;
1212 case VUT_IMPROVEMENT:
1213 ptarget = target->value.building;
1214 build_reqs = &target->value.building->reqs;
1215 tgt_name = city_improvement_name_translation(pcity, ptarget);
1216 signal_name = "building_cant_be_built";
1217 break;
1218 default:
1219 fc_assert_ret_val((target->kind == VUT_IMPROVEMENT
1220 || target->kind == VUT_UTYPE), FALSE);
1221 return FALSE;
1222 break;
1223 }
1224
1225 requirement_vector_iterate(build_reqs, preq) {
1226 if (!is_req_active(&city_ctxt, NULL, preq, RPT_POSSIBLE)) {
1227 known = TRUE;
1228 switch (preq->source.kind) {
1229 case VUT_ADVANCE:
1230 if (preq->present) {
1231 notify_player(pplayer, city_tile(pcity),
1232 E_CITY_CANTBUILD, ftc_server,
1233 _("%s can't build %s from the worklist; "
1234 "tech %s not yet available. Postponing..."),
1235 city_link(pcity),
1236 tgt_name,
1238 (preq->source.value.advance));
1239 script_server_signal_emit(signal_name, ptarget,
1240 pcity, "need_tech");
1241 } else {
1242 /* While techs can be unlearned, this isn't useful feedback */
1243 purge = TRUE;
1244 }
1245 break;
1246 case VUT_TECHFLAG:
1247 if (preq->present) {
1248 notify_player(pplayer, city_tile(pcity),
1249 E_CITY_CANTBUILD, ftc_server,
1250 _("%s can't build %s from the worklist; "
1251 "no tech with flag \"%s\" yet available. "
1252 "Postponing..."),
1253 city_link(pcity),
1254 tgt_name,
1255 tech_flag_id_name(preq->source.value.techflag));
1256 script_server_signal_emit(signal_name, ptarget,
1257 pcity, "need_techflag");
1258 } else {
1259 /* While techs can be unlearned, this isn't useful feedback */
1260 purge = TRUE;
1261 }
1262 break;
1263 case VUT_IMPROVEMENT:
1264 if (preq->range == REQ_RANGE_LOCAL) {
1265 /* Building itself is never going to change */
1266 purge = TRUE;
1267 } else {
1268 if (preq->present) {
1269 notify_player(pplayer, city_tile(pcity),
1270 E_CITY_CANTBUILD, ftc_server,
1271 _("%s can't build %s from the worklist; "
1272 "need to have %s first. Postponing..."),
1273 city_link(pcity),
1274 tgt_name,
1276 preq->source.value.building));
1277 script_server_signal_emit(signal_name, ptarget,
1278 pcity, "need_building");
1279 } else {
1280 notify_player(pplayer, city_tile(pcity),
1281 E_CITY_CANTBUILD, ftc_server,
1282 _("%s can't build %s from the worklist; "
1283 "need to not have %s. Postponing..."),
1284 city_link(pcity),
1285 tgt_name,
1287 preq->source.value.building));
1288 script_server_signal_emit(signal_name, ptarget,
1289 pcity, "have_building");
1290 }
1291 }
1292 break;
1293 case VUT_IMPR_GENUS:
1294 if (preq->range == REQ_RANGE_LOCAL) {
1295 /* Building's own genus is never going to change */
1296 purge = TRUE;
1297 } else {
1298 if (preq->present) {
1299 notify_player(pplayer, city_tile(pcity),
1300 E_CITY_CANTBUILD, ftc_server,
1301 _("%s can't build %s from the worklist; "
1302 "need to have %s first. Postponing..."),
1303 city_link(pcity),
1304 tgt_name,
1305 impr_genus_id_translated_name(
1306 preq->source.value.impr_genus));
1307 script_server_signal_emit(signal_name, ptarget,
1308 pcity, "need_building_genus");
1309 } else {
1310 notify_player(pplayer, city_tile(pcity),
1311 E_CITY_CANTBUILD, ftc_server,
1312 _("%s can't build %s from the worklist; "
1313 "need to not have %s. Postponing..."),
1314 city_link(pcity),
1315 tgt_name,
1316 impr_genus_id_translated_name(
1317 preq->source.value.impr_genus));
1318 script_server_signal_emit(signal_name, ptarget,
1319 pcity, "have_building_genus");
1320 }
1321 }
1322 break;
1323 case VUT_GOVERNMENT:
1324 if (preq->present) {
1325 notify_player(pplayer, city_tile(pcity),
1326 E_CITY_CANTBUILD, ftc_server,
1327 _("%s can't build %s from the worklist; "
1328 "it needs %s government. Postponing..."),
1329 city_link(pcity),
1330 tgt_name,
1331 government_name_translation(preq->source.value.govern));
1332 script_server_signal_emit(signal_name, ptarget,
1333 pcity, "need_government");
1334 } else {
1335 notify_player(pplayer, city_tile(pcity),
1336 E_CITY_CANTBUILD, ftc_server,
1337 _("%s can't build %s from the worklist; "
1338 "it cannot have %s government. Postponing..."),
1339 city_link(pcity),
1340 tgt_name,
1341 government_name_translation(preq->source.value.govern));
1342 script_server_signal_emit(signal_name, ptarget,
1343 pcity, "have_government");
1344 }
1345 break;
1346 case VUT_ACHIEVEMENT:
1347 if (preq->present) {
1348 notify_player(pplayer, city_tile(pcity),
1349 E_CITY_CANTBUILD, ftc_server,
1350 _("%s can't build %s from the worklist; "
1351 "it needs \"%s\" achievement. Postponing..."),
1352 city_link(pcity),
1353 tgt_name,
1354 achievement_name_translation(preq->source.value.achievement));
1355 script_server_signal_emit(signal_name, ptarget,
1356 pcity, "need_achievement");
1357 } else {
1358 /* Can't unachieve things. */
1359 purge = TRUE;
1360 }
1361 break;
1362 case VUT_EXTRA:
1363 if (preq->present) {
1364 notify_player(pplayer, city_tile(pcity),
1365 E_CITY_CANTBUILD, ftc_server,
1366 Q_("?extra:%s can't build %s from the worklist; "
1367 "%s is required. Postponing..."),
1368 city_link(pcity),
1369 tgt_name,
1370 extra_name_translation(preq->source.value.extra));
1371 script_server_signal_emit(signal_name, ptarget,
1372 pcity, "need_extra");
1373 } else {
1374 notify_player(pplayer, city_tile(pcity),
1375 E_CITY_CANTBUILD, ftc_server,
1376 Q_("?extra:%s can't build %s from the worklist; "
1377 "%s is prohibited. Postponing..."),
1378 city_link(pcity),
1379 tgt_name,
1380 extra_name_translation(preq->source.value.extra));
1381 script_server_signal_emit(signal_name, ptarget,
1382 pcity, "have_extra");
1383 }
1384 break;
1385 case VUT_GOOD:
1386 if (preq->present) {
1387 notify_player(pplayer, city_tile(pcity),
1388 E_CITY_CANTBUILD, ftc_server,
1389 Q_("?extra:%s can't build %s from the worklist; "
1390 "%s is required. Postponing..."),
1391 city_link(pcity),
1392 tgt_name,
1393 goods_name_translation(preq->source.value.good));
1394 script_server_signal_emit(signal_name, ptarget,
1395 pcity, "need_good");
1396 } else {
1397 notify_player(pplayer, city_tile(pcity),
1398 E_CITY_CANTBUILD, ftc_server,
1399 Q_("?extra:%s can't build %s from the worklist; "
1400 "%s is prohibited. Postponing..."),
1401 city_link(pcity),
1402 tgt_name,
1403 goods_name_translation(preq->source.value.good));
1404 script_server_signal_emit(signal_name, ptarget,
1405 pcity, "have_good");
1406 }
1407 break;
1408 case VUT_TERRAIN:
1409 if (preq->present) {
1410 notify_player(pplayer, city_tile(pcity),
1411 E_CITY_CANTBUILD, ftc_server,
1412 Q_("?terrain:%s can't build %s from the worklist; "
1413 "%s terrain is required. Postponing..."),
1414 city_link(pcity),
1415 tgt_name,
1416 terrain_name_translation(preq->source.value.terrain));
1417 script_server_signal_emit(signal_name, ptarget,
1418 pcity, "need_terrain");
1419 } else {
1420 notify_player(pplayer, city_tile(pcity),
1421 E_CITY_CANTBUILD, ftc_server,
1422 Q_("?terrain:%s can't build %s from the worklist; "
1423 "%s terrain is prohibited. Postponing..."),
1424 city_link(pcity),
1425 tgt_name,
1426 terrain_name_translation(preq->source.value.terrain));
1427 script_server_signal_emit(signal_name, ptarget,
1428 pcity, "have_terrain");
1429 }
1430 break;
1431 case VUT_NATION:
1432 if (preq->range < REQ_RANGE_TRADE_ROUTE
1433 || preq->range == REQ_RANGE_PLAYER) {
1434 /* At higher ranges, new players with their nations may arrive */
1435 purge = TRUE;
1436 } else {
1437 if (preq->present) {
1438 notify_player(pplayer, city_tile(pcity),
1439 E_CITY_CANTBUILD, ftc_server,
1440 /* TRANS: "%s nation" is adjective */
1441 Q_("?nation:%s can't build %s from the worklist; "
1442 "%s nation is required. Postponing..."),
1443 city_link(pcity),
1444 tgt_name,
1445 nation_adjective_translation(preq->source.value.nation));
1446 script_server_signal_emit(signal_name, ptarget,
1447 pcity, "need_nation");
1448 } else {
1449 notify_player(pplayer, city_tile(pcity),
1450 E_CITY_CANTBUILD, ftc_server,
1451 Q_("?nation:%s can't build %s from the worklist; "
1452 "%s nation is prohibited. Postponing..."),
1453 city_link(pcity),
1454 tgt_name,
1455 nation_adjective_translation(preq->source.value.nation));
1456 script_server_signal_emit(signal_name, ptarget,
1457 pcity, "have_nation");
1458 }
1459 }
1460 break;
1461 case VUT_NATIONGROUP:
1462 if (preq->range < REQ_RANGE_TRADE_ROUTE
1463 || preq->range == REQ_RANGE_PLAYER) {
1464 /* At higher ranges, new players with their nations may arrive */
1465 purge = TRUE;
1466 } else {
1467 if (preq->present) {
1468 notify_player(pplayer, city_tile(pcity),
1469 E_CITY_CANTBUILD, ftc_server,
1470 /* TRANS: "%s nation" is adjective */
1471 Q_("?ngroup:%s can't build %s from the worklist; "
1472 "%s nation is required. Postponing..."),
1473 city_link(pcity),
1474 tgt_name,
1475 nation_group_name_translation(preq->source.value.nationgroup));
1476 script_server_signal_emit(signal_name, ptarget,
1477 pcity, "need_nationgroup");
1478 } else {
1479 notify_player(pplayer, city_tile(pcity),
1480 E_CITY_CANTBUILD, ftc_server,
1481 Q_("?ngroup:%s can't build %s from the worklist; "
1482 "%s nation is prohibited. Postponing..."),
1483 city_link(pcity),
1484 tgt_name,
1485 nation_group_name_translation(preq->source.value.nationgroup));
1486 script_server_signal_emit(signal_name, ptarget,
1487 pcity, "have_nationgroup");
1488 }
1489 }
1490 break;
1491 case VUT_STYLE:
1492 /* FIXME: City styles sometimes change over time, but it isn't
1493 * entirely under player control. Probably better to purge
1494 * with useful explanation. */
1495 if (preq->present) {
1496 notify_player(pplayer, city_tile(pcity),
1497 E_CITY_CANTBUILD, ftc_server,
1498 _("%s can't build %s from the worklist; "
1499 "only %s style cities may build this. Postponing..."),
1500 city_link(pcity),
1501 tgt_name,
1502 style_name_translation(preq->source.value.style));
1503 script_server_signal_emit(signal_name, ptarget,
1504 pcity, "need_style");
1505 } else {
1506 notify_player(pplayer, city_tile(pcity),
1507 E_CITY_CANTBUILD, ftc_server,
1508 _("%s can't build %s from the worklist; "
1509 "%s style cities may not build this. Postponing..."),
1510 city_link(pcity),
1511 tgt_name,
1512 style_name_translation(preq->source.value.style));
1513 script_server_signal_emit(signal_name, ptarget,
1514 pcity, "have_style");
1515 }
1516 break;
1517 case VUT_NATIONALITY:
1518 /* FIXME: Changing citizen nationality is hard: purging might be
1519 * more useful in this case. */
1520 if (preq->present) {
1521 notify_player(pplayer, city_tile(pcity),
1522 E_CITY_CANTBUILD, ftc_server,
1523 /* TRANS: Latter %s is citizen nationality */
1524 _("%s can't build %s from the worklist; "
1525 "only city with %s may build this. Postponing..."),
1526 city_link(pcity),
1527 tgt_name,
1528 nation_plural_translation(preq->source.value.nationality));
1529 script_server_signal_emit(signal_name, ptarget,
1530 pcity, "need_nationality");
1531 } else {
1532 notify_player(pplayer, city_tile(pcity),
1533 E_CITY_CANTBUILD, ftc_server,
1534 /* TRANS: Latter %s is citizen nationality */
1535 _("%s can't build %s from the worklist; "
1536 "only city without %s may build this. Postponing..."),
1537 city_link(pcity),
1538 tgt_name,
1539 nation_plural_translation(preq->source.value.nationality));
1540 script_server_signal_emit(signal_name, ptarget,
1541 pcity, "have_nationality");
1542 }
1543 break;
1544 case VUT_DIPLREL:
1545 case VUT_DIPLREL_TILE: /* The tile owner is the city owner */
1546 case VUT_DIPLREL_TILE_O: /* The tile owner is the city owner */
1547 if (preq->present) {
1548 const char *reason;
1549
1550 notify_player(pplayer, city_tile(pcity),
1551 E_CITY_CANTBUILD, ftc_server,
1552 /* TRANS: '%s' is a wide range of relationships;
1553 * e.g., 'Peace', 'Never met', 'Foreign',
1554 * 'Hosts embassy', 'Provided Casus Belli' */
1555 _("%s can't build %s from the worklist; "
1556 "the relationship '%s' is required."
1557 " Postponing..."),
1558 city_link(pcity),
1559 tgt_name,
1561 preq->source.value.diplrel));
1562
1563 if (preq->source.kind == VUT_DIPLREL_TILE) {
1564 reason = "need_diplrel_tile";
1565 } else if (preq->source.kind == VUT_DIPLREL_TILE_O) {
1566 reason = "need_diplrel_tile_o";
1567 } else {
1568 fc_assert(preq->source.kind == VUT_DIPLREL);
1569 reason = "need_diplrel";
1570 }
1571
1572 script_server_signal_emit(signal_name, ptarget,
1573 pcity, reason);
1574 } else {
1575 const char *reason;
1576
1577 notify_player(pplayer, city_tile(pcity),
1578 E_CITY_CANTBUILD, ftc_server,
1579 _("%s can't build %s from the worklist; "
1580 "the relationship '%s' is prohibited."
1581 " Postponing..."),
1582 city_link(pcity),
1583 tgt_name,
1585 preq->source.value.diplrel));
1586
1587 if (preq->source.kind == VUT_DIPLREL_TILE) {
1588 reason = "have_diplrel_tile";
1589 } else if (preq->source.kind == VUT_DIPLREL_TILE_O) {
1590 reason = "have_diplrel_tile_o";
1591 } else {
1592 fc_assert(preq->source.kind == VUT_DIPLREL);
1593 reason = "have_diplrel";
1594 }
1595
1596 script_server_signal_emit(signal_name, ptarget,
1597 pcity, reason);
1598 }
1599 break;
1600 case VUT_DIPLREL_UNITANY:
1601 case VUT_DIPLREL_UNITANY_O:
1602 if (preq->present) {
1603 const char *reason;
1604
1605 notify_player(pplayer, city_tile(pcity),
1606 E_CITY_CANTBUILD, ftc_server,
1607 /* TRANS: '%s' is a wide range of relationships;
1608 * e.g., 'Peace', 'Never met', 'Foreign',
1609 * 'Hosts embassy', 'Provided Casus Belli' */
1610 _("%s can't build %s from the worklist; "
1611 "unit with the relationship '%s' is required."
1612 " Postponing..."),
1613 city_link(pcity),
1614 tgt_name,
1616 preq->source.value.diplrel));
1617
1618 if (preq->source.kind == VUT_DIPLREL_UNITANY) {
1619 reason = "need_diplrel_unitany";
1620 } else {
1621 fc_assert(preq->source.kind == VUT_DIPLREL_UNITANY_O);
1622 reason = "need_diplrel_unitany_o";
1623 }
1624
1625 script_server_signal_emit(signal_name, ptarget,
1626 pcity, reason);
1627 } else {
1628 const char *reason;
1629
1630 notify_player(pplayer, city_tile(pcity),
1631 E_CITY_CANTBUILD, ftc_server,
1632 _("%s can't build %s from the worklist; "
1633 "unit with the relationship '%s' is prohibited."
1634 " Postponing..."),
1635 city_link(pcity),
1636 tgt_name,
1638 preq->source.value.diplrel));
1639
1640 if (preq->source.kind == VUT_DIPLREL_UNITANY) {
1641 reason = "have_diplrel_unitany";
1642 } else {
1643 fc_assert(preq->source.kind == VUT_DIPLREL_UNITANY_O);
1644 reason = "have_diplrel_unitany_o";
1645 }
1646
1647 script_server_signal_emit(signal_name, ptarget,
1648 pcity, reason);
1649 }
1650 break;
1651 case VUT_MINSIZE:
1652 if (preq->present) {
1653 notify_player(pplayer, city_tile(pcity),
1654 E_CITY_CANTBUILD, ftc_server,
1655 _("%s can't build %s from the worklist; "
1656 "city must be of size %d or larger. "
1657 "Postponing..."),
1658 city_link(pcity),
1659 tgt_name,
1660 preq->source.value.minsize);
1661 script_server_signal_emit(signal_name, ptarget,
1662 pcity, "need_minsize");
1663 } else {
1664 notify_player(pplayer, city_tile(pcity),
1665 E_CITY_CANTBUILD, ftc_server,
1666 _("%s can't build %s from the worklist; "
1667 "city must be of size %d or smaller."
1668 "Postponing..."),
1669 city_link(pcity),
1670 tgt_name,
1671 (preq->source.value.minsize - 1));
1672 script_server_signal_emit(signal_name, ptarget,
1673 pcity, "need_minsize");
1674 }
1675 break;
1676 case VUT_MINCULTURE:
1677 if (preq->present) {
1678 notify_player(pplayer, city_tile(pcity),
1679 E_CITY_CANTBUILD, ftc_server,
1680 _("%s can't build %s from the worklist; "
1681 "city must have culture of %d. Postponing..."),
1682 city_link(pcity),
1683 tgt_name,
1684 preq->source.value.minculture);
1685 script_server_signal_emit(signal_name, ptarget,
1686 pcity, "need_minculture");
1687 } else {
1688 /* What has been written may not be unwritten. */
1689 purge = TRUE;
1690 }
1691 break;
1692 case VUT_MINFOREIGNPCT:
1693 if (preq->present) {
1694 notify_player(pplayer, city_tile(pcity),
1695 E_CITY_CANTBUILD, ftc_server,
1696 _("%s can't build %s from the worklist; "
1697 "city must have %d%% foreign population. Postponing..."),
1698 city_link(pcity),
1699 tgt_name,
1700 preq->source.value.minforeignpct);
1701 script_server_signal_emit(signal_name, ptarget,
1702 pcity, "need_minforeignpct");
1703 } else {
1704 notify_player(pplayer, city_tile(pcity),
1705 E_CITY_CANTBUILD, ftc_server,
1706 _("%s can't build %s from the worklist; "
1707 "city must have %d%% native population. Postponing..."),
1708 city_link(pcity),
1709 tgt_name,
1710 100 - preq->source.value.minforeignpct);
1711 script_server_signal_emit(signal_name, ptarget,
1712 pcity, "need_minforeignpct");
1713 }
1714 break;
1715 case VUT_MINTECHS:
1716 if (preq->present) {
1717 notify_player(pplayer, city_tile(pcity),
1718 E_CITY_CANTBUILD, ftc_server,
1719 _("%s can't build %s from the worklist; "
1720 "%d techs must be known. Postponing..."),
1721 city_link(pcity),
1722 tgt_name,
1723 preq->source.value.min_techs);
1724 script_server_signal_emit(signal_name, ptarget,
1725 pcity, "need_mintechs");
1726 } else {
1727 purge = TRUE;
1728 }
1729 break;
1730 case VUT_MAXTILEUNITS:
1731 if (preq->present) {
1732 notify_player(pplayer, city_tile(pcity),
1733 E_CITY_CANTBUILD, ftc_server,
1734 PL_("%s can't build %s from the worklist; "
1735 "more than %d unit on tile."
1736 " Postponing...",
1737 "%s can't build %s from the worklist; "
1738 "more than %d units on tile."
1739 " Postponing...",
1740 preq->source.value.max_tile_units),
1741 city_link(pcity),
1742 tgt_name,
1743 preq->source.value.max_tile_units);
1744 script_server_signal_emit(signal_name, ptarget,
1745 pcity, "need_tileunits");
1746 } else {
1747 notify_player(pplayer, city_tile(pcity),
1748 E_CITY_CANTBUILD, ftc_server,
1749 PL_("%s can't build %s from the worklist; "
1750 "fewer than %d unit on tile."
1751 " Postponing...",
1752 "%s can't build %s from the worklist; "
1753 "fewer than %d units on tile."
1754 " Postponing...",
1755 preq->source.value.max_tile_units + 1),
1756 city_link(pcity),
1757 tgt_name,
1758 preq->source.value.max_tile_units + 1);
1759 script_server_signal_emit(signal_name, ptarget,
1760 pcity, "need_tileunits");
1761 }
1762 break;
1763 case VUT_AI_LEVEL:
1764 /* Can't change AI level. */
1765 purge = TRUE;
1766 break;
1767 case VUT_TERRAINCLASS:
1768 /* Change of terrain class is expected to be very unlikely. Purge!
1769 * TODO: Analyze ruleset to see how unlikely terrain class change actually is. */
1770 purge = TRUE;
1771 break;
1772 case VUT_TERRFLAG:
1773 if (preq->present) {
1774 notify_player(pplayer, city_tile(pcity),
1775 E_CITY_CANTBUILD, ftc_server,
1776 _("%s can't build %s from the worklist; "
1777 "terrain with \"%s\" flag is required. "
1778 "Postponing..."),
1779 city_link(pcity),
1780 tgt_name,
1781 terrain_flag_id_name(preq->source.value.terrainflag));
1782 script_server_signal_emit(signal_name, ptarget,
1783 pcity, "need_terrainflag");
1784 } else {
1785 notify_player(pplayer, city_tile(pcity),
1786 E_CITY_CANTBUILD, ftc_server,
1787 _("%s can't build %s from the worklist; "
1788 "terrain with \"%s\" flag is prohibited. "
1789 "Postponing..."),
1790 city_link(pcity),
1791 tgt_name,
1792 terrain_flag_id_name(preq->source.value.terrainflag));
1793 script_server_signal_emit(signal_name, ptarget,
1794 pcity, "have_terrainflag");
1795 }
1796 break;
1797 case VUT_ROADFLAG:
1798 if (preq->present) {
1799 notify_player(pplayer, city_tile(pcity),
1800 E_CITY_CANTBUILD, ftc_server,
1801 _("%s can't build %s from the worklist; "
1802 "road with \"%s\" flag is required. "
1803 "Postponing..."),
1804 city_link(pcity),
1805 tgt_name,
1806 road_flag_id_name(preq->source.value.roadflag));
1807 script_server_signal_emit(signal_name, ptarget,
1808 pcity, "need_roadflag");
1809 } else {
1810 notify_player(pplayer, city_tile(pcity),
1811 E_CITY_CANTBUILD, ftc_server,
1812 _("%s can't build %s from the worklist; "
1813 "road with \"%s\" flag is prohibited. "
1814 "Postponing..."),
1815 city_link(pcity),
1816 tgt_name,
1817 road_flag_id_name(preq->source.value.roadflag));
1818 script_server_signal_emit(signal_name, ptarget,
1819 pcity, "have_roadflag");
1820 }
1821 break;
1822 case VUT_EXTRAFLAG:
1823 if (preq->present) {
1824 notify_player(pplayer, city_tile(pcity),
1825 E_CITY_CANTBUILD, ftc_server,
1826 _("%s can't build %s from the worklist; "
1827 "extra with \"%s\" flag is required. "
1828 "Postponing..."),
1829 city_link(pcity),
1830 tgt_name,
1831 extra_flag_id_translated_name(preq->source.value.extraflag));
1832 script_server_signal_emit(signal_name, ptarget,
1833 pcity, "need_extraflag");
1834 } else {
1835 notify_player(pplayer, city_tile(pcity),
1836 E_CITY_CANTBUILD, ftc_server,
1837 _("%s can't build %s from the worklist; "
1838 "extra with \"%s\" flag is prohibited. "
1839 "Postponing..."),
1840 city_link(pcity),
1841 tgt_name,
1842 extra_flag_id_translated_name(preq->source.value.extraflag));
1843 script_server_signal_emit(signal_name, ptarget,
1844 pcity, "have_extraflag");
1845 }
1846 break;
1847 case VUT_UTYPE:
1848 case VUT_UTFLAG:
1849 case VUT_UCLASS:
1850 case VUT_UCFLAG:
1851 case VUT_MINVETERAN:
1852 case VUT_UNITSTATE:
1853 case VUT_ACTIVITY:
1854 case VUT_MINMOVES:
1855 case VUT_MINHP:
1856 case VUT_ACTION:
1857 case VUT_OTYPE:
1858 case VUT_SPECIALIST:
1859 case VUT_TERRAINALTER: /* XXX could do this in principle */
1860 case VUT_CITYTILE:
1861 /* Will only happen with a bogus ruleset. */
1862 log_error("worklist_change_build_target() has bogus preq");
1863 break;
1864 case VUT_CITYSTATUS:
1865 if (preq->source.value.citystatus == CITYS_OWNED_BY_ORIGINAL) {
1866 if (preq->range == REQ_RANGE_CITY) {
1867 /* Can't change at this range */
1868 purge = TRUE;
1869 } else {
1870 if (preq->present) {
1871 notify_player(pplayer, city_tile(pcity),
1872 E_CITY_CANTBUILD, ftc_server,
1873 /* TRANS: last %s is a CityStatus ("OwnedByOriginal") */
1874 _("%s can't build %s from the worklist; "
1875 "only available when city in range %s \"%s\". "
1876 "Postponing..."),
1877 city_link(pcity),
1878 tgt_name, req_range_name(preq->range),
1879 citystatus_type_name(preq->source.value.citystatus));
1880 script_server_signal_emit(signal_name, ptarget,
1881 pcity, "need_citystatus");
1882 } else {
1883 notify_player(pplayer, city_tile(pcity),
1884 E_CITY_CANTBUILD, ftc_server,
1885 /* TRANS: last %s is a CityStatus ("OwnedByOriginal") */
1886 _("%s can't build %s from the worklist; "
1887 "not available when city in range %s is \"%s\". "
1888 "Postponing..."),
1889 city_link(pcity),
1890 tgt_name, req_range_name(preq->range),
1891 citystatus_type_name(preq->source.value.citystatus));
1892 script_server_signal_emit(signal_name, ptarget,
1893 pcity, "have_citystatus");
1894 }
1895 }
1896 } else {
1897 /* Other status types will only happen with a bogus ruleset. */
1898 log_error("worklist_change_build_target() has bogus citystatus preq");
1899 }
1900 break;
1901 case VUT_MINYEAR:
1902 if (preq->present) {
1903 notify_player(pplayer, city_tile(pcity),
1904 E_CITY_CANTBUILD, ftc_server,
1905 /* TRANS: last %s is a date */
1906 _("%s can't build %s from the worklist; "
1907 "only available from %s. Postponing..."),
1908 city_link(pcity),
1909 tgt_name,
1910 textyear(preq->source.value.minyear));
1911 script_server_signal_emit(signal_name, ptarget,
1912 pcity, "need_minyear");
1913 } else {
1914 /* Can't go back in time. */
1915 purge = TRUE;
1916 }
1917 break;
1918 case VUT_MINCALFRAG:
1919 /* Unlike VUT_MINYEAR, a requirement in either direction is
1920 * likely to be fulfilled sooner or later. */
1921 if (preq->present) {
1922 notify_player(pplayer, city_tile(pcity),
1923 E_CITY_CANTBUILD, ftc_server,
1924 /* TRANS: last %s is a calendar fragment from
1925 * the ruleset; may be a bare number */
1926 _("%s can't build %s from the worklist; "
1927 "only available from %s. Postponing..."),
1928 city_link(pcity),
1929 tgt_name,
1930 textcalfrag(preq->source.value.mincalfrag));
1931 script_server_signal_emit(signal_name, ptarget,
1932 pcity, "need_mincalfrag");
1933 } else {
1934 fc_assert_action(preq->source.value.mincalfrag > 0, break);
1935 notify_player(pplayer, city_tile(pcity),
1936 E_CITY_CANTBUILD, ftc_server,
1937 /* TRANS: last %s is a calendar fragment from
1938 * the ruleset; may be a bare number */
1939 _("%s can't build %s from the worklist; "
1940 "not available after %s. Postponing..."),
1941 city_link(pcity),
1942 tgt_name,
1943 textcalfrag(preq->source.value.mincalfrag - 1));
1944 script_server_signal_emit(signal_name, ptarget,
1945 pcity, "have_mincalfrag");
1946 }
1947 break;
1948 case VUT_TOPO:
1949 if (preq->present) {
1950 notify_player(pplayer, city_tile(pcity),
1951 E_CITY_CANTBUILD, ftc_server,
1952 /* TRANS: third %s is topology flag name
1953 * ("WrapX", "ISO", etc) */
1954 _("%s can't build %s from the worklist; "
1955 "only available in worlds with %s map."),
1956 city_link(pcity),
1957 tgt_name,
1958 _(topo_flag_name(preq->source.value.topo_property)));
1959 script_server_signal_emit(signal_name, ptarget,
1960 pcity, "need_topo");
1961 }
1962 purge = TRUE;
1963 break;
1964 case VUT_SERVERSETTING:
1965 notify_player(pplayer, city_tile(pcity),
1966 E_CITY_CANTBUILD, ftc_server,
1967 /* TRANS: %s is a server setting, its value and
1968 * if it is required to be present or absent.
1969 * The string's format is specified in
1970 * ssetv_human_readable().
1971 * Example: "killstack is enabled". */
1972 _("%s can't build %s from the worklist; "
1973 "only available when the server setting "
1974 "%s."),
1975 city_link(pcity),
1976 tgt_name,
1977 ssetv_human_readable(preq->source.value.ssetval,
1978 preq->present));
1979 script_server_signal_emit(signal_name, ptarget,
1980 pcity, "need_setting");
1981 /* Don't assume that the server setting will be changed. */
1982 purge = TRUE;
1983 break;
1984 case VUT_AGE:
1985 if (preq->present) {
1986 notify_player(pplayer, city_tile(pcity),
1987 E_CITY_CANTBUILD, ftc_server,
1988 _("%s can't build %s from the worklist; "
1989 "only available once %d turns old. Postponing..."),
1990 city_link(pcity),
1991 tgt_name,
1992 preq->source.value.age);
1993 script_server_signal_emit(signal_name, ptarget,
1994 pcity, "need_age");
1995 } else {
1996 /* Can't go back in time. */
1997 purge = TRUE;
1998 }
1999 break;
2000 case VUT_NONE:
2001 case VUT_COUNT:
2003 "worklist_change_build_target() "
2004 "called with invalid preq");
2005 break;
2006 /* No default handling here, as we want compiler warning
2007 * if new requirement type is added to enum and it's not handled
2008 * here. */
2009 };
2010 break;
2011 }
2012
2013 /* Almost all cases emit signal in the end, so city check needed. */
2014 if (!city_exist(saved_id)) {
2015 /* Some script has removed city */
2016 return TRUE;
2017 }
2018
2020
2021 if (!known) {
2022 /* FIXME: make can_city_build_improvement_now() return a reason enum,
2023 * so we can notify user with it.
2024 * Likely the building already exist. */
2025 purge = TRUE;
2026 }
2027
2028 return purge;
2029}
2030
2031/**********************************************************************/
2037static bool worklist_change_build_target(struct player *pplayer,
2038 struct city *pcity)
2039{
2040 struct universal target;
2041 bool success = FALSE;
2042 int i;
2043 int saved_id = pcity->id;
2044 bool city_checked = TRUE; /* This is used to avoid spurious city_exist() calls */
2045 struct worklist *pwl = &pcity->worklist;
2046
2047 if (worklist_is_empty(pwl)) {
2048 /* Nothing in the worklist; bail now. */
2049 return FALSE;
2050 }
2051
2052 i = 0;
2053 while (!success && i < worklist_length(pwl)) {
2054
2055 if (!city_checked) {
2056 if (!city_exist(saved_id)) {
2057 /* Some script has removed useless city that cannot build
2058 * what it is told to! */
2059 return FALSE;
2060 }
2061 city_checked = TRUE;
2062 }
2063
2064 if (worklist_peek_ith(pwl, &target, i)) {
2065 success = can_city_build_now(pcity, &target);
2066 } else {
2067 success = FALSE;
2068 }
2069 i++;
2070
2071 if (success) {
2072 break; /* while */
2073 }
2074
2075 switch (target.kind) {
2076 case VUT_UTYPE:
2077 {
2078 const struct unit_type *ptarget = target.value.utype;
2079 const struct unit_type *pupdate = unit_upgrades_to(pcity, ptarget);
2080 bool purge;
2081
2082 /* Maybe we can just upgrade the target to what the city /can/ build. */
2083 if (U_NOT_OBSOLETED == pupdate) {
2084 /* Nope, we're stuck. Skip this item from the worklist. */
2085 if (ptarget->require_advance != NULL
2086 && TECH_KNOWN != research_invention_state
2087 (research_get(pplayer),
2088 advance_number(ptarget->require_advance))) {
2089 notify_player(pplayer, city_tile(pcity),
2090 E_CITY_CANTBUILD, ftc_server,
2091 _("%s can't build %s from the worklist; "
2092 "tech %s not yet available. Postponing..."),
2093 city_link(pcity), utype_name_translation(ptarget),
2095 script_server_signal_emit("unit_cant_be_built", ptarget, pcity,
2096 "need_tech");
2097 } else {
2098 /* Unknown or requirement from vector. */
2099 purge = worklist_item_postpone_req_vec(&target, pcity, pplayer,
2100 saved_id);
2101 }
2102 city_checked = FALSE;
2103 break;
2104 } else {
2105 purge = !can_city_build_unit_later(pcity, pupdate);
2106 }
2107 if (purge) {
2108 /* If the city can never build this unit or its descendants,
2109 * drop it. */
2110 notify_player(pplayer, city_tile(pcity),
2111 E_CITY_CANTBUILD, ftc_server,
2112 _("%s can't build %s from the worklist. Purging..."),
2113 city_link(pcity),
2114 /* Yes, warn about the targets that's actually
2115 in the worklist, not its obsolete-closure
2116 pupdate. */
2117 utype_name_translation(ptarget));
2118 script_server_signal_emit("unit_cant_be_built", ptarget, pcity,
2119 "never");
2120 if (city_exist(saved_id)) {
2121 city_checked = TRUE;
2122 /* Purge this worklist item. */
2123 i--;
2124 worklist_remove(pwl, i);
2125 } else {
2126 city_checked = FALSE;
2127 }
2128 } else {
2129 /* Yep, we can go after pupdate instead. Joy! */
2130 notify_player(pplayer, city_tile(pcity), E_WORKLIST, ftc_server,
2131 _("Production of %s is upgraded to %s in %s."),
2132 utype_name_translation(ptarget),
2133 utype_name_translation(pupdate),
2134 city_link(pcity));
2135 target.value.utype = pupdate;
2136 }
2137 break;
2138 }
2139 case VUT_IMPROVEMENT:
2140 {
2141 const struct impr_type *ptarget = target.value.building;
2142 const struct impr_type *pupdate = building_upgrades_to(pcity, ptarget);
2143 bool purge;
2144
2145 /* If the city can never build this improvement, drop it. */
2146 success = can_city_build_improvement_later(pcity, pupdate);
2147 purge = !success;
2148
2149 /* Maybe this improvement has been obsoleted by something that
2150 we can build. */
2151 if (purge) {
2152 /* Nope, no use. *sigh* */
2153
2154 /* Can it be postponed? */
2155 if (can_city_build_improvement_later(pcity, ptarget)) {
2156 purge = worklist_item_postpone_req_vec(&target, pcity, pplayer,
2157 saved_id);
2158
2159 /* Almost all cases emit signal in the end, so city check needed. */
2160 if (!city_exist(saved_id)) {
2161 /* Some script has removed city */
2162 return FALSE;
2163 }
2164 city_checked = TRUE;
2165 }
2166 } else if (success) {
2167 /* Hey, we can upgrade the improvement! */
2168 notify_player(pplayer, city_tile(pcity), E_WORKLIST, ftc_server,
2169 _("Production of %s is upgraded to %s in %s."),
2170 city_improvement_name_translation(pcity, ptarget),
2171 city_improvement_name_translation(pcity, pupdate),
2172 city_link(pcity));
2173 target.value.building = pupdate;
2174 }
2175
2176 if (purge) {
2177 /* Never in a million years. */
2178 notify_player(pplayer, city_tile(pcity),
2179 E_CITY_CANTBUILD, ftc_server,
2180 _("%s can't build %s from the worklist. Purging..."),
2181 city_link(pcity),
2182 city_improvement_name_translation(pcity, ptarget));
2183 script_server_signal_emit("building_cant_be_built", ptarget, pcity,
2184 "never");
2185 if (city_exist(saved_id)) {
2186 city_checked = TRUE;
2187 /* Purge this worklist item. */
2188 i--;
2189 worklist_remove(pwl, i);
2190 } else {
2191 city_checked = FALSE;
2192 }
2193 }
2194 break;
2195 }
2196 default:
2197 /* skip useless target */
2198 log_error("worklist_change_build_target() has unrecognized "
2199 "target kind (%d)", target.kind);
2200 break;
2201 };
2202 } /* while */
2203
2204 if (success) {
2205 /* All okay. Switch targets. */
2206 change_build_target(pplayer, pcity, &target, E_WORKLIST);
2207
2208 /* i is the index immediately _after_ the item we're changing to.
2209 Remove the (i-1)th item from the worklist. */
2210 worklist_remove(pwl, i - 1);
2211 }
2212
2213 if (worklist_is_empty(pwl)) {
2214 /* There *was* something in the worklist, but it's empty now. Bug the
2215 player about it. */
2216 notify_player(pplayer, city_tile(pcity), E_WORKLIST, ftc_server,
2217 /* TRANS: The <city> worklist .... */
2218 _("The %s worklist is now empty."),
2219 city_link(pcity));
2220 }
2221
2222 return success;
2223}
2224
2225/**********************************************************************/
2230void choose_build_target(struct player *pplayer, struct city *pcity)
2231{
2232 /* Pick the next thing off the worklist. */
2233 if (worklist_change_build_target(pplayer, pcity)) {
2234 return;
2235 }
2236
2237 /* Try building the same thing again. Repeat building doesn't require a
2238 * call to change_build_target, so just return. */
2239 switch (pcity->production.kind) {
2240 case VUT_UTYPE:
2241 /* We can build a unit again unless it's unique or we have lost the tech. */
2242 if (!utype_has_flag(pcity->production.value.utype, UTYF_UNIQUE)
2243 && can_city_build_unit_now(pcity, pcity->production.value.utype)) {
2244 log_base(LOG_BUILD_TARGET, "%s repeats building %s", city_name_get(pcity),
2246 return;
2247 }
2248 break;
2249 case VUT_IMPROVEMENT:
2251 /* We can build space and coinage again, and possibly others. */
2252 log_base(LOG_BUILD_TARGET, "%s repeats building %s", city_name_get(pcity),
2254 return;
2255 }
2256 break;
2257 default:
2258 /* fallthru */
2259 break;
2260 };
2261
2262 /* Find *something* to do! */
2263 log_debug("Trying advisor_choose_build.");
2264 advisor_choose_build(pplayer, pcity);
2265 log_debug("Advisor_choose_build didn't kill us.");
2266}
2267
2268/**********************************************************************/
2273static const struct impr_type *building_upgrades_to(struct city *pcity,
2274 const struct impr_type *pimprove)
2275{
2276 const struct impr_type *check = pimprove;
2277 const struct impr_type *best_upgrade = NULL;
2278
2279 if (!can_city_build_improvement_direct(pcity, check)) {
2280 return NULL;
2281 }
2282 while (valid_improvement(check = improvement_replacement(check))) {
2283 if (can_city_build_improvement_direct(pcity, check)) {
2284 best_upgrade = check;
2285 }
2286 }
2287
2288 return best_upgrade;
2289}
2290
2291/**********************************************************************/
2294static void upgrade_building_prod(struct city *pcity)
2295{
2296 const struct impr_type *producing = pcity->production.value.building;
2297 const struct impr_type *upgrading = building_upgrades_to(pcity, producing);
2298
2299 if (upgrading && can_city_build_improvement_now(pcity, upgrading)) {
2300 notify_player(city_owner(pcity), city_tile(pcity),
2301 E_UNIT_UPGRADED, ftc_server,
2302 _("Production of %s is upgraded to %s in %s."),
2305 city_link(pcity));
2306 pcity->production.kind = VUT_IMPROVEMENT;
2307 pcity->production.value.building = upgrading;
2308 }
2309}
2310
2311/**********************************************************************/
2319static const struct unit_type *unit_upgrades_to(struct city *pcity,
2320 const struct unit_type *punittype)
2321{
2322 const struct unit_type *check = punittype;
2323 const struct unit_type *best_upgrade = U_NOT_OBSOLETED;
2324
2325 if (!can_city_build_unit_direct(pcity, punittype)) {
2326 return U_NOT_OBSOLETED;
2327 }
2328 while ((check = check->obsoleted_by) != U_NOT_OBSOLETED) {
2329 if (can_city_build_unit_direct(pcity, check)) {
2330 best_upgrade = check;
2331 }
2332 }
2333
2334 return best_upgrade;
2335}
2336
2337/**********************************************************************/
2340static void upgrade_unit_prod(struct city *pcity)
2341{
2342 const struct unit_type *producing = pcity->production.value.utype;
2343 const struct unit_type *upgrading = unit_upgrades_to(pcity, producing);
2344
2345 if (upgrading && can_city_build_unit_direct(pcity, upgrading)) {
2346 notify_player(city_owner(pcity), city_tile(pcity),
2347 E_UNIT_UPGRADED, ftc_server,
2348 _("Production of %s is upgraded to %s in %s."),
2349 utype_name_translation(producing),
2350 utype_name_translation(upgrading),
2351 city_link(pcity));
2352 pcity->production.value.utype = upgrading;
2353 }
2354}
2355
2356/**********************************************************************/
2363static bool city_distribute_surplus_shields(struct player *pplayer,
2364 struct city *pcity)
2365{
2366 int size_reduction = 0;
2367 struct unit *sacrifizer;
2368
2369 if (pcity->surplus[O_SHIELD] < 0) {
2371 if (utype_upkeep_cost(unit_type_get(punit), pplayer, O_SHIELD) > 0
2372 && pcity->surplus[O_SHIELD] < 0) {
2373 const char *punit_link = unit_link(punit);
2374
2375 /* TODO: Should the unit try to help cities on adjacent tiles? That
2376 * would be a rules change. (This action is performed by the game
2377 * it self) */
2378 if (upkeep_kill_unit(punit, O_SHIELD, ULR_UPKEEP,
2380 notify_player(pplayer, city_tile(pcity),
2381 E_UNIT_LOST_MISC, ftc_server,
2382 _("%s can't upkeep %s, unit disbanded."),
2383 city_link(pcity), punit_link);
2384 }
2385
2386 /* pcity->surplus[O_SHIELD] is automatically updated. */
2387 }
2389 }
2390
2391 if (pcity->surplus[O_SHIELD] < 0) {
2392 /* Special case: MissingXProtected. This nasty unit won't go so easily.
2393 * It'd rather make the citizens pay in blood for their failure to upkeep
2394 * it! If we make it here all normal units are already disbanded, so only
2395 * undisbandable ones remain. */
2398
2399 if (upkeep > 0 && pcity->surplus[O_SHIELD] < 0) {
2400
2401 size_reduction++;
2402 sacrifizer = punit;
2403
2404 /* No upkeep for the unit this turn. */
2405 pcity->surplus[O_SHIELD] += upkeep;
2406 }
2408 }
2409
2410 /* Now we confirm changes made last turn. */
2411 pcity->shield_stock += pcity->surplus[O_SHIELD];
2412 pcity->before_change_shields = pcity->shield_stock;
2414
2415 /* Previous turn values stored, and they are consistent with
2416 * other previous turn data.
2417 * Now reduce city size, likely messing all the values. */
2418 if (size_reduction > 0) {
2419 if (size_reduction == 1) {
2420 notify_player(pplayer, city_tile(pcity),
2421 E_UNIT_LOST_MISC, ftc_server,
2422 _("Citizens in %s perish for their failure to "
2423 "upkeep %s!"),
2424 city_link(pcity), unit_link(sacrifizer));
2425 } else {
2426 notify_player(pplayer, city_tile(pcity),
2427 E_UNIT_LOST_MISC, ftc_server,
2428 _("Citizens in %s perish for their failure to "
2429 "upkeep units!"),
2430 city_link(pcity));
2431 }
2432
2433 if (!city_reduce_size(pcity, size_reduction, NULL, "upkeep_failure")) {
2434 return FALSE;
2435 }
2436 }
2437
2438 return TRUE;
2439}
2440
2441/**********************************************************************/
2444static bool city_build_building(struct player *pplayer, struct city *pcity)
2445{
2446 bool space_part;
2447 int mod;
2448 const struct impr_type *pimprove;
2449 int saved_id = pcity->id;
2450
2451 if (city_production_has_flag(pcity, IF_GOLD)) {
2452 fc_assert(pcity->before_change_shields >= 0);
2453
2454 /* pcity->before_change_shields already contains the surplus from
2455 * this turn. */
2456 pplayer->economic.gold += pcity->before_change_shields;
2457 pcity->before_change_shields = 0;
2458 pcity->shield_stock = 0;
2459 choose_build_target(pplayer, pcity);
2460
2461 return TRUE;
2462 }
2463
2464 upgrade_building_prod(pcity);
2465
2466 /* The final (after upgrade) build target */
2467 pimprove = pcity->production.value.building;
2468
2469 if (!can_city_build_improvement_now(pcity, pimprove)) {
2470 notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
2471 _("%s is building %s, which is no longer available."),
2472 city_link(pcity),
2473 city_improvement_name_translation(pcity, pimprove));
2474 script_server_signal_emit("building_cant_be_built", pimprove, pcity,
2475 "unavailable");
2476 return TRUE;
2477 }
2478 if (pcity->shield_stock >= impr_build_shield_cost(pcity, pimprove)) {
2479 int cost;
2480
2481 if (is_small_wonder(pimprove)) {
2482 city_list_iterate(pplayer->cities, wcity) {
2483 if (city_has_building(wcity, pimprove)) {
2484 city_remove_improvement(wcity, pimprove);
2485 break;
2486 }
2488 }
2489
2490 space_part = TRUE;
2491 if (get_current_construction_bonus(pcity, EFT_SS_STRUCTURAL,
2492 RPT_CERTAIN) > 0) {
2493 pplayer->spaceship.structurals++;
2494 } else if (get_current_construction_bonus(pcity, EFT_SS_COMPONENT,
2495 RPT_CERTAIN) > 0) {
2496 pplayer->spaceship.components++;
2497 } else if (get_current_construction_bonus(pcity, EFT_SS_MODULE,
2498 RPT_CERTAIN) > 0) {
2499 pplayer->spaceship.modules++;
2500 } else {
2501 space_part = FALSE;
2502 city_add_improvement(pcity, pimprove);
2503 }
2504 cost = impr_build_shield_cost(pcity, pimprove);
2505 pcity->before_change_shields -= cost;
2506 pcity->shield_stock -= cost;
2507 pcity->turn_last_built = game.info.turn;
2508 /* to eliminate micromanagement */
2509 if (is_great_wonder(pimprove)) {
2510 notify_player(NULL, city_tile(pcity), E_WONDER_BUILD, ftc_server,
2511 _("The %s have finished building %s in %s."),
2512 nation_plural_for_player(pplayer),
2513 city_improvement_name_translation(pcity, pimprove),
2514 city_link(pcity));
2515 }
2516
2517 notify_player(pplayer, city_tile(pcity), E_IMP_BUILD, ftc_server,
2518 _("%s has finished building %s."),
2519 city_link(pcity), improvement_name_translation(pimprove));
2520 script_server_signal_emit("building_built", pimprove, pcity);
2521
2522 if (!city_exist(saved_id)) {
2523 /* Script removed city */
2524 return FALSE;
2525 }
2526
2527 /* Call this function since some buildings may change the
2528 * the vision range of a city */
2529 city_refresh_vision(pcity);
2530
2531 if ((mod = get_current_construction_bonus(pcity, EFT_GIVE_IMM_TECH,
2532 RPT_CERTAIN))) {
2533 struct research *presearch = research_get(pplayer);
2534 char research_name[MAX_LEN_NAME * 2];
2535 int i;
2536 const char *provider = improvement_name_translation(pimprove);
2537
2538 notify_research(presearch, NULL, E_TECH_GAIN, ftc_server,
2539 PL_("%s boosts research; you gain %d immediate "
2540 "advance.",
2541 "%s boosts research; you gain %d immediate "
2542 "advances.",
2543 mod), provider, mod);
2544
2545 research_pretty_name(presearch, research_name, sizeof(research_name));
2546 for (i = 0; i < mod; i++) {
2547 Tech_type_id tech = pick_free_tech(presearch);
2548 const char *adv_name = research_advance_name_translation(presearch, tech);
2549
2550 give_immediate_free_tech(presearch, tech);
2551 notify_research(presearch, NULL, E_TECH_GAIN, ftc_server,
2552 /* TRANS: Tech from building (Darwin's Voyage) */
2553 Q_("?frombldg:Acquired %s from %s."), adv_name,
2554 provider);
2555
2556 notify_research_embassies(presearch, NULL, E_TECH_EMBASSY, ftc_server,
2557 /* TRANS: Tech from building (Darwin's
2558 * Voyage) */
2559 Q_("?frombldg:The %s have acquired %s "
2560 "from %s."),
2561 research_name, adv_name, provider);
2562 }
2563 }
2564 if (space_part && pplayer->spaceship.state == SSHIP_NONE) {
2565 notify_player(NULL, city_tile(pcity), E_SPACESHIP, ftc_server,
2566 _("The %s have started building a spaceship!"),
2567 nation_plural_for_player(pplayer));
2568 pplayer->spaceship.state = SSHIP_STARTED;
2569 }
2570 if (space_part) {
2571 /* space ship part build */
2572 send_spaceship_info(pplayer, NULL);
2573 } else {
2574 /* Update city data. */
2575 if (city_refresh(pcity)) {
2576 auto_arrange_workers(pcity);
2577 }
2578 }
2579
2580 /* Move to the next thing in the worklist */
2581 choose_build_target(pplayer, pcity);
2582 }
2583
2584 return TRUE;
2585}
2586
2587/**********************************************************************/
2596static struct unit *city_create_unit(struct city *pcity,
2597 const struct unit_type *utype,
2598 struct citizens_reduction *red)
2599{
2600 struct player *pplayer = city_owner(pcity);
2601 struct unit *punit;
2602 int saved_unit_id;
2603 int pop_cost = utype_pop_value(utype, pcity);
2604
2605 punit = unit_virtual_prepare(pplayer, pcity->tile, utype,
2607 pcity->id, -1, -1);
2608 pplayer->score.units_built++;
2609 if (pop_cost > 0 && pcity->nationality) {
2610 /* We don't reduce city size in-place to keep it correct and
2611 * existing at all while we call the following callback */
2612 citizens_unit_nationality(pcity, pop_cost, red);
2613 } else if (red) {
2614 red->change = 0;
2615 }
2616 (void) place_unit(punit, pplayer, pcity, NULL, FALSE);
2617 saved_unit_id = punit->id;
2618
2619 /* If city has a rally point set, give the unit a move order. */
2620 if (pcity->rally_point.length) {
2625 * sizeof(struct unit_order));
2626 memcpy(punit->orders.list, pcity->rally_point.orders,
2627 pcity->rally_point.length * sizeof(struct unit_order));
2628 }
2629
2630 /* This might destroy pcity and/or punit: */
2631 script_server_signal_emit("unit_built", punit, pcity);
2632
2633 if (unit_is_alive(saved_unit_id)) {
2634 return punit;
2635 } else {
2636 return NULL;
2637 }
2638}
2639
2640/**********************************************************************/
2649static bool city_build_unit(struct player *pplayer, struct city *pcity)
2650{
2651 const struct unit_type *utype;
2652 struct worklist *pwl = &pcity->worklist;
2653 int unit_shield_cost, num_units, i;
2654 int saved_city_id = pcity->id;
2655
2656 fc_assert_ret_val(pcity->production.kind == VUT_UTYPE, FALSE);
2657
2658 /* If the city has already bought a unit which is now obsolete, don't try
2659 * to upgrade the production. The new unit might require more shields, which
2660 * would be bad if it was bought to urgently defend a city. (Equally it
2661 * might be the same cost or cheaper, but tough; you hurried the unit so
2662 * you miss out on technological advances.) */
2663 if (city_can_change_build(pcity)) {
2664 upgrade_unit_prod(pcity);
2665 }
2666
2667 utype = pcity->production.value.utype;
2668 unit_shield_cost = utype_build_shield_cost(pcity, NULL, utype);
2669
2670 /* We must make a special case for barbarians here, because they are
2671 so dumb. Really. They don't know the prerequisite techs for units
2672 they build!! - Per */
2673 if (!can_city_build_unit_direct(pcity, utype)
2674 && !is_barbarian(pplayer)) {
2675 notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
2676 _("%s is building %s, which is no longer available."),
2677 city_link(pcity), utype_name_translation(utype));
2678
2679 /* Log before signal emitting, so pointers are certainly valid */
2680 log_verbose("%s %s tried to build %s, which is not available.",
2682 city_name_get(pcity), utype_rule_name(utype));
2683 script_server_signal_emit("unit_cant_be_built", utype, pcity,
2684 "unavailable");
2685 return city_exist(saved_city_id);
2686 }
2687
2688 if (pcity->shield_stock >= unit_shield_cost) {
2689 int pop_cost = utype_pop_value(utype, pcity);
2690 struct unit *punit;
2691
2692 /* Should we disband the city? -- Massimo */
2693 if (city_size_get(pcity) == pop_cost
2694 && is_city_option_set(pcity, CITYO_DISBAND)) {
2695 return !disband_city(pcity);
2696 }
2697
2698 if (city_size_get(pcity) <= pop_cost) {
2699 notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
2700 /* TRANS: city ... utype ... size ... pop_cost */
2701 _("%s can't build %s yet. "
2702 "(city size: %d, unit population cost: %d)"),
2704 city_size_get(pcity), pop_cost);
2705 script_server_signal_emit("unit_cant_be_built", utype, pcity,
2706 "pop_cost");
2707 return city_exist(saved_city_id);
2708 }
2709
2710 fc_assert(pop_cost == 0 || city_size_get(pcity) >= pop_cost);
2711
2712 /* don't update turn_last_built if we returned above */
2713 pcity->turn_last_built = game.info.turn;
2714
2715 /* check if we can build more than one unit (effect City_Build_Slots) */
2716 (void) city_production_build_units(pcity, FALSE, &num_units);
2717
2718 /* We should be able to build at least one (by checks above) */
2719 fc_assert(num_units >= 1);
2720
2721 for (i = 0; i < num_units; i++) {
2722 struct citizens_reduction natred[MAX_CITY_NATIONALITIES + 1];
2723
2724 punit = city_create_unit(pcity, utype, natred);
2725
2726 /* Check if the city still exists (script might have removed it).
2727 * If not, we assume any effects / announcements done below were
2728 * already replaced by the script if necessary. */
2729 if (!city_exist(saved_city_id)) {
2730 break;
2731 }
2732
2733 if (punit) {
2734 notify_player(pplayer, city_tile(pcity), E_UNIT_BUILT, ftc_server,
2735 /* TRANS: <city> is finished building <unit/building>. */
2736 _("%s is finished building %s."),
2737 city_link(pcity), utype_name_translation(utype));
2738 }
2739
2740 /* After we created the unit remove the citizen. This will also
2741 * rearrange the worker to take into account the extra resources
2742 * (food) needed. */
2743 if (pop_cost > 0) {
2744 /* This won't disband city due to pop_cost, but script might
2745 * still destroy city. */
2746 citizens_reduction_apply(pcity, natred);
2747 /* If the city has changed its nationalities during
2748 * "unit_built" signal, we take some other citizens instead */
2749 if (!city_reduce_size(pcity, pop_cost, NULL, "unit_built")) {
2750 break;
2751 }
2752 }
2753
2754 /* to eliminate micromanagement, we only subtract the unit's cost */
2755 /* signals could change the prod stock! */
2756 if ((pcity->before_change_shields -= unit_shield_cost) < 0) {
2757 pcity->before_change_shields = 0;
2758 }
2759 if ((pcity->shield_stock -= unit_shield_cost) < 0) {
2760 log_normal("City %s (%s) has built %s but has no %d shields "
2761 "for it, nullifying shield stock", city_name_get(pcity),
2762 player_name(pplayer), utype_rule_name(utype),
2763 unit_shield_cost);
2764 pcity->shield_stock = 0;
2765 }
2766
2767 if (pop_cost > 0) {
2768 /* Additional message if the unit has population cost. */
2769 notify_player(pplayer, city_tile(pcity), E_UNIT_BUILT_POP_COST,
2770 ftc_server,
2771 /* TRANS: "<unit> cost... <city> shrinks..."
2772 * Plural in "%d population", not "size %d". */
2773 PL_("%s cost %d population. %s shrinks to size %d.",
2774 "%s cost %d population. %s shrinks to size %d.",
2775 pop_cost),
2776 utype_name_translation(utype), pop_cost,
2777 city_link(pcity), city_size_get(pcity));
2778 }
2779
2780 if (i != 0 && worklist_length(pwl) > 0) {
2781 /* remove the build unit from the worklist; it has to be one less
2782 * than units build to preserve the next build target from the
2783 * worklist */
2784 worklist_remove(pwl, 0);
2785 }
2786 } /* for */
2787
2788 if (city_exist(saved_city_id)) {
2789 if (pcity->rally_point.length && !pcity->rally_point.persistent) {
2791 }
2792
2793 /* Done building this unit; time to move on to the next. */
2794 choose_build_target(pplayer, pcity);
2795 }
2796 } /* if */
2797
2798 return city_exist(saved_city_id);
2799}
2800
2801/**********************************************************************/
2804static bool city_build_stuff(struct player *pplayer, struct city *pcity)
2805{
2806 if (!city_distribute_surplus_shields(pplayer, pcity)) {
2807 return FALSE;
2808 }
2809
2812
2813 switch (pcity->production.kind) {
2814 case VUT_IMPROVEMENT:
2815 return city_build_building(pplayer, pcity);
2816 case VUT_UTYPE:
2817 return city_build_unit(pplayer, pcity);
2818 default:
2819 /* must never happen! */
2821 break;
2822 };
2823 return FALSE;
2824}
2825
2826/**********************************************************************/
2836static bool sell_random_building(struct player *pplayer,
2837 struct cityimpr_list *imprs)
2838{
2839 struct cityimpr *pcityimpr;
2840 int r;
2841
2842 fc_assert_ret_val(pplayer != NULL, FALSE);
2843
2844 if (!imprs || cityimpr_list_size(imprs) == 0) {
2845 return FALSE;
2846 }
2847
2848 r = fc_rand(cityimpr_list_size(imprs));
2849 pcityimpr = cityimpr_list_get(imprs, r);
2850
2851 notify_player(pplayer, city_tile(pcityimpr->pcity), E_IMP_AUCTIONED,
2852 ftc_server,
2853 _("Can't afford to maintain %s in %s, building sold!"),
2855 city_link(pcityimpr->pcity));
2856 log_debug("%s: sold building (%s)", player_name(pplayer),
2858
2859 do_sell_building(pplayer, pcityimpr->pcity, pcityimpr->pimprove, "cant_maintain");
2860
2861 cityimpr_list_remove(imprs, pcityimpr);
2862
2863 /* Get back the gold upkeep that was already paid this turn. */
2864 pplayer->economic.gold += city_improvement_upkeep(pcityimpr->pcity,
2865 pcityimpr->pimprove);
2866
2867 city_refresh_queue_add(pcityimpr->pcity);
2868
2869 FC_FREE(pcityimpr);
2870
2871 return TRUE;
2872}
2873
2874/**********************************************************************/
2882static void uk_rem_gold_callback(struct unit *punit)
2883{
2884 int gold_upkeep;
2885
2886 /* Remove the unit from uk_rem_gold. */
2887 unit_list_remove(uk_rem_gold, punit);
2888
2889 gold_upkeep = punit->server.upkeep_paid[O_GOLD];
2890
2891 /* All units in uk_rem_gold should have gold upkeep! */
2892 fc_assert_ret_msg(gold_upkeep > 0, "%s has %d gold upkeep",
2893 unit_rule_name(punit), gold_upkeep);
2894
2895 /* Get the upkeep gold back. */
2896 unit_owner(punit)->economic.gold += gold_upkeep;
2897}
2898
2899/**********************************************************************/
2903static void uk_rem_gold_append(struct unit *punit)
2904{
2905 /* Make the unit aware that it is on the uk_rem_gold list. */
2907
2908 /* Add the unit to the list. */
2909 unit_list_append(uk_rem_gold, punit);
2910}
2911
2912/**********************************************************************/
2916static void unit_list_referred_destroy(struct unit_list *punitlist)
2917{
2918 unit_list_iterate(punitlist, punit) {
2919 /* Clear the unit's knowledge of the list. */
2922
2923 /* Destroy the list it self. */
2924 unit_list_destroy(punitlist);
2925}
2926
2927/**********************************************************************/
2939static struct unit *sell_random_unit(struct player *pplayer,
2940 struct unit_list *punitlist)
2941{
2942 struct unit *punit;
2943 int r;
2944 struct unit_list *cargo;
2945
2946 fc_assert_ret_val(pplayer != NULL, NULL);
2947
2948 if (!punitlist || unit_list_size(punitlist) == 0) {
2949 return NULL;
2950 }
2951
2952 r = fc_rand(unit_list_size(punitlist));
2953 punit = unit_list_get(punitlist, r);
2954
2955 cargo = unit_list_new();
2956
2957 /* Check if unit is transporting other units from punitlist,
2958 * and sell one of those (recursively) instead.
2959 * Note that in case of recursive transports we have to iterate
2960 * also through those middle transports that themselves are not in
2961 * punitlist. */
2962 unit_cargo_iterate(punit, pcargo) {
2963 /* Optimization, do not iterate over punitlist
2964 * if we are sure that pcargo is not in it. */
2965 if (pcargo->server.upkeep_paid[O_GOLD] > 0) {
2966 unit_list_iterate(punitlist, p2) {
2967 if (pcargo == p2) {
2968 unit_list_append(cargo, pcargo);
2969 }
2971 }
2973
2974 if (unit_list_size(cargo) > 0) {
2975 /* Recursively sell. Note that cargo list has both
2976 * leaf units and middle transports in case of
2977 * recursive transports. */
2978 struct unit *ret = sell_random_unit(pplayer, cargo);
2979
2980 unit_list_destroy(cargo);
2981
2982 unit_list_remove(punitlist, ret);
2983
2984 return ret;
2985 }
2986
2987 unit_list_destroy(cargo);
2988
2989 {
2990 const char *punit_link = unit_tile_link(punit);
2991#ifdef FREECIV_DEBUG
2992 const char *punit_logname = unit_rule_name(punit);
2993#endif /* FREECIV_DEBUG */
2994 struct tile *utile = unit_tile(punit);
2995
2996 if (upkeep_kill_unit(punit, O_GOLD, ULR_SOLD,
2998 unit_list_remove(punitlist, punit);
2999
3000 /* The gold was paid back when the unit removal made
3001 * uk_rem_gold_callback() run as the unit's removal call back. */
3002
3003 notify_player(pplayer, utile, E_UNIT_LOST_MISC, ftc_server,
3004 _("Not enough gold. %s disbanded."),
3005 punit_link);
3006 log_debug("%s: unit sold (%s)", player_name(pplayer),
3007 punit_logname);
3008 } else {
3009 /* Not able to get rid of punit */
3010 return NULL;
3011 }
3012 }
3013
3014 unit_list_remove(punitlist, punit);
3015
3016 return punit;
3017}
3018
3019/**********************************************************************/
3023 (struct player *pplayer)
3024{
3025 struct cityimpr_list *pimprlist;
3026 bool sell_unit = TRUE;
3027
3028 if (!pplayer) {
3029 return FALSE;
3030 }
3031
3032 pimprlist = cityimpr_list_new();
3033 uk_rem_gold = unit_list_new();
3034
3035 city_list_iterate(pplayer->cities, pcity) {
3036 city_built_iterate(pcity, pimprove) {
3037 if (can_city_sell_building(pcity, pimprove)) {
3038 struct cityimpr *ci = fc_malloc(sizeof(*ci));
3039
3040 ci->pcity = pcity;
3041 ci->pimprove = pimprove;
3042 cityimpr_list_append(pimprlist, ci);
3043 }
3045
3047 if (punit->server.upkeep_paid[O_GOLD] > 0) {
3049 }
3052
3053 while (pplayer->economic.gold < 0
3054 && (cityimpr_list_size(pimprlist) > 0
3055 || unit_list_size(uk_rem_gold) > 0)) {
3056 if ((!sell_unit && cityimpr_list_size(pimprlist) > 0)
3057 || unit_list_size(uk_rem_gold) == 0) {
3058 sell_random_building(pplayer, pimprlist);
3059 } else {
3060 sell_random_unit(pplayer, uk_rem_gold);
3061 }
3062 sell_unit = !sell_unit;
3063 }
3064
3065 /* Free remaining entries from list */
3066 cityimpr_list_iterate(pimprlist, pimpr) {
3067 FC_FREE(pimpr);
3069
3070 if (pplayer->economic.gold < 0) {
3071 /* If we get here it means the player has
3072 * negative gold. This should never happen. */
3073 fc_assert_msg(FALSE, "Player %s (nb %d) cannot have negative gold!",
3074 player_name(pplayer), player_number(pplayer));
3075 }
3076
3077 cityimpr_list_destroy(pimprlist);
3079
3080 return pplayer->economic.gold >= 0;
3081}
3082
3083/**********************************************************************/
3086static bool player_balance_treasury_units(struct player *pplayer)
3087{
3088 if (!pplayer) {
3089 return FALSE;
3090 }
3091
3092 uk_rem_gold = unit_list_new();
3093
3094 city_list_iterate(pplayer->cities, pcity) {
3096 if (punit->server.upkeep_paid[O_GOLD] > 0) {
3098 }
3101
3102 while (pplayer->economic.gold < 0
3103 && sell_random_unit(pplayer, uk_rem_gold)) {
3104 /* all done in sell_random_unit() */
3105 }
3106
3107 if (pplayer->economic.gold < 0) {
3108 /* If we get here it means the player has
3109 * negative gold. This should never happen. */
3110 fc_assert_msg(FALSE, "Player %s (nb %d) cannot have negative gold!",
3111 player_name(pplayer), player_number(pplayer));
3112 }
3113
3115
3116 return pplayer->economic.gold >= 0;
3117}
3118
3119/**********************************************************************/
3123{
3124 struct player *pplayer;
3125 struct cityimpr_list *pimprlist;
3126
3127 if (!pcity) {
3128 return TRUE;
3129 }
3130
3131 pplayer = city_owner(pcity);
3132 pimprlist = cityimpr_list_new();
3133
3134 /* Create a vector of all buildings that can be sold. */
3135 city_built_iterate(pcity, pimprove) {
3136 if (can_city_sell_building(pcity, pimprove)) {
3137 struct cityimpr *ci = fc_malloc(sizeof(*ci));
3138
3139 ci->pcity = pcity;
3140 ci->pimprove = pimprove;
3141 cityimpr_list_append(pimprlist, ci);
3142 }
3144
3145 /* Try to sell some buildings. */
3146 while (pplayer->economic.gold < 0
3147 && sell_random_building(pplayer, pimprlist)) {
3148 /* all done in sell_random_building */
3149 }
3150
3151 /* Free remaining entries from list */
3152 cityimpr_list_iterate(pimprlist, pimpr) {
3153 FC_FREE(pimpr);
3155
3156 cityimpr_list_destroy(pimprlist);
3157
3158 return pplayer->economic.gold >= 0;
3159}
3160
3161/**********************************************************************/
3170{
3171 struct player *pplayer;
3172
3173 if (!pcity) {
3174 return TRUE;
3175 }
3176
3177 pplayer = city_owner(pcity);
3178 uk_rem_gold = unit_list_new();
3179
3180 /* Create a vector of all supported units with gold upkeep. */
3182 if (punit->server.upkeep_paid[O_GOLD] > 0) {
3184 }
3186
3187 /* Still not enough gold, so try "selling" some units. */
3188 while (pplayer->economic.gold < 0
3189 && sell_random_unit(pplayer, uk_rem_gold)) {
3190 /* all done in sell_random_unit() */
3191 }
3192
3193 /* If we get here the player has negative gold, but hopefully
3194 * another city will be able to pay the deficit, so continue. */
3195
3197
3198 return pplayer->economic.gold >= 0;
3199}
3200
3201/**********************************************************************/
3204static bool place_pollution(struct city *pcity, enum extra_cause cause)
3205{
3206 struct tile *ptile;
3207 struct tile *pcenter = city_tile(pcity);
3208 int city_radius_sq = city_map_radius_sq_get(pcity);
3209 int k = 100;
3210 const struct civ_map *nmap = &(wld.map);
3211
3212 while (k > 0) {
3213 /* Place pollution on a random city tile */
3214 int cx, cy;
3215 int tile_id = fc_rand(city_map_tiles(city_radius_sq));
3216 struct extra_type *pextra;
3217
3218 city_tile_index_to_xy(&cx, &cy, tile_id, city_radius_sq);
3219
3220 /* Check for a real map position */
3221 if (!(ptile = city_map_to_tile(nmap, pcenter, city_radius_sq, cx, cy))) {
3222 continue;
3223 }
3224
3225 pextra = rand_extra_for_tile(ptile, cause, FALSE);
3226
3227 if (pextra != NULL && !tile_has_extra(ptile, pextra)) {
3228 tile_add_extra(ptile, pextra);
3229 update_tile_knowledge(ptile);
3230
3231 return TRUE;
3232 }
3233 k--;
3234 }
3235 log_debug("pollution not placed: city: %s", city_name_get(pcity));
3236
3237 return FALSE;
3238}
3239
3240/**********************************************************************/
3243static void check_pollution(struct city *pcity)
3244{
3245 if (fc_rand(100) < pcity->pollution) {
3246 if (place_pollution(pcity, EC_POLLUTION)) {
3247 notify_player(city_owner(pcity), city_tile(pcity), E_POLLUTION, ftc_server,
3248 _("Pollution near %s."), city_link(pcity));
3249 }
3250 }
3251}
3252
3253/**********************************************************************/
3260int city_incite_cost(struct player *pplayer, struct city *pcity)
3261{
3262 int dist, size;
3263 double cost; /* Intermediate values can get very large */
3264
3265 /* Gold factor */
3266 cost = city_owner(pcity)->economic.gold + game.server.base_incite_cost;
3267
3268 unit_list_iterate(pcity->tile->units, punit) {
3272
3273 /* Buildings */
3274 city_built_iterate(pcity, pimprove) {
3275 cost += impr_build_shield_cost(pcity, pimprove)
3278
3279 /* Stability bonuses */
3280 if (!city_unhappy(pcity)) {
3281 cost *= 2;
3282 }
3283 if (city_celebrating(pcity)) {
3284 cost *= 2;
3285 }
3286
3287 /* Buy back is cheap, conquered cities are also cheap */
3289 if (city_owner(pcity) != pcity->original) {
3290 if (pplayer == pcity->original) {
3291 cost /= 2; /* Buy back: 50% price reduction */
3292 } else {
3293 cost = cost * 2 / 3; /* Buy conquered: 33% price reduction */
3294 }
3295 }
3296 }
3297
3298 /* Distance from capital */
3299 /* Max penalty. Applied if there is no capital, or it's even further away. */
3300 dist = 32;
3301 city_list_iterate(city_owner(pcity)->cities, capital) {
3302 if (is_capital(capital)) {
3303 int tmp = map_distance(capital->tile, pcity->tile);
3304
3305 if (tmp < dist) {
3306 dist = tmp;
3307 }
3308 }
3310
3311 size = MAX(1, city_size_get(pcity)
3314 - pcity->feel[CITIZEN_ANGRY][FEELING_FINAL] * 3);
3315 cost *= size;
3317 cost = cost / (dist + 3);
3318
3320 int cost_per_citizen = cost / pcity->size;
3321 int natives = citizens_nation_get(pcity, city_owner(pcity)->slot);
3322 int tgt_cit = citizens_nation_get(pcity, pplayer->slot);
3323 int third_party = pcity->size - natives - tgt_cit;
3324
3325 cost = cost_per_citizen * (natives + 0.7 * third_party + 0.5 * tgt_cit);
3326 }
3327
3328 cost += (cost * get_city_bonus(pcity, EFT_INCITE_COST_PCT)) / 100;
3329 cost /= 100;
3330
3333 } else {
3334 return cost;
3335 }
3336}
3337
3338/**********************************************************************/
3341static void define_orig_production_values(struct city *pcity)
3342{
3343 /* Remember what this city is building last turn, so that on the next turn
3344 * the player can switch production to something else and then change it
3345 * back without penalty. This has to be updated _before_ production for
3346 * this turn is calculated, so that the penalty will apply if the player
3347 * changes production away from what has just been completed. This makes
3348 * sense if you consider what this value means: all the shields in the
3349 * city have been dedicated toward the project that was chosen last turn,
3350 * so the player shouldn't be penalized if the governor has to pick
3351 * something different. See city_change_production_penalty(). */
3352 pcity->changed_from = pcity->production;
3353
3354 log_debug("In %s, building %s. Beg of Turn shields = %d",
3356 pcity->before_change_shields);
3357}
3358
3359/**********************************************************************/
3362static void nullify_caravan_and_disband_plus(struct city *pcity)
3363{
3364 pcity->disbanded_shields=0;
3365 pcity->caravan_shields=0;
3366}
3367
3368/**********************************************************************/
3373{
3375 pcity->before_change_shields=0;
3376}
3377
3378/**********************************************************************/
3381static void update_city_activity(struct city *pcity)
3382{
3383 struct player *pplayer;
3384 struct government *gov;
3385 bool is_happy;
3386 bool is_celebrating;
3387
3388 if (!pcity) {
3389 return;
3390 }
3391
3392 pplayer = city_owner(pcity);
3393 gov = government_of_city(pcity);
3394 is_happy = city_happy(pcity);
3395 is_celebrating = city_celebrating(pcity);
3396
3397 if (city_refresh(pcity)) {
3398 auto_arrange_workers(pcity);
3399 }
3400
3401 /* Reporting of celebrations rewritten, copying the treatment of disorder below,
3402 with the added rapture rounds count. 991219 -- Jing */
3403 if (city_build_stuff(pplayer, pcity)) {
3404 int saved_id;
3405 int revolution_turns;
3406
3407 pcity->history += city_history_gain(pcity);
3408
3409 /* History can decrease, but never go below zero */
3410 pcity->history = MAX(pcity->history, 0);
3411
3412 /* Keep old behaviour when building new improvement could keep
3413 city celebrating */
3414 if (!is_happy) {
3415 is_happy = city_happy(pcity);
3416 }
3417
3418 if (city_celebrating(pcity) || is_celebrating) {
3419 pcity->rapture++;
3420 if (pcity->rapture == 1) {
3421 notify_player(pplayer, city_tile(pcity), E_CITY_LOVE, ftc_server,
3422 _("Celebrations in your honor in %s."),
3423 city_link(pcity));
3424 }
3425 } else {
3426 if (pcity->rapture != 0) {
3427 notify_player(pplayer, city_tile(pcity), E_CITY_NORMAL, ftc_server,
3428 _("Celebrations canceled in %s."),
3429 city_link(pcity));
3430 }
3431 pcity->rapture = 0;
3432 }
3433 pcity->was_happy = is_happy;
3434
3435 /* Handle the illness. */
3436 if (game.info.illness_on) {
3437 /* Recalculate city illness; illness due to trade has to be saved
3438 * within the city struct, as the client does not have all
3439 * the data to calculate it */
3440 pcity->server.illness
3441 = city_illness_calc(pcity, NULL, NULL, &(pcity->illness_trade), NULL);
3442
3443 if (city_illness_check(pcity)) {
3444 if (!city_illness_strike(pcity)) {
3445 /* Illness destroyed the city */
3446 return;
3447 }
3448 }
3449 }
3450
3451 /* City population updated here, after the rapture stuff above. --Jing */
3452 saved_id = pcity->id;
3453 city_populate(pcity, pplayer);
3454 if (NULL == player_city_by_number(pplayer, saved_id)) {
3455 return;
3456 }
3457
3458 pcity->did_sell = FALSE;
3459 pcity->did_buy = FALSE;
3460 pcity->airlift = city_airlift_max(pcity);
3461 update_bulbs(pplayer, pcity->prod[O_SCIENCE], FALSE);
3462
3463 pplayer->economic.infra_points += get_city_bonus(pcity, EFT_INFRA_POINTS);
3464
3465 /* Update the treasury. */
3466 pplayer->economic.gold += pcity->surplus[O_GOLD];
3467
3468 /* FIXME: Nation level upkeep should be paid after ALL cities
3469 * have been processed, not after each individual city. */
3470 if (game.info.gold_upkeep_style != GOLD_UPKEEP_CITY) {
3471 /* Unit upkeep was not included in city balance ->
3472 * not reduced from the city surplus. */
3473 pplayer->economic.gold -= city_total_unit_gold_upkeep(pcity);
3474
3475 if (game.info.gold_upkeep_style == GOLD_UPKEEP_NATION) {
3476 /* Building upkeep was not included in city balance ->
3477 * not reduced from the city surplus. */
3478 pplayer->economic.gold -= city_total_impr_gold_upkeep(pcity);
3479 }
3480 }
3481
3482 /* Remember how much gold upkeep each unit was paid. */
3486
3487 if (pplayer->economic.gold < 0) {
3488 /* Not enough gold - we have to sell some buildings, and if that
3489 * is not enough, disband units with gold upkeep, taking into
3490 * account the setting of 'game.info.gold_upkeep_style':
3491 * GOLD_UPKEEP_CITY: Cities pay for buildings and units.
3492 * GOLD_UPKEEP_MIXED: Cities pay only for buildings; the nation pays
3493 * for units.
3494 * GOLD_UPKEEP_NATION: The nation pays for buildings and units. */
3495 switch (game.info.gold_upkeep_style) {
3496 case GOLD_UPKEEP_CITY:
3497 case GOLD_UPKEEP_MIXED:
3499 && game.info.gold_upkeep_style == GOLD_UPKEEP_CITY) {
3501 }
3502 break;
3503 case GOLD_UPKEEP_NATION:
3504 break;
3505 }
3506 }
3507
3508 revolution_turns = get_city_bonus(pcity, EFT_REVOLUTION_UNHAPPINESS);
3509 if (city_unhappy(pcity)) {
3510 const char *revomsg;
3511
3512 pcity->anarchy++;
3513 if (pcity->anarchy == revolution_turns) {
3514 /* Revolution next turn if not dealt with */
3515 /* TRANS: preserve leading space; this string will be appended to
3516 * another sentence */
3517 revomsg = _(" Unrest threatens to spread beyond the city.");
3518 } else {
3519 revomsg = "";
3520 }
3521 if (pcity->anarchy == 1) {
3522 notify_player(pplayer, city_tile(pcity), E_CITY_DISORDER, ftc_server,
3523 /* TRANS: second %s is an optional extra sentence */
3524 _("Civil disorder in %s.%s"),
3525 city_link(pcity), revomsg);
3526 } else {
3527 notify_player(pplayer, city_tile(pcity), E_CITY_DISORDER, ftc_server,
3528 /* TRANS: second %s is an optional extra sentence */
3529 _("CIVIL DISORDER CONTINUES in %s.%s"),
3530 city_link(pcity), revomsg);
3531 }
3532 } else {
3533 if (pcity->anarchy != 0) {
3534 notify_player(pplayer, city_tile(pcity), E_CITY_NORMAL, ftc_server,
3535 _("Order restored in %s."),
3536 city_link(pcity));
3537 }
3538 pcity->anarchy = 0;
3539 }
3540 check_pollution(pcity);
3541
3542 send_city_info(NULL, pcity);
3543
3544 if (revolution_turns > 0 && pcity->anarchy > revolution_turns) {
3545 notify_player(pplayer, city_tile(pcity), E_ANARCHY, ftc_server,
3546 /* TRANS: %s - government form, e.g., Democracy */
3547 _("The people have overthrown your %s, "
3548 "your country is in turmoil."),
3551 }
3552 if (city_refresh(pcity)) {
3553 auto_arrange_workers(pcity);
3554 }
3555 sanity_check_city(pcity);
3556 }
3557}
3558
3559/**********************************************************************/
3562static bool city_illness_check(const struct city * pcity)
3563{
3564 if (fc_rand(1000) < pcity->server.illness) {
3565 return TRUE;
3566 }
3567
3568 return FALSE;
3569}
3570
3571/**********************************************************************/
3575static bool disband_city(struct city *pcity)
3576{
3577 struct player *pplayer = city_owner(pcity);
3578 struct tile *ptile = pcity->tile;
3579 struct city *rcity=NULL;
3580 const struct unit_type *utype = pcity->production.value.utype;
3581 struct unit *punit;
3582 int saved_id = pcity->id;
3583
3584 /* find closest city other than pcity */
3585 rcity = find_closest_city(ptile, pcity, pplayer, FALSE, FALSE, FALSE, TRUE,
3586 FALSE, NULL);
3587
3588 if (!rcity) {
3589 /* What should we do when we try to disband our only city? */
3590 notify_player(pplayer, ptile, E_CITY_CANTBUILD, ftc_server,
3591 _("%s can't build %s yet, "
3592 "as we can't disband our only city."),
3594 script_server_signal_emit("unit_cant_be_built", utype, pcity,
3595 "pop_cost");
3596 if (!city_exist(saved_id)) {
3597 /* Script decided to remove even the last city */
3598 return TRUE;
3599 } else {
3600 return FALSE;
3601 }
3602 }
3603
3604 punit = city_create_unit(pcity, utype, NULL);
3605
3606 /* "unit_built" script handler may have destroyed city. If so, we
3607 * assume something sensible happened to its units, and that the
3608 * script took care of announcing unit creation if required. */
3609 if (city_exist(saved_id)) {
3610 /* Shift all the units supported by pcity (including the new unit)
3611 * to rcity. transfer_city_units does not make sure no units are
3612 * left floating without a transport, but since all units are
3613 * transferred this is not a problem. */
3614 transfer_city_units(pplayer, pplayer, pcity->units_supported, rcity,
3615 pcity, -1, TRUE);
3616
3617 if (punit) {
3618 notify_player(pplayer, ptile, E_UNIT_BUILT, ftc_server,
3619 /* TRANS: "<city> is disbanded into Settler." */
3620 _("%s is disbanded into %s."),
3622 }
3623
3624 script_server_signal_emit("city_destroyed", pcity, pcity->owner, NULL);
3625
3626 if (!city_exist(saved_id)) {
3627 /* Already removed during the script */
3628 return TRUE;
3629 }
3630 remove_city(pcity);
3631
3632 /* Since we've removed the city, we don't need to worry about
3633 * charging for production, disabling rally points, etc. */
3634 }
3635
3636 return TRUE;
3637}
3638
3639/**********************************************************************/
3687static float city_migration_score(struct city *pcity)
3688{
3689 float score = 0.0;
3690 int build_shield_cost = 0;
3691 bool has_wonder = FALSE;
3692
3693 if (!pcity) {
3694 return score;
3695 }
3696
3697 if (pcity->server.mgr_score_calc_turn == game.info.turn) {
3698 /* up-to-date migration score */
3699 return pcity->server.migration_score;
3700 }
3701
3702 /* feeling of the citizens */
3703 score = (city_size_get(pcity)
3704 + 1.00 * pcity->feel[CITIZEN_HAPPY][FEELING_FINAL]
3705 + 0.00 * pcity->feel[CITIZEN_CONTENT][FEELING_FINAL]
3706 - 0.25 * pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL]
3707 - 0.50 * pcity->feel[CITIZEN_ANGRY][FEELING_FINAL]);
3708
3709 /* calculate shield build cost for all buildings */
3710 city_built_iterate(pcity, pimprove) {
3711 build_shield_cost += impr_build_shield_cost(pcity, pimprove);
3712 if (is_wonder(pimprove)) {
3713 /* this city has a wonder */
3714 has_wonder = TRUE;
3715 }
3717
3718 /* take shield costs of all buidings into account; normalized by 1000 */
3719 score *= (1 + (1 - exp(- (float) MAX(0, build_shield_cost) / 1000)) / 5);
3720 /* take trade into account; normalized by 100 */
3721 score *= (1 + (1 - exp(- (float) MAX(0, pcity->surplus[O_TRADE]) / 100))
3722 / 5);
3723 /* take luxury into account; normalized by 100 */
3724 score *= (1 + (1 - exp(- (float) MAX(0, pcity->surplus[O_LUXURY]) / 100))
3725 / 5);
3726 /* take science into account; normalized by 100 */
3727 score *= (1 + (1 - exp(- (float) MAX(0, pcity->surplus[O_SCIENCE]) / 100))
3728 / 5);
3729
3730 score += city_culture(pcity) * game.info.culture_migration_pml / 1000;
3731
3732 /* Take food into account; the food surplus is clipped to values between
3733 * -10..20 and normalize by 10. Thus, the factor is between 0.9 and 1.2. */
3734 score *= (1 + (float) CLIP(-10, pcity->surplus[O_FOOD], 20) / 10 );
3735
3736 /* Reduce the score due to city illness (plague). The illness is given in
3737 * tenth of percent (0..1000) and normalized by 25. Thus, this factor is
3738 * between 0.6 (ill city) and 1.0 (health city). */
3739 score *= (100 - (float)city_illness_calc(pcity, NULL, NULL, NULL, NULL)
3740 / 25);
3741
3742 if (has_wonder) {
3743 /* people like wonders */
3744 score *= 1.25;
3745 }
3746
3747 if (is_capital(pcity)) {
3748 /* the capital is a magnet for the citizens */
3749 score *= 1.25;
3750 }
3751
3752 /* take into account effects */
3753 score *= (1.0 + get_city_bonus(pcity, EFT_MIGRATION_PCT) / 100.0);
3754
3755 log_debug("[M] %s score: %.3f", city_name_get(pcity), score);
3756
3757 /* set migration score for the city */
3758 pcity->server.migration_score = score;
3759 /* set the turn, when the score was calculated */
3761
3762 return score;
3763}
3764
3765/**********************************************************************/
3771static bool do_city_migration(struct city *pcity_from,
3772 struct city *pcity_to)
3773{
3774 struct player *pplayer_from, *pplayer_to, *pplayer_citizen;
3775 struct tile *ptile_from, *ptile_to;
3776 char name_from[MAX_LEN_LINK], name_to[MAX_LEN_LINK];
3777 const char *nation_from, *nation_to;
3778 struct city *rcity = NULL;
3779 int to_id = pcity_to->id;
3780 const struct civ_map *nmap = &(wld.map);
3781
3782 if (!pcity_from || !pcity_to) {
3783 return FALSE;
3784 }
3785
3786 pplayer_from = city_owner(pcity_from);
3787 pplayer_citizen = pplayer_from;
3788 pplayer_to = city_owner(pcity_to);
3789 /* We copy that, because city_link always returns the same pointer. */
3790 sz_strlcpy(name_from, city_link(pcity_from));
3791 sz_strlcpy(name_to, city_link(pcity_to));
3792 nation_from = nation_adjective_for_player(pplayer_from);
3793 nation_to = nation_adjective_for_player(pplayer_to);
3794 ptile_from = city_tile(pcity_from);
3795 ptile_to = city_tile(pcity_to);
3796
3797 /* Check food supply in the receiver city */
3799 bool migration = FALSE;
3800
3801 if (pcity_to->surplus[O_FOOD] >= game.info.food_cost) {
3802 migration = TRUE;
3803 } else {
3804 /* Check if there is a free tile for the new citizen which, when worked,
3805 * leads to zero or positive food surplus for the enlarged city */
3806 int max_food_tile = -1; /* no free tile */
3807
3809 city_tile(pcity_to), ptile) {
3810 if (city_can_work_tile(pcity_to, ptile)
3811 && tile_worked(ptile) != pcity_to) {
3812 /* Safest assumption is that city won't be celebrating once an
3813 * additional citizen is added */
3814 max_food_tile = MAX(max_food_tile,
3815 city_tile_output(pcity_to, ptile, FALSE, O_FOOD));
3816 }
3818 if (max_food_tile >= 0
3819 && pcity_to->surplus[O_FOOD] + max_food_tile >= game.info.food_cost) {
3820 migration = TRUE;
3821 }
3822 }
3823
3824 if (!migration) {
3825 /* insufficiency food in receiver city; no additional citizens */
3826 if (pplayer_from == pplayer_to) {
3827 /* migration between one nation */
3828 notify_player(pplayer_to, ptile_to, E_CITY_TRANSFER, ftc_server,
3829 /* TRANS: From <city1> to <city2>. */
3830 _("Migrants from %s can't go to %s because there is "
3831 "not enough food available!"),
3832 name_from, name_to);
3833 } else {
3834 /* migration between different nations */
3835 notify_player(pplayer_from, ptile_to, E_CITY_TRANSFER, ftc_server,
3836 /* TRANS: From <city1> to <city2> (<city2 nation adjective>). */
3837 _("Migrants from %s can't go to %s (%s) because there "
3838 "is not enough food available!"),
3839 name_from, name_to, nation_to);
3840 notify_player(pplayer_to, ptile_to, E_CITY_TRANSFER, ftc_server,
3841 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
3842 _("Migrants from %s (%s) can't go to %s because there "
3843 "is not enough food available!"),
3844 name_from, nation_from, name_to);
3845 }
3846
3847 return FALSE;
3848 }
3849 }
3850
3851 if (!city_can_grow_to(pcity_to, city_size_get(pcity_to) + 1)) {
3852 /* receiver city can't grow */
3853 if (pplayer_from == pplayer_to) {
3854 /* migration between one nation */
3855 notify_player(pplayer_to, ptile_to, E_CITY_TRANSFER, ftc_server,
3856 /* TRANS: From <city1> to <city2>. */
3857 _("Migrants from %s can't go to %s because it needs "
3858 "an improvement to grow!"),
3859 name_from, name_to);
3860 } else {
3861 /* migration between different nations */
3862 notify_player(pplayer_from, ptile_to, E_CITY_TRANSFER, ftc_server,
3863 /* TRANS: From <city1> to <city2> of <city2 nation adjective>. */
3864 _("Migrants from %s can't go to %s (%s) because it "
3865 "needs an improvement to grow!"),
3866 name_from, name_to, nation_to);
3867 notify_player(pplayer_to, ptile_to, E_CITY_TRANSFER, ftc_server,
3868 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
3869 _("Migrants from %s (%s) can't go to %s because it "
3870 "needs an improvement to grow!"),
3871 name_from, nation_from, name_to);
3872 }
3873
3874 return FALSE;
3875 }
3876
3877 /* reduce size of giver */
3878 if (city_size_get(pcity_from) == 1) {
3879
3881 /* Preserve nationality of city's only citizen */
3882 pplayer_citizen = player_slot_get_player(citizens_random(pcity_from));
3883 }
3884
3885 /* do not destroy wonders */
3886 city_built_iterate(pcity_from, pimprove) {
3887 if (is_wonder(pimprove)) {
3888 return FALSE;
3889 }
3891
3892 /* find closest city other of the same player than pcity_from */
3893 rcity = find_closest_city(ptile_from, pcity_from, pplayer_from, FALSE,
3894 FALSE, FALSE, TRUE, FALSE, NULL);
3895
3896 if (rcity) {
3897 int id = pcity_from->id;
3898
3899 /* transfer all units to the closest city */
3900 transfer_city_units(pplayer_from, pplayer_from,
3901 pcity_from->units_supported, rcity, pcity_from,
3902 -1, TRUE);
3903 sz_strlcpy(name_from, city_tile_link(pcity_from));
3904
3905 script_server_signal_emit("city_size_change", pcity_from,
3906 (lua_Integer)(-1), "migration_from");
3907
3908 if (city_exist(id)) {
3909 script_server_signal_emit("city_destroyed", pcity_from,
3910 pcity_from->owner, NULL);
3911
3912 if (city_exist(id)) {
3913 remove_city(pcity_from);
3914 }
3915 }
3916
3917 notify_player(pplayer_from, ptile_from, E_CITY_LOST, ftc_server,
3918 _("%s was disbanded by its citizens."),
3919 name_from);
3920 } else {
3921 /* it's the only city of the nation */
3922 return FALSE;
3923 }
3924 } else {
3925 /* the migrants take half of the food box with them (this prevents
3926 * migration -> grow -> migration -> ... cycles) */
3927 pcity_from->food_stock /= 2;
3928
3930 /* Those citizens that are from the target nation are most
3931 * ones migrating. */
3932 if (citizens_nation_get(pcity_from, pplayer_to->slot) > 0) {
3933 pplayer_citizen = pplayer_to;
3934 } else if (!citizens_nation_get(pcity_from, pplayer_citizen->slot)) {
3935 /* No native citizens at all in the city, choose random foreigner */
3936 struct player_slot *cit_slot = citizens_random(pcity_from);
3937
3938 pplayer_citizen = player_slot_get_player(cit_slot);
3939 }
3940 /* This should be followed by city_reduce_size(). */
3941 citizens_nation_add(pcity_from, pplayer_citizen->slot, -1);
3942 }
3943 city_reduce_size(pcity_from, 1, pplayer_from, "migration_from");
3944 city_refresh_vision(pcity_from);
3945 if (city_refresh(pcity_from)) {
3946 auto_arrange_workers(pcity_from);
3947 }
3948 }
3949
3950 /* This should be _before_ the size of the city is increased. Thus, the
3951 * order of the messages is correct (1: migration; 2: increased size). */
3952 if (pplayer_from == pplayer_to) {
3953 /* migration between one nation */
3954 notify_player(pplayer_from, ptile_to, E_CITY_TRANSFER, ftc_server,
3955 /* TRANS: From <city1> to <city2>. */
3956 _("Migrants from %s moved to %s in search of a better "
3957 "life."), name_from, name_to);
3958 } else {
3959 /* migration between different nations */
3960 notify_player(pplayer_from, ptile_to, E_CITY_TRANSFER, ftc_server,
3961 /* TRANS: From <city1> to <city2> (<city2 nation adjective>). */
3962 _("Migrants from %s moved to %s (%s) in search of a "
3963 "better life."),
3964 name_from, name_to, nation_to);
3965 notify_player(pplayer_to, ptile_to, E_CITY_TRANSFER, ftc_server,
3966 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
3967 _("Migrants from %s (%s) moved to %s in search of a "
3968 "better life."),
3969 name_from, nation_from, name_to);
3970 }
3971
3972 /* Increase size of receiver city */
3973 if (city_exist(to_id)) {
3974 bool incr_success = city_increase_size(pcity_to);
3975
3976 if (city_exist(to_id)) {
3977 city_refresh_after_city_size_increase(pcity_to, pplayer_citizen);
3978 city_refresh_vision(pcity_to);
3979 if (city_refresh(pcity_to)) {
3980 auto_arrange_workers(pcity_to);
3981 }
3982 if (incr_success) {
3983 script_server_signal_emit("city_size_change", pcity_to,
3984 (lua_Integer)1, "migration_to");
3985 }
3986 }
3987 }
3988
3989 log_debug("[M] T%d migration successful (%s -> %s)",
3990 game.info.turn, name_from, name_to);
3991
3992 return TRUE;
3993}
3994
3995/**********************************************************************/
4017{
4018 bool internat = FALSE;
4019
4020 if (!game.server.migration) {
4021 return FALSE;
4022 }
4023
4025 || (game.server.mgr_worldchance <= 0
4026 && game.server.mgr_nationchance <= 0)) {
4027 return FALSE;
4028 }
4029
4030 /* check for migration */
4031 players_iterate(pplayer) {
4032 if (!pplayer->cities) {
4033 continue;
4034 }
4035
4036 if (check_city_migrations_player(pplayer)) {
4037 internat = TRUE;
4038 }
4040
4041 return internat;
4042}
4043
4044/**********************************************************************/
4048bool city_empty_food_stock(struct city *pcity) {
4049 struct player *pplayer = city_owner(pcity);
4050 struct tile *ptile = city_tile(pcity);
4051
4052 fc_assert_ret_val(pcity != NULL, FALSE);
4053
4054 if (pcity->food_stock > 0) {
4055 pcity->food_stock = 0;
4056
4057 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4058 /* TRANS: %s is a city name */
4059 _("All stored food destroyed in %s."), city_link(pcity));
4060
4061 return TRUE;
4062 }
4063
4064 return FALSE;
4065}
4066
4067/**********************************************************************/
4070static void apply_disaster(struct city *pcity, struct disaster_type *pdis)
4071{
4072 struct player *pplayer = city_owner(pcity);
4073 struct tile *ptile = city_tile(pcity);
4074 bool had_internal_effect = FALSE;
4075
4076 log_debug("%s at %s", disaster_rule_name(pdis), city_name_get(pcity));
4077
4078 notify_player(pplayer, ptile, E_DISASTER,
4079 ftc_server,
4080 /* TRANS: Disasters such as Earthquake */
4081 _("%s was hit by %s."), city_name_get(pcity),
4083
4084 if (disaster_has_effect(pdis, DE_POLLUTION)) {
4085 if (place_pollution(pcity, EC_POLLUTION)) {
4086 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4087 _("Pollution near %s."), city_link(pcity));
4088 had_internal_effect = TRUE;
4089 }
4090 }
4091
4092 if (disaster_has_effect(pdis, DE_FALLOUT)) {
4093 if (place_pollution(pcity, EC_FALLOUT)) {
4094 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4095 _("Fallout near %s."), city_link(pcity));
4096 had_internal_effect = TRUE;
4097 }
4098 }
4099
4100 if (disaster_has_effect(pdis, DE_REDUCE_DESTROY)
4101 || (disaster_has_effect(pdis, DE_REDUCE_POP)
4102 && pcity->size > 1)) {
4103 if (!city_reduce_size(pcity, 1, NULL, "disaster")) {
4104 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4105 /* TRANS: "Industrial Accident destroys Bogota entirely." */
4106 _("%s destroys %s entirely."),
4107 disaster_name_translation(pdis), city_link(pcity));
4108 pcity = NULL;
4109 } else {
4110 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4111 /* TRANS: "Nuclear Accident ... Montreal." */
4112 _("%s causes population loss in %s."),
4113 disaster_name_translation(pdis), city_link(pcity));
4114 }
4115
4116 had_internal_effect = TRUE;
4117 }
4118
4119 if (pcity && disaster_has_effect(pdis, DE_DESTROY_BUILDING)) {
4120 int total = 0;
4121 struct impr_type *imprs[B_LAST];
4122
4123 city_built_iterate(pcity, pimprove) {
4124 if (is_improvement(pimprove)
4125 && !improvement_has_flag(pimprove, IF_DISASTER_PROOF)) {
4126 imprs[total++] = pimprove;
4127 }
4129
4130 if (total > 0) {
4131 int num = fc_rand(total);
4132
4133 building_lost(pcity, imprs[num], "disaster", NULL);
4134
4135 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4136 /* TRANS: second %s is the name of a city improvement */
4137 _("%s destroys %s in %s."),
4139 improvement_name_translation(imprs[num]),
4140 city_link(pcity));
4141
4142 had_internal_effect = TRUE;
4143 }
4144 }
4145
4146 if (pcity && disaster_has_effect(pdis, DE_EMPTY_FOODSTOCK)) {
4147 if (city_empty_food_stock(pcity)) {
4148 had_internal_effect = TRUE;
4149 }
4150 }
4151
4152 if (pcity && disaster_has_effect(pdis, DE_EMPTY_PRODSTOCK)) {
4153 if (pcity->shield_stock > 0) {
4154 char prod[256];
4155
4156 pcity->shield_stock = 0;
4157 nullify_prechange_production(pcity); /* Make it impossible to recover */
4158
4159 universal_name_translation(&pcity->production, prod, sizeof(prod));
4160 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
4161 /* TRANS: "Production of Colossus in Rhodes destroyed." */
4162 _("Production of %s in %s destroyed."),
4163 prod, city_link(pcity));
4164
4165 had_internal_effect = TRUE;
4166 }
4167 }
4168
4169 script_server_signal_emit("disaster_occurred", pdis, pcity,
4170 had_internal_effect);
4171 script_server_signal_emit("disaster", pdis, pcity);
4172}
4173
4174/**********************************************************************/
4178{
4179 if (game.info.disasters == 0) {
4180 /* Shortcut out as no disaster is possible. */
4181 return;
4182 }
4183
4184 players_iterate(pplayer) {
4185 /* Safe city iterator needed as disaster may destroy city */
4186 city_list_iterate_safe(pplayer->cities, pcity) {
4187 int id = pcity->id;
4188
4189 disaster_type_iterate(pdis) {
4190 if (city_exist(id)) {
4191 /* City survived earlier disasters. */
4192 int probability = game.info.disasters * pdis->frequency;
4193 int result = fc_rand(DISASTER_BASE_RARITY);
4194
4195 if (result < probability) {
4196 if (can_disaster_happen(pdis, pcity)) {
4197 apply_disaster(pcity, pdis);
4198 }
4199 }
4200 }
4204}
4205
4206/**********************************************************************/
4214static bool check_city_migrations_player(const struct player *pplayer)
4215{
4216 char city_link_text[MAX_LEN_LINK];
4217 float best_city_player_score, best_city_world_score;
4218 struct city *best_city_player, *best_city_world, *acity;
4219 float score_from, score_tmp, weight;
4220 int dist, mgr_dist;
4221 bool internat = FALSE;
4222
4223 /* check for each city
4224 * city_list_iterate_safe_end must be used because we could
4225 * remove one city from the list */
4226 city_list_iterate_safe(pplayer->cities, pcity) {
4227 /* no migration out of the capital */
4228 if (is_capital(pcity)) {
4229 continue;
4230 }
4231
4232 /* check only each (game.server.mgr_turninterval) turn
4233 * (counted from the funding turn) and do not migrate
4234 * the same turn a city is founded */
4235 if (game.info.turn == pcity->turn_founded
4236 || ((game.info.turn - pcity->turn_founded)
4237 % game.server.mgr_turninterval) != 0) {
4238 continue;
4239 }
4240
4241 best_city_player_score = 0.0;
4242 best_city_world_score = 0.0;
4243 best_city_player = NULL;
4244 best_city_world = NULL;
4245
4246 /* score of the actual city
4247 * taking into account a persistence factor of 3 */
4248 score_from = city_migration_score(pcity) * 3;
4249
4250 log_debug("[M] T%d check city: %s score: %6.3f (%s)",
4251 game.info.turn, city_name_get(pcity), score_from,
4252 player_name(pplayer));
4253
4254 /* consider all cities within the maximal possible distance
4255 * (= CITY_MAP_MAX_RADIUS + GAME_MAX_MGR_DISTANCE) */
4256 iterate_outward(&(wld.map), city_tile(pcity),
4258 acity = tile_city(ptile);
4259
4260 if (!acity || acity == pcity) {
4261 /* no city or the city in the center */
4262 continue;
4263 }
4264
4265 /* Calculate the migration distance. The value of
4266 * game.server.mgr_distance is added to the current city radius. If the
4267 * distance between both cities is lower or equal than this value,
4268 * migration is possible. */
4269 mgr_dist = (int)sqrt((double)MAX(city_map_radius_sq_get(acity),0))
4271
4272 /* distance between the two cities */
4273 dist = real_map_distance(city_tile(pcity), city_tile(acity));
4274
4275 if (dist > mgr_dist) {
4276 /* to far away */
4277 continue;
4278 }
4279
4280 /* score of the second city, weighted by the distance */
4281 weight = ((float) (mgr_dist + 1 - dist) / (float) (mgr_dist + 1));
4282 score_tmp = city_migration_score(acity) * weight;
4283
4284 log_debug("[M] T%d - compare city: %s (%s) dist: %d mgr_dist: %d "
4285 "score: %6.3f", game.info.turn, city_name_get(acity),
4286 player_name(city_owner(acity)), dist, mgr_dist, score_tmp);
4287
4288 if (game.server.mgr_nationchance > 0 && city_owner(acity) == pplayer) {
4289 /* migration between cities of the same owner */
4290 if (score_tmp > score_from && score_tmp > best_city_player_score) {
4291 /* select the best! */
4292 best_city_player_score = score_tmp;
4293 best_city_player = acity;
4294
4295 log_debug("[M] T%d - best city (player): %s (%s) score: "
4296 "%6.3f (> %6.3f)", game.info.turn,
4297 city_name_get(best_city_player), player_name(pplayer),
4298 best_city_player_score, score_from);
4299 }
4300 } else if (game.server.mgr_worldchance > 0
4301 && city_owner(acity) != pplayer) {
4302 /* migration between cities of different owners */
4304 /* Modify the score if citizens could migrate to a city of their
4305 * original nation. */
4306 if (citizens_nation_get(pcity, city_owner(acity)->slot) > 0) {
4307 score_tmp *= 2;
4308 }
4309 }
4310
4311 if (score_tmp > score_from && score_tmp > best_city_world_score) {
4312 /* select the best! */
4313 best_city_world_score = score_tmp;
4314 best_city_world = acity;
4315
4316 log_debug("[M] T%d - best city (world): %s (%s) score: "
4317 "%6.3f (> %6.3f)", game.info.turn,
4318 city_name_get(best_city_world),
4319 player_name(city_owner(best_city_world)),
4320 best_city_world_score, score_from);
4321 }
4322 }
4324
4325 if (best_city_player != NULL) {
4326 /* First, do the migration within one nation */
4327 if (fc_rand(100) >= game.server.mgr_nationchance) {
4328 /* No migration */
4329 /* N.B.: city_link always returns the same pointer. */
4330 sz_strlcpy(city_link_text, city_link(pcity));
4331 notify_player(pplayer, city_tile(pcity), E_CITY_TRANSFER, ftc_server,
4332 _("Citizens of %s are thinking about migrating to %s "
4333 "for a better life."),
4334 city_link_text, city_link(best_city_player));
4335 } else {
4336 do_city_migration(pcity, best_city_player);
4337 }
4338
4339 /* Stop here */
4340 continue;
4341 }
4342
4343 if (best_city_world != NULL) {
4344 /* Second, do the migration between all nations */
4345 if (fc_rand(100) >= game.server.mgr_worldchance) {
4346 const char *nname;
4347
4348 nname = nation_adjective_for_player(city_owner(best_city_world));
4349 /* No migration */
4350 /* N.B.: city_link always returns the same pointer. */
4351 sz_strlcpy(city_link_text, city_link(pcity));
4352 notify_player(pplayer, city_tile(pcity), E_CITY_TRANSFER, ftc_server,
4353 /* TRANS: <city1> to <city2> (<city2 nation adjective>). */
4354 _("Citizens of %s are thinking about migrating to %s "
4355 "(%s) for a better life."),
4356 city_link_text, city_link(best_city_world), nname);
4357 } else {
4358 do_city_migration(pcity, best_city_world);
4359 internat = TRUE;
4360 }
4361
4362 /* Stop here */
4363 continue;
4364 }
4366
4367 return internat;
4368}
4369
4370/**********************************************************************/
4373void city_style_refresh(struct city *pcity)
4374{
4375 pcity->style = city_style(pcity);
4376}
4377
4378/**********************************************************************/
4381void city_tc_effect_refresh(struct player *pplayer)
4382{
4383 const struct civ_map *nmap = &(wld.map);
4384
4385 city_list_iterate(pplayer->cities, pcity) {
4386 bool changed = FALSE;
4387
4389 city_tile(pcity), ptile, idx, x, y) {
4390 if (ptile->worked == pcity
4391 && get_city_tile_output_bonus(pcity, ptile, NULL, EFT_TILE_WORKABLE) <= 0) {
4392 city_map_update_empty(pcity, ptile);
4393 pcity->specialists[DEFAULT_SPECIALIST]++;
4394 changed = TRUE;
4395 }
4397
4398 if (changed) {
4399 auto_arrange_workers(pcity);
4400 send_city_info(NULL, pcity);
4401 }
4403}
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:658
bool can_city_build_unit_later(const struct city *pcity, const struct unit_type *punittype)
Definition city.c:946
int city_granary_size(int city_size)
Definition city.c:2101
bool can_city_build_now(const struct city *pcity, const struct universal *target)
Definition city.c:989
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:300
bool city_has_building(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:1212
bool is_capital(const struct city *pcity)
Definition city.c:1548
const char * city_name_get(const struct city *pcity)
Definition city.c:1111
void city_remove_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3280
int city_improvement_upkeep(const struct city *pcity, const struct impr_type *b)
Definition city.c:1227
int city_airlift_max(const struct city *pcity)
Definition city.c:2841
struct output_type * get_output_type(Output_type_id output)
Definition city.c:633
bool is_city_option_set(const struct city *pcity, enum city_options option)
Definition city.c:3297
void city_size_add(struct city *pcity, int add)
Definition city.c:1138
bool city_production_has_flag(const struct city *pcity, enum impr_flag_id flag)
Definition city.c:712
void city_refresh_from_main_map(struct city *pcity, bool *workers_map)
Definition city.c:3065
int city_production_unit_veteran_level(struct city *pcity, const struct unit_type *punittype)
Definition city.c:789
bool can_city_build_improvement_now(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:839
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:96
bool city_rapture_grow(const struct city *pcity)
Definition city.c:1622
int city_production_turns_to_build(const struct city *pcity, bool include_shield_stock)
Definition city.c:805
bool city_unhappy(const struct city *pcity)
Definition city.c:1595
Specialist_type_id best_specialist(Output_type_id otype, const struct city *pcity)
Definition city.c:3243
struct city * city_list_find_number(struct city_list *This, int id)
Definition city.c:1648
bool city_celebrating(const struct city *pcity)
Definition city.c:1614
bool can_city_build_improvement_direct(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:815
int city_illness_calc(const struct city *pcity, int *ill_base, int *ill_size, int *ill_trade, int *ill_pollution)
Definition city.c:2769
bool city_can_grow_to(const struct city *pcity, int pop_size)
Definition city.c:1981
bool city_happy(const struct city *pcity)
Definition city.c:1583
void city_add_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3266
int city_map_radius_sq_get(const struct city *pcity)
Definition city.c:132
static int cmp(int v1, int v2)
Definition city.c:320
int city_tile_output(const struct city *pcity, const struct tile *ptile, bool is_celebrating, Output_type_id otype)
Definition city.c:1255
int city_map_tiles(int city_radius_sq)
Definition city.c:166
bool can_city_build_unit_direct(const struct city *pcity, const struct unit_type *punittype)
Definition city.c:888
bool city_exist(int id)
Definition city.c:3467
bool city_can_work_tile(const struct city *pcity, const struct tile *ptile)
Definition city.c:1425
bool city_production_build_units(const struct city *pcity, bool add_production, int *num_units)
Definition city.c:732
void city_rally_point_clear(struct city *pcity)
Definition city.c:3521
bool can_city_build_improvement_later(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:856
bool city_had_recent_plague(const struct city *pcity)
Definition city.c:2822
bool city_can_change_build(const struct city *pcity)
Definition city.c:1053
int city_total_unit_gold_upkeep(const struct city *pcity)
Definition city.c:1194
int city_total_impr_gold_upkeep(const struct city *pcity)
Definition city.c:1175
bool can_city_build_unit_now(const struct city *pcity, const struct unit_type *punittype)
Definition city.c:928
#define cities_iterate_end
Definition city.h:497
#define city_list_iterate_safe(citylist, _city)
Definition city.h:502
@ CNA_BROADCAST_PENDING
Definition city.h:300
@ CNA_NOT
Definition city.h:298
@ CNA_NORMAL
Definition city.h:299
#define city_list_iterate(citylist, pcity)
Definition city.h:488
#define city_tile(_pcity_)
Definition city.h:544
#define cities_iterate(pcity)
Definition city.h:492
static citizens city_size_get(const struct city *pcity)
Definition city.h:549
#define city_tile_iterate_skip_free_worked(_nmap, _radius_sq, _city_tile, _tile, _index, _x, _y)
Definition city.h:206
@ CITIZEN_ANGRY
Definition city.h:263
@ CITIZEN_HAPPY
Definition city.h:260
@ CITIZEN_CONTENT
Definition city.h:261
@ CITIZEN_UNHAPPY
Definition city.h:262
#define output_type_iterate(output)
Definition city.h:812
#define INCITE_IMPOSSIBLE_COST
Definition city.h:88
#define city_owner(_pcity_)
Definition city.h:543
#define city_tile_iterate_skip_free_worked_end
Definition city.h:214
#define city_list_iterate_end
Definition city.h:490
#define city_tile_iterate(_nmap, _radius_sq, _city_tile, _tile)
Definition city.h:222
#define city_list_iterate_safe_end
Definition city.h:524
@ FEELING_FINAL
Definition city.h:276
#define city_tile_iterate_end
Definition city.h:230
#define CITY_MAP_MAX_RADIUS
Definition city.h:71
#define city_built_iterate(_pcity, _p)
Definition city.h:801
#define city_built_iterate_end
Definition city.h:807
#define output_type_iterate_end
Definition city.h:818
void city_map_update_empty(struct city *pcity, struct tile *ptile)
Definition citytools.c:3157
void send_city_info(struct player *dest, struct city *pcity)
Definition citytools.c:2320
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:849
void sync_cities(void)
Definition citytools.c:3231
static void void city_freeze_workers(struct city *pcity)
Definition citytools.c:135
bool city_map_update_radius_sq(struct city *pcity)
Definition citytools.c:3356
void change_build_target(struct player *pplayer, struct city *pcity, struct universal *target, enum event_type event)
Definition citytools.c:3068
bool city_illness_strike(struct city *pcity)
Definition citytools.c:2877
void city_thaw_workers(struct city *pcity)
Definition citytools.c:145
void building_lost(struct city *pcity, const struct impr_type *pimprove, const char *reason, struct unit *destroyer)
Definition citytools.c:2970
void city_units_upkeep(const struct city *pcity)
Definition citytools.c:3019
void remove_city(struct city *pcity)
Definition citytools.c:1677
void do_sell_building(struct player *pplayer, struct city *pcity, struct impr_type *pimprove, const char *reason)
Definition citytools.c:2903
struct trade_route * remove_trade_route(struct city *pc1, struct trade_route *proute, bool announce, bool source_gone)
Definition citytools.c:2842
void broadcast_city_info(struct city *pcity)
Definition citytools.c:2210
void city_map_update_all(struct city *pcity)
Definition citytools.c:3250
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:714
void city_map_update_worker(struct city *pcity, struct tile *ptile)
Definition citytools.c:3171
void city_refresh_vision(struct city *pcity)
Definition citytools.c:3331
#define LOG_BUILD_TARGET
Definition citytools.h:21
static bool city_illness_check(const struct city *pcity)
Definition cityturn.c:3562
static void uk_rem_gold_append(struct unit *punit)
Definition cityturn.c:2903
static bool worklist_change_build_target(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2037
void remove_obsolete_buildings(struct player *pplayer)
Definition cityturn.c:272
static bool city_build_stuff(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2804
static void city_reset_foodbox(struct city *pcity, int new_size)
Definition cityturn.c:936
static void apply_disaster(struct city *pcity, struct disaster_type *pdis)
Definition cityturn.c:4070
static bool city_distribute_surplus_shields(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2363
int city_granary_savings(const struct city *pcity)
Definition cityturn.c:923
static void unit_list_referred_destroy(struct unit_list *punitlist)
Definition cityturn.c:2916
static bool place_pollution(struct city *pcity, enum extra_cause cause)
Definition cityturn.c:3204
void choose_build_target(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2230
static void uk_rem_gold_callback(struct unit *punit)
Definition cityturn.c:2882
void auto_arrange_workers(struct city *pcity)
Definition cityturn.c:368
void city_refresh_queue_add(struct city *pcity)
Definition cityturn.c:199
static struct unit_list * uk_rem_gold
Definition cityturn.c:93
static bool city_balance_treasury_buildings(struct city *pcity)
Definition cityturn.c:3122
static void update_city_activity(struct city *pcity)
Definition cityturn.c:3381
static void upgrade_unit_prod(struct city *pcity)
Definition cityturn.c:2340
static float city_migration_score(struct city *pcity)
Definition cityturn.c:3687
void nullify_prechange_production(struct city *pcity)
Definition cityturn.c:3372
bool city_empty_food_stock(struct city *pcity)
Definition cityturn.c:4048
bool check_city_migrations(void)
Definition cityturn.c:4016
static void upgrade_building_prod(struct city *pcity)
Definition cityturn.c:2294
bool city_change_size(struct city *pcity, citizens size, struct player *nationality, const char *reason)
Definition cityturn.c:1060
static citizens city_reduce_specialists(struct city *pcity, citizens change)
Definition cityturn.c:763
static void city_global_turn_notify(struct conn_list *dest)
Definition cityturn.c:492
static void city_populate(struct city *pcity, struct player *nationality)
Definition cityturn.c:1111
static void city_refresh_after_city_size_increase(struct city *pcity, struct player *nationality)
Definition cityturn.c:1026
static void define_orig_production_values(struct city *pcity)
Definition cityturn.c:3341
void city_repair_size(struct city *pcity, int change)
Definition cityturn.c:896
#define cityimpr_list_iterate(cityimprlist, pcityimpr)
Definition cityturn.c:127
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:731
static bool city_build_building(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2444
static struct unit static const struct impr_type * building_upgrades_to(struct city *pcity, const struct impr_type *pimprove)
Definition cityturn.c:2273
void send_city_turn_notifications(struct connection *pconn)
Definition cityturn.c:578
static struct unit * sell_random_unit(struct player *pplayer, struct unit_list *punitlist)
Definition cityturn.c:2939
int city_incite_cost(struct player *pplayer, struct city *pcity)
Definition cityturn.c:3260
static void nullify_caravan_and_disband_plus(struct city *pcity)
Definition cityturn.c:3362
static bool sell_random_building(struct player *pplayer, struct cityimpr_list *imprs)
Definition cityturn.c:2836
static bool disband_city(struct city *pcity)
Definition cityturn.c:3575
void update_city_activities(struct player *pplayer)
Definition cityturn.c:604
bool city_reduce_size(struct city *pcity, citizens pop_loss, struct player *destroyer, const char *reason)
Definition cityturn.c:807
#define cityimpr_list_iterate_end
Definition cityturn.c:129
bool city_refresh(struct city *pcity)
Definition cityturn.c:161
static void city_turn_notify(const struct city *pcity, struct conn_list *dest, const struct player *cache_for_player)
Definition cityturn.c:513
static bool city_increase_size(struct city *pcity)
Definition cityturn.c:949
void apply_cmresult_to_city(struct city *pcity, const struct cm_result *cmr)
Definition cityturn.c:283
void city_style_refresh(struct city *pcity)
Definition cityturn.c:4373
static bool city_balance_treasury_units(struct city *pcity)
Definition cityturn.c:3169
static const struct unit_type * unit_upgrades_to(struct city *pcity, const struct unit_type *id)
Definition cityturn.c:2319
static bool do_city_migration(struct city *pcity_from, struct city *pcity_to)
Definition cityturn.c:3771
static struct city_list * city_refresh_queue
Definition cityturn.c:87
void city_refresh_for_player(struct player *pplayer)
Definition cityturn.c:184
void city_tc_effect_refresh(struct player *pplayer)
Definition cityturn.c:4381
static void check_pollution(struct city *pcity)
Definition cityturn.c:3243
void check_disasters(void)
Definition cityturn.c:4177
static void set_default_city_manager(struct cm_parameter *cmp, struct city *pcity)
Definition cityturn.c:315
static citizens city_reduce_workers(struct city *pcity, citizens change)
Definition cityturn.c:783
static bool worklist_item_postpone_req_vec(struct universal *target, struct city *pcity, struct player *pplayer, int saved_id)
Definition cityturn.c:1186
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:2596
static bool player_balance_treasury_units_and_buildings(struct player *pplayer)
Definition cityturn.c:3023
void city_refresh_queue_processing(void)
Definition cityturn.c:215
void remove_obsolete_buildings_city(struct city *pcity, bool refresh)
Definition cityturn.c:237
static bool check_city_migrations_player(const struct player *pplayer)
Definition cityturn.c:4214
static bool city_build_unit(struct player *pplayer, struct city *pcity)
Definition cityturn.c:2649
static bool player_balance_treasury_units(struct player *pplayer)
Definition cityturn.c:3086
void cm_clear_cache(struct city *pcity)
Definition cm.c:320
void cm_init_parameter(struct cm_parameter *dest)
Definition cm.c:2178
struct cm_result * cm_result_new(struct city *pcity)
Definition cm.c:343
void cm_result_destroy(struct cm_result *result)
Definition cm.c:366
void cm_print_result(const struct cm_result *result)
Definition cm.c:2464
void cm_query_result(struct city *pcity, const struct cm_parameter *param, struct cm_result *result, bool negative_ok)
Definition cm.c:2118
void cm_init_emergency_parameter(struct cm_parameter *dest)
Definition cm.c:2196
void cm_print_city(const struct city *pcity)
Definition cm.c:2426
struct player * conn_get_player(const struct connection *pconn)
Definition connection.c:760
void conn_list_do_unbuffer(struct conn_list *dest)
Definition connection.c:365
void conn_list_do_buffer(struct conn_list *dest)
Definition connection.c:355
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:73
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:73
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:789
int get_current_construction_bonus(const struct city *pcity, enum effect_type effect_type, const enum req_problem_type prob_type)
Definition effects.c:1104
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:838
struct extra_type * rand_extra_for_tile(struct tile *ptile, enum extra_cause cause, bool generated)
Definition extras.c:267
const char * extra_name_translation(const struct extra_type *pextra)
Definition extras.c:186
#define MAX_CITY_NATIONALITIES
Definition fc_types.h:79
int Tech_type_id
Definition fc_types.h:347
unsigned char citizens
Definition fc_types.h:358
@ RPT_CERTAIN
Definition fc_types.h:586
@ RPT_POSSIBLE
Definition fc_types.h:585
#define MAX_LEN_NAME
Definition fc_types.h:66
@ O_SHIELD
Definition fc_types.h:91
@ O_FOOD
Definition fc_types.h:91
@ O_TRADE
Definition fc_types.h:91
@ O_SCIENCE
Definition fc_types.h:91
@ O_LUXURY
Definition fc_types.h:91
@ O_GOLD
Definition fc_types.h:91
enum output_type_id Output_type_id
Definition fc_types.h:348
#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:57
struct world wld
Definition game.c:58
struct city * game_city_by_number(int id)
Definition game.c:102
#define GAME_MAX_MGR_DISTANCE
Definition game.h:511
const char * government_name_translation(const struct government *pgovern)
Definition government.c:142
const char * ruler_title_for_player(const struct player *pplayer, char *buf, size_t buf_len)
Definition government.c:390
struct government * government_of_city(const struct city *pcity)
Definition government.c:122
Government_type_id government_number(const struct government *pgovern)
Definition government.c:90
GType type
Definition repodlgs.c:1312
void handle_player_change_government(struct player *pplayer, Government_type_id government)
Definition plrhand.c:454
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)
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:628
int map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:652
#define iterate_outward(nmap, start_tile, max_dist, itr_tile)
Definition map.h:361
#define iterate_outward_end
Definition map.h:365
void map_update_border(struct tile *ptile, struct player *owner, int old_radius_sq, int new_radius_sq)
Definition maphand.c:2238
void map_claim_border(struct tile *ptile, struct player *owner, int radius_sq)
Definition maphand.c:2270
void update_tile_knowledge(struct tile *ptile)
Definition maphand.c:1427
#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:137
const char * nation_adjective_for_player(const struct player *pplayer)
Definition nation.c:168
const char * nation_adjective_translation(const struct nation_type *pnation)
Definition nation.c:148
struct nation_type * nation_of_city(const struct city *pcity)
Definition nation.c:453
const char * nation_group_name_translation(const struct nation_group *pgroup)
Definition nation.c:1089
const char * nation_plural_translation(const struct nation_type *pnation)
Definition nation.c:158
const char * nation_plural_for_player(const struct player *pplayer)
Definition nation.c:177
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_chat_msg(struct conn_list *dest, const struct packet_chat_msg *packet)
struct city_list * cities
Definition packhand.c:117
int player_number(const struct player *pplayer)
Definition player.c:828
const char * player_name(const struct player *pplayer)
Definition player.c:886
struct city * player_city_by_number(const struct player *pplayer, int city_id)
Definition player.c:1179
const char * diplrel_name_translation(int value)
Definition player.c:1591
struct player * player_slot_get_player(const struct player_slot *pslot)
Definition player.c:430
#define players_iterate_end
Definition player.h:535
#define players_iterate(_pplayer)
Definition player.h:530
static bool is_barbarian(const struct player *pplayer)
Definition player.h:488
#define is_ai(plr)
Definition player.h:234
void send_player_info_c(struct player *src, struct conn_list *dest)
Definition plrhand.c:1000
#define fc_rand(_size)
Definition rand.h:34
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:271
struct research * research_get(const struct player *pplayer)
Definition research.c:126
enum tech_state research_invention_state(const struct research *presearch, Tech_type_id tech)
Definition research.c:616
int research_pretty_name(const struct research *presearch, char *buf, size_t buf_len)
Definition research.c:167
#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:1378
#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:128
@ 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
#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:309
int turn_last_built
Definition city.h:373
int surplus[O_LAST]
Definition city.h:343
int food_stock
Definition city.h:354
struct player * original
Definition city.h:313
int history
Definition city.h:393
int pollution
Definition city.h:356
bool did_sell
Definition city.h:367
int id
Definition city.h:315
int last_turns_shield_surplus
Definition city.h:378
int disbanded_shields
Definition city.h:377
int workers_frozen
Definition city.h:420
bool was_happy
Definition city.h:368
struct player * owner
Definition city.h:312
int airlift
Definition city.h:365
int caravan_shields
Definition city.h:376
bool did_buy
Definition city.h:366
int anarchy
Definition city.h:370
enum city_needs_arrange needs_arrange
Definition city.h:424
struct worklist worklist
Definition city.h:387
struct universal production
Definition city.h:382
struct unit_order * orders
Definition city.h:405
struct city::@16 rally_point
citizens * nationality
Definition city.h:329
bool vigilant
Definition city.h:404
citizens size
Definition city.h:320
int illness
Definition city.h:417
int before_change_shields
Definition city.h:375
int style
Definition city.h:316
size_t length
Definition city.h:400
float migration_score
Definition city.h:414
bool needs_refresh
Definition city.h:428
citizens feel[CITIZEN_LAST][FEELING_LAST]
Definition city.h:321
citizens specialists[SP_MAX]
Definition city.h:324
struct tile * tile
Definition city.h:311
int shield_stock
Definition city.h:355
int prod[O_LAST]
Definition city.h:346
struct city::@17::@19 server
struct cm_parameter * cm_parameter
Definition city.h:408
bool debug
Definition city.h:433
struct universal changed_from
Definition city.h:385
struct unit_list * units_supported
Definition city.h:391
int mgr_score_calc_turn
Definition city.h:415
int illness_trade
Definition city.h:357
bool persistent
Definition city.h:402
int rapture
Definition city.h:371
struct city * pcity
Definition cityturn.c:119
struct impr_type * pimprove
Definition cityturn.c:120
int mgr_worldchance
Definition game.h:161
int incite_total_factor
Definition game.h:149
struct civ_game::@30::@34 server
int mgr_nationchance
Definition game.h:159
bool mgr_foodneeded
Definition game.h:158
int base_incite_cost
Definition game.h:133
struct conn_list * est_connections
Definition game.h:97
struct packet_game_info info
Definition game.h:89
int mgr_turninterval
Definition game.h:160
bool migration
Definition game.h:163
int incite_improvement_factor
Definition game.h:148
int aqueductloss
Definition game.h:128
int incite_unit_factor
Definition game.h:150
int mgr_distance
Definition game.h:157
Definition cm.h:52
bool found_a_valid
Definition cm.h:54
bool * worker_positions
Definition cm.h:59
citizens specialists[SP_MAX]
Definition cm.h:60
struct conn_list * self
Definition connection.h:168
struct requirement_vector reqs
Definition improvement.h:75
enum gold_upkeep_style gold_upkeep_style
int infra_points
Definition player.h:74
int units_built
Definition player.h:111
enum spaceship_state state
Definition spaceship.h:108
struct city_list * cities
Definition player.h:281
int bulbs_last_turn
Definition player.h:351
struct conn_list * connections
Definition player.h:298
struct player_economic economic
Definition player.h:284
struct player_spaceship spaceship
Definition player.h:286
struct player_score score
Definition player.h:283
struct player_slot * slot
Definition player.h:250
struct player::@69::@71 server
const struct player * player
Definition tile.h:49
struct unit_list * units
Definition tile.h:57
struct requirement_vector build_reqs
Definition unittype.h:501
const struct unit_type * obsoleted_by
Definition unittype.h:510
struct advance * require_advance
Definition unittype.h:500
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
int id
Definition unit.h:145
bool vigilant
Definition unit.h:197
struct unit::@79 orders
struct unit_order * list
Definition unit.h:198
struct unit::@80::@83 server
int upkeep_paid[O_LAST]
Definition unit.h:254
const struct unit_type * utype
Definition unit.h:139
enum universals_n kind
Definition fc_types.h:758
universals_u value
Definition fc_types.h:757
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:161
#define fc__attribute(x)
Definition support.h:89
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
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:1360
void give_immediate_free_tech(struct research *presearch, Tech_type_id tech)
Definition techtools.c:1379
void update_bulbs(struct player *pplayer, int bulbs, bool check_tech)
Definition techtools.c:643
const char * terrain_name_translation(const struct terrain *pterrain)
Definition terrain.c:226
void tile_add_extra(struct tile *ptile, const struct extra_type *pextra)
Definition tile.c:940
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
#define tile_worked(_tile)
Definition tile.h:113
#define tile_has_extra(ptile, pextra)
Definition tile.h:146
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(struct city *pcity, 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:604
const struct impr_type * building
Definition fc_types.h:598
bool unit_is_alive(int id)
Definition unit.c:2239
#define unit_tile(_pu)
Definition unit.h:388
#define unit_cargo_iterate_end
Definition unit.h:562
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition unit.h:559
#define unit_owner(_pu)
Definition unit.h:387
#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:1785
bool place_unit(struct unit *punit, struct player *pplayer, struct city *pcity, struct unit *ptrans, bool force)
Definition unittools.c:1727
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:1679
void unit_unset_removal_callback(struct unit *punit)
Definition unittools.c:1799
void wipe_unit(struct unit *punit, enum unit_loss_reason reason, struct player *killer)
Definition unittools.c:2251
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:1528
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:1639
const char * utype_rule_name(const struct unit_type *punittype)
Definition unittype.c:1630
int utype_pop_value(const struct unit_type *punittype, const struct city *pcity)
Definition unittype.c:1580
int utype_build_shield_cost(const struct city *pcity, const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1490
const char * utype_name_translation(const struct unit_type *punittype)
Definition unittype.c:1612
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:604
#define U_NOT_OBSOLETED
Definition unittype.h:509
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