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