diff options
Diffstat (limited to 'src/openvic-simulation/misc')
-rw-r--r-- | src/openvic-simulation/misc/Define.cpp | 270 | ||||
-rw-r--r-- | src/openvic-simulation/misc/Define.hpp | 88 |
2 files changed, 0 insertions, 358 deletions
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); - }; -} |