summaryrefslogtreecommitdiffhomepage
path: root/src/global-coords.hpp
blob: 46ef15141b0042dafc1031026ecd6074789e4cb7 (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
#pragma once
#include "local-coords.hpp"
#include "compat/assert.hpp"
#include <Magnum/Magnum.h>
#include <Magnum/Math/Vector2.h>
#include <Magnum/Math/Vector3.h>

namespace floormat {

struct chunk_coords final {
    std::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)>>))
    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 chunk_coords::operator-(chunk_coords other) const noexcept
{
    return { Int{x} - other.x, Int{y} - other.y };
}

struct global_coords final {
    using u0 = std::integral_constant<std::uint32_t, (1<<15)>;
    using s0 = std::integral_constant<std::int32_t, std::int32_t(u0::value)>;
    std::uint32_t x = u0::value<<4, y = u0::value<<4;

    constexpr global_coords() noexcept = default;
    constexpr global_coords(chunk_coords c, local_coords xy) :
        x{ std::uint32_t((c.x + s0::value) << 4) | (xy.x & 0x0f) },
        y{ std::uint32_t((c.y + s0::value) << 4) | (xy.y & 0x0f) }
    {}
    constexpr global_coords(std::uint32_t x, std::uint32_t y) noexcept : x{x}, y{y} {}
    constexpr global_coords(std::int32_t x, std::int32_t y) noexcept :
          x{std::uint32_t(x + (s0::value<<4))}, y{std::uint32_t(y + (s0::value<<4))}
    {}

    constexpr local_coords local() const noexcept;
    constexpr chunk_coords chunk() 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;
};

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

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

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

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

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

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

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

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

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

} // namespace floormat