summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2023-04-10 09:04:07 +0200
committerStanislaw Halik <sthalik@misaki.pl>2023-04-10 10:35:00 +0200
commit95b940b807eb213e2e86a32f8f4cc98fd6b13400 (patch)
treec42c9ba29424e56e27ed2b81eecf8c2d07dde910 /src
parent8c2dca2cfaf57f22fe2fe84d939d0071c06ddd6c (diff)
src/world: cleanup & fix hash stuff
Diffstat (limited to 'src')
-rw-r--r--src/character.cpp1
-rw-r--r--src/entity.cpp20
-rw-r--r--src/world.cpp5
-rw-r--r--src/world.hpp8
4 files changed, 20 insertions, 14 deletions
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 <cmath>
#include <utility>
#include <algorithm>
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<global_coords, Vector2b> entity::normalize_coords(global_coords coord, Vect
}
template<bool neighbor = true>
-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<false>(&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<false>(&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<chunk_coords_>::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 <unordered_map>
+#include "compat/int-hash.hpp"
#include <memory>
+#include <unordered_map>
#include <tsl/robin_map.h>
template<>
@@ -17,6 +18,9 @@ namespace floormat {
struct entity;
template<typename T> 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<chunk_coords_, chunk> _chunks;
- tsl::robin_map<object_id, std::weak_ptr<entity>> _entities;
+ tsl::robin_map<object_id, std::weak_ptr<entity>, object_id_hasher> _entities;
size_t _last_collection = 0;
size_t _collect_every = 64;
std::shared_ptr<char> _unique_id = std::make_shared<char>('A');