New world format, fixes
This commit is contained in:
+28
-2
@@ -4,8 +4,10 @@
|
||||
|
||||
Chunk::Chunk(int xpos, int zpos) : x(xpos), z(zpos){
|
||||
voxels = new voxel[CHUNK_VOL];
|
||||
for (unsigned int i = 0; i < CHUNK_VOL; i++)
|
||||
voxels[i].id = 1;
|
||||
for (size_t i = 0; i < CHUNK_VOL; i++) {
|
||||
voxels[i].id = 2;
|
||||
voxels[i].states = 0;
|
||||
}
|
||||
lightmap = new Lightmap();
|
||||
renderData.vertices = nullptr;
|
||||
}
|
||||
@@ -36,3 +38,27 @@ Chunk* Chunk::clone() const {
|
||||
return other;
|
||||
}
|
||||
|
||||
/**
|
||||
Current chunk format:
|
||||
[voxel_ids...][voxel_states...];
|
||||
*/
|
||||
ubyte* Chunk::encode() const {
|
||||
ubyte* buffer = new ubyte[CHUNK_DATA_LEN];
|
||||
for (size_t i = 0; i < CHUNK_VOL; i++) {
|
||||
buffer[i] = voxels[i].id;
|
||||
buffer[CHUNK_VOL + i] = voxels[i].states;
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
@return true if all is fine
|
||||
*/
|
||||
bool Chunk::decode(ubyte* data) {
|
||||
for (size_t i = 0; i < CHUNK_VOL; i++) {
|
||||
voxel& vox = voxels[i];
|
||||
vox.id = data[i];
|
||||
vox.states = data[CHUNK_VOL + i];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#define CHUNK_LOADED 0x4
|
||||
#define CHUNK_LIGHTED 0x8
|
||||
#define CHUNK_UNSAVED 0x10
|
||||
#define CHUNK_DATA_LEN (CHUNK_VOL*2)
|
||||
|
||||
struct voxel;
|
||||
class Lightmap;
|
||||
@@ -59,6 +60,9 @@ public:
|
||||
inline void setLighted(bool flag) {BITSET(flags, CHUNK_LIGHTED, flag);}
|
||||
|
||||
inline void setReady(bool flag) {BITSET(flags, CHUNK_READY, flag);}
|
||||
|
||||
ubyte* encode() const;
|
||||
bool decode(ubyte* data);
|
||||
};
|
||||
|
||||
#endif /* VOXELS_CHUNK_H_ */
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "WorldGenerator.h"
|
||||
#include "../lighting/Lightmap.h"
|
||||
#include "../files/WorldFiles.h"
|
||||
#include "../world/LevelEvents.h"
|
||||
|
||||
#include "../graphics/Mesh.h"
|
||||
#include "../voxmaths.h"
|
||||
@@ -15,8 +16,8 @@
|
||||
using glm::vec3;
|
||||
using std::shared_ptr;
|
||||
|
||||
Chunks::Chunks(int w, int d, int ox, int oz) : w(w), d(d), ox(ox), oz(oz){
|
||||
volume = w*d;
|
||||
Chunks::Chunks(int w, int d, int ox, int oz, LevelEvents* events) : w(w), d(d), ox(ox), oz(oz), events(events) {
|
||||
volume = (size_t)w*(size_t)d;
|
||||
chunks = new shared_ptr<Chunk>[volume];
|
||||
chunksSecond = new shared_ptr<Chunk>[volume];
|
||||
|
||||
@@ -258,7 +259,8 @@ void Chunks::translate(WorldFiles* worldFiles, int dx, int dz){
|
||||
if (chunk == nullptr)
|
||||
continue;
|
||||
if (nx < 0 || nz < 0 || nx >= w || nz >= d){
|
||||
worldFiles->put((const ubyte*)chunk->voxels, chunk->x, chunk->z);
|
||||
events->trigger(EVT_CHUNK_HIDDEN, chunk.get());
|
||||
worldFiles->put(chunk.get());
|
||||
chunksCount--;
|
||||
continue;
|
||||
}
|
||||
@@ -292,6 +294,10 @@ bool Chunks::putChunk(shared_ptr<Chunk> chunk) {
|
||||
|
||||
void Chunks::clear(){
|
||||
for (size_t i = 0; i < volume; i++){
|
||||
Chunk* chunk = chunks[i].get();
|
||||
if (chunk) {
|
||||
events->trigger(EVT_CHUNK_HIDDEN, chunk);
|
||||
}
|
||||
chunks[i] = nullptr;
|
||||
}
|
||||
chunksCount = 0;
|
||||
|
||||
+3
-1
@@ -11,6 +11,7 @@ class VoxelRenderer;
|
||||
class Chunk;
|
||||
class voxel;
|
||||
class WorldFiles;
|
||||
class LevelEvents;
|
||||
|
||||
class Chunks {
|
||||
public:
|
||||
@@ -20,8 +21,9 @@ public:
|
||||
size_t chunksCount;
|
||||
int w,d;
|
||||
int ox,oz;
|
||||
LevelEvents* events;
|
||||
|
||||
Chunks(int w, int d, int ox, int oz);
|
||||
Chunks(int w, int d, int ox, int oz, LevelEvents* events);
|
||||
~Chunks();
|
||||
|
||||
bool putChunk(std::shared_ptr<Chunk> chunk);
|
||||
|
||||
@@ -79,9 +79,12 @@ bool ChunksController::loadVisible(WorldFiles* worldFiles){
|
||||
|
||||
chunk = shared_ptr<Chunk>(new Chunk(nearX+ox, nearZ+oz));
|
||||
level->chunksStorage->store(chunk);
|
||||
if (worldFiles->getChunk(chunk->x, chunk->z, (ubyte*)chunk->voxels))
|
||||
ubyte* data = worldFiles->getChunk(chunk->x, chunk->z);
|
||||
if (data) {
|
||||
chunk->decode(data);
|
||||
chunk->setLoaded(true);
|
||||
|
||||
delete[] data;
|
||||
}
|
||||
chunks->putChunk(chunk);
|
||||
|
||||
if (!chunk->isLoaded()) {
|
||||
@@ -90,8 +93,9 @@ bool ChunksController::loadVisible(WorldFiles* worldFiles){
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < CHUNK_VOL; i++) {
|
||||
if (Block::blocks[chunk->voxels[i].id] == nullptr) {
|
||||
std::cout << "corruped block detected at " << i << " of chunk " << chunk->x << "x" << chunk->z << std::endl;
|
||||
blockid_t id = chunk->voxels[i].id;
|
||||
if (Block::blocks[id] == nullptr) {
|
||||
std::cout << "corruped block detected at " << i << " of chunk " << chunk->x << "x" << chunk->z << " -> " << (int)id << std::endl;
|
||||
chunk->voxels[i].id = 11;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,13 @@ shared_ptr<Chunk> ChunksStorage::get(int x, int z) const {
|
||||
return found->second;
|
||||
}
|
||||
|
||||
void ChunksStorage::remove(int x, int z) {
|
||||
auto found = chunksMap.find(ivec2(x, z));
|
||||
if (found != chunksMap.end()) {
|
||||
chunksMap.erase(found->first);
|
||||
}
|
||||
}
|
||||
|
||||
// some magic code
|
||||
void ChunksStorage::getVoxels(VoxelsVolume* volume) const {
|
||||
voxel* voxels = volume->getVoxels();
|
||||
|
||||
@@ -20,6 +20,7 @@ public:
|
||||
|
||||
std::shared_ptr<Chunk> get(int x, int y) const;
|
||||
void store(std::shared_ptr<Chunk> chunk);
|
||||
void remove(int x, int y);
|
||||
void getVoxels(VoxelsVolume* volume) const;
|
||||
|
||||
light_t getLight(int x, int y, int z, ubyte channel) const;
|
||||
|
||||
Reference in New Issue
Block a user