Freeciv-3.3: /fast/freeciv/nightly/main/server/cityturn.c Source File

Freeciv-3.3

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