Botcraft 26.3
Loading...
Searching...
No Matches
Chunk.cpp
Go to the documentation of this file.
5
6#include <algorithm>
7
8using namespace ProtocolCraft;
9
10namespace Botcraft
11{
12 enum class Palette
13 {
14#if PROTOCOL_VERSION > 756 /* > 1.17.1 */
16#endif
19 };
20
21#if PROTOCOL_VERSION > 756 /* > 1.17.1 */
22 namespace
23 {
24 /// @brief Decode one 1.18+ paletted container (block states or biomes).
25 /// @return global ids in y, z, x order (num_entries values). Empty vector on a malformed container.
26 std::vector<int> ReadPalettedContainer(
27 ReadIterator& iter, size_t& length,
28 const int num_entries,
29 const unsigned char min_indirect_bits,
30 const unsigned char max_indirect_bits
31 )
32 {
33 unsigned char bits = ReadData<unsigned char>(iter, length);
34
35 std::vector<int> palette;
36 if (bits == 0) // single value
37 {
38 palette.push_back(ReadData<VarInt>(iter, length));
39 }
40 else if (bits <= max_indirect_bits)
41 {
42 bits = std::max(bits, min_indirect_bits);
43 palette.resize(ReadData<VarInt>(iter, length));
44 for (int& p : palette)
45 {
46 p = ReadData<VarInt>(iter, length);
47 }
48 }
49
50 const int values_per_long = bits == 0 ? 0 : 64 / bits;
51#if PROTOCOL_VERSION < 770 /* < 1.21.5 */
52 const int data_array_size = ReadData<VarInt>(iter, length);
53#else
54 const int data_array_size = bits == 0 ? 0 : (num_entries + values_per_long - 1) / values_per_long;
55#endif
56 if (bits != 0)
57 {
58 const int expected_longs = (num_entries + values_per_long - 1) / values_per_long;
59 if (data_array_size != expected_longs)
60 {
61 LOG_ERROR("Invalid data array size (" << data_array_size << " instead of " << expected_longs << "). Stop loading current chunk data");
62 return {};
63 }
64 }
65
66 std::vector<unsigned long long int> data_array(data_array_size);
67 for (unsigned long long int& l : data_array)
68 {
69 l = ReadData<unsigned long long int>(iter, length);
70 }
71
72 if (bits == 0)
73 {
74 return std::vector<int>(num_entries, palette[0]);
75 }
76
77 std::vector<int> output(num_entries);
78 const unsigned long long int mask = (1ULL << bits) - 1;
79 for (int i = 0; i < num_entries; ++i)
80 {
81 const int raw = static_cast<int>((data_array[i / values_per_long] >> ((i % values_per_long) * bits)) & mask);
82 if (palette.empty()) // global palette: ids used as-is
83 {
84 output[i] = raw;
85 }
86 else
87 {
88 if (raw >= static_cast<int>(palette.size()))
89 {
90 LOG_ERROR("Palette index " << raw << " out of range (" << palette.size() << "). Stop loading current chunk data");
91 return {};
92 }
93 output[i] = palette[raw];
94 }
95 }
96 return output;
97 }
98 }
99#endif
100
101#if PROTOCOL_VERSION < 757 /* < 1.18 */
102 Chunk::Chunk(const size_t dim_index, const bool has_sky_light_)
103#else
104 Chunk::Chunk(const int min_y_, const unsigned int height_, const size_t dim_index, const bool has_sky_light_)
105#endif
106 {
107 dimension_index = dim_index;
108 has_sky_light = has_sky_light_;
109#if PROTOCOL_VERSION > 756 /* > 1.17.1 */
110 height = height_;
111 min_y = min_y_;
112#endif
113
114#if PROTOCOL_VERSION < 552 /* < 1.15 */
115 biomes = std::vector<unsigned char>(CHUNK_WIDTH * CHUNK_WIDTH, 0);
116#else
117 // Each section has 64 biomes, one for each 4*4*4 cubes
118 biomes = std::vector<unsigned char>(64 * height / SECTION_HEIGHT, 0);
119#endif
120 sections = std::vector<std::unique_ptr<Section> >(height / SECTION_HEIGHT);
121
122#if USE_GUI
123 modified_since_last_rendered = true;
124#endif
125 }
126
128 {
131 biomes = c.biomes;
132
133#if PROTOCOL_VERSION > 756 /* > 1.17.1 */
134 height = c.height;
135 min_y = c.min_y;
136#endif
137
138 sections = std::vector<std::unique_ptr<Section> >(height / SECTION_HEIGHT);
139 for (size_t i = 0; i < c.sections.size(); i++)
140 {
141 if (c.sections[i] != nullptr)
142 {
143 sections[i] = std::make_unique<Section>(*c.sections[i]);
144 }
145 }
146
149
150#if USE_GUI
152#endif
153 }
154
155 Chunk::Chunk(Chunk&&) noexcept = default;
156
157 Chunk& Chunk::operator=(Chunk&&) noexcept = default;
158
160 {
161
162 }
163
165 {
166 return Position(
167 static_cast<int>(std::floor(pos.x / static_cast<double>(CHUNK_WIDTH))),
168 0,
169 static_cast<int>(std::floor(pos.z / static_cast<double>(CHUNK_WIDTH)))
170 );
171 }
172
173 int Chunk::GetMinY() const
174 {
175 return min_y;
176 }
177
179 {
180 return height;
181 }
182
183#if USE_GUI
188
193#endif
194
195#if PROTOCOL_VERSION < 757 /* < 1.18 */
196#if PROTOCOL_VERSION < 552 /* < 1.15 */
197 void Chunk::LoadChunkData(const std::vector<unsigned char>& data, const int primary_bit_mask, const bool ground_up_continuous)
198#elif PROTOCOL_VERSION < 755 /* < 1.17 */
199 void Chunk::LoadChunkData(const std::vector<unsigned char>& data, const int primary_bit_mask)
200#else
201 void Chunk::LoadChunkData(const std::vector<unsigned char>& data, const std::vector<unsigned long long int>& primary_bit_mask)
202#endif
203 {
204 std::vector<unsigned char>::const_iterator iter = data.begin();
205 size_t length = data.size();
206
207 if (data.size() == 0)
208 {
209 LOG_ERROR("Cannot load chunk data without data");
210 return;
211 }
212
213 //The chunck sections
214 for (int sectionY = 0; sectionY < height / SECTION_HEIGHT; ++sectionY)
215 {
216#if PROTOCOL_VERSION < 755 /* < 1.17 */
217 if (!(primary_bit_mask & (1 << sectionY)))
218#else
219 if (!(primary_bit_mask[sectionY / 64] & (1 << (sectionY % 64))))
220#endif
221 {
222 continue;
223 }
224
225#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
226 short block_count = ReadData<short>(iter, length);
227#endif
228 unsigned char bits_per_block = ReadData<unsigned char>(iter, length);
229
230 Palette palette_type = (bits_per_block <= 8 ? Palette::SectionPalette : Palette::GlobalPalette);
231
232#if PROTOCOL_VERSION < 384 /* < 1.13 */
233 int palette_length = ReadData<VarInt>(iter, length);
234#else
235 int palette_length = 0;
236 if (palette_type != Palette::GlobalPalette)
237 {
238 palette_length = ReadData<VarInt>(iter, length);
239 }
240#endif
241 std::vector<int> palette(palette_length);
242 if (palette_type != Palette::GlobalPalette)
243 {
244 if (bits_per_block < 4)
245 {
246 LOG_ERROR("Bits per block must be between 4 and 8 when using a palette (current: " << bits_per_block << "). Stop loading current chunk data");
247 return;
248 }
249
250 for (int i = 0; i < palette_length; ++i)
251 {
252 palette[i] = ReadData<VarInt>(iter, length);
253 }
254 }
255 else
256 {
257 if (palette_length != 0)
258 {
259 LOG_ERROR("Palette length should be 0 for global palette (current: " << palette_length << "). Stop loading current chunk data");
260 return;
261 }
262 }
263
264 //A mask 0...01..1 with bits_per_block ones
265 unsigned int individual_value_mask = static_cast<unsigned int>((1 << bits_per_block) - 1);
266
267 //Data array length
268 int data_array_size = ReadData<VarInt>(iter, length);
269
270#if PROTOCOL_VERSION > 712 /* > 1.15.2 */
271 const int values_per_long = 64 / bits_per_block;
272 const int expected_longs = (SECTION_HEIGHT * CHUNK_WIDTH * CHUNK_WIDTH + values_per_long - 1) / values_per_long;
273#else
274 const int expected_longs = (SECTION_HEIGHT * CHUNK_WIDTH * CHUNK_WIDTH * bits_per_block + 63) / 64;
275#endif
276 if(data_array_size != expected_longs)
277 {
278 LOG_ERROR("Invalid chunk data array size (" << data_array_size << " instead of " << expected_longs << "). Stop loading current chunk data");
279 return;
280 }
281
282 //Data array
283 std::vector<unsigned long long int> data_array(data_array_size);
284 for (int i = 0; i < data_array_size; ++i)
285 {
286 data_array[i] = ReadData<unsigned long long int>(iter, length);
287 }
288
289 //Blocks data
290#if PROTOCOL_VERSION > 712 /* > 1.15.2 */
291 int bit_offset = 0;
292#endif
293 Position pos;
294 for (int block_y = 0; block_y < SECTION_HEIGHT; ++block_y)
295 {
296 pos.y = block_y + sectionY * SECTION_HEIGHT + min_y;
297 for (int block_z = 0; block_z < CHUNK_WIDTH; ++block_z)
298 {
299 pos.z = block_z;
300 for (int block_x = 0; block_x < CHUNK_WIDTH; ++block_x)
301 {
302 pos.x = block_x;
303#if PROTOCOL_VERSION > 712 /* > 1.15.2 */
304 // From protocol version 713, the compacted array format has been adjusted so that
305 //individual entries no longer span across multiple longs
306 if (64 - (bit_offset % 64) < bits_per_block)
307 {
308 bit_offset += 64 - (bit_offset % 64);
309 }
310 int start_long_index = bit_offset / 64;
311 int end_long_index = start_long_index;
312 int start_offset = bit_offset % 64;
313 bit_offset += bits_per_block;
314#else
315 int block_index = (((block_y * SECTION_HEIGHT) + block_z) * CHUNK_WIDTH) + block_x;
316 int start_long_index = (block_index * bits_per_block) / 64;
317 int start_offset = (block_index * bits_per_block) % 64;
318 int end_long_index = ((block_index + 1) * bits_per_block - 1) / 64;
319#endif
320 unsigned int raw_id;
321 if (start_long_index == end_long_index)
322 {
323 raw_id = static_cast<unsigned int>(data_array[start_long_index] >> start_offset);
324 }
325 else
326 {
327 int end_offset = 64 - start_offset;
328 raw_id = static_cast<unsigned int>(data_array[start_long_index] >> start_offset | data_array[end_long_index] << end_offset);
329 }
330 raw_id &= individual_value_mask;
331
332 if (palette_type != Palette::GlobalPalette)
333 {
334 if (raw_id >= palette.size())
335 {
336 LOG_ERROR("Palette index " << raw_id << " out of range (" << palette.size() << "). Stop loading current chunk data");
337 return;
338 }
339 raw_id = palette[raw_id];
340 }
341
342#if PROTOCOL_VERSION < 347 /* < 1.13 */
343 int id;
344 unsigned char metadata;
345
346 Blockstate::IdToIdMetadata(raw_id, id, metadata);
347
348 SetBlock(pos, { id, metadata });
349#else
350 SetBlock(pos, raw_id);
351#endif
352 }
353 }
354 }
355
356#if PROTOCOL_VERSION <= 404 /* <= 1.13.2 */
357 //Block light
358 for (int block_y = 0; block_y < SECTION_HEIGHT; ++block_y)
359 {
360 pos.y = block_y + sectionY * SECTION_HEIGHT + min_y;
361 for (int block_z = 0; block_z < CHUNK_WIDTH; ++block_z)
362 {
363 pos.z = block_z;
364 for (int block_x = 0; block_x < CHUNK_WIDTH; block_x += 2)
365 {
366 pos.x = block_x;
367 unsigned char two_light_values = ReadData<unsigned char>(iter, length);
368
369 SetBlockLight(pos, two_light_values & 0x0F);
370 pos.x = block_x + 1;
371 SetBlockLight(pos, (two_light_values >> 4) & 0x0F);
372 }
373 }
374 }
375
376 //Sky light
377 if (has_sky_light)
378 {
379 for (int block_y = 0; block_y < SECTION_HEIGHT; ++block_y)
380 {
381 pos.y = block_y + sectionY * SECTION_HEIGHT + min_y;
382 for (int block_z = 0; block_z < CHUNK_WIDTH; ++block_z)
383 {
384 pos.z = block_z;
385 for (int block_x = 0; block_x < CHUNK_WIDTH; block_x += 2)
386 {
387 pos.x = block_x;
388 unsigned char two_light_values = ReadData<unsigned char>(iter, length);
389
390 SetSkyLight(pos, two_light_values & 0x0F);
391 pos.x = block_x + 1;
392 SetSkyLight(pos, (two_light_values >> 4) & 0x0F);
393 }
394 }
395 }
396 }
397#endif
398 }
399
400#if PROTOCOL_VERSION < 552 /* < 1.15 */
401 //The biomes
402 if (ground_up_continuous)
403 {
404 for (int block_z = 0; block_z < CHUNK_WIDTH; ++block_z)
405 {
406 for (int block_x = 0; block_x < CHUNK_WIDTH; ++block_x)
407 {
408#if PROTOCOL_VERSION < 358 /* < 1.13 */
409 SetBiome(block_x, block_z, ReadData<unsigned char>(iter, length));
410#else
411 SetBiome(block_x, block_z, ReadData<int>(iter, length));
412#endif
413 }
414 }
415 }
416#endif
417#if USE_GUI
418 modified_since_last_rendered = true;
419#endif
420 }
421#else
422 void Chunk::LoadChunkData(const std::vector<unsigned char>& data)
423 {
424 std::vector<unsigned char>::const_iterator iter = data.begin();
425 size_t length = data.size();
426
427 if (data.size() == 0)
428 {
429 LOG_WARNING("Cannot load chunk data without data");
430 return;
431 }
432
433 //The chunck sections
434 for (int sectionY = 0; sectionY < height / SECTION_HEIGHT; ++sectionY)
435 {
436 short block_count = ReadData<short>(iter, length);
437#if PROTOCOL_VERSION > 774 /* > 1.21.11 */
438 short fluid_count = ReadData<short>(iter, length);
439#endif
440
441 const std::vector<int> ids = ReadPalettedContainer(iter, length, SECTION_HEIGHT * CHUNK_WIDTH * CHUNK_WIDTH, 4, 8);
442 if (ids.empty())
443 {
444 return;
445 }
446
447 if (block_count == 0)
448 {
449 sections[sectionY] = nullptr;
450 }
451 else
452 {
453 size_t idx = 0;
454 Position pos;
455 for (int block_y = 0; block_y < SECTION_HEIGHT; ++block_y)
456 {
457 pos.y = block_y + sectionY * SECTION_HEIGHT + min_y;
458 for (int block_z = 0; block_z < CHUNK_WIDTH; ++block_z)
459 {
460 pos.z = block_z;
461 for (int block_x = 0; block_x < CHUNK_WIDTH; ++block_x)
462 {
463 pos.x = block_x;
464 SetBlock(pos, ids[idx++]);
465 }
466 }
467 }
468 }
469
470 const std::vector<int> biomes = ReadPalettedContainer(iter, length, SECTION_HEIGHT / 4 * CHUNK_WIDTH / 4 * CHUNK_WIDTH / 4, 1, 3);
471 if (biomes.empty())
472 {
473 return;
474 }
475 for (size_t i = 0; i < biomes.size(); ++i)
476 {
477 SetBiome(sectionY * SECTION_HEIGHT / 4 * CHUNK_WIDTH / 4 * CHUNK_WIDTH / 4 + static_cast<int>(i), biomes[i]);
478 }
479 }
480#if USE_GUI
482#endif
483 }
484#endif
485
486#if PROTOCOL_VERSION < 757 /* < 1.18 */
487 void Chunk::LoadChunkBlockEntitiesData(const std::vector<NBT::Value>& block_entities)
488#else
489 void Chunk::LoadChunkBlockEntitiesData(const std::vector<BlockEntityInfo>& block_entities)
490#endif
491 {
492 // Block entities data
493 block_entities_data.clear();
494
495 for (int i = 0; i < block_entities.size(); ++i)
496 {
497#if PROTOCOL_VERSION < 757 /* < 1.18 */
498 if (block_entities[i].HasData())
499 {
500 if (block_entities[i].contains("x") &&
501 block_entities[i]["x"].is<int>() &&
502 block_entities[i].contains("y") &&
503 block_entities[i]["y"].is<int>() &&
504 block_entities[i].contains("z") &&
505 block_entities[i]["z"].is<int>())
506 {
507 block_entities_data[Position((block_entities[i]["x"].get<int>() % CHUNK_WIDTH + CHUNK_WIDTH) % CHUNK_WIDTH, block_entities[i]["y"].get<int>(), (block_entities[i]["z"].get<int>() % CHUNK_WIDTH + CHUNK_WIDTH) % CHUNK_WIDTH)] = block_entities[i];
508 }
509 }
510#else
511 const int x = (block_entities[i].GetPackedXZ() >> 4) & 15;
512 const int z = (block_entities[i].GetPackedXZ() & 15);
513 // And what about the type ???
514 block_entities_data[Position((x % CHUNK_WIDTH + CHUNK_WIDTH) % CHUNK_WIDTH, block_entities[i].GetY(), (z % CHUNK_WIDTH + CHUNK_WIDTH) % CHUNK_WIDTH)] = block_entities[i].GetTag();
515#endif
516 }
517
518#if USE_GUI
519 modified_since_last_rendered = true;
520#endif
521 }
522
524 {
525 if (!IsInsideChunk(pos, true))
526 {
527 return;
528 }
529
530 block_entities_data[pos] = block_entity;
531
532#if USE_GUI
534#endif
535 }
536
538 {
539 block_entities_data.erase(pos);
540 }
541
543 {
544 auto it = block_entities_data.find(pos);
545 if (it == block_entities_data.end())
546 {
547 return NBT::Value();
548 }
549
550 return it->second;
551 }
552
553 const Blockstate* Chunk::GetBlock(const Position& pos) const
554 {
555 if (!IsInsideChunk(pos, false))
556 {
557 return nullptr;
558 }
559
560 const int section_y = (pos.y - min_y) / SECTION_HEIGHT;
561 if (sections[section_y] == nullptr)
562 {
563 return nullptr;
564 }
565
566#if PROTOCOL_VERSION < 347 /* < 1.13 */
567 BlockstateId block_id;
568 const unsigned short stored_id = *(sections[section_y]->data_blocks.data() + Section::CoordsToBlockIndex(pos.x, (pos.y - min_y) % SECTION_HEIGHT, pos.z));
569 Blockstate::IdToIdMetadata(static_cast<unsigned int>(stored_id), block_id.first, block_id.second);
570#else
571 const BlockstateId block_id = static_cast<BlockstateId>(*(sections[section_y]->data_blocks.data() + Section::CoordsToBlockIndex(pos.x, (pos.y - min_y) % SECTION_HEIGHT, pos.z)));
572#endif
573 return AssetsManager::getInstance().GetBlockstate(block_id);
574 }
575
576 void Chunk::SetBlock(const Position& pos, const Blockstate* block)
577 {
578 if (block == nullptr)
579 {
580#if PROTOCOL_VERSION < 347 /* < 1.13 */
581 SetBlock(pos, { -1, 0 });
582#else
583 SetBlock(pos, -1);
584#endif
585 }
586 else
587 {
588 SetBlock(pos, block->GetId());
589 }
590 }
591
592 void Chunk::SetBlock(const Position& pos, const BlockstateId id)
593 {
594 if (!IsInsideChunk(pos, false))
595 {
596 return;
597 }
598
599 const int section_y = (pos.y - min_y) / SECTION_HEIGHT;
600 if (!sections[section_y])
601 {
602#if PROTOCOL_VERSION < 347 /* < 1.13 */
603 if (id.first == 0)
604#else
605 if (id == 0)
606#endif
607 {
608 return;
609 }
610 else
611 {
612 AddSection(section_y);
613 }
614 }
615
616#if PROTOCOL_VERSION < 347 /* < 1.13 */
617 const unsigned short block_id = static_cast<unsigned short>(Blockstate::IdMetadataToId(id.first, id.second));
618#else
619 const unsigned short block_id = static_cast<unsigned short>(id);
620#endif
621 sections[section_y]->data_blocks[Section::CoordsToBlockIndex(pos.x, (pos.y - min_y) % SECTION_HEIGHT, pos.z)] = block_id;
622
623#if USE_GUI
625#endif
626 }
627
628 unsigned char Chunk::GetBlockLight(const Position& pos) const
629 {
630 if (!IsInsideChunk(pos, true))
631 {
632 return 0;
633 }
634
635 const int section_y = (pos.y - min_y) / SECTION_HEIGHT;
636 if (!sections[section_y])
637 {
638 return 0;
639 }
640
641 return (sections[section_y]->block_light[Section::CoordsToLightIndex(pos.x, (pos.y - min_y) % SECTION_HEIGHT, pos.z)] >> (4 * (pos.x % 2))) & 0x0F;
642 }
643
644 void Chunk::SetBlockLight(const Position& pos, const unsigned char v)
645 {
646 if (!IsInsideChunk(pos, true))
647 {
648 return;
649 }
650
651 const int section_y = (pos.y - min_y) / SECTION_HEIGHT;
652 if (!sections[section_y])
653 {
654 AddSection(section_y);
655 }
656
657 unsigned char* packed_value = sections[section_y]->block_light.data() + Section::CoordsToLightIndex(pos.x, (pos.y - min_y) % SECTION_HEIGHT, pos.z);
658 if (pos.x % 2 == 1)
659 {
660 const unsigned char first_value = *packed_value & 0x0F;
661 *packed_value = first_value | ((v & 0x0F) << 4);
662 }
663 else
664 {
665 const unsigned char second_value = *packed_value & 0xF0;
666 *packed_value = second_value | (v & 0x0F);
667 }
668 // Not necessary as we don't render lights
669//#if USE_GUI
670// modified_since_last_rendered = true;
671//#endif
672 }
673
674 unsigned char Chunk::GetSkyLight(const Position& pos) const
675 {
676 if (!has_sky_light || !IsInsideChunk(pos, true))
677 {
678 return 0;
679 }
680
681 const int section_y = (pos.y - min_y) / SECTION_HEIGHT;
682 if (!sections[section_y])
683 {
684 return 0;
685 }
686
687 return (sections[section_y]->sky_light[Section::CoordsToLightIndex(pos.x, (pos.y - min_y) % SECTION_HEIGHT, pos.z)] >> (4 * (pos.x % 2))) & 0x0F;
688 }
689
690 void Chunk::SetSkyLight(const Position& pos, const unsigned char v)
691 {
692 if (!has_sky_light || !IsInsideChunk(pos, true))
693 {
694 return;
695 }
696
697 const int section_y = (pos.y - min_y) / SECTION_HEIGHT;
698 if (!sections[section_y])
699 {
700 AddSection(section_y);
701 }
702
703 unsigned char* packed_value = sections[section_y]->sky_light.data() + Section::CoordsToLightIndex(pos.x, (pos.y - min_y) % SECTION_HEIGHT, pos.z);
704 if (pos.x % 2 == 1)
705 {
706 const unsigned char first_value = *packed_value & 0x0F;
707 *packed_value = first_value | ((v & 0x0F) << 4);
708 }
709 else
710 {
711 const unsigned char second_value = *packed_value & 0xF0;
712 *packed_value = second_value | (v & 0x0F);
713 }
714
715 // Not necessary as we don't render lights
716//#if USE_GUI
717// modified_since_last_rendered = true;
718//#endif
719 }
720
722 {
723 return dimension_index;
724 }
725
727 {
728 return has_sky_light;
729 }
730
731 bool Chunk::HasSection(const int y) const
732 {
733 return sections[y] != nullptr;
734 }
735
736 void Chunk::AddSection(const int y)
737 {
738 sections[y] = std::make_unique<Section>(has_sky_light);
739 }
740
741#if PROTOCOL_VERSION < 552 /* < 1.15 */
742 const Biome* Chunk::GetBiome(const int x, const int z) const
743 {
744 if (x < 0 || x > CHUNK_WIDTH - 1 || z < 0 || z > CHUNK_WIDTH - 1)
745 {
746 return nullptr;
747 }
748
750 }
751
752 void Chunk::SetBiome(const int x, const int z, const int b)
753 {
754 if (x < 0 || x > CHUNK_WIDTH - 1 || z < 0 || z > CHUNK_WIDTH - 1)
755 {
756 return;
757 }
758
759 biomes[z * CHUNK_WIDTH + x] = static_cast<unsigned char>(b);
760
761#if USE_GUI
763#endif
764 }
765#else
766 const Biome* Chunk::GetBiome(const int x, const int y, const int z) const
767 {
768 // y / 4 * 16 + z / 4 * 4 + x / 4
769 return GetBiome((((y - min_y) >> 2) & 63) << 4 | ((z >> 2) & 3) << 2 | ((x >> 2) & 3));
770 }
771
772 const Biome* Chunk::GetBiome(const int i) const
773 {
774 if (i < 0 || i > biomes.size() - 1)
775 {
776 return nullptr;
777 }
778
780 }
781
782 void Chunk::SetBiomes(const std::vector<int>& new_biomes)
783 {
784 if (new_biomes.size() != 64 * height / SECTION_HEIGHT)
785 {
786 LOG_ERROR("Trying to set biomes with a wrong size");
787 return;
788 }
789 biomes = std::vector<unsigned char>(new_biomes.size());
790 for (size_t idx = 0; idx < new_biomes.size(); ++idx)
791 {
792 biomes[idx] = static_cast<unsigned char>(new_biomes[idx]);
793 }
794
795#if USE_GUI
797#endif
798 }
799
800 void Chunk::SetBiome(const int x, const int y, const int z, const int new_biome)
801 {
802 // y / 4 * 16 + z / 4 * 4 + x / 4
803 SetBiome((((y - min_y) >> 2) & 63) << 4 | ((z >> 2) & 3) << 2 | ((x >> 2) & 3), new_biome);
804 }
805
806 void Chunk::SetBiome(const int i, const int new_biome)
807 {
808 if (i < 0 || i > biomes.size() - 1)
809 {
810 return;
811 }
812
813 biomes[i] = static_cast<unsigned char>(new_biome);
814
815#if USE_GUI
817#endif
818 }
819#endif
820
821#if PROTOCOL_VERSION > 761 /* > 1.19.3 */
822 void Botcraft::Chunk::LoadBiomesData(const std::vector<unsigned char>& data)
823 {
824 if (data.size() == 0)
825 {
826 LOG_WARNING("Cannot load chunk biomes data without data");
827 return;
828 }
829
830 std::vector<unsigned char>::const_iterator iter = data.begin();
831 size_t length = data.size();
832 for (int section_y = 0; section_y < height / SECTION_HEIGHT; ++section_y)
833 {
834 std::vector<int> biomes = ReadPalettedContainer(iter, length, SECTION_HEIGHT / 4 * CHUNK_WIDTH / 4 * CHUNK_WIDTH / 4, 1, 3);
835 if (biomes.empty())
836 {
837 return;
838 }
839 for (size_t i = 0; i < biomes.size(); ++i)
840 {
841 SetBiome(section_y * SECTION_HEIGHT / 4 * CHUNK_WIDTH / 4 * CHUNK_WIDTH / 4 + static_cast<int>(i), biomes[i]);
842 }
843 }
844 }
845#endif
846
847 void Chunk::UpdateNeighbour(Chunk* const neighbour, const Orientation direction)
848 {
849#if USE_GUI
850 Position this_dest_position = Position(0, 0, 0);
851 Position this_src_position = Position(0, 0, 0);
852 Position neighbour_dest_position = Position(0, 0, 0);
853 Position neighbour_src_position = Position(0, 0, 0);
854 switch (direction)
855 {
856 // The neighbour is on "the left" on the x axis
858 this_dest_position.x = -1;
859 this_src_position.x = 0;
860 neighbour_src_position.x = CHUNK_WIDTH - 1;
861 neighbour_dest_position.x = CHUNK_WIDTH;
862
863 for (int y = min_y; y < height + min_y; ++y)
864 {
865 this_dest_position.y = y;
866 this_src_position.y = y;
867 neighbour_src_position.y = y;
868 neighbour_dest_position.y = y;
869 for (int z = 0; z < CHUNK_WIDTH; ++z)
870 {
871 this_dest_position.z = z;
872 this_src_position.z = z;
873 neighbour_src_position.z = z;
874 neighbour_dest_position.z = z;
875 // If the neighbour chunk is not loaded, just add air instead
876 if (neighbour == nullptr)
877 {
878 SetBlock(this_dest_position, nullptr);
879 }
880 // Otherwise, get the blocks and set this chunk's blocks
881 // on the neighbour
882 else
883 {
884 SetBlock(this_dest_position, neighbour->GetBlock(neighbour_src_position));
885 neighbour->SetBlock(neighbour_dest_position, GetBlock(this_src_position));
886 }
887 }
888 }
889 break;
890 // The neighbour is on "the right" on the x axis
892 this_dest_position.x = CHUNK_WIDTH;
893 this_src_position.x = CHUNK_WIDTH - 1;
894 neighbour_src_position.x = 0;
895 neighbour_dest_position.x = -1;
896
897 for (int y = min_y; y < height + min_y; ++y)
898 {
899 this_dest_position.y = y;
900 this_src_position.y = y;
901 neighbour_src_position.y = y;
902 neighbour_dest_position.y = y;
903 for (int z = 0; z < CHUNK_WIDTH; ++z)
904 {
905 this_dest_position.z = z;
906 this_src_position.z = z;
907 neighbour_src_position.z = z;
908 neighbour_dest_position.z = z;
909 // If the neighbour chunk is not loaded, just add air instead
910 if (neighbour == nullptr)
911 {
912 SetBlock(this_dest_position, nullptr);
913 }
914 // Otherwise, get the blocks and set this chunk's blocks
915 // on the neighbour
916 else
917 {
918 SetBlock(this_dest_position, neighbour->GetBlock(neighbour_src_position));
919 neighbour->SetBlock(neighbour_dest_position, GetBlock(this_src_position));
920 }
921 }
922 }
923 break;
924 // The neighbour is on "the left" on the z axis
926 this_dest_position.z = -1;
927 this_src_position.z = 0;
928 neighbour_src_position.z = CHUNK_WIDTH - 1;
929 neighbour_dest_position.z = CHUNK_WIDTH;
930
931 for (int y = min_y; y < height + min_y; ++y)
932 {
933 this_dest_position.y = y;
934 this_src_position.y = y;
935 neighbour_src_position.y = y;
936 neighbour_dest_position.y = y;
937 for (int x = 0; x < CHUNK_WIDTH; ++x)
938 {
939 this_dest_position.x = x;
940 this_src_position.x = x;
941 neighbour_src_position.x = x;
942 neighbour_dest_position.x = x;
943 // If the neighbour chunk is not loaded, just add air instead
944 if (neighbour == nullptr)
945 {
946 SetBlock(this_dest_position, nullptr);
947 }
948 // Otherwise, get the blocks and set this chunk's blocks
949 // on the neighbour
950 else
951 {
952 SetBlock(this_dest_position, neighbour->GetBlock(neighbour_src_position));
953 neighbour->SetBlock(neighbour_dest_position, GetBlock(this_src_position));
954 }
955 }
956 }
957 break;
958 // The neighbour is on "the right" on the z axis
960 this_dest_position.z = CHUNK_WIDTH;
961 this_src_position.z = CHUNK_WIDTH - 1;
962 neighbour_src_position.z = 0;
963 neighbour_dest_position.z = -1;
964
965 for (int y = min_y; y < height + min_y; ++y)
966 {
967 this_dest_position.y = y;
968 this_src_position.y = y;
969 neighbour_src_position.y = y;
970 neighbour_dest_position.y = y;
971 for (int x = 0; x < CHUNK_WIDTH; ++x)
972 {
973 this_dest_position.x = x;
974 this_src_position.x = x;
975 neighbour_src_position.x = x;
976 neighbour_dest_position.x = x;
977 // If the neighbour chunk is not loaded, just add air instead
978 if (neighbour == nullptr)
979 {
980 SetBlock(this_dest_position, nullptr);
981 }
982 // Otherwise, get the blocks and set this chunk's blocks
983 // on the neighbour
984 else
985 {
986 SetBlock(this_dest_position, neighbour->GetBlock(neighbour_src_position));
987 neighbour->SetBlock(neighbour_dest_position, GetBlock(this_src_position));
988 }
989 }
990 }
991 break;
992 default:
993 break;
994 }
995#endif
996 }
997
998 void Chunk::AddLoader(const std::thread::id& thread_id)
999 {
1000 loaded_from.insert(thread_id);
1001 }
1002
1003 size_t Chunk::RemoveLoader(const std::thread::id& thread_id)
1004 {
1005 loaded_from.erase(thread_id);
1006 return loaded_from.size();
1007 }
1008
1009 bool Chunk::IsInsideChunk(const Position& pos, const bool ignore_gui_borders) const
1010 {
1011 if (ignore_gui_borders)
1012 {
1013 return pos.x >= 0 && pos.x < CHUNK_WIDTH &&
1014 pos.y >= min_y && pos.y < height + min_y &&
1015 pos.z >= 0 && pos.z < CHUNK_WIDTH;
1016 }
1017#if USE_GUI
1018 return pos.x >= -1 && pos.x <= CHUNK_WIDTH &&
1019 pos.y >= min_y && pos.y < height + min_y &&
1020 pos.z >= -1 && pos.z <= CHUNK_WIDTH;
1021#else
1022 return pos.x >= 0 && pos.x < CHUNK_WIDTH &&
1023 pos.y >= min_y && pos.y < height + min_y &&
1024 pos.z >= 0 && pos.z < CHUNK_WIDTH;
1025#endif
1026 }
1027} //Botcraft
#define LOG_ERROR(osstream)
Definition Logger.hpp:45
#define LOG_WARNING(osstream)
Definition Logger.hpp:44
const Biome * GetBiome(const int id) const
static AssetsManager & getInstance()
const Blockstate * GetBlockstate(const BlockstateId id) const
BlockstateId GetId() const
void AddSection(const int y)
Definition Chunk.cpp:736
size_t dimension_index
Definition Chunk.hpp:114
unsigned char GetSkyLight(const Position &pos) const
Definition Chunk.cpp:674
std::vector< std::unique_ptr< Section > > sections
Definition Chunk.hpp:109
void SetBiomes(const std::vector< int > &new_biomes)
Definition Chunk.cpp:782
void AddLoader(const std::thread::id &thread_id)
Add a thread to the loaders list.
Definition Chunk.cpp:998
const Biome * GetBiome(const int x, const int y, const int z) const
Definition Chunk.cpp:766
std::vector< unsigned char > biomes
Definition Chunk.hpp:110
std::unordered_set< std::thread::id > loaded_from
Definition Chunk.hpp:127
bool IsInsideChunk(const Position &pos, const bool ignore_gui_borders) const
Definition Chunk.cpp:1009
void RemoveBlockEntityData(const Position &pos)
Definition Chunk.cpp:537
bool modified_since_last_rendered
Definition Chunk.hpp:125
bool GetHasSkyLight() const
Definition Chunk.cpp:726
bool HasSection(const int y) const
Definition Chunk.cpp:731
ProtocolCraft::NBT::Value GetBlockEntityData(const Position &pos) const
Definition Chunk.cpp:542
size_t RemoveLoader(const std::thread::id &thread_id)
Remove a thread from the loaders list.
Definition Chunk.cpp:1003
void LoadBiomesData(const std::vector< unsigned char > &data)
Definition Chunk.cpp:822
Chunk(const int min_y_, const unsigned int height_, const size_t dim_index, const bool has_sky_light_)
Definition Chunk.cpp:104
bool GetModifiedSinceLastRender() const
Definition Chunk.cpp:184
void UpdateNeighbour(Chunk *const neighbour, const Orientation direction)
Definition Chunk.cpp:847
size_t GetDimensionIndex() const
Definition Chunk.cpp:721
void LoadChunkData(const std::vector< unsigned char > &data)
Definition Chunk.cpp:422
int GetMinY() const
Definition Chunk.cpp:173
std::unordered_map< Position, ProtocolCraft::NBT::Value > block_entities_data
Definition Chunk.hpp:112
int GetHeight() const
Definition Chunk.cpp:178
const Blockstate * GetBlock(const Position &pos) const
Definition Chunk.cpp:553
void SetBlockEntityData(const Position &pos, const ProtocolCraft::NBT::Value &block_entity)
Definition Chunk.cpp:523
void SetBlockLight(const Position &pos, const unsigned char v)
Definition Chunk.cpp:644
void SetModifiedSinceLastRender(const bool b)
Definition Chunk.cpp:189
void SetBiome(const int x, const int y, const int z, const int new_biome)
Definition Chunk.cpp:800
void LoadChunkBlockEntitiesData(const std::vector< ProtocolCraft::BlockEntityInfo > &block_entities)
Definition Chunk.cpp:489
void SetSkyLight(const Position &pos, const unsigned char v)
Definition Chunk.cpp:690
unsigned char GetBlockLight(const Position &pos) const
Definition Chunk.cpp:628
static Position BlockCoordsToChunkCoords(const Position &pos)
Definition Chunk.cpp:164
bool has_sky_light
Definition Chunk.hpp:115
void SetBlock(const Position &pos, const Blockstate *block)
Definition Chunk.cpp:576
static constexpr int SECTION_HEIGHT
Definition Chunk.hpp:22
static constexpr int CHUNK_WIDTH
Definition Chunk.hpp:21
Vector3< int > Position
Definition Vector3.hpp:314
unsigned int BlockstateId
std::vector< unsigned char >::const_iterator ReadIterator
static size_t CoordsToLightIndex(const int x, const int y, const int z)
Definition Section.cpp:32
static size_t CoordsToBlockIndex(const int x, const int y, const int z)
Definition Section.cpp:23