From 25ab3da8db55692cab6a280aa884aa16cd7dd701 Mon Sep 17 00:00:00 2001 From: 1F616EMO Date: Sat, 30 Mar 2024 11:58:55 +0800 Subject: [PATCH 01/10] Allow optional digger in dig_node --- doc/lua_api.md | 3 ++- src/script/lua_api/l_env.cpp | 18 +++++++++++++++++- src/script/lua_api/l_env.h | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/doc/lua_api.md b/doc/lua_api.md index 8a1df92ef43f2..2441d7788ffc3 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -6063,8 +6063,9 @@ Environment access ensures compatibility. * `minetest.place_node(pos, node)` * Place node with the same effects that a player would cause -* `minetest.dig_node(pos)` +* `minetest.dig_node(pos[, digger])` * Dig node with the same effects that a player would cause + * `digger`: The ObjectRef that digs the node (optional) * Returns `true` if successful, `false` on failure (e.g. protected location) * `minetest.punch_node(pos)` * Punch node with the same effects that a player would cause diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index b348a0f47fa01..319e042eb0861 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "lua_api/l_nodetimer.h" #include "lua_api/l_noise.h" #include "lua_api/l_vmanip.h" +#include "lua_api/l_object.h" #include "common/c_converter.h" #include "common/c_content.h" #include "scripting_server.h" @@ -449,7 +450,7 @@ int ModApiEnv::l_place_node(lua_State *L) return 1; } -// dig_node(pos) +// dig_node(pos, [digger]) // pos = {x=num, y=num, z=num} int ModApiEnv::l_dig_node(lua_State *L) { @@ -465,6 +466,21 @@ int ModApiEnv::l_dig_node(lua_State *L) lua_pushboolean(L, false); return 1; } + + if (lua_gettop(L) >= 2 && (! lua_isnoneornil(L, 2))) { + ObjectRef *ref = checkObject(L, 2); + ServerActiveObject *digger = ObjectRef::getobject(ref); + + if (digger != nullptr) { + // Dig the node with the provided digger + bool success = scriptIfaceNode->node_on_dig(pos, n, digger); + lua_pushboolean(L, success); + return 1; + } + // Otherwise, fallback to NULL digger SAO + // (Seems like Lua already handle invalid args but anyway) + } + // Dig it out with a NULL digger (appears in Lua as a // non-functional ObjectRef) bool success = scriptIfaceNode->node_on_dig(pos, n, NULL); diff --git a/src/script/lua_api/l_env.h b/src/script/lua_api/l_env.h index f657424d0045e..263ae46cde0dc 100644 --- a/src/script/lua_api/l_env.h +++ b/src/script/lua_api/l_env.h @@ -93,7 +93,7 @@ class ModApiEnv : public ModApiEnvBase { // pos = {x=num, y=num, z=num} static int l_place_node(lua_State *L); - // dig_node(pos) + // dig_node(pos, [digger]) // pos = {x=num, y=num, z=num} static int l_dig_node(lua_State *L); From 146f7b44f0ed113d401a0df5f46793f407062566 Mon Sep 17 00:00:00 2001 From: 1F616EMO Date: Sat, 30 Mar 2024 12:16:45 +0800 Subject: [PATCH 02/10] extend place_node --- doc/lua_api.md | 3 ++- src/script/lua_api/l_env.cpp | 18 +++++++++++++++++- src/script/lua_api/l_env.h | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/doc/lua_api.md b/doc/lua_api.md index 2441d7788ffc3..bdf8b12812cad 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -6061,8 +6061,9 @@ Environment access * Returns a number between `0` and `15` * Currently it's the same as `math.floor(param1 / 16)`, except that it ensures compatibility. -* `minetest.place_node(pos, node)` +* `minetest.place_node(pos, node[, placer])` * Place node with the same effects that a player would cause + * `placer`: The ObjectRef that places the node (optional) * `minetest.dig_node(pos[, digger])` * Dig node with the same effects that a player would cause * `digger`: The ObjectRef that digs the node (optional) diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 319e042eb0861..87ad142302a53 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -417,7 +417,7 @@ int ModApiEnv::l_get_natural_light(lua_State *L) return 1; } -// place_node(pos, node) +// place_node(pos, node, [placer]) // pos = {x=num, y=num, z=num} int ModApiEnv::l_place_node(lua_State *L) { @@ -437,6 +437,7 @@ int ModApiEnv::l_place_node(lua_State *L) lua_pushboolean(L, false); return 1; } + // Create item to place std::optional item = ItemStack(ndef->get(n).name, 1, 0, idef); // Make pointed position @@ -444,6 +445,21 @@ int ModApiEnv::l_place_node(lua_State *L) pointed.type = POINTEDTHING_NODE; pointed.node_abovesurface = pos; pointed.node_undersurface = pos + v3s16(0,-1,0); + + if (! lua_isnoneornil(L, 3)) { + ObjectRef *ref = checkObject(L, 3); + ServerActiveObject *placer = ObjectRef::getobject(ref); + + if (placer != nullptr) { + // Dig the node with the provided placer + bool success = scriptIfaceItem->item_OnPlace(item, placer, pointed); + lua_pushboolean(L, success); + return 1; + } + // Otherwise, fallback to NULL placer SAO + // (Seems like Lua already handle invalid args but anyway) + } + // Place it with a NULL placer (appears in Lua as nil) bool success = scriptIfaceItem->item_OnPlace(item, nullptr, pointed); lua_pushboolean(L, success); diff --git a/src/script/lua_api/l_env.h b/src/script/lua_api/l_env.h index 263ae46cde0dc..2dcc76ebfa355 100644 --- a/src/script/lua_api/l_env.h +++ b/src/script/lua_api/l_env.h @@ -89,7 +89,7 @@ class ModApiEnv : public ModApiEnvBase { // timeofday: nil = current time, 0 = night, 0.5 = day static int l_get_natural_light(lua_State *L); - // place_node(pos, node) + // place_node(pos, node, [placer]) // pos = {x=num, y=num, z=num} static int l_place_node(lua_State *L); From 4f24acdd3c892302f2ee27575bf9f0f5922906b6 Mon Sep 17 00:00:00 2001 From: 1F616EMO Date: Sat, 30 Mar 2024 12:21:12 +0800 Subject: [PATCH 03/10] Fix comment typo [skip ci] --- src/script/lua_api/l_env.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 87ad142302a53..374e75c110f1b 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -451,7 +451,7 @@ int ModApiEnv::l_place_node(lua_State *L) ServerActiveObject *placer = ObjectRef::getobject(ref); if (placer != nullptr) { - // Dig the node with the provided placer + // Place the node with the provided placer bool success = scriptIfaceItem->item_OnPlace(item, placer, pointed); lua_pushboolean(L, success); return 1; From 7b5a9171abcccd134103b8bf9cb3aad052f89801 Mon Sep 17 00:00:00 2001 From: 1F616EMO Date: Sat, 30 Mar 2024 12:39:51 +0800 Subject: [PATCH 04/10] extend punch_node --- doc/lua_api.md | 3 ++- src/script/lua_api/l_env.cpp | 18 +++++++++++++++++- src/script/lua_api/l_env.h | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/doc/lua_api.md b/doc/lua_api.md index bdf8b12812cad..c28430622dc39 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -6068,8 +6068,9 @@ Environment access * Dig node with the same effects that a player would cause * `digger`: The ObjectRef that digs the node (optional) * Returns `true` if successful, `false` on failure (e.g. protected location) -* `minetest.punch_node(pos)` +* `minetest.punch_node(pos[, puncher])` * Punch node with the same effects that a player would cause + * `puncher`: The ObjectRef that punches the node (optional) * `minetest.spawn_falling_node(pos)` * Change node into falling node * Returns `true` and the ObjectRef of the spawned entity if successful, `false` on failure diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 374e75c110f1b..31169c12bb53b 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -504,7 +504,7 @@ int ModApiEnv::l_dig_node(lua_State *L) return 1; } -// punch_node(pos) +// punch_node(pos, [puncher]) // pos = {x=num, y=num, z=num} int ModApiEnv::l_punch_node(lua_State *L) { @@ -520,6 +520,22 @@ int ModApiEnv::l_punch_node(lua_State *L) lua_pushboolean(L, false); return 1; } + + if (lua_gettop(L) >= 2 && (! lua_isnoneornil(L, 2))) { + ObjectRef *ref = checkObject(L, 2); + ServerActiveObject *puncher = ObjectRef::getobject(ref); + + if (puncher != nullptr) { + // Puncher the node with the provided puncher + // TODO: Allow custom PointedThing w/ or w/o custom puncher + bool success = scriptIfaceNode->node_on_punch(pos, n, puncher, PointedThing()); + lua_pushboolean(L, success); + return 1; + } + // Otherwise, fallback to NULL puncher SAO + // (Seems like Lua already handle invalid args but anyway) + } + // Punch it with a NULL puncher (appears in Lua as a non-functional // ObjectRef) bool success = scriptIfaceNode->node_on_punch(pos, n, NULL, PointedThing()); diff --git a/src/script/lua_api/l_env.h b/src/script/lua_api/l_env.h index 2dcc76ebfa355..2ed0eb114b91f 100644 --- a/src/script/lua_api/l_env.h +++ b/src/script/lua_api/l_env.h @@ -97,7 +97,7 @@ class ModApiEnv : public ModApiEnvBase { // pos = {x=num, y=num, z=num} static int l_dig_node(lua_State *L); - // punch_node(pos) + // punch_node(pos, [puncher]) // pos = {x=num, y=num, z=num} static int l_punch_node(lua_State *L); From 9e2ad6819e3c8734065afcc21650dfa4520a0566 Mon Sep 17 00:00:00 2001 From: 1F616EMO Date: Sat, 30 Mar 2024 12:53:42 +0800 Subject: [PATCH 05/10] minetest.features.node_interaction_actor --- builtin/game/features.lua | 1 + doc/lua_api.md | 2 ++ 2 files changed, 3 insertions(+) diff --git a/builtin/game/features.lua b/builtin/game/features.lua index 874d3e8856ff9..286c3f1461e8b 100644 --- a/builtin/game/features.lua +++ b/builtin/game/features.lua @@ -39,6 +39,7 @@ core.features = { dynamic_add_media_filepath = true, lsystem_decoration_type = true, item_meta_range = true, + node_interaction_actor = true, } function core.has_feature(arg) diff --git a/doc/lua_api.md b/doc/lua_api.md index c28430622dc39..48068beae9158 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -5431,6 +5431,8 @@ Utilities lsystem_decoration_type = true, -- Overrideable pointing range using the itemstack meta key `"range"` (5.9.0) item_meta_range = true, + -- Allow passing in an optional "actor" ObjectRef into node interaction calls (5.9.0) + node_interaction_actor = true, } ``` From cafeb62f20af306195db32d0160b3a7b9e3fb13d Mon Sep 17 00:00:00 2001 From: 1F616EMO Date: Sat, 30 Mar 2024 19:02:08 +0800 Subject: [PATCH 06/10] Uses nullptr to handle nil actor Resolves: https://github.com/minetest/minetest/pull/14505#discussion_r1545245122 Resolves: https://github.com/minetest/minetest/pull/14505#pullrequestreview-1969885653 --- src/script/lua_api/l_env.cpp | 61 +++++++++++++----------------------- 1 file changed, 21 insertions(+), 40 deletions(-) diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 31169c12bb53b..ceeb5c7e12744 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -446,22 +446,16 @@ int ModApiEnv::l_place_node(lua_State *L) pointed.node_abovesurface = pos; pointed.node_undersurface = pos + v3s16(0,-1,0); + ServerActiveObject *placer = nullptr; + if (! lua_isnoneornil(L, 3)) { ObjectRef *ref = checkObject(L, 3); - ServerActiveObject *placer = ObjectRef::getobject(ref); - - if (placer != nullptr) { - // Place the node with the provided placer - bool success = scriptIfaceItem->item_OnPlace(item, placer, pointed); - lua_pushboolean(L, success); - return 1; - } - // Otherwise, fallback to NULL placer SAO - // (Seems like Lua already handle invalid args but anyway) + placer = ObjectRef::getobject(ref); } // Place it with a NULL placer (appears in Lua as nil) - bool success = scriptIfaceItem->item_OnPlace(item, nullptr, pointed); + // or the given ObjectRef SAO + bool success = scriptIfaceItem->item_OnPlace(item, placer, pointed); lua_pushboolean(L, success); return 1; } @@ -483,23 +477,17 @@ int ModApiEnv::l_dig_node(lua_State *L) return 1; } - if (lua_gettop(L) >= 2 && (! lua_isnoneornil(L, 2))) { - ObjectRef *ref = checkObject(L, 2); - ServerActiveObject *digger = ObjectRef::getobject(ref); + ServerActiveObject *digger = nullptr; - if (digger != nullptr) { - // Dig the node with the provided digger - bool success = scriptIfaceNode->node_on_dig(pos, n, digger); - lua_pushboolean(L, success); - return 1; - } - // Otherwise, fallback to NULL digger SAO - // (Seems like Lua already handle invalid args but anyway) + if (! lua_isnoneornil(L, 2)) { + ObjectRef *ref = checkObject(L, 2); + digger = ObjectRef::getobject(ref); } - // Dig it out with a NULL digger (appears in Lua as a - // non-functional ObjectRef) - bool success = scriptIfaceNode->node_on_dig(pos, n, NULL); + // Dig it out with a NULL digger + // (appears in Lua as a non-functional ObjectRef) + // or the given ObjectRef SAO + bool success = scriptIfaceNode->node_on_dig(pos, n, digger); lua_pushboolean(L, success); return 1; } @@ -521,24 +509,17 @@ int ModApiEnv::l_punch_node(lua_State *L) return 1; } - if (lua_gettop(L) >= 2 && (! lua_isnoneornil(L, 2))) { + ServerActiveObject *puncher = nullptr; + + if (! lua_isnoneornil(L, 2)) { ObjectRef *ref = checkObject(L, 2); - ServerActiveObject *puncher = ObjectRef::getobject(ref); - - if (puncher != nullptr) { - // Puncher the node with the provided puncher - // TODO: Allow custom PointedThing w/ or w/o custom puncher - bool success = scriptIfaceNode->node_on_punch(pos, n, puncher, PointedThing()); - lua_pushboolean(L, success); - return 1; - } - // Otherwise, fallback to NULL puncher SAO - // (Seems like Lua already handle invalid args but anyway) + puncher = ObjectRef::getobject(ref); } - // Punch it with a NULL puncher (appears in Lua as a non-functional - // ObjectRef) - bool success = scriptIfaceNode->node_on_punch(pos, n, NULL, PointedThing()); + // Punch it with a NULL puncher + // (appears in Lua as a non-functional ObjectRef) + // or the given ObjectRef SAO + bool success = scriptIfaceNode->node_on_punch(pos, n, puncher, PointedThing()); lua_pushboolean(L, success); return 1; } From 424ce92e0233ded7c268d8741b50d5a31ea25127 Mon Sep 17 00:00:00 2001 From: 1F616EMO Date: Sat, 30 Mar 2024 19:23:14 +0800 Subject: [PATCH 07/10] Adds TODO inline --- src/script/lua_api/l_env.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index ceeb5c7e12744..6301206e3c0ef 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -519,6 +519,7 @@ int ModApiEnv::l_punch_node(lua_State *L) // Punch it with a NULL puncher // (appears in Lua as a non-functional ObjectRef) // or the given ObjectRef SAO + // TODO: custom PointedThing (requires a converter function) bool success = scriptIfaceNode->node_on_punch(pos, n, puncher, PointedThing()); lua_pushboolean(L, success); return 1; From 391f46c62105d536b0fef43a62767c10b95a21a1 Mon Sep 17 00:00:00 2001 From: 1F616EMO Date: Sun, 31 Mar 2024 06:31:15 +0800 Subject: [PATCH 08/10] Code quality fix --- doc/lua_api.md | 3 ++- src/script/lua_api/l_env.cpp | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/doc/lua_api.md b/doc/lua_api.md index 48068beae9158..40b2c237c5a75 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -5431,7 +5431,8 @@ Utilities lsystem_decoration_type = true, -- Overrideable pointing range using the itemstack meta key `"range"` (5.9.0) item_meta_range = true, - -- Allow passing in an optional "actor" ObjectRef into node interaction calls (5.9.0) + -- Allow passing in an optional "actor" ObjectRef into the following functions: (5.9.0) + -- minetest.place_node, minetest.dig_node, minetest.punch_node node_interaction_actor = true, } ``` diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 6301206e3c0ef..37b0151b47311 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -448,7 +448,7 @@ int ModApiEnv::l_place_node(lua_State *L) ServerActiveObject *placer = nullptr; - if (! lua_isnoneornil(L, 3)) { + if (!lua_isnoneornil(L, 3)) { ObjectRef *ref = checkObject(L, 3); placer = ObjectRef::getobject(ref); } @@ -479,7 +479,7 @@ int ModApiEnv::l_dig_node(lua_State *L) ServerActiveObject *digger = nullptr; - if (! lua_isnoneornil(L, 2)) { + if (!lua_isnoneornil(L, 2)) { ObjectRef *ref = checkObject(L, 2); digger = ObjectRef::getobject(ref); } @@ -511,7 +511,7 @@ int ModApiEnv::l_punch_node(lua_State *L) ServerActiveObject *puncher = nullptr; - if (! lua_isnoneornil(L, 2)) { + if (!lua_isnoneornil(L, 2)) { ObjectRef *ref = checkObject(L, 2); puncher = ObjectRef::getobject(ref); } From 52b07aee19b5bd090aa41f3e80603cecb2ea475f Mon Sep 17 00:00:00 2001 From: 1F616EMO Date: Sun, 31 Mar 2024 06:33:26 +0800 Subject: [PATCH 09/10] sorry I forgot to save files --- src/script/lua_api/l_env.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 37b0151b47311..06dc27b2d8d5d 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -453,8 +453,8 @@ int ModApiEnv::l_place_node(lua_State *L) placer = ObjectRef::getobject(ref); } - // Place it with a NULL placer (appears in Lua as nil) - // or the given ObjectRef SAO + // Place it with a nullptr placer (appears in Lua as nil) + // or the given ObjectRef bool success = scriptIfaceItem->item_OnPlace(item, placer, pointed); lua_pushboolean(L, success); return 1; @@ -484,9 +484,9 @@ int ModApiEnv::l_dig_node(lua_State *L) digger = ObjectRef::getobject(ref); } - // Dig it out with a NULL digger + // Dig it out with a nullptr digger // (appears in Lua as a non-functional ObjectRef) - // or the given ObjectRef SAO + // or the given ObjectRef bool success = scriptIfaceNode->node_on_dig(pos, n, digger); lua_pushboolean(L, success); return 1; @@ -516,9 +516,9 @@ int ModApiEnv::l_punch_node(lua_State *L) puncher = ObjectRef::getobject(ref); } - // Punch it with a NULL puncher + // Punch it with a nullptr puncher // (appears in Lua as a non-functional ObjectRef) - // or the given ObjectRef SAO + // or the given ObjectRef // TODO: custom PointedThing (requires a converter function) bool success = scriptIfaceNode->node_on_punch(pos, n, puncher, PointedThing()); lua_pushboolean(L, success); From 93ba3621b7fcd81cbb18577472c72aaa0a807c03 Mon Sep 17 00:00:00 2001 From: grorp Date: Sat, 30 Mar 2024 23:48:46 +0100 Subject: [PATCH 10/10] Update doc/lua_api.md --- doc/lua_api.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/lua_api.md b/doc/lua_api.md index 40b2c237c5a75..b73ae4ee48ec4 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -5431,8 +5431,8 @@ Utilities lsystem_decoration_type = true, -- Overrideable pointing range using the itemstack meta key `"range"` (5.9.0) item_meta_range = true, - -- Allow passing in an optional "actor" ObjectRef into the following functions: (5.9.0) - -- minetest.place_node, minetest.dig_node, minetest.punch_node + -- Allow passing an optional "actor" ObjectRef to the following functions: + -- minetest.place_node, minetest.dig_node, minetest.punch_node (5.9.0) node_interaction_actor = true, } ```