From f83e869def6608f64606aead24ad1cfbb6f5c72a Mon Sep 17 00:00:00 2001 From: hop311 Date: Tue, 16 Jul 2024 23:57:50 +0100 Subject: Add IndexedMap and use in low key count, high value density cases --- .../country/CountryDefinition.cpp | 4 +- .../country/CountryDefinition.hpp | 5 +- src/openvic-simulation/country/CountryInstance.cpp | 57 ++++++++++++++-------- src/openvic-simulation/country/CountryInstance.hpp | 27 +++++++--- 4 files changed, 61 insertions(+), 32 deletions(-) (limited to 'src/openvic-simulation/country') diff --git a/src/openvic-simulation/country/CountryDefinition.cpp b/src/openvic-simulation/country/CountryDefinition.cpp index e414e9e..e73629e 100644 --- a/src/openvic-simulation/country/CountryDefinition.cpp +++ b/src/openvic-simulation/country/CountryDefinition.cpp @@ -25,7 +25,7 @@ CountryParty::CountryParty( CountryDefinition::CountryDefinition( std::string_view new_identifier, colour_t new_colour, - size_t new_index, + index_t new_index, GraphicalCultureType const& new_graphical_culture, IdentifierRegistry&& new_parties, unit_names_map_t&& new_unit_names, @@ -35,7 +35,7 @@ CountryDefinition::CountryDefinition( colour_t new_secondary_unit_colour, colour_t new_tertiary_unit_colour ) : HasIdentifierAndColour { new_identifier, new_colour, false }, - index { new_index }, + HasIndex { new_index }, graphical_culture { new_graphical_culture }, parties { std::move(new_parties) }, unit_names { std::move(new_unit_names) }, diff --git a/src/openvic-simulation/country/CountryDefinition.hpp b/src/openvic-simulation/country/CountryDefinition.hpp index 70e62b9..f04796a 100644 --- a/src/openvic-simulation/country/CountryDefinition.hpp +++ b/src/openvic-simulation/country/CountryDefinition.hpp @@ -41,14 +41,13 @@ namespace OpenVic { }; /* Generic information about a TAG */ - struct CountryDefinition : HasIdentifierAndColour { + struct CountryDefinition : HasIdentifierAndColour, HasIndex<> { friend struct CountryDefinitionManager; using unit_names_map_t = ordered_map; using government_colour_map_t = ordered_map; private: - const size_t PROPERTY(index); GraphicalCultureType const& PROPERTY(graphical_culture); /* Not const to allow elements to be moved, otherwise a copy is forced * which causes a compile error as the copy constructor has been deleted. */ @@ -62,7 +61,7 @@ namespace OpenVic { // Unit colours not const due to being added after construction CountryDefinition( - std::string_view new_identifier, colour_t new_colour, size_t new_index, + std::string_view new_identifier, colour_t new_colour, index_t new_index, GraphicalCultureType const& new_graphical_culture, IdentifierRegistry&& new_parties, unit_names_map_t&& new_unit_names, bool new_dynamic_tag, government_colour_map_t&& new_alternative_colours, colour_t new_primary_unit_colour, colour_t new_secondary_unit_colour, colour_t new_tertiary_unit_colour diff --git a/src/openvic-simulation/country/CountryInstance.cpp b/src/openvic-simulation/country/CountryInstance.cpp index a4086a7..3e21bab 100644 --- a/src/openvic-simulation/country/CountryInstance.cpp +++ b/src/openvic-simulation/country/CountryInstance.cpp @@ -2,15 +2,29 @@ #include "openvic-simulation/country/CountryDefinition.hpp" #include "openvic-simulation/history/CountryHistory.hpp" -#include "openvic-simulation/military/Deployment.hpp" #include "openvic-simulation/military/UnitInstanceGroup.hpp" using namespace OpenVic; -CountryInstance::CountryInstance(CountryDefinition const* new_country_definition) - : country_definition { new_country_definition }, primary_culture { nullptr }, religion { nullptr }, ruling_party { nullptr }, - last_election {}, capital { nullptr }, government_type { nullptr }, plurality { 0 }, national_value { nullptr }, - civilised { false }, prestige { 0 } {} +CountryInstance::CountryInstance( + CountryDefinition const* new_country_definition, + decltype(technologies)::keys_t const& technology_keys, + decltype(inventions)::keys_t const& invention_keys, + decltype(upper_house)::keys_t const& ideology_keys +) : country_definition { new_country_definition }, + primary_culture { nullptr }, + religion { nullptr }, + ruling_party { nullptr }, + last_election {}, + capital { nullptr }, + government_type { nullptr }, + plurality { 0 }, + national_value { nullptr }, + civilised { false }, + prestige { 0 }, + upper_house { &ideology_keys }, + technologies { &technology_keys }, + inventions { &invention_keys } {} std::string_view CountryInstance::get_identifier() const { return country_definition != nullptr ? country_definition->get_identifier() : "NULL"; @@ -41,19 +55,20 @@ bool CountryInstance::remove_accepted_culture(Culture const* culture_to_remove) return true; } -void CountryInstance::add_to_upper_house(Ideology const* party, fixed_point_t popularity) { - upper_house[party] = popularity; -} - -bool CountryInstance::remove_from_upper_house(Ideology const* party) { - return upper_house.erase(party) == 1; +bool CountryInstance::set_upper_house(Ideology const* ideology, fixed_point_t popularity) { + if (ideology != nullptr) { + upper_house[*ideology] = popularity; + return true; + } else { + Logger::error("Trying to set null ideology in upper house of ", get_identifier()); + return false; + } } bool CountryInstance::add_reform(Reform const* new_reform) { if (std::find(reforms.begin(), reforms.end(), new_reform) != reforms.end()) { Logger::warning( - "Attempted to add reform ", new_reform->get_identifier(), " to country ", country_definition->get_identifier(), - ": already present!" + "Attempted to add reform \"", new_reform, "\" to country ", get_identifier(), ": already present!" ); return false; } @@ -65,8 +80,7 @@ bool CountryInstance::remove_reform(Reform const* reform_to_remove) { auto existing_entry = std::find(reforms.begin(), reforms.end(), reform_to_remove); if (existing_entry == reforms.end()) { Logger::warning( - "Attempted to remove reform ", reform_to_remove->get_identifier(), " from country ", - country_definition->get_identifier(), ": not present!" + "Attempted to remove reform \"", reform_to_remove, "\" from country ", get_identifier(), ": not present!" ); return false; } @@ -178,9 +192,7 @@ bool CountryInstance::apply_history_to_country(CountryHistoryEntry const* entry) set_optional(religion, entry->get_religion()); set_optional(ruling_party, entry->get_ruling_party()); set_optional(last_election, entry->get_last_election()); - for (auto const& [ideology, popularity] : entry->get_upper_house()) { - add_to_upper_house(ideology, popularity); - } + ret &= upper_house.copy(entry->get_upper_house()); set_optional(capital, entry->get_capital()); set_optional(government_type, entry->get_government_type()); set_optional(plurality, entry->get_plurality()); @@ -194,11 +206,16 @@ bool CountryInstance::apply_history_to_country(CountryHistoryEntry const* entry) return ret; } -bool CountryInstanceManager::generate_country_instances(CountryDefinitionManager const& country_definition_manager) { +bool CountryInstanceManager::generate_country_instances( + CountryDefinitionManager const& country_definition_manager, + decltype(CountryInstance::technologies)::keys_t const& technology_keys, + decltype(CountryInstance::inventions)::keys_t const& invention_keys, + decltype(CountryInstance::upper_house)::keys_t const& ideology_keys +) { reserve_more(country_instances, country_definition_manager.get_country_definition_count()); for (CountryDefinition const& country_definition : country_definition_manager.get_country_definitions()) { - country_instances.add_item({ &country_definition }); + country_instances.add_item({ &country_definition, technology_keys, invention_keys, ideology_keys }); } return true; diff --git a/src/openvic-simulation/country/CountryInstance.hpp b/src/openvic-simulation/country/CountryInstance.hpp index 3022b6a..885a5fd 100644 --- a/src/openvic-simulation/country/CountryInstance.hpp +++ b/src/openvic-simulation/country/CountryInstance.hpp @@ -6,9 +6,11 @@ #include "openvic-simulation/military/Leader.hpp" #include "openvic-simulation/military/UnitInstanceGroup.hpp" +#include "openvic-simulation/research/Invention.hpp" +#include "openvic-simulation/research/Technology.hpp" #include "openvic-simulation/types/Date.hpp" -#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp" #include "openvic-simulation/types/IdentifierRegistry.hpp" +#include "openvic-simulation/types/IndexedMap.hpp" #include "openvic-simulation/utility/Getters.hpp" namespace OpenVic { @@ -34,7 +36,7 @@ namespace OpenVic { Religion const* PROPERTY_RW(religion); CountryParty const* PROPERTY_RW(ruling_party); Date PROPERTY_RW(last_election); - fixed_point_map_t PROPERTY(upper_house); + IndexedMap PROPERTY(upper_house); // TODO - should this be ProvinceInstance and/or non-const pointer? // Currently ProvinceDefinition as that's what CountryHistoryEntry has (loaded prior to ProvinceInstance generation) ProvinceDefinition const* PROPERTY_RW(capital); @@ -44,21 +46,27 @@ namespace OpenVic { bool PROPERTY_RW(civilised); fixed_point_t PROPERTY_RW(prestige); std::vector PROPERTY(reforms); // TODO: should be map of reform groups to active reforms: must set defaults & validate applied history + + IndexedMap PROPERTY(technologies); + IndexedMap PROPERTY(inventions); + // TODO: Military units + OOBs; will probably need an extensible deployment class plf::colony PROPERTY(generals); plf::colony PROPERTY(admirals); - CountryInstance(CountryDefinition const* new_country_definition); + CountryInstance( + CountryDefinition const* new_country_definition, decltype(technologies)::keys_t const& technology_keys, + decltype(inventions)::keys_t const& invention_keys, decltype(upper_house)::keys_t const& ideology_keys + ); public: std::string_view get_identifier() const; bool add_accepted_culture(Culture const* new_accepted_culture); bool remove_accepted_culture(Culture const* culture_to_remove); - /* Add or modify a party in the upper house. */ - void add_to_upper_house(Ideology const* party, fixed_point_t popularity); - bool remove_from_upper_house(Ideology const* party); + /* Set a party's popularity in the upper house. */ + bool set_upper_house(Ideology const* ideology, fixed_point_t popularity); bool add_reform(Reform const* new_reform); bool remove_reform(Reform const* reform_to_remove); @@ -83,7 +91,12 @@ namespace OpenVic { IdentifierRegistry IDENTIFIER_REGISTRY(country_instance); public: - bool generate_country_instances(CountryDefinitionManager const& country_definition_manager); + bool generate_country_instances( + CountryDefinitionManager const& country_definition_manager, + decltype(CountryInstance::technologies)::keys_t const& technology_keys, + decltype(CountryInstance::inventions)::keys_t const& invention_keys, + decltype(CountryInstance::upper_house)::keys_t const& ideology_keys + ); bool apply_history_to_countries( CountryHistoryManager const& history_manager, Date date, UnitInstanceManager& unit_instance_manager, -- cgit v1.2.3-56-ga3b1