aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic-extension/singletons/MenuSingleton.cpp
blob: 993549cb0b5cc21606ad48a30d3ce1c1bf5c51e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include "MenuSingleton.hpp"

#include <godot_cpp/variant/utility_functions.hpp>

#include <openvic-simulation/GameManager.hpp>

#include "openvic-extension/classes/GFXPieChartTexture.hpp"
#include "openvic-extension/singletons/GameSingleton.hpp"
#include "openvic-extension/utility/ClassBindings.hpp"
#include "openvic-extension/utility/Utilities.hpp"

using namespace godot;
using namespace OpenVic;

using OpenVic::Utilities::std_to_godot_string;
using OpenVic::Utilities::std_view_to_godot_string;

StringName const& MenuSingleton::_signal_population_menu_province_list_changed() {
   static const StringName signal_population_menu_province_list_changed = "population_menu_province_list_changed";
   return signal_population_menu_province_list_changed;
}
StringName const& MenuSingleton::_signal_population_menu_province_list_selected_changed() {
   static const StringName signal_population_menu_province_list_selected_changed = "population_menu_province_list_selected_changed";
   return signal_population_menu_province_list_selected_changed;
}
StringName const& MenuSingleton::_signal_population_menu_pops_changed() {
   static const StringName signal_population_menu_pops_changed = "population_menu_pops_changed";
   return signal_population_menu_pops_changed;
}

void MenuSingleton::_bind_methods() {
   /* PROVINCE OVERVIEW PANEL */
   OV_BIND_METHOD(MenuSingleton::get_province_info_from_index, { "index" });
   OV_BIND_METHOD(MenuSingleton::get_province_building_count);
   OV_BIND_METHOD(MenuSingleton::get_province_building_identifier, { "building_index" });
   OV_BIND_METHOD(MenuSingleton::expand_selected_province_building, { "building_index" });
   OV_BIND_METHOD(MenuSingleton::get_slave_pop_icon_index);
   OV_BIND_METHOD(MenuSingleton::get_administrative_pop_icon_index);
   OV_BIND_METHOD(MenuSingleton::get_rgo_owner_pop_icon_index);

   /* TIME/SPEED CONTROL PANEL */
   OV_BIND_METHOD(MenuSingleton::set_paused, { "paused" });
   OV_BIND_METHOD(MenuSingleton::toggle_paused);
   OV_BIND_METHOD(MenuSingleton::is_paused);
   OV_BIND_METHOD(MenuSingleton::increase_speed);
   OV_BIND_METHOD(MenuSingleton::decrease_speed);
   OV_BIND_METHOD(MenuSingleton::get_speed);
   OV_BIND_METHOD(MenuSingleton::can_increase_speed);
   OV_BIND_METHOD(MenuSingleton::can_decrease_speed);
   OV_BIND_METHOD(MenuSingleton::get_longform_date);

   /* POPULATION MENU */
   OV_BIND_METHOD(MenuSingleton::get_population_menu_province_list_row_count);
   OV_BIND_METHOD(MenuSingleton::get_population_menu_province_list_rows, { "start", "count" });
   OV_BIND_METHOD(
      MenuSingleton::population_menu_select_province_list_entry, { "select_index", "set_scroll_index" }, DEFVAL(false)
   );
   OV_BIND_METHOD(MenuSingleton::population_menu_select_province, { "province_index" });
   OV_BIND_METHOD(MenuSingleton::population_menu_toggle_expanded, { "toggle_index", "emit_selected_changed" }, DEFVAL(true));

   OV_BIND_METHOD(MenuSingleton::population_menu_select_sort_key, { "sort_key" });
   OV_BIND_METHOD(MenuSingleton::get_population_menu_pop_rows, { "start", "count" });
   OV_BIND_METHOD(MenuSingleton::get_population_menu_pop_row_count);

   OV_BIND_METHOD(MenuSingleton::get_population_menu_pop_filter_setup_info);
   OV_BIND_METHOD(MenuSingleton::get_population_menu_pop_filter_info);
   OV_BIND_METHOD(MenuSingleton::population_menu_toggle_pop_filter, { "filter_index" });
   OV_BIND_METHOD(MenuSingleton::population_menu_select_all_pop_filters);
   OV_BIND_METHOD(MenuSingleton::population_menu_deselect_all_pop_filters);

   OV_BIND_METHOD(MenuSingleton::get_population_menu_distribution_setup_info);
   OV_BIND_METHOD(MenuSingleton::get_population_menu_distribution_info);

   ADD_SIGNAL(MethodInfo(_signal_population_menu_province_list_changed()));
   ADD_SIGNAL(
      MethodInfo(_signal_population_menu_province_list_selected_changed(), PropertyInfo(Variant::INT, "scroll_index"))
   );
   ADD_SIGNAL(MethodInfo(_signal_population_menu_pops_changed()));

   using enum population_menu_t::ProvinceListEntry;
   BIND_ENUM_CONSTANT(LIST_ENTRY_NONE);
   BIND_ENUM_CONSTANT(LIST_ENTRY_COUNTRY);
   BIND_ENUM_CONSTANT(LIST_ENTRY_STATE);
   BIND_ENUM_CONSTANT(LIST_ENTRY_PROVINCE);

   using enum population_menu_t::PopSortKey;
   BIND_ENUM_CONSTANT(NONE);
   BIND_ENUM_CONSTANT(SORT_SIZE);
   BIND_ENUM_CONSTANT(SORT_TYPE);
   BIND_ENUM_CONSTANT(SORT_CULTURE);
   BIND_ENUM_CONSTANT(SORT_RELIGION);
   BIND_ENUM_CONSTANT(SORT_LOCATION);
   BIND_ENUM_CONSTANT(SORT_MILITANCY);
   BIND_ENUM_CONSTANT(SORT_CONSCIOUSNESS);
   BIND_ENUM_CONSTANT(SORT_IDEOLOGY);
   BIND_ENUM_CONSTANT(SORT_ISSUES);
   BIND_ENUM_CONSTANT(SORT_UNEMPLOYMENT);
   BIND_ENUM_CONSTANT(SORT_CASH);
   BIND_ENUM_CONSTANT(SORT_LIFE_NEEDS);
   BIND_ENUM_CONSTANT(SORT_EVERYDAY_NEEDS);
   BIND_ENUM_CONSTANT(SORT_LUXURY_NEEDS);
   BIND_ENUM_CONSTANT(SORT_REBEL_FACTION);
   BIND_ENUM_CONSTANT(SORT_SIZE_CHANGE);
   BIND_ENUM_CONSTANT(SORT_LITERACY);
}

