[GameEngine] Commit all Kester's changes made to the gameengine to restore 2.25 like...
[blender-staging.git] / source / gameengine / Ketsji / KX_NearSensor.cpp
1 /**
2  * Sense if other objects are near
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL/BL DUAL 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. The Blender
12  * Foundation also sells licenses for use in proprietary software under
13  * the Blender License.  See http://www.blender.org/BL/ for information
14  * about this.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26  * All rights reserved.
27  *
28  * The Original Code is: all of this file.
29  *
30  * Contributor(s): none yet.
31  *
32  * ***** END GPL/BL DUAL LICENSE BLOCK *****
33  */
34
35 #include "KX_NearSensor.h"
36 #include "SCA_LogicManager.h"
37 #include "KX_GameObject.h"
38 #include "KX_TouchEventManager.h"
39 #include "KX_Scene.h" // needed to create a replica
40
41 #include "SM_Object.h"
42
43 #ifdef HAVE_CONFIG_H
44 #include <config.h>
45 #endif
46 KX_NearSensor::KX_NearSensor(SCA_EventManager* eventmgr,
47                                                          KX_GameObject* gameobj,
48                                                          void *vshape,
49                                                          double margin,
50                                                          double resetmargin,
51                                                          bool bFindMaterial,
52                                                          const STR_String& touchedpropname,
53                                                          class KX_Scene* scene,
54                                                          PyTypeObject* T)
55                          :KX_TouchSensor(eventmgr,
56                                                          gameobj,
57                                                          bFindMaterial,
58                                                          touchedpropname,
59                                                          /* scene, */
60                                                          T),
61                          m_Margin(margin),
62                          m_ResetMargin(resetmargin)
63
64 {
65         m_client_info = new KX_ClientObjectInfo(gameobj);
66         m_client_info->m_type = KX_ClientObjectInfo::NEAR;
67         
68         DT_ShapeHandle shape = (DT_ShapeHandle) vshape;
69         m_sumoObj = new SM_Object(shape,NULL,NULL,NULL);
70         m_sumoObj->setMargin(m_Margin);
71         m_sumoObj->setClientObject(m_client_info);
72         
73         SynchronizeTransform();
74 }
75
76 KX_NearSensor::KX_NearSensor(SCA_EventManager* eventmgr,
77                                                          KX_GameObject* gameobj,
78                                                          double margin,
79                                                          double resetmargin,
80                                                          bool bFindMaterial,
81                                                          const STR_String& touchedpropname,
82                                                          class KX_Scene* scene,
83                                                          PyTypeObject* T)
84                          :KX_TouchSensor(eventmgr,
85                                                          gameobj,
86                                                          bFindMaterial,
87                                                          touchedpropname,
88                                                          /* scene, */
89                                                          T),
90                          m_Margin(margin),
91                          m_ResetMargin(resetmargin)
92
93 {
94         m_client_info = new KX_ClientObjectInfo(gameobj);
95         m_client_info->m_type = KX_ClientObjectInfo::NEAR;
96         m_client_info->m_auxilary_info = NULL;
97         
98         m_sumoObj = new SM_Object(DT_NewSphere(0.0),NULL,NULL,NULL);
99         m_sumoObj->setMargin(m_Margin);
100         m_sumoObj->setClientObject(m_client_info);
101         
102         SynchronizeTransform();
103 }
104
105 void KX_NearSensor::RegisterSumo(KX_TouchEventManager *touchman)
106 {
107         touchman->GetSumoScene()->addSensor(*m_sumoObj);
108 }
109
110 CValue* KX_NearSensor::GetReplica()
111 {
112         KX_NearSensor* replica = new KX_NearSensor(*this);
113         replica->m_colliders = new CListValue();
114         replica->m_bCollision = false;
115         replica->m_bTriggered= false;
116         replica->m_hitObject = NULL;
117         replica->m_bLastTriggered = false;
118         // this will copy properties and so on...
119         CValue::AddDataToReplica(replica);
120         
121         replica->m_client_info = new KX_ClientObjectInfo(m_client_info->m_clientobject);
122         replica->m_client_info->m_type = KX_ClientObjectInfo::NEAR;
123         replica->m_client_info->m_auxilary_info = NULL;
124         
125         replica->m_sumoObj = new SM_Object(DT_NewSphere(0.0),NULL,NULL,NULL);
126         replica->m_sumoObj->setMargin(m_Margin);
127         replica->m_sumoObj->setClientObject(replica->m_client_info);
128         
129         replica->SynchronizeTransform();
130         
131         return replica;
132 }
133
134
135
136 void KX_NearSensor::ReParent(SCA_IObject* parent)
137 {
138         SCA_ISensor::ReParent(parent);
139         
140         m_client_info->m_clientobject = static_cast<KX_GameObject*>(parent); 
141         
142         SynchronizeTransform();
143 }
144
145
146
147 KX_NearSensor::~KX_NearSensor()
148 {
149         // for nearsensor, the sensor is the 'owner' of sumoobj
150         // for touchsensor, it's the parent
151         static_cast<KX_TouchEventManager*>(m_eventmgr)->GetSumoScene()->remove(*m_sumoObj);
152
153         if (m_sumoObj)
154                 delete m_sumoObj;
155                 
156         if (m_client_info)
157                 delete m_client_info;
158 }
159
160
161 bool KX_NearSensor::Evaluate(CValue* event)
162 {
163         bool result = false;
164         KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
165
166         if (m_bTriggered != m_bLastTriggered)
167         {
168                 m_bLastTriggered = m_bTriggered;
169                 if (m_bTriggered)
170                 {
171                         if (m_sumoObj)
172                         {
173                                 m_sumoObj->setMargin(m_ResetMargin);
174                         }
175                 } else
176                 {
177                         if (m_sumoObj)
178                         {
179                                 m_sumoObj->setMargin(m_Margin);
180                         }
181
182                 }
183                 result = true;
184         }
185
186         return result;
187 }
188
189
190
191 DT_Bool KX_NearSensor::HandleCollision(void* obj1,void* obj2,const DT_CollData * coll_data)
192 {
193         KX_TouchEventManager* toucheventmgr = static_cast<KX_TouchEventManager*>(m_eventmgr);
194         KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
195         
196         // need the mapping from SM_Objects to gameobjects now
197         
198         KX_ClientObjectInfo* client_info =static_cast<KX_ClientObjectInfo*> (obj1 == m_sumoObj? 
199                                         ((SM_Object*)obj2)->getClientObject() : 
200                                         ((SM_Object*)obj1)->getClientObject());
201
202         KX_GameObject* gameobj = ( client_info ? 
203                         static_cast<KX_GameObject*>(client_info->m_clientobject) : 
204                         NULL);
205         
206         if (gameobj && (gameobj != parent))
207         {
208                 if (!m_colliders->SearchValue(gameobj))
209                         m_colliders->Add(gameobj->AddRef());
210                 // only take valid colliders
211                 if (client_info->m_type == KX_ClientObjectInfo::ACTOR)
212                 {
213                         if ((m_touchedpropname.Length() == 0) || 
214                                 (gameobj->GetProperty(m_touchedpropname)))
215                         {
216                                 m_bTriggered = true;
217                                 m_hitObject = gameobj;
218                         }
219                 }
220         }
221         
222         return DT_CONTINUE;
223 }
224
225
226
227 // python embedding
228 PyTypeObject KX_NearSensor::Type = {
229         PyObject_HEAD_INIT(&PyType_Type)
230         0,
231         "KX_NearSensor",
232         sizeof(KX_NearSensor),
233         0,
234         PyDestructor,
235         0,
236         __getattr,
237         __setattr,
238         0, //&MyPyCompare,
239         __repr,
240         0, //&cvalue_as_number,
241         0,
242         0,
243         0,
244         0
245 };
246
247
248
249 PyParentObject KX_NearSensor::Parents[] = {
250         &KX_NearSensor::Type,
251         &KX_TouchSensor::Type,
252         &SCA_ISensor::Type,
253         &SCA_ILogicBrick::Type,
254         &CValue::Type,
255         NULL
256 };
257
258
259
260 PyMethodDef KX_NearSensor::Methods[] = {
261         {"setProperty", 
262          (PyCFunction) KX_NearSensor::sPySetProperty,      METH_VARARGS, SetProperty_doc},
263         {"getProperty", 
264          (PyCFunction) KX_NearSensor::sPyGetProperty,      METH_VARARGS, GetProperty_doc},
265         {"getHitObject", 
266          (PyCFunction) KX_NearSensor::sPyGetHitObject,     METH_VARARGS, GetHitObject_doc},
267         {"getHitObjectList", 
268          (PyCFunction) KX_NearSensor::sPyGetHitObjectList, METH_VARARGS, GetHitObjectList_doc},
269         {NULL,NULL} //Sentinel
270 };
271
272
273 PyObject*
274 KX_NearSensor::_getattr(char* attr)
275 {
276   _getattr_up(KX_TouchSensor);
277 }
278