]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/lua_api/l_object.cpp
Clouds API: change speed from 'y' to 'z', ColorSpecs in Lua docs (#6164)
[dragonfireclient.git] / src / script / lua_api / l_object.cpp
index 1fa3663ca5b100d2b010fedd23f03a62ceab08e9..7d2eba8e252e28a5e46075ec3227d7b61e512781 100644 (file)
@@ -29,7 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "content_sao.h"
 #include "server.h"
 #include "hud.h"
-#include "scripting_game.h"
+#include "scripting_server.h"
 
 struct EnumString es_HudElementType[] =
 {
@@ -137,8 +137,8 @@ int ObjectRef::l_remove(lua_State *L)
        if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
                return 0;
 
-       const UNORDERED_SET<int> &child_ids = co->getAttachmentChildIds();
-       UNORDERED_SET<int>::const_iterator it;
+       const std::unordered_set<int> &child_ids = co->getAttachmentChildIds();
+       std::unordered_set<int>::const_iterator it;
        for (it = child_ids.begin(); it != child_ids.end(); ++it) {
                // Child can be NULL if it was deleted earlier
                if (ServerActiveObject *child = env->getActiveObject(*it))
@@ -364,7 +364,7 @@ int ObjectRef::l_set_wielded_item(lua_State *L)
        ServerActiveObject *co = getobject(ref);
        if (co == NULL) return 0;
        // Do it
-       ItemStack item = read_item(L, 2, getServer(L));
+       ItemStack item = read_item(L, 2, getServer(L)->idef());
        bool success = co->setWieldedItem(item);
        if (success && co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
                getServer(L)->SendInventory(((PlayerSAO*)co));
@@ -401,7 +401,7 @@ int ObjectRef::l_get_armor_groups(lua_State *L)
 }
 
 // set_physics_override(self, physics_override_speed, physics_override_jump,
-//                      physics_override_gravity, sneak, sneak_glitch)
+//                      physics_override_gravity, sneak, sneak_glitch, new_move)
 int ObjectRef::l_set_physics_override(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
@@ -410,11 +410,18 @@ int ObjectRef::l_set_physics_override(lua_State *L)
        if (co == NULL) return 0;
        // Do it
        if (lua_istable(L, 2)) {
-               co->m_physics_override_speed = getfloatfield_default(L, 2, "speed", co->m_physics_override_speed);
-               co->m_physics_override_jump = getfloatfield_default(L, 2, "jump", co->m_physics_override_jump);
-               co->m_physics_override_gravity = getfloatfield_default(L, 2, "gravity", co->m_physics_override_gravity);
-               co->m_physics_override_sneak = getboolfield_default(L, 2, "sneak", co->m_physics_override_sneak);
-               co->m_physics_override_sneak_glitch = getboolfield_default(L, 2, "sneak_glitch", co->m_physics_override_sneak_glitch);
+               co->m_physics_override_speed = getfloatfield_default(
+                               L, 2, "speed", co->m_physics_override_speed);
+               co->m_physics_override_jump = getfloatfield_default(
+                               L, 2, "jump", co->m_physics_override_jump);
+               co->m_physics_override_gravity = getfloatfield_default(
+                               L, 2, "gravity", co->m_physics_override_gravity);
+               co->m_physics_override_sneak = getboolfield_default(
+                               L, 2, "sneak", co->m_physics_override_sneak);
+               co->m_physics_override_sneak_glitch = getboolfield_default(
+                               L, 2, "sneak_glitch", co->m_physics_override_sneak_glitch);
+               co->m_physics_override_new_move = getboolfield_default(
+                               L, 2, "new_move", co->m_physics_override_new_move);
                co->m_physics_override_sent = false;
        } else {
                // old, non-table format
@@ -454,6 +461,8 @@ int ObjectRef::l_get_physics_override(lua_State *L)
        lua_setfield(L, -2, "sneak");
        lua_pushboolean(L, co->m_physics_override_sneak_glitch);
        lua_setfield(L, -2, "sneak_glitch");
+       lua_pushboolean(L, co->m_physics_override_new_move);
+       lua_setfield(L, -2, "new_move");
        return 1;
 }
 
@@ -710,7 +719,22 @@ int ObjectRef::l_set_detach(lua_State *L)
        ServerActiveObject *co = getobject(ref);
        if (co == NULL)
                return 0;
-       co->detachFromParent();
+
+       int parent_id = 0;
+       std::string bone = "";
+       v3f position;
+       v3f rotation;
+       co->getAttachment(&parent_id, &bone, &position, &rotation);
+       ServerActiveObject *parent = NULL;
+       if (parent_id) {
+               parent = env->getActiveObject(parent_id);
+               co->setAttachment(0, "", position, rotation);
+       } else {
+               co->setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0));
+       }
+       // Do it
+       if (parent != NULL)
+               parent->removeAttachmentChild(co->getId());
        return 0;
 }
 
@@ -724,7 +748,7 @@ int ObjectRef::l_set_properties(lua_State *L)
        ObjectProperties *prop = co->accessObjectProperties();
        if (!prop)
                return 0;
-       read_object_properties(L, 2, prop);
+       read_object_properties(L, 2, prop, getServer(L)->idef());
        co->notifyObjectPropertiesModified();
        return 0;
 }