MenuSingleton* MenuSingleton::get_singleton() {
   return singleton;
}

MenuSingleton::MenuSingleton() : game_manager {
   []() -> GameManager* {
      GameSingleton* game_singleton = GameSingleton::get_singleton();
      ERR_FAIL_NULL_V_MSG(
         game_singleton, nullptr, "Cannot initialise MenuSingleton's GameManager pointer - GameSingleton not initialised!"
      );
      return &game_singleton->get_game_manager();
   }()
} {
   ERR_FAIL_COND(singleton != nullptr);
   singleton = this;
}

MenuSingleton::~MenuSingleton() {
   ERR_FAIL_COND(singleton != this);
   singleton = nullptr;
}

/* PROVINCE OVERVIEW PANEL */

Dictionary MenuSingleton::get_province_info_from_index(int32_t index) const {
   ERR_FAIL_NULL_V(game_manager, {});

   static const StringName province_info_province_key = "province";
   static const StringName province_info_region_key = "region";
   static const StringName province_info_slave_status_key = "slave_status";
   static const StringName province_info_colony_status_key = "colony_status";
   static const StringName province_info_terrain_type_key = "terrain_type";
   static const StringName province_info_life_rating_key = "life_rating";
   static const StringName province_info_controller_key = "controller";
   static const StringName province_info_rgo_name_key = "rgo_name";
   static const StringName province_info_rgo_icon_key = "rgo_icon";
   static const StringName province_info_crime_name_key = "crime_name";
   static const StringName province_info_crime_icon_key = "crime_icon";
   static const StringName province_info_total_population_key = "total_population";
   static const StringName province_info_pop_types_key = "pop_types";
   static const StringName province_info_pop_ideologies_key = "pop_ideologies";
   static const StringName province_info_pop_cultures_key = "pop_cultures";
   static const StringName province_info_cores_key = "cores";
   static const StringName province_info_buildings_key = "buildings";

   Province const* province = game_manager->get_map().get_province_by_index(index);
   if (province == nullptr) {
      return {};
   }
   Dictionary ret;

   ret[province_info_province_key] = std_view_to_godot_string(province->get_identifier());

   Region const* region = province->get_region();
   if (region != nullptr) {
      ret[province_info_region_key] = std_view_to_godot_string(region->get_identifier());
   }

   ret[province_info_slave_status_key] = province->get_slave();

   ret[province_info_colony_status_key] = static_cast<int32_t>(province->get_colony_status());

   TerrainType const* terrain_type = province->get_terrain_type();
   if (terrain_type != nullptr) {
      ret[province_info_terrain_type_key] = std_view_to_godot_string(terrain_type->get_identifier());
   }

   ret[province_info_life_rating_key] = province->get_life_rating();

   Country const* controller = province->get_controller();
   if (controller != nullptr) {
      ret[province_info_controller_key] = std_view_to_godot_string(controller->get_identifier());
   }

   Good const* rgo = province->get_rgo();
   if (rgo != nullptr) {
      ret[province_info_rgo_name_key] = std_view_to_godot_string(rgo->get_identifier());
      ret[province_info_rgo_icon_key] = static_cast<int32_t>(rgo->get_index());
   }

   Crime const* crime = province->get_crime();
   if (crime != nullptr) {
      ret[province_info_crime_name_key] = std_view_to_godot_string(crime->get_identifier());
      ret[province_info_crime_icon_key] = static_cast<int32_t>(crime->get_icon());
   }

   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);
   }

   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);
   }

   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);
   }

   std::vector<Country const*> const& cores = province->get_cores();
   if (!cores.empty()) {
      PackedStringArray cores_array;
      if (cores_array.resize(cores.size()) == OK) {
         for (size_t idx = 0; idx < cores.size(); ++idx) {
            cores_array[idx] = std_view_to_godot_string(cores[idx]->get_identifier());
         }
         ret[province_info_cores_key] = std::move(cores_array);
      } else {
         UtilityFunctions::push_error(
            "Failed to resize cores array to the correct size (", static_cast<int64_t>(cores.size()), ") for province ",
            std_view_to_godot_string(province->get_identifier())
         );
      }
   }

   static const StringName building_info_level_key = "level";
   static const StringName building_info_expansion_state_key = "expansion_state";
   static const StringName building_info_start_date_key = "start_date";
   static const StringName building_info_end_date_key = "end_date";
   static const StringName building_info_expansion_progress_key = "expansion_progress";

   std::vector<BuildingInstance> const& buildings = province->get_buildings();
   if (!buildings.empty()) {
      /* This system relies on the province buildings all being present in the right order. It will have to
       * be changed if we want to support variable combinations and permutations of province buildings. */
      TypedArray<Dictionary> buildings_array;
      if (buildings_array.resize(buildings.size()) == OK) {
         for (size_t idx = 0; idx < buildings.size(); ++idx) {
            BuildingInstance const& building = buildings[idx];

            Dictionary building_dict;
            building_dict[building_info_level_key] = static_cast<int32_t>(building.get_level());
            building_dict[building_info_expansion_state_key] = static_cast<int32_t>(building.get_expansion_state());
            building_dict[building_info_start_date_key] = std_to_godot_string(building.get_start_date().to_string());
            building_dict[building_info_end_date_key] = std_to_godot_string(building.get_end_date().to_string());
            building_dict[building_info_expansion_progress_key] = building.get_expansion_progress();

            buildings_array[idx] = std::move(building_dict);
         }
         ret[province_info_buildings_key] = std::move(buildings_array);
      } else {
         UtilityFunctions::push_error(
            "Failed to resize buildings array to the correct size (", static_cast<int64_t>(buildings.size()),
            ") for province ", std_view_to_godot_string(province->get_identifier())
         );
      }
   }
   return ret;
}

