aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic-extension/singletons/ModelSingleton.cpp
blob: 87357807958c6ad7f8ba15527c9a0d938b19e545 (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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#include "ModelSingleton.hpp"

#include <numbers>

#include <godot_cpp/variant/utility_functions.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::godot_to_std_string;
using OpenVic::Utilities::std_to_godot_string;
using OpenVic::Utilities::std_view_to_godot_string;

void ModelSingleton::_bind_methods() {
   OV_BIND_METHOD(ModelSingleton::get_units);
   OV_BIND_METHOD(ModelSingleton::get_cultural_gun_model, { "culture" });
   OV_BIND_METHOD(ModelSingleton::get_cultural_helmet_model, { "culture" });
   OV_BIND_METHOD(ModelSingleton::get_flag_model, { "floating" });
   OV_BIND_METHOD(ModelSingleton::get_buildings);
}

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

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

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

GFX::Actor const* ModelSingleton::get_actor(std::string_view name, bool error_on_fail) const {
   GameSingleton const* game_singleton = GameSingleton::get_singleton();
   ERR_FAIL_NULL_V(game_singleton, nullptr);

   GFX::Actor const* actor =
      game_singleton->get_definition_manager().get_ui_manager().get_cast_object_by_identifier<GFX::Actor>(name);

   if (error_on_fail) {
      ERR_FAIL_NULL_V_MSG(actor, nullptr, vformat("Failed to find actor \"%s\"", std_view_to_godot_string(name)));
   }

   return actor;
}

GFX::Actor const* ModelSingleton::get_cultural_actor(
   std::string_view culture, std::string_view name, std::string_view fallback_name
) const {
   GameSingleton const* game_singleton = GameSingleton::get_singleton();
   ERR_FAIL_NULL_V(game_singleton, nullptr);

   ERR_FAIL_COND_V_MSG(
      culture.empty() || name.empty(), nullptr, vformat(
         "Failed to find actor \"%s\" for culture \"%s\" - neither can be empty",
         std_view_to_godot_string(name), std_view_to_godot_string(culture)
      )
   );

   std::string actor_name = StringUtils::append_string_views(culture, name);

   GFX::Actor const* actor = get_actor(actor_name, false);

   // Which should be tried first: "Generic***" or "***Infantry"?

   if (actor == nullptr) {
      /* If no Actor exists for the specified GraphicalCultureType then try the default instead. */
      GraphicalCultureType const* default_graphical_culture_type = game_singleton->get_definition_manager().get_pop_manager()
         .get_culture_manager().get_default_graphical_culture_type();

      if (default_graphical_culture_type != nullptr && default_graphical_culture_type->get_identifier() != culture) {
         actor_name = StringUtils::append_string_views(default_graphical_culture_type->get_identifier(), name);

         actor = get_actor(actor_name, false);
      }

      if (actor == nullptr && !fallback_name.empty() && fallback_name != name) {
         return get_cultural_actor(culture, fallback_name, {});
      }
   }

   ERR_FAIL_NULL_V_MSG(
      actor, nullptr, vformat(
         "Failed to find actor \"%s\" for culture \"%s\"", std_view_to_godot_string(name),
         std_view_to_godot_string(culture)
      )
   );

   return actor;
}

Dictionary ModelSingleton::make_animation_dict(GFX::Actor::Animation const& animation) const {
   static const StringName file_key = "file";
   static const StringName time_key = "time";

   Dictionary dict;

   dict[file_key] = std_view_to_godot_string(animation.get_file());
   dict[time_key] = animation.get_scroll_time().to_float();

   return dict;
}

Dictionary ModelSingleton::make_model_dict(GFX::Actor const& actor) const {
   static const StringName file_key = "file";
   static const StringName scale_key = "scale";
   static const StringName idle_key = "idle";
   static const StringName move_key = "move";
   static const StringName attack_key = "attack";
   static const StringName attachments_key = "attachments";

   Dictionary dict;

   dict[file_key] = std_view_to_godot_string(actor.get_model_file());
   dict[scale_key] = actor.get_scale().to_float();

   const auto set_animation = [this, &dict](StringName const& key, std::optional<GFX::Actor::Animation> const& animation) {
      if (animation.has_value()) {
         dict[key] = make_animation_dict(*animation);
      }
   };

   set_animation(idle_key, actor.get_idle_animation());
   set_animation(move_key, actor.get_move_animation());
   set_animation(attack_key, actor.get_attack_animation());

   std::vector<GFX::Actor::Attachment> const& attachments = actor.get_attachments();

   if (!attachments.empty()) {
      static const StringName attachment_node_key = "node";
      static const StringName attachment_model_key = "model";

      TypedArray<Dictionary> attachments_array;

      if (attachments_array.resize(attachments.size()) == OK) {

         for (size_t idx = 0; idx < attachments_array.size(); ++idx) {

            GFX::Actor::Attachment const& attachment = attachments[idx];

            GFX::Actor const* attachment_actor = get_actor(attachment.get_actor_name());

            ERR_CONTINUE_MSG(
               attachment_actor == nullptr, vformat(
                  "Failed to find \"%s\" attachment actor for actor \"%s\"",
                  std_view_to_godot_string(attachment.get_actor_name()), std_view_to_godot_string(actor.get_name())
               )
            );

            Dictionary attachment_dict;

            attachment_dict[attachment_node_key] = std_view_to_godot_string(attachment.get_attach_node());
            attachment_dict[attachment_model_key] = make_model_dict(*attachment_actor);

            attachments_array[idx] = std::move(attachment_dict);

         }

         if (!attachments_array.is_empty()) {
            dict[attachments_key] = std::move(attachments_array);
         }

      } else {
         UtilityFunctions::push_error(
            "Failed to resize attachments array to the correct size (", static_cast<int64_t>(attachments.size()),
            ") for model for actor \"", std_view_to_godot_string(actor.get_name()), "\""
         );
      }
   }

   return dict;
}

/* 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 {
   GameSingleton const* game_singleton = GameSingleton::get_singleton();
   ERR_FAIL_NULL_V(game_singleton, false);

   static const StringName culture_key = "culture";
   static const StringName model_key = "model";
   static const StringName mount_model_key = "mount_model";
   static const StringName mount_attach_node_key = "mount_attach_node";
   static const StringName flag_index_key = "flag_index";
   static const StringName flag_floating_key = "flag_floating";
   static const StringName position_key = "position";
   static const StringName rotation_key = "rotation";
   static const StringName primary_colour_key = "primary_colour";
   static const StringName secondary_colour_key = "secondary_colour";
   static const StringName tertiary_colour_key = "tertiary_colour";

   if (units.empty()) {
      return true;
   }

   bool ret = true;

   /* Last unit to enter the province is shown on top. */
   T const& unit = *units.back();
   ERR_FAIL_COND_V_MSG(unit.empty(), false, vformat("Empty unit \"%s\"", std_view_to_godot_string(unit.get_name())));

   Country const* country = unit.get_country()->get_base_country();

   GraphicalCultureType const& graphical_culture_type = country->get_graphical_culture();
   UnitType const* display_unit_type = unit.get_display_unit_type();
   ERR_FAIL_NULL_V_MSG(
      display_unit_type, false, vformat(
         "Failed to get display unit type for unit \"%s\"", std_view_to_godot_string(unit.get_name())
      )
   );

   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>) {
      RegimentType const* regiment_type = reinterpret_cast<RegimentType const*>(display_unit_type);

      if (!regiment_type->get_sprite_override().empty()) {
         actor_name = regiment_type->get_sprite_override();
      }

      if (regiment_type->get_sprite_mount().empty() == regiment_type->get_sprite_mount_attach_node().empty()) {
         if (!regiment_type->get_sprite_mount().empty()) {
            mount_actor_name = regiment_type->get_sprite_mount();
            mount_attach_node_name = regiment_type->get_sprite_mount_attach_node();
         }
      } else {
         UtilityFunctions::push_error(
            "Mount sprite and attach node must both be set or both be empty - regiment type \"",
            std_view_to_godot_string(regiment_type->get_identifier()), "\" has mount \"",
            std_view_to_godot_string(regiment_type->get_sprite_mount()), "\" and attach node \"",
            std_view_to_godot_string(regiment_type->get_sprite_mount_attach_node()), "\""
         );
         ret = false;
      }
   }

   // TODO - default without requiring hardcoded name
   static constexpr std::string_view default_fallback_actor_name = "Infantry";
   GFX::Actor const* actor = get_cultural_actor(
      graphical_culture_type.get_identifier(), actor_name, default_fallback_actor_name
   );

   ERR_FAIL_NULL_V_MSG(
      actor, false, vformat(
         "Failed to find \"%s\" actor of graphical culture type \"%s\" for unit \"%s\"",
         std_view_to_godot_string(display_unit_type->get_sprite()),
         std_view_to_godot_string(graphical_culture_type.get_identifier()),
         std_view_to_godot_string(unit.get_name())
      )
   );

   Dictionary dict;

   dict[culture_key] = std_view_to_godot_string(graphical_culture_type.get_identifier());

   dict[model_key] = make_model_dict(*actor);

   if (!mount_actor_name.empty() && !mount_attach_node_name.empty()) {
      GFX::Actor const* mount_actor = get_actor(mount_actor_name);

      if (mount_actor != nullptr) {
         dict[mount_model_key] = make_model_dict(*mount_actor);
         dict[mount_attach_node_key] = std_view_to_godot_string(mount_attach_node_name);
      } else {
         UtilityFunctions::push_error(vformat(
            "Failed to find \"%s\" mount actor of graphical culture type \"%s\" for unit \"%s\"",
            std_view_to_godot_string(mount_actor_name),
            std_view_to_godot_string(graphical_culture_type.get_identifier()),
            std_view_to_godot_string(unit.get_name())
         ));
         ret = false;
      }
   }

   // TODO - government type based flag type
   dict[flag_index_key] = game_singleton->get_flag_sheet_index(country->get_index(), {});

   if (display_unit_type->has_floating_flag()) {
      dict[flag_floating_key] = true;
   }

   dict[position_key] =
      game_singleton->map_position_to_world_coords(unit.get_position()->get_province_definition().get_unit_position());

   if (display_unit_type->get_unit_category() != UnitType::unit_category_t::INFANTRY) {
      dict[rotation_key] = -0.25f * std::numbers::pi_v<float>;
   }

   dict[primary_colour_key] = Utilities::to_godot_color(country->get_primary_unit_colour());
   dict[secondary_colour_key] = Utilities::to_godot_color(country->get_secondary_unit_colour());
   dict[tertiary_colour_key] = Utilities::to_godot_color(country->get_tertiary_unit_colour());

   // TODO - move dict into unit_array ?
   unit_array.push_back(dict);

   return ret;
}

