Initial revision
[blender.git] / source / gameengine / Ketsji / KX_TouchSensor.cpp
1 /**
2  * Senses touch and collision events
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL/BL DUAL 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. The Blender
12  * Foundation also sells licenses for use in proprietary software under
13  * the Blender License.  See http://www.blender.org/BL/ for information
14  * about this.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26  * All rights reserved.
27  *
28  * The Original Code is: all of this file.
29  *
30  * Contributor(s): none yet.
31  *
32  * ***** END GPL/BL DUAL LICENSE BLOCK *****
33  */
34
35 #include "KX_TouchSensor.h"
36 #include "SCA_EventManager.h"
37 #include "SCA_LogicManager.h"
38 #include "KX_GameObject.h"
39 #include "KX_TouchEventManager.h"
40 #include <iostream>
41
42 #ifdef PHYSICS_NOT_YET
43
44 /* ------------------------------------------------------------------------- */
45 /* Native functions                                                          */
46 /* ------------------------------------------------------------------------- */
47
48 void KX_TouchSensor::SynchronizeTransform()
49 {
50
51         if (m_sumoObj)
52         {
53                 m_sumoObj->setPosition(((KX_GameObject*)GetParent())->NodeGetWorldPosition());
54                 m_sumoObj->setOrientation(
55                         ((KX_GameObject*)GetParent())->NodeGetWorldOrientation().getRotation()
56                         );
57                 m_sumoObj->calcXform();
58         }
59         
60 }
61
62
63 void KX_TouchSensor::EndFrame() {
64         m_colliders->ReleaseAndRemoveAll();
65         m_bTriggered = false;
66 }
67
68 bool KX_TouchSensor::Evaluate(CValue* event)
69 {
70         bool result = false;
71
72         if (m_bTriggered != m_bLastTriggered)
73         {
74                 m_bLastTriggered = m_bTriggered;
75                 if (!m_bTriggered)
76                         m_hitObject = NULL;
77                 result = true;
78         }
79         
80         return result;
81 }
82
83 KX_TouchSensor::KX_TouchSensor(SCA_EventManager* eventmgr,KX_GameObject* gameobj,SM_Object* sumoObj,bool bFindMaterial,const STR_String& touchedpropname,PyTypeObject* T)
84 :SCA_ISensor(gameobj,eventmgr,T),
85 m_touchedpropname(touchedpropname),
86 m_bFindMaterial(bFindMaterial),
87 m_sumoObj(sumoObj),
88 m_bCollision(false),
89 m_bTriggered(false),
90 m_bLastTriggered(false)
91 {
92         m_eventmgr = eventmgr;
93         KX_TouchEventManager* touchmgr = (KX_TouchEventManager*) eventmgr;
94         
95         m_resptable = touchmgr->GetResponseTable();
96         
97         m_solidHandle = m_sumoObj->getObjectHandle();
98
99         m_hitObject =  NULL;
100         m_colliders = new CListValue();
101 }
102
103
104 KX_TouchSensor::~KX_TouchSensor()
105 {
106         DT_ClearObjectResponse(m_resptable,m_solidHandle);
107         m_colliders->Release();
108 }
109
110 void    KX_TouchSensor::ReParent(SCA_IObject* parent)
111 {
112
113         m_sumoObj = ((KX_GameObject*)parent)->GetSumoObject();
114         m_solidHandle = m_sumoObj->getObjectHandle();
115
116         m_client_info.m_clientobject = NULL;//parent;
117         m_client_info.m_auxilary_info = NULL;
118         SCA_ISensor::ReParent(parent);
119 }
120
121
122 void KX_TouchSensor::RegisterSumo()
123 {
124
125                 if (m_sumoObj)
126                 {
127                         // collision
128                         DT_SetObjectResponse(
129                                 m_resptable,
130                                 m_solidHandle,
131                                 collisionResponse,
132                                 DT_SIMPLE_RESPONSE,
133                                 this);
134
135                 }
136
137 }
138
139 void    KX_TouchSensor::HandleCollision(void* obj1,void* obj2,const DT_CollData * coll_data)
140 {
141         KX_TouchEventManager* toucheventmgr = (KX_TouchEventManager*)m_eventmgr;
142         KX_GameObject* parent = (KX_GameObject*)GetParent();
143
144         // need the mapping from SM_Objects to gameobjects now
145         
146         SM_ClientObjectInfo* client_info =(SM_ClientObjectInfo*) (obj1 == m_sumoObj? 
147                                         ((SM_Object*)obj2)->getClientObject() : 
148                                         ((SM_Object*)obj1)->getClientObject());
149
150
151         KX_GameObject* gameobj = ( client_info ? 
152                         (KX_GameObject*)client_info->m_clientobject : 
153                         NULL);
154
155         if (gameobj && (gameobj != parent))
156         {
157                 if (!m_colliders->SearchValue(gameobj))
158                         m_colliders->Add(gameobj->AddRef());
159                 
160                 bool found = m_touchedpropname.IsEmpty();
161                 if (!found)
162                 {
163                         if (m_bFindMaterial)
164                         {
165                                 if (client_info->m_auxilary_info)
166                                 {
167                                         found = (m_touchedpropname == ((char*)client_info->m_auxilary_info));
168                                 }
169
170                                 if (found)
171                                 {
172                                         int i=0;
173                                 }
174
175                         } else
176                         {
177                                 found = (gameobj->GetProperty(m_touchedpropname) != NULL);
178                         }
179                 }
180                 if (found)
181                 {
182                         m_bTriggered = true;
183                         m_hitObject = gameobj;
184                 }
185                 
186         } 
187         
188 }
189
190 /* ------------------------------------------------------------------------- */
191 /* Python functions                                                          */
192 /* ------------------------------------------------------------------------- */
193 /* Integration hooks ------------------------------------------------------- */
194 PyTypeObject KX_TouchSensor::Type = {
195         PyObject_HEAD_INIT(&PyType_Type)
196         0,
197         "KX_TouchSensor",
198         sizeof(KX_TouchSensor),
199         0,
200         PyDestructor,
201         0,
202         __getattr,
203         __setattr,
204         0, //&MyPyCompare,
205         __repr,
206         0, //&cvalue_as_number,
207         0,
208         0,
209         0,
210         0
211 };
212
213 PyParentObject KX_TouchSensor::Parents[] = {
214         &KX_TouchSensor::Type,
215         &SCA_ISensor::Type,
216         &SCA_ILogicBrick::Type,
217         &CValue::Type,
218         NULL
219 };
220
221 PyMethodDef KX_TouchSensor::Methods[] = {
222         {"setProperty", 
223          (PyCFunction) KX_TouchSensor::sPySetProperty,      METH_VARARGS, SetProperty_doc},
224         {"getProperty", 
225          (PyCFunction) KX_TouchSensor::sPyGetProperty,      METH_VARARGS, GetProperty_doc},
226         {"getHitObject", 
227          (PyCFunction) KX_TouchSensor::sPyGetHitObject,     METH_VARARGS, GetHitObject_doc},
228         {"getHitObjectList", 
229          (PyCFunction) KX_TouchSensor::sPyGetHitObjectList, METH_VARARGS, GetHitObjectList_doc},
230         {NULL,NULL} //Sentinel
231 };
232
233 PyObject* KX_TouchSensor::_getattr(char* attr) {
234         _getattr_up(SCA_ISensor);
235 }
236
237 /* Python API */
238
239 /* 1. setProperty */
240 char KX_TouchSensor::SetProperty_doc[] = 
241 "setProperty(name)\n"
242 "\t- name: string\n"
243 "\tSet the property or material to collide with. Use\n"
244 "\tsetTouchMaterial() to switch between properties and\n"
245 "\tmaterials.";
246 PyObject* KX_TouchSensor::PySetProperty(PyObject* self, 
247                                                                                 PyObject* args, 
248                                                                                 PyObject* kwds) {
249         char *nameArg;
250         if (!PyArg_ParseTuple(args, "s", &nameArg)) {
251                 return NULL;
252         }
253
254         CValue* prop = GetParent()->FindIdentifier(nameArg);
255
256         if (!prop->IsError()) {
257                 m_touchedpropname = nameArg;
258                 prop->Release();
259         } else {
260                 ; /* not found ... */
261         }
262         
263         Py_Return;
264 }
265 /* 2. getProperty */
266 char KX_TouchSensor::GetProperty_doc[] = 
267 "getProperty(name)\n"
268 "\tReturns the property or material to collide with. Use\n"
269 "\tgetTouchMaterial() to find out whether this sensor\n"
270 "\tlooks for properties or materials.";
271 PyObject*  KX_TouchSensor::PyGetProperty(PyObject* self, 
272                                                                                  PyObject* args, 
273                                                                                  PyObject* kwds) {
274         return PyString_FromString(m_touchedpropname);
275 }
276
277 char KX_TouchSensor::GetHitObject_doc[] = 
278 "getHitObject()\n"
279 ;
280 PyObject* KX_TouchSensor::PyGetHitObject(PyObject* self, 
281                                                                                  PyObject* args, 
282                                                                                  PyObject* kwds)
283 {
284         /* to do: do Py_IncRef if the object is already known in Python */
285         /* otherwise, this leaks memory */
286         if (m_hitObject)
287         {
288                 return m_hitObject->AddRef();
289         }
290         Py_Return;
291 }
292
293 char KX_TouchSensor::GetHitObjectList_doc[] = 
294 "getHitObjectList()\n"
295 "\tReturn a list of the objects this object collided with,\n"
296 "\tbut only those matching the property/material condition.\n";
297 PyObject* KX_TouchSensor::PyGetHitObjectList(PyObject* self, 
298                                                                                  PyObject* args, 
299                                                                                  PyObject* kwds)
300 {
301
302         /* to do: do Py_IncRef if the object is already known in Python */
303         /* otherwise, this leaks memory */
304
305         if ( m_touchedpropname.IsEmpty() ) {
306                 return m_colliders->AddRef();
307         } else {
308                 CListValue* newList = new CListValue();
309                 int i = 0;
310                 while (i < m_colliders->GetCount()) {
311                         if (m_bFindMaterial) {
312                                 /* need to associate the CValues from the list to material
313                                  * names. The collider list _should_ contains only
314                                  * KX_GameObjects. I am loathe to cast them, though... The
315                                  * material name must be retrieved from Sumo. To a Sumo
316                                  * object, a client-info block is attached. This block
317                                  * contains the material name. 
318                                  * - this also doesn't work (obviously) for multi-materials... 
319                                  */
320                                 KX_GameObject* gameob = (KX_GameObject*) m_colliders->GetValue(i);
321                                 SM_Object* smob = (SM_Object*) gameob->GetSumoObject();
322                                 
323                                 if (smob) {
324                                         SM_ClientObjectInfo* cl_inf = (SM_ClientObjectInfo*) smob->getClientObject();
325                                         
326                                         if (m_touchedpropname == ((char*)cl_inf->m_auxilary_info)) {
327                                                 newList->Add(m_colliders->GetValue(i)->AddRef());
328                                         } 
329                                 }
330                                 
331                         } else {
332                                 CValue* val = m_colliders->GetValue(i)->FindIdentifier(m_touchedpropname);
333                                 if (!val->IsError()) {
334                                         newList->Add(m_colliders->GetValue(i)->AddRef());
335                                         val->Release();
336                                 }
337                         }
338                         
339                         i++;
340                 }
341                 return newList->AddRef();
342         }
343
344 }
345
346 /* 5. getTouchMaterial */
347 char KX_TouchSensor::GetTouchMaterial_doc[] = 
348 "getTouchMaterial()\n"
349 "\tReturns KX_TRUE if this sensor looks for a specific material,\n"
350 "\tKX_FALSE if it looks for a specific property.\n" ;
351 PyObject* KX_TouchSensor::PyGetTouchMaterial(PyObject* self, 
352                                                                                          PyObject* args, 
353                                                                                          PyObject* kwds)
354 {
355         int retval = 0;
356         
357         if (m_bFindMaterial) {
358                 retval = KX_TRUE;
359         } else {
360                 retval = KX_FALSE;
361         }
362
363         return PyInt_FromLong(retval);
364 }
365
366 /* 6. setTouchMaterial */
367 char KX_TouchSensor::SetTouchMaterial_doc[] = 
368 "setTouchMaterial(flag)\n"
369 "\t- flag: KX_TRUE or KX_FALSE.\n"
370 "\tSet flag to KX_TRUE to switch on positive pulse mode,\n"
371 "\tKX_FALSE to switch off positive pulse mode.\n" ;
372 PyObject* KX_TouchSensor::PySetTouchMaterial(PyObject* self, PyObject* args, PyObject* kwds)
373 {
374         int pulseArg = 0;
375
376         if(!PyArg_ParseTuple(args, "i", &pulseArg)) {
377                 return NULL;
378         }
379         
380         if (pulseArg == KX_TRUE) {
381                 m_bFindMaterial = true;
382         } else if (pulseArg == KX_FALSE){
383                 m_bFindMaterial = false;
384         } else {
385                 ; /* internal error */
386         }
387
388         Py_Return;
389 }
390
391 #endif //#ifdef PHYSICS_NOT_YET
392
393 /* eof */