remove python2.x support
[blender.git] / source / gameengine / GameLogic / SCA_IObject.cpp
index 826e7bbdf0e28f7ee4415a1a5d5e75b67dff9ad9..2bffd029bd4d980606b4357d524c1579604b1a9b 100644 (file)
 #endif
 
 MT_Point3 SCA_IObject::m_sDummy=MT_Point3(0,0,0);
+SG_QList SCA_IObject::m_activeBookmarkedControllers;
 
-SCA_IObject::SCA_IObject(PyTypeObject* T): m_state(0), CValue(T)
+SCA_IObject::SCA_IObject():
+       CValue(),
+       m_initState(0),
+       m_state(0),
+       m_firstState(NULL)
 {
        m_suspended = false;
 }
-       
-
 
 SCA_IObject::~SCA_IObject()
 {
@@ -59,7 +62,9 @@ SCA_IObject::~SCA_IObject()
        SCA_ControllerList::iterator itc; 
        for (itc = m_controllers.begin(); !(itc == m_controllers.end()); ++itc)
        {
-               ((CValue*)(*itc))->Release();
+               //Use Delete for controller to ensure proper cleaning (expression controller)
+               (*itc)->Delete();
+               //((CValue*)(*itc))->Release();
        }
        SCA_ActuatorList::iterator ita;
        for (ita = m_registeredActuators.begin(); !(ita==m_registeredActuators.end()); ++ita)
@@ -68,7 +73,7 @@ SCA_IObject::~SCA_IObject()
        }
        for (ita = m_actuators.begin(); !(ita==m_actuators.end()); ++ita)
        {
-               ((CValue*)(*ita))->Release();
+               (*ita)->Delete();
        }
 
        //T_InterpolatorList::iterator i;
@@ -77,29 +82,6 @@ SCA_IObject::~SCA_IObject()
        //}
 }
 
