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
|
#include "chunk-region.hpp"
#include "search-bbox.hpp"
#include "world.hpp"
#include "collision.hpp"
#include "object.hpp"
#include "compat/function2.hpp"
#include <bit>
#include <array>
#include <Corrade/Containers/GrowableArray.h>
#include <Magnum/Math/Functions.h>
namespace floormat {
namespace {
using Search::bbox;
using Search::div_factor;
using Search::div_size;
using Search::div_count;
using Search::pred;
static_assert(div_count.x() == div_count.y());
static_assert((iTILE_SIZE2 % div_size).isZero());
constexpr auto chunk_bits = div_count.product(),
visited_bits = div_count.product()*4*4;
constexpr auto div_min = -iTILE_SIZE2/2 + div_size/2;
constexpr bbox<Int> bbox_from_pos1(Vector2i center)
{
constexpr auto half = div_size/2;
auto start = center - half;
return { start, start + div_size };
}
constexpr bbox<Int> bbox_from_pos2(Vector2i pt, Vector2i from)
{
auto bb0 = bbox_from_pos1(from);
auto bb = bbox_from_pos1(pt);
auto min = Math::min(bb0.min, bb.min);
auto max = Math::max(bb0.max, bb.max);
return { min, max };
}
constexpr bbox<Int> make_pos(Vector2i ij, Vector2i from)
{
auto pos = div_min + div_size * ij;
auto pos0 = pos + from*div_size;
return bbox_from_pos2(pos, pos0);
}
bool check_pos(chunk& c, const std::array<chunk*, 8>& nbs, Vector2i ij, Vector2i from, const pred& p)
{
auto pos = make_pos(ij, from);
bool ret = path_search::is_passable_(&c, nbs, Vector2(pos.min), Vector2(pos.max), 0, p);
//if (ret) Debug{} << "check" << ij << pos.min << pos.max << ret;
//Debug{} << "check" << ij << pos.min << pos.max << ret;
return ret;
}
struct node_s
{
Vector2i pos;
};
struct tmp_s
{
Array<node_s> stack;
std::bitset<visited_bits> visited;
static uint32_t get_index(Vector2i pos);
void append(std::bitset<chunk_bits>& passable, Vector2i pos);
[[nodiscard]] bool check_visited(std::bitset<chunk_bits>& passable, Vector2i pos, int from);
};
uint32_t tmp_s::get_index(Vector2i pos)
{
return (uint32_t)pos.y() * (uint32_t)div_count.x() + (uint32_t)pos.x();
}
void tmp_s::append(std::bitset<chunk_bits>& passable, Vector2i pos)
{
auto i = get_index(pos);
passable[i] = true;
arrayAppend(stack, {pos});
}
bool tmp_s::check_visited(std::bitset<chunk_bits>& passable, Vector2i pos, int from)
{
auto i = get_index(pos);
//fm_debug_assert(i < passable.size());
if (passable[i])
return {};
auto v = i*4 + (uint32_t)from;
//fm_debug_assert(v < visited.size());
if (visited[v])
return {};
visited[v] = true;
return true;
}
tmp_s& get_tmp()
{
static Pointer<tmp_s> tmp = [] {
Pointer<tmp_s> p{InPlace};
arrayReserve(p->stack, 4*div_count.product());
return p;
}();
arrayResize(tmp->stack, 0);
tmp->visited = {};
return *tmp;
}
auto default_region_predicate(chunk& c) noexcept
{
return [&c](collision_data data) {
auto x = std::bit_cast<collision_data>(data);
// XXX 'scenery' is used for all object types
if (x.tag == (uint64_t)collision_type::scenery)
{
auto& w = c.world();
auto obj = w.find_object(x.data);
if (obj->type() == object_type::critter)
return path_search_continue::pass;
}
return path_search_continue::blocked;
};
}
} // namespace
void chunk::delete_pass_region(pass_region*& ptr)
{
if (ptr)
{
delete ptr;
ptr = nullptr;
}
}
auto chunk::get_pass_region() -> const pass_region*
{
if (!_region_modified)
{
fm_debug_assert(_region != nullptr);
return _region;
}
_region_modified = false;
if (!_region)
_region = new pass_region;
else
_region->bits = {};
make_pass_region(*_region);
return _region;
}
bool chunk::is_region_modified() const noexcept { return _region_modified; }
void chunk::mark_region_modified() noexcept { _region_modified = true; }
void chunk::make_pass_region(pass_region& ret)
{
return make_pass_region(ret, default_region_predicate(*this));
}
void chunk::make_pass_region(pass_region& ret, const pred& f)
{
ret = {};
auto& tmp = get_tmp();
const auto nbs = _world->neighbors(_coord);
constexpr Vector2i fours[4] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0} };
//if (Vector2i pos{0, 0}; check_pos(*c, nbs, pos, fours[1])) tmp.append(pos, 1); // top
constexpr auto get_positions = [](int i) {
constexpr auto last = div_count - Vector2i{1};
return std::array<Vector2i, 4> {{
{i, last.y()}, // bottom
{i, 0}, // top
{last.x(), i}, // right
{0, i}, // left
}};
};
for (int i = 0; i < div_count.x(); i++)
{
auto positions = get_positions(i);
for (auto i = 0u; i < 4; i++)
if (check_pos(*this, nbs, positions[i], fours[i], f))
tmp.append(ret.bits, positions[i]);
}
while (!tmp.stack.isEmpty())
{
auto p = tmp.stack.back().pos;
arrayRemoveSuffix(tmp.stack);
for (int i = 0; i < 4; i++)
{
Vector2i from = fours[i], pos{p - from};
if ((uint32_t)pos.x() >= div_count.x() || (uint32_t)pos.y() >= div_count.y()) [[unlikely]]
continue;
if (tmp.check_visited(ret.bits, pos, i) && check_pos(*this, nbs, pos, from, f))
tmp.append(ret.bits, pos);
}
}
}
} // namespace floormat
|