]> git.localhorst.tv Git - blank.git/blob - src/world/ChunkLoader.hpp
save and load chunk data
[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         void LoadOne();
50         void LoadN(std::size_t n);
51
52 private:
53         Chunk &Load(const Chunk::Pos &pos);
54         // link given chunk to all loaded neighbors
55         void Insert(Chunk &) noexcept;
56         // remove a loaded chunk
57         // this unlinks it from its neighbors as well as moves it to the free list
58         // given iterator must point to a chunk from the loaded list
59         // returns an iterator to the chunk following the removed one
60         // in the loaded list (end for the last one)
61         std::list<Chunk>::iterator Remove(std::list<Chunk>::iterator) noexcept;
62
63 private:
64         Chunk::Pos base;
65
66         const BlockTypeRegistry &reg;
67         const Generator &gen;
68         const WorldSave &save;
69
70         std::list<Chunk> loaded;
71         std::list<Chunk::Pos> to_load;
72         std::list<Chunk> to_free;
73
74         IntervalTimer gen_timer;
75
76         int load_dist;
77         int unload_dist;
78
79 };
80
81 }
82
83 #endif