summaryrefslogtreecommitdiffhomepage
path: root/src/global-coords.hpp
blob: 6ae3a37c086a3e201f9b08f774582c86b970c322 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#pragma once
#include "local-coords.hpp"
#include "compat/assert.hpp"
#include <compare>
#include <concepts>
#include <Magnum/Magnum.h>
#include <Magnum/Math/Vector2.h>
#include <Magnum/Math/Vector3.h>

namespace floormat {

struct chunk_coords final {
    int16_t x = 0, y = 0;

    constexpr bool operator==(const chunk_coords& other) const noexcept = default;

    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)>>))
    explicit constexpr operator Math::Vector2<T>() const noexcept { return Math::Vector2<T>(T(x), T(y)); }

    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)>>))
    explicit constexpr operator Math::Vector3<T>() const noexcept { return Math::Vector3<T>(T(x), T(y), T(0)); }

    constexpr Vector2i operator-(chunk_coords other) const noexcept { return Vector2i{x - other.x, y - other.y }; }
};

struct chunk_coords_ final {
    int16_t x = 0, y = 0;
    int8_t z = 0;

    constexpr chunk_coords_() noexcept = default;
    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::Vector2<T> off) noexcept {
        x = int16_t(x + int{off.x()}); y = int16_t(y + int{off.y()}); 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()}); return *this;
    }

    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::Vector3<T> off) noexcept {
        x = int16_t(x + int{off.x()}); y = int16_t(y + int{off.y()}); z = int8_t(z + int{off.z()}); return *this;
    }
    template<std::integral T> constexpr chunk_coords_& operator-=(Math::Vector3<T> off) noexcept {
        x = int16_t(x - int{off.x()}); y = int16_t(y - int{off.y()}); z = int8_t(z + int{off.z()}); return *this;
    }

    constexpr Vector3i operator-(chunk_coords_ other) const noexcept { return Vector3i{x - other.x, y - other.y, z - other.z}; }

    explicit operator chunk_coords() const noexcept { return chunk_coords{x, y}; }
};

constexpr inline int8_t chunk_z_min = -1, chunk_z_max = 14;

struct global_coords final
{
    struct raw_coords final { uint32_t &x, &y; }; // NOLINT
    struct raw_coords_ final { uint32_t x, y; };

private:
    using u0 = std::integral_constant<uint32_t, (1<<15)>;
    using s0 = std::integral_constant<int32_t, int32_t(u0::value)>;
    using z0 = std::integral_constant<int32_t, (1 << 0)>;
    using z_mask = std::integral_constant<uint32_t, (1u << 4) - 1u << 20>;
    uint32_t x = u0::value<<4|z0::value<<20, y = u0::value<<4;

public:
    constexpr global_coords() noexcept = default;
    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
        },
        y{ uint32_t((c.y + s0::value) << 4) | (xy.y & 0x0f) }
    {}
    constexpr global_coords(uint32_t x, uint32_t y, std::nullptr_t) noexcept : x{x}, y{y} {}
    constexpr global_coords(uint32_t, uint32_t, uint32_t) = delete;
    constexpr global_coords(int32_t x, int32_t y, int8_t z) noexcept :
        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;
    constexpr chunk_coords chunk() const noexcept;
    constexpr operator chunk_coords_() const noexcept; // todo make invoking this take less typing
    constexpr raw_coords_ raw() const noexcept;
    constexpr raw_coords raw() noexcept;
    constexpr int8_t z() const noexcept;

    constexpr Vector2i to_signed() const noexcept;
    constexpr Vector3i to_signed3() const noexcept;
    constexpr bool operator==(const global_coords& other) const noexcept = default;

    constexpr global_coords operator+(Vector2i vec) const noexcept;
    constexpr global_coords operator-(Vector2i vec) const noexcept;
    constexpr global_coords& operator+=(Vector2i vec) noexcept;
    constexpr global_coords& operator-=(Vector2i vec) noexcept;
    constexpr Vector2i operator-(global_coords other) const noexcept;
};

struct point
{
    global_coords coord;
    Vector2b offset;

    constexpr bool operator==(const point&) const = default;
    friend constexpr std::strong_ordering operator<=>(const point& a, const point& b) noexcept;
};

constexpr local_coords global_coords::local() const noexcept
{
    return { uint8_t(x & 0x0f), uint8_t(y & 0x0f), };
}

constexpr chunk_coords global_coords::chunk() const noexcept
{
    return { int16_t(int32_t(((x & ~z_mask::value)>>4) - u0::value)), int16_t(int32_t((y>>4) - u0::value)), };
}

constexpr global_coords::operator chunk_coords_() const noexcept
{
    return chunk_coords_{ chunk(), z() };
}

constexpr auto global_coords::raw() const noexcept -> raw_coords_ { return {x, y}; }
constexpr auto global_coords::raw() noexcept -> raw_coords { return {x, y}; }

constexpr int8_t global_coords::z() const noexcept
{
    return ((x >> 20) & 0x0f) - z0::value;
}

constexpr Vector2i global_coords::to_signed() const noexcept
{
    return { int32_t((x & ~z_mask::value) - (s0::value<<4)), int32_t(y - (s0::value<<4)), };
}

constexpr Vector3i global_coords::to_signed3() const noexcept
{
    return Vector3i(to_signed(), z());
}

constexpr global_coords global_coords::operator+(Vector2i vec) const noexcept
{
    return { uint32_t((int64_t)x+vec[0]), uint32_t((int64_t)y+vec[1]), nullptr };
}

constexpr global_coords& global_coords::operator+=(Vector2i vec) noexcept
{
    x = uint32_t((int64_t)x+vec[0]);
    y = uint32_t((int64_t)y+vec[1]);
    return *this;
}

constexpr global_coords global_coords::operator-(Vector2i vec) const noexcept
{
    return { uint32_t((int64_t)x-vec[0]), uint32_t((int64_t)y-vec[1]), nullptr };
}

constexpr global_coords& global_coords::operator-=(Vector2i vec) noexcept
{
    x = uint32_t((int64_t)x-vec[0]);
    y = uint32_t((int64_t)y-vec[1]);
    return *this;
}

constexpr Vector2i global_coords::operator-(global_coords other) const noexcept
{
    return to_signed() - other.to_signed();
}

constexpr std::strong_ordering operator<=>(const point& p1, const point& p2) noexcept
{
    auto c1 = p1.coord.to_signed3(), c2 = p2.coord.to_signed3();

    if (auto val = c1.z() <=> c2.z(); val != std::strong_ordering::equal)
        return val;
    if (auto val = c1.y() <=> c2.y(); val != std::strong_ordering::equal)
        return val;
    if (auto val = c1.x() <=> c2.x(); val != std::strong_ordering::equal)
        return val;
    if (auto val = p1.offset.y() <=> p2.offset.y(); val != std::strong_ordering::equal)
        return val;
    if (auto val = p1.offset.x() <=> p2.offset.x(); val != std::strong_ordering::equal)
        return val;

    return std::strong_ordering::equal;
}

} // namespace floormat