Removed silencing of gtk warning logs from gtk3.22-client.
[freeciv.git] / common / style.c
blobeb9fa98778d672a5bb5f5e810f79fa3ce1bce894
1 /***********************************************************************
2 Freeciv - Copyright (C) 2005 - The Freeciv Project
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 "mem.h"
21 /* common */
22 #include "fc_types.h"
23 #include "game.h"
24 #include "name_translation.h"
27 #include "style.h"
29 static struct nation_style *styles = NULL;
31 static struct music_style *music_styles = NULL;
33 /****************************************************************************
34 Initialise styles structures.
35 ****************************************************************************/
36 void styles_alloc(int count)
38 int i;
40 styles = fc_malloc(count * sizeof(struct nation_style));
42 for (i = 0; i < count; i++) {
43 styles[i].id = i;
47 /****************************************************************************
48 Free the memory associated with styles
49 ****************************************************************************/
50 void styles_free(void)
52 FC_FREE(styles);
53 styles = NULL;
56 /**************************************************************************
57 Return the number of styles.
58 **************************************************************************/
59 int style_count(void)
61 return game.control.num_styles;
64 /**************************************************************************
65 Return the style id.
66 **************************************************************************/
67 int style_number(const struct nation_style *pstyle)
69 fc_assert_ret_val(NULL != pstyle, -1);
71 return pstyle->id;
74 /**************************************************************************
75 Return the style index.
76 **************************************************************************/
77 int style_index(const struct nation_style *pstyle)
79 fc_assert_ret_val(NULL != pstyle, -1);
81 return pstyle - styles;
84 /****************************************************************************
85 Return style of given id.
86 ****************************************************************************/
87 struct nation_style *style_by_number(int id)
89 fc_assert_ret_val(id >= 0 && id < game.control.num_styles, NULL);
91 return &styles[id];
94 /**************************************************************************
95 Return the (translated) name of the style.
96 You don't have to free the return pointer.
97 **************************************************************************/
98 const char *style_name_translation(const struct nation_style *pstyle)
100 return name_translation_get(&pstyle->name);
103 /**************************************************************************
104 Return the (untranslated) rule name of the style.
105 You don't have to free the return pointer.
106 **************************************************************************/
107 const char *style_rule_name(const struct nation_style *pstyle)
109 return rule_name_get(&pstyle->name);
112 /**************************************************************************
113 Returns style matching rule name or NULL if there is no style
114 with such name.
115 **************************************************************************/
116 struct nation_style *style_by_rule_name(const char *name)
118 const char *qs = Qn_(name);
120 styles_iterate(pstyle) {
121 if (!fc_strcasecmp(style_rule_name(pstyle), qs)) {
122 return pstyle;
124 } styles_iterate_end;
126 return NULL;
129 /****************************************************************************
130 Initialise music styles structures.
131 ****************************************************************************/
132 void music_styles_alloc(int count)
134 int i;
136 music_styles = fc_malloc(count * sizeof(struct music_style));
138 for (i = 0; i < count; i++) {
139 music_styles[i].id = i;
140 requirement_vector_init(&(music_styles[i].reqs));
144 /****************************************************************************
145 Free the memory associated with music styles
146 ****************************************************************************/
147 void music_styles_free(void)
149 music_styles_iterate(pmus) {
150 requirement_vector_free(&(pmus->reqs));
151 } music_styles_iterate_end;
153 FC_FREE(music_styles);
154 music_styles = NULL;
157 /**************************************************************************
158 Return the music style id.
159 **************************************************************************/
160 int music_style_number(const struct music_style *pms)
162 fc_assert_ret_val(NULL != pms, -1);
164 return pms->id;
167 /****************************************************************************
168 Return music style of given id.
169 ****************************************************************************/
170 struct music_style *music_style_by_number(int id)
172 fc_assert_ret_val(id >= 0 && id < game.control.num_music_styles, NULL);
174 if (music_styles == NULL) {
175 return NULL;
178 return &music_styles[id];
181 /****************************************************************************
182 Return music style for player
183 ****************************************************************************/
184 struct music_style *player_music_style(struct player *plr)
186 struct music_style *best = NULL;
188 music_styles_iterate(pms) {
189 if (are_reqs_active(plr, NULL, NULL, NULL, NULL,
190 NULL, NULL, NULL, NULL, &pms->reqs,
191 RPT_CERTAIN)) {
192 best = pms;
194 } music_styles_iterate_end;
196 return best;
199 /**************************************************************************
200 Evaluate which style should be used to draw a city.
201 **************************************************************************/
202 int style_of_city(const struct city *pcity)
204 return pcity->style;
207 /**************************************************************************
208 Return basic city style representing nation style.
209 **************************************************************************/
210 int basic_city_style_for_style(struct nation_style *pstyle)
212 enum fc_tristate style_style;
213 int i;
215 for (i = game.control.styles_count - 1; i >= 0; i--) {
216 style_style = TRI_MAYBE;
218 requirement_vector_iterate(&city_styles[i].reqs, preq) {
219 if (preq->source.kind == VUT_STYLE
220 && preq->source.value.style == pstyle
221 && style_style != TRI_NO) {
222 style_style = TRI_YES;
223 } else {
224 /* No any other requirements allowed at the moment.
225 * TODO: Allow some other reqs */
226 style_style = TRI_NO;
227 break;
229 } requirement_vector_iterate_end;
231 if (style_style == TRI_YES) {
232 break;
236 if (style_style == TRI_YES) {
237 return i;
240 return -1;
243 /**************************************************************************
244 Return citystyle of the city.
245 **************************************************************************/
246 int city_style(struct city *pcity)
248 int i;
249 struct player *plr = city_owner(pcity);
251 for (i = game.control.styles_count - 1; i >= 0; i--) {
252 if (are_reqs_active(plr, NULL, pcity, NULL, city_tile(pcity),
253 NULL, NULL, NULL, NULL, &city_styles[i].reqs,
254 RPT_CERTAIN)) {
255 return i;
259 return 0;