]> git.localhorst.tv Git - blank.git/blob - src/world/chunk.cpp
2216617ec52aac4dc59b317329d2103da84d872a
[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 bool Chunk::Intersection(
485         const AABB &box,
486         const glm::mat4 &Mbox,
487         const glm::mat4 &Mchunk
488 ) const noexcept {
489         if (!blank::Intersection(box, Mbox, Bounds(), Mchunk)) {
490                 return false;
491         }
492         for (int idx = 0, z = 0; z < depth; ++z) {
493                 for (int y = 0; y < height; ++y) {
494                         for (int x = 0; x < width; ++x, ++idx) {
495                                 const BlockType &type = Type(idx);
496                                 if (!type.visible) {
497                                         continue;
498                                 }
499                                 if (type.shape->Intersects(Mchunk * ToTransform(Pos(x, y, z), idx), box, Mbox)) {
500                                         return true;
501                                 }
502                         }
503                 }
504         }
505         return false;
506 }
507
508
509 namespace {
510
511 BlockModel::Buffer buf;
512
513 }
514
515 void Chunk::CheckUpdate() noexcept {
516         if (dirty) {
517                 Update();
518         }
519 }
520
521 void Chunk::Update() noexcept {
522         int vtx_count = 0, idx_count = 0;
523         for (const auto &block : blocks) {
524                 const Shape *shape = Type(block).shape;
525                 vtx_count += shape->VertexCount();
526                 idx_count += shape->VertexIndexCount();
527         }
528         buf.Clear();
529         buf.Reserve(vtx_count, idx_count);
530
531         int idx = 0;
532         BlockModel::Index vtx_counter = 0;
533         for (size_t z = 0; z < depth; ++z) {
534                 for (size_t y = 0; y < height; ++y) {
535                         for (size_t x = 0; x < width; ++x, ++idx) {
536                                 const BlockType &type = Type(BlockAt(idx));
537                                 const Pos pos(x, y, z);
538
539                                 if (!type.visible || Obstructed(pos).All()) continue;
540
541                                 type.FillBlockModel(buf, ToTransform(pos, idx), vtx_counter);
542                                 size_t vtx_begin = vtx_counter;
543                                 vtx_counter += type.shape->VertexCount();
544
545                                 for (size_t vtx = vtx_begin; vtx < vtx_counter; ++vtx) {
546                                         buf.lights.emplace_back(GetVertexLight(
547                                                 pos,
548                                                 buf.vertices[vtx],
549                                                 type.shape->VertexNormal(vtx - vtx_begin, BlockAt(idx).Transform())
550                                         ));
551                                 }
552                         }
553                 }
554         }
555
556         model.Update(buf);
557         dirty = false;
558 }
559
560 Block::FaceSet Chunk::Obstructed(const Pos &pos) const noexcept {
561         Block::FaceSet result;
562
563         for (int f = 0; f < Block::FACE_COUNT; ++f) {
564                 Block::Face face = Block::Face(f);
565                 BlockLookup next(const_cast<Chunk *>(this), pos, face);
566                 if (next && next.GetType().FaceFilled(next.GetBlock(), Block::Opposite(face))) {
567                         result.Set(face);
568                 }
569         }
570
571         return result;
572 }
573
574 glm::mat4 Chunk::ToTransform(const Pos &pos, int idx) const noexcept {
575         return glm::translate(ToCoords(pos)) * BlockAt(idx).Transform();
576 }
577
578
579 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p) noexcept
580 : chunk(c), pos(p) {
581         while (pos.x >= Chunk::width) {
582                 if (chunk->HasNeighbor(Block::FACE_RIGHT)) {
583                         chunk = &chunk->GetNeighbor(Block::FACE_RIGHT);
584                         pos.x -= Chunk::width;
585                 } else {
586                         chunk = nullptr;
587                         return;
588                 }
589         }
590         while (pos.x < 0) {
591                 if (chunk->HasNeighbor(Block::FACE_LEFT)) {
592                         chunk = &chunk->GetNeighbor(Block::FACE_LEFT);
593                         pos.x += Chunk::width;
594                 } else {
595                         chunk = nullptr;
596                         return;
597                 }
598         }
599         while (pos.y >= Chunk::height) {
600                 if (chunk->HasNeighbor(Block::FACE_UP)) {
601                         chunk = &chunk->GetNeighbor(Block::FACE_UP);
602                         pos.y -= Chunk::height;
603                 } else {
604                         chunk = nullptr;
605                         return;
606                 }
607         }
608         while (pos.y < 0) {
609                 if (chunk->HasNeighbor(Block::FACE_DOWN)) {
610                         chunk = &chunk->GetNeighbor(Block::FACE_DOWN);
611                         pos.y += Chunk::height;
612                 } else {
613                         chunk = nullptr;
614                         return;
615                 }
616         }
617         while (pos.z >= Chunk::depth) {
618                 if (chunk->HasNeighbor(Block::FACE_FRONT)) {
619                         chunk = &chunk->GetNeighbor(Block::FACE_FRONT);
620                         pos.z -= Chunk::depth;
621                 } else {
622                         chunk = nullptr;
623                         return;
624                 }
625         }
626         while (pos.z < 0) {
627                 if (chunk->HasNeighbor(Block::FACE_BACK)) {
628                         chunk = &chunk->GetNeighbor(Block::FACE_BACK);
629                         pos.z += Chunk::depth;
630                 } else {
631                         chunk = nullptr;
632                         return;
633                 }
634         }
635 }
636
637 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face face) noexcept
638 : chunk(c), pos(p) {
639         pos += Block::FaceNormal(face);
640         if (!Chunk::InBounds(pos)) {
641                 pos -= Block::FaceNormal(face) * Chunk::Extent();
642                 chunk = &chunk->GetNeighbor(face);
643         }
644 }
645
646
647 ChunkLoader::ChunkLoader(const Config &config, const BlockTypeRegistry &reg, const Generator &gen) noexcept
648 : base(0, 0, 0)
649 , reg(reg)
650 , gen(gen)
651 , loaded()
652 , to_generate()
653 , to_free()
654 , gen_timer(config.gen_limit)
655 , load_dist(config.load_dist)
656 , unload_dist(config.unload_dist) {
657         gen_timer.Start();
658 }
659
660 namespace {
661
662 struct ChunkLess {
663
664         explicit ChunkLess(const Chunk::Pos &base) noexcept
665         : base(base) { }
666
667         bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const noexcept {
668                 Chunk::Pos da(base - a);
669                 Chunk::Pos db(base - b);
670                 return
671                         da.x * da.x + da.y * da.y + da.z * da.z <
672                         db.x * db.x + db.y * db.y + db.z * db.z;
673         }
674
675         Chunk::Pos base;
676
677 };
678
679 }
680
681 void ChunkLoader::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
682         for (int z = from.z; z < to.z; ++z) {
683                 for (int y = from.y; y < to.y; ++y) {
684                         for (int x = from.x; x < to.x; ++x) {
685                                 Chunk::Pos pos(x, y, z);
686                                 if (Known(pos)) {
687                                         continue;
688                                 } else if (pos == base) {
689                                         Generate(pos);
690
691                                 //      light testing
692                                 //      for (int i = 0; i < 16; ++i) {
693                                 //              for (int j = 0; j < 16; ++j) {
694                                 //                      loaded.back().SetBlock(Chunk::Pos{  i, j,  0 }, Block(1));
695                                 //                      loaded.back().SetBlock(Chunk::Pos{  i, j, 15 }, Block(1));
696                                 //                      loaded.back().SetBlock(Chunk::Pos{  0, j,  i }, Block(1));
697                                 //                      loaded.back().SetBlock(Chunk::Pos{ 15, j,  i }, Block(1));
698                                 //              }
699                                 //      }
700                                 //      loaded.back().SetBlock(Chunk::Pos{  1,  0,  1 }, Block(13));
701                                 //      loaded.back().SetBlock(Chunk::Pos{ 14,  0,  1 }, Block(13));
702                                 //      loaded.back().SetBlock(Chunk::Pos{  1,  0, 14 }, Block(13));
703                                 //      loaded.back().SetBlock(Chunk::Pos{ 14,  0, 14 }, Block(13));
704                                 //      loaded.back().SetBlock(Chunk::Pos{  1, 15,  1 }, Block(13));
705                                 //      loaded.back().SetBlock(Chunk::Pos{ 14, 15,  1 }, Block(13));
706                                 //      loaded.back().SetBlock(Chunk::Pos{  1, 15, 14 }, Block(13));
707                                 //      loaded.back().SetBlock(Chunk::Pos{ 14, 15, 14 }, Block(13));
708                                 //      loaded.back().SetBlock(Chunk::Pos{  7,  7,  0 }, Block(13));
709                                 //      loaded.back().SetBlock(Chunk::Pos{  8,  7,  0 }, Block(13));
710                                 //      loaded.back().SetBlock(Chunk::Pos{  7,  8,  0 }, Block(13));
711                                 //      loaded.back().SetBlock(Chunk::Pos{  8,  8,  0 }, Block(13));
712                                 //      loaded.back().SetBlock(Chunk::Pos{  7,  7, 15 }, Block(13));
713                                 //      loaded.back().SetBlock(Chunk::Pos{  8,  7, 15 }, Block(13));
714                                 //      loaded.back().SetBlock(Chunk::Pos{  7,  8, 15 }, Block(13));
715                                 //      loaded.back().SetBlock(Chunk::Pos{  8,  8, 15 }, Block(13));
716                                 //      loaded.back().SetBlock(Chunk::Pos{  0,  7,  7 }, Block(13));
717                                 //      loaded.back().SetBlock(Chunk::Pos{  0,  7,  8 }, Block(13));
718                                 //      loaded.back().SetBlock(Chunk::Pos{  0,  8,  7 }, Block(13));
719                                 //      loaded.back().SetBlock(Chunk::Pos{  0,  8,  8 }, Block(13));
720                                 //      loaded.back().SetBlock(Chunk::Pos{ 15,  7,  7 }, Block(13));
721                                 //      loaded.back().SetBlock(Chunk::Pos{ 15,  7,  8 }, Block(13));
722                                 //      loaded.back().SetBlock(Chunk::Pos{ 15,  8,  7 }, Block(13));
723                                 //      loaded.back().SetBlock(Chunk::Pos{ 15,  8,  8 }, Block(13));
724                                 //      loaded.back().Invalidate();
725                                 //      loaded.back().CheckUpdate();
726
727                                 //      orientation testing
728                                 //      for (int i = 0; i < Block::FACE_COUNT; ++i) {
729                                 //              for (int j = 0; j < Block::TURN_COUNT; ++j) {
730                                 //                      loaded.back().BlockAt(512 * j + 2 * i) = Block(3 * (j + 1), Block::Face(i), Block::Turn(j));
731                                 //              }
732                                 //      }
733                                 //      loaded.back().Invalidate();
734                                 //      loaded.back().CheckUpdate();
735                                 } else {
736                                         to_generate.emplace_back(pos);
737                                 }
738                         }
739                 }
740         }
741         to_generate.sort(ChunkLess(base));
742 }
743
744 Chunk &ChunkLoader::Generate(const Chunk::Pos &pos) {
745         loaded.emplace_back(reg);
746         Chunk &chunk = loaded.back();
747         chunk.Position(pos);
748         gen(chunk);
749         Insert(chunk);
750         return chunk;
751 }
752
753 void ChunkLoader::Insert(Chunk &chunk) noexcept {
754         for (Chunk &other : loaded) {
755                 chunk.SetNeighbor(other);
756         }
757 }
758
759 void ChunkLoader::Remove(Chunk &chunk) noexcept {
760         chunk.Unlink();
761 }
762
763 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) noexcept {
764         for (Chunk &chunk : loaded) {
765                 if (chunk.Position() == pos) {
766                         return &chunk;
767                 }
768         }
769         return nullptr;
770 }
771
772 bool ChunkLoader::Queued(const Chunk::Pos &pos) noexcept {
773         for (const Chunk::Pos &chunk : to_generate) {
774                 if (chunk == pos) {
775                         return true;
776                 }
777         }
778         return false;
779 }
780
781 bool ChunkLoader::Known(const Chunk::Pos &pos) noexcept {
782         if (Loaded(pos)) return true;
783         return Queued(pos);
784 }
785
786 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
787         Chunk *chunk = Loaded(pos);
788         if (chunk) {
789                 return *chunk;
790         }
791
792         for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
793                 if (*iter == pos) {
794                         to_generate.erase(iter);
795                         break;
796                 }
797         }
798
799         return Generate(pos);
800 }
801
802 bool ChunkLoader::OutOfRange(const Chunk::Pos &pos) const noexcept {
803         return std::abs(base.x - pos.x) > unload_dist
804                         || std::abs(base.y - pos.y) > unload_dist
805                         || std::abs(base.z - pos.z) > unload_dist;
806 }
807
808 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
809         if (new_base == base) {
810                 return;
811         }
812         base = new_base;
813
814         // unload far away chunks
815         for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
816                 if (OutOfRange(*iter)) {
817                         auto saved = iter;
818                         Remove(*saved);
819                         ++iter;
820                         to_free.splice(to_free.end(), loaded, saved);
821                 } else {
822                         ++iter;
823                 }
824         }
825         // abort far away queued chunks
826         for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
827                 if (OutOfRange(*iter)) {
828                         iter = to_generate.erase(iter);
829                 } else {
830                         ++iter;
831                 }
832         }
833         // add missing new chunks
834         GenerateSurrounding(base);
835 }
836
837 void ChunkLoader::GenerateSurrounding(const Chunk::Pos &pos) {
838         const Chunk::Pos offset(load_dist, load_dist, load_dist);
839         Generate(pos - offset, pos + offset);
840 }
841
842 void ChunkLoader::Update(int dt) {
843         gen_timer.Update(dt);
844         if (!gen_timer.Hit() || to_generate.empty()) {
845                 return;
846         }
847
848         Chunk::Pos pos(to_generate.front());
849         to_generate.pop_front();
850
851         for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
852                 if (iter->Position() == pos) {
853                         iter->Relink();
854                         loaded.splice(loaded.end(), to_free, iter);
855                 }
856         }
857
858         if (to_free.empty()) {
859                 loaded.emplace_back(reg);
860         } else {
861                 to_free.front().ClearNeighbors();
862                 loaded.splice(loaded.end(), to_free, to_free.begin());
863         }
864         Chunk &chunk = loaded.back();
865         chunk.Position(pos);
866         gen(chunk);
867         Insert(chunk);
868 }
869
870 }