]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/client/content_cao.cpp
Merge branch 'master' of https://github.com/minetest/minetest
[dragonfireclient.git] / src / client / content_cao.cpp
index a6ce06d205dbfb31a0f8c3945f9fab33ecf72f2c..c44c167b526e3cd1b702dedb58f1f2a9c3c5300a 100644 (file)
@@ -47,6 +47,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <algorithm>
 #include <cmath>
 #include "client/shader.h"
+#include "script/scripting_client.h"
+#include "client/minimap.h"
 
 class Settings;
 struct ToolCapabilities;
@@ -162,6 +164,15 @@ static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
        matrix.setTextureScale(txs, tys);
 }
 
+// Evaluate transform chain recursively; irrlicht does not do this for us
+static void updatePositionRecursive(scene::ISceneNode *node)
+{
+       scene::ISceneNode *parent = node->getParent();
+       if (parent)
+               updatePositionRecursive(parent);
+       node->updateAbsolutePosition();
+}
+
 /*
        TestCAO
 */
@@ -182,7 +193,6 @@ class TestCAO : public ClientActiveObject
        void addToScene(ITextureSource *tsrc);
        void removeFromScene(bool permanent);
        void updateLight(u32 day_night_ratio);
-       v3s16 getLightPosition();
        void updateNodePos();
 
        void step(float dtime, ClientEnvironment *env);
@@ -231,7 +241,7 @@ void TestCAO::addToScene(ITextureSource *tsrc)
        u16 indices[] = {0,1,2,2,3,0};
        buf->append(vertices, 4, indices, 6);
        // Set material
-       buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
+       buf->getMaterial().setFlag(video::EMF_LIGHTING, true);  // false
        buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
        buf->getMaterial().setTexture(0, tsrc->getTextureForMesh("rat.png"));
        buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
@@ -258,11 +268,6 @@ void TestCAO::updateLight(u32 day_night_ratio)
 {
 }
 
-v3s16 TestCAO::getLightPosition()
-{
-       return floatToInt(m_position, BS);
-}
-
 void TestCAO::updateNodePos()
 {
        if (!m_node)
@@ -350,6 +355,8 @@ void GenericCAO::initialize(const std::string &data)
                        m_is_local_player = true;
                        m_is_visible = false;
                        player->setCAO(this);
+
+                       m_prop.show_on_minimap = false;
                }
        }
 
@@ -368,7 +375,7 @@ void GenericCAO::processInitData(const std::string &data)
        }
 
        // PROTOCOL_VERSION >= 37
-       m_name = deSerializeString(is);
+       m_name = deSerializeString16(is);
        m_is_player = readU8(is);
        m_id = readU16(is);
        m_position = readV3F32(is);
@@ -378,7 +385,7 @@ void GenericCAO::processInitData(const std::string &data)
        const u8 num_messages = readU8(is);
 
        for (int i = 0; i < num_messages; i++) {
-               std::string message = deSerializeLongString(is);
+               std::string message = deSerializeString32(is);
                processMessage(message);
        }
 
@@ -453,39 +460,58 @@ void GenericCAO::setChildrenVisible(bool toset)
        for (u16 cao_id : m_attachment_child_ids) {
                GenericCAO *obj = m_env->getGenericCAO(cao_id);
                if (obj) {
-                       obj->setVisible(toset);
+                       // Check if the entity is forced to appear in first person.
+                       obj->setVisible(obj->m_force_visible ? true : toset);
                }
        }
 }
 
