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 bool HasNext(Block::Face face) noexcept {
69 const BlockLookup next(chunk, pos, face);
70 return next && !next.GetType().block_light;
72 SetNode GetNext(Block::Face face) noexcept {
73 const BlockLookup next(chunk, pos, face);
74 return SetNode(&next.GetChunk(), next.GetBlockPos());
84 UnsetNode(Chunk *chunk, Chunk::Pos pos)
85 : SetNode(chunk, pos), level(Get()) { }
87 UnsetNode(const SetNode &set)
88 : SetNode(set), level(Get()) { }
91 bool HasNext(Block::Face face) noexcept {
92 const BlockLookup next(chunk, pos, face);
95 UnsetNode GetNext(Block::Face face) noexcept { return UnsetNode(SetNode::GetNext(face)); }
99 std::queue<SetNode> light_queue;
100 std::queue<UnsetNode> dark_queue;
102 void work_light() noexcept {
103 while (!light_queue.empty()) {
104 SetNode node = light_queue.front();
107 int level = node.Get() - 1;
108 for (int face = 0; face < Block::FACE_COUNT; ++face) {
109 if (node.HasNext(Block::Face(face))) {
110 SetNode other = node.GetNext(Block::Face(face));
111 if (other.Get() < level) {
113 light_queue.emplace(other);
120 void work_dark() noexcept {
121 while (!dark_queue.empty()) {
122 UnsetNode node = dark_queue.front();
125 for (int face = 0; face < Block::FACE_COUNT; ++face) {
126 if (node.HasNext(Block::Face(face))) {
127 UnsetNode other = node.GetNext(Block::Face(face));
128 // TODO: if there a light source here with the same level this will err
129 if (other.Get() != 0 && other.Get() < node.level) {
131 dark_queue.emplace(other);
133 light_queue.emplace(other);
142 void Chunk::SetBlock(int index, const Block &block) noexcept {
143 const BlockType &old_type = Type(blocks[index]);
144 const BlockType &new_type = Type(block);
146 blocks[index] = block;
148 if (&old_type == &new_type) return;
150 if (new_type.luminosity > old_type.luminosity) {
152 SetLight(index, new_type.luminosity);
153 light_queue.emplace(this, ToPos(index));
155 } else if (new_type.luminosity < old_type.luminosity) {
157 dark_queue.emplace(this, ToPos(index));
160 SetLight(index, new_type.luminosity);
161 light_queue.emplace(this, ToPos(index));
163 } else if (new_type.block_light && !old_type.block_light) {
165 if (GetLight(index) > 0) {
166 dark_queue.emplace(this, ToPos(index));
171 } else if (!new_type.block_light && old_type.block_light) {
174 Pos pos(ToPos(index));
175 for (int face = 0; face < Block::FACE_COUNT; ++face) {
176 BlockLookup next_block(this, pos, Block::Face(face));
178 level = std::max(level, next_block.GetLight());
182 SetLight(index, level - 1);
183 light_queue.emplace(this, pos);
191 // propagate light from a's edge to b
193 Chunk &a, const Chunk::Pos &a_pos,
194 Chunk &b, const Chunk::Pos &b_pos
196 if (a.GetLight(a_pos) > 1) {
197 const BlockType &b_type = b.Type(Chunk::ToIndex(b_pos));
198 if (!b_type.block_light) {
199 light_queue.emplace(&a, a_pos);
201 if (b_type.visible) {
209 void Chunk::SetNeighbor(Chunk &other) noexcept {
210 if (other.position == position + Pos(-1, 0, 0)) {
211 if (neighbor[Block::FACE_LEFT] != &other) {
212 neighbor[Block::FACE_LEFT] = &other;
213 other.neighbor[Block::FACE_RIGHT] = this;
214 for (int z = 0; z < depth; ++z) {
215 for (int y = 0; y < height; ++y) {
217 Pos other_pos(width - 1, y, z);
218 edge_light(*this, my_pos, other, other_pos);
219 edge_light(other, other_pos, *this, my_pos);
224 } else if (other.position == position + Pos(1, 0, 0)) {
225 if (neighbor[Block::FACE_RIGHT] != &other) {
226 neighbor[Block::FACE_RIGHT] = &other;
227 other.neighbor[Block::FACE_LEFT] = this;
228 for (int z = 0; z < depth; ++z) {
229 for (int y = 0; y < height; ++y) {
230 Pos my_pos(width - 1, y, z);
231 Pos other_pos(0, y, z);
232 edge_light(*this, my_pos, other, other_pos);
233 edge_light(other, other_pos, *this, my_pos);
238 } else if (other.position == position + Pos(0, -1, 0)) {
239 if (neighbor[Block::FACE_DOWN] != &other) {
240 neighbor[Block::FACE_DOWN] = &other;
241 other.neighbor[Block::FACE_UP] = this;
242 for (int z = 0; z < depth; ++z) {
243 for (int x = 0; x < width; ++x) {
245 Pos other_pos(x, height - 1, z);
246 edge_light(*this, my_pos, other, other_pos);
247 edge_light(other, other_pos, *this, my_pos);
252 } else if (other.position == position + Pos(0, 1, 0)) {
253 if (neighbor[Block::FACE_UP] != &other) {
254 neighbor[Block::FACE_UP] = &other;
255 other.neighbor[Block::FACE_DOWN] = this;
256 for (int z = 0; z < depth; ++z) {
257 for (int x = 0; x < width; ++x) {
258 Pos my_pos(x, height - 1, z);
259 Pos other_pos(x, 0, z);
260 edge_light(*this, my_pos, other, other_pos);
261 edge_light(other, other_pos, *this, my_pos);
266 } else if (other.position == position + Pos(0, 0, -1)) {
267 if (neighbor[Block::FACE_BACK] != &other) {
268 neighbor[Block::FACE_BACK] = &other;
269 other.neighbor[Block::FACE_FRONT] = this;
270 for (int y = 0; y < height; ++y) {
271 for (int x = 0; x < width; ++x) {
273 Pos other_pos(x, y, depth - 1);
274 edge_light(*this, my_pos, other, other_pos);
275 edge_light(other, other_pos, *this, my_pos);
280 } else if (other.position == position + Pos(0, 0, 1)) {
281 if (neighbor[Block::FACE_FRONT] != &other) {
282 neighbor[Block::FACE_FRONT] = &other;
283 other.neighbor[Block::FACE_BACK] = this;
284 for (int y = 0; y < height; ++y) {
285 for (int x = 0; x < width; ++x) {
286 Pos my_pos(x, y, depth - 1);
287 Pos other_pos(x, y, 0);
288 edge_light(*this, my_pos, other, other_pos);
289 edge_light(other, other_pos, *this, my_pos);
297 void Chunk::ClearNeighbors() noexcept {
298 for (int face = 0; face < Block::FACE_COUNT; ++face) {
299 if (neighbor[face]) {
300 neighbor[face]->neighbor[Block::Opposite(Block::Face(face))] = nullptr;
301 neighbor[face] = nullptr;
307 void Chunk::SetLight(int index, int level) noexcept {
308 if (light[index] != level) {
309 light[index] = level;
314 int Chunk::GetLight(int index) const noexcept {
318 float Chunk::GetVertexLight(const Pos &pos, const BlockModel::Position &vtx, const Model::Normal &norm) const noexcept {
319 int index = ToIndex(pos);
320 float light = GetLight(index);
322 Block::Face direct_face(Block::NormalFace(norm));
324 BlockLookup direct(const_cast<Chunk *>(this), pos, Block::NormalFace(norm));
326 float direct_light = direct.GetLight();
327 if (direct_light > light) {
328 light = direct_light;
334 if (Type(BlockAt(index)).luminosity > 0 || direct.GetType().block_light) {
339 switch (Block::Axis(direct_face)) {
341 edge[0] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
342 edge[1] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
345 edge[0] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
346 edge[1] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
349 edge[0] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
350 edge[1] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
357 BlockLookup next[2] = {
358 direct.Next(edge[0]),
359 direct.Next(edge[1]),
363 if (next[0].GetType().block_light) {
366 light += next[0].GetLight();
371 if (next[1].GetType().block_light) {
374 light += next[1].GetLight();
380 BlockLookup corner = next[0].Next(edge[1]);
382 if (corner.GetType().block_light) {
385 light += corner.GetLight();
389 } else if (next[1]) {
390 BlockLookup corner = next[1].Next(edge[0]);
392 if (corner.GetType().block_light) {
395 light += corner.GetLight();
404 return (light / num) - (occlusion * 0.8f);
408 bool Chunk::IsSurface(const Pos &pos) const noexcept {
409 const Block &block = BlockAt(pos);
410 if (!Type(block).visible) {
413 for (int face = 0; face < Block::FACE_COUNT; ++face) {
414 BlockLookup next = BlockLookup(const_cast<Chunk *>(this), pos, Block::Face(face));
415 if (!next || !next.GetType().visible) {
423 void Chunk::Draw() noexcept {
431 bool Chunk::Intersection(
440 dist = std::numeric_limits<float>::infinity();
441 for (int z = 0; z < depth; ++z) {
442 for (int y = 0; y < height; ++y) {
443 for (int x = 0; x < width; ++x, ++idx) {
444 const BlockType &type = Type(idx);
450 if (type.shape->Intersects(ray, M * ToTransform(Pos(x, y, z), idx), cur_dist, cur_norm)) {
451 if (cur_dist < dist) {
464 normal = glm::vec3(BlockAt(blkid).Transform() * glm::vec4(normal, 0.0f));
469 bool Chunk::Intersection(
471 const glm::mat4 &Mbox,
472 const glm::mat4 &Mchunk,
473 std::vector<WorldCollision> &col
479 if (!blank::Intersection(box, Mbox, Bounds(), Mchunk, penetration, normal)) {
482 for (int idx = 0, z = 0; z < depth; ++z) {
483 for (int y = 0; y < height; ++y) {
484 for (int x = 0; x < width; ++x, ++idx) {
485 const BlockType &type = Type(idx);
486 if (!type.collision) {
489 if (type.shape->Intersects(Mchunk * ToTransform(Pos(x, y, z), idx), box, Mbox, penetration, normal)) {
490 col.emplace_back(this, idx, penetration, normal);
502 BlockModel::Buffer buf;
506 void Chunk::CheckUpdate() noexcept {
512 void Chunk::Update() noexcept {
513 int vtx_count = 0, idx_count = 0;
514 for (const auto &block : blocks) {
515 const Shape *shape = Type(block).shape;
516 vtx_count += shape->VertexCount();
517 idx_count += shape->VertexIndexCount();
520 buf.Reserve(vtx_count, idx_count);
523 BlockModel::Index vtx_counter = 0;
524 for (size_t z = 0; z < depth; ++z) {
525 for (size_t y = 0; y < height; ++y) {
526 for (size_t x = 0; x < width; ++x, ++idx) {
527 const BlockType &type = Type(BlockAt(idx));
528 const Pos pos(x, y, z);
530 if (!type.visible || Obstructed(pos).All()) continue;
532 type.FillBlockModel(buf, ToTransform(pos, idx), vtx_counter);
533 size_t vtx_begin = vtx_counter;
534 vtx_counter += type.shape->VertexCount();
536 for (size_t vtx = vtx_begin; vtx < vtx_counter; ++vtx) {
537 buf.lights.emplace_back(GetVertexLight(
540 type.shape->VertexNormal(vtx - vtx_begin, BlockAt(idx).Transform())
551 Block::FaceSet Chunk::Obstructed(const Pos &pos) const noexcept {
552 Block::FaceSet result;
554 for (int f = 0; f < Block::FACE_COUNT; ++f) {
555 Block::Face face = Block::Face(f);
556 BlockLookup next(const_cast<Chunk *>(this), pos, face);
557 if (next && next.GetType().FaceFilled(next.GetBlock(), Block::Opposite(face))) {
565 glm::mat4 Chunk::ToTransform(const Pos &pos, int idx) const noexcept {
566 return glm::translate(ToCoords(pos)) * BlockAt(idx).Transform();
570 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p) noexcept
572 while (pos.x >= Chunk::width) {
573 if (chunk->HasNeighbor(Block::FACE_RIGHT)) {
574 chunk = &chunk->GetNeighbor(Block::FACE_RIGHT);
575 pos.x -= Chunk::width;
582 if (chunk->HasNeighbor(Block::FACE_LEFT)) {
583 chunk = &chunk->GetNeighbor(Block::FACE_LEFT);
584 pos.x += Chunk::width;
590 while (pos.y >= Chunk::height) {
591 if (chunk->HasNeighbor(Block::FACE_UP)) {
592 chunk = &chunk->GetNeighbor(Block::FACE_UP);
593 pos.y -= Chunk::height;
600 if (chunk->HasNeighbor(Block::FACE_DOWN)) {
601 chunk = &chunk->GetNeighbor(Block::FACE_DOWN);
602 pos.y += Chunk::height;
608 while (pos.z >= Chunk::depth) {
609 if (chunk->HasNeighbor(Block::FACE_FRONT)) {
610 chunk = &chunk->GetNeighbor(Block::FACE_FRONT);
611 pos.z -= Chunk::depth;
618 if (chunk->HasNeighbor(Block::FACE_BACK)) {
619 chunk = &chunk->GetNeighbor(Block::FACE_BACK);
620 pos.z += Chunk::depth;
628 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face face) noexcept
630 pos += Block::FaceNormal(face);
631 if (!Chunk::InBounds(pos)) {
632 pos -= Block::FaceNormal(face) * Chunk::Extent();
633 chunk = &chunk->GetNeighbor(face);
638 ChunkLoader::ChunkLoader(const Config &config, const BlockTypeRegistry ®, const Generator &gen) noexcept
645 , gen_timer(config.gen_limit)
646 , load_dist(config.load_dist)
647 , unload_dist(config.unload_dist) {
655 explicit ChunkLess(const Chunk::Pos &base) noexcept
658 bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const noexcept {
659 Chunk::Pos da(base - a);
660 Chunk::Pos db(base - b);
662 da.x * da.x + da.y * da.y + da.z * da.z <
663 db.x * db.x + db.y * db.y + db.z * db.z;
672 void ChunkLoader::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
673 for (int z = from.z; z < to.z; ++z) {
674 for (int y = from.y; y < to.y; ++y) {
675 for (int x = from.x; x < to.x; ++x) {
676 Chunk::Pos pos(x, y, z);
679 } else if (pos == base) {
683 // for (int i = 0; i < 16; ++i) {
684 // for (int j = 0; j < 16; ++j) {
685 // loaded.back().SetBlock(Chunk::Pos{ i, j, 0 }, Block(1));
686 // loaded.back().SetBlock(Chunk::Pos{ i, j, 15 }, Block(1));
687 // loaded.back().SetBlock(Chunk::Pos{ 0, j, i }, Block(1));
688 // loaded.back().SetBlock(Chunk::Pos{ 15, j, i }, Block(1));
691 // loaded.back().SetBlock(Chunk::Pos{ 1, 0, 1 }, Block(13));
692 // loaded.back().SetBlock(Chunk::Pos{ 14, 0, 1 }, Block(13));
693 // loaded.back().SetBlock(Chunk::Pos{ 1, 0, 14 }, Block(13));
694 // loaded.back().SetBlock(Chunk::Pos{ 14, 0, 14 }, Block(13));
695 // loaded.back().SetBlock(Chunk::Pos{ 1, 15, 1 }, Block(13));
696 // loaded.back().SetBlock(Chunk::Pos{ 14, 15, 1 }, Block(13));
697 // loaded.back().SetBlock(Chunk::Pos{ 1, 15, 14 }, Block(13));
698 // loaded.back().SetBlock(Chunk::Pos{ 14, 15, 14 }, Block(13));
699 // loaded.back().SetBlock(Chunk::Pos{ 7, 7, 0 }, Block(13));
700 // loaded.back().SetBlock(Chunk::Pos{ 8, 7, 0 }, Block(13));
701 // loaded.back().SetBlock(Chunk::Pos{ 7, 8, 0 }, Block(13));
702 // loaded.back().SetBlock(Chunk::Pos{ 8, 8, 0 }, Block(13));
703 // loaded.back().SetBlock(Chunk::Pos{ 7, 7, 15 }, Block(13));
704 // loaded.back().SetBlock(Chunk::Pos{ 8, 7, 15 }, Block(13));
705 // loaded.back().SetBlock(Chunk::Pos{ 7, 8, 15 }, Block(13));
706 // loaded.back().SetBlock(Chunk::Pos{ 8, 8, 15 }, Block(13));
707 // loaded.back().SetBlock(Chunk::Pos{ 0, 7, 7 }, Block(13));
708 // loaded.back().SetBlock(Chunk::Pos{ 0, 7, 8 }, Block(13));
709 // loaded.back().SetBlock(Chunk::Pos{ 0, 8, 7 }, Block(13));
710 // loaded.back().SetBlock(Chunk::Pos{ 0, 8, 8 }, Block(13));
711 // loaded.back().SetBlock(Chunk::Pos{ 15, 7, 7 }, Block(13));
712 // loaded.back().SetBlock(Chunk::Pos{ 15, 7, 8 }, Block(13));
713 // loaded.back().SetBlock(Chunk::Pos{ 15, 8, 7 }, Block(13));
714 // loaded.back().SetBlock(Chunk::Pos{ 15, 8, 8 }, Block(13));
715 // loaded.back().Invalidate();
716 // loaded.back().CheckUpdate();
718 // orientation testing
719 // for (int i = 0; i < Block::FACE_COUNT; ++i) {
720 // for (int j = 0; j < Block::TURN_COUNT; ++j) {
721 // loaded.back().BlockAt(512 * j + 2 * i) = Block(3 * (j + 1), Block::Face(i), Block::Turn(j));
724 // loaded.back().Invalidate();
725 // loaded.back().CheckUpdate();
727 to_generate.emplace_back(pos);
732 to_generate.sort(ChunkLess(base));
735 Chunk &ChunkLoader::Generate(const Chunk::Pos &pos) {
736 loaded.emplace_back(reg);
737 Chunk &chunk = loaded.back();
744 void ChunkLoader::Insert(Chunk &chunk) noexcept {
745 for (Chunk &other : loaded) {
746 chunk.SetNeighbor(other);
750 std::list<Chunk>::iterator ChunkLoader::Remove(std::list<Chunk>::iterator chunk) noexcept {
751 // fetch next entry while chunk's still in the list
752 std::list<Chunk>::iterator next = chunk;
754 // unlink neighbors so they won't reference a dead chunk
755 chunk->ClearNeighbors();
756 // and move it from loaded to free list
757 to_free.splice(to_free.end(), loaded, chunk);
761 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) noexcept {
762 for (Chunk &chunk : loaded) {
763 if (chunk.Position() == pos) {
770 bool ChunkLoader::Queued(const Chunk::Pos &pos) noexcept {
771 for (const Chunk::Pos &chunk : to_generate) {
779 bool ChunkLoader::Known(const Chunk::Pos &pos) noexcept {
780 if (Loaded(pos)) return true;
784 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
785 Chunk *chunk = Loaded(pos);
790 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
792 to_generate.erase(iter);
797 return Generate(pos);
800 bool ChunkLoader::OutOfRange(const Chunk::Pos &pos) const noexcept {
801 return std::abs(base.x - pos.x) > unload_dist
802 || std::abs(base.y - pos.y) > unload_dist
803 || std::abs(base.z - pos.z) > unload_dist;
806 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
807 if (new_base == base) {
812 // unload far away chunks
813 for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
814 if (OutOfRange(*iter)) {
820 // abort far away queued chunks
821 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
822 if (OutOfRange(*iter)) {
823 iter = to_generate.erase(iter);
828 // add missing new chunks
829 GenerateSurrounding(base);
832 void ChunkLoader::GenerateSurrounding(const Chunk::Pos &pos) {
833 const Chunk::Pos offset(load_dist, load_dist, load_dist);
834 Generate(pos - offset, pos + offset);
837 void ChunkLoader::Update(int dt) {
838 // check if a chunk generation is scheduled for this frame
839 // and if there's a chunk waiting to be generated
840 gen_timer.Update(dt);
841 if (!gen_timer.Hit() || to_generate.empty()) {
845 // take position of next chunk in queue
846 Chunk::Pos pos(to_generate.front());
847 to_generate.pop_front();
849 // look if the same chunk was already generated and still lingering
850 for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
851 if (iter->Position() == pos) {
852 loaded.splice(loaded.end(), to_free, iter);
853 Insert(loaded.back());
858 // if the free list is empty, allocate a new chunk
859 // otherwise clear an unused one
860 if (to_free.empty()) {
861 loaded.emplace_back(reg);
863 to_free.front().ClearNeighbors();
864 loaded.splice(loaded.end(), to_free, to_free.begin());
867 Chunk &chunk = loaded.back();