aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/misc/SongChance.cpp
diff options
context:
space:
mode:
author Nemrav <50055236+Nemrav@users.noreply.github.com>2024-08-03 21:38:25 +0200
committer GitHub <noreply@github.com>2024-08-03 21:38:25 +0200
commitadc7eb8ad07170ba8da18f684321a92d01447c2c (patch)
treeeedd074580ff5a81b4eff4b8bc43163b7cf59f0e /src/openvic-simulation/misc/SongChance.cpp
parent1f42a6255226b79d271df5060a8391f4ea00fc0a (diff)
parent0b4732befaaf4a742acb319256c535eb449668a7 (diff)
Merge pull request #181 from OpenVicProject/sim_music
Music and Sound Effect define loading
Diffstat (limited to 'src/openvic-simulation/misc/SongChance.cpp')
-rw-r--r--src/openvic-simulation/misc/SongChance.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/openvic-simulation/misc/SongChance.cpp b/src/openvic-simulation/misc/SongChance.cpp
new file mode 100644
index 0000000..4a99362
--- /dev/null
+++ b/src/openvic-simulation/misc/SongChance.cpp
@@ -0,0 +1,53 @@
+#include "SongChance.hpp"
+
+using namespace OpenVic;
+using namespace OpenVic::NodeTools;
+
+SongChance::SongChance(size_t new_index, std::string_view new_filename, ConditionalWeight&& new_chance):
+HasIdentifier { std::to_string(new_index) },
+file_name { new_filename },
+chance { std::move(new_chance) }
+{}
+
+bool SongChance::parse_scripts(DefinitionManager const& definition_manager) {
+ return chance.parse_scripts(definition_manager);
+}
+
+bool SongChanceManager::load_songs_file(ast::NodeCPtr root) {
+ bool ret = true;
+
+ ret &= expect_dictionary_reserve_length(
+ song_chances,
+ [this](std::string_view key, ast::NodeCPtr value) -> bool {
+ if (key != "song") {
+ Logger::error("Invalid song declaration ", key);
+ return false;
+ }
+ std::string_view name {};
+ ConditionalWeight chance { scope_t::COUNTRY, scope_t::COUNTRY, scope_t::NO_SCOPE };
+
+ bool ret = expect_dictionary_keys(
+ "name", ONE_EXACTLY, expect_string(assign_variable_callback(name)),
+ "chance", ONE_EXACTLY, chance.expect_conditional_weight(ConditionalWeight::BASE)
+ )(value);
+
+ ret &= song_chances.add_item({song_chances.size(), name, std::move(chance) });
+ return ret;
+ }
+ )(root);
+
+ if(song_chances.size() == 0) {
+ Logger::error("No songs found in Songs.txt");
+ return false;
+ }
+
+ return ret;
+}
+
+bool SongChanceManager::parse_scripts(DefinitionManager const& definition_manager) {
+ bool ret = true;
+ for (SongChance& songChance : song_chances.get_items()) {
+ ret &= songChance.parse_scripts(definition_manager);
+ }
+ return ret;
+} \ No newline at end of file