From 51583b66feb1d13b285bb2e9c05364659c854130 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Tue, 3 Oct 2023 15:51:45 +0200 Subject: feat: implemented basic technology and school loading (modifier processing is still missing) --- src/openvic-simulation/tech/Technology.cpp | 121 +++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 src/openvic-simulation/tech/Technology.cpp (limited to 'src/openvic-simulation/tech/Technology.cpp') diff --git a/src/openvic-simulation/tech/Technology.cpp b/src/openvic-simulation/tech/Technology.cpp new file mode 100644 index 0000000..71d026e --- /dev/null +++ b/src/openvic-simulation/tech/Technology.cpp @@ -0,0 +1,121 @@ +#include "Technology.hpp" + +using namespace OpenVic; +using namespace OpenVic::NodeTools; + +TechnologyFolder::TechnologyFolder(std::string_view identifier) : HasIdentifier { identifier } {} + +TechnologyArea::TechnologyArea(std::string_view identifier, TechnologyFolder const& folder) : HasIdentifier { identifier }, folder { folder } {} + +Technology::Technology(std::string_view identifier, TechnologyArea const& area, year_t year, fixed_point_t cost, bool unciv_military, ModifierValue&& values) + : Modifier { identifier, std::move(values), 0 }, area { area }, year { year }, cost { cost }, unciv_military { unciv_military } {} + +TechnologySchool::TechnologySchool(std::string_view new_identifier, ModifierValue&& new_values) + : Modifier { new_identifier, std::move(new_values), 0 } {} //TODO: school icon + +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, 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, 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(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 == "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 expect_dictionary([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 return false; + })(root); +} + +bool TechnologyManager::load_technologies_file(ModifierManager const& modifier_manager, ast::NodeCPtr root) { + size_t expected_technologies = 0; + bool ret = expect_dictionary_reserve_length(technologies, [this, &expected_technologies](std::string_view key, ast::NodeCPtr value) -> bool { + return expect_length(add_variable_callback(expected_technologies))(value); + })(root); + technologies.reserve(technologies.size() + expected_technologies); + + ret &= expect_dictionary([this, &modifier_manager](std::string_view tech_key, ast::NodeCPtr tech_value) -> bool { + ModifierValue modifiers; + std::string_view area_identifier; + Technology::year_t year; + fixed_point_t cost; + bool unciv_military = false; + + bool ret = modifier_manager.expect_modifier_value_and_keys(move_variable_callback(modifiers), + "area", ONE_EXACTLY, expect_identifier(assign_variable_callback(area_identifier)), + "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)), + "ai_chance", ONE_EXACTLY, expect_dictionary([this](std::string_view school_key, ast::NodeCPtr school_value) -> bool { return true; }) + )(tech_value); + + TechnologyArea const* area = get_technology_area_by_identifier(area_identifier); + + ret &= add_technology(tech_key, area, year, cost, unciv_military, std::move(modifiers)); + return ret; + })(root); + + return ret; +} \ No newline at end of file -- cgit v1.2.3-56-ga3b1 From c935e471782764dcc10a3175741a02ab3ffffa8e Mon Sep 17 00:00:00 2001 From: zaaarf Date: Thu, 30 Nov 2023 21:20:37 +0100 Subject: feat: implemented modifier identifier flattening, added/fixed remaining tech modifiers effects --- src/openvic-simulation/GameManager.hpp | 7 -- src/openvic-simulation/dataloader/Dataloader.cpp | 100 +++++++++++++++-------- src/openvic-simulation/dataloader/Dataloader.hpp | 9 +- src/openvic-simulation/economy/Good.cpp | 18 ++++ src/openvic-simulation/economy/Good.hpp | 2 + src/openvic-simulation/military/Unit.cpp | 61 ++++++++++++-- src/openvic-simulation/military/Unit.hpp | 2 + src/openvic-simulation/misc/Modifier.cpp | 72 ++++++++++------ src/openvic-simulation/misc/Modifier.hpp | 7 +- src/openvic-simulation/tech/Technology.cpp | 63 +++++++++----- src/openvic-simulation/tech/Technology.hpp | 24 ++++-- 11 files changed, 262 insertions(+), 103 deletions(-) (limited to 'src/openvic-simulation/tech/Technology.cpp') diff --git a/src/openvic-simulation/GameManager.hpp b/src/openvic-simulation/GameManager.hpp index 5cac9c5..1755900 100644 --- a/src/openvic-simulation/GameManager.hpp +++ b/src/openvic-simulation/GameManager.hpp @@ -12,13 +12,6 @@ #include "openvic-simulation/politics/PoliticsManager.hpp" #include "openvic-simulation/pop/Pop.hpp" #include "openvic-simulation/tech/Technology.hpp" -#include "utility/Getters.hpp" - -#define DECLARE_WITH_REF_GETTERS(Type, name) \ - Type name; \ -public: \ - REF_GETTERS(name) \ -private: namespace OpenVic { struct GameManager { diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp index 68e5b92..7715104 100644 --- a/src/openvic-simulation/dataloader/Dataloader.cpp +++ b/src/openvic-simulation/dataloader/Dataloader.cpp @@ -1,14 +1,5 @@ #include "Dataloader.hpp" -#include -#include -#include -#include -#include -#include -#include -#include - #include #include #include @@ -624,26 +615,79 @@ bool Dataloader::_load_pop_types( return ret; } -bool Dataloader::_load_units(UnitManager& unit_manager, GoodManager const& good_manager) const { +bool Dataloader::_load_units(GameManager& game_manager) const { static constexpr std::string_view units_directory = "units"; - const bool ret = apply_to_files( + + UnitManager& unit_manager = game_manager.get_military_manager().get_unit_manager(); + bool ret = apply_to_files( lookup_files_in_dir(units_directory, ".txt"), - [&unit_manager, &good_manager](fs::path const& file) -> bool { - return unit_manager.load_unit_file(good_manager, parse_defines(file).get_file_node()); + [&game_manager, &unit_manager](fs::path const& file) -> bool { + return unit_manager.load_unit_file(game_manager.get_economy_manager().get_good_manager(), parse_defines(file).get_file_node()); } ); + unit_manager.lock_units(); + + if(!unit_manager.generate_modifiers(game_manager.get_modifier_manager())) { + Logger::error("Failed to generate unit-based modifiers!"); + ret &= false; + } + + return ret; +} + +bool Dataloader::_load_goods(GameManager& game_manager, std::string_view goods_file) const { + GoodManager& good_manager = game_manager.get_economy_manager().get_good_manager(); + bool ret = good_manager.load_goods_file(parse_defines(lookup_file(goods_file)).get_file_node()); + + if(!good_manager.generate_modifiers(game_manager.get_modifier_manager())) { + Logger::error("Failed to generate good-based modifiers!"); + ret &= false; + } + return ret; } -bool Dataloader::_load_technologies(TechnologyManager& technology_manager, ModifierManager& modifier_manager) const { +bool Dataloader::_load_technologies(GameManager& game_manager, std::string_view technology_file) const { + TechnologyManager& technology_manager = game_manager.get_technology_manager(); + + bool ret = true; + + const v2script::Parser technology_file_parser = parse_defines(lookup_file(technology_file)); + + if(!technology_manager.load_technology_file_areas(technology_file_parser.get_file_node())) { + Logger::error("Failed to load technology areas and folders!"); + ret = false; + } + + ModifierManager& modifier_manager = game_manager.get_modifier_manager(); + + if(!technology_manager.generate_modifiers(modifier_manager)) { + Logger::error("Failed to generate technollogy-based modifiers!"); + ret = false; + } + + if(!technology_manager.load_technology_file_schools(modifier_manager, technology_file_parser.get_file_node())) { + Logger::error("Failed to load technology schools!"); + ret = false; + } + static constexpr std::string_view technologies_directory = "technologies"; - bool ret = apply_to_files( + if(!apply_to_files( lookup_files_in_dir(technologies_directory, ".txt"), - [&technology_manager, &modifier_manager](fs::path const& file) -> bool { - return technology_manager.load_technologies_file(modifier_manager, parse_defines(file).get_file_node()); + [&game_manager, &technology_manager, &modifier_manager](fs::path const& file) -> bool { + return technology_manager.load_technologies_file( + modifier_manager, + game_manager.get_military_manager().get_unit_manager(), + game_manager.get_economy_manager().get_building_manager(), + parse_defines(file).get_file_node() + ); } - ); + )) { + Logger::error("Failed to load technologies!"); + ret = false; + } + technology_manager.lock_technologies(); return ret; } @@ -906,27 +950,14 @@ bool Dataloader::load_defines(GameManager& game_manager) const { Logger::error("Failed to load defines!"); ret = false; } - if (!game_manager.get_economy_manager().get_good_manager().load_goods_file( - parse_defines(lookup_file(goods_file)).get_file_node() - )) { + if (!_load_goods(game_manager, goods_file)) { Logger::error("Failed to load goods!"); ret = false; } - if (!_load_units( - game_manager.get_military_manager().get_unit_manager(), game_manager.get_economy_manager().get_good_manager() - )) { + if (!_load_units(game_manager)) { Logger::error("Failed to load units!"); ret = false; } - if (!game_manager.get_technology_manager().load_technology_file(game_manager.get_modifier_manager(), - parse_defines(lookup_file(technology_file)).get_file_node())) { - Logger::error("Failed to load technology areas!"); - ret = false; - } - if (!_load_technologies(game_manager.get_technology_manager(), game_manager.get_modifier_manager())) { - Logger::error("Failed to load technologies!"); - ret = false; - } if (!_load_pop_types( game_manager.get_pop_manager(), game_manager.get_military_manager().get_unit_manager(), game_manager.get_economy_manager().get_good_manager() @@ -994,6 +1025,9 @@ bool Dataloader::load_defines(GameManager& game_manager) const { Logger::error("Failed to load buildings!"); ret = false; } + if (!_load_technologies(game_manager, technology_file)) { + ret = false; + } if (!_load_map_dir(game_manager)) { Logger::error("Failed to load map!"); ret = false; diff --git a/src/openvic-simulation/dataloader/Dataloader.hpp b/src/openvic-simulation/dataloader/Dataloader.hpp index 4e2f42e..b5ec553 100644 --- a/src/openvic-simulation/dataloader/Dataloader.hpp +++ b/src/openvic-simulation/dataloader/Dataloader.hpp @@ -1,10 +1,8 @@ #pragma once -#include -#include - #include #include +#include //keep this here or mac builds will fail #include "openvic-simulation/dataloader/NodeTools.hpp" @@ -28,8 +26,9 @@ namespace OpenVic { bool _load_interface_files(UIManager& ui_manager) const; bool _load_pop_types(PopManager& pop_manager, UnitManager const& unit_manager, GoodManager const& good_manager) const; - bool _load_units(UnitManager& unit_manager, GoodManager const& good_manager) const; - bool _load_technologies(TechnologyManager& technology_manager, ModifierManager& modifier_manager) const; + bool _load_units(GameManager& game_manager) const; + bool _load_goods(GameManager& game_manager, std::string_view goods_file) const; + bool _load_technologies(GameManager& game_manager, std::string_view technology_file) 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 b068644..739374b 100644 --- a/src/openvic-simulation/economy/Good.cpp +++ b/src/openvic-simulation/economy/Good.cpp @@ -95,3 +95,21 @@ bool GoodManager::load_goods_file(ast::NodeCPtr root) { lock_goods(); return ret; } + +#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); \ + } + + +bool GoodManager::generate_modifiers(ModifierManager& modifier_manager) { + bool ret = true; + GOOD_MODIFIER("factory_goods_output"); + GOOD_MODIFIER("rgo_goods_output"); + GOOD_MODIFIER("rgo_size"); + return ret; +} \ No newline at end of file diff --git a/src/openvic-simulation/economy/Good.hpp b/src/openvic-simulation/economy/Good.hpp index faf4b66..8a239d5 100644 --- a/src/openvic-simulation/economy/Good.hpp +++ b/src/openvic-simulation/economy/Good.hpp @@ -1,5 +1,6 @@ #pragma once +#include "openvic-simulation/misc/Modifier.hpp" #include "openvic-simulation/types/IdentifierRegistry.hpp" namespace OpenVic { @@ -75,5 +76,6 @@ namespace OpenVic { void reset_to_defaults(); bool load_goods_file(ast::NodeCPtr root); + bool generate_modifiers(ModifierManager& modifier_manager); }; } diff --git a/src/openvic-simulation/military/Unit.cpp b/src/openvic-simulation/military/Unit.cpp index fce4082..1c4efcf 100644 --- a/src/openvic-simulation/military/Unit.cpp +++ b/src/openvic-simulation/military/Unit.cpp @@ -132,7 +132,7 @@ bool UnitManager::load_unit_file(GoodManager const& good_manager, ast::NodeCPtr bool primary_culture = false; std::string_view sprite_override {}, sprite_mount {}, sprite_mount_attach_node {}; fixed_point_t reconnaissance = 0, attack = 0, defence = 0, discipline = 0, support = 0, maneuver = 0, siege = 0; - + ret &= add_key_map_entries(key_map, "primary_culture", ZERO_OR_ONE, expect_bool(assign_variable_callback(primary_culture)), "sprite_override", ZERO_OR_ONE, expect_identifier(assign_variable_callback(sprite_override)), @@ -146,9 +146,9 @@ bool UnitManager::load_unit_file(GoodManager const& good_manager, ast::NodeCPtr "maneuver", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(maneuver)), "siege", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(siege)) ); - + ret &= expect_dictionary_key_map(key_map)(value); - + ret &= add_land_unit(key, UNIT_ARGS, LAND_ARGS); return ret; @@ -180,12 +180,61 @@ bool UnitManager::load_unit_file(GoodManager const& good_manager, ast::NodeCPtr ); ret &= expect_dictionary_key_map(key_map)(value); - + ret &= add_naval_unit(key, UNIT_ARGS, NAVY_ARGS); - + return ret; } - default: Logger::error("Unknown unit type for ", key, ": ", static_cast(type)); return false; + default: Logger::error("Unknown unit type for ", key, ": ", static_cast(type)); return false; } })(root); } + +#define STAT_MODIFIER(name, positive_good, format) \ + ret &= modifier_manager.add_modifier_effect(base_name + 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); + STAT_MODIFIER("maximum_speed", true, RAW_DECIMAL); + STAT_MODIFIER("build_time", false, INT); + STAT_MODIFIER("supply_consumption", false, PROPORTION_DECIMAL); + + switch (type) { + case Unit::type_t::LAND: { + STAT_MODIFIER("reconnaissance", true, RAW_DECIMAL); + STAT_MODIFIER("attack", true, RAW_DECIMAL); + STAT_MODIFIER("defence", true, RAW_DECIMAL); + STAT_MODIFIER("discipline", true, PROPORTION_DECIMAL); + STAT_MODIFIER("support", true, PROPORTION_DECIMAL); + STAT_MODIFIER("maneuver", true, INT); + STAT_MODIFIER("siege", true, RAW_DECIMAL); + break; + } + case Unit::type_t::NAVAL: { + STAT_MODIFIER("colonial_points", true, INT); + STAT_MODIFIER("supply_consumption_score", false, INT); + STAT_MODIFIER("hull", true, RAW_DECIMAL); + STAT_MODIFIER("gun_power", true, RAW_DECIMAL); + STAT_MODIFIER("fire_range", true, RAW_DECIMAL); + STAT_MODIFIER("evasion", true, PROPORTION_DECIMAL); + STAT_MODIFIER("torpedo_attack", true, RAW_DECIMAL); + break; + } + } + + return ret; + }; + + bool ret = true; + ret &= generate_stat_modifiers("army_base", Unit::type_t::LAND); + ret &= generate_stat_modifiers("navy_base", Unit::type_t::NAVAL); + for (Unit const& unit : this->get_units()) + ret &= generate_stat_modifiers(unit.get_identifier(), unit.get_type()); + + return ret; +} \ No newline at end of file diff --git a/src/openvic-simulation/military/Unit.hpp b/src/openvic-simulation/military/Unit.hpp index 65d0936..17228ce 100644 --- a/src/openvic-simulation/military/Unit.hpp +++ b/src/openvic-simulation/military/Unit.hpp @@ -3,6 +3,7 @@ #include #include +#include "openvic-simulation/misc/Modifier.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/economy/Good.hpp" #include "openvic-simulation/types/Date.hpp" @@ -124,5 +125,6 @@ namespace OpenVic { static NodeTools::callback_t expect_type_str(NodeTools::Callback auto callback); bool load_unit_file(GoodManager const& good_manager, ast::NodeCPtr root); + bool generate_modifiers(ModifierManager& modifier_manager); }; } diff --git a/src/openvic-simulation/misc/Modifier.cpp b/src/openvic-simulation/misc/Modifier.cpp index f46718a..86c659b 100644 --- a/src/openvic-simulation/misc/Modifier.cpp +++ b/src/openvic-simulation/misc/Modifier.cpp @@ -132,7 +132,7 @@ bool ModifierManager::setup_modifier_effects() { ret &= add_modifier_effect("influence_modifier", true); ret &= add_modifier_effect("issue_change_speed", true); ret &= add_modifier_effect("land_organisation", true); - ret &= add_modifier_effect("land_unit_start_experience", true, PERCENTAGE_DECIMAL); + ret &= add_modifier_effect("land_unit_start_experience", true, RAW_DECIMAL); ret &= add_modifier_effect("leadership_modifier", true); ret &= add_modifier_effect("loan_interest", false); ret &= add_modifier_effect("max_loan_modifier", true); @@ -153,7 +153,7 @@ bool ModifierManager::setup_modifier_effects() { ret &= add_modifier_effect("mobilisation_impact", false); ret &= add_modifier_effect("mobilisation_size", true); ret &= add_modifier_effect("naval_organisation", true); - ret &= add_modifier_effect("naval_unit_start_experience", true, PERCENTAGE_DECIMAL); + ret &= add_modifier_effect("naval_unit_start_experience", true, RAW_DECIMAL); ret &= add_modifier_effect("non_accepted_pop_consciousness_modifier", false, RAW_DECIMAL); ret &= add_modifier_effect("non_accepted_pop_militancy_modifier", false, RAW_DECIMAL); ret &= add_modifier_effect("org_regain", true); @@ -180,16 +180,23 @@ bool ModifierManager::setup_modifier_effects() { ret &= add_modifier_effect("social_reform_desire", false); ret &= add_modifier_effect("supply_consumption", false); ret &= add_modifier_effect("tax_efficiency", true); - ret &= add_modifier_effect("unit_start_experience", true, PERCENTAGE_DECIMAL); + ret &= add_modifier_effect("unit_start_experience", true, RAW_DECIMAL); ret &= add_modifier_effect("war_exhaustion", false); - - // TODO: make technology group modifiers dynamic - ret &= add_modifier_effect("army_tech_research_bonus", true); - ret &= add_modifier_effect("navy_tech_research_bonus", true); - ret &= add_modifier_effect("commerce_tech_research_bonus", true); - ret &= add_modifier_effect("culture_tech_research_bonus", true); - ret &= add_modifier_effect("industry_tech_research_bonus", true); - ret &= add_modifier_effect("navy_tech_research_bonus", true); + ret &= add_modifier_effect("reinforce_rate", true); + ret &= add_modifier_effect("colonial_migration", true); + ret &= add_modifier_effect("supply_range", true); + ret &= add_modifier_effect("colonial_points", true, INT); + ret &= add_modifier_effect("diplomatic_points", true); + ret &= add_modifier_effect("cb_creation_speed", true); //seemingly works the same way as cb_generation_speed_modifier + ret &= add_modifier_effect("education_efficiency", true); + ret &= add_modifier_effect("increase_research", true); + ret &= add_modifier_effect("influence", true); + ret &= add_modifier_effect("administrative_efficiency", true); + ret &= add_modifier_effect("tax_eff", true); + ret &= add_modifier_effect("military_tactics", true); + 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); /* Province Modifier Effects */ ret &= add_modifier_effect("assimilation_rate", true); @@ -236,6 +243,10 @@ bool ModifierManager::setup_modifier_effects() { return ret; } +void ModifierManager::register_complex_modifier(std::string_view identifier) { + complex_modifiers.emplace(identifier); +} + bool ModifierManager::load_crime_modifiers(ast::NodeCPtr root) { // TODO - DEV TASK: read crime modifiers return true; @@ -272,23 +283,38 @@ 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 { + if (effect_validator(*effect)) { + if (!modifier.values.contains(effect)) { + return expect_fixed_point(assign_variable_callback(modifier.values[effect]))(value); + } else { + Logger::error("Duplicate modifier effect: ", effect->get_identifier()); + return false; + } + } else { + Logger::error("Failed to validate modifier effect: ", effect->get_identifier()); + return false; + } + }; - return [this, &modifier, default_callback, effect_validator](std::string_view key, ast::NodeCPtr value) -> bool { + return [this, &modifier, default_callback, effect_validator, add_modifier_cb](std::string_view key, ast::NodeCPtr value) -> bool { ModifierEffect const* effect = get_modifier_effect_by_identifier(key); - if (effect != nullptr) { - if (effect_validator(*effect)) { - if (!modifier.values.contains(effect)) { - return expect_fixed_point(assign_variable_callback(modifier.values[effect]))(value); + 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("Duplicate modifier effect: ", key); + Logger::error("Could not find flattened modifier: ", flat_identifier); return false; } - } else { - Logger::error("Failed to validate modifier effect: ", key); - return false; - } - } - return default_callback(key, value); + })(value); + } else return default_callback(key, value); }; } diff --git a/src/openvic-simulation/misc/Modifier.hpp b/src/openvic-simulation/misc/Modifier.hpp index 74f0336..9f14f41 100644 --- a/src/openvic-simulation/misc/Modifier.hpp +++ b/src/openvic-simulation/misc/Modifier.hpp @@ -1,6 +1,7 @@ #pragma once #include "openvic-simulation/types/IdentifierRegistry.hpp" +#include namespace OpenVic { struct ModifierManager; @@ -10,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&&); @@ -99,6 +100,7 @@ namespace OpenVic { private: IdentifierInstanceRegistry modifier_effects; IdentifierRegistry event_modifiers; + string_set_t complex_modifiers; /* effect_validator takes in ModifierEffect const& */ NodeTools::key_value_callback_t _modifier_effect_callback( @@ -110,13 +112,14 @@ namespace OpenVic { ModifierManager(); bool add_modifier_effect( - std::string_view identifier, bool province_good, + std::string_view identifier, bool positive_good, ModifierEffect::format_t format = ModifierEffect::format_t::PROPORTION_DECIMAL ); IDENTIFIER_REGISTRY_ACCESSORS(modifier_effect) bool add_event_modifier(std::string_view identifier, ModifierValue&& values, Modifier::icon_t icon); IDENTIFIER_REGISTRY_ACCESSORS(event_modifier) + void register_complex_modifier(std::string_view identifier); bool setup_modifier_effects(); diff --git a/src/openvic-simulation/tech/Technology.cpp b/src/openvic-simulation/tech/Technology.cpp index 71d026e..aa96fe2 100644 --- a/src/openvic-simulation/tech/Technology.cpp +++ b/src/openvic-simulation/tech/Technology.cpp @@ -7,8 +7,8 @@ TechnologyFolder::TechnologyFolder(std::string_view identifier) : HasIdentifier TechnologyArea::TechnologyArea(std::string_view identifier, TechnologyFolder const& folder) : HasIdentifier { identifier }, folder { folder } {} -Technology::Technology(std::string_view identifier, TechnologyArea const& area, year_t year, fixed_point_t cost, bool unciv_military, ModifierValue&& values) - : Modifier { identifier, std::move(values), 0 }, area { area }, year { year }, cost { cost }, unciv_military { unciv_military } {} +Technology::Technology(std::string_view identifier, TechnologyArea const& area, year_t year, fixed_point_t cost, bool unciv_military, uint8_t unit, unit_set_t activated_units, building_set_t activated_buildings, ModifierValue&& values) + : Modifier { identifier, std::move(values), 0 }, area { area }, year { year }, cost { cost }, unciv_military { unciv_military }, unit { unit }, activated_buildings { std::move(activated_units) }, activated_units { std::move(activated_buildings) } {} TechnologySchool::TechnologySchool(std::string_view new_identifier, ModifierValue&& new_values) : Modifier { new_identifier, std::move(new_values), 0 } {} //TODO: school icon @@ -39,7 +39,7 @@ bool TechnologyManager::add_technology_area(std::string_view identifier, Technol 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, ModifierValue&& values) { +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; @@ -50,7 +50,7 @@ bool TechnologyManager::add_technology(std::string_view identifier, TechnologyAr return false; } - return technologies.add_item({ identifier, *area, year, cost, unciv_military, std::move(values) }); + return technologies.add_item({ identifier, *area, year, cost, unciv_military, unit, activated_units, activated_buildings, std::move(values) }); } bool TechnologyManager::add_technology_school(std::string_view identifier, ModifierValue&& values) { @@ -62,8 +62,8 @@ bool TechnologyManager::add_technology_school(std::string_view identifier, Modif return technology_schools.add_item({ identifier, std::move(values) }); } -bool TechnologyManager::load_technology_file(ModifierManager const& modifier_manager, ast::NodeCPtr root) { - return expect_dictionary([this, &modifier_manager](std::string_view root_key, ast::NodeCPtr root_value) -> bool { +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); @@ -77,45 +77,70 @@ bool TechnologyManager::load_technology_file(ModifierManager const& modifier_man })(root_value); lock_technology_folders(); lock_technology_areas(); - } else if (root_key == "schools") { - return expect_dictionary([this, &modifier_manager](std::string_view school_key, ast::NodeCPtr school_value) -> bool { + } 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 return false; + } else if (root_key == "folders") return true; //ignore + else return false; })(root); } -bool TechnologyManager::load_technologies_file(ModifierManager const& modifier_manager, ast::NodeCPtr root) { - size_t expected_technologies = 0; - bool ret = expect_dictionary_reserve_length(technologies, [this, &expected_technologies](std::string_view key, ast::NodeCPtr value) -> bool { - return expect_length(add_variable_callback(expected_technologies))(value); - })(root); - technologies.reserve(technologies.size() + expected_technologies); - - ret &= expect_dictionary([this, &modifier_manager](std::string_view tech_key, ast::NodeCPtr tech_value) -> bool { +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; std::string_view area_identifier; Technology::year_t year; fixed_point_t cost; 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_identifier(assign_variable_callback(area_identifier)), "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)), - "ai_chance", ONE_EXACTLY, expect_dictionary([this](std::string_view school_key, ast::NodeCPtr school_value) -> bool { return true; }) + "unit", ZERO_OR_ONE, expect_uint(assign_variable_callback(unit)), + "activate_unit", ZERO_OR_MORE, expect_identifier([this, &unit_manager, &activated_units](std::string_view identifier) -> bool { + activated_units.insert(unit_manager.get_unit_by_identifier(identifier)); + return true; + }), + "activate_building", ZERO_OR_MORE, expect_identifier([this, &building_manager, &activated_buildings](std::string_view identifier) -> bool { + activated_buildings.insert(building_manager.get_building_type_by_identifier(identifier)); + return true; + }), + "ai_chance", ONE_EXACTLY, expect_dictionary([this](std::string_view identifier, ast::NodeCPtr value) -> bool { return true; }) //TODO )(tech_value); TechnologyArea const* area = get_technology_area_by_identifier(area_identifier); - ret &= add_technology(tech_key, area, year, cost, unciv_military, std::move(modifiers)); + ret &= add_technology(tech_key, area, year, cost, unciv_military, unit, activated_units, 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, ModifierEffect::format_t::PROPORTION_DECIMAL); + ret &= modifier_manager.add_modifier_effect("unciv_economic_modifier", true, ModifierEffect::format_t::PROPORTION_DECIMAL); + + 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, ModifierEffect::format_t::PROPORTION_DECIMAL); + } return ret; } \ No newline at end of file diff --git a/src/openvic-simulation/tech/Technology.hpp b/src/openvic-simulation/tech/Technology.hpp index e6ce2fa..0aca1f6 100644 --- a/src/openvic-simulation/tech/Technology.hpp +++ b/src/openvic-simulation/tech/Technology.hpp @@ -1,9 +1,10 @@ #pragma once -#include "misc/Modifier.hpp" -#include "types/Date.hpp" -#include "types/IdentifierRegistry.hpp" -#include "types/fixed_point/FixedPoint.hpp" +#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 { @@ -31,16 +32,21 @@ namespace OpenVic { 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 identifier, TechnologyArea const& area, year_t year, fixed_point_t cost, bool unciv_military, ModifierValue&& values); + Technology(std::string_view identifier, TechnologyArea const& area, year_t year, fixed_point_t cost, bool unciv_military, uint8_t unit, unit_set_t activated_units, building_set_t activated_buildings, ModifierValue&& values); public: Technology(Technology&&) = default; @@ -68,13 +74,15 @@ namespace OpenVic { 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, ModifierValue&& values); + 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); //, Modifier::icon_t icon); IDENTIFIER_REGISTRY_ACCESSORS(technology_school); - bool load_technology_file(ModifierManager const& modifier_manager, ast::NodeCPtr root); // common/technology.txt - bool load_technologies_file(ModifierManager const& modifier_manager, ast::NodeCPtr root); // technologies/*.txt + 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