TypedArray<Dictionary> ModelSingleton::get_units() const {
   GameSingleton const* game_singleton = GameSingleton::get_singleton();
   ERR_FAIL_NULL_V(game_singleton, {});
   InstanceManager const* instance_manager = game_singleton->get_instance_manager();
   ERR_FAIL_NULL_V(instance_manager, {});

   TypedArray<Dictionary> ret;

   for (ProvinceInstance const& province : instance_manager->get_map_instance().get_province_instances()) {
      if (province.get_province_definition().is_water()) {
         if (!add_unit_dict(province.get_navies(), ret)) {
            UtilityFunctions::push_error(
               "Error adding navy to province \"", std_view_to_godot_string(province.get_identifier()), "\""
            );
         }
      } else {
         if (!add_unit_dict(province.get_armies(), ret)) {
            UtilityFunctions::push_error(
               "Error adding army to province \"", std_view_to_godot_string(province.get_identifier()), "\""
            );
         }
      }

      // TODO - land units in ships
   }

   return ret;
}

Dictionary ModelSingleton::get_cultural_gun_model(String const& culture) const {
   static constexpr std::string_view gun_actor_name = "Gun1";

   GFX::Actor const* actor = get_cultural_actor(godot_to_std_string(culture), gun_actor_name, {});

   ERR_FAIL_NULL_V(actor, {});

   return make_model_dict(*actor);
}

