Fix for bug #28979 "Action actuator breaks animation" reported by Goran Milovanovic...
[blender.git] / source / gameengine / Ketsji / BL_Action.cpp
index 1741e74a771688853b571c7bcaa0e1b0c37627bc..9e8f53ad21980c17712689d60b6188f1c2423e7a 100644 (file)
  * along with this program; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
- * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
- * All rights reserved.
- *
- * The Original Code is: all of this file.
- *
- * Contributor(s): none yet.
+ * Contributor(s): Mitchell Stokes.
  *
  * ***** END GPL LICENSE BLOCK *****
  */
 
+/** \file BL_Action.cpp
+ *  \ingroup ketsji
+ */
+
 #include <cstdlib>
 
 #include "BL_Action.h"
@@ -50,24 +49,25 @@ extern "C" {
 
 BL_Action::BL_Action(class KX_GameObject* gameobj)
 :
+       m_action(NULL),
+       m_pose(NULL),
+       m_blendpose(NULL),
+       m_blendinpose(NULL),
+       m_ptrrna(NULL),
        m_obj(gameobj),
        m_startframe(0.f),
        m_endframe(0.f),
-       m_blendin(0.f),
-       m_playmode(0),
        m_endtime(0.f),
        m_localtime(0.f),
+       m_blendin(0.f),
        m_blendframe(0.f),
        m_blendstart(0.f),
        m_speed(0.f),
        m_priority(0),
+       m_playmode(0),
        m_ipo_flags(0),
-       m_pose(NULL),
-       m_blendpose(NULL),
-       m_sg_contr(NULL),
-       m_ptrrna(NULL),
        m_done(true),
-       m_bcalc_local_time(true)
+       m_calc_localtime(true)
 {
        if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE)
        {
@@ -95,22 +95,33 @@ BL_Action::~BL_Action()
                game_free_pose(m_pose);
        if (m_blendpose)
                game_free_pose(m_blendpose);
-       if (m_sg_contr)
-       {
-               m_obj->GetSGNode()->RemoveSGController(m_sg_contr);
-               delete m_sg_contr;
-       }
+       if (m_blendinpose)
+               game_free_pose(m_blendinpose);
        if (m_ptrrna)
                delete m_ptrrna;
+       ClearControllerList();
 }
 
-void BL_Action::Play(const char* name,
+void BL_Action::ClearControllerList()
+{
+       // Clear out the controller list
+       std::vector<SG_Controller*>::iterator it;
+       for (it = m_sg_contr_list.begin(); it != m_sg_contr_list.end(); it++)
+       {
+               m_obj->GetSGNode()->RemoveSGController((*it));
+               delete *it;
+       }
+
+       m_sg_contr_list.clear();
+}
+
+bool BL_Action::Play(const char* name,
                                        float start,
                                        float end,
                                        short priority,
                                        float blendin,
                                        short play_mode,
-                                       short blend_mode,
+                                       float layer_weight,
                                        short ipo_flags,
                                        float playback_speed)
 {
@@ -118,7 +129,7 @@ void BL_Action::Play(const char* name,
        // Only start playing a new action if we're done, or if
        // the new action has a higher priority
        if (priority != 0 && !IsDone() && priority >= m_priority)
-               return;
+               return false;
        m_priority = priority;
        bAction* prev_action = m_action;
 
@@ -128,15 +139,35 @@ void BL_Action::Play(const char* name,
        {
                printf("Failed to load action: %s\n", name);
                m_done = true;
-               return;
+               return false;
        }
 
        if (prev_action != m_action)
        {
+               // First get rid of any old controllers
+               ClearControllerList();
+
                // Create an SG_Controller
-               m_sg_contr = BL_CreateIPO(m_action, m_obj, KX_GetActiveScene()->GetSceneConverter());
-               m_obj->GetSGNode()->AddSGController(m_sg_contr);
-               m_sg_contr->SetObject(m_obj->GetSGNode());
+               SG_Controller *sg_contr = BL_CreateIPO(m_action, m_obj, KX_GetActiveScene()->GetSceneConverter());
+               m_sg_contr_list.push_back(sg_contr);
+               m_obj->GetSGNode()->AddSGController(sg_contr);
+               sg_contr->SetObject(m_obj->GetSGNode());
+
+               // Extra controllers
+               if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_LIGHT)
+               {
+                       sg_contr = BL_CreateLampIPO(m_action, m_obj, KX_GetActiveScene()->GetSceneConverter());
+                       m_sg_contr_list.push_back(sg_contr);
+                       m_obj->GetSGNode()->AddSGController(sg_contr);
+                       sg_contr->SetObject(m_obj->GetSGNode());
+               }
+               else if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_CAMERA)
+               {
+                       sg_contr = BL_CreateCameraIPO(m_action, m_obj, KX_GetActiveScene()->GetSceneConverter());
+                       m_sg_contr_list.push_back(sg_contr);
+                       m_obj->GetSGNode()->AddSGController(sg_contr);
+                       sg_contr->SetObject(m_obj->GetSGNode());
+               }
        }
        
        m_ipo_flags = ipo_flags;
@@ -145,25 +176,24 @@ void BL_Action::Play(const char* name,
        // Setup blendin shapes/poses
        if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE)
        {
-               if (!m_blendpose)
-               {
-                       BL_ArmatureObject *obj = (BL_ArmatureObject*)m_obj;
-                       obj->GetMRDPose(&m_blendpose);
-               }
+               BL_ArmatureObject *obj = (BL_ArmatureObject*)m_obj;
+               obj->GetMRDPose(&m_blendinpose);
        }
        else
        {
                BL_DeformableGameObject *obj = (BL_DeformableGameObject*)m_obj;
                BL_ShapeDeformer *shape_deformer = dynamic_cast<BL_ShapeDeformer*>(obj->GetDeformer());
                
-               obj->GetShape(m_blendshape);
-
-               // Now that we have the previous blend shape saved, we can clear out the key to avoid any
-               // further interference.
-               KeyBlock *kb;
-               for (kb=(KeyBlock*)shape_deformer->GetKey()->block.first; kb; kb=(KeyBlock*)kb->next)
-                       kb->curval = 0.f;
+               if (shape_deformer && shape_deformer->GetKey())
+               {
+                       obj->GetShape(m_blendinshape);
 
+                       // Now that we have the previous blend shape saved, we can clear out the key to avoid any
+                       // further interference.
+                       KeyBlock *kb;
+                       for (kb=(KeyBlock*)shape_deformer->GetKey()->block.first; kb; kb=(KeyBlock*)kb->next)
+                               kb->curval = 0.f;
+               }
        }
 
        // Now that we have an action, we have something we can play
@@ -176,8 +206,11 @@ void BL_Action::Play(const char* name,
        m_blendframe = 0.f;
        m_blendstart = 0.f;
        m_speed = playback_speed;
+       m_layer_weight = layer_weight;
        
        m_done = false;
+
+       return true;
 }
 
 void BL_Action::Stop()
@@ -185,13 +218,27 @@ void BL_Action::Stop()
        m_done = true;
 }
 
+bool BL_Action::IsDone()
+{
+       return m_done;
+}
+
 void BL_Action::InitIPO()
 {
-               // Initialize the IPO
-               m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_RESET, true);
-               m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_IPO_AS_FORCE, m_ipo_flags & ACT_IPOFLAG_FORCE);
-               m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_IPO_ADD, m_ipo_flags & ACT_IPOFLAG_ADD);
-               m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_LOCAL, m_ipo_flags & ACT_IPOFLAG_LOCAL);
+       // Initialize the IPOs
+       std::vector<SG_Controller*>::iterator it;
+       for (it = m_sg_contr_list.begin(); it != m_sg_contr_list.end(); it++)
+       {
+               (*it)->SetOption(SG_Controller::SG_CONTR_IPO_RESET, true);
+               (*it)->SetOption(SG_Controller::SG_CONTR_IPO_IPO_AS_FORCE, m_ipo_flags & ACT_IPOFLAG_FORCE);
+               (*it)->SetOption(SG_Controller::SG_CONTR_IPO_IPO_ADD, m_ipo_flags & ACT_IPOFLAG_ADD);
+               (*it)->SetOption(SG_Controller::SG_CONTR_IPO_LOCAL, m_ipo_flags & ACT_IPOFLAG_LOCAL);
+       }
+}
+
+bAction *BL_Action::GetAction()
+{
+       return (IsDone()) ? NULL : m_action;
 }
 
 float BL_Action::GetFrame()
