]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/lua_api/l_object.cpp
Add ItemStack:get_description() to get tooltip (#8847)
[dragonfireclient.git] / src / script / lua_api / l_object.cpp
index f87c02a6d5d70cfdae3beff85c12fcc6df8a41f0..be2172f1b853d5cd4a80e37bb5ef4cdce244d43c 100644 (file)
@@ -156,7 +156,7 @@ int ObjectRef::l_move_to(lua_State *L)
        // pos
        v3f pos = checkFloatPos(L, 2);
        // continuous
-       bool continuous = lua_toboolean(L, 3);
+       bool continuous = readParam<bool>(L, 3);
        // Do it
        co->moveTo(pos, continuous);
        return 0;
@@ -170,8 +170,8 @@ int ObjectRef::l_punch(lua_State *L)
        ObjectRef *puncher_ref = checkobject(L, 2);
        ServerActiveObject *co = getobject(ref);
        ServerActiveObject *puncher = getobject(puncher_ref);
-       if (co == NULL) return 0;
-       if (puncher == NULL) return 0;
+       if (!co || !puncher)
+               return 0;
        v3f dir;
        if (lua_type(L, 5) != LUA_TTABLE)
                dir = co->getBasePosition() - puncher->getBasePosition();
@@ -183,8 +183,8 @@ int ObjectRef::l_punch(lua_State *L)
        ToolCapabilities toolcap = read_tool_capabilities(L, 4);
        dir.normalize();
 
-       s16 src_original_hp = co->getHP();
-       s16 dst_origin_hp = puncher->getHP();
+       u16 src_original_hp = co->getHP();
+       u16 dst_origin_hp = puncher->getHP();
 
        // Do it
        co->punch(dir, &toolcap, puncher, time_from_last_punch);
@@ -192,7 +192,8 @@ int ObjectRef::l_punch(lua_State *L)
        // If the punched is a player, and its HP changed
        if (src_original_hp != co->getHP() &&
                        co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
-               getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, puncher));
+               getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co,
+                               PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, puncher));
        }
 
        // If the puncher is a player, and its HP changed
@@ -243,7 +244,8 @@ int ObjectRef::l_set_hp(lua_State *L)
                lua_pushvalue(L, 3);
 
                lua_getfield(L, -1, "type");
-               if (lua_isstring(L, -1) && !reason.setTypeFromString(lua_tostring(L, -1))) {
+               if (lua_isstring(L, -1) &&
+                               !reason.setTypeFromString(readParam<std::string>(L, -1))) {
                        errorstream << "Bad type given!" << std::endl;
                }
                lua_pop(L, 1);
@@ -256,6 +258,9 @@ int ObjectRef::l_set_hp(lua_State *L)
        if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
                getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, reason);
 
+       if (reason.hasLuaReference())
+               luaL_unref(L, LUA_REGISTRYINDEX, reason.lua_reference);
+
        // Return
        return 0;
 }
@@ -303,8 +308,9 @@ int ObjectRef::l_get_wield_list(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
        ObjectRef *ref = checkobject(L, 1);
        ServerActiveObject *co = getobject(ref);
-       if (co == NULL) return 0;
-       // Do it
+       if (!co)
+               return 0;
+
        lua_pushstring(L, co->getWieldList().c_str());
        return 1;
 }
@@ -315,8 +321,9 @@ int ObjectRef::l_get_wield_index(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
        ObjectRef *ref = checkobject(L, 1);
        ServerActiveObject *co = getobject(ref);
-       if (co == NULL) return 0;
-       // Do it
+       if (!co)
+               return 0;
+
        lua_pushinteger(L, co->getWieldIndex() + 1);
        return 1;
 }
