1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
// ChunkData.cpp
// Implements the cChunkData class that represents the block's type, meta, blocklight and skylight storage for a chunk
#include "Globals.h"
#include "ChunkData.h"
#include "BlockType.h"
namespace
{
struct SectionIndices
{
size_t Section = 0; // Index into m_Sections
size_t Index = 0; // Index into a single sChunkSection
};
inline SectionIndices IndicesFromRelPos(const Vector3i a_RelPos)
{
ASSERT(cChunkDef::IsValidRelPos(a_RelPos));
return {
static_cast<size_t>(a_RelPos.y / cChunkDef::SectionHeight),
cChunkDef::MakeIndex(a_RelPos.x, a_RelPos.y % cChunkDef::SectionHeight, a_RelPos.z)
};
}
bool IsCompressed(const size_t ElementCount)
{
return ElementCount != ChunkBlockData::SectionBlockCount;
}
template <size_t ElementCount, typename ValueType> ValueType UnpackDefaultValue(const ValueType DefaultValue)
{
if (IsCompressed(ElementCount))
{
return DefaultValue & 0xF;
}
return DefaultValue;
}
} // namespace
template <class ElementType, size_t ElementCount, ElementType DefaultValue>
void ChunkDataStore<ElementType, ElementCount, DefaultValue>::Assign(
const ChunkDataStore<ElementType, ElementCount, DefaultValue> & a_Other
)
{
for (size_t Y = 0; Y != cChunkDef::NumSections; Y++)
{
Store[Y].reset();
if (const auto & Other = a_Other.Store[Y]; Other != nullptr)
{
Store[Y] = std::make_unique<Type>(*Other);
}
}
}
template <class ElementType, size_t ElementCount, ElementType DefaultValue>
ElementType ChunkDataStore<ElementType, ElementCount, DefaultValue>::Get(const Vector3i a_Position) const
{
const auto Indices = IndicesFromRelPos(a_Position);
const auto & Section = Store[Indices.Section];
if (Section != nullptr)
{
if (IsCompressed(ElementCount))
{
return cChunkDef::ExpandNibble(Section->data(), Indices.Index);
}
else
{
return (*Section)[Indices.Index];
}
}
return UnpackDefaultValue<ElementCount>(DefaultValue);
}
template <class ElementType, size_t ElementCount, ElementType DefaultValue>
typename ChunkDataStore<ElementType, ElementCount, DefaultValue>::Type * ChunkDataStore<
ElementType,
ElementCount,
DefaultValue>::GetSection(const size_t a_Y) const
{
return Store[a_Y].get();
}
template <class ElementType, size_t ElementCount, ElementType DefaultValue>
void ChunkDataStore<ElementType, ElementCount, DefaultValue>::Set(const Vector3i a_Position, const ElementType a_Value)
{
const auto Indices = IndicesFromRelPos(a_Position);
auto & Section = Store[Indices.Section];
if (Section == nullptr)
{
if (a_Value == UnpackDefaultValue<ElementCount>(DefaultValue))
{
return;
}
Section = cpp20::make_unique_for_overwrite<Type>();
std::fill(Section->begin(), Section->end(), DefaultValue);
}
if (IsCompressed(ElementCount))
{
cChunkDef::PackNibble(Section->data(), Indices.Index, a_Value);
}
else
{
(*Section)[Indices.Index] = a_Value;
}
}
template <class ElementType, size_t ElementCount, ElementType DefaultValue>
void ChunkDataStore<ElementType, ElementCount, DefaultValue>::SetSection(
const ElementType (&a_Source)[ElementCount],
const size_t a_Y
)
{
auto & Section = Store[a_Y];
const auto SourceEnd = std::end(a_Source);
if (Section != nullptr)
{
std::copy(a_Source, SourceEnd, Section->begin());
}
else if (std::any_of(a_Source, SourceEnd, [](const auto Value) { return Value != DefaultValue; }))
{
Section = cpp20::make_unique_for_overwrite<Type>();
std::copy(a_Source, SourceEnd, Section->begin());
}
}
template <class ElementType, size_t ElementCount, ElementType DefaultValue>
void ChunkDataStore<ElementType, ElementCount, DefaultValue>::SetAll(const ElementType (&a_Source
)[cChunkDef::NumSections * ElementCount])
{
for (size_t Y = 0; Y != cChunkDef::NumSections; Y++)
{
SetSection(*reinterpret_cast<const ElementType(*)[ElementCount]>(a_Source + Y * ElementCount), Y);
}
}
void ChunkBlockData::Assign(const ChunkBlockData & a_Other)
{
m_Blocks.Assign(a_Other.m_Blocks);
m_Metas.Assign(a_Other.m_Metas);
}
void ChunkBlockData::SetAll(const cChunkDef::BlockTypes & a_BlockSource, const cChunkDef::BlockNibbles & a_MetaSource)
{
m_Blocks.SetAll(a_BlockSource);
m_Metas.SetAll(a_MetaSource);
}
void ChunkBlockData::SetSection(
const SectionType & a_BlockSource,
const SectionMetaType & a_MetaSource,
const size_t a_Y
)
{
m_Blocks.SetSection(a_BlockSource, a_Y);
m_Metas.SetSection(a_MetaSource, a_Y);
}
void ChunkLightData::Assign(const ChunkLightData & a_Other)
{
m_BlockLights.Assign(a_Other.m_BlockLights);
m_SkyLights.Assign(a_Other.m_SkyLights);
}
void ChunkLightData::SetAll(
const cChunkDef::BlockNibbles & a_BlockLightSource,
const cChunkDef::BlockNibbles & a_SkyLightSource
)
{
m_BlockLights.SetAll(a_BlockLightSource);
m_SkyLights.SetAll(a_SkyLightSource);
}
void ChunkLightData::SetSection(
const SectionType & a_BlockLightSource,
const SectionType & a_SkyLightSource,
const size_t a_Y
)
{
m_BlockLights.SetSection(a_BlockLightSource, a_Y);
m_SkyLights.SetSection(a_SkyLightSource, a_Y);
}
template struct ChunkDataStore<BLOCKTYPE, ChunkBlockData::SectionBlockCount, ChunkBlockData::DefaultValue>;
template struct ChunkDataStore<NIBBLETYPE, ChunkBlockData::SectionMetaCount, ChunkLightData::DefaultBlockLightValue>;
template struct ChunkDataStore<NIBBLETYPE, ChunkLightData::SectionLightCount, ChunkLightData::DefaultSkyLightValue>;
|