Added generation seed support
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
#include "../files/WorldFiles.h"
|
||||
#include "ChunksLoader.h"
|
||||
#include <iostream>
|
||||
#include <limits.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#define _WIN32_WINNT 0x0501
|
||||
@@ -19,13 +20,13 @@
|
||||
#define MIN_SURROUNDING 9
|
||||
|
||||
|
||||
ChunksController::ChunksController(Chunks* chunks, Lighting* lighting) : chunks(chunks), lighting(lighting){
|
||||
ChunksController::ChunksController(World* world, Chunks* chunks, Lighting* lighting) : chunks(chunks), lighting(lighting){
|
||||
loadersCount = std::thread::hardware_concurrency() * 2 - 1;
|
||||
if (loadersCount <= 0)
|
||||
loadersCount = 1;
|
||||
loaders = new ChunksLoader*[loadersCount];
|
||||
for (int i = 0; i < loadersCount; i++){
|
||||
loaders[i] = new ChunksLoader();
|
||||
loaders[i] = new ChunksLoader(world);
|
||||
}
|
||||
std::cout << "created " << loadersCount << " loaders" << std::endl;
|
||||
}
|
||||
@@ -173,7 +174,7 @@ bool ChunksController::_buildMeshes(VoxelRenderer* renderer, int tick) {
|
||||
int nearX = 0;
|
||||
int nearY = 0;
|
||||
int nearZ = 0;
|
||||
int minDistance = 1000000000;
|
||||
int minDistance = INT_MAX;
|
||||
for (int y = 0; y < h; y++){
|
||||
for (int z = 1; z < d-1; z++){
|
||||
for (int x = 1; x < w-1; x++){
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef VOXELS_CHUNKSCONTROLLER_H_
|
||||
#define VOXELS_CHUNKSCONTROLLER_H_
|
||||
|
||||
class World;
|
||||
class Chunks;
|
||||
class Lighting;
|
||||
class WorldFiles;
|
||||
@@ -14,7 +15,7 @@ private:
|
||||
ChunksLoader** loaders;
|
||||
int loadersCount;
|
||||
public:
|
||||
ChunksController(Chunks* chunks, Lighting* lighting);
|
||||
ChunksController(World* world, Chunks* chunks, Lighting* lighting);
|
||||
~ChunksController();
|
||||
|
||||
int countFreeLoaders();
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "Chunk.h"
|
||||
#include "Chunks.h"
|
||||
#include "../world/World.h"
|
||||
#include "WorldGenerator.h"
|
||||
#include "../lighting/Lighting.h"
|
||||
#include "../graphics/VoxelRenderer.h"
|
||||
@@ -32,7 +33,7 @@ void ChunksLoader::_thread(){
|
||||
if (state == LOAD){
|
||||
chunks.putChunk(chunk);
|
||||
if (!chunk->loaded){
|
||||
WorldGenerator::generate(chunk->voxels, chunk->x, chunk->y, chunk->z);
|
||||
WorldGenerator::generate(chunk->voxels, chunk->x, chunk->y, chunk->z, world.load()->seed);
|
||||
}
|
||||
|
||||
lighting.onChunkLoaded(chunk->x, chunk->y, chunk->z, true);
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <atomic>
|
||||
|
||||
class Chunk;
|
||||
class World;
|
||||
|
||||
enum LoaderMode {
|
||||
OFF, IDLE, LOAD, RENDER,
|
||||
@@ -22,11 +23,12 @@ private:
|
||||
void _thread();
|
||||
std::atomic<Chunk*> current {nullptr};
|
||||
std::atomic<Chunk**> closes {nullptr};
|
||||
std::atomic<World*> world {nullptr};
|
||||
std::atomic<LoaderMode> state {IDLE};
|
||||
|
||||
void perform(Chunk* chunk, Chunk** closes_passed, LoaderMode mode);
|
||||
public:
|
||||
ChunksLoader() : loaderThread{} {
|
||||
ChunksLoader(World* world) : loaderThread{}, world(world) {
|
||||
loaderThread = std::thread{&ChunksLoader::_thread, this};
|
||||
}
|
||||
~ChunksLoader(){
|
||||
|
||||
@@ -85,9 +85,10 @@ int generate_tree(fnl_state *noise, PseudoRandom* random, const float* heights,
|
||||
return 0;
|
||||
}
|
||||
|
||||
void WorldGenerator::generate(voxel* voxels, int cx, int cy, int cz){
|
||||
void WorldGenerator::generate(voxel* voxels, int cx, int cy, int cz, int seed){
|
||||
fnl_state noise = fnlCreateState();
|
||||
noise.noise_type = FNL_NOISE_OPENSIMPLEX2;
|
||||
noise.seed = seed * 60617077 % 25896307;
|
||||
|
||||
PseudoRandom random;
|
||||
|
||||
@@ -144,4 +145,4 @@ void WorldGenerator::generate(voxel* voxels, int cx, int cy, int cz){
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ class voxel;
|
||||
|
||||
class WorldGenerator {
|
||||
public:
|
||||
static void generate(voxel* voxels, int x, int y, int z);
|
||||
static void generate(voxel* voxels, int x, int y, int z, int seed);
|
||||
};
|
||||
|
||||
#endif /* VOXELS_WORLDGENERATOR_H_ */
|
||||
|
||||
Reference in New Issue
Block a user