-void GenericCAO::setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation)
+void GenericCAO::setAttachment(int parent_id, const std::string &bone,
+               v3f position, v3f rotation, bool force_visible)
 {
        int old_parent = m_attachment_parent_id;
        m_attachment_parent_id = parent_id;
        m_attachment_bone = bone;
        m_attachment_position = position;
        m_attachment_rotation = rotation;
+       m_force_visible = force_visible;
 
        ClientActiveObject *parent = m_env->getActiveObject(parent_id);
 
        if (parent_id != old_parent) {
+               if (old_parent)
+                       m_waiting_for_reattach = 10;
                if (auto *o = m_env->getActiveObject(old_parent))
                        o->removeAttachmentChild(m_id);
                if (parent)
                        parent->addAttachmentChild(m_id);
        }
 
-
        updateAttachments();
+
+       // Forcibly show attachments if required by set_attach
+       if (m_force_visible) {
+               m_is_visible = true;
+       } else if (!m_is_local_player) {
+               // Objects attached to the local player should be hidden in first person
+               m_is_visible = !m_attached_to_local ||
+                       m_client->getCamera()->getCameraMode() != CAMERA_MODE_FIRST || g_settings->getBool("freecam");
+               m_force_visible = false;
+       } else {
+               // Local players need to have this set,
+               // otherwise first person attachments fail.
+               m_is_visible = true;
+       }
 }
 
 void GenericCAO::getAttachment(int *parent_id, std::string *bone, v3f *position,
-       v3f *rotation) const
+       v3f *rotation, bool *force_visible) const
 {
        *parent_id = m_attachment_parent_id;
        *bone = m_attachment_bone;
        *position = m_attachment_position;
        *rotation = m_attachment_rotation;
+       *force_visible = m_force_visible;
 }
 
 void GenericCAO::clearChildAttachments()
@@ -495,7 +521,7 @@ void GenericCAO::clearChildAttachments()
                int child_id = *m_attachment_child_ids.begin();
 
                if (ClientActiveObject *child = m_env->getActiveObject(child_id))
-                       child->setAttachment(0, "", v3f(), v3f());
+                       child->setAttachment(0, "", v3f(), v3f(), false);
 
                removeAttachmentChild(child_id);
        }
@@ -504,9 +530,9 @@ void GenericCAO::clearChildAttachments()
 void GenericCAO::clearParentAttachment()
 {
        if (m_attachment_parent_id)
-               setAttachment(0, "", m_attachment_position, m_attachment_rotation);
+               setAttachment(0, "", m_attachment_position, m_attachment_rotation, false);
        else
-               setAttachment(0, "", v3f(), v3f());
+               setAttachment(0, "", v3f(), v3f(), false);
 }
 
 void GenericCAO::addAttachmentChild(int child_id)
@@ -564,6 +590,9 @@ void GenericCAO::removeFromScene(bool permanent)
                m_client->getCamera()->removeNametag(m_nametag);
                m_nametag = nullptr;
        }
+
+       if (m_marker && m_client->getMinimap())
+               m_client->getMinimap()->removeMarker(&m_marker);
 }
 
 void GenericCAO::addToScene(ITextureSource *tsrc)
@@ -583,10 +612,16 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
 
        if (m_enable_shaders) {
                IShaderSource *shader_source = m_client->getShaderSource();
-               u32 shader_id = shader_source->getShader(
-                               "object_shader",
-                               (m_prop.use_texture_alpha) ? TILE_MATERIAL_ALPHA : TILE_MATERIAL_BASIC,
-                               NDT_NORMAL);
+               MaterialType material_type;
+
+               if (m_prop.shaded && m_prop.glow == 0)
+                       material_type = (m_prop.use_texture_alpha) ?
+                               TILE_MATERIAL_ALPHA : TILE_MATERIAL_BASIC;
+               else
+                       material_type = (m_prop.use_texture_alpha) ?
+                               TILE_MATERIAL_PLAIN_ALPHA : TILE_MATERIAL_PLAIN;
+
+               u32 shader_id = shader_source->getShader("object_shader", material_type, NDT_NORMAL);
                m_material_type = shader_source->getShaderInfo(shader_id).material;
        } else {
                m_material_type = (m_prop.use_texture_alpha) ?
@@ -786,29 +821,40 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
                node->setParent(m_matrixnode);
 
        updateNametag();
+       updateMarker();
        updateNodePos();
        updateAnimation();
        updateBonePosition();
        updateAttachments();
        setNodeLight(m_last_light);
+       updateMeshCulling();
 }
 
 void GenericCAO::updateLight(u32 day_night_ratio)