@@ -327,12 +334,12 @@ int ObjectRef::l_get_wielded_item(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
        ObjectRef *ref = checkobject(L, 1);
        ServerActiveObject *co = getobject(ref);
-       if (co == NULL) {
+       if (!co) {
                // Empty ItemStack
                LuaItemStack::create(L, ItemStack());
                return 1;
        }
-       // Do it
+
        LuaItemStack::create(L, co->getWieldedItem());
        return 1;
 }
@@ -457,7 +464,7 @@ int ObjectRef::l_set_animation(lua_State *L)
        // Do it
        v2f frames = v2f(1, 1);
        if (!lua_isnil(L, 2))
-               frames = read_v2f(L, 2);
+               frames = readParam<v2f>(L, 2);
        float frame_speed = 15;
        if (!lua_isnil(L, 3))
                frame_speed = lua_tonumber(L, 3);
@@ -466,7 +473,7 @@ int ObjectRef::l_set_animation(lua_State *L)
                frame_blend = lua_tonumber(L, 4);
        bool frame_loop = true;
        if (lua_isboolean(L, 5))
-               frame_loop = lua_toboolean(L, 5);
+               frame_loop = readParam<bool>(L, 5);
        co->setAnimation(frames, frame_speed, frame_blend, frame_loop);
        return 0;
 }
@@ -579,6 +586,24 @@ int ObjectRef::l_get_eye_offset(lua_State *L)
        return 2;
 }
 
+// send_mapblock(self, pos)
+int ObjectRef::l_send_mapblock(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+       ObjectRef *ref = checkobject(L, 1);
+
+       RemotePlayer *player = getplayer(ref);
+       if (!player)
+               return 0;
+       v3s16 p = read_v3s16(L, 2);
+
+       session_t peer_id = player->getPeerId();
+       bool r = getServer(L)->SendBlock(peer_id, p);
+
+       lua_pushboolean(L, r);
+       return 1;
+}
+
 // set_animation_frame_speed(self, frame_speed)
 int ObjectRef::l_set_animation_frame_speed(lua_State *L)
 {
@@ -609,7 +634,7 @@ int ObjectRef::l_set_bone_position(lua_State *L)
        // Do it
        std::string bone;
        if (!lua_isnil(L, 2))
-               bone = lua_tostring(L, 2);
+               bone = readParam<std::string>(L, 2);
        v3f position = v3f(0, 0, 0);
        if (!lua_isnil(L, 3))
                position = check_v3f(L, 3);
@@ -631,7 +656,7 @@ int ObjectRef::l_get_bone_position(lua_State *L)
        // Do it
        std::string bone;
        if (!lua_isnil(L, 2))
-               bone = lua_tostring(L, 2);
+               bone = readParam<std::string>(L, 2);
 
        v3f position = v3f(0, 0, 0);
        v3f rotation = v3f(0, 0, 0);
@@ -668,7 +693,7 @@ int ObjectRef::l_set_attach(lua_State *L)
 
        bone = "";
        if (!lua_isnil(L, 3))
-               bone = lua_tostring(L, 3);
+               bone = readParam<std::string>(L, 3);
        position = v3f(0, 0, 0);
        if (!lua_isnil(L, 4))
                position = read_v3f(L, 4);
@@ -727,17 +752,14 @@ int ObjectRef::l_set_properties(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
        ObjectRef *ref = checkobject(L, 1);
        ServerActiveObject *co = getobject(ref);
-       if (co == NULL) return 0;
+       if (!co)
+               return 0;
+
        ObjectProperties *prop = co->accessObjectProperties();
        if (!prop)
                return 0;
-       read_object_properties(L, 2, prop, getServer(L)->idef());
-       if (prop->hp_max < co->getHP()) {
-               PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP);
-               co->setHP(prop->hp_max, reason);
-               if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
-                       getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, reason);
-       }
+
+       read_object_properties(L, 2, co, prop, getServer(L)->idef());
        co->notifyObjectPropertiesModified();
        return 0;
 }
@@ -888,19 +910,51 @@ int ObjectRef::l_get_acceleration(lua_State *L)
        return 1;
 }
 
