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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
#pragma once
#include <cstring>
#include "ChunkDef.h"
#define CHUNK_SECTION_HEIGHT 16
#define CHUNK_SECTION_NUM (256 / CHUNK_SECTION_HEIGHT)
#if __cplusplus < 201103L
// auto_ptr style interface for memory management
#else
// unique_ptr style interface for memory management
#endif
class cChunkBuffer
{
public:
cChunkBuffer()
#if __cplusplus < 201103L
// auto_ptr style interface for memory management
: IsOwner(true)
#endif
{
memset(m_Sections, 0, sizeof(m_Sections));
}
~cChunkBuffer()
{
#if __cplusplus < 201103L
// auto_ptr style interface for memory management
if(!IsOwner) return;
#endif
for (int i = 0; i < CHUNK_SECTION_NUM; i++)
{
if(m_Sections[i]) Free(m_Sections[i]);;
}
}
#if __cplusplus < 201103L
// auto_ptr style interface for memory management
cChunkBuffer(const cChunkBuffer& other) :
IsOwner(true)
{
for (int i = 0; i < CHUNK_SECTION_NUM; i++)
{
m_Sections[i] = other.m_Sections[i];
}
other.IsOwner = false;
}
cChunkBuffer& operator=(const cChunkBuffer& other)
{
if(&other == this)
{
if(IsOwner)
{
for (int i = 0; i < CHUNK_SECTION_NUM; i++)
{
if(m_Sections[i]) Free(m_Sections[i]);;
}
}
IsOwner = true;
for (int i = 0; i < CHUNK_SECTION_NUM; i++)
{
m_Sections[i] = other.m_Sections[i];
}
other.IsOwner = false;
}
}
#else
// unique_ptr style interface for memory management
cChunkBuffer(const cChunkBuffer&& other)
{
for (int i = 0; i < CHUNK_SECTION_NUM; i++)
{
m_Sections[i] = other.m_Sections[i];
}
}
cChunkBuffer& operator=(const cChunkBuffer&& other)
{
for (int i = 0; i < CHUNK_SECTION_NUM; i++)
{
if(m_Sections[i]) Free(m_Sections[i]);;
m_Sections[i] = other.m_Sections[i];
}
}
#endif
BLOCKTYPE GetBlock(int a_X, int a_Y, int a_Z) const
{
ASSERT((a_X >= 0) && (a_X < cChunkDef::Width));
ASSERT((a_Y >= 0) && (a_Y < cChunkDef::Height));
ASSERT((a_Z >= 0) && (a_Z < cChunkDef::Width));
int Section = a_Y / CHUNK_SECTION_HEIGHT;
if(m_Sections[Section])
{
int Index = cChunkDef::MakeIndexNoCheck(a_X, a_Y - (Section * CHUNK_SECTION_HEIGHT), a_Z);
return m_Sections[Section]->m_BlockTypes[Index];
}
else
{
return 0;
}
}
void SetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_Block)
{
if (
(a_RelX >= cChunkDef::Width) || (a_RelX < 0) ||
(a_RelY >= cChunkDef::Height) || (a_RelY < 0) ||
(a_RelZ >= cChunkDef::Width) || (a_RelZ < 0)
)
{
ASSERT(!"cChunkBuffer::SetMeta(): index out of range!");
return;
}
int Section = a_RelY / CHUNK_SECTION_HEIGHT;
if(!m_Sections[Section])
{
if(a_Block == 0x00)
{
return;
}
m_Sections[Section] = Allocate();
if(!m_Sections[Section])
{
ASSERT(!"Failed to allocate a new section in Chunkbuffer");
return;
}
ZeroSection(m_Sections[Section]);
}
int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ);
m_Sections[Section]->m_BlockTypes[Index] = a_Block;
}
NIBBLETYPE GetMeta(int a_RelX, int a_RelY, int a_RelZ) const
{
if ((a_RelX < cChunkDef::Width) && (a_RelX > -1) && (a_RelY < cChunkDef::Height) && (a_RelY > -1) && (a_RelZ < cChunkDef::Width) && (a_RelZ > -1))
{
int Section = a_RelY / CHUNK_SECTION_HEIGHT;
if(m_Sections[Section])
{
int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ);
return (m_Sections[Section]->m_BlockMeta[Index / 2] >> ((Index & 1) * 4)) & 0x0f;
}
else
{
return 0;
}
}
ASSERT(!"cChunkBuffer::GetMeta(): coords out of chunk range!");
return 0;
}
void SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Nibble)
{
if (
(a_RelX >= cChunkDef::Width) || (a_RelX < 0) ||
(a_RelY >= cChunkDef::Height) || (a_RelY < 0) ||
(a_RelZ >= cChunkDef::Width) || (a_RelZ < 0)
)
{
ASSERT(!"cChunkBuffer::SetMeta(): index out of range!");
return;
}
int Section = a_RelY / CHUNK_SECTION_HEIGHT;
if(!m_Sections[Section])
{
if((a_Nibble & 0xf) == 0x00)
{
return;
}
m_Sections[Section] = Allocate();
if(!m_Sections[Section])
{
ASSERT(!"Failed to allocate a new section in Chunkbuffer");
return;
}
ZeroSection(m_Sections[Section]);
}
int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ);
m_Sections[Section]->m_BlockMeta[Index / 2] = static_cast<NIBBLETYPE>(
(m_Sections[Section]->m_BlockMeta[Index / 2] & (0xf0 >> ((Index & 1) * 4))) | // The untouched nibble
((a_Nibble & 0x0f) << ((Index & 1) * 4)) // The nibble being set
);
}
NIBBLETYPE GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const
{
if ((a_RelX < cChunkDef::Width) && (a_RelX > -1) && (a_RelY < cChunkDef::Height) && (a_RelY > -1) && (a_RelZ < cChunkDef::Width) && (a_RelZ > -1))
{
int Section = a_RelY / CHUNK_SECTION_HEIGHT;
if(m_Sections[Section])
{
int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ);
return (m_Sections[Section]->m_BlockLight[Index / 2] >> ((Index & 1) * 4)) & 0x0f;
}
else
{
return 0;
}
}
ASSERT(!"cChunkBuffer::GetMeta(): coords out of chunk range!");
return 0;
}
NIBBLETYPE GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const
{
if ((a_RelX < cChunkDef::Width) && (a_RelX > -1) && (a_RelY < cChunkDef::Height) && (a_RelY > -1) && (a_RelZ < cChunkDef::Width) && (a_RelZ > -1))
{
int Section = a_RelY / CHUNK_SECTION_HEIGHT;
if(m_Sections[Section])
{
int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ);
return (m_Sections[Section]->m_BlockLight[Index / 2] >> ((Index & 1) * 4)) & 0x0f;
}
else
{
return 0xFF;
}
}
ASSERT(!"cChunkBuffer::GetMeta(): coords out of chunk range!");
return 0;
}
cChunkBuffer Copy() const;
void CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx = 0, size_t length = cChunkDef::NumBlocks) const;
void CopyMeta (NIBBLETYPE * a_dest) const;
void CopyLight (NIBBLETYPE * a_dest) const;
void CopySkyLight (NIBBLETYPE * a_dest) const;
void SetBlocks (const BLOCKTYPE * a_src);
void SetMeta (const NIBBLETYPE * a_src);
void SetLight (const NIBBLETYPE * a_src);
void SetSkyLight (const NIBBLETYPE * a_src);
private:
#if __cplusplus < 201103L
// auto_ptr style interface for memory management
mutable bool IsOwner;
#endif
struct sChunkSection {
BLOCKTYPE m_BlockTypes [CHUNK_SECTION_HEIGHT * 16 * 16] ;
NIBBLETYPE m_BlockMeta [CHUNK_SECTION_HEIGHT * 16 * 16 / 2];
NIBBLETYPE m_BlockLight [CHUNK_SECTION_HEIGHT * 16 * 16 / 2];
NIBBLETYPE m_BlockSkyLight[CHUNK_SECTION_HEIGHT * 16 * 16 / 2];
};
sChunkSection *m_Sections[CHUNK_SECTION_NUM];
sChunkSection * Allocate() const;
void Free(sChunkSection * ptr) const;
void ZeroSection(sChunkSection * ptr) const;
};
/** A simple implementation of the cChunkDataCallback interface that collects all block data into a buffer
*/
class cChunkBufferCollector :
public cChunkDataCallback
{
public:
cChunkBuffer m_BlockData;
protected:
virtual void ChunkBuffer(const cChunkBuffer & a_BlockData) override
{
m_BlockData = a_BlockData.Copy();
}
};
/** A simple implementation of the cChunkDataCallback interface that collects all block data into a single buffer
*/
class cChunkDataCollector :
public cChunkDataCallback
{
public:
// Must be unsigned char instead of BLOCKTYPE or NIBBLETYPE, because it houses both.
unsigned char m_BlockData[cChunkDef::BlockDataSize];
protected:
virtual void ChunkBuffer(const cChunkBuffer & a_ChunkBuffer) override
{
a_ChunkBuffer.CopyBlocks(m_BlockData);
a_ChunkBuffer.CopyMeta(m_BlockData + cChunkDef::NumBlocks);
a_ChunkBuffer.CopyLight(m_BlockData + 3 * cChunkDef::NumBlocks / 2);
a_ChunkBuffer.CopySkyLight(m_BlockData + 2 * cChunkDef::NumBlocks);
}
};
/** A simple implementation of the cChunkDataCallback interface that collects all block data into a separate buffers
*/
class cChunkDataSeparateCollector :
public cChunkDataCallback
{
public:
cChunkDef::BlockTypes m_BlockTypes;
cChunkDef::BlockNibbles m_BlockMetas;
cChunkDef::BlockNibbles m_BlockLight;
cChunkDef::BlockNibbles m_BlockSkyLight;
protected:
virtual void ChunkBuffer(const cChunkBuffer & a_ChunkBuffer) override
{
a_ChunkBuffer.CopyBlocks(m_BlockTypes);
a_ChunkBuffer.CopyMeta(m_BlockMetas);
a_ChunkBuffer.CopyLight(m_BlockLight);
a_ChunkBuffer.CopySkyLight(m_BlockSkyLight);
}
} ;
|