aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author zaaarf <80046572+zaaarf@users.noreply.github.com>2023-12-10 21:22:55 +0100
committer GitHub <noreply@github.com>2023-12-10 21:22:55 +0100
commit1eb28bd4fb959b69a30013f6438f0257a2ee7b03 (patch)
tree911563514c661e9bdb5bad1e2c38f200ce4b5428 /src
parentba390fec5a923f7c4b4ab7f85e803eb50bbf9e3a (diff)
parent13ffc9a12725b9dbcd3b957c74471356540ee716 (diff)
Merge pull request #90 from OpenVicProject/dataloading-decisions
Decision dataloading (kind of)
Diffstat (limited to 'src')
-rw-r--r--src/openvic-simulation/GameManager.hpp2
-rw-r--r--src/openvic-simulation/dataloader/Dataloader.cpp20
-rw-r--r--src/openvic-simulation/dataloader/Dataloader.hpp1
-rw-r--r--src/openvic-simulation/misc/Decision.cpp75
-rw-r--r--src/openvic-simulation/misc/Decision.hpp47
5 files changed, 145 insertions, 0 deletions
diff --git a/src/openvic-simulation/GameManager.hpp b/src/openvic-simulation/GameManager.hpp
index 9832842..d03c559 100644
--- a/src/openvic-simulation/GameManager.hpp
+++ b/src/openvic-simulation/GameManager.hpp
@@ -1,5 +1,6 @@
#pragma once
+#include "openvic-simulation/misc/Decision.hpp"
#include "openvic-simulation/country/Country.hpp"
#include "openvic-simulation/economy/EconomyManager.hpp"
#include "openvic-simulation/history/HistoryManager.hpp"
@@ -33,6 +34,7 @@ namespace OpenVic {
CountryManager PROPERTY_REF(country_manager);
CrimeManager PROPERTY_REF(crime_manager);
EventManager PROPERTY_REF(event_manager);
+ DecisionManager PROPERTY_REF(decision_manager);
UIManager PROPERTY_REF(ui_manager);
GameAdvancementHook PROPERTY_REF(clock);
diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp
index 8c6704b..f49ae2d 100644
--- a/src/openvic-simulation/dataloader/Dataloader.cpp
+++ b/src/openvic-simulation/dataloader/Dataloader.cpp
@@ -428,6 +428,22 @@ bool Dataloader::_load_inventions(GameManager& game_manager) const {
return ret;
}
+bool Dataloader::_load_decisions(GameManager& game_manager) const {
+ static constexpr std::string_view decisions_directory = "decisions";
+ DecisionManager& decision_manager = game_manager.get_decision_manager();
+
+ bool ret = apply_to_files(
+ lookup_files_in_dir(decisions_directory, ".txt"),
+ [&decision_manager](fs::path const& file) -> bool {
+ return decision_manager.load_decision_file(parse_defines(file).get_file_node());
+ }
+ );
+
+ decision_manager.lock_decisions();
+
+ return ret;
+}
+
bool Dataloader::_load_history(GameManager& game_manager, bool unused_history_file_warnings) const {
/* Country History */
@@ -805,6 +821,10 @@ bool Dataloader::load_defines(GameManager& game_manager) const {
Logger::error("Failed to load countries!");
ret = false;
}
+ if (!_load_decisions(game_manager)) {
+ Logger::error("Failde to load decisions!");
+ ret = false;
+ }
if (!_load_history(game_manager, false)) {
Logger::error("Failed to load history!");
ret = false;
diff --git a/src/openvic-simulation/dataloader/Dataloader.hpp b/src/openvic-simulation/dataloader/Dataloader.hpp
index 884588a..78021b3 100644
--- a/src/openvic-simulation/dataloader/Dataloader.hpp
+++ b/src/openvic-simulation/dataloader/Dataloader.hpp
@@ -32,6 +32,7 @@ namespace OpenVic {
bool _load_inventions(GameManager& game_manager) const;
bool _load_events(GameManager& game_manager) const;
bool _load_map_dir(GameManager& game_manager) const;
+ bool _load_decisions(GameManager& game_manager) const;
bool _load_history(GameManager& game_manager, bool unused_history_file_warnings) const;
/* _DirIterator is fs::directory_iterator or fs::recursive_directory_iterator. _UniqueKey is the type of a callable
diff --git a/src/openvic-simulation/misc/Decision.cpp b/src/openvic-simulation/misc/Decision.cpp
new file mode 100644
index 0000000..861a584
--- /dev/null
+++ b/src/openvic-simulation/misc/Decision.cpp
@@ -0,0 +1,75 @@
+#include "Decision.hpp"
+
+using namespace OpenVic;
+using namespace OpenVic::NodeTools;
+
+Decision::Decision(
+ std::string_view new_identifier, bool new_alert, bool new_news,
+ std::string_view new_news_title, std::string_view new_news_desc_long,
+ std::string_view new_news_desc_medium, std::string_view new_news_desc_short,
+ std::string_view new_picture
+) : HasIdentifier { new_identifier }, alert { new_alert }, news { new_news }, news_title { new_news_title },
+ news_desc_long { new_news_desc_long }, news_desc_medium { new_news_desc_medium },
+ news_desc_short { new_news_desc_short }, picture { new_picture } {}
+
+DecisionManager::DecisionManager() : decisions { "decisions" } {}
+
+bool DecisionManager::add_decision(
+ std::string_view identifier, bool alert, bool news, std::string_view news_title,
+ std::string_view news_desc_long, std::string_view news_desc_medium,
+ std::string_view news_desc_short, std::string_view picture
+) {
+ if (identifier.empty()) {
+ Logger::error("Invalid decision identifier - empty!");
+ return false;
+ }
+
+ if (news) {
+ if (news_desc_long.empty() || news_desc_medium.empty() || news_desc_short.empty()) {
+ Logger::warning(
+ "Decision with ID ", identifier, " is a news decision but doesn't have long, medium and short descriptions!"
+ );
+ }
+ } else {
+ if (!news_title.empty() || !news_desc_long.empty() || !news_desc_medium.empty() || !news_desc_short.empty()) {
+ Logger::warning("Decision with ID ", identifier, " is not a news decision but has news strings specified!");
+ }
+ }
+
+ return decisions.add_item({
+ identifier,
+ alert,
+ news,
+ news_title,
+ news_desc_long,
+ news_desc_medium,
+ news_desc_short,
+ picture
+ }, duplicate_warning_callback);
+}
+
+bool DecisionManager::load_decision_file(ast::NodeCPtr root) {
+ return expect_dictionary_keys(
+ "political_decisions", ZERO_OR_ONE, expect_dictionary(
+ [this](std::string_view identifier, ast::NodeCPtr node) -> bool {
+ bool alert = true, news = false;
+ std::string_view news_title, news_desc_long, news_desc_medium, news_desc_short, picture;
+ bool ret = expect_dictionary_keys(
+ "alert", ZERO_OR_ONE, expect_bool(assign_variable_callback(alert)),
+ "news", ZERO_OR_ONE, expect_bool(assign_variable_callback(news)),
+ "news_title", ZERO_OR_ONE, expect_string(assign_variable_callback(news_title)),
+ "news_desc_long", ZERO_OR_ONE, expect_string(assign_variable_callback(news_desc_long)),
+ "news_desc_medium", ZERO_OR_ONE, expect_string(assign_variable_callback(news_desc_medium)),
+ "news_desc_short", ZERO_OR_ONE, expect_string(assign_variable_callback(news_desc_short)),
+ "picture", ZERO_OR_ONE, expect_identifier_or_string(assign_variable_callback(picture)),
+ "potential", ONE_EXACTLY, success_callback, //TODO
+ "allow", ONE_EXACTLY, success_callback, //TODO
+ "effect", ONE_EXACTLY, success_callback, //TODO
+ "ai_will_do", ZERO_OR_ONE, success_callback //TODO
+ )(node);
+ ret &= add_decision(identifier, alert, news, news_title, news_desc_long, news_desc_medium, news_desc_short, picture);
+ return ret;
+ }
+ )
+ )(root);
+} \ No newline at end of file
diff --git a/src/openvic-simulation/misc/Decision.hpp b/src/openvic-simulation/misc/Decision.hpp
new file mode 100644
index 0000000..e477f64
--- /dev/null
+++ b/src/openvic-simulation/misc/Decision.hpp
@@ -0,0 +1,47 @@
+#pragma once
+
+#include "openvic-simulation/types/IdentifierRegistry.hpp"
+
+namespace OpenVic {
+ struct DecisionManager;
+
+ struct Decision : HasIdentifier {
+ friend struct DecisionManager;
+
+ private:
+ const bool PROPERTY_CUSTOM_PREFIX(alert, has);
+ const bool PROPERTY_CUSTOM_PREFIX(news, is);
+ const std::string PROPERTY(news_title);
+ const std::string PROPERTY(news_desc_long);
+ const std::string PROPERTY(news_desc_medium);
+ const std::string PROPERTY(news_desc_short);
+ const std::string PROPERTY(picture);
+
+ Decision(
+ std::string_view new_identifier, bool new_alert, bool new_news,
+ std::string_view new_news_title, std::string_view new_news_desc_long,
+ std::string_view new_news_desc_medium, std::string_view new_news_desc_short,
+ std::string_view new_picture
+ );
+
+ public:
+ Decision(Decision&&) = default;
+ };
+
+ struct DecisionManager {
+ private:
+ IdentifierRegistry<Decision> decisions;
+
+ public:
+ DecisionManager();
+
+ bool add_decision(
+ std::string_view identifier, bool alert, bool news, std::string_view news_title,
+ std::string_view news_desc_long, std::string_view news_desc_medium,
+ std::string_view news_desc_short, std::string_view picture
+ );
+ IDENTIFIER_REGISTRY_ACCESSORS(decision)
+
+ bool load_decision_file(ast::NodeCPtr root);
+ };
+}