+// set_rotation(self, {x=num, y=num, z=num})
+// Each 'num' is in radians
+int ObjectRef::l_set_rotation(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+       ObjectRef *ref = checkobject(L, 1);
+       LuaEntitySAO *co = getluaobject(ref);
+       if (!co)
+               return 0;
+
+       v3f rotation = check_v3f(L, 2) * core::RADTODEG;
+       co->setRotation(rotation);
+       return 0;
+}
+
+// get_rotation(self)
+// returns: {x=num, y=num, z=num}
+// Each 'num' is in radians
+int ObjectRef::l_get_rotation(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+       ObjectRef *ref = checkobject(L, 1);
+       LuaEntitySAO *co = getluaobject(ref);
+       if (!co)
+               return 0;
+
+       lua_newtable(L);
+       v3f rotation = co->getRotation() * core::DEGTORAD;
+       push_v3f(L, rotation);
+       return 1;
+}
+
 // set_yaw(self, radians)
 int ObjectRef::l_set_yaw(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
        ObjectRef *ref = checkobject(L, 1);
        LuaEntitySAO *co = getluaobject(ref);
+
        if (co == NULL) return 0;
        if (isNaN(L, 2))
                throw LuaError("ObjectRef::set_yaw: NaN value is not allowed.");
 
        float yaw = readParam<float>(L, 2) * core::RADTODEG;
-       // Do it
-       co->setYaw(yaw);
+       co->setRotation(v3f(0, yaw, 0));
        return 0;
 }
 
@@ -910,9 +964,10 @@ int ObjectRef::l_get_yaw(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
        ObjectRef *ref = checkobject(L, 1);
        LuaEntitySAO *co = getluaobject(ref);
-       if (co == NULL) return 0;
-       // Do it
-       float yaw = co->getYaw() * core::DEGTORAD;
+       if (!co)
+               return 0;
+
+       float yaw = co->getRotation().Y * core::DEGTORAD;
        lua_pushnumber(L, yaw);
        return 1;
 }
@@ -954,7 +1009,7 @@ int ObjectRef::l_set_sprite(lua_State *L)
        // Do it
        v2s16 p(0,0);
        if (!lua_isnil(L, 2))
-               p = read_v2s16(L, 2);
+               p = readParam<v2s16>(L, 2);
        int num_frames = 1;
        if (!lua_isnil(L, 3))
                num_frames = lua_tonumber(L, 3);
@@ -963,7 +1018,7 @@ int ObjectRef::l_set_sprite(lua_State *L)
                framelength = lua_tonumber(L, 4);
        bool select_horiz_by_yawpitch = false;
        if (!lua_isnil(L, 5))
-               select_horiz_by_yawpitch = lua_toboolean(L, 5);
+               select_horiz_by_yawpitch = readParam<bool>(L, 5);
        co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
        return 0;
 }
@@ -1037,6 +1092,27 @@ int ObjectRef::l_get_player_velocity(lua_State *L)
        return 1;
 }
 
+// add_player_velocity(self, {x=num, y=num, z=num})
+int ObjectRef::l_add_player_velocity(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+       ObjectRef *ref = checkobject(L, 1);
+       v3f vel = checkFloatPos(L, 2);
+
+       RemotePlayer *player = getplayer(ref);
+       PlayerSAO *co = getplayersao(ref);
+       if (!player || !co)
+               return 0;
+
+       session_t peer_id = player->getPeerId();
+       if (peer_id == PEER_ID_INEXISTENT)
+               return 0;
+       // Do it
+       co->setMaxSpeedOverride(vel);
+       getServer(L)->SendPlayerSpeed(peer_id, vel);
+       return 0;
+}
+
 // get_look_dir(self)
 int ObjectRef::l_get_look_dir(lua_State *L)
 {
@@ -1045,7 +1121,7 @@ int ObjectRef::l_get_look_dir(lua_State *L)
        PlayerSAO* co = getplayersao(ref);
        if (co == NULL) return 0;
        // Do it
-       float pitch = co->getRadPitchDep();
+       float pitch = co->getRadLookPitchDep();
        float yaw = co->getRadYawDep();
        v3f v(std::cos(pitch) * std::cos(yaw), std::sin(pitch), std::cos(pitch) *
                std::sin(yaw));
@@ -1066,7 +1142,7 @@ int ObjectRef::l_get_look_pitch(lua_State *L)
        PlayerSAO* co = getplayersao(ref);
        if (co == NULL) return 0;
        // Do it
-       lua_pushnumber(L, co->getRadPitchDep());
+       lua_pushnumber(L, co->getRadLookPitchDep());
        return 1;
 }
 
