Skip to content

Commit

Permalink
Cache model and animation definition dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
Hop311 committed Jul 30, 2024
1 parent bf4d061 commit 9aa25a7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
48 changes: 31 additions & 17 deletions extension/src/openvic-extension/singletons/ModelSingleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ GFX::Actor const* ModelSingleton::get_cultural_actor(
return actor;
}

Dictionary ModelSingleton::make_animation_dict(GFX::Actor::Animation const& animation) const {
Dictionary ModelSingleton::get_animation_dict(GFX::Actor::Animation const& animation) {
const animation_map_t::const_iterator it = animation_cache.find(&animation);
if (it != animation_cache.end()) {
return it->second;
}

static const StringName file_key = "file";
static const StringName time_key = "time";

Expand All @@ -107,10 +112,17 @@ Dictionary ModelSingleton::make_animation_dict(GFX::Actor::Animation const& anim
dict[file_key] = std_view_to_godot_string(animation.get_file());
dict[time_key] = animation.get_scroll_time().to_float();

animation_cache.emplace(&animation, dict);

return dict;
}

Dictionary ModelSingleton::make_model_dict(GFX::Actor const& actor) const {
Dictionary ModelSingleton::get_model_dict(GFX::Actor const& actor) {
const model_map_t::const_iterator it = model_cache.find(&actor);
if (it != model_cache.end()) {
return it->second;
}

static const StringName file_key = "file";
static const StringName scale_key = "scale";
static const StringName idle_key = "idle";
Expand All @@ -125,7 +137,7 @@ Dictionary ModelSingleton::make_model_dict(GFX::Actor const& actor) const {

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);
dict[key] = get_animation_dict(*animation);
}
};

Expand Down Expand Up @@ -159,7 +171,7 @@ Dictionary ModelSingleton::make_model_dict(GFX::Actor const& actor) const {
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);
attachment_dict[attachment_model_key] = get_model_dict(*attachment_actor);

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

Expand All @@ -177,6 +189,8 @@ Dictionary ModelSingleton::make_model_dict(GFX::Actor const& actor) const {
}
}

model_cache.emplace(&actor, dict);

return dict;
}

Expand All @@ -185,7 +199,7 @@ Dictionary ModelSingleton::make_model_dict(GFX::Actor const& actor) const {
template<UnitType::branch_t Branch>
bool ModelSingleton::add_unit_dict(
ordered_set<UnitInstanceGroupBranched<Branch>*> const& units, TypedArray<Dictionary>& unit_array
) const {
) {
using _UnitInstanceGroup = UnitInstanceGroupBranched<Branch>;

GameSingleton const* game_singleton = GameSingleton::get_singleton();
Expand Down Expand Up @@ -268,13 +282,13 @@ bool ModelSingleton::add_unit_dict(

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

dict[model_key] = make_model_dict(*actor);
dict[model_key] = get_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_model_key] = get_model_dict(*mount_actor);
dict[mount_attach_node_key] = std_view_to_godot_string(mount_attach_node_name);
} else {
UtilityFunctions::push_error(vformat(
Expand Down Expand Up @@ -311,7 +325,7 @@ bool ModelSingleton::add_unit_dict(
return ret;
}

TypedArray<Dictionary> ModelSingleton::get_units() const {
TypedArray<Dictionary> ModelSingleton::get_units() {
GameSingleton const* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, {});
InstanceManager const* instance_manager = game_singleton->get_instance_manager();
Expand Down Expand Up @@ -340,40 +354,40 @@ TypedArray<Dictionary> ModelSingleton::get_units() const {
return ret;
}

Dictionary ModelSingleton::get_cultural_gun_model(String const& culture) const {
Dictionary ModelSingleton::get_cultural_gun_model(String const& culture) {
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);
return get_model_dict(*actor);
}

Dictionary ModelSingleton::get_cultural_helmet_model(String const& culture) const {
Dictionary ModelSingleton::get_cultural_helmet_model(String const& culture) {
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);
return get_model_dict(*actor);
}

Dictionary ModelSingleton::get_flag_model(bool floating) const {
Dictionary ModelSingleton::get_flag_model(bool floating) {
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);
return get_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();
Expand Down Expand Up @@ -431,7 +445,7 @@ bool ModelSingleton::add_building_dict(

Dictionary dict;

dict[model_key] = make_model_dict(*actor);
dict[model_key] = get_model_dict(*actor);

dict[position_key] = game_singleton->map_position_to_world_coords(
position_ptr != nullptr ? *position_ptr : province_definition.get_centre()
Expand All @@ -447,7 +461,7 @@ bool ModelSingleton::add_building_dict(
return true;
}

TypedArray<Dictionary> ModelSingleton::get_buildings() const {
TypedArray<Dictionary> ModelSingleton::get_buildings() {
GameSingleton const* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, {});
InstanceManager const* instance_manager = game_singleton->get_instance_manager();
Expand Down
24 changes: 15 additions & 9 deletions extension/src/openvic-extension/singletons/ModelSingleton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,32 @@ namespace OpenVic {
std::string_view culture, std::string_view name, std::string_view fallback_name
) const;

godot::Dictionary make_animation_dict(GFX::Actor::Animation const& animation) const;
godot::Dictionary make_model_dict(GFX::Actor const& actor) const;
using animation_map_t = deque_ordered_map<GFX::Actor::Animation const*, godot::Dictionary>;
using model_map_t = deque_ordered_map<GFX::Actor const*, godot::Dictionary>;

animation_map_t animation_cache;
model_map_t model_cache;

godot::Dictionary get_animation_dict(GFX::Actor::Animation const& animation);
godot::Dictionary get_model_dict(GFX::Actor const& actor);

template<UnitType::branch_t Branch>
bool add_unit_dict(
ordered_set<UnitInstanceGroupBranched<Branch>*> const& units, godot::TypedArray<godot::Dictionary>& unit_array
) const;
);

bool add_building_dict(
BuildingInstance const& building, ProvinceInstance const& province,
godot::TypedArray<godot::Dictionary>& building_array
) const;
);

public:
godot::TypedArray<godot::Dictionary> 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::TypedArray<godot::Dictionary> get_units();
godot::Dictionary get_cultural_gun_model(godot::String const& culture);
godot::Dictionary get_cultural_helmet_model(godot::String const& culture);

godot::Dictionary get_flag_model(bool floating) const;
godot::Dictionary get_flag_model(bool floating);

godot::TypedArray<godot::Dictionary> get_buildings() const;
godot::TypedArray<godot::Dictionary> get_buildings();
};
}

0 comments on commit 9aa25a7

Please sign in to comment.