summaryrefslogtreecommitdiffhomepage
path: root/editor/tests/pathfinding.cpp
blob: 82009fdcbf464d65fdd5eba3e519f6145aa7afa9 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "../tests-private.hpp"
#include "compat/limits.hpp"
#include "compat/shared-ptr-wrapper.hpp"
#include "editor/app.hpp"
#include "src/world.hpp"
#include "src/critter.hpp"
#include "src/point.inl"
#include "src/anim-atlas.hpp"
#include "src/nanosecond.hpp"
#include "floormat/main.hpp"
#include "../imgui-raii.hpp"
#include <mg/Functions.h>

namespace floormat::tests {

using namespace floormat::imgui;

namespace {

struct step_s
{
    uint32_t count;
    Vector2b direction;
};

constexpr step_s next_stepʹ(Vector2i vec_in)
{
    const auto vec = Vector2ui(Math::abs(vec_in));
    const auto signs = Vector2b(Math::sign(vec_in));

    if (vec.x() == vec.y())
        return { vec.x(), Vector2b{1, 1} * signs };
    else if (vec.y() == 0)
        return { vec.x(), Vector2b{1, 0} * signs };
    else if (vec.x() == 0)
        return { vec.y(), Vector2b{0, 1} * signs };
    else
    {
        uint32_t major_idx, minor_idx;
        if (vec.x() > vec.y())
        {
            major_idx = 0;
            minor_idx = 1;
        }
        else
        {
            major_idx = 1;
            minor_idx = 0;
        }
        const auto major = vec[major_idx], minor = vec[minor_idx];
        const auto num_axis_aligned = (uint32_t)Math::abs((int)major - (int)minor);
        auto axis_aligned = Vector2b{};
        axis_aligned[major_idx] = 1;
        return { num_axis_aligned, axis_aligned * signs };
    }
}

struct pending_s
{
    point dest;
    bool has_value : 1 = false;
};

struct pf_test final : base_test
{
    pending_s current;

    ~pf_test() noexcept override = default;

