Added generation seed support
This commit is contained in:
parent
37dd882fdb
commit
1038a79b78
@ -92,7 +92,7 @@ void update_level(World* world, Level* level, vec3 position, float delta, long f
|
|||||||
}
|
}
|
||||||
|
|
||||||
Level* load_level(World* world, Player* player) {
|
Level* load_level(World* world, Player* player) {
|
||||||
Level* level = new Level(player, new Chunks(34,1,34, 0,0,0), new PhysicsSolver(vec3(0, -gravity, 0)));
|
Level* level = new Level(world, player, new Chunks(32,1,32, 0,0,0), new PhysicsSolver(vec3(0, -gravity, 0)));
|
||||||
world->wfile->readPlayer(player);
|
world->wfile->readPlayer(player);
|
||||||
|
|
||||||
Camera* camera = player->camera;
|
Camera* camera = player->camera;
|
||||||
@ -129,7 +129,7 @@ int main() {
|
|||||||
std::cout << "-- loading world" << std::endl;
|
std::cout << "-- loading world" << std::endl;
|
||||||
vec3 playerPosition = vec3(-320,200,32);
|
vec3 playerPosition = vec3(-320,200,32);
|
||||||
Camera* camera = new Camera(playerPosition, radians(90.0f));
|
Camera* camera = new Camera(playerPosition, radians(90.0f));
|
||||||
World* world = new World("world-1", "world/");
|
World* world = new World("world-1", "world/", 42);
|
||||||
Player* player = new Player(playerPosition, 4.0f, camera);
|
Player* player = new Player(playerPosition, 4.0f, camera);
|
||||||
Level* level = load_level(world, player);
|
Level* level = load_level(world, player);
|
||||||
|
|
||||||
@ -142,7 +142,7 @@ int main() {
|
|||||||
bool occlusion = false;
|
bool occlusion = false;
|
||||||
bool devdata = false;
|
bool devdata = false;
|
||||||
|
|
||||||
Window::swapInterval(0);
|
Window::swapInterval(1);
|
||||||
|
|
||||||
std::cout << "-- initializing finished" << std::endl;
|
std::cout << "-- initializing finished" << std::endl;
|
||||||
while (!Window::isShouldClose()){
|
while (!Window::isShouldClose()){
|
||||||
|
|||||||
@ -8,6 +8,7 @@
|
|||||||
#include "../files/WorldFiles.h"
|
#include "../files/WorldFiles.h"
|
||||||
#include "ChunksLoader.h"
|
#include "ChunksLoader.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#define _WIN32_WINNT 0x0501
|
#define _WIN32_WINNT 0x0501
|
||||||
@ -19,13 +20,13 @@
|
|||||||
#define MIN_SURROUNDING 9
|
#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;
|
loadersCount = std::thread::hardware_concurrency() * 2 - 1;
|
||||||
if (loadersCount <= 0)
|
if (loadersCount <= 0)
|
||||||
loadersCount = 1;
|
loadersCount = 1;
|
||||||
loaders = new ChunksLoader*[loadersCount];
|
loaders = new ChunksLoader*[loadersCount];
|
||||||
for (int i = 0; i < loadersCount; i++){
|
for (int i = 0; i < loadersCount; i++){
|
||||||
loaders[i] = new ChunksLoader();
|
loaders[i] = new ChunksLoader(world);
|
||||||
}
|
}
|
||||||
std::cout << "created " << loadersCount << " loaders" << std::endl;
|
std::cout << "created " << loadersCount << " loaders" << std::endl;
|
||||||
}
|
}
|
||||||
@ -173,7 +174,7 @@ bool ChunksController::_buildMeshes(VoxelRenderer* renderer, int tick) {
|
|||||||
int nearX = 0;
|
int nearX = 0;
|
||||||
int nearY = 0;
|
int nearY = 0;
|
||||||
int nearZ = 0;
|
int nearZ = 0;
|
||||||
int minDistance = 1000000000;
|
int minDistance = INT_MAX;
|
||||||
for (int y = 0; y < h; y++){
|
for (int y = 0; y < h; y++){
|
||||||
for (int z = 1; z < d-1; z++){
|
for (int z = 1; z < d-1; z++){
|
||||||
for (int x = 1; x < w-1; x++){
|
for (int x = 1; x < w-1; x++){
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
#ifndef VOXELS_CHUNKSCONTROLLER_H_
|
#ifndef VOXELS_CHUNKSCONTROLLER_H_
|
||||||
#define VOXELS_CHUNKSCONTROLLER_H_
|
#define VOXELS_CHUNKSCONTROLLER_H_
|
||||||
|
|
||||||
|
class World;
|
||||||
class Chunks;
|
class Chunks;
|
||||||
class Lighting;
|
class Lighting;
|
||||||
class WorldFiles;
|
class WorldFiles;
|
||||||
@ -14,7 +15,7 @@ private:
|
|||||||
ChunksLoader** loaders;
|
ChunksLoader** loaders;
|
||||||
int loadersCount;
|
int loadersCount;
|
||||||
public:
|
public:
|
||||||
ChunksController(Chunks* chunks, Lighting* lighting);
|
ChunksController(World* world, Chunks* chunks, Lighting* lighting);
|
||||||
~ChunksController();
|
~ChunksController();
|
||||||
|
|
||||||
int countFreeLoaders();
|
int countFreeLoaders();
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include "Chunk.h"
|
#include "Chunk.h"
|
||||||
#include "Chunks.h"
|
#include "Chunks.h"
|
||||||
|
#include "../world/World.h"
|
||||||
#include "WorldGenerator.h"
|
#include "WorldGenerator.h"
|
||||||
#include "../lighting/Lighting.h"
|
#include "../lighting/Lighting.h"
|
||||||
#include "../graphics/VoxelRenderer.h"
|
#include "../graphics/VoxelRenderer.h"
|
||||||
@ -32,7 +33,7 @@ void ChunksLoader::_thread(){
|
|||||||
if (state == LOAD){
|
if (state == LOAD){
|
||||||
chunks.putChunk(chunk);
|
chunks.putChunk(chunk);
|
||||||
if (!chunk->loaded){
|
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);
|
lighting.onChunkLoaded(chunk->x, chunk->y, chunk->z, true);
|
||||||
|
|||||||
@ -11,6 +11,7 @@
|
|||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
|
||||||
class Chunk;
|
class Chunk;
|
||||||
|
class World;
|
||||||
|
|
||||||
enum LoaderMode {
|
enum LoaderMode {
|
||||||
OFF, IDLE, LOAD, RENDER,
|
OFF, IDLE, LOAD, RENDER,
|
||||||
@ -22,11 +23,12 @@ private:
|
|||||||
void _thread();
|
void _thread();
|
||||||
std::atomic<Chunk*> current {nullptr};
|
std::atomic<Chunk*> current {nullptr};
|
||||||
std::atomic<Chunk**> closes {nullptr};
|
std::atomic<Chunk**> closes {nullptr};
|
||||||
|
std::atomic<World*> world {nullptr};
|
||||||
std::atomic<LoaderMode> state {IDLE};
|
std::atomic<LoaderMode> state {IDLE};
|
||||||
|
|
||||||
void perform(Chunk* chunk, Chunk** closes_passed, LoaderMode mode);
|
void perform(Chunk* chunk, Chunk** closes_passed, LoaderMode mode);
|
||||||
public:
|
public:
|
||||||
ChunksLoader() : loaderThread{} {
|
ChunksLoader(World* world) : loaderThread{}, world(world) {
|
||||||
loaderThread = std::thread{&ChunksLoader::_thread, this};
|
loaderThread = std::thread{&ChunksLoader::_thread, this};
|
||||||
}
|
}
|
||||||
~ChunksLoader(){
|
~ChunksLoader(){
|
||||||
|
|||||||
@ -85,9 +85,10 @@ int generate_tree(fnl_state *noise, PseudoRandom* random, const float* heights,
|
|||||||
return 0;
|
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();
|
fnl_state noise = fnlCreateState();
|
||||||
noise.noise_type = FNL_NOISE_OPENSIMPLEX2;
|
noise.noise_type = FNL_NOISE_OPENSIMPLEX2;
|
||||||
|
noise.seed = seed * 60617077 % 25896307;
|
||||||
|
|
||||||
PseudoRandom random;
|
PseudoRandom random;
|
||||||
|
|
||||||
|
|||||||
@ -5,7 +5,7 @@ class voxel;
|
|||||||
|
|
||||||
class WorldGenerator {
|
class WorldGenerator {
|
||||||
public:
|
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_ */
|
#endif /* VOXELS_WORLDGENERATOR_H_ */
|
||||||
|
|||||||
@ -2,12 +2,12 @@
|
|||||||
#include "../lighting/Lighting.h"
|
#include "../lighting/Lighting.h"
|
||||||
#include "../voxels/ChunksController.h"
|
#include "../voxels/ChunksController.h"
|
||||||
|
|
||||||
Level::Level(Player* player, Chunks* chunks, PhysicsSolver* physics) :
|
Level::Level(World* world, Player* player, Chunks* chunks, PhysicsSolver* physics) :
|
||||||
player(player),
|
player(player),
|
||||||
chunks(chunks),
|
chunks(chunks),
|
||||||
physics(physics) {
|
physics(physics) {
|
||||||
lighting = new Lighting(chunks);
|
lighting = new Lighting(chunks);
|
||||||
chunksController = new ChunksController(chunks, lighting);
|
chunksController = new ChunksController(world, chunks, lighting);
|
||||||
}
|
}
|
||||||
|
|
||||||
Level::~Level(){
|
Level::~Level(){
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
#ifndef WORLD_LEVEL_H_
|
#ifndef WORLD_LEVEL_H_
|
||||||
#define WORLD_LEVEL_H_
|
#define WORLD_LEVEL_H_
|
||||||
|
|
||||||
|
class World;
|
||||||
class Player;
|
class Player;
|
||||||
class Chunks;
|
class Chunks;
|
||||||
class Lighting;
|
class Lighting;
|
||||||
@ -14,7 +15,7 @@ public:
|
|||||||
PhysicsSolver* physics;
|
PhysicsSolver* physics;
|
||||||
Lighting* lighting;
|
Lighting* lighting;
|
||||||
ChunksController* chunksController;
|
ChunksController* chunksController;
|
||||||
Level(Player* player, Chunks* chunks, PhysicsSolver* physics);
|
Level(World* world, Player* player, Chunks* chunks, PhysicsSolver* physics);
|
||||||
~Level();
|
~Level();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,7 @@
|
|||||||
#include "../voxels/Chunk.h"
|
#include "../voxels/Chunk.h"
|
||||||
#include "../voxels/Chunks.h"
|
#include "../voxels/Chunks.h"
|
||||||
|
|
||||||
World::World(std::string name, std::string directory) : name(name) {
|
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_VOL * 2 + 8));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -10,8 +10,9 @@ class World {
|
|||||||
public:
|
public:
|
||||||
std::string name;
|
std::string name;
|
||||||
WorldFiles* wfile;
|
WorldFiles* wfile;
|
||||||
|
int seed;
|
||||||
|
|
||||||
World(std::string name, std::string directory);
|
World(std::string name, std::string directory, int seed);
|
||||||
~World();
|
~World();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user