]> git.lizzy.rs Git - minetest.git/blobdiff - src/script/lua_api/l_object.cpp
Remove unneeded ObjectRef setter return values (#12179)
[minetest.git] / src / script / lua_api / l_object.cpp
index 07aa3f7c904efeb8242883fc51b276b9e89c45a4..39b19364eb91157d0fa9f7d90f2b90bce923b9ff 100644 (file)
@@ -172,27 +172,11 @@ int ObjectRef::l_punch(lua_State *L)
        float time_from_last_punch = readParam<float>(L, 3, 1000000.0f);
        ToolCapabilities toolcap = read_tool_capabilities(L, 4);
        v3f dir = readParam<v3f>(L, 5, sao->getBasePosition() - puncher->getBasePosition());
-
        dir.normalize();
-       u16 src_original_hp = sao->getHP();
-       u16 dst_origin_hp = puncher->getHP();
 
-       u16 wear = sao->punch(dir, &toolcap, puncher, time_from_last_punch);
+       u32 wear = sao->punch(dir, &toolcap, puncher, time_from_last_punch);
        lua_pushnumber(L, wear);
 
-       // If the punched is a player, and its HP changed
-       if (src_original_hp != sao->getHP() &&
-                       sao->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
-               getServer(L)->SendPlayerHPOrDie((PlayerSAO *)sao,
-                               PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, puncher));
-       }
-
-       // If the puncher is a player, and its HP changed
-       if (dst_origin_hp != puncher->getHP() &&
-                       puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
-               getServer(L)->SendPlayerHPOrDie((PlayerSAO *)puncher,
-                               PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, sao));
-       }
        return 1;
 }
 
@@ -238,8 +222,6 @@ int ObjectRef::l_set_hp(lua_State *L)
        }
 
        sao->setHP(hp, reason);
-       if (sao->getType() == ACTIVEOBJECT_TYPE_PLAYER)
-               getServer(L)->SendPlayerHPOrDie((PlayerSAO *)sao, reason);
        if (reason.hasLuaReference())
                luaL_unref(L, LUA_REGISTRYINDEX, reason.lua_reference);
        return 0;
@@ -438,8 +420,7 @@ int ObjectRef::l_set_local_animation(lua_State *L)
        float frame_speed = readParam<float>(L, 6, 30.0f);
 
        getServer(L)->setLocalPlayerAnimations(player, frames, frame_speed);
-       lua_pushboolean(L, true);
-       return 1;
+       return 0;
 }
 
 // get_local_animation(self)
@@ -482,8 +463,7 @@ int ObjectRef::l_set_eye_offset(lua_State *L)
        offset_third.Y = rangelim(offset_third.Y,-10,15); //1.5*BS
 
        getServer(L)->setPlayerEyeOffset(player, offset_first, offset_third);
-       lua_pushboolean(L, true);
-       return 1;
+       return 0;
 }
 
 // get_eye_offset(self)
@@ -685,6 +665,7 @@ int ObjectRef::l_set_properties(lua_State *L)
                return 0;
 
        read_object_properties(L, 2, sao, prop, getServer(L)->idef());
+       prop->validate();
        sao->notifyObjectPropertiesModified();
        return 0;
 }
@@ -737,12 +718,24 @@ int ObjectRef::l_set_nametag_attributes(lua_State *L)
        }
        lua_pop(L, 1);
 
+       lua_getfield(L, -1, "bgcolor");
+       if (!lua_isnil(L, -1)) {
+               if (lua_toboolean(L, -1)) {
+                       video::SColor color;
+                       if (read_color(L, -1, &color))
+                               prop->nametag_bgcolor = color;
+               } else {
+                       prop->nametag_bgcolor = nullopt;
+               }
+       }
+       lua_pop(L, 1);
+
        std::string nametag = getstringfield_default(L, 2, "text", "");
        prop->nametag = nametag;
 
+       prop->validate();
        sao->notifyObjectPropertiesModified();
-       lua_pushboolean(L, true);
-       return 1;
+       return 0;
 }
 
 // get_nametag_attributes(self)