-
-
-SCA_ControllerList& SCA_IObject::GetControllers()
-{
-       return m_controllers;
-}
-
-
-
-SCA_SensorList& SCA_IObject::GetSensors()
-{
-       return m_sensors;
-}
-
-
-
-SCA_ActuatorList& SCA_IObject::GetActuators()
-{
-       return m_actuators;
-}
-
-
-
 void SCA_IObject::AddSensor(SCA_ISensor* act)
 {
        act->AddRef();
@@ -131,7 +113,7 @@ void SCA_IObject::RegisterActuator(SCA_IActuator* act)
 void SCA_IObject::UnregisterActuator(SCA_IActuator* act)
 {
        SCA_ActuatorList::iterator ita;
-       for (ita = m_registeredActuators.begin(); ita != m_registeredActuators.end(); ita++)
+       for (ita = m_registeredActuators.begin(); ita != m_registeredActuators.end(); ++ita)
        {
                if ((*ita) == act) {
                        (*ita) = m_registeredActuators.back();
@@ -141,31 +123,19 @@ void SCA_IObject::UnregisterActuator(SCA_IActuator* act)
        }
 }
 
-void SCA_IObject::SetIgnoreActivityCulling(bool b)
-{
-       m_ignore_activity_culling = b;
-}
-
-
-
-bool SCA_IObject::GetIgnoreActivityCulling()
-{
-       return m_ignore_activity_culling;
-}
-
-
-
 void SCA_IObject::ReParentLogic()
 {
-       SCA_SensorList& oldsensors = GetSensors();
-       
-       int sen = 0;
-       SCA_SensorList::iterator its;
-       for (its = oldsensors.begin(); !(its==oldsensors.end()); ++its)
+       SCA_ActuatorList& oldactuators  = GetActuators();
+       int act = 0;
+       SCA_ActuatorList::iterator ita;
+       for (ita = oldactuators.begin(); !(ita==oldactuators.end()); ++ita)
        {
-               SCA_ISensor* newsensor = (SCA_ISensor*)(*its)->GetReplica();
-               newsensor->ReParent(this);
-               oldsensors[sen++] = newsensor;
+               SCA_IActuator* newactuator = (SCA_IActuator*) (*ita)->GetReplica();
+               newactuator->ReParent(this);
+               // actuators are initially not connected to any controller
+               newactuator->SetActive(false);
+               newactuator->ClrLink();
+               oldactuators[act++] = newactuator;
        }
 
        SCA_ControllerList& oldcontrollers = GetControllers();
@@ -175,20 +145,24 @@ void SCA_IObject::ReParentLogic()
        {
                SCA_IController* newcontroller = (SCA_IController*)(*itc)->GetReplica();
                newcontroller->ReParent(this);
+               newcontroller->SetActive(false);
                oldcontrollers[con++]=newcontroller;
 
        }
-       SCA_ActuatorList& oldactuators  = GetActuators();
-       
-       int act = 0;
-       SCA_ActuatorList::iterator ita;
-       for (ita = oldactuators.begin(); !(ita==oldactuators.end()); ++ita)
+       // convert sensors last so that actuators are already available for Actuator sensor
+       SCA_SensorList& oldsensors = GetSensors();
+       int sen = 0;
+       SCA_SensorList::iterator its;
+       for (its = oldsensors.begin(); !(its==oldsensors.end()); ++its)
        {
-               SCA_IActuator* newactuator = (SCA_IActuator*) (*ita)->GetReplica();
-               newactuator->ReParent(this);
-               newactuator->SetActive(false);
-               oldactuators[act++] = newactuator;
+               SCA_ISensor* newsensor = (SCA_ISensor*)(*its)->GetReplica();
+               newsensor->ReParent(this);
+               newsensor->SetActive(false);
+               // sensors are initially not connected to any controller
+               newsensor->ClrLink();
+               oldsensors[sen++] = newsensor;
        }
+
        // a new object cannot be client of any actuator
        m_registeredActuators.clear();
                
@@ -200,7 +174,7 @@ SCA_ISensor* SCA_IObject::FindSensor(const STR_String& sensorname)
 {
        SCA_ISensor* foundsensor = NULL;
 
-       for (SCA_SensorList::iterator its = m_sensors.begin();!(its==m_sensors.end());its++)
+       for (SCA_SensorList::iterator its = m_sensors.begin();!(its==m_sensors.end());++its)
        {
                if ((*its)->GetName() == sensorname)
                {
@@ -217,7 +191,7 @@ SCA_IController* SCA_IObject::FindController(const STR_String& controllername)
 {
        SCA_IController* foundcontroller = NULL;
 
-       for (SCA_ControllerList::iterator itc = m_controllers.begin();!(itc==m_controllers.end());itc++)
+       for (SCA_ControllerList::iterator itc = m_controllers.begin();!(itc==m_controllers.end());++itc)
        {
                if ((*itc)->GetName() == controllername)
                {
@@ -234,7 +208,7 @@ SCA_IActuator* SCA_IObject::FindActuator(const STR_String& actuatorname)
 {
        SCA_IActuator* foundactuator = NULL;
 
-       for (SCA_ActuatorList::iterator ita = m_actuators.begin();!(ita==m_actuators.end());ita++)
+       for (SCA_ActuatorList::iterator ita = m_actuators.begin();!(ita==m_actuators.end());++ita)
        {
                if ((*ita)->GetName() == actuatorname)
                {
@@ -247,60 +221,7 @@ SCA_IActuator* SCA_IObject::FindActuator(const STR_String& actuatorname)
 }
 
 
-
-void SCA_IObject::SetCurrentTime(float currentTime) {
-       //T_InterpolatorList::iterator i;
-       //for (i = m_interpolators.begin(); !(i == m_interpolators.end()); ++i) {
-       //      (*i)->Execute(currentTime);
-       //}
-}
-       
-
-#if 0
-const MT_Point3& SCA_IObject::ConvertPythonPylist(PyObject* pylist)
-{
-       bool error = false;
-       m_sDummy = MT_Vector3(0,0,0);
-       if (pylist->ob_type == &CListValue::Type)
-       {
-               CListValue* listval = (CListValue*) pylist;
-               int numelem = listval->GetCount();
-               if ( numelem <= 3)
-               {
-                       int index;
-                       for (index = 0;index<numelem;index++)
-                       {
-                               m_sDummy[index] = listval->GetValue(index)->GetNumber();
-                       }
-               }       else
-               {
-                       error = true;
-               }
-               
-       } else
-       {
-               
-               // assert the list is long enough...
-               int numitems = PyList_Size(pylist);
-               if (numitems <= 3)
-               {
-                       int index;
-                       for (index=0;index<numitems;index++)
-                       {
-                               m_sDummy[index] = PyFloat_AsDouble(PyList_GetItem(pylist,index));
-                       }
-               }
-               else
-               {
-                       error = true;
-               }
-
-       }
-       return m_sDummy;
-}
-#endif
-
-void SCA_IObject::Suspend(void)
+void SCA_IObject::Suspend()
 {
        if ((!m_ignore_activity_culling) 
                && (!m_suspended))  {
@@ -309,7 +230,7 @@ void SCA_IObject::Suspend(void)
                SCA_SensorList::iterator i = m_sensors.begin();
                while (i != m_sensors.end()) {
                        (*i)->Suspend();
-                       i++;
+                       ++i;
                }
        }
 }
@@ -324,19 +245,38 @@ void SCA_IObject::Resume(void)
                SCA_SensorList::iterator i = m_sensors.begin();
                while (i != m_sensors.end()) {
                        (*i)->Resume();
-                       i++;
+                       ++i;
                }
        }
 }
 
 void SCA_IObject::SetState(unsigned int state)
 {
-       m_state = state;
-       // update the status of the controllers
+       unsigned int tmpstate;
        SCA_ControllerList::iterator contit;
-       for (contit = m_controllers.begin(); contit != m_controllers.end(); contit++)
+
+       // we will update the state in two steps:
+       // 1) set the new state bits that are 1
+       // 2) clr the new state bits that are 0
+       // This to ensure continuity if a sensor is attached to two states
+       // that are switching state: no need to deactive and reactive the sensor 
+       
+       tmpstate = m_state | state;
+       if (tmpstate != m_state)
+       {
+               // update the status of the controllers
+               for (contit = m_controllers.begin(); contit != m_controllers.end(); ++contit)
+               {
+                       (*contit)->ApplyState(tmpstate);
+               }
+       }
+       m_state = state;
+       if (m_state != tmpstate)
        {
-               (*contit)->ApplyState(m_state);
+               for (contit = m_controllers.begin(); contit != m_controllers.end(); ++contit)
+               {
+                       (*contit)->ApplyState(m_state);
+               }
        }
 }
 
@@ -348,43 +288,33 @@ void SCA_IObject::SetState(unsigned int state)
 
 /* Integration hooks ------------------------------------------------------- */
 PyTypeObject SCA_IObject::Type = {
-       PyObject_HEAD_INIT(&PyType_Type)
-       0,
+       PyVarObject_HEAD_INIT(NULL, 0)
        "SCA_IObject",
-       sizeof(SCA_IObject),
+       sizeof(PyObjectPlus_Proxy),
        0,
-       PyDestructor,
+       py_base_dealloc,
        0,
-       __getattr,
-       __setattr,
-       0, //&MyPyCompare,
-       __repr,
-       0, //&cvalue_as_number,
        0,
        0,
        0,
-       0
-};
-
-
-
-PyParentObject SCA_IObject::Parents[] = {
-       &SCA_IObject::Type,
+       py_base_repr,
+       0,0,0,0,0,0,0,0,0,
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+       0,0,0,0,0,0,0,
+       Methods,
+       0,
+       0,
        &CValue::Type,
-       NULL
+       0,0,0,0,0,0,
+       py_base_new
 };
 
-
-
 PyMethodDef SCA_IObject::Methods[] = {
        //{"setOrientation", (PyCFunction) SCA_IObject::sPySetOrientation, METH_VARARGS},
        //{"getOrientation", (PyCFunction) SCA_IObject::sPyGetOrientation, METH_VARARGS},
        {NULL,NULL} //Sentinel
 };
 
-
-
-PyObject* SCA_IObject::_getattr(const STR_String& attr) {
-       _getattr_up(CValue);
-}
-
+PyAttributeDef SCA_IObject::Attributes[] = {
+       { NULL }        //Sentinel
+};