Skip to content

Commit 8e0f3ba

Browse files
committed
Use typed indices
1 parent da0efe6 commit 8e0f3ba

8 files changed

Lines changed: 68 additions & 44 deletions

File tree

extension/src/openvic-extension/classes/GUILabel.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
#include "GUILabel.hpp"
22

3+
#include <optional>
4+
35
#include <godot_cpp/classes/font_file.hpp>
46
#include <godot_cpp/classes/style_box_texture.hpp>
57
#include <godot_cpp/core/error_macros.hpp>
68
#include <godot_cpp/variant/node_path.hpp>
79
#include <godot_cpp/variant/string_name.hpp>
810
#include <godot_cpp/variant/utility_functions.hpp>
911

12+
#include <openvic-simulation/types/TypedIndices.hpp>
13+
1014
#include "openvic-extension/classes/GUINode.hpp"
1115
#include "openvic-extension/core/Convert.hpp"
1216
#include "openvic-extension/singletons/AssetManager.hpp"
@@ -690,7 +694,7 @@ void GUILabel::separate_currency_segments(
690694
GUILabel::flag_segment_t GUILabel::make_flag_segment(String const& identifier) {
691695
GameSingleton& game_singleton = *GameSingleton::get_singleton();
692696

693-
int32_t country_index = -1;
697+
std::optional<country_index_t> country_index = std::nullopt;
694698
StringName flag_type;
695699

696700
InstanceManager* instance_manager = game_singleton.get_instance_manager();
@@ -721,14 +725,14 @@ GUILabel::flag_segment_t GUILabel::make_flag_segment(String const& identifier) {
721725
}
722726

723727
// If no country with the given identifier can be found, fallback to country index 0 (usually REB) and empty flag type
724-
if (country_index < 0) {
728+
if (!country_index.has_value()) {
725729
UtilityFunctions::push_warning(
726730
"Failed to find country with identifier \"", identifier, "\" for GUILabel flag segment, falling back to index 0"
727731
);
728-
country_index = 0;
732+
country_index = country_index_t(0);
729733
}
730734

731-
const Rect2 flag_image_rect = game_singleton.get_flag_sheet_rect(country_index, flag_type);
735+
const Rect2 flag_image_rect = game_singleton.get_flag_sheet_rect(country_index.value(), flag_type);
732736
ERR_FAIL_COND_V(!flag_image_rect.has_area(), {});
733737

734738
flag_segment_t flag_segment;

extension/src/openvic-extension/singletons/GameSingleton.cpp

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
#include "GameSingleton.hpp"
22

33
#include <functional>
4+
#include <type_safe/strong_typedef.hpp>
45

56
#include <godot_cpp/classes/time.hpp>
67
#include <godot_cpp/core/error_macros.hpp>
78
#include <godot_cpp/variant/packed_string_array.hpp>
89
#include <godot_cpp/variant/utility_functions.hpp>
910

1011
#include <openvic-simulation/dataloader/ModManager.hpp>
12+
#include <openvic-simulation/DefinitionManager.hpp>
13+
#include <openvic-simulation/map/Crime.hpp>
14+
#include <openvic-simulation/types/TypedIndices.hpp>
1115
#include <openvic-simulation/utility/Containers.hpp>
1216
#include <openvic-simulation/utility/Logger.hpp>
1317

@@ -203,19 +207,24 @@ TypedArray<Dictionary> GameSingleton::get_bookmark_info() const {
203207
}
204208

205209
Error GameSingleton::setup_game(int32_t bookmark_index) {
206-
Bookmark const* bookmark =
207-
game_manager.get_definition_manager().get_history_manager().get_bookmark_manager().get_bookmark_by_index(bookmark_index
208-
);
210+
DefinitionManager const& definition_manager = game_manager.get_definition_manager();
211+
Bookmark const* bookmark = definition_manager.get_history_manager()
212+
.get_bookmark_manager()
213+
.get_bookmark_by_index(bookmark_index_t(bookmark_index));
209214
ERR_FAIL_NULL_V_MSG(bookmark, FAILED, Utilities::format("Failed to get bookmark with index: %d", bookmark_index));
210215
bool ret = game_manager.setup_instance(bookmark);
211216

212217
// TODO - remove this temporary crime assignment
213218
InstanceManager* instance_manager = get_instance_manager();
214219
ERR_FAIL_NULL_V_MSG(instance_manager, FAILED, "Failed to setup instance manager!");
220+
221+
CrimeManager const& crime_manager = definition_manager.get_crime_manager();
215222
for (ProvinceInstance& province : instance_manager->get_map_instance().get_province_instances()) {
216223
province.set_crime(get_definition_manager().get_crime_manager().get_crime_modifier_by_index(
217224
province.index % get_definition_manager().get_crime_manager().get_crime_modifier_count()
218225
));
226+
const crime_index_t crime_index = crime_index_t(province.index % crime_manager.get_crime_modifier_count());
227+
province.set_crime(crime_manager.get_crime_modifier_by_index(crime_index));
219228
}
220229

221230
ret &= MenuSingleton::get_singleton()->_population_menu_update_provinces() == OK;
@@ -314,7 +323,7 @@ Ref<ImageTexture> GameSingleton::get_flag_sheet_texture() const {
314323
return flag_sheet_texture;
315324
}
316325

317-
int32_t GameSingleton::get_flag_sheet_index(int32_t country_index, StringName const& flag_type) const {
326+
int32_t GameSingleton::get_flag_sheet_index(country_index_t country_index, StringName const& flag_type) const {
318327
ERR_FAIL_COND_V_MSG(
319328
country_index < 0 ||
320329
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
324333
const typename decltype(flag_type_index_map)::const_iterator it = flag_type_index_map.find(flag_type);
325334
ERR_FAIL_COND_V_MSG(it == flag_type_index_map.end(), -1, Utilities::format("Invalid flag type %s", flag_type));
326335

327-
return flag_type_index_map.size() * country_index + it->second;
336+
return flag_type_index_map.size() * type_safe::get(country_index) + it->second;
328337
}
329338

330339
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 {
335344
return { Vector2i { flag_index % flag_sheet_dims.x, flag_index / flag_sheet_dims.x } * flag_dims, flag_dims };
336345
}
337346

338-
Rect2i GameSingleton::get_flag_sheet_rect(int32_t country_index, StringName const& flag_type) const {
347+
Rect2i GameSingleton::get_flag_sheet_rect(country_index_t country_index, StringName const& flag_type) const {
339348
return get_flag_sheet_rect(get_flag_sheet_index(country_index, flag_type));
340349
}
341350

@@ -408,7 +417,7 @@ TypedArray<Dictionary> GameSingleton::get_province_names() const {
408417
TypedArray<Dictionary> ret;
409418
ERR_FAIL_COND_V(ret.resize(map_definition.get_province_definition_count()) != OK, {});
410419

411-
for (int32_t index = 0; index < map_definition.get_province_definition_count(); ++index) {
420+
for (province_index_t index = 0; index < map_definition.get_province_definition_count(); ++index) {
412421
ProvinceDefinition const& province = map_definition.get_province_definitions()[index];
413422

414423
Dictionary province_dict;
@@ -433,35 +442,42 @@ TypedArray<Dictionary> GameSingleton::get_province_names() const {
433442
}
434443

435444
int32_t GameSingleton::get_mapmode_count() const {
436-
return get_definition_manager().get_mapmode_manager().get_mapmode_count();
445+
const auto count = get_definition_manager().get_mapmode_manager().get_mapmode_count();
446+
return type_safe::get(count);
437447
}
438448

439449
String GameSingleton::get_mapmode_identifier(int32_t index) const {
440-
Mapmode const* identifier_mapmode = get_definition_manager().get_mapmode_manager().get_mapmode_by_index(index);
450+
Mapmode const* identifier_mapmode = get_definition_manager()
451+
.get_mapmode_manager()
452+
.get_mapmode_by_index(map_mode_index_t(index));
441453
if (identifier_mapmode != nullptr) {
442454
return convert_to<String>(identifier_mapmode->get_identifier());
443455
}
444456
return String {};
445457
}
446458

447459
String GameSingleton::get_mapmode_localisation_key(int32_t index) const {
448-
Mapmode const* localisation_key_mapmode = get_definition_manager().get_mapmode_manager().get_mapmode_by_index(index);
460+
Mapmode const* localisation_key_mapmode = get_definition_manager()
461+
.get_mapmode_manager()
462+
.get_mapmode_by_index(map_mode_index_t(index));
449463
if (localisation_key_mapmode != nullptr) {
450464
return convert_to<String>(localisation_key_mapmode->get_localisation_key());
451465
}
452466
return String {};
453467
}
454468

455469
int32_t GameSingleton::get_current_mapmode_index() const {
456-
return mapmode->index;
470+
return type_safe::get(mapmode->index);
457471
}
458472

459473
Error GameSingleton::set_mapmode(int32_t index) {
460-
Mapmode const* new_mapmode = get_definition_manager().get_mapmode_manager().get_mapmode_by_index(index);
474+
Mapmode const* new_mapmode = get_definition_manager()
475+
.get_mapmode_manager()
476+
.get_mapmode_by_index(map_mode_index_t(index));
461477
ERR_FAIL_NULL_V_MSG(new_mapmode, FAILED, Utilities::format("Failed to find mapmode with index: %d", index));
462478
mapmode = new_mapmode;
463479
const Error err = _update_colour_image();
464-
emit_signal(_signal_mapmode_changed(), mapmode->index);
480+
emit_signal(_signal_mapmode_changed(), typesafe::get(mapmode->index));
465481
return err;
466482
}
467483

extension/src/openvic-extension/singletons/GameSingleton.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <openvic-simulation/GameManager.hpp>
88
#include <openvic-simulation/dataloader/Dataloader.hpp>
9+
#include <openvic-simulation/types/TypedIndices.hpp>
910

1011
namespace OpenVic {
1112

@@ -106,9 +107,9 @@ namespace OpenVic {
106107

107108
/* The index of the flag in the flag sheet corresponding to the requested country / flag_type
108109
* combination, or -1 if no such flag can be found. */
109-
int32_t get_flag_sheet_index(int32_t country_index, godot::StringName const& flag_type) const;
110+
int32_t get_flag_sheet_index(country_index_t country_index, godot::StringName const& flag_type) const;
110111
godot::Rect2i get_flag_sheet_rect(int32_t flag_index) const;
111-
godot::Rect2i get_flag_sheet_rect(int32_t country_index, godot::StringName const& flag_type) const;
112+
godot::Rect2i get_flag_sheet_rect(country_index_t country_index, godot::StringName const& flag_type) const;
112113

113114
/* Number of (vertical, horizontal) subdivisions the province shape image
114115
* was split into when making the province_shape_texture to ensure no

extension/src/openvic-extension/singletons/MapItemSingleton.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ PackedByteArray MapItemSingleton::get_rgo_icons() const {
226226
}
227227

228228
GoodDefinition const* rgo_good = prov_inst.get_rgo_good();
229-
icons[index++] = rgo_good != nullptr ? rgo_good->index + 1 : 0; // 0 if no rgo good in the province
229+
icons[index++] = rgo_good != nullptr ? type_safe::get(rgo_good->index) + 1 : 0; // 0 if no rgo good in the province
230230
}
231231

232232
return icons;

extension/src/openvic-extension/singletons/MenuSingleton.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ Dictionary MenuSingleton::get_province_info_from_number(int32_t province_number)
539539
ProductionType const& production_type = *rgo.get_production_type_nullable();
540540
GoodDefinition const& rgo_good = *province->get_rgo_good();
541541

542-
ret[province_info_rgo_icon_key] = static_cast<int32_t>(rgo_good.index);
542+
ret[province_info_rgo_icon_key] = type_safe::get(rgo_good.index);
543543

544544
ret[province_info_rgo_output_quantity_yesterday_key] = static_cast<real_t>(rgo.get_output_quantity_yesterday());
545545
ret[province_info_rgo_revenue_yesterday_key] = static_cast<real_t>(rgo.get_revenue_yesterday());

extension/src/openvic-extension/singletons/PlayerSingleton.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "PlayerSingleton.hpp"
22

3+
#include <type_safe/strong_typedef.hpp>
4+
35
#include <openvic-simulation/country/CountryInstance.hpp>
46
#include <openvic-simulation/map/ProvinceInstance.hpp>
57

@@ -98,7 +100,7 @@ void PlayerSingleton::set_player_country(CountryInstance* new_player_country) {
98100
if (player_country != nullptr) {
99101
instance_manager->queue_game_action(
100102
game_action_type_t::GAME_ACTION_SET_AI,
101-
std::pair<uint64_t, bool> { player_country->index, true }
103+
std::pair<uint64_t, bool> { type_safe::get(player_country->index), true }
102104
);
103105
}
104106

@@ -107,7 +109,7 @@ void PlayerSingleton::set_player_country(CountryInstance* new_player_country) {
107109
if (player_country != nullptr) {
108110
instance_manager->queue_game_action(
109111
game_action_type_t::GAME_ACTION_SET_AI,
110-
std::pair<uint64_t, bool> { player_country->index, false }
112+
std::pair<uint64_t, bool> { type_safe::get(player_country->index), false }
111113
);
112114
}
113115

@@ -150,21 +152,21 @@ void PlayerSingleton::set_selected_province(ProvinceInstance const* new_selected
150152
}
151153

152154
void PlayerSingleton::set_selected_province_by_number(int32_t province_number) {
153-
if (province_number == ProvinceDefinition::NULL_INDEX) {
155+
MapInstance const& map_instance = instance_manager->get_map_instance();
156+
const province_index_t province_index = ProvinceDefinition::get_index_from_province_number(province_number);
157+
if (province_index == ProvinceDefinition::NULL_INDEX) {
154158
unset_selected_province();
155159
} else {
156-
InstanceManager const* instance_manager = GameSingleton::get_singleton()->get_instance_manager();
157-
ERR_FAIL_NULL(instance_manager);
158-
159-
MapInstance const& map_instance = instance_manager->get_map_instance();
160-
161-
set_selected_province(map_instance.get_province_instance_from_number(province_number));
162-
160+
ProvinceInstance const* const selected_province = map_instance.get_province_instance_from_index(province_index);
163161
if (selected_province == nullptr) {
164162
spdlog::error_s(
165163
"Trying to set selected province to an invalid number {} (max number is {})",
166164
map_instance.get_province_instance_by_definition().get_count(), province_number
167165
);
166+
} else {
167+
InstanceManager const* instance_manager = GameSingleton::get_singleton()->get_instance_manager();
168+
ERR_FAIL_NULL(instance_manager);
169+
set_selected_province(selected_province);
168170
}
169171
}
170172
}
@@ -217,7 +219,7 @@ void PlayerSingleton::expand_selected_province_building(int32_t building_index)
217219

218220
instance_manager->queue_game_action(
219221
game_action_type_t::GAME_ACTION_EXPAND_PROVINCE_BUILDING,
220-
std::pair<uint64_t, uint64_t> { selected_province->index, building_index }
222+
std::pair<uint64_t, uint64_t> { type_safe::get(selected_province->index), building_index }
221223
);
222224
}
223225

@@ -229,7 +231,7 @@ void PlayerSingleton::set_##value_name##_slider_value(fixed_point_t const value)
229231
} \
230232
GameSingleton::get_singleton()->get_instance_manager()->queue_game_action( \
231233
game_action_type_t::GAME_ACTION_SET_##game_action_name, \
232-
std::pair<uint64_t, fixed_point_t> { player_country->index, value } \
234+
std::pair<uint64_t, fixed_point_t> { type_safe::get(player_country->index), value } \
233235
); \
234236
}
235237

@@ -250,7 +252,7 @@ void PlayerSingleton::set_strata_tax_rate_slider_value(Strata const& strata, fix
250252
}
251253
GameSingleton::get_singleton()->get_instance_manager()->queue_game_action(
252254
game_action_type_t::GAME_ACTION_SET_STRATA_TAX,
253-
std::tuple<uint64_t, uint64_t, fixed_point_t> { player_country->index, strata.index, value }
255+
std::tuple<uint64_t, uint64_t, fixed_point_t> { type_safe::get(player_country->index), strata.index, value }
254256
);
255257
}
256258

@@ -269,7 +271,7 @@ void PlayerSingleton::set_good_automated(int32_t good_index, bool is_automated)
269271

270272
instance_manager->queue_game_action(
271273
game_action_type_t::GAME_ACTION_SET_GOOD_AUTOMATED,
272-
std::tuple<uint64_t, uint64_t, bool> { player_country->index, good_index, is_automated }
274+
std::tuple<uint64_t, uint64_t, bool> { type_safe::get(player_country->index), good_index, is_automated }
273275
);
274276
}
275277

@@ -282,7 +284,7 @@ void PlayerSingleton::set_good_trade_order(int32_t good_index, bool is_selling,
282284

283285
instance_manager->queue_game_action(
284286
game_action_type_t::GAME_ACTION_SET_GOOD_TRADE_ORDER, std::tuple<uint64_t, uint64_t, bool, fixed_point_t> {
285-
player_country->index, good_index, is_selling,
287+
type_safe::get(player_country->index), good_index, is_selling,
286288
MenuSingleton::calculate_trade_menu_stockpile_cutoff_amount_fp(amount_slider->get_value_scaled_fp())
287289
}
288290
);
@@ -299,7 +301,7 @@ void PlayerSingleton::create_leader(bool is_general) const {
299301

300302
instance_manager->queue_game_action(
301303
game_action_type_t::GAME_ACTION_CREATE_LEADER,
302-
std::pair<uint64_t, bool> { player_country->index, is_general }
304+
std::pair<uint64_t, bool> { type_safe::get(player_country->index), is_general }
303305
);
304306
}
305307

@@ -321,7 +323,7 @@ void PlayerSingleton::set_auto_create_leaders(bool value) const {
321323

322324
instance_manager->queue_game_action(
323325
game_action_type_t::GAME_ACTION_SET_AUTO_CREATE_LEADERS,
324-
std::pair<uint64_t, bool> { player_country->index, value }
326+
std::pair<uint64_t, bool> { type_safe::get(player_country->index), value }
325327
);
326328
}
327329

@@ -333,7 +335,7 @@ void PlayerSingleton::set_auto_assign_leaders(bool value) const {
333335

334336
instance_manager->queue_game_action(
335337
game_action_type_t::GAME_ACTION_SET_AUTO_ASSIGN_LEADERS,
336-
std::pair<uint64_t, bool> { player_country->index, value }
338+
std::pair<uint64_t, bool> { type_safe::get(player_country->index), value }
337339
);
338340
}
339341

@@ -345,6 +347,6 @@ void PlayerSingleton::set_mobilise(bool value) const {
345347

346348
instance_manager->queue_game_action(
347349
game_action_type_t::GAME_ACTION_SET_MOBILISE,
348-
std::pair<uint64_t, bool> { player_country->index, value }
350+
std::pair<uint64_t, bool> { type_safe::get(player_country->index), value }
349351
);
350352
}

extension/src/openvic-extension/singletons/PopulationMenu.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <openvic-simulation/types/fixed_point/FixedPoint.hpp>
1212
#include <openvic-simulation/types/IndexedFlatMap.hpp>
1313
#include <openvic-simulation/types/OrderedContainersMath.hpp>
14+
#include <openvic-simulation/types/TypedIndices.hpp>
1415

1516
#include "openvic-extension/classes/GFXPieChartTexture.hpp"
1617
#include "openvic-extension/classes/GUINode.hpp"
@@ -261,7 +262,7 @@ Error MenuSingleton::population_menu_select_province_list_entry(int32_t select_i
261262
}
262263

263264
Error MenuSingleton::population_menu_select_province(int32_t province_number) {
264-
const ProvinceDefinition::index_t province_index = ProvinceDefinition::get_index_from_province_number(province_number);
265+
const province_index_t province_index = ProvinceDefinition::get_index_from_province_number(province_number);
265266
GameSingleton const* game_singleton = GameSingleton::get_singleton();
266267
ERR_FAIL_NULL_V(game_singleton, FAILED);
267268
InstanceManager const* instance_manager = game_singleton->get_instance_manager();
@@ -276,7 +277,7 @@ Error MenuSingleton::population_menu_select_province(int32_t province_number) {
276277

277278
MenuSingleton& menu_singleton;
278279

279-
const ProvinceDefinition::index_t _province_index = 0;
280+
const province_index_t _province_index = province_index_t(0);
280281

281282
int32_t index = 0;
282283

extension/src/openvic-extension/singletons/TradeMenu.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ Dictionary MenuSingleton::get_trade_menu_trade_details_info(
127127
InstanceManager const* instance_manager = GameSingleton::get_singleton()->get_instance_manager();
128128
ERR_FAIL_NULL_V(instance_manager, {});
129129

130-
GoodInstance const* good_instance =
131-
instance_manager->get_good_instance_manager().get_good_instance_by_index(trade_detail_good_index);
130+
GoodInstance const* good_instance = instance_manager->get_good_instance_manager()
131+
.get_good_instance_by_index(good_index_t(trade_detail_good_index));
132132
ERR_FAIL_NULL_V(good_instance, {});
133133

134134
CountryInstance const* country = PlayerSingleton::get_singleton()->get_player_country();

0 commit comments

Comments
 (0)