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 Model::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).All()) 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(
458 type.shape->VertexNormal(vtx - vtx_begin, blocks[i].Transform())
467 Block::FaceSet Chunk::Obstructed(int idx) const {
468 Chunk::Pos pos(ToPos(idx));
469 Block::FaceSet result;
471 for (int f = 0; f < Block::FACE_COUNT; ++f) {
472 Block::Face face = Block::Face(f);
473 BlockLookup next(const_cast<Chunk *>(this), pos, face);
474 if (next && next.GetType().FaceFilled(next.GetBlock(), Block::Opposite(face))) {
482 glm::mat4 Chunk::ToTransform(int idx) const {
483 return glm::translate(glm::mat4(1.0f), ToCoords(idx)) * blocks[idx].Transform();
487 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p)
489 while (pos.x >= Chunk::Width()) {
490 if (chunk->HasNeighbor(Block::FACE_RIGHT)) {
491 chunk = &chunk->GetNeighbor(Block::FACE_RIGHT);
492 pos.x -= Chunk::Width();
499 if (chunk->HasNeighbor(Block::FACE_LEFT)) {
500 chunk = &chunk->GetNeighbor(Block::FACE_LEFT);
501 pos.x += Chunk::Width();
507 while (pos.y >= Chunk::Height()) {
508 if (chunk->HasNeighbor(Block::FACE_UP)) {
509 chunk = &chunk->GetNeighbor(Block::FACE_UP);
510 pos.y -= Chunk::Height();
517 if (chunk->HasNeighbor(Block::FACE_DOWN)) {
518 chunk = &chunk->GetNeighbor(Block::FACE_DOWN);
519 pos.y += Chunk::Height();
525 while (pos.z >= Chunk::Depth()) {
526 if (chunk->HasNeighbor(Block::FACE_FRONT)) {
527 chunk = &chunk->GetNeighbor(Block::FACE_FRONT);
528 pos.z -= Chunk::Depth();
535 if (chunk->HasNeighbor(Block::FACE_BACK)) {
536 chunk = &chunk->GetNeighbor(Block::FACE_BACK);
537 pos.z += Chunk::Depth();
545 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face face)
547 pos += Block::FaceNormal(face);
548 if (!Chunk::InBounds(pos)) {
549 pos -= Block::FaceNormal(face) * Chunk::Extent();
550 chunk = &chunk->GetNeighbor(face);
555 ChunkLoader::ChunkLoader(const Config &config, const BlockTypeRegistry ®, const Generator &gen)
562 , load_dist(config.load_dist)
563 , unload_dist(config.unload_dist) {
571 explicit ChunkLess(const Chunk::Pos &base)
574 bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const {
575 Chunk::Pos da(base - a);
576 Chunk::Pos db(base - b);
578 da.x * da.x + da.y * da.y + da.z * da.z <
579 db.x * db.x + db.y * db.y + db.z * db.z;
588 void ChunkLoader::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
589 for (int z = from.z; z < to.z; ++z) {
590 for (int y = from.y; y < to.y; ++y) {
591 for (int x = from.x; x < to.x; ++x) {
592 Chunk::Pos pos(x, y, z);
595 } else if (pos == base) {
599 // for (int i = 0; i < 16; ++i) {
600 // for (int j = 0; j < 16; ++j) {
601 // loaded.back().SetBlock(Chunk::Pos{ i, j, 0 }, Block(1));
602 // loaded.back().SetBlock(Chunk::Pos{ i, j, 15 }, Block(1));
603 // loaded.back().SetBlock(Chunk::Pos{ 0, j, i }, Block(1));
604 // loaded.back().SetBlock(Chunk::Pos{ 15, j, i }, Block(1));
607 // loaded.back().SetBlock(Chunk::Pos{ 1, 0, 1 }, Block(13));
608 // loaded.back().SetBlock(Chunk::Pos{ 14, 0, 1 }, Block(13));
609 // loaded.back().SetBlock(Chunk::Pos{ 1, 0, 14 }, Block(13));
610 // loaded.back().SetBlock(Chunk::Pos{ 14, 0, 14 }, Block(13));
611 // loaded.back().SetBlock(Chunk::Pos{ 1, 15, 1 }, Block(13));
612 // loaded.back().SetBlock(Chunk::Pos{ 14, 15, 1 }, Block(13));
613 // loaded.back().SetBlock(Chunk::Pos{ 1, 15, 14 }, Block(13));
614 // loaded.back().SetBlock(Chunk::Pos{ 14, 15, 14 }, Block(13));
615 // loaded.back().SetBlock(Chunk::Pos{ 7, 7, 0 }, Block(13));
616 // loaded.back().SetBlock(Chunk::Pos{ 8, 7, 0 }, Block(13));
617 // loaded.back().SetBlock(Chunk::Pos{ 7, 8, 0 }, Block(13));
618 // loaded.back().SetBlock(Chunk::Pos{ 8, 8, 0 }, Block(13));
619 // loaded.back().SetBlock(Chunk::Pos{ 7, 7, 15 }, Block(13));
620 // loaded.back().SetBlock(Chunk::Pos{ 8, 7, 15 }, Block(13));
621 // loaded.back().SetBlock(Chunk::Pos{ 7, 8, 15 }, Block(13));
622 // loaded.back().SetBlock(Chunk::Pos{ 8, 8, 15 }, Block(13));
623 // loaded.back().SetBlock(Chunk::Pos{ 0, 7, 7 }, Block(13));
624 // loaded.back().SetBlock(Chunk::Pos{ 0, 7, 8 }, Block(13));
625 // loaded.back().SetBlock(Chunk::Pos{ 0, 8, 7 }, Block(13));
626 // loaded.back().SetBlock(Chunk::Pos{ 0, 8, 8 }, Block(13));
627 // loaded.back().SetBlock(Chunk::Pos{ 15, 7, 7 }, Block(13));
628 // loaded.back().SetBlock(Chunk::Pos{ 15, 7, 8 }, Block(13));
629 // loaded.back().SetBlock(Chunk::Pos{ 15, 8, 7 }, Block(13));
630 // loaded.back().SetBlock(Chunk::Pos{ 15, 8, 8 }, Block(13));
631 // loaded.back().Invalidate();
632 // loaded.back().CheckUpdate();
634 // orientation testing
635 // for (int i = 0; i < Block::FACE_COUNT; ++i) {
636 // for (int j = 0; j < Block::TURN_COUNT; ++j) {
637 // loaded.back().BlockAt(512 * j + 2 * i) = Block(3 * (j + 1), Block::Face(i), Block::Turn(j));
640 // loaded.back().Invalidate();
641 // loaded.back().CheckUpdate();
643 to_generate.emplace_back(pos);
648 to_generate.sort(ChunkLess(base));
651 Chunk &ChunkLoader::Generate(const Chunk::Pos &pos) {
652 loaded.emplace_back(reg);
653 Chunk &chunk = loaded.back();
660 void ChunkLoader::Insert(Chunk &chunk) {
661 for (Chunk &other : loaded) {
662 chunk.SetNeighbor(other);
666 void ChunkLoader::Remove(Chunk &chunk) {
670 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) {
671 for (Chunk &chunk : loaded) {
672 if (chunk.Position() == pos) {
679 bool ChunkLoader::Queued(const Chunk::Pos &pos) {
680 for (const Chunk::Pos &chunk : to_generate) {
688 bool ChunkLoader::Known(const Chunk::Pos &pos) {
689 if (Loaded(pos)) return true;
693 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
694 Chunk *chunk = Loaded(pos);
699 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
701 to_generate.erase(iter);
706 return Generate(pos);
709 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
710 if (new_base == base) {
715 // unload far away chunks
716 for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
717 if (std::abs(base.x - iter->Position().x) > unload_dist
718 || std::abs(base.y - iter->Position().y) > unload_dist
719 || std::abs(base.z - iter->Position().z) > unload_dist) {
723 to_free.splice(to_free.end(), loaded, saved);
728 // abort far away queued chunks
729 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
730 if (std::abs(base.x - iter->x) > unload_dist
731 || std::abs(base.y - iter->y) > unload_dist
732 || std::abs(base.z - iter->z) > unload_dist) {
733 iter = to_generate.erase(iter);
738 // add missing new chunks
739 GenerateSurrounding(base);
742 void ChunkLoader::GenerateSurrounding(const Chunk::Pos &pos) {
743 const Chunk::Pos offset(load_dist, load_dist, load_dist);
744 Generate(pos - offset, pos + offset);
747 void ChunkLoader::Update() {
748 if (to_generate.empty()) {
752 Chunk::Pos pos(to_generate.front());
753 to_generate.pop_front();
755 for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
756 if (iter->Position() == pos) {
758 loaded.splice(loaded.end(), to_free, iter);
763 if (to_free.empty()) {
764 loaded.emplace_back(reg);
766 to_free.front().ClearNeighbors();
767 loaded.splice(loaded.end(), to_free, to_free.begin());
769 Chunk &chunk = loaded.back();