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