BGE Python api
[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), m_initState(0), m_state(0)
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                 //Use Delete for sensor to ensure proper cleaning
56                 (*its)->Delete();
57                 //((CValue*)(*its))->Release();
58         }
59         SCA_ControllerList::iterator itc; 
60         for (itc = m_controllers.begin(); !(itc == m_controllers.end()); ++itc)
61         {
62                 //Use Delete for controller to ensure proper cleaning (expression controller)
63                 (*itc)->Delete();
64                 //((CValue*)(*itc))->Release();
65         }
66         SCA_ActuatorList::iterator ita;
67         for (ita = m_registeredActuators.begin(); !(ita==m_registeredActuators.end()); ++ita)
68         {
69                 (*ita)->UnlinkObject(this);
70         }
71         for (ita = m_actuators.begin(); !(ita==m_actuators.end()); ++ita)
72         {
73                 ((CValue*)(*ita))->Release();
74         }
75
76         //T_InterpolatorList::iterator i;
77         //for (i = m_interpolators.begin(); !(i == m_interpolators.end()); ++i) {
78         //      delete *i;
79         //}
80 }
81
82
83
84 SCA_ControllerList& SCA_IObject::GetControllers()
85 {
86         return m_controllers;
87 }
88
89
90
91 SCA_SensorList& SCA_IObject::GetSensors()
92 {
93         return m_sensors;
94 }
95
96
97
98 SCA_ActuatorList& SCA_IObject::GetActuators()
99 {
100         return m_actuators;
101 }
102
103
104
105 void SCA_IObject::AddSensor(SCA_ISensor* act)
106 {
107         act->AddRef();
108         m_sensors.push_back(act);
109 }
110
111
112
113 void SCA_IObject::AddController(SCA_IController* act)
114 {
115         act->AddRef();
116         m_controllers.push_back(act);
117 }
118
119
120
121 void SCA_IObject::AddActuator(SCA_IActuator* act)
122 {
123         act->AddRef();
124         m_actuators.push_back(act);
125 }
126
127 void SCA_IObject::RegisterActuator(SCA_IActuator* act)
128 {
129         // don't increase ref count, it would create dead lock
130         m_registeredActuators.push_back(act);
131 }
132
133 void SCA_IObject::UnregisterActuator(SCA_IActuator* act)
134 {
135         SCA_ActuatorList::iterator ita;
136         for (ita = m_registeredActuators.begin(); ita != m_registeredActuators.end(); ita++)
137         {
138                 if ((*ita) == act) {
139                         (*ita) = m_registeredActuators.back();
140                         m_registeredActuators.pop_back();
141                         break;
142                 }
143         }
144 }
145
146 void SCA_IObject::SetIgnoreActivityCulling(bool b)
147 {
148         m_ignore_activity_culling = b;
149 }
150
151
152
153 bool SCA_IObject::GetIgnoreActivityCulling()
154 {
155         return m_ignore_activity_culling;
156 }
157
158
159
160 void SCA_IObject::ReParentLogic()
161 {
162         SCA_ActuatorList& oldactuators  = GetActuators();
163         int act = 0;
164         SCA_ActuatorList::iterator ita;
165         for (ita = oldactuators.begin(); !(ita==oldactuators.end()); ++ita)
166         {
167                 SCA_IActuator* newactuator = (SCA_IActuator*) (*ita)->GetReplica();
168                 newactuator->ReParent(this);
169                 // actuators are initially not connected to any controller
170                 newactuator->SetActive(false);
171                 newactuator->ClrLink();
172                 oldactuators[act++] = newactuator;
173         }
174
175         SCA_ControllerList& oldcontrollers = GetControllers();
176         int con = 0;
177         SCA_ControllerList::iterator itc;
178         for (itc = oldcontrollers.begin(); !(itc==oldcontrollers.end()); ++itc)
179         {
180                 SCA_IController* newcontroller = (SCA_IController*)(*itc)->GetReplica();
181                 newcontroller->ReParent(this);
182                 newcontroller->SetActive(false);
183                 oldcontrollers[con++]=newcontroller;
184
185         }
186         // convert sensors last so that actuators are already available for Actuator sensor
187         SCA_SensorList& oldsensors = GetSensors();
188         int sen = 0;
189         SCA_SensorList::iterator its;
190         for (its = oldsensors.begin(); !(its==oldsensors.end()); ++its)
191         {
192                 SCA_ISensor* newsensor = (SCA_ISensor*)(*its)->GetReplica();
193                 newsensor->ReParent(this);
194                 newsensor->SetActive(false);
195                 // sensors are initially not connected to any controller
196                 newsensor->ClrLink();
197                 oldsensors[sen++] = newsensor;
198         }
199
200         // a new object cannot be client of any actuator
201         m_registeredActuators.clear();
202                 
203 }
204
205
206
207 SCA_ISensor* SCA_IObject::FindSensor(const STR_String& sensorname)
208 {
209         SCA_ISensor* foundsensor = NULL;
210
211         for (SCA_SensorList::iterator its = m_sensors.begin();!(its==m_sensors.end());its++)
212         {
213                 if ((*its)->GetName() == sensorname)
214                 {
215                         foundsensor = (*its);
216                         break;
217                 }
218         }
219         return foundsensor;
220 }
221
222
223
224 SCA_IController* SCA_IObject::FindController(const STR_String& controllername)
225 {
226         SCA_IController* foundcontroller = NULL;
227
228         for (SCA_ControllerList::iterator itc = m_controllers.begin();!(itc==m_controllers.end());itc++)
229         {
230                 if ((*itc)->GetName() == controllername)
231                 {
232                         foundcontroller = (*itc);
233                         break;
234                 }       
235         }
236         return foundcontroller;
237 }
238
239
240
241 SCA_IActuator* SCA_IObject::FindActuator(const STR_String& actuatorname)
242 {
243         SCA_IActuator* foundactuator = NULL;
244
245         for (SCA_ActuatorList::iterator ita = m_actuators.begin();!(ita==m_actuators.end());ita++)
246         {
247                 if ((*ita)->GetName() == actuatorname)
248                 {
249                         foundactuator = (*ita);
250                         break;
251                 }
252         }
253
254         return foundactuator;
255 }
256
257
258
259 void SCA_IObject::SetCurrentTime(float currentTime) {
260         //T_InterpolatorList::iterator i;
261         //for (i = m_interpolators.begin(); !(i == m_interpolators.end()); ++i) {
262         //      (*i)->Execute(currentTime);
263         //}
264 }
265         
266
267 #if 0
268 const MT_Point3& SCA_IObject::ConvertPythonPylist(PyObject* pylist)
269 {
270         bool error = false;
271         m_sDummy = MT_Vector3(0,0,0);
272         if (pylist->ob_type == &CListValue::Type)
273         {
274                 CListValue* listval = (CListValue*) pylist;
275                 int numelem = listval->GetCount();
276                 if ( numelem <= 3)
277                 {
278                         int index;
279                         for (index = 0;index<numelem;index++)
280                         {
281                                 m_sDummy[index] = listval->GetValue(index)->GetNumber();
282                         }
283                 }       else
284                 {
285                         error = true;
286                 }
287                 
288         } else
289         {
290                 
291                 // assert the list is long enough...
292                 int numitems = PyList_Size(pylist);
293                 if (numitems <= 3)
294                 {
295                         int index;
296                         for (index=0;index<numitems;index++)
297                         {
298                                 m_sDummy[index] = PyFloat_AsDouble(PyList_GetItem(pylist,index));
299                         }
300                 }
301                 else
302                 {
303                         error = true;
304                 }
305
306         }
307         return m_sDummy;
308 }
309 #endif
310
311 void SCA_IObject::Suspend()
312 {
313         if ((!m_ignore_activity_culling) 
314                 && (!m_suspended))  {
315                 m_suspended = true;
316                 /* flag suspend for all sensors */
317                 SCA_SensorList::iterator i = m_sensors.begin();
318                 while (i != m_sensors.end()) {
319                         (*i)->Suspend();
320                         i++;
321                 }
322         }
323 }
324
325
326
327 void SCA_IObject::Resume(void)
328 {
329         if (m_suspended) {
330                 m_suspended = false;
331                 /* unflag suspend for all sensors */
332                 SCA_SensorList::iterator i = m_sensors.begin();
333                 while (i != m_sensors.end()) {
334                         (*i)->Resume();
335                         i++;
336                 }
337         }
338 }
339
340 void SCA_IObject::SetState(unsigned int state)
341 {
342         unsigned int tmpstate;
343         SCA_ControllerList::iterator contit;
344
345         // we will update the state in two steps:
346         // 1) set the new state bits that are 1
347         // 2) clr the new state bits that are 0
348         // This to ensure continuity if a sensor is attached to two states
349         // that are switching state: no need to deactive and reactive the sensor 
350         
351         tmpstate = m_state | state;
352         if (tmpstate != m_state)
353         {
354                 // update the status of the controllers
355                 for (contit = m_controllers.begin(); contit != m_controllers.end(); contit++)
356                 {
357                         (*contit)->ApplyState(tmpstate);
358                 }
359         }
360         m_state = state;
361         if (m_state != tmpstate)
362         {
363                 for (contit = m_controllers.begin(); contit != m_controllers.end(); contit++)
364                 {
365                         (*contit)->ApplyState(m_state);
366                 }
367         }
368 }
369
370
371
372 /* ------------------------------------------------------------------------- */
373 /* Python functions                                                          */
374 /* ------------------------------------------------------------------------- */
375
376 /* Integration hooks ------------------------------------------------------- */
377 PyTypeObject SCA_IObject::Type = {
378         PyObject_HEAD_INIT(&PyType_Type)
379         0,
380         "SCA_IObject",
381         sizeof(SCA_IObject),
382         0,
383         PyDestructor,
384         0,
385         __getattr,
386         __setattr,
387         0, //&MyPyCompare,
388         __repr,
389         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
390         Methods
391 };
392
393
394
395 PyParentObject SCA_IObject::Parents[] = {
396         &SCA_IObject::Type,
397         &CValue::Type,
398         NULL
399 };
400
401
402
403 PyMethodDef SCA_IObject::Methods[] = {
404         //{"setOrientation", (PyCFunction) SCA_IObject::sPySetOrientation, METH_VARARGS},
405         //{"getOrientation", (PyCFunction) SCA_IObject::sPyGetOrientation, METH_VARARGS},
406         {NULL,NULL} //Sentinel
407 };
408
409 PyAttributeDef SCA_IObject::Attributes[] = {
410         { NULL }        //Sentinel
411 };
412
413
414 PyObject* SCA_IObject::_getattr(const char *attr) {
415         _getattr_up(CValue);
416 }
417