inventory regions, minor refactor
This commit is contained in:
@@ -97,6 +97,7 @@ public:
|
||||
BlockRotProfile rotations;
|
||||
std::string pickingItem = name+BLOCK_ITEM_SUFFIX;
|
||||
std::string scriptName = name.substr(name.find(':')+1);
|
||||
uint inventorySize = 0;
|
||||
|
||||
struct {
|
||||
blockid_t id;
|
||||
|
||||
+29
-23
@@ -1,30 +1,23 @@
|
||||
#include "Chunk.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "voxel.h"
|
||||
|
||||
#include "../items/Inventory.h"
|
||||
#include "../content/ContentLUT.h"
|
||||
#include "../lighting/Lightmap.h"
|
||||
|
||||
Chunk::Chunk(int xpos, int zpos) : x(xpos), z(zpos){
|
||||
bottom = 0;
|
||||
top = CHUNK_H;
|
||||
voxels = new voxel[CHUNK_VOL];
|
||||
for (size_t i = 0; i < CHUNK_VOL; i++) {
|
||||
for (uint i = 0; i < CHUNK_VOL; i++) {
|
||||
voxels[i].id = 2;
|
||||
voxels[i].states = 0;
|
||||
}
|
||||
lightmap = new Lightmap();
|
||||
}
|
||||
|
||||
Chunk::~Chunk(){
|
||||
delete lightmap;
|
||||
delete[] voxels;
|
||||
}
|
||||
|
||||
bool Chunk::isEmpty(){
|
||||
int id = -1;
|
||||
for (size_t i = 0; i < CHUNK_VOL; i++){
|
||||
for (uint i = 0; i < CHUNK_VOL; i++){
|
||||
if (voxels[i].id != id){
|
||||
if (id != -1)
|
||||
return false;
|
||||
@@ -36,13 +29,12 @@ bool Chunk::isEmpty(){
|
||||
}
|
||||
|
||||
void Chunk::updateHeights() {
|
||||
for (size_t i = 0; i < CHUNK_VOL; i++) {
|
||||
for (uint i = 0; i < CHUNK_VOL; i++) {
|
||||
if (voxels[i].id != 0) {
|
||||
bottom = i / (CHUNK_D * CHUNK_W);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = CHUNK_VOL - 1; i >= 0; i--) {
|
||||
if (voxels[i].id != 0) {
|
||||
top = i / (CHUNK_D * CHUNK_W) + 1;
|
||||
@@ -51,11 +43,28 @@ void Chunk::updateHeights() {
|
||||
}
|
||||
}
|
||||
|
||||
Chunk* Chunk::clone() const {
|
||||
Chunk* other = new Chunk(x,z);
|
||||
for (size_t i = 0; i < CHUNK_VOL; i++)
|
||||
void Chunk::addBlockInventory(std::shared_ptr<Inventory> inventory,
|
||||
uint x, uint y, uint z) {
|
||||
inventories[vox_index(x, y, z)] = inventory;
|
||||
setUnsaved(true);
|
||||
}
|
||||
|
||||
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;
|
||||
const auto& found = inventories.find(vox_index(x, y, z));
|
||||
if (found == inventories.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return found->second;
|
||||
}
|
||||
|
||||
std::unique_ptr<Chunk> Chunk::clone() const {
|
||||
auto other = std::make_unique<Chunk>(x,z);
|
||||
for (uint i = 0; i < CHUNK_VOL; i++) {
|
||||
other->voxels[i] = voxels[i];
|
||||
other->lightmap->set(lightmap);
|
||||
}
|
||||
other->lightmap.set(&lightmap);
|
||||
return other;
|
||||
}
|
||||
|
||||
@@ -75,7 +84,7 @@ Chunk* Chunk::clone() const {
|
||||
*/
|
||||
ubyte* Chunk::encode() const {
|
||||
ubyte* buffer = new ubyte[CHUNK_DATA_LEN];
|
||||
for (size_t i = 0; i < CHUNK_VOL; i++) {
|
||||
for (uint i = 0; i < CHUNK_VOL; i++) {
|
||||
buffer[i] = voxels[i].id >> 8;
|
||||
buffer[CHUNK_VOL+i] = voxels[i].id & 0xFF;
|
||||
buffer[CHUNK_VOL*2 + i] = voxels[i].states >> 8;
|
||||
@@ -84,11 +93,8 @@ ubyte* Chunk::encode() const {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if all is fine
|
||||
**/
|
||||
bool Chunk::decode(ubyte* data) {
|
||||
for (size_t i = 0; i < CHUNK_VOL; i++) {
|
||||
for (uint i = 0; i < CHUNK_VOL; i++) {
|
||||
voxel& vox = voxels[i];
|
||||
|
||||
ubyte bid1 = data[i];
|
||||
@@ -104,7 +110,7 @@ bool Chunk::decode(ubyte* data) {
|
||||
}
|
||||
|
||||
void Chunk::convert(ubyte* data, const ContentLUT* lut) {
|
||||
for (size_t i = 0; i < CHUNK_VOL; i++) {
|
||||
for (uint i = 0; i < CHUNK_VOL; i++) {
|
||||
// see encode method to understand what the hell is going on here
|
||||
blockid_t id = ((blockid_t(data[i]) << 8) |
|
||||
blockid_t(data[CHUNK_VOL+i]));
|
||||
|
||||
+25
-5
@@ -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);
|
||||
|
||||
@@ -107,7 +107,7 @@ ubyte Chunks::getLight(int x, int y, int z, int channel){
|
||||
int lx = x - cx * CHUNK_W;
|
||||
int ly = y - cy * CHUNK_H;
|
||||
int lz = z - cz * CHUNK_D;
|
||||
return chunk->lightmap->get(lx,ly,lz, channel);
|
||||
return chunk->lightmap.get(lx,ly,lz, channel);
|
||||
}
|
||||
|
||||
light_t Chunks::getLight(int x, int y, int z){
|
||||
@@ -124,7 +124,7 @@ light_t Chunks::getLight(int x, int y, int z){
|
||||
int lx = x - cx * CHUNK_W;
|
||||
int ly = y - cy * CHUNK_H;
|
||||
int lz = z - cz * CHUNK_D;
|
||||
return chunk->lightmap->get(lx,ly,lz);
|
||||
return chunk->lightmap.get(lx,ly,lz);
|
||||
}
|
||||
|
||||
Chunk* Chunks::getChunkByVoxel(int x, int y, int z){
|
||||
|
||||
@@ -61,9 +61,9 @@ std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
|
||||
verifyLoadedChunk(level->content->getIndices(), chunk.get());
|
||||
}
|
||||
|
||||
light_t* lights = wfile->getLights(chunk->x, chunk->z);
|
||||
std::unique_ptr<light_t[]> lights (wfile->getLights(chunk->x, chunk->z));
|
||||
if (lights) {
|
||||
chunk->lightmap->set(lights);
|
||||
chunk->lightmap.set(lights.get());
|
||||
chunk->setLoadedLights(true);
|
||||
}
|
||||
return chunk;
|
||||
@@ -114,7 +114,7 @@ void ChunksStorage::getVoxels(VoxelsVolume* volume, bool backlight) const {
|
||||
} else {
|
||||
auto& chunk = found->second;
|
||||
const voxel* cvoxels = chunk->voxels;
|
||||
const light_t* clights = chunk->lightmap->getLights();
|
||||
const light_t* clights = chunk->lightmap.getLights();
|
||||
for (int ly = y; ly < y + h; ly++) {
|
||||
for (int lz = max(z, cz * CHUNK_D);
|
||||
lz < min(z + d, (cz + 1) * CHUNK_D);
|
||||
|
||||
Reference in New Issue
Block a user