Worlds indexing

This commit is contained in:
MihailRis
2023-12-08 21:00:15 +03:00
parent eea920d98f
commit f513c094a9
19 changed files with 383 additions and 141 deletions
+84 -13
View File
@@ -13,11 +13,14 @@
#include "screens.h"
#include "../util/stringutil.h"
#include "../files/engine_paths.h"
#include "../files/WorldConverter.h"
#include "../world/World.h"
#include "../window/Events.h"
#include "../window/Window.h"
#include "../engine.h"
#include "../settings.h"
#include "../content/Content.h"
#include "../content/ContentLUT.h"
#include "gui/gui_util.h"
@@ -25,12 +28,69 @@ using glm::vec2;
using glm::vec4;
using std::string;
using std::wstring;
using std::make_unique;
using std::unique_ptr;
using std::shared_ptr;
using std::filesystem::path;
using std::filesystem::u8path;
using std::filesystem::directory_iterator;
using namespace gui;
void show_content_missing(GUI* gui, const Content* content, ContentLUT* lut) {
PagesControl* menu = gui->getMenu();
Panel* panel = new Panel(vec2(500, 200), vec4(8.0f), 8.0f);
panel->color(vec4(0.0f, 0.0f, 0.0f, 0.5f));
panel->add(new Label(L"Content missing!"));
Panel* subpanel = new Panel(vec2(500, 100));
subpanel->color(vec4(0.0f, 0.0f, 0.0f, 0.5f));
for (size_t i = 0; i < lut->countBlocks(); i++) {
// missing block
if (lut->getBlockId(i) == BLOCK_VOID) {
auto name = lut->getBlockName(i);
Panel* hpanel = new Panel(vec2(500, 30));
hpanel->color(vec4(0.0f));
hpanel->orientation(Orientation::horizontal);
Label* namelabel = new Label(util::str2wstr_utf8(name));
namelabel->color(vec4(1.0f, 0.2f, 0.2f, 0.5f));
Label* typelabel = new Label(L"[block]");
typelabel->color(vec4(0.5f));
hpanel->add(typelabel);
hpanel->add(namelabel);
subpanel->add(hpanel);
}
}
subpanel->maxLength(400);
panel->add(subpanel);
panel->add((new Button(L"Back to Main Menu", vec4(8.0f)))->listenAction([=](GUI*){
menu->back();
}));
panel->refresh();
menu->add("missing-content", panel);
menu->set("missing-content");
}
void show_convert_request(GUI* gui, const Content* content, ContentLUT* lut,
path folder) {
guiutil::confirm(gui, L"Content indices have changed! Convert "
+util::str2wstr_utf8(folder.string())+L"?",
[=]() {
std::cout << "Convert the world: " << folder.string() << std::endl;
// TODO: add multithreading here
auto converter = make_unique<WorldConverter>(folder, content, lut);
while (converter->hasNext()) {
converter->convertNext();
}
converter->write();
delete lut;
}, L"Yes", L"Cancel");
}
Panel* create_main_menu_panel(Engine* engine, PagesControl* menu) {
EnginePaths* paths = engine->getPaths();
@@ -40,7 +100,7 @@ Panel* create_main_menu_panel(Engine* engine, PagesControl* menu) {
panel->add(guiutil::gotoButton(L"New World", "new-world", menu));
Panel* worldsPanel = new Panel(vec2(390, 200), vec4(5.0f));
worldsPanel->color(vec4(0.1f));
worldsPanel->color(vec4(1.0f, 1.0f, 1.0f, 0.07f));
worldsPanel->maxLength(400);
path worldsFolder = paths->getWorldsFolder();
if (std::filesystem::is_directory(worldsFolder)) {
@@ -51,14 +111,25 @@ Panel* create_main_menu_panel(Engine* engine, PagesControl* menu) {
string name = entry.path().filename().string();
Button* button = new Button(util::str2wstr_utf8(name),
vec4(10.0f, 8.0f, 10.0f, 8.0f));
button->color(vec4(0.5f));
button->listenAction([=](GUI*) {
EngineSettings& settings = engine->getSettings();
button->color(vec4(1.0f, 1.0f, 1.0f, 0.1f));
button->listenAction([=](GUI* gui) {
auto* content = engine->getContent();
auto& settings = engine->getSettings();
auto folder = paths->getWorldsFolder()/u8path(name);
Level* level = World::load(folder, settings, engine->getContent());
auto screen = new LevelScreen(engine, level);
engine->setScreen(shared_ptr<Screen>(screen));
std::filesystem::create_directories(folder);
ContentLUT* lut = World::checkIndices(folder, content);
if (lut) {
if (lut->hasMissingContent()) {
show_content_missing(gui, content, lut);
delete lut;
} else {
show_convert_request(gui, content, lut, folder);
}
} else {
Level* level = World::load(folder, settings, content);
auto screen = new LevelScreen(engine, level);
engine->setScreen(shared_ptr<Screen>(screen));
}
});
worldsPanel->add(button);
}
@@ -140,12 +211,13 @@ Panel* create_new_world_panel(Engine* engine, PagesControl* menu) {
seed = hash(seedstr);
}
std::cout << "world seed: " << seed << std::endl;
EngineSettings& settings = engine->getSettings();
auto folder = paths->getWorldsFolder()/u8path(nameutf8);
std::filesystem::create_directories(folder);
Level* level = World::create(nameutf8, folder, seed, settings, engine->getContent());
Level* level = World::create(nameutf8,
folder,
seed,
engine->getSettings(),
engine->getContent());
auto screen = new LevelScreen(engine, level);
engine->setScreen(shared_ptr<Screen>(screen));
});
@@ -207,7 +279,6 @@ Panel* create_settings_panel(Engine* engine, PagesControl* menu) {
Panel* panel = new Panel(vec2(400, 200), vec4(5.0f), 1.0f);
panel->color(vec4(0.0f));
// TODO: simplify repeating code for trackbars
/* Load Distance setting track bar */{
panel->add((new Label(L""))->textSupplier([=]() {
return L"Load Distance: " +