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