inventory regions, minor refactor

This commit is contained in:
MihailRis 2024-01-31 00:14:43 +03:00
parent a57fba4fa4
commit 49fdcdfb27
29 changed files with 259 additions and 160 deletions

View file

@ -1,8 +1,13 @@
#ifndef VOXELS_CHUNK_H_
#define VOXELS_CHUNK_H_
#include <memory>
#include <stdlib.h>
#include <unordered_map>
#include "../constants.h"
#include "voxel.h"
#include "../lighting/Lightmap.h"
struct ChunkFlag {
static const int MODIFIED = 0x1;
@ -14,26 +19,29 @@ struct ChunkFlag {
};
#define CHUNK_DATA_LEN (CHUNK_VOL*4)
struct voxel;
class Lightmap;
class ContentLUT;
class Inventory;
class Chunk {
public:
int x, z;
int bottom, top;
voxel* voxels;
Lightmap* lightmap;
voxel voxels[CHUNK_VOL];
Lightmap lightmap;
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(int x, int z);
~Chunk();
bool isEmpty();
void updateHeights();
Chunk* clone() const;
// unused
std::unique_ptr<Chunk> clone() const;
// flags getters/setters below
inline void setFlags(int mask, bool value){
@ -43,6 +51,14 @@ public:
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,
uint x, uint y, uint z);
/* @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;}
@ -68,6 +84,10 @@ public:
inline void setReady(bool newState) {setFlags(ChunkFlag::READY, newState);}
ubyte* encode() const;
/**
* @return true if all is fine
**/
bool decode(ubyte* data);
static void convert(ubyte* data, const ContentLUT* lut);