]> git.localhorst.tv Git - blank.git/blob - src/world/chunk.cpp
fixed light propagation after obstacle removal
[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                 Pos pos(ToPos(index));
173                 for (int face = 0; face < Block::FACE_COUNT; ++face) {
174                         BlockLookup next_block(this, pos, Block::Face(face));
175                         if (next_block) {
176                                 level = std::max(level, next_block.GetLight());
177                         }
178                 }
179                 if (level > 1) {
180                         SetLight(index, level - 1);
181                         light_queue.emplace(this, pos);
182                         work_light();
183                 }
184         }
185 }
186
187 void Chunk::SetNeighbor(Chunk &other) noexcept {
188         if (other.position == position + Pos(-1, 0, 0)) {
189                 if (neighbor[Block::FACE_LEFT] != &other) {
190                         neighbor[Block::FACE_LEFT] = &other;
191                         other.neighbor[Block::FACE_RIGHT] = this;
192                         for (int z = 0; z < depth; ++z) {
193                                 for (int y = 0; y < height; ++y) {
194                                         Pos my_pos(0, y, z);
195                                         Pos other_pos(width - 1, y, z);
196                                         if (GetLight(my_pos) > 0) {
197                                                 light_queue.emplace(this, my_pos);
198                                         }
199                                         if (other.GetLight(other_pos) > 0) {
200                                                 light_queue.emplace(&other, other_pos);
201                                         }
202                                 }
203                         }
204                         work_light();
205                 }
206         } else if (other.position == position + Pos(1, 0, 0)) {
207                 if (neighbor[Block::FACE_RIGHT] != &other) {
208                         neighbor[Block::FACE_RIGHT] = &other;
209                         other.neighbor[Block::FACE_LEFT] = this;
210                         for (int z = 0; z < depth; ++z) {
211                                 for (int y = 0; y < height; ++y) {
212                                         Pos my_pos(width - 1, y, z);
213                                         Pos other_pos(0, y, z);
214                                         if (GetLight(my_pos) > 0) {
215                                                 light_queue.emplace(this, my_pos);
216                                         }
217                                         if (other.GetLight(other_pos) > 0) {
218                                                 light_queue.emplace(&other, other_pos);
219                                         }
220                                 }
221                         }
222                         work_light();
223                 }
224         } else if (other.position == position + Pos(0, -1, 0)) {
225                 if (neighbor[Block::FACE_DOWN] != &other) {
226                         neighbor[Block::FACE_DOWN] = &other;
227                         other.neighbor[Block::FACE_UP] = this;
228                         for (int z = 0; z < depth; ++z) {
229                                 for (int x = 0; x < width; ++x) {
230                                         Pos my_pos(x, 0, z);
231                                         Pos other_pos(x, height - 1, z);
232                                         if (GetLight(my_pos) > 0) {
233                                                 light_queue.emplace(this, my_pos);
234                                         }
235                                         if (other.GetLight(other_pos) > 0) {
236                                                 light_queue.emplace(&other, other_pos);
237                                         }
238                                 }
239                         }
240                         work_light();
241                 }
242         } else if (other.position == position + Pos(0, 1, 0)) {
243                 if (neighbor[Block::FACE_UP] != &other) {
244                         neighbor[Block::FACE_UP] = &other;
245                         other.neighbor[Block::FACE_DOWN] = this;
246                         for (int z = 0; z < depth; ++z) {
247                                 for (int x = 0; x < width; ++x) {
248                                         Pos my_pos(x, height - 1, z);
249                                         Pos other_pos(x, 0, z);
250                                         if (GetLight(my_pos) > 0) {
251                                                 light_queue.emplace(this, my_pos);
252                                         }
253                                         if (other.GetLight(other_pos) > 0) {
254                                                 light_queue.emplace(&other, other_pos);
255                                         }
256                                 }
257                         }
258                         work_light();
259                 }
260         } else if (other.position == position + Pos(0, 0, -1)) {
261                 if (neighbor[Block::FACE_BACK] != &other) {
262                         neighbor[Block::FACE_BACK] = &other;
263                         other.neighbor[Block::FACE_FRONT] = this;
264                         for (int y = 0; y < height; ++y) {
265                                 for (int x = 0; x < width; ++x) {
266                                         Pos my_pos(x, y, 0);
267                                         Pos other_pos(x, y, depth - 1);
268                                         if (GetLight(my_pos) > 0) {
269                                                 light_queue.emplace(this, my_pos);
270                                         }
271                                         if (other.GetLight(other_pos) > 0) {
272                                                 light_queue.emplace(&other, other_pos);
273                                         }
274                                 }
275                         }
276                         work_light();
277                 }
278         } else if (other.position == position + Pos(0, 0, 1)) {
279                 if (neighbor[Block::FACE_FRONT] != &other) {
280                         neighbor[Block::FACE_FRONT] = &other;
281                         other.neighbor[Block::FACE_BACK] = this;
282                         for (int y = 0; y < height; ++y) {
283                                 for (int x = 0; x < width; ++x) {
284                                         Pos my_pos(x, y, depth - 1);
285                                         Pos other_pos(x, y, 0);
286                                         if (GetLight(my_pos) > 0) {
287                                                 light_queue.emplace(this, my_pos);
288                                         }
289                                         if (other.GetLight(other_pos) > 0) {
290                                                 light_queue.emplace(&other, other_pos);
291                                         }
292                                 }
293                         }
294                         work_light();
295                 }
296         }
297 }
298
299 void Chunk::ClearNeighbors() noexcept {
300         for (int i = 0; i < Block::FACE_COUNT; ++i) {
301                 neighbor[i] = nullptr;
302         }
303 }
304
305 void Chunk::Unlink() noexcept {
306         for (int face = 0; face < Block::FACE_COUNT; ++face) {
307                 if (neighbor[face]) {
308                         neighbor[face]->neighbor[Block::Opposite(Block::Face(face))] = nullptr;
309                 }
310         }
311 }
312
313 void Chunk::Relink() noexcept {
314         for (int face = 0; face < Block::FACE_COUNT; ++face) {
315                 if (neighbor[face]) {
316                         neighbor[face]->neighbor[Block::Opposite(Block::Face(face))] = this;
317                 }
318         }
319 }
320
321
322 void Chunk::SetLight(int index, int level) noexcept {
323         if (light[index] != level) {
324                 light[index] = level;
325                 Invalidate();
326         }
327 }
328
329 int Chunk::GetLight(int index) const noexcept {
330         return light[index];
331 }
332
333 float Chunk::GetVertexLight(const Pos &pos, const BlockModel::Position &vtx, const Model::Normal &norm) const noexcept {
334         int index = ToIndex(pos);
335         float light = GetLight(index);
336
337         Block::Face direct_face(Block::NormalFace(norm));
338         // tis okay
339         BlockLookup direct(const_cast<Chunk *>(this), pos, Block::NormalFace(norm));
340         if (direct) {
341                 float direct_light = direct.GetLight();
342                 if (direct_light > light) {
343                         light = direct_light;
344                 }
345         } else {
346                 return light;
347         }
348
349         if (Type(BlockAt(index)).luminosity > 0 || direct.GetType().block_light) {
350                 return light;
351         }
352
353         Block::Face edge[2];
354         switch (Block::Axis(direct_face)) {
355                 case 0: // X
356                         edge[0] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
357                         edge[1] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
358                         break;
359                 case 1: // Y
360                         edge[0] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
361                         edge[1] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
362                         break;
363                 case 2: // Z
364                         edge[0] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
365                         edge[1] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
366                         break;
367         }
368
369         int num = 1;
370         int occlusion = 0;
371
372         BlockLookup next[2] = {
373                 direct.Next(edge[0]),
374                 direct.Next(edge[1]),
375         };
376
377         if (next[0]) {
378                 if (next[0].GetType().block_light) {
379                         ++occlusion;
380                 } else {
381                         light += next[0].GetLight();
382                         ++num;
383                 }
384         }
385         if (next[1]) {
386                 if (next[1].GetType().block_light) {
387                         ++occlusion;
388                 } else {
389                         light += next[1].GetLight();
390                         ++num;
391                 }
392         }
393         if (occlusion < 2) {
394                 if (next[0]) {
395                         BlockLookup corner = next[0].Next(edge[1]);
396                         if (corner) {
397                                 if (corner.GetType().block_light) {
398                                         ++occlusion;
399                                 } else {
400                                         light += corner.GetLight();
401                                         ++num;
402                                 }
403                         }
404                 } else if (next[1]) {
405                         BlockLookup corner = next[1].Next(edge[0]);
406                         if (corner) {
407                                 if (corner.GetType().block_light) {
408                                         ++occlusion;
409                                 } else {
410                                         light += corner.GetLight();
411                                         ++num;
412                                 }
413                         }
414                 }
415         } else {
416                 ++occlusion;
417         }
418
419         return (light / num) - (occlusion * 0.8f);
420 }
421
422
423 bool Chunk::IsSurface(const Pos &pos) const noexcept {
424         const Block &block = BlockAt(pos);
425         if (!Type(block).visible) {
426                 return false;
427         }
428         for (int face = 0; face < Block::FACE_COUNT; ++face) {
429                 BlockLookup next = BlockLookup(const_cast<Chunk *>(this), pos, Block::Face(face));
430                 if (!next || !next.GetType().visible) {
431                         return true;
432                 }
433         }
434         return false;
435 }
436
437
438 void Chunk::Draw() noexcept {
439         if (dirty) {
440                 Update();
441         }
442         model.Draw();
443 }
444
445
446 bool Chunk::Intersection(
447         const Ray &ray,
448         const glm::mat4 &M,
449         int &blkid,
450         float &dist,
451         glm::vec3 &normal
452 ) const noexcept {
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 false;
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 }