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