]> git.localhorst.tv Git - blank.git/blob - src/chunk.hpp
fix error in border block calculation
[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         glm::mat4 ToTransform(int idx) const;
55
56         static constexpr bool IsBorder(int idx) {
57                 return
58                         idx < Width() * Height() ||                    // low Z plane
59                         idx % Width() == 0 ||                          // low X plane
60                         (idx / (Width() * Height())) == Depth() - 1 || // high Z plane
61                         idx % Width() == Width() - 1 ||                // high X plane
62                         (idx / Width()) % Height() == 0 ||             // low Y plane
63                         (idx / Width()) % Height() == Height() - 1;    // high Y plane
64         }
65
66         // check if block at given index is completely enclosed (and therefore invisible)
67         bool Obstructed(int idx) const;
68
69         void Allocate();
70         void Invalidate() { dirty = true; }
71
72         Block &BlockAt(int index) { return blocks[index]; }
73         const Block &BlockAt(int index) const { return blocks[index]; }
74         Block &BlockAt(const Block::Pos &pos) { return BlockAt(ToIndex(pos)); }
75         const Block &BlockAt(const Block::Pos &pos) const { return BlockAt(ToIndex(pos)); }
76
77         const BlockType &Type(const Block &b) const { return *types->Get(b.type); }
78
79         bool Intersection(
80                 const Ray &ray,
81                 const glm::mat4 &M,
82                 float &dist
83         ) const {
84                 return blank::Intersection(ray, Bounds(), M, &dist);
85         }
86
87         bool Intersection(
88                 const Ray &,
89                 const glm::mat4 &M,
90                 int &blkid,
91                 float &dist,
92                 glm::vec3 &normal) const;
93
94         void Position(const Pos &);
95         const Pos &Position() const { return position; }
96         glm::mat4 Transform(const Pos &offset) const;
97
98         void CheckUpdate();
99         void Draw();
100
101 private:
102         void Update();
103
104 private:
105         const BlockTypeRegistry *types;
106         std::vector<Block> blocks;
107         Model model;
108         Pos position;
109         bool dirty;
110
111 };
112
113
114 class Generator;
115
116 class ChunkLoader {
117
118 public:
119         ChunkLoader(const BlockTypeRegistry &, const Generator &);
120
121         void Generate(const Chunk::Pos &from, const Chunk::Pos &to);
122
123         std::list<Chunk> &Loaded() { return loaded; }
124
125         Chunk *Loaded(const Chunk::Pos &);
126         bool Queued(const Chunk::Pos &);
127         bool Known(const Chunk::Pos &);
128         Chunk &ForceLoad(const Chunk::Pos &);
129
130         void Rebase(const Chunk::Pos &);
131         void Update();
132
133 private:
134         Chunk::Pos base;
135
136         const BlockTypeRegistry &reg;
137         const Generator &gen;
138
139         std::list<Chunk> loaded;
140         std::list<Chunk::Pos> to_generate;
141         std::list<Chunk> to_free;
142
143         int load_dist;
144         int unload_dist;
145
146 };
147
148 }
149
150 #endif