New world format, fixes

This commit is contained in:
MihailRis 2023-11-05 15:32:46 +03:00 committed by GitHub
parent 0c65b13f4e
commit 0cf8382e95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 350 additions and 173 deletions

View File

@ -6,9 +6,12 @@
#define CHUNK_W 16 #define CHUNK_W 16
#define CHUNK_H 256 #define CHUNK_H 256
#define CHUNK_D 16 #define CHUNK_D 16
/* Chunk volume (count of voxels per Chunk) */
#define CHUNK_VOL (CHUNK_W * CHUNK_H * CHUNK_D) #define CHUNK_VOL (CHUNK_W * CHUNK_H * CHUNK_D)
#define BLOCK_VOID (blockid_t)((2 << (sizeof(blockid_t)*8)) - 1) /* BLOCK_VOID is block id used to mark non-existing voxel (voxel of missing chunk) */
#define BLOCK_VOID (blockid_t)((2 << (sizeof(blockid_t)*CHAR_BIT)) - 1)
inline uint vox_index(int x, int y, int z, int w, int d) { inline uint vox_index(int x, int y, int z, int w, int d) {
return (y * d + z) * w + x; return (y * d + z) * w + x;

View File

@ -20,8 +20,9 @@ void initialize_assets(AssetsLoader* loader) {
// All in-game definitions (blocks, items, etc..) // All in-game definitions (blocks, items, etc..)
void setup_definitions() { void setup_definitions() {
for (size_t i = 0; i < 256; i++) for (size_t i = 0; i < 256; i++) {
Block::blocks[i] = nullptr; Block::blocks[i] = nullptr;
}
Block* block = new Block(BLOCK_AIR, 0); Block* block = new Block(BLOCK_AIR, 0);
block->drawGroup = 1; block->drawGroup = 1;

View File

@ -1,6 +1,7 @@
#include "WorldFiles.h" #include "WorldFiles.h"
#include "files.h" #include "files.h"
#include "rle.h"
#include "../window/Camera.h" #include "../window/Camera.h"
#include "../objects/Player.h" #include "../objects/Player.h"
#include "../physics/Hitbox.h" #include "../physics/Hitbox.h"
@ -12,6 +13,7 @@
#include <cassert> #include <cassert>
#include <iostream> #include <iostream>
#include <cstdint> #include <cstdint>
#include <memory>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
@ -20,12 +22,11 @@
#define SECTION_FLAGS 3 #define SECTION_FLAGS 3
#define PLAYER_FLAG_FLIGHT 0x1 #define PLAYER_FLAG_FLIGHT 0x1
#define PLAYER_FLAG_NOCLIP 0x2 #define PLAYER_FLAG_NOCLIP 0x2
#define CHUNK_DATA_LEN (CHUNK_VOL*sizeof(voxel))
using glm::ivec2; using glm::ivec2;
using glm::vec3; using glm::vec3;
using std::ios;
int64_t WorldFiles::totalCompressed = 0; using std::unique_ptr;
int bytes2Int(const ubyte* src, size_t offset){ int bytes2Int(const ubyte* src, size_t offset){
return (src[offset] << 24) | (src[offset+1] << 16) | (src[offset+2] << 8) | (src[offset+3]); return (src[offset] << 24) | (src[offset+1] << 16) | (src[offset+2] << 8) | (src[offset+3]);
@ -55,18 +56,18 @@ float bytes2Float(ubyte* src, uint offset){
} }
WorldFiles::WorldFiles(std::string directory, size_t mainBufferCapacity) : directory(directory){ WorldFiles::WorldFiles(std::string directory, size_t mainBufferCapacity) : directory(directory){
mainBufferIn = new ubyte[CHUNK_DATA_LEN]; mainBufferIn = new ubyte[CHUNK_DATA_LEN * 2];
mainBufferOut = new ubyte[mainBufferCapacity]; compressionBuffer = new ubyte[CHUNK_DATA_LEN * 2];
} }
WorldFiles::~WorldFiles(){ WorldFiles::~WorldFiles(){
delete[] mainBufferIn; delete[] mainBufferIn;
delete[] mainBufferOut; delete[] compressionBuffer;
for (auto it = regions.begin(); it != regions.end(); it++){ for (auto it = regions.begin(); it != regions.end(); it++){
WorldRegion region = it->second; WorldRegion region = it->second;
if (region.chunksData == nullptr) if (region.chunksData == nullptr)
continue; continue;
for (unsigned int i = 0; i < REGION_VOL; i++){ for (size_t i = 0; i < REGION_VOL; i++){
delete[] region.chunksData[i]; delete[] region.chunksData[i];
} }
delete[] region.chunksData; delete[] region.chunksData;
@ -74,37 +75,44 @@ WorldFiles::~WorldFiles(){
regions.clear(); regions.clear();
} }
void WorldFiles::put(const ubyte* chunkData, int x, int y){ void WorldFiles::put(Chunk* chunk){
assert(chunkData != nullptr); assert(chunk != nullptr);
int regionX = floordiv(x, REGION_SIZE); int regionX = floordiv(chunk->x, REGION_SIZE);
int regionY = floordiv(y, REGION_SIZE); int regionY = floordiv(chunk->z, REGION_SIZE);
int localX = x - (regionX * REGION_SIZE); int localX = chunk->x - (regionX * REGION_SIZE);
int localY = y - (regionY * REGION_SIZE); int localY = chunk->z - (regionY * REGION_SIZE);
ivec2 key(regionX, regionY); ivec2 key(regionX, regionY);
auto found = regions.find(key); auto found = regions.find(key);
if (found == regions.end()) { if (found == regions.end()) {
ubyte** chunksData = new ubyte*[REGION_VOL]; ubyte** chunksData = new ubyte*[REGION_VOL];
uint32_t* compressedSizes = new uint32_t[REGION_VOL];
for (uint i = 0; i < REGION_VOL; i++) { for (uint i = 0; i < REGION_VOL; i++) {
chunksData[i] = nullptr; chunksData[i] = nullptr;
} }
regions[key] = { chunksData, true }; regions[key] = { chunksData, compressedSizes, true };
} }
WorldRegion& region = regions[key]; WorldRegion& region = regions[key];
region.unsaved = true; region.unsaved = true;
ubyte* targetChunk = region.chunksData[localY * REGION_SIZE + localX]; size_t chunk_index = localY * REGION_SIZE + localX;
if (targetChunk == nullptr){ ubyte* target_chunk = region.chunksData[chunk_index];
targetChunk = new ubyte[CHUNK_DATA_LEN]; if (target_chunk) {
region.chunksData[localY * REGION_SIZE + localX] = targetChunk; delete[] target_chunk;
totalCompressed += CHUNK_DATA_LEN;
} }
for (uint i = 0; i < CHUNK_DATA_LEN; i++)
targetChunk[i] = chunkData[i];
ubyte* chunk_data = chunk->encode();
size_t compressedSize = extrle::encode(chunk_data, CHUNK_DATA_LEN, compressionBuffer);
delete[] chunk_data;
ubyte* data = new ubyte[compressedSize];
for (size_t i = 0; i < compressedSize; i++) {
data[i] = compressionBuffer[i];
}
region.chunksData[chunk_index] = data;
region.compressedSizes[chunk_index] = compressedSize;
} }
std::string WorldFiles::getRegionFile(int x, int y) { std::string WorldFiles::getRegionFile(int x, int y) {
@ -115,9 +123,7 @@ std::string WorldFiles::getPlayerFile() {
return directory + "/player.bin"; return directory + "/player.bin";
} }
bool WorldFiles::getChunk(int x, int y, ubyte* out){ ubyte* WorldFiles::getChunk(int x, int y){
assert(out != nullptr);
int regionX = floordiv(x, REGION_SIZE); int regionX = floordiv(x, REGION_SIZE);
int regionY = floordiv(y, REGION_SIZE); int regionY = floordiv(y, REGION_SIZE);
@ -131,21 +137,31 @@ bool WorldFiles::getChunk(int x, int y, ubyte* out){
auto found = regions.find(key); auto found = regions.find(key);
if (found == regions.end()) { if (found == regions.end()) {
return readChunk(x, y, out); ubyte** chunksData = new ubyte * [REGION_VOL];
uint32_t* compressedSizes = new uint32_t[REGION_VOL];
for (uint i = 0; i < REGION_VOL; i++) {
chunksData[i] = nullptr;
}
regions[key] = { chunksData, compressedSizes, true };
} }
WorldRegion& region = found->second; WorldRegion& region = regions[key];
ubyte* chunk = region.chunksData[chunkIndex]; ubyte* data = region.chunksData[chunkIndex];
if (chunk == nullptr) if (data == nullptr) {
return readChunk(x,y,out); data = readChunkData(x, y, region.compressedSizes[chunkIndex]);
for (uint i = 0; i < CHUNK_DATA_LEN; i++) if (data) {
out[i] = chunk[i]; region.chunksData[chunkIndex] = data;
return true; }
}
if (data) {
ubyte* decompressed = new ubyte[CHUNK_DATA_LEN];
extrle::decode(data, region.compressedSizes[chunkIndex], decompressed);
return decompressed;
}
return nullptr;
} }
bool WorldFiles::readChunk(int x, int y, ubyte* out){ ubyte* WorldFiles::readChunkData(int x, int y, uint32_t& length){
assert(out != nullptr);
int regionX = floordiv(x, REGION_SIZE); int regionX = floordiv(x, REGION_SIZE);
int regionY = floordiv(y, REGION_SIZE); int regionY = floordiv(y, REGION_SIZE);
@ -157,39 +173,35 @@ bool WorldFiles::readChunk(int x, int y, ubyte* out){
std::string filename = getRegionFile(regionX, regionY); std::string filename = getRegionFile(regionX, regionY);
std::ifstream input(filename, std::ios::binary); std::ifstream input(filename, std::ios::binary);
if (!input.is_open()){ if (!input.is_open()){
return false; return nullptr;
} }
input.seekg(0, ios::end);
size_t file_size = input.tellg();
size_t table_offset = file_size - REGION_VOL * 4;
uint32_t offset; uint32_t offset;
input.seekg(chunkIndex*4); input.seekg(table_offset + chunkIndex * 4);
input.read((char*)(&offset), 4); input.read((char*)(&offset), 4);
// Ordering bytes from big-endian to machine order (any, just reading) offset = bytes2Int((const ubyte*)(&offset), 0);
offset = bytes2Int((const unsigned char*)(&offset), 0);
//assert (offset < 1000000);
if (offset == 0){ if (offset == 0){
input.close(); input.close();
return false; return nullptr;
} }
input.seekg(offset); input.seekg(offset);
input.read((char*)(&offset), 4); input.read((char*)(&offset), 4);
size_t compressedSize = bytes2Int((const ubyte*)(&offset), 0); length = bytes2Int((const ubyte*)(&offset), 0);
input.read((char*)mainBufferIn, compressedSize); ubyte* data = new ubyte[length];
input.read((char*)data, length);
input.close(); input.close();
return data;
decompressRLE((ubyte*)mainBufferIn, compressedSize, (ubyte*)out, CHUNK_DATA_LEN);
return true;
} }
void WorldFiles::write(){ void WorldFiles::write(){
for (auto it = regions.begin(); it != regions.end(); it++){ for (auto it = regions.begin(); it != regions.end(); it++){
if (it->second.chunksData == nullptr || !it->second.unsaved) if (it->second.chunksData == nullptr || !it->second.unsaved)
continue; continue;
ivec2 key = it->first; ivec2 key = it->first;
writeRegion(key.x, key.y, it->second);
unsigned int size = writeRegion(mainBufferOut, key.x, key.y, it->second.chunksData);
write_binary_file(getRegionFile(key.x, key.y), (const char*)mainBufferOut, size);
} }
} }
@ -238,7 +250,7 @@ bool WorldFiles::readPlayer(Player* player) {
break; break;
case SECTION_FLAGS: case SECTION_FLAGS:
{ {
unsigned char flags = data[offset++]; ubyte flags = data[offset++];
player->flight = flags & PLAYER_FLAG_FLIGHT; player->flight = flags & PLAYER_FLAG_FLIGHT;
player->noclip = flags & PLAYER_FLAG_NOCLIP; player->noclip = flags & PLAYER_FLAG_NOCLIP;
} }
@ -250,39 +262,45 @@ bool WorldFiles::readPlayer(Player* player) {
return true; return true;
} }
uint WorldFiles::writeRegion(ubyte* out, int x, int y, ubyte** region){
uint offset = REGION_VOL * 4;
for (uint i = 0; i < offset; i++)
out[i] = 0;
ubyte* compressed = new ubyte[CHUNK_DATA_LEN]; void WorldFiles::writeRegion(int x, int y, WorldRegion& entry){
for (int i = 0; i < REGION_VOL; i++){ ubyte** region = entry.chunksData;
ubyte* chunk = region[i]; uint32_t* sizes = entry.compressedSizes;
if (chunk == nullptr){ for (size_t i = 0; i < REGION_VOL; i++) {
chunk = new ubyte[CHUNK_DATA_LEN]; int chunk_x = (i % REGION_SIZE) + x * REGION_SIZE;
if (readChunk((i % REGION_SIZE) + x * REGION_SIZE, (i / REGION_SIZE) + y * REGION_SIZE, chunk)){ int chunk_y = (i / REGION_SIZE) + y * REGION_SIZE;
region[i] = chunk; if (region[i] == nullptr) {
totalCompressed += CHUNK_DATA_LEN; region[i] = readChunkData(chunk_x, chunk_y, sizes[i]);
} else {
delete[] chunk;
chunk = nullptr;
}
}
if (chunk == nullptr){
int2Bytes(0, out, i*4);
} else {
int2Bytes(offset, out, i*4);
uint compressedSize = compressRLE(chunk, CHUNK_DATA_LEN, compressed);
int2Bytes(compressedSize, out, offset);
offset += 4;
for (uint j = 0; j < compressedSize; j++)
out[offset++] = compressed[j];
} }
} }
delete[] compressed;
return offset; char header[10] = ".VOXREG";
header[8] = REGION_FORMAT_VERSION;
header[9] = 0; // flags
std::ofstream file(getRegionFile(x, y), ios::out | ios::binary);
file.write(header, 10);
size_t offset = 10;
char intbuf[4]{};
uint offsets[REGION_VOL]{};
for (size_t i = 0; i < REGION_VOL; i++) {
ubyte* chunk = region[i];
if (chunk == nullptr){
offsets[i] = 0;
} else {
offsets[i] = offset;
size_t compressedSize = sizes[i];
int2Bytes(compressedSize, (ubyte*)intbuf, 0);
offset += 4 + compressedSize;
file.write(intbuf, 4);
file.write((const char*)chunk, compressedSize);
}
}
for (size_t i = 0; i < REGION_VOL; i++) {
int2Bytes(offsets[i], (ubyte*)intbuf, 0);
file.write(intbuf, 4);
}
} }

View File

@ -11,34 +11,36 @@
#include "../typedefs.h" #include "../typedefs.h"
class Player;
#define REGION_SIZE_BIT 5 #define REGION_SIZE_BIT 5
#define REGION_SIZE (1 << (REGION_SIZE_BIT)) #define REGION_SIZE (1 << (REGION_SIZE_BIT))
#define REGION_VOL ((REGION_SIZE) * (REGION_SIZE)) #define REGION_VOL ((REGION_SIZE) * (REGION_SIZE))
#define REGION_FORMAT_VERSION 1
class Player;
class Chunk;
struct WorldRegion { struct WorldRegion {
ubyte** chunksData; ubyte** chunksData;
uint32_t* compressedSizes;
bool unsaved; bool unsaved;
}; };
class WorldFiles { class WorldFiles {
public: public:
static int64_t totalCompressed;
std::unordered_map<glm::ivec2, WorldRegion> regions; std::unordered_map<glm::ivec2, WorldRegion> regions;
std::string directory; std::string directory;
ubyte* mainBufferIn; ubyte* mainBufferIn;
ubyte* mainBufferOut; ubyte* compressionBuffer;
WorldFiles(std::string directory, size_t mainBufferCapacity); WorldFiles(std::string directory, size_t mainBufferCapacity);
~WorldFiles(); ~WorldFiles();
void put(const ubyte* chunkData, int x, int y); void put(Chunk* chunk);
bool readPlayer(Player* player); bool readPlayer(Player* player);
bool readChunk(int x, int y, ubyte* out); ubyte* readChunkData(int x, int y, uint32_t& length);
bool getChunk(int x, int y, ubyte* out); ubyte* getChunk(int x, int y);
uint writeRegion(ubyte* out, int x, int y, ubyte** region); void writeRegion(int x, int y, WorldRegion& entry);
void writePlayer(Player* player); void writePlayer(Player* player);
void write(); void write();

View File

@ -55,56 +55,3 @@ char* read_binary_file(std::string filename, size_t& length) {
input.close(); input.close();
return data.release(); return data.release();
} }
// returns decompressed length
size_t decompressRLE(const ubyte* src, size_t length, ubyte* dst, size_t targetLength){
size_t offset = 0;
for (size_t i = 0; i < length;){
unsigned char counter = src[i++];
unsigned char c = src[i++];
for (unsigned int j = 0; j <= counter; j++){
dst[offset++] = c;
}
}
return offset;
}
size_t calcRLE(const ubyte* src, size_t length) {
size_t offset = 0;
size_t counter = 0;
ubyte c = src[0];
for (size_t i = 0; i < length; i++){
ubyte cnext = src[i];
if (cnext != c || counter == 255){
offset += 2;
c = cnext;
counter = 0;
} else {
counter++;
}
}
return offset + 2;
}
// max result size = length * 2; returns compressed length
size_t compressRLE(const ubyte* src, size_t length, ubyte* dst) {
if (length == 0)
return 0;
size_t offset = 0;
uint counter = 0;
ubyte c = src[0];
for (size_t i = 1; i < length; i++){
ubyte cnext = src[i];
if (cnext != c || counter == 255){
dst[offset++] = counter;
dst[offset++] = c;
c = cnext;
counter = 0;
} else {
counter++;
}
}
dst[offset++] = counter;
dst[offset++] = c;
return offset;
}

View File

@ -7,11 +7,6 @@
extern bool write_binary_file(std::string filename, const char* data, size_t size); extern bool write_binary_file(std::string filename, const char* data, size_t size);
extern unsigned int append_binary_file(std::string filename, const char* data, size_t size); extern unsigned int append_binary_file(std::string filename, const char* data, size_t size);
extern bool read_binary_file(std::string filename, char* data, size_t size); extern bool read_binary_file(std::string filename, char* data, size_t size);
extern bool read_binary_file(std::string filename, char* data, size_t offset, size_t size);
extern char* read_binary_file(std::string filename, size_t& length); extern char* read_binary_file(std::string filename, size_t& length);
extern size_t calcRLE(const ubyte* src, size_t length);
extern size_t compressRLE(const ubyte* src, size_t length, ubyte* dst);
extern size_t decompressRLE(const ubyte* src, size_t length, ubyte* dst, size_t targetLength);
#endif /* FILES_FILES_H_ */ #endif /* FILES_FILES_H_ */

90
src/files/rle.cpp Normal file
View File

@ -0,0 +1,90 @@
#include "rle.h"
size_t rle::decode(const ubyte* src, size_t srclen, ubyte* dst) {
size_t offset = 0;
for (size_t i = 0; i < srclen;) {
ubyte len = src[i++];
ubyte c = src[i++];
for (size_t j = 0; j <= len; j++) {
dst[offset++] = c;
}
}
return offset;
}
size_t rle::encode(const ubyte* src, size_t srclen, ubyte* dst) {
if (srclen == 0) {
return 0;
}
size_t offset = 0;
ubyte counter = 0;
ubyte c = src[0];
for (size_t i = 1; i < srclen; i++) {
ubyte cnext = src[i];
if (cnext != c || counter == 255) {
dst[offset++] = counter;
dst[offset++] = c;
c = cnext;
counter = 0;
}
else {
counter++;
}
}
dst[offset++] = counter;
dst[offset++] = c;
return offset;
}
size_t extrle::decode(const ubyte* src, size_t srclen, ubyte* dst) {
size_t offset = 0;
for (size_t i = 0; i < srclen;) {
uint len = src[i++];
if (len & 0x80) {
len &= 0x7F;
len |= ((uint)src[i++]) << 7;
}
ubyte c = src[i++];
for (size_t j = 0; j <= len; j++) {
dst[offset++] = c;
}
}
return offset;
}
size_t extrle::encode(const ubyte* src, size_t srclen, ubyte* dst) {
if (srclen == 0) {
return 0;
}
size_t offset = 0;
uint counter = 0;
ubyte c = src[0];
for (size_t i = 1; i < srclen; i++) {
ubyte cnext = src[i];
if (cnext != c || counter == max_sequence) {
if (counter >= 0x80) {
dst[offset++] = 0x80 | (counter & 0x7F);
dst[offset++] = counter >> 7;
}
else {
dst[offset++] = counter;
}
dst[offset++] = c;
c = cnext;
counter = 0;
}
else {
counter++;
}
}
if (counter >= 0x80) {
dst[offset++] = 0x80 | (counter & 0x7F);
dst[offset++] = counter >> 7;
}
else {
dst[offset++] = counter;
}
dst[offset++] = c;
return offset;
}

17
src/files/rle.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef FILES_RLE_H_
#define FILES_RLE_H_
#include "../typedefs.h"
namespace rle {
size_t encode(const ubyte* src, size_t length, ubyte* dst);
size_t decode(const ubyte* src, size_t length, ubyte* dst);
}
namespace extrle {
constexpr uint max_sequence = 0x7FFF;
size_t encode(const ubyte* src, size_t length, ubyte* dst);
size_t decode(const ubyte* src, size_t length, ubyte* dst);
}
#endif // FILES_RLE_H_

View File

@ -4,8 +4,10 @@
Chunk::Chunk(int xpos, int zpos) : x(xpos), z(zpos){ Chunk::Chunk(int xpos, int zpos) : x(xpos), z(zpos){
voxels = new voxel[CHUNK_VOL]; voxels = new voxel[CHUNK_VOL];
for (unsigned int i = 0; i < CHUNK_VOL; i++) for (size_t i = 0; i < CHUNK_VOL; i++) {
voxels[i].id = 1; voxels[i].id = 2;
voxels[i].states = 0;
}
lightmap = new Lightmap(); lightmap = new Lightmap();
renderData.vertices = nullptr; renderData.vertices = nullptr;
} }
@ -36,3 +38,27 @@ Chunk* Chunk::clone() const {
return other; 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;
}

View File

@ -9,6 +9,7 @@
#define CHUNK_LOADED 0x4 #define CHUNK_LOADED 0x4
#define CHUNK_LIGHTED 0x8 #define CHUNK_LIGHTED 0x8
#define CHUNK_UNSAVED 0x10 #define CHUNK_UNSAVED 0x10
#define CHUNK_DATA_LEN (CHUNK_VOL*2)
struct voxel; struct voxel;
class Lightmap; class Lightmap;
@ -59,6 +60,9 @@ public:
inline void setLighted(bool flag) {BITSET(flags, CHUNK_LIGHTED, flag);} inline void setLighted(bool flag) {BITSET(flags, CHUNK_LIGHTED, flag);}
inline void setReady(bool flag) {BITSET(flags, CHUNK_READY, flag);} inline void setReady(bool flag) {BITSET(flags, CHUNK_READY, flag);}
ubyte* encode() const;
bool decode(ubyte* data);
}; };
#endif /* VOXELS_CHUNK_H_ */ #endif /* VOXELS_CHUNK_H_ */

View File

@ -5,6 +5,7 @@
#include "WorldGenerator.h" #include "WorldGenerator.h"
#include "../lighting/Lightmap.h" #include "../lighting/Lightmap.h"
#include "../files/WorldFiles.h" #include "../files/WorldFiles.h"
#include "../world/LevelEvents.h"
#include "../graphics/Mesh.h" #include "../graphics/Mesh.h"
#include "../voxmaths.h" #include "../voxmaths.h"
@ -15,8 +16,8 @@
using glm::vec3; using glm::vec3;
using std::shared_ptr; using std::shared_ptr;
Chunks::Chunks(int w, int d, int ox, int oz) : w(w), d(d), ox(ox), oz(oz){ Chunks::Chunks(int w, int d, int ox, int oz, LevelEvents* events) : w(w), d(d), ox(ox), oz(oz), events(events) {
volume = w*d; volume = (size_t)w*(size_t)d;
chunks = new shared_ptr<Chunk>[volume]; chunks = new shared_ptr<Chunk>[volume];
chunksSecond = 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) if (chunk == nullptr)
continue; continue;
if (nx < 0 || nz < 0 || nx >= w || nz >= d){ 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--; chunksCount--;
continue; continue;
} }
@ -292,6 +294,10 @@ bool Chunks::putChunk(shared_ptr<Chunk> chunk) {
void Chunks::clear(){ void Chunks::clear(){
for (size_t i = 0; i < volume; i++){ for (size_t i = 0; i < volume; i++){
Chunk* chunk = chunks[i].get();
if (chunk) {
events->trigger(EVT_CHUNK_HIDDEN, chunk);
}
chunks[i] = nullptr; chunks[i] = nullptr;
} }
chunksCount = 0; chunksCount = 0;

View File

@ -11,6 +11,7 @@ class VoxelRenderer;
class Chunk; class Chunk;
class voxel; class voxel;
class WorldFiles; class WorldFiles;
class LevelEvents;
class Chunks { class Chunks {
public: public:
@ -20,8 +21,9 @@ public:
size_t chunksCount; size_t chunksCount;
int w,d; int w,d;
int ox,oz; 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(); ~Chunks();
bool putChunk(std::shared_ptr<Chunk> chunk); bool putChunk(std::shared_ptr<Chunk> chunk);

View File

@ -79,9 +79,12 @@ bool ChunksController::loadVisible(WorldFiles* worldFiles){
chunk = shared_ptr<Chunk>(new Chunk(nearX+ox, nearZ+oz)); chunk = shared_ptr<Chunk>(new Chunk(nearX+ox, nearZ+oz));
level->chunksStorage->store(chunk); 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); chunk->setLoaded(true);
delete[] data;
}
chunks->putChunk(chunk); chunks->putChunk(chunk);
if (!chunk->isLoaded()) { if (!chunk->isLoaded()) {
@ -90,8 +93,9 @@ bool ChunksController::loadVisible(WorldFiles* worldFiles){
} }
for (size_t i = 0; i < CHUNK_VOL; i++) { for (size_t i = 0; i < CHUNK_VOL; i++) {
if (Block::blocks[chunk->voxels[i].id] == nullptr) { blockid_t id = chunk->voxels[i].id;
std::cout << "corruped block detected at " << i << " of chunk " << chunk->x << "x" << chunk->z << std::endl; 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; chunk->voxels[i].id = 11;
} }
} }

View File

@ -29,6 +29,13 @@ shared_ptr<Chunk> ChunksStorage::get(int x, int z) const {
return found->second; 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 // some magic code
void ChunksStorage::getVoxels(VoxelsVolume* volume) const { void ChunksStorage::getVoxels(VoxelsVolume* volume) const {
voxel* voxels = volume->getVoxels(); voxel* voxels = volume->getVoxels();

View File

@ -20,6 +20,7 @@ public:
std::shared_ptr<Chunk> get(int x, int y) const; std::shared_ptr<Chunk> get(int x, int y) const;
void store(std::shared_ptr<Chunk> chunk); void store(std::shared_ptr<Chunk> chunk);
void remove(int x, int y);
void getVoxels(VoxelsVolume* volume) const; void getVoxels(VoxelsVolume* volume) const;
light_t getLight(int x, int y, int z, ubyte channel) const; light_t getLight(int x, int y, int z, ubyte channel) const;

View File

@ -1,6 +1,8 @@
#include "Level.h" #include "Level.h"
#include "World.h" #include "World.h"
#include "LevelEvents.h"
#include "../lighting/Lighting.h" #include "../lighting/Lighting.h"
#include "../voxels/Chunk.h"
#include "../voxels/Chunks.h" #include "../voxels/Chunks.h"
#include "../voxels/ChunksController.h" #include "../voxels/ChunksController.h"
#include "../voxels/ChunksStorage.h" #include "../voxels/ChunksStorage.h"
@ -9,19 +11,24 @@
#include "../physics/PhysicsSolver.h" #include "../physics/PhysicsSolver.h"
#include "../objects/Player.h" #include "../objects/Player.h"
Level::Level(World* world, Player* player, Chunks* chunks, ChunksStorage* chunksStorage, PhysicsSolver* physics) : Level::Level(World* world, Player* player, Chunks* chunks, ChunksStorage* chunksStorage, PhysicsSolver* physics, LevelEvents* events) :
world(world), world(world),
player(player), player(player),
chunks(chunks), chunks(chunks),
chunksStorage(chunksStorage), chunksStorage(chunksStorage),
physics(physics) { physics(physics),
events(events) {
lighting = new Lighting(chunks); lighting = new Lighting(chunks);
chunksController = new ChunksController(this, chunks, lighting); chunksController = new ChunksController(this, chunks, lighting);
playerController = new PlayerController(this); playerController = new PlayerController(this);
events->listen(EVT_CHUNK_HIDDEN, [this](lvl_event_type type, Chunk* chunk) {
this->chunksStorage->remove(chunk->x, chunk->z);
});
} }
Level::~Level(){ Level::~Level(){
delete chunks; delete chunks;
delete events;
delete physics; delete physics;
delete player; delete player;
delete lighting; delete lighting;

View File

@ -4,6 +4,7 @@
class World; class World;
class Player; class Player;
class Chunks; class Chunks;
class LevelEvents;
class Lighting; class Lighting;
class PhysicsSolver; class PhysicsSolver;
class ChunksController; class ChunksController;
@ -20,7 +21,8 @@ public:
Lighting* lighting; Lighting* lighting;
ChunksController* chunksController; ChunksController* chunksController;
PlayerController* playerController; PlayerController* playerController;
Level(World* world, Player* player, Chunks* chunks, ChunksStorage* chunksStorage, PhysicsSolver* physics); LevelEvents* events;
Level(World* world, Player* player, Chunks* chunks, ChunksStorage* chunksStorage, PhysicsSolver* physics, LevelEvents* events);
~Level(); ~Level();
void update(float delta, bool interactions); void update(float delta, bool interactions);

16
src/world/LevelEvents.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "LevelEvents.h"
#include "../voxels/Chunk.h"
using std::vector;
void LevelEvents::listen(lvl_event_type type, chunk_event_func func) {
auto& callbacks = chunk_callbacks[type];
callbacks.push_back(func);
}
void LevelEvents::trigger(lvl_event_type type, Chunk* chunk) {
auto& callbacks = chunk_callbacks[type];
for (chunk_event_func func : callbacks) {
func(type, chunk);
}
}

23
src/world/LevelEvents.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef WORLD_LEVEL_EVENTS_H_
#define WORLD_LEVEL_EVENTS_H_
#include <functional>
#include <vector>
#include <unordered_map>
class Chunk;
enum lvl_event_type {
EVT_CHUNK_HIDDEN,
};
typedef std::function<void(lvl_event_type, Chunk*)> chunk_event_func;
class LevelEvents {
std::unordered_map<lvl_event_type, std::vector<chunk_event_func>> chunk_callbacks;
public:
void listen(lvl_event_type type, chunk_event_func func);
void trigger(lvl_event_type type, Chunk* chunk);
};
#endif // WORLD_LEVEL_EVENTS_H_

View File

@ -10,11 +10,12 @@
#include "../objects/Player.h" #include "../objects/Player.h"
#include "../physics/PhysicsSolver.h" #include "../physics/PhysicsSolver.h"
#include "../window/Camera.h" #include "../window/Camera.h"
#include "../world/LevelEvents.h"
using std::shared_ptr; using std::shared_ptr;
World::World(std::string name, std::string directory, int seed) : name(name), seed(seed) { World::World(std::string name, std::string directory, int seed) : name(name), seed(seed) {
wfile = new WorldFiles(directory, REGION_VOL * (CHUNK_VOL * 2 + 8)); wfile = new WorldFiles(directory, REGION_VOL * (CHUNK_DATA_LEN * 2 + 8));
} }
World::~World(){ World::~World(){
@ -24,11 +25,11 @@ World::~World(){
void World::write(Level* level) { void World::write(Level* level) {
Chunks* chunks = level->chunks; Chunks* chunks = level->chunks;
for (unsigned int i = 0; i < chunks->volume; i++) { for (size_t i = 0; i < chunks->volume; i++) {
shared_ptr<Chunk> chunk = chunks->chunks[i]; shared_ptr<Chunk> chunk = chunks->chunks[i];
if (chunk == nullptr || !chunk->isUnsaved()) if (chunk == nullptr || !chunk->isUnsaved())
continue; continue;
wfile->put((const ubyte*)chunk->voxels, chunk->x, chunk->z); wfile->put(chunk.get());
} }
wfile->write(); wfile->write();
@ -37,7 +38,8 @@ void World::write(Level* level) {
Level* World::loadLevel(Player* player) { Level* World::loadLevel(Player* player) {
ChunksStorage* storage = new ChunksStorage(); ChunksStorage* storage = new ChunksStorage();
Level* level = new Level(this, player, new Chunks(16, 16, 0, 0), storage, new PhysicsSolver(vec3(0, -19.6f, 0))); LevelEvents* events = new LevelEvents();
Level* level = new Level(this, player, new Chunks(16, 16, 0, 0, events), storage, new PhysicsSolver(vec3(0, -19.6f, 0)), events);
wfile->readPlayer(player); wfile->readPlayer(player);
Camera* camera = player->camera; Camera* camera = player->camera;

View File

@ -17,6 +17,7 @@
#include "voxels/Block.h" #include "voxels/Block.h"
#include "world/World.h" #include "world/World.h"
#include "world/Level.h" #include "world/Level.h"
#include "world/LevelEvents.h"
#include "objects/Player.h" #include "objects/Player.h"
#include "Assets.h" #include "Assets.h"
#include "player_control.h" #include "player_control.h"
@ -30,6 +31,9 @@ WorldRenderer::WorldRenderer(Level* level, Assets* assets) : assets(assets), lev
lineBatch = new LineBatch(4096); lineBatch = new LineBatch(4096);
batch3d = new Batch3D(1024); batch3d = new Batch3D(1024);
renderer = new ChunksRenderer(level); renderer = new ChunksRenderer(level);
level->events->listen(EVT_CHUNK_HIDDEN, [this](lvl_event_type type, Chunk* chunk) {
renderer->unload(chunk);
});
} }
WorldRenderer::~WorldRenderer() { WorldRenderer::~WorldRenderer() {