- BGE, Some sensors, stored the event manager in 2 places (became confusing to do...
[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,PyTypeObject* T)
101 :SCA_ISensor(gameobj,eventmgr,T),
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 #if (PY_VERSION_HEX >= 0x02060000)
297         PyVarObject_HEAD_INIT(NULL, 0)
298 #else
299         /* python 2.5 and below */
300         PyObject_HEAD_INIT( NULL )  /* required py macro */
301         0,                          /* ob_size */
302 #endif
303         "KX_TouchSensor",
304         sizeof(PyObjectPlus_Proxy),
305         0,
306         py_base_dealloc,
307         0,
308         0,
309         0,
310         0,
311         py_base_repr,
312         0,0,0,0,0,0,
313         py_base_getattro,
314         py_base_setattro,
315         0,0,0,0,0,0,0,0,0,
316         Methods
317 };
318
319 PyParentObject KX_TouchSensor::Parents[] = {
320         &KX_TouchSensor::Type,
321         &SCA_ISensor::Type,
322         &SCA_ILogicBrick::Type,
323         &CValue::Type,
324         NULL
325 };
326
327 PyMethodDef KX_TouchSensor::Methods[] = {
328         //Deprecated ----->
329         {"setProperty", 
330          (PyCFunction) KX_TouchSensor::sPySetProperty,      METH_O, (PY_METHODCHAR)SetProperty_doc},
331         {"getProperty", 
332          (PyCFunction) KX_TouchSensor::sPyGetProperty,      METH_NOARGS, (PY_METHODCHAR)GetProperty_doc},
333         {"getHitObject", 
334          (PyCFunction) KX_TouchSensor::sPyGetHitObject,     METH_NOARGS, (PY_METHODCHAR)GetHitObject_doc},
335         {"getHitObjectList", 
336          (PyCFunction) KX_TouchSensor::sPyGetHitObjectList, METH_NOARGS, (PY_METHODCHAR)GetHitObjectList_doc},
337          //<-----
338         {NULL,NULL} //Sentinel
339 };
340
341 PyAttributeDef KX_TouchSensor::Attributes[] = {
342         KX_PYATTRIBUTE_STRING_RW("propName",0,100,false,KX_TouchSensor,m_touchedpropname),
343         KX_PYATTRIBUTE_BOOL_RW("useMaterial",KX_TouchSensor,m_bFindMaterial),
344         KX_PYATTRIBUTE_BOOL_RW("usePulseCollision",KX_TouchSensor,m_bTouchPulse),
345         KX_PYATTRIBUTE_RO_FUNCTION("hitObject", KX_TouchSensor, pyattr_get_object_hit),
346         KX_PYATTRIBUTE_RO_FUNCTION("hitObjectList", KX_TouchSensor, pyattr_get_object_hit_list),
347         { NULL }        //Sentinel
348 };
349
350 PyObject* KX_TouchSensor::py_getattro(PyObject *attr)
351 {
352         py_getattro_up(SCA_ISensor);
353 }
354
355 PyObject* KX_TouchSensor::py_getattro_dict() {
356         py_getattro_dict_up(SCA_ISensor);
357 }
358
359 int KX_TouchSensor::py_setattro(PyObject *attr, PyObject *value)
360 {
361         py_setattro_up(SCA_ISensor);
362 }
363
364 /* Python API */
365
366 /* 1. setProperty */
367 const char KX_TouchSensor::SetProperty_doc[] = 
368 "setProperty(name)\n"
369 "\t- name: string\n"
370 "\tSet the property or material to collide with. Use\n"
371 "\tsetTouchMaterial() to switch between properties and\n"
372 "\tmaterials.";
373 PyObject* KX_TouchSensor::PySetProperty(PyObject* value)
374 {
375         ShowDeprecationWarning("setProperty()", "the propName property");
376         char *nameArg= PyString_AsString(value);
377         if (nameArg==NULL) {
378                 PyErr_SetString(PyExc_ValueError, "expected a ");
379                 return NULL;
380         }
381         
382         m_touchedpropname = nameArg;
383         Py_RETURN_NONE;
384 }
385 /* 2. getProperty */
386 const char KX_TouchSensor::GetProperty_doc[] = 
387 "getProperty(name)\n"
388 "\tReturns the property or material to collide with. Use\n"
389 "\tgetTouchMaterial() to find out whether this sensor\n"
390 "\tlooks for properties or materials.";
391 PyObject*  KX_TouchSensor::PyGetProperty() {
392         ShowDeprecationWarning("getProperty()", "the propName property");
393         
394         return PyString_FromString(m_touchedpropname);
395 }
396
397 const char KX_TouchSensor::GetHitObject_doc[] = 
398 "getHitObject()\n"
399 ;
400 PyObject* KX_TouchSensor::PyGetHitObject()
401 {
402         ShowDeprecationWarning("getHitObject()", "the hitObject property");
403         /* to do: do Py_IncRef if the object is already known in Python */
404         /* otherwise, this leaks memory */
405         if (m_hitObject)
406         {
407                 return m_hitObject->GetProxy();
408         }
409         Py_RETURN_NONE;
410 }
411
412 const char KX_TouchSensor::GetHitObjectList_doc[] = 
413 "getHitObjectList()\n"
414 "\tReturn a list of the objects this object collided with,\n"
415 "\tbut only those matching the property/material condition.\n";
416 PyObject* KX_TouchSensor::PyGetHitObjectList()
417 {
418         ShowDeprecationWarning("getHitObjectList()", "the hitObjectList property");
419         /* to do: do Py_IncRef if the object is already known in Python */
420         /* otherwise, this leaks memory */ /* Edit, this seems ok and not to leak memory - Campbell */
421         return m_colliders->GetProxy();
422 }
423
424 /*getTouchMaterial and setTouchMaterial were never added to the api,
425 they can probably be removed with out anyone noticing*/
426
427 /* 5. getTouchMaterial */
428 const char KX_TouchSensor::GetTouchMaterial_doc[] = 
429 "getTouchMaterial()\n"
430 "\tReturns KX_TRUE if this sensor looks for a specific material,\n"
431 "\tKX_FALSE if it looks for a specific property.\n" ;
432 PyObject* KX_TouchSensor::PyGetTouchMaterial()
433 {
434         ShowDeprecationWarning("getTouchMaterial()", "the useMaterial property");
435         return PyInt_FromLong(m_bFindMaterial);
436 }
437
438 /* 6. setTouchMaterial */
439 #if 0
440 const char KX_TouchSensor::SetTouchMaterial_doc[] = 
441 "setTouchMaterial(flag)\n"
442 "\t- flag: KX_TRUE or KX_FALSE.\n"
443 "\tSet flag to KX_TRUE to switch on positive pulse mode,\n"
444 "\tKX_FALSE to switch off positive pulse mode.\n" ;
445 PyObject* KX_TouchSensor::PySetTouchMaterial(PyObject *value)
446 {
447         ShowDeprecationWarning("setTouchMaterial()", "the useMaterial property");
448         int pulseArg = PyInt_AsLong(value);
449
450         if(pulseArg ==-1 && PyErr_Occurred()) {
451                 PyErr_SetString(PyExc_ValueError, "expected a bool");
452                 return NULL;
453         }
454         
455         m_bFindMaterial = pulseArg != 0;
456
457         Py_RETURN_NONE;
458 }
459 #endif
460
461 PyObject* KX_TouchSensor::pyattr_get_object_hit(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
462 {
463         KX_TouchSensor* self= static_cast<KX_TouchSensor*>(self_v);
464         
465         if (self->m_hitObject)
466                 return self->m_hitObject->GetProxy();
467         else
468                 Py_RETURN_NONE;
469 }
470
471 PyObject* KX_TouchSensor::pyattr_get_object_hit_list(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
472 {
473         KX_TouchSensor* self= static_cast<KX_TouchSensor*>(self_v);
474         return self->m_colliders->GetProxy();
475 }
476
477
478 /* eof */