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