From 37d80784ddfc0ff07baee214570c80dc5dd92ca7 Mon Sep 17 00:00:00 2001 From: Zughy <63455151+Zughy@users.noreply.github.com> Date: Sat, 22 Jan 2022 12:42:49 +0100 Subject: Allow resetting celestial vault elements by leaving its arguments empty (#11922) --- src/script/lua_api/l_object.cpp | 140 ++++++++++++++++++++-------------------- 1 file changed, 71 insertions(+), 69 deletions(-) (limited to 'src/script/lua_api') diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index 7d937b306..b177a9f7e 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -1732,9 +1732,11 @@ int ObjectRef::l_set_sky(lua_State *L) return 0; SkyboxParams sky_params = player->getSkyParams(); - bool is_colorspec = is_color_table(L, 2); - if (lua_istable(L, 2) && !is_colorspec) { + // reset if empty + if (lua_isnoneornil(L, 2) && lua_isnone(L, 3)) { + sky_params = SkyboxDefaults::getSkyDefaults(); + } else if (lua_istable(L, 2) && !is_color_table(L, 2)) { lua_getfield(L, 2, "base_color"); if (!lua_isnil(L, -1)) read_color(L, -1, &sky_params.bgcolor); @@ -1758,17 +1760,11 @@ int ObjectRef::l_set_sky(lua_State *L) } lua_pop(L, 1); - /* - We want to avoid crashes, so we're checking even if we're not using them. - However, we want to ensure that the skybox can be set to nil when - using "regular" or "plain" skybox modes as textures aren't needed. - */ - - if (sky_params.textures.size() != 6 && sky_params.textures.size() > 0) + // Validate that we either have six or zero textures + if (sky_params.textures.size() != 6 && !sky_params.textures.empty()) throw LuaError("Skybox expects 6 textures!"); - sky_params.clouds = getboolfield_default(L, 2, - "clouds", sky_params.clouds); + sky_params.clouds = getboolfield_default(L, 2, "clouds", sky_params.clouds); lua_getfield(L, 2, "sky_color"); if (lua_istable(L, -1)) { @@ -1816,7 +1812,7 @@ int ObjectRef::l_set_sky(lua_State *L) sky_params.fog_tint_type = luaL_checkstring(L, -1); lua_pop(L, 1); - // Because we need to leave the "sky_color" table. + // pop "sky_color" table lua_pop(L, 1); } } else { @@ -1852,11 +1848,8 @@ int ObjectRef::l_set_sky(lua_State *L) if (lua_istable(L, 4)) { lua_pushnil(L); while (lua_next(L, 4) != 0) { - // Key at index -2, and value at index -1 - if (lua_isstring(L, -1)) - sky_params.textures.emplace_back(readParam(L, -1)); - else - sky_params.textures.emplace_back(""); + // Key at index -2, and value at index -1 + sky_params.textures.emplace_back(readParam(L, -1)); // Remove the value, keep the key for the next iteration lua_pop(L, 1); } @@ -1872,6 +1865,7 @@ int ObjectRef::l_set_sky(lua_State *L) getServer(L)->setMoon(player, moon_params); getServer(L)->setStars(player, star_params); } + getServer(L)->setSky(player, sky_params); lua_pushboolean(L, true); return 1; @@ -1947,21 +1941,20 @@ int ObjectRef::l_set_sun(lua_State *L) if (player == nullptr) return 0; - luaL_checktype(L, 2, LUA_TTABLE); SunParams sun_params = player->getSunParams(); - sun_params.visible = getboolfield_default(L, 2, - "visible", sun_params.visible); - sun_params.texture = getstringfield_default(L, 2, - "texture", sun_params.texture); - sun_params.tonemap = getstringfield_default(L, 2, - "tonemap", sun_params.tonemap); - sun_params.sunrise = getstringfield_default(L, 2, - "sunrise", sun_params.sunrise); - sun_params.sunrise_visible = getboolfield_default(L, 2, - "sunrise_visible", sun_params.sunrise_visible); - sun_params.scale = getfloatfield_default(L, 2, - "scale", sun_params.scale); + // reset if empty + if (lua_isnoneornil(L, 2)) { + sun_params = SkyboxDefaults::getSunDefaults(); + } else { + luaL_checktype(L, 2, LUA_TTABLE); + sun_params.visible = getboolfield_default(L, 2, "visible", sun_params.visible); + sun_params.texture = getstringfield_default(L, 2, "texture", sun_params.texture); + sun_params.tonemap = getstringfield_default(L, 2, "tonemap", sun_params.tonemap); + sun_params.sunrise = getstringfield_default(L, 2, "sunrise", sun_params.sunrise); + sun_params.sunrise_visible = getboolfield_default(L, 2, "sunrise_visible", sun_params.sunrise_visible); + sun_params.scale = getfloatfield_default(L, 2, "scale", sun_params.scale); + } getServer(L)->setSun(player, sun_params); lua_pushboolean(L, true); @@ -2004,17 +1997,18 @@ int ObjectRef::l_set_moon(lua_State *L) if (player == nullptr) return 0; - luaL_checktype(L, 2, LUA_TTABLE); MoonParams moon_params = player->getMoonParams(); - moon_params.visible = getboolfield_default(L, 2, - "visible", moon_params.visible); - moon_params.texture = getstringfield_default(L, 2, - "texture", moon_params.texture); - moon_params.tonemap = getstringfield_default(L, 2, - "tonemap", moon_params.tonemap); - moon_params.scale = getfloatfield_default(L, 2, - "scale", moon_params.scale); + // reset if empty + if (lua_isnoneornil(L, 2)) { + moon_params = SkyboxDefaults::getMoonDefaults(); + } else { + luaL_checktype(L, 2, LUA_TTABLE); + moon_params.visible = getboolfield_default(L, 2, "visible", moon_params.visible); + moon_params.texture = getstringfield_default(L, 2, "texture", moon_params.texture); + moon_params.tonemap = getstringfield_default(L, 2, "tonemap", moon_params.tonemap); + moon_params.scale = getfloatfield_default(L, 2, "scale", moon_params.scale); + } getServer(L)->setMoon(player, moon_params); lua_pushboolean(L, true); @@ -2053,21 +2047,24 @@ int ObjectRef::l_set_stars(lua_State *L) if (player == nullptr) return 0; - luaL_checktype(L, 2, LUA_TTABLE); StarParams star_params = player->getStarParams(); - star_params.visible = getboolfield_default(L, 2, - "visible", star_params.visible); - star_params.count = getintfield_default(L, 2, - "count", star_params.count); + // reset if empty + if (lua_isnoneornil(L, 2)) { + star_params = SkyboxDefaults::getStarDefaults(); + } else { + luaL_checktype(L, 2, LUA_TTABLE); + star_params.visible = getboolfield_default(L, 2, "visible", star_params.visible); + star_params.count = getintfield_default(L, 2, "count", star_params.count); - lua_getfield(L, 2, "star_color"); - if (!lua_isnil(L, -1)) - read_color(L, -1, &star_params.starcolor); - lua_pop(L, 1); + lua_getfield(L, 2, "star_color"); + if (!lua_isnil(L, -1)) + read_color(L, -1, &star_params.starcolor); + lua_pop(L, 1); - star_params.scale = getfloatfield_default(L, 2, - "scale", star_params.scale); + star_params.scale = getfloatfield_default(L, 2, + "scale", star_params.scale); + } getServer(L)->setStars(player, star_params); lua_pushboolean(L, true); @@ -2106,31 +2103,36 @@ int ObjectRef::l_set_clouds(lua_State *L) if (player == nullptr) return 0; - luaL_checktype(L, 2, LUA_TTABLE); CloudParams cloud_params = player->getCloudParams(); - cloud_params.density = getfloatfield_default(L, 2, "density", cloud_params.density); + // reset if empty + if (lua_isnoneornil(L, 2)) { + cloud_params = SkyboxDefaults::getCloudDefaults(); + } else { + luaL_checktype(L, 2, LUA_TTABLE); + cloud_params.density = getfloatfield_default(L, 2, "density", cloud_params.density); - lua_getfield(L, 2, "color"); - if (!lua_isnil(L, -1)) - read_color(L, -1, &cloud_params.color_bright); - lua_pop(L, 1); - lua_getfield(L, 2, "ambient"); - if (!lua_isnil(L, -1)) - read_color(L, -1, &cloud_params.color_ambient); - lua_pop(L, 1); + lua_getfield(L, 2, "color"); + if (!lua_isnil(L, -1)) + read_color(L, -1, &cloud_params.color_bright); + lua_pop(L, 1); + lua_getfield(L, 2, "ambient"); + if (!lua_isnil(L, -1)) + read_color(L, -1, &cloud_params.color_ambient); + lua_pop(L, 1); - cloud_params.height = getfloatfield_default(L, 2, "height", cloud_params.height ); - cloud_params.thickness = getfloatfield_default(L, 2, "thickness", cloud_params.thickness); + cloud_params.height = getfloatfield_default(L, 2, "height", cloud_params.height); + cloud_params.thickness = getfloatfield_default(L, 2, "thickness", cloud_params.thickness); - lua_getfield(L, 2, "speed"); - if (lua_istable(L, -1)) { - v2f new_speed; - new_speed.X = getfloatfield_default(L, -1, "x", 0); - new_speed.Y = getfloatfield_default(L, -1, "z", 0); - cloud_params.speed = new_speed; + lua_getfield(L, 2, "speed"); + if (lua_istable(L, -1)) { + v2f new_speed; + new_speed.X = getfloatfield_default(L, -1, "x", 0); + new_speed.Y = getfloatfield_default(L, -1, "z", 0); + cloud_params.speed = new_speed; + } + lua_pop(L, 1); } - lua_pop(L, 1); getServer(L)->setClouds(player, cloud_params); lua_pushboolean(L, true); -- cgit v1.2.3