]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Add ClientObjectRef:set_properties
authorElias Fleckenstein <eliasfleckenstein@web.de>
Mon, 10 May 2021 14:07:31 +0000 (16:07 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Mon, 10 May 2021 14:07:31 +0000 (16:07 +0200)
doc/client_lua_api.txt
src/client/content_cao.cpp
src/client/content_cao.h
src/script/common/c_content.cpp
src/script/lua_api/l_clientobject.cpp
src/script/lua_api/l_clientobject.h

index bc78d5dda80e3f6be4cdb3056f3005a8bb9bdd1d..8b955db315ecc94441ab95e6e6b575a6749f081c 100644 (file)
@@ -1414,6 +1414,7 @@ This is basically a reference to a C++ `GenericCAO`.
 * `get_nametag()`: returns the nametag (deprecated, use get_properties().nametag instead)
 * `get_item_textures()`: returns the textures (deprecated, use get_properties().textures instead)
 * `get_max_hp()`: returns the maximum heath (deprecated, use get_properties().hp_max instead)
+* `set_properties(object property table)`
 * `get_properties()`: returns object property table
 * `punch()`: punches the object
 * `rightclick()`: rightclicks the object
index 36eb55597e38e105a4e3cb8c02c48ae6a359d5c8..27ba0b6ddd891041f8031d9804ed49ea854ae918 100644 (file)
@@ -831,13 +831,13 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
 }
 
 void GenericCAO::updateLight(u32 day_night_ratio)
