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;
56 SetNode(Chunk *chunk, Chunk::Pos pos)
57 : chunk(chunk), pos(pos) { }
59 int Get() const { return chunk->GetLight(pos); }
60 void Set(int level) { chunk->SetLight(pos, level); }
62 bool HasNext(Block::Face face) {
63 const BlockLookup next(chunk, pos, face);
64 return next && !next.GetType().block_light;
66 SetNode GetNext(Block::Face face) {
67 const BlockLookup next(chunk, pos, face);
68 return SetNode(&next.GetChunk(), next.GetBlockPos());
78 UnsetNode(Chunk *chunk, Chunk::Pos pos)
79 : SetNode(chunk, pos), level(Get()) { }
81 UnsetNode(const SetNode &set)
82 : SetNode(set), level(Get()) { }
85 bool HasNext(Block::Face face) {
86 const BlockLookup next(chunk, pos, face);
89 UnsetNode GetNext(Block::Face face) { return UnsetNode(SetNode::GetNext(face)); }
93 std::queue<SetNode> light_queue;
94 std::queue<UnsetNode> dark_queue;
97 while (!light_queue.empty()) {
98 SetNode node = light_queue.front();
101 int level = node.Get() - 1;
102 for (int face = 0; face < Block::FACE_COUNT; ++face) {
103 if (node.HasNext(Block::Face(face))) {
104 SetNode other = node.GetNext(Block::Face(face));
105 if (other.Get() < level) {
107 light_queue.emplace(other);
115 while (!dark_queue.empty()) {
116 UnsetNode node = dark_queue.front();
119 for (int face = 0; face < Block::FACE_COUNT; ++face) {
120 if (node.HasNext(Block::Face(face))) {
121 UnsetNode other = node.GetNext(Block::Face(face));
122 // TODO: if there a light source here with the same level this will err
123 if (other.Get() != 0 && other.Get() < node.level) {
125 dark_queue.emplace(other);
127 light_queue.emplace(other);
136 void Chunk::SetBlock(int index, const Block &block) {
137 const BlockType &old_type = Type(blocks[index]);
138 const BlockType &new_type = Type(block);
140 blocks[index] = block;
142 if (&old_type == &new_type) return;
144 if (new_type.luminosity > old_type.luminosity) {
146 SetLight(index, new_type.luminosity);
147 light_queue.emplace(this, ToPos(index));
149 } else if (new_type.luminosity < old_type.luminosity) {
151 dark_queue.emplace(this, ToPos(index));
154 SetLight(index, new_type.luminosity);
155 light_queue.emplace(this, ToPos(index));
157 } else if (new_type.block_light && !old_type.block_light) {
159 if (GetLight(index) > 0) {
160 dark_queue.emplace(this, ToPos(index));
165 } else if (!new_type.block_light && old_type.block_light) {
168 for (int face = 0; face < Block::FACE_COUNT; ++face) {
169 Pos next_pos(ToPos(index) + Block::FaceNormal(Block::Face(face)));
171 if (InBounds(next_pos)) {
172 next_level = GetLight(next_pos);
174 if (HasNeighbor(Block::Face(face))) {
175 next_pos -= (Block::FaceNormal(Block::Face(face)) * Chunk::Extent());
176 next_level = GetNeighbor(Block::Face(face)).GetLight(next_pos);
179 if (level < next_level) {
184 SetLight(index, level - 1);
185 light_queue.emplace(this, ToPos(index));
191 const Block *Chunk::FindNext(const Pos &pos, Block::Face face) const {
192 Pos next_pos(pos + Block::FaceNormal(face));
193 if (InBounds(next_pos)) {
194 return &BlockAt(pos + Block::FaceNormal(face));
195 } else if (HasNeighbor(face)) {
196 return &GetNeighbor(face).BlockAt(next_pos - (Block::FaceNormal(face) * Extent()));
202 void Chunk::SetNeighbor(Chunk &other) {
203 if (other.position == position + Pos(-1, 0, 0)) {
204 if (neighbor[Block::FACE_LEFT] != &other) {
205 neighbor[Block::FACE_LEFT] = &other;
206 other.neighbor[Block::FACE_RIGHT] = this;
207 for (int z = 0; z < Depth(); ++z) {
208 for (int y = 0; y < Height(); ++y) {
210 Pos other_pos(Width() - 1, y, z);
211 if (GetLight(my_pos) > 0) {
212 light_queue.emplace(this, my_pos);
214 if (other.GetLight(other_pos) > 0) {
215 light_queue.emplace(&other, other_pos);
221 } else if (other.position == position + Pos(1, 0, 0)) {
222 if (neighbor[Block::FACE_RIGHT] != &other) {
223 neighbor[Block::FACE_RIGHT] = &other;
224 other.neighbor[Block::FACE_LEFT] = this;
225 for (int z = 0; z < Depth(); ++z) {
226 for (int y = 0; y < Height(); ++y) {
227 Pos my_pos(Width() - 1, y, z);
228 Pos other_pos(0, y, z);
229 if (GetLight(my_pos) > 0) {
230 light_queue.emplace(this, my_pos);
232 if (other.GetLight(other_pos) > 0) {
233 light_queue.emplace(&other, other_pos);
239 } else if (other.position == position + Pos(0, -1, 0)) {
240 if (neighbor[Block::FACE_DOWN] != &other) {
241 neighbor[Block::FACE_DOWN] = &other;
242 other.neighbor[Block::FACE_UP] = this;
243 for (int z = 0; z < Depth(); ++z) {
244 for (int x = 0; x < Width(); ++x) {
246 Pos other_pos(x, Height() - 1, z);
247 if (GetLight(my_pos) > 0) {
248 light_queue.emplace(this, my_pos);
250 if (other.GetLight(other_pos) > 0) {
251 light_queue.emplace(&other, other_pos);
257 } else if (other.position == position + Pos(0, 1, 0)) {
258 if (neighbor[Block::FACE_UP] != &other) {
259 neighbor[Block::FACE_UP] = &other;
260 other.neighbor[Block::FACE_DOWN] = this;
261 for (int z = 0; z < Depth(); ++z) {
262 for (int x = 0; x < Width(); ++x) {
263 Pos my_pos(x, Height() - 1, z);
264 Pos other_pos(x, 0, z);
265 if (GetLight(my_pos) > 0) {
266 light_queue.emplace(this, my_pos);
268 if (other.GetLight(other_pos) > 0) {
269 light_queue.emplace(&other, other_pos);
275 } else if (other.position == position + Pos(0, 0, -1)) {
276 if (neighbor[Block::FACE_BACK] != &other) {
277 neighbor[Block::FACE_BACK] = &other;
278 other.neighbor[Block::FACE_FRONT] = this;
279 for (int y = 0; y < Height(); ++y) {
280 for (int x = 0; x < Width(); ++x) {
282 Pos other_pos(x, y, Depth() - 1);
283 if (GetLight(my_pos) > 0) {
284 light_queue.emplace(this, my_pos);
286 if (other.GetLight(other_pos) > 0) {
287 light_queue.emplace(&other, other_pos);
293 } else if (other.position == position + Pos(0, 0, 1)) {
294 if (neighbor[Block::FACE_FRONT] != &other) {
295 neighbor[Block::FACE_FRONT] = &other;
296 other.neighbor[Block::FACE_BACK] = this;
297 for (int y = 0; y < Height(); ++y) {
298 for (int x = 0; x < Width(); ++x) {
299 Pos my_pos(x, y, Depth() - 1);
300 Pos other_pos(x, y, 0);
301 if (GetLight(my_pos) > 0) {
302 light_queue.emplace(this, my_pos);
304 if (other.GetLight(other_pos) > 0) {
305 light_queue.emplace(&other, other_pos);
314 void Chunk::ClearNeighbors() {
315 for (int i = 0; i < Block::FACE_COUNT; ++i) {
316 neighbor[i] = nullptr;
320 void Chunk::Unlink() {
321 if (neighbor[Block::FACE_UP]) {
322 neighbor[Block::FACE_UP]->neighbor[Block::FACE_DOWN] = nullptr;
324 if (neighbor[Block::FACE_DOWN]) {
325 neighbor[Block::FACE_DOWN]->neighbor[Block::FACE_UP] = nullptr;
327 if (neighbor[Block::FACE_LEFT]) {
328 neighbor[Block::FACE_LEFT]->neighbor[Block::FACE_RIGHT] = nullptr;
330 if (neighbor[Block::FACE_RIGHT]) {
331 neighbor[Block::FACE_RIGHT]->neighbor[Block::FACE_LEFT] = nullptr;
333 if (neighbor[Block::FACE_FRONT]) {
334 neighbor[Block::FACE_FRONT]->neighbor[Block::FACE_BACK] = nullptr;
336 if (neighbor[Block::FACE_BACK]) {
337 neighbor[Block::FACE_BACK]->neighbor[Block::FACE_FRONT] = nullptr;
341 void Chunk::Relink() {
342 if (neighbor[Block::FACE_UP]) {
343 neighbor[Block::FACE_UP]->neighbor[Block::FACE_DOWN] = this;
345 if (neighbor[Block::FACE_DOWN]) {
346 neighbor[Block::FACE_DOWN]->neighbor[Block::FACE_UP] = this;
348 if (neighbor[Block::FACE_LEFT]) {
349 neighbor[Block::FACE_LEFT]->neighbor[Block::FACE_RIGHT] = this;
351 if (neighbor[Block::FACE_RIGHT]) {
352 neighbor[Block::FACE_RIGHT]->neighbor[Block::FACE_LEFT] = this;
354 if (neighbor[Block::FACE_FRONT]) {
355 neighbor[Block::FACE_FRONT]->neighbor[Block::FACE_BACK] = this;
357 if (neighbor[Block::FACE_BACK]) {
358 neighbor[Block::FACE_BACK]->neighbor[Block::FACE_FRONT] = this;
363 void Chunk::SetLight(int index, int level) {
364 if (light[index] != level) {
365 light[index] = level;
370 int Chunk::GetLight(int index) const {
374 float Chunk::GetVertexLight(int index, const BlockModel::Position &vtx, const BlockModel::Normal &norm) const {
375 float light = GetLight(index);
376 Chunk::Pos pos(ToPos(index));
378 Block::Face direct_face(Block::NormalFace(norm));
380 BlockLookup direct(const_cast<Chunk *>(this), pos, Block::NormalFace(norm));
382 float direct_light = direct.GetLight();
383 if (direct_light > light) {
384 light = direct_light;
388 // cheap alternative until AO etc are implemented
389 // to tell the faces apart
391 if (direct_face == Block::FACE_LEFT || direct_face == Block::FACE_RIGHT) {
393 } else if (direct_face == Block::FACE_FRONT || direct_face == Block::FACE_BACK) {
401 bool Chunk::IsSurface(const Pos &pos) const {
402 const Block &block = BlockAt(pos);
403 if (!Type(block).visible) {
406 for (int face = 0; face < Block::FACE_COUNT; ++face) {
407 const Block *next = FindNext(pos, Block::Face(face));
408 if (!next || !Type(*next).visible) {
416 void Chunk::Allocate() {
417 blocks.resize(Size(), Block(0));
418 light.resize(Size(), 0);
430 bool Chunk::Intersection(
437 // TODO: should be possible to heavily optimize this
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, ++id) {
444 if (!Type(blocks[id]).visible) {
449 if (Type(blocks[id]).shape->Intersects(ray, M * ToTransform(id), cur_dist, cur_norm)) {
450 if (cur_dist < dist) {
463 normal = glm::vec3(BlockAt(blkid).Transform() * glm::vec4(normal, 0.0f));
468 void Chunk::Position(const Pos &pos) {
472 glm::mat4 Chunk::Transform(const Pos &offset) const {
473 return glm::translate((position - offset) * Extent());
479 BlockModel::Buffer buf;
483 void Chunk::CheckUpdate() {
489 void Chunk::Update() {
490 int vtx_count = 0, idx_count = 0;
491 for (const auto &block : blocks) {
492 const Shape *shape = Type(block).shape;
493 vtx_count += shape->VertexCount();
494 idx_count += shape->VertexIndexCount();
497 buf.Reserve(vtx_count, idx_count);
499 BlockModel::Index vtx_counter = 0;
500 for (size_t i = 0; i < Size(); ++i) {
501 const BlockType &type = Type(blocks[i]);
503 if (!type.visible || Obstructed(i)) continue;
505 type.FillBlockModel(buf, ToTransform(i), vtx_counter);
506 size_t vtx_begin = vtx_counter;
507 vtx_counter += type.shape->VertexCount();
509 for (size_t vtx = vtx_begin; vtx < vtx_counter; ++vtx) {
510 buf.lights.emplace_back(GetVertexLight(i, buf.vertices[vtx], buf.normals[vtx]));
518 bool Chunk::Obstructed(int idx) const {
519 Chunk::Pos pos(ToPos(idx));
521 Chunk::Pos left_pos(pos + Chunk::Pos(-1, 0, 0));
522 const Block *left_block = nullptr;
523 if (InBounds(left_pos)) {
524 left_block = &BlockAt(left_pos);
525 } else if (HasNeighbor(Block::FACE_LEFT)) {
526 left_pos += Chunk::Pos(Width(), 0, 0);
527 left_block = &GetNeighbor(Block::FACE_LEFT).BlockAt(left_pos);
531 if (!Type(*left_block).FaceFilled(*left_block, Block::FACE_RIGHT)) {
535 Chunk::Pos right_pos(pos + Chunk::Pos(1, 0, 0));
536 const Block *right_block = nullptr;
537 if (InBounds(right_pos)) {
538 right_block = &BlockAt(right_pos);
539 } else if (HasNeighbor(Block::FACE_RIGHT)) {
540 right_pos += Chunk::Pos(-Width(), 0, 0);
541 right_block = &GetNeighbor(Block::FACE_RIGHT).BlockAt(right_pos);
545 if (!Type(*right_block).FaceFilled(*right_block, Block::FACE_LEFT)) {
549 Chunk::Pos down_pos(pos + Chunk::Pos(0, -1, 0));
550 const Block *down_block = nullptr;
551 if (InBounds(down_pos)) {
552 down_block = &BlockAt(down_pos);
553 } else if (HasNeighbor(Block::FACE_DOWN)) {
554 down_pos += Chunk::Pos(0, Height(), 0);
555 down_block = &GetNeighbor(Block::FACE_DOWN).BlockAt(down_pos);
559 if (!Type(*down_block).FaceFilled(*down_block, Block::FACE_UP)) {
563 Chunk::Pos up_pos(pos + Chunk::Pos(0, 1, 0));
564 const Block *up_block = nullptr;
565 if (InBounds(up_pos)) {
566 up_block = &BlockAt(up_pos);
567 } else if (HasNeighbor(Block::FACE_UP)) {
568 up_pos += Chunk::Pos(0, -Height(), 0);
569 up_block = &GetNeighbor(Block::FACE_UP).BlockAt(up_pos);
573 if (!Type(*up_block).FaceFilled(*up_block, Block::FACE_DOWN)) {
577 Chunk::Pos back_pos(pos + Chunk::Pos(0, 0, -1));
578 const Block *back_block = nullptr;
579 if (InBounds(back_pos)) {
580 back_block = &BlockAt(back_pos);
581 } else if (HasNeighbor(Block::FACE_BACK)) {
582 back_pos += Chunk::Pos(0, 0, Depth());
583 back_block = &GetNeighbor(Block::FACE_BACK).BlockAt(back_pos);
587 if (!Type(*back_block).FaceFilled(*back_block, Block::FACE_FRONT)) {
591 Chunk::Pos front_pos(pos + Chunk::Pos(0, 0, 1));
592 const Block *front_block = nullptr;
593 if (InBounds(front_pos)) {
594 front_block = &BlockAt(front_pos);
595 } else if (HasNeighbor(Block::FACE_FRONT)) {
596 front_pos += Chunk::Pos(0, 0, -Depth());
597 front_block = &GetNeighbor(Block::FACE_FRONT).BlockAt(front_pos);
601 if (!Type(*front_block).FaceFilled(*front_block, Block::FACE_BACK)) {
608 glm::mat4 Chunk::ToTransform(int idx) const {
609 return glm::translate(glm::mat4(1.0f), ToCoords(idx)) * blocks[idx].Transform();
613 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p)
615 while (pos.x >= Chunk::Width()) {
616 if (chunk->HasNeighbor(Block::FACE_RIGHT)) {
617 chunk = &chunk->GetNeighbor(Block::FACE_RIGHT);
618 pos.x -= Chunk::Width();
625 if (chunk->HasNeighbor(Block::FACE_LEFT)) {
626 chunk = &chunk->GetNeighbor(Block::FACE_LEFT);
627 pos.x += Chunk::Width();
633 while (pos.y >= Chunk::Height()) {
634 if (chunk->HasNeighbor(Block::FACE_UP)) {
635 chunk = &chunk->GetNeighbor(Block::FACE_UP);
636 pos.y -= Chunk::Height();
643 if (chunk->HasNeighbor(Block::FACE_DOWN)) {
644 chunk = &chunk->GetNeighbor(Block::FACE_DOWN);
645 pos.y += Chunk::Height();
651 while (pos.z >= Chunk::Depth()) {
652 if (chunk->HasNeighbor(Block::FACE_FRONT)) {
653 chunk = &chunk->GetNeighbor(Block::FACE_FRONT);
654 pos.z -= Chunk::Depth();
661 if (chunk->HasNeighbor(Block::FACE_BACK)) {
662 chunk = &chunk->GetNeighbor(Block::FACE_BACK);
663 pos.z += Chunk::Depth();
671 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face face)
673 pos += Block::FaceNormal(face);
674 if (!Chunk::InBounds(pos)) {
675 pos -= Block::FaceNormal(face) * Chunk::Extent();
676 chunk = &chunk->GetNeighbor(face);
681 ChunkLoader::ChunkLoader(const Config &config, const BlockTypeRegistry ®, const Generator &gen)
688 , load_dist(config.load_dist)
689 , unload_dist(config.unload_dist) {
697 explicit ChunkLess(const Chunk::Pos &base)
700 bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const {
701 Chunk::Pos da(base - a);
702 Chunk::Pos db(base - b);
704 da.x * da.x + da.y * da.y + da.z * da.z <
705 db.x * db.x + db.y * db.y + db.z * db.z;
714 void ChunkLoader::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
715 for (int z = from.z; z < to.z; ++z) {
716 for (int y = from.y; y < to.y; ++y) {
717 for (int x = from.x; x < to.x; ++x) {
718 Chunk::Pos pos(x, y, z);
721 } else if (pos == base) {
725 // for (int i = 0; i < 16; ++i) {
726 // for (int j = 0; j < 16; ++j) {
727 // loaded.back().SetBlock(Chunk::Pos{ i, j, 0 }, Block(1));
728 // loaded.back().SetBlock(Chunk::Pos{ i, j, 15 }, Block(1));
729 // loaded.back().SetBlock(Chunk::Pos{ 0, j, i }, Block(1));
730 // loaded.back().SetBlock(Chunk::Pos{ 15, j, i }, Block(1));
733 // loaded.back().SetBlock(Chunk::Pos{ 1, 0, 1 }, Block(13));
734 // loaded.back().SetBlock(Chunk::Pos{ 14, 0, 1 }, Block(13));
735 // loaded.back().SetBlock(Chunk::Pos{ 1, 0, 14 }, Block(13));
736 // loaded.back().SetBlock(Chunk::Pos{ 14, 0, 14 }, Block(13));
737 // loaded.back().SetBlock(Chunk::Pos{ 1, 15, 1 }, Block(13));
738 // loaded.back().SetBlock(Chunk::Pos{ 14, 15, 1 }, Block(13));
739 // loaded.back().SetBlock(Chunk::Pos{ 1, 15, 14 }, Block(13));
740 // loaded.back().SetBlock(Chunk::Pos{ 14, 15, 14 }, Block(13));
741 // loaded.back().SetBlock(Chunk::Pos{ 7, 7, 0 }, Block(13));
742 // loaded.back().SetBlock(Chunk::Pos{ 8, 7, 0 }, Block(13));
743 // loaded.back().SetBlock(Chunk::Pos{ 7, 8, 0 }, Block(13));
744 // loaded.back().SetBlock(Chunk::Pos{ 8, 8, 0 }, Block(13));
745 // loaded.back().SetBlock(Chunk::Pos{ 7, 7, 15 }, Block(13));
746 // loaded.back().SetBlock(Chunk::Pos{ 8, 7, 15 }, Block(13));
747 // loaded.back().SetBlock(Chunk::Pos{ 7, 8, 15 }, Block(13));
748 // loaded.back().SetBlock(Chunk::Pos{ 8, 8, 15 }, Block(13));
749 // loaded.back().SetBlock(Chunk::Pos{ 0, 7, 7 }, Block(13));
750 // loaded.back().SetBlock(Chunk::Pos{ 0, 7, 8 }, Block(13));
751 // loaded.back().SetBlock(Chunk::Pos{ 0, 8, 7 }, Block(13));
752 // loaded.back().SetBlock(Chunk::Pos{ 0, 8, 8 }, Block(13));
753 // loaded.back().SetBlock(Chunk::Pos{ 15, 7, 7 }, Block(13));
754 // loaded.back().SetBlock(Chunk::Pos{ 15, 7, 8 }, Block(13));
755 // loaded.back().SetBlock(Chunk::Pos{ 15, 8, 7 }, Block(13));
756 // loaded.back().SetBlock(Chunk::Pos{ 15, 8, 8 }, Block(13));
757 // loaded.back().Invalidate();
758 // loaded.back().CheckUpdate();
760 // orientation testing
761 // for (int i = 0; i < Block::FACE_COUNT; ++i) {
762 // for (int j = 0; j < Block::TURN_COUNT; ++j) {
763 // loaded.back().BlockAt(512 * j + 2 * i) = Block(3 * (j + 1), Block::Face(i), Block::Turn(j));
766 // loaded.back().Invalidate();
767 // loaded.back().CheckUpdate();
769 to_generate.emplace_back(pos);
774 to_generate.sort(ChunkLess(base));
777 Chunk &ChunkLoader::Generate(const Chunk::Pos &pos) {
778 loaded.emplace_back(reg);
779 Chunk &chunk = loaded.back();
787 void ChunkLoader::Insert(Chunk &chunk) {
788 for (Chunk &other : loaded) {
789 chunk.SetNeighbor(other);
793 void ChunkLoader::Remove(Chunk &chunk) {
797 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) {
798 for (Chunk &chunk : loaded) {
799 if (chunk.Position() == pos) {
806 bool ChunkLoader::Queued(const Chunk::Pos &pos) {
807 for (const Chunk::Pos &chunk : to_generate) {
815 bool ChunkLoader::Known(const Chunk::Pos &pos) {
816 if (Loaded(pos)) return true;
820 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
821 Chunk *chunk = Loaded(pos);
826 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
828 to_generate.erase(iter);
833 return Generate(pos);
836 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
837 if (new_base == base) {
842 // unload far away chunks
843 for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
844 if (std::abs(base.x - iter->Position().x) > unload_dist
845 || std::abs(base.y - iter->Position().y) > unload_dist
846 || std::abs(base.z - iter->Position().z) > unload_dist) {
850 to_free.splice(to_free.end(), loaded, saved);
855 // abort far away queued chunks
856 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
857 if (std::abs(base.x - iter->x) > unload_dist
858 || std::abs(base.y - iter->y) > unload_dist
859 || std::abs(base.z - iter->z) > unload_dist) {
860 iter = to_generate.erase(iter);
865 // add missing new chunks
866 GenerateSurrounding(base);
869 void ChunkLoader::GenerateSurrounding(const Chunk::Pos &pos) {
870 const Chunk::Pos offset(load_dist, load_dist, load_dist);
871 Generate(pos - offset, pos + offset);
874 void ChunkLoader::Update() {
876 if (!to_generate.empty()) {
877 Chunk::Pos pos(to_generate.front());
879 for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
880 if (iter->Position() == pos) {
882 loaded.splice(loaded.end(), to_free, iter);
889 if (to_free.empty()) {
890 loaded.emplace_back(reg);
892 to_free.front().ClearNeighbors();
893 loaded.splice(loaded.end(), to_free, to_free.begin());
896 Chunk &chunk = loaded.back();
902 to_generate.pop_front();
905 if (!reused && !to_free.empty()) {