summaryrefslogtreecommitdiffhomepage
path: root/test/path-search.cpp
blob: e54474cf1242ebe45866b782e17d49a3fa08fc20 (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
#include "app.hpp"
#include "compat/assert.hpp"
#include "compat/function2.hpp"
#include "loader/loader.hpp"
#include "src/world.hpp"
#include "src/scenery.hpp"
#include "src/path-search.hpp"

namespace floormat {

namespace {

void test_bbox()
{
    static constexpr auto is_passable_1 = [](chunk& c, path_search::bbox bb) {
        return path_search::is_passable_1(c, bb.min, bb.max, (object_id)-1);
    };

    static constexpr auto is_passable = [](world& w, chunk_coords_ ch, path_search::bbox bb) {
        return path_search::is_passable(w, ch, bb.min, bb.max, (object_id)-1);
    };

    static constexpr auto bbox = [](Vector2i coord, rotation r) {
        return path_search::make_neighbor_tile_bbox(coord, {}, {1,1}, r);
    };

    constexpr auto neighbor_tiles = [](world& w, chunk_coords_ ch, Vector2i pos) {
        return path_search::get_walkable_neighbor_tiles(w, {ch, pos}, {}, (object_id)-1);
    };

    const auto metal2 = loader.tile_atlas("metal2", {2, 2}, pass_mode::blocked);
    const auto table  = loader.scenery("table1");

    {
        constexpr auto coord1 = chunk_coords_{10, 11, 0},
                       coord2 = chunk_coords_{10, 12, 0};
        constexpr auto _15 = TILE_MAX_DIM-1;
        using enum rotation;
        {
            auto w = world();
            [[maybe_unused]] auto& c12 = w[coord2];
            [[maybe_unused]] auto& c11 = w[coord1];
            c12[{0, 0}].wall_north() = {metal2, 0};

            fm_assert( !is_passable_1(c12, bbox({}, N)) );
            fm_assert(  is_passable_1(c12, bbox({}, E)) );
            fm_assert(  is_passable_1(c12, bbox({}, S)) );
            fm_assert(  is_passable_1(c12, bbox({}, W)) );

            fm_assert(  is_passable(w, coord1, bbox({0, _15}, N)) );
            fm_assert(  is_passable(w, coord1, bbox({0, _15}, E)) );
            fm_assert( !is_passable(w, coord1, bbox({0, _15}, S)) );
            fm_assert(  is_passable(w, coord1, bbox({0, _15}, W)) );
        }
    }
    {
        using enum rotation;
        constexpr auto C = rotation_COUNT;
        constexpr auto ch = chunk_coords_{21, 22, 0};

        auto w = world();
        auto& c = w[ch];

        c[{8, 7}].wall_north() = {metal2,0};
        c[{8, 9}].wall_north() = {metal2,0};
        fm_assert(  is_passable_1(c, bbox({8, 6}, N)) );
        fm_assert( !is_passable_1(c, bbox({8, 6}, S)) );
        fm_assert( !is_passable_1(c, bbox({8, 7}, N)) );

        fm_assert(  is_passable_1(c, bbox({8, 8}, N)) );
        fm_assert(  is_passable_1(c, bbox({8, 8}, E)) );
        fm_assert( !is_passable_1(c, bbox({8, 8}, S)) );
        fm_assert(  is_passable_1(c, bbox({8, 8}, W)) );

        fm_assert(neighbor_tiles(w, ch, {8, 8}).size == 3);

        c[{8, 8}].wall_north() = {metal2,0};
        c.mark_passability_modified();
        fm_assert(  is_passable_1(c, bbox({8, 8}, C)) );
        fm_assert( !is_passable_1(c, bbox({8, 7}, S)) );

        fm_assert( !is_passable_1(c, bbox({8, 8}, N)) );
        fm_assert(  is_passable_1(c, bbox({8, 8}, E)) );
        fm_assert( !is_passable_1(c, bbox({8, 8}, S)) );
        fm_assert(  is_passable_1(c, bbox({8, 8}, W)) );

        fm_assert(neighbor_tiles(w, ch, {8, 8}).size == 2);
    }
    {
        using enum rotation;
        constexpr auto ch = chunk_coords_{0, 0, 0};
        auto w = world();
        auto& c = test_app::make_test_chunk(w, ch);

        constexpr auto is_passable_NESW = [](chunk& c, Vector2i coord, std::array<bool, 4> dirs)
        {
            fm_assert(is_passable_1(c, bbox(coord, N)) == dirs[0]);
            fm_assert(is_passable_1(c, bbox(coord, E)) == dirs[1]);
            fm_assert(is_passable_1(c, bbox(coord, S)) == dirs[2]);
            fm_assert(is_passable_1(c, bbox(coord, W)) == dirs[3]);
        };

        is_passable_NESW(c, {8, 8}, { false, false, false, false });
        is_passable_NESW(c, {8, 9}, { false, true,  true,  true  });
        is_passable_NESW(c, {2, 4}, { true,  false, true,  true  });
        is_passable_NESW(c, {4, 4}, { true,  true,  true,  false });

        fm_assert(neighbor_tiles(w, ch, {8, 8}).size == 0);
        fm_assert(neighbor_tiles(w, ch, {8, 9}).size == 3);
        fm_assert(neighbor_tiles(w, ch, {2, 4}).size == 3);
        fm_assert(neighbor_tiles(w, ch, {4, 4}).size == 3);
    }
    {
        constexpr auto ch = chunk_coords_{};
        auto w = world();
        auto& c = w[ch];
        constexpr size_t K = 8;
        const auto wall = tile_image_proto{metal2, 0};
        c[{0,   0  }].wall_north() = wall;
        c[{0,   0  }].wall_west() = wall;
        c[{K,   K  }].wall_north() = { metal2, 0 };
        c[{K,   K  }].wall_west()  = { metal2, 0 };
        c[{K,   K+1}].wall_north() = { metal2, 0 };
        c[{K+1, K  }].wall_west()  = { metal2, 0 };

        path_search search;
        search.ensure_allocated({}, {});
        search.fill_cache_(w, {0, 0, 0}, {}, {});

        static constexpr auto c_idx = [](path_search& search, chunk_coords ch) {
            auto ch_ = Vector2i(ch) - search.cache.start;
            fm_assert(ch_ >= Vector2i{} && ch_ < search.cache.size);
            return ch_.y()*search.cache.size.x() + ch_.x();
        };
        static constexpr auto t_idx = [](local_coords tile) constexpr {
            return (size_t)tile.y * TILE_MAX_DIM + (size_t)tile.x;
        };
        constexpr auto check_N = [&](path_search& search, chunk_coords ch, local_coords tile) {
            return search.cache.array[c_idx(search, ch)].can_go_north[t_idx(tile)];
        };
        constexpr auto check_W = [&](path_search& search, chunk_coords ch, local_coords tile) {
            return search.cache.array[c_idx(search, ch)].can_go_west[t_idx(tile)];
        };

        fm_assert( !check_N(search, {}, {  0,   0} ));
        fm_assert( !check_W(search, {}, {  0,   0} ));
        fm_assert(  check_N(search, {}, {  0,   1} ));
        fm_assert(  check_W(search, {}, {  1,   0} ));

        fm_assert(  check_W(search, {}, {K-1, K  } ));
        fm_assert(  check_N(search, {}, {K-1, K  } ));
        fm_assert(  check_W(search, {}, {K,   K-1} ));
        fm_assert(  check_N(search, {}, {K,   K-1} ));

        fm_assert( !check_N(search, {}, {K,   K  } ));
        fm_assert( !check_W(search, {}, {K,   K  } ));
        fm_assert( !check_W(search, {}, {K+1, K  } ));
        fm_assert(  check_N(search, {}, {K+1, K  } ));
        fm_assert( !check_N(search, {}, {K,   K+1} ));
        fm_assert(  check_W(search, {}, {K,   K+1} ));

        fm_assert(  check_N(search, {}, {K+2, K  } ));
        fm_assert(  check_W(search, {}, {K+2, K  } ));
        fm_assert(  check_N(search, {}, {K,   K+2} ));
        fm_assert(  check_W(search, {}, {K,   K+2} ));
    }
}

} // namespace

void test_app::test_path_search()
{
    test_bbox();
}

} // namespace floormat