From ddf75e39df90aedb25c32009211f56396865d28c Mon Sep 17 00:00:00 2001 From: zaaarf Date: Thu, 14 Dec 2023 01:35:53 +0100 Subject: feat: on_action dataloading --- src/openvic-simulation/dataloader/Dataloader.cpp | 7 ++++ src/openvic-simulation/misc/Event.cpp | 41 ++++++++++++++++++++++++ src/openvic-simulation/misc/Event.hpp | 17 ++++++++++ 3 files changed, 65 insertions(+) diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp index f49ae2d..8065367 100644 --- a/src/openvic-simulation/dataloader/Dataloader.cpp +++ b/src/openvic-simulation/dataloader/Dataloader.cpp @@ -668,6 +668,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; @@ -833,6 +834,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; + + 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 IDENTIFIER_REGISTRY(event); + IdentifierRegistry IDENTIFIER_REGISTRY(on_action); public: bool register_event( @@ -76,6 +90,9 @@ namespace OpenVic { std::vector&& 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 -- cgit v1.2.3-56-ga3b1