@@ -758,13 +751,24 @@ int ObjectRef::l_get_nametag_attributes(lua_State *L)
        if (!prop)
                return 0;
 
-       video::SColor color = prop->nametag_color;
-
        lua_newtable(L);
-       push_ARGB8(L, color);
+
+       push_ARGB8(L, prop->nametag_color);
        lua_setfield(L, -2, "color");
+
+       if (prop->nametag_bgcolor) {
+               push_ARGB8(L, prop->nametag_bgcolor.value());
+               lua_setfield(L, -2, "bgcolor");
+       } else {
+               lua_pushboolean(L, false);
+               lua_setfield(L, -2, "bgcolor");
+       }
+
        lua_pushstring(L, prop->nametag.c_str());
        lua_setfield(L, -2, "text");
+
+
+
        return 1;
 }
 
@@ -1109,7 +1113,7 @@ int ObjectRef::l_set_look_vertical(lua_State *L)
        float pitch = readParam<float>(L, 2) * core::RADTODEG;
 
        playersao->setLookPitchAndSend(pitch);
-       return 1;
+       return 0;
 }
 
 // set_look_horizontal(self, radians)
@@ -1124,7 +1128,7 @@ int ObjectRef::l_set_look_horizontal(lua_State *L)
        float yaw = readParam<float>(L, 2) * core::RADTODEG;
 
        playersao->setPlayerYawAndSend(yaw);
-       return 1;
+       return 0;
 }
 
 // DEPRECATED
@@ -1144,7 +1148,7 @@ int ObjectRef::l_set_look_pitch(lua_State *L)
        float pitch = readParam<float>(L, 2) * core::RADTODEG;
 
        playersao->setLookPitchAndSend(pitch);
-       return 1;
+       return 0;
 }
 
 // DEPRECATED
@@ -1164,7 +1168,7 @@ int ObjectRef::l_set_look_yaw(lua_State *L)
        float yaw = readParam<float>(L, 2) * core::RADTODEG;
 
        playersao->setPlayerYawAndSend(yaw);
-       return 1;
+       return 0;
 }
 
 // set_fov(self, degrees, is_multiplier, transition_time)
@@ -1303,8 +1307,7 @@ int ObjectRef::l_set_inventory_formspec(lua_State *L)
 
        player->inventory_formspec = formspec;
        getServer(L)->reportInventoryFormspecModified(player->getName());
-       lua_pushboolean(L, true);
-       return 1;
+       return 0;
 }
 
 // get_inventory_formspec(self) -> formspec
@@ -1335,8 +1338,7 @@ int ObjectRef::l_set_formspec_prepend(lua_State *L)
 
        player->formspec_prepend = formspec;
        getServer(L)->reportFormspecPrependModified(player->getName());
-       lua_pushboolean(L, true);
-       return 1;
+       return 0;
 }
 
 // get_formspec_prepend(self)
@@ -1360,20 +1362,19 @@ int ObjectRef::l_get_player_control(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
        ObjectRef *ref = checkobject(L, 1);
        RemotePlayer *player = getplayer(ref);
-       if (player == nullptr) {
-               lua_pushlstring(L, "", 0);
-               return 1;
-       }
 
-       const PlayerControl &control = player->getPlayerControl();
        lua_newtable(L);
-       lua_pushboolean(L, control.up);
+       if (player == nullptr)
+               return 1;
+       
+       const PlayerControl &control = player->getPlayerControl();
+       lua_pushboolean(L, control.direction_keys & (1 << 0));
        lua_setfield(L, -2, "up");
-       lua_pushboolean(L, control.down);
+       lua_pushboolean(L, control.direction_keys & (1 << 1));
        lua_setfield(L, -2, "down");
-       lua_pushboolean(L, control.left);
+       lua_pushboolean(L, control.direction_keys & (1 << 2));
        lua_setfield(L, -2, "left");
-       lua_pushboolean(L, control.right);
+       lua_pushboolean(L, control.direction_keys & (1 << 3));
        lua_setfield(L, -2, "right");
        lua_pushboolean(L, control.jump);
        lua_setfield(L, -2, "jump");
@@ -1402,11 +1403,25 @@ int ObjectRef::l_get_player_control_bits(lua_State *L)
        ObjectRef *ref = checkobject(L, 1);
        RemotePlayer *player = getplayer(ref);
        if (player == nullptr) {
-               lua_pushlstring(L, "", 0);
+               lua_pushinteger(L, 0);
                return 1;
        }
 
-       lua_pushnumber(L, player->keyPressed);
+       const auto &c = player->getPlayerControl();
+
+       // This is very close to PlayerControl::getKeysPressed() but duplicated
+       // here so the encoding in the API is not inadvertedly changed.
+       u32 keypress_bits =
+               c.direction_keys |
+               ( (u32)(c.jump  & 1) << 4) |
+               ( (u32)(c.aux1  & 1) << 5) |
+               ( (u32)(c.sneak & 1) << 6) |
+               ( (u32)(c.dig   & 1) << 7) |
+               ( (u32)(c.place & 1) << 8) |
+               ( (u32)(c.zoom  & 1) << 9)
+       ;
+
+       lua_pushinteger(L, keypress_bits);
        return 1;
 }
 
