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