diff --git a/src/openvic-simulation/country/CountryInstance.cpp b/src/openvic-simulation/country/CountryInstance.cpp index 67213d48..872bc7d4 100644 --- a/src/openvic-simulation/country/CountryInstance.cpp +++ b/src/openvic-simulation/country/CountryInstance.cpp @@ -25,6 +25,7 @@ static constexpr colour_t ERROR_COLOUR = colour_t::from_integer(0xFF0000); CountryInstance::CountryInstance( CountryDefinition const* new_country_definition, + index_t new_index, decltype(building_type_unlock_levels)::keys_type const& building_type_keys, decltype(technology_unlock_levels)::keys_type const& technology_keys, decltype(invention_unlock_levels)::keys_type const& invention_keys, @@ -39,6 +40,7 @@ CountryInstance::CountryInstance( decltype(tax_rate_by_strata)::keys_type const& strata_keys, GoodInstanceManager& good_instance_manager ) : FlagStrings { "country" }, + HasIndex { new_index }, /* Main attributes */ country_definition { new_country_definition }, colour { ERROR_COLOUR }, @@ -1517,14 +1519,15 @@ void CountryInstanceManager::update_rankings(Date today, DefineManager const& de } CountryInstanceManager::CountryInstanceManager(CountryDefinitionManager const& new_country_definition_manager) - : country_definition_manager { new_country_definition_manager } {} + : country_definition_manager { new_country_definition_manager }, + country_definition_to_instance_map { &new_country_definition_manager.get_country_definitions() } {} CountryInstance& CountryInstanceManager::get_country_instance_from_definition(CountryDefinition const& country) { - return country_instances.get_items()[country.get_index()]; + return *country_definition_to_instance_map[country]; } CountryInstance const& CountryInstanceManager::get_country_instance_from_definition(CountryDefinition const& country) const { - return country_instances.get_items()[country.get_index()]; + return *country_definition_to_instance_map[country]; } bool CountryInstanceManager::generate_country_instances( @@ -1549,6 +1552,7 @@ bool CountryInstanceManager::generate_country_instances( for (CountryDefinition const& country_definition : country_definition_manager.get_country_definitions()) { if (country_instances.add_item({ &country_definition, + get_country_instance_count(), building_type_keys, technology_keys, invention_keys, @@ -1567,6 +1571,8 @@ bool CountryInstanceManager::generate_country_instances( // after changing between its constructor call and now due to being std::move'd into the registry. CountryInstance& country_instance = get_back_country_instance(); country_instance.modifier_sum.set_this_source(&country_instance); + + country_definition_to_instance_map[country_definition] = &country_instance; } else { ret = false; } diff --git a/src/openvic-simulation/country/CountryInstance.hpp b/src/openvic-simulation/country/CountryInstance.hpp index 18c7aeb7..08b593fc 100644 --- a/src/openvic-simulation/country/CountryInstance.hpp +++ b/src/openvic-simulation/country/CountryInstance.hpp @@ -48,7 +48,7 @@ namespace OpenVic { /* 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. */ - struct CountryInstance : FlagStrings { + struct CountryInstance : FlagStrings, HasIndex<> { friend struct CountryInstanceManager; /* @@ -270,6 +270,7 @@ namespace OpenVic { CountryInstance( CountryDefinition const* new_country_definition, + index_t new_index, decltype(building_type_unlock_levels)::keys_type const& building_type_keys, decltype(technology_unlock_levels)::keys_type const& technology_keys, decltype(invention_unlock_levels)::keys_type const& invention_keys, @@ -530,6 +531,8 @@ namespace OpenVic { IdentifierRegistry IDENTIFIER_REGISTRY(country_instance); + IndexedMap PROPERTY(country_definition_to_instance_map); + std::vector PROPERTY(great_powers); std::vector PROPERTY(secondary_powers); diff --git a/src/openvic-simulation/map/MapInstance.cpp b/src/openvic-simulation/map/MapInstance.cpp index 31a62f6e..19414525 100644 --- a/src/openvic-simulation/map/MapInstance.cpp +++ b/src/openvic-simulation/map/MapInstance.cpp @@ -32,8 +32,7 @@ void MapInstance::set_selected_province(ProvinceDefinition::index_t index) { } ProvinceDefinition::index_t MapInstance::get_selected_province_index() const { - return selected_province != nullptr ? selected_province->get_province_definition().get_index() - : ProvinceDefinition::NULL_INDEX; + return selected_province != nullptr ? selected_province->get_index() : ProvinceDefinition::NULL_INDEX; } bool MapInstance::setup( diff --git a/src/openvic-simulation/map/Mapmode.cpp b/src/openvic-simulation/map/Mapmode.cpp index bf9e3645..8a08eaa3 100644 --- a/src/openvic-simulation/map/Mapmode.cpp +++ b/src/openvic-simulation/map/Mapmode.cpp @@ -77,8 +77,7 @@ bool MapmodeManager::generate_mapmode_colours(MapInstance const& map_instance, M if (map_instance.province_instances_are_locked()) { for (ProvinceInstance const& province : map_instance.get_province_instances()) { - target_stripes[province.get_province_definition().get_index()] = - mapmode->get_base_stripe_colours(map_instance, province); + target_stripes[province.get_index()] = mapmode->get_base_stripe_colours(map_instance, province); } } else { for ( @@ -256,8 +255,7 @@ bool MapmodeManager::setup_mapmodes() { "mapmode_index", [](MapInstance const& map_instance, ProvinceInstance const& province) -> Mapmode::base_stripe_t { const colour_argb_t::value_type f = colour_argb_t::colour_traits::component_from_fraction( - province.get_province_definition().get_index(), - map_instance.get_map_definition().get_province_definition_count() + 1 + province.get_index(), map_instance.get_map_definition().get_province_definition_count() + 1 ); return colour_argb_t::fill_as(f).with_alpha(ALPHA_VALUE); } diff --git a/src/openvic-simulation/map/ProvinceInstance.cpp b/src/openvic-simulation/map/ProvinceInstance.cpp index 8ff3492c..86829146 100644 --- a/src/openvic-simulation/map/ProvinceInstance.cpp +++ b/src/openvic-simulation/map/ProvinceInstance.cpp @@ -25,6 +25,7 @@ ProvinceInstance::ProvinceInstance( decltype(pop_type_distribution)::keys_type const& pop_type_keys, decltype(ideology_distribution)::keys_type const& ideology_keys ) : HasIdentifierAndColour { new_province_definition }, + HasIndex { new_province_definition.get_index() }, FlagStrings { "province" }, province_definition { new_province_definition }, terrain_type { new_province_definition.get_default_terrain_type() }, diff --git a/src/openvic-simulation/map/ProvinceInstance.hpp b/src/openvic-simulation/map/ProvinceInstance.hpp index 28a272cb..51bd402f 100644 --- a/src/openvic-simulation/map/ProvinceInstance.hpp +++ b/src/openvic-simulation/map/ProvinceInstance.hpp @@ -42,7 +42,7 @@ namespace OpenVic { using ArmyInstance = UnitInstanceGroupBranched; using NavyInstance = UnitInstanceGroupBranched; - struct ProvinceInstance : HasIdentifierAndColour, FlagStrings { + struct ProvinceInstance : HasIdentifierAndColour, HasIndex<>, FlagStrings { friend struct MapInstance; using life_rating_t = int8_t;