Initial revision
[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
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
39 #include "ListValue.h"
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
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
274
275
276 const MT_Point3& SCA_IObject::ConvertPythonVectorArg(PyObject* args)
277 {
278
279         PyObject* pylist;
280         PyArg_ParseTuple(args,"O",&pylist);
281         m_sDummy = ConvertPythonPylist(pylist);
282         return m_sDummy;
283 }
284
285
286
287 void SCA_IObject::Suspend(void)
288 {
289         if ((!m_ignore_activity_culling) 
290                 && (!m_suspended))  {
291                 m_suspended = true;
292                 /* flag suspend for all sensors */
293                 SCA_SensorList::iterator i = m_sensors.begin();
294                 while (i != m_sensors.end()) {
295                         (*i)->Suspend();
296                         i++;
297                 }
298         }
299 }
300
301
302
303 void SCA_IObject::Resume(void)
304 {
305         if (m_suspended) {
306                 m_suspended = false;
307                 /* unflag suspend for all sensors */
308                 SCA_SensorList::iterator i = m_sensors.begin();
309                 while (i != m_sensors.end()) {
310                         (*i)->Resume();
311                         i++;
312                 }
313         }
314 }
315
316
317
318 /* ------------------------------------------------------------------------- */
319 /* Python functions                                                          */
320 /* ------------------------------------------------------------------------- */
321
322 /* Integration hooks ------------------------------------------------------- */
323 PyTypeObject SCA_IObject::Type = {
324         PyObject_HEAD_INIT(&PyType_Type)
325         0,
326         "SCA_IObject",
327         sizeof(SCA_IObject),
328         0,
329         PyDestructor,
330         0,
331         __getattr,
332         __setattr,
333         0, //&MyPyCompare,
334         __repr,
335         0, //&cvalue_as_number,
336         0,
337         0,
338         0,
339         0
340 };
341
342
343
344 PyParentObject SCA_IObject::Parents[] = {
345         &SCA_IObject::Type,
346         &CValue::Type,
347         NULL
348 };
349
350
351
352 PyMethodDef SCA_IObject::Methods[] = {
353         //{"setOrientation", (PyCFunction) SCA_IObject::sPySetOrientation, METH_VARARGS},
354         //{"getOrientation", (PyCFunction) SCA_IObject::sPyGetOrientation, METH_VARARGS},
355         {NULL,NULL} //Sentinel
356 };
357
358
359
360 PyObject* SCA_IObject::_getattr(char* attr) {
361         _getattr_up(CValue);
362 }
363