soc-2008-mxcurioni: merged changes to revision 15705
[blender.git] / source / gameengine / Ketsji / KX_NearSensor.cpp
1 /**
2  * Sense if other objects are near
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_NearSensor.h"
33 #include "SCA_LogicManager.h"
34 #include "KX_GameObject.h"
35 #include "KX_TouchEventManager.h"
36 #include "KX_Scene.h" // needed to create a replica
37 #include "PHY_IPhysicsEnvironment.h"
38 #include "PHY_IPhysicsController.h"
39
40
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif
44 KX_NearSensor::KX_NearSensor(SCA_EventManager* eventmgr,
45                                                          KX_GameObject* gameobj,
46                                                          double margin,
47                                                          double resetmargin,
48                                                          bool bFindMaterial,
49                                                          const STR_String& touchedpropname,
50                                                          class KX_Scene* scene,
51                                                          PHY_IPhysicsController*        ctrl,
52                                                          PyTypeObject* T)
53                          :KX_TouchSensor(eventmgr,
54                                                          gameobj,
55                                                          bFindMaterial,
56                                                          touchedpropname,
57                                                          /* scene, */
58                                                          T),
59                          m_Margin(margin),
60                          m_ResetMargin(resetmargin)
61
62 {
63
64         gameobj->getClientInfo()->m_sensors.remove(this);
65         m_client_info = new KX_ClientObjectInfo(gameobj, KX_ClientObjectInfo::NEAR);
66         m_client_info->m_sensors.push_back(this);
67         
68         //DT_ShapeHandle shape = (DT_ShapeHandle) vshape;
69         m_physCtrl = ctrl;
70         if (m_physCtrl)
71         {
72                 m_physCtrl->SetMargin(m_Margin);
73                 m_physCtrl->setNewClientInfo(m_client_info);
74         }
75         SynchronizeTransform();
76 }
77
78 void KX_NearSensor::SynchronizeTransform()
79 {
80         // The near and radar sensors are using a different physical object which is 
81         // not linked to the parent object, must synchronize it.
82         if (m_physCtrl)
83         {
84                 KX_GameObject* parent = ((KX_GameObject*)GetParent());
85                 MT_Vector3 pos = parent->NodeGetWorldPosition();
86                 MT_Quaternion orn = parent->NodeGetWorldOrientation().getRotation();
87                 m_physCtrl->setPosition(pos.x(),pos.y(),pos.z());
88                 m_physCtrl->setOrientation(orn.x(),orn.y(),orn.z(),orn.w());
89                 m_physCtrl->calcXform();
90         }
91 }
92
93 void KX_NearSensor::RegisterSumo(KX_TouchEventManager *touchman)
94 {
95         if (m_physCtrl)
96         {
97                 touchman->GetPhysicsEnvironment()->addSensor(m_physCtrl);
98         }
99 }
100
101 CValue* KX_NearSensor::GetReplica()
102 {
103         KX_NearSensor* replica = new KX_NearSensor(*this);
104         replica->m_colliders = new CListValue();
105         replica->Init();
106         // this will copy properties and so on...
107         CValue::AddDataToReplica(replica);
108         
109         replica->m_client_info = new KX_ClientObjectInfo(m_client_info->m_gameobject, KX_ClientObjectInfo::NEAR);
110         
111         if (replica->m_physCtrl)
112         {
113                 replica->m_physCtrl = replica->m_physCtrl->GetReplica();
114                 if (replica->m_physCtrl)
115                 {
116                         //static_cast<KX_TouchEventManager*>(m_eventmgr)->GetPhysicsEnvironment()->addSensor(replica->m_physCtrl);
117                         replica->m_physCtrl->SetMargin(m_Margin);
118                         replica->m_physCtrl->setNewClientInfo(replica->m_client_info);
119                 }
120                 
121         }
122         //static_cast<KX_TouchEventManager*>(m_eventmgr)->RegisterSensor(this);
123         //todo: make sure replication works fine
124         //>m_sumoObj = new SM_Object(DT_NewSphere(0.0),NULL,NULL,NULL);
125         //replica->m_sumoObj->setMargin(m_Margin);
126         //replica->m_sumoObj->setClientObject(replica->m_client_info);
127         
128         ((KX_GameObject*)replica->GetParent())->GetSGNode()->ComputeWorldTransforms(NULL);
129         replica->SynchronizeTransform();
130         
131         return replica;
132 }
133
134
135
136 void KX_NearSensor::ReParent(SCA_IObject* parent)
137 {
138         m_client_info->m_gameobject = static_cast<KX_GameObject*>(parent); 
139         m_client_info->m_sensors.push_back(this);
140         
141
142 /*      KX_ClientObjectInfo *client_info = gameobj->getClientInfo();
143         client_info->m_gameobject = gameobj;
144         client_info->m_auxilary_info = NULL;
145         
146         client_info->m_sensors.push_back(this);
147         SCA_ISensor::ReParent(parent);
148 */
149         ((KX_GameObject*)GetParent())->GetSGNode()->ComputeWorldTransforms(NULL);
150         SynchronizeTransform();
151         SCA_ISensor::ReParent(parent);
152 }
153
154
155
156 KX_NearSensor::~KX_NearSensor()
157 {
158         // for nearsensor, the sensor is the 'owner' of sumoobj
159         // for touchsensor, it's the parent
160         if (m_physCtrl)
161         {
162                 //static_cast<KX_TouchEventManager*>(m_eventmgr)->GetPhysicsEnvironment()->removeSensor(m_physCtrl);
163                 delete m_physCtrl;
164                 m_physCtrl = NULL;
165         }
166         
167                 
168         if (m_client_info)
169                 delete m_client_info;
170 }
171
172
173 bool KX_NearSensor::Evaluate(CValue* event)
174 {
175         bool result = false;
176 //      KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
177
178         if (m_bTriggered != m_bLastTriggered)
179         {
180                 m_bLastTriggered = m_bTriggered;
181                 if (m_bTriggered)
182                 {
183                         if (m_physCtrl)
184                         {
185                                 m_physCtrl->SetMargin(m_ResetMargin);
186                         }
187                 } else
188                 {
189                         if (m_physCtrl)
190                         {
191                                 m_physCtrl->SetMargin(m_Margin);
192                         }
193
194                 }
195                 result = true;
196         }
197
198         return result;
199 }
200
201 // this function is called at broad phase stage to check if the two controller
202 // need to interact at all. It is used for Near/Radar sensor that don't need to
203 // check collision with object not included in filter
204 bool    KX_NearSensor::BroadPhaseFilterCollision(void*obj1,void*obj2)
205 {
206         KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
207         
208         // need the mapping from PHY_IPhysicsController to gameobjects now
209         assert(obj1==m_physCtrl && obj2);
210         KX_ClientObjectInfo* client_info = static_cast<KX_ClientObjectInfo*>((static_cast<PHY_IPhysicsController*>(obj2))->getNewClientInfo());
211
212         KX_GameObject* gameobj = ( client_info ? 
213                         client_info->m_gameobject :
214                         NULL);
215         
216         if (gameobj && (gameobj != parent))
217         {
218                 // only take valid colliders
219                 if (client_info->m_type == KX_ClientObjectInfo::ACTOR)
220                 {
221                         if ((m_touchedpropname.Length() == 0) || 
222                                 (gameobj->GetProperty(m_touchedpropname)))
223                         {
224                                 return true;
225                         }
226                 }
227         }
228
229         return false;
230 }
231
232 bool    KX_NearSensor::NewHandleCollision(void* obj1,void* obj2,const PHY_CollData * coll_data)
233 {
234 //      KX_TouchEventManager* toucheventmgr = static_cast<KX_TouchEventManager*>(m_eventmgr);
235         KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
236         
237         // need the mapping from PHY_IPhysicsController to gameobjects now
238         
239         KX_ClientObjectInfo* client_info =static_cast<KX_ClientObjectInfo*> (obj1 == m_physCtrl? 
240                                         ((PHY_IPhysicsController*)obj2)->getNewClientInfo() : 
241                                         ((PHY_IPhysicsController*)obj1)->getNewClientInfo());
242
243         KX_GameObject* gameobj = ( client_info ? 
244                         client_info->m_gameobject :
245                         NULL);
246         
247         // these checks are done already in BroadPhaseFilterCollision()
248         if (gameobj /*&& (gameobj != parent)*/)
249         {
250                 if (!m_colliders->SearchValue(gameobj))
251                         m_colliders->Add(gameobj->AddRef());
252                 // only take valid colliders
253                 // These checks are done already in BroadPhaseFilterCollision()
254                 //if (client_info->m_type == KX_ClientObjectInfo::ACTOR)
255                 //{
256                 //      if ((m_touchedpropname.Length() == 0) || 
257                 //              (gameobj->GetProperty(m_touchedpropname)))
258                 //      {
259                                 m_bTriggered = true;
260                                 m_hitObject = gameobj;
261                 //      }
262                 //}
263         }
264         
265         return DT_CONTINUE;
266 }
267
268
269
270 // python embedding
271 PyTypeObject KX_NearSensor::Type = {
272         PyObject_HEAD_INIT(&PyType_Type)
273         0,
274         "KX_NearSensor",
275         sizeof(KX_NearSensor),
276         0,
277         PyDestructor,
278         0,
279         __getattr,
280         __setattr,
281         0, //&MyPyCompare,
282         __repr,
283         0, //&cvalue_as_number,
284         0,
285         0,
286         0,
287         0
288 };
289
290
291
292 PyParentObject KX_NearSensor::Parents[] = {
293         &KX_NearSensor::Type,
294         &KX_TouchSensor::Type,
295         &SCA_ISensor::Type,
296         &SCA_ILogicBrick::Type,
297         &CValue::Type,
298         NULL
299 };
300
301
302
303 PyMethodDef KX_NearSensor::Methods[] = {
304         {"setProperty", 
305          (PyCFunction) KX_NearSensor::sPySetProperty,      METH_VARARGS, SetProperty_doc},
306         {"getProperty", 
307          (PyCFunction) KX_NearSensor::sPyGetProperty,      METH_VARARGS, GetProperty_doc},
308         {"getHitObject", 
309          (PyCFunction) KX_NearSensor::sPyGetHitObject,     METH_VARARGS, GetHitObject_doc},
310         {"getHitObjectList", 
311          (PyCFunction) KX_NearSensor::sPyGetHitObjectList, METH_VARARGS, GetHitObjectList_doc},
312         {NULL,NULL} //Sentinel
313 };
314
315
316 PyObject*
317 KX_NearSensor::_getattr(const STR_String& attr)
318 {
319   _getattr_up(KX_TouchSensor);
320 }
321