-{
+{              
        if (m_glow < 0)
                return;
 
        u8 light_at_pos = 0;
-       bool pos_ok;
-
-       v3s16 p = getLightPosition();
-       MapNode n = m_env->getMap().getNode(p, &pos_ok);
-       if (pos_ok)
-               light_at_pos = n.getLightBlend(day_night_ratio, m_client->ndef());
-       else
+       bool pos_ok = false;
+               
+       v3s16 pos[3];
+       u16 npos = getLightPosition(pos);
+       for (u16 i = 0; i < npos; i++) {
+               bool this_ok;
+               MapNode n = m_env->getMap().getNode(pos[i], &this_ok);
+               if (this_ok) {
+                       u8 this_light = n.getLightBlend(day_night_ratio, m_client->ndef());
+                       light_at_pos = MYMAX(light_at_pos, this_light);
+                       pos_ok = true;
+               }
+       }
+       if (!pos_ok)
                light_at_pos = blend_light(day_night_ratio, LIGHT_SUN, 0);
 
        u8 light = decode_light(light_at_pos + m_glow);
+       if (g_settings->getBool("fullbright"))
+               light = 255;
        if (light != m_last_light) {
                m_last_light = light;
                setNodeLight(light);
@@ -856,18 +902,43 @@ void GenericCAO::setNodeLight(u8 light)
        }
 }
 
-v3s16 GenericCAO::getLightPosition()
+u16 GenericCAO::getLightPosition(v3s16 *pos)
 {
-       if (m_is_player)
-               return floatToInt(m_position + v3f(0, 0.5 * BS, 0), BS);
+       const auto &box = m_prop.collisionbox;
+       pos[0] = floatToInt(m_position + box.MinEdge * BS, BS);
+       pos[1] = floatToInt(m_position + box.MaxEdge * BS, BS);
+
+       // Skip center pos if it falls into the same node as Min or MaxEdge
+       if ((box.MaxEdge - box.MinEdge).getLengthSQ() < 3.0f)
+               return 2;
+       pos[2] = floatToInt(m_position + box.getCenter() * BS, BS);
+       return 3;
+}
+
+void GenericCAO::updateMarker()
+{
+       if (!m_client->getMinimap())
+               return;
+
+       if (!m_prop.show_on_minimap) {
+               if (m_marker)
+                       m_client->getMinimap()->removeMarker(&m_marker);
+               return;
+       }
 
-       return floatToInt(m_position, BS);
+       if (m_marker)
+               return;
+
+       scene::ISceneNode *node = getSceneNode();
+       if (!node)
+               return;
+       m_marker = m_client->getMinimap()->addMarker(node);
 }
 
 void GenericCAO::updateNametag()
 {
-       if (m_is_local_player) // No nametag for local player
-               return;
+       //if (m_is_local_player && ! g_settings->getBool("freecam")) // No nametag for local player
+               //return;
 
        if (m_prop.nametag.empty()) {
                // Delete nametag
@@ -917,18 +988,15 @@ void GenericCAO::updateNodePos()
 
 void GenericCAO::step(float dtime, ClientEnvironment *env)
 {
-       if (m_animated_meshnode) {
-               m_animated_meshnode->animateJoints();
-               updateBonePosition();
-       }
-       
        // Handle model animations and update positions instantly to prevent lags
        if (m_is_local_player) {
                LocalPlayer *player = m_env->getLocalPlayer();
-               m_position = player->getPosition();
+               m_position = player->getLegitPosition();
                pos_translator.val_current = m_position;
-               m_rotation.Y = wrapDegrees_0_360(player->getYaw());
-               rot_translator.val_current = m_rotation;
+               if (! g_settings->getBool("freecam")) {
+                       m_rotation.Y = wrapDegrees_0_360(player->getYaw());
+                       rot_translator.val_current = m_rotation;
+               }
 
                if (m_is_visible) {
                        int old_anim = player->last_animation;
@@ -938,9 +1006,9 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
                        const PlayerControl &controls = player->getPlayerControl();
 
                        bool walking = false;
-                       if (controls.up || controls.down || controls.left || controls.right ||
+                       if ((controls.up || controls.down || controls.left || controls.right ||
                                        controls.forw_move_joystick_axis != 0.f ||
-                                       controls.sidew_move_joystick_axis != 0.f)
+                                       controls.sidew_move_joystick_axis != 0.f) && ! g_settings->getBool("freecam"))
                                walking = true;
 
                        f32 new_speed = player->local_animation_speed;
@@ -948,24 +1016,24 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
                        bool allow_update = false;
 
                        // increase speed if using fast or flying fast
-                       if((g_settings->getBool("fast_move") &&
+                       if(((g_settings->getBool("fast_move") &&
                                        m_client->checkLocalPrivilege("fast")) &&
                                        (controls.aux1 ||
                                        (!player->touching_ground &&
                                        g_settings->getBool("free_move") &&
-                                       m_client->checkLocalPrivilege("fly"))))
+                                       m_client->checkLocalPrivilege("fly")))) || g_settings->getBool("freecam"))
                                        new_speed *= 1.5;
                        // slowdown speed if sneeking
-                       if (controls.sneak && walking)
+                       if (controls.sneak && walking && ! g_settings->getBool("no_slow"))
                                new_speed /= 2;
 
-                       if (walking && (controls.LMB || controls.RMB)) {
+                       if (walking && (controls.dig || controls.place)) {
                                new_anim = player->local_animations[3];
                                player->last_animation = WD_ANIM;
-                       } else if(walking) {
+                       } else if (walking) {
                                new_anim = player->local_animations[1];
                                player->last_animation = WALK_ANIM;
-                       } else if(controls.LMB || controls.RMB) {
+                       } else if (controls.dig || controls.place) {
                                new_anim = player->local_animations[2];
                                player->last_animation = DIG_ANIM;
                        }
@@ -988,9 +1056,9 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
 
                        // Update local player animations
                        if ((player->last_animation != old_anim ||
-                               m_animation_speed != old_anim_speed) &&
-                               player->last_animation != NO_ANIM && allow_update)
-                                       updateAnimation();
+                                       m_animation_speed != old_anim_speed) &&
+                                       player->last_animation != NO_ANIM && allow_update)
+                               updateAnimation();
 
                }
        }
@@ -1105,7 +1173,7 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
                }
        }
 
