3 #include "generator.hpp"
12 Chunk::Chunk(const BlockTypeRegistry &types) noexcept
23 Chunk::Chunk(Chunk &&other) noexcept
25 , model(std::move(other.model))
26 , position(other.position)
27 , dirty(other.dirty) {
28 std::copy(other.neighbor, other.neighbor + sizeof(neighbor), neighbor);
29 std::copy(other.blocks, other.blocks + sizeof(blocks), blocks);
30 std::copy(other.light, other.light + sizeof(light), light);
33 Chunk &Chunk::operator =(Chunk &&other) noexcept {
35 std::copy(other.neighbor, other.neighbor + sizeof(neighbor), neighbor);
36 std::copy(other.blocks, other.blocks + sizeof(blocks), blocks);
37 std::copy(other.light, other.light + sizeof(light), light);
38 model = std::move(other.model);
39 position = other.position;
52 SetNode(Chunk *chunk, Chunk::Pos pos)
53 : chunk(chunk), pos(pos) { }
55 int Get() const noexcept { return chunk->GetLight(pos); }
56 void Set(int level) noexcept { chunk->SetLight(pos, level); }
58 bool HasNext(Block::Face face) noexcept {
59 const BlockLookup next(chunk, pos, face);
60 return next && !next.GetType().block_light;
62 SetNode GetNext(Block::Face face) noexcept {
63 const BlockLookup next(chunk, pos, face);
64 return SetNode(&next.GetChunk(), next.GetBlockPos());
74 UnsetNode(Chunk *chunk, Chunk::Pos pos)
75 : SetNode(chunk, pos), level(Get()) { }
77 UnsetNode(const SetNode &set)
78 : SetNode(set), level(Get()) { }
81 bool HasNext(Block::Face face) noexcept {
82 const BlockLookup next(chunk, pos, face);
85 UnsetNode GetNext(Block::Face face) noexcept { return UnsetNode(SetNode::GetNext(face)); }
89 std::queue<SetNode> light_queue;
90 std::queue<UnsetNode> dark_queue;
92 void work_light() noexcept {
93 while (!light_queue.empty()) {
94 SetNode node = light_queue.front();
97 int level = node.Get() - 1;
98 for (int face = 0; face < Block::FACE_COUNT; ++face) {
99 if (node.HasNext(Block::Face(face))) {
100 SetNode other = node.GetNext(Block::Face(face));
101 if (other.Get() < level) {
103 light_queue.emplace(other);
110 void work_dark() noexcept {
111 while (!dark_queue.empty()) {
112 UnsetNode node = dark_queue.front();
115 for (int face = 0; face < Block::FACE_COUNT; ++face) {
116 if (node.HasNext(Block::Face(face))) {
117 UnsetNode other = node.GetNext(Block::Face(face));
118 // TODO: if there a light source here with the same level this will err
119 if (other.Get() != 0 && other.Get() < node.level) {
121 dark_queue.emplace(other);
123 light_queue.emplace(other);
132 void Chunk::SetBlock(int index, const Block &block) noexcept {
133 const BlockType &old_type = Type(blocks[index]);
134 const BlockType &new_type = Type(block);
136 blocks[index] = block;
138 if (&old_type == &new_type) return;
140 if (new_type.luminosity > old_type.luminosity) {
142 SetLight(index, new_type.luminosity);
143 light_queue.emplace(this, ToPos(index));
145 } else if (new_type.luminosity < old_type.luminosity) {
147 dark_queue.emplace(this, ToPos(index));
150 SetLight(index, new_type.luminosity);
151 light_queue.emplace(this, ToPos(index));
153 } else if (new_type.block_light && !old_type.block_light) {
155 if (GetLight(index) > 0) {
156 dark_queue.emplace(this, ToPos(index));
161 } else if (!new_type.block_light && old_type.block_light) {
164 for (int face = 0; face < Block::FACE_COUNT; ++face) {
165 BlockLookup next_block(this, ToPos(index), Block::Face(face));
167 level = std::min(level, next_block.GetLight());
171 SetLight(index, level - 1);
172 light_queue.emplace(this, ToPos(index));
178 void Chunk::SetNeighbor(Chunk &other) noexcept {
179 if (other.position == position + Pos(-1, 0, 0)) {
180 if (neighbor[Block::FACE_LEFT] != &other) {
181 neighbor[Block::FACE_LEFT] = &other;
182 other.neighbor[Block::FACE_RIGHT] = this;
183 for (int z = 0; z < Depth(); ++z) {
184 for (int y = 0; y < Height(); ++y) {
186 Pos other_pos(Width() - 1, y, z);
187 if (GetLight(my_pos) > 0) {
188 light_queue.emplace(this, my_pos);
190 if (other.GetLight(other_pos) > 0) {
191 light_queue.emplace(&other, other_pos);
197 } else if (other.position == position + Pos(1, 0, 0)) {
198 if (neighbor[Block::FACE_RIGHT] != &other) {
199 neighbor[Block::FACE_RIGHT] = &other;
200 other.neighbor[Block::FACE_LEFT] = this;
201 for (int z = 0; z < Depth(); ++z) {
202 for (int y = 0; y < Height(); ++y) {
203 Pos my_pos(Width() - 1, y, z);
204 Pos other_pos(0, y, z);
205 if (GetLight(my_pos) > 0) {
206 light_queue.emplace(this, my_pos);
208 if (other.GetLight(other_pos) > 0) {
209 light_queue.emplace(&other, other_pos);
215 } else if (other.position == position + Pos(0, -1, 0)) {
216 if (neighbor[Block::FACE_DOWN] != &other) {
217 neighbor[Block::FACE_DOWN] = &other;
218 other.neighbor[Block::FACE_UP] = this;
219 for (int z = 0; z < Depth(); ++z) {
220 for (int x = 0; x < Width(); ++x) {
222 Pos other_pos(x, Height() - 1, z);
223 if (GetLight(my_pos) > 0) {
224 light_queue.emplace(this, my_pos);
226 if (other.GetLight(other_pos) > 0) {
227 light_queue.emplace(&other, other_pos);
233 } else if (other.position == position + Pos(0, 1, 0)) {
234 if (neighbor[Block::FACE_UP] != &other) {
235 neighbor[Block::FACE_UP] = &other;
236 other.neighbor[Block::FACE_DOWN] = this;
237 for (int z = 0; z < Depth(); ++z) {
238 for (int x = 0; x < Width(); ++x) {
239 Pos my_pos(x, Height() - 1, z);
240 Pos other_pos(x, 0, z);
241 if (GetLight(my_pos) > 0) {
242 light_queue.emplace(this, my_pos);
244 if (other.GetLight(other_pos) > 0) {
245 light_queue.emplace(&other, other_pos);
251 } else if (other.position == position + Pos(0, 0, -1)) {
252 if (neighbor[Block::FACE_BACK] != &other) {
253 neighbor[Block::FACE_BACK] = &other;
254 other.neighbor[Block::FACE_FRONT] = this;
255 for (int y = 0; y < Height(); ++y) {
256 for (int x = 0; x < Width(); ++x) {
258 Pos other_pos(x, y, Depth() - 1);
259 if (GetLight(my_pos) > 0) {
260 light_queue.emplace(this, my_pos);
262 if (other.GetLight(other_pos) > 0) {
263 light_queue.emplace(&other, other_pos);
269 } else if (other.position == position + Pos(0, 0, 1)) {
270 if (neighbor[Block::FACE_FRONT] != &other) {
271 neighbor[Block::FACE_FRONT] = &other;
272 other.neighbor[Block::FACE_BACK] = this;
273 for (int y = 0; y < Height(); ++y) {
274 for (int x = 0; x < Width(); ++x) {
275 Pos my_pos(x, y, Depth() - 1);
276 Pos other_pos(x, y, 0);
277 if (GetLight(my_pos) > 0) {
278 light_queue.emplace(this, my_pos);
280 if (other.GetLight(other_pos) > 0) {
281 light_queue.emplace(&other, other_pos);
290 void Chunk::ClearNeighbors() noexcept {
291 for (int i = 0; i < Block::FACE_COUNT; ++i) {
292 neighbor[i] = nullptr;
296 void Chunk::Unlink() noexcept {
297 for (int face = 0; face < Block::FACE_COUNT; ++face) {
298 if (neighbor[face]) {
299 neighbor[face]->neighbor[Block::Opposite(Block::Face(face))] = nullptr;
304 void Chunk::Relink() noexcept {
305 for (int face = 0; face < Block::FACE_COUNT; ++face) {
306 if (neighbor[face]) {
307 neighbor[face]->neighbor[Block::Opposite(Block::Face(face))] = this;
313 void Chunk::SetLight(int index, int level) noexcept {
314 if (light[index] != level) {
315 light[index] = level;
320 int Chunk::GetLight(int index) const noexcept {
324 float Chunk::GetVertexLight(int index, const BlockModel::Position &vtx, const Model::Normal &norm) const noexcept {
325 float light = GetLight(index);
326 Chunk::Pos pos(ToPos(index));
328 Block::Face direct_face(Block::NormalFace(norm));
330 BlockLookup direct(const_cast<Chunk *>(this), pos, Block::NormalFace(norm));
332 float direct_light = direct.GetLight();
333 if (direct_light > light) {
334 light = direct_light;
338 // cheap alternative until AO etc are implemented
339 // to tell the faces apart
341 if (direct_face == Block::FACE_LEFT || direct_face == Block::FACE_RIGHT) {
343 } else if (direct_face == Block::FACE_FRONT || direct_face == Block::FACE_BACK) {
351 bool Chunk::IsSurface(const Pos &pos) const noexcept {
352 const Block &block = BlockAt(pos);
353 if (!Type(block).visible) {
356 for (int face = 0; face < Block::FACE_COUNT; ++face) {
357 BlockLookup next = BlockLookup(const_cast<Chunk *>(this), pos, Block::Face(face));
358 if (!next || !next.GetType().visible) {
366 void Chunk::Draw() noexcept {
374 bool Chunk::Intersection(
381 // TODO: should be possible to heavily optimize this
384 dist = std::numeric_limits<float>::infinity();
385 for (int z = 0; z < Depth(); ++z) {
386 for (int y = 0; y < Height(); ++y) {
387 for (int x = 0; x < Width(); ++x, ++id) {
388 if (!Type(blocks[id]).visible) {
393 if (Type(blocks[id]).shape->Intersects(ray, M * ToTransform(id), cur_dist, cur_norm)) {
394 if (cur_dist < dist) {
407 normal = glm::vec3(BlockAt(blkid).Transform() * glm::vec4(normal, 0.0f));
415 BlockModel::Buffer buf;
419 void Chunk::CheckUpdate() noexcept {
425 void Chunk::Update() noexcept {
426 int vtx_count = 0, idx_count = 0;
427 for (const auto &block : blocks) {
428 const Shape *shape = Type(block).shape;
429 vtx_count += shape->VertexCount();
430 idx_count += shape->VertexIndexCount();
433 buf.Reserve(vtx_count, idx_count);
435 BlockModel::Index vtx_counter = 0;
436 for (size_t i = 0; i < Size(); ++i) {
437 const BlockType &type = Type(blocks[i]);
439 if (!type.visible || Obstructed(i).All()) continue;
441 type.FillBlockModel(buf, ToTransform(i), vtx_counter);
442 size_t vtx_begin = vtx_counter;
443 vtx_counter += type.shape->VertexCount();
445 for (size_t vtx = vtx_begin; vtx < vtx_counter; ++vtx) {
446 buf.lights.emplace_back(GetVertexLight(
449 type.shape->VertexNormal(vtx - vtx_begin, blocks[i].Transform())
458 Block::FaceSet Chunk::Obstructed(int idx) const noexcept {
459 Chunk::Pos pos(ToPos(idx));
460 Block::FaceSet result;
462 for (int f = 0; f < Block::FACE_COUNT; ++f) {
463 Block::Face face = Block::Face(f);
464 BlockLookup next(const_cast<Chunk *>(this), pos, face);
465 if (next && next.GetType().FaceFilled(next.GetBlock(), Block::Opposite(face))) {
473 glm::mat4 Chunk::ToTransform(int idx) const noexcept {
474 return glm::translate(ToCoords(idx)) * blocks[idx].Transform();
478 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p) noexcept
480 while (pos.x >= Chunk::Width()) {
481 if (chunk->HasNeighbor(Block::FACE_RIGHT)) {
482 chunk = &chunk->GetNeighbor(Block::FACE_RIGHT);
483 pos.x -= Chunk::Width();
490 if (chunk->HasNeighbor(Block::FACE_LEFT)) {
491 chunk = &chunk->GetNeighbor(Block::FACE_LEFT);
492 pos.x += Chunk::Width();
498 while (pos.y >= Chunk::Height()) {
499 if (chunk->HasNeighbor(Block::FACE_UP)) {
500 chunk = &chunk->GetNeighbor(Block::FACE_UP);
501 pos.y -= Chunk::Height();
508 if (chunk->HasNeighbor(Block::FACE_DOWN)) {
509 chunk = &chunk->GetNeighbor(Block::FACE_DOWN);
510 pos.y += Chunk::Height();
516 while (pos.z >= Chunk::Depth()) {
517 if (chunk->HasNeighbor(Block::FACE_FRONT)) {
518 chunk = &chunk->GetNeighbor(Block::FACE_FRONT);
519 pos.z -= Chunk::Depth();
526 if (chunk->HasNeighbor(Block::FACE_BACK)) {
527 chunk = &chunk->GetNeighbor(Block::FACE_BACK);
528 pos.z += Chunk::Depth();
536 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face face) noexcept
538 pos += Block::FaceNormal(face);
539 if (!Chunk::InBounds(pos)) {
540 pos -= Block::FaceNormal(face) * Chunk::Extent();
541 chunk = &chunk->GetNeighbor(face);
546 ChunkLoader::ChunkLoader(const Config &config, const BlockTypeRegistry ®, const Generator &gen) noexcept
553 , load_dist(config.load_dist)
554 , unload_dist(config.unload_dist) {
562 explicit ChunkLess(const Chunk::Pos &base) noexcept
565 bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const noexcept {
566 Chunk::Pos da(base - a);
567 Chunk::Pos db(base - b);
569 da.x * da.x + da.y * da.y + da.z * da.z <
570 db.x * db.x + db.y * db.y + db.z * db.z;
579 void ChunkLoader::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
580 for (int z = from.z; z < to.z; ++z) {
581 for (int y = from.y; y < to.y; ++y) {
582 for (int x = from.x; x < to.x; ++x) {
583 Chunk::Pos pos(x, y, z);
586 } else if (pos == base) {
590 // for (int i = 0; i < 16; ++i) {
591 // for (int j = 0; j < 16; ++j) {
592 // loaded.back().SetBlock(Chunk::Pos{ i, j, 0 }, Block(1));
593 // loaded.back().SetBlock(Chunk::Pos{ i, j, 15 }, Block(1));
594 // loaded.back().SetBlock(Chunk::Pos{ 0, j, i }, Block(1));
595 // loaded.back().SetBlock(Chunk::Pos{ 15, j, i }, Block(1));
598 // loaded.back().SetBlock(Chunk::Pos{ 1, 0, 1 }, Block(13));
599 // loaded.back().SetBlock(Chunk::Pos{ 14, 0, 1 }, Block(13));
600 // loaded.back().SetBlock(Chunk::Pos{ 1, 0, 14 }, Block(13));
601 // loaded.back().SetBlock(Chunk::Pos{ 14, 0, 14 }, Block(13));
602 // loaded.back().SetBlock(Chunk::Pos{ 1, 15, 1 }, Block(13));
603 // loaded.back().SetBlock(Chunk::Pos{ 14, 15, 1 }, Block(13));
604 // loaded.back().SetBlock(Chunk::Pos{ 1, 15, 14 }, Block(13));
605 // loaded.back().SetBlock(Chunk::Pos{ 14, 15, 14 }, Block(13));
606 // loaded.back().SetBlock(Chunk::Pos{ 7, 7, 0 }, Block(13));
607 // loaded.back().SetBlock(Chunk::Pos{ 8, 7, 0 }, Block(13));
608 // loaded.back().SetBlock(Chunk::Pos{ 7, 8, 0 }, Block(13));
609 // loaded.back().SetBlock(Chunk::Pos{ 8, 8, 0 }, Block(13));
610 // loaded.back().SetBlock(Chunk::Pos{ 7, 7, 15 }, Block(13));
611 // loaded.back().SetBlock(Chunk::Pos{ 8, 7, 15 }, Block(13));
612 // loaded.back().SetBlock(Chunk::Pos{ 7, 8, 15 }, Block(13));
613 // loaded.back().SetBlock(Chunk::Pos{ 8, 8, 15 }, Block(13));
614 // loaded.back().SetBlock(Chunk::Pos{ 0, 7, 7 }, Block(13));
615 // loaded.back().SetBlock(Chunk::Pos{ 0, 7, 8 }, Block(13));
616 // loaded.back().SetBlock(Chunk::Pos{ 0, 8, 7 }, Block(13));
617 // loaded.back().SetBlock(Chunk::Pos{ 0, 8, 8 }, Block(13));
618 // loaded.back().SetBlock(Chunk::Pos{ 15, 7, 7 }, Block(13));
619 // loaded.back().SetBlock(Chunk::Pos{ 15, 7, 8 }, Block(13));
620 // loaded.back().SetBlock(Chunk::Pos{ 15, 8, 7 }, Block(13));
621 // loaded.back().SetBlock(Chunk::Pos{ 15, 8, 8 }, Block(13));
622 // loaded.back().Invalidate();
623 // loaded.back().CheckUpdate();
625 // orientation testing
626 // for (int i = 0; i < Block::FACE_COUNT; ++i) {
627 // for (int j = 0; j < Block::TURN_COUNT; ++j) {
628 // loaded.back().BlockAt(512 * j + 2 * i) = Block(3 * (j + 1), Block::Face(i), Block::Turn(j));
631 // loaded.back().Invalidate();
632 // loaded.back().CheckUpdate();
634 to_generate.emplace_back(pos);
639 to_generate.sort(ChunkLess(base));
642 Chunk &ChunkLoader::Generate(const Chunk::Pos &pos) {
643 loaded.emplace_back(reg);
644 Chunk &chunk = loaded.back();
651 void ChunkLoader::Insert(Chunk &chunk) noexcept {
652 for (Chunk &other : loaded) {
653 chunk.SetNeighbor(other);
657 void ChunkLoader::Remove(Chunk &chunk) noexcept {
661 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) noexcept {
662 for (Chunk &chunk : loaded) {
663 if (chunk.Position() == pos) {
670 bool ChunkLoader::Queued(const Chunk::Pos &pos) noexcept {
671 for (const Chunk::Pos &chunk : to_generate) {
679 bool ChunkLoader::Known(const Chunk::Pos &pos) noexcept {
680 if (Loaded(pos)) return true;
684 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
685 Chunk *chunk = Loaded(pos);
690 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
692 to_generate.erase(iter);
697 return Generate(pos);
700 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
701 if (new_base == base) {
706 // unload far away chunks
707 for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
708 if (std::abs(base.x - iter->Position().x) > unload_dist
709 || std::abs(base.y - iter->Position().y) > unload_dist
710 || std::abs(base.z - iter->Position().z) > unload_dist) {
714 to_free.splice(to_free.end(), loaded, saved);
719 // abort far away queued chunks
720 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
721 if (std::abs(base.x - iter->x) > unload_dist
722 || std::abs(base.y - iter->y) > unload_dist
723 || std::abs(base.z - iter->z) > unload_dist) {
724 iter = to_generate.erase(iter);
729 // add missing new chunks
730 GenerateSurrounding(base);
733 void ChunkLoader::GenerateSurrounding(const Chunk::Pos &pos) {
734 const Chunk::Pos offset(load_dist, load_dist, load_dist);
735 Generate(pos - offset, pos + offset);
738 void ChunkLoader::Update() {
739 if (to_generate.empty()) {
743 Chunk::Pos pos(to_generate.front());
744 to_generate.pop_front();
746 for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
747 if (iter->Position() == pos) {
749 loaded.splice(loaded.end(), to_free, iter);
754 if (to_free.empty()) {
755 loaded.emplace_back(reg);
757 to_free.front().ClearNeighbors();
758 loaded.splice(loaded.end(), to_free, to_free.begin());
760 Chunk &chunk = loaded.back();