More world generators support

This commit is contained in:
Onran
2024-02-21 11:54:10 +09:00
committed by GitHub
parent 399bd9c362
commit 9cc2f93cfd
9 changed files with 209 additions and 30 deletions
+35 -2
View File
@@ -19,6 +19,7 @@
#include "../files/WorldConverter.h" #include "../files/WorldConverter.h"
#include "../files/WorldFiles.h" #include "../files/WorldFiles.h"
#include "../world/World.h" #include "../world/World.h"
#include "../world/WorldTypes.h"
#include "../world/Level.h" #include "../world/Level.h"
#include "../window/Events.h" #include "../window/Events.h"
#include "../window/Window.h" #include "../window/Window.h"
@@ -38,6 +39,10 @@ using glm::vec4;
namespace fs = std::filesystem; namespace fs = std::filesystem;
using namespace gui; using namespace gui;
namespace menus {
std::string worldType;
}
inline uint64_t randU64() { inline uint64_t randU64() {
srand(time(NULL)); srand(time(NULL));
return rand() ^ (rand() << 8) ^ return rand() ^ (rand() << 8) ^
@@ -168,6 +173,28 @@ void create_languages_panel(Engine* engine) {
panel->add(guiutil::backButton(menu)); panel->add(guiutil::backButton(menu));
} }
void create_world_types_panel(Engine* engine) {
auto menu = engine->getGUI()->getMenu();
auto panel = create_page(engine, "world_types", 400, 0.5f, 1);
panel->setScrollable(true);
std::vector<std::string> worldTypes = WorldTypes::getWorldTypes();
std::sort(worldTypes.begin(), worldTypes.end());
for (std::string& type : worldTypes) {
std::string& fullName = util::wstr2str_utf8(langs::get(util::str2wstr_utf8(type), L"world.types"));
auto button = std::make_shared<Button>(
util::str2wstr_utf8(fullName),
vec4(10.f),
[=](GUI*) {
menus::worldType = type;
menu->back();
}
);
panel->add(button);
}
panel->add(guiutil::backButton(menu));
}
void open_world(std::string name, Engine* engine) { void open_world(std::string name, Engine* engine) {
auto paths = engine->getPaths(); auto paths = engine->getPaths();
auto folder = paths->getWorldsFolder()/fs::u8path(name); auto folder = paths->getWorldsFolder()/fs::u8path(name);
@@ -414,6 +441,8 @@ void create_new_world_panel(Engine* engine) {
auto seedInput = std::make_shared<TextBox>(seedstr, vec4(6.0f)); auto seedInput = std::make_shared<TextBox>(seedstr, vec4(6.0f));
panel->add(seedInput); panel->add(seedInput);
panel->add(guiutil::gotoButton(langs::get(L"World type", L"world"), "world_types", engine->getGUI()->getMenu()));
panel->add(create_button( L"Create World", vec4(10), vec4(1, 20, 1, 1), panel->add(create_button( L"Create World", vec4(10), vec4(1, 20, 1, 1),
[=](GUI*) { [=](GUI*) {
if (!nameInput->validate()) if (!nameInput->validate())
@@ -450,11 +479,12 @@ void create_new_world_panel(Engine* engine) {
} }
Level* level = World::create( Level* level = World::create(
name, folder, seed, name, menus::worldType, folder, seed,
engine->getSettings(), engine->getSettings(),
engine->getContent(), engine->getContent(),
engine->getContentPacks() engine->getContentPacks()
); );
menus::worldType = WorldTypes::getDefaultWorldType();
engine->setScreen(std::make_shared<LevelScreen>(engine, level)); engine->setScreen(std::make_shared<LevelScreen>(engine, level));
})); }));
panel->add(guiutil::backButton(engine->getGUI()->getMenu())); panel->add(guiutil::backButton(engine->getGUI()->getMenu()));
@@ -641,15 +671,18 @@ void create_pause_panel(Engine* engine) {
} }
void menus::create_menus(Engine* engine) { void menus::create_menus(Engine* engine) {
menus::worldType = WorldTypes::getDefaultWorldType();
create_new_world_panel(engine); create_new_world_panel(engine);
create_settings_panel(engine); create_settings_panel(engine);
create_controls_panel(engine); create_controls_panel(engine);
create_pause_panel(engine); create_pause_panel(engine);
create_languages_panel(engine); create_languages_panel(engine);
create_world_types_panel(engine);
create_main_menu_panel(engine); create_main_menu_panel(engine);
} }
void menus::refresh_menus(Engine* engine) { void menus::refresh_menus(Engine* engine) {
create_main_menu_panel(engine); create_main_menu_panel(engine);
create_new_world_panel(engine); create_new_world_panel(engine);
} create_world_types_panel(engine);
}
+3 -2
View File
@@ -10,6 +10,7 @@
#include "../voxels/Chunks.h" #include "../voxels/Chunks.h"
#include "../voxels/ChunksStorage.h" #include "../voxels/ChunksStorage.h"
#include "../voxels/WorldGenerator.h" #include "../voxels/WorldGenerator.h"
#include "../world/WorldTypes.h"
#include "../graphics/Mesh.h" #include "../graphics/Mesh.h"
#include "../lighting/Lighting.h" #include "../lighting/Lighting.h"
#include "../files/WorldFiles.h" #include "../files/WorldFiles.h"
@@ -26,7 +27,7 @@ ChunksController::ChunksController(Level* level, uint padding)
chunks(level->chunks), chunks(level->chunks),
lighting(level->lighting), lighting(level->lighting),
padding(padding), padding(padding),
generator(new WorldGenerator(level->content)) { generator(WorldTypes::createWorldGenerator(level->getWorld()->getType(), level->content)) {
} }
ChunksController::~ChunksController(){ ChunksController::~ChunksController(){
@@ -130,4 +131,4 @@ void ChunksController::createChunk(int x, int z) {
} }
chunk->setLoaded(true); chunk->setLoaded(true);
chunk->setReady(true); chunk->setReady(true);
} }
+28
View File
@@ -0,0 +1,28 @@
#include "FlatWorldGenerator.h"
#include "voxel.h"
#include "Chunk.h"
#include "../content/Content.h"
#include "../core_defs.h"
void FlatWorldGenerator::generate(voxel* voxels, int cx, int cz, int seed) {
for (int z = 0; z < CHUNK_D; z++) {
for (int x = 0; x < CHUNK_W; x++) {
for (int cur_y = 0; cur_y < CHUNK_H; cur_y++){
int id = BLOCK_AIR;
int states = 0;
if(cur_y == 2) {
id = idBazalt;
} else if(cur_y == 6) {
id = idGrassBlock;
} else if(cur_y > 2 && cur_y <= 5) {
id = idDirt;
}
voxels[(cur_y * CHUNK_D + z) * CHUNK_W + x].id = id;
voxels[(cur_y * CHUNK_D + z) * CHUNK_W + x].states = states;
}
}
}
}
+18
View File
@@ -0,0 +1,18 @@
#ifndef VOXELS_FLATWORLDGENERATOR_H_
#define VOXELS_FLATWORLDGENERATOR_H_
#include "../typedefs.h"
#include "../voxels/WorldGenerator.h"
struct voxel;
class Content;
class FlatWorldGenerator : WorldGenerator {
public:
FlatWorldGenerator(const Content* content) : WorldGenerator(content) {}
void generate(voxel* voxels, int x, int z, int seed) override;
};
#endif /* VOXELS_FLATWORLDGENERATOR_H_ */
+3 -2
View File
@@ -7,6 +7,7 @@ struct voxel;
class Content; class Content;
class WorldGenerator { class WorldGenerator {
protected:
blockid_t const idStone; blockid_t const idStone;
blockid_t const idDirt; blockid_t const idDirt;
blockid_t const idGrassBlock; blockid_t const idGrassBlock;
@@ -19,7 +20,7 @@ class WorldGenerator {
blockid_t const idBazalt; blockid_t const idBazalt;
public: public:
WorldGenerator(const Content* content); 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_ */
+52 -23
View File
@@ -6,6 +6,7 @@
#include "Level.h" #include "Level.h"
#include "../files/WorldFiles.h" #include "../files/WorldFiles.h"
#include "../world/WorldTypes.h"
#include "../content/Content.h" #include "../content/Content.h"
#include "../content/ContentLUT.h" #include "../content/ContentLUT.h"
#include "../voxels/Chunk.h" #include "../voxels/Chunk.h"
@@ -34,6 +35,23 @@ World::World(
wfile = new WorldFiles(directory, settings.debug); wfile = new WorldFiles(directory, settings.debug);
} }
World::World(
std::string name,
std::string type,
fs::path directory,
uint64_t seed,
EngineSettings& settings,
const Content* content,
const std::vector<ContentPack> packs)
: name(name),
type(type),
seed(seed),
settings(settings),
content(content),
packs(packs) {
wfile = new WorldFiles(directory, settings.debug);
}
World::~World(){ World::~World(){
delete wfile; delete wfile;
} }
@@ -69,12 +87,13 @@ const float DEF_PLAYER_SPEED = 4.0f;
const int DEF_PLAYER_INVENTORY_SIZE = 40; const int DEF_PLAYER_INVENTORY_SIZE = 40;
Level* World::create(std::string name, Level* World::create(std::string name,
std::string type,
fs::path directory, fs::path directory,
uint64_t seed, uint64_t seed,
EngineSettings& settings, EngineSettings& settings,
const Content* content, const Content* content,
const std::vector<ContentPack>& packs) { const std::vector<ContentPack>& packs) {
auto world = new World(name, directory, seed, settings, content, packs); auto world = new World(name, type, directory, seed, settings, content, packs);
auto inv = std::make_shared<Inventory>(world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE); auto inv = std::make_shared<Inventory>(world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE);
auto player = new Player( auto player = new Player(
glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv
@@ -141,47 +160,57 @@ uint64_t World::getSeed() const {
return seed; return seed;
} }
std::string World::getType() const {
return type;
}
const std::vector<ContentPack>& World::getPacks() const { const std::vector<ContentPack>& World::getPacks() const {
return packs; return packs;
} }
void World::deserialize(dynamic::Map* root) { void World::deserialize(dynamic::Map* root) {
name = root->getStr("name", name); name = root->getStr("name", name);
type = root->getStr("type", type);
seed = root->getInt("seed", seed); seed = root->getInt("seed", seed);
auto verobj = root->map("version"); if(type == "") {
if (verobj) { type = WorldTypes::getDefaultWorldType();
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"); auto verobj = root->map("version");
if (timeobj) { if (verobj) {
timeobj->num("day-time", daytime); int major=0, minor=-1;
timeobj->num("day-time-speed", daytimeSpeed); 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); timeobj->num("total-time", totalTime);
} }
nextInventoryId = root->getNum("next-inventory-id", 2); nextInventoryId = root->getNum("next-inventory-id", 2);
} }
std::unique_ptr<dynamic::Map> World::serialize() const { 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"); auto& versionobj = root->putMap("version");
versionobj.put("major", ENGINE_VERSION_MAJOR); versionobj.put("major", ENGINE_VERSION_MAJOR);
versionobj.put("minor", ENGINE_VERSION_MINOR); versionobj.put("minor", ENGINE_VERSION_MINOR);
root->put("name", name); root->put("name", name);
root->put("seed", seed); root->put("type", type);
root->put("seed", seed);
auto& timeobj = root->putMap("time"); auto& timeobj = root->putMap("time");
timeobj.put("day-time", daytime); timeobj.put("day-time", daytime);
timeobj.put("day-time-speed", daytimeSpeed); timeobj.put("day-time-speed", daytimeSpeed);
timeobj.put("total-time", totalTime); timeobj.put("total-time", totalTime);
root->put("next-inventory-id", nextInventoryId); root->put("next-inventory-id", nextInventoryId);
return root; return root;
} }
+15 -1
View File
@@ -28,6 +28,7 @@ public:
class World : Serializable { class World : Serializable {
std::string name; std::string name;
std::string type;
uint64_t seed; uint64_t seed;
EngineSettings& settings; EngineSettings& settings;
const Content* const content; const Content* const content;
@@ -52,6 +53,15 @@ public:
EngineSettings& settings, EngineSettings& settings,
const Content* content, const Content* content,
std::vector<ContentPack> packs); std::vector<ContentPack> packs);
World(std::string name,
std::string type,
fs::path directory,
uint64_t seed,
EngineSettings& settings,
const Content* content,
std::vector<ContentPack> packs);
~World(); ~World();
/** /**
@@ -77,6 +87,7 @@ public:
* Create new world * Create new world
* @param name internal world name * @param name internal world name
* @param directory root world directory * @param directory root world directory
* @param type of the world
* @param seed world generation seed * @param seed world generation seed
* @param settings current engine settings * @param settings current engine settings
* @param content current engine Content instance * @param content current engine Content instance
@@ -85,6 +96,7 @@ public:
* @return Level instance containing World instance * @return Level instance containing World instance
*/ */
static Level* create(std::string name, static Level* create(std::string name,
std::string type,
fs::path directory, fs::path directory,
uint64_t seed, uint64_t seed,
EngineSettings& settings, EngineSettings& settings,
@@ -124,6 +136,8 @@ public:
/** Get world generation seed */ /** Get world generation seed */
uint64_t getSeed() const; uint64_t getSeed() const;
/** Get world type */
std::string getType() const;
/** /**
* Get vector of all content-packs installed in world * Get vector of all content-packs installed in world
*/ */
@@ -148,4 +162,4 @@ public:
void deserialize(dynamic::Map *src) override; void deserialize(dynamic::Map *src) override;
}; };
#endif /* WORLD_WORLD_H_ */ #endif /* WORLD_WORLD_H_ */
+37
View File
@@ -0,0 +1,37 @@
#include "WorldTypes.h"
#include "../voxels/WorldGenerator.h"
#include "../voxels/FlatWorldGenerator.h"
#include "../content/Content.h"
#include <vector>
#include <string>
#include <iostream>
std::vector<std::string> worldTypes;
void fillTypes() {
worldTypes.push_back("Default");
worldTypes.push_back("Flat");
}
std::vector<std::string> WorldTypes::getWorldTypes() {
if(worldTypes.size() == 0) {
fillTypes();
}
return worldTypes;
}
std::string WorldTypes::getDefaultWorldType() {
return getWorldTypes()[0];
}
WorldGenerator* WorldTypes::createWorldGenerator(std::string worldType, const Content* content) {
if(worldType == "Default") {
return new WorldGenerator(content);
} else if(worldType == "Flat") {
return (WorldGenerator*) new FlatWorldGenerator(content);
}
std::cerr << "unknown world type: " << worldType << std::endl;
return nullptr;
}
+18
View File
@@ -0,0 +1,18 @@
#ifndef WORLD_WORLDTYPES_H_
#define WORLD_WORLDTYPES_H_
#include "../voxels/WorldGenerator.h"
#include "../content/Content.h"
#include <vector>
#include <string>
class WorldTypes {
public:
static std::vector<std::string> getWorldTypes();
static std::string getDefaultWorldType();
static WorldGenerator* createWorldGenerator(std::string worldType, const Content* content);
};
#endif /* WORLD_WORLDTYPES_H_ */