-       if (!getParent() && std::fabs(m_prop.automatic_rotate) > 0.001) {
+       if (!getParent() && node && fabs(m_prop.automatic_rotate) > 0.001f) {
                // This is the child node's rotation. It is only used for automatic_rotate.
                v3f local_rot = node->getRotation();
                local_rot.Y = modulo360f(local_rot.Y - dtime * core::RADTODEG *
@@ -1114,7 +1182,7 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
        }
 
        if (!getParent() && m_prop.automatic_face_movement_dir &&
-                       (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001)) {
+                       (fabs(m_velocity.Z) > 0.001f || fabs(m_velocity.X) > 0.001f)) {
                float target_yaw = atan2(m_velocity.Z, m_velocity.X) * 180 / M_PI
                                + m_prop.automatic_face_movement_dir_offset;
                float max_rotation_per_sec =
@@ -1131,6 +1199,18 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
                rot_translator.val_current = m_rotation;
                updateNodePos();
        }
+
+       if (m_animated_meshnode) {
+               // Everything must be updated; the whole transform
+               // chain as well as the animated mesh node.
+               // Otherwise, bone attachments would be relative to
+               // a position that's one frame old.
+               if (m_matrixnode)
+                       updatePositionRecursive(m_matrixnode);
+               m_animated_meshnode->updateAbsolutePosition();
+               m_animated_meshnode->animateJoints();
+               updateBonePosition();
+       }
 }
 
 void GenericCAO::updateTexturePos()
@@ -1148,6 +1228,7 @@ void GenericCAO::updateTexturePos()
                int row = m_tx_basepos.Y;
                int col = m_tx_basepos.X;
 
+               // Yawpitch goes rightwards
                if (m_tx_select_horiz_by_yawpitch) {
                        if (cam_to_entity.Y > 0.75)
                                col += 5;
@@ -1178,6 +1259,27 @@ void GenericCAO::updateTexturePos()
                float tys = m_tx_size.Y;
                setBillboardTextureMatrix(m_spritenode, txs, tys, col, row);
        }
+
+       else if (m_meshnode) {
+               if (m_prop.visual == "upright_sprite") {
+                       int row = m_tx_basepos.Y;
+                       int col = m_tx_basepos.X;
+
+                       // Animation goes downwards
+                       row += m_anim_frame;
+
+                       const auto &tx = m_tx_size;
+                       v2f t[4] = { // cf. vertices in GenericCAO::addToScene()
+                               tx * v2f(col+1, row+1),
+                               tx * v2f(col, row+1),
+                               tx * v2f(col, row),
+                               tx * v2f(col+1, row),
+                       };
+                       auto mesh = m_meshnode->getMesh();
+                       setMeshBufferTextureCoords(mesh->getMeshBuffer(0), t, 4);
+                       setMeshBufferTextureCoords(mesh->getMeshBuffer(1), t, 4);
+               }
+       }
 }
 
 // Do not pass by reference, see header.
