Stop sharing requirement_unit_state_ereq().
[freeciv.git] / common / improvement.c
bloba1c7742d394785e0342a60f408db8fc4df5fc248
1 /***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
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 ***********************************************************************/
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
18 /* utility */
19 #include "fcintl.h"
20 #include "log.h"
21 #include "mem.h"
22 #include "shared.h" /* ARRAY_SIZE */
23 #include "string_vector.h"
24 #include "support.h"
26 /* common */
27 #include "game.h"
28 #include "map.h"
29 #include "tech.h"
30 #include "victory.h"
32 #include "improvement.h"
34 /**************************************************************************
35 All the city improvements:
36 Use improvement_by_number(id) to access the array.
37 The improvement_types array is now setup in:
38 server/ruleset.c (for the server)
39 client/packhand.c (for the client)
40 **************************************************************************/
41 static struct impr_type improvement_types[B_LAST];
43 /****************************************************************************
44 Initialize building structures.
45 ****************************************************************************/
46 void improvements_init(void)
48 int i;
50 /* Can't use improvement_iterate() or improvement_by_number() here
51 * because num_impr_types isn't known yet. */
52 for (i = 0; i < ARRAY_SIZE(improvement_types); i++) {
53 struct impr_type *p = &improvement_types[i];
55 p->item_number = i;
56 requirement_vector_init(&p->reqs);
57 requirement_vector_init(&p->obsolete_by);
58 p->disabled = FALSE;
62 /**************************************************************************
63 Frees the memory associated with this improvement.
64 **************************************************************************/
65 static void improvement_free(struct impr_type *p)
67 if (NULL != p->helptext) {
68 strvec_destroy(p->helptext);
69 p->helptext = NULL;
72 requirement_vector_free(&p->reqs);
73 requirement_vector_free(&p->obsolete_by);
76 /***************************************************************
77 Frees the memory associated with all improvements.
78 ***************************************************************/
79 void improvements_free()
81 improvement_iterate(p) {
82 improvement_free(p);
83 } improvement_iterate_end;
86 /**************************************************************************
87 Cache features of the improvement
88 **************************************************************************/
89 void improvement_feature_cache_init(void)
91 improvement_iterate(pimprove) {
92 pimprove->allows_units = FALSE;
93 unit_type_iterate(putype) {
94 if (putype->need_improvement == pimprove) {
95 pimprove->allows_units = TRUE;
96 break;
98 } unit_type_iterate_end;
100 pimprove->allows_extras = FALSE;
101 extra_type_iterate(pextra) {
102 if (requirement_needs_improvement(pimprove, &pextra->reqs)) {
103 pimprove->allows_extras = TRUE;
104 break;
106 } extra_type_iterate_end;
108 pimprove->prevents_disaster = FALSE;
109 disaster_type_iterate(pdis) {
110 if (!requirement_fulfilled_by_improvement(pimprove, &pdis->reqs)) {
111 pimprove->prevents_disaster = TRUE;
112 break;
114 } disaster_type_iterate_end;
116 pimprove->protects_vs_actions = FALSE;
117 action_enablers_iterate(act) {
118 if (!requirement_fulfilled_by_improvement(pimprove,
119 &act->target_reqs)) {
120 pimprove->protects_vs_actions = TRUE;
121 break;
123 } action_enablers_iterate_end;
125 } improvement_iterate_end;
128 /**************************************************************************
129 Return the first item of improvements.
130 **************************************************************************/
131 struct impr_type *improvement_array_first(void)
133 if (game.control.num_impr_types > 0) {
134 return improvement_types;
136 return NULL;
139 /**************************************************************************
140 Return the last item of improvements.
141 **************************************************************************/
142 const struct impr_type *improvement_array_last(void)
144 if (game.control.num_impr_types > 0) {
145 return &improvement_types[game.control.num_impr_types - 1];
147 return NULL;
150 /**************************************************************************
151 Return the number of improvements.
152 **************************************************************************/
153 Impr_type_id improvement_count(void)
155 return game.control.num_impr_types;
158 /**************************************************************************
159 Return the improvement index.
161 Currently same as improvement_number(), paired with improvement_count()
162 indicates use as an array index.
163 **************************************************************************/
164 Impr_type_id improvement_index(const struct impr_type *pimprove)
166 fc_assert_ret_val(NULL != pimprove, -1);
167 return pimprove - improvement_types;
170 /**************************************************************************
171 Return the improvement index.
172 **************************************************************************/
173 Impr_type_id improvement_number(const struct impr_type *pimprove)
175 fc_assert_ret_val(NULL != pimprove, -1);
176 return pimprove->item_number;
179 /**************************************************************************
180 Returns the improvement type for the given index/ID. Returns NULL for
181 an out-of-range index.
182 **************************************************************************/
183 struct impr_type *improvement_by_number(const Impr_type_id id)
185 if (id < 0 || id >= improvement_count()) {
186 return NULL;
188 return &improvement_types[id];
191 /**************************************************************************
192 Returns pointer when the improvement_type "exists" in this game,
193 returns NULL otherwise.
195 An improvement_type doesn't exist for any of:
196 - the improvement_type has been flagged as removed by setting its
197 tech_req to A_LAST; [was not in current 2007-07-27]
198 - it is a space part, and the spacerace is not enabled.
199 **************************************************************************/
200 struct impr_type *valid_improvement(struct impr_type *pimprove)
202 if (NULL == pimprove) {
203 return NULL;
206 if (!victory_enabled(VC_SPACERACE)
207 && (building_has_effect(pimprove, EFT_SS_STRUCTURAL)
208 || building_has_effect(pimprove, EFT_SS_COMPONENT)
209 || building_has_effect(pimprove, EFT_SS_MODULE))) {
210 /* This assumes that space parts don't have any other effects. */
211 return NULL;
214 return pimprove;
217 /**************************************************************************
218 Returns pointer when the improvement_type "exists" in this game,
219 returns NULL otherwise.
221 In addition to valid_improvement(), tests for id is out of range.
222 **************************************************************************/
223 struct impr_type *valid_improvement_by_number(const Impr_type_id id)
225 return valid_improvement(improvement_by_number(id));
228 /**************************************************************************
229 Return the (translated) name of the given improvement.
230 You don't have to free the return pointer.
231 **************************************************************************/
232 const char *improvement_name_translation(const struct impr_type *pimprove)
234 return name_translation_get(&pimprove->name);
237 /****************************************************************************
238 Return the (untranslated) rule name of the improvement.
239 You don't have to free the return pointer.
240 ****************************************************************************/
241 const char *improvement_rule_name(const struct impr_type *pimprove)
243 return rule_name_get(&pimprove->name);
246 /****************************************************************************
247 Returns the number of shields it takes to build this improvement.
248 ****************************************************************************/
249 int impr_build_shield_cost(const struct impr_type *pimprove)
251 int base = pimprove->build_cost;
253 return MAX(base * game.info.shieldbox / 100, 1);
256 /****************************************************************************
257 Returns the amount of gold it takes to rush this improvement.
258 ****************************************************************************/
259 int impr_buy_gold_cost(const struct impr_type *pimprove, int shields_in_stock)
261 int cost = 0;
262 const int missing = impr_build_shield_cost(pimprove) - shields_in_stock;
264 if (improvement_has_flag(pimprove, IF_GOLD)) {
265 /* Can't buy capitalization. */
266 return 0;
269 if (missing > 0) {
270 cost = 2 * missing;
273 if (is_wonder(pimprove)) {
274 cost *= 2;
276 if (shields_in_stock == 0) {
277 cost *= 2;
279 return cost;
282 /****************************************************************************
283 Returns the amount of gold received when this improvement is sold.
284 ****************************************************************************/
285 int impr_sell_gold(const struct impr_type *pimprove)
287 return impr_build_shield_cost(pimprove);
290 /**************************************************************************
291 Returns whether improvement is some kind of wonder. Both great wonders
292 and small wonders count.
293 **************************************************************************/
294 bool is_wonder(const struct impr_type *pimprove)
296 return (is_great_wonder(pimprove) || is_small_wonder(pimprove));
299 /**************************************************************************
300 Does a linear search of improvement_types[].name.translated
301 Returns NULL when none match.
302 **************************************************************************/
303 struct impr_type *improvement_by_translated_name(const char *name)
305 improvement_iterate(pimprove) {
306 if (0 == strcmp(improvement_name_translation(pimprove), name)) {
307 return pimprove;
309 } improvement_iterate_end;
311 return NULL;
314 /****************************************************************************
315 Does a linear search of improvement_types[].name.vernacular
316 Returns NULL when none match.
317 ****************************************************************************/
318 struct impr_type *improvement_by_rule_name(const char *name)
320 const char *qname = Qn_(name);
322 improvement_iterate(pimprove) {
323 if (0 == fc_strcasecmp(improvement_rule_name(pimprove), qname)) {
324 return pimprove;
326 } improvement_iterate_end;
328 return NULL;
331 /**************************************************************************
332 Return TRUE if the impr has this flag otherwise FALSE
333 **************************************************************************/
334 bool improvement_has_flag(const struct impr_type *pimprove,
335 enum impr_flag_id flag)
337 fc_assert_ret_val(impr_flag_id_is_valid(flag), FALSE);
338 return BV_ISSET(pimprove->flags, flag);
341 /**************************************************************************
342 Return TRUE if the improvement should be visible to others without spying
343 **************************************************************************/
344 bool is_improvement_visible(const struct impr_type *pimprove)
346 return (is_wonder(pimprove)
347 || improvement_has_flag(pimprove, IF_VISIBLE_BY_OTHERS));
350 /**************************************************************************
351 Return TRUE if the improvement can ever go obsolete.
352 Can be used for buildings or wonders.
353 **************************************************************************/
354 bool can_improvement_go_obsolete(const struct impr_type *pimprove)
356 return requirement_vector_size(&pimprove->obsolete_by) > 0;
359 /**************************************************************************
360 Returns TRUE if the improvement or wonder is obsolete
361 **************************************************************************/
362 bool improvement_obsolete(const struct player *pplayer,
363 const struct impr_type *pimprove,
364 const struct city *pcity)
366 struct tile *ptile = NULL;
368 if (pcity != NULL) {
369 ptile = city_tile(pcity);
372 requirement_vector_iterate(&pimprove->obsolete_by, preq) {
373 if (is_req_active(pplayer, NULL, pcity, pimprove, ptile, NULL, NULL,
374 NULL, NULL, NULL, preq, RPT_CERTAIN)) {
375 return TRUE;
377 } requirement_vector_iterate_end;
379 return FALSE;
382 /**************************************************************************
383 Returns TRUE iff improvement provides units buildable in city
384 **************************************************************************/
385 static bool impr_provides_buildable_units(const struct city *pcity,
386 struct impr_type *pimprove)
388 /* Fast check */
389 if (!pimprove->allows_units) {
390 return FALSE;
393 unit_type_iterate(ut) {
394 if (ut->need_improvement == pimprove
395 && can_city_build_unit_now(pcity, ut)) {
396 return TRUE;
398 } unit_type_iterate_end;
400 return FALSE;
403 /**************************************************************************
404 Returns TRUE iff improvement provides extras buildable in city
405 **************************************************************************/
406 static bool impr_provides_buildable_extras(const struct city *pcity,
407 struct impr_type *pimprove)
410 /* Fast check */
411 if (!pimprove->allows_extras) {
412 return FALSE;
415 extra_type_iterate(pextra) {
416 if (requirement_needs_improvement(pimprove, &pextra->reqs)) {
417 city_tile_iterate(city_map_radius_sq_get(pcity),
418 city_tile(pcity), ptile) {
419 if (player_can_build_extra(pextra, city_owner(pcity), ptile)) {
420 return TRUE;
422 } city_tile_iterate_end;
424 } extra_type_iterate_end;
426 return FALSE;
429 /**************************************************************************
430 Returns TRUE iff improvement prevents a disaster in city
431 **************************************************************************/
432 static bool impr_prevents_disaster(const struct city *pcity,
433 struct impr_type *pimprove)
435 /* Fast check */
436 if (!pimprove->prevents_disaster) {
437 return FALSE;
440 disaster_type_iterate(pdis) {
441 if (!requirement_fulfilled_by_improvement(pimprove, &pdis->reqs)
442 && !can_disaster_happen(pdis, pcity)) {
443 return TRUE;
445 } disaster_type_iterate_end;
447 return FALSE;
450 /**************************************************************************
451 Returns TRUE iff improvement protects against an action on the city
452 FIXME: This is prone to false positives: for example, if one requires
453 a special tech or unit to perform an action, and no other player
454 has or can gain that tech or unit, protection is still claimed.
455 **************************************************************************/
456 static bool impr_protects_vs_actions(const struct city *pcity,
457 struct impr_type *pimprove)
459 /* Fast check */
460 if (!pimprove->protects_vs_actions) {
461 return FALSE;
464 action_enablers_iterate(act) {
465 if (!requirement_fulfilled_by_improvement(pimprove, &act->target_reqs)
466 && !is_action_possible_on_city(act->action, NULL, pcity)) {
467 return TRUE;
469 } action_enablers_iterate_end;
471 return FALSE;
475 /**************************************************************************
476 Check if an improvement has side effects for a city. Side effects
477 are any benefits that accrue that are not tracked by the effects
478 system.
480 Note that this function will always return FALSE if the improvement does
481 not currently provide a benefit to the city (for example, if the improvement
482 has not yet been built, or another city benefits from the improvement in
483 this city (i.e. Wonders)).
484 **************************************************************************/
485 static bool improvement_has_side_effects(const struct city *pcity,
486 struct impr_type *pimprove)
488 /* FIXME: There should probably also be a test as to whether
489 * the improvement *enables* an action (somewhere else),
490 * but this is hard to determine at city scope. */
492 return (impr_provides_buildable_units(pcity, pimprove)
493 || impr_provides_buildable_extras(pcity, pimprove)
494 || impr_prevents_disaster(pcity, pimprove)
495 || impr_protects_vs_actions(pcity, pimprove));
498 /**************************************************************************
499 Returns TRUE iff improvement provides some effect (good or bad).
500 **************************************************************************/
501 static bool improvement_has_effects(const struct city *pcity,
502 struct impr_type *pimprove)
504 struct universal source = { .kind = VUT_IMPROVEMENT,
505 .value = { .building = pimprove } };
506 struct effect_list *plist = get_req_source_effects(&source);
508 if (!plist || improvement_obsolete(city_owner(pcity), pimprove, pcity)) {
509 return FALSE;
512 effect_list_iterate(plist, peffect) {
513 if (0 != get_potential_improvement_bonus(pimprove, pcity,
514 peffect->type, RPT_CERTAIN)) {
515 return TRUE;
517 } effect_list_iterate_end;
519 return FALSE;
522 /**************************************************************************
523 Returns TRUE if an improvement in a city is productive, in some way.
525 Note that unproductive improvements may become productive later, if
526 appropriate conditions are met (e.g. a special building that isn't
527 producing units under the current government might under another).
528 **************************************************************************/
529 bool is_improvement_productive(const struct city *pcity,
530 struct impr_type *pimprove)
532 return (!improvement_obsolete(city_owner(pcity), pimprove, pcity)
533 && (improvement_has_flag(pimprove, IF_GOLD)
534 || improvement_has_side_effects(pcity, pimprove)
535 || improvement_has_effects(pcity, pimprove)));
538 /**************************************************************************
539 Returns TRUE if an improvement in a city is redundant, that is, the
540 city wouldn't lose anything by losing the improvement, or gain anything
541 by building it. This means:
542 - all of its effects (if any) are provided by other means, or it's
543 obsolete (and thus assumed to have no effect); and
544 - it's not enabling the city to build some kind of units; and
545 - it's not Coinage (IF_GOLD).
546 (Note that it's not impossible that this improvement could become useful
547 if circumstances changed, say if a new government enabled the building
548 of its special units.)
549 **************************************************************************/
550 bool is_improvement_redundant(const struct city *pcity,
551 struct impr_type *pimprove)
553 /* A capitalization production is never redundant. */
554 if (improvement_has_flag(pimprove, IF_GOLD)) {
555 return FALSE;
558 /* If an improvement has side effects, don't claim it's redundant. */
559 if (improvement_has_side_effects(pcity, pimprove)) {
560 return FALSE;
563 /* Otherwise, it's redundant if its effects are available by other means,
564 * or if it's an obsolete wonder (great or small). */
565 return is_building_replaced(pcity, pimprove, RPT_CERTAIN)
566 || improvement_obsolete(city_owner(pcity), pimprove, pcity);
569 /**************************************************************************
570 Whether player can build given building somewhere, ignoring whether it
571 is obsolete.
572 **************************************************************************/
573 bool can_player_build_improvement_direct(const struct player *p,
574 struct impr_type *pimprove)
576 bool space_part = FALSE;
578 if (!valid_improvement(pimprove)) {
579 return FALSE;
582 requirement_vector_iterate(&pimprove->reqs, preq) {
583 if (preq->range >= REQ_RANGE_PLAYER
584 && !is_req_active(p, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
585 NULL, NULL, preq, RPT_CERTAIN)) {
586 return FALSE;
588 } requirement_vector_iterate_end;
590 /* Check for space part construction. This assumes that space parts have
591 * no other effects. */
592 if (building_has_effect(pimprove, EFT_SS_STRUCTURAL)) {
593 space_part = TRUE;
594 if (p->spaceship.structurals >= NUM_SS_STRUCTURALS) {
595 return FALSE;
598 if (building_has_effect(pimprove, EFT_SS_COMPONENT)) {
599 space_part = TRUE;
600 if (p->spaceship.components >= NUM_SS_COMPONENTS) {
601 return FALSE;
604 if (building_has_effect(pimprove, EFT_SS_MODULE)) {
605 space_part = TRUE;
606 if (p->spaceship.modules >= NUM_SS_MODULES) {
607 return FALSE;
610 if (space_part &&
611 (get_player_bonus(p, EFT_ENABLE_SPACE) <= 0
612 || p->spaceship.state >= SSHIP_LAUNCHED)) {
613 return FALSE;
616 if (is_great_wonder(pimprove)) {
617 /* Can't build wonder if already built */
618 if (!great_wonder_is_available(pimprove)) {
619 return FALSE;
623 return TRUE;
626 /**************************************************************************
627 Whether player can build given building somewhere immediately.
628 Returns FALSE if building is obsolete.
629 **************************************************************************/
630 bool can_player_build_improvement_now(const struct player *p,
631 struct impr_type *pimprove)
633 if (!can_player_build_improvement_direct(p, pimprove)) {
634 return FALSE;
636 if (improvement_obsolete(p, pimprove, NULL)) {
637 return FALSE;
639 return TRUE;
642 /**************************************************************************
643 Whether player can _eventually_ build given building somewhere -- i.e.,
644 returns TRUE if building is available with current tech OR will be
645 available with future tech. Returns FALSE if building is obsolete.
646 **************************************************************************/
647 bool can_player_build_improvement_later(const struct player *p,
648 struct impr_type *pimprove)
650 if (!valid_improvement(pimprove)) {
651 return FALSE;
653 if (improvement_obsolete(p, pimprove, NULL)) {
654 return FALSE;
656 if (is_great_wonder(pimprove) && !great_wonder_is_available(pimprove)) {
657 /* Can't build wonder if already built */
658 return FALSE;
661 /* Check for requirements that aren't met and that are unchanging (so
662 * they can never be met). */
663 requirement_vector_iterate(&pimprove->reqs, preq) {
664 if (preq->range >= REQ_RANGE_PLAYER
665 && is_req_unchanging(preq)
666 && !is_req_active(p, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
667 NULL, NULL, preq, RPT_POSSIBLE)) {
668 return FALSE;
670 } requirement_vector_iterate_end;
671 /* FIXME: should check some "unchanging" reqs here - like if there's
672 * a nation requirement, we can go ahead and check it now. */
674 return TRUE;
677 /**************************************************************************
678 Is this building a great wonder?
679 **************************************************************************/
680 bool is_great_wonder(const struct impr_type *pimprove)
682 return (pimprove->genus == IG_GREAT_WONDER);
685 /**************************************************************************
686 Is this building a small wonder?
687 **************************************************************************/
688 bool is_small_wonder(const struct impr_type *pimprove)
690 return (pimprove->genus == IG_SMALL_WONDER);
693 /**************************************************************************
694 Is this building a regular improvement?
695 **************************************************************************/
696 bool is_improvement(const struct impr_type *pimprove)
698 return (pimprove->genus == IG_IMPROVEMENT);
701 /**************************************************************************
702 Returns TRUE if this is a "special" improvement. For example, spaceship
703 parts and coinage in the default ruleset are considered special.
704 **************************************************************************/
705 bool is_special_improvement(const struct impr_type *pimprove)
707 return pimprove->genus == IG_SPECIAL;
710 /**************************************************************************
711 Build a wonder in the city.
712 **************************************************************************/
713 void wonder_built(const struct city *pcity, const struct impr_type *pimprove)
715 struct player *pplayer;
716 int windex = improvement_number(pimprove);
718 fc_assert_ret(NULL != pcity);
719 fc_assert_ret(is_wonder(pimprove));
721 pplayer = city_owner(pcity);
722 pplayer->wonders[windex] = pcity->id;
724 if (is_great_wonder(pimprove)) {
725 game.info.great_wonder_owners[windex] = player_number(pplayer);
729 /**************************************************************************
730 Remove a wonder from a city and destroy it if it's a great wonder. To
731 transfer a great wonder, use great_wonder_transfer.
732 **************************************************************************/
733 void wonder_destroyed(const struct city *pcity,
734 const struct impr_type *pimprove)
736 struct player *pplayer;
737 int windex = improvement_number(pimprove);
739 fc_assert_ret(NULL != pcity);
740 fc_assert_ret(is_wonder(pimprove));
742 pplayer = city_owner(pcity);
743 fc_assert_ret(pplayer->wonders[windex] == pcity->id);
744 pplayer->wonders[windex] = WONDER_LOST;
746 if (is_great_wonder(pimprove)) {
747 fc_assert_ret(game.info.great_wonder_owners[windex]
748 == player_number(pplayer));
749 game.info.great_wonder_owners[windex] = WONDER_DESTROYED;
753 /**************************************************************************
754 Returns whether the player has lost this wonder after having owned it
755 (small or great).
756 **************************************************************************/
757 bool wonder_is_lost(const struct player *pplayer,
758 const struct impr_type *pimprove)
760 fc_assert_ret_val(NULL != pplayer, FALSE);
761 fc_assert_ret_val(is_wonder(pimprove), FALSE);
763 return pplayer->wonders[improvement_index(pimprove)] == WONDER_LOST;
766 /**************************************************************************
767 Returns whether the player is currently in possession of this wonder
768 (small or great).
769 **************************************************************************/
770 bool wonder_is_built(const struct player *pplayer,
771 const struct impr_type *pimprove)
773 fc_assert_ret_val(NULL != pplayer, FALSE);
774 fc_assert_ret_val(is_wonder(pimprove), FALSE);
776 return WONDER_BUILT(pplayer->wonders[improvement_index(pimprove)]);
779 /**************************************************************************
780 Get the world city with this wonder (small or great). This doesn't
781 always succeed on the client side, and even when it does, it may
782 return an "invisible" city whose members are unexpectedly NULL;
783 take care.
784 **************************************************************************/
785 struct city *city_from_wonder(const struct player *pplayer,
786 const struct impr_type *pimprove)
788 int city_id = pplayer->wonders[improvement_index(pimprove)];
790 fc_assert_ret_val(NULL != pplayer, NULL);
791 fc_assert_ret_val(is_wonder(pimprove), NULL);
793 if (!WONDER_BUILT(city_id)) {
794 return NULL;
797 #ifdef FREECIV_DEBUG
798 if (is_server()) {
799 /* On client side, this info is not always known. */
800 struct city *pcity = player_city_by_number(pplayer, city_id);
802 if (NULL == pcity) {
803 log_error("Player %s (nb %d) has outdated wonder info for "
804 "%s (nb %d), it points to city nb %d.",
805 player_name(pplayer), player_number(pplayer),
806 improvement_rule_name(pimprove),
807 improvement_number(pimprove), city_id);
808 } else if (!city_has_building(pcity, pimprove)) {
809 log_error("Player %s (nb %d) has outdated wonder info for "
810 "%s (nb %d), the city %s (nb %d) doesn't have this wonder.",
811 player_name(pplayer), player_number(pplayer),
812 improvement_rule_name(pimprove),
813 improvement_number(pimprove), city_name_get(pcity), pcity->id);
814 return NULL;
817 return pcity;
819 #endif /* FREECIV_DEBUG */
821 return player_city_by_number(pplayer, city_id);
824 /**************************************************************************
825 Returns whether this wonder is currently built.
826 **************************************************************************/
827 bool great_wonder_is_built(const struct impr_type *pimprove)
829 fc_assert_ret_val(is_great_wonder(pimprove), FALSE);
831 return WONDER_OWNED(game.info.great_wonder_owners
832 [improvement_index(pimprove)]);
835 /**************************************************************************
836 Returns whether this wonder has been destroyed.
837 **************************************************************************/
838 bool great_wonder_is_destroyed(const struct impr_type *pimprove)
840 fc_assert_ret_val(is_great_wonder(pimprove), FALSE);
842 return (WONDER_DESTROYED
843 == game.info.great_wonder_owners[improvement_index(pimprove)]);
846 /**************************************************************************
847 Returns whether this wonder can be currently built.
848 **************************************************************************/
849 bool great_wonder_is_available(const struct impr_type *pimprove)
851 fc_assert_ret_val(is_great_wonder(pimprove), FALSE);
853 return (WONDER_NOT_OWNED
854 == game.info.great_wonder_owners[improvement_index(pimprove)]);
857 /**************************************************************************
858 Get the world city with this great wonder. This doesn't always success
859 on the client side.
860 **************************************************************************/
861 struct city *city_from_great_wonder(const struct impr_type *pimprove)
863 int player_id = game.info.great_wonder_owners[improvement_index(pimprove)];
865 fc_assert_ret_val(is_great_wonder(pimprove), NULL);
867 if (WONDER_OWNED(player_id)) {
868 #ifdef FREECIV_DEBUG
869 const struct player *pplayer = player_by_number(player_id);
870 struct city *pcity = city_from_wonder(pplayer, pimprove);
872 if (is_server() && NULL == pcity) {
873 log_error("Game has outdated wonder info for %s (nb %d), "
874 "the player %s (nb %d) doesn't have this wonder.",
875 improvement_rule_name(pimprove),
876 improvement_number(pimprove),
877 player_name(pplayer), player_number(pplayer));
880 return pcity;
881 #else
882 return city_from_wonder(player_by_number(player_id), pimprove);
883 #endif /* FREECIV_DEBUG */
884 } else {
885 return NULL;
889 /**************************************************************************
890 Get the player owning this small wonder. This doesn't always success on
891 the client side.
892 **************************************************************************/
893 struct player *great_wonder_owner(const struct impr_type *pimprove)
895 int player_id = game.info.great_wonder_owners[improvement_index(pimprove)];
897 fc_assert_ret_val(is_great_wonder(pimprove), NULL);
899 if (WONDER_OWNED(player_id)) {
900 return player_by_number(player_id);
901 } else {
902 return NULL;
906 /**************************************************************************
907 Returns whether the player has built this small wonder.
908 **************************************************************************/
909 bool small_wonder_is_built(const struct player *pplayer,
910 const struct impr_type *pimprove)
912 fc_assert_ret_val(is_small_wonder(pimprove), FALSE);
914 return (NULL != pplayer
915 && wonder_is_built(pplayer, pimprove));
918 /**************************************************************************
919 Get the player city with this small wonder.
920 **************************************************************************/
921 struct city *city_from_small_wonder(const struct player *pplayer,
922 const struct impr_type *pimprove)
924 fc_assert_ret_val(is_small_wonder(pimprove), NULL);
926 if (NULL == pplayer) {
927 return NULL; /* Used in some places in the client. */
928 } else {
929 return city_from_wonder(pplayer, pimprove);
933 /**************************************************************************
934 Return TRUE iff the improvement can be sold.
935 **************************************************************************/
936 bool can_sell_building(struct impr_type *pimprove)
938 return (valid_improvement(pimprove) && is_improvement(pimprove));
941 /****************************************************************************
942 Return TRUE iff the city can sell the given improvement.
943 ****************************************************************************/
944 bool can_city_sell_building(const struct city *pcity,
945 struct impr_type *pimprove)
947 return (city_has_building(pcity, pimprove) && is_improvement(pimprove));
950 /****************************************************************************
951 Return TRUE iff the player can sell the given improvement from city.
952 If pimprove is NULL, returns iff city could sell some building type (this
953 does not check if such building is in this city)
954 ****************************************************************************/
955 enum test_result test_player_sell_building_now(struct player *pplayer,
956 struct city *pcity,
957 struct impr_type *pimprove)
959 /* Check if player can sell anything from this city */
960 if (pcity->owner != pplayer) {
961 return TR_OTHER_FAILURE;
964 if (pcity->did_sell) {
965 return TR_ALREADY_SOLD;
968 /* Check if particular building can be solt */
969 if (pimprove != NULL
970 && !can_city_sell_building(pcity, pimprove)) {
971 return TR_OTHER_FAILURE;
974 return TR_SUCCESS;
978 /****************************************************************************
979 Try to find a sensible replacement building, based on other buildings
980 that may have caused this one to become obsolete.
981 ****************************************************************************/
982 struct impr_type *improvement_replacement(const struct impr_type *pimprove)
984 requirement_vector_iterate(&pimprove->obsolete_by, pobs) {
985 if (pobs->source.kind == VUT_IMPROVEMENT && pobs->present) {
986 return pobs->source.value.building;
988 } requirement_vector_iterate_end;
990 return NULL;