* make sure we have still a compiling GE with scons/mingw
[blender.git] / source / gameengine / Converter / KX_ConvertSensors.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  * Conversion of Blender data blocks to KX sensor system
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #ifdef WIN32
39 #pragma warning (disable : 4786)
40 #endif //WIN32
41
42 #include "wm_event_types.h"
43 #include "KX_BlenderSceneConverter.h"
44 #include "KX_ConvertSensors.h"
45
46 /* This little block needed for linking to Blender... */
47 #ifdef WIN32
48 #include "BLI_winstuff.h"
49 #endif
50
51 #include "DNA_object_types.h"
52 #include "DNA_material_types.h"
53 #include "DNA_sensor_types.h"
54 #include "DNA_actuator_types.h" /* for SENS_ALL_KEYS ? this define is
55 probably misplaced */
56 /* end of blender include block */
57
58 #include "RAS_IPolygonMaterial.h"
59 // Sensors
60 #include "KX_GameObject.h"
61 #include "RAS_MeshObject.h"
62 #include "SCA_KeyboardSensor.h"
63 #include "SCA_MouseSensor.h"
64 #include "SCA_AlwaysSensor.h"
65 #include "KX_TouchSensor.h"
66 #include "KX_NearSensor.h"
67 #include "KX_RadarSensor.h"
68 #include "KX_MouseFocusSensor.h"
69 #include "SCA_JoystickSensor.h"
70 #include "KX_NetworkMessageSensor.h"
71
72
73 #include "SCA_PropertySensor.h"
74 #include "SCA_RandomSensor.h"
75 #include "KX_RaySensor.h"
76 #include "SCA_EventManager.h"
77 #include "SCA_LogicManager.h"
78 #include "KX_BlenderInputDevice.h"
79 #include "KX_Scene.h"
80 #include "IntValue.h"
81 #include "KX_BlenderKeyboardDevice.h"
82 #include "KX_BlenderGL.h"
83 #include "RAS_ICanvas.h"
84 #include "PHY_IPhysicsEnvironment.h"
85
86 #include "KX_KetsjiEngine.h"
87 #include "KX_BlenderSceneConverter.h"
88
89 // this map is Blender specific: a conversion between blender and ketsji enums
90 std::map<int,SCA_IInputDevice::KX_EnumInputs> gReverseKeyTranslateTable;
91
92
93 void BL_ConvertSensors(struct Object* blenderobject,
94                                            class KX_GameObject* gameobj,
95                                            SCA_LogicManager* logicmgr,
96                                            KX_Scene* kxscene,
97                                            SCA_IInputDevice* keydev,
98                                            int & executePriority,
99                                            int activeLayerBitInfo,
100                                            bool isInActiveLayer,
101                                            RAS_ICanvas* canvas,
102                                            KX_BlenderSceneConverter* converter
103                                            )
104 {
105         
106         
107         
108         /* The reverse table. In order to not confuse ourselves, we      */
109         /* immediately convert all events that come in to KX codes.      */
110         gReverseKeyTranslateTable[LEFTMOUSE                     ] =     SCA_IInputDevice::KX_LEFTMOUSE;
111         gReverseKeyTranslateTable[MIDDLEMOUSE           ] =     SCA_IInputDevice::KX_MIDDLEMOUSE;
112         gReverseKeyTranslateTable[RIGHTMOUSE            ] =     SCA_IInputDevice::KX_RIGHTMOUSE;
113         gReverseKeyTranslateTable[WHEELUPMOUSE          ] =     SCA_IInputDevice::KX_WHEELUPMOUSE;
114         gReverseKeyTranslateTable[WHEELDOWNMOUSE        ] =     SCA_IInputDevice::KX_WHEELDOWNMOUSE;
115         gReverseKeyTranslateTable[MOUSEX                        ] = SCA_IInputDevice::KX_MOUSEX;
116         gReverseKeyTranslateTable[MOUSEY                        ] =     SCA_IInputDevice::KX_MOUSEY;
117         
118         // TIMERS                                                                                                  
119         
120         gReverseKeyTranslateTable[TIMER0                        ] = SCA_IInputDevice::KX_TIMER0;                  
121         gReverseKeyTranslateTable[TIMER1                        ] = SCA_IInputDevice::KX_TIMER1;                  
122         gReverseKeyTranslateTable[TIMER2                        ] = SCA_IInputDevice::KX_TIMER2;                  
123         gReverseKeyTranslateTable[TIMER3                        ] = SCA_IInputDevice::KX_TIMER3;                  
124         
125         // SYSTEM                                                                                                  
126         
127         gReverseKeyTranslateTable[KEYBD                         ] = SCA_IInputDevice::KX_KEYBD;                  
128         gReverseKeyTranslateTable[RAWKEYBD                      ] = SCA_IInputDevice::KX_RAWKEYBD;                  
129         gReverseKeyTranslateTable[REDRAW                        ] = SCA_IInputDevice::KX_REDRAW;                  
130         gReverseKeyTranslateTable[INPUTCHANGE           ] = SCA_IInputDevice::KX_INPUTCHANGE;                  
131         gReverseKeyTranslateTable[QFULL                         ] = SCA_IInputDevice::KX_QFULL;                  
132         gReverseKeyTranslateTable[WINFREEZE                     ] = SCA_IInputDevice::KX_WINFREEZE;                  
133         gReverseKeyTranslateTable[WINTHAW                       ] = SCA_IInputDevice::KX_WINTHAW;                  
134         gReverseKeyTranslateTable[WINCLOSE                      ] = SCA_IInputDevice::KX_WINCLOSE;                  
135         gReverseKeyTranslateTable[WINQUIT                       ] = SCA_IInputDevice::KX_WINQUIT;                  
136         gReverseKeyTranslateTable[Q_FIRSTTIME           ] = SCA_IInputDevice::KX_Q_FIRSTTIME;                  
137         
138         // standard keyboard                                                                                       
139         
140         gReverseKeyTranslateTable[AKEY                          ] = SCA_IInputDevice::KX_AKEY;                  
141         gReverseKeyTranslateTable[BKEY                          ] = SCA_IInputDevice::KX_BKEY;                  
142         gReverseKeyTranslateTable[CKEY                          ] = SCA_IInputDevice::KX_CKEY;                  
143         gReverseKeyTranslateTable[DKEY                          ] = SCA_IInputDevice::KX_DKEY;                  
144         gReverseKeyTranslateTable[EKEY                          ] = SCA_IInputDevice::KX_EKEY;                  
145         gReverseKeyTranslateTable[FKEY                          ] = SCA_IInputDevice::KX_FKEY;                  
146         gReverseKeyTranslateTable[GKEY                          ] = SCA_IInputDevice::KX_GKEY;                  
147 //XXX clean up
148 #ifdef WIN32
149 #define HKEY    'h'
150 #endif
151         gReverseKeyTranslateTable[HKEY                          ] = SCA_IInputDevice::KX_HKEY;                  
152 //XXX clean up
153 #ifdef WIN32
154 #undef HKEY
155 #endif
156         gReverseKeyTranslateTable[IKEY                          ] = SCA_IInputDevice::KX_IKEY;                  
157         gReverseKeyTranslateTable[JKEY                          ] = SCA_IInputDevice::KX_JKEY;                  
158         gReverseKeyTranslateTable[KKEY                          ] = SCA_IInputDevice::KX_KKEY;                  
159         gReverseKeyTranslateTable[LKEY                          ] = SCA_IInputDevice::KX_LKEY;                  
160         gReverseKeyTranslateTable[MKEY                          ] = SCA_IInputDevice::KX_MKEY;                  
161         gReverseKeyTranslateTable[NKEY                          ] = SCA_IInputDevice::KX_NKEY;                  
162         gReverseKeyTranslateTable[OKEY                          ] = SCA_IInputDevice::KX_OKEY;                  
163         gReverseKeyTranslateTable[PKEY                          ] = SCA_IInputDevice::KX_PKEY;                  
164         gReverseKeyTranslateTable[QKEY                          ] = SCA_IInputDevice::KX_QKEY;                  
165         gReverseKeyTranslateTable[RKEY                          ] = SCA_IInputDevice::KX_RKEY;                  
166         gReverseKeyTranslateTable[SKEY                          ] = SCA_IInputDevice::KX_SKEY;                  
167         gReverseKeyTranslateTable[TKEY                          ] = SCA_IInputDevice::KX_TKEY;                  
168         gReverseKeyTranslateTable[UKEY                          ] = SCA_IInputDevice::KX_UKEY;                  
169         gReverseKeyTranslateTable[VKEY                          ] = SCA_IInputDevice::KX_VKEY;                  
170         gReverseKeyTranslateTable[WKEY                          ] = SCA_IInputDevice::KX_WKEY;                  
171         gReverseKeyTranslateTable[XKEY                          ] = SCA_IInputDevice::KX_XKEY;                  
172         gReverseKeyTranslateTable[YKEY                          ] = SCA_IInputDevice::KX_YKEY;                  
173         gReverseKeyTranslateTable[ZKEY                          ] = SCA_IInputDevice::KX_ZKEY;                  
174         
175         gReverseKeyTranslateTable[ZEROKEY                       ] = SCA_IInputDevice::KX_ZEROKEY;                  
176         gReverseKeyTranslateTable[ONEKEY                        ] = SCA_IInputDevice::KX_ONEKEY;                  
177         gReverseKeyTranslateTable[TWOKEY                        ] = SCA_IInputDevice::KX_TWOKEY;                  
178         gReverseKeyTranslateTable[THREEKEY                      ] = SCA_IInputDevice::KX_THREEKEY;                  
179         gReverseKeyTranslateTable[FOURKEY                       ] = SCA_IInputDevice::KX_FOURKEY;                  
180         gReverseKeyTranslateTable[FIVEKEY                       ] = SCA_IInputDevice::KX_FIVEKEY;                  
181         gReverseKeyTranslateTable[SIXKEY                        ] = SCA_IInputDevice::KX_SIXKEY;                  
182         gReverseKeyTranslateTable[SEVENKEY                      ] = SCA_IInputDevice::KX_SEVENKEY;                  
183         gReverseKeyTranslateTable[EIGHTKEY                      ] = SCA_IInputDevice::KX_EIGHTKEY;                  
184         gReverseKeyTranslateTable[NINEKEY                       ] = SCA_IInputDevice::KX_NINEKEY;                  
185         
186         gReverseKeyTranslateTable[CAPSLOCKKEY           ] = SCA_IInputDevice::KX_CAPSLOCKKEY;                  
187         
188         gReverseKeyTranslateTable[LEFTCTRLKEY           ] = SCA_IInputDevice::KX_LEFTCTRLKEY;                  
189         gReverseKeyTranslateTable[LEFTALTKEY            ] = SCA_IInputDevice::KX_LEFTALTKEY;                  
190         gReverseKeyTranslateTable[RIGHTALTKEY           ] = SCA_IInputDevice::KX_RIGHTALTKEY;                  
191         gReverseKeyTranslateTable[RIGHTCTRLKEY          ] = SCA_IInputDevice::KX_RIGHTCTRLKEY;                  
192         gReverseKeyTranslateTable[RIGHTSHIFTKEY         ] = SCA_IInputDevice::KX_RIGHTSHIFTKEY;                  
193         gReverseKeyTranslateTable[LEFTSHIFTKEY          ] = SCA_IInputDevice::KX_LEFTSHIFTKEY;                  
194         
195         gReverseKeyTranslateTable[ESCKEY                        ] = SCA_IInputDevice::KX_ESCKEY;                  
196         gReverseKeyTranslateTable[TABKEY                        ] = SCA_IInputDevice::KX_TABKEY;                  
197         gReverseKeyTranslateTable[RETKEY                        ] = SCA_IInputDevice::KX_RETKEY;                  
198         gReverseKeyTranslateTable[SPACEKEY                      ] = SCA_IInputDevice::KX_SPACEKEY;                  
199         gReverseKeyTranslateTable[LINEFEEDKEY           ] = SCA_IInputDevice::KX_LINEFEEDKEY;                  
200         gReverseKeyTranslateTable[BACKSPACEKEY          ] = SCA_IInputDevice::KX_BACKSPACEKEY;                  
201         gReverseKeyTranslateTable[DELKEY                        ] = SCA_IInputDevice::KX_DELKEY;                  
202         gReverseKeyTranslateTable[SEMICOLONKEY          ] = SCA_IInputDevice::KX_SEMICOLONKEY;                  
203         gReverseKeyTranslateTable[PERIODKEY                     ] = SCA_IInputDevice::KX_PERIODKEY;                  
204         gReverseKeyTranslateTable[COMMAKEY                      ] = SCA_IInputDevice::KX_COMMAKEY;                  
205         gReverseKeyTranslateTable[QUOTEKEY                      ] = SCA_IInputDevice::KX_QUOTEKEY;                  
206         gReverseKeyTranslateTable[ACCENTGRAVEKEY        ] = SCA_IInputDevice::KX_ACCENTGRAVEKEY;                  
207         gReverseKeyTranslateTable[MINUSKEY                      ] = SCA_IInputDevice::KX_MINUSKEY;                  
208         gReverseKeyTranslateTable[SLASHKEY                      ] = SCA_IInputDevice::KX_SLASHKEY;                  
209         gReverseKeyTranslateTable[BACKSLASHKEY          ] = SCA_IInputDevice::KX_BACKSLASHKEY;                  
210         gReverseKeyTranslateTable[EQUALKEY                      ] = SCA_IInputDevice::KX_EQUALKEY;                  
211         gReverseKeyTranslateTable[LEFTBRACKETKEY        ] = SCA_IInputDevice::KX_LEFTBRACKETKEY;                  
212         gReverseKeyTranslateTable[RIGHTBRACKETKEY       ] = SCA_IInputDevice::KX_RIGHTBRACKETKEY;                  
213         
214         gReverseKeyTranslateTable[LEFTARROWKEY          ] = SCA_IInputDevice::KX_LEFTARROWKEY;                  
215         gReverseKeyTranslateTable[DOWNARROWKEY          ] = SCA_IInputDevice::KX_DOWNARROWKEY;                  
216         gReverseKeyTranslateTable[RIGHTARROWKEY         ] = SCA_IInputDevice::KX_RIGHTARROWKEY;                  
217         gReverseKeyTranslateTable[UPARROWKEY            ] = SCA_IInputDevice::KX_UPARROWKEY;                  
218         
219         gReverseKeyTranslateTable[PAD2                          ] = SCA_IInputDevice::KX_PAD2;                  
220         gReverseKeyTranslateTable[PAD4                          ] = SCA_IInputDevice::KX_PAD4;                  
221         gReverseKeyTranslateTable[PAD6                          ] = SCA_IInputDevice::KX_PAD6;                  
222         gReverseKeyTranslateTable[PAD8                          ] = SCA_IInputDevice::KX_PAD8;                  
223         
224         gReverseKeyTranslateTable[PAD1                          ] = SCA_IInputDevice::KX_PAD1;                  
225         gReverseKeyTranslateTable[PAD3                          ] = SCA_IInputDevice::KX_PAD3;                  
226         gReverseKeyTranslateTable[PAD5                          ] = SCA_IInputDevice::KX_PAD5;                  
227         gReverseKeyTranslateTable[PAD7                          ] = SCA_IInputDevice::KX_PAD7;                  
228         gReverseKeyTranslateTable[PAD9                          ] = SCA_IInputDevice::KX_PAD9;                  
229         
230         gReverseKeyTranslateTable[PADPERIOD                     ] = SCA_IInputDevice::KX_PADPERIOD;                  
231         gReverseKeyTranslateTable[PADSLASHKEY           ] = SCA_IInputDevice::KX_PADSLASHKEY;                  
232         gReverseKeyTranslateTable[PADASTERKEY           ] = SCA_IInputDevice::KX_PADASTERKEY;                  
233         
234         gReverseKeyTranslateTable[PAD0                          ] = SCA_IInputDevice::KX_PAD0;                  
235         gReverseKeyTranslateTable[PADMINUS                      ] = SCA_IInputDevice::KX_PADMINUS;                  
236         gReverseKeyTranslateTable[PADENTER                      ] = SCA_IInputDevice::KX_PADENTER;                  
237         gReverseKeyTranslateTable[PADPLUSKEY            ] = SCA_IInputDevice::KX_PADPLUSKEY;                  
238         
239         
240         gReverseKeyTranslateTable[F1KEY                         ] = SCA_IInputDevice::KX_F1KEY;                  
241         gReverseKeyTranslateTable[F2KEY                         ] = SCA_IInputDevice::KX_F2KEY;                  
242         gReverseKeyTranslateTable[F3KEY                         ] = SCA_IInputDevice::KX_F3KEY;                  
243         gReverseKeyTranslateTable[F4KEY                         ] = SCA_IInputDevice::KX_F4KEY;                  
244         gReverseKeyTranslateTable[F5KEY                         ] = SCA_IInputDevice::KX_F5KEY;                  
245         gReverseKeyTranslateTable[F6KEY                         ] = SCA_IInputDevice::KX_F6KEY;                  
246         gReverseKeyTranslateTable[F7KEY                         ] = SCA_IInputDevice::KX_F7KEY;                  
247         gReverseKeyTranslateTable[F8KEY                         ] = SCA_IInputDevice::KX_F8KEY;                  
248         gReverseKeyTranslateTable[F9KEY                         ] = SCA_IInputDevice::KX_F9KEY;                  
249         gReverseKeyTranslateTable[F10KEY                        ] = SCA_IInputDevice::KX_F10KEY;                  
250         gReverseKeyTranslateTable[F11KEY                        ] = SCA_IInputDevice::KX_F11KEY;                  
251         gReverseKeyTranslateTable[F12KEY                        ] = SCA_IInputDevice::KX_F12KEY;                  
252         
253         gReverseKeyTranslateTable[PAUSEKEY                      ] = SCA_IInputDevice::KX_PAUSEKEY;                  
254         gReverseKeyTranslateTable[INSERTKEY                     ] = SCA_IInputDevice::KX_INSERTKEY;                  
255         gReverseKeyTranslateTable[HOMEKEY                       ] = SCA_IInputDevice::KX_HOMEKEY;                  
256         gReverseKeyTranslateTable[PAGEUPKEY                     ] = SCA_IInputDevice::KX_PAGEUPKEY;                  
257         gReverseKeyTranslateTable[PAGEDOWNKEY           ] = SCA_IInputDevice::KX_PAGEDOWNKEY;                  
258         gReverseKeyTranslateTable[ENDKEY                        ] = SCA_IInputDevice::KX_ENDKEY;
259         
260         int uniqueint = 0;
261         bSensor* sens = (bSensor*)blenderobject->sensors.first;
262         bool pos_pulsemode = false;
263         bool neg_pulsemode = false;
264         int frequency = 0;
265         bool invert = false;
266         
267         while(sens)
268         {
269                 SCA_ISensor* gamesensor=NULL;
270                 /* All sensors have a pulse toggle, frequency, and invert field.     */
271                 /* These are extracted here, and set when the sensor is added to the */
272                 /* list.                                                             */
273                 pos_pulsemode = (sens->pulse & SENS_PULSE_REPEAT)!=0;
274                 neg_pulsemode = (sens->pulse & SENS_NEG_PULSE_MODE)!=0;
275                 
276                 frequency = sens->freq;
277                 invert    = !(sens->invert == 0);
278                 
279                 switch (sens->type)
280                 {
281                 case  SENS_ALWAYS:
282                         {
283                                 
284                                 SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::ALWAYS_EVENTMGR);
285                                 if (eventmgr)
286                                 {
287                                         gamesensor = new SCA_AlwaysSensor(eventmgr, gameobj);
288                                 }
289                                 
290                                 break;
291                         }
292                         
293                 case SENS_COLLISION:
294                         {
295                                 SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::TOUCH_EVENTMGR);
296                                 if (eventmgr)
297                                 {
298                                         // collision sensor can sense both materials and properties. 
299                                         
300                                         bool bFindMaterial = false;
301                                         
302                                         bCollisionSensor* blendertouchsensor = (bCollisionSensor*)sens->data;
303                                         
304                                         bFindMaterial = (blendertouchsensor->mode 
305                                                 & SENS_COLLISION_MATERIAL);
306                                         
307                                         
308                                         STR_String touchPropOrMatName = ( bFindMaterial ? 
309                                                 blendertouchsensor->materialName:
310                                         (blendertouchsensor->name ? blendertouchsensor->name: ""));
311                                         
312                                         
313                                         if (gameobj->GetPhysicsController())
314                                         {       
315                                                 gamesensor = new KX_TouchSensor(eventmgr,
316                                                         gameobj,
317                                                         bFindMaterial,
318                                                         touchPropOrMatName);
319                                         }
320                                         
321                                 }
322                                 
323                                 break;
324                         }
325                 case SENS_TOUCH:
326                         {
327                                 SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::TOUCH_EVENTMGR);
328                                 if (eventmgr)
329                                 {
330                                         STR_String touchpropertyname;
331                                         bTouchSensor* blendertouchsensor = (bTouchSensor*)sens->data;
332                                         
333                                         if (blendertouchsensor->ma)
334                                         {
335                                                 touchpropertyname = (char*) (blendertouchsensor->ma->id.name+2);
336                                         }
337                                         bool bFindMaterial = true;
338                                         if (gameobj->GetPhysicsController())
339                                         {       
340                                                 gamesensor = new KX_TouchSensor(eventmgr,
341                                                         gameobj,
342                                                         bFindMaterial,
343                                                         touchpropertyname);
344                                         }
345                                 }
346                                 break;
347                         }
348                 case SENS_MESSAGE:
349                         {
350                                 KX_NetworkEventManager* eventmgr = (KX_NetworkEventManager*)
351                                         logicmgr->FindEventManager(SCA_EventManager::NETWORK_EVENTMGR);
352                                 if (eventmgr) {
353                                         bMessageSensor* msgSens = (bMessageSensor*) sens->data; 
354                                         
355                                         /* Get our NetworkScene */
356                                         NG_NetworkScene *NetworkScene = kxscene->GetNetworkScene();
357                                         /* filter on the incoming subjects, might be empty */
358                                         STR_String subject = (msgSens->subject
359                                                 ? (char*)msgSens->subject
360                                                 : "");
361                                         
362                                         gamesensor = new KX_NetworkMessageSensor(
363                                                 eventmgr,               // our eventmanager
364                                                 NetworkScene,   // our NetworkScene
365                                                 gameobj,                // the sensor controlling object
366                                                 subject);               // subject to filter on
367                                 }
368                                 break;
369                         }
370                 case SENS_NEAR:
371                         {
372                                 
373                                 SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::TOUCH_EVENTMGR);
374                                 if (eventmgr)
375                                 {
376                                         STR_String nearpropertyname;    
377                                         bNearSensor* blendernearsensor = (bNearSensor*)sens->data;
378                                         if (blendernearsensor->name)
379                                         {
380                                                 // only objects that own this property will be taken into account
381                                                 nearpropertyname = (char*) blendernearsensor->name;
382                                         }
383                                         
384                                         //DT_ShapeHandle shape  =       DT_Sphere(0.0);
385                                         
386                                         // this sumoObject is not deleted by a gameobj, so delete it ourself
387                                         // later (memleaks)!
388                                         float radius = blendernearsensor->dist;
389                                         PHY__Vector3 pos;
390                                         const MT_Vector3& wpos = gameobj->NodeGetWorldPosition();
391                                         pos[0] = wpos[0];
392                                         pos[1] = wpos[1];
393                                         pos[2] = wpos[2];
394                                         pos[3] = 0.f;
395                                         bool bFindMaterial = false;
396                                         PHY_IPhysicsController* physCtrl = kxscene->GetPhysicsEnvironment()->CreateSphereController(radius,pos);
397
398                                         if (isInActiveLayer)
399                                                 kxscene->GetPhysicsEnvironment()->addSensor(physCtrl);
400
401                                                 
402
403                                         gamesensor = new KX_NearSensor(eventmgr,gameobj,
404                                                 blendernearsensor->dist,
405                                                 blendernearsensor->resetdist,
406                                                 bFindMaterial,
407                                                 nearpropertyname,kxscene,
408                                                 physCtrl
409                                                 );
410                                         
411                                 }
412                                 break;
413                         }
414                         
415                         
416                 case SENS_KEYBOARD:
417                         {
418                                 /* temporary input device, for converting the code for the keyboard sensor */
419                                 
420                                 bKeyboardSensor* blenderkeybdsensor = (bKeyboardSensor*)sens->data;
421                                 SCA_KeyboardManager* eventmgr = (SCA_KeyboardManager*) logicmgr->FindEventManager(SCA_EventManager::KEYBOARD_EVENTMGR);
422                                 if (eventmgr)
423                                 {
424                                         gamesensor = new SCA_KeyboardSensor(eventmgr,
425                                                 gReverseKeyTranslateTable[blenderkeybdsensor->key],
426                                                 blenderkeybdsensor->qual,
427                                                 blenderkeybdsensor->qual2,
428                                                 (blenderkeybdsensor->type == SENS_ALL_KEYS),
429                                                 blenderkeybdsensor->targetName,
430                                                 blenderkeybdsensor->toggleName,
431                                                 gameobj); //                    blenderkeybdsensor->pad);
432                                         
433                                 } 
434                                 
435                                 break;
436                         }
437                 case SENS_MOUSE:
438                         {
439                                 int keytype = SCA_MouseSensor::KX_MOUSESENSORMODE_NODEF;                        
440                                 int trackfocus = 0;
441                                 bMouseSensor *bmouse = (bMouseSensor *)sens->data;
442                                 
443                                 /* There are two main types of mouse sensors. If there is
444                                 * no focus-related behaviour requested, we can make do
445                                 * with a basic sensor. This cuts down memory usage and
446                                 * gives a slight performance gain. */
447                                 
448                                 SCA_MouseManager *eventmgr 
449                                         = (SCA_MouseManager*) logicmgr->FindEventManager(SCA_EventManager::MOUSE_EVENTMGR);
450                                 if (eventmgr) {
451                                         
452                                         /* Determine key mode. There is at most one active mode. */
453                                         switch (bmouse->type) {
454                                         case BL_SENS_MOUSE_LEFT_BUTTON:
455                                                 keytype = SCA_MouseSensor::KX_MOUSESENSORMODE_LEFTBUTTON;
456                                                 break;
457                                         case BL_SENS_MOUSE_MIDDLE_BUTTON:
458                                                 keytype = SCA_MouseSensor::KX_MOUSESENSORMODE_MIDDLEBUTTON;
459                                                 break;
460                                         case BL_SENS_MOUSE_RIGHT_BUTTON:
461                                                 keytype = SCA_MouseSensor::KX_MOUSESENSORMODE_RIGHTBUTTON;
462                                                 break;
463                                         case BL_SENS_MOUSE_WHEEL_UP:
464                                                 keytype = SCA_MouseSensor::KX_MOUSESENSORMODE_WHEELUP;
465                                                 break;
466                                         case BL_SENS_MOUSE_WHEEL_DOWN:
467                                                 keytype = SCA_MouseSensor::KX_MOUSESENSORMODE_WHEELDOWN;
468                                                 break;
469                                         case BL_SENS_MOUSE_MOVEMENT:
470                                                 keytype = SCA_MouseSensor::KX_MOUSESENSORMODE_MOVEMENT;
471                                                 break;
472                                         case BL_SENS_MOUSE_MOUSEOVER:
473                                                 trackfocus = 1;
474                                                 break;
475                                         case BL_SENS_MOUSE_MOUSEOVER_ANY:
476                                                 trackfocus = 2;
477                                                 break;
478
479                                         default:
480                                                 ; /* error */
481                                         }
482                                         
483                                         /* initial mouse position */                             
484                                         int startx  = canvas->GetWidth()/2;
485                                         int starty = canvas->GetHeight()/2;
486                                         
487                                         if (!trackfocus) {
488                                                 /* plain, simple mouse sensor */
489                                                 gamesensor = new SCA_MouseSensor(eventmgr,
490                                                         startx,starty,
491                                                         keytype,
492                                                         gameobj);
493                                         } else {
494                                                 /* give us a focus-aware sensor */
495                                                 gamesensor = new KX_MouseFocusSensor(eventmgr,
496                                                         startx,
497                                                         starty,
498                                                         keytype,
499                                                         trackfocus,
500                                                         canvas,
501                                                         kxscene,
502                                                         gameobj); 
503                                         }
504                                 } else {
505                                         //                              cout << "\n Could't find mouse event manager..."; - should throw an error here... 
506                                 }
507                                 break;
508                         }
509                 case SENS_PROPERTY:
510                         {
511                                 bPropertySensor* blenderpropsensor = (bPropertySensor*) sens->data;
512                                 SCA_EventManager* eventmgr 
513                                         = logicmgr->FindEventManager(SCA_EventManager::PROPERTY_EVENTMGR);
514                                 if (eventmgr)
515                                 {
516                                         STR_String propname=blenderpropsensor->name;
517                                         STR_String propval=blenderpropsensor->value;
518                                         STR_String propmaxval=blenderpropsensor->maxvalue;
519                                         
520                                         SCA_PropertySensor::KX_PROPSENSOR_TYPE 
521                                                 propchecktype = SCA_PropertySensor::KX_PROPSENSOR_NODEF;
522                                         
523                                         /* Better do an explicit conversion here! (was implicit      */
524                                         /* before...)                                                */
525                                         switch(blenderpropsensor->type) {
526                                         case SENS_PROP_EQUAL:
527                                                 propchecktype = SCA_PropertySensor::KX_PROPSENSOR_EQUAL;
528                                                 break;
529                                         case SENS_PROP_NEQUAL:
530                                                 propchecktype = SCA_PropertySensor::KX_PROPSENSOR_NOTEQUAL;
531                                                 break;
532                                         case SENS_PROP_INTERVAL:
533                                                 propchecktype = SCA_PropertySensor::KX_PROPSENSOR_INTERVAL;
534                                                 break;
535                                         case SENS_PROP_CHANGED:
536                                                 propchecktype = SCA_PropertySensor::KX_PROPSENSOR_CHANGED;
537                                                 break;
538                                         case SENS_PROP_EXPRESSION:
539                                                 propchecktype = SCA_PropertySensor::KX_PROPSENSOR_EXPRESSION;
540                                                 /* error */
541                                                 break;
542                                         default:
543                                                 ; /* error */
544                                         }
545                                         gamesensor = new SCA_PropertySensor(eventmgr,gameobj,propname,propval,propmaxval,propchecktype);
546                                 }
547                                 
548                                 break;
549                         }
550                         
551                 case SENS_RADAR:
552                         {
553                                 
554                                 SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::TOUCH_EVENTMGR);
555                                 if (eventmgr)
556                                 {
557                                         STR_String radarpropertyname;
558                                         STR_String touchpropertyname;
559                                         bRadarSensor* blenderradarsensor = (bRadarSensor*) sens->data;
560                                         
561                                         int radaraxis = blenderradarsensor->axis;
562                                         
563                                         if (blenderradarsensor->name)
564                                         {
565                                                 // only objects that own this property will be taken into account
566                                                 radarpropertyname = (char*) blenderradarsensor->name;
567                                         }
568                                         
569                                         MT_Scalar coneheight = blenderradarsensor->range;
570                                         
571                                         // janco: the angle was doubled, so should I divide the factor in 2
572                                         // or the blenderradarsensor->angle?
573                                         // nzc: the angle is the opening angle. We need to init with 
574                                         // the axis-hull angle,so /2.0.
575                                         MT_Scalar factor = tan(MT_radians((blenderradarsensor->angle)/2.0));
576                                         //MT_Scalar coneradius = coneheight * (factor / 2);
577                                         MT_Scalar coneradius = coneheight * factor;
578                                         
579                                         
580                                         // this sumoObject is not deleted by a gameobj, so delete it ourself
581                                         // later (memleaks)!
582                                         MT_Scalar smallmargin = 0.0;
583                                         MT_Scalar largemargin = 0.0;
584                                         
585                                         bool bFindMaterial = false;
586                                         PHY_IPhysicsController* ctrl = kxscene->GetPhysicsEnvironment()->CreateConeController(coneradius,coneheight);
587
588                                         gamesensor = new KX_RadarSensor(
589                                                 eventmgr,
590                                                 gameobj,
591                                                 ctrl,
592                                                 coneradius,
593                                                 coneheight,
594                                                 radaraxis,
595                                                 smallmargin,
596                                                 largemargin,
597                                                 bFindMaterial,
598                                                 radarpropertyname,
599                                                 kxscene);
600                                                 
601                                 }
602                         
603                                 break;
604                         }
605                 case SENS_RAY:
606                         {
607                                 bRaySensor* blenderraysensor = (bRaySensor*) sens->data;
608                                 
609                                 //blenderradarsensor->angle;
610                                 SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::RAY_EVENTMGR);
611                                 if (eventmgr)
612                                 {
613                                         bool bFindMaterial = (blenderraysensor->mode & SENS_COLLISION_MATERIAL);
614                                         
615                                         STR_String checkname = (bFindMaterial? blenderraysensor->matname : blenderraysensor->propname);
616
617                                         // don't want to get rays of length 0.0 or so
618                                         double distance = (blenderraysensor->range < 0.01 ? 0.01 : blenderraysensor->range );
619                                         int axis = blenderraysensor->axisflag;
620
621                                         
622                                         gamesensor = new KX_RaySensor(eventmgr,
623                                                                                                   gameobj,
624                                                                                                   checkname,
625                                                                                                   bFindMaterial,
626                                                                                                   distance,
627                                                                                                   axis,
628                                                                                                   kxscene);
629
630                                 }
631                                 break;
632                         }
633                         
634                 case SENS_RANDOM:
635                         {
636                                 bRandomSensor* blenderrndsensor = (bRandomSensor*) sens->data;
637                                 // some files didn't write randomsensor, avoid crash now for NULL ptr's
638                                 if (blenderrndsensor)
639                                 {
640                                         SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::RANDOM_EVENTMGR);
641                                         if (eventmgr)
642                                         {
643                                                 int randomSeed = blenderrndsensor->seed;
644                                                 gamesensor = new SCA_RandomSensor(eventmgr, gameobj, randomSeed);
645                                         }
646                                 }
647                                 break;
648                         }
649                 case SENS_JOYSTICK:
650                         {
651                                 int joysticktype = SCA_JoystickSensor::KX_JOYSENSORMODE_NODEF;
652                                 
653                                 bJoystickSensor* bjoy = (bJoystickSensor*) sens->data;
654                                 
655                                 SCA_JoystickManager *eventmgr 
656                                         = (SCA_JoystickManager*) logicmgr->FindEventManager(SCA_EventManager::JOY_EVENTMGR);
657                                 if (eventmgr) 
658                                 {
659                                         int axis        =0;
660                                         int axisf       =0;
661                                         int button      =0;
662                                         int buttonf =0; 
663                                         int hat         =0; 
664                                         int hatf        =0;
665                                         int prec        =0;
666                                         
667                                         switch(bjoy->type)
668                                         {
669                                         case SENS_JOY_AXIS:
670                                                 axis    = bjoy->axis;
671                                                 axisf   = bjoy->axisf;
672                                                 prec    = bjoy->precision;
673                                                 joysticktype  = SCA_JoystickSensor::KX_JOYSENSORMODE_AXIS;
674                                                 break;
675                                         case SENS_JOY_BUTTON:
676                                                 button  = bjoy->button;
677                                                 buttonf = bjoy->buttonf;
678                                                 joysticktype  = SCA_JoystickSensor::KX_JOYSENSORMODE_BUTTON;
679                                                 break;
680                                         case SENS_JOY_HAT:
681                                                 hat             = bjoy->hat;
682                                                 hatf    = bjoy->hatf;
683                                                 joysticktype  = SCA_JoystickSensor::KX_JOYSENSORMODE_HAT;
684                                                 break;
685                                         default:
686                                                 printf("Error: bad case statement\n");
687                                                 break;
688                                         }
689                                         gamesensor = new SCA_JoystickSensor(
690                                                 eventmgr,
691                                                 gameobj,
692                                                 joysticktype,
693                                                 axis,axisf,
694                                                 prec,
695                                                 button,buttonf,
696                                                 hat,hatf);
697                                 } 
698                                 else
699                                 {
700                                         printf("Error there was a problem finding the event manager\n");
701                                 }
702
703                                 break;
704                         }
705                 default:
706                         {
707                         }
708                 }
709
710                 if (gamesensor)
711                 {
712                         gamesensor->SetExecutePriority(executePriority++);
713                         STR_String uniquename = sens->name;
714                         uniquename += "#SENS#";
715                         uniqueint++;
716                         CIntValue* uniqueval = new CIntValue(uniqueint);
717                         uniquename += uniqueval->GetText();
718                         uniqueval->Release();
719                         
720                         /* Conversion succeeded, so we can set the generic props here.   */
721                         gamesensor->SetPulseMode(pos_pulsemode, 
722                                                                          neg_pulsemode, 
723                                                                          frequency);
724                         gamesensor->SetInvert(invert);
725                         gamesensor->SetName(STR_String(sens->name));                    
726                         
727                         gameobj->AddSensor(gamesensor);
728                         
729                         // only register to manager if it's in an active layer
730                         
731                         if (isInActiveLayer)
732                                 gamesensor->RegisterToManager();
733                         
734                         
735                         for (int i=0;i<sens->totlinks;i++)
736                         {
737                                 bController* linkedcont = (bController*) sens->links[i];
738                                 SCA_IController* gamecont = converter->FindGameController(linkedcont);
739
740                                 if (gamecont) {
741                                         logicmgr->RegisterToSensor(gamecont,gamesensor);
742                                 }
743                         }
744                         
745                 }
746                 sens=sens->next;
747         }
748 }
749