Undo revision 23130 which was a merge with 2.5, a messy one because I did something...
[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         PyVarObject_HEAD_INIT(NULL, 0)
174         "KX_SCA_AddObjectActuator",
175         sizeof(PyObjectPlus_Proxy),
176         0,
177         py_base_dealloc,
178         0,
179         0,
180         0,
181         0,
182         py_base_repr,
183         0,0,0,0,0,0,0,0,0,
184         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
185         0,0,0,0,0,0,0,
186         Methods,
187         0,
188         0,
189         &SCA_IActuator::Type,
190         0,0,0,0,0,0,
191         py_base_new
192 };
193
194 PyMethodDef KX_SCA_AddObjectActuator::Methods[] = {
195   // ---> deprecated
196   {"setTime", (PyCFunction) KX_SCA_AddObjectActuator::sPySetTime, METH_O, (const char *)SetTime_doc},
197   {"getTime", (PyCFunction) KX_SCA_AddObjectActuator::sPyGetTime, METH_NOARGS, (const char *)GetTime_doc},
198   {"getLinearVelocity", (PyCFunction) KX_SCA_AddObjectActuator::sPyGetLinearVelocity, METH_NOARGS, (const char *)GetLinearVelocity_doc},
199   {"setLinearVelocity", (PyCFunction) KX_SCA_AddObjectActuator::sPySetLinearVelocity, METH_VARARGS, (const char *)SetLinearVelocity_doc},
200   {"getAngularVelocity", (PyCFunction) KX_SCA_AddObjectActuator::sPyGetAngularVelocity, METH_NOARGS, (const char *)GetAngularVelocity_doc},
201   {"setAngularVelocity", (PyCFunction) KX_SCA_AddObjectActuator::sPySetAngularVelocity, METH_VARARGS, (const char *)SetAngularVelocity_doc},
202   {"getLastCreatedObject", (PyCFunction) KX_SCA_AddObjectActuator::sPyGetLastCreatedObject, METH_NOARGS,"getLastCreatedObject() : get the object handle to the last created object\n"},
203   {"instantAddObject", (PyCFunction) KX_SCA_AddObjectActuator::sPyInstantAddObject, METH_NOARGS,"instantAddObject() : immediately add object without delay\n"},
204   {"setObject", (PyCFunction) KX_SCA_AddObjectActuator::sPySetObject, METH_O, (const char *)SetObject_doc},
205   {"getObject", (PyCFunction) KX_SCA_AddObjectActuator::sPyGetObject, METH_VARARGS, (const char *)GetObject_doc},
206   
207   {NULL,NULL} //Sentinel
208 };
209
210 PyAttributeDef KX_SCA_AddObjectActuator::Attributes[] = {
211         KX_PYATTRIBUTE_RW_FUNCTION("object",KX_SCA_AddObjectActuator,pyattr_get_object,pyattr_set_object),
212         KX_PYATTRIBUTE_RO_FUNCTION("objectLastCreated",KX_SCA_AddObjectActuator,pyattr_get_objectLastCreated),
213         KX_PYATTRIBUTE_INT_RW("time",0,2000,true,KX_SCA_AddObjectActuator,m_timeProp),
214         KX_PYATTRIBUTE_FLOAT_ARRAY_RW("linearVelocity",-FLT_MAX,FLT_MAX,KX_SCA_AddObjectActuator,m_linear_velocity,3),
215         KX_PYATTRIBUTE_FLOAT_ARRAY_RW("angularVelocity",-FLT_MAX,FLT_MAX,KX_SCA_AddObjectActuator,m_angular_velocity,3),
216         { NULL }        //Sentinel
217 };
218
219 PyObject* KX_SCA_AddObjectActuator::pyattr_get_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
220 {
221         KX_SCA_AddObjectActuator* actuator = static_cast<KX_SCA_AddObjectActuator*>(self);
222         if (!actuator->m_OriginalObject)        
223                 Py_RETURN_NONE;
224         else
225                 return actuator->m_OriginalObject->GetProxy();
226 }
227
228 int KX_SCA_AddObjectActuator::pyattr_set_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
229 {
230         KX_SCA_AddObjectActuator* actuator = static_cast<KX_SCA_AddObjectActuator*>(self);
231         KX_GameObject *gameobj;
232                 
233         if (!ConvertPythonToGameObject(value, &gameobj, true, "actuator.object = value: KX_SCA_AddObjectActuator"))
234                 return PY_SET_ATTR_FAIL; // ConvertPythonToGameObject sets the error
235                 
236         if (actuator->m_OriginalObject != NULL)
237                 actuator->m_OriginalObject->UnregisterActuator(actuator);       
238
239         actuator->m_OriginalObject = (SCA_IObject*)gameobj;
240                 
241         if (actuator->m_OriginalObject)
242                 actuator->m_OriginalObject->RegisterActuator(actuator);
243                 
244         return PY_SET_ATTR_SUCCESS;
245 }
246
247 PyObject* KX_SCA_AddObjectActuator::pyattr_get_objectLastCreated(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
248 {
249         KX_SCA_AddObjectActuator* actuator = static_cast<KX_SCA_AddObjectActuator*>(self);
250         if (!actuator->m_lastCreatedObject)
251                 Py_RETURN_NONE;
252         else
253                 return actuator->m_lastCreatedObject->GetProxy();
254 }
255
256 /* 1. setObject */
257 const char KX_SCA_AddObjectActuator::SetObject_doc[] = 
258 "setObject(object)\n"
259 "\t- object: KX_GameObject, string or None\n"
260 "\tSets the object that will be added. There has to be an object\n"
261 "\tof this name. If not, this function does nothing.\n";
262 PyObject* KX_SCA_AddObjectActuator::PySetObject(PyObject* value)
263 {
264         KX_GameObject *gameobj;
265         
266         ShowDeprecationWarning("setObject()", "the object property");
267         
268         if (!ConvertPythonToGameObject(value, &gameobj, true, "actuator.setObject(value): KX_SCA_AddObjectActuator"))
269                 return NULL; // ConvertPythonToGameObject sets the error
270         
271         if (m_OriginalObject != NULL)
272                 m_OriginalObject->UnregisterActuator(this);     
273
274         m_OriginalObject = (SCA_IObject*)gameobj;
275         if (m_OriginalObject)
276                 m_OriginalObject->RegisterActuator(this);
277         
278         Py_RETURN_NONE;
279 }
280
281
282
283 /* 2. setTime */
284 const char KX_SCA_AddObjectActuator::SetTime_doc[] = 
285 "setTime(duration)\n"
286 "\t- duration: integer\n"
287 "\tSets the lifetime of the object that will be added, in frames. \n"
288 "\tIf the duration is negative, it is set to 0.\n";
289
290
291 PyObject* KX_SCA_AddObjectActuator::PySetTime(PyObject* value)
292 {
293         ShowDeprecationWarning("setTime()", "the time property");
294         int deltatime = PyLong_AsSsize_t(value);
295         if (deltatime==-1 && PyErr_Occurred()) {
296                 PyErr_SetString(PyExc_TypeError, "expected an int");
297                 return NULL;
298         }
299         
300         m_timeProp = deltatime;
301         if (m_timeProp < 0) m_timeProp = 0;
302         
303         Py_RETURN_NONE;
304 }
305
306
307
308 /* 3. getTime */
309 const char KX_SCA_AddObjectActuator::GetTime_doc[] = 
310 "getTime()\n"
311 "\tReturns the lifetime of the object that will be added.\n";
312
313
314 PyObject* KX_SCA_AddObjectActuator::PyGetTime()
315 {
316         ShowDeprecationWarning("getTime()", "the time property");
317         return PyLong_FromSsize_t(m_timeProp);
318 }
319
320
321 /* 4. getObject */
322 const char KX_SCA_AddObjectActuator::GetObject_doc[] = 
323 "getObject(name_only = 1)\n"
324 "name_only - optional arg, when true will return the KX_GameObject rather then its name\n"
325 "\tReturns the name of the object that will be added.\n";
326 PyObject* KX_SCA_AddObjectActuator::PyGetObject(PyObject* args)
327 {
328         int ret_name_only = 1;
329         
330         ShowDeprecationWarning("getObject()", "the object property");
331         
332         if (!PyArg_ParseTuple(args, "|i:getObject", &ret_name_only))
333                 return NULL;
334         
335         if (!m_OriginalObject)
336                 Py_RETURN_NONE;
337         
338         if (ret_name_only)
339                 return PyUnicode_FromString(m_OriginalObject->GetName().ReadPtr());
340         else
341                 return m_OriginalObject->GetProxy();
342 }
343
344
345
346 /* 5. getLinearVelocity */
347 const char KX_SCA_AddObjectActuator::GetLinearVelocity_doc[] = 
348 "GetLinearVelocity()\n"
349 "\tReturns the linear velocity that will be assigned to \n"
350 "\tthe created object.\n";
351
352 PyObject* KX_SCA_AddObjectActuator::PyGetLinearVelocity()
353 {
354         ShowDeprecationWarning("getLinearVelocity()", "the linearVelocity property");
355         PyObject *retVal = PyList_New(3);
356
357         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_linear_velocity[0]));
358         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_linear_velocity[1]));
359         PyList_SET_ITEM(retVal, 2, PyFloat_FromDouble(m_linear_velocity[2]));
360         
361         return retVal;
362 }
363
364
365
366 /* 6. setLinearVelocity                                                 */
367 const char KX_SCA_AddObjectActuator::SetLinearVelocity_doc[] = 
368 "setLinearVelocity(vx, vy, vz)\n"
369 "\t- vx: float\n"
370 "\t- vy: float\n"
371 "\t- vz: float\n"
372 "\t- local: bool\n"
373 "\tAssign this velocity to the created object. \n";
374
375 PyObject* KX_SCA_AddObjectActuator::PySetLinearVelocity(PyObject* args)
376 {
377         ShowDeprecationWarning("setLinearVelocity()", "the linearVelocity property");
378         
379         float vecArg[3];
380         if (!PyArg_ParseTuple(args, "fff:setLinearVelocity", &vecArg[0], &vecArg[1], &vecArg[2]))
381                 return NULL;
382
383         m_linear_velocity[0] = vecArg[0];
384         m_linear_velocity[1] = vecArg[1];
385         m_linear_velocity[2] = vecArg[2];
386         Py_RETURN_NONE;
387 }
388
389 /* 7. getAngularVelocity */
390 const char KX_SCA_AddObjectActuator::GetAngularVelocity_doc[] = 
391 "GetAngularVelocity()\n"
392 "\tReturns the angular velocity that will be assigned to \n"
393 "\tthe created object.\n";
394
395 PyObject* KX_SCA_AddObjectActuator::PyGetAngularVelocity()
396 {
397         ShowDeprecationWarning("getAngularVelocity()", "the angularVelocity property");
398         PyObject *retVal = PyList_New(3);
399
400         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_angular_velocity[0]));
401         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_angular_velocity[1]));
402         PyList_SET_ITEM(retVal, 2, PyFloat_FromDouble(m_angular_velocity[2]));
403         
404         return retVal;
405 }
406
407
408
409 /* 8. setAngularVelocity                                                 */
410 const char KX_SCA_AddObjectActuator::SetAngularVelocity_doc[] = 
411 "setAngularVelocity(vx, vy, vz)\n"
412 "\t- vx: float\n"
413 "\t- vy: float\n"
414 "\t- vz: float\n"
415 "\t- local: bool\n"
416 "\tAssign this angular velocity to the created object. \n";
417
418 PyObject* KX_SCA_AddObjectActuator::PySetAngularVelocity(PyObject* args)
419 {
420         ShowDeprecationWarning("setAngularVelocity()", "the angularVelocity property");
421         
422         float vecArg[3];
423         if (!PyArg_ParseTuple(args, "fff:setAngularVelocity", &vecArg[0], &vecArg[1], &vecArg[2]))
424                 return NULL;
425
426         m_angular_velocity[0] = vecArg[0];
427         m_angular_velocity[1] = vecArg[1];
428         m_angular_velocity[2] = vecArg[2];
429         Py_RETURN_NONE;
430 }
431
432 void    KX_SCA_AddObjectActuator::InstantAddObject()
433 {
434         if (m_OriginalObject)
435         {
436                 // Add an identical object, with properties inherited from the original object  
437                 // Now it needs to be added to the current scene.
438                 SCA_IObject* replica = m_scene->AddReplicaObject(m_OriginalObject,GetParent(),m_timeProp );
439                 KX_GameObject * game_obj = static_cast<KX_GameObject *>(replica);
440                 game_obj->setLinearVelocity(m_linear_velocity ,m_localLinvFlag);
441                 game_obj->setAngularVelocity(m_angular_velocity,m_localAngvFlag);
442                 game_obj->ResolveCombinedVelocities(m_linear_velocity, m_angular_velocity, m_localLinvFlag, m_localAngvFlag);
443
444                 // keep a copy of the last object, to allow python scripters to change it
445                 if (m_lastCreatedObject)
446                 {
447                         //Let's not keep a reference to the object: it's bad, if the object is deleted
448                         //this will force to keep a "zombie" in the game for no good reason.
449                         //m_scene->DelayedReleaseObject(m_lastCreatedObject);
450                         //m_lastCreatedObject->Release();
451
452                         //Instead we use the registration mechanism
453                         m_lastCreatedObject->UnregisterActuator(this);
454                         m_lastCreatedObject = NULL;
455                 }
456                 
457                 m_lastCreatedObject = replica;
458                 // no reference
459                 //m_lastCreatedObject->AddRef();
460                 // but registration
461                 m_lastCreatedObject->RegisterActuator(this);
462                 // finished using replica? then release it
463                 replica->Release();
464         }
465 }
466
467 PyObject* KX_SCA_AddObjectActuator::PyInstantAddObject()
468 {
469         InstantAddObject();
470
471         Py_RETURN_NONE;
472 }
473
474
475
476 /* 7. GetLastCreatedObject                                                */
477 const char KX_SCA_AddObjectActuator::GetLastCreatedObject_doc[] = 
478 "getLastCreatedObject()\n"
479 "\tReturn the last created object. \n";
480
481
482 PyObject* KX_SCA_AddObjectActuator::PyGetLastCreatedObject()
483 {
484         ShowDeprecationWarning("getLastCreatedObject()", "the objectLastCreated property");
485         SCA_IObject* result = this->GetLastCreatedObject();
486         
487         // if result->GetSGNode() is NULL
488         // it means the object has ended, The BGE python api crashes in many places if the object is returned.
489         if (result && (static_cast<KX_GameObject *>(result))->GetSGNode()) 
490         {
491                 return result->GetProxy();
492         }
493         // don't return NULL to python anymore, it gives trouble in the scripts
494         Py_RETURN_NONE;
495 }