Optimize updateFastFaceRow processing by removing some TileSpec copy (#5678)
[oweals/minetest.git] / src / content_cao.cpp
index 5ddbd27c93885548798a272525e39a4447b207fa..1bb24aa88a23948698e84fbbde545a39522125f2 100644 (file)
@@ -26,7 +26,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "content_cao.h"
 #include "util/numeric.h" // For IntervalLimiter
 #include "util/serialize.h"
-#include "util/mathconstants.h"
 #include "util/basic_macros.h"
 #include "client/tile.h"
 #include "environment.h"
@@ -575,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),
@@ -604,7 +605,7 @@ bool GenericCAO::getCollisionBox(aabb3f *toset) const
        return false;
 }
 
-bool GenericCAO::collideWithObjects()
+bool GenericCAO::collideWithObjects() const
 {
        return m_prop.collideWithObjects;
 }
@@ -932,28 +933,38 @@ void GenericCAO::addToScene(scene::ISceneManager *smgr,
                        errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
        }
        else if(m_prop.visual == "wielditem") {
-               infostream<<"GenericCAO::addToScene(): wielditem"<<std::endl;
-               infostream<<"textures: "<<m_prop.textures.size()<<std::endl;
-               if(m_prop.textures.size() >= 1){
-                       infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl;
-                       IItemDefManager *idef = m_client->idef();
-                       ItemStack item(m_prop.textures[0], 1, 0, "", idef);
-
-                       m_wield_meshnode = new WieldMeshSceneNode(
-                                       smgr->getRootSceneNode(), smgr, -1);
-                       m_wield_meshnode->setItem(item, m_client);
-
-                       m_wield_meshnode->setScale(v3f(m_prop.visual_size.X/2,
-                                       m_prop.visual_size.Y/2,
-                                       m_prop.visual_size.X/2));
-                       u8 li = m_last_light;
-                       m_wield_meshnode->setColor(video::SColor(255,li,li,li));
+               ItemStack item;
+               infostream << "GenericCAO::addToScene(): wielditem" << std::endl;
+               if (m_prop.wield_item == "") {
+                       // Old format, only textures are specified.
+                       infostream << "textures: " << m_prop.textures.size() << std::endl;
+                       if (m_prop.textures.size() >= 1) {
+                               infostream << "textures[0]: " << m_prop.textures[0]
+                                       << std::endl;
+                               IItemDefManager *idef = m_client->idef();
+                               item = ItemStack(m_prop.textures[0], 1, 0, idef);
+                       }
+               } else {
+                       infostream << "serialized form: " << m_prop.wield_item << std::endl;
+                       item.deSerialize(m_prop.wield_item, m_client->idef());
                }
+               m_wield_meshnode = new WieldMeshSceneNode(smgr->getRootSceneNode(),
+                       smgr, -1);
+               m_wield_meshnode->setItem(item, m_client);
+
+               m_wield_meshnode->setScale(
+                       v3f(m_prop.visual_size.X / 2, m_prop.visual_size.Y / 2,
+                               m_prop.visual_size.X / 2));
+               u8 li = m_last_light;
+               m_wield_meshnode->setColor(video::SColor(255, li, li, li));
        } else {
                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) {
@@ -1030,12 +1041,9 @@ void GenericCAO::updateNodePos()
 void GenericCAO::step(float dtime, ClientEnvironment *env)
 {
        // Handel model of local player instantly to prevent lags
-       if(m_is_local_player)
-       {
+       if (m_is_local_player) {
                LocalPlayer *player = m_env->getLocalPlayer();
-
-               if (m_is_visible)
-               {
+               if (m_is_visible) {
                        int old_anim = player->last_animation;
                        float old_anim_speed = player->last_animation_speed;
                        m_position = player->getPosition() + v3f(0,BS,0);
@@ -1043,7 +1051,7 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
                        m_acceleration = v3f(0,0,0);
                        pos_translator.vect_show = m_position;
                        m_yaw = player->getYaw();
-                       PlayerControl controls = player->getPlayerControl();
+                       const PlayerControl &controls = player->getPlayerControl();
 
                        bool walking = false;
                        if (controls.up || controls.down || controls.left || controls.right ||
@@ -1064,11 +1072,10 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
                                        m_client->checkLocalPrivilege("fly"))))
                                        new_speed *= 1.5;
                        // slowdown speed if sneeking
-                       if(controls.sneak && walking)
+                       if (controls.sneak && walking)
                                new_speed /= 2;
 
-                       if(walking && (controls.LMB || controls.RMB))
-                       {
+                       if (walking && (controls.LMB || controls.RMB)) {
                                new_anim = player->local_animations[3];
                                player->last_animation = WD_ANIM;
                        } else if(walking) {
@@ -1081,8 +1088,7 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
 
                        // Apply animations if input detected and not attached
                        // or set idle animation
-                       if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached)
-                       {
+                       if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached) {
                                allow_update = true;
                                m_animation_range = new_anim;
                                m_animation_speed = new_speed;
@@ -1090,8 +1096,7 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
                        } else {
                                player->last_animation = NO_ANIM;
 
-                               if (old_anim != NO_ANIM)
-                               {
+                               if (old_anim != NO_ANIM) {
                                        m_animation_range = player->local_animations[0];
                                        updateAnimation();
                                }
@@ -1222,9 +1227,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)
@@ -1302,7 +1307,7 @@ void GenericCAO::updateTexturePos()
        }
 }
 
-void GenericCAO::updateTextures(const std::string &mod)
+void GenericCAO::updateTextures(std::string mod)
 {
        ITextureSource *tsrc = m_client->tsrc();
 
@@ -1310,6 +1315,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")
@@ -1612,6 +1620,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);
@@ -1632,6 +1646,7 @@ void GenericCAO::processMessage(const std::string &data)
                // these are sent inverted so we get true when the server sends nothing
                bool sneak = !readU8(is);
                bool sneak_glitch = !readU8(is);
+               bool new_move = !readU8(is);
 
 
                if(m_is_local_player)
@@ -1642,6 +1657,7 @@ void GenericCAO::processMessage(const std::string &data)
                        player->physics_override_gravity = override_gravity;
                        player->physics_override_sneak = sneak;
                        player->physics_override_sneak_glitch = sneak_glitch;
+                       player->physics_override_new_move = new_move;
                }
        } else if (cmd == GENERIC_CMD_SET_ANIMATION) {
                // TODO: change frames send as v2s32 value
@@ -1735,7 +1751,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) {
@@ -1766,7 +1782,7 @@ void GenericCAO::processMessage(const std::string &data)
        } else {
                warningstream << FUNCTION_NAME
                        << ": unknown command or outdated client \""
-                       << cmd << std::endl;
+                       << +cmd << "\"" << std::endl;
        }
 }
 
@@ -1803,7 +1819,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;