chunk flags converted to a bitfield

This commit is contained in:
MihailRis
2024-05-31 11:59:12 +03:00
parent 3e0455358a
commit 3003386670
12 changed files with 41 additions and 77 deletions
+2 -2
View File
@@ -42,12 +42,12 @@ void Chunk::updateHeights() {
void Chunk::addBlockInventory(std::shared_ptr<Inventory> inventory,
uint x, uint y, uint z) {
inventories[vox_index(x, y, z)] = inventory;
setUnsaved(true);
flags.unsaved = true;
}
void Chunk::removeBlockInventory(uint x, uint y, uint z) {
if (inventories.erase(vox_index(x, y, z))) {
setUnsaved(true);
flags.unsaved = true;
}
}
+10 -43
View File
@@ -9,14 +9,6 @@
#include "voxel.hpp"
#include "../lighting/Lightmap.hpp"
struct ChunkFlag {
static const int MODIFIED = 0x1;
static const int READY = 0x2;
static const int LOADED = 0x4;
static const int LIGHTED = 0x8;
static const int UNSAVED = 0x10;
static const int LOADED_LIGHTS = 0x20;
};
inline constexpr int CHUNK_DATA_LEN = CHUNK_VOL*4;
class Lightmap;
@@ -31,7 +23,14 @@ public:
int bottom, top;
voxel voxels[CHUNK_VOL] {};
Lightmap lightmap;
int flags = 0;
struct {
bool modified: 1;
bool ready: 1;
bool loaded: 1;
bool lighted: 1;
bool unsaved: 1;
bool loadedLights: 1;
} flags {};
/* Block inventories map where key is index of block in voxels array */
chunk_inventories_map inventories;
@@ -45,14 +44,6 @@ public:
// unused
std::unique_ptr<Chunk> clone() const;
// flags getters/setters below
inline void setFlags(int mask, bool value){
if (value)
flags |= mask;
else
flags &= ~(mask);
}
/* Creates new block inventory given size
@return inventory id or 0 if block does not exists */
void addBlockInventory(std::shared_ptr<Inventory> inventory,
@@ -63,35 +54,11 @@ public:
/* @return inventory bound to the given block or nullptr */
std::shared_ptr<Inventory> getBlockInventory(uint x, uint y, uint z) const;
inline bool isUnsaved() const {return flags & ChunkFlag::UNSAVED;}
inline bool isModified() const {return flags & ChunkFlag::MODIFIED;}
inline bool isLighted() const {return flags & ChunkFlag::LIGHTED;}
inline bool isLoaded() const {return flags & ChunkFlag::LOADED;}
inline bool isLoadedLights() const {return flags & ChunkFlag::LOADED_LIGHTS;}
inline bool isReady() const {return flags & ChunkFlag::READY;}
inline void setUnsaved(bool newState) {setFlags(ChunkFlag::UNSAVED, newState);}
inline void setModified(bool newState) {setFlags(ChunkFlag::MODIFIED, newState);}
inline void setModifiedAndUnsaved() {
setModified(true);
setUnsaved(true);
flags.modified = true;
flags.unsaved = true;
}
inline void setLoaded(bool newState) {setFlags(ChunkFlag::LOADED, newState);}
inline void setLoadedLights(bool newState) {setFlags(ChunkFlag::LOADED_LIGHTS, newState);}
inline void setLighted(bool newState) {setFlags(ChunkFlag::LIGHTED, newState);}
inline void setReady(bool newState) {setFlags(ChunkFlag::READY, newState);}
std::unique_ptr<ubyte[]> encode() const;
/**
+7 -7
View File
@@ -186,14 +186,14 @@ void Chunks::set(int32_t x, int32_t y, int32_t z, uint32_t id, blockstate state)
else if (id == 0) chunk->updateHeights();
if (lx == 0 && (chunk = getChunk(cx+ox-1, cz+oz)))
chunk->setModified(true);
chunk->flags.modified = true;
if (lz == 0 && (chunk = getChunk(cx+ox, cz+oz-1)))
chunk->setModified(true);
chunk->flags.modified = true;
if (lx == CHUNK_W-1 && (chunk = getChunk(cx+ox+1, cz+oz)))
chunk->setModified(true);
chunk->flags.modified = true;
if (lz == CHUNK_D-1 && (chunk = getChunk(cx+ox, cz+oz+1)))
chunk->setModified(true);
chunk->flags.modified = true;
}
voxel* Chunks::rayCast(
@@ -497,12 +497,12 @@ void Chunks::saveAndClear(){
for (size_t i = 0; i < volume; i++){
Chunk* chunk = chunks[i].get();
chunks[i] = nullptr;
if (chunk == nullptr || !chunk->isLighted())
if (chunk == nullptr || !chunk->flags.lighted)
continue;
bool lightsUnsaved = !chunk->isLoadedLights() &&
bool lightsUnsaved = !chunk->flags.loadedLights &&
worldFiles->doesWriteLights();
if (!chunk->isUnsaved() && !lightsUnsaved)
if (!chunk->flags.unsaved && !lightsUnsaved)
continue;
regions.put(chunk);
}
+2 -2
View File
@@ -61,7 +61,7 @@ std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
chunk->decode(data.get());
auto invs = regions.fetchInventories(chunk->x, chunk->z);
chunk->setBlockInventories(std::move(invs));
chunk->setLoaded(true);
chunk->flags.loaded = true;
for(auto& entry : chunk->inventories) {
level->inventories->store(entry.second);
}
@@ -71,7 +71,7 @@ std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
auto lights = regions.getLights(chunk->x, chunk->z);
if (lights) {
chunk->lightmap.set(lights.get());
chunk->setLoadedLights(true);
chunk->flags.loadedLights = true;
}
return chunk;
}