1 #include "BlockLookup.hpp"
3 #include "ChunkIndex.hpp"
4 #include "ChunkLoader.hpp"
5 #include "ChunkRenderer.hpp"
6 #include "ChunkStore.hpp"
8 #include "Generator.hpp"
9 #include "WorldCollision.hpp"
10 #include "../app/Assets.hpp"
11 #include "../geometry/distance.hpp"
12 #include "../graphics/BlockLighting.hpp"
13 #include "../graphics/BlockMesh.hpp"
14 #include "../graphics/Viewport.hpp"
15 #include "../io/WorldSave.hpp"
25 constexpr int Chunk::side;
26 constexpr int Chunk::size;
29 Chunk::Chunk(const BlockTypeRegistry &types) noexcept
43 Chunk::Chunk(Chunk &&other) noexcept
45 , generated(other.generated)
46 , lighted(other.lighted)
47 , position(other.position)
48 , ref_count(other.ref_count)
49 , dirty_mesh(other.dirty_mesh)
50 , dirty_save(other.dirty_save) {
51 std::copy(other.neighbor, other.neighbor + sizeof(neighbor), neighbor);
52 std::copy(other.blocks, other.blocks + sizeof(blocks), blocks);
53 std::copy(other.light, other.light + sizeof(light), light);
57 Chunk &Chunk::operator =(Chunk &&other) noexcept {
59 std::copy(other.neighbor, other.neighbor + sizeof(neighbor), neighbor);
60 std::copy(other.blocks, other.blocks + sizeof(blocks), blocks);
61 std::copy(other.light, other.light + sizeof(light), light);
62 generated = other.generated;
63 lighted = other.lighted;
64 position = other.position;
65 std::swap(ref_count, other.ref_count);
66 dirty_mesh = other.dirty_save;
67 dirty_save = other.dirty_save;
77 RoughLocation::Fine pos;
79 SetNode(Chunk *chunk, RoughLocation::Fine pos)
80 : chunk(chunk), pos(pos) { }
82 int Get() const noexcept { return chunk->GetLight(pos); }
83 void Set(int level) noexcept { chunk->SetLight(pos, level); }
85 const BlockType &GetType() const noexcept { return chunk->Type(Chunk::ToIndex(pos)); }
87 bool HasNext(Block::Face face) noexcept {
88 const BlockType &type = GetType();
89 if (type.block_light && !type.luminosity) return false;
90 const BlockLookup next(chunk, pos, face);
93 SetNode GetNext(Block::Face face) noexcept {
94 const BlockLookup next(chunk, pos, face);
95 return SetNode(&next.GetChunk(), next.GetBlockPos());
105 UnsetNode(Chunk *chunk, RoughLocation::Fine pos)
106 : SetNode(chunk, pos), level(Get()) { }
108 UnsetNode(const SetNode &set)
109 : SetNode(set), level(Get()) { }
112 bool HasNext(Block::Face face) noexcept {
113 const BlockLookup next(chunk, pos, face);
116 UnsetNode GetNext(Block::Face face) noexcept { return UnsetNode(SetNode::GetNext(face)); }
120 std::queue<SetNode> light_queue;
121 std::queue<UnsetNode> dark_queue;
123 void work_light() noexcept {
124 while (!light_queue.empty()) {
125 SetNode node = light_queue.front();
128 int level = node.Get() - 1;
129 for (int face = 0; face < Block::FACE_COUNT; ++face) {
130 if (node.HasNext(Block::Face(face))) {
131 SetNode other = node.GetNext(Block::Face(face));
132 if (other.Get() < level) {
134 light_queue.emplace(other);
141 void work_dark() noexcept {
142 while (!dark_queue.empty()) {
143 UnsetNode node = dark_queue.front();
146 for (int face = 0; face < Block::FACE_COUNT; ++face) {
147 if (node.HasNext(Block::Face(face))) {
148 UnsetNode other = node.GetNext(Block::Face(face));
149 // TODO: if there a light source here with the same level this will err
150 if (other.Get() != 0 && other.Get() < node.level) {
152 dark_queue.emplace(other);
154 light_queue.emplace(other);
163 void Chunk::SetBlock(int index, const Block &block) noexcept {
164 const BlockType &old_type = Type(blocks[index]);
165 const BlockType &new_type = Type(block);
167 blocks[index] = block;
170 if (!lighted || &old_type == &new_type) return;
172 if (new_type.luminosity > old_type.luminosity) {
174 SetLight(index, new_type.luminosity);
175 light_queue.emplace(this, ToPos(index));
177 } else if (new_type.luminosity < old_type.luminosity) {
179 dark_queue.emplace(this, ToPos(index));
182 SetLight(index, new_type.luminosity);
183 light_queue.emplace(this, ToPos(index));
185 } else if (new_type.block_light && !old_type.block_light) {
187 if (GetLight(index) > 0) {
188 dark_queue.emplace(this, ToPos(index));
193 } else if (!new_type.block_light && old_type.block_light) {
196 RoughLocation::Fine pos(ToPos(index));
197 for (int face = 0; face < Block::FACE_COUNT; ++face) {
198 BlockLookup next_block(this, pos, Block::Face(face));
200 level = std::max(level, next_block.GetLight());
204 SetLight(index, level - 1);
205 light_queue.emplace(this, pos);
211 void Chunk::ScanLights() {
213 RoughLocation::Fine pos(0, 0, 0);
214 for (; pos.z < side; ++pos.z) {
215 for (pos.y = 0; pos.y < side; ++pos.y) {
216 for (pos.x = 0; pos.x < side; ++pos.x, ++idx) {
217 const BlockType &type = Type(blocks[idx]);
218 if (type.luminosity) {
219 SetLight(idx, type.luminosity);
220 light_queue.emplace(this, pos);
229 void Chunk::SetNeighbor(Block::Face face, Chunk &other) noexcept {
230 neighbor[face] = &other;
231 other.neighbor[Block::Opposite(face)] = this;
234 void Chunk::Unlink() noexcept {
235 for (int face = 0; face < Block::FACE_COUNT; ++face) {
236 if (neighbor[face]) {
237 neighbor[face]->neighbor[Block::Opposite(Block::Face(face))] = nullptr;
238 neighbor[face] = nullptr;
244 void Chunk::SetLight(int index, int level) noexcept {
245 if (light[index] != level) {
246 light[index] = level;
251 int Chunk::GetLight(int index) const noexcept {
255 float Chunk::GetVertexLight(const RoughLocation::Fine &pos, const BlockMesh::Position &vtx, const EntityMesh::Normal &norm) const noexcept {
256 int index = ToIndex(pos);
257 float light = GetLight(index);
259 Block::Face direct_face(Block::NormalFace(norm));
261 BlockLookup direct(const_cast<Chunk *>(this), pos, Block::NormalFace(norm));
263 float direct_light = direct.GetLight();
264 if (direct_light > light) {
265 light = direct_light;
271 if (Type(BlockAt(index)).luminosity > 0 || direct.GetType().block_light) {
276 switch (Block::Axis(direct_face)) {
278 edge[0] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
279 edge[1] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
282 edge[0] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
283 edge[1] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
286 edge[0] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
287 edge[1] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
294 BlockLookup next[2] = {
295 direct.Next(edge[0]),
296 direct.Next(edge[1]),
300 if (next[0].GetType().block_light) {
303 light += next[0].GetLight();
308 if (next[1].GetType().block_light) {
311 light += next[1].GetLight();
317 BlockLookup corner = next[0].Next(edge[1]);
319 if (corner.GetType().block_light) {
322 light += corner.GetLight();
326 } else if (next[1]) {
327 BlockLookup corner = next[1].Next(edge[0]);
329 if (corner.GetType().block_light) {
332 light += corner.GetLight();
341 return (light / num) - (occlusion * 0.8f);
345 bool Chunk::IsSurface(const RoughLocation::Fine &pos) const noexcept {
346 const Block &block = BlockAt(pos);
347 if (!Type(block).visible) {
350 for (int face = 0; face < Block::FACE_COUNT; ++face) {
351 BlockLookup next = BlockLookup(const_cast<Chunk *>(this), pos, Block::Face(face));
352 if (!next || !next.GetType().visible) {
360 bool Chunk::Intersection(
368 coll.depth = std::numeric_limits<float>::infinity();
369 for (int z = 0; z < side; ++z) {
370 for (int y = 0; y < side; ++y) {
371 for (int x = 0; x < side; ++x, ++idx) {
372 const BlockType &type = Type(idx);
373 if (!type.collision || !type.shape) {
378 if (type.shape->Intersects(ray, M * ToTransform(RoughLocation::Fine(x, y, z), idx), cur_dist, cur_norm)) {
379 if (cur_dist < coll.depth) {
381 coll.depth = cur_dist;
382 coll.normal = cur_norm;
389 if (coll.block < 0) {
392 coll.normal = glm::vec3(BlockAt(coll.block).Transform() * glm::vec4(coll.normal, 0.0f));
397 bool Chunk::Intersection(
399 const glm::mat4 &Mbox,
400 const glm::mat4 &Mchunk,
401 std::vector<WorldCollision> &col
407 if (!blank::Intersection(box, Mbox, Bounds(), Mchunk, penetration, normal)) {
410 for (int idx = 0, z = 0; z < side; ++z) {
411 for (int y = 0; y < side; ++y) {
412 for (int x = 0; x < side; ++x, ++idx) {
413 const BlockType &type = Type(idx);
414 if (!type.collision || !type.shape) {
417 if (type.shape->Intersects(Mchunk * ToTransform(RoughLocation::Fine(x, y, z), idx), box, Mbox, penetration, normal)) {
418 col.emplace_back(this, idx, penetration, normal);
430 BlockMesh::Buffer buf;
434 void Chunk::Update(BlockMesh &model) noexcept {
435 int vtx_count = 0, idx_count = 0;
436 for (const auto &block : blocks) {
437 const BlockType &type = Type(block);
438 if (type.visible && type.shape) {
439 vtx_count += type.shape->VertexCount();
440 idx_count += type.shape->IndexCount();
444 buf.Reserve(vtx_count, idx_count);
448 BlockMesh::Index vtx_counter = 0;
449 for (size_t z = 0; z < side; ++z) {
450 for (size_t y = 0; y < side; ++y) {
451 for (size_t x = 0; x < side; ++x, ++idx) {
452 const BlockType &type = Type(BlockAt(idx));
453 const RoughLocation::Fine pos(x, y, z);
455 if (!type.visible || !type.shape || Obstructed(pos).All()) continue;
457 type.FillBlockMesh(buf, ToTransform(pos, idx), vtx_counter);
458 size_t vtx_begin = vtx_counter;
459 vtx_counter += type.shape->VertexCount();
461 for (size_t vtx = vtx_begin; vtx < vtx_counter; ++vtx) {
462 buf.lights.emplace_back(GetVertexLight(
465 type.shape->VertexNormal(vtx - vtx_begin, BlockAt(idx).Transform())
477 Block::FaceSet Chunk::Obstructed(const RoughLocation::Fine &pos) const noexcept {
478 Block::FaceSet result;
480 for (int f = 0; f < Block::FACE_COUNT; ++f) {
481 Block::Face face = Block::Face(f);
482 BlockLookup next(const_cast<Chunk *>(this), pos, face);
483 if (next && next.GetType().FaceFilled(next.GetBlock(), Block::Opposite(face))) {
491 glm::mat4 Chunk::ToTransform(const RoughLocation::Fine &pos, int idx) const noexcept {
492 return glm::translate(ToCoords(pos)) * BlockAt(idx).Transform();
496 BlockLookup::BlockLookup(Chunk *c, const RoughLocation::Fine &p) noexcept
498 while (pos.x >= Chunk::side) {
499 if (chunk->HasNeighbor(Block::FACE_RIGHT)) {
500 chunk = &chunk->GetNeighbor(Block::FACE_RIGHT);
501 pos.x -= Chunk::side;
508 if (chunk->HasNeighbor(Block::FACE_LEFT)) {
509 chunk = &chunk->GetNeighbor(Block::FACE_LEFT);
510 pos.x += Chunk::side;
516 while (pos.y >= Chunk::side) {
517 if (chunk->HasNeighbor(Block::FACE_UP)) {
518 chunk = &chunk->GetNeighbor(Block::FACE_UP);
519 pos.y -= Chunk::side;
526 if (chunk->HasNeighbor(Block::FACE_DOWN)) {
527 chunk = &chunk->GetNeighbor(Block::FACE_DOWN);
528 pos.y += Chunk::side;
534 while (pos.z >= Chunk::side) {
535 if (chunk->HasNeighbor(Block::FACE_FRONT)) {
536 chunk = &chunk->GetNeighbor(Block::FACE_FRONT);
537 pos.z -= Chunk::side;
544 if (chunk->HasNeighbor(Block::FACE_BACK)) {
545 chunk = &chunk->GetNeighbor(Block::FACE_BACK);
546 pos.z += Chunk::side;
554 BlockLookup::BlockLookup(Chunk *c, const RoughLocation::Fine &p, Block::Face face) noexcept
556 pos += Block::FaceNormal(face);
557 if (!Chunk::InBounds(pos)) {
558 pos -= Block::FaceNormal(face) * ExactLocation::Extent();
559 chunk = &chunk->GetNeighbor(face);
564 ChunkLoader::ChunkLoader(
566 const Generator &gen,
567 const WorldSave &save
575 void ChunkLoader::Update(int dt) {
576 // check if there's chunks waiting to be loaded
577 // load until one of load or generation limits was hit
578 constexpr int max_load = 10;
579 constexpr int max_gen = 1;
582 while (loaded < max_load && generated < max_gen && store.HasMissing()) {
590 // store a few chunks as well
591 constexpr int max_save = 10;
593 for (Chunk &chunk : store) {
594 if (chunk.ShouldUpdateSave()) {
597 if (saved >= max_save) {
604 int ChunkLoader::ToLoad() const noexcept {
605 return store.EstimateMissing();
608 bool ChunkLoader::LoadOne() {
609 if (!store.HasMissing()) return false;
611 ExactLocation::Coarse pos = store.NextMissing();
612 Chunk *chunk = store.Allocate(pos);
614 // chunk store corrupted?
618 bool generated = false;
619 if (save.Exists(pos)) {
626 ChunkIndex *index = store.ClosestIndex(pos);
631 ExactLocation::Coarse begin(pos - ExactLocation::Coarse(1));
632 ExactLocation::Coarse end(pos + ExactLocation::Coarse(2));
633 for (ExactLocation::Coarse iter(begin); iter.z < end.z; ++iter.z) {
634 for (iter.y = begin.y; iter.y < end.y; ++iter.y) {
635 for (iter.x = begin.x; iter.x < end.x; ++iter.x) {
636 if (index->IsBorder(iter)) continue;
637 Chunk *light_chunk = index->Get(iter);
638 if (!light_chunk) continue;
639 if (index->HasAllSurrounding(iter)) {
640 if (!light_chunk->Lighted()) {
641 light_chunk->ScanLights();
643 light_chunk->InvalidateMesh();
653 void ChunkLoader::LoadN(std::size_t n) {
654 std::size_t end = std::min(n, std::size_t(ToLoad()));
655 for (std::size_t i = 0; i < end && store.HasMissing(); ++i) {
661 ChunkRenderer::ChunkRenderer(ChunkIndex &index)
663 , models(index.TotalChunks())
665 , fog_density(0.0f) {
669 ChunkRenderer::~ChunkRenderer() {
673 int ChunkRenderer::MissingChunks() const noexcept {
674 return index.MissingChunks();
677 void ChunkRenderer::LoadTextures(const AssetLoader &loader, const ResourceIndex &tex_index) {
679 loader.LoadTextures(tex_index, block_tex);
680 block_tex.FilterNearest();
683 void ChunkRenderer::Update(int dt) {
684 for (int i = 0, updates = 0; updates < dt && i < index.TotalChunks(); ++i) {
685 if (!index[i]) continue;
686 if (!index[i]->Lighted() && index.HasAllSurrounding(index[i]->Position())) {
687 index[i]->ScanLights();
689 if (index[i]->ShouldUpdateMesh()) {
690 index[i]->Update(models[i]);
696 void ChunkRenderer::Render(Viewport &viewport) {
697 BlockLighting &chunk_prog = viewport.ChunkProgram();
698 chunk_prog.SetTexture(block_tex);
699 chunk_prog.SetFogDensity(fog_density);
701 for (int i = 0; i < index.TotalChunks(); ++i) {
702 if (!index[i]) continue;
703 glm::mat4 m(index[i]->Transform(index.Base()));
704 glm::mat4 mvp(chunk_prog.GetVP() * m);
705 if (!CullTest(Chunk::Bounds(), mvp)) {
706 if (index[i]->ShouldUpdateMesh()) {
707 index[i]->Update(models[i]);
716 ChunkIndex::ChunkIndex(ChunkStore &store, const ExactLocation::Coarse &base, int extent)
720 , side_length(2 * extent + 1)
721 , total_length(side_length * side_length * side_length)
724 , stride(1, side_length, side_length * side_length)
725 , chunks(total_length, nullptr) {
729 ChunkIndex::~ChunkIndex() {
733 bool ChunkIndex::InRange(const ExactLocation::Coarse &pos) const noexcept {
734 return Distance(pos) <= extent;
737 bool ChunkIndex::IsBorder(const ExactLocation::Coarse &pos) const noexcept {
738 return Distance(pos) == extent;
741 int ChunkIndex::Distance(const ExactLocation::Coarse &pos) const noexcept {
742 return manhattan_radius(pos - base);
745 bool ChunkIndex::HasAllSurrounding(const ExactLocation::Coarse &pos) const noexcept {
746 ExactLocation::Coarse begin(pos - ExactLocation::Coarse(1));
747 ExactLocation::Coarse end(pos + ExactLocation::Coarse(2));
748 for (ExactLocation::Coarse iter(begin); iter.z < end.z; ++iter.z) {
749 for (iter.y = begin.y; iter.y < end.y; ++iter.y) {
750 for (iter.x = begin.x; iter.x < end.x; ++iter.x) {
751 if (!Get(iter)) return false;
758 int ChunkIndex::IndexOf(const ExactLocation::Coarse &pos) const noexcept {
759 ExactLocation::Coarse mod_pos(
764 return mod_pos.x * stride.x
765 + mod_pos.y * stride.y
766 + mod_pos.z * stride.z;
769 ExactLocation::Coarse ChunkIndex::PositionOf(int i) const noexcept {
770 ExactLocation::Coarse zero_pos(
771 (i / stride.x) % side_length,
772 (i / stride.y) % side_length,
773 (i / stride.z) % side_length
775 ExactLocation::Coarse zero_base(
780 ExactLocation::Coarse base_relative(zero_pos - zero_base);
781 if (base_relative.x > extent) base_relative.x -= side_length;
782 else if (base_relative.x < -extent) base_relative.x += side_length;
783 if (base_relative.y > extent) base_relative.y -= side_length;
784 else if (base_relative.y < -extent) base_relative.y += side_length;
785 if (base_relative.z > extent) base_relative.z -= side_length;
786 else if (base_relative.z < -extent) base_relative.z += side_length;
787 return base + base_relative;
790 Chunk *ChunkIndex::Get(const ExactLocation::Coarse &pos) noexcept {
792 return chunks[IndexOf(pos)];
798 const Chunk *ChunkIndex::Get(const ExactLocation::Coarse &pos) const noexcept {
800 return chunks[IndexOf(pos)];
806 void ChunkIndex::Rebase(const ExactLocation::Coarse &new_base) {
807 if (new_base == base) return;
809 ExactLocation::Coarse diff(new_base - base);
811 if (manhattan_radius(diff) > extent) {
812 // that's more than half, so probably not worth shifting
821 Shift(Block::FACE_RIGHT);
825 Shift(Block::FACE_LEFT);
829 Shift(Block::FACE_UP);
833 Shift(Block::FACE_DOWN);
837 Shift(Block::FACE_FRONT);
841 Shift(Block::FACE_BACK);
847 int ChunkIndex::GetCol(int c) const noexcept {
849 if (c < 0) c += side_length;
853 void ChunkIndex::Shift(Block::Face f) {
854 int a_axis = Block::Axis(f);
855 int b_axis = (a_axis + 1) % 3;
856 int c_axis = (a_axis + 2) % 3;
857 int dir = Block::Direction(f);
859 int a = GetCol(base[a_axis] + (extent * dir));
860 int a_stride = a * stride[a_axis];
861 for (int b = 0; b < side_length; ++b) {
862 int b_stride = b * stride[b_axis];
863 for (int c = 0; c < side_length; ++c) {
864 int bc_stride = b_stride + c * stride[c_axis];
865 int index = a_stride + bc_stride;
867 int neighbor = ((a - dir + side_length) % side_length) * stride[a_axis] + bc_stride;
868 if (chunks[neighbor] && chunks[neighbor]->HasNeighbor(f)) {
869 Set(index, chunks[neighbor]->GetNeighbor(f));
875 void ChunkIndex::Clear() noexcept {
876 for (int i = 0; i < total_length && total_indexed > 0; ++i) {
881 void ChunkIndex::Scan() noexcept {
882 for (Chunk &chunk : store) {
887 void ChunkIndex::Register(Chunk &chunk) noexcept {
888 if (InRange(chunk.Position())) {
889 Set(IndexOf(chunk.Position()), chunk);
893 void ChunkIndex::Set(int index, Chunk &chunk) noexcept {
895 chunks[index] = &chunk;
900 void ChunkIndex::Unset(int index) noexcept {
902 chunks[index]->UnRef();
903 chunks[index] = nullptr;
908 ExactLocation::Coarse ChunkIndex::NextMissing() noexcept {
909 if (MissingChunks() > 0) {
910 int roundtrip = last_missing;
911 last_missing = (last_missing + 1) % total_length;
912 while (chunks[last_missing]) {
913 last_missing = (last_missing + 1) % total_length;
914 if (last_missing == roundtrip) {
919 return PositionOf(last_missing);
923 ChunkStore::ChunkStore(const BlockTypeRegistry &types)
931 ChunkStore::~ChunkStore() {
935 ChunkIndex &ChunkStore::MakeIndex(const ExactLocation::Coarse &pos, int extent) {
936 indices.emplace_back(*this, pos, extent);
937 return indices.back();
940 void ChunkStore::UnregisterIndex(ChunkIndex &index) {
941 for (auto i = indices.begin(), end = indices.end(); i != end; ++i) {
951 ChunkIndex *ChunkStore::ClosestIndex(const ExactLocation::Coarse &pos) {
952 ChunkIndex *closest_index = nullptr;
953 int closest_distance = std::numeric_limits<int>::max();
955 for (ChunkIndex &index : indices) {
956 int distance = index.Distance(pos);
957 if (distance < closest_distance) {
958 closest_index = &index;
959 closest_distance = distance;
963 return closest_index;
966 Chunk *ChunkStore::Get(const ExactLocation::Coarse &pos) {
967 for (ChunkIndex &index : indices) {
968 Chunk *chunk = index.Get(pos);
976 Chunk *ChunkStore::Allocate(const ExactLocation::Coarse &pos) {
977 Chunk *chunk = Get(pos);
982 loaded.emplace(loaded.begin(), types);
984 loaded.splice(loaded.begin(), free, free.begin());
985 loaded.front().Unlink();
987 chunk = &loaded.front();
988 chunk->Position(pos);
989 for (ChunkIndex &index : indices) {
990 if (index.InRange(pos)) {
991 index.Register(*chunk);
994 for (int i = 0; i < Block::FACE_COUNT; ++i) {
995 Block::Face face = Block::Face(i);
996 ExactLocation::Coarse neighbor_pos(pos + Block::FaceNormal(face));
997 Chunk *neighbor = Get(neighbor_pos);
999 chunk->SetNeighbor(face, *neighbor);
1005 bool ChunkStore::HasMissing() const noexcept {
1006 for (const ChunkIndex &index : indices) {
1007 if (index.MissingChunks() > 0) {
1014 int ChunkStore::EstimateMissing() const noexcept {
1016 for (const ChunkIndex &index : indices) {
1017 missing += index.MissingChunks();
1022 ExactLocation::Coarse ChunkStore::NextMissing() noexcept {
1023 for (ChunkIndex &index : indices) {
1024 if (index.MissingChunks()) {
1025 return index.NextMissing();
1028 return ExactLocation::Coarse(0, 0, 0);
1031 void ChunkStore::Clean() {
1032 for (auto i = loaded.begin(), end = loaded.end(); i != end;) {
1033 if (i->Referenced() || i->ShouldUpdateSave()) {
1038 free.splice(free.end(), loaded, chunk);
1040 chunk->InvalidateMesh();