-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChunk.cs
461 lines (398 loc) · 14.3 KB
/
Chunk.cs
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
using fNbt;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace AnvilParser
{
public class Chunk
{
public Chunk(NbtCompound nbtData)
{
if (nbtData.Contains("DataVersion"))
{
Version = nbtData.Get<NbtInt>("DataVersion").Value;
}
else
{
// Version is pre-1.9 snapshot 15w32a, so world does not have a Data Version.
// See https://minecraft.wiki/w/Data_version
Version = Versions.VERSION_PRE_15w32a;
}
if (Version >= Versions.VERSION_21w43a)
{
Data = nbtData;
TileEntities = Data.Get<NbtList>("block_entities");
}
else
{
Data = nbtData.Get<NbtCompound>("Level");
TileEntities = Data.Get<NbtList>("TileEntities");
}
X = Data.Get<NbtInt>("xPos").Value;
Z = Data.Get<NbtInt>("zPos").Value;
}
public static Chunk? FromRegion(Region region, int chunkX, int chunkZ)
{
NbtFile? nbtFile = region.ChunkData(chunkX, chunkZ);
if (nbtFile == null)
{
return null;
}
return new Chunk(nbtFile.RootTag);
}
public int Version { get; set; }
public NbtCompound Data { get; set; }
public NbtList TileEntities { get; set; }
public int X { get; set; }
public int Z { get; set; }
public BaseBlock GetBlock(int X, int Y, int Z)
{
if (X < 0 || X > 15)
{
throw new Exception($"X ({X}) must be in range of 0 to 15");
}
if (Z < 0 || Z > 15)
{
throw new Exception($"Z ({Z}) must be in range of 0 to 15");
}
int sectionRangeStart, sectionRangeStop;
if (Version > Versions.VERSION_17w47a)
{
sectionRangeStart = -4;
sectionRangeStop = 20;
}
else
{
sectionRangeStart = 0;
sectionRangeStop = 16;
}
if (Y / 16 < sectionRangeStart || Y / 16 > sectionRangeStop)
{
throw new Exception($"Y ({Y}) must be in range of {sectionRangeStart * 16} to {sectionRangeStop * 16 - 1}");
}
NbtCompound? section = GetSection(Y / 16);
Y %= 16;
// Explained in depth here https://minecraft.wiki/w/index.php?title=Chunk_format&oldid=1153403#Block_format
if (Version < Versions.VERSION_17w47a)
{
if (section == null || !section.Contains("Blocks"))
{
return new OldBlock(0);
}
int inDex = Y * 16 * 16 + Z * 16 + X;
int block_id = section.Get<NbtIntArray>("Blocks")[inDex];
if (section.Contains("Add"))
{
block_id += Nibble(section.Get<NbtByteArray>("Add").Value, inDex) << 8;
}
int block_data = Nibble(section.Get<NbtByteArray>("Data").Value, inDex);
BaseBlock block = new OldBlock(block_id, block_data);
return block;
}
// If its an empty section its most likely an air block
if (section == null)
{
return Block.FromName("minecraft:air");
}
long[] states;
try
{
states = StatesFromSection(section);
}
catch
{
return Block.FromName("minecraft:air");
}
// Number of bits each block is on BlockStates
// Cannot be lower than 4
NbtList palette = PaletteFromSection(section);
int bits = Math.Max(ByteHelper.BitCount(palette.Count - 1), 4);
// Get index on the block list with the order YZX
int index = Y * 16 * 16 + Z * 16 + X;
// in 20w17a and newer blocks cannot occupy more than one element on the BlockStates array
bool stretches = Version < Versions.VERSION_20w17a;
int state;
if (stretches)
{
state = index * bits / 64;
}
{
state = index / (64 / bits);
}
long data = states[state];
long shifted_data;
if (stretches)
{
// shift the number to the right to remove the left over bits
// and shift so the i'th block is the first one
shifted_data = data >> ((bits * index) % 64);
}
else
{
shifted_data = data >> (index % (64 / bits) * bits);
}
// if there aren't enough bits it means the rest are in the next number
if (stretches && (64 - ((bits * index) % 64 ) < bits))
{
data = states[state + 1];
// get how many bits are from a palette index of the next block
int leftover = (bits - ((state + 1) * 64 % bits)) % bits;
// Make sure to keep the length of the bits in the first state
// Example: bits is 5, and leftover is 3
// Next state Current state (already shifted)
// 0b101010110101101010010 0b01
// will result in bin_append(0b010, 0b01, 2) = 0b01001
shifted_data = BinAppend(data & (long)(Math.Pow(2, leftover) - 1), shifted_data, bits - leftover);
}
// get `bits` least significant bits
// which are the palette index
int paletteId = (int)(shifted_data & (int)(Math.Pow(2, bits) - 1));
return Block.FromPalette((NbtCompound)palette[paletteId]);
}
public IEnumerable<BaseBlock> StreamBlocks()
{
NbtCompound? section;
int index;
BaseBlock airBlock;
if (Version < Versions.VERSION_17w47a)
{
airBlock = new OldBlock(0);
}
else
{
airBlock = Block.FromName("minecraft:air");
}
Tuple<int, int> sectionHeight = SectionHeightRange(Version);
for (int sectionIndex = sectionHeight.Item1; sectionIndex < sectionHeight.Item2; sectionIndex++)
{
section = GetSection(sectionIndex);
index = 0;
if (Version < Versions.VERSION_17w47a)
{
if (section == null || !section.Contains("Blocks"))
{
for (int i = 0; i < 4096; i++)
{
yield return airBlock;
}
continue;
}
while (index < 4096)
{
int block_id = (int)ByteHelper.ToUInt8(section.Get<NbtByteArray>("Blocks").Value, index);
if (section.Contains("Add"))
{
block_id += Nibble(section.Get<NbtByteArray>("Add").Value, index) << 8;
}
int block_data = Nibble(section.Get<NbtByteArray>("Data").Value, index);
BaseBlock block = new OldBlock(block_id, block_data);
yield return block;
index++;
}
continue;
}
if (section == null)
{
for (int i = 0; i < 4096; i++)
{
yield return airBlock;
}
continue;
}
long[]? states = null;
try
{
states = StatesFromSection(section);
}
catch { }
if (states == null)
{
for (int i = 0; i < 4096; i++)
{
yield return airBlock;
}
continue;
}
NbtList palette = PaletteFromSection(section);
int bits = Math.Max(ByteHelper.BitCount(palette.Count - 1), 4);
bool stretches = Version < Versions.VERSION_20w17a;
int state;
if (stretches)
{
state = index * bits / 64;
}
else
{
state = index / (64 / bits);
}
long data = states[state];
uint bitsMask = (uint)(Math.Pow(2, bits) - 1);
int offset;
if (stretches)
{
offset = (bits * index) % 64;
}
else
{
offset = index % (64 / bits) * bits;
}
int dataLen = 64 - offset;
data >>= offset;
long newData;
while (index < 4096)
{
if (dataLen < bits)
{
state += 1;
newData = states[state];
if (stretches)
{
int leftover = dataLen;
dataLen += 64;
data = BinAppend(newData, data, leftover);
}
else
{
data = newData;
dataLen = 64;
}
}
int paletteId = (int)(data & bitsMask);
yield return Block.FromPalette((NbtCompound)palette[paletteId]);
index++;
data >>= bits;
dataLen -= bits;
}
}
}
private NbtCompound? GetSection(int Y)
{
int sectionRangeStart, sectionRangeStop;
if (Version > Versions.VERSION_17w47a)
{
sectionRangeStart = -4;
sectionRangeStop = 20;
}
else
{
sectionRangeStart = 0;
sectionRangeStop = 16;
}
if (Y / 16 < sectionRangeStart || Y / 16 > sectionRangeStop)
{
throw new Exception($"Y ({Y}) must be in range of {sectionRangeStart * 16} to {sectionRangeStop * 16 - 1}");
}
NbtList sections;
if (Data.Contains("sections"))
{
sections = Data.Get<NbtList>("sections");
}
else if (Data.Contains("Sections"))
{
sections = Data.Get<NbtList>("Sections");
}
else
{
return null;
}
foreach (NbtCompound section in sections)
{
if (section.Get<NbtByte>("Y").Value == Y)
{
return section;
}
}
return null;
}
private static int Nibble(byte[] data, int index)
{
byte value = data[index / 2];
if (index % 2 != 0)
{
return value >> 4;
}
else
{
return value & 0b1111;
}
}
//Appends number a to the left of b
//bin_append(0b1, 0b10) = 0b110
private static int BinAppend(int a, int b)
{
int length = ByteHelper.BitCount(b);
return (a << length) | b;
}
private static long BinAppend(long a, long b)
{
int length = ByteHelper.BitCount((int)b);
return (a << length) | b;
}
//Appends number a to the left of b
//bin_append(0b1, 0b10) = 0b110
private static int BinAppend(int a, int b, int length)
{
return (a << length) | b;
}
private static long BinAppend(long a, long b, int length)
{
return (a << length) | b;
}
private static long[] StatesFromSection(NbtCompound section)
{
// BlockStates is an array of 64 bit numbers that holds the blocks index on the palette list
long[] states;
if (section.Contains("block_states"))
{
states = section.Get<NbtCompound>("block_states").Get<NbtLongArray>("data").Value;
}
else
{
states = section.Get<NbtLongArray>("BlockStates").Value;
}
// makes sure the number is unsigned
// by adding 2^64
// could also use ctypes.c_ulonglong(n).value but that'd require an extra import
long[] sortedStates = new long[states.Length];
for (int i = 0; i < states.Length; i++)
{
if (states[i] >= 0)
{
sortedStates[i] = states[i];
}
else
{
// code from original anvil-parser, idk if it is needed
///sortedStates[i] = (int)(states[i] * Math.Pow(2, 64));
sortedStates[i] = states[i];
}
}
return sortedStates;
}
private static NbtList PaletteFromSection(NbtCompound section)
{
if (section.Contains("block_states"))
{
return section.Get<NbtCompound>("block_states").Get<NbtList>("palette");
}
else
{
return section.Get<NbtList>("Palette");
}
}
private static Tuple<int, int> SectionHeightRange(int version)
{
if (version > Versions.VERSION_17w47a)
{
return new Tuple<int, int>(-4, 20);
}
else
{
return new Tuple<int, int>(0, 16);
}
}
}
}