summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/camera-offset.cpp1
-rw-r--r--src/global-coords.hpp20
-rw-r--r--src/world.cpp30
-rw-r--r--src/world.hpp33
4 files changed, 58 insertions, 26 deletions
diff --git a/src/camera-offset.cpp b/src/camera-offset.cpp
index 84dc6141..bf700138 100644
--- a/src/camera-offset.cpp
+++ b/src/camera-offset.cpp
@@ -12,6 +12,7 @@ with_shifted_camera_offset::with_shifted_camera_offset(tile_shader& shader, chun
constexpr auto chunk_size = TILE_MAX_DIM20d*dTILE_SIZE;
const auto offset = _camera + tile_shader::project(Vector3d(c) * chunk_size);
+ first.x -= 8; first.y -= 8; last.x += 8; last.y += 8; // Z levels
const auto len_x = (float)(last.x - first.x), cx = (float)(c.x - first.x), cy = (float)(c.y - first.y);
const float depth_offset = shader.depth_tile_size*(cy*TILE_MAX_DIM*len_x*TILE_MAX_DIM + cx*TILE_MAX_DIM);
diff --git a/src/global-coords.hpp b/src/global-coords.hpp
index d8f238f3..f0d73c6f 100644
--- a/src/global-coords.hpp
+++ b/src/global-coords.hpp
@@ -27,6 +27,17 @@ constexpr Vector2i chunk_coords::operator-(chunk_coords other) const noexcept
return { Int{x} - other.x, Int{y} - other.y };
}
+struct chunk_coords_ final {
+ int16_t x = 0, y = 0;
+ int8_t z = 0;
+
+ explicit constexpr operator chunk_coords() const noexcept { return {x, y}; }
+ constexpr chunk_coords_(chunk_coords c) noexcept : x{c.x}, y{c.y} {}
+ constexpr chunk_coords_() noexcept = default;
+ constexpr chunk_coords_(int16_t x, int16_t y, int8_t z = 0) : x{x}, y{y}, z{z} {}
+ constexpr bool operator==(const chunk_coords_&) const noexcept = default;
+};
+
struct global_coords final {
using u0 = std::integral_constant<uint32_t, (1<<15)>;
using s0 = std::integral_constant<int32_t, int32_t(u0::value)>;
@@ -35,7 +46,7 @@ struct global_coords final {
uint32_t x = u0::value<<4|z0::value<<20, y = u0::value<<4;
constexpr global_coords() noexcept = default;
- constexpr global_coords(chunk_coords c, local_coords xy, int8_t z = 0) :
+ constexpr global_coords(chunk_coords c, local_coords xy, int8_t z) noexcept :
x{
uint32_t((c.x + s0::value) << 4) | (xy.x & 0x0f) |
uint32_t(((int)z + z0::value) & 0x0f) << 20
@@ -44,8 +55,11 @@ struct global_coords final {
{}
constexpr global_coords(uint32_t x, uint32_t y) noexcept : x{x}, y{y} {}
constexpr global_coords(int32_t x, int32_t y, int8_t z = 0) noexcept :
- x{uint32_t(x + (s0::value<<4)) | uint32_t(((z + z0::value) & 0x0f) << 20)},
- y{uint32_t(y + (s0::value<<4))}
+ x{uint32_t(x + (s0::value<<4)) | uint32_t(((z + z0::value) & 0x0f) << 20)},
+ y{uint32_t(y + (s0::value<<4))}
+ {}
+ constexpr global_coords(chunk_coords_ c, local_coords xy) noexcept :
+ global_coords{chunk_coords{c.x, c.y}, xy, c.z}
{}
constexpr local_coords local() const noexcept;
diff --git a/src/world.cpp b/src/world.cpp
index 9d669e62..cdf8c087 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -1,11 +1,35 @@
#include "world.hpp"
#include "chunk.hpp"
#include "entity.hpp"
+#include "compat/int-hash.hpp"
+
+using namespace floormat;
+
+size_t std::hash<chunk_coords_>::operator()(const chunk_coords_& coord) const noexcept
+{
+ std::size_t x = 0;
+
+ 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;
+ else
+ x ^= size_t(uint8_t(coord.z+8) & 0xf) * size_t(1664525);
+
+ return int_hash(x);
+}
namespace floormat {
world::world(world&& w) noexcept = default;
+world::world(std::unordered_map<chunk_coords_, chunk>&& 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);
+}
+
world& world::operator=(world&& w) noexcept
{
if (&w != this) [[likely]]
@@ -50,12 +74,12 @@ world::~world() noexcept
_entities.clear();
}
-world::world(size_t capacity) : _chunks{capacity, hasher}
+world::world(size_t capacity) : _chunks{capacity}
{
_chunks.max_load_factor(max_load_factor);
}
-chunk& world::operator[](chunk_coords coord) noexcept
+chunk& world::operator[](chunk_coords_ coord) noexcept
{
auto& [c, coord2] = _last_chunk;
if (coord != coord2)
@@ -70,7 +94,7 @@ auto world::operator[](global_coords pt) noexcept -> pair
return { c, c[pt.local()] };
}
-bool world::contains(chunk_coords c) const noexcept
+bool world::contains(chunk_coords_ c) const noexcept
{
return _chunks.find(c) != _chunks.cend();
}
diff --git a/src/world.hpp b/src/world.hpp
index b057b854..98fa7f47 100644
--- a/src/world.hpp
+++ b/src/world.hpp
@@ -1,5 +1,4 @@
#pragma once
-#include "compat/int-hash.hpp"
#include "compat/defs.hpp"
#include "chunk.hpp"
#include "global-coords.hpp"
@@ -8,6 +7,13 @@
#include <unordered_map>
#include <memory>
+namespace floormat { struct chunk_coords_; }
+
+template<>
+struct std::hash<floormat::chunk_coords_> final {
+ floormat::size_t operator()(const floormat::chunk_coords_& coord) const noexcept;
+};
+
namespace floormat {
struct entity;
@@ -17,17 +23,14 @@ struct world final
{
private:
struct chunk_tuple final {
- static constexpr chunk_coords invalid_coords = { -1 << 15, -1 << 15 };
+ static constexpr chunk_coords_ invalid_coords = { -1 << 15, -1 << 15, -8 };
chunk* c = nullptr;
- chunk_coords pos = invalid_coords;
+ 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<chunk_coords, chunk, decltype(hasher)> _chunks;
+ std::unordered_map<chunk_coords_, chunk> _chunks;
std::unordered_map<object_id, std::weak_ptr<entity>> _entities;
size_t _last_collection = 0;
size_t _collect_every = 64;
@@ -47,15 +50,13 @@ private:
public:
explicit world();
~world() noexcept;
+ explicit world(std::unordered_map<chunk_coords_, chunk>&& chunks);
struct pair final { chunk& c; tile_ref t; }; // NOLINT
- template<typename Hash, typename Alloc, typename Pred>
- explicit world(std::unordered_map<chunk_coords, chunk, Hash, Alloc, Pred>&& chunks);
-
- chunk& operator[](chunk_coords c) noexcept;
+ chunk& operator[](chunk_coords_ c) noexcept;
pair operator[](global_coords pt) noexcept;
- bool contains(chunk_coords c) const noexcept;
+ bool contains(chunk_coords_ c) const noexcept;
void clear();
void collect(bool force = false);
void maybe_collect();
@@ -95,14 +96,6 @@ public:
fm_DECLARE_DEPRECATED_COPY_ASSIGNMENT(world);
};
-template<typename Hash, typename Alloc, typename Pred>
-world::world(std::unordered_map<chunk_coords, chunk, Hash, Alloc, Pred>&& 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<typename T>
std::shared_ptr<T> world::find_entity(object_id id)
{