Added generation seed support

This commit is contained in:
MihailRis 2022-07-01 02:31:30 +03:00 committed by GitHub
parent 37dd882fdb
commit 1038a79b78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 25 additions and 17 deletions

View File

@ -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* 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);
Camera* camera = player->camera;
@ -129,7 +129,7 @@ int main() {
std::cout << "-- loading world" << std::endl;
vec3 playerPosition = vec3(-320,200,32);
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);
Level* level = load_level(world, player);
@ -142,7 +142,7 @@ int main() {
bool occlusion = false;
bool devdata = false;
Window::swapInterval(0);
Window::swapInterval(1);
std::cout << "-- initializing finished" << std::endl;
while (!Window::isShouldClose()){

View File

@ -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++){

View File

@ -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();

View File

@ -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);

View File

@ -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(){

View File

@ -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;

View File

@ -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_ */

View File

@ -2,12 +2,12 @@
#include "../lighting/Lighting.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),
chunks(chunks),
physics(physics) {
lighting = new Lighting(chunks);
chunksController = new ChunksController(chunks, lighting);
chunksController = new ChunksController(world, chunks, lighting);
}
Level::~Level(){

View File

@ -1,6 +1,7 @@
#ifndef WORLD_LEVEL_H_
#define WORLD_LEVEL_H_
class World;
class Player;
class Chunks;
class Lighting;
@ -14,7 +15,7 @@ public:
PhysicsSolver* physics;
Lighting* lighting;
ChunksController* chunksController;
Level(Player* player, Chunks* chunks, PhysicsSolver* physics);
Level(World* world, Player* player, Chunks* chunks, PhysicsSolver* physics);
~Level();
};

View File

@ -4,7 +4,7 @@
#include "../voxels/Chunk.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));
}

View File

@ -10,8 +10,9 @@ class World {
public:
std::string name;
WorldFiles* wfile;
int seed;
World(std::string name, std::string directory);
World(std::string name, std::string directory, int seed);
~World();
};