aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/misc
diff options
context:
space:
mode:
Diffstat (limited to 'src/openvic-simulation/misc')
-rw-r--r--src/openvic-simulation/misc/SongChance.cpp53
-rw-r--r--src/openvic-simulation/misc/SongChance.hpp30
-rw-r--r--src/openvic-simulation/misc/SoundEffect.cpp38
-rw-r--r--src/openvic-simulation/misc/SoundEffect.hpp29
4 files changed, 150 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
diff --git a/src/openvic-simulation/misc/SongChance.hpp b/src/openvic-simulation/misc/SongChance.hpp
new file mode 100644
index 0000000..f92af7a
--- /dev/null
+++ b/src/openvic-simulation/misc/SongChance.hpp
@@ -0,0 +1,30 @@
+#pragma once
+
+#include <openvic-simulation/types/IdentifierRegistry.hpp>
+#include <openvic-simulation/scripts/ConditionalWeight.hpp>
+
+namespace OpenVic {
+ /*For music/Songs.txt if it exists*/
+ struct SongChanceManager;
+ struct SongChance : HasIdentifier {
+ private:
+ friend struct SongChanceManager;
+ std::string PROPERTY(file_name);
+ ConditionalWeight PROPERTY(chance);
+ SongChance(size_t new_index, std::string_view new_filename, ConditionalWeight&& new_chance);
+ bool parse_scripts(DefinitionManager const& definition_manager);
+
+ public:
+ SongChance(SongChance&&) = default;
+ };
+
+ struct SongChanceManager {
+ private:
+ IdentifierRegistry<SongChance> IDENTIFIER_REGISTRY(song_chance);
+ //Songs.txt
+ public:
+ bool load_songs_file(ast::NodeCPtr root);
+ bool parse_scripts(DefinitionManager const& definition_manager);
+ };
+}
+
diff --git a/src/openvic-simulation/misc/SoundEffect.cpp b/src/openvic-simulation/misc/SoundEffect.cpp
new file mode 100644
index 0000000..df3196b
--- /dev/null
+++ b/src/openvic-simulation/misc/SoundEffect.cpp
@@ -0,0 +1,38 @@
+#include "SoundEffect.hpp"
+
+using namespace OpenVic;
+using namespace OpenVic::NodeTools;
+
+SoundEffect::SoundEffect (
+ std::string_view new_identifier, std::string_view new_file, fixed_point_t new_volume
+) : HasIdentifier { new_identifier }, file { new_file }, volume { new_volume } {}
+
+bool SoundEffectManager::_load_sound_define(std::string_view sfx_identifier, ast::NodeCPtr root) {
+ std::string_view file {};
+ fixed_point_t volume = 1;
+ bool ret = expect_dictionary_keys(
+ "file", ONE_EXACTLY, expect_string(assign_variable_callback(file)),
+ "volume", ZERO_OR_ONE,
+ expect_fixed_point(assign_variable_callback(volume))
+ )(root);
+
+ if (sfx_identifier.empty()) {
+ Logger::error("Invalid sound identifier - empty!");
+ return false;
+ }
+ if(file.empty()) {
+ Logger::error("Invalid sound file name - empty!");
+ return false;
+ }
+
+ ret &= sound_effects.add_item({sfx_identifier,file,volume});
+ return ret;
+}
+
+bool SoundEffectManager::load_sound_defines_file(ast::NodeCPtr root) {
+ return expect_dictionary_reserve_length(sound_effects,
+ [this](std::string_view key, ast::NodeCPtr value) -> bool {
+ return _load_sound_define(key,value);
+ }
+ )(root);
+} \ No newline at end of file
diff --git a/src/openvic-simulation/misc/SoundEffect.hpp b/src/openvic-simulation/misc/SoundEffect.hpp
new file mode 100644
index 0000000..7f18dce
--- /dev/null
+++ b/src/openvic-simulation/misc/SoundEffect.hpp
@@ -0,0 +1,29 @@
+#pragma once
+
+#include <openvic-simulation/types/fixed_point/FixedPoint.hpp>
+#include <openvic-simulation/types/IdentifierRegistry.hpp>
+
+namespace OpenVic {
+ /*For interface/Sound.sfx */
+ struct SoundEffectManager;
+ struct SoundEffect : HasIdentifier {
+ private:
+ friend struct SoundEffectManager;
+ std::string PROPERTY(file);
+ fixed_point_t PROPERTY(volume);
+ SoundEffect(std::string_view new_identifier, std::string_view new_file, fixed_point_t new_volume);
+
+ public:
+ SoundEffect(SoundEffect&&) = default;
+ };
+
+ struct SoundEffectManager {
+
+ private:
+ IdentifierRegistry<SoundEffect> IDENTIFIER_REGISTRY(sound_effect);
+ bool _load_sound_define(std::string_view sfx_identifier, ast::NodeCPtr root);
+
+ public:
+ bool load_sound_defines_file(ast::NodeCPtr root);
+ };
+} \ No newline at end of file