diff --git a/builtin/game/features.lua b/builtin/game/features.lua index 4837f29bd..0bbb1d366 100644 --- a/builtin/game/features.lua +++ b/builtin/game/features.lua @@ -48,6 +48,7 @@ core.features = { remove_item_match_meta = true, httpfetch_additional_methods = true, object_guids = true, + on_timer_four_args = true, } function core.has_feature(arg) diff --git a/doc/lua_api.md b/doc/lua_api.md index b7fe22640..a49b48ac2 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -177,8 +177,6 @@ The file is a key-value store of modpack details. * `textdomain`: Textdomain used to translate title and description. Defaults to modpack name. See [Translating content meta](#translating-content-meta). -Note: to support 0.4.x, please also create an empty modpack.txt file. - Mod directory structure ----------------------- @@ -5841,6 +5839,8 @@ Utilities httpfetch_additional_methods = true, -- objects have get_guid method (5.13.0) object_guids = true, + -- The NodeTimer `on_timer` callback is passed additional `node` and `timeout` args (5.14.0) + on_timer_four_args = true, } ``` @@ -5848,6 +5848,7 @@ Utilities * checks for *server-side* feature availability * `arg`: string or table in format `{foo=true, bar=true}` * `missing_features`: `{foo=true, bar=true}` + * `core.get_player_information(player_name)`: Table containing information about a player. Example return value: @@ -10472,8 +10473,6 @@ Used by `core.register_node`. -- itemstack will hold clicker's wielded item. -- Shall return the leftover itemstack. -- Note: pointed_thing can be nil, if a mod calls this function. - -- This function does not get triggered by clients <=0.4.16 if the - -- "formspec" node metadata field is set. on_dig = function(pos, node, digger), -- default: core.node_dig @@ -10481,10 +10480,12 @@ Used by `core.register_node`. -- return true if the node was dug successfully, false otherwise. -- Deprecated: returning nil is the same as returning true. - on_timer = function(pos, elapsed), + on_timer = function(pos, elapsed, node, timeout), -- default: nil -- called by NodeTimers, see core.get_node_timer and NodeTimerRef. - -- elapsed is the total time passed since the timer was started. + -- `elapsed`: total time passed since the timer was started. + -- `node`: node table (since 5.14) + -- `timeout`: timeout value of the just ended timer (since 5.14) -- return true to run the timer for another cycle with the same timeout -- value. diff --git a/games/devtest/mods/callbacks/nodes.lua b/games/devtest/mods/callbacks/nodes.lua index 67f4be455..490fc7e94 100644 --- a/games/devtest/mods/callbacks/nodes.lua +++ b/games/devtest/mods/callbacks/nodes.lua @@ -43,8 +43,9 @@ core.register_node("callbacks:callback_node", { print_to_everything("callbacks:callback_node:after_dig_node("..core.pos_to_string(pos)..")") end, - on_timer = function(pos, elapsed) - print_to_everything("callbacks:callback_node:on_timer(): elapsed="..dump(elapsed)) + on_timer = function(pos, elapsed, node, timeout) + print_to_everything("callbacks:callback_node:on_timer(): elapsed=".. + elapsed .. " node=" .. dump(node) .. " timeout=" .. timeout) return true end, }) diff --git a/src/mapblock.cpp b/src/mapblock.cpp index 68cc0aeec..d555f6423 100644 --- a/src/mapblock.cpp +++ b/src/mapblock.cpp @@ -168,7 +168,7 @@ bool MapBlock::saveStaticObject(u16 id, const StaticObject &obj, u32 reason) return true; } -void MapBlock::step(float dtime, const std::function &on_timer_cb) +void MapBlock::step(float dtime, const std::function &on_timer_cb) { // Run callbacks for elapsed node_timers std::vector elapsed_timers = m_node_timers.step(dtime); @@ -177,8 +177,10 @@ void MapBlock::step(float dtime, const std::function for (const auto &it : elapsed_timers) { n = getNodeNoEx(it.position); p = it.position + getPosRelative(); - if (on_timer_cb(p, n, it.elapsed)) + if (on_timer_cb(p, n, it)) { + // restart setNodeTimer(NodeTimer(it.timeout, 0, it.position)); + } if (isOrphan()) return; } diff --git a/src/mapblock.h b/src/mapblock.h index a827c3f6d..86ff93b59 100644 --- a/src/mapblock.h +++ b/src/mapblock.h @@ -312,7 +312,7 @@ public: bool saveStaticObject(u16 id, const StaticObject &obj, u32 reason); /// @note This method is only for Server, don't call it on client - void step(float dtime, const std::function &on_timer_cb); + void step(float dtime, const std::function &on_timer_cb); //// //// Timestamp (see m_timestamp) diff --git a/src/script/cpp_api/s_node.cpp b/src/script/cpp_api/s_node.cpp index 98169975a..f0886b703 100644 --- a/src/script/cpp_api/s_node.cpp +++ b/src/script/cpp_api/s_node.cpp @@ -215,7 +215,7 @@ void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node) lua_pop(L, 1); // Pop error handler } -bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime) +bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 elapsed, f32 timeout) { SCRIPTAPI_PRECHECKHEADER @@ -229,10 +229,14 @@ bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime) // Call function push_v3s16(L, p); - lua_pushnumber(L,dtime); - PCALL_RES(lua_pcall(L, 2, 1, error_handler)); - lua_remove(L, error_handler); - return readParam(L, -1, false); + lua_pushnumber(L, elapsed); + pushnode(L, node); + lua_pushnumber(L, timeout); + PCALL_RES(lua_pcall(L, 4, 1, error_handler)); + bool ret = readParam(L, -1, false); + lua_pop(L, 2); // error handler, return value + + return ret; } void ScriptApiNode::node_on_receive_fields(v3s16 p, diff --git a/src/script/cpp_api/s_node.h b/src/script/cpp_api/s_node.h index 2c7133df1..2a7897ef1 100644 --- a/src/script/cpp_api/s_node.h +++ b/src/script/cpp_api/s_node.h @@ -28,11 +28,12 @@ public: void node_on_destruct(v3s16 p, MapNode node); bool node_on_flood(v3s16 p, MapNode node, MapNode newnode); void node_after_destruct(v3s16 p, MapNode node); - bool node_on_timer(v3s16 p, MapNode node, f32 dtime); + bool node_on_timer(v3s16 p, MapNode node, f32 elapsed, f32 timeout); void node_on_receive_fields(v3s16 p, const std::string &formname, const StringMap &fields, ServerActiveObject *sender); + public: static struct EnumString es_DrawType[]; static struct EnumString es_ContentParamType[]; diff --git a/src/serverenvironment.cpp b/src/serverenvironment.cpp index 3914ef364..a9f072583 100644 --- a/src/serverenvironment.cpp +++ b/src/serverenvironment.cpp @@ -572,8 +572,8 @@ void ServerEnvironment::activateBlock(MapBlock *block) return; // Run node timers - block->step((float)dtime_s, [&](v3s16 p, MapNode n, f32 d) -> bool { - return m_script->node_on_timer(p, n, d); + block->step((float)dtime_s, [&](v3s16 p, MapNode n, NodeTimer t) -> bool { + return m_script->node_on_timer(p, n, t.elapsed, t.timeout); }); } @@ -999,8 +999,8 @@ void ServerEnvironment::step(float dtime) } // Run node timers - block->step(dtime, [&](v3s16 p, MapNode n, f32 d) -> bool { - return m_script->node_on_timer(p, n, d); + block->step(dtime, [&](v3s16 p, MapNode n, NodeTimer t) -> bool { + return m_script->node_on_timer(p, n, t.elapsed, t.timeout); }); } }