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"
23 #include <glm/gtx/io.hpp>
28 constexpr int Chunk::side;
29 constexpr int Chunk::size;
32 Chunk::Chunk(const BlockTypeRegistry &types) noexcept
46 Chunk::Chunk(Chunk &&other) noexcept
48 , generated(other.generated)
49 , lighted(other.lighted)
50 , position(other.position)
51 , ref_count(other.ref_count)
52 , dirty_mesh(other.dirty_mesh)
53 , dirty_save(other.dirty_save) {
54 std::copy(other.neighbor, other.neighbor + sizeof(neighbor), neighbor);
55 std::copy(other.blocks, other.blocks + sizeof(blocks), blocks);
56 std::copy(other.light, other.light + sizeof(light), light);
60 Chunk &Chunk::operator =(Chunk &&other) noexcept {
62 std::copy(other.neighbor, other.neighbor + sizeof(neighbor), neighbor);
63 std::copy(other.blocks, other.blocks + sizeof(blocks), blocks);
64 std::copy(other.light, other.light + sizeof(light), light);
65 generated = other.generated;
66 lighted = other.lighted;
67 position = other.position;
68 std::swap(ref_count, other.ref_count);
69 dirty_mesh = other.dirty_save;
70 dirty_save = other.dirty_save;
80 RoughLocation::Fine pos;
82 SetNode(Chunk *chunk, RoughLocation::Fine pos)
83 : chunk(chunk), pos(pos) { }
85 int Get() const noexcept { return chunk->GetLight(pos); }
86 void Set(int level) noexcept { chunk->SetLight(pos, level); }
88 const BlockType &GetType() const noexcept { return chunk->Type(Chunk::ToIndex(pos)); }
90 bool HasNext(Block::Face face) noexcept {
91 const BlockType &type = GetType();
92 if (type.block_light && !type.luminosity) return false;
93 const BlockLookup next(chunk, pos, face);
96 SetNode GetNext(Block::Face face) noexcept {
97 const BlockLookup next(chunk, pos, face);
98 return SetNode(&next.GetChunk(), next.GetBlockPos());
108 UnsetNode(Chunk *chunk, RoughLocation::Fine pos)
109 : SetNode(chunk, pos), level(Get()) { }
111 UnsetNode(const SetNode &set)
112 : SetNode(set), level(Get()) { }
115 bool HasNext(Block::Face face) noexcept {
116 const BlockLookup next(chunk, pos, face);
119 UnsetNode GetNext(Block::Face face) noexcept { return UnsetNode(SetNode::GetNext(face)); }
123 std::queue<SetNode> light_queue;
124 std::queue<UnsetNode> dark_queue;
126 void work_light() noexcept {
127 while (!light_queue.empty()) {
128 SetNode node = light_queue.front();
131 int level = node.Get() - 1;
132 for (int face = 0; face < Block::FACE_COUNT; ++face) {
133 if (node.HasNext(Block::Face(face))) {
134 SetNode other = node.GetNext(Block::Face(face));
135 if (other.Get() < level) {
137 light_queue.emplace(other);
144 void work_dark() noexcept {
145 while (!dark_queue.empty()) {
146 UnsetNode node = dark_queue.front();
149 for (int face = 0; face < Block::FACE_COUNT; ++face) {
150 if (node.HasNext(Block::Face(face))) {
151 UnsetNode other = node.GetNext(Block::Face(face));
152 // TODO: if there a light source here with the same level this will err
153 if (other.Get() != 0 && other.Get() < node.level) {
155 dark_queue.emplace(other);
157 light_queue.emplace(other);
166 void Chunk::SetBlock(int index, const Block &block) noexcept {
167 const BlockType &old_type = Type(blocks[index]);
168 const BlockType &new_type = Type(block);
170 blocks[index] = block;
173 if (!lighted || &old_type == &new_type) return;
175 if (new_type.luminosity > old_type.luminosity) {
177 SetLight(index, new_type.luminosity);
178 light_queue.emplace(this, ToPos(index));
180 } else if (new_type.luminosity < old_type.luminosity) {
182 dark_queue.emplace(this, ToPos(index));
185 SetLight(index, new_type.luminosity);
186 light_queue.emplace(this, ToPos(index));
188 } else if (new_type.block_light && !old_type.block_light) {
190 if (GetLight(index) > 0) {
191 dark_queue.emplace(this, ToPos(index));
196 } else if (!new_type.block_light && old_type.block_light) {
199 RoughLocation::Fine pos(ToPos(index));
200 for (int face = 0; face < Block::FACE_COUNT; ++face) {
201 BlockLookup next_block(this, pos, Block::Face(face));
203 level = std::max(level, next_block.GetLight());
207 SetLight(index, level - 1);
208 light_queue.emplace(this, pos);
214 void Chunk::ScanLights() {
216 RoughLocation::Fine pos(0, 0, 0);
217 for (; pos.z < side; ++pos.z) {
218 for (pos.y = 0; pos.y < side; ++pos.y) {
219 for (pos.x = 0; pos.x < side; ++pos.x, ++idx) {
220 const BlockType &type = Type(blocks[idx]);
221 if (type.luminosity) {
222 SetLight(idx, type.luminosity);
223 light_queue.emplace(this, pos);
232 void Chunk::SetNeighbor(Block::Face face, Chunk &other) noexcept {
233 neighbor[face] = &other;
234 other.neighbor[Block::Opposite(face)] = this;
237 void Chunk::Unlink() noexcept {
238 for (int face = 0; face < Block::FACE_COUNT; ++face) {
239 if (neighbor[face]) {
240 neighbor[face]->neighbor[Block::Opposite(Block::Face(face))] = nullptr;
241 neighbor[face] = nullptr;
247 void Chunk::SetLight(int index, int level) noexcept {
248 if (light[index] != level) {
249 light[index] = level;
254 int Chunk::GetLight(int index) const noexcept {
258 float Chunk::GetVertexLight(const RoughLocation::Fine &pos, const BlockMesh::Position &vtx, const EntityMesh::Normal &norm) const noexcept {
259 int index = ToIndex(pos);
260 float light = GetLight(index);
262 Block::Face direct_face(Block::NormalFace(norm));
264 BlockLookup direct(const_cast<Chunk *>(this), pos, Block::NormalFace(norm));
266 float direct_light = direct.GetLight();
267 if (direct_light > light) {
268 light = direct_light;
274 if (Type(BlockAt(index)).luminosity > 0 || direct.GetType().block_light) {
279 switch (Block::Axis(direct_face)) {
281 edge[0] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
282 edge[1] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
285 edge[0] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
286 edge[1] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
289 edge[0] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
290 edge[1] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
297 BlockLookup next[2] = {
298 direct.Next(edge[0]),
299 direct.Next(edge[1]),
303 if (next[0].GetType().block_light) {
306 light += next[0].GetLight();
311 if (next[1].GetType().block_light) {
314 light += next[1].GetLight();
320 BlockLookup corner = next[0].Next(edge[1]);
322 if (corner.GetType().block_light) {
325 light += corner.GetLight();
329 } else if (next[1]) {
330 BlockLookup corner = next[1].Next(edge[0]);
332 if (corner.GetType().block_light) {
335 light += corner.GetLight();
344 return (light / num) - (occlusion * 0.8f);
348 bool Chunk::IsSurface(const RoughLocation::Fine &pos) const noexcept {
349 const Block &block = BlockAt(pos);
350 if (!Type(block).visible) {
353 for (int face = 0; face < Block::FACE_COUNT; ++face) {
354 BlockLookup next = BlockLookup(const_cast<Chunk *>(this), pos, Block::Face(face));
355 if (!next || !next.GetType().visible) {
363 bool Chunk::Intersection(
371 coll.depth = std::numeric_limits<float>::infinity();
372 for (int z = 0; z < side; ++z) {
373 for (int y = 0; y < side; ++y) {
374 for (int x = 0; x < side; ++x, ++idx) {
375 const BlockType &type = Type(idx);
376 if (!type.collision || !type.shape) {
381 if (type.shape->Intersects(ray, M * ToTransform(RoughLocation::Fine(x, y, z), idx), cur_dist, cur_norm)) {
382 if (cur_dist < coll.depth) {
384 coll.depth = cur_dist;
385 coll.normal = cur_norm;
392 if (coll.block < 0) {
395 coll.normal = glm::vec3(BlockAt(coll.block).Transform() * glm::vec4(coll.normal, 0.0f));
400 bool Chunk::Intersection(
402 const glm::mat4 &Mbox,
403 const glm::mat4 &Mchunk,
404 std::vector<WorldCollision> &col
410 if (!blank::Intersection(box, Mbox, Bounds(), Mchunk, penetration, normal)) {
414 // box's origin relative to the chunk
415 const glm::vec3 box_coords(Mbox[3] - Mchunk[3]);
416 const float box_rad = box.OriginRadius();
418 // assume a bounding radius of 2 for blocks
419 constexpr float block_rad = 2.0f;
420 const float bb_radius = box_rad + block_rad;
422 const RoughLocation::Fine begin(max(
423 RoughLocation::Fine(0),
424 RoughLocation::Fine(floor(box_coords - bb_radius))
426 const RoughLocation::Fine end(min(
427 RoughLocation::Fine(side - 1),
428 RoughLocation::Fine(ceil(box_coords + bb_radius))
431 for (RoughLocation::Fine pos(begin); pos.z < end.y; ++pos.z) {
432 for (pos.y = begin.y; pos.y < end.y; ++pos.y) {
433 for (pos.x = begin.x; pos.x < end.x; ++pos.x) {
434 int idx = ToIndex(pos);
435 const BlockType &type = Type(idx);
436 if (!type.collision || !type.shape) {
439 if (type.shape->Intersects(Mchunk * ToTransform(pos, idx), box, Mbox, penetration, normal)) {
440 col.emplace_back(this, idx, penetration, normal);
449 bool Chunk::Intersection(
450 const Entity &entity,
451 const glm::mat4 &Mentity,
452 const glm::mat4 &Mchunk,
453 std::vector<WorldCollision> &col
455 // entity's origin relative to the chunk
456 const glm::vec3 entity_coords(Mentity[3] - Mchunk[3]);
457 const float ec_radius = entity.Radius() + Radius();
459 if (distance_squared(entity_coords, Center()) > ec_radius * ec_radius) {
463 if (entity.ID() == 1) {
464 std::cout << "chunk: " << (Position() * 16) << ", entity: " << entity.AbsolutePosition() << std::endl;
465 std::cout << "\tMentity[3]: " << Mentity[3] << std::endl;
466 std::cout << "\tMchunk[3]: " << Mentity[3] << std::endl;
473 // assume a bounding radius of 2 for blocks
474 constexpr float block_rad = 2.0f;
475 const float eb_radius = entity.Radius() + block_rad;
477 const RoughLocation::Fine begin(max(
478 RoughLocation::Fine(0),
479 RoughLocation::Fine(floor(entity_coords - eb_radius))
481 const RoughLocation::Fine end(min(
482 RoughLocation::Fine(side - 1),
483 RoughLocation::Fine(ceil(entity_coords + eb_radius))
486 for (RoughLocation::Fine pos(begin); pos.z < end.y; ++pos.z) {
487 for (pos.y = begin.y; pos.y < end.y; ++pos.y) {
488 for (pos.x = begin.x; pos.x < end.x; ++pos.x) {
489 int idx = ToIndex(pos);
490 const BlockType &type = Type(idx);
491 if (!type.collision || !type.shape) {
494 if (type.shape->Intersects(Mchunk * ToTransform(pos, idx), entity.Bounds(), Mentity, penetration, normal)) {
495 col.emplace_back(this, idx, penetration, normal);
507 BlockMesh::Buffer buf;
511 void Chunk::Update(BlockMesh &model) noexcept {
512 int vtx_count = 0, idx_count = 0;
513 for (const auto &block : blocks) {
514 const BlockType &type = Type(block);
515 if (type.visible && type.shape) {
516 vtx_count += type.shape->VertexCount();
517 idx_count += type.shape->IndexCount();
521 buf.Reserve(vtx_count, idx_count);
525 BlockMesh::Index vtx_counter = 0;
526 for (size_t z = 0; z < side; ++z) {
527 for (size_t y = 0; y < side; ++y) {
528 for (size_t x = 0; x < side; ++x, ++idx) {
529 const BlockType &type = Type(BlockAt(idx));
530 const RoughLocation::Fine pos(x, y, z);
532 if (!type.visible || !type.shape || Obstructed(pos).All()) continue;
534 type.FillBlockMesh(buf, ToTransform(pos, idx), vtx_counter);
535 size_t vtx_begin = vtx_counter;
536 vtx_counter += type.shape->VertexCount();
538 for (size_t vtx = vtx_begin; vtx < vtx_counter; ++vtx) {
539 buf.lights.emplace_back(GetVertexLight(
542 type.shape->VertexNormal(vtx - vtx_begin, BlockAt(idx).Transform())
554 Block::FaceSet Chunk::Obstructed(const RoughLocation::Fine &pos) const noexcept {
555 Block::FaceSet result;
557 for (int f = 0; f < Block::FACE_COUNT; ++f) {
558 Block::Face face = Block::Face(f);
559 BlockLookup next(const_cast<Chunk *>(this), pos, face);
560 if (next && next.GetType().FaceFilled(next.GetBlock(), Block::Opposite(face))) {
568 glm::mat4 Chunk::ToTransform(const RoughLocation::Fine &pos, int idx) const noexcept {
569 return glm::translate(ToCoords(pos)) * BlockAt(idx).Transform();
573 BlockLookup::BlockLookup(Chunk *c, const RoughLocation::Fine &p) noexcept
575 while (pos.x >= Chunk::side) {
576 if (chunk->HasNeighbor(Block::FACE_RIGHT)) {
577 chunk = &chunk->GetNeighbor(Block::FACE_RIGHT);
578 pos.x -= Chunk::side;
585 if (chunk->HasNeighbor(Block::FACE_LEFT)) {
586 chunk = &chunk->GetNeighbor(Block::FACE_LEFT);
587 pos.x += Chunk::side;
593 while (pos.y >= Chunk::side) {
594 if (chunk->HasNeighbor(Block::FACE_UP)) {
595 chunk = &chunk->GetNeighbor(Block::FACE_UP);
596 pos.y -= Chunk::side;
603 if (chunk->HasNeighbor(Block::FACE_DOWN)) {
604 chunk = &chunk->GetNeighbor(Block::FACE_DOWN);
605 pos.y += Chunk::side;
611 while (pos.z >= Chunk::side) {
612 if (chunk->HasNeighbor(Block::FACE_FRONT)) {
613 chunk = &chunk->GetNeighbor(Block::FACE_FRONT);
614 pos.z -= Chunk::side;
621 if (chunk->HasNeighbor(Block::FACE_BACK)) {
622 chunk = &chunk->GetNeighbor(Block::FACE_BACK);
623 pos.z += Chunk::side;
631 BlockLookup::BlockLookup(Chunk *c, const RoughLocation::Fine &p, Block::Face face) noexcept
633 pos += Block::FaceNormal(face);
634 if (!Chunk::InBounds(pos)) {
635 pos -= Block::FaceNormal(face) * ExactLocation::Extent();
636 chunk = &chunk->GetNeighbor(face);
641 ChunkLoader::ChunkLoader(
643 const Generator &gen,
644 const WorldSave &save
652 void ChunkLoader::Update(int dt) {
653 // check if there's chunks waiting to be loaded
654 // load until one of load or generation limits was hit
655 constexpr int max_load = 10;
656 constexpr int max_gen = 1;
659 while (loaded < max_load && generated < max_gen && store.HasMissing()) {
667 // store a few chunks as well
668 constexpr int max_save = 10;
670 for (Chunk &chunk : store) {
671 if (chunk.ShouldUpdateSave()) {
674 if (saved >= max_save) {
681 int ChunkLoader::ToLoad() const noexcept {
682 return store.EstimateMissing();
685 bool ChunkLoader::LoadOne() {
686 if (!store.HasMissing()) return false;
688 ExactLocation::Coarse pos = store.NextMissing();
689 Chunk *chunk = store.Allocate(pos);
691 // chunk store corrupted?
695 bool generated = false;
696 if (save.Exists(pos)) {
703 ChunkIndex *index = store.ClosestIndex(pos);
708 ExactLocation::Coarse begin(pos - ExactLocation::Coarse(1));
709 ExactLocation::Coarse end(pos + ExactLocation::Coarse(2));
710 for (ExactLocation::Coarse iter(begin); iter.z < end.z; ++iter.z) {
711 for (iter.y = begin.y; iter.y < end.y; ++iter.y) {
712 for (iter.x = begin.x; iter.x < end.x; ++iter.x) {
713 if (index->IsBorder(iter)) continue;
714 Chunk *light_chunk = index->Get(iter);
715 if (!light_chunk) continue;
716 if (index->HasAllSurrounding(iter)) {
717 if (!light_chunk->Lighted()) {
718 light_chunk->ScanLights();
720 light_chunk->InvalidateMesh();
730 void ChunkLoader::LoadN(std::size_t n) {
731 std::size_t end = std::min(n, std::size_t(ToLoad()));
732 for (std::size_t i = 0; i < end && store.HasMissing(); ++i) {
738 ChunkRenderer::ChunkRenderer(ChunkIndex &index)
740 , models(index.TotalChunks())
742 , fog_density(0.0f) {
746 ChunkRenderer::~ChunkRenderer() {
750 int ChunkRenderer::MissingChunks() const noexcept {
751 return index.MissingChunks();
754 void ChunkRenderer::LoadTextures(const AssetLoader &loader, const ResourceIndex &tex_index) {
756 loader.LoadTextures(tex_index, block_tex);
757 block_tex.FilterNearest();
760 void ChunkRenderer::Update(int dt) {
761 for (int i = 0, updates = 0; updates < dt && i < index.TotalChunks(); ++i) {
762 if (!index[i]) continue;
763 if (!index[i]->Lighted() && index.HasAllSurrounding(index[i]->Position())) {
764 index[i]->ScanLights();
766 if (index[i]->ShouldUpdateMesh()) {
767 index[i]->Update(models[i]);
773 void ChunkRenderer::Render(Viewport &viewport) {
774 BlockLighting &chunk_prog = viewport.ChunkProgram();
775 chunk_prog.SetTexture(block_tex);
776 chunk_prog.SetFogDensity(fog_density);
778 for (int i = 0; i < index.TotalChunks(); ++i) {
779 if (!index[i]) continue;
780 glm::mat4 m(index[i]->Transform(index.Base()));
781 glm::mat4 mvp(chunk_prog.GetVP() * m);
782 if (!CullTest(Chunk::Bounds(), mvp)) {
783 if (index[i]->ShouldUpdateMesh()) {
784 index[i]->Update(models[i]);
793 ChunkIndex::ChunkIndex(ChunkStore &store, const ExactLocation::Coarse &base, int extent)
797 , side_length(2 * extent + 1)
798 , total_length(side_length * side_length * side_length)
801 , stride(1, side_length, side_length * side_length)
802 , chunks(total_length, nullptr) {
806 ChunkIndex::~ChunkIndex() {
810 bool ChunkIndex::InRange(const ExactLocation::Coarse &pos) const noexcept {
811 return Distance(pos) <= extent;
814 bool ChunkIndex::IsBorder(const ExactLocation::Coarse &pos) const noexcept {
815 return Distance(pos) == extent;
818 int ChunkIndex::Distance(const ExactLocation::Coarse &pos) const noexcept {
819 return manhattan_radius(pos - base);
822 bool ChunkIndex::HasAllSurrounding(const ExactLocation::Coarse &pos) const noexcept {
823 ExactLocation::Coarse begin(pos - ExactLocation::Coarse(1));
824 ExactLocation::Coarse end(pos + ExactLocation::Coarse(2));
825 for (ExactLocation::Coarse iter(begin); iter.z < end.z; ++iter.z) {
826 for (iter.y = begin.y; iter.y < end.y; ++iter.y) {
827 for (iter.x = begin.x; iter.x < end.x; ++iter.x) {
828 if (!Get(iter)) return false;
835 int ChunkIndex::IndexOf(const ExactLocation::Coarse &pos) const noexcept {
836 ExactLocation::Coarse mod_pos(
841 return mod_pos.x * stride.x
842 + mod_pos.y * stride.y
843 + mod_pos.z * stride.z;
846 ExactLocation::Coarse ChunkIndex::PositionOf(int i) const noexcept {
847 ExactLocation::Coarse zero_pos(
848 (i / stride.x) % side_length,
849 (i / stride.y) % side_length,
850 (i / stride.z) % side_length
852 ExactLocation::Coarse zero_base(
857 ExactLocation::Coarse base_relative(zero_pos - zero_base);
858 if (base_relative.x > extent) base_relative.x -= side_length;
859 else if (base_relative.x < -extent) base_relative.x += side_length;
860 if (base_relative.y > extent) base_relative.y -= side_length;
861 else if (base_relative.y < -extent) base_relative.y += side_length;
862 if (base_relative.z > extent) base_relative.z -= side_length;
863 else if (base_relative.z < -extent) base_relative.z += side_length;
864 return base + base_relative;
867 Chunk *ChunkIndex::Get(const ExactLocation::Coarse &pos) noexcept {
869 return chunks[IndexOf(pos)];
875 const Chunk *ChunkIndex::Get(const ExactLocation::Coarse &pos) const noexcept {
877 return chunks[IndexOf(pos)];
883 void ChunkIndex::Rebase(const ExactLocation::Coarse &new_base) {
884 if (new_base == base) return;
886 ExactLocation::Coarse diff(new_base - base);
888 if (manhattan_radius(diff) > extent) {
889 // that's more than half, so probably not worth shifting
898 Shift(Block::FACE_RIGHT);
902 Shift(Block::FACE_LEFT);
906 Shift(Block::FACE_UP);
910 Shift(Block::FACE_DOWN);
914 Shift(Block::FACE_FRONT);
918 Shift(Block::FACE_BACK);
924 int ChunkIndex::GetCol(int c) const noexcept {
926 if (c < 0) c += side_length;
930 void ChunkIndex::Shift(Block::Face f) {
931 int a_axis = Block::Axis(f);
932 int b_axis = (a_axis + 1) % 3;
933 int c_axis = (a_axis + 2) % 3;
934 int dir = Block::Direction(f);
936 int a = GetCol(base[a_axis] + (extent * dir));
937 int a_stride = a * stride[a_axis];
938 for (int b = 0; b < side_length; ++b) {
939 int b_stride = b * stride[b_axis];
940 for (int c = 0; c < side_length; ++c) {
941 int bc_stride = b_stride + c * stride[c_axis];
942 int index = a_stride + bc_stride;
944 int neighbor = ((a - dir + side_length) % side_length) * stride[a_axis] + bc_stride;
945 if (chunks[neighbor] && chunks[neighbor]->HasNeighbor(f)) {
946 Set(index, chunks[neighbor]->GetNeighbor(f));
952 void ChunkIndex::Clear() noexcept {
953 for (int i = 0; i < total_length && total_indexed > 0; ++i) {
958 void ChunkIndex::Scan() noexcept {
959 for (Chunk &chunk : store) {
964 void ChunkIndex::Register(Chunk &chunk) noexcept {
965 if (InRange(chunk.Position())) {
966 Set(IndexOf(chunk.Position()), chunk);
970 void ChunkIndex::Set(int index, Chunk &chunk) noexcept {
972 chunks[index] = &chunk;
977 void ChunkIndex::Unset(int index) noexcept {
979 chunks[index]->UnRef();
980 chunks[index] = nullptr;
985 ExactLocation::Coarse ChunkIndex::NextMissing() noexcept {
986 if (MissingChunks() > 0) {
987 int roundtrip = last_missing;
988 last_missing = (last_missing + 1) % total_length;
989 while (chunks[last_missing]) {
990 last_missing = (last_missing + 1) % total_length;
991 if (last_missing == roundtrip) {
996 return PositionOf(last_missing);
1000 ChunkStore::ChunkStore(const BlockTypeRegistry &types)
1008 ChunkStore::~ChunkStore() {
1012 ChunkIndex &ChunkStore::MakeIndex(const ExactLocation::Coarse &pos, int extent) {
1013 indices.emplace_back(*this, pos, extent);
1014 return indices.back();
1017 void ChunkStore::UnregisterIndex(ChunkIndex &index) {
1018 for (auto i = indices.begin(), end = indices.end(); i != end; ++i) {
1019 if (&*i == &index) {
1028 ChunkIndex *ChunkStore::ClosestIndex(const ExactLocation::Coarse &pos) {
1029 ChunkIndex *closest_index = nullptr;
1030 int closest_distance = std::numeric_limits<int>::max();
1032 for (ChunkIndex &index : indices) {
1033 int distance = index.Distance(pos);
1034 if (distance < closest_distance) {
1035 closest_index = &index;
1036 closest_distance = distance;
1040 return closest_index;
1043 Chunk *ChunkStore::Get(const ExactLocation::Coarse &pos) {
1044 for (ChunkIndex &index : indices) {
1045 Chunk *chunk = index.Get(pos);
1053 Chunk *ChunkStore::Allocate(const ExactLocation::Coarse &pos) {
1054 Chunk *chunk = Get(pos);
1059 loaded.emplace(loaded.begin(), types);
1061 loaded.splice(loaded.begin(), free, free.begin());
1062 loaded.front().Unlink();
1064 chunk = &loaded.front();
1065 chunk->Position(pos);
1066 for (ChunkIndex &index : indices) {
1067 if (index.InRange(pos)) {
1068 index.Register(*chunk);
1071 for (int i = 0; i < Block::FACE_COUNT; ++i) {
1072 Block::Face face = Block::Face(i);
1073 ExactLocation::Coarse neighbor_pos(pos + Block::FaceNormal(face));
1074 Chunk *neighbor = Get(neighbor_pos);
1076 chunk->SetNeighbor(face, *neighbor);
1082 bool ChunkStore::HasMissing() const noexcept {
1083 for (const ChunkIndex &index : indices) {
1084 if (index.MissingChunks() > 0) {
1091 int ChunkStore::EstimateMissing() const noexcept {
1093 for (const ChunkIndex &index : indices) {
1094 missing += index.MissingChunks();
1099 ExactLocation::Coarse ChunkStore::NextMissing() noexcept {
1100 for (ChunkIndex &index : indices) {
1101 if (index.MissingChunks()) {
1102 return index.NextMissing();
1105 return ExactLocation::Coarse(0, 0, 0);
1108 void ChunkStore::Clean() {
1109 for (auto i = loaded.begin(), end = loaded.end(); i != end;) {
1110 if (i->Referenced() || i->ShouldUpdateSave()) {
1115 free.splice(free.end(), loaded, chunk);
1117 chunk->InvalidateMesh();