@@ -1530,12 +1545,14 @@ int ObjectRef::l_hud_change(lua_State *L)
        if (elem == nullptr)
                return 0;
 
+       HudElementStat stat;
        void *value = nullptr;
-       HudElementStat stat = read_hud_change(L, elem, &value);
+       bool ok = read_hud_change(L, stat, elem, &value);
 
-       getServer(L)->hudChange(player, id, stat, value);
+       if (ok)
+               getServer(L)->hudChange(player, id, stat, value);
 
-       lua_pushboolean(L, true);
+       lua_pushboolean(L, ok);
        return 1;
 }
 
@@ -1581,8 +1598,7 @@ int ObjectRef::l_hud_set_flags(lua_State *L)
        if (!getServer(L)->hudSetFlags(player, flags, mask))
                return 0;
 
-       lua_pushboolean(L, true);
-       return 1;
+       return 0;
 }
 
 // hud_get_flags(self)
@@ -1595,20 +1611,11 @@ int ObjectRef::l_hud_get_flags(lua_State *L)
                return 0;
 
        lua_newtable(L);
-       lua_pushboolean(L, player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE);
-       lua_setfield(L, -2, "hotbar");
-       lua_pushboolean(L, player->hud_flags & HUD_FLAG_HEALTHBAR_VISIBLE);
-       lua_setfield(L, -2, "healthbar");
-       lua_pushboolean(L, player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE);
-       lua_setfield(L, -2, "crosshair");
-       lua_pushboolean(L, player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE);
-       lua_setfield(L, -2, "wielditem");
-       lua_pushboolean(L, player->hud_flags & HUD_FLAG_BREATHBAR_VISIBLE);
-       lua_setfield(L, -2, "breathbar");
-       lua_pushboolean(L, player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE);
-       lua_setfield(L, -2, "minimap");
-       lua_pushboolean(L, player->hud_flags & HUD_FLAG_MINIMAP_RADAR_VISIBLE);
-       lua_setfield(L, -2, "minimap_radar");
+       const EnumString *esp = es_HudBuiltinElement;
+       for (int i = 0; esp[i].str; i++) {
+               lua_pushboolean(L, (player->hud_flags & esp[i].num) != 0);
+               lua_setfield(L, -2, esp[i].str);
+       }
        return 1;
 }
 
@@ -1713,9 +1720,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);
@@ -1739,17 +1748,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)) {
@@ -1797,7 +1800,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 {
@@ -1833,11 +1836,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<std::string>(L, -1));
-                               else
-                                       sky_params.textures.emplace_back("");
+                               // Key at index -2, and value at index -1
+                               sky_params.textures.emplace_back(readParam<std::string>(L, -1));
                                // Remove the value, keep the key for the next iteration
                                lua_pop(L, 1);
                        }
