14#if PROTOCOL_VERSION > 756
21#if PROTOCOL_VERSION > 756
26 std::vector<int> ReadPalettedContainer(
28 const int num_entries,
29 const unsigned char min_indirect_bits,
30 const unsigned char max_indirect_bits
33 unsigned char bits = ReadData<unsigned char>(iter, length);
35 std::vector<int> palette;
38 palette.push_back(ReadData<VarInt>(iter, length));
40 else if (bits <= max_indirect_bits)
42 bits = std::max(bits, min_indirect_bits);
43 palette.resize(ReadData<VarInt>(iter, length));
44 for (
int& p : palette)
46 p = ReadData<VarInt>(iter, length);
50 const int values_per_long = bits == 0 ? 0 : 64 / bits;
51#if PROTOCOL_VERSION < 770
52 const int data_array_size = ReadData<VarInt>(iter, length);
54 const int data_array_size = bits == 0 ? 0 : (num_entries + values_per_long - 1) / values_per_long;
58 const int expected_longs = (num_entries + values_per_long - 1) / values_per_long;
59 if (data_array_size != expected_longs)
61 LOG_ERROR(
"Invalid data array size (" << data_array_size <<
" instead of " << expected_longs <<
"). Stop loading current chunk data");
66 std::vector<unsigned long long int> data_array(data_array_size);
67 for (
unsigned long long int& l : data_array)
69 l = ReadData<unsigned long long int>(iter, length);
74 return std::vector<int>(num_entries, palette[0]);
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)
81 const int raw =
static_cast<int>((data_array[i / values_per_long] >> ((i % values_per_long) * bits)) & mask);
88 if (raw >=
static_cast<int>(palette.size()))
90 LOG_ERROR(
"Palette index " << raw <<
" out of range (" << palette.size() <<
"). Stop loading current chunk data");
93 output[i] = palette[raw];
101#if PROTOCOL_VERSION < 757
102 Chunk::Chunk(
const size_t dim_index,
const bool has_sky_light_)
104 Chunk::Chunk(
const int min_y_,
const unsigned int height_,
const size_t dim_index,
const bool has_sky_light_)
107 dimension_index = dim_index;
108 has_sky_light = has_sky_light_;
109#if PROTOCOL_VERSION > 756
114#if PROTOCOL_VERSION < 552
118 biomes = std::vector<unsigned char>(64 * height /
SECTION_HEIGHT, 0);
120 sections = std::vector<std::unique_ptr<Section> >(height /
SECTION_HEIGHT);
123 modified_since_last_rendered =
true;
133#if PROTOCOL_VERSION > 756
139 for (
size_t i = 0; i < c.
sections.size(); i++)
167 static_cast<int>(std::floor(pos.
x /
static_cast<double>(
CHUNK_WIDTH))),
169 static_cast<int>(std::floor(pos.
z /
static_cast<double>(
CHUNK_WIDTH)))
195#if PROTOCOL_VERSION < 757
196#if PROTOCOL_VERSION < 552
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
201 void Chunk::LoadChunkData(
const std::vector<unsigned char>& data,
const std::vector<unsigned long long int>& primary_bit_mask)
204 std::vector<unsigned char>::const_iterator iter = data.begin();
205 size_t length = data.size();
207 if (data.size() == 0)
209 LOG_ERROR(
"Cannot load chunk data without data");
214 for (
int sectionY = 0; sectionY < height /
SECTION_HEIGHT; ++sectionY)
216#if PROTOCOL_VERSION < 755
217 if (!(primary_bit_mask & (1 << sectionY)))
219 if (!(primary_bit_mask[sectionY / 64] & (1 << (sectionY % 64))))
225#if PROTOCOL_VERSION > 404
226 short block_count = ReadData<short>(iter, length);
228 unsigned char bits_per_block = ReadData<unsigned char>(iter, length);
232#if PROTOCOL_VERSION < 384
233 int palette_length = ReadData<VarInt>(iter, length);
235 int palette_length = 0;
238 palette_length = ReadData<VarInt>(iter, length);
241 std::vector<int> palette(palette_length);
244 if (bits_per_block < 4)
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");
250 for (
int i = 0; i < palette_length; ++i)
252 palette[i] = ReadData<VarInt>(iter, length);
257 if (palette_length != 0)
259 LOG_ERROR(
"Palette length should be 0 for global palette (current: " << palette_length <<
"). Stop loading current chunk data");
265 unsigned int individual_value_mask =
static_cast<unsigned int>((1 << bits_per_block) - 1);
268 int data_array_size = ReadData<VarInt>(iter, length);
270#if PROTOCOL_VERSION > 712
271 const int values_per_long = 64 / bits_per_block;
276 if(data_array_size != expected_longs)
278 LOG_ERROR(
"Invalid chunk data array size (" << data_array_size <<
" instead of " << expected_longs <<
"). Stop loading current chunk data");
283 std::vector<unsigned long long int> data_array(data_array_size);
284 for (
int i = 0; i < data_array_size; ++i)
286 data_array[i] = ReadData<unsigned long long int>(iter, length);
290#if PROTOCOL_VERSION > 712
297 for (
int block_z = 0; block_z <
CHUNK_WIDTH; ++block_z)
300 for (
int block_x = 0; block_x <
CHUNK_WIDTH; ++block_x)
303#if PROTOCOL_VERSION > 712
306 if (64 - (bit_offset % 64) < bits_per_block)
308 bit_offset += 64 - (bit_offset % 64);
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;
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;
321 if (start_long_index == end_long_index)
323 raw_id =
static_cast<unsigned int>(data_array[start_long_index] >> start_offset);
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);
330 raw_id &= individual_value_mask;
334 if (raw_id >= palette.size())
336 LOG_ERROR(
"Palette index " << raw_id <<
" out of range (" << palette.size() <<
"). Stop loading current chunk data");
339 raw_id = palette[raw_id];
342#if PROTOCOL_VERSION < 347
344 unsigned char metadata;
346 Blockstate::IdToIdMetadata(raw_id,
id, metadata);
348 SetBlock(pos, { id, metadata });
350 SetBlock(pos, raw_id);
356#if PROTOCOL_VERSION <= 404
361 for (
int block_z = 0; block_z <
CHUNK_WIDTH; ++block_z)
364 for (
int block_x = 0; block_x <
CHUNK_WIDTH; block_x += 2)
367 unsigned char two_light_values = ReadData<unsigned char>(iter, length);
369 SetBlockLight(pos, two_light_values & 0x0F);
371 SetBlockLight(pos, (two_light_values >> 4) & 0x0F);
382 for (
int block_z = 0; block_z <
CHUNK_WIDTH; ++block_z)
385 for (
int block_x = 0; block_x <
CHUNK_WIDTH; block_x += 2)
388 unsigned char two_light_values = ReadData<unsigned char>(iter, length);
390 SetSkyLight(pos, two_light_values & 0x0F);
392 SetSkyLight(pos, (two_light_values >> 4) & 0x0F);
400#if PROTOCOL_VERSION < 552
402 if (ground_up_continuous)
404 for (
int block_z = 0; block_z <
CHUNK_WIDTH; ++block_z)
406 for (
int block_x = 0; block_x <
CHUNK_WIDTH; ++block_x)
408#if PROTOCOL_VERSION < 358
409 SetBiome(block_x, block_z, ReadData<unsigned char>(iter, length));
411 SetBiome(block_x, block_z, ReadData<int>(iter, length));
418 modified_since_last_rendered =
true;
424 std::vector<unsigned char>::const_iterator iter = data.begin();
425 size_t length = data.size();
427 if (data.size() == 0)
429 LOG_WARNING(
"Cannot load chunk data without data");
436 short block_count = ReadData<short>(iter, length);
437#if PROTOCOL_VERSION > 774
438 short fluid_count = ReadData<short>(iter, length);
447 if (block_count == 0)
458 for (
int block_z = 0; block_z <
CHUNK_WIDTH; ++block_z)
461 for (
int block_x = 0; block_x <
CHUNK_WIDTH; ++block_x)
475 for (
size_t i = 0; i <
biomes.size(); ++i)
486#if PROTOCOL_VERSION < 757
493 block_entities_data.clear();
495 for (
int i = 0; i < block_entities.size(); ++i)
497#if PROTOCOL_VERSION < 757
498 if (block_entities[i].HasData())
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>())
511 const int x = (block_entities[i].GetPackedXZ() >> 4) & 15;
512 const int z = (block_entities[i].GetPackedXZ() & 15);
519 modified_since_last_rendered =
true;
566#if PROTOCOL_VERSION < 347
569 Blockstate::IdToIdMetadata(
static_cast<unsigned int>(stored_id), block_id.first, block_id.second);
578 if (block ==
nullptr)
580#if PROTOCOL_VERSION < 347
602#if PROTOCOL_VERSION < 347
616#if PROTOCOL_VERSION < 347
617 const unsigned short block_id =
static_cast<unsigned short>(Blockstate::IdMetadataToId(
id.first,
id.second));
619 const unsigned short block_id =
static_cast<unsigned short>(id);
660 const unsigned char first_value = *packed_value & 0x0F;
661 *packed_value = first_value | ((v & 0x0F) << 4);
665 const unsigned char second_value = *packed_value & 0xF0;
666 *packed_value = second_value | (v & 0x0F);
706 const unsigned char first_value = *packed_value & 0x0F;
707 *packed_value = first_value | ((v & 0x0F) << 4);
711 const unsigned char second_value = *packed_value & 0xF0;
712 *packed_value = second_value | (v & 0x0F);
741#if PROTOCOL_VERSION < 552
769 return GetBiome((((y -
min_y) >> 2) & 63) << 4 | ((z >> 2) & 3) << 2 | ((x >> 2) & 3));
774 if (i < 0 || i >
biomes.size() - 1)
786 LOG_ERROR(
"Trying to set biomes with a wrong size");
789 biomes = std::vector<unsigned char>(new_biomes.size());
790 for (
size_t idx = 0; idx < new_biomes.size(); ++idx)
792 biomes[idx] =
static_cast<unsigned char>(new_biomes[idx]);
803 SetBiome((((y -
min_y) >> 2) & 63) << 4 | ((z >> 2) & 3) << 2 | ((x >> 2) & 3), new_biome);
808 if (i < 0 || i >
biomes.size() - 1)
813 biomes[i] =
static_cast<unsigned char>(new_biome);
821#if PROTOCOL_VERSION > 761
824 if (data.size() == 0)
826 LOG_WARNING(
"Cannot load chunk biomes data without data");
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)
839 for (
size_t i = 0; i < biomes.size(); ++i)
858 this_dest_position.
x = -1;
859 this_src_position.
x = 0;
865 this_dest_position.
y = y;
866 this_src_position.
y = y;
867 neighbour_src_position.
y = y;
868 neighbour_dest_position.
y = y;
871 this_dest_position.
z = z;
872 this_src_position.
z = z;
873 neighbour_src_position.
z = z;
874 neighbour_dest_position.
z = z;
876 if (neighbour ==
nullptr)
878 SetBlock(this_dest_position,
nullptr);
894 neighbour_src_position.
x = 0;
895 neighbour_dest_position.
x = -1;
899 this_dest_position.
y = y;
900 this_src_position.
y = y;
901 neighbour_src_position.
y = y;
902 neighbour_dest_position.
y = y;
905 this_dest_position.
z = z;
906 this_src_position.
z = z;
907 neighbour_src_position.
z = z;
908 neighbour_dest_position.
z = z;
910 if (neighbour ==
nullptr)
912 SetBlock(this_dest_position,
nullptr);
926 this_dest_position.
z = -1;
927 this_src_position.
z = 0;
933 this_dest_position.
y = y;
934 this_src_position.
y = y;
935 neighbour_src_position.
y = y;
936 neighbour_dest_position.
y = y;
939 this_dest_position.
x = x;
940 this_src_position.
x = x;
941 neighbour_src_position.
x = x;
942 neighbour_dest_position.
x = x;
944 if (neighbour ==
nullptr)
946 SetBlock(this_dest_position,
nullptr);
962 neighbour_src_position.
z = 0;
963 neighbour_dest_position.
z = -1;
967 this_dest_position.
y = y;
968 this_src_position.
y = y;
969 neighbour_src_position.
y = y;
970 neighbour_dest_position.
y = y;
973 this_dest_position.
x = x;
974 this_src_position.
x = x;
975 neighbour_src_position.
x = x;
976 neighbour_dest_position.
x = x;
978 if (neighbour ==
nullptr)
980 SetBlock(this_dest_position,
nullptr);
1011 if (ignore_gui_borders)
#define LOG_ERROR(osstream)
#define LOG_WARNING(osstream)
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)
unsigned char GetSkyLight(const Position &pos) const
std::vector< std::unique_ptr< Section > > sections
void SetBiomes(const std::vector< int > &new_biomes)
void AddLoader(const std::thread::id &thread_id)
Add a thread to the loaders list.
const Biome * GetBiome(const int x, const int y, const int z) const
std::vector< unsigned char > biomes
std::unordered_set< std::thread::id > loaded_from
bool IsInsideChunk(const Position &pos, const bool ignore_gui_borders) const
void RemoveBlockEntityData(const Position &pos)
bool modified_since_last_rendered
bool GetHasSkyLight() const
bool HasSection(const int y) const
ProtocolCraft::NBT::Value GetBlockEntityData(const Position &pos) const
size_t RemoveLoader(const std::thread::id &thread_id)
Remove a thread from the loaders list.
void LoadBiomesData(const std::vector< unsigned char > &data)
Chunk(const int min_y_, const unsigned int height_, const size_t dim_index, const bool has_sky_light_)
bool GetModifiedSinceLastRender() const
void UpdateNeighbour(Chunk *const neighbour, const Orientation direction)
size_t GetDimensionIndex() const
void LoadChunkData(const std::vector< unsigned char > &data)
std::unordered_map< Position, ProtocolCraft::NBT::Value > block_entities_data
const Blockstate * GetBlock(const Position &pos) const
void SetBlockEntityData(const Position &pos, const ProtocolCraft::NBT::Value &block_entity)
void SetBlockLight(const Position &pos, const unsigned char v)
void SetModifiedSinceLastRender(const bool b)
void SetBiome(const int x, const int y, const int z, const int new_biome)
void LoadChunkBlockEntitiesData(const std::vector< ProtocolCraft::BlockEntityInfo > &block_entities)
void SetSkyLight(const Position &pos, const unsigned char v)
unsigned char GetBlockLight(const Position &pos) const
static Position BlockCoordsToChunkCoords(const Position &pos)
void SetBlock(const Position &pos, const Blockstate *block)
static constexpr int SECTION_HEIGHT
static constexpr int CHUNK_WIDTH
unsigned int BlockstateId
std::vector< unsigned char >::const_iterator ReadIterator
static size_t CoordsToLightIndex(const int x, const int y, const int z)
static size_t CoordsToBlockIndex(const int x, const int y, const int z)