]> git.localhorst.tv Git - blank.git/blob - src/world/ChunkLoader.hpp
81e7178a5f4c4bb203b1a8a5ce72317d8e67823d
[blank.git] / src / world / ChunkLoader.hpp
1 #ifndef BLANK_WORLD_CHUNKLOADER_HPP_
2 #define BLANK_WORLD_CHUNKLOADER_HPP_
3
4 #include "Chunk.hpp"
5 #include "../app/IntervalTimer.hpp"
6
7 #include <list>
8
9
10 namespace blank {
11
12 class BlockTypeRegistry;
13 class Generator;
14 class WorldSave;
15
16 class ChunkLoader {
17
18 public:
19         struct Config {
20                 int load_dist = 6;
21                 int unload_dist = 8;
22                 int gen_limit = 16;
23         };
24
25         ChunkLoader(
26                 const Config &,
27                 const BlockTypeRegistry &,
28                 const Generator &,
29                 const WorldSave &
30         ) noexcept;
31
32         void Queue(const Chunk::Pos &from, const Chunk::Pos &to);
33         void QueueSurrounding(const Chunk::Pos &);
34
35         std::list<Chunk> &Loaded() noexcept { return loaded; }
36
37         Chunk *Loaded(const Chunk::Pos &) noexcept;
38         bool Queued(const Chunk::Pos &) noexcept;
39         bool Known(const Chunk::Pos &) noexcept;
40         Chunk &ForceLoad(const Chunk::Pos &);
41
42         bool OutOfRange(const Chunk &c) const noexcept { return OutOfRange(c.Position()); }
43         bool OutOfRange(const Chunk::Pos &) const noexcept;
44
45         void Rebase(const Chunk::Pos &);
46         void Update(int dt);
47
48         std::size_t ToLoad() const noexcept { return to_load.size(); }
49         // returns true if the chunk was generated
50         bool LoadOne();
51         void LoadN(std::size_t n);
52
53 private:
54         Chunk &Load(const Chunk::Pos &pos);
55         // link given chunk to all loaded neighbors
56         void Insert(Chunk &) noexcept;
57         // remove a loaded chunk
58         // this unlinks it from its neighbors as well as moves it to the free list
59         // given iterator must point to a chunk from the loaded list
60         // returns an iterator to the chunk following the removed one
61         // in the loaded list (end for the last one)
62         std::list<Chunk>::iterator Remove(std::list<Chunk>::iterator) noexcept;
63
64 private:
65         Chunk::Pos base;
66
67         const BlockTypeRegistry &reg;
68         const Generator &gen;
69         const WorldSave &save;
70
71         std::list<Chunk> loaded;
72         std::list<Chunk::Pos> to_load;
73         std::list<Chunk> to_free;
74
75         IntervalTimer gen_timer;
76
77         int load_dist;
78         int unload_dist;
79
80 };
81
82 }
83
84 #endif