]> git.localhorst.tv Git - blank.git/blob - src/world/Chunk.hpp
glm backwards compatibility
[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 "../geometry/Location.hpp"
7 #include "../geometry/primitive.hpp"
8 #include "../graphics/glm.hpp"
9
10 #include <set>
11 #include <vector>
12 #include <glm/gtx/transform.hpp>
13
14
15 namespace blank {
16
17 class BlockType;
18 class Entity;
19 class WorldCollision;
20
21 /// cube of size 16 (256 tiles, 4096 blocks)
22 class Chunk {
23
24 public:
25         explicit Chunk(const BlockTypeRegistry &) noexcept;
26
27         Chunk(Chunk &&) noexcept;
28         Chunk &operator =(Chunk &&) noexcept;
29
30         static constexpr int side = ExactLocation::scale;
31         static constexpr float fside = ExactLocation::fscale;
32         static constexpr int size = side * side * side;
33
34         static AABB Bounds() noexcept { return AABB{ { 0.0f, 0.0f, 0.0f }, ExactLocation::FExtent() }; }
35         static glm::vec3 Center() noexcept { return glm::vec3(8.0f); }
36         static float Radius() noexcept { return 27.71281292110203669632f; /* 16 * √3 */ }
37
38         /// get bounding box relative to given reference chunk
39         AABB RelativeBounds(const ExactLocation::Coarse &ref) const noexcept {
40                 AABB bounds;
41                 bounds.min = (position - ref) * ExactLocation::Extent();
42                 bounds.max = bounds.min + ExactLocation::FExtent();
43                 return bounds;
44         }
45
46         static constexpr bool InBounds(const ExactLocation::Fine &pos) noexcept {
47                 return
48                         pos.x >= 0.0f && pos.x < fside &&
49                         pos.y >= 0.0f && pos.y < fside &&
50                         pos.z >= 0.0f && pos.z < fside;
51         }
52         static constexpr bool InBounds(const RoughLocation::Fine &pos) noexcept {
53                 return
54                         pos.x >= 0 && pos.x < side &&
55                         pos.y >= 0 && pos.y < side &&
56                         pos.z >= 0 && pos.z < side;
57         }
58         static int ToIndex(const ExactLocation::Fine &pos) noexcept {
59                 return ToIndex(RoughLocation::Fine(pos));
60         }
61         static constexpr int ToIndex(const RoughLocation::Fine &pos) noexcept {
62                 return pos.x + pos.y * side + pos.z * side * side;
63         }
64         static constexpr bool InBounds(int idx) noexcept {
65                 return idx >= 0 && idx < size;
66         }
67         static ExactLocation::Fine ToCoords(int idx) noexcept {
68                 return ExactLocation::Fine(
69                         0.5f + (idx % side),
70                         0.5f + ((idx / side) % side),
71                         0.5f + (idx / (side * side))
72                 );
73         }
74         static ExactLocation::Fine ToCoords(const RoughLocation::Fine &pos) noexcept {
75                 return ExactLocation::Fine(pos) + 0.5f;
76         }
77         static RoughLocation::Fine ToPos(int idx) noexcept {
78                 return RoughLocation::Fine(
79                         (idx % side),
80                         ((idx / side) % side),
81                         (idx / (side * side))
82                 );
83         }
84         /// get a chunk-local transform for block at given position and index
85         /// (position and index are redundant)
86         glm::mat4 ToTransform(const RoughLocation::Fine &position, int index) const noexcept;
87         /// same as above, but also apply offset to given reference
88         glm::mat4 ToTransform(const ExactLocation::Coarse &ref, const RoughLocation::Fine &pos, int idx) const noexcept;
89
90         ExactLocation::Fine ToSceneCoords(const ExactLocation::Coarse &base, const ExactLocation::Fine &pos) const noexcept {
91                 return ExactLocation::Fine((position - base) * ExactLocation::Extent()) + pos;
92         }
93
94         static bool IsBorder(const RoughLocation::Fine &pos) noexcept {
95                 return
96                         pos.x == 0 ||
97                         pos.x == side - 1 ||
98                         pos.y == 0 ||
99                         pos.y == side - 1 ||
100                         pos.z == 0 ||
101                         pos.z == side - 1;
102         }
103         static constexpr bool IsBorder(int idx) noexcept {
104                 return
105                         idx < side * side ||                 // low Z plane
106                         idx % side == 0 ||                   // low X plane
107                         (idx / (side * side)) == side - 1 || // high Z plane
108                         idx % side == side - 1 ||            // high X plane
109                         (idx / side) % side == 0 ||          // low Y plane
110                         (idx / side) % side == side - 1;     // high Y plane
111         }
112
113         bool IsSurface(int index) const noexcept { return IsSurface(ToPos(index)); }
114         bool IsSurface(const ExactLocation::Fine &pos) const noexcept { return IsSurface(RoughLocation::Fine(pos)); }
115         bool IsSurface(const RoughLocation::Fine &pos) const noexcept;
116
117         void SetNeighbor(Block::Face, Chunk &) noexcept;
118         bool HasNeighbor(Block::Face f) const noexcept { return neighbor[f]; }
119         Chunk &GetNeighbor(Block::Face f) noexcept { return *neighbor[f]; }
120         const Chunk &GetNeighbor(Block::Face f) const noexcept { return *neighbor[f]; }
121         void Unlink() noexcept;
122
123         // check which faces of a block at given index are obstructed (and therefore invisible)
124         Block::FaceSet Obstructed(const RoughLocation::Fine &) const noexcept;
125
126         void SetBlock(int index, const Block &) noexcept;
127         void SetBlock(const ExactLocation::Fine &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
128         void SetBlock(const RoughLocation::Fine &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
129
130         const Block &BlockAt(int index) const noexcept { return blocks[index]; }
131         const Block &BlockAt(const ExactLocation::Fine &pos) const noexcept { return BlockAt(ToIndex(pos)); }
132         const Block &BlockAt(const RoughLocation::Fine &pos) const noexcept { return BlockAt(ToIndex(pos)); }
133
134         const BlockType &Type(const Block &b) const noexcept { return types->Get(b.type); }
135         const BlockType &Type(int index) const noexcept { return Type(BlockAt(index)); }
136
137         void SetLight(int index, int level) noexcept;
138         void SetLight(const ExactLocation::Fine &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
139         void SetLight(const RoughLocation::Fine &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
140
141         int GetLight(int index) const noexcept;
142         int GetLight(const ExactLocation::Fine &pos) const noexcept { return GetLight(ToIndex(pos)); }
143         int GetLight(const RoughLocation::Fine &pos) const noexcept { return GetLight(ToIndex(pos)); }
144
145         float GetVertexLight(const RoughLocation::Fine &, const BlockMesh::Position &, const EntityMesh::Normal &) const noexcept;
146
147         /// get gravity for one unit mass at given point
148         glm::vec3 GravityAt(const ExactLocation &) const noexcept;
149
150         /// check if given ray passes this chunk at all
151         /// given reference indicates the chunk offset of the ray in world space
152         bool Intersection(
153                 const Ray &ray,
154                 const ExactLocation::Coarse &reference,
155                 float &dist
156         ) const noexcept {
157                 return blank::Intersection(ray, RelativeBounds(reference), dist);
158         }
159
160         /// check if given ray intersects any block of this chunk
161         /// given reference indicates the chunk offset of the ray in world space
162         bool Intersection(
163                 const Ray &,
164                 const ExactLocation::Coarse &reference,
165                 WorldCollision &) noexcept;
166
167         /// get all blocks intersecting given box
168         bool Intersection(
169                 const AABB &box,
170                 const glm::mat4 &Mbox,
171                 const glm::mat4 &Mchunk,
172                 std::vector<WorldCollision> &) noexcept;
173
174         bool Intersection(
175                 const Entity &entity,
176                 const glm::mat4 &Mentity,
177                 const glm::mat4 &Mchunk,
178                 std::vector<WorldCollision> &) noexcept;
179
180         void Position(const ExactLocation::Coarse &pos) noexcept { position = pos; }
181         const ExactLocation::Coarse &Position() const noexcept { return position; }
182
183         glm::mat4 Transform(const ExactLocation::Coarse &offset) const noexcept {
184                 return glm::translate(ExactLocation::Fine((position - offset) * ExactLocation::Extent()));
185         }
186
187         void *BlockData() noexcept { return &blocks[0]; }
188         const void *BlockData() const noexcept { return &blocks[0]; }
189         static constexpr std::size_t BlockSize() noexcept { return offsetof(Chunk, position) - offsetof(Chunk, blocks); }
190
191         bool Generated() const noexcept { return generated; }
192         void SetGenerated() noexcept { generated = true; }
193         bool Lighted() const noexcept { return lighted; }
194         void ScanLights();
195
196         /// check for active blocks, should be called after
197         /// block data was modified by means other than SetBlock()
198         void ScanActive();
199
200         void Ref() noexcept { ++ref_count; }
201         void UnRef() noexcept { --ref_count; }
202         bool Referenced() const noexcept { return ref_count > 0; }
203
204         void Invalidate() noexcept { dirty_mesh = dirty_save = true; }
205         void InvalidateMesh() noexcept { dirty_mesh = true; }
206         void ClearMesh() noexcept { dirty_mesh = false; }
207         void ClearSave() noexcept { dirty_save = false; }
208         bool ShouldUpdateMesh() const noexcept { return dirty_mesh; }
209         bool ShouldUpdateSave() const noexcept { return dirty_save; }
210
211         void Update(BlockMesh &) noexcept;
212
213 private:
214         const BlockTypeRegistry *types;
215         Chunk *neighbor[Block::FACE_COUNT];
216
217         std::set<int> gravity;
218
219         Block blocks[size];
220         unsigned char light[size];
221         bool generated;
222         bool lighted;
223
224         ExactLocation::Coarse position;
225         int ref_count;
226         bool dirty_mesh;
227         bool dirty_save;
228
229 };
230
231 }
232
233 #endif