int32_t MenuSingleton::get_province_building_count() const {
   ERR_FAIL_NULL_V(game_manager, 0);

   return game_manager->get_economy_manager().get_building_type_manager().get_province_building_types().size();
}

String MenuSingleton::get_province_building_identifier(int32_t building_index) const {
   ERR_FAIL_NULL_V(game_manager, {});

   std::vector<BuildingType const*> const& province_building_types =
      game_manager->get_economy_manager().get_building_type_manager().get_province_building_types();
   ERR_FAIL_COND_V_MSG(
      building_index < 0 || building_index >= province_building_types.size(), {},
      vformat("Invalid province building index: %d", building_index)
   );
   return std_view_to_godot_string(province_building_types[building_index]->get_identifier());
}

Error MenuSingleton::expand_selected_province_building(int32_t building_index) {
   ERR_FAIL_NULL_V(game_manager, FAILED);

   ERR_FAIL_COND_V_MSG(
      !game_manager->expand_selected_province_building(building_index), FAILED,
      vformat("Failed to expand the currently selected province's building index %d", building_index)
   );
   return OK;
}

int32_t MenuSingleton::get_slave_pop_icon_index() const {
   ERR_FAIL_NULL_V(game_manager, 0);

   const PopType::sprite_t sprite = game_manager->get_pop_manager().get_slave_sprite();
   ERR_FAIL_COND_V_MSG(sprite <= 0, 0, "Slave sprite unset!");
   return sprite;
}

