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