]> git.localhorst.tv Git - blank.git/blob - src/chunk.cpp
simplified block face stuff
[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 BlockModel::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(i, buf.vertices[vtx], buf.normals[vtx]));
456                 }
457         }
458
459         model.Update(buf);
460         dirty = false;
461 }
462
463 bool Chunk::Obstructed(int idx) const {
464         Chunk::Pos pos(ToPos(idx));
465
466         for (int f = 0; f < Block::FACE_COUNT; ++f) {
467                 Block::Face face = Block::Face(f);
468                 BlockLookup next(const_cast<Chunk *>(this), pos, face);
469                 if (!next || !next.GetType().FaceFilled(next.GetBlock(), Block::Opposite(face))) {
470                         return false;
471                 }
472         }
473
474         return true;
475 }
476
477 glm::mat4 Chunk::ToTransform(int idx) const {
478         return glm::translate(glm::mat4(1.0f), ToCoords(idx)) * blocks[idx].Transform();
479 }
480
481
482 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p)
483 : chunk(c), pos(p) {
484         while (pos.x >= Chunk::Width()) {
485                 if (chunk->HasNeighbor(Block::FACE_RIGHT)) {
486                         chunk = &chunk->GetNeighbor(Block::FACE_RIGHT);
487                         pos.x -= Chunk::Width();
488                 } else {
489                         chunk = nullptr;
490                         return;
491                 }
492         }
493         while (pos.x < 0) {
494                 if (chunk->HasNeighbor(Block::FACE_LEFT)) {
495                         chunk = &chunk->GetNeighbor(Block::FACE_LEFT);
496                         pos.x += Chunk::Width();
497                 } else {
498                         chunk = nullptr;
499                         return;
500                 }
501         }
502         while (pos.y >= Chunk::Height()) {
503                 if (chunk->HasNeighbor(Block::FACE_UP)) {
504                         chunk = &chunk->GetNeighbor(Block::FACE_UP);
505                         pos.y -= Chunk::Height();
506                 } else {
507                         chunk = nullptr;
508                         return;
509                 }
510         }
511         while (pos.y < 0) {
512                 if (chunk->HasNeighbor(Block::FACE_DOWN)) {
513                         chunk = &chunk->GetNeighbor(Block::FACE_DOWN);
514                         pos.y += Chunk::Height();
515                 } else {
516                         chunk = nullptr;
517                         return;
518                 }
519         }
520         while (pos.z >= Chunk::Depth()) {
521                 if (chunk->HasNeighbor(Block::FACE_FRONT)) {
522                         chunk = &chunk->GetNeighbor(Block::FACE_FRONT);
523                         pos.z -= Chunk::Depth();
524                 } else {
525                         chunk = nullptr;
526                         return;
527                 }
528         }
529         while (pos.z < 0) {
530                 if (chunk->HasNeighbor(Block::FACE_BACK)) {
531                         chunk = &chunk->GetNeighbor(Block::FACE_BACK);
532                         pos.z += Chunk::Depth();
533                 } else {
534                         chunk = nullptr;
535                         return;
536                 }
537         }
538 }
539
540 BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face face)
541 : chunk(c), pos(p) {
542         pos += Block::FaceNormal(face);
543         if (!Chunk::InBounds(pos)) {
544                 pos -= Block::FaceNormal(face) * Chunk::Extent();
545                 chunk = &chunk->GetNeighbor(face);
546         }
547 }
548
549
550 ChunkLoader::ChunkLoader(const Config &config, const BlockTypeRegistry &reg, const Generator &gen)
551 : base(0, 0, 0)
552 , reg(reg)
553 , gen(gen)
554 , loaded()
555 , to_generate()
556 , to_free()
557 , load_dist(config.load_dist)
558 , unload_dist(config.unload_dist) {
559
560 }
561
562 namespace {
563
564 struct ChunkLess {
565
566         explicit ChunkLess(const Chunk::Pos &base)
567         : base(base) { }
568
569         bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const {
570                 Chunk::Pos da(base - a);
571                 Chunk::Pos db(base - b);
572                 return
573                         da.x * da.x + da.y * da.y + da.z * da.z <
574                         db.x * db.x + db.y * db.y + db.z * db.z;
575         }
576
577         Chunk::Pos base;
578
579 };
580
581 }
582
583 void ChunkLoader::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
584         for (int z = from.z; z < to.z; ++z) {
585                 for (int y = from.y; y < to.y; ++y) {
586                         for (int x = from.x; x < to.x; ++x) {
587                                 Chunk::Pos pos(x, y, z);
588                                 if (Known(pos)) {
589                                         continue;
590                                 } else if (pos == base) {
591                                         Generate(pos);
592
593                                 //      light testing
594                                 //      for (int i = 0; i < 16; ++i) {
595                                 //              for (int j = 0; j < 16; ++j) {
596                                 //                      loaded.back().SetBlock(Chunk::Pos{  i, j,  0 }, Block(1));
597                                 //                      loaded.back().SetBlock(Chunk::Pos{  i, j, 15 }, Block(1));
598                                 //                      loaded.back().SetBlock(Chunk::Pos{  0, j,  i }, Block(1));
599                                 //                      loaded.back().SetBlock(Chunk::Pos{ 15, j,  i }, Block(1));
600                                 //              }
601                                 //      }
602                                 //      loaded.back().SetBlock(Chunk::Pos{  1,  0,  1 }, Block(13));
603                                 //      loaded.back().SetBlock(Chunk::Pos{ 14,  0,  1 }, Block(13));
604                                 //      loaded.back().SetBlock(Chunk::Pos{  1,  0, 14 }, Block(13));
605                                 //      loaded.back().SetBlock(Chunk::Pos{ 14,  0, 14 }, Block(13));
606                                 //      loaded.back().SetBlock(Chunk::Pos{  1, 15,  1 }, Block(13));
607                                 //      loaded.back().SetBlock(Chunk::Pos{ 14, 15,  1 }, Block(13));
608                                 //      loaded.back().SetBlock(Chunk::Pos{  1, 15, 14 }, Block(13));
609                                 //      loaded.back().SetBlock(Chunk::Pos{ 14, 15, 14 }, Block(13));
610                                 //      loaded.back().SetBlock(Chunk::Pos{  7,  7,  0 }, Block(13));
611                                 //      loaded.back().SetBlock(Chunk::Pos{  8,  7,  0 }, Block(13));
612                                 //      loaded.back().SetBlock(Chunk::Pos{  7,  8,  0 }, Block(13));
613                                 //      loaded.back().SetBlock(Chunk::Pos{  8,  8,  0 }, Block(13));
614                                 //      loaded.back().SetBlock(Chunk::Pos{  7,  7, 15 }, Block(13));
615                                 //      loaded.back().SetBlock(Chunk::Pos{  8,  7, 15 }, Block(13));
616                                 //      loaded.back().SetBlock(Chunk::Pos{  7,  8, 15 }, Block(13));
617                                 //      loaded.back().SetBlock(Chunk::Pos{  8,  8, 15 }, Block(13));
618                                 //      loaded.back().SetBlock(Chunk::Pos{  0,  7,  7 }, Block(13));
619                                 //      loaded.back().SetBlock(Chunk::Pos{  0,  7,  8 }, Block(13));
620                                 //      loaded.back().SetBlock(Chunk::Pos{  0,  8,  7 }, Block(13));
621                                 //      loaded.back().SetBlock(Chunk::Pos{  0,  8,  8 }, Block(13));
622                                 //      loaded.back().SetBlock(Chunk::Pos{ 15,  7,  7 }, Block(13));
623                                 //      loaded.back().SetBlock(Chunk::Pos{ 15,  7,  8 }, Block(13));
624                                 //      loaded.back().SetBlock(Chunk::Pos{ 15,  8,  7 }, Block(13));
625                                 //      loaded.back().SetBlock(Chunk::Pos{ 15,  8,  8 }, Block(13));
626                                 //      loaded.back().Invalidate();
627                                 //      loaded.back().CheckUpdate();
628
629                                 //      orientation testing
630                                 //      for (int i = 0; i < Block::FACE_COUNT; ++i) {
631                                 //              for (int j = 0; j < Block::TURN_COUNT; ++j) {
632                                 //                      loaded.back().BlockAt(512 * j + 2 * i) = Block(3 * (j + 1), Block::Face(i), Block::Turn(j));
633                                 //              }
634                                 //      }
635                                 //      loaded.back().Invalidate();
636                                 //      loaded.back().CheckUpdate();
637                                 } else {
638                                         to_generate.emplace_back(pos);
639                                 }
640                         }
641                 }
642         }
643         to_generate.sort(ChunkLess(base));
644 }
645
646 Chunk &ChunkLoader::Generate(const Chunk::Pos &pos) {
647         loaded.emplace_back(reg);
648         Chunk &chunk = loaded.back();
649         chunk.Position(pos);
650         gen(chunk);
651         Insert(chunk);
652         return chunk;
653 }
654
655 void ChunkLoader::Insert(Chunk &chunk) {
656         for (Chunk &other : loaded) {
657                 chunk.SetNeighbor(other);
658         }
659 }
660
661 void ChunkLoader::Remove(Chunk &chunk) {
662         chunk.Unlink();
663 }
664
665 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) {
666         for (Chunk &chunk : loaded) {
667                 if (chunk.Position() == pos) {
668                         return &chunk;
669                 }
670         }
671         return nullptr;
672 }
673
674 bool ChunkLoader::Queued(const Chunk::Pos &pos) {
675         for (const Chunk::Pos &chunk : to_generate) {
676                 if (chunk == pos) {
677                         return true;
678                 }
679         }
680         return nullptr;
681 }
682
683 bool ChunkLoader::Known(const Chunk::Pos &pos) {
684         if (Loaded(pos)) return true;
685         return Queued(pos);
686 }
687
688 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
689         Chunk *chunk = Loaded(pos);
690         if (chunk) {
691                 return *chunk;
692         }
693
694         for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
695                 if (*iter == pos) {
696                         to_generate.erase(iter);
697                         break;
698                 }
699         }
700
701         return Generate(pos);
702 }
703
704 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
705         if (new_base == base) {
706                 return;
707         }
708         base = new_base;
709
710         // unload far away chunks
711         for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
712                 if (std::abs(base.x - iter->Position().x) > unload_dist
713                                 || std::abs(base.y - iter->Position().y) > unload_dist
714                                 || std::abs(base.z - iter->Position().z) > unload_dist) {
715                         auto saved = iter;
716                         Remove(*saved);
717                         ++iter;
718                         to_free.splice(to_free.end(), loaded, saved);
719                 } else {
720                         ++iter;
721                 }
722         }
723         // abort far away queued chunks
724         for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
725                 if (std::abs(base.x - iter->x) > unload_dist
726                                 || std::abs(base.y - iter->y) > unload_dist
727                                 || std::abs(base.z - iter->z) > unload_dist) {
728                         iter = to_generate.erase(iter);
729                 } else {
730                         ++iter;
731                 }
732         }
733         // add missing new chunks
734         GenerateSurrounding(base);
735 }
736
737 void ChunkLoader::GenerateSurrounding(const Chunk::Pos &pos) {
738         const Chunk::Pos offset(load_dist, load_dist, load_dist);
739         Generate(pos - offset, pos + offset);
740 }
741
742 void ChunkLoader::Update() {
743         if (to_generate.empty()) {
744                 return;
745         }
746
747         Chunk::Pos pos(to_generate.front());
748         to_generate.pop_front();
749
750         for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
751                 if (iter->Position() == pos) {
752                         iter->Relink();
753                         loaded.splice(loaded.end(), to_free, iter);
754                         return;
755                 }
756         }
757
758         if (to_free.empty()) {
759                 loaded.emplace_back(reg);
760         } else {
761                 to_free.front().ClearNeighbors();
762                 loaded.splice(loaded.end(), to_free, to_free.begin());
763         }
764         Chunk &chunk = loaded.back();
765         chunk.Position(pos);
766         gen(chunk);
767         Insert(chunk);
768 }
769
770 }