-
Notifications
You must be signed in to change notification settings - Fork 51
/
chunk.cpp
executable file
·786 lines (679 loc) · 24 KB
/
chunk.cpp
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/** Copyright (c) 2013, Sean Kasun */
#include <algorithm> // std::max
#include <typeinfo> // typeid
#include "chunk.h"
#include "identifier/flatteningconverter.h"
#include "identifier/blockidentifier.h"
#include "identifier/biomeidentifier.h"
template<typename ValueT>
inline void* safeMemCpy(void* dest, const std::vector<ValueT>& srcVec, size_t length)
{
const size_t src_data_size = (sizeof(ValueT) * srcVec.size());
if (length > src_data_size) {
#if defined(DEBUG) || defined(_DEBUG) || defined(QT_DEBUG)
qWarning() << "Copy too much data!";
#endif
length = src_data_size; // this happens sometimes and I guess its then actually a bug in the load() implementation. But this way it at least doesn't crash randomly.
}
return memcpy(dest, &srcVec[0], length);
}
Chunk::Chunk()
: version(0)
, highest(INT_MIN)
, lowest(INT_MAX)
, loaded(false)
, rendering(false)
, inhabitedTime(0)
, isChunkLocked(false)
{}
Chunk::~Chunk() {
if (loaded) {
loaded = false;
for (auto sec : this->sections)
if (sec) {
if (!(sec->blockPaletteIsShared) && (sec->blockPaletteLength > 0)) {
delete[] sec->blockPalette;
}
sec->blockPaletteLength = 0;
sec->blockPalette = NULL;
delete sec;
}
this->sections.clear();
}
}
void Chunk::findHighestBlock()
{
// loop over all Sections in reverse order
QMapIterator<qint8, ChunkSection*> it(this->sections);
it.toBack();
while (it.hasPrevious()) {
it.previous();
if (it.value()) {
for (int j = 4095; j >= 0; j--) {
if (it.value()->blocks[j]) {
// found first non-air Block
highest = it.key() * 16 + (j >> 8);
return;
}
}
}
}
}
const Chunk::EntityMap &Chunk::getEntityMap() const {
return entities;
}
//inline
const ChunkSection *Chunk::getSectionByY(int y) const {
if (y < -2048) return NULL;
if (y >= 2048) return NULL;
qint8 section_idx = (y >> 4);
return getSectionByIdx(section_idx);
}
//inline
const ChunkSection *Chunk::getSectionByIdx(qint8 y) const {
auto iter = sections.find(y);
if (iter != sections.end())
return iter.value();
return nullptr;
}
uint Chunk::getBlockHID(int x, int y, int z) const {
const ChunkSection * const section = getSectionByY(y);
if (!section) {
return 0;
}
const PaletteEntry& pdata = section->getPaletteEntry(x, y, z);
return pdata.hid;
}
int Chunk::getBiomeID(int x, int y, int z) const {
int offset;
if (this->version >= 2800) {
// Minecraft 1.18 has Y dependand Biome stored per Section
int x_idx = x >> 2;
int y_idx = (y & 0x0f) >> 2;
int z_idx = z >> 2;
offset = x_idx + 4*z_idx + 16*y_idx;
int s_idx = (y >> 4);
auto s_iter = this->sections.find(s_idx);
if (s_iter != this->sections.end() && s_iter.value())
return s_iter.value()->getBiome(offset);
else {
#if defined(DEBUG) || defined(_DEBUG) || defined(QT_DEBUG)
qWarning() << "Section not found for Biome lookup!";
#endif
return -1;
}
} else if (this->version >= 2203) {
// Minecraft 1.15 has Y dependand Biome
int x_idx = x >> 2;
int y_idx = y >> 2;
int z_idx = z >> 2;
offset = x_idx + 4*z_idx + 16*y_idx;
} else {
// Minecraft <1.15 has fixed Biome per collumn
offset = x + 16*z;
}
if ((offset < 0) || ((unsigned long long)(offset) > sizeof(this->biomes)/sizeof(this->biomes[0]))) {
#if defined(DEBUG) || defined(_DEBUG) || defined(QT_DEBUG)
qWarning() << "Biome index out of range!";
#endif
return -1;
}
return this->biomes[offset];
}
//-------------------------------------------------------------------------------------------------
// this is where we load NBT data and parse it
void Chunk::load(const NBT &nbt) {
renderedAt = INT_MIN; // impossible.
renderedFlags = 0; // no flags
this->sections.clear();
if (nbt.has("DataVersion"))
this->version = nbt.at("DataVersion")->toInt();
else
this->version = 0;
if (nbt.has("Level")) {
const Tag * level = nbt.at("Level");
loadLevelTag(level);
} else if (version >= 2844) {
loadCliffsCaves(nbt);
}
}
// Chunk NBT structure used up to 1.17
// nested with all data below a "Level" tag
void Chunk::loadLevelTag(const Tag * level) {
if (level->has("xPos"))
chunkX = level->at("xPos")->toInt();
if (level->has("xPos"))
chunkZ = level->at("zPos")->toInt();
if (level->has("InhabitedTime"))
inhabitedTime = dynamic_cast<const Tag_Long *>(level->at("InhabitedTime"))->toLong();
// load Biome data
// Partially-generated chunks may have an empty Biomes tag.
// Trying to extract the Biomes data in that case will cause a crash.
if (level->has("Biomes") && level->at("Biomes") && level->at("Biomes")->length()) {
const Tag * biomesTag = level->at("Biomes");
if (typeid(*biomesTag) == typeid(Tag_Int_Array)) {
// Biomes is Tag_Int_Array
// -> format after "The Flattening"
// raw copy Biome data
const Tag_Int_Array * biomeData = dynamic_cast<const Tag_Int_Array*>(level->at("Biomes"));
std::size_t len = std::min(sizeof(this->biomes), (sizeof(int)*biomeData->length()));
safeMemCpy(this->biomes, biomeData->toIntArray(), len);
} else if (typeid(*biomesTag) == typeid(Tag_Byte_Array)) {
// Biomes is Tag_Byte_Array
// -> old Biome format before "The Flattening"
const Tag_Byte_Array * biomeData = dynamic_cast<const Tag_Byte_Array*>(level->at("Biomes"));
// convert quint8 to quint32
auto rawBiomes = biomeData->toByteArray();
int len = std::min(256, biomeData->length());
for (int i=0; i<len; i++) {
this->biomes[i] = rawBiomes[i];
}
}
} else { // no Biome data present
int len = sizeof(this->biomes) / sizeof(this->biomes[0]);
for (int i=0; i<len; i++)
this->biomes[i] = -1;
}
// load available Sections
if (level->has("Sections")) {
auto sections = level->at("Sections");
int numSections = sections->length();
// loop over all stored Sections, they are not guarantied to be ordered or consecutive
for (int s = 0; s < numSections; s++) {
const Tag * section = sections->at(s);
int idx = section->at("Y")->toInt();
const Tag_Compound * tc = static_cast<const Tag_Compound *>(section);
if (tc->length() <= 1)
continue; // skip sections without data
bool sectionContainsData;
ChunkSection *cs = new ChunkSection();
if (this->version >= 2836) {
// after "Cliffs & Caves" update (1.18)
sectionContainsData = loadSection2844(cs, section);
} else if (this->version >= 1519) {
// after "The Flattening" update (1.13)
sectionContainsData = loadSection1519(cs, section);
} else {
sectionContainsData = loadSection1343(cs, section);
}
if (sectionContainsData) {
// only if section contains usefull data, otherwise: delete cs
this->sections[idx] = cs;
this->lowest = std::min(this->lowest, idx*16);
} else { // otherwise: delete cs
delete cs;
}
}
}
// parse Tile Entities in this Chunk
if (level->has("TileEntities")) {
auto nbtListBE = level->at("TileEntities");
auto belist = GeneratedStructure::tryParseBlockEntites(nbtListBE);
for (auto it = belist.begin(); it != belist.end(); ++it) {
emit structureFound(*it);
}
}
// parse Structures that start in this Chunk
if (version >= 1519) {
if (level->has("Structures")) {
auto nbtListStructures = level->at("Structures");
auto structurelist = GeneratedStructure::tryParseChunk(nbtListStructures);
for (auto it = structurelist.begin(); it != structurelist.end(); ++it) {
emit structureFound(*it);
}
}
}
// parse Entities
if (level->has("Entities")) {
auto entitylist = level->at("Entities");
int numEntities = entitylist->length();
for (int i = 0; i < numEntities; ++i) {
auto e = Entity::TryParse(entitylist->at(i));
if (e)
entities.insert(e->type(), e);
}
}
// check for the highest block in this chunk
// todo: use highmap from stored NBT data
findHighestBlock();
loaded = true; // needs to be at the end!
}
// Chunk NBT structure used after Cliffs & Caves update (1.18+)
// flat structure with all data directly below the Chunk, tags mostly with lowercase
void Chunk::loadCliffsCaves(const NBT &nbt) {
if (nbt.has("xPos"))
chunkX = nbt.at("xPos")->toInt();
if (nbt.has("zPos"))
chunkZ = nbt.at("zPos")->toInt();
if (nbt.has("InhabitedTime"))
inhabitedTime = dynamic_cast<const Tag_Long *>(nbt.at("InhabitedTime"))->toLong();
// no Biome data present in this new storage format -> init as minecraft:air
int len = sizeof(this->biomes) / sizeof(this->biomes[0]);
for (int i=0; i<len; i++)
this->biomes[i] = -1;
// load available Sections
if (nbt.has("sections")) {
auto sections = nbt.at("sections");
int numSections = sections->length();
// loop over all stored Sections, they are not guarantied to be ordered or consecutive
for (int s = 0; s < numSections; s++) {
const Tag * section = sections->at(s);
int idx = section->at("Y")->toInt();
const Tag_Compound * tc = static_cast<const Tag_Compound *>(section);
if (tc->length() <= 1)
continue; // skip sections without data
ChunkSection *cs = new ChunkSection();
if (loadSection2844(cs, section)) {
// only if section contains usefull data, otherwise: delete cs
this->sections[idx] = cs;
this->lowest = std::min(this->lowest, idx*16);
} else { // otherwise: delete cs
delete cs;
}
}
}
// parse Block Entities in this Chunk
if (nbt.has("block_entities")) {
auto nbtListBE = nbt.at("block_entities");
auto belist = GeneratedStructure::tryParseBlockEntites(nbtListBE);
for (auto it = belist.begin(); it != belist.end(); ++it) {
emit structureFound(*it);
}
}
// parse Structures that start in this Chunk
if (nbt.has("structures")) {
auto nbtListStructures = nbt.at("structures");
auto structurelist = GeneratedStructure::tryParseChunk(nbtListStructures);
for (auto it = structurelist.begin(); it != structurelist.end(); ++it) {
emit structureFound(*it);
}
}
// check for the highest block in this chunk
// todo: use highmap from stored NBT data
findHighestBlock();
loaded = true; // needs to be at the end!
}
void Chunk::loadEntities(const NBT &nbt) {
// parse Entities in extra folder (1.17+)
if (version >= 2681) {
if (nbt.has("Entities")) {
auto entitylist = nbt.at("Entities");
int numEntities = entitylist->length();
for (int i = 0; i < numEntities; ++i) {
auto entityNbt = entitylist->at(i);
auto e = Entity::TryParse(entityNbt);
if (e) {
entities.insert(e->type(), e);
// Check for ChunkLock-related entities:
loadCheckEntityChunkLock(entityNbt);
}
}
}
}
}
void Chunk::loadCheckEntityChunkLock(const Tag * entityNbt)
{
/* ChunkLock uses a "minecraft:marker" entity to store the state:
- the entity has a "chunklock" Tag
- the entity has a Tag either "locked" or "unlocked"
- the entity has "data"."source"."id" data specifying the item needed for unlocking
- the item count needed for unlocking is stored in scoreboard, not in the entity itself (!)
*/
// Check if this is a "locked" marker:
if (entityNbt->at("id")->toString() != "minecraft:marker") {
return;
}
auto tags = dynamic_cast<const Tag_List *>(entityNbt->at("Tags"));
if (tags == nullptr) {
return;
}
auto len = tags->length();
bool hasChunklockTag = false;
bool hasLockedTag = false;
for (int i = 0; i < len; ++i)
{
auto tagStr = dynamic_cast<const Tag_String *>(tags->at(i));
if (tagStr == nullptr) {
continue;
}
auto tagStrVal = tagStr->toString();
if (tagStrVal == "chunklock") {
hasChunklockTag = true;
} else if (tagStrVal == "locked") {
hasLockedTag = true;
}
}
this->isChunkLocked = hasChunklockTag && hasLockedTag;
if (!this->isChunkLocked) {
return;
}
// Find the name of the item needed for unlocking:
auto tData = dynamic_cast<const Tag_Compound *>(entityNbt->at("data"));
if (tData == nullptr) {
return;
}
auto tDataSource = dynamic_cast<const Tag_Compound *>(tData->at("source"));
if (tDataSource == nullptr) {
return;
}
auto tId = dynamic_cast<const Tag_String *>(tDataSource->at("id"));
if (tId != nullptr) {
this->chunkLockItemName = tId->toString();
}
}
// supported DataVersions:
// 0 = 1.8 and below
//
// 169 = 1.9
// 175 = 1.9.1
// 176 = 1.9.2
// 183 = 1.9.3
// 184 = 1.9.4
//
// 510 = 1.10
// 511 = 1.10.1
// 512 = 1.10.2
//
// 819 = 1.11
// 921 = 1.11.1
// 922 = 1.11.2
//
// 1139 = 1.12
// 1241 = 1.12.1
// 1343 = 1.12.2
//
// 1519 = 1.13
// 1628 = 1.13.1
// 2203 = 1.15.19w36a
bool Chunk::loadSection1343(ChunkSection *cs, const Tag *section) {
// copy raw data
quint8 blocks[4096];
quint8 data[2048];
safeMemCpy(blocks, section->at("Blocks")->toByteArray(), 4096);
safeMemCpy(data, section->at("Data")->toByteArray(), 2048);
safeMemCpy(cs->blockLight, section->at("BlockLight")->toByteArray(), 2048);
// convert old BlockID + data into virtual ID
for (int i = 0; i < 4096; i++) {
int d = data[i>>1]; // get raw data (two nibbles)
if (i & 1) d >>= 4; // get one nibble of data
// Shift enough so virtual IDs never overlap 0-4095 range
cs->blocks[i] = blocks[i] | ((d & 0x0f) << 12);
}
// parse optional "Add" part for higher block IDs in mod packs
if (section->has("Add")) {
auto raw = section->at("Add")->toByteArray();
for (int i = 0; i < 2048; i++) {
cs->blocks[i * 2] |= (raw[i] & 0xf) << 8;
cs->blocks[i * 2 + 1] |= (raw[i] & 0xf0) << 4;
}
}
// link to Converter palette
cs->blockPaletteLength = FlatteningConverter::Instance().paletteLength;
cs->blockPalette = FlatteningConverter::Instance().getPalette();
cs->blockPaletteIsShared = true;
// check if some Block is different to minecraft:air
bool sectionContainsData = false;
for (int i = 0; i < 4096; i++) {
sectionContainsData |= (cs->blocks[i] != 0);
}
return sectionContainsData;
}
// Chunk format after "The Flattening" version 1519
bool Chunk::loadSection1519(ChunkSection *cs, const Tag *section) {
bool sectionContainsData = false;
// decode Palette to be able to map BlockStates
if (section->has("Palette")) {
loadSection_decodeBlockPalette(cs, section->at("Palette"));
} else loadSection_createDummyPalette(cs); // create a dummy palette
// map BlockStates to BlockData
if (section->has("BlockStates")) {
loadSection_loadBlockStates(cs, section->at("BlockStates"));
sectionContainsData = true;
} else {
// set everything to 0 (minecraft:air)
memset(cs->blocks, 0, sizeof(cs->blocks));
}
// copy Light data
// if (section->has("SkyLight")) {
// safeMemCpy(cs->skyLight, section->at("SkyLight")->toByteArray(), 2048);
// }
if (section->has("BlockLight")) {
safeMemCpy(cs->blockLight, section->at("BlockLight")->toByteArray(), 2048);
sectionContainsData = true;
} else {
memset(cs->blockLight, 0, sizeof(cs->blockLight));
}
return sectionContainsData;
}
// Chunk format after "Cliffs & Caves version 2800
bool Chunk::loadSection2844(ChunkSection * cs, const Tag * section) {
bool sectionContainsData = false;
// decode BlockStates-Palette to be able to map BlockStates
if (section->has("block_states") && section->at("block_states")->has("palette")) {
loadSection_decodeBlockPalette(cs, section->at("block_states")->at("palette"));
} else loadSection_createDummyPalette(cs);
// map BlockStates to BlockData
if (section->has("block_states") && section->at("block_states")->has("data")) {
loadSection_loadBlockStates(cs, section->at("block_states")->at("data"));
sectionContainsData = true;
} else {
// set everything to 0 (minecraft:air)
memset(cs->blocks, 0, sizeof(cs->blocks));
}
// decode Biomes-Palette to be able to map Biome
if (section->has("biomes") && section->at("biomes")->has("palette")) {
loadSection_decodeBiomePalette(cs, section->at("biomes"));
sectionContainsData = true;
} else {
// never observed in real live
// probably we should create some default Biome in this case
sectionContainsData = false;
}
// copy Light data
// if (section->has("SkyLight")) {
// safeMemCpy(cs->skyLight, section->at("SkyLight")->toByteArray(), 2048);
// }
if (section->has("BlockLight")) {
safeMemCpy(cs->blockLight, section->at("BlockLight")->toByteArray(), 2048);
sectionContainsData = true;
} else {
memset(cs->blockLight, 0, sizeof(cs->blockLight));
}
return sectionContainsData;
}
void Chunk::loadSection_decodeBlockPalette(ChunkSection * cs, const Tag * paletteTag) {
BlockIdentifier &bi = BlockIdentifier::Instance();
cs->blockPaletteLength = paletteTag->length();
cs->blockPaletteIsShared = false;
cs->blockPalette = new PaletteEntry[cs->blockPaletteLength];
for (int j = 0; j < paletteTag->length(); j++) {
// get name and hash it to hid
cs->blockPalette[j].name = paletteTag->at(j)->at("Name")->toString();
uint hid = qHash(cs->blockPalette[j].name);
// copy all other properties
if (paletteTag->at(j)->has("Properties"))
cs->blockPalette[j].properties = paletteTag->at(j)->at("Properties")->getData().toMap();
// check vor variants
BlockInfo const & block = bi.getBlockInfo(hid);
if (block.hasVariants()) {
// test all available properties
for (auto key : cs->blockPalette[j].properties.keys()) {
QString vname = cs->blockPalette[j].name + ":" + key + ":" + cs->blockPalette[j].properties[key].toString();
uint vhid = qHash(vname);
if (bi.hasBlockInfo(vhid))
hid = vhid; // use this vaiant instead
}
// test all possible combinations of 2 combined properties
if (cs->blockPalette[j].properties.keys().length() > 1) {
for (auto key1 : cs->blockPalette[j].properties.keys()) {
for (auto key2 : cs->blockPalette[j].properties.keys()) {
if (key1 == key2) continue;
QString vname = cs->blockPalette[j].name + ":" +
key1 + ":" + cs->blockPalette[j].properties[key1].toString() + " " +
key2 + ":" + cs->blockPalette[j].properties[key2].toString();
uint vhid = qHash(vname);
if (bi.hasBlockInfo(vhid))
hid = vhid; // use this vaiant instead
}
}
}
}
// store hash of found variant
cs->blockPalette[j].hid = hid;
}
}
void Chunk::loadSection_createDummyPalette(ChunkSection *cs) {
// create a dummy palette
cs->blockPalette = new PaletteEntry[1];
cs->blockPalette[0].name = "minecraft:air";
cs->blockPalette[0].hid = 0;
}
void Chunk::loadSection_loadBlockStates(ChunkSection *cs, const Tag * blockStateTag) {
auto blockStates = blockStateTag->toLongArray();
int bsCnt = 0; // counter for 64bit words
int bitCnt = 0; // counter for bits
if (this->version < 2529) {
// "compact BlockStates" just the first time after "The Flattening"
for (int i = 0; i < 4096; i++) {
int bitSize = (blockStateTag->length())*64/4096;
int bitMask = (1 << bitSize)-1;
if (bitCnt+bitSize <= 64) {
// bits fit into current word
uint64_t blockState = blockStates[bsCnt];
cs->blocks[i] = (blockState >> bitCnt) & bitMask;
bitCnt += bitSize;
if (bitCnt == 64) {
bitCnt = 0;
bsCnt++;
}
} else {
// bits are spread accross two words
uint64_t blockState1 = blockStates[bsCnt++];
uint64_t blockState2 = blockStates[bsCnt];
uint32_t block = (blockState1 >> bitCnt) & bitMask;
bitCnt += bitSize;
bitCnt -= 64;
block += (blockState2 << (bitSize - bitCnt)) & bitMask;
cs->blocks[i] = block;
}
}
} else {
// "optimized for loading" BlockStates since 1.16.20w17a
int bitSize = std::max(4, int(ceil(log2(cs->blockPaletteLength))));
int bitMask = (1 << bitSize)-1;
for (int i = 0; i < 4096; i++) {
uint64_t blockState = blockStates[bsCnt];
cs->blocks[i] = (blockState >> bitCnt) & bitMask;
bitCnt += bitSize;
if (bitCnt+bitSize > 64) {
bsCnt++;
bitCnt = 0;
}
}
}
}
bool Chunk::loadSection_decodeBiomePalette(ChunkSection * cs, const Tag * biomesTag) {
BiomeIdentifier &bi = BiomeIdentifier::Instance();
if (biomesTag->has("palette")) {
auto paletteTag = biomesTag->at("palette");
int biomePaletteLength = paletteTag->length();
PaletteEntry* biomePalette = new PaletteEntry[biomePaletteLength];
for (int j = 0; j < biomePaletteLength; j++) {
biomePalette[j].name = paletteTag->at(j)->toString();
// query BiomeIdentifer for that Biome
quint32 bID = bi.getBiomeByName(biomePalette[j].name).id;
// get name and hash it to hid
biomePalette[j].hid = bID;
}
if (biomesTag->has("data")) {
auto biomeStates = biomesTag->at("data")->toLongArray();
int bsCnt = 0; // counter for 64bit words
int bitCnt = 0; // counter for bits
// "optimized for loading" Biome data
int bitSize = std::max(1, int(ceil(log2(biomePaletteLength))));
int bitMask = (1 << bitSize)-1;
int len = sizeof(cs->biomes)/sizeof(cs->biomes[0]);
for (int i = 0; i < len; i++) {
uint64_t biomeState = biomeStates[bsCnt];
cs->biomes[i] = biomePalette[(biomeState >> bitCnt) & bitMask].hid;
bitCnt += bitSize;
if (bitCnt+bitSize > 64) {
bsCnt++;
bitCnt = 0;
}
}
} else {
// all Biome data is the same
std::fill_n(cs->biomes, sizeof(cs->biomes), biomePalette[0].hid);
}
delete[] biomePalette;
return true;
} else return false;
}
//-------------------------------------------------------------------------------------------------
// ChunkSection
ChunkSection::ChunkSection()
: blockPalette(NULL)
, blockPaletteLength(0)
, blockPaletteIsShared(false) // only the "old" converted format is using one shared palette
{}
const PaletteEntry & ChunkSection::getPaletteEntry(int x, int y, int z) const {
int xoffset = (x & 0x0f);
int yoffset = (y & 0x0f) << 8;
int zoffset = (z & 0x0f) << 4;
return getPaletteEntry(xoffset + yoffset + zoffset);
}
const PaletteEntry & ChunkSection::getPaletteEntry(int offset, int y) const {
int yoffset = (y & 0x0f) << 8;
return getPaletteEntry(offset + yoffset);
}
const PaletteEntry & ChunkSection::getPaletteEntry(int offset) const {
quint16 blockid = blocks[offset];
if (blockid < blockPaletteLength)
return blockPalette[blockid];
else
return blockPalette[0];
}
quint16 ChunkSection::getBiome(int x, int y, int z) const {
int xoffset = x;
int yoffset = (y & 0x0f) << 8;
int zoffset = z << 4;
return getBiome(xoffset + yoffset + zoffset);
}
quint16 ChunkSection::getBiome(int offset, int y) const {
int yoffset = (y & 0x0f) << 8;
return getBiome(offset + yoffset);
}
quint16 ChunkSection::getBiome(int offset) const {
return biomes[offset];
}
//quint8 ChunkSection::getSkyLight(int x, int y, int z) {
// int xoffset = x;
// int yoffset = (y & 0x0f) << 8;
// int zoffset = z << 4;
// int value = skyLight[(xoffset + yoffset + zoffset) / 2];
// if (x & 1) value >>= 4;
// return value & 0x0f;
//}
//quint8 ChunkSection::getSkyLight(int offset, int y) {
// int yoffset = (y & 0x0f) << 8;
// int value = skyLight[(offset + yoffset) / 2];
// if (offset & 1) value >>= 4;
// return value & 0x0f;
//}
quint8 ChunkSection::getBlockLight(int x, int y, int z) const {
int xoffset = x;
int yoffset = (y & 0x0f) << 8;
int zoffset = z << 4;
return getBlockLight(xoffset + yoffset + zoffset);
}
quint8 ChunkSection::getBlockLight(int offset, int y) const {
int yoffset = (y & 0x0f) << 8;
return getBlockLight(offset + yoffset);
}
inline
quint8 ChunkSection::getBlockLight(int offset) const {
int value = blockLight[offset / 2];
if (offset & 1) value >>= 4;
return value & 0x0f;
}