From 3eac39d9dbf64524c11a51989707521ed82474b5 Mon Sep 17 00:00:00 2001 From: hop311 Date: Wed, 17 Jul 2024 00:01:03 +0100 Subject: Use IndexedMap + helper function support --- extension/deps/openvic-simulation | 2 +- .../classes/GFXPieChartTexture.hpp | 45 +++++++++++++++++----- .../openvic-extension/singletons/MenuSingleton.cpp | 21 +++++----- .../singletons/ModelSingleton.cpp | 14 +++++-- .../singletons/ModelSingleton.hpp | 9 +++-- .../singletons/PopulationMenu.cpp | 6 +-- 6 files changed, 68 insertions(+), 29 deletions(-) (limited to 'extension') diff --git a/extension/deps/openvic-simulation b/extension/deps/openvic-simulation index 9479a87..f83e869 160000 --- a/extension/deps/openvic-simulation +++ b/extension/deps/openvic-simulation @@ -1 +1 @@ -Subproject commit 9479a873095e23503994a0ef3b675b06320df4b9 +Subproject commit f83e869def6608f64606aead24ad1cfbb6f5c72a diff --git a/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp b/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp index 8683b10..3135050 100644 --- a/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp +++ b/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp @@ -38,23 +38,50 @@ namespace OpenVic { /* Generate slice data from a distribution of HasIdentifierAndColour derived objects, sorted by their weight. * The resulting Array of Dictionaries can be used as an argument for set_slices_array. */ - template T> - static godot_pie_chart_data_t distribution_to_slices_array(fixed_point_map_t const& dist) { + template + static godot_pie_chart_data_t distribution_to_slices_array(Container const& dist) + requires( + ( + /* fixed_point_map_t, T derived from HasIdentifierAndColour */ + utility::is_specialization_of_v && + std::derived_from, HasIdentifierAndColour> && + std::is_same_v + ) || ( + /* IndexedMap, T derived from HasIdentifierAndColour */ + utility::is_specialization_of_v && + std::derived_from && + std::is_same_v + ) + ) { using namespace godot; - using entry_t = std::pair; + using entry_t = std::pair; std::vector sorted_dist; - sorted_dist.reserve(dist.size()); - for (entry_t const& entry : dist) { - ERR_CONTINUE_MSG( - entry.first == nullptr, vformat("Null distribution key with value %f", entry.second.to_float()) - ); - sorted_dist.push_back(entry); + + if constexpr (utility::is_specialization_of_v) { + sorted_dist.reserve(dist.size()); + for (entry_t const& entry : dist) { + ERR_CONTINUE_MSG( + entry.first == nullptr, vformat("Null distribution key with value %f", entry.second.to_float()) + ); + sorted_dist.push_back(entry); + } + } else { + for (size_t index = 0; index < dist.size(); ++index) { + fixed_point_t const& value = dist[index]; + if (value != 0) { + HasIdentifierAndColour const* key = &dist(index); + sorted_dist.emplace_back(key, value); + } + } } + std::sort(sorted_dist.begin(), sorted_dist.end(), [](entry_t const& lhs, entry_t const& rhs) -> bool { return lhs.first < rhs.first; }); + godot_pie_chart_data_t array; ERR_FAIL_COND_V(array.resize(sorted_dist.size()) != OK, {}); + for (size_t idx = 0; idx < array.size(); ++idx) { auto const& [key, val] = sorted_dist[idx]; Dictionary sub_dict; diff --git a/extension/src/openvic-extension/singletons/MenuSingleton.cpp b/extension/src/openvic-extension/singletons/MenuSingleton.cpp index b4baf40..36e5f58 100644 --- a/extension/src/openvic-extension/singletons/MenuSingleton.cpp +++ b/extension/src/openvic-extension/singletons/MenuSingleton.cpp @@ -226,19 +226,22 @@ Dictionary MenuSingleton::get_province_info_from_index(int32_t index) const { ret[province_info_total_population_key] = province->get_total_population(); - fixed_point_map_t const& pop_types = province->get_pop_type_distribution(); - if (!pop_types.empty()) { - ret[province_info_pop_types_key] = GFXPieChartTexture::distribution_to_slices_array(pop_types); + GFXPieChartTexture::godot_pie_chart_data_t pop_types = + GFXPieChartTexture::distribution_to_slices_array(province->get_pop_type_distribution()); + if (!pop_types.is_empty()) { + ret[province_info_pop_types_key] = std::move(pop_types); } - fixed_point_map_t const& ideologies = province->get_ideology_distribution(); - if (!ideologies.empty()) { - ret[province_info_pop_ideologies_key] = GFXPieChartTexture::distribution_to_slices_array(ideologies); + GFXPieChartTexture::godot_pie_chart_data_t ideologies = + GFXPieChartTexture::distribution_to_slices_array(province->get_ideology_distribution()); + if (!ideologies.is_empty()) { + ret[province_info_pop_ideologies_key] = std::move(ideologies); } - fixed_point_map_t const& cultures = province->get_culture_distribution(); - if (!cultures.empty()) { - ret[province_info_pop_cultures_key] = GFXPieChartTexture::distribution_to_slices_array(cultures); + GFXPieChartTexture::godot_pie_chart_data_t cultures = + GFXPieChartTexture::distribution_to_slices_array(province->get_culture_distribution()); + if (!cultures.is_empty()) { + ret[province_info_pop_cultures_key] = std::move(cultures); } std::vector const& cores = province->get_cores(); diff --git a/extension/src/openvic-extension/singletons/ModelSingleton.cpp b/extension/src/openvic-extension/singletons/ModelSingleton.cpp index 7dea1bd..5fb9cf8 100644 --- a/extension/src/openvic-extension/singletons/ModelSingleton.cpp +++ b/extension/src/openvic-extension/singletons/ModelSingleton.cpp @@ -4,6 +4,8 @@ #include +#include + #include "openvic-extension/singletons/GameSingleton.hpp" #include "openvic-extension/utility/ClassBindings.hpp" #include "openvic-extension/utility/Utilities.hpp" @@ -180,8 +182,12 @@ Dictionary ModelSingleton::make_model_dict(GFX::Actor const& actor) const { /* Returns false if an error occurs while trying to add a unit model for the province, true otherwise. * Returning true doesn't necessarily mean a unit was added, e.g. when units is empty. */ -template T> -bool ModelSingleton::add_unit_dict(ordered_set const& units, TypedArray& unit_array) const { +template +bool ModelSingleton::add_unit_dict( + ordered_set*> const& units, TypedArray& unit_array +) const { + using _UnitInstanceGroup = UnitInstanceGroupBranched; + GameSingleton const* game_singleton = GameSingleton::get_singleton(); ERR_FAIL_NULL_V(game_singleton, false); @@ -204,7 +210,7 @@ bool ModelSingleton::add_unit_dict(ordered_set const& units, TypedArrayget_country_definition(); @@ -220,7 +226,7 @@ bool ModelSingleton::add_unit_dict(ordered_set const& units, TypedArrayget_sprite(); std::string_view mount_actor_name, mount_attach_node_name; - if constexpr (std::same_as) { + if constexpr (Branch == UnitType::branch_t::LAND) { RegimentType const* regiment_type = reinterpret_cast(display_unit_type); if (!regiment_type->get_sprite_override().empty()) { diff --git a/extension/src/openvic-extension/singletons/ModelSingleton.hpp b/extension/src/openvic-extension/singletons/ModelSingleton.hpp index ad1a6d8..8030ffd 100644 --- a/extension/src/openvic-extension/singletons/ModelSingleton.hpp +++ b/extension/src/openvic-extension/singletons/ModelSingleton.hpp @@ -3,7 +3,8 @@ #include #include -#include +#include +#include namespace OpenVic { struct BuildingInstance; @@ -31,8 +32,10 @@ namespace OpenVic { godot::Dictionary make_animation_dict(GFX::Actor::Animation const& animation) const; godot::Dictionary make_model_dict(GFX::Actor const& actor) const; - template T> - bool add_unit_dict(ordered_set const& units, godot::TypedArray& unit_array) const; + template + bool add_unit_dict( + ordered_set*> const& units, godot::TypedArray& unit_array + ) const; bool add_building_dict( BuildingInstance const& building, ProvinceInstance const& province, diff --git a/extension/src/openvic-extension/singletons/PopulationMenu.cpp b/extension/src/openvic-extension/singletons/PopulationMenu.cpp index 1237e86..db2ac53 100644 --- a/extension/src/openvic-extension/singletons/PopulationMenu.cpp +++ b/extension/src/openvic-extension/singletons/PopulationMenu.cpp @@ -407,12 +407,12 @@ void MenuSingleton::_population_menu_update_filtered_pops() { population_menu.distributions[0][&pop->get_type()] += pop->get_size(); population_menu.distributions[1][&pop->get_religion()] += pop->get_size(); population_menu.distributions[2] += - cast_map(pop->get_ideologies() * static_cast(pop->get_size())); + pop->get_ideologies() * static_cast(static_cast(pop->get_size())); population_menu.distributions[3][&pop->get_culture()] += pop->get_size(); population_menu.distributions[4] += cast_map(pop->get_issues() * static_cast(pop->get_size())); population_menu.distributions[5] += - cast_map(pop->get_votes() * static_cast(pop->get_size())); + pop->get_votes() * static_cast(static_cast(pop->get_size())); } for (fixed_point_map_t& distribution : population_menu.distributions) { @@ -467,7 +467,7 @@ MenuSingleton::sort_func_t MenuSingleton::_get_population_menu_sort_func(populat }; case SORT_IDEOLOGY: return [](Pop const* a, Pop const* b) -> bool { - return sorted_fixed_map_less_than(a->get_ideologies(), b->get_ideologies()); + return sorted_indexed_map_less_than(a->get_ideologies(), b->get_ideologies()); }; case SORT_ISSUES: return [](Pop const* a, Pop const* b) -> bool { -- cgit v1.2.3-56-ga3b1