@@ -1219,7 +1321,7 @@ void GenericCAO::updateTextures(std::string mod)
                }
        }
 
-       if (m_animated_meshnode) {
+       else if (m_animated_meshnode) {
                if (m_prop.visual == "mesh") {
                        for (u32 i = 0; i < m_prop.textures.size() &&
                                        i < m_animated_meshnode->getMaterialCount(); ++i) {
@@ -1268,8 +1370,8 @@ void GenericCAO::updateTextures(std::string mod)
                        }
                }
        }
-       if(m_meshnode)
-       {
+
+       else if (m_meshnode) {
                if(m_prop.visual == "cube")
                {
                        for (u32 i = 0; i < 6; ++i)
@@ -1361,6 +1463,9 @@ void GenericCAO::updateTextures(std::string mod)
                                setMeshColor(mesh, m_prop.colors[0]);
                }
        }
+       // Prevent showing the player after changing texture
+       if (m_is_local_player)
+               updateMeshCulling();
 }
 
 void GenericCAO::updateAnimation()
@@ -1403,7 +1508,7 @@ void GenericCAO::updateBonePosition()
                        bone->setRotation(it.second.Y);
                }
        }
-       
+
        // search through bones to find mistakenly rotated bones due to bug in Irrlicht
        for (u32 i = 0; i < m_animated_meshnode->getJointCount(); ++i) {
                irr::scene::IBoneSceneNode *bone = m_animated_meshnode->getJointNode(i);
@@ -1427,11 +1532,23 @@ void GenericCAO::updateBonePosition()
                // and update the bones transformation.
                v3f bone_rot = bone->getRelativeTransformation().getRotationDegrees();
                float offset = fabsf(bone_rot.X - bone->getRotation().X);
-               if (offset > 179.9f && offset < 180.1f) { 
+               if (offset > 179.9f && offset < 180.1f) {
                        bone->setRotation(bone_rot);
                        bone->updateAbsolutePosition();
                }
        }
+       // The following is needed for set_bone_pos to propagate to
+       // attached objects correctly.
+       // Irrlicht ought to do this, but doesn't when using EJUOR_CONTROL.
+       for (u32 i = 0; i < m_animated_meshnode->getJointCount(); ++i) {
+               auto bone = m_animated_meshnode->getJointNode(i);
+               // Look for the root bone.
+               if (bone && bone->getParent() == m_animated_meshnode) {
+                       // Update entire skeleton.
+                       bone->updateAbsolutePositionOfAllChildren();
+                       break;
+               }
+       }
 }
 
 void GenericCAO::updateAttachments()
