remove all python api functions deprecated in 2.49
[blender.git] / source / gameengine / Ketsji / KX_TouchSensor.cpp
1 /**
2  * Senses touch and collision events
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #include "KX_TouchSensor.h"
33 #include "SCA_EventManager.h"
34 #include "SCA_LogicManager.h"
35 #include "KX_GameObject.h"
36 #include "KX_TouchEventManager.h"
37
38 #include "PHY_IPhysicsController.h"
39
40 #include <iostream>
41 #include "PHY_IPhysicsEnvironment.h"
42
43 #ifdef HAVE_CONFIG_H
44 #include <config.h>
45 #endif
46
47 /* ------------------------------------------------------------------------- */
48 /* Native functions                                                          */
49 /* ------------------------------------------------------------------------- */
50
51 void KX_TouchSensor::SynchronizeTransform()
52 {
53         // the touch sensor does not require any synchronization: it uses
54         // the same physical object which is already synchronized by Blender
55 }
56
57
58 void KX_TouchSensor::EndFrame() {
59         m_colliders->ReleaseAndRemoveAll();
60         m_hitObject = NULL;
61         m_bTriggered = false;
62         m_bColliderHash = 0;
63 }
64
65 void KX_TouchSensor::UnregisterToManager()
66 {
67         // before unregistering the sensor, make sure we release all references
68         EndFrame();
69         SCA_ISensor::UnregisterToManager();
70 }
71
72 bool KX_TouchSensor::Evaluate()
73 {
74         bool result = false;
75         bool reset = m_reset && m_level;
76         m_reset = false;
77         if (m_bTriggered != m_bLastTriggered)
78         {
79                 m_bLastTriggered = m_bTriggered;
80                 if (!m_bTriggered)
81                         m_hitObject = NULL;
82                 result = true;
83         }
84         if (reset)
85                 // force an event
86                 result = true;
87         
88         if (m_bTouchPulse) { /* pulse on changes to the colliders */
89                 int count = m_colliders->GetCount();
90                 
91                 if (m_bLastCount!=count || m_bColliderHash!=m_bLastColliderHash) {
92                         m_bLastCount = count;
93                         m_bLastColliderHash= m_bColliderHash;
94                         result = true;
95                 }
96         }
97         return result;
98 }
99
100 KX_TouchSensor::KX_TouchSensor(SCA_EventManager* eventmgr,KX_GameObject* gameobj,bool bFindMaterial,bool bTouchPulse,const STR_String& touchedpropname)
101 :SCA_ISensor(gameobj,eventmgr),
102 m_touchedpropname(touchedpropname),
103 m_bFindMaterial(bFindMaterial),
104 m_bTouchPulse(bTouchPulse)
105 /*m_sumoObj(sumoObj),*/
106 {
107 //      KX_TouchEventManager* touchmgr = (KX_TouchEventManager*) eventmgr;
108 //      m_resptable = touchmgr->GetResponseTable();
109         
110 //      m_solidHandle = m_sumoObj->getObjectHandle();
111
112         m_colliders = new CListValue();
113         
114         KX_ClientObjectInfo *client_info = gameobj->getClientInfo();
115         //client_info->m_gameobject = gameobj;
116         //client_info->m_auxilary_info = NULL;
117         client_info->m_sensors.push_back(this);
118         
119         m_physCtrl = dynamic_cast<PHY_IPhysicsController*>(gameobj->GetPhysicsController());
120         MT_assert( !gameobj->GetPhysicsController() || m_physCtrl );
121         Init();
122 }
123
124 void KX_TouchSensor::Init()
125 {
126         m_bCollision = false;
127         m_bTriggered = false;
128         m_bLastTriggered = (m_invert)?true:false;
129         m_bLastCount = 0;
130         m_bColliderHash = m_bLastColliderHash = 0;
131         m_hitObject =  NULL;
132         m_reset = true;
133 }
134
135 KX_TouchSensor::~KX_TouchSensor()
136 {
137         //DT_ClearObjectResponse(m_resptable,m_solidHandle);
138         m_colliders->Release();
139 }
140
141 CValue* KX_TouchSensor::GetReplica() 
142 {
143         KX_TouchSensor* replica = new KX_TouchSensor(*this);
144         replica->ProcessReplica();
145         return replica;
146 }
147
148 void KX_TouchSensor::ProcessReplica()
149 {
150         SCA_ISensor::ProcessReplica();
151         m_colliders = new CListValue();
152         Init();
153 }
154
155 void    KX_TouchSensor::ReParent(SCA_IObject* parent)
156 {
157         KX_GameObject *gameobj = static_cast<KX_GameObject *>(parent);
158         PHY_IPhysicsController *sphy = dynamic_cast<PHY_IPhysicsController*>(((KX_GameObject*)parent)->GetPhysicsController());
159         if (sphy)
160                 m_physCtrl = sphy;
161         
162 //      m_solidHandle = m_sumoObj->getObjectHandle();
163         KX_ClientObjectInfo *client_info = gameobj->getClientInfo();
164         //client_info->m_gameobject = gameobj;
165         //client_info->m_auxilary_info = NULL;
166         
167         client_info->m_sensors.push_back(this);
168         SCA_ISensor::ReParent(parent);
169 }
170
171 void KX_TouchSensor::RegisterSumo(KX_TouchEventManager *touchman)
172 {
173         if (m_physCtrl)
174         {
175                 if (touchman->GetPhysicsEnvironment()->requestCollisionCallback(m_physCtrl))
176                 {
177                         KX_ClientObjectInfo* client_info = static_cast<KX_ClientObjectInfo*>(m_physCtrl->getNewClientInfo());
178                         if (client_info->isSensor())
179                                 touchman->GetPhysicsEnvironment()->addSensor(m_physCtrl);
180                 }
181         }
182 }
183 void KX_TouchSensor::UnregisterSumo(KX_TouchEventManager* touchman)
184 {
185         if (m_physCtrl)
186         {
187                 if (touchman->GetPhysicsEnvironment()->removeCollisionCallback(m_physCtrl))
188                 {
189                         // no more sensor on the controller, can remove it if it is a sensor object
190                         KX_ClientObjectInfo* client_info = static_cast<KX_ClientObjectInfo*>(m_physCtrl->getNewClientInfo());
191                         if (client_info->isSensor())
192                                 touchman->GetPhysicsEnvironment()->removeSensor(m_physCtrl);
193                 }
194         }
195 }
196
197 // this function is called only for sensor objects
198 // return true if the controller can collide with the object
199 bool    KX_TouchSensor::BroadPhaseSensorFilterCollision(void*obj1,void*obj2)
200 {
201         assert(obj1==m_physCtrl && obj2);
202
203         KX_GameObject* myobj = (KX_GameObject*)GetParent();
204         KX_GameObject* myparent = myobj->GetParent();
205         KX_ClientObjectInfo* client_info = static_cast<KX_ClientObjectInfo*>(((PHY_IPhysicsController*)obj2)->getNewClientInfo());
206         KX_ClientObjectInfo* my_client_info = static_cast<KX_ClientObjectInfo*>(m_physCtrl->getNewClientInfo());
207         KX_GameObject* otherobj = ( client_info ? client_info->m_gameobject : NULL);
208
209         // first, decrement refcount as GetParent() increases it
210         if (myparent)
211                 myparent->Release();
212
213         // we can only check on persistent characteristic: m_link and m_suspended are not
214         // good candidate because they are transient. That must be handled at another level
215         if (!otherobj ||
216                 otherobj == myparent ||         // don't interact with our parent
217                 (my_client_info->m_type == KX_ClientObjectInfo::OBACTORSENSOR &&
218                  client_info->m_type != KX_ClientObjectInfo::ACTOR))    // only with actor objects
219                 return false;
220                 
221         bool found = m_touchedpropname.IsEmpty();
222         if (!found)
223         {
224                 if (m_bFindMaterial)
225                 {
226                         if (client_info->m_auxilary_info)
227                         {
228                                 found = (!strcmp(m_touchedpropname.Ptr(), (char*)client_info->m_auxilary_info));
229                         }
230                 } else
231                 {
232                         found = (otherobj->GetProperty(m_touchedpropname) != NULL);
233                 }
234         }
235         return found;
236 }
237
238 bool    KX_TouchSensor::NewHandleCollision(void*object1,void*object2,const PHY_CollData* colldata)
239 {
240 //      KX_TouchEventManager* toucheventmgr = (KX_TouchEventManager*)m_eventmgr;
241         KX_GameObject* parent = (KX_GameObject*)GetParent();
242
243         // need the mapping from PHY_IPhysicsController to gameobjects now
244         
245         KX_ClientObjectInfo* client_info = static_cast<KX_ClientObjectInfo*> (object1 == m_physCtrl? 
246                                         ((PHY_IPhysicsController*)object2)->getNewClientInfo(): 
247                                         ((PHY_IPhysicsController*)object1)->getNewClientInfo());
248
249         KX_GameObject* gameobj = ( client_info ? 
250                         client_info->m_gameobject : 
251                         NULL);
252         
253         // add the same check as in SCA_ISensor::Activate(), 
254         // we don't want to record collision when the sensor is not active.
255         if (m_links && !m_suspended &&
256                 gameobj && (gameobj != parent) && client_info->isActor())
257         {
258                 
259                 bool found = m_touchedpropname.IsEmpty();
260                 if (!found)
261                 {
262                         if (m_bFindMaterial)
263                         {
264                                 if (client_info->m_auxilary_info)
265                                 {
266                                         found = (!strcmp(m_touchedpropname.Ptr(), (char*)client_info->m_auxilary_info));
267                                 }
268                         } else
269                         {
270                                 found = (gameobj->GetProperty(m_touchedpropname) != NULL);
271                         }
272                 }
273                 if (found)
274                 {
275                         if (!m_colliders->SearchValue(gameobj)) {
276                                 m_colliders->Add(gameobj->AddRef());
277                                 
278                                 if (m_bTouchPulse)
279                                         m_bColliderHash += (uint_ptr)(static_cast<void *>(&gameobj));
280                         }
281                         m_bTriggered = true;
282                         m_hitObject = gameobj;
283                         //printf("KX_TouchSensor::HandleCollision\n");
284                 }
285                 
286         } 
287         return false; // was DT_CONTINUE but this was defined in sumo as false.
288 }
289
290
291 /* ------------------------------------------------------------------------- */
292 /* Python functions                                                          */
293 /* ------------------------------------------------------------------------- */
294 /* Integration hooks ------------------------------------------------------- */
295 PyTypeObject KX_TouchSensor::Type = {
296         PyVarObject_HEAD_INIT(NULL, 0)
297         "KX_TouchSensor",
298         sizeof(PyObjectPlus_Proxy),
299         0,
300         py_base_dealloc,
301         0,
302         0,
303         0,
304         0,
305         py_base_repr,
306         0,0,0,0,0,0,0,0,0,
307         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
308         0,0,0,0,0,0,0,
309         Methods,
310         0,
311         0,
312         &SCA_ISensor::Type,
313         0,0,0,0,0,0,
314         py_base_new
315 };
316
317 PyMethodDef KX_TouchSensor::Methods[] = {
318         {NULL,NULL} //Sentinel
319 };
320
321 PyAttributeDef KX_TouchSensor::Attributes[] = {
322         KX_PYATTRIBUTE_STRING_RW("propName",0,100,false,KX_TouchSensor,m_touchedpropname),
323         KX_PYATTRIBUTE_BOOL_RW("useMaterial",KX_TouchSensor,m_bFindMaterial),
324         KX_PYATTRIBUTE_BOOL_RW("usePulseCollision",KX_TouchSensor,m_bTouchPulse),
325         KX_PYATTRIBUTE_RO_FUNCTION("hitObject", KX_TouchSensor, pyattr_get_object_hit),
326         KX_PYATTRIBUTE_RO_FUNCTION("hitObjectList", KX_TouchSensor, pyattr_get_object_hit_list),
327         { NULL }        //Sentinel
328 };
329
330 /* Python API */
331
332 PyObject* KX_TouchSensor::pyattr_get_object_hit(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
333 {
334         KX_TouchSensor* self= static_cast<KX_TouchSensor*>(self_v);
335         
336         if (self->m_hitObject)
337                 return self->m_hitObject->GetProxy();
338         else
339                 Py_RETURN_NONE;
340 }
341
342 PyObject* KX_TouchSensor::pyattr_get_object_hit_list(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
343 {
344         KX_TouchSensor* self= static_cast<KX_TouchSensor*>(self_v);
345         return self->m_colliders->GetProxy();
346 }
347
348
349 /* eof */