1 /***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
13
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
17
18 /* 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"
25
26 /* common */
27 #include "game.h"
28 #include "map.h"
29 #include "tech.h"
30 #include "victory.h"
31
32 #include "improvement.h"
33
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];
42
43 /****************************************************************************
44 Initialize building structures.
45 ****************************************************************************/
improvements_init(void)46 void improvements_init(void)
47 {
48 int i;
49
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];
54
55 p->item_number = i;
56 requirement_vector_init(&p->reqs);
57 requirement_vector_init(&p->obsolete_by);
58 p->disabled = FALSE;
59 }
60 }
61
62 /**************************************************************************
63 Frees the memory associated with this improvement.
64 **************************************************************************/
improvement_free(struct impr_type * p)65 static void improvement_free(struct impr_type *p)
66 {
67 if (NULL != p->helptext) {
68 strvec_destroy(p->helptext);
69 p->helptext = NULL;
70 }
71
72 requirement_vector_free(&p->reqs);
73 requirement_vector_free(&p->obsolete_by);
74 }
75
76 /***************************************************************
77 Frees the memory associated with all improvements.
78 ***************************************************************/
improvements_free(void)79 void improvements_free(void)
80 {
81 improvement_iterate(p) {
82 improvement_free(p);
83 } improvement_iterate_end;
84 }
85
86 /**************************************************************************
87 Cache features of the improvement
88 **************************************************************************/
improvement_feature_cache_init(void)89 void improvement_feature_cache_init(void)
90 {
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;
97 }
98 } unit_type_iterate_end;
99
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;
105 }
106 } extra_type_iterate_end;
107
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;
113 }
114 } disaster_type_iterate_end;
115
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;
122 }
123 } action_enablers_iterate_end;
124
125 pimprove->allows_actions = FALSE;
126 action_enablers_iterate(act) {
127 if (requirement_needs_improvement(pimprove, &act->actor_reqs)) {
128 pimprove->allows_actions = TRUE;
129 break;
130 }
131 } action_enablers_iterate_end;
132
133 } improvement_iterate_end;
134 }
135
136 /**************************************************************************
137 Return the first item of improvements.
138 **************************************************************************/
improvement_array_first(void)139 struct impr_type *improvement_array_first(void)
140 {
141 if (game.control.num_impr_types > 0) {
142 return improvement_types;
143 }
144 return NULL;
145 }
146
147 /**************************************************************************
148 Return the last item of improvements.
149 **************************************************************************/
improvement_array_last(void)150 const struct impr_type *improvement_array_last(void)
151 {
152 if (game.control.num_impr_types > 0) {
153 return &improvement_types[game.control.num_impr_types - 1];
154 }
155 return NULL;
156 }
157
158 /**************************************************************************
159 Return the number of improvements.
160 **************************************************************************/
improvement_count(void)161 Impr_type_id improvement_count(void)
162 {
163 return game.control.num_impr_types;
164 }
165
166 /**************************************************************************
167 Return the improvement index.
168
169 Currently same as improvement_number(), paired with improvement_count()
170 indicates use as an array index.
171 **************************************************************************/
improvement_index(const struct impr_type * pimprove)172 Impr_type_id improvement_index(const struct impr_type *pimprove)
173 {
174 fc_assert_ret_val(NULL != pimprove, -1);
175 return pimprove - improvement_types;
176 }
177
178 /**************************************************************************
179 Return the improvement index.
180 **************************************************************************/
improvement_number(const struct impr_type * pimprove)181 Impr_type_id improvement_number(const struct impr_type *pimprove)
182 {
183 fc_assert_ret_val(NULL != pimprove, -1);
184 return pimprove->item_number;
185 }
186
187 /**************************************************************************
188 Returns the improvement type for the given index/ID. Returns NULL for
189 an out-of-range index.
190 **************************************************************************/
improvement_by_number(const Impr_type_id id)191 struct impr_type *improvement_by_number(const Impr_type_id id)
192 {
193 if (id < 0 || id >= improvement_count()) {
194 return NULL;
195 }
196 return &improvement_types[id];
197 }
198
199 /**************************************************************************
200 Returns pointer when the improvement_type "exists" in this game,
201 returns NULL otherwise.
202
203 An improvement_type doesn't exist for any of:
204 - the improvement_type has been flagged as removed by setting its
205 tech_req to A_LAST; [was not in current 2007-07-27]
206 - it is a space part, and the spacerace is not enabled.
207 **************************************************************************/
valid_improvement(struct impr_type * pimprove)208 struct impr_type *valid_improvement(struct impr_type *pimprove)
209 {
210 if (NULL == pimprove) {
211 return NULL;
212 }
213
214 if (!victory_enabled(VC_SPACERACE)
215 && (building_has_effect(pimprove, EFT_SS_STRUCTURAL)
216 || building_has_effect(pimprove, EFT_SS_COMPONENT)
217 || building_has_effect(pimprove, EFT_SS_MODULE))) {
218 /* This assumes that space parts don't have any other effects. */
219 return NULL;
220 }
221
222 return pimprove;
223 }
224
225 /**************************************************************************
226 Returns pointer when the improvement_type "exists" in this game,
227 returns NULL otherwise.
228
229 In addition to valid_improvement(), tests for id is out of range.
230 **************************************************************************/
valid_improvement_by_number(const Impr_type_id id)231 struct impr_type *valid_improvement_by_number(const Impr_type_id id)
232 {
233 return valid_improvement(improvement_by_number(id));
234 }
235
236 /**************************************************************************
237 Return the (translated) name of the given improvement.
238 You don't have to free the return pointer.
239 **************************************************************************/
improvement_name_translation(const struct impr_type * pimprove)240 const char *improvement_name_translation(const struct impr_type *pimprove)
241 {
242 return name_translation_get(&pimprove->name);
243 }
244
245 /****************************************************************************
246 Return the (untranslated) rule name of the improvement.
247 You don't have to free the return pointer.
248 ****************************************************************************/
improvement_rule_name(const struct impr_type * pimprove)249 const char *improvement_rule_name(const struct impr_type *pimprove)
250 {
251 return rule_name_get(&pimprove->name);
252 }
253
254 /****************************************************************************
255 Returns the number of shields it takes to build this improvement.
256 ****************************************************************************/
impr_build_shield_cost(const struct impr_type * pimprove)257 int impr_build_shield_cost(const struct impr_type *pimprove)
258 {
259 int base = pimprove->build_cost;
260
261 return MAX(base * game.info.shieldbox / 100, 1);
262 }
263
264 /****************************************************************************
265 Returns the amount of gold it takes to rush this improvement.
266 ****************************************************************************/
impr_buy_gold_cost(const struct impr_type * pimprove,int shields_in_stock)267 int impr_buy_gold_cost(const struct impr_type *pimprove, int shields_in_stock)
268 {
269 int cost = 0;
270 const int missing = impr_build_shield_cost(pimprove) - shields_in_stock;
271
272 if (improvement_has_flag(pimprove, IF_GOLD)) {
273 /* Can't buy capitalization. */
274 return 0;
275 }
276
277 if (missing > 0) {
278 cost = 2 * missing;
279 }
280
281 if (is_great_wonder(pimprove)) {
282 cost *= 2;
283 }
284 if (shields_in_stock == 0) {
285 cost *= 2;
286 }
287 return cost;
288 }
289
290 /****************************************************************************
291 Returns the amount of gold received when this improvement is sold.
292 ****************************************************************************/
impr_sell_gold(const struct impr_type * pimprove)293 int impr_sell_gold(const struct impr_type *pimprove)
294 {
295 return impr_build_shield_cost(pimprove);
296 }
297
298 /**************************************************************************
299 Returns whether improvement is some kind of wonder. Both great wonders
300 and small wonders count.
301 **************************************************************************/
is_wonder(const struct impr_type * pimprove)302 bool is_wonder(const struct impr_type *pimprove)
303 {
304 return (is_great_wonder(pimprove) || is_small_wonder(pimprove));
305 }
306
307 /**************************************************************************
308 Does a linear search of improvement_types[].name.translated
309 Returns NULL when none match.
310 **************************************************************************/
improvement_by_translated_name(const char * name)311 struct impr_type *improvement_by_translated_name(const char *name)
312 {
313 improvement_iterate(pimprove) {
314 if (0 == strcmp(improvement_name_translation(pimprove), name)) {
315 return pimprove;
316 }
317 } improvement_iterate_end;
318
319 return NULL;
320 }
321
322 /****************************************************************************
323 Does a linear search of improvement_types[].name.vernacular
324 Returns NULL when none match.
325 ****************************************************************************/
improvement_by_rule_name(const char * name)326 struct impr_type *improvement_by_rule_name(const char *name)
327 {
328 const char *qname = Qn_(name);
329
330 improvement_iterate(pimprove) {
331 if (0 == fc_strcasecmp(improvement_rule_name(pimprove), qname)) {
332 return pimprove;
333 }
334 } improvement_iterate_end;
335
336 return NULL;
337 }
338
339 /**************************************************************************
340 Return TRUE if the impr has this flag otherwise FALSE
341 **************************************************************************/
improvement_has_flag(const struct impr_type * pimprove,enum impr_flag_id flag)342 bool improvement_has_flag(const struct impr_type *pimprove,
343 enum impr_flag_id flag)
344 {
345 fc_assert_ret_val(impr_flag_id_is_valid(flag), FALSE);
346 return BV_ISSET(pimprove->flags, flag);
347 }
348
349 /**************************************************************************
350 Return TRUE if the improvement should be visible to others without spying
351 **************************************************************************/
is_improvement_visible(const struct impr_type * pimprove)352 bool is_improvement_visible(const struct impr_type *pimprove)
353 {
354 return (is_wonder(pimprove)
355 || improvement_has_flag(pimprove, IF_VISIBLE_BY_OTHERS));
356 }
357
358 /**************************************************************************
359 Return TRUE if the improvement can ever go obsolete.
360 Can be used for buildings or wonders.
361 **************************************************************************/
can_improvement_go_obsolete(const struct impr_type * pimprove)362 bool can_improvement_go_obsolete(const struct impr_type *pimprove)
363 {
364 return requirement_vector_size(&pimprove->obsolete_by) > 0;
365 }
366
367 /**************************************************************************
368 Returns TRUE if the improvement or wonder is obsolete
369 **************************************************************************/
improvement_obsolete(const struct player * pplayer,const struct impr_type * pimprove,const struct city * pcity)370 bool improvement_obsolete(const struct player *pplayer,
371 const struct impr_type *pimprove,
372 const struct city *pcity)
373 {
374 struct tile *ptile = NULL;
375
376 if (pcity != NULL) {
377 ptile = city_tile(pcity);
378 }
379
380 requirement_vector_iterate(&pimprove->obsolete_by, preq) {
381 if (is_req_active(pplayer, NULL, pcity, pimprove, ptile, NULL, NULL,
382 NULL, NULL, preq, RPT_CERTAIN)) {
383 return TRUE;
384 }
385 } requirement_vector_iterate_end;
386
387 return FALSE;
388 }
389
390 /**************************************************************************
391 Returns TRUE iff improvement provides units buildable in city
392 **************************************************************************/
impr_provides_buildable_units(const struct city * pcity,struct impr_type * pimprove)393 static bool impr_provides_buildable_units(const struct city *pcity,
394 struct impr_type *pimprove)
395 {
396 /* Fast check */
397 if (!pimprove->allows_units) {
398 return FALSE;
399 }
400
401 unit_type_iterate(ut) {
402 if (ut->need_improvement == pimprove
403 && can_city_build_unit_now(pcity, ut)) {
404 return TRUE;
405 }
406 } unit_type_iterate_end;
407
408 return FALSE;
409 }
410
411 /**************************************************************************
412 Returns TRUE iff improvement provides extras buildable in city
413 **************************************************************************/
impr_provides_buildable_extras(const struct city * pcity,struct impr_type * pimprove)414 static bool impr_provides_buildable_extras(const struct city *pcity,
415 struct impr_type *pimprove)
416 {
417
418 /* Fast check */
419 if (!pimprove->allows_extras) {
420 return FALSE;
421 }
422
423 extra_type_iterate(pextra) {
424 if (requirement_needs_improvement(pimprove, &pextra->reqs)) {
425 city_tile_iterate(city_map_radius_sq_get(pcity),
426 city_tile(pcity), ptile) {
427 if (player_can_build_extra(pextra, city_owner(pcity), ptile)) {
428 return TRUE;
429 }
430 } city_tile_iterate_end;
431 }
432 } extra_type_iterate_end;
433
434 return FALSE;
435 }
436
437 /**************************************************************************
438 Returns TRUE iff improvement prevents a disaster in city
439 **************************************************************************/
impr_prevents_disaster(const struct city * pcity,struct impr_type * pimprove)440 static bool impr_prevents_disaster(const struct city *pcity,
441 struct impr_type *pimprove)
442 {
443 /* Fast check */
444 if (!pimprove->prevents_disaster) {
445 return FALSE;
446 }
447
448 disaster_type_iterate(pdis) {
449 if (!requirement_fulfilled_by_improvement(pimprove, &pdis->reqs)
450 && !can_disaster_happen(pdis, pcity)) {
451 return TRUE;
452 }
453 } disaster_type_iterate_end;
454
455 return FALSE;
456 }
457
458 /**************************************************************************
459 Returns TRUE iff improvement protects against an action on the city
460 FIXME: This is prone to false positives: for example, if one requires
461 a special tech or unit to perform an action, and no other player
462 has or can gain that tech or unit, protection is still claimed.
463 **************************************************************************/
impr_protects_vs_actions(const struct city * pcity,struct impr_type * pimprove)464 static bool impr_protects_vs_actions(const struct city *pcity,
465 struct impr_type *pimprove)
466 {
467 /* Fast check */
468 if (!pimprove->protects_vs_actions) {
469 return FALSE;
470 }
471
472 action_enablers_iterate(act) {
473 if (!requirement_fulfilled_by_improvement(pimprove, &act->target_reqs)
474 && !is_action_possible_on_city(act->action, NULL, pcity)) {
475 return TRUE;
476 }
477 } action_enablers_iterate_end;
478
479 return FALSE;
480 }
481
482 /**********************************************************************//**
483 Returns TRUE iff improvement allows its owner to perform an action
484 **************************************************************************/
impr_allows_actions(const struct city * pcity,struct impr_type * pimprove)485 static bool impr_allows_actions(const struct city *pcity,
486 struct impr_type *pimprove)
487 {
488 /* Fast check */
489 if (!pimprove->allows_actions) {
490 return FALSE;
491 }
492
493 action_enablers_iterate(act) {
494 if (requirement_needs_improvement(pimprove, &act->actor_reqs)) {
495 switch (action_id_get_actor_kind(act->action)) {
496 case AAK_UNIT:
497 unit_type_iterate(ut) {
498 if (!utype_can_do_action(ut, act->action)) {
499 /* Not relevant */
500 continue;
501 }
502
503 if (utype_player_already_has_this(city_owner(pcity), ut)) {
504 /* The player has a unit that may use the buidling */
505 return TRUE;
506 }
507
508 if (can_city_build_unit_now(pcity, ut)) {
509 /* This city can build a unit that uses the building */
510 return TRUE;
511 }
512 } unit_type_iterate_end;
513 break;
514 case AAK_COUNT:
515 fc_assert(action_id_get_actor_kind(act->action) != AAK_COUNT);
516 break;
517 }
518 }
519 } action_enablers_iterate_end;
520
521 return FALSE;
522 }
523
524 /**************************************************************************
525 Check if an improvement has side effects for a city. Side effects
526 are any benefits that accrue that are not tracked by the effects
527 system.
528
529 Note that this function will always return FALSE if the improvement does
530 not currently provide a benefit to the city (for example, if the improvement
531 has not yet been built, or another city benefits from the improvement in
532 this city (i.e. Wonders)).
533 **************************************************************************/
improvement_has_side_effects(const struct city * pcity,struct impr_type * pimprove)534 static bool improvement_has_side_effects(const struct city *pcity,
535 struct impr_type *pimprove)
536 {
537 return (impr_provides_buildable_units(pcity, pimprove)
538 || impr_provides_buildable_extras(pcity, pimprove)
539 || impr_prevents_disaster(pcity, pimprove)
540 || impr_allows_actions(pcity, pimprove)
541 || impr_protects_vs_actions(pcity, pimprove));
542 }
543
544 /**************************************************************************
545 Returns TRUE iff improvement provides some effect (good or bad).
546 **************************************************************************/
improvement_has_effects(const struct city * pcity,struct impr_type * pimprove)547 static bool improvement_has_effects(const struct city *pcity,
548 struct impr_type *pimprove)
549 {
550 struct universal source = { .kind = VUT_IMPROVEMENT,
551 .value = { .building = pimprove } };
552 struct effect_list *plist = get_req_source_effects(&source);
553
554 if (!plist || improvement_obsolete(city_owner(pcity), pimprove, pcity)) {
555 return FALSE;
556 }
557
558 effect_list_iterate(plist, peffect) {
559 if (0 != get_potential_improvement_bonus(pimprove, pcity,
560 peffect->type, RPT_CERTAIN)) {
561 return TRUE;
562 }
563 } effect_list_iterate_end;
564
565 return FALSE;
566 }
567
568 /**************************************************************************
569 Returns TRUE if an improvement in a city is productive, in some way.
570
571 Note that unproductive improvements may become productive later, if
572 appropriate conditions are met (e.g. a special building that isn't
573 producing units under the current government might under another).
574 **************************************************************************/
is_improvement_productive(const struct city * pcity,struct impr_type * pimprove)575 bool is_improvement_productive(const struct city *pcity,
576 struct impr_type *pimprove)
577 {
578 return (!improvement_obsolete(city_owner(pcity), pimprove, pcity)
579 && (improvement_has_flag(pimprove, IF_GOLD)
580 || improvement_has_side_effects(pcity, pimprove)
581 || improvement_has_effects(pcity, pimprove)));
582 }
583
584 /**************************************************************************
585 Returns TRUE if an improvement in a city is redundant, that is, the
586 city wouldn't lose anything by losing the improvement, or gain anything
587 by building it. This means:
588 - all of its effects (if any) are provided by other means, or it's
589 obsolete (and thus assumed to have no effect); and
590 - it's not enabling the city to build some kind of units; and
591 - it's not Coinage (IF_GOLD).
592 (Note that it's not impossible that this improvement could become useful
593 if circumstances changed, say if a new government enabled the building
594 of its special units.)
595 **************************************************************************/
is_improvement_redundant(const struct city * pcity,struct impr_type * pimprove)596 bool is_improvement_redundant(const struct city *pcity,
597 struct impr_type *pimprove)
598 {
599 /* A capitalization production is never redundant. */
600 if (improvement_has_flag(pimprove, IF_GOLD)) {
601 return FALSE;
602 }
603
604 /* If an improvement has side effects, don't claim it's redundant. */
605 if (improvement_has_side_effects(pcity, pimprove)) {
606 return FALSE;
607 }
608
609 /* Otherwise, it's redundant if its effects are available by other means,
610 * or if it's an obsolete wonder (great or small). */
611 return is_building_replaced(pcity, pimprove, RPT_CERTAIN)
612 || improvement_obsolete(city_owner(pcity), pimprove, pcity);
613 }
614
615 /**************************************************************************
616 Whether player can build given building somewhere, ignoring whether it
617 is obsolete.
618 **************************************************************************/
can_player_build_improvement_direct(const struct player * p,struct impr_type * pimprove)619 bool can_player_build_improvement_direct(const struct player *p,
620 struct impr_type *pimprove)
621 {
622 bool space_part = FALSE;
623
624 if (!valid_improvement(pimprove)) {
625 return FALSE;
626 }
627
628 requirement_vector_iterate(&pimprove->reqs, preq) {
629 if (preq->range >= REQ_RANGE_PLAYER
630 && !is_req_active(p, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
631 NULL, preq, RPT_CERTAIN)) {
632 return FALSE;
633 }
634 } requirement_vector_iterate_end;
635
636 /* Check for space part construction. This assumes that space parts have
637 * no other effects. */
638 if (building_has_effect(pimprove, EFT_SS_STRUCTURAL)) {
639 space_part = TRUE;
640 if (p->spaceship.structurals >= NUM_SS_STRUCTURALS) {
641 return FALSE;
642 }
643 }
644 if (building_has_effect(pimprove, EFT_SS_COMPONENT)) {
645 space_part = TRUE;
646 if (p->spaceship.components >= NUM_SS_COMPONENTS) {
647 return FALSE;
648 }
649 }
650 if (building_has_effect(pimprove, EFT_SS_MODULE)) {
651 space_part = TRUE;
652 if (p->spaceship.modules >= NUM_SS_MODULES) {
653 return FALSE;
654 }
655 }
656 if (space_part &&
657 (get_player_bonus(p, EFT_ENABLE_SPACE) <= 0
658 || p->spaceship.state >= SSHIP_LAUNCHED)) {
659 return FALSE;
660 }
661
662 if (is_great_wonder(pimprove)) {
663 /* Can't build wonder if already built */
664 if (!great_wonder_is_available(pimprove)) {
665 return FALSE;
666 }
667 }
668
669 return TRUE;
670 }
671
672 /**************************************************************************
673 Whether player can build given building somewhere immediately.
674 Returns FALSE if building is obsolete.
675 **************************************************************************/
can_player_build_improvement_now(const struct player * p,struct impr_type * pimprove)676 bool can_player_build_improvement_now(const struct player *p,
677 struct impr_type *pimprove)
678 {
679 if (!can_player_build_improvement_direct(p, pimprove)) {
680 return FALSE;
681 }
682 if (improvement_obsolete(p, pimprove, NULL)) {
683 return FALSE;
684 }
685 return TRUE;
686 }
687
688 /**************************************************************************
689 Whether player can _eventually_ build given building somewhere -- i.e.,
690 returns TRUE if building is available with current tech OR will be
691 available with future tech. Returns FALSE if building is obsolete.
692 **************************************************************************/
can_player_build_improvement_later(const struct player * p,struct impr_type * pimprove)693 bool can_player_build_improvement_later(const struct player *p,
694 struct impr_type *pimprove)
695 {
696 if (!valid_improvement(pimprove)) {
697 return FALSE;
698 }
699 if (improvement_obsolete(p, pimprove, NULL)) {
700 return FALSE;
701 }
702 if (is_great_wonder(pimprove) && !great_wonder_is_available(pimprove)) {
703 /* Can't build wonder if already built */
704 return FALSE;
705 }
706
707 /* Check for requirements that aren't met and that are unchanging (so
708 * they can never be met). */
709 requirement_vector_iterate(&pimprove->reqs, preq) {
710 if (preq->range >= REQ_RANGE_PLAYER
711 && is_req_unchanging(preq)
712 && !is_req_active(p, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
713 NULL, preq, RPT_POSSIBLE)) {
714 return FALSE;
715 }
716 } requirement_vector_iterate_end;
717 /* FIXME: should check some "unchanging" reqs here - like if there's
718 * a nation requirement, we can go ahead and check it now. */
719
720 return TRUE;
721 }
722
723 /**************************************************************************
724 Is this building a great wonder?
725 **************************************************************************/
is_great_wonder(const struct impr_type * pimprove)726 bool is_great_wonder(const struct impr_type *pimprove)
727 {
728 return (pimprove->genus == IG_GREAT_WONDER);
729 }
730
731 /**************************************************************************
732 Is this building a small wonder?
733 **************************************************************************/
is_small_wonder(const struct impr_type * pimprove)734 bool is_small_wonder(const struct impr_type *pimprove)
735 {
736 return (pimprove->genus == IG_SMALL_WONDER);
737 }
738
739 /**************************************************************************
740 Is this building a regular improvement?
741 **************************************************************************/
is_improvement(const struct impr_type * pimprove)742 bool is_improvement(const struct impr_type *pimprove)
743 {
744 return (pimprove->genus == IG_IMPROVEMENT);
745 }
746
747 /**************************************************************************
748 Returns TRUE if this is a "special" improvement. For example, spaceship
749 parts and coinage in the default ruleset are considered special.
750 **************************************************************************/
is_special_improvement(const struct impr_type * pimprove)751 bool is_special_improvement(const struct impr_type *pimprove)
752 {
753 return pimprove->genus == IG_SPECIAL;
754 }
755
756 /**************************************************************************
757 Build a wonder in the city.
758 **************************************************************************/
wonder_built(const struct city * pcity,const struct impr_type * pimprove)759 void wonder_built(const struct city *pcity, const struct impr_type *pimprove)
760 {
761 struct player *pplayer;
762 int windex = improvement_number(pimprove);
763
764 fc_assert_ret(NULL != pcity);
765 fc_assert_ret(is_wonder(pimprove));
766
767 pplayer = city_owner(pcity);
768 pplayer->wonders[windex] = pcity->id;
769
770 if (is_great_wonder(pimprove)) {
771 game.info.great_wonder_owners[windex] = player_number(pplayer);
772 }
773 }
774
775 /**************************************************************************
776 Remove a wonder from a city and destroy it if it's a great wonder. To
777 transfer a great wonder, use great_wonder_transfer.
778 **************************************************************************/
wonder_destroyed(const struct city * pcity,const struct impr_type * pimprove)779 void wonder_destroyed(const struct city *pcity,
780 const struct impr_type *pimprove)
781 {
782 struct player *pplayer;
783 int windex = improvement_number(pimprove);
784
785 fc_assert_ret(NULL != pcity);
786 fc_assert_ret(is_wonder(pimprove));
787
788 pplayer = city_owner(pcity);
789 fc_assert_ret(pplayer->wonders[windex] == pcity->id);
790 pplayer->wonders[windex] = WONDER_LOST;
791
792 if (is_great_wonder(pimprove)) {
793 fc_assert_ret(game.info.great_wonder_owners[windex]
794 == player_number(pplayer));
795 game.info.great_wonder_owners[windex] = WONDER_DESTROYED;
796 }
797 }
798
799 /**************************************************************************
800 Returns whether the player has lost this wonder after having owned it
801 (small or great).
802 **************************************************************************/
wonder_is_lost(const struct player * pplayer,const struct impr_type * pimprove)803 bool wonder_is_lost(const struct player *pplayer,
804 const struct impr_type *pimprove)
805 {
806 fc_assert_ret_val(NULL != pplayer, FALSE);
807 fc_assert_ret_val(is_wonder(pimprove), FALSE);
808
809 return pplayer->wonders[improvement_index(pimprove)] == WONDER_LOST;
810 }
811
812 /**************************************************************************
813 Returns whether the player is currently in possession of this wonder
814 (small or great).
815 **************************************************************************/
wonder_is_built(const struct player * pplayer,const struct impr_type * pimprove)816 bool wonder_is_built(const struct player *pplayer,
817 const struct impr_type *pimprove)
818 {
819 fc_assert_ret_val(NULL != pplayer, FALSE);
820 fc_assert_ret_val(is_wonder(pimprove), FALSE);
821
822 return WONDER_BUILT(pplayer->wonders[improvement_index(pimprove)]);
823 }
824
825 /**************************************************************************
826 Get the world city with this wonder (small or great). This doesn't
827 always succeed on the client side, and even when it does, it may
828 return an "invisible" city whose members are unexpectedly NULL;
829 take care.
830 **************************************************************************/
city_from_wonder(const struct player * pplayer,const struct impr_type * pimprove)831 struct city *city_from_wonder(const struct player *pplayer,
832 const struct impr_type *pimprove)
833 {
834 int city_id = pplayer->wonders[improvement_index(pimprove)];
835
836 fc_assert_ret_val(NULL != pplayer, NULL);
837 fc_assert_ret_val(is_wonder(pimprove), NULL);
838
839 if (!WONDER_BUILT(city_id)) {
840 return NULL;
841 }
842
843 #ifdef FREECIV_DEBUG
844 if (is_server()) {
845 /* On client side, this info is not always known. */
846 struct city *pcity = player_city_by_number(pplayer, city_id);
847
848 if (NULL == pcity) {
849 log_error("Player %s (nb %d) has outdated wonder info for "
850 "%s (nb %d), it points to city nb %d.",
851 player_name(pplayer), player_number(pplayer),
852 improvement_rule_name(pimprove),
853 improvement_number(pimprove), city_id);
854 } else if (!city_has_building(pcity, pimprove)) {
855 log_error("Player %s (nb %d) has outdated wonder info for "
856 "%s (nb %d), the city %s (nb %d) doesn't have this wonder.",
857 player_name(pplayer), player_number(pplayer),
858 improvement_rule_name(pimprove),
859 improvement_number(pimprove), city_name_get(pcity), pcity->id);
860 return NULL;
861 }
862
863 return pcity;
864 }
865 #endif /* FREECIV_DEBUG */
866
867 return player_city_by_number(pplayer, city_id);
868 }
869
870 /**************************************************************************
871 Returns whether this wonder is currently built.
872 **************************************************************************/
great_wonder_is_built(const struct impr_type * pimprove)873 bool great_wonder_is_built(const struct impr_type *pimprove)
874 {
875 fc_assert_ret_val(is_great_wonder(pimprove), FALSE);
876
877 return WONDER_OWNED(game.info.great_wonder_owners
878 [improvement_index(pimprove)]);
879 }
880
881 /**************************************************************************
882 Returns whether this wonder has been destroyed.
883 **************************************************************************/
great_wonder_is_destroyed(const struct impr_type * pimprove)884 bool great_wonder_is_destroyed(const struct impr_type *pimprove)
885 {
886 fc_assert_ret_val(is_great_wonder(pimprove), FALSE);
887
888 return (WONDER_DESTROYED
889 == game.info.great_wonder_owners[improvement_index(pimprove)]);
890 }
891
892 /**************************************************************************
893 Returns whether this wonder can be currently built.
894 **************************************************************************/
great_wonder_is_available(const struct impr_type * pimprove)895 bool great_wonder_is_available(const struct impr_type *pimprove)
896 {
897 fc_assert_ret_val(is_great_wonder(pimprove), FALSE);
898
899 return (WONDER_NOT_OWNED
900 == game.info.great_wonder_owners[improvement_index(pimprove)]);
901 }
902
903 /**************************************************************************
904 Get the world city with this great wonder. This doesn't always success
905 on the client side.
906 **************************************************************************/
city_from_great_wonder(const struct impr_type * pimprove)907 struct city *city_from_great_wonder(const struct impr_type *pimprove)
908 {
909 int player_id = game.info.great_wonder_owners[improvement_index(pimprove)];
910
911 fc_assert_ret_val(is_great_wonder(pimprove), NULL);
912
913 if (WONDER_OWNED(player_id)) {
914 #ifdef FREECIV_DEBUG
915 const struct player *pplayer = player_by_number(player_id);
916 struct city *pcity = city_from_wonder(pplayer, pimprove);
917
918 if (is_server() && NULL == pcity) {
919 log_error("Game has outdated wonder info for %s (nb %d), "
920 "the player %s (nb %d) doesn't have this wonder.",
921 improvement_rule_name(pimprove),
922 improvement_number(pimprove),
923 player_name(pplayer), player_number(pplayer));
924 }
925
926 return pcity;
927 #else
928 return city_from_wonder(player_by_number(player_id), pimprove);
929 #endif /* FREECIV_DEBUG */
930 } else {
931 return NULL;
932 }
933 }
934
935 /**************************************************************************
936 Get the player owning this small wonder. This doesn't always success on
937 the client side.
938 **************************************************************************/
great_wonder_owner(const struct impr_type * pimprove)939 struct player *great_wonder_owner(const struct impr_type *pimprove)
940 {
941 int player_id = game.info.great_wonder_owners[improvement_index(pimprove)];
942
943 fc_assert_ret_val(is_great_wonder(pimprove), NULL);
944
945 if (WONDER_OWNED(player_id)) {
946 return player_by_number(player_id);
947 } else {
948 return NULL;
949 }
950 }
951
952 /**************************************************************************
953 Returns whether the player has built this small wonder.
954 **************************************************************************/
small_wonder_is_built(const struct player * pplayer,const struct impr_type * pimprove)955 bool small_wonder_is_built(const struct player *pplayer,
956 const struct impr_type *pimprove)
957 {
958 fc_assert_ret_val(is_small_wonder(pimprove), FALSE);
959
960 return (NULL != pplayer
961 && wonder_is_built(pplayer, pimprove));
962 }
963
964 /**************************************************************************
965 Get the player city with this small wonder.
966 **************************************************************************/
city_from_small_wonder(const struct player * pplayer,const struct impr_type * pimprove)967 struct city *city_from_small_wonder(const struct player *pplayer,
968 const struct impr_type *pimprove)
969 {
970 fc_assert_ret_val(is_small_wonder(pimprove), NULL);
971
972 if (NULL == pplayer) {
973 return NULL; /* Used in some places in the client. */
974 } else {
975 return city_from_wonder(pplayer, pimprove);
976 }
977 }
978
979 /**************************************************************************
980 Return TRUE iff the improvement can be sold.
981 **************************************************************************/
can_sell_building(struct impr_type * pimprove)982 bool can_sell_building(struct impr_type *pimprove)
983 {
984 return (valid_improvement(pimprove) && is_improvement(pimprove));
985 }
986
987 /****************************************************************************
988 Return TRUE iff the city can sell the given improvement.
989 ****************************************************************************/
can_city_sell_building(const struct city * pcity,struct impr_type * pimprove)990 bool can_city_sell_building(const struct city *pcity,
991 struct impr_type *pimprove)
992 {
993 return (city_has_building(pcity, pimprove) && is_improvement(pimprove));
994 }
995
996 /****************************************************************************
997 Return TRUE iff the player can sell the given improvement from city.
998 If pimprove is NULL, returns iff city could sell some building type (this
999 does not check if such building is in this city)
1000 ****************************************************************************/
test_player_sell_building_now(struct player * pplayer,struct city * pcity,struct impr_type * pimprove)1001 enum test_result test_player_sell_building_now(struct player *pplayer,
1002 struct city *pcity,
1003 struct impr_type *pimprove)
1004 {
1005 /* Check if player can sell anything from this city */
1006 if (pcity->owner != pplayer) {
1007 return TR_OTHER_FAILURE;
1008 }
1009
1010 if (pcity->did_sell) {
1011 return TR_ALREADY_SOLD;
1012 }
1013
1014 /* Check if particular building can be solt */
1015 if (pimprove != NULL
1016 && !can_city_sell_building(pcity, pimprove)) {
1017 return TR_OTHER_FAILURE;
1018 }
1019
1020 return TR_SUCCESS;
1021 }
1022
1023
1024 /****************************************************************************
1025 Try to find a sensible replacement building, based on other buildings
1026 that may have caused this one to become obsolete.
1027 ****************************************************************************/
improvement_replacement(const struct impr_type * pimprove)1028 struct impr_type *improvement_replacement(const struct impr_type *pimprove)
1029 {
1030 requirement_vector_iterate(&pimprove->obsolete_by, pobs) {
1031 if (pobs->source.kind == VUT_IMPROVEMENT && pobs->present) {
1032 return pobs->source.value.building;
1033 }
1034 } requirement_vector_iterate_end;
1035
1036 return NULL;
1037 }
1038