Port Python updates from Tuhopuu2:
[blender.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, KX_ClientObjectInfo::NEAR);
66         
67         DT_ShapeHandle shape = (DT_ShapeHandle) vshape;
68         m_sumoObj = new SM_Object(shape,NULL,NULL,NULL);
69         m_sumoObj->setMargin(m_Margin);
70         m_sumoObj->setClientObject(m_client_info);
71         
72         SynchronizeTransform();
73 }
74
75 KX_NearSensor::KX_NearSensor(SCA_EventManager* eventmgr,
76                                                          KX_GameObject* gameobj,
77                                                          double margin,
78                                                          double resetmargin,
79                                                          bool bFindMaterial,
80                                                          const STR_String& touchedpropname,
81                                                          class KX_Scene* scene,
82                                                          PyTypeObject* T)
83                          :KX_TouchSensor(eventmgr,
84                                                          gameobj,
85                                                          bFindMaterial,
86                                                          touchedpropname,
87                                                          /* scene, */
88                                                          T),
89                          m_Margin(margin),
90                          m_ResetMargin(resetmargin)
91
92 {
93         m_client_info = new KX_ClientObjectInfo(gameobj, KX_ClientObjectInfo::NEAR);
94         
95         m_sumoObj = new SM_Object(DT_NewSphere(0.0),NULL,NULL,NULL);
96         m_sumoObj->setMargin(m_Margin);
97         m_sumoObj->setClientObject(m_client_info);
98         
99         SynchronizeTransform();
100 }
101
102 void KX_NearSensor::RegisterSumo(KX_TouchEventManager *touchman)
103 {
104         touchman->GetSumoScene()->addSensor(*m_sumoObj);
105 }
106
107 CValue* KX_NearSensor::GetReplica()
108 {
109         KX_NearSensor* replica = new KX_NearSensor(*this);
110         replica->m_colliders = new CListValue();
111         replica->m_bCollision = false;
112         replica->m_bTriggered= false;
113         replica->m_hitObject = NULL;
114         replica->m_bLastTriggered = false;
115         // this will copy properties and so on...
116         CValue::AddDataToReplica(replica);
117         
118         replica->m_client_info = new KX_ClientObjectInfo(m_client_info->m_clientobject, KX_ClientObjectInfo::NEAR);
119         
120         replica->m_sumoObj = new SM_Object(DT_NewSphere(0.0),NULL,NULL,NULL);
121         replica->m_sumoObj->setMargin(m_Margin);
122         replica->m_sumoObj->setClientObject(replica->m_client_info);
123         
124         replica->SynchronizeTransform();
125         
126         return replica;
127 }
128
129
130
131 void KX_NearSensor::ReParent(SCA_IObject* parent)
132 {
133         SCA_ISensor::ReParent(parent);
134         
135         m_client_info->m_clientobject = static_cast<KX_GameObject*>(parent); 
136         
137         SynchronizeTransform();
138 }
139
140
141
142 KX_NearSensor::~KX_NearSensor()
143 {
144         // for nearsensor, the sensor is the 'owner' of sumoobj
145         // for touchsensor, it's the parent
146         if (m_sumoObj)
147         {
148                 static_cast<KX_TouchEventManager*>(m_eventmgr)->GetSumoScene()->remove(*m_sumoObj);
149                 delete m_sumoObj;
150                 m_sumoObj = NULL;
151         }
152                 
153         if (m_client_info)
154                 delete m_client_info;
155 }
156
157
158 bool KX_NearSensor::Evaluate(CValue* event)
159 {
160         bool result = false;
161         KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
162
163         if (m_bTriggered != m_bLastTriggered)
164         {
165                 m_bLastTriggered = m_bTriggered;
166                 if (m_bTriggered)
167                 {
168                         if (m_sumoObj)
169                         {
170                                 m_sumoObj->setMargin(m_ResetMargin);
171                         }
172                 } else
173                 {
174                         if (m_sumoObj)
175                         {
176                                 m_sumoObj->setMargin(m_Margin);
177                         }
178
179                 }
180                 result = true;
181         }
182
183         return result;
184 }
185
186
187
188 DT_Bool KX_NearSensor::HandleCollision(void* obj1,void* obj2,const DT_CollData * coll_data)
189 {
190         KX_TouchEventManager* toucheventmgr = static_cast<KX_TouchEventManager*>(m_eventmgr);
191         KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
192         
193         // need the mapping from SM_Objects to gameobjects now
194         
195         KX_ClientObjectInfo* client_info =static_cast<KX_ClientObjectInfo*> (obj1 == m_sumoObj? 
196                                         ((SM_Object*)obj2)->getClientObject() : 
197                                         ((SM_Object*)obj1)->getClientObject());
198
199         KX_GameObject* gameobj = ( client_info ? 
200                         static_cast<KX_GameObject*>(client_info->m_clientobject) : 
201                         NULL);
202         
203         if (gameobj && (gameobj != parent))
204         {
205                 if (!m_colliders->SearchValue(gameobj))
206                         m_colliders->Add(gameobj->AddRef());
207                 // only take valid colliders
208                 if (client_info->m_type == KX_ClientObjectInfo::ACTOR)
209                 {
210                         if ((m_touchedpropname.Length() == 0) || 
211                                 (gameobj->GetProperty(m_touchedpropname)))
212                         {
213                                 m_bTriggered = true;
214                                 m_hitObject = gameobj;
215                         }
216                 }
217         }
218         
219         return DT_CONTINUE;
220 }
221
222
223
224 // python embedding
225 PyTypeObject KX_NearSensor::Type = {
226         PyObject_HEAD_INIT(&PyType_Type)
227         0,
228         "KX_NearSensor",
229         sizeof(KX_NearSensor),
230         0,
231         PyDestructor,
232         0,
233         __getattr,
234         __setattr,
235         0, //&MyPyCompare,
236         __repr,
237         0, //&cvalue_as_number,
238         0,
239         0,
240         0,
241         0
242 };
243
244
245
246 PyParentObject KX_NearSensor::Parents[] = {
247         &KX_NearSensor::Type,
248         &KX_TouchSensor::Type,
249         &SCA_ISensor::Type,
250         &SCA_ILogicBrick::Type,
251         &CValue::Type,
252         NULL
253 };
254
255
256
257 PyMethodDef KX_NearSensor::Methods[] = {
258         {"setProperty", 
259          (PyCFunction) KX_NearSensor::sPySetProperty,      METH_VARARGS, SetProperty_doc},
260         {"getProperty", 
261          (PyCFunction) KX_NearSensor::sPyGetProperty,      METH_VARARGS, GetProperty_doc},
262         {"getHitObject", 
263          (PyCFunction) KX_NearSensor::sPyGetHitObject,     METH_VARARGS, GetHitObject_doc},
264         {"getHitObjectList", 
265          (PyCFunction) KX_NearSensor::sPyGetHitObjectList, METH_VARARGS, GetHitObjectList_doc},
266         {NULL,NULL} //Sentinel
267 };
268
269
270 PyObject*
271 KX_NearSensor::_getattr(const STR_String& attr)
272 {
273   _getattr_up(KX_TouchSensor);
274 }
275