big warning hunt commit
[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 "SM_Object.h"
41 #include "KX_SumoPhysicsController.h"
42 #include <iostream>
43
44 #ifdef HAVE_CONFIG_H
45 #include <config.h>
46 #endif
47
48 /* ------------------------------------------------------------------------- */
49 /* Native functions                                                          */
50 /* ------------------------------------------------------------------------- */
51
52 void KX_TouchSensor::SynchronizeTransform()
53 {
54
55         if (m_sumoObj)
56         {
57                 m_sumoObj->setPosition(((KX_GameObject*)GetParent())->NodeGetWorldPosition());
58                 m_sumoObj->setOrientation(
59                         ((KX_GameObject*)GetParent())->NodeGetWorldOrientation().getRotation()
60                         );
61                 m_sumoObj->calcXform();
62         }
63         
64 }
65
66
67 void KX_TouchSensor::EndFrame() {
68         m_colliders->ReleaseAndRemoveAll();
69         m_bTriggered = false;
70 }
71
72 bool KX_TouchSensor::Evaluate(CValue* event)
73 {
74         bool result = false;
75
76         if (m_bTriggered != m_bLastTriggered)
77         {
78                 m_bLastTriggered = m_bTriggered;
79                 if (!m_bTriggered)
80                         m_hitObject = NULL;
81                 result = true;
82         }
83         
84         return result;
85 }
86
87 KX_TouchSensor::KX_TouchSensor(SCA_EventManager* eventmgr,KX_GameObject* gameobj,/*SM_Object* sumoObj,*/bool bFindMaterial,const STR_String& touchedpropname,PyTypeObject* T)
88 :SCA_ISensor(gameobj,eventmgr,T),
89 m_touchedpropname(touchedpropname),
90 m_bFindMaterial(bFindMaterial),
91 m_eventmgr(eventmgr),
92 /*m_sumoObj(sumoObj),*/
93 m_bCollision(false),
94 m_bTriggered(false),
95 m_bLastTriggered(false)
96 {
97         // KX_TouchEventManager* touchmgr = (KX_TouchEventManager*) eventmgr; /*unused*/
98 //      m_resptable = touchmgr->GetResponseTable();
99         
100 //      m_solidHandle = m_sumoObj->getObjectHandle();
101
102         m_hitObject =  NULL;
103         m_colliders = new CListValue();
104         
105         KX_ClientObjectInfo *client_info = gameobj->getClientInfo();
106         client_info->m_gameobject = gameobj;
107         client_info->m_auxilary_info = NULL;
108         client_info->m_sensors.push_back(this);
109         
110         KX_SumoPhysicsController *sphy = dynamic_cast<KX_SumoPhysicsController *>(gameobj->GetPhysicsController());
111         if (sphy)
112                 m_sumoObj = sphy->GetSumoObject();
113
114 }
115
116
117 KX_TouchSensor::~KX_TouchSensor()
118 {
119         //DT_ClearObjectResponse(m_resptable,m_solidHandle);
120         m_colliders->Release();
121 }
122
123 CValue* KX_TouchSensor::GetReplica() 
124 {
125         KX_TouchSensor* replica = new KX_TouchSensor(*this);
126         replica->m_colliders = new CListValue();
127         replica->m_bCollision = false;
128         replica->m_bTriggered= false;
129         replica->m_hitObject = NULL;
130         replica->m_bLastTriggered = false;
131         // this will copy properties and so on...
132         CValue::AddDataToReplica(replica);
133         return replica;
134 }
135
136 void    KX_TouchSensor::ReParent(SCA_IObject* parent)
137 {
138         KX_GameObject *gameobj = static_cast<KX_GameObject *>(parent);
139         KX_SumoPhysicsController *sphy = dynamic_cast<KX_SumoPhysicsController *>(((KX_GameObject*)parent)->GetPhysicsController());
140         if (sphy)
141                 m_sumoObj = sphy->GetSumoObject();
142
143 //      m_solidHandle = m_sumoObj->getObjectHandle();
144         KX_ClientObjectInfo *client_info = gameobj->getClientInfo();
145         client_info->m_gameobject = gameobj;
146         client_info->m_auxilary_info = NULL;
147         client_info->m_sensors.push_back(this);
148         SCA_ISensor::ReParent(parent);
149 }
150
151 void KX_TouchSensor::RegisterSumo(KX_TouchEventManager *touchman)
152 {
153         if (m_sumoObj)
154         {
155                 touchman->GetSumoScene()->requestCollisionCallback(*m_sumoObj);
156                 // collision
157                 // Deprecated   
158
159         }
160 }
161
162 DT_Bool    KX_TouchSensor::HandleCollision(void* obj1,void* obj2,const DT_CollData * coll_data)
163 {
164         // KX_TouchEventManager* toucheventmgr = (KX_TouchEventManager*)m_eventmgr; /*unused*/
165         KX_GameObject* parent = (KX_GameObject*)GetParent();
166
167         // need the mapping from SM_Objects to gameobjects now
168         
169         KX_ClientObjectInfo* client_info = static_cast<KX_ClientObjectInfo*> (obj1 == m_sumoObj? 
170                                         ((SM_Object*)obj2)->getClientObject() : 
171                                         ((SM_Object*)obj1)->getClientObject());
172
173         KX_GameObject* gameobj = ( client_info ? 
174                         client_info->m_gameobject : 
175                         NULL);
176         
177         if (gameobj && (gameobj != parent) && client_info->isActor())
178         {
179                 if (!m_colliders->SearchValue(gameobj))
180                         m_colliders->Add(gameobj->AddRef());
181                 
182                 bool found = m_touchedpropname.IsEmpty();
183                 if (!found)
184                 {
185                         if (m_bFindMaterial)
186                         {
187                                 if (client_info->m_auxilary_info)
188                                 {
189                                         found = (m_touchedpropname == STR_String((char*)client_info->m_auxilary_info));
190                                 }
191                         } else
192                         {
193                                 found = (gameobj->GetProperty(m_touchedpropname) != NULL);
194                         }
195                 }
196                 if (found)
197                 {
198                         m_bTriggered = true;
199                         m_hitObject = gameobj;
200                         //printf("KX_TouchSensor::HandleCollision\n");
201                 }
202                 
203         } 
204         return DT_CONTINUE;
205 }
206
207
208 /* ------------------------------------------------------------------------- */
209 /* Python functions                                                          */
210 /* ------------------------------------------------------------------------- */
211 /* Integration hooks ------------------------------------------------------- */
212 PyTypeObject KX_TouchSensor::Type = {
213         PyObject_HEAD_INIT(&PyType_Type)
214         0,
215         "KX_TouchSensor",
216         sizeof(KX_TouchSensor),
217         0,
218         PyDestructor,
219         0,
220         __getattr,
221         __setattr,
222         0, //&MyPyCompare,
223         __repr,
224         0, //&cvalue_as_number,
225         0,
226         0,
227         0,
228         0
229 };
230
231 PyParentObject KX_TouchSensor::Parents[] = {
232         &KX_TouchSensor::Type,
233         &SCA_ISensor::Type,
234         &SCA_ILogicBrick::Type,
235         &CValue::Type,
236         NULL
237 };
238
239 PyMethodDef KX_TouchSensor::Methods[] = {
240         {"setProperty", 
241          (PyCFunction) KX_TouchSensor::sPySetProperty,      METH_VARARGS, SetProperty_doc},
242         {"getProperty", 
243          (PyCFunction) KX_TouchSensor::sPyGetProperty,      METH_VARARGS, GetProperty_doc},
244         {"getHitObject", 
245          (PyCFunction) KX_TouchSensor::sPyGetHitObject,     METH_VARARGS, GetHitObject_doc},
246         {"getHitObjectList", 
247          (PyCFunction) KX_TouchSensor::sPyGetHitObjectList, METH_VARARGS, GetHitObjectList_doc},
248         {NULL,NULL} //Sentinel
249 };
250
251 PyObject* KX_TouchSensor::_getattr(const STR_String& attr) {
252         _getattr_up(SCA_ISensor);
253 }
254
255 /* Python API */
256
257 /* 1. setProperty */
258 char KX_TouchSensor::SetProperty_doc[] = 
259 "setProperty(name)\n"
260 "\t- name: string\n"
261 "\tSet the property or material to collide with. Use\n"
262 "\tsetTouchMaterial() to switch between properties and\n"
263 "\tmaterials.";
264 PyObject* KX_TouchSensor::PySetProperty(PyObject* self, 
265                                                                                 PyObject* args, 
266                                                                                 PyObject* kwds) {
267         char *nameArg;
268         if (!PyArg_ParseTuple(args, "s", &nameArg)) {
269                 return NULL;
270         }
271
272         CValue* prop = GetParent()->FindIdentifier(nameArg);
273
274         if (!prop->IsError()) {
275                 m_touchedpropname = nameArg;
276                 prop->Release();
277         } else {
278                 ; /* not found ... */
279         }
280         
281         Py_Return;
282 }
283 /* 2. getProperty */
284 char KX_TouchSensor::GetProperty_doc[] = 
285 "getProperty(name)\n"
286 "\tReturns the property or material to collide with. Use\n"
287 "\tgetTouchMaterial() to find out whether this sensor\n"
288 "\tlooks for properties or materials.";
289 PyObject*  KX_TouchSensor::PyGetProperty(PyObject* self, 
290                                                                                  PyObject* args, 
291                                                                                  PyObject* kwds) {
292         return PyString_FromString(m_touchedpropname);
293 }
294
295 char KX_TouchSensor::GetHitObject_doc[] = 
296 "getHitObject()\n"
297 ;
298 PyObject* KX_TouchSensor::PyGetHitObject(PyObject* self, 
299                                                                                  PyObject* args, 
300                                                                                  PyObject* kwds)
301 {
302         /* to do: do Py_IncRef if the object is already known in Python */
303         /* otherwise, this leaks memory */
304         if (m_hitObject)
305         {
306                 return m_hitObject->AddRef();
307         }
308         Py_Return;
309 }
310
311 char KX_TouchSensor::GetHitObjectList_doc[] = 
312 "getHitObjectList()\n"
313 "\tReturn a list of the objects this object collided with,\n"
314 "\tbut only those matching the property/material condition.\n";
315 PyObject* KX_TouchSensor::PyGetHitObjectList(PyObject* self, 
316                                                                                  PyObject* args, 
317                                                                                  PyObject* kwds)
318 {
319
320         /* to do: do Py_IncRef if the object is already known in Python */
321         /* otherwise, this leaks memory */
322
323         if ( m_touchedpropname.IsEmpty() ) {
324                 return m_colliders->AddRef();
325         } else {
326                 CListValue* newList = new CListValue();
327                 int i = 0;
328                 while (i < m_colliders->GetCount()) {
329                         if (m_bFindMaterial) {
330                                 /* need to associate the CValues from the list to material
331                                  * names. The collider list _should_ contains only
332                                  * KX_GameObjects. I am loathe to cast them, though... The
333                                  * material name must be retrieved from Sumo. To a Sumo
334                                  * object, a client-info block is attached. This block
335                                  * contains the material name. 
336                                  * - this also doesn't work (obviously) for multi-materials... 
337                                  */
338                                 KX_GameObject* gameob = (KX_GameObject*) m_colliders->GetValue(i);
339                                 KX_SumoPhysicsController* spc = dynamic_cast<KX_SumoPhysicsController*>(gameob->GetPhysicsController());
340                                 SM_Object* smob = spc?spc->GetSumoObject():NULL;
341                                 
342                                 if (smob) {
343                                         KX_ClientObjectInfo* cl_inf = static_cast<KX_ClientObjectInfo*>(smob->getClientObject());
344                                         
345                                         if (m_touchedpropname == ((char*)cl_inf->m_auxilary_info)) {
346                                                 newList->Add(m_colliders->GetValue(i)->AddRef());
347                                         } 
348                                 }
349                                 
350                         } else {
351                                 CValue* val = m_colliders->GetValue(i)->FindIdentifier(m_touchedpropname);
352                                 if (!val->IsError()) {
353                                         newList->Add(m_colliders->GetValue(i)->AddRef());
354                                         val->Release();
355                                 }
356                         }
357                         
358                         i++;
359                 }
360                 return newList->AddRef();
361         }
362
363 }
364
365 /* 5. getTouchMaterial */
366 char KX_TouchSensor::GetTouchMaterial_doc[] = 
367 "getTouchMaterial()\n"
368 "\tReturns KX_TRUE if this sensor looks for a specific material,\n"
369 "\tKX_FALSE if it looks for a specific property.\n" ;
370 PyObject* KX_TouchSensor::PyGetTouchMaterial(PyObject* self, 
371                                                                                          PyObject* args, 
372                                                                                          PyObject* kwds)
373 {
374         return PyInt_FromLong(m_bFindMaterial);
375 }
376
377 /* 6. setTouchMaterial */
378 char KX_TouchSensor::SetTouchMaterial_doc[] = 
379 "setTouchMaterial(flag)\n"
380 "\t- flag: KX_TRUE or KX_FALSE.\n"
381 "\tSet flag to KX_TRUE to switch on positive pulse mode,\n"
382 "\tKX_FALSE to switch off positive pulse mode.\n" ;
383 PyObject* KX_TouchSensor::PySetTouchMaterial(PyObject* self, PyObject* args, PyObject* kwds)
384 {
385         int pulseArg = 0;
386
387         if(!PyArg_ParseTuple(args, "i", &pulseArg)) {
388                 return NULL;
389         }
390         
391         m_bFindMaterial = pulseArg != 0;
392
393         Py_Return;
394 }
395
396
397 /* eof */