]> git.localhorst.tv Git - blank.git/blob - src/world/Chunk.hpp
some cleaning :)
[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/BlockModel.hpp"
7 #include "../model/geometry.hpp"
8
9 #include <glm/glm.hpp>
10 #include <glm/gtx/transform.hpp>
11
12
13 namespace blank {
14
15 class BlockType;
16
17 /// cube of size 16 (256 tiles, 4096 blocks)
18 class Chunk {
19
20 public:
21         using Pos = glm::tvec3<int>;
22
23 public:
24         explicit Chunk(const BlockTypeRegistry &) noexcept;
25
26         Chunk(Chunk &&) noexcept;
27         Chunk &operator =(Chunk &&) noexcept;
28
29         static constexpr int width = 16;
30         static constexpr int height = 16;
31         static constexpr int depth = 16;
32         static Pos Extent() noexcept { return { width, height, depth }; }
33         static constexpr int size = width * height * depth;
34
35         static AABB Bounds() noexcept { return AABB{ { 0, 0, 0 }, Extent() }; }
36
37         static constexpr bool InBounds(const Block::Pos &pos) noexcept {
38                 return
39                         pos.x >= 0 && pos.x < width &&
40                         pos.y >= 0 && pos.y < height &&
41                         pos.z >= 0 && pos.z < depth;
42         }
43         static constexpr bool InBounds(const Pos &pos) noexcept {
44                 return
45                         pos.x >= 0 && pos.x < width &&
46                         pos.y >= 0 && pos.y < height &&
47                         pos.z >= 0 && pos.z < depth;
48         }
49         static constexpr int ToIndex(const Pos &pos) noexcept {
50                 return pos.x + pos.y * width + pos.z * width * height;
51         }
52         static constexpr bool InBounds(int idx) noexcept {
53                 return idx >= 0 && idx < size;
54         }
55         static Block::Pos ToCoords(int idx) noexcept {
56                 return Block::Pos(
57                         0.5f + (idx % width),
58                         0.5f + ((idx / width) % height),
59                         0.5f + (idx / (width * height))
60                 );
61         }
62         static Block::Pos ToCoords(const Pos &pos) noexcept {
63                 return Block::Pos(pos) + 0.5f;
64         }
65         static Pos ToPos(int idx) noexcept {
66                 return Pos(
67                         (idx % width),
68                         ((idx / width) % height),
69                         (idx / (width * height))
70                 );
71         }
72         glm::mat4 ToTransform(const Pos &pos, int idx) const noexcept;
73
74         static bool IsBorder(const Pos &pos) noexcept {
75                 return
76                         pos.x == 0 ||
77                         pos.x == width - 1 ||
78                         pos.y == 0 ||
79                         pos.y == height - 1 ||
80                         pos.z == 0 ||
81                         pos.z == depth - 1;
82         }
83         static constexpr bool IsBorder(int idx) noexcept {
84                 return
85                         idx < width * height ||                    // low Z plane
86                         idx % width == 0 ||                          // low X plane
87                         (idx / (width * height)) == depth - 1 || // high Z plane
88                         idx % width == width - 1 ||                // high X plane
89                         (idx / width) % height == 0 ||             // low Y plane
90                         (idx / width) % height == height - 1;    // high Y plane
91         }
92
93         bool IsSurface(int index) const noexcept { return IsSurface(ToPos(index)); }
94         bool IsSurface(const Block::Pos &pos) const noexcept { return IsSurface(Pos(pos)); }
95         bool IsSurface(const Pos &pos) const noexcept;
96
97         void SetNeighbor(Chunk &) noexcept;
98         bool HasNeighbor(Block::Face f) const noexcept { return neighbor[f]; }
99         Chunk &GetNeighbor(Block::Face f) noexcept { return *neighbor[f]; }
100         const Chunk &GetNeighbor(Block::Face f) const noexcept { return *neighbor[f]; }
101         void ClearNeighbors() noexcept;
102         void Unlink() noexcept;
103         void Relink() noexcept;
104
105         // check which faces of a block at given index are obstructed (and therefore invisible)
106         Block::FaceSet Obstructed(const Pos &) const noexcept;
107
108         void Invalidate() noexcept { dirty = true; }
109
110         void SetBlock(int index, const Block &) noexcept;
111         void SetBlock(const Block::Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
112         void SetBlock(const Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
113
114         const Block &BlockAt(int index) const noexcept { return blocks[index]; }
115         const Block &BlockAt(const Block::Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
116         const Block &BlockAt(const Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
117
118         const BlockType &Type(const Block &b) const noexcept { return types->Get(b.type); }
119         const BlockType &Type(int index) const noexcept { return Type(BlockAt(index)); }
120
121         void SetLight(int index, int level) noexcept;
122         void SetLight(const Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
123         void SetLight(const Block::Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
124
125         int GetLight(int index) const noexcept;
126         int GetLight(const Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
127         int GetLight(const Block::Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
128
129         float GetVertexLight(const Pos &, const BlockModel::Position &, const Model::Normal &) const noexcept;
130
131         bool Intersection(
132                 const Ray &ray,
133                 const glm::mat4 &M,
134                 float &dist
135         ) const noexcept {
136                 return blank::Intersection(ray, Bounds(), M, &dist);
137         }
138
139         bool Intersection(
140                 const Ray &,
141                 const glm::mat4 &M,
142                 int &blkid,
143                 float &dist,
144                 glm::vec3 &normal) const noexcept;
145
146         void Position(const Pos &pos) noexcept { position = pos; }
147         const Pos &Position() const noexcept { return position; }
148         glm::mat4 Transform(const Pos &offset) const noexcept {
149                 return glm::translate((position - offset) * Extent());
150         }
151
152         void CheckUpdate() noexcept;
153         void Draw() noexcept;
154
155 private:
156         void Update() noexcept;
157
158 private:
159         const BlockTypeRegistry *types;
160         Chunk *neighbor[Block::FACE_COUNT];
161         Block blocks[size];
162         unsigned char light[size];
163         BlockModel model;
164         Pos position;
165         bool dirty;
166
167 };
168
169 }
170
171 #endif