converting block states to a bitfield
This commit is contained in:
+48
-50
@@ -7,40 +7,36 @@
|
||||
#include "../lighting/Lightmap.hpp"
|
||||
|
||||
Chunk::Chunk(int xpos, int zpos) : x(xpos), z(zpos){
|
||||
bottom = 0;
|
||||
top = CHUNK_H;
|
||||
for (uint i = 0; i < CHUNK_VOL; i++) {
|
||||
voxels[i].id = 2;
|
||||
voxels[i].states = 0;
|
||||
}
|
||||
bottom = 0;
|
||||
top = CHUNK_H;
|
||||
}
|
||||
|
||||
bool Chunk::isEmpty(){
|
||||
int id = -1;
|
||||
for (uint i = 0; i < CHUNK_VOL; i++){
|
||||
if (voxels[i].id != id){
|
||||
if (id != -1)
|
||||
return false;
|
||||
else
|
||||
id = voxels[i].id;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
int id = -1;
|
||||
for (uint i = 0; i < CHUNK_VOL; i++){
|
||||
if (voxels[i].id != id){
|
||||
if (id != -1)
|
||||
return false;
|
||||
else
|
||||
id = voxels[i].id;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void Chunk::updateHeights() {
|
||||
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;
|
||||
break;
|
||||
}
|
||||
}
|
||||
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;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Chunk::addBlockInventory(std::shared_ptr<Inventory> inventory,
|
||||
@@ -50,13 +46,13 @@ void Chunk::addBlockInventory(std::shared_ptr<Inventory> inventory,
|
||||
}
|
||||
|
||||
void Chunk::removeBlockInventory(uint x, uint y, uint z) {
|
||||
if (inventories.erase(vox_index(x, y, z))) {
|
||||
setUnsaved(true);
|
||||
}
|
||||
if (inventories.erase(vox_index(x, y, z))) {
|
||||
setUnsaved(true);
|
||||
}
|
||||
}
|
||||
|
||||
void Chunk::setBlockInventories(chunk_inventories_map map) {
|
||||
inventories = map;
|
||||
inventories = map;
|
||||
}
|
||||
|
||||
std::shared_ptr<Inventory> Chunk::getBlockInventory(uint x, uint y, uint z) const {
|
||||
@@ -70,12 +66,12 @@ std::shared_ptr<Inventory> Chunk::getBlockInventory(uint x, uint y, uint z) cons
|
||||
}
|
||||
|
||||
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];
|
||||
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);
|
||||
return other;
|
||||
other->lightmap.set(&lightmap);
|
||||
return other;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,19 +89,21 @@ std::unique_ptr<Chunk> Chunk::clone() const {
|
||||
Total size: (CHUNK_VOL * 4) bytes
|
||||
*/
|
||||
std::unique_ptr<ubyte[]> Chunk::encode() const {
|
||||
auto buffer = std::make_unique<ubyte[]>(CHUNK_DATA_LEN);
|
||||
for (uint i = 0; i < CHUNK_VOL; i++) {
|
||||
buffer[i] = voxels[i].id >> 8;
|
||||
auto buffer = std::make_unique<ubyte[]>(CHUNK_DATA_LEN);
|
||||
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;
|
||||
buffer[CHUNK_VOL*3 + i] = voxels[i].states & 0xFF;
|
||||
}
|
||||
return buffer;
|
||||
|
||||
blockstate_t state = blockstate2int(voxels[i].state);
|
||||
buffer[CHUNK_VOL*2 + i] = state >> 8;
|
||||
buffer[CHUNK_VOL*3 + i] = state & 0xFF;
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
bool Chunk::decode(const ubyte* data) {
|
||||
for (uint i = 0; i < CHUNK_VOL; i++) {
|
||||
voxel& vox = voxels[i];
|
||||
for (uint i = 0; i < CHUNK_VOL; i++) {
|
||||
voxel& vox = voxels[i];
|
||||
|
||||
ubyte bid1 = data[i];
|
||||
ubyte bid2 = data[CHUNK_VOL + i];
|
||||
@@ -113,10 +111,10 @@ bool Chunk::decode(const ubyte* data) {
|
||||
ubyte bst1 = data[CHUNK_VOL*2 + i];
|
||||
ubyte bst2 = data[CHUNK_VOL*3 + i];
|
||||
|
||||
vox.id = (blockid_t(bid1) << 8) | (blockid_t(bid2));
|
||||
vox.states = (blockstate_t(bst1) << 8) | (blockstate_t(bst2));
|
||||
}
|
||||
return true;
|
||||
vox.id = (blockid_t(bid1) << 8) | (blockid_t(bid2));
|
||||
vox.state = int2blockstate((blockstate_t(bst1) << 8) | (blockstate_t(bst2)));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void Chunk::convert(ubyte* data, const ContentLUT* lut) {
|
||||
|
||||
+38
-38
@@ -10,12 +10,12 @@
|
||||
#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;
|
||||
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;
|
||||
|
||||
@@ -27,72 +27,72 @@ using chunk_inventories_map = std::unordered_map<uint, std::shared_ptr<Inventory
|
||||
|
||||
class Chunk {
|
||||
public:
|
||||
int x, z;
|
||||
int bottom, top;
|
||||
voxel voxels[CHUNK_VOL];
|
||||
Lightmap lightmap;
|
||||
int flags = 0;
|
||||
int x, z;
|
||||
int bottom, top;
|
||||
voxel voxels[CHUNK_VOL] {};
|
||||
Lightmap lightmap;
|
||||
int flags = 0;
|
||||
|
||||
/* Block inventories map where key is index of block in voxels array */
|
||||
chunk_inventories_map inventories;
|
||||
|
||||
Chunk(int x, int z);
|
||||
Chunk(int x, int z);
|
||||
|
||||
bool isEmpty();
|
||||
bool isEmpty();
|
||||
|
||||
void updateHeights();
|
||||
void updateHeights();
|
||||
|
||||
// unused
|
||||
std::unique_ptr<Chunk> clone() const;
|
||||
std::unique_ptr<Chunk> clone() const;
|
||||
|
||||
// flags getters/setters below
|
||||
inline void setFlags(int mask, bool value){
|
||||
if (value)
|
||||
flags |= mask;
|
||||
else
|
||||
flags &= ~(mask);
|
||||
}
|
||||
// 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,
|
||||
uint x, uint y, uint z);
|
||||
void removeBlockInventory(uint x, uint y, uint z);
|
||||
void setBlockInventories(chunk_inventories_map map);
|
||||
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;
|
||||
|
||||
inline bool isUnsaved() const {return flags & ChunkFlag::UNSAVED;}
|
||||
inline bool isUnsaved() const {return flags & ChunkFlag::UNSAVED;}
|
||||
|
||||
inline bool isModified() const {return flags & ChunkFlag::MODIFIED;}
|
||||
inline bool isModified() const {return flags & ChunkFlag::MODIFIED;}
|
||||
|
||||
inline bool isLighted() const {return flags & ChunkFlag::LIGHTED;}
|
||||
inline bool isLighted() const {return flags & ChunkFlag::LIGHTED;}
|
||||
|
||||
inline bool isLoaded() const {return flags & ChunkFlag::LOADED;}
|
||||
inline bool isLoaded() const {return flags & ChunkFlag::LOADED;}
|
||||
|
||||
inline bool isLoadedLights() const {return flags & ChunkFlag::LOADED_LIGHTS;}
|
||||
inline bool isLoadedLights() const {return flags & ChunkFlag::LOADED_LIGHTS;}
|
||||
|
||||
inline bool isReady() const {return flags & ChunkFlag::READY;}
|
||||
inline bool isReady() const {return flags & ChunkFlag::READY;}
|
||||
|
||||
inline void setUnsaved(bool newState) {setFlags(ChunkFlag::UNSAVED, newState);}
|
||||
inline void setUnsaved(bool newState) {setFlags(ChunkFlag::UNSAVED, newState);}
|
||||
|
||||
inline void setModified(bool newState) {setFlags(ChunkFlag::MODIFIED, newState);}
|
||||
inline void setModified(bool newState) {setFlags(ChunkFlag::MODIFIED, newState);}
|
||||
|
||||
inline void setLoaded(bool newState) {setFlags(ChunkFlag::LOADED, newState);}
|
||||
inline void setLoaded(bool newState) {setFlags(ChunkFlag::LOADED, newState);}
|
||||
|
||||
inline void setLoadedLights(bool newState) {setFlags(ChunkFlag::LOADED_LIGHTS, newState);}
|
||||
inline void setLoadedLights(bool newState) {setFlags(ChunkFlag::LOADED_LIGHTS, newState);}
|
||||
|
||||
inline void setLighted(bool newState) {setFlags(ChunkFlag::LIGHTED, newState);}
|
||||
inline void setLighted(bool newState) {setFlags(ChunkFlag::LIGHTED, newState);}
|
||||
|
||||
inline void setReady(bool newState) {setFlags(ChunkFlag::READY, newState);}
|
||||
inline void setReady(bool newState) {setFlags(ChunkFlag::READY, newState);}
|
||||
|
||||
std::unique_ptr<ubyte[]> encode() const;
|
||||
std::unique_ptr<ubyte[]> encode() const;
|
||||
|
||||
/**
|
||||
* @return true if all is fine
|
||||
**/
|
||||
bool decode(const ubyte* data);
|
||||
bool decode(const ubyte* data);
|
||||
|
||||
static void convert(ubyte* data, const ContentLUT* lut);
|
||||
};
|
||||
|
||||
@@ -68,7 +68,7 @@ const AABB* Chunks::isObstacleAt(float x, float y, float z){
|
||||
const Block* def = contentIds->getBlockDef(v->id);
|
||||
if (def->obstacle) {
|
||||
const auto& boxes = def->rotatable
|
||||
? def->rt.hitboxes[v->rotation()]
|
||||
? def->rt.hitboxes[v->state.rotation]
|
||||
: def->hitboxes;
|
||||
for (const auto& hitbox : boxes) {
|
||||
if (hitbox.contains({x - ix, y - iy, z - iz})) {
|
||||
@@ -158,7 +158,7 @@ Chunk* Chunks::getChunk(int x, int z){
|
||||
return chunks[z * w + x].get();
|
||||
}
|
||||
|
||||
void Chunks::set(int32_t x, int32_t y, int32_t z, uint32_t id, uint8_t states) {
|
||||
void Chunks::set(int32_t x, int32_t y, int32_t z, uint32_t id, blockstate state) {
|
||||
if (y < 0 || y >= CHUNK_H)
|
||||
return;
|
||||
x -= ox * CHUNK_W;
|
||||
@@ -178,7 +178,7 @@ void Chunks::set(int32_t x, int32_t y, int32_t z, uint32_t id, uint8_t states) {
|
||||
if (def->inventorySize == 0)
|
||||
chunk->removeBlockInventory(lx, y, lz);
|
||||
vox.id = id;
|
||||
vox.states = states;
|
||||
vox.state = state;
|
||||
|
||||
chunk->setUnsaved(true);
|
||||
chunk->setModified(true);
|
||||
@@ -255,7 +255,7 @@ voxel* Chunks::rayCast(
|
||||
|
||||
if (!def->rt.solid) {
|
||||
const std::vector<AABB>& hitboxes = def->rotatable
|
||||
? def->rt.hitboxes[voxel->rotation()]
|
||||
? def->rt.hitboxes[voxel->state.rotation]
|
||||
: def->hitboxes;
|
||||
|
||||
scalar_t distance = maxDist;
|
||||
@@ -365,7 +365,7 @@ glm::vec3 Chunks::rayCastToObstacle(glm::vec3 start, glm::vec3 dir, float maxDis
|
||||
if (def->obstacle) {
|
||||
if (!def->rt.solid) {
|
||||
const std::vector<AABB>& hitboxes = def->rotatable
|
||||
? def->rt.hitboxes[voxel->rotation()]
|
||||
? def->rt.hitboxes[voxel->state.rotation]
|
||||
: def->modelBoxes;
|
||||
|
||||
scalar_t distance;
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "voxel.hpp"
|
||||
#include "../typedefs.hpp"
|
||||
|
||||
class VoxelRenderer;
|
||||
@@ -13,7 +15,6 @@ struct AABB;
|
||||
class Content;
|
||||
class ContentIndices;
|
||||
class Chunk;
|
||||
struct voxel;
|
||||
class WorldFiles;
|
||||
class LevelEvents;
|
||||
|
||||
@@ -42,7 +43,7 @@ public:
|
||||
voxel* get(int32_t x, int32_t y, int32_t z);
|
||||
light_t getLight(int32_t x, int32_t y, int32_t z);
|
||||
ubyte getLight(int32_t x, int32_t y, int32_t z, int channel);
|
||||
void set(int32_t x, int32_t y, int32_t z, uint32_t id, uint8_t states);
|
||||
void set(int32_t x, int32_t y, int32_t z, uint32_t id, blockstate state);
|
||||
|
||||
voxel* rayCast(
|
||||
glm::vec3 start,
|
||||
|
||||
@@ -163,7 +163,7 @@ void DefaultWorldGenerator::generate(voxel* voxels, int cx, int cz, int seed){
|
||||
for (int cur_y = 0; cur_y < CHUNK_H; cur_y++){
|
||||
// int cur_y = y;
|
||||
int id = cur_y < SEA_LEVEL ? idWater : BLOCK_AIR;
|
||||
int states = 0;
|
||||
blockstate state {};
|
||||
if ((cur_y == (int)height) && (SEA_LEVEL-2 < cur_y)) {
|
||||
id = idGrassBlock;
|
||||
} else if (cur_y < (height - 6)){
|
||||
@@ -177,7 +177,7 @@ void DefaultWorldGenerator::generate(voxel* voxels, int cx, int cz, int seed){
|
||||
treesTile, idWood, idLeaves);
|
||||
if (tree) {
|
||||
id = tree;
|
||||
states = BLOCK_DIR_UP;
|
||||
state.rotation = BLOCK_DIR_UP;
|
||||
}
|
||||
}
|
||||
float sand = fmax(heights.get(MAPS::SAND, cur_x, cur_z), heights.get(MAPS::CLIFF, cur_x, cur_z));
|
||||
@@ -198,10 +198,10 @@ void DefaultWorldGenerator::generate(voxel* voxels, int cx, int cz, int seed){
|
||||
}
|
||||
if ((height > SEA_LEVEL+1) && ((int)(height + 1) == cur_y) && ((unsigned short)randomgrass.rand() > 65533)){
|
||||
id = idWood;
|
||||
states = BLOCK_DIR_UP;
|
||||
state.rotation = BLOCK_DIR_UP;
|
||||
}
|
||||
voxels[(cur_y * CHUNK_D + z) * CHUNK_W + x].id = id;
|
||||
voxels[(cur_y * CHUNK_D + z) * CHUNK_W + x].states = states;
|
||||
voxels[(cur_y * CHUNK_D + z) * CHUNK_W + x].state = state;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ void FlatWorldGenerator::generate(voxel* voxels, int cx, int cz, int seed) {
|
||||
for (int x = 0; x < CHUNK_W; x++) {
|
||||
for (int cur_y = 0; cur_y < CHUNK_H; cur_y++){
|
||||
int id = BLOCK_AIR;
|
||||
int states = 0;
|
||||
blockstate state {};
|
||||
|
||||
if(cur_y == 2) {
|
||||
id = idBazalt;
|
||||
@@ -21,7 +21,7 @@ void FlatWorldGenerator::generate(voxel* voxels, int cx, int cz, int seed) {
|
||||
}
|
||||
|
||||
voxels[(cur_y * CHUNK_D + z) * CHUNK_W + x].id = id;
|
||||
voxels[(cur_y * CHUNK_D + z) * CHUNK_W + x].states = states;
|
||||
voxels[(cur_y * CHUNK_D + z) * CHUNK_W + x].state = state;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+24
-13
@@ -10,22 +10,33 @@ inline constexpr int BLOCK_DIR_EAST = 0x3;
|
||||
inline constexpr int BLOCK_DIR_UP = 0x4;
|
||||
inline constexpr int BLOCK_DIR_DOWN = 0x5;
|
||||
|
||||
// limited to 8 block orientations
|
||||
inline constexpr int BLOCK_ROT_MASK = 0b0000'0111;
|
||||
// reserved bits
|
||||
inline constexpr int BLOCK_RESERVED_MASK = 0b1111'1000;
|
||||
struct blockstate {
|
||||
uint8_t rotation : 3;
|
||||
uint8_t segment : 2;
|
||||
uint8_t reserved : 3;
|
||||
uint8_t userbits : 8;
|
||||
};
|
||||
static_assert (sizeof(blockstate) == 2);
|
||||
|
||||
inline constexpr blockstate_t blockstate2int(blockstate b) {
|
||||
return b.rotation |
|
||||
(b.segment << 3) |
|
||||
(b.reserved << 5) |
|
||||
(b.userbits << 8);
|
||||
}
|
||||
|
||||
inline constexpr blockstate int2blockstate(blockstate_t i) {
|
||||
return {
|
||||
static_cast<uint8_t>(i & 0b111),
|
||||
static_cast<uint8_t>((i >> 3) & 0b11),
|
||||
static_cast<uint8_t>((i >> 5) & 0b111),
|
||||
static_cast<uint8_t>((i >> 8) & 0xFF)
|
||||
};
|
||||
}
|
||||
|
||||
struct voxel {
|
||||
blockid_t id;
|
||||
blockstate_t states;
|
||||
|
||||
inline uint8_t rotation() const {
|
||||
return states & BLOCK_ROT_MASK;
|
||||
}
|
||||
|
||||
inline void setRotation(uint8_t rotation) {
|
||||
states = (states & (~BLOCK_ROT_MASK)) | (rotation & BLOCK_ROT_MASK);
|
||||
}
|
||||
blockstate state;
|
||||
};
|
||||
|
||||
#endif // VOXELS_VOXEL_HPP_
|
||||
|
||||
Reference in New Issue
Block a user