BGE Python 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         // this will copy properties and so on...
115         CValue::AddDataToReplica(replica);
116         
117         replica->m_client_info = new KX_ClientObjectInfo(m_client_info->m_gameobject, KX_ClientObjectInfo::NEAR);
118         
119         if (replica->m_physCtrl)
120         {
121                 replica->m_physCtrl = replica->m_physCtrl->GetReplica();
122                 if (replica->m_physCtrl)
123                 {
124                         //static_cast<KX_TouchEventManager*>(m_eventmgr)->GetPhysicsEnvironment()->addSensor(replica->m_physCtrl);
125                         replica->m_physCtrl->SetMargin(m_Margin);
126                         replica->m_physCtrl->setNewClientInfo(replica->m_client_info);
127                 }
128                 
129         }
130         //static_cast<KX_TouchEventManager*>(m_eventmgr)->RegisterSensor(this);
131         //todo: make sure replication works fine
132         //>m_sumoObj = new SM_Object(DT_NewSphere(0.0),NULL,NULL,NULL);
133         //replica->m_sumoObj->setMargin(m_Margin);
134         //replica->m_sumoObj->setClientObject(replica->m_client_info);
135         
136         ((KX_GameObject*)replica->GetParent())->GetSGNode()->ComputeWorldTransforms(NULL);
137         replica->SynchronizeTransform();
138         
139         return replica;
140 }
141
142
143
144 void KX_NearSensor::ReParent(SCA_IObject* parent)
145 {
146         m_client_info->m_gameobject = static_cast<KX_GameObject*>(parent); 
147         m_client_info->m_sensors.push_back(this);
148         
149
150 /*      KX_ClientObjectInfo *client_info = gameobj->getClientInfo();
151         client_info->m_gameobject = gameobj;
152         client_info->m_auxilary_info = NULL;
153         
154         client_info->m_sensors.push_back(this);
155         SCA_ISensor::ReParent(parent);
156 */
157         ((KX_GameObject*)GetParent())->GetSGNode()->ComputeWorldTransforms(NULL);
158         SynchronizeTransform();
159         SCA_ISensor::ReParent(parent);
160 }
161
162
163
164 KX_NearSensor::~KX_NearSensor()
165 {
166         // for nearsensor, the sensor is the 'owner' of sumoobj
167         // for touchsensor, it's the parent
168         if (m_physCtrl)
169         {
170                 //static_cast<KX_TouchEventManager*>(m_eventmgr)->GetPhysicsEnvironment()->removeSensor(m_physCtrl);
171                 delete m_physCtrl;
172                 m_physCtrl = NULL;
173         }
174         
175                 
176         if (m_client_info)
177                 delete m_client_info;
178 }
179
180
181 bool KX_NearSensor::Evaluate(CValue* event)
182 {
183         bool result = false;
184 //      KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
185
186         if (m_bTriggered != m_bLastTriggered)
187         {
188                 m_bLastTriggered = m_bTriggered;
189                 if (m_bTriggered)
190                 {
191                         if (m_physCtrl)
192                         {
193                                 m_physCtrl->SetRadius(m_ResetMargin);
194                         }
195                 } else
196                 {
197                         if (m_physCtrl)
198                         {
199                                 m_physCtrl->SetRadius(m_Margin);
200                         }
201
202                 }
203                 result = true;
204         }
205
206         return result;
207 }
208
209 // this function is called at broad phase stage to check if the two controller
210 // need to interact at all. It is used for Near/Radar sensor that don't need to
211 // check collision with object not included in filter
212 bool    KX_NearSensor::BroadPhaseFilterCollision(void*obj1,void*obj2)
213 {
214         KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
215         
216         // need the mapping from PHY_IPhysicsController to gameobjects now
217         assert(obj1==m_physCtrl && obj2);
218         KX_ClientObjectInfo* client_info = static_cast<KX_ClientObjectInfo*>((static_cast<PHY_IPhysicsController*>(obj2))->getNewClientInfo());
219
220         KX_GameObject* gameobj = ( client_info ? 
221                         client_info->m_gameobject :
222                         NULL);
223         
224         if (gameobj && (gameobj != parent))
225         {
226                 // only take valid colliders
227                 if (client_info->m_type == KX_ClientObjectInfo::ACTOR)
228                 {
229                         if ((m_touchedpropname.Length() == 0) || 
230                                 (gameobj->GetProperty(m_touchedpropname)))
231                         {
232                                 return true;
233                         }
234                 }
235         }
236
237         return false;
238 }
239
240 bool    KX_NearSensor::NewHandleCollision(void* obj1,void* obj2,const PHY_CollData * coll_data)
241 {
242 //      KX_TouchEventManager* toucheventmgr = static_cast<KX_TouchEventManager*>(m_eventmgr);
243 //      KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
244         
245         // need the mapping from PHY_IPhysicsController to gameobjects now
246         
247         KX_ClientObjectInfo* client_info =static_cast<KX_ClientObjectInfo*> (obj1 == m_physCtrl? 
248                                         ((PHY_IPhysicsController*)obj2)->getNewClientInfo() : 
249                                         ((PHY_IPhysicsController*)obj1)->getNewClientInfo());
250
251         KX_GameObject* gameobj = ( client_info ? 
252                         client_info->m_gameobject :
253                         NULL);
254         
255         // Add the same check as in SCA_ISensor::Activate(), 
256         // we don't want to record collision when the sensor is not active.
257         if (m_links && !m_suspended &&
258                 gameobj /* done in BroadPhaseFilterCollision() && (gameobj != parent)*/)
259         {
260                 if (!m_colliders->SearchValue(gameobj))
261                         m_colliders->Add(gameobj->AddRef());
262                 // only take valid colliders
263                 // These checks are done already in BroadPhaseFilterCollision()
264                 //if (client_info->m_type == KX_ClientObjectInfo::ACTOR)
265                 //{
266                 //      if ((m_touchedpropname.Length() == 0) || 
267                 //              (gameobj->GetProperty(m_touchedpropname)))
268                 //      {
269                                 m_bTriggered = true;
270                                 m_hitObject = gameobj;
271                 //      }
272                 //}
273         }
274         
275         return false; // was DT_CONTINUE; but this was defined in Sumo as false
276 }
277
278
279 /* ------------------------------------------------------------------------- */
280 /* Python Functions                                                                                                                      */
281 /* ------------------------------------------------------------------------- */
282
283 //No methods
284
285 /* ------------------------------------------------------------------------- */
286 /* Python Integration Hooks                                                  */
287 /* ------------------------------------------------------------------------- */
288
289 PyTypeObject KX_NearSensor::Type = {
290         PyObject_HEAD_INIT(NULL)
291         0,
292         "KX_NearSensor",
293         sizeof(KX_NearSensor),
294         0,
295         PyDestructor,
296         0,
297         0,
298         0,
299         0,
300         py_base_repr,
301         0,0,0,0,0,0,
302         py_base_getattro,
303         py_base_setattro,
304         0,0,0,0,0,0,0,0,0,
305         Methods
306 };
307
308
309
310 PyParentObject KX_NearSensor::Parents[] = {
311         &KX_NearSensor::Type,
312         &KX_TouchSensor::Type,
313         &SCA_ISensor::Type,
314         &SCA_ILogicBrick::Type,
315         &CValue::Type,
316         NULL
317 };
318
319
320
321 PyMethodDef KX_NearSensor::Methods[] = {
322         //No methods
323         {NULL,NULL} //Sentinel
324 };
325
326 PyAttributeDef KX_NearSensor::Attributes[] = {
327         KX_PYATTRIBUTE_FLOAT_RW_CHECK("distance", 0, 100, KX_NearSensor, m_Margin, CheckResetDistance),
328         KX_PYATTRIBUTE_FLOAT_RW_CHECK("resetDistance", 0, 100, KX_NearSensor, m_ResetMargin, CheckResetDistance),
329         {NULL} //Sentinel
330 };
331
332
333 PyObject* KX_NearSensor::py_getattro(PyObject *attr)
334 {
335         py_getattro_up(KX_TouchSensor);
336 }
337
338 int KX_NearSensor::py_setattro(PyObject*attr, PyObject* value)
339 {
340         py_setattro_up(KX_TouchSensor);
341 }