]> git.localhorst.tv Git - blank.git/blob - src/world/ChunkLoader.hpp
try to get every chunk change saved to disk
[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         const WorldSave &SaveFile() const noexcept { return save; }
37
38         Chunk *Loaded(const Chunk::Pos &) noexcept;
39         bool Queued(const Chunk::Pos &) noexcept;
40         bool Known(const Chunk::Pos &) noexcept;
41         Chunk &ForceLoad(const Chunk::Pos &);
42
43         bool OutOfRange(const Chunk &c) const noexcept { return OutOfRange(c.Position()); }
44         bool OutOfRange(const Chunk::Pos &) const noexcept;
45
46         void Rebase(const Chunk::Pos &);
47         void Update(int dt);
48
49         std::size_t ToLoad() const noexcept { return to_load.size(); }
50         // returns true if the chunk was generated
51         bool LoadOne();
52         void LoadN(std::size_t n);
53
54 private:
55         Chunk &Load(const Chunk::Pos &pos);
56         // link given chunk to all loaded neighbors
57         void Insert(Chunk &) noexcept;
58         // remove a loaded chunk
59         // this unlinks it from its neighbors as well as moves it to the free list
60         // given iterator must point to a chunk from the loaded list
61         // returns an iterator to the chunk following the removed one
62         // in the loaded list (end for the last one)
63         std::list<Chunk>::iterator Remove(std::list<Chunk>::iterator) noexcept;
64
65 private:
66         Chunk::Pos base;
67
68         const BlockTypeRegistry &reg;
69         const Generator &gen;
70         const WorldSave &save;
71
72         std::list<Chunk> loaded;
73         std::list<Chunk::Pos> to_load;
74         std::list<Chunk> to_free;
75
76         IntervalTimer gen_timer;
77
78         int load_dist;
79         int unload_dist;
80
81 };
82
83 }
84
85 #endif