aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/scripts/Condition.hpp
blob: 748453f09146fd137c7bcf9233d66bd083ec47b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#pragma once

#include <ostream>
#include <string_view>
#include <variant>

#include "openvic-simulation/types/EnumBitfield.hpp"
#include "openvic-simulation/types/IdentifierRegistry.hpp"

namespace OpenVic {
   struct ConditionManager;
   struct ConditionScript;
   struct DefinitionManager;

   enum class value_type_t : uint8_t {
      NO_TYPE     = 0,
      IDENTIFIER  = 1 << 0,
      STRING      = 1 << 1,
      BOOLEAN     = 1 << 2,
      BOOLEAN_INT = 1 << 3,
      INTEGER     = 1 << 4,
      REAL        = 1 << 5,
      COMPLEX     = 1 << 6,
      GROUP       = 1 << 7,
      MAX_VALUE   = (1 << 8) - 1
   };

   // Order matters in this enum, for the fallback system to work
   // smaller entities must have smaller integers associated!
   enum class scope_t : uint8_t { //TODO: maybe distinguish TRANSPARENT from NO_SCOPE
      NO_SCOPE    = 0,
      POP         = 1 << 0,
      PROVINCE    = 1 << 1,
      STATE       = 1 << 2,
      COUNTRY     = 1 << 3,
      THIS        = 1 << 4,
      FROM        = 1 << 5,
      MAX_SCOPE   = (1 << 6) - 1
   };

   enum class identifier_type_t : uint32_t {
      NO_IDENTIFIER   = 0,
      VARIABLE        = 1 << 0,
      GLOBAL_FLAG     = 1 << 1,
      COUNTRY_FLAG    = 1 << 2,
      PROVINCE_FLAG   = 1 << 3,
      COUNTRY_TAG     = 1 << 4,
      PROVINCE_ID     = 1 << 5,
      REGION          = 1 << 6,
      IDEOLOGY        = 1 << 7,
      REFORM_GROUP    = 1 << 8,
      REFORM          = 1 << 9,
      ISSUE           = 1 << 10,
      POP_TYPE        = 1 << 11,
      POP_STRATA      = 1 << 12,
      TECHNOLOGY      = 1 << 13,
      INVENTION       = 1 << 14,
      TECH_SCHOOL     = 1 << 15,
      CULTURE         = 1 << 16,
      CULTURE_GROUP   = 1 << 17,
      RELIGION        = 1 << 18,
      TRADE_GOOD      = 1 << 19,
      BUILDING        = 1 << 20,
      CASUS_BELLI     = 1 << 21,
      GOVERNMENT_TYPE = 1 << 22,
      MODIFIER        = 1 << 23,
      NATIONAL_VALUE  = 1 << 24,
      CULTURE_UNION   = 1 << 25, // same as COUNTRY_TAG but also accepts scope this_union
      CONTINENT       = 1 << 26,
      CRIME           = 1 << 27,
      TERRAIN         = 1 << 28,
   };

   /* Allows enum types to be used with bitwise operators. */
   template<> struct enable_bitfield<value_type_t> : std::true_type {};
   template<> struct enable_bitfield<scope_t> : std::true_type {};
   template<> struct enable_bitfield<identifier_type_t> : std::true_type {};

   /* Returns true if the values have any bit in common. */
   inline constexpr bool share_value_type(value_type_t lhs, value_type_t rhs) {
      return (lhs & rhs) != value_type_t::NO_TYPE;
   }
   inline constexpr bool share_scope(scope_t lhs, scope_t rhs) {
      return (lhs & rhs) != scope_t::NO_SCOPE;
   }
   inline constexpr bool share_identifier_type(identifier_type_t lhs, identifier_type_t rhs) {
      return (lhs & rhs) != identifier_type_t::NO_IDENTIFIER;
   }

#define _BUILD_STRING(entry, share) \
   if (share(value, entry)) { \
      if (type_found) { \
         stream << " | "; \
      } else { \
         type_found = true; \
      } \
      stream << #entry; \
   }

#define BUILD_STRING(entry) _BUILD_STRING(entry, share_value_type)

   inline std::ostream& operator<<(std::ostream& stream, value_type_t value) {
      using enum value_type_t;
      if (value == NO_TYPE) {
         return stream << "[NO_TYPE]";
      }
      bool type_found = false;
      stream << '[';
      BUILD_STRING(IDENTIFIER);
      BUILD_STRING(STRING);
      BUILD_STRING(BOOLEAN);
      BUILD_STRING(BOOLEAN_INT);
      BUILD_STRING(INTEGER);
      BUILD_STRING(REAL);
      BUILD_STRING(COMPLEX);
      BUILD_STRING(GROUP);
      if (!type_found) {
         stream << "INVALID VALUE TYPE";
      }
      return stream << ']';
   }

#undef BUILD_STRING
#define BUILD_STRING(entry) _BUILD_STRING(entry, share_scope)

   inline std::ostream& operator<<(std::ostream& stream, scope_t value) {
      using enum scope_t;
      if (value == NO_SCOPE) {
         return stream << "[NO_SCOPE]";
      }
      bool type_found = false;
      stream << '[';
      BUILD_STRING(COUNTRY);
      BUILD_STRING(STATE);
      BUILD_STRING(PROVINCE);
      BUILD_STRING(POP);
      BUILD_STRING(THIS);
      BUILD_STRING(FROM);
      if (!type_found) {
         stream << "INVALID SCOPE";
      }
      return stream << ']';
   }

#undef BUILD_STRING
#define BUILD_STRING(entry) _BUILD_STRING(entry, share_identifier_type)

