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 /* utility */
24 #include "fcintl.h"
25 #include "log.h"
26 #include "mem.h"
27 #include "rand.h"
28 #include "shared.h"
29 #include "support.h"
30
31 /* common/aicore */
32 #include "cm.h"
33
34 /* common */
35 #include "achievements.h"
36 #include "borders.h"
37 #include "calendar.h"
38 #include "citizens.h"
39 #include "city.h"
40 #include "culture.h"
41 #include "events.h"
42 #include "disaster.h"
43 #include "game.h"
44 #include "government.h"
45 #include "map.h"
46 #include "player.h"
47 #include "research.h"
48 #include "road.h"
49 #include "specialist.h"
50 #include "tech.h"
51 #include "traderoutes.h"
52 #include "unit.h"
53 #include "unitlist.h"
54
55 /* common/scriptcore */
56 #include "luascript_types.h"
57
58 /* server */
59 #include "citizenshand.h"
60 #include "citytools.h"
61 #include "cityturn.h"
62 #include "maphand.h"
63 #include "notify.h"
64 #include "plrhand.h"
65 #include "sanitycheck.h"
66 #include "spacerace.h"
67 #include "srv_log.h"
68 #include "srv_main.h"
69 #include "techtools.h"
70 #include "unittools.h"
71 #include "unithand.h"
72
73 /* server/advisors */
74 #include "advbuilding.h"
75 #include "advdata.h"
76 #include "autosettlers.h"
77
78 /* server/scripting */
79 #include "script_server.h"
80
81 /* Queue for pending city_refresh() */
82 static struct city_list *city_refresh_queue = NULL;
83
84
85 static void check_pollution(struct city *pcity);
86 static void city_populate(struct city *pcity, struct player *nationality);
87
88 static bool worklist_change_build_target(struct player *pplayer,
89 struct city *pcity);
90
91 static bool city_distribute_surplus_shields(struct player *pplayer,
92 struct city *pcity);
93 static bool city_build_building(struct player *pplayer, struct city *pcity);
94 static bool city_build_unit(struct player *pplayer, struct city *pcity);
95 static bool city_build_stuff(struct player *pplayer, struct city *pcity);
96 static struct impr_type *building_upgrades_to(struct city *pcity,
97 struct impr_type *pimprove);
98 static void upgrade_building_prod(struct city *pcity);
99 static struct unit_type *unit_upgrades_to(struct city *pcity,
100 struct unit_type *id);
101 static void upgrade_unit_prod(struct city *pcity);
102
103 /* Helper struct for associating a building to a city. */
104 struct cityimpr {
105 struct city *pcity;
106 struct impr_type *pimprove;
107 };
108
109 #define SPECLIST_TAG cityimpr
110 #define SPECLIST_TYPE struct cityimpr
111 #include "speclist.h"
112
113 #define cityimpr_list_iterate(cityimprlist, pcityimpr) \
114 TYPED_LIST_ITERATE(struct cityimpr, cityimprlist, pcityimpr)
115 #define cityimpr_list_iterate_end LIST_ITERATE_END
116
117 static bool sell_random_building(struct player *pplayer,
118 struct cityimpr_list *imprs);
119 static struct unit *sell_random_unit(struct player *pplayer,
120 struct unit_list *punitlist);
121
122 static citizens city_reduce_specialists(struct city *pcity, citizens change);
123 static citizens city_reduce_workers(struct city *pcity, citizens change);
124
125 static bool city_balance_treasury_buildings(struct city *pcity);
126 static bool city_balance_treasury_units(struct city *pcity);
127 static bool player_balance_treasury_units_and_buildings
128 (struct player *pplayer);
129 static bool player_balance_treasury_units(struct player *pplayer);
130
131 static bool disband_city(struct city *pcity);
132
133 static void define_orig_production_values(struct city *pcity);
134 static void update_city_activity(struct city *pcity);
135 static void nullify_caravan_and_disband_plus(struct city *pcity);
136 static bool city_illness_check(const struct city * pcity);
137
138 static float city_migration_score(struct city *pcity);
139 static bool do_city_migration(struct city *pcity_from,
140 struct city *pcity_to);
141 static bool check_city_migrations_player(const struct player *pplayer);
142
143 /**************************************************************************
144 Updates unit upkeeps and city internal cached data. Returns whether
145 city radius has changed.
146 **************************************************************************/
city_refresh(struct city * pcity)147 bool city_refresh(struct city *pcity)
148 {
149 bool retval;
150
151 pcity->server.needs_refresh = FALSE;
152
153 retval = city_map_update_radius_sq(pcity);
154 city_units_upkeep(pcity); /* update unit upkeep */
155 city_refresh_from_main_map(pcity, NULL);
156 city_style_refresh(pcity);
157
158 if (retval) {
159 /* Force a sync of the city after the change. */
160 send_city_info(city_owner(pcity), pcity);
161 }
162
163 return retval;
164 }
165
166 /**************************************************************************
167 Called on government change or wonder completion or stuff like that
168 -- Syela
169 **************************************************************************/
city_refresh_for_player(struct player * pplayer)170 void city_refresh_for_player(struct player *pplayer)
171 {
172 conn_list_do_buffer(pplayer->connections);
173 city_list_iterate(pplayer->cities, pcity) {
174 if (city_refresh(pcity)) {
175 auto_arrange_workers(pcity);
176 }
177 send_city_info(pplayer, pcity);
178 } city_list_iterate_end;
179 conn_list_do_unbuffer(pplayer->connections);
180 }
181
182 /****************************************************************************
183 Queue pending city_refresh() for later.
184 ****************************************************************************/
city_refresh_queue_add(struct city * pcity)185 void city_refresh_queue_add(struct city *pcity)
186 {
187 if (NULL == city_refresh_queue) {
188 city_refresh_queue = city_list_new();
189 } else if (city_list_find_number(city_refresh_queue, pcity->id)) {
190 return;
191 }
192
193 city_list_prepend(city_refresh_queue, pcity);
194 pcity->server.needs_refresh = TRUE;
195 }
196
197 /*************************************************************************
198 Refresh the listed cities.
199 Called after significant changes to borders, and arranging workers.
200 *************************************************************************/
city_refresh_queue_processing(void)201 void city_refresh_queue_processing(void)
202 {
203 if (NULL == city_refresh_queue) {
204 return;
205 }
206
207 city_list_iterate(city_refresh_queue, pcity) {
208 if (pcity->server.needs_refresh) {
209 if (city_refresh(pcity)) {
210 auto_arrange_workers(pcity);
211 }
212 send_city_info(city_owner(pcity), pcity);
213 }
214 } city_list_iterate_end;
215
216 city_list_destroy(city_refresh_queue);
217 city_refresh_queue = NULL;
218 }
219
220 /**************************************************************************
221 Automatically sells obsolete buildings from city.
222 **************************************************************************/
remove_obsolete_buildings_city(struct city * pcity,bool refresh)223 void remove_obsolete_buildings_city(struct city *pcity, bool refresh)
224 {
225 struct player *pplayer = city_owner(pcity);
226 bool sold = FALSE;
227
228 city_built_iterate(pcity, pimprove) {
229 if (improvement_obsolete(pplayer, pimprove, pcity)
230 && can_city_sell_building(pcity, pimprove)) {
231 int sgold;
232
233 do_sell_building(pplayer, pcity, pimprove);
234 sgold = impr_sell_gold(pimprove);
235 notify_player(pplayer, city_tile(pcity), E_IMP_SOLD, ftc_server,
236 PL_("%s is selling %s (obsolete) for %d.",
237 "%s is selling %s (obsolete) for %d.",
238 sgold),
239 city_link(pcity),
240 improvement_name_translation(pimprove),
241 sgold);
242 sold = TRUE;
243 }
244 } city_built_iterate_end;
245
246 if (sold && refresh) {
247 if (city_refresh(pcity)) {
248 auto_arrange_workers(pcity);
249 }
250 send_city_info(pplayer, pcity);
251 send_player_info_c(pplayer, NULL); /* Send updated gold to all */
252 }
253 }
254
255 /**************************************************************************
256 Sell obsolete buildings from all cities of the player
257 **************************************************************************/
remove_obsolete_buildings(struct player * pplayer)258 void remove_obsolete_buildings(struct player *pplayer)
259 {
260 city_list_iterate(pplayer->cities, pcity) {
261 remove_obsolete_buildings_city(pcity, FALSE);
262 } city_list_iterate_end;
263 }
264
265 /**************************************************************************
266 Rearrange workers according to a cm_result struct. The caller must make
267 sure that the result is valid.
268 **************************************************************************/
apply_cmresult_to_city(struct city * pcity,const struct cm_result * cmr)269 void apply_cmresult_to_city(struct city *pcity,
270 const struct cm_result *cmr)
271 {
272 struct tile *pcenter = city_tile(pcity);
273
274 /* Now apply results */
275 city_tile_iterate_skip_free_worked(city_map_radius_sq_get(pcity), pcenter,
276 ptile, idx, x, y) {
277 struct city *pwork = tile_worked(ptile);
278
279 if (cmr->worker_positions[idx]) {
280 if (NULL == pwork) {
281 city_map_update_worker(pcity, ptile);
282 } else {
283 fc_assert(pwork == pcity);
284 }
285 } else {
286 if (pwork == pcity) {
287 city_map_update_empty(pcity, ptile);
288 }
289 }
290 } city_tile_iterate_skip_free_worked_end;
291
292 specialist_type_iterate(sp) {
293 pcity->specialists[sp] = cmr->specialists[sp];
294 } specialist_type_iterate_end;
295 }
296
297 /**************************************************************************
298 Call sync_cities() to send the affected cities to the clients.
299 **************************************************************************/
auto_arrange_workers(struct city * pcity)300 void auto_arrange_workers(struct city *pcity)
301 {
302 struct cm_parameter cmp;
303 struct cm_result *cmr;
304 bool broadcast_needed;
305
306 /* See comment in freeze_workers(): we can't rearrange while
307 * workers are frozen (i.e. multiple updates need to be done). */
308 if (pcity->server.workers_frozen > 0) {
309 if (pcity->server.needs_arrange == CNA_NOT) {
310 pcity->server.needs_arrange = CNA_NORMAL;
311 }
312 return;
313 }
314 TIMING_LOG(AIT_CITIZEN_ARRANGE, TIMER_START);
315
316 broadcast_needed = (pcity->server.needs_arrange == CNA_BROADCAST_PENDING);
317
318 /* Freeze the workers and make sure all the tiles around the city
319 * are up to date. Then thaw, but hackishly make sure that thaw
320 * doesn't call us recursively, which would waste time. */
321 city_freeze_workers(pcity);
322 pcity->server.needs_arrange = CNA_NOT;
323
324 city_map_update_all(pcity);
325
326 pcity->server.needs_arrange = CNA_NOT;
327 city_thaw_workers(pcity);
328
329 /* Now start actually rearranging. */
330 city_refresh(pcity);
331
332 sanity_check_city(pcity);
333 cm_clear_cache(pcity);
334
335 cm_init_parameter(&cmp);
336 cmp.require_happy = FALSE;
337 cmp.allow_disorder = FALSE;
338 cmp.allow_specialists = TRUE;
339
340 /* We used to look at pplayer->ai.xxx_priority to determine the values
341 * to be used here. However that doesn't work at all because those values
342 * are on a different scale. Later the ai may wish to adjust its
343 * priorities - this should be done via a separate set of variables. */
344 if (city_size_get(pcity) > 1) {
345 if (city_size_get(pcity) <= game.info.notradesize) {
346 cmp.factor[O_FOOD] = 15;
347 } else {
348 if (city_granary_size(city_size_get(pcity)) == pcity->food_stock) {
349 /* We don't need more food if the granary is full. */
350 cmp.factor[O_FOOD] = 0;
351 } else {
352 cmp.factor[O_FOOD] = 10;
353 }
354 }
355 } else {
356 /* Growing to size 2 is the highest priority. */
357 cmp.factor[O_FOOD] = 20;
358 }
359 cmp.factor[O_SHIELD] = 5;
360 cmp.factor[O_TRADE] = 0; /* Trade only provides gold/science. */
361 cmp.factor[O_GOLD] = 2;
362 cmp.factor[O_LUXURY] = 0; /* Luxury only influences happiness. */
363 cmp.factor[O_SCIENCE] = 2;
364 cmp.happy_factor = 0;
365
366 if (city_granary_size(city_size_get(pcity)) == pcity->food_stock) {
367 cmp.minimal_surplus[O_FOOD] = 0;
368 } else {
369 cmp.minimal_surplus[O_FOOD] = 1;
370 }
371 cmp.minimal_surplus[O_SHIELD] = 1;
372 cmp.minimal_surplus[O_TRADE] = 0;
373 cmp.minimal_surplus[O_GOLD] = -FC_INFINITY;
374 cmp.minimal_surplus[O_LUXURY] = 0;
375 cmp.minimal_surplus[O_SCIENCE] = 0;
376
377 /* This must be after city_refresh() so that the result gets created for the right
378 * city radius */
379 cmr = cm_result_new(pcity);
380 cm_query_result(pcity, &cmp, cmr, FALSE);
381
382 if (!cmr->found_a_valid) {
383 /* Drop surpluses and try again. */
384 cmp.minimal_surplus[O_FOOD] = 0;
385 cmp.minimal_surplus[O_SHIELD] = 0;
386 cmp.minimal_surplus[O_GOLD] = -FC_INFINITY;
387 cm_query_result(pcity, &cmp, cmr, FALSE);
388 }
389 if (!cmr->found_a_valid) {
390 /* Emergency management. Get _some_ result. This doesn't use
391 * cm_init_emergency_parameter so we can keep the factors from
392 * above. */
393 output_type_iterate(o) {
394 cmp.minimal_surplus[o] = MIN(cmp.minimal_surplus[o],
395 MIN(pcity->surplus[o], 0));
396 } output_type_iterate_end;
397 cmp.require_happy = FALSE;
398 cmp.allow_disorder = city_owner(pcity)->ai_controlled ? FALSE : TRUE;
399 cm_query_result(pcity, &cmp, cmr, FALSE);
400 }
401 if (!cmr->found_a_valid) {
402 CITY_LOG(LOG_DEBUG, pcity, "emergency management");
403 cm_init_emergency_parameter(&cmp);
404 cm_query_result(pcity, &cmp, cmr, TRUE);
405 }
406 fc_assert_ret(cmr->found_a_valid);
407
408 apply_cmresult_to_city(pcity, cmr);
409
410 if (pcity->server.debug) {
411 /* Print debug output if requested. */
412 cm_print_city(pcity);
413 cm_print_result(cmr);
414 }
415
416 if (city_refresh(pcity)) {
417 log_error("%s radius changed when already arranged workers.",
418 city_name_get(pcity));
419 /* Can't do anything - don't want to enter infinite recursive loop
420 * by trying to arrange workers more. */
421 }
422 sanity_check_city(pcity);
423
424 if (broadcast_needed) {
425 broadcast_city_info(pcity);
426 }
427
428 cm_result_destroy(cmr);
429 TIMING_LOG(AIT_CITIZEN_ARRANGE, TIMER_STOP);
430 }
431
432 /****************************************************************************
433 Notices about cities that should be sent to all players.
434 ****************************************************************************/
city_global_turn_notify(struct conn_list * dest)435 static void city_global_turn_notify(struct conn_list *dest)
436 {
437 cities_iterate(pcity) {
438 struct impr_type *pimprove = pcity->production.value.building;
439
440 if (VUT_IMPROVEMENT == pcity->production.kind
441 && is_great_wonder(pimprove)
442 && (1 >= city_production_turns_to_build(pcity, TRUE)
443 && can_city_build_improvement_now(pcity, pimprove))) {
444 notify_conn(dest, city_tile(pcity),
445 E_WONDER_WILL_BE_BUILT, ftc_server,
446 _("Notice: Wonder %s in %s will be finished next turn."),
447 improvement_name_translation(pimprove), city_link(pcity));
448 }
449 } cities_iterate_end;
450 }
451
452 /****************************************************************************
453 Send turn notifications for specified city to specified connections.
454 If 'pplayer' is not NULL, the message will be cached for this player.
455 ****************************************************************************/
city_turn_notify(const struct city * pcity,struct conn_list * dest,const struct player * cache_for_player)456 static void city_turn_notify(const struct city *pcity,
457 struct conn_list *dest,
458 const struct player *cache_for_player)
459 {
460 struct impr_type *pimprove = pcity->production.value.building;
461 struct packet_chat_msg packet;
462 int turns_growth, turns_granary;
463
464 if (0 < pcity->surplus[O_FOOD]) {
465 turns_growth = (city_granary_size(city_size_get(pcity))
466 - pcity->food_stock - 1) / pcity->surplus[O_FOOD];
467
468 if (0 == get_city_bonus(pcity, EFT_GROWTH_FOOD)
469 && 0 < get_current_construction_bonus(pcity, EFT_GROWTH_FOOD,
470 RPT_CERTAIN)
471 && 0 < pcity->surplus[O_SHIELD]) {
472 /* From the check above, the surplus must always be positive. */
473 turns_granary = (impr_build_shield_cost(pimprove)
474 - pcity->shield_stock) / pcity->surplus[O_SHIELD];
475 /* If growth and granary completion occur simultaneously, granary
476 * preserves food. -AJS. */
477 if (5 > turns_growth && 5 > turns_granary
478 && turns_growth < turns_granary) {
479 package_event(&packet, city_tile(pcity),
480 E_CITY_GRAN_THROTTLE, ftc_server,
481 _("Suggest throttling growth in %s to use %s "
482 "(being built) more effectively."),
483 city_link(pcity),
484 improvement_name_translation(pimprove));
485 lsend_packet_chat_msg(dest, &packet);
486 if (NULL != cache_for_player) {
487 event_cache_add_for_player(&packet, cache_for_player);
488 }
489 }
490 }
491
492 if (0 >= turns_growth && !city_celebrating(pcity)
493 && city_can_grow_to(pcity, city_size_get(pcity) + 1)) {
494 package_event(&packet, city_tile(pcity),
495 E_CITY_MAY_SOON_GROW, ftc_server,
496 _("%s may soon grow to size %i."),
497 city_link(pcity), city_size_get(pcity) + 1);
498 lsend_packet_chat_msg(dest, &packet);
499 if (NULL != cache_for_player) {
500 event_cache_add_for_player(&packet, cache_for_player);
501 }
502 }
503 } else {
504 if (0 >= pcity->food_stock + pcity->surplus[O_FOOD]
505 && 0 > pcity->surplus[O_FOOD]) {
506 package_event(&packet, city_tile(pcity),
507 E_CITY_FAMINE_FEARED, ftc_server,
508 _("Warning: Famine feared in %s."), city_link(pcity));
509 lsend_packet_chat_msg(dest, &packet);
510 if (NULL != cache_for_player) {
511 event_cache_add_for_player(&packet, cache_for_player);
512 }
513 }
514 }
515 }
516
517 /****************************************************************************
518 Send global and player specific city turn notifications. If 'pconn' is
519 NULL, it will send to all connections and cache the events.
520 ****************************************************************************/
send_city_turn_notifications(struct connection * pconn)521 void send_city_turn_notifications(struct connection *pconn)
522 {
523 if (NULL != pconn) {
524 struct player *pplayer = conn_get_player(pconn);
525
526 if (NULL != pplayer) {
527 city_list_iterate(pplayer->cities, pcity) {
528 city_turn_notify(pcity, pconn->self, NULL);
529 } city_list_iterate_end;
530 }
531 city_global_turn_notify(pconn->self);
532 } else {
533 players_iterate(pplayer) {
534 city_list_iterate(pplayer->cities, pcity) {
535 city_turn_notify(pcity, pplayer->connections, pplayer);
536 } city_list_iterate_end;
537 } players_iterate_end;
538 /* NB: notifications to 'game.est_connections' are automatically
539 * cached. */
540 city_global_turn_notify(game.est_connections);
541 }
542 }
543
544 /**************************************************************************
545 Update all cities of one nation (costs for buildings, unit upkeep, ...).
546 **************************************************************************/
update_city_activities(struct player * pplayer)547 void update_city_activities(struct player *pplayer)
548 {
549 char buf[4 * MAX_LEN_NAME];
550 int n, gold;
551
552 fc_assert(NULL != pplayer);
553 fc_assert(NULL != pplayer->cities);
554
555 n = city_list_size(pplayer->cities);
556 gold = pplayer->economic.gold;
557 pplayer->server.bulbs_last_turn = 0;
558
559 if (n > 0) {
560 struct city *cities[n];
561 int i = 0, r;
562
563 city_list_iterate(pplayer->cities, pcity) {
564 int ci;
565
566 citizens_convert(pcity);
567
568 /* Cancel traderoutes that cannot exist any more */
569 for (ci = 0; ci < MAX_TRADE_ROUTES; ci++) {
570 struct city *tcity = game_city_by_number(pcity->trade[ci]);
571
572 if (tcity != NULL && !can_cities_trade(pcity, tcity)) {
573 enum trade_route_type type = cities_trade_route_type(pcity, tcity);
574 struct trade_route_settings *settings = trade_route_settings_by_type(type);
575
576 if (settings->cancelling == TRI_CANCEL) {
577 remove_trade_route(pcity, tcity, TRUE, FALSE);
578 }
579 }
580 }
581
582 /* Add cities to array for later random order handling */
583 cities[i++] = pcity;
584 } city_list_iterate_end;
585
586 /* How gold upkeep is handled depends on the setting
587 * 'game.info.gold_upkeep_style':
588 * GOLD_UPKEEP_CITY: Each city tries to balance its upkeep individually
589 * (this is done in update_city_activity()).
590 * GOLD_UPKEEP_MIXED: Each city tries to balance its upkeep for
591 * buildings individually; the upkeep for units is
592 * paid by the nation.
593 * GOLD_UPKEEP_NATION: The nation as a whole balances the treasury. If
594 * the treasury is not balance units and buildings
595 * are sold. */
596
597 /* Iterate over cities in a random order. */
598 while (i > 0) {
599 r = fc_rand(i);
600 /* update unit upkeep */
601 city_units_upkeep(cities[r]);
602 update_city_activity(cities[r]);
603 cities[r] = cities[--i];
604 }
605
606 if (pplayer->economic.gold < 0) {
607 switch (game.info.gold_upkeep_style) {
608 case GOLD_UPKEEP_CITY:
609 break;
610 case GOLD_UPKEEP_MIXED:
611 /* Nation pays for units. */
612 player_balance_treasury_units(pplayer);
613 break;
614 case GOLD_UPKEEP_NATION:
615 /* Nation pays for units and buildings. */
616 player_balance_treasury_units_and_buildings(pplayer);
617 break;
618 }
619 }
620
621 /* Should not happen. */
622 fc_assert(pplayer->economic.gold >= 0);
623 }
624
625 /* This test includes the cost of the units because
626 * units are paid for in update_city_activity() or
627 * player_balance_treasury_units(). */
628 if (gold - (gold - pplayer->economic.gold) * 3 < 0) {
629 notify_player(pplayer, NULL, E_LOW_ON_FUNDS, ftc_server,
630 _("WARNING, we're LOW on FUNDS %s."),
631 ruler_title_for_player(pplayer, buf, sizeof(buf)));
632 }
633
634 #if 0
635 /* Uncomment to unbalance the game, like in civ1 (CLG). */
636 if (pplayer->got_tech && pplayer->research->researched > 0) {
637 pplayer->research->researched = 0;
638 }
639 #endif
640
641 city_refresh_queue_processing();
642 }
643
644 /**************************************************************************
645 Reduce the city specialists by some (positive) value.
646 Return the amount of reduction.
647 **************************************************************************/
city_reduce_specialists(struct city * pcity,citizens change)648 static citizens city_reduce_specialists(struct city *pcity, citizens change)
649 {
650 citizens want = change;
651
652 fc_assert_ret_val(0 < change, 0);
653
654 specialist_type_iterate(sp) {
655 citizens fix = MIN(want, pcity->specialists[sp]);
656
657 pcity->specialists[sp] -= fix;
658 want -= fix;
659 } specialist_type_iterate_end;
660
661 return change - want;
662 }
663
664 /**************************************************************************
665 Reduce the city workers by some (positive) value.
666 Return the amount of reduction.
667 **************************************************************************/
city_reduce_workers(struct city * pcity,citizens change)668 static citizens city_reduce_workers(struct city *pcity, citizens change)
669 {
670 struct tile *pcenter = city_tile(pcity);
671 int want = change;
672
673 fc_assert_ret_val(0 < change, 0);
674
675 city_tile_iterate_skip_free_worked(city_map_radius_sq_get(pcity), pcenter,
676 ptile, _index, _x, _y) {
677 if (0 < want && tile_worked(ptile) == pcity) {
678 city_map_update_empty(pcity, ptile);
679 want--;
680 }
681 } city_tile_iterate_skip_free_worked_end;
682
683 return change - want;
684 }
685
686 /**************************************************************************
687 Reduce the city size. Return TRUE if the city survives the population
688 loss.
689 **************************************************************************/
city_reduce_size(struct city * pcity,citizens pop_loss,struct player * destroyer,const char * reason)690 bool city_reduce_size(struct city *pcity, citizens pop_loss,
691 struct player *destroyer, const char *reason)
692 {
693 citizens loss_remain;
694 int old_radius_sq;
695
696 if (pop_loss == 0) {
697 return TRUE;
698 }
699
700 if (city_size_get(pcity) <= pop_loss) {
701
702 script_server_signal_emit("city_destroyed", 3,
703 API_TYPE_CITY, pcity,
704 API_TYPE_PLAYER, pcity->owner,
705 API_TYPE_PLAYER, destroyer);
706
707 remove_city(pcity);
708 return FALSE;
709 }
710 old_radius_sq = tile_border_source_radius_sq(pcity->tile);
711 city_size_add(pcity, -pop_loss);
712 map_update_border(pcity->tile, pcity->owner, old_radius_sq,
713 tile_border_source_radius_sq(pcity->tile));
714
715 /* Cap the food stock at the new granary size. */
716 if (pcity->food_stock > city_granary_size(city_size_get(pcity))) {
717 pcity->food_stock = city_granary_size(city_size_get(pcity));
718 }
719
720 /* First try to kill off the specialists */
721 loss_remain = pop_loss - city_reduce_specialists(pcity, pop_loss);
722
723 if (loss_remain > 0) {
724 /* Take it out on workers */
725 loss_remain -= city_reduce_workers(pcity, loss_remain);
726 }
727
728 /* Update citizens. */
729 citizens_update(pcity, NULL);
730
731 /* Update number of people in each feelings category.
732 * This also updates the city radius if needed. */
733 city_refresh(pcity);
734
735 auto_arrange_workers(pcity);
736
737 /* Send city data. */
738 sync_cities();
739
740 fc_assert_ret_val_msg(0 == loss_remain, TRUE,
741 "city_reduce_size() has remaining"
742 "%d of %d for \"%s\"[%d]",
743 loss_remain, pop_loss,
744 city_name_get(pcity), city_size_get(pcity));
745
746 /* Update cities that have trade routes with us */
747 trade_routes_iterate(pcity, pcity2) {
748 if (city_refresh(pcity2)) {
749 /* This should never happen, but if it does, make sure not to
750 * leave workers outside city radius. */
751 auto_arrange_workers(pcity2);
752 }
753 } trade_routes_iterate_end;
754
755 sanity_check_city(pcity);
756
757 if (reason != NULL) {
758 int id = pcity->id;
759
760 script_server_signal_emit("city_size_change", 3,
761 API_TYPE_CITY, pcity,
762 API_TYPE_INT, -pop_loss,
763 API_TYPE_STRING, reason);
764
765 return city_exist(id);
766 }
767
768 return TRUE;
769 }
770
771 /**************************************************************************
772 Repair the city population without affecting city size.
773 Used by savegame.c and sanitycheck.c
774 **************************************************************************/
city_repair_size(struct city * pcity,int change)775 void city_repair_size(struct city *pcity, int change)
776 {
777 if (change > 0) {
778 pcity->specialists[DEFAULT_SPECIALIST] += change;
779 } else if (change < 0) {
780 int need = change + city_reduce_specialists(pcity, -change);
781
782 if (0 > need) {
783 need += city_reduce_workers(pcity, -need);
784 }
785
786 fc_assert_msg(0 == need,
787 "city_repair_size() has remaining %d of %d for \"%s\"[%d]",
788 need, change, city_name_get(pcity), city_size_get(pcity));
789 }
790 }
791
792 /**************************************************************************
793 Return the percentage of food that is kept in this city after city
794 size changes.
795
796 Normally this value is 0% but this can be increased by EFT_GROWTH_FOOD
797 effects.
798 **************************************************************************/
granary_savings(const struct city * pcity)799 static int granary_savings(const struct city *pcity)
800 {
801 int savings = get_city_bonus(pcity, EFT_GROWTH_FOOD);
802
803 return CLIP(0, savings, 100);
804 }
805
806 /**************************************************************************
807 Reset the foodbox, usually when a city grows or shrinks.
808 By default it is reset to zero, but this can be increased by Growth_Food
809 effects.
810 Usually this should be called before the city changes size.
811 **************************************************************************/
city_reset_foodbox(struct city * pcity,int new_size)812 static void city_reset_foodbox(struct city *pcity, int new_size)
813 {
814 fc_assert_ret(pcity != NULL);
815 pcity->food_stock = (city_granary_size(new_size)
816 * granary_savings(pcity)) / 100;
817 }
818
819 /**************************************************************************
820 Increase city size by one. We do not refresh borders or send info about
821 the city to the clients as part of this function. There might be several
822 calls to this function at once, and those actions are needed only once.
823 **************************************************************************/
city_increase_size(struct city * pcity,struct player * nationality)824 static bool city_increase_size(struct city *pcity, struct player *nationality)
825 {
826 int new_food;
827 int savings_pct = granary_savings(pcity);
828 bool have_square = FALSE;
829 bool rapture_grow = city_rapture_grow(pcity); /* check before size increase! */
830 struct tile *pcenter = city_tile(pcity);
831 struct player *powner = city_owner(pcity);
832 struct impr_type *pimprove = pcity->production.value.building;
833 int saved_id = pcity->id;
834
835 if (!city_can_grow_to(pcity, city_size_get(pcity) + 1)) {
836 /* need improvement */
837 if (get_current_construction_bonus(pcity, EFT_SIZE_ADJ, RPT_CERTAIN) > 0
838 || get_current_construction_bonus(pcity, EFT_SIZE_UNLIMIT, RPT_CERTAIN) > 0) {
839 notify_player(powner, city_tile(pcity), E_CITY_AQ_BUILDING, ftc_server,
840 _("%s needs %s (being built) to grow beyond size %d."),
841 city_link(pcity),
842 improvement_name_translation(pimprove),
843 city_size_get(pcity));
844 } else {
845 notify_player(powner, city_tile(pcity), E_CITY_AQUEDUCT, ftc_server,
846 _("%s needs an improvement to grow beyond size %d."),
847 city_link(pcity), city_size_get(pcity));
848 }
849 /* Granary can only hold so much */
850 new_food = (city_granary_size(city_size_get(pcity))
851 * (100 * 100 - game.server.aqueductloss * (100 - savings_pct))
852 / (100 * 100));
853 pcity->food_stock = MIN(pcity->food_stock, new_food);
854 return FALSE;
855 }
856
857 city_size_add(pcity, 1);
858
859 /* Do not empty food stock if city is growing by celebrating */
860 if (rapture_grow) {
861 new_food = city_granary_size(city_size_get(pcity));
862 } else {
863 new_food = city_granary_size(city_size_get(pcity)) * savings_pct / 100;
864 }
865 pcity->food_stock = MIN(pcity->food_stock, new_food);
866
867 /* If there is enough food, and the city is big enough,
868 * make new citizens into scientists or taxmen -- Massimo */
869
870 /* Ignore food if no square can be worked */
871 city_tile_iterate_skip_free_worked(city_map_radius_sq_get(pcity), pcenter,
872 ptile, _index, _x, _y) {
873 if (tile_worked(ptile) != pcity /* quick test */
874 && city_can_work_tile(pcity, ptile)) {
875 have_square = TRUE;
876 }
877 } city_tile_iterate_skip_free_worked_end;
878
879 if ((pcity->surplus[O_FOOD] >= 2 || !have_square)
880 && is_city_option_set(pcity, CITYO_NEW_EINSTEIN)) {
881 pcity->specialists[best_specialist(O_SCIENCE, pcity)]++;
882 } else if ((pcity->surplus[O_FOOD] >= 2 || !have_square)
883 && is_city_option_set(pcity, CITYO_NEW_TAXMAN)) {
884 pcity->specialists[best_specialist(O_GOLD, pcity)]++;
885 } else {
886 pcity->specialists[DEFAULT_SPECIALIST]++; /* or else city is !sane */
887 }
888
889 /* Update citizens. */
890 citizens_update(pcity, nationality);
891
892 /* Refresh the city data; this also checks the squared city radius. */
893 city_refresh(pcity);
894
895 auto_arrange_workers(pcity);
896
897 /* Update cities that have trade routes with us */
898 trade_routes_iterate(pcity, pcity2) {
899 if (city_refresh(pcity2)) {
900 /* This should never happen, but if it does, make sure not to
901 * leave workers outside city radius. */
902 auto_arrange_workers(pcity2);
903 }
904 } trade_routes_iterate_end;
905
906 notify_player(powner, city_tile(pcity), E_CITY_GROWTH, ftc_server,
907 _("%s grows to size %d."),
908 city_link(pcity), city_size_get(pcity));
909
910 /* Deprecated signal. Connect your lua functions to "city_size_change" that's
911 * emitted from calling functions which know the 'reason' of the increase. */
912 script_server_signal_emit("city_growth", 2,
913 API_TYPE_CITY, pcity,
914 API_TYPE_INT, city_size_get(pcity));
915 if (city_exist(saved_id)) {
916 /* Script didn't destroy this city */
917 sanity_check_city(pcity);
918 }
919 sync_cities();
920
921 return TRUE;
922 }
923
924 /****************************************************************************
925 Change the city size. Return TRUE iff the city is still alive afterwards.
926 ****************************************************************************/
city_change_size(struct city * pcity,citizens size,struct player * nationality,const char * reason)927 bool city_change_size(struct city *pcity, citizens size,
928 struct player *nationality, const char *reason)
929 {
930 int change = size - city_size_get(pcity);
931
932 if (change > 0) {
933 int old_size = city_size_get(pcity);
934 int real_change;
935
936 /* Increase city size until size reached, or increase fails */
937 while (size > city_size_get(pcity)
938 && city_increase_size(pcity, nationality)) {
939 /* city_increase_size() does all the work. */
940 }
941
942 real_change = city_size_get(pcity) - old_size;
943
944 if (real_change != 0 && reason != NULL) {
945 int id = pcity->id;
946
947 script_server_signal_emit("city_size_change", 3,
948 API_TYPE_CITY, pcity,
949 API_TYPE_INT, real_change,
950 API_TYPE_STRING, reason);
951
952 if (!city_exist(id)) {
953 return FALSE;
954 }
955 }
956 } else if (change < 0) {
957 /* We assume that city_change_size() is never called because
958 * of enemy actions. If that changes, enemy must be passed
959 * to city_reduce_size() */
960 return city_reduce_size(pcity, -change, NULL, reason);
961 }
962
963 map_claim_border(pcity->tile, pcity->owner, -1);
964
965 return TRUE;
966 }
967
968 /**************************************************************************
969 Check whether the population can be increased or
970 if the city is unable to support a 'settler'...
971 **************************************************************************/
city_populate(struct city * pcity,struct player * nationality)972 static void city_populate(struct city *pcity, struct player *nationality)
973 {
974 int saved_id = pcity->id;
975 int granary_size = city_granary_size(city_size_get(pcity));
976
977 pcity->food_stock += pcity->surplus[O_FOOD];
978 if (pcity->food_stock >= granary_size || city_rapture_grow(pcity)) {
979 if (city_had_recent_plague(pcity)) {
980 notify_player(city_owner(pcity), city_tile(pcity),
981 E_CITY_PLAGUE, ftc_server,
982 _("A recent plague outbreak prevents growth in %s."),
983 city_link(pcity));
984 /* Lose excess food */
985 pcity->food_stock = MIN(pcity->food_stock, granary_size);
986 } else {
987 bool success;
988
989 success = city_increase_size(pcity, nationality);
990 map_claim_border(pcity->tile, pcity->owner, -1);
991
992 if (success) {
993 script_server_signal_emit("city_size_change", 3,
994 API_TYPE_CITY, pcity,
995 API_TYPE_INT, 1,
996 API_TYPE_STRING, "growth");
997 }
998 }
999 } else if (pcity->food_stock < 0) {
1000 /* FIXME: should this depend on units with ability to build
1001 * cities or on units that require food in upkeep?
1002 * I'll assume citybuilders (units that 'contain' 1 pop) -- sjolie
1003 * The above may make more logical sense, but in game terms
1004 * you want to disband a unit that is draining your food
1005 * reserves. Hence, I'll assume food upkeep > 0 units. -- jjm
1006 */
1007 unit_list_iterate_safe(pcity->units_supported, punit) {
1008 if (punit->upkeep[O_FOOD] > 0
1009 && !unit_has_type_flag(punit, UTYF_UNDISBANDABLE)) {
1010
1011 notify_player(city_owner(pcity), city_tile(pcity),
1012 E_UNIT_LOST_MISC, ftc_server,
1013 _("Famine feared in %s, %s lost!"),
1014 city_link(pcity), unit_tile_link(punit));
1015
1016 wipe_unit(punit, ULR_STARVED, NULL);
1017
1018 if (city_exist(saved_id)) {
1019 city_reset_foodbox(pcity, city_size_get(pcity));
1020 }
1021 return;
1022 }
1023 } unit_list_iterate_safe_end;
1024 if (city_size_get(pcity) > 1) {
1025 notify_player(city_owner(pcity), city_tile(pcity),
1026 E_CITY_FAMINE, ftc_server,
1027 _("Famine causes population loss in %s."),
1028 city_link(pcity));
1029 } else {
1030 notify_player(city_owner(pcity), city_tile(pcity),
1031 E_CITY_FAMINE, ftc_server,
1032 _("Famine destroys %s entirely."),
1033 city_link(pcity));
1034 }
1035 city_reset_foodbox(pcity, city_size_get(pcity) - 1);
1036 city_reduce_size(pcity, 1, NULL, "famine");
1037 }
1038 }
1039
1040 /**************************************************************************
1041 Examine the worklist and change the build target. Return 0 if no
1042 targets are available to change to. Otherwise return non-zero. Has
1043 the side-effect of removing from the worklist any no-longer-available
1044 targets as well as the target actually selected, if any.
1045 **************************************************************************/
worklist_change_build_target(struct player * pplayer,struct city * pcity)1046 static bool worklist_change_build_target(struct player *pplayer,
1047 struct city *pcity)
1048 {
1049 struct universal target;
1050 bool success = FALSE;
1051 int i;
1052 int saved_id = pcity->id;
1053 bool city_checked = TRUE; /* This is used to avoid spurious city_exist() calls */
1054 struct worklist *pwl = &pcity->worklist;
1055
1056 if (worklist_is_empty(pwl)) {
1057 /* Nothing in the worklist; bail now. */
1058 return FALSE;
1059 }
1060
1061 i = 0;
1062 while (!success && i < worklist_length(pwl)) {
1063
1064 if (!city_checked) {
1065 if (!city_exist(saved_id)) {
1066 /* Some script has removed useless city that cannot build
1067 * what it is told to! */
1068 return FALSE;
1069 }
1070 city_checked = TRUE;
1071 }
1072
1073 if (worklist_peek_ith(pwl, &target, i)) {
1074 success = can_city_build_now(pcity, &target);
1075 } else {
1076 success = FALSE;
1077 }
1078 i++;
1079
1080 if (success) {
1081 break; /* while */
1082 }
1083
1084 switch (target.kind) {
1085 case VUT_UTYPE:
1086 {
1087 struct unit_type *ptarget = target.value.utype;
1088 struct unit_type *pupdate = unit_upgrades_to(pcity, ptarget);
1089
1090 /* Maybe we can just upgrade the target to what the city /can/ build. */
1091 if (U_NOT_OBSOLETED == pupdate) {
1092 /* Nope, we're stuck. Skip this item from the worklist. */
1093 if (ptarget->need_government != NULL
1094 && ptarget->need_government != government_of_player(pplayer)) {
1095 notify_player(pplayer, city_tile(pcity),
1096 E_CITY_CANTBUILD, ftc_server,
1097 _("%s can't build %s from the worklist; "
1098 "it needs %s government. Postponing..."),
1099 city_link(pcity), utype_name_translation(ptarget),
1100 government_name_translation(ptarget->need_government));
1101 script_server_signal_emit("unit_cant_be_built", 3,
1102 API_TYPE_BUILDING_TYPE, ptarget,
1103 API_TYPE_CITY, pcity,
1104 API_TYPE_STRING, "need_government");
1105 } else if (ptarget->need_improvement != NULL
1106 && !city_has_building(pcity, ptarget->need_improvement)) {
1107 notify_player(pplayer, city_tile(pcity),
1108 E_CITY_CANTBUILD, ftc_server,
1109 _("%s can't build %s from the worklist; "
1110 "need to have %s first. Postponing..."),
1111 city_link(pcity), utype_name_translation(ptarget),
1112 city_improvement_name_translation(pcity,
1113 ptarget->need_improvement));
1114 script_server_signal_emit("unit_cant_be_built", 3,
1115 API_TYPE_UNIT_TYPE, ptarget,
1116 API_TYPE_CITY, pcity,
1117 API_TYPE_STRING, "need_building");
1118 } else if (ptarget->require_advance != NULL
1119 && TECH_KNOWN != research_invention_state
1120 (research_get(pplayer),
1121 advance_number(ptarget->require_advance))) {
1122 notify_player(pplayer, city_tile(pcity),
1123 E_CITY_CANTBUILD, ftc_server,
1124 _("%s can't build %s from the worklist; "
1125 "tech %s not yet available. Postponing..."),
1126 city_link(pcity), utype_name_translation(ptarget),
1127 advance_name_translation(ptarget->require_advance));
1128 script_server_signal_emit("unit_cant_be_built", 3,
1129 API_TYPE_UNIT_TYPE, ptarget,
1130 API_TYPE_CITY, pcity,
1131 API_TYPE_STRING, "need_tech");
1132 } else {
1133 /* This shouldn't happen, but in case it does... */
1134 notify_player(pplayer, city_tile(pcity),
1135 E_CITY_CANTBUILD, ftc_server,
1136 _("%s can't build %s from the worklist; "
1137 "reason unknown! Postponing..."),
1138 city_link(pcity), utype_name_translation(ptarget));
1139 }
1140 city_checked = FALSE;
1141 break;
1142 }
1143 success = can_city_build_unit_later(pcity, pupdate);
1144 if (!success) {
1145 /* If the city can never build this unit or its descendants,
1146 * drop it. */
1147 notify_player(pplayer, city_tile(pcity),
1148 E_CITY_CANTBUILD, ftc_server,
1149 _("%s can't build %s from the worklist. Purging..."),
1150 city_link(pcity),
1151 /* Yes, warn about the targets that's actually
1152 in the worklist, not its obsolete-closure
1153 pupdate. */
1154 utype_name_translation(ptarget));
1155 script_server_signal_emit("unit_cant_be_built", 3,
1156 API_TYPE_UNIT_TYPE, ptarget,
1157 API_TYPE_CITY, pcity,
1158 API_TYPE_STRING, "never");
1159 if (city_exist(saved_id)) {
1160 city_checked = TRUE;
1161 /* Purge this worklist item. */
1162 i--;
1163 worklist_remove(pwl, i);
1164 } else {
1165 city_checked = FALSE;
1166 }
1167 } else {
1168 /* Yep, we can go after pupdate instead. Joy! */
1169 notify_player(pplayer, city_tile(pcity), E_WORKLIST, ftc_server,
1170 _("Production of %s is upgraded to %s in %s."),
1171 utype_name_translation(ptarget),
1172 utype_name_translation(pupdate),
1173 city_link(pcity));
1174 target.value.utype = pupdate;
1175 }
1176 break;
1177 }
1178 case VUT_IMPROVEMENT:
1179 {
1180 struct impr_type *ptarget = target.value.building;
1181 struct impr_type *pupdate = building_upgrades_to(pcity, ptarget);
1182 bool purge;
1183
1184 /* If the city can never build this improvement, drop it. */
1185 success = can_city_build_improvement_later(pcity, pupdate);
1186 purge = !success;
1187
1188 /* Maybe this improvement has been obsoleted by something that
1189 we can build. */
1190 if (purge && can_city_build_improvement_later(pcity, ptarget)) {
1191 purge = FALSE;
1192 bool known = FALSE;
1193
1194 /* Nope, no use. *sigh* */
1195 requirement_vector_iterate(&ptarget->reqs, preq) {
1196 if (!is_req_active(pplayer, NULL, pcity, NULL, NULL, NULL, NULL,
1197 NULL, NULL, preq, RPT_POSSIBLE)) {
1198 known = TRUE;
1199 switch (preq->source.kind) {
1200 case VUT_ADVANCE:
1201 if (preq->present) {
1202 notify_player(pplayer, city_tile(pcity),
1203 E_CITY_CANTBUILD, ftc_server,
1204 _("%s can't build %s from the worklist; "
1205 "tech %s not yet available. Postponing..."),
1206 city_link(pcity),
1207 city_improvement_name_translation(pcity, ptarget),
1208 advance_name_translation
1209 (preq->source.value.advance));
1210 script_server_signal_emit("building_cant_be_built", 3,
1211 API_TYPE_BUILDING_TYPE, ptarget,
1212 API_TYPE_CITY, pcity,
1213 API_TYPE_STRING, "need_tech");
1214 } else {
1215 /* While techs can be unlearned, this isn't useful feedback */
1216 purge = TRUE;
1217 }
1218 break;
1219 case VUT_TECHFLAG:
1220 if (preq->present) {
1221 notify_player(pplayer, city_tile(pcity),
1222 E_CITY_CANTBUILD, ftc_server,
1223 _("%s can't build %s from the worklist; "
1224 "no tech with flag \"%s\" yet available. "
1225 "Postponing..."),
1226 city_link(pcity),
1227 city_improvement_name_translation(pcity, ptarget),
1228 tech_flag_id_name(preq->source.value.techflag));
1229 script_server_signal_emit("building_cant_be_built", 3,
1230 API_TYPE_BUILDING_TYPE, ptarget,
1231 API_TYPE_CITY, pcity,
1232 API_TYPE_STRING, "need_techflag");
1233 } else {
1234 /* While techs can be unlearned, this isn't useful feedback */
1235 purge = TRUE;
1236 }
1237 break;
1238 case VUT_IMPROVEMENT:
1239 if (preq->present) {
1240 notify_player(pplayer, city_tile(pcity),
1241 E_CITY_CANTBUILD, ftc_server,
1242 _("%s can't build %s from the worklist; "
1243 "need to have %s first. Postponing..."),
1244 city_link(pcity),
1245 city_improvement_name_translation(pcity, ptarget),
1246 city_improvement_name_translation(pcity,
1247 preq->source.value.building));
1248 script_server_signal_emit("building_cant_be_built", 3,
1249 API_TYPE_BUILDING_TYPE, ptarget,
1250 API_TYPE_CITY, pcity,
1251 API_TYPE_STRING, "need_building");
1252 } else {
1253 notify_player(pplayer, city_tile(pcity),
1254 E_CITY_CANTBUILD, ftc_server,
1255 _("%s can't build %s from the worklist; "
1256 "need to not have %s. Postponing..."),
1257 city_link(pcity),
1258 city_improvement_name_translation(pcity, ptarget),
1259 city_improvement_name_translation(pcity,
1260 preq->source.value.building));
1261 script_server_signal_emit("building_cant_be_built", 3,
1262 API_TYPE_BUILDING_TYPE, ptarget,
1263 API_TYPE_CITY, pcity,
1264 API_TYPE_STRING, "have_building");
1265 }
1266 break;
1267 case VUT_GOVERNMENT:
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 "it needs %s government. Postponing..."),
1273 city_link(pcity),
1274 city_improvement_name_translation(pcity, ptarget),
1275 government_name_translation(preq->source.value.govern));
1276 script_server_signal_emit("building_cant_be_built", 3,
1277 API_TYPE_BUILDING_TYPE, ptarget,
1278 API_TYPE_CITY, pcity,
1279 API_TYPE_STRING, "need_government");
1280 } else {
1281 notify_player(pplayer, city_tile(pcity),
1282 E_CITY_CANTBUILD, ftc_server,
1283 _("%s can't build %s from the worklist; "
1284 "it cannot have %s government. Postponing..."),
1285 city_link(pcity),
1286 city_improvement_name_translation(pcity, ptarget),
1287 government_name_translation(preq->source.value.govern));
1288 script_server_signal_emit("building_cant_be_built", 3,
1289 API_TYPE_BUILDING_TYPE, ptarget,
1290 API_TYPE_CITY, pcity,
1291 API_TYPE_STRING, "have_government");
1292 }
1293 break;
1294 case VUT_ACHIEVEMENT:
1295 if (preq->present) {
1296 notify_player(pplayer, city_tile(pcity),
1297 E_CITY_CANTBUILD, ftc_server,
1298 _("%s can't build %s from the worklist; "
1299 "it needs \"%s\" achievement. Postponing..."),
1300 city_link(pcity),
1301 city_improvement_name_translation(pcity, ptarget),
1302 achievement_name_translation(preq->source.value.achievement));
1303 script_server_signal_emit("building_cant_be_built", 3,
1304 API_TYPE_BUILDING_TYPE, ptarget,
1305 API_TYPE_CITY, pcity,
1306 API_TYPE_STRING, "need_achievement");
1307 } else {
1308 /* Can't unachieve things. */
1309 purge = TRUE;
1310 }
1311 break;
1312 case VUT_EXTRA:
1313 if (preq->present) {
1314 notify_player(pplayer, city_tile(pcity),
1315 E_CITY_CANTBUILD, ftc_server,
1316 Q_("?extra:%s can't build %s from the worklist; "
1317 "%s is required. Postponing..."),
1318 city_link(pcity),
1319 city_improvement_name_translation(pcity, ptarget),
1320 extra_name_translation(preq->source.value.extra));
1321 script_server_signal_emit("building_cant_be_built", 3,
1322 API_TYPE_BUILDING_TYPE, ptarget,
1323 API_TYPE_CITY, pcity,
1324 API_TYPE_STRING, "need_extra");
1325 } else {
1326 notify_player(pplayer, city_tile(pcity),
1327 E_CITY_CANTBUILD, ftc_server,
1328 Q_("?extra:%s can't build %s from the worklist; "
1329 "%s is prohibited. Postponing..."),
1330 city_link(pcity),
1331 city_improvement_name_translation(pcity, ptarget),
1332 extra_name_translation(preq->source.value.extra));
1333 script_server_signal_emit("building_cant_be_built", 3,
1334 API_TYPE_BUILDING_TYPE, ptarget,
1335 API_TYPE_CITY, pcity,
1336 API_TYPE_STRING, "have_extra");
1337 }
1338 break;
1339 case VUT_TERRAIN:
1340 if (preq->present) {
1341 notify_player(pplayer, city_tile(pcity),
1342 E_CITY_CANTBUILD, ftc_server,
1343 Q_("?terrain:%s can't build %s from the worklist; "
1344 "%s terrain is required. Postponing..."),
1345 city_link(pcity),
1346 city_improvement_name_translation(pcity, ptarget),
1347 terrain_name_translation(preq->source.value.terrain));
1348 script_server_signal_emit("building_cant_be_built", 3,
1349 API_TYPE_BUILDING_TYPE, ptarget,
1350 API_TYPE_CITY, pcity,
1351 API_TYPE_STRING, "need_terrain");
1352 } else {
1353 notify_player(pplayer, city_tile(pcity),
1354 E_CITY_CANTBUILD, ftc_server,
1355 Q_("?terrain:%s can't build %s from the worklist; "
1356 "%s terrain is prohibited. Postponing..."),
1357 city_link(pcity),
1358 city_improvement_name_translation(pcity, ptarget),
1359 terrain_name_translation(preq->source.value.terrain));
1360 script_server_signal_emit("building_cant_be_built", 3,
1361 API_TYPE_BUILDING_TYPE, ptarget,
1362 API_TYPE_CITY, pcity,
1363 API_TYPE_STRING, "have_terrain");
1364 }
1365 break;
1366 case VUT_RESOURCE:
1367 if (preq->present) {
1368 notify_player(pplayer, city_tile(pcity),
1369 E_CITY_CANTBUILD, ftc_server,
1370 Q_("?resource:%s can't build %s from the worklist; "
1371 "%s is required. Postponing..."),
1372 city_link(pcity),
1373 city_improvement_name_translation(pcity, ptarget),
1374 resource_name_translation(preq->source.value.resource));
1375 script_server_signal_emit("building_cant_be_built", 3,
1376 API_TYPE_BUILDING_TYPE, ptarget,
1377 API_TYPE_CITY, pcity,
1378 API_TYPE_STRING, "need_resource");
1379 } else {
1380 /* FIXME: Yes, it's possible to destroy a resource, but the
1381 * player probably wanted us to have purged, rather than
1382 * postponed. Purging should be separate from impossible. */
1383 notify_player(pplayer, city_tile(pcity),
1384 E_CITY_CANTBUILD, ftc_server,
1385 Q_("?resource:%s can't build %s from the worklist; "
1386 "%s is prohibited. Postponing..."),
1387 city_link(pcity),
1388 city_improvement_name_translation(pcity, ptarget),
1389 resource_name_translation(preq->source.value.resource));
1390 script_server_signal_emit("building_cant_be_built", 3,
1391 API_TYPE_BUILDING_TYPE, ptarget,
1392 API_TYPE_CITY, pcity,
1393 API_TYPE_STRING, "have_resource");
1394 }
1395 break;
1396 case VUT_NATION:
1397 /* Nation can be required at Alliance range, which may change. */
1398 if (preq->present) {
1399 notify_player(pplayer, city_tile(pcity),
1400 E_CITY_CANTBUILD, ftc_server,
1401 /* TRANS: "%s nation" is adjective */
1402 Q_("?nation:%s can't build %s from the worklist; "
1403 "%s nation is required. Postponing..."),
1404 city_link(pcity),
1405 city_improvement_name_translation(pcity, ptarget),
1406 nation_adjective_translation(preq->source.value.nation));
1407 script_server_signal_emit("building_cant_be_built", 3,
1408 API_TYPE_BUILDING_TYPE, ptarget,
1409 API_TYPE_CITY, pcity,
1410 API_TYPE_STRING, "need_nation");
1411 } else {
1412 notify_player(pplayer, city_tile(pcity),
1413 E_CITY_CANTBUILD, ftc_server,
1414 Q_("?nation:%s can't build %s from the worklist; "
1415 "%s nation is prohibited. Postponing..."),
1416 city_link(pcity),
1417 city_improvement_name_translation(pcity, ptarget),
1418 nation_adjective_translation(preq->source.value.nation));
1419 script_server_signal_emit("building_cant_be_built", 3,
1420 API_TYPE_BUILDING_TYPE, ptarget,
1421 API_TYPE_CITY, pcity,
1422 API_TYPE_STRING, "have_nation");
1423 }
1424 break;
1425 case VUT_NATIONGROUP:
1426 /* Nation group can be required at Alliance range, which may
1427 * change. */
1428 if (preq->present) {
1429 notify_player(pplayer, city_tile(pcity),
1430 E_CITY_CANTBUILD, ftc_server,
1431 /* TRANS: "%s nation" is adjective */
1432 Q_("?ngroup:%s can't build %s from the worklist; "
1433 "%s nation is required. Postponing..."),
1434 city_link(pcity),
1435 city_improvement_name_translation(pcity, ptarget),
1436 nation_group_name_translation(preq->source.value.nationgroup));
1437 script_server_signal_emit("building_cant_be_built", 3,
1438 API_TYPE_BUILDING_TYPE, ptarget,
1439 API_TYPE_CITY, pcity,
1440 API_TYPE_STRING, "need_nationgroup");
1441 } else {
1442 notify_player(pplayer, city_tile(pcity),
1443 E_CITY_CANTBUILD, ftc_server,
1444 Q_("?ngroup:%s can't build %s from the worklist; "
1445 "%s nation is prohibited. Postponing..."),
1446 city_link(pcity),
1447 city_improvement_name_translation(pcity, ptarget),
1448 nation_group_name_translation(preq->source.value.nationgroup));
1449 script_server_signal_emit("building_cant_be_built", 3,
1450 API_TYPE_BUILDING_TYPE, ptarget,
1451 API_TYPE_CITY, pcity,
1452 API_TYPE_STRING, "have_nationgroup");
1453 }
1454 break;
1455 case VUT_STYLE:
1456 /* FIXME: City styles sometimes change over time, but it isn't
1457 * entirely under player control. Probably better to purge
1458 * with useful explanation. */
1459 if (preq->present) {
1460 notify_player(pplayer, city_tile(pcity),
1461 E_CITY_CANTBUILD, ftc_server,
1462 _("%s can't build %s from the worklist; "
1463 "only %s style cities may build this. Postponing..."),
1464 city_link(pcity),
1465 city_improvement_name_translation(pcity, ptarget),
1466 style_name_translation(preq->source.value.style));
1467 script_server_signal_emit("building_cant_be_built", 3,
1468 API_TYPE_BUILDING_TYPE, ptarget,
1469 API_TYPE_CITY, pcity,
1470 API_TYPE_STRING, "need_style");
1471 } else {
1472 notify_player(pplayer, city_tile(pcity),
1473 E_CITY_CANTBUILD, ftc_server,
1474 _("%s can't build %s from the worklist; "
1475 "%s style cities may not build this. Postponing..."),
1476 city_link(pcity),
1477 city_improvement_name_translation(pcity, ptarget),
1478 style_name_translation(preq->source.value.style));
1479 script_server_signal_emit("building_cant_be_built", 3,
1480 API_TYPE_BUILDING_TYPE, ptarget,
1481 API_TYPE_CITY, pcity,
1482 API_TYPE_STRING, "have_style");
1483 }
1484 break;
1485 case VUT_NATIONALITY:
1486 /* FIXME: Changing citizen nationality is hard: purging might be
1487 * more useful in this case. */
1488 if (preq->present) {
1489 notify_player(pplayer, city_tile(pcity),
1490 E_CITY_CANTBUILD, ftc_server,
1491 /* TRANS: Latter %s is citizen nationality */
1492 _("%s can't build %s from the worklist; "
1493 "only city with %s may build this. Postponing..."),
1494 city_link(pcity),
1495 city_improvement_name_translation(pcity, ptarget),
1496 nation_plural_translation(preq->source.value.nationality));
1497 script_server_signal_emit("building_cant_be_built", 3,
1498 API_TYPE_BUILDING_TYPE, ptarget,
1499 API_TYPE_CITY, pcity,
1500 API_TYPE_STRING, "need_nationality");
1501 } else {
1502 notify_player(pplayer, city_tile(pcity),
1503 E_CITY_CANTBUILD, ftc_server,
1504 /* TRANS: Latter %s is citizen nationality */
1505 _("%s can't build %s from the worklist; "
1506 "only city without %s may build this. Postponing..."),
1507 city_link(pcity),
1508 city_improvement_name_translation(pcity, ptarget),
1509 nation_plural_translation(preq->source.value.nationality));
1510 script_server_signal_emit("building_cant_be_built", 3,
1511 API_TYPE_BUILDING_TYPE, ptarget,
1512 API_TYPE_CITY, pcity,
1513 API_TYPE_STRING, "have_nationality");
1514 }
1515 break;
1516 case VUT_DIPLREL:
1517 if (preq->present) {
1518 notify_player(pplayer, city_tile(pcity),
1519 E_CITY_CANTBUILD, ftc_server,
1520 /* TRANS: '%s' is a wide range of relationships;
1521 * e.g., 'Peace', 'Never met', 'Is foreign',
1522 * 'Hosts embassy', 'Provided Casus Belli' */
1523 _("%s can't build %s from the worklist; "
1524 "the relationship '%s' is required."
1525 " Postponing..."),
1526 city_link(pcity),
1527 city_improvement_name_translation(pcity,
1528 ptarget),
1529 diplrel_name_translation(
1530 preq->source.value.diplrel));
1531 script_server_signal_emit("building_cant_be_built", 3,
1532 API_TYPE_BUILDING_TYPE, ptarget,
1533 API_TYPE_CITY, pcity,
1534 API_TYPE_STRING, "need_diplrel");
1535 } else {
1536 notify_player(pplayer, city_tile(pcity),
1537 E_CITY_CANTBUILD, ftc_server,
1538 _("%s can't build %s from the worklist; "
1539 "the relationship '%s' is prohibited."
1540 " Postponing..."),
1541 city_link(pcity),
1542 city_improvement_name_translation(pcity,
1543 ptarget),
1544 diplrel_name_translation(
1545 preq->source.value.diplrel));
1546 script_server_signal_emit("building_cant_be_built", 3,
1547 API_TYPE_BUILDING_TYPE, ptarget,
1548 API_TYPE_CITY, pcity,
1549 API_TYPE_STRING, "have_diplrel");
1550 }
1551 break;
1552 case VUT_MINSIZE:
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 "city must be of size %d or larger. "
1558 "Postponing..."),
1559 city_link(pcity),
1560 city_improvement_name_translation(pcity, ptarget),
1561 preq->source.value.minsize);
1562 script_server_signal_emit("building_cant_be_built", 3,
1563 API_TYPE_BUILDING_TYPE, ptarget,
1564 API_TYPE_CITY, pcity,
1565 API_TYPE_STRING, "need_minsize");
1566 } else {
1567 notify_player(pplayer, city_tile(pcity),
1568 E_CITY_CANTBUILD, ftc_server,
1569 _("%s can't build %s from the worklist; "
1570 "city must be of size %d or smaller."
1571 "Postponing..."),
1572 city_link(pcity),
1573 city_improvement_name_translation(pcity, ptarget),
1574 (preq->source.value.minsize - 1));
1575 script_server_signal_emit("building_cant_be_built", 3,
1576 API_TYPE_BUILDING_TYPE, ptarget,
1577 API_TYPE_CITY, pcity,
1578 API_TYPE_STRING, "need_minsize");
1579 }
1580 break;
1581 case VUT_MINCULTURE:
1582 if (preq->present) {
1583 notify_player(pplayer, city_tile(pcity),
1584 E_CITY_CANTBUILD, ftc_server,
1585 _("%s can't build %s from the worklist; "
1586 "city must have culture of %d. Postponing..."),
1587 city_link(pcity),
1588 city_improvement_name_translation(pcity, ptarget),
1589 preq->source.value.minculture);
1590 script_server_signal_emit("building_cant_be_built", 3,
1591 API_TYPE_BUILDING_TYPE, ptarget,
1592 API_TYPE_CITY, pcity,
1593 API_TYPE_STRING, "need_minculture");
1594 } else {
1595 /* What has been written may not be unwritten. */
1596 purge = TRUE;
1597 }
1598 break;
1599 case VUT_MAXTILEUNITS:
1600 if (preq->present) {
1601 notify_player(pplayer, city_tile(pcity),
1602 E_CITY_CANTBUILD, ftc_server,
1603 PL_("%s can't build %s from the worklist; "
1604 "more than %d unit on tile."
1605 " Postponing...",
1606 "%s can't build %s from the worklist; "
1607 "more than %d units on tile."
1608 " Postponing...",
1609 preq->source.value.max_tile_units),
1610 city_link(pcity),
1611 city_improvement_name_translation(pcity,
1612 ptarget),
1613 preq->source.value.max_tile_units);
1614 script_server_signal_emit("building_cant_be_built", 3,
1615 API_TYPE_BUILDING_TYPE, ptarget,
1616 API_TYPE_CITY, pcity,
1617 API_TYPE_STRING, "need_tileunits");
1618 } else {
1619 notify_player(pplayer, city_tile(pcity),
1620 E_CITY_CANTBUILD, ftc_server,
1621 PL_("%s can't build %s from the worklist; "
1622 "fewer than %d unit on tile."
1623 " Postponing...",
1624 "%s can't build %s from the worklist; "
1625 "fewer than %d units on tile."
1626 " Postponing...",
1627 preq->source.value.max_tile_units + 1),
1628 city_link(pcity),
1629 city_improvement_name_translation(pcity,
1630 ptarget),
1631 preq->source.value.max_tile_units + 1);
1632 script_server_signal_emit("building_cant_be_built", 3,
1633 API_TYPE_BUILDING_TYPE, ptarget,
1634 API_TYPE_CITY, pcity,
1635 API_TYPE_STRING, "need_tileunits");
1636 }
1637 break;
1638 case VUT_AI_LEVEL:
1639 /* Can't change AI level. */
1640 purge = TRUE;
1641 break;
1642 case VUT_TERRAINCLASS:
1643 if (preq->present) {
1644 notify_player(pplayer, city_tile(pcity),
1645 E_CITY_CANTBUILD, ftc_server,
1646 Q_("?terrainclass:%s can't build %s from the "
1647 "worklist; %s terrain is required."
1648 " Postponing..."),
1649 city_link(pcity),
1650 city_improvement_name_translation(pcity, ptarget),
1651 terrain_class_name_translation(preq->source.value.terrainclass));
1652 script_server_signal_emit("building_cant_be_built", 3,
1653 API_TYPE_BUILDING_TYPE, ptarget,
1654 API_TYPE_CITY, pcity,
1655 API_TYPE_STRING, "need_terrainclass");
1656 } else {
1657 notify_player(pplayer, city_tile(pcity),
1658 E_CITY_CANTBUILD, ftc_server,
1659 Q_("?terrainclass:%s can't build %s from the "
1660 "worklist; %s terrain is prohibited."
1661 " Postponing..."),
1662 city_link(pcity),
1663 city_improvement_name_translation(pcity, ptarget),
1664 terrain_class_name_translation(preq->source.value.terrainclass));
1665 script_server_signal_emit("building_cant_be_built", 3,
1666 API_TYPE_BUILDING_TYPE, ptarget,
1667 API_TYPE_CITY, pcity,
1668 API_TYPE_STRING, "have_terrainclass");
1669 }
1670 break;
1671 case VUT_TERRFLAG:
1672 if (preq->present) {
1673 notify_player(pplayer, city_tile(pcity),
1674 E_CITY_CANTBUILD, ftc_server,
1675 _("%s can't build %s from the worklist; "
1676 "terrain with \"%s\" flag is required. "
1677 "Postponing..."),
1678 city_link(pcity),
1679 city_improvement_name_translation(pcity, ptarget),
1680 terrain_flag_id_name(preq->source.value.terrainflag));
1681 script_server_signal_emit("building_cant_be_built", 3,
1682 API_TYPE_BUILDING_TYPE, ptarget,
1683 API_TYPE_CITY, pcity,
1684 API_TYPE_STRING, "need_terrainflag");
1685 } else {
1686 notify_player(pplayer, city_tile(pcity),
1687 E_CITY_CANTBUILD, ftc_server,
1688 _("%s can't build %s from the worklist; "
1689 "terrain with \"%s\" flag is prohibited. "
1690 "Postponing..."),
1691 city_link(pcity),
1692 city_improvement_name_translation(pcity, ptarget),
1693 terrain_flag_id_name(preq->source.value.terrainflag));
1694 script_server_signal_emit("building_cant_be_built", 3,
1695 API_TYPE_BUILDING_TYPE, ptarget,
1696 API_TYPE_CITY, pcity,
1697 API_TYPE_STRING, "have_terrainflag");
1698 }
1699 break;
1700 case VUT_BASEFLAG:
1701 if (preq->present) {
1702 notify_player(pplayer, city_tile(pcity),
1703 E_CITY_CANTBUILD, ftc_server,
1704 _("%s can't build %s from the worklist; "
1705 "base with \"%s\" flag is required. "
1706 "Postponing..."),
1707 city_link(pcity),
1708 city_improvement_name_translation(pcity, ptarget),
1709 base_flag_id_name(preq->source.value.baseflag));
1710 script_server_signal_emit("building_cant_be_built", 3,
1711 API_TYPE_BUILDING_TYPE, ptarget,
1712 API_TYPE_CITY, pcity,
1713 API_TYPE_STRING, "need_baseflag");
1714 } else {
1715 notify_player(pplayer, city_tile(pcity),
1716 E_CITY_CANTBUILD, ftc_server,
1717 _("%s can't build %s from the worklist; "
1718 "base with \"%s\" flag is prohibited. "
1719 "Postponing..."),
1720 city_link(pcity),
1721 city_improvement_name_translation(pcity, ptarget),
1722 base_flag_id_name(preq->source.value.baseflag));
1723 script_server_signal_emit("building_cant_be_built", 3,
1724 API_TYPE_BUILDING_TYPE, ptarget,
1725 API_TYPE_CITY, pcity,
1726 API_TYPE_STRING, "have_baseflag");
1727 }
1728 break;
1729 case VUT_ROADFLAG:
1730 if (preq->present) {
1731 notify_player(pplayer, city_tile(pcity),
1732 E_CITY_CANTBUILD, ftc_server,
1733 _("%s can't build %s from the worklist; "
1734 "road with \"%s\" flag is required. "
1735 "Postponing..."),
1736 city_link(pcity),
1737 city_improvement_name_translation(pcity, ptarget),
1738 road_flag_id_name(preq->source.value.roadflag));
1739 script_server_signal_emit("building_cant_be_built", 3,
1740 API_TYPE_BUILDING_TYPE, ptarget,
1741 API_TYPE_CITY, pcity,
1742 API_TYPE_STRING, "need_roadflag");
1743 } else {
1744 notify_player(pplayer, city_tile(pcity),
1745 E_CITY_CANTBUILD, ftc_server,
1746 _("%s can't build %s from the worklist; "
1747 "road with \"%s\" flag is prohibited. "
1748 "Postponing..."),
1749 city_link(pcity),
1750 city_improvement_name_translation(pcity, ptarget),
1751 road_flag_id_name(preq->source.value.roadflag));
1752 script_server_signal_emit("building_cant_be_built", 3,
1753 API_TYPE_BUILDING_TYPE, ptarget,
1754 API_TYPE_CITY, pcity,
1755 API_TYPE_STRING, "have_roadflag");
1756 }
1757 break;
1758 case VUT_EXTRAFLAG:
1759 if (preq->present) {
1760 notify_player(pplayer, city_tile(pcity),
1761 E_CITY_CANTBUILD, ftc_server,
1762 _("%s can't build %s from the worklist; "
1763 "extra with \"%s\" flag is required. "
1764 "Postponing..."),
1765 city_link(pcity),
1766 city_improvement_name_translation(pcity, ptarget),
1767 extra_flag_id_translated_name(preq->source.value.extraflag));
1768 script_server_signal_emit("building_cant_be_built", 3,
1769 API_TYPE_BUILDING_TYPE, ptarget,
1770 API_TYPE_CITY, pcity,
1771 API_TYPE_STRING, "need_extraflag");
1772 } else {
1773 notify_player(pplayer, city_tile(pcity),
1774 E_CITY_CANTBUILD, ftc_server,
1775 _("%s can't build %s from the worklist; "
1776 "extra with \"%s\" flag is prohibited. "
1777 "Postponing..."),
1778 city_link(pcity),
1779 city_improvement_name_translation(pcity, ptarget),
1780 extra_flag_id_translated_name(preq->source.value.extraflag));
1781 script_server_signal_emit("building_cant_be_built", 3,
1782 API_TYPE_BUILDING_TYPE, ptarget,
1783 API_TYPE_CITY, pcity,
1784 API_TYPE_STRING, "have_extraflag");
1785 }
1786 break;
1787 case VUT_UTYPE:
1788 case VUT_UTFLAG:
1789 case VUT_UCLASS:
1790 case VUT_UCFLAG:
1791 case VUT_MINVETERAN:
1792 case VUT_UNITSTATE:
1793 case VUT_MINMOVES:
1794 case VUT_MINHP:
1795 case VUT_OTYPE:
1796 case VUT_SPECIALIST:
1797 case VUT_TERRAINALTER: /* XXX could do this in principle */
1798 case VUT_CITYTILE:
1799 /* Will only happen with a bogus ruleset. */
1800 log_error("worklist_change_build_target() has bogus preq");
1801 break;
1802 case VUT_MINYEAR:
1803 if (preq->present) {
1804 notify_player(pplayer, city_tile(pcity),
1805 E_CITY_CANTBUILD, ftc_server,
1806 /* TRANS: last %s is a date */
1807 _("%s can't build %s from the worklist; "
1808 "only available from %s. Postponing..."),
1809 city_link(pcity),
1810 city_improvement_name_translation(pcity, ptarget),
1811 textyear(preq->source.value.minyear));
1812 script_server_signal_emit("building_cant_be_built", 3,
1813 API_TYPE_BUILDING_TYPE, ptarget,
1814 API_TYPE_CITY, pcity,
1815 API_TYPE_STRING, "need_minyear");
1816 } else {
1817 /* Can't go back in time. */
1818 purge = TRUE;
1819 }
1820 break;
1821 case VUT_TOPO:
1822 if (preq->present) {
1823 notify_player(pplayer, city_tile(pcity),
1824 E_CITY_CANTBUILD, ftc_server,
1825 /* TRANS: third %s is topology flag name
1826 * ("WrapX", "ISO", etc) */
1827 _("%s can't build %s from the worklist; "
1828 "only available in worlds with %s map."),
1829 city_link(pcity),
1830 city_improvement_name_translation(pcity, ptarget),
1831 _(topo_flag_name(preq->source.value.topo_property)));
1832 script_server_signal_emit("building_cant_be_built", 3,
1833 API_TYPE_BUILDING_TYPE, ptarget,
1834 API_TYPE_CITY, pcity,
1835 API_TYPE_STRING, "need_topo");
1836 }
1837 purge = TRUE;
1838 break;
1839 case VUT_AGE:
1840 if (preq->present) {
1841 notify_player(pplayer, city_tile(pcity),
1842 E_CITY_CANTBUILD, ftc_server,
1843 _("%s can't build %s from the worklist; "
1844 "only available once %d turns old. Postponing..."),
1845 city_link(pcity),
1846 city_improvement_name_translation(pcity, ptarget),
1847 preq->source.value.age);
1848 script_server_signal_emit("building_cant_be_built", 3,
1849 API_TYPE_BUILDING_TYPE, ptarget,
1850 API_TYPE_CITY, pcity,
1851 API_TYPE_STRING, "need_age");
1852 } else {
1853 /* Can't go back in time. */
1854 purge = TRUE;
1855 }
1856 break;
1857 case VUT_NONE:
1858 case VUT_COUNT:
1859 fc_assert_ret_val_msg(FALSE, TRUE,
1860 "worklist_change_build_target() "
1861 "called with invalid preq");
1862 break;
1863 /* No default handling here, as we want compiler warning
1864 * if new requirement type is added to enum and it's not handled
1865 * here. */
1866 };
1867 break;
1868 }
1869
1870 /* Almost all cases emit signal in the end, so city check needed. */
1871 if (!city_exist(saved_id)) {
1872 /* Some script has removed city */
1873 return FALSE;
1874 }
1875 city_checked = TRUE;
1876
1877 } requirement_vector_iterate_end;
1878
1879 if (!known) {
1880 /* FIXME: make can_city_build_improvement_now() return a reason enum,
1881 * so we can notify user with it.
1882 * Likely the building already exist. */
1883 purge = TRUE;
1884 }
1885 } else if (success) {
1886 /* Hey, we can upgrade the improvement! */
1887 notify_player(pplayer, city_tile(pcity), E_WORKLIST, ftc_server,
1888 _("Production of %s is upgraded to %s in %s."),
1889 city_improvement_name_translation(pcity, ptarget),
1890 city_improvement_name_translation(pcity, pupdate),
1891 city_link(pcity));
1892 target.value.building = pupdate;
1893 }
1894
1895 if (purge) {
1896 /* Never in a million years. */
1897 notify_player(pplayer, city_tile(pcity),
1898 E_CITY_CANTBUILD, ftc_server,
1899 _("%s can't build %s from the worklist. Purging..."),
1900 city_link(pcity),
1901 city_improvement_name_translation(pcity, ptarget));
1902 script_server_signal_emit("building_cant_be_built", 3,
1903 API_TYPE_BUILDING_TYPE, ptarget,
1904 API_TYPE_CITY, pcity,
1905 API_TYPE_STRING, "never");
1906 if (city_exist(saved_id)) {
1907 city_checked = TRUE;
1908 /* Purge this worklist item. */
1909 i--;
1910 worklist_remove(pwl, i);
1911 } else {
1912 city_checked = FALSE;
1913 }
1914 }
1915 break;
1916 }
1917 default:
1918 /* skip useless target */
1919 log_error("worklist_change_build_target() has unrecognized "
1920 "target kind (%d)", target.kind);
1921 break;
1922 };
1923 } /* while */
1924
1925 if (success) {
1926 /* All okay. Switch targets. */
1927 change_build_target(pplayer, pcity, &target, E_WORKLIST);
1928
1929 /* i is the index immediately _after_ the item we're changing to.
1930 Remove the (i-1)th item from the worklist. */
1931 worklist_remove(pwl, i - 1);
1932 }
1933
1934 if (worklist_is_empty(pwl)) {
1935 /* There *was* something in the worklist, but it's empty now. Bug the
1936 player about it. */
1937 notify_player(pplayer, city_tile(pcity), E_WORKLIST, ftc_server,
1938 /* TRANS: The <city> worklist .... */
1939 _("The %s worklist is now empty."),
1940 city_link(pcity));
1941 }
1942
1943 return success;
1944 }
1945
1946 /**************************************************************************
1947 Assuming we just finished building something, find something new to
1948 build. The policy is: use the worklist if we can; if not, try not
1949 changing; if we must change, get desparate and use the AI advisor.
1950 **************************************************************************/
choose_build_target(struct player * pplayer,struct city * pcity)1951 void choose_build_target(struct player *pplayer, struct city *pcity)
1952 {
1953 /* Pick the next thing off the worklist. */
1954 if (worklist_change_build_target(pplayer, pcity)) {
1955 return;
1956 }
1957
1958 /* Try building the same thing again. Repeat building doesn't require a
1959 * call to change_build_target, so just return. */
1960 switch (pcity->production.kind) {
1961 case VUT_UTYPE:
1962 /* We can build a unit again unless it's unique or we have lost the tech. */
1963 if (!utype_has_flag(pcity->production.value.utype, UTYF_UNIQUE)
1964 && can_city_build_unit_now(pcity, pcity->production.value.utype)) {
1965 log_debug("%s repeats building %s", city_name_get(pcity),
1966 utype_rule_name(pcity->production.value.utype));
1967 return;
1968 }
1969 break;
1970 case VUT_IMPROVEMENT:
1971 if (can_city_build_improvement_now(pcity, pcity->production.value.building)) {
1972 /* We can build space and coinage again, and possibly others. */
1973 log_debug("%s repeats building %s", city_name_get(pcity),
1974 improvement_rule_name(pcity->production.value.building));
1975 return;
1976 }
1977 break;
1978 default:
1979 /* fallthru */
1980 break;
1981 };
1982
1983 /* Find *something* to do! */
1984 log_debug("Trying advisor_choose_build.");
1985 advisor_choose_build(pplayer, pcity);
1986 log_debug("Advisor_choose_build didn't kill us.");
1987 }
1988
1989 /**************************************************************************
1990 Follow the list of replacement buildings until we hit something that
1991 we can build. Returns NULL if we can't upgrade at all (including if the
1992 original building is unbuildable).
1993 **************************************************************************/
building_upgrades_to(struct city * pcity,struct impr_type * pimprove)1994 static struct impr_type *building_upgrades_to(struct city *pcity,
1995 struct impr_type *pimprove)
1996 {
1997 struct impr_type *check = pimprove;
1998 struct impr_type *best_upgrade = NULL;
1999
2000 if (!can_city_build_improvement_direct(pcity, check)) {
2001 return NULL;
2002 }
2003 while (valid_improvement(check = improvement_replacement(check))) {
2004 if (can_city_build_improvement_direct(pcity, check)) {
2005 best_upgrade = check;
2006 }
2007 }
2008
2009 return best_upgrade;
2010 }
2011
2012 /**************************************************************************
2013 Try to upgrade production in pcity.
2014 **************************************************************************/
upgrade_building_prod(struct city * pcity)2015 static void upgrade_building_prod(struct city *pcity)
2016 {
2017 struct impr_type *producing = pcity->production.value.building;
2018 struct impr_type *upgrading = building_upgrades_to(pcity, producing);
2019
2020 if (upgrading && can_city_build_improvement_now(pcity, upgrading)) {
2021 notify_player(city_owner(pcity), city_tile(pcity),
2022 E_UNIT_UPGRADED, ftc_server,
2023 _("Production of %s is upgraded to %s in %s."),
2024 improvement_name_translation(producing),
2025 improvement_name_translation(upgrading),
2026 city_link(pcity));
2027 pcity->production.kind = VUT_IMPROVEMENT;
2028 pcity->production.value.building = upgrading;
2029 }
2030 }
2031
2032 /**************************************************************************
2033 Follow the list of obsoleted_by units until we hit something that
2034 we can build. Return NULL when we can't upgrade at all. NB: returning
2035 something doesn't guarantee that pcity really _can_ build it; just that
2036 pcity can't build whatever _obsoletes_ it.
2037
2038 FIXME: this function is a duplicate of can_upgrade_unittype.
2039 **************************************************************************/
unit_upgrades_to(struct city * pcity,struct unit_type * punittype)2040 static struct unit_type *unit_upgrades_to(struct city *pcity,
2041 struct unit_type *punittype)
2042 {
2043 struct unit_type *check = punittype;
2044 struct unit_type *best_upgrade = U_NOT_OBSOLETED;
2045
2046 if (!can_city_build_unit_direct(pcity, punittype)) {
2047 return U_NOT_OBSOLETED;
2048 }
2049 while ((check = check->obsoleted_by) != U_NOT_OBSOLETED) {
2050 if (can_city_build_unit_direct(pcity, check)) {
2051 best_upgrade = check;
2052 }
2053 }
2054
2055 return best_upgrade;
2056 }
2057
2058 /**************************************************************************
2059 Try to upgrade production in pcity.
2060 **************************************************************************/
upgrade_unit_prod(struct city * pcity)2061 static void upgrade_unit_prod(struct city *pcity)
2062 {
2063 struct unit_type *producing = pcity->production.value.utype;
2064 struct unit_type *upgrading = unit_upgrades_to(pcity, producing);
2065
2066 if (upgrading && can_city_build_unit_direct(pcity, upgrading)) {
2067 notify_player(city_owner(pcity), city_tile(pcity),
2068 E_UNIT_UPGRADED, ftc_server,
2069 _("Production of %s is upgraded to %s in %s."),
2070 utype_name_translation(producing),
2071 utype_name_translation(upgrading),
2072 city_link(pcity));
2073 pcity->production.value.utype = upgrading;
2074 }
2075 }
2076
2077 /**************************************************************************
2078 Disband units if we don't have enough shields to support them. Returns
2079 FALSE if the _city_ is disbanded as a result.
2080 **************************************************************************/
city_distribute_surplus_shields(struct player * pplayer,struct city * pcity)2081 static bool city_distribute_surplus_shields(struct player *pplayer,
2082 struct city *pcity)
2083 {
2084 if (pcity->surplus[O_SHIELD] < 0) {
2085 unit_list_iterate_safe(pcity->units_supported, punit) {
2086 if (utype_upkeep_cost(unit_type_get(punit), pplayer, O_SHIELD) > 0
2087 && pcity->surplus[O_SHIELD] < 0
2088 && !unit_has_type_flag(punit, UTYF_UNDISBANDABLE)) {
2089 notify_player(pplayer, city_tile(pcity),
2090 E_UNIT_LOST_MISC, ftc_server,
2091 _("%s can't upkeep %s, unit disbanded."),
2092 city_link(pcity), unit_link(punit));
2093 handle_unit_disband(pplayer, punit->id);
2094 /* pcity->surplus[O_SHIELD] is automatically updated. */
2095 }
2096 } unit_list_iterate_safe_end;
2097 }
2098
2099 if (pcity->surplus[O_SHIELD] < 0) {
2100 /* Special case: UTYF_UNDISBANDABLE. This nasty unit won't go so easily.
2101 * It'd rather make the citizens pay in blood for their failure to upkeep
2102 * it! If we make it here all normal units are already disbanded, so only
2103 * undisbandable ones remain. */
2104 unit_list_iterate_safe(pcity->units_supported, punit) {
2105 int upkeep = utype_upkeep_cost(unit_type_get(punit), pplayer, O_SHIELD);
2106
2107 if (upkeep > 0 && pcity->surplus[O_SHIELD] < 0) {
2108 fc_assert_action(unit_has_type_flag(punit, UTYF_UNDISBANDABLE),
2109 continue);
2110 notify_player(pplayer, city_tile(pcity),
2111 E_UNIT_LOST_MISC, ftc_server,
2112 _("Citizens in %s perish for their failure to "
2113 "upkeep %s!"),
2114 city_link(pcity), unit_link(punit));
2115 if (!city_reduce_size(pcity, 1, NULL, "upkeep_failure")) {
2116 return FALSE;
2117 }
2118
2119 /* No upkeep for the unit this turn. */
2120 pcity->surplus[O_SHIELD] += upkeep;
2121 }
2122 } unit_list_iterate_safe_end;
2123 }
2124
2125 /* Now we confirm changes made last turn. */
2126 pcity->shield_stock += pcity->surplus[O_SHIELD];
2127 pcity->before_change_shields = pcity->shield_stock;
2128 pcity->last_turns_shield_surplus = pcity->surplus[O_SHIELD];
2129
2130 return TRUE;
2131 }
2132
2133 /**************************************************************************
2134 Returns FALSE when the city is removed, TRUE otherwise.
2135 **************************************************************************/
city_build_building(struct player * pplayer,struct city * pcity)2136 static bool city_build_building(struct player *pplayer, struct city *pcity)
2137 {
2138 bool space_part;
2139 int mod;
2140 struct impr_type *pimprove;
2141 int saved_id = pcity->id;
2142
2143 if (city_production_has_flag(pcity, IF_GOLD)) {
2144 fc_assert(pcity->surplus[O_SHIELD] >= 0);
2145 /* pcity->before_change_shields already contains the surplus from
2146 * this turn. */
2147 pplayer->economic.gold += pcity->before_change_shields;
2148 pcity->before_change_shields = 0;
2149 pcity->shield_stock = 0;
2150 choose_build_target(pplayer, pcity);
2151 return TRUE;
2152 }
2153 upgrade_building_prod(pcity);
2154
2155 /* The final (after upgrade) build target */
2156 pimprove = pcity->production.value.building;
2157
2158 if (!can_city_build_improvement_now(pcity, pimprove)) {
2159 notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
2160 _("%s is building %s, which is no longer available."),
2161 city_link(pcity),
2162 city_improvement_name_translation(pcity, pimprove));
2163 script_server_signal_emit("building_cant_be_built", 3,
2164 API_TYPE_BUILDING_TYPE, pimprove,
2165 API_TYPE_CITY, pcity,
2166 API_TYPE_STRING, "unavailable");
2167 return TRUE;
2168 }
2169 if (pcity->shield_stock >= impr_build_shield_cost(pimprove)) {
2170 if (is_small_wonder(pimprove)) {
2171 city_list_iterate(pplayer->cities, wcity) {
2172 if (city_has_building(wcity, pimprove)) {
2173 city_remove_improvement(wcity, pimprove);
2174 break;
2175 }
2176 } city_list_iterate_end;
2177 }
2178
2179 space_part = TRUE;
2180 if (get_current_construction_bonus(pcity, EFT_SS_STRUCTURAL,
2181 RPT_CERTAIN) > 0) {
2182 pplayer->spaceship.structurals++;
2183 } else if (get_current_construction_bonus(pcity, EFT_SS_COMPONENT,
2184 RPT_CERTAIN) > 0) {
2185 pplayer->spaceship.components++;
2186 } else if (get_current_construction_bonus(pcity, EFT_SS_MODULE,
2187 RPT_CERTAIN) > 0) {
2188 pplayer->spaceship.modules++;
2189 } else {
2190 space_part = FALSE;
2191 city_add_improvement(pcity, pimprove);
2192 }
2193 pcity->before_change_shields -= impr_build_shield_cost(pimprove);
2194 pcity->shield_stock -= impr_build_shield_cost(pimprove);
2195 pcity->turn_last_built = game.info.turn;
2196 /* to eliminate micromanagement */
2197 if (is_great_wonder(pimprove)) {
2198 notify_player(NULL, city_tile(pcity), E_WONDER_BUILD, ftc_server,
2199 _("The %s have finished building %s in %s."),
2200 nation_plural_for_player(pplayer),
2201 city_improvement_name_translation(pcity, pimprove),
2202 city_link(pcity));
2203 }
2204
2205 notify_player(pplayer, city_tile(pcity), E_IMP_BUILD, ftc_server,
2206 _("%s has finished building %s."),
2207 city_link(pcity), improvement_name_translation(pimprove));
2208 script_server_signal_emit("building_built", 2,
2209 API_TYPE_BUILDING_TYPE, pimprove,
2210 API_TYPE_CITY, pcity);
2211
2212 if (!city_exist(saved_id)) {
2213 /* Script removed city */
2214 return FALSE;
2215 }
2216
2217 /* Call this function since some buildings may change the
2218 * the vision range of a city */
2219 city_refresh_vision(pcity);
2220
2221 if ((mod = get_current_construction_bonus(pcity, EFT_GIVE_IMM_TECH,
2222 RPT_CERTAIN))) {
2223 struct research *presearch = research_get(pplayer);
2224 char research_name[MAX_LEN_NAME * 2];
2225 int i;
2226 const char *provider = improvement_name_translation(pimprove);
2227
2228 notify_research(presearch, NULL, E_TECH_GAIN, ftc_server,
2229 PL_("%s boosts research; you gain %d immediate "
2230 "advance.",
2231 "%s boosts research; you gain %d immediate "
2232 "advances.",
2233 mod), provider, mod);
2234
2235 research_pretty_name(presearch, research_name, sizeof(research_name));
2236 for (i = 0; i < mod; i++) {
2237 Tech_type_id tech = pick_free_tech(presearch);
2238 const char *adv_name = research_advance_name_translation(presearch, tech);
2239
2240 give_immediate_free_tech(presearch, tech);
2241 notify_research(presearch, NULL, E_TECH_GAIN, ftc_server,
2242 /* TRANS: Tech from building (Darwin's Voyage) */
2243 Q_("?frombldg:Acquired %s from %s."), adv_name,
2244 provider);
2245
2246 notify_research_embassies(presearch, NULL, E_TECH_EMBASSY, ftc_server,
2247 /* TRANS: Tech from building (Darwin's
2248 * Voyage) */
2249 Q_("?frombldg:The %s have acquired %s "
2250 "from %s."),
2251 research_name, adv_name, provider);
2252 }
2253 }
2254 if (space_part && pplayer->spaceship.state == SSHIP_NONE) {
2255 notify_player(NULL, city_tile(pcity), E_SPACESHIP, ftc_server,
2256 _("The %s have started building a spaceship!"),
2257 nation_plural_for_player(pplayer));
2258 pplayer->spaceship.state = SSHIP_STARTED;
2259 }
2260 if (space_part) {
2261 /* space ship part build */
2262 send_spaceship_info(pplayer, NULL);
2263 } else {
2264 /* Update city data. */
2265 if (city_refresh(pcity)) {
2266 auto_arrange_workers(pcity);
2267 }
2268 }
2269
2270 /* Move to the next thing in the worklist */
2271 choose_build_target(pplayer, pcity);
2272 }
2273
2274 return TRUE;
2275 }
2276
2277 /**********************************************************************//**
2278 Helper function to create one unit in a city.
2279 Doesn't make any announcements.
2280 This might destroy the city due to scripts (but not otherwise; in
2281 particular, pop_cost is the caller's problem).
2282 Returns the new unit (if it survived scripts).
2283 **************************************************************************/
city_create_unit(struct city * pcity,struct unit_type * utype)2284 static struct unit *city_create_unit(struct city *pcity,
2285 struct unit_type *utype)
2286 {
2287 struct player *pplayer = city_owner(pcity);
2288 struct unit *punit;
2289 int saved_unit_id;
2290
2291 punit = create_unit(pplayer, pcity->tile, utype,
2292 do_make_unit_veteran(pcity, utype),
2293 pcity->id, 0);
2294 pplayer->score.units_built++;
2295 saved_unit_id = punit->id;
2296
2297 /* This might destroy pcity and/or punit: */
2298 script_server_signal_emit("unit_built", 2,
2299 API_TYPE_UNIT, punit,
2300 API_TYPE_CITY, pcity);
2301
2302 if (unit_is_alive(saved_unit_id)) {
2303 return punit;
2304 } else {
2305 return NULL;
2306 }
2307 }
2308
2309 /**********************************************************************//**
2310 Build city units. Several units can be built in one turn if the effect
2311 City_Build_Slots is used.
2312 Returns FALSE when the city is removed, TRUE otherwise.
2313 **************************************************************************/
city_build_unit(struct player * pplayer,struct city * pcity)2314 static bool city_build_unit(struct player *pplayer, struct city *pcity)
2315 {
2316 struct unit_type *utype;
2317 struct worklist *pwl = &pcity->worklist;
2318 int unit_shield_cost, num_units, i;
2319 int saved_city_id = pcity->id;
2320
2321 fc_assert_ret_val(pcity->production.kind == VUT_UTYPE, FALSE);
2322
2323 /* If the city has already bought a unit which is now obsolete, don't try
2324 * to upgrade the production. The new unit might require more shields, which
2325 * would be bad if it was bought to urgently defend a city. (Equally it
2326 * might be the same cost or cheaper, but tough; you hurried the unit so
2327 * you miss out on technological advances.) */
2328 if (city_can_change_build(pcity)) {
2329 upgrade_unit_prod(pcity);
2330 }
2331
2332 utype = pcity->production.value.utype;
2333 unit_shield_cost = utype_build_shield_cost(utype);
2334
2335 /* We must make a special case for barbarians here, because they are
2336 so dumb. Really. They don't know the prerequisite techs for units
2337 they build!! - Per */
2338 if (!can_city_build_unit_direct(pcity, utype)
2339 && !is_barbarian(pplayer)) {
2340 notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
2341 _("%s is building %s, which is no longer available."),
2342 city_link(pcity), utype_name_translation(utype));
2343
2344 /* Log before signal emitting, so pointers are certainly valid */
2345 log_verbose("%s %s tried to build %s, which is not available.",
2346 nation_rule_name(nation_of_city(pcity)),
2347 city_name_get(pcity), utype_rule_name(utype));
2348 script_server_signal_emit("unit_cant_be_built", 3,
2349 API_TYPE_UNIT_TYPE, utype,
2350 API_TYPE_CITY, pcity,
2351 API_TYPE_STRING, "unavailable");
2352 return city_exist(saved_city_id);
2353 }
2354
2355 if (pcity->shield_stock >= unit_shield_cost) {
2356 int pop_cost = utype_pop_value(utype);
2357 struct unit *punit;
2358
2359 /* Should we disband the city? -- Massimo */
2360 if (city_size_get(pcity) == pop_cost
2361 && is_city_option_set(pcity, CITYO_DISBAND)) {
2362 return !disband_city(pcity);
2363 }
2364
2365 if (city_size_get(pcity) <= pop_cost) {
2366 notify_player(pplayer, city_tile(pcity), E_CITY_CANTBUILD, ftc_server,
2367 /* TRANS: city ... utype ... size ... pop_cost */
2368 _("%s can't build %s yet. "
2369 "(city size: %d, unit population cost: %d)"),
2370 city_link(pcity), utype_name_translation(utype),
2371 city_size_get(pcity), pop_cost);
2372 script_server_signal_emit("unit_cant_be_built", 3,
2373 API_TYPE_UNIT_TYPE, utype,
2374 API_TYPE_CITY, pcity,
2375 API_TYPE_STRING, "pop_cost");
2376 return city_exist(saved_city_id);
2377 }
2378
2379 fc_assert(pop_cost == 0 || city_size_get(pcity) >= pop_cost);
2380
2381 /* don't update turn_last_built if we returned above */
2382 pcity->turn_last_built = game.info.turn;
2383
2384 /* check if we can build more than one unit (effect City_Build_Slots) */
2385 (void) city_production_build_units(pcity, FALSE, &num_units);
2386
2387 /* We should be able to build at least one (by checks above) */
2388 fc_assert(num_units >= 1);
2389
2390 for (i = 0; i < num_units; i++) {
2391 punit = city_create_unit(pcity, utype);
2392
2393 /* Check if the city still exists (script might have removed it).
2394 * If not, we assume any effects / announcements done below were
2395 * already replaced by the script if necessary. */
2396 if (!city_exist(saved_city_id)) {
2397 break;
2398 }
2399
2400 if (punit) {
2401 notify_player(pplayer, city_tile(pcity), E_UNIT_BUILT, ftc_server,
2402 /* TRANS: <city> is finished building <unit/building>. */
2403 _("%s is finished building %s."),
2404 city_link(pcity), utype_name_translation(utype));
2405 }
2406
2407 /* After we created the unit remove the citizen. This will also
2408 * rearrange the worker to take into account the extra resources
2409 * (food) needed. */
2410 if (pop_cost > 0) {
2411 /* This won't disband city due to pop_cost, but script might
2412 * still destroy city. */
2413 if (!city_reduce_size(pcity, pop_cost, NULL, "unit_built")) {
2414 break;
2415 }
2416 }
2417
2418 /* to eliminate micromanagement, we only subtract the unit's cost */
2419 pcity->before_change_shields -= unit_shield_cost;
2420 pcity->shield_stock -= unit_shield_cost;
2421
2422 if (pop_cost > 0) {
2423 /* Additional message if the unit has population cost. */
2424 notify_player(pplayer, city_tile(pcity), E_UNIT_BUILT_POP_COST,
2425 ftc_server,
2426 /* TRANS: "<unit> cost... <city> shrinks..."
2427 * Plural in "%d population", not "size %d". */
2428 PL_("%s cost %d population. %s shrinks to size %d.",
2429 "%s cost %d population. %s shrinks to size %d.",
2430 pop_cost),
2431 utype_name_translation(utype), pop_cost,
2432 city_link(pcity), city_size_get(pcity));
2433 }
2434
2435 if (i != 0 && worklist_length(pwl) > 0) {
2436 /* remove the build unit from the worklist; it has to be one less
2437 * than units build to preserve the next build target from the
2438 * worklist */
2439 worklist_remove(pwl, 0);
2440 }
2441 }
2442
2443 if (city_exist(saved_city_id)) {
2444 /* Done building this unit; time to move on to the next. */
2445 choose_build_target(pplayer, pcity);
2446 }
2447 }
2448
2449 return city_exist(saved_city_id);
2450 }
2451
2452 /**************************************************************************
2453 Returns FALSE when the city is removed, TRUE otherwise.
2454 **************************************************************************/
city_build_stuff(struct player * pplayer,struct city * pcity)2455 static bool city_build_stuff(struct player *pplayer, struct city *pcity)
2456 {
2457 if (!city_distribute_surplus_shields(pplayer, pcity)) {
2458 return FALSE;
2459 }
2460
2461 nullify_caravan_and_disband_plus(pcity);
2462 define_orig_production_values(pcity);
2463
2464 switch (pcity->production.kind) {
2465 case VUT_IMPROVEMENT:
2466 return city_build_building(pplayer, pcity);
2467 case VUT_UTYPE:
2468 return city_build_unit(pplayer, pcity);
2469 default:
2470 /* must never happen! */
2471 fc_assert(FALSE);
2472 break;
2473 };
2474 return FALSE;
2475 }
2476
2477 /**************************************************************************
2478 Randomly sell a building from the given list. Returns TRUE if a building
2479 was sold.
2480
2481 NB: It is assumed that gold upkeep for the buildings has already been
2482 paid this turn, hence when a building is sold its upkeep is given back
2483 to the player.
2484 NB: The contents of 'imprs' are usually mangled by this function.
2485 NB: It is assumed that all buildings in 'imprs' can be sold.
2486 **************************************************************************/
sell_random_building(struct player * pplayer,struct cityimpr_list * imprs)2487 static bool sell_random_building(struct player *pplayer,
2488 struct cityimpr_list *imprs)
2489 {
2490 struct cityimpr *pcityimpr;
2491 int r;
2492
2493 fc_assert_ret_val(pplayer != NULL, FALSE);
2494
2495 if (!imprs || cityimpr_list_size(imprs) == 0) {
2496 return FALSE;
2497 }
2498
2499 r = fc_rand(cityimpr_list_size(imprs));
2500 pcityimpr = cityimpr_list_get(imprs, r);
2501
2502 notify_player(pplayer, city_tile(pcityimpr->pcity), E_IMP_AUCTIONED,
2503 ftc_server,
2504 _("Can't afford to maintain %s in %s, building sold!"),
2505 improvement_name_translation(pcityimpr->pimprove),
2506 city_link(pcityimpr->pcity));
2507 log_debug("%s: sold building (%s)", player_name(pplayer),
2508 improvement_name_translation(pcityimpr->pimprove));
2509
2510 do_sell_building(pplayer, pcityimpr->pcity, pcityimpr->pimprove);
2511
2512 cityimpr_list_remove(imprs, pcityimpr);
2513
2514 /* Get back the gold upkeep that was already paid this turn. */
2515 pplayer->economic.gold += city_improvement_upkeep(pcityimpr->pcity,
2516 pcityimpr->pimprove);
2517
2518 city_refresh_queue_add(pcityimpr->pcity);
2519
2520 FC_FREE(pcityimpr);
2521
2522 return TRUE;
2523 }
2524
2525 /**************************************************************************
2526 Randomly "sell" a unit from the given list. Returns pointer to sold unit.
2527 This pointer is not valid any more, but can be removed from the lists.
2528
2529 NB: It is assumed that gold upkeep for the units has already been paid
2530 this turn, hence when a unit is "sold" its upkeep is given back to the
2531 player.
2532 NB: The contents of 'units' are usually mangled by this function.
2533 NB: It is assumed that all units in 'units' have positive gold upkeep.
2534 **************************************************************************/
sell_random_unit(struct player * pplayer,struct unit_list * punitlist)2535 static struct unit *sell_random_unit(struct player *pplayer,
2536 struct unit_list *punitlist)
2537 {
2538 struct unit *punit;
2539 int gold_upkeep, r;
2540 struct unit_list *cargo;
2541
2542 fc_assert_ret_val(pplayer != NULL, NULL);
2543
2544 if (!punitlist || unit_list_size(punitlist) == 0) {
2545 return NULL;
2546 }
2547
2548 r = fc_rand(unit_list_size(punitlist));
2549 punit = unit_list_get(punitlist, r);
2550
2551 cargo = unit_list_new();
2552
2553 /* Check if unit is transporting other units from punitlist,
2554 * and sell one of those (recursively) instead.
2555 * Note that in case of recursive transports we have to iterate
2556 * also through those middle transports that themselves are not in
2557 * punitlist. */
2558 unit_cargo_iterate(punit, pcargo) {
2559 if (pcargo->upkeep[O_GOLD] > 0) { /* Optimization, do not iterate over punitlist
2560 * if we are sure that pcargo is not in it. */
2561 unit_list_iterate(punitlist, p2) {
2562 if (pcargo == p2) {
2563 unit_list_append(cargo, pcargo);
2564 }
2565 } unit_list_iterate_end;
2566 }
2567 } unit_cargo_iterate_end;
2568
2569 if (unit_list_size(cargo) > 0) {
2570 /* Recursively sell. Note that cargo list has both
2571 * leaf units and middle transports in case of
2572 * recursive transports. */
2573 struct unit *ret = sell_random_unit(pplayer, cargo);
2574
2575 if (ret != NULL) {
2576 /* Remove from original list too */
2577 unit_list_remove(punitlist, ret);
2578 }
2579
2580 unit_list_destroy(cargo);
2581
2582 return ret;
2583 }
2584
2585 unit_list_destroy(cargo);
2586
2587 gold_upkeep = punit->upkeep[O_GOLD];
2588
2589 /* All units in punitlist should have gold upkeep! */
2590 fc_assert_ret_val(gold_upkeep > 0, NULL);
2591
2592 notify_player(pplayer, unit_tile(punit), E_UNIT_LOST_MISC, ftc_server,
2593 _("Not enough gold. %s disbanded."),
2594 unit_tile_link(punit));
2595 log_debug("%s: unit sold (%s)", player_name(pplayer),
2596 unit_name_translation(punit));
2597
2598 unit_list_remove(punitlist, punit);
2599 wipe_unit(punit, ULR_SOLD, NULL);
2600
2601 /* Get the upkeep gold back. */
2602 pplayer->economic.gold += gold_upkeep;
2603
2604 return punit;
2605 }
2606
2607 /**************************************************************************
2608 Balance the gold of a nation by selling some random units and buildings.
2609 **************************************************************************/
player_balance_treasury_units_and_buildings(struct player * pplayer)2610 static bool player_balance_treasury_units_and_buildings
2611 (struct player *pplayer)
2612 {
2613 struct cityimpr_list *pimprlist;
2614 struct unit_list *punitlist;
2615 bool sell_unit = TRUE;
2616
2617 if (!pplayer) {
2618 return FALSE;
2619 }
2620
2621 pimprlist = cityimpr_list_new();
2622 punitlist = unit_list_new();
2623
2624 city_list_iterate(pplayer->cities, pcity) {
2625 city_built_iterate(pcity, pimprove) {
2626 if (can_city_sell_building(pcity, pimprove)) {
2627 struct cityimpr *ci = fc_malloc(sizeof(*ci));
2628
2629 ci->pcity = pcity;
2630 ci->pimprove = pimprove;
2631 cityimpr_list_append(pimprlist, ci);
2632 }
2633 } city_built_iterate_end;
2634
2635 unit_list_iterate(pcity->units_supported, punit) {
2636 if (punit->upkeep[O_GOLD] > 0) {
2637 unit_list_append(punitlist, punit);
2638 }
2639 } unit_list_iterate_end;
2640 } city_list_iterate_end;
2641
2642 while (pplayer->economic.gold < 0
2643 && (cityimpr_list_size(pimprlist) > 0
2644 || unit_list_size(punitlist) > 0)) {
2645 if ((!sell_unit && cityimpr_list_size(pimprlist) > 0)
2646 || unit_list_size(punitlist) == 0) {
2647 sell_random_building(pplayer, pimprlist);
2648 } else {
2649 sell_random_unit(pplayer, punitlist);
2650 }
2651 sell_unit = !sell_unit;
2652 }
2653
2654 /* Free remaining entries from list */
2655 cityimpr_list_iterate(pimprlist, pimpr) {
2656 FC_FREE(pimpr);
2657 } cityimpr_list_iterate_end;
2658
2659 if (pplayer->economic.gold < 0) {
2660 /* If we get here it means the player has
2661 * negative gold. This should never happen. */
2662 fc_assert_msg(FALSE, "Player %s (nb %d) cannot have negative gold!",
2663 player_name(pplayer), player_number(pplayer));
2664 }
2665
2666 cityimpr_list_destroy(pimprlist);
2667 unit_list_destroy(punitlist);
2668
2669 return pplayer->economic.gold >= 0;
2670 }
2671
2672 /**************************************************************************
2673 Balance the gold of a nation by selling some units which need gold upkeep.
2674 **************************************************************************/
player_balance_treasury_units(struct player * pplayer)2675 static bool player_balance_treasury_units(struct player *pplayer)
2676 {
2677 struct unit_list *punitlist;
2678
2679 if (!pplayer) {
2680 return FALSE;
2681 }
2682
2683 punitlist = unit_list_new();
2684
2685 city_list_iterate(pplayer->cities, pcity) {
2686 unit_list_iterate(pcity->units_supported, punit) {
2687 if (punit->upkeep[O_GOLD] > 0) {
2688 unit_list_append(punitlist, punit);
2689 }
2690 } unit_list_iterate_end;
2691 } city_list_iterate_end;
2692
2693 while (pplayer->economic.gold < 0
2694 && sell_random_unit(pplayer, punitlist)) {
2695 /* all done in sell_random_unit() */
2696 }
2697
2698 if (pplayer->economic.gold < 0) {
2699 /* If we get here it means the player has
2700 * negative gold. This should never happen. */
2701 fc_assert_msg(FALSE, "Player %s (nb %d) cannot have negative gold!",
2702 player_name(pplayer), player_number(pplayer));
2703 }
2704
2705 unit_list_destroy(punitlist);
2706
2707 return pplayer->economic.gold >= 0;
2708 }
2709
2710 /**************************************************************************
2711 Balance the gold of one city by randomly selling some buildings.
2712 **************************************************************************/
city_balance_treasury_buildings(struct city * pcity)2713 static bool city_balance_treasury_buildings(struct city *pcity)
2714 {
2715 struct player *pplayer;
2716 struct cityimpr_list *pimprlist;
2717
2718 if (!pcity) {
2719 return TRUE;
2720 }
2721
2722 pplayer = city_owner(pcity);
2723 pimprlist = cityimpr_list_new();
2724
2725 /* Create a vector of all buildings that can be sold. */
2726 city_built_iterate(pcity, pimprove) {
2727 if (can_city_sell_building(pcity, pimprove)) {
2728 struct cityimpr *ci = fc_malloc(sizeof(*ci));
2729
2730 ci->pcity = pcity;
2731 ci->pimprove = pimprove;
2732 cityimpr_list_append(pimprlist, ci);
2733 }
2734 } city_built_iterate_end;
2735
2736 /* Try to sell some buildings. */
2737 while (pplayer->economic.gold < 0
2738 && sell_random_building(pplayer, pimprlist)) {
2739 /* all done in sell_random_building */
2740 }
2741
2742 /* Free remaining entries from list */
2743 cityimpr_list_iterate(pimprlist, pimpr) {
2744 FC_FREE(pimpr);
2745 } cityimpr_list_iterate_end;
2746
2747 cityimpr_list_destroy(pimprlist);
2748
2749 return pplayer->economic.gold >= 0;
2750 }
2751
2752 /**************************************************************************
2753 Balance the gold of one city by randomly selling some units which need
2754 gold upkeep.
2755
2756 NB: This function adds the gold upkeep of disbanded units back to the
2757 player's gold. Hence it assumes that this gold was previously taken
2758 from the player (i.e. in update_city_activity()).
2759 **************************************************************************/
city_balance_treasury_units(struct city * pcity)2760 static bool city_balance_treasury_units(struct city *pcity)
2761 {
2762 struct player *pplayer;
2763 struct unit_list *punitlist;
2764
2765 if (!pcity) {
2766 return TRUE;
2767 }
2768
2769 pplayer = city_owner(pcity);
2770 punitlist = unit_list_new();
2771
2772 /* Create a vector of all supported units with gold upkeep. */
2773 unit_list_iterate(pcity->units_supported, punit) {
2774 if (punit->upkeep[O_GOLD] > 0) {
2775 unit_list_append(punitlist, punit);
2776 }
2777 } unit_list_iterate_end;
2778
2779 /* Still not enough gold, so try "selling" some units. */
2780 while (pplayer->economic.gold < 0
2781 && sell_random_unit(pplayer, punitlist)) {
2782 /* all done in sell_random_unit() */
2783 }
2784
2785 /* If we get here the player has negative gold, but hopefully
2786 * another city will be able to pay the deficit, so continue. */
2787
2788 unit_list_destroy(punitlist);
2789
2790 return pplayer->economic.gold >= 0;
2791 }
2792
2793 /**************************************************************************
2794 Add some Pollution if we have waste
2795 **************************************************************************/
place_pollution(struct city * pcity,enum extra_cause cause)2796 static bool place_pollution(struct city *pcity, enum extra_cause cause)
2797 {
2798 struct tile *ptile;
2799 struct tile *pcenter = city_tile(pcity);
2800 int city_radius_sq = city_map_radius_sq_get(pcity);
2801 int k = 100;
2802
2803 while (k > 0) {
2804 /* place pollution on a random city tile */
2805 int cx, cy;
2806 int tile_id = fc_rand(city_map_tiles(city_radius_sq));
2807 struct extra_type *pextra;
2808
2809 city_tile_index_to_xy(&cx, &cy, tile_id, city_radius_sq);
2810
2811 /* check for a a real map position */
2812 if (!(ptile = city_map_to_tile(pcenter, city_radius_sq, cx, cy))) {
2813 continue;
2814 }
2815
2816 pextra = rand_extra_for_tile(ptile, cause);
2817
2818 if (pextra != NULL && !tile_has_extra(ptile, pextra)) {
2819 tile_add_extra(ptile, pextra);
2820 update_tile_knowledge(ptile);
2821
2822 return TRUE;
2823 }
2824 k--;
2825 }
2826 log_debug("pollution not placed: city: %s", city_name_get(pcity));
2827
2828 return FALSE;
2829 }
2830
2831 /**************************************************************************
2832 Add some Pollution if we have waste
2833 **************************************************************************/
check_pollution(struct city * pcity)2834 static void check_pollution(struct city *pcity)
2835 {
2836 if (fc_rand(100) < pcity->pollution) {
2837 if (place_pollution(pcity, EC_POLLUTION)) {
2838 notify_player(city_owner(pcity), city_tile(pcity), E_POLLUTION, ftc_server,
2839 _("Pollution near %s."), city_link(pcity));
2840 }
2841 }
2842 }
2843
2844 /**************************************************************************
2845 Returns the cost to incite a city. This depends on the size of the city,
2846 the number of happy, unhappy and angry citizens, whether it is
2847 celebrating, how close it is to the capital, how many units it has and
2848 upkeeps, presence of courthouse, its buildings and wonders, and who
2849 originally built it.
2850 **************************************************************************/
city_incite_cost(struct player * pplayer,struct city * pcity)2851 int city_incite_cost(struct player *pplayer, struct city *pcity)
2852 {
2853 struct city *capital;
2854 int dist, size;
2855 double cost; /* Intermediate values can get very large */
2856
2857 /* Gold factor */
2858 cost = city_owner(pcity)->economic.gold + game.server.base_incite_cost;
2859
2860 unit_list_iterate(pcity->tile->units, punit) {
2861 cost += (unit_build_shield_cost(punit)
2862 * game.server.incite_unit_factor);
2863 } unit_list_iterate_end;
2864
2865 /* Buildings */
2866 city_built_iterate(pcity, pimprove) {
2867 cost += impr_build_shield_cost(pimprove)
2868 * game.server.incite_improvement_factor;
2869 } city_built_iterate_end;
2870
2871 /* Stability bonuses */
2872 if (!city_unhappy(pcity)) {
2873 cost *= 2;
2874 }
2875 if (city_celebrating(pcity)) {
2876 cost *= 2;
2877 }
2878
2879 /* Buy back is cheap, conquered cities are also cheap */
2880 if (!game.info.citizen_nationality) {
2881 if (city_owner(pcity) != pcity->original) {
2882 if (pplayer == pcity->original) {
2883 cost /= 2; /* buy back: 50% price reduction */
2884 } else {
2885 cost = cost * 2 / 3; /* buy conquered: 33% price reduction */
2886 }
2887 }
2888 }
2889
2890 /* Distance from capital */
2891 capital = player_capital(city_owner(pcity));
2892 if (capital) {
2893 int tmp = map_distance(capital->tile, pcity->tile);
2894 dist = MIN(32, tmp);
2895 } else {
2896 /* No capital? Take max penalty! */
2897 dist = 32;
2898 }
2899
2900 size = MAX(1, city_size_get(pcity)
2901 + pcity->feel[CITIZEN_HAPPY][FEELING_FINAL]
2902 - pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL]
2903 - pcity->feel[CITIZEN_ANGRY][FEELING_FINAL] * 3);
2904 cost *= size;
2905 cost *= game.server.incite_total_factor;
2906 cost = cost / (dist + 3);
2907
2908 if (game.info.citizen_nationality) {
2909 int cost_per_citizen = cost / pcity->size;
2910 int natives = citizens_nation_get(pcity, city_owner(pcity)->slot);
2911 int tgt_cit = citizens_nation_get(pcity, pplayer->slot);
2912 int third_party = pcity->size - natives - tgt_cit;
2913
2914 cost = cost_per_citizen * (natives + 0.7 * third_party + 0.5 * tgt_cit);
2915 }
2916
2917 cost += (cost * get_city_bonus(pcity, EFT_INCITE_COST_PCT)) / 100;
2918 cost /= 100;
2919
2920 if (cost >= INCITE_IMPOSSIBLE_COST) {
2921 return INCITE_IMPOSSIBLE_COST;
2922 } else {
2923 return cost;
2924 }
2925 }
2926
2927 /**************************************************************************
2928 Called every turn, at beginning of turn, for every city.
2929 **************************************************************************/
define_orig_production_values(struct city * pcity)2930 static void define_orig_production_values(struct city *pcity)
2931 {
2932 /* Remember what this city is building last turn, so that on the next turn
2933 * the player can switch production to something else and then change it
2934 * back without penalty. This has to be updated _before_ production for
2935 * this turn is calculated, so that the penalty will apply if the player
2936 * changes production away from what has just been completed. This makes
2937 * sense if you consider what this value means: all the shields in the
2938 * city have been dedicated toward the project that was chosen last turn,
2939 * so the player shouldn't be penalized if the governor has to pick
2940 * something different. See city_change_production_penalty(). */
2941 pcity->changed_from = pcity->production;
2942
2943 log_debug("In %s, building %s. Beg of Turn shields = %d",
2944 city_name_get(pcity), universal_rule_name(&pcity->changed_from),
2945 pcity->before_change_shields);
2946 }
2947
2948 /**************************************************************************
2949 Let the advisor set up city building target.
2950 **************************************************************************/
nullify_caravan_and_disband_plus(struct city * pcity)2951 static void nullify_caravan_and_disband_plus(struct city *pcity)
2952 {
2953 pcity->disbanded_shields=0;
2954 pcity->caravan_shields=0;
2955 }
2956
2957 /**************************************************************************
2958 Initialize all variables containing information about production
2959 before it was changed.
2960 **************************************************************************/
nullify_prechange_production(struct city * pcity)2961 void nullify_prechange_production(struct city *pcity)
2962 {
2963 nullify_caravan_and_disband_plus(pcity);
2964 pcity->before_change_shields=0;
2965 }
2966
2967 /**************************************************************************
2968 Called every turn, at end of turn, for every city.
2969 **************************************************************************/
update_city_activity(struct city * pcity)2970 static void update_city_activity(struct city *pcity)
2971 {
2972 struct player *pplayer;
2973 struct government *gov;
2974 bool is_happy;
2975 bool is_celebrating;
2976
2977 if (!pcity) {
2978 return;
2979 }
2980
2981 pplayer = city_owner(pcity);
2982 gov = government_of_city(pcity);
2983 is_happy = city_happy(pcity);
2984 is_celebrating = city_celebrating(pcity);
2985
2986 if (city_refresh(pcity)) {
2987 auto_arrange_workers(pcity);
2988 }
2989
2990 /* Reporting of celebrations rewritten, copying the treatment of disorder below,
2991 with the added rapture rounds count. 991219 -- Jing */
2992 if (city_build_stuff(pplayer, pcity)) {
2993 int saved_id;
2994 int revolution_turns;
2995
2996 pcity->history += city_history_gain(pcity);
2997
2998 /* History can decrease, but never go below zero */
2999 pcity->history = MAX(pcity->history, 0);
3000
3001 /* Keep old behaviour when building new improvement could keep
3002 city celebrating */
3003 if (is_happy == FALSE) {
3004 is_happy = city_happy(pcity);
3005 }
3006
3007 if (city_celebrating(pcity) || is_celebrating) {
3008 pcity->rapture++;
3009 if (pcity->rapture == 1) {
3010 notify_player(pplayer, city_tile(pcity), E_CITY_LOVE, ftc_server,
3011 _("Celebrations in your honor in %s."),
3012 city_link(pcity));
3013 }
3014 } else {
3015 if (pcity->rapture != 0) {
3016 notify_player(pplayer, city_tile(pcity), E_CITY_NORMAL, ftc_server,
3017 _("Celebrations canceled in %s."),
3018 city_link(pcity));
3019 }
3020 pcity->rapture = 0;
3021 }
3022 pcity->was_happy = is_happy;
3023
3024 /* Handle the illness. */
3025 if (game.info.illness_on) {
3026 /* recalculate city illness; illness due to trade has to be saved
3027 * within the city struct as the client has not all data to
3028 * calculate it */
3029 pcity->server.illness
3030 = city_illness_calc(pcity, NULL, NULL, &(pcity->illness_trade), NULL);
3031
3032 if (city_illness_check(pcity)) {
3033 notify_player(pplayer, city_tile(pcity), E_CITY_PLAGUE, ftc_server,
3034 _("%s has been struck by a plague! Population lost!"),
3035 city_link(pcity));
3036 if (!city_reduce_size(pcity, 1, NULL, "plague")) {
3037 /* City destroyed completely. */
3038 return;
3039 }
3040
3041 pcity->turn_plague = game.info.turn;
3042
3043 /* recalculate illness */
3044 pcity->server.illness
3045 = city_illness_calc(pcity, NULL, NULL, &(pcity->illness_trade),
3046 NULL);
3047 }
3048 }
3049
3050 /* City population updated here, after the rapture stuff above. --Jing */
3051 saved_id = pcity->id;
3052 city_populate(pcity, pplayer);
3053 if (NULL == player_city_by_number(pplayer, saved_id)) {
3054 return;
3055 }
3056
3057 pcity->did_sell = FALSE;
3058 pcity->did_buy = FALSE;
3059 pcity->airlift = city_airlift_max(pcity);
3060 update_bulbs(pplayer, pcity->prod[O_SCIENCE], FALSE);
3061
3062 /* Update the treasury. */
3063 pplayer->economic.gold += pcity->prod[O_GOLD];
3064 pplayer->economic.gold -= city_total_impr_gold_upkeep(pcity);
3065 pplayer->economic.gold -= city_total_unit_gold_upkeep(pcity);
3066
3067 if (pplayer->economic.gold < 0) {
3068 /* Not enough gold - we have to sell some buildings, and if that
3069 * is not enough, disband units with gold upkeep, taking into
3070 * account the setting of 'game.info.gold_upkeep_style':
3071 * GOLD_UPKEEP_CITY: Cities pay for buildings and units.
3072 * GOLD_UPKEEP_MIXED: Cities pay only for buildings; the nation pays
3073 * for units.
3074 * GOLD_UPKEEP_NATION: The nation pays for buildings and units. */
3075 switch (game.info.gold_upkeep_style) {
3076 case GOLD_UPKEEP_CITY:
3077 case GOLD_UPKEEP_MIXED:
3078 if (!city_balance_treasury_buildings(pcity)
3079 && game.info.gold_upkeep_style == GOLD_UPKEEP_CITY) {
3080 city_balance_treasury_units(pcity);
3081 }
3082 break;
3083 case GOLD_UPKEEP_NATION:
3084 break;
3085 }
3086 }
3087
3088 revolution_turns = get_city_bonus(pcity, EFT_REVOLUTION_UNHAPPINESS);
3089 if (city_unhappy(pcity)) {
3090 const char *revomsg;
3091
3092 pcity->anarchy++;
3093 if (pcity->anarchy == revolution_turns) {
3094 /* Revolution next turn if not dealt with */
3095 /* TRANS: preserve leading space; this string will be appended to
3096 * another sentence */
3097 revomsg = _(" Unrest threatens to spread beyond the city.");
3098 } else {
3099 revomsg = "";
3100 }
3101 if (pcity->anarchy == 1) {
3102 notify_player(pplayer, city_tile(pcity), E_CITY_DISORDER, ftc_server,
3103 /* TRANS: second %s is an optional extra sentence */
3104 _("Civil disorder in %s.%s"),
3105 city_link(pcity), revomsg);
3106 } else {
3107 notify_player(pplayer, city_tile(pcity), E_CITY_DISORDER, ftc_server,
3108 /* TRANS: second %s is an optional extra sentence */
3109 _("CIVIL DISORDER CONTINUES in %s.%s"),
3110 city_link(pcity), revomsg);
3111 }
3112 } else {
3113 if (pcity->anarchy != 0) {
3114 notify_player(pplayer, city_tile(pcity), E_CITY_NORMAL, ftc_server,
3115 _("Order restored in %s."),
3116 city_link(pcity));
3117 }
3118 pcity->anarchy = 0;
3119 }
3120 check_pollution(pcity);
3121
3122 send_city_info(NULL, pcity);
3123
3124 if (revolution_turns > 0 && pcity->anarchy > revolution_turns) {
3125 notify_player(pplayer, city_tile(pcity), E_ANARCHY, ftc_server,
3126 /* TRANS: %s - government form, e.g., Democracy */
3127 _("The people have overthrown your %s, "
3128 "your country is in turmoil."),
3129 government_name_translation(gov));
3130 handle_player_change_government(pplayer, government_number(gov));
3131 }
3132 if (city_refresh(pcity)) {
3133 auto_arrange_workers(pcity);
3134 }
3135 sanity_check_city(pcity);
3136 }
3137 }
3138
3139 /*****************************************************************************
3140 check if city suffers from a plague. Return TRUE if it does, FALSE if not.
3141 ****************************************************************************/
city_illness_check(const struct city * pcity)3142 static bool city_illness_check(const struct city * pcity)
3143 {
3144 if (fc_rand(1000) < pcity->server.illness) {
3145 return TRUE;
3146 }
3147
3148 return FALSE;
3149 }
3150
3151 /**********************************************************************//**
3152 Disband a city into the built unit, supported by the closest city.
3153 Returns TRUE if the city was disbanded.
3154 **************************************************************************/
disband_city(struct city * pcity)3155 static bool disband_city(struct city *pcity)
3156 {
3157 struct player *pplayer = city_owner(pcity);
3158 struct tile *ptile = pcity->tile;
3159 struct city *rcity=NULL;
3160 struct unit_type *utype = pcity->production.value.utype;
3161 struct unit *punit;
3162 int saved_id = pcity->id;
3163
3164 /* find closest city other than pcity */
3165 rcity = find_closest_city(ptile, pcity, pplayer, FALSE, FALSE, FALSE, TRUE,
3166 FALSE, NULL);
3167
3168 if (!rcity) {
3169 /* What should we do when we try to disband our only city? */
3170 notify_player(pplayer, ptile, E_CITY_CANTBUILD, ftc_server,
3171 _("%s can't build %s yet, "
3172 "as we can't disband our only city."),
3173 city_link(pcity), utype_name_translation(utype));
3174 script_server_signal_emit("unit_cant_be_built", 3,
3175 API_TYPE_UNIT_TYPE, utype,
3176 API_TYPE_CITY, pcity,
3177 API_TYPE_STRING, "pop_cost");
3178 if (!city_exist(saved_id)) {
3179 /* Script decided to remove even the last city */
3180 return TRUE;
3181 } else {
3182 return FALSE;
3183 }
3184 }
3185
3186 punit = city_create_unit(pcity, utype);
3187
3188 /* "unit_built" script handler may have destroyed city. If so, we
3189 * assume something sensible happened to its units, and that the
3190 * script took care of announcing unit creation if required. */
3191 if (city_exist(saved_id)) {
3192 /* Shift all the units supported by pcity (including the new unit)
3193 * to rcity. transfer_city_units does not make sure no units are
3194 * left floating without a transport, but since all units are
3195 * transferred this is not a problem. */
3196 transfer_city_units(pplayer, pplayer, pcity->units_supported, rcity,
3197 pcity, -1, TRUE);
3198
3199 if (punit) {
3200 notify_player(pplayer, ptile, E_UNIT_BUILT, ftc_server,
3201 /* TRANS: "<city> is disbanded into Settler." */
3202 _("%s is disbanded into %s."),
3203 city_tile_link(pcity), utype_name_translation(utype));
3204 }
3205
3206 script_server_signal_emit("city_destroyed", 3,
3207 API_TYPE_CITY, pcity,
3208 API_TYPE_PLAYER, pcity->owner,
3209 API_TYPE_PLAYER, NULL);
3210
3211 remove_city(pcity);
3212
3213 /* Since we've removed the city, we don't need to worry about
3214 * charging for production, etc. */
3215 }
3216
3217 return TRUE;
3218 }
3219
3220 /***************************************************************************
3221 Helper function to calculate a "score" of a city. The score is used to get
3222 an estimate of the "migration desirability" of the city. The higher the
3223 score the more likely citizens will migrate to it.
3224
3225 The score depends on the city size, the feeling of its citizens, the cost
3226 of all buildings in the city, and the surplus of trade, luxury and
3227 science.
3228
3229 formula:
3230 score = ([city size] + feeling) * factors
3231
3232 * feeling of the citizens
3233 feeling = 1.00 * happy citizens
3234 + 0.00 * content citizens
3235 - 0.25 * unhappy citizens
3236 - 0.50 * angry citizens
3237
3238 * factors
3239 * the build costs of all buildings
3240 f = (1 + (1 - exp(-[build shield cost]/1000))/5)
3241 * the trade of the city
3242 f = (1 + (1 - exp(-[city surplus trade]/100))/5)
3243 * the luxury within the city
3244 f = (1 + (1 - exp(-[city surplus luxury]/100))/5)
3245 * the science within the city
3246 f = (1 + (1 - exp(-[city surplus science]/100))/5)
3247
3248 all factors f have values between 1 and 1.2; the overall factor will be
3249 between 1.0 (smaller cities) and 2.0 (bigger cities)
3250
3251 [build shield cost], [city surplus trade], [city surplus luxury] and
3252 [city surplus science] _must_ be >= 0!
3253
3254 The food furplus is considered by an additional factor
3255
3256 * the food surplus of the city
3257 f = (1 + [city surplus food; interval -10..20]/10)
3258
3259 The health factor is defined as:
3260
3261 * the health of the city
3262 f = (100 - [city illness; tenth of %]/25)
3263
3264 * if the city has at least one wonder a factor of 1.25 is added
3265 * for the capital an additional factor of 1.25 is used
3266 * the score is also modified by the effect EFT_MIGRATION_PCT
3267 **************************************************************************/
city_migration_score(struct city * pcity)3268 static float city_migration_score(struct city *pcity)
3269 {
3270 float score = 0.0;
3271 int build_shield_cost = 0;
3272 bool has_wonder = FALSE;
3273
3274 if (!pcity) {
3275 return score;
3276 }
3277
3278 if (pcity->server.mgr_score_calc_turn == game.info.turn) {
3279 /* up-to-date migration score */
3280 return pcity->server.migration_score;
3281 }
3282
3283 /* feeling of the citizens */
3284 score = (city_size_get(pcity)
3285 + 1.00 * pcity->feel[CITIZEN_HAPPY][FEELING_FINAL]
3286 + 0.00 * pcity->feel[CITIZEN_CONTENT][FEELING_FINAL]
3287 - 0.25 * pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL]
3288 - 0.50 * pcity->feel[CITIZEN_ANGRY][FEELING_FINAL]);
3289
3290 /* calculate shield build cost for all buildings */
3291 city_built_iterate(pcity, pimprove) {
3292 build_shield_cost += impr_build_shield_cost(pimprove);
3293 if (is_wonder(pimprove)) {
3294 /* this city has a wonder */
3295 has_wonder = TRUE;
3296 }
3297 } city_built_iterate_end;
3298
3299 /* take shield costs of all buidings into account; normalized by 1000 */
3300 score *= (1 + (1 - exp(- (float) MAX(0, build_shield_cost) / 1000)) / 5);
3301 /* take trade into account; normalized by 100 */
3302 score *= (1 + (1 - exp(- (float) MAX(0, pcity->surplus[O_TRADE]) / 100))
3303 / 5);
3304 /* take luxury into account; normalized by 100 */
3305 score *= (1 + (1 - exp(- (float) MAX(0, pcity->surplus[O_LUXURY]) / 100))
3306 / 5);
3307 /* take science into account; normalized by 100 */
3308 score *= (1 + (1 - exp(- (float) MAX(0, pcity->surplus[O_SCIENCE]) / 100))
3309 / 5);
3310
3311 score += city_culture(pcity) * game.info.culture_migration_pml / 1000;
3312
3313 /* Take food into account; the food surplus is clipped to values between
3314 * -10..20 and normalize by 10. Thus, the factor is between 0.9 and 1.2. */
3315 score *= (1 + (float) CLIP(-10, pcity->surplus[O_FOOD], 20) / 10 );
3316
3317 /* Reduce the score due to city illness (plague). The illness is given in
3318 * tenth of percent (0..1000) and normalized by 25. Thus, this factor is
3319 * between 0.6 (ill city) and 1.0 (health city). */
3320 score *= (100 - (float)city_illness_calc(pcity, NULL, NULL, NULL, NULL)
3321 / 25);
3322
3323 if (has_wonder) {
3324 /* people like wonders */
3325 score *= 1.25;
3326 }
3327
3328 if (is_capital(pcity)) {
3329 /* the capital is a magnet for the citizens */
3330 score *= 1.25;
3331 }
3332
3333 /* take into account effects */
3334 score *= (1.0 + get_city_bonus(pcity, EFT_MIGRATION_PCT) / 100.0);
3335
3336 log_debug("[M] %s score: %.3f", city_name_get(pcity), score);
3337
3338 /* set migration score for the city */
3339 pcity->server.migration_score = score;
3340 /* set the turn, when the score was calculated */
3341 pcity->server.mgr_score_calc_turn = game.info.turn;
3342
3343 return score;
3344 }
3345
3346 /**************************************************************************
3347 Do the migrations between the cities that overlap, if the growth of the
3348 target city is not blocked due to a missing improvement or missing food.
3349
3350 Returns TRUE if migration occurred.
3351 **************************************************************************/
do_city_migration(struct city * pcity_from,struct city * pcity_to)3352 static bool do_city_migration(struct city *pcity_from,
3353 struct city *pcity_to)
3354 {
3355 struct player *pplayer_from, *pplayer_to, *pplayer_citizen;
3356 struct tile *ptile_from, *ptile_to;
3357 char name_from[MAX_LEN_LINK], name_to[MAX_LEN_LINK];
3358 const char *nation_from, *nation_to;
3359 struct city *rcity = NULL;
3360 bool incr_success;
3361 int to_id = pcity_to->id;
3362
3363 if (!pcity_from || !pcity_to) {
3364 return FALSE;
3365 }
3366
3367 pplayer_from = city_owner(pcity_from);
3368 pplayer_citizen = pplayer_from;
3369 pplayer_to = city_owner(pcity_to);
3370 /* We copy that, because city_link always returns the same pointer. */
3371 sz_strlcpy(name_from, city_link(pcity_from));
3372 sz_strlcpy(name_to, city_link(pcity_to));
3373 nation_from = nation_adjective_for_player(pplayer_from);
3374 nation_to = nation_adjective_for_player(pplayer_to);
3375 ptile_from = city_tile(pcity_from);
3376 ptile_to = city_tile(pcity_to);
3377
3378 /* check food supply in the receiver city */
3379 if (game.server.mgr_foodneeded) {
3380 bool migration = FALSE;
3381
3382 if (pcity_to->surplus[O_FOOD] >= game.info.food_cost) {
3383 migration = TRUE;
3384 } else {
3385 /* check if there is a free tile for the new citizen which, when worked,
3386 * leads to zero or positive food surplus for the enlarged city */
3387 int max_food_tile = -1; /* no free tile */
3388 city_tile_iterate(city_map_radius_sq_get(pcity_to),
3389 city_tile(pcity_to), ptile) {
3390 if (city_can_work_tile(pcity_to, ptile)
3391 && tile_worked(ptile) != pcity_to) {
3392 /* Safest assumption is that city won't be celebrating once an
3393 * additional citizen is added */
3394 max_food_tile = MAX(max_food_tile,
3395 city_tile_output(pcity_to, ptile, FALSE, O_FOOD));
3396 }
3397 } city_tile_iterate_end;
3398 if (max_food_tile >= 0
3399 && pcity_to->surplus[O_FOOD] + max_food_tile >= game.info.food_cost) {
3400 migration = TRUE;
3401 }
3402 }
3403
3404 if (!migration) {
3405 /* insufficiency food in receiver city; no additional citizens */
3406 if (pplayer_from == pplayer_to) {
3407 /* migration between one nation */
3408 notify_player(pplayer_to, ptile_to, E_CITY_TRANSFER, ftc_server,
3409 /* TRANS: From <city1> to <city2>. */
3410 _("Migrants from %s can't go to %s because there is "
3411 "not enough food available!"),
3412 name_from, name_to);
3413 } else {
3414 /* migration between different nations */
3415 notify_player(pplayer_from, ptile_to, E_CITY_TRANSFER, ftc_server,
3416 /* TRANS: From <city1> to <city2> (<city2 nation adjective>). */
3417 _("Migrants from %s can't go to %s (%s) because there "
3418 "is not enough food available!"),
3419 name_from, name_to, nation_to);
3420 notify_player(pplayer_to, ptile_to, E_CITY_TRANSFER, ftc_server,
3421 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
3422 _("Migrants from %s (%s) can't go to %s because there "
3423 "is not enough food available!"),
3424 name_from, nation_from, name_to);
3425 }
3426
3427 return FALSE;
3428 }
3429 }
3430
3431 if (!city_can_grow_to(pcity_to, city_size_get(pcity_to) + 1)) {
3432 /* receiver city can't grow */
3433 if (pplayer_from == pplayer_to) {
3434 /* migration between one nation */
3435 notify_player(pplayer_to, ptile_to, E_CITY_TRANSFER, ftc_server,
3436 /* TRANS: From <city1> to <city2>. */
3437 _("Migrants from %s can't go to %s because it needs "
3438 "an improvement to grow!"),
3439 name_from, name_to);
3440 } else {
3441 /* migration between different nations */
3442 notify_player(pplayer_from, ptile_to, E_CITY_TRANSFER, ftc_server,
3443 /* TRANS: From <city1> to <city2> of <city2 nation adjective>. */
3444 _("Migrants from %s can't go to %s (%s) because it "
3445 "needs an improvement to grow!"),
3446 name_from, name_to, nation_to);
3447 notify_player(pplayer_to, ptile_to, E_CITY_TRANSFER, ftc_server,
3448 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
3449 _("Migrants from %s (%s) can't go to %s because it "
3450 "needs an improvement to grow!"),
3451 name_from, nation_from, name_to);
3452 }
3453
3454 return FALSE;
3455 }
3456
3457 /* reduce size of giver */
3458 if (city_size_get(pcity_from) == 1) {
3459
3460 if (game.info.citizen_nationality) {
3461 /* Preserve nationality of city's only citizen */
3462 pplayer_citizen = player_slot_get_player(citizens_random(pcity_from));
3463 }
3464
3465 /* do not destroy wonders */
3466 city_built_iterate(pcity_from, pimprove) {
3467 if (is_wonder(pimprove)) {
3468 return FALSE;
3469 }
3470 } city_built_iterate_end;
3471
3472 /* find closest city other of the same player than pcity_from */
3473 rcity = find_closest_city(ptile_from, pcity_from, pplayer_from, FALSE,
3474 FALSE, FALSE, TRUE, FALSE, NULL);
3475
3476 if (rcity) {
3477 int id = pcity_from->id;
3478
3479 /* transfer all units to the closest city */
3480 transfer_city_units(pplayer_from, pplayer_from,
3481 pcity_from->units_supported, rcity, pcity_from,
3482 -1, TRUE);
3483 sz_strlcpy(name_from, city_tile_link(pcity_from));
3484
3485 script_server_signal_emit("city_size_change", 3,
3486 API_TYPE_CITY, pcity_from,
3487 API_TYPE_INT, -1,
3488 API_TYPE_STRING, "migration_from");
3489
3490 if (city_exist(id)) {
3491 script_server_signal_emit("city_destroyed", 3,
3492 API_TYPE_CITY, pcity_from,
3493 API_TYPE_PLAYER, pcity_from->owner,
3494 API_TYPE_PLAYER, NULL);
3495
3496 if (city_exist(id)) {
3497 remove_city(pcity_from);
3498 }
3499 }
3500
3501 notify_player(pplayer_from, ptile_from, E_CITY_LOST, ftc_server,
3502 _("%s was disbanded by its citizens."),
3503 name_from);
3504 } else {
3505 /* it's the only city of the nation */
3506 return FALSE;
3507 }
3508 } else {
3509 /* the migrants take half of the food box with them (this prevents
3510 * migration -> grow -> migration -> ... cycles) */
3511 pcity_from->food_stock /= 2;
3512
3513 if (game.info.citizen_nationality) {
3514 /* Those citizens that are from the target nation are most
3515 * ones migrating. */
3516 if (citizens_nation_get(pcity_from, pplayer_to->slot) > 0) {
3517 pplayer_citizen = pplayer_to;
3518 } else if (!citizens_nation_get(pcity_from, pplayer_citizen->slot)) {
3519 /* No native citizens at all in the city, choose random foreigner */
3520 struct player_slot *cit_slot = citizens_random(pcity_from);
3521
3522 pplayer_citizen = player_slot_get_player(cit_slot);
3523 }
3524 /* This should be followed by city_reduce_size(). */
3525 citizens_nation_add(pcity_from, pplayer_citizen->slot, -1);
3526 }
3527 city_reduce_size(pcity_from, 1, pplayer_from, "migration_from");
3528 city_refresh_vision(pcity_from);
3529 if (city_refresh(pcity_from)) {
3530 auto_arrange_workers(pcity_from);
3531 }
3532 }
3533
3534 /* This should be _before_ the size of the city is increased. Thus, the
3535 * order of the messages is correct (1: migration; 2: increased size). */
3536 if (pplayer_from == pplayer_to) {
3537 /* migration between one nation */
3538 notify_player(pplayer_from, ptile_to, E_CITY_TRANSFER, ftc_server,
3539 /* TRANS: From <city1> to <city2>. */
3540 _("Migrants from %s moved to %s in search of a better "
3541 "life."), name_from, name_to);
3542 } else {
3543 /* migration between different nations */
3544 notify_player(pplayer_from, ptile_to, E_CITY_TRANSFER, ftc_server,
3545 /* TRANS: From <city1> to <city2> (<city2 nation adjective>). */
3546 _("Migrants from %s moved to %s (%s) in search of a "
3547 "better life."),
3548 name_from, name_to, nation_to);
3549 notify_player(pplayer_to, ptile_to, E_CITY_TRANSFER, ftc_server,
3550 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
3551 _("Migrants from %s (%s) moved to %s in search of a "
3552 "better life."),
3553 name_from, nation_from, name_to);
3554 }
3555
3556 /* raise size of receiver city */
3557 if (city_exist(to_id)) {
3558 incr_success = city_increase_size(pcity_to, pplayer_citizen);
3559 if (city_exist(to_id)) {
3560 city_refresh_vision(pcity_to);
3561 if (city_refresh(pcity_to)) {
3562 auto_arrange_workers(pcity_to);
3563 }
3564 if (incr_success) {
3565 script_server_signal_emit("city_size_change", 3,
3566 API_TYPE_CITY, pcity_to,
3567 API_TYPE_INT, 1,
3568 API_TYPE_STRING, "migration_to");
3569 }
3570 }
3571 }
3572
3573 log_debug("[M] T%d migration successful (%s -> %s)",
3574 game.info.turn, name_from, name_to);
3575
3576 return TRUE;
3577 }
3578
3579 /**************************************************************************
3580 Check for citizens who want to migrate between the cities that overlap.
3581 Migrants go to the city with higher score, if the growth of the target
3582 city is not blocked due to a missing improvement.
3583
3584 The following setting are used:
3585
3586 'game.server.mgr_turninterval' controls the number of turns between
3587 migration checks for one city (counted from the founding). If this
3588 setting is zero, or it is the first turn (T0), migration does no occur.
3589
3590 'game.server.mgr_distance' is the maximal distance for migration.
3591
3592 'game.server.mgr_nationchance' gives the chance for migration within one
3593 nation.
3594
3595 'game.server.mgr_worldchance' gives the chance for migration between all
3596 nations.
3597
3598 Returns TRUE iff there has been INTERNATIONAL migration.
3599 **************************************************************************/
check_city_migrations(void)3600 bool check_city_migrations(void)
3601 {
3602 bool internat = FALSE;
3603
3604 if (!game.server.migration) {
3605 return FALSE;
3606 }
3607
3608 if (game.server.mgr_turninterval <= 0
3609 || (game.server.mgr_worldchance <= 0
3610 && game.server.mgr_nationchance <= 0)) {
3611 return FALSE;
3612 }
3613
3614 /* check for migration */
3615 players_iterate(pplayer) {
3616 if (!pplayer->cities) {
3617 continue;
3618 }
3619
3620 if (check_city_migrations_player(pplayer)) {
3621 internat = TRUE;
3622 }
3623 } players_iterate_end;
3624
3625 return internat;
3626 }
3627
3628 /**************************************************************************
3629 Disaster has hit a city. Apply its effects.
3630 **************************************************************************/
apply_disaster(struct city * pcity,struct disaster_type * pdis)3631 static void apply_disaster(struct city *pcity, struct disaster_type *pdis)
3632 {
3633 struct player *pplayer = city_owner(pcity);
3634 struct tile *ptile = city_tile(pcity);
3635 bool had_internal_effect = FALSE;
3636
3637 log_debug("%s at %s", disaster_rule_name(pdis), city_name_get(pcity));
3638
3639 notify_player(pplayer, ptile, E_DISASTER,
3640 ftc_server,
3641 /* TRANS: Disasters such as Earthquake */
3642 _("%s was hit by %s."), city_name_get(pcity),
3643 disaster_name_translation(pdis));
3644
3645 if (disaster_has_effect(pdis, DE_POLLUTION)) {
3646 if (place_pollution(pcity, EC_POLLUTION)) {
3647 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
3648 _("Pollution near %s."), city_link(pcity));
3649 had_internal_effect = TRUE;
3650 }
3651 }
3652
3653 if (disaster_has_effect(pdis, DE_FALLOUT)) {
3654 if (place_pollution(pcity, EC_FALLOUT)) {
3655 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
3656 _("Fallout near %s."), city_link(pcity));
3657 had_internal_effect = TRUE;
3658 }
3659 }
3660
3661 if (disaster_has_effect(pdis, DE_REDUCE_DESTROY)
3662 || (disaster_has_effect(pdis, DE_REDUCE_POP)
3663 && pcity->size > 1)) {
3664 if (!city_reduce_size(pcity, 1, NULL, "disaster")) {
3665 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
3666 /* TRANS: "Industrial Accident destroys Bogota entirely." */
3667 _("%s destroys %s entirely."),
3668 disaster_name_translation(pdis), city_link(pcity));
3669 pcity = NULL;
3670 } else {
3671 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
3672 /* TRANS: "Nuclear Accident ... Montreal." */
3673 _("%s causes population loss in %s."),
3674 disaster_name_translation(pdis), city_link(pcity));
3675 }
3676
3677 had_internal_effect = TRUE;
3678 }
3679
3680 if (pcity && disaster_has_effect(pdis, DE_DESTROY_BUILDING)) {
3681 int total = 0;
3682 struct impr_type *imprs[B_LAST];
3683
3684 city_built_iterate(pcity, pimprove) {
3685 if (is_improvement(pimprove)) {
3686 imprs[total++] = pimprove;
3687 }
3688 } city_built_iterate_end;
3689
3690 if (total > 0) {
3691 int num = fc_rand(total);
3692
3693 building_lost(pcity, imprs[num]);
3694
3695 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
3696 /* TRANS: second %s is the name of a city improvement */
3697 _("%s destroys %s in %s."),
3698 disaster_name_translation(pdis),
3699 improvement_name_translation(imprs[num]),
3700 city_link(pcity));
3701
3702 had_internal_effect = TRUE;
3703 }
3704 }
3705
3706 if (pcity && disaster_has_effect(pdis, DE_EMPTY_FOODSTOCK)) {
3707 if (pcity->food_stock > 0) {
3708 pcity->food_stock = 0;
3709
3710 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
3711 /* TRANS: %s is a city name */
3712 _("All stored food destroyed in %s."), city_link(pcity));
3713
3714 had_internal_effect = TRUE;
3715 }
3716 }
3717
3718 if (pcity && disaster_has_effect(pdis, DE_EMPTY_PRODSTOCK)) {
3719 if (pcity->shield_stock > 0) {
3720 char prod[256];
3721
3722 pcity->shield_stock = 0;
3723 nullify_prechange_production(pcity); /* Make it impossible to recover */
3724
3725 universal_name_translation(&pcity->production, prod, sizeof(prod));
3726 notify_player(pplayer, ptile, E_DISASTER, ftc_server,
3727 /* TRANS: "Production of Colossus in Rhodes destroyed." */
3728 _("Production of %s in %s destroyed."),
3729 prod, city_link(pcity));
3730
3731 had_internal_effect = TRUE;
3732 }
3733 }
3734
3735 script_server_signal_emit("disaster_occurred", 3,
3736 API_TYPE_DISASTER, pdis,
3737 API_TYPE_CITY, pcity,
3738 API_TYPE_BOOL, had_internal_effect);
3739 script_server_signal_emit("disaster", 2,
3740 API_TYPE_DISASTER, pdis,
3741 API_TYPE_CITY, pcity);
3742 }
3743
3744 /**************************************************************************
3745 Check for any disasters hitting any city, and apply those disasters.
3746 **************************************************************************/
check_disasters(void)3747 void check_disasters(void)
3748 {
3749 if (game.info.disasters == 0) {
3750 /* Shortcut out as no disaster is possible. */
3751 return;
3752 }
3753
3754 players_iterate(pplayer) {
3755 /* Safe city iterator needed as disaster may destroy city */
3756 city_list_iterate_safe(pplayer->cities, pcity) {
3757 int id = pcity->id;
3758
3759 disaster_type_iterate(pdis) {
3760 if (city_exist(id)) {
3761 /* City survived earlier disasters. */
3762 int probability = game.info.disasters * pdis->frequency;
3763 int result = fc_rand(DISASTER_BASE_RARITY);
3764
3765 if (result < probability) {
3766 if (can_disaster_happen(pdis, pcity)) {
3767 apply_disaster(pcity, pdis);
3768 }
3769 }
3770 }
3771 } disaster_type_iterate_end;
3772 } city_list_iterate_safe_end;
3773 } players_iterate_end;
3774 }
3775
3776 /**************************************************************************
3777 Check for migration for each city of one player.
3778
3779 For each city of the player do:
3780 * check each tile within GAME_MAX_MGR_DISTANCE for a city
3781 * if a city is found check the distance
3782 * compare the migration score
3783 **************************************************************************/
check_city_migrations_player(const struct player * pplayer)3784 static bool check_city_migrations_player(const struct player *pplayer)
3785 {
3786 char city_link_text[MAX_LEN_LINK];
3787 float best_city_player_score, best_city_world_score;
3788 struct city *best_city_player, *best_city_world, *acity;
3789 float score_from, score_tmp, weight;
3790 int dist, mgr_dist;
3791 bool internat = FALSE;
3792
3793 /* check for each city
3794 * city_list_iterate_safe_end must be used because we could
3795 * remove one city from the list */
3796 city_list_iterate_safe(pplayer->cities, pcity) {
3797 /* no migration out of the capital */
3798 if (is_capital(pcity)) {
3799 continue;
3800 }
3801
3802 /* check only each (game.server.mgr_turninterval) turn
3803 * (counted from the funding turn) and do not migrate
3804 * the same turn a city is founded */
3805 if (game.info.turn == pcity->turn_founded
3806 || ((game.info.turn - pcity->turn_founded)
3807 % game.server.mgr_turninterval) != 0) {
3808 continue;
3809 }
3810
3811 best_city_player_score = 0.0;
3812 best_city_world_score = 0.0;
3813 best_city_player = NULL;
3814 best_city_world = NULL;
3815
3816 /* score of the actual city
3817 * taking into account a persistence factor of 3 */
3818 score_from = city_migration_score(pcity) * 3;
3819
3820 log_debug("[M] T%d check city: %s score: %6.3f (%s)",
3821 game.info.turn, city_name_get(pcity), score_from,
3822 player_name(pplayer));
3823
3824 /* consider all cities within the maximal possible distance
3825 * (= CITY_MAP_MAX_RADIUS + GAME_MAX_MGR_DISTANCE) */
3826 iterate_outward(city_tile(pcity), CITY_MAP_MAX_RADIUS
3827 + GAME_MAX_MGR_DISTANCE, ptile) {
3828 acity = tile_city(ptile);
3829
3830 if (!acity || acity == pcity) {
3831 /* no city or the city in the center */
3832 continue;
3833 }
3834
3835 /* Calculate the migration distance. The value of
3836 * game.server.mgr_distance is added to the current city radius. If the
3837 * distance between both cities is lower or equal than this value,
3838 * migration is possible. */
3839 mgr_dist = (int)sqrt((double)MAX(city_map_radius_sq_get(acity),0))
3840 + game.server.mgr_distance;
3841
3842 /* distance between the two cities */
3843 dist = real_map_distance(city_tile(pcity), city_tile(acity));
3844
3845 if (dist > mgr_dist) {
3846 /* to far away */
3847 continue;
3848 }
3849
3850 /* score of the second city, weighted by the distance */
3851 weight = ((float) (mgr_dist + 1 - dist) / (float) (mgr_dist + 1));
3852 score_tmp = city_migration_score(acity) * weight;
3853
3854 log_debug("[M] T%d - compare city: %s (%s) dist: %d mgr_dist: %d "
3855 "score: %6.3f", game.info.turn, city_name_get(acity),
3856 player_name(city_owner(acity)), dist, mgr_dist, score_tmp);
3857
3858 if (game.server.mgr_nationchance > 0 && city_owner(acity) == pplayer) {
3859 /* migration between cities of the same owner */
3860 if (score_tmp > score_from && score_tmp > best_city_player_score) {
3861 /* select the best! */
3862 best_city_player_score = score_tmp;
3863 best_city_player = acity;
3864
3865 log_debug("[M] T%d - best city (player): %s (%s) score: "
3866 "%6.3f (> %6.3f)", game.info.turn,
3867 city_name_get(best_city_player), player_name(pplayer),
3868 best_city_player_score, score_from);
3869 }
3870 } else if (game.server.mgr_worldchance > 0
3871 && city_owner(acity) != pplayer) {
3872 /* migration between cities of different owners */
3873 if (game.info.citizen_nationality) {
3874 /* Modify the score if citizens could migrate to a city of their
3875 * original nation. */
3876 if (citizens_nation_get(pcity, city_owner(acity)->slot) > 0) {
3877 score_tmp *= 2;
3878 }
3879 }
3880
3881 if (score_tmp > score_from && score_tmp > best_city_world_score) {
3882 /* select the best! */
3883 best_city_world_score = score_tmp;
3884 best_city_world = acity;
3885
3886 log_debug("[M] T%d - best city (world): %s (%s) score: "
3887 "%6.3f (> %6.3f)", game.info.turn,
3888 city_name_get(best_city_world),
3889 player_name(city_owner(best_city_world)),
3890 best_city_world_score, score_from);
3891 }
3892 }
3893 } iterate_outward_end;
3894
3895 if (best_city_player_score > 0) {
3896 /* first, do the migration within one nation */
3897 if (fc_rand(100) >= game.server.mgr_nationchance) {
3898 /* no migration */
3899 /* N.B.: city_link always returns the same pointer. */
3900 sz_strlcpy(city_link_text, city_link(pcity));
3901 notify_player(pplayer, city_tile(pcity), E_CITY_TRANSFER, ftc_server,
3902 _("Citizens of %s are thinking about migrating to %s "
3903 "for a better life."),
3904 city_link_text, city_link(best_city_player));
3905 } else {
3906 do_city_migration(pcity, best_city_player);
3907 }
3908
3909 /* stop here */
3910 continue;
3911 }
3912
3913 if (best_city_world_score > 0) {
3914 /* second, do the migration between all nations */
3915 if (fc_rand(100) >= game.server.mgr_worldchance) {
3916 const char *nname;
3917
3918 nname = nation_adjective_for_player(city_owner(best_city_world));
3919 /* no migration */
3920 /* N.B.: city_link always returns the same pointer. */
3921 sz_strlcpy(city_link_text, city_link(pcity));
3922 notify_player(pplayer, city_tile(pcity), E_CITY_TRANSFER, ftc_server,
3923 /* TRANS: <city1> to <city2> (<city2 nation adjective>). */
3924 _("Citizens of %s are thinking about migrating to %s "
3925 "(%s) for a better life."),
3926 city_link_text, city_link(best_city_world), nname);
3927 } else {
3928 do_city_migration(pcity, best_city_world);
3929 internat = TRUE;
3930 }
3931
3932 /* stop here */
3933 continue;
3934 }
3935 } city_list_iterate_safe_end;
3936
3937 return internat;
3938 }
3939
3940 /**************************************************************************
3941 Recheck and store style of the city.
3942 **************************************************************************/
city_style_refresh(struct city * pcity)3943 void city_style_refresh(struct city *pcity)
3944 {
3945 pcity->style = city_style(pcity);
3946 }
3947