1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-09-30 19:22:14 +00:00

Give more infos to on_timer() callback

closes #15817
This commit is contained in:
sfan5 2025-08-10 17:22:06 +02:00
parent 7c88996210
commit fd3588d49c
8 changed files with 31 additions and 21 deletions

View file

@ -168,7 +168,7 @@ bool MapBlock::saveStaticObject(u16 id, const StaticObject &obj, u32 reason)
return true;
}
void MapBlock::step(float dtime, const std::function<bool(v3s16, MapNode, f32)> &on_timer_cb)
void MapBlock::step(float dtime, const std::function<bool(v3s16, MapNode, NodeTimer)> &on_timer_cb)
{
// Run callbacks for elapsed node_timers
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)>
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;
}

View file

@ -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<bool(v3s16, MapNode, f32)> &on_timer_cb);
void step(float dtime, const std::function<bool(v3s16, MapNode, NodeTimer)> &on_timer_cb);
////
//// Timestamp (see m_timestamp)

View file

@ -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<bool>(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<bool>(L, -1, false);
lua_pop(L, 2); // error handler, return value
return ret;
}
void ScriptApiNode::node_on_receive_fields(v3s16 p,

View file

@ -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[];

View file

@ -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);
});
}
}