From 9d35200ea54a0729d489dc2c76823a2a13cdcac4 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 14 Apr 2024 08:53:35 +0200 Subject: add find_object specialization for scenery subtypes --- compat/base-of.hpp | 8 ++++++++ src/scenery-proto.hpp | 3 +++ src/scenery-type.hpp | 2 ++ src/scenery.hpp | 3 +++ src/world.cpp | 27 ++++++++++++++++++++++----- src/world.hpp | 4 ++++ test/save.cpp | 23 ++++++++++++++++++----- 7 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 compat/base-of.hpp diff --git a/compat/base-of.hpp b/compat/base-of.hpp new file mode 100644 index 00000000..d5f539d5 --- /dev/null +++ b/compat/base-of.hpp @@ -0,0 +1,8 @@ +#pragma once + +namespace floormat { + +template +constexpr inline bool is_strict_base_of = std::is_base_of_v && !std::is_same_v; + +} // namespace floormat diff --git a/src/scenery-proto.hpp b/src/scenery-proto.hpp index c5889b04..44c8fe6f 100644 --- a/src/scenery-proto.hpp +++ b/src/scenery-proto.hpp @@ -41,4 +41,7 @@ struct scenery_proto : object_proto scenery_proto& operator=(scenery_proto&&) noexcept; }; +template<> struct scenery_type_ : std::integral_constant {}; +template<> struct scenery_type_ : std::integral_constant {}; + } // namespace floormat diff --git a/src/scenery-type.hpp b/src/scenery-type.hpp index 3879e225..98cb9b10 100644 --- a/src/scenery-type.hpp +++ b/src/scenery-type.hpp @@ -6,4 +6,6 @@ enum class scenery_type : unsigned char { none, generic, door, COUNT, }; +template struct scenery_type_; + } // namespace floormat diff --git a/src/scenery.hpp b/src/scenery.hpp index 6699fe2f..beb06db5 100644 --- a/src/scenery.hpp +++ b/src/scenery.hpp @@ -66,4 +66,7 @@ template<> struct object_type_ : std::integral_constant struct object_type_ : std::integral_constant {}; template<> struct object_type_ : std::integral_constant {}; +template<> struct scenery_type_ : std::integral_constant {}; +template<> struct scenery_type_ : std::integral_constant {}; + } // namespace floormat diff --git a/src/world.cpp b/src/world.cpp index 9f7c0cc3..dd6f2ca7 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -196,7 +196,12 @@ void world::set_object_counter(object_id value) void world::throw_on_wrong_object_type(object_id id, object_type actual, object_type expected) { - fm_throw("object '{}' has wrong object type '{}', should be '{}'"_cf, id, (size_t)actual, (size_t)expected); + fm_throw("object {} has wrong object type {}, should be {}"_cf, id, (size_t)actual, (size_t)expected); +} + +void world::throw_on_wrong_scenery_type(object_id id, scenery_type actual, scenery_type expected) +{ + fm_throw("object {} has wrong scenery type {}, should be {}"_cf, id, (size_t)actual, (size_t)expected); } void world::throw_on_empty_scenery_proto(object_id id, global_coords pos, Vector2b offset) @@ -285,18 +290,30 @@ std::shared_ptr world::find_object(object_id id) return {}; else if constexpr(std::is_same_v) return ptr; + else if (ptr->type() != object_type_::value) [[unlikely]] + throw_on_wrong_object_type(id, ptr->type(), object_type_::value); + else + return static_pointer_cast(move(ptr)); +} + +template +requires is_strict_base_of +std::shared_ptr world::find_object(object_id id) +{ + if (auto ptr = find_object(id); !ptr) + return {}; + else if (ptr->scenery_type() != scenery_type_::value) [[unlikely]] + throw_on_wrong_scenery_type(id, ptr->scenery_type(), scenery_type_::value); else - { - if (!(ptr->type() == object_type_::value)) [[unlikely]] - throw_on_wrong_object_type(id, ptr->type(), object_type_::value); return static_pointer_cast(move(ptr)); - } } template std::shared_ptr world::find_object(object_id id); template std::shared_ptr world::find_object(object_id id); template std::shared_ptr world::find_object(object_id id); template std::shared_ptr world::find_object(object_id id); +template std::shared_ptr world::find_object(object_id id); +template std::shared_ptr world::find_object(object_id id); template std::shared_ptr world::make_scenery(object_id id, global_coords pos, scenery_proto&& proto) diff --git a/src/world.hpp b/src/world.hpp index e92979db..52cb1f64 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -1,8 +1,10 @@ #pragma once #include "compat/safe-ptr.hpp" +#include "compat/base-of.hpp" #include "chunk.hpp" #include "global-coords.hpp" #include "object-type.hpp" +#include "scenery-type.hpp" #include "loader/policy.hpp" #include #include @@ -50,6 +52,7 @@ private: std::shared_ptr find_object_(object_id id); [[noreturn]] static void throw_on_wrong_object_type(object_id id, object_type actual, object_type expected); + [[noreturn]] static void throw_on_wrong_scenery_type(object_id id, scenery_type actual, scenery_type expected); [[noreturn]] static void throw_on_empty_scenery_proto(object_id id, global_coords pos, Vector2b offset); friend struct object; @@ -91,6 +94,7 @@ public: } template std::shared_ptr make_scenery(object_id id, global_coords pos, scenery_proto&& proto); template std::shared_ptr find_object(object_id id); + template requires is_strict_base_of std::shared_ptr find_object(object_id id); shared_ptr_wrapper ensure_player_character(object_id& id, critter_proto p); shared_ptr_wrapper ensure_player_character(object_id& id); diff --git a/test/save.cpp b/test/save.cpp index 8edb5b4d..675041c7 100644 --- a/test/save.cpp +++ b/test/save.cpp @@ -1,6 +1,7 @@ #include "app.hpp" #include "src/world.hpp" #include "loader/loader.hpp" +#include "loader/scenery-cell.hpp" #include "src/scenery.hpp" #include "src/scenery-proto.hpp" #include "src/critter.hpp" @@ -264,18 +265,30 @@ void test_save_objs() fm_assert(p.falloff == obj2.falloff); fm_assert(p.enabled == obj2.enabled); assert_chunks_equal(w.at(ch), w2.at(ch)); + //const auto obj3 = w.find_object(id); // must fail + } + + { + // --- scenery --- + auto w = world(); + scenery_proto p; + p.atlas = loader.invalid_scenery_atlas().proto->atlas; + p.subtype = generic_scenery_proto{}; + constexpr auto ch = chunk_coords_{-3, 4, 0}; + constexpr auto coord = global_coords{ch, { 3, 4}}; + const auto id = w.make_id(); + const auto objʹ = w.make_scenery(id, coord, move(p)); + const auto obj = std::static_pointer_cast(objʹ); + const auto obj2 = w.find_object(id); + //const auto obj3 = w.find_object(id); // must fail + fm_assert(obj == obj2); } #if 0 - constexpr auto coord = global_coords{{-3, 4, 0}, { 3, 4}}; constexpr auto coord = global_coords{{ 5, -6, 0}, { 4, 7}}; constexpr auto coord = global_coords{{-7, 8, 0}, { 9, 1}}; constexpr auto coord = global_coords{{ 9, 0, 0}, {15, 0}}; #endif - - { - - } } } // namespace -- cgit v1.2.3