add missing files after merging
[blender.git] / source / gameengine / Ketsji / KX_SCA_AddObjectActuator.cpp
1 /** \file gameengine/Ketsji/KX_SCA_AddObjectActuator.cpp
2  *  \ingroup ketsji
3  */
4 //
5 // Add an object when this actuator is triggered
6 //
7 // $Id$
8 //
9 // ***** BEGIN GPL LICENSE BLOCK *****
10 //
11 // This program is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License
13 // as published by the Free Software Foundation; either version 2
14 // of the License, or (at your option) any later version.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 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 #include "PyObjectPlus.h" 
45
46 /* ------------------------------------------------------------------------- */
47 /* Native functions                                                          */
48 /* ------------------------------------------------------------------------- */
49
50 KX_SCA_AddObjectActuator::KX_SCA_AddObjectActuator(SCA_IObject *gameobj,
51                                                                                                    SCA_IObject *original,
52                                                                                                    int time,
53                                                                                                    SCA_IScene* scene,
54                                                                                                    const float *linvel,
55                                                                                                    bool linv_local,
56                                                                                                    const float *angvel,
57                                                                                                    bool angv_local)
58         : 
59         SCA_IActuator(gameobj, KX_ACT_ADD_OBJECT),
60         m_OriginalObject(original),
61         m_scene(scene),
62         
63         m_localLinvFlag(linv_local),
64         m_localAngvFlag(angv_local)
65 {
66         m_linear_velocity[0] = linvel[0];
67         m_linear_velocity[1] = linvel[1];
68         m_linear_velocity[2] = linvel[2];
69         m_angular_velocity[0] = angvel[0];
70         m_angular_velocity[1] = angvel[1];
71         m_angular_velocity[2] = angvel[2];
72
73         if (m_OriginalObject)
74                 m_OriginalObject->RegisterActuator(this);
75
76         m_lastCreatedObject = NULL;
77         m_timeProp = time;
78
79
80
81
82 KX_SCA_AddObjectActuator::~KX_SCA_AddObjectActuator()
83
84         if (m_OriginalObject)
85                 m_OriginalObject->UnregisterActuator(this);
86         if (m_lastCreatedObject)
87                 m_lastCreatedObject->UnregisterActuator(this);
88
89
90
91
92 bool KX_SCA_AddObjectActuator::Update()
93 {
94         //bool result = false;  /*unused*/
95         bool bNegativeEvent = IsNegativeEvent();
96         RemoveAllEvents();
97         
98         if (bNegativeEvent) return false; // do nothing on negative events
99
100         InstantAddObject();
101
102         
103         return false;
104 }
105
106
107
108
109 SCA_IObject* KX_SCA_AddObjectActuator::GetLastCreatedObject() const 
110 {
111         return m_lastCreatedObject;
112 }
113
114
115
116 CValue* KX_SCA_AddObjectActuator::GetReplica() 
117 {
118         KX_SCA_AddObjectActuator* replica = new KX_SCA_AddObjectActuator(*this);
119
120         if (replica == NULL)
121                 return NULL;
122
123         // this will copy properties and so on...
124         replica->ProcessReplica();
125
126         return replica;
127 }
128
129 void KX_SCA_AddObjectActuator::ProcessReplica()
130 {
131         if (m_OriginalObject)
132                 m_OriginalObject->RegisterActuator(this);
133         m_lastCreatedObject=NULL;
134         SCA_IActuator::ProcessReplica();
135 }
136
137 bool KX_SCA_AddObjectActuator::UnlinkObject(SCA_IObject* clientobj)
138 {
139         if (clientobj == m_OriginalObject)
140         {
141                 // this object is being deleted, we cannot continue to track it.
142                 m_OriginalObject = NULL;
143                 return true;
144         }
145         if (clientobj == m_lastCreatedObject)
146         {
147                 // this object is being deleted, we cannot continue to track it.
148                 m_lastCreatedObject = NULL;
149                 return true;
150         }
151         return false;
152 }
153
154 void KX_SCA_AddObjectActuator::Relink(CTR_Map<CTR_HashedPtr, void*> *obj_map)
155 {
156         void **h_obj = (*obj_map)[m_OriginalObject];
157         if (h_obj) {
158                 if (m_OriginalObject)
159                         m_OriginalObject->UnregisterActuator(this);
160                 m_OriginalObject = (SCA_IObject*)(*h_obj);
161                 m_OriginalObject->RegisterActuator(this);
162         }
163 }
164
165 #ifdef WITH_PYTHON
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   {"instantAddObject", (PyCFunction) KX_SCA_AddObjectActuator::sPyInstantAddObject, METH_NOARGS,"instantAddObject() : immediately add object without delay\n"},
196   {NULL,NULL} //Sentinel
197 };
198
199 PyAttributeDef KX_SCA_AddObjectActuator::Attributes[] = {
200         KX_PYATTRIBUTE_RW_FUNCTION("object",KX_SCA_AddObjectActuator,pyattr_get_object,pyattr_set_object),
201         KX_PYATTRIBUTE_RO_FUNCTION("objectLastCreated",KX_SCA_AddObjectActuator,pyattr_get_objectLastCreated),
202         KX_PYATTRIBUTE_INT_RW("time",0,2000,true,KX_SCA_AddObjectActuator,m_timeProp),
203         KX_PYATTRIBUTE_FLOAT_ARRAY_RW("linearVelocity",-FLT_MAX,FLT_MAX,KX_SCA_AddObjectActuator,m_linear_velocity,3),
204         KX_PYATTRIBUTE_FLOAT_ARRAY_RW("angularVelocity",-FLT_MAX,FLT_MAX,KX_SCA_AddObjectActuator,m_angular_velocity,3),
205         { NULL }        //Sentinel
206 };
207
208 PyObject* KX_SCA_AddObjectActuator::pyattr_get_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
209 {
210         KX_SCA_AddObjectActuator* actuator = static_cast<KX_SCA_AddObjectActuator*>(self);
211         if (!actuator->m_OriginalObject)        
212                 Py_RETURN_NONE;
213         else
214                 return actuator->m_OriginalObject->GetProxy();
215 }
216
217 int KX_SCA_AddObjectActuator::pyattr_set_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
218 {
219         KX_SCA_AddObjectActuator* actuator = static_cast<KX_SCA_AddObjectActuator*>(self);
220         KX_GameObject *gameobj;
221                 
222         if (!ConvertPythonToGameObject(value, &gameobj, true, "actuator.object = value: KX_SCA_AddObjectActuator"))
223                 return PY_SET_ATTR_FAIL; // ConvertPythonToGameObject sets the error
224                 
225         if (actuator->m_OriginalObject != NULL)
226                 actuator->m_OriginalObject->UnregisterActuator(actuator);       
227
228         actuator->m_OriginalObject = (SCA_IObject*)gameobj;
229                 
230         if (actuator->m_OriginalObject)
231                 actuator->m_OriginalObject->RegisterActuator(actuator);
232                 
233         return PY_SET_ATTR_SUCCESS;
234 }
235
236 PyObject* KX_SCA_AddObjectActuator::pyattr_get_objectLastCreated(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
237 {
238         KX_SCA_AddObjectActuator* actuator = static_cast<KX_SCA_AddObjectActuator*>(self);
239         if (!actuator->m_lastCreatedObject)
240                 Py_RETURN_NONE;
241         else
242                 return actuator->m_lastCreatedObject->GetProxy();
243 }
244
245 PyObject* KX_SCA_AddObjectActuator::PyInstantAddObject()
246 {
247         InstantAddObject();
248
249         Py_RETURN_NONE;
250 }
251
252 #endif // WITH_PYTHON
253
254 void    KX_SCA_AddObjectActuator::InstantAddObject()
255 {
256         if (m_OriginalObject)
257         {
258                 // Add an identical object, with properties inherited from the original object  
259                 // Now it needs to be added to the current scene.
260                 SCA_IObject* replica = m_scene->AddReplicaObject(m_OriginalObject,GetParent(),m_timeProp );
261                 KX_GameObject * game_obj = static_cast<KX_GameObject *>(replica);
262                 game_obj->setLinearVelocity(m_linear_velocity ,m_localLinvFlag);
263                 game_obj->setAngularVelocity(m_angular_velocity,m_localAngvFlag);
264                 game_obj->ResolveCombinedVelocities(m_linear_velocity, m_angular_velocity, m_localLinvFlag, m_localAngvFlag);
265
266                 // keep a copy of the last object, to allow python scripters to change it
267                 if (m_lastCreatedObject)
268                 {
269                         //Let's not keep a reference to the object: it's bad, if the object is deleted
270                         //this will force to keep a "zombie" in the game for no good reason.
271                         //m_scene->DelayedReleaseObject(m_lastCreatedObject);
272                         //m_lastCreatedObject->Release();
273
274                         //Instead we use the registration mechanism
275                         m_lastCreatedObject->UnregisterActuator(this);
276                         m_lastCreatedObject = NULL;
277                 }
278                 
279                 m_lastCreatedObject = replica;
280                 // no reference
281                 //m_lastCreatedObject->AddRef();
282                 // but registration
283                 m_lastCreatedObject->RegisterActuator(this);
284                 // finished using replica? then release it
285                 replica->Release();
286         }
287 }