From ac1504f6fa981947bd9543d24325b3751236e925 Mon Sep 17 00:00:00 2001 From: hop311 Date: Tue, 7 May 2024 22:05:55 +0100 Subject: Generate army and navy models --- .../singletons/ModelSingleton.cpp | 362 +++++++++++++++++++++ 1 file changed, 362 insertions(+) create mode 100644 extension/src/openvic-extension/singletons/ModelSingleton.cpp (limited to 'extension/src/openvic-extension/singletons/ModelSingleton.cpp') diff --git a/extension/src/openvic-extension/singletons/ModelSingleton.cpp b/extension/src/openvic-extension/singletons/ModelSingleton.cpp new file mode 100644 index 0000000..349d336 --- /dev/null +++ b/extension/src/openvic-extension/singletons/ModelSingleton.cpp @@ -0,0 +1,362 @@ +#include "ModelSingleton.hpp" + +#include + +#include + +#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" }); +} + +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_game_manager().get_ui_manager().get_cast_object_by_identifier(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_game_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 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 const& attachments = actor.get_attachments(); + + if (!attachments.empty()) { + static const StringName attachment_node_key = "node"; + static const StringName attachment_model_key = "model"; + + TypedArray 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(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 T> +bool ModelSingleton::add_unit_dict(ordered_set const& units, TypedArray& 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) { + RegimentType const* regiment_type = reinterpret_cast(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_unit_position()); + + if (display_unit_type->get_unit_category() != UnitType::unit_category_t::INFANTRY) { + dict[rotation_key] = -0.25f * std::numbers::pi_v; + } + + 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 ModelSingleton::get_units() const { + GameSingleton const* game_singleton = GameSingleton::get_singleton(); + ERR_FAIL_NULL_V(game_singleton, {}); + + TypedArray ret; + + for (Province const& province : game_singleton->get_game_manager().get_map().get_provinces()) { + if (province.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); +} -- cgit v1.2.3-56-ga3b1 From ac29e4040fc20c50c8f0eb64b1194f6398165eb0 Mon Sep 17 00:00:00 2001 From: hop311 Date: Wed, 8 May 2024 00:14:00 +0100 Subject: Generate building models --- .../singletons/ModelSingleton.cpp | 96 ++++++++++++++++++++++ .../singletons/ModelSingleton.hpp | 6 ++ game/src/Game/GameSession/GameSession.gd | 1 + game/src/Game/GameSession/ModelManager.gd | 19 +++++ 4 files changed, 122 insertions(+) (limited to 'extension/src/openvic-extension/singletons/ModelSingleton.cpp') diff --git a/extension/src/openvic-extension/singletons/ModelSingleton.cpp b/extension/src/openvic-extension/singletons/ModelSingleton.cpp index 349d336..88a69c9 100644 --- a/extension/src/openvic-extension/singletons/ModelSingleton.cpp +++ b/extension/src/openvic-extension/singletons/ModelSingleton.cpp @@ -20,6 +20,7 @@ void ModelSingleton::_bind_methods() { 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() { @@ -360,3 +361,98 @@ Dictionary ModelSingleton::get_flag_model(bool floating) const { return make_model_dict(*actor); } + +bool ModelSingleton::add_building_dict( + BuildingInstance const& building, Province const& province, TypedArray& building_array +) const { + 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_game_manager().get_economy_manager().get_building_type_manager().get_port_building_type() + ) { + /* Port */ + if (!province.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.get_building_position(&building.get_building_type()); + const float rotation = province.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.get_centre()); + + if (rotation != 0.0f) { + dict[rotation_key] = rotation; + } + + // TODO - move dict into unit_array ? + building_array.push_back(dict); + + return true; +} + +TypedArray ModelSingleton::get_buildings() const { + GameSingleton const* game_singleton = GameSingleton::get_singleton(); + ERR_FAIL_NULL_V(game_singleton, {}); + + TypedArray ret; + + for (Province const& province : game_singleton->get_game_manager().get_map().get_provinces()) { + if (!province.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; +} diff --git a/extension/src/openvic-extension/singletons/ModelSingleton.hpp b/extension/src/openvic-extension/singletons/ModelSingleton.hpp index 9ec163e..17c2dd0 100644 --- a/extension/src/openvic-extension/singletons/ModelSingleton.hpp +++ b/extension/src/openvic-extension/singletons/ModelSingleton.hpp @@ -32,11 +32,17 @@ namespace OpenVic { template T> bool add_unit_dict(ordered_set const& units, godot::TypedArray& unit_array) const; + bool add_building_dict( + BuildingInstance const& building, Province const& province, godot::TypedArray& building_array + ) const; + public: godot::TypedArray get_units() const; godot::Dictionary get_cultural_gun_model(godot::String const& culture) const; godot::Dictionary get_cultural_helmet_model(godot::String const& culture) const; godot::Dictionary get_flag_model(bool floating) const; + + godot::TypedArray get_buildings() const; }; } diff --git a/game/src/Game/GameSession/GameSession.gd b/game/src/Game/GameSession/GameSession.gd index a07ce32..843ecd8 100644 --- a/game/src/Game/GameSession/GameSession.gd +++ b/game/src/Game/GameSession/GameSession.gd @@ -9,6 +9,7 @@ func _ready() -> void: push_error("Failed to setup game") _model_manager.generate_units() + _model_manager.generate_buildings() func _process(_delta : float) -> void: GameSingleton.try_tick() diff --git a/game/src/Game/GameSession/ModelManager.gd b/game/src/Game/GameSession/ModelManager.gd index 17d2c1e..8cec49d 100644 --- a/game/src/Game/GameSession/ModelManager.gd +++ b/game/src/Game/GameSession/ModelManager.gd @@ -61,6 +61,25 @@ func _generate_unit(unit_dict : Dictionary) -> void: add_child(model) +func generate_buildings() -> void: + for building : Dictionary in ModelSingleton.get_buildings(): + _generate_building(building) + +func _generate_building(building_dict : Dictionary) -> void: + const model_key : StringName = &"model" + const position_key : StringName = &"position" + const rotation_key : StringName = &"rotation" + + var model : Node3D = _generate_model(building_dict[model_key]) + if not model: + return + + model.scale *= MODEL_SCALE + model.rotate_y(PI + building_dict.get(rotation_key, 0.0)) + model.set_position(_map_view._map_to_world_coords(building_dict[position_key]) + Vector3(0, 0.1 * MODEL_SCALE, 0)) + + add_child(model) + func _generate_model(model_dict : Dictionary, culture : String = "", is_unit : bool = false) -> Node3D: const file_key : StringName = &"file" const scale_key : StringName = &"scale" -- cgit v1.2.3-56-ga3b1