Allow resetting celestial vault elements by leaving its arguments empty (#11922)

This commit is contained in:
Zughy 2022-01-22 12:42:49 +01:00 committed by GitHub
parent f66ed2c27f
commit 37d80784dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 124 additions and 120 deletions

@ -6794,12 +6794,15 @@ object you are working with still exists.
* `set_sky(sky_parameters)`
* The presence of the function `set_sun`, `set_moon` or `set_stars` indicates
whether `set_sky` accepts this format. Check the legacy format otherwise.
* Passing no arguments resets the sky to its default values.
* `sky_parameters` is a table with the following optional fields:
* `base_color`: ColorSpec, changes fog in "skybox" and "plain".
(default: `#ffffff`)
* `type`: Available types:
* `"regular"`: Uses 0 textures, `base_color` ignored
* `"skybox"`: Uses 6 textures, `base_color` used as fog.
* `"plain"`: Uses 0 textures, `base_color` used as both fog and sky.
(default: `"regular"`)
* `textures`: A table containing up to six textures in the following
order: Y+ (top), Y- (bottom), X- (west), X+ (east), Z+ (north), Z- (south).
* `clouds`: Boolean for whether clouds appear. (default: `true`)
@ -6828,9 +6831,9 @@ object you are working with still exists.
* `indoors`: ColorSpec, for when you're either indoors or underground.
(default: `#646464`)
* `fog_sun_tint`: ColorSpec, changes the fog tinting for the sun
at sunrise and sunset.
at sunrise and sunset. (default: `#f47d1d`)
* `fog_moon_tint`: ColorSpec, changes the fog tinting for the moon
at sunrise and sunset.
at sunrise and sunset. (default: `#7f99cc`)
* `fog_tint_type`: string, changes which mode the directional fog
abides by, `"custom"` uses `sun_tint` and `moon_tint`, while
`"default"` uses the classic Minetest sun and moon tinting.
@ -6848,6 +6851,7 @@ object you are working with still exists.
* `get_sky_color()`: returns a table with the `sky_color` parameters as in
`set_sky`.
* `set_sun(sun_parameters)`:
* Passing no arguments resets the sun to its default values.
* `sun_parameters` is a table with the following optional fields:
* `visible`: Boolean for whether the sun is visible.
(default: `true`)
@ -6863,6 +6867,7 @@ object you are working with still exists.
* `get_sun()`: returns a table with the current sun parameters as in
`set_sun`.
* `set_moon(moon_parameters)`:
* Passing no arguments resets the moon to its default values.
* `moon_parameters` is a table with the following optional fields:
* `visible`: Boolean for whether the moon is visible.
(default: `true`)
@ -6874,6 +6879,7 @@ object you are working with still exists.
* `get_moon()`: returns a table with the current moon parameters as in
`set_moon`.
* `set_stars(star_parameters)`:
* Passing no arguments resets stars to their default values.
* `star_parameters` is a table with the following optional fields:
* `visible`: Boolean for whether the stars are visible.
(default: `true`)
@ -6887,6 +6893,7 @@ object you are working with still exists.
* `get_stars()`: returns a table with the current stars parameters as in
`set_stars`.
* `set_clouds(cloud_parameters)`: set cloud parameters
* Passing no arguments resets clouds to their default values.
* `cloud_parameters` is a table with the following optional fields:
* `density`: from `0` (no clouds) to `1` (full clouds) (default `0.4`)
* `color`: basic cloud color with alpha channel, ColorSpec

@ -22,7 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "irrlichttypes_extrabloated.h"
#include <iostream>
#include "constants.h"
#include "cloudparams.h"
#include "skyparams.h"
// Menu clouds
class Clouds;

@ -1,30 +0,0 @@
/*
Minetest
Copyright (C) 2017 bendeutsch, Ben Deutsch <ben@bendeutsch.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
struct CloudParams
{
float density;
video::SColor color_bright;
video::SColor color_ambient;
float thickness;
float height;
v2f speed;
};

@ -68,19 +68,10 @@ RemotePlayer::RemotePlayer(const char *name, IItemDefManager *idef):
m_cloud_params.speed = v2f(0.0f, -2.0f);
// Skybox defaults:
SkyboxDefaults sky_defaults;
m_skybox_params.sky_color = sky_defaults.getSkyColorDefaults();
m_skybox_params.type = "regular";
m_skybox_params.clouds = true;
m_skybox_params.fog_sun_tint = video::SColor(255, 244, 125, 29);
m_skybox_params.fog_moon_tint = video::SColorf(0.5, 0.6, 0.8, 1).toSColor();
m_skybox_params.fog_tint_type = "default";
m_sun_params = sky_defaults.getSunDefaults();
m_moon_params = sky_defaults.getMoonDefaults();
m_star_params = sky_defaults.getStarDefaults();
m_skybox_params = SkyboxDefaults::getSkyDefaults();
m_sun_params = SkyboxDefaults::getSunDefaults();
m_moon_params = SkyboxDefaults::getMoonDefaults();
m_star_params = SkyboxDefaults::getStarDefaults();
}

@ -21,7 +21,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
#include "player.h"
#include "cloudparams.h"
#include "skyparams.h"
class PlayerSAO;

@ -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 {
@ -1853,10 +1849,7 @@ int ObjectRef::l_set_sky(lua_State *L)
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<std::string>(L, -1));
else
sky_params.textures.emplace_back("");
// 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,13 +2047,15 @@ 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))
@ -2068,6 +2064,7 @@ int ObjectRef::l_set_stars(lua_State *L)
star_params.scale = getfloatfield_default(L, 2,
"scale", star_params.scale);
}
getServer(L)->setStars(player, star_params);
lua_pushboolean(L, true);
@ -2106,9 +2103,13 @@ int ObjectRef::l_set_clouds(lua_State *L)
if (player == nullptr)
return 0;
luaL_checktype(L, 2, LUA_TTABLE);
CloudParams cloud_params = player->getCloudParams();
// 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");
@ -2120,7 +2121,7 @@ int ObjectRef::l_set_clouds(lua_State *L)
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.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");
@ -2131,6 +2132,7 @@ int ObjectRef::l_set_clouds(lua_State *L)
cloud_params.speed = new_speed;
}
lua_pop(L, 1);
}
getServer(L)->setClouds(player, cloud_params);
lua_pushboolean(L, true);

@ -68,11 +68,34 @@ struct StarParams
f32 scale;
};
struct CloudParams
{
float density;
video::SColor color_bright;
video::SColor color_ambient;
float thickness;
float height;
v2f speed;
};
// Utility class for setting default sky, sun, moon, stars values:
class SkyboxDefaults
{
public:
const SkyColor getSkyColorDefaults()
static const SkyboxParams getSkyDefaults()
{
SkyboxParams sky;
sky.bgcolor = video::SColor(255, 255, 255, 255);
sky.type = "regular";
sky.clouds = true;
sky.sky_color = getSkyColorDefaults();
sky.fog_sun_tint = video::SColor(255, 244, 125, 29);
sky.fog_moon_tint = video::SColorf(0.5, 0.6, 0.8, 1).toSColor();
sky.fog_tint_type = "default";
return sky;
}
static const SkyColor getSkyColorDefaults()
{
SkyColor sky;
// Horizon colors
@ -87,7 +110,7 @@ public:
return sky;
}
const SunParams getSunDefaults()
static const SunParams getSunDefaults()
{
SunParams sun;
sun.visible = true;
@ -99,7 +122,7 @@ public:
return sun;
}
const MoonParams getMoonDefaults()
static const MoonParams getMoonDefaults()
{
MoonParams moon;
moon.visible = true;
@ -109,7 +132,7 @@ public:
return moon;
}
const StarParams getStarDefaults()
static const StarParams getStarDefaults()
{
StarParams stars;
stars.visible = true;
@ -118,4 +141,16 @@ public:
stars.scale = 1;
return stars;
}
static const CloudParams getCloudDefaults()
{
CloudParams clouds;
clouds.density = 0.4f;
clouds.color_bright = video::SColor(229, 240, 240, 255);
clouds.color_ambient = video::SColor(255, 0, 0, 0);
clouds.thickness = 16.0f;
clouds.height = 120;
clouds.speed = v2f(0.0f, -2.0f);
return clouds;
}
};