diff --git a/src/voxel_engine.cpp b/src/voxel_engine.cpp index 9c1a0bed..9fc3e307 100644 --- a/src/voxel_engine.cpp +++ b/src/voxel_engine.cpp @@ -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()){ diff --git a/src/voxels/ChunksController.cpp b/src/voxels/ChunksController.cpp index 05b1ab28..96b2cac4 100644 --- a/src/voxels/ChunksController.cpp +++ b/src/voxels/ChunksController.cpp @@ -8,6 +8,7 @@ #include "../files/WorldFiles.h" #include "ChunksLoader.h" #include +#include #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++){ diff --git a/src/voxels/ChunksController.h b/src/voxels/ChunksController.h index d31d925f..9212e207 100644 --- a/src/voxels/ChunksController.h +++ b/src/voxels/ChunksController.h @@ -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(); diff --git a/src/voxels/ChunksLoader.cpp b/src/voxels/ChunksLoader.cpp index be82d168..f71b5363 100644 --- a/src/voxels/ChunksLoader.cpp +++ b/src/voxels/ChunksLoader.cpp @@ -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); diff --git a/src/voxels/ChunksLoader.h b/src/voxels/ChunksLoader.h index 98433b7c..5e5a5b22 100644 --- a/src/voxels/ChunksLoader.h +++ b/src/voxels/ChunksLoader.h @@ -11,6 +11,7 @@ #include class Chunk; +class World; enum LoaderMode { OFF, IDLE, LOAD, RENDER, @@ -22,11 +23,12 @@ private: void _thread(); std::atomic current {nullptr}; std::atomic closes {nullptr}; + std::atomic world {nullptr}; std::atomic 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(){ diff --git a/src/voxels/WorldGenerator.cpp b/src/voxels/WorldGenerator.cpp index 9d6d5afc..a1495004 100644 --- a/src/voxels/WorldGenerator.cpp +++ b/src/voxels/WorldGenerator.cpp @@ -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){ } } } -} \ No newline at end of file +} diff --git a/src/voxels/WorldGenerator.h b/src/voxels/WorldGenerator.h index c04dbd31..ae164319 100644 --- a/src/voxels/WorldGenerator.h +++ b/src/voxels/WorldGenerator.h @@ -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_ */ diff --git a/src/world/Level.cpp b/src/world/Level.cpp index 3faa316f..7770d124 100644 --- a/src/world/Level.cpp +++ b/src/world/Level.cpp @@ -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(){ diff --git a/src/world/Level.h b/src/world/Level.h index 8284cbb0..de4bf554 100644 --- a/src/world/Level.h +++ b/src/world/Level.h @@ -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(); }; diff --git a/src/world/World.cpp b/src/world/World.cpp index 92e7c0e2..2e336dce 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -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)); } diff --git a/src/world/World.h b/src/world/World.h index 019601bd..7b51192b 100644 --- a/src/world/World.h +++ b/src/world/World.h @@ -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(); };