BGE logic patch: new "Add" mode for Ipo actuator, several corrections in state system.
[blender.git] / source / gameengine / Ketsji / KX_Scene.cpp
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * Ketsji scene. Holds references to all scene data.
29  */
30
31 #ifdef WIN32
32 #pragma warning (disable : 4786)
33 #endif //WIN32
34
35
36 #include "KX_Scene.h"
37 #include "MT_assert.h"
38
39 #include "KX_KetsjiEngine.h"
40 #include "RAS_IPolygonMaterial.h"
41 #include "ListValue.h"
42 #include "SCA_LogicManager.h"
43 #include "SCA_TimeEventManager.h"
44 #include "SCA_AlwaysEventManager.h"
45 #include "SCA_RandomEventManager.h"
46 #include "KX_RayEventManager.h"
47 #include "KX_TouchEventManager.h"
48 #include "SCA_KeyboardManager.h"
49 #include "SCA_MouseManager.h"
50 #include "SCA_PropertyEventManager.h"
51 #include "SCA_ActuatorEventManager.h"
52 #include "KX_Camera.h"
53 #include "SCA_JoystickManager.h"
54
55 #include "RAS_MeshObject.h"
56 #include "BL_SkinMeshObject.h"
57
58 #include "RAS_IRasterizer.h"
59 #include "RAS_BucketManager.h"
60
61 #include "FloatValue.h"
62 #include "SCA_IController.h"
63 #include "SCA_IActuator.h"
64 #include "SG_Node.h"
65 #include "SYS_System.h"
66 #include "SG_Controller.h"
67 #include "SG_IObject.h"
68 #include "SG_Tree.h"
69
70 #include "KX_SG_NodeRelationships.h"
71
72 #include "KX_NetworkEventManager.h"
73 #include "NG_NetworkScene.h"
74 #include "PHY_IPhysicsEnvironment.h"
75 #include "KX_IPhysicsController.h"
76 #include "KX_BlenderSceneConverter.h"
77
78 #include "BL_ShapeDeformer.h"
79 #include "BL_DeformableGameObject.h"
80
81 // to get USE_BULLET!
82 #include "KX_ConvertPhysicsObject.h"
83
84 #ifdef USE_BULLET
85 #include "CcdPhysicsEnvironment.h"
86 #include "CcdPhysicsController.h"
87 #endif
88
89 void* KX_SceneReplicationFunc(SG_IObject* node,void* gameobj,void* scene)
90 {
91         KX_GameObject* replica = ((KX_Scene*)scene)->AddNodeReplicaObject(node,(KX_GameObject*)gameobj);
92
93         return (void*)replica;
94 }
95
96 void* KX_SceneDestructionFunc(SG_IObject* node,void* gameobj,void* scene)
97 {
98         ((KX_Scene*)scene)->RemoveNodeDestructObject(node,(KX_GameObject*)gameobj);
99
100         return NULL;
101 };
102
103 SG_Callbacks KX_Scene::m_callbacks = SG_Callbacks(KX_SceneReplicationFunc,KX_SceneDestructionFunc,KX_GameObject::UpdateTransformFunc);
104
105 // temporarily var until there is a button in the userinterface
106 // (defined in KX_PythonInit.cpp)
107 extern bool gUseVisibilityTemp;
108
109 KX_Scene::KX_Scene(class SCA_IInputDevice* keyboarddevice,
110                                    class SCA_IInputDevice* mousedevice,
111                                    class NG_NetworkDeviceInterface *ndi,
112                                    class SND_IAudioDevice* adi,
113                                    const STR_String& sceneName): 
114         PyObjectPlus(&KX_Scene::Type),
115         m_keyboardmgr(NULL),
116         m_mousemgr(NULL),
117         m_physicsEnvironment(0),
118         m_sceneName(sceneName),
119         m_adi(adi),
120         m_networkDeviceInterface(ndi),
121         m_active_camera(NULL),
122         m_ueberExecutionPriority(0),
123         m_sceneConverter(NULL)
124 {
125         m_suspendedtime = 0.0;
126         m_suspendeddelta = 0.0;
127
128         m_activity_culling = false;
129         m_suspend = false;
130         m_isclearingZbuffer = true;
131         m_tempObjectList = new CListValue();
132         m_objectlist = new CListValue();
133         m_parentlist = new CListValue();
134         m_lightlist= new CListValue();
135         m_inactivelist = new CListValue();
136         m_euthanasyobjects = new CListValue();
137         m_delayReleaseObjects = new CListValue();
138
139         m_logicmgr = new SCA_LogicManager();
140         
141         m_timemgr = new SCA_TimeEventManager(m_logicmgr);
142         m_keyboardmgr = new SCA_KeyboardManager(m_logicmgr,keyboarddevice);
143         m_mousemgr = new SCA_MouseManager(m_logicmgr,mousedevice);
144         
145         SCA_AlwaysEventManager* alwaysmgr = new SCA_AlwaysEventManager(m_logicmgr);
146         SCA_PropertyEventManager* propmgr = new SCA_PropertyEventManager(m_logicmgr);
147         SCA_ActuatorEventManager* actmgr = new SCA_ActuatorEventManager(m_logicmgr);
148         SCA_RandomEventManager* rndmgr = new SCA_RandomEventManager(m_logicmgr);
149         KX_RayEventManager* raymgr = new KX_RayEventManager(m_logicmgr);
150
151         KX_NetworkEventManager* netmgr = new KX_NetworkEventManager(m_logicmgr, ndi);
152         
153         SCA_JoystickManager *joymgr     = new SCA_JoystickManager(m_logicmgr);
154
155         m_logicmgr->RegisterEventManager(alwaysmgr);
156         m_logicmgr->RegisterEventManager(propmgr);
157         m_logicmgr->RegisterEventManager(actmgr);
158         m_logicmgr->RegisterEventManager(m_keyboardmgr);
159         m_logicmgr->RegisterEventManager(m_mousemgr);
160         m_logicmgr->RegisterEventManager(m_timemgr);
161         m_logicmgr->RegisterEventManager(rndmgr);
162         m_logicmgr->RegisterEventManager(raymgr);
163         m_logicmgr->RegisterEventManager(netmgr);
164         m_logicmgr->RegisterEventManager(joymgr);
165
166         m_soundScene = new SND_Scene(adi);
167         MT_assert (m_networkDeviceInterface != NULL);
168         m_networkScene = new NG_NetworkScene(m_networkDeviceInterface);
169         
170         m_rootnode = NULL;
171
172         m_bucketmanager=new RAS_BucketManager();
173
174         m_canvasDesignWidth = 0;
175         m_canvasDesignHeight = 0;
176         
177         m_attrlist = PyDict_New(); /* new ref */
178 }
179
180
181
182 KX_Scene::~KX_Scene()
183 {
184         // The release of debug properties used to be in SCA_IScene::~SCA_IScene
185         // It's still there but we remove all properties here otherwise some
186         // reference might be hanging and causing late release of objects
187         RemoveAllDebugProperties();
188
189         while (GetRootParentList()->GetCount() > 0) 
190         {
191                 KX_GameObject* parentobj = (KX_GameObject*) GetRootParentList()->GetValue(0);
192                 this->RemoveObject(parentobj);
193         }
194
195         if(m_objectlist)
196                 m_objectlist->Release();
197
198         if (m_parentlist)
199                 m_parentlist->Release();
200         
201         if (m_inactivelist)
202                 m_inactivelist->Release();
203
204         if (m_lightlist)
205                 m_lightlist->Release();
206         
207         if (m_tempObjectList)
208                 m_tempObjectList->Release();
209
210         if (m_euthanasyobjects)
211                 m_euthanasyobjects->Release();
212         if (m_delayReleaseObjects)
213                 m_delayReleaseObjects->Release();
214
215         if (m_logicmgr)
216                 delete m_logicmgr;
217
218         if (m_physicsEnvironment)
219                 delete m_physicsEnvironment;
220
221         if (m_soundScene)
222                 delete m_soundScene;
223
224         if (m_networkScene)
225                 delete m_networkScene;
226         
227         if (m_bucketmanager)
228         {
229                 delete m_bucketmanager;
230         }
231 #ifdef USE_BULLET
232         // This is a fix for memory leaks in bullet: the collision shapes is not destroyed 
233         // when the physical controllers are destroyed. The reason is that shapes are shared
234         // between replicas of an object. There is no reference count in Bullet so the
235         // only workaround that does not involve changes in Bullet is to save in this array
236         // the list of shapes that are created when the scene is created (see KX_ConvertPhysicsObjects.cpp)
237         class btCollisionShape* shape;
238         class btTriangleMeshShape* meshShape;
239         vector<class btCollisionShape*>::iterator it = m_shapes.begin();
240         while (it != m_shapes.end()) {
241                 shape = *it;
242                 if (shape->getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE)
243                 {
244                         meshShape = static_cast<btTriangleMeshShape*>(shape);
245                         // shapes based on meshes use an interface that contains the vertices.
246                         // Again the idea is to be able to share the interface between shapes but
247                         // this is not used in Blender: each base object will have its own interface 
248                         btStridingMeshInterface* meshInterface = meshShape->getMeshInterface();
249                         if (meshInterface)
250                                 delete meshInterface;
251                 }
252                 delete shape;
253                 it++;
254         }
255 #endif
256         //Py_DECREF(m_attrlist);
257 }
258
259 void KX_Scene::AddShape(class btCollisionShape*shape)
260 {
261         m_shapes.push_back(shape);
262 }
263
264
265 void KX_Scene::SetProjectionMatrix(MT_CmMatrix4x4& pmat)
266 {
267         m_projectionmat = pmat;
268 }
269
270
271
272 RAS_BucketManager* KX_Scene::GetBucketManager()
273 {
274         return m_bucketmanager;
275 }
276
277
278
279 CListValue* KX_Scene::GetObjectList()
280 {
281         return m_objectlist;
282 }
283
284
285
286 CListValue* KX_Scene::GetRootParentList()
287 {
288         return m_parentlist;
289 }
290
291 CListValue* KX_Scene::GetInactiveList()
292 {
293         return m_inactivelist;
294 }
295
296
297
298 CListValue* KX_Scene::GetLightList()
299 {
300         return m_lightlist;
301 }
302
303 SCA_LogicManager* KX_Scene::GetLogicManager()
304 {
305         return m_logicmgr;
306 }
307
308 SCA_TimeEventManager* KX_Scene::GetTimeEventManager()
309 {
310         return m_timemgr;
311 }
312
313
314
315  
316 list<class KX_Camera*>* KX_Scene::GetCameras()
317 {
318         return &m_cameras;
319 }
320
321
322
323 void KX_Scene::SetFramingType(RAS_FrameSettings & frame_settings)
324 {
325         m_frame_settings = frame_settings;
326 };
327
328 /**
329  * Return a const reference to the framing 
330  * type set by the above call.
331  * The contents are not guarenteed to be sensible
332  * if you don't call the above function.
333  */
334 const RAS_FrameSettings& KX_Scene::GetFramingType() const 
335 {
336         return m_frame_settings;
337 };      
338
339
340
341 /**
342  * Store the current scene's viewport on the 
343  * game engine canvas.
344  */
345 void KX_Scene::SetSceneViewport(const RAS_Rect &viewport)
346 {
347         m_viewport = viewport;
348 }
349
350
351
352 const RAS_Rect& KX_Scene::GetSceneViewport() const 
353 {
354         return m_viewport;
355 }
356
357
358
359 void KX_Scene::SetWorldInfo(class KX_WorldInfo* worldinfo)
360 {
361         m_worldinfo = worldinfo;
362 }
363
364
365
366 class KX_WorldInfo* KX_Scene::GetWorldInfo()
367 {
368         return m_worldinfo;
369 }
370
371
372
373 SND_Scene* KX_Scene::GetSoundScene()
374 {
375         return m_soundScene;
376 }
377
378 const STR_String& KX_Scene::GetName()
379 {
380         return m_sceneName;
381 }
382
383
384 void KX_Scene::Suspend()
385 {
386         m_suspend = true;
387 }
388
389 void KX_Scene::Resume()
390 {
391         m_suspend = false;
392 }
393
394 void KX_Scene::SetActivityCulling(bool b)
395 {
396         m_activity_culling = b;
397 }
398
399 bool KX_Scene::IsSuspended()
400 {
401         return m_suspend;
402 }
403
404 bool KX_Scene::IsClearingZBuffer()
405 {
406         return m_isclearingZbuffer;
407 }
408
409 void KX_Scene::EnableZBufferClearing(bool isclearingZbuffer)
410 {
411         m_isclearingZbuffer = isclearingZbuffer;
412 }
413
414 void KX_Scene::RemoveNodeDestructObject(class SG_IObject* node,class CValue* gameobj)
415 {
416         KX_GameObject* orgobj = (KX_GameObject*)gameobj;        
417         if (NewRemoveObject(orgobj) != 0)
418         {
419                 // object is not yet deleted (this can happen when it hangs in an add object actuator
420                 // last object created reference. It's a bad situation, don't know how to fix it exactly
421                 // The least I can do, is remove the reference to the node in the object as the node
422                 // will in any case be deleted. This ensures that the object will not try to use the node
423                 // when it is finally deleted (see KX_GameObject destructor)
424                 orgobj->SetSGNode(NULL);
425         }
426         if (node)
427                 delete node;
428 }
429
430 KX_GameObject* KX_Scene::AddNodeReplicaObject(class SG_IObject* node, class CValue* gameobj)
431 {
432         KX_GameObject* orgobj = (KX_GameObject*)gameobj;
433         KX_GameObject* newobj = (KX_GameObject*)orgobj->GetReplica();
434         m_map_gameobject_to_replica.insert(orgobj, newobj);
435
436         // also register 'timers' (time properties) of the replica
437         int numprops = newobj->GetPropertyCount();
438
439         for (int i = 0; i < numprops; i++)
440         {
441                 CValue* prop = newobj->GetProperty(i);
442
443                 if (prop->GetProperty("timer"))
444                         this->m_timemgr->AddTimeProperty(prop);
445         }
446
447         if (node)
448         {
449                 newobj->SetSGNode((SG_Node*)node);
450         }
451         else
452         {
453                 m_rootnode = new SG_Node(newobj,this,KX_Scene::m_callbacks);
454         
455                 // this fixes part of the scaling-added object bug
456                 SG_Node* orgnode = orgobj->GetSGNode();
457                 m_rootnode->SetLocalScale(orgnode->GetLocalScale());
458                 m_rootnode->SetLocalPosition(orgnode->GetLocalPosition());
459                 m_rootnode->SetLocalOrientation(orgnode->GetLocalOrientation());
460
461                 // define the relationship between this node and it's parent.
462                 KX_NormalParentRelation * parent_relation = 
463                         KX_NormalParentRelation::New();
464                 m_rootnode->SetParentRelation(parent_relation);
465
466                 newobj->SetSGNode(m_rootnode);
467         }
468         
469         SG_IObject* replicanode = newobj->GetSGNode();
470 //      SG_Node* rootnode = (replicanode == m_rootnode ? NULL : m_rootnode);
471
472         replicanode->SetSGClientObject(newobj);
473
474         // this is the list of object that are send to the graphics pipeline
475         m_objectlist->Add(newobj->AddRef());
476         newobj->Bucketize();
477
478         // logic cannot be replicated, until the whole hierarchy is replicated.
479         m_logicHierarchicalGameObjects.push_back(newobj);
480         //replicate controllers of this node
481         SGControllerList        scenegraphcontrollers = orgobj->GetSGNode()->GetSGControllerList();
482         replicanode->RemoveAllControllers();
483         SGControllerList::iterator cit;
484         //int numcont = scenegraphcontrollers.size();
485         
486         for (cit = scenegraphcontrollers.begin();!(cit==scenegraphcontrollers.end());++cit)
487         {
488                 // controller replication is quite complicated
489                 // only replicate ipo and physics controller for now
490
491                 SG_Controller* replicacontroller = (*cit)->GetReplica((SG_Node*) replicanode);
492                 if (replicacontroller)
493                 {
494                         replicacontroller->SetObject(replicanode);
495                         replicanode->AddSGController(replicacontroller);
496                 }
497         }
498         
499         return newobj;
500 }
501
502
503
504 // before calling this method KX_Scene::ReplicateLogic(), make sure to
505 // have called 'GameObject::ReParentLogic' for each object this
506 // hierarchy that's because first ALL bricks must exist in the new
507 // replica of the hierarchy in order to make cross-links work properly
508 // !
509 void KX_Scene::ReplicateLogic(KX_GameObject* newobj)
510 {
511         // also relink the controller to sensors/actuators
512         SCA_ControllerList& controllers = newobj->GetControllers();
513         //SCA_SensorList&     sensors     = newobj->GetSensors();
514         //SCA_ActuatorList&   actuators   = newobj->GetActuators();
515
516         for (SCA_ControllerList::iterator itc = controllers.begin(); !(itc==controllers.end());itc++)
517         {
518                 SCA_IController* cont = (*itc);
519                 cont->SetUeberExecutePriority(m_ueberExecutionPriority);
520                 vector<SCA_ISensor*> linkedsensors = cont->GetLinkedSensors();
521                 vector<SCA_IActuator*> linkedactuators = cont->GetLinkedActuators();
522
523                 // disconnect the sensors and actuators
524                 cont->UnlinkAllSensors();
525                 cont->UnlinkAllActuators();
526                 
527                 // now relink each sensor
528                 for (vector<SCA_ISensor*>::iterator its = linkedsensors.begin();!(its==linkedsensors.end());its++)
529                 {
530                         SCA_ISensor* oldsensor = (*its);
531                         STR_String name = oldsensor->GetName();
532                         //find this name in the list
533                         SCA_ISensor* newsensor = newobj->FindSensor(name);
534                 
535                         if (newsensor)
536                         {
537                                 // relink this newsensor to the controller
538                                 m_logicmgr->RegisterToSensor(cont,newsensor);
539                         }
540                         else
541                         {
542                                 // it can be linked somewhere in the hierarchy or...
543                                 for (vector<KX_GameObject*>::iterator git = m_logicHierarchicalGameObjects.begin();
544                                 !(git==m_logicHierarchicalGameObjects.end());++git)
545                                 {
546                                         newsensor = (*git)->FindSensor(name);
547                                         if (newsensor)
548                                                 break;
549                                 } 
550
551                                 if (newsensor)
552                                 {
553                                         // relink this newsensor to the controller somewhere else within this
554                                         // hierarchy
555                                         m_logicmgr->RegisterToSensor(cont,newsensor);
556                                 }
557                                 else
558                                 {
559                                         // must be an external sensor, so...
560                                         m_logicmgr->RegisterToSensor(cont,oldsensor);
561                                 }
562                         }
563                 }
564                 
565                 // now relink each actuator
566                 for (vector<SCA_IActuator*>::iterator ita = linkedactuators.begin();!(ita==linkedactuators.end());ita++)
567                 {
568                         SCA_IActuator* oldactuator = (*ita);
569                         STR_String name = oldactuator->GetName();
570                         //find this name in the list
571                         SCA_IActuator* newactuator = newobj->FindActuator(name);
572                         if (newactuator)
573                         {
574                                 // relink this newsensor to the controller
575                                 m_logicmgr->RegisterToActuator(cont,newactuator);
576                                 newactuator->SetUeberExecutePriority(m_ueberExecutionPriority);
577                         }
578                         else
579                         {
580                                 // it can be linked somewhere in the hierarchy or...
581                                 for (vector<KX_GameObject*>::iterator git = m_logicHierarchicalGameObjects.begin();
582                                 !(git==m_logicHierarchicalGameObjects.end());++git)
583                                 {
584                                         newactuator= (*git)->FindActuator(name);
585                                         if (newactuator)
586                                                 break;
587                                 } 
588
589                                 if (newactuator)
590                                 {
591                                         // relink this actuator to the controller somewhere else within this
592                                         // hierarchy
593                                         m_logicmgr->RegisterToActuator(cont,newactuator);
594                                         newactuator->SetUeberExecutePriority(m_ueberExecutionPriority);
595                                 }
596                                 else
597                                 {
598                                         // must be an external actuator, so...
599                                         m_logicmgr->RegisterToActuator(cont,oldactuator);
600                                 }
601                         }
602                 }
603         }
604         // ready to set initial state
605         newobj->ResetState();
606 }
607
608
609
610 SCA_IObject* KX_Scene::AddReplicaObject(class CValue* originalobject,
611                                                                                 class CValue* parentobject,
612                                                                                 int lifespan)
613 {
614
615         m_logicHierarchicalGameObjects.clear();
616         m_map_gameobject_to_replica.clear();
617
618         // todo: place a timebomb in the object, for temporarily objects :)
619         // lifespan of zero means 'this object lives forever'
620         KX_GameObject* originalobj = (KX_GameObject*) originalobject;
621         KX_GameObject* parentobj = (KX_GameObject*) parentobject;
622
623         m_ueberExecutionPriority++;
624
625         // lets create a replica
626         KX_GameObject* replica = (KX_GameObject*) AddNodeReplicaObject(NULL,originalobj);
627
628         if (lifespan > 0)
629         {
630                 // add a timebomb to this object
631                 // for now, convert between so called frames and realtime
632                 m_tempObjectList->Add(replica->AddRef());
633                 CValue *fval = new CFloatValue(lifespan*0.02);
634                 replica->SetProperty("::timebomb",fval);
635                 fval->Release();
636         }
637
638         // add to 'rootparent' list (this is the list of top hierarchy objects, updated each frame)
639         m_parentlist->Add(replica->AddRef());
640
641         // recurse replication into children nodes
642
643         NodeList& children = originalobj->GetSGNode()->GetSGChildren();
644
645         replica->GetSGNode()->ClearSGChildren();
646         for (NodeList::iterator childit = children.begin();!(childit==children.end());++childit)
647         {
648                 SG_Node* orgnode = (*childit);
649                 SG_Node* childreplicanode = orgnode->GetSGReplica();
650                 replica->GetSGNode()->AddChild(childreplicanode);
651         }
652
653         //      relink any pointers as necessary, sort of a temporary solution
654         vector<KX_GameObject*>::iterator git;
655         for (git = m_logicHierarchicalGameObjects.begin();!(git==m_logicHierarchicalGameObjects.end());++git)
656         {
657                 (*git)->Relink(&m_map_gameobject_to_replica);
658                 // add the object in the layer of the parent
659                 (*git)->SetLayer(parentobj->GetLayer());
660         }
661
662         // now replicate logic
663         for (git = m_logicHierarchicalGameObjects.begin();!(git==m_logicHierarchicalGameObjects.end());++git)
664         {
665                 (*git)->ReParentLogic();
666         }
667         
668         // replicate crosslinks etc. between logic bricks
669         for (git = m_logicHierarchicalGameObjects.begin();!(git==m_logicHierarchicalGameObjects.end());++git)
670         {
671                 ReplicateLogic((*git));
672         }
673         
674         MT_Point3 newpos = ((KX_GameObject*) parentobject)->NodeGetWorldPosition();
675         replica->NodeSetLocalPosition(newpos);
676
677         MT_Matrix3x3 newori = ((KX_GameObject*) parentobject)->NodeGetWorldOrientation();
678         replica->NodeSetLocalOrientation(newori);
679         
680         // get the rootnode's scale
681         MT_Vector3 newscale = parentobj->GetSGNode()->GetRootSGParent()->GetLocalScale();
682
683         // set the replica's relative scale with the rootnode's scale
684         replica->NodeSetRelativeScale(newscale);
685
686         if (replica->GetPhysicsController())
687         {
688                 replica->GetPhysicsController()->setPosition(newpos);
689                 replica->GetPhysicsController()->setOrientation(newori.getRotation());
690                 replica->GetPhysicsController()->setScaling(newscale);
691         }
692
693         // here we want to set the relative scale: the rootnode's scale will override all other
694         // scalings, so lets better prepare for it
695
696
697         replica->GetSGNode()->UpdateWorldData(0);
698         replica->GetSGNode()->SetBBox(originalobj->GetSGNode()->BBox());
699         replica->GetSGNode()->SetRadius(originalobj->GetSGNode()->Radius());
700         //      don't release replica here because we are returning it, not done with it...
701         return replica;
702 }
703
704
705
706 void KX_Scene::RemoveObject(class CValue* gameobj)
707 {
708         KX_GameObject* newobj = (KX_GameObject*) gameobj;
709
710         // first disconnect child from parent
711         SG_Node* node = newobj->GetSGNode();
712
713         if (node)
714         {
715                 node->DisconnectFromParent();
716
717                 // recursively destruct
718                 node->Destruct();
719         }
720         //no need to do that: the object is destroyed and memory released 
721         //newobj->SetSGNode(0);
722 }
723
724 void KX_Scene::DelayedReleaseObject(CValue* gameobj)
725 {
726         m_delayReleaseObjects->Add(gameobj->AddRef());
727 }
728
729
730 void KX_Scene::DelayedRemoveObject(class CValue* gameobj)
731 {
732         //KX_GameObject* newobj = (KX_GameObject*) gameobj;
733         if (!m_euthanasyobjects->SearchValue(gameobj))
734         {
735                 m_euthanasyobjects->Add(gameobj->AddRef());
736         } 
737 }
738
739
740
741 int KX_Scene::NewRemoveObject(class CValue* gameobj)
742 {
743         int ret;
744         KX_GameObject* newobj = (KX_GameObject*) gameobj;
745
746         //todo: look at this
747         //GetPhysicsEnvironment()->RemovePhysicsController(gameobj->getPhysicsController());
748
749         // remove all sensors/controllers/actuators from logicsystem...
750         
751         SCA_SensorList& sensors = newobj->GetSensors();
752         for (SCA_SensorList::iterator its = sensors.begin();
753                  !(its==sensors.end());its++)
754         {
755                 m_logicmgr->RemoveSensor(*its);
756         }
757         
758     SCA_ControllerList& controllers = newobj->GetControllers();
759         for (SCA_ControllerList::iterator itc = controllers.begin();
760                  !(itc==controllers.end());itc++)
761         {
762                 m_logicmgr->RemoveController(*itc);
763         }
764
765         SCA_ActuatorList& actuators = newobj->GetActuators();
766         for (SCA_ActuatorList::iterator ita = actuators.begin();
767                  !(ita==actuators.end());ita++)
768         {
769                 m_logicmgr->RemoveDestroyedActuator(*ita);
770         }
771         // the sensors/controllers/actuators must also be released, this is done in ~SCA_IObject
772
773         // now remove the timer properties from the time manager
774         int numprops = newobj->GetPropertyCount();
775
776         for (int i = 0; i < numprops; i++)
777         {
778                 CValue* propval = newobj->GetProperty(i);
779                 if (propval->GetProperty("timer"))
780                 {
781                         m_timemgr->RemoveTimeProperty(propval);
782                 }
783         }
784         
785         newobj->RemoveMeshes();
786         ret = 1;
787         if (m_objectlist->RemoveValue(newobj))
788                 ret = newobj->Release();
789         if (m_tempObjectList->RemoveValue(newobj))
790                 ret = newobj->Release();
791         if (m_parentlist->RemoveValue(newobj))
792                 ret = newobj->Release();
793         if (m_inactivelist->RemoveValue(newobj))
794                 ret = newobj->Release();
795         if (m_euthanasyobjects->RemoveValue(newobj))
796                 ret = newobj->Release();
797                 
798         if (newobj == m_active_camera)
799         {
800                 //no AddRef done on m_active_camera so no Release
801                 //m_active_camera->Release();
802                 m_active_camera = NULL;
803         }
804         // in case this is a camera
805         m_cameras.remove((KX_Camera*)newobj);
806
807         if (m_sceneConverter)
808                 m_sceneConverter->UnregisterGameObject(newobj);
809         // return value will be 0 if the object is actually deleted (all reference gone)
810         return ret;
811 }
812
813
814
815 void KX_Scene::ReplaceMesh(class CValue* obj,void* meshobj)
816 {
817         KX_GameObject* gameobj = static_cast<KX_GameObject*>(obj);
818         RAS_MeshObject* mesh = static_cast<RAS_MeshObject*>(meshobj);
819
820         if(!gameobj || !mesh)
821         {
822                 std::cout << "warning: invalid object, mesh will not be replaced" << std::endl;
823                 return;
824         }
825
826         gameobj->RemoveMeshes();
827         gameobj->AddMesh(mesh);
828         
829         if (gameobj->m_isDeformable)
830         {
831                 BL_DeformableGameObject* newobj = static_cast<BL_DeformableGameObject*>( gameobj );
832                 
833                 if (newobj->m_pDeformer)
834                 {
835                         delete newobj->m_pDeformer;
836                         newobj->m_pDeformer = NULL;
837                 }
838
839                 if (mesh->m_class == 1) 
840                 {
841                         // we must create a new deformer but which one?
842                         KX_GameObject* parentobj = newobj->GetParent();
843                         // this always return the original game object (also for replicate)
844                         Object* blendobj = newobj->GetBlenderObject();
845                         // object that owns the new mesh
846                         Object* oldblendobj = static_cast<struct Object*>(m_logicmgr->FindBlendObjByGameMeshName(mesh->GetName()));
847                         Mesh* blendmesh = mesh->GetMesh();
848
849                         bool bHasShapeKey = blendmesh->key != NULL && blendmesh->key->type==KEY_RELATIVE;
850                         bool bHasDvert = blendmesh->dvert != NULL;
851                         bool bHasArmature = 
852                                 parentobj &&                                                            // current parent is armature
853                                 parentobj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE &&
854                                 oldblendobj &&                                                          // needed for mesh deform
855                                 blendobj->parent &&                                                     // original object had armature (not sure this test is needed)
856                                 blendobj->parent->type == OB_ARMATURE && 
857                                 blendobj->partype==PARSKEL && 
858                                 blendmesh->dvert!=NULL;                                         // mesh has vertex group
859                         bool releaseParent = true;
860
861                         if (bHasShapeKey)
862                         {
863                                 BL_ShapeDeformer* shapeDeformer;
864                                 if (bHasArmature) 
865                                 {
866                                         shapeDeformer = new BL_ShapeDeformer(
867                                                 newobj,
868                                                 oldblendobj, blendobj,
869                                                 static_cast<BL_SkinMeshObject*>(mesh),
870                                                 true,
871                                                 static_cast<BL_ArmatureObject*>( parentobj )
872                                         );
873                                         releaseParent= false;
874                                         shapeDeformer->LoadShapeDrivers(blendobj->parent);
875                                 }
876                                 else
877                                 {
878                                         shapeDeformer = new BL_ShapeDeformer(
879                                                 newobj,
880                                                 oldblendobj, blendobj,
881                                                 static_cast<BL_SkinMeshObject*>(mesh),
882                                                 false,
883                                                 NULL
884                                         );
885                                 }
886                                 newobj->m_pDeformer = shapeDeformer;
887                         }
888                         else if (bHasArmature) 
889                         {
890                                 BL_SkinDeformer* skinDeformer = new BL_SkinDeformer(
891                                         oldblendobj, blendobj,
892                                         static_cast<BL_SkinMeshObject*>(mesh),
893                                         true,
894                                         static_cast<BL_ArmatureObject*>( parentobj )
895                                 );
896                                 releaseParent= false;
897                                 newobj->m_pDeformer = skinDeformer;
898                         }
899                         else if (bHasDvert)
900                         {
901                                 BL_MeshDeformer* meshdeformer = new BL_MeshDeformer(
902                                         oldblendobj, static_cast<BL_SkinMeshObject*>(mesh)
903                                 );
904                                 newobj->m_pDeformer = meshdeformer;
905                         }
906
907                         // release parent reference if its not being used 
908                         if( releaseParent && parentobj)
909                                 parentobj->Release();
910                 }
911         }
912         gameobj->Bucketize();
913 }
914
915
916
917 MT_CmMatrix4x4& KX_Scene::GetViewMatrix()
918 {
919         MT_Scalar cammat[16];
920         m_active_camera->GetWorldToCamera().getValue(cammat);
921         m_viewmat = cammat;
922         return m_viewmat;
923 }
924
925
926
927 MT_CmMatrix4x4& KX_Scene::GetProjectionMatrix()
928 {
929         return m_projectionmat;
930 }
931
932
933 KX_Camera* KX_Scene::FindCamera(KX_Camera* cam)
934 {
935         list<KX_Camera*>::iterator it = m_cameras.begin();
936
937         while ( (it != m_cameras.end()) 
938                         && ((*it) != cam) ) {
939           it++;
940         }
941
942         return ((it == m_cameras.end()) ? NULL : (*it));
943 }
944
945
946 KX_Camera* KX_Scene::FindCamera(STR_String& name)
947 {
948         list<KX_Camera*>::iterator it = m_cameras.begin();
949
950         while ( (it != m_cameras.end()) 
951                         && ((*it)->GetName() != name) ) {
952           it++;
953         }
954
955         return ((it == m_cameras.end()) ? NULL : (*it));
956 }
957
958 void KX_Scene::AddCamera(KX_Camera* cam)
959 {
960         if (!FindCamera(cam))
961                 m_cameras.push_back(cam);
962 }
963
964
965 KX_Camera* KX_Scene::GetActiveCamera()
966 {       
967         // NULL if not defined
968         return m_active_camera;
969 }
970
971
972 void KX_Scene::SetActiveCamera(KX_Camera* cam)
973 {
974         // only set if the cam is in the active list? Or add it otherwise?
975         if (!FindCamera(cam)){
976                 AddCamera(cam);
977                 if (cam) std::cout << "Added cam " << cam->GetName() << std::endl;
978         } 
979
980         m_active_camera = cam;
981 }
982
983 void KX_Scene::SetCameraOnTop(KX_Camera* cam)
984 {
985         if (!FindCamera(cam)){
986                 // adding is always done at the back, so that's all that needs to be done
987                 AddCamera(cam);
988                 if (cam) std::cout << "Added cam " << cam->GetName() << std::endl;
989         } else {
990                 m_cameras.remove(cam);
991                 m_cameras.push_back(cam);
992         }
993 }
994
995
996 void KX_Scene::UpdateMeshTransformations()
997 {
998         // do this incrementally in the future
999         for (int i = 0; i < m_objectlist->GetCount(); i++)
1000         {
1001                 KX_GameObject* gameobj = (KX_GameObject*)m_objectlist->GetValue(i);
1002                 gameobj->GetOpenGLMatrix();
1003 //              gameobj->UpdateNonDynas();
1004         }
1005 }
1006
1007 void KX_Scene::MarkVisible(SG_Tree *node, RAS_IRasterizer* rasty, KX_Camera* cam)
1008 {
1009         int intersect = KX_Camera::INTERSECT;
1010         KX_GameObject *gameobj = node->Client()?(KX_GameObject*) node->Client()->GetSGClientObject():NULL;
1011         bool dotest = (gameobj && gameobj->GetVisible()) || node->Left() || node->Right();
1012         
1013         /* If the camera is inside the box, assume intersect. */
1014         if (dotest && !node->inside( cam->NodeGetWorldPosition()))
1015         {
1016                 MT_Scalar radius = node->Radius();
1017                 MT_Point3 center = node->Center();
1018                 
1019                 intersect =  cam->SphereInsideFrustum(center, radius); 
1020                 
1021                 if (intersect == KX_Camera::INTERSECT)
1022                 {
1023                         MT_Point3 box[8];
1024                         node->get(box);
1025                         intersect = cam->BoxInsideFrustum(box);
1026                 }
1027         }
1028
1029         switch (intersect)
1030         {
1031                 case KX_Camera::OUTSIDE:
1032                         MarkSubTreeVisible(node, rasty, false, cam);
1033                         break;
1034                 case KX_Camera::INTERSECT:
1035                         if (gameobj)
1036                                 MarkVisible(rasty, gameobj,cam);
1037                         if (node->Left())
1038                                 MarkVisible(node->Left(), rasty,cam);
1039                         if (node->Right())
1040                                 MarkVisible(node->Right(), rasty,cam);
1041                         break;
1042                 case KX_Camera::INSIDE:
1043                         MarkSubTreeVisible(node, rasty, true,cam);
1044                         break;
1045         }
1046 }
1047
1048 void KX_Scene::MarkSubTreeVisible(SG_Tree *node, RAS_IRasterizer* rasty, bool visible,KX_Camera* cam)
1049 {
1050         if (node->Client())
1051         {
1052                 KX_GameObject *gameobj = (KX_GameObject*) node->Client()->GetSGClientObject();
1053                 if (gameobj->GetVisible())
1054                 {
1055                         if (visible)
1056                         {
1057                                 int nummeshes = gameobj->GetMeshCount();
1058                                 MT_Transform t( cam->GetWorldToCamera() * gameobj->GetSGNode()->GetWorldTransform());
1059         
1060                                 
1061                                 for (int m=0;m<nummeshes;m++)
1062                                 {
1063                                         // this adds the vertices to the display list
1064                                         (gameobj->GetMesh(m))->SchedulePolygons(t, rasty->GetDrawingMode());
1065                                 }
1066                         }
1067                         gameobj->MarkVisible(visible);
1068                 }
1069         }
1070         if (node->Left())
1071                 MarkSubTreeVisible(node->Left(), rasty, visible,cam);
1072         if (node->Right())
1073                 MarkSubTreeVisible(node->Right(), rasty, visible,cam);
1074 }
1075
1076 void KX_Scene::MarkVisible(RAS_IRasterizer* rasty, KX_GameObject* gameobj,KX_Camera*  cam)
1077 {
1078         // User (Python/Actuator) has forced object invisible...
1079         if (!gameobj->GetVisible())
1080                 return;
1081         // If Frustum culling is off, the object is always visible.
1082         bool vis = !cam->GetFrustumCulling();
1083         
1084         // If the camera is inside this node, then the object is visible.
1085         if (!vis)
1086         {
1087                 vis = gameobj->GetSGNode()->inside( GetActiveCamera()->GetCameraLocation() );
1088         }
1089                 
1090         // Test the object's bound sphere against the view frustum.
1091         if (!vis)
1092         {
1093                 MT_Vector3 scale = gameobj->GetSGNode()->GetWorldScaling();
1094                 MT_Scalar radius = fabs(scale[scale.closestAxis()] * gameobj->GetSGNode()->Radius());
1095                 switch (cam->SphereInsideFrustum(gameobj->NodeGetWorldPosition(), radius))
1096                 {
1097                         case KX_Camera::INSIDE:
1098                                 vis = true;
1099                                 break;
1100                         case KX_Camera::OUTSIDE:
1101                                 vis = false;
1102                                 break;
1103                         case KX_Camera::INTERSECT:
1104                                 // Test the object's bound box against the view frustum.
1105                                 MT_Point3 box[8];
1106                                 gameobj->GetSGNode()->getBBox(box); 
1107                                 vis = cam->BoxInsideFrustum(box) != KX_Camera::OUTSIDE;
1108                                 break;
1109                 }
1110         }
1111         
1112         if (vis)
1113         {
1114                 int nummeshes = gameobj->GetMeshCount();
1115                 MT_Transform t(cam->GetWorldToCamera() * gameobj->GetSGNode()->GetWorldTransform());
1116                 
1117                 for (int m=0;m<nummeshes;m++)
1118                 {
1119                         // this adds the vertices to the display list
1120                         (gameobj->GetMesh(m))->SchedulePolygons(t, rasty->GetDrawingMode());
1121                 }
1122                 // Visibility/ non-visibility are marked
1123                 // elsewhere now.
1124                 gameobj->MarkVisible();
1125         } else {
1126                 gameobj->MarkVisible(false);
1127         }
1128 }
1129
1130 void KX_Scene::CalculateVisibleMeshes(RAS_IRasterizer* rasty,KX_Camera* cam)
1131 {
1132 // FIXME: When tree is operational
1133 #if 1
1134         // do this incrementally in the future
1135         for (int i = 0; i < m_objectlist->GetCount(); i++)
1136         {
1137                 MarkVisible(rasty, static_cast<KX_GameObject*>(m_objectlist->GetValue(i)), cam);
1138         }
1139 #else
1140         if (cam->GetFrustumCulling())
1141                 MarkVisible(m_objecttree, rasty, cam);
1142         else
1143                 MarkSubTreeVisible(m_objecttree, rasty, true, cam);
1144 #endif
1145 }
1146
1147 // logic stuff
1148 void KX_Scene::LogicBeginFrame(double curtime)
1149 {
1150         // have a look at temp objects ...
1151         int lastobj = m_tempObjectList->GetCount() - 1;
1152         
1153         for (int i = lastobj; i >= 0; i--)
1154         {
1155                 CValue* objval = m_tempObjectList->GetValue(i);
1156                 CFloatValue* propval = (CFloatValue*) objval->GetProperty("::timebomb");
1157                 
1158                 if (propval)
1159                 {
1160                         float timeleft = propval->GetNumber() - 1.0/KX_KetsjiEngine::GetTicRate();
1161                         
1162                         if (timeleft > 0)
1163                         {
1164                                 propval->SetFloat(timeleft);
1165                         }
1166                         else
1167                         {
1168                                 DelayedRemoveObject(objval);
1169                                 // remove obj
1170                         }
1171                 }
1172                 else
1173                 {
1174                         // all object is the tempObjectList should have a clock
1175                 }
1176         }
1177         m_logicmgr->BeginFrame(curtime, 1.0/KX_KetsjiEngine::GetTicRate());
1178 }
1179
1180
1181
1182 void KX_Scene::LogicUpdateFrame(double curtime, bool frame)
1183 {
1184         m_logicmgr->UpdateFrame(curtime, frame);
1185 }
1186
1187
1188
1189 void KX_Scene::LogicEndFrame()
1190 {
1191         m_logicmgr->EndFrame();
1192         int numobj = m_euthanasyobjects->GetCount();
1193         int i;
1194         for (i = numobj - 1; i >= 0; i--)
1195         {
1196                 KX_GameObject* gameobj = (KX_GameObject*)m_euthanasyobjects->GetValue(i);
1197                 // KX_Scene::RemoveObject will also remove the object from this list
1198                 // that's why we start from the end
1199                 this->RemoveObject(gameobj);
1200         }
1201
1202         numobj= m_delayReleaseObjects->GetCount();
1203         for (i = numobj-1;i>=0;i--)
1204         {
1205                 KX_GameObject* gameobj = (KX_GameObject*)m_delayReleaseObjects->GetValue(i);
1206                 // This list is not for object removal, but just object release
1207                 gameobj->Release();
1208         }
1209         // empty the list as we have removed all references
1210         m_delayReleaseObjects->Resize(0);       
1211 }
1212
1213
1214
1215 /**
1216   * UpdateParents: SceneGraph transformation update.
1217   */
1218 void KX_Scene::UpdateParents(double curtime)
1219 {
1220 //      int numrootobjects = GetRootParentList()->GetCount();
1221
1222         for (int i=0; i<GetRootParentList()->GetCount(); i++)
1223         {
1224                 KX_GameObject* parentobj = (KX_GameObject*)GetRootParentList()->GetValue(i);
1225                 parentobj->NodeUpdateGS(curtime,true);
1226         }
1227 }
1228
1229
1230
1231 RAS_MaterialBucket* KX_Scene::FindBucket(class RAS_IPolyMaterial* polymat, bool &bucketCreated)
1232 {
1233         return m_bucketmanager->RAS_BucketManagerFindBucket(polymat, bucketCreated);
1234 }
1235
1236
1237
1238 void KX_Scene::RenderBuckets(const MT_Transform & cameratransform,
1239                                                          class RAS_IRasterizer* rasty,
1240                                                          class RAS_IRenderTools* rendertools)
1241 {
1242         m_bucketmanager->Renderbuckets(cameratransform,rasty,rendertools);
1243 }
1244
1245
1246
1247 void KX_Scene::UpdateObjectActivity(void) 
1248 {
1249         if (m_activity_culling) {
1250                 /* determine the activity criterium and set objects accordingly */
1251                 int i=0;
1252                 
1253                 MT_Point3 camloc = GetActiveCamera()->NodeGetWorldPosition(); //GetCameraLocation();
1254                 
1255                 for (i=0;i<GetObjectList()->GetCount();i++)
1256                 {
1257                         KX_GameObject* ob = (KX_GameObject*) GetObjectList()->GetValue(i);
1258                         
1259                         if (!ob->GetIgnoreActivityCulling()) {
1260                                 /* Simple test: more than 10 away from the camera, count
1261                                  * Manhattan distance. */
1262                                 MT_Point3 obpos = ob->NodeGetWorldPosition();
1263                                 
1264                                 if ( (fabs(camloc[0] - obpos[0]) > m_activity_box_radius)
1265                                          || (fabs(camloc[1] - obpos[1]) > m_activity_box_radius)
1266                                          || (fabs(camloc[2] - obpos[2]) > m_activity_box_radius) )
1267                                 {                       
1268                                         ob->Suspend();
1269                                 } else {
1270                                         ob->Resume();
1271                                 }
1272                         }
1273                 }               
1274         }
1275 }
1276
1277 void KX_Scene::SetActivityCullingRadius(float f)
1278 {
1279         if (f < 0.5)
1280                 f = 0.5;
1281         m_activity_box_radius = f;
1282 }
1283         
1284 NG_NetworkDeviceInterface* KX_Scene::GetNetworkDeviceInterface()
1285 {
1286         return m_networkDeviceInterface;
1287 }
1288
1289 NG_NetworkScene* KX_Scene::GetNetworkScene()
1290 {
1291         return m_networkScene;
1292 }
1293
1294 void KX_Scene::SetNetworkDeviceInterface(NG_NetworkDeviceInterface* newInterface)
1295 {
1296         m_networkDeviceInterface = newInterface;
1297 }
1298
1299 void KX_Scene::SetNetworkScene(NG_NetworkScene *newScene)
1300 {
1301         m_networkScene = newScene;
1302 }
1303
1304
1305 void    KX_Scene::SetGravity(const MT_Vector3& gravity)
1306 {
1307         GetPhysicsEnvironment()->setGravity(gravity[0],gravity[1],gravity[2]);
1308 }
1309
1310 void KX_Scene::SetNodeTree(SG_Tree* root)
1311 {
1312         m_objecttree = root;
1313 }
1314
1315 void KX_Scene::SetSceneConverter(class KX_BlenderSceneConverter* sceneConverter)
1316 {
1317         m_sceneConverter = sceneConverter;
1318 }
1319
1320 void KX_Scene::SetPhysicsEnvironment(class PHY_IPhysicsEnvironment* physEnv)
1321 {
1322         m_physicsEnvironment = physEnv;
1323
1324         KX_TouchEventManager* touchmgr = new KX_TouchEventManager(m_logicmgr, physEnv);
1325         m_logicmgr->RegisterEventManager(touchmgr);
1326         return;
1327 }
1328  
1329 void KX_Scene::setSuspendedTime(double suspendedtime)
1330 {
1331         m_suspendedtime = suspendedtime;
1332 }
1333 double KX_Scene::getSuspendedTime()
1334 {
1335         return m_suspendedtime;
1336 }
1337 void KX_Scene::setSuspendedDelta(double suspendeddelta)
1338 {
1339         m_suspendeddelta = suspendeddelta;
1340 }
1341 double KX_Scene::getSuspendedDelta()
1342 {
1343         return m_suspendeddelta;
1344 }
1345
1346 //----------------------------------------------------------------------------
1347 //Python
1348
1349 PyMethodDef KX_Scene::Methods[] = {
1350         KX_PYMETHODTABLE(KX_Scene, getLightList),
1351         KX_PYMETHODTABLE(KX_Scene, getObjectList),
1352         KX_PYMETHODTABLE(KX_Scene, getName),
1353         
1354         {NULL,NULL} //Sentinel
1355 };
1356
1357 PyTypeObject KX_Scene::Type = {
1358         PyObject_HEAD_INIT(&PyType_Type)
1359                 0,
1360                 "KX_Scene",
1361                 sizeof(KX_Scene),
1362                 0,
1363                 PyDestructor,
1364                 0,
1365                 __getattr,
1366                 __setattr,
1367                 0, //&MyPyCompare,
1368                 __repr,
1369                 0, //&cvalue_as_number,
1370                 0,
1371                 0,
1372                 0,
1373                 0, 0, 0, 0, 0, 0
1374 };
1375
1376 PyParentObject KX_Scene::Parents[] = {
1377         &KX_Scene::Type,
1378                 &CValue::Type,
1379                 NULL
1380 };
1381
1382 PyObject* KX_Scene::_getattr(const STR_String& attr)
1383 {
1384         if (attr == "name")
1385                 return PyString_FromString(GetName());
1386         
1387         if (attr == "active_camera")
1388         {
1389                 KX_Camera *camera = GetActiveCamera();
1390                 camera->AddRef();
1391                 return (PyObject*) camera;
1392         }
1393         
1394         if (attr == "suspended")
1395                 return PyInt_FromLong(m_suspend);
1396         
1397         if (attr == "activity_culling")
1398                 return PyInt_FromLong(m_activity_culling);
1399         
1400         if (attr == "activity_culling_radius")
1401                 return PyFloat_FromDouble(m_activity_box_radius);
1402         
1403         PyObject* value = PyDict_GetItemString(m_attrlist, const_cast<char *>(attr.ReadPtr()));
1404         if (value)
1405         {
1406                 Py_INCREF(value);
1407                 return value;
1408         }
1409         
1410         _getattr_up(PyObjectPlus);
1411 }
1412
1413 int KX_Scene::_delattr(const STR_String &attr)
1414 {
1415         PyDict_DelItemString(m_attrlist, const_cast<char *>(attr.ReadPtr()));
1416         return 0;
1417 }
1418
1419 int KX_Scene::_setattr(const STR_String &attr, PyObject *pyvalue)
1420 {
1421
1422         if (!PyDict_SetItemString(m_attrlist, const_cast<char *>(attr.ReadPtr()), pyvalue))
1423                 return 0;
1424
1425         return PyObjectPlus::_setattr(attr, pyvalue);
1426 }
1427
1428 KX_PYMETHODDEF_DOC(KX_Scene, getLightList,
1429 "getLightList() -> list [KX_Light]\n"
1430 "Returns a list of all lights in the scene.\n"
1431 )
1432 {
1433         m_lightlist->AddRef();
1434         return (PyObject*) m_lightlist;
1435 }
1436
1437 KX_PYMETHODDEF_DOC(KX_Scene, getObjectList,
1438 "getObjectList() -> list [KX_GameObject]\n"
1439 "Returns a list of all game objects in the scene.\n"
1440 )
1441 {
1442         m_objectlist->AddRef();
1443         return (PyObject*) m_objectlist;
1444 }
1445
1446 KX_PYMETHODDEF_DOC(KX_Scene, getName,
1447 "getName() -> string\n"
1448 "Returns the name of the scene.\n"
1449 )
1450 {
1451         return PyString_FromString(GetName());
1452 }