]> git.lizzy.rs Git - minetest.git/blobdiff - src/content_cao.cpp
Allow the LUA API to set animations to meshes as well as the animation speed. Also...
[minetest.git] / src / content_cao.cpp
index c993f3f834e3f3f8720cbe1816ecb6917adbc7dd..4df238b249c551ced6bf3a38cf7137b4d8581895 100644 (file)
@@ -38,6 +38,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "localplayer.h"
 #include "util/numeric.h" // For IntervalLimiter
 #include "util/serialize.h"
+#include "util/mathconstants.h"
+#include "map.h"
+#include <IMeshManipulator.h>
+#include <IAnimatedMeshSceneNode.h>
+#include <IBoneSceneNode.h>
 
 class Settings;
 struct ToolCapabilities;
@@ -557,6 +562,7 @@ class GenericCAO : public ClientActiveObject
        IrrlichtDevice *m_irr;
        core::aabbox3d<f32> m_selection_box;
        scene::IMeshSceneNode *m_meshnode;
+       scene::IAnimatedMeshSceneNode *m_animated_meshnode;
        scene::IBillboardSceneNode *m_spritenode;
        scene::ITextSceneNode* m_textnode;
        v3f m_position;
@@ -591,6 +597,7 @@ class GenericCAO : public ClientActiveObject
                m_irr(NULL),
                m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
                m_meshnode(NULL),
+               m_animated_meshnode(NULL),
                m_spritenode(NULL),
                m_textnode(NULL),
                m_position(v3f(0,10*BS,0)),
@@ -680,6 +687,10 @@ class GenericCAO : public ClientActiveObject
                        m_meshnode->remove();
                        m_meshnode = NULL;
                }
+               if(m_animated_meshnode){
+                       m_animated_meshnode->remove();
+                       m_animated_meshnode = NULL;
+               }
                if(m_spritenode){
                        m_spritenode->remove();
                        m_spritenode = NULL;
@@ -692,7 +703,7 @@ class GenericCAO : public ClientActiveObject
                m_smgr = smgr;
                m_irr = irr;
 
-               if(m_meshnode != NULL || m_spritenode != NULL)
+               if(m_meshnode != NULL || m_animated_meshnode != NULL || m_spritenode != NULL)
                        return;
                
                m_visuals_expired = false;
@@ -788,15 +799,40 @@ class GenericCAO : public ClientActiveObject
                                        m_prop.visual_size.X));
                        u8 li = m_last_light;
                        setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
