]> git.localhorst.tv Git - blank.git/blob - src/world/Chunk.hpp
some code reorganization
[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 constexpr bool IsBorder(int idx) noexcept {
75                 return
76                         idx < width * height ||                    // low Z plane
77                         idx % width == 0 ||                          // low X plane
78                         (idx / (width * height)) == depth - 1 || // high Z plane
79                         idx % width == width - 1 ||                // high X plane
80                         (idx / width) % height == 0 ||             // low Y plane
81                         (idx / width) % height == height - 1;    // high Y plane
82         }
83
84         bool IsSurface(int index) const noexcept { return IsSurface(ToPos(index)); }
85         bool IsSurface(const Block::Pos &pos) const noexcept { return IsSurface(Pos(pos)); }
86         bool IsSurface(const Pos &pos) const noexcept;
87
88         void SetNeighbor(Chunk &) noexcept;
89         bool HasNeighbor(Block::Face f) const noexcept { return neighbor[f]; }
90         Chunk &GetNeighbor(Block::Face f) noexcept { return *neighbor[f]; }
91         const Chunk &GetNeighbor(Block::Face f) const noexcept { return *neighbor[f]; }
92         void ClearNeighbors() noexcept;
93         void Unlink() noexcept;
94         void Relink() noexcept;
95
96         // check which faces of a block at given index are obstructed (and therefore invisible)
97         Block::FaceSet Obstructed(const Pos &) const noexcept;
98
99         void Invalidate() noexcept { dirty = true; }
100
101         void SetBlock(int index, const Block &) noexcept;
102         void SetBlock(const Block::Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
103         void SetBlock(const Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
104
105         const Block &BlockAt(int index) const noexcept { return blocks[index]; }
106         const Block &BlockAt(const Block::Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
107         const Block &BlockAt(const Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
108
109         const BlockType &Type(const Block &b) const noexcept { return types->Get(b.type); }
110         const BlockType &Type(int index) const noexcept { return Type(BlockAt(index)); }
111
112         void SetLight(int index, int level) noexcept;
113         void SetLight(const Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
114         void SetLight(const Block::Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
115
116         int GetLight(int index) const noexcept;
117         int GetLight(const Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
118         int GetLight(const Block::Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
119
120         float GetVertexLight(const Pos &, const BlockModel::Position &, const Model::Normal &) const noexcept;
121
122         bool Intersection(
123                 const Ray &ray,
124                 const glm::mat4 &M,
125                 float &dist
126         ) const noexcept {
127                 return blank::Intersection(ray, Bounds(), M, &dist);
128         }
129
130         bool Intersection(
131                 const Ray &,
132                 const glm::mat4 &M,
133                 int &blkid,
134                 float &dist,
135                 glm::vec3 &normal) const noexcept;
136
137         void Position(const Pos &pos) noexcept { position = pos; }
138         const Pos &Position() const noexcept { return position; }
139         glm::mat4 Transform(const Pos &offset) const noexcept {
140                 return glm::translate((position - offset) * Extent());
141         }
142
143         void CheckUpdate() noexcept;
144         void Draw() noexcept;
145
146 private:
147         void Update() noexcept;
148
149 private:
150         const BlockTypeRegistry *types;
151         Chunk *neighbor[Block::FACE_COUNT];
152         Block blocks[16 * 16 * 16];
153         unsigned char light[16 * 16 * 16];
154         BlockModel model;
155         Pos position;
156         bool dirty;
157
158 };
159
160 }
161
162 #endif