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
|
#include "app.hpp"
#include <cinttypes>
#include "compat/debug.hpp"
#include "compat/shared-ptr-wrapper.hpp"
#include "src/critter.hpp"
#include "src/world.hpp"
#include "src/wall-atlas.hpp"
#include "src/timer.hpp"
#include "src/log.hpp"
#include "loader/loader.hpp"
#include <iostream>
namespace floormat {
namespace {
constexpr auto constantly(const auto& x) noexcept {
return [x]<typename... Ts> (const Ts&...) constexpr -> const auto& { return x; };
}
template<typename F>
void run(StringView name, const F& make_dt, critter& npc, const Ns max_time,
const point start, const rotation r,
const point end, const Ns expected_time,
const uint32_t fuzz_pixels, const Ns fuzz_time)
{
constexpr uint32_t max_steps = 10'000;
const bool verbose = is_log_verbose();
fm_assert(max_time < Second*200);
auto index = npc.index();
npc.teleport_to(index, start, rotation_COUNT);
char buf[81];
Ns time{0};
uint32_t steps;
Debug{} << name << npc.position();
auto last_pos = npc.position();
uint32_t i;
bool stopped = false;
for (i = 0; i <= max_steps; i++)
{
auto dt = Ns{make_dt()};
fm_assert(dt <= Ns(1e9));
npc.update_movement(index, dt, r);
time += dt;
const auto pos = npc.position();
if (pos == last_pos)
{
stopped = true;
break;
}
last_pos = pos;
Debug{} << "-" << pos << colon(',') << time;
if (time > max_time)
{
if (verbose)
Debug{&std::cerr} << "timeout:" << max_time << "reached!";
fm_EMIT_ABORT();
}
fm_assert(i != max_steps);
}
if (stopped)
{
Debug{} << "===>" << i << "iters" << colon(',') << time;
// todo! calc distance
}
else
Debug{} << "!!! error: position doesn't converge after" << i << "iterations!";
}
/* ***** TEST 1 *****
*
* wall n 0x0 - 8:9
* wall n 0x1 - 8:0
*
* bbox-offset=0 bbox-size=32x32
*
* npc speed=1 ==> 6800 ms
* npc speed=5 ==> 1350 ms
*
* before chunk=0x0 tile=8:15 offset=-8:8
* after chunk=0x0 tile=8:9 offset=-8:-16
*
* time=6800ms
*/
critter_proto make_proto(int accel)
{
critter_proto proto;
proto.atlas = loader.anim_atlas("npc-walk", loader.ANIM_PATH);
proto.name = "Player"_s;
proto.speed = accel;
proto.playable = true;
proto.offset = {};
proto.bbox_offset = {};
proto.bbox_size = Vector2ub(tile_size_xy/2);
return proto;
}
using enum rotation;
template<typename F> void test1(const F& make_dt, int accel)
{
const auto W = wall_image_proto{ loader.wall_atlas("empty"), 0 };
auto w = world();
w[{{0,0,0}, {8,9}}].t.wall_north() = W;
w[{{0,1,0}, {8,0}}].t.wall_north() = W;
constexpr point init {{0,0,0}, {8,15}, {-8, 8}};
constexpr point end {{0,0,0}, {8, 9}, {-8,-16}};
object_id id = 0;
auto player = w.ensure_player_character(id, make_proto(accel)).ptr;
w[chunk_coords_{0,0,0}].mark_modified();
w[chunk_coords_{0,1,0}].mark_modified();
run("test1"_s, make_dt, *player, Second*20, init, N, end, Second*7, 16, Millisecond*350);
}
/* ***** TEST 2 *****
*
*/
template<typename F> void test2(F&& make_dt, int accel)
{
// TODO diagonal!
}
} // namespace
void test_app::test_critter()
{
Debug{} << "";
Debug{} << "--";
Debug{} << "";
test1(constantly(Millisecond * 100), 1);
Debug{} << "";
}
} // namespace floormat
|