    bool handle_key(app& a, const key_event& e, bool is_down) override;
    bool handle_mouse_click(app& a, const mouse_button_event& e, bool is_down) override;
    bool handle_mouse_move(app& a, const mouse_move_event& e) override;
    void draw_overlay(app& a) override;
    void draw_ui(app& a, float menu_bar_height) override;
    void update_pre(app& a, const Ns& dt) override;
    void update_post(app& a, const Ns& dt) override;
};

constexpr step_s next_step(point from, point to)
{
    fm_debug_assert(from.chunk3().z == to.chunk3().z);
    const auto vec = to - from;
    fm_debug_assert(!vec.isZero());
    return next_stepʹ(vec);
}

#if 0
constexpr rotation dir_from_step(step_s step)
{
    if (!step) [[unlikely]]
        return rotation_COUNT;

    auto x = step.direction.x() + 1;
    auto y = step.direction.y() + 1;
    fm_debug_assert((x & 3) == x && (y & 3) == y);
    auto val = x << 2 | y;

    switch (val)
    {
    using enum rotation;
    case 0 << 2 | 0: /* -1 -1 */ return NW;
    case 0 << 2 | 1: /* -1  0 */ return W;
    case 0 << 2 | 2: /* -1  1 */ return SW;
    case 1 << 2 | 0: /*  0 -1 */ return N;
    case 1 << 2 | 1: /*  0  0 */ return rotation_COUNT;
    case 1 << 2 | 2: /*  0  1 */ return S;
    case 2 << 2 | 0: /*  1 -1 */ return NE;
    case 2 << 2 | 1: /*  1  0 */ return E;
    case 2 << 2 | 2: /*  1  1 */ return SE;
    default: return rotation_COUNT;
    }
}
#endif

#if 0
constexpr std::array<bool, 4> arrows_from_step(step_s step)
{
    auto x = step.direction.x() + 1;
    auto y = step.direction.y() + 1;
    fm_debug_assert((x & 3) == x && (y & 3) == y);
    auto val = (unsigned)x << 2 | (unsigned)y;

    switch (val)
    {
    using enum rotation;
    case 0 << 2 | 0: /* -1 -1 */ return { 1, 0, 1, 0 }; //NOLINT(*-use-bool-literals)
    case 0 << 2 | 1: /* -1  0 */ return { 1, 0, 0, 0 }; //NOLINT(*-use-bool-literals)
    case 0 << 2 | 2: /* -1  1 */ return { 1, 0, 0, 1 }; //NOLINT(*-use-bool-literals)
    case 1 << 2 | 0: /*  0 -1 */ return { 0, 0, 1, 0 }; //NOLINT(*-use-bool-literals)
    case 1 << 2 | 1: /*  0  0 */ return {};
    case 1 << 2 | 2: /*  0  1 */ return { 0, 0, 0, 1 }; //NOLINT(*-use-bool-literals)
    case 2 << 2 | 0: /*  1 -1 */ return { 0, 1, 1, 0 }; //NOLINT(*-use-bool-literals)
    case 2 << 2 | 1: /*  1  0 */ return { 0, 1, 0, 0 }; //NOLINT(*-use-bool-literals)
    case 2 << 2 | 2: /*  1  1 */ return { 0, 1, 0, 1 }; //NOLINT(*-use-bool-literals)
    default: return {};
    }
}
#if 0
static_assert(arrows_from_step({0, { 1, -1}}) == std::array<bool, 4>{0, 1, 1, 0});
static_assert(arrows_from_step({0, {-1,  1}}) == std::array<bool, 4>{1, 0, 0, 1});
static_assert(arrows_from_step({0, { 0, -1}}) == std::array<bool, 4>{0, 0, 1, 0});
static_assert(arrows_from_step({0, { 1,  0}}) == std::array<bool, 4>{0, 1, 0, 0});
#endif
#endif

constexpr float step_magnitude(Vector2b vec)
{
    constexpr double= critter::move_speed * critter::frame_time;
    constexpr double=/ Vector2d{1,  1}.length();
    constexpr auto c = (float), d = (float);

    if (vec.x() * vec.y() != 0)
        // diagonal
        return c;
    else
        // axis-aligned
        return d;
}

bool pf_test::handle_key(app& a, const key_event& e, bool is_down)
{
    (void) a; (void)e; (void)is_down;
    return false;
}

bool pf_test::handle_mouse_click(app& a, const mouse_button_event& e, bool is_down)
{
    if (e.button == mouse_button_left && is_down)
    {
        if (auto ptʹ = a.cursor_state().point())
        {
            current = {
                .dest = *ptʹ,
                .has_value = true,
            };
            return true;
        }
    }
    else if (e.button == mouse_button_right && is_down)
        current = {};
    return false;
}

bool pf_test::handle_mouse_move(floormat::app &a, const mouse_move_event& e)
{
    if (e.buttons & mouse_button_left)
        return handle_mouse_click(a, {e.position, e.mods, mouse_button_left, 1}, true);
    else
        return false;
}

void pf_test::draw_overlay(app& a)
{
    (void)a;
}

void pf_test::draw_ui(app& a, float)
{
    (void)a;
}

void pf_test::update_pre(app& a, const Ns& dt)
{
    if (!current.has_value)
        return;

    auto& m = a.main();
    auto& C = *a.ensure_player_character(m.world()).ptr;
    fm_assert(C.is_dynamic());

    const auto hz = C.atlas->info().fps;
    const auto nframes = C.alloc_frame_time(dt, C.delta, hz, C.speed);

    if (nframes == 0)
        return;

    C.set_keys(false, false, false, false);

    auto index = C.index();
    bool ok = true;

    for (uint32_t i = 0; i < nframes; i++)
    {
        C.chunk().ensure_passability();

        const auto from = C.position();
        if (from == current.dest)
        {
            current.has_value = false;
            Debug{} << "done!" << from;
            return;
        }
        const auto step = next_step(from, current.dest);
        Debug{} << "step" << step.direction << step.count;
        C.set_keys_auto();
        if (step.direction == Vector2b{})
        {
            ok = false;
            break;
        }
        fm_assert(step.count > 0);
        using Frac = decltype(critter::offset_frac)::Type;
        constexpr auto inv_frac = 1.f / (float)unsigned{limits<Frac>::max};
        const auto vec = Vector2(step.direction) * step_magnitude(step.direction);
        const auto sign_vec = Math::sign(vec);
        const auto frac = Vector2(C.offset_frac) * sign_vec * inv_frac;
        auto offset_ = vec + frac;
        auto off_i = Vector2i(offset_);
        if (!off_i.isZero())
        {
            if (C.can_move_to(off_i))
                C.move_to(index, off_i, C.r);
            else
            {
                ok = false;
                break;
            }
        }
        else
            C.offset_frac = Vector2us(Math::min({1.f,1.f}, Math::abs(offset_)) * frac);
    }

    if (!ok) [[unlikely]]
    {
        C.delta = {};
        C.offset_frac = {};
    }
}

void pf_test::update_post(app&, const Ns&)
{
}

} // namespace

Pointer<base_test> tests_data::make_test_pathfinding() { return Pointer<pf_test>{InPlaceInit}; }

} // namespace floormat::tests