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