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());
+10 -10
View File
@@ -345,7 +345,7 @@ bool scripting::on_item_break_block(
);
}
dynamic::Value scripting::get_component_value(
dv::value scripting::get_component_value(
const scriptenv& env, const std::string& name
) {
auto L = lua::get_main_thread();
@@ -353,15 +353,15 @@ dynamic::Value scripting::get_component_value(
if (lua::getfield(L, name)) {
return lua::tovalue(L, -1);
}
return dynamic::NONE;
return nullptr;
}
void scripting::on_entity_spawn(
const EntityDef&,
entityid_t eid,
const std::vector<std::unique_ptr<UserComponent>>& components,
dynamic::Map_sptr args,
dynamic::Map_sptr saved
const dv::value& args,
const dv::value& saved
) {
auto L = lua::get_main_thread();
lua::requireglobal(L, STDCOMP);
@@ -384,8 +384,8 @@ void scripting::on_entity_spawn(
if (args != nullptr) {
std::string compfieldname = component->name;
util::replaceAll(compfieldname, ":", "__");
if (auto datamap = args->map(compfieldname)) {
lua::pushvalue(L, datamap);
if (args.has(compfieldname)) {
lua::pushvalue(L, args[compfieldname]);
} else {
lua::createtable(L, 0, 0);
}
@@ -397,8 +397,8 @@ void scripting::on_entity_spawn(
if (saved == nullptr) {
lua::createtable(L, 0, 0);
} else {
if (auto datamap = saved->map(component->name)) {
lua::pushvalue(L, datamap);
if (saved.has(component->name)) {
lua::pushvalue(L, saved[component->name]);
} else {
lua::createtable(L, 0, 0);
}
@@ -579,10 +579,10 @@ void scripting::on_entities_render(float delta) {
}
void scripting::on_ui_open(
UiDocument* layout, std::vector<dynamic::Value> args
UiDocument* layout, std::vector<dv::value> args
) {
auto argsptr =
std::make_shared<std::vector<dynamic::Value>>(std::move(args));
std::make_shared<std::vector<dv::value>>(std::move(args));
std::string name = layout->getId() + ".open";
lua::emit_event(lua::get_main_thread(), name, [=](auto L) {
for (const auto& value : *argsptr) {
+5 -5
View File
@@ -6,7 +6,7 @@
#include <string>
#include <vector>
#include "data/dynamic.hpp"
#include "data/dv.hpp"
#include "delegates.hpp"
#include "typedefs.hpp"
#include "scripting_functional.hpp"
@@ -89,15 +89,15 @@ namespace scripting {
Player* player, const ItemDef& item, int x, int y, int z
);
dynamic::Value get_component_value(
dv::value get_component_value(
const scriptenv& env, const std::string& name
);
void on_entity_spawn(
const EntityDef& def,
entityid_t eid,
const std::vector<std::unique_ptr<UserComponent>>& components,
dynamic::Map_sptr args,
dynamic::Map_sptr saved
const dv::value& args,
const dv::value& saved
);
void on_entity_despawn(const Entity& entity);
void on_entity_grounded(const Entity& entity, float force);
@@ -113,7 +113,7 @@ namespace scripting {
void on_entity_used(const Entity& entity, Player* player);
/// @brief Called on UI view show
void on_ui_open(UiDocument* layout, std::vector<dynamic::Value> args);
void on_ui_open(UiDocument* layout, std::vector<dv::value> args);
void on_ui_progress(UiDocument* layout, int workDone, int totalWork);
+2 -2
View File
@@ -160,7 +160,7 @@ vec2supplier scripting::create_vec2_supplier(
};
}
dynamic::to_string_func scripting::create_tostring(
value_to_string_func scripting::create_tostring(
const scriptenv& env, const std::string& src, const std::string& file
) {
auto L = lua::get_main_thread();
@@ -168,7 +168,7 @@ dynamic::to_string_func scripting::create_tostring(
lua::loadbuffer(L, *env, src, file);
lua::call(L, 0, 1);
auto func = lua::create_lambda(L);
return [func](const dynamic::Value& value) {
return [func](const dv::value& value) {
auto result = func({value});
return json::stringify(result, true, " ");
};
+4 -4
View File
@@ -3,13 +3,13 @@
#include <glm/glm.hpp>
#include <string>
#include "data/dynamic.hpp"
#include "data/dv.hpp"
#include "delegates.hpp"
#include "typedefs.hpp"
namespace scripting {
using common_func =
std::function<dynamic::Value(const std::vector<dynamic::Value>&)>;
using common_func = std::function<dv::value(const std::vector<dv::value>&)>;
using value_to_string_func = std::function<std::string(const dv::value&)>;
runnable create_runnable(
const scriptenv& env,
@@ -71,7 +71,7 @@ namespace scripting {
const std::string& file = "<string>"
);
dynamic::to_string_func create_tostring(
value_to_string_func create_tostring(
const scriptenv& env,
const std::string& src,
const std::string& file = "<string>"
@@ -10,7 +10,7 @@
#include "content/Content.hpp"
#include "voxels/Block.hpp"
#include "voxels/Chunk.hpp"
#include "data/dynamic.hpp"
#include "data/dv.hpp"
#include "world/generator/GeneratorDef.hpp"
class LuaGeneratorScript : public GeneratorScript {
@@ -105,11 +105,12 @@ public:
};
static BlocksLayer load_layer(
const dynamic::Map_sptr& map, uint& lastLayersHeight, bool& hasResizeableLayer
const dv::value& map, uint& lastLayersHeight, bool& hasResizeableLayer
) {
auto name = map->get<std::string>("block");
int height = map->get<integer_t>("height");
bool belowSeaLevel = map->get("below_sea_level", true);
const auto& name = map["block"].asString();
int height = map["height"].asInteger();
bool belowSeaLevel = true;
map.at("below_sea_level").get(belowSeaLevel);
if (hasResizeableLayer) {
lastLayersHeight += height;
@@ -124,14 +125,14 @@ static BlocksLayer load_layer(
}
static inline BlocksLayers load_layers(
const dynamic::List_sptr& layersArr, const std::string& fieldname
const dv::value& layersArr, const std::string& fieldname
) {
uint lastLayersHeight = 0;
bool hasResizeableLayer = false;
std::vector<BlocksLayer> layers;
for (int i = 0; i < layersArr->size(); i++) {
const auto& layerMap = layersArr->map(i);
for (int i = 0; i < layersArr.size(); i++) {
const auto& layerMap = layersArr[i];
try {
layers.push_back(
load_layer(layerMap, lastLayersHeight, hasResizeableLayer));
@@ -144,18 +145,18 @@ static inline BlocksLayers load_layers(
}
static inline BiomePlants load_plants(
const dynamic::Map_sptr& biomeMap
const dv::value& biomeMap
) {
float plantChance = biomeMap->get("plant_chance", 0.0);
float plantChance = 0.0f;
biomeMap.at("plant_chance").get(plantChance);
float plantsWeightSum = 0.0f;
std::vector<PlantEntry> plants;
if (biomeMap->has("plants")) {
auto plantsArr = biomeMap->list("plants");
for (size_t i = 0; i < plantsArr->size(); i++) {
auto entry = plantsArr->map(i);
auto block = entry->get<std::string>("block");
float weight = entry->get<number_t>("weight");
if (biomeMap.has("plants")) {
const auto& plantsArr = biomeMap["plants"];
for (const auto& entry : plantsArr) {
const auto& block = entry["block"].asString();
float weight = entry["weight"].asNumber();
if (weight <= 0.0f) {
throw std::runtime_error("weight must be positive");
}
@@ -169,28 +170,28 @@ static inline BiomePlants load_plants(
}
static inline Biome load_biome(
const dynamic::Map_sptr& biomeMap,
const dv::value& biomeMap,
const std::string& name,
uint parametersCount,
int idx
) {
std::vector<BiomeParameter> parameters;
const auto& paramsArr = biomeMap->list("parameters");
if (paramsArr->size() < parametersCount) {
const auto& paramsArr = biomeMap["parameters"];
if (paramsArr.size() < parametersCount) {
throw std::runtime_error(
std::to_string(parametersCount)+" parameters expected");
}
for (size_t i = 0; i < parametersCount; i++) {
const auto& paramMap = paramsArr->map(i);
float value = paramMap->get<number_t>("value");
float weight = paramMap->get<number_t>("weight");
const auto& paramMap = paramsArr[i];
float value = paramMap["value"].asNumber();
float weight = paramMap["weight"].asNumber();
parameters.push_back(BiomeParameter {value, weight});
}
BiomePlants plants = load_plants(biomeMap);
BlocksLayers groundLayers = load_layers(biomeMap->list("layers"), "layers");
BlocksLayers seaLayers = load_layers(biomeMap->list("sea_layers"), "sea_layers");
BlocksLayers groundLayers = load_layers(biomeMap["layers"], "layers");
BlocksLayers seaLayers = load_layers(biomeMap["sea_layers"], "sea_layers");
return Biome {
name,
std::move(parameters),
@@ -209,19 +210,16 @@ std::unique_ptr<GeneratorScript> scripting::load_generator(
lua::pop(L, load_script(*env, "generator", file));
lua::pushenv(L, *env);
auto val = lua::tovalue(L, -1);
auto root = lua::tovalue(L, -1);
lua::pop(L);
auto root = std::get<dynamic::Map_sptr>(val);
uint biomeParameters = root->get<integer_t>("biome_parameters");
uint seaLevel = root->get<integer_t>("sea_level");
uint biomeParameters = root["biome_parameters"].asInteger();
uint seaLevel = root["sea_level"].asInteger();
std::vector<Biome> biomes;
const auto& biomesMap = root->map("biomes");
for (const auto& [biomeName, value] : biomesMap->values) {
const auto& biomeMap = std::get<dynamic::Map_sptr>(value);
const auto& biomesMap = root["biomes"];
for (const auto& [biomeName, biomeMap] : biomesMap.asObject()) {
try {
biomes.push_back(
load_biome(biomeMap, biomeName, biomeParameters, -2));