3 #include "generator.hpp"
8 #include <glm/gtx/transform.hpp>
13 Chunk::Chunk(const BlockTypeRegistry &types)
24 Chunk::Chunk(Chunk &&other)
26 , model(std::move(other.model))
27 , position(other.position)
28 , dirty(other.dirty) {
29 std::copy(other.neighbor, other.neighbor + sizeof(neighbor), neighbor);
30 std::copy(other.blocks, other.blocks + sizeof(blocks), blocks);
31 std::copy(other.light, other.light + sizeof(light), light);
34 Chunk &Chunk::operator =(Chunk &&other) {
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);
39 model = std::move(other.model);
40 position = other.position;
53 SetNode(Chunk *chunk, Chunk::Pos pos)
54 : chunk(chunk), pos(pos) { }
56 int Get() const { return chunk->GetLight(pos); }
57 void Set(int level) { chunk->SetLight(pos, level); }
59 bool HasNext(Block::Face face) {
60 const BlockLookup next(chunk, pos, face);
61 return next && !next.GetType().block_light;
63 SetNode GetNext(Block::Face face) {
64 const BlockLookup next(chunk, pos, face);
65 return SetNode(&next.GetChunk(), next.GetBlockPos());
75 UnsetNode(Chunk *chunk, Chunk::Pos pos)
76 : SetNode(chunk, pos), level(Get()) { }
78 UnsetNode(const SetNode &set)
79 : SetNode(set), level(Get()) { }
82 bool HasNext(Block::Face face) {
83 const BlockLookup next(chunk, pos, face);
86 UnsetNode GetNext(Block::Face face) { return UnsetNode(SetNode::GetNext(face)); }
90 std::queue<SetNode> light_queue;
91 std::queue<UnsetNode> dark_queue;
94 while (!light_queue.empty()) {
95 SetNode node = light_queue.front();
98 int level = node.Get() - 1;
99 for (int face = 0; face < Block::FACE_COUNT; ++face) {
100 if (node.HasNext(Block::Face(face))) {
101 SetNode other = node.GetNext(Block::Face(face));
102 if (other.Get() < level) {
104 light_queue.emplace(other);
112 while (!dark_queue.empty()) {
113 UnsetNode node = dark_queue.front();
116 for (int face = 0; face < Block::FACE_COUNT; ++face) {
117 if (node.HasNext(Block::Face(face))) {
118 UnsetNode other = node.GetNext(Block::Face(face));
119 // TODO: if there a light source here with the same level this will err
120 if (other.Get() != 0 && other.Get() < node.level) {
122 dark_queue.emplace(other);
124 light_queue.emplace(other);
133 void Chunk::SetBlock(int index, const Block &block) {
134 const BlockType &old_type = Type(blocks[index]);
135 const BlockType &new_type = Type(block);
137 blocks[index] = block;
139 if (&old_type == &new_type) return;
141 if (new_type.luminosity > old_type.luminosity) {
143 SetLight(index, new_type.luminosity);
144 light_queue.emplace(this, ToPos(index));
146 } else if (new_type.luminosity < old_type.luminosity) {
148 dark_queue.emplace(this, ToPos(index));
151 SetLight(index, new_type.luminosity);
152 light_queue.emplace(this, ToPos(index));
154 } else if (new_type.block_light && !old_type.block_light) {
156 if (GetLight(index) > 0) {
157 dark_queue.emplace(this, ToPos(index));
162 } else if (!new_type.block_light && old_type.block_light) {
165 for (int face = 0; face < Block::FACE_COUNT; ++face) {
166 BlockLookup next_block(this, ToPos(index), Block::Face(face));
168 level = std::min(level, next_block.GetLight());
172 SetLight(index, level - 1);
173 light_queue.emplace(this, ToPos(index));
179 void Chunk::SetNeighbor(Chunk &other) {
180 if (other.position == position + Pos(-1, 0, 0)) {
181 if (neighbor[Block::FACE_LEFT] != &other) {
182 neighbor[Block::FACE_LEFT] = &other;
183 other.neighbor[Block::FACE_RIGHT] = this;
184 for (int z = 0; z < Depth(); ++z) {
185 for (int y = 0; y < Height(); ++y) {
187 Pos other_pos(Width() - 1, y, z);
188 if (GetLight(my_pos) > 0) {
189 light_queue.emplace(this, my_pos);
191 if (other.GetLight(other_pos) > 0) {
192 light_queue.emplace(&other, other_pos);
198 } else if (other.position == position + Pos(1, 0, 0)) {
199 if (neighbor[Block::FACE_RIGHT] != &other) {
200 neighbor[Block::FACE_RIGHT] = &other;
201 other.neighbor[Block::FACE_LEFT] = this;
202 for (int z = 0; z < Depth(); ++z) {
203 for (int y = 0; y < Height(); ++y) {
204 Pos my_pos(Width() - 1, y, z);
205 Pos other_pos(0, y, z);
206 if (GetLight(my_pos) > 0) {
207 light_queue.emplace(this, my_pos);
209 if (other.GetLight(other_pos) > 0) {
210 light_queue.emplace(&other, other_pos);
216 } else if (other.position == position + Pos(0, -1, 0)) {
217 if (neighbor[Block::FACE_DOWN] != &other) {
218 neighbor[Block::FACE_DOWN] = &other;
219 other.neighbor[Block::FACE_UP] = this;
220 for (int z = 0; z < Depth(); ++z) {
221 for (int x = 0; x < Width(); ++x) {
223 Pos other_pos(x, Height() - 1, z);
224 if (GetLight(my_pos) > 0) {
225 light_queue.emplace(this, my_pos);
227 if (other.GetLight(other_pos) > 0) {
228 light_queue.emplace(&other, other_pos);
234 } else if (other.position == position + Pos(0, 1, 0)) {
235 if (neighbor[Block::FACE_UP] != &other) {
236 neighbor[Block::FACE_UP] = &other;
237 other.neighbor[Block::FACE_DOWN] = this;
238 for (int z = 0; z < Depth(); ++z) {
239 for (int x = 0; x < Width(); ++x) {
240 Pos my_pos(x, Height() - 1, z);
241 Pos other_pos(x, 0, z);
242 if (GetLight(my_pos) > 0) {
243 light_queue.emplace(this, my_pos);
245 if (other.GetLight(other_pos) > 0) {
246 light_queue.emplace(&other, other_pos);
252 } else if (other.position == position + Pos(0, 0, -1)) {
253 if (neighbor[Block::FACE_BACK] != &other) {
254 neighbor[Block::FACE_BACK] = &other;
255 other.neighbor[Block::FACE_FRONT] = this;
256 for (int y = 0; y < Height(); ++y) {
257 for (int x = 0; x < Width(); ++x) {
259 Pos other_pos(x, y, Depth() - 1);
260 if (GetLight(my_pos) > 0) {
261 light_queue.emplace(this, my_pos);
263 if (other.GetLight(other_pos) > 0) {
264 light_queue.emplace(&other, other_pos);
270 } else if (other.position == position + Pos(0, 0, 1)) {
271 if (neighbor[Block::FACE_FRONT] != &other) {
272 neighbor[Block::FACE_FRONT] = &other;
273 other.neighbor[Block::FACE_BACK] = this;
274 for (int y = 0; y < Height(); ++y) {
275 for (int x = 0; x < Width(); ++x) {
276 Pos my_pos(x, y, Depth() - 1);
277 Pos other_pos(x, y, 0);
278 if (GetLight(my_pos) > 0) {
279 light_queue.emplace(this, my_pos);
281 if (other.GetLight(other_pos) > 0) {
282 light_queue.emplace(&other, other_pos);
291 void Chunk::ClearNeighbors() {
292 for (int i = 0; i < Block::FACE_COUNT; ++i) {
293 neighbor[i] = nullptr;
297 void Chunk::Unlink() {
298 for (int face = 0; face < Block::FACE_COUNT; ++face) {
299 if (neighbor[face]) {
300 neighbor[face]->neighbor[Block::Opposite(Block::Face(face))] = nullptr;
305 void Chunk::Relink() {
306 for (int face = 0; face < Block::FACE_COUNT; ++face) {
307 if (neighbor[face]) {
308 neighbor[face]->neighbor[Block::Opposite(Block::Face(face))] = this;
314 void Chunk::SetLight(int index, int level) {
315 if (light[index] != level) {
316 light[index] = level;
321 int Chunk::GetLight(int index) const {
325 float Chunk::GetVertexLight(int index, const BlockModel::Position &vtx, const BlockModel::Normal &norm) const {
326 float light = GetLight(index);
327 Chunk::Pos pos(ToPos(index));
329 Block::Face direct_face(Block::NormalFace(norm));
331 BlockLookup direct(const_cast<Chunk *>(this), pos, Block::NormalFace(norm));
333 float direct_light = direct.GetLight();
334 if (direct_light > light) {
335 light = direct_light;
339 // cheap alternative until AO etc are implemented
340 // to tell the faces apart
342 if (direct_face == Block::FACE_LEFT || direct_face == Block::FACE_RIGHT) {
344 } else if (direct_face == Block::FACE_FRONT || direct_face == Block::FACE_BACK) {
352 bool Chunk::IsSurface(const Pos &pos) const {
353 const Block &block = BlockAt(pos);
354 if (!Type(block).visible) {
357 for (int face = 0; face < Block::FACE_COUNT; ++face) {
358 BlockLookup next = BlockLookup(const_cast<Chunk *>(this), pos, Block::Face(face));
359 if (!next || !next.GetType().visible) {
375 bool Chunk::Intersection(
382 // TODO: should be possible to heavily optimize this
385 dist = std::numeric_limits<float>::infinity();
386 for (int z = 0; z < Depth(); ++z) {
387 for (int y = 0; y < Height(); ++y) {
388 for (int x = 0; x < Width(); ++x, ++id) {
389 if (!Type(blocks[id]).visible) {
394 if (Type(blocks[id]).shape->Intersects(ray, M * ToTransform(id), cur_dist, cur_norm)) {
395 if (cur_dist < dist) {
408 normal = glm::vec3(BlockAt(blkid).Transform() * glm::vec4(normal, 0.0f));
413 void Chunk::Position(const Pos &pos) {
417 glm::mat4 Chunk::Transform(const Pos &offset) const {
418 return glm::translate((position - offset) * Extent());
424 BlockModel::Buffer buf;
428 void Chunk::CheckUpdate() {
434 void Chunk::Update() {
435 int vtx_count = 0, idx_count = 0;
436 for (const auto &block : blocks) {
437 const Shape *shape = Type(block).shape;
438 vtx_count += shape->VertexCount();
439 idx_count += shape->VertexIndexCount();
442 buf.Reserve(vtx_count, idx_count);
444 BlockModel::Index vtx_counter = 0;
445 for (size_t i = 0; i < Size(); ++i) {
446 const BlockType &type = Type(blocks[i]);
448 if (!type.visible || Obstructed(i)) continue;
450 type.FillBlockModel(buf, ToTransform(i), vtx_counter);
451 size_t vtx_begin = vtx_counter;
452 vtx_counter += type.shape->VertexCount();
454 for (size_t vtx = vtx_begin; vtx < vtx_counter; ++vtx) {
455 buf.lights.emplace_back(GetVertexLight(i, buf.vertices[vtx], buf.normals[vtx]));
463 bool Chunk::Obstructed(int idx) const {
464 Chunk::Pos pos(ToPos(idx));
466 for (int f = 0; f < Block::FACE_COUNT; ++f) {
467 Block::Face face = Block::Face(f);
468 BlockLookup next(const_cast<Chunk *>(this), pos, face);
469 if (!next || !next.GetType().FaceFilled(next.GetBlock(), Block::Opposite(face))) {
477 glm::mat4 Chunk::ToTransform(int idx) const {
478 return glm::translate(glm::mat4(1.0f), ToCoords(idx)) * blocks[idx].Transform();
482 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p)
484 while (pos.x >= Chunk::Width()) {
485 if (chunk->HasNeighbor(Block::FACE_RIGHT)) {
486 chunk = &chunk->GetNeighbor(Block::FACE_RIGHT);
487 pos.x -= Chunk::Width();
494 if (chunk->HasNeighbor(Block::FACE_LEFT)) {
495 chunk = &chunk->GetNeighbor(Block::FACE_LEFT);
496 pos.x += Chunk::Width();
502 while (pos.y >= Chunk::Height()) {
503 if (chunk->HasNeighbor(Block::FACE_UP)) {
504 chunk = &chunk->GetNeighbor(Block::FACE_UP);
505 pos.y -= Chunk::Height();
512 if (chunk->HasNeighbor(Block::FACE_DOWN)) {
513 chunk = &chunk->GetNeighbor(Block::FACE_DOWN);
514 pos.y += Chunk::Height();
520 while (pos.z >= Chunk::Depth()) {
521 if (chunk->HasNeighbor(Block::FACE_FRONT)) {
522 chunk = &chunk->GetNeighbor(Block::FACE_FRONT);
523 pos.z -= Chunk::Depth();
530 if (chunk->HasNeighbor(Block::FACE_BACK)) {
531 chunk = &chunk->GetNeighbor(Block::FACE_BACK);
532 pos.z += Chunk::Depth();
540 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face face)
542 pos += Block::FaceNormal(face);
543 if (!Chunk::InBounds(pos)) {
544 pos -= Block::FaceNormal(face) * Chunk::Extent();
545 chunk = &chunk->GetNeighbor(face);
550 ChunkLoader::ChunkLoader(const Config &config, const BlockTypeRegistry ®, const Generator &gen)
557 , load_dist(config.load_dist)
558 , unload_dist(config.unload_dist) {
566 explicit ChunkLess(const Chunk::Pos &base)
569 bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const {
570 Chunk::Pos da(base - a);
571 Chunk::Pos db(base - b);
573 da.x * da.x + da.y * da.y + da.z * da.z <
574 db.x * db.x + db.y * db.y + db.z * db.z;
583 void ChunkLoader::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
584 for (int z = from.z; z < to.z; ++z) {
585 for (int y = from.y; y < to.y; ++y) {
586 for (int x = from.x; x < to.x; ++x) {
587 Chunk::Pos pos(x, y, z);
590 } else if (pos == base) {
594 // for (int i = 0; i < 16; ++i) {
595 // for (int j = 0; j < 16; ++j) {
596 // loaded.back().SetBlock(Chunk::Pos{ i, j, 0 }, Block(1));
597 // loaded.back().SetBlock(Chunk::Pos{ i, j, 15 }, Block(1));
598 // loaded.back().SetBlock(Chunk::Pos{ 0, j, i }, Block(1));
599 // loaded.back().SetBlock(Chunk::Pos{ 15, j, i }, Block(1));
602 // loaded.back().SetBlock(Chunk::Pos{ 1, 0, 1 }, Block(13));
603 // loaded.back().SetBlock(Chunk::Pos{ 14, 0, 1 }, Block(13));
604 // loaded.back().SetBlock(Chunk::Pos{ 1, 0, 14 }, Block(13));
605 // loaded.back().SetBlock(Chunk::Pos{ 14, 0, 14 }, Block(13));
606 // loaded.back().SetBlock(Chunk::Pos{ 1, 15, 1 }, Block(13));
607 // loaded.back().SetBlock(Chunk::Pos{ 14, 15, 1 }, Block(13));
608 // loaded.back().SetBlock(Chunk::Pos{ 1, 15, 14 }, Block(13));
609 // loaded.back().SetBlock(Chunk::Pos{ 14, 15, 14 }, Block(13));
610 // loaded.back().SetBlock(Chunk::Pos{ 7, 7, 0 }, Block(13));
611 // loaded.back().SetBlock(Chunk::Pos{ 8, 7, 0 }, Block(13));
612 // loaded.back().SetBlock(Chunk::Pos{ 7, 8, 0 }, Block(13));
613 // loaded.back().SetBlock(Chunk::Pos{ 8, 8, 0 }, Block(13));
614 // loaded.back().SetBlock(Chunk::Pos{ 7, 7, 15 }, Block(13));
615 // loaded.back().SetBlock(Chunk::Pos{ 8, 7, 15 }, Block(13));
616 // loaded.back().SetBlock(Chunk::Pos{ 7, 8, 15 }, Block(13));
617 // loaded.back().SetBlock(Chunk::Pos{ 8, 8, 15 }, Block(13));
618 // loaded.back().SetBlock(Chunk::Pos{ 0, 7, 7 }, Block(13));
619 // loaded.back().SetBlock(Chunk::Pos{ 0, 7, 8 }, Block(13));
620 // loaded.back().SetBlock(Chunk::Pos{ 0, 8, 7 }, Block(13));
621 // loaded.back().SetBlock(Chunk::Pos{ 0, 8, 8 }, Block(13));
622 // loaded.back().SetBlock(Chunk::Pos{ 15, 7, 7 }, Block(13));
623 // loaded.back().SetBlock(Chunk::Pos{ 15, 7, 8 }, Block(13));
624 // loaded.back().SetBlock(Chunk::Pos{ 15, 8, 7 }, Block(13));
625 // loaded.back().SetBlock(Chunk::Pos{ 15, 8, 8 }, Block(13));
626 // loaded.back().Invalidate();
627 // loaded.back().CheckUpdate();
629 // orientation testing
630 // for (int i = 0; i < Block::FACE_COUNT; ++i) {
631 // for (int j = 0; j < Block::TURN_COUNT; ++j) {
632 // loaded.back().BlockAt(512 * j + 2 * i) = Block(3 * (j + 1), Block::Face(i), Block::Turn(j));
635 // loaded.back().Invalidate();
636 // loaded.back().CheckUpdate();
638 to_generate.emplace_back(pos);
643 to_generate.sort(ChunkLess(base));
646 Chunk &ChunkLoader::Generate(const Chunk::Pos &pos) {
647 loaded.emplace_back(reg);
648 Chunk &chunk = loaded.back();
655 void ChunkLoader::Insert(Chunk &chunk) {
656 for (Chunk &other : loaded) {
657 chunk.SetNeighbor(other);
661 void ChunkLoader::Remove(Chunk &chunk) {
665 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) {
666 for (Chunk &chunk : loaded) {
667 if (chunk.Position() == pos) {
674 bool ChunkLoader::Queued(const Chunk::Pos &pos) {
675 for (const Chunk::Pos &chunk : to_generate) {
683 bool ChunkLoader::Known(const Chunk::Pos &pos) {
684 if (Loaded(pos)) return true;
688 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
689 Chunk *chunk = Loaded(pos);
694 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
696 to_generate.erase(iter);
701 return Generate(pos);
704 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
705 if (new_base == base) {
710 // unload far away chunks
711 for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
712 if (std::abs(base.x - iter->Position().x) > unload_dist
713 || std::abs(base.y - iter->Position().y) > unload_dist
714 || std::abs(base.z - iter->Position().z) > unload_dist) {
718 to_free.splice(to_free.end(), loaded, saved);
723 // abort far away queued chunks
724 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
725 if (std::abs(base.x - iter->x) > unload_dist
726 || std::abs(base.y - iter->y) > unload_dist
727 || std::abs(base.z - iter->z) > unload_dist) {
728 iter = to_generate.erase(iter);
733 // add missing new chunks
734 GenerateSurrounding(base);
737 void ChunkLoader::GenerateSurrounding(const Chunk::Pos &pos) {
738 const Chunk::Pos offset(load_dist, load_dist, load_dist);
739 Generate(pos - offset, pos + offset);
742 void ChunkLoader::Update() {
743 if (to_generate.empty()) {
747 Chunk::Pos pos(to_generate.front());
748 to_generate.pop_front();
750 for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
751 if (iter->Position() == pos) {
753 loaded.splice(loaded.end(), to_free, iter);
758 if (to_free.empty()) {
759 loaded.emplace_back(reg);
761 to_free.front().ClearNeighbors();
762 loaded.splice(loaded.end(), to_free, to_free.begin());
764 Chunk &chunk = loaded.back();