@@ -206,9 +253,20 @@ void BL_Action::SetFrame(float frame)
                frame = min(m_startframe, m_endframe);
        else if (frame > max(m_startframe, m_endframe))
                frame = max(m_startframe, m_endframe);
-
+       
        m_localtime = frame;
-       m_bcalc_local_time = false;
+       m_calc_localtime = false;
+}
+
+void BL_Action::SetPlayMode(short play_mode)
+{
+       m_playmode = play_mode;
+}
+
+void BL_Action::SetTimes(float start, float end)
+{
+       m_startframe = start;
+       m_endframe = end;
 }
 
 void BL_Action::SetLocalTime(float curtime)
@@ -221,6 +279,14 @@ void BL_Action::SetLocalTime(float curtime)
        m_localtime = m_startframe + dt;
 }
 
+void BL_Action::ResetStartTime(float curtime)
+{
+       float dt = m_localtime - m_startframe;
+
+       m_starttime = curtime - dt / (KX_KetsjiEngine::GetAnimFrameRate()*m_speed);
+       SetLocalTime(curtime);
+}
+
 void BL_Action::IncrementBlending(float curtime)
 {
        // Setup m_blendstart if we need to
@@ -236,7 +302,7 @@ void BL_Action::IncrementBlending(float curtime)
 }
 
 
