svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r22935:23022
[blender.git] / source / gameengine / Ketsji / KX_ObjectActuator.cpp
1 /**
2  * Do translation/rotation actions
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #include "KX_ObjectActuator.h"
33 #include "KX_GameObject.h"
34 #include "KX_PyMath.h" // For PyVecTo - should this include be put in PyObjectPlus?
35 #include "KX_IPhysicsController.h"
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 /* ------------------------------------------------------------------------- */
42 /* Native functions                                                          */
43 /* ------------------------------------------------------------------------- */
44
45 KX_ObjectActuator::
46 KX_ObjectActuator(
47         SCA_IObject* gameobj,
48         KX_GameObject* refobj,
49         const MT_Vector3& force,
50         const MT_Vector3& torque,
51         const MT_Vector3& dloc,
52         const MT_Vector3& drot,
53         const MT_Vector3& linV,
54         const MT_Vector3& angV,
55         const short damping,
56         const KX_LocalFlags& flag
57 ) : 
58         SCA_IActuator(gameobj),
59         m_force(force),
60         m_torque(torque),
61         m_dloc(dloc),
62         m_drot(drot),
63         m_linear_velocity(linV),
64         m_angular_velocity(angV),
65         m_linear_length2(0.0),
66         m_current_linear_factor(0.0),
67         m_current_angular_factor(0.0),
68         m_damping(damping),
69         m_previous_error(0.0,0.0,0.0),
70         m_error_accumulator(0.0,0.0,0.0),
71         m_bitLocalFlag (flag),
72         m_reference(refobj),
73         m_active_combined_velocity (false),
74         m_linear_damping_active(false),
75         m_angular_damping_active(false)
76 {
77         if (m_bitLocalFlag.ServoControl)
78         {
79                 // in servo motion, the force is local if the target velocity is local
80                 m_bitLocalFlag.Force = m_bitLocalFlag.LinearVelocity;
81
82                 m_pid = m_torque;
83         }
84         if (m_reference)
85                 m_reference->RegisterActuator(this);
86         UpdateFuzzyFlags();
87 }
88
89 KX_ObjectActuator::~KX_ObjectActuator()
90 {
91         if (m_reference)
92                 m_reference->UnregisterActuator(this);
93 }
94
95 bool KX_ObjectActuator::Update()
96 {
97         
98         bool bNegativeEvent = IsNegativeEvent();
99         RemoveAllEvents();
100                 
101         KX_GameObject *parent = static_cast<KX_GameObject *>(GetParent()); 
102
103         if (bNegativeEvent) {
104                 // If we previously set the linear velocity we now have to inform
105                 // the physics controller that we no longer wish to apply it and that
106                 // it should reconcile the externally set velocity with it's 
107                 // own velocity.
108                 if (m_active_combined_velocity) {
109                         if (parent)
110                                 parent->ResolveCombinedVelocities(
111                                                 m_linear_velocity,
112                                                 m_angular_velocity,
113                                                 (m_bitLocalFlag.LinearVelocity) != 0,
114                                                 (m_bitLocalFlag.AngularVelocity) != 0
115                                         );
116                         m_active_combined_velocity = false;
117                 } 
118                 m_linear_damping_active = false;
119                 m_angular_damping_active = false;
120                 m_error_accumulator.setValue(0.0,0.0,0.0);
121                 m_previous_error.setValue(0.0,0.0,0.0);
122                 return false; 
123
124         } else if (parent)
125         {
126                 if (m_bitLocalFlag.ServoControl) 
127                 {
128                         // In this mode, we try to reach a target speed using force
129                         // As we don't know the friction, we must implement a generic 
130                         // servo control to achieve the speed in a configurable
131                         // v = current velocity
132                         // V = target velocity
133                         // e = V-v = speed error
134                         // dt = time interval since previous update
135                         // I = sum(e(t)*dt)
136                         // dv = e(t) - e(t-1)
137                         // KP, KD, KI : coefficient
138                         // F = KP*e+KI*I+KD*dv
139                         MT_Scalar mass = parent->GetMass();
140                         if (mass < MT_EPSILON)
141                                 return false;
142                         MT_Vector3 v = parent->GetLinearVelocity(m_bitLocalFlag.LinearVelocity);
143                         if (m_reference)
144                         {
145                                 const MT_Point3& mypos = parent->NodeGetWorldPosition();
146                                 const MT_Point3& refpos = m_reference->NodeGetWorldPosition();
147                                 MT_Point3 relpos;
148                                 relpos = (mypos-refpos);
149                                 MT_Vector3 vel= m_reference->GetVelocity(relpos);
150                                 if (m_bitLocalFlag.LinearVelocity)
151                                         // must convert in local space
152                                         vel = parent->NodeGetWorldOrientation().transposed()*vel;
153                                 v -= vel;
154                         }
155                         MT_Vector3 e = m_linear_velocity - v;
156                         MT_Vector3 dv = e - m_previous_error;
157                         MT_Vector3 I = m_error_accumulator + e;
158
159                         m_force = m_pid.x()*e+m_pid.y()*I+m_pid.z()*dv;
160                         // to automatically adapt the PID coefficient to mass;
161                         m_force *= mass;
162                         if (m_bitLocalFlag.Torque) 
163                         {
164                                 if (m_force[0] > m_dloc[0])
165                                 {
166                                         m_force[0] = m_dloc[0];
167                                         I[0] = m_error_accumulator[0];
168                                 } else if (m_force[0] < m_drot[0])
169                                 {
170                                         m_force[0] = m_drot[0];
171                                         I[0] = m_error_accumulator[0];
172                                 }
173                         }
174                         if (m_bitLocalFlag.DLoc) 
175                         {
176                                 if (m_force[1] > m_dloc[1])
177                                 {
178                                         m_force[1] = m_dloc[1];
179                                         I[1] = m_error_accumulator[1];
180                                 } else if (m_force[1] < m_drot[1])
181                                 {
182                                         m_force[1] = m_drot[1];
183                                         I[1] = m_error_accumulator[1];
184                                 }
185                         }
186                         if (m_bitLocalFlag.DRot) 
187                         {
188                                 if (m_force[2] > m_dloc[2])
189                                 {
190                                         m_force[2] = m_dloc[2];
191                                         I[2] = m_error_accumulator[2];
192                                 } else if (m_force[2] < m_drot[2])
193                                 {
194                                         m_force[2] = m_drot[2];
195                                         I[2] = m_error_accumulator[2];
196                                 }
197                         }
198                         m_previous_error = e;
199                         m_error_accumulator = I;
200                         parent->ApplyForce(m_force,(m_bitLocalFlag.LinearVelocity) != 0);
201                 } else
202                 {
203                         if (!m_bitLocalFlag.ZeroForce)
204                         {
205                                 parent->ApplyForce(m_force,(m_bitLocalFlag.Force) != 0);
206                         }
207                         if (!m_bitLocalFlag.ZeroTorque)
208                         {
209                                 parent->ApplyTorque(m_torque,(m_bitLocalFlag.Torque) != 0);
210                         }
211                         if (!m_bitLocalFlag.ZeroDLoc)
212                         {
213                                 parent->ApplyMovement(m_dloc,(m_bitLocalFlag.DLoc) != 0);
214                         }
215                         if (!m_bitLocalFlag.ZeroDRot)
216                         {
217                                 parent->ApplyRotation(m_drot,(m_bitLocalFlag.DRot) != 0);
218                         }
219                         if (!m_bitLocalFlag.ZeroLinearVelocity)
220                         {
221                                 if (m_bitLocalFlag.AddOrSetLinV) {
222                                         parent->addLinearVelocity(m_linear_velocity,(m_bitLocalFlag.LinearVelocity) != 0);
223                                 } else {
224                                         m_active_combined_velocity = true;
225                                         if (m_damping > 0) {
226                                                 MT_Vector3 linV;
227                                                 if (!m_linear_damping_active) {
228                                                         // delta and the start speed (depends on the existing speed in that direction)
229                                                         linV = parent->GetLinearVelocity(m_bitLocalFlag.LinearVelocity);
230                                                         // keep only the projection along the desired direction
231                                                         m_current_linear_factor = linV.dot(m_linear_velocity)/m_linear_length2;
232                                                         m_linear_damping_active = true;
233                                                 }
234                                                 if (m_current_linear_factor < 1.0)
235                                                         m_current_linear_factor += 1.0/m_damping;
236                                                 if (m_current_linear_factor > 1.0)
237                                                         m_current_linear_factor = 1.0;
238                                                 linV = m_current_linear_factor * m_linear_velocity;
239                                                 parent->setLinearVelocity(linV,(m_bitLocalFlag.LinearVelocity) != 0);
240                                         } else {
241                                                 parent->setLinearVelocity(m_linear_velocity,(m_bitLocalFlag.LinearVelocity) != 0);
242                                         }
243                                 }
244                         }
245                         if (!m_bitLocalFlag.ZeroAngularVelocity)
246                         {
247                                 m_active_combined_velocity = true;
248                                 if (m_damping > 0) {
249                                         MT_Vector3 angV;
250                                         if (!m_angular_damping_active) {
251                                                 // delta and the start speed (depends on the existing speed in that direction)
252                                                 angV = parent->GetAngularVelocity(m_bitLocalFlag.AngularVelocity);
253                                                 // keep only the projection along the desired direction
254                                                 m_current_angular_factor = angV.dot(m_angular_velocity)/m_angular_length2;
255                                                 m_angular_damping_active = true;
256                                         }
257                                         if (m_current_angular_factor < 1.0)
258                                                 m_current_angular_factor += 1.0/m_damping;
259                                         if (m_current_angular_factor > 1.0)
260                                                 m_current_angular_factor = 1.0;
261                                         angV = m_current_angular_factor * m_angular_velocity;
262                                         parent->setAngularVelocity(angV,(m_bitLocalFlag.AngularVelocity) != 0);
263                                 } else {
264                                         parent->setAngularVelocity(m_angular_velocity,(m_bitLocalFlag.AngularVelocity) != 0);
265                                 }
266                         }
267                 }
268                 
269         }
270         return true;
271 }
272
273
274
275 CValue* KX_ObjectActuator::GetReplica()
276 {
277         KX_ObjectActuator* replica = new KX_ObjectActuator(*this);//m_float,GetName());
278         replica->ProcessReplica();
279
280         return replica;
281 }
282
283 void KX_ObjectActuator::ProcessReplica()
284 {
285         SCA_IActuator::ProcessReplica();
286         if (m_reference)
287                 m_reference->RegisterActuator(this);
288 }
289
290 bool KX_ObjectActuator::UnlinkObject(SCA_IObject* clientobj)
291 {
292         if (clientobj == (SCA_IObject*)m_reference)
293         {
294                 // this object is being deleted, we cannot continue to use it as reference.
295                 m_reference = NULL;
296                 return true;
297         }
298         return false;
299 }
300
301 void KX_ObjectActuator::Relink(GEN_Map<GEN_HashedPtr, void*> *obj_map)
302 {
303         void **h_obj = (*obj_map)[m_reference];
304         if (h_obj) {
305                 if (m_reference)
306                         m_reference->UnregisterActuator(this);
307                 m_reference = (KX_GameObject*)(*h_obj);
308                 m_reference->RegisterActuator(this);
309         }
310 }
311
312 /* some 'standard' utilities... */
313 bool KX_ObjectActuator::isValid(KX_ObjectActuator::KX_OBJECT_ACT_VEC_TYPE type)
314 {
315         bool res = false;
316         res = (type > KX_OBJECT_ACT_NODEF) && (type < KX_OBJECT_ACT_MAX);
317         return res;
318 }
319
320
321
322 /* ------------------------------------------------------------------------- */
323 /* Python functions                                                          */
324 /* ------------------------------------------------------------------------- */
325
326 /* Integration hooks ------------------------------------------------------- */
327 PyTypeObject KX_ObjectActuator::Type = {
328         PyVarObject_HEAD_INIT(NULL, 0)
329         "KX_ObjectActuator",
330         sizeof(PyObjectPlus_Proxy),
331         0,
332         py_base_dealloc,
333         0,
334         0,
335         0,
336         0,
337         py_base_repr,
338         0,0,0,0,0,0,0,0,0,
339         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
340         0,0,0,0,0,0,0,
341         Methods,
342         0,
343         0,
344         &SCA_IActuator::Type,
345         0,0,0,0,0,0,
346         py_base_new
347 };
348
349 PyMethodDef KX_ObjectActuator::Methods[] = {
350         {NULL,NULL} //Sentinel
351 };
352
353 PyAttributeDef KX_ObjectActuator::Attributes[] = {
354         KX_PYATTRIBUTE_VECTOR_RW_CHECK("force", -1000, 1000, false, KX_ObjectActuator, m_force, PyUpdateFuzzyFlags),
355         KX_PYATTRIBUTE_BOOL_RW("useLocalForce", KX_ObjectActuator, m_bitLocalFlag.Force),
356         KX_PYATTRIBUTE_VECTOR_RW_CHECK("torque", -1000, 1000, false, KX_ObjectActuator, m_torque, PyUpdateFuzzyFlags),
357         KX_PYATTRIBUTE_BOOL_RW("useLocalTorque", KX_ObjectActuator, m_bitLocalFlag.Torque),
358         KX_PYATTRIBUTE_VECTOR_RW_CHECK("dLoc", -1000, 1000, false, KX_ObjectActuator, m_dloc, PyUpdateFuzzyFlags),
359         KX_PYATTRIBUTE_BOOL_RW("useLocalDLoc", KX_ObjectActuator, m_bitLocalFlag.DLoc),
360         KX_PYATTRIBUTE_VECTOR_RW_CHECK("dRot", -1000, 1000, false, KX_ObjectActuator, m_drot, PyUpdateFuzzyFlags),
361         KX_PYATTRIBUTE_BOOL_RW("useLocalDRot", KX_ObjectActuator, m_bitLocalFlag.DRot),
362 #ifdef USE_MATHUTILS
363         KX_PYATTRIBUTE_RW_FUNCTION("linV", KX_ObjectActuator, pyattr_get_linV, pyattr_set_linV),
364         KX_PYATTRIBUTE_RW_FUNCTION("angV", KX_ObjectActuator, pyattr_get_angV, pyattr_set_angV),
365 #else
366         KX_PYATTRIBUTE_VECTOR_RW_CHECK("linV", -1000, 1000, false, KX_ObjectActuator, m_linear_velocity, PyUpdateFuzzyFlags),
367         KX_PYATTRIBUTE_VECTOR_RW_CHECK("angV", -1000, 1000, false, KX_ObjectActuator, m_angular_velocity, PyUpdateFuzzyFlags),
368 #endif
369         KX_PYATTRIBUTE_BOOL_RW("useLocalLinV", KX_ObjectActuator, m_bitLocalFlag.LinearVelocity),
370         KX_PYATTRIBUTE_BOOL_RW("useLocalAngV", KX_ObjectActuator, m_bitLocalFlag.AngularVelocity),
371         KX_PYATTRIBUTE_SHORT_RW("damping", 0, 1000, false, KX_ObjectActuator, m_damping),
372         KX_PYATTRIBUTE_RW_FUNCTION("forceLimitX", KX_ObjectActuator, pyattr_get_forceLimitX, pyattr_set_forceLimitX),
373         KX_PYATTRIBUTE_RW_FUNCTION("forceLimitY", KX_ObjectActuator, pyattr_get_forceLimitY, pyattr_set_forceLimitY),
374         KX_PYATTRIBUTE_RW_FUNCTION("forceLimitZ", KX_ObjectActuator, pyattr_get_forceLimitZ, pyattr_set_forceLimitZ),
375         KX_PYATTRIBUTE_VECTOR_RW_CHECK("pid", -100, 200, true, KX_ObjectActuator, m_pid, PyCheckPid),
376         KX_PYATTRIBUTE_RW_FUNCTION("reference", KX_ObjectActuator,pyattr_get_reference,pyattr_set_reference),
377         { NULL }        //Sentinel
378 };
379
380 /* Attribute get/set functions */
381
382 #ifdef USE_MATHUTILS
383
384 /* These require an SGNode */
385 #define MATHUTILS_VEC_CB_LINV 1
386 #define MATHUTILS_VEC_CB_ANGV 2
387
388 static int mathutils_kxobactu_vector_cb_index= -1; /* index for our callbacks */
389
390 static int mathutils_obactu_generic_check(PyObject *self_v)
391 {
392         KX_ObjectActuator* self= static_cast<KX_ObjectActuator*>BGE_PROXY_REF(self_v);
393         if(self==NULL)
394                 return 0;
395
396         return 1;
397 }
398
399 static int mathutils_obactu_vector_get(PyObject *self_v, int subtype, float *vec_from)
400 {
401         KX_ObjectActuator* self= static_cast<KX_ObjectActuator*>BGE_PROXY_REF(self_v);
402         if(self==NULL)
403                 return 0;
404
405         switch(subtype) {
406                 case MATHUTILS_VEC_CB_LINV:
407                         self->m_linear_velocity.getValue(vec_from);
408                         break;
409                 case MATHUTILS_VEC_CB_ANGV:
410                         self->m_angular_velocity.getValue(vec_from);
411                         break;
412         }
413
414         return 1;
415 }
416
417 static int mathutils_obactu_vector_set(PyObject *self_v, int subtype, float *vec_to)
418 {
419         KX_ObjectActuator* self= static_cast<KX_ObjectActuator*>BGE_PROXY_REF(self_v);
420         if(self==NULL)
421                 return 0;
422
423         switch(subtype) {
424                 case MATHUTILS_VEC_CB_LINV:
425                         self->m_linear_velocity.setValue(vec_to);
426                         break;
427                 case MATHUTILS_VEC_CB_ANGV:
428                         self->m_angular_velocity.setValue(vec_to);
429                         break;
430         }
431
432         return 1;
433 }
434
435 static int mathutils_obactu_vector_get_index(PyObject *self_v, int subtype, float *vec_from, int index)
436 {
437         float f[4];
438         /* lazy, avoid repeteing the case statement */
439         if(!mathutils_obactu_vector_get(self_v, subtype, f))
440                 return 0;
441
442         vec_from[index]= f[index];
443         return 1;
444 }
445
446 static int mathutils_obactu_vector_set_index(PyObject *self_v, int subtype, float *vec_to, int index)
447 {
448         float f= vec_to[index];
449
450         /* lazy, avoid repeteing the case statement */
451         if(!mathutils_obactu_vector_get(self_v, subtype, vec_to))
452                 return 0;
453
454         vec_to[index]= f;
455         mathutils_obactu_vector_set(self_v, subtype, vec_to);
456
457         return 1;
458 }
459
460 Mathutils_Callback mathutils_obactu_vector_cb = {
461         mathutils_obactu_generic_check,
462         mathutils_obactu_vector_get,
463         mathutils_obactu_vector_set,
464         mathutils_obactu_vector_get_index,
465         mathutils_obactu_vector_set_index
466 };
467
468 PyObject* KX_ObjectActuator::pyattr_get_linV(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
469 {
470         return newVectorObject_cb(BGE_PROXY_FROM_REF(self_v), 3, mathutils_kxobactu_vector_cb_index, MATHUTILS_VEC_CB_LINV);
471 }
472
473 int KX_ObjectActuator::pyattr_set_linV(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
474 {
475         KX_ObjectActuator* self= static_cast<KX_ObjectActuator*>(self_v);
476         if (!PyVecTo(value, self->m_linear_velocity))
477                 return PY_SET_ATTR_FAIL;
478
479         return PY_SET_ATTR_SUCCESS;
480 }
481
482 PyObject* KX_ObjectActuator::pyattr_get_angV(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
483 {
484         return newVectorObject_cb(BGE_PROXY_FROM_REF(self_v), 3, mathutils_kxobactu_vector_cb_index, MATHUTILS_VEC_CB_ANGV);
485 }
486
487 int KX_ObjectActuator::pyattr_set_angV(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
488 {
489         KX_ObjectActuator* self= static_cast<KX_ObjectActuator*>(self_v);
490         if (!PyVecTo(value, self->m_angular_velocity))
491                 return PY_SET_ATTR_FAIL;
492
493         return PY_SET_ATTR_SUCCESS;
494 }
495
496
497 void KX_ObjectActuator_Mathutils_Callback_Init(void)
498 {
499         // register mathutils callbacks, ok to run more then once.
500         mathutils_kxobactu_vector_cb_index= Mathutils_RegisterCallback(&mathutils_obactu_vector_cb);
501 }
502
503 #endif // USE_MATHUTILS
504
505 PyObject* KX_ObjectActuator::pyattr_get_forceLimitX(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
506 {
507         KX_ObjectActuator* self = reinterpret_cast<KX_ObjectActuator*>(self_v);
508         PyObject *retVal = PyList_New(3);
509
510         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(self->m_drot[0]));
511         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(self->m_dloc[0]));
512         PyList_SET_ITEM(retVal, 2, PyBool_FromLong(self->m_bitLocalFlag.Torque));
513         
514         return retVal;
515 }
516
517 int KX_ObjectActuator::pyattr_set_forceLimitX(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
518 {
519         KX_ObjectActuator* self = reinterpret_cast<KX_ObjectActuator*>(self_v);
520
521         PyObject* seq = PySequence_Fast(value, "");
522         if (seq && PySequence_Fast_GET_SIZE(seq) == 3)
523         {
524                 self->m_drot[0] = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value, 0));
525                 self->m_dloc[0] = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value, 1));
526                 self->m_bitLocalFlag.Torque = (PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 2)) != 0);
527
528                 if (!PyErr_Occurred())
529                 {
530                         Py_DECREF(seq);
531                         return PY_SET_ATTR_SUCCESS;
532                 }
533         }
534
535         Py_XDECREF(seq);
536
537         PyErr_SetString(PyExc_ValueError, "expected a sequence of 2 floats and a bool");
538         return PY_SET_ATTR_FAIL;
539 }
540
541 PyObject* KX_ObjectActuator::pyattr_get_forceLimitY(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
542 {
543         KX_ObjectActuator* self = reinterpret_cast<KX_ObjectActuator*>(self_v);
544         PyObject *retVal = PyList_New(3);
545
546         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(self->m_drot[1]));
547         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(self->m_dloc[1]));
548         PyList_SET_ITEM(retVal, 2, PyBool_FromLong(self->m_bitLocalFlag.DLoc));
549         
550         return retVal;
551 }
552
553 int     KX_ObjectActuator::pyattr_set_forceLimitY(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
554 {
555         KX_ObjectActuator* self = reinterpret_cast<KX_ObjectActuator*>(self_v);
556
557         PyObject* seq = PySequence_Fast(value, "");
558         if (seq && PySequence_Fast_GET_SIZE(seq) == 3)
559         {
560                 self->m_drot[1] = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value, 0));
561                 self->m_dloc[1] = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value, 1));
562                 self->m_bitLocalFlag.DLoc = (PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 2)) != 0);
563
564                 if (!PyErr_Occurred())
565                 {
566                         Py_DECREF(seq);
567                         return PY_SET_ATTR_SUCCESS;
568                 }
569         }
570
571         Py_XDECREF(seq);
572
573         PyErr_SetString(PyExc_ValueError, "expected a sequence of 2 floats and a bool");
574         return PY_SET_ATTR_FAIL;
575 }
576
577 PyObject* KX_ObjectActuator::pyattr_get_forceLimitZ(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
578 {
579         KX_ObjectActuator* self = reinterpret_cast<KX_ObjectActuator*>(self_v);
580         PyObject *retVal = PyList_New(3);
581
582         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(self->m_drot[2]));
583         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(self->m_dloc[2]));
584         PyList_SET_ITEM(retVal, 2, PyBool_FromLong(self->m_bitLocalFlag.DRot));
585         
586         return retVal;
587 }
588
589 int     KX_ObjectActuator::pyattr_set_forceLimitZ(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
590 {
591         KX_ObjectActuator* self = reinterpret_cast<KX_ObjectActuator*>(self_v);
592
593         PyObject* seq = PySequence_Fast(value, "");
594         if (seq && PySequence_Fast_GET_SIZE(seq) == 3)
595         {
596                 self->m_drot[2] = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value, 0));
597                 self->m_dloc[2] = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value, 1));
598                 self->m_bitLocalFlag.DRot = (PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 2)) != 0);
599
600                 if (!PyErr_Occurred())
601                 {
602                         Py_DECREF(seq);
603                         return PY_SET_ATTR_SUCCESS;
604                 }
605         }
606
607         Py_XDECREF(seq);
608
609         PyErr_SetString(PyExc_ValueError, "expected a sequence of 2 floats and a bool");
610         return PY_SET_ATTR_FAIL;
611 }
612
613 PyObject* KX_ObjectActuator::pyattr_get_reference(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
614 {
615         KX_ObjectActuator* actuator = static_cast<KX_ObjectActuator*>(self);
616         if (!actuator->m_reference)
617                 Py_RETURN_NONE;
618         
619         return actuator->m_reference->GetProxy();
620 }
621
622 int KX_ObjectActuator::pyattr_set_reference(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
623 {
624         KX_ObjectActuator* actuator = static_cast<KX_ObjectActuator*>(self);
625         KX_GameObject *refOb;
626         
627         if (!ConvertPythonToGameObject(value, &refOb, true, "actu.reference = value: KX_ObjectActuator"))
628                 return PY_SET_ATTR_FAIL;
629         
630         if (actuator->m_reference)
631                 actuator->m_reference->UnregisterActuator(actuator);
632         
633         if(refOb==NULL) {
634                 actuator->m_reference= NULL;
635         }
636         else {  
637                 actuator->m_reference = refOb;
638                 actuator->m_reference->RegisterActuator(actuator);
639         }
640         
641         return PY_SET_ATTR_SUCCESS;
642 }
643
644 /* eof */