diff options
65 files changed, 3648 insertions, 1295 deletions
diff --git a/src/headless/main.cpp b/src/headless/main.cpp index 85c0ce2..a50b4ba 100644 --- a/src/headless/main.cpp +++ b/src/headless/main.cpp @@ -1,14 +1,10 @@ -#include <cstring> -#include <string> -#include <vector> - -#include "economy/GoodDefinition.hpp" #include <openvic-simulation/country/CountryInstance.hpp> +#include <openvic-simulation/dataloader/Dataloader.hpp> +#include <openvic-simulation/economy/GoodDefinition.hpp> #include <openvic-simulation/economy/production/ProductionType.hpp> #include <openvic-simulation/economy/production/ResourceGatheringOperation.hpp> -#include <openvic-simulation/pop/Pop.hpp> -#include <openvic-simulation/dataloader/Dataloader.hpp> #include <openvic-simulation/GameManager.hpp> +#include <openvic-simulation/pop/Pop.hpp> #include <openvic-simulation/testing/Testing.hpp> #include <openvic-simulation/utility/Logger.hpp> @@ -26,38 +22,36 @@ static void print_help(std::ostream& stream, char const* program_name) { } static void print_rgo(ProvinceInstance const& province) { - ResourceGatheringOperation const* const rgo = province.get_rgo(); - if(rgo == nullptr) { - Logger::info("\n ", province.get_identifier(), " - rgo: nullptr"); - } - else { - ProductionType const* const production_type = rgo->get_production_type(); - if (production_type == nullptr) { - Logger::error( - "\n ", province.get_identifier(), - " - production_type: nullptr" - ); - } - else { - GoodDefinition const* const output_good = production_type->get_output_good(); - if(output_good == nullptr) { - Logger::error( - "\n ", province.get_identifier(), - " - good: nullptr", - ", production_type: ", production_type->get_identifier() - ); - } - else { - Logger::info( - "\n ", province.get_identifier(), - " - good: ", output_good->get_identifier(), - ", production_type: ", production_type->get_identifier(), - ", size_multiplier: ", rgo->get_size_multiplier().to_string(3), - ", output_quantity_yesterday: ", rgo->get_output_quantity_yesterday().to_string(3), - ", revenue_yesterday: ", rgo->get_revenue_yesterday().to_string(3) - ); + ResourceGatheringOperation const& rgo = province.get_rgo(); + ProductionType const* const production_type_nullable = rgo.get_production_type_nullable(); + if (production_type_nullable == nullptr) { + Logger::error( + "\n ", province.get_identifier(), + " - production_type: nullptr" + ); + } else { + ProductionType const& production_type = *production_type_nullable; + GoodDefinition const& output_good = production_type.get_output_good(); + std::string text = StringUtils::append_string_views( + "\n\t", province.get_identifier(), + " - good: ", output_good.get_identifier(), + ", production_type: ", production_type.get_identifier(), + ", size_multiplier: ", rgo.get_size_multiplier().to_string(3), + ", output_quantity_yesterday: ", rgo.get_output_quantity_yesterday().to_string(3), + ", revenue_yesterday: ", rgo.get_revenue_yesterday().to_string(3), + ", total owner income: ", rgo.get_total_owner_income_cache().to_string(3), + ", total employee income: ", rgo.get_total_employee_income_cache().to_string(3), + "\n\temployees:" + ); + + auto const& employee_count_per_type_cache=rgo.get_employee_count_per_type_cache(); + for (PopType const& pop_type : *employee_count_per_type_cache.get_keys()) { + const Pop::pop_size_t employees_of_type = employee_count_per_type_cache[pop_type]; + if (employees_of_type > 0) { + text += StringUtils::append_string_views("\n\t\t", std::to_string(employees_of_type), " ", pop_type.get_identifier()); } } + Logger::info("", text); } } @@ -121,19 +115,13 @@ static bool run_headless(Dataloader::path_vector_t const& roots, bool run_tests) print_ranking_list("All countries", country_instance_manager.get_total_ranking()); Logger::info("===== RGO test... ====="); - for (int i = 0; i < 3; ++i) { - CountryInstance const* const great_power = great_powers[i]; - if(great_power == nullptr) { - Logger::warning("Great power ",std::to_string(i), " is null."); - } - else { - ProvinceInstance const* const capital_province = great_power->get_capital(); - if(capital_province == nullptr) { - Logger::warning(great_power->get_identifier(), " has no capital ProvinceInstance set."); - } - else { - print_rgo(*capital_province); - } + for (size_t i = 0; i < std::min<size_t>(3, great_powers.size()); ++i) { + CountryInstance const& great_power = *great_powers[i]; + ProvinceInstance const* const capital_province = great_power.get_capital(); + if (capital_province == nullptr) { + Logger::warning(great_power.get_identifier(), " has no capital ProvinceInstance set."); + } else { + print_rgo(*capital_province); } } } else { diff --git a/src/openvic-simulation/DefinitionManager.hpp b/src/openvic-simulation/DefinitionManager.hpp index 887cc1b..b5df507 100644 --- a/src/openvic-simulation/DefinitionManager.hpp +++ b/src/openvic-simulation/DefinitionManager.hpp @@ -14,7 +14,7 @@ #include "openvic-simulation/misc/Event.hpp" #include "openvic-simulation/misc/SongChance.hpp" #include "openvic-simulation/misc/SoundEffect.hpp" -#include "openvic-simulation/modifier/Modifier.hpp" +#include "openvic-simulation/modifier/ModifierManager.hpp" #include "openvic-simulation/politics/PoliticsManager.hpp" #include "openvic-simulation/pop/Pop.hpp" #include "openvic-simulation/research/ResearchManager.hpp" diff --git a/src/openvic-simulation/InstanceManager.cpp b/src/openvic-simulation/InstanceManager.cpp index d0a4a18..670cbfc 100644 --- a/src/openvic-simulation/InstanceManager.cpp +++ b/src/openvic-simulation/InstanceManager.cpp @@ -12,7 +12,7 @@ InstanceManager::InstanceManager( map_instance { new_definition_manager.get_map_definition() }, simulation_clock { std::bind(&InstanceManager::tick, this), std::bind(&InstanceManager::update_gamestate, this), - clock_state_changed_callback ? std::move(clock_state_changed_callback) : []() {} + clock_state_changed_callback ? std::move(clock_state_changed_callback) : []() {} }, game_instance_setup { false }, game_session_started { false }, @@ -38,11 +38,12 @@ void InstanceManager::update_gamestate() { currently_updating_gamestate = true; Logger::info("Update: ", today); - + update_modifier_sums(); // Update gamestate... map_instance.update_gamestate(today, definition_manager.get_define_manager()); country_instance_manager.update_gamestate( - today, definition_manager.get_define_manager(), definition_manager.get_military_manager().get_unit_type_manager() + today, definition_manager.get_define_manager(), definition_manager.get_military_manager().get_unit_type_manager(), + definition_manager.get_modifier_manager().get_modifier_effect_cache() ); gamestate_updated(); @@ -126,8 +127,7 @@ bool InstanceManager::load_bookmark(Bookmark const* new_bookmark) { definition_manager.get_history_manager().get_province_manager(), today, country_instance_manager, // TODO - the following argument is for generating test pop attributes - definition_manager.get_politics_manager().get_issue_manager(), - definition_manager.get_economy_manager().get_production_type_manager() + definition_manager.get_politics_manager().get_issue_manager() ); ret &= country_instance_manager.apply_history_to_countries( @@ -138,6 +138,11 @@ bool InstanceManager::load_bookmark(Bookmark const* new_bookmark) { map_instance, definition_manager.get_pop_manager().get_pop_types() ); + if (ret) { + update_modifier_sums(); + map_instance.initialise_for_new_game(definition_manager.get_modifier_manager().get_modifier_effect_cache()); + } + return ret; } @@ -179,3 +184,30 @@ bool InstanceManager::expand_selected_province_building(size_t building_index) { } return province->expand_building(building_index); } + +void InstanceManager::update_modifier_sums() { + if constexpr (ProvinceInstance::ADD_OWNER_CONTRIBUTION) { + // Calculate local province modifier sums first, then national country modifier sums, then loop over owned provinces + // adding their contributions to the owner country's modifier sum and loop over them again to add the country's total + // (including province contributions) to the provinces' modifier sum. This results in every country and province + // having a full copy of all the modifiers affecting them in their modifier sum. + map_instance.update_modifier_sums( + today, definition_manager.get_modifier_manager().get_static_modifier_cache() + ); + country_instance_manager.update_modifier_sums( + today, definition_manager.get_modifier_manager().get_static_modifier_cache() + ); + } else { + // Calculate national country modifier sums first, then local province modifier sums, adding province contributions + // to owner countries' modifier sums if each province has an owner. This results in every country having a full copy + // of all the modifiers affecting them in their modifier sum, but provinces only having their directly/locally applied + // modifiers in their modifier sum, hence requiring both province and owner country modifier effect values to be looked + // up and added together to get the full effect on the province. + country_instance_manager.update_modifier_sums( + today, definition_manager.get_modifier_manager().get_static_modifier_cache() + ); + map_instance.update_modifier_sums( + today, definition_manager.get_modifier_manager().get_static_modifier_cache() + ); + } +}
\ No newline at end of file diff --git a/src/openvic-simulation/InstanceManager.hpp b/src/openvic-simulation/InstanceManager.hpp index cfb5447..05ae412 100644 --- a/src/openvic-simulation/InstanceManager.hpp +++ b/src/openvic-simulation/InstanceManager.hpp @@ -33,6 +33,7 @@ namespace OpenVic { bool PROPERTY_CUSTOM_PREFIX(game_instance_setup, is); bool PROPERTY_CUSTOM_PREFIX(game_session_started, is); + void update_modifier_sums(); public: inline constexpr bool is_bookmark_loaded() const { return bookmark != nullptr; diff --git a/src/openvic-simulation/country/CountryInstance.cpp b/src/openvic-simulation/country/CountryInstance.cpp index 183b0c8..043494d 100644 --- a/src/openvic-simulation/country/CountryInstance.cpp +++ b/src/openvic-simulation/country/CountryInstance.cpp @@ -5,6 +5,8 @@ #include "openvic-simulation/map/Crime.hpp" #include "openvic-simulation/map/MapInstance.hpp" #include "openvic-simulation/misc/Define.hpp" +#include "openvic-simulation/modifier/ModifierEffectCache.hpp" +#include "openvic-simulation/modifier/StaticModifierCache.hpp" #include "openvic-simulation/politics/Ideology.hpp" #include "openvic-simulation/research/Invention.hpp" #include "openvic-simulation/research/Technology.hpp" @@ -17,16 +19,16 @@ static constexpr colour_t ERROR_COLOUR = colour_t::from_integer(0xFF0000); CountryInstance::CountryInstance( CountryDefinition const* new_country_definition, - decltype(unlocked_building_types)::keys_t const& building_type_keys, - decltype(unlocked_technologies)::keys_t const& technology_keys, - decltype(unlocked_inventions)::keys_t const& invention_keys, + decltype(building_type_unlock_levels)::keys_t const& building_type_keys, + decltype(technology_unlock_levels)::keys_t const& technology_keys, + decltype(invention_unlock_levels)::keys_t const& invention_keys, decltype(upper_house)::keys_t const& ideology_keys, decltype(reforms)::keys_t const& reform_keys, decltype(government_flag_overrides)::keys_t const& government_type_keys, - decltype(unlocked_crimes)::keys_t const& crime_keys, + decltype(crime_unlock_levels)::keys_t const& crime_keys, decltype(pop_type_distribution)::keys_t const& pop_type_keys, - decltype(unlocked_regiment_types)::keys_t const& unlocked_regiment_types_keys, - decltype(unlocked_ship_types)::keys_t const& unlocked_ship_types_keys + decltype(regiment_type_unlock_levels)::keys_t const& regiment_type_unlock_levels_keys, + decltype(ship_type_unlock_levels)::keys_t const& ship_type_unlock_levels_keys ) : /* Main attributes */ country_definition { new_country_definition }, colour { ERROR_COLOUR }, @@ -41,6 +43,8 @@ CountryInstance::CountryInstance( controlled_provinces {}, core_provinces {}, states {}, + modifier_sum {}, + event_modifiers {}, /* Production */ industrial_power { 0 }, @@ -48,14 +52,14 @@ CountryInstance::CountryInstance( industrial_power_from_investments {}, industrial_rank { 0 }, foreign_investments {}, - unlocked_building_types { &building_type_keys }, + building_type_unlock_levels { &building_type_keys }, /* Budget */ cash_stockpile { 0 }, /* Technology */ - unlocked_technologies { &technology_keys }, - unlocked_inventions { &invention_keys }, + technology_unlock_levels { &technology_keys }, + invention_unlock_levels { &invention_keys }, current_research { nullptr }, invested_research_points { 0 }, expected_completion_date {}, @@ -79,7 +83,7 @@ CountryInstance::CountryInstance( infamy { 0 }, plurality { 0 }, revanchism { 0 }, - unlocked_crimes { &crime_keys }, + crime_unlock_levels { &crime_keys }, /* Population */ primary_culture { nullptr }, @@ -121,32 +125,32 @@ CountryInstance::CountryInstance( war_exhaustion { 0 }, mobilised { false }, disarmed { false }, - unlocked_regiment_types { &unlocked_regiment_types_keys }, + regiment_type_unlock_levels { ®iment_type_unlock_levels_keys }, allowed_regiment_cultures { RegimentType::allowed_cultures_t::NO_CULTURES }, - unlocked_ship_types { &unlocked_ship_types_keys }, + ship_type_unlock_levels { &ship_type_unlock_levels_keys }, gas_attack_unlock_level { 0 }, gas_defence_unlock_level { 0 }, unit_variant_unlock_levels {} { - for (BuildingType const& building_type : *unlocked_building_types.get_keys()) { + for (BuildingType const& building_type : *building_type_unlock_levels.get_keys()) { if (building_type.is_default_enabled()) { unlock_building_type(building_type); } } - for (Crime const& crime : *unlocked_crimes.get_keys()) { + for (Crime const& crime : *crime_unlock_levels.get_keys()) { if (crime.is_default_active()) { unlock_crime(crime); } } - for (RegimentType const& regiment_type : *unlocked_regiment_types.get_keys()) { + for (RegimentType const& regiment_type : *regiment_type_unlock_levels.get_keys()) { if (regiment_type.is_active()) { unlock_unit_type(regiment_type); } } - for (ShipType const& ship_type : *unlocked_ship_types.get_keys()) { + for (ShipType const& ship_type : *ship_type_unlock_levels.get_keys()) { if (ship_type.is_active()) { unlock_unit_type(ship_type); } @@ -335,7 +339,7 @@ template bool CountryInstance::remove_leader(LeaderBranched<UnitType::branch_t:: template<UnitType::branch_t Branch> bool CountryInstance::modify_unit_type_unlock(UnitTypeBranched<Branch> const& unit_type, unlock_level_t unlock_level_change) { - IndexedMap<UnitTypeBranched<Branch>, unlock_level_t>& unlocked_unit_types = get_unlocked_unit_types<Branch>(); + IndexedMap<UnitTypeBranched<Branch>, unlock_level_t>& unlocked_unit_types = get_unit_type_unlock_levels<Branch>(); typename IndexedMap<UnitTypeBranched<Branch>, unlock_level_t>::value_ref_t unlock_level = unlocked_unit_types[unit_type]; @@ -384,9 +388,9 @@ bool CountryInstance::is_unit_type_unlocked(UnitType const& unit_type) const { switch (unit_type.get_branch()) { case LAND: - return unlocked_regiment_types[static_cast<UnitTypeBranched<LAND> const&>(unit_type)] > 0; + return regiment_type_unlock_levels[static_cast<UnitTypeBranched<LAND> const&>(unit_type)] > 0; case NAVAL: - return unlocked_ship_types[static_cast<UnitTypeBranched<NAVAL> const&>(unit_type)] > 0; + return ship_type_unlock_levels[static_cast<UnitTypeBranched<NAVAL> const&>(unit_type)] > 0; default: Logger::error( "Attempted to check if unit type \"", unit_type.get_identifier(), "\" with invalid branch ", @@ -397,7 +401,7 @@ bool CountryInstance::is_unit_type_unlocked(UnitType const& unit_type) const { } bool CountryInstance::modify_building_type_unlock(BuildingType const& building_type, unlock_level_t unlock_level_change) { - decltype(unlocked_building_types)::value_ref_t unlock_level = unlocked_building_types[building_type]; + decltype(building_type_unlock_levels)::value_ref_t unlock_level = building_type_unlock_levels[building_type]; // This catches subtracting below 0 or adding above the int types maximum value if (unlock_level + unlock_level_change < 0) { @@ -420,11 +424,11 @@ bool CountryInstance::unlock_building_type(BuildingType const& building_type) { } bool CountryInstance::is_building_type_unlocked(BuildingType const& building_type) const { - return unlocked_building_types[building_type] > 0; + return building_type_unlock_levels[building_type] > 0; } bool CountryInstance::modify_crime_unlock(Crime const& crime, unlock_level_t unlock_level_change) { - decltype(unlocked_crimes)::value_ref_t unlock_level = unlocked_crimes[crime]; + decltype(crime_unlock_levels)::value_ref_t unlock_level = crime_unlock_levels[crime]; // This catches subtracting below 0 or adding above the int types maximum value if (unlock_level + unlock_level_change < 0) { @@ -447,7 +451,7 @@ bool CountryInstance::unlock_crime(Crime const& crime) { } bool CountryInstance::is_crime_unlocked(Crime const& crime) const { - return unlocked_crimes[crime] > 0; + return crime_unlock_levels[crime] > 0; } bool CountryInstance::modify_gas_attack_unlock(unlock_level_t unlock_level_change) { @@ -543,7 +547,7 @@ CountryInstance::unit_variant_t CountryInstance::get_max_unlocked_unit_variant() } bool CountryInstance::modify_technology_unlock(Technology const& technology, unlock_level_t unlock_level_change) { - decltype(unlocked_technologies)::value_ref_t unlock_level = unlocked_technologies[technology]; + decltype(technology_unlock_levels)::value_ref_t unlock_level = technology_unlock_levels[technology]; // This catches subtracting below 0 or adding above the int types maximum value if (unlock_level + unlock_level_change < 0) { @@ -576,7 +580,7 @@ bool CountryInstance::modify_technology_unlock(Technology const& technology, unl } bool CountryInstance::set_technology_unlock_level(Technology const& technology, unlock_level_t unlock_level) { - const unlock_level_t unlock_level_change = unlock_level - unlocked_technologies[technology]; + const unlock_level_t unlock_level_change = unlock_level - technology_unlock_levels[technology]; return unlock_level_change != 0 ? modify_technology_unlock(technology, unlock_level_change) : true; } @@ -585,11 +589,11 @@ bool CountryInstance::unlock_technology(Technology const& technology) { } bool CountryInstance::is_technology_unlocked(Technology const& technology) const { - return unlocked_technologies[technology] > 0; + return technology_unlock_levels[technology] > 0; } bool CountryInstance::modify_invention_unlock(Invention const& invention, unlock_level_t unlock_level_change) { - decltype(unlocked_inventions)::value_ref_t unlock_level = unlocked_inventions[invention]; + decltype(invention_unlock_levels)::value_ref_t unlock_level = invention_unlock_levels[invention]; // This catches subtracting below 0 or adding above the int types maximum value if (unlock_level + unlock_level_change < 0) { @@ -628,7 +632,7 @@ bool CountryInstance::modify_invention_unlock(Invention const& invention, unlock } bool CountryInstance::set_invention_unlock_level(Invention const& invention, unlock_level_t unlock_level) { - const unlock_level_t unlock_level_change = unlock_level - unlocked_inventions[invention]; + const unlock_level_t unlock_level_change = unlock_level - invention_unlock_levels[invention]; return unlock_level_change != 0 ? modify_invention_unlock(invention, unlock_level_change) : true; } @@ -637,7 +641,7 @@ bool CountryInstance::unlock_invention(Invention const& invention) { } bool CountryInstance::is_invention_unlocked(Invention const& invention) const { - return unlocked_inventions[invention] > 0; + return invention_unlock_levels[invention] > 0; } bool CountryInstance::is_primary_culture(Culture const& culture) const { @@ -672,8 +676,12 @@ bool CountryInstance::apply_history_to_country( bool ret = true; set_optional(primary_culture, entry.get_primary_culture()); - for (Culture const* culture : entry.get_accepted_cultures()) { - ret &= add_accepted_culture(*culture); + for (auto const& [culture, add] : entry.get_accepted_cultures()) { + if (add) { + ret &= add_accepted_culture(*culture); + } else { + ret &= remove_accepted_culture(*culture); + } } set_optional(religion, entry.get_religion()); if (entry.get_ruling_party()) { @@ -816,7 +824,10 @@ void CountryInstance::_update_diplomacy() { // TODO - update diplomatic points and colonial power } -void CountryInstance::_update_military(DefineManager const& define_manager, UnitTypeManager const& unit_type_manager) { +void CountryInstance::_update_military( + DefineManager const& define_manager, UnitTypeManager const& unit_type_manager, + ModifierEffectCache const& modifier_effect_cache +) { regiment_count = 0; for (ArmyInstance const* army : armies) { @@ -847,8 +858,8 @@ void CountryInstance::_update_military(DefineManager const& define_manager, Unit max_supported_regiment_count += state->get_max_supported_regiments(); } - // TODO - apply country/tech modifiers to supply consumption - supply_consumption = 1; + supply_consumption = + fixed_point_t::_1() + get_modifier_effect_value_nullcheck(modifier_effect_cache.get_supply_consumption()); const size_t regular_army_size = std::min(4 * deployed_non_mobilised_regiments, max_supported_regiment_count); @@ -890,7 +901,7 @@ void CountryInstance::_update_military(DefineManager const& define_manager, Unit military_power = military_power_from_land + military_power_from_sea + military_power_from_leaders; // Mobilisation calculations - mobilisation_impact = 0; // TODO - apply ruling party's war policy + mobilisation_impact = get_modifier_effect_value_nullcheck(modifier_effect_cache.get_mobilization_impact()); mobilisation_max_regiment_count = ((fixed_point_t::_1() + mobilisation_impact) * fixed_point_t::parse(regiment_count)).to_int64_t(); @@ -923,7 +934,125 @@ bool CountryInstance::update_rule_set() { return rule_set.trim_and_resolve_conflicts(true); } -void CountryInstance::update_gamestate(DefineManager const& define_manager, UnitTypeManager const& unit_type_manager) { +void CountryInstance::update_modifier_sum(Date today, StaticModifierCache const& static_modifier_cache) { + // Update sum of national modifiers + modifier_sum.clear(); + + const ModifierSum::modifier_source_t country_source { this }; + + // Erase expired event modifiers and add non-expired ones to the sum + std::erase_if(event_modifiers, [this, today, &country_source](ModifierInstance const& modifier) -> bool { + if (today <= modifier.get_expiry_date()) { + modifier_sum.add_modifier(*modifier.get_modifier(), country_source); + return false; + } else { + return true; + } + }); + + // Add static modifiers + modifier_sum.add_modifier(static_modifier_cache.get_base_modifier(), country_source); + + switch (country_status) { + using enum country_status_t; + case COUNTRY_STATUS_GREAT_POWER: + modifier_sum.add_modifier(static_modifier_cache.get_great_power(), country_source); + break; + case COUNTRY_STATUS_SECONDARY_POWER: + modifier_sum.add_modifier(static_modifier_cache.get_secondary_power(), country_source); + break; + case COUNTRY_STATUS_CIVILISED: + modifier_sum.add_modifier(static_modifier_cache.get_civilised(), country_source); + break; + default: + modifier_sum.add_modifier(static_modifier_cache.get_uncivilised(), country_source); + } + if (is_disarmed()) { + modifier_sum.add_modifier(static_modifier_cache.get_disarming(), country_source); + } + modifier_sum.add_modifier(static_modifier_cache.get_war_exhaustion(), country_source, war_exhaustion); + modifier_sum.add_modifier(static_modifier_cache.get_infamy(), country_source, infamy); + modifier_sum.add_modifier(static_modifier_cache.get_literacy(), country_source, national_literacy); + modifier_sum.add_modifier(static_modifier_cache.get_plurality(), country_source, plurality); + // TODO - difficulty modifiers, war, peace, debt_default_to, bad_debter, generalised_debt_default, + // total_occupation, total_blockaded, in_bankrupcy + + // TODO - handle triggered modifiers + + if (ruling_party != nullptr) { + for (Issue const* issue : ruling_party->get_policies()) { + // The ruling party's issues here could be null as they're stored in an IndexedMap which has + // values for every IssueGroup regardless of whether or not they have a policy set. + modifier_sum.add_modifier_nullcheck(issue, country_source); + } + } + + for (Reform const* reform : reforms) { + // The country's reforms here could be null as they're stored in an IndexedMap which has + // values for every ReformGroup regardless of whether or not they have a reform set. + modifier_sum.add_modifier_nullcheck(reform, country_source); + } + + modifier_sum.add_modifier_nullcheck(national_value, country_source); + + modifier_sum.add_modifier_nullcheck(tech_school, country_source); + + for (Technology const& technology : *technology_unlock_levels.get_keys()) { + if (is_technology_unlocked(technology)) { + modifier_sum.add_modifier(technology, country_source); + } + } + + for (Invention const& invention : *invention_unlock_levels.get_keys()) { + if (is_invention_unlocked(invention)) { + modifier_sum.add_modifier(invention, country_source); + } + } + + if constexpr (ProvinceInstance::ADD_OWNER_CONTRIBUTION) { + // Add province base modifiers (with local province modifier effects removed) + for (ProvinceInstance const* province : controlled_provinces) { + contribute_province_modifier_sum(province->get_modifier_sum()); + } + + // This has to be done after adding all province modifiers to the country's sum, otherwise provinces + // earlier in the list wouldn't be affected by modifiers from provinces later in the list. + for (ProvinceInstance* province : owned_provinces) { + province->contribute_country_modifier_sum(modifier_sum); + } + } + + // TODO - calculate stats for each unit type (locked and unlocked) +} + +void CountryInstance::contribute_province_modifier_sum(ModifierSum const& province_modifier_sum) { + using enum ModifierEffect::target_t; + + modifier_sum.add_modifier_sum_exclude_targets(province_modifier_sum, PROVINCE); +} + +fixed_point_t CountryInstance::get_modifier_effect_value(ModifierEffect const& effect) const { + return modifier_sum.get_effect(effect); +} + +fixed_point_t CountryInstance::get_modifier_effect_value_nullcheck(ModifierEffect const* effect) const { + return modifier_sum.get_effect_nullcheck(effect); +} + +void CountryInstance::push_contributing_modifiers( + ModifierEffect const& effect, std::vector<ModifierSum::modifier_entry_t>& contributions +) const { + modifier_sum.push_contributing_modifiers(effect, contributions); +} + +std::vector<ModifierSum::modifier_entry_t> CountryInstance::get_contributing_modifiers(ModifierEffect const& effect) const { + return modifier_sum.get_contributing_modifiers(effect); +} + +void CountryInstance::update_gamestate( + DefineManager const& define_manager, UnitTypeManager const& unit_type_manager, + ModifierEffectCache const& modifier_effect_cache +) { // Order of updates might need to be changed/functions split up to account for dependencies _update_production(define_manager); _update_budget(); @@ -932,7 +1061,7 @@ void CountryInstance::update_gamestate(DefineManager const& define_manager, Unit _update_population(); _update_trade(); _update_diplomacy(); - _update_military(define_manager, unit_type_manager); + _update_military(define_manager, unit_type_manager, modifier_effect_cache); total_score = prestige + industrial_power + military_power; @@ -1091,16 +1220,16 @@ CountryInstance const& CountryInstanceManager::get_country_instance_from_definit bool CountryInstanceManager::generate_country_instances( CountryDefinitionManager const& country_definition_manager, - decltype(CountryInstance::unlocked_building_types)::keys_t const& building_type_keys, - decltype(CountryInstance::unlocked_technologies)::keys_t const& technology_keys, - decltype(CountryInstance::unlocked_inventions)::keys_t const& invention_keys, + decltype(CountryInstance::building_type_unlock_levels)::keys_t const& building_type_keys, + decltype(CountryInstance::technology_unlock_levels)::keys_t const& technology_keys, + decltype(CountryInstance::invention_unlock_levels)::keys_t const& invention_keys, decltype(CountryInstance::upper_house)::keys_t const& ideology_keys, decltype(CountryInstance::reforms)::keys_t const& reform_keys, decltype(CountryInstance::government_flag_overrides)::keys_t const& government_type_keys, - decltype(CountryInstance::unlocked_crimes)::keys_t const& crime_keys, + decltype(CountryInstance::crime_unlock_levels)::keys_t const& crime_keys, decltype(CountryInstance::pop_type_distribution)::keys_t const& pop_type_keys, - decltype(CountryInstance::unlocked_regiment_types)::keys_t const& unlocked_regiment_types_keys, - decltype(CountryInstance::unlocked_ship_types)::keys_t const& unlocked_ship_types_keys + decltype(CountryInstance::regiment_type_unlock_levels)::keys_t const& regiment_type_unlock_levels_keys, + decltype(CountryInstance::ship_type_unlock_levels)::keys_t const& ship_type_unlock_levels_keys ) { reserve_more(country_instances, country_definition_manager.get_country_definition_count()); @@ -1117,8 +1246,8 @@ bool CountryInstanceManager::generate_country_instances( government_type_keys, crime_keys, pop_type_keys, - unlocked_regiment_types_keys, - unlocked_ship_types_keys + regiment_type_unlock_levels_keys, + ship_type_unlock_levels_keys }); } @@ -1167,11 +1296,18 @@ bool CountryInstanceManager::apply_history_to_countries( return ret; } +void CountryInstanceManager::update_modifier_sums(Date today, StaticModifierCache const& static_modifier_cache) { + for (CountryInstance& country : country_instances.get_items()) { + country.update_modifier_sum(today, static_modifier_cache); + } +} + void CountryInstanceManager::update_gamestate( - Date today, DefineManager const& define_manager, UnitTypeManager const& unit_type_manager + Date today, DefineManager const& define_manager, UnitTypeManager const& unit_type_manager, + ModifierEffectCache const& modifier_effect_cache ) { for (CountryInstance& country : country_instances.get_items()) { - country.update_gamestate(define_manager, unit_type_manager); + country.update_gamestate(define_manager, unit_type_manager, modifier_effect_cache); } update_rankings(today, define_manager); diff --git a/src/openvic-simulation/country/CountryInstance.hpp b/src/openvic-simulation/country/CountryInstance.hpp index a7128aa..6e01649 100644 --- a/src/openvic-simulation/country/CountryInstance.hpp +++ b/src/openvic-simulation/country/CountryInstance.hpp @@ -6,6 +6,7 @@ #include "openvic-simulation/military/Leader.hpp" #include "openvic-simulation/military/UnitInstanceGroup.hpp" +#include "openvic-simulation/modifier/ModifierSum.hpp" #include "openvic-simulation/politics/Rule.hpp" #include "openvic-simulation/pop/Pop.hpp" #include "openvic-simulation/types/Date.hpp" @@ -34,6 +35,8 @@ namespace OpenVic { struct CountryHistoryEntry; struct MapInstance; struct DefineManager; + struct ModifierEffectCache; + struct StaticModifierCache; /* Representation of a country's mutable attributes, with a CountryDefinition that is unique at any single time * but can be swapped with other CountryInstance's CountryDefinition when switching tags. */ @@ -80,13 +83,17 @@ namespace OpenVic { ordered_set<ProvinceInstance*> PROPERTY(core_provinces); ordered_set<State*> PROPERTY(states); + // The total/resultant modifier affecting this country, including owned province contributions. + ModifierSum PROPERTY(modifier_sum); + std::vector<ModifierInstance> PROPERTY(event_modifiers); + /* Production */ fixed_point_t PROPERTY(industrial_power); std::vector<std::pair<State const*, fixed_point_t>> PROPERTY(industrial_power_from_states); std::vector<std::pair<CountryInstance const*, fixed_point_t>> PROPERTY(industrial_power_from_investments); size_t PROPERTY(industrial_rank); fixed_point_map_t<CountryInstance const*> PROPERTY(foreign_investments); - IndexedMap<BuildingType, unlock_level_t> PROPERTY(unlocked_building_types); + IndexedMap<BuildingType, unlock_level_t> PROPERTY(building_type_unlock_levels); // TODO - total amount of each good produced /* Budget */ @@ -94,8 +101,8 @@ namespace OpenVic { // TODO - cash stockpile change over last 30 days /* Technology */ - IndexedMap<Technology, unlock_level_t> PROPERTY(unlocked_technologies); - IndexedMap<Invention, unlock_level_t> PROPERTY(unlocked_inventions); + IndexedMap<Technology, unlock_level_t> PROPERTY(technology_unlock_levels); + IndexedMap<Invention, unlock_level_t> PROPERTY(invention_unlock_levels); Technology const* PROPERTY(current_research); fixed_point_t PROPERTY(invested_research_points); Date PROPERTY(expected_completion_date); @@ -118,10 +125,10 @@ namespace OpenVic { IndexedMap<GovernmentType, GovernmentType const*> PROPERTY(government_flag_overrides); GovernmentType const* PROPERTY(flag_government_type); fixed_point_t PROPERTY(suppression_points); - fixed_point_t PROPERTY(infamy); - fixed_point_t PROPERTY(plurality); + fixed_point_t PROPERTY(infamy); // in 0-25+ range + fixed_point_t PROPERTY(plurality); // in 0-100 range fixed_point_t PROPERTY(revanchism); - IndexedMap<Crime, unlock_level_t> PROPERTY(unlocked_crimes); + IndexedMap<Crime, unlock_level_t> PROPERTY(crime_unlock_levels); // TODO - rebel movements /* Population */ @@ -132,7 +139,7 @@ namespace OpenVic { // TODO - population change over last 30 days fixed_point_t PROPERTY(national_consciousness); fixed_point_t PROPERTY(national_militancy); - IndexedMap<PopType, fixed_point_t> PROPERTY(pop_type_distribution); + IndexedMap<PopType, Pop::pop_size_t> PROPERTY(pop_type_distribution); size_t PROPERTY(national_focus_capacity) // TODO - national foci @@ -165,32 +172,32 @@ namespace OpenVic { fixed_point_t PROPERTY(total_consumed_ship_supply); fixed_point_t PROPERTY(max_ship_supply); fixed_point_t PROPERTY(leadership_points); - fixed_point_t PROPERTY(war_exhaustion); + fixed_point_t PROPERTY(war_exhaustion); // in 0-100 range bool PROPERTY_CUSTOM_PREFIX(mobilised, is); bool PROPERTY_CUSTOM_PREFIX(disarmed, is); - IndexedMap<RegimentType, unlock_level_t> PROPERTY(unlocked_regiment_types); + IndexedMap<RegimentType, unlock_level_t> PROPERTY(regiment_type_unlock_levels); RegimentType::allowed_cultures_t PROPERTY(allowed_regiment_cultures); - IndexedMap<ShipType, unlock_level_t> PROPERTY(unlocked_ship_types); + IndexedMap<ShipType, unlock_level_t> PROPERTY(ship_type_unlock_levels); unlock_level_t PROPERTY(gas_attack_unlock_level); unlock_level_t PROPERTY(gas_defence_unlock_level); std::vector<unlock_level_t> PROPERTY(unit_variant_unlock_levels); UNIT_BRANCHED_GETTER(get_unit_instance_groups, armies, navies); UNIT_BRANCHED_GETTER(get_leaders, generals, admirals); - UNIT_BRANCHED_GETTER(get_unlocked_unit_types, unlocked_regiment_types, unlocked_ship_types); + UNIT_BRANCHED_GETTER(get_unit_type_unlock_levels, regiment_type_unlock_levels, ship_type_unlock_levels); CountryInstance( CountryDefinition const* new_country_definition, - decltype(unlocked_building_types)::keys_t const& building_type_keys, - decltype(unlocked_technologies)::keys_t const& technology_keys, - decltype(unlocked_inventions)::keys_t const& invention_keys, + decltype(building_type_unlock_levels)::keys_t const& building_type_keys, + decltype(technology_unlock_levels)::keys_t const& technology_keys, + decltype(invention_unlock_levels)::keys_t const& invention_keys, decltype(upper_house)::keys_t const& ideology_keys, decltype(reforms)::keys_t const& reform_keys, decltype(government_flag_overrides)::keys_t const& government_type_keys, - decltype(unlocked_crimes)::keys_t const& crime_keys, + decltype(crime_unlock_levels)::keys_t const& crime_keys, decltype(pop_type_distribution)::keys_t const& pop_type_keys, - decltype(unlocked_regiment_types)::keys_t const& unlocked_regiment_types_keys, - decltype(unlocked_ship_types)::keys_t const& unlocked_ship_types_keys + decltype(regiment_type_unlock_levels)::keys_t const& regiment_type_unlock_levels_keys, + decltype(ship_type_unlock_levels)::keys_t const& ship_type_unlock_levels_keys ); public: @@ -290,13 +297,28 @@ namespace OpenVic { void _update_population(); void _update_trade(); void _update_diplomacy(); - void _update_military(DefineManager const& define_manager, UnitTypeManager const& unit_type_manager); + void _update_military( + DefineManager const& define_manager, UnitTypeManager const& unit_type_manager, + ModifierEffectCache const& modifier_effect_cache + ); bool update_rule_set(); public: - void update_gamestate(DefineManager const& define_manager, UnitTypeManager const& unit_type_manager); + void update_modifier_sum(Date today, StaticModifierCache const& static_modifier_cache); + void contribute_province_modifier_sum(ModifierSum const& province_modifier_sum); + fixed_point_t get_modifier_effect_value(ModifierEffect const& effect) const; + fixed_point_t get_modifier_effect_value_nullcheck(ModifierEffect const* effect) const; + void push_contributing_modifiers( + ModifierEffect const& effect, std::vector<ModifierSum::modifier_entry_t>& contributions + ) const; + std::vector<ModifierSum::modifier_entry_t> get_contributing_modifiers(ModifierEffect const& effect) const; + + void update_gamestate( + DefineManager const& define_manager, UnitTypeManager const& unit_type_manager, + ModifierEffectCache const& modifier_effect_cache + ); void tick(); }; @@ -324,16 +346,16 @@ namespace OpenVic { bool generate_country_instances( CountryDefinitionManager const& country_definition_manager, - decltype(CountryInstance::unlocked_building_types)::keys_t const& building_type_keys, - decltype(CountryInstance::unlocked_technologies)::keys_t const& technology_keys, - decltype(CountryInstance::unlocked_inventions)::keys_t const& invention_keys, + decltype(CountryInstance::building_type_unlock_levels)::keys_t const& building_type_keys, + decltype(CountryInstance::technology_unlock_levels)::keys_t const& technology_keys, + decltype(CountryInstance::invention_unlock_levels)::keys_t const& invention_keys, decltype(CountryInstance::upper_house)::keys_t const& ideology_keys, decltype(CountryInstance::reforms)::keys_t const& reform_keys, decltype(CountryInstance::government_flag_overrides)::keys_t const& government_type_keys, - decltype(CountryInstance::unlocked_crimes)::keys_t const& crime_keys, + decltype(CountryInstance::crime_unlock_levels)::keys_t const& crime_keys, decltype(CountryInstance::pop_type_distribution)::keys_t const& pop_type_keys, - decltype(CountryInstance::unlocked_regiment_types)::keys_t const& unlocked_regiment_types_keys, - decltype(CountryInstance::unlocked_ship_types)::keys_t const& unlocked_ship_types_keys + decltype(CountryInstance::regiment_type_unlock_levels)::keys_t const& regiment_type_unlock_levels_keys, + decltype(CountryInstance::ship_type_unlock_levels)::keys_t const& ship_type_unlock_levels_keys ); bool apply_history_to_countries( @@ -341,7 +363,11 @@ namespace OpenVic { MapInstance& map_instance ); - void update_gamestate(Date today, DefineManager const& define_manager, UnitTypeManager const& unit_type_manager); + void update_modifier_sums(Date today, StaticModifierCache const& static_modifier_cache); + void update_gamestate( + Date today, DefineManager const& define_manager, UnitTypeManager const& unit_type_manager, + ModifierEffectCache const& modifier_effect_cache + ); void tick(); }; } diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp index 99a8d68..2b438c7 100644 --- a/src/openvic-simulation/dataloader/Dataloader.cpp +++ b/src/openvic-simulation/dataloader/Dataloader.cpp @@ -778,6 +778,10 @@ bool Dataloader::_load_map_dir(DefinitionManager& definition_manager) const { Logger::error("Failed to load terrain types!"); ret = false; } + if (!map_definition.get_terrain_type_manager().generate_modifiers(definition_manager.get_modifier_manager())) { + Logger::error("Failed to generate terrain-based modifiers!"); + ret = false; + } if (!map_definition.load_map_images( lookup_file(append_string_views(map_directory, provinces)), @@ -964,11 +968,12 @@ bool Dataloader::load_defines(DefinitionManager& definition_manager) { Logger::error("Failed to load rebel types!"); ret = false; } + definition_manager.get_modifier_manager().lock_all_modifier_except_base_country_effects(); if (!_load_technologies(definition_manager)) { Logger::error("Failed to load technologies!"); ret = false; } - definition_manager.get_modifier_manager().lock_modifier_effects(); + definition_manager.get_modifier_manager().lock_base_country_modifier_effects(); if (!definition_manager.get_politics_manager().get_rule_manager().setup_rules( definition_manager.get_economy_manager().get_building_type_manager() )) { @@ -1008,6 +1013,7 @@ bool Dataloader::load_defines(DefinitionManager& definition_manager) { Logger::error("Failed to load crime modifiers!"); ret = false; } + if (!definition_manager.get_modifier_manager().load_event_modifiers( parse_defines(lookup_file(event_modifiers_file)).get_file_node() )) { @@ -1020,6 +1026,8 @@ bool Dataloader::load_defines(DefinitionManager& definition_manager) { Logger::error("Failed to load static modifiers!"); ret = false; } + definition_manager.get_modifier_manager().lock_event_modifiers(); + if (!definition_manager.get_modifier_manager().load_triggered_modifiers( parse_defines_cached(lookup_file(triggered_modifiers_file)).get_file_node() )) { diff --git a/src/openvic-simulation/dataloader/Vic2PathSearch.cpp b/src/openvic-simulation/dataloader/Vic2PathSearch.cpp index a23d0ce..a1a125d 100644 --- a/src/openvic-simulation/dataloader/Vic2PathSearch.cpp +++ b/src/openvic-simulation/dataloader/Vic2PathSearch.cpp @@ -206,7 +206,7 @@ static fs::path _search_for_game_path(fs::path hint_path = {}) { } return empty_fail_result_callback("Could not parse VDF at '", current_path, "'."); } - std::optional current_node = *(parser.get_key_values()); + std::optional current_node = *parser.get_key_values(); // check "libraryfolders" list auto it = current_node.value().find("libraryfolders"); diff --git a/src/openvic-simulation/economy/BuildingType.cpp b/src/openvic-simulation/economy/BuildingType.cpp index e7c0d59..2801cfa 100644 --- a/src/openvic-simulation/economy/BuildingType.cpp +++ b/src/openvic-simulation/economy/BuildingType.cpp @@ -1,5 +1,7 @@ #include "BuildingType.hpp" +#include "openvic-simulation/modifier/ModifierManager.hpp" + using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -61,7 +63,8 @@ bool BuildingTypeManager::load_buildings_file( ) -> bool { BuildingType::building_type_args_t building_type_args {}; - bool ret = modifier_manager.expect_modifier_value_and_keys(move_variable_callback(building_type_args.modifier), + bool ret = NodeTools::expect_dictionary_keys_and_default( + modifier_manager.expect_base_province_modifier(building_type_args.modifier), "type", ONE_EXACTLY, expect_identifier(assign_variable_callback(building_type_args.type)), "on_completion", ZERO_OR_ONE, expect_identifier(assign_variable_callback(building_type_args.on_completion)), "completion_size", ZERO_OR_ONE, @@ -112,18 +115,27 @@ bool BuildingTypeManager::load_buildings_file( )(root); lock_building_types(); - for (BuildingType const& building_type : building_types.get_items()) { + IndexedMap<BuildingType, ModifierEffectCache::building_type_effects_t>& building_type_effects = + modifier_manager.modifier_effect_cache.building_type_effects; + + building_type_effects.set_keys(&get_building_types()); + + for (BuildingType const& building_type : get_building_types()) { using enum ModifierEffect::format_t; + using enum ModifierEffect::target_t; + + ModifierEffectCache::building_type_effects_t& this_building_type_effects = building_type_effects[building_type]; static constexpr std::string_view max_prefix = "max_"; static constexpr std::string_view min_prefix = "min_build_"; - ret &= modifier_manager.add_modifier_effect( - StringUtils::append_string_views(max_prefix, building_type.get_identifier()), true, INT, - StringUtils::append_string_views("$", building_type.get_identifier(), "$ $TECH_MAX_LEVEL$") + ret &= modifier_manager.register_technology_modifier_effect( + this_building_type_effects.max_level, StringUtils::append_string_views(max_prefix, building_type.get_identifier()), + true, INT, StringUtils::append_string_views("$", building_type.get_identifier(), "$ $TECH_MAX_LEVEL$") ); // TODO - add custom localisation for "min_build_$building_type$" modifiers - ret &= modifier_manager.add_modifier_effect( - StringUtils::append_string_views(min_prefix, building_type.get_identifier()), false, INT + ret &= modifier_manager.register_terrain_modifier_effect( + this_building_type_effects.min_level, StringUtils::append_string_views(min_prefix, building_type.get_identifier()), + false, INT ); if (building_type.is_in_province()) { diff --git a/src/openvic-simulation/economy/GoodDefinition.cpp b/src/openvic-simulation/economy/GoodDefinition.cpp index ed24549..ba5aff8 100644 --- a/src/openvic-simulation/economy/GoodDefinition.cpp +++ b/src/openvic-simulation/economy/GoodDefinition.cpp @@ -1,5 +1,7 @@ #include "GoodDefinition.hpp" +#include "openvic-simulation/modifier/ModifierManager.hpp" + using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -11,18 +13,18 @@ GoodDefinition::GoodDefinition( index_t new_index, GoodCategory const& new_category, fixed_point_t new_base_price, - bool new_available_from_start, - bool new_tradeable, - bool new_money, - bool new_overseas_penalty + bool new_is_available_from_start, + bool new_is_tradeable, + bool new_is_money, + bool new_counters_overseas_penalty ) : HasIdentifierAndColour { new_identifier, new_colour, false }, HasIndex { new_index }, category { new_category }, base_price { new_base_price }, - available_from_start { new_available_from_start }, - tradeable { new_tradeable }, - money { new_money }, - overseas_penalty { new_overseas_penalty } {} + is_available_from_start { new_is_available_from_start }, + is_tradeable { new_is_tradeable }, + is_money { new_is_money }, + counters_overseas_penalty { new_counters_overseas_penalty } {} bool GoodDefinitionManager::add_good_category(std::string_view identifier) { if (identifier.empty()) { @@ -34,7 +36,7 @@ bool GoodDefinitionManager::add_good_category(std::string_view identifier) { bool GoodDefinitionManager::add_good_definition( std::string_view identifier, colour_t colour, GoodCategory const& category, fixed_point_t base_price, - bool available_from_start, bool tradeable, bool money, bool overseas_penalty + bool is_available_from_start, bool is_tradeable, bool is_money, bool has_overseas_penalty ) { if (identifier.empty()) { Logger::error("Invalid good identifier - empty!"); @@ -45,8 +47,8 @@ bool GoodDefinitionManager::add_good_definition( return false; } return good_definitions.add_item({ - identifier, colour, get_good_definition_count(), category, base_price, available_from_start, - tradeable, money, overseas_penalty + identifier, colour, get_good_definition_count(), category, base_price, is_available_from_start, + is_tradeable, is_money, has_overseas_penalty }); } @@ -66,19 +68,19 @@ bool GoodDefinitionManager::load_goods_file(ast::NodeCPtr root) { return expect_dictionary([this, &good_category](std::string_view key, ast::NodeCPtr value) -> bool { colour_t colour = colour_t::null(); fixed_point_t base_price; - bool available_from_start = true, tradeable = true; - bool money = false, overseas_penalty = false; + bool is_available_from_start = true, is_tradeable = true; + bool is_money = false, has_overseas_penalty = false; bool ret = expect_dictionary_keys( "color", ONE_EXACTLY, expect_colour(assign_variable_callback(colour)), "cost", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(base_price)), - "available_from_start", ZERO_OR_ONE, expect_bool(assign_variable_callback(available_from_start)), - "tradeable", ZERO_OR_ONE, expect_bool(assign_variable_callback(tradeable)), - "money", ZERO_OR_ONE, expect_bool(assign_variable_callback(money)), - "overseas_penalty", ZERO_OR_ONE, expect_bool(assign_variable_callback(overseas_penalty)) + "available_from_start", ZERO_OR_ONE, expect_bool(assign_variable_callback(is_available_from_start)), + "tradeable", ZERO_OR_ONE, expect_bool(assign_variable_callback(is_tradeable)), + "money", ZERO_OR_ONE, expect_bool(assign_variable_callback(is_money)), + "overseas_penalty", ZERO_OR_ONE, expect_bool(assign_variable_callback(has_overseas_penalty)) )(value); ret &= add_good_definition( - key, colour, good_category, base_price, available_from_start, tradeable, money, overseas_penalty + key, colour, good_category, base_price, is_available_from_start, is_tradeable, is_money, has_overseas_penalty ); return ret; })(good_category_value); @@ -89,39 +91,82 @@ bool GoodDefinitionManager::load_goods_file(ast::NodeCPtr root) { bool GoodDefinitionManager::generate_modifiers(ModifierManager& modifier_manager) const { using enum ModifierEffect::format_t; + using enum ModifierEffect::target_t; + + IndexedMap<GoodDefinition, ModifierEffectCache::good_effects_t>& good_effects = + modifier_manager.modifier_effect_cache.good_effects; + + good_effects.set_keys(&get_good_definitions()); bool ret = true; - const auto good_modifier = [this, &modifier_manager, &ret]( - std::string_view name, bool is_positive_good, auto make_localisation_suffix - ) -> void { - ret &= modifier_manager.register_complex_modifier(name); + ret &= modifier_manager.register_complex_modifier("artisan_goods_input"); + ret &= modifier_manager.register_complex_modifier("artisan_goods_output"); + ret &= modifier_manager.register_complex_modifier("artisan_goods_throughput"); + ret &= modifier_manager.register_complex_modifier("factory_goods_input"); + ret &= modifier_manager.register_complex_modifier("factory_goods_output"); + ret &= modifier_manager.register_complex_modifier("factory_goods_throughput"); + ret &= modifier_manager.register_complex_modifier("rgo_goods_output"); + ret &= modifier_manager.register_complex_modifier("rgo_goods_throughput"); + ret &= modifier_manager.register_complex_modifier("rgo_size"); - for (GoodDefinition const& good : get_good_definitions()) { - ret &= modifier_manager.add_modifier_effect( - ModifierManager::get_flat_identifier(name, good.get_identifier()), is_positive_good, PROPORTION_DECIMAL, - make_localisation_suffix(good.get_identifier()) + for (GoodDefinition const& good : get_good_definitions()) { + const std::string_view good_identifier = good.get_identifier(); + ModifierEffectCache::good_effects_t& this_good_effects = good_effects[good]; + + const auto good_modifier = [&modifier_manager, &ret, &good_identifier]( + ModifierEffect const*& effect_cache, std::string_view name, bool is_positive_good, + std::string_view localisation_key + ) -> void { + ret &= modifier_manager.register_technology_modifier_effect( + effect_cache, ModifierManager::get_flat_identifier(name, good_identifier), is_positive_good, + PROPORTION_DECIMAL, localisation_key ); - } - }; + }; - const auto make_production_localisation_suffix = [](std::string_view localisation_suffix) -> auto { - return [localisation_suffix](std::string_view good_identifier) -> std::string { - return StringUtils::append_string_views("$", good_identifier, "$ $", localisation_suffix, "$"); + const auto make_production_localisation_suffix = [&good_identifier]( + std::string_view localisation_suffix + ) -> std::string { + return StringUtils::append_string_views("$", good_identifier, "$ $", localisation_suffix, "$"); }; - }; - - good_modifier("artisan_goods_input", false, make_production_localisation_suffix("TECH_INPUT")); - good_modifier("artisan_goods_output", true, make_production_localisation_suffix("TECH_OUTPUT")); - good_modifier("artisan_goods_throughput", true, make_production_localisation_suffix("TECH_THROUGHPUT")); - good_modifier("factory_goods_input", false, make_production_localisation_suffix("TECH_INPUT")); - good_modifier("factory_goods_output", true, make_production_localisation_suffix("TECH_OUTPUT")); - good_modifier("factory_goods_throughput", true, make_production_localisation_suffix("TECH_THROUGHPUT")); - good_modifier("rgo_goods_output", true, make_production_localisation_suffix("TECH_OUTPUT")); - good_modifier("rgo_goods_throughput", true, make_production_localisation_suffix("TECH_THROUGHPUT")); - good_modifier("rgo_size", true, [](std::string_view good_identifier) -> std::string { - return StringUtils::append_string_views(good_identifier, "_RGO_SIZE"); - }); + + good_modifier( + this_good_effects.artisan_goods_input, "artisan_goods_input", false, + make_production_localisation_suffix("TECH_INPUT") + ); + good_modifier( + this_good_effects.artisan_goods_output, "artisan_goods_output", true, + make_production_localisation_suffix("TECH_OUTPUT") + ); + good_modifier( + this_good_effects.artisan_goods_throughput, "artisan_goods_throughput", true, + make_production_localisation_suffix("TECH_THROUGHPUT") + ); + good_modifier( + this_good_effects.factory_goods_input, "factory_goods_input", false, + make_production_localisation_suffix("TECH_INPUT") + ); + good_modifier( + this_good_effects.factory_goods_output, "factory_goods_output", true, + make_production_localisation_suffix("TECH_OUTPUT") + ); + good_modifier( + this_good_effects.factory_goods_throughput, "factory_goods_throughput", true, + make_production_localisation_suffix("TECH_THROUGHPUT") + ); + good_modifier( + this_good_effects.rgo_goods_output, "rgo_goods_output", true, + make_production_localisation_suffix("TECH_OUTPUT") + ); + good_modifier( + this_good_effects.rgo_goods_throughput, "rgo_goods_throughput", true, + make_production_localisation_suffix("TECH_THROUGHPUT") + ); + good_modifier( + this_good_effects.rgo_size, "rgo_size", true, + StringUtils::append_string_views(good_identifier, "_RGO_SIZE") + ); + } return ret; } diff --git a/src/openvic-simulation/economy/GoodDefinition.hpp b/src/openvic-simulation/economy/GoodDefinition.hpp index bc231cb..7cb87cc 100644 --- a/src/openvic-simulation/economy/GoodDefinition.hpp +++ b/src/openvic-simulation/economy/GoodDefinition.hpp @@ -1,6 +1,5 @@ #pragma once -#include "openvic-simulation/modifier/Modifier.hpp" #include "openvic-simulation/types/IdentifierRegistry.hpp" namespace OpenVic { @@ -36,21 +35,23 @@ namespace OpenVic { private: GoodCategory const& PROPERTY(category); const fixed_point_t PROPERTY(base_price); - const bool PROPERTY_CUSTOM_PREFIX(available_from_start, is); - const bool PROPERTY_CUSTOM_PREFIX(tradeable, is); - const bool PROPERTY(money); - const bool PROPERTY(overseas_penalty); + const bool PROPERTY(is_available_from_start); + const bool PROPERTY(is_tradeable); + const bool PROPERTY(is_money); + const bool PROPERTY(counters_overseas_penalty); GoodDefinition( std::string_view new_identifier, colour_t new_colour, index_t new_index, GoodCategory const& new_category, - fixed_point_t new_base_price, bool new_available_from_start, bool new_tradeable, bool new_money, - bool new_overseas_penalty + fixed_point_t new_base_price, bool new_is_available_from_start, bool new_is_tradeable, bool new_is_money, + bool new_counters_overseas_penalty ); public: GoodDefinition(GoodDefinition&&) = default; }; + struct ModifierManager; + struct GoodDefinitionManager { private: IdentifierRegistry<GoodCategory> IDENTIFIER_REGISTRY_CUSTOM_PLURAL(good_category, good_categories); @@ -61,7 +62,7 @@ namespace OpenVic { bool add_good_definition( std::string_view identifier, colour_t colour, GoodCategory const& category, fixed_point_t base_price, - bool available_from_start, bool tradeable, bool money, bool overseas_penalty + bool is_available_from_start, bool is_tradeable, bool is_money, bool has_overseas_penalty ); bool load_goods_file(ast::NodeCPtr root); diff --git a/src/openvic-simulation/economy/GoodInstance.cpp b/src/openvic-simulation/economy/GoodInstance.cpp index 6fe3a2f..ac081c9 100644 --- a/src/openvic-simulation/economy/GoodInstance.cpp +++ b/src/openvic-simulation/economy/GoodInstance.cpp @@ -4,7 +4,7 @@ using namespace OpenVic; GoodInstance::GoodInstance(GoodDefinition const& new_good_definition) : HasIdentifierAndColour { new_good_definition }, good_definition { new_good_definition }, - price { new_good_definition.get_base_price() }, available { new_good_definition.is_available_from_start() } {} + price { new_good_definition.get_base_price() }, is_available { new_good_definition.get_is_available_from_start() } {} bool GoodInstanceManager::setup(GoodDefinitionManager const& good_definition_manager) { if (good_instances_are_locked()) { diff --git a/src/openvic-simulation/economy/GoodInstance.hpp b/src/openvic-simulation/economy/GoodInstance.hpp index 20370ef..74ec939 100644 --- a/src/openvic-simulation/economy/GoodInstance.hpp +++ b/src/openvic-simulation/economy/GoodInstance.hpp @@ -14,7 +14,7 @@ namespace OpenVic { private: GoodDefinition const& PROPERTY(good_definition); fixed_point_t PROPERTY(price); - bool PROPERTY(available); + bool PROPERTY(is_available); // TODO - supply, demand, actual bought GoodInstance(GoodDefinition const& new_good_definition); diff --git a/src/openvic-simulation/economy/production/Employee.cpp b/src/openvic-simulation/economy/production/Employee.cpp new file mode 100644 index 0000000..569299a --- /dev/null +++ b/src/openvic-simulation/economy/production/Employee.cpp @@ -0,0 +1,8 @@ +#include "Employee.hpp" + +using namespace OpenVic; + +Employee::Employee(Pop& new_pop, const Pop::pop_size_t new_size) + : pop { new_pop }, + size { new_size } + {}
\ No newline at end of file diff --git a/src/openvic-simulation/economy/production/Employee.hpp b/src/openvic-simulation/economy/production/Employee.hpp new file mode 100644 index 0000000..8a09c31 --- /dev/null +++ b/src/openvic-simulation/economy/production/Employee.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "openvic-simulation/pop/Pop.hpp" + +namespace OpenVic { + struct Employee { + private: + Pop::pop_size_t PROPERTY_RW(size); + public: + Pop& pop; + Employee(Pop& new_pop, const Pop::pop_size_t new_size); + }; +}
\ No newline at end of file diff --git a/src/openvic-simulation/economy/production/ProductionType.cpp b/src/openvic-simulation/economy/production/ProductionType.cpp index fd9bce7..c5db641 100644 --- a/src/openvic-simulation/economy/production/ProductionType.cpp +++ b/src/openvic-simulation/economy/production/ProductionType.cpp @@ -1,7 +1,6 @@ #include "ProductionType.hpp" #include <openvic-dataloader/v2script/Parser.hpp> -#include "dataloader/NodeTools.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -10,26 +9,26 @@ Job::Job( PopType const* new_pop_type, effect_t new_effect_type, fixed_point_t new_effect_multiplier, - fixed_point_t new_desired_workforce_share + fixed_point_t new_amount ) : pop_type { new_pop_type }, effect_type { new_effect_type }, effect_multiplier { new_effect_multiplier }, - desired_workforce_share { new_desired_workforce_share } {} + amount { new_amount } {} ProductionType::ProductionType( - std::string_view new_identifier, - std::optional<Job> new_owner, + const std::string_view new_identifier, + const std::optional<Job> new_owner, std::vector<Job>&& new_jobs, - template_type_t new_template_type, - Pop::pop_size_t new_base_workforce_size, + const template_type_t new_template_type, + const Pop::pop_size_t new_base_workforce_size, GoodDefinition::good_definition_map_t&& new_input_goods, - GoodDefinition const* new_output_good, - fixed_point_t new_base_output_quantity, + GoodDefinition const& new_output_good, + const fixed_point_t new_base_output_quantity, std::vector<bonus_t>&& new_bonuses, GoodDefinition::good_definition_map_t&& new_maintenance_requirements, - bool new_is_coastal, - bool new_is_farm, - bool new_is_mine + const bool new_is_coastal, + const bool new_is_farm, + const bool new_is_mine ) : HasIdentifier { new_identifier }, owner { new_owner }, jobs { std::move(new_jobs) }, @@ -63,7 +62,7 @@ node_callback_t ProductionTypeManager::_expect_job( using enum Job::effect_t; std::string_view pop_type {}; - Job::effect_t effect_type {THROUGHPUT}; + Job::effect_t effect_type { THROUGHPUT }; fixed_point_t effect_multiplier = 1, desired_workforce_share = 1; static const string_map_t<Job::effect_t> effect_map = { @@ -95,19 +94,19 @@ node_callback_t ProductionTypeManager::_expect_job_list( } bool ProductionTypeManager::add_production_type( - std::string_view identifier, + const std::string_view identifier, std::optional<Job> owner, std::vector<Job>&& jobs, - ProductionType::template_type_t template_type, - Pop::pop_size_t base_workforce_size, + const ProductionType::template_type_t template_type, + const Pop::pop_size_t base_workforce_size, GoodDefinition::good_definition_map_t&& input_goods, - GoodDefinition const* output_good, - fixed_point_t base_output_quantity, + GoodDefinition const* const output_good, + const fixed_point_t base_output_quantity, std::vector<ProductionType::bonus_t>&& bonuses, GoodDefinition::good_definition_map_t&& maintenance_requirements, - bool is_coastal, - bool is_farm, - bool is_mine + const bool is_coastal, + const bool is_farm, + const bool is_mine ) { if (identifier.empty()) { Logger::error("Invalid production type identifier - empty!"); @@ -170,20 +169,16 @@ bool ProductionTypeManager::add_production_type( } const bool ret = production_types.add_item({ - identifier, owner, std::move(jobs), template_type, base_workforce_size, std::move(input_goods), output_good, + identifier, owner, std::move(jobs), template_type, base_workforce_size, std::move(input_goods), *output_good, base_output_quantity, std::move(bonuses), std::move(maintenance_requirements), is_coastal, is_farm, is_mine }); - if(ret && (template_type == RGO)) { + if (ret && (template_type == RGO)) { ProductionType const& production_type = production_types.get_items().back(); - auto current_rgo_pt = good_to_rgo_production_type[*output_good]; - if(current_rgo_pt == nullptr) { - // first rgo pt - good_to_rgo_production_type[*output_good] = &production_type; - } - else if (is_farm && !current_rgo_pt->is_farm()) { - // farms are preferred (over mines) in V2 - good_to_rgo_production_type[*output_good] = &production_type; + ProductionType const*& current_rgo_pt = good_to_rgo_production_type[*output_good]; + if (current_rgo_pt == nullptr || (is_farm && !current_rgo_pt->is_farm())) { + // first rgo pt or farms are preferred (over mines) in V2 + current_rgo_pt = &production_type; } //else ignore, we already have an rgo pt } diff --git a/src/openvic-simulation/economy/production/ProductionType.hpp b/src/openvic-simulation/economy/production/ProductionType.hpp index b06c9b5..f9b1778 100644 --- a/src/openvic-simulation/economy/production/ProductionType.hpp +++ b/src/openvic-simulation/economy/production/ProductionType.hpp @@ -20,13 +20,13 @@ namespace OpenVic { PopType const* PROPERTY(pop_type); effect_t PROPERTY(effect_type); fixed_point_t PROPERTY(effect_multiplier); - fixed_point_t PROPERTY(desired_workforce_share); + fixed_point_t PROPERTY(amount); Job( PopType const* new_pop_type, effect_t new_effect_type, fixed_point_t new_effect_multiplier, - fixed_point_t new_desired_workforce_share + fixed_point_t new_amount ); public: @@ -47,7 +47,7 @@ namespace OpenVic { const Pop::pop_size_t PROPERTY(base_workforce_size); GoodDefinition::good_definition_map_t PROPERTY(input_goods); - GoodDefinition const* PROPERTY(output_good); + GoodDefinition const& PROPERTY(output_good); const fixed_point_t PROPERTY(base_output_quantity); std::vector<bonus_t> PROPERTY(bonuses); @@ -58,19 +58,19 @@ namespace OpenVic { const bool PROPERTY_CUSTOM_PREFIX(mine, is); ProductionType( - std::string_view new_identifier, - std::optional<Job> new_owner, + const std::string_view new_identifier, + const std::optional<Job> new_owner, std::vector<Job>&& new_jobs, - template_type_t new_template_type, - Pop::pop_size_t new_base_workforce_size, + const template_type_t new_template_type, + const Pop::pop_size_t new_base_workforce_size, GoodDefinition::good_definition_map_t&& new_input_goods, - GoodDefinition const* new_output_goods, - fixed_point_t new_base_output_quantity, + GoodDefinition const& new_output_good, + const fixed_point_t new_base_output_quantity, std::vector<bonus_t>&& new_bonuses, GoodDefinition::good_definition_map_t&& new_maintenance_requirements, - bool new_is_coastal, - bool new_is_farm, - bool new_is_mine + const bool new_is_coastal, + const bool new_is_farm, + const bool new_is_mine ); bool parse_scripts(DefinitionManager const& definition_manager); @@ -98,19 +98,19 @@ namespace OpenVic { ProductionTypeManager(); bool add_production_type( - std::string_view identifier, + const std::string_view identifier, std::optional<Job> owner, - std::vector<Job>&& employees, - ProductionType::template_type_t template_type, - Pop::pop_size_t workforce, + std::vector<Job>&& jobs, + const ProductionType::template_type_t template_type, + const Pop::pop_size_t base_workforce_size, GoodDefinition::good_definition_map_t&& input_goods, - GoodDefinition const* output_goods, - fixed_point_t value, + GoodDefinition const* const output_good, + const fixed_point_t base_output_quantity, std::vector<ProductionType::bonus_t>&& bonuses, GoodDefinition::good_definition_map_t&& maintenance_requirements, - bool coastal, - bool farm, - bool mine + const bool is_coastal, + const bool is_farm, + const bool is_mine ); bool load_production_types_file( diff --git a/src/openvic-simulation/economy/production/ResourceGatheringOperation.cpp b/src/openvic-simulation/economy/production/ResourceGatheringOperation.cpp index 92bf921..df91645 100644 --- a/src/openvic-simulation/economy/production/ResourceGatheringOperation.cpp +++ b/src/openvic-simulation/economy/production/ResourceGatheringOperation.cpp @@ -1,21 +1,343 @@ #include "ResourceGatheringOperation.hpp" +#include <vector> + +#include "openvic-simulation/economy/production/Employee.hpp" +#include "openvic-simulation/economy/production/ProductionType.hpp" +#include "openvic-simulation/map/ProvinceInstance.hpp" +#include "openvic-simulation/map/State.hpp" +#include "openvic-simulation/modifier/ModifierEffectCache.hpp" +#include "openvic-simulation/pop/Pop.hpp" +#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" +#include "openvic-simulation/utility/Logger.hpp" + using namespace OpenVic; ResourceGatheringOperation::ResourceGatheringOperation( - ProductionType const* new_production_type, + ProductionType const* new_production_type_nullable, fixed_point_t new_size_multiplier, fixed_point_t new_revenue_yesterday, fixed_point_t new_output_quantity_yesterday, fixed_point_t new_unsold_quantity_yesterday, - ordered_map<Pop*, Pop::pop_size_t>&& new_employees -) : production_type { new_production_type }, + std::vector<Employee>&& new_employees, + decltype(employee_count_per_type_cache)::keys_t const& pop_type_keys +) : production_type_nullable { new_production_type_nullable }, revenue_yesterday { new_revenue_yesterday }, output_quantity_yesterday { new_output_quantity_yesterday }, unsold_quantity_yesterday { new_unsold_quantity_yesterday }, size_multiplier { new_size_multiplier }, - employees { std::move(new_employees) } {} + employees { std::move(new_employees) }, + max_employee_count_cache { 0 }, + total_employees_count_cache { 0 }, + total_paid_employees_count_cache { 0 }, + total_owner_income_cache { }, + total_employee_income_cache { }, + employee_count_per_type_cache { &pop_type_keys } +{ } -ResourceGatheringOperation::ResourceGatheringOperation( - ProductionType const* new_production_type, fixed_point_t new_size_multiplier -) : ResourceGatheringOperation { new_production_type, new_size_multiplier, 0, 0, 0, {} } {} +ResourceGatheringOperation::ResourceGatheringOperation(decltype(employee_count_per_type_cache)::keys_t const& pop_type_keys) : ResourceGatheringOperation { + nullptr, fixed_point_t::_0(), + fixed_point_t::_0(), fixed_point_t::_0(), + fixed_point_t::_0(), {}, pop_type_keys +} {} + +void ResourceGatheringOperation::initialise_for_new_game(ProvinceInstance& location, ModifierEffectCache const& modifier_effect_cache) { + if (production_type_nullable == nullptr) { + output_quantity_yesterday = 0; + revenue_yesterday = 0; + return; + } + + ProductionType const& production_type = *production_type_nullable; + const fixed_point_t size_modifier = calculate_size_modifier(location, modifier_effect_cache); + const Pop::pop_size_t total_worker_count_in_province = update_size_and_return_total_worker_count(location, modifier_effect_cache, size_modifier); + hire(location, total_worker_count_in_province); + Pop::pop_size_t total_owner_count_in_state_cache = 0; + std::vector<Pop*> owner_pops_cache {}; + output_quantity_yesterday = produce(location, owner_pops_cache, total_owner_count_in_state_cache, modifier_effect_cache, size_modifier); + revenue_yesterday = output_quantity_yesterday * production_type.get_output_good().get_base_price(); //TODO sell on market + pay_employees(location, revenue_yesterday, total_worker_count_in_province, owner_pops_cache, total_owner_count_in_state_cache); +} + +Pop::pop_size_t ResourceGatheringOperation::update_size_and_return_total_worker_count( + ProvinceInstance& location, + ModifierEffectCache const& modifier_effect_cache, + const fixed_point_t size_modifier +) { + if (production_type_nullable == nullptr) { + size_multiplier = fixed_point_t::_0(); + max_employee_count_cache = fixed_point_t::_0(); + return fixed_point_t::_0(); + } + + Pop::pop_size_t total_worker_count_in_province = 0; //not counting equivalents + ProductionType const& production_type = *production_type_nullable; + std::vector<Job> const& jobs = production_type.get_jobs(); + //can't use pop_type_distribution as it is not filled correctly yet (possibly due to equivalent pop type conversion) + for (Pop const& pop : location.get_pops()){ + PopType const* pop_type = pop.get_type(); + for(Job const& job : jobs) { + if (job.get_pop_type() == pop_type) { + total_worker_count_in_province += pop.get_size(); + break; + } + } + } + + const fixed_point_t base_workforce_size = production_type.get_base_workforce_size(); + if (size_modifier == fixed_point_t::_0()) { + size_multiplier = 0; + } else { + size_multiplier = ((total_worker_count_in_province / (size_modifier * base_workforce_size)).ceil() * fixed_point_t::_1_50()).floor(); + } + max_employee_count_cache = (size_modifier * size_multiplier * base_workforce_size).floor(); + return total_worker_count_in_province; +} + +fixed_point_t ResourceGatheringOperation::calculate_size_modifier(ProvinceInstance const& location, ModifierEffectCache const& modifier_effect_cache) const { + if (production_type_nullable == nullptr) { + return fixed_point_t::_1(); + } + + ProductionType const& production_type = *production_type_nullable; + + fixed_point_t size_modifier = fixed_point_t::_1(); + if (production_type.is_farm()) { + size_modifier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_farm_rgo_size_global()) + + location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_farm_rgo_size_local()); + } + if (production_type.is_mine()) { + size_modifier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_mine_rgo_size_global()) + + location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_mine_rgo_size_local()); + } + auto const& good_effects = modifier_effect_cache.get_good_effects()[production_type.get_output_good()]; + size_modifier += location.get_modifier_effect_value_nullcheck(good_effects.get_rgo_size()); + return size_modifier > fixed_point_t::_0() ? size_modifier : fixed_point_t::_0(); +} + +void ResourceGatheringOperation::hire(ProvinceInstance& location, Pop::pop_size_t available_worker_count) { + total_employees_count_cache = 0; + total_paid_employees_count_cache=0; + if (production_type_nullable == nullptr) { + employees.clear(); + employee_count_per_type_cache.fill(fixed_point_t::_0()); + return; + } + + ProductionType const& production_type = *production_type_nullable; + if (max_employee_count_cache <= 0) { return; } + if (available_worker_count <= 0) { return; } + + fixed_point_t proportion_to_hire; + if (max_employee_count_cache >= available_worker_count) { + //hire everyone + proportion_to_hire = fixed_point_t::_1(); + } else { + //hire all pops proportionally + const fixed_point_t max_worker_count_real = max_employee_count_cache, available_worker_count_real = available_worker_count; + proportion_to_hire = max_worker_count_real / available_worker_count_real; + } + + std::vector<Job> const& jobs = production_type.get_jobs(); + for (Pop& pop : location.get_mutable_pops()){ + PopType const& pop_type = *pop.get_type(); + for(Job const& job : jobs) { + if (job.get_pop_type() == &pop_type) { + const Pop::pop_size_t pop_size_to_hire = static_cast<Pop::pop_size_t>((proportion_to_hire * pop.get_size()).floor()); + employee_count_per_type_cache[pop_type] += pop_size_to_hire; + employees.emplace_back(pop, pop_size_to_hire); + total_employees_count_cache += pop_size_to_hire; + if (!pop_type.get_is_slave()) { + total_paid_employees_count_cache += pop_size_to_hire; + } + break; + } + } + } +} + +fixed_point_t ResourceGatheringOperation::produce( + ProvinceInstance& location, + std::vector<Pop*>& owner_pops_cache, + Pop::pop_size_t& total_owner_count_in_state_cache, + ModifierEffectCache const& modifier_effect_cache, + const fixed_point_t size_modifier +) { + if (size_modifier == fixed_point_t::_0()){ + return fixed_point_t::_0(); + } + + total_owner_count_in_state_cache = 0; + owner_pops_cache = {}; + if (production_type_nullable == nullptr || max_employee_count_cache <= 0) { + return fixed_point_t::_0(); + } + + ProductionType const& production_type = *production_type_nullable; + fixed_point_t throughput_multiplier = fixed_point_t::_1(); + fixed_point_t output_multilpier = fixed_point_t::_1(); + + std::optional<Job> const& owner = production_type.get_owner(); + if (owner.has_value()) { + Job const& owner_job = owner.value(); + PopType const* owner_job_pop_type_nullable = owner_job.get_pop_type(); + if (owner_job_pop_type_nullable == nullptr) { + Logger::error("Owner job for ", production_type.get_identifier(), " has nullptr as pop_type."); + return fixed_point_t::_0(); + } + PopType const& owner_pop_type = *owner_job_pop_type_nullable; + State const* state_nullable = location.get_state(); + if (state_nullable == nullptr) { + Logger::error("Province ", location.get_identifier(), " has no state."); + return fixed_point_t::_0(); + } + State const& state = *state_nullable; + Pop::pop_size_t state_population = 0; //state.get_total_population() is not filled yet + std::vector<ProvinceInstance*> const& provinces_in_state = state.get_provinces(); + for (ProvinceInstance* const province_nullable : provinces_in_state) { + if (province_nullable == nullptr) { + Logger::error("State ", state.get_identifier(), " has nullptr in provinces."); + return fixed_point_t::_0(); + } + ProvinceInstance& province = *province_nullable; + for (Pop& pop : province.get_mutable_pops()){ + state_population += pop.get_size(); + if (&owner_pop_type == pop.get_type()) { + owner_pops_cache.push_back(&pop); + total_owner_count_in_state_cache += pop.get_size(); + } + } + } + + if (total_owner_count_in_state_cache > 0) { + switch (owner_job.get_effect_type()) { + case Job::effect_t::OUTPUT: + output_multilpier += owner_job.get_effect_multiplier() * total_owner_count_in_state_cache / state_population; + break; + case Job::effect_t::THROUGHPUT: + throughput_multiplier += owner_job.get_effect_multiplier() * total_owner_count_in_state_cache / state_population; + break; + default: + Logger::error("Invalid job effect in RGO ",production_type.get_identifier()); + break; + } + } + } + + throughput_multiplier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_rgo_throughput()) + +location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_local_rgo_throughput()); + output_multilpier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_rgo_output()) + +location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_local_rgo_output()); + + if (production_type.is_farm()) { + throughput_multiplier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_farm_rgo_throughput_global()); + output_multilpier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_farm_rgo_output_global()) + + location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_farm_rgo_output_local()); + } + if (production_type.is_mine()) { + throughput_multiplier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_mine_rgo_throughput_global()); + output_multilpier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_mine_rgo_output_global()) + + location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_mine_rgo_output_local()); + } + auto const& good_effects = modifier_effect_cache.get_good_effects()[production_type.get_output_good()]; + throughput_multiplier += location.get_modifier_effect_value_nullcheck(good_effects.get_rgo_goods_throughput()); + output_multilpier += location.get_modifier_effect_value_nullcheck(good_effects.get_rgo_goods_output()); + + fixed_point_t throughput_from_workers = fixed_point_t::_0(); + fixed_point_t output_from_workers = fixed_point_t::_1(); + for (PopType const& pop_type : *employee_count_per_type_cache.get_keys()) { + const Pop::pop_size_t employees_of_type = employee_count_per_type_cache[pop_type]; + + for(Job const& job : production_type.get_jobs()) { + if (job.get_pop_type() != &pop_type) { + continue; + } + + fixed_point_t const effect_multiplier = job.get_effect_multiplier(); + fixed_point_t relative_to_workforce = fixed_point_t::parse(employees_of_type) / fixed_point_t::parse(max_employee_count_cache); + fixed_point_t const amount = job.get_amount(); + if (effect_multiplier != fixed_point_t::_1() && relative_to_workforce > amount) { + relative_to_workforce = amount; + } + switch (job.get_effect_type()) { + case Job::effect_t::OUTPUT: + output_from_workers += effect_multiplier * relative_to_workforce; + break; + case Job::effect_t::THROUGHPUT: + throughput_from_workers += effect_multiplier * relative_to_workforce; + break; + default: + Logger::error("Invalid job effect in RGO ",production_type.get_identifier()); + break; + } + } + } + + //if province is overseas multiply by (1 + overseas penalty) + + return production_type.get_base_output_quantity() + * size_modifier * size_multiplier + * throughput_multiplier * throughput_from_workers + * output_multilpier * output_from_workers; +} + +void ResourceGatheringOperation::pay_employees( + ProvinceInstance& location, + const fixed_point_t revenue, + const Pop::pop_size_t total_worker_count_in_province, + std::vector<Pop*>& owner_pops_cache, + const Pop::pop_size_t total_owner_count_in_state_cache +) { + total_owner_income_cache = 0; + total_employee_income_cache = 0; + if (production_type_nullable == nullptr || revenue <= 0 || total_worker_count_in_province <= 0) { + if (revenue < 0) { Logger::error("Negative revenue for province ", location.get_identifier()); } + if (total_worker_count_in_province < 0) { Logger::error("Negative total worker count for province ", location.get_identifier()); } + return; + } + + ProductionType const& production_type = *production_type_nullable; + + fixed_point_t revenue_left = revenue; + if (total_owner_count_in_state_cache > 0) { + Job const& owner_job = production_type.get_owner().value(); + PopType const* owner_job_pop_type_nullable = owner_job.get_pop_type(); + + fixed_point_t owner_share = (fixed_point_t::_2() * total_owner_count_in_state_cache / total_worker_count_in_province); + constexpr fixed_point_t upper_limit = fixed_point_t::_0_50(); + if (owner_share > upper_limit) { + owner_share = upper_limit; + } + + for(Pop* owner_pop_nullable : owner_pops_cache) { + Pop& owner_pop = *owner_pop_nullable; + const fixed_point_t income_for_this_pop = revenue_left * owner_share * owner_pop.get_size() / total_owner_count_in_state_cache; + owner_pop.add_rgo_owner_income(income_for_this_pop); + total_owner_income_cache += income_for_this_pop; + } + revenue_left *= (fixed_point_t::_1() - owner_share); + } + + if (total_paid_employees_count_cache > 0) { + for (Employee& employee : employees) { + Pop& employee_pop = employee.pop; + PopType const* employee_pop_type_nullable = employee_pop.get_type(); + if (employee_pop_type_nullable == nullptr) { + Logger::error("employee has nullptr pop_type."); + return; + } + PopType const& employee_pop_type = *employee_pop_type_nullable; + if (employee_pop_type.get_is_slave()) { + continue; + } + + const Pop::pop_size_t employee_size = employee.get_size(); + const fixed_point_t income_for_this_pop = revenue_left * employee_size / total_paid_employees_count_cache; + employee_pop.add_rgo_worker_income(income_for_this_pop); + total_employee_income_cache += income_for_this_pop; + } + } else { + //scenario slaves only + //Money is removed from system in Victoria 2. + } +}
\ No newline at end of file diff --git a/src/openvic-simulation/economy/production/ResourceGatheringOperation.hpp b/src/openvic-simulation/economy/production/ResourceGatheringOperation.hpp index 2fb782e..a15e87d 100644 --- a/src/openvic-simulation/economy/production/ResourceGatheringOperation.hpp +++ b/src/openvic-simulation/economy/production/ResourceGatheringOperation.hpp @@ -1,25 +1,64 @@ #pragma once +#include "openvic-simulation/economy/production/Employee.hpp" #include "openvic-simulation/economy/production/ProductionType.hpp" +#include "openvic-simulation/modifier/ModifierEffectCache.hpp" +#include "openvic-simulation/pop/Pop.hpp" #include "openvic-simulation/types/fixed_point/FixedPoint.hpp" #include "openvic-simulation/utility/Getters.hpp" namespace OpenVic { struct ResourceGatheringOperation { private: - ProductionType const* PROPERTY_RW(production_type); + ProductionType const* PROPERTY_RW(production_type_nullable); fixed_point_t PROPERTY(revenue_yesterday); fixed_point_t PROPERTY(output_quantity_yesterday); fixed_point_t PROPERTY(unsold_quantity_yesterday); - fixed_point_t PROPERTY(size_multiplier); - ordered_map<Pop*, Pop::pop_size_t> PROPERTY(employees); + fixed_point_t PROPERTY_RW(size_multiplier); + std::vector<Employee> PROPERTY(employees); + Pop::pop_size_t PROPERTY(max_employee_count_cache); + Pop::pop_size_t PROPERTY(total_employees_count_cache); + Pop::pop_size_t PROPERTY(total_paid_employees_count_cache); + fixed_point_t PROPERTY(total_owner_income_cache); + fixed_point_t PROPERTY(total_employee_income_cache); + IndexedMap<PopType, Pop::pop_size_t> PROPERTY(employee_count_per_type_cache); + + Pop::pop_size_t update_size_and_return_total_worker_count( + ProvinceInstance& location, + ModifierEffectCache const& modifier_effect_cache, + const fixed_point_t size_modifier + ); + fixed_point_t calculate_size_modifier(ProvinceInstance const& location, ModifierEffectCache const& modifier_effect_cache) const; + void hire(ProvinceInstance& location, const Pop::pop_size_t available_worker_count); + fixed_point_t produce( + ProvinceInstance& location, + std::vector<Pop*>& owner_pops_cache, + Pop::pop_size_t& total_owner_count_in_state_cache, + ModifierEffectCache const& modifier_effect_cache, + const fixed_point_t size_modifier + ); + void pay_employees( + ProvinceInstance& location, + const fixed_point_t revenue, + const Pop::pop_size_t total_worker_count_in_province, + std::vector<Pop*>& owner_pops_cache, + const Pop::pop_size_t total_owner_count_in_state_cache + ); public: ResourceGatheringOperation( - ProductionType const* new_production_type, fixed_point_t new_size_multiplier, fixed_point_t new_revenue_yesterday, - fixed_point_t new_output_quantity_yesterday, fixed_point_t new_unsold_quantity_yesterday, - ordered_map<Pop*, Pop::pop_size_t>&& new_employees + ProductionType const* new_production_type_nullable, + fixed_point_t new_size_multiplier, + fixed_point_t new_revenue_yesterday, + fixed_point_t new_output_quantity_yesterday, + fixed_point_t new_unsold_quantity_yesterday, + std::vector<Employee>&& new_employees, + decltype(employee_count_per_type_cache)::keys_t const& pop_type_keys ); - ResourceGatheringOperation(ProductionType const* new_production_type, fixed_point_t new_size_multiplier); + ResourceGatheringOperation(decltype(employee_count_per_type_cache)::keys_t const& pop_type_keys); + constexpr bool is_valid() const { + return production_type_nullable != nullptr; + } + void initialise_for_new_game(ProvinceInstance& location, ModifierEffectCache const& modifier_effect_cache); }; } diff --git a/src/openvic-simulation/history/CountryHistory.cpp b/src/openvic-simulation/history/CountryHistory.cpp index cd51e19..c5d8977 100644 --- a/src/openvic-simulation/history/CountryHistory.cpp +++ b/src/openvic-simulation/history/CountryHistory.cpp @@ -35,6 +35,33 @@ bool CountryHistoryMap::_load_history_entry( InventionManager const& invention_manager = definition_manager.get_research_manager().get_invention_manager(); DecisionManager const& decision_manager = definition_manager.get_decision_manager(); + const auto accepted_culture_instruction = [&entry](bool add) { + return [&entry, add](Culture const& culture) -> bool { + const auto it = entry.accepted_cultures.find(&culture); + if (it == entry.accepted_cultures.end()) { + // No current culture instruction + entry.accepted_cultures.emplace(&culture, add); + return true; + } else if (it->second == add) { + // Desired culture instruction already exists + Logger::warning( + "Duplicate attempt to ", add ? "add" : "remove", " accepted culture ", culture.get_identifier(), + " ", add ? "to" : "from", " country history of ", entry.get_country() + ); + return true; + } else { + // Opposite culture instruction exists + entry.accepted_cultures.erase(it); + Logger::warning( + "Attempted to ", add ? "add" : "remove", " accepted culture ", culture.get_identifier(), + " ", add ? "to" : "from", " country history of ", entry.get_country(), + " after previously ", add ? "removing" : "adding", " it" + ); + return true; + } + }; + }; + return expect_dictionary_keys_and_default( [this, &definition_manager, &dataloader, &deployment_manager, &issue_manager, &technology_manager, &invention_manager, &country_definition_manager, &entry](std::string_view key, ast::NodeCPtr value) -> bool { @@ -84,7 +111,8 @@ bool CountryHistoryMap::_load_history_entry( ), "primary_culture", ZERO_OR_ONE, culture_manager.expect_culture_identifier(assign_variable_callback_pointer_opt(entry.primary_culture)), - "culture", ZERO_OR_MORE, culture_manager.expect_culture_identifier(set_callback_pointer(entry.accepted_cultures)), + "culture", ZERO_OR_MORE, culture_manager.expect_culture_identifier(accepted_culture_instruction(true)), + "remove_culture", ZERO_OR_MORE, culture_manager.expect_culture_identifier(accepted_culture_instruction(false)), "religion", ZERO_OR_ONE, definition_manager.get_pop_manager().get_religion_manager().expect_religion_identifier( assign_variable_callback_pointer_opt(entry.religion) ), diff --git a/src/openvic-simulation/history/CountryHistory.hpp b/src/openvic-simulation/history/CountryHistory.hpp index 04de653..7ee9d6b 100644 --- a/src/openvic-simulation/history/CountryHistory.hpp +++ b/src/openvic-simulation/history/CountryHistory.hpp @@ -33,7 +33,7 @@ namespace OpenVic { CountryDefinition const& PROPERTY(country); std::optional<Culture const*> PROPERTY(primary_culture); - ordered_set<Culture const*> PROPERTY(accepted_cultures); + ordered_map<Culture const*, bool> PROPERTY(accepted_cultures); std::optional<Religion const*> PROPERTY(religion); std::optional<CountryParty const*> PROPERTY(ruling_party); std::optional<Date> PROPERTY(last_election); diff --git a/src/openvic-simulation/history/ProvinceHistory.cpp b/src/openvic-simulation/history/ProvinceHistory.cpp index 7d3d6eb..bc92f48 100644 --- a/src/openvic-simulation/history/ProvinceHistory.cpp +++ b/src/openvic-simulation/history/ProvinceHistory.cpp @@ -3,8 +3,7 @@ #include "openvic-simulation/DefinitionManager.hpp" #include "openvic-simulation/economy/GoodDefinition.hpp" #include "openvic-simulation/map/ProvinceDefinition.hpp" -#include "dataloader/NodeTools.hpp" -#include "types/IdentifierRegistry.hpp" +#include "openvic-simulation/utility/Logger.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -53,7 +52,7 @@ bool ProvinceHistoryMap::_load_history_entry( Logger::warning( "Attempted to ", add ? "add" : "remove", " core of country ", country.get_identifier(), " ", add ? "to" : "from", " province history of ", entry.get_province(), - " after previously ", add ? "adding" : "removing", " it" + " after previously ", add ? "removing" : "adding", " it" ); return true; } @@ -106,10 +105,14 @@ bool ProvinceHistoryMap::_load_history_entry( "trade_goods", ZERO_OR_ONE, good_definition_manager.expect_good_definition_identifier_or_string( [&definition_manager, &entry](GoodDefinition const& rgo_good) ->bool { - entry.rgo_production_type = definition_manager.get_economy_manager().get_production_type_manager().get_good_to_rgo_production_type()[rgo_good]; - return entry.rgo_production_type != nullptr; - //we expect the good to have an rgo production type - //Victoria 2 treats this as null, but clearly the modder wanted there to be a good + entry.rgo_production_type_nullable = definition_manager.get_economy_manager().get_production_type_manager().get_good_to_rgo_production_type()[rgo_good]; + if (entry.rgo_production_type_nullable == nullptr) { + Logger::error(entry.province.get_identifier(), " has trade_goods ", rgo_good.get_identifier(), " which has no rgo production type defined."); + //we expect the good to have an rgo production type + //Victoria 2 treats this as null, but clearly the modder wanted there to be a good + return false; + } + return true; }, allow_empty_true, //could be explicitly setting trade_goods to null do_warn //could be typo in good identifier diff --git a/src/openvic-simulation/history/ProvinceHistory.hpp b/src/openvic-simulation/history/ProvinceHistory.hpp index db6b57f..f44fc81 100644 --- a/src/openvic-simulation/history/ProvinceHistory.hpp +++ b/src/openvic-simulation/history/ProvinceHistory.hpp @@ -32,7 +32,7 @@ namespace OpenVic { std::optional<ProvinceInstance::colony_status_t> PROPERTY(colonial); std::optional<bool> PROPERTY(slave); ordered_map<CountryDefinition const*, bool> PROPERTY(cores); - ProductionType const* PROPERTY(rgo_production_type); + std::optional<ProductionType const*> PROPERTY(rgo_production_type_nullable); std::optional<ProvinceInstance::life_rating_t> PROPERTY(life_rating); std::optional<TerrainType const*> PROPERTY(terrain_type); ordered_map<BuildingType const*, BuildingType::level_t> PROPERTY(province_buildings); diff --git a/src/openvic-simulation/map/Crime.cpp b/src/openvic-simulation/map/Crime.cpp index fb521ad..f52352c 100644 --- a/src/openvic-simulation/map/Crime.cpp +++ b/src/openvic-simulation/map/Crime.cpp @@ -1,5 +1,8 @@ #include "Crime.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" +#include "openvic-simulation/modifier/ModifierManager.hpp" + using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -31,13 +34,16 @@ bool CrimeManager::load_crime_modifiers(ModifierManager const& modifier_manager, IconModifier::icon_t icon = 0; ConditionScript trigger { scope_t::PROVINCE, scope_t::NO_SCOPE, scope_t::NO_SCOPE }; bool default_active = false; - bool ret = modifier_manager.expect_modifier_value_and_keys( - move_variable_callback(modifier_value), + + bool ret = NodeTools::expect_dictionary_keys_and_default( + modifier_manager.expect_base_province_modifier(modifier_value), "icon", ZERO_OR_ONE, expect_uint(assign_variable_callback(icon)), "trigger", ONE_EXACTLY, trigger.expect_script(), "active", ZERO_OR_ONE, expect_bool(assign_variable_callback(default_active)) )(value); + ret &= add_crime_modifier(key, std::move(modifier_value), icon, std::move(trigger), default_active); + return ret; } )(root); diff --git a/src/openvic-simulation/map/MapDefinition.cpp b/src/openvic-simulation/map/MapDefinition.cpp index 828a679..faea838 100644 --- a/src/openvic-simulation/map/MapDefinition.cpp +++ b/src/openvic-simulation/map/MapDefinition.cpp @@ -3,6 +3,8 @@ #include <cstdint> #include <vector> +#include "openvic-simulation/dataloader/NodeTools.hpp" +#include "openvic-simulation/modifier/ModifierManager.hpp" #include "openvic-simulation/types/Colour.hpp" #include "openvic-simulation/types/OrderedContainers.hpp" #include "openvic-simulation/types/Vector.hpp" @@ -955,7 +957,11 @@ bool MapDefinition::load_climate_file(ModifierManager const& modifier_manager, a Climate* cur_climate = climates.get_item_by_identifier(identifier); if (cur_climate == nullptr) { ModifierValue values; - ret &= modifier_manager.expect_modifier_value(move_variable_callback(values))(node); + + ret &= NodeTools::expect_dictionary( + modifier_manager.expect_base_province_modifier(values) + )(node); + ret &= climates.add_item({ identifier, std::move(values), Modifier::modifier_type_t::CLIMATE }); } else { ret &= expect_list_reserve_length(*cur_climate, expect_province_definition_identifier( @@ -999,6 +1005,7 @@ bool MapDefinition::load_continent_file(ModifierManager const& modifier_manager, bool ret = expect_dictionary_reserve_length( continents, [this, &modifier_manager](std::string_view identifier, ast::NodeCPtr node) -> bool { + if (identifier.empty()) { Logger::error("Invalid continent identifier - empty!"); return false; @@ -1006,7 +1013,8 @@ bool MapDefinition::load_continent_file(ModifierManager const& modifier_manager, ModifierValue values; std::vector<ProvinceDefinition const*> prov_list; - bool ret = modifier_manager.expect_modifier_value_and_keys(move_variable_callback(values), + bool ret = NodeTools::expect_dictionary_keys_and_default( + modifier_manager.expect_base_province_modifier(values), "provinces", ONE_EXACTLY, expect_list_reserve_length(prov_list, expect_province_definition_identifier( [&prov_list](ProvinceDefinition const& province) -> bool { if (province.continent == nullptr) { diff --git a/src/openvic-simulation/map/MapInstance.cpp b/src/openvic-simulation/map/MapInstance.cpp index 0b5286f..4a4a1e5 100644 --- a/src/openvic-simulation/map/MapInstance.cpp +++ b/src/openvic-simulation/map/MapInstance.cpp @@ -3,7 +3,6 @@ #include "openvic-simulation/history/ProvinceHistory.hpp" #include "openvic-simulation/map/MapDefinition.hpp" #include "openvic-simulation/utility/Logger.hpp" -#include "economy/production/ProductionType.hpp" using namespace OpenVic; @@ -83,7 +82,7 @@ bool MapInstance::setup( bool MapInstance::apply_history_to_provinces( ProvinceHistoryManager const& history_manager, Date date, CountryInstanceManager& country_manager, - IssueManager const& issue_manager, ProductionTypeManager const& production_type_manager + IssueManager const& issue_manager ) { bool ret = true; @@ -94,30 +93,34 @@ bool MapInstance::apply_history_to_provinces( if (history_map != nullptr) { ProvinceHistoryEntry const* pop_history_entry = nullptr; - ProductionType const* rgo_production_type = nullptr; + ProductionType const* rgo_production_type_nullable = nullptr; for (auto const& [entry_date, entry] : history_map->get_entries()) { - if (entry_date > date) { - break; + if(entry_date > date) { + if(pop_history_entry != nullptr) { + break; + } + } else { + province.apply_history_to_province(*entry, country_manager); + std::optional<ProductionType const*> const& rgo_production_type_nullable_optional = entry->get_rgo_production_type_nullable(); + if (rgo_production_type_nullable_optional.has_value()) { + rgo_production_type_nullable = rgo_production_type_nullable_optional.value(); + } } - rgo_production_type = entry->get_rgo_production_type(); - province.apply_history_to_province(*entry, country_manager); - if (!entry->get_pops().empty()) { pop_history_entry = entry.get(); } } - if (pop_history_entry != nullptr) { + if (pop_history_entry == nullptr) { + Logger::warning("No pop history entry for province ",province.get_identifier(), " for date ", date.to_string()); + } else { province.add_pop_vec(pop_history_entry->get_pops()); - province.setup_pop_test_values(issue_manager); } - if(rgo_production_type != nullptr) { - province.setup_rgo(*rgo_production_type); - } + ret&=province.set_rgo_production_type_nullable(rgo_production_type_nullable); } } } @@ -125,6 +128,12 @@ bool MapInstance::apply_history_to_provinces( return ret; } +void MapInstance::update_modifier_sums(Date today, StaticModifierCache const& static_modifier_cache) { + for (ProvinceInstance& province : province_instances.get_items()) { + province.update_modifier_sum(today, static_modifier_cache); + } +} + void MapInstance::update_gamestate(Date today, DefineManager const& define_manager) { for (ProvinceInstance& province : province_instances.get_items()) { province.update_gamestate(today, define_manager); @@ -151,3 +160,9 @@ void MapInstance::tick(Date today) { province.tick(today); } } + +void MapInstance::initialise_for_new_game(ModifierEffectCache const& modifier_effect_cache){ + for (ProvinceInstance& province : province_instances.get_items()) { + province.initialise_for_new_game(modifier_effect_cache); + } +}
\ No newline at end of file diff --git a/src/openvic-simulation/map/MapInstance.hpp b/src/openvic-simulation/map/MapInstance.hpp index 22a564f..a580c55 100644 --- a/src/openvic-simulation/map/MapInstance.hpp +++ b/src/openvic-simulation/map/MapInstance.hpp @@ -5,7 +5,6 @@ #include "openvic-simulation/map/State.hpp" #include "openvic-simulation/types/Date.hpp" #include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "economy/production/ProductionType.hpp" namespace OpenVic { struct MapDefinition; @@ -50,10 +49,12 @@ namespace OpenVic { ); bool apply_history_to_provinces( ProvinceHistoryManager const& history_manager, Date date, CountryInstanceManager& country_manager, - IssueManager const& issue_manager, ProductionTypeManager const& production_type_manager + IssueManager const& issue_manager ); + void update_modifier_sums(Date today, StaticModifierCache const& static_modifier_cache); void update_gamestate(Date today, DefineManager const& define_manager); void tick(Date today); + void initialise_for_new_game(ModifierEffectCache const& modifier_effect_cache); }; } diff --git a/src/openvic-simulation/map/ProvinceDefinition.cpp b/src/openvic-simulation/map/ProvinceDefinition.cpp index 14828e8..60c8d6c 100644 --- a/src/openvic-simulation/map/ProvinceDefinition.cpp +++ b/src/openvic-simulation/map/ProvinceDefinition.cpp @@ -89,7 +89,7 @@ bool ProvinceDefinition::load_positions( port = true; port_adjacent_province = province; } else { - /* Expected provinces with invalid ports: 39, 296, 1047, 1406, 2044 */ + /* Expected provinces with invalid ports: 39, 296, 1047, 1406, 2044 */ Logger::warning( "Invalid port for province ", get_identifier(), ": facing province ", province, " which has: water = ", province->is_water(), ", adjacent = ", is_adjacent_to(province) diff --git a/src/openvic-simulation/map/ProvinceInstance.cpp b/src/openvic-simulation/map/ProvinceInstance.cpp index ffcdfa1..64a104b 100644 --- a/src/openvic-simulation/map/ProvinceInstance.cpp +++ b/src/openvic-simulation/map/ProvinceInstance.cpp @@ -2,14 +2,18 @@ #include "openvic-simulation/country/CountryInstance.hpp" #include "openvic-simulation/economy/production/ProductionType.hpp" +#include "openvic-simulation/economy/production/ResourceGatheringOperation.hpp" #include "openvic-simulation/history/ProvinceHistory.hpp" +#include "openvic-simulation/map/Crime.hpp" #include "openvic-simulation/map/ProvinceDefinition.hpp" +#include "openvic-simulation/map/Region.hpp" +#include "openvic-simulation/map/TerrainType.hpp" #include "openvic-simulation/military/UnitInstanceGroup.hpp" #include "openvic-simulation/misc/Define.hpp" +#include "openvic-simulation/modifier/StaticModifierCache.hpp" #include "openvic-simulation/politics/Ideology.hpp" -#include "economy/production/ResourceGatheringOperation.hpp" -#include "pop/Pop.hpp" -#include "types/fixed_point/FixedPoint.hpp" +#include "openvic-simulation/pop/Pop.hpp" +#include "openvic-simulation/utility/Logger.hpp" using namespace OpenVic; @@ -25,9 +29,11 @@ ProvinceInstance::ProvinceInstance( owner { nullptr }, controller { nullptr }, cores {}, + modifier_sum {}, + event_modifiers {}, slave { false }, crime { nullptr }, - rgo { nullptr }, + rgo { pop_type_keys }, buildings { "buildings", false }, armies {}, navies {}, @@ -40,15 +46,22 @@ ProvinceInstance::ProvinceInstance( max_supported_regiments { 0 } {} GoodDefinition const* ProvinceInstance::get_rgo_good() const { - if(rgo == nullptr) { return nullptr; } - return rgo->get_production_type()->get_output_good(); + if (!rgo.is_valid()) { return nullptr; } + return &(rgo.get_production_type_nullable()->get_output_good()); } -void ProvinceInstance::set_rgo_production_type(ProductionType const& rgo_production_type) { - if(rgo_production_type.get_template_type() != ProductionType::template_type_t::RGO) { - //error +bool ProvinceInstance::set_rgo_production_type_nullable(ProductionType const* rgo_production_type_nullable) { + bool is_valid_operation = true; + if (rgo_production_type_nullable != nullptr) { + ProductionType const& rgo_production_type = *rgo_production_type_nullable; + if (rgo_production_type.get_template_type() != ProductionType::template_type_t::RGO) { + Logger::error("Tried setting province ", get_identifier(), " rgo to ", rgo_production_type.get_identifier(), " which is not of template_type RGO."); + is_valid_operation = false; + } + is_valid_operation&=convert_rgo_worker_pops_to_equivalent(rgo_production_type); } - convert_rgo_worker_pops_to_equivalent(rgo_production_type); - rgo->set_production_type(&rgo_production_type); + + rgo.set_production_type_nullable(rgo_production_type_nullable); + return is_valid_operation; } bool ProvinceInstance::set_owner(CountryInstance* new_owner) { @@ -201,49 +214,142 @@ void ProvinceInstance::_update_pops(DefineManager const& define_manager) { } } -fixed_point_t ProvinceInstance::calculate_rgo_size(ProductionType const& production_type) const { - std::vector<Job> const& jobs = production_type.get_jobs(); - fixed_point_t total_worker_count_in_province = 0; //not counting equivalents - for(Pop const& pop : pops) { - bool is_worker_pop_type = false; - for(Job const& job : jobs) { - if(job.get_pop_type() == pop.get_type()) { - is_worker_pop_type = true; - break; +void ProvinceInstance::update_modifier_sum(Date today, StaticModifierCache const& static_modifier_cache) { + // Update sum of direct province modifiers + modifier_sum.clear(); + + const ModifierSum::modifier_source_t province_source { this }; + + // Erase expired event modifiers and add non-expired ones to the sum + std::erase_if(event_modifiers, [this, today, &province_source](ModifierInstance const& modifier) -> bool { + if (today <= modifier.get_expiry_date()) { + modifier_sum.add_modifier(*modifier.get_modifier(), province_source); + return false; + } else { + return true; + } + }); + + // Add static modifiers + if (is_owner_core()) { + modifier_sum.add_modifier(static_modifier_cache.get_core(), province_source); + } + if (province_definition.is_water()) { + modifier_sum.add_modifier(static_modifier_cache.get_sea_zone(), province_source); + } else { + modifier_sum.add_modifier(static_modifier_cache.get_land_province(), province_source); + + if (province_definition.is_coastal()) { + modifier_sum.add_modifier(static_modifier_cache.get_coastal(), province_source); + } else { + modifier_sum.add_modifier(static_modifier_cache.get_non_coastal(), province_source); + } + + // TODO - overseas, blockaded, no_adjacent_controlled, has_siege, occupied, nationalism, infrastructure + } + + for (BuildingInstance const& building : buildings.get_items()) { + modifier_sum.add_modifier(building.get_building_type(), province_source); + } + + modifier_sum.add_modifier_nullcheck(crime, province_source); + + modifier_sum.add_modifier_nullcheck(province_definition.get_continent(), province_source); + + modifier_sum.add_modifier_nullcheck(province_definition.get_climate(), province_source); + + modifier_sum.add_modifier_nullcheck(terrain_type, province_source); + + if constexpr (!ADD_OWNER_CONTRIBUTION) { + if (controller != nullptr) { + controller->contribute_province_modifier_sum(modifier_sum); + } + } +} + +void ProvinceInstance::contribute_country_modifier_sum(ModifierSum const& owner_modifier_sum) { + modifier_sum.add_modifier_sum_exclude_source(owner_modifier_sum, this); +} + +fixed_point_t ProvinceInstance::get_modifier_effect_value(ModifierEffect const& effect) const { + if constexpr (ADD_OWNER_CONTRIBUTION) { + return modifier_sum.get_effect(effect); + } else { + using enum ModifierEffect::target_t; + + if (owner != nullptr) { + if (ModifierEffect::excludes_targets(effect.get_targets(), PROVINCE)) { + // Non-province targeted effects are already added to the country modifier sum + return owner->get_modifier_effect_value(effect); + } else { + // Province-targeted effects aren't passed to the country modifier sum + return owner->get_modifier_effect_value(effect) + modifier_sum.get_effect(effect); } + } else { + return modifier_sum.get_effect(effect); } + } +} + +fixed_point_t ProvinceInstance::get_modifier_effect_value_nullcheck(ModifierEffect const* effect) const { + if (effect != nullptr) { + return get_modifier_effect_value(*effect); + } else { + return fixed_point_t::_0(); + } +} + +void ProvinceInstance::push_contributing_modifiers( + ModifierEffect const& effect, std::vector<ModifierSum::modifier_entry_t>& contributions +) const { + if constexpr (ADD_OWNER_CONTRIBUTION) { + modifier_sum.push_contributing_modifiers(effect, contributions); + } else { + using enum ModifierEffect::target_t; - if(is_worker_pop_type) { - total_worker_count_in_province += pop.get_size(); + if (owner != nullptr) { + if (ModifierEffect::excludes_targets(effect.get_targets(), PROVINCE)) { + // Non-province targeted effects are already added to the country modifier sum + owner->push_contributing_modifiers(effect, contributions); + } else { + // Province-targeted effects aren't passed to the country modifier sum + modifier_sum.push_contributing_modifiers(effect, contributions); + owner->push_contributing_modifiers(effect, contributions); + } + } else { + modifier_sum.push_contributing_modifiers(effect, contributions); } } +} + +std::vector<ModifierSum::modifier_entry_t> ProvinceInstance::get_contributing_modifiers(ModifierEffect const& effect) const { + if constexpr (ADD_OWNER_CONTRIBUTION) { + return modifier_sum.get_contributing_modifiers(effect); + } else { + std::vector<ModifierSum::modifier_entry_t> contributions; - const fixed_point_t base_workforce_size = production_type.get_base_workforce_size(); - //TODO: if is_farm add farm size bonus - //TODO: if is_mine add mine size bonus - return ((total_worker_count_in_province / base_workforce_size).ceil() * fixed_point_t::_1_50()).floor(); + push_contributing_modifiers(effect, contributions); + + return contributions; + } } -void ProvinceInstance::convert_rgo_worker_pops_to_equivalent(ProductionType const& production_type) { +bool ProvinceInstance::convert_rgo_worker_pops_to_equivalent(ProductionType const& production_type) { + bool is_valid_operation = true; std::vector<Job> const& jobs = production_type.get_jobs(); - fixed_point_t total_worker_count_in_province = 0; //not counting equivalents for(Pop& pop : pops) { - bool is_worker_pop_type = false; for(Job const& job : jobs) { PopType const* const job_pop_type = job.get_pop_type(); PopType const* old_pop_type = pop.get_type(); - if(job_pop_type != old_pop_type) { + if (job_pop_type != old_pop_type) { PopType const* const equivalent = old_pop_type->get_equivalent(); - if(job_pop_type == equivalent) { - pop.convert_to_equivalent(); + if (job_pop_type == equivalent) { + is_valid_operation&=pop.convert_to_equivalent(); } } } - - if(is_worker_pop_type) { - total_worker_count_in_province += pop.get_size(); - } } + return is_valid_operation; } void ProvinceInstance::update_gamestate(Date today, DefineManager const& define_manager) { @@ -360,12 +466,8 @@ bool ProvinceInstance::apply_history_to_province(ProvinceHistoryEntry const& ent return ret; } -void ProvinceInstance::setup_rgo(ProductionType const& rgo_production_type) { - convert_rgo_worker_pops_to_equivalent(rgo_production_type); - rgo = new ResourceGatheringOperation { - &rgo_production_type, - calculate_rgo_size(rgo_production_type) - }; +void ProvinceInstance::initialise_for_new_game(ModifierEffectCache const& modifier_effect_cache) { + rgo.initialise_for_new_game(*this, modifier_effect_cache); } void ProvinceInstance::setup_pop_test_values(IssueManager const& issue_manager) { @@ -373,3 +475,7 @@ void ProvinceInstance::setup_pop_test_values(IssueManager const& issue_manager) pop.setup_pop_test_values(issue_manager); } } + +plf::colony<Pop>& ProvinceInstance::get_mutable_pops() { + return pops; +}
\ No newline at end of file diff --git a/src/openvic-simulation/map/ProvinceInstance.hpp b/src/openvic-simulation/map/ProvinceInstance.hpp index f1d4b75..ba800a9 100644 --- a/src/openvic-simulation/map/ProvinceInstance.hpp +++ b/src/openvic-simulation/map/ProvinceInstance.hpp @@ -1,20 +1,16 @@ #pragma once -#include <cstddef> - #include <plf_colony.h> #include "openvic-simulation/economy/BuildingInstance.hpp" #include "openvic-simulation/economy/production/ProductionType.hpp" +#include "openvic-simulation/economy/production/ResourceGatheringOperation.hpp" #include "openvic-simulation/military/UnitInstance.hpp" #include "openvic-simulation/military/UnitType.hpp" +#include "openvic-simulation/modifier/ModifierSum.hpp" #include "openvic-simulation/pop/Pop.hpp" #include "openvic-simulation/types/HasIdentifier.hpp" #include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp" -#include "economy/production/ResourceGatheringOperation.hpp" -#include "types/fixed_point/FixedPoint.hpp" - namespace OpenVic { struct MapInstance; @@ -27,6 +23,7 @@ namespace OpenVic { struct Ideology; struct Culture; struct Religion; + struct StaticModifierCache; struct BuildingTypeManager; struct ProvinceHistoryEntry; struct IssueManager; @@ -74,9 +71,18 @@ namespace OpenVic { CountryInstance* PROPERTY(controller); ordered_set<CountryInstance*> PROPERTY(cores); + public: + static constexpr bool ADD_OWNER_CONTRIBUTION = true; + + private: + // The total/resultant modifier affecting this province, including owner country contributions if + // ADD_OWNER_CONTRIBUTION is true. + ModifierSum PROPERTY(modifier_sum); + std::vector<ModifierInstance> PROPERTY(event_modifiers); + bool PROPERTY(slave); Crime const* PROPERTY_RW(crime); - ResourceGatheringOperation* PROPERTY(rgo); + ResourceGatheringOperation PROPERTY(rgo); IdentifierRegistry<BuildingInstance> IDENTIFIER_REGISTRY(building); ordered_set<ArmyInstance*> PROPERTY(armies); ordered_set<NavyInstance*> PROPERTY(navies); @@ -89,7 +95,7 @@ namespace OpenVic { fixed_point_t PROPERTY(average_literacy); fixed_point_t PROPERTY(average_consciousness); fixed_point_t PROPERTY(average_militancy); - IndexedMap<PopType, fixed_point_t> PROPERTY(pop_type_distribution); + IndexedMap<PopType, Pop::pop_size_t> PROPERTY(pop_type_distribution); IndexedMap<Ideology, fixed_point_t> PROPERTY(ideology_distribution); fixed_point_map_t<Culture const*> PROPERTY(culture_distribution); fixed_point_map_t<Religion const*> PROPERTY(religion_distribution); @@ -102,8 +108,7 @@ namespace OpenVic { void _add_pop(Pop&& pop); void _update_pops(DefineManager const& define_manager); - fixed_point_t calculate_rgo_size(ProductionType const& production_type) const; - void convert_rgo_worker_pops_to_equivalent(ProductionType const& production_type); + bool convert_rgo_worker_pops_to_equivalent(ProductionType const& production_type); public: ProvinceInstance(ProvinceInstance&&) = default; @@ -120,7 +125,7 @@ namespace OpenVic { } GoodDefinition const* get_rgo_good() const; - void set_rgo_production_type(ProductionType const& rgo_production_type); + bool set_rgo_production_type_nullable(ProductionType const* rgo_production_type_nullable); bool set_owner(CountryInstance* new_owner); bool set_controller(CountryInstance* new_controller); @@ -134,6 +139,15 @@ namespace OpenVic { bool add_pop_vec(std::vector<PopBase> const& pop_vec); size_t get_pop_count() const; + void update_modifier_sum(Date today, StaticModifierCache const& static_modifier_cache); + void contribute_country_modifier_sum(ModifierSum const& owner_modifier_sum); + fixed_point_t get_modifier_effect_value(ModifierEffect const& effect) const; + fixed_point_t get_modifier_effect_value_nullcheck(ModifierEffect const* effect) const; + void push_contributing_modifiers( + ModifierEffect const& effect, std::vector<ModifierSum::modifier_entry_t>& contributions + ) const; + std::vector<ModifierSum::modifier_entry_t> get_contributing_modifiers(ModifierEffect const& effect) const; + void update_gamestate(Date today, DefineManager const& define_manager); void tick(Date today); @@ -145,9 +159,9 @@ namespace OpenVic { bool setup(BuildingTypeManager const& building_type_manager); bool apply_history_to_province(ProvinceHistoryEntry const& entry, CountryInstanceManager& country_manager); - //after history and pops! - void setup_rgo(ProductionType const& rgo_production_type); + void initialise_for_new_game(ModifierEffectCache const& modifier_effect_cache); void setup_pop_test_values(IssueManager const& issue_manager); + plf::colony<Pop>& get_mutable_pops(); }; } diff --git a/src/openvic-simulation/map/State.cpp b/src/openvic-simulation/map/State.cpp index 020b6f1..31ea05f 100644 --- a/src/openvic-simulation/map/State.cpp +++ b/src/openvic-simulation/map/State.cpp @@ -5,6 +5,7 @@ #include "openvic-simulation/map/MapInstance.hpp" #include "openvic-simulation/map/ProvinceInstance.hpp" #include "openvic-simulation/map/Region.hpp" +#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" #include "openvic-simulation/utility/StringUtils.hpp" using namespace OpenVic; @@ -65,10 +66,19 @@ void State::update_gamestate() { const int32_t potential_workforce_in_state = 0; // sum of worker pops, regardless of employment const int32_t potential_employment_in_state = 0; // sum of (factory level * production method base_workforce_size) - industrial_power = total_factory_levels_in_state * std::clamp( - (fixed_point_t { potential_workforce_in_state } / 100).floor() * 400 / potential_employment_in_state, - fixed_point_t::_0_20(), fixed_point_t::_4() - ); + fixed_point_t workforce_scalar; + constexpr fixed_point_t min_workforce_scalar = fixed_point_t::_0_20(); + constexpr fixed_point_t max_workforce_scalar = fixed_point_t::_4(); + if (potential_employment_in_state <= 0) { + workforce_scalar = min_workforce_scalar; + } else { + workforce_scalar = std::clamp( + (fixed_point_t { potential_workforce_in_state } / 100).floor() * 400 / potential_employment_in_state, + min_workforce_scalar, max_workforce_scalar + ); + } + + industrial_power = total_factory_levels_in_state * workforce_scalar; } /* Whether two provinces in the same region should be grouped into the same state or not. diff --git a/src/openvic-simulation/map/State.hpp b/src/openvic-simulation/map/State.hpp index 596206a..a39eea6 100644 --- a/src/openvic-simulation/map/State.hpp +++ b/src/openvic-simulation/map/State.hpp @@ -29,7 +29,7 @@ namespace OpenVic { fixed_point_t PROPERTY(average_literacy); fixed_point_t PROPERTY(average_consciousness); fixed_point_t PROPERTY(average_militancy); - IndexedMap<PopType, fixed_point_t> PROPERTY(pop_type_distribution); + IndexedMap<PopType, Pop::pop_size_t> PROPERTY(pop_type_distribution); fixed_point_t PROPERTY(industrial_power); diff --git a/src/openvic-simulation/map/TerrainType.cpp b/src/openvic-simulation/map/TerrainType.cpp index b4cc430..011a3c3 100644 --- a/src/openvic-simulation/map/TerrainType.cpp +++ b/src/openvic-simulation/map/TerrainType.cpp @@ -1,5 +1,8 @@ #include "TerrainType.hpp" +#include <string_view> + +#include "openvic-simulation/modifier/ModifierManager.hpp" #include "openvic-simulation/types/Colour.hpp" using namespace OpenVic; @@ -16,6 +19,38 @@ TerrainTypeMapping::TerrainTypeMapping( ) : HasIdentifier { new_identifier }, type { new_type }, terrain_indices { std::move(new_terrain_indicies) }, priority { new_priority }, has_texture { new_has_texture } {} +bool TerrainTypeManager::generate_modifiers(ModifierManager& modifier_manager) const { + using enum ModifierEffect::format_t; + IndexedMap<TerrainType, ModifierEffectCache::unit_terrain_effects_t>& unit_terrain_effects = + modifier_manager.modifier_effect_cache.unit_terrain_effects; + + unit_terrain_effects.set_keys(&get_terrain_types()); + + constexpr bool has_no_effect = true; + bool ret = true; + for (TerrainType const& terrain_type : get_terrain_types()) { + const std::string_view identifier = terrain_type.get_identifier(); + ModifierEffectCache::unit_terrain_effects_t& this_unit_terrain_effects = unit_terrain_effects[terrain_type]; + ret &= modifier_manager.register_unit_terrain_modifier_effect( + this_unit_terrain_effects.attack, ModifierManager::get_flat_identifier("attack", identifier), true, + PROPORTION_DECIMAL, "UA_ATTACK", has_no_effect + ); + ret &= modifier_manager.register_unit_terrain_modifier_effect( + this_unit_terrain_effects.defence, ModifierManager::get_flat_identifier("defence", identifier), true, + PROPORTION_DECIMAL, "UA_DEFENCE", has_no_effect + ); + ret &= modifier_manager.register_unit_terrain_modifier_effect( + this_unit_terrain_effects.attrition, ModifierManager::get_flat_identifier("attrition", identifier), false, + RAW_DECIMAL, "UA_ATTRITION", has_no_effect + ); + ret &= modifier_manager.register_unit_terrain_modifier_effect( + this_unit_terrain_effects.movement, ModifierManager::get_flat_identifier("movement", identifier), true, + PROPORTION_DECIMAL, "UA_MOVEMENT" + ); + } + return ret; +} + bool TerrainTypeManager::add_terrain_type( std::string_view identifier, colour_t colour, ModifierValue&& values, bool is_water ) { @@ -74,11 +109,15 @@ node_callback_t TerrainTypeManager::_load_terrain_type_categories(ModifierManage ModifierValue values; colour_t colour = colour_t::null(); bool is_water = false; - bool ret = modifier_manager.expect_modifier_value_and_keys(move_variable_callback(values), + + bool ret = NodeTools::expect_dictionary_keys_and_default( + modifier_manager.expect_terrain_modifier(values), "color", ONE_EXACTLY, expect_colour(assign_variable_callback(colour)), "is_water", ZERO_OR_ONE, expect_bool(assign_variable_callback(is_water)) )(type_node); + ret &= add_terrain_type(type_key, colour, std::move(values), is_water); + return ret; } )(root); diff --git a/src/openvic-simulation/map/TerrainType.hpp b/src/openvic-simulation/map/TerrainType.hpp index 3a88610..b15e650 100644 --- a/src/openvic-simulation/map/TerrainType.hpp +++ b/src/openvic-simulation/map/TerrainType.hpp @@ -67,5 +67,6 @@ namespace OpenVic { TerrainTypeMapping::index_t get_terrain_texture_limit() const; bool load_terrain_types(ModifierManager const& modifier_manager, ast::NodeCPtr root); + bool generate_modifiers(ModifierManager& modifier_manager) const; }; } diff --git a/src/openvic-simulation/military/Deployment.cpp b/src/openvic-simulation/military/Deployment.cpp index bd176be..27cd124 100644 --- a/src/openvic-simulation/military/Deployment.cpp +++ b/src/openvic-simulation/military/Deployment.cpp @@ -144,7 +144,7 @@ bool DeploymentManager::load_oob_file( return false; } - army_regiments.push_back({regiment_name, *regiment_type, regiment_home}); + army_regiments.push_back({ regiment_name, *regiment_type, regiment_home }); return ret; }, diff --git a/src/openvic-simulation/military/LeaderTrait.cpp b/src/openvic-simulation/military/LeaderTrait.cpp index da7331a..e00deec 100644 --- a/src/openvic-simulation/military/LeaderTrait.cpp +++ b/src/openvic-simulation/military/LeaderTrait.cpp @@ -1,5 +1,8 @@ #include "LeaderTrait.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" +#include "openvic-simulation/modifier/ModifierManager.hpp" + using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -30,17 +33,14 @@ bool LeaderTraitManager::load_leader_traits_file(ModifierManager const& modifier return expect_dictionary_reserve_length( leader_traits, [this, &modifier_manager, type](std::string_view trait_identifier, ast::NodeCPtr value) -> bool { - static const string_set_t allowed_modifiers = { - "attack", "defence", "morale", "organisation", "reconnaissance", - "speed", "attrition", "experience", "reliability" - }; - ModifierValue modifiers; - bool ret = modifier_manager.expect_whitelisted_modifier_value( - move_variable_callback(modifiers), allowed_modifiers + + bool ret = NodeTools::expect_dictionary( + modifier_manager.expect_leader_modifier(modifiers) )(value); ret &= add_leader_trait(trait_identifier, type, std::move(modifiers)); + return ret; } ); diff --git a/src/openvic-simulation/military/UnitType.cpp b/src/openvic-simulation/military/UnitType.cpp index 71bc5c3..c185154 100644 --- a/src/openvic-simulation/military/UnitType.cpp +++ b/src/openvic-simulation/military/UnitType.cpp @@ -1,7 +1,9 @@ #include "UnitType.hpp" #include "openvic-simulation/country/CountryInstance.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/map/TerrainType.hpp" +#include "openvic-simulation/modifier/ModifierManager.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -28,8 +30,17 @@ UnitType::UnitType( build_time { unit_args.build_time }, build_cost { std::move(unit_args.build_cost) }, supply_consumption { unit_args.supply_consumption }, - supply_cost { std::move(unit_args.supply_cost) }, - terrain_modifiers { std::move(unit_args.terrain_modifiers) } {} + supply_cost { std::move(unit_args.supply_cost) } { + + using enum Modifier::modifier_type_t; + + for (auto [terrain, modifier_value] : mutable_iterator(unit_args.terrain_modifier_values)) { + terrain_modifiers.emplace(terrain, Modifier { + StringUtils::append_string_views(new_identifier, " ", terrain->get_identifier()), std::move(modifier_value), + UNIT_TERRAIN + }); + } +} bool UnitTypeBranched<LAND>::allowed_cultures_check_culture_in_country( allowed_cultures_t allowed_cultures, Culture const& culture, CountryInstance const& country @@ -105,8 +116,8 @@ static bool _check_shared_parameters(std::string_view identifier, UnitType::unit return false; } - if (unit_args.icon <= 0) { - Logger::error("Invalid icon for unit ", identifier, " - ", unit_args.icon, " (must be positive)"); + if (unit_args.icon < 0) { + Logger::error("Invalid icon for unit ", identifier, " - ", unit_args.icon, " (must be >= 0)"); return false; } @@ -231,16 +242,18 @@ bool UnitTypeManager::load_unit_type_file( good_definition_manager.expect_good_definition_decimal_map(move_variable_callback(unit_args.supply_cost)) ); - auto add_terrain_modifier = [&unit_args, &terrain_type_manager, &modifier_manager]( + auto add_terrain_modifier_value = [&unit_args, &terrain_type_manager, &modifier_manager]( std::string_view default_key, ast::NodeCPtr default_value ) -> bool { TerrainType const* terrain_type = terrain_type_manager.get_terrain_type_by_identifier(default_key); + if (terrain_type != nullptr) { - // TODO - restrict what modifier effects can be used here - return modifier_manager.expect_modifier_value( - map_callback(unit_args.terrain_modifiers, terrain_type) + ModifierValue& modifier_value = unit_args.terrain_modifier_values[terrain_type]; + return expect_dictionary( + modifier_manager.expect_unit_terrain_modifier(modifier_value, terrain_type->get_identifier()) )(default_value); } + return key_value_invalid_callback(default_key, default_value); }; @@ -274,7 +287,7 @@ bool UnitTypeManager::load_unit_type_file( regiment_type_args.allowed_cultures = RegimentType::allowed_cultures_t::ALL_CULTURES; } - ret &= expect_dictionary_key_map_and_default(key_map, add_terrain_modifier)(value); + ret &= expect_dictionary_key_map_and_default(key_map, add_terrain_modifier_value)(value); ret &= add_regiment_type(key, unit_args, regiment_type_args); @@ -301,7 +314,7 @@ bool UnitTypeManager::load_unit_type_file( "torpedo_attack", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(ship_type_args.torpedo_attack)) ); - ret &= expect_dictionary_key_map_and_default(key_map, add_terrain_modifier)(value); + ret &= expect_dictionary_key_map_and_default(key_map, add_terrain_modifier_value)(value); ret &= add_ship_type(key, unit_args, ship_type_args); @@ -319,59 +332,71 @@ bool UnitTypeManager::generate_modifiers(ModifierManager& modifier_manager) cons bool ret = true; const auto generate_stat_modifiers = [&modifier_manager, &ret]( - std::string_view identifier, UnitType::branch_t branch + std::derived_from<ModifierEffectCache::unit_type_effects_t> auto unit_type_effects, std::string_view identifier ) -> void { + using enum ModifierEffect::format_t; + const auto stat_modifier = [&modifier_manager, &ret, &identifier]( - std::string_view suffix, bool is_positive_good, ModifierEffect::format_t format, std::string_view localisation_key + ModifierEffect const*& effect_cache, std::string_view suffix, bool is_positive_good, + ModifierEffect::format_t format, std::string_view localisation_key ) -> void { - ret &= modifier_manager.add_modifier_effect( - ModifierManager::get_flat_identifier(identifier, suffix), is_positive_good, format, + ret &= modifier_manager.register_technology_modifier_effect( + effect_cache, ModifierManager::get_flat_identifier(identifier, suffix), is_positive_good, format, StringUtils::append_string_views("$", identifier, "$: $", localisation_key, "$") ); }; - using enum ModifierEffect::format_t; - ret &= modifier_manager.register_complex_modifier(identifier); - stat_modifier("attack", true, RAW_DECIMAL, "ATTACK"); - stat_modifier("defence", true, RAW_DECIMAL, "DEFENCE"); - stat_modifier("default_organisation", true, RAW_DECIMAL, "DEFAULT_ORG"); - stat_modifier("maximum_speed", true, RAW_DECIMAL, "MAXIMUM_SPEED"); - stat_modifier("build_time", false, INT, "BUILD_TIME"); - stat_modifier("supply_consumption", false, PROPORTION_DECIMAL, "SUPPLY_CONSUMPTION"); + stat_modifier(unit_type_effects.attack, "attack", true, RAW_DECIMAL, "ATTACK"); + stat_modifier(unit_type_effects.defence, "defence", true, RAW_DECIMAL, "DEFENCE"); + stat_modifier(unit_type_effects.default_organisation, "default_organisation", true, RAW_DECIMAL, "DEFAULT_ORG"); + stat_modifier(unit_type_effects.maximum_speed, "maximum_speed", true, RAW_DECIMAL, "MAXIMUM_SPEED"); + stat_modifier(unit_type_effects.build_time, "build_time", false, INT, "BUILD_TIME"); + stat_modifier( + unit_type_effects.supply_consumption, "supply_consumption", false, PROPORTION_DECIMAL, "SUPPLY_CONSUMPTION" + ); - switch (branch) { - case LAND: - stat_modifier("reconnaissance", true, RAW_DECIMAL, "RECONAISSANCE"); - stat_modifier("discipline", true, PROPORTION_DECIMAL, "DISCIPLINE"); - stat_modifier("support", true, PROPORTION_DECIMAL, "SUPPORT"); - stat_modifier("maneuver", true, INT, "Maneuver"); - stat_modifier("siege", true, RAW_DECIMAL, "SIEGE"); - break; - case NAVAL: - stat_modifier("colonial_points", true, INT, "COLONIAL_POINTS_TECH"); - stat_modifier("supply_consumption_score", false, INT, "SUPPLY_LOAD"); - stat_modifier("hull", true, RAW_DECIMAL, "HULL"); - stat_modifier("gun_power", true, RAW_DECIMAL, "GUN_POWER"); - stat_modifier("fire_range", true, RAW_DECIMAL, "FIRE_RANGE"); - stat_modifier("evasion", true, PROPORTION_DECIMAL, "EVASION"); - stat_modifier("torpedo_attack", true, RAW_DECIMAL, "TORPEDO_ATTACK"); - break; - default: + if constexpr (std::same_as<decltype(unit_type_effects), ModifierEffectCache::regiment_type_effects_t>) { + stat_modifier(unit_type_effects.reconnaissance, "reconnaissance", true, RAW_DECIMAL, "RECONAISSANCE"); + stat_modifier(unit_type_effects.discipline, "discipline", true, PROPORTION_DECIMAL, "DISCIPLINE"); + stat_modifier(unit_type_effects.support, "support", true, PROPORTION_DECIMAL, "SUPPORT"); + stat_modifier(unit_type_effects.maneuver, "maneuver", true, INT, "Maneuver"); + stat_modifier(unit_type_effects.siege, "siege", true, RAW_DECIMAL, "SIEGE"); + } else if constexpr(std::same_as<decltype(unit_type_effects), ModifierEffectCache::ship_type_effects_t>) { + stat_modifier(unit_type_effects.colonial_points, "colonial_points", true, INT, "COLONIAL_POINTS_TECH"); + stat_modifier(unit_type_effects.supply_consumption_score, "supply_consumption_score", false, INT, "SUPPLY_LOAD"); + stat_modifier(unit_type_effects.hull, "hull", true, RAW_DECIMAL, "HULL"); + stat_modifier(unit_type_effects.gun_power, "gun_power", true, RAW_DECIMAL, "GUN_POWER"); + stat_modifier(unit_type_effects.fire_range, "fire_range", true, RAW_DECIMAL, "FIRE_RANGE"); + stat_modifier(unit_type_effects.evasion, "evasion", true, PROPORTION_DECIMAL, "EVASION"); + stat_modifier(unit_type_effects.torpedo_attack, "torpedo_attack", true, RAW_DECIMAL, "TORPEDO_ATTACK"); + } else { /* Unreachable - unit types are only added via add_regiment_type or add_ship_type which set branch to LAND or NAVAL. */ - Logger::error("Invalid branch for unit ", identifier, ": ", static_cast<int>(branch)); + Logger::error("Invalid branch for unit ", identifier, " - not LAND or NAVAL!"); } }; - generate_stat_modifiers("army_base", LAND); + generate_stat_modifiers(modifier_manager.modifier_effect_cache.army_base_effects, "army_base"); + + IndexedMap<RegimentType, ModifierEffectCache::regiment_type_effects_t>& regiment_type_effects = + modifier_manager.modifier_effect_cache.regiment_type_effects; + + regiment_type_effects.set_keys(&get_regiment_types()); + for (RegimentType const& regiment_type : get_regiment_types()) { - generate_stat_modifiers(regiment_type.get_identifier(), LAND); + generate_stat_modifiers(regiment_type_effects[regiment_type], regiment_type.get_identifier()); } - generate_stat_modifiers("navy_base", NAVAL); + generate_stat_modifiers(modifier_manager.modifier_effect_cache.navy_base_effects, "navy_base"); + + IndexedMap<ShipType, ModifierEffectCache::ship_type_effects_t>& ship_type_effects = + modifier_manager.modifier_effect_cache.ship_type_effects; + + ship_type_effects.set_keys(&get_ship_types()); + for (ShipType const& ship_type : get_ship_types()) { - generate_stat_modifiers(ship_type.get_identifier(), NAVAL); + generate_stat_modifiers(ship_type_effects[ship_type], ship_type.get_identifier()); } return ret; diff --git a/src/openvic-simulation/military/UnitType.hpp b/src/openvic-simulation/military/UnitType.hpp index 3fa9af5..9527041 100644 --- a/src/openvic-simulation/military/UnitType.hpp +++ b/src/openvic-simulation/military/UnitType.hpp @@ -20,7 +20,7 @@ namespace OpenVic { struct UnitType : HasIdentifier { using icon_t = uint32_t; - using terrain_modifiers_t = ordered_map<TerrainType const*, ModifierValue>; + using terrain_modifiers_t = ordered_map<TerrainType const*, Modifier>; enum struct branch_t : uint8_t { INVALID_BRANCH, LAND, NAVAL }; enum struct unit_category_t : uint8_t { @@ -28,6 +28,8 @@ namespace OpenVic { }; struct unit_type_args_t { + using terrain_modifier_values_t = ordered_map<TerrainType const*, ModifierValue>; + icon_t icon = 0; unit_category_t unit_category = unit_category_t::INVALID_UNIT_CATEGORY; // TODO defaults for move_sound and select_sound @@ -38,7 +40,7 @@ namespace OpenVic { supply_consumption = 0; Timespan build_time; GoodDefinition::good_definition_map_t build_cost, supply_cost; - terrain_modifiers_t terrain_modifiers; + terrain_modifier_values_t terrain_modifier_values; unit_type_args_t() = default; unit_type_args_t(unit_type_args_t&&) = default; diff --git a/src/openvic-simulation/misc/SoundEffect.cpp b/src/openvic-simulation/misc/SoundEffect.cpp index b32d353..6f64406 100644 --- a/src/openvic-simulation/misc/SoundEffect.cpp +++ b/src/openvic-simulation/misc/SoundEffect.cpp @@ -25,14 +25,14 @@ bool SoundEffectManager::_load_sound_define(std::string_view sfx_identifier, ast return false; } - ret &= sound_effects.add_item({sfx_identifier,file,volume}); + ret &= sound_effects.add_item({ sfx_identifier, file, volume }); return ret; } bool SoundEffectManager::load_sound_defines_file(ast::NodeCPtr root) { return expect_dictionary_reserve_length(sound_effects, [this](std::string_view key, ast::NodeCPtr value) -> bool { - return _load_sound_define(key,value); + return _load_sound_define(key, value); } )(root); } diff --git a/src/openvic-simulation/modifier/Modifier.cpp b/src/openvic-simulation/modifier/Modifier.cpp index b26b8dd..3cec7df 100644 --- a/src/openvic-simulation/modifier/Modifier.cpp +++ b/src/openvic-simulation/modifier/Modifier.cpp @@ -1,131 +1,31 @@ #include "Modifier.hpp" -#include <string> - -#include <openvic-dataloader/v2script/AbstractSyntaxTree.hpp> - -#include <dryad/node.hpp> - -#include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/utility/TslHelper.hpp" - using namespace OpenVic; -using namespace OpenVic::NodeTools; - -static std::string make_default_modifier_effect_localisation_key(std::string_view identifier) { - return "MODIFIER_" + StringUtils::string_toupper(identifier); -} - -ModifierEffect::ModifierEffect( - std::string_view new_identifier, bool new_positive_good, format_t new_format, std::string_view new_localisation_key -) : HasIdentifier { new_identifier }, positive_good { new_positive_good }, format { new_format }, - localisation_key { - new_localisation_key.empty() ? make_default_modifier_effect_localisation_key(new_identifier) : new_localisation_key - } {} - -ModifierValue::ModifierValue() = default; -ModifierValue::ModifierValue(effect_map_t&& new_values) : values { std::move(new_values) } {} -ModifierValue::ModifierValue(ModifierValue const&) = default; -ModifierValue::ModifierValue(ModifierValue&&) = default; - -ModifierValue& ModifierValue::operator=(ModifierValue const&) = default; -ModifierValue& ModifierValue::operator=(ModifierValue&&) = default; - -void ModifierValue::trim() { - erase_if(values, [](effect_map_t::value_type const& value) -> bool { - return value.second == fixed_point_t::_0(); - }); -} - -size_t ModifierValue::get_effect_count() const { - return values.size(); -} - -void ModifierValue::clear() { - values.clear(); -} -bool ModifierValue::empty() const { - return values.empty(); -} - -fixed_point_t ModifierValue::get_effect(ModifierEffect const& effect, bool* effect_found) const { - const effect_map_t::const_iterator it = values.find(&effect); - if (it != values.end()) { - if (effect_found != nullptr) { - *effect_found = true; - } - return it->second; - } - if (effect_found != nullptr) { - *effect_found = false; - } - return fixed_point_t::_0(); -} - -bool ModifierValue::has_effect(ModifierEffect const& effect) const { - return values.contains(&effect); -} - -void ModifierValue::set_effect(ModifierEffect const& effect, fixed_point_t value) { - values[&effect] = value; -} - -ModifierValue& ModifierValue::operator+=(ModifierValue const& right) { - for (effect_map_t::value_type const& value : right.values) { - values[value.first] += value.second; - } - return *this; -} - -ModifierValue ModifierValue::operator+(ModifierValue const& right) const { - ModifierValue copy = *this; - return copy += right; -} - -ModifierValue ModifierValue::operator-() const { - ModifierValue copy = *this; - for (auto value : mutable_iterator(copy.values)) { - value.second = -value.second; - } - return copy; -} - -ModifierValue& ModifierValue::operator-=(ModifierValue const& right) { - for (effect_map_t::value_type const& value : right.values) { - values[value.first] -= value.second; - } - return *this; -} - -ModifierValue ModifierValue::operator-(ModifierValue const& right) const { - ModifierValue copy = *this; - return copy -= right; -} - -ModifierValue& ModifierValue::operator*=(fixed_point_t const& right) { - for (auto value : mutable_iterator(values)) { - value.second *= right; - } - return *this; -} - -ModifierValue ModifierValue::operator*(fixed_point_t const& right) const { - ModifierValue copy = *this; - return copy *= right; -} - -fixed_point_t& ModifierValue::operator[](ModifierEffect const& effect) { - return values[&effect]; -} - -void ModifierValue::multiply_add(ModifierValue const& other, fixed_point_t multiplier) { - if (multiplier == fixed_point_t::_1()) { - *this += other; - } else if (multiplier != fixed_point_t::_0()) { - for (effect_map_t::value_type const& value : other.values) { - values[value.first] += value.second * multiplier; - } +std::string_view Modifier::modifier_type_to_string(modifier_type_t type) { + using enum Modifier::modifier_type_t; + + switch (type) { +#define _CASE(X) case X: return #X; + _CASE(EVENT) + _CASE(STATIC) + _CASE(TRIGGERED) + _CASE(CRIME) + _CASE(TERRAIN) + _CASE(CLIMATE) + _CASE(CONTINENT) + _CASE(BUILDING) + _CASE(LEADER) + _CASE(UNIT_TERRAIN) + _CASE(NATIONAL_VALUE) + _CASE(NATIONAL_FOCUS) + _CASE(ISSUE) + _CASE(REFORM) + _CASE(TECHNOLOGY) + _CASE(INVENTION) + _CASE(TECH_SCHOOL) +#undef _CASE + default: return "INVALID MODIFIER TYPE"; } } @@ -147,510 +47,3 @@ bool TriggeredModifier::parse_scripts(DefinitionManager const& definition_manage ModifierInstance::ModifierInstance(Modifier const& new_modifier, Date new_expiry_date) : modifier { &new_modifier }, expiry_date { new_expiry_date } {} - -bool ModifierManager::add_modifier_effect( - std::string_view identifier, bool positive_good, ModifierEffect::format_t format, std::string_view localisation_key -) { - if (identifier.empty()) { - Logger::error("Invalid modifier effect identifier - empty!"); - return false; - } - return modifier_effects.add_item({ std::move(identifier), positive_good, format, localisation_key }); -} - -bool ModifierManager::setup_modifier_effects() { - bool ret = true; - - using enum ModifierEffect::format_t; - /* Tech/inventions only */ - ret &= add_modifier_effect("cb_creation_speed", true, PROPORTION_DECIMAL, "CB_MANUFACTURE_TECH"); - ret &= add_modifier_effect("combat_width", false); - ret &= add_modifier_effect("plurality", true, PERCENTAGE_DECIMAL, "TECH_PLURALITY"); - ret &= add_modifier_effect("pop_growth", true, PROPORTION_DECIMAL, "TECH_POP_GROWTH"); - ret &= add_modifier_effect("regular_experience_level", true, RAW_DECIMAL, "REGULAR_EXP_TECH"); - ret &= add_modifier_effect("reinforce_rate", true, PROPORTION_DECIMAL, "REINFORCE_TECH"); - ret &= add_modifier_effect("seperatism", false, PROPORTION_DECIMAL, "SEPARATISM_TECH"); // paradox typo - ret &= add_modifier_effect("shared_prestige", true, RAW_DECIMAL, "SHARED_PRESTIGE_TECH"); - ret &= add_modifier_effect("tax_eff", true, PROPORTION_DECIMAL, "TECH_TAX_EFF"); - - /* Country Modifier Effects */ - ret &= add_modifier_effect("administrative_efficiency", true); - ret &= add_modifier_effect( - "administrative_efficiency_modifier", true, PROPORTION_DECIMAL, - make_default_modifier_effect_localisation_key("administrative_efficiency") - ); - ret &= add_modifier_effect("artisan_input", false); - ret &= add_modifier_effect("artisan_output", true); - ret &= add_modifier_effect("artisan_throughput", true); - ret &= add_modifier_effect("badboy", false, RAW_DECIMAL); - ret &= add_modifier_effect("cb_generation_speed_modifier", true); - ret &= add_modifier_effect( - "civilization_progress_modifier", true, PROPORTION_DECIMAL, - make_default_modifier_effect_localisation_key("civilization_progress") - ); - ret &= add_modifier_effect("colonial_life_rating", false, INT, "COLONIAL_LIFE_TECH"); - ret &= add_modifier_effect("colonial_migration", true, PROPORTION_DECIMAL, "COLONIAL_MIGRATION_TECH"); - ret &= add_modifier_effect("colonial_points", true, INT, "COLONIAL_POINTS_TECH"); - ret &= add_modifier_effect("colonial_prestige", true, PROPORTION_DECIMAL, "COLONIAL_PRESTIGE_MODIFIER_TECH"); - ret &= add_modifier_effect("core_pop_consciousness_modifier", false, RAW_DECIMAL); - ret &= add_modifier_effect("core_pop_militancy_modifier", false, RAW_DECIMAL); - ret &= add_modifier_effect("dig_in_cap", true, INT, "DIGIN_FROM_TECH"); - ret &= add_modifier_effect("diplomatic_points", true, PROPORTION_DECIMAL, "DIPLOMATIC_POINTS_TECH"); - ret &= add_modifier_effect( - "diplomatic_points_modifier", true, PROPORTION_DECIMAL, - make_default_modifier_effect_localisation_key("diplopoints_gain") - ); - ret &= add_modifier_effect("education_efficiency", true); - ret &= add_modifier_effect( - "education_efficiency_modifier", true, PROPORTION_DECIMAL, - make_default_modifier_effect_localisation_key("education_efficiency") - ); - ret &= add_modifier_effect("factory_cost", false); - ret &= add_modifier_effect("factory_input", false); - ret &= add_modifier_effect("factory_maintenance", false); - ret &= add_modifier_effect("factory_output", true); - ret &= add_modifier_effect("factory_owner_cost", false); - ret &= add_modifier_effect("factory_throughput", true); - ret &= add_modifier_effect( - "global_assimilation_rate", true, PROPORTION_DECIMAL, - make_default_modifier_effect_localisation_key("assimilation_rate") - ); - ret &= add_modifier_effect( - "global_immigrant_attract", true, PROPORTION_DECIMAL, - make_default_modifier_effect_localisation_key("immigant_attract") - ); - ret &= add_modifier_effect("global_pop_consciousness_modifier", false, RAW_DECIMAL); - ret &= add_modifier_effect("global_pop_militancy_modifier", false, RAW_DECIMAL); - ret &= add_modifier_effect( - "global_population_growth", true, PROPORTION_DECIMAL, - make_default_modifier_effect_localisation_key("population_growth") - ); - ret &= add_modifier_effect("goods_demand", false); - ret &= add_modifier_effect("import_cost", false); - ret &= add_modifier_effect("increase_research", true, PROPORTION_DECIMAL, "INC_RES_TECH"); - ret &= add_modifier_effect("influence", true, PROPORTION_DECIMAL, "TECH_GP_INFLUENCE"); - ret &= add_modifier_effect( - "influence_modifier", true, PROPORTION_DECIMAL, - make_default_modifier_effect_localisation_key("greatpower_influence_gain") - ); - ret &= add_modifier_effect("issue_change_speed", true); - ret &= add_modifier_effect( - "land_attack_modifier", true, PROPORTION_DECIMAL, make_default_modifier_effect_localisation_key("land_attack") - ); - ret &= add_modifier_effect("land_attrition", false, PROPORTION_DECIMAL, "LAND_ATTRITION_TECH"); - ret &= add_modifier_effect( - "land_defense_modifier", true, PROPORTION_DECIMAL, make_default_modifier_effect_localisation_key("land_defense") - ); - ret &= add_modifier_effect("land_organisation", true); - ret &= add_modifier_effect("land_unit_start_experience", true, RAW_DECIMAL); - ret &= add_modifier_effect("leadership", true, RAW_DECIMAL, "LEADERSHIP"); - ret &= add_modifier_effect( - "leadership_modifier", true, PROPORTION_DECIMAL, - make_default_modifier_effect_localisation_key("global_leadership_modifier") - ); - ret &= add_modifier_effect("literacy_con_impact", false); - ret &= add_modifier_effect("loan_interest", false); - ret &= add_modifier_effect( - "max_loan_modifier", true, PROPORTION_DECIMAL, make_default_modifier_effect_localisation_key("max_loan_amount") - ); - ret &= add_modifier_effect("max_military_spending", true); - ret &= add_modifier_effect("max_national_focus", true, INT, "TECH_MAX_FOCUS"); - ret &= add_modifier_effect("max_social_spending", true); - ret &= add_modifier_effect("max_tariff", true); - ret &= add_modifier_effect("max_tax", true); - ret &= add_modifier_effect("max_war_exhaustion", true, PERCENTAGE_DECIMAL, "MAX_WAR_EXHAUSTION"); - ret &= add_modifier_effect("military_tactics", true, PROPORTION_DECIMAL, "MIL_TACTICS_TECH"); - ret &= add_modifier_effect("min_military_spending", true); - ret &= add_modifier_effect("min_social_spending", true); - ret &= add_modifier_effect("min_tariff", true); - ret &= add_modifier_effect("min_tax", true); - ret &= add_modifier_effect( - "minimum_wage", true, PROPORTION_DECIMAL, make_default_modifier_effect_localisation_key("minimun_wage") - ); - ret &= add_modifier_effect("mobilisation_economy_impact", false); - ret &= add_modifier_effect("mobilisation_size", true); - ret &= add_modifier_effect("mobilization_impact", false); - ret &= add_modifier_effect( - "naval_attack_modifier", true, PROPORTION_DECIMAL, make_default_modifier_effect_localisation_key("naval_attack") - ); - ret &= add_modifier_effect("naval_attrition", false, PROPORTION_DECIMAL, "NAVAL_ATTRITION_TECH"); - ret &= add_modifier_effect( - "naval_defense_modifier", true, PROPORTION_DECIMAL, make_default_modifier_effect_localisation_key("naval_defense") - ); - ret &= add_modifier_effect("naval_organisation", true); - 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); - ret &= add_modifier_effect("pension_level", true); - ret &= add_modifier_effect("permanent_prestige", true, RAW_DECIMAL, "PERMANENT_PRESTIGE_TECH"); - ret &= add_modifier_effect("political_reform_desire", false); - ret &= add_modifier_effect("poor_savings_modifier", true); - ret &= add_modifier_effect("prestige", true, RAW_DECIMAL); - ret &= add_modifier_effect("reinforce_speed", true); - ret &= add_modifier_effect("research_points", true, RAW_DECIMAL); - ret &= add_modifier_effect("research_points_modifier", true); - ret &= add_modifier_effect("research_points_on_conquer", true); - ret &= add_modifier_effect("rgo_output", true); - ret &= add_modifier_effect("rgo_throughput", true); - ret &= add_modifier_effect("ruling_party_support", true); - ret &= add_modifier_effect( - "self_unciv_economic_modifier", false, PROPORTION_DECIMAL, - make_default_modifier_effect_localisation_key("self_unciv_economic") - ); - ret &= add_modifier_effect( - "self_unciv_military_modifier", false, PROPORTION_DECIMAL, - make_default_modifier_effect_localisation_key("self_unciv_military") - ); - ret &= add_modifier_effect("social_reform_desire", false); - ret &= add_modifier_effect("soldier_to_pop_loss", true, PROPORTION_DECIMAL, "SOLDIER_TO_POP_LOSS_TECH"); - ret &= add_modifier_effect("supply_consumption", false); - ret &= add_modifier_effect("supply_range", true, PROPORTION_DECIMAL, "SUPPLY_RANGE_TECH"); - ret &= add_modifier_effect("suppression_points_modifier", true, PROPORTION_DECIMAL, "SUPPRESSION_TECH"); - ret &= add_modifier_effect( - "tariff_efficiency_modifier", true, PROPORTION_DECIMAL, - make_default_modifier_effect_localisation_key("tariff_efficiency") - ); - ret &= add_modifier_effect("tax_efficiency", true); - ret &= add_modifier_effect("unemployment_benefit", true); - ret &= add_modifier_effect( - "unciv_economic_modifier", false, PROPORTION_DECIMAL, make_default_modifier_effect_localisation_key("unciv_economic") - ); - ret &= add_modifier_effect( - "unciv_military_modifier", false, PROPORTION_DECIMAL, make_default_modifier_effect_localisation_key("unciv_military") - ); - ret &= add_modifier_effect("unit_recruitment_time", false); - ret &= add_modifier_effect("war_exhaustion", false, PROPORTION_DECIMAL, "WAR_EXHAUST_BATTLES"); - - /* Province Modifier Effects */ - ret &= add_modifier_effect("assimilation_rate", true); - ret &= add_modifier_effect("boost_strongest_party", false); - ret &= add_modifier_effect("farm_rgo_eff", true, PROPORTION_DECIMAL, "TECH_FARM_OUTPUT"); - ret &= add_modifier_effect( - "farm_rgo_size", true, PROPORTION_DECIMAL, make_default_modifier_effect_localisation_key("farm_size") - ); - ret &= add_modifier_effect( - "immigrant_attract", true, PROPORTION_DECIMAL, make_default_modifier_effect_localisation_key("immigant_attract") - ); - ret &= add_modifier_effect( - "immigrant_push", false, PROPORTION_DECIMAL, make_default_modifier_effect_localisation_key("immigant_push") - ); - ret &= add_modifier_effect("life_rating", true); - ret &= add_modifier_effect( - "local_artisan_input", false, PROPORTION_DECIMAL, make_default_modifier_effect_localisation_key("artisan_input") - ); - ret &= add_modifier_effect( - "local_artisan_output", true, PROPORTION_DECIMAL, make_default_modifier_effect_localisation_key("artisan_output") - ); - ret &= add_modifier_effect( - "local_artisan_throughput", true, PROPORTION_DECIMAL, - make_default_modifier_effect_localisation_key("artisan_throughput") - ); - ret &= add_modifier_effect( - "local_factory_input", false, PROPORTION_DECIMAL, make_default_modifier_effect_localisation_key("factory_input") - ); - ret &= add_modifier_effect( - "local_factory_output", true, PROPORTION_DECIMAL, make_default_modifier_effect_localisation_key("factory_output") - ); - ret &= add_modifier_effect( - "local_factory_throughput", true, PROPORTION_DECIMAL, - make_default_modifier_effect_localisation_key("factory_throughput") - ); - ret &= add_modifier_effect("local_repair", true); - ret &= add_modifier_effect( - "local_rgo_output", true, PROPORTION_DECIMAL, make_default_modifier_effect_localisation_key("rgo_output") - ); - ret &= add_modifier_effect( - "local_rgo_throughput", true, PROPORTION_DECIMAL, make_default_modifier_effect_localisation_key("rgo_throughput") - ); - ret &= add_modifier_effect( - "local_ruling_party_support", true, PROPORTION_DECIMAL, - make_default_modifier_effect_localisation_key("ruling_party_support") - ); - ret &= add_modifier_effect("local_ship_build", false); - ret &= add_modifier_effect("max_attrition", false, RAW_DECIMAL); - ret &= add_modifier_effect("mine_rgo_eff", true, PROPORTION_DECIMAL, "TECH_MINE_OUTPUT"); - ret &= add_modifier_effect( - "mine_rgo_size", true, PROPORTION_DECIMAL, make_default_modifier_effect_localisation_key("mine_size") - ); - ret &= add_modifier_effect("movement_cost", false); - ret &= add_modifier_effect("number_of_voters", false); - ret &= add_modifier_effect("pop_consciousness_modifier", false, RAW_DECIMAL); - ret &= add_modifier_effect("pop_militancy_modifier", false, RAW_DECIMAL); - ret &= add_modifier_effect("population_growth", true); - ret &= add_modifier_effect("supply_limit", true, RAW_DECIMAL); - - /* Military Modifier Effects */ - ret &= add_modifier_effect("attack", true, INT, "TRAIT_ATTACK"); - ret &= add_modifier_effect("attrition", false, RAW_DECIMAL, "ATTRITION"); - ret &= add_modifier_effect("defence", true, INT, "TRAIT_DEFEND"); - ret &= add_modifier_effect("experience", true, PROPORTION_DECIMAL, "TRAIT_EXPERIENCE"); - ret &= add_modifier_effect("morale", true, PROPORTION_DECIMAL, "TRAIT_MORALE"); - ret &= add_modifier_effect("organisation", true, PROPORTION_DECIMAL, "TRAIT_ORGANISATION"); - ret &= add_modifier_effect("reconnaissance", true, PROPORTION_DECIMAL, "TRAIT_RECONAISSANCE"); - ret &= add_modifier_effect("reliability", true, RAW_DECIMAL, "TRAIT_RELIABILITY"); - ret &= add_modifier_effect("speed", true, PROPORTION_DECIMAL, "TRAIT_SPEED"); - - return ret; -} - -bool ModifierManager::register_complex_modifier(std::string_view identifier) { - if (complex_modifiers.emplace(identifier).second) { - return true; - } else { - Logger::error("Duplicate complex modifier: ", identifier); - return false; - } -} - -std::string ModifierManager::get_flat_identifier( - std::string_view complex_modifier_identifier, std::string_view variant_identifier -) { - return StringUtils::append_string_views(complex_modifier_identifier, " ", variant_identifier); -} - -bool ModifierManager::add_event_modifier(std::string_view identifier, ModifierValue&& values, IconModifier::icon_t icon) { - if (identifier.empty()) { - Logger::error("Invalid event modifier effect identifier - empty!"); - return false; - } - - return event_modifiers.add_item( - { identifier, std::move(values), Modifier::modifier_type_t::EVENT, icon }, duplicate_warning_callback - ); -} - -bool ModifierManager::load_event_modifiers(ast::NodeCPtr root) { - const bool ret = expect_dictionary_reserve_length( - event_modifiers, - [this](std::string_view key, ast::NodeCPtr value) -> bool { - ModifierValue modifier_value; - IconModifier::icon_t icon = 0; - bool ret = expect_modifier_value_and_keys( - move_variable_callback(modifier_value), - "icon", ZERO_OR_ONE, expect_uint(assign_variable_callback(icon)) - )(value); - ret &= add_event_modifier(key, std::move(modifier_value), icon); - return ret; - } - )(root); - - lock_event_modifiers(); - - return ret; -} - -bool ModifierManager::add_static_modifier(std::string_view identifier, ModifierValue&& values) { - if (identifier.empty()) { - Logger::error("Invalid static modifier effect identifier - empty!"); - return false; - } - - return static_modifiers.add_item( - { identifier, std::move(values), Modifier::modifier_type_t::STATIC }, duplicate_warning_callback - ); -} - -bool ModifierManager::load_static_modifiers(ast::NodeCPtr root) { - const bool ret = expect_dictionary_reserve_length( - static_modifiers, - [this](std::string_view key, ast::NodeCPtr value) -> bool { - ModifierValue modifier_value; - bool ret = expect_modifier_value(move_variable_callback(modifier_value))(value); - ret &= add_static_modifier(key, std::move(modifier_value)); - return ret; - } - )(root); - - lock_static_modifiers(); - - return ret; -} - -bool ModifierManager::add_triggered_modifier( - std::string_view identifier, ModifierValue&& values, IconModifier::icon_t icon, ConditionScript&& trigger -) { - if (identifier.empty()) { - Logger::error("Invalid triggered modifier effect identifier - empty!"); - return false; - } - - return triggered_modifiers.add_item( - { identifier, std::move(values), Modifier::modifier_type_t::TRIGGERED, icon, std::move(trigger) }, - duplicate_warning_callback - ); -} - -bool ModifierManager::load_triggered_modifiers(ast::NodeCPtr root) { - const bool ret = expect_dictionary_reserve_length( - triggered_modifiers, - [this](std::string_view key, ast::NodeCPtr value) -> bool { - ModifierValue modifier_value; - IconModifier::icon_t icon = 0; - ConditionScript trigger { scope_t::COUNTRY, scope_t::COUNTRY, scope_t::NO_SCOPE }; - - bool ret = expect_modifier_value_and_keys( - move_variable_callback(modifier_value), - "icon", ZERO_OR_ONE, expect_uint(assign_variable_callback(icon)), - "trigger", ONE_EXACTLY, trigger.expect_script() - )(value); - ret &= add_triggered_modifier(key, std::move(modifier_value), icon, std::move(trigger)); - return ret; - } - )(root); - - lock_triggered_modifiers(); - - return ret; -} - -bool ModifierManager::parse_scripts(DefinitionManager const& definition_manager) { - bool ret = true; - - for (TriggeredModifier& modifier : triggered_modifiers.get_items()) { - ret &= modifier.parse_scripts(definition_manager); - } - - return ret; -} - -key_value_callback_t ModifierManager::_modifier_effect_callback( - ModifierValue& modifier, key_value_callback_t default_callback, ModifierEffectValidator auto effect_validator -) const { - const auto add_modifier_cb = [this, &modifier, effect_validator]( - ModifierEffect const* effect, ast::NodeCPtr value - ) -> bool { - if (effect_validator(*effect)) { - static const case_insensitive_string_set_t no_effect_modifiers { - "boost_strongest_party", "poor_savings_modifier", "local_artisan_input", "local_artisan_throughput", - "local_artisan_output", "artisan_input", "artisan_throughput", "artisan_output", - "import_cost", "unciv_economic_modifier", "unciv_military_modifier" - }; - if (no_effect_modifiers.contains(effect->get_identifier())) { - Logger::warning("This modifier does nothing: ", effect->get_identifier()); - } - return expect_fixed_point(map_callback(modifier.values, effect))(value); - } else { - Logger::error("Failed to validate modifier effect: ", effect->get_identifier()); - return false; - } - }; - - 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 = get_flat_identifier(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 && dryad::node_has_kind<ast::IdentifierValue>(value)) { - return add_modifier_cb(effect, value); - } else if (complex_modifiers.contains(key) && dryad::node_has_kind<ast::ListValue>(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_front(add_flattened_modifier_cb, key))(value); - } - } else if (key == "war_exhaustion_effect") { - Logger::warning("war_exhaustion_effect does nothing (vanilla issues have it)."); - return true; - } else { - return default_callback(key, value); - } - }; -} - -node_callback_t ModifierManager::expect_validated_modifier_value_and_default( - callback_t<ModifierValue&&> modifier_callback, key_value_callback_t default_callback, - ModifierEffectValidator auto effect_validator -) const { - return [this, modifier_callback, default_callback, effect_validator](ast::NodeCPtr root) -> bool { - ModifierValue modifier; - bool ret = expect_dictionary_reserve_length( - modifier.values, - _modifier_effect_callback(modifier, default_callback, effect_validator) - )(root); - ret &= modifier_callback(std::move(modifier)); - return ret; - }; -} - -node_callback_t ModifierManager::expect_validated_modifier_value( - callback_t<ModifierValue&&> modifier_callback, ModifierEffectValidator auto effect_validator -) const { - return expect_validated_modifier_value_and_default(modifier_callback, key_value_invalid_callback, effect_validator); -} - -node_callback_t ModifierManager::expect_modifier_value_and_default( - callback_t<ModifierValue&&> modifier_callback, key_value_callback_t default_callback -) const { - return expect_validated_modifier_value_and_default(modifier_callback, default_callback, [](ModifierEffect const&) -> bool { - return true; - }); -} - -node_callback_t ModifierManager::expect_modifier_value(callback_t<ModifierValue&&> modifier_callback) const { - return expect_modifier_value_and_default(modifier_callback, key_value_invalid_callback); -} - -node_callback_t ModifierManager::expect_whitelisted_modifier_value_and_default( - callback_t<ModifierValue&&> modifier_callback, string_set_t const& whitelist, key_value_callback_t default_callback -) const { - return expect_validated_modifier_value_and_default( - modifier_callback, default_callback, - [&whitelist](ModifierEffect const& effect) -> bool { - return whitelist.contains(effect.get_identifier()); - } - ); -} - -node_callback_t ModifierManager::expect_whitelisted_modifier_value( - callback_t<ModifierValue&&> modifier_callback, string_set_t const& whitelist -) const { - return expect_whitelisted_modifier_value_and_default(modifier_callback, whitelist, key_value_invalid_callback); -} - -node_callback_t ModifierManager::expect_modifier_value_and_key_map_and_default( - callback_t<ModifierValue&&> modifier_callback, key_value_callback_t default_callback, key_map_t&& key_map -) const { - return [this, modifier_callback, default_callback, key_map = std::move(key_map)](ast::NodeCPtr node) mutable -> bool { - bool ret = expect_modifier_value_and_default( - modifier_callback, - dictionary_keys_callback(key_map, default_callback) - )(node); - ret &= check_key_map_counts(key_map); - return ret; - }; -} - -node_callback_t ModifierManager::expect_modifier_value_and_key_map( - callback_t<ModifierValue&&> modifier_callback, key_map_t&& key_map -) const { - return expect_modifier_value_and_key_map_and_default(modifier_callback, key_value_invalid_callback, std::move(key_map)); -} - -namespace OpenVic { // so the compiler shuts up - std::ostream& operator<<(std::ostream& stream, ModifierValue const& value) { - for (ModifierValue::effect_map_t::value_type const& effect : value.values) { - stream << effect.first << ": " << effect.second << "\n"; - } - return stream; - } -} diff --git a/src/openvic-simulation/modifier/Modifier.hpp b/src/openvic-simulation/modifier/Modifier.hpp index 80d2db6..f8c28d8 100644 --- a/src/openvic-simulation/modifier/Modifier.hpp +++ b/src/openvic-simulation/modifier/Modifier.hpp @@ -1,89 +1,27 @@ #pragma once +#include <string_view> + +#include "openvic-simulation/modifier/ModifierValue.hpp" #include "openvic-simulation/scripts/ConditionScript.hpp" -#include "openvic-simulation/types/IdentifierRegistry.hpp" +#include "openvic-simulation/types/Date.hpp" +#include "openvic-simulation/utility/Getters.hpp" namespace OpenVic { - struct ModifierManager; - - struct ModifierEffect : HasIdentifier { - friend struct ModifierManager; - - enum class format_t { - 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 */ - }; - - private: - /* If true, positive values will be green and negative values will be red. - * If false, the colours will be switced. - */ - const bool PROPERTY_CUSTOM_PREFIX(positive_good, is); - const format_t PROPERTY(format); - std::string PROPERTY(localisation_key); - - // TODO - format/precision, e.g. 80% vs 0.8 vs 0.800, 2 vs 2.0 vs 200% - - ModifierEffect( - std::string_view new_identifier, bool new_positive_good, format_t new_format, std::string_view new_localisation_key - ); - - public: - ModifierEffect(ModifierEffect&&) = default; - }; - - struct ModifierValue { - friend struct ModifierManager; - - using effect_map_t = fixed_point_map_t<ModifierEffect const*>; - - private: - effect_map_t PROPERTY(values); - - public: - ModifierValue(); - ModifierValue(effect_map_t&& new_values); - ModifierValue(ModifierValue const&); - ModifierValue(ModifierValue&&); - - ModifierValue& operator=(ModifierValue const&); - ModifierValue& operator=(ModifierValue&&); - - /* Removes effect entries with a value of zero. */ - void trim(); - size_t get_effect_count() const; - void clear(); - bool empty() const; - - fixed_point_t get_effect(ModifierEffect const& effect, bool* effect_found = nullptr) const; - bool has_effect(ModifierEffect const& effect) const; - void set_effect(ModifierEffect const& effect, fixed_point_t value); - - ModifierValue& operator+=(ModifierValue const& right); - ModifierValue operator+(ModifierValue const& right) const; - ModifierValue operator-() const; - ModifierValue& operator-=(ModifierValue const& right); - ModifierValue operator-(ModifierValue const& right) const; - ModifierValue& operator*=(fixed_point_t const& right); - ModifierValue operator*(fixed_point_t const& right) const; - - fixed_point_t& operator[](ModifierEffect const& effect); - - void multiply_add(ModifierValue const& other, fixed_point_t multiplier); - - friend std::ostream& operator<<(std::ostream& stream, ModifierValue const& value); - }; + struct UnitType; struct Modifier : HasIdentifier, ModifierValue { friend struct ModifierManager; + friend struct StaticModifierCache; + friend struct UnitType; enum struct modifier_type_t : uint8_t { - EVENT, STATIC, TRIGGERED, CRIME, TERRAIN, CLIMATE, CONTINENT, BUILDING, LEADER, NATIONAL_VALUE, NATIONAL_FOCUS, - ISSUE, REFORM, TECHNOLOGY, INVENTION, TECH_SCHOOL + EVENT, STATIC, TRIGGERED, CRIME, TERRAIN, CLIMATE, CONTINENT, BUILDING, LEADER, UNIT_TERRAIN, + NATIONAL_VALUE, NATIONAL_FOCUS, ISSUE, REFORM, TECHNOLOGY, INVENTION, TECH_SCHOOL }; + static std::string_view modifier_type_to_string(modifier_type_t type); + private: const modifier_type_t PROPERTY(type); @@ -137,108 +75,4 @@ namespace OpenVic { public: ModifierInstance(Modifier const& new_modifier, Date new_expiry_date); }; - - template<typename Fn> - concept ModifierEffectValidator = std::predicate<Fn, ModifierEffect const&>; - - struct ModifierManager { - /* Some ModifierEffects are generated mid-load, such as max/min count modifiers for each building, so - * we can't lock it until loading is over. This means we can't rely on locking for pointer stability, - * so instead we store the effects in a deque which doesn't invalidate pointers on insert. - */ - private: - CaseInsensitiveIdentifierRegistry<ModifierEffect, RegistryStorageInfoDeque> IDENTIFIER_REGISTRY(modifier_effect); - case_insensitive_string_set_t complex_modifiers; - - IdentifierRegistry<IconModifier> IDENTIFIER_REGISTRY(event_modifier); - IdentifierRegistry<Modifier> IDENTIFIER_REGISTRY(static_modifier); - IdentifierRegistry<TriggeredModifier> IDENTIFIER_REGISTRY(triggered_modifier); - - /* effect_validator takes in ModifierEffect const& */ - NodeTools::key_value_callback_t _modifier_effect_callback( - ModifierValue& modifier, NodeTools::key_value_callback_t default_callback, - ModifierEffectValidator auto effect_validator - ) const; - - public: - bool add_modifier_effect( - std::string_view identifier, bool positive_good, - ModifierEffect::format_t format = ModifierEffect::format_t::PROPORTION_DECIMAL, - std::string_view localisation_key = {} - ); - - bool register_complex_modifier(std::string_view identifier); - static std::string get_flat_identifier( - std::string_view complex_modifier_identifier, std::string_view variant_identifier - ); - - bool setup_modifier_effects(); - - bool add_event_modifier(std::string_view identifier, ModifierValue&& values, IconModifier::icon_t icon); - bool load_event_modifiers(ast::NodeCPtr root); - - bool add_static_modifier(std::string_view identifier, ModifierValue&& values); - bool load_static_modifiers(ast::NodeCPtr root); - - bool add_triggered_modifier( - std::string_view identifier, ModifierValue&& values, IconModifier::icon_t icon, ConditionScript&& trigger - ); - bool load_triggered_modifiers(ast::NodeCPtr root); - - bool parse_scripts(DefinitionManager const& definition_manager); - - NodeTools::node_callback_t expect_validated_modifier_value_and_default( - NodeTools::callback_t<ModifierValue&&> modifier_callback, NodeTools::key_value_callback_t default_callback, - ModifierEffectValidator auto effect_validator - ) const; - NodeTools::node_callback_t expect_validated_modifier_value( - NodeTools::callback_t<ModifierValue&&> modifier_callback, ModifierEffectValidator auto effect_validator - ) const; - - NodeTools::node_callback_t expect_modifier_value_and_default( - NodeTools::callback_t<ModifierValue&&> modifier_callback, NodeTools::key_value_callback_t default_callback - ) const; - NodeTools::node_callback_t expect_modifier_value(NodeTools::callback_t<ModifierValue&&> modifier_callback) const; - - NodeTools::node_callback_t expect_whitelisted_modifier_value_and_default( - NodeTools::callback_t<ModifierValue&&> modifier_callback, string_set_t const& whitelist, - NodeTools::key_value_callback_t default_callback - ) const; - NodeTools::node_callback_t expect_whitelisted_modifier_value( - NodeTools::callback_t<ModifierValue&&> modifier_callback, string_set_t const& whitelist - ) const; - - NodeTools::node_callback_t expect_modifier_value_and_key_map_and_default( - NodeTools::callback_t<ModifierValue&&> modifier_callback, NodeTools::key_value_callback_t default_callback, - NodeTools::key_map_t&& key_map - ) const; - NodeTools::node_callback_t expect_modifier_value_and_key_map( - NodeTools::callback_t<ModifierValue&&> modifier_callback, NodeTools::key_map_t&& key_map - ) const; - - template<typename... Args> - NodeTools::node_callback_t expect_modifier_value_and_key_map_and_default( - NodeTools::callback_t<ModifierValue&&> modifier_callback, NodeTools::key_value_callback_t default_callback, - NodeTools::key_map_t&& key_map, Args... args - ) const { - NodeTools::add_key_map_entries(key_map, args...); - return expect_modifier_value_and_key_map_and_default(modifier_callback, default_callback, std::move(key_map)); - } - - template<typename... Args> - NodeTools::node_callback_t expect_modifier_value_and_keys_and_default( - NodeTools::callback_t<ModifierValue&&> modifier_callback, NodeTools::key_value_callback_t default_callback, - Args... args - ) const { - return expect_modifier_value_and_key_map_and_default(modifier_callback, default_callback, {}, args...); - } - template<typename... Args> - NodeTools::node_callback_t expect_modifier_value_and_keys( - NodeTools::callback_t<ModifierValue&&> modifier_callback, Args... args - ) const { - return expect_modifier_value_and_key_map_and_default( - modifier_callback, NodeTools::key_value_invalid_callback, {}, args... - ); - } - }; } diff --git a/src/openvic-simulation/modifier/ModifierEffect.cpp b/src/openvic-simulation/modifier/ModifierEffect.cpp new file mode 100644 index 0000000..eadaf58 --- /dev/null +++ b/src/openvic-simulation/modifier/ModifierEffect.cpp @@ -0,0 +1,49 @@ +#include "ModifierEffect.hpp" + +#include "openvic-simulation/utility/StringUtils.hpp" + +using namespace OpenVic; + +std::string ModifierEffect::target_to_string(target_t target) { + using enum target_t; + + if (target == NO_TARGETS) { + return "NO TARGETS"; + } + + if (target == ALL_TARGETS) { + return "ALL TARGETS"; + } + + static constexpr std::string_view SEPARATOR = " | "; + + std::string ret; + + const auto append_target = [target, &ret](target_t check_target, std::string_view target_str) -> void { + if (!ModifierEffect::excludes_targets(target, check_target)) { + if (!ret.empty()) { + ret += SEPARATOR; + } + ret += target_str; + } + }; + + append_target(COUNTRY, "COUNTRY"); + append_target(PROVINCE, "PROVINCE"); + append_target(UNIT, "UNIT"); + append_target(~ALL_TARGETS, "INVALID TARGET"); + + return ret; +} + +std::string ModifierEffect::make_default_modifier_effect_localisation_key(std::string_view identifier) { + return "MODIFIER_" + StringUtils::string_toupper(identifier); +} + +ModifierEffect::ModifierEffect( + std::string_view new_identifier, bool new_is_positive_good, format_t new_format, target_t new_targets, + std::string_view new_localisation_key, bool new_has_no_effect +) : HasIdentifier { new_identifier }, positive_good { new_is_positive_good }, format { new_format }, targets { new_targets }, + localisation_key { + new_localisation_key.empty() ? make_default_modifier_effect_localisation_key(new_identifier) : new_localisation_key + }, no_effect { new_has_no_effect } {} diff --git a/src/openvic-simulation/modifier/ModifierEffect.hpp b/src/openvic-simulation/modifier/ModifierEffect.hpp new file mode 100644 index 0000000..9eb425b --- /dev/null +++ b/src/openvic-simulation/modifier/ModifierEffect.hpp @@ -0,0 +1,64 @@ +#pragma once + +#include <string> +#include <string_view> + +#include "openvic-simulation/types/EnumBitfield.hpp" +#include "openvic-simulation/types/HasIdentifier.hpp" + +namespace OpenVic { + struct ModifierManager; + + struct ModifierEffect : HasIdentifier { + friend struct ModifierManager; + + enum class format_t : uint8_t { + 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 */ + }; + + enum class target_t : uint8_t { + NO_TARGETS = 0, + COUNTRY = 1 << 0, + PROVINCE = 1 << 1, + UNIT = 1 << 2, + ALL_TARGETS = (1 << 3) - 1 + }; + + static constexpr bool excludes_targets(target_t targets, target_t excluded_target); + + static std::string target_to_string(target_t target); + + static std::string make_default_modifier_effect_localisation_key(std::string_view identifier); + + private: + /* If true, positive values will be green and negative values will be red. + * If false, the colours will be switced. + */ + const bool PROPERTY_CUSTOM_PREFIX(positive_good, is); + const bool PROPERTY_CUSTOM_PREFIX(no_effect, has); + const format_t PROPERTY(format); + const target_t PROPERTY(targets); + std::string PROPERTY(localisation_key); + + // TODO - format/precision, e.g. 80% vs 0.8 vs 0.800, 2 vs 2.0 vs 200% + + ModifierEffect( + std::string_view new_identifier, bool new_is_positive_good, format_t new_format, target_t new_targets, + std::string_view new_localisation_key, bool new_has_no_effect + ); + + public: + ModifierEffect(ModifierEffect&&) = default; + }; + + template<> struct enable_bitfield<ModifierEffect::target_t> : std::true_type {}; + + constexpr bool ModifierEffect::excludes_targets(target_t targets, target_t excluded_targets) { + using enum target_t; + + return (targets & excluded_targets) == NO_TARGETS; + } +} diff --git a/src/openvic-simulation/modifier/ModifierEffectCache.cpp b/src/openvic-simulation/modifier/ModifierEffectCache.cpp new file mode 100644 index 0000000..afb733a --- /dev/null +++ b/src/openvic-simulation/modifier/ModifierEffectCache.cpp @@ -0,0 +1,249 @@ +#include "ModifierEffectCache.hpp" + +#include "openvic-simulation/economy/BuildingType.hpp" +#include "openvic-simulation/economy/GoodDefinition.hpp" +#include "openvic-simulation/map/TerrainType.hpp" +#include "openvic-simulation/politics/Rebel.hpp" +#include "openvic-simulation/research/Technology.hpp" + +using namespace OpenVic; + +ModifierEffectCache::building_type_effects_t::building_type_effects_t() + : min_level { nullptr }, + max_level { nullptr } {} + +ModifierEffectCache::good_effects_t::good_effects_t() + : artisan_goods_input { nullptr }, + artisan_goods_output { nullptr }, + artisan_goods_throughput { nullptr }, + factory_goods_input { nullptr }, + factory_goods_output { nullptr }, + factory_goods_throughput { nullptr }, + rgo_goods_output { nullptr }, + rgo_goods_throughput { nullptr }, + rgo_size { nullptr } {} + +ModifierEffectCache::unit_type_effects_t::unit_type_effects_t() + : attack { nullptr }, + defence { nullptr }, + default_organisation { nullptr }, + maximum_speed { nullptr }, + build_time { nullptr }, + supply_consumption { nullptr } {} + +ModifierEffectCache::regiment_type_effects_t::regiment_type_effects_t() + : unit_type_effects_t {}, + reconnaissance { nullptr }, + discipline { nullptr }, + support { nullptr }, + maneuver { nullptr }, + siege { nullptr } {} + +ModifierEffectCache::ship_type_effects_t::ship_type_effects_t() + : unit_type_effects_t {}, + colonial_points { nullptr }, + supply_consumption_score { nullptr }, + hull { nullptr }, + gun_power { nullptr }, + fire_range { nullptr }, + evasion { nullptr }, + torpedo_attack { nullptr } {} + +ModifierEffectCache::unit_terrain_effects_t::unit_terrain_effects_t() + : attack { nullptr }, + defence { nullptr }, + attrition { nullptr }, + movement { nullptr } {} + +ModifierEffectCache::strata_effects_t::strata_effects_t() + : income_modifier { nullptr }, + vote { nullptr }, + life_needs { nullptr }, + everyday_needs { nullptr }, + luxury_needs { nullptr } {} + +ModifierEffectCache::ModifierEffectCache() + : /* Tech/inventions only */ + cb_creation_speed { nullptr }, + combat_width_additive { nullptr }, + plurality { nullptr }, + pop_growth { nullptr }, + prestige_gain_multiplier { nullptr }, + regular_experience_level { nullptr }, + reinforce_rate { nullptr }, + separatism { nullptr }, + shared_prestige { nullptr }, + tax_eff { nullptr }, + + /* Country Modifier Effects */ + administrative_efficiency { nullptr }, + administrative_efficiency_modifier { nullptr }, + artisan_input { nullptr }, + artisan_output { nullptr }, + artisan_throughput { nullptr }, + badboy { nullptr }, + cb_generation_speed_modifier { nullptr }, + civilization_progress_modifier { nullptr }, + colonial_life_rating { nullptr }, + colonial_migration { nullptr }, + colonial_points { nullptr }, + colonial_prestige { nullptr }, + core_pop_consciousness_modifier { nullptr }, + core_pop_militancy_modifier { nullptr }, + dig_in_cap { nullptr }, + diplomatic_points { nullptr }, + diplomatic_points_modifier { nullptr }, + education_efficiency { nullptr }, + education_efficiency_modifier { nullptr }, + factory_cost { nullptr }, + factory_input { nullptr }, + factory_maintenance { nullptr }, + factory_output { nullptr }, + factory_owner_cost { nullptr }, + factory_throughput { nullptr }, + global_assimilation_rate { nullptr }, + global_immigrant_attract { nullptr }, + global_pop_consciousness_modifier { nullptr }, + global_pop_militancy_modifier { nullptr }, + global_population_growth { nullptr }, + goods_demand { nullptr }, + import_cost { nullptr }, + increase_research { nullptr }, + influence { nullptr }, + influence_modifier { nullptr }, + issue_change_speed { nullptr }, + land_attack_modifier { nullptr }, + land_attrition { nullptr }, + land_defense_modifier { nullptr }, + land_organisation { nullptr }, + land_unit_start_experience { nullptr }, + leadership { nullptr }, + leadership_modifier { nullptr }, + literacy_con_impact { nullptr }, + loan_interest_base { nullptr }, + loan_interest_foreign { nullptr }, + max_loan_modifier { nullptr }, + max_military_spending { nullptr }, + max_national_focus { nullptr }, + max_social_spending { nullptr }, + max_tariff { nullptr }, + max_tax { nullptr }, + max_war_exhaustion { nullptr }, + military_tactics { nullptr }, + min_military_spending { nullptr }, + min_social_spending { nullptr }, + min_tariff { nullptr }, + min_tax { nullptr }, + minimum_wage { nullptr }, + mobilisation_economy_impact { nullptr }, + mobilisation_size { nullptr }, + mobilization_impact { nullptr }, + naval_attack_modifier { nullptr }, + naval_attrition { nullptr }, + naval_defense_modifier { nullptr }, + naval_organisation { nullptr }, + naval_unit_start_experience { nullptr }, + non_accepted_pop_consciousness_modifier { nullptr }, + non_accepted_pop_militancy_modifier { nullptr }, + org_regain { nullptr }, + pension_level { nullptr }, + permanent_prestige { nullptr }, + political_reform_desire { nullptr }, + poor_savings_modifier { nullptr }, + prestige_monthly_gain { nullptr }, + reinforce_speed { nullptr }, + research_points { nullptr }, + research_points_modifier { nullptr }, + research_points_on_conquer { nullptr }, + rgo_output { nullptr }, + rgo_throughput { nullptr }, + ruling_party_support { nullptr }, + self_unciv_economic_modifier { nullptr }, + self_unciv_military_modifier { nullptr }, + social_reform_desire { nullptr }, + soldier_to_pop_loss { nullptr }, + supply_consumption { nullptr }, + supply_range { nullptr }, + suppression_points_modifier { nullptr }, + tariff_efficiency_modifier { nullptr }, + tax_efficiency { nullptr }, + unemployment_benefit { nullptr }, + unciv_economic_modifier { nullptr }, + unciv_military_modifier { nullptr }, + unit_recruitment_time { nullptr }, + war_exhaustion { nullptr }, + + /* Province Modifier Effects */ + assimilation_rate { nullptr }, + boost_strongest_party { nullptr }, + combat_width_percentage_change { nullptr }, + defence_terrain { nullptr }, + farm_rgo_throughput_global { nullptr }, + farm_rgo_output_global { nullptr }, + farm_rgo_output_local { nullptr }, + farm_rgo_size_global { nullptr }, + farm_rgo_size_local { nullptr }, + immigrant_attract { nullptr }, + immigrant_push { nullptr }, + life_rating { nullptr }, + local_artisan_input { nullptr }, + local_artisan_output { nullptr }, + local_artisan_throughput { nullptr }, + local_factory_input { nullptr }, + local_factory_output { nullptr }, + local_factory_throughput { nullptr }, + local_repair { nullptr }, + local_rgo_output { nullptr }, + local_rgo_throughput { nullptr }, + local_ruling_party_support { nullptr }, + local_ship_build { nullptr }, + attrition_local { nullptr }, + max_attrition { nullptr }, + mine_rgo_throughput_global { nullptr }, + mine_rgo_output_global { nullptr }, + mine_rgo_output_local { nullptr }, + mine_rgo_size_global { nullptr }, + mine_rgo_size_local { nullptr }, + movement_cost_base { nullptr }, + movement_cost_percentage_change { nullptr }, + number_of_voters { nullptr }, + pop_consciousness_modifier { nullptr }, + pop_militancy_modifier { nullptr }, + population_growth { nullptr }, + supply_limit_global_percentage_change { nullptr }, + supply_limit_local_base { nullptr }, + + /* Military Modifier Effects */ + attack_leader { nullptr }, + attrition_leader { nullptr }, + defence_leader { nullptr }, + experience { nullptr }, + morale_global { nullptr }, + morale_leader { nullptr }, + organisation { nullptr }, + reconnaissance { nullptr }, + reliability { nullptr }, + speed { nullptr }, + + /* BuildingType Effects */ + building_type_effects { nullptr }, + + /* GoodDefinition Effects */ + good_effects { nullptr }, + + /* UnitType Effects */ + army_base_effects {}, + regiment_type_effects { nullptr }, + navy_base_effects {}, + ship_type_effects { nullptr }, + unit_terrain_effects { nullptr }, + + /* Rebel Effects */ + rebel_org_gain_all { nullptr }, + rebel_org_gain_effects { nullptr }, + + /* Pop Effects */ + strata_effects { nullptr }, + + /* Technology Effects */ + research_bonus_effects { nullptr } {} diff --git a/src/openvic-simulation/modifier/ModifierEffectCache.hpp b/src/openvic-simulation/modifier/ModifierEffectCache.hpp new file mode 100644 index 0000000..757eca0 --- /dev/null +++ b/src/openvic-simulation/modifier/ModifierEffectCache.hpp @@ -0,0 +1,339 @@ +#pragma once + +#include "openvic-simulation/military/UnitType.hpp" +#include "openvic-simulation/types/IndexedMap.hpp" +#include "openvic-simulation/utility/Getters.hpp" + +namespace OpenVic { + struct ModifierEffect; + struct ModifierManager; + + struct BuildingTypeManager; + struct BuildingType; + struct GoodDefinitionManager; + struct GoodDefinition; + struct RebelManager; + struct RebelType; + struct PopManager; + struct Strata; + struct TechnologyManager; + struct TechnologyFolder; + struct TerrainTypeManager; + struct TerrainType; + + struct ModifierEffectCache { + friend struct ModifierManager; + friend struct BuildingTypeManager; + friend struct GoodDefinitionManager; + friend struct UnitTypeManager; + friend struct RebelManager; + friend struct PopManager; + friend struct TechnologyManager; + friend struct TerrainTypeManager; + + private: + /* Tech/inventions only */ + ModifierEffect const* PROPERTY(cb_creation_speed); + ModifierEffect const* PROPERTY(combat_width_additive); + ModifierEffect const* PROPERTY(plurality); + ModifierEffect const* PROPERTY(pop_growth); + ModifierEffect const* PROPERTY(prestige_gain_multiplier); + ModifierEffect const* PROPERTY(regular_experience_level); + ModifierEffect const* PROPERTY(reinforce_rate); + ModifierEffect const* PROPERTY(separatism); + ModifierEffect const* PROPERTY(shared_prestige); + ModifierEffect const* PROPERTY(tax_eff); + + /* Country Modifier Effects */ + ModifierEffect const* PROPERTY(administrative_efficiency); + ModifierEffect const* PROPERTY(administrative_efficiency_modifier); + ModifierEffect const* PROPERTY(artisan_input); + ModifierEffect const* PROPERTY(artisan_output); + ModifierEffect const* PROPERTY(artisan_throughput); + ModifierEffect const* PROPERTY(badboy); + ModifierEffect const* PROPERTY(cb_generation_speed_modifier); + ModifierEffect const* PROPERTY(civilization_progress_modifier); + ModifierEffect const* PROPERTY(colonial_life_rating); + ModifierEffect const* PROPERTY(colonial_migration); + ModifierEffect const* PROPERTY(colonial_points); + ModifierEffect const* PROPERTY(colonial_prestige); + ModifierEffect const* PROPERTY(core_pop_consciousness_modifier); + ModifierEffect const* PROPERTY(core_pop_militancy_modifier); + ModifierEffect const* PROPERTY(dig_in_cap); + ModifierEffect const* PROPERTY(diplomatic_points); + ModifierEffect const* PROPERTY(diplomatic_points_modifier); + ModifierEffect const* PROPERTY(education_efficiency); + ModifierEffect const* PROPERTY(education_efficiency_modifier); + ModifierEffect const* PROPERTY(factory_cost); + ModifierEffect const* PROPERTY(factory_input); + ModifierEffect const* PROPERTY(factory_maintenance); + ModifierEffect const* PROPERTY(factory_output); + ModifierEffect const* PROPERTY(factory_owner_cost); + ModifierEffect const* PROPERTY(factory_throughput); + ModifierEffect const* PROPERTY(global_assimilation_rate); + ModifierEffect const* PROPERTY(global_immigrant_attract); + ModifierEffect const* PROPERTY(global_pop_consciousness_modifier); + ModifierEffect const* PROPERTY(global_pop_militancy_modifier); + ModifierEffect const* PROPERTY(global_population_growth); + ModifierEffect const* PROPERTY(goods_demand); + ModifierEffect const* PROPERTY(import_cost); + ModifierEffect const* PROPERTY(increase_research); + ModifierEffect const* PROPERTY(influence); + ModifierEffect const* PROPERTY(influence_modifier); + ModifierEffect const* PROPERTY(issue_change_speed); + ModifierEffect const* PROPERTY(land_attack_modifier); + ModifierEffect const* PROPERTY(land_attrition); + ModifierEffect const* PROPERTY(land_defense_modifier); + ModifierEffect const* PROPERTY(land_organisation); + ModifierEffect const* PROPERTY(land_unit_start_experience); + ModifierEffect const* PROPERTY(leadership); + ModifierEffect const* PROPERTY(leadership_modifier); + ModifierEffect const* PROPERTY(literacy_con_impact); + ModifierEffect const* PROPERTY(loan_interest_base); + ModifierEffect const* PROPERTY(loan_interest_foreign); + ModifierEffect const* PROPERTY(max_loan_modifier); + ModifierEffect const* PROPERTY(max_military_spending); + ModifierEffect const* PROPERTY(max_national_focus); + ModifierEffect const* PROPERTY(max_social_spending); + ModifierEffect const* PROPERTY(max_tariff); + ModifierEffect const* PROPERTY(max_tax); + ModifierEffect const* PROPERTY(max_war_exhaustion); + ModifierEffect const* PROPERTY(military_tactics); + ModifierEffect const* PROPERTY(min_military_spending); + ModifierEffect const* PROPERTY(min_social_spending); + ModifierEffect const* PROPERTY(min_tariff); + ModifierEffect const* PROPERTY(min_tax); + ModifierEffect const* PROPERTY(minimum_wage); + ModifierEffect const* PROPERTY(mobilisation_economy_impact); + ModifierEffect const* PROPERTY(mobilisation_size); + ModifierEffect const* PROPERTY(mobilization_impact); + ModifierEffect const* PROPERTY(morale_global); + ModifierEffect const* PROPERTY(naval_attack_modifier); + ModifierEffect const* PROPERTY(naval_attrition); + ModifierEffect const* PROPERTY(naval_defense_modifier); + ModifierEffect const* PROPERTY(naval_organisation); + ModifierEffect const* PROPERTY(naval_unit_start_experience); + ModifierEffect const* PROPERTY(non_accepted_pop_consciousness_modifier); + ModifierEffect const* PROPERTY(non_accepted_pop_militancy_modifier); + ModifierEffect const* PROPERTY(org_regain); + ModifierEffect const* PROPERTY(pension_level); + ModifierEffect const* PROPERTY(permanent_prestige); + ModifierEffect const* PROPERTY(political_reform_desire); + ModifierEffect const* PROPERTY(poor_savings_modifier); + ModifierEffect const* PROPERTY(prestige_monthly_gain); + ModifierEffect const* PROPERTY(reinforce_speed); + ModifierEffect const* PROPERTY(research_points); + ModifierEffect const* PROPERTY(research_points_modifier); + ModifierEffect const* PROPERTY(research_points_on_conquer); + ModifierEffect const* PROPERTY(rgo_output); + ModifierEffect const* PROPERTY(rgo_throughput); + ModifierEffect const* PROPERTY(ruling_party_support); + ModifierEffect const* PROPERTY(self_unciv_economic_modifier); + ModifierEffect const* PROPERTY(self_unciv_military_modifier); + ModifierEffect const* PROPERTY(social_reform_desire); + ModifierEffect const* PROPERTY(soldier_to_pop_loss); + ModifierEffect const* PROPERTY(supply_consumption); + ModifierEffect const* PROPERTY(supply_range); + ModifierEffect const* PROPERTY(suppression_points_modifier); + ModifierEffect const* PROPERTY(tariff_efficiency_modifier); + ModifierEffect const* PROPERTY(tax_efficiency); + ModifierEffect const* PROPERTY(unemployment_benefit); + ModifierEffect const* PROPERTY(unciv_economic_modifier); + ModifierEffect const* PROPERTY(unciv_military_modifier); + ModifierEffect const* PROPERTY(unit_recruitment_time); + ModifierEffect const* PROPERTY(war_exhaustion); + + /* Province Modifier Effects */ + ModifierEffect const* PROPERTY(assimilation_rate); + ModifierEffect const* PROPERTY(boost_strongest_party); + ModifierEffect const* PROPERTY(combat_width_percentage_change); + ModifierEffect const* PROPERTY(defence_terrain); + ModifierEffect const* PROPERTY(farm_rgo_throughput_global); + ModifierEffect const* PROPERTY(farm_rgo_output_global); + ModifierEffect const* PROPERTY(farm_rgo_output_local); + ModifierEffect const* PROPERTY(farm_rgo_size_global); + ModifierEffect const* PROPERTY(farm_rgo_size_local); + ModifierEffect const* PROPERTY(immigrant_attract); + ModifierEffect const* PROPERTY(immigrant_push); + ModifierEffect const* PROPERTY(life_rating); + ModifierEffect const* PROPERTY(local_artisan_input); + ModifierEffect const* PROPERTY(local_artisan_output); + ModifierEffect const* PROPERTY(local_artisan_throughput); + ModifierEffect const* PROPERTY(local_factory_input); + ModifierEffect const* PROPERTY(local_factory_output); + ModifierEffect const* PROPERTY(local_factory_throughput); + ModifierEffect const* PROPERTY(local_repair); + ModifierEffect const* PROPERTY(local_rgo_output); + ModifierEffect const* PROPERTY(local_rgo_throughput); + ModifierEffect const* PROPERTY(local_ruling_party_support); + ModifierEffect const* PROPERTY(local_ship_build); + ModifierEffect const* PROPERTY(attrition_local); + ModifierEffect const* PROPERTY(max_attrition); + ModifierEffect const* PROPERTY(mine_rgo_throughput_global); + ModifierEffect const* PROPERTY(mine_rgo_output_global); + ModifierEffect const* PROPERTY(mine_rgo_output_local); + ModifierEffect const* PROPERTY(mine_rgo_size_global); + ModifierEffect const* PROPERTY(mine_rgo_size_local); + ModifierEffect const* PROPERTY(movement_cost_base); + ModifierEffect const* PROPERTY(movement_cost_percentage_change); + ModifierEffect const* PROPERTY(number_of_voters); + ModifierEffect const* PROPERTY(pop_consciousness_modifier); + ModifierEffect const* PROPERTY(pop_militancy_modifier); + ModifierEffect const* PROPERTY(population_growth); + ModifierEffect const* PROPERTY(supply_limit_global_percentage_change); + ModifierEffect const* PROPERTY(supply_limit_global_base); + ModifierEffect const* PROPERTY(supply_limit_local_base); + + /* Military Modifier Effects */ + ModifierEffect const* PROPERTY(attack_leader); + ModifierEffect const* PROPERTY(attrition_leader); + ModifierEffect const* PROPERTY(defence_leader); + ModifierEffect const* PROPERTY(experience); + ModifierEffect const* PROPERTY(morale_leader); + ModifierEffect const* PROPERTY(organisation); + ModifierEffect const* PROPERTY(reconnaissance); + ModifierEffect const* PROPERTY(reliability); + ModifierEffect const* PROPERTY(speed); + + /* BuildingType Effects */ + public: + struct building_type_effects_t { + friend struct BuildingTypeManager; + + private: + ModifierEffect const* PROPERTY(min_level); + ModifierEffect const* PROPERTY(max_level); + + public: + building_type_effects_t(); + }; + + private: + IndexedMap<BuildingType, building_type_effects_t> PROPERTY(building_type_effects); + + /* GoodDefinition Effects */ + public: + struct good_effects_t { + friend struct GoodDefinitionManager; + + private: + ModifierEffect const* PROPERTY(artisan_goods_input); + ModifierEffect const* PROPERTY(artisan_goods_output); + ModifierEffect const* PROPERTY(artisan_goods_throughput); + ModifierEffect const* PROPERTY(factory_goods_input); + ModifierEffect const* PROPERTY(factory_goods_output); + ModifierEffect const* PROPERTY(factory_goods_throughput); + ModifierEffect const* PROPERTY(rgo_goods_output); + ModifierEffect const* PROPERTY(rgo_goods_throughput); + ModifierEffect const* PROPERTY(rgo_size); + + public: + good_effects_t(); + }; + + private: + IndexedMap<GoodDefinition, good_effects_t> PROPERTY(good_effects); + + /* UnitType Effects */ + public: + struct unit_type_effects_t { + friend struct UnitTypeManager; + + private: + ModifierEffect const* PROPERTY(attack); + ModifierEffect const* PROPERTY(defence); + ModifierEffect const* PROPERTY(default_organisation); + ModifierEffect const* PROPERTY(maximum_speed); + ModifierEffect const* PROPERTY(build_time); + ModifierEffect const* PROPERTY(supply_consumption); + + protected: + unit_type_effects_t(); + }; + + struct regiment_type_effects_t : unit_type_effects_t { + friend struct UnitTypeManager; + + private: + ModifierEffect const* PROPERTY(reconnaissance); + ModifierEffect const* PROPERTY(discipline); + ModifierEffect const* PROPERTY(support); + ModifierEffect const* PROPERTY(maneuver); + ModifierEffect const* PROPERTY(siege); + + public: + regiment_type_effects_t(); + }; + + struct ship_type_effects_t : unit_type_effects_t { + friend struct UnitTypeManager; + + private: + ModifierEffect const* PROPERTY(colonial_points); + ModifierEffect const* PROPERTY(supply_consumption_score); + ModifierEffect const* PROPERTY(hull); + ModifierEffect const* PROPERTY(gun_power); + ModifierEffect const* PROPERTY(fire_range); + ModifierEffect const* PROPERTY(evasion); + ModifierEffect const* PROPERTY(torpedo_attack); + + public: + ship_type_effects_t(); + }; + + private: + regiment_type_effects_t PROPERTY(army_base_effects); + IndexedMap<RegimentType, regiment_type_effects_t> PROPERTY(regiment_type_effects); + ship_type_effects_t PROPERTY(navy_base_effects); + IndexedMap<ShipType, ship_type_effects_t> PROPERTY(ship_type_effects); + + /* Unit terrain Effects */ + public: + struct unit_terrain_effects_t { + friend struct TerrainTypeManager; + + private: + ModifierEffect const* PROPERTY(attack); + ModifierEffect const* PROPERTY(defence); + ModifierEffect const* PROPERTY(attrition); + ModifierEffect const* PROPERTY(movement); + + public: + unit_terrain_effects_t(); + }; + + private: + IndexedMap<TerrainType, unit_terrain_effects_t> PROPERTY(unit_terrain_effects); + + /* Rebel Effects */ + ModifierEffect const* PROPERTY(rebel_org_gain_all); + IndexedMap<RebelType, ModifierEffect const*> PROPERTY(rebel_org_gain_effects); + + /* Pop Effects */ + public: + struct strata_effects_t { + friend struct PopManager; + + private: + ModifierEffect const* PROPERTY(income_modifier); + ModifierEffect const* PROPERTY(vote); + ModifierEffect const* PROPERTY(life_needs); + ModifierEffect const* PROPERTY(everyday_needs); + ModifierEffect const* PROPERTY(luxury_needs); + + public: + strata_effects_t(); + }; + + private: + IndexedMap<Strata, strata_effects_t> PROPERTY(strata_effects); + + /* Technology Effects */ + IndexedMap<TechnologyFolder, ModifierEffect const*> PROPERTY(research_bonus_effects); + + ModifierEffectCache(); + + public: + ModifierEffectCache(ModifierEffectCache&&) = default; + }; +} diff --git a/src/openvic-simulation/modifier/ModifierManager.cpp b/src/openvic-simulation/modifier/ModifierManager.cpp new file mode 100644 index 0000000..a5ef8a8 --- /dev/null +++ b/src/openvic-simulation/modifier/ModifierManager.cpp @@ -0,0 +1,790 @@ +#include "ModifierManager.hpp" + +#include <utility> + +#include "openvic-simulation/dataloader/NodeTools.hpp" +#include "openvic-simulation/modifier/Modifier.hpp" +#include "openvic-simulation/modifier/ModifierEffect.hpp" + +using namespace OpenVic; +using namespace OpenVic::NodeTools; +using namespace OpenVic::StringUtils; + +using enum ModifierEffect::target_t; + +void ModifierManager::lock_all_modifier_except_base_country_effects() { + lock_leader_modifier_effects(); + lock_unit_terrain_modifier_effects(); + lock_shared_tech_country_modifier_effects(); + lock_technology_modifier_effects(); + lock_base_province_modifier_effects(); + lock_terrain_modifier_effects(); +} + +bool ModifierManager::_register_modifier_effect( + modifier_effect_registry_t& registry, + ModifierEffect::target_t targets, + ModifierEffect const*& effect_cache, + const std::string_view identifier, + const bool is_positive_good, + const ModifierEffect::format_t format, + const std::string_view localisation_key, + const bool has_no_effect +) { + using enum ModifierEffect::target_t; + + if (identifier.empty()) { + Logger::error("Invalid modifier effect identifier - empty!"); + return false; + } + + if (targets == NO_TARGETS) { + Logger::error("Invalid targets for modifier effect \"", identifier, "\" - none!"); + return false; + } + + if (!NumberUtils::is_power_of_two(static_cast<uint64_t>(targets))) { + Logger::error( + "Invalid targets for modifier effect \"", identifier, "\" - ", ModifierEffect::target_to_string(targets), + " (can only contain one target)" + ); + return false; + } + + if (effect_cache != nullptr) { + Logger::error( + "Cache variable for modifier effect \"", identifier, "\" is already filled with modifier effect \"", + effect_cache->get_identifier(), "\"" + ); + return false; + } + + const bool ret = registry.add_item({ std::move(identifier), is_positive_good, format, targets, localisation_key, has_no_effect }); + + if (ret) { + effect_cache = ®istry.get_items().back(); + } + + return ret; +} + +#define REGISTER_MODIFIER_EFFECT(MAPPING_TYPE, TARGETS) \ +bool ModifierManager::register_##MAPPING_TYPE##_modifier_effect( \ + ModifierEffect const*& effect_cache, \ + const std::string_view identifier, \ + const bool is_positive_good, \ + const ModifierEffect::format_t format, \ + const std::string_view localisation_key, \ + const bool has_no_effect \ +) { \ + return _register_modifier_effect( \ + MAPPING_TYPE##_modifier_effects, \ + TARGETS, \ + effect_cache, \ + std::move(identifier), \ + is_positive_good, \ + format, \ + localisation_key, \ + has_no_effect \ + ); \ +} + +REGISTER_MODIFIER_EFFECT(leader, UNIT) +REGISTER_MODIFIER_EFFECT(unit_terrain, COUNTRY) +REGISTER_MODIFIER_EFFECT(shared_tech_country, COUNTRY) +REGISTER_MODIFIER_EFFECT(technology, COUNTRY) +REGISTER_MODIFIER_EFFECT(base_country, COUNTRY) +REGISTER_MODIFIER_EFFECT(base_province, PROVINCE) +REGISTER_MODIFIER_EFFECT(terrain, PROVINCE) + +#undef REGISTER_MODIFIER_EFFECT + +bool ModifierManager::setup_modifier_effects() { + constexpr bool has_no_effect = true; + bool ret = true; + + using enum ModifierEffect::format_t; + + /* Tech/inventions only */ + ret &= register_technology_modifier_effect( + modifier_effect_cache.cb_creation_speed, "cb_creation_speed", true, PROPORTION_DECIMAL, "CB_MANUFACTURE_TECH" + ); + // When applied to countries (army tech/inventions), combat_width is an additive integer value. + ret &= register_technology_modifier_effect( + modifier_effect_cache.combat_width_additive, "combat_width", false, INT + ); + ret &= register_technology_modifier_effect( + modifier_effect_cache.plurality, "plurality", true, PERCENTAGE_DECIMAL, "TECH_PLURALITY" + ); + ret &= register_technology_modifier_effect( + modifier_effect_cache.pop_growth, "pop_growth", true, PROPORTION_DECIMAL, "TECH_POP_GROWTH" + ); + ret &= register_technology_modifier_effect( + modifier_effect_cache.prestige_gain_multiplier, "prestige", true, PROPORTION_DECIMAL, + "PRESTIGE_MODIFIER_TECH" + ); + ret &= register_technology_modifier_effect( + modifier_effect_cache.regular_experience_level, "regular_experience_level", true, RAW_DECIMAL, + "REGULAR_EXP_TECH" + ); + ret &= register_technology_modifier_effect( + modifier_effect_cache.reinforce_rate, "reinforce_rate", true, PROPORTION_DECIMAL, "REINFORCE_TECH" + ); + ret &= register_technology_modifier_effect( + modifier_effect_cache.separatism, "seperatism", // paradox typo + false, PROPORTION_DECIMAL, "SEPARATISM_TECH" + ); + ret &= register_technology_modifier_effect( + modifier_effect_cache.shared_prestige, "shared_prestige", true, RAW_DECIMAL, "SHARED_PRESTIGE_TECH" + ); + ret &= register_technology_modifier_effect(modifier_effect_cache.tax_eff, "tax_eff", true, PERCENTAGE_DECIMAL, "TECH_TAX_EFF"); + + /* Country Modifier Effects */ + ret &= register_technology_modifier_effect( + modifier_effect_cache.administrative_efficiency, "administrative_efficiency", true, PROPORTION_DECIMAL + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.administrative_efficiency_modifier, "administrative_efficiency_modifier", true, + PROPORTION_DECIMAL, ModifierEffect::make_default_modifier_effect_localisation_key("administrative_efficiency") + ); + ret &= register_technology_modifier_effect(modifier_effect_cache.artisan_input, "artisan_input", false, PROPORTION_DECIMAL,{},has_no_effect); + ret &= register_technology_modifier_effect(modifier_effect_cache.artisan_output, "artisan_output", true, PROPORTION_DECIMAL,{},has_no_effect); + ret &= register_technology_modifier_effect( modifier_effect_cache.artisan_throughput, "artisan_throughput", true, PROPORTION_DECIMAL,{},has_no_effect); + ret &= register_base_country_modifier_effect(modifier_effect_cache.badboy, "badboy", false, RAW_DECIMAL); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.cb_generation_speed_modifier, "cb_generation_speed_modifier", true, PROPORTION_DECIMAL + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.civilization_progress_modifier, "civilization_progress_modifier", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("civilization_progress") + ); + ret &= register_technology_modifier_effect( + modifier_effect_cache.colonial_life_rating, "colonial_life_rating", false, INT, "COLONIAL_LIFE_TECH" + ); + ret &= register_technology_modifier_effect( + modifier_effect_cache.colonial_migration, "colonial_migration", true, PROPORTION_DECIMAL, + "COLONIAL_MIGRATION_TECH" + ); + ret &= register_technology_modifier_effect( + modifier_effect_cache.colonial_points, "colonial_points", true, INT, "COLONIAL_POINTS_TECH" + ); + ret &= register_technology_modifier_effect( + modifier_effect_cache.colonial_prestige, "colonial_prestige", true, PROPORTION_DECIMAL, + "COLONIAL_PRESTIGE_MODIFIER_TECH" + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.core_pop_consciousness_modifier, "core_pop_consciousness_modifier", false, RAW_DECIMAL + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.core_pop_militancy_modifier, "core_pop_militancy_modifier", false, RAW_DECIMAL + ); + ret &= register_technology_modifier_effect(modifier_effect_cache.dig_in_cap, "dig_in_cap", true, INT, "DIGIN_FROM_TECH"); + ret &= register_technology_modifier_effect( + modifier_effect_cache.diplomatic_points, "diplomatic_points", true, PROPORTION_DECIMAL, + "DIPLOMATIC_POINTS_TECH" + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.diplomatic_points_modifier, "diplomatic_points_modifier", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("diplopoints_gain") + ); + ret &= register_technology_modifier_effect( + modifier_effect_cache.education_efficiency, "education_efficiency", true, PROPORTION_DECIMAL + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.education_efficiency_modifier, "education_efficiency_modifier", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("education_efficiency") + ); + ret &= register_shared_tech_country_modifier_effect(modifier_effect_cache.factory_cost, "factory_cost", false, PROPORTION_DECIMAL); + ret &= register_shared_tech_country_modifier_effect(modifier_effect_cache.factory_input, "factory_input", false, PROPORTION_DECIMAL); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.factory_maintenance, "factory_maintenance", false, PROPORTION_DECIMAL + ); + ret &= register_shared_tech_country_modifier_effect(modifier_effect_cache.factory_output, "factory_output", true, PROPORTION_DECIMAL); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.factory_owner_cost, "factory_owner_cost", false, PROPORTION_DECIMAL + ); + ret &= register_shared_tech_country_modifier_effect( + modifier_effect_cache.factory_throughput, "factory_throughput", true, PROPORTION_DECIMAL + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.global_assimilation_rate, "global_assimilation_rate", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("assimilation_rate") + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.global_immigrant_attract, "global_immigrant_attract", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("immigant_attract") + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.global_pop_consciousness_modifier, "global_pop_consciousness_modifier", false, RAW_DECIMAL + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.global_pop_militancy_modifier, "global_pop_militancy_modifier", false, RAW_DECIMAL + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.global_population_growth, "global_population_growth", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("population_growth") + ); + ret &= register_base_country_modifier_effect(modifier_effect_cache.goods_demand, "goods_demand", false, PROPORTION_DECIMAL); + ret &= register_base_country_modifier_effect(modifier_effect_cache.import_cost, "import_cost", false, PROPORTION_DECIMAL,{},has_no_effect); + ret &= register_technology_modifier_effect( + modifier_effect_cache.increase_research, "increase_research", true, PROPORTION_DECIMAL, "INC_RES_TECH" + ); + ret &= register_technology_modifier_effect( + modifier_effect_cache.influence, "influence", true, PROPORTION_DECIMAL, "TECH_GP_INFLUENCE" + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.influence_modifier, "influence_modifier", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("greatpower_influence_gain") + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.issue_change_speed, "issue_change_speed", true, PROPORTION_DECIMAL + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.land_attack_modifier, "land_attack_modifier", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("land_attack") + ); + ret &= register_technology_modifier_effect( + modifier_effect_cache.land_attrition, "land_attrition", false, PROPORTION_DECIMAL, "LAND_ATTRITION_TECH" + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.land_defense_modifier, "land_defense_modifier", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("land_defense") + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.land_organisation, "land_organisation", true, PROPORTION_DECIMAL + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.land_unit_start_experience, "land_unit_start_experience", true, RAW_DECIMAL + ); + ret &= register_base_country_modifier_effect(modifier_effect_cache.leadership, "leadership", true, RAW_DECIMAL, "LEADERSHIP"); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.leadership_modifier, "leadership_modifier", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("global_leadership_modifier") + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.literacy_con_impact, "literacy_con_impact", false, PROPORTION_DECIMAL + ); + ret &= register_technology_modifier_effect(modifier_effect_cache.loan_interest_base, "loan_interest", false, PROPORTION_DECIMAL); + ret &= register_base_country_modifier_effect(modifier_effect_cache.loan_interest_foreign, "loan_interest", false, PROPORTION_DECIMAL); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.max_loan_modifier, "max_loan_modifier", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("max_loan_amount") + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.max_military_spending, "max_military_spending", true, PROPORTION_DECIMAL + ); + ret &= register_technology_modifier_effect( + modifier_effect_cache.max_national_focus, "max_national_focus", true, INT, "TECH_MAX_FOCUS" + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.max_social_spending, "max_social_spending", true, PROPORTION_DECIMAL + ); + ret &= register_base_country_modifier_effect(modifier_effect_cache.max_tariff, "max_tariff", true, PROPORTION_DECIMAL); + ret &= register_base_country_modifier_effect(modifier_effect_cache.max_tax, "max_tax", true, PROPORTION_DECIMAL); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.max_war_exhaustion, "max_war_exhaustion", true, PERCENTAGE_DECIMAL, "MAX_WAR_EXHAUSTION" + ); + ret &= register_technology_modifier_effect( + modifier_effect_cache.military_tactics, "military_tactics", true, PROPORTION_DECIMAL, "MIL_TACTICS_TECH" + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.min_military_spending, "min_military_spending", true, PROPORTION_DECIMAL + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.min_social_spending, "min_social_spending", true, PROPORTION_DECIMAL + ); + ret &= register_base_country_modifier_effect(modifier_effect_cache.min_tariff, "min_tariff", true, PROPORTION_DECIMAL); + ret &= register_base_country_modifier_effect(modifier_effect_cache.min_tax, "min_tax", true, PROPORTION_DECIMAL); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.minimum_wage, "minimum_wage", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("minimun_wage") + ); + ret &= register_shared_tech_country_modifier_effect( + modifier_effect_cache.mobilisation_economy_impact, "mobilisation_economy_impact", false, PROPORTION_DECIMAL + ); + ret &= register_shared_tech_country_modifier_effect( + modifier_effect_cache.mobilisation_size, "mobilisation_size", true, PROPORTION_DECIMAL + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.mobilization_impact, "mobilization_impact", false, PROPORTION_DECIMAL + ); + ret &= register_technology_modifier_effect(modifier_effect_cache.morale_global, "morale", true, PROPORTION_DECIMAL, "MORALE_TECH"); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.naval_attack_modifier, "naval_attack_modifier", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("naval_attack") + ); + ret &= register_technology_modifier_effect( + modifier_effect_cache.naval_attrition, "naval_attrition", false, PROPORTION_DECIMAL, "NAVAL_ATTRITION_TECH" + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.naval_defense_modifier, "naval_defense_modifier", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("naval_defense") + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.naval_organisation, "naval_organisation", true, PROPORTION_DECIMAL + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.naval_unit_start_experience, "naval_unit_start_experience", true, RAW_DECIMAL + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.non_accepted_pop_consciousness_modifier, "non_accepted_pop_consciousness_modifier", false, + RAW_DECIMAL + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.non_accepted_pop_militancy_modifier, "non_accepted_pop_militancy_modifier", false, RAW_DECIMAL + ); + ret &= register_base_country_modifier_effect(modifier_effect_cache.org_regain, "org_regain", true, PROPORTION_DECIMAL); + ret &= register_base_country_modifier_effect(modifier_effect_cache.pension_level, "pension_level", true, PROPORTION_DECIMAL); + ret &= register_technology_modifier_effect( + modifier_effect_cache.permanent_prestige, "permanent_prestige", true, RAW_DECIMAL, "PERMANENT_PRESTIGE_TECH" + ); + ret &= register_shared_tech_country_modifier_effect( + modifier_effect_cache.political_reform_desire, "political_reform_desire", false, PROPORTION_DECIMAL + ); + + ret &= register_technology_modifier_effect( modifier_effect_cache.poor_savings_modifier, "poor_savings_modifier", true, PROPORTION_DECIMAL,{},has_no_effect); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.prestige_monthly_gain, "prestige", true, RAW_DECIMAL + ); + ret &= register_base_country_modifier_effect(modifier_effect_cache.reinforce_speed, "reinforce_speed", true, PROPORTION_DECIMAL); + ret &= register_base_country_modifier_effect(modifier_effect_cache.research_points, "research_points", true, RAW_DECIMAL); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.research_points_modifier, "research_points_modifier", true, PROPORTION_DECIMAL + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.research_points_on_conquer, "research_points_on_conquer", true, PROPORTION_DECIMAL + ); + ret &= register_shared_tech_country_modifier_effect(modifier_effect_cache.rgo_output, "rgo_output", true, PROPORTION_DECIMAL); + ret &= register_shared_tech_country_modifier_effect(modifier_effect_cache.rgo_throughput, "rgo_throughput", true, PROPORTION_DECIMAL); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.ruling_party_support, "ruling_party_support", true, PROPORTION_DECIMAL + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.self_unciv_economic_modifier, "self_unciv_economic_modifier", false, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("self_unciv_economic") + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.self_unciv_military_modifier, "self_unciv_military_modifier", false, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("self_unciv_military") + ); + ret &= register_shared_tech_country_modifier_effect( + modifier_effect_cache.social_reform_desire, "social_reform_desire", false, PROPORTION_DECIMAL + ); + ret &= register_technology_modifier_effect( + modifier_effect_cache.soldier_to_pop_loss, "soldier_to_pop_loss", true, PROPORTION_DECIMAL, + "SOLDIER_TO_POP_LOSS_TECH" + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.supply_consumption, "supply_consumption", false, PROPORTION_DECIMAL + ); + ret &= register_technology_modifier_effect( + modifier_effect_cache.supply_range, "supply_range", true, PROPORTION_DECIMAL, "SUPPLY_RANGE_TECH" + ); + ret &= register_shared_tech_country_modifier_effect( + modifier_effect_cache.suppression_points_modifier, "suppression_points_modifier", true, PROPORTION_DECIMAL, + "SUPPRESSION_TECH" + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.tariff_efficiency_modifier, "tariff_efficiency_modifier", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("tariff_efficiency") + ); + ret &= register_base_country_modifier_effect(modifier_effect_cache.tax_efficiency, "tax_efficiency", true, PROPORTION_DECIMAL); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.unemployment_benefit, "unemployment_benefit", true, PROPORTION_DECIMAL + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.unciv_economic_modifier, "unciv_economic_modifier", false, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("unciv_economic"), has_no_effect + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.unciv_military_modifier, "unciv_military_modifier", false, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("unciv_military"), has_no_effect + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.unit_recruitment_time, "unit_recruitment_time", false, PROPORTION_DECIMAL + ); + ret &= register_shared_tech_country_modifier_effect( + modifier_effect_cache.war_exhaustion, "war_exhaustion", false, PROPORTION_DECIMAL, "WAR_EXHAUST_BATTLES" + ); + + /* Province Modifier Effects */ + ret &= register_base_province_modifier_effect( + modifier_effect_cache.assimilation_rate, "assimilation_rate", true, PROPORTION_DECIMAL + ); + ret &= register_base_province_modifier_effect( + modifier_effect_cache.boost_strongest_party, "boost_strongest_party", false, PROPORTION_DECIMAL,{},has_no_effect + ); + // When applied to provinces (terrain), combat_width is a multiplicative proportional decimal value. + ret &= register_base_province_modifier_effect( + modifier_effect_cache.combat_width_percentage_change, "combat_width", false, PROPORTION_DECIMAL + ); + ret &= register_terrain_modifier_effect(modifier_effect_cache.defence_terrain, "defence", true, INT, "TRAIT_DEFEND"); + ret &= register_technology_modifier_effect( + modifier_effect_cache.farm_rgo_throughput_global, "farm_rgo_eff", true, PROPORTION_DECIMAL, "TECH_FARM_OUTPUT" + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.farm_rgo_output_global, "farm_rgo_eff", true, PROPORTION_DECIMAL, "TECH_FARM_OUTPUT" + ); + ret &= register_base_province_modifier_effect( + modifier_effect_cache.farm_rgo_output_local, "farm_rgo_eff", true, PROPORTION_DECIMAL, "TECH_FARM_OUTPUT" + ); + ret &= register_shared_tech_country_modifier_effect( + modifier_effect_cache.farm_rgo_size_global, "farm_rgo_size", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("farm_size") + ); + ret &= register_base_province_modifier_effect( + modifier_effect_cache.farm_rgo_size_local, "farm_rgo_size", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("farm_size") + ); + ret &= register_base_province_modifier_effect( + modifier_effect_cache.immigrant_attract, "immigrant_attract", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("immigant_attract") + ); + ret &= register_base_province_modifier_effect( + modifier_effect_cache.immigrant_push, "immigrant_push", false, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("immigant_push") + ); + ret &= register_base_province_modifier_effect(modifier_effect_cache.life_rating, "life_rating", true, PROPORTION_DECIMAL); + ret &= register_base_province_modifier_effect( + modifier_effect_cache.local_artisan_input, "local_artisan_input", false, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("artisan_input"), + has_no_effect + ); + ret &= register_base_province_modifier_effect( + modifier_effect_cache.local_artisan_output, "local_artisan_output", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("artisan_output"), + has_no_effect + ); + ret &= register_base_province_modifier_effect( + modifier_effect_cache.local_artisan_throughput, "local_artisan_throughput", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("artisan_throughput"), + has_no_effect + ); + ret &= register_base_province_modifier_effect( + modifier_effect_cache.local_factory_input, "local_factory_input", false, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("factory_input") + ); + ret &= register_base_province_modifier_effect( + modifier_effect_cache.local_factory_output, "local_factory_output", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("factory_output") + ); + ret &= register_base_province_modifier_effect( + modifier_effect_cache.local_factory_throughput, "local_factory_throughput", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("factory_throughput") + ); + ret &= register_base_province_modifier_effect(modifier_effect_cache.local_repair, "local_repair", true, PROPORTION_DECIMAL); + ret &= register_base_province_modifier_effect( + modifier_effect_cache.local_rgo_output, "local_rgo_output", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("rgo_output") + ); + ret &= register_base_province_modifier_effect( + modifier_effect_cache.local_rgo_throughput, "local_rgo_throughput", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("rgo_throughput") + ); + ret &= register_base_province_modifier_effect( + modifier_effect_cache.local_ruling_party_support, "local_ruling_party_support", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("ruling_party_support") + ); + ret &= register_base_province_modifier_effect( + modifier_effect_cache.local_ship_build, "local_ship_build", false, PROPORTION_DECIMAL + ); + ret &= register_terrain_modifier_effect(modifier_effect_cache.attrition_local, "attrition", false, PROPORTION_DECIMAL, "UA_ATTRITION"); + ret &= register_base_province_modifier_effect(modifier_effect_cache.max_attrition, "max_attrition", false, RAW_DECIMAL); + ret &= register_technology_modifier_effect( + modifier_effect_cache.mine_rgo_throughput_global, "mine_rgo_eff", true, PROPORTION_DECIMAL, "TECH_MINE_OUTPUT" + ); + ret &= register_base_country_modifier_effect( + modifier_effect_cache.mine_rgo_output_global, "mine_rgo_eff", true, PROPORTION_DECIMAL, "TECH_MINE_OUTPUT" + ); + ret &= register_base_province_modifier_effect( + modifier_effect_cache.mine_rgo_output_local, "mine_rgo_eff", true, PROPORTION_DECIMAL, "TECH_MINE_OUTPUT" + ); + ret &= register_shared_tech_country_modifier_effect( + modifier_effect_cache.mine_rgo_size_global, "mine_rgo_size", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("mine_size") + ); + ret &= register_base_province_modifier_effect( + modifier_effect_cache.mine_rgo_size_local, "mine_rgo_size", true, PROPORTION_DECIMAL, + ModifierEffect::make_default_modifier_effect_localisation_key("mine_size") + ); + ret &= register_terrain_modifier_effect( + modifier_effect_cache.movement_cost_base, "movement_cost", true, PROPORTION_DECIMAL + ); + ret &= register_base_province_modifier_effect( + modifier_effect_cache.movement_cost_percentage_change, "movement_cost", false, PROPORTION_DECIMAL + ); + ret &= register_base_province_modifier_effect( + modifier_effect_cache.number_of_voters, "number_of_voters", false, PROPORTION_DECIMAL + ); + ret &= register_base_province_modifier_effect( + modifier_effect_cache.pop_consciousness_modifier, "pop_consciousness_modifier", false, RAW_DECIMAL + ); + ret &= register_base_province_modifier_effect( + modifier_effect_cache.pop_militancy_modifier, "pop_militancy_modifier", false, RAW_DECIMAL + ); + ret &= register_base_province_modifier_effect( + modifier_effect_cache.population_growth, "population_growth", true, PROPORTION_DECIMAL + ); + ret &= register_technology_modifier_effect(modifier_effect_cache.supply_limit_global_percentage_change, "supply_limit", true, RAW_DECIMAL); + ret &= register_base_country_modifier_effect(modifier_effect_cache.supply_limit_global_base, "supply_limit", true, RAW_DECIMAL); + ret &= register_base_province_modifier_effect(modifier_effect_cache.supply_limit_local_base, "supply_limit", true, RAW_DECIMAL); + + /* Military Modifier Effects */ + ret &= register_leader_modifier_effect(modifier_effect_cache.attack_leader, "attack", true, INT, "TRAIT_ATTACK"); + ret &= register_leader_modifier_effect(modifier_effect_cache.attrition_leader, "attrition", false, RAW_DECIMAL, "ATTRITION"); + ret &= register_leader_modifier_effect(modifier_effect_cache.defence_leader, "defence", true, INT, "TRAIT_DEFEND"); + ret &= register_leader_modifier_effect( + modifier_effect_cache.experience, "experience", true, PROPORTION_DECIMAL, "TRAIT_EXPERIENCE" + ); + ret &= register_leader_modifier_effect(modifier_effect_cache.morale_leader, "morale", true, PROPORTION_DECIMAL, "TRAIT_MORALE"); + ret &= register_leader_modifier_effect( + modifier_effect_cache.organisation, "organisation", true, PROPORTION_DECIMAL, "TRAIT_ORGANISATION" + ); + ret &= register_leader_modifier_effect( + modifier_effect_cache.reconnaissance, "reconnaissance", true, PROPORTION_DECIMAL, "TRAIT_RECONAISSANCE" + ); + ret &= register_leader_modifier_effect(modifier_effect_cache.reliability, "reliability", true, RAW_DECIMAL, "TRAIT_RELIABILITY"); + ret &= register_leader_modifier_effect(modifier_effect_cache.speed, "speed", true, PROPORTION_DECIMAL, "TRAIT_SPEED"); + + return ret; +} + +bool ModifierManager::register_complex_modifier(const std::string_view identifier) { + if (complex_modifiers.emplace(identifier).second) { + return true; + } else { + Logger::error("Duplicate complex modifier: ", identifier); + return false; + } +} + +std::string ModifierManager::get_flat_identifier( + const std::string_view complex_modifier_identifier, + const std::string_view variant_identifier +) { + return StringUtils::append_string_views(complex_modifier_identifier, " ", variant_identifier); +} + +bool ModifierManager::add_event_modifier( + const std::string_view identifier, + ModifierValue&& values, + const IconModifier::icon_t icon, + const Modifier::modifier_type_t type +) { + + if (identifier.empty()) { + Logger::error("Invalid event modifier effect identifier - empty!"); + return false; + } + + return event_modifiers.add_item( + { identifier, std::move(values), type, icon }, duplicate_warning_callback + ); +} + +bool ModifierManager::load_event_modifiers(const ast::NodeCPtr root) { + const bool ret = expect_dictionary_reserve_length( + event_modifiers, + [this](std::string_view key, ast::NodeCPtr value) -> bool { + ModifierValue modifier_value; + IconModifier::icon_t icon = 0; + + bool ret = expect_dictionary_keys_and_default( + expect_base_province_modifier(modifier_value), + "icon", ZERO_OR_ONE, expect_uint(assign_variable_callback(icon)) + )(value); + + ret &= add_event_modifier(key, std::move(modifier_value), icon); + + return ret; + } + )(root); + + return ret; +} + +bool ModifierManager::load_static_modifiers(const ast::NodeCPtr root) { + return static_modifier_cache.load_static_modifiers(*this, root); +} + +bool ModifierManager::add_triggered_modifier( + const std::string_view identifier, + ModifierValue&& values, + const IconModifier::icon_t icon, + ConditionScript&& trigger +) { + using enum Modifier::modifier_type_t; + + if (identifier.empty()) { + Logger::error("Invalid triggered modifier effect identifier - empty!"); + return false; + } + + return triggered_modifiers.add_item( + { identifier, std::move(values), TRIGGERED, icon, std::move(trigger) }, + duplicate_warning_callback + ); +} + +bool ModifierManager::load_triggered_modifiers(const ast::NodeCPtr root) { + const bool ret = expect_dictionary_reserve_length( + triggered_modifiers, + [this](const std::string_view key, const ast::NodeCPtr value) -> bool { + ModifierValue modifier_value {}; + IconModifier::icon_t icon = 0; + ConditionScript trigger { scope_t::COUNTRY, scope_t::COUNTRY, scope_t::NO_SCOPE }; + + bool ret = expect_dictionary_keys_and_default( + expect_base_country_modifier(modifier_value), + "icon", ZERO_OR_ONE, expect_uint(assign_variable_callback(icon)), + "trigger", ONE_EXACTLY, trigger.expect_script() + )(value); + + ret &= add_triggered_modifier(key, std::move(modifier_value), icon, std::move(trigger)); + + return ret; + } + )(root); + + lock_triggered_modifiers(); + + return ret; +} + +bool ModifierManager::parse_scripts(DefinitionManager const& definition_manager) { + bool ret = true; + + for (TriggeredModifier& modifier : triggered_modifiers.get_items()) { + ret &= modifier.parse_scripts(definition_manager); + } + + return ret; +} + +bool ModifierManager::_add_flattened_modifier_cb( + ModifierValue& modifier_value, + const std::string_view prefix, + const std::string_view key, + const ast::NodeCPtr value +) const { + const std::string flat_identifier = get_flat_identifier(prefix, key); + ModifierEffect const* effect = technology_modifier_effects.get_item_by_identifier(flat_identifier); + if (effect != nullptr) { + return _add_modifier_cb(modifier_value, effect, value); + } else { + Logger::error("Could not find flattened modifier: ", flat_identifier); + return false; + } +}; + +bool ModifierManager::_add_modifier_cb( + ModifierValue& modifier_value, + ModifierEffect const* const effect, + const ast::NodeCPtr value +) const { + if (effect->has_no_effect()) { + Logger::warning("This modifier does nothing: ", effect->get_identifier()); + } + return expect_fixed_point(map_callback(modifier_value.values, effect))(value); +} + +NodeTools::key_value_callback_t ModifierManager::_expect_modifier_effect( + modifier_effect_registry_t const& registry, + ModifierValue& modifier_value +) const { + return _expect_modifier_effect_with_fallback(registry, modifier_value, key_value_invalid_callback); +} +NodeTools::key_value_callback_t ModifierManager::_expect_modifier_effect_with_fallback( + modifier_effect_registry_t const& registry, + ModifierValue& modifier_value, + const NodeTools::key_value_callback_t fallback +) const { + return [this, ®istry, &modifier_value, fallback](const std::string_view key, const ast::NodeCPtr value) -> bool { + if (dryad::node_has_kind<ast::ListValue>(value) && complex_modifiers.contains(key)) { + return expect_dictionary([this, &modifier_value, key]( + const std::string_view inner_key, const ast::NodeCPtr inner_value + ) -> bool { + return _add_flattened_modifier_cb(modifier_value, key, inner_key, inner_value); + })(value); + } + + ModifierEffect const* effect = registry.get_item_by_identifier(key); + if (effect == nullptr) { + return fallback(key, value); + } + return _add_modifier_cb(modifier_value, effect, value); + }; +} +NodeTools::key_value_callback_t ModifierManager::_expect_shared_tech_country_modifier_effect(ModifierValue& modifier_value) const { + return _expect_modifier_effect(shared_tech_country_modifier_effects, modifier_value); +} +NodeTools::key_value_callback_t ModifierManager::expect_leader_modifier(ModifierValue& modifier_value) const { + return _expect_modifier_effect(leader_modifier_effects, modifier_value); +} +NodeTools::key_value_callback_t ModifierManager::expect_technology_modifier(ModifierValue& modifier_value) const { + return [this, &modifier_value](const std::string_view key, const ast::NodeCPtr value) { + if (strings_equal_case_insensitive(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(modifier_value, key, faction_identifier, value_node); + + return ret; + } + + if (strings_equal_case_insensitive(key, "farm_rgo_eff")) { + return _add_modifier_cb(modifier_value, modifier_effect_cache.farm_rgo_throughput_global, value) + && _add_modifier_cb(modifier_value, modifier_effect_cache.farm_rgo_output_global, value); + } + + if (strings_equal_case_insensitive(key, "mine_rgo_eff")) { + return _add_modifier_cb(modifier_value, modifier_effect_cache.mine_rgo_throughput_global, value) + && _add_modifier_cb(modifier_value, modifier_effect_cache.mine_rgo_output_global, value); + } + + return _expect_modifier_effect_with_fallback( + technology_modifier_effects, + modifier_value, + _expect_shared_tech_country_modifier_effect(modifier_value) + )(key, value); + }; +} +NodeTools::key_value_callback_t ModifierManager::expect_unit_terrain_modifier( + ModifierValue& modifier_value, + const std::string_view terrain_type_identifier +) const { + return [this, &modifier_value, terrain_type_identifier](const std::string_view key, const ast::NodeCPtr value) -> bool { + const std::string flat_identifier = get_flat_identifier(key, terrain_type_identifier); + ModifierEffect const* effect = unit_terrain_modifier_effects.get_item_by_identifier(flat_identifier); + if (effect == nullptr) { + return key_value_invalid_callback(flat_identifier, value); + } + return _add_modifier_cb(modifier_value, effect, value); + }; +} +NodeTools::key_value_callback_t ModifierManager::expect_base_country_modifier(ModifierValue& modifier_value) const { + return _expect_modifier_effect_with_fallback( + base_country_modifier_effects, + modifier_value, + _expect_shared_tech_country_modifier_effect(modifier_value) + ); +} +NodeTools::key_value_callback_t ModifierManager::expect_base_province_modifier(ModifierValue& modifier_value) const { + return _expect_modifier_effect_with_fallback( + base_province_modifier_effects, + modifier_value, + expect_base_country_modifier(modifier_value) + ); +} +NodeTools::key_value_callback_t ModifierManager::expect_terrain_modifier(ModifierValue& modifier_value) const { + return _expect_modifier_effect_with_fallback( + terrain_modifier_effects, + modifier_value, + expect_base_province_modifier(modifier_value) + ); +}
\ No newline at end of file diff --git a/src/openvic-simulation/modifier/ModifierManager.hpp b/src/openvic-simulation/modifier/ModifierManager.hpp new file mode 100644 index 0000000..eb524a3 --- /dev/null +++ b/src/openvic-simulation/modifier/ModifierManager.hpp @@ -0,0 +1,136 @@ +#pragma once + +#include <string_view> + +#include "openvic-simulation/modifier/Modifier.hpp" +#include "openvic-simulation/modifier/ModifierEffectCache.hpp" +#include "openvic-simulation/modifier/StaticModifierCache.hpp" +#include "openvic-simulation/types/IdentifierRegistry.hpp" + +namespace OpenVic { + struct ModifierManager { + friend struct StaticModifierCache; + friend struct BuildingTypeManager; + friend struct GoodDefinitionManager; + friend struct UnitTypeManager; + friend struct RebelManager; + friend struct PopManager; + friend struct TechnologyManager; + friend struct TerrainTypeManager; + + using modifier_effect_registry_t = CaseInsensitiveIdentifierRegistry<ModifierEffect, RegistryStorageInfoDeque>; + + /* Some ModifierEffects are generated mid-load, such as max/min count modifiers for each building, so + * we can't lock it until loading is over. This means we can't rely on locking for pointer stability, + * so instead we store the effects in a deque which doesn't invalidate pointers on insert. + */ + private: + modifier_effect_registry_t IDENTIFIER_REGISTRY(leader_modifier_effect); + modifier_effect_registry_t IDENTIFIER_REGISTRY(unit_terrain_modifier_effect); + modifier_effect_registry_t IDENTIFIER_REGISTRY(shared_tech_country_modifier_effect); + modifier_effect_registry_t IDENTIFIER_REGISTRY(technology_modifier_effect); + modifier_effect_registry_t IDENTIFIER_REGISTRY(base_country_modifier_effect); + modifier_effect_registry_t IDENTIFIER_REGISTRY(base_province_modifier_effect); + modifier_effect_registry_t IDENTIFIER_REGISTRY(terrain_modifier_effect); + case_insensitive_string_set_t complex_modifiers; + + IdentifierRegistry<IconModifier> IDENTIFIER_REGISTRY(event_modifier); + IdentifierRegistry<TriggeredModifier> IDENTIFIER_REGISTRY(triggered_modifier); + + ModifierEffectCache PROPERTY(modifier_effect_cache); + StaticModifierCache PROPERTY(static_modifier_cache); + + bool _register_modifier_effect( + modifier_effect_registry_t& registry, + ModifierEffect::target_t targets, + ModifierEffect const*& effect_cache, + const std::string_view identifier, + const bool is_positive_good, + const ModifierEffect::format_t format, + const std::string_view localisation_key, + const bool has_no_effect + ); + + +#define REGISTER_MODIFIER_EFFECT_DEFINITION(MAPPING_TYPE) \ + bool register_##MAPPING_TYPE##_modifier_effect( \ + ModifierEffect const*& effect_cache, \ + std::string_view identifier, \ + bool is_positive_good, \ + ModifierEffect::format_t format, \ + std::string_view localisation_key = {}, \ + bool has_no_effect = false \ + ); + + REGISTER_MODIFIER_EFFECT_DEFINITION(leader) + REGISTER_MODIFIER_EFFECT_DEFINITION(unit_terrain) + REGISTER_MODIFIER_EFFECT_DEFINITION(shared_tech_country) + REGISTER_MODIFIER_EFFECT_DEFINITION(technology) + REGISTER_MODIFIER_EFFECT_DEFINITION(base_country) + REGISTER_MODIFIER_EFFECT_DEFINITION(base_province) + REGISTER_MODIFIER_EFFECT_DEFINITION(terrain) +#undef REGISTER_MODIFIER_EFFECT_DEFINITION + + bool _add_flattened_modifier_cb( + ModifierValue& modifier_value, + const std::string_view prefix, + const std::string_view key, + const ast::NodeCPtr value + ) const; + + bool _add_modifier_cb( + ModifierValue& modifier_value, + ModifierEffect const* const effect, + const ast::NodeCPtr value + ) const; + + NodeTools::key_value_callback_t _expect_modifier_effect( + modifier_effect_registry_t const& registry, + ModifierValue& modifier_value + ) const; + + NodeTools::key_value_callback_t _expect_modifier_effect_with_fallback( + modifier_effect_registry_t const& registry, + ModifierValue& modifier_value, + const NodeTools::key_value_callback_t fallback + ) const; + + NodeTools::key_value_callback_t _expect_shared_tech_country_modifier_effect(ModifierValue& modifier_value) const; + public: + bool register_complex_modifier(const std::string_view identifier); + static std::string get_flat_identifier(const std::string_view complex_modifier_identifier, const std::string_view variant_identifier); + + bool setup_modifier_effects(); + + bool add_event_modifier( + const std::string_view identifier, + ModifierValue&& values, + const IconModifier::icon_t icon, + const Modifier::modifier_type_t type = Modifier::modifier_type_t::EVENT + ); + bool load_event_modifiers(const ast::NodeCPtr root); + + bool load_static_modifiers(const ast::NodeCPtr root); + + bool add_triggered_modifier( + const std::string_view identifier, + ModifierValue&& values, + const IconModifier::icon_t icon, + ConditionScript&& trigger + ); + bool load_triggered_modifiers(const ast::NodeCPtr root); + + bool parse_scripts(DefinitionManager const& definition_manager); + void lock_all_modifier_except_base_country_effects(); + + NodeTools::key_value_callback_t expect_leader_modifier(ModifierValue& modifier_value) const; + NodeTools::key_value_callback_t expect_technology_modifier(ModifierValue& modifier_value) const; + NodeTools::key_value_callback_t expect_unit_terrain_modifier( + ModifierValue& modifier_value, + const std::string_view terrain_type_identifier + ) const; + NodeTools::key_value_callback_t expect_base_country_modifier(ModifierValue& modifier_value) const; + NodeTools::key_value_callback_t expect_base_province_modifier(ModifierValue& modifier_value) const; + NodeTools::key_value_callback_t expect_terrain_modifier(ModifierValue& modifier_value) const; + }; +} diff --git a/src/openvic-simulation/modifier/ModifierSum.cpp b/src/openvic-simulation/modifier/ModifierSum.cpp index 8e5ce48..676e872 100644 --- a/src/openvic-simulation/modifier/ModifierSum.cpp +++ b/src/openvic-simulation/modifier/ModifierSum.cpp @@ -1,7 +1,28 @@ #include "ModifierSum.hpp" +#include "openvic-simulation/modifier/Modifier.hpp" + +#include "openvic-simulation/country/CountryInstance.hpp" +#include "openvic-simulation/map/ProvinceInstance.hpp" + using namespace OpenVic; +std::string_view ModifierSum::source_to_string(modifier_source_t const& source) { + return std::visit( + [](HasGetIdentifier auto const* has_identifier) -> std::string_view { + return has_identifier->get_identifier(); + }, + source + ); +} + +std::string ModifierSum::modifier_entry_t::to_string() const { + return StringUtils::append_string_views( + "[", modifier->get_identifier(), ", ", multiplier.to_string(), ", ", source_to_string(source), ", ", + ModifierEffect::target_to_string(excluded_targets), "]" + ); +} + void ModifierSum::clear() { modifiers.clear(); value_sum.clear(); @@ -15,44 +36,88 @@ fixed_point_t ModifierSum::get_effect(ModifierEffect const& effect, bool* effect return value_sum.get_effect(effect, effect_found); } +fixed_point_t ModifierSum::get_effect_nullcheck(ModifierEffect const* effect, bool* effect_found) const { + return value_sum.get_effect_nullcheck(effect, effect_found); +} + bool ModifierSum::has_effect(ModifierEffect const& effect) const { return value_sum.has_effect(effect); } -void ModifierSum::add_modifier(Modifier const& modifier, fixed_point_t multiplier) { - modifiers[&modifier] += multiplier; - value_sum.multiply_add(modifier, multiplier); +void ModifierSum::add_modifier( + Modifier const& modifier, modifier_source_t const& source, fixed_point_t multiplier, + ModifierEffect::target_t excluded_targets +) { + using enum ModifierEffect::target_t; + + // We could test that excluded_targets != ALL_TARGETS, but in practice it's always + // called with an explcit/hardcoded value and so won't ever exclude everything. + if (multiplier != fixed_point_t::_0()) { + modifiers.emplace_back(&modifier, multiplier, source, excluded_targets); + value_sum.multiply_add_exclude_targets(modifier, multiplier, excluded_targets); + } +} + +void ModifierSum::add_modifier_nullcheck( + Modifier const* modifier, modifier_source_t const& source, fixed_point_t multiplier, + ModifierEffect::target_t excluded_targets +) { + if (modifier != nullptr) { + add_modifier(*modifier, source, multiplier, excluded_targets); + } } void ModifierSum::add_modifier_sum(ModifierSum const& modifier_sum) { - modifiers += modifier_sum.modifiers; + modifiers.insert(modifiers.end(), modifier_sum.modifiers.begin(), modifier_sum.modifiers.end()); value_sum += modifier_sum.value_sum; } -ModifierSum& ModifierSum::operator+=(Modifier const& modifier) { - add_modifier(modifier); - return *this; +void ModifierSum::add_modifier_sum_exclude_targets( + ModifierSum const& modifier_sum, ModifierEffect::target_t excluded_targets +) { + // We could test that excluded_targets != ALL_TARGETS, but in practice it's always + // called with an explcit/hardcoded value and so won't ever exclude everything. + for (modifier_entry_t const& modifier_entry : modifier_sum.modifiers) { + add_modifier( + *modifier_entry.modifier, modifier_entry.source, modifier_entry.multiplier, + modifier_entry.excluded_targets | excluded_targets + ); + } } -ModifierSum& ModifierSum::operator+=(ModifierSum const& modifier_sum) { - add_modifier_sum(modifier_sum); - return *this; +void ModifierSum::add_modifier_sum_exclude_source(ModifierSum const& modifier_sum, modifier_source_t const& excluded_source) { + for (modifier_entry_t const& modifier_entry : modifier_sum.modifiers) { + if (modifier_entry.source != excluded_source) { + add_modifier( + *modifier_entry.modifier, modifier_entry.source, modifier_entry.multiplier, modifier_entry.excluded_targets + ); + } + } } // TODO - include value_sum[effect] in result? Early return if lookup in value_sum fails? -std::vector<std::pair<Modifier const*, fixed_point_t>> ModifierSum::get_contributing_modifiers( - ModifierEffect const& effect + +void ModifierSum::push_contributing_modifiers( + ModifierEffect const& effect, std::vector<modifier_entry_t>& contributions ) const { - std::vector<std::pair<Modifier const*, fixed_point_t>> ret; + using enum ModifierEffect::target_t; - for (auto const& [modifier, multiplier] : modifiers) { - bool effect_found = false; - const fixed_point_t value = modifier->get_effect(effect, &effect_found); + for (modifier_entry_t const& modifier_entry : modifiers) { + if (ModifierEffect::excludes_targets(effect.get_targets(), modifier_entry.excluded_targets)) { + bool effect_found = false; + const fixed_point_t value = modifier_entry.modifier->get_effect(effect, &effect_found); - if (effect_found) { - ret.emplace_back(modifier, value * multiplier); + if (effect_found) { + contributions.push_back(modifier_entry); + } } } +} + +std::vector<ModifierSum::modifier_entry_t> ModifierSum::get_contributing_modifiers(ModifierEffect const& effect) const { + std::vector<modifier_entry_t> contributions; + + push_contributing_modifiers(effect, contributions); - return ret; + return contributions; } diff --git a/src/openvic-simulation/modifier/ModifierSum.hpp b/src/openvic-simulation/modifier/ModifierSum.hpp index 957ffab..843fcfc 100644 --- a/src/openvic-simulation/modifier/ModifierSum.hpp +++ b/src/openvic-simulation/modifier/ModifierSum.hpp @@ -1,30 +1,77 @@ #pragma once -#include "openvic-simulation/modifier/Modifier.hpp" -#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp" +#include <variant> + +#include "openvic-simulation/modifier/ModifierValue.hpp" +#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" namespace OpenVic { + struct CountryInstance; + struct ProvinceInstance; + struct Modifier; + struct ModifierSum { + using modifier_source_t = std::variant<CountryInstance const*, ProvinceInstance const*>; + + static std::string_view source_to_string(modifier_source_t const& source); + + struct modifier_entry_t { + Modifier const* modifier; + fixed_point_t multiplier; + modifier_source_t source; + ModifierEffect::target_t excluded_targets; + + constexpr modifier_entry_t( + Modifier const* new_modifier, + fixed_point_t new_multiplier, + modifier_source_t const& new_source, + ModifierEffect::target_t new_excluded_targets + ) : modifier { new_modifier }, + multiplier { new_multiplier }, + source { new_source }, + excluded_targets { new_excluded_targets } {} + + constexpr bool operator==(modifier_entry_t const& other) const { + return modifier == other.modifier + && multiplier == other.multiplier + && source_to_string(source) == source_to_string(other.source) + && excluded_targets == other.excluded_targets; + } + + std::string to_string() const; + }; + private: - fixed_point_map_t<Modifier const*> PROPERTY(modifiers); + std::vector<modifier_entry_t> PROPERTY(modifiers); ModifierValue PROPERTY(value_sum); public: ModifierSum() = default; + ModifierSum(ModifierSum const&) = default; ModifierSum(ModifierSum&&) = default; + ModifierSum& operator=(ModifierSum const&) = default; + ModifierSum& operator=(ModifierSum&&) = default; void clear(); bool empty(); fixed_point_t get_effect(ModifierEffect const& effect, bool* effect_found = nullptr) const; + fixed_point_t get_effect_nullcheck(ModifierEffect const* effect, bool* effect_found = nullptr) const; bool has_effect(ModifierEffect const& effect) const; - void add_modifier(Modifier const& modifier, fixed_point_t multiplier = fixed_point_t::_1()); + void add_modifier( + Modifier const& modifier, modifier_source_t const& source, fixed_point_t multiplier = fixed_point_t::_1(), + ModifierEffect::target_t excluded_targets = ModifierEffect::target_t::NO_TARGETS + ); + void add_modifier_nullcheck( + Modifier const* modifier, modifier_source_t const& source, fixed_point_t multiplier = fixed_point_t::_1(), + ModifierEffect::target_t excluded_targets = ModifierEffect::target_t::NO_TARGETS + ); void add_modifier_sum(ModifierSum const& modifier_sum); + void add_modifier_sum_exclude_targets(ModifierSum const& modifier_sum, ModifierEffect::target_t excluded_targets); + void add_modifier_sum_exclude_source(ModifierSum const& modifier_sum, modifier_source_t const& excluded_source); - ModifierSum& operator+=(Modifier const& modifier); - ModifierSum& operator+=(ModifierSum const& modifier_sum); - - std::vector<std::pair<Modifier const*, fixed_point_t>> get_contributing_modifiers(ModifierEffect const& effect) const; + void push_contributing_modifiers(ModifierEffect const& effect, std::vector<modifier_entry_t>& contributions) const; + std::vector<modifier_entry_t> get_contributing_modifiers(ModifierEffect const& effect) const; }; } diff --git a/src/openvic-simulation/modifier/ModifierValue.cpp b/src/openvic-simulation/modifier/ModifierValue.cpp new file mode 100644 index 0000000..e3e7b0b --- /dev/null +++ b/src/openvic-simulation/modifier/ModifierValue.cpp @@ -0,0 +1,149 @@ +#include "ModifierValue.hpp" + +#include "openvic-simulation/utility/TslHelper.hpp" + +using namespace OpenVic; + +ModifierValue::ModifierValue() = default; +ModifierValue::ModifierValue(effect_map_t&& new_values) : values { std::move(new_values) } {} +ModifierValue::ModifierValue(ModifierValue const&) = default; +ModifierValue::ModifierValue(ModifierValue&&) = default; + +ModifierValue& ModifierValue::operator=(ModifierValue const&) = default; +ModifierValue& ModifierValue::operator=(ModifierValue&&) = default; + +void ModifierValue::trim() { + erase_if(values, [](effect_map_t::value_type const& value) -> bool { + return value.second == fixed_point_t::_0(); + }); +} + +size_t ModifierValue::get_effect_count() const { + return values.size(); +} + +void ModifierValue::clear() { + values.clear(); +} + +bool ModifierValue::empty() const { + return values.empty(); +} + +fixed_point_t ModifierValue::get_effect(ModifierEffect const& effect, bool* effect_found) const { + const effect_map_t::const_iterator it = values.find(&effect); + if (it != values.end()) { + if (effect_found != nullptr) { + *effect_found = true; + } + return it->second; + } + + if (effect_found != nullptr) { + *effect_found = false; + } + return fixed_point_t::_0(); +} + +fixed_point_t ModifierValue::get_effect_nullcheck(ModifierEffect const* effect, bool* effect_found) const { + if (effect != nullptr) { + return get_effect(*effect, effect_found); + } + + if (effect_found != nullptr) { + *effect_found = false; + } + return fixed_point_t::_0(); +} + +bool ModifierValue::has_effect(ModifierEffect const& effect) const { + return values.contains(&effect); +} + +void ModifierValue::set_effect(ModifierEffect const& effect, fixed_point_t value) { + values[&effect] = value; +} + +ModifierValue& ModifierValue::operator+=(ModifierValue const& right) { + for (effect_map_t::value_type const& value : right.values) { + values[value.first] += value.second; + } + return *this; +} + +ModifierValue ModifierValue::operator+(ModifierValue const& right) const { + ModifierValue copy = *this; + return copy += right; +} + +ModifierValue ModifierValue::operator-() const { + ModifierValue copy = *this; + for (auto value : mutable_iterator(copy.values)) { + value.second = -value.second; + } + return copy; +} + +ModifierValue& ModifierValue::operator-=(ModifierValue const& right) { + for (effect_map_t::value_type const& value : right.values) { + values[value.first] -= value.second; + } + return *this; +} + +ModifierValue ModifierValue::operator-(ModifierValue const& right) const { + ModifierValue copy = *this; + return copy -= right; +} + +ModifierValue& ModifierValue::operator*=(fixed_point_t const& right) { + for (auto value : mutable_iterator(values)) { + value.second *= right; + } + return *this; +} + +ModifierValue ModifierValue::operator*(fixed_point_t const& right) const { + ModifierValue copy = *this; + return copy *= right; +} + +void ModifierValue::apply_exclude_targets(ModifierEffect::target_t excluded_targets) { + using enum ModifierEffect::target_t; + + // We could test if excluded_targets is NO_TARGETS (and so we do nothing) or ALL_TARGETS (and so we clear everything), + // but so long as this is always called with an explicit/hardcoded value then we'll never have either of those cases. + erase_if( + values, + [excluded_targets](effect_map_t::value_type const& value) -> bool { + return !ModifierEffect::excludes_targets(value.first->get_targets(), excluded_targets); + } + ); +} + +void ModifierValue::multiply_add_exclude_targets( + ModifierValue const& other, fixed_point_t multiplier, ModifierEffect::target_t excluded_targets +) { + using enum ModifierEffect::target_t; + + if (multiplier == fixed_point_t::_1() && excluded_targets == NO_TARGETS) { + *this += other; + } else if (multiplier != fixed_point_t::_0()) { + // We could test that excluded_targets != ALL_TARGETS, but in practice it's always + // called with an explcit/hardcoded value and so won't ever exclude everything. + for (effect_map_t::value_type const& value : other.values) { + if (ModifierEffect::excludes_targets(value.first->get_targets(), excluded_targets)) { + values[value.first] += value.second * multiplier; + } + } + } +} + +namespace OpenVic { // so the compiler shuts up + std::ostream& operator<<(std::ostream& stream, ModifierValue const& value) { + for (ModifierValue::effect_map_t::value_type const& effect : value.values) { + stream << effect.first << ": " << effect.second << "\n"; + } + return stream; + } +} diff --git a/src/openvic-simulation/modifier/ModifierValue.hpp b/src/openvic-simulation/modifier/ModifierValue.hpp new file mode 100644 index 0000000..6e6a29b --- /dev/null +++ b/src/openvic-simulation/modifier/ModifierValue.hpp @@ -0,0 +1,50 @@ +#pragma once + +#include "openvic-simulation/modifier/ModifierEffect.hpp" +#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp" + +namespace OpenVic { + struct ModifierValue { + friend struct ModifierManager; + + using effect_map_t = fixed_point_map_t<ModifierEffect const*>; + + private: + effect_map_t PROPERTY(values); + + public: + ModifierValue(); + ModifierValue(effect_map_t&& new_values); + ModifierValue(ModifierValue const&); + ModifierValue(ModifierValue&&); + + ModifierValue& operator=(ModifierValue const&); + ModifierValue& operator=(ModifierValue&&); + + /* Removes effect entries with a value of zero. */ + void trim(); + size_t get_effect_count() const; + void clear(); + bool empty() const; + + fixed_point_t get_effect(ModifierEffect const& effect, bool* effect_found = nullptr) const; + fixed_point_t get_effect_nullcheck(ModifierEffect const* effect, bool* effect_found = nullptr) const; + bool has_effect(ModifierEffect const& effect) const; + void set_effect(ModifierEffect const& effect, fixed_point_t value); + + ModifierValue& operator+=(ModifierValue const& right); + ModifierValue operator+(ModifierValue const& right) const; + ModifierValue operator-() const; + ModifierValue& operator-=(ModifierValue const& right); + ModifierValue operator-(ModifierValue const& right) const; + ModifierValue& operator*=(fixed_point_t const& right); + ModifierValue operator*(fixed_point_t const& right) const; + + void apply_exclude_targets(ModifierEffect::target_t excluded_targets); + void multiply_add_exclude_targets( + ModifierValue const& other, fixed_point_t multiplier, ModifierEffect::target_t excluded_targets + ); + + friend std::ostream& operator<<(std::ostream& stream, ModifierValue const& value); + }; +} diff --git a/src/openvic-simulation/modifier/StaticModifierCache.cpp b/src/openvic-simulation/modifier/StaticModifierCache.cpp new file mode 100644 index 0000000..953b49b --- /dev/null +++ b/src/openvic-simulation/modifier/StaticModifierCache.cpp @@ -0,0 +1,171 @@ +#include "StaticModifierCache.hpp" + +#include <string_view> + +#include "openvic-simulation/dataloader/NodeTools.hpp" +#include "openvic-simulation/modifier/Modifier.hpp" +#include "openvic-simulation/modifier/ModifierManager.hpp" + +using namespace OpenVic; +using namespace OpenVic::NodeTools; +using enum Modifier::modifier_type_t; + +StaticModifierCache::StaticModifierCache() + : // Country modifiers + very_easy_player { "very_easy_player", {}, STATIC }, + easy_player { "easy_player", {}, STATIC }, + hard_player { "hard_player", {}, STATIC }, + very_hard_player { "very_hard_player", {}, STATIC }, + very_easy_ai { "very_easy_ai", {}, STATIC }, + easy_ai { "easy_ai", {}, STATIC }, + hard_ai { "hard_ai", {}, STATIC }, + very_hard_ai { "very_hard_ai", {}, STATIC }, + base_modifier { "base_values", {}, STATIC }, + war { "war", {}, STATIC }, + peace { "peace", {}, STATIC }, + disarming { "disarming", {}, STATIC }, + war_exhaustion { "war_exhaustion", {}, STATIC }, + infamy { "badboy", {}, STATIC }, + debt_default_to { "debt_default_to", {}, STATIC }, + great_power { "great_power", {}, STATIC }, + secondary_power { "second_power", {}, STATIC }, + civilised { "civ_nation", {}, STATIC }, + uncivilised { "unciv_nation", {}, STATIC }, + literacy { "average_literacy", {}, STATIC }, + plurality { "plurality", {}, STATIC }, + total_occupation { "total_occupation", {}, STATIC }, + total_blockaded { "total_blockaded", {}, STATIC }, + in_bankruptcy { nullptr }, + bad_debtor { nullptr }, + generalised_debt_default { nullptr }, + // Province modifiers + overseas { "overseas", {}, STATIC }, + coastal { "coastal", {}, STATIC }, + non_coastal { "non_coastal", {}, STATIC }, + coastal_sea { "coastal_sea", {}, STATIC }, + sea_zone { "sea_zone", {}, STATIC }, + land_province { "land_province", {}, STATIC }, + blockaded { "blockaded", {}, STATIC }, + no_adjacent_controlled { "no_adjacent_controlled", {}, STATIC }, + core { "core", {}, STATIC }, + has_siege { "has_siege", {}, STATIC }, + occupied { "occupied", {}, STATIC }, + nationalism { "nationalism", {}, STATIC }, + infrastructure { "infrastructure", {}, STATIC } {} + +bool StaticModifierCache::load_static_modifiers(ModifierManager& modifier_manager, const ast::NodeCPtr root) { + bool ret = true; + key_map_t key_map {}; + + const auto set_static_country_modifier = [&key_map, &modifier_manager, &ret]( + Modifier& modifier + ) -> void { + ret &= add_key_map_entry( + key_map, modifier.get_identifier(), ONE_EXACTLY, + expect_dictionary(modifier_manager.expect_base_country_modifier(modifier)) + ); + }; + + const auto set_country_event_modifier = [&key_map, &modifier_manager, &ret]( + const std::string_view identifier, const IconModifier::icon_t icon + ) -> void { + ret &= add_key_map_entry( + key_map, identifier, ONE_EXACTLY, + [&modifier_manager, identifier, icon](const ast::NodeCPtr value) -> bool { + if (modifier_manager.get_event_modifier_by_identifier(identifier) != nullptr) { + return true; //an event modifier overrides a static modifier with the same identifier. + } + + ModifierValue modifier_value {}; + bool has_parsed_modifier = expect_dictionary(modifier_manager.expect_base_country_modifier(modifier_value))(value); + has_parsed_modifier &= modifier_manager.add_event_modifier(identifier, std::move(modifier_value), icon, STATIC); + return has_parsed_modifier; + } + ); + + }; + + const auto set_static_province_modifier = [&key_map, &modifier_manager, &ret]( + Modifier& modifier + ) -> void { + ret &= add_key_map_entry( + key_map, modifier.get_identifier(), ONE_EXACTLY, + expect_dictionary(modifier_manager.expect_base_province_modifier(modifier)) + ); + }; + + // Country modifiers + set_static_country_modifier(very_easy_player); + set_static_country_modifier(easy_player); + set_static_country_modifier(hard_player); + set_static_country_modifier(very_hard_player); + set_static_country_modifier(very_easy_ai); + set_static_country_modifier(easy_ai); + set_static_country_modifier(hard_ai); + set_static_country_modifier(very_hard_ai); + + ret &= add_key_map_entry( + key_map, base_modifier.get_identifier(), ONE_EXACTLY, + expect_dictionary_keys_and_default( + modifier_manager.expect_base_country_modifier(base_modifier), + "supply_limit", ZERO_OR_ONE, [this, &modifier_manager](const ast::NodeCPtr value) -> bool { + return modifier_manager._add_modifier_cb( + base_modifier, + modifier_manager.get_modifier_effect_cache().get_supply_limit_global_base(), + value + ); + } + ) + ); + + set_static_country_modifier(war); + set_static_country_modifier(peace); + set_static_country_modifier(disarming); + set_static_country_modifier(war_exhaustion); + set_static_country_modifier(infamy); + set_static_country_modifier(debt_default_to); + set_static_country_modifier(great_power); + set_static_country_modifier(secondary_power); + set_static_country_modifier(civilised); + set_static_country_modifier(uncivilised); + set_static_country_modifier(literacy); + set_static_country_modifier(plurality); + set_static_country_modifier(total_occupation); + set_static_country_modifier(total_blockaded); + + // Country Event modifiers + static constexpr IconModifier::icon_t default_icon = 0; + static constexpr std::string_view bad_debtor_id = "bad_debter"; //paradox typo + static constexpr std::string_view in_bankruptcy_id = "in_bankrupcy"; //paradox typo + static constexpr std::string_view generalised_debt_default_id = "generalised_debt_default"; + set_country_event_modifier(bad_debtor_id, default_icon); + set_country_event_modifier(in_bankruptcy_id, default_icon); + set_country_event_modifier(generalised_debt_default_id, default_icon); + + // Province modifiers + set_static_province_modifier(overseas); + set_static_province_modifier(coastal); + set_static_province_modifier(non_coastal); + set_static_province_modifier(coastal_sea); + set_static_province_modifier(sea_zone); + set_static_province_modifier(land_province); + set_static_province_modifier(blockaded); + set_static_province_modifier(no_adjacent_controlled); + set_static_province_modifier(core); + set_static_province_modifier(has_siege); + set_static_province_modifier(occupied); + set_static_province_modifier(nationalism); + set_static_province_modifier(infrastructure); + + + ret &= expect_dictionary_key_map_and_default(key_map, key_value_invalid_callback)(root); + + if (ret) { + bad_debtor = modifier_manager.get_event_modifier_by_identifier(bad_debtor_id); + in_bankruptcy = modifier_manager.get_event_modifier_by_identifier(in_bankruptcy_id); + generalised_debt_default = modifier_manager.get_event_modifier_by_identifier(generalised_debt_default_id); + total_occupation *= 100; + } + + return ret; +} diff --git a/src/openvic-simulation/modifier/StaticModifierCache.hpp b/src/openvic-simulation/modifier/StaticModifierCache.hpp new file mode 100644 index 0000000..b31e127 --- /dev/null +++ b/src/openvic-simulation/modifier/StaticModifierCache.hpp @@ -0,0 +1,67 @@ +#pragma once + +#include "openvic-simulation/dataloader/NodeTools.hpp" +#include "openvic-simulation/modifier/Modifier.hpp" +#include "openvic-simulation/utility/Getters.hpp" + +namespace OpenVic { + struct ModifierManager; + + struct StaticModifierCache { + friend struct ModifierManager; + + private: + // Country modifiers + Modifier PROPERTY(very_easy_player); + Modifier PROPERTY(easy_player); + Modifier PROPERTY(hard_player); + Modifier PROPERTY(very_hard_player); + Modifier PROPERTY(very_easy_ai); + Modifier PROPERTY(easy_ai); + Modifier PROPERTY(hard_ai); + Modifier PROPERTY(very_hard_ai); + + Modifier PROPERTY(base_modifier); + Modifier PROPERTY(war); + Modifier PROPERTY(peace); + Modifier PROPERTY(disarming); + Modifier PROPERTY(war_exhaustion); + Modifier PROPERTY(infamy); + Modifier PROPERTY(debt_default_to); + Modifier PROPERTY(great_power); + Modifier PROPERTY(secondary_power); + Modifier PROPERTY(civilised); + Modifier PROPERTY(uncivilised); + Modifier PROPERTY(literacy); + Modifier PROPERTY(plurality); + Modifier PROPERTY(total_occupation); + Modifier PROPERTY(total_blockaded); + + // Country event modifiers + Modifier const* PROPERTY(in_bankruptcy); + Modifier const* PROPERTY(bad_debtor); + Modifier const* PROPERTY(generalised_debt_default); //possibly, as it's used to trigger gunboat CB + + // Province modifiers + Modifier PROPERTY(overseas); + Modifier PROPERTY(coastal); + Modifier PROPERTY(non_coastal); + Modifier PROPERTY(coastal_sea); + Modifier PROPERTY(sea_zone); + Modifier PROPERTY(land_province); + Modifier PROPERTY(blockaded); + Modifier PROPERTY(no_adjacent_controlled); + Modifier PROPERTY(core); + Modifier PROPERTY(has_siege); + Modifier PROPERTY(occupied); + Modifier PROPERTY(nationalism); + Modifier PROPERTY(infrastructure); + + StaticModifierCache(); + + bool load_static_modifiers(ModifierManager& modifier_manager, const ast::NodeCPtr root); + + public: + StaticModifierCache(StaticModifierCache&&) = default; + }; +} diff --git a/src/openvic-simulation/politics/Issue.cpp b/src/openvic-simulation/politics/Issue.cpp index 3174e23..8da09fc 100644 --- a/src/openvic-simulation/politics/Issue.cpp +++ b/src/openvic-simulation/politics/Issue.cpp @@ -1,5 +1,8 @@ #include "Issue.hpp" +#include "openvic-simulation/modifier/ModifierManager.hpp" +#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" + using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -180,9 +183,14 @@ bool IssueManager::_load_issue( RuleSet rules; bool jingoism = false; - bool ret = modifier_manager.expect_modifier_value_and_keys(move_variable_callback(values), + bool ret = NodeTools::expect_dictionary_keys_and_default( + modifier_manager.expect_base_country_modifier(values), "is_jingoism", ZERO_OR_ONE, expect_bool(assign_variable_callback(jingoism)), - "rules", ZERO_OR_ONE, rule_manager.expect_rule_set(move_variable_callback(rules)) + "rules", ZERO_OR_ONE, rule_manager.expect_rule_set(move_variable_callback(rules)), + "war_exhaustion_effect", ZERO_OR_ONE, [](const ast::NodeCPtr _) -> bool { + Logger::warning("war_exhaustion_effect does nothing (vanilla issues have it)."); + return true; + } )(node); ret &= add_issue( @@ -221,8 +229,8 @@ bool IssueManager::_load_reform( ConditionScript on_execute_trigger { scope_t::COUNTRY, scope_t::COUNTRY, scope_t::NO_SCOPE }; EffectScript on_execute_effect; - bool ret = modifier_manager.expect_modifier_value_and_keys( - move_variable_callback(values), + bool ret = NodeTools::expect_dictionary_keys_and_default( + modifier_manager.expect_base_country_modifier(values), "administrative_multiplier", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(administrative_multiplier)), "technology_cost", ZERO_OR_ONE, expect_uint(assign_variable_callback(technology_cost)), "allow", ZERO_OR_ONE, allow.expect_script(), diff --git a/src/openvic-simulation/politics/NationalFocus.cpp b/src/openvic-simulation/politics/NationalFocus.cpp index 4887ed6..2427d7c 100644 --- a/src/openvic-simulation/politics/NationalFocus.cpp +++ b/src/openvic-simulation/politics/NationalFocus.cpp @@ -1,6 +1,7 @@ #include "NationalFocus.hpp" #include "openvic-simulation/economy/GoodDefinition.hpp" +#include "openvic-simulation/modifier/ModifierManager.hpp" #include "openvic-simulation/politics/Ideology.hpp" #include "openvic-simulation/pop/Pop.hpp" @@ -129,9 +130,9 @@ bool NationalFocusManager::load_national_foci_file( scope_t::PROVINCE | scope_t::COUNTRY, scope_t::PROVINCE | scope_t::COUNTRY, scope_t::NO_SCOPE }; - bool ret = modifier_manager.expect_modifier_value_and_keys_and_default( - move_variable_callback(modifiers), - [&good_definition_manager, &encourage_goods, &pop_manager, &encourage_pop_types]( + const auto expect_base_province_modifier_cb = modifier_manager.expect_base_province_modifier(modifiers); + bool ret = NodeTools::expect_dictionary_keys_and_default( + [&good_definition_manager, &encourage_goods, &pop_manager, &encourage_pop_types, &expect_base_province_modifier_cb]( std::string_view key, ast::NodeCPtr value ) -> bool { GoodDefinition const* good = good_definition_manager.get_good_definition_by_identifier(key); @@ -144,7 +145,7 @@ bool NationalFocusManager::load_national_foci_file( return expect_fixed_point(map_callback(encourage_pop_types, pop_type))(value); } - return key_value_invalid_callback(key, value); + return expect_base_province_modifier_cb(key, value) || key_value_invalid_callback(key, value); }, "icon", ONE_EXACTLY, expect_uint(assign_variable_callback(icon)), "ideology", ZERO_OR_ONE, diff --git a/src/openvic-simulation/politics/NationalValue.cpp b/src/openvic-simulation/politics/NationalValue.cpp index e9f7f2a..9a30fa1 100644 --- a/src/openvic-simulation/politics/NationalValue.cpp +++ b/src/openvic-simulation/politics/NationalValue.cpp @@ -1,5 +1,7 @@ #include "NationalValue.hpp" +#include "openvic-simulation/modifier/ModifierManager.hpp" + using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -20,10 +22,12 @@ bool NationalValueManager::load_national_values_file(ModifierManager const& modi national_values, [this, &modifier_manager](std::string_view national_value_identifier, ast::NodeCPtr value) -> bool { ModifierValue modifiers; - - bool ret = modifier_manager.expect_modifier_value(move_variable_callback(modifiers))(value); + bool ret = NodeTools::expect_dictionary( + modifier_manager.expect_base_country_modifier(modifiers) + )(value); ret &= add_national_value(national_value_identifier, std::move(modifiers)); + return ret; } )(root); diff --git a/src/openvic-simulation/politics/Rebel.cpp b/src/openvic-simulation/politics/Rebel.cpp index fef61c0..9b2927c 100644 --- a/src/openvic-simulation/politics/Rebel.cpp +++ b/src/openvic-simulation/politics/Rebel.cpp @@ -2,6 +2,8 @@ #include <string_view> +#include "openvic-simulation/modifier/ModifierManager.hpp" + using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -175,6 +177,7 @@ bool RebelManager::load_rebels_file( bool RebelManager::generate_modifiers(ModifierManager& modifier_manager) const { using enum ModifierEffect::format_t; + using enum ModifierEffect::target_t; bool ret = true; @@ -183,13 +186,20 @@ bool RebelManager::generate_modifiers(ModifierManager& modifier_manager) const { ret &= modifier_manager.register_complex_modifier(identifier); - ret &= modifier_manager.add_modifier_effect( - ModifierManager::get_flat_identifier(identifier, "all"), is_positive_good, PROPORTION_DECIMAL, "TECH_REBEL_ORG_GAIN" + ret &= modifier_manager.register_technology_modifier_effect( + modifier_manager.modifier_effect_cache.rebel_org_gain_all, ModifierManager::get_flat_identifier(identifier, "all"), + is_positive_good, PROPORTION_DECIMAL, "TECH_REBEL_ORG_GAIN" ); + IndexedMap<RebelType, ModifierEffect const*>& rebel_org_gain_effects = + modifier_manager.modifier_effect_cache.rebel_org_gain_effects; + + rebel_org_gain_effects.set_keys(&get_rebel_types()); + for (RebelType const& rebel_type : get_rebel_types()) { - ret &= modifier_manager.add_modifier_effect( - ModifierManager::get_flat_identifier(identifier, rebel_type.get_identifier()), is_positive_good, PROPORTION_DECIMAL, + ret &= modifier_manager.register_technology_modifier_effect( + rebel_org_gain_effects[rebel_type], ModifierManager::get_flat_identifier(identifier, rebel_type.get_identifier()), + is_positive_good, PROPORTION_DECIMAL, StringUtils::append_string_views("$", rebel_type.get_identifier(), "_title$ $TECH_REBEL_ORG_GAIN$") ); } diff --git a/src/openvic-simulation/politics/Rebel.hpp b/src/openvic-simulation/politics/Rebel.hpp index fc82c84..b47bcbb 100644 --- a/src/openvic-simulation/politics/Rebel.hpp +++ b/src/openvic-simulation/politics/Rebel.hpp @@ -2,7 +2,6 @@ #include <cstdint> -#include "openvic-simulation/modifier/Modifier.hpp" #include "openvic-simulation/politics/Government.hpp" #include "openvic-simulation/politics/Ideology.hpp" #include "openvic-simulation/scripts/ConditionalWeight.hpp" @@ -79,6 +78,8 @@ namespace OpenVic { RebelType(RebelType&&) = default; }; + struct ModifierManager; + struct RebelManager { private: IdentifierRegistry<RebelType> IDENTIFIER_REGISTRY(rebel_type); diff --git a/src/openvic-simulation/pop/Pop.cpp b/src/openvic-simulation/pop/Pop.cpp index 85730e3..0f0de69 100644 --- a/src/openvic-simulation/pop/Pop.cpp +++ b/src/openvic-simulation/pop/Pop.cpp @@ -5,9 +5,11 @@ #include "openvic-simulation/map/ProvinceInstance.hpp" #include "openvic-simulation/military/UnitType.hpp" #include "openvic-simulation/misc/Define.hpp" +#include "openvic-simulation/modifier/ModifierManager.hpp" #include "openvic-simulation/politics/Ideology.hpp" #include "openvic-simulation/politics/Issue.hpp" #include "openvic-simulation/politics/Rebel.hpp" +#include "openvic-simulation/utility/Logger.hpp" #include "openvic-simulation/utility/TslHelper.hpp" using namespace OpenVic; @@ -114,14 +116,15 @@ void Pop::setup_pop_test_values(IssueManager const& issue_manager) { luxury_needs_fulfilled = test_range(); } -void Pop::convert_to_equivalent() { +bool Pop::convert_to_equivalent() { PopType const* const equivalent = get_type()->get_equivalent(); - if(equivalent == nullptr) { - //error? - } - else { - type = equivalent; + if (equivalent == nullptr) { + Logger::error("Tried to convert pop of type ", get_type()->get_identifier(), " to equivalent, but there is no equivalent."); + return false; } + + type = equivalent; + return true; } void Pop::set_location(ProvinceInstance const& new_location) { @@ -154,6 +157,10 @@ void Pop::update_gamestate( } } +//TODO store income +void Pop::add_rgo_owner_income(const fixed_point_t income) {} +void Pop::add_rgo_worker_income(const fixed_point_t income) {} + Strata::Strata(std::string_view new_identifier) : HasIdentifier { new_identifier } {} PopType::PopType( @@ -664,24 +671,33 @@ bool PopManager::load_pop_bases_into_vector( bool PopManager::generate_modifiers(ModifierManager& modifier_manager) const { using enum ModifierEffect::format_t; + using enum ModifierEffect::target_t; + + IndexedMap<Strata, ModifierEffectCache::strata_effects_t>& strata_effects = + modifier_manager.modifier_effect_cache.strata_effects; + + strata_effects.set_keys(&get_stratas()); bool ret = true; for (Strata const& strata : get_stratas()) { const auto strata_modifier = [&modifier_manager, &ret, &strata]( - std::string_view suffix, bool is_positive_good + ModifierEffect const*& effect_cache, std::string_view suffix, bool is_positive_good ) -> void { - ret &= modifier_manager.add_modifier_effect( - StringUtils::append_string_views(strata.get_identifier(), suffix), is_positive_good + ret &= modifier_manager.register_base_country_modifier_effect( + effect_cache, StringUtils::append_string_views(strata.get_identifier(), suffix), is_positive_good, + PROPORTION_DECIMAL ); }; - strata_modifier("_income_modifier", true); - strata_modifier("_vote", true); + ModifierEffectCache::strata_effects_t& this_strata_effects = strata_effects[strata]; + + strata_modifier(this_strata_effects.income_modifier, "_income_modifier", true); // Has no effect in game + strata_modifier(this_strata_effects.vote, "_vote", true); - strata_modifier("_life_needs", false); - strata_modifier("_everyday_needs", false); - strata_modifier("_luxury_needs", false); + strata_modifier(this_strata_effects.life_needs, "_life_needs", false); + strata_modifier(this_strata_effects.everyday_needs, "_everyday_needs", false); + strata_modifier(this_strata_effects.luxury_needs, "_luxury_needs", false); } return ret; diff --git a/src/openvic-simulation/pop/Pop.hpp b/src/openvic-simulation/pop/Pop.hpp index ec8b955..88c397a 100644 --- a/src/openvic-simulation/pop/Pop.hpp +++ b/src/openvic-simulation/pop/Pop.hpp @@ -95,7 +95,7 @@ namespace OpenVic { Pop& operator=(Pop&&) = delete; void setup_pop_test_values(IssueManager const& issue_manager); - void convert_to_equivalent(); + bool convert_to_equivalent(); void set_location(ProvinceInstance const& new_location); @@ -103,6 +103,9 @@ namespace OpenVic { DefineManager const& define_manager, CountryInstance const* owner, fixed_point_t const& pop_size_per_regiment_multiplier ); + + void add_rgo_owner_income(const fixed_point_t income); + void add_rgo_worker_income(const fixed_point_t income); }; struct Strata : HasIdentifier { diff --git a/src/openvic-simulation/research/Invention.cpp b/src/openvic-simulation/research/Invention.cpp index 299cefb..1076a62 100644 --- a/src/openvic-simulation/research/Invention.cpp +++ b/src/openvic-simulation/research/Invention.cpp @@ -3,6 +3,7 @@ #include "openvic-simulation/economy/BuildingType.hpp" #include "openvic-simulation/map/Crime.hpp" #include "openvic-simulation/military/UnitType.hpp" +#include "openvic-simulation/modifier/ModifierManager.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -54,12 +55,14 @@ bool InventionManager::add_invention( } bool InventionManager::load_inventions_file( - ModifierManager const& modifier_manager, UnitTypeManager const& unit_type_manager, BuildingTypeManager const& building_type_manager, - CrimeManager const& crime_manager, ast::NodeCPtr root + ModifierManager const& modifier_manager, UnitTypeManager const& unit_type_manager, + BuildingTypeManager const& building_type_manager, CrimeManager const& crime_manager, ast::NodeCPtr root ) { return expect_dictionary_reserve_length( inventions, [this, &modifier_manager, &unit_type_manager, &building_type_manager, &crime_manager]( - std::string_view identifier, ast::NodeCPtr value) -> bool { + std::string_view identifier, ast::NodeCPtr value + ) -> bool { + // TODO - use the same variable for all modifiers rather than combining them at the end? ModifierValue loose_modifiers; ModifierValue modifiers; @@ -74,15 +77,17 @@ bool InventionManager::load_inventions_file( ConditionScript limit { scope_t::COUNTRY, scope_t::COUNTRY, scope_t::NO_SCOPE }; ConditionalWeight chance { scope_t::COUNTRY, scope_t::COUNTRY, scope_t::NO_SCOPE }; - bool ret = modifier_manager.expect_modifier_value_and_keys(move_variable_callback(loose_modifiers), + bool ret = NodeTools::expect_dictionary_keys_and_default( + modifier_manager.expect_base_country_modifier(loose_modifiers), "news", ZERO_OR_ONE, expect_bool(assign_variable_callback(news)), "limit", ONE_EXACTLY, limit.expect_script(), "chance", ONE_EXACTLY, chance.expect_conditional_weight(ConditionalWeight::BASE), - "effect", ZERO_OR_ONE, modifier_manager.expect_modifier_value_and_keys( - move_variable_callback(modifiers), + "effect", ZERO_OR_ONE, NodeTools::expect_dictionary_keys_and_default( + modifier_manager.expect_technology_modifier(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_type_manager.expect_unit_type_identifier(set_callback_pointer(activated_units)), + "activate_unit", ZERO_OR_MORE, + unit_type_manager.expect_unit_type_identifier(set_callback_pointer(activated_units)), "activate_building", ZERO_OR_MORE, building_type_manager.expect_building_type_identifier( set_callback_pointer(activated_buildings) ), diff --git a/src/openvic-simulation/research/Technology.cpp b/src/openvic-simulation/research/Technology.cpp index a5256f3..9a1cf27 100644 --- a/src/openvic-simulation/research/Technology.cpp +++ b/src/openvic-simulation/research/Technology.cpp @@ -1,5 +1,7 @@ #include "Technology.hpp" +#include "openvic-simulation/modifier/ModifierManager.hpp" + using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -141,7 +143,10 @@ bool TechnologyManager::load_technology_file_schools( 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); + + bool ret = NodeTools::expect_dictionary( + modifier_manager.expect_base_country_modifier(modifiers) + )(school_value); ret &= add_technology_school(school_key, std::move(modifiers)); @@ -173,8 +178,8 @@ bool TechnologyManager::load_technologies_file( Technology::building_set_t activated_buildings; ConditionalWeight ai_chance { scope_t::COUNTRY, scope_t::COUNTRY, scope_t::NO_SCOPE }; - bool ret = modifier_manager.expect_modifier_value_and_keys( - move_variable_callback(modifiers), + bool ret = NodeTools::expect_dictionary_keys_and_default( + modifier_manager.expect_technology_modifier(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)), @@ -197,13 +202,21 @@ bool TechnologyManager::load_technologies_file( bool TechnologyManager::generate_modifiers(ModifierManager& modifier_manager) const { using enum ModifierEffect::format_t; + using enum ModifierEffect::target_t; + + IndexedMap<TechnologyFolder, ModifierEffect const*>& research_bonus_effects = + modifier_manager.modifier_effect_cache.research_bonus_effects; + + research_bonus_effects.set_keys(&get_technology_folders()); bool ret = true; for (TechnologyFolder const& folder : get_technology_folders()) { const std::string modifier_identifier = StringUtils::append_string_views(folder.get_identifier(), "_research_bonus"); - ret &= modifier_manager.add_modifier_effect(modifier_identifier, true, PROPORTION_DECIMAL, modifier_identifier); + ret &= modifier_manager.register_base_country_modifier_effect( + research_bonus_effects[folder], modifier_identifier, true, PROPORTION_DECIMAL, modifier_identifier + ); } return ret; diff --git a/src/openvic-simulation/scripts/Condition.cpp b/src/openvic-simulation/scripts/Condition.cpp index 7abd717..fd1f4fa 100644 --- a/src/openvic-simulation/scripts/Condition.cpp +++ b/src/openvic-simulation/scripts/Condition.cpp @@ -155,7 +155,7 @@ bool ConditionManager::setup_conditions(DefinitionManager const& definition_mana ret &= add_condition("great_wars_enabled", BOOLEAN, COUNTRY); ret &= add_condition("have_core_in", IDENTIFIER, COUNTRY, NO_SCOPE, NO_IDENTIFIER, COUNTRY_TAG); ret &= add_condition("has_country_flag", IDENTIFIER, COUNTRY, NO_SCOPE, NO_IDENTIFIER, COUNTRY_FLAG); - ret &= add_condition("has_country_modifier", IDENTIFIER, COUNTRY, NO_SCOPE, NO_IDENTIFIER, MODIFIER); + ret &= add_condition("has_country_modifier", IDENTIFIER, COUNTRY, NO_SCOPE, NO_IDENTIFIER, COUNTRY_EVENT_MODIFIER); ret &= add_condition("has_cultural_sphere", BOOLEAN, COUNTRY); ret &= add_condition("has_leader", STRING, COUNTRY); ret &= add_condition("has_pop_culture", IDENTIFIER, COUNTRY, NO_SCOPE, NO_IDENTIFIER, CULTURE); @@ -299,7 +299,7 @@ bool ConditionManager::setup_conditions(DefinitionManager const& definition_mana ret &= add_condition("has_empty_adjacent_state", BOOLEAN, PROVINCE); ret &= add_condition("has_national_minority", BOOLEAN, PROVINCE); ret &= add_condition("has_province_flag", IDENTIFIER, PROVINCE, NO_SCOPE, NO_IDENTIFIER, PROVINCE_FLAG); - ret &= add_condition("has_province_modifier", IDENTIFIER, PROVINCE, NO_SCOPE, NO_IDENTIFIER, MODIFIER); + ret &= add_condition("has_province_modifier", IDENTIFIER, PROVINCE, NO_SCOPE, NO_IDENTIFIER, PROVINCE_EVENT_MODIFIER); ret &= add_condition("has_recent_imigration", INTEGER, PROVINCE); //paradox typo ret &= add_condition("is_blockaded", BOOLEAN, PROVINCE); ret &= add_condition("is_accepted_culture", IDENTIFIER | BOOLEAN, PROVINCE, NO_SCOPE, NO_IDENTIFIER, COUNTRY_TAG); @@ -505,9 +505,8 @@ callback_t<std::string_view> ConditionManager::expect_parse_identifier( EXPECT_CALL(BUILDING, building_type, definition_manager.get_economy_manager().get_building_type_manager(), "FACTORY"); EXPECT_CALL(CASUS_BELLI, wargoal_type, definition_manager.get_military_manager().get_wargoal_type_manager()); EXPECT_CALL(GOVERNMENT_TYPE, government_type, definition_manager.get_politics_manager().get_government_type_manager()); - EXPECT_CALL(MODIFIER, event_modifier, definition_manager.get_modifier_manager()); - EXPECT_CALL(MODIFIER, triggered_modifier, definition_manager.get_modifier_manager()); - EXPECT_CALL(MODIFIER, static_modifier, definition_manager.get_modifier_manager()); + EXPECT_CALL(COUNTRY_EVENT_MODIFIER | PROVINCE_EVENT_MODIFIER, event_modifier, definition_manager.get_modifier_manager()); + EXPECT_CALL(COUNTRY_EVENT_MODIFIER, triggered_modifier, definition_manager.get_modifier_manager()); EXPECT_CALL(NATIONAL_VALUE, national_value, definition_manager.get_politics_manager().get_national_value_manager()); EXPECT_CALL( CULTURE_UNION, country_definition, definition_manager.get_country_definition_manager(), "THIS", "FROM", "THIS_UNION" diff --git a/src/openvic-simulation/scripts/Condition.hpp b/src/openvic-simulation/scripts/Condition.hpp index 748453f..8d4d246 100644 --- a/src/openvic-simulation/scripts/Condition.hpp +++ b/src/openvic-simulation/scripts/Condition.hpp @@ -63,12 +63,13 @@ namespace OpenVic { BUILDING = 1 << 20, CASUS_BELLI = 1 << 21, GOVERNMENT_TYPE = 1 << 22, - MODIFIER = 1 << 23, - NATIONAL_VALUE = 1 << 24, - CULTURE_UNION = 1 << 25, // same as COUNTRY_TAG but also accepts scope this_union - CONTINENT = 1 << 26, - CRIME = 1 << 27, - TERRAIN = 1 << 28, + COUNTRY_EVENT_MODIFIER = 1 << 23, + PROVINCE_EVENT_MODIFIER = 1 << 24, + NATIONAL_VALUE = 1 << 25, + CULTURE_UNION = 1 << 26, // same as COUNTRY_TAG but also accepts scope this_union + CONTINENT = 1 << 27, + CRIME = 1 << 28, + TERRAIN = 1 << 29 }; /* Allows enum types to be used with bitwise operators. */ @@ -175,7 +176,8 @@ namespace OpenVic { BUILD_STRING(BUILDING); BUILD_STRING(CASUS_BELLI); BUILD_STRING(GOVERNMENT_TYPE); - BUILD_STRING(MODIFIER); + BUILD_STRING(COUNTRY_EVENT_MODIFIER); + BUILD_STRING(PROVINCE_EVENT_MODIFIER); BUILD_STRING(NATIONAL_VALUE); BUILD_STRING(CULTURE_UNION); BUILD_STRING(CONTINENT); diff --git a/src/openvic-simulation/utility/NumberUtils.hpp b/src/openvic-simulation/utility/NumberUtils.hpp index d814019..8656f27 100644 --- a/src/openvic-simulation/utility/NumberUtils.hpp +++ b/src/openvic-simulation/utility/NumberUtils.hpp @@ -49,4 +49,8 @@ namespace OpenVic::NumberUtils { return c; } + + constexpr bool is_power_of_two(uint64_t n) { + return n > 0 && (n & (n - 1)) == 0; + } } |