Clear the Python error flag between 'overloaded' Python methods.
[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/BL DUAL 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. The Blender
12 // Foundation also sells licenses for use in proprietary software under
13 // the Blender License.  See http://www.blender.org/BL/ for information
14 // about this.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software Foundation,
23 // Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24 //
25 // The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26 // All rights reserved.
27 //
28 // The Original Code is: all of this file.
29 //
30 // Contributor(s): none yet.
31 //
32 // ***** END GPL/BL DUAL LICENSE BLOCK *****
33 // Previously existed as:
34
35 // \source\gameengine\GameLogic\SCA_AddObjectActuator.cpp
36
37 // Please look here for revision history.
38
39
40 #include "KX_SCA_AddObjectActuator.h"
41 #include "SCA_IScene.h"
42 #include "KX_GameObject.h"
43 #include "KX_IPhysicsController.h"
44
45 #ifdef HAVE_CONFIG_H
46 #include <config.h>
47 #endif
48
49 /* ------------------------------------------------------------------------- */
50 /* Native functions                                                          */
51 /* ------------------------------------------------------------------------- */
52
53 KX_SCA_AddObjectActuator::KX_SCA_AddObjectActuator(SCA_IObject *gameobj,
54                                                                                                    CValue* original,
55                                                                                                    int time,
56                                                                                                    SCA_IScene* scene,
57                                                                                                    const MT_Vector3& linvel,
58                                                                                                    bool local,
59                                                                                                    PyTypeObject* T)
60         : 
61         SCA_IActuator(gameobj, T),
62         m_OriginalObject(original),
63         m_scene(scene),
64         m_linear_velocity(linvel),
65         m_localFlag(local)
66 {
67         m_lastCreatedObject = NULL;
68         m_timeProp = time;
69
70
71
72
73 KX_SCA_AddObjectActuator::~KX_SCA_AddObjectActuator()
74
75         if (m_lastCreatedObject)
76                 m_lastCreatedObject->Release();
77
78
79
80
81 bool KX_SCA_AddObjectActuator::Update(double curtime,
82                                                                           double deltatime)
83 {
84         bool result = false;    
85         bool bNegativeEvent = IsNegativeEvent();
86         RemoveAllEvents();
87         
88         if (bNegativeEvent) return false; // do nothing on negative events
89         if (m_OriginalObject)
90         {
91                 // Add an identical object, with properties inherited from the original object  
92                 // Now it needs to be added to the current scene.
93                 SCA_IObject* replica = m_scene->AddReplicaObject(m_OriginalObject,GetParent(),m_timeProp );
94                 KX_GameObject * game_obj = static_cast<KX_GameObject *>(replica);
95                 game_obj->setLinearVelocity(m_linear_velocity,m_localFlag);
96                 game_obj->ResolveCombinedVelocities(m_linear_velocity, MT_Vector3(0., 0., 0.), m_localFlag, false);
97
98                 // keep a copy of the last object, to allow python scripters to change it
99                 if (m_lastCreatedObject)
100                         m_lastCreatedObject->Release();
101                 
102                 m_lastCreatedObject = replica;
103                 m_lastCreatedObject->AddRef();
104         }
105
106         return false;
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         replica->m_lastCreatedObject=NULL;
128         CValue::AddDataToReplica(replica);
129
130         return replica;
131 }
132
133
134
135 /* ------------------------------------------------------------------------- */
136 /* Python functions                                                          */
137 /* ------------------------------------------------------------------------- */
138
139 /* Integration hooks ------------------------------------------------------- */
140 PyTypeObject KX_SCA_AddObjectActuator::Type = {
141         PyObject_HEAD_INIT(&PyType_Type)
142         0,
143         "KX_SCA_AddObjectActuator",
144         sizeof(KX_SCA_AddObjectActuator),
145         0,
146         PyDestructor,
147         0,
148         __getattr,
149         __setattr,
150         0, 
151         __repr,
152         0,
153         0,
154         0,
155         0,
156         0
157 };
158
159 PyParentObject KX_SCA_AddObjectActuator::Parents[] = {
160         &SCA_IActuator::Type,
161         &SCA_ILogicBrick::Type,
162         &CValue::Type,
163         NULL
164 };
165 PyMethodDef KX_SCA_AddObjectActuator::Methods[] = {
166   {"setObject", (PyCFunction) KX_SCA_AddObjectActuator::sPySetObject, METH_VARARGS, SetObject_doc},
167   {"setTime", (PyCFunction) KX_SCA_AddObjectActuator::sPySetTime, METH_VARARGS, SetTime_doc},
168   {"getObject", (PyCFunction) KX_SCA_AddObjectActuator::sPyGetObject, METH_VARARGS, GetObject_doc},
169   {"getTime", (PyCFunction) KX_SCA_AddObjectActuator::sPyGetTime, METH_VARARGS, GetTime_doc},
170   {"getLinearVelocity", (PyCFunction) KX_SCA_AddObjectActuator::sPyGetLinearVelocity, METH_VARARGS, GetLinearVelocity_doc},
171   {"setLinearVelocity", (PyCFunction) KX_SCA_AddObjectActuator::sPySetLinearVelocity, METH_VARARGS, SetLinearVelocity_doc},
172   {"getLastCreatedObject", (PyCFunction) KX_SCA_AddObjectActuator::sPyGetLastCreatedObject, METH_VARARGS,"getLastCreatedObject() : get the object handle to the last created object\n"},
173   {NULL,NULL} //Sentinel
174 };
175
176
177 PyObject* KX_SCA_AddObjectActuator::_getattr(const STR_String& attr)
178 {
179   _getattr_up(SCA_IActuator);
180 }
181
182 /* 1. setObject */
183 char KX_SCA_AddObjectActuator::SetObject_doc[] = 
184 "setObject(name)\n"
185 "\t- name: string\n"
186 "\tSets the object that will be added. There has to be an object\n"
187 "\tof this name. If not, this function does nothing.\n";
188
189
190
191 PyObject* KX_SCA_AddObjectActuator::PySetObject(PyObject* self,
192                                                                                                 PyObject* args,
193                                                                                                 PyObject* kwds)
194 {    
195         PyObject* gameobj;
196         if (PyArg_ParseTuple(args, "O!", &KX_GameObject::Type, &gameobj))
197         {
198                 m_OriginalObject = (CValue*)gameobj;
199                 Py_Return;
200         }
201         PyErr_Clear();
202         
203         char* objectname;
204         if (PyArg_ParseTuple(args, "s", &objectname))
205         {
206                 m_OriginalObject= (CValue*)SCA_ILogicBrick::m_sCurrentLogicManager->GetGameObjectByName(STR_String(objectname));;
207                 
208                 Py_Return;
209         }
210         
211         return NULL;
212 }
213
214
215
216 /* 2. setTime */
217 char KX_SCA_AddObjectActuator::SetTime_doc[] = 
218 "setTime(duration)\n"
219 "\t- duration: integer\n"
220 "\tSets the lifetime of the object that will be added, in frames. \n"
221 "\tIf the duration is negative, it is set to 0.\n";
222
223
224 PyObject* KX_SCA_AddObjectActuator::PySetTime(PyObject* self,
225                                                                                           PyObject* args,
226                                                                                           PyObject* kwds)
227 {
228         int deltatime;
229         
230         if (!PyArg_ParseTuple(args, "i", &deltatime))
231                 return NULL;
232         
233         m_timeProp = deltatime;
234         if (m_timeProp < 0) m_timeProp = 0;
235         
236         Py_Return;
237 }
238
239
240
241 /* 3. getTime */
242 char KX_SCA_AddObjectActuator::GetTime_doc[] = 
243 "GetTime()\n"
244 "\tReturns the lifetime of the object that will be added.\n";
245
246
247 PyObject* KX_SCA_AddObjectActuator::PyGetTime(PyObject* self,
248                                                                                           PyObject* args,
249                                                                                           PyObject* kwds)
250 {
251         return PyInt_FromLong(m_timeProp);
252 }
253
254
255 /* 4. getObject */
256 char KX_SCA_AddObjectActuator::GetObject_doc[] = 
257 "getObject()\n"
258 "\tReturns the name of the object that will be added.\n";
259
260
261         
262 PyObject* KX_SCA_AddObjectActuator::PyGetObject(PyObject* self,
263                                                                                                 PyObject* args,
264                                                                                                 PyObject* kwds)
265 {
266         return PyString_FromString(m_OriginalObject->GetName());
267 }
268
269
270
271 /* 5. getLinearVelocity */
272 char KX_SCA_AddObjectActuator::GetLinearVelocity_doc[] = 
273 "GetLinearVelocity()\n"
274 "\tReturns the linear velocity that will be assigned to \n"
275 "\tthe created object.\n";
276
277
278
279 PyObject* KX_SCA_AddObjectActuator::PyGetLinearVelocity(PyObject* self,
280                                                                                                                 PyObject* args,
281                                                                                                                 PyObject* kwds)
282 {
283         PyObject *retVal = PyList_New(3);
284
285         PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_linear_velocity[0]));
286         PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_linear_velocity[1]));
287         PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_linear_velocity[2]));
288         
289         return retVal;
290 }
291
292
293
294 /* 6. setLinearVelocity                                                 */
295 char KX_SCA_AddObjectActuator::SetLinearVelocity_doc[] = 
296 "setLinearVelocity(vx, vy, vz)\n"
297 "\t- vx: float\n"
298 "\t- vy: float\n"
299 "\t- vz: float\n"
300 "\tAssign this velocity to the created object. \n";
301
302
303 PyObject* KX_SCA_AddObjectActuator::PySetLinearVelocity(PyObject* self,
304                                                                                                                 PyObject* args,
305                                                                                                                 PyObject* kwds)
306 {
307         
308         float vecArg[3];
309         if (!PyArg_ParseTuple(args, "fff", &vecArg[0], &vecArg[1], &vecArg[2]))
310                 return NULL;
311
312         m_linear_velocity.setValue(vecArg);
313         Py_Return;
314 }
315
316
317
318 /* 7. GetLastCreatedObject                                                */
319 char KX_SCA_AddObjectActuator::GetLastCreatedObject_doc[] = 
320 "getLastCreatedObject()\n"
321 "\tReturn the last created object. \n";
322
323
324 PyObject* KX_SCA_AddObjectActuator::PyGetLastCreatedObject(PyObject* self,
325                                                                                                                    PyObject* args,
326                                                                                                                    PyObject* kwds)
327 {
328         SCA_IObject* result = this->GetLastCreatedObject();
329         if (result)
330         {
331                 result->AddRef();
332                 return result;
333         }
334         // don't return NULL to python anymore, it gives trouble in the scripts
335         Py_Return;
336 }