1 #include "BlockLookup.hpp"
3 #include "ChunkLoader.hpp"
5 #include "Generator.hpp"
6 #include "WorldCollision.hpp"
16 constexpr int Chunk::width;
17 constexpr int Chunk::height;
18 constexpr int Chunk::depth;
19 constexpr int Chunk::size;
22 Chunk::Chunk(const BlockTypeRegistry &types) noexcept
33 Chunk::Chunk(Chunk &&other) noexcept
35 , model(std::move(other.model))
36 , position(other.position)
37 , dirty(other.dirty) {
38 std::copy(other.neighbor, other.neighbor + sizeof(neighbor), neighbor);
39 std::copy(other.blocks, other.blocks + sizeof(blocks), blocks);
40 std::copy(other.light, other.light + sizeof(light), light);
43 Chunk &Chunk::operator =(Chunk &&other) noexcept {
45 std::copy(other.neighbor, other.neighbor + sizeof(neighbor), neighbor);
46 std::copy(other.blocks, other.blocks + sizeof(blocks), blocks);
47 std::copy(other.light, other.light + sizeof(light), light);
48 model = std::move(other.model);
49 position = other.position;
62 SetNode(Chunk *chunk, Chunk::Pos pos)
63 : chunk(chunk), pos(pos) { }
65 int Get() const noexcept { return chunk->GetLight(pos); }
66 void Set(int level) noexcept { chunk->SetLight(pos, level); }
68 const BlockType &GetType() const noexcept { return chunk->Type(Chunk::ToIndex(pos)); }
70 bool HasNext(Block::Face face) noexcept {
71 const BlockType &type = GetType();
72 if (type.block_light && !type.luminosity) return false;
73 const BlockLookup next(chunk, pos, face);
76 SetNode GetNext(Block::Face face) noexcept {
77 const BlockLookup next(chunk, pos, face);
78 return SetNode(&next.GetChunk(), next.GetBlockPos());
88 UnsetNode(Chunk *chunk, Chunk::Pos pos)
89 : SetNode(chunk, pos), level(Get()) { }
91 UnsetNode(const SetNode &set)
92 : SetNode(set), level(Get()) { }
95 bool HasNext(Block::Face face) noexcept {
96 const BlockLookup next(chunk, pos, face);
99 UnsetNode GetNext(Block::Face face) noexcept { return UnsetNode(SetNode::GetNext(face)); }
103 std::queue<SetNode> light_queue;
104 std::queue<UnsetNode> dark_queue;
106 void work_light() noexcept {
107 while (!light_queue.empty()) {
108 SetNode node = light_queue.front();
111 int level = node.Get() - 1;
112 for (int face = 0; face < Block::FACE_COUNT; ++face) {
113 if (node.HasNext(Block::Face(face))) {
114 SetNode other = node.GetNext(Block::Face(face));
115 if (other.Get() < level) {
117 light_queue.emplace(other);
124 void work_dark() noexcept {
125 while (!dark_queue.empty()) {
126 UnsetNode node = dark_queue.front();
129 for (int face = 0; face < Block::FACE_COUNT; ++face) {
130 if (node.HasNext(Block::Face(face))) {
131 UnsetNode other = node.GetNext(Block::Face(face));
132 // TODO: if there a light source here with the same level this will err
133 if (other.Get() != 0 && other.Get() < node.level) {
135 dark_queue.emplace(other);
137 light_queue.emplace(other);
146 void Chunk::SetBlock(int index, const Block &block) noexcept {
147 const BlockType &old_type = Type(blocks[index]);
148 const BlockType &new_type = Type(block);
150 blocks[index] = block;
152 if (&old_type == &new_type) return;
154 if (new_type.luminosity > old_type.luminosity) {
156 SetLight(index, new_type.luminosity);
157 light_queue.emplace(this, ToPos(index));
159 } else if (new_type.luminosity < old_type.luminosity) {
161 dark_queue.emplace(this, ToPos(index));
164 SetLight(index, new_type.luminosity);
165 light_queue.emplace(this, ToPos(index));
167 } else if (new_type.block_light && !old_type.block_light) {
169 if (GetLight(index) > 0) {
170 dark_queue.emplace(this, ToPos(index));
175 } else if (!new_type.block_light && old_type.block_light) {
178 Pos pos(ToPos(index));
179 for (int face = 0; face < Block::FACE_COUNT; ++face) {
180 BlockLookup next_block(this, pos, Block::Face(face));
182 level = std::max(level, next_block.GetLight());
186 SetLight(index, level - 1);
187 light_queue.emplace(this, pos);
195 // propagate light from a's edge to b
197 Chunk &a, const Chunk::Pos &a_pos,
198 Chunk &b, const Chunk::Pos &b_pos
200 if (a.GetLight(a_pos) > 1) {
201 const BlockType &b_type = b.Type(Chunk::ToIndex(b_pos));
202 if (!b_type.block_light) {
203 light_queue.emplace(&a, a_pos);
205 if (b_type.visible) {
213 void Chunk::SetNeighbor(Chunk &other) noexcept {
214 if (other.position == position + Pos(-1, 0, 0)) {
215 if (neighbor[Block::FACE_LEFT] != &other) {
216 neighbor[Block::FACE_LEFT] = &other;
217 other.neighbor[Block::FACE_RIGHT] = this;
218 for (int z = 0; z < depth; ++z) {
219 for (int y = 0; y < height; ++y) {
221 Pos other_pos(width - 1, y, z);
222 edge_light(*this, my_pos, other, other_pos);
223 edge_light(other, other_pos, *this, my_pos);
228 } else if (other.position == position + Pos(1, 0, 0)) {
229 if (neighbor[Block::FACE_RIGHT] != &other) {
230 neighbor[Block::FACE_RIGHT] = &other;
231 other.neighbor[Block::FACE_LEFT] = this;
232 for (int z = 0; z < depth; ++z) {
233 for (int y = 0; y < height; ++y) {
234 Pos my_pos(width - 1, y, z);
235 Pos other_pos(0, y, z);
236 edge_light(*this, my_pos, other, other_pos);
237 edge_light(other, other_pos, *this, my_pos);
242 } else if (other.position == position + Pos(0, -1, 0)) {
243 if (neighbor[Block::FACE_DOWN] != &other) {
244 neighbor[Block::FACE_DOWN] = &other;
245 other.neighbor[Block::FACE_UP] = this;
246 for (int z = 0; z < depth; ++z) {
247 for (int x = 0; x < width; ++x) {
249 Pos other_pos(x, height - 1, z);
250 edge_light(*this, my_pos, other, other_pos);
251 edge_light(other, other_pos, *this, my_pos);
256 } else if (other.position == position + Pos(0, 1, 0)) {
257 if (neighbor[Block::FACE_UP] != &other) {
258 neighbor[Block::FACE_UP] = &other;
259 other.neighbor[Block::FACE_DOWN] = this;
260 for (int z = 0; z < depth; ++z) {
261 for (int x = 0; x < width; ++x) {
262 Pos my_pos(x, height - 1, z);
263 Pos other_pos(x, 0, z);
264 edge_light(*this, my_pos, other, other_pos);
265 edge_light(other, other_pos, *this, my_pos);
270 } else if (other.position == position + Pos(0, 0, -1)) {
271 if (neighbor[Block::FACE_BACK] != &other) {
272 neighbor[Block::FACE_BACK] = &other;
273 other.neighbor[Block::FACE_FRONT] = this;
274 for (int y = 0; y < height; ++y) {
275 for (int x = 0; x < width; ++x) {
277 Pos other_pos(x, y, depth - 1);
278 edge_light(*this, my_pos, other, other_pos);
279 edge_light(other, other_pos, *this, my_pos);
284 } else if (other.position == position + Pos(0, 0, 1)) {
285 if (neighbor[Block::FACE_FRONT] != &other) {
286 neighbor[Block::FACE_FRONT] = &other;
287 other.neighbor[Block::FACE_BACK] = this;
288 for (int y = 0; y < height; ++y) {
289 for (int x = 0; x < width; ++x) {
290 Pos my_pos(x, y, depth - 1);
291 Pos other_pos(x, y, 0);
292 edge_light(*this, my_pos, other, other_pos);
293 edge_light(other, other_pos, *this, my_pos);
301 void Chunk::ClearNeighbors() noexcept {
302 for (int face = 0; face < Block::FACE_COUNT; ++face) {
303 if (neighbor[face]) {
304 neighbor[face]->neighbor[Block::Opposite(Block::Face(face))] = nullptr;
305 neighbor[face] = nullptr;
311 void Chunk::SetLight(int index, int level) noexcept {
312 if (light[index] != level) {
313 light[index] = level;
318 int Chunk::GetLight(int index) const noexcept {
322 float Chunk::GetVertexLight(const Pos &pos, const BlockModel::Position &vtx, const EntityModel::Normal &norm) const noexcept {
323 int index = ToIndex(pos);
324 float light = GetLight(index);
326 Block::Face direct_face(Block::NormalFace(norm));
328 BlockLookup direct(const_cast<Chunk *>(this), pos, Block::NormalFace(norm));
330 float direct_light = direct.GetLight();
331 if (direct_light > light) {
332 light = direct_light;
338 if (Type(BlockAt(index)).luminosity > 0 || direct.GetType().block_light) {
343 switch (Block::Axis(direct_face)) {
345 edge[0] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
346 edge[1] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
349 edge[0] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
350 edge[1] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
353 edge[0] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
354 edge[1] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
361 BlockLookup next[2] = {
362 direct.Next(edge[0]),
363 direct.Next(edge[1]),
367 if (next[0].GetType().block_light) {
370 light += next[0].GetLight();
375 if (next[1].GetType().block_light) {
378 light += next[1].GetLight();
384 BlockLookup corner = next[0].Next(edge[1]);
386 if (corner.GetType().block_light) {
389 light += corner.GetLight();
393 } else if (next[1]) {
394 BlockLookup corner = next[1].Next(edge[0]);
396 if (corner.GetType().block_light) {
399 light += corner.GetLight();
408 return (light / num) - (occlusion * 0.8f);
412 bool Chunk::IsSurface(const Pos &pos) const noexcept {
413 const Block &block = BlockAt(pos);
414 if (!Type(block).visible) {
417 for (int face = 0; face < Block::FACE_COUNT; ++face) {
418 BlockLookup next = BlockLookup(const_cast<Chunk *>(this), pos, Block::Face(face));
419 if (!next || !next.GetType().visible) {
427 void Chunk::Draw() noexcept {
435 bool Chunk::Intersection(
444 dist = std::numeric_limits<float>::infinity();
445 for (int z = 0; z < depth; ++z) {
446 for (int y = 0; y < height; ++y) {
447 for (int x = 0; x < width; ++x, ++idx) {
448 const BlockType &type = Type(idx);
454 if (type.shape->Intersects(ray, M * ToTransform(Pos(x, y, z), idx), cur_dist, cur_norm)) {
455 if (cur_dist < dist) {
468 normal = glm::vec3(BlockAt(blkid).Transform() * glm::vec4(normal, 0.0f));
473 bool Chunk::Intersection(
475 const glm::mat4 &Mbox,
476 const glm::mat4 &Mchunk,
477 std::vector<WorldCollision> &col
483 if (!blank::Intersection(box, Mbox, Bounds(), Mchunk, penetration, normal)) {
486 for (int idx = 0, z = 0; z < depth; ++z) {
487 for (int y = 0; y < height; ++y) {
488 for (int x = 0; x < width; ++x, ++idx) {
489 const BlockType &type = Type(idx);
490 if (!type.collision) {
493 if (type.shape->Intersects(Mchunk * ToTransform(Pos(x, y, z), idx), box, Mbox, penetration, normal)) {
494 col.emplace_back(this, idx, penetration, normal);
506 BlockModel::Buffer buf;
510 void Chunk::CheckUpdate() noexcept {
516 void Chunk::Update() noexcept {
517 int vtx_count = 0, idx_count = 0;
518 for (const auto &block : blocks) {
519 const Shape *shape = Type(block).shape;
520 vtx_count += shape->VertexCount();
521 idx_count += shape->VertexIndexCount();
524 buf.Reserve(vtx_count, idx_count);
527 BlockModel::Index vtx_counter = 0;
528 for (size_t z = 0; z < depth; ++z) {
529 for (size_t y = 0; y < height; ++y) {
530 for (size_t x = 0; x < width; ++x, ++idx) {
531 const BlockType &type = Type(BlockAt(idx));
532 const Pos pos(x, y, z);
534 if (!type.visible || Obstructed(pos).All()) continue;
536 type.FillBlockModel(buf, ToTransform(pos, idx), vtx_counter);
537 size_t vtx_begin = vtx_counter;
538 vtx_counter += type.shape->VertexCount();
540 for (size_t vtx = vtx_begin; vtx < vtx_counter; ++vtx) {
541 buf.lights.emplace_back(GetVertexLight(
544 type.shape->VertexNormal(vtx - vtx_begin, BlockAt(idx).Transform())
555 Block::FaceSet Chunk::Obstructed(const Pos &pos) const noexcept {
556 Block::FaceSet result;
558 for (int f = 0; f < Block::FACE_COUNT; ++f) {
559 Block::Face face = Block::Face(f);
560 BlockLookup next(const_cast<Chunk *>(this), pos, face);
561 if (next && next.GetType().FaceFilled(next.GetBlock(), Block::Opposite(face))) {
569 glm::mat4 Chunk::ToTransform(const Pos &pos, int idx) const noexcept {
570 return glm::translate(ToCoords(pos)) * BlockAt(idx).Transform();
574 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p) noexcept
576 while (pos.x >= Chunk::width) {
577 if (chunk->HasNeighbor(Block::FACE_RIGHT)) {
578 chunk = &chunk->GetNeighbor(Block::FACE_RIGHT);
579 pos.x -= Chunk::width;
586 if (chunk->HasNeighbor(Block::FACE_LEFT)) {
587 chunk = &chunk->GetNeighbor(Block::FACE_LEFT);
588 pos.x += Chunk::width;
594 while (pos.y >= Chunk::height) {
595 if (chunk->HasNeighbor(Block::FACE_UP)) {
596 chunk = &chunk->GetNeighbor(Block::FACE_UP);
597 pos.y -= Chunk::height;
604 if (chunk->HasNeighbor(Block::FACE_DOWN)) {
605 chunk = &chunk->GetNeighbor(Block::FACE_DOWN);
606 pos.y += Chunk::height;
612 while (pos.z >= Chunk::depth) {
613 if (chunk->HasNeighbor(Block::FACE_FRONT)) {
614 chunk = &chunk->GetNeighbor(Block::FACE_FRONT);
615 pos.z -= Chunk::depth;
622 if (chunk->HasNeighbor(Block::FACE_BACK)) {
623 chunk = &chunk->GetNeighbor(Block::FACE_BACK);
624 pos.z += Chunk::depth;
632 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face face) noexcept
634 pos += Block::FaceNormal(face);
635 if (!Chunk::InBounds(pos)) {
636 pos -= Block::FaceNormal(face) * Chunk::Extent();
637 chunk = &chunk->GetNeighbor(face);
642 ChunkLoader::ChunkLoader(const Config &config, const BlockTypeRegistry ®, const Generator &gen) noexcept
649 , gen_timer(config.gen_limit)
650 , load_dist(config.load_dist)
651 , unload_dist(config.unload_dist) {
659 explicit ChunkLess(const Chunk::Pos &base) noexcept
662 bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const noexcept {
663 Chunk::Pos da(base - a);
664 Chunk::Pos db(base - b);
666 da.x * da.x + da.y * da.y + da.z * da.z <
667 db.x * db.x + db.y * db.y + db.z * db.z;
676 void ChunkLoader::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
677 for (int z = from.z; z < to.z; ++z) {
678 for (int y = from.y; y < to.y; ++y) {
679 for (int x = from.x; x < to.x; ++x) {
680 Chunk::Pos pos(x, y, z);
683 } else if (pos == base) {
687 // for (int i = 0; i < 16; ++i) {
688 // for (int j = 0; j < 16; ++j) {
689 // loaded.back().SetBlock(Chunk::Pos{ i, j, 0 }, Block(1));
690 // loaded.back().SetBlock(Chunk::Pos{ i, j, 15 }, Block(1));
691 // loaded.back().SetBlock(Chunk::Pos{ 0, j, i }, Block(1));
692 // loaded.back().SetBlock(Chunk::Pos{ 15, j, i }, Block(1));
695 // loaded.back().SetBlock(Chunk::Pos{ 1, 0, 1 }, Block(13));
696 // loaded.back().SetBlock(Chunk::Pos{ 14, 0, 1 }, Block(13));
697 // loaded.back().SetBlock(Chunk::Pos{ 1, 0, 14 }, Block(13));
698 // loaded.back().SetBlock(Chunk::Pos{ 14, 0, 14 }, Block(13));
699 // loaded.back().SetBlock(Chunk::Pos{ 1, 15, 1 }, Block(13));
700 // loaded.back().SetBlock(Chunk::Pos{ 14, 15, 1 }, Block(13));
701 // loaded.back().SetBlock(Chunk::Pos{ 1, 15, 14 }, Block(13));
702 // loaded.back().SetBlock(Chunk::Pos{ 14, 15, 14 }, Block(13));
703 // loaded.back().SetBlock(Chunk::Pos{ 7, 7, 0 }, Block(13));
704 // loaded.back().SetBlock(Chunk::Pos{ 8, 7, 0 }, Block(13));
705 // loaded.back().SetBlock(Chunk::Pos{ 7, 8, 0 }, Block(13));
706 // loaded.back().SetBlock(Chunk::Pos{ 8, 8, 0 }, Block(13));
707 // loaded.back().SetBlock(Chunk::Pos{ 7, 7, 15 }, Block(13));
708 // loaded.back().SetBlock(Chunk::Pos{ 8, 7, 15 }, Block(13));
709 // loaded.back().SetBlock(Chunk::Pos{ 7, 8, 15 }, Block(13));
710 // loaded.back().SetBlock(Chunk::Pos{ 8, 8, 15 }, Block(13));
711 // loaded.back().SetBlock(Chunk::Pos{ 0, 7, 7 }, Block(13));
712 // loaded.back().SetBlock(Chunk::Pos{ 0, 7, 8 }, Block(13));
713 // loaded.back().SetBlock(Chunk::Pos{ 0, 8, 7 }, Block(13));
714 // loaded.back().SetBlock(Chunk::Pos{ 0, 8, 8 }, Block(13));
715 // loaded.back().SetBlock(Chunk::Pos{ 15, 7, 7 }, Block(13));
716 // loaded.back().SetBlock(Chunk::Pos{ 15, 7, 8 }, Block(13));
717 // loaded.back().SetBlock(Chunk::Pos{ 15, 8, 7 }, Block(13));
718 // loaded.back().SetBlock(Chunk::Pos{ 15, 8, 8 }, Block(13));
719 // loaded.back().Invalidate();
720 // loaded.back().CheckUpdate();
722 // orientation testing
723 // for (int i = 0; i < Block::FACE_COUNT; ++i) {
724 // for (int j = 0; j < Block::TURN_COUNT; ++j) {
725 // loaded.back().BlockAt(512 * j + 2 * i) = Block(3 * (j + 1), Block::Face(i), Block::Turn(j));
728 // loaded.back().Invalidate();
729 // loaded.back().CheckUpdate();
731 to_generate.emplace_back(pos);
736 to_generate.sort(ChunkLess(base));
739 Chunk &ChunkLoader::Generate(const Chunk::Pos &pos) {
740 loaded.emplace_back(reg);
741 Chunk &chunk = loaded.back();
748 void ChunkLoader::Insert(Chunk &chunk) noexcept {
749 for (Chunk &other : loaded) {
750 chunk.SetNeighbor(other);
754 std::list<Chunk>::iterator ChunkLoader::Remove(std::list<Chunk>::iterator chunk) noexcept {
755 // fetch next entry while chunk's still in the list
756 std::list<Chunk>::iterator next = chunk;
758 // unlink neighbors so they won't reference a dead chunk
759 chunk->ClearNeighbors();
760 // and move it from loaded to free list
761 to_free.splice(to_free.end(), loaded, chunk);
765 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) noexcept {
766 for (Chunk &chunk : loaded) {
767 if (chunk.Position() == pos) {
774 bool ChunkLoader::Queued(const Chunk::Pos &pos) noexcept {
775 for (const Chunk::Pos &chunk : to_generate) {
783 bool ChunkLoader::Known(const Chunk::Pos &pos) noexcept {
784 if (Loaded(pos)) return true;
788 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
789 Chunk *chunk = Loaded(pos);
794 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
796 to_generate.erase(iter);
801 return Generate(pos);
804 bool ChunkLoader::OutOfRange(const Chunk::Pos &pos) const noexcept {
805 return std::abs(base.x - pos.x) > unload_dist
806 || std::abs(base.y - pos.y) > unload_dist
807 || std::abs(base.z - pos.z) > unload_dist;
810 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
811 if (new_base == base) {
816 // unload far away chunks
817 for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
818 if (OutOfRange(*iter)) {
824 // abort far away queued chunks
825 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
826 if (OutOfRange(*iter)) {
827 iter = to_generate.erase(iter);
832 // add missing new chunks
833 GenerateSurrounding(base);
836 void ChunkLoader::GenerateSurrounding(const Chunk::Pos &pos) {
837 const Chunk::Pos offset(load_dist, load_dist, load_dist);
838 Generate(pos - offset, pos + offset);
841 void ChunkLoader::Update(int dt) {
842 // check if a chunk generation is scheduled for this frame
843 // and if there's a chunk waiting to be generated
844 gen_timer.Update(dt);
845 if (gen_timer.Hit()) {
850 void ChunkLoader::LoadN(std::size_t n) {
851 std::size_t end = std::min(n, ToLoad());
852 for (std::size_t i = 0; i < end; ++i) {
857 void ChunkLoader::LoadOne() {
858 if (to_generate.empty()) return;
860 // take position of next chunk in queue
861 Chunk::Pos pos(to_generate.front());
862 to_generate.pop_front();
864 // look if the same chunk was already generated and still lingering
865 for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
866 if (iter->Position() == pos) {
867 loaded.splice(loaded.end(), to_free, iter);
868 Insert(loaded.back());
873 // if the free list is empty, allocate a new chunk
874 // otherwise clear an unused one
875 if (to_free.empty()) {
876 loaded.emplace_back(reg);
878 to_free.front().ClearNeighbors();
879 loaded.splice(loaded.end(), to_free, to_free.begin());
882 Chunk &chunk = loaded.back();