#pragma once #include "compat/int-hash.hpp" #include "compat/defs.hpp" #include "chunk.hpp" #include "global-coords.hpp" #include "entity-type.hpp" #include "compat/exception.hpp" #include #include namespace floormat { struct entity; template struct entity_type_; struct world final { private: struct chunk_tuple final { static constexpr chunk_coords invalid_coords = { -1 << 15, -1 << 15 }; chunk* c = nullptr; chunk_coords pos = invalid_coords; } _last_chunk; static constexpr size_t initial_capacity = 64; static constexpr float max_load_factor = .5; static constexpr auto hasher = [](chunk_coords c) constexpr -> size_t { return int_hash((size_t)c.y << 16 | (size_t)c.x); }; std::unordered_map _chunks; std::unordered_map> _entities; size_t _last_collection = 0; size_t _collect_every = 64; std::shared_ptr _unique_id = std::make_shared('A'); object_id _entity_counter = 0; uint64_t _current_frame = 1; // zero is special for struct entity bool _teardown : 1 = false; explicit world(size_t capacity); void do_make_entity(const std::shared_ptr& e, global_coords pos, bool sorted); void do_kill_entity(object_id id); std::shared_ptr find_entity_(object_id id); friend struct entity; public: explicit world(); ~world() noexcept; struct pair final { chunk& c; tile_ref t; }; // NOLINT template explicit world(std::unordered_map&& chunks); chunk& operator[](chunk_coords c) noexcept; pair operator[](global_coords pt) noexcept; bool contains(chunk_coords c) const noexcept; void clear(); void collect(bool force = false); void maybe_collect(); size_t size() const noexcept { return _chunks.size(); } const auto& chunks() const noexcept { return _chunks; } void serialize(StringView filename); static world deserialize(StringView filename); void set_collect_threshold(size_t value) { _collect_every = value; } size_t collect_threshold() const noexcept { return _collect_every; } auto frame_no() const { return _current_frame; } auto increment_frame_no() { return _current_frame++; } template requires requires(chunk& c) { T{object_id(), c, std::declval()...}; std::is_base_of_v; } std::shared_ptr make_entity(object_id id, global_coords pos, Xs&&... xs) { auto ret = std::shared_ptr(new T{id, operator[](pos.chunk()), std::forward(xs)...}); do_make_entity(std::static_pointer_cast(ret), pos, sorted); return ret; } template std::shared_ptr find_entity(object_id id); bool is_teardown() const { return _teardown; } object_id entity_counter() const { return _entity_counter; } [[nodiscard]] object_id make_id() { return ++_entity_counter; } void set_entity_counter(object_id value); world& operator=(world&& w) noexcept; world(world&& w) noexcept; fm_DECLARE_DEPRECATED_COPY_ASSIGNMENT(world); }; template world::world(std::unordered_map&& chunks) : world{std::max(initial_capacity, size_t(1/max_load_factor * 2 * chunks.size()))} { for (auto&& [coord, c] : chunks) operator[](coord) = std::move(c); } template std::shared_ptr world::find_entity(object_id id) { static_assert(std::is_base_of_v); // make it a dependent name so that including "src/entity.hpp" isn't needed using U = std::conditional_t, T, entity>; if (std::shared_ptr ptr = find_entity_(id); !ptr) return {}; else if constexpr(std::is_same_v) return ptr; else { fm_soft_assert(ptr->type() == entity_type_::value); return std::static_pointer_cast(std::move(ptr)); } } } // namespace floormat