Patch to change license to GPL only, from GSR.
[blender.git] / source / gameengine / GameLogic / SCA_IObject.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL 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.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28 #include <iostream>
29
30 #include "SCA_IObject.h"
31 #include "SCA_ISensor.h"
32 #include "SCA_IController.h"
33 #include "SCA_IActuator.h"
34 #include "MT_Point3.h"
35 #include "ListValue.h"
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 MT_Point3 SCA_IObject::m_sDummy=MT_Point3(0,0,0);
42
43 SCA_IObject::SCA_IObject(PyTypeObject* T): CValue(T)
44 {
45         m_suspended = false;
46 }
47         
48
49
50 SCA_IObject::~SCA_IObject()
51 {
52         SCA_SensorList::iterator its;
53         for (its = m_sensors.begin(); !(its == m_sensors.end()); ++its)
54         {
55                 ((CValue*)(*its))->Release();
56         }
57         SCA_ControllerList::iterator itc; 
58         for (itc = m_controllers.begin(); !(itc == m_controllers.end()); ++itc)
59         {
60                 ((CValue*)(*itc))->Release();
61         }
62         SCA_ActuatorList::iterator ita;
63         for (ita = m_actuators.begin(); !(ita==m_actuators.end()); ++ita)
64         {
65                 ((CValue*)(*ita))->Release();
66         }
67
68         //T_InterpolatorList::iterator i;
69         //for (i = m_interpolators.begin(); !(i == m_interpolators.end()); ++i) {
70         //      delete *i;
71         //}
72 }
73
74
75
76 SCA_ControllerList& SCA_IObject::GetControllers()
77 {
78         return m_controllers;
79 }
80
81
82
83 SCA_SensorList& SCA_IObject::GetSensors()
84 {
85         return m_sensors;
86 }
87
88
89
90 SCA_ActuatorList& SCA_IObject::GetActuators()
91 {
92         return m_actuators;
93 }
94
95
96
97 void SCA_IObject::AddSensor(SCA_ISensor* act)
98 {
99         m_sensors.push_back(act);
100 }
101
102
103
104 void SCA_IObject::AddController(SCA_IController* act)
105 {
106         m_controllers.push_back(act);
107 }
108
109
110
111 void SCA_IObject::AddActuator(SCA_IActuator* act)
112 {
113         m_actuators.push_back(act);
114 }
115
116
117
118 void SCA_IObject::SetIgnoreActivityCulling(bool b)
119 {
120         m_ignore_activity_culling = b;
121 }
122
123
124
125 bool SCA_IObject::GetIgnoreActivityCulling()
126 {
127         return m_ignore_activity_culling;
128 }
129
130
131
132 void SCA_IObject::ReParentLogic()
133 {
134         SCA_SensorList& oldsensors = GetSensors();
135         
136         int sen = 0;
137         SCA_SensorList::iterator its;
138         for (its = oldsensors.begin(); !(its==oldsensors.end()); ++its)
139         {
140                 SCA_ISensor* newsensor = (SCA_ISensor*)(*its)->GetReplica();
141                 newsensor->ReParent(this);
142                 oldsensors[sen++] = newsensor;
143         }
144
145         SCA_ControllerList& oldcontrollers = GetControllers();
146         int con = 0;
147         SCA_ControllerList::iterator itc;
148         for (itc = oldcontrollers.begin(); !(itc==oldcontrollers.end()); ++itc)
149         {
150                 SCA_IController* newcontroller = (SCA_IController*)(*itc)->GetReplica();
151                 newcontroller->ReParent(this);
152                 oldcontrollers[con++]=newcontroller;
153
154         }
155         SCA_ActuatorList& oldactuators  = GetActuators();
156         
157         int act = 0;
158         SCA_ActuatorList::iterator ita;
159         for (ita = oldactuators.begin(); !(ita==oldactuators.end()); ++ita)
160         {
161                 SCA_IActuator* newactuator = (SCA_IActuator*) (*ita)->GetReplica();
162                 newactuator->ReParent(this);
163                 newactuator->SetActive(false);
164                 oldactuators[act++] = newactuator;
165         }
166                 
167 }
168
169
170
171 SCA_ISensor* SCA_IObject::FindSensor(const STR_String& sensorname)
172 {
173         SCA_ISensor* foundsensor = NULL;
174
175         for (SCA_SensorList::iterator its = m_sensors.begin();!(its==m_sensors.end());its++)
176         {
177                 if ((*its)->GetName() == sensorname)
178                 {
179                         foundsensor = (*its);
180                         break;
181                 }
182         }
183         return foundsensor;
184 }
185
186
187
188 SCA_IController* SCA_IObject::FindController(const STR_String& controllername)
189 {
190         SCA_IController* foundcontroller = NULL;
191
192         for (SCA_ControllerList::iterator itc = m_controllers.begin();!(itc==m_controllers.end());itc++)
193         {
194                 if ((*itc)->GetName() == controllername)
195                 {
196                         foundcontroller = (*itc);
197                         break;
198                 }       
199         }
200         return foundcontroller;
201 }
202
203
204
205 SCA_IActuator* SCA_IObject::FindActuator(const STR_String& actuatorname)
206 {
207         SCA_IActuator* foundactuator = NULL;
208
209         for (SCA_ActuatorList::iterator ita = m_actuators.begin();!(ita==m_actuators.end());ita++)
210         {
211                 if ((*ita)->GetName() == actuatorname)
212                 {
213                         foundactuator = (*ita);
214                         break;
215                 }
216         }
217
218         return foundactuator;
219 }
220
221
222
223 void SCA_IObject::SetCurrentTime(float currentTime) {
224         //T_InterpolatorList::iterator i;
225         //for (i = m_interpolators.begin(); !(i == m_interpolators.end()); ++i) {
226         //      (*i)->Execute(currentTime);
227         //}
228 }
229         
230
231 #if 0
232 const MT_Point3& SCA_IObject::ConvertPythonPylist(PyObject* pylist)
233 {
234         bool error = false;
235         m_sDummy = MT_Vector3(0,0,0);
236         if (pylist->ob_type == &CListValue::Type)
237         {
238                 CListValue* listval = (CListValue*) pylist;
239                 int numelem = listval->GetCount();
240                 if ( numelem <= 3)
241                 {
242                         int index;
243                         for (index = 0;index<numelem;index++)
244                         {
245                                 m_sDummy[index] = listval->GetValue(index)->GetNumber();
246                         }
247                 }       else
248                 {
249                         error = true;
250                 }
251                 
252         } else
253         {
254                 
255                 // assert the list is long enough...
256                 int numitems = PyList_Size(pylist);
257                 if (numitems <= 3)
258                 {
259                         int index;
260                         for (index=0;index<numitems;index++)
261                         {
262                                 m_sDummy[index] = PyFloat_AsDouble(PyList_GetItem(pylist,index));
263                         }
264                 }
265                 else
266                 {
267                         error = true;
268                 }
269
270         }
271         return m_sDummy;
272 }
273 #endif
274
275 void SCA_IObject::Suspend(void)
276 {
277         if ((!m_ignore_activity_culling) 
278                 && (!m_suspended))  {
279                 m_suspended = true;
280                 /* flag suspend for all sensors */
281                 SCA_SensorList::iterator i = m_sensors.begin();
282                 while (i != m_sensors.end()) {
283                         (*i)->Suspend();
284                         i++;
285                 }
286         }
287 }
288
289
290
291 void SCA_IObject::Resume(void)
292 {
293         if (m_suspended) {
294                 m_suspended = false;
295                 /* unflag suspend for all sensors */
296                 SCA_SensorList::iterator i = m_sensors.begin();
297                 while (i != m_sensors.end()) {
298                         (*i)->Resume();
299                         i++;
300                 }
301         }
302 }
303
304
305
306 /* ------------------------------------------------------------------------- */
307 /* Python functions                                                          */
308 /* ------------------------------------------------------------------------- */
309
310 /* Integration hooks ------------------------------------------------------- */
311 PyTypeObject SCA_IObject::Type = {
312         PyObject_HEAD_INIT(&PyType_Type)
313         0,
314         "SCA_IObject",
315         sizeof(SCA_IObject),
316         0,
317         PyDestructor,
318         0,
319         __getattr,
320         __setattr,
321         0, //&MyPyCompare,
322         __repr,
323         0, //&cvalue_as_number,
324         0,
325         0,
326         0,
327         0
328 };
329
330
331
332 PyParentObject SCA_IObject::Parents[] = {
333         &SCA_IObject::Type,
334         &CValue::Type,
335         NULL
336 };
337
338
339
340 PyMethodDef SCA_IObject::Methods[] = {
341         //{"setOrientation", (PyCFunction) SCA_IObject::sPySetOrientation, METH_VARARGS},
342         //{"getOrientation", (PyCFunction) SCA_IObject::sPyGetOrientation, METH_VARARGS},
343         {NULL,NULL} //Sentinel
344 };
345
346
347
348 PyObject* SCA_IObject::_getattr(const STR_String& attr) {
349         _getattr_up(CValue);
350 }
351