Merge of itasc branch. Project files, scons and cmake should be working. Makefile...
[blender.git] / source / gameengine / Ketsji / KX_Scene.cpp
index c0d8a70..140be3f 100644 (file)
 
 #include "KX_Scene.h"
 #include "MT_assert.h"
-#include "SND_Scene.h"
 #include "KX_KetsjiEngine.h"
 #include "KX_BlenderMaterial.h"
 #include "RAS_IPolygonMaterial.h"
 #include "ListValue.h"
 #include "SCA_LogicManager.h"
 #include "SCA_TimeEventManager.h"
-#include "SCA_AlwaysEventManager.h"
-#include "SCA_RandomEventManager.h"
-#include "KX_RayEventManager.h"
+//#include "SCA_AlwaysEventManager.h"
+//#include "SCA_RandomEventManager.h"
+//#include "KX_RayEventManager.h"
 #include "KX_TouchEventManager.h"
 #include "SCA_KeyboardManager.h"
 #include "SCA_MouseManager.h"
-#include "SCA_PropertyEventManager.h"
+//#include "SCA_PropertyEventManager.h"
 #include "SCA_ActuatorEventManager.h"
+#include "SCA_BasicEventManager.h"
 #include "KX_Camera.h"
 #include "SCA_JoystickManager.h"
 
@@ -135,16 +135,14 @@ extern bool gUseVisibilityTemp;
 KX_Scene::KX_Scene(class SCA_IInputDevice* keyboarddevice,
                                   class SCA_IInputDevice* mousedevice,
                                   class NG_NetworkDeviceInterface *ndi,
-                                  class SND_IAudioDevice* adi,
                                   const STR_String& sceneName,
                                   Scene *scene): 
-       PyObjectPlus(&KX_Scene::Type),
+       PyObjectPlus(),
        m_keyboardmgr(NULL),
        m_mousemgr(NULL),
        m_sceneConverter(NULL),
        m_physicsEnvironment(0),
        m_sceneName(sceneName),
-       m_adi(adi),
        m_networkDeviceInterface(ndi),
        m_active_camera(NULL),
        m_ueberExecutionPriority(0),
@@ -171,25 +169,27 @@ KX_Scene::KX_Scene(class SCA_IInputDevice* keyboarddevice,
        m_keyboardmgr = new SCA_KeyboardManager(m_logicmgr,keyboarddevice);
        m_mousemgr = new SCA_MouseManager(m_logicmgr,mousedevice);
        
-       SCA_AlwaysEventManager* alwaysmgr = new SCA_AlwaysEventManager(m_logicmgr);
-       SCA_PropertyEventManager* propmgr = new SCA_PropertyEventManager(m_logicmgr);
+       //SCA_AlwaysEventManager* alwaysmgr = new SCA_AlwaysEventManager(m_logicmgr);
+       //SCA_PropertyEventManager* propmgr = new SCA_PropertyEventManager(m_logicmgr);
        SCA_ActuatorEventManager* actmgr = new SCA_ActuatorEventManager(m_logicmgr);
-       SCA_RandomEventManager* rndmgr = new SCA_RandomEventManager(m_logicmgr);
-       KX_RayEventManager* raymgr = new KX_RayEventManager(m_logicmgr);
+       //SCA_RandomEventManager* rndmgr = new SCA_RandomEventManager(m_logicmgr);
+       SCA_BasicEventManager* basicmgr = new SCA_BasicEventManager(m_logicmgr);
+       //KX_RayEventManager* raymgr = new KX_RayEventManager(m_logicmgr);
 
        KX_NetworkEventManager* netmgr = new KX_NetworkEventManager(m_logicmgr, ndi);
        
        
 
-       m_logicmgr->RegisterEventManager(alwaysmgr);
-       m_logicmgr->RegisterEventManager(propmgr);
+       //m_logicmgr->RegisterEventManager(alwaysmgr);
+       //m_logicmgr->RegisterEventManager(propmgr);
        m_logicmgr->RegisterEventManager(actmgr);
        m_logicmgr->RegisterEventManager(m_keyboardmgr);
        m_logicmgr->RegisterEventManager(m_mousemgr);
        m_logicmgr->RegisterEventManager(m_timemgr);
-       m_logicmgr->RegisterEventManager(rndmgr);
-       m_logicmgr->RegisterEventManager(raymgr);
+       //m_logicmgr->RegisterEventManager(rndmgr);
+       //m_logicmgr->RegisterEventManager(raymgr);
        m_logicmgr->RegisterEventManager(netmgr);
+       m_logicmgr->RegisterEventManager(basicmgr);
 
 
        SYS_SystemHandle hSystem = SYS_GetSystem();
@@ -200,7 +200,6 @@ KX_Scene::KX_Scene(class SCA_IInputDevice* keyboarddevice,
                m_logicmgr->RegisterEventManager(joymgr);
        }
 
-       m_soundScene = new SND_Scene(adi);
        MT_assert (m_networkDeviceInterface != NULL);
        m_networkScene = new NG_NetworkScene(m_networkDeviceInterface);
        
@@ -250,9 +249,6 @@ KX_Scene::~KX_Scene()
        if (m_physicsEnvironment)
                delete m_physicsEnvironment;
 
-       if (m_soundScene)
-               delete m_soundScene;
-
        if (m_networkScene)
                delete m_networkScene;
        
@@ -364,12 +360,6 @@ class KX_WorldInfo* KX_Scene::GetWorldInfo()
 }
 
 
