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