Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add country and province instance indices + country_definition_to_instance_map #307

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/openvic-simulation/country/CountryInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 },
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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;
}
Expand Down
5 changes: 4 additions & 1 deletion src/openvic-simulation/country/CountryInstance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/*
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -530,6 +531,8 @@ namespace OpenVic {

IdentifierRegistry<CountryInstance> IDENTIFIER_REGISTRY(country_instance);

IndexedMap<CountryDefinition, CountryInstance*> PROPERTY(country_definition_to_instance_map);

std::vector<CountryInstance*> PROPERTY(great_powers);
std::vector<CountryInstance*> PROPERTY(secondary_powers);

Expand Down
3 changes: 1 addition & 2 deletions src/openvic-simulation/map/MapInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 2 additions & 4 deletions src/openvic-simulation/map/Mapmode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/openvic-simulation/map/ProvinceInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() },
Expand Down
2 changes: 1 addition & 1 deletion src/openvic-simulation/map/ProvinceInstance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace OpenVic {
using ArmyInstance = UnitInstanceGroupBranched<UnitType::branch_t::LAND>;
using NavyInstance = UnitInstanceGroupBranched<UnitType::branch_t::NAVAL>;

struct ProvinceInstance : HasIdentifierAndColour, FlagStrings {
struct ProvinceInstance : HasIdentifierAndColour, HasIndex<>, FlagStrings {
friend struct MapInstance;

using life_rating_t = int8_t;
Expand Down
Loading