BGE Animations: Fixing a bug with priority and non continuous animations.
[blender.git] / source / gameengine / Ketsji / BL_Action.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <cstdlib>
31
32 #include "BL_Action.h"
33 #include "BL_ArmatureObject.h"
34 #include "BL_DeformableGameObject.h"
35 #include "BL_ShapeDeformer.h"
36 #include "KX_IpoConvert.h"
37 #include "KX_GameObject.h"
38
39 // These three are for getting the action from the logic manager
40 #include "KX_Scene.h"
41 #include "KX_PythonInit.h"
42 #include "SCA_LogicManager.h"
43
44 extern "C" {
45 #include "BKE_animsys.h"
46 #include "BKE_action.h"
47 #include "RNA_access.h"
48 #include "RNA_define.h"
49 }
50
51 BL_Action::BL_Action(class KX_GameObject* gameobj)
52 :
53         m_obj(gameobj),
54         m_startframe(0.f),
55         m_endframe(0.f),
56         m_blendin(0.f),
57         m_playmode(0),
58         m_endtime(0.f),
59         m_localtime(0.f),
60         m_blendframe(0.f),
61         m_blendstart(0.f),
62         m_speed(0.f),
63         m_priority(0),
64         m_ipo_flags(0),
65         m_pose(NULL),
66         m_blendpose(NULL),
67         m_sg_contr(NULL),
68         m_ptrrna(NULL),
69         m_done(true),
70         m_bcalc_local_time(true)
71 {
72         if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE)
73         {
74                 BL_ArmatureObject *obj = (BL_ArmatureObject*)m_obj;
75
76                 m_ptrrna = new PointerRNA();
77                 RNA_id_pointer_create(&obj->GetArmatureObject()->id, m_ptrrna);
78         }
79         else
80         {
81                 BL_DeformableGameObject *obj = (BL_DeformableGameObject*)m_obj;
82                 BL_ShapeDeformer *shape_deformer = dynamic_cast<BL_ShapeDeformer*>(obj->GetDeformer());
83
84                 if (shape_deformer)
85                 {
86                         m_ptrrna = new PointerRNA();
87                         RNA_id_pointer_create(&shape_deformer->GetKey()->id, m_ptrrna);
88                 }
89         }
90 }
91
92 BL_Action::~BL_Action()
93 {
94         if (m_pose)
95                 game_free_pose(m_pose);
96         if (m_blendpose)
97                 game_free_pose(m_blendpose);
98         if (m_sg_contr)
99         {
100                 m_obj->GetSGNode()->RemoveSGController(m_sg_contr);
101                 delete m_sg_contr;
102         }
103         if (m_ptrrna)
104                 delete m_ptrrna;
105 }
106
107 bool BL_Action::Play(const char* name,
108                                         float start,
109                                         float end,
110                                         short priority,
111                                         float blendin,
112                                         short play_mode,
113                                         short blend_mode,
114                                         short ipo_flags,
115                                         float playback_speed)
116 {
117
118         // Only start playing a new action if we're done, or if
119         // the new action has a higher priority
120         if (priority != 0 && !IsDone() && priority >= m_priority)
121                 return false;
122         m_priority = priority;
123         bAction* prev_action = m_action;
124
125         // First try to load the action
126         m_action = (bAction*)KX_GetActiveScene()->GetLogicManager()->GetActionByName(name);
127         if (!m_action)
128         {
129                 printf("Failed to load action: %s\n", name);
130                 m_done = true;
131                 return false;
132         }
133
134         if (prev_action != m_action)
135         {
136                 // Create an SG_Controller
137                 m_sg_contr = BL_CreateIPO(m_action, m_obj, KX_GetActiveScene()->GetSceneConverter());
138                 m_obj->GetSGNode()->AddSGController(m_sg_contr);
139                 m_sg_contr->SetObject(m_obj->GetSGNode());
140         }
141         
142         m_ipo_flags = ipo_flags;
143         InitIPO();
144
145         // Setup blendin shapes/poses
146         if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE)
147         {
148                 if (!m_blendpose)
149                 {
150                         BL_ArmatureObject *obj = (BL_ArmatureObject*)m_obj;
151                         obj->GetMRDPose(&m_blendpose);
152                 }
153         }
154         else
155         {
156                 BL_DeformableGameObject *obj = (BL_DeformableGameObject*)m_obj;
157                 BL_ShapeDeformer *shape_deformer = dynamic_cast<BL_ShapeDeformer*>(obj->GetDeformer());
158                 
159                 obj->GetShape(m_blendshape);
160
161                 // Now that we have the previous blend shape saved, we can clear out the key to avoid any
162                 // further interference.
163                 KeyBlock *kb;
164                 for (kb=(KeyBlock*)shape_deformer->GetKey()->block.first; kb; kb=(KeyBlock*)kb->next)
165                         kb->curval = 0.f;
166
167         }
168
169         // Now that we have an action, we have something we can play
170         m_starttime = KX_GetActiveEngine()->GetFrameTime();
171         m_startframe = m_localtime = start;
172         m_endframe = end;
173         m_blendin = blendin;
174         m_playmode = play_mode;
175         m_endtime = 0.f;
176         m_blendframe = 0.f;
177         m_blendstart = 0.f;
178         m_speed = playback_speed;
179         
180         m_done = false;
181
182         return true;
183 }
184
185 void BL_Action::Stop()
186 {
187         m_done = true;
188 }
189
190 void BL_Action::InitIPO()
191 {
192                 // Initialize the IPO
193                 m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_RESET, true);
194                 m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_IPO_AS_FORCE, m_ipo_flags & ACT_IPOFLAG_FORCE);
195                 m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_IPO_ADD, m_ipo_flags & ACT_IPOFLAG_ADD);
196                 m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_LOCAL, m_ipo_flags & ACT_IPOFLAG_LOCAL);
197 }
198
199 float BL_Action::GetFrame()
200 {
201         return m_localtime;
202 }
203
204 void BL_Action::SetFrame(float frame)
205 {
206         // Clamp the frame to the start and end frame
207         if (frame < min(m_startframe, m_endframe))
208                 frame = min(m_startframe, m_endframe);
209         else if (frame > max(m_startframe, m_endframe))
210                 frame = max(m_startframe, m_endframe);
211
212         m_localtime = frame;
213         m_bcalc_local_time = false;
214 }
215
216 void BL_Action::SetLocalTime(float curtime)
217 {
218         float dt = (curtime-m_starttime)*KX_KetsjiEngine::GetAnimFrameRate()*m_speed;
219
220         if (m_endframe < m_startframe)
221                 dt = -dt;
222
223         m_localtime = m_startframe + dt;
224 }
225
226 void BL_Action::IncrementBlending(float curtime)
227 {
228         // Setup m_blendstart if we need to
229         if (m_blendstart == 0.f)
230                 m_blendstart = curtime;
231         
232         // Bump the blend frame
233         m_blendframe = (curtime - m_blendstart)*KX_KetsjiEngine::GetAnimFrameRate();
234
235         // Clamp
236         if (m_blendframe>m_blendin)
237                 m_blendframe = m_blendin;
238 }
239
240
241 void BL_Action::BlendShape(Key* key, float srcweight)
242 {
243         vector<float>::const_iterator it;
244         float dstweight;
245         KeyBlock *kb;
246         
247         dstweight = 1.0F - srcweight;
248         //printf("Dst: %f\tSrc: %f\n", srcweight, dstweight);
249         for (it=m_blendshape.begin(), kb = (KeyBlock*)key->block.first; 
250                  kb && it != m_blendshape.end(); 
251                  kb = (KeyBlock*)kb->next, it++) {
252                 //printf("OirgKeys: %f\t%f\n", kb->curval, (*it));
253                 kb->curval = kb->curval * dstweight + (*it) * srcweight;
254                 //printf("NewKey: %f\n", kb->curval);
255         }
256         //printf("\n");
257 }
258
259 void BL_Action::Update(float curtime)
260 {
261         // Don't bother if we're done with the animation
262         if (m_done)
263                 return;
264
265         // We only want to calculate the current time if we weren't given a frame (e.g., from SetFrame())
266         if (m_bcalc_local_time)
267         {
268                 curtime -= KX_KetsjiEngine::GetSuspendedDelta();
269                 SetLocalTime(curtime);
270         }
271         else
272         {
273                 m_bcalc_local_time = true;
274         }
275
276         // Handle wrap around
277         if (m_localtime < min(m_startframe, m_endframe) || m_localtime > max(m_startframe, m_endframe))
278         {
279                 switch(m_playmode)
280                 {
281                 case ACT_MODE_PLAY:
282                         // Clamp
283                         m_localtime = m_endframe;
284                         m_done = true;
285                         break;
286                 case ACT_MODE_LOOP:
287                         // Put the time back to the beginning
288                         m_localtime = m_startframe;
289                         m_starttime = curtime;
290                         break;
291                 case ACT_MODE_PING_PONG:
292                         // Swap the start and end frames
293                         float temp = m_startframe;
294                         m_startframe = m_endframe;
295                         m_endframe = temp;
296
297                         m_starttime = curtime;
298
299                         break;
300                 }
301
302                 if (!m_done && m_sg_contr)
303                         InitIPO();
304         }
305
306         if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE)
307         {
308                 BL_ArmatureObject *obj = (BL_ArmatureObject*)m_obj;
309                 obj->GetPose(&m_pose);
310
311                 // Extract the pose from the action
312                 {
313                         Object *arm = obj->GetArmatureObject();
314                         bPose *temp = arm->pose;
315
316                         arm->pose = m_pose;
317                         animsys_evaluate_action(m_ptrrna, m_action, NULL, m_localtime);
318
319                         arm->pose = temp;
320                 }
321
322                 // Handle blending between armature actions
323                 if (m_blendin && m_blendframe<m_blendin)
324                 {
325                         IncrementBlending(curtime);
326
327                         // Calculate weight
328                         float weight = 1.f - (m_blendframe/m_blendin);
329
330                         // Blend the poses
331                         game_blend_poses(m_pose, m_blendpose, weight);
332                 }
333
334
335                 // Handle layer blending
336                 
337                 obj->SetPose(m_pose);
338
339                 obj->SetActiveAction(NULL, 0, curtime);
340         }
341         else
342         {
343                 BL_DeformableGameObject *obj = (BL_DeformableGameObject*)m_obj;
344                 BL_ShapeDeformer *shape_deformer = dynamic_cast<BL_ShapeDeformer*>(obj->GetDeformer());
345
346                 // Handle shape actions if we have any
347                 if (shape_deformer)
348                 {
349                         Key *key = shape_deformer->GetKey();
350
351
352                         animsys_evaluate_action(m_ptrrna, m_action, NULL, m_localtime);
353
354                         // Handle blending between shape actions
355                         if (m_blendin && m_blendframe < m_blendin)
356                         {
357                                 IncrementBlending(curtime);
358
359                                 float weight = 1.f - (m_blendframe/m_blendin);
360
361                                 // We go through and clear out the keyblocks so there isn't any interference
362                                 // from other shape actions
363                                 KeyBlock *kb;
364                                 for (kb=(KeyBlock*)key->block.first; kb; kb=(KeyBlock*)kb->next)
365                                         kb->curval = 0.f;
366
367                                 // Now blend the shape
368                                 BlendShape(key, weight);
369                         }
370
371                         obj->SetActiveAction(NULL, 0, m_localtime);
372                 }
373
374
375                 InitIPO();
376                 m_obj->UpdateIPO(m_localtime, m_ipo_flags & ACT_IPOFLAG_CHILD);
377         }
378 }