]> git.localhorst.tv Git - blank.git/blob - src/chunk.hpp
split chunk loader from world
[blank.git] / src / chunk.hpp
1 #ifndef BLANK_CHUNK_HPP_
2 #define BLANK_CHUNK_HPP_
3
4 #include "block.hpp"
5 #include "geometry.hpp"
6 #include "model.hpp"
7
8 #include <list>
9 #include <vector>
10 #include <glm/glm.hpp>
11
12
13 namespace blank {
14
15 /// cube of size 16 (256 tiles, 4096 blocks)
16 class Chunk {
17
18 public:
19         using Pos = glm::tvec3<int>;
20
21 public:
22         explicit Chunk(const BlockTypeRegistry &);
23
24         Chunk(Chunk &&);
25         Chunk &operator =(Chunk &&);
26
27         static constexpr int Width() { return 16; }
28         static constexpr int Height() { return 16; }
29         static constexpr int Depth() { return 16; }
30         static Pos Extent() { return { Width(), Height(), Depth() }; }
31         static constexpr int Size() { return Width() * Height() * Depth(); }
32
33         static AABB Bounds() { return AABB{ { 0, 0, 0 }, Extent() }; }
34
35         static constexpr bool InBounds(const glm::vec3 &pos) {
36                 return
37                         pos.x >= 0 && pos.x < Width() &&
38                         pos.y >= 0 && pos.y < Height() &&
39                         pos.z >= 0 && pos.z < Depth();
40         }
41         static constexpr int ToIndex(const glm::vec3 &pos) {
42                 return int(pos.x) + int(pos.y) * Width() + int(pos.z) * Width() * Height();
43         }
44         static constexpr bool InBounds(int idx) {
45                 return idx >= 0 && idx < Size();
46         }
47         static Block::Pos ToCoords(int idx) {
48                 return Block::Pos(
49                         0.5f + (idx % Width()),
50                         0.5f + ((idx / Width()) % Height()),
51                         0.5f + (idx / (Width() * Height()))
52                 );
53         }
54
55         void Allocate();
56         void Invalidate() { dirty = true; }
57
58         Block &BlockAt(int index) { return blocks[index]; }
59         const Block &BlockAt(int index) const { return blocks[index]; }
60         Block &BlockAt(const Block::Pos &pos) { return BlockAt(ToIndex(pos)); }
61         const Block &BlockAt(const Block::Pos &pos) const { return BlockAt(ToIndex(pos)); }
62
63         const BlockType &Type(const Block &b) const { return *types->Get(b.type); }
64
65         bool Intersection(
66                 const Ray &,
67                 const glm::mat4 &M,
68                 int *blkid = nullptr,
69                 float *dist = nullptr,
70                 glm::vec3 *normal = nullptr) const;
71
72         void Position(const Pos &);
73         const Pos &Position() const { return position; }
74         glm::mat4 Transform(const Pos &offset) const;
75
76         void CheckUpdate();
77         void Draw();
78
79 private:
80         void Update();
81
82 private:
83         const BlockTypeRegistry *types;
84         std::vector<Block> blocks;
85         Model model;
86         Pos position;
87         bool dirty;
88
89 };
90
91
92 class Generator;
93
94 class ChunkLoader {
95
96 public:
97         ChunkLoader(const BlockTypeRegistry &, const Generator &);
98
99         void Generate(const Chunk::Pos &from, const Chunk::Pos &to);
100
101         std::list<Chunk> &Loaded() { return loaded; }
102
103         Chunk *Loaded(const Chunk::Pos &);
104         Chunk *Queued(const Chunk::Pos &);
105         Chunk *Known(const Chunk::Pos &);
106         Chunk &ForceLoad(const Chunk::Pos &);
107
108         void Rebase(const Chunk::Pos &);
109         void Update();
110
111 private:
112         Chunk::Pos base;
113
114         const BlockTypeRegistry &reg;
115         const Generator &gen;
116
117         std::list<Chunk> loaded;
118         std::list<Chunk> to_generate;
119         std::list<Chunk> to_free;
120
121         int load_dist;
122         int unload_dist;
123
124 };
125
126 }
127
128 #endif