ifdef's for future py3 support, after this adding py3 can mostly be done with defines...
[blender.git] / source / gameengine / Ketsji / KX_RadarSensor.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include "KX_RadarSensor.h"
30 #include "KX_GameObject.h"
31 #include "KX_PyMath.h"
32 #include "PHY_IPhysicsController.h"
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 /**
39  *      RadarSensor constructor. Creates a near-sensor derived class, with a cone collision shape.
40  */
41 KX_RadarSensor::KX_RadarSensor(SCA_EventManager* eventmgr,
42                 KX_GameObject* gameobj,
43                 PHY_IPhysicsController* physCtrl,
44                         double coneradius,
45                         double coneheight,
46                         int     axis,
47                         double margin,
48                         double resetmargin,
49                         bool bFindMaterial,
50                         const STR_String& touchedpropname,
51                         class KX_Scene* kxscene,
52                         PyTypeObject* T)
53
54                         : KX_NearSensor(
55                                 eventmgr,
56                                 gameobj,
57                                 //DT_NewCone(coneradius,coneheight),
58                                 margin,
59                                 resetmargin,
60                                 bFindMaterial,
61                                 touchedpropname,
62                                 kxscene,
63                                 physCtrl,
64                                 T),
65                                 m_coneradius(coneradius),
66                                 m_coneheight(coneheight),
67                                 m_axis(axis)
68 {
69         m_client_info->m_type = KX_ClientObjectInfo::RADAR;
70         //m_client_info->m_clientobject = gameobj;
71         //m_client_info->m_auxilary_info = NULL;
72         //sumoObj->setClientObject(&m_client_info);
73 }
74                         
75 KX_RadarSensor::~KX_RadarSensor()
76 {
77         
78 }
79
80 CValue* KX_RadarSensor::GetReplica()
81 {
82         KX_RadarSensor* replica = new KX_RadarSensor(*this);
83         replica->ProcessReplica();
84         return replica;
85 }
86
87 void KX_RadarSensor::ProcessReplica()
88 {
89         KX_NearSensor::ProcessReplica();
90         m_client_info->m_type = KX_ClientObjectInfo::RADAR;
91 }
92
93 /**
94  *      Transforms the collision object. A cone is not correctly centered
95  *      for usage.  */
96 void KX_RadarSensor::SynchronizeTransform()
97 {
98         // Getting the parent location was commented out. Why?
99         MT_Transform trans;
100         trans.setOrigin(((KX_GameObject*)GetParent())->NodeGetWorldPosition());
101         trans.setBasis(((KX_GameObject*)GetParent())->NodeGetWorldOrientation());
102         // What is the default orientation? pointing in the -y direction?
103         // is the geometry correctly converted?
104
105         // a collision cone is oriented
106         // center the cone correctly 
107         // depends on the radar 'axis'
108         switch (m_axis)
109         {
110         case 0: // +X Axis
111                 {
112                         MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(90));
113                         trans.rotate(rotquatje);
114                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
115                         break;
116                 };
117         case 1: // +Y Axis
118                 {
119                         MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-180));
120                         trans.rotate(rotquatje);
121                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
122                         break;
123                 };
124         case 2: // +Z Axis
125                 {
126                         MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-90));
127                         trans.rotate(rotquatje);
128                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
129                         break;
130                 };
131         case 3: // -X Axis
132                 {
133                         MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(-90));
134                         trans.rotate(rotquatje);
135                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
136                         break;
137                 };
138         case 4: // -Y Axis
139                 {
140                         //MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-180));
141                         //trans.rotate(rotquatje);
142                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
143                         break;
144                 };
145         case 5: // -Z Axis
146                 {
147                         MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(90));
148                         trans.rotate(rotquatje);
149                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
150                         break;
151                 };
152         default:
153                 {
154                 }
155         }
156         
157         //Using a temp variable to translate MT_Point3 to float[3].
158         //float[3] works better for the Python interface.
159         MT_Point3 temp = trans.getOrigin();
160         m_cone_origin[0] = temp[0];
161         m_cone_origin[1] = temp[1];
162         m_cone_origin[2] = temp[2];
163
164         temp = trans(MT_Point3(0, -m_coneheight/2.0 ,0));
165         m_cone_target[0] = temp[0];
166         m_cone_target[1] = temp[1];
167         m_cone_target[2] = temp[2];
168
169
170         if (m_physCtrl)
171         {
172                 MT_Quaternion orn = trans.getRotation();
173                 MT_Point3 pos = trans.getOrigin();
174                 m_physCtrl->setPosition(pos[0],pos[1],pos[2]);
175                 m_physCtrl->setOrientation(orn[0],orn[1],orn[2],orn[3]);
176                 m_physCtrl->calcXform();
177         }
178
179 }
180
181 /* ------------------------------------------------------------------------- */
182 /* Python Functions                                                                                                                      */
183 /* ------------------------------------------------------------------------- */
184
185 //Deprecated ----->
186 /* getConeOrigin */
187 const char KX_RadarSensor::GetConeOrigin_doc[] = 
188 "getConeOrigin()\n"
189 "\tReturns the origin of the cone with which to test. The origin\n"
190 "\tis in the middle of the cone.";
191 PyObject* KX_RadarSensor::PyGetConeOrigin() {
192         ShowDeprecationWarning("getConeOrigin()", "the coneOrigin property");
193
194         PyObject *retVal = PyList_New(3);
195         
196         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_cone_origin[0]));
197         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_cone_origin[1]));
198         PyList_SET_ITEM(retVal, 2, PyFloat_FromDouble(m_cone_origin[2]));
199         
200         return retVal;
201 }
202
203 /* getConeOrigin */
204 const char KX_RadarSensor::GetConeTarget_doc[] = 
205 "getConeTarget()\n"
206 "\tReturns the center of the bottom face of the cone with which to test.\n";
207 PyObject* KX_RadarSensor::PyGetConeTarget() {
208         ShowDeprecationWarning("getConeTarget()", "the coneTarget property");
209
210         PyObject *retVal = PyList_New(3);
211         
212         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_cone_target[0]));
213         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_cone_target[1]));
214         PyList_SET_ITEM(retVal, 2, PyFloat_FromDouble(m_cone_target[2]));
215         
216         return retVal;
217 }
218
219 /* getConeHeight */
220 const char KX_RadarSensor::GetConeHeight_doc[] = 
221 "getConeHeight()\n"
222 "\tReturns the height of the cone with which to test.\n";
223 PyObject* KX_RadarSensor::PyGetConeHeight() {
224                                                                                           
225         ShowDeprecationWarning("getConeHeight()", "the distance property");
226
227         return PyFloat_FromDouble(m_coneheight);
228 }
229 //<----- Deprecated
230
231 /* ------------------------------------------------------------------------- */
232 /* Python Integration Hooks                                                  */
233 /* ------------------------------------------------------------------------- */
234 PyTypeObject KX_RadarSensor::Type = {
235 #if (PY_VERSION_HEX >= 0x02060000)
236         PyVarObject_HEAD_INIT(NULL, 0)
237 #else
238         /* python 2.5 and below */
239         PyObject_HEAD_INIT( NULL )  /* required py macro */
240         0,                          /* ob_size */
241 #endif
242         "KX_RadarSensor",
243         sizeof(PyObjectPlus_Proxy),
244         0,
245         py_base_dealloc,
246         0,
247         0,
248         0,
249         0,
250         py_base_repr,
251         0,0,0,0,0,0,
252         py_base_getattro,
253         py_base_setattro,
254         0,0,0,0,0,0,0,0,0,
255         Methods
256 };
257
258 PyParentObject KX_RadarSensor::Parents[] = {
259         &KX_RadarSensor::Type,
260         &KX_NearSensor::Type,
261         &KX_TouchSensor::Type,
262         &SCA_ISensor::Type,
263         &SCA_ILogicBrick::Type,
264         &CValue::Type,
265         NULL
266 };
267
268 PyMethodDef KX_RadarSensor::Methods[] = {
269         //Deprecated ----->
270         {"getConeOrigin", (PyCFunction) KX_RadarSensor::sPyGetConeOrigin, 
271          METH_VARARGS, (PY_METHODCHAR)GetConeOrigin_doc},
272         {"getConeTarget", (PyCFunction) KX_RadarSensor::sPyGetConeTarget, 
273          METH_VARARGS, (PY_METHODCHAR)GetConeTarget_doc},
274         {"getConeHeight", (PyCFunction) KX_RadarSensor::sPyGetConeHeight, 
275          METH_VARARGS, (PY_METHODCHAR)GetConeHeight_doc},
276          //<-----
277         {NULL} //Sentinel
278 };
279
280 PyAttributeDef KX_RadarSensor::Attributes[] = {
281         KX_PYATTRIBUTE_FLOAT_ARRAY_RO("coneOrigin", KX_RadarSensor, m_cone_origin, 3),
282         KX_PYATTRIBUTE_FLOAT_ARRAY_RO("coneTarget", KX_RadarSensor, m_cone_target, 3),
283         KX_PYATTRIBUTE_FLOAT_RW("angle", 0, 360, KX_RadarSensor, m_coneradius),
284         KX_PYATTRIBUTE_INT_RW("axis", 0, 5, true, KX_RadarSensor, m_axis),
285         {NULL} //Sentinel
286 };
287
288 PyObject* KX_RadarSensor::py_getattro(PyObject *attr)
289 {
290         py_getattro_up(KX_NearSensor);
291 }
292
293 PyObject* KX_RadarSensor::py_getattro_dict() {
294         py_getattro_dict_up(KX_NearSensor);
295 }
296
297 int KX_RadarSensor::py_setattro(PyObject *attr, PyObject* value)
298 {
299         py_setattro_up(KX_NearSensor);
300 }