synched with trunk at revision 36569
[blender.git] / source / gameengine / Ketsji / KX_GameObject.cpp
index bfafce1..d4ef462 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * $Id$
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
@@ -15,7 +15,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  * All rights reserved.
  * Game object wrapper
  */
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
+/** \file gameengine/Ketsji/KX_GameObject.cpp
+ *  \ingroup ketsji
+ */
+
 
 #if defined(_WIN64)
 typedef unsigned __int64 uint_ptr;
@@ -38,7 +39,7 @@ typedef unsigned __int64 uint_ptr;
 typedef unsigned long uint_ptr;
 #endif
 
-#ifdef WIN32
+#if defined(WIN32) && !defined(FREE_WINDOWS)
 // This warning tells us about truncation of __long__ stl-generated names.
 // It can occasionally cause DevStudio to have internal compiler warnings.
 #pragma warning( disable : 4786 )     
@@ -51,6 +52,7 @@ typedef unsigned long uint_ptr;
 #include "KX_GameObject.h"
 #include "KX_Camera.h" // only for their ::Type
 #include "KX_Light.h"  // only for their ::Type
+#include "KX_FontObject.h"  // only for their ::Type
 #include "RAS_MeshObject.h"
 #include "KX_MeshProxy.h"
 #include "KX_PolyProxy.h"
@@ -71,6 +73,7 @@ typedef unsigned long uint_ptr;
 #include "SCA_ISensor.h"
 #include "SCA_IController.h"
 #include "NG_NetworkScene.h" //Needed for sendMessage()
+#include "KX_ObstacleSimulation.h"
 
 #include "PyObjectPlus.h" /* python stuff */
 
@@ -79,6 +82,8 @@ typedef unsigned long uint_ptr;
 
 #include "KX_SG_NodeRelationships.h"
 
+#include "BLI_math.h"
+
 static MT_Point3 dummy_point= MT_Point3(0.0, 0.0, 0.0);
 static MT_Vector3 dummy_scaling = MT_Vector3(1.0, 1.0, 1.0);
 static MT_Matrix3x3 dummy_orientation = MT_Matrix3x3(  1.0, 0.0, 0.0,
@@ -87,10 +92,8 @@ static MT_Matrix3x3 dummy_orientation = MT_Matrix3x3(        1.0, 0.0, 0.0,
 
 KX_GameObject::KX_GameObject(
        void* sgReplicationInfo,
-       SG_Callbacks callbacks,
-       PyTypeObject* T
-) : 
-       SCA_IObject(T),
+       SG_Callbacks callbacks)
+       : SCA_IObject(),
        m_bDyna(false),
        m_layer(0),
        m_pBlenderObject(NULL),
@@ -106,7 +109,10 @@ KX_GameObject::KX_GameObject(
        m_xray(false),
        m_pHitObject(NULL),
        m_isDeformable(false),
-       m_attr_dict(NULL)
+       m_pObstacleSimulation(NULL)
+#ifdef WITH_PYTHON
+       , m_attr_dict(NULL)
+#endif
 {
        m_ignore_activity_culling = false;
        m_pClient_info = new KX_ClientObjectInfo(this, KX_ClientObjectInfo::ACTOR);
@@ -143,16 +149,26 @@ KX_GameObject::~KX_GameObject()
                        (*contit)->ClearObject();
                }
                m_pSGNode->SetSGClientObject(NULL);
+
+               /* m_pSGNode is freed in KX_Scene::RemoveNodeDestructObject */
        }
        if (m_pGraphicController)
        {
                delete m_pGraphicController;
        }
-       
+
+       if (m_pObstacleSimulation)
+       {
+               m_pObstacleSimulation->DestroyObstacleForObj(this);
+       }
+
+#ifdef WITH_PYTHON
        if (m_attr_dict) {
-               PyDict_Clear(m_attr_dict); /* incase of circular refs or other weired cases */
-               Py_DECREF(m_attr_dict);
+               PyDict_Clear(m_attr_dict); /* incase of circular refs or other weird cases */
+               /* Py_CLEAR: Py_DECREF's and NULL's */
+               Py_CLEAR(m_attr_dict);
        }
+#endif // WITH_PYTHON
 }
 
 KX_GameObject* KX_GameObject::GetClientObject(KX_ClientObjectInfo* info)
@@ -229,7 +245,12 @@ KX_GameObject* KX_GameObject::GetParent()
 void KX_GameObject::SetParent(KX_Scene *scene, KX_GameObject* obj, bool addToCompound, bool ghost)
 {
        // check on valid node in case a python controller holds a reference to a deleted object
-       if (obj && GetSGNode() && obj->GetSGNode() && GetSGNode()->GetSGParent() != obj->GetSGNode())
+       if (obj && 
+               GetSGNode() &&                  // object is not zombi
+               obj->GetSGNode() &&             // object is not zombi
+               GetSGNode()->GetSGParent() != obj->GetSGNode() &&       // not already parented to same object
+               !GetSGNode()->IsAncessor(obj->GetSGNode()) &&           // no parenting loop
+               this != obj)                                                                            // not the object itself
        {
                // Make sure the objects have some scale
                MT_Vector3 scale1 = NodeGetWorldScaling();
@@ -341,8 +362,19 @@ void KX_GameObject::ProcessReplica()
        m_pClient_info = new KX_ClientObjectInfo(*m_pClient_info);
        m_pClient_info->m_gameobject = this;
        m_state = 0;
+
+       KX_Scene* scene = KX_GetActiveScene();
+       KX_ObstacleSimulation* obssimulation = scene->GetObstacleSimulation();
+       struct Object* blenderobject = GetBlenderObject();
+       if (obssimulation && (blenderobject->gameflag & OB_HASOBSTACLE))
+       {
+               obssimulation->AddObstacleForObj(this);
+       }
+
+#ifdef WITH_PYTHON
        if(m_attr_dict)
                m_attr_dict= PyDict_Copy(m_attr_dict);
+#endif
                
 }
 
@@ -456,6 +488,22 @@ double*    KX_GameObject::GetOpenGLMatrix()
        return fl;
 }
 
