2ee30ee0bfaa489257398989b9788b94a333049f
[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         // Deprecated ----->
351         {"getForce", (PyCFunction) KX_ObjectActuator::sPyGetForce, METH_NOARGS},
352         {"setForce", (PyCFunction) KX_ObjectActuator::sPySetForce, METH_VARARGS},
353         {"getTorque", (PyCFunction) KX_ObjectActuator::sPyGetTorque, METH_NOARGS},
354         {"setTorque", (PyCFunction) KX_ObjectActuator::sPySetTorque, METH_VARARGS},
355         {"getDLoc", (PyCFunction) KX_ObjectActuator::sPyGetDLoc, METH_NOARGS},
356         {"setDLoc", (PyCFunction) KX_ObjectActuator::sPySetDLoc, METH_VARARGS},
357         {"getDRot", (PyCFunction) KX_ObjectActuator::sPyGetDRot, METH_NOARGS},
358         {"setDRot", (PyCFunction) KX_ObjectActuator::sPySetDRot, METH_VARARGS},
359         {"getLinearVelocity", (PyCFunction) KX_ObjectActuator::sPyGetLinearVelocity, METH_NOARGS},
360         {"setLinearVelocity", (PyCFunction) KX_ObjectActuator::sPySetLinearVelocity, METH_VARARGS},
361         {"getAngularVelocity", (PyCFunction) KX_ObjectActuator::sPyGetAngularVelocity, METH_NOARGS},
362         {"setAngularVelocity", (PyCFunction) KX_ObjectActuator::sPySetAngularVelocity, METH_VARARGS},
363         {"setDamping", (PyCFunction) KX_ObjectActuator::sPySetDamping, METH_VARARGS},
364         {"getDamping", (PyCFunction) KX_ObjectActuator::sPyGetDamping, METH_NOARGS},
365         {"setForceLimitX", (PyCFunction) KX_ObjectActuator::sPySetForceLimitX, METH_VARARGS},
366         {"getForceLimitX", (PyCFunction) KX_ObjectActuator::sPyGetForceLimitX, METH_NOARGS},
367         {"setForceLimitY", (PyCFunction) KX_ObjectActuator::sPySetForceLimitY, METH_VARARGS},
368         {"getForceLimitY", (PyCFunction) KX_ObjectActuator::sPyGetForceLimitY, METH_NOARGS},
369         {"setForceLimitZ", (PyCFunction) KX_ObjectActuator::sPySetForceLimitZ, METH_VARARGS},
370         {"getForceLimitZ", (PyCFunction) KX_ObjectActuator::sPyGetForceLimitZ, METH_NOARGS},
371         {"setPID", (PyCFunction) KX_ObjectActuator::sPyGetPID, METH_NOARGS},
372         {"getPID", (PyCFunction) KX_ObjectActuator::sPySetPID, METH_VARARGS},
373
374         // <----- Deprecated
375
376         {NULL,NULL} //Sentinel
377 };
378
379 PyAttributeDef KX_ObjectActuator::Attributes[] = {
380         KX_PYATTRIBUTE_VECTOR_RW_CHECK("force", -1000, 1000, false, KX_ObjectActuator, m_force, PyUpdateFuzzyFlags),
381         KX_PYATTRIBUTE_BOOL_RW("useLocalForce", KX_ObjectActuator, m_bitLocalFlag.Force),
382         KX_PYATTRIBUTE_VECTOR_RW_CHECK("torque", -1000, 1000, false, KX_ObjectActuator, m_torque, PyUpdateFuzzyFlags),
383         KX_PYATTRIBUTE_BOOL_RW("useLocalTorque", KX_ObjectActuator, m_bitLocalFlag.Torque),
384         KX_PYATTRIBUTE_VECTOR_RW_CHECK("dLoc", -1000, 1000, false, KX_ObjectActuator, m_dloc, PyUpdateFuzzyFlags),
385         KX_PYATTRIBUTE_BOOL_RW("useLocalDLoc", KX_ObjectActuator, m_bitLocalFlag.DLoc),
386         KX_PYATTRIBUTE_VECTOR_RW_CHECK("dRot", -1000, 1000, false, KX_ObjectActuator, m_drot, PyUpdateFuzzyFlags),
387         KX_PYATTRIBUTE_BOOL_RW("useLocalDRot", KX_ObjectActuator, m_bitLocalFlag.DRot),
388 #ifdef USE_MATHUTILS
389         KX_PYATTRIBUTE_RW_FUNCTION("linV", KX_ObjectActuator, pyattr_get_linV, pyattr_set_linV),
390         KX_PYATTRIBUTE_RW_FUNCTION("angV", KX_ObjectActuator, pyattr_get_angV, pyattr_set_angV),
391 #else
392         KX_PYATTRIBUTE_VECTOR_RW_CHECK("linV", -1000, 1000, false, KX_ObjectActuator, m_linear_velocity, PyUpdateFuzzyFlags),
393         KX_PYATTRIBUTE_VECTOR_RW_CHECK("angV", -1000, 1000, false, KX_ObjectActuator, m_angular_velocity, PyUpdateFuzzyFlags),
394 #endif
395         KX_PYATTRIBUTE_BOOL_RW("useLocalLinV", KX_ObjectActuator, m_bitLocalFlag.LinearVelocity),
396         KX_PYATTRIBUTE_BOOL_RW("useLocalAngV", KX_ObjectActuator, m_bitLocalFlag.AngularVelocity),
397         KX_PYATTRIBUTE_SHORT_RW("damping", 0, 1000, false, KX_ObjectActuator, m_damping),
398         KX_PYATTRIBUTE_RW_FUNCTION("forceLimitX", KX_ObjectActuator, pyattr_get_forceLimitX, pyattr_set_forceLimitX),
399         KX_PYATTRIBUTE_RW_FUNCTION("forceLimitY", KX_ObjectActuator, pyattr_get_forceLimitY, pyattr_set_forceLimitY),
400         KX_PYATTRIBUTE_RW_FUNCTION("forceLimitZ", KX_ObjectActuator, pyattr_get_forceLimitZ, pyattr_set_forceLimitZ),
401         KX_PYATTRIBUTE_VECTOR_RW_CHECK("pid", -100, 200, true, KX_ObjectActuator, m_pid, PyCheckPid),
402         KX_PYATTRIBUTE_RW_FUNCTION("reference", KX_ObjectActuator,pyattr_get_reference,pyattr_set_reference),
403         { NULL }        //Sentinel
404 };
405
406 /* Attribute get/set functions */
407
408 #ifdef USE_MATHUTILS
409
410 /* These require an SGNode */
411 #define MATHUTILS_VEC_CB_LINV 1
412 #define MATHUTILS_VEC_CB_ANGV 2
413
414 static int mathutils_kxobactu_vector_cb_index= -1; /* index for our callbacks */
415
416 static int mathutils_obactu_generic_check(PyObject *self_v)
417 {
418         KX_ObjectActuator* self= static_cast<KX_ObjectActuator*>BGE_PROXY_REF(self_v);
419         if(self==NULL)
420                 return 0;
421
422         return 1;
423 }
424
425 static int mathutils_obactu_vector_get(PyObject *self_v, int subtype, float *vec_from)
426 {
427         KX_ObjectActuator* self= static_cast<KX_ObjectActuator*>BGE_PROXY_REF(self_v);
428         if(self==NULL)
429                 return 0;
430
431         switch(subtype) {
432                 case MATHUTILS_VEC_CB_LINV:
433                         self->m_linear_velocity.getValue(vec_from);
434                         break;
435                 case MATHUTILS_VEC_CB_ANGV:
436                         self->m_angular_velocity.getValue(vec_from);
437                         break;
438         }
439
440         return 1;
441 }
442
443 static int mathutils_obactu_vector_set(PyObject *self_v, int subtype, float *vec_to)
444 {
445         KX_ObjectActuator* self= static_cast<KX_ObjectActuator*>BGE_PROXY_REF(self_v);
446         if(self==NULL)
447                 return 0;
448
449         switch(subtype) {
450                 case MATHUTILS_VEC_CB_LINV:
451                         self->m_linear_velocity.setValue(vec_to);
452                         break;
453                 case MATHUTILS_VEC_CB_ANGV:
454                         self->m_angular_velocity.setValue(vec_to);
455                         break;
456         }
457
458         return 1;
459 }
460
461 static int mathutils_obactu_vector_get_index(PyObject *self_v, int subtype, float *vec_from, int index)
462 {
463         float f[4];
464         /* lazy, avoid repeteing the case statement */
465         if(!mathutils_obactu_vector_get(self_v, subtype, f))
466                 return 0;
467
468         vec_from[index]= f[index];
469         return 1;
470 }
471
472 static int mathutils_obactu_vector_set_index(PyObject *self_v, int subtype, float *vec_to, int index)
473 {
474         float f= vec_to[index];
475
476         /* lazy, avoid repeteing the case statement */
477         if(!mathutils_obactu_vector_get(self_v, subtype, vec_to))
478                 return 0;
479
480         vec_to[index]= f;
481         mathutils_obactu_vector_set(self_v, subtype, vec_to);
482
483         return 1;
484 }
485
486 Mathutils_Callback mathutils_obactu_vector_cb = {
487         mathutils_obactu_generic_check,
488         mathutils_obactu_vector_get,
489         mathutils_obactu_vector_set,
490         mathutils_obactu_vector_get_index,
491         mathutils_obactu_vector_set_index
492 };
493
494 PyObject* KX_ObjectActuator::pyattr_get_linV(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
495 {
496         return newVectorObject_cb((PyObject *)self_v, 3, mathutils_kxobactu_vector_cb_index, MATHUTILS_VEC_CB_LINV);
497 }
498
499 int KX_ObjectActuator::pyattr_set_linV(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
500 {
501         KX_ObjectActuator* self= static_cast<KX_ObjectActuator*>(self_v);
502         if (!PyVecTo(value, self->m_linear_velocity))
503                 return PY_SET_ATTR_FAIL;
504
505         return PY_SET_ATTR_SUCCESS;
506 }
507
508 PyObject* KX_ObjectActuator::pyattr_get_angV(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
509 {
510         return newVectorObject_cb((PyObject *)self_v, 3, mathutils_kxobactu_vector_cb_index, MATHUTILS_VEC_CB_ANGV);
511 }
512
513 int KX_ObjectActuator::pyattr_set_angV(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
514 {
515         KX_ObjectActuator* self= static_cast<KX_ObjectActuator*>(self_v);
516         if (!PyVecTo(value, self->m_angular_velocity))
517                 return PY_SET_ATTR_FAIL;
518
519         return PY_SET_ATTR_SUCCESS;
520 }
521
522
523 void KX_ObjectActuator_Mathutils_Callback_Init(void)
524 {
525         // register mathutils callbacks, ok to run more then once.
526         mathutils_kxobactu_vector_cb_index= Mathutils_RegisterCallback(&mathutils_obactu_vector_cb);
527 }
528
529 #endif // USE_MATHUTILS
530
531 PyObject* KX_ObjectActuator::pyattr_get_forceLimitX(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
532 {
533         KX_ObjectActuator* self = reinterpret_cast<KX_ObjectActuator*>(self_v);
534         PyObject *retVal = PyList_New(3);
535
536         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(self->m_drot[0]));
537         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(self->m_dloc[0]));
538         PyList_SET_ITEM(retVal, 2, PyBool_FromLong(self->m_bitLocalFlag.Torque));
539         
540         return retVal;
541 }
542
543 int KX_ObjectActuator::pyattr_set_forceLimitX(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
544 {
545         KX_ObjectActuator* self = reinterpret_cast<KX_ObjectActuator*>(self_v);
546
547         PyObject* seq = PySequence_Fast(value, "");
548         if (seq && PySequence_Fast_GET_SIZE(seq) == 3)
549         {
550                 self->m_drot[0] = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value, 0));
551                 self->m_dloc[0] = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value, 1));
552                 self->m_bitLocalFlag.Torque = (PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 2)) != 0);
553
554                 if (!PyErr_Occurred())
555                 {
556                         Py_DECREF(seq);
557                         return PY_SET_ATTR_SUCCESS;
558                 }
559         }
560
561         Py_XDECREF(seq);
562
563         PyErr_SetString(PyExc_ValueError, "expected a sequence of 2 floats and a bool");
564         return PY_SET_ATTR_FAIL;
565 }
566
567 PyObject* KX_ObjectActuator::pyattr_get_forceLimitY(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
568 {
569         KX_ObjectActuator* self = reinterpret_cast<KX_ObjectActuator*>(self_v);
570         PyObject *retVal = PyList_New(3);
571
572         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(self->m_drot[1]));
573         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(self->m_dloc[1]));
574         PyList_SET_ITEM(retVal, 2, PyBool_FromLong(self->m_bitLocalFlag.DLoc));
575         
576         return retVal;
577 }
578
579 int     KX_ObjectActuator::pyattr_set_forceLimitY(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
580 {
581         KX_ObjectActuator* self = reinterpret_cast<KX_ObjectActuator*>(self_v);
582
583         PyObject* seq = PySequence_Fast(value, "");
584         if (seq && PySequence_Fast_GET_SIZE(seq) == 3)
585         {
586                 self->m_drot[1] = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value, 0));
587                 self->m_dloc[1] = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value, 1));
588                 self->m_bitLocalFlag.DLoc = (PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 2)) != 0);
589
590                 if (!PyErr_Occurred())
591                 {
592                         Py_DECREF(seq);
593                         return PY_SET_ATTR_SUCCESS;
594                 }
595         }
596
597         Py_XDECREF(seq);
598
599         PyErr_SetString(PyExc_ValueError, "expected a sequence of 2 floats and a bool");
600         return PY_SET_ATTR_FAIL;
601 }
602
603 PyObject* KX_ObjectActuator::pyattr_get_forceLimitZ(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
604 {
605         KX_ObjectActuator* self = reinterpret_cast<KX_ObjectActuator*>(self_v);
606         PyObject *retVal = PyList_New(3);
607
608         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(self->m_drot[2]));
609         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(self->m_dloc[2]));
610         PyList_SET_ITEM(retVal, 2, PyBool_FromLong(self->m_bitLocalFlag.DRot));
611         
612         return retVal;
613 }
614
615 int     KX_ObjectActuator::pyattr_set_forceLimitZ(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
616 {
617         KX_ObjectActuator* self = reinterpret_cast<KX_ObjectActuator*>(self_v);
618
619         PyObject* seq = PySequence_Fast(value, "");
620         if (seq && PySequence_Fast_GET_SIZE(seq) == 3)
621         {
622                 self->m_drot[2] = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value, 0));
623                 self->m_dloc[2] = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value, 1));
624                 self->m_bitLocalFlag.DRot = (PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 2)) != 0);
625
626                 if (!PyErr_Occurred())
627                 {
628                         Py_DECREF(seq);
629                         return PY_SET_ATTR_SUCCESS;
630                 }
631         }
632
633         Py_XDECREF(seq);
634
635         PyErr_SetString(PyExc_ValueError, "expected a sequence of 2 floats and a bool");
636         return PY_SET_ATTR_FAIL;
637 }
638
639 PyObject* KX_ObjectActuator::pyattr_get_reference(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
640 {
641         KX_ObjectActuator* actuator = static_cast<KX_ObjectActuator*>(self);
642         if (!actuator->m_reference)
643                 Py_RETURN_NONE;
644         
645         return actuator->m_reference->GetProxy();
646 }
647
648 int KX_ObjectActuator::pyattr_set_reference(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
649 {
650         KX_ObjectActuator* actuator = static_cast<KX_ObjectActuator*>(self);
651         KX_GameObject *refOb;
652         
653         if (!ConvertPythonToGameObject(value, &refOb, true, "actu.reference = value: KX_ObjectActuator"))
654                 return PY_SET_ATTR_FAIL;
655         
656         if (actuator->m_reference)
657                 actuator->m_reference->UnregisterActuator(actuator);
658         
659         if(refOb==NULL) {
660                 actuator->m_reference= NULL;
661         }
662         else {  
663                 actuator->m_reference = refOb;
664                 actuator->m_reference->RegisterActuator(actuator);
665         }
666         
667         return PY_SET_ATTR_SUCCESS;
668 }
669
670
671 /* 1. set ------------------------------------------------------------------ */
672 /* Removed! */
673
674 /* 2. getForce                                                               */
675 PyObject* KX_ObjectActuator::PyGetForce()
676 {
677         ShowDeprecationWarning("getForce()", "the force and the useLocalForce properties");
678         PyObject *retVal = PyList_New(4);
679
680         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_force[0]));
681         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_force[1]));
682         PyList_SET_ITEM(retVal, 2, PyFloat_FromDouble(m_force[2]));
683         PyList_SET_ITEM(retVal, 3, BoolToPyArg(m_bitLocalFlag.Force));
684         
685         return retVal;
686 }
687 /* 3. setForce                                                               */
688 PyObject* KX_ObjectActuator::PySetForce(PyObject* args)
689 {
690         ShowDeprecationWarning("setForce()", "the force and the useLocalForce properties");
691         float vecArg[3];
692         int bToggle = 0;
693         if (!PyArg_ParseTuple(args, "fffi:setForce", &vecArg[0], &vecArg[1], 
694                                                   &vecArg[2], &bToggle)) {
695                 return NULL;
696         }
697         m_force.setValue(vecArg);
698         m_bitLocalFlag.Force = PyArgToBool(bToggle);
699         UpdateFuzzyFlags();
700         Py_RETURN_NONE;
701 }
702
703 /* 4. getTorque                                                              */
704 PyObject* KX_ObjectActuator::PyGetTorque()
705 {
706         ShowDeprecationWarning("getTorque()", "the torque and the useLocalTorque properties");
707         PyObject *retVal = PyList_New(4);
708
709         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_torque[0]));
710         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_torque[1]));
711         PyList_SET_ITEM(retVal, 2, PyFloat_FromDouble(m_torque[2]));
712         PyList_SET_ITEM(retVal, 3, BoolToPyArg(m_bitLocalFlag.Torque));
713         
714         return retVal;
715 }
716 /* 5. setTorque                                                              */
717 PyObject* KX_ObjectActuator::PySetTorque(PyObject* args)
718 {
719         ShowDeprecationWarning("setTorque()", "the torque and the useLocalTorque properties");
720         float vecArg[3];
721         int bToggle = 0;
722         if (!PyArg_ParseTuple(args, "fffi:setTorque", &vecArg[0], &vecArg[1], 
723                                                   &vecArg[2], &bToggle)) {
724                 return NULL;
725         }
726         m_torque.setValue(vecArg);
727         m_bitLocalFlag.Torque = PyArgToBool(bToggle);
728         UpdateFuzzyFlags();
729         Py_RETURN_NONE;
730 }
731
732 /* 6. getDLoc                                                                */
733 PyObject* KX_ObjectActuator::PyGetDLoc()
734 {
735         ShowDeprecationWarning("getDLoc()", "the dLoc and the useLocalDLoc properties");
736         PyObject *retVal = PyList_New(4);
737
738         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_dloc[0]));
739         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_dloc[1]));
740         PyList_SET_ITEM(retVal, 2, PyFloat_FromDouble(m_dloc[2]));
741         PyList_SET_ITEM(retVal, 3, BoolToPyArg(m_bitLocalFlag.DLoc));
742         
743         return retVal;
744 }
745 /* 7. setDLoc                                                                */
746 PyObject* KX_ObjectActuator::PySetDLoc(PyObject* args)
747 {
748         ShowDeprecationWarning("setDLoc()", "the dLoc and the useLocalDLoc properties");
749         float vecArg[3];
750         int bToggle = 0;
751         if(!PyArg_ParseTuple(args, "fffi:setDLoc", &vecArg[0], &vecArg[1], 
752                                                  &vecArg[2], &bToggle)) {
753                 return NULL;
754         }
755         m_dloc.setValue(vecArg);
756         m_bitLocalFlag.DLoc = PyArgToBool(bToggle);
757         UpdateFuzzyFlags();
758         Py_RETURN_NONE;
759 }
760
761 /* 8. getDRot                                                                */
762 PyObject* KX_ObjectActuator::PyGetDRot()
763 {
764         ShowDeprecationWarning("getDRot()", "the dRot and the useLocalDRot properties");
765         PyObject *retVal = PyList_New(4);
766
767         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_drot[0]));
768         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_drot[1]));
769         PyList_SET_ITEM(retVal, 2, PyFloat_FromDouble(m_drot[2]));
770         PyList_SET_ITEM(retVal, 3, BoolToPyArg(m_bitLocalFlag.DRot));
771         
772         return retVal;
773 }
774 /* 9. setDRot                                                                */
775 PyObject* KX_ObjectActuator::PySetDRot(PyObject* args)
776 {
777         ShowDeprecationWarning("setDRot()", "the dRot and the useLocalDRot properties");
778         float vecArg[3];
779         int bToggle = 0;
780         if (!PyArg_ParseTuple(args, "fffi:setDRot", &vecArg[0], &vecArg[1], 
781                                                   &vecArg[2], &bToggle)) {
782                 return NULL;
783         }
784         m_drot.setValue(vecArg);
785         m_bitLocalFlag.DRot = PyArgToBool(bToggle);
786         UpdateFuzzyFlags();
787         Py_RETURN_NONE;
788 }
789
790 /* 10. getLinearVelocity                                                 */
791 PyObject* KX_ObjectActuator::PyGetLinearVelocity() {
792         ShowDeprecationWarning("getLinearVelocity()", "the linV and the useLocalLinV properties");
793         PyObject *retVal = PyList_New(4);
794
795         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_linear_velocity[0]));
796         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_linear_velocity[1]));
797         PyList_SET_ITEM(retVal, 2, PyFloat_FromDouble(m_linear_velocity[2]));
798         PyList_SET_ITEM(retVal, 3, BoolToPyArg(m_bitLocalFlag.LinearVelocity));
799         
800         return retVal;
801 }
802
803 /* 11. setLinearVelocity                                                 */
804 PyObject* KX_ObjectActuator::PySetLinearVelocity(PyObject* args) {
805         ShowDeprecationWarning("setLinearVelocity()", "the linV and the useLocalLinV properties");
806         float vecArg[3];
807         int bToggle = 0;
808         if (!PyArg_ParseTuple(args, "fffi:setLinearVelocity", &vecArg[0], &vecArg[1], 
809                                                   &vecArg[2], &bToggle)) {
810                 return NULL;
811         }
812         m_linear_velocity.setValue(vecArg);
813         m_bitLocalFlag.LinearVelocity = PyArgToBool(bToggle);
814         UpdateFuzzyFlags();
815         Py_RETURN_NONE;
816 }
817
818
819 /* 12. getAngularVelocity                                                */
820 PyObject* KX_ObjectActuator::PyGetAngularVelocity() {
821         ShowDeprecationWarning("getAngularVelocity()", "the angV and the useLocalAngV properties");
822         PyObject *retVal = PyList_New(4);
823
824         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_angular_velocity[0]));
825         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_angular_velocity[1]));
826         PyList_SET_ITEM(retVal, 2, PyFloat_FromDouble(m_angular_velocity[2]));
827         PyList_SET_ITEM(retVal, 3, BoolToPyArg(m_bitLocalFlag.AngularVelocity));
828         
829         return retVal;
830 }
831 /* 13. setAngularVelocity                                                */
832 PyObject* KX_ObjectActuator::PySetAngularVelocity(PyObject* args) {
833         ShowDeprecationWarning("setAngularVelocity()", "the angV and the useLocalAngV properties");
834         float vecArg[3];
835         int bToggle = 0;
836         if (!PyArg_ParseTuple(args, "fffi:setAngularVelocity", &vecArg[0], &vecArg[1], 
837                                                   &vecArg[2], &bToggle)) {
838                 return NULL;
839         }
840         m_angular_velocity.setValue(vecArg);
841         m_bitLocalFlag.AngularVelocity = PyArgToBool(bToggle);
842         UpdateFuzzyFlags();
843         Py_RETURN_NONE;
844 }
845
846 /* 13. setDamping                                                */
847 PyObject* KX_ObjectActuator::PySetDamping(PyObject* args) {
848         ShowDeprecationWarning("setDamping()", "the damping property");
849         int damping = 0;
850         if (!PyArg_ParseTuple(args, "i:setDamping", &damping) || damping < 0 || damping > 1000) {
851                 return NULL;
852         }
853         m_damping = damping;
854         Py_RETURN_NONE;
855 }
856
857 /* 13. getVelocityDamping                                                */
858 PyObject* KX_ObjectActuator::PyGetDamping() {
859         ShowDeprecationWarning("getDamping()", "the damping property");
860         return Py_BuildValue("i",m_damping);
861 }
862 /* 6. getForceLimitX                                                                */
863 PyObject* KX_ObjectActuator::PyGetForceLimitX()
864 {
865         ShowDeprecationWarning("getForceLimitX()", "the forceLimitX property");
866         PyObject *retVal = PyList_New(3);
867
868         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_drot[0]));
869         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_dloc[0]));
870         PyList_SET_ITEM(retVal, 2, BoolToPyArg(m_bitLocalFlag.Torque));
871         
872         return retVal;
873 }
874 /* 7. setForceLimitX                                                         */
875 PyObject* KX_ObjectActuator::PySetForceLimitX(PyObject* args)
876 {
877         ShowDeprecationWarning("setForceLimitX()", "the forceLimitX property");
878         float vecArg[2];
879         int bToggle = 0;
880         if(!PyArg_ParseTuple(args, "ffi:setForceLimitX", &vecArg[0], &vecArg[1], &bToggle)) {
881                 return NULL;
882         }
883         m_drot[0] = vecArg[0];
884         m_dloc[0] = vecArg[1];
885         m_bitLocalFlag.Torque = PyArgToBool(bToggle);
886         Py_RETURN_NONE;
887 }
888
889 /* 6. getForceLimitY                                                                */
890 PyObject* KX_ObjectActuator::PyGetForceLimitY()
891 {
892         ShowDeprecationWarning("getForceLimitY()", "the forceLimitY property");
893         PyObject *retVal = PyList_New(3);
894
895         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_drot[1]));
896         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_dloc[1]));
897         PyList_SET_ITEM(retVal, 2, BoolToPyArg(m_bitLocalFlag.DLoc));
898         
899         return retVal;
900 }
901 /* 7. setForceLimitY                                                                */
902 PyObject* KX_ObjectActuator::PySetForceLimitY(PyObject* args)
903 {
904         ShowDeprecationWarning("setForceLimitY()", "the forceLimitY property");
905         float vecArg[2];
906         int bToggle = 0;
907         if(!PyArg_ParseTuple(args, "ffi:setForceLimitY", &vecArg[0], &vecArg[1], &bToggle)) {
908                 return NULL;
909         }
910         m_drot[1] = vecArg[0];
911         m_dloc[1] = vecArg[1];
912         m_bitLocalFlag.DLoc = PyArgToBool(bToggle);
913         Py_RETURN_NONE;
914 }
915
916 /* 6. getForceLimitZ                                                                */
917 PyObject* KX_ObjectActuator::PyGetForceLimitZ()
918 {
919         ShowDeprecationWarning("getForceLimitZ()", "the forceLimitZ property");
920         PyObject *retVal = PyList_New(3);
921
922         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_drot[2]));
923         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_dloc[2]));
924         PyList_SET_ITEM(retVal, 2, BoolToPyArg(m_bitLocalFlag.DRot));
925         
926         return retVal;
927 }
928 /* 7. setForceLimitZ                                                                */
929 PyObject* KX_ObjectActuator::PySetForceLimitZ(PyObject* args)
930 {
931         ShowDeprecationWarning("setForceLimitZ()", "the forceLimitZ property");
932         float vecArg[2];
933         int bToggle = 0;
934         if(!PyArg_ParseTuple(args, "ffi:setForceLimitZ", &vecArg[0], &vecArg[1], &bToggle)) {
935                 return NULL;
936         }
937         m_drot[2] = vecArg[0];
938         m_dloc[2] = vecArg[1];
939         m_bitLocalFlag.DRot = PyArgToBool(bToggle);
940         Py_RETURN_NONE;
941 }
942
943 /* 4. getPID                                                              */
944 PyObject* KX_ObjectActuator::PyGetPID()
945 {
946         ShowDeprecationWarning("getPID()", "the pid property");
947         PyObject *retVal = PyList_New(3);
948
949         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_pid[0]));
950         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_pid[1]));
951         PyList_SET_ITEM(retVal, 2, PyFloat_FromDouble(m_pid[2]));
952         
953         return retVal;
954 }
955 /* 5. setPID                                                              */
956 PyObject* KX_ObjectActuator::PySetPID(PyObject* args)
957 {
958         ShowDeprecationWarning("setPID()", "the pid property");
959         float vecArg[3];
960         if (!PyArg_ParseTuple(args, "fff:setPID", &vecArg[0], &vecArg[1], &vecArg[2])) {
961                 return NULL;
962         }
963         m_pid.setValue(vecArg);
964         Py_RETURN_NONE;
965 }
966
967
968
969
970
971 /* eof */