Refactor, world.bin file, BinaryWriter, BinaryReader
This commit is contained in:
+33
-32
@@ -97,17 +97,15 @@ light_t Chunks::getLight(int x, int y, int z){
|
||||
}
|
||||
|
||||
Chunk* Chunks::getChunkByVoxel(int x, int y, int z){
|
||||
if (y < 0 || y >= CHUNK_H)
|
||||
return nullptr;
|
||||
x -= ox * CHUNK_W;
|
||||
z -= oz * CHUNK_D;
|
||||
int cx = x / CHUNK_W;
|
||||
int cy = y / CHUNK_H;
|
||||
int cz = z / CHUNK_D;
|
||||
if (x < 0) cx--;
|
||||
if (y < 0) cy--;
|
||||
if (z < 0) cz--;
|
||||
if (cx < 0 || cy < 0 || cz < 0 || cx >= w || cy >= 1 || cz >= d)
|
||||
int cx = floordiv(x, CHUNK_W);
|
||||
int cz = floordiv(z, CHUNK_D);
|
||||
if (cx < 0 || cz < 0 || cx >= w || cz >= d)
|
||||
return nullptr;
|
||||
return chunks[(cy * d + cz) * w + cx].get();
|
||||
return chunks[cz * w + cx].get();
|
||||
}
|
||||
|
||||
Chunk* Chunks::getChunk(int x, int z){
|
||||
@@ -119,14 +117,12 @@ Chunk* Chunks::getChunk(int x, int z){
|
||||
}
|
||||
|
||||
void Chunks::set(int x, int y, int z, int id, uint8_t states){
|
||||
x -= ox * CHUNK_W;
|
||||
z -= oz * CHUNK_D;
|
||||
int cx = x / CHUNK_W;
|
||||
if (y < 0 || y >= CHUNK_H)
|
||||
return;
|
||||
int cz = z / CHUNK_D;
|
||||
if (x < 0) cx--;
|
||||
if (z < 0) cz--;
|
||||
x -= ox * CHUNK_W;
|
||||
z -= oz * CHUNK_D;
|
||||
int cx = floordiv(x, CHUNK_W);
|
||||
int cz = floordiv(z, CHUNK_D);
|
||||
if (cx < 0 || cz < 0 || cx >= w || cz >= d)
|
||||
return;
|
||||
Chunk* chunk = chunks[cz * w + cx].get();
|
||||
@@ -143,17 +139,26 @@ void Chunks::set(int x, int y, int z, int id, uint8_t states){
|
||||
else if (y + 1 > chunk->top) chunk->top = y + 1;
|
||||
else if (id == 0) chunk->updateHeights();
|
||||
|
||||
if (lx == 0 && (chunk = getChunk(cx+ox-1, cz+oz))) chunk->setModified(true);
|
||||
if (lz == 0 && (chunk = getChunk(cx+ox, cz+oz-1))) chunk->setModified(true);
|
||||
if (lx == 0 && (chunk = getChunk(cx+ox-1, cz+oz)))
|
||||
chunk->setModified(true);
|
||||
if (lz == 0 && (chunk = getChunk(cx+ox, cz+oz-1)))
|
||||
chunk->setModified(true);
|
||||
|
||||
if (lx == CHUNK_W-1 && (chunk = getChunk(cx+ox+1, cz+oz))) chunk->setModified(true);
|
||||
if (lz == CHUNK_D-1 && (chunk = getChunk(cx+ox, cz+oz+1))) chunk->setModified(true);
|
||||
if (lx == CHUNK_W-1 && (chunk = getChunk(cx+ox+1, cz+oz)))
|
||||
chunk->setModified(true);
|
||||
if (lz == CHUNK_D-1 && (chunk = getChunk(cx+ox, cz+oz+1)))
|
||||
chunk->setModified(true);
|
||||
}
|
||||
|
||||
voxel* Chunks::rayCast(vec3 a, vec3 dir, float maxDist, vec3& end, vec3& norm, vec3& iend) {
|
||||
float px = a.x;
|
||||
float py = a.y;
|
||||
float pz = a.z;
|
||||
voxel* Chunks::rayCast(vec3 start,
|
||||
vec3 dir,
|
||||
float maxDist,
|
||||
vec3& end,
|
||||
vec3& norm,
|
||||
vec3& iend) {
|
||||
float px = start.x;
|
||||
float py = start.y;
|
||||
float pz = start.z;
|
||||
|
||||
float dx = dir.x;
|
||||
float dy = dir.y;
|
||||
@@ -239,14 +244,10 @@ voxel* Chunks::rayCast(vec3 a, vec3 dir, float maxDist, vec3& end, vec3& norm, v
|
||||
}
|
||||
|
||||
void Chunks::setCenter(int x, int z) {
|
||||
int cx = x / CHUNK_W;
|
||||
int cz = z / CHUNK_D;
|
||||
cx -= ox;
|
||||
cz -= oz;
|
||||
if (x < 0) cx--;
|
||||
if (z < 0) cz--;
|
||||
cx -= w/2;
|
||||
cz -= d/2;
|
||||
int cx = floordiv(x, CHUNK_W);
|
||||
int cz = floordiv(z, CHUNK_D);
|
||||
cx -= ox + w / 2;
|
||||
cz -= oz + d / 2;
|
||||
if (cx | cz) {
|
||||
translate(cx,cz);
|
||||
}
|
||||
@@ -295,8 +296,8 @@ void Chunks::resize(int newW, int newD) {
|
||||
translate(0, delta);
|
||||
}
|
||||
const int newVolume = newW * newD;
|
||||
shared_ptr<Chunk>* newChunks = new shared_ptr<Chunk>[newVolume] {};
|
||||
shared_ptr<Chunk>* newChunksSecond = new shared_ptr<Chunk>[newVolume] {};
|
||||
auto newChunks = new shared_ptr<Chunk>[newVolume] {};
|
||||
auto newChunksSecond = new shared_ptr<Chunk>[newVolume] {};
|
||||
for (int z = 0; z < d && z < newD; z++) {
|
||||
for (int x = 0; x < w && x < newW; x++) {
|
||||
newChunks[z * newW + x] = chunks[z * w + x];
|
||||
|
||||
+10
-2
@@ -13,6 +13,7 @@ class voxel;
|
||||
class WorldFiles;
|
||||
class LevelEvents;
|
||||
|
||||
/* Player-centred chunks matrix */
|
||||
class Chunks {
|
||||
public:
|
||||
std::shared_ptr<Chunk>* chunks;
|
||||
@@ -25,7 +26,8 @@ public:
|
||||
WorldFiles* worldFiles;
|
||||
LevelEvents* events;
|
||||
|
||||
Chunks(int w, int d, int ox, int oz, WorldFiles* worldFiles, LevelEvents* events);
|
||||
Chunks(int w, int d, int ox, int oz,
|
||||
WorldFiles* worldFiles, LevelEvents* events);
|
||||
~Chunks();
|
||||
|
||||
bool putChunk(std::shared_ptr<Chunk> chunk);
|
||||
@@ -36,7 +38,13 @@ public:
|
||||
light_t getLight(int x, int y, int z);
|
||||
ubyte getLight(int x, int y, int z, int channel);
|
||||
void set(int x, int y, int z, int id, uint8_t states);
|
||||
voxel* rayCast(glm::vec3 start, glm::vec3 dir, float maxLength, glm::vec3& end, glm::vec3& norm, glm::vec3& iend);
|
||||
|
||||
voxel* rayCast(glm::vec3 start,
|
||||
glm::vec3 dir,
|
||||
float maxLength,
|
||||
glm::vec3& end,
|
||||
glm::vec3& norm,
|
||||
glm::vec3& iend);
|
||||
|
||||
bool isObstacle(int x, int y, int z);
|
||||
|
||||
|
||||
@@ -15,16 +15,10 @@
|
||||
#include <memory>
|
||||
#include <chrono>
|
||||
|
||||
#if defined(_WIN32) && defined(__MINGW32__)
|
||||
#define _WIN32_WINNT 0x0501
|
||||
#include <mingw.thread.h>
|
||||
#else
|
||||
#include <thread>
|
||||
#endif
|
||||
|
||||
#define MAX_WORK_PER_FRAME 16
|
||||
#define MIN_SURROUNDING 9
|
||||
|
||||
using std::unique_ptr;
|
||||
using std::shared_ptr;
|
||||
using std::chrono::high_resolution_clock;
|
||||
using std::chrono::duration_cast;
|
||||
@@ -63,8 +57,8 @@ bool ChunksController::loadVisible(){
|
||||
int nearX = 0;
|
||||
int nearZ = 0;
|
||||
int minDistance = ((w-padding*2)/2)*((w-padding*2)/2);
|
||||
for (int z = padding; z < d-padding; z++){
|
||||
for (int x = padding; x < w-padding; x++){
|
||||
for (uint z = padding; z < d-padding; z++){
|
||||
for (uint x = padding; x < w-padding; x++){
|
||||
int index = z * w + x;
|
||||
shared_ptr<Chunk> chunk = chunks->chunks[index];
|
||||
if (chunk != nullptr){
|
||||
@@ -101,14 +95,7 @@ bool ChunksController::loadVisible(){
|
||||
return false;
|
||||
}
|
||||
|
||||
chunk = shared_ptr<Chunk>(new Chunk(nearX+ox, nearZ+oz));
|
||||
level->chunksStorage->store(chunk);
|
||||
ubyte* data = level->world->wfile->getChunk(chunk->x, chunk->z);
|
||||
if (data) {
|
||||
chunk->decode(data);
|
||||
chunk->setLoaded(true);
|
||||
delete[] data;
|
||||
}
|
||||
chunk = level->chunksStorage->create(nearX+ox, nearZ+oz);
|
||||
chunks->putChunk(chunk);
|
||||
|
||||
if (!chunk->isLoaded()) {
|
||||
|
||||
@@ -4,14 +4,18 @@
|
||||
|
||||
#include "VoxelsVolume.h"
|
||||
#include "Chunk.h"
|
||||
#include "../files/WorldFiles.h"
|
||||
#include "../world/Level.h"
|
||||
#include "../world/World.h"
|
||||
#include "../maths/voxmaths.h"
|
||||
#include "../lighting/Lightmap.h"
|
||||
|
||||
#include "../typedefs.h"
|
||||
|
||||
using glm::ivec2;
|
||||
using std::unique_ptr;
|
||||
using std::shared_ptr;
|
||||
|
||||
ChunksStorage::ChunksStorage() {
|
||||
ChunksStorage::ChunksStorage(Level* level) : level(level) {
|
||||
}
|
||||
|
||||
ChunksStorage::~ChunksStorage() {
|
||||
@@ -36,6 +40,17 @@ void ChunksStorage::remove(int x, int z) {
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
|
||||
auto chunk = shared_ptr<Chunk>(new Chunk(x, z));
|
||||
store(chunk);
|
||||
unique_ptr<ubyte> data(level->world->wfile->getChunk(chunk->x, chunk->z));
|
||||
if (data) {
|
||||
chunk->decode(data.get());
|
||||
chunk->setLoaded(true);
|
||||
}
|
||||
return chunk;
|
||||
}
|
||||
|
||||
// some magic code
|
||||
void ChunksStorage::getVoxels(VoxelsVolume* volume) const {
|
||||
voxel* voxels = volume->getVoxels();
|
||||
@@ -70,13 +85,13 @@ void ChunksStorage::getVoxels(VoxelsVolume* volume) const {
|
||||
for (int lx = max(x, cx * CHUNK_W);
|
||||
lx < min(x + w, (cx + 1) * CHUNK_W);
|
||||
lx++) {
|
||||
voxels[vox_index(lx - x, ly - y, lz - z, w, d)].id = BLOCK_VOID;
|
||||
lights[vox_index(lx - x, ly - y, lz - z, w, d)] = 0;
|
||||
uint idx = vox_index(lx - x, ly - y, lz - z, w, d);
|
||||
voxels[idx].id = BLOCK_VOID;
|
||||
lights[idx] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
const std::shared_ptr<Chunk>& chunk = found->second;
|
||||
const voxel* cvoxels = chunk->voxels;
|
||||
const light_t* clights = chunk->lightmap->getLights();
|
||||
@@ -87,10 +102,11 @@ void ChunksStorage::getVoxels(VoxelsVolume* volume) const {
|
||||
for (int lx = max(x, cx * CHUNK_W);
|
||||
lx < min(x + w, (cx + 1) * CHUNK_W);
|
||||
lx++) {
|
||||
voxels[vox_index(lx - x, ly - y, lz - z, w, d)] =
|
||||
cvoxels[vox_index(lx - cx * CHUNK_W, ly, lz - cz * CHUNK_D, CHUNK_W, CHUNK_D)];
|
||||
lights[vox_index(lx - x, ly - y, lz - z, w, d)] =
|
||||
clights[vox_index(lx - cx * CHUNK_W, ly, lz - cz * CHUNK_D, CHUNK_W, CHUNK_D)];
|
||||
uint vidx = vox_index(lx - x, ly - y, lz - z, w, d);
|
||||
uint cidx = vox_index(lx - cx * CHUNK_W, ly,
|
||||
lz - cz * CHUNK_D, CHUNK_W, CHUNK_D);
|
||||
voxels[vidx] = cvoxels[cidx];
|
||||
lights[vidx] = clights[cidx];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,18 +10,21 @@
|
||||
#include "glm/gtx/hash.hpp"
|
||||
|
||||
class Chunk;
|
||||
class Level;
|
||||
class VoxelsVolume;
|
||||
|
||||
class ChunksStorage {
|
||||
Level* level;
|
||||
std::unordered_map<glm::ivec2, std::shared_ptr<Chunk>> chunksMap;
|
||||
public:
|
||||
ChunksStorage();
|
||||
ChunksStorage(Level* level);
|
||||
virtual ~ChunksStorage();
|
||||
|
||||
std::shared_ptr<Chunk> get(int x, int y) const;
|
||||
std::shared_ptr<Chunk> get(int x, int z) const;
|
||||
void store(std::shared_ptr<Chunk> chunk);
|
||||
void remove(int x, int y);
|
||||
void getVoxels(VoxelsVolume* volume) const;
|
||||
std::shared_ptr<Chunk> create(int x, int z);
|
||||
|
||||
light_t getLight(int x, int y, int z, ubyte channel) const;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user