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

#include <cassert>

using namespace OpenVic;

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

Religion::Religion(const std::string_view new_identifier, colour_t new_colour,
   ReligionGroup const& new_group, icon_t new_icon, bool new_pagan)
   : HasIdentifierAndColour { new_identifier, new_colour, true },
     group { new_group },
     icon { new_icon },
     pagan { new_pagan } {
   assert(icon > 0);
}

ReligionGroup const& Religion::get_group() const {
   return group;
}

Religion::icon_t Religion::get_icon() const {
   return icon;
}

bool Religion::get_pagan() const {
   return pagan;
}

ReligionManager::ReligionManager()
   : religion_groups { "religion groups" },
     religions { "religions" } {}

return_t ReligionManager::add_religion_group(const std::string_view identifier) {
   if (identifier.empty()) {
      Logger::error("Invalid religion group identifier - empty!");
      return FAILURE;
   }
   return religion_groups.add_item({ identifier });
}

void ReligionManager::lock_religion_groups() {
   religion_groups.lock();
}

ReligionGroup const* ReligionManager::get_religion_group_by_identifier(const std::string_view identifier) const {
   return religion_groups.get_item_by_identifier(identifier);
}

return_t ReligionManager::add_religion(const std::string_view identifier, colour_t colour, ReligionGroup const* group, Religion::icon_t icon, bool pagan) {
   if (!religion_groups.is_locked()) {
      Logger::error("Cannot register religions until religion groups are locked!");
      return FAILURE;
   }
   if (identifier.empty()) {
      Logger::error("Invalid religion identifier - empty!");
      return FAILURE;
   }
   if (group == nullptr) {
      Logger::error("Null religion group for ", identifier);
      return FAILURE;
   }
   if (colour > MAX_COLOUR_RGB) {
      Logger::error("Invalid religion colour for ", identifier, ": ", Religion::colour_to_hex_string(colour));
      return FAILURE;
   }
   if (icon <= 0) {
      Logger::error("Invalid religion icon for ", identifier, ": ", icon);
      return FAILURE;
   }
   return religions.add_item({ identifier, colour, *group, icon, pagan });
}

void ReligionManager::lock_religions() {
   religions.lock();
}

Religion const* ReligionManager::get_religion_by_identifier(const std::string_view identifier) const {
   return religions.get_item_by_identifier(identifier);
}

return_t ReligionManager::load_religion_file(ast::NodeCPtr root) {

   return_t ret = NodeTools::expect_dictionary(root, [this](std::string_view key, ast::NodeCPtr value) -> return_t {
      return_t ret = NodeTools::expect_dictionary_keys(value, {}, true);
      if (add_religion_group(key) != SUCCESS) ret = FAILURE;
      return ret;
   }, true);
   lock_religion_groups();

   if (NodeTools::expect_dictionary(root, [this](std::string_view religion_group_key, ast::NodeCPtr religion_group_value) -> return_t {

      ReligionGroup const* religion_group = get_religion_group_by_identifier(religion_group_key);

      return NodeTools::expect_dictionary(religion_group_value, [this, religion_group](std::string_view key, ast::NodeCPtr value) -> return_t {
         colour_t colour = NULL_COLOUR;
         Religion::icon_t icon = 0;
         bool pagan = true;

         return_t ret = NodeTools::expect_dictionary_keys(value, {
            { "icon", { true, false, [&icon](ast::NodeCPtr node) -> return_t {
               return NodeTools::expect_int(node, [&icon](Religion::icon_t val) -> return_t {
                  icon = val;
                  return SUCCESS;
               });
            } } },
            { "color", { true, false, [&colour](ast::NodeCPtr node) -> return_t {
               return NodeTools::expect_colour(node, [&colour](colour_t val) -> return_t {
                  colour = val;
                  return SUCCESS;
               });
            } } },
            { "pagan", { false, false, [&pagan](ast::NodeCPtr node) -> return_t {
            return NodeTools::expect_bool(node, [&pagan](bool val) -> return_t {
               pagan = val;
               return SUCCESS;
            });
         } } }
         });
         if (add_religion(key, colour, religion_group, icon, pagan) != SUCCESS) ret = FAILURE;
         return ret;
      });
   }, true) != SUCCESS) ret = FAILURE;
   lock_religions();
   return ret;
}