unknown property fixed in sensor/actuators
[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         m_sensors.push_back(act);
105 }
106
107
108
109 void SCA_IObject::AddController(SCA_IController* act)
110 {
111         m_controllers.push_back(act);
112 }
113
114
115
116 void SCA_IObject::AddActuator(SCA_IActuator* act)
117 {
118         m_actuators.push_back(act);
119 }
120
121
122
123 void SCA_IObject::SetIgnoreActivityCulling(bool b)
124 {
125         m_ignore_activity_culling = b;
126 }
127
128
129
130 bool SCA_IObject::GetIgnoreActivityCulling()
131 {
132         return m_ignore_activity_culling;
133 }
134
135
136
137 void SCA_IObject::ReParentLogic()
138 {
139         SCA_SensorList& oldsensors = GetSensors();
140         
141         int sen = 0;
142         SCA_SensorList::iterator its;
143         for (its = oldsensors.begin(); !(its==oldsensors.end()); ++its)
144         {
145                 SCA_ISensor* newsensor = (SCA_ISensor*)(*its)->GetReplica();
146                 newsensor->ReParent(this);
147                 oldsensors[sen++] = newsensor;
148         }
149
150         SCA_ControllerList& oldcontrollers = GetControllers();
151         int con = 0;
152         SCA_ControllerList::iterator itc;
153         for (itc = oldcontrollers.begin(); !(itc==oldcontrollers.end()); ++itc)
154         {
155                 SCA_IController* newcontroller = (SCA_IController*)(*itc)->GetReplica();
156                 newcontroller->ReParent(this);
157                 oldcontrollers[con++]=newcontroller;
158
159         }
160         SCA_ActuatorList& oldactuators  = GetActuators();
161         
162         int act = 0;
163         SCA_ActuatorList::iterator ita;
164         for (ita = oldactuators.begin(); !(ita==oldactuators.end()); ++ita)
165         {
166                 SCA_IActuator* newactuator = (SCA_IActuator*) (*ita)->GetReplica();
167                 newactuator->ReParent(this);
168                 newactuator->SetActive(false);
169                 oldactuators[act++] = newactuator;
170         }
171                 
172 }
173
174
175
176 SCA_ISensor* SCA_IObject::FindSensor(const STR_String& sensorname)
177 {
178         SCA_ISensor* foundsensor = NULL;
179
180         for (SCA_SensorList::iterator its = m_sensors.begin();!(its==m_sensors.end());its++)
181         {
182                 if ((*its)->GetName() == sensorname)
183                 {
184                         foundsensor = (*its);
185                         break;
186                 }
187         }
188         return foundsensor;
189 }
190
191
192
193 SCA_IController* SCA_IObject::FindController(const STR_String& controllername)
194 {
195         SCA_IController* foundcontroller = NULL;
196
197         for (SCA_ControllerList::iterator itc = m_controllers.begin();!(itc==m_controllers.end());itc++)
198         {
199                 if ((*itc)->GetName() == controllername)
200                 {
201                         foundcontroller = (*itc);
202                         break;
203                 }       
204         }
205         return foundcontroller;
206 }
207
208
209
210 SCA_IActuator* SCA_IObject::FindActuator(const STR_String& actuatorname)
211 {
212         SCA_IActuator* foundactuator = NULL;
213
214         for (SCA_ActuatorList::iterator ita = m_actuators.begin();!(ita==m_actuators.end());ita++)
215         {
216                 if ((*ita)->GetName() == actuatorname)
217                 {
218                         foundactuator = (*ita);
219                         break;
220                 }
221         }
222
223         return foundactuator;
224 }
225
226
227
228 void SCA_IObject::SetCurrentTime(float currentTime) {
229         //T_InterpolatorList::iterator i;
230         //for (i = m_interpolators.begin(); !(i == m_interpolators.end()); ++i) {
231         //      (*i)->Execute(currentTime);
232         //}
233 }
234         
235
236 #if 0
237 const MT_Point3& SCA_IObject::ConvertPythonPylist(PyObject* pylist)
238 {
239         bool error = false;
240         m_sDummy = MT_Vector3(0,0,0);
241         if (pylist->ob_type == &CListValue::Type)
242         {
243                 CListValue* listval = (CListValue*) pylist;
244                 int numelem = listval->GetCount();
245                 if ( numelem <= 3)
246                 {
247                         int index;
248                         for (index = 0;index<numelem;index++)
249                         {
250                                 m_sDummy[index] = listval->GetValue(index)->GetNumber();
251                         }
252                 }       else
253                 {
254                         error = true;
255                 }
256                 
257         } else
258         {
259                 
260                 // assert the list is long enough...
261                 int numitems = PyList_Size(pylist);
262                 if (numitems <= 3)
263                 {
264                         int index;
265                         for (index=0;index<numitems;index++)
266                         {
267                                 m_sDummy[index] = PyFloat_AsDouble(PyList_GetItem(pylist,index));
268                         }
269                 }
270                 else
271                 {
272                         error = true;
273                 }
274
275         }
276         return m_sDummy;
277 }
278 #endif
279
280 void SCA_IObject::Suspend(void)
281 {
282         if ((!m_ignore_activity_culling) 
283                 && (!m_suspended))  {
284                 m_suspended = true;
285                 /* flag suspend for all sensors */
286                 SCA_SensorList::iterator i = m_sensors.begin();
287                 while (i != m_sensors.end()) {
288                         (*i)->Suspend();
289                         i++;
290                 }
291         }
292 }
293
294
295
296 void SCA_IObject::Resume(void)
297 {
298         if (m_suspended) {
299                 m_suspended = false;
300                 /* unflag suspend for all sensors */
301                 SCA_SensorList::iterator i = m_sensors.begin();
302                 while (i != m_sensors.end()) {
303                         (*i)->Resume();
304                         i++;
305                 }
306         }
307 }
308
309
310
311 /* ------------------------------------------------------------------------- */
312 /* Python functions                                                          */
313 /* ------------------------------------------------------------------------- */
314
315 /* Integration hooks ------------------------------------------------------- */
316 PyTypeObject SCA_IObject::Type = {
317         PyObject_HEAD_INIT(&PyType_Type)
318         0,
319         "SCA_IObject",
320         sizeof(SCA_IObject),
321         0,
322         PyDestructor,
323         0,
324         __getattr,
325         __setattr,
326         0, //&MyPyCompare,
327         __repr,
328         0, //&cvalue_as_number,
329         0,
330         0,
331         0,
332         0
333 };
334
335
336
337 PyParentObject SCA_IObject::Parents[] = {
338         &SCA_IObject::Type,
339         &CValue::Type,
340         NULL
341 };
342
343
344
345 PyMethodDef SCA_IObject::Methods[] = {
346         //{"setOrientation", (PyCFunction) SCA_IObject::sPySetOrientation, METH_VARARGS},
347         //{"getOrientation", (PyCFunction) SCA_IObject::sPyGetOrientation, METH_VARARGS},
348         {NULL,NULL} //Sentinel
349 };
350
351
352
353 PyObject* SCA_IObject::_getattr(const STR_String& attr) {
354         _getattr_up(CValue);
355 }
356