3 #include "generator.hpp"
12 constexpr int Chunk::width;
13 constexpr int Chunk::height;
14 constexpr int Chunk::depth;
15 constexpr int Chunk::size;
18 Chunk::Chunk(const BlockTypeRegistry &types) noexcept
29 Chunk::Chunk(Chunk &&other) noexcept
31 , model(std::move(other.model))
32 , position(other.position)
33 , dirty(other.dirty) {
34 std::copy(other.neighbor, other.neighbor + sizeof(neighbor), neighbor);
35 std::copy(other.blocks, other.blocks + sizeof(blocks), blocks);
36 std::copy(other.light, other.light + sizeof(light), light);
39 Chunk &Chunk::operator =(Chunk &&other) noexcept {
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);
44 model = std::move(other.model);
45 position = other.position;
58 SetNode(Chunk *chunk, Chunk::Pos pos)
59 : chunk(chunk), pos(pos) { }
61 int Get() const noexcept { return chunk->GetLight(pos); }
62 void Set(int level) noexcept { chunk->SetLight(pos, level); }
64 bool HasNext(Block::Face face) noexcept {
65 const BlockLookup next(chunk, pos, face);
66 return next && !next.GetType().block_light;
68 SetNode GetNext(Block::Face face) noexcept {
69 const BlockLookup next(chunk, pos, face);
70 return SetNode(&next.GetChunk(), next.GetBlockPos());
80 UnsetNode(Chunk *chunk, Chunk::Pos pos)
81 : SetNode(chunk, pos), level(Get()) { }
83 UnsetNode(const SetNode &set)
84 : SetNode(set), level(Get()) { }
87 bool HasNext(Block::Face face) noexcept {
88 const BlockLookup next(chunk, pos, face);
91 UnsetNode GetNext(Block::Face face) noexcept { return UnsetNode(SetNode::GetNext(face)); }
95 std::queue<SetNode> light_queue;
96 std::queue<UnsetNode> dark_queue;
98 void work_light() noexcept {
99 while (!light_queue.empty()) {
100 SetNode node = light_queue.front();
103 int level = node.Get() - 1;
104 for (int face = 0; face < Block::FACE_COUNT; ++face) {
105 if (node.HasNext(Block::Face(face))) {
106 SetNode other = node.GetNext(Block::Face(face));
107 if (other.Get() < level) {
109 light_queue.emplace(other);
116 void work_dark() noexcept {
117 while (!dark_queue.empty()) {
118 UnsetNode node = dark_queue.front();
121 for (int face = 0; face < Block::FACE_COUNT; ++face) {
122 if (node.HasNext(Block::Face(face))) {
123 UnsetNode other = node.GetNext(Block::Face(face));
124 // TODO: if there a light source here with the same level this will err
125 if (other.Get() != 0 && other.Get() < node.level) {
127 dark_queue.emplace(other);
129 light_queue.emplace(other);
138 void Chunk::SetBlock(int index, const Block &block) noexcept {
139 const BlockType &old_type = Type(blocks[index]);
140 const BlockType &new_type = Type(block);
142 blocks[index] = block;
144 if (&old_type == &new_type) return;
146 if (new_type.luminosity > old_type.luminosity) {
148 SetLight(index, new_type.luminosity);
149 light_queue.emplace(this, ToPos(index));
151 } else if (new_type.luminosity < old_type.luminosity) {
153 dark_queue.emplace(this, ToPos(index));
156 SetLight(index, new_type.luminosity);
157 light_queue.emplace(this, ToPos(index));
159 } else if (new_type.block_light && !old_type.block_light) {
161 if (GetLight(index) > 0) {
162 dark_queue.emplace(this, ToPos(index));
167 } else if (!new_type.block_light && old_type.block_light) {
170 for (int face = 0; face < Block::FACE_COUNT; ++face) {
171 BlockLookup next_block(this, ToPos(index), Block::Face(face));
173 level = std::min(level, next_block.GetLight());
177 SetLight(index, level - 1);
178 light_queue.emplace(this, ToPos(index));
184 void Chunk::SetNeighbor(Chunk &other) noexcept {
185 if (other.position == position + Pos(-1, 0, 0)) {
186 if (neighbor[Block::FACE_LEFT] != &other) {
187 neighbor[Block::FACE_LEFT] = &other;
188 other.neighbor[Block::FACE_RIGHT] = this;
189 for (int z = 0; z < depth; ++z) {
190 for (int y = 0; y < height; ++y) {
192 Pos other_pos(width - 1, y, z);
193 if (GetLight(my_pos) > 0) {
194 light_queue.emplace(this, my_pos);
196 if (other.GetLight(other_pos) > 0) {
197 light_queue.emplace(&other, other_pos);
203 } else if (other.position == position + Pos(1, 0, 0)) {
204 if (neighbor[Block::FACE_RIGHT] != &other) {
205 neighbor[Block::FACE_RIGHT] = &other;
206 other.neighbor[Block::FACE_LEFT] = this;
207 for (int z = 0; z < depth; ++z) {
208 for (int y = 0; y < height; ++y) {
209 Pos my_pos(width - 1, y, z);
210 Pos other_pos(0, y, z);
211 if (GetLight(my_pos) > 0) {
212 light_queue.emplace(this, my_pos);
214 if (other.GetLight(other_pos) > 0) {
215 light_queue.emplace(&other, other_pos);
221 } else if (other.position == position + Pos(0, -1, 0)) {
222 if (neighbor[Block::FACE_DOWN] != &other) {
223 neighbor[Block::FACE_DOWN] = &other;
224 other.neighbor[Block::FACE_UP] = this;
225 for (int z = 0; z < depth; ++z) {
226 for (int x = 0; x < width; ++x) {
228 Pos other_pos(x, height - 1, z);
229 if (GetLight(my_pos) > 0) {
230 light_queue.emplace(this, my_pos);
232 if (other.GetLight(other_pos) > 0) {
233 light_queue.emplace(&other, other_pos);
239 } else if (other.position == position + Pos(0, 1, 0)) {
240 if (neighbor[Block::FACE_UP] != &other) {
241 neighbor[Block::FACE_UP] = &other;
242 other.neighbor[Block::FACE_DOWN] = this;
243 for (int z = 0; z < depth; ++z) {
244 for (int x = 0; x < width; ++x) {
245 Pos my_pos(x, height - 1, z);
246 Pos other_pos(x, 0, z);
247 if (GetLight(my_pos) > 0) {
248 light_queue.emplace(this, my_pos);
250 if (other.GetLight(other_pos) > 0) {
251 light_queue.emplace(&other, other_pos);
257 } else if (other.position == position + Pos(0, 0, -1)) {
258 if (neighbor[Block::FACE_BACK] != &other) {
259 neighbor[Block::FACE_BACK] = &other;
260 other.neighbor[Block::FACE_FRONT] = this;
261 for (int y = 0; y < height; ++y) {
262 for (int x = 0; x < width; ++x) {
264 Pos other_pos(x, y, depth - 1);
265 if (GetLight(my_pos) > 0) {
266 light_queue.emplace(this, my_pos);
268 if (other.GetLight(other_pos) > 0) {
269 light_queue.emplace(&other, other_pos);
275 } else if (other.position == position + Pos(0, 0, 1)) {
276 if (neighbor[Block::FACE_FRONT] != &other) {
277 neighbor[Block::FACE_FRONT] = &other;
278 other.neighbor[Block::FACE_BACK] = this;
279 for (int y = 0; y < height; ++y) {
280 for (int x = 0; x < width; ++x) {
281 Pos my_pos(x, y, depth - 1);
282 Pos other_pos(x, y, 0);
283 if (GetLight(my_pos) > 0) {
284 light_queue.emplace(this, my_pos);
286 if (other.GetLight(other_pos) > 0) {
287 light_queue.emplace(&other, other_pos);
296 void Chunk::ClearNeighbors() noexcept {
297 for (int i = 0; i < Block::FACE_COUNT; ++i) {
298 neighbor[i] = nullptr;
302 void Chunk::Unlink() noexcept {
303 for (int face = 0; face < Block::FACE_COUNT; ++face) {
304 if (neighbor[face]) {
305 neighbor[face]->neighbor[Block::Opposite(Block::Face(face))] = nullptr;
310 void Chunk::Relink() noexcept {
311 for (int face = 0; face < Block::FACE_COUNT; ++face) {
312 if (neighbor[face]) {
313 neighbor[face]->neighbor[Block::Opposite(Block::Face(face))] = this;
319 void Chunk::SetLight(int index, int level) noexcept {
320 if (light[index] != level) {
321 light[index] = level;
326 int Chunk::GetLight(int index) const noexcept {
330 float Chunk::GetVertexLight(const Pos &pos, const BlockModel::Position &vtx, const Model::Normal &norm) const noexcept {
331 int index = ToIndex(pos);
332 float light = GetLight(index);
334 Block::Face direct_face(Block::NormalFace(norm));
336 BlockLookup direct(const_cast<Chunk *>(this), pos, Block::NormalFace(norm));
338 float direct_light = direct.GetLight();
339 if (direct_light > light) {
340 light = direct_light;
346 if (Type(BlockAt(index)).luminosity > 0 || direct.GetType().block_light) {
351 switch (Block::Axis(direct_face)) {
353 edge[0] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
354 edge[1] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
357 edge[0] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
358 edge[1] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
361 edge[0] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
362 edge[1] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
369 BlockLookup next[2] = {
370 direct.Next(edge[0]),
371 direct.Next(edge[1]),
375 if (next[0].GetType().block_light) {
378 light += next[0].GetLight();
383 if (next[1].GetType().block_light) {
386 light += next[1].GetLight();
392 BlockLookup corner = next[0].Next(edge[1]);
394 if (corner.GetType().block_light) {
397 light += corner.GetLight();
401 } else if (next[1]) {
402 BlockLookup corner = next[1].Next(edge[0]);
404 if (corner.GetType().block_light) {
407 light += corner.GetLight();
416 return (light / num) - (occlusion * 0.8f);
420 bool Chunk::IsSurface(const Pos &pos) const noexcept {
421 const Block &block = BlockAt(pos);
422 if (!Type(block).visible) {
425 for (int face = 0; face < Block::FACE_COUNT; ++face) {
426 BlockLookup next = BlockLookup(const_cast<Chunk *>(this), pos, Block::Face(face));
427 if (!next || !next.GetType().visible) {
435 void Chunk::Draw() noexcept {
443 bool Chunk::Intersection(
450 // TODO: should be possible to heavily optimize this
453 dist = std::numeric_limits<float>::infinity();
454 for (int z = 0; z < depth; ++z) {
455 for (int y = 0; y < height; ++y) {
456 for (int x = 0; x < width; ++x, ++idx) {
457 const BlockType &type = Type(idx);
463 if (type.shape->Intersects(ray, M * ToTransform(Pos(x, y, z), idx), cur_dist, cur_norm)) {
464 if (cur_dist < dist) {
477 normal = glm::vec3(BlockAt(blkid).Transform() * glm::vec4(normal, 0.0f));
485 BlockModel::Buffer buf;
489 void Chunk::CheckUpdate() noexcept {
495 void Chunk::Update() noexcept {
496 int vtx_count = 0, idx_count = 0;
497 for (const auto &block : blocks) {
498 const Shape *shape = Type(block).shape;
499 vtx_count += shape->VertexCount();
500 idx_count += shape->VertexIndexCount();
503 buf.Reserve(vtx_count, idx_count);
506 BlockModel::Index vtx_counter = 0;
507 for (size_t z = 0; z < depth; ++z) {
508 for (size_t y = 0; y < height; ++y) {
509 for (size_t x = 0; x < width; ++x, ++idx) {
510 const BlockType &type = Type(BlockAt(idx));
511 const Pos pos(x, y, z);
513 if (!type.visible || Obstructed(pos).All()) continue;
515 type.FillBlockModel(buf, ToTransform(pos, idx), vtx_counter);
516 size_t vtx_begin = vtx_counter;
517 vtx_counter += type.shape->VertexCount();
519 for (size_t vtx = vtx_begin; vtx < vtx_counter; ++vtx) {
520 buf.lights.emplace_back(GetVertexLight(
523 type.shape->VertexNormal(vtx - vtx_begin, BlockAt(idx).Transform())
534 Block::FaceSet Chunk::Obstructed(const Pos &pos) const noexcept {
535 Block::FaceSet result;
537 for (int f = 0; f < Block::FACE_COUNT; ++f) {
538 Block::Face face = Block::Face(f);
539 BlockLookup next(const_cast<Chunk *>(this), pos, face);
540 if (next && next.GetType().FaceFilled(next.GetBlock(), Block::Opposite(face))) {
548 glm::mat4 Chunk::ToTransform(const Pos &pos, int idx) const noexcept {
549 return glm::translate(ToCoords(pos)) * BlockAt(idx).Transform();
553 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p) noexcept
555 while (pos.x >= Chunk::width) {
556 if (chunk->HasNeighbor(Block::FACE_RIGHT)) {
557 chunk = &chunk->GetNeighbor(Block::FACE_RIGHT);
558 pos.x -= Chunk::width;
565 if (chunk->HasNeighbor(Block::FACE_LEFT)) {
566 chunk = &chunk->GetNeighbor(Block::FACE_LEFT);
567 pos.x += Chunk::width;
573 while (pos.y >= Chunk::height) {
574 if (chunk->HasNeighbor(Block::FACE_UP)) {
575 chunk = &chunk->GetNeighbor(Block::FACE_UP);
576 pos.y -= Chunk::height;
583 if (chunk->HasNeighbor(Block::FACE_DOWN)) {
584 chunk = &chunk->GetNeighbor(Block::FACE_DOWN);
585 pos.y += Chunk::height;
591 while (pos.z >= Chunk::depth) {
592 if (chunk->HasNeighbor(Block::FACE_FRONT)) {
593 chunk = &chunk->GetNeighbor(Block::FACE_FRONT);
594 pos.z -= Chunk::depth;
601 if (chunk->HasNeighbor(Block::FACE_BACK)) {
602 chunk = &chunk->GetNeighbor(Block::FACE_BACK);
603 pos.z += Chunk::depth;
611 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face face) noexcept
613 pos += Block::FaceNormal(face);
614 if (!Chunk::InBounds(pos)) {
615 pos -= Block::FaceNormal(face) * Chunk::Extent();
616 chunk = &chunk->GetNeighbor(face);
621 ChunkLoader::ChunkLoader(const Config &config, const BlockTypeRegistry ®, const Generator &gen) noexcept
628 , load_dist(config.load_dist)
629 , unload_dist(config.unload_dist) {
637 explicit ChunkLess(const Chunk::Pos &base) noexcept
640 bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const noexcept {
641 Chunk::Pos da(base - a);
642 Chunk::Pos db(base - b);
644 da.x * da.x + da.y * da.y + da.z * da.z <
645 db.x * db.x + db.y * db.y + db.z * db.z;
654 void ChunkLoader::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
655 for (int z = from.z; z < to.z; ++z) {
656 for (int y = from.y; y < to.y; ++y) {
657 for (int x = from.x; x < to.x; ++x) {
658 Chunk::Pos pos(x, y, z);
661 } else if (pos == base) {
665 // for (int i = 0; i < 16; ++i) {
666 // for (int j = 0; j < 16; ++j) {
667 // loaded.back().SetBlock(Chunk::Pos{ i, j, 0 }, Block(1));
668 // loaded.back().SetBlock(Chunk::Pos{ i, j, 15 }, Block(1));
669 // loaded.back().SetBlock(Chunk::Pos{ 0, j, i }, Block(1));
670 // loaded.back().SetBlock(Chunk::Pos{ 15, j, i }, Block(1));
673 // loaded.back().SetBlock(Chunk::Pos{ 1, 0, 1 }, Block(13));
674 // loaded.back().SetBlock(Chunk::Pos{ 14, 0, 1 }, Block(13));
675 // loaded.back().SetBlock(Chunk::Pos{ 1, 0, 14 }, Block(13));
676 // loaded.back().SetBlock(Chunk::Pos{ 14, 0, 14 }, Block(13));
677 // loaded.back().SetBlock(Chunk::Pos{ 1, 15, 1 }, Block(13));
678 // loaded.back().SetBlock(Chunk::Pos{ 14, 15, 1 }, Block(13));
679 // loaded.back().SetBlock(Chunk::Pos{ 1, 15, 14 }, Block(13));
680 // loaded.back().SetBlock(Chunk::Pos{ 14, 15, 14 }, Block(13));
681 // loaded.back().SetBlock(Chunk::Pos{ 7, 7, 0 }, Block(13));
682 // loaded.back().SetBlock(Chunk::Pos{ 8, 7, 0 }, Block(13));
683 // loaded.back().SetBlock(Chunk::Pos{ 7, 8, 0 }, Block(13));
684 // loaded.back().SetBlock(Chunk::Pos{ 8, 8, 0 }, Block(13));
685 // loaded.back().SetBlock(Chunk::Pos{ 7, 7, 15 }, Block(13));
686 // loaded.back().SetBlock(Chunk::Pos{ 8, 7, 15 }, Block(13));
687 // loaded.back().SetBlock(Chunk::Pos{ 7, 8, 15 }, Block(13));
688 // loaded.back().SetBlock(Chunk::Pos{ 8, 8, 15 }, Block(13));
689 // loaded.back().SetBlock(Chunk::Pos{ 0, 7, 7 }, Block(13));
690 // loaded.back().SetBlock(Chunk::Pos{ 0, 7, 8 }, Block(13));
691 // loaded.back().SetBlock(Chunk::Pos{ 0, 8, 7 }, Block(13));
692 // loaded.back().SetBlock(Chunk::Pos{ 0, 8, 8 }, Block(13));
693 // loaded.back().SetBlock(Chunk::Pos{ 15, 7, 7 }, Block(13));
694 // loaded.back().SetBlock(Chunk::Pos{ 15, 7, 8 }, Block(13));
695 // loaded.back().SetBlock(Chunk::Pos{ 15, 8, 7 }, Block(13));
696 // loaded.back().SetBlock(Chunk::Pos{ 15, 8, 8 }, Block(13));
697 // loaded.back().Invalidate();
698 // loaded.back().CheckUpdate();
700 // orientation testing
701 // for (int i = 0; i < Block::FACE_COUNT; ++i) {
702 // for (int j = 0; j < Block::TURN_COUNT; ++j) {
703 // loaded.back().BlockAt(512 * j + 2 * i) = Block(3 * (j + 1), Block::Face(i), Block::Turn(j));
706 // loaded.back().Invalidate();
707 // loaded.back().CheckUpdate();
709 to_generate.emplace_back(pos);
714 to_generate.sort(ChunkLess(base));
717 Chunk &ChunkLoader::Generate(const Chunk::Pos &pos) {
718 loaded.emplace_back(reg);
719 Chunk &chunk = loaded.back();
726 void ChunkLoader::Insert(Chunk &chunk) noexcept {
727 for (Chunk &other : loaded) {
728 chunk.SetNeighbor(other);
732 void ChunkLoader::Remove(Chunk &chunk) noexcept {
736 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) noexcept {
737 for (Chunk &chunk : loaded) {
738 if (chunk.Position() == pos) {
745 bool ChunkLoader::Queued(const Chunk::Pos &pos) noexcept {
746 for (const Chunk::Pos &chunk : to_generate) {
754 bool ChunkLoader::Known(const Chunk::Pos &pos) noexcept {
755 if (Loaded(pos)) return true;
759 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
760 Chunk *chunk = Loaded(pos);
765 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
767 to_generate.erase(iter);
772 return Generate(pos);
775 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
776 if (new_base == base) {
781 // unload far away chunks
782 for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
783 if (std::abs(base.x - iter->Position().x) > unload_dist
784 || std::abs(base.y - iter->Position().y) > unload_dist
785 || std::abs(base.z - iter->Position().z) > unload_dist) {
789 to_free.splice(to_free.end(), loaded, saved);
794 // abort far away queued chunks
795 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
796 if (std::abs(base.x - iter->x) > unload_dist
797 || std::abs(base.y - iter->y) > unload_dist
798 || std::abs(base.z - iter->z) > unload_dist) {
799 iter = to_generate.erase(iter);
804 // add missing new chunks
805 GenerateSurrounding(base);
808 void ChunkLoader::GenerateSurrounding(const Chunk::Pos &pos) {
809 const Chunk::Pos offset(load_dist, load_dist, load_dist);
810 Generate(pos - offset, pos + offset);
813 void ChunkLoader::Update() {
814 if (to_generate.empty()) {
818 Chunk::Pos pos(to_generate.front());
819 to_generate.pop_front();
821 for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
822 if (iter->Position() == pos) {
824 loaded.splice(loaded.end(), to_free, iter);
829 if (to_free.empty()) {
830 loaded.emplace_back(reg);
832 to_free.front().ClearNeighbors();
833 loaded.splice(loaded.end(), to_free, to_free.begin());
835 Chunk &chunk = loaded.back();