+void KX_GameObject::UpdateBlenderObjectMatrix(Object* blendobj)
+{
+       if (!blendobj)
+               blendobj = m_pBlenderObject;
+       if (blendobj) {
+               const MT_Matrix3x3& rot = NodeGetWorldOrientation();
+               const MT_Vector3& scale = NodeGetWorldScaling();
+               const MT_Vector3& pos = NodeGetWorldPosition();
+               rot.getValue(blendobj->obmat[0]);
+               pos.getValue(blendobj->obmat[3]);
+               mul_v3_fl(blendobj->obmat[0], scale[0]);
+               mul_v3_fl(blendobj->obmat[1], scale[1]);
+               mul_v3_fl(blendobj->obmat[2], scale[2]);
+       }
+}
+
 void KX_GameObject::AddMeshUser()
 {
        for (size_t i=0;i<m_meshes.size();i++)
@@ -466,7 +514,7 @@ void KX_GameObject::AddMeshUser()
        double* fl = GetOpenGLMatrixPtr()->getPointer();
 
        SG_QList::iterator<RAS_MeshSlot> mit(m_meshSlots);
-       RAS_MeshSlot* ms;
+//     RAS_MeshSlot* ms;
        for(mit.begin(); !mit.end(); ++mit)
        {
                (*mit)->m_OpenGLMatrix = fl;
@@ -787,6 +835,11 @@ void KX_GameObject::SetObjectColor(const MT_Vector4& rgbavec)
        m_objectColor = rgbavec;
 }
 
+const MT_Vector4& KX_GameObject::GetObjectColor()
+{
+       return m_objectColor;
+}
+
 void KX_GameObject::AlignAxisToVect(const MT_Vector3& dir, int axis, float fac)
 {
        MT_Matrix3x3 orimat;
@@ -816,7 +869,7 @@ void KX_GameObject::AlignAxisToVect(const MT_Vector3& dir, int axis, float fac)
        {       
                case 0: //x axis
                        ori.setValue(orimat[0][2], orimat[1][2], orimat[2][2]); //pivot axis
-                       if (MT_abs(vect.dot(ori)) > 1.0-3.0*MT_EPSILON) //is the vector paralell to the pivot?
+                       if (MT_abs(vect.dot(ori)) > 1.0-3.0*MT_EPSILON) //is the vector parallel to the pivot?
                                ori.setValue(orimat[0][1], orimat[1][1], orimat[2][1]); //change the pivot!
                        if (fac == 1.0) {
                                x = vect;
@@ -983,7 +1036,17 @@ void KX_GameObject::NodeSetLocalOrientation(const MT_Matrix3x3& rot)
        GetSGNode()->SetLocalOrientation(rot);
 }
 
+void KX_GameObject::NodeSetGlobalOrientation(const MT_Matrix3x3& rot)
+{
+       // check on valid node in case a python controller holds a reference to a deleted object
+       if (!GetSGNode())
+               return;
 
+       if (GetSGNode()->GetSGParent())
+               GetSGNode()->SetLocalOrientation(GetSGNode()->GetSGParent()->GetWorldOrientation().inverse()*rot);
+       else
+               NodeSetLocalOrientation(rot);
+}
 
 void KX_GameObject::NodeSetLocalScale(const MT_Vector3& scale)
 {
@@ -1062,7 +1125,13 @@ const MT_Matrix3x3& KX_GameObject::NodeGetWorldOrientation() const
        return GetSGNode()->GetWorldOrientation();
 }
 
-
+const MT_Matrix3x3& KX_GameObject::NodeGetLocalOrientation() const
+{
+       // check on valid node in case a python controller holds a reference to a deleted object
+       if (!GetSGNode())
+               return dummy_orientation;
+       return GetSGNode()->GetLocalOrientation();
+}
 
 const MT_Vector3& KX_GameObject::NodeGetWorldScaling() const
 {
@@ -1073,7 +1142,14 @@ const MT_Vector3& KX_GameObject::NodeGetWorldScaling() const
        return GetSGNode()->GetWorldScaling();
 }
 
+const MT_Vector3& KX_GameObject::NodeGetLocalScaling() const
+{
+       // check on valid node in case a python controller holds a reference to a deleted object
+       if (!GetSGNode())
+               return dummy_scaling;
 
+       return GetSGNode()->GetLocalScale();
+}
 
 const MT_Point3& KX_GameObject::NodeGetWorldPosition() const
 {
@@ -1084,6 +1160,16 @@ const MT_Point3& KX_GameObject::NodeGetWorldPosition() const
                return dummy_point;
 }
 
+const MT_Point3& KX_GameObject::NodeGetLocalPosition() const
+{
+       // check on valid node in case a python controller holds a reference to a deleted object
+       if (GetSGNode())
+               return GetSGNode()->GetLocalPosition();
+       else
+               return dummy_point;
+}
+
+
 /* Suspend/ resume: for the dynamic behaviour, there is a simple
  * method. For the residual motion, there is not. I wonder what the
  * correct solution is for Sumo. Remove from the motion-update tree?
@@ -1151,9 +1237,246 @@ CListValue* KX_GameObject::GetChildrenRecursive()
        return list;
 }
 
+/* ---------------------------------------------------------------------
+ * Some stuff taken from the header
+ * --------------------------------------------------------------------- */
+void KX_GameObject::Relink(CTR_Map<CTR_HashedPtr, void*> *map_parameter)
+{
+       // we will relink the sensors and actuators that use object references
+       // if the object is part of the replicated hierarchy, use the new
+       // object reference instead
+       SCA_SensorList& sensorlist = GetSensors();
+       SCA_SensorList::iterator sit;
+       for (sit=sensorlist.begin(); sit != sensorlist.end(); sit++)
+       {
+               (*sit)->Relink(map_parameter);
+       }
+       SCA_ActuatorList& actuatorlist = GetActuators();
+       SCA_ActuatorList::iterator ait;
+       for (ait=actuatorlist.begin(); ait != actuatorlist.end(); ait++)
+       {
+               (*ait)->Relink(map_parameter);
+       }
+}
+
+#ifdef USE_MATHUTILS
+
+/* These require an SGNode */
+#define MATHUTILS_VEC_CB_POS_LOCAL 1
+#define MATHUTILS_VEC_CB_POS_GLOBAL 2
+#define MATHUTILS_VEC_CB_SCALE_LOCAL 3
+#define MATHUTILS_VEC_CB_SCALE_GLOBAL 4
+#define MATHUTILS_VEC_CB_INERTIA_LOCAL 5
+#define MATHUTILS_VEC_CB_OBJECT_COLOR 6
+#define MATHUTILS_VEC_CB_LINVEL_LOCAL 7
+#define MATHUTILS_VEC_CB_LINVEL_GLOBAL 8
+#define MATHUTILS_VEC_CB_ANGVEL_LOCAL 9
+#define MATHUTILS_VEC_CB_ANGVEL_GLOBAL 10
+
+static int mathutils_kxgameob_vector_cb_index= -1; /* index for our callbacks */
+
+static int mathutils_kxgameob_generic_check(BaseMathObject *bmo)
+{
+       KX_GameObject* self= static_cast<KX_GameObject*>BGE_PROXY_REF(bmo->cb_user);
+       if(self==NULL)
+               return -1;
+       
+       return 0;
+}
+
+static int mathutils_kxgameob_vector_get(BaseMathObject *bmo, int subtype)
+{
+       KX_GameObject* self= static_cast<KX_GameObject*>BGE_PROXY_REF(bmo->cb_user);
+       if(self==NULL)
+               return -1;
+
+#define PHYS_ERR(attr) PyErr_SetString(PyExc_AttributeError, "KX_GameObject." attr ", is missing a physics controller")
+
+       switch(subtype) {
+               case MATHUTILS_VEC_CB_POS_LOCAL:
+                       self->NodeGetLocalPosition().getValue(bmo->data);
+                       break;
+               case MATHUTILS_VEC_CB_POS_GLOBAL:
+                       self->NodeGetWorldPosition().getValue(bmo->data);
+                       break;
+               case MATHUTILS_VEC_CB_SCALE_LOCAL:
+                       self->NodeGetLocalScaling().getValue(bmo->data);
+                       break;
+               case MATHUTILS_VEC_CB_SCALE_GLOBAL:
+                       self->NodeGetWorldScaling().getValue(bmo->data);
+                       break;
+               case MATHUTILS_VEC_CB_INERTIA_LOCAL:
+                       if(!self->GetPhysicsController()) return PHYS_ERR("localInertia"), -1;
+                       self->GetPhysicsController()->GetLocalInertia().getValue(bmo->data);
+                       break;
+               case MATHUTILS_VEC_CB_OBJECT_COLOR:
+                       self->GetObjectColor().getValue(bmo->data);
+                       break;
+               case MATHUTILS_VEC_CB_LINVEL_LOCAL:
+                       if(!self->GetPhysicsController()) return PHYS_ERR("localLinearVelocity"), -1;
+                       self->GetLinearVelocity(true).getValue(bmo->data);
+                       break;
+               case MATHUTILS_VEC_CB_LINVEL_GLOBAL:
+                       if(!self->GetPhysicsController()) return PHYS_ERR("worldLinearVelocity"), -1;
+                       self->GetLinearVelocity(false).getValue(bmo->data);
+                       break;
+               case MATHUTILS_VEC_CB_ANGVEL_LOCAL:
+                       if(!self->GetPhysicsController()) return PHYS_ERR("localLinearVelocity"), -1;
+                       self->GetAngularVelocity(true).getValue(bmo->data);
+                       break;
+               case MATHUTILS_VEC_CB_ANGVEL_GLOBAL:
+                       if(!self->GetPhysicsController()) return PHYS_ERR("worldLinearVelocity"), -1;
+                       self->GetAngularVelocity(false).getValue(bmo->data);
+                       break;
+                       
+       }
+       
+#undef PHYS_ERR
+       
+       return 0;
+}
+
+static int mathutils_kxgameob_vector_set(BaseMathObject *bmo, int subtype)
+{
+       KX_GameObject* self= static_cast<KX_GameObject*>BGE_PROXY_REF(bmo->cb_user);
+       if(self==NULL)
+               return -1;
+       
+       switch(subtype) {
+               case MATHUTILS_VEC_CB_POS_LOCAL:
+                       self->NodeSetLocalPosition(MT_Point3(bmo->data));
+                       self->NodeUpdateGS(0.f);
+                       break;
+               case MATHUTILS_VEC_CB_POS_GLOBAL:
+                       self->NodeSetWorldPosition(MT_Point3(bmo->data));
+                       self->NodeUpdateGS(0.f);
+                       break;
+               case MATHUTILS_VEC_CB_SCALE_LOCAL:
+                       self->NodeSetLocalScale(MT_Point3(bmo->data));
+                       self->NodeUpdateGS(0.f);
+                       break;
+               case MATHUTILS_VEC_CB_SCALE_GLOBAL:
+                       PyErr_SetString(PyExc_AttributeError, "KX_GameObject.worldScale is read-only");
+                       return -1;
+               case MATHUTILS_VEC_CB_INERTIA_LOCAL:
+                       /* read only */
+                       break;
+               case MATHUTILS_VEC_CB_OBJECT_COLOR:
+                       self->SetObjectColor(MT_Vector4(bmo->data));
+                       break;
+               case MATHUTILS_VEC_CB_LINVEL_LOCAL:
+                       self->setLinearVelocity(MT_Point3(bmo->data),true);
+                       break;
+               case MATHUTILS_VEC_CB_LINVEL_GLOBAL:
+                       self->setLinearVelocity(MT_Point3(bmo->data),false);
+                       break;
+               case MATHUTILS_VEC_CB_ANGVEL_LOCAL:
+                       self->setAngularVelocity(MT_Point3(bmo->data),true);
+                       break;
+               case MATHUTILS_VEC_CB_ANGVEL_GLOBAL:
+                       self->setAngularVelocity(MT_Point3(bmo->data),false);
+                       break;
+       }
+       
+       return 0;
+}
+
+static int mathutils_kxgameob_vector_get_index(BaseMathObject *bmo, int subtype, int index)
+{
+       /* lazy, avoid repeteing the case statement */
+       if(mathutils_kxgameob_vector_get(bmo, subtype) == -1)
+               return -1;
+       return 0;
+}
+
+static int mathutils_kxgameob_vector_set_index(BaseMathObject *bmo, int subtype, int index)
+{
+       float f= bmo->data[index];
+       
+       /* lazy, avoid repeteing the case statement */
+       if(mathutils_kxgameob_vector_get(bmo, subtype) == -1)
+               return -1;
+       
+       bmo->data[index]= f;
+       return mathutils_kxgameob_vector_set(bmo, subtype);
+}
+
+Mathutils_Callback mathutils_kxgameob_vector_cb = {
+       mathutils_kxgameob_generic_check,
+       mathutils_kxgameob_vector_get,
+       mathutils_kxgameob_vector_set,
+       mathutils_kxgameob_vector_get_index,
+       mathutils_kxgameob_vector_set_index
+};
+
+/* Matrix */
+#define MATHUTILS_MAT_CB_ORI_LOCAL 1
+#define MATHUTILS_MAT_CB_ORI_GLOBAL 2
+
+static int mathutils_kxgameob_matrix_cb_index= -1; /* index for our callbacks */
+
+static int mathutils_kxgameob_matrix_get(BaseMathObject *bmo, int subtype)
+{
+       KX_GameObject* self= static_cast<KX_GameObject*>BGE_PROXY_REF(bmo->cb_user);
+       if(self==NULL)
+               return -1;
+
+       switch(subtype) {
+               case MATHUTILS_MAT_CB_ORI_LOCAL:
+                       self->NodeGetLocalOrientation().getValue3x3(bmo->data);
+                       break;
+               case MATHUTILS_MAT_CB_ORI_GLOBAL:
+                       self->NodeGetWorldOrientation().getValue3x3(bmo->data);
+                       break;
+       }
+       
+       return 0;
+}
+
+
+static int mathutils_kxgameob_matrix_set(BaseMathObject *bmo, int subtype)
+{
+       KX_GameObject* self= static_cast<KX_GameObject*>BGE_PROXY_REF(bmo->cb_user);
+       if(self==NULL)
+               return -1;
+       
+       MT_Matrix3x3 mat3x3;
+       switch(subtype) {
+               case MATHUTILS_MAT_CB_ORI_LOCAL:
+                       mat3x3.setValue3x3(bmo->data);
+                       self->NodeSetLocalOrientation(mat3x3);
+                       self->NodeUpdateGS(0.f);
+                       break;
+               case MATHUTILS_MAT_CB_ORI_GLOBAL:
+                       mat3x3.setValue3x3(bmo->data);
+                       self->NodeSetLocalOrientation(mat3x3);
+                       self->NodeUpdateGS(0.f);
+                       break;
+       }
+       
+       return 0;
+}
+
+Mathutils_Callback mathutils_kxgameob_matrix_cb = {
+       mathutils_kxgameob_generic_check,
+       mathutils_kxgameob_matrix_get,
+       mathutils_kxgameob_matrix_set,
+       NULL,
+       NULL
+};
 
-/* ------- python stuff ---------------------------------------------------*/
 
+void KX_GameObject_Mathutils_Callback_Init(void)
+{
+       // register mathutils callbacks, ok to run more then once.
+       mathutils_kxgameob_vector_cb_index= Mathutils_RegisterCallback(&mathutils_kxgameob_vector_cb);
+       mathutils_kxgameob_matrix_cb_index= Mathutils_RegisterCallback(&mathutils_kxgameob_matrix_cb);
+}
+
+#endif // USE_MATHUTILS
+
+#ifdef WITH_PYTHON
+/* ------- python stuff ---------------------------------------------------*/
 PyMethodDef KX_GameObject::Methods[] = {
        {"applyForce", (PyCFunction)    KX_GameObject::sPyApplyForce, METH_VARARGS},
        {"applyTorque", (PyCFunction)   KX_GameObject::sPyApplyTorque, METH_VARARGS},
@@ -1177,8 +1500,8 @@ PyMethodDef KX_GameObject::Methods[] = {
        {"setVisible",(PyCFunction) KX_GameObject::sPySetVisible, METH_VARARGS},
        {"setOcclusion",(PyCFunction) KX_GameObject::sPySetOcclusion, METH_VARARGS},
        {"removeParent", (PyCFunction)KX_GameObject::sPyRemoveParent,METH_NOARGS},
-       {"getChildren", (PyCFunction)KX_GameObject::sPyGetChildren,METH_NOARGS},
-       {"getChildrenRecursive", (PyCFunction)KX_GameObject::sPyGetChildrenRecursive,METH_NOARGS},
+
+
        {"getPhysicsId", (PyCFunction)KX_GameObject::sPyGetPhysicsId,METH_NOARGS},
        {"getPropertyNames", (PyCFunction)KX_GameObject::sPyGetPropertyNames,METH_NOARGS},
        {"replaceMesh",(PyCFunction) KX_GameObject::sPyReplaceMesh, METH_VARARGS},
@@ -1192,27 +1515,15 @@ PyMethodDef KX_GameObject::Methods[] = {
        KX_PYMETHODTABLE(KX_GameObject, sendMessage),
        
        // dict style access for props
-       {"has_key",(PyCFunction) KX_GameObject::sPyhas_key, METH_O},
        {"get",(PyCFunction) KX_GameObject::sPyget, METH_VARARGS},
        
-       // deprecated
-       {"getPosition", (PyCFunction) KX_GameObject::sPyGetPosition, METH_NOARGS},
-       {"setPosition", (PyCFunction) KX_GameObject::sPySetPosition, METH_O},
-       {"setWorldPosition", (PyCFunction) KX_GameObject::sPySetWorldPosition, METH_O},
-       {"getOrientation", (PyCFunction) KX_GameObject::sPyGetOrientation, METH_NOARGS},
-       {"setOrientation", (PyCFunction) KX_GameObject::sPySetOrientation, METH_O},
-       {"getState",(PyCFunction) KX_GameObject::sPyGetState, METH_NOARGS},
-       {"setState",(PyCFunction) KX_GameObject::sPySetState, METH_O},
-       {"getParent", (PyCFunction)KX_GameObject::sPyGetParent,METH_NOARGS},
-       {"getVisible",(PyCFunction) KX_GameObject::sPyGetVisible, METH_NOARGS},
-       {"getMass", (PyCFunction) KX_GameObject::sPyGetMass, METH_NOARGS},
-       {"getMesh", (PyCFunction)KX_GameObject::sPyGetMesh,METH_VARARGS},
        {NULL,NULL} //Sentinel
 };
 
 PyAttributeDef KX_GameObject::Attributes[] = {
        KX_PYATTRIBUTE_RO_FUNCTION("name",              KX_GameObject, pyattr_get_name),
        KX_PYATTRIBUTE_RO_FUNCTION("parent",    KX_GameObject, pyattr_get_parent),
+       KX_PYATTRIBUTE_RO_FUNCTION("life",              KX_GameObject, pyattr_get_life),
        KX_PYATTRIBUTE_RW_FUNCTION("mass",              KX_GameObject, pyattr_get_mass,         pyattr_set_mass),
        KX_PYATTRIBUTE_RW_FUNCTION("linVelocityMin",            KX_GameObject, pyattr_get_lin_vel_min, pyattr_set_lin_vel_min),
        KX_PYATTRIBUTE_RW_FUNCTION("linVelocityMax",            KX_GameObject, pyattr_get_lin_vel_max, pyattr_set_lin_vel_max),
@@ -1231,9 +1542,16 @@ PyAttributeDef KX_GameObject::Attributes[] = {
        KX_PYATTRIBUTE_RW_FUNCTION("worldPosition",     KX_GameObject, pyattr_get_worldPosition,    pyattr_set_worldPosition),
        KX_PYATTRIBUTE_RW_FUNCTION("localScale",        KX_GameObject, pyattr_get_localScaling, pyattr_set_localScaling),
        KX_PYATTRIBUTE_RO_FUNCTION("worldScale",        KX_GameObject, pyattr_get_worldScaling),
+       KX_PYATTRIBUTE_RW_FUNCTION("linearVelocity", KX_GameObject, pyattr_get_localLinearVelocity, pyattr_set_worldLinearVelocity),
+       KX_PYATTRIBUTE_RW_FUNCTION("localLinearVelocity", KX_GameObject, pyattr_get_localLinearVelocity, pyattr_set_localLinearVelocity),
+       KX_PYATTRIBUTE_RW_FUNCTION("worldLinearVelocity", KX_GameObject, pyattr_get_worldLinearVelocity, pyattr_set_worldLinearVelocity),
+       KX_PYATTRIBUTE_RW_FUNCTION("angularVelocity", KX_GameObject, pyattr_get_localAngularVelocity, pyattr_set_worldAngularVelocity),
+       KX_PYATTRIBUTE_RW_FUNCTION("localAngularVelocity", KX_GameObject, pyattr_get_localAngularVelocity, pyattr_set_localAngularVelocity),
+       KX_PYATTRIBUTE_RW_FUNCTION("worldAngularVelocity", KX_GameObject, pyattr_get_worldAngularVelocity, pyattr_set_worldAngularVelocity),
        KX_PYATTRIBUTE_RO_FUNCTION("children",  KX_GameObject, pyattr_get_children),
        KX_PYATTRIBUTE_RO_FUNCTION("childrenRecursive", KX_GameObject, pyattr_get_children_recursive),
        KX_PYATTRIBUTE_RO_FUNCTION("attrDict",  KX_GameObject, pyattr_get_attrDict),
+       KX_PYATTRIBUTE_RW_FUNCTION("color", KX_GameObject, pyattr_get_obcolor, pyattr_set_obcolor),
        
        /* Experemental, dont rely on these yet */
        KX_PYATTRIBUTE_RO_FUNCTION("sensors",           KX_GameObject, pyattr_get_sensors),
@@ -1242,23 +1560,6 @@ PyAttributeDef KX_GameObject::Attributes[] = {
        {NULL} //Sentinel
 };
 
-
-/*
-bool KX_GameObject::ConvertPythonVectorArgs(PyObject* args,
-                                                                                       MT_Vector3& pos,
-                                                                                       MT_Vector3& pos2)
-{
-       PyObject* pylist;
-       PyObject* pylist2;
-       bool error = (PyArg_ParseTuple(args,"OO",&pylist,&pylist2)) != 0;
-
-       pos = ConvertPythonPylist(pylist);
-       pos2 = ConvertPythonPylist(pylist2);
-               
-       return error;
-}
-*/
-
 PyObject* KX_GameObject::PyReplaceMesh(PyObject* args)
 {
        KX_Scene *scene = KX_GetActiveScene();
@@ -1301,25 +1602,18 @@ PyObject* KX_GameObject::PyReinstancePhysicsMesh(PyObject* args)
                ) {
                return NULL;
        }
-       
+#ifdef USE_BULLET
        /* gameobj and mesh can be NULL */
        if(KX_ReInstanceBulletShapeFromMesh(this, gameobj, mesh))
                Py_RETURN_TRUE;
-
+#endif
        Py_RETURN_FALSE;
 }
 
-
-PyObject* KX_GameObject::PyGetPosition()
-{
-       ShowDeprecationWarning("getPosition()", "the position property");
-       return PyObjectFrom(NodeGetWorldPosition());
-}
-
 static PyObject *Map_GetItem(PyObject *self_v, PyObject *item)
 {
        KX_GameObject* self= static_cast<KX_GameObject*>BGE_PROXY_REF(self_v);
-       const char *attr_str= PyString_AsString(item);
+       const char *attr_str= _PyUnicode_AsString(item);
        CValue* resultattr;
        PyObject* pyconvert;
        
@@ -1353,7 +1647,7 @@ static PyObject *Map_GetItem(PyObject *self_v, PyObject *item)
 static int Map_SetItem(PyObject *self_v, PyObject *key, PyObject *val)
 {
        KX_GameObject* self= static_cast<KX_GameObject*>BGE_PROXY_REF(self_v);
-       const char *attr_str= PyString_AsString(key);
+       const char *attr_str= _PyUnicode_AsString(key);
        if(attr_str==NULL)
                PyErr_Clear();
        
@@ -1385,7 +1679,7 @@ static int Map_SetItem(PyObject *self_v, PyObject *key, PyObject *val)
                int set= 0;
                
                /* as CValue */
-               if(attr_str && BGE_PROXY_CHECK_TYPE(val)==0) /* dont allow GameObjects for eg to be assigned to CValue props */
+               if(attr_str && PyObject_TypeCheck(val, &PyObjectPlus::Type)==0) /* dont allow GameObjects for eg to be assigned to CValue props */
                {
                        CValue* vallie = self->ConvertPythonToValue(val, ""); /* error unused */
                        
@@ -1447,7 +1741,7 @@ static int Seq_Contains(PyObject *self_v, PyObject *value)
                return -1;
        }
        
-       if(PyString_Check(value) && self->GetProperty(PyString_AsString(value)))
+       if(PyUnicode_Check(value) && self->GetProperty(_PyUnicode_AsString(value)))
                return 1;
        
        if (self->m_attr_dict && PyDict_GetItem(self->m_attr_dict, value))
@@ -1472,53 +1766,42 @@ PySequenceMethods KX_GameObject::Sequence = {
        NULL,           /* sq_ass_item */
        NULL,           /* sq_ass_slice */
        (objobjproc)Seq_Contains,       /* sq_contains */
+       (binaryfunc) NULL, /* sq_inplace_concat */
+       (ssizeargfunc) NULL, /* sq_inplace_repeat */
 };
 
 PyTypeObject KX_GameObject::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_GameObject",
-               sizeof(PyObjectPlus_Proxy),
-               0,
-               py_base_dealloc,
-               0,
-               0,
-               0,
-               0,
-               py_base_repr,
-               0,
-               &Sequence,
-               &Mapping,
-               0,0,0,
-               py_base_getattro,
-               py_base_setattro,
-               0,
-               Py_TPFLAGS_DEFAULT,
-               0,0,0,0,0,0,0,
-               Methods
-};
-
-
-
-
-
-
-PyParentObject KX_GameObject::Parents[] = {
-       &KX_GameObject::Type,
-               &SCA_IObject::Type,
-               &CValue::Type,
-               NULL
+       "KX_GameObject",
+       sizeof(PyObjectPlus_Proxy),
+       0,
+       py_base_dealloc,
+       0,
+       0,
+       0,
+       0,
+       py_base_repr,
+       0,
+       &Sequence,
+       &Mapping,
+       0,0,0,
+       NULL,
+       NULL,
+       0,
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+       0,0,0,0,0,0,0,
+       Methods,
+       0,
+       0,
+       &SCA_IObject::Type,
+       0,0,0,0,0,0,
+       py_base_new
 };
 
 PyObject* KX_GameObject::pyattr_get_name(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
        KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
-       return PyString_FromString(self->GetName().ReadPtr());
+       return PyUnicode_FromString(self->GetName().ReadPtr());
 }
 
 PyObject* KX_GameObject::pyattr_get_parent(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
@@ -1532,6 +1815,19 @@ PyObject* KX_GameObject::pyattr_get_parent(void *self_v, const KX_PYATTRIBUTE_DE
        Py_RETURN_NONE;
 }
 
+PyObject* KX_GameObject::pyattr_get_life(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
+
+       CValue *life = self->GetProperty("::timebomb");
+       if (life)
+               // this convert the timebomb seconds to frames, hard coded 50.0 (assuming 50fps)
+               // value hardcoded in KX_Scene::AddReplicaObject()
+               return PyFloat_FromDouble(life->GetNumber() * 50.0);
+       else
+               Py_RETURN_NONE;
+}
+
 PyObject* KX_GameObject::pyattr_get_mass(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
        KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
@@ -1624,8 +1920,12 @@ int KX_GameObject::pyattr_set_visible(void *self_v, const KX_PYATTRIBUTE_DEF *at
 
 PyObject* KX_GameObject::pyattr_get_worldPosition(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
+#ifdef USE_MATHUTILS
+       return newVectorObject_cb(BGE_PROXY_FROM_REF(self_v), 3, mathutils_kxgameob_vector_cb_index, MATHUTILS_VEC_CB_POS_GLOBAL);
+#else
        KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
        return PyObjectFrom(self->NodeGetWorldPosition());
+#endif
 }
 
 int KX_GameObject::pyattr_set_worldPosition(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
@@ -1642,11 +1942,12 @@ int KX_GameObject::pyattr_set_worldPosition(void *self_v, const KX_PYATTRIBUTE_D
 
 PyObject* KX_GameObject::pyattr_get_localPosition(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
+#ifdef USE_MATHUTILS   
+       return newVectorObject_cb(BGE_PROXY_FROM_REF(self_v), 3, mathutils_kxgameob_vector_cb_index, MATHUTILS_VEC_CB_POS_LOCAL);
+#else  
        KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
-       if (self->GetSGNode())
-               return PyObjectFrom(self->GetSGNode()->GetLocalPosition());
-       else
-               return PyObjectFrom(dummy_point);
+       return PyObjectFrom(self->NodeGetLocalPosition());
+#endif
 }
 
 int KX_GameObject::pyattr_set_localPosition(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
@@ -1663,18 +1964,24 @@ int KX_GameObject::pyattr_set_localPosition(void *self_v, const KX_PYATTRIBUTE_D
 
 PyObject* KX_GameObject::pyattr_get_localInertia(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
+#ifdef USE_MATHUTILS
+       return newVectorObject_cb(BGE_PROXY_FROM_REF(self_v), 3, mathutils_kxgameob_vector_cb_index, MATHUTILS_VEC_CB_INERTIA_LOCAL);
+#else
        KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
        if (self->GetPhysicsController())
-       {
                return PyObjectFrom(self->GetPhysicsController()->GetLocalInertia());
-       }
        return Py_BuildValue("fff", 0.0f, 0.0f, 0.0f);
+#endif
 }
 
 PyObject* KX_GameObject::pyattr_get_worldOrientation(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
+#ifdef USE_MATHUTILS
+       return newMatrixObject_cb(BGE_PROXY_FROM_REF(self_v), 3, 3, mathutils_kxgameob_matrix_cb_index, MATHUTILS_MAT_CB_ORI_GLOBAL);
+#else
        KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
        return PyObjectFrom(self->NodeGetWorldOrientation());
+#endif
 }
 
 int KX_GameObject::pyattr_set_worldOrientation(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
@@ -1686,12 +1993,7 @@ int KX_GameObject::pyattr_set_worldOrientation(void *self_v, const KX_PYATTRIBUT
        if (!PyOrientationTo(value, rot, "gameOb.worldOrientation = sequence: KX_GameObject, "))
                return PY_SET_ATTR_FAIL;
 
-       if (self->GetSGNode() && self->GetSGNode()->GetSGParent()) {
-               self->NodeSetLocalOrientation(self->GetSGNode()->GetSGParent()->GetWorldOrientation().inverse()*rot);
-       }
-       else {
-               self->NodeSetLocalOrientation(rot);
-       }
+       self->NodeSetGlobalOrientation(rot);
        
        self->NodeUpdateGS(0.f);
        return PY_SET_ATTR_SUCCESS;
@@ -1699,11 +2001,12 @@ int KX_GameObject::pyattr_set_worldOrientation(void *self_v, const KX_PYATTRIBUT
 
 PyObject* KX_GameObject::pyattr_get_localOrientation(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
+#ifdef USE_MATHUTILS
+       return newMatrixObject_cb(BGE_PROXY_FROM_REF(self_v), 3, 3, mathutils_kxgameob_matrix_cb_index, MATHUTILS_MAT_CB_ORI_LOCAL);
+#else
        KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
-       if (self->GetSGNode())
-               return PyObjectFrom(self->GetSGNode()->GetLocalOrientation());
-       else
-               return PyObjectFrom(dummy_orientation);
+       return PyObjectFrom(self->NodeGetLocalOrientation());
+#endif
 }
 
 int KX_GameObject::pyattr_set_localOrientation(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
@@ -1722,17 +2025,22 @@ int KX_GameObject::pyattr_set_localOrientation(void *self_v, const KX_PYATTRIBUT
 
 PyObject* KX_GameObject::pyattr_get_worldScaling(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
+#ifdef USE_MATHUTILS
+       return newVectorObject_cb(BGE_PROXY_FROM_REF(self_v), 3, mathutils_kxgameob_vector_cb_index, MATHUTILS_VEC_CB_SCALE_GLOBAL);
+#else
        KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
        return PyObjectFrom(self->NodeGetWorldScaling());
+#endif
 }
 
 PyObject* KX_GameObject::pyattr_get_localScaling(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
+#ifdef USE_MATHUTILS
+       return newVectorObject_cb(BGE_PROXY_FROM_REF(self_v), 3, mathutils_kxgameob_vector_cb_index, MATHUTILS_VEC_CB_SCALE_LOCAL);
+#else
        KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
-       if (self->GetSGNode())
-               return PyObjectFrom(self->GetSGNode()->GetLocalScale());
-       else
-               return PyObjectFrom(dummy_scaling);
+       return PyObjectFrom(self->NodeGetLocalScaling());
+#endif
 }
 
 int KX_GameObject::pyattr_set_localScaling(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
@@ -1747,6 +2055,96 @@ int KX_GameObject::pyattr_set_localScaling(void *self_v, const KX_PYATTRIBUTE_DE
        return PY_SET_ATTR_SUCCESS;
 }
 
+
+PyObject* KX_GameObject::pyattr_get_worldLinearVelocity(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+#ifdef USE_MATHUTILS
+       return newVectorObject_cb(BGE_PROXY_FROM_REF(self_v), 3, mathutils_kxgameob_vector_cb_index, MATHUTILS_VEC_CB_LINVEL_GLOBAL);
+#else
+       KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
+       return PyObjectFrom(GetLinearVelocity(false));
+#endif
+}
+
+int KX_GameObject::pyattr_set_worldLinearVelocity(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
+{
+       KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
+       MT_Vector3 velocity;
+       if (!PyVecTo(value, velocity))
+               return PY_SET_ATTR_FAIL;
+
+       self->setLinearVelocity(velocity, false);
+
+       return PY_SET_ATTR_SUCCESS;
+}
+
+PyObject* KX_GameObject::pyattr_get_localLinearVelocity(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+#ifdef USE_MATHUTILS
+       return newVectorObject_cb(BGE_PROXY_FROM_REF(self_v), 3, mathutils_kxgameob_vector_cb_index, MATHUTILS_VEC_CB_LINVEL_LOCAL);
+#else
+       KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
+       return PyObjectFrom(GetLinearVelocity(true));
+#endif
+}
+
+int KX_GameObject::pyattr_set_localLinearVelocity(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
+{
+       KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
+       MT_Vector3 velocity;
+       if (!PyVecTo(value, velocity))
+               return PY_SET_ATTR_FAIL;
+
+       self->setLinearVelocity(velocity, true);
+
+       return PY_SET_ATTR_SUCCESS;
+}
+
+PyObject* KX_GameObject::pyattr_get_worldAngularVelocity(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+#ifdef USE_MATHUTILS
+       return newVectorObject_cb(BGE_PROXY_FROM_REF(self_v), 3, mathutils_kxgameob_vector_cb_index, MATHUTILS_VEC_CB_ANGVEL_GLOBAL);
+#else
+       KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
+       return PyObjectFrom(GetAngularVelocity(false));
+#endif
+}
+
+int KX_GameObject::pyattr_set_worldAngularVelocity(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
+{
+       KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
+       MT_Vector3 velocity;
+       if (!PyVecTo(value, velocity))
+               return PY_SET_ATTR_FAIL;
+
+       self->setAngularVelocity(velocity, false);
+
+       return PY_SET_ATTR_SUCCESS;
+}
+
+PyObject* KX_GameObject::pyattr_get_localAngularVelocity(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+#ifdef USE_MATHUTILS
+       return newVectorObject_cb(BGE_PROXY_FROM_REF(self_v), 3, mathutils_kxgameob_vector_cb_index, MATHUTILS_VEC_CB_ANGVEL_LOCAL);
+#else
+       KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
+       return PyObjectFrom(GetAngularVelocity(true));
+#endif
+}
+
+int KX_GameObject::pyattr_set_localAngularVelocity(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
+{
+       KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
+       MT_Vector3 velocity;
+       if (!PyVecTo(value, velocity))
+               return PY_SET_ATTR_FAIL;
+
+       self->setAngularVelocity(velocity, true);
+
+       return PY_SET_ATTR_SUCCESS;
+}
+
+
 PyObject* KX_GameObject::pyattr_get_timeOffset(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
        KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
@@ -1779,13 +2177,13 @@ PyObject* KX_GameObject::pyattr_get_state(void *self_v, const KX_PYATTRIBUTE_DEF
        KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
        int state = 0;
        state |= self->GetState();
-       return PyInt_FromLong(state);
+       return PyLong_FromSsize_t(state);
 }
 
 int KX_GameObject::pyattr_set_state(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
 {
        KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
-       int state_i = PyInt_AsLong(value);
+       int state_i = PyLong_AsSsize_t(value);
        unsigned int state = 0;
        
        if (state_i == -1 && PyErr_Occurred()) {
@@ -1817,7 +2215,28 @@ PyObject* KX_GameObject::pyattr_get_meshes(void *self_v, const KX_PYATTRIBUTE_DE
        return meshes;
 }
 
-/* experemental! */
+PyObject* KX_GameObject::pyattr_get_obcolor(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+#ifdef USE_MATHUTILS
+       return newVectorObject_cb(BGE_PROXY_FROM_REF(self_v), 4, mathutils_kxgameob_vector_cb_index, MATHUTILS_VEC_CB_OBJECT_COLOR);
+#else
+       KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
+       return PyObjectFrom(self->GetObjectColor());
+#endif
+}
+
+int KX_GameObject::pyattr_set_obcolor(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
+{
+       KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
+       MT_Vector4 obcolor;
+       if (!PyVecTo(value, obcolor))
+               return PY_SET_ATTR_FAIL;
+
+       self->SetObjectColor(obcolor);
+       return PY_SET_ATTR_SUCCESS;
+}
+
+/* These are experimental! */
 PyObject* KX_GameObject::pyattr_get_sensors(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
        return KX_PythonSeq_CreatePyObject((static_cast<KX_GameObject*>(self_v))->m_proxy, KX_PYGENSEQ_OB_TYPE_SENSORS);
@@ -1832,6 +2251,7 @@ PyObject* KX_GameObject::pyattr_get_actuators(void *self_v, const KX_PYATTRIBUTE
 {
        return KX_PythonSeq_CreatePyObject((static_cast<KX_GameObject*>(self_v))->m_proxy, KX_PYGENSEQ_OB_TYPE_ACTUATORS);
 }
+/* End experimental */
 
 PyObject* KX_GameObject::pyattr_get_children(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
@@ -1856,128 +2276,6 @@ PyObject* KX_GameObject::pyattr_get_attrDict(void *self_v, const KX_PYATTRIBUTE_
        return self->m_attr_dict;
 }
 
-/* We need these because the macros have a return in them */
-PyObject* KX_GameObject::py_getattro__internal(PyObject *attr)
-{
-       py_getattro_up(SCA_IObject);
-}
-
-int KX_GameObject::py_setattro__internal(PyObject *attr, PyObject *value)      // py_setattro method
-{
-       py_setattro_up(SCA_IObject);
-}
-
-
-PyObject* KX_GameObject::py_getattro(PyObject *attr)
-{
-       PyObject *object= py_getattro__internal(attr);
-       
-       if (object==NULL && m_attr_dict)
-       {
-               /* backup the exception incase the attr doesnt exist in the dict either */
-               PyObject *err_type, *err_value, *err_tb;
-               PyErr_Fetch(&err_type, &err_value, &err_tb);
-               
-               object= PyDict_GetItem(m_attr_dict, attr);
-               if (object) {
-                       Py_INCREF(object);
-                       
-                       PyErr_Clear();
-                       Py_XDECREF( err_type );
-                       Py_XDECREF( err_value );
-                       Py_XDECREF( err_tb );
-               }
-               else {
-                       PyErr_Restore(err_type, err_value, err_tb); /* use the error from the parent function */
-               }
-       }
-       return object;
-}
-
-PyObject* KX_GameObject::py_getattro_dict() {
-       //py_getattro_dict_up(SCA_IObject);
-       PyObject *dict= py_getattr_dict(SCA_IObject::py_getattro_dict(), Type.tp_dict);
-       if(dict==NULL)
-               return NULL;
-       
-       /* normally just return this but KX_GameObject has some more items */
-
-       
-       /* Not super fast getting as a list then making into dict keys but its only for dir() */
-       PyObject *list= ConvertKeysToPython();
-       if(list)
-       {
-               int i;
-               for(i=0; i<PyList_Size(list); i++)
-                       PyDict_SetItem(dict, PyList_GET_ITEM(list, i), Py_None);
-       }
-       else
-               PyErr_Clear();
-       
-       Py_DECREF(list);
-       
-       /* Add m_attr_dict if we have it */
-       if(m_attr_dict)
-               PyDict_Update(dict, m_attr_dict);
-       
-       return dict;
-}
-
-int KX_GameObject::py_setattro(PyObject *attr, PyObject *value)        // py_setattro method
-{
-       int ret= py_setattro__internal(attr, value);
-       
-       if (ret==PY_SET_ATTR_SUCCESS) {
-               /* remove attribute in our own dict to avoid double ups */
-               /* NOTE: Annoying that we also do this for setting builtin attributes like mass and visibility :/ */
-               if (m_attr_dict) {
-                       if (PyDict_DelItem(m_attr_dict, attr) != 0)
-                               PyErr_Clear();
-               }
-       }
-       
-       if (ret==PY_SET_ATTR_COERCE_FAIL) {
-               /* CValue attribute exists, remove CValue and add PyDict value */
-               RemoveProperty(PyString_AsString(attr));
-               ret= PY_SET_ATTR_MISSING;
-       }
-       
-       if (ret==PY_SET_ATTR_MISSING) {
-               /* Lazy initialization */
-               if (m_attr_dict==NULL)
-                       m_attr_dict = PyDict_New();
-               
-               if (PyDict_SetItem(m_attr_dict, attr, value)==0) {
-                       PyErr_Clear();
-                       ret= PY_SET_ATTR_SUCCESS;
-               }
-               else {
-                       PyErr_Format(PyExc_AttributeError, "gameOb.myAttr = value: KX_GameObject, failed assigning value to internal dictionary");
-                       ret= PY_SET_ATTR_FAIL;
-               }
-       }
-       
-       return ret;     
-}
-
-
-int    KX_GameObject::py_delattro(PyObject *attr)
-{
-       ShowDeprecationWarning("del ob.attr", "del ob['attr'] for user defined properties");
-       
-       char *attr_str= PyString_AsString(attr); 
-       
-       if (RemoveProperty(attr_str)) // XXX - should call CValues instead but its only 2 lines here
-               return PY_SET_ATTR_SUCCESS;
-       
-       if (m_attr_dict && (PyDict_DelItem(m_attr_dict, attr) == 0))
-               return PY_SET_ATTR_SUCCESS;
-       
-       PyErr_Format(PyExc_AttributeError, "del gameOb.myAttr: KX_GameObject, attribute \"%s\" dosnt exist", attr_str);
-       return PY_SET_ATTR_MISSING;
-}
-
-
 PyObject* KX_GameObject::PyApplyForce(PyObject* args)
 {
        int local = 0;
@@ -2118,41 +2416,6 @@ PyObject* KX_GameObject::PySetOcclusion(PyObject* args)
        Py_RETURN_NONE;
 }
 
-PyObject* KX_GameObject::PyGetVisible()
-{
-       ShowDeprecationWarning("getVisible()", "the visible property");
-       return PyInt_FromLong(m_bVisible);      
-}
-
-PyObject* KX_GameObject::PyGetState()
-{
-       ShowDeprecationWarning("getState()", "the state property");
-       int state = 0;
-       state |= GetState();
-       return PyInt_FromLong(state);
-}
-
-PyObject* KX_GameObject::PySetState(PyObject* value)
-{
-       ShowDeprecationWarning("setState()", "the state property");
-       int state_i = PyInt_AsLong(value);
-       unsigned int state = 0;
-       
-       if (state_i == -1 && PyErr_Occurred()) {
-               PyErr_SetString(PyExc_TypeError, "expected an int bit field");
-               return NULL;
-       }
-       
-       state |= state_i;
-       if ((state & ((1<<30)-1)) == 0) {
-               PyErr_SetString(PyExc_AttributeError, "The state bitfield was not between 0 and 30 (1<<0 and 1<<29)");
-               return NULL;
-       }
-       SetState(state);
-       
-       Py_RETURN_NONE;
-}
-
 PyObject* KX_GameObject::PyGetVelocity(PyObject* args)
 {
        // only can get the velocity if we have a physics object connected to us...
@@ -2171,14 +2434,6 @@ PyObject* KX_GameObject::PyGetVelocity(PyObject* args)
        }
 }
 
-
-
-PyObject* KX_GameObject::PyGetMass()
-{
-       ShowDeprecationWarning("getMass()", "the mass property");
-       return PyFloat_FromDouble((GetPhysicsController() != NULL) ? GetPhysicsController()->GetMass() : 0.0f);
-}
-
 PyObject* KX_GameObject::PyGetReactionForce()
 {
        // only can get the velocity if we have a physics object connected to us...
@@ -2215,18 +2470,6 @@ PyObject* KX_GameObject::PyDisableRigidBody()
 }
 
 
-
-PyObject* KX_GameObject::PyGetParent()
-{
-       ShowDeprecationWarning("getParent()", "the parent property");
-       KX_GameObject* parent = this->GetParent();
-       if (parent) {
-               parent->Release(); /* self->GetParent() AddRef's */
-               return parent->GetProxy();
-       }
-       Py_RETURN_NONE;
-}
-
 PyObject* KX_GameObject::PySetParent(PyObject* args)
 {
        KX_Scene *scene = KX_GetActiveScene();
@@ -2252,41 +2495,6 @@ PyObject* KX_GameObject::PyRemoveParent()
        Py_RETURN_NONE;
 }
 
-PyObject* KX_GameObject::PyGetChildren()
-{
-       ShowDeprecationWarning("getChildren()", "the children property");
-       
-       return GetChildren()->NewProxy(true);
-}
-
-PyObject* KX_GameObject::PyGetChildrenRecursive()
-{
-       ShowDeprecationWarning("getChildrenRecursive()", "the childrenRecursive property");
-       
-       return GetChildrenRecursive()->NewProxy(true);
-}
-
-PyObject* KX_GameObject::PyGetMesh(PyObject* args)
-{
-       ShowDeprecationWarning("getMesh()", "the meshes property (now a list of meshes)");
-       
-       int mesh = 0;
-
-       if (!PyArg_ParseTuple(args, "|i:getMesh", &mesh))
-               return NULL; // python sets a simple error
-       
-       if (((unsigned int)mesh < m_meshes.size()) && mesh >= 0)
-       {
-               KX_MeshProxy* meshproxy = new KX_MeshProxy(m_meshes[mesh]);
-               return meshproxy->NewProxy(true); // XXX Todo Python own.
-       }
-       
-       Py_RETURN_NONE;
-}
-
-
-
-
 
 PyObject* KX_GameObject::PySetCollisionMargin(PyObject* value)
 {
@@ -2350,29 +2558,6 @@ PyObject* KX_GameObject::PyRestoreDynamics()
 }
 
 
-
-PyObject* KX_GameObject::PyGetOrientation() //keywords
-{
-       ShowDeprecationWarning("getOrientation()", "the orientation property");
-       return PyObjectFrom(NodeGetWorldOrientation());
-}
-
-
-
-PyObject* KX_GameObject::PySetOrientation(PyObject* value)
-{
-       ShowDeprecationWarning("setOrientation()", "the orientation property");
-       MT_Matrix3x3 rot;
-       
-       /* if value is not a sequence PyOrientationTo makes an error */
-       if (!PyOrientationTo(value, rot, "gameOb.setOrientation(sequence): KX_GameObject, "))
-               return NULL;
-
-       NodeSetLocalOrientation(rot);
-       NodeUpdateGS(0.f);
-       Py_RETURN_NONE;
-}
-
 PyObject* KX_GameObject::PyAlignAxisToVect(PyObject* args)
 {
        PyObject* pyvect;
@@ -2405,33 +2590,6 @@ PyObject* KX_GameObject::PyGetAxisVect(PyObject* value)
        return NULL;
 }
 
-PyObject* KX_GameObject::PySetPosition(PyObject* value)
-{
-       ShowDeprecationWarning("setPosition()", "the localPosition property");
-       MT_Point3 pos;
-       if (PyVecTo(value, pos))
-       {
-               NodeSetLocalPosition(pos);
-               NodeUpdateGS(0.f);
-               Py_RETURN_NONE;
-       }
-
-       return NULL;
-}
-
-PyObject* KX_GameObject::PySetWorldPosition(PyObject* value)
-{
-       ShowDeprecationWarning("setWorldPosition()", "the worldPosition property");
-       MT_Point3 pos;
-       if (PyVecTo(value, pos))
-       {
-               NodeSetWorldPosition(pos);
-               NodeUpdateGS(0.f);
-               Py_RETURN_NONE;
-       }
-
-       return NULL;
-}
 
 PyObject* KX_GameObject::PyGetPhysicsId()
 {
@@ -2441,7 +2599,7 @@ PyObject* KX_GameObject::PyGetPhysicsId()
        {
                physid= (uint_ptr)ctrl->GetUserData();
        }
-       return PyInt_FromLong((long)physid);
+       return PyLong_FromSsize_t((long)physid);
 }
 
 PyObject* KX_GameObject::PyGetPropertyNames()
@@ -2617,7 +2775,7 @@ KX_PYMETHODDEF_DOC(KX_GameObject, rayCastTo,
        KX_RayCast::Callback<KX_GameObject> callback(this,spc);
        KX_RayCast::RayTest(pe, fromPoint, toPoint, callback);
 
-    if (m_pHitObject)
+       if (m_pHitObject)
                return m_pHitObject->GetProxy();
        
        Py_RETURN_NONE;
@@ -2651,9 +2809,26 @@ static PyObject *none_tuple_4()
        return ret;
 }
 
+static PyObject *none_tuple_5()
+{
+       PyObject *ret= PyTuple_New(5);
+       PyTuple_SET_ITEM(ret, 0, Py_None);
+       PyTuple_SET_ITEM(ret, 1, Py_None);
+       PyTuple_SET_ITEM(ret, 2, Py_None);
+       PyTuple_SET_ITEM(ret, 3, Py_None);
+       PyTuple_SET_ITEM(ret, 4, Py_None);
+       
+       Py_INCREF(Py_None);
+       Py_INCREF(Py_None);
+       Py_INCREF(Py_None);
+       Py_INCREF(Py_None);
+       Py_INCREF(Py_None);
+       return ret;
+}
+
 KX_PYMETHODDEF_DOC(KX_GameObject, rayCast,
-                                  "rayCast(to,from,dist,prop,face,xray,poly): cast a ray and return 3-tuple (object,hit,normal) or 4-tuple (object,hit,normal,polygon) of contact point with object within dist that matches prop.\n"
-                                  " If no hit, return (None,None,None) or (None,None,None,None).\n"
+                                  "rayCast(to,from,dist,prop,face,xray,poly): cast a ray and return 3-tuple (object,hit,normal) or 4-tuple (object,hit,normal,polygon) or 4-tuple (object,hit,normal,polygon,hituv) of contact point with object within dist that matches prop.\n"
+                                  " If no hit, return (None,None,None) or (None,None,None,None) or (None,None,None,None,None).\n"
 " to   = 3-tuple or object reference for destination of ray (if object, use center of object)\n"
 " from = 3-tuple or object reference for origin of ray (if object, use center of object)\n"
 "        Can be None or omitted => start from self object center\n"
@@ -2663,6 +2838,8 @@ KX_PYMETHODDEF_DOC(KX_GameObject, rayCast,
 " xray = X-ray option: 1=>skip objects that don't match prop; 0 or omitted => stop on first object\n"
 " poly = polygon option: 1=>return value is a 4-tuple and the 4th element is a KX_PolyProxy object\n"
 "                           which can be None if hit object has no mesh or if there is no hit\n"
+"                        2=>return value is a 5-tuple, the 4th element is the KX_PolyProxy object\n"
+"                           and the 5th element is the vector of UV coordinates at the hit point of the None if there is no UV mapping\n"
 "        If 0 or omitted, return value is a 3-tuple\n"
 "Note: The object on which you call this method matters: the ray will ignore it.\n"
 "      prop and xray option interact as follow:\n"
@@ -2743,12 +2920,12 @@ KX_PYMETHODDEF_DOC(KX_GameObject, rayCast,
                m_testPropName.SetLength(0);
        m_xray = xray;
        // to get the hit results
-       KX_RayCast::Callback<KX_GameObject> callback(this,spc,NULL,face);
+       KX_RayCast::Callback<KX_GameObject> callback(this,spc,NULL,face,(poly==2));
        KX_RayCast::RayTest(pe, fromPoint, toPoint, callback);
 
        if (m_pHitObject)
        {
-               PyObject* returnValue = (poly) ? PyTuple_New(4) : PyTuple_New(3);
+               PyObject* returnValue = (poly==2) ? PyTuple_New(5) : (poly) ? PyTuple_New(4) : PyTuple_New(3);
                if (returnValue) { // unlikely this would ever fail, if it does python sets an error
                        PyTuple_SET_ITEM(returnValue, 0, m_pHitObject->GetProxy());
                        PyTuple_SET_ITEM(returnValue, 1, PyObjectFrom(callback.m_hitPoint));
@@ -2761,22 +2938,36 @@ KX_PYMETHODDEF_DOC(KX_GameObject, rayCast,
                                        RAS_Polygon* polygon = callback.m_hitMesh->GetPolygon(callback.m_hitPolygon);
                                        KX_PolyProxy* polyproxy = new KX_PolyProxy(callback.m_hitMesh, polygon);
                                        PyTuple_SET_ITEM(returnValue, 3, polyproxy->NewProxy(true));
+                                       if (poly == 2)
+                                       {
+                                               if (callback.m_hitUVOK)
+                                                       PyTuple_SET_ITEM(returnValue, 4, PyObjectFrom(callback.m_hitUV));
+                                               else {
+                                                       Py_INCREF(Py_None);
+                                                       PyTuple_SET_ITEM(returnValue, 4, Py_None);
+                                               }
+                                       }
                                }
                                else
                                {
                                        Py_INCREF(Py_None);
                                        PyTuple_SET_ITEM(returnValue, 3, Py_None);
+                                       if (poly==2)
+                                       {
+                                               Py_INCREF(Py_None);
+                                               PyTuple_SET_ITEM(returnValue, 4, Py_None);
+                                       }
                                }
                        }
                }
                return returnValue;
        }
        // no hit
-       if (poly)
-               //return Py_BuildValue("OOOO", Py_None, Py_None, Py_None, Py_None);
+       if (poly == 2)
+               return none_tuple_5();
+       else if (poly)
                return none_tuple_4();
        else
-               //return Py_BuildValue("OOO", Py_None, Py_None, Py_None);
                return none_tuple_3();
 }
 
@@ -2814,8 +3005,8 @@ PyObject* KX_GameObject::Pyget(PyObject *args)
                return NULL;
        
        
-       if(PyString_Check(key)) {
-               CValue *item = GetProperty(PyString_AsString(key));
+       if(PyUnicode_Check(key)) {
+               CValue *item = GetProperty(_PyUnicode_AsString(key));
                if (item) {
                        ret = item->ConvertValueToPython();
                        if(ret)
@@ -2834,37 +3025,6 @@ PyObject* KX_GameObject::Pyget(PyObject *args)
        return def;
 }
 
-/* Matches python dict.has_key() */
-PyObject* KX_GameObject::Pyhas_key(PyObject* value)
-{
-       // the ONLY error case is invalid data, this is checked by the macro'd static function
-       // that calls this one. but make sure Seq_Contains doesnt add extra errors later on.
-       return PyBool_FromLong(Seq_Contains((PyObject *)this, value));
-}
-
-/* --------------------------------------------------------------------- 
- * Some stuff taken from the header
- * --------------------------------------------------------------------- */
-void KX_GameObject::Relink(GEN_Map<GEN_HashedPtr, void*> *map_parameter)       
-{
-       // we will relink the sensors and actuators that use object references
-       // if the object is part of the replicated hierarchy, use the new
-       // object reference instead
-       SCA_SensorList& sensorlist = GetSensors();
-       SCA_SensorList::iterator sit;
-       for (sit=sensorlist.begin(); sit != sensorlist.end(); sit++)
-       {
-               (*sit)->Relink(map_parameter);
-       }
-       SCA_ActuatorList& actuatorlist = GetActuators();
-       SCA_ActuatorList::iterator ait;
-       for (ait=actuatorlist.begin(); ait != actuatorlist.end(); ait++)
-       {
-               (*ait)->Relink(map_parameter);
-       }
-}
-
-
 bool ConvertPythonToGameObject(PyObject * value, KX_GameObject **object, bool py_none_ok, const char *error_prefix)
 {
        if (value==NULL) {
@@ -2884,20 +3044,21 @@ bool ConvertPythonToGameObject(PyObject * value, KX_GameObject **object, bool py
                }
        }
        
-       if (PyString_Check(value)) {
-               *object = (KX_GameObject*)SCA_ILogicBrick::m_sCurrentLogicManager->GetGameObjectByName(STR_String( PyString_AsString(value) ));
+       if (PyUnicode_Check(value)) {
+               *object = (KX_GameObject*)SCA_ILogicBrick::m_sCurrentLogicManager->GetGameObjectByName(STR_String( _PyUnicode_AsString(value) ));
                
                if (*object) {
                        return true;
                } else {
-                       PyErr_Format(PyExc_ValueError, "%s, requested name \"%s\" did not match any KX_GameObject in this scene", error_prefix, PyString_AsString(value));
+                       PyErr_Format(PyExc_ValueError, "%s, requested name \"%s\" did not match any KX_GameObject in this scene", error_prefix, _PyUnicode_AsString(value));
                        return false;
                }
        }
        
        if (    PyObject_TypeCheck(value, &KX_GameObject::Type) ||
                        PyObject_TypeCheck(value, &KX_LightObject::Type)        ||
-                       PyObject_TypeCheck(value, &KX_Camera::Type)     )
+                       PyObject_TypeCheck(value, &KX_Camera::Type)                     ||
+                       PyObject_TypeCheck(value, &KX_FontObject::Type))
        {
                *object = static_cast<KX_GameObject*>BGE_PROXY_REF(value);
                
@@ -2920,3 +3081,4 @@ bool ConvertPythonToGameObject(PyObject * value, KX_GameObject **object, bool py
        
        return false;
 }
+#endif // WITH_PYTHON