9bae119107e72934487dd4e40af582da2ccc19f9
[blender.git] / source / gameengine / Ketsji / BL_Action.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Mitchell Stokes.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file BL_Action.cpp
24  *  \ingroup ketsji
25  */
26
27 #include <cstdlib>
28 #include <stdio.h>
29
30 #include "BL_Action.h"
31 #include "BL_ArmatureObject.h"
32 #include "BL_DeformableGameObject.h"
33 #include "BL_ShapeDeformer.h"
34 #include "KX_IpoConvert.h"
35 #include "KX_GameObject.h"
36
37 #include "SG_Controller.h"
38
39 // These three are for getting the action from the logic manager
40 #include "KX_Scene.h"
41 #include "SCA_LogicManager.h"
42
43 extern "C" {
44 #include "BKE_animsys.h"
45 #include "BKE_action.h"
46 #include "RNA_access.h"
47 #include "RNA_define.h"
48
49 // Needed for material IPOs
50 #include "BKE_material.h"
51 #include "DNA_material_types.h"
52 #include "DNA_scene_types.h"
53 }
54
55 #include "MEM_guardedalloc.h"
56 #include "BKE_library.h"
57 #include "BKE_global.h"
58
59 BL_Action::BL_Action(class KX_GameObject* gameobj)
60 :
61         m_action(NULL),
62         m_tmpaction(NULL),
63         m_blendpose(NULL),
64         m_blendinpose(NULL),
65         m_obj(gameobj),
66         m_startframe(0.f),
67         m_endframe(0.f),
68         m_endtime(0.f),
69         m_localtime(0.f),
70         m_blendin(0.f),
71         m_blendframe(0.f),
72         m_blendstart(0.f),
73         m_speed(0.f),
74         m_priority(0),
75         m_playmode(ACT_MODE_PLAY),
76         m_blendmode(ACT_BLEND_BLEND),
77         m_ipo_flags(0),
78         m_done(true),
79         m_calc_localtime(true)
80 {
81 }
82
83 BL_Action::~BL_Action()
84 {
85         if (m_blendpose)
86                 BKE_pose_free(m_blendpose);
87         if (m_blendinpose)
88                 BKE_pose_free(m_blendinpose);
89         ClearControllerList();
90
91         if (m_tmpaction) {
92                 BKE_libblock_free(G.main, m_tmpaction);
93                 m_tmpaction = NULL;
94         }
95 }
96
97 void BL_Action::ClearControllerList()
98 {
99         // Clear out the controller list
100         std::vector<SG_Controller*>::iterator it;
101         for (it = m_sg_contr_list.begin(); it != m_sg_contr_list.end(); it++)
102         {
103                 m_obj->GetSGNode()->RemoveSGController((*it));
104                 delete *it;
105         }
106
107         m_sg_contr_list.clear();
108 }
109
110 bool BL_Action::Play(const char* name,
111                                         float start,
112                                         float end,
113                                         short priority,
114                                         float blendin,
115                                         short play_mode,
116                                         float layer_weight,
117                                         short ipo_flags,
118                                         float playback_speed,
119                                         short blend_mode)
120 {
121
122         // Only start playing a new action if we're done, or if
123         // the new action has a higher priority
124         if (!IsDone() && priority > m_priority)
125                 return false;
126         m_priority = priority;
127         bAction* prev_action = m_action;
128
129         KX_Scene* kxscene = m_obj->GetScene();
130
131         // First try to load the action
132         m_action = (bAction*)kxscene->GetLogicManager()->GetActionByName(name);
133         if (!m_action)
134         {
135                 printf("Failed to load action: %s\n", name);
136                 m_done = true;
137                 return false;
138         }
139
140         // If we have the same settings, don't play again
141         // This is to resolve potential issues with pulses on sensors such as the ones
142         // reported in bug #29412. The fix is here so it works for both logic bricks and Python.
143         // However, this may eventually lead to issues where a user wants to override an already
144         // playing action with the same action and settings. If this becomes an issue,
145         // then this fix may have to be re-evaluated.
146         if (!IsDone() && m_action == prev_action && m_startframe == start && m_endframe == end
147                         && m_priority == priority && m_speed == playback_speed)
148                 return false;
149
150         // Keep a copy of the action for threading purposes
151         if (m_tmpaction) {
152                 BKE_libblock_free(G.main, m_tmpaction);
153                 m_tmpaction = NULL;
154         }
155         m_tmpaction = BKE_action_copy(m_action);
156
157         // First get rid of any old controllers
158         ClearControllerList();
159
160         // Create an SG_Controller
161         SG_Controller *sg_contr = BL_CreateIPO(m_action, m_obj, kxscene->GetSceneConverter());
162         m_sg_contr_list.push_back(sg_contr);
163         m_obj->GetSGNode()->AddSGController(sg_contr);
164         sg_contr->SetObject(m_obj->GetSGNode());
165
166         // World
167         sg_contr = BL_CreateWorldIPO(m_action, kxscene->GetBlenderScene()->world, kxscene->GetSceneConverter());
168         if (sg_contr) {
169                 m_sg_contr_list.push_back(sg_contr);
170                 m_obj->GetSGNode()->AddSGController(sg_contr);
171                 sg_contr->SetObject(m_obj->GetSGNode());
172         }
173
174         // Try obcolor
175         sg_contr = BL_CreateObColorIPO(m_action, m_obj, kxscene->GetSceneConverter());
176         if (sg_contr) {
177                 m_sg_contr_list.push_back(sg_contr);
178                 m_obj->GetSGNode()->AddSGController(sg_contr);
179                 sg_contr->SetObject(m_obj->GetSGNode());
180         }
181
182         // Now try materials
183         if (m_obj->GetBlenderObject()->totcol==1) {
184                 Material *mat = give_current_material(m_obj->GetBlenderObject(), 1);
185                 if (mat) {
186                         sg_contr = BL_CreateMaterialIpo(m_action, mat, 0, m_obj, kxscene->GetSceneConverter());
187                         if (sg_contr) {
188                                 m_sg_contr_list.push_back(sg_contr);
189                                 m_obj->GetSGNode()->AddSGController(sg_contr);
190                                 sg_contr->SetObject(m_obj->GetSGNode());
191                         }
192                 }
193         } else {
194                 Material *mat;
195                 STR_HashedString matname;
196
197                 for (int matidx = 1; matidx <= m_obj->GetBlenderObject()->totcol; ++matidx) {
198                         mat = give_current_material(m_obj->GetBlenderObject(), matidx);
199                         if (mat) {
200                                 matname = mat->id.name;
201                                 sg_contr = BL_CreateMaterialIpo(m_action, mat, matname.hash(), m_obj, kxscene->GetSceneConverter());
202                                 if (sg_contr) {
203                                         m_sg_contr_list.push_back(sg_contr);
204                                         m_obj->GetSGNode()->AddSGController(sg_contr);
205                                         sg_contr->SetObject(m_obj->GetSGNode());
206                                 }
207                         }
208                 }
209         }
210
211         // Extra controllers
212         if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_LIGHT)
213         {
214                 sg_contr = BL_CreateLampIPO(m_action, m_obj, kxscene->GetSceneConverter());
215                 m_sg_contr_list.push_back(sg_contr);
216                 m_obj->GetSGNode()->AddSGController(sg_contr);
217                 sg_contr->SetObject(m_obj->GetSGNode());
218         }
219         else if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_CAMERA)
220         {
221                 sg_contr = BL_CreateCameraIPO(m_action, m_obj, kxscene->GetSceneConverter());
222                 m_sg_contr_list.push_back(sg_contr);
223                 m_obj->GetSGNode()->AddSGController(sg_contr);
224                 sg_contr->SetObject(m_obj->GetSGNode());
225         }
226         
227         m_ipo_flags = ipo_flags;
228         InitIPO();
229
230         // Setup blendin shapes/poses
231         if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE)
232         {
233                 BL_ArmatureObject *obj = (BL_ArmatureObject*)m_obj;
234                 obj->GetPose(&m_blendinpose);
235         }
236         else
237         {
238                 BL_DeformableGameObject *obj = (BL_DeformableGameObject*)m_obj;
239                 BL_ShapeDeformer *shape_deformer = dynamic_cast<BL_ShapeDeformer*>(obj->GetDeformer());
240                 
241                 if (shape_deformer && shape_deformer->GetKey())
242                 {
243                         obj->GetShape(m_blendinshape);
244
245                         // Now that we have the previous blend shape saved, we can clear out the key to avoid any
246                         // further interference.
247                         KeyBlock *kb;
248                         for (kb=(KeyBlock *)shape_deformer->GetKey()->block.first; kb; kb=(KeyBlock *)kb->next)
249                                 kb->curval = 0.f;
250                 }
251         }
252
253         // Now that we have an action, we have something we can play
254         m_starttime = -1.f; // We get the start time on our first update
255         m_startframe = m_localtime = start;
256         m_endframe = end;
257         m_blendin = blendin;
258         m_playmode = play_mode;
259         m_blendmode = blend_mode;
260         m_endtime = 0.f;
261         m_blendframe = 0.f;
262         m_blendstart = 0.f;
263         m_speed = playback_speed;
264         m_layer_weight = layer_weight;
265         
266         m_done = false;
267
268         return true;
269 }
270
271 void BL_Action::Stop()
272 {
273         m_done = true;
274 }
275
276 bool BL_Action::IsDone()
277 {
278         return m_done;
279 }
280
281 void BL_Action::InitIPO()
282 {
283         // Initialize the IPOs
284         std::vector<SG_Controller*>::iterator it;
285         for (it = m_sg_contr_list.begin(); it != m_sg_contr_list.end(); it++)
286         {
287                 (*it)->SetOption(SG_Controller::SG_CONTR_IPO_RESET, true);
288                 (*it)->SetOption(SG_Controller::SG_CONTR_IPO_IPO_AS_FORCE, m_ipo_flags & ACT_IPOFLAG_FORCE);
289                 (*it)->SetOption(SG_Controller::SG_CONTR_IPO_IPO_ADD, m_ipo_flags & ACT_IPOFLAG_ADD);
290                 (*it)->SetOption(SG_Controller::SG_CONTR_IPO_LOCAL, m_ipo_flags & ACT_IPOFLAG_LOCAL);
291         }
292 }
293
294 bAction *BL_Action::GetAction()
295 {
296         return (IsDone()) ? NULL : m_action;
297 }
298
299 float BL_Action::GetFrame()
300 {
301         return m_localtime;
302 }
303
304 void BL_Action::SetFrame(float frame)
305 {
306         // Clamp the frame to the start and end frame
307         if (frame < min(m_startframe, m_endframe))
308                 frame = min(m_startframe, m_endframe);
309         else if (frame > max(m_startframe, m_endframe))
310                 frame = max(m_startframe, m_endframe);
311         
312         m_localtime = frame;
313         m_calc_localtime = false;
314 }
315
316 void BL_Action::SetPlayMode(short play_mode)
317 {
318         m_playmode = play_mode;
319 }
320
321 void BL_Action::SetTimes(float start, float end)
322 {
323         m_startframe = start;
324         m_endframe = end;
325 }
326
327 void BL_Action::SetLocalTime(float curtime)
328 {
329         float dt = (curtime-m_starttime)*KX_KetsjiEngine::GetAnimFrameRate()*m_speed;
330
331         if (m_endframe < m_startframe)
332                 dt = -dt;
333
334         m_localtime = m_startframe + dt;
335 }
336
337 void BL_Action::ResetStartTime(float curtime)
338 {
339         float dt = (m_localtime > m_startframe) ? m_localtime - m_startframe : m_startframe - m_localtime;
340
341         m_starttime = curtime - dt / (KX_KetsjiEngine::GetAnimFrameRate()*m_speed);
342         SetLocalTime(curtime);
343 }
344
345 void BL_Action::IncrementBlending(float curtime)
346 {
347         // Setup m_blendstart if we need to
348         if (m_blendstart == 0.f)
349                 m_blendstart = curtime;
350         
351         // Bump the blend frame
352         m_blendframe = (curtime - m_blendstart)*KX_KetsjiEngine::GetAnimFrameRate();
353
354         // Clamp
355         if (m_blendframe>m_blendin)
356                 m_blendframe = m_blendin;
357 }
358
359
360 void BL_Action::BlendShape(Key* key, float srcweight, std::vector<float>& blendshape)
361 {
362         vector<float>::const_iterator it;
363         float dstweight;
364         KeyBlock *kb;
365         
366         dstweight = 1.0F - srcweight;
367         //printf("Dst: %f\tSrc: %f\n", srcweight, dstweight);
368         for (it=blendshape.begin(), kb = (KeyBlock *)key->block.first; 
369              kb && it != blendshape.end();
370              kb = (KeyBlock *)kb->next, it++)
371         {
372                 //printf("OirgKeys: %f\t%f\n", kb->curval, (*it));
373                 kb->curval = kb->curval * dstweight + (*it) * srcweight;
374                 //printf("NewKey: %f\n", kb->curval);
375         }
376         //printf("\n");
377 }
378
379 void BL_Action::Update(float curtime)
380 {
381         // Don't bother if we're done with the animation
382         if (m_done)
383                 return;
384
385         curtime -= KX_KetsjiEngine::GetSuspendedDelta();
386
387         // Grab the start time here so we don't end up with a negative m_localtime when
388         // suspending and resuming scenes.
389         if (m_starttime < 0)
390                 m_starttime = curtime;
391
392         if (m_calc_localtime)
393                 SetLocalTime(curtime);
394         else
395         {
396                 ResetStartTime(curtime);
397                 m_calc_localtime = true;
398         }
399
400         // Handle wrap around
401         if (m_localtime < min(m_startframe, m_endframe) || m_localtime > max(m_startframe, m_endframe)) {
402                 switch (m_playmode) {
403                         case ACT_MODE_PLAY:
404                                 // Clamp
405                                 m_localtime = m_endframe;
406                                 m_done = true;
407                                 break;
408                         case ACT_MODE_LOOP:
409                                 // Put the time back to the beginning
410                                 m_localtime = m_startframe;
411                                 m_starttime = curtime;
412                                 break;
413                         case ACT_MODE_PING_PONG:
414                                 // Swap the start and end frames
415                                 float temp = m_startframe;
416                                 m_startframe = m_endframe;
417                                 m_endframe = temp;
418
419                                 m_starttime = curtime;
420
421                                 break;
422                 }
423         }
424
425         if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE)
426         {
427                 BL_ArmatureObject *obj = (BL_ArmatureObject*)m_obj;
428
429                 if (m_layer_weight >= 0)
430                         obj->GetPose(&m_blendpose);
431
432                 // Extract the pose from the action
433                 obj->SetPoseByAction(m_tmpaction, m_localtime);
434
435                 // Handle blending between armature actions
436                 if (m_blendin && m_blendframe<m_blendin)
437                 {
438                         IncrementBlending(curtime);
439
440                         // Calculate weight
441                         float weight = 1.f - (m_blendframe/m_blendin);
442
443                         // Blend the poses
444                         obj->BlendInPose(m_blendinpose, weight, ACT_BLEND_BLEND);
445                 }
446
447
448                 // Handle layer blending
449                 if (m_layer_weight >= 0)
450                         obj->BlendInPose(m_blendpose, m_layer_weight, m_blendmode);
451
452                 obj->UpdateTimestep(curtime);
453         }
454         else
455         {
456                 BL_DeformableGameObject *obj = (BL_DeformableGameObject*)m_obj;
457                 BL_ShapeDeformer *shape_deformer = dynamic_cast<BL_ShapeDeformer*>(obj->GetDeformer());
458
459                 // Handle shape actions if we have any
460                 if (shape_deformer && shape_deformer->GetKey())
461                 {
462                         Key *key = shape_deformer->GetKey();
463
464                         PointerRNA ptrrna;
465                         RNA_id_pointer_create(&key->id, &ptrrna);
466
467                         animsys_evaluate_action(&ptrrna, m_tmpaction, NULL, m_localtime);
468
469                         // Handle blending between shape actions
470                         if (m_blendin && m_blendframe < m_blendin)
471                         {
472                                 IncrementBlending(curtime);
473
474                                 float weight = 1.f - (m_blendframe/m_blendin);
475
476                                 // We go through and clear out the keyblocks so there isn't any interference
477                                 // from other shape actions
478                                 KeyBlock *kb;
479                                 for (kb=(KeyBlock *)key->block.first; kb; kb=(KeyBlock *)kb->next)
480                                         kb->curval = 0.f;
481
482                                 // Now blend the shape
483                                 BlendShape(key, weight, m_blendinshape);
484                         }
485
486                         // Handle layer blending
487                         if (m_layer_weight >= 0)
488                         {
489                                 obj->GetShape(m_blendshape);
490                                 BlendShape(key, m_layer_weight, m_blendshape);
491                         }
492
493                         obj->SetActiveAction(NULL, 0, curtime);
494                 }
495         }
496
497         // This isn't thread-safe, so we move it into it's own function for now
498         //m_obj->UpdateIPO(m_localtime, m_ipo_flags & ACT_IPOFLAG_CHILD);
499
500         if (m_done)
501                 ClearControllerList();
502 }
503
504 void BL_Action::UpdateIPOs()
505 {
506         if (!m_done)
507                 m_obj->UpdateIPO(m_localtime, m_ipo_flags & ACT_IPOFLAG_CHILD);
508 }