@@ -1454,15 +1571,17 @@ void GenericCAO::updateAttachments()
 
        if (!parent) { // Detach or don't attach
                if (m_matrixnode) {
+                       v3s16 camera_offset = m_env->getCameraOffset();
                        v3f old_pos = getPosition();
 
                        m_matrixnode->setParent(m_smgr->getRootSceneNode());
-                       getPosRotMatrix().setTranslation(old_pos);
+                       getPosRotMatrix().setTranslation(old_pos - intToFloat(camera_offset, BS));
                        m_matrixnode->updateAbsolutePosition();
                }
        }
        else // Attach
        {
+               parent->updateAttachments();
                scene::ISceneNode *parent_node = parent->getSceneNode();
                scene::IAnimatedMeshSceneNode *parent_animated_mesh_node =
                                parent->getAnimatedMeshSceneNode();
@@ -1472,6 +1591,7 @@ void GenericCAO::updateAttachments()
 
                if (m_matrixnode && parent_node) {
                        m_matrixnode->setParent(parent_node);
+                       parent_node->updateAbsolutePosition();
                        getPosRotMatrix().setTranslation(m_attachment_position);
                        //setPitchYawRoll(getPosRotMatrix(), m_attachment_rotation);
                        // use Irrlicht eulers instead
@@ -1484,18 +1604,27 @@ void GenericCAO::updateAttachments()
 bool GenericCAO::visualExpiryRequired(const ObjectProperties &new_) const
 {
        const ObjectProperties &old = m_prop;
+       /* Visuals do not need to be expired for:
+        * - nametag props: handled by updateNametag()
+        * - textures:      handled by updateTextures()
+        * - sprite props:  handled by updateTexturePos()
+        * - glow:          handled by updateLight()
+        * - any other properties that do not change appearance
+        */
+
+       bool uses_legacy_texture = new_.wield_item.empty() &&
+               (new_.visual == "wielditem" || new_.visual == "item");
        // Ordered to compare primitive types before std::vectors
        return old.backface_culling != new_.backface_culling ||
-               old.initial_sprite_basepos != new_.initial_sprite_basepos ||
                old.is_visible != new_.is_visible ||
                old.mesh != new_.mesh ||
-               old.spritediv != new_.spritediv ||
+               old.shaded != new_.shaded ||
                old.use_texture_alpha != new_.use_texture_alpha ||
                old.visual != new_.visual ||
                old.visual_size != new_.visual_size ||
                old.wield_item != new_.wield_item ||
                old.colors != new_.colors ||
-               old.textures != new_.textures;
+               (uses_legacy_texture && old.textures != new_.textures);
 }
 
 void GenericCAO::processMessage(const std::string &data)
@@ -1506,10 +1635,13 @@ void GenericCAO::processMessage(const std::string &data)
        u8 cmd = readU8(is);
        if (cmd == AO_CMD_SET_PROPERTIES) {
                ObjectProperties newprops;
+               newprops.show_on_minimap = m_is_player; // default
+
                newprops.deSerialize(is);
 
                // Check what exactly changed
                bool expire_visuals = visualExpiryRequired(newprops);
+               bool textures_changed = m_prop.textures != newprops.textures;
 
                // Apply changes
                m_prop = std::move(newprops);
@@ -1538,13 +1670,21 @@ void GenericCAO::processMessage(const std::string &data)
 
                if ((m_is_player && !m_is_local_player) && m_prop.nametag.empty())
                        m_prop.nametag = m_name;
-               updateNametag();
+               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.
@@ -1577,7 +1717,7 @@ void GenericCAO::processMessage(const std::string &data)
                rot_translator.update(m_rotation, false, update_interval);
                updateNodePos();
        } else if (cmd == AO_CMD_SET_TEXTURE_MOD) {
-               std::string mod = deSerializeString(is);
+               std::string mod = deSerializeString16(is);
 
                // immediately reset a engine issued texture modifier if a mod sends a different one
                if (m_reset_textures_timer > 0) {
@@ -1609,6 +1749,11 @@ 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;
@@ -1655,7 +1800,7 @@ void GenericCAO::processMessage(const std::string &data)
                m_animation_speed = readF32(is);
                updateAnimationSpeed();
        } else if (cmd == AO_CMD_SET_BONE_POSITION) {
-               std::string bone = deSerializeString(is);
+               std::string bone = deSerializeString16(is);
                v3f position = readV3F32(is);
                v3f rotation = readV3F32(is);
                m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
@@ -1663,15 +1808,12 @@ void GenericCAO::processMessage(const std::string &data)
                // updateBonePosition(); now called every step
        } else if (cmd == AO_CMD_ATTACH_TO) {
                u16 parent_id = readS16(is);
-               std::string bone = deSerializeString(is);
+               std::string bone = deSerializeString16(is);
                v3f position = readV3F32(is);
                v3f rotation = readV3F32(is);
+               bool force_visible = readU8(is); // Returns false for EOF
 
-               setAttachment(parent_id, bone, position, rotation);
-
-               // localplayer itself can't be attached to localplayer
-               if (!m_is_local_player)
-                       m_is_visible = !m_attached_to_local;
+               setAttachment(parent_id, bone, position, rotation, force_visible);
        } else if (cmd == AO_CMD_PUNCHED) {
                u16 result_hp = readU16(is);
 
@@ -1713,7 +1855,7 @@ void GenericCAO::processMessage(const std::string &data)
                int armor_groups_size = readU16(is);
                for(int i=0; i<armor_groups_size; i++)
                {
-                       std::string name = deSerializeString(is);
+                       std::string name = deSerializeString16(is);
                        int rating = readS16(is);
                        m_armor_groups[name] = rating;
                }
@@ -1785,5 +1927,43 @@ std::string GenericCAO::debugInfoText()
        return os.str();
 }
 
+void GenericCAO::updateMeshCulling()
+{
+       if (!m_is_local_player)
+               return;
+
+       const bool hidden = m_client->getCamera()->getCameraMode() == CAMERA_MODE_FIRST && ! g_settings->getBool("freecam");
+
+       if (m_meshnode && m_prop.visual == "upright_sprite") {
+               u32 buffers = m_meshnode->getMesh()->getMeshBufferCount();
+               for (u32 i = 0; i < buffers; i++) {
+                       video::SMaterial &mat = m_meshnode->getMesh()->getMeshBuffer(i)->getMaterial();
+                       // upright sprite has no backface culling
+                       mat.setFlag(video::EMF_FRONT_FACE_CULLING, hidden);
+               }
+               return;
+       }
+
+       irr::scene::ISceneNode *node = getSceneNode();
+       if (!node)
+               return;
+
+       if (hidden) {
+               // Hide the mesh by culling both front and
+               // back faces. Serious hackyness but it works for our
+               // purposes. This also preserves the skeletal armature.
+               node->setMaterialFlag(video::EMF_BACK_FACE_CULLING,
+                       true);
+               node->setMaterialFlag(video::EMF_FRONT_FACE_CULLING,
+                       true);
+       } else {
+               // Restore mesh visibility.
+               node->setMaterialFlag(video::EMF_BACK_FACE_CULLING,
+                       m_prop.backface_culling);
+               node->setMaterialFlag(video::EMF_FRONT_FACE_CULLING,
+                       false);
+       }
+}
+
 // Prototype
 GenericCAO proto_GenericCAO(NULL, NULL);