merge with 2.5 at r18679
[blender.git] / source / gameengine / Converter / KX_ConvertSensors.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL 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.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * Conversion of Blender data blocks to KX sensor system
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #ifdef WIN32
36 #pragma warning (disable : 4786)
37 #endif //WIN32
38
39 #include "wm_event_types.h"
40 #include "KX_BlenderSceneConverter.h"
41 #include "KX_ConvertSensors.h"
42
43 /* This little block needed for linking to Blender... */
44 #ifdef WIN32
45 #include "BLI_winstuff.h"
46 #endif
47
48 #include "DNA_object_types.h"
49 #include "DNA_material_types.h"
50 #include "DNA_sensor_types.h"
51 #include "DNA_actuator_types.h" /* for SENS_ALL_KEYS ? this define is
52 probably misplaced */
53 /* end of blender include block */
54
55 #include "RAS_IPolygonMaterial.h"
56 // Sensors
57 #include "KX_GameObject.h"
58 #include "RAS_MeshObject.h"
59 #include "SCA_KeyboardSensor.h"
60 #include "SCA_MouseSensor.h"
61 #include "SCA_AlwaysSensor.h"
62 #include "KX_TouchSensor.h"
63 #include "KX_NearSensor.h"
64 #include "KX_RadarSensor.h"
65 #include "KX_MouseFocusSensor.h"
66 #include "SCA_JoystickSensor.h"
67 #include "KX_NetworkMessageSensor.h"
68 #include "SCA_ActuatorSensor.h"
69 #include "SCA_DelaySensor.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                                            KX_KetsjiEngine* kxengine,
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         
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 //XXX clean up
147 #ifdef WIN32
148 #define HKEY    'h'
149 #endif
150         gReverseKeyTranslateTable[HKEY                          ] = SCA_IInputDevice::KX_HKEY;                  
151 //XXX clean up
152 #ifdef WIN32
153 #undef HKEY
154 #endif
155         gReverseKeyTranslateTable[IKEY                          ] = SCA_IInputDevice::KX_IKEY;                  
156         gReverseKeyTranslateTable[JKEY                          ] = SCA_IInputDevice::KX_JKEY;                  
157         gReverseKeyTranslateTable[KKEY                          ] = SCA_IInputDevice::KX_KKEY;                  
158         gReverseKeyTranslateTable[LKEY                          ] = SCA_IInputDevice::KX_LKEY;                  
159         gReverseKeyTranslateTable[MKEY                          ] = SCA_IInputDevice::KX_MKEY;                  
160         gReverseKeyTranslateTable[NKEY                          ] = SCA_IInputDevice::KX_NKEY;                  
161         gReverseKeyTranslateTable[OKEY                          ] = SCA_IInputDevice::KX_OKEY;                  
162         gReverseKeyTranslateTable[PKEY                          ] = SCA_IInputDevice::KX_PKEY;                  
163         gReverseKeyTranslateTable[QKEY                          ] = SCA_IInputDevice::KX_QKEY;                  
164         gReverseKeyTranslateTable[RKEY                          ] = SCA_IInputDevice::KX_RKEY;                  
165         gReverseKeyTranslateTable[SKEY                          ] = SCA_IInputDevice::KX_SKEY;                  
166         gReverseKeyTranslateTable[TKEY                          ] = SCA_IInputDevice::KX_TKEY;                  
167         gReverseKeyTranslateTable[UKEY                          ] = SCA_IInputDevice::KX_UKEY;                  
168         gReverseKeyTranslateTable[VKEY                          ] = SCA_IInputDevice::KX_VKEY;                  
169         gReverseKeyTranslateTable[WKEY                          ] = SCA_IInputDevice::KX_WKEY;                  
170         gReverseKeyTranslateTable[XKEY                          ] = SCA_IInputDevice::KX_XKEY;                  
171         gReverseKeyTranslateTable[YKEY                          ] = SCA_IInputDevice::KX_YKEY;                  
172         gReverseKeyTranslateTable[ZKEY                          ] = SCA_IInputDevice::KX_ZKEY;                  
173         
174         gReverseKeyTranslateTable[ZEROKEY                       ] = SCA_IInputDevice::KX_ZEROKEY;                  
175         gReverseKeyTranslateTable[ONEKEY                        ] = SCA_IInputDevice::KX_ONEKEY;                  
176         gReverseKeyTranslateTable[TWOKEY                        ] = SCA_IInputDevice::KX_TWOKEY;                  
177         gReverseKeyTranslateTable[THREEKEY                      ] = SCA_IInputDevice::KX_THREEKEY;                  
178         gReverseKeyTranslateTable[FOURKEY                       ] = SCA_IInputDevice::KX_FOURKEY;                  
179         gReverseKeyTranslateTable[FIVEKEY                       ] = SCA_IInputDevice::KX_FIVEKEY;                  
180         gReverseKeyTranslateTable[SIXKEY                        ] = SCA_IInputDevice::KX_SIXKEY;                  
181         gReverseKeyTranslateTable[SEVENKEY                      ] = SCA_IInputDevice::KX_SEVENKEY;                  
182         gReverseKeyTranslateTable[EIGHTKEY                      ] = SCA_IInputDevice::KX_EIGHTKEY;                  
183         gReverseKeyTranslateTable[NINEKEY                       ] = SCA_IInputDevice::KX_NINEKEY;                  
184         
185         gReverseKeyTranslateTable[CAPSLOCKKEY           ] = SCA_IInputDevice::KX_CAPSLOCKKEY;                  
186         
187         gReverseKeyTranslateTable[LEFTCTRLKEY           ] = SCA_IInputDevice::KX_LEFTCTRLKEY;                  
188         gReverseKeyTranslateTable[LEFTALTKEY            ] = SCA_IInputDevice::KX_LEFTALTKEY;                  
189         gReverseKeyTranslateTable[RIGHTALTKEY           ] = SCA_IInputDevice::KX_RIGHTALTKEY;                  
190         gReverseKeyTranslateTable[RIGHTCTRLKEY          ] = SCA_IInputDevice::KX_RIGHTCTRLKEY;                  
191         gReverseKeyTranslateTable[RIGHTSHIFTKEY         ] = SCA_IInputDevice::KX_RIGHTSHIFTKEY;                  
192         gReverseKeyTranslateTable[LEFTSHIFTKEY          ] = SCA_IInputDevice::KX_LEFTSHIFTKEY;                  
193         
194         gReverseKeyTranslateTable[ESCKEY                        ] = SCA_IInputDevice::KX_ESCKEY;                  
195         gReverseKeyTranslateTable[TABKEY                        ] = SCA_IInputDevice::KX_TABKEY;                  
196         gReverseKeyTranslateTable[RETKEY                        ] = SCA_IInputDevice::KX_RETKEY;                  
197         gReverseKeyTranslateTable[SPACEKEY                      ] = SCA_IInputDevice::KX_SPACEKEY;                  
198         gReverseKeyTranslateTable[LINEFEEDKEY           ] = SCA_IInputDevice::KX_LINEFEEDKEY;                  
199         gReverseKeyTranslateTable[BACKSPACEKEY          ] = SCA_IInputDevice::KX_BACKSPACEKEY;                  
200         gReverseKeyTranslateTable[DELKEY                        ] = SCA_IInputDevice::KX_DELKEY;                  
201         gReverseKeyTranslateTable[SEMICOLONKEY          ] = SCA_IInputDevice::KX_SEMICOLONKEY;                  
202         gReverseKeyTranslateTable[PERIODKEY                     ] = SCA_IInputDevice::KX_PERIODKEY;                  
203         gReverseKeyTranslateTable[COMMAKEY                      ] = SCA_IInputDevice::KX_COMMAKEY;                  
204         gReverseKeyTranslateTable[QUOTEKEY                      ] = SCA_IInputDevice::KX_QUOTEKEY;                  
205         gReverseKeyTranslateTable[ACCENTGRAVEKEY        ] = SCA_IInputDevice::KX_ACCENTGRAVEKEY;                  
206         gReverseKeyTranslateTable[MINUSKEY                      ] = SCA_IInputDevice::KX_MINUSKEY;                  
207         gReverseKeyTranslateTable[SLASHKEY                      ] = SCA_IInputDevice::KX_SLASHKEY;                  
208         gReverseKeyTranslateTable[BACKSLASHKEY          ] = SCA_IInputDevice::KX_BACKSLASHKEY;                  
209         gReverseKeyTranslateTable[EQUALKEY                      ] = SCA_IInputDevice::KX_EQUALKEY;                  
210         gReverseKeyTranslateTable[LEFTBRACKETKEY        ] = SCA_IInputDevice::KX_LEFTBRACKETKEY;                  
211         gReverseKeyTranslateTable[RIGHTBRACKETKEY       ] = SCA_IInputDevice::KX_RIGHTBRACKETKEY;                  
212         
213         gReverseKeyTranslateTable[LEFTARROWKEY          ] = SCA_IInputDevice::KX_LEFTARROWKEY;                  
214         gReverseKeyTranslateTable[DOWNARROWKEY          ] = SCA_IInputDevice::KX_DOWNARROWKEY;                  
215         gReverseKeyTranslateTable[RIGHTARROWKEY         ] = SCA_IInputDevice::KX_RIGHTARROWKEY;                  
216         gReverseKeyTranslateTable[UPARROWKEY            ] = SCA_IInputDevice::KX_UPARROWKEY;                  
217         
218         gReverseKeyTranslateTable[PAD2                          ] = SCA_IInputDevice::KX_PAD2;                  
219         gReverseKeyTranslateTable[PAD4                          ] = SCA_IInputDevice::KX_PAD4;                  
220         gReverseKeyTranslateTable[PAD6                          ] = SCA_IInputDevice::KX_PAD6;                  
221         gReverseKeyTranslateTable[PAD8                          ] = SCA_IInputDevice::KX_PAD8;                  
222         
223         gReverseKeyTranslateTable[PAD1                          ] = SCA_IInputDevice::KX_PAD1;                  
224         gReverseKeyTranslateTable[PAD3                          ] = SCA_IInputDevice::KX_PAD3;                  
225         gReverseKeyTranslateTable[PAD5                          ] = SCA_IInputDevice::KX_PAD5;                  
226         gReverseKeyTranslateTable[PAD7                          ] = SCA_IInputDevice::KX_PAD7;                  
227         gReverseKeyTranslateTable[PAD9                          ] = SCA_IInputDevice::KX_PAD9;                  
228         
229         gReverseKeyTranslateTable[PADPERIOD                     ] = SCA_IInputDevice::KX_PADPERIOD;                  
230         gReverseKeyTranslateTable[PADSLASHKEY           ] = SCA_IInputDevice::KX_PADSLASHKEY;                  
231         gReverseKeyTranslateTable[PADASTERKEY           ] = SCA_IInputDevice::KX_PADASTERKEY;                  
232         
233         gReverseKeyTranslateTable[PAD0                          ] = SCA_IInputDevice::KX_PAD0;                  
234         gReverseKeyTranslateTable[PADMINUS                      ] = SCA_IInputDevice::KX_PADMINUS;                  
235         gReverseKeyTranslateTable[PADENTER                      ] = SCA_IInputDevice::KX_PADENTER;                  
236         gReverseKeyTranslateTable[PADPLUSKEY            ] = SCA_IInputDevice::KX_PADPLUSKEY;                  
237         
238         
239         gReverseKeyTranslateTable[F1KEY                         ] = SCA_IInputDevice::KX_F1KEY;                  
240         gReverseKeyTranslateTable[F2KEY                         ] = SCA_IInputDevice::KX_F2KEY;                  
241         gReverseKeyTranslateTable[F3KEY                         ] = SCA_IInputDevice::KX_F3KEY;                  
242         gReverseKeyTranslateTable[F4KEY                         ] = SCA_IInputDevice::KX_F4KEY;                  
243         gReverseKeyTranslateTable[F5KEY                         ] = SCA_IInputDevice::KX_F5KEY;                  
244         gReverseKeyTranslateTable[F6KEY                         ] = SCA_IInputDevice::KX_F6KEY;                  
245         gReverseKeyTranslateTable[F7KEY                         ] = SCA_IInputDevice::KX_F7KEY;                  
246         gReverseKeyTranslateTable[F8KEY                         ] = SCA_IInputDevice::KX_F8KEY;                  
247         gReverseKeyTranslateTable[F9KEY                         ] = SCA_IInputDevice::KX_F9KEY;                  
248         gReverseKeyTranslateTable[F10KEY                        ] = SCA_IInputDevice::KX_F10KEY;                  
249         gReverseKeyTranslateTable[F11KEY                        ] = SCA_IInputDevice::KX_F11KEY;                  
250         gReverseKeyTranslateTable[F12KEY                        ] = SCA_IInputDevice::KX_F12KEY;                  
251         
252         gReverseKeyTranslateTable[PAUSEKEY                      ] = SCA_IInputDevice::KX_PAUSEKEY;                  
253         gReverseKeyTranslateTable[INSERTKEY                     ] = SCA_IInputDevice::KX_INSERTKEY;                  
254         gReverseKeyTranslateTable[HOMEKEY                       ] = SCA_IInputDevice::KX_HOMEKEY;                  
255         gReverseKeyTranslateTable[PAGEUPKEY                     ] = SCA_IInputDevice::KX_PAGEUPKEY;                  
256         gReverseKeyTranslateTable[PAGEDOWNKEY           ] = SCA_IInputDevice::KX_PAGEDOWNKEY;                  
257         gReverseKeyTranslateTable[ENDKEY                        ] = SCA_IInputDevice::KX_ENDKEY;
258         
259         int uniqueint = 0;
260         bSensor* sens = (bSensor*)blenderobject->sensors.first;
261         bool pos_pulsemode = false;
262         bool neg_pulsemode = false;
263         int frequency = 0;
264         bool invert = false;
265         bool level = 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                 level     = !(sens->level == 0);
279
280                 switch (sens->type)
281                 {
282                 case  SENS_ALWAYS:
283                         {
284                                 
285                                 SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::ALWAYS_EVENTMGR);
286                                 if (eventmgr)
287                                 {
288                                         gamesensor = new SCA_AlwaysSensor(eventmgr, gameobj);
289                                 }
290                                 
291                                 break;
292                         }
293                         
294                 case  SENS_DELAY:
295                         {
296                                 // we can reuse the Always event manager for the delay sensor
297                                 SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::ALWAYS_EVENTMGR);
298                                 if (eventmgr)
299                                 {
300                                         bDelaySensor* delaysensor = (bDelaySensor*)sens->data;
301                                         gamesensor = new SCA_DelaySensor(eventmgr, 
302                                                 gameobj,
303                                                 delaysensor->delay,
304                                                 delaysensor->duration,
305                                                 (delaysensor->flag & SENS_DELAY_REPEAT) != 0);
306                                 }
307                                 break;
308                         }
309
310                 case SENS_COLLISION:
311                         {
312                                 SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::TOUCH_EVENTMGR);
313                                 if (eventmgr)
314                                 {
315                                         // collision sensor can sense both materials and properties. 
316                                         
317                                         bool bFindMaterial = false;
318                                         
319                                         bCollisionSensor* blendertouchsensor = (bCollisionSensor*)sens->data;
320                                         
321                                         bFindMaterial = (blendertouchsensor->mode 
322                                                 & SENS_COLLISION_MATERIAL);
323                                         
324                                         
325                                         STR_String touchPropOrMatName = ( bFindMaterial ? 
326                                                 blendertouchsensor->materialName:
327                                         (blendertouchsensor->name ? blendertouchsensor->name: ""));
328                                         
329                                         
330                                         if (gameobj->GetPhysicsController())
331                                         {       
332                                                 gamesensor = new KX_TouchSensor(eventmgr,
333                                                         gameobj,
334                                                         bFindMaterial,
335                                                         touchPropOrMatName);
336                                         }
337                                         
338                                 }
339                                 
340                                 break;
341                         }
342                 case SENS_TOUCH:
343                         {
344                                 SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::TOUCH_EVENTMGR);
345                                 if (eventmgr)
346                                 {
347                                         STR_String touchpropertyname;
348                                         bTouchSensor* blendertouchsensor = (bTouchSensor*)sens->data;
349                                         
350                                         if (blendertouchsensor->ma)
351                                         {
352                                                 touchpropertyname = (char*) (blendertouchsensor->ma->id.name+2);
353                                         }
354                                         bool bFindMaterial = true;
355                                         if (gameobj->GetPhysicsController())
356                                         {       
357                                                 gamesensor = new KX_TouchSensor(eventmgr,
358                                                         gameobj,
359                                                         bFindMaterial,
360                                                         touchpropertyname);
361                                         }
362                                 }
363                                 break;
364                         }
365                 case SENS_MESSAGE:
366                         {
367                                 KX_NetworkEventManager* eventmgr = (KX_NetworkEventManager*)
368                                         logicmgr->FindEventManager(SCA_EventManager::NETWORK_EVENTMGR);
369                                 if (eventmgr) {
370                                         bMessageSensor* msgSens = (bMessageSensor*) sens->data; 
371                                         
372                                         /* Get our NetworkScene */
373                                         NG_NetworkScene *NetworkScene = kxscene->GetNetworkScene();
374                                         /* filter on the incoming subjects, might be empty */
375                                         STR_String subject = (msgSens->subject
376                                                 ? (char*)msgSens->subject
377                                                 : "");
378                                         
379                                         gamesensor = new KX_NetworkMessageSensor(
380                                                 eventmgr,               // our eventmanager
381                                                 NetworkScene,   // our NetworkScene
382                                                 gameobj,                // the sensor controlling object
383                                                 subject);               // subject to filter on
384                                 }
385                                 break;
386                         }
387                 case SENS_NEAR:
388                         {
389                                 
390                                 SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::TOUCH_EVENTMGR);
391                                 if (eventmgr)
392                                 {
393                                         STR_String nearpropertyname;    
394                                         bNearSensor* blendernearsensor = (bNearSensor*)sens->data;
395                                         if (blendernearsensor->name)
396                                         {
397                                                 // only objects that own this property will be taken into account
398                                                 nearpropertyname = (char*) blendernearsensor->name;
399                                         }
400                                         
401                                         //DT_ShapeHandle shape  =       DT_Sphere(0.0);
402                                         
403                                         // this sumoObject is not deleted by a gameobj, so delete it ourself
404                                         // later (memleaks)!
405                                         float radius = blendernearsensor->dist;
406                                         PHY__Vector3 pos;
407                                         const MT_Vector3& wpos = gameobj->NodeGetWorldPosition();
408                                         pos[0] = wpos[0];
409                                         pos[1] = wpos[1];
410                                         pos[2] = wpos[2];
411                                         pos[3] = 0.f;
412                                         bool bFindMaterial = false;
413                                         PHY_IPhysicsController* physCtrl = kxscene->GetPhysicsEnvironment()->CreateSphereController(radius,pos);
414
415                                         //will be done in KX_TouchEventManager::RegisterSensor()  
416                                         //if (isInActiveLayer)
417                                         //      kxscene->GetPhysicsEnvironment()->addSensor(physCtrl);
418
419                                                 
420
421                                         gamesensor = new KX_NearSensor(eventmgr,gameobj,
422                                                 blendernearsensor->dist,
423                                                 blendernearsensor->resetdist,
424                                                 bFindMaterial,
425                                                 nearpropertyname,kxscene,
426                                                 physCtrl
427                                                 );
428                                         
429                                 }
430                                 break;
431                         }
432                         
433                         
434                 case SENS_KEYBOARD:
435                         {
436                                 /* temporary input device, for converting the code for the keyboard sensor */
437                                 
438                                 bKeyboardSensor* blenderkeybdsensor = (bKeyboardSensor*)sens->data;
439                                 SCA_KeyboardManager* eventmgr = (SCA_KeyboardManager*) logicmgr->FindEventManager(SCA_EventManager::KEYBOARD_EVENTMGR);
440                                 if (eventmgr)
441                                 {
442                                         gamesensor = new SCA_KeyboardSensor(eventmgr,
443                                                 gReverseKeyTranslateTable[blenderkeybdsensor->key],
444                                                 gReverseKeyTranslateTable[blenderkeybdsensor->qual],
445                                                 gReverseKeyTranslateTable[blenderkeybdsensor->qual2],
446                                                 (blenderkeybdsensor->type == SENS_ALL_KEYS),
447                                                 blenderkeybdsensor->targetName,
448                                                 blenderkeybdsensor->toggleName,
449                                                 gameobj); //                    blenderkeybdsensor->pad);
450                                         
451                                 } 
452                                 
453                                 break;
454                         }
455                 case SENS_MOUSE:
456                         {
457                                 int keytype = SCA_MouseSensor::KX_MOUSESENSORMODE_NODEF;                        
458                                 int trackfocus = 0;
459                                 bMouseSensor *bmouse = (bMouseSensor *)sens->data;
460                                 
461                                 /* There are two main types of mouse sensors. If there is
462                                 * no focus-related behaviour requested, we can make do
463                                 * with a basic sensor. This cuts down memory usage and
464                                 * gives a slight performance gain. */
465                                 
466                                 SCA_MouseManager *eventmgr 
467                                         = (SCA_MouseManager*) logicmgr->FindEventManager(SCA_EventManager::MOUSE_EVENTMGR);
468                                 if (eventmgr) {
469                                         
470                                         /* Determine key mode. There is at most one active mode. */
471                                         switch (bmouse->type) {
472                                         case BL_SENS_MOUSE_LEFT_BUTTON:
473                                                 keytype = SCA_MouseSensor::KX_MOUSESENSORMODE_LEFTBUTTON;
474                                                 break;
475                                         case BL_SENS_MOUSE_MIDDLE_BUTTON:
476                                                 keytype = SCA_MouseSensor::KX_MOUSESENSORMODE_MIDDLEBUTTON;
477                                                 break;
478                                         case BL_SENS_MOUSE_RIGHT_BUTTON:
479                                                 keytype = SCA_MouseSensor::KX_MOUSESENSORMODE_RIGHTBUTTON;
480                                                 break;
481                                         case BL_SENS_MOUSE_WHEEL_UP:
482                                                 keytype = SCA_MouseSensor::KX_MOUSESENSORMODE_WHEELUP;
483                                                 break;
484                                         case BL_SENS_MOUSE_WHEEL_DOWN:
485                                                 keytype = SCA_MouseSensor::KX_MOUSESENSORMODE_WHEELDOWN;
486                                                 break;
487                                         case BL_SENS_MOUSE_MOVEMENT:
488                                                 keytype = SCA_MouseSensor::KX_MOUSESENSORMODE_MOVEMENT;
489                                                 break;
490                                         case BL_SENS_MOUSE_MOUSEOVER:
491                                                 trackfocus = 1;
492                                                 break;
493                                         case BL_SENS_MOUSE_MOUSEOVER_ANY:
494                                                 trackfocus = 2;
495                                                 break;
496
497                                         default:
498                                                 ; /* error */
499                                         }
500                                         
501                                         /* initial mouse position */                             
502                                         int startx  = canvas->GetWidth()/2;
503                                         int starty = canvas->GetHeight()/2;
504                                         
505                                         if (!trackfocus) {
506                                                 /* plain, simple mouse sensor */
507                                                 gamesensor = new SCA_MouseSensor(eventmgr,
508                                                         startx,starty,
509                                                         keytype,
510                                                         gameobj);
511                                         } else {
512                                                 /* give us a focus-aware sensor */
513                                                 gamesensor = new KX_MouseFocusSensor(eventmgr,
514                                                         startx,
515                                                         starty,
516                                                         keytype,
517                                                         trackfocus,
518                                                         canvas,
519                                                         kxscene,
520                                                         kxengine,
521                                                         gameobj); 
522                                         }
523                                 } else {
524                                         //                              cout << "\n Could't find mouse event manager..."; - should throw an error here... 
525                                 }
526                                 break;
527                         }
528                 case SENS_PROPERTY:
529                         {
530                                 bPropertySensor* blenderpropsensor = (bPropertySensor*) sens->data;
531                                 SCA_EventManager* eventmgr 
532                                         = logicmgr->FindEventManager(SCA_EventManager::PROPERTY_EVENTMGR);
533                                 if (eventmgr)
534                                 {
535                                         STR_String propname=blenderpropsensor->name;
536                                         STR_String propval=blenderpropsensor->value;
537                                         STR_String propmaxval=blenderpropsensor->maxvalue;
538                                         
539                                         SCA_PropertySensor::KX_PROPSENSOR_TYPE 
540                                                 propchecktype = SCA_PropertySensor::KX_PROPSENSOR_NODEF;
541                                         
542                                         /* Better do an explicit conversion here! (was implicit      */
543                                         /* before...)                                                */
544                                         switch(blenderpropsensor->type) {
545                                         case SENS_PROP_EQUAL:
546                                                 propchecktype = SCA_PropertySensor::KX_PROPSENSOR_EQUAL;
547                                                 break;
548                                         case SENS_PROP_NEQUAL:
549                                                 propchecktype = SCA_PropertySensor::KX_PROPSENSOR_NOTEQUAL;
550                                                 break;
551                                         case SENS_PROP_INTERVAL:
552                                                 propchecktype = SCA_PropertySensor::KX_PROPSENSOR_INTERVAL;
553                                                 break;
554                                         case SENS_PROP_CHANGED:
555                                                 propchecktype = SCA_PropertySensor::KX_PROPSENSOR_CHANGED;
556                                                 break;
557                                         case SENS_PROP_EXPRESSION:
558                                                 propchecktype = SCA_PropertySensor::KX_PROPSENSOR_EXPRESSION;
559                                                 /* error */
560                                                 break;
561                                         default:
562                                                 ; /* error */
563                                         }
564                                         gamesensor = new SCA_PropertySensor(eventmgr,gameobj,propname,propval,propmaxval,propchecktype);
565                                 }
566                                 
567                                 break;
568                         }
569                 case SENS_ACTUATOR:
570                         {
571                                 bActuatorSensor* blenderactsensor = (bActuatorSensor*) sens->data;
572                                 // we will reuse the property event manager, there is nothing special with this sensor
573                                 SCA_EventManager* eventmgr 
574                                         = logicmgr->FindEventManager(SCA_EventManager::ACTUATOR_EVENTMGR);
575                                 if (eventmgr)
576                                 {
577                                         STR_String propname=blenderactsensor->name;
578                                         gamesensor = new SCA_ActuatorSensor(eventmgr,gameobj,propname);
579                                 }
580                                 break;
581                         }
582                         
583                 case SENS_RADAR:
584                         {
585                                 
586                                 SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::TOUCH_EVENTMGR);
587                                 if (eventmgr)
588                                 {
589                                         STR_String radarpropertyname;
590                                         STR_String touchpropertyname;
591                                         bRadarSensor* blenderradarsensor = (bRadarSensor*) sens->data;
592                                         
593                                         int radaraxis = blenderradarsensor->axis;
594                                         
595                                         if (blenderradarsensor->name)
596                                         {
597                                                 // only objects that own this property will be taken into account
598                                                 radarpropertyname = (char*) blenderradarsensor->name;
599                                         }
600                                         
601                                         MT_Scalar coneheight = blenderradarsensor->range;
602                                         
603                                         // janco: the angle was doubled, so should I divide the factor in 2
604                                         // or the blenderradarsensor->angle?
605                                         // nzc: the angle is the opening angle. We need to init with 
606                                         // the axis-hull angle,so /2.0.
607                                         MT_Scalar factor = tan(MT_radians((blenderradarsensor->angle)/2.0));
608                                         //MT_Scalar coneradius = coneheight * (factor / 2);
609                                         MT_Scalar coneradius = coneheight * factor;
610                                         
611                                         
612                                         // this sumoObject is not deleted by a gameobj, so delete it ourself
613                                         // later (memleaks)!
614                                         MT_Scalar smallmargin = 0.0;
615                                         MT_Scalar largemargin = 0.0;
616                                         
617                                         bool bFindMaterial = false;
618                                         PHY_IPhysicsController* ctrl = kxscene->GetPhysicsEnvironment()->CreateConeController(coneradius,coneheight);
619
620                                         gamesensor = new KX_RadarSensor(
621                                                 eventmgr,
622                                                 gameobj,
623                                                 ctrl,
624                                                 coneradius,
625                                                 coneheight,
626                                                 radaraxis,
627                                                 smallmargin,
628                                                 largemargin,
629                                                 bFindMaterial,
630                                                 radarpropertyname,
631                                                 kxscene);
632                                                 
633                                 }
634                         
635                                 break;
636                         }
637                 case SENS_RAY:
638                         {
639                                 bRaySensor* blenderraysensor = (bRaySensor*) sens->data;
640                                 
641                                 //blenderradarsensor->angle;
642                                 SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::RAY_EVENTMGR);
643                                 if (eventmgr)
644                                 {
645                                         bool bFindMaterial = (blenderraysensor->mode & SENS_COLLISION_MATERIAL);
646                                         bool bXRay = (blenderraysensor->mode & SENS_RAY_XRAY);
647                                         
648                                         STR_String checkname = (bFindMaterial? blenderraysensor->matname : blenderraysensor->propname);
649
650                                         // don't want to get rays of length 0.0 or so
651                                         double distance = (blenderraysensor->range < 0.01 ? 0.01 : blenderraysensor->range );
652                                         int axis = blenderraysensor->axisflag;
653
654                                         
655                                         gamesensor = new KX_RaySensor(eventmgr,
656                                                                                                   gameobj,
657                                                                                                   checkname,
658                                                                                                   bFindMaterial,
659                                                                                                   bXRay,
660                                                                                                   distance,
661                                                                                                   axis,
662                                                                                                   kxscene);
663
664                                 }
665                                 break;
666                         }
667                         
668                 case SENS_RANDOM:
669                         {
670                                 bRandomSensor* blenderrndsensor = (bRandomSensor*) sens->data;
671                                 // some files didn't write randomsensor, avoid crash now for NULL ptr's
672                                 if (blenderrndsensor)
673                                 {
674                                         SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::RANDOM_EVENTMGR);
675                                         if (eventmgr)
676                                         {
677                                                 int randomSeed = blenderrndsensor->seed;
678                                                 gamesensor = new SCA_RandomSensor(eventmgr, gameobj, randomSeed);
679                                         }
680                                 }
681                                 break;
682                         }
683                 case SENS_JOYSTICK:
684                         {
685                                 int joysticktype = SCA_JoystickSensor::KX_JOYSENSORMODE_NODEF;
686                                 
687                                 bJoystickSensor* bjoy = (bJoystickSensor*) sens->data;
688                                 
689                                 SCA_JoystickManager *eventmgr 
690                                         = (SCA_JoystickManager*) logicmgr->FindEventManager(SCA_EventManager::JOY_EVENTMGR);
691                                 if (eventmgr) 
692                                 {
693                                         int axis        =0;
694                                         int axisf       =0;
695                                         int button      =0;
696                                         int hat         =0; 
697                                         int hatf        =0;
698                                         int prec        =0;
699                                         
700                                         switch(bjoy->type)
701                                         {
702                                         case SENS_JOY_AXIS:
703                                                 axis    = bjoy->axis;
704                                                 axisf   = bjoy->axisf;
705                                                 prec    = bjoy->precision;
706                                                 joysticktype  = SCA_JoystickSensor::KX_JOYSENSORMODE_AXIS;
707                                                 break;
708                                         case SENS_JOY_BUTTON:
709                                                 button  = bjoy->button;
710                                                 joysticktype  = SCA_JoystickSensor::KX_JOYSENSORMODE_BUTTON;
711                                                 break;
712                                         case SENS_JOY_HAT:
713                                                 hat             = bjoy->hat;
714                                                 hatf    = bjoy->hatf;
715                                                 joysticktype  = SCA_JoystickSensor::KX_JOYSENSORMODE_HAT;
716                                                 break;
717                                         default:
718                                                 printf("Error: bad case statement\n");
719                                                 break;
720                                         }
721                                         gamesensor = new SCA_JoystickSensor(
722                                                 eventmgr,
723                                                 gameobj,
724                                                 bjoy->joyindex,
725                                                 joysticktype,
726                                                 axis,axisf,
727                                                 prec,
728                                                 button,
729                                                 hat,hatf,
730                                                 (bjoy->flag & SENS_JOY_ANY_EVENT));
731                                 } 
732                                 else
733                                 {
734                                         printf("Error there was a problem finding the event manager\n");
735                                 }
736
737                                 break;
738                         }
739                 default:
740                         {
741                         }
742                 }
743
744                 if (gamesensor)
745                 {
746                         gamesensor->SetExecutePriority(executePriority++);
747                         STR_String uniquename = sens->name;
748                         uniquename += "#SENS#";
749                         uniqueint++;
750                         CIntValue* uniqueval = new CIntValue(uniqueint);
751                         uniquename += uniqueval->GetText();
752                         uniqueval->Release();
753                         
754                         /* Conversion succeeded, so we can set the generic props here.   */
755                         gamesensor->SetPulseMode(pos_pulsemode, 
756                                                                          neg_pulsemode, 
757                                                                          frequency);
758                         gamesensor->SetInvert(invert);
759                         gamesensor->SetLevel(level);
760                         gamesensor->SetName(STR_String(sens->name));                    
761                         
762                         gameobj->AddSensor(gamesensor);
763                         
764                         // only register to manager if it's in an active layer
765                         // Make registration dynamic: only when sensor is activated
766                         //if (isInActiveLayer)
767                         //      gamesensor->RegisterToManager();
768                         
769                         
770                         for (int i=0;i<sens->totlinks;i++)
771                         {
772                                 bController* linkedcont = (bController*) sens->links[i];
773                                 if (linkedcont) {
774                                         SCA_IController* gamecont = converter->FindGameController(linkedcont);
775
776                                         if (gamecont) {
777                                                 logicmgr->RegisterToSensor(gamecont,gamesensor);
778                                         } else {
779                                                 printf(
780                                                         "Warning, sensor \"%s\" could not find its controller "
781                                                         "(link %d of %d) from object \"%s\"\n"
782                                                         "\tthere has been an error converting the blender controller for the game engine,"
783                                                         "logic may be incorrect\n", sens->name, i+1, sens->totlinks, blenderobject->id.name+2);
784                                         }
785                                 } else {
786                                         printf(
787                                                 "Warning, sensor \"%s\" has lost a link to a controller "
788                                                 "(link %d of %d) from object \"%s\"\n"
789                                                 "\tpossible causes are partially appended objects or an error reading the file,"
790                                                 "logic may be incorrect\n", sens->name, i+1, sens->totlinks, blenderobject->id.name+2);
791                                 }
792                         }
793                         // special case: Keyboard sensor with no link
794                         // this combination is usually used for key logging. 
795                         if (sens->type == SENS_KEYBOARD && sens->totlinks == 0) {
796                                 // Force the registration so that the sensor runs
797                                 gamesensor->IncLink();
798                         }
799                                 
800                         // done with gamesensor
801                         gamesensor->Release();
802                         
803                 }
804                 sens=sens->next;
805         }
806 }
807