From fa2ca50905f327713207069cf9a2e66cf6c00076 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Tue, 5 Dec 2023 10:16:27 +0100 Subject: feat: implemented invention dataloading --- src/openvic-simulation/GameManager.hpp | 5 +- src/openvic-simulation/dataloader/Dataloader.cpp | 57 ++++++- src/openvic-simulation/dataloader/Dataloader.hpp | 2 + src/openvic-simulation/economy/Good.cpp | 14 +- src/openvic-simulation/military/Unit.cpp | 7 +- src/openvic-simulation/misc/Modifier.cpp | 49 ++++-- src/openvic-simulation/misc/Modifier.hpp | 2 +- src/openvic-simulation/politics/Rebel.cpp | 14 ++ src/openvic-simulation/politics/Rebel.hpp | 6 +- src/openvic-simulation/research/Invention.cpp | 80 ++++++++++ src/openvic-simulation/research/Invention.hpp | 55 +++++++ .../research/ResearchManager.hpp | 12 ++ src/openvic-simulation/research/Technology.cpp | 167 +++++++++++++++++++++ src/openvic-simulation/research/Technology.hpp | 98 ++++++++++++ src/openvic-simulation/tech/Technology.cpp | 165 -------------------- src/openvic-simulation/tech/Technology.hpp | 99 ------------ 16 files changed, 534 insertions(+), 298 deletions(-) create mode 100644 src/openvic-simulation/research/Invention.cpp create mode 100644 src/openvic-simulation/research/Invention.hpp create mode 100644 src/openvic-simulation/research/ResearchManager.hpp create mode 100644 src/openvic-simulation/research/Technology.cpp create mode 100644 src/openvic-simulation/research/Technology.hpp delete mode 100644 src/openvic-simulation/tech/Technology.cpp delete mode 100644 src/openvic-simulation/tech/Technology.hpp diff --git a/src/openvic-simulation/GameManager.hpp b/src/openvic-simulation/GameManager.hpp index ed17c30..0e8eb2b 100644 --- a/src/openvic-simulation/GameManager.hpp +++ b/src/openvic-simulation/GameManager.hpp @@ -11,7 +11,8 @@ #include "openvic-simulation/misc/Define.hpp" #include "openvic-simulation/politics/PoliticsManager.hpp" #include "openvic-simulation/pop/Pop.hpp" -#include "openvic-simulation/tech/Technology.hpp" +#include "openvic-simulation/research/Technology.hpp" +#include "research/ResearchManager.hpp" namespace OpenVic { struct GameManager { @@ -25,7 +26,7 @@ namespace OpenVic { ModifierManager PROPERTY_REF(modifier_manager); PoliticsManager PROPERTY_REF(politics_manager); HistoryManager PROPERTY_REF(history_manager); - TechnologyManager PROPERTY_REF(technology_manager); + ResearchManager PROPERTY_REF(research_manager); PopManager PROPERTY_REF(pop_manager); CountryManager PROPERTY_REF(country_manager); UIManager PROPERTY_REF(ui_manager); diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp index 6bf43f6..ef0b398 100644 --- a/src/openvic-simulation/dataloader/Dataloader.cpp +++ b/src/openvic-simulation/dataloader/Dataloader.cpp @@ -648,10 +648,30 @@ bool Dataloader::_load_goods(GameManager& game_manager) const { return ret; } +bool Dataloader::_load_rebel_types(GameManager& game_manager) const { + static constexpr std::string_view rebel_types_file = "common/rebel_types.txt"; + + PoliticsManager& politics_manager = game_manager.get_politics_manager(); + RebelManager& rebel_manager = politics_manager.get_rebel_manager(); + + bool ret = rebel_manager.load_rebels_file( + politics_manager.get_ideology_manager(), + politics_manager.get_government_type_manager(), + parse_defines(lookup_file(rebel_types_file)).get_file_node() + ); + + if(!rebel_manager.generate_modifiers(game_manager.get_modifier_manager())) { + Logger::error("Failed to generate rebel type-based modifiers!"); + ret &= false; + } + + return ret; +} + bool Dataloader::_load_technologies(GameManager& game_manager) const { static constexpr std::string_view technology_file = "common/technology.txt"; - TechnologyManager& technology_manager = game_manager.get_technology_manager(); + TechnologyManager& technology_manager = game_manager.get_research_manager().get_technology_manager(); bool ret = true; @@ -694,6 +714,28 @@ bool Dataloader::_load_technologies(GameManager& game_manager) const { return ret; } +bool Dataloader::_load_inventions(GameManager& game_manager) const { + static constexpr std::string_view inventions_directory = "inventions"; + + InventionManager& invention_manager = game_manager.get_research_manager().get_invention_manager(); + + bool ret = apply_to_files( + lookup_files_in_dir(inventions_directory, ".txt"), + [&game_manager, &invention_manager](fs::path const& file) -> bool { + return invention_manager.load_inventions_file( + game_manager.get_modifier_manager(), + game_manager.get_military_manager().get_unit_manager(), + game_manager.get_economy_manager().get_building_manager(), + parse_defines(file).get_file_node() + ); + } + ); + + invention_manager.lock_inventions(); + + return ret; +} + bool Dataloader::_load_history(GameManager& game_manager, bool unused_history_file_warnings) const { /* Country History */ @@ -910,7 +952,6 @@ bool Dataloader::load_defines(GameManager& game_manager) const { static constexpr std::string_view event_modifiers_file = "common/event_modifiers.txt"; static constexpr std::string_view static_modifiers_file = "common/static_modifiers.txt"; static constexpr std::string_view triggered_modifiers_file = "common/triggered_modifiers.txt"; - static constexpr std::string_view rebel_types_file = "common/rebel_types.txt"; bool ret = true; @@ -1001,6 +1042,10 @@ bool Dataloader::load_defines(GameManager& game_manager) const { Logger::error("Failed to load buildings!"); ret = false; } + if (!_load_rebel_types(game_manager)) { + Logger::error("Failed to load rebel types!"); + ret = false; + } if (!_load_technologies(game_manager)) { ret = false; } @@ -1028,6 +1073,10 @@ bool Dataloader::load_defines(GameManager& game_manager) const { Logger::error("Failed to load triggered modifiers!"); ret = false; } + if (!_load_inventions(game_manager)) { + Logger::error("Failed to load inventions!"); + ret = false; + } if (!_load_map_dir(game_manager)) { Logger::error("Failed to load map!"); ret = false; @@ -1056,10 +1105,6 @@ bool Dataloader::load_defines(GameManager& game_manager) const { Logger::error("Failed to load countries!"); ret = false; } - if (!game_manager.get_politics_manager().load_rebels_file(parse_defines(lookup_file(rebel_types_file)).get_file_node())) { - Logger::error("Failed to load rebel types!"); - 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 a2e377b..f0a0678 100644 --- a/src/openvic-simulation/dataloader/Dataloader.hpp +++ b/src/openvic-simulation/dataloader/Dataloader.hpp @@ -28,7 +28,9 @@ namespace OpenVic { bool _load_pop_types(PopManager& pop_manager, UnitManager const& unit_manager, GoodManager const& good_manager) const; bool _load_units(GameManager& game_manager) const; bool _load_goods(GameManager& game_manager) const; + bool _load_rebel_types(GameManager& game_manager) const; bool _load_technologies(GameManager& game_manager) const; + bool _load_inventions(GameManager& game_manager) const; bool _load_map_dir(GameManager& game_manager) const; bool _load_history(GameManager& game_manager, bool unused_history_file_warnings) const; diff --git a/src/openvic-simulation/economy/Good.cpp b/src/openvic-simulation/economy/Good.cpp index 2aa5d42..02c4f0b 100644 --- a/src/openvic-simulation/economy/Good.cpp +++ b/src/openvic-simulation/economy/Good.cpp @@ -99,16 +99,20 @@ bool GoodManager::load_goods_file(ast::NodeCPtr root) { #define GOOD_MODIFIER(name) \ modifier_manager.register_complex_modifier(name); \ for (Good const& good : this->get_goods()) { \ - std::string modifier_name = name; \ - modifier_name += "_"; \ - modifier_name += good.get_identifier(); \ - ret &= modifier_manager.add_modifier_effect(modifier_name, true, ModifierEffect::format_t::PROPORTION_DECIMAL); \ + ret &= modifier_manager.add_modifier_effect( \ + StringUtils::append_string_views(name, "_", good.get_identifier()), \ + true, \ + ModifierEffect::format_t::PROPORTION_DECIMAL \ + ); \ } bool GoodManager::generate_modifiers(ModifierManager& modifier_manager) { bool ret = true; GOOD_MODIFIER("factory_goods_output"); + GOOD_MODIFIER("factory_goods_throughput"); GOOD_MODIFIER("rgo_goods_output"); GOOD_MODIFIER("rgo_size"); return ret; -} \ No newline at end of file +} + +#undef GOOD_MODIFIER \ No newline at end of file diff --git a/src/openvic-simulation/military/Unit.cpp b/src/openvic-simulation/military/Unit.cpp index 7995aeb..ed3d37b 100644 --- a/src/openvic-simulation/military/Unit.cpp +++ b/src/openvic-simulation/military/Unit.cpp @@ -191,12 +191,11 @@ bool UnitManager::load_unit_file(GoodManager const& good_manager, ast::NodeCPtr } #define STAT_MODIFIER(name, positive_good, format) \ - ret &= modifier_manager.add_modifier_effect(base_name + name, positive_good, ModifierEffect::format_t::format) + ret &= modifier_manager.add_modifier_effect(StringUtils::append_string_views(identifier, "_", name), positive_good, ModifierEffect::format_t::format) bool UnitManager::generate_modifiers(ModifierManager& modifier_manager) { std::function generate_stat_modifiers = [this, &modifier_manager](std::string_view identifier, Unit::type_t type) -> bool { modifier_manager.register_complex_modifier(identifier); - std::string base_name = std::string(identifier) + "_"; bool ret = true; STAT_MODIFIER("default_organisation", true, RAW_DECIMAL); @@ -237,4 +236,6 @@ bool UnitManager::generate_modifiers(ModifierManager& modifier_manager) { ret &= generate_stat_modifiers(unit.get_identifier(), unit.get_type()); return ret; -} \ No newline at end of file +} + +#undef STAT_MODIFIER \ No newline at end of file diff --git a/src/openvic-simulation/misc/Modifier.cpp b/src/openvic-simulation/misc/Modifier.cpp index 6ad14aa..87a6b65 100644 --- a/src/openvic-simulation/misc/Modifier.cpp +++ b/src/openvic-simulation/misc/Modifier.cpp @@ -1,4 +1,5 @@ #include "Modifier.hpp" +#include using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -196,6 +197,17 @@ bool ModifierManager::setup_modifier_effects() { ret &= add_modifier_effect("dig_in_cap", true, INT); ret &= add_modifier_effect("max_national_focus", true, INT); ret &= add_modifier_effect("regular_experience_level", true, RAW_DECIMAL); + ret &= add_modifier_effect("land_attrition", false); + ret &= add_modifier_effect("naval_attrition", false); + ret &= add_modifier_effect("permanent_prestige", true, RAW_DECIMAL); + ret &= add_modifier_effect("shared_prestige", true, RAW_DECIMAL); + ret &= add_modifier_effect("colonial_prestige", true); + ret &= add_modifier_effect("soldier_to_pop_loss", true); + ret &= add_modifier_effect("pop_growth", true); + ret &= add_modifier_effect("plurality", true, PERCENTAGE_DECIMAL); + ret &= add_modifier_effect("suppression_points_modifier", true); + ret &= add_modifier_effect("colonial_life_rating", false, INT); + ret &= add_modifier_effect("seperatism", false); //paradox typo /* Province Modifier Effects */ ret &= add_modifier_effect("assimilation_rate", true); @@ -359,7 +371,7 @@ bool ModifierManager::load_triggered_modifiers(ast::NodeCPtr root) { key_value_callback_t ModifierManager::_modifier_effect_callback( ModifierValue& modifier, key_value_callback_t default_callback, ModifierEffectValidator auto effect_validator ) const { - std::function add_modifier_cb = [this, &modifier, effect_validator](ModifierEffect const* effect, ast::NodeCPtr value) -> bool { + const auto add_modifier_cb = [this, &modifier, effect_validator](ModifierEffect const* effect, ast::NodeCPtr value) -> bool { if (effect_validator(*effect)) { if (!modifier.values.contains(effect)) { return expect_fixed_point(assign_variable_callback(modifier.values[effect]))(value); @@ -373,23 +385,32 @@ key_value_callback_t ModifierManager::_modifier_effect_callback( } }; - return [this, &modifier, default_callback, effect_validator, add_modifier_cb](std::string_view key, ast::NodeCPtr value) -> bool { + const auto add_flattened_modifier_cb = [this, add_modifier_cb](std::string_view prefix, std::string_view key, ast::NodeCPtr value) -> bool { + const std::string flat_identifier = StringUtils::append_string_views(prefix, "_", key); + ModifierEffect const* effect = get_modifier_effect_by_identifier(flat_identifier); + if (effect != nullptr) { + return add_modifier_cb(effect, value); + } else { + Logger::error("Could not find flattened modifier: ", flat_identifier); + return false; + } + }; + + return [this, default_callback, add_modifier_cb, add_flattened_modifier_cb](std::string_view key, ast::NodeCPtr value) -> bool { ModifierEffect const* effect = get_modifier_effect_by_identifier(key); if (effect != nullptr && value->is_type()) { return add_modifier_cb(effect, value); } else if (complex_modifiers.contains(key) && value->is_derived_from()) { - return expect_dictionary([this, &key, &add_modifier_cb, &default_callback](std::string_view identifier, ast::NodeCPtr node) -> bool { - std::string flat_identifier = std::string(key); - flat_identifier += "_"; - flat_identifier += identifier; - ModifierEffect const* effect = get_modifier_effect_by_identifier(flat_identifier); - if(effect != nullptr) { - return add_modifier_cb(effect, node); - } else { - Logger::error("Could not find flattened modifier: ", flat_identifier); - return false; - } - })(value); + if (key == "rebel_org_gain") { //because of course there's a special one + std::string_view faction_identifier; + ast::NodeCPtr value_node = nullptr; + bool ret = expect_dictionary_keys( + "faction", ONE_EXACTLY, expect_identifier(assign_variable_callback(faction_identifier)), + "value", ONE_EXACTLY, assign_variable_callback(value_node) + )(value); + ret &= add_flattened_modifier_cb(key, faction_identifier, value_node); + return ret; + } else return expect_dictionary(std::bind(add_flattened_modifier_cb, key, std::placeholders::_1, std::placeholders::_2))(value); } else return default_callback(key, value); }; } diff --git a/src/openvic-simulation/misc/Modifier.hpp b/src/openvic-simulation/misc/Modifier.hpp index 9665e07..f3678d9 100644 --- a/src/openvic-simulation/misc/Modifier.hpp +++ b/src/openvic-simulation/misc/Modifier.hpp @@ -11,7 +11,7 @@ namespace OpenVic { PROPORTION_DECIMAL, /* An unscaled fraction/ratio, with 1 being "full"/"whole" */ PERCENTAGE_DECIMAL, /* A fraction/ratio scaled so that 100 is "full"/"whole" */ RAW_DECIMAL, /* A continuous quantity, e.g. attack strength */ - INT /* A discrete quantity, e.g. building count limit */ + INT /* A discrete quantity, e.g. building count limit */ }; friend std::unique_ptr std::make_unique(std::string_view&&, bool&&, format_t&&); diff --git a/src/openvic-simulation/politics/Rebel.cpp b/src/openvic-simulation/politics/Rebel.cpp index e58b3de..ec39c9e 100644 --- a/src/openvic-simulation/politics/Rebel.cpp +++ b/src/openvic-simulation/politics/Rebel.cpp @@ -141,5 +141,19 @@ bool RebelManager::load_rebels_file( lock_rebel_types(); + return ret; +} + +bool RebelManager::generate_modifiers(ModifierManager& modifier_manager) { + bool ret = true; + + modifier_manager.register_complex_modifier("rebel_org_gain"); + + ret &= modifier_manager.add_modifier_effect("rebel_org_gain_all", false); + + for (RebelType const& rebel_type : get_rebel_types()) { + std::string modifier_name = StringUtils::append_string_views("rebel_org_gain_", rebel_type.get_identifier()); + ret &= modifier_manager.add_modifier_effect(modifier_name, false); + } return ret; } \ No newline at end of file diff --git a/src/openvic-simulation/politics/Rebel.hpp b/src/openvic-simulation/politics/Rebel.hpp index f012829..ba7b33f 100644 --- a/src/openvic-simulation/politics/Rebel.hpp +++ b/src/openvic-simulation/politics/Rebel.hpp @@ -1,5 +1,6 @@ #pragma once +#include "openvic-simulation/misc/Modifier.hpp" #include "openvic-simulation/types/IdentifierRegistry.hpp" #include "openvic-simulation/politics/Government.hpp" #include "openvic-simulation/politics/Ideology.hpp" @@ -78,8 +79,7 @@ namespace OpenVic { bool reinforcing, bool general, bool smart, bool unit_transfer, fixed_point_t occupation_mult ); - bool load_rebels_file( - IdeologyManager const& ideology_manager, GovernmentTypeManager const& government_type_manager, ast::NodeCPtr root - ); + bool load_rebels_file(IdeologyManager const& ideology_manager, GovernmentTypeManager const& government_type_manager, ast::NodeCPtr root); + bool generate_modifiers(ModifierManager& modifier_manager); }; } \ No newline at end of file diff --git a/src/openvic-simulation/research/Invention.cpp b/src/openvic-simulation/research/Invention.cpp new file mode 100644 index 0000000..eb4e342 --- /dev/null +++ b/src/openvic-simulation/research/Invention.cpp @@ -0,0 +1,80 @@ +#include "Invention.hpp" +#include +#include +#include + +using namespace OpenVic; +using namespace OpenVic::NodeTools; + +Invention::Invention( + std::string_view identifier, ModifierValue&& values, bool news, unit_set_t activated_units, building_set_t activated_buildings, + crime_set_t enabled_crimes, bool unlock_gas_attack, bool unlock_gas_defence) + : Modifier { identifier, std::move(values), 0 }, + news { news }, activated_units { std::move(activated_units) }, activated_buildings { std::move(activated_buildings) }, + enabled_crimes { std::move(enabled_crimes) }, unlock_gas_attack { unlock_gas_attack }, unlock_gas_defence { unlock_gas_defence } {} //TODO icon + +InventionManager::InventionManager() : inventions { "inventions" } {} + +bool InventionManager::add_invention( + std::string_view identifier, ModifierValue&& values, bool news, Invention::unit_set_t activated_units, + Invention::building_set_t activated_buildings, Invention::crime_set_t enabled_crimes, + bool unlock_gas_attack, bool unlock_gas_defence +) { + if (identifier.empty()) { + Logger::error("Invalid invention identifier - empty!"); + return false; + } + + return inventions.add_item({ + identifier, std::move(values), news, std::move(activated_units), std::move(activated_buildings), + std::move(enabled_crimes), unlock_gas_attack, unlock_gas_defence + }); +} + +bool InventionManager::load_inventions_file( + ModifierManager const& modifier_manager, UnitManager const& unit_manager, BuildingManager const& building_manager, ast::NodeCPtr root +) { + return expect_dictionary_reserve_length(inventions, [this, &modifier_manager, &unit_manager, &building_manager](std::string_view identifier, ast::NodeCPtr value) -> bool { + ModifierValue loose_modifiers; + ModifierValue modifiers; + + Invention::unit_set_t activated_units; + Invention::building_set_t activated_buildings; + Invention::crime_set_t enabled_crimes; + + bool unlock_gas_attack = false; + bool unlock_gas_defence = false; + bool news = true; //defaults to true! + + bool ret = modifier_manager.expect_modifier_value_and_keys(move_variable_callback(loose_modifiers), + "news", ZERO_OR_ONE, expect_bool(assign_variable_callback(news)), + "limit", ONE_EXACTLY, success_callback, + "chance", ONE_EXACTLY, success_callback, + "effect", ZERO_OR_ONE, modifier_manager.expect_modifier_value_and_keys( + move_variable_callback(modifiers), + "gas_attack", ZERO_OR_ONE, expect_bool(assign_variable_callback(unlock_gas_attack)), + "gas_defence", ZERO_OR_ONE, expect_bool(assign_variable_callback(unlock_gas_defence)), + "activate_unit", ZERO_OR_MORE, unit_manager.expect_unit_identifier([this, &activated_units](Unit const& unit) -> bool { + activated_units.insert(&unit); + return true; + }), + "activate_building", ZERO_OR_MORE, building_manager.expect_building_type_identifier([this, &activated_buildings](BuildingType const& type) -> bool { + activated_buildings.insert(&type); + return true; + }), + "enable_crime", ZERO_OR_ONE, modifier_manager.expect_crime_modifier_identifier([this, &enabled_crimes](Crime const& crime) -> bool { + enabled_crimes.insert(&crime); + return true; + })) + )(value); + + modifiers += loose_modifiers; + + ret &= add_invention( + identifier, std::move(modifiers), news, activated_units, activated_buildings, enabled_crimes, + unlock_gas_attack, unlock_gas_defence + ); + + return ret; + })(root); +} \ No newline at end of file diff --git a/src/openvic-simulation/research/Invention.hpp b/src/openvic-simulation/research/Invention.hpp new file mode 100644 index 0000000..321d982 --- /dev/null +++ b/src/openvic-simulation/research/Invention.hpp @@ -0,0 +1,55 @@ +#pragma once + +#include "openvic-simulation/economy/BuildingType.hpp" +#include "openvic-simulation/military/Unit.hpp" +#include "openvic-simulation/misc/Modifier.hpp" +#include "openvic-simulation/types/IdentifierRegistry.hpp" +#include +#include + +namespace OpenVic { + struct Invention : Modifier { + friend struct InventionManager; + //TODO implement limit and chance + using unit_set_t = std::unordered_set; + using building_set_t = std::unordered_set; + using crime_set_t = std::unordered_set; + + private: + const bool PROPERTY_CUSTOM_PREFIX(news, is); + unit_set_t PROPERTY(activated_units); + building_set_t PROPERTY(activated_buildings); + crime_set_t PROPERTY(enabled_crimes); + const bool PROPERTY_CUSTOM_PREFIX(unlock_gas_attack, will); + const bool PROPERTY_CUSTOM_PREFIX(unlock_gas_defence, will); + + Invention( + std::string_view identifier, ModifierValue&& values, bool news, unit_set_t activated_units, + building_set_t activated_buildings, crime_set_t enabled_crimes, bool unlock_gas_attack, + bool unlock_gas_defence + ); + + public: + Invention(Invention&&) = default; + }; + + struct InventionManager { + IdentifierRegistry inventions; + + public: + InventionManager(); + + bool add_invention( + std::string_view identifier, ModifierValue&& values, bool news, Invention::unit_set_t activated_units, + Invention::building_set_t activated_buildings, Invention::crime_set_t enabled_crimes, bool unlock_gas_attack, + bool unlock_gas_defence + ); + + IDENTIFIER_REGISTRY_ACCESSORS(invention); + + bool load_inventions_file( + ModifierManager const& modifier_manager, UnitManager const& unit_manager, BuildingManager const& building_manager, + ast::NodeCPtr root + ); // inventions/*.txt + }; +} \ No newline at end of file diff --git a/src/openvic-simulation/research/ResearchManager.hpp b/src/openvic-simulation/research/ResearchManager.hpp new file mode 100644 index 0000000..d402a34 --- /dev/null +++ b/src/openvic-simulation/research/ResearchManager.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include "openvic-simulation/research/Invention.hpp" +#include "openvic-simulation/research/Technology.hpp" + +namespace OpenVic { + struct ResearchManager { + private: + TechnologyManager PROPERTY_REF(technology_manager); + InventionManager PROPERTY_REF(invention_manager); + }; +} diff --git a/src/openvic-simulation/research/Technology.cpp b/src/openvic-simulation/research/Technology.cpp new file mode 100644 index 0000000..07faaa3 --- /dev/null +++ b/src/openvic-simulation/research/Technology.cpp @@ -0,0 +1,167 @@ +#include "Technology.hpp" + +using namespace OpenVic; +using namespace OpenVic::NodeTools; + +TechnologyFolder::TechnologyFolder(std::string_view new_identifier) : HasIdentifier { new_identifier } {} + +TechnologyArea::TechnologyArea(std::string_view new_identifier, TechnologyFolder const& new_folder) + : HasIdentifier { new_identifier }, folder { new_folder } {} + +Technology::Technology( + std::string_view new_identifier, TechnologyArea const& new_area, Date::year_t new_year, fixed_point_t new_cost, + bool new_unciv_military, uint8_t new_unit, unit_set_t&& new_activated_units, building_set_t&& new_activated_buildings, + ModifierValue&& new_values +) : Modifier { new_identifier, std::move(new_values), 0 }, area { new_area }, year { new_year }, cost { new_cost }, + unciv_military { new_unciv_military }, unit { new_unit }, activated_buildings { std::move(new_activated_units) }, + activated_units { std::move(new_activated_buildings) } {} + +TechnologySchool::TechnologySchool(std::string_view new_identifier, ModifierValue&& new_values) + : Modifier { new_identifier, std::move(new_values), 0 } {} + +TechnologyManager::TechnologyManager() + : technology_folders { "technology folders" }, technology_areas { "technology areas" }, technologies { "technologies" }, + technology_schools { "technology schools" } {} + +bool TechnologyManager::add_technology_folder(std::string_view identifier) { + if (identifier.empty()) { + Logger::error("Invalid technology folder identifier - empty!"); + return false; + } + + return technology_folders.add_item({ identifier }); +} + +bool TechnologyManager::add_technology_area(std::string_view identifier, TechnologyFolder const* folder) { + if (identifier.empty()) { + Logger::error("Invalid technology area identifier - empty!"); + return false; + } + + if (folder == nullptr) { + Logger::error("Null folder for technology area \"", identifier, "\"!"); + return false; + } + + return technology_areas.add_item({ identifier, *folder }); +} + +bool TechnologyManager::add_technology( + std::string_view identifier, TechnologyArea const* area, Date::year_t year, fixed_point_t cost, bool unciv_military, + uint8_t unit, Technology::unit_set_t&& activated_units, Technology::building_set_t&& activated_buildings, + ModifierValue&& values +) { + if (identifier.empty()) { + Logger::error("Invalid technology identifier - empty!"); + return false; + } + + if (area == nullptr) { + Logger::error("Null area for technology \"", identifier, "\"!"); + return false; + } + + return technologies.add_item({ + identifier, *area, year, cost, unciv_military, unit, std::move(activated_units), std::move(activated_buildings), + std::move(values) + }); +} + +bool TechnologyManager::add_technology_school(std::string_view identifier, ModifierValue&& values) { + if (identifier.empty()) { + Logger::error("Invalid modifier effect identifier - empty!"); + return false; + } + + return technology_schools.add_item({ identifier, std::move(values) }); +} + +bool TechnologyManager::load_technology_file_areas(ast::NodeCPtr root) { + return expect_dictionary_reserve_length(technology_folders, [this](std::string_view root_key, ast::NodeCPtr root_value) -> bool { + if (root_key == "folders") { + return expect_dictionary([this](std::string_view folder_key, ast::NodeCPtr folder_value) -> bool { + bool ret = add_technology_folder(folder_key); + TechnologyFolder const* current_folder = get_technology_folder_by_identifier(folder_key); + if (current_folder == nullptr) + return false; + ret &= expect_list(expect_identifier([this, current_folder](std::string_view area) -> bool { + return add_technology_area(area, current_folder); + }))(folder_value); + return ret; + })(root_value); + lock_technology_folders(); + lock_technology_areas(); + } else if (root_key == "schools") return true; //ignore + else return false; + })(root); +} + +bool TechnologyManager::load_technology_file_schools(ModifierManager const& modifier_manager, ast::NodeCPtr root) { + return expect_dictionary([this, &modifier_manager](std::string_view root_key, ast::NodeCPtr root_value) -> bool { + if (root_key == "schools") { + return expect_dictionary_reserve_length(technology_schools, [this, &modifier_manager](std::string_view school_key, ast::NodeCPtr school_value) -> bool { + ModifierValue modifiers; + bool ret = modifier_manager.expect_modifier_value(move_variable_callback(modifiers))(school_value); + ret &= add_technology_school(school_key, std::move(modifiers)); + return ret; + })(root_value); + lock_technology_schools(); + } else if (root_key == "folders") return true; //ignore + else return false; + })(root); +} + +bool TechnologyManager::load_technologies_file(ModifierManager const& modifier_manager, UnitManager const& unit_manager, BuildingManager const& building_manager, ast::NodeCPtr root) { + return expect_dictionary_reserve_length(technologies, [this, &modifier_manager, &unit_manager, &building_manager](std::string_view tech_key, ast::NodeCPtr tech_value) -> bool { + ModifierValue modifiers; + TechnologyArea const* area = nullptr; + Date::year_t year = 0; + fixed_point_t cost = 0; + bool unciv_military = false; + uint8_t unit = 0; + Technology::unit_set_t activated_units; + Technology::building_set_t activated_buildings; + + bool ret = modifier_manager.expect_modifier_value_and_keys(move_variable_callback(modifiers), + "area", ONE_EXACTLY, expect_technology_area_identifier(assign_variable_callback_pointer(area)), + "year", ONE_EXACTLY, expect_uint(assign_variable_callback(year)), + "cost", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(cost)), + "unciv_military", ZERO_OR_ONE, expect_bool(assign_variable_callback(unciv_military)), + "unit", ZERO_OR_ONE, expect_uint(assign_variable_callback(unit)), + "activate_unit", ZERO_OR_MORE, unit_manager.expect_unit_identifier([&activated_units](Unit const& unit) -> bool { + activated_units.insert(&unit); + return true; + }), + "activate_building", ZERO_OR_MORE, building_manager.expect_building_type_identifier( + [&activated_buildings](BuildingType const& building_type) -> bool { + activated_buildings.insert(&building_type); + return true; + } + ), + "ai_chance", ONE_EXACTLY, success_callback //TODO + )(tech_value); + + ret &= add_technology( + tech_key, area, year, cost, unciv_military, unit, std::move(activated_units), std::move(activated_buildings), + std::move(modifiers) + ); + return ret; + })(root); +} + +#define TECH_MODIFIER(NAME, POS) ret &= modifier_manager.add_modifier_effect(NAME, POS, ModifierEffect::format_t::PROPORTION_DECIMAL) +#define UNCIV_TECH_MODIFIER(NAME) TECH_MODIFIER(NAME, false); TECH_MODIFIER(StringUtils::append_string_views("self_", NAME), false); +bool TechnologyManager::generate_modifiers(ModifierManager& modifier_manager) { + bool ret = true; + + UNCIV_TECH_MODIFIER("unciv_military_modifier"); + UNCIV_TECH_MODIFIER("unciv_economic_modifier"); + + for (TechnologyFolder const& folder : get_technology_folders()) { + TECH_MODIFIER(StringUtils::append_string_views(folder.get_identifier(), "_research_bonus"), true); + } + + return ret; +} +#undef UNCIV_TECH_MODIFIER +#undef TECH_MODIFIER \ No newline at end of file diff --git a/src/openvic-simulation/research/Technology.hpp b/src/openvic-simulation/research/Technology.hpp new file mode 100644 index 0000000..0939369 --- /dev/null +++ b/src/openvic-simulation/research/Technology.hpp @@ -0,0 +1,98 @@ +#pragma once + +#include "openvic-simulation/misc/Modifier.hpp" +#include "openvic-simulation/types/Date.hpp" +#include "openvic-simulation/military/Unit.hpp" +#include "openvic-simulation/economy/BuildingType.hpp" +#include + +namespace OpenVic { + struct TechnologyFolder : HasIdentifier { + friend struct TechnologyManager; + + private: + TechnologyFolder(std::string_view new_identifier); + + public: + TechnologyFolder(TechnologyFolder&&) = default; + }; + + struct TechnologyArea : HasIdentifier { + friend struct TechnologyManager; + + private: + TechnologyFolder const& PROPERTY(folder); + + TechnologyArea(std::string_view new_identifier, TechnologyFolder const& new_folder); + + public: + TechnologyArea(TechnologyArea&&) = default; + }; + + struct Technology : Modifier { + friend struct TechnologyManager; + + using unit_set_t = std::unordered_set; + using building_set_t = std::unordered_set; + + private: + TechnologyArea const& PROPERTY(area); + const Date::year_t PROPERTY(year); + const fixed_point_t PROPERTY(cost); + const bool PROPERTY(unciv_military); + const uint8_t PROPERTY(unit); + const unit_set_t PROPERTY(activated_buildings); + const building_set_t PROPERTY(activated_units); + + //TODO: implement rules/modifiers and ai_chance + + Technology( + std::string_view new_identifier, TechnologyArea const& new_area, Date::year_t new_year, fixed_point_t new_cost, + bool new_unciv_military, uint8_t new_unit, unit_set_t&& new_activated_units, + building_set_t&& new_activated_buildings, ModifierValue&& new_values + ); + + public: + Technology(Technology&&) = default; + }; + + struct TechnologySchool : Modifier { + friend struct TechnologyManager; + + private: + TechnologySchool(std::string_view new_identifier, ModifierValue&& new_values); + }; + + struct TechnologyManager { + IdentifierRegistry technology_folders; + IdentifierRegistry technology_areas; + IdentifierRegistry technologies; + IdentifierRegistry technology_schools; + + public: + TechnologyManager(); + + bool add_technology_folder(std::string_view identifier); + IDENTIFIER_REGISTRY_ACCESSORS(technology_folder) + + bool add_technology_area(std::string_view identifier, TechnologyFolder const* folder); + IDENTIFIER_REGISTRY_ACCESSORS(technology_area) + + bool add_technology( + std::string_view identifier, TechnologyArea const* area, Date::year_t year, fixed_point_t cost, + bool unciv_military, uint8_t unit, Technology::unit_set_t&& activated_units, + Technology::building_set_t&& activated_buildings, ModifierValue&& values); + IDENTIFIER_REGISTRY_ACCESSORS_CUSTOM_PLURAL(technology, technologies) + + bool add_technology_school(std::string_view identifier, ModifierValue&& values); + IDENTIFIER_REGISTRY_ACCESSORS(technology_school); + + bool load_technology_file_areas(ast::NodeCPtr root); // common/technology.txt + bool load_technology_file_schools(ModifierManager const& modifier_manager, ast::NodeCPtr root); // also common/technology.txt + bool load_technologies_file( + ModifierManager const& modifier_manager, UnitManager const& unit_manager, BuildingManager const& building_manager, + ast::NodeCPtr root + ); // technologies/*.txt + bool generate_modifiers(ModifierManager& modifier_manager); + }; +} \ No newline at end of file diff --git a/src/openvic-simulation/tech/Technology.cpp b/src/openvic-simulation/tech/Technology.cpp deleted file mode 100644 index 1234ff3..0000000 --- a/src/openvic-simulation/tech/Technology.cpp +++ /dev/null @@ -1,165 +0,0 @@ -#include "Technology.hpp" - -using namespace OpenVic; -using namespace OpenVic::NodeTools; - -TechnologyFolder::TechnologyFolder(std::string_view new_identifier) : HasIdentifier { new_identifier } {} - -TechnologyArea::TechnologyArea(std::string_view new_identifier, TechnologyFolder const& new_folder) - : HasIdentifier { new_identifier }, folder { new_folder } {} - -Technology::Technology( - std::string_view new_identifier, TechnologyArea const& new_area, year_t new_year, fixed_point_t new_cost, - bool new_unciv_military, uint8_t new_unit, unit_set_t&& new_activated_units, building_set_t&& new_activated_buildings, - ModifierValue&& new_values -) : Modifier { new_identifier, std::move(new_values), 0 }, area { new_area }, year { new_year }, cost { new_cost }, - unciv_military { new_unciv_military }, unit { new_unit }, activated_buildings { std::move(new_activated_units) }, - activated_units { std::move(new_activated_buildings) } {} - -TechnologySchool::TechnologySchool(std::string_view new_identifier, ModifierValue&& new_values) - : Modifier { new_identifier, std::move(new_values), 0 } {} - -TechnologyManager::TechnologyManager() - : technology_folders { "technology folders" }, technology_areas { "technology areas" }, technologies { "technologies" }, - technology_schools { "technology schools" } {} - -bool TechnologyManager::add_technology_folder(std::string_view identifier) { - if (identifier.empty()) { - Logger::error("Invalid technology folder identifier - empty!"); - return false; - } - - return technology_folders.add_item({ identifier }); -} - -bool TechnologyManager::add_technology_area(std::string_view identifier, TechnologyFolder const* folder) { - if (identifier.empty()) { - Logger::error("Invalid technology area identifier - empty!"); - return false; - } - - if (folder == nullptr) { - Logger::error("Null folder for technology area \"", identifier, "\"!"); - return false; - } - - return technology_areas.add_item({ identifier, *folder }); -} - -bool TechnologyManager::add_technology( - std::string_view identifier, TechnologyArea const* area, Technology::year_t year, fixed_point_t cost, bool unciv_military, - uint8_t unit, Technology::unit_set_t&& activated_units, Technology::building_set_t&& activated_buildings, - ModifierValue&& values -) { - if (identifier.empty()) { - Logger::error("Invalid technology identifier - empty!"); - return false; - } - - if (area == nullptr) { - Logger::error("Null area for technology \"", identifier, "\"!"); - return false; - } - - return technologies.add_item({ - identifier, *area, year, cost, unciv_military, unit, std::move(activated_units), std::move(activated_buildings), - std::move(values) - }); -} - -bool TechnologyManager::add_technology_school(std::string_view identifier, ModifierValue&& values) { - if (identifier.empty()) { - Logger::error("Invalid modifier effect identifier - empty!"); - return false; - } - - return technology_schools.add_item({ identifier, std::move(values) }); -} - -bool TechnologyManager::load_technology_file_areas(ast::NodeCPtr root) { - return expect_dictionary_reserve_length(technology_folders, [this](std::string_view root_key, ast::NodeCPtr root_value) -> bool { - if (root_key == "folders") { - return expect_dictionary([this](std::string_view folder_key, ast::NodeCPtr folder_value) -> bool { - bool ret = add_technology_folder(folder_key); - TechnologyFolder const* current_folder = get_technology_folder_by_identifier(folder_key); - if (current_folder == nullptr) - return false; - ret &= expect_list(expect_identifier([this, current_folder](std::string_view area) -> bool { - return add_technology_area(area, current_folder); - }))(folder_value); - return ret; - })(root_value); - lock_technology_folders(); - lock_technology_areas(); - } else if (root_key == "schools") return true; //ignore - else return false; - })(root); -} - -bool TechnologyManager::load_technology_file_schools(ModifierManager const& modifier_manager, ast::NodeCPtr root) { - return expect_dictionary([this, &modifier_manager](std::string_view root_key, ast::NodeCPtr root_value) -> bool { - if (root_key == "schools") { - return expect_dictionary_reserve_length(technology_schools, [this, &modifier_manager](std::string_view school_key, ast::NodeCPtr school_value) -> bool { - ModifierValue modifiers; - bool ret = modifier_manager.expect_modifier_value(move_variable_callback(modifiers))(school_value); - ret &= add_technology_school(school_key, std::move(modifiers)); - return ret; - })(root_value); - lock_technology_schools(); - } else if (root_key == "folders") return true; //ignore - else return false; - })(root); -} - -bool TechnologyManager::load_technologies_file(ModifierManager const& modifier_manager, UnitManager const& unit_manager, BuildingManager const& building_manager, ast::NodeCPtr root) { - return expect_dictionary_reserve_length(technologies, [this, &modifier_manager, &unit_manager, &building_manager](std::string_view tech_key, ast::NodeCPtr tech_value) -> bool { - ModifierValue modifiers; - TechnologyArea const* area = nullptr; - Technology::year_t year = 0; - fixed_point_t cost = 0; - bool unciv_military = false; - uint8_t unit = 0; - Technology::unit_set_t activated_units; - Technology::building_set_t activated_buildings; - - bool ret = modifier_manager.expect_modifier_value_and_keys(move_variable_callback(modifiers), - "area", ONE_EXACTLY, expect_technology_area_identifier(assign_variable_callback_pointer(area)), - "year", ONE_EXACTLY, expect_uint(assign_variable_callback(year)), - "cost", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(cost)), - "unciv_military", ZERO_OR_ONE, expect_bool(assign_variable_callback(unciv_military)), - "unit", ZERO_OR_ONE, expect_uint(assign_variable_callback(unit)), - "activate_unit", ZERO_OR_MORE, unit_manager.expect_unit_identifier([&activated_units](Unit const& unit) -> bool { - activated_units.insert(&unit); - return true; - }), - "activate_building", ZERO_OR_MORE, building_manager.expect_building_type_identifier( - [&activated_buildings](BuildingType const& building_type) -> bool { - activated_buildings.insert(&building_type); - return true; - } - ), - "ai_chance", ONE_EXACTLY, success_callback //TODO - )(tech_value); - - ret &= add_technology( - tech_key, area, year, cost, unciv_military, unit, std::move(activated_units), std::move(activated_buildings), - std::move(modifiers) - ); - return ret; - })(root); -} - -bool TechnologyManager::generate_modifiers(ModifierManager& modifier_manager) { - bool ret = true; - - ret &= modifier_manager.add_modifier_effect("unciv_military_modifier", true); - ret &= modifier_manager.add_modifier_effect("unciv_economic_modifier", true); - ret &= modifier_manager.add_modifier_effect("self_unciv_economic_modifier", true); - ret &= modifier_manager.add_modifier_effect("self_unciv_military_modifier", true); - - for (TechnologyFolder const& folder : get_technology_folders()) { - std::string folder_name = std::string(folder.get_identifier()) + "_research_bonus"; - ret &= modifier_manager.add_modifier_effect(folder_name, true); - } - return ret; -} \ No newline at end of file diff --git a/src/openvic-simulation/tech/Technology.hpp b/src/openvic-simulation/tech/Technology.hpp deleted file mode 100644 index 6f50f01..0000000 --- a/src/openvic-simulation/tech/Technology.hpp +++ /dev/null @@ -1,99 +0,0 @@ -#pragma once - -#include "openvic-simulation/misc/Modifier.hpp" -#include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/military/Unit.hpp" -#include "openvic-simulation/economy/BuildingType.hpp" -#include - -namespace OpenVic { - struct TechnologyFolder : HasIdentifier { - friend struct TechnologyManager; - - private: - TechnologyFolder(std::string_view new_identifier); - - public: - TechnologyFolder(TechnologyFolder&&) = default; - }; - - struct TechnologyArea : HasIdentifier { - friend struct TechnologyManager; - - private: - TechnologyFolder const& PROPERTY(folder); - - TechnologyArea(std::string_view new_identifier, TechnologyFolder const& new_folder); - - public: - TechnologyArea(TechnologyArea&&) = default; - }; - - struct Technology : Modifier { - friend struct TechnologyManager; - - using year_t = Date::year_t; - using unit_set_t = std::unordered_set; - using building_set_t = std::unordered_set; - - private: - TechnologyArea const& PROPERTY(area); - const year_t PROPERTY(year); - const fixed_point_t PROPERTY(cost); - const bool PROPERTY(unciv_military); - const uint8_t PROPERTY(unit); - const unit_set_t PROPERTY(activated_buildings); - const building_set_t PROPERTY(activated_units); - - //TODO: implement rules/modifiers and ai_chance - - Technology( - std::string_view new_identifier, TechnologyArea const& new_area, year_t new_year, fixed_point_t new_cost, - bool new_unciv_military, uint8_t new_unit, unit_set_t&& new_activated_units, - building_set_t&& new_activated_buildings, ModifierValue&& new_values - ); - - public: - Technology(Technology&&) = default; - }; - - struct TechnologySchool : Modifier { - friend struct TechnologyManager; - - private: - TechnologySchool(std::string_view new_identifier, ModifierValue&& new_values); - }; - - struct TechnologyManager { - IdentifierRegistry technology_folders; - IdentifierRegistry technology_areas; - IdentifierRegistry technologies; - IdentifierRegistry technology_schools; - - public: - TechnologyManager(); - - bool add_technology_folder(std::string_view identifier); - IDENTIFIER_REGISTRY_ACCESSORS(technology_folder) - - bool add_technology_area(std::string_view identifier, TechnologyFolder const* folder); - IDENTIFIER_REGISTRY_ACCESSORS(technology_area) - - bool add_technology( - std::string_view identifier, TechnologyArea const* area, Technology::year_t year, fixed_point_t cost, - bool unciv_military, uint8_t unit, Technology::unit_set_t&& activated_units, - Technology::building_set_t&& activated_buildings, ModifierValue&& values); - IDENTIFIER_REGISTRY_ACCESSORS_CUSTOM_PLURAL(technology, technologies) - - bool add_technology_school(std::string_view identifier, ModifierValue&& values); - IDENTIFIER_REGISTRY_ACCESSORS(technology_school); - - bool load_technology_file_areas(ast::NodeCPtr root); // common/technology.txt - bool load_technology_file_schools(ModifierManager const& modifier_manager, ast::NodeCPtr root); // also common/technology.txt - bool load_technologies_file( - ModifierManager const& modifier_manager, UnitManager const& unit_manager, BuildingManager const& building_manager, - ast::NodeCPtr root - ); // technologies/*.txt - bool generate_modifiers(ModifierManager& modifier_manager); - }; -} \ No newline at end of file -- cgit v1.2.3-56-ga3b1