@@ -1095,7 +1171,7 @@ int ObjectRef::l_get_look_vertical(lua_State *L)
        PlayerSAO* co = getplayersao(ref);
        if (co == NULL) return 0;
        // Do it
-       lua_pushnumber(L, co->getRadPitch());
+       lua_pushnumber(L, co->getRadLookPitch());
        return 1;
 }
 
@@ -1107,7 +1183,7 @@ int ObjectRef::l_get_look_horizontal(lua_State *L)
        PlayerSAO* co = getplayersao(ref);
        if (co == NULL) return 0;
        // Do it
-       lua_pushnumber(L, co->getRadYaw());
+       lua_pushnumber(L, co->getRadRotation().Y);
        return 1;
 }
 
@@ -1120,7 +1196,7 @@ int ObjectRef::l_set_look_vertical(lua_State *L)
        if (co == NULL) return 0;
        float pitch = readParam<float>(L, 2) * core::RADTODEG;
        // Do it
-       co->setPitchAndSend(pitch);
+       co->setLookPitchAndSend(pitch);
        return 1;
 }
 
@@ -1133,7 +1209,7 @@ int ObjectRef::l_set_look_horizontal(lua_State *L)
        if (co == NULL) return 0;
        float yaw = readParam<float>(L, 2) * core::RADTODEG;
        // Do it
-       co->setYawAndSend(yaw);
+       co->setPlayerYawAndSend(yaw);
        return 1;
 }
 
@@ -1151,7 +1227,7 @@ int ObjectRef::l_set_look_pitch(lua_State *L)
        if (co == NULL) return 0;
        float pitch = readParam<float>(L, 2) * core::RADTODEG;
        // Do it
-       co->setPitchAndSend(pitch);
+       co->setLookPitchAndSend(pitch);
        return 1;
 }
 
@@ -1169,7 +1245,7 @@ int ObjectRef::l_set_look_yaw(lua_State *L)
        if (co == NULL) return 0;
        float yaw = readParam<float>(L, 2) * core::RADTODEG;
        // Do it
-       co->setYawAndSend(yaw);
+       co->setPlayerYawAndSend(yaw);
        return 1;
 }
 
