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