1 #ifndef BLANK_WORLD_CHUNK_HPP_
2 #define BLANK_WORLD_CHUNK_HPP_
5 #include "BlockTypeRegistry.hpp"
6 #include "../model/geometry.hpp"
10 #include <glm/gtx/transform.hpp>
18 /// cube of size 16 (256 tiles, 4096 blocks)
22 using Pos = glm::ivec3;
25 explicit Chunk(const BlockTypeRegistry &) noexcept;
27 Chunk(Chunk &&) noexcept;
28 Chunk &operator =(Chunk &&) noexcept;
30 static constexpr int width = 16;
31 static constexpr int height = 16;
32 static constexpr int depth = 16;
33 static Pos Extent() noexcept { return { width, height, depth }; }
34 static constexpr int size = width * height * depth;
36 static AABB Bounds() noexcept { return AABB{ { 0, 0, 0 }, Extent() }; }
38 static constexpr bool InBounds(const Block::Pos &pos) noexcept {
40 pos.x >= 0 && pos.x < width &&
41 pos.y >= 0 && pos.y < height &&
42 pos.z >= 0 && pos.z < depth;
44 static constexpr bool InBounds(const Pos &pos) noexcept {
46 pos.x >= 0 && pos.x < width &&
47 pos.y >= 0 && pos.y < height &&
48 pos.z >= 0 && pos.z < depth;
50 static constexpr int ToIndex(const Pos &pos) noexcept {
51 return pos.x + pos.y * width + pos.z * width * height;
53 static constexpr bool InBounds(int idx) noexcept {
54 return idx >= 0 && idx < size;
56 static Block::Pos ToCoords(int idx) noexcept {
59 0.5f + ((idx / width) % height),
60 0.5f + (idx / (width * height))
63 static Block::Pos ToCoords(const Pos &pos) noexcept {
64 return Block::Pos(pos) + 0.5f;
66 static Pos ToPos(int idx) noexcept {
69 ((idx / width) % height),
70 (idx / (width * height))
73 glm::mat4 ToTransform(const Pos &pos, int idx) const noexcept;
75 Block::Pos ToSceneCoords(const Pos &base, const Block::Pos &pos) const noexcept {
76 return Block::Pos((position - base) * Extent()) + pos;
79 static bool IsBorder(const Pos &pos) noexcept {
84 pos.y == height - 1 ||
88 static constexpr bool IsBorder(int idx) noexcept {
90 idx < width * height || // low Z plane
91 idx % width == 0 || // low X plane
92 (idx / (width * height)) == depth - 1 || // high Z plane
93 idx % width == width - 1 || // high X plane
94 (idx / width) % height == 0 || // low Y plane
95 (idx / width) % height == height - 1; // high Y plane
98 bool IsSurface(int index) const noexcept { return IsSurface(ToPos(index)); }
99 bool IsSurface(const Block::Pos &pos) const noexcept { return IsSurface(Pos(pos)); }
100 bool IsSurface(const Pos &pos) const noexcept;
102 void SetNeighbor(Chunk &) noexcept;
103 bool HasNeighbor(Block::Face f) const noexcept { return neighbor[f]; }
104 Chunk &GetNeighbor(Block::Face f) noexcept { return *neighbor[f]; }
105 const Chunk &GetNeighbor(Block::Face f) const noexcept { return *neighbor[f]; }
106 void ClearNeighbors() noexcept;
107 void Unlink() noexcept;
109 // check which faces of a block at given index are obstructed (and therefore invisible)
110 Block::FaceSet Obstructed(const Pos &) const noexcept;
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); }
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)); }
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)); }
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); }
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)); }
131 float GetVertexLight(const Pos &, const BlockModel::Position &, const EntityModel::Normal &) const noexcept;
138 return blank::Intersection(ray, Bounds(), M, &dist);
144 WorldCollision &) noexcept;
148 const glm::mat4 &Mbox,
149 const glm::mat4 &Mchunk,
150 std::vector<WorldCollision> &) noexcept;
152 void Position(const Pos &pos) noexcept { position = pos; }
153 const Pos &Position() const noexcept { return position; }
154 glm::mat4 Transform(const Pos &offset) const noexcept {
155 return glm::translate((position - offset) * Extent());
158 void *BlockData() noexcept { return &blocks[0]; }
159 const void *BlockData() const noexcept { return &blocks[0]; }
160 static constexpr std::size_t BlockSize() noexcept { return sizeof(blocks) + sizeof(light); }
162 void Invalidate() noexcept { dirty_model = dirty_save = true; }
163 void InvalidateModel() noexcept { dirty_model = true; }
164 void ClearModel() noexcept { dirty_model = false; }
165 void ClearSave() noexcept { dirty_save = false; }
166 bool ShouldUpdateModel() const noexcept { return dirty_model; }
167 bool ShouldUpdateSave() const noexcept { return dirty_save; }
169 void Update(BlockModel &) noexcept;
172 const BlockTypeRegistry *types;
173 Chunk *neighbor[Block::FACE_COUNT];
175 unsigned char light[size];