]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/content_cao.cpp
Trigger on_rightclick regardless on the formspec meta field
[dragonfireclient.git] / src / content_cao.cpp
index 0b9df64515e27bee79a7c1f36117d4b55f72fbd3..771820cb09c1003cc5c1a61dab10d15b4f4b47bf 100644 (file)
@@ -22,7 +22,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <IBillboardSceneNode.h>
 #include <IMeshManipulator.h>
 #include <IAnimatedMeshSceneNode.h>
-#include <IBoneSceneNode.h>
 #include "content_cao.h"
 #include "util/numeric.h" // For IntervalLimiter
 #include "util/serialize.h"
@@ -43,8 +42,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "map.h"
 #include "camera.h" // CameraModes
 #include "wieldmesh.h"
-#include "log.h"
 #include <algorithm>
+#include "client/renderingengine.h"
 
 class Settings;
 struct ToolCapabilities;
@@ -128,8 +127,7 @@ class TestCAO : public ClientActiveObject
 
        static ClientActiveObject* create(Client *client, ClientEnvironment *env);
 
-       void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
-                       IrrlichtDevice *irr);
+       void addToScene(ITextureSource *tsrc);
        void removeFromScene(bool permanent);
        void updateLight(u8 light_at_pos);
        v3s16 getLightPosition();
@@ -165,8 +163,7 @@ ClientActiveObject* TestCAO::create(Client *client, ClientEnvironment *env)
        return new TestCAO(client, env);
 }
 
-void TestCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
-                       IrrlichtDevice *irr)
+void TestCAO::addToScene(ITextureSource *tsrc)
 {
        if(m_node != NULL)
                return;
@@ -195,14 +192,14 @@ void TestCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
        // Add to mesh
        mesh->addMeshBuffer(buf);
        buf->drop();
-       m_node = smgr->addMeshSceneNode(mesh, NULL);
+       m_node = RenderingEngine::get_scene_manager()->addMeshSceneNode(mesh, NULL);
        mesh->drop();
        updateNodePos();
 }
 
 void TestCAO::removeFromScene(bool permanent)
 {
-       if(m_node == NULL)
+       if (!m_node)
                return;
 
        m_node->remove();
@@ -220,7 +217,7 @@ v3s16 TestCAO::getLightPosition()
 
 void TestCAO::updateNodePos()
 {
-       if(m_node == NULL)
+       if (!m_node)
                return;
 
        m_node->setPosition(m_position);
@@ -272,8 +269,7 @@ class ItemCAO : public ClientActiveObject
 
        static ClientActiveObject* create(Client *client, ClientEnvironment *env);
 
-       void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
-                       IrrlichtDevice *irr);
+       void addToScene(ITextureSource *tsrc);
        void removeFromScene(bool permanent);
        void updateLight(u8 light_at_pos);
        v3s16 getLightPosition();
@@ -287,8 +283,14 @@ class ItemCAO : public ClientActiveObject
 
        void initialize(const std::string &data);
 
-       aabb3f *getSelectionBox()
-               {return &m_selection_box;}
+
+       virtual bool getSelectionBox(aabb3f *toset) const
+       {
+               *toset = m_selection_box;
+               return true;
+       }
+
+
        v3f getPosition()
                {return m_position;}
        inline float getYaw() const
@@ -329,8 +331,7 @@ ClientActiveObject* ItemCAO::create(Client *client, ClientEnvironment *env)
        return new ItemCAO(client, env);
 }
 