Dictionary ModelSingleton::get_cultural_helmet_model(String const& culture) const {
   static constexpr std::string_view helmet_actor_name = "Helmet1";

   GFX::Actor const* actor = get_cultural_actor(godot_to_std_string(culture), helmet_actor_name, {});

   ERR_FAIL_NULL_V(actor, {});

   return make_model_dict(*actor);
}

Dictionary ModelSingleton::get_flag_model(bool floating) const {
   static constexpr std::string_view flag_name = "Flag";
   static constexpr std::string_view flag_floating_name = "FlagFloating";

   GFX::Actor const* actor = get_actor(floating ? flag_floating_name : flag_name);

   ERR_FAIL_NULL_V(actor, {});

   return make_model_dict(*actor);
}

bool ModelSingleton::add_building_dict(
   BuildingInstance const& building, ProvinceInstance const& province, TypedArray<Dictionary>& building_array
) const {
   ProvinceDefinition const& province_definition = province.get_province_definition();

   GameSingleton const* game_singleton = GameSingleton::get_singleton();
   ERR_FAIL_NULL_V(game_singleton, false);

   static const StringName model_key = "model";
   static const StringName position_key = "position";
   static const StringName rotation_key = "rotation";

   std::string suffix;

   if (
      &building.get_building_type() ==
         game_singleton->get_definition_manager().get_economy_manager().get_building_type_manager().get_port_building_type()
   ) {
      /* Port */
      if (!province_definition.has_port()) {
         return true;
      }

      if (building.get_level() > 0) {
         suffix = std::to_string(building.get_level());
      }

      if (!province.get_navies().empty()) {
         suffix += "_ships";
      }
   } else if (building.get_identifier() == "fort") {
      /* Fort */
      if (building.get_level() < 1) {
         return true;
      }

      if (building.get_level() > 1) {
         suffix = std::to_string(building.get_level());
      }
   } else {
      // TODO - railroad (trainstations)
      return true;
   }

   fvec2_t const* position_ptr = province_definition.get_building_position(&building.get_building_type());
   const float rotation = province_definition.get_building_rotation(&building.get_building_type());

   const std::string actor_name = StringUtils::append_string_views("building_", building.get_identifier(), suffix);

   GFX::Actor const* actor = get_actor(actor_name);
   ERR_FAIL_NULL_V_MSG(
      actor, false, vformat(
         "Failed to find \"%s\" actor for building \"%s\" in province \"%s\"",
         std_to_godot_string(actor_name), std_view_to_godot_string(building.get_identifier()),
         std_view_to_godot_string(province.get_identifier())
      )
   );

   Dictionary dict;

   dict[model_key] = make_model_dict(*actor);

   dict[position_key] = game_singleton->map_position_to_world_coords(
      position_ptr != nullptr ? *position_ptr : province_definition.get_centre()
   );

   if (rotation != 0.0f) {
      dict[rotation_key] = rotation;
   }

   // TODO - move dict into unit_array ?
   building_array.push_back(dict);

   return true;
}

TypedArray<Dictionary> ModelSingleton::get_buildings() const {
   GameSingleton const* game_singleton = GameSingleton::get_singleton();
   ERR_FAIL_NULL_V(game_singleton, {});
   InstanceManager const* instance_manager = game_singleton->get_instance_manager();
   ERR_FAIL_NULL_V(instance_manager, {});

   TypedArray<Dictionary> ret;

   for (ProvinceInstance const& province : instance_manager->get_map_instance().get_province_instances()) {
      if (!province.get_province_definition().is_water()) {
         for (BuildingInstance const& building : province.get_buildings()) {
            if (!add_building_dict(building, province, ret)) {
               UtilityFunctions::push_error(
                  "Error adding building \"", std_view_to_godot_string(building.get_identifier()), "\" to province \"",
                  std_view_to_godot_string(province.get_identifier()), "\""
               );
            }
         }
      }
   }

   return ret;
}