Patch from GSR that a) fixes a whole bunch of GPL/BL license
[blender-staging.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::RegisterSumo(KX_TouchEventManager *touchman)
79 {
80         if (m_physCtrl)
81         {
82                 touchman->GetPhysicsEnvironment()->addSensor(m_physCtrl);
83         }
84 }
85
86 CValue* KX_NearSensor::GetReplica()
87 {
88         KX_NearSensor* replica = new KX_NearSensor(*this);
89         replica->m_colliders = new CListValue();
90         replica->m_bCollision = false;
91         replica->m_bTriggered= false;
92         replica->m_hitObject = NULL;
93         replica->m_bLastTriggered = false;
94         // this will copy properties and so on...
95         CValue::AddDataToReplica(replica);
96         
97         replica->m_client_info = new KX_ClientObjectInfo(m_client_info->m_gameobject, KX_ClientObjectInfo::NEAR);
98         
99         if (replica->m_physCtrl)
100         {
101                 replica->m_physCtrl = replica->m_physCtrl->GetReplica();
102                 if (replica->m_physCtrl)
103                 {
104                         //static_cast<KX_TouchEventManager*>(m_eventmgr)->GetPhysicsEnvironment()->addSensor(replica->m_physCtrl);
105                         replica->m_physCtrl->SetMargin(m_Margin);
106                         replica->m_physCtrl->setNewClientInfo(replica->m_client_info);
107                 }
108                 
109         }
110         //static_cast<KX_TouchEventManager*>(m_eventmgr)->RegisterSensor(this);
111         //todo: make sure replication works fine
112         //>m_sumoObj = new SM_Object(DT_NewSphere(0.0),NULL,NULL,NULL);
113         //replica->m_sumoObj->setMargin(m_Margin);
114         //replica->m_sumoObj->setClientObject(replica->m_client_info);
115         
116         ((KX_GameObject*)replica->GetParent())->GetSGNode()->ComputeWorldTransforms(NULL);
117         replica->SynchronizeTransform();
118         
119         return replica;
120 }
121
122
123
124 void KX_NearSensor::ReParent(SCA_IObject* parent)
125 {
126
127         SCA_ISensor::ReParent(parent);
128         
129         m_client_info->m_gameobject = static_cast<KX_GameObject*>(parent); 
130         m_client_info->m_sensors.push_back(this);
131         
132
133 /*      KX_ClientObjectInfo *client_info = gameobj->getClientInfo();
134         client_info->m_gameobject = gameobj;
135         client_info->m_auxilary_info = NULL;
136         
137         client_info->m_sensors.push_back(this);
138         SCA_ISensor::ReParent(parent);
139 */
140         ((KX_GameObject*)GetParent())->GetSGNode()->ComputeWorldTransforms(NULL);
141         SynchronizeTransform();
142 }
143
144
145
146 KX_NearSensor::~KX_NearSensor()
147 {
148         // for nearsensor, the sensor is the 'owner' of sumoobj
149         // for touchsensor, it's the parent
150         if (m_physCtrl)
151         {
152                 //static_cast<KX_TouchEventManager*>(m_eventmgr)->GetPhysicsEnvironment()->removeSensor(m_physCtrl);
153                 delete m_physCtrl;
154                 m_physCtrl = NULL;
155         }
156         
157                 
158         if (m_client_info)
159                 delete m_client_info;
160 }
161
162
163 bool KX_NearSensor::Evaluate(CValue* event)
164 {
165         bool result = false;
166 //      KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
167
168         if (m_bTriggered != m_bLastTriggered)
169         {
170                 m_bLastTriggered = m_bTriggered;
171                 if (m_bTriggered)
172                 {
173                         if (m_physCtrl)
174                         {
175                                 m_physCtrl->SetMargin(m_ResetMargin);
176                         }
177                 } else
178                 {
179                         if (m_physCtrl)
180                         {
181                                 m_physCtrl->SetMargin(m_Margin);
182                         }
183
184                 }
185                 result = true;
186         }
187
188         return result;
189 }
190
191 // this function is called at broad phase stage to check if the two controller
192 // need to interact at all. It is used for Near/Radar sensor that don't need to
193 // check collision with object not included in filter
194 bool    KX_NearSensor::BroadPhaseFilterCollision(void*obj1,void*obj2)
195 {
196         KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
197         
198         // need the mapping from PHY_IPhysicsController to gameobjects now
199         assert(obj1==m_physCtrl && obj2);
200         KX_ClientObjectInfo* client_info = static_cast<KX_ClientObjectInfo*>((static_cast<PHY_IPhysicsController*>(obj2))->getNewClientInfo());
201
202         KX_GameObject* gameobj = ( client_info ? 
203                         client_info->m_gameobject :
204                         NULL);
205         
206         if (gameobj && (gameobj != parent))
207         {
208                 // only take valid colliders
209                 if (client_info->m_type == KX_ClientObjectInfo::ACTOR)
210                 {
211                         if ((m_touchedpropname.Length() == 0) || 
212                                 (gameobj->GetProperty(m_touchedpropname)))
213                         {
214                                 return true;
215                         }
216                 }
217         }
218
219         return false;
220 }
221
222 bool    KX_NearSensor::NewHandleCollision(void* obj1,void* obj2,const PHY_CollData * coll_data)
223 {
224 //      KX_TouchEventManager* toucheventmgr = static_cast<KX_TouchEventManager*>(m_eventmgr);
225         KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
226         
227         // need the mapping from PHY_IPhysicsController to gameobjects now
228         
229         KX_ClientObjectInfo* client_info =static_cast<KX_ClientObjectInfo*> (obj1 == m_physCtrl? 
230                                         ((PHY_IPhysicsController*)obj2)->getNewClientInfo() : 
231                                         ((PHY_IPhysicsController*)obj1)->getNewClientInfo());
232
233         KX_GameObject* gameobj = ( client_info ? 
234                         client_info->m_gameobject :
235                         NULL);
236         
237         // these checks are done already in BroadPhaseFilterCollision()
238         if (gameobj /*&& (gameobj != parent)*/)
239         {
240                 if (!m_colliders->SearchValue(gameobj))
241                         m_colliders->Add(gameobj->AddRef());
242                 // only take valid colliders
243                 // These checks are done already in BroadPhaseFilterCollision()
244                 //if (client_info->m_type == KX_ClientObjectInfo::ACTOR)
245                 //{
246                 //      if ((m_touchedpropname.Length() == 0) || 
247                 //              (gameobj->GetProperty(m_touchedpropname)))
248                 //      {
249                                 m_bTriggered = true;
250                                 m_hitObject = gameobj;
251                 //      }
252                 //}
253         }
254         
255         return DT_CONTINUE;
256 }
257
258
259
260 // python embedding
261 PyTypeObject KX_NearSensor::Type = {
262         PyObject_HEAD_INIT(&PyType_Type)
263         0,
264         "KX_NearSensor",
265         sizeof(KX_NearSensor),
266         0,
267         PyDestructor,
268         0,
269         __getattr,
270         __setattr,
271         0, //&MyPyCompare,
272         __repr,
273         0, //&cvalue_as_number,
274         0,
275         0,
276         0,
277         0
278 };
279
280
281
282 PyParentObject KX_NearSensor::Parents[] = {
283         &KX_NearSensor::Type,
284         &KX_TouchSensor::Type,
285         &SCA_ISensor::Type,
286         &SCA_ILogicBrick::Type,
287         &CValue::Type,
288         NULL
289 };
290
291
292
293 PyMethodDef KX_NearSensor::Methods[] = {
294         {"setProperty", 
295          (PyCFunction) KX_NearSensor::sPySetProperty,      METH_VARARGS, SetProperty_doc},
296         {"getProperty", 
297          (PyCFunction) KX_NearSensor::sPyGetProperty,      METH_VARARGS, GetProperty_doc},
298         {"getHitObject", 
299          (PyCFunction) KX_NearSensor::sPyGetHitObject,     METH_VARARGS, GetHitObject_doc},
300         {"getHitObjectList", 
301          (PyCFunction) KX_NearSensor::sPyGetHitObjectList, METH_VARARGS, GetHitObjectList_doc},
302         {NULL,NULL} //Sentinel
303 };
304
305
306 PyObject*
307 KX_NearSensor::_getattr(const STR_String& attr)
308 {
309   _getattr_up(KX_TouchSensor);
310 }
311