-{              
+{
        if (m_glow < 0)
                return;
 
        u8 light_at_pos = 0;
        bool pos_ok = false;
-               
+
        v3s16 pos[3];
        u16 npos = getLightPosition(pos);
        for (u16 i = 0; i < npos; i++) {
@@ -1629,6 +1629,57 @@ bool GenericCAO::visualExpiryRequired(const ObjectProperties &new_) const
                (uses_legacy_texture && old.textures != new_.textures);
 }
 
+void GenericCAO::setProperties(ObjectProperties newprops)
+{
+       // Check what exactly changed
+       bool expire_visuals = visualExpiryRequired(newprops);
+       bool textures_changed = m_prop.textures != newprops.textures;
+
+       // Apply changes
+       m_prop = std::move(newprops);
+
+       m_selection_box = m_prop.selectionbox;
+       m_selection_box.MinEdge *= BS;
+       m_selection_box.MaxEdge *= BS;
+
+       m_tx_size.X = 1.0f / m_prop.spritediv.X;
+       m_tx_size.Y = 1.0f / m_prop.spritediv.Y;
+
+       if(!m_initial_tx_basepos_set){
+               m_initial_tx_basepos_set = true;
+               m_tx_basepos = m_prop.initial_sprite_basepos;
+       }
+       if (m_is_local_player) {
+               LocalPlayer *player = m_env->getLocalPlayer();
+               player->makes_footstep_sound = m_prop.makes_footstep_sound;
+               aabb3f collision_box = m_prop.collisionbox;
+               collision_box.MinEdge *= BS;
+               collision_box.MaxEdge *= BS;
+               player->setCollisionbox(collision_box);
+               player->setEyeHeight(m_prop.eye_height);
+               player->setZoomFOV(m_prop.zoom_fov);
+       }
+
+       if ((m_is_player && !m_is_local_player) && m_prop.nametag.empty())
+               m_prop.nametag = m_name;
+       if (m_is_local_player)
+               m_prop.show_on_minimap = false;
+
+       if (expire_visuals) {
+               expireVisuals();
+       } else {
+               infostream << "GenericCAO: properties updated but expiring visuals"
+                       << " not necessary" << std::endl;
+               if (textures_changed) {
+                       // don't update while punch texture modifier is active
+                       if (m_reset_textures_timer < 0)
+                               updateTextures(m_current_texture_modifier);
+               }
+               updateNametag();
+               updateMarker();
+       }
+}
+
 void GenericCAO::processMessage(const std::string &data)
 {
        //infostream<<"GenericCAO: Got message"<<std::endl;
@@ -1640,54 +1691,8 @@ void GenericCAO::processMessage(const std::string &data)
                newprops.show_on_minimap = m_is_player; // default
 
                newprops.deSerialize(is);
+               setProperties(newprops);
 
-               // Check what exactly changed
-               bool expire_visuals = visualExpiryRequired(newprops);
-               bool textures_changed = m_prop.textures != newprops.textures;
-
-               // Apply changes
-               m_prop = std::move(newprops);
-
-               m_selection_box = m_prop.selectionbox;
-               m_selection_box.MinEdge *= BS;
-               m_selection_box.MaxEdge *= BS;
-
-               m_tx_size.X = 1.0f / m_prop.spritediv.X;
-               m_tx_size.Y = 1.0f / m_prop.spritediv.Y;
-
-               if(!m_initial_tx_basepos_set){
-                       m_initial_tx_basepos_set = true;
-                       m_tx_basepos = m_prop.initial_sprite_basepos;
-               }
-               if (m_is_local_player) {
-                       LocalPlayer *player = m_env->getLocalPlayer();
-                       player->makes_footstep_sound = m_prop.makes_footstep_sound;
-                       aabb3f collision_box = m_prop.collisionbox;
-                       collision_box.MinEdge *= BS;
-                       collision_box.MaxEdge *= BS;
-                       player->setCollisionbox(collision_box);
-                       player->setEyeHeight(m_prop.eye_height);
-                       player->setZoomFOV(m_prop.zoom_fov);
-               }
-
-               if ((m_is_player && !m_is_local_player) && m_prop.nametag.empty())
-                       m_prop.nametag = m_name;
-               if (m_is_local_player)
-                       m_prop.show_on_minimap = false;
-
-               if (expire_visuals) {
-                       expireVisuals();
-               } else {
-                       infostream << "GenericCAO: properties updated but expiring visuals"
-                               << " not necessary" << std::endl;
-                       if (textures_changed) {
-                               // don't update while punch texture modifier is active
-                               if (m_reset_textures_timer < 0)
-                                       updateTextures(m_current_texture_modifier);
-                       }
-                       updateNametag();
-                       updateMarker();
-               }
        } else if (cmd == AO_CMD_UPDATE_POSITION) {
                // Not sent by the server if this object is an attachment.
                // We might however get here if the server notices the object being detached before the client.
@@ -1752,10 +1757,10 @@ void GenericCAO::processMessage(const std::string &data)
                if(m_is_local_player)
                {
                        Client *client = m_env->getGameDef();
-                       
+
                        if (client->modsLoaded() && client->getScript()->on_recieve_physics_override(override_speed, override_jump, override_gravity, sneak, sneak_glitch, new_move))
                                return;
-                       
+
                        LocalPlayer *player = m_env->getLocalPlayer();
                        player->physics_override_speed = override_speed;
                        player->physics_override_jump = override_jump;
index 09c26bd9c12d69db4c539173dce24e2380094ce3..360c30995137de1e54316c970c42dbd7c72c2830 100644 (file)
@@ -181,7 +181,7 @@ class GenericCAO : public ClientActiveObject
        {
                return m_velocity;
        }
-       
+
        inline const u16 getHp() const
        {
                return m_hp;
@@ -307,13 +307,15 @@ class GenericCAO : public ClientActiveObject
        {
                return m_prop.infotext;
        }
-       
+
        float m_waiting_for_reattach;
-       
+
        ObjectProperties *getProperties()
        {
                return &m_prop;
        }
 
+       void setProperties(ObjectProperties newprops);
+
        void updateMeshCulling();
 };
index eeaf69da1a420f76d17fdd854d2a781f20e308f9..8543b70ced4a810ca5ad71972235f8361eb2a642 100644 (file)
@@ -200,7 +200,7 @@ void read_object_properties(lua_State *L, int index,
        if (getintfield(L, -1, "hp_max", hp_max)) {
                prop->hp_max = (u16)rangelim(hp_max, 0, U16_MAX);
 
-               if (prop->hp_max < sao->getHP()) {
+               if (sao && prop->hp_max < sao->getHP()) {
                        PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP);
                        sao->setHP(prop->hp_max, reason);
                        if (sao->getType() == ACTIVEOBJECT_TYPE_PLAYER)
@@ -209,7 +209,7 @@ void read_object_properties(lua_State *L, int index,
        }
 
        if (getintfield(L, -1, "breath_max", prop->breath_max)) {
-               if (sao->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
+               if (sao && sao->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
                        PlayerSAO *player = (PlayerSAO *)sao;
                        if (prop->breath_max < player->getBreath())
                                player->setBreath(prop->breath_max);
index 7b9c4c3fa04b2eaa7bc3eb20460cb2f2d0890294..0147fd48b1636aba6adf1ef9ba8c796c191535f6 100644 (file)
@@ -160,6 +160,16 @@ int ClientObjectRef::l_get_properties(lua_State *L)
        return 1;
 }
 
+int ClientObjectRef::l_set_properties(lua_State *L)
+{
+       ClientObjectRef *ref = checkobject(L, 1);
+       GenericCAO *gcao = get_generic_cao(ref, L);
+       ObjectProperties prop = *gcao->getProperties();
+       read_object_properties(L, 2, nullptr, &prop, getClient(L)->idef());
+       gcao->setProperties(prop);
+       return 1;
+}
+
 int ClientObjectRef::l_get_hp(lua_State *L)
 {
        ClientObjectRef *ref = checkobject(L, 1);
@@ -259,6 +269,7 @@ luaL_Reg ClientObjectRef::methods[] = {luamethod(ClientObjectRef, get_pos),
                luamethod(ClientObjectRef, get_nametag),
                luamethod(ClientObjectRef, get_item_textures),
                luamethod(ClientObjectRef, get_properties),
+               luamethod(ClientObjectRef, set_properties),
                luamethod(ClientObjectRef, get_hp),
                luamethod(ClientObjectRef, get_max_hp), luamethod(ClientObjectRef, punch),
                luamethod(ClientObjectRef, rightclick), {0, 0}};
index 160b6c4fe5e8fcb17ead8bba7667e0c48e122cf0..22d2f4a1cb00623365831c40982eb9a9a1aba922 100644 (file)
@@ -75,12 +75,15 @@ class ClientObjectRef : public ModApiBase
        // get_nametag(self)
        static int l_get_nametag(lua_State *L);
 
-       // get_textures(self)
+       // get_item_textures(self)
        static int l_get_item_textures(lua_State *L);
 
        // get_properties(self)
        static int l_get_properties(lua_State *L);
 
+       // set_properties(self, properties)
+       static int l_set_properties(lua_State *L);
+
        // get_hp(self)
        static int l_get_hp(lua_State *L);