-
-SND_Scene* KX_Scene::GetSoundScene()
-{
-       return m_soundScene;
-}
-
 const STR_String& KX_Scene::GetName()
 {
        return m_sceneName;
@@ -1008,8 +998,12 @@ int KX_Scene::NewRemoveObject(class CValue* gameobj)
        // in case this is a camera
        m_cameras.remove((KX_Camera*)newobj);
 
+       /* currently does nothing, keep incase we need to Unregister something */
+#if 0
        if (m_sceneConverter)
                m_sceneConverter->UnregisterGameObject(newobj);
+#endif
+       
        // return value will be 0 if the object is actually deleted (all reference gone)
        
        return ret;
@@ -1017,17 +1011,18 @@ int KX_Scene::NewRemoveObject(class CValue* gameobj)
 
 
 
-void KX_Scene::ReplaceMesh(class CValue* obj,void* meshobj)
+void KX_Scene::ReplaceMesh(class CValue* obj,void* meshobj, bool use_gfx, bool use_phys)
 {
        KX_GameObject* gameobj = static_cast<KX_GameObject*>(obj);
        RAS_MeshObject* mesh = static_cast<RAS_MeshObject*>(meshobj);
 
-       if(!gameobj || !mesh)
-       {
-               std::cout << "warning: invalid object, mesh will not be replaced" << std::endl;
+       if(!gameobj) {
+               std::cout << "KX_Scene::ReplaceMesh Warning: invalid object, doing nothing" << std::endl;
                return;
        }
 
+       if(use_gfx && mesh != NULL)
+       {               
        gameobj->RemoveMeshes();
        gameobj->AddMesh(mesh);
        
@@ -1156,6 +1151,11 @@ void KX_Scene::ReplaceMesh(class CValue* obj,void* meshobj)
        }
 
        gameobj->AddMeshUser();
+       }
+       
+       if(use_phys) { /* update the new assigned mesh with the physics mesh */
+               KX_ReInstanceBulletShapeFromMesh(gameobj, NULL, use_gfx?NULL:mesh);
+       }
 }
 
 KX_Camera* KX_Scene::FindCamera(KX_Camera* cam)
@@ -1573,11 +1573,6 @@ void     KX_Scene::SetGravity(const MT_Vector3& gravity)
        GetPhysicsEnvironment()->setGravity(gravity[0],gravity[1],gravity[2]);
 }
 