   inline std::ostream& operator<<(std::ostream& stream, identifier_type_t value) {
      using enum identifier_type_t;
      if (value == NO_IDENTIFIER) {
         return stream << "[NO_IDENTIFIER]";
      }
      bool type_found = false;
      stream << '[';
      BUILD_STRING(VARIABLE);
      BUILD_STRING(GLOBAL_FLAG);
      BUILD_STRING(COUNTRY_FLAG);
      BUILD_STRING(PROVINCE_FLAG);
      BUILD_STRING(COUNTRY_TAG);
      BUILD_STRING(PROVINCE_ID);
      BUILD_STRING(REGION);
      BUILD_STRING(IDEOLOGY);
      BUILD_STRING(REFORM_GROUP);
      BUILD_STRING(REFORM);
      BUILD_STRING(ISSUE);
      BUILD_STRING(POP_TYPE);
      BUILD_STRING(POP_STRATA);
      BUILD_STRING(TECHNOLOGY);
      BUILD_STRING(INVENTION);
      BUILD_STRING(TECH_SCHOOL);
      BUILD_STRING(CULTURE);
      BUILD_STRING(CULTURE_GROUP);
      BUILD_STRING(RELIGION);
      BUILD_STRING(TRADE_GOOD);
      BUILD_STRING(BUILDING);
      BUILD_STRING(CASUS_BELLI);
      BUILD_STRING(GOVERNMENT_TYPE);
      BUILD_STRING(MODIFIER);
      BUILD_STRING(NATIONAL_VALUE);
      BUILD_STRING(CULTURE_UNION);
      BUILD_STRING(CONTINENT);
      BUILD_STRING(CRIME);
      BUILD_STRING(TERRAIN);
      if (!type_found) {
         stream << "INVALID IDENTIFIER TYPE";
      }
      return stream << ']';
   }

#undef BUILD_STRING
#undef _BUILD_STRING

   struct Condition : HasIdentifier {
      friend struct ConditionManager;
      using enum identifier_type_t;

   private:
      const value_type_t PROPERTY(value_type);
      const scope_t PROPERTY(scope);
      const scope_t PROPERTY(scope_change);
      const identifier_type_t PROPERTY(key_identifier_type);
      const identifier_type_t PROPERTY(value_identifier_type);

      Condition(
         std::string_view new_identifier, value_type_t new_value_type, scope_t new_scope,
         scope_t new_scope_change, identifier_type_t new_key_identifier_type,
         identifier_type_t new_value_identifier_type
      );

   public:
      Condition(Condition&&) = default;
   };

   struct ConditionNode {
      friend struct ConditionManager;
      friend struct ConditionScript;

      using string_t = std::string;
      using boolean_t = bool;
      using double_boolean_t = std::pair<bool, bool>;
      using integer_t = uint64_t;
      using real_t = fixed_point_t;
      using identifier_real_t = std::pair<std::string, real_t>;
      using condition_list_t = std::vector<ConditionNode>;
      using value_t = std::variant<
         string_t, boolean_t, double_boolean_t, integer_t, real_t, identifier_real_t, condition_list_t
      >;

   private:
      Condition const* PROPERTY(condition);
      value_t PROPERTY(value);
      HasIdentifier const* PROPERTY(condition_key_item);
      HasIdentifier const* PROPERTY(condition_value_item);
      bool PROPERTY_CUSTOM_PREFIX(valid, is);

      ConditionNode(
         Condition const* new_condition = nullptr, value_t&& new_value = 0,
         bool new_valid = false,
         HasIdentifier const* new_condition_key_item = nullptr,
         HasIdentifier const* new_condition_value_item = nullptr
      );
   };

   struct ConditionManager {
   private:
      CaseInsensitiveIdentifierRegistry<Condition> IDENTIFIER_REGISTRY(condition);
      Condition const* root_condition = nullptr;

      bool add_condition(
         std::string_view identifier, value_type_t value_type, scope_t scope,
         scope_t scope_change = scope_t::NO_SCOPE,
         identifier_type_t key_identifier_type = identifier_type_t::NO_IDENTIFIER,
         identifier_type_t value_identifier_type = identifier_type_t::NO_IDENTIFIER
      );

      NodeTools::callback_t<std::string_view> expect_parse_identifier(
         DefinitionManager const& definition_manager, identifier_type_t identifier_type,
         NodeTools::callback_t<HasIdentifier const*> callback
      ) const;

      NodeTools::node_callback_t expect_condition_node(
         DefinitionManager const& definition_manager, Condition const& condition, scope_t this_scope,
         scope_t from_scope, scope_t cur_scope, NodeTools::callback_t<ConditionNode&&> callback
      ) const;

      NodeTools::node_callback_t expect_condition_node_list(
         DefinitionManager const& definition_manager, scope_t this_scope, scope_t from_scope,
         scope_t cur_scope, bool top_scope, NodeTools::callback_t<ConditionNode&&> callback
      ) const;

   public:
      bool setup_conditions(DefinitionManager const& definition_manager);

      NodeTools::node_callback_t expect_condition_script(
         DefinitionManager const& definition_manager, scope_t initial_scope, scope_t this_scope, scope_t from_scope,
         NodeTools::callback_t<ConditionNode&&> callback
      ) const;
   };
}