-               } else if(m_prop.visual == "wielditem"){
+               }
+               else if(m_prop.visual == "mesh"){
+                       infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
+                       scene::IAnimatedMesh *mesh = smgr->getMesh(m_prop.mesh.c_str());
+                       if(mesh)
+                       {
+                               m_animated_meshnode = smgr->addAnimatedMeshSceneNode(mesh, NULL);
+                               m_animated_meshnode->setMD2Animation(scene::EMAT_STAND);
+                               m_animated_meshnode->animateJoints(); // Needed for some animations
+                               m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
+                                               m_prop.visual_size.Y,
+                                               m_prop.visual_size.X));
+                               u8 li = m_last_light;
+                               setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
+                       }
+                       else
+                               errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
+               }
+               else if(m_prop.visual == "wielditem"){
                        infostream<<"GenericCAO::addToScene(): node"<<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_gamedef->idef();
                                ItemStack item(m_prop.textures[0], 1, 0, "", idef);
-                               scene::IMesh *mesh = item.getDefinition(idef).wield_mesh;
+                               scene::IMesh *item_mesh = item.getDefinition(idef).wield_mesh;
+                               
+                               // Copy mesh to be able to set unique vertex colors
+                               scene::IMeshManipulator *manip =
+                                               irr->getVideoDriver()->getMeshManipulator();
+                               scene::IMesh *mesh = manip->createMeshUniquePrimitives(item_mesh);
+
                                m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
+                               mesh->drop();
                                
                                m_meshnode->setScale(v3f(m_prop.visual_size.X/2,
                                                m_prop.visual_size.Y/2,
@@ -813,6 +849,8 @@ class GenericCAO : public ClientActiveObject
                scene::ISceneNode *node = NULL;
                if(m_spritenode)
                        node = m_spritenode;
+               else if(m_animated_meshnode)
+                       node = m_animated_meshnode;
                else if(m_meshnode)
                        node = m_meshnode;
                if(node && m_is_player && !m_is_local_player){
@@ -836,15 +874,21 @@ class GenericCAO : public ClientActiveObject
        {
                bool is_visible = (m_hp != 0);
                u8 li = decode_light(light_at_pos);
-               m_last_light = li;
-               video::SColor color(255,li,li,li);
-               if(m_meshnode){
-                       setMeshColor(m_meshnode->getMesh(), color);
-                       m_meshnode->setVisible(is_visible);
-               }
-               if(m_spritenode){
-                       m_spritenode->setColor(color);
-                       m_spritenode->setVisible(is_visible);
+               if(li != m_last_light){
+                       m_last_light = li;
+                       video::SColor color(255,li,li,li);
+                       if(m_meshnode){
+                               setMeshColor(m_meshnode->getMesh(), color);
+                               m_meshnode->setVisible(is_visible);
+                       }
+                       if(m_animated_meshnode){
+                               setMeshColor(m_animated_meshnode->getMesh(), color);
+                               m_animated_meshnode->setVisible(is_visible);
+                       }
+                       if(m_spritenode){
+                               m_spritenode->setColor(color);
+                               m_spritenode->setVisible(is_visible);
+                       }
                }
        }
 
@@ -861,6 +905,12 @@ class GenericCAO : public ClientActiveObject
                        rot.Y = -m_yaw;
                        m_meshnode->setRotation(rot);
                }
+               if(m_animated_meshnode){
+                       m_animated_meshnode->setPosition(pos_translator.vect_show);
+                       v3f rot = m_animated_meshnode->getRotation();
+                       rot.Y = -m_yaw;
+                       m_animated_meshnode->setRotation(rot);
+               }
                if(m_spritenode){
                        m_spritenode->setPosition(pos_translator.vect_show);
                }
@@ -874,6 +924,7 @@ class GenericCAO : public ClientActiveObject
                        m_visuals_expired = false;
                        removeFromScene();
                        addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
+                       updateAnimations();
                }
 
                if(m_prop.physical){
@@ -881,22 +932,24 @@ class GenericCAO : public ClientActiveObject
                        box.MinEdge *= BS;
                        box.MaxEdge *= BS;
                        collisionMoveResult moveresult;
-                       f32 pos_max_d = BS*0.25; // Distance per iteration
+                       f32 pos_max_d = BS*0.125; // Distance per iteration
+                       f32 stepheight = 0;
                        v3f p_pos = m_position;
                        v3f p_velocity = m_velocity;
+                       v3f p_acceleration = m_acceleration;
                        IGameDef *gamedef = env->getGameDef();
-                       moveresult = collisionMovePrecise(&env->getMap(), gamedef,
-                                       pos_max_d, box, dtime, p_pos, p_velocity);
+                       moveresult = collisionMoveSimple(&env->getMap(), gamedef,
+                                       pos_max_d, box, stepheight, dtime,
+                                       p_pos, p_velocity, p_acceleration);
                        // Apply results
                        m_position = p_pos;
                        m_velocity = p_velocity;
+                       m_acceleration = p_acceleration;
                        
                        bool is_end_position = moveresult.collides;
                        pos_translator.update(m_position, is_end_position, dtime);
                        pos_translator.translate(dtime);
                        updateNodePos();
-
-                       m_velocity += dtime * m_acceleration;
                } else {
                        m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
                        m_velocity += dtime * m_acceleration;
@@ -1006,6 +1059,31 @@ class GenericCAO : public ClientActiveObject
                                                tsrc->getTextureRaw(texturestring));
                        }
                }
+               if(m_animated_meshnode)
+               {
+                       if(m_prop.visual == "mesh")
+                       {
+                               for (u32 i = 0; i < m_prop.textures.size(); ++i)
+                               {
+                                       std::string texturestring = m_prop.textures[i];
+                                       if(texturestring == "")
+                                               continue; // Empty texture string means don't modify that material
+                                       texturestring += mod;
+                                       video::ITexture* texture = tsrc->getTextureRaw(texturestring);
+                                       if(!texture)
+                                       {
+                                               errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
+                                               continue;
+                                       }
+
+                                       // Set material flags and texture
+                                       m_animated_meshnode->setMaterialTexture(i, texture);
+                                       video::SMaterial& material = m_animated_meshnode->getMaterial(i);
+                                       material.setFlag(video::EMF_LIGHTING, false);
+                                       material.setFlag(video::EMF_BILINEAR_FILTER, false);
+                               }
+                       }
+               }
                if(m_meshnode)
                {
                        if(m_prop.visual == "cube")
@@ -1059,6 +1137,25 @@ class GenericCAO : public ClientActiveObject
                }
        }
 
+       void updateAnimations()
+       {
+               if(!m_animated_meshnode)
+                       return;
+
+               m_animated_meshnode->setFrameLoop(m_prop.animation_frames.X, m_prop.animation_frames.Y);
+               m_animated_meshnode->setAnimationSpeed(m_prop.animation_speed);
+               m_animated_meshnode->setTransitionTime(m_prop.animation_blend);
+
+               for(std::map<std::string, v3f>::const_iterator ii = m_prop.animation_bone_position.begin(); ii != m_prop.animation_bone_position.end(); ++ii){
+                       if((*ii).second.X || (*ii).second.Y || (*ii).second.Z) { }
+                       // Bone positioning code will go here
+               }
+               for(std::map<std::string, v3f>::const_iterator ii = m_prop.animation_bone_rotation.begin(); ii != m_prop.animation_bone_rotation.end(); ++ii){
+                       if((*ii).second.X || (*ii).second.Y || (*ii).second.Z) { }
+                       // Bone rotation code will go here
+               }
+       }
+
        void processMessage(const std::string &data)
        {
                //infostream<<"GenericCAO: Got message"<<std::endl;