]> git.localhorst.tv Git - blank.git/blob - src/world/Chunk.hpp
sped up chunk generation a little
[blank.git] / src / world / Chunk.hpp
1 #ifndef BLANK_WORLD_CHUNK_HPP_
2 #define BLANK_WORLD_CHUNK_HPP_
3
4 #include "Block.hpp"
5 #include "BlockTypeRegistry.hpp"
6 #include "../model/geometry.hpp"
7
8 #include <vector>
9 #include <glm/glm.hpp>
10 #include <glm/gtx/transform.hpp>
11
12
13 namespace blank {
14
15 class BlockType;
16 class WorldCollision;
17
18 /// cube of size 16 (256 tiles, 4096 blocks)
19 class Chunk {
20
21 public:
22         using Pos = glm::ivec3;
23
24 public:
25         explicit Chunk(const BlockTypeRegistry &) noexcept;
26
27         Chunk(Chunk &&) noexcept;
28         Chunk &operator =(Chunk &&) noexcept;
29
30         static constexpr int width = 16;
31         static constexpr int height = 16;
32         static constexpr int depth = 16;
33         static Pos Extent() noexcept { return { width, height, depth }; }
34         static constexpr int size = width * height * depth;
35
36         static AABB Bounds() noexcept { return AABB{ { 0, 0, 0 }, Extent() }; }
37
38         static constexpr bool InBounds(const Block::Pos &pos) noexcept {
39                 return
40                         pos.x >= 0 && pos.x < width &&
41                         pos.y >= 0 && pos.y < height &&
42                         pos.z >= 0 && pos.z < depth;
43         }
44         static constexpr bool InBounds(const Pos &pos) noexcept {
45                 return
46                         pos.x >= 0 && pos.x < width &&
47                         pos.y >= 0 && pos.y < height &&
48                         pos.z >= 0 && pos.z < depth;
49         }
50         static constexpr int ToIndex(const Pos &pos) noexcept {
51                 return pos.x + pos.y * width + pos.z * width * height;
52         }
53         static constexpr bool InBounds(int idx) noexcept {
54                 return idx >= 0 && idx < size;
55         }
56         static Block::Pos ToCoords(int idx) noexcept {
57                 return Block::Pos(
58                         0.5f + (idx % width),
59                         0.5f + ((idx / width) % height),
60                         0.5f + (idx / (width * height))
61                 );
62         }
63         static Block::Pos ToCoords(const Pos &pos) noexcept {
64                 return Block::Pos(pos) + 0.5f;
65         }
66         static Pos ToPos(int idx) noexcept {
67                 return Pos(
68                         (idx % width),
69                         ((idx / width) % height),
70                         (idx / (width * height))
71                 );
72         }
73         glm::mat4 ToTransform(const Pos &pos, int idx) const noexcept;
74
75         Block::Pos ToSceneCoords(const Pos &base, const Block::Pos &pos) const noexcept {
76                 return Block::Pos((position - base) * Extent()) + pos;
77         }
78
79         static bool IsBorder(const Pos &pos) noexcept {
80                 return
81                         pos.x == 0 ||
82                         pos.x == width - 1 ||
83                         pos.y == 0 ||
84                         pos.y == height - 1 ||
85                         pos.z == 0 ||
86                         pos.z == depth - 1;
87         }
88         static constexpr bool IsBorder(int idx) noexcept {
89                 return
90                         idx < width * height ||                    // low Z plane
91                         idx % width == 0 ||                          // low X plane
92                         (idx / (width * height)) == depth - 1 || // high Z plane
93                         idx % width == width - 1 ||                // high X plane
94                         (idx / width) % height == 0 ||             // low Y plane
95                         (idx / width) % height == height - 1;    // high Y plane
96         }
97
98         bool IsSurface(int index) const noexcept { return IsSurface(ToPos(index)); }
99         bool IsSurface(const Block::Pos &pos) const noexcept { return IsSurface(Pos(pos)); }
100         bool IsSurface(const Pos &pos) const noexcept;
101
102         void SetNeighbor(Block::Face, Chunk &) noexcept;
103         bool HasNeighbor(Block::Face f) const noexcept { return neighbor[f]; }
104         Chunk &GetNeighbor(Block::Face f) noexcept { return *neighbor[f]; }
105         const Chunk &GetNeighbor(Block::Face f) const noexcept { return *neighbor[f]; }
106         void Unlink() noexcept;
107
108         // check which faces of a block at given index are obstructed (and therefore invisible)
109         Block::FaceSet Obstructed(const Pos &) const noexcept;
110
111         void SetBlock(int index, const Block &) noexcept;
112         void SetBlock(const Block::Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
113         void SetBlock(const Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
114
115         const Block &BlockAt(int index) const noexcept { return blocks[index]; }
116         const Block &BlockAt(const Block::Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
117         const Block &BlockAt(const Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
118
119         const BlockType &Type(const Block &b) const noexcept { return types->Get(b.type); }
120         const BlockType &Type(int index) const noexcept { return Type(BlockAt(index)); }
121
122         void SetLight(int index, int level) noexcept;
123         void SetLight(const Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
124         void SetLight(const Block::Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
125
126         int GetLight(int index) const noexcept;
127         int GetLight(const Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
128         int GetLight(const Block::Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
129
130         float GetVertexLight(const Pos &, const BlockModel::Position &, const EntityModel::Normal &) const noexcept;
131
132         bool Intersection(
133                 const Ray &ray,
134                 const glm::mat4 &M,
135                 float &dist
136         ) const noexcept {
137                 return blank::Intersection(ray, Bounds(), M, &dist);
138         }
139
140         bool Intersection(
141                 const Ray &,
142                 const glm::mat4 &M,
143                 WorldCollision &) noexcept;
144
145         bool Intersection(
146                 const AABB &box,
147                 const glm::mat4 &Mbox,
148                 const glm::mat4 &Mchunk,
149                 std::vector<WorldCollision> &) noexcept;
150
151         void Position(const Pos &pos) noexcept { position = pos; }
152         const Pos &Position() const noexcept { return position; }
153         glm::mat4 Transform(const Pos &offset) const noexcept {
154                 return glm::translate((position - offset) * Extent());
155         }
156
157         void *BlockData() noexcept { return &blocks[0]; }
158         const void *BlockData() const noexcept { return &blocks[0]; }
159         static constexpr std::size_t BlockSize() noexcept { return offsetof(Chunk, position) - offsetof(Chunk, blocks); }
160
161         bool Generated() const noexcept { return generated; }
162         void SetGenerated() noexcept { generated = true; }
163         bool Lighted() const noexcept { return lighted; }
164         void ScanLights();
165
166         void Ref() noexcept { ++ref_count; }
167         void UnRef() noexcept { --ref_count; }
168         bool Referenced() const noexcept { return ref_count > 0; }
169
170         void Invalidate() noexcept { dirty_model = dirty_save = true; }
171         void InvalidateModel() noexcept { dirty_model = true; }
172         void ClearModel() noexcept { dirty_model = false; }
173         void ClearSave() noexcept { dirty_save = false; }
174         bool ShouldUpdateModel() const noexcept { return dirty_model; }
175         bool ShouldUpdateSave() const noexcept { return dirty_save; }
176
177         void Update(BlockModel &) noexcept;
178
179 private:
180         const BlockTypeRegistry *types;
181         Chunk *neighbor[Block::FACE_COUNT];
182
183         Block blocks[size];
184         unsigned char light[size];
185         bool generated;
186         bool lighted;
187
188         Pos position;
189         int ref_count;
190         bool dirty_model;
191         bool dirty_save;
192
193 };
194
195 }
196
197 #endif