Add Entity get_texture_mod() to Lua API
authorsapier <sapier at gmx dot net>
Sat, 14 Jan 2017 18:32:10 +0000 (19:32 +0100)
committersapier <sapier at gmx dot net>
Sat, 21 Jan 2017 16:01:02 +0000 (17:01 +0100)
Send texture modifier to clients connecting later too

doc/lua_api.txt
src/content_cao.cpp
src/content_cao.h
src/content_sao.cpp
src/content_sao.h
src/script/lua_api/l_object.cpp
src/script/lua_api/l_object.h

index aada851a1fb968a9a199c70c28642c01525bfc78..e5a3362ee096afb224bf8c7bdc8aed9a8a75b9fa 100644 (file)
@@ -2848,6 +2848,7 @@ This is basically a reference to a C++ `ServerActiveObject`
 * `set_yaw(radians)`
 * `get_yaw()`: returns number in radians
 * `set_texture_mod(mod)`
+* `get_texture_mod()` returns current texture modifier
 * `set_sprite(p={x=0,y=0}, num_frames=1, framelength=0.2,
   select_horiz_by_yawpitch=false)`
     * Select sprite from spritesheet with optional animation and DM-style
index 93ac1f785fc0f33f5483c88f5e953268b2e17f69..e829fc7614dd85d8dfaa863c33e900bc224364f5 100644 (file)
@@ -574,6 +574,8 @@ GenericCAO::GenericCAO(Client *client, ClientEnvironment *env):
                m_anim_framelength(0.2),
                m_anim_timer(0),
                m_reset_textures_timer(-1),
+               m_previous_texture_modifier(""),
+               m_current_texture_modifier(""),
                m_visuals_expired(false),
                m_step_distance_counter(0),
                m_last_light(255),
@@ -952,7 +954,10 @@ void GenericCAO::addToScene(scene::ISceneManager *smgr,
                infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
                                <<"\" not supported"<<std::endl;
        }
-       updateTextures("");
+
+       /* don't update while punch texture modifier is active */
+       if (m_reset_textures_timer < 0)
+               updateTextures(m_current_texture_modifier);
 
        scene::ISceneNode *node = getSceneNode();
        if (node && m_prop.nametag != "" && !m_is_local_player) {
@@ -1221,9 +1226,9 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
        if(m_reset_textures_timer >= 0)
        {
                m_reset_textures_timer -= dtime;
-               if(m_reset_textures_timer <= 0){
+               if(m_reset_textures_timer <= 0) {
                        m_reset_textures_timer = -1;
-                       updateTextures("");
+                       updateTextures(m_previous_texture_modifier);
                }
        }
        if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001)
@@ -1301,7 +1306,7 @@ void GenericCAO::updateTexturePos()
        }
 }
 
-void GenericCAO::updateTextures(const std::string &mod)
+void GenericCAO::updateTextures(const std::string mod)
 {
        ITextureSource *tsrc = m_client->tsrc();
 
@@ -1309,6 +1314,9 @@ void GenericCAO::updateTextures(const std::string &mod)
        bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
        bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
 
+       m_previous_texture_modifier = m_current_texture_modifier;
+       m_current_texture_modifier = mod;
+
        if(m_spritenode)
        {
                if(m_prop.visual == "sprite")
@@ -1611,6 +1619,12 @@ void GenericCAO::processMessage(const std::string &data)
                updateNodePos();
        } else if (cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
                std::string mod = deSerializeString(is);
+
+               // immediatly reset a engine issued texture modifier if a mod sends a different one
+               if (m_reset_textures_timer > 0) {
+                       m_reset_textures_timer = -1;
+                       updateTextures(m_previous_texture_modifier);
+               }
                updateTextures(mod);
        } else if (cmd == GENERIC_CMD_SET_SPRITE) {
                v2s16 p = readV2S16(is);
@@ -1734,7 +1748,7 @@ void GenericCAO::processMessage(const std::string &data)
                                m_reset_textures_timer = 0.05;
                                if(damage >= 2)
                                        m_reset_textures_timer += 0.05 * damage;
-                               updateTextures("^[brighten");
+                               updateTextures(m_current_texture_modifier + "^[brighten");
                        }
                }
        } else if (cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
@@ -1802,7 +1816,7 @@ bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
                m_reset_textures_timer = 0.05;
                if(result.damage >= 2)
                        m_reset_textures_timer += 0.05 * result.damage;
-               updateTextures("^[brighten");
+               updateTextures(m_current_texture_modifier + "^[brighten");
        }
 
        return false;
