aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/misc/Event.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/openvic-simulation/misc/Event.cpp')
-rw-r--r--src/openvic-simulation/misc/Event.cpp41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/openvic-simulation/misc/Event.cpp b/src/openvic-simulation/misc/Event.cpp
index 59b1f54..b7046af 100644
--- a/src/openvic-simulation/misc/Event.cpp
+++ b/src/openvic-simulation/misc/Event.cpp
@@ -20,6 +20,9 @@ Event::Event(
news_desc_long { new_news_desc_long }, news_desc_medium { new_news_desc_medium }, news_desc_short { new_news_desc_short },
election { new_election }, election_issue_group { new_election_issue_group }, options { std::move(new_options) } {}
+OnAction::OnAction(std::string_view new_identifier, weight_map_t new_weighted_events)
+ : HasIdentifier { new_identifier }, weighted_events { std::move(new_weighted_events) } {}
+
bool EventManager::register_event(
std::string_view identifier, std::string_view title, std::string_view description, std::string_view image,
Event::event_type_t type, bool triggered_only, bool major, bool fire_only_once, bool allows_multiple_instances, bool news,
@@ -69,6 +72,15 @@ bool EventManager::register_event(
}, duplicate_warning_callback);
}
+bool EventManager::add_on_action(std::string_view identifier, OnAction::weight_map_t weighted_events) {
+ if (identifier.empty()) {
+ Logger::error("Invalid decision identifier - empty!");
+ return false;
+ }
+
+ return on_actions.add_item({ identifier, std::move(weighted_events) });
+}
+
bool EventManager::load_event_file(IssueManager const& issue_manager, ast::NodeCPtr root) {
return expect_dictionary(
[this, &issue_manager](std::string_view key, ast::NodeCPtr value) -> bool {
@@ -132,3 +144,32 @@ bool EventManager::load_event_file(IssueManager const& issue_manager, ast::NodeC
}
)(root);
}
+
+bool EventManager::load_on_action_file(ast::NodeCPtr root) {
+ bool ret = expect_dictionary([this](std::string_view identifier, ast::NodeCPtr node) -> bool {
+ OnAction::weight_map_t weighted_events;
+ bool ret = expect_dictionary([this, &identifier, &weighted_events](std::string_view weight_str, ast::NodeCPtr event_node) -> bool {
+ Event const* event = nullptr;
+
+ bool ret = false;
+ uint64_t weight = StringUtils::string_to_uint64(weight_str, &ret);
+ if (!ret) {
+ Logger::error("Invalid weight ", weight_str, " on action ", identifier);
+ return ret;
+ }
+
+ ret &= expect_event_identifier(assign_variable_callback_pointer(event))(event_node);
+
+ if (event != nullptr) {
+ ret &= weighted_events.emplace(event, weight).second;
+ } else Logger::warning("Non-existing event ", event->get_identifier(), " loaded on action ", identifier, "with weight", weight, "!");
+
+ return ret;
+ }
+ )(node);
+ ret &= add_on_action(identifier, std::move(weighted_events));
+ return ret;
+ })(root);
+ on_actions.lock();
+ return ret;
+} \ No newline at end of file