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