aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation
diff options
context:
space:
mode:
author Hop311 <Hop3114@gmail.com>2023-12-14 08:37:35 +0100
committer GitHub <noreply@github.com>2023-12-14 08:37:35 +0100
commite6b455b8924948761dbd792756303efe18875c6d (patch)
tree36fcb92720eabeb18554aa934cac86ed9a9609ed /src/openvic-simulation
parentdf7c04079a7b5936e1701f37a845199bac94b1fb (diff)
parentddf75e39df90aedb25c32009211f56396865d28c (diff)
Merge pull request #92 from OpenVicProject/dataloading-onactions
on_action dataloading
Diffstat (limited to 'src/openvic-simulation')
-rw-r--r--src/openvic-simulation/dataloader/Dataloader.cpp7
-rw-r--r--src/openvic-simulation/misc/Event.cpp41
-rw-r--r--src/openvic-simulation/misc/Event.hpp17
3 files changed, 65 insertions, 0 deletions
diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp
index c23a09b..97b5592 100644
--- a/src/openvic-simulation/dataloader/Dataloader.cpp
+++ b/src/openvic-simulation/dataloader/Dataloader.cpp
@@ -709,6 +709,7 @@ bool Dataloader::load_defines(GameManager& game_manager) const {
static constexpr std::string_view event_modifiers_file = "common/event_modifiers.txt";
static constexpr std::string_view static_modifiers_file = "common/static_modifiers.txt";
static constexpr std::string_view triggered_modifiers_file = "common/triggered_modifiers.txt";
+ static constexpr std::string_view on_actions_file = "common/on_actions.txt";
bool ret = true;
@@ -871,6 +872,12 @@ bool Dataloader::load_defines(GameManager& game_manager) const {
Logger::error("Failed to load events!");
ret = false;
}
+ if (!game_manager.get_event_manager().load_on_action_file(
+ parse_defines(lookup_file(on_actions_file)).get_file_node()
+ )) {
+ Logger::error("Failed to load on actions!");
+ ret = false;
+ }
return ret;
}
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
diff --git a/src/openvic-simulation/misc/Event.hpp b/src/openvic-simulation/misc/Event.hpp
index d1f7963..a518f78 100644
--- a/src/openvic-simulation/misc/Event.hpp
+++ b/src/openvic-simulation/misc/Event.hpp
@@ -63,9 +63,23 @@ namespace OpenVic {
Event(Event&&) = default;
};
+ struct OnAction : HasIdentifier {
+ friend struct EventManager;
+ using weight_map_t = std::map<Event const*, uint64_t>;
+
+ private:
+ weight_map_t PROPERTY(weighted_events);
+
+ OnAction(std::string_view new_identifier, weight_map_t new_weighted_events);
+
+ public:
+ OnAction(OnAction&&) = default;
+ };
+
struct EventManager {
private:
IdentifierRegistry<Event> IDENTIFIER_REGISTRY(event);
+ IdentifierRegistry<OnAction> IDENTIFIER_REGISTRY(on_action);
public:
bool register_event(
@@ -76,6 +90,9 @@ namespace OpenVic {
std::vector<Event::EventOption>&& options
);
+ bool add_on_action(std::string_view identifier, OnAction::weight_map_t new_weighted_events);
+
bool load_event_file(IssueManager const& issue_manager, ast::NodeCPtr root);
+ bool load_on_action_file(ast::NodeCPtr root);
};
} // namespace OpenVic