]> git.localhorst.tv Git - blank.git/blob - src/world/chunk.cpp
75f9c4f424c28d8babc77ef9322921a7db96a899
[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 namespace {
190
191 // propagate light from a's edge to b
192 void edge_light(
193         Chunk &a, const Chunk::Pos &a_pos,
194         Chunk &b, const Chunk::Pos &b_pos
195 ) noexcept {
196         if (a.GetLight(a_pos) > 1) {
197                 const BlockType &b_type = b.Type(Chunk::ToIndex(b_pos));
198                 if (!b_type.block_light) {
199                         light_queue.emplace(&a, a_pos);
200                 }
201                 if (b_type.visible) {
202                         b.Invalidate();
203                 }
204         }
205 }
206
207 }
208
209 void Chunk::SetNeighbor(Chunk &other) noexcept {
210         if (other.position == position + Pos(-1, 0, 0)) {
211                 if (neighbor[Block::FACE_LEFT] != &other) {
212                         neighbor[Block::FACE_LEFT] = &other;
213                         other.neighbor[Block::FACE_RIGHT] = this;
214                         for (int z = 0; z < depth; ++z) {
215                                 for (int y = 0; y < height; ++y) {
216                                         Pos my_pos(0, y, z);
217                                         Pos other_pos(width - 1, y, z);
218                                         edge_light(*this, my_pos, other, other_pos);
219                                         edge_light(other, other_pos, *this, my_pos);
220                                 }
221                         }
222                         work_light();
223                 }
224         } else if (other.position == position + Pos(1, 0, 0)) {
225                 if (neighbor[Block::FACE_RIGHT] != &other) {
226                         neighbor[Block::FACE_RIGHT] = &other;
227                         other.neighbor[Block::FACE_LEFT] = this;
228                         for (int z = 0; z < depth; ++z) {
229                                 for (int y = 0; y < height; ++y) {
230                                         Pos my_pos(width - 1, y, z);
231                                         Pos other_pos(0, y, z);
232                                         edge_light(*this, my_pos, other, other_pos);
233                                         edge_light(other, other_pos, *this, my_pos);
234                                 }
235                         }
236                         work_light();
237                 }
238         } else if (other.position == position + Pos(0, -1, 0)) {
239                 if (neighbor[Block::FACE_DOWN] != &other) {
240                         neighbor[Block::FACE_DOWN] = &other;
241                         other.neighbor[Block::FACE_UP] = this;
242                         for (int z = 0; z < depth; ++z) {
243                                 for (int x = 0; x < width; ++x) {
244                                         Pos my_pos(x, 0, z);
245                                         Pos other_pos(x, height - 1, z);
246                                         edge_light(*this, my_pos, other, other_pos);
247                                         edge_light(other, other_pos, *this, my_pos);
248                                 }
249                         }
250                         work_light();
251                 }
252         } else if (other.position == position + Pos(0, 1, 0)) {
253                 if (neighbor[Block::FACE_UP] != &other) {
254                         neighbor[Block::FACE_UP] = &other;
255                         other.neighbor[Block::FACE_DOWN] = this;
256                         for (int z = 0; z < depth; ++z) {
257                                 for (int x = 0; x < width; ++x) {
258                                         Pos my_pos(x, height - 1, z);
259                                         Pos other_pos(x, 0, z);
260                                         edge_light(*this, my_pos, other, other_pos);
261                                         edge_light(other, other_pos, *this, my_pos);
262                                 }
263                         }
264                         work_light();
265                 }
266         } else if (other.position == position + Pos(0, 0, -1)) {
267                 if (neighbor[Block::FACE_BACK] != &other) {
268                         neighbor[Block::FACE_BACK] = &other;
269                         other.neighbor[Block::FACE_FRONT] = this;
270                         for (int y = 0; y < height; ++y) {
271                                 for (int x = 0; x < width; ++x) {
272                                         Pos my_pos(x, y, 0);
273                                         Pos other_pos(x, y, depth - 1);
274                                         edge_light(*this, my_pos, other, other_pos);
275                                         edge_light(other, other_pos, *this, my_pos);
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                                         edge_light(*this, my_pos, other, other_pos);
289                                         edge_light(other, other_pos, *this, my_pos);
290                                 }
291                         }
292                         work_light();
293                 }
294         }
295 }
296
297 void Chunk::ClearNeighbors() noexcept {
298         for (int i = 0; i < Block::FACE_COUNT; ++i) {
299                 neighbor[i] = nullptr;
300         }
301 }
302
303 void Chunk::Unlink() noexcept {
304         for (int face = 0; face < Block::FACE_COUNT; ++face) {
305                 if (neighbor[face]) {
306                         neighbor[face]->neighbor[Block::Opposite(Block::Face(face))] = nullptr;
307                 }
308         }
309 }
310
311 void Chunk::Relink() noexcept {
312         for (int face = 0; face < Block::FACE_COUNT; ++face) {
313                 if (neighbor[face]) {
314                         neighbor[face]->neighbor[Block::Opposite(Block::Face(face))] = this;
315                 }
316         }
317 }
318
319
320 void Chunk::SetLight(int index, int level) noexcept {
321         if (light[index] != level) {
322                 light[index] = level;
323                 Invalidate();
324         }
325 }
326
327 int Chunk::GetLight(int index) const noexcept {
328         return light[index];
329 }
330
331 float Chunk::GetVertexLight(const Pos &pos, const BlockModel::Position &vtx, const Model::Normal &norm) const noexcept {
332         int index = ToIndex(pos);
333         float light = GetLight(index);
334
335         Block::Face direct_face(Block::NormalFace(norm));
336         // tis okay
337         BlockLookup direct(const_cast<Chunk *>(this), pos, Block::NormalFace(norm));
338         if (direct) {
339                 float direct_light = direct.GetLight();
340                 if (direct_light > light) {
341                         light = direct_light;
342                 }
343         } else {
344                 return light;
345         }
346
347         if (Type(BlockAt(index)).luminosity > 0 || direct.GetType().block_light) {
348                 return light;
349         }
350
351         Block::Face edge[2];
352         switch (Block::Axis(direct_face)) {
353                 case 0: // X
354                         edge[0] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
355                         edge[1] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
356                         break;
357                 case 1: // Y
358                         edge[0] = (vtx.z - pos.z) > 0.5f ? Block::FACE_FRONT : Block::FACE_BACK;
359                         edge[1] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
360                         break;
361                 case 2: // Z
362                         edge[0] = (vtx.x - pos.x) > 0.5f ? Block::FACE_RIGHT : Block::FACE_LEFT;
363                         edge[1] = (vtx.y - pos.y) > 0.5f ? Block::FACE_UP : Block::FACE_DOWN;
364                         break;
365         }
366
367         int num = 1;
368         int occlusion = 0;
369
370         BlockLookup next[2] = {
371                 direct.Next(edge[0]),
372                 direct.Next(edge[1]),
373         };
374
375         if (next[0]) {
376                 if (next[0].GetType().block_light) {
377                         ++occlusion;
378                 } else {
379                         light += next[0].GetLight();
380                         ++num;
381                 }
382         }
383         if (next[1]) {
384                 if (next[1].GetType().block_light) {
385                         ++occlusion;
386                 } else {
387                         light += next[1].GetLight();
388                         ++num;
389                 }
390         }
391         if (occlusion < 2) {
392                 if (next[0]) {
393                         BlockLookup corner = next[0].Next(edge[1]);
394                         if (corner) {
395                                 if (corner.GetType().block_light) {
396                                         ++occlusion;
397                                 } else {
398                                         light += corner.GetLight();
399                                         ++num;
400                                 }
401                         }
402                 } else if (next[1]) {
403                         BlockLookup corner = next[1].Next(edge[0]);
404                         if (corner) {
405                                 if (corner.GetType().block_light) {
406                                         ++occlusion;
407                                 } else {
408                                         light += corner.GetLight();
409                                         ++num;
410                                 }
411                         }
412                 }
413         } else {
414                 ++occlusion;
415         }
416
417         return (light / num) - (occlusion * 0.8f);
418 }
419
420
421 bool Chunk::IsSurface(const Pos &pos) const noexcept {
422         const Block &block = BlockAt(pos);
423         if (!Type(block).visible) {
424                 return false;
425         }
426         for (int face = 0; face < Block::FACE_COUNT; ++face) {
427                 BlockLookup next = BlockLookup(const_cast<Chunk *>(this), pos, Block::Face(face));
428                 if (!next || !next.GetType().visible) {
429                         return true;
430                 }
431         }
432         return false;
433 }
434
435
436 void Chunk::Draw() noexcept {
437         if (dirty) {
438                 Update();
439         }
440         model.Draw();
441 }
442
443
444 bool Chunk::Intersection(
445         const Ray &ray,
446         const glm::mat4 &M,
447         int &blkid,
448         float &dist,
449         glm::vec3 &normal
450 ) const noexcept {
451         int idx = 0;
452         blkid = -1;
453         dist = std::numeric_limits<float>::infinity();
454         for (int z = 0; z < depth; ++z) {
455                 for (int y = 0; y < height; ++y) {
456                         for (int x = 0; x < width; ++x, ++idx) {
457                                 const BlockType &type = Type(idx);
458                                 if (!type.visible) {
459                                         continue;
460                                 }
461                                 float cur_dist;
462                                 glm::vec3 cur_norm;
463                                 if (type.shape->Intersects(ray, M * ToTransform(Pos(x, y, z), idx), cur_dist, cur_norm)) {
464                                         if (cur_dist < dist) {
465                                                 blkid = idx;
466                                                 dist = cur_dist;
467                                                 normal = cur_norm;
468                                         }
469                                 }
470                         }
471                 }
472         }
473
474         if (blkid < 0) {
475                 return false;
476         } else {
477                 normal = glm::vec3(BlockAt(blkid).Transform() * glm::vec4(normal, 0.0f));
478                 return true;
479         }
480 }
481
482 bool Chunk::Intersection(
483         const AABB &box,
484         const glm::mat4 &Mbox,
485         const glm::mat4 &Mchunk,
486         std::vector<WorldCollision> &col
487 ) const noexcept {
488         bool any = false;
489         float penetration;
490         glm::vec3 normal;
491
492         if (!blank::Intersection(box, Mbox, Bounds(), Mchunk, penetration, normal)) {
493                 return false;
494         }
495         for (int idx = 0, z = 0; z < depth; ++z) {
496                 for (int y = 0; y < height; ++y) {
497                         for (int x = 0; x < width; ++x, ++idx) {
498                                 const BlockType &type = Type(idx);
499                                 if (!type.collision) {
500                                         continue;
501                                 }
502                                 if (type.shape->Intersects(Mchunk * ToTransform(Pos(x, y, z), idx), box, Mbox, penetration, normal)) {
503                                         col.emplace_back(this, idx, penetration, normal);
504                                         any = true;
505                                 }
506                         }
507                 }
508         }
509         return any;
510 }
511
512
513 namespace {
514
515 BlockModel::Buffer buf;
516
517 }
518
519 void Chunk::CheckUpdate() noexcept {
520         if (dirty) {
521                 Update();
522         }
523 }
524
525 void Chunk::Update() noexcept {
526         int vtx_count = 0, idx_count = 0;
527         for (const auto &block : blocks) {
528                 const Shape *shape = Type(block).shape;
529                 vtx_count += shape->VertexCount();
530                 idx_count += shape->VertexIndexCount();
531         }
532         buf.Clear();
533         buf.Reserve(vtx_count, idx_count);
534
535         int idx = 0;
536         BlockModel::Index vtx_counter = 0;
537         for (size_t z = 0; z < depth; ++z) {
538                 for (size_t y = 0; y < height; ++y) {
539                         for (size_t x = 0; x < width; ++x, ++idx) {
540                                 const BlockType &type = Type(BlockAt(idx));
541                                 const Pos pos(x, y, z);
542
543                                 if (!type.visible || Obstructed(pos).All()) continue;
544
545                                 type.FillBlockModel(buf, ToTransform(pos, idx), vtx_counter);
546                                 size_t vtx_begin = vtx_counter;
547                                 vtx_counter += type.shape->VertexCount();
548
549                                 for (size_t vtx = vtx_begin; vtx < vtx_counter; ++vtx) {
550                                         buf.lights.emplace_back(GetVertexLight(
551                                                 pos,
552                                                 buf.vertices[vtx],
553                                                 type.shape->VertexNormal(vtx - vtx_begin, BlockAt(idx).Transform())
554                                         ));
555                                 }
556                         }
557                 }
558         }
559
560         model.Update(buf);
561         dirty = false;
562 }
563
564 Block::FaceSet Chunk::Obstructed(const Pos &pos) const noexcept {
565         Block::FaceSet result;
566
567         for (int f = 0; f < Block::FACE_COUNT; ++f) {
568                 Block::Face face = Block::Face(f);
569                 BlockLookup next(const_cast<Chunk *>(this), pos, face);
570                 if (next && next.GetType().FaceFilled(next.GetBlock(), Block::Opposite(face))) {
571                         result.Set(face);
572                 }
573         }
574
575         return result;
576 }
577
578 glm::mat4 Chunk::ToTransform(const Pos &pos, int idx) const noexcept {
579         return glm::translate(ToCoords(pos)) * BlockAt(idx).Transform();
580 }
581
582
583 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p) noexcept
584 : chunk(c), pos(p) {
585         while (pos.x >= Chunk::width) {
586                 if (chunk->HasNeighbor(Block::FACE_RIGHT)) {
587                         chunk = &chunk->GetNeighbor(Block::FACE_RIGHT);
588                         pos.x -= Chunk::width;
589                 } else {
590                         chunk = nullptr;
591                         return;
592                 }
593         }
594         while (pos.x < 0) {
595                 if (chunk->HasNeighbor(Block::FACE_LEFT)) {
596                         chunk = &chunk->GetNeighbor(Block::FACE_LEFT);
597                         pos.x += Chunk::width;
598                 } else {
599                         chunk = nullptr;
600                         return;
601                 }
602         }
603         while (pos.y >= Chunk::height) {
604                 if (chunk->HasNeighbor(Block::FACE_UP)) {
605                         chunk = &chunk->GetNeighbor(Block::FACE_UP);
606                         pos.y -= Chunk::height;
607                 } else {
608                         chunk = nullptr;
609                         return;
610                 }
611         }
612         while (pos.y < 0) {
613                 if (chunk->HasNeighbor(Block::FACE_DOWN)) {
614                         chunk = &chunk->GetNeighbor(Block::FACE_DOWN);
615                         pos.y += Chunk::height;
616                 } else {
617                         chunk = nullptr;
618                         return;
619                 }
620         }
621         while (pos.z >= Chunk::depth) {
622                 if (chunk->HasNeighbor(Block::FACE_FRONT)) {
623                         chunk = &chunk->GetNeighbor(Block::FACE_FRONT);
624                         pos.z -= Chunk::depth;
625                 } else {
626                         chunk = nullptr;
627                         return;
628                 }
629         }
630         while (pos.z < 0) {
631                 if (chunk->HasNeighbor(Block::FACE_BACK)) {
632                         chunk = &chunk->GetNeighbor(Block::FACE_BACK);
633                         pos.z += Chunk::depth;
634                 } else {
635                         chunk = nullptr;
636                         return;
637                 }
638         }
639 }
640
641 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face face) noexcept
642 : chunk(c), pos(p) {
643         pos += Block::FaceNormal(face);
644         if (!Chunk::InBounds(pos)) {
645                 pos -= Block::FaceNormal(face) * Chunk::Extent();
646                 chunk = &chunk->GetNeighbor(face);
647         }
648 }
649
650
651 ChunkLoader::ChunkLoader(const Config &config, const BlockTypeRegistry &reg, const Generator &gen) noexcept
652 : base(0, 0, 0)
653 , reg(reg)
654 , gen(gen)
655 , loaded()
656 , to_generate()
657 , to_free()
658 , gen_timer(config.gen_limit)
659 , load_dist(config.load_dist)
660 , unload_dist(config.unload_dist) {
661         gen_timer.Start();
662 }
663
664 namespace {
665
666 struct ChunkLess {
667
668         explicit ChunkLess(const Chunk::Pos &base) noexcept
669         : base(base) { }
670
671         bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const noexcept {
672                 Chunk::Pos da(base - a);
673                 Chunk::Pos db(base - b);
674                 return
675                         da.x * da.x + da.y * da.y + da.z * da.z <
676                         db.x * db.x + db.y * db.y + db.z * db.z;
677         }
678
679         Chunk::Pos base;
680
681 };
682
683 }
684
685 void ChunkLoader::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
686         for (int z = from.z; z < to.z; ++z) {
687                 for (int y = from.y; y < to.y; ++y) {
688                         for (int x = from.x; x < to.x; ++x) {
689                                 Chunk::Pos pos(x, y, z);
690                                 if (Known(pos)) {
691                                         continue;
692                                 } else if (pos == base) {
693                                         Generate(pos);
694
695                                 //      light testing
696                                 //      for (int i = 0; i < 16; ++i) {
697                                 //              for (int j = 0; j < 16; ++j) {
698                                 //                      loaded.back().SetBlock(Chunk::Pos{  i, j,  0 }, Block(1));
699                                 //                      loaded.back().SetBlock(Chunk::Pos{  i, j, 15 }, Block(1));
700                                 //                      loaded.back().SetBlock(Chunk::Pos{  0, j,  i }, Block(1));
701                                 //                      loaded.back().SetBlock(Chunk::Pos{ 15, j,  i }, Block(1));
702                                 //              }
703                                 //      }
704                                 //      loaded.back().SetBlock(Chunk::Pos{  1,  0,  1 }, Block(13));
705                                 //      loaded.back().SetBlock(Chunk::Pos{ 14,  0,  1 }, Block(13));
706                                 //      loaded.back().SetBlock(Chunk::Pos{  1,  0, 14 }, Block(13));
707                                 //      loaded.back().SetBlock(Chunk::Pos{ 14,  0, 14 }, Block(13));
708                                 //      loaded.back().SetBlock(Chunk::Pos{  1, 15,  1 }, Block(13));
709                                 //      loaded.back().SetBlock(Chunk::Pos{ 14, 15,  1 }, Block(13));
710                                 //      loaded.back().SetBlock(Chunk::Pos{  1, 15, 14 }, Block(13));
711                                 //      loaded.back().SetBlock(Chunk::Pos{ 14, 15, 14 }, Block(13));
712                                 //      loaded.back().SetBlock(Chunk::Pos{  7,  7,  0 }, Block(13));
713                                 //      loaded.back().SetBlock(Chunk::Pos{  8,  7,  0 }, Block(13));
714                                 //      loaded.back().SetBlock(Chunk::Pos{  7,  8,  0 }, Block(13));
715                                 //      loaded.back().SetBlock(Chunk::Pos{  8,  8,  0 }, Block(13));
716                                 //      loaded.back().SetBlock(Chunk::Pos{  7,  7, 15 }, Block(13));
717                                 //      loaded.back().SetBlock(Chunk::Pos{  8,  7, 15 }, Block(13));
718                                 //      loaded.back().SetBlock(Chunk::Pos{  7,  8, 15 }, Block(13));
719                                 //      loaded.back().SetBlock(Chunk::Pos{  8,  8, 15 }, Block(13));
720                                 //      loaded.back().SetBlock(Chunk::Pos{  0,  7,  7 }, Block(13));
721                                 //      loaded.back().SetBlock(Chunk::Pos{  0,  7,  8 }, Block(13));
722                                 //      loaded.back().SetBlock(Chunk::Pos{  0,  8,  7 }, Block(13));
723                                 //      loaded.back().SetBlock(Chunk::Pos{  0,  8,  8 }, Block(13));
724                                 //      loaded.back().SetBlock(Chunk::Pos{ 15,  7,  7 }, Block(13));
725                                 //      loaded.back().SetBlock(Chunk::Pos{ 15,  7,  8 }, Block(13));
726                                 //      loaded.back().SetBlock(Chunk::Pos{ 15,  8,  7 }, Block(13));
727                                 //      loaded.back().SetBlock(Chunk::Pos{ 15,  8,  8 }, Block(13));
728                                 //      loaded.back().Invalidate();
729                                 //      loaded.back().CheckUpdate();
730
731                                 //      orientation testing
732                                 //      for (int i = 0; i < Block::FACE_COUNT; ++i) {
733                                 //              for (int j = 0; j < Block::TURN_COUNT; ++j) {
734                                 //                      loaded.back().BlockAt(512 * j + 2 * i) = Block(3 * (j + 1), Block::Face(i), Block::Turn(j));
735                                 //              }
736                                 //      }
737                                 //      loaded.back().Invalidate();
738                                 //      loaded.back().CheckUpdate();
739                                 } else {
740                                         to_generate.emplace_back(pos);
741                                 }
742                         }
743                 }
744         }
745         to_generate.sort(ChunkLess(base));
746 }
747
748 Chunk &ChunkLoader::Generate(const Chunk::Pos &pos) {
749         loaded.emplace_back(reg);
750         Chunk &chunk = loaded.back();
751         chunk.Position(pos);
752         gen(chunk);
753         Insert(chunk);
754         return chunk;
755 }
756
757 void ChunkLoader::Insert(Chunk &chunk) noexcept {
758         for (Chunk &other : loaded) {
759                 chunk.SetNeighbor(other);
760         }
761 }
762
763 void ChunkLoader::Remove(Chunk &chunk) noexcept {
764         chunk.Unlink();
765 }
766
767 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) noexcept {
768         for (Chunk &chunk : loaded) {
769                 if (chunk.Position() == pos) {
770                         return &chunk;
771                 }
772         }
773         return nullptr;
774 }
775
776 bool ChunkLoader::Queued(const Chunk::Pos &pos) noexcept {
777         for (const Chunk::Pos &chunk : to_generate) {
778                 if (chunk == pos) {
779                         return true;
780                 }
781         }
782         return false;
783 }
784
785 bool ChunkLoader::Known(const Chunk::Pos &pos) noexcept {
786         if (Loaded(pos)) return true;
787         return Queued(pos);
788 }
789
790 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
791         Chunk *chunk = Loaded(pos);
792         if (chunk) {
793                 return *chunk;
794         }
795
796         for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
797                 if (*iter == pos) {
798                         to_generate.erase(iter);
799                         break;
800                 }
801         }
802
803         return Generate(pos);
804 }
805
806 bool ChunkLoader::OutOfRange(const Chunk::Pos &pos) const noexcept {
807         return std::abs(base.x - pos.x) > unload_dist
808                         || std::abs(base.y - pos.y) > unload_dist
809                         || std::abs(base.z - pos.z) > unload_dist;
810 }
811
812 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
813         if (new_base == base) {
814                 return;
815         }
816         base = new_base;
817
818         // unload far away chunks
819         for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
820                 if (OutOfRange(*iter)) {
821                         auto saved = iter;
822                         Remove(*saved);
823                         ++iter;
824                         to_free.splice(to_free.end(), loaded, saved);
825                 } else {
826                         ++iter;
827                 }
828         }
829         // abort far away queued chunks
830         for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
831                 if (OutOfRange(*iter)) {
832                         iter = to_generate.erase(iter);
833                 } else {
834                         ++iter;
835                 }
836         }
837         // add missing new chunks
838         GenerateSurrounding(base);
839 }
840
841 void ChunkLoader::GenerateSurrounding(const Chunk::Pos &pos) {
842         const Chunk::Pos offset(load_dist, load_dist, load_dist);
843         Generate(pos - offset, pos + offset);
844 }
845
846 void ChunkLoader::Update(int dt) {
847         gen_timer.Update(dt);
848         if (!gen_timer.Hit() || to_generate.empty()) {
849                 return;
850         }
851
852         Chunk::Pos pos(to_generate.front());
853         to_generate.pop_front();
854
855         for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
856                 if (iter->Position() == pos) {
857                         iter->Relink();
858                         loaded.splice(loaded.end(), to_free, iter);
859                 }
860         }
861
862         if (to_free.empty()) {
863                 loaded.emplace_back(reg);
864         } else {
865                 to_free.front().ClearNeighbors();
866                 loaded.splice(loaded.end(), to_free, to_free.begin());
867         }
868         Chunk &chunk = loaded.back();
869         chunk.Position(pos);
870         gen(chunk);
871         Insert(chunk);
872 }
873
874 }