@@ -1202,6 +1278,9 @@ int ObjectRef::l_get_breath(lua_State *L)
 // set_attribute(self, attribute, value)
 int ObjectRef::l_set_attribute(lua_State *L)
 {
+       log_deprecated(L,
+               "Deprecated call to set_attribute, use MetaDataRef methods instead.");
+
        ObjectRef *ref = checkobject(L, 1);
        PlayerSAO* co = getplayersao(ref);
        if (co == NULL)
@@ -1220,6 +1299,9 @@ int ObjectRef::l_set_attribute(lua_State *L)
 // get_attribute(self, attribute)
 int ObjectRef::l_get_attribute(lua_State *L)
 {
+       log_deprecated(L,
+               "Deprecated call to get_attribute, use MetaDataRef methods instead.");
+
        ObjectRef *ref = checkobject(L, 1);
        PlayerSAO* co = getplayersao(ref);
        if (co == NULL)
@@ -1536,7 +1618,7 @@ int ObjectRef::l_hud_set_hotbar_image(lua_State *L)
        if (player == NULL)
                return 0;
 
-       std::string name = lua_tostring(L, 2);
+       std::string name = readParam<std::string>(L, 2);
 
        getServer(L)->hudSetHotbarImage(player, name);
        return 1;
@@ -1565,7 +1647,7 @@ int ObjectRef::l_hud_set_hotbar_selected_image(lua_State *L)
        if (player == NULL)
                return 0;
 
-       std::string name = lua_tostring(L, 2);
+       std::string name = readParam<std::string>(L, 2);
 
        getServer(L)->hudSetHotbarSelectedImage(player, name);
        return 1;
@@ -1605,7 +1687,7 @@ int ObjectRef::l_set_sky(lua_State *L)
                while (lua_next(L, 4) != 0) {
                        // key at index -2 and value at index -1
                        if (lua_isstring(L, -1))
-                               params.emplace_back(lua_tostring(L, -1));
+                               params.emplace_back(readParam<std::string>(L, -1));
                        else
                                params.emplace_back("");
                        // removes value, keeps key for next iteration
@@ -1618,7 +1700,7 @@ int ObjectRef::l_set_sky(lua_State *L)
 
        bool clouds = true;
        if (lua_isboolean(L, 5))
-               clouds = lua_toboolean(L, 5);
+               clouds = readParam<bool>(L, 5);
 
        getServer(L)->setSky(player, bgcolor, type, params, clouds);
        lua_pushboolean(L, true);
@@ -1647,8 +1729,7 @@ int ObjectRef::l_get_sky(lua_State *L)
        s16 i = 1;
        for (const std::string &param : params) {
                lua_pushlstring(L, param.c_str(), param.size());
-               lua_rawseti(L, -2, i);
-               i++;
+               lua_rawseti(L, -2, i++);
        }
        lua_pushboolean(L, clouds);
        return 4;
@@ -1814,6 +1895,7 @@ void ObjectRef::Register(lua_State *L)
 
        lua_pop(L, 1);  // drop metatable
 
+       markAliasDeprecated(methods);
        luaL_openlib(L, 0, methods, 0);  // fill methodtable
        lua_pop(L, 1);  // drop methodtable
 
@@ -1822,7 +1904,7 @@ void ObjectRef::Register(lua_State *L)
 }
 
 const char ObjectRef::className[] = "ObjectRef";
-const luaL_Reg ObjectRef::methods[] = {
+luaL_Reg ObjectRef::methods[] = {
        // ServerActiveObject
        luamethod(ObjectRef, remove),
        luamethod_aliased(ObjectRef, get_pos, getpos),
@@ -1859,6 +1941,8 @@ const luaL_Reg ObjectRef::methods[] = {
        luamethod_aliased(ObjectRef, get_acceleration, getacceleration),
        luamethod_aliased(ObjectRef, set_yaw, setyaw),
        luamethod_aliased(ObjectRef, get_yaw, getyaw),
+       luamethod(ObjectRef, set_rotation),
+       luamethod(ObjectRef, get_rotation),
        luamethod_aliased(ObjectRef, set_texture_mod, settexturemod),
        luamethod_aliased(ObjectRef, set_sprite, setsprite),
        luamethod(ObjectRef, get_entity_name),
@@ -1868,6 +1952,7 @@ const luaL_Reg ObjectRef::methods[] = {
        luamethod(ObjectRef, is_player_connected),
        luamethod(ObjectRef, get_player_name),
        luamethod(ObjectRef, get_player_velocity),
+       luamethod(ObjectRef, add_player_velocity),
        luamethod(ObjectRef, get_look_dir),
        luamethod(ObjectRef, get_look_pitch),
        luamethod(ObjectRef, get_look_yaw),
@@ -1912,5 +1997,6 @@ const luaL_Reg ObjectRef::methods[] = {
        luamethod(ObjectRef, get_local_animation),
        luamethod(ObjectRef, set_eye_offset),
        luamethod(ObjectRef, get_eye_offset),
+       luamethod(ObjectRef, send_mapblock),
        {0,0}
 };