Freeciv21
Develop your civilization from humble roots to a global empire
style.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 1996-2020 Freeciv21 and Freeciv contributors. This file
3  is part of Freeciv21. Freeciv21 is free software:
4 |\_/|,,_____,~~` you can redistribute it and/or modify it under the
5 (.".)~~ )`~}} terms of the GNU General Public License as published
6  \o/\ /---~\\ ~}} by the Free Software Foundation, either version 3 of
7  _// _// ~} the License, or (at your option) any later version.
8  You should have received a copy of the GNU General
9  Public License along with Freeciv21. If not, see
10  https://www.gnu.org/licenses/.
11  */
12 
13 // utility
14 #include "fcintl.h"
15 
16 // common
17 #include "fc_types.h"
18 #include "game.h"
19 #include "name_translation.h"
20 
21 #include "style.h"
22 
23 static struct nation_style *styles = nullptr;
24 
25 static struct music_style *music_styles = nullptr;
26 
30 void styles_alloc(int count)
31 {
32  int i;
33 
34  styles = new nation_style[count];
35 
36  for (i = 0; i < count; i++) {
37  styles[i].id = i;
38  styles[i].ruledit_disabled = false;
39  }
40 }
41 
46 {
47  delete[] styles;
48  styles = nullptr;
49 }
50 
54 int style_number(const struct nation_style *pstyle)
55 {
56  fc_assert_ret_val(nullptr != pstyle, 0);
57 
58  return pstyle->id;
59 }
60 
64 int style_index(const struct nation_style *pstyle)
65 {
66  fc_assert_ret_val(nullptr != pstyle, 0);
67 
68  return pstyle - styles;
69 }
70 
75 {
76  fc_assert_ret_val(id >= 0 && id < game.control.num_styles, nullptr);
77 
78  return &styles[id];
79 }
80 
85 const char *style_name_translation(const struct nation_style *pstyle)
86 {
87  return name_translation_get(&pstyle->name);
88 }
89 
94 const char *style_rule_name(const struct nation_style *pstyle)
95 {
96  return rule_name_get(&pstyle->name);
97 }
98 
104 {
105  const char *qs = Qn_(name);
106 
107  styles_iterate(pstyle)
108  {
109  if (!fc_strcasecmp(style_rule_name(pstyle), qs)) {
110  return pstyle;
111  }
112  }
114 
115  return nullptr;
116 }
117 
121 void music_styles_alloc(int count)
122 {
123  int i;
124 
125  music_styles = new music_style[count];
126 
127  for (i = 0; i < count; i++) {
128  music_styles[i].id = i;
129  requirement_vector_init(&(music_styles[i].reqs));
130  }
131 }
132 
137 {
138  music_styles_iterate(pmus) { requirement_vector_free(&(pmus->reqs)); }
140 
141  delete[] music_styles;
142  music_styles = nullptr;
143 }
144 
148 int music_style_number(const struct music_style *pms)
149 {
150  fc_assert_ret_val(nullptr != pms, -1);
151 
152  return pms->id;
153 }
154 
159 {
160  fc_assert_ret_val(id >= 0 && id < game.control.num_music_styles, nullptr);
161 
162  if (music_styles == nullptr) {
163  return nullptr;
164  }
165 
166  return &music_styles[id];
167 }
168 
173 {
174  struct music_style *best = nullptr;
175 
177  {
178  if (are_reqs_active(plr, nullptr, nullptr, nullptr, nullptr, nullptr,
179  nullptr, nullptr, nullptr, nullptr, &pms->reqs,
180  RPT_CERTAIN)) {
181  best = pms;
182  }
183  }
185 
186  return best;
187 }
188 
192 int style_of_city(const struct city *pcity) { return pcity->style; }
193 
198 {
199  enum fc_tristate style_style = TRI_MAYBE;
200  int i;
201 
202  for (i = game.control.styles_count - 1; i >= 0; i--) {
203  style_style = TRI_MAYBE;
204 
206  {
207  if (preq->source.kind == VUT_STYLE
208  && preq->source.value.style == pstyle) {
209  style_style = TRI_YES;
210  } else {
211  /* No any other requirements allowed at the moment.
212  * TODO: Allow some other reqs */
213  style_style = TRI_NO;
214  break;
215  }
216  }
218 
219  if (style_style == TRI_YES) {
220  break;
221  }
222  }
223 
224  if (style_style == TRI_YES) {
225  return i;
226  }
227 
228  return -1;
229 }
230 
234 int city_style(struct city *pcity)
235 {
236  int i;
237  struct player *plr = city_owner(pcity);
238 
239  for (i = game.control.styles_count - 1; i >= 0; i--) {
240  if (are_reqs_active(plr, nullptr, pcity, nullptr, city_tile(pcity),
241  nullptr, nullptr, nullptr, nullptr, nullptr,
242  &city_styles[i].reqs, RPT_CERTAIN)) {
243  return i;
244  }
245  }
246 
247  return 0;
248 }
struct player * city_owner(const struct city *pcity)
Return the owner of the city.
Definition: city.cpp:1083
struct citystyle * city_styles
Definition: city.cpp:78
struct tile * city_tile(const struct city *pcity)
Return the tile location of the city.
Definition: city.cpp:1095
struct @19::@20 reqs
@ RPT_CERTAIN
Definition: fc_types.h:568
#define Qn_(String)
Definition: fcintl.h:66
struct civ_game game
Definition: game.cpp:47
const char * name
Definition: inputfile.cpp:118
#define fc_assert_ret_val(condition, val)
Definition: log.h:114
static const char * rule_name_get(const struct name_translation *ptrans)
static const char * name_translation_get(const struct name_translation *ptrans)
bool are_reqs_active(const struct player *target_player, const struct player *other_player, const struct city *target_city, const struct impr_type *target_building, const struct tile *target_tile, const struct unit *target_unit, const struct unit_type *target_unittype, const struct output_type *target_output, const struct specialist *target_specialist, const struct action *target_action, const struct requirement_vector *reqs, const enum req_problem_type prob_type, const enum vision_layer vision_layer, const enum national_intelligence nintel)
Checks the requirement(s) to see if they are active on the given target.
#define requirement_vector_iterate_end
Definition: requirements.h:80
#define requirement_vector_iterate(req_vec, preq)
Definition: requirements.h:78
fc_tristate
Definition: shared.h:42
@ TRI_YES
Definition: shared.h:42
@ TRI_NO
Definition: shared.h:42
@ TRI_MAYBE
Definition: shared.h:42
Definition: city.h:291
int style
Definition: city.h:297
struct packet_ruleset_control control
Definition: game.h:74
int id
Definition: style.h:25
struct name_translation name
Definition: style.h:20
int id
Definition: style.h:19
bool ruledit_disabled
Definition: style.h:21
Definition: player.h:231
struct music_style * player_music_style(struct player *plr)
Return music style for player.
Definition: style.cpp:172
int city_style(struct city *pcity)
Return citystyle of the city.
Definition: style.cpp:234
struct nation_style * style_by_rule_name(const char *name)
Returns style matching rule name or nullptr if there is no style with such name.
Definition: style.cpp:103
const char * style_rule_name(const struct nation_style *pstyle)
Return the (untranslated) rule name of the style.
Definition: style.cpp:94
void music_styles_free()
Free the memory associated with music styles.
Definition: style.cpp:136
static struct nation_style * styles
Definition: style.cpp:23
struct music_style * music_style_by_number(int id)
Return music style of given id.
Definition: style.cpp:158
void styles_alloc(int count)
Initialise styles structures.
Definition: style.cpp:30
int music_style_number(const struct music_style *pms)
Return the music style id.
Definition: style.cpp:148
void styles_free()
Free the memory associated with styles.
Definition: style.cpp:45
struct nation_style * style_by_number(int id)
Return style of given id.
Definition: style.cpp:74
int style_of_city(const struct city *pcity)
Evaluate which style should be used to draw a city.
Definition: style.cpp:192
int style_number(const struct nation_style *pstyle)
Return the style id.
Definition: style.cpp:54
static struct music_style * music_styles
Definition: style.cpp:25
void music_styles_alloc(int count)
Initialise music styles structures.
Definition: style.cpp:121
int style_index(const struct nation_style *pstyle)
Return the style index.
Definition: style.cpp:64
int basic_city_style_for_style(struct nation_style *pstyle)
Return basic city style representing nation style.
Definition: style.cpp:197
const char * style_name_translation(const struct nation_style *pstyle)
Return the (translated) name of the style.
Definition: style.cpp:85
#define music_styles_iterate(_p)
Definition: style.h:68
#define music_styles_iterate_end
Definition: style.h:75
#define styles_iterate(_p)
Definition: style.h:40
#define styles_iterate_end
Definition: style.h:46
int fc_strcasecmp(const char *str0, const char *str1)
Compare strings like strcmp(), but ignoring case.
Definition: support.cpp:89