diff --git a/extension/deps/openvic-simulation b/extension/deps/openvic-simulation index 6c9475fe..d0b3fcbb 160000 --- a/extension/deps/openvic-simulation +++ b/extension/deps/openvic-simulation @@ -1 +1 @@ -Subproject commit 6c9475fe5167f4a462d650cf81ac36ef6e6fddd3 +Subproject commit d0b3fcbbf3ad30d6b5b5d0f17cb9f30b08e2b320 diff --git a/extension/src/openvic-extension/classes/GUILabel.cpp b/extension/src/openvic-extension/classes/GUILabel.cpp index d9b9adf9..c8cac28b 100644 --- a/extension/src/openvic-extension/classes/GUILabel.cpp +++ b/extension/src/openvic-extension/classes/GUILabel.cpp @@ -1,5 +1,7 @@ #include "GUILabel.hpp" +#include + #include #include #include @@ -7,6 +9,8 @@ #include #include +#include + #include "openvic-extension/classes/GUINode.hpp" #include "openvic-extension/core/Convert.hpp" #include "openvic-extension/singletons/AssetManager.hpp" @@ -690,7 +694,7 @@ void GUILabel::separate_currency_segments( GUILabel::flag_segment_t GUILabel::make_flag_segment(String const& identifier) { GameSingleton& game_singleton = *GameSingleton::get_singleton(); - int32_t country_index = -1; + std::optional country_index = std::nullopt; StringName flag_type; InstanceManager* instance_manager = game_singleton.get_instance_manager(); @@ -721,14 +725,14 @@ GUILabel::flag_segment_t GUILabel::make_flag_segment(String const& identifier) { } // If no country with the given identifier can be found, fallback to country index 0 (usually REB) and empty flag type - if (country_index < 0) { + if (!country_index.has_value()) { UtilityFunctions::push_warning( "Failed to find country with identifier \"", identifier, "\" for GUILabel flag segment, falling back to index 0" ); - country_index = 0; + country_index = country_index_t(0); } - const Rect2 flag_image_rect = game_singleton.get_flag_sheet_rect(country_index, flag_type); + const Rect2 flag_image_rect = game_singleton.get_flag_sheet_rect(country_index.value(), flag_type); ERR_FAIL_COND_V(!flag_image_rect.has_area(), {}); flag_segment_t flag_segment; diff --git a/extension/src/openvic-extension/singletons/GameSingleton.cpp b/extension/src/openvic-extension/singletons/GameSingleton.cpp index ded5f680..d88f8ae7 100644 --- a/extension/src/openvic-extension/singletons/GameSingleton.cpp +++ b/extension/src/openvic-extension/singletons/GameSingleton.cpp @@ -1,6 +1,7 @@ #include "GameSingleton.hpp" #include +#include #include #include @@ -8,6 +9,9 @@ #include #include +#include +#include +#include #include #include @@ -203,19 +207,24 @@ TypedArray GameSingleton::get_bookmark_info() const { } Error GameSingleton::setup_game(int32_t bookmark_index) { - Bookmark const* bookmark = - game_manager.get_definition_manager().get_history_manager().get_bookmark_manager().get_bookmark_by_index(bookmark_index - ); + DefinitionManager const& definition_manager = game_manager.get_definition_manager(); + Bookmark const* bookmark = definition_manager.get_history_manager() + .get_bookmark_manager() + .get_bookmark_by_index(bookmark_index_t(bookmark_index)); ERR_FAIL_NULL_V_MSG(bookmark, FAILED, Utilities::format("Failed to get bookmark with index: %d", bookmark_index)); bool ret = game_manager.setup_instance(bookmark); // TODO - remove this temporary crime assignment InstanceManager* instance_manager = get_instance_manager(); ERR_FAIL_NULL_V_MSG(instance_manager, FAILED, "Failed to setup instance manager!"); + + CrimeManager const& crime_manager = definition_manager.get_crime_manager(); for (ProvinceInstance& province : instance_manager->get_map_instance().get_province_instances()) { province.set_crime(get_definition_manager().get_crime_manager().get_crime_modifier_by_index( province.index % get_definition_manager().get_crime_manager().get_crime_modifier_count() )); + const crime_index_t crime_index = crime_index_t(province.index % crime_manager.get_crime_modifier_count()); + province.set_crime(crime_manager.get_crime_modifier_by_index(crime_index)); } ret &= MenuSingleton::get_singleton()->_population_menu_update_provinces() == OK; @@ -314,7 +323,7 @@ Ref GameSingleton::get_flag_sheet_texture() const { return flag_sheet_texture; } -int32_t GameSingleton::get_flag_sheet_index(int32_t country_index, StringName const& flag_type) const { +int32_t GameSingleton::get_flag_sheet_index(country_index_t country_index, StringName const& flag_type) const { ERR_FAIL_COND_V_MSG( country_index < 0 || country_index >= get_definition_manager().get_country_definition_manager().get_country_definition_count(), @@ -324,7 +333,7 @@ int32_t GameSingleton::get_flag_sheet_index(int32_t country_index, StringName co const typename decltype(flag_type_index_map)::const_iterator it = flag_type_index_map.find(flag_type); ERR_FAIL_COND_V_MSG(it == flag_type_index_map.end(), -1, Utilities::format("Invalid flag type %s", flag_type)); - return flag_type_index_map.size() * country_index + it->second; + return flag_type_index_map.size() * type_safe::get(country_index) + it->second; } Rect2i GameSingleton::get_flag_sheet_rect(int32_t flag_index) const { @@ -335,7 +344,7 @@ Rect2i GameSingleton::get_flag_sheet_rect(int32_t flag_index) const { return { Vector2i { flag_index % flag_sheet_dims.x, flag_index / flag_sheet_dims.x } * flag_dims, flag_dims }; } -Rect2i GameSingleton::get_flag_sheet_rect(int32_t country_index, StringName const& flag_type) const { +Rect2i GameSingleton::get_flag_sheet_rect(country_index_t country_index, StringName const& flag_type) const { return get_flag_sheet_rect(get_flag_sheet_index(country_index, flag_type)); } @@ -408,7 +417,7 @@ TypedArray GameSingleton::get_province_names() const { TypedArray ret; ERR_FAIL_COND_V(ret.resize(map_definition.get_province_definition_count()) != OK, {}); - for (int32_t index = 0; index < map_definition.get_province_definition_count(); ++index) { + for (province_index_t index = 0; index < map_definition.get_province_definition_count(); ++index) { ProvinceDefinition const& province = map_definition.get_province_definitions()[index]; Dictionary province_dict; @@ -433,11 +442,14 @@ TypedArray GameSingleton::get_province_names() const { } int32_t GameSingleton::get_mapmode_count() const { - return get_definition_manager().get_mapmode_manager().get_mapmode_count(); + const auto count = get_definition_manager().get_mapmode_manager().get_mapmode_count(); + return type_safe::get(count); } String GameSingleton::get_mapmode_identifier(int32_t index) const { - Mapmode const* identifier_mapmode = get_definition_manager().get_mapmode_manager().get_mapmode_by_index(index); + Mapmode const* identifier_mapmode = get_definition_manager() + .get_mapmode_manager() + .get_mapmode_by_index(map_mode_index_t(index)); if (identifier_mapmode != nullptr) { return convert_to(identifier_mapmode->get_identifier()); } @@ -445,7 +457,9 @@ String GameSingleton::get_mapmode_identifier(int32_t index) const { } String GameSingleton::get_mapmode_localisation_key(int32_t index) const { - Mapmode const* localisation_key_mapmode = get_definition_manager().get_mapmode_manager().get_mapmode_by_index(index); + Mapmode const* localisation_key_mapmode = get_definition_manager() + .get_mapmode_manager() + .get_mapmode_by_index(map_mode_index_t(index)); if (localisation_key_mapmode != nullptr) { return convert_to(localisation_key_mapmode->get_localisation_key()); } @@ -453,15 +467,17 @@ String GameSingleton::get_mapmode_localisation_key(int32_t index) const { } int32_t GameSingleton::get_current_mapmode_index() const { - return mapmode->index; + return type_safe::get(mapmode->index); } Error GameSingleton::set_mapmode(int32_t index) { - Mapmode const* new_mapmode = get_definition_manager().get_mapmode_manager().get_mapmode_by_index(index); + Mapmode const* new_mapmode = get_definition_manager() + .get_mapmode_manager() + .get_mapmode_by_index(map_mode_index_t(index)); ERR_FAIL_NULL_V_MSG(new_mapmode, FAILED, Utilities::format("Failed to find mapmode with index: %d", index)); mapmode = new_mapmode; const Error err = _update_colour_image(); - emit_signal(_signal_mapmode_changed(), mapmode->index); + emit_signal(_signal_mapmode_changed(), typesafe::get(mapmode->index)); return err; } diff --git a/extension/src/openvic-extension/singletons/GameSingleton.hpp b/extension/src/openvic-extension/singletons/GameSingleton.hpp index 167124b9..bb7f3c13 100644 --- a/extension/src/openvic-extension/singletons/GameSingleton.hpp +++ b/extension/src/openvic-extension/singletons/GameSingleton.hpp @@ -6,6 +6,7 @@ #include #include +#include namespace OpenVic { @@ -106,9 +107,9 @@ namespace OpenVic { /* The index of the flag in the flag sheet corresponding to the requested country / flag_type * combination, or -1 if no such flag can be found. */ - int32_t get_flag_sheet_index(int32_t country_index, godot::StringName const& flag_type) const; + int32_t get_flag_sheet_index(country_index_t country_index, godot::StringName const& flag_type) const; godot::Rect2i get_flag_sheet_rect(int32_t flag_index) const; - godot::Rect2i get_flag_sheet_rect(int32_t country_index, godot::StringName const& flag_type) const; + godot::Rect2i get_flag_sheet_rect(country_index_t country_index, godot::StringName const& flag_type) const; /* Number of (vertical, horizontal) subdivisions the province shape image * was split into when making the province_shape_texture to ensure no diff --git a/extension/src/openvic-extension/singletons/MapItemSingleton.cpp b/extension/src/openvic-extension/singletons/MapItemSingleton.cpp index ff2e3ebd..447ca858 100644 --- a/extension/src/openvic-extension/singletons/MapItemSingleton.cpp +++ b/extension/src/openvic-extension/singletons/MapItemSingleton.cpp @@ -226,7 +226,7 @@ PackedByteArray MapItemSingleton::get_rgo_icons() const { } GoodDefinition const* rgo_good = prov_inst.get_rgo_good(); - icons[index++] = rgo_good != nullptr ? rgo_good->index + 1 : 0; // 0 if no rgo good in the province + icons[index++] = rgo_good != nullptr ? type_safe::get(rgo_good->index) + 1 : 0; // 0 if no rgo good in the province } return icons; diff --git a/extension/src/openvic-extension/singletons/MenuSingleton.cpp b/extension/src/openvic-extension/singletons/MenuSingleton.cpp index 0a5c4427..739a3489 100644 --- a/extension/src/openvic-extension/singletons/MenuSingleton.cpp +++ b/extension/src/openvic-extension/singletons/MenuSingleton.cpp @@ -539,7 +539,7 @@ Dictionary MenuSingleton::get_province_info_from_number(int32_t province_number) ProductionType const& production_type = *rgo.get_production_type_nullable(); GoodDefinition const& rgo_good = *province->get_rgo_good(); - ret[province_info_rgo_icon_key] = static_cast(rgo_good.index); + ret[province_info_rgo_icon_key] = type_safe::get(rgo_good.index); ret[province_info_rgo_output_quantity_yesterday_key] = static_cast(rgo.get_output_quantity_yesterday()); ret[province_info_rgo_revenue_yesterday_key] = static_cast(rgo.get_revenue_yesterday()); diff --git a/extension/src/openvic-extension/singletons/PlayerSingleton.cpp b/extension/src/openvic-extension/singletons/PlayerSingleton.cpp index 97ebdc08..cb27a1ae 100644 --- a/extension/src/openvic-extension/singletons/PlayerSingleton.cpp +++ b/extension/src/openvic-extension/singletons/PlayerSingleton.cpp @@ -1,5 +1,7 @@ #include "PlayerSingleton.hpp" +#include + #include #include @@ -98,7 +100,7 @@ void PlayerSingleton::set_player_country(CountryInstance* new_player_country) { if (player_country != nullptr) { instance_manager->queue_game_action( game_action_type_t::GAME_ACTION_SET_AI, - std::pair { player_country->index, true } + std::pair { type_safe::get(player_country->index), true } ); } @@ -107,7 +109,7 @@ void PlayerSingleton::set_player_country(CountryInstance* new_player_country) { if (player_country != nullptr) { instance_manager->queue_game_action( game_action_type_t::GAME_ACTION_SET_AI, - std::pair { player_country->index, false } + std::pair { type_safe::get(player_country->index), false } ); } @@ -150,21 +152,21 @@ void PlayerSingleton::set_selected_province(ProvinceInstance const* new_selected } void PlayerSingleton::set_selected_province_by_number(int32_t province_number) { - if (province_number == ProvinceDefinition::NULL_INDEX) { + MapInstance const& map_instance = instance_manager->get_map_instance(); + const province_index_t province_index = ProvinceDefinition::get_index_from_province_number(province_number); + if (province_index == ProvinceDefinition::NULL_INDEX) { unset_selected_province(); } else { - InstanceManager const* instance_manager = GameSingleton::get_singleton()->get_instance_manager(); - ERR_FAIL_NULL(instance_manager); - - MapInstance const& map_instance = instance_manager->get_map_instance(); - - set_selected_province(map_instance.get_province_instance_from_number(province_number)); - + ProvinceInstance const* const selected_province = map_instance.get_province_instance_from_index(province_index); if (selected_province == nullptr) { spdlog::error_s( "Trying to set selected province to an invalid number {} (max number is {})", map_instance.get_province_instance_by_definition().get_count(), province_number ); + } else { + InstanceManager const* instance_manager = GameSingleton::get_singleton()->get_instance_manager(); + ERR_FAIL_NULL(instance_manager); + set_selected_province(selected_province); } } } @@ -217,7 +219,7 @@ void PlayerSingleton::expand_selected_province_building(int32_t building_index) instance_manager->queue_game_action( game_action_type_t::GAME_ACTION_EXPAND_PROVINCE_BUILDING, - std::pair { selected_province->index, building_index } + std::pair { type_safe::get(selected_province->index), building_index } ); } @@ -229,7 +231,7 @@ void PlayerSingleton::set_##value_name##_slider_value(fixed_point_t const value) } \ GameSingleton::get_singleton()->get_instance_manager()->queue_game_action( \ game_action_type_t::GAME_ACTION_SET_##game_action_name, \ - std::pair { player_country->index, value } \ + std::pair { type_safe::get(player_country->index), value } \ ); \ } @@ -250,7 +252,7 @@ void PlayerSingleton::set_strata_tax_rate_slider_value(Strata const& strata, fix } GameSingleton::get_singleton()->get_instance_manager()->queue_game_action( game_action_type_t::GAME_ACTION_SET_STRATA_TAX, - std::tuple { player_country->index, strata.index, value } + std::tuple { type_safe::get(player_country->index), strata.index, value } ); } @@ -269,7 +271,7 @@ void PlayerSingleton::set_good_automated(int32_t good_index, bool is_automated) instance_manager->queue_game_action( game_action_type_t::GAME_ACTION_SET_GOOD_AUTOMATED, - std::tuple { player_country->index, good_index, is_automated } + std::tuple { type_safe::get(player_country->index), good_index, is_automated } ); } @@ -282,7 +284,7 @@ void PlayerSingleton::set_good_trade_order(int32_t good_index, bool is_selling, instance_manager->queue_game_action( game_action_type_t::GAME_ACTION_SET_GOOD_TRADE_ORDER, std::tuple { - player_country->index, good_index, is_selling, + type_safe::get(player_country->index), good_index, is_selling, MenuSingleton::calculate_trade_menu_stockpile_cutoff_amount_fp(amount_slider->get_value_scaled_fp()) } ); @@ -299,7 +301,7 @@ void PlayerSingleton::create_leader(bool is_general) const { instance_manager->queue_game_action( game_action_type_t::GAME_ACTION_CREATE_LEADER, - std::pair { player_country->index, is_general } + std::pair { type_safe::get(player_country->index), is_general } ); } @@ -321,7 +323,7 @@ void PlayerSingleton::set_auto_create_leaders(bool value) const { instance_manager->queue_game_action( game_action_type_t::GAME_ACTION_SET_AUTO_CREATE_LEADERS, - std::pair { player_country->index, value } + std::pair { type_safe::get(player_country->index), value } ); } @@ -333,7 +335,7 @@ void PlayerSingleton::set_auto_assign_leaders(bool value) const { instance_manager->queue_game_action( game_action_type_t::GAME_ACTION_SET_AUTO_ASSIGN_LEADERS, - std::pair { player_country->index, value } + std::pair { type_safe::get(player_country->index), value } ); } @@ -345,6 +347,6 @@ void PlayerSingleton::set_mobilise(bool value) const { instance_manager->queue_game_action( game_action_type_t::GAME_ACTION_SET_MOBILISE, - std::pair { player_country->index, value } + std::pair { type_safe::get(player_country->index), value } ); } diff --git a/extension/src/openvic-extension/singletons/PopulationMenu.cpp b/extension/src/openvic-extension/singletons/PopulationMenu.cpp index 53a041ea..402f70a1 100644 --- a/extension/src/openvic-extension/singletons/PopulationMenu.cpp +++ b/extension/src/openvic-extension/singletons/PopulationMenu.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include "openvic-extension/classes/GFXPieChartTexture.hpp" #include "openvic-extension/classes/GUINode.hpp" @@ -261,7 +262,7 @@ Error MenuSingleton::population_menu_select_province_list_entry(int32_t select_i } Error MenuSingleton::population_menu_select_province(int32_t province_number) { - const ProvinceDefinition::index_t province_index = ProvinceDefinition::get_index_from_province_number(province_number); + const province_index_t province_index = ProvinceDefinition::get_index_from_province_number(province_number); GameSingleton const* game_singleton = GameSingleton::get_singleton(); ERR_FAIL_NULL_V(game_singleton, FAILED); InstanceManager const* instance_manager = game_singleton->get_instance_manager(); @@ -276,7 +277,7 @@ Error MenuSingleton::population_menu_select_province(int32_t province_number) { MenuSingleton& menu_singleton; - const ProvinceDefinition::index_t _province_index = 0; + const province_index_t _province_index = province_index_t(0); int32_t index = 0; diff --git a/extension/src/openvic-extension/singletons/TradeMenu.cpp b/extension/src/openvic-extension/singletons/TradeMenu.cpp index ed75eaf4..502736f4 100644 --- a/extension/src/openvic-extension/singletons/TradeMenu.cpp +++ b/extension/src/openvic-extension/singletons/TradeMenu.cpp @@ -127,8 +127,8 @@ Dictionary MenuSingleton::get_trade_menu_trade_details_info( InstanceManager const* instance_manager = GameSingleton::get_singleton()->get_instance_manager(); ERR_FAIL_NULL_V(instance_manager, {}); - GoodInstance const* good_instance = - instance_manager->get_good_instance_manager().get_good_instance_by_index(trade_detail_good_index); + GoodInstance const* good_instance = instance_manager->get_good_instance_manager() + .get_good_instance_by_index(good_index_t(trade_detail_good_index)); ERR_FAIL_NULL_V(good_instance, {}); CountryInstance const* country = PlayerSingleton::get_singleton()->get_player_country();