I18n markup: TRANS comments and missing no-c-format.
[freeciv.git] / common / base.c
blob9b12a10d202952de3d15cf9884a3571ed98c9671
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)
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 "string_vector.h"
23 /* common */
24 #include "game.h"
25 #include "tile.h"
26 #include "unit.h"
28 #include "base.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) {
65 return FALSE;
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
103 with such name.
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)) {
111 return pbase;
113 } base_type_iterate_end;
115 return NULL;
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)) {
126 return pbase;
128 } base_type_iterate_end;
130 return NULL;
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)) {
141 return TRUE;
143 } cardinal_adjc_iterate_end;
145 return FALSE;
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)) {
156 return TRUE;
158 } adjc_iterate_end;
160 return FALSE;
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. */
171 return FALSE;
174 if (!pbase->buildable) {
175 /* Base type not buildable. */
176 return FALSE;
179 if (tile_has_base(ptile, pbase)) {
180 /* Exist already */
181 return FALSE;
184 if (tile_city(ptile) != NULL && pbase->border_sq >= 0) {
185 return FALSE;
188 return TRUE;
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)) {
199 return FALSE;
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)) {
212 return FALSE;
214 return are_reqs_active(unit_owner(punit), NULL, NULL, ptile,
215 unit_type(punit), NULL, NULL, &pbase->reqs,
216 RPT_CERTAIN);
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) {
225 return NULL;
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];
267 return NULL;
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) {
276 return base_types;
278 return NULL;
281 /****************************************************************************
282 Initialize base_type structures.
283 ****************************************************************************/
284 void base_types_init(void)
286 int i;
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))) {
318 return pbase;
320 } base_type_iterate_end;
322 return NULL;
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) {
331 return TRUE;
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);