diff options
author | Hop311 <Hop3114@gmail.com> | 2024-07-17 14:01:19 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-17 14:01:19 +0200 |
commit | 2d111ea003e975ea1adbcd7e4d903f760f1daa07 (patch) | |
tree | fae6a1086f3ae698c4fb3f18340c5ed5f580c889 /src/openvic-simulation/country | |
parent | e8a3b33f13ebdf3a388b4996308b4db9763dc375 (diff) | |
parent | f83e869def6608f64606aead24ad1cfbb6f5c72a (diff) |
Merge pull request #174 from OpenVicProject/indexed-map
Add IndexedMap and use in low key count, high value density cases
Diffstat (limited to 'src/openvic-simulation/country')
4 files changed, 61 insertions, 32 deletions
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<CountryParty>&& 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<UnitType const*, name_list_t>; using government_colour_map_t = ordered_map<GovernmentType const*, colour_t>; 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<CountryParty>&& 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<Ideology const*> PROPERTY(upper_house); + IndexedMap<Ideology, fixed_point_t> 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<Reform const*> PROPERTY(reforms); // TODO: should be map of reform groups to active reforms: must set defaults & validate applied history + + IndexedMap<Technology, bool> PROPERTY(technologies); + IndexedMap<Invention, bool> PROPERTY(inventions); + // TODO: Military units + OOBs; will probably need an extensible deployment class plf::colony<General> PROPERTY(generals); plf::colony<Admiral> 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<CountryInstance> 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, |