From 2260ab972100f1d6679690b730b6f0aa7409b014 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 13 Apr 2024 17:42:30 +0200 Subject: instantiate scenery in a uniform manner Now using world::make_scenery(). Add `std::monostate` to the `scenery_proto` variant to catch potential bugs. --- src/object-type.hpp | 1 + src/object.cpp | 9 ++++++--- src/object.hpp | 16 ++++++++-------- src/scenery.cpp | 6 ++++-- src/scenery.hpp | 4 ++-- src/world.cpp | 44 +++++++++++++++++++++++++++++++++++++++----- src/world.hpp | 8 +++++--- 7 files changed, 65 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/object-type.hpp b/src/object-type.hpp index 8c84c31a..35b25ccf 100644 --- a/src/object-type.hpp +++ b/src/object-type.hpp @@ -5,5 +5,6 @@ namespace floormat { enum class object_type : unsigned char { none, critter, scenery, light, COUNT, }; +template struct object_type_; } // namespace floormat diff --git a/src/object.cpp b/src/object.cpp index 03491de3..cd883795 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -43,10 +43,12 @@ constexpr inline Pair normalize_coord(const int8_t cur, const int n } // namespace bool object_proto::operator==(const object_proto&) const = default; -object_proto& object_proto::operator=(const object_proto&) = default; +object_proto::object_proto(const object_proto&) noexcept = default; +object_proto& object_proto::operator=(const object_proto&) noexcept = default; +object_proto::object_proto(object_proto&&) noexcept = default; +object_proto& object_proto::operator=(object_proto&&) noexcept = default; object_proto::~object_proto() noexcept = default; -object_proto::object_proto() = default; -object_proto::object_proto(const object_proto&) = default; +object_proto::object_proto() noexcept = default; object_type object_proto::type_of() const noexcept { return type; } object::object(object_id id, class chunk& c, const object_proto& proto) : @@ -90,6 +92,7 @@ float object::ordinal(local_coords xy, Vector2b offset, Vector2s z_offset) const size_t object::index() const { auto& c = chunk(); + fm_assert(c._objects_sorted); const auto fn = [id = id](const auto& a, const auto&) { return a->id < id; }; auto& es = c._objects; auto it = std::lower_bound(es.cbegin(), es.cend(), nullptr, fn); diff --git a/src/object.hpp b/src/object.hpp index ce793fe8..e1582419 100644 --- a/src/object.hpp +++ b/src/object.hpp @@ -11,7 +11,6 @@ namespace floormat { -template struct object_type_; class anim_atlas; class world; class chunk; @@ -28,15 +27,16 @@ struct object_proto rotation r = rotation::N; pass_mode pass = pass_mode::see_through; // todo move to struct scenery, add inherit bit - object_proto& operator=(const object_proto&); - object_proto(); - object_proto(const object_proto&); - - virtual bool operator==(const object_proto&) const; - bool operator!=(const object_proto& o) const { return !operator==(o); } + object_proto() noexcept; virtual ~object_proto() noexcept; - + virtual bool operator==(const object_proto&) const; + bool operator!=(const object_proto& o) const noexcept { return !operator==(o); } object_type type_of() const noexcept; + + object_proto& operator=(const object_proto&) noexcept; + object_proto(const object_proto&) noexcept; + object_proto& operator=(object_proto&&) noexcept; + object_proto(object_proto&&) noexcept; }; struct object diff --git a/src/scenery.cpp b/src/scenery.cpp index 4d47da5a..26213757 100644 --- a/src/scenery.cpp +++ b/src/scenery.cpp @@ -1,6 +1,7 @@ #include "scenery.hpp" #include "compat/assert.hpp" #include "compat/exception.hpp" +#include "compat/overloaded.hpp" #include "tile-constants.hpp" #include "anim-atlas.hpp" #include "rotation.inl" @@ -32,9 +33,10 @@ scenery_proto::operator bool() const { return atlas != nullptr; } enum scenery_type scenery_proto::scenery_type() const { - return std::visit( + return std::visit(overloaded { + [](std::monostate) { return scenery_type::none; }, [](const T&) { return T::scenery_type(); }, - subtype + }, subtype ); } diff --git a/src/scenery.hpp b/src/scenery.hpp index be57296d..6ae7a10a 100644 --- a/src/scenery.hpp +++ b/src/scenery.hpp @@ -33,7 +33,7 @@ struct door_scenery_proto static enum scenery_type scenery_type(); }; -using scenery_proto_variants = std::variant; +using scenery_proto_variants = std::variant; struct scenery_proto : object_proto { @@ -55,7 +55,7 @@ struct scenery : object float depth_offset() const override; enum object_type type() const noexcept override; virtual enum scenery_type scenery_type() const = 0; - virtual explicit operator scenery_proto() const; + virtual explicit operator scenery_proto() const; // todo make protected? protected: scenery(object_id id, class chunk& c, const scenery_proto& proto); diff --git a/src/world.cpp b/src/world.cpp index edab80b6..821e4c65 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -7,6 +7,7 @@ #include "compat/shared-ptr-wrapper.hpp" #include "compat/int-hash.hpp" #include "compat/exception.hpp" +#include "compat/overloaded.hpp" #include #include @@ -118,7 +119,7 @@ chunk* world::at(chunk_coords_ c) noexcept bool world::contains(chunk_coords_ c) const noexcept { - return _chunks.find(c) != _chunks.cend(); + return _chunks.contains(c); } void world::clear() @@ -197,6 +198,16 @@ void world::throw_on_wrong_object_type(object_id id, object_type actual, object_ fm_throw("object '{}' has wrong object 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) +{ + ERR_nospace << "scenery_proto subtype not set" + << " id:" << id + << " pos:" << point{pos, offset}; + auto ch = Vector3i(pos.chunk3()); + auto t = Vector2i(pos.local()); + fm_throw("scenery_proto subtype not set! id:{} chunk:{}x{}x{} tile:{}x{}"_cf, id, ch.x(), ch.y(), ch.z(), t.x(), t.y()); +} + auto world::neighbors(chunk_coords_ coord) -> std::array { std::array ret; @@ -283,9 +294,32 @@ 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::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) +{ + using type = std::shared_ptr; + + return std::visit(overloaded { + [&](std::monostate) -> type { + throw_on_empty_scenery_proto(id, pos, proto.offset); + }, + [&](generic_scenery_proto&& p) -> type { + fm_debug_assert(p.scenery_type() == scenery_type::generic); + return make_object(id, pos, move(p), move(proto)); + }, + [&](door_scenery_proto&& p) -> type { + fm_debug_assert(p.scenery_type() == scenery_type::door); + return make_object(id, pos, move(p), move(proto)); + }, + }, move(proto.subtype)); +} + +template std::shared_ptr world::make_scenery(object_id id, global_coords pos, scenery_proto&& proto); +template std::shared_ptr world::make_scenery(object_id id, global_coords pos, scenery_proto&& proto); } // namespace floormat diff --git a/src/world.hpp b/src/world.hpp index e805ae1f..e92979db 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -9,11 +9,12 @@ namespace floormat { +template struct shared_ptr_wrapper; struct object; struct critter; struct critter_proto; -template struct object_type_; -template struct shared_ptr_wrapper; +struct scenery; +struct scenery_proto; class world final { @@ -49,6 +50,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_empty_scenery_proto(object_id id, global_coords pos, Vector2b offset); friend struct object; @@ -87,7 +89,7 @@ public: do_make_object(std::static_pointer_cast(ret), pos, sorted); return ret; } - + template std::shared_ptr make_scenery(object_id id, global_coords pos, scenery_proto&& proto); template std::shared_ptr find_object(object_id id); shared_ptr_wrapper ensure_player_character(object_id& id, critter_proto p); -- cgit v1.2.3