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