WorldFiles refactor

This commit is contained in:
MihailRis
2023-12-06 12:57:08 +03:00
parent 40d468f184
commit 93182869d3
2 changed files with 31 additions and 11 deletions
+23 -8
View File
@@ -58,22 +58,37 @@ void int2Bytes(int value, ubyte* dest, size_t offset){
WorldRegion::WorldRegion() { WorldRegion::WorldRegion() {
chunksData = new ubyte*[REGION_VOL]{}; chunksData = new ubyte*[REGION_VOL]{};
compressedSizes = new uint32_t[REGION_VOL]{}; sizes = new uint32_t[REGION_VOL]{};
} }
WorldRegion::~WorldRegion() { WorldRegion::~WorldRegion() {
for (uint i = 0; i < REGION_VOL; i++) { for (uint i = 0; i < REGION_VOL; i++) {
delete[] chunksData[i]; delete[] chunksData[i];
} }
delete[] compressedSizes; delete[] sizes;
delete[] chunksData; delete[] chunksData;
} }
void WorldRegion::setUnsaved(bool unsaved) {
this->unsaved = unsaved;
}
bool WorldRegion::isUnsaved() const {
return unsaved;
}
ubyte** WorldRegion::getChunks() const {
return chunksData;
}
uint32_t* WorldRegion::getSizes() const {
return sizes;
}
void WorldRegion::put(uint x, uint z, ubyte* data, uint32_t size) { void WorldRegion::put(uint x, uint z, ubyte* data, uint32_t size) {
size_t chunk_index = z * REGION_SIZE + x; size_t chunk_index = z * REGION_SIZE + x;
delete[] chunksData[chunk_index]; delete[] chunksData[chunk_index];
chunksData[chunk_index] = data; chunksData[chunk_index] = data;
compressedSizes[chunk_index] = size; sizes[chunk_index] = size;
} }
ubyte* WorldRegion::get(uint x, uint z) { ubyte* WorldRegion::get(uint x, uint z) {
@@ -81,7 +96,7 @@ ubyte* WorldRegion::get(uint x, uint z) {
} }
uint WorldRegion::getSize(uint x, uint z) { uint WorldRegion::getSize(uint x, uint z) {
return compressedSizes[z * REGION_SIZE + x]; return sizes[z * REGION_SIZE + x];
} }
WorldFiles::WorldFiles(path directory, bool generatorTestMode) WorldFiles::WorldFiles(path directory, bool generatorTestMode)
@@ -130,7 +145,7 @@ void WorldFiles::put(Chunk* chunk){
region = new WorldRegion(); region = new WorldRegion();
regions[ivec2(regionX, regionZ)] = region; regions[ivec2(regionX, regionZ)] = region;
} }
region->unsaved = true; region->setUnsaved(true);
int localX = chunk->x - (regionX * REGION_SIZE); int localX = chunk->x - (regionX * REGION_SIZE);
int localZ = chunk->z - (regionZ * REGION_SIZE); int localZ = chunk->z - (regionZ * REGION_SIZE);
@@ -235,8 +250,8 @@ ubyte* WorldFiles::readChunkData(int x, int z, uint32_t& length, path filename){
} }
void WorldFiles::writeRegion(int x, int y, WorldRegion* entry, path filename){ void WorldFiles::writeRegion(int x, int y, WorldRegion* entry, path filename){
ubyte** region = entry->chunksData; ubyte** region = entry->getChunks();
uint32_t* sizes = entry->compressedSizes; uint32_t* sizes = entry->getSizes();
for (size_t i = 0; i < REGION_VOL; i++) { for (size_t i = 0; i < REGION_VOL; i++) {
int chunk_x = (i % REGION_SIZE) + x * REGION_SIZE; int chunk_x = (i % REGION_SIZE) + x * REGION_SIZE;
int chunk_z = (i / REGION_SIZE) + y * REGION_SIZE; int chunk_z = (i / REGION_SIZE) + y * REGION_SIZE;
@@ -286,7 +301,7 @@ void WorldFiles::write(const World* world, const Content* content) {
return; return;
writeIndices(content->indices); writeIndices(content->indices);
for (auto it = regions.begin(); it != regions.end(); it++){ for (auto it = regions.begin(); it != regions.end(); it++){
if (it->second->chunksData == nullptr || !it->second->unsaved) if (it->second->getChunks() == nullptr || !it->second->isUnsaved())
continue; continue;
ivec2 key = it->first; ivec2 key = it->first;
writeRegion(key.x, key.y, it->second, getRegionFile(key.x, key.y)); writeRegion(key.x, key.y, it->second, getRegionFile(key.x, key.y));
+8 -3
View File
@@ -28,17 +28,22 @@ class ContentIndices;
class World; class World;
class WorldRegion { class WorldRegion {
public:
ubyte** chunksData; ubyte** chunksData;
uint32_t* compressedSizes; uint32_t* sizes;
bool unsaved = true; bool unsaved = true;
public:
WorldRegion(); WorldRegion();
~WorldRegion(); ~WorldRegion();
void put(uint x, uint z, ubyte* data, uint32_t size); void put(uint x, uint z, ubyte* data, uint32_t size);
ubyte* get(uint x, uint z); ubyte* get(uint x, uint z);
uint getSize(uint x, uint z); uint getSize(uint x, uint z);
void setUnsaved(bool unsaved);
bool isUnsaved() const;
ubyte** getChunks() const;
uint32_t* getSizes() const;
}; };
class WorldFiles { class WorldFiles {