-void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
-                       IrrlichtDevice *irr)
+void ItemCAO::addToScene(ITextureSource *tsrc)
 {
        if(m_node != NULL)
                return;
@@ -364,7 +365,7 @@ void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
        // Add to mesh
        mesh->addMeshBuffer(buf);
        buf->drop();
-       m_node = smgr->addMeshSceneNode(mesh, NULL);
+       m_node = RenderingEngine::get_scene_manager()->addMeshSceneNode(mesh, NULL);
        mesh->drop();
        updateNodePos();
 
@@ -377,16 +378,16 @@ void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
 
 void ItemCAO::removeFromScene(bool permanent)
 {
-       if(m_node == NULL)
+       if (!m_node)
                return;
 
        m_node->remove();
-       m_node = NULL;
+       m_node = nullptr;
 }
 
 void ItemCAO::updateLight(u8 light_at_pos)
 {
-       if(m_node == NULL)
+       if (!m_node)
                return;
 
        u8 li = decode_light(light_at_pos);
@@ -401,7 +402,7 @@ v3s16 ItemCAO::getLightPosition()
 
 void ItemCAO::updateNodePos()
 {
-       if(m_node == NULL)
+       if (!m_node)
                return;
 
        m_node->setPosition(m_position);
@@ -428,7 +429,7 @@ void ItemCAO::updateInfoText()
 
 void ItemCAO::updateTexture()
 {
-       if(m_node == NULL)
+       if (!m_node)
                return;
 
        // Create an inventory item to see what is its image
@@ -610,11 +611,14 @@ GenericCAO::~GenericCAO()
        removeFromScene(true);
 }
 
-aabb3f *GenericCAO::getSelectionBox()
+bool GenericCAO::getSelectionBox(aabb3f *toset) const
 {
-       if(!m_prop.is_visible || !m_is_visible || m_is_local_player || getParent() != NULL)
-               return NULL;
-       return &m_selection_box;
+       if (!m_prop.is_visible || !m_is_visible || m_is_local_player
+                       || getParent() != NULL){
+               return false;
+       }
+       *toset = m_selection_box;
+       return true;
 }
 
 v3f GenericCAO::getPosition()
@@ -663,7 +667,7 @@ void GenericCAO::setAttachments()
        updateAttachments();
 }
 
-ClientActiveObject* GenericCAO::getParent()
+ClientActiveObject* GenericCAO::getParent() const
 {
        ClientActiveObject *obj = NULL;
 
@@ -721,11 +725,9 @@ void GenericCAO::removeFromScene(bool permanent)
        }
 }
 
-void GenericCAO::addToScene(scene::ISceneManager *smgr,
-               ITextureSource *tsrc, IrrlichtDevice *irr)
+void GenericCAO::addToScene(ITextureSource *tsrc)
 {
-       m_smgr = smgr;
-       m_irr = irr;
+       m_smgr = RenderingEngine::get_scene_manager();
 
        if (getSceneNode() != NULL) {
                return;
@@ -739,7 +741,7 @@ void GenericCAO::addToScene(scene::ISceneManager *smgr,
 
        if (m_prop.visual == "sprite") {
                infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
-               m_spritenode = smgr->addBillboardSceneNode(
+               m_spritenode = RenderingEngine::get_scene_manager()->addBillboardSceneNode(
                                NULL, v2f(1, 1), v3f(0,0,0), -1);
                m_spritenode->grab();
                m_spritenode->setMaterialTexture(0,
@@ -802,7 +804,7 @@ void GenericCAO::addToScene(scene::ISceneManager *smgr,
                        mesh->addMeshBuffer(buf);
                        buf->drop();
                }
-               m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
+               m_meshnode = RenderingEngine::get_scene_manager()->addMeshSceneNode(mesh, NULL);
                m_meshnode->grab();
                mesh->drop();
                // Set it to use the materials of the meshbuffers directly.
@@ -812,7 +814,7 @@ void GenericCAO::addToScene(scene::ISceneManager *smgr,
        else if(m_prop.visual == "cube") {
                infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
                scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
-               m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
+               m_meshnode = RenderingEngine::get_scene_manager()->addMeshSceneNode(mesh, NULL);
                m_meshnode->grab();
                mesh->drop();
 
@@ -832,7 +834,8 @@ void GenericCAO::addToScene(scene::ISceneManager *smgr,
                scene::IAnimatedMesh *mesh = m_client->getMesh(m_prop.mesh);
                if(mesh)
                {
-                       m_animated_meshnode = smgr->addAnimatedMeshSceneNode(mesh, NULL);
+                       m_animated_meshnode = RenderingEngine::get_scene_manager()->
+                               addAnimatedMeshSceneNode(mesh, NULL);
                        m_animated_meshnode->grab();
                        mesh->drop(); // The scene node took hold of it
                        m_animated_meshnode->animateJoints(); // Needed for some animations
@@ -871,8 +874,8 @@ void GenericCAO::addToScene(scene::ISceneManager *smgr,
                        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 = new WieldMeshSceneNode(
+                       RenderingEngine::get_scene_manager(), -1);
                m_wield_meshnode->setItem(item, m_client);
 
                m_wield_meshnode->setScale(
@@ -892,8 +895,11 @@ void GenericCAO::addToScene(scene::ISceneManager *smgr,
        scene::ISceneNode *node = getSceneNode();
        if (node && m_prop.nametag != "" && !m_is_local_player) {
                // Add nametag
+               v3f pos;
+               pos.Y = m_prop.collisionbox.MaxEdge.Y + 0.3f;
                m_nametag = m_client->getCamera()->addNametag(node,
-                       m_prop.nametag, m_prop.nametag_color);
+                       m_prop.nametag, m_prop.nametag_color,
+                       pos);
        }
 
        updateNodePos();
@@ -940,7 +946,7 @@ void GenericCAO::updateLightNoCheck(u8 light_at_pos)
 
 v3s16 GenericCAO::getLightPosition()
 {
-       return floatToInt(m_position, BS);
+       return floatToInt(m_position + v3f(0, 0.5 * BS, 0), BS);
 }
 
 void GenericCAO::updateNodePos()
@@ -969,7 +975,7 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
                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);
+                       m_position = player->getPosition();
                        m_velocity = v3f(0,0,0);
                        m_acceleration = v3f(0,0,0);
                        pos_translator.vect_show = m_position;
@@ -1034,7 +1040,7 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
                }
        }
 
-       if(m_visuals_expired && m_smgr && m_irr){
+       if (m_visuals_expired && m_smgr) {
                m_visuals_expired = false;
 
                // Attachments, part 1: All attached objects must be unparented first,
@@ -1056,7 +1062,7 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
                }
 
                removeFromScene(false);
-               addToScene(m_smgr, m_client->tsrc(), m_irr);
+               addToScene(m_client->tsrc());
 
                // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
                for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
@@ -1155,13 +1161,13 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
                        updateTextures(m_previous_texture_modifier);
                }
        }
-       if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001)
+       if(!getParent() && fabs(m_prop.automatic_rotate) > 0.001)
        {
                m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
                updateNodePos();
        }
 
-       if (getParent() == NULL && m_prop.automatic_face_movement_dir &&
+       if (!getParent() && m_prop.automatic_face_movement_dir &&
                        (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
        {
                float optimal_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
@@ -1408,7 +1414,7 @@ void GenericCAO::updateTextures(std::string mod)
 
 void GenericCAO::updateAnimation()
 {
-       if(m_animated_meshnode == NULL)
+       if (!m_animated_meshnode)
                return;
 
        if (m_animated_meshnode->getStartFrame() != m_animation_range.X ||
@@ -1426,7 +1432,7 @@ void GenericCAO::updateAnimation()
 
 void GenericCAO::updateBonePosition()
 {
-       if(m_bone_position.empty() || m_animated_meshnode == NULL)
+       if(m_bone_position.empty() || !m_animated_meshnode)
                return;
 
        m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
@@ -1447,7 +1453,7 @@ void GenericCAO::updateBonePosition()
 void GenericCAO::updateAttachments()
 {
 
-       if (getParent() == NULL) { // Detach or don't attach
+       if (!getParent()) { // Detach or don't attach
                scene::ISceneNode *node = getSceneNode();
                if (node) {
                        v3f old_position = node->getAbsolutePosition();
@@ -1509,6 +1515,7 @@ void GenericCAO::processMessage(const std::string &data)
                if (m_is_local_player) {
                        LocalPlayer *player = m_env->getLocalPlayer();
                        player->makes_footstep_sound = m_prop.makes_footstep_sound;
+                       player->setCollisionbox(m_selection_box);
                }
 
                if ((m_is_player && !m_is_local_player) && m_prop.nametag == "")
@@ -1696,6 +1703,9 @@ void GenericCAO::processMessage(const std::string &data)
                m_prop.nametag_color = readARGB8(is);
                if (m_nametag != NULL) {
                        m_nametag->nametag_color = m_prop.nametag_color;
+                       v3f pos;
+                       pos.Y = m_prop.collisionbox.MaxEdge.Y + 0.3f;
+                       m_nametag->nametag_pos = pos;
                }
        } else if (cmd == GENERIC_CMD_SPAWN_INFANT) {
                u16 child_id = readU16(is);