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