@@ -1168,6 +1192,46 @@ int ObjectRef::l_get_breath(lua_State *L)
        return 1;
 }
 
+// set_attribute(self, attribute, value)
+int ObjectRef::l_set_attribute(lua_State *L)
+{
+       ObjectRef *ref = checkobject(L, 1);
+       PlayerSAO* co = getplayersao(ref);
+       if (co == NULL) {
+               return 0;
+       }
+
+       std::string attr = luaL_checkstring(L, 2);
+       if (lua_isnil(L, 3)) {
+               co->removeExtendedAttribute(attr);
+       } else {
+               std::string value = luaL_checkstring(L, 3);
+               co->setExtendedAttribute(attr, value);
+       }
+       return 1;
+}
+
+// get_attribute(self, attribute)
+int ObjectRef::l_get_attribute(lua_State *L)
+{
+       ObjectRef *ref = checkobject(L, 1);
+       PlayerSAO* co = getplayersao(ref);
+       if (co == NULL) {
+               return 0;
+       }
+
+       std::string attr = luaL_checkstring(L, 2);
+
+       std::string value = "";
+       if (co->getExtendedAttribute(attr, &value)) {
+               lua_pushstring(L, value.c_str());
+               return 1;
+       }
+
+       return 0;
+}
+
+
 // set_inventory_formspec(self, formspec)
 int ObjectRef::l_set_inventory_formspec(lua_State *L)
 {
@@ -1599,7 +1663,7 @@ int ObjectRef::l_hud_get_hotbar_selected_image(lua_State *L)
        return 1;
 }
 
