1 /****************************************************************************
2 Freeciv - Copyright (C) 2004 - The Freeciv Team
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 "string_vector.h"
30 static struct base_type base_types
[MAX_BASE_TYPES
];
32 /****************************************************************************
33 Check if base provides effect
34 ****************************************************************************/
35 bool base_has_flag(const struct base_type
*pbase
, enum base_flag_id flag
)
37 return BV_ISSET(pbase
->flags
, flag
);
40 /****************************************************************************
41 Is base native to unit class?
42 ****************************************************************************/
43 bool is_native_base_to_uclass(const struct base_type
*pbase
,
44 const struct unit_class
*pclass
)
46 return BV_ISSET(pbase
->native_to
, uclass_index(pclass
));
49 /****************************************************************************
50 Is base native to unit type?
51 ****************************************************************************/
52 bool is_native_base_to_utype(const struct base_type
*pbase
,
53 const struct unit_type
*punittype
)
55 return is_native_base_to_uclass(pbase
, utype_class(punittype
));
58 /****************************************************************************
59 Is tile native to base?
60 ****************************************************************************/
61 bool is_native_tile_to_base(const struct base_type
*pbase
,
62 const struct tile
*ptile
)
64 if (tile_city(ptile
) != NULL
&& pbase
->border_sq
>= 0) {
68 return are_reqs_active(NULL
, NULL
, NULL
, ptile
,
69 NULL
, NULL
, NULL
, &pbase
->reqs
, RPT_POSSIBLE
);
72 /****************************************************************************
73 Base provides base flag for unit? Checks if base provides flag and if
74 base is native to unit.
75 ****************************************************************************/
76 bool base_has_flag_for_utype(const struct base_type
*pbase
,
77 enum base_flag_id flag
,
78 const struct unit_type
*punittype
)
80 return base_has_flag(pbase
, flag
) && is_native_base_to_utype(pbase
, punittype
);
83 /**************************************************************************
84 Return the (translated) name of the base type.
85 You don't have to free the return pointer.
86 **************************************************************************/
87 const char *base_name_translation(const struct base_type
*pbase
)
89 return name_translation(&pbase
->name
);
92 /**************************************************************************
93 Return the (untranslated) rule name of the base type.
94 You don't have to free the return pointer.
95 **************************************************************************/
96 const char *base_rule_name(const struct base_type
*pbase
)
98 return rule_name(&pbase
->name
);
101 /**************************************************************************
102 Returns base type matching rule name or NULL if there is no base type
104 **************************************************************************/
105 struct base_type
*base_type_by_rule_name(const char *name
)
107 const char *qs
= Qn_(name
);
109 base_type_iterate(pbase
) {
110 if (!fc_strcasecmp(base_rule_name(pbase
), qs
)) {
113 } base_type_iterate_end
;
118 /**************************************************************************
119 Returns base type matching the translated name, or NULL if there is no
120 base type with that name.
121 **************************************************************************/
122 struct base_type
*base_type_by_translated_name(const char *name
)
124 base_type_iterate(pbase
) {
125 if (0 == strcmp(base_name_translation(pbase
), name
)) {
128 } base_type_iterate_end
;
133 /****************************************************************************
134 Is there base of the given type cardinally near tile?
135 (Does not check ptile itself.)
136 ****************************************************************************/
137 bool is_base_card_near(const struct tile
*ptile
, const struct base_type
*pbase
)
139 cardinal_adjc_iterate(ptile
, adjc_tile
) {
140 if (tile_has_base(adjc_tile
, pbase
)) {
143 } cardinal_adjc_iterate_end
;
148 /****************************************************************************
149 Is there base of the given type near tile?
150 (Does not check ptile itself.)
151 ****************************************************************************/
152 bool is_base_near_tile(const struct tile
*ptile
, const struct base_type
*pbase
)
154 adjc_iterate(ptile
, adjc_tile
) {
155 if (tile_has_base(adjc_tile
, pbase
)) {
163 /**************************************************************************
164 Can unit build base to given tile?
165 **************************************************************************/
166 static bool base_can_be_built(const struct base_type
*pbase
,
167 const struct tile
*ptile
)
169 if (tile_terrain(ptile
)->base_time
== 0) {
170 /* Bases cannot be built on this terrain. */
174 if (!pbase
->buildable
) {
175 /* Base type not buildable. */
179 if (tile_has_base(ptile
, pbase
)) {
184 if (tile_city(ptile
) != NULL
&& pbase
->border_sq
>= 0) {
191 /****************************************************************************
192 Tells if player can build base to tile with suitable unit.
193 ****************************************************************************/
194 bool player_can_build_base(const struct base_type
*pbase
,
195 const struct player
*pplayer
,
196 const struct tile
*ptile
)
198 if (!base_can_be_built(pbase
, ptile
)) {
201 return are_reqs_active(pplayer
, NULL
, NULL
, ptile
,
202 NULL
, NULL
, NULL
, &pbase
->reqs
, RPT_POSSIBLE
);
205 /**************************************************************************
206 Can unit build base to given tile?
207 **************************************************************************/
208 bool can_build_base(const struct unit
*punit
, const struct base_type
*pbase
,
209 const struct tile
*ptile
)
211 if (!base_can_be_built(pbase
, ptile
)) {
214 return are_reqs_active(unit_owner(punit
), NULL
, NULL
, ptile
,
215 unit_type(punit
), NULL
, NULL
, &pbase
->reqs
,
219 /****************************************************************************
220 Returns base_type entry for an ID value.
221 ****************************************************************************/
222 struct base_type
*base_by_number(const Base_type_id id
)
224 if (id
< 0 || id
>= game
.control
.num_base_types
) {
227 return &base_types
[id
];
230 /**************************************************************************
231 Return the base index.
232 **************************************************************************/
233 Base_type_id
base_number(const struct base_type
*pbase
)
235 fc_assert_ret_val(NULL
!= pbase
, -1);
236 return pbase
->item_number
;
239 /**************************************************************************
240 Return the base index.
242 Currently same as base_number(), paired with base_count()
243 indicates use as an array index.
244 **************************************************************************/
245 Base_type_id
base_index(const struct base_type
*pbase
)
247 fc_assert_ret_val(NULL
!= pbase
, -1);
248 return pbase
- base_types
;
251 /**************************************************************************
252 Return the number of base_types.
253 **************************************************************************/
254 Base_type_id
base_count(void)
256 return game
.control
.num_base_types
;
259 /**************************************************************************
260 Return the last item of base_types.
261 **************************************************************************/
262 const struct base_type
*base_array_last(void)
264 if (game
.control
.num_base_types
> 0) {
265 return &base_types
[game
.control
.num_base_types
- 1];
270 /**************************************************************************
271 Return the first item of base_types.
272 **************************************************************************/
273 struct base_type
*base_array_first(void)
275 if (game
.control
.num_base_types
> 0) {
281 /****************************************************************************
282 Initialize base_type structures.
283 ****************************************************************************/
284 void base_types_init(void)
288 for (i
= 0; i
< ARRAY_SIZE(base_types
); i
++) {
289 base_types
[i
].item_number
= i
;
290 requirement_vector_init(&base_types
[i
].reqs
);
294 /****************************************************************************
295 Free the memory associated with base types
296 ****************************************************************************/
297 void base_types_free(void)
299 base_type_iterate(pbase
) {
300 requirement_vector_free(&pbase
->reqs
);
301 if (NULL
!= pbase
->helptext
) {
302 strvec_destroy(pbase
->helptext
);
303 pbase
->helptext
= NULL
;
305 } base_type_iterate_end
;
308 /**************************************************************************
309 Get best gui_type base for given parameters
310 **************************************************************************/
311 struct base_type
*get_base_by_gui_type(enum base_gui_type type
,
312 const struct unit
*punit
,
313 const struct tile
*ptile
)
315 base_type_iterate(pbase
) {
316 if (type
== pbase
->gui_type
317 && (!punit
|| can_build_base(punit
, pbase
, ptile
))) {
320 } base_type_iterate_end
;
325 /**************************************************************************
326 Can two bases coexist in same tile?
327 **************************************************************************/
328 bool can_bases_coexist(const struct base_type
*base1
, const struct base_type
*base2
)
330 if (base1
== base2
) {
334 return !BV_ISSET(base1
->conflicts
, base_index(base2
));
337 /**************************************************************************
338 Does this base type claim territory?
339 **************************************************************************/
340 bool territory_claiming_base(const struct base_type
*pbase
)
342 return pbase
->border_sq
>= 0;
345 /**************************************************************************
346 Who owns bases on tile
347 **************************************************************************/
348 struct player
*base_owner(const struct tile
*ptile
)
350 return tile_owner(ptile
);