BGE C++ API
[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                                                          float margin,
46                                                          float 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                                                          false,
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 void KX_NearSensor::UnregisterSumo(KX_TouchEventManager* touchman)
102 {
103         if (m_physCtrl)
104         {
105                 touchman->GetPhysicsEnvironment()->removeSensor(m_physCtrl);
106         }
107 }
108
109 CValue* KX_NearSensor::GetReplica()
110 {
111         KX_NearSensor* replica = new KX_NearSensor(*this);
112         replica->m_colliders = new CListValue();
113         replica->Init();
114         replica->ProcessReplica();
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         //Wrong: the parent object could be a child, this code works only if it is a root parent.
130         //Anyway, at this stage, the parent object is already synchronized, nothing to do.
131         //bool parentUpdated = false;
132         //((KX_GameObject*)replica->GetParent())->GetSGNode()->ComputeWorldTransforms(NULL, parentUpdated);
133         replica->SynchronizeTransform();
134         
135         return replica;
136 }
137
138
139
140 void KX_NearSensor::ReParent(SCA_IObject* parent)
141 {
142         m_client_info->m_gameobject = static_cast<KX_GameObject*>(parent); 
143         m_client_info->m_sensors.push_back(this);
144         
145
146 /*      KX_ClientObjectInfo *client_info = gameobj->getClientInfo();
147         client_info->m_gameobject = gameobj;
148         client_info->m_auxilary_info = NULL;
149         
150         client_info->m_sensors.push_back(this);
151         SCA_ISensor::ReParent(parent);
152 */
153         //Not needed, was done in GetReplica() already
154         //bool parentUpdated = false;
155         //((KX_GameObject*)GetParent())->GetSGNode()->ComputeWorldTransforms(NULL,parentUpdated);
156         //SynchronizeTransform();
157         SCA_ISensor::ReParent(parent);
158 }
159
160
161
162 KX_NearSensor::~KX_NearSensor()
163 {
164         // for nearsensor, the sensor is the 'owner' of sumoobj
165         // for touchsensor, it's the parent
166         if (m_physCtrl)
167         {
168                 //static_cast<KX_TouchEventManager*>(m_eventmgr)->GetPhysicsEnvironment()->removeSensor(m_physCtrl);
169                 delete m_physCtrl;
170                 m_physCtrl = NULL;
171         }
172         
173                 
174         if (m_client_info)
175                 delete m_client_info;
176 }
177
178
179 bool KX_NearSensor::Evaluate(CValue* event)
180 {
181         bool result = false;
182 //      KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
183
184         if (m_bTriggered != m_bLastTriggered)
185         {
186                 m_bLastTriggered = m_bTriggered;
187                 if (m_bTriggered)
188                 {
189                         if (m_physCtrl)
190                         {
191                                 m_physCtrl->SetRadius(m_ResetMargin);
192                         }
193                 } else
194                 {
195                         if (m_physCtrl)
196                         {
197                                 m_physCtrl->SetRadius(m_Margin);
198                         }
199
200                 }
201                 result = true;
202         }
203
204         return result;
205 }
206
207 // this function is called at broad phase stage to check if the two controller
208 // need to interact at all. It is used for Near/Radar sensor that don't need to
209 // check collision with object not included in filter
210 bool    KX_NearSensor::BroadPhaseFilterCollision(void*obj1,void*obj2)
211 {
212         KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
213         
214         // need the mapping from PHY_IPhysicsController to gameobjects now
215         assert(obj1==m_physCtrl && obj2);
216         KX_ClientObjectInfo* client_info = static_cast<KX_ClientObjectInfo*>((static_cast<PHY_IPhysicsController*>(obj2))->getNewClientInfo());
217
218         KX_GameObject* gameobj = ( client_info ? 
219                         client_info->m_gameobject :
220                         NULL);
221         
222         if (gameobj && (gameobj != parent))
223         {
224                 // only take valid colliders
225                 if (client_info->m_type == KX_ClientObjectInfo::ACTOR)
226                 {
227                         if ((m_touchedpropname.Length() == 0) || 
228                                 (gameobj->GetProperty(m_touchedpropname)))
229                         {
230                                 return true;
231                         }
232                 }
233         }
234
235         return false;
236 }
237
238 bool    KX_NearSensor::NewHandleCollision(void* obj1,void* obj2,const PHY_CollData * coll_data)
239 {
240 //      KX_TouchEventManager* toucheventmgr = static_cast<KX_TouchEventManager*>(m_eventmgr);
241 //      KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
242         
243         // need the mapping from PHY_IPhysicsController to gameobjects now
244         
245         KX_ClientObjectInfo* client_info =static_cast<KX_ClientObjectInfo*> (obj1 == m_physCtrl? 
246                                         ((PHY_IPhysicsController*)obj2)->getNewClientInfo() : 
247                                         ((PHY_IPhysicsController*)obj1)->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 /* done in BroadPhaseFilterCollision() && (gameobj != parent)*/)
257         {
258                 if (!m_colliders->SearchValue(gameobj))
259                         m_colliders->Add(gameobj->AddRef());
260                 // only take valid colliders
261                 // These checks are done already in BroadPhaseFilterCollision()
262                 //if (client_info->m_type == KX_ClientObjectInfo::ACTOR)
263                 //{
264                 //      if ((m_touchedpropname.Length() == 0) || 
265                 //              (gameobj->GetProperty(m_touchedpropname)))
266                 //      {
267                                 m_bTriggered = true;
268                                 m_hitObject = gameobj;
269                 //      }
270                 //}
271         }
272         
273         return false; // was DT_CONTINUE; but this was defined in Sumo as false
274 }
275
276
277 /* ------------------------------------------------------------------------- */
278 /* Python Functions                                                                                                                      */
279 /* ------------------------------------------------------------------------- */
280
281 //No methods
282
283 /* ------------------------------------------------------------------------- */
284 /* Python Integration Hooks                                                  */
285 /* ------------------------------------------------------------------------- */
286
287 PyTypeObject KX_NearSensor::Type = {
288         PyObject_HEAD_INIT(NULL)
289         0,
290         "KX_NearSensor",
291         sizeof(PyObjectPlus_Proxy),
292         0,
293         py_base_dealloc,
294         0,
295         0,
296         0,
297         0,
298         py_base_repr,
299         0,0,0,0,0,0,
300         py_base_getattro,
301         py_base_setattro,
302         0,0,0,0,0,0,0,0,0,
303         Methods
304 };
305
306
307
308 PyParentObject KX_NearSensor::Parents[] = {
309         &KX_NearSensor::Type,
310         &KX_TouchSensor::Type,
311         &SCA_ISensor::Type,
312         &SCA_ILogicBrick::Type,
313         &CValue::Type,
314         NULL
315 };
316
317
318
319 PyMethodDef KX_NearSensor::Methods[] = {
320         //No methods
321         {NULL,NULL} //Sentinel
322 };
323
324 PyAttributeDef KX_NearSensor::Attributes[] = {
325         KX_PYATTRIBUTE_FLOAT_RW_CHECK("distance", 0, 100, KX_NearSensor, m_Margin, CheckResetDistance),
326         KX_PYATTRIBUTE_FLOAT_RW_CHECK("resetDistance", 0, 100, KX_NearSensor, m_ResetMargin, CheckResetDistance),
327         {NULL} //Sentinel
328 };
329
330
331 PyObject* KX_NearSensor::py_getattro(PyObject *attr)
332 {
333         py_getattro_up(KX_TouchSensor);
334 }
335
336 PyObject* KX_NearSensor::py_getattro_dict() {
337         py_getattro_dict_up(KX_TouchSensor);
338 }
339
340 int KX_NearSensor::py_setattro(PyObject*attr, PyObject* value)
341 {
342         py_setattro_up(KX_TouchSensor);
343 }