resolved conflict state with HEAD r14096
[blender.git] / source / gameengine / GameLogic / SCA_IObject.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
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/BL DUAL LICENSE BLOCK *****
30  */
31 #include <iostream>
32
33 #include "SCA_IObject.h"
34 #include "SCA_ISensor.h"
35 #include "SCA_IController.h"
36 #include "SCA_IActuator.h"
37 #include "MT_Point3.h"
38 #include "ListValue.h"
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 MT_Point3 SCA_IObject::m_sDummy=MT_Point3(0,0,0);
45
46 SCA_IObject::SCA_IObject(PyTypeObject* T): CValue(T)
47 {
48         m_suspended = false;
49 }
50         
51
52
53 SCA_IObject::~SCA_IObject()
54 {
55         SCA_SensorList::iterator its;
56         for (its = m_sensors.begin(); !(its == m_sensors.end()); ++its)
57         {
58                 //Use Delete for sensor to ensure proper cleaning
59                 (*its)->Delete();
60                 //((CValue*)(*its))->Release();
61         }
62         SCA_ControllerList::iterator itc; 
63         for (itc = m_controllers.begin(); !(itc == m_controllers.end()); ++itc)
64         {
65                 ((CValue*)(*itc))->Release();
66         }
67         SCA_ActuatorList::iterator ita;
68         for (ita = m_actuators.begin(); !(ita==m_actuators.end()); ++ita)
69         {
70                 ((CValue*)(*ita))->Release();
71         }
72
73         //T_InterpolatorList::iterator i;
74         //for (i = m_interpolators.begin(); !(i == m_interpolators.end()); ++i) {
75         //      delete *i;
76         //}
77 }
78
79
80
81 SCA_ControllerList& SCA_IObject::GetControllers()
82 {
83         return m_controllers;
84 }
85
86
87
88 SCA_SensorList& SCA_IObject::GetSensors()
89 {
90         return m_sensors;
91 }
92
93
94
95 SCA_ActuatorList& SCA_IObject::GetActuators()
96 {
97         return m_actuators;
98 }
99
100
101
102 void SCA_IObject::AddSensor(SCA_ISensor* act)
103 {
104         act->AddRef();
105         m_sensors.push_back(act);
106 }
107
108
109
110 void SCA_IObject::AddController(SCA_IController* act)
111 {
112         act->AddRef();
113         m_controllers.push_back(act);
114 }
115
116
117
118 void SCA_IObject::AddActuator(SCA_IActuator* act)
119 {
120         act->AddRef();
121         m_actuators.push_back(act);
122 }
123
124
125
126 void SCA_IObject::SetIgnoreActivityCulling(bool b)
127 {
128         m_ignore_activity_culling = b;
129 }
130
131
132
133 bool SCA_IObject::GetIgnoreActivityCulling()
134 {
135         return m_ignore_activity_culling;
136 }
137
138
139
140 void SCA_IObject::ReParentLogic()
141 {
142         SCA_SensorList& oldsensors = GetSensors();
143         
144         int sen = 0;
145         SCA_SensorList::iterator its;
146         for (its = oldsensors.begin(); !(its==oldsensors.end()); ++its)
147         {
148                 SCA_ISensor* newsensor = (SCA_ISensor*)(*its)->GetReplica();
149                 newsensor->ReParent(this);
150                 oldsensors[sen++] = newsensor;
151         }
152
153         SCA_ControllerList& oldcontrollers = GetControllers();
154         int con = 0;
155         SCA_ControllerList::iterator itc;
156         for (itc = oldcontrollers.begin(); !(itc==oldcontrollers.end()); ++itc)
157         {
158                 SCA_IController* newcontroller = (SCA_IController*)(*itc)->GetReplica();
159                 newcontroller->ReParent(this);
160                 oldcontrollers[con++]=newcontroller;
161
162         }
163         SCA_ActuatorList& oldactuators  = GetActuators();
164         
165         int act = 0;
166         SCA_ActuatorList::iterator ita;
167         for (ita = oldactuators.begin(); !(ita==oldactuators.end()); ++ita)
168         {
169                 SCA_IActuator* newactuator = (SCA_IActuator*) (*ita)->GetReplica();
170                 newactuator->ReParent(this);
171                 newactuator->SetActive(false);
172                 oldactuators[act++] = newactuator;
173         }
174                 
175 }
176
177
178
179 SCA_ISensor* SCA_IObject::FindSensor(const STR_String& sensorname)
180 {
181         SCA_ISensor* foundsensor = NULL;
182
183         for (SCA_SensorList::iterator its = m_sensors.begin();!(its==m_sensors.end());its++)
184         {
185                 if ((*its)->GetName() == sensorname)
186                 {
187                         foundsensor = (*its);
188                         break;
189                 }
190         }
191         return foundsensor;
192 }
193
194
195
196 SCA_IController* SCA_IObject::FindController(const STR_String& controllername)
197 {
198         SCA_IController* foundcontroller = NULL;
199
200         for (SCA_ControllerList::iterator itc = m_controllers.begin();!(itc==m_controllers.end());itc++)
201         {
202                 if ((*itc)->GetName() == controllername)
203                 {
204                         foundcontroller = (*itc);
205                         break;
206                 }       
207         }
208         return foundcontroller;
209 }
210
211
212
213 SCA_IActuator* SCA_IObject::FindActuator(const STR_String& actuatorname)
214 {
215         SCA_IActuator* foundactuator = NULL;
216
217         for (SCA_ActuatorList::iterator ita = m_actuators.begin();!(ita==m_actuators.end());ita++)
218         {
219                 if ((*ita)->GetName() == actuatorname)
220                 {
221                         foundactuator = (*ita);
222                         break;
223                 }
224         }
225
226         return foundactuator;
227 }
228
229
230
231 void SCA_IObject::SetCurrentTime(float currentTime) {
232         //T_InterpolatorList::iterator i;
233         //for (i = m_interpolators.begin(); !(i == m_interpolators.end()); ++i) {
234         //      (*i)->Execute(currentTime);
235         //}
236 }
237         
238
239 #if 0
240 const MT_Point3& SCA_IObject::ConvertPythonPylist(PyObject* pylist)
241 {
242         bool error = false;
243         m_sDummy = MT_Vector3(0,0,0);
244         if (pylist->ob_type == &CListValue::Type)
245         {
246                 CListValue* listval = (CListValue*) pylist;
247                 int numelem = listval->GetCount();
248                 if ( numelem <= 3)
249                 {
250                         int index;
251                         for (index = 0;index<numelem;index++)
252                         {
253                                 m_sDummy[index] = listval->GetValue(index)->GetNumber();
254                         }
255                 }       else
256                 {
257                         error = true;
258                 }
259                 
260         } else
261         {
262                 
263                 // assert the list is long enough...
264                 int numitems = PyList_Size(pylist);
265                 if (numitems <= 3)
266                 {
267                         int index;
268                         for (index=0;index<numitems;index++)
269                         {
270                                 m_sDummy[index] = PyFloat_AsDouble(PyList_GetItem(pylist,index));
271                         }
272                 }
273                 else
274                 {
275                         error = true;
276                 }
277
278         }
279         return m_sDummy;
280 }
281 #endif
282
283 void SCA_IObject::Suspend(void)
284 {
285         if ((!m_ignore_activity_culling) 
286                 && (!m_suspended))  {
287                 m_suspended = true;
288                 /* flag suspend for all sensors */
289                 SCA_SensorList::iterator i = m_sensors.begin();
290                 while (i != m_sensors.end()) {
291                         (*i)->Suspend();
292                         i++;
293                 }
294         }
295 }
296
297
298
299 void SCA_IObject::Resume(void)
300 {
301         if (m_suspended) {
302                 m_suspended = false;
303                 /* unflag suspend for all sensors */
304                 SCA_SensorList::iterator i = m_sensors.begin();
305                 while (i != m_sensors.end()) {
306                         (*i)->Resume();
307                         i++;
308                 }
309         }
310 }
311
312
313
314 /* ------------------------------------------------------------------------- */
315 /* Python functions                                                          */
316 /* ------------------------------------------------------------------------- */
317
318 /* Integration hooks ------------------------------------------------------- */
319 PyTypeObject SCA_IObject::Type = {
320         PyObject_HEAD_INIT(&PyType_Type)
321         0,
322         "SCA_IObject",
323         sizeof(SCA_IObject),
324         0,
325         PyDestructor,
326         0,
327         __getattr,
328         __setattr,
329         0, //&MyPyCompare,
330         __repr,
331         0, //&cvalue_as_number,
332         0,
333         0,
334         0,
335         0
336 };
337
338
339
340 PyParentObject SCA_IObject::Parents[] = {
341         &SCA_IObject::Type,
342         &CValue::Type,
343         NULL
344 };
345
346
347
348 PyMethodDef SCA_IObject::Methods[] = {
349         //{"setOrientation", (PyCFunction) SCA_IObject::sPySetOrientation, METH_VARARGS},
350         //{"getOrientation", (PyCFunction) SCA_IObject::sPyGetOrientation, METH_VARARGS},
351         {NULL,NULL} //Sentinel
352 };
353
354
355
356 PyObject* SCA_IObject::_getattr(const STR_String& attr) {
357         _getattr_up(CValue);
358 }
359