summaryrefslogtreecommitdiffhomepage
path: root/src/chunk-collision.cpp
blob: 5b85de1e966c8349d8859acb57669a59b2500658 (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
#include "chunk.hpp"
#include "RTree.hpp"
#include "tile-atlas.hpp"
#include <bit>
#include <Corrade/Containers/PairStl.h>

namespace floormat {

const chunk::RTree* chunk::rtree() const noexcept { return &_rtree; }
chunk::RTree* chunk::rtree() noexcept { return &_rtree; }

namespace {

constexpr Vector2 tile_start(std::size_t k)
{
    constexpr auto half_tile = Vector2(TILE_SIZE2)/2;
    const local_coords coord{k};
    return TILE_SIZE2 * Vector2(coord) - half_tile;
}

constexpr Pair<Vector2, Vector2> scenery_tile(std::size_t k, const scenery& sc)
{
    const local_coords coord{k};
    auto center = TILE_SIZE2 * Vector2(coord) + Vector2(sc.offset) + Vector2(sc.bbox_offset);
    auto start = center - Vector2(sc.bbox_size);
    auto size = Vector2(sc.bbox_size)*2;
    return { start, start + size, };
};

constexpr Pair<Vector2, Vector2> whole_tile(std::size_t k)
{
    auto start = tile_start(k);
    return { start, start + TILE_SIZE2, };
};

constexpr Pair<Vector2, Vector2> wall_north(std::size_t k)
{
    auto start = tile_start(k) - Vector2(0, 1);
    return { start, start + Vector2(TILE_SIZE2[0], 2), };
};

constexpr Pair<Vector2, Vector2> wall_west(std::size_t k)
{
    auto start = tile_start(k) - Vector2(1, 0);
    return { start, start + Vector2(2, TILE_SIZE2[1]), };
};

constexpr std::uint64_t make_id(collision_type type, std::uint64_t id)
{
    return std::bit_cast<std::uint64_t>(collision_data { (std::uint64_t)type, id });
}

} // namespace

void chunk::ensure_passability() noexcept
{
    if (!_pass_modified)
        return;
    _pass_modified = false;

    _rtree.RemoveAll();

    for (auto i = 0_uz; i < TILE_COUNT; i++)
    {
        auto tile = operator[](i);
        if (auto s = tile.scenery())
            if (s.frame.passability != pass_mode::pass && Vector2ui(s.frame.bbox_size).product() > 0)
            {
                auto [start, end] = scenery_tile(i, s.frame);
                auto id = make_id(collision_type::scenery, i);
                _rtree.Insert(start.data(), end.data(), id);
            }
    }
    for (auto i = 0_uz; i < TILE_COUNT; i++)
    {
        if (const auto* atlas = ground_atlas_at(i))
            if (atlas->pass_mode(pass_mode::pass) != pass_mode::pass)
            {
                auto [start, end] = whole_tile(i);
                auto id = make_id(collision_type::geometry, i);
                _rtree.Insert(start.data(), end.data(), id);
            }
    }
    for (auto i = 0_uz; i < TILE_COUNT; i++)
    {
        auto tile = operator[](i);
        if (const auto* atlas = tile.wall_north_atlas().get())
            if (atlas->pass_mode(pass_mode::blocked) != pass_mode::pass)
            {
                auto [start, end] = wall_north(i);
                auto id = make_id(collision_type::geometry, i);
                _rtree.Insert(start.data(), end.data(), id);
            }
        if (const auto* atlas = tile.wall_west_atlas().get())
            if (atlas->pass_mode(pass_mode::blocked) != pass_mode::pass)
            {
                auto [start, end] = wall_west(i);
                auto id = make_id(collision_type::geometry, i);
                _rtree.Insert(start.data(), end.data(), id);
            }
    }
}

} // namespace floormat