76ce086ab97451917d324fd37e48b16bf886da5d
[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
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45
46 /* ------------------------------------------------------------------------- */
47 /* Native functions                                                          */
48 /* ------------------------------------------------------------------------- */
49
50 KX_SCA_AddObjectActuator::KX_SCA_AddObjectActuator(SCA_IObject *gameobj,
51                                                                                                    CValue* original,
52                                                                                                    int time,
53                                                                                                    SCA_IScene* scene,
54                                                                                                    const MT_Vector3& linvel,
55                                                                                                    bool local,
56                                                                                                    PyTypeObject* T)
57         : 
58         SCA_IActuator(gameobj, T),
59         m_OriginalObject(original),
60         m_scene(scene),
61         m_linear_velocity(linvel),
62         m_localFlag(local)
63 {
64         m_lastCreatedObject = NULL;
65         m_timeProp = time;
66
67
68
69
70 KX_SCA_AddObjectActuator::~KX_SCA_AddObjectActuator()
71
72         if (m_lastCreatedObject)
73                 m_lastCreatedObject->Release();
74
75
76
77
78 bool KX_SCA_AddObjectActuator::Update()
79 {
80         //bool result = false;  /*unused*/
81         bool bNegativeEvent = IsNegativeEvent();
82         RemoveAllEvents();
83         
84         if (bNegativeEvent) return false; // do nothing on negative events
85
86         InstantAddObject();
87
88         
89         return false;
90 }
91
92
93
94
95 SCA_IObject* KX_SCA_AddObjectActuator::GetLastCreatedObject() const 
96 {
97         return m_lastCreatedObject;
98 }
99
100
101
102 CValue* KX_SCA_AddObjectActuator::GetReplica() 
103 {
104         KX_SCA_AddObjectActuator* replica = new KX_SCA_AddObjectActuator(*this);
105
106         if (replica == NULL)
107                 return NULL;
108
109         // this will copy properties and so on...
110         replica->ProcessReplica();
111         replica->m_lastCreatedObject=NULL;
112         CValue::AddDataToReplica(replica);
113
114         return replica;
115 }
116
117
118
119 /* ------------------------------------------------------------------------- */
120 /* Python functions                                                          */
121 /* ------------------------------------------------------------------------- */
122
123 /* Integration hooks ------------------------------------------------------- */
124 PyTypeObject KX_SCA_AddObjectActuator::Type = {
125         PyObject_HEAD_INIT(&PyType_Type)
126         0,
127         "KX_SCA_AddObjectActuator",
128         sizeof(KX_SCA_AddObjectActuator),
129         0,
130         PyDestructor,
131         0,
132         __getattr,
133         __setattr,
134         0, 
135         __repr,
136         0,
137         0,
138         0,
139         0,
140         0
141 };
142
143 PyParentObject KX_SCA_AddObjectActuator::Parents[] = {
144         &SCA_IActuator::Type,
145         &SCA_ILogicBrick::Type,
146         &CValue::Type,
147         NULL
148 };
149 PyMethodDef KX_SCA_AddObjectActuator::Methods[] = {
150   {"setObject", (PyCFunction) KX_SCA_AddObjectActuator::sPySetObject, METH_VARARGS, SetObject_doc},
151   {"setTime", (PyCFunction) KX_SCA_AddObjectActuator::sPySetTime, METH_VARARGS, SetTime_doc},
152   {"getObject", (PyCFunction) KX_SCA_AddObjectActuator::sPyGetObject, METH_VARARGS, GetObject_doc},
153   {"getTime", (PyCFunction) KX_SCA_AddObjectActuator::sPyGetTime, METH_VARARGS, GetTime_doc},
154   {"getLinearVelocity", (PyCFunction) KX_SCA_AddObjectActuator::sPyGetLinearVelocity, METH_VARARGS, GetLinearVelocity_doc},
155   {"setLinearVelocity", (PyCFunction) KX_SCA_AddObjectActuator::sPySetLinearVelocity, METH_VARARGS, SetLinearVelocity_doc},
156   {"getLastCreatedObject", (PyCFunction) KX_SCA_AddObjectActuator::sPyGetLastCreatedObject, METH_VARARGS,"getLastCreatedObject() : get the object handle to the last created object\n"},
157   {"instantAddObject", (PyCFunction) KX_SCA_AddObjectActuator::sPyInstantAddObject, METH_VARARGS,"instantAddObject() : immediately add object without delay\n"},
158   
159   {NULL,NULL} //Sentinel
160 };
161
162
163 PyObject* KX_SCA_AddObjectActuator::_getattr(const STR_String& attr)
164 {
165   _getattr_up(SCA_IActuator);
166 }
167
168 /* 1. setObject */
169 char KX_SCA_AddObjectActuator::SetObject_doc[] = 
170 "setObject(name)\n"
171 "\t- name: string\n"
172 "\tSets the object that will be added. There has to be an object\n"
173 "\tof this name. If not, this function does nothing.\n";
174
175
176
177 PyObject* KX_SCA_AddObjectActuator::PySetObject(PyObject* self,
178                                                                                                 PyObject* args,
179                                                                                                 PyObject* kwds)
180 {    
181         PyObject* gameobj;
182         if (PyArg_ParseTuple(args, "O!", &KX_GameObject::Type, &gameobj))
183         {
184                 m_OriginalObject = (CValue*)gameobj;
185                 Py_Return;
186         }
187         PyErr_Clear();
188         
189         char* objectname;
190         if (PyArg_ParseTuple(args, "s", &objectname))
191         {
192                 m_OriginalObject= (CValue*)SCA_ILogicBrick::m_sCurrentLogicManager->GetGameObjectByName(STR_String(objectname));;
193                 
194                 Py_Return;
195         }
196         
197         return NULL;
198 }
199
200
201
202 /* 2. setTime */
203 char KX_SCA_AddObjectActuator::SetTime_doc[] = 
204 "setTime(duration)\n"
205 "\t- duration: integer\n"
206 "\tSets the lifetime of the object that will be added, in frames. \n"
207 "\tIf the duration is negative, it is set to 0.\n";
208
209
210 PyObject* KX_SCA_AddObjectActuator::PySetTime(PyObject* self,
211                                                                                           PyObject* args,
212                                                                                           PyObject* kwds)
213 {
214         int deltatime;
215         
216         if (!PyArg_ParseTuple(args, "i", &deltatime))
217                 return NULL;
218         
219         m_timeProp = deltatime;
220         if (m_timeProp < 0) m_timeProp = 0;
221         
222         Py_Return;
223 }
224
225
226
227 /* 3. getTime */
228 char KX_SCA_AddObjectActuator::GetTime_doc[] = 
229 "GetTime()\n"
230 "\tReturns the lifetime of the object that will be added.\n";
231
232
233 PyObject* KX_SCA_AddObjectActuator::PyGetTime(PyObject* self,
234                                                                                           PyObject* args,
235                                                                                           PyObject* kwds)
236 {
237         return PyInt_FromLong(m_timeProp);
238 }
239
240
241 /* 4. getObject */
242 char KX_SCA_AddObjectActuator::GetObject_doc[] = 
243 "getObject()\n"
244 "\tReturns the name of the object that will be added.\n";
245
246
247         
248 PyObject* KX_SCA_AddObjectActuator::PyGetObject(PyObject* self,
249                                                                                                 PyObject* args,
250                                                                                                 PyObject* kwds)
251 {
252         if (!m_OriginalObject)
253                 Py_Return;
254
255         return PyString_FromString(m_OriginalObject->GetName());
256 }
257
258
259
260 /* 5. getLinearVelocity */
261 char KX_SCA_AddObjectActuator::GetLinearVelocity_doc[] = 
262 "GetLinearVelocity()\n"
263 "\tReturns the linear velocity that will be assigned to \n"
264 "\tthe created object.\n";
265
266
267
268 PyObject* KX_SCA_AddObjectActuator::PyGetLinearVelocity(PyObject* self,
269                                                                                                                 PyObject* args,
270                                                                                                                 PyObject* kwds)
271 {
272         PyObject *retVal = PyList_New(3);
273
274         PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_linear_velocity[0]));
275         PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_linear_velocity[1]));
276         PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_linear_velocity[2]));
277         
278         return retVal;
279 }
280
281
282
283 /* 6. setLinearVelocity                                                 */
284 char KX_SCA_AddObjectActuator::SetLinearVelocity_doc[] = 
285 "setLinearVelocity(vx, vy, vz)\n"
286 "\t- vx: float\n"
287 "\t- vy: float\n"
288 "\t- vz: float\n"
289 "\tAssign this velocity to the created object. \n";
290
291
292 PyObject* KX_SCA_AddObjectActuator::PySetLinearVelocity(PyObject* self,
293                                                                                                                 PyObject* args,
294                                                                                                                 PyObject* kwds)
295 {
296         
297         float vecArg[3];
298         if (!PyArg_ParseTuple(args, "fff", &vecArg[0], &vecArg[1], &vecArg[2]))
299                 return NULL;
300
301         m_linear_velocity.setValue(vecArg);
302         Py_Return;
303 }
304
305 void    KX_SCA_AddObjectActuator::InstantAddObject()
306 {
307         if (m_OriginalObject)
308         {
309                 // Add an identical object, with properties inherited from the original object  
310                 // Now it needs to be added to the current scene.
311                 SCA_IObject* replica = m_scene->AddReplicaObject(m_OriginalObject,GetParent(),m_timeProp );
312                 KX_GameObject * game_obj = static_cast<KX_GameObject *>(replica);
313                 game_obj->setLinearVelocity(m_linear_velocity,m_localFlag);
314                 game_obj->ResolveCombinedVelocities(m_linear_velocity, MT_Vector3(0., 0., 0.), m_localFlag, false);
315
316                 // keep a copy of the last object, to allow python scripters to change it
317                 if (m_lastCreatedObject)
318                 {
319                         //careful with destruction, it might still have outstanding collision callbacks
320                         m_scene->DelayedReleaseObject(m_lastCreatedObject);
321                         m_lastCreatedObject->Release();
322                 }
323                 
324                 m_lastCreatedObject = replica;
325                 m_lastCreatedObject->AddRef();
326                 // finished using replica? then release it
327                 replica->Release();
328         }
329 }
330
331 PyObject* KX_SCA_AddObjectActuator::PyInstantAddObject(PyObject* self,
332                                                                                                                    PyObject* args,
333                                                                                                                    PyObject* kwds)
334 {
335         InstantAddObject();
336
337         Py_Return;
338 }
339
340
341
342 /* 7. GetLastCreatedObject                                                */
343 char KX_SCA_AddObjectActuator::GetLastCreatedObject_doc[] = 
344 "getLastCreatedObject()\n"
345 "\tReturn the last created object. \n";
346
347
348 PyObject* KX_SCA_AddObjectActuator::PyGetLastCreatedObject(PyObject* self,
349                                                                                                                    PyObject* args,
350                                                                                                                    PyObject* kwds)
351 {
352         SCA_IObject* result = this->GetLastCreatedObject();
353         if (result)
354         {
355                 result->AddRef();
356                 return result;
357         }
358         // don't return NULL to python anymore, it gives trouble in the scripts
359         Py_Return;
360 }