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)
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 ***********************************************************************/
15 #include <fc_config.h>
21 #include <math.h> /* exp, sqrt */
35 #include "achievements.h"
36 #include "actiontools.h"
45 #include "government.h"
50 #include "specialist.h"
52 #include "traderoutes.h"
56 /* common/scriptcore */
57 #include "luascript_types.h"
60 #include "citizenshand.h"
61 #include "citytools.h"
66 #include "sanitycheck.h"
67 #include "spacerace.h"
70 #include "techtools.h"
71 #include "unittools.h"
75 #include "advbuilding.h"
77 #include "autosettlers.h"
79 /* server/scripting */
80 #include "script_server.h"
82 /* Queue for pending city_refresh() */
83 static struct city_list
*city_refresh_queue
= NULL
;
85 /* The game is currently considering to remove the listed units because of
86 * missing gold upkeep. A unit ends up here if it has gold upkeep that
87 * can't be payed. A random unit in the list will be removed until the
88 * problem is solved. */
89 static struct unit_list
*uk_rem_gold
= NULL
;
91 static void check_pollution(struct city
*pcity
);
92 static void city_populate(struct city
*pcity
, struct player
*nationality
);
94 static bool worklist_change_build_target(struct player
*pplayer
,
97 static bool city_distribute_surplus_shields(struct player
*pplayer
,
99 static bool city_build_building(struct player
*pplayer
, struct city
*pcity
);
100 static bool city_build_unit(struct player
*pplayer
, struct city
*pcity
);
101 static bool city_build_stuff(struct player
*pplayer
, struct city
*pcity
);
102 static struct impr_type
*building_upgrades_to(struct city
*pcity
,
103 struct impr_type
*pimprove
);
104 static void upgrade_building_prod(struct city
*pcity
);
105 static struct unit_type
*unit_upgrades_to(struct city
*pcity
,
106 struct unit_type
*id
);
107 static void upgrade_unit_prod(struct city
*pcity
);
109 /* Helper struct for associating a building to a city. */
112 struct impr_type
*pimprove
;
115 #define SPECLIST_TAG cityimpr
116 #define SPECLIST_TYPE struct cityimpr
117 #include "speclist.h"
119 #define cityimpr_list_iterate(cityimprlist, pcityimpr) \
120 TYPED_LIST_ITERATE(struct cityimpr, cityimprlist, pcityimpr)
121 #define cityimpr_list_iterate_end LIST_ITERATE_END
123 static bool sell_random_building(struct player
*pplayer
,
124 struct cityimpr_list
*imprs
);
125 static struct unit
*sell_random_unit(struct player
*pplayer
,
126 struct unit_list
*punitlist
);
128 static citizens
city_reduce_specialists(struct city
*pcity
, citizens change
);
129 static citizens
city_reduce_workers(struct city
*pcity
, citizens change
);
131 static bool city_balance_treasury_buildings(struct city
*pcity
);
132 static bool city_balance_treasury_units(struct city
*pcity
);
133 static bool player_balance_treasury_units_and_buildings
134 (struct player
*pplayer
);
135 static bool player_balance_treasury_units(struct player
*pplayer
);
137 static bool disband_city(struct city
*pcity
);
139 static void define_orig_production_values(struct city
*pcity
);
140 static void update_city_activity(struct city
*pcity
);
141 static void nullify_caravan_and_disband_plus(struct city
*pcity
);
142 static bool city_illness_check(const struct city
* pcity
);
144 static float city_migration_score(struct city
*pcity
);
145 static bool do_city_migration(struct city
*pcity_from
,
146 struct city
*pcity_to
);
147 static bool check_city_migrations_player(const struct player
*pplayer
);
149 /**************************************************************************
150 Updates unit upkeeps and city internal cached data. Returns whether
151 city radius has changed.
152 **************************************************************************/
153 bool city_refresh(struct city
*pcity
)
157 pcity
->server
.needs_refresh
= FALSE
;
159 retval
= city_map_update_radius_sq(pcity
);
160 city_units_upkeep(pcity
); /* update unit upkeep */
161 city_refresh_from_main_map(pcity
, NULL
);
162 city_style_refresh(pcity
);
165 /* Force a sync of the city after the change. */
166 send_city_info(city_owner(pcity
), pcity
);
172 /**************************************************************************
173 Called on government change or wonder completion or stuff like that
175 **************************************************************************/
176 void city_refresh_for_player(struct player
*pplayer
)
178 conn_list_do_buffer(pplayer
->connections
);
179 city_list_iterate(pplayer
->cities
, pcity
) {
180 if (city_refresh(pcity
)) {
181 auto_arrange_workers(pcity
);
183 send_city_info(pplayer
, pcity
);
184 } city_list_iterate_end
;
185 conn_list_do_unbuffer(pplayer
->connections
);
188 /****************************************************************************
189 Queue pending city_refresh() for later.
190 ****************************************************************************/
191 void city_refresh_queue_add(struct city
*pcity
)
193 if (NULL
== city_refresh_queue
) {
194 city_refresh_queue
= city_list_new();
195 } else if (city_list_find_number(city_refresh_queue
, pcity
->id
)) {
199 city_list_prepend(city_refresh_queue
, pcity
);
200 pcity
->server
.needs_refresh
= TRUE
;
203 /*************************************************************************
204 Refresh the listed cities.
205 Called after significant changes to borders, and arranging workers.
206 *************************************************************************/
207 void city_refresh_queue_processing(void)
209 if (NULL
== city_refresh_queue
) {
213 city_list_iterate(city_refresh_queue
, pcity
) {
214 if (pcity
->server
.needs_refresh
) {
215 if (city_refresh(pcity
)) {
216 auto_arrange_workers(pcity
);
218 send_city_info(city_owner(pcity
), pcity
);
220 } city_list_iterate_end
;
222 city_list_destroy(city_refresh_queue
);
223 city_refresh_queue
= NULL
;
226 /**************************************************************************
227 Automatically sells obsolete buildings from city.
228 **************************************************************************/
229 void remove_obsolete_buildings_city(struct city
*pcity
, bool refresh
)
231 struct player
*pplayer
= city_owner(pcity
);
234 city_built_iterate(pcity
, pimprove
) {
235 if (improvement_obsolete(pplayer
, pimprove
, pcity
)
236 && can_city_sell_building(pcity
, pimprove
)) {
239 do_sell_building(pplayer
, pcity
, pimprove
);
240 sgold
= impr_sell_gold(pimprove
);
241 notify_player(pplayer
, city_tile(pcity
), E_IMP_SOLD
, ftc_server
,
242 PL_("%s is selling %s (obsolete) for %d.",
243 "%s is selling %s (obsolete) for %d.",
246 improvement_name_translation(pimprove
),
250 } city_built_iterate_end
;
252 if (sold
&& refresh
) {
253 if (city_refresh(pcity
)) {
254 auto_arrange_workers(pcity
);
256 send_city_info(pplayer
, pcity
);
257 send_player_info_c(pplayer
, NULL
); /* Send updated gold to all */
261 /**************************************************************************
262 Sell obsolete buildings from all cities of the player
263 **************************************************************************/
264 void remove_obsolete_buildings(struct player
*pplayer
)
266 city_list_iterate(pplayer
->cities
, pcity
) {
267 remove_obsolete_buildings_city(pcity
, FALSE
);
268 } city_list_iterate_end
;
271 /**************************************************************************
272 Rearrange workers according to a cm_result struct. The caller must make
273 sure that the result is valid.
274 **************************************************************************/
275 void apply_cmresult_to_city(struct city
*pcity
,
276 const struct cm_result
*cmr
)
278 struct tile
*pcenter
= city_tile(pcity
);
280 /* Now apply results */
281 city_tile_iterate_skip_free_worked(city_map_radius_sq_get(pcity
), pcenter
,
283 struct city
*pwork
= tile_worked(ptile
);
285 if (cmr
->worker_positions
[idx
]) {
287 city_map_update_worker(pcity
, ptile
);
289 fc_assert(pwork
== pcity
);
292 if (pwork
== pcity
) {
293 city_map_update_empty(pcity
, ptile
);
296 } city_tile_iterate_skip_free_worked_end
;
298 specialist_type_iterate(sp
) {
299 pcity
->specialists
[sp
] = cmr
->specialists
[sp
];
300 } specialist_type_iterate_end
;
303 /**************************************************************************
304 Call sync_cities() to send the affected cities to the clients.
305 **************************************************************************/
306 void auto_arrange_workers(struct city
*pcity
)
308 struct cm_parameter cmp
;
309 struct cm_result
*cmr
;
311 /* See comment in freeze_workers(): we can't rearrange while
312 * workers are frozen (i.e. multiple updates need to be done). */
313 if (pcity
->server
.workers_frozen
> 0) {
314 pcity
->server
.needs_arrange
= TRUE
;
317 TIMING_LOG(AIT_CITIZEN_ARRANGE
, TIMER_START
);
319 /* Freeze the workers and make sure all the tiles around the city
320 * are up to date. Then thaw, but hackishly make sure that thaw
321 * doesn't call us recursively, which would waste time. */
322 city_freeze_workers(pcity
);
323 pcity
->server
.needs_arrange
= FALSE
;
325 city_map_update_all(pcity
);
327 pcity
->server
.needs_arrange
= FALSE
;
328 city_thaw_workers(pcity
);
330 /* Now start actually rearranging. */
333 sanity_check_city(pcity
);
334 cm_clear_cache(pcity
);
336 cm_init_parameter(&cmp
);
337 cmp
.require_happy
= FALSE
;
338 cmp
.allow_disorder
= FALSE
;
339 cmp
.allow_specialists
= TRUE
;
341 /* We used to look at pplayer->ai.xxx_priority to determine the values
342 * to be used here. However that doesn't work at all because those values
343 * are on a different scale. Later the ai may wish to adjust its
344 * priorities - this should be done via a separate set of variables. */
345 if (city_size_get(pcity
) > 1) {
346 if (city_size_get(pcity
) <= game
.info
.notradesize
) {
347 cmp
.factor
[O_FOOD
] = 15;
349 if (city_granary_size(city_size_get(pcity
)) == pcity
->food_stock
) {
350 /* We don't need more food if the granary is full. */
351 cmp
.factor
[O_FOOD
] = 0;
353 cmp
.factor
[O_FOOD
] = 10;
357 /* Growing to size 2 is the highest priority. */
358 cmp
.factor
[O_FOOD
] = 20;
360 cmp
.factor
[O_SHIELD
] = 5;
361 cmp
.factor
[O_TRADE
] = 0; /* Trade only provides gold/science. */
362 cmp
.factor
[O_GOLD
] = 2;
363 cmp
.factor
[O_LUXURY
] = 0; /* Luxury only influences happiness. */
364 cmp
.factor
[O_SCIENCE
] = 2;
365 cmp
.happy_factor
= 0;
367 if (city_granary_size(city_size_get(pcity
)) == pcity
->food_stock
) {
368 cmp
.minimal_surplus
[O_FOOD
] = 0;
370 cmp
.minimal_surplus
[O_FOOD
] = 1;
372 cmp
.minimal_surplus
[O_SHIELD
] = 1;
373 cmp
.minimal_surplus
[O_TRADE
] = 0;
374 cmp
.minimal_surplus
[O_GOLD
] = -FC_INFINITY
;
375 cmp
.minimal_surplus
[O_LUXURY
] = 0;
376 cmp
.minimal_surplus
[O_SCIENCE
] = 0;
378 /* This must be after city_refresh() so that the result gets created for the right
380 cmr
= cm_result_new(pcity
);
381 cm_query_result(pcity
, &cmp
, cmr
);
383 if (!cmr
->found_a_valid
) {
384 /* Drop surpluses and try again. */
385 cmp
.minimal_surplus
[O_FOOD
] = 0;
386 cmp
.minimal_surplus
[O_SHIELD
] = 0;
387 cmp
.minimal_surplus
[O_GOLD
] = -FC_INFINITY
;
388 cm_query_result(pcity
, &cmp
, cmr
);
390 if (!cmr
->found_a_valid
) {
391 /* Emergency management. Get _some_ result. This doesn't use
392 * cm_init_emergency_parameter so we can keep the factors from
394 output_type_iterate(o
) {
395 cmp
.minimal_surplus
[o
] = MIN(cmp
.minimal_surplus
[o
],
396 MIN(pcity
->surplus
[o
], 0));
397 } output_type_iterate_end
;
398 cmp
.require_happy
= FALSE
;
399 cmp
.allow_disorder
= is_ai(city_owner(pcity
)) ? FALSE
: TRUE
;
400 cm_query_result(pcity
, &cmp
, cmr
);
402 if (!cmr
->found_a_valid
) {
403 /* Should never happen. */
404 CITY_LOG(LOG_DEBUG
, pcity
, "emergency management");
405 cm_init_emergency_parameter(&cmp
);
406 cm_query_result(pcity
, &cmp
, cmr
);
408 fc_assert_ret(cmr
->found_a_valid
);
410 apply_cmresult_to_city(pcity
, cmr
);
412 if (pcity
->server
.debug
) {
413 /* Print debug output if requested. */
414 cm_print_city(pcity
);
415 cm_print_result(cmr
);
418 if (city_refresh(pcity
)) {
419 log_error("%s radius changed when already arranged workers.",
420 city_name_get(pcity
));
421 /* Can't do anything - don't want to enter infinite recursive loop
422 * by trying to arrange workers more. */
424 sanity_check_city(pcity
);
426 cm_result_destroy(cmr
);
427 TIMING_LOG(AIT_CITIZEN_ARRANGE
, TIMER_STOP
);
430 /****************************************************************************
431 Notices about cities that should be sent to all players.
432 ****************************************************************************/
433 static void city_global_turn_notify(struct conn_list
*dest
)
435 cities_iterate(pcity
) {
436 struct impr_type
*pimprove
= pcity
->production
.value
.building
;
438 if (VUT_IMPROVEMENT
== pcity
->production
.kind
439 && is_great_wonder(pimprove
)
440 && great_wonder_is_available(pimprove
)
441 && (1 >= city_production_turns_to_build(pcity
, TRUE
))) {
442 notify_conn(dest
, city_tile(pcity
),
443 E_WONDER_WILL_BE_BUILT
, ftc_server
,
444 _("Notice: Wonder %s in %s will be finished next turn."),
445 improvement_name_translation(pimprove
), city_link(pcity
));
447 } cities_iterate_end
;
450 /****************************************************************************
451 Send turn notifications for specified city to specified connections.
452 If 'pplayer' is not NULL, the message will be cached for this player.
453 ****************************************************************************/
454 static void city_turn_notify(const struct city
*pcity
,
455 struct conn_list
*dest
,
456 const struct player
*cache_for_player
)
458 struct impr_type
*pimprove
= pcity
->production
.value
.building
;
459 struct packet_chat_msg packet
;
460 int turns_growth
, turns_granary
;
462 if (0 < pcity
->surplus
[O_FOOD
]) {
463 turns_growth
= (city_granary_size(city_size_get(pcity
))
464 - pcity
->food_stock
- 1) / pcity
->surplus
[O_FOOD
];
466 if (0 == get_city_bonus(pcity
, EFT_GROWTH_FOOD
)
467 && 0 < get_current_construction_bonus(pcity
, EFT_GROWTH_FOOD
,
469 && 0 < pcity
->surplus
[O_SHIELD
]) {
470 /* From the check above, the surplus must always be positive. */
471 turns_granary
= (impr_build_shield_cost(pimprove
)
472 - pcity
->shield_stock
) / pcity
->surplus
[O_SHIELD
];
473 /* If growth and granary completion occur simultaneously, granary
474 * preserves food. -AJS. */
475 if (5 > turns_growth
&& 5 > turns_granary
476 && turns_growth
< turns_granary
) {
477 package_event(&packet
, city_tile(pcity
),
478 E_CITY_GRAN_THROTTLE
, ftc_server
,
479 _("Suggest throttling growth in %s to use %s "
480 "(being built) more effectively."),
482 improvement_name_translation(pimprove
));
483 lsend_packet_chat_msg(dest
, &packet
);
484 if (NULL
!= cache_for_player
) {
485 event_cache_add_for_player(&packet
, cache_for_player
);
490 if (0 >= turns_growth
&& !city_celebrating(pcity
)
491 && city_can_grow_to(pcity
, city_size_get(pcity
) + 1)) {
492 package_event(&packet
, city_tile(pcity
),
493 E_CITY_MAY_SOON_GROW
, ftc_server
,
494 _("%s may soon grow to size %i."),
495 city_link(pcity
), city_size_get(pcity
) + 1);
496 lsend_packet_chat_msg(dest
, &packet
);
497 if (NULL
!= cache_for_player
) {
498 event_cache_add_for_player(&packet
, cache_for_player
);
502 if (0 >= pcity
->food_stock
+ pcity
->surplus
[O_FOOD
]
503 && 0 > pcity
->surplus
[O_FOOD
]) {
504 package_event(&packet
, city_tile(pcity
),
505 E_CITY_FAMINE_FEARED
, ftc_server
,
506 _("Warning: Famine feared in %s."), city_link(pcity
));
507 lsend_packet_chat_msg(dest
, &packet
);
508 if (NULL
!= cache_for_player
) {
509 event_cache_add_for_player(&packet
, cache_for_player
);
515 /****************************************************************************
516 Send global and player specific city turn notifications. If 'pconn' is
517 NULL, it will send to all connections and cache the events.
518 ****************************************************************************/
519 void send_city_turn_notifications(struct connection
*pconn
)
522 struct player
*pplayer
= conn_get_player(pconn
);
524 if (NULL
!= pplayer
) {
525 city_list_iterate(pplayer
->cities
, pcity
) {
526 city_turn_notify(pcity
, pconn
->self
, NULL
);
527 } city_list_iterate_end
;
529 city_global_turn_notify(pconn
->self
);
531 players_iterate(pplayer
) {
532 city_list_iterate(pplayer
->cities
, pcity
) {
533 city_turn_notify(pcity
, pplayer
->connections
, pplayer
);
534 } city_list_iterate_end
;
535 } players_iterate_end
;
536 /* NB: notifications to 'game.est_connections' are automatically
538 city_global_turn_notify(game
.est_connections
);
542 /**************************************************************************
543 Update all cities of one nation (costs for buildings, unit upkeep, ...).
544 **************************************************************************/
545 void update_city_activities(struct player
*pplayer
)
547 char buf
[4 * MAX_LEN_NAME
];
550 fc_assert(NULL
!= pplayer
);
551 fc_assert(NULL
!= pplayer
->cities
);
553 n
= city_list_size(pplayer
->cities
);
554 gold
= pplayer
->economic
.gold
;
555 pplayer
->server
.bulbs_last_turn
= 0;
558 struct city
*cities
[n
];
561 city_list_iterate(pplayer
->cities
, pcity
) {
563 citizens_convert(pcity
);
565 /* Cancel traderoutes that cannot exist any more */
566 trade_routes_iterate_safe(pcity
, proute
) {
567 struct city
*tcity
= game_city_by_number(proute
->partner
);
572 if (proute
->dir
!= RDIR_FROM
&& goods_has_flag(proute
->goods
, GF_DEPLETES
)
573 && !goods_can_be_provided(tcity
, proute
->goods
, NULL
)) {
576 if (!cancel
&& !can_cities_trade(pcity
, tcity
)) {
577 enum trade_route_type type
= cities_trade_route_type(pcity
, tcity
);
578 struct trade_route_settings
*settings
= trade_route_settings_by_type(type
);
580 if (settings
->cancelling
== TRI_CANCEL
) {
586 struct trade_route
*back
;
588 back
= remove_trade_route(pcity
, proute
, TRUE
, FALSE
);
593 } trade_routes_iterate_safe_end
;
595 /* Add cities to array for later random order handling */
597 } city_list_iterate_end
;
599 /* How gold upkeep is handled depends on the setting
600 * 'game.info.gold_upkeep_style':
601 * GOLD_UPKEEP_CITY: Each city tries to balance its upkeep individually
602 * (this is done in update_city_activity()).
603 * GOLD_UPKEEP_MIXED: Each city tries to balance its upkeep for
604 * buildings individually; the upkeep for units is
605 * paid by the nation.
606 * GOLD_UPKEEP_NATION: The nation as a whole balances the treasury. If
607 * the treasury is not balance units and buildings
610 /* Iterate over cities in a random order. */
613 /* update unit upkeep */
614 city_units_upkeep(cities
[r
]);
615 update_city_activity(cities
[r
]);
616 cities
[r
] = cities
[--i
];
619 if (pplayer
->economic
.gold
< 0) {
620 switch (game
.info
.gold_upkeep_style
) {
621 case GOLD_UPKEEP_CITY
:
623 case GOLD_UPKEEP_MIXED
:
624 /* Nation pays for units. */
625 player_balance_treasury_units(pplayer
);
627 case GOLD_UPKEEP_NATION
:
628 /* Nation pays for units and buildings. */
629 player_balance_treasury_units_and_buildings(pplayer
);
634 /* Should not happen. */
635 fc_assert(pplayer
->economic
.gold
>= 0);
638 /* This test includes the cost of the units because
639 * units are paid for in update_city_activity() or
640 * player_balance_treasury_units(). */
641 if (gold
- (gold
- pplayer
->economic
.gold
) * 3 < 0) {
642 notify_player(pplayer
, NULL
, E_LOW_ON_FUNDS
, ftc_server
,
643 _("WARNING, we're LOW on FUNDS %s."),
644 ruler_title_for_player(pplayer
, buf
, sizeof(buf
)));
648 /* Uncomment to unbalance the game, like in civ1 (CLG). */
649 if (pplayer
->got_tech
&& pplayer
->research
->researched
> 0) {
650 pplayer
->research
->researched
= 0;
654 city_refresh_queue_processing();
657 /**************************************************************************
658 Try to get rid of a unit because of missing upkeep.
660 Won't try to get rid of a unit without any action auto performers for
661 AAPC_UNIT_UPKEEP. Those are seen as protected from being destroyed
662 because of missing upkeep.
664 Can optionally wipe the unit in the end if it survived the actions in
665 the selected action auto performer.
667 Returns TRUE if the unit went away.
668 **************************************************************************/
669 static bool upkeep_kill_unit(struct unit
*punit
, Output_type_id outp
,
670 enum unit_loss_reason wipe_reason
,
671 bool wipe_in_the_end
)
675 if (!action_auto_perf_unit_sel(AAPC_UNIT_UPKEEP
, punit
,
676 NULL
, get_output_type(outp
))) {
677 /* Can't get rid of this unit. It is undisbandable for the current
682 punit_id
= punit
->id
;
684 /* Try to perform this unit's can't upkeep actions. */
685 action_auto_perf_unit_do(AAPC_UNIT_UPKEEP
, punit
,
686 NULL
, get_output_type(outp
),
689 if (wipe_in_the_end
&& unit_is_alive(punit_id
)) {
690 /* No forced action was able to kill the unit. Finish the job. */
691 wipe_unit(punit
, wipe_reason
, NULL
);
694 return !unit_is_alive(punit_id
);
697 /**************************************************************************
698 Reduce the city specialists by some (positive) value.
699 Return the amount of reduction.
700 **************************************************************************/
701 static citizens
city_reduce_specialists(struct city
*pcity
, citizens change
)
703 citizens want
= change
;
705 fc_assert_ret_val(0 < change
, 0);
707 specialist_type_iterate(sp
) {
708 citizens fix
= MIN(want
, pcity
->specialists
[sp
]);
710 pcity
->specialists
[sp
] -= fix
;
712 } specialist_type_iterate_end
;
714 return change
- want
;
717 /**************************************************************************
718 Reduce the city workers by some (positive) value.
719 Return the amount of reduction.
720 **************************************************************************/
721 static citizens
city_reduce_workers(struct city
*pcity
, citizens change
)
723 struct tile
*pcenter
= city_tile(pcity
);
726 fc_assert_ret_val(0 < change
, 0);
728 city_tile_iterate_skip_free_worked(city_map_radius_sq_get(pcity
), pcenter
,
729 ptile
, _index
, _x
, _y
) {
730 if (0 < want
&& tile_worked(ptile
) == pcity
) {
731 city_map_update_empty(pcity
, ptile
);
734 } city_tile_iterate_skip_free_worked_end
;
736 return change
- want
;
739 /**************************************************************************
740 Reduce the city size. Return TRUE if the city survives the population
742 **************************************************************************/
743 bool city_reduce_size(struct city
*pcity
, citizens pop_loss
,
744 struct player
*destroyer
, const char *reason
)
746 citizens loss_remain
;
753 if (reason
!= NULL
) {
754 script_server_signal_emit("city_size_change", 3,
755 API_TYPE_CITY
, pcity
,
756 API_TYPE_INT
, -pop_loss
,
757 API_TYPE_STRING
, reason
);
760 if (city_size_get(pcity
) <= pop_loss
) {
762 script_server_signal_emit("city_destroyed", 3,
763 API_TYPE_CITY
, pcity
,
764 API_TYPE_PLAYER
, pcity
->owner
,
765 API_TYPE_PLAYER
, destroyer
);
770 old_radius_sq
= tile_border_source_radius_sq(pcity
->tile
);
771 city_size_add(pcity
, -pop_loss
);
772 map_update_border(pcity
->tile
, pcity
->owner
, old_radius_sq
,
773 tile_border_source_radius_sq(pcity
->tile
));
775 /* Cap the food stock at the new granary size. */
776 if (pcity
->food_stock
> city_granary_size(city_size_get(pcity
))) {
777 pcity
->food_stock
= city_granary_size(city_size_get(pcity
));
780 /* First try to kill off the specialists */
781 loss_remain
= pop_loss
- city_reduce_specialists(pcity
, pop_loss
);
783 if (loss_remain
> 0) {
784 /* Take it out on workers */
785 loss_remain
-= city_reduce_workers(pcity
, loss_remain
);
788 /* Update citizens. */
789 citizens_update(pcity
, NULL
);
791 /* Update number of people in each feelings category.
792 * This also updates the city radius if needed. */
795 auto_arrange_workers(pcity
);
797 /* Send city data. */
800 fc_assert_ret_val_msg(0 == loss_remain
, TRUE
,
801 "city_reduce_size() has remaining"
802 "%d of %d for \"%s\"[%d]",
803 loss_remain
, pop_loss
,
804 city_name_get(pcity
), city_size_get(pcity
));
806 /* Update cities that have trade routes with us */
807 trade_partners_iterate(pcity
, pcity2
) {
808 if (city_refresh(pcity2
)) {
809 /* This should never happen, but if it does, make sure not to
810 * leave workers outside city radius. */
811 auto_arrange_workers(pcity2
);
813 } trade_partners_iterate_end
;
815 sanity_check_city(pcity
);
819 /**************************************************************************
820 Repair the city population without affecting city size.
821 Used by savegame.c and sanitycheck.c
822 **************************************************************************/
823 void city_repair_size(struct city
*pcity
, int change
)
826 pcity
->specialists
[DEFAULT_SPECIALIST
] += change
;
827 } else if (change
< 0) {
828 int need
= change
+ city_reduce_specialists(pcity
, -change
);
831 need
+= city_reduce_workers(pcity
, -need
);
834 fc_assert_msg(0 == need
,
835 "city_repair_size() has remaining %d of %d for \"%s\"[%d]",
836 need
, change
, city_name_get(pcity
), city_size_get(pcity
));
840 /**************************************************************************
841 Return the percentage of food that is lost in this city.
843 Normally this value is 0% but this can be increased by EFT_GROWTH_FOOD
845 **************************************************************************/
846 static int granary_savings(const struct city
*pcity
)
848 int savings
= get_city_bonus(pcity
, EFT_GROWTH_FOOD
);
850 return CLIP(0, savings
, 100);
853 /**************************************************************************
854 Reset the foodbox, usually when a city grows or shrinks.
855 By default it is reset to zero, but this can be increased by Growth_Food
857 Usually this should be called before the city changes size.
858 **************************************************************************/
859 static void city_reset_foodbox(struct city
*pcity
, int new_size
)
861 fc_assert_ret(pcity
!= NULL
);
862 pcity
->food_stock
= (city_granary_size(new_size
)
863 * granary_savings(pcity
)) / 100;
866 /**************************************************************************
867 Increase city size by one. We do not refresh borders or send info about
868 the city to the clients as part of this function. There might be several
869 calls to this function at once, and those actions are needed only once.
870 **************************************************************************/
871 static bool city_increase_size(struct city
*pcity
, struct player
*nationality
)
874 int savings_pct
= granary_savings(pcity
);
875 bool have_square
= FALSE
;
876 bool rapture_grow
= city_rapture_grow(pcity
); /* check before size increase! */
877 struct tile
*pcenter
= city_tile(pcity
);
878 struct player
*powner
= city_owner(pcity
);
879 struct impr_type
*pimprove
= pcity
->production
.value
.building
;
880 int saved_id
= pcity
->id
;
882 if (!city_can_grow_to(pcity
, city_size_get(pcity
) + 1)) {
883 /* need improvement */
884 if (get_current_construction_bonus(pcity
, EFT_SIZE_ADJ
, RPT_CERTAIN
) > 0
885 || get_current_construction_bonus(pcity
, EFT_SIZE_UNLIMIT
, RPT_CERTAIN
) > 0) {
886 notify_player(powner
, city_tile(pcity
), E_CITY_AQ_BUILDING
, ftc_server
,
887 _("%s needs %s (being built) to grow beyond size %d."),
889 improvement_name_translation(pimprove
),
890 city_size_get(pcity
));
892 notify_player(powner
, city_tile(pcity
), E_CITY_AQUEDUCT
, ftc_server
,
893 _("%s needs an improvement to grow beyond size %d."),
894 city_link(pcity
), city_size_get(pcity
));
896 /* Granary can only hold so much */
897 new_food
= (city_granary_size(city_size_get(pcity
))
898 * (100 * 100 - game
.server
.aqueductloss
* (100 - savings_pct
))
900 pcity
->food_stock
= MIN(pcity
->food_stock
, new_food
);
904 city_size_add(pcity
, 1);
906 /* Do not empty food stock if city is growing by celebrating */
908 new_food
= city_granary_size(city_size_get(pcity
));
910 new_food
= city_granary_size(city_size_get(pcity
)) * savings_pct
/ 100;
912 pcity
->food_stock
= MIN(pcity
->food_stock
, new_food
);
914 /* If there is enough food, and the city is big enough,
915 * make new citizens into scientists or taxmen -- Massimo */
917 /* Ignore food if no square can be worked */
918 city_tile_iterate_skip_free_worked(city_map_radius_sq_get(pcity
), pcenter
,
919 ptile
, _index
, _x
, _y
) {
920 if (tile_worked(ptile
) != pcity
/* quick test */
921 && city_can_work_tile(pcity
, ptile
)) {
924 } city_tile_iterate_skip_free_worked_end
;
926 if ((pcity
->surplus
[O_FOOD
] >= 2 || !have_square
)
927 && is_city_option_set(pcity
, CITYO_SCIENCE_SPECIALISTS
)) {
928 pcity
->specialists
[best_specialist(O_SCIENCE
, pcity
)]++;
929 } else if ((pcity
->surplus
[O_FOOD
] >= 2 || !have_square
)
930 && is_city_option_set(pcity
, CITYO_GOLD_SPECIALISTS
)) {
931 pcity
->specialists
[best_specialist(O_GOLD
, pcity
)]++;
933 pcity
->specialists
[DEFAULT_SPECIALIST
]++; /* or else city is !sane */
936 /* Update citizens. */
937 citizens_update(pcity
, nationality
);
939 /* Refresh the city data; this also checks the squared city radius. */
942 auto_arrange_workers(pcity
);
944 /* Update cities that have trade routes with us */
945 trade_partners_iterate(pcity
, pcity2
) {
946 if (city_refresh(pcity2
)) {
947 /* This should never happen, but if it does, make sure not to
948 * leave workers outside city radius. */
949 auto_arrange_workers(pcity2
);
951 } trade_partners_iterate_end
;
953 notify_player(powner
, city_tile(pcity
), E_CITY_GROWTH
, ftc_server
,
954 _("%s grows to size %d."),
955 city_link(pcity
), city_size_get(pcity
));
957 /* Deprecated signal. Connect your lua functions to "city_size_change" that's
958 * emitted from calling functions which know the 'reason' of the increase. */
959 script_server_signal_emit("city_growth", 2,
960 API_TYPE_CITY
, pcity
,
961 API_TYPE_INT
, city_size_get(pcity
));
962 if (city_exist(saved_id
)) {
963 /* Script didn't destroy this city */
964 sanity_check_city(pcity
);
971 /****************************************************************************
972 Change the city size. Return TRUE iff the city is still alive afterwards.
973 ****************************************************************************/
974 bool city_change_size(struct city
*pcity
, citizens size
,
975 struct player
*nationality
, const char *reason
)
977 int change
= size
- city_size_get(pcity
);
979 if (change
!= 0 && reason
!= NULL
) {
980 script_server_signal_emit("city_size_change", 3,
981 API_TYPE_CITY
, pcity
,
982 API_TYPE_INT
, size
- city_size_get(pcity
),
983 API_TYPE_STRING
, reason
);
987 /* Increase city size until size reached, or increase fails */
988 while (size
> city_size_get(pcity
)
989 && city_increase_size(pcity
, nationality
)) {
990 /* city_increase_size() does all the work. */
992 } else if (change
< 0) {
993 /* We assume that city_change_size() is never called because
994 * of enemy actions. If that changes, enemy must be passed
995 * to city_reduce_size() */
996 return city_reduce_size(pcity
, -change
, NULL
, NULL
);
999 map_claim_border(pcity
->tile
, pcity
->owner
, -1);
1004 /**************************************************************************
1005 Check whether the population can be increased or
1006 if the city is unable to support a 'settler'...
1007 **************************************************************************/
1008 static void city_populate(struct city
*pcity
, struct player
*nationality
)
1010 int saved_id
= pcity
->id
;
1011 int granary_size
= city_granary_size(city_size_get(pcity
));
1013 pcity
->food_stock
+= pcity
->surplus
[O_FOOD
];
1014 if (pcity
->food_stock
>= granary_size
|| city_rapture_grow(pcity
)) {
1015 if (city_had_recent_plague(pcity
)) {
1016 notify_player(city_owner(pcity
), city_tile(pcity
),
1017 E_CITY_PLAGUE
, ftc_server
,
1018 _("A recent plague outbreak prevents growth in %s."),
1020 /* Lose excess food */
1021 pcity
->food_stock
= MIN(pcity
->food_stock
, granary_size
);
1023 city_increase_size(pcity
, nationality
);
1024 map_claim_border(pcity
->tile
, pcity
->owner
, -1);
1025 script_server_signal_emit("city_size_change", 3,
1026 API_TYPE_CITY
, pcity
,
1028 API_TYPE_STRING
, "growth");
1030 } else if (pcity
->food_stock
< 0) {
1031 /* FIXME: should this depend on units with ability to build
1032 * cities or on units that require food in upkeep?
1033 * I'll assume citybuilders (units that 'contain' 1 pop) -- sjolie
1034 * The above may make more logical sense, but in game terms
1035 * you want to disband a unit that is draining your food
1036 * reserves. Hence, I'll assume food upkeep > 0 units. -- jjm
1038 unit_list_iterate_safe(pcity
->units_supported
, punit
) {
1039 if (punit
->upkeep
[O_FOOD
] > 0) {
1040 const char *punit_link
= unit_tile_link(punit
);
1042 if (upkeep_kill_unit(punit
, O_FOOD
, ULR_STARVED
,
1043 game
.info
.muuk_food_wipe
)) {
1044 notify_player(city_owner(pcity
), city_tile(pcity
),
1045 E_UNIT_LOST_MISC
, ftc_server
,
1046 _("Famine feared in %s, %s lost!"),
1047 city_link(pcity
), punit_link
);
1050 if (city_exist(saved_id
)) {
1051 city_reset_foodbox(pcity
, city_size_get(pcity
));
1055 } unit_list_iterate_safe_end
;
1056 if (city_size_get(pcity
) > 1) {
1057 notify_player(city_owner(pcity
), city_tile(pcity
),
1058 E_CITY_FAMINE
, ftc_server
,
1059 _("Famine causes population loss in %s."),
1062 notify_player(city_owner(pcity
), city_tile(pcity
),
1063 E_CITY_FAMINE
, ftc_server
,
1064 _("Famine destroys %s entirely."),
1067 city_reset_foodbox(pcity
, city_size_get(pcity
) - 1);
1068 city_reduce_size(pcity
, 1, NULL
, "famine");
1072 /**************************************************************************
1073 Examine the worklist and change the build target. Return 0 if no
1074 targets are available to change to. Otherwise return non-zero. Has
1075 the side-effect of removing from the worklist any no-longer-available
1076 targets as well as the target actually selected, if any.
1077 **************************************************************************/
1078 static bool worklist_change_build_target(struct player
*pplayer
,
1081 struct universal target
;
1082 bool success
= FALSE
;
1084 int saved_id
= pcity
->id
;
1085 bool city_checked
= TRUE
; /* This is used to avoid spurious city_exist() calls */
1086 struct worklist
*pwl
= &pcity
->worklist
;
1088 if (worklist_is_empty(pwl
)) {
1089 /* Nothing in the worklist; bail now. */
1094 while (!success
&& i
< worklist_length(pwl
)) {
1096 if (!city_checked
) {
1097 if (!city_exist(saved_id
)) {
1098 /* Some script has removed useless city that cannot build
1099 * what it is told to! */
1102 city_checked
= TRUE
;
1105 if (worklist_peek_ith(pwl
, &target
, i
)) {
1106 success
= can_city_build_now(pcity
, &target
);
1116 switch (target
.kind
) {
1119 struct unit_type
*ptarget
= target
.value
.utype
;
1120 struct unit_type
*pupdate
= unit_upgrades_to(pcity
, ptarget
);
1122 /* Maybe we can just upgrade the target to what the city /can/ build. */
1123 if (U_NOT_OBSOLETED
== pupdate
) {
1124 /* Nope, we're stuck. Skip this item from the worklist. */
1125 if (ptarget
->need_government
!= NULL
1126 && ptarget
->need_government
!= government_of_player(pplayer
)) {
1127 notify_player(pplayer
, city_tile(pcity
),
1128 E_CITY_CANTBUILD
, ftc_server
,
1129 _("%s can't build %s from the worklist; "
1130 "it needs %s government. Postponing..."),
1131 city_link(pcity
), utype_name_translation(ptarget
),
1132 government_name_translation(ptarget
->need_government
));
1133 script_server_signal_emit("unit_cant_be_built", 3,
1134 API_TYPE_BUILDING_TYPE
, ptarget
,
1135 API_TYPE_CITY
, pcity
,
1136 API_TYPE_STRING
, "need_government");
1137 } else if (ptarget
->need_improvement
!= NULL
1138 && !city_has_building(pcity
, ptarget
->need_improvement
)) {
1139 notify_player(pplayer
, city_tile(pcity
),
1140 E_CITY_CANTBUILD
, ftc_server
,
1141 _("%s can't build %s from the worklist; "
1142 "need to have %s first. Postponing..."),
1143 city_link(pcity
), utype_name_translation(ptarget
),
1144 city_improvement_name_translation(pcity
,
1145 ptarget
->need_improvement
));
1146 script_server_signal_emit("unit_cant_be_built", 3,
1147 API_TYPE_UNIT_TYPE
, ptarget
,
1148 API_TYPE_CITY
, pcity
,
1149 API_TYPE_STRING
, "need_building");
1150 } else if (ptarget
->require_advance
!= NULL
1151 && TECH_KNOWN
!= research_invention_state
1152 (research_get(pplayer
),
1153 advance_number(ptarget
->require_advance
))) {
1154 notify_player(pplayer
, city_tile(pcity
),
1155 E_CITY_CANTBUILD
, ftc_server
,
1156 _("%s can't build %s from the worklist; "
1157 "tech %s not yet available. Postponing..."),
1158 city_link(pcity
), utype_name_translation(ptarget
),
1159 advance_name_translation(ptarget
->require_advance
));
1160 script_server_signal_emit("unit_cant_be_built", 3,
1161 API_TYPE_UNIT_TYPE
, ptarget
,
1162 API_TYPE_CITY
, pcity
,
1163 API_TYPE_STRING
, "need_tech");
1165 /* This shouldn't happen, but in case it does... */
1166 notify_player(pplayer
, city_tile(pcity
),
1167 E_CITY_CANTBUILD
, ftc_server
,
1168 _("%s can't build %s from the worklist; "
1169 "reason unknown! Postponing..."),
1170 city_link(pcity
), utype_name_translation(ptarget
));
1172 city_checked
= FALSE
;
1175 success
= can_city_build_unit_later(pcity
, pupdate
);
1177 /* If the city can never build this unit or its descendants,
1179 notify_player(pplayer
, city_tile(pcity
),
1180 E_CITY_CANTBUILD
, ftc_server
,
1181 _("%s can't build %s from the worklist. Purging..."),
1183 /* Yes, warn about the targets that's actually
1184 in the worklist, not its obsolete-closure
1186 utype_name_translation(ptarget
));
1187 script_server_signal_emit("unit_cant_be_built", 3,
1188 API_TYPE_UNIT_TYPE
, ptarget
,
1189 API_TYPE_CITY
, pcity
,
1190 API_TYPE_STRING
, "never");
1191 if (city_exist(saved_id
)) {
1192 city_checked
= TRUE
;
1193 /* Purge this worklist item. */
1195 worklist_remove(pwl
, i
);
1197 city_checked
= FALSE
;
1200 /* Yep, we can go after pupdate instead. Joy! */
1201 notify_player(pplayer
, city_tile(pcity
), E_WORKLIST
, ftc_server
,
1202 _("Production of %s is upgraded to %s in %s."),
1203 utype_name_translation(ptarget
),
1204 utype_name_translation(pupdate
),
1206 target
.value
.utype
= pupdate
;
1210 case VUT_IMPROVEMENT
:
1212 struct impr_type
*ptarget
= target
.value
.building
;
1213 struct impr_type
*pupdate
= building_upgrades_to(pcity
, ptarget
);
1215 /* If the city can never build this improvement, drop it. */
1216 success
= can_city_build_improvement_later(pcity
, pupdate
);
1218 /* Maybe this improvement has been obsoleted by something that
1220 if (success
&& pupdate
== ptarget
) {
1223 /* Nope, no use. *sigh* */
1224 requirement_vector_iterate(&ptarget
->reqs
, preq
) {
1225 if (!is_req_active(pplayer
, NULL
, pcity
, NULL
, NULL
, NULL
, NULL
,
1226 NULL
, NULL
, NULL
, preq
, RPT_POSSIBLE
)) {
1228 switch (preq
->source
.kind
) {
1230 if (preq
->present
) {
1231 notify_player(pplayer
, city_tile(pcity
),
1232 E_CITY_CANTBUILD
, ftc_server
,
1233 _("%s can't build %s from the worklist; "
1234 "tech %s not yet available. Postponing..."),
1236 city_improvement_name_translation(pcity
, ptarget
),
1237 advance_name_translation
1238 (preq
->source
.value
.advance
));
1239 script_server_signal_emit("building_cant_be_built", 3,
1240 API_TYPE_BUILDING_TYPE
, ptarget
,
1241 API_TYPE_CITY
, pcity
,
1242 API_TYPE_STRING
, "need_tech");
1244 /* While techs can be unlearned, this isn't useful feedback */
1249 if (preq
->present
) {
1250 notify_player(pplayer
, city_tile(pcity
),
1251 E_CITY_CANTBUILD
, ftc_server
,
1252 _("%s can't build %s from the worklist; "
1253 "no tech with flag \"%s\" yet available. "
1256 city_improvement_name_translation(pcity
, ptarget
),
1257 tech_flag_id_name(preq
->source
.value
.techflag
));
1258 script_server_signal_emit("building_cant_be_built", 3,
1259 API_TYPE_BUILDING_TYPE
, ptarget
,
1260 API_TYPE_CITY
, pcity
,
1261 API_TYPE_STRING
, "need_techflag");
1263 /* While techs can be unlearned, this isn't useful feedback */
1267 case VUT_IMPROVEMENT
:
1268 if (preq
->present
) {
1269 notify_player(pplayer
, city_tile(pcity
),
1270 E_CITY_CANTBUILD
, ftc_server
,
1271 _("%s can't build %s from the worklist; "
1272 "need to have %s first. Postponing..."),
1274 city_improvement_name_translation(pcity
, ptarget
),
1275 city_improvement_name_translation(pcity
,
1276 preq
->source
.value
.building
));
1277 script_server_signal_emit("building_cant_be_built", 3,
1278 API_TYPE_BUILDING_TYPE
, ptarget
,
1279 API_TYPE_CITY
, pcity
,
1280 API_TYPE_STRING
, "need_building");
1282 notify_player(pplayer
, city_tile(pcity
),
1283 E_CITY_CANTBUILD
, ftc_server
,
1284 _("%s can't build %s from the worklist; "
1285 "need to not have %s. Postponing..."),
1287 city_improvement_name_translation(pcity
, ptarget
),
1288 city_improvement_name_translation(pcity
,
1289 preq
->source
.value
.building
));
1290 script_server_signal_emit("building_cant_be_built", 3,
1291 API_TYPE_BUILDING_TYPE
, ptarget
,
1292 API_TYPE_CITY
, pcity
,
1293 API_TYPE_STRING
, "have_building");
1296 case VUT_IMPR_GENUS
:
1297 if (preq
->present
) {
1298 notify_player(pplayer
, city_tile(pcity
),
1299 E_CITY_CANTBUILD
, ftc_server
,
1300 _("%s can't build %s from the worklist; "
1301 "need to have %s first. Postponing..."),
1303 city_improvement_name_translation(pcity
, ptarget
),
1304 impr_genus_id_translated_name(
1305 preq
->source
.value
.impr_genus
));
1306 script_server_signal_emit("building_cant_be_built", 3,
1307 API_TYPE_BUILDING_TYPE
, ptarget
,
1308 API_TYPE_CITY
, pcity
,
1309 API_TYPE_STRING
, "need_building_genus");
1311 notify_player(pplayer
, city_tile(pcity
),
1312 E_CITY_CANTBUILD
, ftc_server
,
1313 _("%s can't build %s from the worklist; "
1314 "need to not have %s. Postponing..."),
1316 city_improvement_name_translation(pcity
, ptarget
),
1317 impr_genus_id_translated_name(
1318 preq
->source
.value
.impr_genus
));
1319 script_server_signal_emit("building_cant_be_built", 3,
1320 API_TYPE_BUILDING_TYPE
, ptarget
,
1321 API_TYPE_CITY
, pcity
,
1322 API_TYPE_STRING
, "have_building_genus");
1325 case VUT_GOVERNMENT
:
1326 if (preq
->present
) {
1327 notify_player(pplayer
, city_tile(pcity
),
1328 E_CITY_CANTBUILD
, ftc_server
,
1329 _("%s can't build %s from the worklist; "
1330 "it needs %s government. Postponing..."),
1332 city_improvement_name_translation(pcity
, ptarget
),
1333 government_name_translation(preq
->source
.value
.govern
));
1334 script_server_signal_emit("building_cant_be_built", 3,
1335 API_TYPE_BUILDING_TYPE
, ptarget
,
1336 API_TYPE_CITY
, pcity
,
1337 API_TYPE_STRING
, "need_government");
1339 notify_player(pplayer
, city_tile(pcity
),
1340 E_CITY_CANTBUILD
, ftc_server
,
1341 _("%s can't build %s from the worklist; "
1342 "it cannot have %s government. Postponing..."),
1344 city_improvement_name_translation(pcity
, ptarget
),
1345 government_name_translation(preq
->source
.value
.govern
));
1346 script_server_signal_emit("building_cant_be_built", 3,
1347 API_TYPE_BUILDING_TYPE
, ptarget
,
1348 API_TYPE_CITY
, pcity
,
1349 API_TYPE_STRING
, "have_government");
1352 case VUT_ACHIEVEMENT
:
1353 if (preq
->present
) {
1354 notify_player(pplayer
, city_tile(pcity
),
1355 E_CITY_CANTBUILD
, ftc_server
,
1356 _("%s can't build %s from the worklist; "
1357 "it needs \"%s\" achievement. Postponing..."),
1359 city_improvement_name_translation(pcity
, ptarget
),
1360 achievement_name_translation(preq
->source
.value
.achievement
));
1361 script_server_signal_emit("building_cant_be_built", 3,
1362 API_TYPE_BUILDING_TYPE
, ptarget
,
1363 API_TYPE_CITY
, pcity
,
1364 API_TYPE_STRING
, "need_achievement");
1366 /* Can't unachieve things. */
1371 if (preq
->present
) {
1372 notify_player(pplayer
, city_tile(pcity
),
1373 E_CITY_CANTBUILD
, ftc_server
,
1374 Q_("?extra:%s can't build %s from the worklist; "
1375 "%s is required. Postponing..."),
1377 city_improvement_name_translation(pcity
, ptarget
),
1378 extra_name_translation(preq
->source
.value
.extra
));
1379 script_server_signal_emit("building_cant_be_built", 3,
1380 API_TYPE_BUILDING_TYPE
, ptarget
,
1381 API_TYPE_CITY
, pcity
,
1382 API_TYPE_STRING
, "need_extra");
1384 notify_player(pplayer
, city_tile(pcity
),
1385 E_CITY_CANTBUILD
, ftc_server
,
1386 Q_("?extra:%s can't build %s from the worklist; "
1387 "%s is prohibited. Postponing..."),
1389 city_improvement_name_translation(pcity
, ptarget
),
1390 extra_name_translation(preq
->source
.value
.extra
));
1391 script_server_signal_emit("building_cant_be_built", 3,
1392 API_TYPE_BUILDING_TYPE
, ptarget
,
1393 API_TYPE_CITY
, pcity
,
1394 API_TYPE_STRING
, "have_extra");
1398 if (preq
->present
) {
1399 notify_player(pplayer
, city_tile(pcity
),
1400 E_CITY_CANTBUILD
, ftc_server
,
1401 Q_("?extra:%s can't build %s from the worklist; "
1402 "%s is required. Postponing..."),
1404 city_improvement_name_translation(pcity
, ptarget
),
1405 goods_name_translation(preq
->source
.value
.good
));
1406 script_server_signal_emit("building_cant_be_built", 3,
1407 API_TYPE_BUILDING_TYPE
, ptarget
,
1408 API_TYPE_CITY
, pcity
,
1409 API_TYPE_STRING
, "need_good");
1411 notify_player(pplayer
, city_tile(pcity
),
1412 E_CITY_CANTBUILD
, ftc_server
,
1413 Q_("?extra:%s can't build %s from the worklist; "
1414 "%s is prohibited. Postponing..."),
1416 city_improvement_name_translation(pcity
, ptarget
),
1417 goods_name_translation(preq
->source
.value
.good
));
1418 script_server_signal_emit("building_cant_be_built", 3,
1419 API_TYPE_BUILDING_TYPE
, ptarget
,
1420 API_TYPE_CITY
, pcity
,
1421 API_TYPE_STRING
, "have_good");
1425 if (preq
->present
) {
1426 notify_player(pplayer
, city_tile(pcity
),
1427 E_CITY_CANTBUILD
, ftc_server
,
1428 Q_("?terrain:%s can't build %s from the worklist; "
1429 "%s terrain is required. Postponing..."),
1431 city_improvement_name_translation(pcity
, ptarget
),
1432 terrain_name_translation(preq
->source
.value
.terrain
));
1433 script_server_signal_emit("building_cant_be_built", 3,
1434 API_TYPE_BUILDING_TYPE
, ptarget
,
1435 API_TYPE_CITY
, pcity
,
1436 API_TYPE_STRING
, "need_terrain");
1438 notify_player(pplayer
, city_tile(pcity
),
1439 E_CITY_CANTBUILD
, ftc_server
,
1440 Q_("?terrain:%s can't build %s from the worklist; "
1441 "%s terrain is prohibited. Postponing..."),
1443 city_improvement_name_translation(pcity
, ptarget
),
1444 terrain_name_translation(preq
->source
.value
.terrain
));
1445 script_server_signal_emit("building_cant_be_built", 3,
1446 API_TYPE_BUILDING_TYPE
, ptarget
,
1447 API_TYPE_CITY
, pcity
,
1448 API_TYPE_STRING
, "have_terrain");
1452 /* Nation can be required at Alliance range, which may change. */
1453 if (preq
->present
) {
1454 notify_player(pplayer
, city_tile(pcity
),
1455 E_CITY_CANTBUILD
, ftc_server
,
1456 /* TRANS: "%s nation" is adjective */
1457 Q_("?nation:%s can't build %s from the worklist; "
1458 "%s nation is required. Postponing..."),
1460 city_improvement_name_translation(pcity
, ptarget
),
1461 nation_adjective_translation(preq
->source
.value
.nation
));
1462 script_server_signal_emit("building_cant_be_built", 3,
1463 API_TYPE_BUILDING_TYPE
, ptarget
,
1464 API_TYPE_CITY
, pcity
,
1465 API_TYPE_STRING
, "need_nation");
1467 notify_player(pplayer
, city_tile(pcity
),
1468 E_CITY_CANTBUILD
, ftc_server
,
1469 Q_("?nation:%s can't build %s from the worklist; "
1470 "%s nation is prohibited. Postponing..."),
1472 city_improvement_name_translation(pcity
, ptarget
),
1473 nation_adjective_translation(preq
->source
.value
.nation
));
1474 script_server_signal_emit("building_cant_be_built", 3,
1475 API_TYPE_BUILDING_TYPE
, ptarget
,
1476 API_TYPE_CITY
, pcity
,
1477 API_TYPE_STRING
, "have_nation");
1480 case VUT_NATIONGROUP
:
1481 /* Nation group can be required at Alliance range, which may
1483 if (preq
->present
) {
1484 notify_player(pplayer
, city_tile(pcity
),
1485 E_CITY_CANTBUILD
, ftc_server
,
1486 /* TRANS: "%s nation" is adjective */
1487 Q_("?ngroup:%s can't build %s from the worklist; "
1488 "%s nation is required. Postponing..."),
1490 city_improvement_name_translation(pcity
, ptarget
),
1491 nation_group_name_translation(preq
->source
.value
.nationgroup
));
1492 script_server_signal_emit("building_cant_be_built", 3,
1493 API_TYPE_BUILDING_TYPE
, ptarget
,
1494 API_TYPE_CITY
, pcity
,
1495 API_TYPE_STRING
, "need_nationgroup");
1497 notify_player(pplayer
, city_tile(pcity
),
1498 E_CITY_CANTBUILD
, ftc_server
,
1499 Q_("?ngroup:%s can't build %s from the worklist; "
1500 "%s nation is prohibited. Postponing..."),
1502 city_improvement_name_translation(pcity
, ptarget
),
1503 nation_group_name_translation(preq
->source
.value
.nationgroup
));
1504 script_server_signal_emit("building_cant_be_built", 3,
1505 API_TYPE_BUILDING_TYPE
, ptarget
,
1506 API_TYPE_CITY
, pcity
,
1507 API_TYPE_STRING
, "have_nationgroup");
1511 /* FIXME: City styles sometimes change over time, but it isn't
1512 * entirely under player control. Probably better to purge
1513 * with useful explanation. */
1514 if (preq
->present
) {
1515 notify_player(pplayer
, city_tile(pcity
),
1516 E_CITY_CANTBUILD
, ftc_server
,
1517 _("%s can't build %s from the worklist; "
1518 "only %s style cities may build this. Postponing..."),
1520 city_improvement_name_translation(pcity
, ptarget
),
1521 style_name_translation(preq
->source
.value
.style
));
1522 script_server_signal_emit("building_cant_be_built", 3,
1523 API_TYPE_BUILDING_TYPE
, ptarget
,
1524 API_TYPE_CITY
, pcity
,
1525 API_TYPE_STRING
, "need_style");
1527 notify_player(pplayer
, city_tile(pcity
),
1528 E_CITY_CANTBUILD
, ftc_server
,
1529 _("%s can't build %s from the worklist; "
1530 "%s style cities may not build this. Postponing..."),
1532 city_improvement_name_translation(pcity
, ptarget
),
1533 style_name_translation(preq
->source
.value
.style
));
1534 script_server_signal_emit("building_cant_be_built", 3,
1535 API_TYPE_BUILDING_TYPE
, ptarget
,
1536 API_TYPE_CITY
, pcity
,
1537 API_TYPE_STRING
, "have_style");
1540 case VUT_NATIONALITY
:
1541 /* FIXME: Changing citizen nationality is hard: purging might be
1542 * more useful in this case. */
1543 if (preq
->present
) {
1544 notify_player(pplayer
, city_tile(pcity
),
1545 E_CITY_CANTBUILD
, ftc_server
,
1546 /* TRANS: Latter %s is citizen nationality */
1547 _("%s can't build %s from the worklist; "
1548 "only city with %s may build this. Postponing..."),
1550 city_improvement_name_translation(pcity
, ptarget
),
1551 nation_plural_translation(preq
->source
.value
.nationality
));
1552 script_server_signal_emit("building_cant_be_built", 3,
1553 API_TYPE_BUILDING_TYPE
, ptarget
,
1554 API_TYPE_CITY
, pcity
,
1555 API_TYPE_STRING
, "need_nationality");
1557 notify_player(pplayer
, city_tile(pcity
),
1558 E_CITY_CANTBUILD
, ftc_server
,
1559 /* TRANS: Latter %s is citizen nationality */
1560 _("%s can't build %s from the worklist; "
1561 "only city without %s may build this. Postponing..."),
1563 city_improvement_name_translation(pcity
, ptarget
),
1564 nation_plural_translation(preq
->source
.value
.nationality
));
1565 script_server_signal_emit("building_cant_be_built", 3,
1566 API_TYPE_BUILDING_TYPE
, ptarget
,
1567 API_TYPE_CITY
, pcity
,
1568 API_TYPE_STRING
, "have_nationality");
1572 if (preq
->present
) {
1573 notify_player(pplayer
, city_tile(pcity
),
1574 E_CITY_CANTBUILD
, ftc_server
,
1575 /* TRANS: '%s' is a wide range of relationships;
1576 * e.g., 'Peace', 'Never met', 'Foreign',
1577 * 'Hosts embassy', 'Provided Casus Belli' */
1578 _("%s can't build %s from the worklist; "
1579 "the relationship '%s' is required."
1582 city_improvement_name_translation(pcity
,
1584 diplrel_name_translation(
1585 preq
->source
.value
.diplrel
));
1586 script_server_signal_emit("building_cant_be_built", 3,
1587 API_TYPE_BUILDING_TYPE
, ptarget
,
1588 API_TYPE_CITY
, pcity
,
1589 API_TYPE_STRING
, "need_diplrel");
1591 notify_player(pplayer
, city_tile(pcity
),
1592 E_CITY_CANTBUILD
, ftc_server
,
1593 _("%s can't build %s from the worklist; "
1594 "the relationship '%s' is prohibited."
1597 city_improvement_name_translation(pcity
,
1599 diplrel_name_translation(
1600 preq
->source
.value
.diplrel
));
1601 script_server_signal_emit("building_cant_be_built", 3,
1602 API_TYPE_BUILDING_TYPE
, ptarget
,
1603 API_TYPE_CITY
, pcity
,
1604 API_TYPE_STRING
, "have_diplrel");
1608 if (preq
->present
) {
1609 notify_player(pplayer
, city_tile(pcity
),
1610 E_CITY_CANTBUILD
, ftc_server
,
1611 _("%s can't build %s from the worklist; "
1612 "city must be of size %d or larger. "
1615 city_improvement_name_translation(pcity
, ptarget
),
1616 preq
->source
.value
.minsize
);
1617 script_server_signal_emit("building_cant_be_built", 3,
1618 API_TYPE_BUILDING_TYPE
, ptarget
,
1619 API_TYPE_CITY
, pcity
,
1620 API_TYPE_STRING
, "need_minsize");
1622 notify_player(pplayer
, city_tile(pcity
),
1623 E_CITY_CANTBUILD
, ftc_server
,
1624 _("%s can't build %s from the worklist; "
1625 "city must be of size %d or smaller."
1628 city_improvement_name_translation(pcity
, ptarget
),
1629 (preq
->source
.value
.minsize
- 1));
1630 script_server_signal_emit("building_cant_be_built", 3,
1631 API_TYPE_BUILDING_TYPE
, ptarget
,
1632 API_TYPE_CITY
, pcity
,
1633 API_TYPE_STRING
, "need_minsize");
1636 case VUT_MINCULTURE
:
1637 if (preq
->present
) {
1638 notify_player(pplayer
, city_tile(pcity
),
1639 E_CITY_CANTBUILD
, ftc_server
,
1640 _("%s can't build %s from the worklist; "
1641 "city must have culture of %d. Postponing..."),
1643 city_improvement_name_translation(pcity
, ptarget
),
1644 preq
->source
.value
.minculture
);
1645 script_server_signal_emit("building_cant_be_built", 3,
1646 API_TYPE_BUILDING_TYPE
, ptarget
,
1647 API_TYPE_CITY
, pcity
,
1648 API_TYPE_STRING
, "need_minculture");
1650 /* What has been written may not be unwritten. */
1655 if (preq
->present
) {
1656 notify_player(pplayer
, city_tile(pcity
),
1657 E_CITY_CANTBUILD
, ftc_server
,
1658 _("%s can't build %s from the worklist; "
1659 "%d techs must be known. Postponing..."),
1661 city_improvement_name_translation(pcity
, ptarget
),
1662 preq
->source
.value
.min_techs
);
1663 script_server_signal_emit("building_cant_be_built", 3,
1664 API_TYPE_BUILDING_TYPE
, ptarget
,
1665 API_TYPE_CITY
, pcity
,
1666 API_TYPE_STRING
, "need_mintechs");
1671 case VUT_MAXTILEUNITS
:
1672 if (preq
->present
) {
1673 notify_player(pplayer
, city_tile(pcity
),
1674 E_CITY_CANTBUILD
, ftc_server
,
1675 PL_("%s can't build %s from the worklist; "
1676 "more than %d unit on tile."
1678 "%s can't build %s from the worklist; "
1679 "more than %d units on tile."
1681 preq
->source
.value
.max_tile_units
),
1683 city_improvement_name_translation(pcity
,
1685 preq
->source
.value
.max_tile_units
);
1686 script_server_signal_emit("building_cant_be_built", 3,
1687 API_TYPE_BUILDING_TYPE
, ptarget
,
1688 API_TYPE_CITY
, pcity
,
1689 API_TYPE_STRING
, "need_tileunits");
1691 notify_player(pplayer
, city_tile(pcity
),
1692 E_CITY_CANTBUILD
, ftc_server
,
1693 PL_("%s can't build %s from the worklist; "
1694 "fewer than %d unit on tile."
1696 "%s can't build %s from the worklist; "
1697 "fewer than %d units on tile."
1699 preq
->source
.value
.max_tile_units
+ 1),
1701 city_improvement_name_translation(pcity
,
1703 preq
->source
.value
.max_tile_units
+ 1);
1704 script_server_signal_emit("building_cant_be_built", 3,
1705 API_TYPE_BUILDING_TYPE
, ptarget
,
1706 API_TYPE_CITY
, pcity
,
1707 API_TYPE_STRING
, "need_tileunits");
1711 /* Can't change AI level. */
1714 case VUT_TERRAINCLASS
:
1715 if (preq
->present
) {
1716 notify_player(pplayer
, city_tile(pcity
),
1717 E_CITY_CANTBUILD
, ftc_server
,
1718 Q_("?terrainclass:%s can't build %s from the "
1719 "worklist; %s terrain is required."
1722 city_improvement_name_translation(pcity
, ptarget
),
1723 terrain_class_name_translation(preq
->source
.value
.terrainclass
));
1724 script_server_signal_emit("building_cant_be_built", 3,
1725 API_TYPE_BUILDING_TYPE
, ptarget
,
1726 API_TYPE_CITY
, pcity
,
1727 API_TYPE_STRING
, "need_terrainclass");
1729 notify_player(pplayer
, city_tile(pcity
),
1730 E_CITY_CANTBUILD
, ftc_server
,
1731 Q_("?terrainclass:%s can't build %s from the "
1732 "worklist; %s terrain is prohibited."
1735 city_improvement_name_translation(pcity
, ptarget
),
1736 terrain_class_name_translation(preq
->source
.value
.terrainclass
));
1737 script_server_signal_emit("building_cant_be_built", 3,
1738 API_TYPE_BUILDING_TYPE
, ptarget
,
1739 API_TYPE_CITY
, pcity
,
1740 API_TYPE_STRING
, "have_terrainclass");
1744 if (preq
->present
) {
1745 notify_player(pplayer
, city_tile(pcity
),
1746 E_CITY_CANTBUILD
, ftc_server
,
1747 _("%s can't build %s from the worklist; "
1748 "terrain with \"%s\" flag is required. "
1751 city_improvement_name_translation(pcity
, ptarget
),
1752 terrain_flag_id_name(preq
->source
.value
.terrainflag
));
1753 script_server_signal_emit("building_cant_be_built", 3,
1754 API_TYPE_BUILDING_TYPE
, ptarget
,
1755 API_TYPE_CITY
, pcity
,
1756 API_TYPE_STRING
, "need_terrainflag");
1758 notify_player(pplayer
, city_tile(pcity
),
1759 E_CITY_CANTBUILD
, ftc_server
,
1760 _("%s can't build %s from the worklist; "
1761 "terrain with \"%s\" flag is prohibited. "
1764 city_improvement_name_translation(pcity
, ptarget
),
1765 terrain_flag_id_name(preq
->source
.value
.terrainflag
));
1766 script_server_signal_emit("building_cant_be_built", 3,
1767 API_TYPE_BUILDING_TYPE
, ptarget
,
1768 API_TYPE_CITY
, pcity
,
1769 API_TYPE_STRING
, "have_terrainflag");
1773 if (preq
->present
) {
1774 notify_player(pplayer
, city_tile(pcity
),
1775 E_CITY_CANTBUILD
, ftc_server
,
1776 _("%s can't build %s from the worklist; "
1777 "base with \"%s\" flag is required. "
1780 city_improvement_name_translation(pcity
, ptarget
),
1781 base_flag_id_name(preq
->source
.value
.baseflag
));
1782 script_server_signal_emit("building_cant_be_built", 3,
1783 API_TYPE_BUILDING_TYPE
, ptarget
,
1784 API_TYPE_CITY
, pcity
,
1785 API_TYPE_STRING
, "need_baseflag");
1787 notify_player(pplayer
, city_tile(pcity
),
1788 E_CITY_CANTBUILD
, ftc_server
,
1789 _("%s can't build %s from the worklist; "
1790 "base with \"%s\" flag is prohibited. "
1793 city_improvement_name_translation(pcity
, ptarget
),
1794 base_flag_id_name(preq
->source
.value
.baseflag
));
1795 script_server_signal_emit("building_cant_be_built", 3,
1796 API_TYPE_BUILDING_TYPE
, ptarget
,
1797 API_TYPE_CITY
, pcity
,
1798 API_TYPE_STRING
, "have_baseflag");
1802 if (preq
->present
) {
1803 notify_player(pplayer
, city_tile(pcity
),
1804 E_CITY_CANTBUILD
, ftc_server
,
1805 _("%s can't build %s from the worklist; "
1806 "road with \"%s\" flag is required. "
1809 city_improvement_name_translation(pcity
, ptarget
),
1810 road_flag_id_name(preq
->source
.value
.roadflag
));
1811 script_server_signal_emit("building_cant_be_built", 3,
1812 API_TYPE_BUILDING_TYPE
, ptarget
,
1813 API_TYPE_CITY
, pcity
,
1814 API_TYPE_STRING
, "need_roadflag");
1816 notify_player(pplayer
, city_tile(pcity
),
1817 E_CITY_CANTBUILD
, ftc_server
,
1818 _("%s can't build %s from the worklist; "
1819 "road with \"%s\" flag is prohibited. "
1822 city_improvement_name_translation(pcity
, ptarget
),
1823 road_flag_id_name(preq
->source
.value
.roadflag
));
1824 script_server_signal_emit("building_cant_be_built", 3,
1825 API_TYPE_BUILDING_TYPE
, ptarget
,
1826 API_TYPE_CITY
, pcity
,
1827 API_TYPE_STRING
, "have_roadflag");
1831 if (preq
->present
) {
1832 notify_player(pplayer
, city_tile(pcity
),
1833 E_CITY_CANTBUILD
, ftc_server
,
1834 _("%s can't build %s from the worklist; "
1835 "extra with \"%s\" flag is required. "
1838 city_improvement_name_translation(pcity
, ptarget
),
1839 extra_flag_id_translated_name(preq
->source
.value
.extraflag
));
1840 script_server_signal_emit("building_cant_be_built", 3,
1841 API_TYPE_BUILDING_TYPE
, ptarget
,
1842 API_TYPE_CITY
, pcity
,
1843 API_TYPE_STRING
, "need_extraflag");
1845 notify_player(pplayer
, city_tile(pcity
),
1846 E_CITY_CANTBUILD
, ftc_server
,
1847 _("%s can't build %s from the worklist; "
1848 "extra with \"%s\" flag is prohibited. "
1851 city_improvement_name_translation(pcity
, ptarget
),
1852 extra_flag_id_translated_name(preq
->source
.value
.extraflag
));
1853 script_server_signal_emit("building_cant_be_built", 3,
1854 API_TYPE_BUILDING_TYPE
, ptarget
,
1855 API_TYPE_CITY
, pcity
,
1856 API_TYPE_STRING
, "have_extraflag");
1863 case VUT_MINVETERAN
:
1869 case VUT_SPECIALIST
:
1870 case VUT_TERRAINALTER
: /* XXX could do this in principle */
1872 /* Will only happen with a bogus ruleset. */
1873 log_error("worklist_change_build_target() has bogus preq");
1876 if (preq
->present
) {
1877 notify_player(pplayer
, city_tile(pcity
),
1878 E_CITY_CANTBUILD
, ftc_server
,
1879 /* TRANS: last %s is a date */
1880 _("%s can't build %s from the worklist; "
1881 "only available from %s. Postponing..."),
1883 city_improvement_name_translation(pcity
, ptarget
),
1884 textyear(preq
->source
.value
.minyear
));
1885 script_server_signal_emit("building_cant_be_built", 3,
1886 API_TYPE_BUILDING_TYPE
, ptarget
,
1887 API_TYPE_CITY
, pcity
,
1888 API_TYPE_STRING
, "need_minyear");
1890 /* Can't go back in time. */
1894 case VUT_MINCALFRAG
:
1895 /* Unlike VUT_MINYEAR, a requirement in either direction is
1896 * likely to be fulfilled sooner or later. */
1897 if (preq
->present
) {
1898 notify_player(pplayer
, city_tile(pcity
),
1899 E_CITY_CANTBUILD
, ftc_server
,
1900 /* TRANS: last %s is a calendar fragment from
1901 * the ruleset; may be a bare number */
1902 _("%s can't build %s from the worklist; "
1903 "only available from %s. Postponing..."),
1905 city_improvement_name_translation(pcity
, ptarget
),
1906 textcalfrag(preq
->source
.value
.mincalfrag
));
1907 script_server_signal_emit("building_cant_be_built", 3,
1908 API_TYPE_BUILDING_TYPE
, ptarget
,
1909 API_TYPE_CITY
, pcity
,
1910 API_TYPE_STRING
, "need_mincalfrag");
1912 fc_assert_action(preq
->source
.value
.mincalfrag
> 0, break);
1913 notify_player(pplayer
, city_tile(pcity
),
1914 E_CITY_CANTBUILD
, ftc_server
,
1915 /* TRANS: last %s is a calendar fragment from
1916 * the ruleset; may be a bare number */
1917 _("%s can't build %s from the worklist; "
1918 "not available after %s. Postponing..."),
1920 city_improvement_name_translation(pcity
, ptarget
),
1921 textcalfrag(preq
->source
.value
.mincalfrag
-1));
1922 script_server_signal_emit("building_cant_be_built", 3,
1923 API_TYPE_BUILDING_TYPE
, ptarget
,
1924 API_TYPE_CITY
, pcity
,
1925 API_TYPE_STRING
, "have_mincalfrag");
1929 if (preq
->present
) {
1930 notify_player(pplayer
, city_tile(pcity
),
1931 E_CITY_CANTBUILD
, ftc_server
,
1932 _("%s can't build %s from the workist; "
1933 "only available in worlds with %s map."),
1935 city_improvement_name_translation(pcity
, ptarget
),
1936 _(topo_flag_name(preq
->source
.value
.topo_property
)));
1937 script_server_signal_emit("building_cant_be_built", 3,
1938 API_TYPE_BUILDING_TYPE
, ptarget
,
1939 API_TYPE_CITY
, pcity
,
1940 API_TYPE_STRING
, "need_topo");
1945 if (preq
->present
) {
1946 notify_player(pplayer
, city_tile(pcity
),
1947 E_CITY_CANTBUILD
, ftc_server
,
1948 _("%s can't build %s from the worklist; "
1949 "only available once %d turns old. Postponing..."),
1951 city_improvement_name_translation(pcity
, ptarget
),
1952 preq
->source
.value
.age
);
1953 script_server_signal_emit("building_cant_be_built", 3,
1954 API_TYPE_BUILDING_TYPE
, ptarget
,
1955 API_TYPE_CITY
, pcity
,
1956 API_TYPE_STRING
, "need_age");
1958 /* Can't go back in time. */
1964 fc_assert_ret_val_msg(FALSE
, TRUE
,
1965 "worklist_change_build_target() "
1966 "called with invalid preq");
1968 /* No default handling here, as we want compiler warning
1969 * if new requirement type is added to enum and it's not handled
1975 /* Almost all cases emit signal in the end, so city check needed. */
1976 if (!city_exist(saved_id
)) {
1977 /* Some script has removed city */
1980 city_checked
= TRUE
;
1982 } requirement_vector_iterate_end
;
1985 /* This shouldn't happen...
1986 FIXME: make can_city_build_improvement_now() return a reason enum. */
1987 notify_player(pplayer
, city_tile(pcity
),
1988 E_CITY_CANTBUILD
, ftc_server
,
1989 _("%s can't build %s from the worklist; "
1990 "reason unknown! Postponing..."),
1992 city_improvement_name_translation(pcity
, ptarget
));
1994 } else if (success
) {
1995 /* Hey, we can upgrade the improvement! */
1996 notify_player(pplayer
, city_tile(pcity
), E_WORKLIST
, ftc_server
,
1997 _("Production of %s is upgraded to %s in %s."),
1998 city_improvement_name_translation(pcity
, ptarget
),
1999 city_improvement_name_translation(pcity
, pupdate
),
2001 target
.value
.building
= pupdate
;
2006 /* Never in a million years. */
2007 notify_player(pplayer
, city_tile(pcity
),
2008 E_CITY_CANTBUILD
, ftc_server
,
2009 _("%s can't build %s from the worklist. Purging..."),
2011 city_improvement_name_translation(pcity
, ptarget
));
2012 script_server_signal_emit("building_cant_be_built", 3,
2013 API_TYPE_BUILDING_TYPE
, ptarget
,
2014 API_TYPE_CITY
, pcity
,
2015 API_TYPE_STRING
, "never");
2016 if (city_exist(saved_id
)) {
2017 city_checked
= TRUE
;
2018 /* Purge this worklist item. */
2020 worklist_remove(pwl
, i
);
2022 city_checked
= FALSE
;
2028 /* skip useless target */
2029 log_error("worklist_change_build_target() has unrecognized "
2030 "target kind (%d)", target
.kind
);
2036 /* All okay. Switch targets. */
2037 change_build_target(pplayer
, pcity
, &target
, E_WORKLIST
);
2039 /* i is the index immediately _after_ the item we're changing to.
2040 Remove the (i-1)th item from the worklist. */
2041 worklist_remove(pwl
, i
- 1);
2044 if (worklist_is_empty(pwl
)) {
2045 /* There *was* something in the worklist, but it's empty now. Bug the
2047 notify_player(pplayer
, city_tile(pcity
), E_WORKLIST
, ftc_server
,
2048 /* TRANS: The <city> worklist .... */
2049 _("The %s worklist is now empty."),
2056 /**************************************************************************
2057 Assuming we just finished building something, find something new to
2058 build. The policy is: use the worklist if we can; if not, try not
2059 changing; if we must change, get desparate and use the AI advisor.
2060 **************************************************************************/
2061 void choose_build_target(struct player
*pplayer
, struct city
*pcity
)
2063 /* Pick the next thing off the worklist. */
2064 if (worklist_change_build_target(pplayer
, pcity
)) {
2068 /* Try building the same thing again. Repeat building doesn't require a
2069 * call to change_build_target, so just return. */
2070 switch (pcity
->production
.kind
) {
2072 /* We can build a unit again unless it's unique or we have lost the tech. */
2073 if (!utype_has_flag(pcity
->production
.value
.utype
, UTYF_UNIQUE
)
2074 && can_city_build_unit_now(pcity
, pcity
->production
.value
.utype
)) {
2075 log_base(LOG_BUILD_TARGET
, "%s repeats building %s", city_name_get(pcity
),
2076 utype_rule_name(pcity
->production
.value
.utype
));
2080 case VUT_IMPROVEMENT
:
2081 if (can_city_build_improvement_now(pcity
, pcity
->production
.value
.building
)) {
2082 /* We can build space and coinage again, and possibly others. */
2083 log_base(LOG_BUILD_TARGET
, "%s repeats building %s", city_name_get(pcity
),
2084 improvement_rule_name(pcity
->production
.value
.building
));
2093 /* Find *something* to do! */
2094 log_debug("Trying advisor_choose_build.");
2095 advisor_choose_build(pplayer
, pcity
);
2096 log_debug("Advisor_choose_build didn't kill us.");
2099 /**************************************************************************
2100 Follow the list of replacement buildings until we hit something that
2101 we can build. Returns NULL if we can't upgrade at all (including if the
2102 original building is unbuildable).
2103 **************************************************************************/
2104 static struct impr_type
*building_upgrades_to(struct city
*pcity
,
2105 struct impr_type
*pimprove
)
2107 struct impr_type
*check
= pimprove
;
2108 struct impr_type
*best_upgrade
= NULL
;
2110 if (!can_city_build_improvement_direct(pcity
, check
)) {
2113 while (valid_improvement(check
= improvement_replacement(check
))) {
2114 if (can_city_build_improvement_direct(pcity
, check
)) {
2115 best_upgrade
= check
;
2119 return best_upgrade
;
2122 /**************************************************************************
2123 Try to upgrade production in pcity.
2124 **************************************************************************/
2125 static void upgrade_building_prod(struct city
*pcity
)
2127 struct impr_type
*producing
= pcity
->production
.value
.building
;
2128 struct impr_type
*upgrading
= building_upgrades_to(pcity
, producing
);
2130 if (upgrading
&& can_city_build_improvement_now(pcity
, upgrading
)) {
2131 notify_player(city_owner(pcity
), city_tile(pcity
),
2132 E_UNIT_UPGRADED
, ftc_server
,
2133 _("Production of %s is upgraded to %s in %s."),
2134 improvement_name_translation(producing
),
2135 improvement_name_translation(upgrading
),
2137 pcity
->production
.kind
= VUT_IMPROVEMENT
;
2138 pcity
->production
.value
.building
= upgrading
;
2142 /**************************************************************************
2143 Follow the list of obsoleted_by units until we hit something that
2144 we can build. Return NULL when we can't upgrade at all. NB: returning
2145 something doesn't guarantee that pcity really _can_ build it; just that
2146 pcity can't build whatever _obsoletes_ it.
2148 FIXME: this function is a duplicate of can_upgrade_unittype.
2149 **************************************************************************/
2150 static struct unit_type
*unit_upgrades_to(struct city
*pcity
,
2151 struct unit_type
*punittype
)
2153 struct unit_type
*check
= punittype
;
2154 struct unit_type
*best_upgrade
= U_NOT_OBSOLETED
;
2156 if (!can_city_build_unit_direct(pcity
, punittype
)) {
2157 return U_NOT_OBSOLETED
;
2159 while ((check
= check
->obsoleted_by
) != U_NOT_OBSOLETED
) {
2160 if (can_city_build_unit_direct(pcity
, check
)) {
2161 best_upgrade
= check
;
2165 return best_upgrade
;
2168 /**************************************************************************
2169 Try to upgrade production in pcity.
2170 **************************************************************************/
2171 static void upgrade_unit_prod(struct city
*pcity
)
2173 struct unit_type
*producing
= pcity
->production
.value
.utype
;
2174 struct unit_type
*upgrading
= unit_upgrades_to(pcity
, producing
);
2176 if (upgrading
&& can_city_build_unit_direct(pcity
, upgrading
)) {
2177 notify_player(city_owner(pcity
), city_tile(pcity
),
2178 E_UNIT_UPGRADED
, ftc_server
,
2179 _("Production of %s is upgraded to %s in %s."),
2180 utype_name_translation(producing
),
2181 utype_name_translation(upgrading
),
2183 pcity
->production
.value
.utype
= upgrading
;
2187 /**************************************************************************
2188 Disband units if we don't have enough shields to support them. Returns
2189 FALSE if the _city_ is disbanded as a result.
2190 **************************************************************************/
2191 static bool city_distribute_surplus_shields(struct player
*pplayer
,
2194 if (pcity
->surplus
[O_SHIELD
] < 0) {
2195 unit_list_iterate_safe(pcity
->units_supported
, punit
) {
2196 if (utype_upkeep_cost(unit_type_get(punit
), pplayer
, O_SHIELD
) > 0
2197 && pcity
->surplus
[O_SHIELD
] < 0) {
2198 const char *punit_link
= unit_link(punit
);
2200 /* TODO: Should the unit try to help cities on adjacent tiles? That
2201 * would be a rules change. (This action is performed by the game
2203 if (upkeep_kill_unit(punit
, O_SHIELD
, ULR_DISBANDED
,
2204 game
.info
.muuk_shield_wipe
)) {
2205 notify_player(pplayer
, city_tile(pcity
),
2206 E_UNIT_LOST_MISC
, ftc_server
,
2207 _("%s can't upkeep %s, unit disbanded."),
2208 city_link(pcity
), punit_link
);
2211 /* pcity->surplus[O_SHIELD] is automatically updated. */
2213 } unit_list_iterate_safe_end
;
2216 if (pcity
->surplus
[O_SHIELD
] < 0) {
2217 /* Special case: MissingXProtected. This nasty unit won't go so easily.
2218 * It'd rather make the citizens pay in blood for their failure to upkeep
2219 * it! If we make it here all normal units are already disbanded, so only
2220 * undisbandable ones remain. */
2221 unit_list_iterate_safe(pcity
->units_supported
, punit
) {
2222 int upkeep
= utype_upkeep_cost(unit_type_get(punit
), pplayer
, O_SHIELD
);
2224 if (upkeep
> 0 && pcity
->surplus
[O_SHIELD
] < 0) {
2225 notify_player(pplayer
, city_tile(pcity
),
2226 E_UNIT_LOST_MISC
, ftc_server
,
2227 _("Citizens in %s perish for their failure to "
2229 city_link(pcity
), unit_link(punit
));
2230 if (!city_reduce_size(pcity
, 1, NULL
, "upkeep_failure")) {
2234 /* No upkeep for the unit this turn. */
2235 pcity
->surplus
[O_SHIELD
] += upkeep
;
2237 } unit_list_iterate_safe_end
;
2240 /* Now we confirm changes made last turn. */
2241 pcity
->shield_stock
+= pcity
->surplus
[O_SHIELD
];
2242 pcity
->before_change_shields
= pcity
->shield_stock
;
2243 pcity
->last_turns_shield_surplus
= pcity
->surplus
[O_SHIELD
];
2248 /**************************************************************************
2249 Returns FALSE when the city is removed, TRUE otherwise.
2250 **************************************************************************/
2251 static bool city_build_building(struct player
*pplayer
, struct city
*pcity
)
2255 struct impr_type
*pimprove
= pcity
->production
.value
.building
;
2256 int saved_id
= pcity
->id
;
2258 if (city_production_has_flag(pcity
, IF_GOLD
)) {
2259 fc_assert(pcity
->surplus
[O_SHIELD
] >= 0);
2260 /* pcity->before_change_shields already contains the surplus from
2262 pplayer
->economic
.gold
+= pcity
->before_change_shields
;
2263 pcity
->before_change_shields
= 0;
2264 pcity
->shield_stock
= 0;
2265 choose_build_target(pplayer
, pcity
);
2268 upgrade_building_prod(pcity
);
2270 if (!can_city_build_improvement_now(pcity
, pimprove
)) {
2271 notify_player(pplayer
, city_tile(pcity
), E_CITY_CANTBUILD
, ftc_server
,
2272 _("%s is building %s, which is no longer available."),
2274 city_improvement_name_translation(pcity
, pimprove
));
2275 script_server_signal_emit("building_cant_be_built", 3,
2276 API_TYPE_BUILDING_TYPE
, pimprove
,
2277 API_TYPE_CITY
, pcity
,
2278 API_TYPE_STRING
, "unavailable");
2281 if (pcity
->shield_stock
>= impr_build_shield_cost(pimprove
)) {
2282 if (is_small_wonder(pimprove
)) {
2283 city_list_iterate(pplayer
->cities
, wcity
) {
2284 if (city_has_building(wcity
, pimprove
)) {
2285 city_remove_improvement(wcity
, pimprove
);
2288 } city_list_iterate_end
;
2292 if (get_current_construction_bonus(pcity
, EFT_SS_STRUCTURAL
,
2294 pplayer
->spaceship
.structurals
++;
2295 } else if (get_current_construction_bonus(pcity
, EFT_SS_COMPONENT
,
2297 pplayer
->spaceship
.components
++;
2298 } else if (get_current_construction_bonus(pcity
, EFT_SS_MODULE
,
2300 pplayer
->spaceship
.modules
++;
2303 city_add_improvement(pcity
, pimprove
);
2305 pcity
->before_change_shields
-= impr_build_shield_cost(pimprove
);
2306 pcity
->shield_stock
-= impr_build_shield_cost(pimprove
);
2307 pcity
->turn_last_built
= game
.info
.turn
;
2308 /* to eliminate micromanagement */
2309 if (is_great_wonder(pimprove
)) {
2310 notify_player(NULL
, city_tile(pcity
), E_WONDER_BUILD
, ftc_server
,
2311 _("The %s have finished building %s in %s."),
2312 nation_plural_for_player(pplayer
),
2313 city_improvement_name_translation(pcity
, pimprove
),
2317 notify_player(pplayer
, city_tile(pcity
), E_IMP_BUILD
, ftc_server
,
2318 _("%s has finished building %s."),
2319 city_link(pcity
), improvement_name_translation(pimprove
));
2320 script_server_signal_emit("building_built", 2,
2321 API_TYPE_BUILDING_TYPE
, pimprove
,
2322 API_TYPE_CITY
, pcity
);
2324 if (!city_exist(saved_id
)) {
2325 /* Script removed city */
2329 /* Call this function since some buildings may change the
2330 * the vision range of a city */
2331 city_refresh_vision(pcity
);
2333 if ((mod
= get_current_construction_bonus(pcity
, EFT_GIVE_IMM_TECH
,
2335 struct research
*presearch
= research_get(pplayer
);
2336 char research_name
[MAX_LEN_NAME
* 2];
2338 const char *provider
= improvement_name_translation(pimprove
);
2340 notify_research(presearch
, NULL
, E_TECH_GAIN
, ftc_server
,
2341 PL_("%s boosts research; you gain %d immediate "
2343 "%s boosts research; you gain %d immediate "
2345 mod
), provider
, mod
);
2347 research_pretty_name(presearch
, research_name
, sizeof(research_name
));
2348 for (i
= 0; i
< mod
; i
++) {
2349 Tech_type_id tech
= give_immediate_free_tech(presearch
);
2350 const char *adv_name
= research_advance_name_translation(presearch
, tech
);
2352 notify_research(presearch
, NULL
, E_TECH_GAIN
, ftc_server
,
2353 /* TRANS: Tech from building (Darwin's Voyage) */
2354 Q_("?frombldg:Acquired %s from %s."), adv_name
,
2357 notify_research_embassies(presearch
, NULL
, E_TECH_EMBASSY
, ftc_server
,
2358 /* TRANS: Tech from building (Darwin's
2360 Q_("?frombldg:The %s have acquired %s "
2362 research_name
, adv_name
, provider
);
2365 if (space_part
&& pplayer
->spaceship
.state
== SSHIP_NONE
) {
2366 notify_player(NULL
, city_tile(pcity
), E_SPACESHIP
, ftc_server
,
2367 _("The %s have started building a spaceship!"),
2368 nation_plural_for_player(pplayer
));
2369 pplayer
->spaceship
.state
= SSHIP_STARTED
;
2372 /* space ship part build */
2373 send_spaceship_info(pplayer
, NULL
);
2375 /* Update city data. */
2376 if (city_refresh(pcity
)) {
2377 auto_arrange_workers(pcity
);
2381 /* Move to the next thing in the worklist */
2382 choose_build_target(pplayer
, pcity
);
2388 /**************************************************************************
2389 Build city units. Several units can be built in one turn if the effect
2390 City_Build_Slots is used.
2391 **************************************************************************/
2392 static bool city_build_unit(struct player
*pplayer
, struct city
*pcity
)
2394 struct unit_type
*utype
;
2395 struct worklist
*pwl
= &pcity
->worklist
;;
2396 int unit_shield_cost
, num_units
, i
;
2398 fc_assert_ret_val(pcity
->production
.kind
== VUT_UTYPE
, FALSE
);
2400 /* If the city has already bought a unit which is now obsolete, don't try
2401 * to upgrade the production. The new unit might require more shields, which
2402 * would be bad if it was bought to urgently defend a city. (Equally it
2403 * might be the same cost or cheaper, but tough; you hurried the unit so
2404 * you miss out on technological advances.) */
2405 if (city_can_change_build(pcity
)) {
2406 upgrade_unit_prod(pcity
);
2409 utype
= pcity
->production
.value
.utype
;
2410 unit_shield_cost
= utype_build_shield_cost(utype
);
2412 /* We must make a special case for barbarians here, because they are
2413 so dumb. Really. They don't know the prerequisite techs for units
2414 they build!! - Per */
2415 if (!can_city_build_unit_direct(pcity
, utype
)
2416 && !is_barbarian(pplayer
)) {
2417 notify_player(pplayer
, city_tile(pcity
), E_CITY_CANTBUILD
, ftc_server
,
2418 _("%s is building %s, which is no longer available."),
2419 city_link(pcity
), utype_name_translation(utype
));
2421 /* Log before signal emitting, so pointers are certainly valid */
2422 log_verbose("%s %s tried to build %s, which is not available.",
2423 nation_rule_name(nation_of_city(pcity
)),
2424 city_name_get(pcity
), utype_rule_name(utype
));
2425 script_server_signal_emit("unit_cant_be_built", 3,
2426 API_TYPE_UNIT_TYPE
, utype
,
2427 API_TYPE_CITY
, pcity
,
2428 API_TYPE_STRING
, "unavailable");
2432 if (pcity
->shield_stock
>= unit_shield_cost
) {
2433 int pop_cost
= utype_pop_value(utype
);
2435 int saved_city_id
= pcity
->id
;
2437 /* Should we disband the city? -- Massimo */
2438 if (city_size_get(pcity
) == pop_cost
2439 && is_city_option_set(pcity
, CITYO_DISBAND
)) {
2440 return !disband_city(pcity
);
2443 if (city_size_get(pcity
) <= pop_cost
) {
2444 notify_player(pplayer
, city_tile(pcity
), E_CITY_CANTBUILD
, ftc_server
,
2445 /* TRANS: city ... utype ... size ... pop_cost */
2446 _("%s can't build %s yet. "
2447 "(city size: %d, unit population cost: %d)"),
2448 city_link(pcity
), utype_name_translation(utype
),
2449 city_size_get(pcity
), pop_cost
);
2450 script_server_signal_emit("unit_cant_be_built", 3,
2451 API_TYPE_UNIT_TYPE
, utype
,
2452 API_TYPE_CITY
, pcity
,
2453 API_TYPE_STRING
, "pop_cost");
2457 fc_assert(pop_cost
== 0 || city_size_get(pcity
) >= pop_cost
);
2459 /* don't update turn_last_built if we returned above */
2460 pcity
->turn_last_built
= game
.info
.turn
;
2462 /* check if we can build more than one unit (effect City_Build_Slots) */
2463 (void) city_production_build_units(pcity
, FALSE
, &num_units
);
2465 /* We should be able to build at least one (by checks above) */
2466 fc_assert(num_units
>= 1);
2468 for (i
= 0; i
< num_units
; i
++) {
2469 punit
= create_unit(pplayer
, pcity
->tile
, utype
,
2470 do_make_unit_veteran(pcity
, utype
),
2472 pplayer
->score
.units_built
++;
2474 /* After we created the unit remove the citizen. This will also
2475 * rearrange the worker to take into account the extra resources
2478 city_reduce_size(pcity
, pop_cost
, NULL
, "unit_built");
2481 /* to eliminate micromanagement, we only subtract the unit's cost */
2482 pcity
->before_change_shields
-= unit_shield_cost
;
2483 pcity
->shield_stock
-= unit_shield_cost
;
2485 notify_player(pplayer
, city_tile(pcity
), E_UNIT_BUILT
, ftc_server
,
2486 /* TRANS: <city> is finished building <unit/building>. */
2487 _("%s is finished building %s."),
2488 city_link(pcity
), utype_name_translation(utype
));
2491 /* Additional message if the unit has population cost. */
2492 notify_player(pplayer
, city_tile(pcity
), E_UNIT_BUILT_POP_COST
,
2494 /* TRANS: "<unit> cost... <city> shrinks..."
2495 * Plural in "%d population", not "size %d". */
2496 PL_("%s cost %d population. %s shrinks to size %d.",
2497 "%s cost %d population. %s shrinks to size %d.",
2499 utype_name_translation(utype
), pop_cost
,
2500 city_link(pcity
), city_size_get(pcity
));
2503 script_server_signal_emit("unit_built", 2,
2504 API_TYPE_UNIT
, punit
,
2505 API_TYPE_CITY
, pcity
);
2507 /* check if the city still exists */
2508 if (!city_exist(saved_city_id
)) {
2512 if (i
!= 0 && worklist_length(pwl
) > 0) {
2513 /* remove the build unit from the worklist; it has to be one less
2514 * than units build to preserve the next build target from the
2516 worklist_remove(pwl
, 0);
2520 if (city_exist(saved_city_id
)) {
2521 /* Done building this unit; time to move on to the next. */
2522 choose_build_target(pplayer
, pcity
);
2529 /**************************************************************************
2530 Returns FALSE when the city is removed, TRUE otherwise.
2531 **************************************************************************/
2532 static bool city_build_stuff(struct player
*pplayer
, struct city
*pcity
)
2534 if (!city_distribute_surplus_shields(pplayer
, pcity
)) {
2538 nullify_caravan_and_disband_plus(pcity
);
2539 define_orig_production_values(pcity
);
2541 switch (pcity
->production
.kind
) {
2542 case VUT_IMPROVEMENT
:
2543 return city_build_building(pplayer
, pcity
);
2545 return city_build_unit(pplayer
, pcity
);
2547 /* must never happen! */
2554 /**************************************************************************
2555 Randomly sell a building from the given list. Returns TRUE if a building
2558 NB: It is assumed that gold upkeep for the buildings has already been
2559 paid this turn, hence when a building is sold its upkeep is given back
2561 NB: The contents of 'imprs' are usually mangled by this function.
2562 NB: It is assumed that all buildings in 'imprs' can be sold.
2563 **************************************************************************/
2564 static bool sell_random_building(struct player
*pplayer
,
2565 struct cityimpr_list
*imprs
)
2567 struct cityimpr
*pcityimpr
;
2570 fc_assert_ret_val(pplayer
!= NULL
, FALSE
);
2572 if (!imprs
|| cityimpr_list_size(imprs
) == 0) {
2576 r
= fc_rand(cityimpr_list_size(imprs
));
2577 pcityimpr
= cityimpr_list_get(imprs
, r
);
2579 notify_player(pplayer
, city_tile(pcityimpr
->pcity
), E_IMP_AUCTIONED
,
2581 _("Can't afford to maintain %s in %s, building sold!"),
2582 improvement_name_translation(pcityimpr
->pimprove
),
2583 city_link(pcityimpr
->pcity
));
2584 log_debug("%s: sold building (%s)", player_name(pplayer
),
2585 improvement_name_translation(pcityimpr
->pimprove
));
2587 do_sell_building(pplayer
, pcityimpr
->pcity
, pcityimpr
->pimprove
);
2589 cityimpr_list_remove(imprs
, pcityimpr
);
2591 /* Get back the gold upkeep that was already paid this turn. */
2592 pplayer
->economic
.gold
+= city_improvement_upkeep(pcityimpr
->pcity
,
2593 pcityimpr
->pimprove
);
2595 city_refresh_queue_add(pcityimpr
->pcity
);
2602 /**************************************************************************
2603 Call back for when a unit in uk_rem_gold dies.
2605 A unit can die as a side effect of an action another unit in the list is
2606 forced to perform. This isn't limited to "Explode Nuclear". A Lua call
2607 back for another action could cause more collateral damage than "Explode
2609 **************************************************************************/
2610 static void uk_rem_gold_callback(struct unit
*punit
)
2614 /* Remove the unit from uk_rem_gold. */
2615 unit_list_remove(uk_rem_gold
, punit
);
2617 gold_upkeep
= punit
->server
.upkeep_payed
[O_GOLD
];
2619 /* All units in uk_rem_gold should have gold upkeep! */
2620 fc_assert_ret_msg(gold_upkeep
> 0, "%s has %d gold upkeep",
2621 unit_rule_name(punit
), gold_upkeep
);
2623 /* Get the upkeep gold back. */
2624 unit_owner(punit
)->economic
.gold
+= gold_upkeep
;
2627 /**************************************************************************
2628 Add a unit to uk_rem_gold and make the unit remove it self from it if
2629 it dies before it is processed.
2630 **************************************************************************/
2631 static void uk_rem_gold_append(struct unit
*punit
)
2633 /* Make the unit aware that it is on the uk_rem_gold list. */
2634 unit_set_removal_callback(punit
, uk_rem_gold_callback
);
2636 /* Add the unit to the list. */
2637 unit_list_append(uk_rem_gold
, punit
);
2640 /**************************************************************************
2641 Destroy a unit list and make the units it contains aware that it no
2642 longer refers to them.
2643 **************************************************************************/
2644 static void unit_list_referred_destroy(struct unit_list
*punitlist
)
2646 unit_list_iterate(punitlist
, punit
) {
2647 /* Clear the unit's knowledge of the list. */
2648 unit_unset_removal_callback(punit
);
2649 } unit_list_iterate_end
;
2651 /* Destroy the list it self. */
2652 unit_list_destroy(punitlist
);
2655 /**************************************************************************
2656 Randomly "sell" a unit from the given list. Returns pointer to sold unit.
2657 This pointer is not valid any more, but can be removed from the lists.
2659 NB: It is assumed that gold upkeep for the units has already been paid
2660 this turn, hence when a unit is "sold" its upkeep is given back to the
2662 NB: The contents of 'units' are usually mangled by this function.
2663 NB: It is assumed that all units in 'units' have positive gold upkeep.
2664 **************************************************************************/
2665 static struct unit
*sell_random_unit(struct player
*pplayer
,
2666 struct unit_list
*punitlist
)
2670 struct unit_list
*cargo
;
2672 fc_assert_ret_val(pplayer
!= NULL
, FALSE
);
2674 if (!punitlist
|| unit_list_size(punitlist
) == 0) {
2678 r
= fc_rand(unit_list_size(punitlist
));
2679 punit
= unit_list_get(punitlist
, r
);
2681 cargo
= unit_list_new();
2683 /* Check if unit is transporting other units from punitlist,
2684 * and sell one of those (recursively) instead. */
2685 unit_list_iterate(unit_transport_cargo(punit
), pcargo
) {
2686 /* Optimization, do not iterate over punitlist
2687 * if we are sure that pcargo is not in it. */
2688 if (pcargo
->server
.upkeep_payed
[O_GOLD
] > 0) {
2689 unit_list_iterate(punitlist
, p2
) {
2691 unit_list_append(cargo
, pcargo
);
2693 } unit_list_iterate_end
;
2695 } unit_list_iterate_end
;
2697 if (unit_list_size(cargo
) > 0) {
2698 struct unit
*ret
= sell_random_unit(pplayer
, cargo
);
2700 unit_list_destroy(cargo
);
2705 unit_list_destroy(cargo
);
2708 const char *punit_link
= unit_tile_link(punit
);
2709 #ifdef FREECIV_DEBUG
2710 const char *punit_logname
= unit_name_translation(punit
);
2711 #endif /* FREECIV_DEBUG */
2712 struct tile
*utile
= unit_tile(punit
);
2714 if (upkeep_kill_unit(punit
, O_GOLD
, ULR_SOLD
,
2715 game
.info
.muuk_gold_wipe
)) {
2716 unit_list_remove(punitlist
, punit
);
2718 /* The gold was payed back when the unit removal made
2719 * uk_rem_gold_callback() run as the unit's removal call back. */
2721 notify_player(pplayer
, utile
, E_UNIT_LOST_MISC
, ftc_server
,
2722 _("Not enough gold. %s disbanded."),
2724 log_debug("%s: unit sold (%s)", player_name(pplayer
),
2727 /* Not able to get rid of punit */
2735 /**************************************************************************
2736 Balance the gold of a nation by selling some random units and buildings.
2737 **************************************************************************/
2738 static bool player_balance_treasury_units_and_buildings
2739 (struct player
*pplayer
)
2741 struct cityimpr_list
*pimprlist
;
2742 bool sell_unit
= TRUE
;
2748 pimprlist
= cityimpr_list_new();
2749 uk_rem_gold
= unit_list_new();
2751 city_list_iterate(pplayer
->cities
, pcity
) {
2752 city_built_iterate(pcity
, pimprove
) {
2753 if (can_city_sell_building(pcity
, pimprove
)) {
2754 struct cityimpr
*ci
= fc_malloc(sizeof(*ci
));
2757 ci
->pimprove
= pimprove
;
2758 cityimpr_list_append(pimprlist
, ci
);
2760 } city_built_iterate_end
;
2762 unit_list_iterate(pcity
->units_supported
, punit
) {
2763 if (punit
->server
.upkeep_payed
[O_GOLD
] > 0) {
2764 uk_rem_gold_append(punit
);
2766 } unit_list_iterate_end
;
2767 } city_list_iterate_end
;
2769 while (pplayer
->economic
.gold
< 0
2770 && (cityimpr_list_size(pimprlist
) > 0
2771 || unit_list_size(uk_rem_gold
) > 0)) {
2772 if ((!sell_unit
&& cityimpr_list_size(pimprlist
) > 0)
2773 || unit_list_size(uk_rem_gold
) == 0) {
2774 sell_random_building(pplayer
, pimprlist
);
2776 sell_random_unit(pplayer
, uk_rem_gold
);
2778 sell_unit
= !sell_unit
;
2781 /* Free remaining entries from list */
2782 cityimpr_list_iterate(pimprlist
, pimpr
) {
2784 } cityimpr_list_iterate_end
;
2786 if (pplayer
->economic
.gold
< 0) {
2787 /* If we get here it means the player has
2788 * negative gold. This should never happen. */
2789 fc_assert_msg(FALSE
, "Player %s (nb %d) cannot have negative gold!",
2790 player_name(pplayer
), player_number(pplayer
));
2793 cityimpr_list_destroy(pimprlist
);
2794 unit_list_referred_destroy(uk_rem_gold
);
2796 return pplayer
->economic
.gold
>= 0;
2799 /**************************************************************************
2800 Balance the gold of a nation by selling some units which need gold upkeep.
2801 **************************************************************************/
2802 static bool player_balance_treasury_units(struct player
*pplayer
)
2808 uk_rem_gold
= unit_list_new();
2810 city_list_iterate(pplayer
->cities
, pcity
) {
2811 unit_list_iterate(pcity
->units_supported
, punit
) {
2812 if (punit
->server
.upkeep_payed
[O_GOLD
] > 0) {
2813 uk_rem_gold_append(punit
);
2815 } unit_list_iterate_end
;
2816 } city_list_iterate_end
;
2818 while (pplayer
->economic
.gold
< 0
2819 && sell_random_unit(pplayer
, uk_rem_gold
)) {
2820 /* all done in sell_random_unit() */
2823 if (pplayer
->economic
.gold
< 0) {
2824 /* If we get here it means the player has
2825 * negative gold. This should never happen. */
2826 fc_assert_msg(FALSE
, "Player %s (nb %d) cannot have negative gold!",
2827 player_name(pplayer
), player_number(pplayer
));
2830 unit_list_referred_destroy(uk_rem_gold
);
2832 return pplayer
->economic
.gold
>= 0;
2835 /**************************************************************************
2836 Balance the gold of one city by randomly selling some buildings.
2837 **************************************************************************/
2838 static bool city_balance_treasury_buildings(struct city
*pcity
)
2840 struct player
*pplayer
;
2841 struct cityimpr_list
*pimprlist
;
2847 pplayer
= city_owner(pcity
);
2848 pimprlist
= cityimpr_list_new();
2850 /* Create a vector of all buildings that can be sold. */
2851 city_built_iterate(pcity
, pimprove
) {
2852 if (can_city_sell_building(pcity
, pimprove
)) {
2853 struct cityimpr
*ci
= fc_malloc(sizeof(*ci
));
2856 ci
->pimprove
= pimprove
;
2857 cityimpr_list_append(pimprlist
, ci
);
2859 } city_built_iterate_end
;
2861 /* Try to sell some buildings. */
2862 while (pplayer
->economic
.gold
< 0
2863 && sell_random_building(pplayer
, pimprlist
)) {
2864 /* all done in sell_random_building */
2867 /* Free remaining entries from list */
2868 cityimpr_list_iterate(pimprlist
, pimpr
) {
2870 } cityimpr_list_iterate_end
;
2872 cityimpr_list_destroy(pimprlist
);
2874 return pplayer
->economic
.gold
>= 0;
2877 /**************************************************************************
2878 Balance the gold of one city by randomly selling some units which need
2881 NB: This function adds the gold upkeep of disbanded units back to the
2882 player's gold. Hence it assumes that this gold was previously taken
2883 from the player (i.e. in update_city_activity()).
2884 **************************************************************************/
2885 static bool city_balance_treasury_units(struct city
*pcity
)
2887 struct player
*pplayer
;
2893 pplayer
= city_owner(pcity
);
2894 uk_rem_gold
= unit_list_new();
2896 /* Create a vector of all supported units with gold upkeep. */
2897 unit_list_iterate(pcity
->units_supported
, punit
) {
2898 if (punit
->server
.upkeep_payed
[O_GOLD
] > 0) {
2899 uk_rem_gold_append(punit
);
2901 } unit_list_iterate_end
;
2903 /* Still not enough gold, so try "selling" some units. */
2904 while (pplayer
->economic
.gold
< 0
2905 && sell_random_unit(pplayer
, uk_rem_gold
)) {
2906 /* all done in sell_random_unit() */
2909 /* If we get here the player has negative gold, but hopefully
2910 * another city will be able to pay the deficit, so continue. */
2912 unit_list_referred_destroy(uk_rem_gold
);
2914 return pplayer
->economic
.gold
>= 0;
2917 /**************************************************************************
2918 Add some Pollution if we have waste
2919 **************************************************************************/
2920 static bool place_pollution(struct city
*pcity
, enum extra_cause cause
)
2923 struct tile
*pcenter
= city_tile(pcity
);
2924 int city_radius_sq
= city_map_radius_sq_get(pcity
);
2928 /* place pollution on a random city tile */
2930 int tile_id
= fc_rand(city_map_tiles(city_radius_sq
));
2931 struct extra_type
*pextra
;
2933 city_tile_index_to_xy(&cx
, &cy
, tile_id
, city_radius_sq
);
2935 /* check for a a real map position */
2936 if (!(ptile
= city_map_to_tile(pcenter
, city_radius_sq
, cx
, cy
))) {
2940 pextra
= rand_extra_for_tile(ptile
, cause
);
2942 if (pextra
!= NULL
&& !tile_has_extra(ptile
, pextra
)) {
2943 tile_add_extra(ptile
, pextra
);
2944 update_tile_knowledge(ptile
);
2950 log_debug("pollution not placed: city: %s", city_name_get(pcity
));
2955 /**************************************************************************
2956 Add some Pollution if we have waste
2957 **************************************************************************/
2958 static void check_pollution(struct city
*pcity
)
2960 if (pcity
->pollution
!= 0 && fc_rand(100) <= pcity
->pollution
) {
2961 if (place_pollution(pcity
, EC_POLLUTION
)) {
2962 notify_player(city_owner(pcity
), city_tile(pcity
), E_POLLUTION
, ftc_server
,
2963 _("Pollution near %s."), city_link(pcity
));
2968 /**************************************************************************
2969 Returns the cost to incite a city. This depends on the size of the city,
2970 the number of happy, unhappy and angry citizens, whether it is
2971 celebrating, how close it is to the capital, how many units it has and
2972 upkeeps, presence of courthouse, its buildings and wonders, and who
2973 originally built it.
2974 **************************************************************************/
2975 int city_incite_cost(struct player
*pplayer
, struct city
*pcity
)
2977 struct city
*capital
;
2979 double cost
; /* Intermediate values can get very large */
2982 cost
= city_owner(pcity
)->economic
.gold
+ game
.server
.base_incite_cost
;
2984 unit_list_iterate(pcity
->tile
->units
, punit
) {
2985 cost
+= (unit_build_shield_cost(punit
)
2986 * game
.server
.incite_unit_factor
);
2987 } unit_list_iterate_end
;
2990 city_built_iterate(pcity
, pimprove
) {
2991 cost
+= impr_build_shield_cost(pimprove
)
2992 * game
.server
.incite_improvement_factor
;
2993 } city_built_iterate_end
;
2995 /* Stability bonuses */
2996 if (!city_unhappy(pcity
)) {
2999 if (city_celebrating(pcity
)) {
3003 /* Buy back is cheap, conquered cities are also cheap */
3004 if (!game
.info
.citizen_nationality
) {
3005 if (city_owner(pcity
) != pcity
->original
) {
3006 if (pplayer
== pcity
->original
) {
3007 cost
/= 2; /* buy back: 50% price reduction */
3009 cost
= cost
* 2 / 3; /* buy conquered: 33% price reduction */
3014 /* Distance from capital */
3015 capital
= player_capital(city_owner(pcity
));
3017 int tmp
= map_distance(capital
->tile
, pcity
->tile
);
3018 dist
= MIN(32, tmp
);
3020 /* No capital? Take max penalty! */
3024 size
= MAX(1, city_size_get(pcity
)
3025 + pcity
->feel
[CITIZEN_HAPPY
][FEELING_FINAL
]
3026 - pcity
->feel
[CITIZEN_UNHAPPY
][FEELING_FINAL
]
3027 - pcity
->feel
[CITIZEN_ANGRY
][FEELING_FINAL
] * 3);
3029 cost
*= game
.server
.incite_total_factor
;
3030 cost
= cost
/ (dist
+ 3);
3032 if (game
.info
.citizen_nationality
) {
3033 int cost_per_citizen
= cost
/ pcity
->size
;
3034 int natives
= citizens_nation_get(pcity
, city_owner(pcity
)->slot
);
3035 int tgt_cit
= citizens_nation_get(pcity
, pplayer
->slot
);
3036 int third_party
= pcity
->size
- natives
- tgt_cit
;
3038 cost
= cost_per_citizen
* (natives
+ 0.7 * third_party
+ 0.5 * tgt_cit
);
3041 cost
+= (cost
* get_city_bonus(pcity
, EFT_INCITE_COST_PCT
)) / 100;
3044 if (cost
>= INCITE_IMPOSSIBLE_COST
) {
3045 return INCITE_IMPOSSIBLE_COST
;
3051 /**************************************************************************
3052 Called every turn, at beginning of turn, for every city.
3053 **************************************************************************/
3054 static void define_orig_production_values(struct city
*pcity
)
3056 /* Remember what this city is building last turn, so that on the next turn
3057 * the player can switch production to something else and then change it
3058 * back without penalty. This has to be updated _before_ production for
3059 * this turn is calculated, so that the penalty will apply if the player
3060 * changes production away from what has just been completed. This makes
3061 * sense if you consider what this value means: all the shields in the
3062 * city have been dedicated toward the project that was chosen last turn,
3063 * so the player shouldn't be penalized if the governor has to pick
3064 * something different. See city_change_production_penalty(). */
3065 pcity
->changed_from
= pcity
->production
;
3067 log_debug("In %s, building %s. Beg of Turn shields = %d",
3068 city_name_get(pcity
), universal_rule_name(&pcity
->changed_from
),
3069 pcity
->before_change_shields
);
3072 /**************************************************************************
3073 Let the advisor set up city building target.
3074 **************************************************************************/
3075 static void nullify_caravan_and_disband_plus(struct city
*pcity
)
3077 pcity
->disbanded_shields
=0;
3078 pcity
->caravan_shields
=0;
3081 /**************************************************************************
3082 Initialize all variables containing information about production
3083 before it was changed.
3084 **************************************************************************/
3085 void nullify_prechange_production(struct city
*pcity
)
3087 nullify_caravan_and_disband_plus(pcity
);
3088 pcity
->before_change_shields
=0;
3091 /**************************************************************************
3092 Called every turn, at end of turn, for every city.
3093 **************************************************************************/
3094 static void update_city_activity(struct city
*pcity
)
3096 struct player
*pplayer
;
3097 struct government
*gov
;
3103 pplayer
= city_owner(pcity
);
3104 gov
= government_of_city(pcity
);
3106 if (city_refresh(pcity
)) {
3107 auto_arrange_workers(pcity
);
3110 /* Reporting of celebrations rewritten, copying the treatment of disorder below,
3111 with the added rapture rounds count. 991219 -- Jing */
3112 if (city_build_stuff(pplayer
, pcity
)) {
3114 int revolution_turns
;
3116 pcity
->history
+= city_history_gain(pcity
);
3118 /* History can decrease, but never go below zero */
3119 pcity
->history
= MAX(pcity
->history
, 0);
3121 if (city_celebrating(pcity
)) {
3123 if (pcity
->rapture
== 1) {
3124 notify_player(pplayer
, city_tile(pcity
), E_CITY_LOVE
, ftc_server
,
3125 _("Celebrations in your honor in %s."),
3129 if (pcity
->rapture
!= 0) {
3130 notify_player(pplayer
, city_tile(pcity
), E_CITY_NORMAL
, ftc_server
,
3131 _("Celebrations canceled in %s."),
3136 pcity
->was_happy
= city_happy(pcity
);
3138 /* Handle the illness. */
3139 if (game
.info
.illness_on
) {
3140 /* recalculate city illness; illness due to trade has to be saved
3141 * within the city struct as the client has not all data to
3143 pcity
->server
.illness
3144 = city_illness_calc(pcity
, NULL
, NULL
, &(pcity
->illness_trade
), NULL
);
3146 if (city_illness_check(pcity
)) {
3147 notify_player(pplayer
, city_tile(pcity
), E_CITY_PLAGUE
, ftc_server
,
3148 _("%s has been struck by a plague! Population lost!"),
3150 city_reduce_size(pcity
, 1, NULL
, "plague");
3151 pcity
->turn_plague
= game
.info
.turn
;
3153 /* recalculate illness */
3154 pcity
->server
.illness
3155 = city_illness_calc(pcity
, NULL
, NULL
, &(pcity
->illness_trade
),
3160 /* City population updated here, after the rapture stuff above. --Jing */
3161 saved_id
= pcity
->id
;
3162 city_populate(pcity
, pplayer
);
3163 if (NULL
== player_city_by_number(pplayer
, saved_id
)) {
3167 pcity
->did_sell
= FALSE
;
3168 pcity
->did_buy
= FALSE
;
3169 pcity
->airlift
= city_airlift_max(pcity
);
3170 update_bulbs(pplayer
, pcity
->prod
[O_SCIENCE
], FALSE
);
3172 /* Update the treasury. */
3173 pplayer
->economic
.gold
+= pcity
->prod
[O_GOLD
];
3174 pplayer
->economic
.gold
-= city_total_impr_gold_upkeep(pcity
);
3175 pplayer
->economic
.gold
-= city_total_unit_gold_upkeep(pcity
);
3177 /* Remember how much gold upkeep each unit was payed. */
3178 unit_list_iterate(pcity
->units_supported
, punit
) {
3179 punit
->server
.upkeep_payed
[O_GOLD
] = punit
->upkeep
[O_GOLD
];
3180 } unit_list_iterate_end
;
3182 if (pplayer
->economic
.gold
< 0) {
3183 /* Not enough gold - we have to sell some buildings, and if that
3184 * is not enough, disband units with gold upkeep, taking into
3185 * account the setting of 'game.info.gold_upkeep_style':
3186 * GOLD_UPKEEP_CITY: Cities pay for buildings and units.
3187 * GOLD_UPKEEP_MIXED: Cities pay only for buildings; the nation pays
3189 * GOLD_UPKEEP_NATION: The nation pays for buildings and units. */
3190 switch (game
.info
.gold_upkeep_style
) {
3191 case GOLD_UPKEEP_CITY
:
3192 case GOLD_UPKEEP_MIXED
:
3193 if (!city_balance_treasury_buildings(pcity
)
3194 && game
.info
.gold_upkeep_style
== GOLD_UPKEEP_CITY
) {
3195 city_balance_treasury_units(pcity
);
3198 case GOLD_UPKEEP_NATION
:
3203 revolution_turns
= get_city_bonus(pcity
, EFT_REVOLUTION_UNHAPPINESS
);
3204 if (city_unhappy(pcity
)) {
3205 const char *revomsg
;
3208 if (pcity
->anarchy
== revolution_turns
) {
3209 /* Revolution next turn if not dealt with */
3210 /* TRANS: preserve leading space; this string will be appended to
3211 * another sentence */
3212 revomsg
= _(" Unrest threatens to spread beyond the city.");
3216 if (pcity
->anarchy
== 1) {
3217 notify_player(pplayer
, city_tile(pcity
), E_CITY_DISORDER
, ftc_server
,
3218 /* TRANS: second %s is an optional extra sentence */
3219 _("Civil disorder in %s.%s"),
3220 city_link(pcity
), revomsg
);
3222 notify_player(pplayer
, city_tile(pcity
), E_CITY_DISORDER
, ftc_server
,
3223 /* TRANS: second %s is an optional extra sentence */
3224 _("CIVIL DISORDER CONTINUES in %s.%s"),
3225 city_link(pcity
), revomsg
);
3228 if (pcity
->anarchy
!= 0) {
3229 notify_player(pplayer
, city_tile(pcity
), E_CITY_NORMAL
, ftc_server
,
3230 _("Order restored in %s."),
3235 check_pollution(pcity
);
3237 send_city_info(NULL
, pcity
);
3239 if (revolution_turns
> 0 && pcity
->anarchy
> revolution_turns
) {
3240 notify_player(pplayer
, city_tile(pcity
), E_ANARCHY
, ftc_server
,
3241 /* TRANS: %s - government form, e.g., Democracy */
3242 _("The people have overthrown your %s, "
3243 "your country is in turmoil."),
3244 government_name_translation(gov
));
3245 handle_player_change_government(pplayer
, government_number(gov
));
3247 if (city_refresh(pcity
)) {
3248 auto_arrange_workers(pcity
);
3250 sanity_check_city(pcity
);
3254 /*****************************************************************************
3255 check if city suffers from a plague. Return TRUE if it does, FALSE if not.
3256 ****************************************************************************/
3257 static bool city_illness_check(const struct city
* pcity
)
3259 if (fc_rand(1000) < pcity
->server
.illness
) {
3266 /**************************************************************************
3267 Disband a city into the built unit, supported by the closest city.
3268 **************************************************************************/
3269 static bool disband_city(struct city
*pcity
)
3271 struct player
*pplayer
= city_owner(pcity
);
3272 struct tile
*ptile
= pcity
->tile
;
3273 struct city
*rcity
=NULL
;
3274 struct unit_type
*utype
= pcity
->production
.value
.utype
;
3275 int saved_id
= pcity
->id
;
3277 /* find closest city other than pcity */
3278 rcity
= find_closest_city(ptile
, pcity
, pplayer
, FALSE
, FALSE
, FALSE
, TRUE
,
3282 /* What should we do when we try to disband our only city? */
3283 notify_player(pplayer
, ptile
, E_CITY_CANTBUILD
, ftc_server
,
3284 _("%s can't build %s yet, "
3285 "and we can't disband our only city."),
3286 city_link(pcity
), utype_name_translation(utype
));
3287 script_server_signal_emit("unit_cant_be_built", 3,
3288 API_TYPE_UNIT_TYPE
, utype
,
3289 API_TYPE_CITY
, pcity
,
3290 API_TYPE_STRING
, "pop_cost");
3291 if (!city_exist(saved_id
)) {
3292 /* Script decided to remove even the last city */
3299 (void) create_unit(pplayer
, ptile
, utype
,
3300 do_make_unit_veteran(pcity
, utype
),
3302 pplayer
->score
.units_built
++;
3304 /* Shift all the units supported by pcity (including the new unit)
3305 * to rcity. transfer_city_units does not make sure no units are
3306 * left floating without a transport, but since all units are
3307 * transferred this is not a problem. */
3308 transfer_city_units(pplayer
, pplayer
, pcity
->units_supported
, rcity
,
3311 notify_player(pplayer
, ptile
, E_UNIT_BUILT
, ftc_server
,
3312 /* TRANS: "<city> is disbanded into Settler." */
3313 _("%s is disbanded into %s."),
3314 city_tile_link(pcity
), utype_name_translation(utype
));
3316 script_server_signal_emit("city_destroyed", 3,
3317 API_TYPE_CITY
, pcity
,
3318 API_TYPE_PLAYER
, pcity
->owner
,
3319 API_TYPE_PLAYER
, NULL
);
3325 /***************************************************************************
3326 Helper function to calculate a "score" of a city. The score is used to get
3327 an estimate of the "migration desirability" of the city. The higher the
3328 score the more likely citizens will migrate to it.
3330 The score depends on the city size, the feeling of its citizens, the cost
3331 of all buildings in the city, and the surplus of trade, luxury and
3335 score = ([city size] + feeling) * factors
3337 * feeling of the citizens
3338 feeling = 1.00 * happy citizens
3339 + 0.00 * content citizens
3340 - 0.25 * unhappy citizens
3341 - 0.50 * angry citizens
3344 * the build costs of all buildings
3345 f = (1 + (1 - exp(-[build shield cost]/1000))/5)
3346 * the trade of the city
3347 f = (1 + (1 - exp(-[city surplus trade]/100))/5)
3348 * the luxury within the city
3349 f = (1 + (1 - exp(-[city surplus luxury]/100))/5)
3350 * the science within the city
3351 f = (1 + (1 - exp(-[city surplus science]/100))/5)
3353 all factors f have values between 1 and 1.2; the overall factor will be
3354 between 1.0 (smaller cities) and 2.0 (bigger cities)
3356 [build shield cost], [city surplus trade], [city surplus luxury] and
3357 [city surplus science] _must_ be >= 0!
3359 The food furplus is considered by an additional factor
3361 * the food surplus of the city
3362 f = (1 + [city surplus food; interval -10..20]/10)
3364 The health factor is defined as:
3366 * the health of the city
3367 f = (100 - [city illness; tenth of %]/25)
3369 * if the city has at least one wonder a factor of 1.25 is added
3370 * for the capital an additional factor of 1.25 is used
3371 * the score is also modified by the effect EFT_MIGRATION_PCT
3372 **************************************************************************/
3373 static float city_migration_score(struct city
*pcity
)
3376 int build_shield_cost
= 0;
3377 bool has_wonder
= FALSE
;
3383 if (pcity
->server
.mgr_score_calc_turn
== game
.info
.turn
) {
3384 /* up-to-date migration score */
3385 return pcity
->server
.migration_score
;
3388 /* feeling of the citizens */
3389 score
= (city_size_get(pcity
)
3390 + 1.00 * pcity
->feel
[CITIZEN_HAPPY
][FEELING_FINAL
]
3391 + 0.00 * pcity
->feel
[CITIZEN_CONTENT
][FEELING_FINAL
]
3392 - 0.25 * pcity
->feel
[CITIZEN_UNHAPPY
][FEELING_FINAL
]
3393 - 0.50 * pcity
->feel
[CITIZEN_ANGRY
][FEELING_FINAL
]);
3395 /* calculate shield build cost for all buildings */
3396 city_built_iterate(pcity
, pimprove
) {
3397 build_shield_cost
+= impr_build_shield_cost(pimprove
);
3398 if (is_wonder(pimprove
)) {
3399 /* this city has a wonder */
3402 } city_built_iterate_end
;
3404 /* take shield costs of all buidings into account; normalized by 1000 */
3405 score
*= (1 + (1 - exp(- (float) MAX(0, build_shield_cost
) / 1000)) / 5);
3406 /* take trade into account; normalized by 100 */
3407 score
*= (1 + (1 - exp(- (float) MAX(0, pcity
->surplus
[O_TRADE
]) / 100))
3409 /* take luxury into account; normalized by 100 */
3410 score
*= (1 + (1 - exp(- (float) MAX(0, pcity
->surplus
[O_LUXURY
]) / 100))
3412 /* take science into account; normalized by 100 */
3413 score
*= (1 + (1 - exp(- (float) MAX(0, pcity
->surplus
[O_SCIENCE
]) / 100))
3416 score
+= city_culture(pcity
) * game
.info
.culture_migration_pml
/ 1000;
3418 /* Take food into account; the food surplus is clipped to values between
3419 * -10..20 and normalize by 10. Thus, the factor is between 0.9 and 1.2. */
3420 score
*= (1 + (float) CLIP(-10, pcity
->surplus
[O_FOOD
], 20) / 10 );
3422 /* Reduce the score due to city illness (plague). The illness is given in
3423 * tenth of percent (0..1000) and normalized by 25. Thus, this factor is
3424 * between 0.6 (ill city) and 1.0 (health city). */
3425 score
*= (100 - (float)city_illness_calc(pcity
, NULL
, NULL
, NULL
, NULL
)
3429 /* people like wonders */
3433 if (is_capital(pcity
)) {
3434 /* the capital is a magnet for the citizens */
3438 /* take into account effects */
3439 score
*= (1.0 + get_city_bonus(pcity
, EFT_MIGRATION_PCT
) / 100.0);
3441 log_debug("[M] %s score: %.3f", city_name_get(pcity
), score
);
3443 /* set migration score for the city */
3444 pcity
->server
.migration_score
= score
;
3445 /* set the turn, when the score was calculated */
3446 pcity
->server
.mgr_score_calc_turn
= game
.info
.turn
;
3451 /**************************************************************************
3452 Do the migrations between the cities that overlap, if the growth of the
3453 target city is not blocked due to a missing improvement or missing food.
3455 Returns TRUE if migration occurred.
3456 **************************************************************************/
3457 static bool do_city_migration(struct city
*pcity_from
,
3458 struct city
*pcity_to
)
3460 struct player
*pplayer_from
, *pplayer_to
, *pplayer_citizen
;
3461 struct tile
*ptile_from
, *ptile_to
;
3462 char name_from
[MAX_LEN_LINK
], name_to
[MAX_LEN_LINK
];
3463 const char *nation_from
, *nation_to
;
3464 struct city
*rcity
= NULL
;
3466 if (!pcity_from
|| !pcity_to
) {
3470 pplayer_from
= city_owner(pcity_from
);
3471 pplayer_citizen
= pplayer_from
;
3472 pplayer_to
= city_owner(pcity_to
);
3473 /* We copy that, because city_link always returns the same pointer. */
3474 sz_strlcpy(name_from
, city_link(pcity_from
));
3475 sz_strlcpy(name_to
, city_link(pcity_to
));
3476 nation_from
= nation_adjective_for_player(pplayer_from
);
3477 nation_to
= nation_adjective_for_player(pplayer_to
);
3478 ptile_from
= city_tile(pcity_from
);
3479 ptile_to
= city_tile(pcity_to
);
3481 /* check food supply in the receiver city */
3482 if (game
.server
.mgr_foodneeded
) {
3483 bool migration
= FALSE
;
3485 if (pcity_to
->surplus
[O_FOOD
] >= game
.info
.food_cost
) {
3488 /* check if there is a free tile for the new citizen which, when worked,
3489 * leads to zero or positive food surplus for the enlarged city */
3490 int max_food_tile
= -1; /* no free tile */
3491 city_tile_iterate(city_map_radius_sq_get(pcity_to
),
3492 city_tile(pcity_to
), ptile
) {
3493 if (city_can_work_tile(pcity_to
, ptile
)
3494 && tile_worked(ptile
) != pcity_to
) {
3495 /* Safest assumption is that city won't be celebrating once an
3496 * additional citizen is added */
3497 max_food_tile
= MAX(max_food_tile
,
3498 city_tile_output(pcity_to
, ptile
, FALSE
, O_FOOD
));
3500 } city_tile_iterate_end
;
3501 if (max_food_tile
>= 0
3502 && pcity_to
->surplus
[O_FOOD
] + max_food_tile
>= game
.info
.food_cost
) {
3508 /* insufficiency food in receiver city; no additional citizens */
3509 if (pplayer_from
== pplayer_to
) {
3510 /* migration between one nation */
3511 notify_player(pplayer_to
, ptile_to
, E_CITY_TRANSFER
, ftc_server
,
3512 /* TRANS: From <city1> to <city2>. */
3513 _("Migrants from %s can't go to %s because there is "
3514 "not enough food available!"),
3515 name_from
, name_to
);
3517 /* migration between different nations */
3518 notify_player(pplayer_from
, ptile_to
, E_CITY_TRANSFER
, ftc_server
,
3519 /* TRANS: From <city1> to <city2> (<city2 nation adjective>). */
3520 _("Migrants from %s can't go to %s (%s) because there "
3521 "is not enough food available!"),
3522 name_from
, name_to
, nation_to
);
3523 notify_player(pplayer_to
, ptile_to
, E_CITY_TRANSFER
, ftc_server
,
3524 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
3525 _("Migrants from %s (%s) can't go to %s because there "
3526 "is not enough food available!"),
3527 name_from
, nation_from
, name_to
);
3534 if (!city_can_grow_to(pcity_to
, city_size_get(pcity_to
) + 1)) {
3535 /* receiver city can't grow */
3536 if (pplayer_from
== pplayer_to
) {
3537 /* migration between one nation */
3538 notify_player(pplayer_to
, ptile_to
, E_CITY_TRANSFER
, ftc_server
,
3539 /* TRANS: From <city1> to <city2>. */
3540 _("Migrants from %s can't go to %s because it needs "
3541 "an improvement to grow!"),
3542 name_from
, name_to
);
3544 /* migration between different nations */
3545 notify_player(pplayer_from
, ptile_to
, E_CITY_TRANSFER
, ftc_server
,
3546 /* TRANS: From <city1> to <city2> of <city2 nation adjective>. */
3547 _("Migrants from %s can't go to %s (%s) because it "
3548 "needs an improvement to grow!"),
3549 name_from
, name_to
, nation_to
);
3550 notify_player(pplayer_to
, ptile_to
, E_CITY_TRANSFER
, ftc_server
,
3551 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
3552 _("Migrants from %s (%s) can't go to %s because it "
3553 "needs an improvement to grow!"),
3554 name_from
, nation_from
, name_to
);
3560 /* reduce size of giver */
3561 if (city_size_get(pcity_from
) == 1) {
3563 if (game
.info
.citizen_nationality
) {
3564 /* Preserve nationality of city's only citizen */
3565 pplayer_citizen
= player_slot_get_player(citizens_random(pcity_from
));
3568 /* do not destroy wonders */
3569 city_built_iterate(pcity_from
, pimprove
) {
3570 if (is_wonder(pimprove
)) {
3573 } city_built_iterate_end
;
3575 /* find closest city other of the same player than pcity_from */
3576 rcity
= find_closest_city(ptile_from
, pcity_from
, pplayer_from
, FALSE
,
3577 FALSE
, FALSE
, TRUE
, FALSE
, NULL
);
3580 /* transfer all units to the closest city */
3581 transfer_city_units(pplayer_from
, pplayer_from
,
3582 pcity_from
->units_supported
, rcity
, pcity_from
,
3584 sz_strlcpy(name_from
, city_tile_link(pcity_from
));
3586 script_server_signal_emit("city_size_change", 3,
3587 API_TYPE_CITY
, pcity_from
,
3589 API_TYPE_STRING
, "migration_from");
3590 script_server_signal_emit("city_destroyed", 3,
3591 API_TYPE_CITY
, pcity_from
,
3592 API_TYPE_PLAYER
, pcity_from
->owner
,
3593 API_TYPE_PLAYER
, NULL
);
3595 remove_city(pcity_from
);
3597 notify_player(pplayer_from
, ptile_from
, E_CITY_LOST
, ftc_server
,
3598 _("%s was disbanded by its citizens."),
3601 /* it's the only city of the nation */
3605 /* the migrants take half of the food box with them (this prevents
3606 * migration -> grow -> migration -> ... cycles) */
3607 pcity_from
->food_stock
/= 2;
3609 if (game
.info
.citizen_nationality
) {
3610 /* Those citizens that are from the target nation are most
3611 * ones migrating. */
3612 if (citizens_nation_get(pcity_from
, pplayer_to
->slot
) > 0) {
3613 pplayer_citizen
= pplayer_to
;
3614 } else if (!citizens_nation_get(pcity_from
, pplayer_citizen
->slot
)) {
3615 /* No native citizens at all in the city, choose random foreigner */
3616 struct player_slot
*cit_slot
= citizens_random(pcity_from
);
3618 pplayer_citizen
= player_slot_get_player(cit_slot
);
3620 /* This should be followed by city_reduce_size(). */
3621 citizens_nation_add(pcity_from
, pplayer_citizen
->slot
, -1);
3623 city_reduce_size(pcity_from
, 1, pplayer_from
, "migration_from");
3624 city_refresh_vision(pcity_from
);
3625 if (city_refresh(pcity_from
)) {
3626 auto_arrange_workers(pcity_from
);
3630 /* This should be _before_ the size of the city is increased. Thus, the
3631 * order of the messages is correct (1: migration; 2: increased size). */
3632 if (pplayer_from
== pplayer_to
) {
3633 /* migration between one nation */
3634 notify_player(pplayer_from
, ptile_to
, E_CITY_TRANSFER
, ftc_server
,
3635 /* TRANS: From <city1> to <city2>. */
3636 _("Migrants from %s moved to %s in search of a better "
3637 "life."), name_from
, name_to
);
3639 /* migration between different nations */
3640 notify_player(pplayer_from
, ptile_to
, E_CITY_TRANSFER
, ftc_server
,
3641 /* TRANS: From <city1> to <city2> (<city2 nation adjective>). */
3642 _("Migrants from %s moved to %s (%s) in search of a "
3644 name_from
, name_to
, nation_to
);
3645 notify_player(pplayer_to
, ptile_to
, E_CITY_TRANSFER
, ftc_server
,
3646 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
3647 _("Migrants from %s (%s) moved to %s in search of a "
3649 name_from
, nation_from
, name_to
);
3652 /* raise size of receiver city */
3653 city_increase_size(pcity_to
, pplayer_citizen
);
3654 city_refresh_vision(pcity_to
);
3655 if (city_refresh(pcity_to
)) {
3656 auto_arrange_workers(pcity_to
);
3658 script_server_signal_emit("city_size_change", 3,
3659 API_TYPE_CITY
, pcity_to
,
3661 API_TYPE_STRING
, "migration_to");
3663 log_debug("[M] T%d migration successful (%s -> %s)",
3664 game
.info
.turn
, name_from
, name_to
);
3669 /**************************************************************************
3670 Check for citizens who want to migrate between the cities that overlap.
3671 Migrants go to the city with higher score, if the growth of the target
3672 city is not blocked due to a missing improvement.
3674 The following setting are used:
3676 'game.server.mgr_turninterval' controls the number of turns between
3677 migration checks for one city (counted from the founding). If this
3678 setting is zero, or it is the first turn (T1), migration does no occur.
3680 'game.server.mgr_distance' is the maximal distance for migration.
3682 'game.server.mgr_nationchance' gives the chance for migration within one
3685 'game.server.mgr_worldchance' gives the chance for migration between all
3688 Returns TRUE iff there has been INTERNATIONAL migration.
3689 **************************************************************************/
3690 bool check_city_migrations(void)
3692 bool internat
= FALSE
;
3694 if (!game
.server
.migration
) {
3698 if (game
.server
.mgr_turninterval
<= 0
3699 || (game
.server
.mgr_worldchance
<= 0
3700 && game
.server
.mgr_nationchance
<= 0)) {
3704 /* check for migration */
3705 players_iterate(pplayer
) {
3706 if (!pplayer
->cities
) {
3710 if (check_city_migrations_player(pplayer
)) {
3713 } players_iterate_end
;
3718 /**************************************************************************
3719 Returns TRUE iff the city's food stock was emptied. Should empty the
3720 food stock unless it already is empty.
3721 **************************************************************************/
3722 bool city_empty_food_stock(struct city
*pcity
) {
3723 struct player
*pplayer
= city_owner(pcity
);
3724 struct tile
*ptile
= city_tile(pcity
);
3726 fc_assert_ret_val(pcity
!= NULL
, FALSE
);
3728 if (pcity
->food_stock
> 0) {
3729 pcity
->food_stock
= 0;
3731 notify_player(pplayer
, ptile
, E_DISASTER
, ftc_server
,
3732 /* TRANS: %s is a city name */
3733 _("All stored food destroyed in %s."), city_link(pcity
));
3741 /**************************************************************************
3742 Disaster has hit a city. Apply its effects.
3743 **************************************************************************/
3744 static void apply_disaster(struct city
*pcity
, struct disaster_type
*pdis
)
3746 struct player
*pplayer
= city_owner(pcity
);
3747 struct tile
*ptile
= city_tile(pcity
);
3748 bool had_internal_effect
= FALSE
;
3750 log_debug("%s at %s", disaster_rule_name(pdis
), city_name_get(pcity
));
3752 notify_player(pplayer
, ptile
, E_DISASTER
,
3754 /* TRANS: Disasters such as Earthquake */
3755 _("%s was hit by %s."), city_name_get(pcity
),
3756 disaster_name_translation(pdis
));
3758 if (disaster_has_effect(pdis
, DE_POLLUTION
)) {
3759 if (place_pollution(pcity
, EC_POLLUTION
)) {
3760 notify_player(pplayer
, ptile
, E_DISASTER
, ftc_server
,
3761 _("Pollution near %s."), city_link(pcity
));
3762 had_internal_effect
= TRUE
;
3766 if (disaster_has_effect(pdis
, DE_FALLOUT
)) {
3767 if (place_pollution(pcity
, EC_FALLOUT
)) {
3768 notify_player(pplayer
, ptile
, E_DISASTER
, ftc_server
,
3769 _("Fallout near %s."), city_link(pcity
));
3770 had_internal_effect
= TRUE
;
3774 if (disaster_has_effect(pdis
, DE_REDUCE_DESTROY
)
3775 || (disaster_has_effect(pdis
, DE_REDUCE_POP
)
3776 && pcity
->size
> 1)) {
3777 if (!city_reduce_size(pcity
, 1, NULL
, "disaster")) {
3778 notify_player(pplayer
, ptile
, E_DISASTER
, ftc_server
,
3779 /* TRANS: "Industrial Accident destroys Bogota entirely." */
3780 _("%s destroys %s entirely."),
3781 disaster_name_translation(pdis
), city_link(pcity
));
3784 notify_player(pplayer
, ptile
, E_DISASTER
, ftc_server
,
3785 /* TRANS: "Nuclear Accident ... Montreal." */
3786 _("%s causes population loss in %s."),
3787 disaster_name_translation(pdis
), city_link(pcity
));
3790 had_internal_effect
= TRUE
;
3793 if (pcity
&& disaster_has_effect(pdis
, DE_DESTROY_BUILDING
)) {
3795 struct impr_type
*imprs
[B_LAST
];
3797 city_built_iterate(pcity
, pimprove
) {
3798 if (is_improvement(pimprove
)
3799 && !improvement_has_flag(pimprove
, IF_DISASTER_PROOF
)) {
3800 imprs
[total
++] = pimprove
;
3802 } city_built_iterate_end
;
3805 int num
= fc_rand(total
);
3807 building_lost(pcity
, imprs
[num
]);
3809 notify_player(pplayer
, ptile
, E_DISASTER
, ftc_server
,
3810 /* TRANS: second %s is the name of a city improvement */
3811 _("%s destroys %s in %s."),
3812 disaster_name_translation(pdis
),
3813 improvement_name_translation(imprs
[num
]),
3816 had_internal_effect
= TRUE
;
3820 if (pcity
&& disaster_has_effect(pdis
, DE_EMPTY_FOODSTOCK
)) {
3821 if (city_empty_food_stock(pcity
)) {
3822 had_internal_effect
= TRUE
;
3826 if (pcity
&& disaster_has_effect(pdis
, DE_EMPTY_PRODSTOCK
)) {
3827 if (pcity
->shield_stock
> 0) {
3830 pcity
->shield_stock
= 0;
3831 nullify_prechange_production(pcity
); /* Make it impossible to recover */
3833 universal_name_translation(&pcity
->production
, prod
, sizeof(prod
));
3834 notify_player(pplayer
, ptile
, E_DISASTER
, ftc_server
,
3835 /* TRANS: "Production of Colossus in Rhodes destroyed." */
3836 _("Production of %s in %s destroyed."),
3837 prod
, city_link(pcity
));
3839 had_internal_effect
= TRUE
;
3843 script_server_signal_emit("disaster_occurred", 3,
3844 API_TYPE_DISASTER
, pdis
,
3845 API_TYPE_CITY
, pcity
,
3846 API_TYPE_BOOL
, had_internal_effect
);
3847 script_server_signal_emit("disaster", 2,
3848 API_TYPE_DISASTER
, pdis
,
3849 API_TYPE_CITY
, pcity
);
3852 /**************************************************************************
3853 Check for any disasters hitting any city, and apply those disasters.
3854 **************************************************************************/
3855 void check_disasters(void)
3857 if (game
.info
.disasters
== 0) {
3858 /* Shortcut out as no disaster is possible. */
3862 players_iterate(pplayer
) {
3863 /* Safe city iterator needed as disaster may destroy city */
3864 city_list_iterate_safe(pplayer
->cities
, pcity
) {
3867 disaster_type_iterate(pdis
) {
3868 if (city_exist(id
)) {
3869 /* City survived earlier disasters. */
3870 int probability
= game
.info
.disasters
* pdis
->frequency
;
3871 int result
= fc_rand(DISASTER_BASE_RARITY
);
3873 if (result
< probability
) {
3874 if (can_disaster_happen(pdis
, pcity
)) {
3875 apply_disaster(pcity
, pdis
);
3879 } disaster_type_iterate_end
;
3880 } city_list_iterate_safe_end
;
3881 } players_iterate_end
;
3884 /**************************************************************************
3885 Check for migration for each city of one player.
3887 For each city of the player do:
3888 * check each tile within GAME_MAX_MGR_DISTANCE for a city
3889 * if a city is found check the distance
3890 * compare the migration score
3891 **************************************************************************/
3892 static bool check_city_migrations_player(const struct player
*pplayer
)
3894 char city_link_text
[MAX_LEN_LINK
];
3895 float best_city_player_score
, best_city_world_score
;
3896 struct city
*best_city_player
, *best_city_world
, *acity
;
3897 float score_from
, score_tmp
, weight
;
3899 bool internat
= FALSE
;
3901 /* check for each city
3902 * city_list_iterate_safe_end must be used because we could
3903 * remove one city from the list */
3904 city_list_iterate_safe(pplayer
->cities
, pcity
) {
3905 /* no migration out of the capital */
3906 if (is_capital(pcity
)) {
3910 /* check only each (game.server.mgr_turninterval) turn
3911 * (counted from the funding turn) and do not migrate
3912 * the same turn a city is founded */
3913 if (game
.info
.turn
== pcity
->turn_founded
3914 || ((game
.info
.turn
- pcity
->turn_founded
)
3915 % game
.server
.mgr_turninterval
) != 0) {
3919 best_city_player_score
= 0.0;
3920 best_city_world_score
= 0.0;
3921 best_city_player
= NULL
;
3922 best_city_world
= NULL
;
3924 /* score of the actual city
3925 * taking into account a persistence factor of 3 */
3926 score_from
= city_migration_score(pcity
) * 3;
3928 log_debug("[M] T%d check city: %s score: %6.3f (%s)",
3929 game
.info
.turn
, city_name_get(pcity
), score_from
,
3930 player_name(pplayer
));
3932 /* consider all cities within the maximal possible distance
3933 * (= CITY_MAP_MAX_RADIUS + GAME_MAX_MGR_DISTANCE) */
3934 iterate_outward(city_tile(pcity
), CITY_MAP_MAX_RADIUS
3935 + GAME_MAX_MGR_DISTANCE
, ptile
) {
3936 acity
= tile_city(ptile
);
3938 if (!acity
|| acity
== pcity
) {
3939 /* no city or the city in the center */
3943 /* Calculate the migration distance. The value of
3944 * game.server.mgr_distance is added to the current city radius. If the
3945 * distance between both cities is lower or equal than this value,
3946 * migration is possible. */
3947 mgr_dist
= (int)sqrt((double)MAX(city_map_radius_sq_get(acity
),0))
3948 + game
.server
.mgr_distance
;
3950 /* distance between the two cities */
3951 dist
= real_map_distance(city_tile(pcity
), city_tile(acity
));
3953 if (dist
> mgr_dist
) {
3958 /* score of the second city, weighted by the distance */
3959 weight
= ((float) (mgr_dist
+ 1 - dist
) / (float) (mgr_dist
+ 1));
3960 score_tmp
= city_migration_score(acity
) * weight
;
3962 log_debug("[M] T%d - compare city: %s (%s) dist: %d mgr_dist: %d "
3963 "score: %6.3f", game
.info
.turn
, city_name_get(acity
),
3964 player_name(city_owner(acity
)), dist
, mgr_dist
, score_tmp
);
3966 if (game
.server
.mgr_nationchance
> 0 && city_owner(acity
) == pplayer
) {
3967 /* migration between cities of the same owner */
3968 if (score_tmp
> score_from
&& score_tmp
> best_city_player_score
) {
3969 /* select the best! */
3970 best_city_player_score
= score_tmp
;
3971 best_city_player
= acity
;
3973 log_debug("[M] T%d - best city (player): %s (%s) score: "
3974 "%6.3f (> %6.3f)", game
.info
.turn
,
3975 city_name_get(best_city_player
), player_name(pplayer
),
3976 best_city_player_score
, score_from
);
3978 } else if (game
.server
.mgr_worldchance
> 0
3979 && city_owner(acity
) != pplayer
) {
3980 /* migration between cities of different owners */
3981 if (game
.info
.citizen_nationality
) {
3982 /* Modify the score if citizens could migrate to a city of their
3983 * original nation. */
3984 if (citizens_nation_get(pcity
, city_owner(acity
)->slot
) > 0) {
3989 if (score_tmp
> score_from
&& score_tmp
> best_city_world_score
) {
3990 /* select the best! */
3991 best_city_world_score
= score_tmp
;
3992 best_city_world
= acity
;
3994 log_debug("[M] T%d - best city (world): %s (%s) score: "
3995 "%6.3f (> %6.3f)", game
.info
.turn
,
3996 city_name_get(best_city_world
),
3997 player_name(city_owner(best_city_world
)),
3998 best_city_world_score
, score_from
);
4001 } iterate_outward_end
;
4003 if (best_city_player_score
> 0) {
4004 /* first, do the migration within one nation */
4005 if (fc_rand(100) >= game
.server
.mgr_nationchance
) {
4007 /* N.B.: city_link always returns the same pointer. */
4008 sz_strlcpy(city_link_text
, city_link(pcity
));
4009 notify_player(pplayer
, city_tile(pcity
), E_CITY_TRANSFER
, ftc_server
,
4010 _("Citizens of %s are thinking about migrating to %s "
4011 "for a better life."),
4012 city_link_text
, city_link(best_city_player
));
4014 do_city_migration(pcity
, best_city_player
);
4021 if (best_city_world_score
> 0) {
4022 /* second, do the migration between all nations */
4023 if (fc_rand(100) >= game
.server
.mgr_worldchance
) {
4026 nname
= nation_adjective_for_player(city_owner(best_city_world
));
4028 /* N.B.: city_link always returns the same pointer. */
4029 sz_strlcpy(city_link_text
, city_link(pcity
));
4030 notify_player(pplayer
, city_tile(pcity
), E_CITY_TRANSFER
, ftc_server
,
4031 /* TRANS: <city1> to <city2> (<city2 nation adjective>). */
4032 _("Citizens of %s are thinking about migrating to %s "
4033 "(%s) for a better life."),
4034 city_link_text
, city_link(best_city_world
), nname
);
4036 do_city_migration(pcity
, best_city_world
);
4043 } city_list_iterate_safe_end
;
4048 /**************************************************************************
4049 Recheck and store style of the city.
4050 **************************************************************************/
4051 void city_style_refresh(struct city
*pcity
)
4053 pcity
->style
= city_style(pcity
);