3 #include "generator.hpp"
7 #include <glm/gtx/transform.hpp>
12 Chunk::Chunk(const BlockTypeRegistry &types)
14 , neighbor{ 0, 0, 0, 0, 0, 0 }
23 Chunk::Chunk(Chunk &&other)
25 , blocks(std::move(other.blocks))
26 , light(std::move(other.light))
27 , model(std::move(other.model))
28 , position(other.position)
29 , dirty(other.dirty) {
30 for (size_t i = 0; i < Block::FACE_COUNT; ++i) {
31 neighbor[i] = other.neighbor[i];
35 Chunk &Chunk::operator =(Chunk &&other) {
37 for (size_t i = 0; i < Block::FACE_COUNT; ++i) {
38 neighbor[i] = other.neighbor[i];
40 blocks = std::move(other.blocks);
41 light = std::move(other.light);
42 model = std::move(other.model);
43 position = other.position;
49 void Chunk::SetNeighbor(Chunk &other) {
50 if (other.position == position + Pos(-1, 0, 0)) {
51 neighbor[Block::FACE_LEFT] = &other;
52 other.neighbor[Block::FACE_RIGHT] = this;
53 } else if (other.position == position + Pos(1, 0, 0)) {
54 neighbor[Block::FACE_RIGHT] = &other;
55 other.neighbor[Block::FACE_LEFT] = this;
56 } else if (other.position == position + Pos(0, -1, 0)) {
57 neighbor[Block::FACE_DOWN] = &other;
58 other.neighbor[Block::FACE_UP] = this;
59 } else if (other.position == position + Pos(0, 1, 0)) {
60 neighbor[Block::FACE_UP] = &other;
61 other.neighbor[Block::FACE_DOWN] = this;
62 } else if (other.position == position + Pos(0, 0, -1)) {
63 neighbor[Block::FACE_BACK] = &other;
64 other.neighbor[Block::FACE_FRONT] = this;
65 } else if (other.position == position + Pos(0, 0, 1)) {
66 neighbor[Block::FACE_FRONT] = &other;
67 other.neighbor[Block::FACE_BACK] = this;
71 void Chunk::ClearNeighbors() {
72 for (int i = 0; i < Block::FACE_COUNT; ++i) {
73 neighbor[i] = nullptr;
77 void Chunk::Unlink() {
78 if (neighbor[Block::FACE_UP]) {
79 neighbor[Block::FACE_UP]->neighbor[Block::FACE_DOWN] = nullptr;
81 if (neighbor[Block::FACE_DOWN]) {
82 neighbor[Block::FACE_DOWN]->neighbor[Block::FACE_UP] = nullptr;
84 if (neighbor[Block::FACE_LEFT]) {
85 neighbor[Block::FACE_LEFT]->neighbor[Block::FACE_RIGHT] = nullptr;
87 if (neighbor[Block::FACE_RIGHT]) {
88 neighbor[Block::FACE_RIGHT]->neighbor[Block::FACE_LEFT] = nullptr;
90 if (neighbor[Block::FACE_FRONT]) {
91 neighbor[Block::FACE_FRONT]->neighbor[Block::FACE_BACK] = nullptr;
93 if (neighbor[Block::FACE_BACK]) {
94 neighbor[Block::FACE_BACK]->neighbor[Block::FACE_FRONT] = nullptr;
98 void Chunk::Relink() {
99 if (neighbor[Block::FACE_UP]) {
100 neighbor[Block::FACE_UP]->neighbor[Block::FACE_DOWN] = this;
102 if (neighbor[Block::FACE_DOWN]) {
103 neighbor[Block::FACE_DOWN]->neighbor[Block::FACE_UP] = this;
105 if (neighbor[Block::FACE_LEFT]) {
106 neighbor[Block::FACE_LEFT]->neighbor[Block::FACE_RIGHT] = this;
108 if (neighbor[Block::FACE_RIGHT]) {
109 neighbor[Block::FACE_RIGHT]->neighbor[Block::FACE_LEFT] = this;
111 if (neighbor[Block::FACE_FRONT]) {
112 neighbor[Block::FACE_FRONT]->neighbor[Block::FACE_BACK] = this;
114 if (neighbor[Block::FACE_BACK]) {
115 neighbor[Block::FACE_BACK]->neighbor[Block::FACE_FRONT] = this;
127 SetNode(Chunk *chunk, Chunk::Pos pos)
128 : chunk(chunk), pos(pos) { }
130 int Get() const { return chunk->GetLight(pos); }
131 void Set(int level) { chunk->SetLight(pos, level); }
133 bool HasNext(Block::Face face) {
134 const Block *next = chunk->FindNext(pos, face);
135 return next && !chunk->Type(*next).block_light;
137 SetNode GetNext(Block::Face face) {
138 Chunk::Pos next_pos(pos + Block::FaceNormal(face));
139 if (Chunk::InBounds(next_pos)) {
140 return SetNode(chunk, next_pos);
142 return SetNode(&chunk->GetNeighbor(face), next_pos - (Block::FaceNormal(face) * Chunk::Extent()));
153 UnsetNode(Chunk *chunk, Chunk::Pos pos)
154 : SetNode(chunk, pos), level(Get()) { }
156 UnsetNode(const SetNode &set)
157 : SetNode(set), level(Get()) { }
159 UnsetNode GetNext(Block::Face face) { return UnsetNode(SetNode::GetNext(face)); }
163 std::queue<SetNode> light_queue;
164 std::queue<UnsetNode> dark_queue;
167 while (!light_queue.empty()) {
168 SetNode node = light_queue.front();
171 int level = node.Get() - 1;
172 for (int face = 0; face < Block::FACE_COUNT; ++face) {
173 if (node.HasNext(Block::Face(face))) {
174 SetNode other = node.GetNext(Block::Face(face));
175 if (other.Get() < level) {
177 light_queue.emplace(other);
185 while (!dark_queue.empty()) {
186 UnsetNode node = dark_queue.front();
189 for (int face = 0; face < Block::FACE_COUNT; ++face) {
190 if (node.HasNext(Block::Face(face))) {
191 UnsetNode other = node.GetNext(Block::Face(face));
192 // TODO: if there a light source here with the same level this will err
193 if (other.Get() != 0 && other.Get() < node.level) {
195 dark_queue.emplace(other);
197 light_queue.emplace(other);
206 void Chunk::SetBlock(int index, const Block &block) {
207 const BlockType &old_type = Type(blocks[index]);
208 const BlockType &new_type = Type(block);
210 blocks[index] = block;
212 if (&old_type == &new_type) return;
214 if (new_type.luminosity > old_type.luminosity) {
216 SetLight(index, new_type.luminosity);
217 light_queue.emplace(this, ToPos(index));
219 } else if (new_type.luminosity < old_type.luminosity) {
221 dark_queue.emplace(this, ToPos(index));
224 SetLight(index, new_type.luminosity);
225 light_queue.emplace(this, ToPos(index));
227 } else if (new_type.block_light && !old_type.block_light) {
229 dark_queue.emplace(this, ToPos(index));
233 } else if (!new_type.block_light && old_type.block_light) {
236 for (int face = 0; face < Block::FACE_COUNT; ++face) {
237 Pos next_pos(ToPos(index) + Block::FaceNormal(Block::Face(face)));
239 if (InBounds(next_pos)) {
240 next_level = GetLight(next_pos);
242 if (HasNeighbor(Block::Face(face))) {
243 next_pos -= (Block::FaceNormal(Block::Face(face)) * Chunk::Extent());
244 next_level = GetNeighbor(Block::Face(face)).GetLight(next_pos);
247 if (level < next_level) {
252 SetLight(index, level - 1);
253 light_queue.emplace(this, ToPos(index));
259 const Block *Chunk::FindNext(const Pos &pos, Block::Face face) const {
260 Pos next_pos(pos + Block::FaceNormal(face));
261 if (InBounds(next_pos)) {
262 return &BlockAt(pos + Block::FaceNormal(face));
263 } else if (HasNeighbor(face)) {
264 return &GetNeighbor(face).BlockAt(next_pos - (Block::FaceNormal(face) * Extent()));
271 void Chunk::SetLight(int index, int level) {
272 light[index] = level;
275 int Chunk::GetLight(int index) const {
280 bool Chunk::IsSurface(const Pos &pos) const {
281 const Block &block = BlockAt(pos);
282 if (!Type(block).visible) {
285 for (int face = 0; face < Block::FACE_COUNT; ++face) {
286 const Block *next = FindNext(pos, Block::Face(face));
287 if (!next || !Type(*next).visible) {
295 void Chunk::Allocate() {
296 blocks.resize(Size(), Block(0));
297 light.resize(Size(), 0);
309 bool Chunk::Intersection(
316 // TODO: should be possible to heavily optimize this
319 dist = std::numeric_limits<float>::infinity();
320 for (int z = 0; z < Depth(); ++z) {
321 for (int y = 0; y < Height(); ++y) {
322 for (int x = 0; x < Width(); ++x, ++id) {
323 if (!Type(blocks[id]).visible) {
328 if (Type(blocks[id]).shape->Intersects(ray, M * ToTransform(id), cur_dist, cur_norm)) {
329 if (cur_dist < dist) {
342 normal = glm::vec3(BlockAt(blkid).Transform() * glm::vec4(normal, 0.0f));
347 void Chunk::Position(const Pos &pos) {
351 glm::mat4 Chunk::Transform(const Pos &offset) const {
352 return glm::translate((position - offset) * Extent());
362 void Chunk::CheckUpdate() {
368 void Chunk::Update() {
369 int vtx_count = 0, idx_count = 0;
370 for (const auto &block : blocks) {
371 const Shape *shape = Type(block).shape;
372 vtx_count += shape->VertexCount();
373 idx_count += shape->VertexIndexCount();
376 buf.Reserve(vtx_count, idx_count);
378 Model::Index vtx_counter = 0;
379 for (size_t i = 0; i < Size(); ++i) {
380 const BlockType &type = Type(blocks[i]);
382 if (!type.visible || Obstructed(i)) continue;
384 type.FillModel(buf, ToTransform(i), vtx_counter);
385 vtx_counter += type.shape->VertexCount();
392 bool Chunk::Obstructed(int idx) const {
393 Chunk::Pos pos(ToPos(idx));
395 Chunk::Pos left_pos(pos + Chunk::Pos(-1, 0, 0));
396 const Block *left_block = nullptr;
397 if (InBounds(left_pos)) {
398 left_block = &BlockAt(left_pos);
399 } else if (HasNeighbor(Block::FACE_LEFT)) {
400 left_pos += Chunk::Pos(Width(), 0, 0);
401 left_block = &GetNeighbor(Block::FACE_LEFT).BlockAt(left_pos);
405 if (!Type(*left_block).FaceFilled(*left_block, Block::FACE_RIGHT)) {
409 Chunk::Pos right_pos(pos + Chunk::Pos(1, 0, 0));
410 const Block *right_block = nullptr;
411 if (InBounds(right_pos)) {
412 right_block = &BlockAt(right_pos);
413 } else if (HasNeighbor(Block::FACE_RIGHT)) {
414 right_pos += Chunk::Pos(-Width(), 0, 0);
415 right_block = &GetNeighbor(Block::FACE_RIGHT).BlockAt(right_pos);
419 if (!Type(*right_block).FaceFilled(*right_block, Block::FACE_LEFT)) {
423 Chunk::Pos down_pos(pos + Chunk::Pos(0, -1, 0));
424 const Block *down_block = nullptr;
425 if (InBounds(down_pos)) {
426 down_block = &BlockAt(down_pos);
427 } else if (HasNeighbor(Block::FACE_DOWN)) {
428 down_pos += Chunk::Pos(0, Height(), 0);
429 down_block = &GetNeighbor(Block::FACE_DOWN).BlockAt(down_pos);
433 if (!Type(*down_block).FaceFilled(*down_block, Block::FACE_UP)) {
437 Chunk::Pos up_pos(pos + Chunk::Pos(0, 1, 0));
438 const Block *up_block = nullptr;
439 if (InBounds(up_pos)) {
440 up_block = &BlockAt(up_pos);
441 } else if (HasNeighbor(Block::FACE_UP)) {
442 up_pos += Chunk::Pos(0, -Height(), 0);
443 up_block = &GetNeighbor(Block::FACE_UP).BlockAt(up_pos);
447 if (!Type(*up_block).FaceFilled(*up_block, Block::FACE_DOWN)) {
451 Chunk::Pos back_pos(pos + Chunk::Pos(0, 0, -1));
452 const Block *back_block = nullptr;
453 if (InBounds(back_pos)) {
454 back_block = &BlockAt(back_pos);
455 } else if (HasNeighbor(Block::FACE_BACK)) {
456 back_pos += Chunk::Pos(0, 0, Depth());
457 back_block = &GetNeighbor(Block::FACE_BACK).BlockAt(back_pos);
461 if (!Type(*back_block).FaceFilled(*back_block, Block::FACE_FRONT)) {
465 Chunk::Pos front_pos(pos + Chunk::Pos(0, 0, 1));
466 const Block *front_block = nullptr;
467 if (InBounds(front_pos)) {
468 front_block = &BlockAt(front_pos);
469 } else if (HasNeighbor(Block::FACE_FRONT)) {
470 front_pos += Chunk::Pos(0, 0, -Depth());
471 front_block = &GetNeighbor(Block::FACE_FRONT).BlockAt(front_pos);
475 if (!Type(*front_block).FaceFilled(*front_block, Block::FACE_BACK)) {
482 glm::mat4 Chunk::ToTransform(int idx) const {
483 return glm::translate(glm::mat4(1.0f), ToCoords(idx)) * blocks[idx].Transform();
487 ChunkLoader::ChunkLoader(const Config &config, const BlockTypeRegistry ®, const Generator &gen)
494 , load_dist(config.load_dist)
495 , unload_dist(config.unload_dist) {
503 explicit ChunkLess(const Chunk::Pos &base)
506 bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const {
507 Chunk::Pos da(base - a);
508 Chunk::Pos db(base - b);
510 da.x * da.x + da.y * da.y + da.z * da.z <
511 db.x * db.x + db.y * db.y + db.z * db.z;
520 void ChunkLoader::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
521 for (int z = from.z; z < to.z; ++z) {
522 for (int y = from.y; y < to.y; ++y) {
523 for (int x = from.x; x < to.x; ++x) {
524 Chunk::Pos pos(x, y, z);
527 } else if (pos == base) {
530 // orientation testing
531 // for (int i = 0; i < Block::FACE_COUNT; ++i) {
532 // for (int j = 0; j < Block::TURN_COUNT; ++j) {
533 // loaded.back().BlockAt(512 * j + 2 * i) = Block(3 * (j + 1), Block::Face(i), Block::Turn(j));
536 // loaded.back().Invalidate();
537 // loaded.back().CheckUpdate();
539 to_generate.emplace_back(pos);
544 to_generate.sort(ChunkLess(base));
547 Chunk &ChunkLoader::Generate(const Chunk::Pos &pos) {
548 loaded.emplace_back(reg);
549 Chunk &chunk = loaded.back();
556 void ChunkLoader::Insert(Chunk &chunk) {
557 for (Chunk &other : loaded) {
558 chunk.SetNeighbor(other);
562 void ChunkLoader::Remove(Chunk &chunk) {
566 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) {
567 for (Chunk &chunk : loaded) {
568 if (chunk.Position() == pos) {
575 bool ChunkLoader::Queued(const Chunk::Pos &pos) {
576 for (const Chunk::Pos &chunk : to_generate) {
584 bool ChunkLoader::Known(const Chunk::Pos &pos) {
585 if (Loaded(pos)) return true;
589 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
590 Chunk *chunk = Loaded(pos);
595 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
597 to_generate.erase(iter);
602 return Generate(pos);
605 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
606 if (new_base == base) {
611 // unload far away chunks
612 for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
613 if (std::abs(base.x - iter->Position().x) > unload_dist
614 || std::abs(base.y - iter->Position().y) > unload_dist
615 || std::abs(base.z - iter->Position().z) > unload_dist) {
619 to_free.splice(to_free.end(), loaded, saved);
624 // abort far away queued chunks
625 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
626 if (std::abs(base.x - iter->x) > unload_dist
627 || std::abs(base.y - iter->y) > unload_dist
628 || std::abs(base.z - iter->z) > unload_dist) {
629 iter = to_generate.erase(iter);
634 // add missing new chunks
635 GenerateSurrounding(base);
638 void ChunkLoader::GenerateSurrounding(const Chunk::Pos &pos) {
639 const Chunk::Pos offset(load_dist, load_dist, load_dist);
640 Generate(pos - offset, pos + offset);
643 void ChunkLoader::Update() {
645 if (!to_generate.empty()) {
646 Chunk::Pos pos(to_generate.front());
648 for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
649 if (iter->Position() == pos) {
651 loaded.splice(loaded.end(), to_free, iter);
658 if (to_free.empty()) {
659 loaded.emplace_back(reg);
661 to_free.front().ClearNeighbors();
662 loaded.splice(loaded.end(), to_free, to_free.begin());
665 Chunk &chunk = loaded.back();
670 to_generate.pop_front();
673 if (!reused && !to_free.empty()) {