Merge branch 'main' into heightmaps

This commit is contained in:
MihailRis
2024-10-03 19:12:37 +03:00
64 changed files with 3472 additions and 732 deletions
+52 -19
View File
@@ -4,10 +4,10 @@
#include <filesystem>
#include <memory>
#include "coders/commons.hpp"
#include "content/ContentLUT.hpp"
#include "debug/Logger.hpp"
#include "engine.hpp"
#include "coders/commons.hpp"
#include "debug/Logger.hpp"
#include "content/ContentReport.hpp"
#include "files/WorldConverter.hpp"
#include "files/WorldFiles.hpp"
#include "frontend/locale.hpp"
@@ -46,19 +46,28 @@ std::shared_ptr<Task> create_converter(
Engine* engine,
const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content,
const std::shared_ptr<ContentLUT>& lut,
const std::shared_ptr<ContentReport>& report,
const runnable& postRunnable
) {
ConvertMode mode;
if (report->isUpgradeRequired()) {
mode = ConvertMode::UPGRADE;
} else if (report->hasContentReorder()) {
mode = ConvertMode::REINDEX;
} else {
mode = ConvertMode::BLOCK_FIELDS;
}
return WorldConverter::startTask(
worldFiles,
content,
lut,
report,
[=]() {
auto menu = engine->getGUI()->getMenu();
menu->reset();
menu->setPage("main", false);
engine->getGUI()->postRunnable([=]() { postRunnable(); });
},
mode,
true
);
}
@@ -66,31 +75,55 @@ std::shared_ptr<Task> create_converter(
void show_convert_request(
Engine* engine,
const Content* content,
const std::shared_ptr<ContentLUT>& lut,
const std::shared_ptr<ContentReport>& report,
const std::shared_ptr<WorldFiles>& worldFiles,
const runnable& postRunnable
) {
guiutil::confirm(
engine->getGUI(),
langs::get(L"world.convert-request"),
[=]() {
auto on_confirm = [=]() {
auto converter =
create_converter(engine, worldFiles, content, lut, postRunnable);
create_converter(engine, worldFiles, content, report, postRunnable);
menus::show_process_panel(
engine, converter, L"Converting world..."
);
},
};
std::wstring message = L"world.convert-block-layouts";
if (report->hasContentReorder()) {
message = L"world.convert-request";
}
if (report->isUpgradeRequired()) {
message = L"world.upgrade-request";
} else if (report->hasDataLoss()) {
message = L"world.convert-with-loss";
std::wstring text;
for (const auto& line : report->getDataLoss()) {
text += util::str2wstr_utf8(line) + L"\n";
}
guiutil::confirmWithMemo(
engine->getGUI(),
langs::get(message),
text,
on_confirm,
L"",
langs::get(L"Cancel")
);
return;
}
guiutil::confirm(
engine->getGUI(),
langs::get(message),
on_confirm,
L"",
langs::get(L"Cancel")
);
}
static void show_content_missing(
Engine* engine, const std::shared_ptr<ContentLUT>& lut
Engine* engine, const std::shared_ptr<ContentReport>& report
) {
auto root = dv::object();
auto& contentEntries = root.list("content");
for (auto& entry : lut->getMissingContent()) {
for (auto& entry : report->getMissingContent()) {
std::string contentName = ContentType_name(entry.type);
auto& contentEntry = contentEntries.object();
contentEntry["type"] = contentName;
@@ -134,10 +167,10 @@ void EngineController::openWorld(const std::string& name, bool confirmConvert) {
auto* content = engine->getContent();
auto worldFiles = std::make_shared<WorldFiles>(
folder, engine->getSettings().debug);
if (auto lut = World::checkIndices(worldFiles, content)) {
if (lut->hasMissingContent()) {
if (auto report = World::checkIndices(worldFiles, content)) {
if (report->hasMissingContent()) {
engine->setScreen(std::make_shared<MenuScreen>(engine));
show_content_missing(engine, lut);
show_content_missing(engine, report);
} else {
if (confirmConvert) {
menus::show_process_panel(
@@ -146,13 +179,13 @@ void EngineController::openWorld(const std::string& name, bool confirmConvert) {
engine,
worldFiles,
content,
lut,
report,
[=]() { openWorld(name, false); }
),
L"Converting world..."
);
} else {
show_convert_request(engine, content, lut, std::move(worldFiles), [=]() {
show_convert_request(engine, content, report, std::move(worldFiles), [=]() {
openWorld(name, false);
});
}
+132
View File
@@ -7,6 +7,8 @@
#include "voxels/Chunks.hpp"
#include "voxels/voxel.hpp"
#include "world/Level.hpp"
#include "maths/voxmaths.hpp"
#include "data/StructLayout.hpp"
#include "api_lua.hpp"
using namespace scripting;
@@ -436,6 +438,134 @@ static int l_decompose_state(lua::State* L) {
return 1;
}
static int get_field(
lua::State* L,
const ubyte* src,
const data::Field& field,
size_t index,
const data::StructLayout& dataStruct
) {
switch (field.type) {
case data::FieldType::I8:
case data::FieldType::I16:
case data::FieldType::I32:
case data::FieldType::I64:
return lua::pushinteger(L, dataStruct.getInteger(src, field, index));
case data::FieldType::F32:
case data::FieldType::F64:
return lua::pushnumber(L, dataStruct.getNumber(src, field, index));
case data::FieldType::CHAR:
return lua::pushstring(L,
std::string(dataStruct.getChars(src, field)).c_str());
}
return 0;
}
static int l_get_field(lua::State* L) {
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
auto name = lua::require_string(L, 4);
size_t index = 0;
if (lua::gettop(L) >= 5) {
index = lua::tointeger(L, 5);
}
auto vox = level->chunks->get(x, y, z);
auto cx = floordiv(x, CHUNK_W);
auto cz = floordiv(z, CHUNK_D);
auto chunk = level->chunks->getChunk(cx, cz);
auto lx = x - cx * CHUNK_W;
auto lz = z - cz * CHUNK_W;
size_t voxelIndex = vox_index(lx, y, lz);
const auto& def = content->getIndices()->blocks.require(vox->id);
if (def.dataStruct == nullptr) {
return 0;
}
const auto& dataStruct = *def.dataStruct;
const auto field = dataStruct.getField(name);
if (field == nullptr) {
return 0;
}
if (index >= field->elements) {
throw std::out_of_range(
"index out of bounds [0, "+std::to_string(field->elements)+"]");
}
const ubyte* src = chunk->blocksMetadata.find(voxelIndex);
if (src == nullptr) {
return 0;
}
return get_field(L, src, *field, index, dataStruct);
}
static int set_field(
lua::State* L,
ubyte* dst,
const data::Field& field,
size_t index,
const data::StructLayout& dataStruct,
const dv::value& value
) {
switch (field.type) {
case data::FieldType::CHAR:
if (value.isString()) {
return lua::pushinteger(L,
dataStruct.setUnicode(dst, value.asString(), field));
}
case data::FieldType::I8:
case data::FieldType::I16:
case data::FieldType::I32:
case data::FieldType::I64:
dataStruct.setInteger(dst, value.asInteger(), field, index);
break;
case data::FieldType::F32:
case data::FieldType::F64:
dataStruct.setNumber(dst, value.asNumber(), field, index);
break;
}
return 0;
}
static int l_set_field(lua::State* L) {
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
auto name = lua::require_string(L, 4);
auto value = lua::tovalue(L, 5);
size_t index = 0;
if (lua::gettop(L) >= 6) {
index = lua::tointeger(L, 6);
}
auto vox = level->chunks->get(x, y, z);
auto cx = floordiv(x, CHUNK_W);
auto cz = floordiv(z, CHUNK_D);
auto chunk = level->chunks->getChunk(cx, cz);
auto lx = x - cx * CHUNK_W;
auto lz = z - cz * CHUNK_W;
size_t voxelIndex = vox_index(lx, y, lz);
const auto& def = content->getIndices()->blocks.require(vox->id);
if (def.dataStruct == nullptr) {
return 0;
}
const auto& dataStruct = *def.dataStruct;
const auto field = dataStruct.getField(name);
if (field == nullptr) {
return 0;
}
if (index >= field->elements) {
throw std::out_of_range(
"index out of bounds [0, "+std::to_string(field->elements)+"]");
}
ubyte* dst = chunk->blocksMetadata.find(voxelIndex);
if (dst == nullptr) {
dst = chunk->blocksMetadata.allocate(voxelIndex, dataStruct.size());
}
chunk->flags.unsaved = true;
chunk->flags.blocksData = true;
return set_field(L, dst, *field, index, dataStruct, value);
}
const luaL_Reg blocklib[] = {
{"index", lua::wrap<l_index>},
{"name", lua::wrap<l_get_def>},
@@ -469,5 +599,7 @@ const luaL_Reg blocklib[] = {
{"raycast", lua::wrap<l_raycast>},
{"compose_state", lua::wrap<l_compose_state>},
{"decompose_state", lua::wrap<l_decompose_state>},
{"get_field", lua::wrap<l_get_field>},
{"set_field", lua::wrap<l_set_field>},
{NULL, NULL}
};
+12 -1
View File
@@ -51,12 +51,23 @@ int lua::pushvalue(State* L, const dv::value& value) {
}
break;
}
case value_type::object: {
case value_type::object:
createtable(L, 0, value.size());
for (const auto& [key, elem] : value.asObject()) {
pushvalue(L, elem);
setfield(L, key);
}
break;
case value_type::bytes: {
const auto& bytes = value.asBytes();
createtable(L, 0, bytes.size());
size_t size = bytes.size();
for (size_t i = 0; i < size;) {
pushinteger(L, bytes[i]);
i++;
rawseti(L, i);
}
break;
}
}
return 1;