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