BGE Animations: Fixing the Continue option when using the Flipper play type. Also...
[blender.git] / source / gameengine / Converter / BL_ActionActuator.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 /** \file gameengine/Converter/BL_ActionActuator.cpp
31  *  \ingroup bgeconv
32  */
33
34
35 #include "SCA_LogicManager.h"
36 #include "BL_ActionActuator.h"
37 #include "BL_ArmatureObject.h"
38 #include "BL_SkinDeformer.h"
39 #include "BL_Action.h"
40 #include "KX_GameObject.h"
41 #include "STR_HashedString.h"
42 #include "MEM_guardedalloc.h"
43 #include "DNA_nla_types.h"
44 #include "DNA_action_types.h"
45 #include "DNA_armature_types.h"
46 #include "DNA_scene_types.h"
47 #include "BLI_blenlib.h"
48 #include "BLI_math.h"
49 #include "BLI_utildefines.h"
50 #include "MT_Matrix4x4.h"
51
52 #include "BKE_action.h"
53 #include "FloatValue.h"
54 #include "PyObjectPlus.h"
55 #include "KX_PyMath.h"
56
57 extern "C" {
58 #include "BKE_animsys.h"
59 #include "BKE_action.h"
60 #include "RNA_access.h"
61 #include "RNA_define.h"
62 }
63
64 BL_ActionActuator::BL_ActionActuator(SCA_IObject* gameobj,
65                                         const STR_String& propname,
66                                         const STR_String& framepropname,
67                                         float starttime,
68                                         float endtime,
69                                         struct bAction *action,
70                                         short   playtype,
71                                         short   blendin,
72                                         short   priority,
73                                         short   layer,
74                                         float   layer_weight,
75                                         short   ipo_flags,
76                                         short   end_reset,
77                                         float   stride) 
78         : SCA_IActuator(gameobj, KX_ACT_ACTION),
79                 
80         m_lastpos(0, 0, 0),
81         m_blendframe(0),
82         m_flag(0),
83         m_startframe (starttime),
84         m_endframe(endtime) ,
85         m_starttime(0),
86         m_localtime(starttime),
87         m_lastUpdate(-1),
88         m_blendin(blendin),
89         m_blendstart(0),
90         m_stridelength(stride),
91         m_playtype(playtype),
92         m_priority(priority),
93         m_layer(layer),
94         m_layer_weight(layer_weight),
95         m_ipo_flags(ipo_flags),
96         m_pose(NULL),
97         m_blendpose(NULL),
98         m_userpose(NULL),
99         m_action(action),
100         m_propname(propname),
101         m_framepropname(framepropname)          
102 {
103         if (!end_reset)
104                 m_flag |= ACT_FLAG_CONTINUE;
105 };
106
107 BL_ActionActuator::~BL_ActionActuator()
108 {
109         if (m_pose)
110                 game_free_pose(m_pose);
111         if (m_userpose)
112                 game_free_pose(m_userpose);
113         if (m_blendpose)
114                 game_free_pose(m_blendpose);
115 }
116
117 void BL_ActionActuator::ProcessReplica()
118 {
119         SCA_IActuator::ProcessReplica();
120         
121         m_pose = NULL;
122         m_blendpose = NULL;
123         m_localtime=m_startframe;
124         m_lastUpdate=-1;
125         
126 }
127
128 void BL_ActionActuator::SetBlendTime (float newtime){
129         m_blendframe = newtime;
130 }
131
132 CValue* BL_ActionActuator::GetReplica() {
133         BL_ActionActuator* replica = new BL_ActionActuator(*this);//m_float,GetName());
134         replica->ProcessReplica();
135         return replica;
136 }
137
138 bool BL_ActionActuator::Update(double curtime, bool frame)
139 {
140         bool bNegativeEvent = false;
141         bool bPositiveEvent = false;
142         bool use_continue = false;
143         KX_GameObject *obj = (KX_GameObject*)GetParent();
144         short play_mode = BL_Action::ACT_MODE_PLAY;
145         float start = m_startframe, end = m_endframe;
146
147         // If we don't have an action, we can't do anything
148         if (!m_action)
149                 return false;
150
151         // Convert playmode
152         if (m_playtype == ACT_ACTION_LOOP_END)
153                 play_mode = BL_Action::ACT_MODE_LOOP;
154         else if (m_playtype == ACT_ACTION_LOOP_STOP)
155                 play_mode = BL_Action::ACT_MODE_LOOP;
156         else if (m_playtype == ACT_ACTION_PINGPONG)
157         {
158                 // We handle ping pong ourselves to increase compabitility with the pre-Pepper actuator
159                 play_mode = BL_Action::ACT_MODE_PLAY;
160                 
161                 if (m_flag & ACT_FLAG_REVERSE)
162                 {
163                         float tmp = start;
164                         start = end;
165                         end = tmp;
166                         m_localtime = end;
167                 }
168         }
169         else if (m_playtype == ACT_ACTION_FROM_PROP)
170         {
171                 CValue* prop = GetParent()->GetProperty(m_propname);
172
173                 play_mode = BL_Action::ACT_MODE_PLAY;
174                 start = end = prop->GetNumber();
175         }
176
177         // Continue only really makes sense for play stop and flipper. All other modes go until they are complete.
178         if (m_flag & ACT_FLAG_CONTINUE &&
179                 (m_playtype == ACT_ACTION_LOOP_STOP ||
180                 m_playtype == ACT_ACTION_FLIPPER))
181                 use_continue = true;
182         
183         
184         // Handle events
185         if (frame)
186         {
187                 bNegativeEvent = m_negevent;
188                 bPositiveEvent = m_posevent;
189                 RemoveAllEvents();
190         }
191         
192         if (bPositiveEvent)
193         {
194                 if (use_continue && m_flag & ACT_FLAG_ACTIVE)
195                         start = m_localtime = obj->GetActionFrame(m_layer);
196
197                 if (obj->PlayAction(m_action->id.name+2, start, end, m_layer, m_priority, m_blendin, play_mode, m_layer_weight, m_ipo_flags))
198                 {
199                         m_flag |= ACT_FLAG_ACTIVE;
200                         if (use_continue)
201                                 obj->SetActionFrame(m_layer, m_localtime);
202
203                         if (m_playtype == ACT_ACTION_PLAY)
204                                 m_flag |= ACT_FLAG_PLAY_END;
205                         else
206                                 m_flag &= ~ACT_FLAG_PLAY_END;
207                 }
208                 else
209                         return false;
210         }
211         else if ((m_flag & ACT_FLAG_ACTIVE) && bNegativeEvent)
212         {       
213                 bAction *curr_action = obj->GetCurrentAction(m_layer);
214                 if (curr_action && curr_action != m_action)
215                 {
216                         // Someone changed the action on us, so we wont mess with it
217                         // Hopefully there wont be too many problems with two actuators using
218                         // the same action...
219                         m_flag &= ~ACT_FLAG_ACTIVE;
220                         return false;
221                 }
222
223                 
224                 m_localtime = obj->GetActionFrame(m_layer);
225                 if (m_localtime < min(m_startframe, m_endframe) || m_localtime > max(m_startframe, m_endframe))
226                         m_localtime = m_startframe;
227
228                 if (m_playtype == ACT_ACTION_LOOP_STOP)
229                 {
230                         obj->StopAction(m_layer); // Stop the action after getting the frame
231
232                         // We're done
233                         m_flag &= ~ACT_FLAG_ACTIVE;
234                         return false;
235                 }
236                 else if (m_playtype == ACT_ACTION_LOOP_END || m_playtype == ACT_ACTION_PINGPONG)
237                 {
238                         // Convert into a play and let it finish
239                         obj->SetPlayMode(m_layer, BL_Action::ACT_MODE_PLAY);
240
241                         m_flag |= ACT_FLAG_PLAY_END;
242
243                         if (m_playtype == ACT_ACTION_PINGPONG)
244                                 m_flag ^= ACT_FLAG_REVERSE;
245                 }
246                 else if (m_playtype == ACT_ACTION_FLIPPER)
247                 {
248                         // Convert into a play action and play back to the beginning
249                         end = start;
250                         start = obj->GetActionFrame(m_layer);
251                         obj->PlayAction(m_action->id.name+2, start, end, m_layer, m_priority, 0, BL_Action::ACT_MODE_PLAY, m_layer_weight, m_ipo_flags);
252
253                         m_flag |= ACT_FLAG_PLAY_END;
254                 }
255         }
256
257         // Handle a frame property if it's defined
258         if ((m_flag & ACT_FLAG_ACTIVE) && m_framepropname[0] != 0)
259         {
260                 CValue* oldprop = obj->GetProperty(m_framepropname);
261                 CValue* newval = new CFloatValue(obj->GetActionFrame(m_layer));
262                 if (oldprop)
263                         oldprop->SetValue(newval);
264                 else
265                         obj->SetProperty(m_framepropname, newval);
266
267                 newval->Release();
268         }
269
270         // Handle a finished animation
271         if ((m_flag & ACT_FLAG_PLAY_END) && obj->IsActionDone(m_layer))
272         {
273                 m_flag &= ~ACT_FLAG_ACTIVE;
274                 obj->StopAction(m_layer);
275                 return false;
276         }
277
278         return true;
279 }
280
281 #ifdef WITH_PYTHON
282
283 /* ------------------------------------------------------------------------- */
284 /* Python functions                                                          */
285 /* ------------------------------------------------------------------------- */
286
287 PyObject* BL_ActionActuator::PyGetChannel(PyObject* value) {
288         char *string= _PyUnicode_AsString(value);
289         
290         if (!string) {
291                 PyErr_SetString(PyExc_TypeError, "expected a single string");
292                 return NULL;
293         }
294         
295         bPoseChannel *pchan;
296         
297         if(m_userpose==NULL && m_pose==NULL) {
298                 BL_ArmatureObject *obj = (BL_ArmatureObject*)GetParent();
299                 obj->GetPose(&m_pose); /* Get the underlying pose from the armature */
300         }
301         
302         // get_pose_channel accounts for NULL pose, run on both incase one exists but
303         // the channel doesnt
304         if(             !(pchan=get_pose_channel(m_userpose, string)) &&
305                         !(pchan=get_pose_channel(m_pose, string))  )
306         {
307                 PyErr_SetString(PyExc_ValueError, "channel doesnt exist");
308                 return NULL;
309         }
310
311         PyObject *ret = PyTuple_New(3);
312         
313         PyObject *list = PyList_New(3); 
314         PyList_SET_ITEM(list, 0, PyFloat_FromDouble(pchan->loc[0]));
315         PyList_SET_ITEM(list, 1, PyFloat_FromDouble(pchan->loc[1]));
316         PyList_SET_ITEM(list, 2, PyFloat_FromDouble(pchan->loc[2]));
317         PyTuple_SET_ITEM(ret, 0, list);
318         
319         list = PyList_New(3);
320         PyList_SET_ITEM(list, 0, PyFloat_FromDouble(pchan->size[0]));
321         PyList_SET_ITEM(list, 1, PyFloat_FromDouble(pchan->size[1]));
322         PyList_SET_ITEM(list, 2, PyFloat_FromDouble(pchan->size[2]));
323         PyTuple_SET_ITEM(ret, 1, list);
324         
325         list = PyList_New(4);
326         PyList_SET_ITEM(list, 0, PyFloat_FromDouble(pchan->quat[0]));
327         PyList_SET_ITEM(list, 1, PyFloat_FromDouble(pchan->quat[1]));
328         PyList_SET_ITEM(list, 2, PyFloat_FromDouble(pchan->quat[2]));
329         PyList_SET_ITEM(list, 3, PyFloat_FromDouble(pchan->quat[3]));
330         PyTuple_SET_ITEM(ret, 2, list);
331
332         return ret;
333 /*
334         return Py_BuildValue("([fff][fff][ffff])",
335                 pchan->loc[0], pchan->loc[1], pchan->loc[2],
336                 pchan->size[0], pchan->size[1], pchan->size[2],
337                 pchan->quat[0], pchan->quat[1], pchan->quat[2], pchan->quat[3] );
338 */
339 }
340
341 /*     setChannel                                                         */
342 KX_PYMETHODDEF_DOC(BL_ActionActuator, setChannel,
343 "setChannel(channel, matrix)\n"
344 "\t - channel   : A string specifying the name of the bone channel.\n"
345 "\t - matrix    : A 4x4 matrix specifying the overriding transformation\n"
346 "\t               as an offset from the bone's rest position.\n")
347 {
348         BL_ArmatureObject *obj = (BL_ArmatureObject*)GetParent();
349         char *string;
350         PyObject *pymat= NULL;
351         PyObject *pyloc= NULL, *pysize= NULL, *pyquat= NULL;
352         bPoseChannel *pchan;
353         
354         if(PyTuple_Size(args)==2) {
355                 if (!PyArg_ParseTuple(args,"sO:setChannel", &string, &pymat)) // matrix
356                         return NULL;
357         }
358         else if(PyTuple_Size(args)==4) {
359                 if (!PyArg_ParseTuple(args,"sOOO:setChannel", &string, &pyloc, &pysize, &pyquat)) // loc/size/quat
360                         return NULL;
361         }
362         else {
363                 PyErr_SetString(PyExc_ValueError, "Expected a string and a 4x4 matrix (2 args) or a string and loc/size/quat sequences (4 args)");
364                 return NULL;
365         }
366         
367         if(pymat) {
368                 float matrix[4][4];
369                 MT_Matrix4x4 mat;
370                 
371                 if(!PyMatTo(pymat, mat))
372                         return NULL;
373                 
374                 mat.getValue((float*)matrix);
375                 
376                 BL_ArmatureObject *obj = (BL_ArmatureObject*)GetParent();
377                 
378                 if (!m_userpose) {
379                         if(!m_pose)
380                                 obj->GetPose(&m_pose); /* Get the underlying pose from the armature */
381                         game_copy_pose(&m_userpose, m_pose, 0);
382                 }
383                 // pchan= verify_pose_channel(m_userpose, string); // adds the channel if its not there.
384                 pchan= get_pose_channel(m_userpose, string); // adds the channel if its not there.
385                 
386                 if(pchan) {
387                         VECCOPY (pchan->loc, matrix[3]);
388                         mat4_to_size( pchan->size,matrix);
389                         mat4_to_quat( pchan->quat,matrix);
390                 }
391         }
392         else {
393                 MT_Vector3 loc;
394                 MT_Vector3 size;
395                 MT_Quaternion quat;
396                 
397                 if (!PyVecTo(pyloc, loc) || !PyVecTo(pysize, size) || !PyQuatTo(pyquat, quat))
398                         return NULL;
399                 
400                 // same as above
401                 if (!m_userpose) {
402                         if(!m_pose)
403                                 obj->GetPose(&m_pose); /* Get the underlying pose from the armature */
404                         game_copy_pose(&m_userpose, m_pose, 0);
405                 }
406                 // pchan= verify_pose_channel(m_userpose, string);
407                 pchan= get_pose_channel(m_userpose, string); // adds the channel if its not there.
408                 
409                 // for some reason loc.setValue(pchan->loc) fails
410                 if(pchan) {
411                         pchan->loc[0]= loc[0]; pchan->loc[1]= loc[1]; pchan->loc[2]= loc[2];
412                         pchan->size[0]= size[0]; pchan->size[1]= size[1]; pchan->size[2]= size[2];
413                         pchan->quat[0]= quat[3]; pchan->quat[1]= quat[0]; pchan->quat[2]= quat[1]; pchan->quat[3]= quat[2]; /* notice xyzw -> wxyz is intentional */
414                 }
415         }
416         
417         if(pchan==NULL) {
418                 PyErr_SetString(PyExc_ValueError, "Channel could not be found, use the 'channelNames' attribute to get a list of valid channels");
419                 return NULL;
420         }
421         
422         Py_RETURN_NONE;
423 }
424
425 /* ------------------------------------------------------------------------- */
426 /* Python Integration Hooks                                                                      */
427 /* ------------------------------------------------------------------------- */
428
429 PyTypeObject BL_ActionActuator::Type = {
430         PyVarObject_HEAD_INIT(NULL, 0)
431         "BL_ActionActuator",
432         sizeof(PyObjectPlus_Proxy),
433         0,
434         py_base_dealloc,
435         0,
436         0,
437         0,
438         0,
439         py_base_repr,
440         0,0,0,0,0,0,0,0,0,
441         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
442         0,0,0,0,0,0,0,
443         Methods,
444         0,
445         0,
446         &SCA_IActuator::Type,
447         0,0,0,0,0,0,
448         py_base_new
449 };
450
451 PyMethodDef BL_ActionActuator::Methods[] = {
452         {"getChannel", (PyCFunction) BL_ActionActuator::sPyGetChannel, METH_O},
453         KX_PYMETHODTABLE(BL_ActionActuator, setChannel),
454         {NULL,NULL} //Sentinel
455 };
456
457 PyAttributeDef BL_ActionActuator::Attributes[] = {
458         KX_PYATTRIBUTE_FLOAT_RW("frameStart", 0, MAXFRAMEF, BL_ActionActuator, m_startframe),
459         KX_PYATTRIBUTE_FLOAT_RW("frameEnd", 0, MAXFRAMEF, BL_ActionActuator, m_endframe),
460         KX_PYATTRIBUTE_FLOAT_RW("blendIn", 0, MAXFRAMEF, BL_ActionActuator, m_blendin),
461         KX_PYATTRIBUTE_RW_FUNCTION("action", BL_ActionActuator, pyattr_get_action, pyattr_set_action),
462         KX_PYATTRIBUTE_RO_FUNCTION("channelNames", BL_ActionActuator, pyattr_get_channel_names),
463         KX_PYATTRIBUTE_SHORT_RW("priority", 0, 100, false, BL_ActionActuator, m_priority),
464         KX_PYATTRIBUTE_FLOAT_RW_CHECK("frame", 0, MAXFRAMEF, BL_ActionActuator, m_localtime, CheckFrame),
465         KX_PYATTRIBUTE_STRING_RW("propName", 0, 31, false, BL_ActionActuator, m_propname),
466         KX_PYATTRIBUTE_STRING_RW("framePropName", 0, 31, false, BL_ActionActuator, m_framepropname),
467         KX_PYATTRIBUTE_RW_FUNCTION("useContinue", BL_ActionActuator, pyattr_get_use_continue, pyattr_set_use_continue),
468         KX_PYATTRIBUTE_FLOAT_RW_CHECK("blendTime", 0, MAXFRAMEF, BL_ActionActuator, m_blendframe, CheckBlendTime),
469         KX_PYATTRIBUTE_SHORT_RW_CHECK("mode",0,100,false,BL_ActionActuator,m_playtype,CheckType),
470         { NULL }        //Sentinel
471 };
472
473 PyObject* BL_ActionActuator::pyattr_get_action(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
474 {
475         BL_ActionActuator* self= static_cast<BL_ActionActuator*>(self_v);
476         return PyUnicode_FromString(self->GetAction() ? self->GetAction()->id.name+2 : "");
477 }
478
479 int BL_ActionActuator::pyattr_set_action(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
480 {
481         BL_ActionActuator* self= static_cast<BL_ActionActuator*>(self_v);
482         
483         if (!PyUnicode_Check(value))
484         {
485                 PyErr_SetString(PyExc_ValueError, "actuator.action = val: Action Actuator, expected the string name of the action");
486                 return PY_SET_ATTR_FAIL;
487         }
488
489         bAction *action= NULL;
490         STR_String val = _PyUnicode_AsString(value);
491         
492         if (val != "")
493         {
494                 action= (bAction*)SCA_ILogicBrick::m_sCurrentLogicManager->GetActionByName(val);
495                 if (!action)
496                 {
497                         PyErr_SetString(PyExc_ValueError, "actuator.action = val: Action Actuator, action not found!");
498                         return PY_SET_ATTR_FAIL;
499                 }
500         }
501         
502         self->SetAction(action);
503         return PY_SET_ATTR_SUCCESS;
504
505 }
506
507 PyObject* BL_ActionActuator::pyattr_get_channel_names(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
508 {
509         BL_ActionActuator* self= static_cast<BL_ActionActuator*>(self_v);
510         PyObject *ret= PyList_New(0);
511         PyObject *item;
512         
513         bPose *pose= ((BL_ArmatureObject*)self->GetParent())->GetOrigPose();
514         
515         if(pose) {
516                 bPoseChannel *pchan;
517                 for(pchan= (bPoseChannel *)pose->chanbase.first; pchan; pchan= (bPoseChannel *)pchan->next) {
518                         item= PyUnicode_FromString(pchan->name);
519                         PyList_Append(ret, item);
520                         Py_DECREF(item);
521                 }
522         }
523         
524         return ret;
525 }
526
527 PyObject* BL_ActionActuator::pyattr_get_use_continue(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
528 {
529         BL_ActionActuator* self= static_cast<BL_ActionActuator*>(self_v);
530         return PyBool_FromLong(self->m_flag & ACT_FLAG_CONTINUE);
531 }
532
533 int BL_ActionActuator::pyattr_set_use_continue(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
534 {
535         BL_ActionActuator* self= static_cast<BL_ActionActuator*>(self_v);
536         
537         if (PyObject_IsTrue(value))
538                 self->m_flag |= ACT_FLAG_CONTINUE;
539         else
540                 self->m_flag &= ~ACT_FLAG_CONTINUE;
541         
542         return PY_SET_ATTR_SUCCESS;
543 }
544
545 #endif // WITH_PYTHON