@@ -1853,12 +1853,39 @@ 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;
+       return 0;
+}
+
+static void push_sky_color(lua_State *L, const SkyboxParams &params)
+{
+       lua_newtable(L);
+       if (params.type == "regular") {
+               push_ARGB8(L, params.sky_color.day_sky);
+               lua_setfield(L, -2, "day_sky");
+               push_ARGB8(L, params.sky_color.day_horizon);
+               lua_setfield(L, -2, "day_horizon");
+               push_ARGB8(L, params.sky_color.dawn_sky);
+               lua_setfield(L, -2, "dawn_sky");
+               push_ARGB8(L, params.sky_color.dawn_horizon);
+               lua_setfield(L, -2, "dawn_horizon");
+               push_ARGB8(L, params.sky_color.night_sky);
+               lua_setfield(L, -2, "night_sky");
+               push_ARGB8(L, params.sky_color.night_horizon);
+               lua_setfield(L, -2, "night_horizon");
+               push_ARGB8(L, params.sky_color.indoors);
+               lua_setfield(L, -2, "indoors");
+       }
+       push_ARGB8(L, params.fog_sun_tint);
+       lua_setfield(L, -2, "fog_sun_tint");
+       push_ARGB8(L, params.fog_moon_tint);
+       lua_setfield(L, -2, "fog_moon_tint");
+       lua_pushstring(L, params.fog_tint_type.c_str());
+       lua_setfield(L, -2, "fog_tint_type");
 }
 
-// get_sky(self)
+// get_sky(self, as_table)
 int ObjectRef::l_get_sky(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
@@ -1867,10 +1894,30 @@ int ObjectRef::l_get_sky(lua_State *L)
        if (player == nullptr)
                return 0;
 
-       SkyboxParams skybox_params = player->getSkyParams();
+       const SkyboxParams &skybox_params = player->getSkyParams();
+
+       // handle the deprecated version
+       if (!readParam<bool>(L, 2, false)) {
+               log_deprecated(L, "Deprecated call to get_sky, please check lua_api.txt");
+
+               push_ARGB8(L, skybox_params.bgcolor);
+               lua_pushlstring(L, skybox_params.type.c_str(), skybox_params.type.size());
 
+               lua_newtable(L);
+               s16 i = 1;
+               for (const std::string &texture : skybox_params.textures) {
+                       lua_pushlstring(L, texture.c_str(), texture.size());
+                       lua_rawseti(L, -2, i++);
+               }
+               lua_pushboolean(L, skybox_params.clouds);
+               return 4;
+       }
+
+       lua_newtable(L);
        push_ARGB8(L, skybox_params.bgcolor);
+       lua_setfield(L, -2, "base_color");
        lua_pushlstring(L, skybox_params.type.c_str(), skybox_params.type.size());
+       lua_setfield(L, -2, "type");
 
        lua_newtable(L);
        s16 i = 1;
@@ -1878,44 +1925,30 @@ int ObjectRef::l_get_sky(lua_State *L)
                lua_pushlstring(L, texture.c_str(), texture.size());
                lua_rawseti(L, -2, i++);
        }
+       lua_setfield(L, -2, "textures");
        lua_pushboolean(L, skybox_params.clouds);
-       return 4;
+       lua_setfield(L, -2, "clouds");
+
+       push_sky_color(L, skybox_params);
+       lua_setfield(L, -2, "sky_color");
+       return 1;
 }
 
