]> git.localhorst.tv Git - blank.git/blob - src/world/Chunk.hpp
collect collisions for each entity
[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         void Relink() noexcept;
106
107         // check which faces of a block at given index are obstructed (and therefore invisible)
108         Block::FaceSet Obstructed(const Pos &) const noexcept;
109
110         void Invalidate() noexcept { dirty = true; }
111
112         void SetBlock(int index, const Block &) noexcept;
113         void SetBlock(const Block::Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
114         void SetBlock(const Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
115
116         const Block &BlockAt(int index) const noexcept { return blocks[index]; }
117         const Block &BlockAt(const Block::Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
118         const Block &BlockAt(const Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
119
120         const BlockType &Type(const Block &b) const noexcept { return types->Get(b.type); }
121         const BlockType &Type(int index) const noexcept { return Type(BlockAt(index)); }
122
123         void SetLight(int index, int level) noexcept;
124         void SetLight(const Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
125         void SetLight(const Block::Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
126
127         int GetLight(int index) const noexcept;
128         int GetLight(const Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
129         int GetLight(const Block::Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
130
131         float GetVertexLight(const Pos &, const BlockModel::Position &, const Model::Normal &) const noexcept;
132
133         bool Intersection(
134                 const Ray &ray,
135                 const glm::mat4 &M,
136                 float &dist
137         ) const noexcept {
138                 return blank::Intersection(ray, Bounds(), M, &dist);
139         }
140
141         bool Intersection(
142                 const Ray &,
143                 const glm::mat4 &M,
144                 int &blkid,
145                 float &dist,
146                 glm::vec3 &normal) const noexcept;
147
148         bool Intersection(
149                 const AABB &box,
150                 const glm::mat4 &Mbox,
151                 const glm::mat4 &Mchunk,
152                 std::vector<WorldCollision> &) const noexcept;
153
154         void Position(const Pos &pos) noexcept { position = pos; }
155         const Pos &Position() const noexcept { return position; }
156         glm::mat4 Transform(const Pos &offset) const noexcept {
157                 return glm::translate((position - offset) * Extent());
158         }
159
160         void CheckUpdate() noexcept;
161         void Draw() noexcept;
162
163 private:
164         void Update() noexcept;
165
166 private:
167         const BlockTypeRegistry *types;
168         Chunk *neighbor[Block::FACE_COUNT];
169         Block blocks[size];
170         unsigned char light[size];
171         BlockModel model;
172         Pos position;
173         bool dirty;
174
175 };
176
177 }
178
179 #endif