Freeciv21
Develop your civilization from humble roots to a global empire
validity.cpp
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 
14 // utility
15 #include "support.h"
16 
17 // common
18 #include "disaster.h"
19 #include "game.h"
20 #include "government.h"
21 #include "improvement.h"
22 #include "requirements.h"
23 #include "specialist.h"
24 #include "style.h"
25 #include "tech.h"
26 
27 #include "validity.h"
28 
30  bool needed;
31  struct universal *uni;
34 };
35 
39 static bool effect_list_universal_needed_cb(struct effect *peffect,
40  void *data)
41 {
42  struct effect_list_cb_data *cbdata = (struct effect_list_cb_data *) data;
43 
44  if (universal_is_mentioned_by_requirements(&peffect->reqs, cbdata->uni)) {
45  cbdata->cb(R__("Effect"), cbdata->requirers_data);
46  cbdata->needed = true;
47  }
48 
49  // Always continue to next until all effects checked
50  return true;
51 }
52 
57  void *data)
58 {
59  bool needed = false;
60  bool needed_by_music_style = false;
61  int i;
62  struct effect_list_cb_data cb_data;
63 
65  {
66  if (universal_is_mentioned_by_requirements(&pdis->reqs, uni)) {
67  cb(disaster_rule_name(pdis), data);
68  needed = true;
69  }
70  }
72 
73  improvement_iterate(pimprove)
74  {
75  if (universal_is_mentioned_by_requirements(&pimprove->reqs, uni)
76  || universal_is_mentioned_by_requirements(&pimprove->obsolete_by,
77  uni)) {
78  cb(improvement_rule_name(pimprove), data);
79  needed = true;
80  }
81  }
83 
84  for (auto &pgov : governments) {
86  cb(government_rule_name(&pgov), data);
87  needed = true;
88  }
89  }
90 
92  {
93  struct specialist *psp = specialist_by_number(sp);
94 
96  cb(specialist_rule_name(psp), data);
97  needed = true;
98  }
99  }
101 
102  extra_type_iterate(pextra)
103  {
104  if (universal_is_mentioned_by_requirements(&pextra->reqs, uni)
105  || universal_is_mentioned_by_requirements(&pextra->rmreqs, uni)) {
106  cb(extra_rule_name(pextra), data);
107  needed = true;
108  }
109  }
111 
112  goods_type_iterate(pgood)
113  {
114  if (universal_is_mentioned_by_requirements(&pgood->reqs, uni)) {
115  cb(goods_rule_name(pgood), data);
116  needed = true;
117  }
118  }
120 
121  action_iterate(act)
122  {
124  {
125  if (universal_is_mentioned_by_requirements(&(enabler->actor_reqs), uni)
126  || universal_is_mentioned_by_requirements(&(enabler->target_reqs),
127  uni)) {
128  cb(R__("Action Enabler"), data);
129  needed = true;
130  }
131  }
133  }
135 
136  for (i = 0; i < game.control.styles_count; i++) {
138  cb(city_style_rule_name(i), data);
139  needed = true;
140  }
141  }
142 
144  {
145  if (universal_is_mentioned_by_requirements(&pmus->reqs, uni)) {
146  needed_by_music_style = true;
147  }
148  }
150 
151  if (needed_by_music_style) {
152  cb(R__("Music Style"), data);
153  needed = true;
154  }
155 
156  cb_data.needed = false;
157  cb_data.uni = uni;
158  cb_data.cb = cb;
159  cb_data.requirers_data = data;
160 
162  needed |= cb_data.needed;
163 
164  return needed;
165 }
166 
170 bool is_tech_needed(struct advance *padv, requirers_cb cb, void *data)
171 {
172  struct universal uni = {.value = {.advance = padv}, .kind = VUT_ADVANCE};
173  bool needed = false;
174 
175  advance_iterate(A_FIRST, pdependant)
176  {
177  if (pdependant->require[AR_ONE] == padv
178  || pdependant->require[AR_TWO] == padv
179  || pdependant->require[AR_ROOT] == padv) {
180  cb(advance_rule_name(pdependant), data);
181  needed = true;
182  }
183  }
185 
186  unit_type_iterate(ptype)
187  {
188  if (ptype->require_advance == padv) {
189  cb(utype_rule_name(ptype), data);
190  needed = true;
191  }
192  }
194 
195  extra_type_iterate(pextra)
196  {
197  if (pextra->visibility_req == advance_number(padv)) {
198  char buf[512];
199 
200  fc_snprintf(buf, sizeof(buf), "%s visibility",
201  extra_rule_name(pextra));
202  cb(buf, data);
203  }
204  }
206 
207  needed |= is_universal_needed(&uni, cb, data);
208 
209  return needed;
210 }
211 
215 bool is_building_needed(struct impr_type *pimpr, requirers_cb cb, void *data)
216 {
217  struct universal uni = {.value = {.building = pimpr},
218  .kind = VUT_IMPROVEMENT};
219  bool needed = false;
220 
221  needed |= is_universal_needed(&uni, cb, data);
222 
223  return needed;
224 }
225 
229 bool is_utype_needed(struct unit_type *ptype, requirers_cb cb, void *data)
230 {
231  struct universal uni = {.value = {.utype = ptype}, .kind = VUT_UTYPE};
232  bool needed = false;
233 
234  needed |= is_universal_needed(&uni, cb, data);
235 
236  return needed;
237 }
238 
242 bool is_good_needed(struct goods_type *pgood, requirers_cb cb, void *data)
243 {
244  struct universal uni = {.value = {.good = pgood}, .kind = VUT_GOOD};
245  bool needed = false;
246 
247  needed |= is_universal_needed(&uni, cb, data);
248 
249  return needed;
250 }
251 
255 bool is_extra_needed(struct extra_type *pextra, requirers_cb cb, void *data)
256 {
257  struct universal uni = {.value = {.extra = pextra}, .kind = VUT_EXTRA};
258  bool needed = false;
259  bool conflicts = false;
260  bool hides = false;
261  int id = extra_index(pextra);
262 
263  extra_type_iterate(requirer)
264  {
265  conflicts |= BV_ISSET(requirer->conflicts, id);
266  hides |= BV_ISSET(requirer->hidden_by, id);
267  }
269 
270  if (conflicts) {
271  cb(R__("Conflicting extra"), data);
272  }
273  if (hides) {
274  cb(R__("Hidden extra"), data);
275  }
276  needed |= conflicts | hides;
277 
278  needed |= is_universal_needed(&uni, cb, data);
279 
280  return needed;
281 }
282 
286 bool is_terrain_needed(struct terrain *pterr, requirers_cb cb, void *data)
287 {
288  struct universal uni = {.value = {.terrain = pterr}, .kind = VUT_TERRAIN};
289  bool needed = false;
290 
291  needed |= is_universal_needed(&uni, cb, data);
292 
293  return needed;
294 }
295 
300  void *data)
301 {
302  struct universal uni = {.value = {.govern = pgov}, .kind = VUT_GOVERNMENT};
303  bool needed = false;
304 
305  needed |= is_universal_needed(&uni, cb, data);
306 
307  return needed;
308 }
309 
311  bool needed;
312  struct multiplier *pmul;
315 };
316 
320 static bool effect_list_multiplier_cb(struct effect *peffect, void *data)
321 {
322  struct effect_list_multiplier_data *cbdata =
323  (struct effect_list_multiplier_data *) data;
324 
325  if (peffect->multiplier == cbdata->pmul) {
326  cbdata->cb(R__("Effect"), cbdata->requirers_data);
327  cbdata->needed = true;
328  }
329 
330  // Always continue to next until all effects checked
331  return true;
332 }
333 
338  void *data)
339 {
340  struct effect_list_multiplier_data cb_data;
341  bool needed = false;
342 
343  cb_data.needed = false;
344  cb_data.pmul = pmul;
345  cb_data.cb = cb;
346  cb_data.requirers_data = data;
347 
349  needed |= cb_data.needed;
350 
351  return needed;
352 }
struct action_enabler_list * action_enablers_for_action(action_id action)
Get all enablers for an action in the current ruleset.
Definition: actions.cpp:1884
#define action_enabler_list_iterate_end
Definition: actions.h:376
#define action_iterate_end
Definition: actions.h:383
#define action_enabler_list_iterate(action_enabler_list, aenabler)
Definition: actions.h:374
#define action_iterate(_act_)
Definition: actions.h:378
bool BV_ISSET(const BV &bv, int bit)
Definition: bitvector.h:37
struct citystyle * city_styles
Definition: city.cpp:78
const char * city_style_rule_name(const int style)
Return the (untranslated) rule name of the city style.
Definition: city.cpp:1651
const char * disaster_rule_name(struct disaster_type *pdis)
Return untranslated name of this disaster type.
Definition: disaster.cpp:91
#define disaster_type_iterate(_p)
Definition: disaster.h:70
#define disaster_type_iterate_end
Definition: disaster.h:76
struct @19::@20 reqs
bool iterate_effect_cache(iec_cb cb, void *data)
Iterate through all the effects in cache, and call callback for each.
Definition: effects.cpp:1259
const char * extra_rule_name(const struct extra_type *pextra)
Return the (untranslated) rule name of the extra type.
Definition: extras.cpp:174
#define extra_type_iterate(_p)
Definition: extras.h:279
#define extra_type_iterate_end
Definition: extras.h:285
#define extra_index(_e_)
Definition: extras.h:163
#define R__(String)
Definition: fcintl.h:58
struct civ_game game
Definition: game.cpp:47
const char * government_rule_name(const struct government *pgovern)
Return the (untranslated) rule name of the government.
Definition: government.cpp:126
std::vector< government > governments
Definition: government.cpp:28
const char * improvement_rule_name(const struct impr_type *pimprove)
Return the (untranslated) rule name of the improvement.
#define improvement_iterate_end
Definition: improvement.h:199
#define improvement_iterate(_p)
Definition: improvement.h:193
bool universal_is_mentioned_by_requirements(const struct requirement_vector *reqs, const struct universal *psource)
Returns TRUE iff the universal 'psource' is directly mentioned by any of the requirements in 'reqs'.
const char * specialist_rule_name(const struct specialist *sp)
Return the (untranslated) rule name of the specialist type.
Definition: specialist.cpp:142
struct specialist * specialist_by_number(const Specialist_type_id id)
Return the specialist pointer for the given number.
Definition: specialist.cpp:92
#define specialist_type_iterate_end
Definition: specialist.h:73
#define specialist_type_iterate(sp)
Definition: specialist.h:67
Definition: tech.h:113
struct advance * require[AR_SIZE]
Definition: tech.h:120
struct packet_ruleset_control control
Definition: game.h:74
requirers_cb cb
Definition: validity.cpp:32
struct universal * uni
Definition: validity.cpp:31
struct multiplier * pmul
Definition: validity.cpp:312
struct multiplier * multiplier
Definition: effects.h:331
struct requirement_vector reqs
Definition: effects.h:340
struct requirement_vector reqs
Definition: specialist.h:32
universals_u value
Definition: fc_types.h:739
#define music_styles_iterate(_p)
Definition: style.h:68
#define music_styles_iterate_end
Definition: style.h:75
int fc_snprintf(char *str, size_t n, const char *format,...)
See also fc_utf8_snprintf_trunc(), fc_utf8_snprintf_rep().
Definition: support.cpp:537
const char * advance_rule_name(const struct advance *padvance)
Return the (untranslated) rule name of the advance/technology.
Definition: tech.cpp:283
Tech_type_id advance_number(const struct advance *padvance)
Return the advance index.
Definition: tech.cpp:85
@ AR_TWO
Definition: tech.h:104
@ AR_ROOT
Definition: tech.h:104
@ AR_ONE
Definition: tech.h:104
#define advance_iterate(_start, _p)
Definition: tech.h:232
#define A_FIRST
Definition: tech.h:37
#define advance_iterate_end
Definition: tech.h:238
const char * goods_rule_name(struct goods_type *pgood)
Return untranslated name of this goods type.
#define goods_type_iterate_end
Definition: traderoutes.h:224
#define goods_type_iterate(_p)
Definition: traderoutes.h:218
const struct unit_type * utype
Definition: fc_types.h:585
struct extra_type * extra
Definition: fc_types.h:586
struct government * govern
Definition: fc_types.h:578
const struct impr_type * building
Definition: fc_types.h:579
struct advance * advance
Definition: fc_types.h:577
struct goods_type * good
Definition: fc_types.h:591
struct terrain * terrain
Definition: fc_types.h:583
const char * utype_rule_name(const struct unit_type *punittype)
Return the (untranslated) rule name of the unit type.
Definition: unittype.cpp:1274
#define unit_type_iterate(_p)
Definition: unittype.h:785
#define unit_type_iterate_end
Definition: unittype.h:791
static bool is_universal_needed(struct universal *uni, requirers_cb cb, void *data)
Check if anything in ruleset needs universal.
Definition: validity.cpp:56
bool is_multiplier_needed(struct multiplier *pmul, requirers_cb cb, void *data)
Check if anything in ruleset needs multiplier.
Definition: validity.cpp:337
bool is_tech_needed(struct advance *padv, requirers_cb cb, void *data)
Check if anything in ruleset needs tech.
Definition: validity.cpp:170
static bool effect_list_multiplier_cb(struct effect *peffect, void *data)
Callback to check if effect needs universal.
Definition: validity.cpp:320
bool is_terrain_needed(struct terrain *pterr, requirers_cb cb, void *data)
Check if anything in ruleset needs terrain type.
Definition: validity.cpp:286
bool is_good_needed(struct goods_type *pgood, requirers_cb cb, void *data)
Check if anything in ruleset needs goods type.
Definition: validity.cpp:242
bool is_government_needed(struct government *pgov, requirers_cb cb, void *data)
Check if anything in ruleset needs government.
Definition: validity.cpp:299
static bool effect_list_universal_needed_cb(struct effect *peffect, void *data)
Callback to check if effect needs universal.
Definition: validity.cpp:39
bool is_utype_needed(struct unit_type *ptype, requirers_cb cb, void *data)
Check if anything in ruleset needs unit type.
Definition: validity.cpp:229
bool is_extra_needed(struct extra_type *pextra, requirers_cb cb, void *data)
Check if anything in ruleset needs extra type.
Definition: validity.cpp:255
bool is_building_needed(struct impr_type *pimpr, requirers_cb cb, void *data)
Check if anything in ruleset needs building.
Definition: validity.cpp:215
void(* requirers_cb)(const char *msg, void *data)
Definition: validity.h:16