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