Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow optional actor ObjectRef value in node interaction calls #14505

Merged
merged 10 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions builtin/game/features.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 8 additions & 3 deletions doc/lua_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Emojigit marked this conversation as resolved.
Show resolved Hide resolved
node_interaction_actor = true,
}
```

Expand Down Expand Up @@ -6061,13 +6063,16 @@ 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
* `minetest.dig_node(pos)`
* `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)
* 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
Expand Down
54 changes: 51 additions & 3 deletions src/script/lua_api/l_env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -416,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)
{
Expand All @@ -436,20 +437,36 @@ int ModApiEnv::l_place_node(lua_State *L)
lua_pushboolean(L, false);
return 1;
}

// Create item to place
std::optional<ItemStack> item = ItemStack(ndef->get(n).name, 1, 0, idef);
// Make pointed position
PointedThing pointed;
pointed.type = POINTEDTHING_NODE;
pointed.node_abovesurface = pos;
pointed.node_undersurface = pos + v3s16(0,-1,0);

if (! lua_isnoneornil(L, 3)) {
Emojigit marked this conversation as resolved.
Show resolved Hide resolved
ObjectRef *ref = checkObject<ObjectRef>(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)
}

// Place it with a NULL placer (appears in Lua as nil)
Emojigit marked this conversation as resolved.
Show resolved Hide resolved
bool success = scriptIfaceItem->item_OnPlace(item, nullptr, pointed);
lua_pushboolean(L, success);
return 1;
}

// dig_node(pos)
// dig_node(pos, [digger])
// pos = {x=num, y=num, z=num}
int ModApiEnv::l_dig_node(lua_State *L)
{
Expand All @@ -465,14 +482,29 @@ int ModApiEnv::l_dig_node(lua_State *L)
lua_pushboolean(L, false);
return 1;
}

if (lua_gettop(L) >= 2 && (! lua_isnoneornil(L, 2))) {
Emojigit marked this conversation as resolved.
Show resolved Hide resolved
ObjectRef *ref = checkObject<ObjectRef>(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);
lua_pushboolean(L, success);
return 1;
}

// punch_node(pos)
// punch_node(pos, [puncher])
// pos = {x=num, y=num, z=num}
int ModApiEnv::l_punch_node(lua_State *L)
{
Expand All @@ -488,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<ObjectRef>(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());
Expand Down
6 changes: 3 additions & 3 deletions src/script/lua_api/l_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ 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);

// dig_node(pos)
// dig_node(pos, [digger])
// 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);

Expand Down
Loading