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
|
#include "chunk.hpp"
#include "object.hpp"
#include "tile-iterator.hpp"
#include <algorithm>
#include <Magnum/GL/Context.h>
namespace floormat {
namespace {
constexpr auto object_id_lessp = [](const auto& a, const auto& b) { return a->id < b->id; };
size_t _reload_no_ = 0; // NOLINT
bool is_log_quiet()
{
using GLCCF = GL::Implementation::ContextConfigurationFlag;
auto flags = GL::Context::current().configurationFlags();
return !!(flags & GLCCF::QuietLog);
}
} // namespace
bool chunk::empty(bool force) const noexcept
{
if (!force && !_maybe_empty)
return false;
for (auto i = 0uz; i < TILE_COUNT; i++)
if (!_objects.empty() || _ground && _ground->_ground_atlases[i] || _walls && (_walls->_wall_atlases[i*2+0] || _walls->_wall_atlases[i*2+1]))
return _maybe_empty = false;
if (!_objects.empty())
return false;
return true;
}
tile_atlas* chunk::ground_atlas_at(size_t i) const noexcept { return _ground ? _ground->_ground_atlases[i].get() : nullptr; }
tile_atlas* chunk::wall_atlas_at(size_t i) const noexcept { return _walls ? _walls->_wall_atlases[i].get() : nullptr; }
tile_ref chunk::operator[](size_t idx) noexcept { return { *this, uint8_t(idx) }; }
tile_proto chunk::operator[](size_t idx) const noexcept { return tile_proto(tile_ref { *const_cast<chunk*>(this), uint8_t(idx) }); }
tile_ref chunk::operator[](local_coords xy) noexcept { return operator[](xy.to_index()); }
tile_proto chunk::operator[](local_coords xy) const noexcept { return operator[](xy.to_index()); }
auto chunk::begin() noexcept -> iterator { return iterator { *this, 0 }; }
auto chunk::end() noexcept -> iterator { return iterator { *this, TILE_COUNT }; }
auto chunk::cbegin() const noexcept -> const_iterator { return const_iterator { *this, 0 }; }
auto chunk::cend() const noexcept -> const_iterator { return const_iterator { *this, TILE_COUNT }; }
auto chunk::begin() const noexcept -> const_iterator { return cbegin(); }
auto chunk::end() const noexcept -> const_iterator { return cend(); }
void chunk::mark_ground_modified() noexcept
{
if (!_ground_modified && !is_log_quiet())
fm_debug("ground reload %zu", ++_reload_no_);
_ground_modified = true;
mark_passability_modified();
}
// todo this can use _replace_bbox too
void chunk::mark_walls_modified() noexcept
{
if (!_walls_modified && !is_log_quiet())
fm_debug("wall reload %zu", ++_reload_no_);
_walls_modified = true;
mark_passability_modified();
}
void chunk::mark_scenery_modified() noexcept
{
if (!_scenery_modified && !is_log_quiet())
fm_debug("scenery reload %zu", ++_reload_no_);
_scenery_modified = true;
}
void chunk::mark_passability_modified() noexcept
{
if (!_pass_modified && !is_log_quiet())
fm_debug("pass reload %zu", ++_reload_no_);
_pass_modified = true;
}
bool chunk::is_passability_modified() const noexcept { return _pass_modified; }
bool chunk::is_scenery_modified() const noexcept { return _scenery_modified; }
void chunk::mark_modified() noexcept
{
mark_ground_modified();
mark_walls_modified();
mark_scenery_modified();
mark_passability_modified();
}
chunk::chunk(struct world& w, chunk_coords_ ch) noexcept : _world{&w}, _coord{ch}
{
}
chunk::~chunk() noexcept
{
_teardown = true;
_objects.clear();
_rtree.RemoveAll();
}
chunk::chunk(chunk&&) noexcept = default;
chunk& chunk::operator=(chunk&&) noexcept = default;
bool chunk::bbox::operator==(const bbox& other) const noexcept = default;
void chunk::add_object_unsorted(const std::shared_ptr<object>& e)
{
_objects_sorted = false;
if (!e->is_dynamic())
mark_scenery_modified();
if (bbox bb; _bbox_for_scenery(*e, bb))
_add_bbox(bb);
_objects.push_back(e);
}
void chunk::sort_objects()
{
if (_objects_sorted)
return;
_objects_sorted = true;
mark_scenery_modified();
std::sort(_objects.begin(), _objects.end(), [](const auto& a, const auto& b) {
return a->id < b->id;
});
}
void chunk::add_object(const std::shared_ptr<object>& e)
{
fm_assert(_objects_sorted);
if (!e->is_dynamic())
mark_scenery_modified();
if (bbox bb; _bbox_for_scenery(*e, bb))
_add_bbox(bb);
auto& es = _objects;
es.reserve(es.size() + 1);
auto it = std::lower_bound(es.cbegin(), es.cend(), e, object_id_lessp);
_objects.insert(it, e);
}
void chunk::remove_object(size_t i)
{
fm_assert(_objects_sorted);
auto& es = _objects;
fm_debug_assert(i < es.size());
const auto& e = es[i];
if (!e->is_dynamic())
mark_scenery_modified();
if (bbox bb; _bbox_for_scenery(*e, bb))
_remove_bbox(bb);
es.erase(es.cbegin() + ptrdiff_t(i));
}
const std::vector<std::shared_ptr<object>>& chunk::objects() const
{
fm_assert(_objects_sorted);
return _objects;
}
} // namespace floormat
|