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