-void KX_Scene::SetNodeTree(SG_Tree* root)
-{
-       m_objecttree = root;
-}
-
 void KX_Scene::SetSceneConverter(class KX_BlenderSceneConverter* sceneConverter)
 {
        m_sceneConverter = sceneConverter;
@@ -1613,48 +1608,147 @@ double KX_Scene::getSuspendedDelta()
 //Python
 
 PyTypeObject KX_Scene::Type = {
-#if (PY_VERSION_HEX >= 0x02060000)
        PyVarObject_HEAD_INIT(NULL, 0)
-#else
-       /* python 2.5 and below */
-       PyObject_HEAD_INIT( NULL )  /* required py macro */
-       0,                          /* ob_size */
-#endif
-               "KX_Scene",
-               sizeof(PyObjectPlus_Proxy),
-               0,
-               py_base_dealloc,
-               0,
-               0,
-               0,
-               0,
-               py_base_repr,
-               0,0,0,0,0,0,
-               py_base_getattro,
-               py_base_setattro,
-               0,0,0,0,0,0,0,0,0,
-               Methods
-};
-
-PyParentObject KX_Scene::Parents[] = {
-       &KX_Scene::Type,
-               &CValue::Type,
-               NULL
+       "KX_Scene",
+       sizeof(PyObjectPlus_Proxy),
+       0,
+       py_base_dealloc,
+       0,
+       0,
+       0,
+       0,
+       py_base_repr,
+       0,
+       &Sequence,
+       &Mapping,
+       0,0,0,0,0,0,
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+       0,0,0,0,0,0,0,
+       Methods,
+       0,
+       0,
+       &CValue::Type,
+       0,0,0,0,0,0,
+       py_base_new
 };
 
 PyMethodDef KX_Scene::Methods[] = {
-       KX_PYMETHODTABLE_NOARGS(KX_Scene, getLightList),
-       KX_PYMETHODTABLE_NOARGS(KX_Scene, getObjectList),
-       KX_PYMETHODTABLE_NOARGS(KX_Scene, getName),
        KX_PYMETHODTABLE(KX_Scene, addObject),
        
+       /* dict style access */
+       KX_PYMETHODTABLE(KX_Scene, get),
+       
        {NULL,NULL} //Sentinel
 };
+static PyObject *Map_GetItem(PyObject *self_v, PyObject *item)
+{
+       KX_Scene* self= static_cast<KX_Scene*>BGE_PROXY_REF(self_v);
+       const char *attr_str= _PyUnicode_AsString(item);
+       PyObject* pyconvert;
+       
+       if (self==NULL) {
+               PyErr_SetString(PyExc_SystemError, "val = scene[key]: KX_Scene, "BGE_PROXY_ERROR_MSG);
+               return NULL;
+       }
+       
+       if (self->m_attr_dict && (pyconvert=PyDict_GetItem(self->m_attr_dict, item))) {
+               
+               if (attr_str)
+                       PyErr_Clear();
+               Py_INCREF(pyconvert);
+               return pyconvert;
+       }
+       else {
+               if(attr_str)    PyErr_Format(PyExc_KeyError, "value = scene[key]: KX_Scene, key \"%s\" does not exist", attr_str);
+               else                    PyErr_SetString(PyExc_KeyError, "value = scene[key]: KX_Scene, key does not exist");
+               return NULL;
+       }
+               
+}
+
+static int Map_SetItem(PyObject *self_v, PyObject *key, PyObject *val)
+{
+       KX_Scene* self= static_cast<KX_Scene*>BGE_PROXY_REF(self_v);
+       const char *attr_str= _PyUnicode_AsString(key);
+       if(attr_str==NULL)
+               PyErr_Clear();
+       
+       if (self==NULL) {
+               PyErr_SetString(PyExc_SystemError, "scene[key] = value: KX_Scene, "BGE_PROXY_ERROR_MSG);
+               return -1;
+       }
+       
+       if (val==NULL) { /* del ob["key"] */
+               int del= 0;
+               
+               if(self->m_attr_dict)
+                       del |= (PyDict_DelItem(self->m_attr_dict, key)==0) ? 1:0;
+               
+               if (del==0) {
+                       if(attr_str)    PyErr_Format(PyExc_KeyError, "scene[key] = value: KX_Scene, key \"%s\" could not be set", attr_str);
+                       else                    PyErr_SetString(PyExc_KeyError, "del scene[key]: KX_Scene, key could not be deleted");
+                       return -1;
+               }
+               else if (self->m_attr_dict) {
+                       PyErr_Clear(); /* PyDict_DelItem sets an error when it fails */
+               }
+       }
+       else { /* ob["key"] = value */
+               int set = 0;
+
+               if (self->m_attr_dict==NULL) /* lazy init */
+                       self->m_attr_dict= PyDict_New();
+               
+               
+               if(PyDict_SetItem(self->m_attr_dict, key, val)==0)
+                       set= 1;
+               else
+                       PyErr_SetString(PyExc_KeyError, "scene[key] = value: KX_Scene, key not be added to internal dictionary");
+       
+               if(set==0)
+                       return -1; /* pythons error value */
+               
+       }
+       
+       return 0; /* success */
+}
+
+static int Seq_Contains(PyObject *self_v, PyObject *value)
+{
+       KX_Scene* self= static_cast<KX_Scene*>BGE_PROXY_REF(self_v);
+       
+       if (self==NULL) {
+               PyErr_SetString(PyExc_SystemError, "val in scene: KX_Scene, "BGE_PROXY_ERROR_MSG);
+               return -1;
+       }
+       
+       if (self->m_attr_dict && PyDict_GetItem(self->m_attr_dict, value))
+               return 1;
+       
+       return 0;
+}
+
+PyMappingMethods KX_Scene::Mapping = {
+       (lenfunc)NULL                                   ,                       /*inquiry mp_length */
+       (binaryfunc)Map_GetItem,                /*binaryfunc mp_subscript */
+       (objobjargproc)Map_SetItem,     /*objobjargproc mp_ass_subscript */
+};
+
+PySequenceMethods KX_Scene::Sequence = {
+       NULL,           /* Cant set the len otherwise it can evaluate as false */
+       NULL,           /* sq_concat */
+       NULL,           /* sq_repeat */
+       NULL,           /* sq_item */
+       NULL,           /* sq_slice */
+       NULL,           /* sq_ass_item */
+       NULL,           /* sq_ass_slice */
+       (objobjproc)Seq_Contains,       /* sq_contains */
+};
 
 PyObject* KX_Scene::pyattr_get_name(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
        KX_Scene* self= static_cast<KX_Scene*>(self_v);
-       return PyString_FromString(self->GetName().ReadPtr());
+       return PyUnicode_FromString(self->GetName().ReadPtr());
 }
 
 PyObject* KX_Scene::pyattr_get_objects(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
@@ -1730,99 +1824,6 @@ PyAttributeDef KX_Scene::Attributes[] = {
        { NULL }        //Sentinel
 };
 
-PyObject* KX_Scene::py_getattro__internal(PyObject *attr)
-{      
-       py_getattro_up(PyObjectPlus);
-}
-
-int KX_Scene::py_setattro__internal(PyObject *attr, PyObject *value)
-{
-       py_setattro_up(PyObjectPlus);
-}
-
-PyObject* KX_Scene::py_getattro(PyObject *attr)
-{
-       PyObject *object = py_getattro__internal(attr);
-       
-       if (object==NULL)
-       {
-               PyErr_Clear();
-               object = PyDict_GetItem(m_attr_dict, attr);
-               if(object) {
-                       Py_INCREF(object);
-               }
-               else {
-                       PyErr_Format(PyExc_AttributeError, "value = scene.myAttr: KX_Scene, attribute \"%s\" not found", PyString_AsString(attr));
-               }
-       }
-       
-       return object;
-}
-
-PyObject* KX_Scene::py_getattro_dict() {
-       //py_getattro_dict_up(PyObjectPlus);
-       
-       PyObject *dict= py_getattr_dict(PyObjectPlus::py_getattro_dict(), Type.tp_dict);
-       if(dict==NULL)
-               return NULL;
-       
-       /* normally just return this but KX_Scene has some more items */
-       
-       PyDict_Update(dict, m_attr_dict);
-       return dict;
-}
-
-int KX_Scene::py_setattro(PyObject *attr, PyObject *value)
-{
-       int ret= py_setattro__internal(attr, value);
-       
-       if (ret==PY_SET_ATTR_MISSING) {
-               if (PyDict_SetItem(m_attr_dict, attr, value)==0) {
-                       PyErr_Clear();
-                       ret= PY_SET_ATTR_SUCCESS;
-               }
-               else {
-                       PyErr_SetString(PyExc_AttributeError, "scene.UserAttr = value: KX_Scenes, failed assigning value to internal dictionary");
-                       ret= PY_SET_ATTR_FAIL;
-               }
-       }
-       
-       return ret;
-}
-
-int KX_Scene::py_delattro(PyObject *attr)
-{
-       PyDict_DelItem(m_attr_dict, attr);
-       return 0;
-}
-
-KX_PYMETHODDEF_DOC_NOARGS(KX_Scene, getLightList,
-"getLightList() -> list [KX_Light]\n"
-"Returns a list of all lights in the scene.\n"
-)
-{
-       ShowDeprecationWarning("getLightList()", "the lights property");
-       return m_lightlist->GetProxy();
-}
-
-KX_PYMETHODDEF_DOC_NOARGS(KX_Scene, getObjectList,
-"getObjectList() -> list [KX_GameObject]\n"
-"Returns a list of all game objects in the scene.\n"
-)
-{
-       ShowDeprecationWarning("getObjectList()", "the objects property");
-       return m_objectlist->GetProxy();
-}
-
-KX_PYMETHODDEF_DOC_NOARGS(KX_Scene, getName,
-"getName() -> string\n"
-"Returns the name of the scene.\n"
-)
-{
-       ShowDeprecationWarning("getName()", "the name property");
-       return PyString_FromString(GetName());
-}
-
 KX_PYMETHODDEF_DOC(KX_Scene, addObject,
 "addObject(object, other, time=0)\n"
 "Returns the added object.\n")
@@ -1847,3 +1848,22 @@ KX_PYMETHODDEF_DOC(KX_Scene, addObject,
        replica->Release();
        return replica->GetProxy();
 }
+
+/* Matches python dict.get(key, [default]) */
+KX_PYMETHODDEF_DOC(KX_Scene, get, "")
+{
+       PyObject *key;
+       PyObject* def = Py_None;
+       PyObject* ret;
+
+       if (!PyArg_ParseTuple(args, "O|O:get", &key, &def))
+               return NULL;
+       
+       if (m_attr_dict && (ret=PyDict_GetItem(m_attr_dict, key))) {
+               Py_INCREF(ret);
+               return ret;
+       }
+       
+       Py_INCREF(def);
+       return def;
+}