Skip to content

Commit fd3588d

Browse files
committed
Give more infos to on_timer() callback
closes #15817
1 parent 7c88996 commit fd3588d

8 files changed

Lines changed: 31 additions & 21 deletions

File tree

‎builtin/game/features.lua‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ core.features = {
4848
remove_item_match_meta = true,
4949
httpfetch_additional_methods = true,
5050
object_guids = true,
51+
on_timer_four_args = true,
5152
}
5253

5354
function core.has_feature(arg)

‎doc/lua_api.md‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,6 @@ The file is a key-value store of modpack details.
177177
* `textdomain`: Textdomain used to translate title and description. Defaults to modpack name.
178178
See [Translating content meta](#translating-content-meta).
179179

180-
Note: to support 0.4.x, please also create an empty modpack.txt file.
181-
182180
Mod directory structure
183181
-----------------------
184182

@@ -5841,13 +5839,16 @@ Utilities
58415839
httpfetch_additional_methods = true,
58425840
-- objects have get_guid method (5.13.0)
58435841
object_guids = true,
5842+
-- The NodeTimer `on_timer` callback is passed additional `node` and `timeout` args (5.14.0)
5843+
on_timer_four_args = true,
58445844
}
58455845
```
58465846

58475847
* `core.has_feature(arg)`: returns `boolean, missing_features`
58485848
* checks for *server-side* feature availability
58495849
* `arg`: string or table in format `{foo=true, bar=true}`
58505850
* `missing_features`: `{foo=true, bar=true}`
5851+
58515852
* `core.get_player_information(player_name)`: Table containing information
58525853
about a player. Example return value:
58535854

@@ -10472,19 +10473,19 @@ Used by `core.register_node`.
1047210473
-- itemstack will hold clicker's wielded item.
1047310474
-- Shall return the leftover itemstack.
1047410475
-- Note: pointed_thing can be nil, if a mod calls this function.
10475-
-- This function does not get triggered by clients <=0.4.16 if the
10476-
-- "formspec" node metadata field is set.
1047710476

1047810477
on_dig = function(pos, node, digger),
1047910478
-- default: core.node_dig
1048010479
-- By default checks privileges, wears out item (if tool) and removes node.
1048110480
-- return true if the node was dug successfully, false otherwise.
1048210481
-- Deprecated: returning nil is the same as returning true.
1048310482

10484-
on_timer = function(pos, elapsed),
10483+
on_timer = function(pos, elapsed, node, timeout),
1048510484
-- default: nil
1048610485
-- called by NodeTimers, see core.get_node_timer and NodeTimerRef.
10487-
-- elapsed is the total time passed since the timer was started.
10486+
-- `elapsed`: total time passed since the timer was started.
10487+
-- `node`: node table (since 5.14)
10488+
-- `timeout`: timeout value of the just ended timer (since 5.14)
1048810489
-- return true to run the timer for another cycle with the same timeout
1048910490
-- value.
1049010491

‎games/devtest/mods/callbacks/nodes.lua‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ core.register_node("callbacks:callback_node", {
4343
print_to_everything("callbacks:callback_node:after_dig_node("..core.pos_to_string(pos)..")")
4444
end,
4545

46-
on_timer = function(pos, elapsed)
47-
print_to_everything("callbacks:callback_node:on_timer(): elapsed="..dump(elapsed))
46+
on_timer = function(pos, elapsed, node, timeout)
47+
print_to_everything("callbacks:callback_node:on_timer(): elapsed="..
48+
elapsed .. " node=" .. dump(node) .. " timeout=" .. timeout)
4849
return true
4950
end,
5051
})

‎src/mapblock.cpp‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ bool MapBlock::saveStaticObject(u16 id, const StaticObject &obj, u32 reason)
168168
return true;
169169
}
170170

171-
void MapBlock::step(float dtime, const std::function<bool(v3s16, MapNode, f32)> &on_timer_cb)
171+
void MapBlock::step(float dtime, const std::function<bool(v3s16, MapNode, NodeTimer)> &on_timer_cb)
172172
{
173173
// Run callbacks for elapsed node_timers
174174
std::vector<NodeTimer> elapsed_timers = m_node_timers.step(dtime);
@@ -177,8 +177,10 @@ void MapBlock::step(float dtime, const std::function<bool(v3s16, MapNode, f32)>
177177
for (const auto &it : elapsed_timers) {
178178
n = getNodeNoEx(it.position);
179179
p = it.position + getPosRelative();
180-
if (on_timer_cb(p, n, it.elapsed))
180+
if (on_timer_cb(p, n, it)) {
181+
// restart
181182
setNodeTimer(NodeTimer(it.timeout, 0, it.position));
183+
}
182184
if (isOrphan())
183185
return;
184186
}

‎src/mapblock.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ class MapBlock
312312
bool saveStaticObject(u16 id, const StaticObject &obj, u32 reason);
313313

314314
/// @note This method is only for Server, don't call it on client
315-
void step(float dtime, const std::function<bool(v3s16, MapNode, f32)> &on_timer_cb);
315+
void step(float dtime, const std::function<bool(v3s16, MapNode, NodeTimer)> &on_timer_cb);
316316

317317
////
318318
//// Timestamp (see m_timestamp)

‎src/script/cpp_api/s_node.cpp‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
215215
lua_pop(L, 1); // Pop error handler
216216
}
217217

218-
bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
218+
bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 elapsed, f32 timeout)
219219
{
220220
SCRIPTAPI_PRECHECKHEADER
221221

@@ -229,10 +229,14 @@ bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
229229

230230
// Call function
231231
push_v3s16(L, p);
232-
lua_pushnumber(L,dtime);
233-
PCALL_RES(lua_pcall(L, 2, 1, error_handler));
234-
lua_remove(L, error_handler);
235-
return readParam<bool>(L, -1, false);
232+
lua_pushnumber(L, elapsed);
233+
pushnode(L, node);
234+
lua_pushnumber(L, timeout);
235+
PCALL_RES(lua_pcall(L, 4, 1, error_handler));
236+
bool ret = readParam<bool>(L, -1, false);
237+
lua_pop(L, 2); // error handler, return value
238+
239+
return ret;
236240
}
237241

238242
void ScriptApiNode::node_on_receive_fields(v3s16 p,

‎src/script/cpp_api/s_node.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ class ScriptApiNode
2828
void node_on_destruct(v3s16 p, MapNode node);
2929
bool node_on_flood(v3s16 p, MapNode node, MapNode newnode);
3030
void node_after_destruct(v3s16 p, MapNode node);
31-
bool node_on_timer(v3s16 p, MapNode node, f32 dtime);
31+
bool node_on_timer(v3s16 p, MapNode node, f32 elapsed, f32 timeout);
3232
void node_on_receive_fields(v3s16 p,
3333
const std::string &formname,
3434
const StringMap &fields,
3535
ServerActiveObject *sender);
36+
3637
public:
3738
static struct EnumString es_DrawType[];
3839
static struct EnumString es_ContentParamType[];

‎src/serverenvironment.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,8 @@ void ServerEnvironment::activateBlock(MapBlock *block)
572572
return;
573573

574574
// Run node timers
575-
block->step((float)dtime_s, [&](v3s16 p, MapNode n, f32 d) -> bool {
576-
return m_script->node_on_timer(p, n, d);
575+
block->step((float)dtime_s, [&](v3s16 p, MapNode n, NodeTimer t) -> bool {
576+
return m_script->node_on_timer(p, n, t.elapsed, t.timeout);
577577
});
578578
}
579579

@@ -999,8 +999,8 @@ void ServerEnvironment::step(float dtime)
999999
}
10001000

10011001
// Run node timers
1002-
block->step(dtime, [&](v3s16 p, MapNode n, f32 d) -> bool {
1003-
return m_script->node_on_timer(p, n, d);
1002+
block->step(dtime, [&](v3s16 p, MapNode n, NodeTimer t) -> bool {
1003+
return m_script->node_on_timer(p, n, t.elapsed, t.timeout);
10041004
});
10051005
}
10061006
}

0 commit comments

Comments
 (0)