int32_t MenuSingleton::get_administrative_pop_icon_index() const {
   ERR_FAIL_NULL_V(game_manager, 0);

   const PopType::sprite_t sprite = game_manager->get_pop_manager().get_administrative_sprite();
   ERR_FAIL_COND_V_MSG(sprite <= 0, 0, "Administrative sprite unset!");
   return sprite;
}

int32_t MenuSingleton::get_rgo_owner_pop_icon_index() const {
   ERR_FAIL_NULL_V(game_manager, 0);

   const PopType::sprite_t sprite = game_manager->get_economy_manager().get_production_type_manager().get_rgo_owner_sprite();
   ERR_FAIL_COND_V_MSG(sprite <= 0, 0, "RGO owner sprite unset!");
   return sprite;
}

/* TIME/SPEED CONTROL PANEL */

void MenuSingleton::set_paused(bool paused) {
   ERR_FAIL_NULL(game_manager);

   game_manager->get_simulation_clock().set_paused(paused);
}

void MenuSingleton::toggle_paused() {
   ERR_FAIL_NULL(game_manager);

   game_manager->get_simulation_clock().toggle_paused();
}

bool MenuSingleton::is_paused() const {
   ERR_FAIL_NULL_V(game_manager, true);

   return game_manager->get_simulation_clock().is_paused();
}

void MenuSingleton::increase_speed() {
   ERR_FAIL_NULL(game_manager);

   game_manager->get_simulation_clock().increase_simulation_speed();
}

void MenuSingleton::decrease_speed() {
   ERR_FAIL_NULL(game_manager);

   game_manager->get_simulation_clock().decrease_simulation_speed();
}

int32_t MenuSingleton::get_speed() const {
   ERR_FAIL_NULL_V(game_manager, 0);

   return game_manager->get_simulation_clock().get_simulation_speed();
}

bool MenuSingleton::can_increase_speed() const {
   ERR_FAIL_NULL_V(game_manager, false);

   return game_manager->get_simulation_clock().can_increase_simulation_speed();
}

bool MenuSingleton::can_decrease_speed() const {
   ERR_FAIL_NULL_V(game_manager, false);

   return game_manager->get_simulation_clock().can_decrease_simulation_speed();
}

String MenuSingleton::get_longform_date() const {
   ERR_FAIL_NULL_V(game_manager, {});

   return Utilities::date_to_formatted_string(game_manager->get_today());
}