1 #include "BlockLookup.hpp"
3 #include "ChunkLoader.hpp"
5 #include "Generator.hpp"
14 constexpr int Chunk::width;
15 constexpr int Chunk::height;
16 constexpr int Chunk::depth;
17 constexpr int Chunk::size;
20 Chunk::Chunk(const BlockTypeRegistry &types) noexcept
31 Chunk::Chunk(Chunk &&other) noexcept
33 , model(std::move(other.model))
34 , position(other.position)
35 , dirty(other.dirty) {
36 std::copy(other.neighbor, other.neighbor + sizeof(neighbor), neighbor);
37 std::copy(other.blocks, other.blocks + sizeof(blocks), blocks);
38 std::copy(other.light, other.light + sizeof(light), light);
41 Chunk &Chunk::operator =(Chunk &&other) noexcept {
43 std::copy(other.neighbor, other.neighbor + sizeof(neighbor), neighbor);
44 std::copy(other.blocks, other.blocks + sizeof(blocks), blocks);
45 std::copy(other.light, other.light + sizeof(light), light);
46 model = std::move(other.model);
47 position = other.position;
60 SetNode(Chunk *chunk, Chunk::Pos pos)
61 : chunk(chunk), pos(pos) { }
63 int Get() const noexcept { return chunk->GetLight(pos); }
64 void Set(int level) noexcept { chunk->SetLight(pos, level); }
66 bool HasNext(Block::Face face) noexcept {
67 const BlockLookup next(chunk, pos, face);
68 return next && !next.GetType().block_light;
70 SetNode GetNext(Block::Face face) noexcept {
71 const BlockLookup next(chunk, pos, face);
72 return SetNode(&next.GetChunk(), next.GetBlockPos());
82 UnsetNode(Chunk *chunk, Chunk::Pos pos)
83 : SetNode(chunk, pos), level(Get()) { }
85 UnsetNode(const SetNode &set)
86 : SetNode(set), level(Get()) { }
89 bool HasNext(Block::Face face) noexcept {
90 const BlockLookup next(chunk, pos, face);
93 UnsetNode GetNext(Block::Face face) noexcept { return UnsetNode(SetNode::GetNext(face)); }
97 std::queue<SetNode> light_queue;
98 std::queue<UnsetNode> dark_queue;
100 void work_light() noexcept {
101 while (!light_queue.empty()) {
102 SetNode node = light_queue.front();
105 int level = node.Get() - 1;
106 for (int face = 0; face < Block::FACE_COUNT; ++face) {
107 if (node.HasNext(Block::Face(face))) {
108 SetNode other = node.GetNext(Block::Face(face));
109 if (other.Get() < level) {
111 light_queue.emplace(other);
118 void work_dark() noexcept {
119 while (!dark_queue.empty()) {
120 UnsetNode node = dark_queue.front();
123 for (int face = 0; face < Block::FACE_COUNT; ++face) {
124 if (node.HasNext(Block::Face(face))) {
125 UnsetNode other = node.GetNext(Block::Face(face));
126 // TODO: if there a light source here with the same level this will err
127 if (other.Get() != 0 && other.Get() < node.level) {
129 dark_queue.emplace(other);
131 light_queue.emplace(other);
140 void Chunk::SetBlock(int index, const Block &block) noexcept {
141 const BlockType &old_type = Type(blocks[index]);
142 const BlockType &new_type = Type(block);
144 blocks[index] = block;
146 if (&old_type == &new_type) return;
148 if (new_type.luminosity > old_type.luminosity) {
150 SetLight(index, new_type.luminosity);
151 light_queue.emplace(this, ToPos(index));
153 } else if (new_type.luminosity < old_type.luminosity) {
155 dark_queue.emplace(this, ToPos(index));
158 SetLight(index, new_type.luminosity);
159 light_queue.emplace(this, ToPos(index));
161 } else if (new_type.block_light && !old_type.block_light) {
163 if (GetLight(index) > 0) {
164 dark_queue.emplace(this, ToPos(index));
169 } else if (!new_type.block_light && old_type.block_light) {
172 Pos pos(ToPos(index));
173 for (int face = 0; face < Block::FACE_COUNT; ++face) {
174 BlockLookup next_block(this, pos, Block::Face(face));
176 level = std::max(level, next_block.GetLight());
180 SetLight(index, level - 1);
181 light_queue.emplace(this, pos);
187 void Chunk::SetNeighbor(Chunk &other) noexcept {
188 if (other.position == position + Pos(-1, 0, 0)) {
189 if (neighbor[Block::FACE_LEFT] != &other) {
190 neighbor[Block::FACE_LEFT] = &other;
191 other.neighbor[Block::FACE_RIGHT] = this;
192 for (int z = 0; z < depth; ++z) {
193 for (int y = 0; y < height; ++y) {
195 Pos other_pos(width - 1, y, z);
196 if (GetLight(my_pos) > 0) {
197 light_queue.emplace(this, my_pos);
199 if (other.GetLight(other_pos) > 0) {
200 light_queue.emplace(&other, other_pos);
206 } else if (other.position == position + Pos(1, 0, 0)) {
207 if (neighbor[Block::FACE_RIGHT] != &other) {
208 neighbor[Block::FACE_RIGHT] = &other;
209 other.neighbor[Block::FACE_LEFT] = this;
210 for (int z = 0; z < depth; ++z) {
211 for (int y = 0; y < height; ++y) {
212 Pos my_pos(width - 1, y, z);
213 Pos other_pos(0, y, z);
214 if (GetLight(my_pos) > 0) {
215 light_queue.emplace(this, my_pos);
217 if (other.GetLight(other_pos) > 0) {
218 light_queue.emplace(&other, other_pos);
224 } else if (other.position == position + Pos(0, -1, 0)) {
225 if (neighbor[Block::FACE_DOWN] != &other) {
226 neighbor[Block::FACE_DOWN] = &other;
227 other.neighbor[Block::FACE_UP] = this;
228 for (int z = 0; z < depth; ++z) {
229 for (int x = 0; x < width; ++x) {
231 Pos other_pos(x, height - 1, z);
232 if (GetLight(my_pos) > 0) {
233 light_queue.emplace(this, my_pos);
235 if (other.GetLight(other_pos) > 0) {
236 light_queue.emplace(&other, other_pos);
242 } else if (other.position == position + Pos(0, 1, 0)) {
243 if (neighbor[Block::FACE_UP] != &other) {
244 neighbor[Block::FACE_UP] = &other;
245 other.neighbor[Block::FACE_DOWN] = this;
246 for (int z = 0; z < depth; ++z) {
247 for (int x = 0; x < width; ++x) {
248 Pos my_pos(x, height - 1, z);
249 Pos other_pos(x, 0, z);
250 if (GetLight(my_pos) > 0) {
251 light_queue.emplace(this, my_pos);
253 if (other.GetLight(other_pos) > 0) {
254 light_queue.emplace(&other, other_pos);
260 } else if (other.position == position + Pos(0, 0, -1)) {
261 if (neighbor[Block::FACE_BACK] != &other) {
262 neighbor[Block::FACE_BACK] = &other;
263 other.neighbor[Block::FACE_FRONT] = this;
264 for (int y = 0; y < height; ++y) {
265 for (int x = 0; x < width; ++x) {
267 Pos other_pos(x, y, depth - 1);
268 if (GetLight(my_pos) > 0) {
269 light_queue.emplace(this, my_pos);
271 if (other.GetLight(other_pos) > 0) {
272 light_queue.emplace(&other, other_pos);
278 } else if (other.position == position + Pos(0, 0, 1)) {
279 if (neighbor[Block::FACE_FRONT] != &other) {
280 neighbor[Block::FACE_FRONT] = &other;
281 other.neighbor[Block::FACE_BACK] = this;
282 for (int y = 0; y < height; ++y) {
283 for (int x = 0; x < width; ++x) {
284 Pos my_pos(x, y, depth - 1);
285 Pos other_pos(x, y, 0);
286 if (GetLight(my_pos) > 0) {
287 light_queue.emplace(this, my_pos);
289 if (other.GetLight(other_pos) > 0) {
290 light_queue.emplace(&other, other_pos);
299 void Chunk::ClearNeighbors() noexcept {
300 for (int i = 0; i < Block::FACE_COUNT; ++i) {
301 neighbor[i] = nullptr;
305 void Chunk::Unlink() noexcept {
306 for (int face = 0; face < Block::FACE_COUNT; ++face) {
307 if (neighbor[face]) {
308 neighbor[face]->neighbor[Block::Opposite(Block::Face(face))] = nullptr;
313 void Chunk::Relink() noexcept {
314 for (int face = 0; face < Block::FACE_COUNT; ++face) {
315 if (neighbor[face]) {
316 neighbor[face]->neighbor[Block::Opposite(Block::Face(face))] = this;
322 void Chunk::SetLight(int index, int level) noexcept {
323 if (light[index] != level) {
324 light[index] = level;
329 int Chunk::GetLight(int index) const noexcept {
333 float Chunk::GetVertexLight(const Pos &pos, const BlockModel::Position &vtx, const Model::Normal &norm) const noexcept {
334 int index = ToIndex(pos);
335 float light = GetLight(index);
337 Block::Face direct_face(Block::NormalFace(norm));
339 BlockLookup direct(const_cast<Chunk *>(this), pos, Block::NormalFace(norm));
341 float direct_light = direct.GetLight();
342 if (direct_light > light) {
343 light = direct_light;
349 if (Type(BlockAt(index)).luminosity > 0 || direct.GetType().block_light) {
354 switch (Block::Axis(direct_face)) {
356 edge[0] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
357 edge[1] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
360 edge[0] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
361 edge[1] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
364 edge[0] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
365 edge[1] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
372 BlockLookup next[2] = {
373 direct.Next(edge[0]),
374 direct.Next(edge[1]),
378 if (next[0].GetType().block_light) {
381 light += next[0].GetLight();
386 if (next[1].GetType().block_light) {
389 light += next[1].GetLight();
395 BlockLookup corner = next[0].Next(edge[1]);
397 if (corner.GetType().block_light) {
400 light += corner.GetLight();
404 } else if (next[1]) {
405 BlockLookup corner = next[1].Next(edge[0]);
407 if (corner.GetType().block_light) {
410 light += corner.GetLight();
419 return (light / num) - (occlusion * 0.8f);
423 bool Chunk::IsSurface(const Pos &pos) const noexcept {
424 const Block &block = BlockAt(pos);
425 if (!Type(block).visible) {
428 for (int face = 0; face < Block::FACE_COUNT; ++face) {
429 BlockLookup next = BlockLookup(const_cast<Chunk *>(this), pos, Block::Face(face));
430 if (!next || !next.GetType().visible) {
438 void Chunk::Draw() noexcept {
446 bool Chunk::Intersection(
455 dist = std::numeric_limits<float>::infinity();
456 for (int z = 0; z < depth; ++z) {
457 for (int y = 0; y < height; ++y) {
458 for (int x = 0; x < width; ++x, ++idx) {
459 const BlockType &type = Type(idx);
465 if (type.shape->Intersects(ray, M * ToTransform(Pos(x, y, z), idx), cur_dist, cur_norm)) {
466 if (cur_dist < dist) {
479 normal = glm::vec3(BlockAt(blkid).Transform() * glm::vec4(normal, 0.0f));
484 bool Chunk::Intersection(
486 const glm::mat4 &Mbox,
487 const glm::mat4 &Mchunk
489 if (!blank::Intersection(box, Mbox, Bounds(), Mchunk)) {
492 for (int idx = 0, z = 0; z < depth; ++z) {
493 for (int y = 0; y < height; ++y) {
494 for (int x = 0; x < width; ++x, ++idx) {
495 const BlockType &type = Type(idx);
499 if (type.shape->Intersects(Mchunk * ToTransform(Pos(x, y, z), idx), box, Mbox)) {
511 BlockModel::Buffer buf;
515 void Chunk::CheckUpdate() noexcept {
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(const Config &config, const BlockTypeRegistry ®, const Generator &gen) noexcept
654 , gen_timer(config.gen_limit)
655 , load_dist(config.load_dist)
656 , unload_dist(config.unload_dist) {
664 explicit ChunkLess(const Chunk::Pos &base) noexcept
667 bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const noexcept {
668 Chunk::Pos da(base - a);
669 Chunk::Pos db(base - b);
671 da.x * da.x + da.y * da.y + da.z * da.z <
672 db.x * db.x + db.y * db.y + db.z * db.z;
681 void ChunkLoader::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
682 for (int z = from.z; z < to.z; ++z) {
683 for (int y = from.y; y < to.y; ++y) {
684 for (int x = from.x; x < to.x; ++x) {
685 Chunk::Pos pos(x, y, z);
688 } else if (pos == base) {
692 // for (int i = 0; i < 16; ++i) {
693 // for (int j = 0; j < 16; ++j) {
694 // loaded.back().SetBlock(Chunk::Pos{ i, j, 0 }, Block(1));
695 // loaded.back().SetBlock(Chunk::Pos{ i, j, 15 }, Block(1));
696 // loaded.back().SetBlock(Chunk::Pos{ 0, j, i }, Block(1));
697 // loaded.back().SetBlock(Chunk::Pos{ 15, j, i }, Block(1));
700 // loaded.back().SetBlock(Chunk::Pos{ 1, 0, 1 }, Block(13));
701 // loaded.back().SetBlock(Chunk::Pos{ 14, 0, 1 }, Block(13));
702 // loaded.back().SetBlock(Chunk::Pos{ 1, 0, 14 }, Block(13));
703 // loaded.back().SetBlock(Chunk::Pos{ 14, 0, 14 }, Block(13));
704 // loaded.back().SetBlock(Chunk::Pos{ 1, 15, 1 }, Block(13));
705 // loaded.back().SetBlock(Chunk::Pos{ 14, 15, 1 }, Block(13));
706 // loaded.back().SetBlock(Chunk::Pos{ 1, 15, 14 }, Block(13));
707 // loaded.back().SetBlock(Chunk::Pos{ 14, 15, 14 }, Block(13));
708 // loaded.back().SetBlock(Chunk::Pos{ 7, 7, 0 }, Block(13));
709 // loaded.back().SetBlock(Chunk::Pos{ 8, 7, 0 }, Block(13));
710 // loaded.back().SetBlock(Chunk::Pos{ 7, 8, 0 }, Block(13));
711 // loaded.back().SetBlock(Chunk::Pos{ 8, 8, 0 }, Block(13));
712 // loaded.back().SetBlock(Chunk::Pos{ 7, 7, 15 }, Block(13));
713 // loaded.back().SetBlock(Chunk::Pos{ 8, 7, 15 }, Block(13));
714 // loaded.back().SetBlock(Chunk::Pos{ 7, 8, 15 }, Block(13));
715 // loaded.back().SetBlock(Chunk::Pos{ 8, 8, 15 }, Block(13));
716 // loaded.back().SetBlock(Chunk::Pos{ 0, 7, 7 }, Block(13));
717 // loaded.back().SetBlock(Chunk::Pos{ 0, 7, 8 }, Block(13));
718 // loaded.back().SetBlock(Chunk::Pos{ 0, 8, 7 }, Block(13));
719 // loaded.back().SetBlock(Chunk::Pos{ 0, 8, 8 }, Block(13));
720 // loaded.back().SetBlock(Chunk::Pos{ 15, 7, 7 }, Block(13));
721 // loaded.back().SetBlock(Chunk::Pos{ 15, 7, 8 }, Block(13));
722 // loaded.back().SetBlock(Chunk::Pos{ 15, 8, 7 }, Block(13));
723 // loaded.back().SetBlock(Chunk::Pos{ 15, 8, 8 }, Block(13));
724 // loaded.back().Invalidate();
725 // loaded.back().CheckUpdate();
727 // orientation testing
728 // for (int i = 0; i < Block::FACE_COUNT; ++i) {
729 // for (int j = 0; j < Block::TURN_COUNT; ++j) {
730 // loaded.back().BlockAt(512 * j + 2 * i) = Block(3 * (j + 1), Block::Face(i), Block::Turn(j));
733 // loaded.back().Invalidate();
734 // loaded.back().CheckUpdate();
736 to_generate.emplace_back(pos);
741 to_generate.sort(ChunkLess(base));
744 Chunk &ChunkLoader::Generate(const Chunk::Pos &pos) {
745 loaded.emplace_back(reg);
746 Chunk &chunk = loaded.back();
753 void ChunkLoader::Insert(Chunk &chunk) noexcept {
754 for (Chunk &other : loaded) {
755 chunk.SetNeighbor(other);
759 void ChunkLoader::Remove(Chunk &chunk) noexcept {
763 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) noexcept {
764 for (Chunk &chunk : loaded) {
765 if (chunk.Position() == pos) {
772 bool ChunkLoader::Queued(const Chunk::Pos &pos) noexcept {
773 for (const Chunk::Pos &chunk : to_generate) {
781 bool ChunkLoader::Known(const Chunk::Pos &pos) noexcept {
782 if (Loaded(pos)) return true;
786 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
787 Chunk *chunk = Loaded(pos);
792 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
794 to_generate.erase(iter);
799 return Generate(pos);
802 bool ChunkLoader::OutOfRange(const Chunk::Pos &pos) const noexcept {
803 return std::abs(base.x - pos.x) > unload_dist
804 || std::abs(base.y - pos.y) > unload_dist
805 || std::abs(base.z - pos.z) > unload_dist;
808 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
809 if (new_base == base) {
814 // unload far away chunks
815 for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
816 if (OutOfRange(*iter)) {
820 to_free.splice(to_free.end(), loaded, saved);
825 // abort far away queued chunks
826 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
827 if (OutOfRange(*iter)) {
828 iter = to_generate.erase(iter);
833 // add missing new chunks
834 GenerateSurrounding(base);
837 void ChunkLoader::GenerateSurrounding(const Chunk::Pos &pos) {
838 const Chunk::Pos offset(load_dist, load_dist, load_dist);
839 Generate(pos - offset, pos + offset);
842 void ChunkLoader::Update(int dt) {
843 gen_timer.Update(dt);
844 if (!gen_timer.Hit() || to_generate.empty()) {
848 Chunk::Pos pos(to_generate.front());
849 to_generate.pop_front();
851 for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
852 if (iter->Position() == pos) {
854 loaded.splice(loaded.end(), to_free, iter);
858 if (to_free.empty()) {
859 loaded.emplace_back(reg);
861 to_free.front().ClearNeighbors();
862 loaded.splice(loaded.end(), to_free, to_free.begin());
864 Chunk &chunk = loaded.back();