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
|
#include "Rule.hpp"
#include "openvic-simulation/economy/BuildingType.hpp"
#include "openvic-simulation/utility/TslHelper.hpp"
using namespace OpenVic;
using namespace OpenVic::NodeTools;
Rule::Rule(std::string_view new_identifier, rule_group_t new_group, index_t new_index)
: HasIdentifier { new_identifier }, HasIndex { new_index }, group { new_group } {}
RuleSet::RuleSet(rule_group_map_t&& new_rule_groups) : rule_groups { std::move(new_rule_groups) } {}
bool RuleSet::trim_and_resolve_conflicts(bool log) {
bool ret = true;
for (auto [group, rule_map] : mutable_iterator(rule_groups)) {
if (Rule::is_mutually_exclusive_group(group)) {
Rule const* primary_rule = nullptr;
for (auto const& [rule, value] : rule_map) {
if (value) {
if (primary_rule == nullptr) {
primary_rule = rule;
} else {
if (primary_rule->get_index() < rule->get_index()) {
primary_rule = rule;
}
ret = false;
}
}
}
if (log) {
for (auto const& [rule, value] : rule_map) {
if (value) {
if (rule != primary_rule) {
Logger::error(
"Conflicting mutually exclusive rule: ", rule, " superceeded by ", primary_rule, " - removing!"
);
}
} else {
Logger::warning("Disabled mutually exclusive rule: ", rule, " - removing!");
}
}
}
rule_map.clear();
if (primary_rule != nullptr) {
rule_map.emplace(primary_rule, true);
}
}
}
return ret;
}
size_t RuleSet::get_rule_group_count() const {
return rule_groups.size();
}
size_t RuleSet::get_rule_count() const {
size_t ret = 0;
for (auto const& [group, rule_map] : rule_groups) {
ret += rule_map.size();
}
return ret;
}
RuleSet::rule_map_t const& RuleSet::get_rule_group(Rule::rule_group_t group, bool* successful) const {
const rule_group_map_t::const_iterator it = rule_groups.find(group);
if (it != rule_groups.end()) {
if (successful != nullptr) {
*successful = true;
}
return it->second;
}
if (successful != nullptr) {
*successful = false;
}
static const rule_map_t empty_map {};
return empty_map;
}
bool RuleSet::get_rule(Rule const* rule, bool* successful) const {
if (rule == nullptr) {
Logger::error("Invalid rule - null!");
return false;
}
rule_map_t const& rule_map = get_rule_group(rule->get_group());
const rule_map_t::const_iterator it = rule_map.find(rule);
if (it != rule_map.end()) {
if (successful != nullptr) {
*successful = true;
}
return it->second;
}
if (successful != nullptr) {
*successful = false;
}
return Rule::is_default_enabled(rule->get_group());
}
bool RuleSet::has_rule(Rule const* rule) const {
if (rule == nullptr) {
Logger::error("Invalid rule - null!");
return false;
}
return get_rule_group(rule->get_group()).contains(rule);
}
bool RuleSet::set_rule(Rule const* rule, bool value) {
if (rule == nullptr) {
Logger::error("Invalid rule - null!");
return false;
}
rule_groups[rule->get_group()][rule] = value;
return true;
}
RuleSet& RuleSet::operator|=(RuleSet const& right) {
for (auto const& [group, rule_map] : right.rule_groups) {
for (auto const& [rule, value] : rule_map) {
rule_groups[group][rule] |= value;
}
}
return *this;
}
RuleSet RuleSet::operator|(RuleSet const& right) const {
RuleSet ret = *this;
return ret |= right;
}
bool RuleManager::add_rule(std::string_view identifier, Rule::rule_group_t group) {
if (identifier.empty()) {
Logger::error("Invalid rule identifier - empty!");
return false;
}
return rules.add_item({ identifier, group, rule_group_sizes[group]++ });
}
bool RuleManager::setup_rules(BuildingTypeManager const& building_type_manager) {
bool ret = true;
using enum Rule::rule_group_t;
static const ordered_map<Rule::rule_group_t, std::vector<std::string_view>> hardcoded_rules {
{ ECONOMY, {
"expand_factory", "open_factory", "destroy_factory", "pop_build_factory", "pop_expand_factory", "pop_open_factory",
"can_subsidise", "factory_priority", "delete_factory_if_no_input", "build_factory_invest", "expand_factory_invest",
"open_factory_invest", "build_railway_invest", "pop_build_factory_invest", "pop_expand_factory_invest",
"can_invest_in_pop_projects", "allow_foreign_investment"
} },
{ CITIZENSHIP, { "primary_culture_voting", "culture_voting", "all_voting" } },
{ SLAVERY, { "slavery_allowed" } },
{ UPPER_HOUSE, { "same_as_ruling_party", "rich_only", "state_vote", "population_vote" } },
{ VOTING, {
"largest_share" /* First Past the Post */,
"dhont" /* Jefferson Method */,
"sainte_laque" /* Proportional Representation */
} }
};
size_t rule_count = building_type_manager.get_building_type_types().size();
for (auto const& [group, rule_list] : hardcoded_rules) {
rule_count += rule_list.size();
}
reserve_more_rules(rule_count);
for (auto const& [group, rule_list] : hardcoded_rules) {
for (std::string_view const& rule : rule_list) {
ret &= add_rule(rule, group);
}
}
for (std::string const& type : building_type_manager.get_building_type_types()) {
std::string build_rule_string = "build_";
if (type != "infrastructure") {
build_rule_string += type;
} else {
build_rule_string += "railway";
}
ret &= add_rule(build_rule_string, ECONOMY);
}
lock_rules();
return ret;
}
node_callback_t RuleManager::expect_rule_set(callback_t<RuleSet&&> ruleset_callback) const {
return [this, ruleset_callback](ast::NodeCPtr root) -> bool {
RuleSet ruleset;
bool ret = expect_dictionary(
[this, &ruleset](std::string_view rule_key, ast::NodeCPtr rule_value) -> bool {
Rule const* rule = get_rule_by_identifier(rule_key);
if (rule != nullptr) {
return expect_bool(
[&ruleset, rule](bool value) -> bool {
/* Wrapped in a lambda function so that the rule group is only initialised
* if the value bool is successfully parsed. */
return map_callback(ruleset.rule_groups[rule->get_group()], rule)(value);
}
)(rule_value);
} else {
Logger::error("Invalid rule identifier: ", rule_key);
return false;
}
}
)(root);
ret &= ruleset.trim_and_resolve_conflicts(true);
ret &= ruleset_callback(std::move(ruleset));
return ret;
};
}
namespace OpenVic { // so the compiler shuts up (copied from Modifier.cpp)
std::ostream& operator<<(std::ostream& stream, RuleSet const& value) {
for (auto const& [group, rule_map] : value.rule_groups) {
for (auto const& [rule, value] : rule_map) {
stream << rule << ": " << (value ? "yes" : "no") << "\n";
}
}
return stream;
}
}
|