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