]> 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 aed576372fccdb24e1362de6d6e5b7ab1c538d10..c44c167b526e3cd1b702dedb58f1f2a9c3c5300a 100644 (file)
@@ -48,6 +48,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <cmath>
 #include "client/shader.h"
 #include "script/scripting_client.h"
+#include "client/minimap.h"
 
 class Settings;
 struct ToolCapabilities;
@@ -354,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;
                }
        }
 
@@ -372,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);
@@ -382,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);
        }
 
@@ -457,18 +460,21 @@ 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);
 
@@ -480,17 +486,32 @@ void GenericCAO::setAttachment(int parent_id, const std::string &bone, v3f posit
                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()
@@ -500,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);
        }
@@ -509,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)
@@ -569,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)
@@ -797,11 +821,13 @@ 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)
@@ -811,9 +837,6 @@ void GenericCAO::updateLight(u32 day_night_ratio)
 
        u8 light_at_pos = 0;
        bool pos_ok = false;
-
-       if (g_settings->getBool("fullbright"))
-               light_at_pos = 255;
                
        v3s16 pos[3];
        u16 npos = getLightPosition(pos);
@@ -830,6 +853,8 @@ void GenericCAO::updateLight(u32 day_night_ratio)
                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);
@@ -890,11 +915,31 @@ u16 GenericCAO::getLightPosition(v3s16 *pos)
        return 3;
 }
 
-void GenericCAO::updateNametag()
+void GenericCAO::updateMarker()
 {
-       if (m_is_local_player && ! g_settings->getBool("freecam")) // No nametag for local player
+       if (!m_client->getMinimap())
+               return;
+
+       if (!m_prop.show_on_minimap) {
+               if (m_marker)
+                       m_client->getMinimap()->removeMarker(&m_marker);
+               return;
+       }
+
+       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 && ! g_settings->getBool("freecam")) // No nametag for local player
+               //return;
+
        if (m_prop.nametag.empty()) {
                // Delete nametag
                if (m_nametag) {
@@ -971,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 && ! 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;
                        }
@@ -1011,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();
 
                }
        }
@@ -1128,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 *
@@ -1137,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 =
@@ -1183,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;
@@ -1213,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.
@@ -1254,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) {
@@ -1303,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)
@@ -1396,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()
@@ -1565,6 +1635,8 @@ 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
@@ -1598,6 +1670,8 @@ void GenericCAO::processMessage(const std::string &data)
 
                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();
@@ -1610,6 +1684,7 @@ void GenericCAO::processMessage(const std::string &data)
                                        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.
@@ -1642,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) {
@@ -1725,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);
@@ -1733,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);
 
@@ -1783,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;
                }
@@ -1855,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);