diff options
author | hop311 <hop3114@gmail.com> | 2024-07-17 01:01:03 +0200 |
---|---|---|
committer | hop311 <hop3114@gmail.com> | 2024-07-17 01:01:03 +0200 |
commit | 3eac39d9dbf64524c11a51989707521ed82474b5 (patch) | |
tree | c953768a21cec2a2834813d170562a9b9a2cd722 /extension/src | |
parent | 114394f4d1bcf5249089b6e3632d259a195ec584 (diff) |
Use IndexedMap + helper function supportindexed-map
Diffstat (limited to 'extension/src')
5 files changed, 67 insertions, 28 deletions
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<std::derived_from<HasIdentifierAndColour> T> - static godot_pie_chart_data_t distribution_to_slices_array(fixed_point_map_t<T const*> const& dist) { + template<typename Container> + static godot_pie_chart_data_t distribution_to_slices_array(Container const& dist) + requires( + ( + /* fixed_point_map_t<T const*>, T derived from HasIdentifierAndColour */ + utility::is_specialization_of_v<Container, tsl::ordered_map> && + std::derived_from<std::remove_pointer_t<typename Container::key_type>, HasIdentifierAndColour> && + std::is_same_v<typename Container::mapped_type, fixed_point_t> + ) || ( + /* IndexedMap<T, fixed_point_t>, T derived from HasIdentifierAndColour */ + utility::is_specialization_of_v<Container, IndexedMap> && + std::derived_from<typename Container::key_t, HasIdentifierAndColour> && + std::is_same_v<typename Container::value_t, fixed_point_t> + ) + ) { using namespace godot; - using entry_t = std::pair<T const*, fixed_point_t>; + using entry_t = std::pair<HasIdentifierAndColour const*, fixed_point_t>; std::vector<entry_t> 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<Container, tsl::ordered_map>) { + 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<PopType const*> 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<Ideology const*> 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<Culture const*> 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<CountryDefinition const*> 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 <godot_cpp/variant/utility_functions.hpp> +#include <openvic-simulation/map/ProvinceInstance.hpp> + #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<utility::is_derived_from_specialization_of<UnitInstanceGroup> T> -bool ModelSingleton::add_unit_dict(ordered_set<T*> const& units, TypedArray<Dictionary>& unit_array) const { +template<UnitType::branch_t Branch> +bool ModelSingleton::add_unit_dict( + ordered_set<UnitInstanceGroupBranched<Branch>*> const& units, TypedArray<Dictionary>& unit_array +) const { + using _UnitInstanceGroup = UnitInstanceGroupBranched<Branch>; + 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<T*> const& units, TypedArray<Dict bool ret = true; /* Last unit to enter the province is shown on top. */ - T const& unit = *units.back(); + _UnitInstanceGroup const& unit = *units.back(); ERR_FAIL_COND_V_MSG(unit.empty(), false, vformat("Empty unit \"%s\"", std_view_to_godot_string(unit.get_name()))); CountryDefinition const* country = unit.get_country()->get_country_definition(); @@ -220,7 +226,7 @@ bool ModelSingleton::add_unit_dict(ordered_set<T*> const& units, TypedArray<Dict std::string_view actor_name = display_unit_type->get_sprite(); std::string_view mount_actor_name, mount_attach_node_name; - if constexpr (std::same_as<T, ArmyInstance>) { + if constexpr (Branch == UnitType::branch_t::LAND) { RegimentType const* regiment_type = reinterpret_cast<RegimentType const*>(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 <godot_cpp/classes/object.hpp> #include <openvic-simulation/interface/GFXObject.hpp> -#include <openvic-simulation/military/UnitInstance.hpp> +#include <openvic-simulation/military/UnitInstanceGroup.hpp> +#include <openvic-simulation/types/OrderedContainers.hpp> 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<utility::is_derived_from_specialization_of<UnitInstanceGroup> T> - bool add_unit_dict(ordered_set<T*> const& units, godot::TypedArray<godot::Dictionary>& unit_array) const; + template<UnitType::branch_t Branch> + bool add_unit_dict( + ordered_set<UnitInstanceGroupBranched<Branch>*> const& units, godot::TypedArray<godot::Dictionary>& 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<HasIdentifierAndColour>(pop->get_ideologies() * static_cast<int32_t>(pop->get_size())); + pop->get_ideologies() * static_cast<fixed_point_t>(static_cast<int32_t>(pop->get_size())); population_menu.distributions[3][&pop->get_culture()] += pop->get_size(); population_menu.distributions[4] += cast_map<HasIdentifierAndColour>(pop->get_issues() * static_cast<int32_t>(pop->get_size())); population_menu.distributions[5] += - cast_map<HasIdentifierAndColour>(pop->get_votes() * static_cast<int32_t>(pop->get_size())); + pop->get_votes() * static_cast<fixed_point_t>(static_cast<int32_t>(pop->get_size())); } for (fixed_point_map_t<HasIdentifierAndColour const*>& 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 { |