]> git.localhorst.tv Git - blank.git/blob - src/chunk.cpp
minor optimizations in chunk
[blank.git] / src / chunk.cpp
1 #include "chunk.hpp"
2
3 #include "generator.hpp"
4
5 #include <algorithm>
6 #include <limits>
7 #include <queue>
8
9
10 namespace blank {
11
12 constexpr int Chunk::width;
13 constexpr int Chunk::height;
14 constexpr int Chunk::depth;
15 constexpr int Chunk::size;
16
17
18 Chunk::Chunk(const BlockTypeRegistry &types) noexcept
19 : types(&types)
20 , neighbor{0}
21 , blocks{}
22 , light{0}
23 , model()
24 , position(0, 0, 0)
25 , dirty(false) {
26
27 }
28
29 Chunk::Chunk(Chunk &&other) noexcept
30 : types(other.types)
31 , model(std::move(other.model))
32 , position(other.position)
33 , dirty(other.dirty) {
34         std::copy(other.neighbor, other.neighbor + sizeof(neighbor), neighbor);
35         std::copy(other.blocks, other.blocks + sizeof(blocks), blocks);
36         std::copy(other.light, other.light + sizeof(light), light);
37 }
38
39 Chunk &Chunk::operator =(Chunk &&other) noexcept {
40         types = other.types;
41         std::copy(other.neighbor, other.neighbor + sizeof(neighbor), neighbor);
42         std::copy(other.blocks, other.blocks + sizeof(blocks), blocks);
43         std::copy(other.light, other.light + sizeof(light), light);
44         model = std::move(other.model);
45         position = other.position;
46         dirty = other.dirty;
47         return *this;
48 }
49
50
51 namespace {
52
53 struct SetNode {
54
55         Chunk *chunk;
56         Chunk::Pos pos;
57
58         SetNode(Chunk *chunk, Chunk::Pos pos)
59         : chunk(chunk), pos(pos) { }
60
61         int Get() const noexcept { return chunk->GetLight(pos); }
62         void Set(int level) noexcept { chunk->SetLight(pos, level); }
63
64         bool HasNext(Block::Face face) noexcept {
65                 const BlockLookup next(chunk, pos, face);
66                 return next && !next.GetType().block_light;
67         }
68         SetNode GetNext(Block::Face face) noexcept {
69                 const BlockLookup next(chunk, pos, face);
70                 return SetNode(&next.GetChunk(), next.GetBlockPos());
71         }
72
73 };
74
75 struct UnsetNode
76 : public SetNode {
77
78         int level;
79
80         UnsetNode(Chunk *chunk, Chunk::Pos pos)
81         : SetNode(chunk, pos), level(Get()) { }
82
83         UnsetNode(const SetNode &set)
84         : SetNode(set), level(Get()) { }
85
86
87         bool HasNext(Block::Face face) noexcept {
88                 const BlockLookup next(chunk, pos, face);
89                 return next;
90         }
91         UnsetNode GetNext(Block::Face face) noexcept { return UnsetNode(SetNode::GetNext(face)); }
92
93 };
94
95 std::queue<SetNode> light_queue;
96 std::queue<UnsetNode> dark_queue;
97
98 void work_light() noexcept {
99         while (!light_queue.empty()) {
100                 SetNode node = light_queue.front();
101                 light_queue.pop();
102
103                 int level = node.Get() - 1;
104                 for (int face = 0; face < Block::FACE_COUNT; ++face) {
105                         if (node.HasNext(Block::Face(face))) {
106                                 SetNode other = node.GetNext(Block::Face(face));
107                                 if (other.Get() < level) {
108                                         other.Set(level);
109                                         light_queue.emplace(other);
110                                 }
111                         }
112                 }
113         }
114 }
115
116 void work_dark() noexcept {
117         while (!dark_queue.empty()) {
118                 UnsetNode node = dark_queue.front();
119                 dark_queue.pop();
120
121                 for (int face = 0; face < Block::FACE_COUNT; ++face) {
122                         if (node.HasNext(Block::Face(face))) {
123                                 UnsetNode other = node.GetNext(Block::Face(face));
124                                 // TODO: if there a light source here with the same level this will err
125                                 if (other.Get() != 0 && other.Get() < node.level) {
126                                         other.Set(0);
127                                         dark_queue.emplace(other);
128                                 } else {
129                                         light_queue.emplace(other);
130                                 }
131                         }
132                 }
133         }
134 }
135
136 }
137
138 void Chunk::SetBlock(int index, const Block &block) noexcept {
139         const BlockType &old_type = Type(blocks[index]);
140         const BlockType &new_type = Type(block);
141
142         blocks[index] = block;
143
144         if (&old_type == &new_type) return;
145
146         if (new_type.luminosity > old_type.luminosity) {
147                 // light added
148                 SetLight(index, new_type.luminosity);
149                 light_queue.emplace(this, ToPos(index));
150                 work_light();
151         } else if (new_type.luminosity < old_type.luminosity) {
152                 // light removed
153                 dark_queue.emplace(this, ToPos(index));
154                 SetLight(index, 0);
155                 work_dark();
156                 SetLight(index, new_type.luminosity);
157                 light_queue.emplace(this, ToPos(index));
158                 work_light();
159         } else if (new_type.block_light && !old_type.block_light) {
160                 // obstacle added
161                 if (GetLight(index) > 0) {
162                         dark_queue.emplace(this, ToPos(index));
163                         SetLight(index, 0);
164                         work_dark();
165                         work_light();
166                 }
167         } else if (!new_type.block_light && old_type.block_light) {
168                 // obstacle removed
169                 int level = 0;
170                 for (int face = 0; face < Block::FACE_COUNT; ++face) {
171                         BlockLookup next_block(this, ToPos(index), Block::Face(face));
172                         if (next_block) {
173                                 level = std::min(level, next_block.GetLight());
174                         }
175                 }
176                 if (level > 1) {
177                         SetLight(index, level - 1);
178                         light_queue.emplace(this, ToPos(index));
179                         work_light();
180                 }
181         }
182 }
183
184 void Chunk::SetNeighbor(Chunk &other) noexcept {
185         if (other.position == position + Pos(-1, 0, 0)) {
186                 if (neighbor[Block::FACE_LEFT] != &other) {
187                         neighbor[Block::FACE_LEFT] = &other;
188                         other.neighbor[Block::FACE_RIGHT] = this;
189                         for (int z = 0; z < depth; ++z) {
190                                 for (int y = 0; y < height; ++y) {
191                                         Pos my_pos(0, y, z);
192                                         Pos other_pos(width - 1, y, z);
193                                         if (GetLight(my_pos) > 0) {
194                                                 light_queue.emplace(this, my_pos);
195                                         }
196                                         if (other.GetLight(other_pos) > 0) {
197                                                 light_queue.emplace(&other, other_pos);
198                                         }
199                                 }
200                         }
201                         work_light();
202                 }
203         } else if (other.position == position + Pos(1, 0, 0)) {
204                 if (neighbor[Block::FACE_RIGHT] != &other) {
205                         neighbor[Block::FACE_RIGHT] = &other;
206                         other.neighbor[Block::FACE_LEFT] = this;
207                         for (int z = 0; z < depth; ++z) {
208                                 for (int y = 0; y < height; ++y) {
209                                         Pos my_pos(width - 1, y, z);
210                                         Pos other_pos(0, y, z);
211                                         if (GetLight(my_pos) > 0) {
212                                                 light_queue.emplace(this, my_pos);
213                                         }
214                                         if (other.GetLight(other_pos) > 0) {
215                                                 light_queue.emplace(&other, other_pos);
216                                         }
217                                 }
218                         }
219                         work_light();
220                 }
221         } else if (other.position == position + Pos(0, -1, 0)) {
222                 if (neighbor[Block::FACE_DOWN] != &other) {
223                         neighbor[Block::FACE_DOWN] = &other;
224                         other.neighbor[Block::FACE_UP] = this;
225                         for (int z = 0; z < depth; ++z) {
226                                 for (int x = 0; x < width; ++x) {
227                                         Pos my_pos(x, 0, z);
228                                         Pos other_pos(x, height - 1, z);
229                                         if (GetLight(my_pos) > 0) {
230                                                 light_queue.emplace(this, my_pos);
231                                         }
232                                         if (other.GetLight(other_pos) > 0) {
233                                                 light_queue.emplace(&other, other_pos);
234                                         }
235                                 }
236                         }
237                         work_light();
238                 }
239         } else if (other.position == position + Pos(0, 1, 0)) {
240                 if (neighbor[Block::FACE_UP] != &other) {
241                         neighbor[Block::FACE_UP] = &other;
242                         other.neighbor[Block::FACE_DOWN] = this;
243                         for (int z = 0; z < depth; ++z) {
244                                 for (int x = 0; x < width; ++x) {
245                                         Pos my_pos(x, height - 1, z);
246                                         Pos other_pos(x, 0, z);
247                                         if (GetLight(my_pos) > 0) {
248                                                 light_queue.emplace(this, my_pos);
249                                         }
250                                         if (other.GetLight(other_pos) > 0) {
251                                                 light_queue.emplace(&other, other_pos);
252                                         }
253                                 }
254                         }
255                         work_light();
256                 }
257         } else if (other.position == position + Pos(0, 0, -1)) {
258                 if (neighbor[Block::FACE_BACK] != &other) {
259                         neighbor[Block::FACE_BACK] = &other;
260                         other.neighbor[Block::FACE_FRONT] = this;
261                         for (int y = 0; y < height; ++y) {
262                                 for (int x = 0; x < width; ++x) {
263                                         Pos my_pos(x, y, 0);
264                                         Pos other_pos(x, y, depth - 1);
265                                         if (GetLight(my_pos) > 0) {
266                                                 light_queue.emplace(this, my_pos);
267                                         }
268                                         if (other.GetLight(other_pos) > 0) {
269                                                 light_queue.emplace(&other, other_pos);
270                                         }
271                                 }
272                         }
273                         work_light();
274                 }
275         } else if (other.position == position + Pos(0, 0, 1)) {
276                 if (neighbor[Block::FACE_FRONT] != &other) {
277                         neighbor[Block::FACE_FRONT] = &other;
278                         other.neighbor[Block::FACE_BACK] = this;
279                         for (int y = 0; y < height; ++y) {
280                                 for (int x = 0; x < width; ++x) {
281                                         Pos my_pos(x, y, depth - 1);
282                                         Pos other_pos(x, y, 0);
283                                         if (GetLight(my_pos) > 0) {
284                                                 light_queue.emplace(this, my_pos);
285                                         }
286                                         if (other.GetLight(other_pos) > 0) {
287                                                 light_queue.emplace(&other, other_pos);
288                                         }
289                                 }
290                         }
291                         work_light();
292                 }
293         }
294 }
295
296 void Chunk::ClearNeighbors() noexcept {
297         for (int i = 0; i < Block::FACE_COUNT; ++i) {
298                 neighbor[i] = nullptr;
299         }
300 }
301
302 void Chunk::Unlink() noexcept {
303         for (int face = 0; face < Block::FACE_COUNT; ++face) {
304                 if (neighbor[face]) {
305                         neighbor[face]->neighbor[Block::Opposite(Block::Face(face))] = nullptr;
306                 }
307         }
308 }
309
310 void Chunk::Relink() noexcept {
311         for (int face = 0; face < Block::FACE_COUNT; ++face) {
312                 if (neighbor[face]) {
313                         neighbor[face]->neighbor[Block::Opposite(Block::Face(face))] = this;
314                 }
315         }
316 }
317
318
319 void Chunk::SetLight(int index, int level) noexcept {
320         if (light[index] != level) {
321                 light[index] = level;
322                 Invalidate();
323         }
324 }
325
326 int Chunk::GetLight(int index) const noexcept {
327         return light[index];
328 }
329
330 float Chunk::GetVertexLight(const Pos &pos, const BlockModel::Position &vtx, const Model::Normal &norm) const noexcept {
331         int index = ToIndex(pos);
332         float light = GetLight(index);
333
334         Block::Face direct_face(Block::NormalFace(norm));
335         // tis okay
336         BlockLookup direct(const_cast<Chunk *>(this), pos, Block::NormalFace(norm));
337         if (direct) {
338                 float direct_light = direct.GetLight();
339                 if (direct_light > light) {
340                         light = direct_light;
341                 }
342         } else {
343                 return light;
344         }
345
346         if (Type(BlockAt(index)).luminosity > 0 || direct.GetType().block_light) {
347                 return light;
348         }
349
350         Block::Face edge[2];
351         switch (Block::Axis(direct_face)) {
352                 case 0: // X
353                         edge[0] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
354                         edge[1] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
355                         break;
356                 case 1: // Y
357                         edge[0] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
358                         edge[1] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
359                         break;
360                 case 2: // Z
361                         edge[0] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
362                         edge[1] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
363                         break;
364         }
365
366         int num = 1;
367         int occlusion = 0;
368
369         BlockLookup next[2] = {
370                 direct.Next(edge[0]),
371                 direct.Next(edge[1]),
372         };
373
374         if (next[0]) {
375                 if (next[0].GetType().block_light) {
376                         ++occlusion;
377                 } else {
378                         light += next[0].GetLight();
379                         ++num;
380                 }
381         }
382         if (next[1]) {
383                 if (next[1].GetType().block_light) {
384                         ++occlusion;
385                 } else {
386                         light += next[1].GetLight();
387                         ++num;
388                 }
389         }
390         if (occlusion < 2) {
391                 if (next[0]) {
392                         BlockLookup corner = next[0].Next(edge[1]);
393                         if (corner) {
394                                 if (corner.GetType().block_light) {
395                                         ++occlusion;
396                                 } else {
397                                         light += corner.GetLight();
398                                         ++num;
399                                 }
400                         }
401                 } else if (next[1]) {
402                         BlockLookup corner = next[1].Next(edge[0]);
403                         if (corner) {
404                                 if (corner.GetType().block_light) {
405                                         ++occlusion;
406                                 } else {
407                                         light += corner.GetLight();
408                                         ++num;
409                                 }
410                         }
411                 }
412         } else {
413                 ++occlusion;
414         }
415
416         return (light / num) - (occlusion * 0.8f);
417 }
418
419
420 bool Chunk::IsSurface(const Pos &pos) const noexcept {
421         const Block &block = BlockAt(pos);
422         if (!Type(block).visible) {
423                 return false;
424         }
425         for (int face = 0; face < Block::FACE_COUNT; ++face) {
426                 BlockLookup next = BlockLookup(const_cast<Chunk *>(this), pos, Block::Face(face));
427                 if (!next || !next.GetType().visible) {
428                         return true;
429                 }
430         }
431         return false;
432 }
433
434
435 void Chunk::Draw() noexcept {
436         if (dirty) {
437                 Update();
438         }
439         model.Draw();
440 }
441
442
443 bool Chunk::Intersection(
444         const Ray &ray,
445         const glm::mat4 &M,
446         int &blkid,
447         float &dist,
448         glm::vec3 &normal
449 ) const noexcept {
450         // TODO: should be possible to heavily optimize this
451         int idx = 0;
452         blkid = -1;
453         dist = std::numeric_limits<float>::infinity();
454         for (int z = 0; z < depth; ++z) {
455                 for (int y = 0; y < height; ++y) {
456                         for (int x = 0; x < width; ++x, ++idx) {
457                                 const BlockType &type = Type(idx);
458                                 if (!type.visible) {
459                                         continue;
460                                 }
461                                 float cur_dist;
462                                 glm::vec3 cur_norm;
463                                 if (type.shape->Intersects(ray, M * ToTransform(Pos(x, y, z), idx), cur_dist, cur_norm)) {
464                                         if (cur_dist < dist) {
465                                                 blkid = idx;
466                                                 dist = cur_dist;
467                                                 normal = cur_norm;
468                                         }
469                                 }
470                         }
471                 }
472         }
473
474         if (blkid < 0) {
475                 return false;
476         } else {
477                 normal = glm::vec3(BlockAt(blkid).Transform() * glm::vec4(normal, 0.0f));
478                 return true;
479         }
480 }
481
482
483 namespace {
484
485 BlockModel::Buffer buf;
486
487 }
488
489 void Chunk::CheckUpdate() noexcept {
490         if (dirty) {
491                 Update();
492         }
493 }
494
495 void Chunk::Update() noexcept {
496         int vtx_count = 0, idx_count = 0;
497         for (const auto &block : blocks) {
498                 const Shape *shape = Type(block).shape;
499                 vtx_count += shape->VertexCount();
500                 idx_count += shape->VertexIndexCount();
501         }
502         buf.Clear();
503         buf.Reserve(vtx_count, idx_count);
504
505         int idx = 0;
506         BlockModel::Index vtx_counter = 0;
507         for (size_t z = 0; z < depth; ++z) {
508                 for (size_t y = 0; y < height; ++y) {
509                         for (size_t x = 0; x < width; ++x, ++idx) {
510                                 const BlockType &type = Type(BlockAt(idx));
511                                 const Pos pos(x, y, z);
512
513                                 if (!type.visible || Obstructed(pos).All()) continue;
514
515                                 type.FillBlockModel(buf, ToTransform(pos, idx), vtx_counter);
516                                 size_t vtx_begin = vtx_counter;
517                                 vtx_counter += type.shape->VertexCount();
518
519                                 for (size_t vtx = vtx_begin; vtx < vtx_counter; ++vtx) {
520                                         buf.lights.emplace_back(GetVertexLight(
521                                                 pos,
522                                                 buf.vertices[vtx],
523                                                 type.shape->VertexNormal(vtx - vtx_begin, BlockAt(idx).Transform())
524                                         ));
525                                 }
526                         }
527                 }
528         }
529
530         model.Update(buf);
531         dirty = false;
532 }
533
534 Block::FaceSet Chunk::Obstructed(const Pos &pos) const noexcept {
535         Block::FaceSet result;
536
537         for (int f = 0; f < Block::FACE_COUNT; ++f) {
538                 Block::Face face = Block::Face(f);
539                 BlockLookup next(const_cast<Chunk *>(this), pos, face);
540                 if (next && next.GetType().FaceFilled(next.GetBlock(), Block::Opposite(face))) {
541                         result.Set(face);
542                 }
543         }
544
545         return result;
546 }
547
548 glm::mat4 Chunk::ToTransform(const Pos &pos, int idx) const noexcept {
549         return glm::translate(ToCoords(pos)) * BlockAt(idx).Transform();
550 }
551
552
553 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p) noexcept
554 : chunk(c), pos(p) {
555         while (pos.x >= Chunk::width) {
556                 if (chunk->HasNeighbor(Block::FACE_RIGHT)) {
557                         chunk = &chunk->GetNeighbor(Block::FACE_RIGHT);
558                         pos.x -= Chunk::width;
559                 } else {
560                         chunk = nullptr;
561                         return;
562                 }
563         }
564         while (pos.x < 0) {
565                 if (chunk->HasNeighbor(Block::FACE_LEFT)) {
566                         chunk = &chunk->GetNeighbor(Block::FACE_LEFT);
567                         pos.x += Chunk::width;
568                 } else {
569                         chunk = nullptr;
570                         return;
571                 }
572         }
573         while (pos.y >= Chunk::height) {
574                 if (chunk->HasNeighbor(Block::FACE_UP)) {
575                         chunk = &chunk->GetNeighbor(Block::FACE_UP);
576                         pos.y -= Chunk::height;
577                 } else {
578                         chunk = nullptr;
579                         return;
580                 }
581         }
582         while (pos.y < 0) {
583                 if (chunk->HasNeighbor(Block::FACE_DOWN)) {
584                         chunk = &chunk->GetNeighbor(Block::FACE_DOWN);
585                         pos.y += Chunk::height;
586                 } else {
587                         chunk = nullptr;
588                         return;
589                 }
590         }
591         while (pos.z >= Chunk::depth) {
592                 if (chunk->HasNeighbor(Block::FACE_FRONT)) {
593                         chunk = &chunk->GetNeighbor(Block::FACE_FRONT);
594                         pos.z -= Chunk::depth;
595                 } else {
596                         chunk = nullptr;
597                         return;
598                 }
599         }
600         while (pos.z < 0) {
601                 if (chunk->HasNeighbor(Block::FACE_BACK)) {
602                         chunk = &chunk->GetNeighbor(Block::FACE_BACK);
603                         pos.z += Chunk::depth;
604                 } else {
605                         chunk = nullptr;
606                         return;
607                 }
608         }
609 }
610
611 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face face) noexcept
612 : chunk(c), pos(p) {
613         pos += Block::FaceNormal(face);
614         if (!Chunk::InBounds(pos)) {
615                 pos -= Block::FaceNormal(face) * Chunk::Extent();
616                 chunk = &chunk->GetNeighbor(face);
617         }
618 }
619
620
621 ChunkLoader::ChunkLoader(const Config &config, const BlockTypeRegistry &reg, const Generator &gen) noexcept
622 : base(0, 0, 0)
623 , reg(reg)
624 , gen(gen)
625 , loaded()
626 , to_generate()
627 , to_free()
628 , load_dist(config.load_dist)
629 , unload_dist(config.unload_dist) {
630
631 }
632
633 namespace {
634
635 struct ChunkLess {
636
637         explicit ChunkLess(const Chunk::Pos &base) noexcept
638         : base(base) { }
639
640         bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const noexcept {
641                 Chunk::Pos da(base - a);
642                 Chunk::Pos db(base - b);
643                 return
644                         da.x * da.x + da.y * da.y + da.z * da.z <
645                         db.x * db.x + db.y * db.y + db.z * db.z;
646         }
647
648         Chunk::Pos base;
649
650 };
651
652 }
653
654 void ChunkLoader::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
655         for (int z = from.z; z < to.z; ++z) {
656                 for (int y = from.y; y < to.y; ++y) {
657                         for (int x = from.x; x < to.x; ++x) {
658                                 Chunk::Pos pos(x, y, z);
659                                 if (Known(pos)) {
660                                         continue;
661                                 } else if (pos == base) {
662                                         Generate(pos);
663
664                                 //      light testing
665                                 //      for (int i = 0; i < 16; ++i) {
666                                 //              for (int j = 0; j < 16; ++j) {
667                                 //                      loaded.back().SetBlock(Chunk::Pos{  i, j,  0 }, Block(1));
668                                 //                      loaded.back().SetBlock(Chunk::Pos{  i, j, 15 }, Block(1));
669                                 //                      loaded.back().SetBlock(Chunk::Pos{  0, j,  i }, Block(1));
670                                 //                      loaded.back().SetBlock(Chunk::Pos{ 15, j,  i }, Block(1));
671                                 //              }
672                                 //      }
673                                 //      loaded.back().SetBlock(Chunk::Pos{  1,  0,  1 }, Block(13));
674                                 //      loaded.back().SetBlock(Chunk::Pos{ 14,  0,  1 }, Block(13));
675                                 //      loaded.back().SetBlock(Chunk::Pos{  1,  0, 14 }, Block(13));
676                                 //      loaded.back().SetBlock(Chunk::Pos{ 14,  0, 14 }, Block(13));
677                                 //      loaded.back().SetBlock(Chunk::Pos{  1, 15,  1 }, Block(13));
678                                 //      loaded.back().SetBlock(Chunk::Pos{ 14, 15,  1 }, Block(13));
679                                 //      loaded.back().SetBlock(Chunk::Pos{  1, 15, 14 }, Block(13));
680                                 //      loaded.back().SetBlock(Chunk::Pos{ 14, 15, 14 }, Block(13));
681                                 //      loaded.back().SetBlock(Chunk::Pos{  7,  7,  0 }, Block(13));
682                                 //      loaded.back().SetBlock(Chunk::Pos{  8,  7,  0 }, Block(13));
683                                 //      loaded.back().SetBlock(Chunk::Pos{  7,  8,  0 }, Block(13));
684                                 //      loaded.back().SetBlock(Chunk::Pos{  8,  8,  0 }, Block(13));
685                                 //      loaded.back().SetBlock(Chunk::Pos{  7,  7, 15 }, Block(13));
686                                 //      loaded.back().SetBlock(Chunk::Pos{  8,  7, 15 }, Block(13));
687                                 //      loaded.back().SetBlock(Chunk::Pos{  7,  8, 15 }, Block(13));
688                                 //      loaded.back().SetBlock(Chunk::Pos{  8,  8, 15 }, Block(13));
689                                 //      loaded.back().SetBlock(Chunk::Pos{  0,  7,  7 }, Block(13));
690                                 //      loaded.back().SetBlock(Chunk::Pos{  0,  7,  8 }, Block(13));
691                                 //      loaded.back().SetBlock(Chunk::Pos{  0,  8,  7 }, Block(13));
692                                 //      loaded.back().SetBlock(Chunk::Pos{  0,  8,  8 }, Block(13));
693                                 //      loaded.back().SetBlock(Chunk::Pos{ 15,  7,  7 }, Block(13));
694                                 //      loaded.back().SetBlock(Chunk::Pos{ 15,  7,  8 }, Block(13));
695                                 //      loaded.back().SetBlock(Chunk::Pos{ 15,  8,  7 }, Block(13));
696                                 //      loaded.back().SetBlock(Chunk::Pos{ 15,  8,  8 }, Block(13));
697                                 //      loaded.back().Invalidate();
698                                 //      loaded.back().CheckUpdate();
699
700                                 //      orientation testing
701                                 //      for (int i = 0; i < Block::FACE_COUNT; ++i) {
702                                 //              for (int j = 0; j < Block::TURN_COUNT; ++j) {
703                                 //                      loaded.back().BlockAt(512 * j + 2 * i) = Block(3 * (j + 1), Block::Face(i), Block::Turn(j));
704                                 //              }
705                                 //      }
706                                 //      loaded.back().Invalidate();
707                                 //      loaded.back().CheckUpdate();
708                                 } else {
709                                         to_generate.emplace_back(pos);
710                                 }
711                         }
712                 }
713         }
714         to_generate.sort(ChunkLess(base));
715 }
716
717 Chunk &ChunkLoader::Generate(const Chunk::Pos &pos) {
718         loaded.emplace_back(reg);
719         Chunk &chunk = loaded.back();
720         chunk.Position(pos);
721         gen(chunk);
722         Insert(chunk);
723         return chunk;
724 }
725
726 void ChunkLoader::Insert(Chunk &chunk) noexcept {
727         for (Chunk &other : loaded) {
728                 chunk.SetNeighbor(other);
729         }
730 }
731
732 void ChunkLoader::Remove(Chunk &chunk) noexcept {
733         chunk.Unlink();
734 }
735
736 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) noexcept {
737         for (Chunk &chunk : loaded) {
738                 if (chunk.Position() == pos) {
739                         return &chunk;
740                 }
741         }
742         return nullptr;
743 }
744
745 bool ChunkLoader::Queued(const Chunk::Pos &pos) noexcept {
746         for (const Chunk::Pos &chunk : to_generate) {
747                 if (chunk == pos) {
748                         return true;
749                 }
750         }
751         return nullptr;
752 }
753
754 bool ChunkLoader::Known(const Chunk::Pos &pos) noexcept {
755         if (Loaded(pos)) return true;
756         return Queued(pos);
757 }
758
759 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
760         Chunk *chunk = Loaded(pos);
761         if (chunk) {
762                 return *chunk;
763         }
764
765         for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
766                 if (*iter == pos) {
767                         to_generate.erase(iter);
768                         break;
769                 }
770         }
771
772         return Generate(pos);
773 }
774
775 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
776         if (new_base == base) {
777                 return;
778         }
779         base = new_base;
780
781         // unload far away chunks
782         for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
783                 if (std::abs(base.x - iter->Position().x) > unload_dist
784                                 || std::abs(base.y - iter->Position().y) > unload_dist
785                                 || std::abs(base.z - iter->Position().z) > unload_dist) {
786                         auto saved = iter;
787                         Remove(*saved);
788                         ++iter;
789                         to_free.splice(to_free.end(), loaded, saved);
790                 } else {
791                         ++iter;
792                 }
793         }
794         // abort far away queued chunks
795         for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
796                 if (std::abs(base.x - iter->x) > unload_dist
797                                 || std::abs(base.y - iter->y) > unload_dist
798                                 || std::abs(base.z - iter->z) > unload_dist) {
799                         iter = to_generate.erase(iter);
800                 } else {
801                         ++iter;
802                 }
803         }
804         // add missing new chunks
805         GenerateSurrounding(base);
806 }
807
808 void ChunkLoader::GenerateSurrounding(const Chunk::Pos &pos) {
809         const Chunk::Pos offset(load_dist, load_dist, load_dist);
810         Generate(pos - offset, pos + offset);
811 }
812
813 void ChunkLoader::Update() {
814         if (to_generate.empty()) {
815                 return;
816         }
817
818         Chunk::Pos pos(to_generate.front());
819         to_generate.pop_front();
820
821         for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
822                 if (iter->Position() == pos) {
823                         iter->Relink();
824                         loaded.splice(loaded.end(), to_free, iter);
825                         return;
826                 }
827         }
828
829         if (to_free.empty()) {
830                 loaded.emplace_back(reg);
831         } else {
832                 to_free.front().ClearNeighbors();
833                 loaded.splice(loaded.end(), to_free, to_free.begin());
834         }
835         Chunk &chunk = loaded.back();
836         chunk.Position(pos);
837         gen(chunk);
838         Insert(chunk);
839 }
840
841 }