1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Add core.spawn_tree_on_vmanip (#15415)

This function works like `core.spawn_tree`, but spawns an L-system tree onto a VoxelManip object instead on the map.
This commit is contained in:
cx384 2024-12-08 20:27:22 +01:00 committed by GitHub
parent 8d43ad2522
commit c7fe2ee5c9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 110 additions and 10 deletions

View file

@ -875,4 +875,15 @@ void make_pine_tree(MMVManip &vmanip, v3s16 p0, const NodeDefManager *ndef,
}
}
std::string error_to_string(error e)
{
switch (e) {
case SUCCESS:
return "success";
case UNBALANCED_BRACKETS:
return "closing ']' has no matching opening bracket";
}
return "unknown error";
}
}; // namespace treegen

View file

@ -68,4 +68,7 @@ namespace treegen {
treegen::error make_ltree(MMVManip &vmanip, v3s16 p0, const TreeDef &def);
// Helper to spawn it directly on map
treegen::error spawn_ltree(ServerMap *map, v3s16 p0, const TreeDef &def);
// Helper to get a string from the error message
std::string error_to_string(error e);
}; // namespace treegen

View file

@ -1307,11 +1307,7 @@ int ModApiEnv::l_spawn_tree(lua_State *L)
ServerMap *map = &env->getServerMap();
treegen::error e;
if ((e = treegen::spawn_ltree (map, p0, tree_def)) != treegen::SUCCESS) {
if (e == treegen::UNBALANCED_BRACKETS) {
luaL_error(L, "spawn_tree(): closing ']' has no matching opening bracket");
} else {
luaL_error(L, "spawn_tree(): unknown error");
}
throw LuaError("spawn_tree(): " + treegen::error_to_string(e));
}
lua_pushboolean(L, true);
@ -1614,11 +1610,7 @@ int ModApiEnvVM::l_spawn_tree(lua_State *L)
treegen::error e;
if ((e = treegen::make_ltree(*vm, p0, tree_def)) != treegen::SUCCESS) {
if (e == treegen::UNBALANCED_BRACKETS) {
throw LuaError("spawn_tree(): closing ']' has no matching opening bracket");
} else {
throw LuaError("spawn_tree(): unknown error");
}
throw LuaError("spawn_tree(): " + treegen::error_to_string(e));
}
lua_pushboolean(L, true);

View file

@ -1773,6 +1773,27 @@ int ModApiMapgen::l_place_schematic_on_vmanip(lua_State *L)
return 1;
}
// spawn_tree_on_vmanip(vmanip, pos, treedef)
int ModApiMapgen::l_spawn_tree_on_vmanip(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
MMVManip *vm = checkObject<LuaVoxelManip>(L, 1)->vm;
v3s16 p0 = read_v3s16(L, 2);
treegen::TreeDef tree_def;
const NodeDefManager *ndef = getGameDef(L)->ndef();
if (!read_tree_def(L, 3, ndef, tree_def))
return 0;
treegen::error e = treegen::make_ltree(*vm, p0, tree_def);
if (e != treegen::SUCCESS) {
throw LuaError("spawn_tree_on_vmanip(): " + treegen::error_to_string(e));
}
lua_pushboolean(L, true);
return 1;
}
// serialize_schematic(schematic, format, options={...})
int ModApiMapgen::l_serialize_schematic(lua_State *L)
@ -2023,6 +2044,7 @@ void ModApiMapgen::Initialize(lua_State *L, int top)
API_FCT(create_schematic);
API_FCT(place_schematic);
API_FCT(place_schematic_on_vmanip);
API_FCT(spawn_tree_on_vmanip);
API_FCT(serialize_schematic);
API_FCT(read_schematic);
}
@ -2049,6 +2071,7 @@ void ModApiMapgen::InitializeEmerge(lua_State *L, int top)
API_FCT(generate_ores);
API_FCT(generate_decorations);
API_FCT(place_schematic_on_vmanip);
API_FCT(spawn_tree_on_vmanip);
API_FCT(serialize_schematic);
API_FCT(read_schematic);
}

View file

@ -131,6 +131,9 @@ private:
// replacements, force_placement, flagstring)
static int l_place_schematic_on_vmanip(lua_State *L);
// spawn_tree_on_vmanip(vmanip, pos, treedef)
static int l_spawn_tree_on_vmanip(lua_State *L);
// serialize_schematic(schematic, format, options={...})
static int l_serialize_schematic(lua_State *L);