MERGING TRUNK
[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                 m_logicmgr->RemoveController(*itc);
758         }
759
760         SCA_ActuatorList& actuators = newobj->GetActuators();
761         for (SCA_ActuatorList::iterator ita = actuators.begin();
762                  !(ita==actuators.end());ita++)
763         {
764                 m_logicmgr->RemoveDestroyedActuator(*ita);
765         }
766         // the sensors/controllers/actuators must also be released, this is done in ~SCA_IObject
767
768         // now remove the timer properties from the time manager
769         int numprops = newobj->GetPropertyCount();
770
771         for (int i = 0; i < numprops; i++)
772         {
773                 CValue* propval = newobj->GetProperty(i);
774                 if (propval->GetProperty("timer"))
775                 {
776                         m_timemgr->RemoveTimeProperty(propval);
777                 }
778         }
779         
780         newobj->RemoveMeshes();
781         ret = 1;
782         if (m_objectlist->RemoveValue(newobj))
783                 ret = newobj->Release();
784         if (m_tempObjectList->RemoveValue(newobj))
785                 ret = newobj->Release();
786         if (m_parentlist->RemoveValue(newobj))
787                 ret = newobj->Release();
788         if (m_inactivelist->RemoveValue(newobj))
789                 ret = newobj->Release();
790         if (m_euthanasyobjects->RemoveValue(newobj))
791                 ret = newobj->Release();
792                 
793         if (newobj == m_active_camera)
794         {
795                 //no AddRef done on m_active_camera so no Release
796                 //m_active_camera->Release();
797                 m_active_camera = NULL;
798         }
799         // in case this is a camera
800         m_cameras.remove((KX_Camera*)newobj);
801
802         if (m_sceneConverter)
803                 m_sceneConverter->UnregisterGameObject(newobj);
804         // return value will be 0 if the object is actually deleted (all reference gone)
805         return ret;
806 }
807
808
809
810 void KX_Scene::ReplaceMesh(class CValue* obj,void* meshobj)
811 {
812         KX_GameObject* gameobj = static_cast<KX_GameObject*>(obj);
813         RAS_MeshObject* mesh = static_cast<RAS_MeshObject*>(meshobj);
814
815         if(!gameobj || !mesh)
816         {
817                 std::cout << "warning: invalid object, mesh will not be replaced" << std::endl;
818                 return;
819         }
820
821         gameobj->RemoveMeshes();
822         gameobj->AddMesh(mesh);
823         
824         if (gameobj->m_isDeformable)
825         {
826                 BL_DeformableGameObject* newobj = static_cast<BL_DeformableGameObject*>( gameobj );
827                 
828                 if (newobj->m_pDeformer)
829                 {
830                         delete newobj->m_pDeformer;
831                         newobj->m_pDeformer = NULL;
832                 }
833
834                 if (mesh->m_class == 1) 
835                 {
836                         // we must create a new deformer but which one?
837                         KX_GameObject* parentobj = newobj->GetParent();
838                         // this always return the original game object (also for replicate)
839                         Object* blendobj = newobj->GetBlenderObject();
840                         // object that owns the new mesh
841                         Object* oldblendobj = static_cast<struct Object*>(m_logicmgr->FindBlendObjByGameMeshName(mesh->GetName()));
842                         Mesh* blendmesh = mesh->GetMesh();
843
844                         bool bHasShapeKey = blendmesh->key != NULL && blendmesh->key->type==KEY_RELATIVE;
845                         bool bHasDvert = blendmesh->dvert != NULL;
846                         bool bHasArmature = 
847                                 parentobj &&                                                            // current parent is armature
848                                 parentobj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE &&
849                                 oldblendobj &&                                                          // needed for mesh deform
850                                 blendobj->parent &&                                                     // original object had armature (not sure this test is needed)
851                                 blendobj->parent->type == OB_ARMATURE && 
852                                 blendobj->partype==PARSKEL && 
853                                 blendmesh->dvert!=NULL;                                         // mesh has vertex group
854                         bool releaseParent = true;
855
856                         if (bHasShapeKey)
857                         {
858                                 BL_ShapeDeformer* shapeDeformer;
859                                 if (bHasArmature) 
860                                 {
861                                         shapeDeformer = new BL_ShapeDeformer(
862                                                 newobj,
863                                                 oldblendobj, blendobj,
864                                                 static_cast<BL_SkinMeshObject*>(mesh),
865                                                 true,
866                                                 static_cast<BL_ArmatureObject*>( parentobj )
867                                         );
868                                         releaseParent= false;
869                                 }
870                                 else
871                                 {
872                                         shapeDeformer = new BL_ShapeDeformer(
873                                                 newobj,
874                                                 oldblendobj, blendobj,
875                                                 static_cast<BL_SkinMeshObject*>(mesh),
876                                                 false,
877                                                 NULL
878                                         );
879                                 }
880                                 newobj->m_pDeformer = shapeDeformer;
881                         }
882                         else if (bHasArmature) 
883                         {
884                                 BL_SkinDeformer* skinDeformer = new BL_SkinDeformer(
885                                         oldblendobj, blendobj,
886                                         static_cast<BL_SkinMeshObject*>(mesh),
887                                         true,
888                                         static_cast<BL_ArmatureObject*>( parentobj )
889                                 );
890                                 releaseParent= false;
891                                 newobj->m_pDeformer = skinDeformer;
892                         }
893                         else if (bHasDvert)
894                         {
895                                 BL_MeshDeformer* meshdeformer = new BL_MeshDeformer(
896                                         oldblendobj, static_cast<BL_SkinMeshObject*>(mesh)
897                                 );
898                                 newobj->m_pDeformer = meshdeformer;
899                         }
900
901                         // release parent reference if its not being used 
902                         if( releaseParent && parentobj)
903                                 parentobj->Release();
904                 }
905         }
906         gameobj->Bucketize();
907 }
908
909
910
911 MT_CmMatrix4x4& KX_Scene::GetViewMatrix()
912 {
913         MT_Scalar cammat[16];
914         m_active_camera->GetWorldToCamera().getValue(cammat);
915         m_viewmat = cammat;
916         return m_viewmat;
917 }
918
919
920
921 MT_CmMatrix4x4& KX_Scene::GetProjectionMatrix()
922 {
923         return m_projectionmat;
924 }
925
926
927 KX_Camera* KX_Scene::FindCamera(KX_Camera* cam)
928 {
929         list<KX_Camera*>::iterator it = m_cameras.begin();
930
931         while ( (it != m_cameras.end()) 
932                         && ((*it) != cam) ) {
933           it++;
934         }
935
936         return ((it == m_cameras.end()) ? NULL : (*it));
937 }
938
939
940 KX_Camera* KX_Scene::FindCamera(STR_String& name)
941 {
942         list<KX_Camera*>::iterator it = m_cameras.begin();
943
944         while ( (it != m_cameras.end()) 
945                         && ((*it)->GetName() != name) ) {
946           it++;
947         }
948
949         return ((it == m_cameras.end()) ? NULL : (*it));
950 }
951
952 void KX_Scene::AddCamera(KX_Camera* cam)
953 {
954         if (!FindCamera(cam))
955                 m_cameras.push_back(cam);
956 }
957
958
959 KX_Camera* KX_Scene::GetActiveCamera()
960 {       
961         // NULL if not defined
962         return m_active_camera;
963 }
964
965
966 void KX_Scene::SetActiveCamera(KX_Camera* cam)
967 {
968         // only set if the cam is in the active list? Or add it otherwise?
969         if (!FindCamera(cam)){
970                 AddCamera(cam);
971                 if (cam) std::cout << "Added cam " << cam->GetName() << std::endl;
972         } 
973
974         m_active_camera = cam;
975 }
976
977 void KX_Scene::SetCameraOnTop(KX_Camera* cam)
978 {
979         if (!FindCamera(cam)){
980                 // adding is always done at the back, so that's all that needs to be done
981                 AddCamera(cam);
982                 if (cam) std::cout << "Added cam " << cam->GetName() << std::endl;
983         } else {
984                 m_cameras.remove(cam);
985                 m_cameras.push_back(cam);
986         }
987 }
988
989
990 void KX_Scene::UpdateMeshTransformations()
991 {
992         // do this incrementally in the future
993         for (int i = 0; i < m_objectlist->GetCount(); i++)
994         {
995                 KX_GameObject* gameobj = (KX_GameObject*)m_objectlist->GetValue(i);
996                 gameobj->GetOpenGLMatrix();
997 //              gameobj->UpdateNonDynas();
998         }
999 }
1000
1001 void KX_Scene::MarkVisible(SG_Tree *node, RAS_IRasterizer* rasty, KX_Camera* cam)
1002 {
1003         int intersect = KX_Camera::INTERSECT;
1004         KX_GameObject *gameobj = node->Client()?(KX_GameObject*) node->Client()->GetSGClientObject():NULL;
1005         bool dotest = (gameobj && gameobj->GetVisible()) || node->Left() || node->Right();
1006         
1007         /* If the camera is inside the box, assume intersect. */
1008         if (dotest && !node->inside( cam->NodeGetWorldPosition()))
1009         {
1010                 MT_Scalar radius = node->Radius();
1011                 MT_Point3 center = node->Center();
1012                 
1013                 intersect =  cam->SphereInsideFrustum(center, radius); 
1014                 
1015                 if (intersect == KX_Camera::INTERSECT)
1016                 {
1017                         MT_Point3 box[8];
1018                         node->get(box);
1019                         intersect = cam->BoxInsideFrustum(box);
1020                 }
1021         }
1022
1023         switch (intersect)
1024         {
1025                 case KX_Camera::OUTSIDE:
1026                         MarkSubTreeVisible(node, rasty, false, cam);
1027                         break;
1028                 case KX_Camera::INTERSECT:
1029                         if (gameobj)
1030                                 MarkVisible(rasty, gameobj,cam);
1031                         if (node->Left())
1032                                 MarkVisible(node->Left(), rasty,cam);
1033                         if (node->Right())
1034                                 MarkVisible(node->Right(), rasty,cam);
1035                         break;
1036                 case KX_Camera::INSIDE:
1037                         MarkSubTreeVisible(node, rasty, true,cam);
1038                         break;
1039         }
1040 }
1041
1042 void KX_Scene::MarkSubTreeVisible(SG_Tree *node, RAS_IRasterizer* rasty, bool visible,KX_Camera* cam)
1043 {
1044         if (node->Client())
1045         {
1046                 KX_GameObject *gameobj = (KX_GameObject*) node->Client()->GetSGClientObject();
1047                 if (gameobj->GetVisible())
1048                 {
1049                         if (visible)
1050                         {
1051                                 int nummeshes = gameobj->GetMeshCount();
1052                                 MT_Transform t( cam->GetWorldToCamera() * gameobj->GetSGNode()->GetWorldTransform());
1053         
1054                                 
1055                                 for (int m=0;m<nummeshes;m++)
1056                                 {
1057                                         // this adds the vertices to the display list
1058                                         (gameobj->GetMesh(m))->SchedulePolygons(t, rasty->GetDrawingMode());
1059                                 }
1060                         }
1061                         gameobj->MarkVisible(visible);
1062                 }
1063         }
1064         if (node->Left())
1065                 MarkSubTreeVisible(node->Left(), rasty, visible,cam);
1066         if (node->Right())
1067                 MarkSubTreeVisible(node->Right(), rasty, visible,cam);
1068 }
1069
1070 void KX_Scene::MarkVisible(RAS_IRasterizer* rasty, KX_GameObject* gameobj,KX_Camera*  cam)
1071 {
1072         // User (Python/Actuator) has forced object invisible...
1073         if (!gameobj->GetVisible())
1074                 return;
1075         // If Frustum culling is off, the object is always visible.
1076         bool vis = !cam->GetFrustumCulling();
1077         
1078         // If the camera is inside this node, then the object is visible.
1079         if (!vis)
1080         {
1081                 vis = gameobj->GetSGNode()->inside( GetActiveCamera()->GetCameraLocation() );
1082         }
1083                 
1084         // Test the object's bound sphere against the view frustum.
1085         if (!vis)
1086         {
1087                 MT_Vector3 scale = gameobj->GetSGNode()->GetWorldScaling();
1088                 MT_Scalar radius = fabs(scale[scale.closestAxis()] * gameobj->GetSGNode()->Radius());
1089                 switch (cam->SphereInsideFrustum(gameobj->NodeGetWorldPosition(), radius))
1090                 {
1091                         case KX_Camera::INSIDE:
1092                                 vis = true;
1093                                 break;
1094                         case KX_Camera::OUTSIDE:
1095                                 vis = false;
1096                                 break;
1097                         case KX_Camera::INTERSECT:
1098                                 // Test the object's bound box against the view frustum.
1099                                 MT_Point3 box[8];
1100                                 gameobj->GetSGNode()->getBBox(box); 
1101                                 vis = cam->BoxInsideFrustum(box) != KX_Camera::OUTSIDE;
1102                                 break;
1103                 }
1104         }
1105         
1106         if (vis)
1107         {
1108                 int nummeshes = gameobj->GetMeshCount();
1109                 MT_Transform t(cam->GetWorldToCamera() * gameobj->GetSGNode()->GetWorldTransform());
1110                 
1111                 for (int m=0;m<nummeshes;m++)
1112                 {
1113                         // this adds the vertices to the display list
1114                         (gameobj->GetMesh(m))->SchedulePolygons(t, rasty->GetDrawingMode());
1115                 }
1116                 // Visibility/ non-visibility are marked
1117                 // elsewhere now.
1118                 gameobj->MarkVisible();
1119         } else {
1120                 gameobj->MarkVisible(false);
1121         }
1122 }
1123
1124 void KX_Scene::CalculateVisibleMeshes(RAS_IRasterizer* rasty,KX_Camera* cam)
1125 {
1126 // FIXME: When tree is operational
1127 #if 1
1128         // do this incrementally in the future
1129         for (int i = 0; i < m_objectlist->GetCount(); i++)
1130         {
1131                 MarkVisible(rasty, static_cast<KX_GameObject*>(m_objectlist->GetValue(i)), cam);
1132         }
1133 #else
1134         if (cam->GetFrustumCulling())
1135                 MarkVisible(m_objecttree, rasty, cam);
1136         else
1137                 MarkSubTreeVisible(m_objecttree, rasty, true, cam);
1138 #endif
1139 }
1140
1141 // logic stuff
1142 void KX_Scene::LogicBeginFrame(double curtime)
1143 {
1144         // have a look at temp objects ...
1145         int lastobj = m_tempObjectList->GetCount() - 1;
1146         
1147         for (int i = lastobj; i >= 0; i--)
1148         {
1149                 CValue* objval = m_tempObjectList->GetValue(i);
1150                 CFloatValue* propval = (CFloatValue*) objval->GetProperty("::timebomb");
1151                 
1152                 if (propval)
1153                 {
1154                         float timeleft = propval->GetNumber() - 1.0/KX_KetsjiEngine::GetTicRate();
1155                         
1156                         if (timeleft > 0)
1157                         {
1158                                 propval->SetFloat(timeleft);
1159                         }
1160                         else
1161                         {
1162                                 DelayedRemoveObject(objval);
1163                                 // remove obj
1164                         }
1165                 }
1166                 else
1167                 {
1168                         // all object is the tempObjectList should have a clock
1169                 }
1170         }
1171         m_logicmgr->BeginFrame(curtime, 1.0/KX_KetsjiEngine::GetTicRate());
1172 }
1173
1174
1175
1176 void KX_Scene::LogicUpdateFrame(double curtime, bool frame)
1177 {
1178         m_logicmgr->UpdateFrame(curtime, frame);
1179 }
1180
1181
1182
1183 void KX_Scene::LogicEndFrame()
1184 {
1185         m_logicmgr->EndFrame();
1186         int numobj = m_euthanasyobjects->GetCount();
1187         int i;
1188         for (i = numobj - 1; i >= 0; i--)
1189         {
1190                 KX_GameObject* gameobj = (KX_GameObject*)m_euthanasyobjects->GetValue(i);
1191                 // KX_Scene::RemoveObject will also remove the object from this list
1192                 // that's why we start from the end
1193                 this->RemoveObject(gameobj);
1194         }
1195
1196         numobj= m_delayReleaseObjects->GetCount();
1197         for (i = numobj-1;i>=0;i--)
1198         {
1199                 KX_GameObject* gameobj = (KX_GameObject*)m_delayReleaseObjects->GetValue(i);
1200                 // This list is not for object removal, but just object release
1201                 gameobj->Release();
1202         }
1203         // empty the list as we have removed all references
1204         m_delayReleaseObjects->Resize(0);       
1205 }
1206
1207
1208
1209 /**
1210   * UpdateParents: SceneGraph transformation update.
1211   */
1212 void KX_Scene::UpdateParents(double curtime)
1213 {
1214 //      int numrootobjects = GetRootParentList()->GetCount();
1215
1216         for (int i=0; i<GetRootParentList()->GetCount(); i++)
1217         {
1218                 KX_GameObject* parentobj = (KX_GameObject*)GetRootParentList()->GetValue(i);
1219                 parentobj->NodeUpdateGS(curtime,true);
1220         }
1221 }
1222
1223
1224
1225 RAS_MaterialBucket* KX_Scene::FindBucket(class RAS_IPolyMaterial* polymat, bool &bucketCreated)
1226 {
1227         return m_bucketmanager->RAS_BucketManagerFindBucket(polymat, bucketCreated);
1228 }
1229
1230
1231
1232 void KX_Scene::RenderBuckets(const MT_Transform & cameratransform,
1233                                                          class RAS_IRasterizer* rasty,
1234                                                          class RAS_IRenderTools* rendertools)
1235 {
1236         m_bucketmanager->Renderbuckets(cameratransform,rasty,rendertools);
1237 }
1238
1239
1240
1241 void KX_Scene::UpdateObjectActivity(void) 
1242 {
1243         if (m_activity_culling) {
1244                 /* determine the activity criterium and set objects accordingly */
1245                 int i=0;
1246                 
1247                 MT_Point3 camloc = GetActiveCamera()->NodeGetWorldPosition(); //GetCameraLocation();
1248                 
1249                 for (i=0;i<GetObjectList()->GetCount();i++)
1250                 {
1251                         KX_GameObject* ob = (KX_GameObject*) GetObjectList()->GetValue(i);
1252                         
1253                         if (!ob->GetIgnoreActivityCulling()) {
1254                                 /* Simple test: more than 10 away from the camera, count
1255                                  * Manhattan distance. */
1256                                 MT_Point3 obpos = ob->NodeGetWorldPosition();
1257                                 
1258                                 if ( (fabs(camloc[0] - obpos[0]) > m_activity_box_radius)
1259                                          || (fabs(camloc[1] - obpos[1]) > m_activity_box_radius)
1260                                          || (fabs(camloc[2] - obpos[2]) > m_activity_box_radius) )
1261                                 {                       
1262                                         ob->Suspend();
1263                                 } else {
1264                                         ob->Resume();
1265                                 }
1266                         }
1267                 }               
1268         }
1269 }
1270
1271 void KX_Scene::SetActivityCullingRadius(float f)
1272 {
1273         if (f < 0.5)
1274                 f = 0.5;
1275         m_activity_box_radius = f;
1276 }
1277         
1278 NG_NetworkDeviceInterface* KX_Scene::GetNetworkDeviceInterface()
1279 {
1280         return m_networkDeviceInterface;
1281 }
1282
1283 NG_NetworkScene* KX_Scene::GetNetworkScene()
1284 {
1285         return m_networkScene;
1286 }
1287
1288 void KX_Scene::SetNetworkDeviceInterface(NG_NetworkDeviceInterface* newInterface)
1289 {
1290         m_networkDeviceInterface = newInterface;
1291 }
1292
1293 void KX_Scene::SetNetworkScene(NG_NetworkScene *newScene)
1294 {
1295         m_networkScene = newScene;
1296 }
1297
1298
1299 void    KX_Scene::SetGravity(const MT_Vector3& gravity)
1300 {
1301         GetPhysicsEnvironment()->setGravity(gravity[0],gravity[1],gravity[2]);
1302 }
1303
1304 void KX_Scene::SetNodeTree(SG_Tree* root)
1305 {
1306         m_objecttree = root;
1307 }
1308
1309 void KX_Scene::SetSceneConverter(class KX_BlenderSceneConverter* sceneConverter)
1310 {
1311         m_sceneConverter = sceneConverter;
1312 }
1313
1314 void KX_Scene::SetPhysicsEnvironment(class PHY_IPhysicsEnvironment* physEnv)
1315 {
1316         m_physicsEnvironment = physEnv;
1317
1318         KX_TouchEventManager* touchmgr = new KX_TouchEventManager(m_logicmgr, physEnv);
1319         m_logicmgr->RegisterEventManager(touchmgr);
1320         return;
1321 }
1322  
1323 void KX_Scene::setSuspendedTime(double suspendedtime)
1324 {
1325         m_suspendedtime = suspendedtime;
1326 }
1327 double KX_Scene::getSuspendedTime()
1328 {
1329         return m_suspendedtime;
1330 }
1331 void KX_Scene::setSuspendedDelta(double suspendeddelta)
1332 {
1333         m_suspendeddelta = suspendeddelta;
1334 }
1335 double KX_Scene::getSuspendedDelta()
1336 {
1337         return m_suspendeddelta;
1338 }
1339
1340 //----------------------------------------------------------------------------
1341 //Python
1342
1343 PyMethodDef KX_Scene::Methods[] = {
1344         KX_PYMETHODTABLE(KX_Scene, getLightList),
1345         KX_PYMETHODTABLE(KX_Scene, getObjectList),
1346         KX_PYMETHODTABLE(KX_Scene, getName),
1347         
1348         {NULL,NULL} //Sentinel
1349 };
1350
1351 PyTypeObject KX_Scene::Type = {
1352         PyObject_HEAD_INIT(&PyType_Type)
1353                 0,
1354                 "KX_Scene",
1355                 sizeof(KX_Scene),
1356                 0,
1357                 PyDestructor,
1358                 0,
1359                 __getattr,
1360                 __setattr,
1361                 0, //&MyPyCompare,
1362                 __repr,
1363                 0, //&cvalue_as_number,
1364                 0,
1365                 0,
1366                 0,
1367                 0, 0, 0, 0, 0, 0
1368 };
1369
1370 PyParentObject KX_Scene::Parents[] = {
1371         &KX_Scene::Type,
1372                 &CValue::Type,
1373                 NULL
1374 };
1375
1376 PyObject* KX_Scene::_getattr(const STR_String& attr)
1377 {
1378         if (attr == "name")
1379                 return PyString_FromString(GetName());
1380         
1381         if (attr == "active_camera")
1382         {
1383                 KX_Camera *camera = GetActiveCamera();
1384                 camera->AddRef();
1385                 return (PyObject*) camera;
1386         }
1387         
1388         if (attr == "suspended")
1389                 return PyInt_FromLong(m_suspend);
1390         
1391         if (attr == "activity_culling")
1392                 return PyInt_FromLong(m_activity_culling);
1393         
1394         if (attr == "activity_culling_radius")
1395                 return PyFloat_FromDouble(m_activity_box_radius);
1396         
1397         PyObject* value = PyDict_GetItemString(m_attrlist, const_cast<char *>(attr.ReadPtr()));
1398         if (value)
1399         {
1400                 Py_INCREF(value);
1401                 return value;
1402         }
1403         
1404         _getattr_up(PyObjectPlus);
1405 }
1406
1407 int KX_Scene::_delattr(const STR_String &attr)
1408 {
1409         PyDict_DelItemString(m_attrlist, const_cast<char *>(attr.ReadPtr()));
1410         return 0;
1411 }
1412
1413 int KX_Scene::_setattr(const STR_String &attr, PyObject *pyvalue)
1414 {
1415
1416         if (!PyDict_SetItemString(m_attrlist, const_cast<char *>(attr.ReadPtr()), pyvalue))
1417                 return 0;
1418
1419         return PyObjectPlus::_setattr(attr, pyvalue);
1420 }
1421
1422 KX_PYMETHODDEF_DOC(KX_Scene, getLightList,
1423 "getLightList() -> list [KX_Light]\n"
1424 "Returns a list of all lights in the scene.\n"
1425 )
1426 {
1427         m_lightlist->AddRef();
1428         return (PyObject*) m_lightlist;
1429 }
1430
1431 KX_PYMETHODDEF_DOC(KX_Scene, getObjectList,
1432 "getObjectList() -> list [KX_GameObject]\n"
1433 "Returns a list of all game objects in the scene.\n"
1434 )
1435 {
1436         m_objectlist->AddRef();
1437         return (PyObject*) m_objectlist;
1438 }
1439
1440 KX_PYMETHODDEF_DOC(KX_Scene, getName,
1441 "getName() -> string\n"
1442 "Returns the name of the scene.\n"
1443 )
1444 {
1445         return PyString_FromString(GetName());
1446 }