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 "../graphics/BlockLighting.hpp"
12 #include "../graphics/BlockMesh.hpp"
13 #include "../graphics/Viewport.hpp"
14 #include "../io/WorldSave.hpp"
24 constexpr int Chunk::width;
25 constexpr int Chunk::height;
26 constexpr int Chunk::depth;
27 constexpr int Chunk::size;
30 Chunk::Chunk(const BlockTypeRegistry &types) noexcept
44 Chunk::Chunk(Chunk &&other) noexcept
46 , generated(other.generated)
47 , lighted(other.lighted)
48 , position(other.position)
49 , ref_count(other.ref_count)
50 , dirty_mesh(other.dirty_mesh)
51 , dirty_save(other.dirty_save) {
52 std::copy(other.neighbor, other.neighbor + sizeof(neighbor), neighbor);
53 std::copy(other.blocks, other.blocks + sizeof(blocks), blocks);
54 std::copy(other.light, other.light + sizeof(light), light);
58 Chunk &Chunk::operator =(Chunk &&other) noexcept {
60 std::copy(other.neighbor, other.neighbor + sizeof(neighbor), neighbor);
61 std::copy(other.blocks, other.blocks + sizeof(blocks), blocks);
62 std::copy(other.light, other.light + sizeof(light), light);
63 generated = other.generated;
64 lighted = other.lighted;
65 position = other.position;
66 std::swap(ref_count, other.ref_count);
67 dirty_mesh = other.dirty_save;
68 dirty_save = other.dirty_save;
80 SetNode(Chunk *chunk, Chunk::Pos pos)
81 : chunk(chunk), pos(pos) { }
83 int Get() const noexcept { return chunk->GetLight(pos); }
84 void Set(int level) noexcept { chunk->SetLight(pos, level); }
86 const BlockType &GetType() const noexcept { return chunk->Type(Chunk::ToIndex(pos)); }
88 bool HasNext(Block::Face face) noexcept {
89 const BlockType &type = GetType();
90 if (type.block_light && !type.luminosity) return false;
91 const BlockLookup next(chunk, pos, face);
94 SetNode GetNext(Block::Face face) noexcept {
95 const BlockLookup next(chunk, pos, face);
96 return SetNode(&next.GetChunk(), next.GetBlockPos());
106 UnsetNode(Chunk *chunk, Chunk::Pos pos)
107 : SetNode(chunk, pos), level(Get()) { }
109 UnsetNode(const SetNode &set)
110 : SetNode(set), level(Get()) { }
113 bool HasNext(Block::Face face) noexcept {
114 const BlockLookup next(chunk, pos, face);
117 UnsetNode GetNext(Block::Face face) noexcept { return UnsetNode(SetNode::GetNext(face)); }
121 std::queue<SetNode> light_queue;
122 std::queue<UnsetNode> dark_queue;
124 void work_light() noexcept {
125 while (!light_queue.empty()) {
126 SetNode node = light_queue.front();
129 int level = node.Get() - 1;
130 for (int face = 0; face < Block::FACE_COUNT; ++face) {
131 if (node.HasNext(Block::Face(face))) {
132 SetNode other = node.GetNext(Block::Face(face));
133 if (other.Get() < level) {
135 light_queue.emplace(other);
142 void work_dark() noexcept {
143 while (!dark_queue.empty()) {
144 UnsetNode node = dark_queue.front();
147 for (int face = 0; face < Block::FACE_COUNT; ++face) {
148 if (node.HasNext(Block::Face(face))) {
149 UnsetNode other = node.GetNext(Block::Face(face));
150 // TODO: if there a light source here with the same level this will err
151 if (other.Get() != 0 && other.Get() < node.level) {
153 dark_queue.emplace(other);
155 light_queue.emplace(other);
164 void Chunk::SetBlock(int index, const Block &block) noexcept {
165 const BlockType &old_type = Type(blocks[index]);
166 const BlockType &new_type = Type(block);
168 blocks[index] = block;
171 if (!lighted || &old_type == &new_type) return;
173 if (new_type.luminosity > old_type.luminosity) {
175 SetLight(index, new_type.luminosity);
176 light_queue.emplace(this, ToPos(index));
178 } else if (new_type.luminosity < old_type.luminosity) {
180 dark_queue.emplace(this, ToPos(index));
183 SetLight(index, new_type.luminosity);
184 light_queue.emplace(this, ToPos(index));
186 } else if (new_type.block_light && !old_type.block_light) {
188 if (GetLight(index) > 0) {
189 dark_queue.emplace(this, ToPos(index));
194 } else if (!new_type.block_light && old_type.block_light) {
197 Pos pos(ToPos(index));
198 for (int face = 0; face < Block::FACE_COUNT; ++face) {
199 BlockLookup next_block(this, pos, Block::Face(face));
201 level = std::max(level, next_block.GetLight());
205 SetLight(index, level - 1);
206 light_queue.emplace(this, pos);
212 void Chunk::ScanLights() {
215 for (; pos.z < depth; ++pos.z) {
216 for (pos.y = 0; pos.y < height; ++pos.y) {
217 for (pos.x = 0; pos.x < width; ++pos.x, ++idx) {
218 const BlockType &type = Type(blocks[idx]);
219 if (type.luminosity) {
220 SetLight(idx, type.luminosity);
221 light_queue.emplace(this, pos);
230 void Chunk::SetNeighbor(Block::Face face, Chunk &other) noexcept {
231 neighbor[face] = &other;
232 other.neighbor[Block::Opposite(face)] = this;
235 void Chunk::Unlink() noexcept {
236 for (int face = 0; face < Block::FACE_COUNT; ++face) {
237 if (neighbor[face]) {
238 neighbor[face]->neighbor[Block::Opposite(Block::Face(face))] = nullptr;
239 neighbor[face] = nullptr;
245 void Chunk::SetLight(int index, int level) noexcept {
246 if (light[index] != level) {
247 light[index] = level;
252 int Chunk::GetLight(int index) const noexcept {
256 float Chunk::GetVertexLight(const Pos &pos, const BlockMesh::Position &vtx, const EntityMesh::Normal &norm) const noexcept {
257 int index = ToIndex(pos);
258 float light = GetLight(index);
260 Block::Face direct_face(Block::NormalFace(norm));
262 BlockLookup direct(const_cast<Chunk *>(this), pos, Block::NormalFace(norm));
264 float direct_light = direct.GetLight();
265 if (direct_light > light) {
266 light = direct_light;
272 if (Type(BlockAt(index)).luminosity > 0 || direct.GetType().block_light) {
277 switch (Block::Axis(direct_face)) {
279 edge[0] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
280 edge[1] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
283 edge[0] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
284 edge[1] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
287 edge[0] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
288 edge[1] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
295 BlockLookup next[2] = {
296 direct.Next(edge[0]),
297 direct.Next(edge[1]),
301 if (next[0].GetType().block_light) {
304 light += next[0].GetLight();
309 if (next[1].GetType().block_light) {
312 light += next[1].GetLight();
318 BlockLookup corner = next[0].Next(edge[1]);
320 if (corner.GetType().block_light) {
323 light += corner.GetLight();
327 } else if (next[1]) {
328 BlockLookup corner = next[1].Next(edge[0]);
330 if (corner.GetType().block_light) {
333 light += corner.GetLight();
342 return (light / num) - (occlusion * 0.8f);
346 bool Chunk::IsSurface(const Pos &pos) const noexcept {
347 const Block &block = BlockAt(pos);
348 if (!Type(block).visible) {
351 for (int face = 0; face < Block::FACE_COUNT; ++face) {
352 BlockLookup next = BlockLookup(const_cast<Chunk *>(this), pos, Block::Face(face));
353 if (!next || !next.GetType().visible) {
361 bool Chunk::Intersection(
369 coll.depth = std::numeric_limits<float>::infinity();
370 for (int z = 0; z < depth; ++z) {
371 for (int y = 0; y < height; ++y) {
372 for (int x = 0; x < width; ++x, ++idx) {
373 const BlockType &type = Type(idx);
374 if (!type.collision || !type.shape) {
379 if (type.shape->Intersects(ray, M * ToTransform(Pos(x, y, z), idx), cur_dist, cur_norm)) {
380 if (cur_dist < coll.depth) {
382 coll.depth = cur_dist;
383 coll.normal = cur_norm;
390 if (coll.block < 0) {
393 coll.normal = glm::vec3(BlockAt(coll.block).Transform() * glm::vec4(coll.normal, 0.0f));
398 bool Chunk::Intersection(
400 const glm::mat4 &Mbox,
401 const glm::mat4 &Mchunk,
402 std::vector<WorldCollision> &col
408 if (!blank::Intersection(box, Mbox, Bounds(), Mchunk, penetration, normal)) {
411 for (int idx = 0, z = 0; z < depth; ++z) {
412 for (int y = 0; y < height; ++y) {
413 for (int x = 0; x < width; ++x, ++idx) {
414 const BlockType &type = Type(idx);
415 if (!type.collision || !type.shape) {
418 if (type.shape->Intersects(Mchunk * ToTransform(Pos(x, y, z), idx), box, Mbox, penetration, normal)) {
419 col.emplace_back(this, idx, penetration, normal);
431 BlockMesh::Buffer buf;
435 void Chunk::Update(BlockMesh &model) noexcept {
436 int vtx_count = 0, idx_count = 0;
437 for (const auto &block : blocks) {
438 const BlockType &type = Type(block);
439 if (type.visible && type.shape) {
440 vtx_count += type.shape->VertexCount();
441 idx_count += type.shape->IndexCount();
445 buf.Reserve(vtx_count, idx_count);
449 BlockMesh::Index vtx_counter = 0;
450 for (size_t z = 0; z < depth; ++z) {
451 for (size_t y = 0; y < height; ++y) {
452 for (size_t x = 0; x < width; ++x, ++idx) {
453 const BlockType &type = Type(BlockAt(idx));
454 const Pos pos(x, y, z);
456 if (!type.visible || !type.shape || Obstructed(pos).All()) continue;
458 type.FillBlockMesh(buf, ToTransform(pos, idx), vtx_counter);
459 size_t vtx_begin = vtx_counter;
460 vtx_counter += type.shape->VertexCount();
462 for (size_t vtx = vtx_begin; vtx < vtx_counter; ++vtx) {
463 buf.lights.emplace_back(GetVertexLight(
466 type.shape->VertexNormal(vtx - vtx_begin, BlockAt(idx).Transform())
478 Block::FaceSet Chunk::Obstructed(const Pos &pos) const noexcept {
479 Block::FaceSet result;
481 for (int f = 0; f < Block::FACE_COUNT; ++f) {
482 Block::Face face = Block::Face(f);
483 BlockLookup next(const_cast<Chunk *>(this), pos, face);
484 if (next && next.GetType().FaceFilled(next.GetBlock(), Block::Opposite(face))) {
492 glm::mat4 Chunk::ToTransform(const Pos &pos, int idx) const noexcept {
493 return glm::translate(ToCoords(pos)) * BlockAt(idx).Transform();
497 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p) noexcept
499 while (pos.x >= Chunk::width) {
500 if (chunk->HasNeighbor(Block::FACE_RIGHT)) {
501 chunk = &chunk->GetNeighbor(Block::FACE_RIGHT);
502 pos.x -= Chunk::width;
509 if (chunk->HasNeighbor(Block::FACE_LEFT)) {
510 chunk = &chunk->GetNeighbor(Block::FACE_LEFT);
511 pos.x += Chunk::width;
517 while (pos.y >= Chunk::height) {
518 if (chunk->HasNeighbor(Block::FACE_UP)) {
519 chunk = &chunk->GetNeighbor(Block::FACE_UP);
520 pos.y -= Chunk::height;
527 if (chunk->HasNeighbor(Block::FACE_DOWN)) {
528 chunk = &chunk->GetNeighbor(Block::FACE_DOWN);
529 pos.y += Chunk::height;
535 while (pos.z >= Chunk::depth) {
536 if (chunk->HasNeighbor(Block::FACE_FRONT)) {
537 chunk = &chunk->GetNeighbor(Block::FACE_FRONT);
538 pos.z -= Chunk::depth;
545 if (chunk->HasNeighbor(Block::FACE_BACK)) {
546 chunk = &chunk->GetNeighbor(Block::FACE_BACK);
547 pos.z += Chunk::depth;
555 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face face) noexcept
557 pos += Block::FaceNormal(face);
558 if (!Chunk::InBounds(pos)) {
559 pos -= Block::FaceNormal(face) * Chunk::Extent();
560 chunk = &chunk->GetNeighbor(face);
565 ChunkLoader::ChunkLoader(
567 const Generator &gen,
568 const WorldSave &save
576 void ChunkLoader::Update(int dt) {
577 // check if there's chunks waiting to be loaded
578 // load until one of load or generation limits was hit
579 constexpr int max_load = 10;
580 constexpr int max_gen = 1;
583 while (loaded < max_load && generated < max_gen && store.HasMissing()) {
591 // store a few chunks as well
592 constexpr int max_save = 10;
594 for (Chunk &chunk : store) {
595 if (chunk.ShouldUpdateSave()) {
598 if (saved >= max_save) {
605 int ChunkLoader::ToLoad() const noexcept {
606 return store.EstimateMissing();
609 bool ChunkLoader::LoadOne() {
610 if (!store.HasMissing()) return false;
612 Chunk::Pos pos = store.NextMissing();
613 Chunk *chunk = store.Allocate(pos);
615 // chunk store corrupted?
619 bool generated = false;
620 if (save.Exists(pos)) {
627 ChunkIndex *index = store.ClosestIndex(pos);
632 Chunk::Pos begin(pos - Chunk::Pos(1));
633 Chunk::Pos end(pos + Chunk::Pos(2));
634 for (Chunk::Pos iter(begin); iter.z < end.z; ++iter.z) {
635 for (iter.y = begin.y; iter.y < end.y; ++iter.y) {
636 for (iter.x = begin.x; iter.x < end.x; ++iter.x) {
637 if (index->IsBorder(iter)) continue;
638 Chunk *light_chunk = index->Get(iter);
639 if (!light_chunk) continue;
640 if (index->HasAllSurrounding(iter)) {
641 if (!light_chunk->Lighted()) {
642 light_chunk->ScanLights();
644 light_chunk->InvalidateMesh();
654 void ChunkLoader::LoadN(std::size_t n) {
655 std::size_t end = std::min(n, std::size_t(ToLoad()));
656 for (std::size_t i = 0; i < end && store.HasMissing(); ++i) {
662 ChunkRenderer::ChunkRenderer(ChunkIndex &index)
664 , models(index.TotalChunks())
666 , fog_density(0.0f) {
670 ChunkRenderer::~ChunkRenderer() {
674 int ChunkRenderer::MissingChunks() const noexcept {
675 return index.MissingChunks();
678 void ChunkRenderer::LoadTextures(const AssetLoader &loader, const ResourceIndex &tex_index) {
680 loader.LoadTextures(tex_index, block_tex);
681 block_tex.FilterNearest();
684 void ChunkRenderer::Update(int dt) {
685 for (int i = 0, updates = 0; updates < dt && i < index.TotalChunks(); ++i) {
686 if (!index[i]) continue;
687 if (!index[i]->Lighted() && index.HasAllSurrounding(index[i]->Position())) {
688 index[i]->ScanLights();
690 if (index[i]->ShouldUpdateMesh()) {
691 index[i]->Update(models[i]);
697 void ChunkRenderer::Render(Viewport &viewport) {
698 BlockLighting &chunk_prog = viewport.ChunkProgram();
699 chunk_prog.SetTexture(block_tex);
700 chunk_prog.SetFogDensity(fog_density);
702 for (int i = 0; i < index.TotalChunks(); ++i) {
703 if (!index[i]) continue;
704 glm::mat4 m(index[i]->Transform(index.Base()));
705 glm::mat4 mvp(chunk_prog.GetVP() * m);
706 if (!CullTest(Chunk::Bounds(), mvp)) {
707 if (index[i]->ShouldUpdateMesh()) {
708 index[i]->Update(models[i]);
717 ChunkIndex::ChunkIndex(ChunkStore &store, const Chunk::Pos &base, int extent)
721 , side_length(2 * extent + 1)
722 , total_length(side_length * side_length * side_length)
725 , stride(1, side_length, side_length * side_length)
726 , chunks(total_length, nullptr) {
730 ChunkIndex::~ChunkIndex() {
734 bool ChunkIndex::InRange(const Chunk::Pos &pos) const noexcept {
735 return Distance(pos) <= extent;
738 bool ChunkIndex::IsBorder(const Chunk::Pos &pos) const noexcept {
739 return Distance(pos) == extent;
742 int ChunkIndex::Distance(const Chunk::Pos &pos) const noexcept {
743 return manhattan_radius(pos - base);
746 bool ChunkIndex::HasAllSurrounding(const Chunk::Pos &pos) const noexcept {
747 Chunk::Pos begin(pos - Chunk::Pos(1));
748 Chunk::Pos end(pos + Chunk::Pos(2));
749 for (Chunk::Pos iter(begin); iter.z < end.z; ++iter.z) {
750 for (iter.y = begin.y; iter.y < end.y; ++iter.y) {
751 for (iter.x = begin.x; iter.x < end.x; ++iter.x) {
752 if (!Get(iter)) return false;
759 int ChunkIndex::IndexOf(const Chunk::Pos &pos) const noexcept {
765 return mod_pos.x * stride.x
766 + mod_pos.y * stride.y
767 + mod_pos.z * stride.z;
770 Chunk::Pos ChunkIndex::PositionOf(int i) const noexcept {
772 (i / stride.x) % side_length,
773 (i / stride.y) % side_length,
774 (i / stride.z) % side_length
776 Chunk::Pos zero_base(
781 Chunk::Pos base_relative(zero_pos - zero_base);
782 if (base_relative.x > extent) base_relative.x -= side_length;
783 else if (base_relative.x < -extent) base_relative.x += side_length;
784 if (base_relative.y > extent) base_relative.y -= side_length;
785 else if (base_relative.y < -extent) base_relative.y += side_length;
786 if (base_relative.z > extent) base_relative.z -= side_length;
787 else if (base_relative.z < -extent) base_relative.z += side_length;
788 return base + base_relative;
791 Chunk *ChunkIndex::Get(const Chunk::Pos &pos) noexcept {
793 return chunks[IndexOf(pos)];
799 const Chunk *ChunkIndex::Get(const Chunk::Pos &pos) const noexcept {
801 return chunks[IndexOf(pos)];
807 void ChunkIndex::Rebase(const Chunk::Pos &new_base) {
808 if (new_base == base) return;
810 Chunk::Pos diff(new_base - base);
812 if (manhattan_radius(diff) > extent) {
813 // that's more than half, so probably not worth shifting
822 Shift(Block::FACE_RIGHT);
826 Shift(Block::FACE_LEFT);
830 Shift(Block::FACE_UP);
834 Shift(Block::FACE_DOWN);
838 Shift(Block::FACE_FRONT);
842 Shift(Block::FACE_BACK);
848 int ChunkIndex::GetCol(int c) const noexcept {
850 if (c < 0) c += side_length;
854 void ChunkIndex::Shift(Block::Face f) {
855 int a_axis = Block::Axis(f);
856 int b_axis = (a_axis + 1) % 3;
857 int c_axis = (a_axis + 2) % 3;
858 int dir = Block::Direction(f);
860 int a = GetCol(base[a_axis] + (extent * dir));
861 int a_stride = a * stride[a_axis];
862 for (int b = 0; b < side_length; ++b) {
863 int b_stride = b * stride[b_axis];
864 for (int c = 0; c < side_length; ++c) {
865 int bc_stride = b_stride + c * stride[c_axis];
866 int index = a_stride + bc_stride;
868 int neighbor = ((a - dir + side_length) % side_length) * stride[a_axis] + bc_stride;
869 if (chunks[neighbor] && chunks[neighbor]->HasNeighbor(f)) {
870 Set(index, chunks[neighbor]->GetNeighbor(f));
876 void ChunkIndex::Clear() noexcept {
877 for (int i = 0; i < total_length && total_indexed > 0; ++i) {
882 void ChunkIndex::Scan() noexcept {
883 for (Chunk &chunk : store) {
888 void ChunkIndex::Register(Chunk &chunk) noexcept {
889 if (InRange(chunk.Position())) {
890 Set(IndexOf(chunk.Position()), chunk);
894 void ChunkIndex::Set(int index, Chunk &chunk) noexcept {
896 chunks[index] = &chunk;
901 void ChunkIndex::Unset(int index) noexcept {
903 chunks[index]->UnRef();
904 chunks[index] = nullptr;
909 Chunk::Pos ChunkIndex::NextMissing() noexcept {
910 if (MissingChunks() > 0) {
911 int roundtrip = last_missing;
912 last_missing = (last_missing + 1) % total_length;
913 while (chunks[last_missing]) {
914 last_missing = (last_missing + 1) % total_length;
915 if (last_missing == roundtrip) {
920 return PositionOf(last_missing);
924 ChunkStore::ChunkStore(const BlockTypeRegistry &types)
932 ChunkStore::~ChunkStore() {
936 ChunkIndex &ChunkStore::MakeIndex(const Chunk::Pos &pos, int extent) {
937 indices.emplace_back(*this, pos, extent);
938 return indices.back();
941 void ChunkStore::UnregisterIndex(ChunkIndex &index) {
942 for (auto i = indices.begin(), end = indices.end(); i != end; ++i) {
952 ChunkIndex *ChunkStore::ClosestIndex(const Chunk::Pos &pos) {
953 ChunkIndex *closest_index = nullptr;
954 int closest_distance = std::numeric_limits<int>::max();
956 for (ChunkIndex &index : indices) {
957 int distance = index.Distance(pos);
958 if (distance < closest_distance) {
959 closest_index = &index;
960 closest_distance = distance;
964 return closest_index;
967 Chunk *ChunkStore::Get(const Chunk::Pos &pos) {
968 for (ChunkIndex &index : indices) {
969 Chunk *chunk = index.Get(pos);
977 Chunk *ChunkStore::Allocate(const Chunk::Pos &pos) {
978 Chunk *chunk = Get(pos);
983 loaded.emplace(loaded.begin(), types);
985 loaded.splice(loaded.begin(), free, free.begin());
986 loaded.front().Unlink();
988 chunk = &loaded.front();
989 chunk->Position(pos);
990 for (ChunkIndex &index : indices) {
991 if (index.InRange(pos)) {
992 index.Register(*chunk);
995 for (int i = 0; i < Block::FACE_COUNT; ++i) {
996 Block::Face face = Block::Face(i);
997 Chunk::Pos neighbor_pos(pos + Block::FaceNormal(face));
998 Chunk *neighbor = Get(neighbor_pos);
1000 chunk->SetNeighbor(face, *neighbor);
1006 bool ChunkStore::HasMissing() const noexcept {
1007 for (const ChunkIndex &index : indices) {
1008 if (index.MissingChunks() > 0) {
1015 int ChunkStore::EstimateMissing() const noexcept {
1017 for (const ChunkIndex &index : indices) {
1018 missing += index.MissingChunks();
1023 Chunk::Pos ChunkStore::NextMissing() noexcept {
1024 for (ChunkIndex &index : indices) {
1025 if (index.MissingChunks()) {
1026 return index.NextMissing();
1029 return Chunk::Pos(0, 0, 0);
1032 void ChunkStore::Clean() {
1033 for (auto i = loaded.begin(), end = loaded.end(); i != end;) {
1034 if (i->Referenced() || i->ShouldUpdateSave()) {
1039 free.splice(free.end(), loaded, chunk);
1041 chunk->InvalidateMesh();