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