55 lines
1.6 KiB
C++
55 lines
1.6 KiB
C++
#include "Chunk.hpp"
|
|
|
|
#include <algorithm>
|
|
#include "../../core/data/math.hpp"
|
|
#include <Tracy.hpp>
|
|
|
|
using namespace world::server;
|
|
|
|
Chunk::Chunk(const chunk_pos& pos, const std::unique_ptr<generator::Abstract>& rnd): world::Chunk() {
|
|
rnd->generate(pos, voxels);
|
|
}
|
|
#include <iostream>
|
|
Chunk::Chunk(std::istream& str, bool rle): world::Chunk(str, rle) { }
|
|
Chunk::~Chunk() { }
|
|
|
|
void Chunk::write(std::ostream& str, bool rle) const {
|
|
if (rle) {
|
|
const auto *it = voxels.begin();
|
|
uint16_t counter = 1;
|
|
Voxel current = *it;
|
|
while(true) {
|
|
++it;
|
|
const auto end = (it == voxels.end());
|
|
if(end || current.value != it->value) {
|
|
str.write(reinterpret_cast<char *>(&counter), sizeof(counter));
|
|
str.write(reinterpret_cast<char *>(¤t), sizeof(current));
|
|
if(end)
|
|
break;
|
|
|
|
current = *it;
|
|
counter = 1;
|
|
} else {
|
|
counter++;
|
|
}
|
|
}
|
|
} else {
|
|
for(auto current: voxels) {
|
|
str.write(reinterpret_cast<char *>(¤t), sizeof(current));
|
|
}
|
|
}
|
|
}
|
|
|
|
void Chunk::set(uint16_t idx, const Voxel& val) {
|
|
modified = modified || (voxels[idx].value != val.value);
|
|
voxels[idx] = val;
|
|
}
|
|
void Chunk::setAt(const chunk_voxel_pos& pos, const Voxel& val) {
|
|
set(glm::toIdx(pos), val);
|
|
}
|
|
std::optional<world::Item> Chunk::replace(chunk_voxel_idx idx, const Voxel& val, float) {
|
|
const auto res = voxels[idx];
|
|
set(idx, val);
|
|
return {world::Item{res.density(), res.material()}};
|
|
}
|