-// set_sky(self, bgcolor, type, list)
+// set_sky(self, bgcolor, type, list, clouds = true)
 int ObjectRef::l_set_sky(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
@@ -1615,9 +1679,8 @@ int ObjectRef::l_set_sky(lua_State *L)
 
        std::vector<std::string> params;
        if (lua_istable(L, 4)) {
-               int table = lua_gettop(L);
                lua_pushnil(L);
-               while (lua_next(L, table) != 0) {
+               while (lua_next(L, 4) != 0) {
                        // key at index -2 and value at index -1
                        if (lua_isstring(L, -1))
                                params.push_back(lua_tostring(L, -1));
@@ -1631,7 +1694,11 @@ int ObjectRef::l_set_sky(lua_State *L)
        if (type == "skybox" && params.size() != 6)
                throw LuaError("skybox expects 6 textures");
 
-       if (!getServer(L)->setSky(player, bgcolor, type, params))
+       bool clouds = true;
+       if (lua_isboolean(L, 5))
+               clouds = lua_toboolean(L, 5);
+
+       if (!getServer(L)->setSky(player, bgcolor, type, params, clouds))
                return 0;
 
        lua_pushboolean(L, true);
@@ -1649,8 +1716,9 @@ int ObjectRef::l_get_sky(lua_State *L)
        video::SColor bgcolor(255, 255, 255, 255);
        std::string type;
        std::vector<std::string> params;
+       bool clouds;
 
-       player->getSky(&bgcolor, &type, &params);
+       player->getSky(&bgcolor, &type, &params, &clouds);
        type = type == "" ? "regular" : type;
 
        push_ARGB8(L, bgcolor);
@@ -1663,9 +1731,89 @@ int ObjectRef::l_get_sky(lua_State *L)
                lua_rawseti(L, -2, i);
                i++;
        }
-       return 3;
+       lua_pushboolean(L, clouds);
+       return 4;
 }
 
+// set_clouds(self, {density=, color=, ambient=, height=, thickness=, speed=})
+int ObjectRef::l_set_clouds(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+       ObjectRef *ref = checkobject(L, 1);
+       RemotePlayer *player = getplayer(ref);
+       if (!player)
+               return 0;
+       if (!lua_istable(L, 2))
+               return 0;
+
+       CloudParams cloud_params = player->getCloudParams();
+
+       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);
+
+       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_pop(L, 1);
+
+       if (!getServer(L)->setClouds(player, cloud_params.density,
+                       cloud_params.color_bright, cloud_params.color_ambient,
+                       cloud_params.height, cloud_params.thickness,
+                       cloud_params.speed))
+               return 0;
+
+       player->setCloudParams(cloud_params);
+
+       lua_pushboolean(L, true);
+       return 1;
+}
+
+int ObjectRef::l_get_clouds(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+       ObjectRef *ref = checkobject(L, 1);
+       RemotePlayer *player = getplayer(ref);
+       if (!player)
+               return 0;
+       const CloudParams &cloud_params = player->getCloudParams();
+
+       lua_newtable(L);
+       lua_pushnumber(L, cloud_params.density);
+       lua_setfield(L, -2, "density");
+       push_ARGB8(L, cloud_params.color_bright);
+       lua_setfield(L, -2, "color");
+       push_ARGB8(L, cloud_params.color_ambient);
+       lua_setfield(L, -2, "ambient");
+       lua_pushnumber(L, cloud_params.height);
+       lua_setfield(L, -2, "height");
+       lua_pushnumber(L, cloud_params.thickness);
+       lua_setfield(L, -2, "thickness");
+       lua_newtable(L);
+       lua_pushnumber(L, cloud_params.speed.X);
+       lua_setfield(L, -2, "x");
+       lua_pushnumber(L, cloud_params.speed.Y);
+       lua_setfield(L, -2, "y");
+       lua_setfield(L, -2, "speed");
+
+       return 1;
+}
+
+
 // override_day_night_ratio(self, brightness=0...1)
 int ObjectRef::l_override_day_night_ratio(lua_State *L)
 {
@@ -1771,7 +1919,7 @@ void ObjectRef::Register(lua_State *L)
 }
 
 const char ObjectRef::className[] = "ObjectRef";
-const luaL_reg ObjectRef::methods[] = {
+const luaL_Reg ObjectRef::methods[] = {
        // ServerActiveObject
        luamethod(ObjectRef, remove),
        luamethod_aliased(ObjectRef, get_pos, getpos),
@@ -1826,6 +1974,8 @@ const luaL_reg ObjectRef::methods[] = {
        luamethod(ObjectRef, set_look_pitch),
        luamethod(ObjectRef, get_breath),
        luamethod(ObjectRef, set_breath),
+       luamethod(ObjectRef, get_attribute),
+       luamethod(ObjectRef, set_attribute),
        luamethod(ObjectRef, set_inventory_formspec),
        luamethod(ObjectRef, get_inventory_formspec),
        luamethod(ObjectRef, get_player_control),
@@ -1846,6 +1996,8 @@ const luaL_reg ObjectRef::methods[] = {
        luamethod(ObjectRef, hud_get_hotbar_selected_image),
        luamethod(ObjectRef, set_sky),
        luamethod(ObjectRef, get_sky),
+       luamethod(ObjectRef, set_clouds),
+       luamethod(ObjectRef, get_clouds),
        luamethod(ObjectRef, override_day_night_ratio),
        luamethod(ObjectRef, get_day_night_ratio),
        luamethod(ObjectRef, set_local_animation),