summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2024-04-13 17:42:30 +0200
committerStanislaw Halik <sthalik@misaki.pl>2024-04-13 17:42:30 +0200
commit2260ab972100f1d6679690b730b6f0aa7409b014 (patch)
tree18386a59968c1e7933db702281fc13fe62ff9b1e /src
parentcb13eb86db3593e723ce06c48a5cb2c94505d6ae (diff)
instantiate scenery in a uniform manner
Now using world::make_scenery(). Add `std::monostate` to the `scenery_proto` variant to catch potential bugs.
Diffstat (limited to 'src')
-rw-r--r--src/object-type.hpp1
-rw-r--r--src/object.cpp9
-rw-r--r--src/object.hpp16
-rw-r--r--src/scenery.cpp6
-rw-r--r--src/scenery.hpp4
-rw-r--r--src/world.cpp44
-rw-r--r--src/world.hpp8
7 files changed, 65 insertions, 23 deletions
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<typename T> 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<int, int8_t> 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<typename T> 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; },
[]<typename T>(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<generic_scenery_proto, door_scenery_proto>;
+using scenery_proto_variants = std::variant<std::monostate, generic_scenery_proto, door_scenery_proto>;
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 <cr/GrowableArray.h>
#include <tsl/robin_map.h>
@@ -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<chunk*, 8>
{
std::array<chunk*, 8> ret;
@@ -283,9 +294,32 @@ std::shared_ptr<T> world::find_object(object_id id)
}
}
-template std::shared_ptr<object> world::find_object(object_id id);
-template std::shared_ptr<critter> world::find_object(object_id id);
-template std::shared_ptr<scenery> world::find_object(object_id id);
-template std::shared_ptr<light> world::find_object(object_id id);
+template std::shared_ptr<object> world::find_object<object>(object_id id);
+template std::shared_ptr<critter> world::find_object<critter>(object_id id);
+template std::shared_ptr<scenery> world::find_object<scenery>(object_id id);
+template std::shared_ptr<light> world::find_object<light>(object_id id);
+
+template<bool sorted>
+std::shared_ptr<scenery> world::make_scenery(object_id id, global_coords pos, scenery_proto&& proto)
+{
+ using type = std::shared_ptr<scenery>;
+
+ 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<generic_scenery, sorted>(id, pos, move(p), move(proto));
+ },
+ [&](door_scenery_proto&& p) -> type {
+ fm_debug_assert(p.scenery_type() == scenery_type::door);
+ return make_object<door_scenery, sorted>(id, pos, move(p), move(proto));
+ },
+ }, move(proto.subtype));
+}
+
+template std::shared_ptr<scenery> world::make_scenery<false>(object_id id, global_coords pos, scenery_proto&& proto);
+template std::shared_ptr<scenery> world::make_scenery<true>(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<typename T> struct shared_ptr_wrapper;
struct object;
struct critter;
struct critter_proto;
-template<typename T> struct object_type_;
-template<typename T> struct shared_ptr_wrapper;
+struct scenery;
+struct scenery_proto;
class world final
{
@@ -49,6 +50,7 @@ private:
std::shared_ptr<object> 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<object>(ret), pos, sorted);
return ret;
}
-
+ template<bool sorted = true> std::shared_ptr<scenery> make_scenery(object_id id, global_coords pos, scenery_proto&& proto);
template<typename T = object> std::shared_ptr<T> find_object(object_id id);
shared_ptr_wrapper<critter> ensure_player_character(object_id& id, critter_proto p);