Freeciv21
Develop your civilization from humble roots to a global empire
univ_value.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 // common
15 #include "achievements.h"
16 #include "game.h"
17 #include "government.h"
18 #include "nation.h"
19 #include "server_settings.h"
20 #include "specialist.h"
21 #include "style.h"
22 #include "tech.h"
23 #include "traderoutes.h"
24 // server
25 #include "rssanity.h"
26 #include "settings.h"
27 
28 #include "univ_value.h"
29 
37 {
38  switch (src->kind) {
39  case VUT_NONE:
40  // Value of None should never be used
41  return true;
42  case VUT_ADVANCE:
43  if (game.control.num_tech_types <= 0) {
44  return false;
45  }
47  return true;
48  case VUT_GOVERNMENT:
50  return true;
51  case VUT_IMPROVEMENT:
52  if (game.control.num_impr_types <= 0) {
53  return false;
54  }
56  return true;
57  case VUT_TERRAIN:
59  return true;
60  case VUT_NATION:
61  if (game.control.nation_count <= 0) {
62  return false;
63  }
64  src->value.nation = nation_by_number(0);
65  return true;
66  case VUT_UTYPE:
67  if (game.control.num_unit_types <= 0) {
68  return false;
69  }
70  src->value.utype = utype_by_number(0);
71  return true;
72  case VUT_UTFLAG:
73  src->value.unitflag = (enum unit_type_flag_id) 0;
74  return true;
75  case VUT_UCLASS:
76  if (game.control.num_unit_classes <= 0) {
77  return false;
78  }
79  src->value.uclass = uclass_by_number(0);
80  return true;
81  case VUT_UCFLAG:
82  src->value.unitclassflag = (enum unit_class_flag_id) 0;
83  return true;
84  case VUT_OTYPE:
85  src->value.outputtype = (enum output_type_id) 0;
86  return true;
87  case VUT_SPECIALIST:
88  if (game.control.num_specialist_types <= 0) {
89  return false;
90  }
92  return true;
93  case VUT_MINSIZE:
94  src->value.minsize = 0;
95  return true;
96  case VUT_AI_LEVEL:
97  src->value.ai_level = AI_LEVEL_CHEATING;
98  return true;
99  case VUT_TERRAINCLASS:
100  src->value.terrainclass = TC_LAND;
101  return true;
102  case VUT_MINYEAR:
103  src->value.minyear = 0;
104  return true;
105  case VUT_MINCALFRAG:
106  src->value.mincalfrag = 0;
107  return true;
108  case VUT_TERRAINALTER:
109  src->value.terrainalter = TA_CAN_IRRIGATE;
110  return true;
111  case VUT_CITYTILE:
112  src->value.citytile = CITYT_CENTER;
113  return true;
114  case VUT_CITYSTATUS:
115  src->value.citystatus = CITYS_OWNED_BY_ORIGINAL;
116  return true;
117  case VUT_GOOD:
118  if (game.control.num_goods_types <= 0) {
119  return false;
120  }
121  src->value.good = goods_by_number(0);
122  return true;
123  case VUT_TERRFLAG:
124  src->value.terrainflag = TER_NO_BARBS;
125  return true;
126  case VUT_NATIONALITY:
127  if (game.control.nation_count <= 0) {
128  return false;
129  }
131  return true;
132  case VUT_BASEFLAG:
133  src->value.baseflag = BF_NOT_AGGRESSIVE;
134  return true;
135  case VUT_ROADFLAG:
136  src->value.roadflag = RF_RIVER;
137  return true;
138  case VUT_EXTRA:
139  if (game.control.num_extra_types <= 0) {
140  return false;
141  }
142  src->value.extra = extra_by_number(0);
143  return true;
144  case VUT_TECHFLAG:
145  src->value.techflag = TF_BONUS_TECH;
146  return true;
147  case VUT_ACHIEVEMENT:
148  if (game.control.num_achievement_types <= 0) {
149  return false;
150  }
152  return true;
153  case VUT_DIPLREL:
154  src->value.diplrel = DS_WAR;
155  return true;
156  case VUT_MAXTILEUNITS:
157  src->value.max_tile_units = 0;
158  return true;
159  case VUT_STYLE:
160  if (game.control.num_styles <= 0) {
161  return false;
162  }
163  src->value.style = style_by_number(0);
164  return true;
165  case VUT_MINCULTURE:
166  src->value.minculture = 0;
167  return true;
168  case VUT_MINFOREIGNPCT:
169  src->value.minforeignpct = 0;
170  return true;
171  case VUT_UNITSTATE:
172  src->value.unit_state = USP_TRANSPORTED;
173  return true;
174  case VUT_ACTIVITY:
175  src->value.activity = ACTIVITY_IDLE;
176  return true;
177  case VUT_MINMOVES:
178  src->value.minmoves = 0;
179  return true;
180  case VUT_MINVETERAN:
181  src->value.minveteran = 0;
182  return true;
183  case VUT_MINHP:
184  src->value.min_hit_points = 0;
185  return true;
186  case VUT_AGE:
187  src->value.age = 0;
188  return true;
189  case VUT_NATIONGROUP:
190  if (nation_group_count() <= 0) {
191  return false;
192  }
194  return true;
195  case VUT_TOPO:
196  src->value.topo_property = TF_ISO;
197  return true;
198  case VUT_SERVERSETTING:
199  src->value.ssetval =
200  ssetv_from_values(server_setting_by_name("killstack"), true);
201  return true;
202  case VUT_IMPR_GENUS:
203  src->value.impr_genus = IG_IMPROVEMENT;
204  return true;
205  case VUT_ACTION:
206  src->value.action = action_by_number(0);
207  return true;
208  case VUT_MINTECHS:
209  src->value.min_techs = 0;
210  return true;
211  case VUT_EXTRAFLAG:
212  src->value.extraflag = EF_NATIVE_TILE;
213  return true;
214  case VUT_VISIONLAYER:
215  src->value.vlayer = V_MAIN;
216  return true;
217  case VUT_NINTEL:
218  src->value.nintel = NI_CULTURE; // We like culture
219  return true;
220  case VUT_COUNT:
221  fc_assert(src->kind != VUT_COUNT);
222  return false;
223  }
224 
225  return false;
226 }
227 
232  void *data)
233 {
234  int i;
235 
236  switch (univ->kind) {
237  case VUT_NONE:
238  break;
239  case VUT_ADVANCE:
241  {
242  cb(advance_rule_name(padv), univ->value.advance == padv, data);
243  }
245  break;
246  case VUT_GOVERNMENT:
247  for (auto &pgov : governments) {
248  if (!pgov.ruledit_disabled) {
249  {
250  cb(government_rule_name(&pgov), univ->value.govern == &pgov, data);
251  }
252  }
253  }
254  break;
255  case VUT_IMPROVEMENT:
257  {
258  cb(improvement_rule_name(pimpr), univ->value.building == pimpr, data);
259  }
261  break;
262  case VUT_TERRAIN:
264  {
265  cb(terrain_rule_name(pterr), univ->value.terrain == pterr, data);
266  }
268  break;
269  case VUT_NATION:
270  for (auto &pnat : nations) {
271  cb(nation_rule_name(&pnat), univ->value.nation == &pnat, data);
272  }
273  break;
274  case VUT_UTYPE:
276  {
277  cb(utype_rule_name(putype), univ->value.utype == putype, data);
278  }
280  break;
281  case VUT_UCLASS:
283  {
284  cb(uclass_rule_name(pclass), univ->value.uclass == pclass, data);
285  }
287  break;
288  case VUT_OTYPE:
289  output_type_iterate(otype)
290  {
291  cb(get_output_name(otype), univ->value.outputtype == otype, data);
292  }
294  break;
295  case VUT_GOOD:
297  {
298  cb(goods_rule_name(pgood), univ->value.good == pgood, data);
299  }
301  break;
302  case VUT_NATIONALITY:
303  for (auto &pnat : nations) {
304  cb(nation_rule_name(&pnat), univ->value.nationality == &pnat, data);
305  }
306  break;
307  case VUT_EXTRA:
309  {
310  cb(extra_rule_name(pextra), univ->value.extra == pextra, data);
311  }
313  break;
314  case VUT_STYLE:
316  {
317  cb(style_rule_name(pstyle), univ->value.style == pstyle, data);
318  }
320  break;
321  case VUT_AI_LEVEL:
322  for (i = 0; i < AI_LEVEL_COUNT; i++) {
323  cb(ai_level_name(ai_level(i)), univ->value.ai_level == i, data);
324  }
325  break;
326  case VUT_SPECIALIST:
328  {
329  cb(specialist_rule_name(pspe), univ->value.specialist == pspe, data);
330  }
332  break;
333  case VUT_TERRAINCLASS:
334  for (i = 0; i < TC_COUNT; i++) {
335  cb(terrain_class_name(terrain_class(i)), univ->value.terrainclass == i,
336  data);
337  }
338  break;
339  case VUT_UTFLAG:
340  for (i = 0; i < UTYF_LAST_USER_FLAG; i++) {
341  cb(unit_type_flag_id_name(unit_type_flag_id(i)),
342  univ->value.unitflag == i, data);
343  }
344  break;
345  case VUT_UCFLAG:
346  for (i = 0; i < UCF_COUNT; i++) {
347  cb(unit_class_flag_id_name(unit_class_flag_id(i)),
348  univ->value.unitclassflag == i, data);
349  }
350  break;
351  case VUT_TERRFLAG:
352  for (i = 0; i < TER_USER_LAST; i++) {
353  cb(terrain_flag_id_name(terrain_flag_id(i)),
354  univ->value.terrainflag == i, data);
355  }
356  break;
357  case VUT_BASEFLAG:
358  for (i = 0; i < BF_COUNT; i++) {
359  cb(base_flag_id_name(base_flag_id(i)), univ->value.baseflag == i,
360  data);
361  }
362  break;
363  case VUT_ROADFLAG:
364  for (i = 0; i < RF_COUNT; i++) {
365  cb(road_flag_id_name(road_flag_id(i)), univ->value.roadflag == i,
366  data);
367  }
368  break;
369  case VUT_TECHFLAG:
370  for (i = 0; i < TF_COUNT; i++) {
371  cb(tech_flag_id_name(tech_flag_id(i)), univ->value.techflag == i,
372  data);
373  }
374  break;
375  case VUT_EXTRAFLAG:
376  for (i = 0; i < EF_COUNT; i++) {
377  cb(extra_flag_id_name(extra_flag_id(i)), univ->value.extraflag == i,
378  data);
379  }
380  break;
381  case VUT_TERRAINALTER:
382  for (i = 0; i < TA_COUNT; i++) {
383  cb(terrain_alteration_name(terrain_alteration(i)),
384  univ->value.terrainalter == i, data);
385  }
386  break;
387  case VUT_CITYTILE:
388  for (i = 0; i < CITYT_LAST; i++) {
389  cb(citytile_type_name(citytile_type(i)), univ->value.citytile == i,
390  data);
391  }
392  break;
393  case VUT_CITYSTATUS:
394  for (i = 0; i < CITYS_LAST; i++) {
395  cb(citystatus_type_name(citystatus_type(i)),
396  univ->value.citystatus == i, data);
397  }
398  break;
399  case VUT_ACHIEVEMENT:
401  {
402  cb(achievement_rule_name(pach), univ->value.achievement == pach, data);
403  }
405  break;
406  case VUT_DIPLREL:
407  for (i = 0; i < DS_LAST; i++) {
408  cb(diplstate_type_name(diplstate_type(i)), univ->value.diplrel == i,
409  data);
410  }
411  for (; i < DRO_LAST; i++) {
412  cb(diplrel_other_name(diplrel_other(i)), univ->value.diplrel == i,
413  data);
414  }
415  break;
416  case VUT_UNITSTATE:
417  for (i = 0; i < USP_COUNT; i++) {
418  cb(ustate_prop_name(ustate_prop(i)), univ->value.unit_state == i,
419  data);
420  }
421  break;
422  case VUT_ACTIVITY:
424  {
425  cb(unit_activity_name(act), univ->value.activity == act, data);
426  }
428  break;
429  case VUT_NATIONGROUP:
430  nation_groups_iterate(pgroup)
431  {
432  cb(nation_group_rule_name(pgroup), univ->value.nationgroup == pgroup,
433  data);
434  }
436  break;
437  case VUT_TOPO:
438  for (i = 0; i < TOPO_FLAG_BITS; i++) {
439  cb(topo_flag_name(topo_flag(1 << i)),
440  univ->value.topo_property == 1 << i, data);
441  }
442  break;
443  case VUT_SERVERSETTING:
444  for (i = 0;
445  /* Only binary settings with the value TRUE are currently
446  * supported. */
447  i < settings_number(); i++) {
449  cb(ssetv_rule_name(i),
450  univ->value.ssetval == ssetv_from_values(i, true), data);
451  }
452  }
453  break;
454  case VUT_IMPR_GENUS:
455  for (i = 0; i < IG_COUNT; i++) {
456  cb(impr_genus_id_name(impr_genus_id(i)), univ->value.impr_genus == i,
457  data);
458  }
459  break;
460  case VUT_ACTION:
461  action_iterate(act)
462  {
463  struct action *pact = action_by_number(act);
464 
465  cb(action_rule_name(pact), univ->value.action == pact, data);
466  }
468  break;
469  case VUT_VISIONLAYER:
470  for (i = 0; i < V_COUNT; i++) {
471  cb(vision_layer_name(vision_layer(i)), univ->value.vlayer == i, data);
472  }
473  break;
474  case VUT_NINTEL:
475  for (i = 0; i < NI_COUNT; i++) {
476  cb(national_intelligence_name(static_cast<national_intelligence>(i)),
477  univ->value.nintel == i, data);
478  }
479  break;
480  case VUT_MINSIZE:
481  case VUT_MINYEAR:
482  case VUT_MINCALFRAG:
483  case VUT_MAXTILEUNITS:
484  case VUT_MINCULTURE:
485  case VUT_MINFOREIGNPCT:
486  case VUT_MINMOVES:
487  case VUT_MINVETERAN:
488  case VUT_MINHP:
489  case VUT_AGE:
490  case VUT_MINTECHS:
491  // Requirement types having numerical value
492  cb(nullptr, false, data);
493  break;
494  case VUT_COUNT:
495  fc_assert(univ->kind != VUT_COUNT);
496  break;
497  }
498 }
const char * achievement_rule_name(struct achievement *pach)
Return untranslated name of this achievement type.
struct achievement * achievement_by_number(int id)
Return achievements of given id.
#define achievements_re_active_iterate(_p)
Definition: achievements.h:68
#define achievements_re_active_iterate_end
Definition: achievements.h:73
const char * action_rule_name(const struct action *action)
Get the rule name of the action.
Definition: actions.cpp:1343
struct action * action_by_number(action_id act_id)
Return the action with the given id.
Definition: actions.cpp:1149
#define action_iterate_end
Definition: actions.h:383
#define action_iterate(_act_)
Definition: actions.h:378
const char * get_output_name(Output_type_id output)
Return a translated name for the output type.
Definition: city.cpp:602
#define output_type_iterate(output)
Definition: city.h:764
#define output_type_iterate_end
Definition: city.h:771
struct extra_type * extra_by_number(int id)
Return extras type of given id.
Definition: extras.cpp:154
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_re_active_iterate_end
Definition: extras.h:294
#define extra_type_re_active_iterate(_p)
Definition: extras.h:289
#define TOPO_FLAG_BITS
Definition: fc_types.h:502
output_type_id
Definition: fc_types.h:84
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.
struct impr_type * improvement_by_number(const Impr_type_id id)
Returns the improvement type for the given index/ID.
#define improvement_re_active_iterate_end
Definition: improvement.h:209
#define improvement_re_active_iterate(_p)
Definition: improvement.h:204
#define fc_assert(condition)
Definition: log.h:89
struct nation_type * nation_by_number(const Nation_type_id nation)
Return the nation with the given index.
Definition: nation.cpp:450
const char * nation_rule_name(const struct nation_type *pnation)
Return the (untranslated) rule name of the nation (adjective form).
Definition: nation.cpp:115
std::vector< nation_type > nations
Definition: nation.cpp:38
int nation_group_count()
Return the number of nation groups.
Definition: nation.cpp:809
struct nation_group * nation_group_by_number(int id)
Return the nation group with the given index.
Definition: nation.cpp:870
const char * nation_group_rule_name(const struct nation_group *pgroup)
Return the (untranslated) rule name of a nation group.
Definition: nation.cpp:942
#define nation_groups_iterate(NAME_pgroup)
Definition: nation.h:292
#define nation_groups_iterate_end
Definition: nation.h:296
bool sanity_check_server_setting_value_in_req(ssetv ssetval)
Returns TRUE iff the given server setting and value combination is allowed to appear in ServerSetting...
Definition: rssanity.cpp:122
server_setting_id server_setting_by_name(const char *name)
Returns the server setting with the specified name.
const char * ssetv_rule_name(ssetv val)
Returns the server setting - value pair encoded as a string.
ssetv ssetv_from_values(server_setting_id setting, int value)
Returns a server setting - value pair from its setting and value;.
int settings_number()
Returns the total number of settings.
Definition: settings.cpp:4816
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_re_active_iterate_end
Definition: specialist.h:83
#define specialist_type_re_active_iterate(_p)
Definition: specialist.h:77
struct packet_ruleset_control control
Definition: game.h:74
struct government * government_during_revolution
Definition: game.h:85
enum universals_n kind
Definition: fc_types.h:740
universals_u value
Definition: fc_types.h:739
const char * style_rule_name(const struct nation_style *pstyle)
Return the (untranslated) rule name of the style.
Definition: style.cpp:94
struct nation_style * style_by_number(int id)
Return style of given id.
Definition: style.cpp:74
#define styles_re_active_iterate_end
Definition: style.h:55
#define styles_re_active_iterate(_p)
Definition: style.h:50
const char * advance_rule_name(const struct advance *padvance)
Return the (untranslated) rule name of the advance/technology.
Definition: tech.cpp:283
struct advance * advance_by_number(const Tech_type_id atype)
Return the advance for the given advance index.
Definition: tech.cpp:94
#define advance_re_active_iterate(_p)
Definition: tech.h:243
#define advance_re_active_iterate_end
Definition: tech.h:248
#define A_NONE
Definition: tech.h:36
struct terrain * terrain_by_number(const Terrain_type_id type)
Return the terrain for the given terrain index.
Definition: terrain.cpp:128
const char * terrain_rule_name(const struct terrain *pterrain)
Return the (untranslated) rule name of the terrain.
Definition: terrain.cpp:184
#define terrain_re_active_iterate_end
Definition: terrain.h:347
#define terrain_re_active_iterate(_p)
Definition: terrain.h:342
struct goods_type * goods_by_number(Goods_type_id id)
Return goods type of given id.
const char * goods_rule_name(struct goods_type *pgood)
Return untranslated name of this goods type.
#define goods_type_re_active_iterate_end
Definition: traderoutes.h:233
#define goods_type_re_active_iterate(_p)
Definition: traderoutes.h:228
const struct unit_type * utype
Definition: fc_types.h:585
int unitflag
Definition: fc_types.h:607
int min_techs
Definition: fc_types.h:623
int mincalfrag
Definition: fc_types.h:602
struct nation_style * style
Definition: fc_types.h:589
enum vision_layer vlayer
Definition: fc_types.h:596
enum ai_level ai_level
Definition: fc_types.h:593
struct specialist * specialist
Definition: fc_types.h:582
enum impr_genus_id impr_genus
Definition: fc_types.h:617
enum citytile_type citytile
Definition: fc_types.h:594
struct nation_group * nationgroup
Definition: fc_types.h:588
struct extra_type * extra
Definition: fc_types.h:586
struct nation_type * nation
Definition: fc_types.h:580
int minmoves
Definition: fc_types.h:618
int terrainclass
Definition: fc_types.h:604
int unitclassflag
Definition: fc_types.h:606
struct government * govern
Definition: fc_types.h:578
int max_tile_units
Definition: fc_types.h:619
int terrainalter
Definition: fc_types.h:605
int minculture
Definition: fc_types.h:599
enum citystatus_type citystatus
Definition: fc_types.h:595
int minforeignpct
Definition: fc_types.h:600
const struct impr_type * building
Definition: fc_types.h:579
struct achievement * achievement
Definition: fc_types.h:587
ssetv ssetval
Definition: fc_types.h:626
int techflag
Definition: fc_types.h:609
struct advance * advance
Definition: fc_types.h:577
enum unit_activity activity
Definition: fc_types.h:616
struct goods_type * good
Definition: fc_types.h:591
struct terrain * terrain
Definition: fc_types.h:583
int terrainflag
Definition: fc_types.h:608
enum national_intelligence nintel
Definition: fc_types.h:597
enum ustate_prop unit_state
Definition: fc_types.h:615
int minveteran
Definition: fc_types.h:620
int extraflag
Definition: fc_types.h:612
Output_type_id outputtype
Definition: fc_types.h:603
int roadflag
Definition: fc_types.h:611
enum topo_flag topo_property
Definition: fc_types.h:625
int min_hit_points
Definition: fc_types.h:621
struct unit_class * uclass
Definition: fc_types.h:584
struct nation_type * nationality
Definition: fc_types.h:581
struct action * action
Definition: fc_types.h:590
int baseflag
Definition: fc_types.h:610
#define activity_type_iterate(_act_)
Definition: unit.h:282
#define activity_type_iterate_end
Definition: unit.h:286
struct unit_type * utype_by_number(const Unit_type_id id)
Return a pointer for the unit type struct for the given unit type id.
Definition: unittype.cpp:103
const char * utype_rule_name(const struct unit_type *punittype)
Return the (untranslated) rule name of the unit type.
Definition: unittype.cpp:1274
struct unit_class * uclass_by_number(const Unit_Class_id id)
Returns unit class pointer for an ID value.
Definition: unittype.cpp:2129
const char * uclass_rule_name(const struct unit_class *pclass)
Return the (untranslated) rule name of the unit class.
Definition: unittype.cpp:1333
#define unit_type_re_active_iterate(_p)
Definition: unittype.h:796
#define unit_class_re_active_iterate_end
Definition: unittype.h:839
#define UTYF_LAST_USER_FLAG
Definition: unittype.h:302
#define unit_class_re_active_iterate(_p)
Definition: unittype.h:834
#define unit_type_re_active_iterate_end
Definition: unittype.h:801
bool universal_value_initial(struct universal *src)
Initialize universal value with a value suitable for the kind.
Definition: univ_value.cpp:36
void universal_kind_values(struct universal *univ, univ_kind_values_cb cb, void *data)
Call cb for each value possible for the universal kind.
Definition: univ_value.cpp:231
void(* univ_kind_values_cb)(const char *value, bool current, void *data)
Definition: univ_value.h:16