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