+// DEPRECATED
 // get_sky_color(self)
 int ObjectRef::l_get_sky_color(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
+
+       log_deprecated(L, "Deprecated call to get_sky_color, use get_sky instead");
+
        ObjectRef *ref = checkobject(L, 1);
        RemotePlayer *player = getplayer(ref);
        if (player == nullptr)
                return 0;
 
        const SkyboxParams &skybox_params = player->getSkyParams();
-
-       lua_newtable(L);
-       if (skybox_params.type == "regular") {
-               push_ARGB8(L, skybox_params.sky_color.day_sky);
-               lua_setfield(L, -2, "day_sky");
-               push_ARGB8(L, skybox_params.sky_color.day_horizon);
-               lua_setfield(L, -2, "day_horizon");
-               push_ARGB8(L, skybox_params.sky_color.dawn_sky);
-               lua_setfield(L, -2, "dawn_sky");
-               push_ARGB8(L, skybox_params.sky_color.dawn_horizon);
-               lua_setfield(L, -2, "dawn_horizon");
-               push_ARGB8(L, skybox_params.sky_color.night_sky);
-               lua_setfield(L, -2, "night_sky");
-               push_ARGB8(L, skybox_params.sky_color.night_horizon);
-               lua_setfield(L, -2, "night_horizon");
-               push_ARGB8(L, skybox_params.sky_color.indoors);
-               lua_setfield(L, -2, "indoors");
-       }
-       push_ARGB8(L, skybox_params.fog_sun_tint);
-       lua_setfield(L, -2, "fog_sun_tint");
-       push_ARGB8(L, skybox_params.fog_moon_tint);
-       lua_setfield(L, -2, "fog_moon_tint");
-       lua_pushstring(L, skybox_params.fog_tint_type.c_str());
-       lua_setfield(L, -2, "fog_tint_type");
+       push_sky_color(L, skybox_params);
        return 1;
 }
 
@@ -1928,25 +1961,23 @@ 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);
-       return 1;
+       return 0;
 }
 
 //get_sun(self)
@@ -1985,21 +2016,21 @@ 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);
-       return 1;
+       return 0;
 }
 
 // get_moon(self)
@@ -2034,25 +2065,27 @@ 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);
-       return 1;
+       return 0;
 }
 
 // get_stars(self)
@@ -2087,35 +2120,39 @@ 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);
-       return 1;
+       return 0;
 }
 
 int ObjectRef::l_get_clouds(lua_State *L)
@@ -2169,8 +2206,7 @@ int ObjectRef::l_override_day_night_ratio(lua_State *L)
        }
 
        getServer(L)->overrideDayNightRatio(player, do_override, ratio);
-       lua_pushboolean(L, true);
-       return 1;
+       return 0;
 }
 
 // get_day_night_ratio(self)
@@ -2247,6 +2283,46 @@ int ObjectRef::l_set_minimap_modes(lua_State *L)
        return 0;
 }
 
+// set_lighting(self, lighting)
+int ObjectRef::l_set_lighting(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+       ObjectRef *ref = checkobject(L, 1);
+       RemotePlayer *player = getplayer(ref);
+       if (player == nullptr)
+               return 0;
+
+       luaL_checktype(L, 2, LUA_TTABLE);
+       Lighting lighting = player->getLighting();
+       lua_getfield(L, 2, "shadows");
+       if (lua_istable(L, -1)) {
+               lighting.shadow_intensity = getfloatfield_default(L, -1, "intensity",    lighting.shadow_intensity);
+       }
+       lua_pop(L, -1);
+
+       getServer(L)->setLighting(player, lighting);
+       return 0;
+}
+
+// get_lighting(self)
+int ObjectRef::l_get_lighting(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+       ObjectRef *ref = checkobject(L, 1);
+       RemotePlayer *player = getplayer(ref);
+       if (player == nullptr)
+               return 0;
+
+       const Lighting &lighting = player->getLighting();
+
+       lua_newtable(L); // result
+       lua_newtable(L); // "shadows"
+       lua_pushnumber(L, lighting.shadow_intensity);
+       lua_setfield(L, -2, "intensity");
+       lua_setfield(L, -2, "shadows");
+       return 1;
+}
+
 ObjectRef::ObjectRef(ServerActiveObject *object):
        m_object(object)
 {}
@@ -2288,7 +2364,7 @@ void ObjectRef::Register(lua_State *L)
 
        lua_pop(L, 1);  // drop metatable
 
-       luaL_openlib(L, 0, methods, 0);  // fill methodtable
+       luaL_register(L, nullptr, methods);  // fill methodtable
        lua_pop(L, 1);  // drop methodtable
 }
 
@@ -2400,5 +2476,7 @@ luaL_Reg ObjectRef::methods[] = {
        luamethod(ObjectRef, get_eye_offset),
        luamethod(ObjectRef, send_mapblock),
        luamethod(ObjectRef, set_minimap_modes),
+       luamethod(ObjectRef, set_lighting),
+       luamethod(ObjectRef, get_lighting),
        {0,0}
 };