diff --git a/res/scripts/stdcmd.lua b/res/scripts/stdcmd.lua index 42aae5ee..d3b632f6 100644 --- a/res/scripts/stdcmd.lua +++ b/res/scripts/stdcmd.lua @@ -273,7 +273,7 @@ console.add_command( return "weather preset not found" end local preset = json.parse(file.read(filename)) - gfx.weather.change(preset, args[2]) + gfx.weather.change(preset, args[2], args[1]) return "weather set to "..filename.." preset ("..tostring(args[2]).." s)" end ) diff --git a/src/graphics/render/WorldRenderer.hpp b/src/graphics/render/WorldRenderer.hpp index 2b415568..d5f4d23c 100644 --- a/src/graphics/render/WorldRenderer.hpp +++ b/src/graphics/render/WorldRenderer.hpp @@ -37,6 +37,8 @@ struct EngineSettings; struct Weather { WeatherPreset a {}; WeatherPreset b {}; + std::string nameA; + std::string nameB; float t = 1.0f; float speed = 0.0f; @@ -47,11 +49,13 @@ struct Weather { a.intensity = 1.0f - t; } - void change(WeatherPreset preset, float time) { + void change(WeatherPreset preset, float time, std::string name="") { std::swap(a, b); + std::swap(nameA, nameB); b = std::move(preset); t = 0.0f; speed = 1.0f / glm::max(time, 1.e-5f); + nameB = std::move(name); update(0.0f); } diff --git a/src/logic/scripting/lua/libs/libweather.cpp b/src/logic/scripting/lua/libs/libweather.cpp index 8544ab0d..3a2dc096 100644 --- a/src/logic/scripting/lua/libs/libweather.cpp +++ b/src/logic/scripting/lua/libs/libweather.cpp @@ -6,11 +6,44 @@ static int l_change(lua::State* L) { WeatherPreset weather {}; weather.deserialize(lua::tovalue(L, 1)); float time = lua::tonumber(L, 2); - renderer->weather.change(std::move(weather), time); + std::string name; + if (lua::isstring(L, 3)) { + name = lua::tostring(L, 3); + } + renderer->weather.change(std::move(weather), time, std::move(name)); return 0; } +static int l_get_current(lua::State* L) { + if (renderer->weather.t > 0.5f) { + return lua::pushstring(L, renderer->weather.nameB); + } else { + return lua::pushstring(L, renderer->weather.nameA); + } +} + +static int l_get_fall_intencity(lua::State* L) { + const auto& a = renderer->weather.a; + const auto& b = renderer->weather.b; + float t = renderer->weather.t; + return lua::pushnumber(L, + (a.fall.texture.empty() ? 0.0f : (1.0f - t)) + + (b.fall.texture.empty() ? 0.0f : t) + ); +} + +static int l_get_current_data(lua::State* L) { + if (renderer->weather.t > 0.5f) { + return lua::pushvalue(L, renderer->weather.b.serialize()); + } else { + return lua::pushvalue(L, renderer->weather.a.serialize()); + } +} + const luaL_Reg weatherlib[] = { {"change", wrap_hud}, + {"get_current", wrap_hud}, + {"get_current_data", wrap_hud}, + {"get_fall_intencity", wrap_hud}, {NULL, NULL} };