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

#include "openvic/dataloader/NodeTools.hpp"

using namespace OpenVic;

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

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

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

Culture::Culture(const std::string_view new_identifier, colour_t new_colour, CultureGroup const& new_group,
   name_list_t const& new_first_names, name_list_t 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;
}

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);
}

return_t CultureManager::add_culture_group(const std::string_view identifier, 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 (graphical_culture_type == nullptr) {
      Logger::error("Null graphical culture type for ", identifier);
      return FAILURE;
   }
   return culture_groups.add_item({ identifier, *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);
}

return_t CultureManager::add_culture(const std::string_view identifier, colour_t colour, CultureGroup const* group, Culture::name_list_t const& first_names, Culture::name_list_t 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, ": ", Culture::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);
}

return_t CultureManager::load_graphical_culture_type_file(ast::NodeCPtr root) {
   const return_t ret = NodeTools::expect_list(root, [this](ast::NodeCPtr node) -> return_t {
      return NodeTools::expect_identifier(node, [this](std::string_view identifier) -> return_t {
         return add_graphical_culture_type(identifier);
      });
   }, 0, true);
   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);
   }

   return_t ret = NodeTools::expect_dictionary(root, [this, default_unit_graphical_culture_type](std::string_view key, ast::NodeCPtr value) -> return_t {

      GraphicalCultureType const* unit_graphical_culture_type = default_unit_graphical_culture_type;
      bool is_overseas = true;

      return_t ret = NodeTools::expect_dictionary_keys(value, {
         { "leader", { true, false, NodeTools::success_callback } },
         { "unit", { false, false, [this, &unit_graphical_culture_type](ast::NodeCPtr node) -> return_t {
            return NodeTools::expect_identifier(node, [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", { false, false, NodeTools::success_callback } },
         { "is_overseas", { false, false, [&is_overseas](ast::NodeCPtr node) -> return_t {
            return NodeTools::expect_bool(node, [&is_overseas](bool val) -> return_t {
               is_overseas = val;
               return SUCCESS;
            });
         } } }
      }, true);
      if (add_culture_group(key, unit_graphical_culture_type, is_overseas) != SUCCESS) ret = FAILURE;
      return ret;
   }, true);
   lock_culture_groups();
   if (NodeTools::expect_dictionary(root, [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 NodeTools::expect_dictionary(culture_group_value, [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;   
         Culture::name_list_t first_names, last_names;

         static const std::function<return_t(Culture::name_list_t&, ast::NodeCPtr)> read_name_list = [](Culture::name_list_t& names, ast::NodeCPtr node) -> return_t {
            return NodeTools::expect_list(node, [&names](ast::NodeCPtr val) -> return_t {
               return NodeTools::expect_identifier_or_string(val, [&names](std::string_view str) -> return_t {
                  if (!str.empty()) {
                     names.push_back(std::string { str });
                     return SUCCESS;
                  }
                  Logger::error("Empty identifier or string");
                  return FAILURE;
               });
            });
         };

         return_t ret = NodeTools::expect_dictionary_keys(value, {
            { "color", { true, false, [&colour](ast::NodeCPtr node) -> return_t {
               return NodeTools::expect_colour(node, [&colour](colour_t val) -> return_t {
                  colour = val;
                  return SUCCESS;
               });
            } } },
            { "first_names", { true, false, [&first_names](ast::NodeCPtr node) -> return_t {
               return read_name_list(first_names, node);
            } } },
            { "last_names", { true, false, [&last_names](ast::NodeCPtr node) -> return_t {
               return read_name_list(last_names, node);
            } } },
            { "radicalism", { false, false, NodeTools::success_callback } },
            { "primary", { false, false, NodeTools::success_callback } }
         });
         if (add_culture(key, colour, culture_group, first_names, last_names) != SUCCESS) ret = FAILURE;
         return ret;
      });
   }, true) != SUCCESS) ret = FAILURE;
   lock_cultures();
   return ret;
}