index 846e0690a2e3d831905cd42d82f36b3cd76413d6..f30e90e21049f2359d13a8c34c27461e18326299 100644 (file)
@@ -102,6 +102,8 @@ private:
        float m_anim_timer;
        ItemGroupList m_armor_groups;
        float m_reset_textures_timer;
+       std::string m_previous_texture_modifier; // stores texture modifier before punch update
+       std::string m_current_texture_modifier;  // last applied texture modifier
        bool m_visuals_expired;
        float m_step_distance_counter;
        u8 m_last_light;
@@ -198,7 +200,7 @@ public:
 
        void updateTexturePos();
 
-       void updateTextures(const std::string &mod);
+       void updateTextures(const std::string mod);
 
        void updateAnimation();
 
index d6581144fc25a47b0c5b6bd72374052950f6f9e2..f0973082d7d90656e2fad020b6617ac1bf19e874 100644 (file)
@@ -257,7 +257,8 @@ LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos,
        m_last_sent_position(0,0,0),
        m_last_sent_velocity(0,0,0),
        m_last_sent_position_timer(0),
-       m_last_sent_move_precision(0)
+       m_last_sent_move_precision(0),
+       m_current_texture_modifier("")
 {
        // Only register type if no environment supplied
        if(env == NULL){
@@ -511,6 +512,9 @@ std::string LuaEntitySAO::getClientInitializationData(u16 protocol_version)
                        }
                }
 
+               msg_os << serializeLongString(gob_cmd_set_texture_mod(m_current_texture_modifier));
+               message_count++;
+
                writeU8(os, message_count);
                os.write(msg_os.str().c_str(), msg_os.str().size());
        }
@@ -687,11 +691,17 @@ v3f LuaEntitySAO::getAcceleration()
 void LuaEntitySAO::setTextureMod(const std::string &mod)
 {
        std::string str = gob_cmd_set_texture_mod(mod);
+       m_current_texture_modifier = mod;
        // create message and add to list
        ActiveObjectMessage aom(getId(), true, str);
        m_messages_out.push(aom);
 }
 
+std::string LuaEntitySAO::getTextureMod() const
+{
+       return m_current_texture_modifier;
+}
+
 void LuaEntitySAO::setSprite(v2s16 p, int num_frames, float framelength,
                bool select_horiz_by_yawpitch)
 {
index 884f0f4069b2db6c92f99ce50fbb42f3cca7f797..56a26fb0d242b73ad5f843700cea75fa7226db6b 100644 (file)
@@ -122,6 +122,7 @@ public:
        v3f getAcceleration();
 
        void setTextureMod(const std::string &mod);
+       std::string getTextureMod() const;
        void setSprite(v2s16 p, int num_frames, float framelength,
                        bool select_horiz_by_yawpitch);
        std::string getName();
@@ -143,6 +144,7 @@ private:
        v3f m_last_sent_velocity;
        float m_last_sent_position_timer;
        float m_last_sent_move_precision;
+       std::string m_current_texture_modifier;
 };
 
 /*
index f579c0b861ee5e534609203e52c27f0c990fc6ee..1fa3663ca5b100d2b010fedd23f03a62ceab08e9 100644 (file)
@@ -900,6 +900,19 @@ int ObjectRef::l_set_texture_mod(lua_State *L)
        return 0;
 }
 
+// get_texture_mod(self)
+int ObjectRef::l_get_texture_mod(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+       ObjectRef *ref = checkobject(L, 1);
+       LuaEntitySAO *co = getluaobject(ref);
+       if (co == NULL) return 0;
+       // Do it
+       std::string mod = co->getTextureMod();
+       lua_pushstring(L, mod.c_str());
+       return 1;
+}
+
 // set_sprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
 //           select_horiz_by_yawpitch=false)
 int ObjectRef::l_set_sprite(lua_State *L)
index 06b1bb79b90d9b0b95e9978cd9eef981a6aeb7a0..96d0abae80ec9552c927151e9c78f0443b4fa2c2 100644 (file)
@@ -164,6 +164,9 @@ private:
        // set_texture_mod(self, mod)
        static int l_set_texture_mod(lua_State *L);
 
+       // l_get_texture_mod(self)
+       static int l_get_texture_mod(lua_State *L);
+
        // set_sprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
        //           select_horiz_by_yawpitch=false)
        static int l_set_sprite(lua_State *L);