get/set Angular velocity for KX_GameObjects python api and for the AddObject actuator.
[blender.git] / source / gameengine / Ketsji / KX_SCA_AddObjectActuator.cpp
1 //
2 // Add an object when this actuator is triggered
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 // Previously existed as:
31
32 // \source\gameengine\GameLogic\SCA_AddObjectActuator.cpp
33
34 // Please look here for revision history.
35
36
37 #include "KX_SCA_AddObjectActuator.h"
38 #include "SCA_IScene.h"
39 #include "KX_GameObject.h"
40 #include "KX_IPhysicsController.h"
41
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45
46 /* ------------------------------------------------------------------------- */
47 /* Native functions                                                          */
48 /* ------------------------------------------------------------------------- */
49
50 KX_SCA_AddObjectActuator::KX_SCA_AddObjectActuator(SCA_IObject *gameobj,
51                                                                                                    SCA_IObject *original,
52                                                                                                    int time,
53                                                                                                    SCA_IScene* scene,
54                                                                                                    const MT_Vector3& linvel,
55                                                                                                    bool linv_local,
56                                                                                                    const MT_Vector3& angvel,
57                                                                                                    bool angv_local,
58                                                                                                    PyTypeObject* T)
59         : 
60         SCA_IActuator(gameobj, T),
61         m_OriginalObject(original),
62         m_scene(scene),
63         
64         m_linear_velocity(linvel),
65         m_localLinvFlag(linv_local),
66         
67         m_angular_velocity(angvel),
68         m_localAngvFlag(angv_local)
69 {
70         if (m_OriginalObject)
71                 m_OriginalObject->RegisterActuator(this);
72
73         m_lastCreatedObject = NULL;
74         m_timeProp = time;
75
76
77
78
79 KX_SCA_AddObjectActuator::~KX_SCA_AddObjectActuator()
80
81         if (m_OriginalObject)
82                 m_OriginalObject->UnregisterActuator(this);
83         if (m_lastCreatedObject)
84                 m_lastCreatedObject->Release();
85
86
87
88
89 bool KX_SCA_AddObjectActuator::Update()
90 {
91         //bool result = false;  /*unused*/
92         bool bNegativeEvent = IsNegativeEvent();
93         RemoveAllEvents();
94         
95         if (bNegativeEvent) return false; // do nothing on negative events
96
97         InstantAddObject();
98
99         
100         return false;
101 }
102
103
104
105
106 SCA_IObject* KX_SCA_AddObjectActuator::GetLastCreatedObject() const 
107 {
108         return m_lastCreatedObject;
109 }
110
111
112
113 CValue* KX_SCA_AddObjectActuator::GetReplica() 
114 {
115         KX_SCA_AddObjectActuator* replica = new KX_SCA_AddObjectActuator(*this);
116
117         if (replica == NULL)
118                 return NULL;
119
120         // this will copy properties and so on...
121         replica->ProcessReplica();
122         CValue::AddDataToReplica(replica);
123
124         return replica;
125 }
126
127 void KX_SCA_AddObjectActuator::ProcessReplica()
128 {
129         if (m_OriginalObject)
130                 m_OriginalObject->RegisterActuator(this);
131         m_lastCreatedObject=NULL;
132         SCA_IActuator::ProcessReplica();
133 }
134
135 bool KX_SCA_AddObjectActuator::UnlinkObject(SCA_IObject* clientobj)
136 {
137         if (clientobj == m_OriginalObject)
138         {
139                 // this object is being deleted, we cannot continue to track it.
140                 m_OriginalObject = NULL;
141                 return true;
142         }
143         return false;
144 }
145
146 void KX_SCA_AddObjectActuator::Relink(GEN_Map<GEN_HashedPtr, void*> *obj_map)
147 {
148         void **h_obj = (*obj_map)[m_OriginalObject];
149         if (h_obj) {
150                 if (m_OriginalObject)
151                         m_OriginalObject->UnregisterActuator(this);
152                 m_OriginalObject = (SCA_IObject*)(*h_obj);
153                 m_OriginalObject->RegisterActuator(this);
154         }
155 }
156
157
158 /* ------------------------------------------------------------------------- */
159 /* Python functions                                                          */
160 /* ------------------------------------------------------------------------- */
161
162 /* Integration hooks ------------------------------------------------------- */
163 PyTypeObject KX_SCA_AddObjectActuator::Type = {
164         PyObject_HEAD_INIT(&PyType_Type)
165         0,
166         "KX_SCA_AddObjectActuator",
167         sizeof(KX_SCA_AddObjectActuator),
168         0,
169         PyDestructor,
170         0,
171         __getattr,
172         __setattr,
173         0, 
174         __repr,
175         0,
176         0,
177         0,
178         0,
179         0
180 };
181
182 PyParentObject KX_SCA_AddObjectActuator::Parents[] = {
183         &SCA_IActuator::Type,
184         &SCA_ILogicBrick::Type,
185         &CValue::Type,
186         NULL
187 };
188 PyMethodDef KX_SCA_AddObjectActuator::Methods[] = {
189   {"setObject", (PyCFunction) KX_SCA_AddObjectActuator::sPySetObject, METH_O, SetObject_doc},
190   {"setTime", (PyCFunction) KX_SCA_AddObjectActuator::sPySetTime, METH_O, SetTime_doc},
191   {"getObject", (PyCFunction) KX_SCA_AddObjectActuator::sPyGetObject, METH_VARARGS, GetObject_doc},
192   {"getTime", (PyCFunction) KX_SCA_AddObjectActuator::sPyGetTime, METH_NOARGS, GetTime_doc},
193   {"getLinearVelocity", (PyCFunction) KX_SCA_AddObjectActuator::sPyGetLinearVelocity, METH_NOARGS, GetLinearVelocity_doc},
194   {"setLinearVelocity", (PyCFunction) KX_SCA_AddObjectActuator::sPySetLinearVelocity, METH_VARARGS, SetLinearVelocity_doc},
195   {"getAngularVelocity", (PyCFunction) KX_SCA_AddObjectActuator::sPyGetAngularVelocity, METH_NOARGS, GetAngularVelocity_doc},
196   {"setAngularVelocity", (PyCFunction) KX_SCA_AddObjectActuator::sPySetAngularVelocity, METH_VARARGS, SetAngularVelocity_doc},
197   {"getLastCreatedObject", (PyCFunction) KX_SCA_AddObjectActuator::sPyGetLastCreatedObject, METH_NOARGS,"getLastCreatedObject() : get the object handle to the last created object\n"},
198   {"instantAddObject", (PyCFunction) KX_SCA_AddObjectActuator::sPyInstantAddObject, METH_NOARGS,"instantAddObject() : immediately add object without delay\n"},
199   
200   {NULL,NULL} //Sentinel
201 };
202
203
204 PyObject* KX_SCA_AddObjectActuator::_getattr(const STR_String& attr)
205 {
206   _getattr_up(SCA_IActuator);
207 }
208
209 /* 1. setObject */
210 char KX_SCA_AddObjectActuator::SetObject_doc[] = 
211 "setObject(object)\n"
212 "\t- object: KX_GameObject, string or None\n"
213 "\tSets the object that will be added. There has to be an object\n"
214 "\tof this name. If not, this function does nothing.\n";
215 PyObject* KX_SCA_AddObjectActuator::PySetObject(PyObject* self, PyObject* value)
216 {
217         KX_GameObject *gameobj;
218         
219         if (!ConvertPythonToGameObject(value, &gameobj, true))
220                 return NULL; // ConvertPythonToGameObject sets the error
221         
222         if (m_OriginalObject != NULL)
223                 m_OriginalObject->UnregisterActuator(this);     
224
225         m_OriginalObject = (SCA_IObject*)gameobj;
226         if (m_OriginalObject)
227                 m_OriginalObject->RegisterActuator(this);
228         
229         Py_RETURN_NONE;
230 }
231
232
233
234 /* 2. setTime */
235 char KX_SCA_AddObjectActuator::SetTime_doc[] = 
236 "setTime(duration)\n"
237 "\t- duration: integer\n"
238 "\tSets the lifetime of the object that will be added, in frames. \n"
239 "\tIf the duration is negative, it is set to 0.\n";
240
241
242 PyObject* KX_SCA_AddObjectActuator::PySetTime(PyObject* self, PyObject* value)
243 {
244         int deltatime = PyInt_AsLong(value);
245         if (deltatime==-1 && PyErr_Occurred()) {
246                 PyErr_SetString(PyExc_TypeError, "expected an int");
247                 return NULL;
248         }
249         
250         m_timeProp = deltatime;
251         if (m_timeProp < 0) m_timeProp = 0;
252         
253         Py_RETURN_NONE;
254 }
255
256
257
258 /* 3. getTime */
259 char KX_SCA_AddObjectActuator::GetTime_doc[] = 
260 "GetTime()\n"
261 "\tReturns the lifetime of the object that will be added.\n";
262
263
264 PyObject* KX_SCA_AddObjectActuator::PyGetTime(PyObject* self)
265 {
266         return PyInt_FromLong(m_timeProp);
267 }
268
269
270 /* 4. getObject */
271 char KX_SCA_AddObjectActuator::GetObject_doc[] = 
272 "getObject(name_only = 1)\n"
273 "name_only - optional arg, when true will return the KX_GameObject rather then its name\n"
274 "\tReturns the name of the object that will be added.\n";
275 PyObject* KX_SCA_AddObjectActuator::PyGetObject(PyObject* self, PyObject* args)
276 {
277         int ret_name_only = 1;
278         if (!PyArg_ParseTuple(args, "|i", &ret_name_only))
279                 return NULL;
280         
281         if (!m_OriginalObject)
282                 Py_RETURN_NONE;
283         
284         if (ret_name_only)
285                 return PyString_FromString(m_OriginalObject->GetName());
286         else
287                 return m_OriginalObject->AddRef();
288 }
289
290
291
292 /* 5. getLinearVelocity */
293 char KX_SCA_AddObjectActuator::GetLinearVelocity_doc[] = 
294 "GetLinearVelocity()\n"
295 "\tReturns the linear velocity that will be assigned to \n"
296 "\tthe created object.\n";
297
298 PyObject* KX_SCA_AddObjectActuator::PyGetLinearVelocity(PyObject* self)
299 {
300         PyObject *retVal = PyList_New(3);
301
302         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_linear_velocity[0]));
303         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_linear_velocity[1]));
304         PyList_SET_ITEM(retVal, 2, PyFloat_FromDouble(m_linear_velocity[2]));
305         
306         return retVal;
307 }
308
309
310
311 /* 6. setLinearVelocity                                                 */
312 char KX_SCA_AddObjectActuator::SetLinearVelocity_doc[] = 
313 "setLinearVelocity(vx, vy, vz)\n"
314 "\t- vx: float\n"
315 "\t- vy: float\n"
316 "\t- vz: float\n"
317 "\t- local: bool\n"
318 "\tAssign this velocity to the created object. \n";
319
320 PyObject* KX_SCA_AddObjectActuator::PySetLinearVelocity(PyObject* self, PyObject* args)
321 {
322         
323         float vecArg[3];
324         if (!PyArg_ParseTuple(args, "fff", &vecArg[0], &vecArg[1], &vecArg[2]))
325                 return NULL;
326
327         m_linear_velocity.setValue(vecArg);
328         Py_RETURN_NONE;
329 }
330
331 /* 7. getAngularVelocity */
332 char KX_SCA_AddObjectActuator::GetAngularVelocity_doc[] = 
333 "GetAngularVelocity()\n"
334 "\tReturns the angular velocity that will be assigned to \n"
335 "\tthe created object.\n";
336
337 PyObject* KX_SCA_AddObjectActuator::PyGetAngularVelocity(PyObject* self)
338 {
339         PyObject *retVal = PyList_New(3);
340
341         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_angular_velocity[0]));
342         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_angular_velocity[1]));
343         PyList_SET_ITEM(retVal, 2, PyFloat_FromDouble(m_angular_velocity[2]));
344         
345         return retVal;
346 }
347
348
349
350 /* 8. setAngularVelocity                                                 */
351 char KX_SCA_AddObjectActuator::SetAngularVelocity_doc[] = 
352 "setAngularVelocity(vx, vy, vz)\n"
353 "\t- vx: float\n"
354 "\t- vy: float\n"
355 "\t- vz: float\n"
356 "\t- local: bool\n"
357 "\tAssign this angular velocity to the created object. \n";
358
359 PyObject* KX_SCA_AddObjectActuator::PySetAngularVelocity(PyObject* self, PyObject* args)
360 {
361         
362         float vecArg[3];
363         if (!PyArg_ParseTuple(args, "fff", &vecArg[0], &vecArg[1], &vecArg[2]))
364                 return NULL;
365
366         m_angular_velocity.setValue(vecArg);
367         Py_RETURN_NONE;
368 }
369
370 void    KX_SCA_AddObjectActuator::InstantAddObject()
371 {
372         if (m_OriginalObject)
373         {
374                 // Add an identical object, with properties inherited from the original object  
375                 // Now it needs to be added to the current scene.
376                 SCA_IObject* replica = m_scene->AddReplicaObject(m_OriginalObject,GetParent(),m_timeProp );
377                 KX_GameObject * game_obj = static_cast<KX_GameObject *>(replica);
378                 game_obj->setLinearVelocity(m_linear_velocity,m_localLinvFlag);
379                 game_obj->setAngularVelocity(m_angular_velocity,m_localAngvFlag);
380                 game_obj->ResolveCombinedVelocities(m_linear_velocity, m_angular_velocity, m_localLinvFlag, m_localAngvFlag);
381
382                 // keep a copy of the last object, to allow python scripters to change it
383                 if (m_lastCreatedObject)
384                 {
385                         //careful with destruction, it might still have outstanding collision callbacks
386                         m_scene->DelayedReleaseObject(m_lastCreatedObject);
387                         m_lastCreatedObject->Release();
388                 }
389                 
390                 m_lastCreatedObject = replica;
391                 m_lastCreatedObject->AddRef();
392                 // finished using replica? then release it
393                 replica->Release();
394         }
395 }
396
397 PyObject* KX_SCA_AddObjectActuator::PyInstantAddObject(PyObject* self)
398 {
399         InstantAddObject();
400
401         Py_RETURN_NONE;
402 }
403
404
405
406 /* 7. GetLastCreatedObject                                                */
407 char KX_SCA_AddObjectActuator::GetLastCreatedObject_doc[] = 
408 "getLastCreatedObject()\n"
409 "\tReturn the last created object. \n";
410
411
412 PyObject* KX_SCA_AddObjectActuator::PyGetLastCreatedObject(PyObject* self)
413 {
414         SCA_IObject* result = this->GetLastCreatedObject();
415         
416         // if result->GetSGNode() is NULL
417         // it means the object has ended, The BGE python api crashes in many places if the object is returned.
418         if (result && (static_cast<KX_GameObject *>(result))->GetSGNode()) 
419         {
420                 result->AddRef();
421                 return result;
422         }
423         // don't return NULL to python anymore, it gives trouble in the scripts
424         Py_RETURN_NONE;
425 }