From 87b07a5c872ba37676ea02e6f9961307839137b2 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 18 Mar 2023 02:02:49 +0100 Subject: a --- src/chunk.cpp | 5 +++++ src/chunk.hpp | 3 ++- src/world.cpp | 10 +++++++--- src/world.hpp | 14 ++++---------- 4 files changed, 18 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/chunk.cpp b/src/chunk.cpp index a99facf6..e0a09012 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -108,6 +108,7 @@ bool chunk::bbox::operator==(const bbox& other) const noexcept = default; void chunk::add_entity_unsorted(const std::shared_ptr& e) { + _entities_sorted = false; if (!e->is_dynamic()) mark_scenery_modified(false); if (bbox bb; _bbox_for_scenery(*e, bb)) @@ -117,6 +118,7 @@ void chunk::add_entity_unsorted(const std::shared_ptr& e) void chunk::sort_entities() { + _entities_sorted = true; mark_scenery_modified(false); std::sort(_entities.begin(), _entities.end(), [](const auto& a, const auto& b) { @@ -126,6 +128,7 @@ void chunk::sort_entities() void chunk::add_entity(const std::shared_ptr& e) { + fm_assert(_entities_sorted); if (e->atlas->info().fps == 0) mark_scenery_modified(false); if (bbox bb; _bbox_for_scenery(*e, bb)) @@ -140,6 +143,7 @@ void chunk::add_entity(const std::shared_ptr& e) void chunk::remove_entity(std::size_t i) { + fm_assert(_entities_sorted); fm_debug_assert(i < _entities.size()); const auto& e = _entities[i]; if (!e->is_dynamic()) @@ -152,6 +156,7 @@ void chunk::remove_entity(std::size_t i) const std::vector>& chunk::entities() const { + fm_assert(_entities_sorted); return _entities; } diff --git a/src/chunk.hpp b/src/chunk.hpp index e825ce33..e51c55ac 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -129,7 +129,8 @@ private: _walls_modified : 1 = true, _scenery_modified : 1 = true, _pass_modified : 1 = true, - _teardown : 1 = false; + _teardown : 1 = false, + _entities_sorted : 1 = true; struct bbox final // NOLINT(cppcoreguidelines-pro-type-member-init) { diff --git a/src/world.cpp b/src/world.cpp index ee1ef459..8743981f 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -106,15 +106,19 @@ void world::collect(bool force) fm_debug("world: collected %zu/%zu chunks", len, len0); } -void world::do_make_entity(const std::shared_ptr& e, global_coords pos) +void world::do_make_entity(const std::shared_ptr& e, global_coords pos, bool sorted) { - fm_debug_assert(e->id > 0); + fm_assert(e->id > 0); fm_debug_assert(e->c->world()._unique_id == _unique_id); + fm_assert(!_entities.contains(e->id)); fm_assert(Vector2ui(e->bbox_size).product() > 0); fm_assert(e->type != entity_type::none); e->coord = pos; _entities[e->id] = e; - e->c->add_entity(e); + if (sorted) + e->c->add_entity(e); + else + e->c->add_entity_unsorted(e); } void world::do_kill_entity(std::uint64_t id) diff --git a/src/world.hpp b/src/world.hpp index 08e4d384..0b7b87a5 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -37,7 +37,7 @@ private: explicit world(std::size_t capacity); - void do_make_entity(const std::shared_ptr& e, global_coords pos); + void do_make_entity(const std::shared_ptr& e, global_coords pos, bool sorted); void do_kill_entity(std::uint64_t id); std::shared_ptr find_entity_(std::uint64_t id); @@ -67,27 +67,21 @@ public: void set_collect_threshold(std::size_t value) { _collect_every = value; } std::size_t collect_threshold() const noexcept { return _collect_every; } - template + template requires requires(chunk& c) { T{std::uint64_t(), c, entity_type(), std::declval()...}; } std::shared_ptr make_entity(std::uint64_t id, global_coords pos, Xs&&... xs) { static_assert(std::is_base_of_v); auto ret = std::shared_ptr(new T{id, operator[](pos.chunk()), entity_type_::value, std::forward(xs)...}); - do_make_entity(std::static_pointer_cast(ret), pos); + do_make_entity(std::static_pointer_cast(ret), pos, sorted); return ret; } - template - requires requires(chunk& c) { T{std::uint64_t(), c, entity_type(), std::declval()...}; } - inline std::shared_ptr make_entity(global_coords pos, Xs&&... xs) - { - return make_entity(++_entity_counter, pos, std::forward(xs)...); - } - template std::shared_ptr find_entity(std::uint64_t id); bool is_teardown() const { return _teardown; } std::uint64_t entity_counter() const { return _entity_counter; } + [[nodiscard]] std::uint64_t make_id() { return ++_entity_counter; } void set_entity_counter(std::uint64_t value); world& operator=(world&& w) noexcept; -- cgit v1.2.3