synched with trunk at revision 32129
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 /* ------------------------------------------------------------------------- */
44 /* Native functions                                                          */
45 /* ------------------------------------------------------------------------- */
46
47 void KX_TouchSensor::SynchronizeTransform()
48 {
49         // the touch sensor does not require any synchronization: it uses
50         // the same physical object which is already synchronized by Blender
51 }
52
53
54 void KX_TouchSensor::EndFrame() {
55         m_colliders->ReleaseAndRemoveAll();
56         m_hitObject = NULL;
57         m_bTriggered = false;
58         m_bColliderHash = 0;
59 }
60
61 void KX_TouchSensor::UnregisterToManager()
62 {
63         // before unregistering the sensor, make sure we release all references
64         EndFrame();
65         SCA_ISensor::UnregisterToManager();
66 }
67
68 bool KX_TouchSensor::Evaluate()
69 {
70         bool result = false;
71         bool reset = m_reset && m_level;
72         m_reset = false;
73         if (m_bTriggered != m_bLastTriggered)
74         {
75                 m_bLastTriggered = m_bTriggered;
76                 if (!m_bTriggered)
77                         m_hitObject = NULL;
78                 result = true;
79         }
80         if (reset)
81                 // force an event
82                 result = true;
83         
84         if (m_bTouchPulse) { /* pulse on changes to the colliders */
85                 int count = m_colliders->GetCount();
86                 
87                 if (m_bLastCount!=count || m_bColliderHash!=m_bLastColliderHash) {
88                         m_bLastCount = count;
89                         m_bLastColliderHash= m_bColliderHash;
90                         result = true;
91                 }
92         }
93         return result;
94 }
95
96 KX_TouchSensor::KX_TouchSensor(SCA_EventManager* eventmgr,KX_GameObject* gameobj,bool bFindMaterial,bool bTouchPulse,const STR_String& touchedpropname)
97 :SCA_ISensor(gameobj,eventmgr),
98 m_touchedpropname(touchedpropname),
99 m_bFindMaterial(bFindMaterial),
100 m_bTouchPulse(bTouchPulse)
101 /*m_sumoObj(sumoObj),*/
102 {
103 //      KX_TouchEventManager* touchmgr = (KX_TouchEventManager*) eventmgr;
104 //      m_resptable = touchmgr->GetResponseTable();
105         
106 //      m_solidHandle = m_sumoObj->getObjectHandle();
107
108         m_colliders = new CListValue();
109         
110         KX_ClientObjectInfo *client_info = gameobj->getClientInfo();
111         //client_info->m_gameobject = gameobj;
112         //client_info->m_auxilary_info = NULL;
113         client_info->m_sensors.push_back(this);
114         
115         m_physCtrl = dynamic_cast<PHY_IPhysicsController*>(gameobj->GetPhysicsController());
116         MT_assert( !gameobj->GetPhysicsController() || m_physCtrl );
117         Init();
118 }
119
120 void KX_TouchSensor::Init()
121 {
122         m_bCollision = false;
123         m_bTriggered = false;
124         m_bLastTriggered = (m_invert)?true:false;
125         m_bLastCount = 0;
126         m_bColliderHash = m_bLastColliderHash = 0;
127         m_hitObject =  NULL;
128         m_reset = true;
129 }
130
131 KX_TouchSensor::~KX_TouchSensor()
132 {
133         //DT_ClearObjectResponse(m_resptable,m_solidHandle);
134         m_colliders->Release();
135 }
136
137 CValue* KX_TouchSensor::GetReplica() 
138 {
139         KX_TouchSensor* replica = new KX_TouchSensor(*this);
140         replica->ProcessReplica();
141         return replica;
142 }
143
144 void KX_TouchSensor::ProcessReplica()
145 {
146         SCA_ISensor::ProcessReplica();
147         m_colliders = new CListValue();
148         Init();
149 }
150
151 void    KX_TouchSensor::ReParent(SCA_IObject* parent)
152 {
153         KX_GameObject *gameobj = static_cast<KX_GameObject *>(parent);
154         PHY_IPhysicsController *sphy = dynamic_cast<PHY_IPhysicsController*>(((KX_GameObject*)parent)->GetPhysicsController());
155         if (sphy)
156                 m_physCtrl = sphy;
157         
158 //      m_solidHandle = m_sumoObj->getObjectHandle();
159         KX_ClientObjectInfo *client_info = gameobj->getClientInfo();
160         //client_info->m_gameobject = gameobj;
161         //client_info->m_auxilary_info = NULL;
162         
163         client_info->m_sensors.push_back(this);
164         SCA_ISensor::ReParent(parent);
165 }
166
167 void KX_TouchSensor::RegisterSumo(KX_TouchEventManager *touchman)
168 {
169         if (m_physCtrl)
170         {
171                 if (touchman->GetPhysicsEnvironment()->requestCollisionCallback(m_physCtrl))
172                 {
173                         KX_ClientObjectInfo* client_info = static_cast<KX_ClientObjectInfo*>(m_physCtrl->getNewClientInfo());
174                         if (client_info->isSensor())
175                                 touchman->GetPhysicsEnvironment()->addSensor(m_physCtrl);
176                 }
177         }
178 }
179 void KX_TouchSensor::UnregisterSumo(KX_TouchEventManager* touchman)
180 {
181         if (m_physCtrl)
182         {
183                 if (touchman->GetPhysicsEnvironment()->removeCollisionCallback(m_physCtrl))
184                 {
185                         // no more sensor on the controller, can remove it if it is a sensor object
186                         KX_ClientObjectInfo* client_info = static_cast<KX_ClientObjectInfo*>(m_physCtrl->getNewClientInfo());
187                         if (client_info->isSensor())
188                                 touchman->GetPhysicsEnvironment()->removeSensor(m_physCtrl);
189                 }
190         }
191 }
192
193 // this function is called only for sensor objects
194 // return true if the controller can collide with the object
195 bool    KX_TouchSensor::BroadPhaseSensorFilterCollision(void*obj1,void*obj2)
196 {
197         assert(obj1==m_physCtrl && obj2);
198
199         KX_GameObject* myobj = (KX_GameObject*)GetParent();
200         KX_GameObject* myparent = myobj->GetParent();
201         KX_ClientObjectInfo* client_info = static_cast<KX_ClientObjectInfo*>(((PHY_IPhysicsController*)obj2)->getNewClientInfo());
202         KX_ClientObjectInfo* my_client_info = static_cast<KX_ClientObjectInfo*>(m_physCtrl->getNewClientInfo());
203         KX_GameObject* otherobj = ( client_info ? client_info->m_gameobject : NULL);
204
205         // first, decrement refcount as GetParent() increases it
206         if (myparent)
207                 myparent->Release();
208
209         // we can only check on persistent characteristic: m_link and m_suspended are not
210         // good candidate because they are transient. That must be handled at another level
211         if (!otherobj ||
212                 otherobj == myparent ||         // don't interact with our parent
213                 (my_client_info->m_type == KX_ClientObjectInfo::OBACTORSENSOR &&
214                  client_info->m_type != KX_ClientObjectInfo::ACTOR))    // only with actor objects
215                 return false;
216                 
217         bool found = m_touchedpropname.IsEmpty();
218         if (!found)
219         {
220                 if (m_bFindMaterial)
221                 {
222                         if (client_info->m_auxilary_info)
223                         {
224                                 found = (!strcmp(m_touchedpropname.Ptr(), (char*)client_info->m_auxilary_info));
225                         }
226                 } else
227                 {
228                         found = (otherobj->GetProperty(m_touchedpropname) != NULL);
229                 }
230         }
231         return found;
232 }
233
234 bool    KX_TouchSensor::NewHandleCollision(void*object1,void*object2,const PHY_CollData* colldata)
235 {
236 //      KX_TouchEventManager* toucheventmgr = (KX_TouchEventManager*)m_eventmgr;
237         KX_GameObject* parent = (KX_GameObject*)GetParent();
238
239         // need the mapping from PHY_IPhysicsController to gameobjects now
240         
241         KX_ClientObjectInfo* client_info = static_cast<KX_ClientObjectInfo*> (object1 == m_physCtrl? 
242                                         ((PHY_IPhysicsController*)object2)->getNewClientInfo(): 
243                                         ((PHY_IPhysicsController*)object1)->getNewClientInfo());
244
245         KX_GameObject* gameobj = ( client_info ? 
246                         client_info->m_gameobject : 
247                         NULL);
248         
249         // add the same check as in SCA_ISensor::Activate(), 
250         // we don't want to record collision when the sensor is not active.
251         if (m_links && !m_suspended &&
252                 gameobj && (gameobj != parent) && client_info->isActor())
253         {
254                 
255                 bool found = m_touchedpropname.IsEmpty();
256                 if (!found)
257                 {
258                         if (m_bFindMaterial)
259                         {
260                                 if (client_info->m_auxilary_info)
261                                 {
262                                         found = (!strcmp(m_touchedpropname.Ptr(), (char*)client_info->m_auxilary_info));
263                                 }
264                         } else
265                         {
266                                 found = (gameobj->GetProperty(m_touchedpropname) != NULL);
267                         }
268                 }
269                 if (found)
270                 {
271                         if (!m_colliders->SearchValue(gameobj)) {
272                                 m_colliders->Add(gameobj->AddRef());
273                                 
274                                 if (m_bTouchPulse)
275                                         m_bColliderHash += (uint_ptr)(static_cast<void *>(&gameobj));
276                         }
277                         m_bTriggered = true;
278                         m_hitObject = gameobj;
279                         //printf("KX_TouchSensor::HandleCollision\n");
280                 }
281                 
282         } 
283         return false; // was DT_CONTINUE but this was defined in sumo as false.
284 }
285
286 #ifndef DISABLE_PYTHON
287
288 /* ------------------------------------------------------------------------- */
289 /* Python functions                                                          */
290 /* ------------------------------------------------------------------------- */
291 /* Integration hooks ------------------------------------------------------- */
292 PyTypeObject KX_TouchSensor::Type = {
293         PyVarObject_HEAD_INIT(NULL, 0)
294         "KX_TouchSensor",
295         sizeof(PyObjectPlus_Proxy),
296         0,
297         py_base_dealloc,
298         0,
299         0,
300         0,
301         0,
302         py_base_repr,
303         0,0,0,0,0,0,0,0,0,
304         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
305         0,0,0,0,0,0,0,
306         Methods,
307         0,
308         0,
309         &SCA_ISensor::Type,
310         0,0,0,0,0,0,
311         py_base_new
312 };
313
314 PyMethodDef KX_TouchSensor::Methods[] = {
315         {NULL,NULL} //Sentinel
316 };
317
318 PyAttributeDef KX_TouchSensor::Attributes[] = {
319         KX_PYATTRIBUTE_STRING_RW("propName",0,100,false,KX_TouchSensor,m_touchedpropname),
320         KX_PYATTRIBUTE_BOOL_RW("useMaterial",KX_TouchSensor,m_bFindMaterial),
321         KX_PYATTRIBUTE_BOOL_RW("usePulseCollision",KX_TouchSensor,m_bTouchPulse),
322         KX_PYATTRIBUTE_RO_FUNCTION("hitObject", KX_TouchSensor, pyattr_get_object_hit),
323         KX_PYATTRIBUTE_RO_FUNCTION("hitObjectList", KX_TouchSensor, pyattr_get_object_hit_list),
324         { NULL }        //Sentinel
325 };
326
327 /* Python API */
328
329 PyObject* KX_TouchSensor::pyattr_get_object_hit(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
330 {
331         KX_TouchSensor* self= static_cast<KX_TouchSensor*>(self_v);
332         
333         if (self->m_hitObject)
334                 return self->m_hitObject->GetProxy();
335         else
336                 Py_RETURN_NONE;
337 }
338
339 PyObject* KX_TouchSensor::pyattr_get_object_hit_list(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
340 {
341         KX_TouchSensor* self= static_cast<KX_TouchSensor*>(self_v);
342         return self->m_colliders->GetProxy();
343 }
344
345 #endif
346
347 /* eof */