block inventories (backend)

This commit is contained in:
MihailRis
2024-02-12 18:28:18 +03:00
parent fe57c38edf
commit 87f50f79c9
12 changed files with 108 additions and 18 deletions
+10
View File
@@ -49,6 +49,16 @@ void Chunk::addBlockInventory(std::shared_ptr<Inventory> inventory,
setUnsaved(true);
}
void Chunk::removeBlockInventory(uint x, uint y, uint z) {
if (inventories.erase(vox_index(x, y, z))) {
setUnsaved(true);
}
}
void Chunk::setBlockInventories(chunk_inventories_map map) {
inventories = map;
}
std::shared_ptr<Inventory> Chunk::getBlockInventory(uint x, uint y, uint z) const {
if (x >= CHUNK_W || y >= CHUNK_H || z >= CHUNK_D)
return nullptr;
+5 -1
View File
@@ -23,6 +23,8 @@ class Lightmap;
class ContentLUT;
class Inventory;
using chunk_inventories_map = std::unordered_map<uint, std::shared_ptr<Inventory>>;
class Chunk {
public:
int x, z;
@@ -32,7 +34,7 @@ public:
int flags = 0;
/* Block inventories map where key is index of block in voxels array */
std::unordered_map<uint, std::shared_ptr<Inventory>> inventories;
chunk_inventories_map inventories;
Chunk(int x, int z);
@@ -55,6 +57,8 @@ public:
@return inventory id or 0 if block does not exists */
void addBlockInventory(std::shared_ptr<Inventory> inventory,
uint x, uint y, uint z);
void removeBlockInventory(uint x, uint y, uint z);
void setBlockInventories(chunk_inventories_map map);
/* @return inventory bound to the given block or nullptr */
std::shared_ptr<Inventory> getBlockInventory(uint x, uint y, uint z) const;
+3
View File
@@ -162,6 +162,9 @@ void Chunks::set(int x, int y, int z, int id, uint8_t states){
int lz = z - cz * CHUNK_D;
voxel& vox = chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx];
auto def = contentIds->getBlockDef(vox.id);
if (def->inventorySize == 0)
chunk->removeBlockInventory(lx, y, lz);
vox.id = id;
vox.states = states;
+2
View File
@@ -57,6 +57,8 @@ std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
std::unique_ptr<ubyte[]> data(wfile->getChunk(chunk->x, chunk->z));
if (data) {
chunk->decode(data.get());
auto invs = wfile->fetchInventories(chunk->x, chunk->z);
chunk->setBlockInventories(std::move(invs));
chunk->setLoaded(true);
verifyLoadedChunk(level->content->getIndices(), chunk.get());
}