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 for (int face = 0; face < Block::FACE_COUNT; ++face) {
173 BlockLookup next_block(this, ToPos(index), Block::Face(face));
175 level = std::min(level, next_block.GetLight());
179 SetLight(index, level - 1);
180 light_queue.emplace(this, ToPos(index));
186 void Chunk::SetNeighbor(Chunk &other) noexcept {
187 if (other.position == position + Pos(-1, 0, 0)) {
188 if (neighbor[Block::FACE_LEFT] != &other) {
189 neighbor[Block::FACE_LEFT] = &other;
190 other.neighbor[Block::FACE_RIGHT] = this;
191 for (int z = 0; z < depth; ++z) {
192 for (int y = 0; y < height; ++y) {
194 Pos other_pos(width - 1, y, z);
195 if (GetLight(my_pos) > 0) {
196 light_queue.emplace(this, my_pos);
198 if (other.GetLight(other_pos) > 0) {
199 light_queue.emplace(&other, other_pos);
205 } else if (other.position == position + Pos(1, 0, 0)) {
206 if (neighbor[Block::FACE_RIGHT] != &other) {
207 neighbor[Block::FACE_RIGHT] = &other;
208 other.neighbor[Block::FACE_LEFT] = this;
209 for (int z = 0; z < depth; ++z) {
210 for (int y = 0; y < height; ++y) {
211 Pos my_pos(width - 1, y, z);
212 Pos other_pos(0, y, z);
213 if (GetLight(my_pos) > 0) {
214 light_queue.emplace(this, my_pos);
216 if (other.GetLight(other_pos) > 0) {
217 light_queue.emplace(&other, other_pos);
223 } else if (other.position == position + Pos(0, -1, 0)) {
224 if (neighbor[Block::FACE_DOWN] != &other) {
225 neighbor[Block::FACE_DOWN] = &other;
226 other.neighbor[Block::FACE_UP] = this;
227 for (int z = 0; z < depth; ++z) {
228 for (int x = 0; x < width; ++x) {
230 Pos other_pos(x, height - 1, z);
231 if (GetLight(my_pos) > 0) {
232 light_queue.emplace(this, my_pos);
234 if (other.GetLight(other_pos) > 0) {
235 light_queue.emplace(&other, other_pos);
241 } else if (other.position == position + Pos(0, 1, 0)) {
242 if (neighbor[Block::FACE_UP] != &other) {
243 neighbor[Block::FACE_UP] = &other;
244 other.neighbor[Block::FACE_DOWN] = this;
245 for (int z = 0; z < depth; ++z) {
246 for (int x = 0; x < width; ++x) {
247 Pos my_pos(x, height - 1, z);
248 Pos other_pos(x, 0, z);
249 if (GetLight(my_pos) > 0) {
250 light_queue.emplace(this, my_pos);
252 if (other.GetLight(other_pos) > 0) {
253 light_queue.emplace(&other, other_pos);
259 } else if (other.position == position + Pos(0, 0, -1)) {
260 if (neighbor[Block::FACE_BACK] != &other) {
261 neighbor[Block::FACE_BACK] = &other;
262 other.neighbor[Block::FACE_FRONT] = this;
263 for (int y = 0; y < height; ++y) {
264 for (int x = 0; x < width; ++x) {
266 Pos other_pos(x, y, depth - 1);
267 if (GetLight(my_pos) > 0) {
268 light_queue.emplace(this, my_pos);
270 if (other.GetLight(other_pos) > 0) {
271 light_queue.emplace(&other, other_pos);
277 } else if (other.position == position + Pos(0, 0, 1)) {
278 if (neighbor[Block::FACE_FRONT] != &other) {
279 neighbor[Block::FACE_FRONT] = &other;
280 other.neighbor[Block::FACE_BACK] = this;
281 for (int y = 0; y < height; ++y) {
282 for (int x = 0; x < width; ++x) {
283 Pos my_pos(x, y, depth - 1);
284 Pos other_pos(x, y, 0);
285 if (GetLight(my_pos) > 0) {
286 light_queue.emplace(this, my_pos);
288 if (other.GetLight(other_pos) > 0) {
289 light_queue.emplace(&other, other_pos);
298 void Chunk::ClearNeighbors() noexcept {
299 for (int i = 0; i < Block::FACE_COUNT; ++i) {
300 neighbor[i] = nullptr;
304 void Chunk::Unlink() noexcept {
305 for (int face = 0; face < Block::FACE_COUNT; ++face) {
306 if (neighbor[face]) {
307 neighbor[face]->neighbor[Block::Opposite(Block::Face(face))] = nullptr;
312 void Chunk::Relink() noexcept {
313 for (int face = 0; face < Block::FACE_COUNT; ++face) {
314 if (neighbor[face]) {
315 neighbor[face]->neighbor[Block::Opposite(Block::Face(face))] = this;
321 void Chunk::SetLight(int index, int level) noexcept {
322 if (light[index] != level) {
323 light[index] = level;
328 int Chunk::GetLight(int index) const noexcept {
332 float Chunk::GetVertexLight(const Pos &pos, const BlockModel::Position &vtx, const Model::Normal &norm) const noexcept {
333 int index = ToIndex(pos);
334 float light = GetLight(index);
336 Block::Face direct_face(Block::NormalFace(norm));
338 BlockLookup direct(const_cast<Chunk *>(this), pos, Block::NormalFace(norm));
340 float direct_light = direct.GetLight();
341 if (direct_light > light) {
342 light = direct_light;
348 if (Type(BlockAt(index)).luminosity > 0 || direct.GetType().block_light) {
353 switch (Block::Axis(direct_face)) {
355 edge[0] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
356 edge[1] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
359 edge[0] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
360 edge[1] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
363 edge[0] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
364 edge[1] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
371 BlockLookup next[2] = {
372 direct.Next(edge[0]),
373 direct.Next(edge[1]),
377 if (next[0].GetType().block_light) {
380 light += next[0].GetLight();
385 if (next[1].GetType().block_light) {
388 light += next[1].GetLight();
394 BlockLookup corner = next[0].Next(edge[1]);
396 if (corner.GetType().block_light) {
399 light += corner.GetLight();
403 } else if (next[1]) {
404 BlockLookup corner = next[1].Next(edge[0]);
406 if (corner.GetType().block_light) {
409 light += corner.GetLight();
418 return (light / num) - (occlusion * 0.8f);
422 bool Chunk::IsSurface(const Pos &pos) const noexcept {
423 const Block &block = BlockAt(pos);
424 if (!Type(block).visible) {
427 for (int face = 0; face < Block::FACE_COUNT; ++face) {
428 BlockLookup next = BlockLookup(const_cast<Chunk *>(this), pos, Block::Face(face));
429 if (!next || !next.GetType().visible) {
437 void Chunk::Draw() noexcept {
445 bool Chunk::Intersection(
452 // TODO: should be possible to heavily optimize this
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));
487 BlockModel::Buffer buf;
491 void Chunk::CheckUpdate() noexcept {
497 void Chunk::Update() noexcept {
498 int vtx_count = 0, idx_count = 0;
499 for (const auto &block : blocks) {
500 const Shape *shape = Type(block).shape;
501 vtx_count += shape->VertexCount();
502 idx_count += shape->VertexIndexCount();
505 buf.Reserve(vtx_count, idx_count);
508 BlockModel::Index vtx_counter = 0;
509 for (size_t z = 0; z < depth; ++z) {
510 for (size_t y = 0; y < height; ++y) {
511 for (size_t x = 0; x < width; ++x, ++idx) {
512 const BlockType &type = Type(BlockAt(idx));
513 const Pos pos(x, y, z);
515 if (!type.visible || Obstructed(pos).All()) continue;
517 type.FillBlockModel(buf, ToTransform(pos, idx), vtx_counter);
518 size_t vtx_begin = vtx_counter;
519 vtx_counter += type.shape->VertexCount();
521 for (size_t vtx = vtx_begin; vtx < vtx_counter; ++vtx) {
522 buf.lights.emplace_back(GetVertexLight(
525 type.shape->VertexNormal(vtx - vtx_begin, BlockAt(idx).Transform())
536 Block::FaceSet Chunk::Obstructed(const Pos &pos) const noexcept {
537 Block::FaceSet result;
539 for (int f = 0; f < Block::FACE_COUNT; ++f) {
540 Block::Face face = Block::Face(f);
541 BlockLookup next(const_cast<Chunk *>(this), pos, face);
542 if (next && next.GetType().FaceFilled(next.GetBlock(), Block::Opposite(face))) {
550 glm::mat4 Chunk::ToTransform(const Pos &pos, int idx) const noexcept {
551 return glm::translate(ToCoords(pos)) * BlockAt(idx).Transform();
555 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p) noexcept
557 while (pos.x >= Chunk::width) {
558 if (chunk->HasNeighbor(Block::FACE_RIGHT)) {
559 chunk = &chunk->GetNeighbor(Block::FACE_RIGHT);
560 pos.x -= Chunk::width;
567 if (chunk->HasNeighbor(Block::FACE_LEFT)) {
568 chunk = &chunk->GetNeighbor(Block::FACE_LEFT);
569 pos.x += Chunk::width;
575 while (pos.y >= Chunk::height) {
576 if (chunk->HasNeighbor(Block::FACE_UP)) {
577 chunk = &chunk->GetNeighbor(Block::FACE_UP);
578 pos.y -= Chunk::height;
585 if (chunk->HasNeighbor(Block::FACE_DOWN)) {
586 chunk = &chunk->GetNeighbor(Block::FACE_DOWN);
587 pos.y += Chunk::height;
593 while (pos.z >= Chunk::depth) {
594 if (chunk->HasNeighbor(Block::FACE_FRONT)) {
595 chunk = &chunk->GetNeighbor(Block::FACE_FRONT);
596 pos.z -= Chunk::depth;
603 if (chunk->HasNeighbor(Block::FACE_BACK)) {
604 chunk = &chunk->GetNeighbor(Block::FACE_BACK);
605 pos.z += Chunk::depth;
613 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face face) noexcept
615 pos += Block::FaceNormal(face);
616 if (!Chunk::InBounds(pos)) {
617 pos -= Block::FaceNormal(face) * Chunk::Extent();
618 chunk = &chunk->GetNeighbor(face);
623 ChunkLoader::ChunkLoader(const Config &config, const BlockTypeRegistry ®, const Generator &gen) noexcept
630 , load_dist(config.load_dist)
631 , unload_dist(config.unload_dist) {
639 explicit ChunkLess(const Chunk::Pos &base) noexcept
642 bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const noexcept {
643 Chunk::Pos da(base - a);
644 Chunk::Pos db(base - b);
646 da.x * da.x + da.y * da.y + da.z * da.z <
647 db.x * db.x + db.y * db.y + db.z * db.z;
656 void ChunkLoader::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
657 for (int z = from.z; z < to.z; ++z) {
658 for (int y = from.y; y < to.y; ++y) {
659 for (int x = from.x; x < to.x; ++x) {
660 Chunk::Pos pos(x, y, z);
663 } else if (pos == base) {
667 // for (int i = 0; i < 16; ++i) {
668 // for (int j = 0; j < 16; ++j) {
669 // loaded.back().SetBlock(Chunk::Pos{ i, j, 0 }, Block(1));
670 // loaded.back().SetBlock(Chunk::Pos{ i, j, 15 }, Block(1));
671 // loaded.back().SetBlock(Chunk::Pos{ 0, j, i }, Block(1));
672 // loaded.back().SetBlock(Chunk::Pos{ 15, j, i }, Block(1));
675 // loaded.back().SetBlock(Chunk::Pos{ 1, 0, 1 }, Block(13));
676 // loaded.back().SetBlock(Chunk::Pos{ 14, 0, 1 }, Block(13));
677 // loaded.back().SetBlock(Chunk::Pos{ 1, 0, 14 }, Block(13));
678 // loaded.back().SetBlock(Chunk::Pos{ 14, 0, 14 }, Block(13));
679 // loaded.back().SetBlock(Chunk::Pos{ 1, 15, 1 }, Block(13));
680 // loaded.back().SetBlock(Chunk::Pos{ 14, 15, 1 }, Block(13));
681 // loaded.back().SetBlock(Chunk::Pos{ 1, 15, 14 }, Block(13));
682 // loaded.back().SetBlock(Chunk::Pos{ 14, 15, 14 }, Block(13));
683 // loaded.back().SetBlock(Chunk::Pos{ 7, 7, 0 }, Block(13));
684 // loaded.back().SetBlock(Chunk::Pos{ 8, 7, 0 }, Block(13));
685 // loaded.back().SetBlock(Chunk::Pos{ 7, 8, 0 }, Block(13));
686 // loaded.back().SetBlock(Chunk::Pos{ 8, 8, 0 }, Block(13));
687 // loaded.back().SetBlock(Chunk::Pos{ 7, 7, 15 }, Block(13));
688 // loaded.back().SetBlock(Chunk::Pos{ 8, 7, 15 }, Block(13));
689 // loaded.back().SetBlock(Chunk::Pos{ 7, 8, 15 }, Block(13));
690 // loaded.back().SetBlock(Chunk::Pos{ 8, 8, 15 }, Block(13));
691 // loaded.back().SetBlock(Chunk::Pos{ 0, 7, 7 }, Block(13));
692 // loaded.back().SetBlock(Chunk::Pos{ 0, 7, 8 }, Block(13));
693 // loaded.back().SetBlock(Chunk::Pos{ 0, 8, 7 }, Block(13));
694 // loaded.back().SetBlock(Chunk::Pos{ 0, 8, 8 }, Block(13));
695 // loaded.back().SetBlock(Chunk::Pos{ 15, 7, 7 }, Block(13));
696 // loaded.back().SetBlock(Chunk::Pos{ 15, 7, 8 }, Block(13));
697 // loaded.back().SetBlock(Chunk::Pos{ 15, 8, 7 }, Block(13));
698 // loaded.back().SetBlock(Chunk::Pos{ 15, 8, 8 }, Block(13));
699 // loaded.back().Invalidate();
700 // loaded.back().CheckUpdate();
702 // orientation testing
703 // for (int i = 0; i < Block::FACE_COUNT; ++i) {
704 // for (int j = 0; j < Block::TURN_COUNT; ++j) {
705 // loaded.back().BlockAt(512 * j + 2 * i) = Block(3 * (j + 1), Block::Face(i), Block::Turn(j));
708 // loaded.back().Invalidate();
709 // loaded.back().CheckUpdate();
711 to_generate.emplace_back(pos);
716 to_generate.sort(ChunkLess(base));
719 Chunk &ChunkLoader::Generate(const Chunk::Pos &pos) {
720 loaded.emplace_back(reg);
721 Chunk &chunk = loaded.back();
728 void ChunkLoader::Insert(Chunk &chunk) noexcept {
729 for (Chunk &other : loaded) {
730 chunk.SetNeighbor(other);
734 void ChunkLoader::Remove(Chunk &chunk) noexcept {
738 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) noexcept {
739 for (Chunk &chunk : loaded) {
740 if (chunk.Position() == pos) {
747 bool ChunkLoader::Queued(const Chunk::Pos &pos) noexcept {
748 for (const Chunk::Pos &chunk : to_generate) {
756 bool ChunkLoader::Known(const Chunk::Pos &pos) noexcept {
757 if (Loaded(pos)) return true;
761 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
762 Chunk *chunk = Loaded(pos);
767 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
769 to_generate.erase(iter);
774 return Generate(pos);
777 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
778 if (new_base == base) {
783 // unload far away chunks
784 for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
785 if (std::abs(base.x - iter->Position().x) > unload_dist
786 || std::abs(base.y - iter->Position().y) > unload_dist
787 || std::abs(base.z - iter->Position().z) > unload_dist) {
791 to_free.splice(to_free.end(), loaded, saved);
796 // abort far away queued chunks
797 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
798 if (std::abs(base.x - iter->x) > unload_dist
799 || std::abs(base.y - iter->y) > unload_dist
800 || std::abs(base.z - iter->z) > unload_dist) {
801 iter = to_generate.erase(iter);
806 // add missing new chunks
807 GenerateSurrounding(base);
810 void ChunkLoader::GenerateSurrounding(const Chunk::Pos &pos) {
811 const Chunk::Pos offset(load_dist, load_dist, load_dist);
812 Generate(pos - offset, pos + offset);
815 void ChunkLoader::Update() {
816 if (to_generate.empty()) {
820 Chunk::Pos pos(to_generate.front());
821 to_generate.pop_front();
823 for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
824 if (iter->Position() == pos) {
826 loaded.splice(loaded.end(), to_free, iter);
831 if (to_free.empty()) {
832 loaded.emplace_back(reg);
834 to_free.front().ClearNeighbors();
835 loaded.splice(loaded.end(), to_free, to_free.begin());
837 Chunk &chunk = loaded.back();