]> git.localhorst.tv Git - blank.git/blob - src/world/Chunk.hpp
a75d8e202ddb3e5544fb0f7cc25e3b44c2a650f5
[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 <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         using Pos = glm::tvec3<int>;
24
25 public:
26         explicit Chunk(const BlockTypeRegistry &) noexcept;
27
28         Chunk(Chunk &&) noexcept;
29         Chunk &operator =(Chunk &&) noexcept;
30
31         static constexpr int width = 16;
32         static constexpr int height = 16;
33         static constexpr int depth = 16;
34         static Pos Extent() noexcept { return { width, height, depth }; }
35         static constexpr int size = width * height * depth;
36
37         static AABB Bounds() noexcept { return AABB{ { 0, 0, 0 }, Extent() }; }
38
39         static constexpr bool InBounds(const Block::Pos &pos) noexcept {
40                 return
41                         pos.x >= 0 && pos.x < width &&
42                         pos.y >= 0 && pos.y < height &&
43                         pos.z >= 0 && pos.z < depth;
44         }
45         static constexpr bool InBounds(const Pos &pos) noexcept {
46                 return
47                         pos.x >= 0 && pos.x < width &&
48                         pos.y >= 0 && pos.y < height &&
49                         pos.z >= 0 && pos.z < depth;
50         }
51         static constexpr int ToIndex(const Pos &pos) noexcept {
52                 return pos.x + pos.y * width + pos.z * width * height;
53         }
54         static constexpr bool InBounds(int idx) noexcept {
55                 return idx >= 0 && idx < size;
56         }
57         static Block::Pos ToCoords(int idx) noexcept {
58                 return Block::Pos(
59                         0.5f + (idx % width),
60                         0.5f + ((idx / width) % height),
61                         0.5f + (idx / (width * height))
62                 );
63         }
64         static Block::Pos ToCoords(const Pos &pos) noexcept {
65                 return Block::Pos(pos) + 0.5f;
66         }
67         static Pos ToPos(int idx) noexcept {
68                 return Pos(
69                         (idx % width),
70                         ((idx / width) % height),
71                         (idx / (width * height))
72                 );
73         }
74         glm::mat4 ToTransform(const Pos &pos, int idx) const noexcept;
75
76         static bool IsBorder(const Pos &pos) noexcept {
77                 return
78                         pos.x == 0 ||
79                         pos.x == width - 1 ||
80                         pos.y == 0 ||
81                         pos.y == height - 1 ||
82                         pos.z == 0 ||
83                         pos.z == depth - 1;
84         }
85         static constexpr bool IsBorder(int idx) noexcept {
86                 return
87                         idx < width * height ||                    // low Z plane
88                         idx % width == 0 ||                          // low X plane
89                         (idx / (width * height)) == depth - 1 || // high Z plane
90                         idx % width == width - 1 ||                // high X plane
91                         (idx / width) % height == 0 ||             // low Y plane
92                         (idx / width) % height == height - 1;    // high Y plane
93         }
94
95         bool IsSurface(int index) const noexcept { return IsSurface(ToPos(index)); }
96         bool IsSurface(const Block::Pos &pos) const noexcept { return IsSurface(Pos(pos)); }
97         bool IsSurface(const Pos &pos) const noexcept;
98
99         void SetNeighbor(Chunk &) noexcept;
100         bool HasNeighbor(Block::Face f) const noexcept { return neighbor[f]; }
101         Chunk &GetNeighbor(Block::Face f) noexcept { return *neighbor[f]; }
102         const Chunk &GetNeighbor(Block::Face f) const noexcept { return *neighbor[f]; }
103         void ClearNeighbors() noexcept;
104         void Unlink() noexcept;
105
106         // check which faces of a block at given index are obstructed (and therefore invisible)
107         Block::FaceSet Obstructed(const Pos &) const noexcept;
108
109         void Invalidate() noexcept { dirty = true; }
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 Model::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                 int &blkid,
144                 float &dist,
145                 glm::vec3 &normal) const noexcept;
146
147         bool Intersection(
148                 const AABB &box,
149                 const glm::mat4 &Mbox,
150                 const glm::mat4 &Mchunk,
151                 std::vector<WorldCollision> &) const noexcept;
152
153         void Position(const Pos &pos) noexcept { position = pos; }
154         const Pos &Position() const noexcept { return position; }
155         glm::mat4 Transform(const Pos &offset) const noexcept {
156                 return glm::translate((position - offset) * Extent());
157         }
158
159         void CheckUpdate() noexcept;
160         void Draw() noexcept;
161
162 private:
163         void Update() noexcept;
164
165 private:
166         const BlockTypeRegistry *types;
167         Chunk *neighbor[Block::FACE_COUNT];
168         Block blocks[size];
169         unsigned char light[size];
170         BlockModel model;
171         Pos position;
172         bool dirty;
173
174 };
175
176 }
177
178 #endif