Add files via upload
This commit is contained in:
parent
549b392871
commit
a5087cf221
@ -12,6 +12,9 @@
|
||||
#include "audio/Audio.h"
|
||||
#include "assets/Assets.h"
|
||||
#include "assets/AssetsLoader.h"
|
||||
#include "world/WorldGenerators.h"
|
||||
#include "voxels/DefaultWorldGenerator.h"
|
||||
#include "voxels/FlatWorldGenerator.h"
|
||||
#include "window/Window.h"
|
||||
#include "window/Events.h"
|
||||
#include "window/Camera.h"
|
||||
@ -80,6 +83,12 @@ Engine::Engine(EngineSettings& settings, EnginePaths* paths)
|
||||
menus::create_version_label(this);
|
||||
}
|
||||
setLanguage(settings.ui.language);
|
||||
addWorldGenerators();
|
||||
}
|
||||
|
||||
void addWorldGenerators() {
|
||||
WorldGenerators::addWorldGenerator<DefaultWorldGenerator>("core:default");
|
||||
WorldGenerators::addWorldGenerator<FlatWorldGenerator>("core:flat");
|
||||
}
|
||||
|
||||
void Engine::updateTimers() {
|
||||
@ -251,4 +260,4 @@ EnginePaths* Engine::getPaths() {
|
||||
|
||||
std::shared_ptr<Screen> Engine::getScreen() {
|
||||
return screen;
|
||||
}
|
||||
}
|
||||
@ -19,6 +19,7 @@
|
||||
#include "../files/WorldConverter.h"
|
||||
#include "../files/WorldFiles.h"
|
||||
#include "../world/World.h"
|
||||
#include "../world/WorldGenerators.h"
|
||||
#include "../world/Level.h"
|
||||
#include "../window/Events.h"
|
||||
#include "../window/Window.h"
|
||||
@ -38,6 +39,10 @@ using glm::vec4;
|
||||
namespace fs = std::filesystem;
|
||||
using namespace gui;
|
||||
|
||||
namespace menus {
|
||||
std::string generatorID;
|
||||
}
|
||||
|
||||
inline uint64_t randU64() {
|
||||
srand(time(NULL));
|
||||
return rand() ^ (rand() << 8) ^
|
||||
@ -82,7 +87,6 @@ static std::shared_ptr<Button> create_button(
|
||||
return btn;
|
||||
}
|
||||
|
||||
|
||||
void menus::create_version_label(Engine* engine) {
|
||||
auto gui = engine->getGUI();
|
||||
auto vlabel = std::make_shared<gui::Label>(
|
||||
@ -182,6 +186,28 @@ void create_languages_panel(Engine* engine) {
|
||||
panel->add(guiutil::backButton(menu));
|
||||
}
|
||||
|
||||
void create_world_generators_panel(Engine* engine) {
|
||||
auto menu = engine->getGUI()->getMenu();
|
||||
auto panel = create_page(engine, "world_generators", 400, 0.5f, 1);
|
||||
panel->setScrollable(true);
|
||||
|
||||
std::vector<std::string> generatorsIDs = WorldGenerators::getGeneratorsIDs();
|
||||
std::sort(generatorsIDs.begin(), generatorsIDs.end());
|
||||
for (std::string& type : generatorsIDs) {
|
||||
const std::string& fullName = util::wstr2str_utf8(langs::get(util::str2wstr_utf8(type), L"world.generators"));
|
||||
auto button = std::make_shared<Button>(
|
||||
util::str2wstr_utf8(fullName),
|
||||
vec4(10.f),
|
||||
[=](GUI*) {
|
||||
menus::generatorID = type;
|
||||
menu->back();
|
||||
}
|
||||
);
|
||||
panel->add(button);
|
||||
}
|
||||
panel->add(guiutil::backButton(menu));
|
||||
}
|
||||
|
||||
void open_world(std::string name, Engine* engine) {
|
||||
auto paths = engine->getPaths();
|
||||
auto folder = paths->getWorldsFolder()/fs::u8path(name);
|
||||
@ -428,6 +454,8 @@ void create_new_world_panel(Engine* engine) {
|
||||
auto seedInput = std::make_shared<TextBox>(seedstr, vec4(6.0f));
|
||||
panel->add(seedInput);
|
||||
|
||||
panel->add(guiutil::gotoButton(langs::get(L"World generator", L"world"), "world_generators", engine->getGUI()->getMenu()));
|
||||
|
||||
panel->add(create_button( L"Create World", vec4(10), vec4(1, 20, 1, 1),
|
||||
[=](GUI*) {
|
||||
if (!nameInput->validate())
|
||||
@ -464,11 +492,12 @@ void create_new_world_panel(Engine* engine) {
|
||||
}
|
||||
|
||||
Level* level = World::create(
|
||||
name, folder, seed,
|
||||
name, menus::generatorID, folder, seed,
|
||||
engine->getSettings(),
|
||||
engine->getContent(),
|
||||
engine->getContentPacks()
|
||||
);
|
||||
menus::generatorID = WorldGenerators::getDefaultWorldGeneratorID();
|
||||
engine->setScreen(std::make_shared<LevelScreen>(engine, level));
|
||||
}));
|
||||
panel->add(guiutil::backButton(engine->getGUI()->getMenu()));
|
||||
@ -655,15 +684,18 @@ void create_pause_panel(Engine* engine) {
|
||||
}
|
||||
|
||||
void menus::create_menus(Engine* engine) {
|
||||
menus::generatorID = WorldGenerators::getDefaultWorldGeneratorID();
|
||||
create_new_world_panel(engine);
|
||||
create_settings_panel(engine);
|
||||
create_controls_panel(engine);
|
||||
create_pause_panel(engine);
|
||||
create_languages_panel(engine);
|
||||
create_world_generators_panel(engine);
|
||||
create_main_menu_panel(engine);
|
||||
}
|
||||
|
||||
void menus::refresh_menus(Engine* engine) {
|
||||
create_main_menu_panel(engine);
|
||||
create_new_world_panel(engine);
|
||||
}
|
||||
create_world_generators_panel(engine);
|
||||
}
|
||||
@ -10,6 +10,7 @@
|
||||
#include "../voxels/Chunks.h"
|
||||
#include "../voxels/ChunksStorage.h"
|
||||
#include "../voxels/WorldGenerator.h"
|
||||
#include "../world/WorldGenerators.h"
|
||||
#include "../graphics/Mesh.h"
|
||||
#include "../lighting/Lighting.h"
|
||||
#include "../files/WorldFiles.h"
|
||||
@ -26,7 +27,7 @@ ChunksController::ChunksController(Level* level, uint padding)
|
||||
chunks(level->chunks),
|
||||
lighting(level->lighting),
|
||||
padding(padding),
|
||||
generator(new WorldGenerator(level->content)) {
|
||||
generator(WorldGenerators::createWorldGenerator(level->getWorld()->getGenerator(), level->content)) {
|
||||
}
|
||||
|
||||
ChunksController::~ChunksController(){
|
||||
@ -130,4 +131,4 @@ void ChunksController::createChunk(int x, int z) {
|
||||
}
|
||||
chunk->setLoaded(true);
|
||||
chunk->setReady(true);
|
||||
}
|
||||
}
|
||||
208
src/voxels/DefaultWorldGenerator.cpp
Normal file
208
src/voxels/DefaultWorldGenerator.cpp
Normal file
@ -0,0 +1,208 @@
|
||||
#include "DefaultWorldGenerator.h"
|
||||
#include "WorldGenerator.h"
|
||||
#include "voxel.h"
|
||||
#include "Chunk.h"
|
||||
#include "Block.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <time.h>
|
||||
#include <stdexcept>
|
||||
#include <math.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/noise.hpp>
|
||||
#define FNL_IMPL
|
||||
#include "../maths/FastNoiseLite.h"
|
||||
|
||||
#include "../content/Content.h"
|
||||
#include "../maths/voxmaths.h"
|
||||
#include "../maths/util.h"
|
||||
#include "../core_defs.h"
|
||||
|
||||
// TODO: do something with long conditions + move magic numbers to constants
|
||||
|
||||
const int SEA_LEVEL = 55;
|
||||
|
||||
enum class MAPS{
|
||||
SAND,
|
||||
TREE,
|
||||
CLIFF,
|
||||
HEIGHT
|
||||
};
|
||||
#define MAPS_LEN 4
|
||||
|
||||
class Map2D {
|
||||
int x, z;
|
||||
int w, d;
|
||||
float* heights[MAPS_LEN];
|
||||
public:
|
||||
Map2D(int x, int z, int w, int d) : x(x), z(z), w(w), d(d) {
|
||||
for (int i = 0; i < MAPS_LEN; i++)
|
||||
heights[i] = new float[w*d];
|
||||
}
|
||||
~Map2D() {
|
||||
for (int i = 0; i < MAPS_LEN; i++)
|
||||
delete[] heights[i];
|
||||
}
|
||||
|
||||
inline float get(MAPS map, int x, int z) {
|
||||
x -= this->x;
|
||||
z -= this->z;
|
||||
if (x < 0 || z < 0 || x >= w || z >= d) {
|
||||
throw std::runtime_error("out of heightmap");
|
||||
}
|
||||
return heights[(int)map][z * w + x];
|
||||
}
|
||||
|
||||
inline void set(MAPS map, int x, int z, float value) {
|
||||
x -= this->x;
|
||||
z -= this->z;
|
||||
if (x < 0 || z < 0 || x >= w || z >= d) {
|
||||
throw std::runtime_error("out of heightmap");
|
||||
}
|
||||
heights[(int)map][z * w + x] = value;
|
||||
}
|
||||
};
|
||||
|
||||
float calc_height(fnl_state *noise, int cur_x, int cur_z){
|
||||
float height = 0;
|
||||
|
||||
height += fnlGetNoise2D(noise, cur_x*0.0125f*8-125567,cur_z*0.0125f*8+3546);
|
||||
height += fnlGetNoise2D(noise, cur_x*0.025f*8+4647,cur_z*0.025f*8-3436)*0.5f;
|
||||
height += fnlGetNoise2D(noise, cur_x*0.05f*8-834176,cur_z*0.05f*8+23678)*0.25f;
|
||||
height += fnlGetNoise2D(noise,
|
||||
cur_x*0.2f*8 + fnlGetNoise2D(noise, cur_x*0.1f*8-23557,cur_z*0.1f*8-6568)*50,
|
||||
cur_z*0.2f*8 + fnlGetNoise2D(noise, cur_x*0.1f*8+4363,cur_z*0.1f*8+4456)*50
|
||||
) * fnlGetNoise2D(noise, cur_x*0.01f-834176,cur_z*0.01f+23678) * 0.25;
|
||||
height += fnlGetNoise2D(noise, cur_x*0.1f*8-3465,cur_z*0.1f*8+4534)*0.125f;
|
||||
height *= fnlGetNoise2D(noise, cur_x*0.1f+1000,cur_z*0.1f+1000)*0.5f+0.5f;
|
||||
height += 1.0f;
|
||||
height *= 64.0f;
|
||||
return height;
|
||||
}
|
||||
|
||||
int generate_tree(fnl_state *noise,
|
||||
PseudoRandom* random,
|
||||
Map2D& heights,
|
||||
// Map2D& humidity,
|
||||
int cur_x,
|
||||
int cur_y,
|
||||
int cur_z,
|
||||
int tileSize,
|
||||
blockid_t idWood,
|
||||
blockid_t idLeaves){
|
||||
const int tileX = floordiv(cur_x, tileSize);
|
||||
const int tileZ = floordiv(cur_z, tileSize);
|
||||
|
||||
random->setSeed(tileX*4325261+tileZ*12160951+tileSize*9431111);
|
||||
|
||||
int randomX = (random->rand() % (tileSize/2)) - tileSize/4;
|
||||
int randomZ = (random->rand() % (tileSize/2)) - tileSize/4;
|
||||
|
||||
int centerX = tileX * tileSize + tileSize/2 + randomX;
|
||||
int centerZ = tileZ * tileSize + tileSize/2 + randomZ;
|
||||
|
||||
bool gentree = (random->rand() % 10) < heights.get(MAPS::TREE, centerX, centerZ) * 13;
|
||||
if (!gentree)
|
||||
return 0;
|
||||
|
||||
int height = (int)(heights.get(MAPS::HEIGHT, centerX, centerZ));
|
||||
if (height < SEA_LEVEL+1)
|
||||
return 0;
|
||||
int lx = cur_x - centerX;
|
||||
int radius = random->rand() % 4 + 2;
|
||||
int ly = cur_y - height - 3 * radius;
|
||||
int lz = cur_z - centerZ;
|
||||
if (lx == 0 && lz == 0 && cur_y - height < (3*radius + radius/2))
|
||||
return idWood;
|
||||
if (lx*lx+ly*ly/2+lz*lz < radius*radius)
|
||||
return idLeaves;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void WorldGenerator::generate(voxel* voxels, int cx, int cz, int seed){
|
||||
const int treesTile = 12;
|
||||
fnl_state noise = fnlCreateState();
|
||||
noise.noise_type = FNL_NOISE_OPENSIMPLEX2;
|
||||
noise.seed = seed * 60617077 % 25896307;
|
||||
PseudoRandom randomtree;
|
||||
PseudoRandom randomgrass;
|
||||
|
||||
int padding = 8;
|
||||
Map2D heights(cx * CHUNK_W - padding,
|
||||
cz * CHUNK_D - padding,
|
||||
CHUNK_W + padding * 2,
|
||||
CHUNK_D + padding * 2);
|
||||
|
||||
for (int z = -padding; z < CHUNK_D+padding; z++){
|
||||
for (int x = -padding; x < CHUNK_W+padding; x++){
|
||||
int cur_x = x + cx * CHUNK_W;
|
||||
int cur_z = z + cz * CHUNK_D;
|
||||
float height = calc_height(&noise, cur_x, cur_z);
|
||||
float hum = fnlGetNoise2D(&noise, cur_x * 0.3 + 633, cur_z * 0.3);
|
||||
float sand = fnlGetNoise2D(&noise, cur_x * 0.1 - 633, cur_z * 0.1 + 1000);
|
||||
float cliff = pow((sand + abs(sand)) / 2, 2);
|
||||
float w = pow(fmax(-abs(height-SEA_LEVEL)+4,0)/6,2) * cliff;
|
||||
float h1 = -abs(height-SEA_LEVEL - 0.03);
|
||||
float h2 = abs(height-SEA_LEVEL + 0.04);
|
||||
float h = (h1 + h2)*100;
|
||||
height += (h * w);
|
||||
heights.set(MAPS::HEIGHT, cur_x, cur_z, height);
|
||||
heights.set(MAPS::TREE, cur_x, cur_z, hum);
|
||||
heights.set(MAPS::SAND, cur_x, cur_z, sand);
|
||||
heights.set(MAPS::CLIFF, cur_x, cur_z, cliff);
|
||||
}
|
||||
}
|
||||
|
||||
for (int z = 0; z < CHUNK_D; z++){
|
||||
int cur_z = z + cz * CHUNK_D;
|
||||
for (int x = 0; x < CHUNK_W; x++){
|
||||
int cur_x = x + cx * CHUNK_W;
|
||||
float height = heights.get(MAPS::HEIGHT, cur_x, cur_z);
|
||||
|
||||
for (int cur_y = 0; cur_y < CHUNK_H; cur_y++){
|
||||
// int cur_y = y;
|
||||
int id = cur_y < SEA_LEVEL ? idWater : BLOCK_AIR;
|
||||
int states = 0;
|
||||
if ((cur_y == (int)height) && (SEA_LEVEL-2 < cur_y)) {
|
||||
id = idGrassBlock;
|
||||
} else if (cur_y < (height - 6)){
|
||||
id = idStone;
|
||||
} else if (cur_y < height){
|
||||
id = idDirt;
|
||||
} else {
|
||||
int tree = generate_tree(
|
||||
&noise, &randomtree, heights,
|
||||
cur_x, cur_y, cur_z,
|
||||
treesTile, idWood, idLeaves);
|
||||
if (tree) {
|
||||
id = tree;
|
||||
states = BLOCK_DIR_UP;
|
||||
}
|
||||
}
|
||||
float sand = fmax(heights.get(MAPS::SAND, cur_x, cur_z), heights.get(MAPS::CLIFF, cur_x, cur_z));
|
||||
if (((height - (1.1 - 0.2 * pow(height - 54, 4)) +
|
||||
(5*sand)) < cur_y + (height - 0.01- (int)height))
|
||||
&& (cur_y < height)){
|
||||
id = idSand;
|
||||
}
|
||||
if (cur_y <= 2)
|
||||
id = idBazalt;
|
||||
|
||||
randomgrass.setSeed(cur_x,cur_z);
|
||||
if ((id == 0) && ((height > SEA_LEVEL+0.4) || (sand > 0.1)) && ((int)(height + 1) == cur_y) && ((unsigned short)randomgrass.rand() > 56000)){
|
||||
id = idGrass;
|
||||
}
|
||||
if ((id == 0) && (height > SEA_LEVEL+0.4) && ((int)(height + 1) == cur_y) && ((unsigned short)randomgrass.rand() > 65000)){
|
||||
id = idFlower;
|
||||
}
|
||||
if ((height > SEA_LEVEL+1) && ((int)(height + 1) == cur_y) && ((unsigned short)randomgrass.rand() > 65533)){
|
||||
id = idWood;
|
||||
states = BLOCK_DIR_UP;
|
||||
}
|
||||
voxels[(cur_y * CHUNK_D + z) * CHUNK_W + x].id = id;
|
||||
voxels[(cur_y * CHUNK_D + z) * CHUNK_W + x].states = states;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
18
src/voxels/DefaultWorldGenerator.h
Normal file
18
src/voxels/DefaultWorldGenerator.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef VOXELS_WORLDGENERATOR_H_
|
||||
#define VOXELS_WORLDGENERATOR_H_
|
||||
|
||||
#include "../typedefs.h"
|
||||
#include "../voxels/WorldGenerator.h"
|
||||
#include <string>
|
||||
|
||||
struct voxel;
|
||||
class Content;
|
||||
|
||||
class DefaultWorldGenerator : WorldGenerator {
|
||||
public:
|
||||
DefaultWorldGenerator(const Content* content) : WorldGenerator(content) {}
|
||||
|
||||
void generate(voxel* voxels, int x, int z, int seed) override;
|
||||
};
|
||||
|
||||
#endif /* VOXELS_WORLDGENERATOR_H_ */
|
||||
@ -1,219 +1,15 @@
|
||||
#include "WorldGenerator.h"
|
||||
#include "voxel.h"
|
||||
#include "Chunk.h"
|
||||
#include "Block.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <time.h>
|
||||
#include <stdexcept>
|
||||
#include <math.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/noise.hpp>
|
||||
#define FNL_IMPL
|
||||
#include "../maths/FastNoiseLite.h"
|
||||
|
||||
#include "../content/Content.h"
|
||||
#include "../maths/voxmaths.h"
|
||||
#include "../maths/util.h"
|
||||
#include "../core_defs.h"
|
||||
|
||||
// TODO: do something with long conditions + move magic numbers to constants
|
||||
|
||||
const int SEA_LEVEL = 55;
|
||||
|
||||
enum class MAPS{
|
||||
SAND,
|
||||
TREE,
|
||||
CLIFF,
|
||||
HEIGHT
|
||||
};
|
||||
#define MAPS_LEN 4
|
||||
|
||||
class Map2D {
|
||||
int x, z;
|
||||
int w, d;
|
||||
float* heights[MAPS_LEN];
|
||||
public:
|
||||
Map2D(int x, int z, int w, int d) : x(x), z(z), w(w), d(d) {
|
||||
for (int i = 0; i < MAPS_LEN; i++)
|
||||
heights[i] = new float[w*d];
|
||||
}
|
||||
~Map2D() {
|
||||
for (int i = 0; i < MAPS_LEN; i++)
|
||||
delete[] heights[i];
|
||||
}
|
||||
|
||||
inline float get(MAPS map, int x, int z) {
|
||||
x -= this->x;
|
||||
z -= this->z;
|
||||
if (x < 0 || z < 0 || x >= w || z >= d) {
|
||||
throw std::runtime_error("out of heightmap");
|
||||
}
|
||||
return heights[(int)map][z * w + x];
|
||||
}
|
||||
|
||||
inline void set(MAPS map, int x, int z, float value) {
|
||||
x -= this->x;
|
||||
z -= this->z;
|
||||
if (x < 0 || z < 0 || x >= w || z >= d) {
|
||||
throw std::runtime_error("out of heightmap");
|
||||
}
|
||||
heights[(int)map][z * w + x] = value;
|
||||
}
|
||||
};
|
||||
|
||||
float calc_height(fnl_state *noise, int cur_x, int cur_z){
|
||||
float height = 0;
|
||||
|
||||
height += fnlGetNoise2D(noise, cur_x*0.0125f*8-125567,cur_z*0.0125f*8+3546);
|
||||
height += fnlGetNoise2D(noise, cur_x*0.025f*8+4647,cur_z*0.025f*8-3436)*0.5f;
|
||||
height += fnlGetNoise2D(noise, cur_x*0.05f*8-834176,cur_z*0.05f*8+23678)*0.25f;
|
||||
height += fnlGetNoise2D(noise,
|
||||
cur_x*0.2f*8 + fnlGetNoise2D(noise, cur_x*0.1f*8-23557,cur_z*0.1f*8-6568)*50,
|
||||
cur_z*0.2f*8 + fnlGetNoise2D(noise, cur_x*0.1f*8+4363,cur_z*0.1f*8+4456)*50
|
||||
) * fnlGetNoise2D(noise, cur_x*0.01f-834176,cur_z*0.01f+23678) * 0.25;
|
||||
height += fnlGetNoise2D(noise, cur_x*0.1f*8-3465,cur_z*0.1f*8+4534)*0.125f;
|
||||
height *= fnlGetNoise2D(noise, cur_x*0.1f+1000,cur_z*0.1f+1000)*0.5f+0.5f;
|
||||
height += 1.0f;
|
||||
height *= 64.0f;
|
||||
return height;
|
||||
}
|
||||
|
||||
WorldGenerator::WorldGenerator(const Content* content)
|
||||
: idStone(content->requireBlock("base:stone").rt.id),
|
||||
idDirt(content->requireBlock("base:dirt").rt.id),
|
||||
idGrassBlock(content->requireBlock("base:grass_block").rt.id),
|
||||
idSand(content->requireBlock("base:sand").rt.id),
|
||||
idWater(content->requireBlock("base:water").rt.id),
|
||||
idWood(content->requireBlock("base:wood").rt.id),
|
||||
idLeaves(content->requireBlock("base:leaves").rt.id),
|
||||
idGrass(content->requireBlock("base:grass").rt.id),
|
||||
idFlower(content->requireBlock("base:flower").rt.id),
|
||||
idBazalt(content->requireBlock("base:bazalt").rt.id) {}
|
||||
|
||||
int generate_tree(fnl_state *noise,
|
||||
PseudoRandom* random,
|
||||
Map2D& heights,
|
||||
// Map2D& humidity,
|
||||
int cur_x,
|
||||
int cur_y,
|
||||
int cur_z,
|
||||
int tileSize,
|
||||
blockid_t idWood,
|
||||
blockid_t idLeaves){
|
||||
const int tileX = floordiv(cur_x, tileSize);
|
||||
const int tileZ = floordiv(cur_z, tileSize);
|
||||
|
||||
random->setSeed(tileX*4325261+tileZ*12160951+tileSize*9431111);
|
||||
|
||||
int randomX = (random->rand() % (tileSize/2)) - tileSize/4;
|
||||
int randomZ = (random->rand() % (tileSize/2)) - tileSize/4;
|
||||
|
||||
int centerX = tileX * tileSize + tileSize/2 + randomX;
|
||||
int centerZ = tileZ * tileSize + tileSize/2 + randomZ;
|
||||
|
||||
bool gentree = (random->rand() % 10) < heights.get(MAPS::TREE, centerX, centerZ) * 13;
|
||||
if (!gentree)
|
||||
return 0;
|
||||
|
||||
int height = (int)(heights.get(MAPS::HEIGHT, centerX, centerZ));
|
||||
if (height < SEA_LEVEL+1)
|
||||
return 0;
|
||||
int lx = cur_x - centerX;
|
||||
int radius = random->rand() % 4 + 2;
|
||||
int ly = cur_y - height - 3 * radius;
|
||||
int lz = cur_z - centerZ;
|
||||
if (lx == 0 && lz == 0 && cur_y - height < (3*radius + radius/2))
|
||||
return idWood;
|
||||
if (lx*lx+ly*ly/2+lz*lz < radius*radius)
|
||||
return idLeaves;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void WorldGenerator::generate(voxel* voxels, int cx, int cz, int seed){
|
||||
const int treesTile = 12;
|
||||
fnl_state noise = fnlCreateState();
|
||||
noise.noise_type = FNL_NOISE_OPENSIMPLEX2;
|
||||
noise.seed = seed * 60617077 % 25896307;
|
||||
PseudoRandom randomtree;
|
||||
PseudoRandom randomgrass;
|
||||
|
||||
int padding = 8;
|
||||
Map2D heights(cx * CHUNK_W - padding,
|
||||
cz * CHUNK_D - padding,
|
||||
CHUNK_W + padding * 2,
|
||||
CHUNK_D + padding * 2);
|
||||
|
||||
for (int z = -padding; z < CHUNK_D+padding; z++){
|
||||
for (int x = -padding; x < CHUNK_W+padding; x++){
|
||||
int cur_x = x + cx * CHUNK_W;
|
||||
int cur_z = z + cz * CHUNK_D;
|
||||
float height = calc_height(&noise, cur_x, cur_z);
|
||||
float hum = fnlGetNoise2D(&noise, cur_x * 0.3 + 633, cur_z * 0.3);
|
||||
float sand = fnlGetNoise2D(&noise, cur_x * 0.1 - 633, cur_z * 0.1 + 1000);
|
||||
float cliff = pow((sand + abs(sand)) / 2, 2);
|
||||
float w = pow(fmax(-abs(height-SEA_LEVEL)+4,0)/6,2) * cliff;
|
||||
float h1 = -abs(height-SEA_LEVEL - 0.03);
|
||||
float h2 = abs(height-SEA_LEVEL + 0.04);
|
||||
float h = (h1 + h2)*100;
|
||||
height += (h * w);
|
||||
heights.set(MAPS::HEIGHT, cur_x, cur_z, height);
|
||||
heights.set(MAPS::TREE, cur_x, cur_z, hum);
|
||||
heights.set(MAPS::SAND, cur_x, cur_z, sand);
|
||||
heights.set(MAPS::CLIFF, cur_x, cur_z, cliff);
|
||||
}
|
||||
}
|
||||
|
||||
for (int z = 0; z < CHUNK_D; z++){
|
||||
int cur_z = z + cz * CHUNK_D;
|
||||
for (int x = 0; x < CHUNK_W; x++){
|
||||
int cur_x = x + cx * CHUNK_W;
|
||||
float height = heights.get(MAPS::HEIGHT, cur_x, cur_z);
|
||||
|
||||
for (int cur_y = 0; cur_y < CHUNK_H; cur_y++){
|
||||
// int cur_y = y;
|
||||
int id = cur_y < SEA_LEVEL ? idWater : BLOCK_AIR;
|
||||
int states = 0;
|
||||
if ((cur_y == (int)height) && (SEA_LEVEL-2 < cur_y)) {
|
||||
id = idGrassBlock;
|
||||
} else if (cur_y < (height - 6)){
|
||||
id = idStone;
|
||||
} else if (cur_y < height){
|
||||
id = idDirt;
|
||||
} else {
|
||||
int tree = generate_tree(
|
||||
&noise, &randomtree, heights,
|
||||
cur_x, cur_y, cur_z,
|
||||
treesTile, idWood, idLeaves);
|
||||
if (tree) {
|
||||
id = tree;
|
||||
states = BLOCK_DIR_UP;
|
||||
}
|
||||
}
|
||||
float sand = fmax(heights.get(MAPS::SAND, cur_x, cur_z), heights.get(MAPS::CLIFF, cur_x, cur_z));
|
||||
if (((height - (1.1 - 0.2 * pow(height - 54, 4)) +
|
||||
(5*sand)) < cur_y + (height - 0.01- (int)height))
|
||||
&& (cur_y < height)){
|
||||
id = idSand;
|
||||
}
|
||||
if (cur_y <= 2)
|
||||
id = idBazalt;
|
||||
|
||||
randomgrass.setSeed(cur_x,cur_z);
|
||||
if ((id == 0) && ((height > SEA_LEVEL+0.4) || (sand > 0.1)) && ((int)(height + 1) == cur_y) && ((unsigned short)randomgrass.rand() > 56000)){
|
||||
id = idGrass;
|
||||
}
|
||||
if ((id == 0) && (height > SEA_LEVEL+0.4) && ((int)(height + 1) == cur_y) && ((unsigned short)randomgrass.rand() > 65000)){
|
||||
id = idFlower;
|
||||
}
|
||||
if ((height > SEA_LEVEL+1) && ((int)(height + 1) == cur_y) && ((unsigned short)randomgrass.rand() > 65533)){
|
||||
id = idWood;
|
||||
states = BLOCK_DIR_UP;
|
||||
}
|
||||
voxels[(cur_y * CHUNK_D + z) * CHUNK_W + x].id = id;
|
||||
voxels[(cur_y * CHUNK_D + z) * CHUNK_W + x].states = states;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#include "WorldGenerator.h"
|
||||
|
||||
#include "../content/Content.h"
|
||||
|
||||
WorldGenerator::WorldGenerator(const Content* content)
|
||||
: idStone(content->requireBlock("base:stone").rt.id),
|
||||
idDirt(content->requireBlock("base:dirt").rt.id),
|
||||
idGrassBlock(content->requireBlock("base:grass_block").rt.id),
|
||||
idSand(content->requireBlock("base:sand").rt.id),
|
||||
idWater(content->requireBlock("base:water").rt.id),
|
||||
idWood(content->requireBlock("base:wood").rt.id),
|
||||
idLeaves(content->requireBlock("base:leaves").rt.id),
|
||||
idGrass(content->requireBlock("base:grass").rt.id),
|
||||
idFlower(content->requireBlock("base:flower").rt.id),
|
||||
idBazalt(content->requireBlock("base:bazalt").rt.id) {}
|
||||
@ -2,11 +2,13 @@
|
||||
#define VOXELS_WORLDGENERATOR_H_
|
||||
|
||||
#include "../typedefs.h"
|
||||
#include <string>
|
||||
|
||||
struct voxel;
|
||||
class Content;
|
||||
|
||||
class WorldGenerator {
|
||||
protected:
|
||||
blockid_t const idStone;
|
||||
blockid_t const idDirt;
|
||||
blockid_t const idGrassBlock;
|
||||
@ -19,7 +21,8 @@ class WorldGenerator {
|
||||
blockid_t const idBazalt;
|
||||
public:
|
||||
WorldGenerator(const Content* content);
|
||||
void generate(voxel* voxels, int x, int z, int seed);
|
||||
|
||||
virtual void generate(voxel* voxels, int x, int z, int seed);
|
||||
};
|
||||
|
||||
#endif /* VOXELS_WORLDGENERATOR_H_ */
|
||||
#endif /* VOXELS_WORLDGENERATOR_H_ */
|
||||
@ -6,7 +6,9 @@
|
||||
|
||||
#include "Level.h"
|
||||
#include "../files/WorldFiles.h"
|
||||
#include "../world/WorldTypes.h"
|
||||
#include "../content/Content.h"
|
||||
#include "../world/WorldGenerators.h"
|
||||
#include "../content/ContentLUT.h"
|
||||
#include "../voxels/Chunk.h"
|
||||
#include "../voxels/Chunks.h"
|
||||
@ -21,12 +23,14 @@ world_load_error::world_load_error(std::string message)
|
||||
|
||||
World::World(
|
||||
std::string name,
|
||||
std::string generator,
|
||||
fs::path directory,
|
||||
uint64_t seed,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack> packs)
|
||||
: name(name),
|
||||
generator(generator),
|
||||
seed(seed),
|
||||
settings(settings),
|
||||
content(content),
|
||||
@ -65,12 +69,13 @@ void World::write(Level* level) {
|
||||
}
|
||||
|
||||
Level* World::create(std::string name,
|
||||
std::string generator,
|
||||
fs::path directory,
|
||||
uint64_t seed,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack>& packs) {
|
||||
auto world = new World(name, directory, seed, settings, content, packs);
|
||||
auto world = new World(name, generator, directory, seed, settings, content, packs);
|
||||
auto level = new Level(world, content, settings);
|
||||
auto inventory = level->player->getInventory();
|
||||
inventory->setId(world->getNextInventoryId());
|
||||
@ -83,7 +88,7 @@ Level* World::load(fs::path directory,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack>& packs) {
|
||||
auto world = std::make_unique<World>(
|
||||
".", directory, 0, settings, content, packs
|
||||
".", WorldGenerators::getDefaultGeneratorID(), directory, 0, settings, content, packs
|
||||
);
|
||||
auto& wfile = world->wfile;
|
||||
|
||||
@ -111,6 +116,10 @@ void World::setName(const std::string& name) {
|
||||
this->name = name;
|
||||
}
|
||||
|
||||
void world::setGenerator(const std::string& generator) {
|
||||
this->generator = generator;
|
||||
}
|
||||
|
||||
bool World::hasPack(const std::string& id) const {
|
||||
for (auto& pack : packs) {
|
||||
if (pack.id == id)
|
||||
@ -131,47 +140,57 @@ uint64_t World::getSeed() const {
|
||||
return seed;
|
||||
}
|
||||
|
||||
std::string World::getGenerator() const {
|
||||
return generator;
|
||||
}
|
||||
|
||||
const std::vector<ContentPack>& World::getPacks() const {
|
||||
return packs;
|
||||
}
|
||||
|
||||
void World::deserialize(dynamic::Map* root) {
|
||||
name = root->getStr("name", name);
|
||||
generator = root->getStr("generator", generator);
|
||||
seed = root->getInt("seed", seed);
|
||||
|
||||
auto verobj = root->map("version");
|
||||
if (verobj) {
|
||||
int major=0, minor=-1;
|
||||
verobj->num("major", major);
|
||||
verobj->num("minor", minor);
|
||||
std::cout << "world version: " << major << "." << minor << std::endl;
|
||||
}
|
||||
if(generator == "") {
|
||||
generator = WorldGenerators::getDefaultGeneratorID();
|
||||
}
|
||||
|
||||
auto timeobj = root->map("time");
|
||||
if (timeobj) {
|
||||
timeobj->num("day-time", daytime);
|
||||
timeobj->num("day-time-speed", daytimeSpeed);
|
||||
auto verobj = root->map("version");
|
||||
if (verobj) {
|
||||
int major=0, minor=-1;
|
||||
verobj->num("major", major);
|
||||
verobj->num("minor", minor);
|
||||
std::cout << "world version: " << major << "." << minor << std::endl;
|
||||
}
|
||||
|
||||
auto timeobj = root->map("time");
|
||||
if (timeobj) {
|
||||
timeobj->num("day-time", daytime);
|
||||
timeobj->num("day-time-speed", daytimeSpeed);
|
||||
timeobj->num("total-time", totalTime);
|
||||
}
|
||||
}
|
||||
|
||||
nextInventoryId = root->getNum("next-inventory-id", 2);
|
||||
}
|
||||
|
||||
std::unique_ptr<dynamic::Map> World::serialize() const {
|
||||
auto root = std::make_unique<dynamic::Map>();
|
||||
auto root = std::make_unique<dynamic::Map>();
|
||||
|
||||
auto& versionobj = root->putMap("version");
|
||||
versionobj.put("major", ENGINE_VERSION_MAJOR);
|
||||
versionobj.put("minor", ENGINE_VERSION_MINOR);
|
||||
auto& versionobj = root->putMap("version");
|
||||
versionobj.put("major", ENGINE_VERSION_MAJOR);
|
||||
versionobj.put("minor", ENGINE_VERSION_MINOR);
|
||||
|
||||
root->put("name", name);
|
||||
root->put("seed", seed);
|
||||
|
||||
root->put("name", name);
|
||||
root->put("generator", generator);
|
||||
root->put("seed", seed);
|
||||
|
||||
auto& timeobj = root->putMap("time");
|
||||
timeobj.put("day-time", daytime);
|
||||
timeobj.put("day-time-speed", daytimeSpeed);
|
||||
timeobj.put("day-time", daytime);
|
||||
timeobj.put("day-time-speed", daytimeSpeed);
|
||||
timeobj.put("total-time", totalTime);
|
||||
|
||||
root->put("next-inventory-id", nextInventoryId);
|
||||
return root;
|
||||
}
|
||||
}
|
||||
@ -26,8 +26,9 @@ public:
|
||||
world_load_error(std::string message);
|
||||
};
|
||||
|
||||
class World : public Serializable {
|
||||
class World : Serializable {
|
||||
std::string name;
|
||||
std::string generator;
|
||||
uint64_t seed;
|
||||
EngineSettings& settings;
|
||||
const Content* const content;
|
||||
@ -47,11 +48,13 @@ public:
|
||||
double totalTime = 0.0;
|
||||
|
||||
World(std::string name,
|
||||
std::string generator,
|
||||
fs::path directory,
|
||||
uint64_t seed,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
std::vector<ContentPack> packs);
|
||||
|
||||
~World();
|
||||
|
||||
/**
|
||||
@ -77,6 +80,7 @@ public:
|
||||
* Create new world
|
||||
* @param name internal world name
|
||||
* @param directory root world directory
|
||||
* @param type of the world
|
||||
* @param seed world generation seed
|
||||
* @param settings current engine settings
|
||||
* @param content current engine Content instance
|
||||
@ -85,6 +89,7 @@ public:
|
||||
* @return Level instance containing World instance
|
||||
*/
|
||||
static Level* create(std::string name,
|
||||
std::string generator,
|
||||
fs::path directory,
|
||||
uint64_t seed,
|
||||
EngineSettings& settings,
|
||||
@ -108,7 +113,8 @@ public:
|
||||
|
||||
void setName(const std::string& name);
|
||||
void setSeed(uint64_t seed);
|
||||
|
||||
void setGenerator(const std::string& generator);
|
||||
|
||||
/**
|
||||
* Check if world has content-pack installed
|
||||
* @param id content-pack id
|
||||
@ -124,6 +130,8 @@ public:
|
||||
/** Get world generation seed */
|
||||
uint64_t getSeed() const;
|
||||
|
||||
/** Get world generator id */
|
||||
std::string getGenerator() const;
|
||||
/**
|
||||
* Get vector of all content-packs installed in world
|
||||
*/
|
||||
@ -148,4 +156,4 @@ public:
|
||||
void deserialize(dynamic::Map *src) override;
|
||||
};
|
||||
|
||||
#endif /* WORLD_WORLD_H_ */
|
||||
#endif /* WORLD_WORLD_H_ */
|
||||
41
src/world/WorldGenerators.cpp
Normal file
41
src/world/WorldGenerators.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
#include "WorldGenerators.h"
|
||||
#include "../voxels/WorldGenerator.h"
|
||||
#include "../voxels/FlatWorldGenerator.h"
|
||||
#include "../content/Content.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <function>
|
||||
|
||||
std::map<std::string, std::function> generators;
|
||||
|
||||
template <typename T>
|
||||
void WorldGenerators::addGenerator(std::string id) {
|
||||
using create_func = std::function<T*>(const Content*);
|
||||
generators[id] = create_func
|
||||
}
|
||||
|
||||
std::vector<std::string> WorldGenerators::getGeneratorsIDs() {
|
||||
std::vector<std::string> ids;
|
||||
|
||||
for(std::map<std::string, std::function>::iterator it = generators.begin(); it != generators.end(); ++it) {
|
||||
ids.push_back(it->first);
|
||||
}
|
||||
|
||||
return ids;
|
||||
}
|
||||
|
||||
std::string WorldGenerators::getDefaultWorldGeneratorID() {
|
||||
return "core:default";
|
||||
}
|
||||
|
||||
WorldGenerator* WorldGenerators::createWorldGenerator(std::string id, const Content* content) {
|
||||
for(std::map<std::string, std::function>::iterator it = generators.begin(); it != generators.end(); ++it) {
|
||||
if(id == it->first) {
|
||||
return (WorldGenerator*) it->second(content);
|
||||
}
|
||||
}
|
||||
|
||||
std::cerr << "unknown generator id: " << id << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
21
src/world/WorldGenerators.h
Normal file
21
src/world/WorldGenerators.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef WORLD_WORLDTYPES_H_
|
||||
#define WORLD_WORLDTYPES_H_
|
||||
|
||||
#include "../voxels/WorldGenerator.h"
|
||||
#include "../content/Content.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
class WorldGenerators {
|
||||
|
||||
public:
|
||||
static void addGenerator(std::string id);
|
||||
|
||||
static std::vector<std::string> getGeneratorsIDs();
|
||||
|
||||
static std::string getDefaultGeneratorID();
|
||||
|
||||
static WorldGenerator* createGenerator(std::string id, const Content* content);
|
||||
};
|
||||
|
||||
#endif /* WORLD_WORLDTYPES_H_ */
|
||||
Loading…
x
Reference in New Issue
Block a user