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"
44 #include "government.h"
49 #include "specialist.h"
51 #include "traderoutes.h"
55 /* common/scriptcore */
56 #include "luascript_types.h"
59 #include "citizenshand.h"
60 #include "citytools.h"
65 #include "sanitycheck.h"
66 #include "spacerace.h"
69 #include "techtools.h"
70 #include "unittools.h"
74 #include "advbuilding.h"
76 #include "autosettlers.h"
78 /* server/scripting */
79 #include "script_server.h"
81 /* Queue for pending city_refresh() */
82 static struct city_list
*city_refresh_queue
= NULL
;
85 static void check_pollution(struct city
*pcity
);
86 static void city_populate(struct city
*pcity
, struct player
*nationality
);
88 static bool worklist_change_build_target(struct player
*pplayer
,
91 static bool city_distribute_surplus_shields(struct player
*pplayer
,
93 static bool city_build_building(struct player
*pplayer
, struct city
*pcity
);
94 static bool city_build_unit(struct player
*pplayer
, struct city
*pcity
);
95 static bool city_build_stuff(struct player
*pplayer
, struct city
*pcity
);
96 static struct impr_type
*building_upgrades_to(struct city
*pcity
,
97 struct impr_type
*pimprove
);
98 static void upgrade_building_prod(struct city
*pcity
);
99 static struct unit_type
*unit_upgrades_to(struct city
*pcity
,
100 struct unit_type
*id
);
101 static void upgrade_unit_prod(struct city
*pcity
);
103 /* Helper struct for associating a building to a city. */
106 struct impr_type
*pimprove
;
109 #define SPECLIST_TAG cityimpr
110 #define SPECLIST_TYPE struct cityimpr
111 #include "speclist.h"
113 #define cityimpr_list_iterate(cityimprlist, pcityimpr) \
114 TYPED_LIST_ITERATE(struct cityimpr, cityimprlist, pcityimpr)
115 #define cityimpr_list_iterate_end LIST_ITERATE_END
117 static bool sell_random_building(struct player
*pplayer
,
118 struct cityimpr_list
*imprs
);
119 static struct unit
*sell_random_unit(struct player
*pplayer
,
120 struct unit_list
*punitlist
);
122 static citizens
city_reduce_specialists(struct city
*pcity
, citizens change
);
123 static citizens
city_reduce_workers(struct city
*pcity
, citizens change
);
125 static bool city_balance_treasury_buildings(struct city
*pcity
);
126 static bool city_balance_treasury_units(struct city
*pcity
);
127 static bool player_balance_treasury_units_and_buildings
128 (struct player
*pplayer
);
129 static bool player_balance_treasury_units(struct player
*pplayer
);
131 static bool disband_city(struct city
*pcity
);
133 static void define_orig_production_values(struct city
*pcity
);
134 static void update_city_activity(struct city
*pcity
);
135 static void nullify_caravan_and_disband_plus(struct city
*pcity
);
136 static bool city_illness_check(const struct city
* pcity
);
138 static float city_migration_score(struct city
*pcity
);
139 static bool do_city_migration(struct city
*pcity_from
,
140 struct city
*pcity_to
);
141 static bool check_city_migrations_player(const struct player
*pplayer
);
143 /**************************************************************************
144 Updates unit upkeeps and city internal cached data. Returns whether
145 city radius has changed.
146 **************************************************************************/
147 bool city_refresh(struct city
*pcity
)
151 pcity
->server
.needs_refresh
= FALSE
;
153 retval
= city_map_update_radius_sq(pcity
);
154 city_units_upkeep(pcity
); /* update unit upkeep */
155 city_refresh_from_main_map(pcity
, NULL
);
156 city_style_refresh(pcity
);
159 /* Force a sync of the city after the change. */
160 send_city_info(city_owner(pcity
), pcity
);
166 /**************************************************************************
167 Called on government change or wonder completion or stuff like that
169 **************************************************************************/
170 void city_refresh_for_player(struct player
*pplayer
)
172 conn_list_do_buffer(pplayer
->connections
);
173 city_list_iterate(pplayer
->cities
, pcity
) {
174 if (city_refresh(pcity
)) {
175 auto_arrange_workers(pcity
);
177 send_city_info(pplayer
, pcity
);
178 } city_list_iterate_end
;
179 conn_list_do_unbuffer(pplayer
->connections
);
182 /****************************************************************************
183 Queue pending city_refresh() for later.
184 ****************************************************************************/
185 void city_refresh_queue_add(struct city
*pcity
)
187 if (NULL
== city_refresh_queue
) {
188 city_refresh_queue
= city_list_new();
189 } else if (city_list_find_number(city_refresh_queue
, pcity
->id
)) {
193 city_list_prepend(city_refresh_queue
, pcity
);
194 pcity
->server
.needs_refresh
= TRUE
;
197 /*************************************************************************
198 Refresh the listed cities.
199 Called after significant changes to borders, and arranging workers.
200 *************************************************************************/
201 void city_refresh_queue_processing(void)
203 if (NULL
== city_refresh_queue
) {
207 city_list_iterate(city_refresh_queue
, pcity
) {
208 if (pcity
->server
.needs_refresh
) {
209 if (city_refresh(pcity
)) {
210 auto_arrange_workers(pcity
);
212 send_city_info(city_owner(pcity
), pcity
);
214 } city_list_iterate_end
;
216 city_list_destroy(city_refresh_queue
);
217 city_refresh_queue
= NULL
;
220 /**************************************************************************
221 Automatically sells obsolete buildings from city.
222 **************************************************************************/
223 void remove_obsolete_buildings_city(struct city
*pcity
, bool refresh
)
225 struct player
*pplayer
= city_owner(pcity
);
228 city_built_iterate(pcity
, pimprove
) {
229 if (improvement_obsolete(pplayer
, pimprove
, pcity
)
230 && can_city_sell_building(pcity
, pimprove
)) {
233 do_sell_building(pplayer
, pcity
, pimprove
);
234 sgold
= impr_sell_gold(pimprove
);
235 notify_player(pplayer
, city_tile(pcity
), E_IMP_SOLD
, ftc_server
,
236 PL_("%s is selling %s (obsolete) for %d.",
237 "%s is selling %s (obsolete) for %d.",
240 improvement_name_translation(pimprove
),
244 } city_built_iterate_end
;
246 if (sold
&& refresh
) {
247 if (city_refresh(pcity
)) {
248 auto_arrange_workers(pcity
);
250 send_city_info(pplayer
, pcity
);
251 send_player_info_c(pplayer
, NULL
); /* Send updated gold to all */
255 /**************************************************************************
256 Sell obsolete buildings from all cities of the player
257 **************************************************************************/
258 void remove_obsolete_buildings(struct player
*pplayer
)
260 city_list_iterate(pplayer
->cities
, pcity
) {
261 remove_obsolete_buildings_city(pcity
, FALSE
);
262 } city_list_iterate_end
;
265 /**************************************************************************
266 Rearrange workers according to a cm_result struct. The caller must make
267 sure that the result is valid.
268 **************************************************************************/
269 void apply_cmresult_to_city(struct city
*pcity
,
270 const struct cm_result
*cmr
)
272 struct tile
*pcenter
= city_tile(pcity
);
274 /* Now apply results */
275 city_tile_iterate_skip_free_worked(city_map_radius_sq_get(pcity
), pcenter
,
277 struct city
*pwork
= tile_worked(ptile
);
279 if (cmr
->worker_positions
[idx
]) {
281 city_map_update_worker(pcity
, ptile
);
283 fc_assert(pwork
== pcity
);
286 if (pwork
== pcity
) {
287 city_map_update_empty(pcity
, ptile
);
290 } city_tile_iterate_skip_free_worked_end
;
292 specialist_type_iterate(sp
) {
293 pcity
->specialists
[sp
] = cmr
->specialists
[sp
];
294 } specialist_type_iterate_end
;
297 /**************************************************************************
298 Call sync_cities() to send the affected cities to the clients.
299 **************************************************************************/
300 void auto_arrange_workers(struct city
*pcity
)
302 struct cm_parameter cmp
;
303 struct cm_result
*cmr
;
305 /* See comment in freeze_workers(): we can't rearrange while
306 * workers are frozen (i.e. multiple updates need to be done). */
307 if (pcity
->server
.workers_frozen
> 0) {
308 pcity
->server
.needs_arrange
= TRUE
;
311 TIMING_LOG(AIT_CITIZEN_ARRANGE
, TIMER_START
);
313 /* Freeze the workers and make sure all the tiles around the city
314 * are up to date. Then thaw, but hackishly make sure that thaw
315 * doesn't call us recursively, which would waste time. */
316 city_freeze_workers(pcity
);
317 pcity
->server
.needs_arrange
= FALSE
;
319 city_map_update_all(pcity
);
321 pcity
->server
.needs_arrange
= FALSE
;
322 city_thaw_workers(pcity
);
324 /* Now start actually rearranging. */
327 sanity_check_city(pcity
);
328 cm_clear_cache(pcity
);
330 cm_init_parameter(&cmp
);
331 cmp
.require_happy
= FALSE
;
332 cmp
.allow_disorder
= FALSE
;
333 cmp
.allow_specialists
= TRUE
;
335 /* We used to look at pplayer->ai.xxx_priority to determine the values
336 * to be used here. However that doesn't work at all because those values
337 * are on a different scale. Later the ai may wish to adjust its
338 * priorities - this should be done via a separate set of variables. */
339 if (city_size_get(pcity
) > 1) {
340 if (city_size_get(pcity
) <= game
.info
.notradesize
) {
341 cmp
.factor
[O_FOOD
] = 15;
343 if (city_granary_size(city_size_get(pcity
)) == pcity
->food_stock
) {
344 /* We don't need more food if the granary is full. */
345 cmp
.factor
[O_FOOD
] = 0;
347 cmp
.factor
[O_FOOD
] = 10;
351 /* Growing to size 2 is the highest priority. */
352 cmp
.factor
[O_FOOD
] = 20;
354 cmp
.factor
[O_SHIELD
] = 5;
355 cmp
.factor
[O_TRADE
] = 0; /* Trade only provides gold/science. */
356 cmp
.factor
[O_GOLD
] = 2;
357 cmp
.factor
[O_LUXURY
] = 0; /* Luxury only influences happiness. */
358 cmp
.factor
[O_SCIENCE
] = 2;
359 cmp
.happy_factor
= 0;
361 if (city_granary_size(city_size_get(pcity
)) == pcity
->food_stock
) {
362 cmp
.minimal_surplus
[O_FOOD
] = 0;
364 cmp
.minimal_surplus
[O_FOOD
] = 1;
366 cmp
.minimal_surplus
[O_SHIELD
] = 1;
367 cmp
.minimal_surplus
[O_TRADE
] = 0;
368 cmp
.minimal_surplus
[O_GOLD
] = -FC_INFINITY
;
369 cmp
.minimal_surplus
[O_LUXURY
] = 0;
370 cmp
.minimal_surplus
[O_SCIENCE
] = 0;
372 /* This must be after city_refresh() so that the result gets created for the right
374 cmr
= cm_result_new(pcity
);
375 cm_query_result(pcity
, &cmp
, cmr
);
377 if (!cmr
->found_a_valid
) {
378 /* Drop surpluses and try again. */
379 cmp
.minimal_surplus
[O_FOOD
] = 0;
380 cmp
.minimal_surplus
[O_SHIELD
] = 0;
381 cmp
.minimal_surplus
[O_GOLD
] = -FC_INFINITY
;
382 cm_query_result(pcity
, &cmp
, cmr
);
384 if (!cmr
->found_a_valid
) {
385 /* Emergency management. Get _some_ result. This doesn't use
386 * cm_init_emergency_parameter so we can keep the factors from
388 output_type_iterate(o
) {
389 cmp
.minimal_surplus
[o
] = MIN(cmp
.minimal_surplus
[o
],
390 MIN(pcity
->surplus
[o
], 0));
391 } output_type_iterate_end
;
392 cmp
.require_happy
= FALSE
;
393 cmp
.allow_disorder
= city_owner(pcity
)->ai_controlled
? FALSE
: TRUE
;
394 cm_query_result(pcity
, &cmp
, cmr
);
396 if (!cmr
->found_a_valid
) {
397 /* Should never happen. */
398 CITY_LOG(LOG_DEBUG
, pcity
, "emergency management");
399 cm_init_emergency_parameter(&cmp
);
400 cm_query_result(pcity
, &cmp
, cmr
);
402 fc_assert_ret(cmr
->found_a_valid
);
404 apply_cmresult_to_city(pcity
, cmr
);
406 if (pcity
->server
.debug
) {
407 /* Print debug output if requested. */
408 cm_print_city(pcity
);
409 cm_print_result(cmr
);
412 if (city_refresh(pcity
)) {
413 log_error("%s radius changed when already arranged workers.",
414 city_name_get(pcity
));
415 /* Can't do anything - don't want to enter infinite recursive loop
416 * by trying to arrange workers more. */
418 sanity_check_city(pcity
);
420 cm_result_destroy(cmr
);
421 TIMING_LOG(AIT_CITIZEN_ARRANGE
, TIMER_STOP
);
424 /****************************************************************************
425 Notices about cities that should be sent to all players.
426 ****************************************************************************/
427 static void city_global_turn_notify(struct conn_list
*dest
)
429 cities_iterate(pcity
) {
430 struct impr_type
*pimprove
= pcity
->production
.value
.building
;
432 if (VUT_IMPROVEMENT
== pcity
->production
.kind
433 && is_great_wonder(pimprove
)
434 && great_wonder_is_available(pimprove
)
435 && (1 >= city_production_turns_to_build(pcity
, TRUE
))) {
436 notify_conn(dest
, city_tile(pcity
),
437 E_WONDER_WILL_BE_BUILT
, ftc_server
,
438 _("Notice: Wonder %s in %s will be finished next turn."),
439 improvement_name_translation(pimprove
), city_link(pcity
));
441 } cities_iterate_end
;
444 /****************************************************************************
445 Send turn notifications for specified city to specified connections.
446 If 'pplayer' is not NULL, the message will be cached for this player.
447 ****************************************************************************/
448 static void city_turn_notify(const struct city
*pcity
,
449 struct conn_list
*dest
,
450 const struct player
*cache_for_player
)
452 struct impr_type
*pimprove
= pcity
->production
.value
.building
;
453 struct packet_chat_msg packet
;
454 int turns_growth
, turns_granary
;
456 if (0 < pcity
->surplus
[O_FOOD
]) {
457 turns_growth
= (city_granary_size(city_size_get(pcity
))
458 - pcity
->food_stock
- 1) / pcity
->surplus
[O_FOOD
];
460 if (0 == get_city_bonus(pcity
, EFT_GROWTH_FOOD
)
461 && 0 < get_current_construction_bonus(pcity
, EFT_GROWTH_FOOD
,
463 && 0 < pcity
->surplus
[O_SHIELD
]) {
464 /* From the check above, the surplus must always be positive. */
465 turns_granary
= (impr_build_shield_cost(pimprove
)
466 - pcity
->shield_stock
) / pcity
->surplus
[O_SHIELD
];
467 /* If growth and granary completion occur simultaneously, granary
468 * preserves food. -AJS. */
469 if (5 > turns_growth
&& 5 > turns_granary
470 && turns_growth
< turns_granary
) {
471 package_event(&packet
, city_tile(pcity
),
472 E_CITY_GRAN_THROTTLE
, ftc_server
,
473 _("Suggest throttling growth in %s to use %s "
474 "(being built) more effectively."),
476 improvement_name_translation(pimprove
));
477 lsend_packet_chat_msg(dest
, &packet
);
478 if (NULL
!= cache_for_player
) {
479 event_cache_add_for_player(&packet
, cache_for_player
);
484 if (0 >= turns_growth
&& !city_celebrating(pcity
)
485 && city_can_grow_to(pcity
, city_size_get(pcity
) + 1)) {
486 package_event(&packet
, city_tile(pcity
),
487 E_CITY_MAY_SOON_GROW
, ftc_server
,
488 _("%s may soon grow to size %i."),
489 city_link(pcity
), city_size_get(pcity
) + 1);
490 lsend_packet_chat_msg(dest
, &packet
);
491 if (NULL
!= cache_for_player
) {
492 event_cache_add_for_player(&packet
, cache_for_player
);
496 if (0 >= pcity
->food_stock
+ pcity
->surplus
[O_FOOD
]
497 && 0 > pcity
->surplus
[O_FOOD
]) {
498 package_event(&packet
, city_tile(pcity
),
499 E_CITY_FAMINE_FEARED
, ftc_server
,
500 _("Warning: Famine feared in %s."), city_link(pcity
));
501 lsend_packet_chat_msg(dest
, &packet
);
502 if (NULL
!= cache_for_player
) {
503 event_cache_add_for_player(&packet
, cache_for_player
);
509 /****************************************************************************
510 Send global and player specific city turn notifications. If 'pconn' is
511 NULL, it will send to all connections and cache the events.
512 ****************************************************************************/
513 void send_city_turn_notifications(struct connection
*pconn
)
516 struct player
*pplayer
= conn_get_player(pconn
);
518 if (NULL
!= pplayer
) {
519 city_list_iterate(pplayer
->cities
, pcity
) {
520 city_turn_notify(pcity
, pconn
->self
, NULL
);
521 } city_list_iterate_end
;
523 city_global_turn_notify(pconn
->self
);
525 players_iterate(pplayer
) {
526 city_list_iterate(pplayer
->cities
, pcity
) {
527 city_turn_notify(pcity
, pplayer
->connections
, pplayer
);
528 } city_list_iterate_end
;
529 } players_iterate_end
;
530 /* NB: notifications to 'game.est_connections' are automatically
532 city_global_turn_notify(game
.est_connections
);
536 /**************************************************************************
537 Update all cities of one nation (costs for buildings, unit upkeep, ...).
538 **************************************************************************/
539 void update_city_activities(struct player
*pplayer
)
541 char buf
[4 * MAX_LEN_NAME
];
544 fc_assert(NULL
!= pplayer
);
545 fc_assert(NULL
!= pplayer
->cities
);
547 n
= city_list_size(pplayer
->cities
);
548 gold
= pplayer
->economic
.gold
;
549 pplayer
->server
.bulbs_last_turn
= 0;
552 struct city
*cities
[n
];
555 city_list_iterate(pplayer
->cities
, pcity
) {
558 citizens_convert(pcity
);
560 /* Cancel traderoutes that cannot exist any more */
561 for (ci
= 0; ci
< MAX_TRADE_ROUTES
; ci
++) {
562 struct city
*tcity
= game_city_by_number(pcity
->trade
[ci
]);
564 if (tcity
!= NULL
&& !can_cities_trade(pcity
, tcity
)) {
565 enum trade_route_type type
= cities_trade_route_type(pcity
, tcity
);
566 struct trade_route_settings
*settings
= trade_route_settings_by_type(type
);
568 if (settings
->cancelling
== TRI_CANCEL
) {
569 remove_trade_route(pcity
, tcity
, TRUE
, FALSE
);
574 /* Add cities to array for later random order handling */
576 } city_list_iterate_end
;
578 /* How gold upkeep is handled depends on the setting
579 * 'game.info.gold_upkeep_style':
580 * GOLD_UPKEEP_CITY: Each city tries to balance its upkeep individually
581 * (this is done in update_city_activity()).
582 * GOLD_UPKEEP_MIXED: Each city tries to balance its upkeep for
583 * buildings individually; the upkeep for units is
584 * paid by the nation.
585 * GOLD_UPKEEP_NATION: The nation as a whole balances the treasury. If
586 * the treasury is not balance units and buildings
589 /* Iterate over cities in a random order. */
592 /* update unit upkeep */
593 city_units_upkeep(cities
[r
]);
594 update_city_activity(cities
[r
]);
595 cities
[r
] = cities
[--i
];
598 if (pplayer
->economic
.gold
< 0) {
599 switch (game
.info
.gold_upkeep_style
) {
600 case GOLD_UPKEEP_CITY
:
602 case GOLD_UPKEEP_MIXED
:
603 /* Nation pays for units. */
604 player_balance_treasury_units(pplayer
);
606 case GOLD_UPKEEP_NATION
:
607 /* Nation pays for units and buildings. */
608 player_balance_treasury_units_and_buildings(pplayer
);
613 /* Should not happen. */
614 fc_assert(pplayer
->economic
.gold
>= 0);
617 /* This test includes the cost of the units because
618 * units are paid for in update_city_activity() or
619 * player_balance_treasury_units(). */
620 if (gold
- (gold
- pplayer
->economic
.gold
) * 3 < 0) {
621 notify_player(pplayer
, NULL
, E_LOW_ON_FUNDS
, ftc_server
,
622 _("WARNING, we're LOW on FUNDS %s."),
623 ruler_title_for_player(pplayer
, buf
, sizeof(buf
)));
627 /* Uncomment to unbalance the game, like in civ1 (CLG). */
628 if (pplayer
->got_tech
&& pplayer
->research
->researched
> 0) {
629 pplayer
->research
->researched
= 0;
633 city_refresh_queue_processing();
636 /**************************************************************************
637 Reduce the city specialists by some (positive) value.
638 Return the amount of reduction.
639 **************************************************************************/
640 static citizens
city_reduce_specialists(struct city
*pcity
, citizens change
)
642 citizens want
= change
;
644 fc_assert_ret_val(0 < change
, 0);
646 specialist_type_iterate(sp
) {
647 citizens fix
= MIN(want
, pcity
->specialists
[sp
]);
649 pcity
->specialists
[sp
] -= fix
;
651 } specialist_type_iterate_end
;
653 return change
- want
;
656 /**************************************************************************
657 Reduce the city workers by some (positive) value.
658 Return the amount of reduction.
659 **************************************************************************/
660 static citizens
city_reduce_workers(struct city
*pcity
, citizens change
)
662 struct tile
*pcenter
= city_tile(pcity
);
665 fc_assert_ret_val(0 < change
, 0);
667 city_tile_iterate_skip_free_worked(city_map_radius_sq_get(pcity
), pcenter
,
668 ptile
, _index
, _x
, _y
) {
669 if (0 < want
&& tile_worked(ptile
) == pcity
) {
670 city_map_update_empty(pcity
, ptile
);
673 } city_tile_iterate_skip_free_worked_end
;
675 return change
- want
;
678 /**************************************************************************
679 Reduce the city size. Return TRUE if the city survives the population
681 **************************************************************************/
682 bool city_reduce_size(struct city
*pcity
, citizens pop_loss
,
683 struct player
*destroyer
, const char *reason
)
685 citizens loss_remain
;
692 if (reason
!= NULL
) {
693 script_server_signal_emit("city_size_change", 3,
694 API_TYPE_CITY
, pcity
,
695 API_TYPE_INT
, -pop_loss
,
696 API_TYPE_STRING
, reason
);
699 if (city_size_get(pcity
) <= pop_loss
) {
701 script_server_signal_emit("city_destroyed", 3,
702 API_TYPE_CITY
, pcity
,
703 API_TYPE_PLAYER
, pcity
->owner
,
704 API_TYPE_PLAYER
, destroyer
);
709 old_radius_sq
= tile_border_source_radius_sq(pcity
->tile
);
710 city_size_add(pcity
, -pop_loss
);
711 map_update_border(pcity
->tile
, pcity
->owner
, old_radius_sq
,
712 tile_border_source_radius_sq(pcity
->tile
));
714 /* Cap the food stock at the new granary size. */
715 if (pcity
->food_stock
> city_granary_size(city_size_get(pcity
))) {
716 pcity
->food_stock
= city_granary_size(city_size_get(pcity
));
719 /* First try to kill off the specialists */
720 loss_remain
= pop_loss
- city_reduce_specialists(pcity
, pop_loss
);
722 if (loss_remain
> 0) {
723 /* Take it out on workers */
724 loss_remain
-= city_reduce_workers(pcity
, loss_remain
);
727 /* Update citizens. */
728 citizens_update(pcity
, NULL
);
730 /* Update number of people in each feelings category.
731 * This also updates the city radius if needed. */
734 auto_arrange_workers(pcity
);
736 /* Send city data. */
739 fc_assert_ret_val_msg(0 == loss_remain
, TRUE
,
740 "city_reduce_size() has remaining"
741 "%d of %d for \"%s\"[%d]",
742 loss_remain
, pop_loss
,
743 city_name_get(pcity
), city_size_get(pcity
));
745 /* Update cities that have trade routes with us */
746 trade_routes_iterate(pcity
, pcity2
) {
747 if (city_refresh(pcity2
)) {
748 /* This should never happen, but if it does, make sure not to
749 * leave workers outside city radius. */
750 auto_arrange_workers(pcity2
);
752 } trade_routes_iterate_end
;
754 sanity_check_city(pcity
);
758 /**************************************************************************
759 Repair the city population without affecting city size.
760 Used by savegame.c and sanitycheck.c
761 **************************************************************************/
762 void city_repair_size(struct city
*pcity
, int change
)
765 pcity
->specialists
[DEFAULT_SPECIALIST
] += change
;
766 } else if (change
< 0) {
767 int need
= change
+ city_reduce_specialists(pcity
, -change
);
770 need
+= city_reduce_workers(pcity
, -need
);
773 fc_assert_msg(0 == need
,
774 "city_repair_size() has remaining %d of %d for \"%s\"[%d]",
775 need
, change
, city_name_get(pcity
), city_size_get(pcity
));
779 /**************************************************************************
780 Return the percentage of food that is lost in this city.
782 Normally this value is 0% but this can be increased by EFT_GROWTH_FOOD
784 **************************************************************************/
785 static int granary_savings(const struct city
*pcity
)
787 int savings
= get_city_bonus(pcity
, EFT_GROWTH_FOOD
);
789 return CLIP(0, savings
, 100);
792 /**************************************************************************
793 Reset the foodbox, usually when a city grows or shrinks.
794 By default it is reset to zero, but this can be increased by Growth_Food
796 Usually this should be called before the city changes size.
797 **************************************************************************/
798 static void city_reset_foodbox(struct city
*pcity
, int new_size
)
800 fc_assert_ret(pcity
!= NULL
);
801 pcity
->food_stock
= (city_granary_size(new_size
)
802 * granary_savings(pcity
)) / 100;
805 /**************************************************************************
806 Increase city size by one. We do not refresh borders or send info about
807 the city to the clients as part of this function. There might be several
808 calls to this function at once, and those actions are needed only once.
809 **************************************************************************/
810 static bool city_increase_size(struct city
*pcity
, struct player
*nationality
)
813 int savings_pct
= granary_savings(pcity
);
814 bool have_square
= FALSE
;
815 bool rapture_grow
= city_rapture_grow(pcity
); /* check before size increase! */
816 struct tile
*pcenter
= city_tile(pcity
);
817 struct player
*powner
= city_owner(pcity
);
818 struct impr_type
*pimprove
= pcity
->production
.value
.building
;
819 int saved_id
= pcity
->id
;
821 if (!city_can_grow_to(pcity
, city_size_get(pcity
) + 1)) {
822 /* need improvement */
823 if (get_current_construction_bonus(pcity
, EFT_SIZE_ADJ
, RPT_CERTAIN
) > 0
824 || get_current_construction_bonus(pcity
, EFT_SIZE_UNLIMIT
, RPT_CERTAIN
) > 0) {
825 notify_player(powner
, city_tile(pcity
), E_CITY_AQ_BUILDING
, ftc_server
,
826 _("%s needs %s (being built) to grow beyond size %d."),
828 improvement_name_translation(pimprove
),
829 city_size_get(pcity
));
831 notify_player(powner
, city_tile(pcity
), E_CITY_AQUEDUCT
, ftc_server
,
832 _("%s needs an improvement to grow beyond size %d."),
833 city_link(pcity
), city_size_get(pcity
));
835 /* Granary can only hold so much */
836 new_food
= (city_granary_size(city_size_get(pcity
))
837 * (100 * 100 - game
.server
.aqueductloss
* (100 - savings_pct
))
839 pcity
->food_stock
= MIN(pcity
->food_stock
, new_food
);
843 city_size_add(pcity
, 1);
845 /* Do not empty food stock if city is growing by celebrating */
847 new_food
= city_granary_size(city_size_get(pcity
));
849 new_food
= city_granary_size(city_size_get(pcity
)) * savings_pct
/ 100;
851 pcity
->food_stock
= MIN(pcity
->food_stock
, new_food
);
853 /* If there is enough food, and the city is big enough,
854 * make new citizens into scientists or taxmen -- Massimo */
856 /* Ignore food if no square can be worked */
857 city_tile_iterate_skip_free_worked(city_map_radius_sq_get(pcity
), pcenter
,
858 ptile
, _index
, _x
, _y
) {
859 if (tile_worked(ptile
) != pcity
/* quick test */
860 && city_can_work_tile(pcity
, ptile
)) {
863 } city_tile_iterate_skip_free_worked_end
;
865 if ((pcity
->surplus
[O_FOOD
] >= 2 || !have_square
)
866 && is_city_option_set(pcity
, CITYO_NEW_EINSTEIN
)) {
867 pcity
->specialists
[best_specialist(O_SCIENCE
, pcity
)]++;
868 } else if ((pcity
->surplus
[O_FOOD
] >= 2 || !have_square
)
869 && is_city_option_set(pcity
, CITYO_NEW_TAXMAN
)) {
870 pcity
->specialists
[best_specialist(O_GOLD
, pcity
)]++;
872 pcity
->specialists
[DEFAULT_SPECIALIST
]++; /* or else city is !sane */
875 /* Update citizens. */
876 citizens_update(pcity
, nationality
);
878 /* Refresh the city data; this also checks the squared city radius. */
881 auto_arrange_workers(pcity
);
883 /* Update cities that have trade routes with us */
884 trade_routes_iterate(pcity
, pcity2
) {
885 if (city_refresh(pcity2
)) {
886 /* This should never happen, but if it does, make sure not to
887 * leave workers outside city radius. */
888 auto_arrange_workers(pcity2
);
890 } trade_routes_iterate_end
;
892 notify_player(powner
, city_tile(pcity
), E_CITY_GROWTH
, ftc_server
,
893 _("%s grows to size %d."),
894 city_link(pcity
), city_size_get(pcity
));
896 /* Deprecated signal. Connect your lua functions to "city_size_change" that's
897 * emitted from calling functions which know the 'reason' of the increase. */
898 script_server_signal_emit("city_growth", 2,
899 API_TYPE_CITY
, pcity
,
900 API_TYPE_INT
, city_size_get(pcity
));
901 if (city_exist(saved_id
)) {
902 /* Script didn't destroy this city */
903 sanity_check_city(pcity
);
910 /****************************************************************************
911 Change the city size. Return TRUE iff the city is still alive afterwards.
912 ****************************************************************************/
913 bool city_change_size(struct city
*pcity
, citizens size
,
914 struct player
*nationality
, const char *reason
)
916 int change
= size
- city_size_get(pcity
);
918 if (change
!= 0 && reason
!= NULL
) {
919 script_server_signal_emit("city_size_change", 3,
920 API_TYPE_CITY
, pcity
,
921 API_TYPE_INT
, size
- city_size_get(pcity
),
922 API_TYPE_STRING
, reason
);
926 /* Increase city size until size reached, or increase fails */
927 while (size
> city_size_get(pcity
)
928 && city_increase_size(pcity
, nationality
)) {
929 /* city_increase_size() does all the work. */
931 } else if (change
< 0) {
932 /* We assume that city_change_size() is never called because
933 * of enemy actions. If that changes, enemy must be passed
934 * to city_reduce_size() */
935 return city_reduce_size(pcity
, -change
, NULL
, NULL
);
938 map_claim_border(pcity
->tile
, pcity
->owner
, -1);
943 /**************************************************************************
944 Check whether the population can be increased or
945 if the city is unable to support a 'settler'...
946 **************************************************************************/
947 static void city_populate(struct city
*pcity
, struct player
*nationality
)
949 int saved_id
= pcity
->id
;
950 int granary_size
= city_granary_size(city_size_get(pcity
));
952 pcity
->food_stock
+= pcity
->surplus
[O_FOOD
];
953 if (pcity
->food_stock
>= granary_size
|| city_rapture_grow(pcity
)) {
954 if (city_had_recent_plague(pcity
)) {
955 notify_player(city_owner(pcity
), city_tile(pcity
),
956 E_CITY_PLAGUE
, ftc_server
,
957 _("A recent plague outbreak prevents growth in %s."),
959 /* Lose excess food */
960 pcity
->food_stock
= MIN(pcity
->food_stock
, granary_size
);
962 city_increase_size(pcity
, nationality
);
963 map_claim_border(pcity
->tile
, pcity
->owner
, -1);
964 script_server_signal_emit("city_size_change", 3,
965 API_TYPE_CITY
, pcity
,
967 API_TYPE_STRING
, "growth");
969 } else if (pcity
->food_stock
< 0) {
970 /* FIXME: should this depend on units with ability to build
971 * cities or on units that require food in upkeep?
972 * I'll assume citybuilders (units that 'contain' 1 pop) -- sjolie
973 * The above may make more logical sense, but in game terms
974 * you want to disband a unit that is draining your food
975 * reserves. Hence, I'll assume food upkeep > 0 units. -- jjm
977 unit_list_iterate_safe(pcity
->units_supported
, punit
) {
978 if (punit
->upkeep
[O_FOOD
] > 0
979 && !unit_has_type_flag(punit
, UTYF_UNDISBANDABLE
)) {
981 notify_player(city_owner(pcity
), city_tile(pcity
),
982 E_UNIT_LOST_MISC
, ftc_server
,
983 _("Famine feared in %s, %s lost!"),
984 city_link(pcity
), unit_tile_link(punit
));
986 wipe_unit(punit
, ULR_STARVED
, NULL
);
988 if (city_exist(saved_id
)) {
989 city_reset_foodbox(pcity
, city_size_get(pcity
));
993 } unit_list_iterate_safe_end
;
994 if (city_size_get(pcity
) > 1) {
995 notify_player(city_owner(pcity
), city_tile(pcity
),
996 E_CITY_FAMINE
, ftc_server
,
997 _("Famine causes population loss in %s."),
1000 notify_player(city_owner(pcity
), city_tile(pcity
),
1001 E_CITY_FAMINE
, ftc_server
,
1002 _("Famine destroys %s entirely."),
1005 city_reset_foodbox(pcity
, city_size_get(pcity
) - 1);
1006 city_reduce_size(pcity
, 1, NULL
, "famine");
1010 /**************************************************************************
1011 Examine the worklist and change the build target. Return 0 if no
1012 targets are available to change to. Otherwise return non-zero. Has
1013 the side-effect of removing from the worklist any no-longer-available
1014 targets as well as the target actually selected, if any.
1015 **************************************************************************/
1016 static bool worklist_change_build_target(struct player
*pplayer
,
1019 struct universal target
;
1020 bool success
= FALSE
;
1022 int saved_id
= pcity
->id
;
1023 bool city_checked
= TRUE
; /* This is used to avoid spurious city_exist() calls */
1024 struct worklist
*pwl
= &pcity
->worklist
;
1026 if (worklist_is_empty(pwl
)) {
1027 /* Nothing in the worklist; bail now. */
1032 while (!success
&& i
< worklist_length(pwl
)) {
1034 if (!city_checked
) {
1035 if (!city_exist(saved_id
)) {
1036 /* Some script has removed useless city that cannot build
1037 * what it is told to! */
1040 city_checked
= TRUE
;
1043 if (worklist_peek_ith(pwl
, &target
, i
)) {
1044 success
= can_city_build_now(pcity
, &target
);
1054 switch (target
.kind
) {
1057 struct unit_type
*ptarget
= target
.value
.utype
;
1058 struct unit_type
*pupdate
= unit_upgrades_to(pcity
, ptarget
);
1060 /* Maybe we can just upgrade the target to what the city /can/ build. */
1061 if (U_NOT_OBSOLETED
== pupdate
) {
1062 /* Nope, we're stuck. Skip this item from the worklist. */
1063 if (ptarget
->need_government
!= NULL
1064 && ptarget
->need_government
!= government_of_player(pplayer
)) {
1065 notify_player(pplayer
, city_tile(pcity
),
1066 E_CITY_CANTBUILD
, ftc_server
,
1067 _("%s can't build %s from the worklist; "
1068 "it needs %s government. Postponing..."),
1069 city_link(pcity
), utype_name_translation(ptarget
),
1070 government_name_translation(ptarget
->need_government
));
1071 script_server_signal_emit("unit_cant_be_built", 3,
1072 API_TYPE_BUILDING_TYPE
, ptarget
,
1073 API_TYPE_CITY
, pcity
,
1074 API_TYPE_STRING
, "need_government");
1075 } else if (ptarget
->need_improvement
!= NULL
1076 && !city_has_building(pcity
, ptarget
->need_improvement
)) {
1077 notify_player(pplayer
, city_tile(pcity
),
1078 E_CITY_CANTBUILD
, ftc_server
,
1079 _("%s can't build %s from the worklist; "
1080 "need to have %s first. Postponing..."),
1081 city_link(pcity
), utype_name_translation(ptarget
),
1082 city_improvement_name_translation(pcity
,
1083 ptarget
->need_improvement
));
1084 script_server_signal_emit("unit_cant_be_built", 3,
1085 API_TYPE_UNIT_TYPE
, ptarget
,
1086 API_TYPE_CITY
, pcity
,
1087 API_TYPE_STRING
, "need_building");
1088 } else if (ptarget
->require_advance
!= NULL
1089 && TECH_KNOWN
!= research_invention_state
1090 (research_get(pplayer
),
1091 advance_number(ptarget
->require_advance
))) {
1092 notify_player(pplayer
, city_tile(pcity
),
1093 E_CITY_CANTBUILD
, ftc_server
,
1094 _("%s can't build %s from the worklist; "
1095 "tech %s not yet available. Postponing..."),
1096 city_link(pcity
), utype_name_translation(ptarget
),
1097 advance_name_translation(ptarget
->require_advance
));
1098 script_server_signal_emit("unit_cant_be_built", 3,
1099 API_TYPE_UNIT_TYPE
, ptarget
,
1100 API_TYPE_CITY
, pcity
,
1101 API_TYPE_STRING
, "need_tech");
1103 /* This shouldn't happen, but in case it does... */
1104 notify_player(pplayer
, city_tile(pcity
),
1105 E_CITY_CANTBUILD
, ftc_server
,
1106 _("%s can't build %s from the worklist; "
1107 "reason unknown! Postponing..."),
1108 city_link(pcity
), utype_name_translation(ptarget
));
1110 city_checked
= FALSE
;
1113 success
= can_city_build_unit_later(pcity
, pupdate
);
1115 /* If the city can never build this unit or its descendants,
1117 notify_player(pplayer
, city_tile(pcity
),
1118 E_CITY_CANTBUILD
, ftc_server
,
1119 _("%s can't build %s from the worklist. Purging..."),
1121 /* Yes, warn about the targets that's actually
1122 in the worklist, not its obsolete-closure
1124 utype_name_translation(ptarget
));
1125 script_server_signal_emit("unit_cant_be_built", 3,
1126 API_TYPE_UNIT_TYPE
, ptarget
,
1127 API_TYPE_CITY
, pcity
,
1128 API_TYPE_STRING
, "never");
1129 if (city_exist(saved_id
)) {
1130 city_checked
= TRUE
;
1131 /* Purge this worklist item. */
1133 worklist_remove(pwl
, i
);
1135 city_checked
= FALSE
;
1138 /* Yep, we can go after pupdate instead. Joy! */
1139 notify_player(pplayer
, city_tile(pcity
), E_WORKLIST
, ftc_server
,
1140 _("Production of %s is upgraded to %s in %s."),
1141 utype_name_translation(ptarget
),
1142 utype_name_translation(pupdate
),
1144 target
.value
.utype
= pupdate
;
1148 case VUT_IMPROVEMENT
:
1150 struct impr_type
*ptarget
= target
.value
.building
;
1151 struct impr_type
*pupdate
= building_upgrades_to(pcity
, ptarget
);
1153 /* If the city can never build this improvement, drop it. */
1154 success
= can_city_build_improvement_later(pcity
, pupdate
);
1156 /* Maybe this improvement has been obsoleted by something that
1158 if (success
&& pupdate
== ptarget
) {
1161 /* Nope, no use. *sigh* */
1162 requirement_vector_iterate(&ptarget
->reqs
, preq
) {
1163 if (!is_req_active(pplayer
, NULL
, pcity
, NULL
, NULL
, NULL
, NULL
,
1164 NULL
, NULL
, preq
, RPT_POSSIBLE
)) {
1166 switch (preq
->source
.kind
) {
1168 if (preq
->present
) {
1169 notify_player(pplayer
, city_tile(pcity
),
1170 E_CITY_CANTBUILD
, ftc_server
,
1171 _("%s can't build %s from the worklist; "
1172 "tech %s not yet available. Postponing..."),
1174 city_improvement_name_translation(pcity
, ptarget
),
1175 advance_name_translation
1176 (preq
->source
.value
.advance
));
1177 script_server_signal_emit("building_cant_be_built", 3,
1178 API_TYPE_BUILDING_TYPE
, ptarget
,
1179 API_TYPE_CITY
, pcity
,
1180 API_TYPE_STRING
, "need_tech");
1182 /* While techs can be unlearned, this isn't useful feedback */
1187 if (preq
->present
) {
1188 notify_player(pplayer
, city_tile(pcity
),
1189 E_CITY_CANTBUILD
, ftc_server
,
1190 _("%s can't build %s from the worklist; "
1191 "no tech with flag \"%s\" yet available. "
1194 city_improvement_name_translation(pcity
, ptarget
),
1195 tech_flag_id_name(preq
->source
.value
.techflag
));
1196 script_server_signal_emit("building_cant_be_built", 3,
1197 API_TYPE_BUILDING_TYPE
, ptarget
,
1198 API_TYPE_CITY
, pcity
,
1199 API_TYPE_STRING
, "need_techflag");
1201 /* While techs can be unlearned, this isn't useful feedback */
1205 case VUT_IMPROVEMENT
:
1206 if (preq
->present
) {
1207 notify_player(pplayer
, city_tile(pcity
),
1208 E_CITY_CANTBUILD
, ftc_server
,
1209 _("%s can't build %s from the worklist; "
1210 "need to have %s first. Postponing..."),
1212 city_improvement_name_translation(pcity
, ptarget
),
1213 city_improvement_name_translation(pcity
,
1214 preq
->source
.value
.building
));
1215 script_server_signal_emit("building_cant_be_built", 3,
1216 API_TYPE_BUILDING_TYPE
, ptarget
,
1217 API_TYPE_CITY
, pcity
,
1218 API_TYPE_STRING
, "need_building");
1220 notify_player(pplayer
, city_tile(pcity
),
1221 E_CITY_CANTBUILD
, ftc_server
,
1222 _("%s can't build %s from the worklist; "
1223 "need to not have %s. Postponing..."),
1225 city_improvement_name_translation(pcity
, ptarget
),
1226 city_improvement_name_translation(pcity
,
1227 preq
->source
.value
.building
));
1228 script_server_signal_emit("building_cant_be_built", 3,
1229 API_TYPE_BUILDING_TYPE
, ptarget
,
1230 API_TYPE_CITY
, pcity
,
1231 API_TYPE_STRING
, "have_building");
1234 case VUT_GOVERNMENT
:
1235 if (preq
->present
) {
1236 notify_player(pplayer
, city_tile(pcity
),
1237 E_CITY_CANTBUILD
, ftc_server
,
1238 _("%s can't build %s from the worklist; "
1239 "it needs %s government. Postponing..."),
1241 city_improvement_name_translation(pcity
, ptarget
),
1242 government_name_translation(preq
->source
.value
.govern
));
1243 script_server_signal_emit("building_cant_be_built", 3,
1244 API_TYPE_BUILDING_TYPE
, ptarget
,
1245 API_TYPE_CITY
, pcity
,
1246 API_TYPE_STRING
, "need_government");
1248 notify_player(pplayer
, city_tile(pcity
),
1249 E_CITY_CANTBUILD
, ftc_server
,
1250 _("%s can't build %s from the worklist; "
1251 "it cannot have %s government. Postponing..."),
1253 city_improvement_name_translation(pcity
, ptarget
),
1254 government_name_translation(preq
->source
.value
.govern
));
1255 script_server_signal_emit("building_cant_be_built", 3,
1256 API_TYPE_BUILDING_TYPE
, ptarget
,
1257 API_TYPE_CITY
, pcity
,
1258 API_TYPE_STRING
, "have_government");
1261 case VUT_ACHIEVEMENT
:
1262 if (preq
->present
) {
1263 notify_player(pplayer
, city_tile(pcity
),
1264 E_CITY_CANTBUILD
, ftc_server
,
1265 _("%s can't build %s from the worklist; "
1266 "it needs \"%s\" achievement. Postponing..."),
1268 city_improvement_name_translation(pcity
, ptarget
),
1269 achievement_name_translation(preq
->source
.value
.achievement
));
1270 script_server_signal_emit("building_cant_be_built", 3,
1271 API_TYPE_BUILDING_TYPE
, ptarget
,
1272 API_TYPE_CITY
, pcity
,
1273 API_TYPE_STRING
, "need_achievement");
1275 /* Can't unachieve things. */
1280 if (preq
->present
) {
1281 notify_player(pplayer
, city_tile(pcity
),
1282 E_CITY_CANTBUILD
, ftc_server
,
1283 Q_("?extra:%s can't build %s from the worklist; "
1284 "%s is required. Postponing..."),
1286 city_improvement_name_translation(pcity
, ptarget
),
1287 extra_name_translation(preq
->source
.value
.extra
));
1288 script_server_signal_emit("building_cant_be_built", 3,
1289 API_TYPE_BUILDING_TYPE
, ptarget
,
1290 API_TYPE_CITY
, pcity
,
1291 API_TYPE_STRING
, "need_extra");
1293 notify_player(pplayer
, city_tile(pcity
),
1294 E_CITY_CANTBUILD
, ftc_server
,
1295 Q_("?extra:%s can't build %s from the worklist; "
1296 "%s is prohibited. Postponing..."),
1298 city_improvement_name_translation(pcity
, ptarget
),
1299 extra_name_translation(preq
->source
.value
.extra
));
1300 script_server_signal_emit("building_cant_be_built", 3,
1301 API_TYPE_BUILDING_TYPE
, ptarget
,
1302 API_TYPE_CITY
, pcity
,
1303 API_TYPE_STRING
, "have_extra");
1307 if (preq
->present
) {
1308 notify_player(pplayer
, city_tile(pcity
),
1309 E_CITY_CANTBUILD
, ftc_server
,
1310 Q_("?terrain:%s can't build %s from the worklist; "
1311 "%s terrain is required. Postponing..."),
1313 city_improvement_name_translation(pcity
, ptarget
),
1314 terrain_name_translation(preq
->source
.value
.terrain
));
1315 script_server_signal_emit("building_cant_be_built", 3,
1316 API_TYPE_BUILDING_TYPE
, ptarget
,
1317 API_TYPE_CITY
, pcity
,
1318 API_TYPE_STRING
, "need_terrain");
1320 notify_player(pplayer
, city_tile(pcity
),
1321 E_CITY_CANTBUILD
, ftc_server
,
1322 Q_("?terrain:%s can't build %s from the worklist; "
1323 "%s terrain is prohibited. Postponing..."),
1325 city_improvement_name_translation(pcity
, ptarget
),
1326 terrain_name_translation(preq
->source
.value
.terrain
));
1327 script_server_signal_emit("building_cant_be_built", 3,
1328 API_TYPE_BUILDING_TYPE
, ptarget
,
1329 API_TYPE_CITY
, pcity
,
1330 API_TYPE_STRING
, "have_terrain");
1334 if (preq
->present
) {
1335 notify_player(pplayer
, city_tile(pcity
),
1336 E_CITY_CANTBUILD
, ftc_server
,
1337 Q_("?resource:%s can't build %s from the worklist; "
1338 "%s is required. Postponing..."),
1340 city_improvement_name_translation(pcity
, ptarget
),
1341 resource_name_translation(preq
->source
.value
.resource
));
1342 script_server_signal_emit("building_cant_be_built", 3,
1343 API_TYPE_BUILDING_TYPE
, ptarget
,
1344 API_TYPE_CITY
, pcity
,
1345 API_TYPE_STRING
, "need_resource");
1347 /* FIXME: Yes, it's possible to destroy a resource, but the
1348 * player probably wanted us to have purged, rather than
1349 * postponed. Purging should be separate from impossible. */
1350 notify_player(pplayer
, city_tile(pcity
),
1351 E_CITY_CANTBUILD
, ftc_server
,
1352 Q_("?resource:%s can't build %s from the worklist; "
1353 "%s is prohibited. Postponing..."),
1355 city_improvement_name_translation(pcity
, ptarget
),
1356 resource_name_translation(preq
->source
.value
.resource
));
1357 script_server_signal_emit("building_cant_be_built", 3,
1358 API_TYPE_BUILDING_TYPE
, ptarget
,
1359 API_TYPE_CITY
, pcity
,
1360 API_TYPE_STRING
, "have_resource");
1364 /* Nation can be required at Alliance range, which may change. */
1365 if (preq
->present
) {
1366 notify_player(pplayer
, city_tile(pcity
),
1367 E_CITY_CANTBUILD
, ftc_server
,
1368 /* TRANS: "%s nation" is adjective */
1369 Q_("?nation:%s can't build %s from the worklist; "
1370 "%s nation is required. Postponing..."),
1372 city_improvement_name_translation(pcity
, ptarget
),
1373 nation_adjective_translation(preq
->source
.value
.nation
));
1374 script_server_signal_emit("building_cant_be_built", 3,
1375 API_TYPE_BUILDING_TYPE
, ptarget
,
1376 API_TYPE_CITY
, pcity
,
1377 API_TYPE_STRING
, "need_nation");
1379 notify_player(pplayer
, city_tile(pcity
),
1380 E_CITY_CANTBUILD
, ftc_server
,
1381 Q_("?nation:%s can't build %s from the worklist; "
1382 "%s nation is prohibited. Postponing..."),
1384 city_improvement_name_translation(pcity
, ptarget
),
1385 nation_adjective_translation(preq
->source
.value
.nation
));
1386 script_server_signal_emit("building_cant_be_built", 3,
1387 API_TYPE_BUILDING_TYPE
, ptarget
,
1388 API_TYPE_CITY
, pcity
,
1389 API_TYPE_STRING
, "have_nation");
1392 case VUT_NATIONGROUP
:
1393 /* Nation group can be required at Alliance range, which may
1395 if (preq
->present
) {
1396 notify_player(pplayer
, city_tile(pcity
),
1397 E_CITY_CANTBUILD
, ftc_server
,
1398 /* TRANS: "%s nation" is adjective */
1399 Q_("?ngroup:%s can't build %s from the worklist; "
1400 "%s nation is required. Postponing..."),
1402 city_improvement_name_translation(pcity
, ptarget
),
1403 nation_group_name_translation(preq
->source
.value
.nationgroup
));
1404 script_server_signal_emit("building_cant_be_built", 3,
1405 API_TYPE_BUILDING_TYPE
, ptarget
,
1406 API_TYPE_CITY
, pcity
,
1407 API_TYPE_STRING
, "need_nationgroup");
1409 notify_player(pplayer
, city_tile(pcity
),
1410 E_CITY_CANTBUILD
, ftc_server
,
1411 Q_("?ngroup:%s can't build %s from the worklist; "
1412 "%s nation is prohibited. Postponing..."),
1414 city_improvement_name_translation(pcity
, ptarget
),
1415 nation_group_name_translation(preq
->source
.value
.nationgroup
));
1416 script_server_signal_emit("building_cant_be_built", 3,
1417 API_TYPE_BUILDING_TYPE
, ptarget
,
1418 API_TYPE_CITY
, pcity
,
1419 API_TYPE_STRING
, "have_nationgroup");
1423 /* FIXME: City styles sometimes change over time, but it isn't
1424 * entirely under player control. Probably better to purge
1425 * with useful explanation. */
1426 if (preq
->present
) {
1427 notify_player(pplayer
, city_tile(pcity
),
1428 E_CITY_CANTBUILD
, ftc_server
,
1429 _("%s can't build %s from the worklist; "
1430 "only %s style cities may build this. Postponing..."),
1432 city_improvement_name_translation(pcity
, ptarget
),
1433 style_name_translation(preq
->source
.value
.style
));
1434 script_server_signal_emit("building_cant_be_built", 3,
1435 API_TYPE_BUILDING_TYPE
, ptarget
,
1436 API_TYPE_CITY
, pcity
,
1437 API_TYPE_STRING
, "need_style");
1439 notify_player(pplayer
, city_tile(pcity
),
1440 E_CITY_CANTBUILD
, ftc_server
,
1441 _("%s can't build %s from the worklist; "
1442 "%s style cities may not build this. Postponing..."),
1444 city_improvement_name_translation(pcity
, ptarget
),
1445 style_name_translation(preq
->source
.value
.style
));
1446 script_server_signal_emit("building_cant_be_built", 3,
1447 API_TYPE_BUILDING_TYPE
, ptarget
,
1448 API_TYPE_CITY
, pcity
,
1449 API_TYPE_STRING
, "have_style");
1452 case VUT_NATIONALITY
:
1453 /* FIXME: Changing citizen nationality is hard: purging might be
1454 * more useful in this case. */
1455 if (preq
->present
) {
1456 notify_player(pplayer
, city_tile(pcity
),
1457 E_CITY_CANTBUILD
, ftc_server
,
1458 /* TRANS: Latter %s is citizen nationality */
1459 _("%s can't build %s from the worklist; "
1460 "only city with %s may build this. Postponing..."),
1462 city_improvement_name_translation(pcity
, ptarget
),
1463 nation_plural_translation(preq
->source
.value
.nationality
));
1464 script_server_signal_emit("building_cant_be_built", 3,
1465 API_TYPE_BUILDING_TYPE
, ptarget
,
1466 API_TYPE_CITY
, pcity
,
1467 API_TYPE_STRING
, "need_nationality");
1469 notify_player(pplayer
, city_tile(pcity
),
1470 E_CITY_CANTBUILD
, ftc_server
,
1471 /* TRANS: Latter %s is citizen nationality */
1472 _("%s can't build %s from the worklist; "
1473 "only city without %s may build this. Postponing..."),
1475 city_improvement_name_translation(pcity
, ptarget
),
1476 nation_plural_translation(preq
->source
.value
.nationality
));
1477 script_server_signal_emit("building_cant_be_built", 3,
1478 API_TYPE_BUILDING_TYPE
, ptarget
,
1479 API_TYPE_CITY
, pcity
,
1480 API_TYPE_STRING
, "have_nationality");
1484 if (preq
->present
) {
1485 notify_player(pplayer
, city_tile(pcity
),
1486 E_CITY_CANTBUILD
, ftc_server
,
1487 /* TRANS: '%s' is a wide range of relationships;
1488 * e.g., 'Peace', 'Never met', 'Is foreign',
1489 * 'Hosts embassy', 'Provided Casus Belli' */
1490 _("%s can't build %s from the worklist; "
1491 "the relationship '%s' is required."
1494 city_improvement_name_translation(pcity
,
1496 diplrel_name_translation(
1497 preq
->source
.value
.diplrel
));
1498 script_server_signal_emit("building_cant_be_built", 3,
1499 API_TYPE_BUILDING_TYPE
, ptarget
,
1500 API_TYPE_CITY
, pcity
,
1501 API_TYPE_STRING
, "need_diplrel");
1503 notify_player(pplayer
, city_tile(pcity
),
1504 E_CITY_CANTBUILD
, ftc_server
,
1505 _("%s can't build %s from the worklist; "
1506 "the relationship '%s' is prohibited."
1509 city_improvement_name_translation(pcity
,
1511 diplrel_name_translation(
1512 preq
->source
.value
.diplrel
));
1513 script_server_signal_emit("building_cant_be_built", 3,
1514 API_TYPE_BUILDING_TYPE
, ptarget
,
1515 API_TYPE_CITY
, pcity
,
1516 API_TYPE_STRING
, "have_diplrel");
1520 if (preq
->present
) {
1521 notify_player(pplayer
, city_tile(pcity
),
1522 E_CITY_CANTBUILD
, ftc_server
,
1523 _("%s can't build %s from the worklist; "
1524 "city must be of size %d or larger. "
1527 city_improvement_name_translation(pcity
, ptarget
),
1528 preq
->source
.value
.minsize
);
1529 script_server_signal_emit("building_cant_be_built", 3,
1530 API_TYPE_BUILDING_TYPE
, ptarget
,
1531 API_TYPE_CITY
, pcity
,
1532 API_TYPE_STRING
, "need_minsize");
1534 notify_player(pplayer
, city_tile(pcity
),
1535 E_CITY_CANTBUILD
, ftc_server
,
1536 _("%s can't build %s from the worklist; "
1537 "city must be of size %d or smaller."
1540 city_improvement_name_translation(pcity
, ptarget
),
1541 (preq
->source
.value
.minsize
- 1));
1542 script_server_signal_emit("building_cant_be_built", 3,
1543 API_TYPE_BUILDING_TYPE
, ptarget
,
1544 API_TYPE_CITY
, pcity
,
1545 API_TYPE_STRING
, "need_minsize");
1548 case VUT_MINCULTURE
:
1549 if (preq
->present
) {
1550 notify_player(pplayer
, city_tile(pcity
),
1551 E_CITY_CANTBUILD
, ftc_server
,
1552 _("%s can't build %s from the worklist; "
1553 "city must have culture of %d. Postponing..."),
1555 city_improvement_name_translation(pcity
, ptarget
),
1556 preq
->source
.value
.minculture
);
1557 script_server_signal_emit("building_cant_be_built", 3,
1558 API_TYPE_BUILDING_TYPE
, ptarget
,
1559 API_TYPE_CITY
, pcity
,
1560 API_TYPE_STRING
, "need_minculture");
1562 /* What has been written may not be unwritten. */
1566 case VUT_MAXTILEUNITS
:
1567 if (preq
->present
) {
1568 notify_player(pplayer
, city_tile(pcity
),
1569 E_CITY_CANTBUILD
, ftc_server
,
1570 PL_("%s can't build %s from the worklist; "
1571 "more than %d unit on tile."
1573 "%s can't build %s from the worklist; "
1574 "more than %d units on tile."
1576 preq
->source
.value
.max_tile_units
),
1578 city_improvement_name_translation(pcity
,
1580 preq
->source
.value
.max_tile_units
);
1581 script_server_signal_emit("building_cant_be_built", 3,
1582 API_TYPE_BUILDING_TYPE
, ptarget
,
1583 API_TYPE_CITY
, pcity
,
1584 API_TYPE_STRING
, "need_tileunits");
1586 notify_player(pplayer
, city_tile(pcity
),
1587 E_CITY_CANTBUILD
, ftc_server
,
1588 PL_("%s can't build %s from the worklist; "
1589 "fewer than %d unit on tile."
1591 "%s can't build %s from the worklist; "
1592 "fewer than %d units on tile."
1594 preq
->source
.value
.max_tile_units
+ 1),
1596 city_improvement_name_translation(pcity
,
1598 preq
->source
.value
.max_tile_units
+ 1);
1599 script_server_signal_emit("building_cant_be_built", 3,
1600 API_TYPE_BUILDING_TYPE
, ptarget
,
1601 API_TYPE_CITY
, pcity
,
1602 API_TYPE_STRING
, "need_tileunits");
1606 /* Can't change AI level. */
1609 case VUT_TERRAINCLASS
:
1610 if (preq
->present
) {
1611 notify_player(pplayer
, city_tile(pcity
),
1612 E_CITY_CANTBUILD
, ftc_server
,
1613 Q_("?terrainclass:%s can't build %s from the "
1614 "worklist; %s terrain is required."
1617 city_improvement_name_translation(pcity
, ptarget
),
1618 terrain_class_name_translation(preq
->source
.value
.terrainclass
));
1619 script_server_signal_emit("building_cant_be_built", 3,
1620 API_TYPE_BUILDING_TYPE
, ptarget
,
1621 API_TYPE_CITY
, pcity
,
1622 API_TYPE_STRING
, "need_terrainclass");
1624 notify_player(pplayer
, city_tile(pcity
),
1625 E_CITY_CANTBUILD
, ftc_server
,
1626 Q_("?terrainclass:%s can't build %s from the "
1627 "worklist; %s terrain is prohibited."
1630 city_improvement_name_translation(pcity
, ptarget
),
1631 terrain_class_name_translation(preq
->source
.value
.terrainclass
));
1632 script_server_signal_emit("building_cant_be_built", 3,
1633 API_TYPE_BUILDING_TYPE
, ptarget
,
1634 API_TYPE_CITY
, pcity
,
1635 API_TYPE_STRING
, "have_terrainclass");
1639 if (preq
->present
) {
1640 notify_player(pplayer
, city_tile(pcity
),
1641 E_CITY_CANTBUILD
, ftc_server
,
1642 _("%s can't build %s from the worklist; "
1643 "terrain with \"%s\" flag is required. "
1646 city_improvement_name_translation(pcity
, ptarget
),
1647 terrain_flag_id_name(preq
->source
.value
.terrainflag
));
1648 script_server_signal_emit("building_cant_be_built", 3,
1649 API_TYPE_BUILDING_TYPE
, ptarget
,
1650 API_TYPE_CITY
, pcity
,
1651 API_TYPE_STRING
, "need_terrainflag");
1653 notify_player(pplayer
, city_tile(pcity
),
1654 E_CITY_CANTBUILD
, ftc_server
,
1655 _("%s can't build %s from the worklist; "
1656 "terrain with \"%s\" flag is prohibited. "
1659 city_improvement_name_translation(pcity
, ptarget
),
1660 terrain_flag_id_name(preq
->source
.value
.terrainflag
));
1661 script_server_signal_emit("building_cant_be_built", 3,
1662 API_TYPE_BUILDING_TYPE
, ptarget
,
1663 API_TYPE_CITY
, pcity
,
1664 API_TYPE_STRING
, "have_terrainflag");
1668 if (preq
->present
) {
1669 notify_player(pplayer
, city_tile(pcity
),
1670 E_CITY_CANTBUILD
, ftc_server
,
1671 _("%s can't build %s from the worklist; "
1672 "base with \"%s\" flag is required. "
1675 city_improvement_name_translation(pcity
, ptarget
),
1676 base_flag_id_name(preq
->source
.value
.baseflag
));
1677 script_server_signal_emit("building_cant_be_built", 3,
1678 API_TYPE_BUILDING_TYPE
, ptarget
,
1679 API_TYPE_CITY
, pcity
,
1680 API_TYPE_STRING
, "need_baseflag");
1682 notify_player(pplayer
, city_tile(pcity
),
1683 E_CITY_CANTBUILD
, ftc_server
,
1684 _("%s can't build %s from the worklist; "
1685 "base with \"%s\" flag is prohibited. "
1688 city_improvement_name_translation(pcity
, ptarget
),
1689 base_flag_id_name(preq
->source
.value
.baseflag
));
1690 script_server_signal_emit("building_cant_be_built", 3,
1691 API_TYPE_BUILDING_TYPE
, ptarget
,
1692 API_TYPE_CITY
, pcity
,
1693 API_TYPE_STRING
, "have_baseflag");
1697 if (preq
->present
) {
1698 notify_player(pplayer
, city_tile(pcity
),
1699 E_CITY_CANTBUILD
, ftc_server
,
1700 _("%s can't build %s from the worklist; "
1701 "road with \"%s\" flag is required. "
1704 city_improvement_name_translation(pcity
, ptarget
),
1705 road_flag_id_name(preq
->source
.value
.roadflag
));
1706 script_server_signal_emit("building_cant_be_built", 3,
1707 API_TYPE_BUILDING_TYPE
, ptarget
,
1708 API_TYPE_CITY
, pcity
,
1709 API_TYPE_STRING
, "need_roadflag");
1711 notify_player(pplayer
, city_tile(pcity
),
1712 E_CITY_CANTBUILD
, ftc_server
,
1713 _("%s can't build %s from the worklist; "
1714 "road with \"%s\" flag is prohibited. "
1717 city_improvement_name_translation(pcity
, ptarget
),
1718 road_flag_id_name(preq
->source
.value
.roadflag
));
1719 script_server_signal_emit("building_cant_be_built", 3,
1720 API_TYPE_BUILDING_TYPE
, ptarget
,
1721 API_TYPE_CITY
, pcity
,
1722 API_TYPE_STRING
, "have_roadflag");
1726 if (preq
->present
) {
1727 notify_player(pplayer
, city_tile(pcity
),
1728 E_CITY_CANTBUILD
, ftc_server
,
1729 _("%s can't build %s from the worklist; "
1730 "extra with \"%s\" flag is required. "
1733 city_improvement_name_translation(pcity
, ptarget
),
1734 extra_flag_id_translated_name(preq
->source
.value
.extraflag
));
1735 script_server_signal_emit("building_cant_be_built", 3,
1736 API_TYPE_BUILDING_TYPE
, ptarget
,
1737 API_TYPE_CITY
, pcity
,
1738 API_TYPE_STRING
, "need_extraflag");
1740 notify_player(pplayer
, city_tile(pcity
),
1741 E_CITY_CANTBUILD
, ftc_server
,
1742 _("%s can't build %s from the worklist; "
1743 "extra with \"%s\" flag is prohibited. "
1746 city_improvement_name_translation(pcity
, ptarget
),
1747 extra_flag_id_translated_name(preq
->source
.value
.extraflag
));
1748 script_server_signal_emit("building_cant_be_built", 3,
1749 API_TYPE_BUILDING_TYPE
, ptarget
,
1750 API_TYPE_CITY
, pcity
,
1751 API_TYPE_STRING
, "have_extraflag");
1758 case VUT_MINVETERAN
:
1763 case VUT_SPECIALIST
:
1764 case VUT_TERRAINALTER
: /* XXX could do this in principle */
1766 /* Will only happen with a bogus ruleset. */
1767 log_error("worklist_change_build_target() has bogus preq");
1770 if (preq
->present
) {
1771 notify_player(pplayer
, city_tile(pcity
),
1772 E_CITY_CANTBUILD
, ftc_server
,
1773 /* TRANS: last %s is a date */
1774 _("%s can't build %s from the worklist; "
1775 "only available from %s. Postponing..."),
1777 city_improvement_name_translation(pcity
, ptarget
),
1778 textyear(preq
->source
.value
.minyear
));
1779 script_server_signal_emit("building_cant_be_built", 3,
1780 API_TYPE_BUILDING_TYPE
, ptarget
,
1781 API_TYPE_CITY
, pcity
,
1782 API_TYPE_STRING
, "need_minyear");
1784 /* Can't go back in time. */
1789 if (preq
->present
) {
1790 notify_player(pplayer
, city_tile(pcity
),
1791 E_CITY_CANTBUILD
, ftc_server
,
1792 _("%s can't build %s from the workist; "
1793 "only available in worlds with %s map."),
1795 city_improvement_name_translation(pcity
, ptarget
),
1796 _(topo_flag_name(preq
->source
.value
.topo_property
)));
1797 script_server_signal_emit("building_cant_be_built", 3,
1798 API_TYPE_BUILDING_TYPE
, ptarget
,
1799 API_TYPE_CITY
, pcity
,
1800 API_TYPE_STRING
, "need_topo");
1805 if (preq
->present
) {
1806 notify_player(pplayer
, city_tile(pcity
),
1807 E_CITY_CANTBUILD
, ftc_server
,
1808 _("%s can't build %s from the worklist; "
1809 "only available once %d turns old. Postponing..."),
1811 city_improvement_name_translation(pcity
, ptarget
),
1812 preq
->source
.value
.age
);
1813 script_server_signal_emit("building_cant_be_built", 3,
1814 API_TYPE_BUILDING_TYPE
, ptarget
,
1815 API_TYPE_CITY
, pcity
,
1816 API_TYPE_STRING
, "need_age");
1818 /* Can't go back in time. */
1824 fc_assert_ret_val_msg(FALSE
, TRUE
,
1825 "worklist_change_build_target() "
1826 "called with invalid preq");
1828 /* No default handling here, as we want compiler warning
1829 * if new requirement type is added to enum and it's not handled
1835 /* Almost all cases emit signal in the end, so city check needed. */
1836 if (!city_exist(saved_id
)) {
1837 /* Some script has removed city */
1840 city_checked
= TRUE
;
1842 } requirement_vector_iterate_end
;
1845 /* This shouldn't happen...
1846 FIXME: make can_city_build_improvement_now() return a reason enum. */
1847 notify_player(pplayer
, city_tile(pcity
),
1848 E_CITY_CANTBUILD
, ftc_server
,
1849 _("%s can't build %s from the worklist; "
1850 "reason unknown! Postponing..."),
1852 city_improvement_name_translation(pcity
, ptarget
));
1854 } else if (success
) {
1855 /* Hey, we can upgrade the improvement! */
1856 notify_player(pplayer
, city_tile(pcity
), E_WORKLIST
, ftc_server
,
1857 _("Production of %s is upgraded to %s in %s."),
1858 city_improvement_name_translation(pcity
, ptarget
),
1859 city_improvement_name_translation(pcity
, pupdate
),
1861 target
.value
.building
= pupdate
;
1866 /* Never in a million years. */
1867 notify_player(pplayer
, city_tile(pcity
),
1868 E_CITY_CANTBUILD
, ftc_server
,
1869 _("%s can't build %s from the worklist. Purging..."),
1871 city_improvement_name_translation(pcity
, ptarget
));
1872 script_server_signal_emit("building_cant_be_built", 3,
1873 API_TYPE_BUILDING_TYPE
, ptarget
,
1874 API_TYPE_CITY
, pcity
,
1875 API_TYPE_STRING
, "never");
1876 if (city_exist(saved_id
)) {
1877 city_checked
= TRUE
;
1878 /* Purge this worklist item. */
1880 worklist_remove(pwl
, i
);
1882 city_checked
= FALSE
;
1888 /* skip useless target */
1889 log_error("worklist_change_build_target() has unrecognized "
1890 "target kind (%d)", target
.kind
);
1896 /* All okay. Switch targets. */
1897 change_build_target(pplayer
, pcity
, &target
, E_WORKLIST
);
1899 /* i is the index immediately _after_ the item we're changing to.
1900 Remove the (i-1)th item from the worklist. */
1901 worklist_remove(pwl
, i
- 1);
1904 if (worklist_is_empty(pwl
)) {
1905 /* There *was* something in the worklist, but it's empty now. Bug the
1907 notify_player(pplayer
, city_tile(pcity
), E_WORKLIST
, ftc_server
,
1908 /* TRANS: The <city> worklist .... */
1909 _("The %s worklist is now empty."),
1916 /**************************************************************************
1917 Assuming we just finished building something, find something new to
1918 build. The policy is: use the worklist if we can; if not, try not
1919 changing; if we must change, get desparate and use the AI advisor.
1920 **************************************************************************/
1921 void choose_build_target(struct player
*pplayer
, struct city
*pcity
)
1923 /* Pick the next thing off the worklist. */
1924 if (worklist_change_build_target(pplayer
, pcity
)) {
1928 /* Try building the same thing again. Repeat building doesn't require a
1929 * call to change_build_target, so just return. */
1930 switch (pcity
->production
.kind
) {
1932 /* We can build a unit again unless it's unique or we have lost the tech. */
1933 if (!utype_has_flag(pcity
->production
.value
.utype
, UTYF_UNIQUE
)
1934 && can_city_build_unit_now(pcity
, pcity
->production
.value
.utype
)) {
1935 log_debug("%s repeats building %s", city_name_get(pcity
),
1936 utype_rule_name(pcity
->production
.value
.utype
));
1940 case VUT_IMPROVEMENT
:
1941 if (can_city_build_improvement_now(pcity
, pcity
->production
.value
.building
)) {
1942 /* We can build space and coinage again, and possibly others. */
1943 log_debug("%s repeats building %s", city_name_get(pcity
),
1944 improvement_rule_name(pcity
->production
.value
.building
));
1953 /* Find *something* to do! */
1954 log_debug("Trying advisor_choose_build.");
1955 advisor_choose_build(pplayer
, pcity
);
1956 log_debug("Advisor_choose_build didn't kill us.");
1959 /**************************************************************************
1960 Follow the list of replacement buildings until we hit something that
1961 we can build. Returns NULL if we can't upgrade at all (including if the
1962 original building is unbuildable).
1963 **************************************************************************/
1964 static struct impr_type
*building_upgrades_to(struct city
*pcity
,
1965 struct impr_type
*pimprove
)
1967 struct impr_type
*check
= pimprove
;
1968 struct impr_type
*best_upgrade
= NULL
;
1970 if (!can_city_build_improvement_direct(pcity
, check
)) {
1973 while (valid_improvement(check
= improvement_replacement(check
))) {
1974 if (can_city_build_improvement_direct(pcity
, check
)) {
1975 best_upgrade
= check
;
1979 return best_upgrade
;
1982 /**************************************************************************
1983 Try to upgrade production in pcity.
1984 **************************************************************************/
1985 static void upgrade_building_prod(struct city
*pcity
)
1987 struct impr_type
*producing
= pcity
->production
.value
.building
;
1988 struct impr_type
*upgrading
= building_upgrades_to(pcity
, producing
);
1990 if (upgrading
&& can_city_build_improvement_now(pcity
, upgrading
)) {
1991 notify_player(city_owner(pcity
), city_tile(pcity
),
1992 E_UNIT_UPGRADED
, ftc_server
,
1993 _("Production of %s is upgraded to %s in %s."),
1994 improvement_name_translation(producing
),
1995 improvement_name_translation(upgrading
),
1997 pcity
->production
.kind
= VUT_IMPROVEMENT
;
1998 pcity
->production
.value
.building
= upgrading
;
2002 /**************************************************************************
2003 Follow the list of obsoleted_by units until we hit something that
2004 we can build. Return NULL when we can't upgrade at all. NB: returning
2005 something doesn't guarantee that pcity really _can_ build it; just that
2006 pcity can't build whatever _obsoletes_ it.
2008 FIXME: this function is a duplicate of can_upgrade_unittype.
2009 **************************************************************************/
2010 static struct unit_type
*unit_upgrades_to(struct city
*pcity
,
2011 struct unit_type
*punittype
)
2013 struct unit_type
*check
= punittype
;
2014 struct unit_type
*best_upgrade
= U_NOT_OBSOLETED
;
2016 if (!can_city_build_unit_direct(pcity
, punittype
)) {
2017 return U_NOT_OBSOLETED
;
2019 while ((check
= check
->obsoleted_by
) != U_NOT_OBSOLETED
) {
2020 if (can_city_build_unit_direct(pcity
, check
)) {
2021 best_upgrade
= check
;
2025 return best_upgrade
;
2028 /**************************************************************************
2029 Try to upgrade production in pcity.
2030 **************************************************************************/
2031 static void upgrade_unit_prod(struct city
*pcity
)
2033 struct unit_type
*producing
= pcity
->production
.value
.utype
;
2034 struct unit_type
*upgrading
= unit_upgrades_to(pcity
, producing
);
2036 if (upgrading
&& can_city_build_unit_direct(pcity
, upgrading
)) {
2037 notify_player(city_owner(pcity
), city_tile(pcity
),
2038 E_UNIT_UPGRADED
, ftc_server
,
2039 _("Production of %s is upgraded to %s in %s."),
2040 utype_name_translation(producing
),
2041 utype_name_translation(upgrading
),
2043 pcity
->production
.value
.utype
= upgrading
;
2047 /**************************************************************************
2048 Disband units if we don't have enough shields to support them. Returns
2049 FALSE if the _city_ is disbanded as a result.
2050 **************************************************************************/
2051 static bool city_distribute_surplus_shields(struct player
*pplayer
,
2054 if (pcity
->surplus
[O_SHIELD
] < 0) {
2055 unit_list_iterate_safe(pcity
->units_supported
, punit
) {
2056 if (utype_upkeep_cost(unit_type_get(punit
), pplayer
, O_SHIELD
) > 0
2057 && pcity
->surplus
[O_SHIELD
] < 0
2058 && !unit_has_type_flag(punit
, UTYF_UNDISBANDABLE
)) {
2059 notify_player(pplayer
, city_tile(pcity
),
2060 E_UNIT_LOST_MISC
, ftc_server
,
2061 _("%s can't upkeep %s, unit disbanded."),
2062 city_link(pcity
), unit_link(punit
));
2063 handle_unit_disband(pplayer
, punit
->id
);
2064 /* pcity->surplus[O_SHIELD] is automatically updated. */
2066 } unit_list_iterate_safe_end
;
2069 if (pcity
->surplus
[O_SHIELD
] < 0) {
2070 /* Special case: UTYF_UNDISBANDABLE. This nasty unit won't go so easily.
2071 * It'd rather make the citizens pay in blood for their failure to upkeep
2072 * it! If we make it here all normal units are already disbanded, so only
2073 * undisbandable ones remain. */
2074 unit_list_iterate_safe(pcity
->units_supported
, punit
) {
2075 int upkeep
= utype_upkeep_cost(unit_type_get(punit
), pplayer
, O_SHIELD
);
2077 if (upkeep
> 0 && pcity
->surplus
[O_SHIELD
] < 0) {
2078 fc_assert_action(unit_has_type_flag(punit
, UTYF_UNDISBANDABLE
),
2080 notify_player(pplayer
, city_tile(pcity
),
2081 E_UNIT_LOST_MISC
, ftc_server
,
2082 _("Citizens in %s perish for their failure to "
2084 city_link(pcity
), unit_link(punit
));
2085 if (!city_reduce_size(pcity
, 1, NULL
, "upkeep_failure")) {
2089 /* No upkeep for the unit this turn. */
2090 pcity
->surplus
[O_SHIELD
] += upkeep
;
2092 } unit_list_iterate_safe_end
;
2095 /* Now we confirm changes made last turn. */
2096 pcity
->shield_stock
+= pcity
->surplus
[O_SHIELD
];
2097 pcity
->before_change_shields
= pcity
->shield_stock
;
2098 pcity
->last_turns_shield_surplus
= pcity
->surplus
[O_SHIELD
];
2103 /**************************************************************************
2104 Returns FALSE when the city is removed, TRUE otherwise.
2105 **************************************************************************/
2106 static bool city_build_building(struct player
*pplayer
, struct city
*pcity
)
2110 struct impr_type
*pimprove
= pcity
->production
.value
.building
;
2111 int saved_id
= pcity
->id
;
2113 if (city_production_has_flag(pcity
, IF_GOLD
)) {
2114 fc_assert(pcity
->surplus
[O_SHIELD
] >= 0);
2115 /* pcity->before_change_shields already contains the surplus from
2117 pplayer
->economic
.gold
+= pcity
->before_change_shields
;
2118 pcity
->before_change_shields
= 0;
2119 pcity
->shield_stock
= 0;
2120 choose_build_target(pplayer
, pcity
);
2123 upgrade_building_prod(pcity
);
2125 if (!can_city_build_improvement_now(pcity
, pimprove
)) {
2126 notify_player(pplayer
, city_tile(pcity
), E_CITY_CANTBUILD
, ftc_server
,
2127 _("%s is building %s, which is no longer available."),
2129 city_improvement_name_translation(pcity
, pimprove
));
2130 script_server_signal_emit("building_cant_be_built", 3,
2131 API_TYPE_BUILDING_TYPE
, pimprove
,
2132 API_TYPE_CITY
, pcity
,
2133 API_TYPE_STRING
, "unavailable");
2136 if (pcity
->shield_stock
>= impr_build_shield_cost(pimprove
)) {
2137 if (is_small_wonder(pimprove
)) {
2138 city_list_iterate(pplayer
->cities
, wcity
) {
2139 if (city_has_building(wcity
, pimprove
)) {
2140 city_remove_improvement(wcity
, pimprove
);
2143 } city_list_iterate_end
;
2147 if (get_current_construction_bonus(pcity
, EFT_SS_STRUCTURAL
,
2149 pplayer
->spaceship
.structurals
++;
2150 } else if (get_current_construction_bonus(pcity
, EFT_SS_COMPONENT
,
2152 pplayer
->spaceship
.components
++;
2153 } else if (get_current_construction_bonus(pcity
, EFT_SS_MODULE
,
2155 pplayer
->spaceship
.modules
++;
2158 city_add_improvement(pcity
, pimprove
);
2160 pcity
->before_change_shields
-= impr_build_shield_cost(pimprove
);
2161 pcity
->shield_stock
-= impr_build_shield_cost(pimprove
);
2162 pcity
->turn_last_built
= game
.info
.turn
;
2163 /* to eliminate micromanagement */
2164 if (is_great_wonder(pimprove
)) {
2165 notify_player(NULL
, city_tile(pcity
), E_WONDER_BUILD
, ftc_server
,
2166 _("The %s have finished building %s in %s."),
2167 nation_plural_for_player(pplayer
),
2168 city_improvement_name_translation(pcity
, pimprove
),
2172 notify_player(pplayer
, city_tile(pcity
), E_IMP_BUILD
, ftc_server
,
2173 _("%s has finished building %s."),
2174 city_link(pcity
), improvement_name_translation(pimprove
));
2175 script_server_signal_emit("building_built", 2,
2176 API_TYPE_BUILDING_TYPE
, pimprove
,
2177 API_TYPE_CITY
, pcity
);
2179 if (!city_exist(saved_id
)) {
2180 /* Script removed city */
2184 /* Call this function since some buildings may change the
2185 * the vision range of a city */
2186 city_refresh_vision(pcity
);
2188 if ((mod
= get_current_construction_bonus(pcity
, EFT_GIVE_IMM_TECH
,
2190 struct research
*presearch
= research_get(pplayer
);
2191 char research_name
[MAX_LEN_NAME
* 2];
2193 const char *provider
= improvement_name_translation(pimprove
);
2195 notify_research(presearch
, NULL
, E_TECH_GAIN
, ftc_server
,
2196 PL_("%s boosts research; you gain %d immediate "
2198 "%s boosts research; you gain %d immediate "
2200 mod
), provider
, mod
);
2202 research_pretty_name(presearch
, research_name
, sizeof(research_name
));
2203 for (i
= 0; i
< mod
; i
++) {
2204 Tech_type_id tech
= give_immediate_free_tech(presearch
);
2205 const char *adv_name
= research_advance_name_translation(presearch
, tech
);
2207 notify_research(presearch
, NULL
, E_TECH_GAIN
, ftc_server
,
2208 /* TRANS: Tech from building (Darwin's Voyage) */
2209 Q_("?frombldg:Acquired %s from %s."), adv_name
,
2212 notify_research_embassies(presearch
, NULL
, E_TECH_EMBASSY
, ftc_server
,
2213 /* TRANS: Tech from building (Darwin's
2215 Q_("?frombldg:The %s have acquired %s "
2217 research_name
, adv_name
, provider
);
2220 if (space_part
&& pplayer
->spaceship
.state
== SSHIP_NONE
) {
2221 notify_player(NULL
, city_tile(pcity
), E_SPACESHIP
, ftc_server
,
2222 _("The %s have started building a spaceship!"),
2223 nation_plural_for_player(pplayer
));
2224 pplayer
->spaceship
.state
= SSHIP_STARTED
;
2227 /* space ship part build */
2228 send_spaceship_info(pplayer
, NULL
);
2230 /* Update city data. */
2231 if (city_refresh(pcity
)) {
2232 auto_arrange_workers(pcity
);
2236 /* Move to the next thing in the worklist */
2237 choose_build_target(pplayer
, pcity
);
2243 /**************************************************************************
2244 Build city units. Several units can be built in one turn if the effect
2245 City_Build_Slots is used.
2246 **************************************************************************/
2247 static bool city_build_unit(struct player
*pplayer
, struct city
*pcity
)
2249 struct unit_type
*utype
;
2250 struct worklist
*pwl
= &pcity
->worklist
;;
2251 int unit_shield_cost
, num_units
, i
;
2253 fc_assert_ret_val(pcity
->production
.kind
== VUT_UTYPE
, FALSE
);
2255 /* If the city has already bought a unit which is now obsolete, don't try
2256 * to upgrade the production. The new unit might require more shields, which
2257 * would be bad if it was bought to urgently defend a city. (Equally it
2258 * might be the same cost or cheaper, but tough; you hurried the unit so
2259 * you miss out on technological advances.) */
2260 if (city_can_change_build(pcity
)) {
2261 upgrade_unit_prod(pcity
);
2264 utype
= pcity
->production
.value
.utype
;
2265 unit_shield_cost
= utype_build_shield_cost(utype
);
2267 /* We must make a special case for barbarians here, because they are
2268 so dumb. Really. They don't know the prerequisite techs for units
2269 they build!! - Per */
2270 if (!can_city_build_unit_direct(pcity
, utype
)
2271 && !is_barbarian(pplayer
)) {
2272 notify_player(pplayer
, city_tile(pcity
), E_CITY_CANTBUILD
, ftc_server
,
2273 _("%s is building %s, which is no longer available."),
2274 city_link(pcity
), utype_name_translation(utype
));
2276 /* Log before signal emitting, so pointers are certainly valid */
2277 log_verbose("%s %s tried to build %s, which is not available.",
2278 nation_rule_name(nation_of_city(pcity
)),
2279 city_name_get(pcity
), utype_rule_name(utype
));
2280 script_server_signal_emit("unit_cant_be_built", 3,
2281 API_TYPE_UNIT_TYPE
, utype
,
2282 API_TYPE_CITY
, pcity
,
2283 API_TYPE_STRING
, "unavailable");
2287 if (pcity
->shield_stock
>= unit_shield_cost
) {
2288 int pop_cost
= utype_pop_value(utype
);
2290 int saved_city_id
= pcity
->id
;
2292 /* Should we disband the city? -- Massimo */
2293 if (city_size_get(pcity
) == pop_cost
2294 && is_city_option_set(pcity
, CITYO_DISBAND
)) {
2295 return !disband_city(pcity
);
2298 if (city_size_get(pcity
) <= pop_cost
) {
2299 notify_player(pplayer
, city_tile(pcity
), E_CITY_CANTBUILD
, ftc_server
,
2300 /* TRANS: city ... utype ... size ... pop_cost */
2301 _("%s can't build %s yet. "
2302 "(city size: %d, unit population cost: %d)"),
2303 city_link(pcity
), utype_name_translation(utype
),
2304 city_size_get(pcity
), pop_cost
);
2305 script_server_signal_emit("unit_cant_be_built", 3,
2306 API_TYPE_UNIT_TYPE
, utype
,
2307 API_TYPE_CITY
, pcity
,
2308 API_TYPE_STRING
, "pop_cost");
2312 fc_assert(pop_cost
== 0 || city_size_get(pcity
) >= pop_cost
);
2314 /* don't update turn_last_built if we returned above */
2315 pcity
->turn_last_built
= game
.info
.turn
;
2317 /* check if we can build more than one unit (effect City_Build_Slots) */
2318 (void) city_production_build_units(pcity
, FALSE
, &num_units
);
2320 /* We should be able to build at least one (by checks above) */
2321 fc_assert(num_units
>= 1);
2323 for (i
= 0; i
< num_units
; i
++) {
2324 punit
= create_unit(pplayer
, pcity
->tile
, utype
,
2325 do_make_unit_veteran(pcity
, utype
),
2327 pplayer
->score
.units_built
++;
2329 /* After we created the unit remove the citizen. This will also
2330 * rearrange the worker to take into account the extra resources
2333 city_reduce_size(pcity
, pop_cost
, NULL
, "unit_built");
2336 /* to eliminate micromanagement, we only subtract the unit's cost */
2337 pcity
->before_change_shields
-= unit_shield_cost
;
2338 pcity
->shield_stock
-= unit_shield_cost
;
2340 notify_player(pplayer
, city_tile(pcity
), E_UNIT_BUILT
, ftc_server
,
2341 /* TRANS: <city> is finished building <unit/building>. */
2342 _("%s is finished building %s."),
2343 city_link(pcity
), utype_name_translation(utype
));
2346 /* Additional message if the unit has population cost. */
2347 notify_player(pplayer
, city_tile(pcity
), E_UNIT_BUILT_POP_COST
,
2349 /* TRANS: "<unit> cost... <city> shrinks..."
2350 * Plural in "%d population", not "size %d". */
2351 PL_("%s cost %d population. %s shrinks to size %d.",
2352 "%s cost %d population. %s shrinks to size %d.",
2354 utype_name_translation(utype
), pop_cost
,
2355 city_link(pcity
), city_size_get(pcity
));
2358 script_server_signal_emit("unit_built", 2,
2359 API_TYPE_UNIT
, punit
,
2360 API_TYPE_CITY
, pcity
);
2362 /* check if the city still exists */
2363 if (!city_exist(saved_city_id
)) {
2367 if (i
!= 0 && worklist_length(pwl
) > 0) {
2368 /* remove the build unit from the worklist; it has to be one less
2369 * than units build to preserve the next build target from the
2371 worklist_remove(pwl
, 0);
2375 if (city_exist(saved_city_id
)) {
2376 /* Done building this unit; time to move on to the next. */
2377 choose_build_target(pplayer
, pcity
);
2384 /**************************************************************************
2385 Returns FALSE when the city is removed, TRUE otherwise.
2386 **************************************************************************/
2387 static bool city_build_stuff(struct player
*pplayer
, struct city
*pcity
)
2389 if (!city_distribute_surplus_shields(pplayer
, pcity
)) {
2393 nullify_caravan_and_disband_plus(pcity
);
2394 define_orig_production_values(pcity
);
2396 switch (pcity
->production
.kind
) {
2397 case VUT_IMPROVEMENT
:
2398 return city_build_building(pplayer
, pcity
);
2400 return city_build_unit(pplayer
, pcity
);
2402 /* must never happen! */
2409 /**************************************************************************
2410 Randomly sell a building from the given list. Returns TRUE if a building
2413 NB: It is assumed that gold upkeep for the buildings has already been
2414 paid this turn, hence when a building is sold its upkeep is given back
2416 NB: The contents of 'imprs' are usually mangled by this function.
2417 NB: It is assumed that all buildings in 'imprs' can be sold.
2418 **************************************************************************/
2419 static bool sell_random_building(struct player
*pplayer
,
2420 struct cityimpr_list
*imprs
)
2422 struct cityimpr
*pcityimpr
;
2425 fc_assert_ret_val(pplayer
!= NULL
, FALSE
);
2427 if (!imprs
|| cityimpr_list_size(imprs
) == 0) {
2431 r
= fc_rand(cityimpr_list_size(imprs
));
2432 pcityimpr
= cityimpr_list_get(imprs
, r
);
2434 notify_player(pplayer
, city_tile(pcityimpr
->pcity
), E_IMP_AUCTIONED
,
2436 _("Can't afford to maintain %s in %s, building sold!"),
2437 improvement_name_translation(pcityimpr
->pimprove
),
2438 city_link(pcityimpr
->pcity
));
2439 log_debug("%s: sold building (%s)", player_name(pplayer
),
2440 improvement_name_translation(pcityimpr
->pimprove
));
2442 do_sell_building(pplayer
, pcityimpr
->pcity
, pcityimpr
->pimprove
);
2444 cityimpr_list_remove(imprs
, pcityimpr
);
2446 /* Get back the gold upkeep that was already paid this turn. */
2447 pplayer
->economic
.gold
+= city_improvement_upkeep(pcityimpr
->pcity
,
2448 pcityimpr
->pimprove
);
2450 city_refresh_queue_add(pcityimpr
->pcity
);
2457 /**************************************************************************
2458 Randomly "sell" a unit from the given list. Returns pointer to sold unit.
2459 This pointer is not valid any more, but can be removed from the lists.
2461 NB: It is assumed that gold upkeep for the units has already been paid
2462 this turn, hence when a unit is "sold" its upkeep is given back to the
2464 NB: The contents of 'units' are usually mangled by this function.
2465 NB: It is assumed that all units in 'units' have positive gold upkeep.
2466 **************************************************************************/
2467 static struct unit
*sell_random_unit(struct player
*pplayer
,
2468 struct unit_list
*punitlist
)
2472 struct unit_list
*cargo
;
2474 fc_assert_ret_val(pplayer
!= NULL
, FALSE
);
2476 if (!punitlist
|| unit_list_size(punitlist
) == 0) {
2480 r
= fc_rand(unit_list_size(punitlist
));
2481 punit
= unit_list_get(punitlist
, r
);
2483 cargo
= unit_list_new();
2485 /* Check if unit is transporting other units from punitlist,
2486 * and sell one of those (recursively) instead. */
2487 unit_list_iterate(unit_transport_cargo(punit
), pcargo
) {
2488 if (pcargo
->upkeep
[O_GOLD
] > 0) { /* Optimization, do not iterate over punitlist
2489 * if we are sure that pcargo is not in it. */
2490 unit_list_iterate(punitlist
, p2
) {
2492 unit_list_append(cargo
, pcargo
);
2494 } unit_list_iterate_end
;
2496 } unit_list_iterate_end
;
2498 if (unit_list_size(cargo
) > 0) {
2499 struct unit
*ret
= sell_random_unit(pplayer
, cargo
);
2502 /* Remove from original list too */
2503 unit_list_remove(punitlist
, ret
);
2506 unit_list_destroy(cargo
);
2511 unit_list_destroy(cargo
);
2513 gold_upkeep
= punit
->upkeep
[O_GOLD
];
2515 /* All units in punitlist should have gold upkeep! */
2516 fc_assert_ret_val(gold_upkeep
> 0, NULL
);
2518 notify_player(pplayer
, unit_tile(punit
), E_UNIT_LOST_MISC
, ftc_server
,
2519 _("Not enough gold. %s disbanded."),
2520 unit_tile_link(punit
));
2521 log_debug("%s: unit sold (%s)", player_name(pplayer
),
2522 unit_name_translation(punit
));
2524 unit_list_remove(punitlist
, punit
);
2525 wipe_unit(punit
, ULR_SOLD
, NULL
);
2527 /* Get the upkeep gold back. */
2528 pplayer
->economic
.gold
+= gold_upkeep
;
2533 /**************************************************************************
2534 Balance the gold of a nation by selling some random units and buildings.
2535 **************************************************************************/
2536 static bool player_balance_treasury_units_and_buildings
2537 (struct player
*pplayer
)
2539 struct cityimpr_list
*pimprlist
;
2540 struct unit_list
*punitlist
;
2541 bool sell_unit
= TRUE
;
2547 pimprlist
= cityimpr_list_new();
2548 punitlist
= unit_list_new();
2550 city_list_iterate(pplayer
->cities
, pcity
) {
2551 city_built_iterate(pcity
, pimprove
) {
2552 if (can_city_sell_building(pcity
, pimprove
)) {
2553 struct cityimpr
*ci
= fc_malloc(sizeof(*ci
));
2556 ci
->pimprove
= pimprove
;
2557 cityimpr_list_append(pimprlist
, ci
);
2559 } city_built_iterate_end
;
2561 unit_list_iterate(pcity
->units_supported
, punit
) {
2562 if (punit
->upkeep
[O_GOLD
] > 0) {
2563 unit_list_append(punitlist
, punit
);
2565 } unit_list_iterate_end
;
2566 } city_list_iterate_end
;
2568 while (pplayer
->economic
.gold
< 0
2569 && (cityimpr_list_size(pimprlist
) > 0
2570 || unit_list_size(punitlist
) > 0)) {
2571 if ((!sell_unit
&& cityimpr_list_size(pimprlist
) > 0)
2572 || unit_list_size(punitlist
) == 0) {
2573 sell_random_building(pplayer
, pimprlist
);
2575 sell_random_unit(pplayer
, punitlist
);
2577 sell_unit
= !sell_unit
;
2580 /* Free remaining entries from list */
2581 cityimpr_list_iterate(pimprlist
, pimpr
) {
2583 } cityimpr_list_iterate_end
;
2585 if (pplayer
->economic
.gold
< 0) {
2586 /* If we get here it means the player has
2587 * negative gold. This should never happen. */
2588 fc_assert_msg(FALSE
, "Player %s (nb %d) cannot have negative gold!",
2589 player_name(pplayer
), player_number(pplayer
));
2592 cityimpr_list_destroy(pimprlist
);
2593 unit_list_destroy(punitlist
);
2595 return pplayer
->economic
.gold
>= 0;
2598 /**************************************************************************
2599 Balance the gold of a nation by selling some units which need gold upkeep.
2600 **************************************************************************/
2601 static bool player_balance_treasury_units(struct player
*pplayer
)
2603 struct unit_list
*punitlist
;
2609 punitlist
= unit_list_new();
2611 city_list_iterate(pplayer
->cities
, pcity
) {
2612 unit_list_iterate(pcity
->units_supported
, punit
) {
2613 if (punit
->upkeep
[O_GOLD
] > 0) {
2614 unit_list_append(punitlist
, punit
);
2616 } unit_list_iterate_end
;
2617 } city_list_iterate_end
;
2619 while (pplayer
->economic
.gold
< 0
2620 && sell_random_unit(pplayer
, punitlist
)) {
2621 /* all done in sell_random_unit() */
2624 if (pplayer
->economic
.gold
< 0) {
2625 /* If we get here it means the player has
2626 * negative gold. This should never happen. */
2627 fc_assert_msg(FALSE
, "Player %s (nb %d) cannot have negative gold!",
2628 player_name(pplayer
), player_number(pplayer
));
2631 unit_list_destroy(punitlist
);
2633 return pplayer
->economic
.gold
>= 0;
2636 /**************************************************************************
2637 Balance the gold of one city by randomly selling some buildings.
2638 **************************************************************************/
2639 static bool city_balance_treasury_buildings(struct city
*pcity
)
2641 struct player
*pplayer
;
2642 struct cityimpr_list
*pimprlist
;
2648 pplayer
= city_owner(pcity
);
2649 pimprlist
= cityimpr_list_new();
2651 /* Create a vector of all buildings that can be sold. */
2652 city_built_iterate(pcity
, pimprove
) {
2653 if (can_city_sell_building(pcity
, pimprove
)) {
2654 struct cityimpr
*ci
= fc_malloc(sizeof(*ci
));
2657 ci
->pimprove
= pimprove
;
2658 cityimpr_list_append(pimprlist
, ci
);
2660 } city_built_iterate_end
;
2662 /* Try to sell some buildings. */
2663 while (pplayer
->economic
.gold
< 0
2664 && sell_random_building(pplayer
, pimprlist
)) {
2665 /* all done in sell_random_building */
2668 /* Free remaining entries from list */
2669 cityimpr_list_iterate(pimprlist
, pimpr
) {
2671 } cityimpr_list_iterate_end
;
2673 cityimpr_list_destroy(pimprlist
);
2675 return pplayer
->economic
.gold
>= 0;
2678 /**************************************************************************
2679 Balance the gold of one city by randomly selling some units which need
2682 NB: This function adds the gold upkeep of disbanded units back to the
2683 player's gold. Hence it assumes that this gold was previously taken
2684 from the player (i.e. in update_city_activity()).
2685 **************************************************************************/
2686 static bool city_balance_treasury_units(struct city
*pcity
)
2688 struct player
*pplayer
;
2689 struct unit_list
*punitlist
;
2695 pplayer
= city_owner(pcity
);
2696 punitlist
= unit_list_new();
2698 /* Create a vector of all supported units with gold upkeep. */
2699 unit_list_iterate(pcity
->units_supported
, punit
) {
2700 if (punit
->upkeep
[O_GOLD
] > 0) {
2701 unit_list_append(punitlist
, punit
);
2703 } unit_list_iterate_end
;
2705 /* Still not enough gold, so try "selling" some units. */
2706 while (pplayer
->economic
.gold
< 0
2707 && sell_random_unit(pplayer
, punitlist
)) {
2708 /* all done in sell_random_unit() */
2711 /* If we get here the player has negative gold, but hopefully
2712 * another city will be able to pay the deficit, so continue. */
2714 unit_list_destroy(punitlist
);
2716 return pplayer
->economic
.gold
>= 0;
2719 /**************************************************************************
2720 Add some Pollution if we have waste
2721 **************************************************************************/
2722 static bool place_pollution(struct city
*pcity
, enum extra_cause cause
)
2725 struct tile
*pcenter
= city_tile(pcity
);
2726 int city_radius_sq
= city_map_radius_sq_get(pcity
);
2730 /* place pollution on a random city tile */
2732 int tile_id
= fc_rand(city_map_tiles(city_radius_sq
));
2733 struct extra_type
*pextra
;
2735 city_tile_index_to_xy(&cx
, &cy
, tile_id
, city_radius_sq
);
2737 /* check for a a real map position */
2738 if (!(ptile
= city_map_to_tile(pcenter
, city_radius_sq
, cx
, cy
))) {
2742 pextra
= rand_extra_for_tile(ptile
, cause
);
2744 if (pextra
!= NULL
&& !tile_has_extra(ptile
, pextra
)) {
2745 tile_add_extra(ptile
, pextra
);
2746 update_tile_knowledge(ptile
);
2752 log_debug("pollution not placed: city: %s", city_name_get(pcity
));
2757 /**************************************************************************
2758 Add some Pollution if we have waste
2759 **************************************************************************/
2760 static void check_pollution(struct city
*pcity
)
2762 if (pcity
->pollution
!= 0 && fc_rand(100) <= pcity
->pollution
) {
2763 if (place_pollution(pcity
, EC_POLLUTION
)) {
2764 notify_player(city_owner(pcity
), city_tile(pcity
), E_POLLUTION
, ftc_server
,
2765 _("Pollution near %s."), city_link(pcity
));
2770 /**************************************************************************
2771 Returns the cost to incite a city. This depends on the size of the city,
2772 the number of happy, unhappy and angry citizens, whether it is
2773 celebrating, how close it is to the capital, how many units it has and
2774 upkeeps, presence of courthouse, its buildings and wonders, and who
2775 originally built it.
2776 **************************************************************************/
2777 int city_incite_cost(struct player
*pplayer
, struct city
*pcity
)
2779 struct city
*capital
;
2781 double cost
; /* Intermediate values can get very large */
2784 cost
= city_owner(pcity
)->economic
.gold
+ game
.server
.base_incite_cost
;
2786 unit_list_iterate(pcity
->tile
->units
, punit
) {
2787 cost
+= (unit_build_shield_cost(punit
)
2788 * game
.server
.incite_unit_factor
);
2789 } unit_list_iterate_end
;
2792 city_built_iterate(pcity
, pimprove
) {
2793 cost
+= impr_build_shield_cost(pimprove
)
2794 * game
.server
.incite_improvement_factor
;
2795 } city_built_iterate_end
;
2797 /* Stability bonuses */
2798 if (!city_unhappy(pcity
)) {
2801 if (city_celebrating(pcity
)) {
2805 /* Buy back is cheap, conquered cities are also cheap */
2806 if (!game
.info
.citizen_nationality
) {
2807 if (city_owner(pcity
) != pcity
->original
) {
2808 if (pplayer
== pcity
->original
) {
2809 cost
/= 2; /* buy back: 50% price reduction */
2811 cost
= cost
* 2 / 3; /* buy conquered: 33% price reduction */
2816 /* Distance from capital */
2817 capital
= player_capital(city_owner(pcity
));
2819 int tmp
= map_distance(capital
->tile
, pcity
->tile
);
2820 dist
= MIN(32, tmp
);
2822 /* No capital? Take max penalty! */
2826 size
= MAX(1, city_size_get(pcity
)
2827 + pcity
->feel
[CITIZEN_HAPPY
][FEELING_FINAL
]
2828 - pcity
->feel
[CITIZEN_UNHAPPY
][FEELING_FINAL
]
2829 - pcity
->feel
[CITIZEN_ANGRY
][FEELING_FINAL
] * 3);
2831 cost
*= game
.server
.incite_total_factor
;
2832 cost
= cost
/ (dist
+ 3);
2834 if (game
.info
.citizen_nationality
) {
2835 int cost_per_citizen
= cost
/ pcity
->size
;
2836 int natives
= citizens_nation_get(pcity
, city_owner(pcity
)->slot
);
2837 int tgt_cit
= citizens_nation_get(pcity
, pplayer
->slot
);
2838 int third_party
= pcity
->size
- natives
- tgt_cit
;
2840 cost
= cost_per_citizen
* (natives
+ 0.7 * third_party
+ 0.5 * tgt_cit
);
2843 cost
+= (cost
* get_city_bonus(pcity
, EFT_INCITE_COST_PCT
)) / 100;
2846 if (cost
>= INCITE_IMPOSSIBLE_COST
) {
2847 return INCITE_IMPOSSIBLE_COST
;
2853 /**************************************************************************
2854 Called every turn, at beginning of turn, for every city.
2855 **************************************************************************/
2856 static void define_orig_production_values(struct city
*pcity
)
2858 /* Remember what this city is building last turn, so that on the next turn
2859 * the player can switch production to something else and then change it
2860 * back without penalty. This has to be updated _before_ production for
2861 * this turn is calculated, so that the penalty will apply if the player
2862 * changes production away from what has just been completed. This makes
2863 * sense if you consider what this value means: all the shields in the
2864 * city have been dedicated toward the project that was chosen last turn,
2865 * so the player shouldn't be penalized if the governor has to pick
2866 * something different. See city_change_production_penalty(). */
2867 pcity
->changed_from
= pcity
->production
;
2869 log_debug("In %s, building %s. Beg of Turn shields = %d",
2870 city_name_get(pcity
), universal_rule_name(&pcity
->changed_from
),
2871 pcity
->before_change_shields
);
2874 /**************************************************************************
2875 Let the advisor set up city building target.
2876 **************************************************************************/
2877 static void nullify_caravan_and_disband_plus(struct city
*pcity
)
2879 pcity
->disbanded_shields
=0;
2880 pcity
->caravan_shields
=0;
2883 /**************************************************************************
2884 Initialize all variables containing information about production
2885 before it was changed.
2886 **************************************************************************/
2887 void nullify_prechange_production(struct city
*pcity
)
2889 nullify_caravan_and_disband_plus(pcity
);
2890 pcity
->before_change_shields
=0;
2893 /**************************************************************************
2894 Called every turn, at end of turn, for every city.
2895 **************************************************************************/
2896 static void update_city_activity(struct city
*pcity
)
2898 struct player
*pplayer
;
2899 struct government
*gov
;
2905 pplayer
= city_owner(pcity
);
2906 gov
= government_of_city(pcity
);
2908 if (city_refresh(pcity
)) {
2909 auto_arrange_workers(pcity
);
2912 /* Reporting of celebrations rewritten, copying the treatment of disorder below,
2913 with the added rapture rounds count. 991219 -- Jing */
2914 if (city_build_stuff(pplayer
, pcity
)) {
2916 int revolution_turns
;
2918 pcity
->history
+= city_history_gain(pcity
);
2920 /* History can decrease, but never go below zero */
2921 pcity
->history
= MAX(pcity
->history
, 0);
2923 if (city_celebrating(pcity
)) {
2925 if (pcity
->rapture
== 1) {
2926 notify_player(pplayer
, city_tile(pcity
), E_CITY_LOVE
, ftc_server
,
2927 _("Celebrations in your honor in %s."),
2931 if (pcity
->rapture
!= 0) {
2932 notify_player(pplayer
, city_tile(pcity
), E_CITY_NORMAL
, ftc_server
,
2933 _("Celebrations canceled in %s."),
2938 pcity
->was_happy
= city_happy(pcity
);
2940 /* Handle the illness. */
2941 if (game
.info
.illness_on
) {
2942 /* recalculate city illness; illness due to trade has to be saved
2943 * within the city struct as the client has not all data to
2945 pcity
->server
.illness
2946 = city_illness_calc(pcity
, NULL
, NULL
, &(pcity
->illness_trade
), NULL
);
2948 if (city_illness_check(pcity
)) {
2949 notify_player(pplayer
, city_tile(pcity
), E_CITY_PLAGUE
, ftc_server
,
2950 _("%s has been struck by a plague! Population lost!"),
2952 city_reduce_size(pcity
, 1, NULL
, "plague");
2953 pcity
->turn_plague
= game
.info
.turn
;
2955 /* recalculate illness */
2956 pcity
->server
.illness
2957 = city_illness_calc(pcity
, NULL
, NULL
, &(pcity
->illness_trade
),
2962 /* City population updated here, after the rapture stuff above. --Jing */
2963 saved_id
= pcity
->id
;
2964 city_populate(pcity
, pplayer
);
2965 if (NULL
== player_city_by_number(pplayer
, saved_id
)) {
2969 pcity
->did_sell
= FALSE
;
2970 pcity
->did_buy
= FALSE
;
2971 pcity
->airlift
= city_airlift_max(pcity
);
2972 update_bulbs(pplayer
, pcity
->prod
[O_SCIENCE
], FALSE
);
2974 /* Update the treasury. */
2975 pplayer
->economic
.gold
+= pcity
->prod
[O_GOLD
];
2976 pplayer
->economic
.gold
-= city_total_impr_gold_upkeep(pcity
);
2977 pplayer
->economic
.gold
-= city_total_unit_gold_upkeep(pcity
);
2979 if (pplayer
->economic
.gold
< 0) {
2980 /* Not enough gold - we have to sell some buildings, and if that
2981 * is not enough, disband units with gold upkeep, taking into
2982 * account the setting of 'game.info.gold_upkeep_style':
2983 * GOLD_UPKEEP_CITY: Cities pay for buildings and units.
2984 * GOLD_UPKEEP_MIXED: Cities pay only for buildings; the nation pays
2986 * GOLD_UPKEEP_NATION: The nation pays for buildings and units. */
2987 switch (game
.info
.gold_upkeep_style
) {
2988 case GOLD_UPKEEP_CITY
:
2989 case GOLD_UPKEEP_MIXED
:
2990 if (!city_balance_treasury_buildings(pcity
)
2991 && game
.info
.gold_upkeep_style
== GOLD_UPKEEP_CITY
) {
2992 city_balance_treasury_units(pcity
);
2995 case GOLD_UPKEEP_NATION
:
3000 revolution_turns
= get_city_bonus(pcity
, EFT_REVOLUTION_UNHAPPINESS
);
3001 if (city_unhappy(pcity
)) {
3002 const char *revomsg
;
3005 if (pcity
->anarchy
== revolution_turns
) {
3006 /* Revolution next turn if not dealt with */
3007 /* TRANS: preserve leading space; this string will be appended to
3008 * another sentence */
3009 revomsg
= _(" Unrest threatens to spread beyond the city.");
3013 if (pcity
->anarchy
== 1) {
3014 notify_player(pplayer
, city_tile(pcity
), E_CITY_DISORDER
, ftc_server
,
3015 /* TRANS: second %s is an optional extra sentence */
3016 _("Civil disorder in %s.%s"),
3017 city_link(pcity
), revomsg
);
3019 notify_player(pplayer
, city_tile(pcity
), E_CITY_DISORDER
, ftc_server
,
3020 /* TRANS: second %s is an optional extra sentence */
3021 _("CIVIL DISORDER CONTINUES in %s.%s"),
3022 city_link(pcity
), revomsg
);
3025 if (pcity
->anarchy
!= 0) {
3026 notify_player(pplayer
, city_tile(pcity
), E_CITY_NORMAL
, ftc_server
,
3027 _("Order restored in %s."),
3032 check_pollution(pcity
);
3034 send_city_info(NULL
, pcity
);
3036 if (revolution_turns
> 0 && pcity
->anarchy
> revolution_turns
) {
3037 notify_player(pplayer
, city_tile(pcity
), E_ANARCHY
, ftc_server
,
3038 /* TRANS: %s - government form, e.g., Democracy */
3039 _("The people have overthrown your %s, "
3040 "your country is in turmoil."),
3041 government_name_translation(gov
));
3042 handle_player_change_government(pplayer
, government_number(gov
));
3044 if (city_refresh(pcity
)) {
3045 auto_arrange_workers(pcity
);
3047 sanity_check_city(pcity
);
3051 /*****************************************************************************
3052 check if city suffers from a plague. Return TRUE if it does, FALSE if not.
3053 ****************************************************************************/
3054 static bool city_illness_check(const struct city
* pcity
)
3056 if (fc_rand(1000) < pcity
->server
.illness
) {
3063 /**************************************************************************
3064 Disband a city into the built unit, supported by the closest city.
3065 **************************************************************************/
3066 static bool disband_city(struct city
*pcity
)
3068 struct player
*pplayer
= city_owner(pcity
);
3069 struct tile
*ptile
= pcity
->tile
;
3070 struct city
*rcity
=NULL
;
3071 struct unit_type
*utype
= pcity
->production
.value
.utype
;
3072 int saved_id
= pcity
->id
;
3074 /* find closest city other than pcity */
3075 rcity
= find_closest_city(ptile
, pcity
, pplayer
, FALSE
, FALSE
, FALSE
, TRUE
,
3079 /* What should we do when we try to disband our only city? */
3080 notify_player(pplayer
, ptile
, E_CITY_CANTBUILD
, ftc_server
,
3081 _("%s can't build %s yet, "
3082 "and we can't disband our only city."),
3083 city_link(pcity
), utype_name_translation(utype
));
3084 script_server_signal_emit("unit_cant_be_built", 3,
3085 API_TYPE_UNIT_TYPE
, utype
,
3086 API_TYPE_CITY
, pcity
,
3087 API_TYPE_STRING
, "pop_cost");
3088 if (!city_exist(saved_id
)) {
3089 /* Script decided to remove even the last city */
3096 (void) create_unit(pplayer
, ptile
, utype
,
3097 do_make_unit_veteran(pcity
, utype
),
3099 pplayer
->score
.units_built
++;
3101 /* Shift all the units supported by pcity (including the new unit)
3102 * to rcity. transfer_city_units does not make sure no units are
3103 * left floating without a transport, but since all units are
3104 * transferred this is not a problem. */
3105 transfer_city_units(pplayer
, pplayer
, pcity
->units_supported
, rcity
,
3108 notify_player(pplayer
, ptile
, E_UNIT_BUILT
, ftc_server
,
3109 /* TRANS: "<city> is disbanded into Settler." */
3110 _("%s is disbanded into %s."),
3111 city_tile_link(pcity
), utype_name_translation(utype
));
3113 script_server_signal_emit("city_destroyed", 3,
3114 API_TYPE_CITY
, pcity
,
3115 API_TYPE_PLAYER
, pcity
->owner
,
3116 API_TYPE_PLAYER
, NULL
);
3122 /***************************************************************************
3123 Helper function to calculate a "score" of a city. The score is used to get
3124 an estimate of the "migration desirability" of the city. The higher the
3125 score the more likely citizens will migrate to it.
3127 The score depends on the city size, the feeling of its citizens, the cost
3128 of all buildings in the city, and the surplus of trade, luxury and
3132 score = ([city size] + feeling) * factors
3134 * feeling of the citizens
3135 feeling = 1.00 * happy citizens
3136 + 0.00 * content citizens
3137 - 0.25 * unhappy citizens
3138 - 0.50 * angry citizens
3141 * the build costs of all buildings
3142 f = (1 + (1 - exp(-[build shield cost]/1000))/5)
3143 * the trade of the city
3144 f = (1 + (1 - exp(-[city surplus trade]/100))/5)
3145 * the luxury within the city
3146 f = (1 + (1 - exp(-[city surplus luxury]/100))/5)
3147 * the science within the city
3148 f = (1 + (1 - exp(-[city surplus science]/100))/5)
3150 all factors f have values between 1 and 1.2; the overall factor will be
3151 between 1.0 (smaller cities) and 2.0 (bigger cities)
3153 [build shield cost], [city surplus trade], [city surplus luxury] and
3154 [city surplus science] _must_ be >= 0!
3156 The food furplus is considered by an additional factor
3158 * the food surplus of the city
3159 f = (1 + [city surplus food; interval -10..20]/10)
3161 The health factor is defined as:
3163 * the health of the city
3164 f = (100 - [city illness; tenth of %]/25)
3166 * if the city has at least one wonder a factor of 1.25 is added
3167 * for the capital an additional factor of 1.25 is used
3168 * the score is also modified by the effect EFT_MIGRATION_PCT
3169 **************************************************************************/
3170 static float city_migration_score(struct city
*pcity
)
3173 int build_shield_cost
= 0;
3174 bool has_wonder
= FALSE
;
3180 if (pcity
->server
.mgr_score_calc_turn
== game
.info
.turn
) {
3181 /* up-to-date migration score */
3182 return pcity
->server
.migration_score
;
3185 /* feeling of the citizens */
3186 score
= (city_size_get(pcity
)
3187 + 1.00 * pcity
->feel
[CITIZEN_HAPPY
][FEELING_FINAL
]
3188 + 0.00 * pcity
->feel
[CITIZEN_CONTENT
][FEELING_FINAL
]
3189 - 0.25 * pcity
->feel
[CITIZEN_UNHAPPY
][FEELING_FINAL
]
3190 - 0.50 * pcity
->feel
[CITIZEN_ANGRY
][FEELING_FINAL
]);
3192 /* calculate shield build cost for all buildings */
3193 city_built_iterate(pcity
, pimprove
) {
3194 build_shield_cost
+= impr_build_shield_cost(pimprove
);
3195 if (is_wonder(pimprove
)) {
3196 /* this city has a wonder */
3199 } city_built_iterate_end
;
3201 /* take shield costs of all buidings into account; normalized by 1000 */
3202 score
*= (1 + (1 - exp(- (float) MAX(0, build_shield_cost
) / 1000)) / 5);
3203 /* take trade into account; normalized by 100 */
3204 score
*= (1 + (1 - exp(- (float) MAX(0, pcity
->surplus
[O_TRADE
]) / 100))
3206 /* take luxury into account; normalized by 100 */
3207 score
*= (1 + (1 - exp(- (float) MAX(0, pcity
->surplus
[O_LUXURY
]) / 100))
3209 /* take science into account; normalized by 100 */
3210 score
*= (1 + (1 - exp(- (float) MAX(0, pcity
->surplus
[O_SCIENCE
]) / 100))
3213 score
+= city_culture(pcity
) * game
.info
.culture_migration_pml
/ 1000;
3215 /* Take food into account; the food surplus is clipped to values between
3216 * -10..20 and normalize by 10. Thus, the factor is between 0.9 and 1.2. */
3217 score
*= (1 + (float) CLIP(-10, pcity
->surplus
[O_FOOD
], 20) / 10 );
3219 /* Reduce the score due to city illness (plague). The illness is given in
3220 * tenth of percent (0..1000) and normalized by 25. Thus, this factor is
3221 * between 0.6 (ill city) and 1.0 (health city). */
3222 score
*= (100 - (float)city_illness_calc(pcity
, NULL
, NULL
, NULL
, NULL
)
3226 /* people like wonders */
3230 if (is_capital(pcity
)) {
3231 /* the capital is a magnet for the citizens */
3235 /* take into account effects */
3236 score
*= (1.0 + get_city_bonus(pcity
, EFT_MIGRATION_PCT
) / 100.0);
3238 log_debug("[M] %s score: %.3f", city_name_get(pcity
), score
);
3240 /* set migration score for the city */
3241 pcity
->server
.migration_score
= score
;
3242 /* set the turn, when the score was calculated */
3243 pcity
->server
.mgr_score_calc_turn
= game
.info
.turn
;
3248 /**************************************************************************
3249 Do the migrations between the cities that overlap, if the growth of the
3250 target city is not blocked due to a missing improvement or missing food.
3252 Returns TRUE if migration occurred.
3253 **************************************************************************/
3254 static bool do_city_migration(struct city
*pcity_from
,
3255 struct city
*pcity_to
)
3257 struct player
*pplayer_from
, *pplayer_to
, *pplayer_citizen
;
3258 struct tile
*ptile_from
, *ptile_to
;
3259 char name_from
[MAX_LEN_LINK
], name_to
[MAX_LEN_LINK
];
3260 const char *nation_from
, *nation_to
;
3261 struct city
*rcity
= NULL
;
3263 if (!pcity_from
|| !pcity_to
) {
3267 pplayer_from
= city_owner(pcity_from
);
3268 pplayer_citizen
= pplayer_from
;
3269 pplayer_to
= city_owner(pcity_to
);
3270 /* We copy that, because city_link always returns the same pointer. */
3271 sz_strlcpy(name_from
, city_link(pcity_from
));
3272 sz_strlcpy(name_to
, city_link(pcity_to
));
3273 nation_from
= nation_adjective_for_player(pplayer_from
);
3274 nation_to
= nation_adjective_for_player(pplayer_to
);
3275 ptile_from
= city_tile(pcity_from
);
3276 ptile_to
= city_tile(pcity_to
);
3278 /* check food supply in the receiver city */
3279 if (game
.server
.mgr_foodneeded
) {
3280 bool migration
= FALSE
;
3282 if (pcity_to
->surplus
[O_FOOD
] >= game
.info
.food_cost
) {
3285 /* check if there is a free tile for the new citizen which, when worked,
3286 * leads to zero or positive food surplus for the enlarged city */
3287 int max_food_tile
= -1; /* no free tile */
3288 city_tile_iterate(city_map_radius_sq_get(pcity_to
),
3289 city_tile(pcity_to
), ptile
) {
3290 if (city_can_work_tile(pcity_to
, ptile
)
3291 && tile_worked(ptile
) != pcity_to
) {
3292 /* Safest assumption is that city won't be celebrating once an
3293 * additional citizen is added */
3294 max_food_tile
= MAX(max_food_tile
,
3295 city_tile_output(pcity_to
, ptile
, FALSE
, O_FOOD
));
3297 } city_tile_iterate_end
;
3298 if (max_food_tile
>= 0
3299 && pcity_to
->surplus
[O_FOOD
] + max_food_tile
>= game
.info
.food_cost
) {
3305 /* insufficiency food in receiver city; no additional citizens */
3306 if (pplayer_from
== pplayer_to
) {
3307 /* migration between one nation */
3308 notify_player(pplayer_to
, ptile_to
, E_CITY_TRANSFER
, ftc_server
,
3309 /* TRANS: From <city1> to <city2>. */
3310 _("Migrants from %s can't go to %s because there is "
3311 "not enough food available!"),
3312 name_from
, name_to
);
3314 /* migration between different nations */
3315 notify_player(pplayer_from
, ptile_to
, E_CITY_TRANSFER
, ftc_server
,
3316 /* TRANS: From <city1> to <city2> (<city2 nation adjective>). */
3317 _("Migrants from %s can't go to %s (%s) because there "
3318 "is not enough food available!"),
3319 name_from
, name_to
, nation_to
);
3320 notify_player(pplayer_to
, ptile_to
, E_CITY_TRANSFER
, ftc_server
,
3321 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
3322 _("Migrants from %s (%s) can't go to %s because there "
3323 "is not enough food available!"),
3324 name_from
, nation_from
, name_to
);
3331 if (!city_can_grow_to(pcity_to
, city_size_get(pcity_to
) + 1)) {
3332 /* receiver city can't grow */
3333 if (pplayer_from
== pplayer_to
) {
3334 /* migration between one nation */
3335 notify_player(pplayer_to
, ptile_to
, E_CITY_TRANSFER
, ftc_server
,
3336 /* TRANS: From <city1> to <city2>. */
3337 _("Migrants from %s can't go to %s because it needs "
3338 "an improvement to grow!"),
3339 name_from
, name_to
);
3341 /* migration between different nations */
3342 notify_player(pplayer_from
, ptile_to
, E_CITY_TRANSFER
, ftc_server
,
3343 /* TRANS: From <city1> to <city2> of <city2 nation adjective>. */
3344 _("Migrants from %s can't go to %s (%s) because it "
3345 "needs an improvement to grow!"),
3346 name_from
, name_to
, nation_to
);
3347 notify_player(pplayer_to
, ptile_to
, E_CITY_TRANSFER
, ftc_server
,
3348 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
3349 _("Migrants from %s (%s) can't go to %s because it "
3350 "needs an improvement to grow!"),
3351 name_from
, nation_from
, name_to
);
3357 /* reduce size of giver */
3358 if (city_size_get(pcity_from
) == 1) {
3360 if (game
.info
.citizen_nationality
) {
3361 /* Preserve nationality of city's only citizen */
3362 pplayer_citizen
= player_slot_get_player(citizens_random(pcity_from
));
3365 /* do not destroy wonders */
3366 city_built_iterate(pcity_from
, pimprove
) {
3367 if (is_wonder(pimprove
)) {
3370 } city_built_iterate_end
;
3372 /* find closest city other of the same player than pcity_from */
3373 rcity
= find_closest_city(ptile_from
, pcity_from
, pplayer_from
, FALSE
,
3374 FALSE
, FALSE
, TRUE
, FALSE
, NULL
);
3377 /* transfer all units to the closest city */
3378 transfer_city_units(pplayer_from
, pplayer_from
,
3379 pcity_from
->units_supported
, rcity
, pcity_from
,
3381 sz_strlcpy(name_from
, city_tile_link(pcity_from
));
3383 script_server_signal_emit("city_size_change", 3,
3384 API_TYPE_CITY
, pcity_from
,
3386 API_TYPE_STRING
, "migration_from");
3387 script_server_signal_emit("city_destroyed", 3,
3388 API_TYPE_CITY
, pcity_from
,
3389 API_TYPE_PLAYER
, pcity_from
->owner
,
3390 API_TYPE_PLAYER
, NULL
);
3392 remove_city(pcity_from
);
3394 notify_player(pplayer_from
, ptile_from
, E_CITY_LOST
, ftc_server
,
3395 _("%s was disbanded by its citizens."),
3398 /* it's the only city of the nation */
3402 /* the migrants take half of the food box with them (this prevents
3403 * migration -> grow -> migration -> ... cycles) */
3404 pcity_from
->food_stock
/= 2;
3406 if (game
.info
.citizen_nationality
) {
3407 /* Those citizens that are from the target nation are most
3408 * ones migrating. */
3409 if (citizens_nation_get(pcity_from
, pplayer_to
->slot
) > 0) {
3410 pplayer_citizen
= pplayer_to
;
3411 } else if (!citizens_nation_get(pcity_from
, pplayer_citizen
->slot
)) {
3412 /* No native citizens at all in the city, choose random foreigner */
3413 struct player_slot
*cit_slot
= citizens_random(pcity_from
);
3415 pplayer_citizen
= player_slot_get_player(cit_slot
);
3417 /* This should be followed by city_reduce_size(). */
3418 citizens_nation_add(pcity_from
, pplayer_citizen
->slot
, -1);
3420 city_reduce_size(pcity_from
, 1, pplayer_from
, "migration_from");
3421 city_refresh_vision(pcity_from
);
3422 if (city_refresh(pcity_from
)) {
3423 auto_arrange_workers(pcity_from
);
3427 /* This should be _before_ the size of the city is increased. Thus, the
3428 * order of the messages is correct (1: migration; 2: increased size). */
3429 if (pplayer_from
== pplayer_to
) {
3430 /* migration between one nation */
3431 notify_player(pplayer_from
, ptile_to
, E_CITY_TRANSFER
, ftc_server
,
3432 /* TRANS: From <city1> to <city2>. */
3433 _("Migrants from %s moved to %s in search of a better "
3434 "life."), name_from
, name_to
);
3436 /* migration between different nations */
3437 notify_player(pplayer_from
, ptile_to
, E_CITY_TRANSFER
, ftc_server
,
3438 /* TRANS: From <city1> to <city2> (<city2 nation adjective>). */
3439 _("Migrants from %s moved to %s (%s) in search of a "
3441 name_from
, name_to
, nation_to
);
3442 notify_player(pplayer_to
, ptile_to
, E_CITY_TRANSFER
, ftc_server
,
3443 /* TRANS: From <city1> (<city1 nation adjective>) to <city2>. */
3444 _("Migrants from %s (%s) moved to %s in search of a "
3446 name_from
, nation_from
, name_to
);
3449 /* raise size of receiver city */
3450 city_increase_size(pcity_to
, pplayer_citizen
);
3451 city_refresh_vision(pcity_to
);
3452 if (city_refresh(pcity_to
)) {
3453 auto_arrange_workers(pcity_to
);
3455 script_server_signal_emit("city_size_change", 3,
3456 API_TYPE_CITY
, pcity_to
,
3458 API_TYPE_STRING
, "migration_to");
3460 log_debug("[M] T%d migration successful (%s -> %s)",
3461 game
.info
.turn
, name_from
, name_to
);
3466 /**************************************************************************
3467 Check for citizens who want to migrate between the cities that overlap.
3468 Migrants go to the city with higher score, if the growth of the target
3469 city is not blocked due to a missing improvement.
3471 The following setting are used:
3473 'game.server.mgr_turninterval' controls the number of turns between
3474 migration checks for one city (counted from the founding). If this
3475 setting is zero, or it is the first turn (T0), migration does no occur.
3477 'game.server.mgr_distance' is the maximal distance for migration.
3479 'game.server.mgr_nationchance' gives the chance for migration within one
3482 'game.server.mgr_worldchance' gives the chance for migration between all
3485 Returns TRUE iff there has been INTERNATIONAL migration.
3486 **************************************************************************/
3487 bool check_city_migrations(void)
3489 bool internat
= FALSE
;
3491 if (!game
.server
.migration
) {
3495 if (game
.server
.mgr_turninterval
<= 0
3496 || (game
.server
.mgr_worldchance
<= 0
3497 && game
.server
.mgr_nationchance
<= 0)) {
3501 /* check for migration */
3502 players_iterate(pplayer
) {
3503 if (!pplayer
->cities
) {
3507 if (check_city_migrations_player(pplayer
)) {
3510 } players_iterate_end
;
3515 /**************************************************************************
3516 Disaster has hit a city. Apply its effects.
3517 **************************************************************************/
3518 static void apply_disaster(struct city
*pcity
, struct disaster_type
*pdis
)
3520 struct player
*pplayer
= city_owner(pcity
);
3521 struct tile
*ptile
= city_tile(pcity
);
3522 bool had_internal_effect
= FALSE
;
3524 log_debug("%s at %s", disaster_rule_name(pdis
), city_name_get(pcity
));
3526 notify_player(pplayer
, ptile
, E_DISASTER
,
3528 /* TRANS: Disasters such as Earthquake */
3529 _("%s was hit by %s."), city_name_get(pcity
),
3530 disaster_name_translation(pdis
));
3532 if (disaster_has_effect(pdis
, DE_POLLUTION
)) {
3533 if (place_pollution(pcity
, EC_POLLUTION
)) {
3534 notify_player(pplayer
, ptile
, E_DISASTER
, ftc_server
,
3535 _("Pollution near %s."), city_link(pcity
));
3536 had_internal_effect
= TRUE
;
3540 if (disaster_has_effect(pdis
, DE_FALLOUT
)) {
3541 if (place_pollution(pcity
, EC_FALLOUT
)) {
3542 notify_player(pplayer
, ptile
, E_DISASTER
, ftc_server
,
3543 _("Fallout near %s."), city_link(pcity
));
3544 had_internal_effect
= TRUE
;
3548 if (disaster_has_effect(pdis
, DE_REDUCE_DESTROY
)
3549 || (disaster_has_effect(pdis
, DE_REDUCE_POP
)
3550 && pcity
->size
> 1)) {
3551 if (!city_reduce_size(pcity
, 1, NULL
, "disaster")) {
3552 notify_player(pplayer
, ptile
, E_DISASTER
, ftc_server
,
3553 /* TRANS: "Industrial Accident destroys Bogota entirely." */
3554 _("%s destroys %s entirely."),
3555 disaster_name_translation(pdis
), city_link(pcity
));
3558 notify_player(pplayer
, ptile
, E_DISASTER
, ftc_server
,
3559 /* TRANS: "Nuclear Accident ... Montreal." */
3560 _("%s causes population loss in %s."),
3561 disaster_name_translation(pdis
), city_link(pcity
));
3564 had_internal_effect
= TRUE
;
3567 if (pcity
&& disaster_has_effect(pdis
, DE_DESTROY_BUILDING
)) {
3569 struct impr_type
*imprs
[B_LAST
];
3571 city_built_iterate(pcity
, pimprove
) {
3572 if (is_improvement(pimprove
)) {
3573 imprs
[total
++] = pimprove
;
3575 } city_built_iterate_end
;
3578 int num
= fc_rand(total
);
3580 building_lost(pcity
, imprs
[num
]);
3582 notify_player(pplayer
, ptile
, E_DISASTER
, ftc_server
,
3583 /* TRANS: second %s is the name of a city improvement */
3584 _("%s destroys %s in %s."),
3585 disaster_name_translation(pdis
),
3586 improvement_name_translation(imprs
[num
]),
3589 had_internal_effect
= TRUE
;
3593 if (pcity
&& disaster_has_effect(pdis
, DE_EMPTY_FOODSTOCK
)) {
3594 if (pcity
->food_stock
> 0) {
3595 pcity
->food_stock
= 0;
3597 notify_player(pplayer
, ptile
, E_DISASTER
, ftc_server
,
3598 /* TRANS: %s is a city name */
3599 _("All stored food destroyed in %s."), city_link(pcity
));
3601 had_internal_effect
= TRUE
;
3605 if (pcity
&& disaster_has_effect(pdis
, DE_EMPTY_PRODSTOCK
)) {
3606 if (pcity
->shield_stock
> 0) {
3609 pcity
->shield_stock
= 0;
3610 nullify_prechange_production(pcity
); /* Make it impossible to recover */
3612 universal_name_translation(&pcity
->production
, prod
, sizeof(prod
));
3613 notify_player(pplayer
, ptile
, E_DISASTER
, ftc_server
,
3614 /* TRANS: "Production of Colossus in Rhodes destroyed." */
3615 _("Production of %s in %s destroyed."),
3616 prod
, city_link(pcity
));
3618 had_internal_effect
= TRUE
;
3622 script_server_signal_emit("disaster_occurred", 3,
3623 API_TYPE_DISASTER
, pdis
,
3624 API_TYPE_CITY
, pcity
,
3625 API_TYPE_BOOL
, had_internal_effect
);
3626 script_server_signal_emit("disaster", 2,
3627 API_TYPE_DISASTER
, pdis
,
3628 API_TYPE_CITY
, pcity
);
3631 /**************************************************************************
3632 Check for any disasters hitting any city, and apply those disasters.
3633 **************************************************************************/
3634 void check_disasters(void)
3636 if (game
.info
.disasters
== 0) {
3637 /* Shortcut out as no disaster is possible. */
3641 players_iterate(pplayer
) {
3642 /* Safe city iterator needed as disaster may destroy city */
3643 city_list_iterate_safe(pplayer
->cities
, pcity
) {
3646 disaster_type_iterate(pdis
) {
3647 if (city_exist(id
)) {
3648 /* City survived earlier disasters. */
3649 int probability
= game
.info
.disasters
* pdis
->frequency
;
3650 int result
= fc_rand(DISASTER_BASE_RARITY
);
3652 if (result
< probability
) {
3653 if (can_disaster_happen(pdis
, pcity
)) {
3654 apply_disaster(pcity
, pdis
);
3658 } disaster_type_iterate_end
;
3659 } city_list_iterate_safe_end
;
3660 } players_iterate_end
;
3663 /**************************************************************************
3664 Check for migration for each city of one player.
3666 For each city of the player do:
3667 * check each tile within GAME_MAX_MGR_DISTANCE for a city
3668 * if a city is found check the distance
3669 * compare the migration score
3670 **************************************************************************/
3671 static bool check_city_migrations_player(const struct player
*pplayer
)
3673 char city_link_text
[MAX_LEN_LINK
];
3674 float best_city_player_score
, best_city_world_score
;
3675 struct city
*best_city_player
, *best_city_world
, *acity
;
3676 float score_from
, score_tmp
, weight
;
3678 bool internat
= FALSE
;
3680 /* check for each city
3681 * city_list_iterate_safe_end must be used because we could
3682 * remove one city from the list */
3683 city_list_iterate_safe(pplayer
->cities
, pcity
) {
3684 /* no migration out of the capital */
3685 if (is_capital(pcity
)) {
3689 /* check only each (game.server.mgr_turninterval) turn
3690 * (counted from the funding turn) and do not migrate
3691 * the same turn a city is founded */
3692 if (game
.info
.turn
== pcity
->turn_founded
3693 || ((game
.info
.turn
- pcity
->turn_founded
)
3694 % game
.server
.mgr_turninterval
) != 0) {
3698 best_city_player_score
= 0.0;
3699 best_city_world_score
= 0.0;
3700 best_city_player
= NULL
;
3701 best_city_world
= NULL
;
3703 /* score of the actual city
3704 * taking into account a persistence factor of 3 */
3705 score_from
= city_migration_score(pcity
) * 3;
3707 log_debug("[M] T%d check city: %s score: %6.3f (%s)",
3708 game
.info
.turn
, city_name_get(pcity
), score_from
,
3709 player_name(pplayer
));
3711 /* consider all cities within the maximal possible distance
3712 * (= CITY_MAP_MAX_RADIUS + GAME_MAX_MGR_DISTANCE) */
3713 iterate_outward(city_tile(pcity
), CITY_MAP_MAX_RADIUS
3714 + GAME_MAX_MGR_DISTANCE
, ptile
) {
3715 acity
= tile_city(ptile
);
3717 if (!acity
|| acity
== pcity
) {
3718 /* no city or the city in the center */
3722 /* Calculate the migration distance. The value of
3723 * game.server.mgr_distance is added to the current city radius. If the
3724 * distance between both cities is lower or equal than this value,
3725 * migration is possible. */
3726 mgr_dist
= (int)sqrt((double)MAX(city_map_radius_sq_get(acity
),0))
3727 + game
.server
.mgr_distance
;
3729 /* distance between the two cities */
3730 dist
= real_map_distance(city_tile(pcity
), city_tile(acity
));
3732 if (dist
> mgr_dist
) {
3737 /* score of the second city, weighted by the distance */
3738 weight
= ((float) (mgr_dist
+ 1 - dist
) / (float) (mgr_dist
+ 1));
3739 score_tmp
= city_migration_score(acity
) * weight
;
3741 log_debug("[M] T%d - compare city: %s (%s) dist: %d mgr_dist: %d "
3742 "score: %6.3f", game
.info
.turn
, city_name_get(acity
),
3743 player_name(city_owner(acity
)), dist
, mgr_dist
, score_tmp
);
3745 if (game
.server
.mgr_nationchance
> 0 && city_owner(acity
) == pplayer
) {
3746 /* migration between cities of the same owner */
3747 if (score_tmp
> score_from
&& score_tmp
> best_city_player_score
) {
3748 /* select the best! */
3749 best_city_player_score
= score_tmp
;
3750 best_city_player
= acity
;
3752 log_debug("[M] T%d - best city (player): %s (%s) score: "
3753 "%6.3f (> %6.3f)", game
.info
.turn
,
3754 city_name_get(best_city_player
), player_name(pplayer
),
3755 best_city_player_score
, score_from
);
3757 } else if (game
.server
.mgr_worldchance
> 0
3758 && city_owner(acity
) != pplayer
) {
3759 /* migration between cities of different owners */
3760 if (game
.info
.citizen_nationality
) {
3761 /* Modify the score if citizens could migrate to a city of their
3762 * original nation. */
3763 if (citizens_nation_get(pcity
, city_owner(acity
)->slot
) > 0) {
3768 if (score_tmp
> score_from
&& score_tmp
> best_city_world_score
) {
3769 /* select the best! */
3770 best_city_world_score
= score_tmp
;
3771 best_city_world
= acity
;
3773 log_debug("[M] T%d - best city (world): %s (%s) score: "
3774 "%6.3f (> %6.3f)", game
.info
.turn
,
3775 city_name_get(best_city_world
),
3776 player_name(city_owner(best_city_world
)),
3777 best_city_world_score
, score_from
);
3780 } iterate_outward_end
;
3782 if (best_city_player_score
> 0) {
3783 /* first, do the migration within one nation */
3784 if (fc_rand(100) >= game
.server
.mgr_nationchance
) {
3786 /* N.B.: city_link always returns the same pointer. */
3787 sz_strlcpy(city_link_text
, city_link(pcity
));
3788 notify_player(pplayer
, city_tile(pcity
), E_CITY_TRANSFER
, ftc_server
,
3789 _("Citizens of %s are thinking about migrating to %s "
3790 "for a better life."),
3791 city_link_text
, city_link(best_city_player
));
3793 do_city_migration(pcity
, best_city_player
);
3800 if (best_city_world_score
> 0) {
3801 /* second, do the migration between all nations */
3802 if (fc_rand(100) >= game
.server
.mgr_worldchance
) {
3805 nname
= nation_adjective_for_player(city_owner(best_city_world
));
3807 /* N.B.: city_link always returns the same pointer. */
3808 sz_strlcpy(city_link_text
, city_link(pcity
));
3809 notify_player(pplayer
, city_tile(pcity
), E_CITY_TRANSFER
, ftc_server
,
3810 /* TRANS: <city1> to <city2> (<city2 nation adjective>). */
3811 _("Citizens of %s are thinking about migrating to %s "
3812 "(%s) for a better life."),
3813 city_link_text
, city_link(best_city_world
), nname
);
3815 do_city_migration(pcity
, best_city_world
);
3822 } city_list_iterate_safe_end
;
3827 /**************************************************************************
3828 Recheck and store style of the city.
3829 **************************************************************************/
3830 void city_style_refresh(struct city
*pcity
)
3832 pcity
->style
= city_style(pcity
);