diff options
Diffstat (limited to 'src/world.hpp')
-rw-r--r-- | src/world.hpp | 43 |
1 files changed, 21 insertions, 22 deletions
diff --git a/src/world.hpp b/src/world.hpp index 29d5a8dd..589bbd67 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -4,7 +4,6 @@ #include "chunk.hpp" #include "global-coords.hpp" #include "entity-type.hpp" -#include <cstddef> #include <unordered_map> #include <memory> @@ -22,24 +21,24 @@ private: chunk_coords pos = invalid_coords; } _last_chunk; - static constexpr std::size_t initial_capacity = 64; + static constexpr size_t initial_capacity = 64; static constexpr float max_load_factor = .5; - static constexpr auto hasher = [](chunk_coords c) constexpr -> std::size_t { - return int_hash((std::size_t)c.y << 16 | (std::size_t)c.x); + 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<chunk_coords, chunk, decltype(hasher)> _chunks; - std::unordered_map<std::uint64_t, std::weak_ptr<entity>> _entities; - std::size_t _last_collection = 0; - std::size_t _collect_every = 64; + std::unordered_map<object_id, std::weak_ptr<entity>> _entities; + size_t _last_collection = 0; + size_t _collect_every = 64; std::shared_ptr<char> _unique_id = std::make_shared<char>('A'); - std::uint64_t _entity_counter = 0; + object_id _entity_counter = 0; bool _teardown : 1 = false; - explicit world(std::size_t capacity); + explicit world(size_t capacity); void do_make_entity(const std::shared_ptr<entity>& e, global_coords pos, bool sorted); - void do_kill_entity(std::uint64_t id); - std::shared_ptr<entity> find_entity_(std::uint64_t id); + void do_kill_entity(object_id id); + std::shared_ptr<entity> find_entity_(object_id id); friend struct entity; @@ -58,18 +57,18 @@ public: void clear(); void collect(bool force = false); void maybe_collect(); - std::size_t size() const noexcept { return _chunks.size(); } + 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(std::size_t value) { _collect_every = value; } - std::size_t collect_threshold() const noexcept { return _collect_every; } + void set_collect_threshold(size_t value) { _collect_every = value; } + size_t collect_threshold() const noexcept { return _collect_every; } template<typename T, bool sorted = true, typename... Xs> - requires requires(chunk& c) { T{std::uint64_t(), c, entity_type(), std::declval<Xs>()...}; } - std::shared_ptr<T> make_entity(std::uint64_t id, global_coords pos, Xs&&... xs) + requires requires(chunk& c) { T{object_id(), c, entity_type(), std::declval<Xs>()...}; } + std::shared_ptr<T> make_entity(object_id id, global_coords pos, Xs&&... xs) { static_assert(std::is_base_of_v<entity, T>); auto ret = std::shared_ptr<T>(new T{id, operator[](pos.chunk()), entity_type_<T>::value, std::forward<Xs>(xs)...}); @@ -77,12 +76,12 @@ public: return ret; } - template<typename T = entity> std::shared_ptr<T> find_entity(std::uint64_t id); + template<typename T = entity> std::shared_ptr<T> find_entity(object_id 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); + 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; @@ -92,14 +91,14 @@ public: template<typename Hash, typename Alloc, typename Pred> world::world(std::unordered_map<chunk_coords, chunk, Hash, Alloc, Pred>&& chunks) : - world{std::max(initial_capacity, std::size_t(1/max_load_factor * 2 * chunks.size()))} + 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<typename T> -std::shared_ptr<T> world::find_entity(std::uint64_t id) +std::shared_ptr<T> world::find_entity(object_id id) { static_assert(std::is_base_of_v<entity, T>); // make it a dependent name so that including "src/entity.hpp" isn't needed |