From 7bab390413845f78cf80a0b95e066b9c18e24fe7 Mon Sep 17 00:00:00 2001 From: Gregor Parzefall Date: Fri, 13 Sep 2024 13:14:31 +0200 Subject: [PATCH] Add time_to_day_night_ratio to the Lua API --- doc/lua_api.md | 4 ++++ src/script/lua_api/l_util.cpp | 15 +++++++++++++++ src/script/lua_api/l_util.h | 3 +++ 3 files changed, 22 insertions(+) diff --git a/doc/lua_api.md b/doc/lua_api.md index ec00b5130..ad8eacb06 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -5659,6 +5659,9 @@ Utilities form. If the ColorSpec is invalid, returns `nil`. You can use this to parse ColorStrings. * `colorspec`: The ColorSpec to convert +* `minetest.time_to_day_night_ratio(time_of_day)`: Returns a "day-night ratio" value + (as accepted by `ObjectRef:override_day_night_ratio`) that is equivalent to + the given "time of day" value (as returned by `minetest.get_timeofday`). * `minetest.encode_png(width, height, data, [compression])`: Encode a PNG image and return it in string form. * `width`: Width of the image @@ -8519,6 +8522,7 @@ child will follow movement and rotation of that bone. * `0`...`1`: Overrides day-night ratio, controlling sunlight to a specific amount. * Passing no arguments disables override, defaulting to sunlight based on day-night cycle + * See also `minetest.time_to_day_night_ratio`, * `get_day_night_ratio()`: returns the ratio or nil if it isn't overridden * `set_local_animation(idle, walk, dig, walk_while_dig, frame_speed)`: set animation for player model in third person view. diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index 0c66521f7..75a11a050 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -45,6 +45,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "my_sha256.h" #include "util/png.h" #include "player.h" +#include "daynightratio.h" #include // only available in zstd 1.3.5+ @@ -640,6 +641,17 @@ int ModApiUtil::l_colorspec_to_table(lua_State *L) return 0; } +// time_to_day_night_ratio(time_of_day) +int ModApiUtil::l_time_to_day_night_ratio(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + float time_of_day = lua_tonumber(L, 1) * 24000; + u32 dnr = time_to_daynight_ratio(time_of_day, true); + lua_pushnumber(L, dnr / 1000.0f); + return 1; +} + // encode_png(w, h, data, level) int ModApiUtil::l_encode_png(lua_State *L) { @@ -741,6 +753,7 @@ void ModApiUtil::Initialize(lua_State *L, int top) API_FCT(colorspec_to_colorstring); API_FCT(colorspec_to_bytes); API_FCT(colorspec_to_table); + API_FCT(time_to_day_night_ratio); API_FCT(encode_png); @@ -777,6 +790,7 @@ void ModApiUtil::InitializeClient(lua_State *L, int top) API_FCT(colorspec_to_colorstring); API_FCT(colorspec_to_bytes); API_FCT(colorspec_to_table); + API_FCT(time_to_day_night_ratio); API_FCT(get_last_run_mod); API_FCT(set_last_run_mod); @@ -822,6 +836,7 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top) API_FCT(colorspec_to_colorstring); API_FCT(colorspec_to_bytes); API_FCT(colorspec_to_table); + API_FCT(time_to_day_night_ratio); API_FCT(encode_png); diff --git a/src/script/lua_api/l_util.h b/src/script/lua_api/l_util.h index 442e0749d..89cc684e1 100644 --- a/src/script/lua_api/l_util.h +++ b/src/script/lua_api/l_util.h @@ -125,6 +125,9 @@ private: // colorspec_to_table(colorspec) static int l_colorspec_to_table(lua_State *L); + // time_to_day_night_ratio(time_of_day) + static int l_time_to_day_night_ratio(lua_State *L); + // encode_png(w, h, data, level) static int l_encode_png(lua_State *L);