1 #include "BlockLookup.hpp"
3 #include "ChunkLoader.hpp"
5 #include "Generator.hpp"
6 #include "WorldCollision.hpp"
7 #include "../io/WorldSave.hpp"
17 constexpr int Chunk::width;
18 constexpr int Chunk::height;
19 constexpr int Chunk::depth;
20 constexpr int Chunk::size;
23 Chunk::Chunk(const BlockTypeRegistry &types) noexcept
35 Chunk::Chunk(Chunk &&other) noexcept
37 , model(std::move(other.model))
38 , position(other.position)
39 , dirty_model(other.dirty_model)
40 , dirty_save(other.dirty_save) {
41 std::copy(other.neighbor, other.neighbor + sizeof(neighbor), neighbor);
42 std::copy(other.blocks, other.blocks + sizeof(blocks), blocks);
43 std::copy(other.light, other.light + sizeof(light), light);
46 Chunk &Chunk::operator =(Chunk &&other) noexcept {
48 std::copy(other.neighbor, other.neighbor + sizeof(neighbor), neighbor);
49 std::copy(other.blocks, other.blocks + sizeof(blocks), blocks);
50 std::copy(other.light, other.light + sizeof(light), light);
51 model = std::move(other.model);
52 position = other.position;
53 dirty_model = other.dirty_save;
54 dirty_save = other.dirty_save;
66 SetNode(Chunk *chunk, Chunk::Pos pos)
67 : chunk(chunk), pos(pos) { }
69 int Get() const noexcept { return chunk->GetLight(pos); }
70 void Set(int level) noexcept { chunk->SetLight(pos, level); }
72 const BlockType &GetType() const noexcept { return chunk->Type(Chunk::ToIndex(pos)); }
74 bool HasNext(Block::Face face) noexcept {
75 const BlockType &type = GetType();
76 if (type.block_light && !type.luminosity) return false;
77 const BlockLookup next(chunk, pos, face);
80 SetNode GetNext(Block::Face face) noexcept {
81 const BlockLookup next(chunk, pos, face);
82 return SetNode(&next.GetChunk(), next.GetBlockPos());
92 UnsetNode(Chunk *chunk, Chunk::Pos pos)
93 : SetNode(chunk, pos), level(Get()) { }
95 UnsetNode(const SetNode &set)
96 : SetNode(set), level(Get()) { }
99 bool HasNext(Block::Face face) noexcept {
100 const BlockLookup next(chunk, pos, face);
103 UnsetNode GetNext(Block::Face face) noexcept { return UnsetNode(SetNode::GetNext(face)); }
107 std::queue<SetNode> light_queue;
108 std::queue<UnsetNode> dark_queue;
110 void work_light() noexcept {
111 while (!light_queue.empty()) {
112 SetNode node = light_queue.front();
115 int level = node.Get() - 1;
116 for (int face = 0; face < Block::FACE_COUNT; ++face) {
117 if (node.HasNext(Block::Face(face))) {
118 SetNode other = node.GetNext(Block::Face(face));
119 if (other.Get() < level) {
121 light_queue.emplace(other);
128 void work_dark() noexcept {
129 while (!dark_queue.empty()) {
130 UnsetNode node = dark_queue.front();
133 for (int face = 0; face < Block::FACE_COUNT; ++face) {
134 if (node.HasNext(Block::Face(face))) {
135 UnsetNode other = node.GetNext(Block::Face(face));
136 // TODO: if there a light source here with the same level this will err
137 if (other.Get() != 0 && other.Get() < node.level) {
139 dark_queue.emplace(other);
141 light_queue.emplace(other);
150 void Chunk::SetBlock(int index, const Block &block) noexcept {
151 const BlockType &old_type = Type(blocks[index]);
152 const BlockType &new_type = Type(block);
154 blocks[index] = block;
157 if (&old_type == &new_type) return;
159 if (new_type.luminosity > old_type.luminosity) {
161 SetLight(index, new_type.luminosity);
162 light_queue.emplace(this, ToPos(index));
164 } else if (new_type.luminosity < old_type.luminosity) {
166 dark_queue.emplace(this, ToPos(index));
169 SetLight(index, new_type.luminosity);
170 light_queue.emplace(this, ToPos(index));
172 } else if (new_type.block_light && !old_type.block_light) {
174 if (GetLight(index) > 0) {
175 dark_queue.emplace(this, ToPos(index));
180 } else if (!new_type.block_light && old_type.block_light) {
183 Pos pos(ToPos(index));
184 for (int face = 0; face < Block::FACE_COUNT; ++face) {
185 BlockLookup next_block(this, pos, Block::Face(face));
187 level = std::max(level, next_block.GetLight());
191 SetLight(index, level - 1);
192 light_queue.emplace(this, pos);
200 // propagate light from a's edge to b
202 Chunk &a, const Chunk::Pos &a_pos,
203 Chunk &b, const Chunk::Pos &b_pos
205 if (a.GetLight(a_pos) > 1) {
206 const BlockType &b_type = b.Type(Chunk::ToIndex(b_pos));
207 if (!b_type.block_light) {
208 light_queue.emplace(&a, a_pos);
210 if (b_type.visible) {
218 void Chunk::SetNeighbor(Chunk &other) noexcept {
219 if (other.position == position + Pos(-1, 0, 0)) {
220 if (neighbor[Block::FACE_LEFT] != &other) {
221 neighbor[Block::FACE_LEFT] = &other;
222 other.neighbor[Block::FACE_RIGHT] = this;
223 for (int z = 0; z < depth; ++z) {
224 for (int y = 0; y < height; ++y) {
226 Pos other_pos(width - 1, y, z);
227 edge_light(*this, my_pos, other, other_pos);
228 edge_light(other, other_pos, *this, my_pos);
233 } else if (other.position == position + Pos(1, 0, 0)) {
234 if (neighbor[Block::FACE_RIGHT] != &other) {
235 neighbor[Block::FACE_RIGHT] = &other;
236 other.neighbor[Block::FACE_LEFT] = this;
237 for (int z = 0; z < depth; ++z) {
238 for (int y = 0; y < height; ++y) {
239 Pos my_pos(width - 1, y, z);
240 Pos other_pos(0, y, z);
241 edge_light(*this, my_pos, other, other_pos);
242 edge_light(other, other_pos, *this, my_pos);
247 } else if (other.position == position + Pos(0, -1, 0)) {
248 if (neighbor[Block::FACE_DOWN] != &other) {
249 neighbor[Block::FACE_DOWN] = &other;
250 other.neighbor[Block::FACE_UP] = this;
251 for (int z = 0; z < depth; ++z) {
252 for (int x = 0; x < width; ++x) {
254 Pos other_pos(x, height - 1, z);
255 edge_light(*this, my_pos, other, other_pos);
256 edge_light(other, other_pos, *this, my_pos);
261 } else if (other.position == position + Pos(0, 1, 0)) {
262 if (neighbor[Block::FACE_UP] != &other) {
263 neighbor[Block::FACE_UP] = &other;
264 other.neighbor[Block::FACE_DOWN] = this;
265 for (int z = 0; z < depth; ++z) {
266 for (int x = 0; x < width; ++x) {
267 Pos my_pos(x, height - 1, z);
268 Pos other_pos(x, 0, z);
269 edge_light(*this, my_pos, other, other_pos);
270 edge_light(other, other_pos, *this, my_pos);
275 } else if (other.position == position + Pos(0, 0, -1)) {
276 if (neighbor[Block::FACE_BACK] != &other) {
277 neighbor[Block::FACE_BACK] = &other;
278 other.neighbor[Block::FACE_FRONT] = this;
279 for (int y = 0; y < height; ++y) {
280 for (int x = 0; x < width; ++x) {
282 Pos other_pos(x, y, depth - 1);
283 edge_light(*this, my_pos, other, other_pos);
284 edge_light(other, other_pos, *this, my_pos);
289 } else if (other.position == position + Pos(0, 0, 1)) {
290 if (neighbor[Block::FACE_FRONT] != &other) {
291 neighbor[Block::FACE_FRONT] = &other;
292 other.neighbor[Block::FACE_BACK] = this;
293 for (int y = 0; y < height; ++y) {
294 for (int x = 0; x < width; ++x) {
295 Pos my_pos(x, y, depth - 1);
296 Pos other_pos(x, y, 0);
297 edge_light(*this, my_pos, other, other_pos);
298 edge_light(other, other_pos, *this, my_pos);
306 void Chunk::ClearNeighbors() noexcept {
307 for (int face = 0; face < Block::FACE_COUNT; ++face) {
308 if (neighbor[face]) {
309 neighbor[face]->neighbor[Block::Opposite(Block::Face(face))] = nullptr;
310 neighbor[face] = nullptr;
316 void Chunk::SetLight(int index, int level) noexcept {
317 if (light[index] != level) {
318 light[index] = level;
323 int Chunk::GetLight(int index) const noexcept {
327 float Chunk::GetVertexLight(const Pos &pos, const BlockModel::Position &vtx, const EntityModel::Normal &norm) const noexcept {
328 int index = ToIndex(pos);
329 float light = GetLight(index);
331 Block::Face direct_face(Block::NormalFace(norm));
333 BlockLookup direct(const_cast<Chunk *>(this), pos, Block::NormalFace(norm));
335 float direct_light = direct.GetLight();
336 if (direct_light > light) {
337 light = direct_light;
343 if (Type(BlockAt(index)).luminosity > 0 || direct.GetType().block_light) {
348 switch (Block::Axis(direct_face)) {
350 edge[0] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
351 edge[1] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
354 edge[0] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
355 edge[1] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
358 edge[0] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
359 edge[1] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
366 BlockLookup next[2] = {
367 direct.Next(edge[0]),
368 direct.Next(edge[1]),
372 if (next[0].GetType().block_light) {
375 light += next[0].GetLight();
380 if (next[1].GetType().block_light) {
383 light += next[1].GetLight();
389 BlockLookup corner = next[0].Next(edge[1]);
391 if (corner.GetType().block_light) {
394 light += corner.GetLight();
398 } else if (next[1]) {
399 BlockLookup corner = next[1].Next(edge[0]);
401 if (corner.GetType().block_light) {
404 light += corner.GetLight();
413 return (light / num) - (occlusion * 0.8f);
417 bool Chunk::IsSurface(const Pos &pos) const noexcept {
418 const Block &block = BlockAt(pos);
419 if (!Type(block).visible) {
422 for (int face = 0; face < Block::FACE_COUNT; ++face) {
423 BlockLookup next = BlockLookup(const_cast<Chunk *>(this), pos, Block::Face(face));
424 if (!next || !next.GetType().visible) {
432 void Chunk::Draw() noexcept {
433 if (ShouldUpdateModel()) {
440 bool Chunk::Intersection(
449 dist = std::numeric_limits<float>::infinity();
450 for (int z = 0; z < depth; ++z) {
451 for (int y = 0; y < height; ++y) {
452 for (int x = 0; x < width; ++x, ++idx) {
453 const BlockType &type = Type(idx);
459 if (type.shape->Intersects(ray, M * ToTransform(Pos(x, y, z), idx), cur_dist, cur_norm)) {
460 if (cur_dist < dist) {
473 normal = glm::vec3(BlockAt(blkid).Transform() * glm::vec4(normal, 0.0f));
478 bool Chunk::Intersection(
480 const glm::mat4 &Mbox,
481 const glm::mat4 &Mchunk,
482 std::vector<WorldCollision> &col
488 if (!blank::Intersection(box, Mbox, Bounds(), Mchunk, penetration, normal)) {
491 for (int idx = 0, z = 0; z < depth; ++z) {
492 for (int y = 0; y < height; ++y) {
493 for (int x = 0; x < width; ++x, ++idx) {
494 const BlockType &type = Type(idx);
495 if (!type.collision) {
498 if (type.shape->Intersects(Mchunk * ToTransform(Pos(x, y, z), idx), box, Mbox, penetration, normal)) {
499 col.emplace_back(this, idx, penetration, normal);
511 BlockModel::Buffer buf;
515 void Chunk::CheckUpdate() noexcept {
516 if (ShouldUpdateModel()) {
521 void Chunk::Update() noexcept {
522 int vtx_count = 0, idx_count = 0;
523 for (const auto &block : blocks) {
524 const Shape *shape = Type(block).shape;
525 vtx_count += shape->VertexCount();
526 idx_count += shape->VertexIndexCount();
529 buf.Reserve(vtx_count, idx_count);
532 BlockModel::Index vtx_counter = 0;
533 for (size_t z = 0; z < depth; ++z) {
534 for (size_t y = 0; y < height; ++y) {
535 for (size_t x = 0; x < width; ++x, ++idx) {
536 const BlockType &type = Type(BlockAt(idx));
537 const Pos pos(x, y, z);
539 if (!type.visible || Obstructed(pos).All()) continue;
541 type.FillBlockModel(buf, ToTransform(pos, idx), vtx_counter);
542 size_t vtx_begin = vtx_counter;
543 vtx_counter += type.shape->VertexCount();
545 for (size_t vtx = vtx_begin; vtx < vtx_counter; ++vtx) {
546 buf.lights.emplace_back(GetVertexLight(
549 type.shape->VertexNormal(vtx - vtx_begin, BlockAt(idx).Transform())
560 Block::FaceSet Chunk::Obstructed(const Pos &pos) const noexcept {
561 Block::FaceSet result;
563 for (int f = 0; f < Block::FACE_COUNT; ++f) {
564 Block::Face face = Block::Face(f);
565 BlockLookup next(const_cast<Chunk *>(this), pos, face);
566 if (next && next.GetType().FaceFilled(next.GetBlock(), Block::Opposite(face))) {
574 glm::mat4 Chunk::ToTransform(const Pos &pos, int idx) const noexcept {
575 return glm::translate(ToCoords(pos)) * BlockAt(idx).Transform();
579 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p) noexcept
581 while (pos.x >= Chunk::width) {
582 if (chunk->HasNeighbor(Block::FACE_RIGHT)) {
583 chunk = &chunk->GetNeighbor(Block::FACE_RIGHT);
584 pos.x -= Chunk::width;
591 if (chunk->HasNeighbor(Block::FACE_LEFT)) {
592 chunk = &chunk->GetNeighbor(Block::FACE_LEFT);
593 pos.x += Chunk::width;
599 while (pos.y >= Chunk::height) {
600 if (chunk->HasNeighbor(Block::FACE_UP)) {
601 chunk = &chunk->GetNeighbor(Block::FACE_UP);
602 pos.y -= Chunk::height;
609 if (chunk->HasNeighbor(Block::FACE_DOWN)) {
610 chunk = &chunk->GetNeighbor(Block::FACE_DOWN);
611 pos.y += Chunk::height;
617 while (pos.z >= Chunk::depth) {
618 if (chunk->HasNeighbor(Block::FACE_FRONT)) {
619 chunk = &chunk->GetNeighbor(Block::FACE_FRONT);
620 pos.z -= Chunk::depth;
627 if (chunk->HasNeighbor(Block::FACE_BACK)) {
628 chunk = &chunk->GetNeighbor(Block::FACE_BACK);
629 pos.z += Chunk::depth;
637 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face face) noexcept
639 pos += Block::FaceNormal(face);
640 if (!Chunk::InBounds(pos)) {
641 pos -= Block::FaceNormal(face) * Chunk::Extent();
642 chunk = &chunk->GetNeighbor(face);
647 ChunkLoader::ChunkLoader(
648 const Config &config,
649 const BlockTypeRegistry ®,
650 const Generator &gen,
651 const WorldSave &save
660 , gen_timer(config.gen_limit)
661 , load_dist(config.load_dist)
662 , unload_dist(config.unload_dist) {
670 explicit ChunkLess(const Chunk::Pos &base) noexcept
673 bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const noexcept {
674 Chunk::Pos da(base - a);
675 Chunk::Pos db(base - b);
677 da.x * da.x + da.y * da.y + da.z * da.z <
678 db.x * db.x + db.y * db.y + db.z * db.z;
687 void ChunkLoader::Queue(const Chunk::Pos &from, const Chunk::Pos &to) {
688 for (int z = from.z; z < to.z; ++z) {
689 for (int y = from.y; y < to.y; ++y) {
690 for (int x = from.x; x < to.x; ++x) {
691 Chunk::Pos pos(x, y, z);
694 } else if (pos == base) {
698 // for (int i = 0; i < 16; ++i) {
699 // for (int j = 0; j < 16; ++j) {
700 // loaded.back().SetBlock(Chunk::Pos{ i, j, 0 }, Block(1));
701 // loaded.back().SetBlock(Chunk::Pos{ i, j, 15 }, Block(1));
702 // loaded.back().SetBlock(Chunk::Pos{ 0, j, i }, Block(1));
703 // loaded.back().SetBlock(Chunk::Pos{ 15, j, i }, Block(1));
706 // loaded.back().SetBlock(Chunk::Pos{ 1, 0, 1 }, Block(13));
707 // loaded.back().SetBlock(Chunk::Pos{ 14, 0, 1 }, Block(13));
708 // loaded.back().SetBlock(Chunk::Pos{ 1, 0, 14 }, Block(13));
709 // loaded.back().SetBlock(Chunk::Pos{ 14, 0, 14 }, Block(13));
710 // loaded.back().SetBlock(Chunk::Pos{ 1, 15, 1 }, Block(13));
711 // loaded.back().SetBlock(Chunk::Pos{ 14, 15, 1 }, Block(13));
712 // loaded.back().SetBlock(Chunk::Pos{ 1, 15, 14 }, Block(13));
713 // loaded.back().SetBlock(Chunk::Pos{ 14, 15, 14 }, Block(13));
714 // loaded.back().SetBlock(Chunk::Pos{ 7, 7, 0 }, Block(13));
715 // loaded.back().SetBlock(Chunk::Pos{ 8, 7, 0 }, Block(13));
716 // loaded.back().SetBlock(Chunk::Pos{ 7, 8, 0 }, Block(13));
717 // loaded.back().SetBlock(Chunk::Pos{ 8, 8, 0 }, Block(13));
718 // loaded.back().SetBlock(Chunk::Pos{ 7, 7, 15 }, Block(13));
719 // loaded.back().SetBlock(Chunk::Pos{ 8, 7, 15 }, Block(13));
720 // loaded.back().SetBlock(Chunk::Pos{ 7, 8, 15 }, Block(13));
721 // loaded.back().SetBlock(Chunk::Pos{ 8, 8, 15 }, Block(13));
722 // loaded.back().SetBlock(Chunk::Pos{ 0, 7, 7 }, Block(13));
723 // loaded.back().SetBlock(Chunk::Pos{ 0, 7, 8 }, Block(13));
724 // loaded.back().SetBlock(Chunk::Pos{ 0, 8, 7 }, Block(13));
725 // loaded.back().SetBlock(Chunk::Pos{ 0, 8, 8 }, Block(13));
726 // loaded.back().SetBlock(Chunk::Pos{ 15, 7, 7 }, Block(13));
727 // loaded.back().SetBlock(Chunk::Pos{ 15, 7, 8 }, Block(13));
728 // loaded.back().SetBlock(Chunk::Pos{ 15, 8, 7 }, Block(13));
729 // loaded.back().SetBlock(Chunk::Pos{ 15, 8, 8 }, Block(13));
730 // loaded.back().Invalidate();
731 // loaded.back().CheckUpdate();
733 // orientation testing
734 // for (int i = 0; i < Block::FACE_COUNT; ++i) {
735 // for (int j = 0; j < Block::TURN_COUNT; ++j) {
736 // loaded.back().BlockAt(512 * j + 2 * i) = Block(3 * (j + 1), Block::Face(i), Block::Turn(j));
739 // loaded.back().Invalidate();
740 // loaded.back().CheckUpdate();
742 to_load.emplace_back(pos);
747 to_load.sort(ChunkLess(base));
750 Chunk &ChunkLoader::Load(const Chunk::Pos &pos) {
751 loaded.emplace_back(reg);
752 Chunk &chunk = loaded.back();
754 if (save.Exists(pos)) {
763 void ChunkLoader::Insert(Chunk &chunk) noexcept {
764 for (Chunk &other : loaded) {
765 chunk.SetNeighbor(other);
769 std::list<Chunk>::iterator ChunkLoader::Remove(std::list<Chunk>::iterator chunk) noexcept {
770 // fetch next entry while chunk's still in the list
771 std::list<Chunk>::iterator next = chunk;
773 // unlink neighbors so they won't reference a dead chunk
774 chunk->ClearNeighbors();
775 // if it should be saved, do it now
776 if (chunk->ShouldUpdateSave()) {
779 // and move it from loaded to free list
780 to_free.splice(to_free.end(), loaded, chunk);
784 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) noexcept {
785 for (Chunk &chunk : loaded) {
786 if (chunk.Position() == pos) {
793 bool ChunkLoader::Queued(const Chunk::Pos &pos) noexcept {
794 for (const Chunk::Pos &chunk : to_load) {
802 bool ChunkLoader::Known(const Chunk::Pos &pos) noexcept {
803 if (Loaded(pos)) return true;
807 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
808 Chunk *chunk = Loaded(pos);
813 for (auto iter(to_load.begin()), end(to_load.end()); iter != end; ++iter) {
823 bool ChunkLoader::OutOfRange(const Chunk::Pos &pos) const noexcept {
824 return std::abs(base.x - pos.x) > unload_dist
825 || std::abs(base.y - pos.y) > unload_dist
826 || std::abs(base.z - pos.z) > unload_dist;
829 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
830 if (new_base == base) {
835 // unload far away chunks
836 for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
837 if (OutOfRange(*iter)) {
843 // abort far away queued chunks
844 for (auto iter(to_load.begin()), end(to_load.end()); iter != end;) {
845 if (OutOfRange(*iter)) {
846 iter = to_load.erase(iter);
851 // add missing new chunks
852 QueueSurrounding(base);
855 void ChunkLoader::QueueSurrounding(const Chunk::Pos &pos) {
856 const Chunk::Pos offset(load_dist, load_dist, load_dist);
857 Queue(pos - offset, pos + offset);
860 void ChunkLoader::Update(int dt) {
861 // check if a chunk load is scheduled for this frame
862 // and if there's chunks waiting to be loaded
863 gen_timer.Update(dt);
864 if (gen_timer.Hit()) {
866 // load until one of load or generation limits was hit
867 constexpr int max_load = 10;
868 constexpr int max_gen = 1;
871 while (!to_load.empty() && loaded < max_load && generated < max_gen) {
880 constexpr int max_save = 10;
882 for (Chunk &chunk : loaded) {
883 if (chunk.ShouldUpdateSave()) {
886 if (saved >= max_save) {
893 void ChunkLoader::LoadN(std::size_t n) {
894 std::size_t end = std::min(n, ToLoad());
895 for (std::size_t i = 0; i < end; ++i) {
900 bool ChunkLoader::LoadOne() {
901 if (to_load.empty()) return false;
903 // take position of next chunk in queue
904 Chunk::Pos pos(to_load.front());
907 // look if the same chunk was already generated and still lingering
908 for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
909 if (iter->Position() == pos) {
910 loaded.splice(loaded.end(), to_free, iter);
911 Insert(loaded.back());
916 // if the free list is empty, allocate a new chunk
917 // otherwise clear an unused one
918 if (to_free.empty()) {
919 loaded.emplace_back(reg);
921 to_free.front().ClearNeighbors();
922 loaded.splice(loaded.end(), to_free, to_free.begin());
925 bool generated = false;
926 Chunk &chunk = loaded.back();
928 if (save.Exists(pos)) {