aboutsummaryrefslogtreecommitdiff
path: root/src/openvic/pop/Culture.cpp
blob: 2670f00e66fe660ce82cbc779953fd7ad8909a2e (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
#include "Culture.hpp"

#include "openvic/dataloader/NodeTools.hpp"

using namespace OpenVic;
using namespace OpenVic::NodeTools;

GraphicalCultureType::GraphicalCultureType(const std::string_view new_identifier) : HasIdentifier { new_identifier } {}

CultureGroup::CultureGroup(const std::string_view new_identifier, const std::string_view new_leader,
   GraphicalCultureType const& new_unit_graphical_culture_type, bool new_is_overseas)
   : HasIdentifier { new_identifier }, leader { new_leader },
     unit_graphical_culture_type { new_unit_graphical_culture_type },
     is_overseas { new_is_overseas } {}


std::string const& CultureGroup::get_leader() const {
   return leader;
}

GraphicalCultureType const& CultureGroup::get_unit_graphical_culture_type() const {
   return unit_graphical_culture_type;
}

bool CultureGroup::get_is_overseas() const {
   return is_overseas;
}

Culture::Culture(const std::string_view new_identifier, colour_t new_colour, CultureGroup const& new_group,
   std::vector<std::string> const& new_first_names, std::vector<std::string> const& new_last_names)
   : HasIdentifierAndColour { new_identifier, new_colour, true },
     group { new_group },
     first_names { new_first_names },
     last_names { new_last_names } {}

CultureGroup const& Culture::get_group() const {
   return group;
}

std::vector<std::string> const& Culture::get_first_names() const {
   return first_names;
}

std::vector<std::string> const& Culture::get_last_names() const {
   return last_names;
}

CultureManager::CultureManager()
   : graphical_culture_types { "graphical culture types" },
     culture_groups { "culture groups" },
     cultures { "cultures" } {}

return_t CultureManager::add_graphical_culture_type(const std::string_view identifier) {
   if (identifier.empty()) {
      Logger::error("Invalid culture group identifier - empty!");
      return FAILURE;
   }
   return graphical_culture_types.add_item({ identifier });
}

void CultureManager::lock_graphical_culture_types() {
   graphical_culture_types.lock();
}

GraphicalCultureType const* CultureManager::get_graphical_culture_type_by_identifier(const std::string_view identifier) const {
   return graphical_culture_types.get_item_by_identifier(identifier);
}

size_t CultureManager::get_graphical_culture_type_count() const {
   return graphical_culture_types.size(); 
}

std::vector<GraphicalCultureType> const& CultureManager::get_graphical_culture_types() const {
   return graphical_culture_types.get_items();
}

return_t CultureManager::add_culture_group(const std::string_view identifier, const std::string_view leader, GraphicalCultureType const* graphical_culture_type, bool is_overseas) {
   if (!graphical_culture_types.is_locked()) {
      Logger::error("Cannot register culture groups until graphical culture types are locked!");
      return FAILURE;
   }
   if (identifier.empty()) {
      Logger::error("Invalid culture group identifier - empty!");
      return FAILURE;
   }
   if (leader.empty()) {
      Logger::error("Invalid culture group leader - empty!");
      return FAILURE;
   }
   if (graphical_culture_type == nullptr) {
      Logger::error("Null graphical culture type for ", identifier);
      return FAILURE;
   }
   return culture_groups.add_item({ identifier, leader, *graphical_culture_type, is_overseas });
}

void CultureManager::lock_culture_groups() {
   culture_groups.lock();
}

CultureGroup const* CultureManager::get_culture_group_by_identifier(const std::string_view identifier) const {
   return culture_groups.get_item_by_identifier(identifier);
}

size_t CultureManager::get_culture_group_count() const {
   return culture_groups.size(); 
}

std::vector<CultureGroup> const& CultureManager::get_culture_groups() const {
   return culture_groups.get_items();
}

return_t CultureManager::add_culture(const std::string_view identifier, colour_t colour, CultureGroup const* group, std::vector<std::string> const& first_names, std::vector<std::string> const& last_names) {
   if (!culture_groups.is_locked()) {
      Logger::error("Cannot register cultures until culture groups are locked!");
      return FAILURE;
   }
   if (identifier.empty()) {
      Logger::error("Invalid culture identifier - empty!");
      return FAILURE;
   }
   if (group == nullptr) {
      Logger::error("Null culture group for ", identifier);
      return FAILURE;
   }
   if (colour > MAX_COLOUR_RGB) {
      Logger::error("Invalid culture colour for ", identifier, ": ", colour_to_hex_string(colour));
      return FAILURE;
   }
   return cultures.add_item({ identifier, colour, *group, first_names, last_names });
}

void CultureManager::lock_cultures() {
   cultures.lock();
}

Culture const* CultureManager::get_culture_by_identifier(const std::string_view identifier) const {
   return cultures.get_item_by_identifier(identifier);
}

size_t CultureManager::get_culture_count() const {
   return cultures.size(); 
}

std::vector<Culture> const& CultureManager::get_cultures() const {
   return cultures.get_items();
}

return_t CultureManager::load_graphical_culture_type_file(ast::NodeCPtr root) {
   const return_t ret = expect_list_reserve_length(
      graphical_culture_types,
      expect_identifier(
         std::bind(&CultureManager::add_graphical_culture_type, this, std::placeholders::_1)
      )
   )(root);
   lock_graphical_culture_types();
   return ret;
}

return_t CultureManager::load_culture_file(ast::NodeCPtr root) {
   if (!graphical_culture_types.is_locked()) {
      Logger::error("Cannot load culture groups until graphical culture types are locked!");
      return FAILURE;
   }

   static const std::string default_unit_graphical_culture_type_identifier = "Generic";
   GraphicalCultureType const* const default_unit_graphical_culture_type = get_graphical_culture_type_by_identifier(default_unit_graphical_culture_type_identifier);
   if (default_unit_graphical_culture_type == nullptr) {
      Logger::error("Failed to find default unit graphical culture type: ", default_unit_graphical_culture_type_identifier);
   }

   size_t total_expected_cultures = 0;
   return_t ret = expect_dictionary_reserve_length(
      culture_groups,
      [this, default_unit_graphical_culture_type, &total_expected_cultures](std::string_view key, ast::NodeCPtr value) -> return_t {
         std::string_view leader;
         GraphicalCultureType const* unit_graphical_culture_type = default_unit_graphical_culture_type;
         bool is_overseas = true;

         return_t ret = expect_dictionary_keys_and_length(
            [&total_expected_cultures](size_t size) -> size_t {
               total_expected_cultures += size;
               return size;
            },
            ALLOW_OTHER_KEYS,
            "leader", ONE_EXACTLY, expect_identifier(assign_variable_callback(leader)),
            "unit", ZERO_OR_ONE,
               expect_identifier(
                  [this, &unit_graphical_culture_type](std::string_view identifier) -> return_t {
                     unit_graphical_culture_type = get_graphical_culture_type_by_identifier(identifier);
                     if (unit_graphical_culture_type != nullptr) return SUCCESS;
                     Logger::error("Invalid unit graphical culture type: ", identifier);
                     return FAILURE;
                  }
               ),
            "union", ZERO_OR_ONE, success_callback,
            "is_overseas", ZERO_OR_ONE, expect_bool(assign_variable_callback(is_overseas))
         )(value);
         if (add_culture_group(key, leader, unit_graphical_culture_type, is_overseas) != SUCCESS) ret = FAILURE;
         return ret;
      }
   )(root);
   lock_culture_groups();
   cultures.reserve(cultures.size() + total_expected_cultures);

   if (expect_dictionary(
      [this](std::string_view culture_group_key, ast::NodeCPtr culture_group_value) -> return_t {

         CultureGroup const* culture_group = get_culture_group_by_identifier(culture_group_key);

         return expect_dictionary(
            [this, culture_group](std::string_view key, ast::NodeCPtr value) -> return_t {
               if (key == "leader" || key == "unit" || key == "union" || key == "is_overseas") return SUCCESS;

               colour_t colour = NULL_COLOUR;
               std::vector<std::string> first_names, last_names;

               return_t ret = expect_dictionary_keys(
                  "color", ONE_EXACTLY, expect_colour(assign_variable_callback(colour)),
                  "first_names", ONE_EXACTLY, name_list_callback(first_names),
                  "last_names", ONE_EXACTLY, name_list_callback(last_names),
                  "radicalism", ZERO_OR_ONE, success_callback,
                  "primary", ZERO_OR_ONE, success_callback
               )(value);
               if (add_culture(key, colour, culture_group, first_names, last_names) != SUCCESS) ret = FAILURE;
               return ret;
            }
         )(culture_group_value);
      }
   )(root) != SUCCESS) ret = FAILURE;
   lock_cultures();
   return ret;
}