From 95b940b807eb213e2e86a32f8f4cc98fd6b13400 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 10 Apr 2023 09:04:07 +0200 Subject: src/world: cleanup & fix hash stuff --- src/character.cpp | 1 + src/entity.cpp | 20 ++++++++++---------- src/world.cpp | 5 +++-- src/world.hpp | 8 ++++++-- 4 files changed, 20 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/character.cpp b/src/character.cpp index e6f2da04..a2098788 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -5,6 +5,7 @@ #include "src/entity.hpp" #include "shaders/tile.hpp" #include "src/RTree-search.hpp" +#include "compat/exception.hpp" #include #include #include diff --git a/src/entity.cpp b/src/entity.cpp index be91db7f..13f19620 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -127,7 +127,7 @@ Pair entity::normalize_coords(global_coords coord, Vect } template -static bool do_search(struct chunk* c, chunk_coords_ coord, object_id id, Vector2 min, Vector2 max, Vector2i off = {}) +static bool do_search(struct chunk* c, chunk_coords_ coord, object_id id, Vector2 min, Vector2 max, Vector2b off = {}) { if constexpr(neighbor) { @@ -168,15 +168,15 @@ bool entity::can_move_to(Vector2i delta, global_coords coord2, Vector2b offset, half_bbox = Vector2(bbox_size)*.5f, min = center - half_bbox, max = min + Vector2(bbox_size); auto ch = chunk_coords_{coord_.chunk(), coord_.z()}; - return do_search(&c_, ch, id, min, max) && - do_search(&c_, ch, id, min, max, {-1, -1}) && - do_search(&c_, ch, id, min, max, {-1, 0}) && - do_search(&c_, ch, id, min, max, { 0, -1}) && - do_search(&c_, ch, id, min, max, { 1, 1}) && - do_search(&c_, ch, id, min, max, { 1, 0}) && - do_search(&c_, ch, id, min, max, { 0, 1}) && - do_search(&c_, ch, id, min, max, { 1, -1}) && - do_search(&c_, ch, id, min, max, {-1, 1}); + if (!do_search(&c_, ch, id, min, max)) + return false; + constexpr Vector2b offsets[] = { + {-1, -1}, {-1, 0}, { 0, -1}, { 1, 1}, { 1, 0}, { 0, 1}, { 1, -1}, {-1, 1}, + }; + for (const auto& off : offsets) + if (!do_search(&c_, ch, id, min, max, off)) + return false; + return true; } bool entity::can_move_to(Vector2i delta) diff --git a/src/world.cpp b/src/world.cpp index f0818b44..028ddd45 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -2,6 +2,7 @@ #include "chunk.hpp" #include "entity.hpp" #include "compat/int-hash.hpp" +#include "compat/exception.hpp" using namespace floormat; @@ -12,9 +13,9 @@ size_t std::hash::operator()(const chunk_coords_& coord) const no x |= size_t(uint16_t(coord.y)) << 16; x |= size_t(uint16_t(coord.x)); if constexpr(sizeof(size_t) > 4) - x |= size_t(uint8_t(coord.z+8) & 0xf) << 20; + x |= size_t(uint8_t(coord.z-chunk_min_z) & 0xf) << 32; else - x ^= size_t(uint8_t(coord.z+8) & 0xf) * size_t(1664525); + x ^= size_t(uint8_t(coord.z-chunk_min_z) & 0xf) * size_t(1664525); return int_hash(x); } diff --git a/src/world.hpp b/src/world.hpp index 90067c35..2dce958c 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -4,8 +4,9 @@ #include "global-coords.hpp" #include "entity-type.hpp" #include "compat/exception.hpp" -#include +#include "compat/int-hash.hpp" #include +#include #include template<> @@ -17,6 +18,9 @@ namespace floormat { struct entity; template struct entity_type_; +struct object_id_hasher { + size_t operator()(object_id id) const noexcept { return int_hash(id); } +}; struct world final { @@ -32,7 +36,7 @@ private: } _last_chunk; std::unordered_map _chunks; - tsl::robin_map> _entities; + tsl::robin_map, object_id_hasher> _entities; size_t _last_collection = 0; size_t _collect_every = 64; std::shared_ptr _unique_id = std::make_shared('A'); -- cgit v1.2.3