Freeciv-3.3: /fast/freeciv/nightly/main/common/style.c Source File

Freeciv-3.3

Loading...
Searching...
No Matches
style.c
Go to the documentation of this file.
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.
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 "mem.h"
20
21/* common */
22#include "fc_types.h"
23#include "game.h"
24#include "name_translation.h"
25
26
27#include "style.h"
28
29 static struct nation_style *styles = NULL;
30
31 static struct music_style *music_styles = NULL;
32
33/**********************************************************************/
36 void styles_alloc(int count)
37{
38 int i;
39
40 styles = fc_malloc(count * sizeof(struct nation_style));
41
42 for (i = 0; i < count; i++) {
43 styles[i].id = i;
44 styles[i].ruledit_disabled = FALSE;
45 }
46}
47
48/**********************************************************************/
51 void styles_free(void)
52{
53 FC_FREE(styles);
54 styles = NULL;
55}
56
57/**********************************************************************/
60 int style_count(void)
61{
62 return game.control.num_styles;
63}
64
65/**********************************************************************/
68 int style_number(const struct nation_style *pstyle)
69{
70 fc_assert_ret_val(NULL != pstyle, -1);
71
72 return pstyle->id;
73}
74
75/**********************************************************************/
78 int style_index(const struct nation_style *pstyle)
79{
80 fc_assert_ret_val(NULL != pstyle, -1);
81
82 return pstyle - styles;
83}
84
85/**********************************************************************/
88 struct nation_style *style_by_number(int id)
89{
90 fc_assert_ret_val(id >= 0 && id < game.control.num_styles, NULL);
91
92 return &styles[id];
93}
94
95/**********************************************************************/
99 const char *style_name_translation(const struct nation_style *pstyle)
100{
101 return name_translation_get(&pstyle->name);
102}
103
104/**********************************************************************/
108 const char *style_rule_name(const struct nation_style *pstyle)
109{
110 return rule_name_get(&pstyle->name);
111}
112
113/**********************************************************************/
117 struct nation_style *style_by_rule_name(const char *name)
118{
119 const char *qs = Qn_(name);
120
121 styles_iterate(pstyle) {
122 if (!fc_strcasecmp(style_rule_name(pstyle), qs)) {
123 return pstyle;
124 }
125 } styles_iterate_end;
126
127 return NULL;
128}
129
130/**********************************************************************/
133 void music_styles_alloc(int count)
134{
135 int i;
136
137 music_styles = fc_malloc(count * sizeof(struct music_style));
138
139 for (i = 0; i < count; i++) {
140 music_styles[i].id = i;
141 requirement_vector_init(&(music_styles[i].reqs));
142 }
143}
144
145/**********************************************************************/
148 void music_styles_free(void)
149{
150 music_styles_iterate(pmus) {
151 requirement_vector_free(&(pmus->reqs));
152 } music_styles_iterate_end;
153
154 FC_FREE(music_styles);
155 music_styles = NULL;
156}
157
158/**********************************************************************/
161 int music_style_number(const struct music_style *pms)
162{
163 fc_assert_ret_val(NULL != pms, -1);
164
165 return pms->id;
166}
167
168/**********************************************************************/
171 struct music_style *music_style_by_number(int id)
172{
173 fc_assert_ret_val(id >= 0 && id < game.control.num_music_styles, NULL);
174
175 if (music_styles == NULL) {
176 return NULL;
177 }
178
179 return &music_styles[id];
180}
181
182/**********************************************************************/
185 struct music_style *player_music_style(struct player *plr)
186{
187 struct music_style *best = NULL;
188 const struct req_context plr_context = { .player = plr };
189
190 music_styles_iterate(pms) {
191 if (are_reqs_active(&plr_context, NULL, &pms->reqs, RPT_CERTAIN)) {
192 best = pms;
193 }
194 } music_styles_iterate_end;
195
196 return best;
197}
198
199/**********************************************************************/
202 int style_of_city(const struct city *pcity)
203{
204 return pcity->style;
205}
206
207/**********************************************************************/
210 int basic_city_style_for_style(struct nation_style *pstyle)
211{
212 int i;
213
214 for (i = game.control.num_city_styles - 1; i >= 0; i--) {
215 enum fc_tristate style_style = TRI_MAYBE;
216
217 requirement_vector_iterate(&city_styles[i].reqs, preq) {
218 if (preq->source.kind == VUT_STYLE
219 && preq->source.value.style == pstyle
220 && style_style != TRI_NO) {
221 style_style = TRI_YES;
222 } else {
223 /* No any other requirements allowed at the moment.
224 * TODO: Allow some other reqs */
225 style_style = TRI_NO;
226 break;
227 }
228 } requirement_vector_iterate_end;
229
230 if (style_style == TRI_YES) {
231 return i;
232 }
233 }
234
235 return -1;
236}
237
238/**********************************************************************/
241 int city_style(struct city *pcity)
242{
243 int i;
244 const struct req_context context = {
245 .player = city_owner(pcity),
246 .city = pcity,
247 .tile = city_tile(pcity),
248 };
249
250 for (i = game.control.num_city_styles - 1; i >= 0; i--) {
251 if (are_reqs_active(&context, NULL,
252 &city_styles[i].reqs, RPT_CERTAIN)) {
253 return i;
254 }
255 }
256
257 return 0;
258}
city_styles
static const struct city struct citystyle * city_styles
Definition city.c:84
city_tile
#define city_tile(_pcity_)
Definition city.h:564
city_owner
#define city_owner(_pcity_)
Definition city.h:563
incite_cost
char * incite_cost
Definition comments.c:74
id
int int id
Definition editgui_g.h:28
reqs
struct @21::@22 reqs
fc_types.h
RPT_CERTAIN
@ RPT_CERTAIN
Definition fc_types.h:678
Qn_
#define Qn_(String)
Definition fcintl.h:89
game
struct civ_game game
Definition game.c:61
game.h
name
const char * name
Definition inputfile.c:127
fc_assert_ret_val
#define fc_assert_ret_val(condition, val)
Definition log.h:194
mem.h
FC_FREE
#define FC_FREE(ptr)
Definition mem.h:41
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
name_translation.h
rule_name_get
static const char * rule_name_get(const struct name_translation *ptrans)
Definition name_translation.h:112
name_translation_get
static const char * name_translation_get(const struct name_translation *ptrans)
Definition name_translation.h:121
are_reqs_active
bool are_reqs_active(const struct req_context *context, const struct req_context *other_context, const struct requirement_vector *reqs, const enum req_problem_type prob_type)
Definition requirements.c:5807
requirement_vector_iterate_end
#define requirement_vector_iterate_end
Definition requirements.h:94
requirement_vector_iterate
#define requirement_vector_iterate(req_vec, preq)
Definition requirements.h:92
fc_tristate
fc_tristate
Definition shared.h:46
TRI_YES
@ TRI_YES
Definition shared.h:46
TRI_NO
@ TRI_NO
Definition shared.h:46
TRI_MAYBE
@ TRI_MAYBE
Definition shared.h:46
city
Definition city.h:320
city::style
int style
Definition city.h:327
city::tile
struct tile * tile
Definition city.h:322
civ_game::control
struct packet_ruleset_control control
Definition game.h:83
music_style
Definition style.h:29
music_style::id
int id
Definition style.h:30
nation_style
Definition style.h:22
nation_style::id
int id
Definition style.h:23
nation_style::ruledit_disabled
bool ruledit_disabled
Definition style.h:25
packet_ruleset_control::num_styles
int num_styles
Definition packets_gen.h:1450
packet_ruleset_control::num_music_styles
int num_music_styles
Definition packets_gen.h:1451
packet_ruleset_control::num_city_styles
int num_city_styles
Definition packets_gen.h:1454
player
Definition player.h:249
req_context
Definition requirements.h:100
req_context::player
const struct player * player
Definition requirements.h:101
city_style
int city_style(struct city *pcity)
Definition style.c:241
styles_free
void styles_free(void)
Definition style.c:51
style_by_rule_name
struct nation_style * style_by_rule_name(const char *name)
Definition style.c:117
styles
static struct nation_style * styles
Definition style.c:29
style_by_number
struct nation_style * style_by_number(int id)
Definition style.c:88
style_name_translation
const char * style_name_translation(const struct nation_style *pstyle)
Definition style.c:99
player_music_style
struct music_style * player_music_style(struct player *plr)
Definition style.c:185
styles_alloc
void styles_alloc(int count)
Definition style.c:36
music_style_number
int music_style_number(const struct music_style *pms)
Definition style.c:161
style_of_city
int style_of_city(const struct city *pcity)
Definition style.c:202
music_style_by_number
struct music_style * music_style_by_number(int id)
Definition style.c:171
style_rule_name
const char * style_rule_name(const struct nation_style *pstyle)
Definition style.c:108
style_number
int style_number(const struct nation_style *pstyle)
Definition style.c:68
music_styles
static struct music_style * music_styles
Definition style.c:31
music_styles_free
void music_styles_free(void)
Definition style.c:148
music_styles_alloc
void music_styles_alloc(int count)
Definition style.c:133
style_count
int style_count(void)
Definition style.c:60
style_index
int style_index(const struct nation_style *pstyle)
Definition style.c:78
basic_city_style_for_style
int basic_city_style_for_style(struct nation_style *pstyle)
Definition style.c:210
style.h
music_styles_iterate
#define music_styles_iterate(_p)
Definition style.h:72
music_styles_iterate_end
#define music_styles_iterate_end
Definition style.h:79
styles_iterate
#define styles_iterate(_p)
Definition style.h:46
styles_iterate_end
#define styles_iterate_end
Definition style.h:52
fc_strcasecmp
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:186
FALSE
#define FALSE
Definition support.h:47