From dcd030c6ed41e080846d1d50cf82e1055ae48edd Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Fri, 15 Sep 2017 20:46:56 +0500 Subject: 2017-09-15 --- src/Section.cpp | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) (limited to 'src/Section.cpp') diff --git a/src/Section.cpp b/src/Section.cpp index 554cdbf..67dde70 100644 --- a/src/Section.cpp +++ b/src/Section.cpp @@ -2,14 +2,26 @@ #include -void Section::CalculateHash() { - std::vector rawData; - rawData.reserve(block.size() * sizeof(long long) + light.size() + sky.size()); - std::copy(block.begin(), block.end(), std::back_inserter(rawData)); - std::copy(light.begin(), light.end(), std::back_inserter(rawData)); +void Section::CalculateHash() const { + size_t offset = 0; + + std::vector rawData; + rawData.resize(block.size() * sizeof(long long) + light.size() + sky.size()); + + std::memcpy(rawData.data() + offset, block.data(), block.size() * sizeof(BlockId)); + offset += block.size() * sizeof(BlockId); + + std::memcpy(rawData.data() + offset, light.data(), light.size() * sizeof(unsigned char)); + offset += light.size() * sizeof(unsigned char); + if (!sky.empty()) - std::copy(sky.begin(), sky.end(), std::back_inserter(rawData)); + std::memcpy(rawData.data() + offset, sky.data(), sky.size() * sizeof(unsigned char)); + for (auto& it : overrideList) { + rawData.push_back(*reinterpret_cast (&it.second) & 0xF); + rawData.push_back(*reinterpret_cast (&it.second) >> 0xF); + } + const unsigned char *from = reinterpret_cast(rawData.data()); size_t length = rawData.size(); @@ -30,12 +42,10 @@ Section::Section(Vector pos, unsigned char bitsPerBlock, std::vectorlight = std::move(lightData); this->sky = std::move(skyData); - CalculateHash(); + hash = -1; } -Section::Section() { - - CalculateHash(); +Section::Section():hash(-1),bitsPerBlock(0) { } Section::~Section() { @@ -45,19 +55,24 @@ Section::~Section() { Section::Section(Section && other) noexcept { using std::swap; swap(*this, other); - CalculateHash(); + hash = -1; } Section &Section::operator=(Section other) noexcept { using std::swap; swap(*this, other); - CalculateHash(); + hash = -1; return *this; } BlockId Section::GetBlockId(Vector pos) const { if (block.empty()) return BlockId{ 0,0 }; + + auto iter = overrideList.find(pos); + if (iter != overrideList.end()) + return iter->second; + int value; unsigned char individualValueMask = ((1 << bitsPerBlock) - 1); @@ -67,7 +82,7 @@ BlockId Section::GetBlockId(Vector pos) const { int startOffset = (blockNumber * bitsPerBlock) % 64; int endLong = ((blockNumber + 1) * bitsPerBlock - 1) / 64; - unsigned short t; + unsigned char t; if (startLong == endLong) { t = (block[startLong] >> startOffset); @@ -108,7 +123,8 @@ unsigned char Section::GetBlockSkyLight(Vector pos) const } void Section::SetBlockId(Vector pos, BlockId value) { - LOG(WARNING) << "Block changing not implemented!"; + overrideList[pos] = value; + hash = -1; } void swap(Section& lhs, Section& rhs) noexcept { @@ -137,5 +153,7 @@ Vector Section::GetPosition() const { } size_t Section::GetHash() const { + if (hash == -1) + CalculateHash(); return hash; } \ No newline at end of file -- cgit v1.2.3