]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/client/content_cao.cpp
Added configureable Colors for PlayerESP and EntityESP
[dragonfireclient.git] / src / client / content_cao.cpp
index 7f573b5a10b149afd9b08e05c8de38e08cde0c29..7208212d41d0a086236a21972bb0315bab31ac4a 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
 */
@@ -230,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);
@@ -344,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;
                }
        }
 
@@ -362,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);
@@ -372,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);
        }
 
@@ -447,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;
+               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()
@@ -489,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);
        }
@@ -498,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)
@@ -558,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)
@@ -786,21 +821,23 @@ 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 = false;
-
+               
        v3s16 pos[3];
        u16 npos = getLightPosition(pos);
        for (u16 i = 0; i < npos; i++) {
@@ -816,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);
@@ -876,9 +915,29 @@ u16 GenericCAO::getLightPosition(v3s16 *pos)
        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;
+       }
+
+       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
+       if (m_is_local_player && ! g_settings->getBool("freecam")) // No nametag for local player
                return;
 
        if (m_prop.nametag.empty()) {
@@ -929,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;
@@ -950,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;
@@ -968,16 +1024,16 @@ 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 && ! 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;
                        }
@@ -1000,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();
 
                }
        }
@@ -1143,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()
@@ -1160,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;
@@ -1190,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.
@@ -1231,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) {
@@ -1280,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)
@@ -1373,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()
@@ -1444,6 +1537,18 @@ void GenericCAO::updateBonePosition()
                        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()
@@ -1530,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
@@ -1563,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();
@@ -1575,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.
@@ -1607,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) {
@@ -1639,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;
@@ -1685,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);
@@ -1693,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);
 
@@ -1743,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;
                }
@@ -1815,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;
+
+       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);