diff options
Diffstat (limited to 'src/openvic-simulation/misc')
-rw-r--r-- | src/openvic-simulation/misc/Decision.cpp | 11 | ||||
-rw-r--r-- | src/openvic-simulation/misc/Define.cpp | 270 | ||||
-rw-r--r-- | src/openvic-simulation/misc/Define.hpp | 88 | ||||
-rw-r--r-- | src/openvic-simulation/misc/Event.cpp | 16 | ||||
-rw-r--r-- | src/openvic-simulation/misc/SongChance.cpp | 4 | ||||
-rw-r--r-- | src/openvic-simulation/misc/SoundEffect.cpp | 4 |
6 files changed, 22 insertions, 371 deletions
diff --git a/src/openvic-simulation/misc/Decision.cpp b/src/openvic-simulation/misc/Decision.cpp index 41d4f81..7812fc1 100644 --- a/src/openvic-simulation/misc/Decision.cpp +++ b/src/openvic-simulation/misc/Decision.cpp @@ -55,12 +55,15 @@ bool DecisionManager::load_decision_file(ast::NodeCPtr root) { "political_decisions", ZERO_OR_ONE, expect_dictionary_reserve_length( decisions, [this](std::string_view identifier, ast::NodeCPtr node) -> bool { + using enum scope_type_t; + bool alert = true, news = false; std::string_view news_title, news_desc_long, news_desc_medium, news_desc_short, picture; - ConditionScript potential { scope_t::COUNTRY, scope_t::COUNTRY, scope_t::NO_SCOPE }; - ConditionScript allow { scope_t::COUNTRY, scope_t::COUNTRY, scope_t::NO_SCOPE }; - ConditionalWeight ai_will_do { scope_t::COUNTRY, scope_t::COUNTRY, scope_t::NO_SCOPE }; + ConditionScript potential { COUNTRY, COUNTRY, NO_SCOPE }; + ConditionScript allow { COUNTRY, COUNTRY, NO_SCOPE }; + ConditionalWeight ai_will_do { COUNTRY, COUNTRY, NO_SCOPE }; EffectScript effect; + 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)), @@ -74,10 +77,12 @@ bool DecisionManager::load_decision_file(ast::NodeCPtr root) { "effect", ONE_EXACTLY, effect.expect_script(), "ai_will_do", ZERO_OR_ONE, ai_will_do.expect_conditional_weight(ConditionalWeight::FACTOR) )(node); + ret &= add_decision( identifier, alert, news, news_title, news_desc_long, news_desc_medium, news_desc_short, picture, std::move(potential), std::move(allow), std::move(ai_will_do), std::move(effect) ); + return ret; } ) diff --git a/src/openvic-simulation/misc/Define.cpp b/src/openvic-simulation/misc/Define.cpp deleted file mode 100644 index c28aab0..0000000 --- a/src/openvic-simulation/misc/Define.cpp +++ /dev/null @@ -1,270 +0,0 @@ -#include "Define.hpp" - -#include <openvic-dataloader/v2script/AbstractSyntaxTree.hpp> - -#include "openvic-simulation/dataloader/NodeTools.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include "openvic-simulation/utility/StringUtils.hpp" - -using namespace OpenVic; -using namespace OpenVic::NodeTools; - -std::string_view Define::type_to_string(Type type) { - using enum Type; - - switch (type) { - case Date: return "date"; - case Country: return "country"; - case Economy: return "economy"; - case Military: return "military"; - case Diplomacy: return "diplomacy"; - case Pops: return "pops"; - case Ai: return "ai"; - case Graphics: return "graphics"; - default: return "unknown"; - } -} - -Define::Type Define::string_to_type(std::string_view str) { - using enum Type; - - static const string_map_t<Define::Type> type_map { - { "country", Country }, - { "economy", Economy }, - { "military", Military }, - { "diplomacy", Diplomacy }, - { "pops", Pops }, - { "ai", Ai }, - { "graphics", Graphics }, - }; - - const string_map_t<Define::Type>::const_iterator type_it = type_map.find(str); - - if (type_it != type_map.end()) { - return type_it->second; - } else { - return Unknown; - } -} - -Define::Define(std::string_view new_identifier, std::string_view new_value, Type new_type) - : HasIdentifier { new_identifier }, value { new_value }, type { new_type } {} - -Date Define::get_value_as_date(bool* successful) const { - return Date::from_string(value, successful); -} - -fixed_point_t Define::get_value_as_fp(bool* successful) const { - return fixed_point_t::parse(value, successful); -} - -int64_t Define::get_value_as_int(bool* successful) const { - return StringUtils::string_to_int64(value, successful); -} - -uint64_t Define::get_value_as_uint(bool* successful) const { - return StringUtils::string_to_uint64(value, successful); -} - -std::ostream& OpenVic::operator<<(std::ostream& os, Define::Type type) { - return os << Define::type_to_string(type); -} - -template<typename T> -bool DefineManager::load_define(T& value, Define::Type type, std::string_view name) const { - static_assert( - std::same_as<T, OpenVic::Date> || std::same_as<T, fixed_point_t> || std::integral<T> - ); - - Define const* define = defines.get_item_by_identifier(name); - - if (define != nullptr) { - if (define->type != type) { - Logger::warning("Mismatched define type for \"", name, "\" - expected ", type, ", got ", define->type); - } - - const auto parse = - [define, &value, &name]<typename U, U (Define::*Func)(bool*) const>(std::string_view type_name) -> bool { - bool success = false; - const U result = (define->*Func)(&success); - if (success) { - value = static_cast<T>(result); - return true; - } else { - Logger::error("Failed to parse ", type_name, " \"", define->get_value(), "\" for define \"", name, "\""); - return false; - } - }; - - if constexpr (std::same_as<T, OpenVic::Date>) { - return parse.template operator()<Date, &Define::get_value_as_date>("date"); - } else if constexpr (std::same_as<T, fixed_point_t>) { - return parse.template operator()<fixed_point_t, &Define::get_value_as_fp>("fixed point"); - } else if constexpr (std::signed_integral<T>) { - return parse.template operator()<int64_t, &Define::get_value_as_int>("signed int"); - } else if constexpr (std::unsigned_integral<T>) { - return parse.template operator()<uint64_t, &Define::get_value_as_uint>("unsigned int"); - } - } else { - Logger::error("Missing define \"", name, "\""); - return false; - } -} - -template<Timespan (*Func)(Timespan::day_t)> -bool DefineManager::_load_define_timespan(Timespan& value, Define::Type type, std::string_view name) const { - Define const* define = defines.get_item_by_identifier(name); - if (define != nullptr) { - if (define->type != type) { - Logger::warning("Mismatched define type for \"", name, "\" - expected ", type, ", got ", define->type); - } - bool success = false; - const int64_t result = define->get_value_as_int(&success); - if (success) { - value = Func(result); - return true; - } else { - Logger::error("Failed to parse days \"", define->get_value(), "\" for define \"", name, "\""); - return false; - } - } else { - Logger::error("Missing define \"", name, "\""); - return false; - } -} - -bool DefineManager::load_define_days(Timespan& value, Define::Type type, std::string_view name) const { - return _load_define_timespan<Timespan::from_days>(value, type, name); -} - -bool DefineManager::load_define_months(Timespan& value, Define::Type type, std::string_view name) const { - return _load_define_timespan<Timespan::from_months>(value, type, name); -} - -bool DefineManager::load_define_years(Timespan& value, Define::Type type, std::string_view name) const { - return _load_define_timespan<Timespan::from_years>(value, type, name); -} - -DefineManager::DefineManager() - : // Date - start_date { 1836, 1, 1 }, - end_date { 1936, 1, 1 }, - - // Country - great_power_rank { 8 }, - lose_great_power_grace_days { Timespan::from_years(1) }, - secondary_power_rank { 16 }, - country_investment_industrial_score_factor { 1 }, - - // Economy - - // Military - pop_size_per_regiment { 1000 }, - min_pop_size_for_regiment { 1000 }, - pop_size_per_regiment_protectorate_multiplier { 1 }, - pop_size_per_regiment_colony_multiplier { 1 }, - pop_size_per_regiment_non_core_multiplier { 1 }, - - // Diplomacy - disarmed_penalty { 0 } - - // Pops - - // Ai - - // Graphics - - {} - -bool DefineManager::add_define(std::string_view name, std::string_view value, Define::Type type) { - if (name.empty()) { - Logger::error("Invalid define identifier - empty!"); - return false; - } - - if (value.empty()) { - Logger::error("Invalid define value for \"", name, "\" - empty!"); - return false; - } - - return defines.add_item({ name, value, type }, duplicate_warning_callback); -} - -bool DefineManager::in_game_period(Date date) const { - return date.in_range(start_date, end_date); -} - -bool DefineManager::load_defines_file(ast::NodeCPtr root) { - using enum Define::Type; - - bool ret = expect_dictionary_keys( - "defines", ONE_EXACTLY, expect_dictionary([this](std::string_view key, ast::NodeCPtr value) -> bool { - - const Define::Type type = Define::string_to_type(key); - - if (type != Unknown) { - - return expect_dictionary_reserve_length( - defines, - [this, type](std::string_view inner_key, ast::NodeCPtr value) -> bool { - return expect_identifier_or_string( - [this, &inner_key, type](std::string_view value) -> bool { - return add_define(inner_key, value, type); - } - )(value); - } - )(value); - - } else if (key == "start_date" || key == "end_date") { - - return expect_identifier_or_string( - [this, &key](std::string_view value) -> bool { - return add_define(key, value, Date); - } - )(value); - - } else { - - Logger::error("Invalid define type - \"", key, "\""); - return false; - - } - }) - )(root); - - lock_defines(); - - // Date - ret &= load_define(start_date, Date, "start_date"); - ret &= load_define(end_date, Date, "end_date"); - - // Country - ret &= load_define(great_power_rank, Country, "GREAT_NATIONS_COUNT"); - ret &= load_define_days(lose_great_power_grace_days, Country, "GREATNESS_DAYS"); - ret &= load_define(secondary_power_rank, Country, "COLONIAL_RANK"); - ret &= load_define(country_investment_industrial_score_factor, Country, "INVESTMENT_SCORE_FACTOR"); - - // Economy - - // Military - ret &= load_define(pop_size_per_regiment, Military, "POP_SIZE_PER_REGIMENT"); - ret &= load_define(min_pop_size_for_regiment, Military, "POP_MIN_SIZE_FOR_REGIMENT"); - ret &= load_define( - pop_size_per_regiment_protectorate_multiplier, Military, "POP_MIN_SIZE_FOR_REGIMENT_PROTECTORATE_MULTIPLIER" - ); - ret &= load_define(pop_size_per_regiment_colony_multiplier, Military, "POP_MIN_SIZE_FOR_REGIMENT_COLONY_MULTIPLIER"); - ret &= load_define(pop_size_per_regiment_non_core_multiplier, Military, "POP_MIN_SIZE_FOR_REGIMENT_NONCORE_MULTIPLIER"); - - // Diplomacy - ret &= load_define(disarmed_penalty, Diplomacy, "DISARMAMENT_ARMY_HIT"); - - // Pops - - // Ai - - // Graphics - - return ret; -} diff --git a/src/openvic-simulation/misc/Define.hpp b/src/openvic-simulation/misc/Define.hpp deleted file mode 100644 index 064883c..0000000 --- a/src/openvic-simulation/misc/Define.hpp +++ /dev/null @@ -1,88 +0,0 @@ -#pragma once - -#include "openvic-simulation/pop/Pop.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" - -namespace OpenVic { - struct DefineManager; - - struct Define : HasIdentifier { - friend struct DefineManager; - - enum class Type : unsigned char { Unknown, Date, Country, Economy, Military, Diplomacy, Pops, Ai, Graphics }; - - static std::string_view type_to_string(Type type); - // This only accepts type names found in defines.lua, so it will never return Type::Date - static Type string_to_type(std::string_view str); - - private: - std::string PROPERTY(value); - const Type PROPERTY(type); - - Define(std::string_view new_identifier, std::string_view new_value, Type new_type); - - public: - Define(Define&&) = default; - - Date get_value_as_date(bool* successful = nullptr) const; - fixed_point_t get_value_as_fp(bool* successful = nullptr) const; - int64_t get_value_as_int(bool* successful = nullptr) const; - uint64_t get_value_as_uint(bool* successful = nullptr) const; - }; - - std::ostream& operator<<(std::ostream& os, Define::Type type); - - struct DefineManager { - private: - IdentifierRegistry<Define> IDENTIFIER_REGISTRY(define); - - // Date - Date PROPERTY(start_date); // start_date - Date PROPERTY(end_date); // end_date - - // Country - size_t PROPERTY(great_power_rank); // GREAT_NATIONS_COUNT - Timespan PROPERTY(lose_great_power_grace_days); // GREATNESS_DAYS - size_t PROPERTY(secondary_power_rank); // COLONIAL_RANK - fixed_point_t PROPERTY(country_investment_industrial_score_factor); // INVESTMENT_SCORE_FACTOR - - // Economy - - // Military - Pop::pop_size_t PROPERTY(pop_size_per_regiment); // POP_SIZE_PER_REGIMENT - Pop::pop_size_t PROPERTY(min_pop_size_for_regiment); // POP_MIN_SIZE_FOR_REGIMENT - // POP_MIN_SIZE_FOR_REGIMENT_PROTECTORATE_MULTIPLIER - fixed_point_t PROPERTY(pop_size_per_regiment_protectorate_multiplier); - fixed_point_t PROPERTY(pop_size_per_regiment_colony_multiplier); // POP_MIN_SIZE_FOR_REGIMENT_COLONY_MULTIPLIER - fixed_point_t PROPERTY(pop_size_per_regiment_non_core_multiplier); // POP_MIN_SIZE_FOR_REGIMENT_NONCORE_MULTIPLIER - - // Diplomacy - fixed_point_t PROPERTY(disarmed_penalty); // DISARMAMENT_ARMY_HIT - - // Pops - - // Ai - - // Graphics - - template<typename T> - bool load_define(T& value, Define::Type type, std::string_view name) const; - - template<Timespan (*Func)(Timespan::day_t)> - bool _load_define_timespan(Timespan& value, Define::Type type, std::string_view name) const; - - bool load_define_days(Timespan& value, Define::Type type, std::string_view name) const; - bool load_define_months(Timespan& value, Define::Type type, std::string_view name) const; - bool load_define_years(Timespan& value, Define::Type type, std::string_view name) const; - - public: - DefineManager(); - - bool add_define(std::string_view name, std::string_view value, Define::Type type); - - bool in_game_period(Date date) const; - - bool load_defines_file(ast::NodeCPtr root); - }; -} diff --git a/src/openvic-simulation/misc/Event.cpp b/src/openvic-simulation/misc/Event.cpp index 5bfc929..64b1f64 100644 --- a/src/openvic-simulation/misc/Event.cpp +++ b/src/openvic-simulation/misc/Event.cpp @@ -119,15 +119,17 @@ bool EventManager::load_event_file(IssueManager const& issue_manager, ast::NodeC return expect_dictionary_reserve_length( events, [this, &issue_manager](std::string_view key, ast::NodeCPtr value) -> bool { + using enum scope_type_t; + Event::event_type_t type; - scope_t initial_scope; + scope_type_t initial_scope; if (key == "country_event") { type = Event::event_type_t::COUNTRY; - initial_scope = scope_t::COUNTRY; + initial_scope = COUNTRY; } else if (key == "province_event") { type = Event::event_type_t::PROVINCE; - initial_scope = scope_t::PROVINCE; + initial_scope = PROVINCE; } else { Logger::error("Invalid event type: ", key); return false; @@ -138,8 +140,8 @@ bool EventManager::load_event_file(IssueManager const& issue_manager, ast::NodeC bool triggered_only = false, major = false, fire_only_once = false, allows_multiple_instances = false, news = false, election = false; IssueGroup const* election_issue_group = nullptr; - ConditionScript trigger { initial_scope, initial_scope, scope_t::NO_SCOPE }; - ConditionalWeight mean_time_to_happen { initial_scope, initial_scope, scope_t::NO_SCOPE }; + ConditionScript trigger { initial_scope, initial_scope, NO_SCOPE }; + ConditionalWeight mean_time_to_happen { initial_scope, initial_scope, NO_SCOPE }; EffectScript immediate; std::vector<Event::EventOption> options; @@ -166,7 +168,7 @@ bool EventManager::load_event_file(IssueManager const& issue_manager, ast::NodeC ConditionalWeight ai_chance { initial_scope, initial_scope, - scope_t::COUNTRY | scope_t::PROVINCE + COUNTRY | PROVINCE // TODO - decide which to use? }; bool ret = expect_dictionary_keys_and_default( @@ -181,7 +183,7 @@ bool EventManager::load_event_file(IssueManager const& issue_manager, ast::NodeC return ret; }, "trigger", ZERO_OR_ONE, trigger.expect_script(), - "mean_time_to_happen", ZERO_OR_ONE, mean_time_to_happen.expect_conditional_weight(ConditionalWeight::MONTHS), + "mean_time_to_happen", ZERO_OR_ONE, mean_time_to_happen.expect_conditional_weight(ConditionalWeight::TIME), "immediate", ZERO_OR_MORE, immediate.expect_script() )(value); ret &= register_event( diff --git a/src/openvic-simulation/misc/SongChance.cpp b/src/openvic-simulation/misc/SongChance.cpp index 94fb571..d05afdf 100644 --- a/src/openvic-simulation/misc/SongChance.cpp +++ b/src/openvic-simulation/misc/SongChance.cpp @@ -17,12 +17,14 @@ bool SongChanceManager::load_songs_file(ast::NodeCPtr root) { ret &= expect_dictionary_reserve_length( song_chances, [this](std::string_view key, ast::NodeCPtr value) -> bool { + using enum scope_type_t; + 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 }; + ConditionalWeight chance { COUNTRY, COUNTRY, NO_SCOPE }; bool ret = expect_dictionary_keys( "name", ONE_EXACTLY, expect_string(assign_variable_callback(name)), diff --git a/src/openvic-simulation/misc/SoundEffect.cpp b/src/openvic-simulation/misc/SoundEffect.cpp index b32d353..6f64406 100644 --- a/src/openvic-simulation/misc/SoundEffect.cpp +++ b/src/openvic-simulation/misc/SoundEffect.cpp @@ -25,14 +25,14 @@ bool SoundEffectManager::_load_sound_define(std::string_view sfx_identifier, ast return false; } - ret &= sound_effects.add_item({sfx_identifier,file,volume}); + 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); + return _load_sound_define(key, value); } )(root); } |