mirror of
https://github.com/minetest/minetest.git
synced 2025-01-03 03:47:31 +01:00
Add minetest.bulk_swap_node (#15043)
Co-authored-by: sfan5 <sfan5@live.de>
This commit is contained in:
parent
d8f1daac25
commit
f65fe80e81
@ -23,6 +23,8 @@ core.add_node = core.set_node
|
|||||||
-- we don't deal with metadata currently
|
-- we don't deal with metadata currently
|
||||||
core.swap_node = core.set_node
|
core.swap_node = core.set_node
|
||||||
|
|
||||||
|
core.bulk_swap_node = core.bulk_set_node
|
||||||
|
|
||||||
function core.remove_node(pos)
|
function core.remove_node(pos)
|
||||||
return core.vmanip:set_node_at(pos, {name="air"})
|
return core.vmanip:set_node_at(pos, {name="air"})
|
||||||
end
|
end
|
||||||
|
@ -6145,6 +6145,8 @@ Environment access
|
|||||||
* `minetest.swap_node(pos, node)`
|
* `minetest.swap_node(pos, node)`
|
||||||
* Swap node at position with another.
|
* Swap node at position with another.
|
||||||
* This keeps the metadata intact and will not run con-/destructor callbacks.
|
* This keeps the metadata intact and will not run con-/destructor callbacks.
|
||||||
|
* `minetest.bulk_swap_node({pos1, pos2, pos3, ...}, node)`
|
||||||
|
* Equivalent to `minetest.swap_node` but in bulk.
|
||||||
* `minetest.remove_node(pos)`: Remove a node
|
* `minetest.remove_node(pos)`: Remove a node
|
||||||
* Equivalent to `minetest.set_node(pos, {name="air"})`, but a bit faster.
|
* Equivalent to `minetest.set_node(pos, {name="air"})`, but a bit faster.
|
||||||
* `minetest.get_node(pos)`
|
* `minetest.get_node(pos)`
|
||||||
|
@ -154,3 +154,36 @@ minetest.register_chatcommand("bench_bulk_get_node", {
|
|||||||
return true, msg
|
return true, msg
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
minetest.register_chatcommand("bench_bulk_swap_node", {
|
||||||
|
params = "",
|
||||||
|
description = "Benchmark: Bulk-swap 99×99×99 stone nodes",
|
||||||
|
func = function(name, param)
|
||||||
|
local player = minetest.get_player_by_name(name)
|
||||||
|
if not player then
|
||||||
|
return false, "No player."
|
||||||
|
end
|
||||||
|
local pos_list = get_positions_cube(player:get_pos())
|
||||||
|
|
||||||
|
minetest.chat_send_player(name, "Benchmarking minetest.bulk_swap_node. Warming up ...")
|
||||||
|
|
||||||
|
-- warm up because first execution otherwise becomes
|
||||||
|
-- significantly slower
|
||||||
|
minetest.bulk_swap_node(pos_list, {name = "mapgen_stone"})
|
||||||
|
|
||||||
|
minetest.chat_send_player(name, "Warming up finished, now benchmarking ...")
|
||||||
|
|
||||||
|
local start_time = minetest.get_us_time()
|
||||||
|
for i=1,#pos_list do
|
||||||
|
minetest.swap_node(pos_list[i], {name = "mapgen_stone"})
|
||||||
|
end
|
||||||
|
local middle_time = minetest.get_us_time()
|
||||||
|
minetest.bulk_swap_node(pos_list, {name = "mapgen_stone"})
|
||||||
|
local end_time = minetest.get_us_time()
|
||||||
|
local msg = string.format("Benchmark results: minetest.swap_node loop: %.2f ms; minetest.bulk_swap_node: %.2f ms",
|
||||||
|
((middle_time - start_time)) / 1000,
|
||||||
|
((end_time - middle_time)) / 1000
|
||||||
|
)
|
||||||
|
return true, msg
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
@ -253,6 +253,31 @@ int ModApiEnv::l_swap_node(lua_State *L)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// bulk_swap_node([pos1, pos2, ...], node)
|
||||||
|
// pos = {x=num, y=num, z=num}
|
||||||
|
int ModApiEnv::l_bulk_swap_node(lua_State *L)
|
||||||
|
{
|
||||||
|
GET_ENV_PTR;
|
||||||
|
|
||||||
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
|
|
||||||
|
s32 len = lua_objlen(L, 1);
|
||||||
|
|
||||||
|
MapNode n = readnode(L, 2);
|
||||||
|
|
||||||
|
// Do it
|
||||||
|
bool succeeded = true;
|
||||||
|
for (s32 i = 1; i <= len; i++) {
|
||||||
|
lua_rawgeti(L, 1, i);
|
||||||
|
if (!env->swapNode(read_v3s16(L, -1), n))
|
||||||
|
succeeded = false;
|
||||||
|
lua_pop(L, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_pushboolean(L, succeeded);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
// get_node_raw(x, y, z) -> content, param1, param2, pos_ok
|
// get_node_raw(x, y, z) -> content, param1, param2, pos_ok
|
||||||
int ModApiEnv::l_get_node_raw(lua_State *L)
|
int ModApiEnv::l_get_node_raw(lua_State *L)
|
||||||
{
|
{
|
||||||
@ -1377,6 +1402,7 @@ void ModApiEnv::Initialize(lua_State *L, int top)
|
|||||||
API_FCT(bulk_set_node);
|
API_FCT(bulk_set_node);
|
||||||
API_FCT(add_node);
|
API_FCT(add_node);
|
||||||
API_FCT(swap_node);
|
API_FCT(swap_node);
|
||||||
|
API_FCT(bulk_swap_node);
|
||||||
API_FCT(add_item);
|
API_FCT(add_item);
|
||||||
API_FCT(remove_node);
|
API_FCT(remove_node);
|
||||||
API_FCT(get_node_raw);
|
API_FCT(get_node_raw);
|
||||||
|
@ -65,6 +65,10 @@ private:
|
|||||||
// pos = {x=num, y=num, z=num}
|
// pos = {x=num, y=num, z=num}
|
||||||
static int l_bulk_set_node(lua_State *L);
|
static int l_bulk_set_node(lua_State *L);
|
||||||
|
|
||||||
|
// bulk_swap_node([pos1, pos2, ...], node)
|
||||||
|
// pos = {x=num, y=num, z=num}
|
||||||
|
static int l_bulk_swap_node(lua_State *L);
|
||||||
|
|
||||||
static int l_add_node(lua_State *L);
|
static int l_add_node(lua_State *L);
|
||||||
|
|
||||||
// remove_node(pos)
|
// remove_node(pos)
|
||||||
|
Loading…
Reference in New Issue
Block a user