-void BL_Action::BlendShape(Key* key, float srcweight)
+void BL_Action::BlendShape(Key* key, float srcweight, std::vector<float>& blendshape)
 {
        vector<float>::const_iterator it;
        float dstweight;
@@ -244,8 +310,8 @@ void BL_Action::BlendShape(Key* key, float srcweight)
        
        dstweight = 1.0F - srcweight;
        //printf("Dst: %f\tSrc: %f\n", srcweight, dstweight);
-       for (it=m_blendshape.begin(), kb = (KeyBlock*)key->block.first; 
-                kb && it != m_blendshape.end(); 
+       for (it=blendshape.begin(), kb = (KeyBlock*)key->block.first; 
+                kb && it != blendshape.end(); 
                 kb = (KeyBlock*)kb->next, it++) {
                //printf("OirgKeys: %f\t%f\n", kb->curval, (*it));
                kb->curval = kb->curval * dstweight + (*it) * srcweight;
@@ -260,15 +326,14 @@ void BL_Action::Update(float curtime)
        if (m_done)
                return;
 
-       // We only want to calculate the current time if we weren't given a frame (e.g., from SetFrame())
-       if (m_bcalc_local_time)
-       {
-               curtime -= KX_KetsjiEngine::GetSuspendedDelta();
+       curtime -= KX_KetsjiEngine::GetSuspendedDelta();
+
+       if (m_calc_localtime)
                SetLocalTime(curtime);
-       }
        else
        {
-               m_bcalc_local_time = true;
+               ResetStartTime(curtime);
+               m_calc_localtime = true;
        }
 
        // Handle wrap around
@@ -296,9 +361,6 @@ void BL_Action::Update(float curtime)
 
                        break;
                }
-
-               if (!m_done && m_sg_contr)
-                       InitIPO();
        }
 
        if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE)
@@ -326,7 +388,15 @@ void BL_Action::Update(float curtime)
                        float weight = 1.f - (m_blendframe/m_blendin);
 
                        // Blend the poses
-                       game_blend_poses(m_pose, m_blendpose, weight);
+                       game_blend_poses(m_pose, m_blendinpose, weight);
+               }
+
+
+               // Handle layer blending
+               if (m_layer_weight >= 0)
+               {
+                       obj->GetMRDPose(&m_blendpose);
+                       game_blend_poses(m_pose, m_blendpose, m_layer_weight);
                }
 
                obj->SetPose(m_pose);
@@ -339,7 +409,7 @@ void BL_Action::Update(float curtime)
                BL_ShapeDeformer *shape_deformer = dynamic_cast<BL_ShapeDeformer*>(obj->GetDeformer());
 
                // Handle shape actions if we have any
-               if (shape_deformer)
+               if (shape_deformer && shape_deformer->GetKey())
                {
                        Key *key = shape_deformer->GetKey();
 
@@ -360,14 +430,19 @@ void BL_Action::Update(float curtime)
                                        kb->curval = 0.f;
 
                                // Now blend the shape
-                               BlendShape(key, weight);
+                               BlendShape(key, weight, m_blendinshape);
                        }
 
-                       obj->SetActiveAction(NULL, 0, m_localtime);
-               }
+                       // Handle layer blending
+                       if (m_layer_weight >= 0)
+                       {
+                               obj->GetShape(m_blendshape);
+                               BlendShape(key, m_layer_weight, m_blendshape);
+                       }
 
+                       obj->SetActiveAction(NULL, 0, curtime);
+               }
 
-               InitIPO();
                m_obj->UpdateIPO(m_localtime, m_ipo_flags & ACT_IPOFLAG_CHILD);
        }
 }