svn merge -r39930:39947 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / gameengine / Converter / KX_ConvertActuators.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 * Convert Blender actuators for use in the GameEngine
29 */
30
31 /** \file gameengine/Converter/KX_ConvertActuators.cpp
32  *  \ingroup bgeconv
33  */
34
35
36 #if defined(WIN32) && !defined(FREE_WINDOWS)
37 #pragma warning (disable : 4786) 
38 #endif //WIN32
39
40 #include <math.h>
41
42 #include "MEM_guardedalloc.h"
43
44 #include "KX_BlenderSceneConverter.h"
45 #include "KX_ConvertActuators.h"
46
47 #ifdef WITH_AUDASPACE
48 #  include "AUD_C-API.h"
49 #  include "AUD_ChannelMapperFactory.h"
50 #endif
51
52 // Actuators
53 //SCA logiclibrary native logicbricks
54 #include "SCA_PropertyActuator.h"
55 #include "SCA_LogicManager.h"
56 #include "SCA_RandomActuator.h"
57 #include "SCA_2DFilterActuator.h"
58
59 // Ketsji specific logicbricks
60 #include "KX_SceneActuator.h"
61 #include "KX_IpoActuator.h"
62 #include "KX_SoundActuator.h"
63 #include "KX_ObjectActuator.h"
64 #include "KX_TrackToActuator.h"
65 #include "KX_ConstraintActuator.h"
66 #include "KX_CameraActuator.h"
67 #include "KX_GameActuator.h"
68 #include "KX_StateActuator.h"
69 #include "KX_VisibilityActuator.h"
70 #include "KX_SCA_AddObjectActuator.h"
71 #include "KX_SCA_EndObjectActuator.h"
72 #include "KX_SCA_ReplaceMeshActuator.h"
73 #include "KX_ParentActuator.h"
74 #include "KX_SCA_DynamicActuator.h"
75
76 #include "KX_Scene.h"
77 #include "KX_KetsjiEngine.h"
78
79 #include "IntValue.h"
80 #include "KX_GameObject.h"
81
82 /* This little block needed for linking to Blender... */
83 #include "BKE_text.h"
84 #include "BLI_blenlib.h"
85 #include "BLI_math_base.h"
86
87 #define FILE_MAX 240 // repeated here to avoid dependency from BKE_utildefines.h
88
89 #include "KX_NetworkMessageActuator.h"
90
91 #ifdef WIN32
92 #include "BLI_winstuff.h"
93 #endif
94
95 #include "DNA_object_types.h"
96 #include "DNA_sound_types.h"
97 #include "DNA_scene_types.h"
98 #include "DNA_actuator_types.h"
99 #include "DNA_packedFile_types.h"
100 #include "BL_ActionActuator.h"
101 #include "BL_ShapeActionActuator.h"
102 #include "BL_ArmatureActuator.h"
103 #include "BL_Action.h"
104 /* end of blender include block */
105
106 #include "BL_BlenderDataConversion.h"
107
108 /** 
109 KX_BLENDERTRUNC needed to round 'almost' zero values to zero, else velocities etc. are incorrectly set 
110 */
111
112 #define KX_BLENDERTRUNC(x)  (( x < 0.0001 && x > -0.0001 )  ? 0.0 : x)
113
114 void BL_ConvertActuators(char* maggiename,
115                                                  struct Object* blenderobject,
116                                                  KX_GameObject* gameobj,
117                                                  SCA_LogicManager* logicmgr,
118                                                  KX_Scene* scene,
119                                                  KX_KetsjiEngine* ketsjiEngine,
120                                                  int activeLayerBitInfo,
121                                                  bool isInActiveLayer,
122                                                  RAS_IRenderTools* rendertools,
123                                                  KX_BlenderSceneConverter* converter
124                                                  )
125 {
126         
127         int uniqueint = 0;
128         int actcount = 0;
129         int executePriority = 0;
130         bActuator* bact = (bActuator*) blenderobject->actuators.first;
131         while (bact)
132         {
133                 actcount++;
134                 bact = bact->next;
135         }
136         gameobj->ReserveActuator(actcount);
137         bact = (bActuator*) blenderobject->actuators.first;
138         while(bact)
139         {
140                 STR_String uniquename = bact->name;
141                 STR_String& objectname = gameobj->GetName();
142                 
143                 SCA_IActuator* baseact = NULL;
144                 switch (bact->type)
145                 {
146                 case ACT_OBJECT:
147                         {
148                                 bObjectActuator* obact = (bObjectActuator*) bact->data;
149                                 KX_GameObject* obref = NULL;
150                                 MT_Vector3 forcevec(KX_BLENDERTRUNC(obact->forceloc[0]),
151                                         KX_BLENDERTRUNC(obact->forceloc[1]),
152                                         KX_BLENDERTRUNC(obact->forceloc[2]));
153                                 MT_Vector3 torquevec(obact->forcerot[0],obact->forcerot[1],obact->forcerot[2]);
154                                 MT_Vector3 dlocvec ( KX_BLENDERTRUNC(obact->dloc[0]),
155                                         KX_BLENDERTRUNC(obact->dloc[1]),
156                                         KX_BLENDERTRUNC(obact->dloc[2]));
157                                 MT_Vector3 drotvec ( KX_BLENDERTRUNC(obact->drot[0]),obact->drot[1],obact->drot[2]);
158                                 MT_Vector3 linvelvec ( KX_BLENDERTRUNC(obact->linearvelocity[0]),
159                                         KX_BLENDERTRUNC(obact->linearvelocity[1]),
160                                         KX_BLENDERTRUNC(obact->linearvelocity[2]));
161                                 MT_Vector3 angvelvec ( KX_BLENDERTRUNC(obact->angularvelocity[0]),
162                                         KX_BLENDERTRUNC(obact->angularvelocity[1]),
163                                         KX_BLENDERTRUNC(obact->angularvelocity[2]));
164                                 short damping = obact->damping;
165                                 
166                                 /* Blender uses a bit vector internally for the local-flags. In */
167                                 /* KX, we have four bools. The compiler should be smart enough  */
168                                 /* to do the right thing. We need to explicitly convert here!   */
169                                 
170                                 KX_LocalFlags bitLocalFlag;
171                                 
172                                 bitLocalFlag.Force = bool((obact->flag & ACT_FORCE_LOCAL)!=0);
173                                 bitLocalFlag.Torque = bool((obact->flag & ACT_TORQUE_LOCAL) !=0);//rlocal;
174                                 bitLocalFlag.DLoc = bool((obact->flag & ACT_DLOC_LOCAL)!=0);
175                                 bitLocalFlag.DRot = bool((obact->flag & ACT_DROT_LOCAL)!=0);
176                                 bitLocalFlag.LinearVelocity = bool((obact->flag & ACT_LIN_VEL_LOCAL)!=0);
177                                 bitLocalFlag.AngularVelocity = bool((obact->flag & ACT_ANG_VEL_LOCAL)!=0);
178                                 bitLocalFlag.ServoControl = bool(obact->type == ACT_OBJECT_SERVO);
179                                 bitLocalFlag.AddOrSetLinV = bool((obact->flag & ACT_ADD_LIN_VEL)!=0);
180                                 if (obact->reference && bitLocalFlag.ServoControl)
181                                 {
182                                         obref = converter->FindGameObject(obact->reference);
183                                 }
184                                 
185                                 KX_ObjectActuator* tmpbaseact = new KX_ObjectActuator(gameobj,
186                                         obref,
187                                         forcevec.getValue(),
188                                         torquevec.getValue(),
189                                         dlocvec.getValue(),
190                                         drotvec.getValue(),
191                                         linvelvec.getValue(),
192                                         angvelvec.getValue(),
193                                         damping,
194                                         bitLocalFlag
195                                         );
196                                 baseact = tmpbaseact;
197                                 break;
198                         }
199                 case ACT_ACTION:
200                         {
201                                 bActionActuator* actact = (bActionActuator*) bact->data;
202                                 STR_String propname = (actact->name ? actact->name : "");
203                                 STR_String propframe = (actact->frameProp ? actact->frameProp : "");
204
205                                 short ipo_flags = 0;
206
207                                 // Convert flags
208                                 if (actact->flag & ACT_IPOFORCE) ipo_flags |= BL_Action::ACT_IPOFLAG_FORCE;
209                                 if (actact->flag & ACT_IPOLOCAL) ipo_flags |= BL_Action::ACT_IPOFLAG_LOCAL;
210                                 if (actact->flag & ACT_IPOADD) ipo_flags |= BL_Action::ACT_IPOFLAG_ADD;
211                                 if (actact->flag & ACT_IPOCHILD) ipo_flags |= BL_Action::ACT_IPOFLAG_CHILD;
212                                         
213                                 BL_ActionActuator* tmpbaseact = new BL_ActionActuator(
214                                         gameobj,
215                                         propname,
216                                         propframe,
217                                         actact->sta,
218                                         actact->end,
219                                         actact->act,
220                                         actact->type, // + 1, because Blender starts to count at zero,
221                                         actact->blendin,
222                                         actact->priority,
223                                         actact->layer,
224                                         actact->layer_weight,
225                                         ipo_flags,
226                                         actact->end_reset,
227                                         actact->stridelength
228                                         // Ketsji at 1, because zero is reserved for "NoDef"
229                                         );
230                                 baseact= tmpbaseact;
231                                 break;
232                         }
233                 case ACT_SHAPEACTION:
234                         {
235                                 if (blenderobject->type==OB_MESH){
236                                         bActionActuator* actact = (bActionActuator*) bact->data;
237                                         STR_String propname = (actact->name ? actact->name : "");
238                                         STR_String propframe = (actact->frameProp ? actact->frameProp : "");
239                                         
240                                         BL_ShapeActionActuator* tmpbaseact = new BL_ShapeActionActuator(
241                                                 gameobj,
242                                                 propname,
243                                                 propframe,
244                                                 actact->sta,
245                                                 actact->end,
246                                                 actact->act,
247                                                 actact->type, // + 1, because Blender starts to count at zero,
248                                                 actact->blendin,
249                                                 actact->priority,
250                                                 actact->stridelength
251                                                 // Ketsji at 1, because zero is reserved for "NoDef"
252                                                 );
253                                         baseact= tmpbaseact;
254                                         break;
255                                 }
256                                 else
257                                         printf ("Discarded shape action actuator from non-mesh object [%s]\n", blenderobject->id.name+2);
258                         }
259                 case ACT_IPO:
260                         {
261                                 bIpoActuator* ipoact = (bIpoActuator*) bact->data;
262                                 bool ipochild = (ipoact->flag & ACT_IPOCHILD) !=0;
263                                 STR_String propname = ipoact->name;
264                                 STR_String frameProp = ipoact->frameProp;
265                                 // first bit?
266                                 bool ipo_as_force = (ipoact->flag & ACT_IPOFORCE);
267                                 bool local = (ipoact->flag & ACT_IPOLOCAL);
268                                 bool ipo_add = (ipoact->flag & ACT_IPOADD);
269                                 
270                                 KX_IpoActuator* tmpbaseact = new KX_IpoActuator(
271                                         gameobj,
272                                         propname ,
273                                         frameProp,
274                                         ipoact->sta,
275                                         ipoact->end,
276                                         ipochild,
277                                         ipoact->type + 1, // + 1, because Blender starts to count at zero,
278                                         // Ketsji at 1, because zero is reserved for "NoDef"
279                                         ipo_as_force,
280                                         ipo_add,
281                                         local);
282                                 baseact = tmpbaseact;
283                                 break;
284                         }
285                 case ACT_LAMP:
286                         {
287                                 break;
288                         }
289                 case ACT_CAMERA:
290                         {
291                                 bCameraActuator *camact = (bCameraActuator *) bact->data;
292                                 if (camact->ob) {
293                                         KX_GameObject *tmpgob = converter->FindGameObject(camact->ob);
294                                         
295                                         /* visifac, fac and axis are not copied from the struct...   */ 
296                                         /* that's some internal state...                             */
297                                         KX_CameraActuator *tmpcamact
298                                                 = new KX_CameraActuator(gameobj,
299                                                 tmpgob,
300                                                 camact->height,
301                                                 camact->min,
302                                                 camact->max,
303                                                 camact->axis=='x',
304                                                 camact->damping);
305                                         baseact = tmpcamact;
306                                 }
307                                 break;
308                         }
309                 case ACT_MESSAGE:
310                         {
311                                 bMessageActuator *msgAct = (bMessageActuator *) bact->data;
312                                 
313                                 /**
314                                 * Get the name of the properties that objects must own that
315                                 * we're sending to, if present
316                                 */
317                                 STR_String toPropName = (msgAct->toPropName
318                                         ? (char*) msgAct->toPropName
319                                         : "");
320                                 
321                                 /**
322                                 * Get the Message Subject to send.
323                                 */
324                                 STR_String subject = (msgAct->subject
325                                         ? (char*) msgAct->subject
326                                         : "");
327                                 
328                                 /**
329                                 * Get the bodyType
330                                 */
331                                 int bodyType = msgAct->bodyType;
332                                 
333                                 /**
334                                 * Get the body (text message or property name whose value
335                                 * we'll be sending, might be empty
336                                 */
337                                 STR_String body = (msgAct->body
338                                         ? (char*) msgAct->body
339                                         : "");
340                                 
341                                 KX_NetworkMessageActuator *tmpmsgact = 
342                                         new KX_NetworkMessageActuator(
343                                         gameobj,                                        // actuator controlling object
344                                         scene->GetNetworkScene(),       // needed for replication
345                                         toPropName,
346                                         subject,
347                                         bodyType,
348                                         body);
349                                 baseact = tmpmsgact;
350                                 break;
351                         }
352                 case ACT_MATERIAL:
353                         {
354                                 break;
355                         }
356                 case ACT_SOUND:
357                         {
358                                 bSoundActuator* soundact = (bSoundActuator*) bact->data;
359                                 /* get type, and possibly a start and end frame */
360                                 KX_SoundActuator::KX_SOUNDACT_TYPE
361                                         soundActuatorType = KX_SoundActuator::KX_SOUNDACT_NODEF;
362                                 
363                                 switch(soundact->type) {
364                                 case ACT_SND_PLAY_STOP_SOUND:
365                                         soundActuatorType = KX_SoundActuator::KX_SOUNDACT_PLAYSTOP;
366                                         break;
367                                 case ACT_SND_PLAY_END_SOUND:
368                                         soundActuatorType = KX_SoundActuator::KX_SOUNDACT_PLAYEND;
369                                         break;
370                                 case ACT_SND_LOOP_STOP_SOUND:
371                                         soundActuatorType = KX_SoundActuator::KX_SOUNDACT_LOOPSTOP;
372                                         break;
373                                 case ACT_SND_LOOP_END_SOUND:
374                                         soundActuatorType = KX_SoundActuator::KX_SOUNDACT_LOOPEND;
375                                         break;
376                                 case ACT_SND_LOOP_BIDIRECTIONAL_SOUND:
377                                         soundActuatorType = KX_SoundActuator::KX_SOUNDACT_LOOPBIDIRECTIONAL;
378                                         break;
379                                 case ACT_SND_LOOP_BIDIRECTIONAL_STOP_SOUND:
380                                         soundActuatorType = KX_SoundActuator::KX_SOUNDACT_LOOPBIDIRECTIONAL_STOP;
381                                         break;
382                                         
383                                 default:
384                                         /* This is an error!!! */
385                                         soundActuatorType = KX_SoundActuator::KX_SOUNDACT_NODEF;
386                                 }
387                                 
388                                 if (soundActuatorType != KX_SoundActuator::KX_SOUNDACT_NODEF) 
389                                 {
390                                         bSound* sound = soundact->sound;
391                                         bool is3d = soundact->flag & ACT_SND_3D_SOUND ? true : false;
392                                         AUD_Reference<AUD_IFactory> snd_sound;
393                                         KX_3DSoundSettings settings;
394                                         settings.cone_inner_angle = soundact->sound3D.cone_inner_angle;
395                                         settings.cone_outer_angle = soundact->sound3D.cone_outer_angle;
396                                         settings.cone_outer_gain = soundact->sound3D.cone_outer_gain;
397                                         settings.max_distance = soundact->sound3D.max_distance;
398                                         settings.max_gain = soundact->sound3D.max_gain;
399                                         settings.min_gain = soundact->sound3D.min_gain;
400                                         settings.reference_distance = soundact->sound3D.reference_distance;
401                                         settings.rolloff_factor = soundact->sound3D.rolloff_factor;
402
403                                         if(!sound)
404                                         {
405                                                 std::cout <<    "WARNING: Sound actuator \"" << bact->name <<
406                                                                                 "\" from object \"" <<  blenderobject->id.name+2 <<
407                                                                                 "\" has no sound datablock." << std::endl;
408                                         }
409                                         else
410                                         {
411                                                 snd_sound = *reinterpret_cast<AUD_Reference<AUD_IFactory>*>(sound->playback_handle);
412
413                                                 // if sound shall be 3D but isn't mono, we have to make it mono!
414                                                 if(is3d)
415                                                 {
416                                                         AUD_Reference<AUD_IReader> reader = snd_sound->createReader();
417                                                         if(reader->getSpecs().channels != AUD_CHANNELS_MONO)
418                                                         {
419                                                                 AUD_DeviceSpecs specs;
420                                                                 specs.channels = AUD_CHANNELS_MONO;
421                                                                 specs.rate = AUD_RATE_INVALID;
422                                                                 specs.format = AUD_FORMAT_INVALID;
423                                                                 snd_sound = new AUD_ChannelMapperFactory(snd_sound, specs);
424                                                         }
425                                                 }
426                                         }
427                                         KX_SoundActuator* tmpsoundact =
428                                                 new KX_SoundActuator(gameobj,
429                                                 snd_sound,
430                                                 soundact->volume,
431                                                 (float)(exp((soundact->pitch / 12.0) * M_LN2)),
432                                                 is3d,
433                                                 settings,
434                                                 soundActuatorType);
435
436                                         tmpsoundact->SetName(bact->name);
437                                         baseact = tmpsoundact;
438                                 }
439                                 break;
440                         }
441                 case ACT_PROPERTY:
442                         {
443                                 bPropertyActuator* propact = (bPropertyActuator*) bact->data;
444                                 SCA_IObject* destinationObj = NULL;
445                                 
446                                 /*
447                                 here the destinationobject is searched. problem with multiple scenes: other scenes
448                                 have not been converted yet, so the destobj will not be found, so the prop will
449                                 not be copied.
450                                 possible solutions:
451                                 - convert everything when possible and not realtime only when needed.
452                                 - let the object-with-property report itself to the act when converted
453                                 */
454                                 if (propact->ob)
455                                         destinationObj = converter->FindGameObject(propact->ob);
456                                 
457                                 SCA_PropertyActuator* tmppropact = new SCA_PropertyActuator(
458                                         gameobj,
459                                         destinationObj,
460                                         propact->name,
461                                         propact->value,
462                                         propact->type+1); // + 1 because Ketsji Logic starts
463                                 // with 0 for KX_ACT_PROP_NODEF
464                                 baseact = tmppropact;
465                                 break;
466                         }
467                 case ACT_EDIT_OBJECT:
468                         {
469                                 bEditObjectActuator *editobact 
470                                         = (bEditObjectActuator *) bact->data;
471                                 /* There are four different kinds of 'edit object' thingies  */
472                                 /* The alternative to this lengthy conversion is packing     */
473                                 /* several actuators in one, which is not very nice design.. */
474                                 switch (editobact->type) {
475                                 case ACT_EDOB_ADD_OBJECT: 
476                                         {
477                                                 
478                                                 // does the 'original' for replication exists, and 
479                                                 // is it in a non-active layer ?
480                                                 SCA_IObject* originalval = NULL;
481                                                 if (editobact->ob)
482                                                 {
483                                                         if (editobact->ob->lay & activeLayerBitInfo)
484                                                         {
485                                                                 fprintf(stderr, "Warning, object \"%s\" from AddObject actuator \"%s\" is not in a hidden layer.\n", objectname.Ptr(), uniquename.Ptr());
486                                                         }
487                                                         else {
488                                                                 originalval = converter->FindGameObject(editobact->ob);
489                                                         }
490                                                 }
491                                                 
492                                                 KX_SCA_AddObjectActuator* tmpaddact = 
493                                                         new KX_SCA_AddObjectActuator(
494                                                                 gameobj, 
495                                                                 originalval,
496                                                                 editobact->time,
497                                                                 scene,
498                                                                 editobact->linVelocity,
499                                                                 (editobact->localflag & ACT_EDOB_LOCAL_LINV)!=0,
500                                                                 editobact->angVelocity,
501                                                                 (editobact->localflag & ACT_EDOB_LOCAL_ANGV)!=0
502                                                                 );
503                                                                 
504                                                                 //editobact->ob to gameobj
505                                                                 baseact = tmpaddact;
506                                         }
507                                         break;
508                                 case ACT_EDOB_END_OBJECT:
509                                         {
510                                                 KX_SCA_EndObjectActuator* tmpendact 
511                                                         = new KX_SCA_EndObjectActuator(gameobj,scene);
512                                                 baseact = tmpendact;
513                                         }
514                                         break;
515                                 case ACT_EDOB_REPLACE_MESH:
516                                         {
517                                                 RAS_MeshObject *tmpmesh = NULL;
518                                                 if (editobact->me)
519                                                         tmpmesh = BL_ConvertMesh(
520                                                                 editobact->me,
521                                                                 blenderobject,
522                                                                 scene,
523                                                                 converter
524                                                                 );
525
526                                                 KX_SCA_ReplaceMeshActuator* tmpreplaceact
527                                                         = new KX_SCA_ReplaceMeshActuator(
528                                                                 gameobj,
529                                                                 tmpmesh,
530                                                                 scene,
531                                                                 (editobact->flag & ACT_EDOB_REPLACE_MESH_NOGFX)==0,
532                                                                 (editobact->flag & ACT_EDOB_REPLACE_MESH_PHYS)!=0
533                                                                 
534                                                                 );
535                                                         
536                                                         baseact = tmpreplaceact;
537                                         }
538                                         break;
539                                 case ACT_EDOB_TRACK_TO:
540                                         {
541                                                 SCA_IObject* originalval = NULL;
542                                                 if (editobact->ob)
543                                                         originalval = converter->FindGameObject(editobact->ob);
544                                                         
545                                                 KX_TrackToActuator* tmptrackact 
546                                                         = new KX_TrackToActuator(gameobj, 
547                                                                 originalval,
548                                                                 editobact->time,
549                                                                 editobact->flag,
550                                                                 blenderobject->trackflag,
551                                                                 blenderobject->upflag
552                                                                 );
553                                                         baseact = tmptrackact;
554                                                 break;
555                                         }
556                                 case ACT_EDOB_DYNAMICS:
557                                         {
558                                                 KX_SCA_DynamicActuator* tmpdynact 
559                                                         = new KX_SCA_DynamicActuator(gameobj, 
560                                                                 editobact->dyn_operation,
561                                                                 editobact->mass
562                                                                 );
563                                                         baseact = tmpdynact;
564                                         }
565                                 }
566                                 break;
567                         }
568                 case ACT_CONSTRAINT:
569                         {
570                                 float min = 0.0, max = 0.0;
571                                 char *prop = NULL;
572                                 KX_ConstraintActuator::KX_CONSTRAINTTYPE locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_NODEF;
573                                 bConstraintActuator *conact 
574                                         = (bConstraintActuator*) bact->data;
575                                 /* convert settings... degrees in the ui become radians  */ 
576                                 /* internally                                            */ 
577                                 if (conact->type == ACT_CONST_TYPE_ORI) {
578                                         min = (float)((MT_2_PI * conact->minloc[0])/360.0);
579                                         max = (float)((MT_2_PI * conact->maxloc[0])/360.0);
580                                         switch (conact->mode) {
581                                         case ACT_CONST_DIRPX:
582                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_ORIX;
583                                                 break;
584                                         case ACT_CONST_DIRPY:
585                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_ORIY;
586                                                 break;
587                                         case ACT_CONST_DIRPZ:
588                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_ORIZ;
589                                                 break;
590                                         }
591                                 } else if (conact->type == ACT_CONST_TYPE_DIST) {
592                                         switch (conact->mode) {
593                                         case ACT_CONST_DIRPX:
594                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRPX;
595                                                 min = conact->minloc[0];
596                                                 max = conact->maxloc[0];
597                                                 break;
598                                         case ACT_CONST_DIRPY:
599                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRPY;
600                                                 min = conact->minloc[1];
601                                                 max = conact->maxloc[1];
602                                                 break;
603                                         case ACT_CONST_DIRPZ:
604                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRPZ;
605                                                 min = conact->minloc[2];
606                                                 max = conact->maxloc[2];
607                                                 break;
608                                         case ACT_CONST_DIRNX:
609                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRNX;
610                                                 min = conact->minloc[0];
611                                                 max = conact->maxloc[0];
612                                                 break;
613                                         case ACT_CONST_DIRNY:
614                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRNY;
615                                                 min = conact->minloc[1];
616                                                 max = conact->maxloc[1];
617                                                 break;
618                                         case ACT_CONST_DIRNZ:
619                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRNZ;
620                                                 min = conact->minloc[2];
621                                                 max = conact->maxloc[2];
622                                                 break;
623                                         }
624                                         prop = conact->matprop;
625                                 } else if (conact->type == ACT_CONST_TYPE_FH) {
626                                         switch (conact->mode) {
627                                         case ACT_CONST_DIRPX:
628                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_FHPX;
629                                                 min = conact->minloc[0];
630                                                 max = conact->maxloc[0];
631                                                 break;
632                                         case ACT_CONST_DIRPY:
633                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_FHPY;
634                                                 min = conact->minloc[1];
635                                                 max = conact->maxloc[1];
636                                                 break;
637                                         case ACT_CONST_DIRPZ:
638                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_FHPZ;
639                                                 min = conact->minloc[2];
640                                                 max = conact->maxloc[2];
641                                                 break;
642                                         case ACT_CONST_DIRNX:
643                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_FHNX;
644                                                 min = conact->minloc[0];
645                                                 max = conact->maxloc[0];
646                                                 break;
647                                         case ACT_CONST_DIRNY:
648                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_FHNY;
649                                                 min = conact->minloc[1];
650                                                 max = conact->maxloc[1];
651                                                 break;
652                                         case ACT_CONST_DIRNZ:
653                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_FHNZ;
654                                                 min = conact->minloc[2];
655                                                 max = conact->maxloc[2];
656                                                 break;
657                                         }
658                                         prop = conact->matprop;
659                                 } else {
660                                         switch (conact->flag) {
661                                         case ACT_CONST_LOCX:
662                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCX; 
663                                                 min = conact->minloc[0];
664                                                 max = conact->maxloc[0];
665                                                 break;
666                                         case ACT_CONST_LOCY:
667                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCY; 
668                                                 min = conact->minloc[1];
669                                                 max = conact->maxloc[1];
670                                                 break;
671                                         case ACT_CONST_LOCZ:
672                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCZ;
673                                                 min = conact->minloc[2];
674                                                 max = conact->maxloc[2];
675                                                 break;
676                                         case ACT_CONST_ROTX:
677                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTX;
678                                                 min = conact->minrot[0] * (float)MT_RADS_PER_DEG;
679                                                 max = conact->maxrot[0] * (float)MT_RADS_PER_DEG;
680                                                 break;
681                                         case ACT_CONST_ROTY:
682                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTY;
683                                                 min = conact->minrot[1] * (float)MT_RADS_PER_DEG;
684                                                 max = conact->maxrot[1] * (float)MT_RADS_PER_DEG;
685                                                 break;
686                                         case ACT_CONST_ROTZ:
687                                                 locrot = KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTZ;
688                                                 min = conact->minrot[2] * (float)MT_RADS_PER_DEG;
689                                                 max = conact->maxrot[2] * (float)MT_RADS_PER_DEG;
690                                                 break;
691                                         default:
692                                                 ; /* error */ 
693                                         }
694                                 }
695                                 KX_ConstraintActuator *tmpconact 
696                                         = new KX_ConstraintActuator(gameobj,
697                                                 conact->damp,
698                                                 conact->rotdamp,
699                                                 min,
700                                                 max,
701                                                 conact->maxrot,
702                                                 locrot,
703                                                 conact->time,
704                                                 conact->flag,
705                                                 prop);
706                                 baseact = tmpconact;
707                                 break;
708                         }
709                 case ACT_GROUP:
710                         {
711                                 // deprecated
712                         }
713                         break;
714                 case ACT_SCENE:
715                         {
716                                 bSceneActuator *sceneact = (bSceneActuator *) bact->data;
717                                 STR_String nextSceneName("");
718                                 
719                                 KX_SceneActuator* tmpsceneact;
720                                 int mode = KX_SceneActuator::KX_SCENE_NODEF;
721                                 KX_Camera *cam = NULL;
722                                 //KX_Scene* scene = NULL;
723                                 switch (sceneact->type)
724                                 {
725                                 case ACT_SCENE_RESUME:
726                                 case ACT_SCENE_SUSPEND:
727                                 case ACT_SCENE_ADD_FRONT:
728                                 case ACT_SCENE_ADD_BACK:
729                                 case ACT_SCENE_REMOVE:
730                                 case ACT_SCENE_SET:
731                                         {
732                                                 switch (sceneact->type)
733                                                 {
734                                                 case ACT_SCENE_RESUME:
735                                                         mode = KX_SceneActuator::KX_SCENE_RESUME;
736                                                         break;
737                                                 case ACT_SCENE_SUSPEND:
738                                                         mode = KX_SceneActuator::KX_SCENE_SUSPEND;
739                                                         break;
740                                                 case ACT_SCENE_ADD_FRONT:
741                                                         mode = KX_SceneActuator::KX_SCENE_ADD_FRONT_SCENE;
742                                                         break;
743                                                 case ACT_SCENE_ADD_BACK:
744                                                         mode = KX_SceneActuator::KX_SCENE_ADD_BACK_SCENE;
745                                                         break;
746                                                 case ACT_SCENE_REMOVE:
747                                                         mode = KX_SceneActuator::KX_SCENE_REMOVE_SCENE;
748                                                         break;
749                                                 case ACT_SCENE_SET:
750                                                 default:
751                                                         mode = KX_SceneActuator::KX_SCENE_SET_SCENE;
752                                                         break;
753                                                 };
754                                                 
755                                                 if (sceneact->scene)
756                                                 {
757                                                         nextSceneName = sceneact->scene->id.name + 2; // this '2' is necessary to remove prefix 'SC'
758                                                 }
759                                                 
760                                                 break;
761                                         }
762                                 case ACT_SCENE_CAMERA:
763                                         mode = KX_SceneActuator::KX_SCENE_SET_CAMERA;
764                                         if (sceneact->camera)
765                                         {
766                                                 cam = (KX_Camera*) converter->FindGameObject(sceneact->camera);
767                                         }
768                                         break;
769                                 case ACT_SCENE_RESTART:
770                                         {
771                                                 
772                                                 mode =  KX_SceneActuator::KX_SCENE_RESTART;
773                                                 break;
774                                         }
775                                 default:
776                                         ; /* flag error */
777                                 }
778                                 tmpsceneact = new KX_SceneActuator(gameobj,
779                                                 mode,
780                                                 scene,
781                                                 ketsjiEngine,
782                                                 nextSceneName,
783                                                 cam);
784                                         baseact = tmpsceneact;
785                                 break;
786                         }
787                 case ACT_GAME:
788                         {
789                                 bGameActuator *gameact = (bGameActuator *) bact->data;
790                                 KX_GameActuator* tmpgameact;
791                                 STR_String filename = maggiename;
792                                 STR_String loadinganimationname = "";
793                                 int mode = KX_GameActuator::KX_GAME_NODEF;
794                                 switch (gameact->type)
795                                 {
796                                 case ACT_GAME_LOAD:
797                                         {
798                                                 mode = KX_GameActuator::KX_GAME_LOAD;
799                                                 filename = gameact->filename;
800                                                 loadinganimationname = gameact->loadaniname;
801                                                 break;
802                                         }
803                                 case ACT_GAME_START:
804                                         {
805                                                 mode = KX_GameActuator::KX_GAME_START;
806                                                 filename = gameact->filename;
807                                                 loadinganimationname = gameact->loadaniname;
808                                                 break;
809                                         }
810                                 case ACT_GAME_RESTART:
811                                         {
812                                                 mode = KX_GameActuator::KX_GAME_RESTART;
813                                                 break;
814                                         }
815                                 case ACT_GAME_QUIT:
816                                         {
817                                                 mode = KX_GameActuator::KX_GAME_QUIT;
818                                                 break;
819                                         }
820                                 case ACT_GAME_SAVECFG:
821                                         {
822                                                 mode = KX_GameActuator::KX_GAME_SAVECFG;
823                                                 break;
824                                         }
825                                 case ACT_GAME_LOADCFG:
826                                         {
827                                                 mode = KX_GameActuator::KX_GAME_LOADCFG;
828                                                 break;
829                                         }
830                                 default:
831                                         ; /* flag error */
832                                 }
833                                         tmpgameact = new KX_GameActuator(gameobj,
834                                                 mode,
835                                                 filename,
836                                                 loadinganimationname,
837                                                 scene,
838                                                 ketsjiEngine);
839                                         baseact = tmpgameact;
840
841                                 break;
842                         }
843                 case ACT_RANDOM:
844                         {
845                                 bRandomActuator *randAct 
846                                         = (bRandomActuator *) bact->data;
847                                 
848                                 unsigned long seedArg = randAct->seed;
849                                 if (seedArg == 0)
850                                 {
851                                         seedArg = (int)(ketsjiEngine->GetRealTime()*100000.0);
852                                         seedArg ^= (intptr_t)randAct;
853                                 }
854                                 SCA_RandomActuator::KX_RANDOMACT_MODE modeArg 
855                                         = SCA_RandomActuator::KX_RANDOMACT_NODEF;
856                                 SCA_RandomActuator *tmprandomact;
857                                 float paraArg1 = 0.0;
858                                 float paraArg2 = 0.0;
859                                 
860                                 switch  (randAct->distribution) {
861                                 case ACT_RANDOM_BOOL_CONST:
862                                         modeArg = SCA_RandomActuator::KX_RANDOMACT_BOOL_CONST;
863                                         paraArg1 = (float) randAct->int_arg_1;
864                                         break;
865                                 case ACT_RANDOM_BOOL_UNIFORM:
866                                         modeArg = SCA_RandomActuator::KX_RANDOMACT_BOOL_UNIFORM;
867                                         break;
868                                 case ACT_RANDOM_BOOL_BERNOUILLI:
869                                         paraArg1 = randAct->float_arg_1;
870                                         modeArg = SCA_RandomActuator::KX_RANDOMACT_BOOL_BERNOUILLI;
871                                         break;
872                                 case ACT_RANDOM_INT_CONST:
873                                         modeArg = SCA_RandomActuator::KX_RANDOMACT_INT_CONST;
874                                         paraArg1 = (float) randAct->int_arg_1;
875                                         break;
876                                 case ACT_RANDOM_INT_UNIFORM:
877                                         paraArg1 = (float) randAct->int_arg_1;
878                                         paraArg2 = (float) randAct->int_arg_2;
879                                         modeArg = SCA_RandomActuator::KX_RANDOMACT_INT_UNIFORM;
880                                         break;
881                                 case ACT_RANDOM_INT_POISSON:
882                                         paraArg1 = randAct->float_arg_1;
883                                         modeArg = SCA_RandomActuator::KX_RANDOMACT_INT_POISSON;
884                                         break;
885                                 case ACT_RANDOM_FLOAT_CONST:
886                                         paraArg1 = randAct->float_arg_1;
887                                         modeArg = SCA_RandomActuator::KX_RANDOMACT_FLOAT_CONST;
888                                         break;
889                                 case ACT_RANDOM_FLOAT_UNIFORM:
890                                         paraArg1 = randAct->float_arg_1;
891                                         paraArg2 = randAct->float_arg_2;
892                                         modeArg = SCA_RandomActuator::KX_RANDOMACT_FLOAT_UNIFORM;
893                                         break;
894                                 case ACT_RANDOM_FLOAT_NORMAL:
895                                         paraArg1 = randAct->float_arg_1;
896                                         paraArg2 = randAct->float_arg_2;
897                                         modeArg = SCA_RandomActuator::KX_RANDOMACT_FLOAT_NORMAL;
898                                         break;
899                                 case ACT_RANDOM_FLOAT_NEGATIVE_EXPONENTIAL:
900                                         paraArg1 = randAct->float_arg_1;
901                                         modeArg = SCA_RandomActuator::KX_RANDOMACT_FLOAT_NEGATIVE_EXPONENTIAL;
902                                         break;
903                                 default:
904                                         ; /* error */                           
905                                 }
906                                 tmprandomact = new SCA_RandomActuator(gameobj,
907                                         seedArg,
908                                         modeArg, 
909                                         paraArg1,
910                                         paraArg2,
911                                         randAct->propname);
912                                 baseact = tmprandomact;
913                         }
914                         break;
915
916                 case ACT_VISIBILITY:
917                 {
918                         bVisibilityActuator *vis_act = (bVisibilityActuator *) bact->data;
919                         KX_VisibilityActuator * tmp_vis_act = NULL;
920                         bool v = ((vis_act->flag & ACT_VISIBILITY_INVISIBLE) != 0);
921                         bool o = ((vis_act->flag & ACT_VISIBILITY_OCCLUSION) != 0);
922                         bool recursive = ((vis_act->flag & ACT_VISIBILITY_RECURSIVE) != 0);
923
924                         tmp_vis_act = new KX_VisibilityActuator(gameobj, !v, o, recursive);
925                         
926                         baseact = tmp_vis_act;
927                 }
928                 break;
929
930                 case ACT_STATE:
931                 {
932                         bStateActuator *sta_act = (bStateActuator *) bact->data;
933                         KX_StateActuator * tmp_sta_act = NULL;
934
935                         tmp_sta_act = 
936                                 new KX_StateActuator(gameobj, sta_act->type, sta_act->mask);
937                         
938                         baseact = tmp_sta_act;
939                 }
940                 break;
941
942                 case ACT_2DFILTER:
943                 {
944                         bTwoDFilterActuator *_2dfilter = (bTwoDFilterActuator*) bact->data;
945                         SCA_2DFilterActuator *tmp = NULL;
946
947                         RAS_2DFilterManager::RAS_2DFILTER_MODE filtermode;
948                         switch(_2dfilter->type)
949                         {
950                                 case ACT_2DFILTER_MOTIONBLUR:
951                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_MOTIONBLUR;
952                                         break;
953                                 case ACT_2DFILTER_BLUR:
954                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_BLUR;
955                                         break;
956                                 case ACT_2DFILTER_SHARPEN:
957                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_SHARPEN;
958                                         break;
959                                 case ACT_2DFILTER_DILATION:
960                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_DILATION;
961                                         break;
962                                 case ACT_2DFILTER_EROSION:
963                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_EROSION;
964                                         break;
965                                 case ACT_2DFILTER_LAPLACIAN:
966                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_LAPLACIAN;
967                                         break;
968                                 case ACT_2DFILTER_SOBEL:
969                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_SOBEL;
970                                         break;
971                                 case ACT_2DFILTER_PREWITT:
972                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_PREWITT;
973                                         break;
974                                 case ACT_2DFILTER_GRAYSCALE:
975                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_GRAYSCALE;
976                                         break;
977                                 case ACT_2DFILTER_SEPIA:
978                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_SEPIA;
979                                         break;
980                                 case ACT_2DFILTER_INVERT:
981                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_INVERT;
982                                         break;
983                                 case ACT_2DFILTER_CUSTOMFILTER:
984                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_CUSTOMFILTER;
985                                         break;
986                                 case ACT_2DFILTER_NOFILTER:
987                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_NOFILTER;
988                                         break;
989                                 case ACT_2DFILTER_DISABLED:
990                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_DISABLED;
991                                         break;
992                                 case ACT_2DFILTER_ENABLED:
993                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_ENABLED;
994                                         break;
995                                 default:
996                                         filtermode = RAS_2DFilterManager::RAS_2DFILTER_NOFILTER;
997                                         break;
998                         }
999
1000                         tmp = new SCA_2DFilterActuator(gameobj, filtermode, _2dfilter->flag,
1001                                 _2dfilter->float_arg,_2dfilter->int_arg,ketsjiEngine->GetRasterizer(),scene);
1002
1003                         if (_2dfilter->text)
1004                         {
1005                                 char *buf;
1006                                 // this is some blender specific code
1007                                 buf = txt_to_buf(_2dfilter->text);
1008                                 if (buf)
1009                                 {
1010                                         tmp->SetShaderText(buf);
1011                                         MEM_freeN(buf);
1012                                 }
1013                         }
1014
1015                         baseact = tmp;
1016
1017                 }
1018                 break;
1019                 case ACT_PARENT:
1020                         {
1021                                 bParentActuator *parAct = (bParentActuator *) bact->data;
1022                                 int mode = KX_ParentActuator::KX_PARENT_NODEF;
1023                                 bool addToCompound = true;
1024                                 bool ghost = true;
1025                                 KX_GameObject *tmpgob = NULL;
1026
1027                                 switch(parAct->type)
1028                                 {
1029                                         case ACT_PARENT_SET:
1030                                                 mode = KX_ParentActuator::KX_PARENT_SET;
1031                                                 tmpgob = converter->FindGameObject(parAct->ob);
1032                                                 addToCompound = !(parAct->flag & ACT_PARENT_COMPOUND);
1033                                                 ghost = !(parAct->flag & ACT_PARENT_GHOST);
1034                                                 break;
1035                                         case ACT_PARENT_REMOVE:
1036                                                 mode = KX_ParentActuator::KX_PARENT_REMOVE;
1037                                                 tmpgob = NULL;
1038                                                 break;
1039                                 }
1040         
1041                                 KX_ParentActuator *tmpparact
1042                                         = new KX_ParentActuator(gameobj,
1043                                         mode,
1044                                         addToCompound,
1045                                         ghost,
1046                                         tmpgob);
1047                                 baseact = tmpparact;
1048                                 break;
1049                         }
1050                 
1051                 case ACT_ARMATURE:
1052                         {
1053                                 bArmatureActuator* armAct = (bArmatureActuator*) bact->data;
1054                                 KX_GameObject *tmpgob = converter->FindGameObject(armAct->target);
1055                                 KX_GameObject *subgob = converter->FindGameObject(armAct->subtarget);
1056                                 BL_ArmatureActuator* tmparmact = new BL_ArmatureActuator(gameobj, armAct->type, armAct->posechannel, armAct->constraint, tmpgob, subgob, armAct->weight);
1057                                 baseact = tmparmact;
1058                                 break;
1059                         }
1060                 default:
1061                         ; /* generate some error */
1062                 }
1063                 
1064                 if (baseact)
1065                 {
1066                         baseact->SetExecutePriority(executePriority++);
1067                         uniquename += "#ACT#";
1068                         uniqueint++;
1069                         CIntValue* uniqueval = new CIntValue(uniqueint);
1070                         uniquename += uniqueval->GetText();
1071                         uniqueval->Release();
1072                         baseact->SetName(bact->name);
1073                         //gameobj->SetProperty(uniquename,baseact);
1074                         gameobj->AddActuator(baseact);
1075                         
1076                         converter->RegisterGameActuator(baseact, bact);
1077                         // done with baseact, release it
1078                         baseact->Release();
1079                 }
1080                 
1081                 bact = bact->next;
1082         }
1083 }
1084
1085