diff options
author | Hop311 <Hop3114@gmail.com> | 2023-12-02 01:41:15 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-02 01:41:15 +0100 |
commit | cd6875d5e0ca5e2545fd0e1647678cd18a6c81c2 (patch) | |
tree | 49016457c09ef3583b200a8bd016dc702b6e298e /src/openvic-simulation | |
parent | 0de3d5849cfb9faad0e3c8ce10a8152a916bba21 (diff) | |
parent | c935e471782764dcc10a3175741a02ab3ffffa8e (diff) |
Merge pull request #51 from OpenVicProject/dataloading-tech
Dataloading tech
Diffstat (limited to 'src/openvic-simulation')
-rw-r--r-- | src/openvic-simulation/GameManager.hpp | 3 | ||||
-rw-r--r-- | src/openvic-simulation/dataloader/Dataloader.cpp | 94 | ||||
-rw-r--r-- | src/openvic-simulation/dataloader/Dataloader.hpp | 10 | ||||
-rw-r--r-- | src/openvic-simulation/economy/Good.cpp | 18 | ||||
-rw-r--r-- | src/openvic-simulation/economy/Good.hpp | 2 | ||||
-rw-r--r-- | src/openvic-simulation/military/Unit.cpp | 61 | ||||
-rw-r--r-- | src/openvic-simulation/military/Unit.hpp | 2 | ||||
-rw-r--r-- | src/openvic-simulation/misc/Modifier.cpp | 71 | ||||
-rw-r--r-- | src/openvic-simulation/misc/Modifier.hpp | 8 | ||||
-rw-r--r-- | src/openvic-simulation/tech/Technology.cpp | 146 | ||||
-rw-r--r-- | src/openvic-simulation/tech/Technology.hpp | 88 |
11 files changed, 450 insertions, 53 deletions
diff --git a/src/openvic-simulation/GameManager.hpp b/src/openvic-simulation/GameManager.hpp index 5b66c8b..1755900 100644 --- a/src/openvic-simulation/GameManager.hpp +++ b/src/openvic-simulation/GameManager.hpp @@ -10,6 +10,8 @@ #include "openvic-simulation/military/MilitaryManager.hpp" #include "openvic-simulation/misc/Define.hpp" #include "openvic-simulation/politics/PoliticsManager.hpp" +#include "openvic-simulation/pop/Pop.hpp" +#include "openvic-simulation/tech/Technology.hpp" namespace OpenVic { struct GameManager { @@ -23,6 +25,7 @@ namespace OpenVic { ModifierManager PROPERTY_REF(modifier_manager); PoliticsManager PROPERTY_REF(politics_manager); HistoryManager PROPERTY_REF(history_manager); + TechnologyManager PROPERTY_REF(technology_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 7ffd68a..7715104 100644 --- a/src/openvic-simulation/dataloader/Dataloader.cpp +++ b/src/openvic-simulation/dataloader/Dataloader.cpp @@ -1,14 +1,5 @@ #include "Dataloader.hpp" -#include <array> -#include <cstdlib> -#include <filesystem> -#include <optional> -#include <string_view> -#include <system_error> -#include <type_traits> -#include <variant> - #include <openvic-dataloader/csv/Parser.hpp> #include <openvic-dataloader/detail/CallbackOStream.hpp> #include <openvic-dataloader/v2script/Parser.hpp> @@ -624,15 +615,80 @@ 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(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"; + if(!apply_to_files( + lookup_files_in_dir(technologies_directory, ".txt"), + [&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; } @@ -847,6 +903,7 @@ bool Dataloader::load_defines(GameManager& game_manager) const { static const std::string national_values_file = "common/nationalvalues.txt"; static const std::string production_types_file = "common/production_types.txt"; static const std::string religion_file = "common/religion.txt"; + static const std::string technology_file = "common/technology.txt"; static const std::string leader_traits_file = "common/traits.txt"; static const std::string cb_types_file = "common/cb_types.txt"; static const std::string crime_modifiers_file = "common/crime.txt"; @@ -893,15 +950,11 @@ 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; } @@ -972,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 5039582..b5ec553 100644 --- a/src/openvic-simulation/dataloader/Dataloader.hpp +++ b/src/openvic-simulation/dataloader/Dataloader.hpp @@ -1,10 +1,8 @@ #pragma once -#include <filesystem> -#include <unordered_map> - #include <openvic-dataloader/csv/Parser.hpp> #include <openvic-dataloader/v2script/Parser.hpp> +#include <unordered_map> //keep this here or mac builds will fail #include "openvic-simulation/dataloader/NodeTools.hpp" @@ -15,6 +13,8 @@ namespace OpenVic { class UIManager; struct PopManager; struct UnitManager; + struct ModifierManager; + struct TechnologyManager; struct GoodManager; class Dataloader { @@ -26,7 +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_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<int>(type)); return false; + default: Logger::error("Unknown unit type for ", key, ": ", static_cast<int>(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<bool(std::string_view, Unit::type_t)> 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 <cstdint> #include <string_view> +#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<std::string_view> expect_type_str(NodeTools::Callback<Unit::type_t> 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 89a411c..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,15 +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("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); @@ -235,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; @@ -271,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<bool(ModifierEffect const*, ast::NodeCPtr)> 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<ast::IdentifierNode>()) { + return add_modifier_cb(effect, value); + } else if (complex_modifiers.contains(key) && value->is_derived_from<ast::AbstractListNode>()) { + 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 c1c532a..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 <unordered_set> 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<ModifierEffect> std::make_unique<ModifierEffect>(std::string_view&&, bool&&, format_t&&); @@ -72,6 +73,7 @@ namespace OpenVic { /* A modifier can have no icon (zero). */ const icon_t PROPERTY(icon); + protected: Modifier(std::string_view new_identifier, ModifierValue&& new_values, icon_t new_icon); public: @@ -98,6 +100,7 @@ namespace OpenVic { private: IdentifierInstanceRegistry<ModifierEffect> modifier_effects; IdentifierRegistry<Modifier> event_modifiers; + string_set_t complex_modifiers; /* effect_validator takes in ModifierEffect const& */ NodeTools::key_value_callback_t _modifier_effect_callback( @@ -109,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 new file mode 100644 index 0000000..aa96fe2 --- /dev/null +++ b/src/openvic-simulation/tech/Technology.cpp @@ -0,0 +1,146 @@ +#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, 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 + +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, activated_units, 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; + 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)), + "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, 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 new file mode 100644 index 0000000..0aca1f6 --- /dev/null +++ b/src/openvic-simulation/tech/Technology.hpp @@ -0,0 +1,88 @@ +#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 <cstdint> + +namespace OpenVic { + struct TechnologyFolder : HasIdentifier { + friend struct TechnologyManager; + + private: + TechnologyFolder(std::string_view identifier); + + public: + TechnologyFolder(TechnologyFolder&&) = default; + }; + + struct TechnologyArea : HasIdentifier { + friend struct TechnologyManager; + + private: + TechnologyFolder const& PROPERTY(folder); + + TechnologyArea(std::string_view identifier, TechnologyFolder const& folder); + + public: + TechnologyArea(TechnologyArea&&) = default; + }; + + struct Technology : Modifier { + friend struct TechnologyManager; + using year_t = Date::year_t; + using unit_set_t = std::unordered_set<Unit const*>; + using building_set_t = std::unordered_set<BuildingType const*>; + + 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, uint8_t unit, unit_set_t activated_units, building_set_t activated_buildings, ModifierValue&& values); + + public: + Technology(Technology&&) = default; + }; + + struct TechnologySchool : Modifier { + friend struct TechnologyManager; + + private: + TechnologySchool(std::string_view new_identifier, ModifierValue&& new_values); + }; + + struct TechnologyManager { + IdentifierRegistry<TechnologyFolder> technology_folders; + IdentifierRegistry<TechnologyArea> technology_areas; + IdentifierRegistry<Technology> technologies; + IdentifierRegistry<TechnologySchool> 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); //, Modifier::icon_t icon); + 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 |