summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/chunk.hpp2
-rw-r--r--src/entity.cpp5
-rw-r--r--src/global-coords.cpp6
-rw-r--r--src/global-coords.hpp31
-rw-r--r--src/world.cpp11
-rw-r--r--src/world.hpp8
6 files changed, 52 insertions, 11 deletions
diff --git a/src/chunk.hpp b/src/chunk.hpp
index 031ba88e..8453bf83 100644
--- a/src/chunk.hpp
+++ b/src/chunk.hpp
@@ -118,7 +118,7 @@ private:
std::array<std::shared_ptr<tile_atlas>, TILE_COUNT> _ground_atlases;
std::array<uint8_t, TILE_COUNT> ground_indexes = {};
std::array<variant_t, TILE_COUNT> _ground_variants = {};
- std::array<std::shared_ptr<tile_atlas>, TILE_COUNT*2> _wall_atlases;
+ std::array<std::shared_ptr<tile_atlas>, TILE_COUNT*2> _wall_atlases; // todo make into vector
std::array<uint16_t, TILE_COUNT*2> wall_indexes = {};
std::array<variant_t, TILE_COUNT*2> _wall_variants = {};
std::vector<std::shared_ptr<entity>> _entities;
diff --git a/src/entity.cpp b/src/entity.cpp
index 13f19620..8aa3752f 100644
--- a/src/entity.cpp
+++ b/src/entity.cpp
@@ -170,10 +170,7 @@ bool entity::can_move_to(Vector2i delta, global_coords coord2, Vector2b offset,
auto ch = chunk_coords_{coord_.chunk(), coord_.z()};
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)
+ for (const auto& off : world::neighbor_offsets)
if (!do_search(&c_, ch, id, min, max, off))
return false;
return true;
diff --git a/src/global-coords.cpp b/src/global-coords.cpp
index de7c9ee2..6ffc06e1 100644
--- a/src/global-coords.cpp
+++ b/src/global-coords.cpp
@@ -18,4 +18,10 @@ static_assert(global_coords{(1u + (1<<15)) << 4 | 3, (2u + (1<<15)) << 4 | 4, nu
static_assert(global_coords{-123, 456, 1}.z() == 1);
static_assert(global_coords{-123, 511, 5}.chunk() == chunk_coords{-8, 31});
+static_assert(chunk_coords_{(short)100, (short)200, (char)300} + Vector3i(1, 2, 3) == chunk_coords_{(short)101, (short)202, (char)303});
+static_assert(chunk_coords_{(short)101, (short)202, (char)303} - Vector3i(1, 2, 3) == chunk_coords_{(short)100, (short)200, (char)300});
+
+static_assert(chunk_coords_{(short)100, (short)200, (char)300} + Vector2i(1, 2) == chunk_coords_{(short)101, (short)202, (char)300});
+static_assert(chunk_coords_{(short)101, (short)202, (char)300} - Vector2i(1, 2) == chunk_coords_{(short)100, (short)200, (char)300});
+
} // namespace floormat
diff --git a/src/global-coords.hpp b/src/global-coords.hpp
index b3681992..6d8e1525 100644
--- a/src/global-coords.hpp
+++ b/src/global-coords.hpp
@@ -1,6 +1,7 @@
#pragma once
#include "local-coords.hpp"
#include "compat/assert.hpp"
+#include <concepts>
#include <Magnum/Magnum.h>
#include <Magnum/Math/Vector2.h>
#include <Magnum/Math/Vector3.h>
@@ -11,7 +12,6 @@ struct chunk_coords final {
int16_t x = 0, y = 0;
constexpr bool operator==(const chunk_coords& other) const noexcept = default;
- constexpr Vector2i operator-(chunk_coords other) const noexcept;
template<typename T>
requires (std::is_floating_point_v<T> || std::is_integral_v<T> && (sizeof(T) > sizeof(x) || std::is_same_v<T, std::decay_t<decltype(x)>>))
@@ -22,11 +22,6 @@ struct chunk_coords final {
explicit constexpr operator Math::Vector3<T>() const noexcept { return Math::Vector3<T>(T(x), T(y), T(0)); }
};
-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;
@@ -35,6 +30,30 @@ struct chunk_coords_ final {
constexpr chunk_coords_(int16_t x, int16_t y, int8_t z) noexcept : x{x}, y{y}, z{z} {}
constexpr chunk_coords_(chunk_coords c, int8_t z) noexcept : x{c.x}, y{c.y}, z{z} {}
constexpr bool operator==(const chunk_coords_&) const noexcept = default;
+
+ template<std::integral T> constexpr chunk_coords_ operator+(Math::Vector2<T> off) const noexcept {
+ return { int16_t(x + int{off.x()}), int16_t(y + int{off.y()}), z };
+ }
+
+ template<std::integral T> constexpr chunk_coords_ operator-(Math::Vector2<T> off) const noexcept {
+ return { int16_t(x - int{off.x()}), int16_t(y - int{off.y()}), z };
+ }
+
+ template<std::integral T> constexpr chunk_coords_ operator+(Math::Vector3<T> off) const noexcept {
+ return { int16_t(x + int{off.x()}), int16_t(y + int{off.y()}), int8_t(z + int{off.z()}) };
+ }
+
+ template<std::integral T> constexpr chunk_coords_ operator-(Math::Vector3<T> off) const noexcept {
+ return { int16_t(x - int{off.x()}), int16_t(y - int{off.y()}), int8_t(z - int{off.z()}) };
+ }
+
+ template<std::integral T> constexpr chunk_coords_& operator+=(Math::Vector2<T> off) noexcept {
+ x = int16_t(x + int{off.x()}); y = int16_t(y + int{off.y()}); z = int8_t(z + off.z()); return *this;
+ }
+
+ template<std::integral T> constexpr chunk_coords_& operator-=(Math::Vector2<T> off) noexcept {
+ x = int16_t(x - int{off.x()}); y = int16_t(y - int{off.y()}); z = int8_t(z - off.z()); return *this;
+ }
};
constexpr inline int8_t chunk_z_min = -1, chunk_z_max = 14;
diff --git a/src/world.cpp b/src/world.cpp
index 9c3f8276..6749210a 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -199,4 +199,15 @@ void world::throw_on_wrong_entity_type(object_id id, entity_type actual, entity_
fm_throw("object '{}' has wrong entity type '{}', should be '{}'"_cf, id, (size_t)actual, (size_t)expected);
}
+auto world::neighbors(floormat::chunk_coords_ coord) -> std::array<neighbor_pair, 8>
+{
+ std::array<neighbor_pair, 8> ret;
+ for (auto i = 0uz; const auto& x : neighbor_offsets)
+ {
+ auto ch = coord + x;
+ ret[i++] = { at(ch), ch };
+ }
+ return ret;
+}
+
} // namespace floormat
diff --git a/src/world.hpp b/src/world.hpp
index c4231173..4871a62c 100644
--- a/src/world.hpp
+++ b/src/world.hpp
@@ -97,6 +97,14 @@ public:
[[nodiscard]] object_id make_id() { return ++_entity_counter; }
void set_entity_counter(object_id value);
+ struct neighbor_pair final { chunk* c; chunk_coords_ coord; };
+
+ std::array<neighbor_pair, 8> neighbors(chunk_coords_ coord);
+
+ static constexpr std::array<Vector2b, 8> neighbor_offsets = {{
+ {-1, -1}, {-1, 0}, { 0, -1}, { 1, 1}, { 1, 0}, { 0, 1}, { 1, -1}, {-1, 1},
+ }};
+
world& operator=(world&& w) noexcept;
world(world&& w) noexcept;