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