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/tech/Technology.cpp | 63 +++++++++++++++++++++--------- src/openvic-simulation/tech/Technology.hpp | 24 ++++++++---- 2 files changed, 60 insertions(+), 27 deletions(-) (limited to 'src/openvic-simulation/tech') 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