Skip to content

Commit

Permalink
Expose target server step dtime to Lua API
Browse files Browse the repository at this point in the history
  • Loading branch information
grorp committed Oct 28, 2024
1 parent c81cc4f commit 2da9767
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions doc/lua_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -5934,6 +5934,8 @@ Call these functions only at load time!
* `core.register_globalstep(function(dtime))`
* Called every server step, usually interval of 0.1s.
* `dtime` is the time since last execution in seconds.
* You can get the target interval via `core.get_target_dtime()`.
* This is usually true: `dtime >= core.get_target_dtime()`
* `core.register_on_mods_loaded(function())`
* Called after mods have finished loading and before the media is cached or the
aliases handled.
Expand Down Expand Up @@ -6378,6 +6380,12 @@ Environment access
* `core.get_day_count()`: returns number days elapsed since world was
created.
* Time changes are accounted for.
* `core.get_target_dtime()`: returns the target server step `dtime` in seconds.
* This is a lower bound, i.e. server steps may not be shorter than this,
but they are often longer.
* On dedicated servers, this value can be configured via the
`dedicated_server_step` setting in minetest.conf.
* See `core.register_globalstep`.
* `core.find_node_near(pos, radius, nodenames, [search_center])`: returns
pos or `nil`.
* `radius`: using a maximum metric
Expand Down
1 change: 1 addition & 0 deletions games/devtest/mods/unittests/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ dofile(modpath .. "/metadata.lua")
dofile(modpath .. "/raycast.lua")
dofile(modpath .. "/inventory.lua")
dofile(modpath .. "/load_time.lua")
dofile(modpath .. "/step.lua")
dofile(modpath .. "/on_shutdown.lua")
dofile(modpath .. "/color.lua")

Expand Down
8 changes: 8 additions & 0 deletions games/devtest/mods/unittests/step.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local last_target

core.register_globalstep(function(dtime)
local target = core.get_target_dtime()
-- dtime only changes in the next step
assert(dtime == 0 or dtime >= target or dtime >= last_target)
last_target = target
end)
11 changes: 11 additions & 0 deletions src/script/lua_api/l_env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,16 @@ int ModApiEnv::l_get_gametime(lua_State *L)
return 1;
}

// get_target_dtime()
int ModApiEnv::l_get_target_dtime(lua_State *L)
{
GET_ENV_PTR;

auto server = getServer(L);
lua_pushnumber(L, server->getStepSettings().steplen);
return 1;
}

void ModApiEnvBase::collectNodeIds(lua_State *L, int idx, const NodeDefManager *ndef,
std::vector<content_t> &filter)
{
Expand Down Expand Up @@ -1413,6 +1423,7 @@ void ModApiEnv::Initialize(lua_State *L, int top)
API_FCT(get_timeofday);
API_FCT(get_gametime);
API_FCT(get_day_count);
API_FCT(get_target_dtime);
API_FCT(find_node_near);
API_FCT(find_nodes_in_area);
API_FCT(find_nodes_in_area_under_air);
Expand Down
3 changes: 3 additions & 0 deletions src/script/lua_api/l_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ class ModApiEnv : public ModApiEnvBase {
// get_day_count() -> int
static int l_get_day_count(lua_State *L);

// get_target_dtime()
static int l_get_target_dtime(lua_State *L);

// find_node_near(pos, radius, nodenames, search_center) -> pos or nil
// nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
static int l_find_node_near(lua_State *L);
Expand Down

0 comments on commit 2da9767

Please sign in to comment.