Merge branch 'main' into heightmaps

This commit is contained in:
MihailRis
2024-09-19 15:16:14 +03:00
74 changed files with 1885 additions and 1782 deletions
+3 -6
View File
@@ -50,14 +50,11 @@ static int l_spawn(lua::State* L) {
auto defname = lua::tostring(L, 1);
auto& def = content->entities.require(defname);
auto pos = lua::tovec3(L, 2);
dynamic::Map_sptr args = nullptr;
dv::value args = nullptr;
if (lua::gettop(L) > 2) {
auto value = lua::tovalue(L, 3);
if (auto map = std::get_if<dynamic::Map_sptr>(&value)) {
args = *map;
}
args = lua::tovalue(L, 3);
}
level->entities->spawn(def, pos, args);
level->entities->spawn(def, pos, std::move(args));
return 1;
}
+1 -1
View File
@@ -20,7 +20,7 @@ static int l_save_structure(lua::State* L) {
auto structure = VoxelStructure::create(level, pointA, pointB, saveEntities);
auto map = structure->serialize();
auto bytes = json::to_binary(map.get());
auto bytes = json::to_binary(map);
files::write_bytes(fs::u8path(filename), bytes.data(), bytes.size());
return 0;
}
+3 -8
View File
@@ -1,17 +1,12 @@
#include "coders/json.hpp"
#include "data/dynamic.hpp"
#include "api_lua.hpp"
static int l_json_stringify(lua::State* L) {
auto value = lua::tovalue(L, 1);
if (auto mapptr = std::get_if<dynamic::Map_sptr>(&value)) {
bool nice = lua::toboolean(L, 2);
auto string = json::stringify(mapptr->get(), nice, " ");
return lua::pushstring(L, string);
} else {
throw std::runtime_error("table expected");
}
bool nice = lua::toboolean(L, 2);
auto string = json::stringify(value, nice, " ");
return lua::pushstring(L, string);
}
static int l_json_parse(lua::State* L) {
+3 -5
View File
@@ -1,5 +1,4 @@
#include "coders/toml.hpp"
#include "data/dynamic.hpp"
#include "api_lua.hpp"
using namespace scripting;
@@ -7,8 +6,8 @@ using namespace scripting;
static int l_toml_stringify(lua::State* L) {
auto value = lua::tovalue(L, 1);
if (auto mapptr = std::get_if<dynamic::Map_sptr>(&value)) {
auto string = toml::stringify(**mapptr);
if (value.isObject()) {
auto string = toml::stringify(value);
return lua::pushstring(L, string);
} else {
throw std::runtime_error("table expected");
@@ -18,8 +17,7 @@ static int l_toml_stringify(lua::State* L) {
static int l_toml_parse(lua::State* L) {
auto string = lua::require_string(L, 1);
auto element = toml::parse("<string>", string);
auto value = std::make_unique<dynamic::Value>(element);
return lua::pushvalue(L, *value);
return lua::pushvalue(L, element);
}
const luaL_Reg tomllib[] = {
-1
View File
@@ -3,7 +3,6 @@
#include <stdexcept>
#include <string>
#include "data/dynamic.hpp"
#include "delegates.hpp"
#include "logic/scripting/scripting_functional.hpp"
#include "lua_util.hpp"
+42 -33
View File
@@ -22,33 +22,42 @@ std::string lua::env_name(int env) {
return "_ENV" + util::mangleid(env);
}
int lua::pushvalue(State* L, const dynamic::Value& value) {
using namespace dynamic;
int lua::pushvalue(State* L, const dv::value& value) {
using dv::value_type;
if (auto* flag = std::get_if<bool>(&value)) {
pushboolean(L, *flag);
} else if (auto* num = std::get_if<integer_t>(&value)) {
pushinteger(L, *num);
} else if (auto* num = std::get_if<number_t>(&value)) {
pushnumber(L, *num);
} else if (auto* str = std::get_if<std::string>(&value)) {
pushstring(L, *str);
} else if (auto listptr = std::get_if<List_sptr>(&value)) {
auto list = *listptr;
createtable(L, list->size(), 0);
for (size_t i = 0; i < list->size(); i++) {
pushvalue(L, list->get(i));
rawseti(L, i + 1);
switch (value.getType()) {
case value_type::none:
pushnil(L);
break;
case value_type::boolean:
pushboolean(L, value.asBoolean());
break;
case value_type::number:
pushnumber(L, value.asNumber());
break;
case value_type::integer:
pushinteger(L, value.asInteger());
break;
case value_type::string:
pushstring(L, value.asString());
break;
case value_type::list: {
createtable(L, value.size(), 0);
size_t index = 1;
for (const auto& elem : value) {
pushvalue(L, elem);
rawseti(L, index);
index++;
}
break;
}
} else if (auto mapptr = std::get_if<Map_sptr>(&value)) {
auto map = *mapptr;
createtable(L, 0, map->size());
for (auto& entry : map->values) {
pushvalue(L, entry.second);
setfield(L, entry.first);
case value_type::object: {
createtable(L, 0, value.size());
for (const auto& [key, elem] : value.asObject()) {
pushvalue(L, elem);
setfield(L, key);
}
}
} else {
pushnil(L);
}
return 1;
}
@@ -61,13 +70,13 @@ int lua::pushwstring(State* L, const std::wstring& str) {
return pushstring(L, util::wstr2str_utf8(str));
}
dynamic::Value lua::tovalue(State* L, int idx) {
using namespace dynamic;
dv::value lua::tovalue(State* L, int idx) {
using dv::value_type;
auto type = lua::type(L, idx);
switch (type) {
case LUA_TNIL:
case LUA_TNONE:
return dynamic::NONE;
return nullptr;
case LUA_TBOOLEAN:
return toboolean(L, idx) == 1;
case LUA_TNUMBER: {
@@ -91,22 +100,22 @@ dynamic::Value lua::tovalue(State* L, int idx) {
int len = objlen(L, idx);
if (len) {
// array
auto list = create_list();
auto list = dv::list();
for (int i = 1; i <= len; i++) {
rawgeti(L, i, idx);
list->put(tovalue(L, -1));
list.add(tovalue(L, -1));
pop(L);
}
return list;
} else {
// table
auto map = create_map();
auto map = dv::object();
pushvalue(L, idx);
pushnil(L);
while (next(L, -2)) {
pushvalue(L, -2);
auto key = tostring(L, -1);
map->put(key, tovalue(L, -2));
map[key] = tovalue(L, -2);
pop(L, 2);
}
pop(L);
@@ -219,7 +228,7 @@ runnable lua::create_runnable(State* L) {
scripting::common_func lua::create_lambda(State* L) {
auto funcptr = create_lambda_handler(L);
return [=](const std::vector<dynamic::Value>& args) {
return [=](const std::vector<dv::value>& args) -> dv::value {
getglobal(L, LAMBDAS_TABLE);
getfield(L, *funcptr);
for (const auto& arg : args) {
@@ -230,7 +239,7 @@ scripting::common_func lua::create_lambda(State* L) {
pop(L);
return result;
}
return dynamic::Value(dynamic::NONE);
return nullptr;
};
}
+3 -2
View File
@@ -4,6 +4,7 @@
#include <typeinfo>
#include <unordered_map>
#include "data/dv.hpp"
#include "lua_wrapper.hpp"
#include "lua_custom_types.hpp"
#define GLM_ENABLE_EXPERIMENTAL
@@ -401,10 +402,10 @@ namespace lua {
return glm::vec4(r / 255, g / 255, b / 255, a / 255);
}
int pushvalue(lua::State*, const dynamic::Value& value);
int pushvalue(lua::State*, const dv::value& value);
[[nodiscard]]
dynamic::Value tovalue(lua::State*, int idx);
dv::value tovalue(lua::State*, int idx);
inline bool getfield(lua::State* L, const std::string& name, int idx = -1) {
lua_getfield(L, idx, name.c_str());