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 #ifndef FC__IMPROVEMENT_H
14 #define FC__IMPROVEMENT_H
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif /* __cplusplus */
19 
20 /* City Improvements, including Wonders.  (Alternatively "Buildings".) */
21 
22 /* utility */
23 #include "bitvector.h"
24 #include "support.h"            /* bool */
25 
26 /* common */
27 #include "fc_types.h"
28 #include "name_translation.h"
29 #include "requirements.h"
30 
31 struct strvec;          /* Actually defined in "utility/string_vector.h". */
32 
33 /* B_LAST is a value that is guaranteed to be larger than all
34  * actual Impr_type_id values.  It is used as a flag value; it can
35  * also be used for fixed allocations to ensure ability to hold the
36  * full number of improvement types.
37  *
38  * B_NEVER is the pointer equivalent replacement for B_LAST flag value.
39  *
40  * Used in the network protocol.
41  */
42 #define B_LAST MAX_NUM_ITEMS
43 
44 #define B_NEVER (NULL)
45 
46 /* Changing these breaks network compatibility. */
47 #define SPECENUM_NAME impr_flag_id
48 /* improvement should be visible to others without spying */
49 #define SPECENUM_VALUE0 IF_VISIBLE_BY_OTHERS
50 #define SPECENUM_VALUE0NAME "VisibleByOthers"
51 /* this small wonder is moved to another city if game.savepalace is on. */
52 #define SPECENUM_VALUE1 IF_SAVE_SMALL_WONDER
53 #define SPECENUM_VALUE1NAME "SaveSmallWonder"
54 /* when built, gives gold */
55 #define SPECENUM_VALUE2 IF_GOLD
56 #define SPECENUM_VALUE2NAME "Gold"
57 #define SPECENUM_COUNT IF_COUNT
58 #define SPECENUM_BITVECTOR bv_impr_flags
59 #include "specenum_gen.h"
60 
61 /* Used in the network protocol. */
62 #define SPECENUM_NAME impr_genus_id
63 #define SPECENUM_VALUE0 IG_GREAT_WONDER
64 #define SPECENUM_VALUE0NAME "GreatWonder"
65 #define SPECENUM_VALUE1 IG_SMALL_WONDER
66 #define SPECENUM_VALUE1NAME "SmallWonder"
67 #define SPECENUM_VALUE2 IG_IMPROVEMENT
68 #define SPECENUM_VALUE2NAME "Improvement"
69 #define SPECENUM_VALUE3 IG_SPECIAL
70 #define SPECENUM_VALUE3NAME "Special"
71 #include "specenum_gen.h"
72 
73 /* Used in the network protocol. */
74 BV_DEFINE(bv_imprs, B_LAST);
75 
76 /* Type of improvement. (Read from buildings.ruleset file.) */
77 struct impr_type {
78   Impr_type_id item_number;
79   struct name_translation name;
80   bool disabled;                        /* Does not really exist - hole in improvments array */
81   char graphic_str[MAX_LEN_NAME];	/* city icon of improv. */
82   char graphic_alt[MAX_LEN_NAME];	/* city icon of improv. */
83   struct requirement_vector reqs;
84   struct requirement_vector obsolete_by;
85   int build_cost;			/* Use wrappers to access this. */
86   int upkeep;
87   int sabotage;		/* Base chance of diplomat sabotage succeeding. */
88   enum impr_genus_id genus;		/* genus; e.g. GreatWonder */
89   bv_impr_flags flags;
90   struct strvec *helptext;
91   char soundtag[MAX_LEN_NAME];
92   char soundtag_alt[MAX_LEN_NAME];
93 
94   /* Cache */
95   bool allows_units;
96   bool allows_extras;
97   bool prevents_disaster;
98   bool protects_vs_actions;
99   bool allows_actions;
100 };
101 
102 
103 /* General improvement accessor functions. */
104 Impr_type_id improvement_count(void);
105 Impr_type_id improvement_index(const struct impr_type *pimprove);
106 Impr_type_id improvement_number(const struct impr_type *pimprove);
107 
108 struct impr_type *improvement_by_number(const Impr_type_id id);
109 
110 struct impr_type *valid_improvement(struct impr_type *pimprove);
111 struct impr_type *valid_improvement_by_number(const Impr_type_id id);
112 
113 struct impr_type *improvement_by_rule_name(const char *name);
114 struct impr_type *improvement_by_translated_name(const char *name);
115 
116 const char *improvement_rule_name(const struct impr_type *pimprove);
117 const char *improvement_name_translation(const struct impr_type *pimprove);
118 
119 /* General improvement flag accessor routines */
120 bool improvement_has_flag(const struct impr_type *pimprove,
121                           enum impr_flag_id flag);
122 
123 /* Ancillary routines */
124 int impr_build_shield_cost(const struct impr_type *pimprove);
125 int impr_buy_gold_cost(const struct impr_type *pimprove, int shields_in_stock);
126 int impr_sell_gold(const struct impr_type *pimprove);
127 
128 bool is_improvement_visible(const struct impr_type *pimprove);
129 
130 bool is_great_wonder(const struct impr_type *pimprove);
131 bool is_small_wonder(const struct impr_type *pimprove);
132 bool is_wonder(const struct impr_type *pimprove);
133 bool is_improvement(const struct impr_type *pimprove);
134 bool is_special_improvement(const struct impr_type *pimprove);
135 
136 bool can_improvement_go_obsolete(const struct impr_type *pimprove);
137 
138 bool can_sell_building(struct impr_type *pimprove);
139 bool can_city_sell_building(const struct city *pcity,
140 			    struct impr_type *pimprove);
141 enum test_result test_player_sell_building_now(struct player *pplayer,
142                                                struct city *pcity,
143                                                struct impr_type *pimprove);
144 
145 struct impr_type *improvement_replacement(const struct impr_type *pimprove);
146 
147 /* Macros for struct packet_game_info::great_wonder_owners[]. */
148 #define WONDER_DESTROYED (MAX_NUM_PLAYER_SLOTS + 1)  /* Used as player id. */
149 #define WONDER_NOT_OWNED (MAX_NUM_PLAYER_SLOTS + 2)  /* Used as player id. */
150 #define WONDER_OWNED(player_id) ((player_id) < MAX_NUM_PLAYER_SLOTS)
151 
152 /* Macros for struct player::wonders[]. */
153 #define WONDER_LOST (-1)        /* Used as city id. */
154 #define WONDER_NOT_BUILT 0      /* Used as city id. */
155 #define WONDER_BUILT(city_id) ((city_id) > 0)
156 
157 void wonder_built(const struct city *pcity, const struct impr_type *pimprove);
158 void wonder_destroyed(const struct city *pcity,
159                       const struct impr_type *pimprove);
160 
161 bool wonder_is_lost(const struct player *pplayer,
162                     const struct impr_type *pimprove);
163 bool wonder_is_built(const struct player *pplayer,
164                      const struct impr_type *pimprove);
165 struct city *city_from_wonder(const struct player *pplayer,
166                               const struct impr_type *pimprove);
167 
168 bool great_wonder_is_built(const struct impr_type *pimprove);
169 bool great_wonder_is_destroyed(const struct impr_type *pimprove);
170 bool great_wonder_is_available(const struct impr_type *pimprove);
171 struct city *city_from_great_wonder(const struct impr_type *pimprove);
172 struct player *great_wonder_owner(const struct impr_type *pimprove);
173 
174 bool small_wonder_is_built(const struct player *pplayer,
175                            const struct impr_type *pimprove);
176 struct city *city_from_small_wonder(const struct player *pplayer,
177                                     const struct impr_type *pimprove);
178 
179 /* player related improvement functions */
180 bool improvement_obsolete(const struct player *pplayer,
181 			  const struct impr_type *pimprove,
182                           const struct city *pcity);
183 bool is_improvement_productive(const struct city *pcity,
184                                struct impr_type *pimprove);
185 bool is_improvement_redundant(const struct city *pcity,
186                               struct impr_type *pimprove);
187 
188 bool can_player_build_improvement_direct(const struct player *p,
189 					 struct impr_type *pimprove);
190 bool can_player_build_improvement_later(const struct player *p,
191 					struct impr_type *pimprove);
192 bool can_player_build_improvement_now(const struct player *p,
193 				      struct impr_type *pimprove);
194 
195 /* Initialization and iteration */
196 void improvements_init(void);
197 void improvements_free(void);
198 
199 void improvement_feature_cache_init(void);
200 
201 struct impr_type *improvement_array_first(void);
202 const struct impr_type *improvement_array_last(void);
203 
204 #define improvement_iterate(_p)						\
205 {									\
206   struct impr_type *_p = improvement_array_first();			\
207   if (NULL != _p) {							\
208     for (; _p <= improvement_array_last(); _p++) {
209 
210 #define improvement_iterate_end						\
211     }									\
212   }									\
213 }
214 
215 #define improvement_active_iterate(_p)                                  \
216   improvement_iterate(_p) {                                             \
217     if (!_p->disabled) {
218 
219 #define improvement_active_iterate_end                                  \
220     }                                                                   \
221   } improvement_iterate_end;
222 
223 #ifdef __cplusplus
224 }
225 #endif /* __cplusplus */
226 
227 #endif  /* FC__IMPROVEMENT_H */
228